69c724135caad07820b408b615712e9cf89e7721
[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 #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
67         if (kds_callback_init(&private->kds_cb, 1,
68                               exynos_drm_kds_callback) < 0) {
69                 DRM_ERROR("kds alloc queue failed.\n");
70                 ret = -ENOMEM;
71                 goto err_kds;
72         }
73         if (kds_callback_init(&private->kds_cb_rm_fb, 0,
74                               exynos_drm_kds_callback_rm_fb) < 0) {
75                 DRM_ERROR("kds alloc queue failed.\n");
76                 ret = -ENOMEM;
77                 goto err_kds_rm_fb;
78         }
79 #endif
80
81         DRM_INIT_WAITQUEUE(&private->wait_vsync_queue);
82         atomic_set(&private->wait_vsync_event, 0);
83
84         dev->dev_private = (void *)private;
85
86         drm_mode_config_init(dev);
87
88         /* init kms poll for handling hpd */
89         drm_kms_helper_poll_init(dev);
90
91         exynos_drm_mode_config_init(dev);
92
93         /*
94          * EXYNOS4 is enough to have two CRTCs and each crtc would be used
95          * without dependency of hardware.
96          */
97         for (nr = 0; nr < MAX_CRTC; nr++) {
98                 ret = exynos_drm_crtc_create(dev, nr);
99                 if (ret)
100                         goto err_crtc;
101         }
102
103         for (nr = 0; nr < MAX_PLANE; nr++) {
104                 ret = exynos_plane_init(dev, nr);
105                 if (ret)
106                         goto err_crtc;
107         }
108
109         ret = drm_vblank_init(dev, MAX_CRTC);
110         if (ret)
111                 goto err_crtc;
112
113         /*
114          * probe sub drivers such as display controller and hdmi driver,
115          * that were registered at probe() of platform driver
116          * to the sub driver and create encoder and connector for them.
117          */
118         ret = exynos_drm_device_register(dev);
119         if (ret)
120                 goto err_vblank;
121
122         /* setup possible_clones. */
123         exynos_drm_encoder_setup(dev);
124
125         /*
126          * create and configure fb helper and also exynos specific
127          * fbdev object.
128          */
129         ret = exynos_drm_fbdev_init(dev);
130         if (ret) {
131                 DRM_ERROR("failed to initialize drm fbdev\n");
132                 goto err_drm_device;
133         }
134
135         drm_vblank_offdelay = VBLANK_OFF_DELAY;
136
137         return 0;
138
139 err_drm_device:
140         exynos_drm_device_unregister(dev);
141 err_vblank:
142         drm_vblank_cleanup(dev);
143 err_crtc:
144         drm_mode_config_cleanup(dev);
145 #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
146         kds_callback_term(&private->kds_cb_rm_fb);
147 err_kds_rm_fb:
148         kds_callback_term(&private->kds_cb);
149 err_kds:
150 #endif
151         kfree(private);
152
153         return ret;
154 }
155
156 static int exynos_drm_unload(struct drm_device *dev)
157 {
158         struct exynos_drm_private *private = dev->dev_private;
159
160         DRM_DEBUG_DRIVER("%s\n", __FILE__);
161
162         exynos_drm_fbdev_fini(dev);
163         exynos_drm_device_unregister(dev);
164         drm_vblank_cleanup(dev);
165         drm_kms_helper_poll_fini(dev);
166         drm_mode_config_cleanup(dev);
167 #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
168         kds_callback_term(&private->kds_cb);
169 #endif
170         kfree(private);
171
172         dev->dev_private = NULL;
173
174         return 0;
175 }
176
177 static int exynos_drm_open(struct drm_device *dev, struct drm_file *file)
178 {
179         DRM_DEBUG_DRIVER("%s\n", __FILE__);
180
181         return exynos_drm_subdrv_open(dev, file);
182 }
183
184 static void exynos_drm_preclose(struct drm_device *dev,
185                                         struct drm_file *file)
186 {
187         DRM_DEBUG_DRIVER("%s\n", __FILE__);
188
189         exynos_drm_subdrv_close(dev, file);
190 }
191
192 static void exynos_drm_postclose(struct drm_device *dev, struct drm_file *file)
193 {
194         DRM_DEBUG_DRIVER("%s\n", __FILE__);
195
196         if (!file->driver_priv)
197                 return;
198
199         kfree(file->driver_priv);
200         file->driver_priv = NULL;
201 }
202
203 static void exynos_drm_lastclose(struct drm_device *dev)
204 {
205         DRM_DEBUG_DRIVER("%s\n", __FILE__);
206
207         exynos_drm_fbdev_restore_mode(dev);
208 }
209
210 static struct vm_operations_struct exynos_drm_gem_vm_ops = {
211         .fault = exynos_drm_gem_fault,
212         .open = drm_gem_vm_open,
213         .close = drm_gem_vm_close,
214 };
215
216 static struct drm_ioctl_desc exynos_ioctls[] = {
217         DRM_IOCTL_DEF_DRV(EXYNOS_GEM_CREATE, exynos_drm_gem_create_ioctl,
218                         DRM_UNLOCKED | DRM_AUTH),
219         DRM_IOCTL_DEF_DRV(EXYNOS_GEM_MAP_OFFSET,
220                         exynos_drm_gem_map_offset_ioctl, DRM_UNLOCKED |
221                         DRM_AUTH),
222         DRM_IOCTL_DEF_DRV(EXYNOS_GEM_MMAP,
223                         exynos_drm_gem_mmap_ioctl, DRM_UNLOCKED | DRM_AUTH),
224         DRM_IOCTL_DEF_DRV(EXYNOS_PLANE_SET_ZPOS, exynos_plane_set_zpos_ioctl,
225                         DRM_UNLOCKED | DRM_AUTH),
226         DRM_IOCTL_DEF_DRV(EXYNOS_VIDI_CONNECTION,
227                         vidi_connection_ioctl, DRM_UNLOCKED | DRM_AUTH),
228 };
229
230 static const struct file_operations exynos_drm_driver_fops = {
231         .owner          = THIS_MODULE,
232         .open           = drm_open,
233         .mmap           = exynos_drm_gem_mmap,
234         .poll           = drm_poll,
235         .read           = drm_read,
236         .unlocked_ioctl = drm_ioctl,
237         .release        = drm_release,
238 };
239
240 static struct drm_driver exynos_drm_driver = {
241         .driver_features        = DRIVER_HAVE_IRQ | DRIVER_BUS_PLATFORM |
242                                   DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME,
243         .load                   = exynos_drm_load,
244         .unload                 = exynos_drm_unload,
245         .open                   = exynos_drm_open,
246         .preclose               = exynos_drm_preclose,
247         .lastclose              = exynos_drm_lastclose,
248         .postclose              = exynos_drm_postclose,
249         .get_vblank_counter     = drm_vblank_count,
250         .enable_vblank          = exynos_drm_crtc_enable_vblank,
251         .disable_vblank         = exynos_drm_crtc_disable_vblank,
252         .gem_init_object        = exynos_drm_gem_init_object,
253         .gem_free_object        = exynos_drm_gem_free_object,
254         .gem_vm_ops             = &exynos_drm_gem_vm_ops,
255         .dumb_create            = exynos_drm_gem_dumb_create,
256         .dumb_map_offset        = exynos_drm_gem_dumb_map_offset,
257         .dumb_destroy           = exynos_drm_gem_dumb_destroy,
258         .prime_handle_to_fd     = drm_gem_prime_handle_to_fd,
259         .prime_fd_to_handle     = drm_gem_prime_fd_to_handle,
260         .gem_prime_export       = exynos_dmabuf_prime_export,
261         .gem_prime_import       = exynos_dmabuf_prime_import,
262         .ioctls                 = exynos_ioctls,
263         .fops                   = &exynos_drm_driver_fops,
264         .name   = DRIVER_NAME,
265         .desc   = DRIVER_DESC,
266         .date   = DRIVER_DATE,
267         .major  = DRIVER_MAJOR,
268         .minor  = DRIVER_MINOR,
269 };
270
271 #ifdef CONFIG_EXYNOS_IOMMU
272 static int iommu_init(struct platform_device *pdev)
273 {
274         /* DRM device expects a IOMMU mapping to be already
275          * created in FIMD. Else this function should
276          * throw an error.
277          */
278         if (exynos_drm_common_mapping==NULL) {
279                 printk(KERN_ERR "exynos drm common mapping is invalid\n");
280                 return -1;
281         }
282
283         /*
284          * The ordering in Makefile warrants that this is initialized after
285          * FIMD, so only just ensure that it works as expected and we are
286          * reusing the mapping originally created in exynos_drm_fimd.c.
287          */
288         WARN_ON(!exynos_drm_common_mapping);
289         if (!s5p_create_iommu_mapping(&pdev->dev, 0,
290                                 0, 0, exynos_drm_common_mapping)) {
291                 printk(KERN_ERR "failed to create IOMMU mapping\n");
292                 return -1;
293         }
294
295         return 0;
296 }
297
298 static void iommu_deinit(struct platform_device *pdev)
299 {
300         /* detach the device and mapping */
301         s5p_destroy_iommu_mapping(&pdev->dev);
302         DRM_DEBUG("released the IOMMU mapping\n");
303
304         return;
305 }
306 #endif
307
308 static int exynos_drm_platform_probe(struct platform_device *pdev)
309 {
310         DRM_DEBUG_DRIVER("%s\n", __FILE__);
311
312 #ifdef CONFIG_EXYNOS_IOMMU
313         if (iommu_init(pdev)) {
314                 DRM_ERROR("failed to initialize IOMMU\n");
315                 return -ENODEV;
316         }
317 #endif
318
319         exynos_drm_driver.num_ioctls = DRM_ARRAY_SIZE(exynos_ioctls);
320
321         return drm_platform_init(&exynos_drm_driver, pdev);
322 }
323
324 static int __devexit exynos_drm_platform_remove(struct platform_device *pdev)
325 {
326         DRM_DEBUG_DRIVER("%s\n", __FILE__);
327
328         drm_platform_exit(&exynos_drm_driver, pdev);
329
330 #ifdef CONFIG_EXYNOS_IOMMU
331         iommu_deinit(pdev);
332 #endif
333         return 0;
334 }
335
336 static struct platform_driver exynos_drm_platform_driver = {
337         .probe          = exynos_drm_platform_probe,
338         .remove         = __devexit_p(exynos_drm_platform_remove),
339         .driver         = {
340                 .owner  = THIS_MODULE,
341                 .name   = "exynos-drm",
342         },
343 };
344
345 static int __init exynos_drm_init(void)
346 {
347         int ret;
348
349         DRM_DEBUG_DRIVER("%s\n", __FILE__);
350
351 #ifdef CONFIG_DRM_EXYNOS_FIMD
352         ret = platform_driver_register(&fimd_driver);
353         if (ret < 0)
354                 goto out_fimd;
355 #endif
356
357 #ifdef CONFIG_DRM_EXYNOS_DP
358         ret = platform_driver_register(&dp_driver);
359         if (ret < 0)
360                 goto out_dp_driver;
361 #endif
362
363 #ifdef CONFIG_DRM_EXYNOS_HDMI
364         ret = platform_driver_register(&hdmi_driver);
365         if (ret < 0)
366                 goto out_hdmi;
367         ret = platform_driver_register(&mixer_driver);
368         if (ret < 0)
369                 goto out_mixer;
370         ret = platform_driver_register(&exynos_drm_common_hdmi_driver);
371         if (ret < 0)
372                 goto out_common_hdmi;
373 #endif
374
375 #ifdef CONFIG_DRM_EXYNOS_VIDI
376         ret = platform_driver_register(&vidi_driver);
377         if (ret < 0)
378                 goto out_vidi;
379 #endif
380
381         ret = platform_driver_register(&exynos_drm_platform_driver);
382         if (ret < 0)
383                 goto out;
384
385         return 0;
386
387 out:
388 #ifdef CONFIG_DRM_EXYNOS_VIDI
389 out_vidi:
390         platform_driver_unregister(&vidi_driver);
391 #endif
392
393 #ifdef CONFIG_DRM_EXYNOS_HDMI
394         platform_driver_unregister(&exynos_drm_common_hdmi_driver);
395 out_common_hdmi:
396         platform_driver_unregister(&mixer_driver);
397 out_mixer:
398         platform_driver_unregister(&hdmi_driver);
399 out_hdmi:
400 #endif
401
402         platform_driver_unregister(&dp_driver);
403 out_dp_driver:
404 #ifdef CONFIG_DRM_EXYNOS_FIMD
405         platform_driver_unregister(&fimd_driver);
406 out_fimd:
407 #endif
408         return ret;
409 }
410
411 static void __exit exynos_drm_exit(void)
412 {
413         DRM_DEBUG_DRIVER("%s\n", __FILE__);
414
415         platform_driver_unregister(&exynos_drm_platform_driver);
416
417 #ifdef CONFIG_DRM_EXYNOS_HDMI
418         platform_driver_unregister(&exynos_drm_common_hdmi_driver);
419         platform_driver_unregister(&mixer_driver);
420         platform_driver_unregister(&hdmi_driver);
421 #endif
422
423 #ifdef CONFIG_DRM_EXYNOS_VIDI
424         platform_driver_unregister(&vidi_driver);
425 #endif
426
427         platform_driver_unregister(&dp_driver);
428 #ifdef CONFIG_DRM_EXYNOS_FIMD
429         platform_driver_unregister(&fimd_driver);
430 #endif
431 }
432
433 module_init(exynos_drm_init);
434 module_exit(exynos_drm_exit);
435
436 MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
437 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
438 MODULE_AUTHOR("Seung-Woo Kim <sw0312.kim@samsung.com>");
439 MODULE_DESCRIPTION("Samsung SoC DRM Driver");
440 MODULE_LICENSE("GPL");