xref: /linux/drivers/gpu/drm/sti/sti_drv.c (revision 45d8b572fac3aa8b49d53c946b3685eaf78a2824)
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.h>
12 #include <linux/of_platform.h>
13 #include <linux/platform_device.h>
14 
15 #include <drm/drm_atomic.h>
16 #include <drm/drm_atomic_helper.h>
17 #include <drm/drm_debugfs.h>
18 #include <drm/drm_drv.h>
19 #include <drm/drm_fbdev_dma.h>
20 #include <drm/drm_gem_dma_helper.h>
21 #include <drm/drm_gem_framebuffer_helper.h>
22 #include <drm/drm_of.h>
23 #include <drm/drm_probe_helper.h>
24 
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 void sti_drm_dbg_init(struct drm_minor *minor)
97 {
98 	drm_debugfs_create_files(sti_drm_dbg_list,
99 				 ARRAY_SIZE(sti_drm_dbg_list),
100 				 minor->debugfs_root, minor);
101 
102 	debugfs_create_file("fps_show", S_IRUGO | S_IWUSR, minor->debugfs_root,
103 			    minor->dev, &sti_drm_fps_fops);
104 
105 	DRM_INFO("%s: debugfs installed\n", DRIVER_NAME);
106 }
107 
108 static const struct drm_mode_config_funcs sti_mode_config_funcs = {
109 	.fb_create = drm_gem_fb_create,
110 	.atomic_check = drm_atomic_helper_check,
111 	.atomic_commit = drm_atomic_helper_commit,
112 };
113 
114 static void sti_mode_config_init(struct drm_device *dev)
115 {
116 	dev->mode_config.min_width = 0;
117 	dev->mode_config.min_height = 0;
118 
119 	/*
120 	 * set max width and height as default value.
121 	 * this value would be used to check framebuffer size limitation
122 	 * at drm_mode_addfb().
123 	 */
124 	dev->mode_config.max_width = STI_MAX_FB_WIDTH;
125 	dev->mode_config.max_height = STI_MAX_FB_HEIGHT;
126 
127 	dev->mode_config.funcs = &sti_mode_config_funcs;
128 
129 	dev->mode_config.normalize_zpos = true;
130 }
131 
132 DEFINE_DRM_GEM_DMA_FOPS(sti_driver_fops);
133 
134 static const struct drm_driver sti_driver = {
135 	.driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
136 	.fops = &sti_driver_fops,
137 	DRM_GEM_DMA_DRIVER_OPS,
138 
139 	.debugfs_init = sti_drm_dbg_init,
140 
141 	.name = DRIVER_NAME,
142 	.desc = DRIVER_DESC,
143 	.date = DRIVER_DATE,
144 	.major = DRIVER_MAJOR,
145 	.minor = DRIVER_MINOR,
146 };
147 
148 static int sti_init(struct drm_device *ddev)
149 {
150 	struct sti_private *private;
151 
152 	private = kzalloc(sizeof(*private), GFP_KERNEL);
153 	if (!private)
154 		return -ENOMEM;
155 
156 	ddev->dev_private = (void *)private;
157 	dev_set_drvdata(ddev->dev, ddev);
158 	private->drm_dev = ddev;
159 
160 	drm_mode_config_init(ddev);
161 
162 	sti_mode_config_init(ddev);
163 
164 	drm_kms_helper_poll_init(ddev);
165 
166 	return 0;
167 }
168 
169 static void sti_cleanup(struct drm_device *ddev)
170 {
171 	struct sti_private *private = ddev->dev_private;
172 
173 	drm_kms_helper_poll_fini(ddev);
174 	drm_atomic_helper_shutdown(ddev);
175 	drm_mode_config_cleanup(ddev);
176 	component_unbind_all(ddev->dev, ddev);
177 	dev_set_drvdata(ddev->dev, NULL);
178 	kfree(private);
179 	ddev->dev_private = NULL;
180 }
181 
182 static int sti_bind(struct device *dev)
183 {
184 	struct drm_device *ddev;
185 	int ret;
186 
187 	ddev = drm_dev_alloc(&sti_driver, dev);
188 	if (IS_ERR(ddev))
189 		return PTR_ERR(ddev);
190 
191 	ret = sti_init(ddev);
192 	if (ret)
193 		goto err_drm_dev_put;
194 
195 	ret = component_bind_all(ddev->dev, ddev);
196 	if (ret)
197 		goto err_cleanup;
198 
199 	ret = drm_dev_register(ddev, 0);
200 	if (ret)
201 		goto err_cleanup;
202 
203 	drm_mode_config_reset(ddev);
204 
205 	drm_fbdev_dma_setup(ddev, 32);
206 
207 	return 0;
208 
209 err_cleanup:
210 	sti_cleanup(ddev);
211 err_drm_dev_put:
212 	drm_dev_put(ddev);
213 	return ret;
214 }
215 
216 static void sti_unbind(struct device *dev)
217 {
218 	struct drm_device *ddev = dev_get_drvdata(dev);
219 
220 	drm_dev_unregister(ddev);
221 	sti_cleanup(ddev);
222 	drm_dev_put(ddev);
223 }
224 
225 static const struct component_master_ops sti_ops = {
226 	.bind = sti_bind,
227 	.unbind = sti_unbind,
228 };
229 
230 static int sti_platform_probe(struct platform_device *pdev)
231 {
232 	struct device *dev = &pdev->dev;
233 	struct device_node *node = dev->of_node;
234 	struct device_node *child_np;
235 	struct component_match *match = NULL;
236 
237 	dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
238 
239 	devm_of_platform_populate(dev);
240 
241 	child_np = of_get_next_available_child(node, NULL);
242 
243 	while (child_np) {
244 		drm_of_component_match_add(dev, &match, component_compare_of,
245 					   child_np);
246 		child_np = of_get_next_available_child(node, child_np);
247 	}
248 
249 	return component_master_add_with_match(dev, &sti_ops, match);
250 }
251 
252 static void sti_platform_remove(struct platform_device *pdev)
253 {
254 	component_master_del(&pdev->dev, &sti_ops);
255 }
256 
257 static void sti_platform_shutdown(struct platform_device *pdev)
258 {
259 	drm_atomic_helper_shutdown(platform_get_drvdata(pdev));
260 }
261 
262 static const struct of_device_id sti_dt_ids[] = {
263 	{ .compatible = "st,sti-display-subsystem", },
264 	{ /* end node */ },
265 };
266 MODULE_DEVICE_TABLE(of, sti_dt_ids);
267 
268 static struct platform_driver sti_platform_driver = {
269 	.probe = sti_platform_probe,
270 	.remove_new = sti_platform_remove,
271 	.shutdown = sti_platform_shutdown,
272 	.driver = {
273 		.name = DRIVER_NAME,
274 		.of_match_table = sti_dt_ids,
275 	},
276 };
277 
278 static struct platform_driver * const drivers[] = {
279 	&sti_tvout_driver,
280 	&sti_hqvdp_driver,
281 	&sti_hdmi_driver,
282 	&sti_hda_driver,
283 	&sti_dvo_driver,
284 	&sti_vtg_driver,
285 	&sti_compositor_driver,
286 	&sti_platform_driver,
287 };
288 
289 static int sti_drm_init(void)
290 {
291 	if (drm_firmware_drivers_only())
292 		return -ENODEV;
293 
294 	return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
295 }
296 module_init(sti_drm_init);
297 
298 static void sti_drm_exit(void)
299 {
300 	platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
301 }
302 module_exit(sti_drm_exit);
303 
304 MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
305 MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
306 MODULE_LICENSE("GPL");
307