1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
5 * Inki Dae <inki.dae@samsung.com>
6 * Joonyoung Shim <jy0922.shim@samsung.com>
7 * Seung-Woo Kim <sw0312.kim@samsung.com>
10 #include <linux/component.h>
11 #include <linux/pm_runtime.h>
13 #include <drm/drm_atomic.h>
14 #include <drm/drm_atomic_helper.h>
15 #include <drm/drm_fb_helper.h>
16 #include <drm/drm_probe_helper.h>
17 #include <drm/exynos_drm.h>
19 #include "exynos_drm_drv.h"
20 #include "exynos_drm_fb.h"
21 #include "exynos_drm_fbdev.h"
22 #include "exynos_drm_g2d.h"
23 #include "exynos_drm_gem.h"
24 #include "exynos_drm_ipp.h"
25 #include "exynos_drm_plane.h"
26 #include "exynos_drm_vidi.h"
28 #define DRIVER_NAME "exynos"
29 #define DRIVER_DESC "Samsung SoC DRM"
30 #define DRIVER_DATE "20180330"
35 * 1.0 - Original version
36 * 1.1 - Upgrade IPP driver to version 2.0
38 #define DRIVER_MAJOR 1
39 #define DRIVER_MINOR 1
41 static int exynos_drm_open(struct drm_device *dev, struct drm_file *file)
43 struct drm_exynos_file_private *file_priv;
46 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
50 file->driver_priv = file_priv;
51 ret = g2d_open(dev, file);
53 goto err_file_priv_free;
59 file->driver_priv = NULL;
63 static void exynos_drm_postclose(struct drm_device *dev, struct drm_file *file)
66 kfree(file->driver_priv);
67 file->driver_priv = NULL;
70 static const struct vm_operations_struct exynos_drm_gem_vm_ops = {
71 .fault = exynos_drm_gem_fault,
72 .open = drm_gem_vm_open,
73 .close = drm_gem_vm_close,
76 static const struct drm_ioctl_desc exynos_ioctls[] = {
77 DRM_IOCTL_DEF_DRV(EXYNOS_GEM_CREATE, exynos_drm_gem_create_ioctl,
78 DRM_AUTH | DRM_RENDER_ALLOW),
79 DRM_IOCTL_DEF_DRV(EXYNOS_GEM_MAP, exynos_drm_gem_map_ioctl,
80 DRM_AUTH | DRM_RENDER_ALLOW),
81 DRM_IOCTL_DEF_DRV(EXYNOS_GEM_GET, exynos_drm_gem_get_ioctl,
83 DRM_IOCTL_DEF_DRV(EXYNOS_VIDI_CONNECTION, vidi_connection_ioctl,
85 DRM_IOCTL_DEF_DRV(EXYNOS_G2D_GET_VER, exynos_g2d_get_ver_ioctl,
86 DRM_AUTH | DRM_RENDER_ALLOW),
87 DRM_IOCTL_DEF_DRV(EXYNOS_G2D_SET_CMDLIST, exynos_g2d_set_cmdlist_ioctl,
88 DRM_AUTH | DRM_RENDER_ALLOW),
89 DRM_IOCTL_DEF_DRV(EXYNOS_G2D_EXEC, exynos_g2d_exec_ioctl,
90 DRM_AUTH | DRM_RENDER_ALLOW),
91 DRM_IOCTL_DEF_DRV(EXYNOS_IPP_GET_RESOURCES,
92 exynos_drm_ipp_get_res_ioctl,
93 DRM_AUTH | DRM_RENDER_ALLOW),
94 DRM_IOCTL_DEF_DRV(EXYNOS_IPP_GET_CAPS, exynos_drm_ipp_get_caps_ioctl,
95 DRM_AUTH | DRM_RENDER_ALLOW),
96 DRM_IOCTL_DEF_DRV(EXYNOS_IPP_GET_LIMITS,
97 exynos_drm_ipp_get_limits_ioctl,
98 DRM_AUTH | DRM_RENDER_ALLOW),
99 DRM_IOCTL_DEF_DRV(EXYNOS_IPP_COMMIT, exynos_drm_ipp_commit_ioctl,
100 DRM_AUTH | DRM_RENDER_ALLOW),
103 static const struct file_operations exynos_drm_driver_fops = {
104 .owner = THIS_MODULE,
106 .mmap = exynos_drm_gem_mmap,
109 .unlocked_ioctl = drm_ioctl,
110 .compat_ioctl = drm_compat_ioctl,
111 .release = drm_release,
114 static struct drm_driver exynos_drm_driver = {
115 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME
116 | DRIVER_ATOMIC | DRIVER_RENDER,
117 .open = exynos_drm_open,
118 .lastclose = drm_fb_helper_lastclose,
119 .postclose = exynos_drm_postclose,
120 .gem_free_object_unlocked = exynos_drm_gem_free_object,
121 .gem_vm_ops = &exynos_drm_gem_vm_ops,
122 .dumb_create = exynos_drm_gem_dumb_create,
123 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
124 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
125 .gem_prime_export = drm_gem_prime_export,
126 .gem_prime_import = exynos_drm_gem_prime_import,
127 .gem_prime_get_sg_table = exynos_drm_gem_prime_get_sg_table,
128 .gem_prime_import_sg_table = exynos_drm_gem_prime_import_sg_table,
129 .gem_prime_vmap = exynos_drm_gem_prime_vmap,
130 .gem_prime_vunmap = exynos_drm_gem_prime_vunmap,
131 .gem_prime_mmap = exynos_drm_gem_prime_mmap,
132 .ioctls = exynos_ioctls,
133 .num_ioctls = ARRAY_SIZE(exynos_ioctls),
134 .fops = &exynos_drm_driver_fops,
138 .major = DRIVER_MAJOR,
139 .minor = DRIVER_MINOR,
142 static int exynos_drm_suspend(struct device *dev)
144 struct drm_device *drm_dev = dev_get_drvdata(dev);
146 return drm_mode_config_helper_suspend(drm_dev);
149 static void exynos_drm_resume(struct device *dev)
151 struct drm_device *drm_dev = dev_get_drvdata(dev);
153 drm_mode_config_helper_resume(drm_dev);
156 static const struct dev_pm_ops exynos_drm_pm_ops = {
157 .prepare = exynos_drm_suspend,
158 .complete = exynos_drm_resume,
161 /* forward declaration */
162 static struct platform_driver exynos_drm_platform_driver;
164 struct exynos_drm_driver_info {
165 struct platform_driver *driver;
169 #define DRM_COMPONENT_DRIVER BIT(0) /* supports component framework */
170 #define DRM_VIRTUAL_DEVICE BIT(1) /* create virtual platform device */
171 #define DRM_FIMC_DEVICE BIT(2) /* devices shared with V4L2 subsystem */
173 #define DRV_PTR(drv, cond) (IS_ENABLED(cond) ? &drv : NULL)
176 * Connector drivers should not be placed before associated crtc drivers,
177 * because connector requires pipe number of its crtc during initialization.
179 static struct exynos_drm_driver_info exynos_drm_drivers[] = {
181 DRV_PTR(fimd_driver, CONFIG_DRM_EXYNOS_FIMD),
184 DRV_PTR(exynos5433_decon_driver, CONFIG_DRM_EXYNOS5433_DECON),
187 DRV_PTR(decon_driver, CONFIG_DRM_EXYNOS7_DECON),
190 DRV_PTR(mixer_driver, CONFIG_DRM_EXYNOS_MIXER),
193 DRV_PTR(mic_driver, CONFIG_DRM_EXYNOS_MIC),
196 DRV_PTR(dp_driver, CONFIG_DRM_EXYNOS_DP),
199 DRV_PTR(dsi_driver, CONFIG_DRM_EXYNOS_DSI),
202 DRV_PTR(hdmi_driver, CONFIG_DRM_EXYNOS_HDMI),
205 DRV_PTR(vidi_driver, CONFIG_DRM_EXYNOS_VIDI),
206 DRM_COMPONENT_DRIVER | DRM_VIRTUAL_DEVICE
208 DRV_PTR(g2d_driver, CONFIG_DRM_EXYNOS_G2D),
211 DRV_PTR(fimc_driver, CONFIG_DRM_EXYNOS_FIMC),
212 DRM_COMPONENT_DRIVER | DRM_FIMC_DEVICE,
214 DRV_PTR(rotator_driver, CONFIG_DRM_EXYNOS_ROTATOR),
217 DRV_PTR(scaler_driver, CONFIG_DRM_EXYNOS_SCALER),
220 DRV_PTR(gsc_driver, CONFIG_DRM_EXYNOS_GSC),
223 &exynos_drm_platform_driver,
228 static int compare_dev(struct device *dev, void *data)
230 return dev == (struct device *)data;
233 static struct component_match *exynos_drm_match_add(struct device *dev)
235 struct component_match *match = NULL;
238 for (i = 0; i < ARRAY_SIZE(exynos_drm_drivers); ++i) {
239 struct exynos_drm_driver_info *info = &exynos_drm_drivers[i];
240 struct device *p = NULL, *d;
242 if (!info->driver || !(info->flags & DRM_COMPONENT_DRIVER))
245 while ((d = bus_find_device(&platform_bus_type, p,
246 &info->driver->driver,
247 (void *)platform_bus_type.match))) {
250 if (!(info->flags & DRM_FIMC_DEVICE) ||
251 exynos_drm_check_fimc_device(d) == 0)
252 component_match_add(dev, &match,
259 return match ?: ERR_PTR(-ENODEV);
262 static int exynos_drm_bind(struct device *dev)
264 struct exynos_drm_private *private;
265 struct drm_encoder *encoder;
266 struct drm_device *drm;
267 unsigned int clone_mask;
270 drm = drm_dev_alloc(&exynos_drm_driver, dev);
274 private = kzalloc(sizeof(struct exynos_drm_private), GFP_KERNEL);
280 init_waitqueue_head(&private->wait);
281 spin_lock_init(&private->lock);
283 dev_set_drvdata(dev, drm);
284 drm->dev_private = (void *)private;
286 drm_mode_config_init(drm);
288 exynos_drm_mode_config_init(drm);
290 /* setup possible_clones. */
293 list_for_each_entry(encoder, &drm->mode_config.encoder_list, head)
294 clone_mask |= (1 << (cnt++));
296 list_for_each_entry(encoder, &drm->mode_config.encoder_list, head)
297 encoder->possible_clones = clone_mask;
299 /* Try to bind all sub drivers. */
300 ret = component_bind_all(drm->dev, drm);
302 goto err_mode_config_cleanup;
304 ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
308 drm_mode_config_reset(drm);
311 * enable drm irq mode.
312 * - with irq_enabled = true, we can use the vblank feature.
314 * P.S. note that we wouldn't use drm irq handler but
315 * just specific driver own one instead because
316 * drm framework supports only one irq handler.
318 drm->irq_enabled = true;
320 /* init kms poll for handling hpd */
321 drm_kms_helper_poll_init(drm);
323 ret = exynos_drm_fbdev_init(drm);
325 goto err_cleanup_poll;
327 /* register the DRM device */
328 ret = drm_dev_register(drm, 0);
330 goto err_cleanup_fbdev;
335 exynos_drm_fbdev_fini(drm);
337 drm_kms_helper_poll_fini(drm);
339 component_unbind_all(drm->dev, drm);
340 err_mode_config_cleanup:
341 drm_mode_config_cleanup(drm);
342 exynos_drm_cleanup_dma(drm);
350 static void exynos_drm_unbind(struct device *dev)
352 struct drm_device *drm = dev_get_drvdata(dev);
354 drm_dev_unregister(drm);
356 exynos_drm_fbdev_fini(drm);
357 drm_kms_helper_poll_fini(drm);
359 component_unbind_all(drm->dev, drm);
360 drm_mode_config_cleanup(drm);
361 exynos_drm_cleanup_dma(drm);
363 kfree(drm->dev_private);
364 drm->dev_private = NULL;
365 dev_set_drvdata(dev, NULL);
370 static const struct component_master_ops exynos_drm_ops = {
371 .bind = exynos_drm_bind,
372 .unbind = exynos_drm_unbind,
375 static int exynos_drm_platform_probe(struct platform_device *pdev)
377 struct component_match *match;
379 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
381 match = exynos_drm_match_add(&pdev->dev);
383 return PTR_ERR(match);
385 return component_master_add_with_match(&pdev->dev, &exynos_drm_ops,
389 static int exynos_drm_platform_remove(struct platform_device *pdev)
391 component_master_del(&pdev->dev, &exynos_drm_ops);
395 static struct platform_driver exynos_drm_platform_driver = {
396 .probe = exynos_drm_platform_probe,
397 .remove = exynos_drm_platform_remove,
399 .name = "exynos-drm",
400 .pm = &exynos_drm_pm_ops,
404 static void exynos_drm_unregister_devices(void)
408 for (i = ARRAY_SIZE(exynos_drm_drivers) - 1; i >= 0; --i) {
409 struct exynos_drm_driver_info *info = &exynos_drm_drivers[i];
412 if (!info->driver || !(info->flags & DRM_VIRTUAL_DEVICE))
415 while ((dev = bus_find_device(&platform_bus_type, NULL,
416 &info->driver->driver,
417 (void *)platform_bus_type.match))) {
419 platform_device_unregister(to_platform_device(dev));
424 static int exynos_drm_register_devices(void)
426 struct platform_device *pdev;
429 for (i = 0; i < ARRAY_SIZE(exynos_drm_drivers); ++i) {
430 struct exynos_drm_driver_info *info = &exynos_drm_drivers[i];
432 if (!info->driver || !(info->flags & DRM_VIRTUAL_DEVICE))
435 pdev = platform_device_register_simple(
436 info->driver->driver.name, -1, NULL, 0);
443 exynos_drm_unregister_devices();
444 return PTR_ERR(pdev);
447 static void exynos_drm_unregister_drivers(void)
451 for (i = ARRAY_SIZE(exynos_drm_drivers) - 1; i >= 0; --i) {
452 struct exynos_drm_driver_info *info = &exynos_drm_drivers[i];
457 platform_driver_unregister(info->driver);
461 static int exynos_drm_register_drivers(void)
465 for (i = 0; i < ARRAY_SIZE(exynos_drm_drivers); ++i) {
466 struct exynos_drm_driver_info *info = &exynos_drm_drivers[i];
471 ret = platform_driver_register(info->driver);
477 exynos_drm_unregister_drivers();
481 static int exynos_drm_init(void)
485 ret = exynos_drm_register_devices();
489 ret = exynos_drm_register_drivers();
491 goto err_unregister_pdevs;
495 err_unregister_pdevs:
496 exynos_drm_unregister_devices();
501 static void exynos_drm_exit(void)
503 exynos_drm_unregister_drivers();
504 exynos_drm_unregister_devices();
507 module_init(exynos_drm_init);
508 module_exit(exynos_drm_exit);
510 MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
511 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
512 MODULE_AUTHOR("Seung-Woo Kim <sw0312.kim@samsung.com>");
513 MODULE_DESCRIPTION("Samsung SoC DRM Driver");
514 MODULE_LICENSE("GPL");