Merge remote-tracking branch 'asoc/topic/rcar' into asoc-next
[sfrench/cifs-2.6.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  * This program is free software; you can redistribute  it and/or modify it
9  * under  the terms of  the GNU General  Public License as published by the
10  * Free Software Foundation;  either version 2 of the  License, or (at your
11  * option) any later version.
12  */
13
14 #include <linux/pm_runtime.h>
15 #include <drm/drmP.h>
16 #include <drm/drm_atomic.h>
17 #include <drm/drm_atomic_helper.h>
18 #include <drm/drm_crtc_helper.h>
19
20 #include <linux/component.h>
21
22 #include <drm/exynos_drm.h>
23
24 #include "exynos_drm_drv.h"
25 #include "exynos_drm_fbdev.h"
26 #include "exynos_drm_fb.h"
27 #include "exynos_drm_gem.h"
28 #include "exynos_drm_plane.h"
29 #include "exynos_drm_vidi.h"
30 #include "exynos_drm_g2d.h"
31 #include "exynos_drm_ipp.h"
32 #include "exynos_drm_iommu.h"
33
34 #define DRIVER_NAME     "exynos"
35 #define DRIVER_DESC     "Samsung SoC DRM"
36 #define DRIVER_DATE     "20110530"
37 #define DRIVER_MAJOR    1
38 #define DRIVER_MINOR    0
39
40 int exynos_atomic_check(struct drm_device *dev,
41                         struct drm_atomic_state *state)
42 {
43         int ret;
44
45         ret = drm_atomic_helper_check_modeset(dev, state);
46         if (ret)
47                 return ret;
48
49         ret = drm_atomic_normalize_zpos(dev, state);
50         if (ret)
51                 return ret;
52
53         ret = drm_atomic_helper_check_planes(dev, state);
54         if (ret)
55                 return ret;
56
57         return ret;
58 }
59
60 static int exynos_drm_open(struct drm_device *dev, struct drm_file *file)
61 {
62         struct drm_exynos_file_private *file_priv;
63         int ret;
64
65         file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
66         if (!file_priv)
67                 return -ENOMEM;
68
69         file->driver_priv = file_priv;
70
71         ret = exynos_drm_subdrv_open(dev, file);
72         if (ret)
73                 goto err_file_priv_free;
74
75         return ret;
76
77 err_file_priv_free:
78         kfree(file_priv);
79         file->driver_priv = NULL;
80         return ret;
81 }
82
83 static void exynos_drm_postclose(struct drm_device *dev, struct drm_file *file)
84 {
85         exynos_drm_subdrv_close(dev, file);
86         kfree(file->driver_priv);
87         file->driver_priv = NULL;
88 }
89
90 static void exynos_drm_lastclose(struct drm_device *dev)
91 {
92         exynos_drm_fbdev_restore_mode(dev);
93 }
94
95 static const struct vm_operations_struct exynos_drm_gem_vm_ops = {
96         .fault = exynos_drm_gem_fault,
97         .open = drm_gem_vm_open,
98         .close = drm_gem_vm_close,
99 };
100
101 static const struct drm_ioctl_desc exynos_ioctls[] = {
102         DRM_IOCTL_DEF_DRV(EXYNOS_GEM_CREATE, exynos_drm_gem_create_ioctl,
103                         DRM_AUTH | DRM_RENDER_ALLOW),
104         DRM_IOCTL_DEF_DRV(EXYNOS_GEM_MAP, exynos_drm_gem_map_ioctl,
105                         DRM_AUTH | DRM_RENDER_ALLOW),
106         DRM_IOCTL_DEF_DRV(EXYNOS_GEM_GET, exynos_drm_gem_get_ioctl,
107                         DRM_RENDER_ALLOW),
108         DRM_IOCTL_DEF_DRV(EXYNOS_VIDI_CONNECTION, vidi_connection_ioctl,
109                         DRM_AUTH),
110         DRM_IOCTL_DEF_DRV(EXYNOS_G2D_GET_VER, exynos_g2d_get_ver_ioctl,
111                         DRM_AUTH | DRM_RENDER_ALLOW),
112         DRM_IOCTL_DEF_DRV(EXYNOS_G2D_SET_CMDLIST, exynos_g2d_set_cmdlist_ioctl,
113                         DRM_AUTH | DRM_RENDER_ALLOW),
114         DRM_IOCTL_DEF_DRV(EXYNOS_G2D_EXEC, exynos_g2d_exec_ioctl,
115                         DRM_AUTH | DRM_RENDER_ALLOW),
116         DRM_IOCTL_DEF_DRV(EXYNOS_IPP_GET_PROPERTY, exynos_drm_ipp_get_property,
117                         DRM_AUTH | DRM_RENDER_ALLOW),
118         DRM_IOCTL_DEF_DRV(EXYNOS_IPP_SET_PROPERTY, exynos_drm_ipp_set_property,
119                         DRM_AUTH | DRM_RENDER_ALLOW),
120         DRM_IOCTL_DEF_DRV(EXYNOS_IPP_QUEUE_BUF, exynos_drm_ipp_queue_buf,
121                         DRM_AUTH | DRM_RENDER_ALLOW),
122         DRM_IOCTL_DEF_DRV(EXYNOS_IPP_CMD_CTRL, exynos_drm_ipp_cmd_ctrl,
123                         DRM_AUTH | DRM_RENDER_ALLOW),
124 };
125
126 static const struct file_operations exynos_drm_driver_fops = {
127         .owner          = THIS_MODULE,
128         .open           = drm_open,
129         .mmap           = exynos_drm_gem_mmap,
130         .poll           = drm_poll,
131         .read           = drm_read,
132         .unlocked_ioctl = drm_ioctl,
133         .compat_ioctl = drm_compat_ioctl,
134         .release        = drm_release,
135 };
136
137 static struct drm_driver exynos_drm_driver = {
138         .driver_features        = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME
139                                   | DRIVER_ATOMIC | DRIVER_RENDER,
140         .open                   = exynos_drm_open,
141         .lastclose              = exynos_drm_lastclose,
142         .postclose              = exynos_drm_postclose,
143         .gem_free_object_unlocked = exynos_drm_gem_free_object,
144         .gem_vm_ops             = &exynos_drm_gem_vm_ops,
145         .dumb_create            = exynos_drm_gem_dumb_create,
146         .prime_handle_to_fd     = drm_gem_prime_handle_to_fd,
147         .prime_fd_to_handle     = drm_gem_prime_fd_to_handle,
148         .gem_prime_export       = drm_gem_prime_export,
149         .gem_prime_import       = exynos_drm_gem_prime_import,
150         .gem_prime_get_sg_table = exynos_drm_gem_prime_get_sg_table,
151         .gem_prime_import_sg_table      = exynos_drm_gem_prime_import_sg_table,
152         .gem_prime_vmap         = exynos_drm_gem_prime_vmap,
153         .gem_prime_vunmap       = exynos_drm_gem_prime_vunmap,
154         .gem_prime_mmap         = exynos_drm_gem_prime_mmap,
155         .ioctls                 = exynos_ioctls,
156         .num_ioctls             = ARRAY_SIZE(exynos_ioctls),
157         .fops                   = &exynos_drm_driver_fops,
158         .name   = DRIVER_NAME,
159         .desc   = DRIVER_DESC,
160         .date   = DRIVER_DATE,
161         .major  = DRIVER_MAJOR,
162         .minor  = DRIVER_MINOR,
163 };
164
165 #ifdef CONFIG_PM_SLEEP
166 static int exynos_drm_suspend(struct device *dev)
167 {
168         struct drm_device *drm_dev = dev_get_drvdata(dev);
169         struct exynos_drm_private *private;
170
171         if (pm_runtime_suspended(dev) || !drm_dev)
172                 return 0;
173
174         private = drm_dev->dev_private;
175
176         drm_kms_helper_poll_disable(drm_dev);
177         exynos_drm_fbdev_suspend(drm_dev);
178         private->suspend_state = drm_atomic_helper_suspend(drm_dev);
179         if (IS_ERR(private->suspend_state)) {
180                 exynos_drm_fbdev_resume(drm_dev);
181                 drm_kms_helper_poll_enable(drm_dev);
182                 return PTR_ERR(private->suspend_state);
183         }
184
185         return 0;
186 }
187
188 static int exynos_drm_resume(struct device *dev)
189 {
190         struct drm_device *drm_dev = dev_get_drvdata(dev);
191         struct exynos_drm_private *private;
192
193         if (pm_runtime_suspended(dev) || !drm_dev)
194                 return 0;
195
196         private = drm_dev->dev_private;
197         drm_atomic_helper_resume(drm_dev, private->suspend_state);
198         exynos_drm_fbdev_resume(drm_dev);
199         drm_kms_helper_poll_enable(drm_dev);
200
201         return 0;
202 }
203 #endif
204
205 static const struct dev_pm_ops exynos_drm_pm_ops = {
206         SET_SYSTEM_SLEEP_PM_OPS(exynos_drm_suspend, exynos_drm_resume)
207 };
208
209 /* forward declaration */
210 static struct platform_driver exynos_drm_platform_driver;
211
212 struct exynos_drm_driver_info {
213         struct platform_driver *driver;
214         unsigned int flags;
215 };
216
217 #define DRM_COMPONENT_DRIVER    BIT(0)  /* supports component framework */
218 #define DRM_VIRTUAL_DEVICE      BIT(1)  /* create virtual platform device */
219 #define DRM_DMA_DEVICE          BIT(2)  /* can be used for dma allocations */
220
221 #define DRV_PTR(drv, cond) (IS_ENABLED(cond) ? &drv : NULL)
222
223 /*
224  * Connector drivers should not be placed before associated crtc drivers,
225  * because connector requires pipe number of its crtc during initialization.
226  */
227 static struct exynos_drm_driver_info exynos_drm_drivers[] = {
228         {
229                 DRV_PTR(fimd_driver, CONFIG_DRM_EXYNOS_FIMD),
230                 DRM_COMPONENT_DRIVER | DRM_DMA_DEVICE
231         }, {
232                 DRV_PTR(exynos5433_decon_driver, CONFIG_DRM_EXYNOS5433_DECON),
233                 DRM_COMPONENT_DRIVER | DRM_DMA_DEVICE
234         }, {
235                 DRV_PTR(decon_driver, CONFIG_DRM_EXYNOS7_DECON),
236                 DRM_COMPONENT_DRIVER | DRM_DMA_DEVICE
237         }, {
238                 DRV_PTR(mixer_driver, CONFIG_DRM_EXYNOS_MIXER),
239                 DRM_COMPONENT_DRIVER | DRM_DMA_DEVICE
240         }, {
241                 DRV_PTR(mic_driver, CONFIG_DRM_EXYNOS_MIC),
242                 DRM_COMPONENT_DRIVER
243         }, {
244                 DRV_PTR(dp_driver, CONFIG_DRM_EXYNOS_DP),
245                 DRM_COMPONENT_DRIVER
246         }, {
247                 DRV_PTR(dsi_driver, CONFIG_DRM_EXYNOS_DSI),
248                 DRM_COMPONENT_DRIVER
249         }, {
250                 DRV_PTR(hdmi_driver, CONFIG_DRM_EXYNOS_HDMI),
251                 DRM_COMPONENT_DRIVER
252         }, {
253                 DRV_PTR(vidi_driver, CONFIG_DRM_EXYNOS_VIDI),
254                 DRM_COMPONENT_DRIVER | DRM_VIRTUAL_DEVICE
255         }, {
256                 DRV_PTR(g2d_driver, CONFIG_DRM_EXYNOS_G2D),
257         }, {
258                 DRV_PTR(fimc_driver, CONFIG_DRM_EXYNOS_FIMC),
259         }, {
260                 DRV_PTR(rotator_driver, CONFIG_DRM_EXYNOS_ROTATOR),
261         }, {
262                 DRV_PTR(gsc_driver, CONFIG_DRM_EXYNOS_GSC),
263         }, {
264                 DRV_PTR(ipp_driver, CONFIG_DRM_EXYNOS_IPP),
265                 DRM_VIRTUAL_DEVICE
266         }, {
267                 &exynos_drm_platform_driver,
268                 DRM_VIRTUAL_DEVICE
269         }
270 };
271
272 static int compare_dev(struct device *dev, void *data)
273 {
274         return dev == (struct device *)data;
275 }
276
277 static struct component_match *exynos_drm_match_add(struct device *dev)
278 {
279         struct component_match *match = NULL;
280         int i;
281
282         for (i = 0; i < ARRAY_SIZE(exynos_drm_drivers); ++i) {
283                 struct exynos_drm_driver_info *info = &exynos_drm_drivers[i];
284                 struct device *p = NULL, *d;
285
286                 if (!info->driver || !(info->flags & DRM_COMPONENT_DRIVER))
287                         continue;
288
289                 while ((d = bus_find_device(&platform_bus_type, p,
290                                             &info->driver->driver,
291                                             (void *)platform_bus_type.match))) {
292                         put_device(p);
293                         component_match_add(dev, &match, compare_dev, d);
294                         p = d;
295                 }
296                 put_device(p);
297         }
298
299         return match ?: ERR_PTR(-ENODEV);
300 }
301
302 static struct device *exynos_drm_get_dma_device(void)
303 {
304         int i;
305
306         for (i = 0; i < ARRAY_SIZE(exynos_drm_drivers); ++i) {
307                 struct exynos_drm_driver_info *info = &exynos_drm_drivers[i];
308                 struct device *dev;
309
310                 if (!info->driver || !(info->flags & DRM_DMA_DEVICE))
311                         continue;
312
313                 while ((dev = bus_find_device(&platform_bus_type, NULL,
314                                             &info->driver->driver,
315                                             (void *)platform_bus_type.match))) {
316                         put_device(dev);
317                         return dev;
318                 }
319         }
320         return NULL;
321 }
322
323 static int exynos_drm_bind(struct device *dev)
324 {
325         struct exynos_drm_private *private;
326         struct drm_encoder *encoder;
327         struct drm_device *drm;
328         unsigned int clone_mask;
329         int cnt, ret;
330
331         drm = drm_dev_alloc(&exynos_drm_driver, dev);
332         if (IS_ERR(drm))
333                 return PTR_ERR(drm);
334
335         private = kzalloc(sizeof(struct exynos_drm_private), GFP_KERNEL);
336         if (!private) {
337                 ret = -ENOMEM;
338                 goto err_free_drm;
339         }
340
341         init_waitqueue_head(&private->wait);
342         spin_lock_init(&private->lock);
343
344         dev_set_drvdata(dev, drm);
345         drm->dev_private = (void *)private;
346
347         /* the first real CRTC device is used for all dma mapping operations */
348         private->dma_dev = exynos_drm_get_dma_device();
349         if (!private->dma_dev) {
350                 DRM_ERROR("no device found for DMA mapping operations.\n");
351                 ret = -ENODEV;
352                 goto err_free_private;
353         }
354         DRM_INFO("Exynos DRM: using %s device for DMA mapping operations\n",
355                  dev_name(private->dma_dev));
356
357         /* create common IOMMU mapping for all devices attached to Exynos DRM */
358         ret = drm_create_iommu_mapping(drm);
359         if (ret < 0) {
360                 DRM_ERROR("failed to create iommu mapping.\n");
361                 goto err_free_private;
362         }
363
364         drm_mode_config_init(drm);
365
366         exynos_drm_mode_config_init(drm);
367
368         /* setup possible_clones. */
369         cnt = 0;
370         clone_mask = 0;
371         list_for_each_entry(encoder, &drm->mode_config.encoder_list, head)
372                 clone_mask |= (1 << (cnt++));
373
374         list_for_each_entry(encoder, &drm->mode_config.encoder_list, head)
375                 encoder->possible_clones = clone_mask;
376
377         /* Try to bind all sub drivers. */
378         ret = component_bind_all(drm->dev, drm);
379         if (ret)
380                 goto err_mode_config_cleanup;
381
382         ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
383         if (ret)
384                 goto err_unbind_all;
385
386         /* Probe non kms sub drivers and virtual display driver. */
387         ret = exynos_drm_device_subdrv_probe(drm);
388         if (ret)
389                 goto err_unbind_all;
390
391         drm_mode_config_reset(drm);
392
393         /*
394          * enable drm irq mode.
395          * - with irq_enabled = true, we can use the vblank feature.
396          *
397          * P.S. note that we wouldn't use drm irq handler but
398          *      just specific driver own one instead because
399          *      drm framework supports only one irq handler.
400          */
401         drm->irq_enabled = true;
402
403         /* init kms poll for handling hpd */
404         drm_kms_helper_poll_init(drm);
405
406         ret = exynos_drm_fbdev_init(drm);
407         if (ret)
408                 goto err_cleanup_poll;
409
410         /* register the DRM device */
411         ret = drm_dev_register(drm, 0);
412         if (ret < 0)
413                 goto err_cleanup_fbdev;
414
415         return 0;
416
417 err_cleanup_fbdev:
418         exynos_drm_fbdev_fini(drm);
419 err_cleanup_poll:
420         drm_kms_helper_poll_fini(drm);
421         exynos_drm_device_subdrv_remove(drm);
422 err_unbind_all:
423         component_unbind_all(drm->dev, drm);
424 err_mode_config_cleanup:
425         drm_mode_config_cleanup(drm);
426         drm_release_iommu_mapping(drm);
427 err_free_private:
428         kfree(private);
429 err_free_drm:
430         drm_dev_unref(drm);
431
432         return ret;
433 }
434
435 static void exynos_drm_unbind(struct device *dev)
436 {
437         struct drm_device *drm = dev_get_drvdata(dev);
438
439         drm_dev_unregister(drm);
440
441         exynos_drm_device_subdrv_remove(drm);
442
443         exynos_drm_fbdev_fini(drm);
444         drm_kms_helper_poll_fini(drm);
445
446         component_unbind_all(drm->dev, drm);
447         drm_mode_config_cleanup(drm);
448         drm_release_iommu_mapping(drm);
449
450         kfree(drm->dev_private);
451         drm->dev_private = NULL;
452         dev_set_drvdata(dev, NULL);
453
454         drm_dev_unref(drm);
455 }
456
457 static const struct component_master_ops exynos_drm_ops = {
458         .bind           = exynos_drm_bind,
459         .unbind         = exynos_drm_unbind,
460 };
461
462 static int exynos_drm_platform_probe(struct platform_device *pdev)
463 {
464         struct component_match *match;
465
466         pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
467
468         match = exynos_drm_match_add(&pdev->dev);
469         if (IS_ERR(match))
470                 return PTR_ERR(match);
471
472         return component_master_add_with_match(&pdev->dev, &exynos_drm_ops,
473                                                match);
474 }
475
476 static int exynos_drm_platform_remove(struct platform_device *pdev)
477 {
478         component_master_del(&pdev->dev, &exynos_drm_ops);
479         return 0;
480 }
481
482 static struct platform_driver exynos_drm_platform_driver = {
483         .probe  = exynos_drm_platform_probe,
484         .remove = exynos_drm_platform_remove,
485         .driver = {
486                 .name   = "exynos-drm",
487                 .pm     = &exynos_drm_pm_ops,
488         },
489 };
490
491 static void exynos_drm_unregister_devices(void)
492 {
493         int i;
494
495         for (i = ARRAY_SIZE(exynos_drm_drivers) - 1; i >= 0; --i) {
496                 struct exynos_drm_driver_info *info = &exynos_drm_drivers[i];
497                 struct device *dev;
498
499                 if (!info->driver || !(info->flags & DRM_VIRTUAL_DEVICE))
500                         continue;
501
502                 while ((dev = bus_find_device(&platform_bus_type, NULL,
503                                             &info->driver->driver,
504                                             (void *)platform_bus_type.match))) {
505                         put_device(dev);
506                         platform_device_unregister(to_platform_device(dev));
507                 }
508         }
509 }
510
511 static int exynos_drm_register_devices(void)
512 {
513         struct platform_device *pdev;
514         int i;
515
516         for (i = 0; i < ARRAY_SIZE(exynos_drm_drivers); ++i) {
517                 struct exynos_drm_driver_info *info = &exynos_drm_drivers[i];
518
519                 if (!info->driver || !(info->flags & DRM_VIRTUAL_DEVICE))
520                         continue;
521
522                 pdev = platform_device_register_simple(
523                                         info->driver->driver.name, -1, NULL, 0);
524                 if (IS_ERR(pdev))
525                         goto fail;
526         }
527
528         return 0;
529 fail:
530         exynos_drm_unregister_devices();
531         return PTR_ERR(pdev);
532 }
533
534 static void exynos_drm_unregister_drivers(void)
535 {
536         int i;
537
538         for (i = ARRAY_SIZE(exynos_drm_drivers) - 1; i >= 0; --i) {
539                 struct exynos_drm_driver_info *info = &exynos_drm_drivers[i];
540
541                 if (!info->driver)
542                         continue;
543
544                 platform_driver_unregister(info->driver);
545         }
546 }
547
548 static int exynos_drm_register_drivers(void)
549 {
550         int i, ret;
551
552         for (i = 0; i < ARRAY_SIZE(exynos_drm_drivers); ++i) {
553                 struct exynos_drm_driver_info *info = &exynos_drm_drivers[i];
554
555                 if (!info->driver)
556                         continue;
557
558                 ret = platform_driver_register(info->driver);
559                 if (ret)
560                         goto fail;
561         }
562         return 0;
563 fail:
564         exynos_drm_unregister_drivers();
565         return ret;
566 }
567
568 static int exynos_drm_init(void)
569 {
570         int ret;
571
572         ret = exynos_drm_register_devices();
573         if (ret)
574                 return ret;
575
576         ret = exynos_drm_register_drivers();
577         if (ret)
578                 goto err_unregister_pdevs;
579
580         return 0;
581
582 err_unregister_pdevs:
583         exynos_drm_unregister_devices();
584
585         return ret;
586 }
587
588 static void exynos_drm_exit(void)
589 {
590         exynos_drm_unregister_drivers();
591         exynos_drm_unregister_devices();
592 }
593
594 module_init(exynos_drm_init);
595 module_exit(exynos_drm_exit);
596
597 MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
598 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
599 MODULE_AUTHOR("Seung-Woo Kim <sw0312.kim@samsung.com>");
600 MODULE_DESCRIPTION("Samsung SoC DRM Driver");
601 MODULE_LICENSE("GPL");