Merge branch 'chromeos-exynos-3.4' into chromeos-3.4
[cascardo/linux.git] / drivers / gpu / drm / exynos / exynos_drm_drv.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd.
3  * Authors:
4  *      Inki Dae <inki.dae@samsung.com>
5  *      Joonyoung Shim <jy0922.shim@samsung.com>
6  *      Seung-Woo Kim <sw0312.kim@samsung.com>
7  *
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:
14  *
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
17  * Software.
18  *
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.
26  */
27
28 #include "drmP.h"
29 #include "drm.h"
30 #include "drm_crtc_helper.h"
31
32 #include <drm/exynos_drm.h>
33
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
44 #define DRIVER_NAME     "exynos"
45 #define DRIVER_DESC     "Samsung SoC DRM"
46 #define DRIVER_DATE     "20110530"
47 #define DRIVER_MAJOR    1
48 #define DRIVER_MINOR    0
49
50 #define VBLANK_OFF_DELAY        50000
51
52 static int exynos_drm_load(struct drm_device *dev, unsigned long flags)
53 {
54         struct exynos_drm_private *private;
55         int ret;
56         int nr;
57
58         DRM_DEBUG_DRIVER("%s\n", __FILE__);
59
60         private = kzalloc(sizeof(struct exynos_drm_private), GFP_KERNEL);
61         if (!private) {
62                 DRM_ERROR("failed to allocate private\n");
63                 return -ENOMEM;
64         }
65
66         INIT_LIST_HEAD(&private->pageflip_event_list);
67         dev->dev_private = (void *)private;
68
69         drm_mode_config_init(dev);
70
71         /* init kms poll for handling hpd */
72         drm_kms_helper_poll_init(dev);
73
74         exynos_drm_mode_config_init(dev);
75
76         /*
77          * EXYNOS4 is enough to have two CRTCs and each crtc would be used
78          * without dependency of hardware.
79          */
80         for (nr = 0; nr < MAX_CRTC; nr++) {
81                 ret = exynos_drm_crtc_create(dev, nr);
82                 if (ret)
83                         goto err_crtc;
84         }
85
86         for (nr = 0; nr < MAX_PLANE; nr++) {
87                 ret = exynos_plane_init(dev, nr);
88                 if (ret)
89                         goto err_crtc;
90         }
91
92         ret = drm_vblank_init(dev, MAX_CRTC);
93         if (ret)
94                 goto err_crtc;
95
96         /*
97          * probe sub drivers such as display controller and hdmi driver,
98          * that were registered at probe() of platform driver
99          * to the sub driver and create encoder and connector for them.
100          */
101         ret = exynos_drm_device_register(dev);
102         if (ret)
103                 goto err_vblank;
104
105         /* setup possible_clones. */
106         exynos_drm_encoder_setup(dev);
107
108         /*
109          * create and configure fb helper and also exynos specific
110          * fbdev object.
111          */
112         ret = exynos_drm_fbdev_init(dev);
113         if (ret) {
114                 DRM_ERROR("failed to initialize drm fbdev\n");
115                 goto err_drm_device;
116         }
117
118         drm_vblank_offdelay = VBLANK_OFF_DELAY;
119
120         return 0;
121
122 err_drm_device:
123         exynos_drm_device_unregister(dev);
124 err_vblank:
125         drm_vblank_cleanup(dev);
126 err_crtc:
127         drm_mode_config_cleanup(dev);
128         kfree(private);
129
130         return ret;
131 }
132
133 static int exynos_drm_unload(struct drm_device *dev)
134 {
135         DRM_DEBUG_DRIVER("%s\n", __FILE__);
136
137         exynos_drm_fbdev_fini(dev);
138         exynos_drm_device_unregister(dev);
139         drm_vblank_cleanup(dev);
140         drm_kms_helper_poll_fini(dev);
141         drm_mode_config_cleanup(dev);
142         kfree(dev->dev_private);
143
144         dev->dev_private = NULL;
145
146         return 0;
147 }
148
149 static int exynos_drm_open(struct drm_device *dev, struct drm_file *file)
150 {
151         DRM_DEBUG_DRIVER("%s\n", __FILE__);
152
153         drm_prime_init_file_private(&file->prime);
154
155         return exynos_drm_subdrv_open(dev, file);
156 }
157
158 static void exynos_drm_preclose(struct drm_device *dev,
159                                         struct drm_file *file)
160 {
161         struct exynos_drm_private *private = dev->dev_private;
162         struct drm_pending_vblank_event *e, *t;
163         unsigned long flags;
164
165         DRM_DEBUG_DRIVER("%s\n", __FILE__);
166
167         /* release events of current file */
168         spin_lock_irqsave(&dev->event_lock, flags);
169         list_for_each_entry_safe(e, t, &private->pageflip_event_list,
170                         base.link) {
171                 if (e->base.file_priv == file) {
172                         list_del(&e->base.link);
173                         e->base.destroy(&e->base);
174                 }
175         }
176         drm_prime_destroy_file_private(&file->prime);
177         spin_unlock_irqrestore(&dev->event_lock, flags);
178
179         exynos_drm_subdrv_close(dev, file);
180 }
181
182 static void exynos_drm_postclose(struct drm_device *dev, struct drm_file *file)
183 {
184         DRM_DEBUG_DRIVER("%s\n", __FILE__);
185
186         if (!file->driver_priv)
187                 return;
188
189         kfree(file->driver_priv);
190         file->driver_priv = NULL;
191 }
192
193 static void exynos_drm_lastclose(struct drm_device *dev)
194 {
195         DRM_DEBUG_DRIVER("%s\n", __FILE__);
196
197         exynos_drm_fbdev_restore_mode(dev);
198 }
199
200 static struct vm_operations_struct exynos_drm_gem_vm_ops = {
201         .fault = exynos_drm_gem_fault,
202         .open = drm_gem_vm_open,
203         .close = drm_gem_vm_close,
204 };
205
206 static struct drm_ioctl_desc exynos_ioctls[] = {
207         DRM_IOCTL_DEF_DRV(EXYNOS_GEM_CREATE, exynos_drm_gem_create_ioctl,
208                         DRM_UNLOCKED | DRM_AUTH),
209         DRM_IOCTL_DEF_DRV(EXYNOS_GEM_MAP_OFFSET,
210                         exynos_drm_gem_map_offset_ioctl, DRM_UNLOCKED |
211                         DRM_AUTH),
212         DRM_IOCTL_DEF_DRV(EXYNOS_GEM_MMAP,
213                         exynos_drm_gem_mmap_ioctl, DRM_UNLOCKED | DRM_AUTH),
214         DRM_IOCTL_DEF_DRV(EXYNOS_PLANE_SET_ZPOS, exynos_plane_set_zpos_ioctl,
215                         DRM_UNLOCKED | DRM_AUTH),
216         DRM_IOCTL_DEF_DRV(EXYNOS_VIDI_CONNECTION,
217                         vidi_connection_ioctl, DRM_UNLOCKED | DRM_AUTH),
218 };
219
220 static const struct file_operations exynos_drm_driver_fops = {
221         .owner          = THIS_MODULE,
222         .open           = drm_open,
223         .mmap           = exynos_drm_gem_mmap,
224         .poll           = drm_poll,
225         .read           = drm_read,
226         .unlocked_ioctl = drm_ioctl,
227         .release        = drm_release,
228 };
229
230 static struct drm_driver exynos_drm_driver = {
231         .driver_features        = DRIVER_HAVE_IRQ | DRIVER_BUS_PLATFORM |
232                                   DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME,
233         .load                   = exynos_drm_load,
234         .unload                 = exynos_drm_unload,
235         .open                   = exynos_drm_open,
236         .preclose               = exynos_drm_preclose,
237         .lastclose              = exynos_drm_lastclose,
238         .postclose              = exynos_drm_postclose,
239         .get_vblank_counter     = drm_vblank_count,
240         .enable_vblank          = exynos_drm_crtc_enable_vblank,
241         .disable_vblank         = exynos_drm_crtc_disable_vblank,
242         .gem_init_object        = exynos_drm_gem_init_object,
243         .gem_free_object        = exynos_drm_gem_free_object,
244         .gem_vm_ops             = &exynos_drm_gem_vm_ops,
245         .dumb_create            = exynos_drm_gem_dumb_create,
246         .dumb_map_offset        = exynos_drm_gem_dumb_map_offset,
247         .dumb_destroy           = exynos_drm_gem_dumb_destroy,
248         .prime_handle_to_fd     = drm_gem_prime_handle_to_fd,
249         .prime_fd_to_handle     = drm_gem_prime_fd_to_handle,
250         .gem_prime_export       = exynos_dmabuf_prime_export,
251         .gem_prime_import       = exynos_dmabuf_prime_import,
252         .ioctls                 = exynos_ioctls,
253         .fops                   = &exynos_drm_driver_fops,
254         .name   = DRIVER_NAME,
255         .desc   = DRIVER_DESC,
256         .date   = DRIVER_DATE,
257         .major  = DRIVER_MAJOR,
258         .minor  = DRIVER_MINOR,
259 };
260
261 #ifdef CONFIG_EXYNOS_IOMMU
262 static int iommu_init(struct platform_device *pdev)
263 {
264         /* DRM device expects a IOMMU mapping to be already
265          * created in FIMD. Else this function should
266          * throw an error.
267          */
268         if (exynos_drm_common_mapping==NULL) {
269                 printk(KERN_ERR "exynos drm common mapping is invalid\n");
270                 return -1;
271         }
272
273         if (!s5p_create_iommu_mapping(&pdev->dev, 0,
274                                 0, 0, exynos_drm_common_mapping)) {
275                 printk(KERN_ERR "failed to create IOMMU mapping\n");
276                 return -1;
277         }
278
279         return 0;
280 }
281 #endif
282
283 static int exynos_drm_platform_probe(struct platform_device *pdev)
284 {
285         DRM_DEBUG_DRIVER("%s\n", __FILE__);
286
287 #ifdef CONFIG_EXYNOS_IOMMU
288         if (iommu_init(pdev)) {
289                 DRM_ERROR("failed to initialize IOMMU\n");
290                 return -ENODEV;
291         }
292 #endif
293
294         exynos_drm_driver.num_ioctls = DRM_ARRAY_SIZE(exynos_ioctls);
295
296         return drm_platform_init(&exynos_drm_driver, pdev);
297 }
298
299 static int exynos_drm_platform_remove(struct platform_device *pdev)
300 {
301         DRM_DEBUG_DRIVER("%s\n", __FILE__);
302
303         drm_platform_exit(&exynos_drm_driver, pdev);
304
305         return 0;
306 }
307
308 static struct platform_driver exynos_drm_platform_driver = {
309         .probe          = exynos_drm_platform_probe,
310         .remove         = __devexit_p(exynos_drm_platform_remove),
311         .driver         = {
312                 .owner  = THIS_MODULE,
313                 .name   = "exynos-drm",
314         },
315 };
316
317 static int __init exynos_drm_init(void)
318 {
319         int ret;
320
321         DRM_DEBUG_DRIVER("%s\n", __FILE__);
322
323 #ifdef CONFIG_DRM_EXYNOS_FIMD
324         ret = platform_driver_register(&fimd_driver);
325         if (ret < 0)
326                 goto out_fimd;
327 #endif
328
329 #ifdef CONFIG_DRM_EXYNOS_HDMI
330         ret = platform_driver_register(&hdmi_driver);
331         if (ret < 0)
332                 goto out_hdmi;
333         ret = platform_driver_register(&mixer_driver);
334         if (ret < 0)
335                 goto out_mixer;
336         ret = platform_driver_register(&exynos_drm_common_hdmi_driver);
337         if (ret < 0)
338                 goto out_common_hdmi;
339 #endif
340
341 #ifdef CONFIG_DRM_EXYNOS_VIDI
342         ret = platform_driver_register(&vidi_driver);
343         if (ret < 0)
344                 goto out_vidi;
345 #endif
346
347         ret = platform_driver_register(&exynos_drm_platform_driver);
348         if (ret < 0)
349                 goto out;
350
351         return 0;
352
353 out:
354 #ifdef CONFIG_DRM_EXYNOS_VIDI
355 out_vidi:
356         platform_driver_unregister(&vidi_driver);
357 #endif
358
359 #ifdef CONFIG_DRM_EXYNOS_HDMI
360         platform_driver_unregister(&exynos_drm_common_hdmi_driver);
361 out_common_hdmi:
362         platform_driver_unregister(&mixer_driver);
363 out_mixer:
364         platform_driver_unregister(&hdmi_driver);
365 out_hdmi:
366 #endif
367
368 #ifdef CONFIG_DRM_EXYNOS_FIMD
369         platform_driver_unregister(&fimd_driver);
370 out_fimd:
371 #endif
372         return ret;
373 }
374
375 static void __exit exynos_drm_exit(void)
376 {
377         DRM_DEBUG_DRIVER("%s\n", __FILE__);
378
379         platform_driver_unregister(&exynos_drm_platform_driver);
380
381 #ifdef CONFIG_DRM_EXYNOS_HDMI
382         platform_driver_unregister(&exynos_drm_common_hdmi_driver);
383         platform_driver_unregister(&mixer_driver);
384         platform_driver_unregister(&hdmi_driver);
385 #endif
386
387 #ifdef CONFIG_DRM_EXYNOS_VIDI
388         platform_driver_unregister(&vidi_driver);
389 #endif
390
391 #ifdef CONFIG_DRM_EXYNOS_FIMD
392         platform_driver_unregister(&fimd_driver);
393 #endif
394 }
395
396 module_init(exynos_drm_init);
397 module_exit(exynos_drm_exit);
398
399 MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
400 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
401 MODULE_AUTHOR("Seung-Woo Kim <sw0312.kim@samsung.com>");
402 MODULE_DESCRIPTION("Samsung SoC DRM Driver");
403 MODULE_LICENSE("GPL");