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.
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.
15 #include <drm/drm_crtc_helper.h>
17 #include <drm/rockchip_drm.h>
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"
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
35 #define VBLANK_OFF_DELAY 50000
37 /* platform device pointer for eynos drm device. */
38 static struct platform_device *rockchip_drm_pdev;
40 static int rockchip_drm_load(struct drm_device *dev, unsigned long flags)
42 struct rockchip_drm_private *private;
46 DRM_DEBUG_DRIVER("%s\n", __FILE__);
48 private = kzalloc(sizeof(struct rockchip_drm_private), GFP_KERNEL);
50 DRM_ERROR("failed to allocate private\n");
54 INIT_LIST_HEAD(&private->pageflip_event_list);
55 dev->dev_private = (void *)private;
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
63 ret = drm_create_iommu_mapping(dev);
65 DRM_ERROR("failed to create iommu mapping.\n");
69 drm_mode_config_init(dev);
71 /* init kms poll for handling hpd */
72 drm_kms_helper_poll_init(dev);
74 rockchip_drm_mode_config_init(dev);
77 * ROCKCHIP4 is enough to have two CRTCs and each crtc would be used
78 * without dependency of hardware.
80 for (nr = 0; nr < MAX_CRTC; nr++) {
81 ret = rockchip_drm_crtc_create(dev, nr);
83 goto err_release_iommu_mapping;
86 for (nr = 0; nr < MAX_PLANE; nr++) {
87 struct drm_plane *plane;
88 unsigned int possible_crtcs = (1 << MAX_CRTC) - 1;
90 plane = rockchip_plane_init(dev, possible_crtcs, false);
92 goto err_release_iommu_mapping;
95 ret = drm_vblank_init(dev, MAX_CRTC);
97 goto err_release_iommu_mapping;
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.
104 ret = rockchip_drm_device_register(dev);
108 /* setup possible_clones. */
109 rockchip_drm_encoder_setup(dev);
112 * create and configure fb helper and also rockchip specific
115 ret = rockchip_drm_fbdev_init(dev);
117 DRM_ERROR("failed to initialize drm fbdev\n");
121 drm_vblank_offdelay = VBLANK_OFF_DELAY;
126 rockchip_drm_device_unregister(dev);
128 drm_vblank_cleanup(dev);
129 err_release_iommu_mapping:
130 drm_release_iommu_mapping(dev);
132 drm_mode_config_cleanup(dev);
138 static int rockchip_drm_unload(struct drm_device *dev)
140 DRM_DEBUG_DRIVER("%s\n", __FILE__);
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);
148 drm_release_iommu_mapping(dev);
149 kfree(dev->dev_private);
151 dev->dev_private = NULL;
156 static int rockchip_drm_open(struct drm_device *dev, struct drm_file *file)
158 struct drm_rockchip_file_private *file_priv;
160 DRM_DEBUG_DRIVER("%s\n", __FILE__);
162 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
166 file->driver_priv = file_priv;
168 return rockchip_drm_subdrv_open(dev, file);
171 static void rockchip_drm_preclose(struct drm_device *dev,
172 struct drm_file *file)
174 struct rockchip_drm_private *private = dev->dev_private;
175 struct drm_pending_vblank_event *e, *t;
178 DRM_DEBUG_DRIVER("%s\n", __FILE__);
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,
184 if (e->base.file_priv == file) {
185 list_del(&e->base.link);
186 e->base.destroy(&e->base);
189 spin_unlock_irqrestore(&dev->event_lock, flags);
191 rockchip_drm_subdrv_close(dev, file);
194 static void rockchip_drm_postclose(struct drm_device *dev, struct drm_file *file)
196 DRM_DEBUG_DRIVER("%s\n", __FILE__);
198 if (!file->driver_priv)
201 kfree(file->driver_priv);
202 file->driver_priv = NULL;
205 static void rockchip_drm_lastclose(struct drm_device *dev)
207 DRM_DEBUG_DRIVER("%s\n", __FILE__);
209 rockchip_drm_fbdev_restore_mode(dev);
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,
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 |
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),
230 static const struct file_operations rockchip_drm_driver_fops = {
231 .owner = THIS_MODULE,
233 .mmap = rockchip_drm_gem_mmap,
236 .unlocked_ioctl = drm_ioctl,
238 .compat_ioctl = drm_compat_ioctl,
240 .release = drm_release,
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,
270 .major = DRIVER_MAJOR,
271 .minor = DRIVER_MINOR,
274 static int rockchip_drm_platform_probe(struct platform_device *pdev)
276 DRM_DEBUG_DRIVER("%s\n", __FILE__);
278 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
279 rockchip_drm_driver.num_ioctls = DRM_ARRAY_SIZE(rockchip_ioctls);
281 return drm_platform_init(&rockchip_drm_driver, pdev);
284 static int rockchip_drm_platform_remove(struct platform_device *pdev)
286 DRM_DEBUG_DRIVER("%s\n", __FILE__);
288 drm_platform_exit(&rockchip_drm_driver, pdev);
293 static struct platform_driver rockchip_drm_platform_driver = {
294 .probe = rockchip_drm_platform_probe,
295 .remove = rockchip_drm_platform_remove,
297 .owner = THIS_MODULE,
298 .name = "rockchip-drm",
302 static int __init rockchip_drm_init(void)
306 DRM_DEBUG_DRIVER("%s\n", __FILE__);
309 #ifdef CONFIG_DRM_RK_PRIMARY
310 ret = platform_driver_register(&primary_platform_driver);
313 platform_device_register_simple("primary-display", -1,
317 ret = platform_driver_register(&rockchip_drm_platform_driver);
322 rockchip_drm_pdev = platform_device_register_simple("rockchip-drm", -1,
324 if (IS_ERR(rockchip_drm_pdev)) {
325 ret = PTR_ERR(rockchip_drm_pdev);
332 platform_driver_unregister(&rockchip_drm_platform_driver);
334 #ifdef CONFIG_DRM_RK_PRIMARY
335 platform_driver_unregister(&primary_platform_driver);
341 static void __exit rockchip_drm_exit(void)
343 DRM_DEBUG_DRIVER("%s\n", __FILE__);
345 platform_device_unregister(rockchip_drm_pdev);
347 platform_driver_unregister(&rockchip_drm_platform_driver);
350 module_init(rockchip_drm_init);
351 module_exit(rockchip_drm_exit);