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>
20 #include <drm/drm_of.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 plane->fps_info.output = (val >> i) & 1;
68 DEFINE_SIMPLE_ATTRIBUTE(sti_drm_fps_fops,
69 sti_drm_fps_get, sti_drm_fps_set, "%llu\n");
71 static int sti_drm_fps_dbg_show(struct seq_file *s, void *data)
73 struct drm_info_node *node = s->private;
74 struct drm_device *dev = node->minor->dev;
77 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
78 struct sti_plane *plane = to_sti_plane(p);
80 seq_printf(s, "%s%s\n",
81 plane->fps_info.fps_str,
82 plane->fps_info.fips_str);
88 static struct drm_info_list sti_drm_dbg_list[] = {
89 {"fps_get", sti_drm_fps_dbg_show, 0},
92 static int sti_drm_debugfs_create(struct dentry *root,
93 struct drm_minor *minor,
95 const struct file_operations *fops)
97 struct drm_device *dev = minor->dev;
98 struct drm_info_node *node;
101 ent = debugfs_create_file(name, S_IRUGO | S_IWUSR, root, dev, fops);
105 node = kmalloc(sizeof(*node), GFP_KERNEL);
113 node->info_ent = (void *)fops;
115 mutex_lock(&minor->debugfs_lock);
116 list_add(&node->list, &minor->debugfs_list);
117 mutex_unlock(&minor->debugfs_lock);
122 static int sti_drm_dbg_init(struct drm_minor *minor)
126 ret = drm_debugfs_create_files(sti_drm_dbg_list,
127 ARRAY_SIZE(sti_drm_dbg_list),
128 minor->debugfs_root, minor);
132 ret = sti_drm_debugfs_create(minor->debugfs_root, minor, "fps_show",
137 DRM_INFO("%s: debugfs installed\n", DRIVER_NAME);
140 DRM_ERROR("%s: cannot install debugfs\n", DRIVER_NAME);
144 static void sti_drm_dbg_cleanup(struct drm_minor *minor)
146 drm_debugfs_remove_files(sti_drm_dbg_list,
147 ARRAY_SIZE(sti_drm_dbg_list), minor);
149 drm_debugfs_remove_files((struct drm_info_list *)&sti_drm_fps_fops,
153 static void sti_atomic_schedule(struct sti_private *private,
154 struct drm_atomic_state *state)
156 private->commit.state = state;
157 schedule_work(&private->commit.work);
160 static void sti_atomic_complete(struct sti_private *private,
161 struct drm_atomic_state *state)
163 struct drm_device *drm = private->drm_dev;
166 * Everything below can be run asynchronously without the need to grab
167 * any modeset locks at all under one condition: It must be guaranteed
168 * that the asynchronous work has either been cancelled (if the driver
169 * supports it, which at least requires that the framebuffers get
170 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
171 * before the new state gets committed on the software side with
172 * drm_atomic_helper_swap_state().
174 * This scheme allows new atomic state updates to be prepared and
175 * checked in parallel to the asynchronous completion of the previous
176 * update. Which is important since compositors need to figure out the
177 * composition of the next frame right after having submitted the
181 drm_atomic_helper_commit_modeset_disables(drm, state);
182 drm_atomic_helper_commit_planes(drm, state, 0);
183 drm_atomic_helper_commit_modeset_enables(drm, state);
185 drm_atomic_helper_wait_for_vblanks(drm, state);
187 drm_atomic_helper_cleanup_planes(drm, state);
188 drm_atomic_state_put(state);
191 static void sti_atomic_work(struct work_struct *work)
193 struct sti_private *private = container_of(work,
194 struct sti_private, commit.work);
196 sti_atomic_complete(private, private->commit.state);
199 static int sti_atomic_commit(struct drm_device *drm,
200 struct drm_atomic_state *state, bool nonblock)
202 struct sti_private *private = drm->dev_private;
205 err = drm_atomic_helper_prepare_planes(drm, state);
209 /* serialize outstanding nonblocking commits */
210 mutex_lock(&private->commit.lock);
211 flush_work(&private->commit.work);
214 * This is the point of no return - everything below never fails except
215 * when the hw goes bonghits. Which means we can commit the new state on
216 * the software side now.
219 drm_atomic_helper_swap_state(state, true);
221 drm_atomic_state_get(state);
223 sti_atomic_schedule(private, state);
225 sti_atomic_complete(private, state);
227 mutex_unlock(&private->commit.lock);
231 static void sti_output_poll_changed(struct drm_device *ddev)
233 struct sti_private *private = ddev->dev_private;
235 if (!ddev->mode_config.num_connector)
238 if (private->fbdev) {
239 drm_fbdev_cma_hotplug_event(private->fbdev);
243 private->fbdev = drm_fbdev_cma_init(ddev, 32,
244 ddev->mode_config.num_crtc,
245 ddev->mode_config.num_connector);
246 if (IS_ERR(private->fbdev))
247 private->fbdev = NULL;
250 static const struct drm_mode_config_funcs sti_mode_config_funcs = {
251 .fb_create = drm_fb_cma_create,
252 .output_poll_changed = sti_output_poll_changed,
253 .atomic_check = drm_atomic_helper_check,
254 .atomic_commit = sti_atomic_commit,
257 static void sti_mode_config_init(struct drm_device *dev)
259 dev->mode_config.min_width = 0;
260 dev->mode_config.min_height = 0;
263 * set max width and height as default value.
264 * this value would be used to check framebuffer size limitation
265 * at drm_mode_addfb().
267 dev->mode_config.max_width = STI_MAX_FB_WIDTH;
268 dev->mode_config.max_height = STI_MAX_FB_HEIGHT;
270 dev->mode_config.funcs = &sti_mode_config_funcs;
273 static const struct file_operations sti_driver_fops = {
274 .owner = THIS_MODULE,
276 .mmap = drm_gem_cma_mmap,
279 .unlocked_ioctl = drm_ioctl,
281 .compat_ioctl = drm_compat_ioctl,
283 .release = drm_release,
286 static struct drm_driver sti_driver = {
287 .driver_features = DRIVER_MODESET |
288 DRIVER_GEM | DRIVER_PRIME | DRIVER_ATOMIC,
289 .gem_free_object_unlocked = drm_gem_cma_free_object,
290 .gem_vm_ops = &drm_gem_cma_vm_ops,
291 .dumb_create = drm_gem_cma_dumb_create,
292 .dumb_map_offset = drm_gem_cma_dumb_map_offset,
293 .dumb_destroy = drm_gem_dumb_destroy,
294 .fops = &sti_driver_fops,
296 .get_vblank_counter = drm_vblank_no_hw_counter,
297 .enable_vblank = sti_crtc_enable_vblank,
298 .disable_vblank = sti_crtc_disable_vblank,
300 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
301 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
302 .gem_prime_export = drm_gem_prime_export,
303 .gem_prime_import = drm_gem_prime_import,
304 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
305 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
306 .gem_prime_vmap = drm_gem_cma_prime_vmap,
307 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
308 .gem_prime_mmap = drm_gem_cma_prime_mmap,
310 .debugfs_init = sti_drm_dbg_init,
311 .debugfs_cleanup = sti_drm_dbg_cleanup,
316 .major = DRIVER_MAJOR,
317 .minor = DRIVER_MINOR,
320 static int compare_of(struct device *dev, void *data)
322 return dev->of_node == data;
325 static int sti_init(struct drm_device *ddev)
327 struct sti_private *private;
329 private = kzalloc(sizeof(*private), GFP_KERNEL);
333 ddev->dev_private = (void *)private;
334 dev_set_drvdata(ddev->dev, ddev);
335 private->drm_dev = ddev;
337 mutex_init(&private->commit.lock);
338 INIT_WORK(&private->commit.work, sti_atomic_work);
340 drm_mode_config_init(ddev);
342 sti_mode_config_init(ddev);
344 drm_kms_helper_poll_init(ddev);
349 static void sti_cleanup(struct drm_device *ddev)
351 struct sti_private *private = ddev->dev_private;
353 if (private->fbdev) {
354 drm_fbdev_cma_fini(private->fbdev);
355 private->fbdev = NULL;
358 drm_kms_helper_poll_fini(ddev);
359 drm_vblank_cleanup(ddev);
361 ddev->dev_private = NULL;
364 static int sti_bind(struct device *dev)
366 struct drm_device *ddev;
369 ddev = drm_dev_alloc(&sti_driver, dev);
371 return PTR_ERR(ddev);
373 ddev->platformdev = to_platform_device(dev);
375 ret = sti_init(ddev);
377 goto err_drm_dev_unref;
379 ret = component_bind_all(ddev->dev, ddev);
383 ret = drm_dev_register(ddev, 0);
387 drm_mode_config_reset(ddev);
392 drm_mode_config_cleanup(ddev);
400 static void sti_unbind(struct device *dev)
402 struct drm_device *ddev = dev_get_drvdata(dev);
404 drm_dev_unregister(ddev);
409 static const struct component_master_ops sti_ops = {
411 .unbind = sti_unbind,
414 static int sti_platform_probe(struct platform_device *pdev)
416 struct device *dev = &pdev->dev;
417 struct device_node *node = dev->of_node;
418 struct device_node *child_np;
419 struct component_match *match = NULL;
421 dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
423 of_platform_populate(node, NULL, NULL, dev);
425 child_np = of_get_next_available_child(node, NULL);
428 drm_of_component_match_add(dev, &match, compare_of,
430 child_np = of_get_next_available_child(node, child_np);
433 return component_master_add_with_match(dev, &sti_ops, match);
436 static int sti_platform_remove(struct platform_device *pdev)
438 component_master_del(&pdev->dev, &sti_ops);
439 of_platform_depopulate(&pdev->dev);
444 static const struct of_device_id sti_dt_ids[] = {
445 { .compatible = "st,sti-display-subsystem", },
448 MODULE_DEVICE_TABLE(of, sti_dt_ids);
450 static struct platform_driver sti_platform_driver = {
451 .probe = sti_platform_probe,
452 .remove = sti_platform_remove,
455 .of_match_table = sti_dt_ids,
459 static struct platform_driver * const drivers[] = {
467 &sti_compositor_driver,
468 &sti_platform_driver,
471 static int sti_drm_init(void)
473 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
475 module_init(sti_drm_init);
477 static void sti_drm_exit(void)
479 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
481 module_exit(sti_drm_exit);
483 MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
484 MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
485 MODULE_LICENSE("GPL");