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 <drm/drmP.h>
8
9 #include <linux/component.h>
10 #include <linux/debugfs.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/of_platform.h>
14
15 #include <drm/drm_atomic.h>
16 #include <drm/drm_atomic_helper.h>
17 #include <drm/drm_crtc_helper.h>
18 #include <drm/drm_gem_cma_helper.h>
19 #include <drm/drm_gem_framebuffer_helper.h>
20 #include <drm/drm_fb_helper.h>
21 #include <drm/drm_fb_cma_helper.h>
22 #include <drm/drm_of.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
sti_drm_fps_get(void * data,u64 * val)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
sti_drm_fps_set(void * data,u64 val)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
sti_drm_fps_dbg_show(struct seq_file * s,void * data)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
sti_drm_dbg_init(struct drm_minor * minor)96 static int sti_drm_dbg_init(struct drm_minor *minor)
97 {
98 struct dentry *dentry;
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 dentry = debugfs_create_file("fps_show", S_IRUGO | S_IWUSR,
108 minor->debugfs_root, minor->dev,
109 &sti_drm_fps_fops);
110 if (!dentry) {
111 ret = -ENOMEM;
112 goto err;
113 }
114
115 DRM_INFO("%s: debugfs installed\n", DRIVER_NAME);
116 return 0;
117 err:
118 DRM_ERROR("%s: cannot install debugfs\n", DRIVER_NAME);
119 return ret;
120 }
121
122 static const struct drm_mode_config_funcs sti_mode_config_funcs = {
123 .fb_create = drm_gem_fb_create,
124 .output_poll_changed = drm_fb_helper_output_poll_changed,
125 .atomic_check = drm_atomic_helper_check,
126 .atomic_commit = drm_atomic_helper_commit,
127 };
128
sti_mode_config_init(struct drm_device * dev)129 static void sti_mode_config_init(struct drm_device *dev)
130 {
131 dev->mode_config.min_width = 0;
132 dev->mode_config.min_height = 0;
133
134 /*
135 * set max width and height as default value.
136 * this value would be used to check framebuffer size limitation
137 * at drm_mode_addfb().
138 */
139 dev->mode_config.max_width = STI_MAX_FB_WIDTH;
140 dev->mode_config.max_height = STI_MAX_FB_HEIGHT;
141
142 dev->mode_config.funcs = &sti_mode_config_funcs;
143
144 dev->mode_config.normalize_zpos = true;
145 }
146
147 DEFINE_DRM_GEM_CMA_FOPS(sti_driver_fops);
148
149 static struct drm_driver sti_driver = {
150 .driver_features = DRIVER_MODESET |
151 DRIVER_GEM | DRIVER_PRIME | DRIVER_ATOMIC,
152 .gem_free_object_unlocked = drm_gem_cma_free_object,
153 .gem_vm_ops = &drm_gem_cma_vm_ops,
154 .dumb_create = drm_gem_cma_dumb_create,
155 .fops = &sti_driver_fops,
156
157 .enable_vblank = sti_crtc_enable_vblank,
158 .disable_vblank = sti_crtc_disable_vblank,
159
160 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
161 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
162 .gem_prime_export = drm_gem_prime_export,
163 .gem_prime_import = drm_gem_prime_import,
164 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
165 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
166 .gem_prime_vmap = drm_gem_cma_prime_vmap,
167 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
168 .gem_prime_mmap = drm_gem_cma_prime_mmap,
169
170 .debugfs_init = sti_drm_dbg_init,
171
172 .name = DRIVER_NAME,
173 .desc = DRIVER_DESC,
174 .date = DRIVER_DATE,
175 .major = DRIVER_MAJOR,
176 .minor = DRIVER_MINOR,
177 };
178
compare_of(struct device * dev,void * data)179 static int compare_of(struct device *dev, void *data)
180 {
181 return dev->of_node == data;
182 }
183
sti_init(struct drm_device * ddev)184 static int sti_init(struct drm_device *ddev)
185 {
186 struct sti_private *private;
187
188 private = kzalloc(sizeof(*private), GFP_KERNEL);
189 if (!private)
190 return -ENOMEM;
191
192 ddev->dev_private = (void *)private;
193 dev_set_drvdata(ddev->dev, ddev);
194 private->drm_dev = ddev;
195
196 drm_mode_config_init(ddev);
197
198 sti_mode_config_init(ddev);
199
200 drm_kms_helper_poll_init(ddev);
201
202 return 0;
203 }
204
sti_cleanup(struct drm_device * ddev)205 static void sti_cleanup(struct drm_device *ddev)
206 {
207 struct sti_private *private = ddev->dev_private;
208
209 drm_fb_cma_fbdev_fini(ddev);
210 drm_kms_helper_poll_fini(ddev);
211 component_unbind_all(ddev->dev, ddev);
212 kfree(private);
213 ddev->dev_private = NULL;
214 }
215
sti_bind(struct device * dev)216 static int sti_bind(struct device *dev)
217 {
218 struct drm_device *ddev;
219 int ret;
220
221 ddev = drm_dev_alloc(&sti_driver, dev);
222 if (IS_ERR(ddev))
223 return PTR_ERR(ddev);
224
225 ret = sti_init(ddev);
226 if (ret)
227 goto err_drm_dev_put;
228
229 ret = component_bind_all(ddev->dev, ddev);
230 if (ret)
231 goto err_cleanup;
232
233 ret = drm_dev_register(ddev, 0);
234 if (ret)
235 goto err_register;
236
237 drm_mode_config_reset(ddev);
238
239 if (ddev->mode_config.num_connector) {
240 ret = drm_fb_cma_fbdev_init(ddev, 32, 0);
241 if (ret)
242 DRM_DEBUG_DRIVER("Warning: fails to create fbdev\n");
243 }
244
245 return 0;
246
247 err_register:
248 drm_mode_config_cleanup(ddev);
249 err_cleanup:
250 sti_cleanup(ddev);
251 err_drm_dev_put:
252 drm_dev_put(ddev);
253 return ret;
254 }
255
sti_unbind(struct device * dev)256 static void sti_unbind(struct device *dev)
257 {
258 struct drm_device *ddev = dev_get_drvdata(dev);
259
260 drm_dev_unregister(ddev);
261 sti_cleanup(ddev);
262 drm_dev_put(ddev);
263 }
264
265 static const struct component_master_ops sti_ops = {
266 .bind = sti_bind,
267 .unbind = sti_unbind,
268 };
269
sti_platform_probe(struct platform_device * pdev)270 static int sti_platform_probe(struct platform_device *pdev)
271 {
272 struct device *dev = &pdev->dev;
273 struct device_node *node = dev->of_node;
274 struct device_node *child_np;
275 struct component_match *match = NULL;
276
277 dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
278
279 devm_of_platform_populate(dev);
280
281 child_np = of_get_next_available_child(node, NULL);
282
283 while (child_np) {
284 drm_of_component_match_add(dev, &match, compare_of,
285 child_np);
286 child_np = of_get_next_available_child(node, child_np);
287 }
288
289 return component_master_add_with_match(dev, &sti_ops, match);
290 }
291
sti_platform_remove(struct platform_device * pdev)292 static int sti_platform_remove(struct platform_device *pdev)
293 {
294 component_master_del(&pdev->dev, &sti_ops);
295
296 return 0;
297 }
298
299 static const struct of_device_id sti_dt_ids[] = {
300 { .compatible = "st,sti-display-subsystem", },
301 { /* end node */ },
302 };
303 MODULE_DEVICE_TABLE(of, sti_dt_ids);
304
305 static struct platform_driver sti_platform_driver = {
306 .probe = sti_platform_probe,
307 .remove = sti_platform_remove,
308 .driver = {
309 .name = DRIVER_NAME,
310 .of_match_table = sti_dt_ids,
311 },
312 };
313
314 static struct platform_driver * const drivers[] = {
315 &sti_tvout_driver,
316 &sti_hqvdp_driver,
317 &sti_hdmi_driver,
318 &sti_hda_driver,
319 &sti_dvo_driver,
320 &sti_vtg_driver,
321 &sti_compositor_driver,
322 &sti_platform_driver,
323 };
324
sti_drm_init(void)325 static int sti_drm_init(void)
326 {
327 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
328 }
329 module_init(sti_drm_init);
330
sti_drm_exit(void)331 static void sti_drm_exit(void)
332 {
333 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
334 }
335 module_exit(sti_drm_exit);
336
337 MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
338 MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
339 MODULE_LICENSE("GPL");
340