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