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