Merge tag 'lsk-v3.10-15.09-android'
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / rockchip / rockchip_drm_drv.c
1 /*
2  * Copyright (C) ROCKCHIP, Inc.
3  * Author:yzq<yzq@rock-chips.com>
4  * 
5  * based on exynos_drm_drv.c
6  *
7  * This software is licensed under the terms of the GNU General Public
8  * License version 2, as published by the Free Software Foundation, and
9  * may be copied, distributed, and modified under those terms.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  */
16
17 #include <drm/drmP.h>
18 #include <drm/drm_crtc_helper.h>
19
20 #include <drm/rockchip_drm.h>
21
22 #include "rockchip_drm_drv.h"
23 #include "rockchip_drm_crtc.h"
24 #include "rockchip_drm_encoder.h"
25 #include "rockchip_drm_fbdev.h"
26 #include "rockchip_drm_fb.h"
27 #include "rockchip_drm_gem.h"
28 #include "rockchip_drm_plane.h"
29 #include "rockchip_drm_dmabuf.h"
30 #include "rockchip_drm_iommu.h"
31
32 #define DRIVER_NAME     "rockchip"
33 #define DRIVER_DESC     "rockchip Soc DRM"
34 #define DRIVER_DATE     "20140318"
35 #define DRIVER_MAJOR    1
36 #define DRIVER_MINOR    0
37
38 #define VBLANK_OFF_DELAY        50000
39
40 /* platform device pointer for eynos drm device. */
41 static struct platform_device *rockchip_drm_pdev;
42
43 static int rockchip_drm_load(struct drm_device *dev, unsigned long flags)
44 {
45         struct rockchip_drm_private *private;
46         int ret;
47         int nr;
48
49         DRM_DEBUG_DRIVER("%s\n", __FILE__);
50
51         private = kzalloc(sizeof(struct rockchip_drm_private), GFP_KERNEL);
52         if (!private) {
53                 DRM_ERROR("failed to allocate private\n");
54                 return -ENOMEM;
55         }
56
57         INIT_LIST_HEAD(&private->pageflip_event_list);
58         dev->dev_private = (void *)private;
59
60         /*
61          * create mapping to manage iommu table and set a pointer to iommu
62          * mapping structure to iommu_mapping of private data.
63          * also this iommu_mapping can be used to check if iommu is supported
64          * or not.
65          */
66         ret = drm_create_iommu_mapping(dev);
67         if (ret < 0) {
68                 DRM_ERROR("failed to create iommu mapping.\n");
69                 goto err_crtc;
70         }
71
72         drm_mode_config_init(dev);
73
74         /* init kms poll for handling hpd */
75         drm_kms_helper_poll_init(dev);
76
77         rockchip_drm_mode_config_init(dev);
78
79         /*
80          * ROCKCHIP4 is enough to have two CRTCs and each crtc would be used
81          * without dependency of hardware.
82          */
83         for (nr = 0; nr < MAX_CRTC; nr++) {
84                 ret = rockchip_drm_crtc_create(dev, nr);
85                 if (ret)
86                         goto err_release_iommu_mapping;
87         }
88
89         for (nr = 0; nr < MAX_PLANE; nr++) {
90                 struct drm_plane *plane;
91                 unsigned int possible_crtcs = (1 << MAX_CRTC) - 1;
92
93                 plane = rockchip_plane_init(dev, possible_crtcs, false);
94                 if (!plane)
95                         goto err_release_iommu_mapping;
96         }
97
98         ret = drm_vblank_init(dev, MAX_CRTC);
99         if (ret)
100                 goto err_release_iommu_mapping;
101
102         /*
103          * probe sub drivers such as display controller and hdmi driver,
104          * that were registered at probe() of platform driver
105          * to the sub driver and create encoder and connector for them.
106          */
107         ret = rockchip_drm_device_register(dev);
108         if (ret)
109                 goto err_vblank;
110
111         /* setup possible_clones. */
112         rockchip_drm_encoder_setup(dev);
113
114         /*
115          * create and configure fb helper and also rockchip specific
116          * fbdev object.
117          */
118         ret = rockchip_drm_fbdev_init(dev);
119         if (ret) {
120                 DRM_ERROR("failed to initialize drm fbdev\n");
121                 goto err_drm_device;
122         }
123
124         drm_vblank_offdelay = VBLANK_OFF_DELAY;
125
126         return 0;
127
128 err_drm_device:
129         rockchip_drm_device_unregister(dev);
130 err_vblank:
131         drm_vblank_cleanup(dev);
132 err_release_iommu_mapping:
133         drm_release_iommu_mapping(dev);
134 err_crtc:
135         drm_mode_config_cleanup(dev);
136         kfree(private);
137
138         return ret;
139 }
140
141 static int rockchip_drm_unload(struct drm_device *dev)
142 {
143         DRM_DEBUG_DRIVER("%s\n", __FILE__);
144
145         rockchip_drm_fbdev_fini(dev);
146         rockchip_drm_device_unregister(dev);
147         drm_vblank_cleanup(dev);
148         drm_kms_helper_poll_fini(dev);
149         drm_mode_config_cleanup(dev);
150
151         drm_release_iommu_mapping(dev);
152         kfree(dev->dev_private);
153
154         dev->dev_private = NULL;
155
156         return 0;
157 }
158
159 static int rockchip_drm_open(struct drm_device *dev, struct drm_file *file)
160 {
161         struct drm_rockchip_file_private *file_priv;
162
163         DRM_DEBUG_DRIVER("%s\n", __FILE__);
164
165         file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
166         if (!file_priv)
167                 return -ENOMEM;
168
169         file->driver_priv = file_priv;
170
171         return rockchip_drm_subdrv_open(dev, file);
172 }
173
174 static void rockchip_drm_preclose(struct drm_device *dev,
175                                         struct drm_file *file)
176 {
177         struct rockchip_drm_private *private = dev->dev_private;
178         struct drm_pending_vblank_event *e, *t;
179         unsigned long flags;
180
181         DRM_DEBUG_DRIVER("%s\n", __FILE__);
182
183         /* release events of current file */
184         spin_lock_irqsave(&dev->event_lock, flags);
185         list_for_each_entry_safe(e, t, &private->pageflip_event_list,
186                         base.link) {
187                 if (e->base.file_priv == file) {
188                         list_del(&e->base.link);
189                         e->base.destroy(&e->base);
190                 }
191         }
192         spin_unlock_irqrestore(&dev->event_lock, flags);
193
194         rockchip_drm_subdrv_close(dev, file);
195 }
196
197 static void rockchip_drm_postclose(struct drm_device *dev, struct drm_file *file)
198 {
199         DRM_DEBUG_DRIVER("%s\n", __FILE__);
200
201         if (!file->driver_priv)
202                 return;
203
204         kfree(file->driver_priv);
205         file->driver_priv = NULL;
206 }
207
208 static void rockchip_drm_lastclose(struct drm_device *dev)
209 {
210         DRM_DEBUG_DRIVER("%s\n", __FILE__);
211
212         rockchip_drm_fbdev_restore_mode(dev);
213 }
214
215 static const struct vm_operations_struct rockchip_drm_gem_vm_ops = {
216         .fault = rockchip_drm_gem_fault,
217         .open = drm_gem_vm_open,
218         .close = drm_gem_vm_close,
219 };
220
221 static struct drm_ioctl_desc rockchip_ioctls[] = {
222         DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_CREATE, rockchip_drm_gem_create_ioctl,
223                         DRM_UNLOCKED | DRM_AUTH),
224         DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_MAP_OFFSET,
225                         rockchip_drm_gem_map_offset_ioctl, DRM_UNLOCKED |
226                         DRM_AUTH),
227         DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_MMAP,
228                         rockchip_drm_gem_mmap_ioctl, DRM_UNLOCKED | DRM_AUTH),
229         DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_GET,
230                         rockchip_drm_gem_get_ioctl, DRM_UNLOCKED),
231 };
232
233 static const struct file_operations rockchip_drm_driver_fops = {
234         .owner          = THIS_MODULE,
235         .open           = drm_open,
236         .mmap           = rockchip_drm_gem_mmap,
237         .poll           = drm_poll,
238         .read           = drm_read,
239         .unlocked_ioctl = drm_ioctl,
240 #ifdef CONFIG_COMPAT
241         .compat_ioctl = drm_compat_ioctl,
242 #endif
243         .release        = drm_release,
244 };
245
246 static struct drm_driver rockchip_drm_driver = {
247         .driver_features        = DRIVER_HAVE_IRQ | DRIVER_MODESET |
248                                         DRIVER_GEM | DRIVER_PRIME,
249         .load                   = rockchip_drm_load,
250         .unload                 = rockchip_drm_unload,
251         .open                   = rockchip_drm_open,
252         .preclose               = rockchip_drm_preclose,
253         .lastclose              = rockchip_drm_lastclose,
254         .postclose              = rockchip_drm_postclose,
255         .get_vblank_counter     = drm_vblank_count,
256         .enable_vblank          = rockchip_drm_crtc_enable_vblank,
257         .disable_vblank         = rockchip_drm_crtc_disable_vblank,
258 //      .get_vblank_timestamp   = rockchip_get_crtc_vblank_timestamp,
259         .gem_init_object        = rockchip_drm_gem_init_object,
260         .gem_free_object        = rockchip_drm_gem_free_object,
261         .gem_vm_ops             = &rockchip_drm_gem_vm_ops,
262         .dumb_create            = rockchip_drm_gem_dumb_create,
263         .dumb_map_offset        = rockchip_drm_gem_dumb_map_offset,
264         .dumb_destroy           = rockchip_drm_gem_dumb_destroy,
265         .prime_handle_to_fd     = drm_gem_prime_handle_to_fd,
266         .prime_fd_to_handle     = drm_gem_prime_fd_to_handle,
267         .gem_prime_export       = rockchip_dmabuf_prime_export,
268         .gem_prime_import       = rockchip_dmabuf_prime_import,
269         .ioctls                 = rockchip_ioctls,
270         .fops                   = &rockchip_drm_driver_fops,
271         .name   = DRIVER_NAME,
272         .desc   = DRIVER_DESC,
273         .date   = DRIVER_DATE,
274         .major  = DRIVER_MAJOR,
275         .minor  = DRIVER_MINOR,
276 };
277
278 static int rockchip_drm_platform_probe(struct platform_device *pdev)
279 {
280         DRM_DEBUG_DRIVER("%s\n", __FILE__);
281
282         pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
283         rockchip_drm_driver.num_ioctls = DRM_ARRAY_SIZE(rockchip_ioctls);
284
285         return drm_platform_init(&rockchip_drm_driver, pdev);
286 }
287
288 static int rockchip_drm_platform_remove(struct platform_device *pdev)
289 {
290         DRM_DEBUG_DRIVER("%s\n", __FILE__);
291
292         drm_platform_exit(&rockchip_drm_driver, pdev);
293
294         return 0;
295 }
296
297 static struct platform_driver rockchip_drm_platform_driver = {
298         .probe          = rockchip_drm_platform_probe,
299         .remove         = rockchip_drm_platform_remove,
300         .driver         = {
301                 .owner  = THIS_MODULE,
302                 .name   = "rockchip-drm",
303         },
304 };
305
306 static int __init rockchip_drm_init(void)
307 {
308         int ret;
309
310         DRM_DEBUG_DRIVER("%s\n", __FILE__);
311
312
313 #ifdef CONFIG_DRM_ROCKCHIP_PRIMARY
314         ret = platform_driver_register(&primary_platform_driver);
315         if (ret < 0)
316                 goto out_primary;
317         platform_device_register_simple("primary-display", -1,
318                         NULL, 0);
319 #endif
320 #ifdef CONFIG_DRM_ROCKCHIP_HDMI
321         ret = platform_driver_register(&extend_platform_driver);
322         if (ret < 0)
323                 goto out_extend;
324         platform_device_register_simple("extend-display", -1,
325                         NULL, 0);
326 #endif
327
328         ret = platform_driver_register(&rockchip_drm_platform_driver);
329         if (ret < 0)
330                 goto out_drm;
331
332
333         rockchip_drm_pdev = platform_device_register_simple("rockchip-drm", -1,
334                                 NULL, 0);
335         if (IS_ERR(rockchip_drm_pdev)) {
336                 ret = PTR_ERR(rockchip_drm_pdev);
337                 goto out;
338         }
339
340         return 0;
341
342 out:
343         platform_driver_unregister(&rockchip_drm_platform_driver);
344 out_drm:
345 #ifdef CONFIG_DRM_ROCKCHIP_PRIMARY
346         platform_driver_unregister(&primary_platform_driver);
347 out_primary:
348 #endif
349 #ifdef CONFIG_DRM_ROCKCHIP_HDMI
350         platform_driver_unregister(&extend_platform_driver);
351 out_extend:
352 #endif
353         return ret;
354 }
355
356 static void __exit rockchip_drm_exit(void)
357 {
358         DRM_DEBUG_DRIVER("%s\n", __FILE__);
359
360         platform_device_unregister(rockchip_drm_pdev);
361
362         platform_driver_unregister(&rockchip_drm_platform_driver);
363 }
364
365 module_init(rockchip_drm_init);
366 module_exit(rockchip_drm_exit);