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