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