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