2 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
4 * Inki Dae <inki.dae@samsung.com>
5 * Joonyoung Shim <jy0922.shim@samsung.com>
6 * Seung-Woo Kim <sw0312.kim@samsung.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
14 #include <linux/pm_runtime.h>
16 #include <drm/drm_crtc_helper.h>
18 #include <linux/anon_inodes.h>
19 #include <linux/component.h>
21 #include <drm/exynos_drm.h>
23 #include "exynos_drm_drv.h"
24 #include "exynos_drm_crtc.h"
25 #include "exynos_drm_encoder.h"
26 #include "exynos_drm_fbdev.h"
27 #include "exynos_drm_fb.h"
28 #include "exynos_drm_gem.h"
29 #include "exynos_drm_plane.h"
30 #include "exynos_drm_vidi.h"
31 #include "exynos_drm_dmabuf.h"
32 #include "exynos_drm_g2d.h"
33 #include "exynos_drm_ipp.h"
34 #include "exynos_drm_iommu.h"
36 #define DRIVER_NAME "exynos"
37 #define DRIVER_DESC "Samsung SoC DRM"
38 #define DRIVER_DATE "20110530"
39 #define DRIVER_MAJOR 1
40 #define DRIVER_MINOR 0
42 static struct platform_device *exynos_drm_pdev;
44 static DEFINE_MUTEX(drm_component_lock);
45 static LIST_HEAD(drm_component_list);
47 struct component_dev {
48 struct list_head list;
49 struct device *crtc_dev;
50 struct device *conn_dev;
51 enum exynos_drm_output_type out_type;
52 unsigned int dev_type_flag;
55 static int exynos_drm_load(struct drm_device *dev, unsigned long flags)
57 struct exynos_drm_private *private;
61 private = kzalloc(sizeof(struct exynos_drm_private), GFP_KERNEL);
65 INIT_LIST_HEAD(&private->pageflip_event_list);
66 dev_set_drvdata(dev->dev, dev);
67 dev->dev_private = (void *)private;
70 * create mapping to manage iommu table and set a pointer to iommu
71 * mapping structure to iommu_mapping of private data.
72 * also this iommu_mapping can be used to check if iommu is supported
75 ret = drm_create_iommu_mapping(dev);
77 DRM_ERROR("failed to create iommu mapping.\n");
78 goto err_free_private;
81 drm_mode_config_init(dev);
83 exynos_drm_mode_config_init(dev);
85 for (nr = 0; nr < MAX_PLANE; nr++) {
86 struct drm_plane *plane;
87 unsigned long possible_crtcs = (1 << MAX_CRTC) - 1;
89 plane = exynos_plane_init(dev, possible_crtcs, false);
91 goto err_mode_config_cleanup;
94 /* init kms poll for handling hpd */
95 drm_kms_helper_poll_init(dev);
97 ret = drm_vblank_init(dev, MAX_CRTC);
99 goto err_mode_config_cleanup;
101 /* setup possible_clones. */
102 exynos_drm_encoder_setup(dev);
104 platform_set_drvdata(dev->platformdev, dev);
106 /* Try to bind all sub drivers. */
107 ret = component_bind_all(dev->dev, dev);
109 goto err_cleanup_vblank;
111 /* Probe non kms sub drivers and virtual display driver. */
112 ret = exynos_drm_device_subdrv_probe(dev);
116 /* force connectors detection */
117 drm_helper_hpd_irq_event(dev);
122 component_unbind_all(dev->dev, dev);
124 drm_vblank_cleanup(dev);
125 err_mode_config_cleanup:
126 drm_mode_config_cleanup(dev);
127 drm_release_iommu_mapping(dev);
134 static int exynos_drm_unload(struct drm_device *dev)
136 exynos_drm_device_subdrv_remove(dev);
138 exynos_drm_fbdev_fini(dev);
139 drm_vblank_cleanup(dev);
140 drm_kms_helper_poll_fini(dev);
141 drm_mode_config_cleanup(dev);
143 drm_release_iommu_mapping(dev);
144 kfree(dev->dev_private);
146 component_unbind_all(dev->dev, dev);
147 dev->dev_private = NULL;
152 static const struct file_operations exynos_drm_gem_fops = {
153 .mmap = exynos_drm_gem_mmap_buffer,
156 static int exynos_drm_suspend(struct drm_device *dev, pm_message_t state)
158 struct drm_connector *connector;
160 drm_modeset_lock_all(dev);
161 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
162 int old_dpms = connector->dpms;
164 if (connector->funcs->dpms)
165 connector->funcs->dpms(connector, DRM_MODE_DPMS_OFF);
167 /* Set the old mode back to the connector for resume */
168 connector->dpms = old_dpms;
170 drm_modeset_unlock_all(dev);
175 static int exynos_drm_resume(struct drm_device *dev)
177 struct drm_connector *connector;
179 drm_modeset_lock_all(dev);
180 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
181 if (connector->funcs->dpms)
182 connector->funcs->dpms(connector, connector->dpms);
184 drm_modeset_unlock_all(dev);
186 drm_helper_resume_force_mode(dev);
191 static int exynos_drm_open(struct drm_device *dev, struct drm_file *file)
193 struct drm_exynos_file_private *file_priv;
194 struct file *anon_filp;
197 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
201 file->driver_priv = file_priv;
203 ret = exynos_drm_subdrv_open(dev, file);
205 goto err_file_priv_free;
207 anon_filp = anon_inode_getfile("exynos_gem", &exynos_drm_gem_fops,
209 if (IS_ERR(anon_filp)) {
210 ret = PTR_ERR(anon_filp);
211 goto err_subdrv_close;
214 anon_filp->f_mode = FMODE_READ | FMODE_WRITE;
215 file_priv->anon_filp = anon_filp;
220 exynos_drm_subdrv_close(dev, file);
224 file->driver_priv = NULL;
228 static void exynos_drm_preclose(struct drm_device *dev,
229 struct drm_file *file)
231 exynos_drm_subdrv_close(dev, file);
234 static void exynos_drm_postclose(struct drm_device *dev, struct drm_file *file)
236 struct exynos_drm_private *private = dev->dev_private;
237 struct drm_exynos_file_private *file_priv;
238 struct drm_pending_vblank_event *v, *vt;
239 struct drm_pending_event *e, *et;
242 if (!file->driver_priv)
245 /* Release all events not unhandled by page flip handler. */
246 spin_lock_irqsave(&dev->event_lock, flags);
247 list_for_each_entry_safe(v, vt, &private->pageflip_event_list,
249 if (v->base.file_priv == file) {
250 list_del(&v->base.link);
251 drm_vblank_put(dev, v->pipe);
252 v->base.destroy(&v->base);
256 /* Release all events handled by page flip handler but not freed. */
257 list_for_each_entry_safe(e, et, &file->event_list, link) {
261 spin_unlock_irqrestore(&dev->event_lock, flags);
263 file_priv = file->driver_priv;
264 if (file_priv->anon_filp)
265 fput(file_priv->anon_filp);
267 kfree(file->driver_priv);
268 file->driver_priv = NULL;
271 static void exynos_drm_lastclose(struct drm_device *dev)
273 exynos_drm_fbdev_restore_mode(dev);
276 static const struct vm_operations_struct exynos_drm_gem_vm_ops = {
277 .fault = exynos_drm_gem_fault,
278 .open = drm_gem_vm_open,
279 .close = drm_gem_vm_close,
282 static const struct drm_ioctl_desc exynos_ioctls[] = {
283 DRM_IOCTL_DEF_DRV(EXYNOS_GEM_CREATE, exynos_drm_gem_create_ioctl,
284 DRM_UNLOCKED | DRM_AUTH),
285 DRM_IOCTL_DEF_DRV(EXYNOS_GEM_MAP_OFFSET,
286 exynos_drm_gem_map_offset_ioctl, DRM_UNLOCKED |
288 DRM_IOCTL_DEF_DRV(EXYNOS_GEM_MMAP,
289 exynos_drm_gem_mmap_ioctl, DRM_UNLOCKED | DRM_AUTH),
290 DRM_IOCTL_DEF_DRV(EXYNOS_GEM_GET,
291 exynos_drm_gem_get_ioctl, DRM_UNLOCKED),
292 DRM_IOCTL_DEF_DRV(EXYNOS_VIDI_CONNECTION,
293 vidi_connection_ioctl, DRM_UNLOCKED | DRM_AUTH),
294 DRM_IOCTL_DEF_DRV(EXYNOS_G2D_GET_VER,
295 exynos_g2d_get_ver_ioctl, DRM_UNLOCKED | DRM_AUTH),
296 DRM_IOCTL_DEF_DRV(EXYNOS_G2D_SET_CMDLIST,
297 exynos_g2d_set_cmdlist_ioctl, DRM_UNLOCKED | DRM_AUTH),
298 DRM_IOCTL_DEF_DRV(EXYNOS_G2D_EXEC,
299 exynos_g2d_exec_ioctl, DRM_UNLOCKED | DRM_AUTH),
300 DRM_IOCTL_DEF_DRV(EXYNOS_IPP_GET_PROPERTY,
301 exynos_drm_ipp_get_property, DRM_UNLOCKED | DRM_AUTH),
302 DRM_IOCTL_DEF_DRV(EXYNOS_IPP_SET_PROPERTY,
303 exynos_drm_ipp_set_property, DRM_UNLOCKED | DRM_AUTH),
304 DRM_IOCTL_DEF_DRV(EXYNOS_IPP_QUEUE_BUF,
305 exynos_drm_ipp_queue_buf, DRM_UNLOCKED | DRM_AUTH),
306 DRM_IOCTL_DEF_DRV(EXYNOS_IPP_CMD_CTRL,
307 exynos_drm_ipp_cmd_ctrl, DRM_UNLOCKED | DRM_AUTH),
310 static const struct file_operations exynos_drm_driver_fops = {
311 .owner = THIS_MODULE,
313 .mmap = exynos_drm_gem_mmap,
316 .unlocked_ioctl = drm_ioctl,
318 .compat_ioctl = drm_compat_ioctl,
320 .release = drm_release,
323 static struct drm_driver exynos_drm_driver = {
324 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME,
325 .load = exynos_drm_load,
326 .unload = exynos_drm_unload,
327 .suspend = exynos_drm_suspend,
328 .resume = exynos_drm_resume,
329 .open = exynos_drm_open,
330 .preclose = exynos_drm_preclose,
331 .lastclose = exynos_drm_lastclose,
332 .postclose = exynos_drm_postclose,
333 .set_busid = drm_platform_set_busid,
334 .get_vblank_counter = drm_vblank_count,
335 .enable_vblank = exynos_drm_crtc_enable_vblank,
336 .disable_vblank = exynos_drm_crtc_disable_vblank,
337 .gem_free_object = exynos_drm_gem_free_object,
338 .gem_vm_ops = &exynos_drm_gem_vm_ops,
339 .dumb_create = exynos_drm_gem_dumb_create,
340 .dumb_map_offset = exynos_drm_gem_dumb_map_offset,
341 .dumb_destroy = drm_gem_dumb_destroy,
342 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
343 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
344 .gem_prime_export = exynos_dmabuf_prime_export,
345 .gem_prime_import = exynos_dmabuf_prime_import,
346 .ioctls = exynos_ioctls,
347 .num_ioctls = ARRAY_SIZE(exynos_ioctls),
348 .fops = &exynos_drm_driver_fops,
352 .major = DRIVER_MAJOR,
353 .minor = DRIVER_MINOR,
356 #ifdef CONFIG_PM_SLEEP
357 static int exynos_drm_sys_suspend(struct device *dev)
359 struct drm_device *drm_dev = dev_get_drvdata(dev);
360 pm_message_t message;
362 if (pm_runtime_suspended(dev) || !drm_dev)
365 message.event = PM_EVENT_SUSPEND;
366 return exynos_drm_suspend(drm_dev, message);
369 static int exynos_drm_sys_resume(struct device *dev)
371 struct drm_device *drm_dev = dev_get_drvdata(dev);
373 if (pm_runtime_suspended(dev) || !drm_dev)
376 return exynos_drm_resume(drm_dev);
380 static const struct dev_pm_ops exynos_drm_pm_ops = {
381 SET_SYSTEM_SLEEP_PM_OPS(exynos_drm_sys_suspend, exynos_drm_sys_resume)
384 int exynos_drm_component_add(struct device *dev,
385 enum exynos_drm_device_type dev_type,
386 enum exynos_drm_output_type out_type)
388 struct component_dev *cdev;
390 if (dev_type != EXYNOS_DEVICE_TYPE_CRTC &&
391 dev_type != EXYNOS_DEVICE_TYPE_CONNECTOR) {
392 DRM_ERROR("invalid device type.\n");
396 mutex_lock(&drm_component_lock);
399 * Make sure to check if there is a component which has two device
400 * objects, for connector and for encoder/connector.
401 * It should make sure that crtc and encoder/connector drivers are
402 * ready before exynos drm core binds them.
404 list_for_each_entry(cdev, &drm_component_list, list) {
405 if (cdev->out_type == out_type) {
407 * If crtc and encoder/connector device objects are
408 * added already just return.
410 if (cdev->dev_type_flag == (EXYNOS_DEVICE_TYPE_CRTC |
411 EXYNOS_DEVICE_TYPE_CONNECTOR)) {
412 mutex_unlock(&drm_component_lock);
416 if (dev_type == EXYNOS_DEVICE_TYPE_CRTC) {
417 cdev->crtc_dev = dev;
418 cdev->dev_type_flag |= dev_type;
421 if (dev_type == EXYNOS_DEVICE_TYPE_CONNECTOR) {
422 cdev->conn_dev = dev;
423 cdev->dev_type_flag |= dev_type;
426 mutex_unlock(&drm_component_lock);
431 mutex_unlock(&drm_component_lock);
433 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
437 if (dev_type == EXYNOS_DEVICE_TYPE_CRTC)
438 cdev->crtc_dev = dev;
439 if (dev_type == EXYNOS_DEVICE_TYPE_CONNECTOR)
440 cdev->conn_dev = dev;
442 cdev->out_type = out_type;
443 cdev->dev_type_flag = dev_type;
445 mutex_lock(&drm_component_lock);
446 list_add_tail(&cdev->list, &drm_component_list);
447 mutex_unlock(&drm_component_lock);
452 void exynos_drm_component_del(struct device *dev,
453 enum exynos_drm_device_type dev_type)
455 struct component_dev *cdev, *next;
457 mutex_lock(&drm_component_lock);
459 list_for_each_entry_safe(cdev, next, &drm_component_list, list) {
460 if (dev_type == EXYNOS_DEVICE_TYPE_CRTC) {
461 if (cdev->crtc_dev == dev) {
462 cdev->crtc_dev = NULL;
463 cdev->dev_type_flag &= ~dev_type;
467 if (dev_type == EXYNOS_DEVICE_TYPE_CONNECTOR) {
468 if (cdev->conn_dev == dev) {
469 cdev->conn_dev = NULL;
470 cdev->dev_type_flag &= ~dev_type;
475 * Release cdev object only in case that both of crtc and
476 * encoder/connector device objects are NULL.
478 if (!cdev->crtc_dev && !cdev->conn_dev) {
479 list_del(&cdev->list);
486 mutex_unlock(&drm_component_lock);
489 static int compare_dev(struct device *dev, void *data)
491 return dev == (struct device *)data;
494 static struct component_match *exynos_drm_match_add(struct device *dev)
496 struct component_match *match = NULL;
497 struct component_dev *cdev;
498 unsigned int attach_cnt = 0;
500 mutex_lock(&drm_component_lock);
502 list_for_each_entry(cdev, &drm_component_list, list) {
504 * Add components to master only in case that crtc and
505 * encoder/connector device objects exist.
507 if (!cdev->crtc_dev || !cdev->conn_dev)
512 mutex_unlock(&drm_component_lock);
515 * fimd and dpi modules have same device object so add
516 * only crtc device object in this case.
518 if (cdev->crtc_dev == cdev->conn_dev) {
519 component_match_add(dev, &match, compare_dev,
525 * Do not chage below call order.
526 * crtc device first should be added to master because
527 * connector/encoder need pipe number of crtc when they
530 component_match_add(dev, &match, compare_dev, cdev->crtc_dev);
531 component_match_add(dev, &match, compare_dev, cdev->conn_dev);
534 mutex_lock(&drm_component_lock);
537 mutex_unlock(&drm_component_lock);
539 return attach_cnt ? match : ERR_PTR(-EPROBE_DEFER);
542 static int exynos_drm_bind(struct device *dev)
544 return drm_platform_init(&exynos_drm_driver, to_platform_device(dev));
547 static void exynos_drm_unbind(struct device *dev)
549 drm_put_dev(dev_get_drvdata(dev));
552 static const struct component_master_ops exynos_drm_ops = {
553 .bind = exynos_drm_bind,
554 .unbind = exynos_drm_unbind,
557 static int exynos_drm_platform_probe(struct platform_device *pdev)
559 struct component_match *match;
562 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
563 exynos_drm_driver.num_ioctls = ARRAY_SIZE(exynos_ioctls);
565 #ifdef CONFIG_DRM_EXYNOS_FIMD
566 ret = platform_driver_register(&fimd_driver);
571 #ifdef CONFIG_DRM_EXYNOS_DP
572 ret = platform_driver_register(&dp_driver);
574 goto err_unregister_fimd_drv;
577 #ifdef CONFIG_DRM_EXYNOS_DSI
578 ret = platform_driver_register(&dsi_driver);
580 goto err_unregister_dp_drv;
583 #ifdef CONFIG_DRM_EXYNOS_HDMI
584 ret = platform_driver_register(&mixer_driver);
586 goto err_unregister_dsi_drv;
587 ret = platform_driver_register(&hdmi_driver);
589 goto err_unregister_mixer_drv;
592 #ifdef CONFIG_DRM_EXYNOS_G2D
593 ret = platform_driver_register(&g2d_driver);
595 goto err_unregister_hdmi_drv;
598 #ifdef CONFIG_DRM_EXYNOS_FIMC
599 ret = platform_driver_register(&fimc_driver);
601 goto err_unregister_g2d_drv;
604 #ifdef CONFIG_DRM_EXYNOS_ROTATOR
605 ret = platform_driver_register(&rotator_driver);
607 goto err_unregister_fimc_drv;
610 #ifdef CONFIG_DRM_EXYNOS_GSC
611 ret = platform_driver_register(&gsc_driver);
613 goto err_unregister_rotator_drv;
616 #ifdef CONFIG_DRM_EXYNOS_IPP
617 ret = platform_driver_register(&ipp_driver);
619 goto err_unregister_gsc_drv;
621 ret = exynos_platform_device_ipp_register();
623 goto err_unregister_ipp_drv;
626 match = exynos_drm_match_add(&pdev->dev);
628 ret = PTR_ERR(match);
629 goto err_unregister_resources;
632 ret = component_master_add_with_match(&pdev->dev, &exynos_drm_ops,
635 goto err_unregister_resources;
639 err_unregister_resources:
641 #ifdef CONFIG_DRM_EXYNOS_IPP
642 exynos_platform_device_ipp_unregister();
643 err_unregister_ipp_drv:
644 platform_driver_unregister(&ipp_driver);
645 err_unregister_gsc_drv:
648 #ifdef CONFIG_DRM_EXYNOS_GSC
649 platform_driver_unregister(&gsc_driver);
650 err_unregister_rotator_drv:
653 #ifdef CONFIG_DRM_EXYNOS_ROTATOR
654 platform_driver_unregister(&rotator_driver);
655 err_unregister_fimc_drv:
658 #ifdef CONFIG_DRM_EXYNOS_FIMC
659 platform_driver_unregister(&fimc_driver);
660 err_unregister_g2d_drv:
663 #ifdef CONFIG_DRM_EXYNOS_G2D
664 platform_driver_unregister(&g2d_driver);
665 err_unregister_hdmi_drv:
668 #ifdef CONFIG_DRM_EXYNOS_HDMI
669 platform_driver_unregister(&hdmi_driver);
670 err_unregister_mixer_drv:
671 platform_driver_unregister(&mixer_driver);
672 err_unregister_dsi_drv:
675 #ifdef CONFIG_DRM_EXYNOS_DSI
676 platform_driver_unregister(&dsi_driver);
677 err_unregister_dp_drv:
680 #ifdef CONFIG_DRM_EXYNOS_DP
681 platform_driver_unregister(&dp_driver);
682 err_unregister_fimd_drv:
685 #ifdef CONFIG_DRM_EXYNOS_FIMD
686 platform_driver_unregister(&fimd_driver);
691 static int exynos_drm_platform_remove(struct platform_device *pdev)
693 #ifdef CONFIG_DRM_EXYNOS_IPP
694 exynos_platform_device_ipp_unregister();
695 platform_driver_unregister(&ipp_driver);
698 #ifdef CONFIG_DRM_EXYNOS_GSC
699 platform_driver_unregister(&gsc_driver);
702 #ifdef CONFIG_DRM_EXYNOS_ROTATOR
703 platform_driver_unregister(&rotator_driver);
706 #ifdef CONFIG_DRM_EXYNOS_FIMC
707 platform_driver_unregister(&fimc_driver);
710 #ifdef CONFIG_DRM_EXYNOS_G2D
711 platform_driver_unregister(&g2d_driver);
714 #ifdef CONFIG_DRM_EXYNOS_HDMI
715 platform_driver_unregister(&mixer_driver);
716 platform_driver_unregister(&hdmi_driver);
719 #ifdef CONFIG_DRM_EXYNOS_FIMD
720 platform_driver_unregister(&fimd_driver);
723 #ifdef CONFIG_DRM_EXYNOS_DSI
724 platform_driver_unregister(&dsi_driver);
727 #ifdef CONFIG_DRM_EXYNOS_DP
728 platform_driver_unregister(&dp_driver);
730 component_master_del(&pdev->dev, &exynos_drm_ops);
734 static struct platform_driver exynos_drm_platform_driver = {
735 .probe = exynos_drm_platform_probe,
736 .remove = exynos_drm_platform_remove,
738 .owner = THIS_MODULE,
739 .name = "exynos-drm",
740 .pm = &exynos_drm_pm_ops,
744 static int exynos_drm_init(void)
748 exynos_drm_pdev = platform_device_register_simple("exynos-drm", -1,
750 if (IS_ERR(exynos_drm_pdev))
751 return PTR_ERR(exynos_drm_pdev);
753 #ifdef CONFIG_DRM_EXYNOS_VIDI
754 ret = exynos_drm_probe_vidi();
756 goto err_unregister_pd;
759 ret = platform_driver_register(&exynos_drm_platform_driver);
761 goto err_remove_vidi;
766 #ifdef CONFIG_DRM_EXYNOS_VIDI
767 exynos_drm_remove_vidi();
771 platform_device_unregister(exynos_drm_pdev);
776 static void exynos_drm_exit(void)
778 platform_driver_unregister(&exynos_drm_platform_driver);
779 #ifdef CONFIG_DRM_EXYNOS_VIDI
780 exynos_drm_remove_vidi();
782 platform_device_unregister(exynos_drm_pdev);
785 module_init(exynos_drm_init);
786 module_exit(exynos_drm_exit);
788 MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
789 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
790 MODULE_AUTHOR("Seung-Woo Kim <sw0312.kim@samsung.com>");
791 MODULE_DESCRIPTION("Samsung SoC DRM Driver");
792 MODULE_LICENSE("GPL");