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_fb_cma_helper.h>
18 #include <drm/drm_fb_helper.h>
19 #include <drm/drm_gem_cma_helper.h>
20 #include <drm/drm_gem_framebuffer_helper.h>
21 #include <drm/drm_of.h>
22 #include <drm/drm_probe_helper.h>
25 #include "sti_plane.h"
27 #define DRIVER_NAME "sti"
28 #define DRIVER_DESC "STMicroelectronics SoC DRM"
29 #define DRIVER_DATE "20140601"
30 #define DRIVER_MAJOR 1
31 #define DRIVER_MINOR 0
33 #define STI_MAX_FB_HEIGHT 4096
34 #define STI_MAX_FB_WIDTH 4096
36 static int sti_drm_fps_get(void *data, u64 *val)
38 struct drm_device *drm_dev = data;
43 list_for_each_entry(p, &drm_dev->mode_config.plane_list, head) {
44 struct sti_plane *plane = to_sti_plane(p);
46 *val |= plane->fps_info.output << i;
53 static int sti_drm_fps_set(void *data, u64 val)
55 struct drm_device *drm_dev = data;
59 list_for_each_entry(p, &drm_dev->mode_config.plane_list, head) {
60 struct sti_plane *plane = to_sti_plane(p);
62 memset(&plane->fps_info, 0, sizeof(plane->fps_info));
63 plane->fps_info.output = (val >> i) & 1;
71 DEFINE_SIMPLE_ATTRIBUTE(sti_drm_fps_fops,
72 sti_drm_fps_get, sti_drm_fps_set, "%llu\n");
74 static int sti_drm_fps_dbg_show(struct seq_file *s, void *data)
76 struct drm_info_node *node = s->private;
77 struct drm_device *dev = node->minor->dev;
80 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
81 struct sti_plane *plane = to_sti_plane(p);
83 seq_printf(s, "%s%s\n",
84 plane->fps_info.fps_str,
85 plane->fps_info.fips_str);
91 static struct drm_info_list sti_drm_dbg_list[] = {
92 {"fps_get", sti_drm_fps_dbg_show, 0},
95 static int sti_drm_dbg_init(struct drm_minor *minor)
99 ret = drm_debugfs_create_files(sti_drm_dbg_list,
100 ARRAY_SIZE(sti_drm_dbg_list),
101 minor->debugfs_root, minor);
105 debugfs_create_file("fps_show", S_IRUGO | S_IWUSR, minor->debugfs_root,
106 minor->dev, &sti_drm_fps_fops);
108 DRM_INFO("%s: debugfs installed\n", DRIVER_NAME);
111 DRM_ERROR("%s: cannot install debugfs\n", DRIVER_NAME);
115 static const struct drm_mode_config_funcs sti_mode_config_funcs = {
116 .fb_create = drm_gem_fb_create,
117 .atomic_check = drm_atomic_helper_check,
118 .atomic_commit = drm_atomic_helper_commit,
121 static void sti_mode_config_init(struct drm_device *dev)
123 dev->mode_config.min_width = 0;
124 dev->mode_config.min_height = 0;
127 * set max width and height as default value.
128 * this value would be used to check framebuffer size limitation
129 * at drm_mode_addfb().
131 dev->mode_config.max_width = STI_MAX_FB_WIDTH;
132 dev->mode_config.max_height = STI_MAX_FB_HEIGHT;
134 dev->mode_config.funcs = &sti_mode_config_funcs;
136 dev->mode_config.normalize_zpos = true;
139 DEFINE_DRM_GEM_CMA_FOPS(sti_driver_fops);
141 static struct drm_driver sti_driver = {
142 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
143 .gem_free_object_unlocked = drm_gem_cma_free_object,
144 .gem_vm_ops = &drm_gem_cma_vm_ops,
145 .dumb_create = drm_gem_cma_dumb_create,
146 .fops = &sti_driver_fops,
148 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
149 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
150 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
151 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
152 .gem_prime_vmap = drm_gem_cma_prime_vmap,
153 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
154 .gem_prime_mmap = drm_gem_cma_prime_mmap,
156 .debugfs_init = sti_drm_dbg_init,
161 .major = DRIVER_MAJOR,
162 .minor = DRIVER_MINOR,
165 static int compare_of(struct device *dev, void *data)
167 return dev->of_node == data;
170 static int sti_init(struct drm_device *ddev)
172 struct sti_private *private;
174 private = kzalloc(sizeof(*private), GFP_KERNEL);
178 ddev->dev_private = (void *)private;
179 dev_set_drvdata(ddev->dev, ddev);
180 private->drm_dev = ddev;
182 drm_mode_config_init(ddev);
184 sti_mode_config_init(ddev);
186 drm_kms_helper_poll_init(ddev);
191 static void sti_cleanup(struct drm_device *ddev)
193 struct sti_private *private = ddev->dev_private;
195 drm_kms_helper_poll_fini(ddev);
196 drm_atomic_helper_shutdown(ddev);
197 drm_mode_config_cleanup(ddev);
198 component_unbind_all(ddev->dev, ddev);
200 ddev->dev_private = NULL;
203 static int sti_bind(struct device *dev)
205 struct drm_device *ddev;
208 ddev = drm_dev_alloc(&sti_driver, dev);
210 return PTR_ERR(ddev);
212 ret = sti_init(ddev);
214 goto err_drm_dev_put;
216 ret = component_bind_all(ddev->dev, ddev);
220 ret = drm_dev_register(ddev, 0);
224 drm_mode_config_reset(ddev);
226 drm_fbdev_generic_setup(ddev, 32);
237 static void sti_unbind(struct device *dev)
239 struct drm_device *ddev = dev_get_drvdata(dev);
241 drm_dev_unregister(ddev);
246 static const struct component_master_ops sti_ops = {
248 .unbind = sti_unbind,
251 static int sti_platform_probe(struct platform_device *pdev)
253 struct device *dev = &pdev->dev;
254 struct device_node *node = dev->of_node;
255 struct device_node *child_np;
256 struct component_match *match = NULL;
258 dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
260 devm_of_platform_populate(dev);
262 child_np = of_get_next_available_child(node, NULL);
265 drm_of_component_match_add(dev, &match, compare_of,
267 child_np = of_get_next_available_child(node, child_np);
270 return component_master_add_with_match(dev, &sti_ops, match);
273 static int sti_platform_remove(struct platform_device *pdev)
275 component_master_del(&pdev->dev, &sti_ops);
280 static const struct of_device_id sti_dt_ids[] = {
281 { .compatible = "st,sti-display-subsystem", },
284 MODULE_DEVICE_TABLE(of, sti_dt_ids);
286 static struct platform_driver sti_platform_driver = {
287 .probe = sti_platform_probe,
288 .remove = sti_platform_remove,
291 .of_match_table = sti_dt_ids,
295 static struct platform_driver * const drivers[] = {
302 &sti_compositor_driver,
303 &sti_platform_driver,
306 static int sti_drm_init(void)
308 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
310 module_init(sti_drm_init);
312 static void sti_drm_exit(void)
314 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
316 module_exit(sti_drm_exit);
319 MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
320 MODULE_LICENSE("GPL");