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 #define DRIVER_NAME "exynos"
46 #define DRIVER_DESC "Samsung SoC DRM"
47 #define DRIVER_DATE "20110530"
48 #define DRIVER_MAJOR 1
49 #define DRIVER_MINOR 0
51 #define VBLANK_OFF_DELAY 50000
53 static int exynos_drm_load(struct drm_device *dev, unsigned long flags)
55 struct exynos_drm_private *private;
59 DRM_DEBUG_DRIVER("%s\n", __FILE__);
61 private = kzalloc(sizeof(struct exynos_drm_private), GFP_KERNEL);
63 DRM_ERROR("failed to allocate private\n");
67 #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
68 if (kds_callback_init(&private->kds_cb, 1,
69 exynos_drm_kds_callback) < 0) {
70 DRM_ERROR("kds alloc queue failed.\n");
74 if (kds_callback_init(&private->kds_cb_rm_fb, 0,
75 exynos_drm_kds_callback_rm_fb) < 0) {
76 DRM_ERROR("kds alloc queue failed.\n");
82 DRM_INIT_WAITQUEUE(&private->wait_vsync_queue);
83 atomic_set(&private->wait_vsync_event, 0);
85 dev->dev_private = (void *)private;
87 drm_mode_config_init(dev);
89 /* init kms poll for handling hpd */
90 drm_kms_helper_poll_init(dev);
92 exynos_drm_mode_config_init(dev);
95 * EXYNOS4 is enough to have two CRTCs and each crtc would be used
96 * without dependency of hardware.
98 for (nr = 0; nr < MAX_CRTC; nr++) {
99 ret = exynos_drm_crtc_create(dev, nr);
104 for (nr = 0; nr < MAX_PLANE; nr++) {
105 ret = exynos_plane_init(dev, nr);
110 ret = drm_vblank_init(dev, MAX_CRTC);
115 * probe sub drivers such as display controller and hdmi driver,
116 * that were registered at probe() of platform driver
117 * to the sub driver and create encoder and connector for them.
119 ret = exynos_drm_device_register(dev);
123 /* setup possible_clones. */
124 exynos_drm_encoder_setup(dev);
127 * create and configure fb helper and also exynos specific
130 ret = exynos_drm_fbdev_init(dev);
132 DRM_ERROR("failed to initialize drm fbdev\n");
136 drm_vblank_offdelay = VBLANK_OFF_DELAY;
141 exynos_drm_device_unregister(dev);
143 drm_vblank_cleanup(dev);
145 drm_mode_config_cleanup(dev);
146 #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
147 kds_callback_term(&private->kds_cb_rm_fb);
149 kds_callback_term(&private->kds_cb);
157 static int exynos_drm_unload(struct drm_device *dev)
159 struct exynos_drm_private *private = dev->dev_private;
161 DRM_DEBUG_DRIVER("%s\n", __FILE__);
163 exynos_drm_fbdev_fini(dev);
164 exynos_drm_device_unregister(dev);
165 drm_vblank_cleanup(dev);
166 drm_kms_helper_poll_fini(dev);
167 drm_mode_config_cleanup(dev);
168 #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
169 kds_callback_term(&private->kds_cb);
173 dev->dev_private = NULL;
178 static int exynos_drm_open(struct drm_device *dev, struct drm_file *file)
180 DRM_DEBUG_DRIVER("%s\n", __FILE__);
182 return exynos_drm_subdrv_open(dev, file);
185 static void exynos_drm_preclose(struct drm_device *dev,
186 struct drm_file *file)
188 DRM_DEBUG_DRIVER("%s\n", __FILE__);
190 exynos_drm_subdrv_close(dev, file);
193 static void exynos_drm_postclose(struct drm_device *dev, struct drm_file *file)
195 DRM_DEBUG_DRIVER("%s\n", __FILE__);
197 if (!file->driver_priv)
200 kfree(file->driver_priv);
201 file->driver_priv = NULL;
204 static void exynos_drm_lastclose(struct drm_device *dev)
206 DRM_DEBUG_DRIVER("%s\n", __FILE__);
208 exynos_drm_fbdev_restore_mode(dev);
211 static struct vm_operations_struct exynos_drm_gem_vm_ops = {
212 .fault = exynos_drm_gem_fault,
213 .open = drm_gem_vm_open,
214 .close = drm_gem_vm_close,
217 static struct drm_ioctl_desc exynos_ioctls[] = {
218 DRM_IOCTL_DEF_DRV(EXYNOS_GEM_CREATE, exynos_drm_gem_create_ioctl,
219 DRM_UNLOCKED | DRM_AUTH),
220 DRM_IOCTL_DEF_DRV(EXYNOS_GEM_MAP_OFFSET,
221 exynos_drm_gem_map_offset_ioctl, DRM_UNLOCKED |
223 DRM_IOCTL_DEF_DRV(EXYNOS_GEM_MMAP,
224 exynos_drm_gem_mmap_ioctl, DRM_UNLOCKED | DRM_AUTH),
225 DRM_IOCTL_DEF_DRV(EXYNOS_PLANE_SET_ZPOS, exynos_plane_set_zpos_ioctl,
226 DRM_UNLOCKED | DRM_AUTH),
227 DRM_IOCTL_DEF_DRV(EXYNOS_VIDI_CONNECTION,
228 vidi_connection_ioctl, DRM_UNLOCKED | DRM_AUTH),
231 static const struct file_operations exynos_drm_driver_fops = {
232 .owner = THIS_MODULE,
234 .mmap = exynos_drm_gem_mmap,
237 .unlocked_ioctl = drm_ioctl,
238 .release = drm_release,
241 static struct drm_driver exynos_drm_driver = {
242 .driver_features = DRIVER_HAVE_IRQ | DRIVER_BUS_PLATFORM |
243 DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME,
244 .load = exynos_drm_load,
245 .unload = exynos_drm_unload,
246 .open = exynos_drm_open,
247 .preclose = exynos_drm_preclose,
248 .lastclose = exynos_drm_lastclose,
249 .postclose = exynos_drm_postclose,
250 .get_vblank_counter = drm_vblank_count,
251 .enable_vblank = exynos_drm_crtc_enable_vblank,
252 .disable_vblank = exynos_drm_crtc_disable_vblank,
253 .gem_init_object = exynos_drm_gem_init_object,
254 .gem_free_object = exynos_drm_gem_free_object,
255 .gem_vm_ops = &exynos_drm_gem_vm_ops,
256 .dumb_create = exynos_drm_gem_dumb_create,
257 .dumb_map_offset = exynos_drm_gem_dumb_map_offset,
258 .dumb_destroy = exynos_drm_gem_dumb_destroy,
259 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
260 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
261 .gem_prime_export = exynos_dmabuf_prime_export,
262 .gem_prime_import = exynos_dmabuf_prime_import,
263 .ioctls = exynos_ioctls,
264 .fops = &exynos_drm_driver_fops,
268 .major = DRIVER_MAJOR,
269 .minor = DRIVER_MINOR,
272 #ifdef CONFIG_EXYNOS_IOMMU
273 static int iommu_init(struct platform_device *pdev)
275 /* DRM device expects a IOMMU mapping to be already
276 * created in FIMD. Else this function should
279 if (exynos_drm_common_mapping==NULL) {
280 printk(KERN_ERR "exynos drm common mapping is invalid\n");
285 * The ordering in Makefile warrants that this is initialized after
286 * FIMD, so only just ensure that it works as expected and we are
287 * reusing the mapping originally created in exynos_drm_fimd.c.
289 WARN_ON(!exynos_drm_common_mapping);
290 if (!s5p_create_iommu_mapping(&pdev->dev, 0,
291 0, 0, exynos_drm_common_mapping)) {
292 printk(KERN_ERR "failed to create IOMMU mapping\n");
299 static void iommu_deinit(struct platform_device *pdev)
301 /* detach the device and mapping */
302 s5p_destroy_iommu_mapping(&pdev->dev);
303 DRM_DEBUG("released the IOMMU mapping\n");
309 static int exynos_drm_platform_probe(struct platform_device *pdev)
311 struct device *dev = &pdev->dev;
313 DRM_DEBUG_DRIVER("%s\n", __FILE__);
315 #ifdef CONFIG_EXYNOS_IOMMU
316 if (iommu_init(pdev)) {
317 DRM_ERROR("failed to initialize IOMMU\n");
322 exynos_drm_driver.num_ioctls = DRM_ARRAY_SIZE(exynos_ioctls);
324 pm_runtime_enable(dev);
325 pm_runtime_get_sync(dev);
327 return drm_platform_init(&exynos_drm_driver, pdev);
330 static int __devexit exynos_drm_platform_remove(struct platform_device *pdev)
332 struct device *dev = &pdev->dev;
334 DRM_DEBUG_DRIVER("%s\n", __FILE__);
336 pm_runtime_disable(dev);
338 drm_platform_exit(&exynos_drm_driver, pdev);
340 #ifdef CONFIG_EXYNOS_IOMMU
346 /* TODO (seanpaul): Once we remove platform drivers, we'll be calling the
347 * various panel/controller init functions directly. These init functions will
348 * return to us the ops and context, so we can get rid of these attach
349 * functions. Once the attach functions are gone, we can move this array of
350 * display pointers into the drm device's platform data.
352 * For now, we'll use a global to keep track of things.
354 static struct exynos_drm_display *displays[EXYNOS_DRM_DISPLAY_NUM_DISPLAYS];
356 void exynos_display_attach_panel(enum exynos_drm_display_type type,
357 struct exynos_panel_ops *ops, void *ctx)
360 for (i = 0; i < EXYNOS_DRM_DISPLAY_NUM_DISPLAYS; i++) {
361 if (displays[i]->display_type == type) {
362 displays[i]->panel_ctx = ctx;
363 displays[i]->panel_ops = ops;
369 void exynos_display_attach_controller(enum exynos_drm_display_type type,
370 struct exynos_controller_ops *ops, void *ctx)
373 for (i = 0; i < EXYNOS_DRM_DISPLAY_NUM_DISPLAYS; i++) {
374 if (displays[i]->display_type == type) {
375 displays[i]->controller_ctx = ctx;
376 displays[i]->controller_ops = ops;
382 static int display_subdrv_probe(struct drm_device *drm_dev,
383 struct exynos_drm_subdrv *subdrv)
385 struct exynos_drm_display *display = subdrv->display;
388 if (!display->controller_ops || !display->panel_ops)
391 if (display->controller_ops->subdrv_probe) {
392 ret = display->controller_ops->subdrv_probe(
393 display->controller_ctx, drm_dev);
398 if (display->panel_ops->subdrv_probe) {
399 ret = display->panel_ops->subdrv_probe(display->panel_ctx,
408 int exynos_display_init(struct exynos_drm_display *display,
409 enum exynos_drm_display_type type)
411 struct exynos_drm_subdrv *subdrv;
413 subdrv = kzalloc(sizeof(*subdrv), GFP_KERNEL);
415 DRM_ERROR("Failed to allocate display subdrv\n");
419 display->display_type = type;
421 display->subdrv = subdrv;
423 subdrv->probe = display_subdrv_probe;
424 subdrv->display = display;
425 exynos_drm_subdrv_register(subdrv);
430 void exynos_display_remove(struct exynos_drm_display *display)
432 if (display->subdrv) {
433 exynos_drm_subdrv_unregister(display->subdrv);
434 kfree(display->subdrv);
438 static int exynos_drm_resume_displays(void)
442 for (i = 0; i < EXYNOS_DRM_DISPLAY_NUM_DISPLAYS; i++) {
443 struct exynos_drm_display *display = displays[i];
444 struct drm_encoder *encoder = display->subdrv->encoder;
449 exynos_drm_encoder_dpms(encoder, display->suspend_dpms);
454 static int exynos_drm_suspend_displays(void)
458 for (i = 0; i < EXYNOS_DRM_DISPLAY_NUM_DISPLAYS; i++) {
459 struct exynos_drm_display *display = displays[i];
460 struct drm_encoder *encoder = display->subdrv->encoder;
465 display->suspend_dpms = exynos_drm_encoder_get_dpms(encoder);
466 exynos_drm_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
471 #ifdef CONFIG_PM_SLEEP
472 static int exynos_drm_suspend(struct device *dev)
474 if (pm_runtime_suspended(dev))
477 return exynos_drm_suspend_displays();
480 static int exynos_drm_resume(struct device *dev)
482 if (pm_runtime_suspended(dev))
485 return exynos_drm_resume_displays();
489 #ifdef CONFIG_PM_RUNTIME
490 static int exynos_drm_runtime_resume(struct device *dev)
492 return exynos_drm_resume_displays();
495 static int exynos_drm_runtime_suspend(struct device *dev)
497 return exynos_drm_suspend_displays();
501 static const struct dev_pm_ops drm_pm_ops = {
502 SET_SYSTEM_SLEEP_PM_OPS(exynos_drm_suspend, exynos_drm_resume)
503 SET_RUNTIME_PM_OPS(exynos_drm_runtime_suspend,
504 exynos_drm_runtime_resume, NULL)
507 static struct platform_driver exynos_drm_platform_driver = {
508 .probe = exynos_drm_platform_probe,
509 .remove = __devexit_p(exynos_drm_platform_remove),
511 .owner = THIS_MODULE,
512 .name = "exynos-drm",
517 static int __init exynos_drm_init(void)
521 DRM_DEBUG_DRIVER("%s\n", __FILE__);
523 for (i = 0; i < EXYNOS_DRM_DISPLAY_NUM_DISPLAYS; i++) {
524 displays[i] = kzalloc(sizeof(*displays[i]), GFP_KERNEL);
530 ret = exynos_display_init(displays[i], i);
535 #ifdef CONFIG_DRM_EXYNOS_FIMD
536 ret = platform_driver_register(&fimd_driver);
541 #ifdef CONFIG_DRM_EXYNOS_DP
542 ret = platform_driver_register(&dp_driver);
547 #ifdef CONFIG_DRM_EXYNOS_HDMI
548 ret = platform_driver_register(&hdmi_driver);
551 ret = platform_driver_register(&mixer_driver);
556 #ifdef CONFIG_DRM_EXYNOS_VIDI
557 ret = platform_driver_register(&vidi_driver);
562 ret = platform_driver_register(&exynos_drm_platform_driver);
569 #ifdef CONFIG_DRM_EXYNOS_VIDI
571 platform_driver_unregister(&vidi_driver);
574 #ifdef CONFIG_DRM_EXYNOS_HDMI
575 platform_driver_unregister(&mixer_driver);
577 platform_driver_unregister(&hdmi_driver);
581 platform_driver_unregister(&dp_driver);
583 #ifdef CONFIG_DRM_EXYNOS_FIMD
584 platform_driver_unregister(&fimd_driver);
588 for (i = 0; i < EXYNOS_DRM_DISPLAY_NUM_DISPLAYS; i++) {
592 exynos_display_remove(displays[i]);
598 static void __exit exynos_drm_exit(void)
602 DRM_DEBUG_DRIVER("%s\n", __FILE__);
604 platform_driver_unregister(&exynos_drm_platform_driver);
606 #ifdef CONFIG_DRM_EXYNOS_HDMI
607 platform_driver_unregister(&mixer_driver);
608 platform_driver_unregister(&hdmi_driver);
611 #ifdef CONFIG_DRM_EXYNOS_VIDI
612 platform_driver_unregister(&vidi_driver);
615 platform_driver_unregister(&dp_driver);
616 #ifdef CONFIG_DRM_EXYNOS_FIMD
617 platform_driver_unregister(&fimd_driver);
620 for (i = 0; i < EXYNOS_DRM_DISPLAY_NUM_DISPLAYS; i++) {
624 exynos_display_remove(displays[i]);
629 module_init(exynos_drm_init);
630 module_exit(exynos_drm_exit);
632 MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
633 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
634 MODULE_AUTHOR("Seung-Woo Kim <sw0312.kim@samsung.com>");
635 MODULE_DESCRIPTION("Samsung SoC DRM Driver");
636 MODULE_LICENSE("GPL");