1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) STMicroelectronics SA 2014
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>
13 #include <linux/of_platform.h>
14 #include <linux/platform_device.h>
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_probe_helper.h>
28 #include "sti_plane.h"
30 #define DRIVER_NAME "sti"
31 #define DRIVER_DESC "STMicroelectronics SoC DRM"
32 #define DRIVER_MAJOR 1
33 #define DRIVER_MINOR 0
35 #define STI_MAX_FB_HEIGHT 4096
36 #define STI_MAX_FB_WIDTH 4096
38 static int sti_drm_fps_get(void *data, u64 *val)
40 struct drm_device *drm_dev = data;
45 list_for_each_entry(p, &drm_dev->mode_config.plane_list, head) {
46 struct sti_plane *plane = to_sti_plane(p);
48 *val |= plane->fps_info.output << i;
55 static int sti_drm_fps_set(void *data, u64 val)
57 struct drm_device *drm_dev = data;
61 list_for_each_entry(p, &drm_dev->mode_config.plane_list, head) {
62 struct sti_plane *plane = to_sti_plane(p);
64 memset(&plane->fps_info, 0, sizeof(plane->fps_info));
65 plane->fps_info.output = (val >> i) & 1;
73 DEFINE_SIMPLE_ATTRIBUTE(sti_drm_fps_fops,
74 sti_drm_fps_get, sti_drm_fps_set, "%llu\n");
76 static int sti_drm_fps_dbg_show(struct seq_file *s, void *data)
78 struct drm_info_node *node = s->private;
79 struct drm_device *dev = node->minor->dev;
82 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
83 struct sti_plane *plane = to_sti_plane(p);
85 seq_printf(s, "%s%s\n",
86 plane->fps_info.fps_str,
87 plane->fps_info.fips_str);
93 static struct drm_info_list sti_drm_dbg_list[] = {
94 {"fps_get", sti_drm_fps_dbg_show, 0},
97 static void sti_drm_dbg_init(struct drm_minor *minor)
99 drm_debugfs_create_files(sti_drm_dbg_list,
100 ARRAY_SIZE(sti_drm_dbg_list),
101 minor->debugfs_root, minor);
103 debugfs_create_file("fps_show", S_IRUGO | S_IWUSR, minor->debugfs_root,
104 minor->dev, &sti_drm_fps_fops);
106 DRM_INFO("%s: debugfs installed\n", DRIVER_NAME);
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,
115 static void sti_mode_config_init(struct drm_device *dev)
117 dev->mode_config.min_width = 0;
118 dev->mode_config.min_height = 0;
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().
125 dev->mode_config.max_width = STI_MAX_FB_WIDTH;
126 dev->mode_config.max_height = STI_MAX_FB_HEIGHT;
128 dev->mode_config.funcs = &sti_mode_config_funcs;
130 dev->mode_config.normalize_zpos = true;
133 DEFINE_DRM_GEM_DMA_FOPS(sti_driver_fops);
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 DRM_FBDEV_DMA_DRIVER_OPS,
141 .debugfs_init = sti_drm_dbg_init,
145 .major = DRIVER_MAJOR,
146 .minor = DRIVER_MINOR,
149 static int sti_init(struct drm_device *ddev)
151 struct sti_private *private;
153 private = kzalloc(sizeof(*private), GFP_KERNEL);
157 ddev->dev_private = (void *)private;
158 dev_set_drvdata(ddev->dev, ddev);
159 private->drm_dev = ddev;
161 drm_mode_config_init(ddev);
163 sti_mode_config_init(ddev);
165 drm_kms_helper_poll_init(ddev);
170 static void sti_cleanup(struct drm_device *ddev)
172 struct sti_private *private = ddev->dev_private;
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);
180 ddev->dev_private = NULL;
183 static int sti_bind(struct device *dev)
185 struct drm_device *ddev;
188 ddev = drm_dev_alloc(&sti_driver, dev);
190 return PTR_ERR(ddev);
192 ret = sti_init(ddev);
194 goto err_drm_dev_put;
196 ret = component_bind_all(ddev->dev, ddev);
200 ret = drm_dev_register(ddev, 0);
204 drm_mode_config_reset(ddev);
206 drm_client_setup(ddev, NULL);
217 static void sti_unbind(struct device *dev)
219 struct drm_device *ddev = dev_get_drvdata(dev);
221 drm_dev_unregister(ddev);
226 static const struct component_master_ops sti_ops = {
228 .unbind = sti_unbind,
231 static int sti_platform_probe(struct platform_device *pdev)
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;
238 dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
240 devm_of_platform_populate(dev);
242 child_np = of_get_next_available_child(node, NULL);
245 drm_of_component_match_add(dev, &match, component_compare_of,
247 child_np = of_get_next_available_child(node, child_np);
250 return component_master_add_with_match(dev, &sti_ops, match);
253 static void sti_platform_remove(struct platform_device *pdev)
255 component_master_del(&pdev->dev, &sti_ops);
258 static void sti_platform_shutdown(struct platform_device *pdev)
260 drm_atomic_helper_shutdown(platform_get_drvdata(pdev));
263 static const struct of_device_id sti_dt_ids[] = {
264 { .compatible = "st,sti-display-subsystem", },
267 MODULE_DEVICE_TABLE(of, sti_dt_ids);
269 static struct platform_driver sti_platform_driver = {
270 .probe = sti_platform_probe,
271 .remove = sti_platform_remove,
272 .shutdown = sti_platform_shutdown,
275 .of_match_table = sti_dt_ids,
279 static struct platform_driver * const drivers[] = {
286 &sti_compositor_driver,
287 &sti_platform_driver,
290 static int sti_drm_init(void)
292 if (drm_firmware_drivers_only())
295 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
297 module_init(sti_drm_init);
299 static void sti_drm_exit(void)
301 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
303 module_exit(sti_drm_exit);
306 MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
307 MODULE_LICENSE("GPL");