2 * drivers/gpu/drm/omapdrm/omap_drv.c
4 * Copyright (C) 2011 Texas Instruments
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <linux/sys_soc.h>
22 #include <drm/drm_atomic.h>
23 #include <drm/drm_atomic_helper.h>
24 #include <drm/drm_crtc_helper.h>
25 #include <drm/drm_fb_helper.h>
27 #include "omap_dmm_tiler.h"
30 #define DRIVER_NAME MODULE_NAME
31 #define DRIVER_DESC "OMAP DRM"
32 #define DRIVER_DATE "20110917"
33 #define DRIVER_MAJOR 1
34 #define DRIVER_MINOR 0
35 #define DRIVER_PATCHLEVEL 0
41 /* Notes about mapping DSS and DRM entities:
43 * encoder: manager.. with some extension to allow one primary CRTC
44 * and zero or more video CRTC's to be mapped to one encoder?
45 * connector: dssdev.. manager can be attached/detached from different
49 static void omap_fb_output_poll_changed(struct drm_device *dev)
51 struct omap_drm_private *priv = dev->dev_private;
54 drm_fb_helper_hotplug_event(priv->fbdev);
57 static void omap_atomic_wait_for_completion(struct drm_device *dev,
58 struct drm_atomic_state *old_state)
60 struct drm_crtc_state *new_crtc_state;
61 struct drm_crtc *crtc;
65 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
66 if (!new_crtc_state->active)
69 ret = omap_crtc_wait_pending(crtc);
73 "atomic complete timeout (pipe %u)!\n", i);
77 static void omap_atomic_commit_tail(struct drm_atomic_state *old_state)
79 struct drm_device *dev = old_state->dev;
80 struct omap_drm_private *priv = dev->dev_private;
82 priv->dispc_ops->runtime_get();
84 /* Apply the atomic update. */
85 drm_atomic_helper_commit_modeset_disables(dev, old_state);
87 if (priv->omaprev != 0x3430) {
88 /* With the current dss dispc implementation we have to enable
89 * the new modeset before we can commit planes. The dispc ovl
90 * configuration relies on the video mode configuration been
91 * written into the HW when the ovl configuration is
94 * This approach is not ideal because after a mode change the
95 * plane update is executed only after the first vblank
96 * interrupt. The dispc implementation should be fixed so that
97 * it is able use uncommitted drm state information.
99 drm_atomic_helper_commit_modeset_enables(dev, old_state);
100 omap_atomic_wait_for_completion(dev, old_state);
102 drm_atomic_helper_commit_planes(dev, old_state, 0);
104 drm_atomic_helper_commit_hw_done(old_state);
107 * OMAP3 DSS seems to have issues with the work-around above,
108 * resulting in endless sync losts if a crtc is enabled without
109 * a plane. For now, skip the WA for OMAP3.
111 drm_atomic_helper_commit_planes(dev, old_state, 0);
113 drm_atomic_helper_commit_modeset_enables(dev, old_state);
115 drm_atomic_helper_commit_hw_done(old_state);
119 * Wait for completion of the page flips to ensure that old buffers
120 * can't be touched by the hardware anymore before cleaning up planes.
122 omap_atomic_wait_for_completion(dev, old_state);
124 drm_atomic_helper_cleanup_planes(dev, old_state);
126 priv->dispc_ops->runtime_put();
129 static const struct drm_mode_config_helper_funcs omap_mode_config_helper_funcs = {
130 .atomic_commit_tail = omap_atomic_commit_tail,
133 static const struct drm_mode_config_funcs omap_mode_config_funcs = {
134 .fb_create = omap_framebuffer_create,
135 .output_poll_changed = omap_fb_output_poll_changed,
136 .atomic_check = drm_atomic_helper_check,
137 .atomic_commit = drm_atomic_helper_commit,
140 static int get_connector_type(struct omap_dss_device *dssdev)
142 switch (dssdev->type) {
143 case OMAP_DISPLAY_TYPE_HDMI:
144 return DRM_MODE_CONNECTOR_HDMIA;
145 case OMAP_DISPLAY_TYPE_DVI:
146 return DRM_MODE_CONNECTOR_DVID;
147 case OMAP_DISPLAY_TYPE_DSI:
148 return DRM_MODE_CONNECTOR_DSI;
149 case OMAP_DISPLAY_TYPE_DPI:
150 case OMAP_DISPLAY_TYPE_DBI:
151 return DRM_MODE_CONNECTOR_DPI;
152 case OMAP_DISPLAY_TYPE_VENC:
153 /* TODO: This could also be composite */
154 return DRM_MODE_CONNECTOR_SVIDEO;
155 case OMAP_DISPLAY_TYPE_SDI:
156 return DRM_MODE_CONNECTOR_LVDS;
158 return DRM_MODE_CONNECTOR_Unknown;
162 static void omap_disconnect_dssdevs(void)
164 struct omap_dss_device *dssdev = NULL;
166 for_each_dss_dev(dssdev)
167 dssdev->driver->disconnect(dssdev);
170 static int omap_connect_dssdevs(void)
173 struct omap_dss_device *dssdev = NULL;
175 if (!omapdss_stack_is_ready())
176 return -EPROBE_DEFER;
178 for_each_dss_dev(dssdev) {
179 r = dssdev->driver->connect(dssdev);
180 if (r == -EPROBE_DEFER) {
181 omap_dss_put_device(dssdev);
184 dev_warn(dssdev->dev, "could not connect display: %s\n",
193 * if we are deferring probe, we disconnect the devices we previously
196 omap_disconnect_dssdevs();
201 static int omap_modeset_init_properties(struct drm_device *dev)
203 struct omap_drm_private *priv = dev->dev_private;
204 unsigned int num_planes = priv->dispc_ops->get_num_ovls();
206 priv->zorder_prop = drm_property_create_range(dev, 0, "zorder", 0,
208 if (!priv->zorder_prop)
214 static int omap_modeset_init(struct drm_device *dev)
216 struct omap_drm_private *priv = dev->dev_private;
217 struct omap_dss_device *dssdev = NULL;
218 int num_ovls = priv->dispc_ops->get_num_ovls();
219 int num_mgrs = priv->dispc_ops->get_num_mgrs();
220 int num_crtcs, crtc_idx, plane_idx;
224 drm_mode_config_init(dev);
226 ret = omap_modeset_init_properties(dev);
231 * This function creates exactly one connector, encoder, crtc,
232 * and primary plane per each connected dss-device. Each
233 * connector->encoder->crtc chain is expected to be separate
234 * and each crtc is connect to a single dss-channel. If the
235 * configuration does not match the expectations or exceeds
236 * the available resources, the configuration is rejected.
239 for_each_dss_dev(dssdev)
240 if (omapdss_device_is_connected(dssdev))
243 if (num_crtcs > num_mgrs || num_crtcs > num_ovls ||
244 num_crtcs > ARRAY_SIZE(priv->crtcs) ||
245 num_crtcs > ARRAY_SIZE(priv->planes) ||
246 num_crtcs > ARRAY_SIZE(priv->encoders) ||
247 num_crtcs > ARRAY_SIZE(priv->connectors)) {
248 dev_err(dev->dev, "%s(): Too many connected displays\n",
253 /* All planes can be put to any CRTC */
254 plane_crtc_mask = (1 << num_crtcs) - 1;
260 for_each_dss_dev(dssdev) {
261 struct drm_connector *connector;
262 struct drm_encoder *encoder;
263 struct drm_plane *plane;
264 struct drm_crtc *crtc;
266 if (!omapdss_device_is_connected(dssdev))
269 encoder = omap_encoder_init(dev, dssdev);
273 connector = omap_connector_init(dev,
274 get_connector_type(dssdev), dssdev, encoder);
278 plane = omap_plane_init(dev, plane_idx, DRM_PLANE_TYPE_PRIMARY,
281 return PTR_ERR(plane);
283 crtc = omap_crtc_init(dev, plane, dssdev);
285 return PTR_ERR(crtc);
287 drm_mode_connector_attach_encoder(connector, encoder);
288 encoder->possible_crtcs = (1 << crtc_idx);
290 priv->crtcs[priv->num_crtcs++] = crtc;
291 priv->planes[priv->num_planes++] = plane;
292 priv->encoders[priv->num_encoders++] = encoder;
293 priv->connectors[priv->num_connectors++] = connector;
300 * Create normal planes for the remaining overlays:
302 for (; plane_idx < num_ovls; plane_idx++) {
303 struct drm_plane *plane;
305 if (WARN_ON(priv->num_planes >= ARRAY_SIZE(priv->planes)))
308 plane = omap_plane_init(dev, plane_idx, DRM_PLANE_TYPE_OVERLAY,
311 return PTR_ERR(plane);
313 priv->planes[priv->num_planes++] = plane;
316 DBG("registered %d planes, %d crtcs, %d encoders and %d connectors\n",
317 priv->num_planes, priv->num_crtcs, priv->num_encoders,
318 priv->num_connectors);
320 dev->mode_config.min_width = 8;
321 dev->mode_config.min_height = 2;
323 /* note: eventually will need some cpu_is_omapXYZ() type stuff here
324 * to fill in these limits properly on different OMAP generations..
326 dev->mode_config.max_width = 2048;
327 dev->mode_config.max_height = 2048;
329 dev->mode_config.funcs = &omap_mode_config_funcs;
330 dev->mode_config.helper_private = &omap_mode_config_helper_funcs;
332 drm_mode_config_reset(dev);
334 omap_drm_irq_install(dev);
340 * Enable the HPD in external components if supported
342 static void omap_modeset_enable_external_hpd(void)
344 struct omap_dss_device *dssdev = NULL;
346 for_each_dss_dev(dssdev) {
347 if (dssdev->driver->enable_hpd)
348 dssdev->driver->enable_hpd(dssdev);
353 * Disable the HPD in external components if supported
355 static void omap_modeset_disable_external_hpd(void)
357 struct omap_dss_device *dssdev = NULL;
359 for_each_dss_dev(dssdev) {
360 if (dssdev->driver->disable_hpd)
361 dssdev->driver->disable_hpd(dssdev);
370 static int ioctl_get_param(struct drm_device *dev, void *data,
371 struct drm_file *file_priv)
373 struct omap_drm_private *priv = dev->dev_private;
374 struct drm_omap_param *args = data;
376 DBG("%p: param=%llu", dev, args->param);
378 switch (args->param) {
379 case OMAP_PARAM_CHIPSET_ID:
380 args->value = priv->omaprev;
383 DBG("unknown parameter %lld", args->param);
390 static int ioctl_set_param(struct drm_device *dev, void *data,
391 struct drm_file *file_priv)
393 struct drm_omap_param *args = data;
395 switch (args->param) {
397 DBG("unknown parameter %lld", args->param);
404 #define OMAP_BO_USER_MASK 0x00ffffff /* flags settable by userspace */
406 static int ioctl_gem_new(struct drm_device *dev, void *data,
407 struct drm_file *file_priv)
409 struct drm_omap_gem_new *args = data;
410 u32 flags = args->flags & OMAP_BO_USER_MASK;
412 VERB("%p:%p: size=0x%08x, flags=%08x", dev, file_priv,
413 args->size.bytes, flags);
415 return omap_gem_new_handle(dev, file_priv, args->size, flags,
419 static int ioctl_gem_info(struct drm_device *dev, void *data,
420 struct drm_file *file_priv)
422 struct drm_omap_gem_info *args = data;
423 struct drm_gem_object *obj;
426 VERB("%p:%p: handle=%d", dev, file_priv, args->handle);
428 obj = drm_gem_object_lookup(file_priv, args->handle);
432 args->size = omap_gem_mmap_size(obj);
433 args->offset = omap_gem_mmap_offset(obj);
435 drm_gem_object_unreference_unlocked(obj);
440 static const struct drm_ioctl_desc ioctls[DRM_COMMAND_END - DRM_COMMAND_BASE] = {
441 DRM_IOCTL_DEF_DRV(OMAP_GET_PARAM, ioctl_get_param,
442 DRM_AUTH | DRM_RENDER_ALLOW),
443 DRM_IOCTL_DEF_DRV(OMAP_SET_PARAM, ioctl_set_param,
444 DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY),
445 DRM_IOCTL_DEF_DRV(OMAP_GEM_NEW, ioctl_gem_new,
446 DRM_AUTH | DRM_RENDER_ALLOW),
447 /* Deprecated, to be removed. */
448 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_PREP, drm_noop,
449 DRM_AUTH | DRM_RENDER_ALLOW),
450 /* Deprecated, to be removed. */
451 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_FINI, drm_noop,
452 DRM_AUTH | DRM_RENDER_ALLOW),
453 DRM_IOCTL_DEF_DRV(OMAP_GEM_INFO, ioctl_gem_info,
454 DRM_AUTH | DRM_RENDER_ALLOW),
461 static int dev_open(struct drm_device *dev, struct drm_file *file)
463 file->driver_priv = NULL;
465 DBG("open: dev=%p, file=%p", dev, file);
471 * lastclose - clean up after all DRM clients have exited
474 * Take care of cleaning up after all DRM clients have exited. In the
475 * mode setting case, we want to restore the kernel's initial mode (just
476 * in case the last client left us in a bad state).
478 static void dev_lastclose(struct drm_device *dev)
480 struct omap_drm_private *priv = dev->dev_private;
483 DBG("lastclose: dev=%p", dev);
486 ret = drm_fb_helper_restore_fbdev_mode_unlocked(priv->fbdev);
488 DBG("failed to restore crtc mode");
492 static const struct vm_operations_struct omap_gem_vm_ops = {
493 .fault = omap_gem_fault,
494 .open = drm_gem_vm_open,
495 .close = drm_gem_vm_close,
498 static const struct file_operations omapdriver_fops = {
499 .owner = THIS_MODULE,
501 .unlocked_ioctl = drm_ioctl,
502 .compat_ioctl = drm_compat_ioctl,
503 .release = drm_release,
504 .mmap = omap_gem_mmap,
507 .llseek = noop_llseek,
510 static struct drm_driver omap_drm_driver = {
511 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
512 DRIVER_ATOMIC | DRIVER_RENDER,
514 .lastclose = dev_lastclose,
515 #ifdef CONFIG_DEBUG_FS
516 .debugfs_init = omap_debugfs_init,
518 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
519 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
520 .gem_prime_export = omap_gem_prime_export,
521 .gem_prime_import = omap_gem_prime_import,
522 .gem_free_object = omap_gem_free_object,
523 .gem_vm_ops = &omap_gem_vm_ops,
524 .dumb_create = omap_gem_dumb_create,
525 .dumb_map_offset = omap_gem_dumb_map_offset,
527 .num_ioctls = DRM_OMAP_NUM_IOCTLS,
528 .fops = &omapdriver_fops,
532 .major = DRIVER_MAJOR,
533 .minor = DRIVER_MINOR,
534 .patchlevel = DRIVER_PATCHLEVEL,
537 static const struct soc_device_attribute omapdrm_soc_devices[] = {
538 { .family = "OMAP3", .data = (void *)0x3430 },
539 { .family = "OMAP4", .data = (void *)0x4430 },
540 { .family = "OMAP5", .data = (void *)0x5430 },
541 { .family = "DRA7", .data = (void *)0x0752 },
545 static int pdev_probe(struct platform_device *pdev)
547 const struct soc_device_attribute *soc;
548 struct omap_drm_private *priv;
549 struct drm_device *ddev;
553 DBG("%s", pdev->name);
555 if (omapdss_is_initialized() == false)
556 return -EPROBE_DEFER;
558 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
560 dev_err(&pdev->dev, "Failed to set the DMA mask\n");
564 omap_crtc_pre_init();
566 ret = omap_connect_dssdevs();
568 goto err_crtc_uninit;
570 /* Allocate and initialize the driver private structure. */
571 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
574 goto err_disconnect_dssdevs;
577 priv->dispc_ops = dispc_get_ops();
579 soc = soc_device_match(omapdrm_soc_devices);
580 priv->omaprev = soc ? (unsigned int)soc->data : 0;
581 priv->wq = alloc_ordered_workqueue("omapdrm", 0);
583 spin_lock_init(&priv->list_lock);
584 INIT_LIST_HEAD(&priv->obj_list);
586 /* Allocate and initialize the DRM device. */
587 ddev = drm_dev_alloc(&omap_drm_driver, &pdev->dev);
593 ddev->dev_private = priv;
594 platform_set_drvdata(pdev, ddev);
598 ret = omap_modeset_init(ddev);
600 dev_err(&pdev->dev, "omap_modeset_init failed: ret=%d\n", ret);
601 goto err_free_drm_dev;
604 /* Initialize vblank handling, start with all CRTCs disabled. */
605 ret = drm_vblank_init(ddev, priv->num_crtcs);
607 dev_err(&pdev->dev, "could not init vblank\n");
608 goto err_cleanup_modeset;
611 for (i = 0; i < priv->num_crtcs; i++)
612 drm_crtc_vblank_off(priv->crtcs[i]);
614 priv->fbdev = omap_fbdev_init(ddev);
616 drm_kms_helper_poll_init(ddev);
617 omap_modeset_enable_external_hpd();
620 * Register the DRM device with the core and the connectors with
623 ret = drm_dev_register(ddev, 0);
625 goto err_cleanup_helpers;
630 omap_modeset_disable_external_hpd();
631 drm_kms_helper_poll_fini(ddev);
633 omap_fbdev_free(ddev);
635 drm_mode_config_cleanup(ddev);
636 omap_drm_irq_uninstall(ddev);
638 omap_gem_deinit(ddev);
641 destroy_workqueue(priv->wq);
643 err_disconnect_dssdevs:
644 omap_disconnect_dssdevs();
646 omap_crtc_pre_uninit();
650 static int pdev_remove(struct platform_device *pdev)
652 struct drm_device *ddev = platform_get_drvdata(pdev);
653 struct omap_drm_private *priv = ddev->dev_private;
657 drm_dev_unregister(ddev);
659 omap_modeset_disable_external_hpd();
660 drm_kms_helper_poll_fini(ddev);
663 omap_fbdev_free(ddev);
665 drm_atomic_helper_shutdown(ddev);
667 drm_mode_config_cleanup(ddev);
669 omap_drm_irq_uninstall(ddev);
670 omap_gem_deinit(ddev);
674 destroy_workqueue(priv->wq);
677 omap_disconnect_dssdevs();
678 omap_crtc_pre_uninit();
683 #ifdef CONFIG_PM_SLEEP
684 static int omap_drm_suspend_all_displays(void)
686 struct omap_dss_device *dssdev = NULL;
688 for_each_dss_dev(dssdev) {
692 if (dssdev->state == OMAP_DSS_DISPLAY_ACTIVE) {
693 dssdev->driver->disable(dssdev);
694 dssdev->activate_after_resume = true;
696 dssdev->activate_after_resume = false;
703 static int omap_drm_resume_all_displays(void)
705 struct omap_dss_device *dssdev = NULL;
707 for_each_dss_dev(dssdev) {
711 if (dssdev->activate_after_resume) {
712 dssdev->driver->enable(dssdev);
713 dssdev->activate_after_resume = false;
720 static int omap_drm_suspend(struct device *dev)
722 struct drm_device *drm_dev = dev_get_drvdata(dev);
724 drm_kms_helper_poll_disable(drm_dev);
726 drm_modeset_lock_all(drm_dev);
727 omap_drm_suspend_all_displays();
728 drm_modeset_unlock_all(drm_dev);
733 static int omap_drm_resume(struct device *dev)
735 struct drm_device *drm_dev = dev_get_drvdata(dev);
737 drm_modeset_lock_all(drm_dev);
738 omap_drm_resume_all_displays();
739 drm_modeset_unlock_all(drm_dev);
741 drm_kms_helper_poll_enable(drm_dev);
743 return omap_gem_resume(dev);
747 static SIMPLE_DEV_PM_OPS(omapdrm_pm_ops, omap_drm_suspend, omap_drm_resume);
749 static struct platform_driver pdev = {
752 .pm = &omapdrm_pm_ops,
755 .remove = pdev_remove,
758 static struct platform_driver * const drivers[] = {
763 static int __init omap_drm_init(void)
767 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
770 static void __exit omap_drm_fini(void)
774 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
777 /* need late_initcall() so we load after dss_driver's are loaded */
778 late_initcall(omap_drm_init);
779 module_exit(omap_drm_fini);
782 MODULE_DESCRIPTION("OMAP DRM Display Driver");
783 MODULE_ALIAS("platform:" DRIVER_NAME);
784 MODULE_LICENSE("GPL v2");