1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
8 #include <linux/sort.h>
9 #include <linux/sys_soc.h>
11 #include <drm/drm_atomic.h>
12 #include <drm/drm_atomic_helper.h>
13 #include <drm/drm_fb_helper.h>
14 #include <drm/drm_probe_helper.h>
15 #include <drm/drm_panel.h>
17 #include "omap_dmm_tiler.h"
20 #define DRIVER_NAME MODULE_NAME
21 #define DRIVER_DESC "OMAP DRM"
22 #define DRIVER_DATE "20110917"
23 #define DRIVER_MAJOR 1
24 #define DRIVER_MINOR 0
25 #define DRIVER_PATCHLEVEL 0
31 /* Notes about mapping DSS and DRM entities:
33 * encoder: manager.. with some extension to allow one primary CRTC
34 * and zero or more video CRTC's to be mapped to one encoder?
35 * connector: dssdev.. manager can be attached/detached from different
39 static void omap_atomic_wait_for_completion(struct drm_device *dev,
40 struct drm_atomic_state *old_state)
42 struct drm_crtc_state *new_crtc_state;
43 struct drm_crtc *crtc;
47 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
48 if (!new_crtc_state->active)
51 ret = omap_crtc_wait_pending(crtc);
55 "atomic complete timeout (pipe %u)!\n", i);
59 static void omap_atomic_commit_tail(struct drm_atomic_state *old_state)
61 struct drm_device *dev = old_state->dev;
62 struct omap_drm_private *priv = dev->dev_private;
64 priv->dispc_ops->runtime_get(priv->dispc);
66 /* Apply the atomic update. */
67 drm_atomic_helper_commit_modeset_disables(dev, old_state);
69 if (priv->omaprev != 0x3430) {
70 /* With the current dss dispc implementation we have to enable
71 * the new modeset before we can commit planes. The dispc ovl
72 * configuration relies on the video mode configuration been
73 * written into the HW when the ovl configuration is
76 * This approach is not ideal because after a mode change the
77 * plane update is executed only after the first vblank
78 * interrupt. The dispc implementation should be fixed so that
79 * it is able use uncommitted drm state information.
81 drm_atomic_helper_commit_modeset_enables(dev, old_state);
82 omap_atomic_wait_for_completion(dev, old_state);
84 drm_atomic_helper_commit_planes(dev, old_state, 0);
86 drm_atomic_helper_commit_hw_done(old_state);
89 * OMAP3 DSS seems to have issues with the work-around above,
90 * resulting in endless sync losts if a crtc is enabled without
91 * a plane. For now, skip the WA for OMAP3.
93 drm_atomic_helper_commit_planes(dev, old_state, 0);
95 drm_atomic_helper_commit_modeset_enables(dev, old_state);
97 drm_atomic_helper_commit_hw_done(old_state);
101 * Wait for completion of the page flips to ensure that old buffers
102 * can't be touched by the hardware anymore before cleaning up planes.
104 omap_atomic_wait_for_completion(dev, old_state);
106 drm_atomic_helper_cleanup_planes(dev, old_state);
108 priv->dispc_ops->runtime_put(priv->dispc);
111 static const struct drm_mode_config_helper_funcs omap_mode_config_helper_funcs = {
112 .atomic_commit_tail = omap_atomic_commit_tail,
115 static const struct drm_mode_config_funcs omap_mode_config_funcs = {
116 .fb_create = omap_framebuffer_create,
117 .output_poll_changed = drm_fb_helper_output_poll_changed,
118 .atomic_check = drm_atomic_helper_check,
119 .atomic_commit = drm_atomic_helper_commit,
122 static void omap_disconnect_pipelines(struct drm_device *ddev)
124 struct omap_drm_private *priv = ddev->dev_private;
127 for (i = 0; i < priv->num_pipes; i++) {
128 struct omap_drm_pipeline *pipe = &priv->pipes[i];
130 if (pipe->output->panel)
131 drm_panel_detach(pipe->output->panel);
133 omapdss_device_disconnect(NULL, pipe->output);
135 omapdss_device_put(pipe->output);
139 memset(&priv->channels, 0, sizeof(priv->channels));
144 static int omap_connect_pipelines(struct drm_device *ddev)
146 struct omap_drm_private *priv = ddev->dev_private;
147 struct omap_dss_device *output = NULL;
150 for_each_dss_output(output) {
151 r = omapdss_device_connect(priv->dss, NULL, output);
152 if (r == -EPROBE_DEFER) {
153 omapdss_device_put(output);
156 dev_warn(output->dev, "could not connect output %s\n",
159 struct omap_drm_pipeline *pipe;
161 pipe = &priv->pipes[priv->num_pipes++];
162 pipe->output = omapdss_device_get(output);
164 if (priv->num_pipes == ARRAY_SIZE(priv->pipes)) {
165 /* To balance the 'for_each_dss_output' loop */
166 omapdss_device_put(output);
175 static int omap_compare_pipelines(const void *a, const void *b)
177 const struct omap_drm_pipeline *pipe1 = a;
178 const struct omap_drm_pipeline *pipe2 = b;
180 if (pipe1->alias_id > pipe2->alias_id)
182 else if (pipe1->alias_id < pipe2->alias_id)
187 static int omap_modeset_init_properties(struct drm_device *dev)
189 struct omap_drm_private *priv = dev->dev_private;
190 unsigned int num_planes = priv->dispc_ops->get_num_ovls(priv->dispc);
192 priv->zorder_prop = drm_property_create_range(dev, 0, "zorder", 0,
194 if (!priv->zorder_prop)
200 static int omap_display_id(struct omap_dss_device *output)
202 struct device_node *node = NULL;
205 struct omap_dss_device *display;
207 display = omapdss_display_get(output);
208 node = display->dev->of_node;
209 omapdss_device_put(display);
210 } else if (output->bridge) {
211 struct drm_bridge *bridge = output->bridge;
214 bridge = bridge->next;
216 node = bridge->of_node;
217 } else if (output->panel) {
218 node = output->panel->dev->of_node;
221 return node ? of_alias_get_id(node, "display") : -ENODEV;
224 static int omap_modeset_init(struct drm_device *dev)
226 struct omap_drm_private *priv = dev->dev_private;
227 int num_ovls = priv->dispc_ops->get_num_ovls(priv->dispc);
228 int num_mgrs = priv->dispc_ops->get_num_mgrs(priv->dispc);
233 if (!omapdss_stack_is_ready())
234 return -EPROBE_DEFER;
236 drm_mode_config_init(dev);
238 ret = omap_modeset_init_properties(dev);
243 * This function creates exactly one connector, encoder, crtc,
244 * and primary plane per each connected dss-device. Each
245 * connector->encoder->crtc chain is expected to be separate
246 * and each crtc is connect to a single dss-channel. If the
247 * configuration does not match the expectations or exceeds
248 * the available resources, the configuration is rejected.
250 ret = omap_connect_pipelines(dev);
254 if (priv->num_pipes > num_mgrs || priv->num_pipes > num_ovls) {
255 dev_err(dev->dev, "%s(): Too many connected displays\n",
260 /* Create all planes first. They can all be put to any CRTC. */
261 plane_crtc_mask = (1 << priv->num_pipes) - 1;
263 for (i = 0; i < num_ovls; i++) {
264 enum drm_plane_type type = i < priv->num_pipes
265 ? DRM_PLANE_TYPE_PRIMARY
266 : DRM_PLANE_TYPE_OVERLAY;
267 struct drm_plane *plane;
269 if (WARN_ON(priv->num_planes >= ARRAY_SIZE(priv->planes)))
272 plane = omap_plane_init(dev, i, type, plane_crtc_mask);
274 return PTR_ERR(plane);
276 priv->planes[priv->num_planes++] = plane;
280 * Create the encoders, attach the bridges and get the pipeline alias
283 for (i = 0; i < priv->num_pipes; i++) {
284 struct omap_drm_pipeline *pipe = &priv->pipes[i];
287 pipe->encoder = omap_encoder_init(dev, pipe->output);
291 if (pipe->output->bridge) {
292 ret = drm_bridge_attach(pipe->encoder,
293 pipe->output->bridge, NULL);
298 id = omap_display_id(pipe->output);
299 pipe->alias_id = id >= 0 ? id : i;
302 /* Sort the pipelines by DT aliases. */
303 sort(priv->pipes, priv->num_pipes, sizeof(priv->pipes[0]),
304 omap_compare_pipelines, NULL);
307 * Populate the pipeline lookup table by DISPC channel. Only one display
308 * is allowed per channel.
310 for (i = 0; i < priv->num_pipes; ++i) {
311 struct omap_drm_pipeline *pipe = &priv->pipes[i];
312 enum omap_channel channel = pipe->output->dispc_channel;
314 if (WARN_ON(priv->channels[channel] != NULL))
317 priv->channels[channel] = pipe;
320 /* Create the connectors and CRTCs. */
321 for (i = 0; i < priv->num_pipes; i++) {
322 struct omap_drm_pipeline *pipe = &priv->pipes[i];
323 struct drm_encoder *encoder = pipe->encoder;
324 struct drm_crtc *crtc;
326 if (!pipe->output->bridge) {
327 pipe->connector = omap_connector_init(dev, pipe->output,
329 if (!pipe->connector)
332 drm_connector_attach_encoder(pipe->connector, encoder);
334 if (pipe->output->panel) {
335 ret = drm_panel_attach(pipe->output->panel,
342 crtc = omap_crtc_init(dev, pipe, priv->planes[i]);
344 return PTR_ERR(crtc);
346 encoder->possible_crtcs = 1 << i;
350 DBG("registered %u planes, %u crtcs/encoders/connectors\n",
351 priv->num_planes, priv->num_pipes);
353 dev->mode_config.min_width = 8;
354 dev->mode_config.min_height = 2;
357 * Note: these values are used for multiple independent things:
358 * connector mode filtering, buffer sizes, crtc sizes...
359 * Use big enough values here to cover all use cases, and do more
360 * specific checking in the respective code paths.
362 dev->mode_config.max_width = 8192;
363 dev->mode_config.max_height = 8192;
365 /* We want the zpos to be normalized */
366 dev->mode_config.normalize_zpos = true;
368 dev->mode_config.funcs = &omap_mode_config_funcs;
369 dev->mode_config.helper_private = &omap_mode_config_helper_funcs;
371 drm_mode_config_reset(dev);
373 omap_drm_irq_install(dev);
379 * Enable the HPD in external components if supported
381 static void omap_modeset_enable_external_hpd(struct drm_device *ddev)
383 struct omap_drm_private *priv = ddev->dev_private;
386 for (i = 0; i < priv->num_pipes; i++) {
387 if (priv->pipes[i].connector)
388 omap_connector_enable_hpd(priv->pipes[i].connector);
393 * Disable the HPD in external components if supported
395 static void omap_modeset_disable_external_hpd(struct drm_device *ddev)
397 struct omap_drm_private *priv = ddev->dev_private;
400 for (i = 0; i < priv->num_pipes; i++) {
401 if (priv->pipes[i].connector)
402 omap_connector_disable_hpd(priv->pipes[i].connector);
411 static int ioctl_get_param(struct drm_device *dev, void *data,
412 struct drm_file *file_priv)
414 struct omap_drm_private *priv = dev->dev_private;
415 struct drm_omap_param *args = data;
417 DBG("%p: param=%llu", dev, args->param);
419 switch (args->param) {
420 case OMAP_PARAM_CHIPSET_ID:
421 args->value = priv->omaprev;
424 DBG("unknown parameter %lld", args->param);
431 #define OMAP_BO_USER_MASK 0x00ffffff /* flags settable by userspace */
433 static int ioctl_gem_new(struct drm_device *dev, void *data,
434 struct drm_file *file_priv)
436 struct drm_omap_gem_new *args = data;
437 u32 flags = args->flags & OMAP_BO_USER_MASK;
439 VERB("%p:%p: size=0x%08x, flags=%08x", dev, file_priv,
440 args->size.bytes, flags);
442 return omap_gem_new_handle(dev, file_priv, args->size, flags,
446 static int ioctl_gem_info(struct drm_device *dev, void *data,
447 struct drm_file *file_priv)
449 struct drm_omap_gem_info *args = data;
450 struct drm_gem_object *obj;
453 VERB("%p:%p: handle=%d", dev, file_priv, args->handle);
455 obj = drm_gem_object_lookup(file_priv, args->handle);
459 args->size = omap_gem_mmap_size(obj);
460 args->offset = omap_gem_mmap_offset(obj);
462 drm_gem_object_put_unlocked(obj);
467 static const struct drm_ioctl_desc ioctls[DRM_COMMAND_END - DRM_COMMAND_BASE] = {
468 DRM_IOCTL_DEF_DRV(OMAP_GET_PARAM, ioctl_get_param,
469 DRM_AUTH | DRM_RENDER_ALLOW),
470 DRM_IOCTL_DEF_DRV(OMAP_SET_PARAM, drm_invalid_op,
471 DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY),
472 DRM_IOCTL_DEF_DRV(OMAP_GEM_NEW, ioctl_gem_new,
473 DRM_AUTH | DRM_RENDER_ALLOW),
474 /* Deprecated, to be removed. */
475 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_PREP, drm_noop,
476 DRM_AUTH | DRM_RENDER_ALLOW),
477 /* Deprecated, to be removed. */
478 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_FINI, drm_noop,
479 DRM_AUTH | DRM_RENDER_ALLOW),
480 DRM_IOCTL_DEF_DRV(OMAP_GEM_INFO, ioctl_gem_info,
481 DRM_AUTH | DRM_RENDER_ALLOW),
488 static int dev_open(struct drm_device *dev, struct drm_file *file)
490 file->driver_priv = NULL;
492 DBG("open: dev=%p, file=%p", dev, file);
497 static const struct vm_operations_struct omap_gem_vm_ops = {
498 .fault = omap_gem_fault,
499 .open = drm_gem_vm_open,
500 .close = drm_gem_vm_close,
503 static const struct file_operations omapdriver_fops = {
504 .owner = THIS_MODULE,
506 .unlocked_ioctl = drm_ioctl,
507 .compat_ioctl = drm_compat_ioctl,
508 .release = drm_release,
509 .mmap = omap_gem_mmap,
512 .llseek = noop_llseek,
515 static struct drm_driver omap_drm_driver = {
516 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
517 DRIVER_ATOMIC | DRIVER_RENDER,
519 .lastclose = drm_fb_helper_lastclose,
520 #ifdef CONFIG_DEBUG_FS
521 .debugfs_init = omap_debugfs_init,
523 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
524 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
525 .gem_prime_export = omap_gem_prime_export,
526 .gem_prime_import = omap_gem_prime_import,
527 .gem_free_object_unlocked = omap_gem_free_object,
528 .gem_vm_ops = &omap_gem_vm_ops,
529 .dumb_create = omap_gem_dumb_create,
530 .dumb_map_offset = omap_gem_dumb_map_offset,
532 .num_ioctls = DRM_OMAP_NUM_IOCTLS,
533 .fops = &omapdriver_fops,
537 .major = DRIVER_MAJOR,
538 .minor = DRIVER_MINOR,
539 .patchlevel = DRIVER_PATCHLEVEL,
542 static const struct soc_device_attribute omapdrm_soc_devices[] = {
543 { .family = "OMAP3", .data = (void *)0x3430 },
544 { .family = "OMAP4", .data = (void *)0x4430 },
545 { .family = "OMAP5", .data = (void *)0x5430 },
546 { .family = "DRA7", .data = (void *)0x0752 },
550 static int omapdrm_init(struct omap_drm_private *priv, struct device *dev)
552 const struct soc_device_attribute *soc;
553 struct drm_device *ddev;
557 DBG("%s", dev_name(dev));
559 /* Allocate and initialize the DRM device. */
560 ddev = drm_dev_alloc(&omap_drm_driver, dev);
562 return PTR_ERR(ddev);
565 ddev->dev_private = priv;
568 priv->dss = omapdss_get_dss();
569 priv->dispc = dispc_get_dispc(priv->dss);
570 priv->dispc_ops = dispc_get_ops(priv->dss);
572 omap_crtc_pre_init(priv);
574 soc = soc_device_match(omapdrm_soc_devices);
575 priv->omaprev = soc ? (unsigned int)soc->data : 0;
576 priv->wq = alloc_ordered_workqueue("omapdrm", 0);
578 mutex_init(&priv->list_lock);
579 INIT_LIST_HEAD(&priv->obj_list);
581 /* Get memory bandwidth limits */
582 if (priv->dispc_ops->get_memory_bandwidth_limit)
583 priv->max_bandwidth =
584 priv->dispc_ops->get_memory_bandwidth_limit(priv->dispc);
588 ret = omap_modeset_init(ddev);
590 dev_err(priv->dev, "omap_modeset_init failed: ret=%d\n", ret);
594 /* Initialize vblank handling, start with all CRTCs disabled. */
595 ret = drm_vblank_init(ddev, priv->num_pipes);
597 dev_err(priv->dev, "could not init vblank\n");
598 goto err_cleanup_modeset;
601 for (i = 0; i < priv->num_pipes; i++)
602 drm_crtc_vblank_off(priv->pipes[i].crtc);
604 omap_fbdev_init(ddev);
606 drm_kms_helper_poll_init(ddev);
607 omap_modeset_enable_external_hpd(ddev);
610 * Register the DRM device with the core and the connectors with
613 ret = drm_dev_register(ddev, 0);
615 goto err_cleanup_helpers;
620 omap_modeset_disable_external_hpd(ddev);
621 drm_kms_helper_poll_fini(ddev);
623 omap_fbdev_fini(ddev);
625 drm_mode_config_cleanup(ddev);
626 omap_drm_irq_uninstall(ddev);
628 omap_gem_deinit(ddev);
629 destroy_workqueue(priv->wq);
630 omap_disconnect_pipelines(ddev);
631 omap_crtc_pre_uninit(priv);
636 static void omapdrm_cleanup(struct omap_drm_private *priv)
638 struct drm_device *ddev = priv->ddev;
642 drm_dev_unregister(ddev);
644 omap_modeset_disable_external_hpd(ddev);
645 drm_kms_helper_poll_fini(ddev);
647 omap_fbdev_fini(ddev);
649 drm_atomic_helper_shutdown(ddev);
651 drm_mode_config_cleanup(ddev);
653 omap_drm_irq_uninstall(ddev);
654 omap_gem_deinit(ddev);
656 destroy_workqueue(priv->wq);
658 omap_disconnect_pipelines(ddev);
659 omap_crtc_pre_uninit(priv);
664 static int pdev_probe(struct platform_device *pdev)
666 struct omap_drm_private *priv;
669 if (omapdss_is_initialized() == false)
670 return -EPROBE_DEFER;
672 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
674 dev_err(&pdev->dev, "Failed to set the DMA mask\n");
678 /* Allocate and initialize the driver private structure. */
679 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
683 platform_set_drvdata(pdev, priv);
685 ret = omapdrm_init(priv, &pdev->dev);
692 static int pdev_remove(struct platform_device *pdev)
694 struct omap_drm_private *priv = platform_get_drvdata(pdev);
696 omapdrm_cleanup(priv);
702 #ifdef CONFIG_PM_SLEEP
703 static int omap_drm_suspend(struct device *dev)
705 struct omap_drm_private *priv = dev_get_drvdata(dev);
706 struct drm_device *drm_dev = priv->ddev;
708 return drm_mode_config_helper_suspend(drm_dev);
711 static int omap_drm_resume(struct device *dev)
713 struct omap_drm_private *priv = dev_get_drvdata(dev);
714 struct drm_device *drm_dev = priv->ddev;
716 drm_mode_config_helper_resume(drm_dev);
718 return omap_gem_resume(drm_dev);
722 static SIMPLE_DEV_PM_OPS(omapdrm_pm_ops, omap_drm_suspend, omap_drm_resume);
724 static struct platform_driver pdev = {
727 .pm = &omapdrm_pm_ops,
730 .remove = pdev_remove,
733 static struct platform_driver * const drivers[] = {
738 static int __init omap_drm_init(void)
742 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
745 static void __exit omap_drm_fini(void)
749 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
752 /* need late_initcall() so we load after dss_driver's are loaded */
753 late_initcall(omap_drm_init);
754 module_exit(omap_drm_fini);
757 MODULE_DESCRIPTION("OMAP DRM Display Driver");
758 MODULE_ALIAS("platform:" DRIVER_NAME);
759 MODULE_LICENSE("GPL v2");