xref: /linux/drivers/gpu/drm/sti/sti_drv.c (revision 0526b56cbc3c489642bd6a5fe4b718dea7ef0ee8)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) STMicroelectronics SA 2014
4  * Author: Benjamin Gaignard <benjamin.gaignard@st.com> for STMicroelectronics.
5  */
6 
7 #include <linux/component.h>
8 #include <linux/dma-mapping.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/of_platform.h>
12 
13 #include <drm/drm_atomic.h>
14 #include <drm/drm_atomic_helper.h>
15 #include <drm/drm_debugfs.h>
16 #include <drm/drm_drv.h>
17 #include <drm/drm_fbdev_dma.h>
18 #include <drm/drm_gem_dma_helper.h>
19 #include <drm/drm_gem_framebuffer_helper.h>
20 #include <drm/drm_of.h>
21 #include <drm/drm_probe_helper.h>
22 
23 #include "sti_drv.h"
24 #include "sti_plane.h"
25 
26 #define DRIVER_NAME	"sti"
27 #define DRIVER_DESC	"STMicroelectronics SoC DRM"
28 #define DRIVER_DATE	"20140601"
29 #define DRIVER_MAJOR	1
30 #define DRIVER_MINOR	0
31 
32 #define STI_MAX_FB_HEIGHT	4096
33 #define STI_MAX_FB_WIDTH	4096
34 
35 static int sti_drm_fps_get(void *data, u64 *val)
36 {
37 	struct drm_device *drm_dev = data;
38 	struct drm_plane *p;
39 	unsigned int i = 0;
40 
41 	*val = 0;
42 	list_for_each_entry(p, &drm_dev->mode_config.plane_list, head) {
43 		struct sti_plane *plane = to_sti_plane(p);
44 
45 		*val |= plane->fps_info.output << i;
46 		i++;
47 	}
48 
49 	return 0;
50 }
51 
52 static int sti_drm_fps_set(void *data, u64 val)
53 {
54 	struct drm_device *drm_dev = data;
55 	struct drm_plane *p;
56 	unsigned int i = 0;
57 
58 	list_for_each_entry(p, &drm_dev->mode_config.plane_list, head) {
59 		struct sti_plane *plane = to_sti_plane(p);
60 
61 		memset(&plane->fps_info, 0, sizeof(plane->fps_info));
62 		plane->fps_info.output = (val >> i) & 1;
63 
64 		i++;
65 	}
66 
67 	return 0;
68 }
69 
70 DEFINE_SIMPLE_ATTRIBUTE(sti_drm_fps_fops,
71 			sti_drm_fps_get, sti_drm_fps_set, "%llu\n");
72 
73 static int sti_drm_fps_dbg_show(struct seq_file *s, void *data)
74 {
75 	struct drm_info_node *node = s->private;
76 	struct drm_device *dev = node->minor->dev;
77 	struct drm_plane *p;
78 
79 	list_for_each_entry(p, &dev->mode_config.plane_list, head) {
80 		struct sti_plane *plane = to_sti_plane(p);
81 
82 		seq_printf(s, "%s%s\n",
83 			   plane->fps_info.fps_str,
84 			   plane->fps_info.fips_str);
85 	}
86 
87 	return 0;
88 }
89 
90 static struct drm_info_list sti_drm_dbg_list[] = {
91 	{"fps_get", sti_drm_fps_dbg_show, 0},
92 };
93 
94 static void sti_drm_dbg_init(struct drm_minor *minor)
95 {
96 	drm_debugfs_create_files(sti_drm_dbg_list,
97 				 ARRAY_SIZE(sti_drm_dbg_list),
98 				 minor->debugfs_root, minor);
99 
100 	debugfs_create_file("fps_show", S_IRUGO | S_IWUSR, minor->debugfs_root,
101 			    minor->dev, &sti_drm_fps_fops);
102 
103 	DRM_INFO("%s: debugfs installed\n", DRIVER_NAME);
104 }
105 
106 static const struct drm_mode_config_funcs sti_mode_config_funcs = {
107 	.fb_create = drm_gem_fb_create,
108 	.atomic_check = drm_atomic_helper_check,
109 	.atomic_commit = drm_atomic_helper_commit,
110 };
111 
112 static void sti_mode_config_init(struct drm_device *dev)
113 {
114 	dev->mode_config.min_width = 0;
115 	dev->mode_config.min_height = 0;
116 
117 	/*
118 	 * set max width and height as default value.
119 	 * this value would be used to check framebuffer size limitation
120 	 * at drm_mode_addfb().
121 	 */
122 	dev->mode_config.max_width = STI_MAX_FB_WIDTH;
123 	dev->mode_config.max_height = STI_MAX_FB_HEIGHT;
124 
125 	dev->mode_config.funcs = &sti_mode_config_funcs;
126 
127 	dev->mode_config.normalize_zpos = true;
128 }
129 
130 DEFINE_DRM_GEM_DMA_FOPS(sti_driver_fops);
131 
132 static const struct drm_driver sti_driver = {
133 	.driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
134 	.fops = &sti_driver_fops,
135 	DRM_GEM_DMA_DRIVER_OPS,
136 
137 	.debugfs_init = sti_drm_dbg_init,
138 
139 	.name = DRIVER_NAME,
140 	.desc = DRIVER_DESC,
141 	.date = DRIVER_DATE,
142 	.major = DRIVER_MAJOR,
143 	.minor = DRIVER_MINOR,
144 };
145 
146 static int sti_init(struct drm_device *ddev)
147 {
148 	struct sti_private *private;
149 
150 	private = kzalloc(sizeof(*private), GFP_KERNEL);
151 	if (!private)
152 		return -ENOMEM;
153 
154 	ddev->dev_private = (void *)private;
155 	dev_set_drvdata(ddev->dev, ddev);
156 	private->drm_dev = ddev;
157 
158 	drm_mode_config_init(ddev);
159 
160 	sti_mode_config_init(ddev);
161 
162 	drm_kms_helper_poll_init(ddev);
163 
164 	return 0;
165 }
166 
167 static void sti_cleanup(struct drm_device *ddev)
168 {
169 	struct sti_private *private = ddev->dev_private;
170 
171 	drm_kms_helper_poll_fini(ddev);
172 	drm_atomic_helper_shutdown(ddev);
173 	drm_mode_config_cleanup(ddev);
174 	component_unbind_all(ddev->dev, ddev);
175 	kfree(private);
176 	ddev->dev_private = NULL;
177 }
178 
179 static int sti_bind(struct device *dev)
180 {
181 	struct drm_device *ddev;
182 	int ret;
183 
184 	ddev = drm_dev_alloc(&sti_driver, dev);
185 	if (IS_ERR(ddev))
186 		return PTR_ERR(ddev);
187 
188 	ret = sti_init(ddev);
189 	if (ret)
190 		goto err_drm_dev_put;
191 
192 	ret = component_bind_all(ddev->dev, ddev);
193 	if (ret)
194 		goto err_cleanup;
195 
196 	ret = drm_dev_register(ddev, 0);
197 	if (ret)
198 		goto err_cleanup;
199 
200 	drm_mode_config_reset(ddev);
201 
202 	drm_fbdev_dma_setup(ddev, 32);
203 
204 	return 0;
205 
206 err_cleanup:
207 	sti_cleanup(ddev);
208 err_drm_dev_put:
209 	drm_dev_put(ddev);
210 	return ret;
211 }
212 
213 static void sti_unbind(struct device *dev)
214 {
215 	struct drm_device *ddev = dev_get_drvdata(dev);
216 
217 	drm_dev_unregister(ddev);
218 	sti_cleanup(ddev);
219 	drm_dev_put(ddev);
220 }
221 
222 static const struct component_master_ops sti_ops = {
223 	.bind = sti_bind,
224 	.unbind = sti_unbind,
225 };
226 
227 static int sti_platform_probe(struct platform_device *pdev)
228 {
229 	struct device *dev = &pdev->dev;
230 	struct device_node *node = dev->of_node;
231 	struct device_node *child_np;
232 	struct component_match *match = NULL;
233 
234 	dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
235 
236 	devm_of_platform_populate(dev);
237 
238 	child_np = of_get_next_available_child(node, NULL);
239 
240 	while (child_np) {
241 		drm_of_component_match_add(dev, &match, component_compare_of,
242 					   child_np);
243 		child_np = of_get_next_available_child(node, child_np);
244 	}
245 
246 	return component_master_add_with_match(dev, &sti_ops, match);
247 }
248 
249 static int sti_platform_remove(struct platform_device *pdev)
250 {
251 	component_master_del(&pdev->dev, &sti_ops);
252 
253 	return 0;
254 }
255 
256 static const struct of_device_id sti_dt_ids[] = {
257 	{ .compatible = "st,sti-display-subsystem", },
258 	{ /* end node */ },
259 };
260 MODULE_DEVICE_TABLE(of, sti_dt_ids);
261 
262 static struct platform_driver sti_platform_driver = {
263 	.probe = sti_platform_probe,
264 	.remove = sti_platform_remove,
265 	.driver = {
266 		.name = DRIVER_NAME,
267 		.of_match_table = sti_dt_ids,
268 	},
269 };
270 
271 static struct platform_driver * const drivers[] = {
272 	&sti_tvout_driver,
273 	&sti_hqvdp_driver,
274 	&sti_hdmi_driver,
275 	&sti_hda_driver,
276 	&sti_dvo_driver,
277 	&sti_vtg_driver,
278 	&sti_compositor_driver,
279 	&sti_platform_driver,
280 };
281 
282 static int sti_drm_init(void)
283 {
284 	if (drm_firmware_drivers_only())
285 		return -ENODEV;
286 
287 	return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
288 }
289 module_init(sti_drm_init);
290 
291 static void sti_drm_exit(void)
292 {
293 	platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
294 }
295 module_exit(sti_drm_exit);
296 
297 MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
298 MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
299 MODULE_LICENSE("GPL");
300