2 * Copyright (C) STMicroelectronics SA 2014
3 * Author: Benjamin Gaignard <benjamin.gaignard@st.com> for STMicroelectronics.
4 * License terms: GNU General Public License (GPL), version 2
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>
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_fb_cma_helper.h>
23 #include "sti_plane.h"
25 #define DRIVER_NAME "sti"
26 #define DRIVER_DESC "STMicroelectronics SoC DRM"
27 #define DRIVER_DATE "20140601"
28 #define DRIVER_MAJOR 1
29 #define DRIVER_MINOR 0
31 #define STI_MAX_FB_HEIGHT 4096
32 #define STI_MAX_FB_WIDTH 4096
34 static int sti_drm_fps_get(void *data, u64 *val)
36 struct drm_device *drm_dev = data;
41 list_for_each_entry(p, &drm_dev->mode_config.plane_list, head) {
42 struct sti_plane *plane = to_sti_plane(p);
44 *val |= plane->fps_info.output << i;
51 static int sti_drm_fps_set(void *data, u64 val)
53 struct drm_device *drm_dev = data;
57 list_for_each_entry(p, &drm_dev->mode_config.plane_list, head) {
58 struct sti_plane *plane = to_sti_plane(p);
60 plane->fps_info.output = (val >> i) & 1;
67 DEFINE_SIMPLE_ATTRIBUTE(sti_drm_fps_fops,
68 sti_drm_fps_get, sti_drm_fps_set, "%llu\n");
70 static int sti_drm_fps_dbg_show(struct seq_file *s, void *data)
72 struct drm_info_node *node = s->private;
73 struct drm_device *dev = node->minor->dev;
76 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
77 struct sti_plane *plane = to_sti_plane(p);
79 seq_printf(s, "%s%s\n",
80 plane->fps_info.fps_str,
81 plane->fps_info.fips_str);
87 static struct drm_info_list sti_drm_dbg_list[] = {
88 {"fps_get", sti_drm_fps_dbg_show, 0},
91 static int sti_drm_debugfs_create(struct dentry *root,
92 struct drm_minor *minor,
94 const struct file_operations *fops)
96 struct drm_device *dev = minor->dev;
97 struct drm_info_node *node;
100 ent = debugfs_create_file(name, S_IRUGO | S_IWUSR, root, dev, fops);
104 node = kmalloc(sizeof(*node), GFP_KERNEL);
112 node->info_ent = (void *)fops;
114 mutex_lock(&minor->debugfs_lock);
115 list_add(&node->list, &minor->debugfs_list);
116 mutex_unlock(&minor->debugfs_lock);
121 static int sti_drm_dbg_init(struct drm_minor *minor)
125 ret = drm_debugfs_create_files(sti_drm_dbg_list,
126 ARRAY_SIZE(sti_drm_dbg_list),
127 minor->debugfs_root, minor);
131 ret = sti_drm_debugfs_create(minor->debugfs_root, minor, "fps_show",
136 DRM_INFO("%s: debugfs installed\n", DRIVER_NAME);
139 DRM_ERROR("%s: cannot install debugfs\n", DRIVER_NAME);
143 void sti_drm_dbg_cleanup(struct drm_minor *minor)
145 drm_debugfs_remove_files(sti_drm_dbg_list,
146 ARRAY_SIZE(sti_drm_dbg_list), minor);
148 drm_debugfs_remove_files((struct drm_info_list *)&sti_drm_fps_fops,
152 static void sti_atomic_schedule(struct sti_private *private,
153 struct drm_atomic_state *state)
155 private->commit.state = state;
156 schedule_work(&private->commit.work);
159 static void sti_atomic_complete(struct sti_private *private,
160 struct drm_atomic_state *state)
162 struct drm_device *drm = private->drm_dev;
165 * Everything below can be run asynchronously without the need to grab
166 * any modeset locks at all under one condition: It must be guaranteed
167 * that the asynchronous work has either been cancelled (if the driver
168 * supports it, which at least requires that the framebuffers get
169 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
170 * before the new state gets committed on the software side with
171 * drm_atomic_helper_swap_state().
173 * This scheme allows new atomic state updates to be prepared and
174 * checked in parallel to the asynchronous completion of the previous
175 * update. Which is important since compositors need to figure out the
176 * composition of the next frame right after having submitted the
180 drm_atomic_helper_commit_modeset_disables(drm, state);
181 drm_atomic_helper_commit_planes(drm, state, false);
182 drm_atomic_helper_commit_modeset_enables(drm, state);
184 drm_atomic_helper_wait_for_vblanks(drm, state);
186 drm_atomic_helper_cleanup_planes(drm, state);
187 drm_atomic_state_free(state);
190 static void sti_atomic_work(struct work_struct *work)
192 struct sti_private *private = container_of(work,
193 struct sti_private, commit.work);
195 sti_atomic_complete(private, private->commit.state);
198 static int sti_atomic_commit(struct drm_device *drm,
199 struct drm_atomic_state *state, bool nonblock)
201 struct sti_private *private = drm->dev_private;
204 err = drm_atomic_helper_prepare_planes(drm, state);
208 /* serialize outstanding nonblocking commits */
209 mutex_lock(&private->commit.lock);
210 flush_work(&private->commit.work);
213 * This is the point of no return - everything below never fails except
214 * when the hw goes bonghits. Which means we can commit the new state on
215 * the software side now.
218 drm_atomic_helper_swap_state(state, true);
221 sti_atomic_schedule(private, state);
223 sti_atomic_complete(private, state);
225 mutex_unlock(&private->commit.lock);
229 static const struct drm_mode_config_funcs sti_mode_config_funcs = {
230 .fb_create = drm_fb_cma_create,
231 .atomic_check = drm_atomic_helper_check,
232 .atomic_commit = sti_atomic_commit,
235 static void sti_mode_config_init(struct drm_device *dev)
237 dev->mode_config.min_width = 0;
238 dev->mode_config.min_height = 0;
241 * set max width and height as default value.
242 * this value would be used to check framebuffer size limitation
243 * at drm_mode_addfb().
245 dev->mode_config.max_width = STI_MAX_FB_WIDTH;
246 dev->mode_config.max_height = STI_MAX_FB_HEIGHT;
248 dev->mode_config.funcs = &sti_mode_config_funcs;
251 static int sti_load(struct drm_device *dev, unsigned long flags)
253 struct sti_private *private;
256 private = kzalloc(sizeof(*private), GFP_KERNEL);
258 DRM_ERROR("Failed to allocate private\n");
261 dev->dev_private = (void *)private;
262 private->drm_dev = dev;
264 mutex_init(&private->commit.lock);
265 INIT_WORK(&private->commit.work, sti_atomic_work);
267 drm_mode_config_init(dev);
268 drm_kms_helper_poll_init(dev);
270 sti_mode_config_init(dev);
272 ret = component_bind_all(dev->dev, dev);
274 drm_kms_helper_poll_fini(dev);
275 drm_mode_config_cleanup(dev);
280 drm_mode_config_reset(dev);
282 drm_helper_disable_unused_functions(dev);
283 drm_fbdev_cma_init(dev, 32,
284 dev->mode_config.num_crtc,
285 dev->mode_config.num_connector);
290 static const struct file_operations sti_driver_fops = {
291 .owner = THIS_MODULE,
293 .mmap = drm_gem_cma_mmap,
296 .unlocked_ioctl = drm_ioctl,
298 .compat_ioctl = drm_compat_ioctl,
300 .release = drm_release,
303 static struct drm_driver sti_driver = {
304 .driver_features = DRIVER_HAVE_IRQ | DRIVER_MODESET |
305 DRIVER_GEM | DRIVER_PRIME | DRIVER_ATOMIC,
307 .gem_free_object_unlocked = drm_gem_cma_free_object,
308 .gem_vm_ops = &drm_gem_cma_vm_ops,
309 .dumb_create = drm_gem_cma_dumb_create,
310 .dumb_map_offset = drm_gem_cma_dumb_map_offset,
311 .dumb_destroy = drm_gem_dumb_destroy,
312 .fops = &sti_driver_fops,
314 .get_vblank_counter = drm_vblank_no_hw_counter,
315 .enable_vblank = sti_crtc_enable_vblank,
316 .disable_vblank = sti_crtc_disable_vblank,
318 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
319 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
320 .gem_prime_export = drm_gem_prime_export,
321 .gem_prime_import = drm_gem_prime_import,
322 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
323 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
324 .gem_prime_vmap = drm_gem_cma_prime_vmap,
325 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
326 .gem_prime_mmap = drm_gem_cma_prime_mmap,
328 .debugfs_init = sti_drm_dbg_init,
329 .debugfs_cleanup = sti_drm_dbg_cleanup,
334 .major = DRIVER_MAJOR,
335 .minor = DRIVER_MINOR,
338 static int compare_of(struct device *dev, void *data)
340 return dev->of_node == data;
343 static int sti_bind(struct device *dev)
345 return drm_platform_init(&sti_driver, to_platform_device(dev));
348 static void sti_unbind(struct device *dev)
350 drm_put_dev(dev_get_drvdata(dev));
353 static const struct component_master_ops sti_ops = {
355 .unbind = sti_unbind,
358 static int sti_platform_probe(struct platform_device *pdev)
360 struct device *dev = &pdev->dev;
361 struct device_node *node = dev->of_node;
362 struct device_node *child_np;
363 struct component_match *match = NULL;
365 dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
367 of_platform_populate(node, NULL, NULL, dev);
369 child_np = of_get_next_available_child(node, NULL);
372 component_match_add(dev, &match, compare_of, child_np);
373 of_node_put(child_np);
374 child_np = of_get_next_available_child(node, child_np);
377 return component_master_add_with_match(dev, &sti_ops, match);
380 static int sti_platform_remove(struct platform_device *pdev)
382 component_master_del(&pdev->dev, &sti_ops);
383 of_platform_depopulate(&pdev->dev);
388 static const struct of_device_id sti_dt_ids[] = {
389 { .compatible = "st,sti-display-subsystem", },
392 MODULE_DEVICE_TABLE(of, sti_dt_ids);
394 static struct platform_driver sti_platform_driver = {
395 .probe = sti_platform_probe,
396 .remove = sti_platform_remove,
399 .of_match_table = sti_dt_ids,
403 static struct platform_driver * const drivers[] = {
411 &sti_compositor_driver,
412 &sti_platform_driver,
415 static int sti_drm_init(void)
417 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
419 module_init(sti_drm_init);
421 static void sti_drm_exit(void)
423 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
425 module_exit(sti_drm_exit);
427 MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
428 MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
429 MODULE_LICENSE("GPL");