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 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
30 #include "drm_crtc_helper.h"
32 #include <drm/exynos_drm.h>
34 #include "exynos_drm_drv.h"
35 #include "exynos_drm_crtc.h"
36 #include "exynos_drm_encoder.h"
37 #include "exynos_drm_fbdev.h"
38 #include "exynos_drm_fb.h"
39 #include "exynos_drm_gem.h"
40 #include "exynos_drm_plane.h"
41 #include "exynos_drm_vidi.h"
42 #include "exynos_drm_dmabuf.h"
43 #include "exynos_drm_display.h"
45 #include <linux/i2c.h>
46 #include <linux/of_i2c.h>
48 #include <drm/bridge/ptn3460.h>
49 #include <drm/bridge/ps8622.h>
51 #define DRIVER_NAME "exynos"
52 #define DRIVER_DESC "Samsung SoC DRM"
53 #define DRIVER_DATE "20110530"
54 #define DRIVER_MAJOR 1
55 #define DRIVER_MINOR 0
57 #define VBLANK_OFF_DELAY 50000
59 /* TODO (seanpaul): Once we remove platform drivers, we'll be calling the
60 * various panel/controller init functions directly. These init functions will
61 * return to us the ops and context, so we can get rid of these attach
62 * functions. Once the attach functions are gone, we can move this array of
63 * display pointers into the drm device's platform data.
65 * For now, we'll use a global to keep track of things.
67 static struct exynos_drm_display *displays[EXYNOS_DRM_DISPLAY_NUM_DISPLAYS];
70 struct i2c_client *client;
71 struct device_node *node;
75 static int find_bridge(const char *name, struct bridge_init *bridge)
77 bridge->valid = false;
78 bridge->client = NULL;
79 bridge->node = of_find_node_by_name(NULL, name);
83 bridge->client = of_find_i2c_device_by_node(bridge->node);
91 static int exynos_drm_load(struct drm_device *dev, unsigned long flags)
93 struct exynos_drm_private *private;
96 struct bridge_init bridge;
98 DRM_DEBUG("[DEV:%s] flags: 0x%lx\n", dev->devname, flags);
100 private = kzalloc(sizeof(struct exynos_drm_private), GFP_KERNEL);
102 DRM_ERROR("failed to allocate private\n");
106 #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
107 if (kds_callback_init(&private->kds_cb, 1,
108 exynos_drm_kds_callback) < 0) {
109 DRM_ERROR("kds alloc queue failed.\n");
115 DRM_INIT_WAITQUEUE(&private->wait_vsync_queue);
117 dev->dev_private = (void *)private;
119 drm_mode_config_init(dev);
121 /* init kms poll for handling hpd */
122 drm_kms_helper_poll_init(dev);
124 exynos_drm_mode_config_init(dev);
127 * EXYNOS4 is enough to have two CRTCs and each crtc would be used
128 * without dependency of hardware.
130 for (nr = 0; nr < EXYNOS_DRM_DISPLAY_NUM_DISPLAYS; nr++) {
131 ret = exynos_drm_crtc_create(dev, nr, displays[nr]);
136 for (nr = 0; nr < MAX_PLANE; nr++) {
137 struct drm_plane *plane;
138 unsigned int possible_crtcs =
139 (1 << EXYNOS_DRM_DISPLAY_NUM_DISPLAYS) - 1;
141 plane = exynos_plane_init(dev, possible_crtcs, false);
146 ret = drm_vblank_init(dev, EXYNOS_DRM_DISPLAY_NUM_DISPLAYS);
150 ret = find_bridge("ptn3460-bridge", &bridge);
152 DRM_ERROR("Could not get PTN3460 bridge %d\n", ret);
156 ret = ptn3460_init(dev, bridge.client, bridge.node);
158 DRM_ERROR("Failed to initialize the ptn bridge\n");
162 ret = find_bridge("ps8622-bridge", &bridge);
164 DRM_ERROR("Could not get PS8622 bridge %d\n", ret);
169 ret = ps8622_init(dev, bridge.client, bridge.node);
171 DRM_ERROR("Failed to initialize the Parade "
179 * probe sub drivers such as display controller and hdmi driver,
180 * that were registered at probe() of platform driver
181 * to the sub driver and create encoder and connector for them.
183 ret = exynos_drm_device_register(dev);
187 /* setup possible_clones. */
188 exynos_drm_encoder_setup(dev);
191 * create and configure fb helper and also exynos specific
194 ret = exynos_drm_fbdev_init(dev);
196 DRM_ERROR("failed to initialize drm fbdev\n");
200 drm_vblank_offdelay = VBLANK_OFF_DELAY;
205 exynos_drm_device_unregister(dev);
207 drm_vblank_cleanup(dev);
209 drm_mode_config_cleanup(dev);
210 #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
211 kds_callback_term(&private->kds_cb);
219 static int exynos_drm_unload(struct drm_device *dev)
221 struct exynos_drm_private *private = dev->dev_private;
223 DRM_DEBUG("[DEV:%s]\n", dev->devname);
225 exynos_drm_fbdev_fini(dev);
226 exynos_drm_device_unregister(dev);
227 drm_vblank_cleanup(dev);
228 drm_kms_helper_poll_fini(dev);
229 drm_mode_config_cleanup(dev);
230 #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
231 kds_callback_term(&private->kds_cb);
235 dev->dev_private = NULL;
240 static int exynos_drm_open(struct drm_device *dev, struct drm_file *file)
242 struct exynos_drm_file_private *file_private;
244 DRM_DEBUG("[DEV:%s]\n", dev->devname);
246 file_private = kzalloc(sizeof(*file_private), GFP_KERNEL);
248 DRM_ERROR("failed to allocate exynos_drm_file_private\n");
251 INIT_LIST_HEAD(&file_private->gem_cpu_acquire_list);
253 file->driver_priv = file_private;
255 return exynos_drm_subdrv_open(dev, file);
258 static void exynos_drm_preclose(struct drm_device *dev,
259 struct drm_file *file)
261 struct exynos_drm_file_private *file_private = file->driver_priv;
262 struct exynos_drm_gem_obj_node *cur, *d;
264 DRM_DEBUG("[DEV:%s]\n", dev->devname);
266 mutex_lock(&dev->struct_mutex);
267 /* release kds resource sets for outstanding GEM object acquires */
268 list_for_each_entry_safe(cur, d,
269 &file_private->gem_cpu_acquire_list, list) {
270 #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
271 BUG_ON(cur->exynos_gem_obj->resource_set == NULL);
272 kds_resource_set_release(&cur->exynos_gem_obj->resource_set);
274 drm_gem_object_unreference(&cur->exynos_gem_obj->base);
277 mutex_unlock(&dev->struct_mutex);
278 INIT_LIST_HEAD(&file_private->gem_cpu_acquire_list);
280 exynos_drm_subdrv_close(dev, file);
283 static void exynos_drm_postclose(struct drm_device *dev, struct drm_file *file)
285 DRM_DEBUG("[DEV:%s]\n", dev->devname);
287 if (!file->driver_priv)
290 kfree(file->driver_priv);
291 file->driver_priv = NULL;
294 static void exynos_drm_lastclose(struct drm_device *dev)
296 DRM_DEBUG("[DEV:%s]\n", dev->devname);
298 exynos_drm_fbdev_restore_mode(dev);
301 static struct vm_operations_struct exynos_drm_gem_vm_ops = {
302 .fault = exynos_drm_gem_fault,
303 .open = drm_gem_vm_open,
304 .close = drm_gem_vm_close,
307 static struct drm_ioctl_desc exynos_ioctls[] = {
308 DRM_IOCTL_DEF_DRV(EXYNOS_GEM_CREATE, exynos_drm_gem_create_ioctl,
309 DRM_UNLOCKED | DRM_AUTH),
310 DRM_IOCTL_DEF_DRV(EXYNOS_GEM_MAP_OFFSET,
311 exynos_drm_gem_map_offset_ioctl, DRM_UNLOCKED |
313 DRM_IOCTL_DEF_DRV(EXYNOS_GEM_MMAP,
314 exynos_drm_gem_mmap_ioctl, DRM_UNLOCKED | DRM_AUTH),
315 DRM_IOCTL_DEF_DRV(EXYNOS_VIDI_CONNECTION,
316 vidi_connection_ioctl, DRM_UNLOCKED | DRM_AUTH),
317 DRM_IOCTL_DEF_DRV(EXYNOS_GEM_CPU_ACQUIRE,
318 exynos_drm_gem_cpu_acquire_ioctl,
319 DRM_UNLOCKED | DRM_AUTH),
320 DRM_IOCTL_DEF_DRV(EXYNOS_GEM_CPU_RELEASE,
321 exynos_drm_gem_cpu_release_ioctl,
322 DRM_UNLOCKED | DRM_AUTH),
325 static const struct file_operations exynos_drm_driver_fops = {
326 .owner = THIS_MODULE,
328 .mmap = exynos_drm_gem_mmap,
331 .unlocked_ioctl = drm_ioctl,
332 .release = drm_release,
335 static struct drm_driver exynos_drm_driver = {
336 .driver_features = DRIVER_HAVE_IRQ | DRIVER_BUS_PLATFORM |
337 DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME,
338 .load = exynos_drm_load,
339 .unload = exynos_drm_unload,
340 .open = exynos_drm_open,
341 .preclose = exynos_drm_preclose,
342 .lastclose = exynos_drm_lastclose,
343 .postclose = exynos_drm_postclose,
344 .get_vblank_counter = drm_vblank_count,
345 .enable_vblank = exynos_drm_crtc_enable_vblank,
346 .disable_vblank = exynos_drm_crtc_disable_vblank,
347 #if defined(CONFIG_DEBUG_FS)
348 .debugfs_init = exynos_drm_debugfs_init,
349 .debugfs_cleanup = exynos_drm_debugfs_cleanup,
351 .gem_free_object = exynos_drm_gem_free_object,
352 .gem_vm_ops = &exynos_drm_gem_vm_ops,
353 .dumb_create = exynos_drm_gem_dumb_create,
354 .dumb_map_offset = exynos_drm_gem_dumb_map_offset,
355 .dumb_destroy = exynos_drm_gem_dumb_destroy,
356 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
357 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
358 .gem_prime_export = exynos_dmabuf_prime_export,
359 .gem_prime_import = exynos_dmabuf_prime_import,
360 .ioctls = exynos_ioctls,
361 .fops = &exynos_drm_driver_fops,
365 .major = DRIVER_MAJOR,
366 .minor = DRIVER_MINOR,
369 #ifdef CONFIG_EXYNOS_IOMMU
370 static int iommu_init(struct platform_device *pdev)
372 /* DRM device expects a IOMMU mapping to be already
373 * created in FIMD. Else this function should
376 if (exynos_drm_common_mapping==NULL) {
377 printk(KERN_ERR "exynos drm common mapping is invalid\n");
382 * The ordering in Makefile warrants that this is initialized after
383 * FIMD, so only just ensure that it works as expected and we are
384 * reusing the mapping originally created in exynos_drm_fimd.c.
386 WARN_ON(!exynos_drm_common_mapping);
387 if (!s5p_create_iommu_mapping(&pdev->dev, 0,
388 0, 0, exynos_drm_common_mapping)) {
389 printk(KERN_ERR "failed to create IOMMU mapping\n");
396 static void iommu_deinit(struct platform_device *pdev)
398 /* detach the device and mapping */
399 s5p_destroy_iommu_mapping(&pdev->dev);
400 DRM_DEBUG("released the IOMMU mapping\n");
406 static int exynos_drm_platform_probe(struct platform_device *pdev)
408 struct device *dev = &pdev->dev;
411 DRM_DEBUG("[PDEV:%s]\n", pdev->name);
413 #ifdef CONFIG_EXYNOS_IOMMU
414 if (iommu_init(pdev)) {
415 DRM_ERROR("failed to initialize IOMMU\n");
420 exynos_drm_driver.num_ioctls = DRM_ARRAY_SIZE(exynos_ioctls);
422 pm_vt_switch_required(dev, false);
423 pm_runtime_enable(dev);
424 pm_runtime_get_sync(dev);
426 ret = drm_platform_init(&exynos_drm_driver, pdev);
427 #ifdef CONFIG_EXYNOS_IOMMU
435 static int __devexit exynos_drm_platform_remove(struct platform_device *pdev)
437 struct device *dev = &pdev->dev;
439 DRM_DEBUG("[PDEV:%s]\n", pdev->name);
441 pm_vt_switch_unregister(dev);
442 pm_runtime_disable(dev);
444 drm_platform_exit(&exynos_drm_driver, pdev);
446 #ifdef CONFIG_EXYNOS_IOMMU
452 const char *exynos_display_type_name(const struct exynos_drm_display *display)
454 switch (display->display_type) {
455 case EXYNOS_DRM_DISPLAY_TYPE_FIMD:
457 case EXYNOS_DRM_DISPLAY_TYPE_MIXER:
459 case EXYNOS_DRM_DISPLAY_TYPE_VIDI:
466 void exynos_display_attach_panel(enum exynos_drm_display_type type,
467 struct exynos_panel_ops *ops, void *ctx)
470 for (i = 0; i < EXYNOS_DRM_DISPLAY_NUM_DISPLAYS; i++) {
471 if (displays[i]->display_type == type) {
472 displays[i]->panel_ctx = ctx;
473 displays[i]->panel_ops = ops;
479 void exynos_display_attach_controller(enum exynos_drm_display_type type,
480 struct exynos_controller_ops *ops, void *ctx)
483 for (i = 0; i < EXYNOS_DRM_DISPLAY_NUM_DISPLAYS; i++) {
484 if (displays[i]->display_type == type) {
485 displays[i]->controller_ctx = ctx;
486 displays[i]->controller_ops = ops;
492 static int display_subdrv_probe(struct drm_device *drm_dev,
493 struct exynos_drm_subdrv *subdrv)
495 struct exynos_drm_display *display = subdrv->display;
498 if (!display->controller_ops || !display->panel_ops)
501 if (display->controller_ops->subdrv_probe) {
502 ret = display->controller_ops->subdrv_probe(
503 display->controller_ctx, drm_dev);
508 if (display->panel_ops->subdrv_probe) {
509 ret = display->panel_ops->subdrv_probe(display->panel_ctx,
518 int exynos_display_init(struct exynos_drm_display *display,
519 enum exynos_drm_display_type type)
521 struct exynos_drm_subdrv *subdrv;
523 subdrv = kzalloc(sizeof(*subdrv), GFP_KERNEL);
525 DRM_ERROR("Failed to allocate display subdrv\n");
529 display->display_type = type;
531 display->subdrv = subdrv;
533 subdrv->probe = display_subdrv_probe;
534 subdrv->display = display;
535 exynos_drm_subdrv_register(subdrv);
540 void exynos_display_remove(struct exynos_drm_display *display)
542 if (display->subdrv) {
543 exynos_drm_subdrv_unregister(display->subdrv);
544 kfree(display->subdrv);
548 static int exynos_drm_resume_displays(struct drm_device *dev)
552 for (i = 0; i < EXYNOS_DRM_DISPLAY_NUM_DISPLAYS; i++) {
553 struct exynos_drm_display *display = displays[i];
554 struct drm_connector *connector = display->subdrv->connector;
556 if (!connector || !connector->funcs->dpms)
559 connector->funcs->dpms(connector, display->suspend_dpms);
562 drm_helper_resume_force_mode(dev);
567 static int exynos_drm_suspend_displays(void)
571 for (i = 0; i < EXYNOS_DRM_DISPLAY_NUM_DISPLAYS; i++) {
572 struct exynos_drm_display *display = displays[i];
573 struct drm_connector *connector = display->subdrv->connector;
578 display->suspend_dpms = connector->dpms;
579 if (connector->funcs->dpms)
580 connector->funcs->dpms(connector, DRM_MODE_DPMS_OFF);
585 #ifdef CONFIG_PM_SLEEP
586 static int exynos_drm_suspend(struct device *dev)
588 if (pm_runtime_suspended(dev))
591 return exynos_drm_suspend_displays();
594 static int exynos_drm_resume(struct device *dev)
596 struct platform_device *pdev = to_platform_device(dev);
597 struct drm_device *drm_dev = platform_get_drvdata(pdev);
599 if (pm_runtime_suspended(dev))
602 return exynos_drm_resume_displays(drm_dev);
606 #ifdef CONFIG_PM_RUNTIME
607 static int exynos_drm_runtime_resume(struct device *dev)
609 struct platform_device *pdev = to_platform_device(dev);
610 struct drm_device *drm_dev = platform_get_drvdata(pdev);
612 /* Check drm_dev here since this function is called from probe */
616 return exynos_drm_resume_displays(drm_dev);
619 static int exynos_drm_runtime_suspend(struct device *dev)
621 return exynos_drm_suspend_displays();
625 static const struct dev_pm_ops drm_pm_ops = {
626 SET_SYSTEM_SLEEP_PM_OPS(exynos_drm_suspend, exynos_drm_resume)
627 SET_RUNTIME_PM_OPS(exynos_drm_runtime_suspend,
628 exynos_drm_runtime_resume, NULL)
631 static struct platform_driver exynos_drm_platform_driver = {
632 .probe = exynos_drm_platform_probe,
633 .remove = __devexit_p(exynos_drm_platform_remove),
635 .owner = THIS_MODULE,
636 .name = "exynos-drm",
641 static int __init exynos_drm_init(void)
645 DRM_DEBUG_DRIVER("\n");
647 for (i = 0; i < EXYNOS_DRM_DISPLAY_NUM_DISPLAYS; i++) {
648 displays[i] = kzalloc(sizeof(*displays[i]), GFP_KERNEL);
654 ret = exynos_display_init(displays[i], i);
659 #ifdef CONFIG_DRM_EXYNOS_FIMD
660 ret = platform_driver_register(&fimd_driver);
665 #ifdef CONFIG_DRM_EXYNOS_DP
666 ret = platform_driver_register(&dp_driver);
671 #ifdef CONFIG_DRM_EXYNOS_HDMI
672 ret = platform_driver_register(&hdmi_driver);
675 ret = platform_driver_register(&mixer_driver);
680 #ifdef CONFIG_DRM_EXYNOS_VIDI
681 ret = platform_driver_register(&vidi_driver);
686 ret = platform_driver_register(&exynos_drm_platform_driver);
693 #ifdef CONFIG_DRM_EXYNOS_VIDI
695 platform_driver_unregister(&vidi_driver);
698 #ifdef CONFIG_DRM_EXYNOS_HDMI
699 platform_driver_unregister(&mixer_driver);
701 platform_driver_unregister(&hdmi_driver);
705 platform_driver_unregister(&dp_driver);
707 #ifdef CONFIG_DRM_EXYNOS_FIMD
708 platform_driver_unregister(&fimd_driver);
712 for (i = 0; i < EXYNOS_DRM_DISPLAY_NUM_DISPLAYS; i++) {
716 exynos_display_remove(displays[i]);
722 static void __exit exynos_drm_exit(void)
726 DRM_DEBUG_DRIVER("\n");
728 platform_driver_unregister(&exynos_drm_platform_driver);
730 #ifdef CONFIG_DRM_EXYNOS_HDMI
731 platform_driver_unregister(&mixer_driver);
732 platform_driver_unregister(&hdmi_driver);
735 #ifdef CONFIG_DRM_EXYNOS_VIDI
736 platform_driver_unregister(&vidi_driver);
739 platform_driver_unregister(&dp_driver);
740 #ifdef CONFIG_DRM_EXYNOS_FIMD
741 platform_driver_unregister(&fimd_driver);
744 for (i = 0; i < EXYNOS_DRM_DISPLAY_NUM_DISPLAYS; i++) {
748 exynos_display_remove(displays[i]);
753 module_init(exynos_drm_init);
754 module_exit(exynos_drm_exit);
756 MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
757 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
758 MODULE_AUTHOR("Seung-Woo Kim <sw0312.kim@samsung.com>");
759 MODULE_DESCRIPTION("Samsung SoC DRM Driver");
760 MODULE_LICENSE("GPL");