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