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