2 * Copyright (C) ROCKCHIP, Inc.
3 * Author:yzq<yzq@rock-chips.com>
5 * based on exynos_drm_drv.c
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.
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.
18 #include <drm/drm_crtc_helper.h>
20 #include <drm/rockchip_drm.h>
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"
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
38 #define VBLANK_OFF_DELAY 50000
40 /* platform device pointer for eynos drm device. */
41 static struct platform_device *rockchip_drm_pdev;
43 static int rockchip_drm_load(struct drm_device *dev, unsigned long flags)
45 struct rockchip_drm_private *private;
49 DRM_DEBUG_DRIVER("%s\n", __FILE__);
51 private = kzalloc(sizeof(struct rockchip_drm_private), GFP_KERNEL);
53 DRM_ERROR("failed to allocate private\n");
57 INIT_LIST_HEAD(&private->pageflip_event_list);
58 dev->dev_private = (void *)private;
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
66 ret = drm_create_iommu_mapping(dev);
68 DRM_ERROR("failed to create iommu mapping.\n");
72 drm_mode_config_init(dev);
74 /* init kms poll for handling hpd */
75 drm_kms_helper_poll_init(dev);
77 rockchip_drm_mode_config_init(dev);
80 * ROCKCHIP4 is enough to have two CRTCs and each crtc would be used
81 * without dependency of hardware.
83 for (nr = 0; nr < MAX_CRTC; nr++) {
84 ret = rockchip_drm_crtc_create(dev, nr);
86 goto err_release_iommu_mapping;
89 for (nr = 0; nr < MAX_PLANE; nr++) {
90 struct drm_plane *plane;
91 unsigned int possible_crtcs = (1 << MAX_CRTC) - 1;
93 plane = rockchip_plane_init(dev, possible_crtcs, false);
95 goto err_release_iommu_mapping;
98 ret = drm_vblank_init(dev, MAX_CRTC);
100 goto err_release_iommu_mapping;
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.
107 ret = rockchip_drm_device_register(dev);
111 /* setup possible_clones. */
112 rockchip_drm_encoder_setup(dev);
115 * create and configure fb helper and also rockchip specific
118 ret = rockchip_drm_fbdev_init(dev);
120 DRM_ERROR("failed to initialize drm fbdev\n");
124 drm_vblank_offdelay = VBLANK_OFF_DELAY;
129 rockchip_drm_device_unregister(dev);
131 drm_vblank_cleanup(dev);
132 err_release_iommu_mapping:
133 drm_release_iommu_mapping(dev);
135 drm_mode_config_cleanup(dev);
141 static int rockchip_drm_unload(struct drm_device *dev)
143 DRM_DEBUG_DRIVER("%s\n", __FILE__);
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);
151 drm_release_iommu_mapping(dev);
152 kfree(dev->dev_private);
154 dev->dev_private = NULL;
159 static int rockchip_drm_open(struct drm_device *dev, struct drm_file *file)
161 struct drm_rockchip_file_private *file_priv;
163 DRM_DEBUG_DRIVER("%s\n", __FILE__);
165 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
169 file->driver_priv = file_priv;
171 return rockchip_drm_subdrv_open(dev, file);
174 static void rockchip_drm_preclose(struct drm_device *dev,
175 struct drm_file *file)
177 struct rockchip_drm_private *private = dev->dev_private;
178 struct drm_pending_vblank_event *e, *t;
181 DRM_DEBUG_DRIVER("%s\n", __FILE__);
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,
187 if (e->base.file_priv == file) {
188 list_del(&e->base.link);
189 e->base.destroy(&e->base);
192 spin_unlock_irqrestore(&dev->event_lock, flags);
194 rockchip_drm_subdrv_close(dev, file);
197 static void rockchip_drm_postclose(struct drm_device *dev, struct drm_file *file)
199 DRM_DEBUG_DRIVER("%s\n", __FILE__);
201 if (!file->driver_priv)
204 kfree(file->driver_priv);
205 file->driver_priv = NULL;
208 static void rockchip_drm_lastclose(struct drm_device *dev)
210 DRM_DEBUG_DRIVER("%s\n", __FILE__);
212 rockchip_drm_fbdev_restore_mode(dev);
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,
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 |
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),
233 static const struct file_operations rockchip_drm_driver_fops = {
234 .owner = THIS_MODULE,
236 .mmap = rockchip_drm_gem_mmap,
239 .unlocked_ioctl = drm_ioctl,
241 .compat_ioctl = drm_compat_ioctl,
243 .release = drm_release,
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,
274 .major = DRIVER_MAJOR,
275 .minor = DRIVER_MINOR,
278 static int rockchip_drm_platform_probe(struct platform_device *pdev)
280 DRM_DEBUG_DRIVER("%s\n", __FILE__);
282 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
283 rockchip_drm_driver.num_ioctls = DRM_ARRAY_SIZE(rockchip_ioctls);
285 return drm_platform_init(&rockchip_drm_driver, pdev);
288 static int rockchip_drm_platform_remove(struct platform_device *pdev)
290 DRM_DEBUG_DRIVER("%s\n", __FILE__);
292 drm_platform_exit(&rockchip_drm_driver, pdev);
297 static struct platform_driver rockchip_drm_platform_driver = {
298 .probe = rockchip_drm_platform_probe,
299 .remove = rockchip_drm_platform_remove,
301 .owner = THIS_MODULE,
302 .name = "rockchip-drm",
306 static int __init rockchip_drm_init(void)
310 DRM_DEBUG_DRIVER("%s\n", __FILE__);
313 #ifdef CONFIG_DRM_ROCKCHIP_PRIMARY
314 ret = platform_driver_register(&primary_platform_driver);
317 platform_device_register_simple("primary-display", -1,
320 #ifdef CONFIG_DRM_ROCKCHIP_HDMI
321 ret = platform_driver_register(&extend_platform_driver);
324 platform_device_register_simple("extend-display", -1,
328 ret = platform_driver_register(&rockchip_drm_platform_driver);
333 rockchip_drm_pdev = platform_device_register_simple("rockchip-drm", -1,
335 if (IS_ERR(rockchip_drm_pdev)) {
336 ret = PTR_ERR(rockchip_drm_pdev);
343 platform_driver_unregister(&rockchip_drm_platform_driver);
345 #ifdef CONFIG_DRM_ROCKCHIP_PRIMARY
346 platform_driver_unregister(&primary_platform_driver);
349 #ifdef CONFIG_DRM_ROCKCHIP_HDMI
350 platform_driver_unregister(&extend_platform_driver);
356 static void __exit rockchip_drm_exit(void)
358 DRM_DEBUG_DRIVER("%s\n", __FILE__);
360 platform_device_unregister(rockchip_drm_pdev);
362 platform_driver_unregister(&rockchip_drm_platform_driver);
365 module_init(rockchip_drm_init);
366 module_exit(rockchip_drm_exit);