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