1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) STMicroelectronics SA 2014
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>
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_fbdev_dma.h>
18 #include <drm/drm_gem_dma_helper.h>
19 #include <drm/drm_gem_framebuffer_helper.h>
20 #include <drm/drm_of.h>
21 #include <drm/drm_probe_helper.h>
24 #include "sti_plane.h"
26 #define DRIVER_NAME "sti"
27 #define DRIVER_DESC "STMicroelectronics SoC DRM"
28 #define DRIVER_DATE "20140601"
29 #define DRIVER_MAJOR 1
30 #define DRIVER_MINOR 0
32 #define STI_MAX_FB_HEIGHT 4096
33 #define STI_MAX_FB_WIDTH 4096
35 static int sti_drm_fps_get(void *data, u64 *val)
37 struct drm_device *drm_dev = data;
42 list_for_each_entry(p, &drm_dev->mode_config.plane_list, head) {
43 struct sti_plane *plane = to_sti_plane(p);
45 *val |= plane->fps_info.output << i;
52 static int sti_drm_fps_set(void *data, u64 val)
54 struct drm_device *drm_dev = data;
58 list_for_each_entry(p, &drm_dev->mode_config.plane_list, head) {
59 struct sti_plane *plane = to_sti_plane(p);
61 memset(&plane->fps_info, 0, sizeof(plane->fps_info));
62 plane->fps_info.output = (val >> i) & 1;
70 DEFINE_SIMPLE_ATTRIBUTE(sti_drm_fps_fops,
71 sti_drm_fps_get, sti_drm_fps_set, "%llu\n");
73 static int sti_drm_fps_dbg_show(struct seq_file *s, void *data)
75 struct drm_info_node *node = s->private;
76 struct drm_device *dev = node->minor->dev;
79 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
80 struct sti_plane *plane = to_sti_plane(p);
82 seq_printf(s, "%s%s\n",
83 plane->fps_info.fps_str,
84 plane->fps_info.fips_str);
90 static struct drm_info_list sti_drm_dbg_list[] = {
91 {"fps_get", sti_drm_fps_dbg_show, 0},
94 static void sti_drm_dbg_init(struct drm_minor *minor)
96 drm_debugfs_create_files(sti_drm_dbg_list,
97 ARRAY_SIZE(sti_drm_dbg_list),
98 minor->debugfs_root, minor);
100 debugfs_create_file("fps_show", S_IRUGO | S_IWUSR, minor->debugfs_root,
101 minor->dev, &sti_drm_fps_fops);
103 DRM_INFO("%s: debugfs installed\n", DRIVER_NAME);
106 static const struct drm_mode_config_funcs sti_mode_config_funcs = {
107 .fb_create = drm_gem_fb_create,
108 .atomic_check = drm_atomic_helper_check,
109 .atomic_commit = drm_atomic_helper_commit,
112 static void sti_mode_config_init(struct drm_device *dev)
114 dev->mode_config.min_width = 0;
115 dev->mode_config.min_height = 0;
118 * set max width and height as default value.
119 * this value would be used to check framebuffer size limitation
120 * at drm_mode_addfb().
122 dev->mode_config.max_width = STI_MAX_FB_WIDTH;
123 dev->mode_config.max_height = STI_MAX_FB_HEIGHT;
125 dev->mode_config.funcs = &sti_mode_config_funcs;
127 dev->mode_config.normalize_zpos = true;
130 DEFINE_DRM_GEM_DMA_FOPS(sti_driver_fops);
132 static const struct drm_driver sti_driver = {
133 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
134 .fops = &sti_driver_fops,
135 DRM_GEM_DMA_DRIVER_OPS,
137 .debugfs_init = sti_drm_dbg_init,
142 .major = DRIVER_MAJOR,
143 .minor = DRIVER_MINOR,
146 static int sti_init(struct drm_device *ddev)
148 struct sti_private *private;
150 private = kzalloc(sizeof(*private), GFP_KERNEL);
154 ddev->dev_private = (void *)private;
155 dev_set_drvdata(ddev->dev, ddev);
156 private->drm_dev = ddev;
158 drm_mode_config_init(ddev);
160 sti_mode_config_init(ddev);
162 drm_kms_helper_poll_init(ddev);
167 static void sti_cleanup(struct drm_device *ddev)
169 struct sti_private *private = ddev->dev_private;
171 drm_kms_helper_poll_fini(ddev);
172 drm_atomic_helper_shutdown(ddev);
173 drm_mode_config_cleanup(ddev);
174 component_unbind_all(ddev->dev, ddev);
176 ddev->dev_private = NULL;
179 static int sti_bind(struct device *dev)
181 struct drm_device *ddev;
184 ddev = drm_dev_alloc(&sti_driver, dev);
186 return PTR_ERR(ddev);
188 ret = sti_init(ddev);
190 goto err_drm_dev_put;
192 ret = component_bind_all(ddev->dev, ddev);
196 ret = drm_dev_register(ddev, 0);
200 drm_mode_config_reset(ddev);
202 drm_fbdev_dma_setup(ddev, 32);
213 static void sti_unbind(struct device *dev)
215 struct drm_device *ddev = dev_get_drvdata(dev);
217 drm_dev_unregister(ddev);
222 static const struct component_master_ops sti_ops = {
224 .unbind = sti_unbind,
227 static int sti_platform_probe(struct platform_device *pdev)
229 struct device *dev = &pdev->dev;
230 struct device_node *node = dev->of_node;
231 struct device_node *child_np;
232 struct component_match *match = NULL;
234 dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
236 devm_of_platform_populate(dev);
238 child_np = of_get_next_available_child(node, NULL);
241 drm_of_component_match_add(dev, &match, component_compare_of,
243 child_np = of_get_next_available_child(node, child_np);
246 return component_master_add_with_match(dev, &sti_ops, match);
249 static int sti_platform_remove(struct platform_device *pdev)
251 component_master_del(&pdev->dev, &sti_ops);
256 static const struct of_device_id sti_dt_ids[] = {
257 { .compatible = "st,sti-display-subsystem", },
260 MODULE_DEVICE_TABLE(of, sti_dt_ids);
262 static struct platform_driver sti_platform_driver = {
263 .probe = sti_platform_probe,
264 .remove = sti_platform_remove,
267 .of_match_table = sti_dt_ids,
271 static struct platform_driver * const drivers[] = {
278 &sti_compositor_driver,
279 &sti_platform_driver,
282 static int sti_drm_init(void)
284 if (drm_firmware_drivers_only())
287 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
289 module_init(sti_drm_init);
291 static void sti_drm_exit(void)
293 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
295 module_exit(sti_drm_exit);
298 MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
299 MODULE_LICENSE("GPL");