2 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <linux/sort.h>
20 #include <linux/sys_soc.h>
22 #include <drm/drm_atomic.h>
23 #include <drm/drm_atomic_helper.h>
24 #include <drm/drm_fb_helper.h>
25 #include <drm/drm_probe_helper.h>
26 #include <drm/drm_panel.h>
28 #include "omap_dmm_tiler.h"
31 #define DRIVER_NAME MODULE_NAME
32 #define DRIVER_DESC "OMAP DRM"
33 #define DRIVER_DATE "20110917"
34 #define DRIVER_MAJOR 1
35 #define DRIVER_MINOR 0
36 #define DRIVER_PATCHLEVEL 0
42 /* Notes about mapping DSS and DRM entities:
44 * encoder: manager.. with some extension to allow one primary CRTC
45 * and zero or more video CRTC's to be mapped to one encoder?
46 * connector: dssdev.. manager can be attached/detached from different
50 static void omap_atomic_wait_for_completion(struct drm_device *dev,
51 struct drm_atomic_state *old_state)
53 struct drm_crtc_state *new_crtc_state;
54 struct drm_crtc *crtc;
58 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
59 if (!new_crtc_state->active)
62 ret = omap_crtc_wait_pending(crtc);
66 "atomic complete timeout (pipe %u)!\n", i);
70 static void omap_atomic_commit_tail(struct drm_atomic_state *old_state)
72 struct drm_device *dev = old_state->dev;
73 struct omap_drm_private *priv = dev->dev_private;
75 priv->dispc_ops->runtime_get(priv->dispc);
77 /* Apply the atomic update. */
78 drm_atomic_helper_commit_modeset_disables(dev, old_state);
80 if (priv->omaprev != 0x3430) {
81 /* With the current dss dispc implementation we have to enable
82 * the new modeset before we can commit planes. The dispc ovl
83 * configuration relies on the video mode configuration been
84 * written into the HW when the ovl configuration is
87 * This approach is not ideal because after a mode change the
88 * plane update is executed only after the first vblank
89 * interrupt. The dispc implementation should be fixed so that
90 * it is able use uncommitted drm state information.
92 drm_atomic_helper_commit_modeset_enables(dev, old_state);
93 omap_atomic_wait_for_completion(dev, old_state);
95 drm_atomic_helper_commit_planes(dev, old_state, 0);
97 drm_atomic_helper_commit_hw_done(old_state);
100 * OMAP3 DSS seems to have issues with the work-around above,
101 * resulting in endless sync losts if a crtc is enabled without
102 * a plane. For now, skip the WA for OMAP3.
104 drm_atomic_helper_commit_planes(dev, old_state, 0);
106 drm_atomic_helper_commit_modeset_enables(dev, old_state);
108 drm_atomic_helper_commit_hw_done(old_state);
112 * Wait for completion of the page flips to ensure that old buffers
113 * can't be touched by the hardware anymore before cleaning up planes.
115 omap_atomic_wait_for_completion(dev, old_state);
117 drm_atomic_helper_cleanup_planes(dev, old_state);
119 priv->dispc_ops->runtime_put(priv->dispc);
122 static const struct drm_mode_config_helper_funcs omap_mode_config_helper_funcs = {
123 .atomic_commit_tail = omap_atomic_commit_tail,
126 static const struct drm_mode_config_funcs omap_mode_config_funcs = {
127 .fb_create = omap_framebuffer_create,
128 .output_poll_changed = drm_fb_helper_output_poll_changed,
129 .atomic_check = drm_atomic_helper_check,
130 .atomic_commit = drm_atomic_helper_commit,
133 static void omap_disconnect_pipelines(struct drm_device *ddev)
135 struct omap_drm_private *priv = ddev->dev_private;
138 for (i = 0; i < priv->num_pipes; i++) {
139 struct omap_drm_pipeline *pipe = &priv->pipes[i];
141 if (pipe->output->panel)
142 drm_panel_detach(pipe->output->panel);
144 omapdss_device_disconnect(NULL, pipe->output);
146 omapdss_device_put(pipe->output);
150 memset(&priv->channels, 0, sizeof(priv->channels));
155 static int omap_connect_pipelines(struct drm_device *ddev)
157 struct omap_drm_private *priv = ddev->dev_private;
158 struct omap_dss_device *output = NULL;
161 for_each_dss_output(output) {
162 r = omapdss_device_connect(priv->dss, NULL, output);
163 if (r == -EPROBE_DEFER) {
164 omapdss_device_put(output);
167 dev_warn(output->dev, "could not connect output %s\n",
170 struct omap_drm_pipeline *pipe;
172 pipe = &priv->pipes[priv->num_pipes++];
173 pipe->output = omapdss_device_get(output);
175 if (priv->num_pipes == ARRAY_SIZE(priv->pipes)) {
176 /* To balance the 'for_each_dss_output' loop */
177 omapdss_device_put(output);
186 static int omap_compare_pipelines(const void *a, const void *b)
188 const struct omap_drm_pipeline *pipe1 = a;
189 const struct omap_drm_pipeline *pipe2 = b;
191 if (pipe1->alias_id > pipe2->alias_id)
193 else if (pipe1->alias_id < pipe2->alias_id)
198 static int omap_modeset_init_properties(struct drm_device *dev)
200 struct omap_drm_private *priv = dev->dev_private;
201 unsigned int num_planes = priv->dispc_ops->get_num_ovls(priv->dispc);
203 priv->zorder_prop = drm_property_create_range(dev, 0, "zorder", 0,
205 if (!priv->zorder_prop)
211 static int omap_display_id(struct omap_dss_device *output)
213 struct device_node *node = NULL;
216 struct omap_dss_device *display;
218 display = omapdss_display_get(output);
219 node = display->dev->of_node;
220 omapdss_device_put(display);
221 } else if (output->bridge) {
222 struct drm_bridge *bridge = output->bridge;
225 bridge = bridge->next;
227 node = bridge->of_node;
228 } else if (output->panel) {
229 node = output->panel->dev->of_node;
232 return node ? of_alias_get_id(node, "display") : -ENODEV;
235 static int omap_modeset_init(struct drm_device *dev)
237 struct omap_drm_private *priv = dev->dev_private;
238 int num_ovls = priv->dispc_ops->get_num_ovls(priv->dispc);
239 int num_mgrs = priv->dispc_ops->get_num_mgrs(priv->dispc);
244 if (!omapdss_stack_is_ready())
245 return -EPROBE_DEFER;
247 drm_mode_config_init(dev);
249 ret = omap_modeset_init_properties(dev);
254 * This function creates exactly one connector, encoder, crtc,
255 * and primary plane per each connected dss-device. Each
256 * connector->encoder->crtc chain is expected to be separate
257 * and each crtc is connect to a single dss-channel. If the
258 * configuration does not match the expectations or exceeds
259 * the available resources, the configuration is rejected.
261 ret = omap_connect_pipelines(dev);
265 if (priv->num_pipes > num_mgrs || priv->num_pipes > num_ovls) {
266 dev_err(dev->dev, "%s(): Too many connected displays\n",
271 /* Create all planes first. They can all be put to any CRTC. */
272 plane_crtc_mask = (1 << priv->num_pipes) - 1;
274 for (i = 0; i < num_ovls; i++) {
275 enum drm_plane_type type = i < priv->num_pipes
276 ? DRM_PLANE_TYPE_PRIMARY
277 : DRM_PLANE_TYPE_OVERLAY;
278 struct drm_plane *plane;
280 if (WARN_ON(priv->num_planes >= ARRAY_SIZE(priv->planes)))
283 plane = omap_plane_init(dev, i, type, plane_crtc_mask);
285 return PTR_ERR(plane);
287 priv->planes[priv->num_planes++] = plane;
291 * Create the encoders, attach the bridges and get the pipeline alias
294 for (i = 0; i < priv->num_pipes; i++) {
295 struct omap_drm_pipeline *pipe = &priv->pipes[i];
298 pipe->encoder = omap_encoder_init(dev, pipe->output);
302 if (pipe->output->bridge) {
303 ret = drm_bridge_attach(pipe->encoder,
304 pipe->output->bridge, NULL);
309 id = omap_display_id(pipe->output);
310 pipe->alias_id = id >= 0 ? id : i;
313 /* Sort the pipelines by DT aliases. */
314 sort(priv->pipes, priv->num_pipes, sizeof(priv->pipes[0]),
315 omap_compare_pipelines, NULL);
318 * Populate the pipeline lookup table by DISPC channel. Only one display
319 * is allowed per channel.
321 for (i = 0; i < priv->num_pipes; ++i) {
322 struct omap_drm_pipeline *pipe = &priv->pipes[i];
323 enum omap_channel channel = pipe->output->dispc_channel;
325 if (WARN_ON(priv->channels[channel] != NULL))
328 priv->channels[channel] = pipe;
331 /* Create the connectors and CRTCs. */
332 for (i = 0; i < priv->num_pipes; i++) {
333 struct omap_drm_pipeline *pipe = &priv->pipes[i];
334 struct drm_encoder *encoder = pipe->encoder;
335 struct drm_crtc *crtc;
337 if (!pipe->output->bridge) {
338 pipe->connector = omap_connector_init(dev, pipe->output,
340 if (!pipe->connector)
343 drm_connector_attach_encoder(pipe->connector, encoder);
345 if (pipe->output->panel) {
346 ret = drm_panel_attach(pipe->output->panel,
353 crtc = omap_crtc_init(dev, pipe, priv->planes[i]);
355 return PTR_ERR(crtc);
357 encoder->possible_crtcs = 1 << i;
361 DBG("registered %u planes, %u crtcs/encoders/connectors\n",
362 priv->num_planes, priv->num_pipes);
364 dev->mode_config.min_width = 8;
365 dev->mode_config.min_height = 2;
368 * Note: these values are used for multiple independent things:
369 * connector mode filtering, buffer sizes, crtc sizes...
370 * Use big enough values here to cover all use cases, and do more
371 * specific checking in the respective code paths.
373 dev->mode_config.max_width = 8192;
374 dev->mode_config.max_height = 8192;
376 /* We want the zpos to be normalized */
377 dev->mode_config.normalize_zpos = true;
379 dev->mode_config.funcs = &omap_mode_config_funcs;
380 dev->mode_config.helper_private = &omap_mode_config_helper_funcs;
382 drm_mode_config_reset(dev);
384 omap_drm_irq_install(dev);
390 * Enable the HPD in external components if supported
392 static void omap_modeset_enable_external_hpd(struct drm_device *ddev)
394 struct omap_drm_private *priv = ddev->dev_private;
397 for (i = 0; i < priv->num_pipes; i++) {
398 if (priv->pipes[i].connector)
399 omap_connector_enable_hpd(priv->pipes[i].connector);
404 * Disable the HPD in external components if supported
406 static void omap_modeset_disable_external_hpd(struct drm_device *ddev)
408 struct omap_drm_private *priv = ddev->dev_private;
411 for (i = 0; i < priv->num_pipes; i++) {
412 if (priv->pipes[i].connector)
413 omap_connector_disable_hpd(priv->pipes[i].connector);
422 static int ioctl_get_param(struct drm_device *dev, void *data,
423 struct drm_file *file_priv)
425 struct omap_drm_private *priv = dev->dev_private;
426 struct drm_omap_param *args = data;
428 DBG("%p: param=%llu", dev, args->param);
430 switch (args->param) {
431 case OMAP_PARAM_CHIPSET_ID:
432 args->value = priv->omaprev;
435 DBG("unknown parameter %lld", args->param);
442 static int ioctl_set_param(struct drm_device *dev, void *data,
443 struct drm_file *file_priv)
445 struct drm_omap_param *args = data;
447 switch (args->param) {
449 DBG("unknown parameter %lld", args->param);
456 #define OMAP_BO_USER_MASK 0x00ffffff /* flags settable by userspace */
458 static int ioctl_gem_new(struct drm_device *dev, void *data,
459 struct drm_file *file_priv)
461 struct drm_omap_gem_new *args = data;
462 u32 flags = args->flags & OMAP_BO_USER_MASK;
464 VERB("%p:%p: size=0x%08x, flags=%08x", dev, file_priv,
465 args->size.bytes, flags);
467 return omap_gem_new_handle(dev, file_priv, args->size, flags,
471 static int ioctl_gem_info(struct drm_device *dev, void *data,
472 struct drm_file *file_priv)
474 struct drm_omap_gem_info *args = data;
475 struct drm_gem_object *obj;
478 VERB("%p:%p: handle=%d", dev, file_priv, args->handle);
480 obj = drm_gem_object_lookup(file_priv, args->handle);
484 args->size = omap_gem_mmap_size(obj);
485 args->offset = omap_gem_mmap_offset(obj);
487 drm_gem_object_put_unlocked(obj);
492 static const struct drm_ioctl_desc ioctls[DRM_COMMAND_END - DRM_COMMAND_BASE] = {
493 DRM_IOCTL_DEF_DRV(OMAP_GET_PARAM, ioctl_get_param,
494 DRM_AUTH | DRM_RENDER_ALLOW),
495 DRM_IOCTL_DEF_DRV(OMAP_SET_PARAM, ioctl_set_param,
496 DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY),
497 DRM_IOCTL_DEF_DRV(OMAP_GEM_NEW, ioctl_gem_new,
498 DRM_AUTH | DRM_RENDER_ALLOW),
499 /* Deprecated, to be removed. */
500 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_PREP, drm_noop,
501 DRM_AUTH | DRM_RENDER_ALLOW),
502 /* Deprecated, to be removed. */
503 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_FINI, drm_noop,
504 DRM_AUTH | DRM_RENDER_ALLOW),
505 DRM_IOCTL_DEF_DRV(OMAP_GEM_INFO, ioctl_gem_info,
506 DRM_AUTH | DRM_RENDER_ALLOW),
513 static int dev_open(struct drm_device *dev, struct drm_file *file)
515 file->driver_priv = NULL;
517 DBG("open: dev=%p, file=%p", dev, file);
522 static const struct vm_operations_struct omap_gem_vm_ops = {
523 .fault = omap_gem_fault,
524 .open = drm_gem_vm_open,
525 .close = drm_gem_vm_close,
528 static const struct file_operations omapdriver_fops = {
529 .owner = THIS_MODULE,
531 .unlocked_ioctl = drm_ioctl,
532 .compat_ioctl = drm_compat_ioctl,
533 .release = drm_release,
534 .mmap = omap_gem_mmap,
537 .llseek = noop_llseek,
540 static struct drm_driver omap_drm_driver = {
541 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
542 DRIVER_ATOMIC | DRIVER_RENDER,
544 .lastclose = drm_fb_helper_lastclose,
545 #ifdef CONFIG_DEBUG_FS
546 .debugfs_init = omap_debugfs_init,
548 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
549 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
550 .gem_prime_export = omap_gem_prime_export,
551 .gem_prime_import = omap_gem_prime_import,
552 .gem_free_object_unlocked = omap_gem_free_object,
553 .gem_vm_ops = &omap_gem_vm_ops,
554 .dumb_create = omap_gem_dumb_create,
555 .dumb_map_offset = omap_gem_dumb_map_offset,
557 .num_ioctls = DRM_OMAP_NUM_IOCTLS,
558 .fops = &omapdriver_fops,
562 .major = DRIVER_MAJOR,
563 .minor = DRIVER_MINOR,
564 .patchlevel = DRIVER_PATCHLEVEL,
567 static const struct soc_device_attribute omapdrm_soc_devices[] = {
568 { .family = "OMAP3", .data = (void *)0x3430 },
569 { .family = "OMAP4", .data = (void *)0x4430 },
570 { .family = "OMAP5", .data = (void *)0x5430 },
571 { .family = "DRA7", .data = (void *)0x0752 },
575 static int omapdrm_init(struct omap_drm_private *priv, struct device *dev)
577 const struct soc_device_attribute *soc;
578 struct drm_device *ddev;
582 DBG("%s", dev_name(dev));
584 /* Allocate and initialize the DRM device. */
585 ddev = drm_dev_alloc(&omap_drm_driver, dev);
587 return PTR_ERR(ddev);
590 ddev->dev_private = priv;
593 priv->dss = omapdss_get_dss();
594 priv->dispc = dispc_get_dispc(priv->dss);
595 priv->dispc_ops = dispc_get_ops(priv->dss);
597 omap_crtc_pre_init(priv);
599 soc = soc_device_match(omapdrm_soc_devices);
600 priv->omaprev = soc ? (unsigned int)soc->data : 0;
601 priv->wq = alloc_ordered_workqueue("omapdrm", 0);
603 mutex_init(&priv->list_lock);
604 INIT_LIST_HEAD(&priv->obj_list);
606 /* Get memory bandwidth limits */
607 if (priv->dispc_ops->get_memory_bandwidth_limit)
608 priv->max_bandwidth =
609 priv->dispc_ops->get_memory_bandwidth_limit(priv->dispc);
613 ret = omap_modeset_init(ddev);
615 dev_err(priv->dev, "omap_modeset_init failed: ret=%d\n", ret);
619 /* Initialize vblank handling, start with all CRTCs disabled. */
620 ret = drm_vblank_init(ddev, priv->num_pipes);
622 dev_err(priv->dev, "could not init vblank\n");
623 goto err_cleanup_modeset;
626 for (i = 0; i < priv->num_pipes; i++)
627 drm_crtc_vblank_off(priv->pipes[i].crtc);
629 omap_fbdev_init(ddev);
631 drm_kms_helper_poll_init(ddev);
632 omap_modeset_enable_external_hpd(ddev);
635 * Register the DRM device with the core and the connectors with
638 ret = drm_dev_register(ddev, 0);
640 goto err_cleanup_helpers;
645 omap_modeset_disable_external_hpd(ddev);
646 drm_kms_helper_poll_fini(ddev);
648 omap_fbdev_fini(ddev);
650 drm_mode_config_cleanup(ddev);
651 omap_drm_irq_uninstall(ddev);
653 omap_gem_deinit(ddev);
654 destroy_workqueue(priv->wq);
655 omap_disconnect_pipelines(ddev);
656 omap_crtc_pre_uninit(priv);
661 static void omapdrm_cleanup(struct omap_drm_private *priv)
663 struct drm_device *ddev = priv->ddev;
667 drm_dev_unregister(ddev);
669 omap_modeset_disable_external_hpd(ddev);
670 drm_kms_helper_poll_fini(ddev);
672 omap_fbdev_fini(ddev);
674 drm_atomic_helper_shutdown(ddev);
676 drm_mode_config_cleanup(ddev);
678 omap_drm_irq_uninstall(ddev);
679 omap_gem_deinit(ddev);
681 destroy_workqueue(priv->wq);
683 omap_disconnect_pipelines(ddev);
684 omap_crtc_pre_uninit(priv);
689 static int pdev_probe(struct platform_device *pdev)
691 struct omap_drm_private *priv;
694 if (omapdss_is_initialized() == false)
695 return -EPROBE_DEFER;
697 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
699 dev_err(&pdev->dev, "Failed to set the DMA mask\n");
703 /* Allocate and initialize the driver private structure. */
704 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
708 platform_set_drvdata(pdev, priv);
710 ret = omapdrm_init(priv, &pdev->dev);
717 static int pdev_remove(struct platform_device *pdev)
719 struct omap_drm_private *priv = platform_get_drvdata(pdev);
721 omapdrm_cleanup(priv);
727 #ifdef CONFIG_PM_SLEEP
728 static int omap_drm_suspend(struct device *dev)
730 struct omap_drm_private *priv = dev_get_drvdata(dev);
731 struct drm_device *drm_dev = priv->ddev;
733 return drm_mode_config_helper_suspend(drm_dev);
736 static int omap_drm_resume(struct device *dev)
738 struct omap_drm_private *priv = dev_get_drvdata(dev);
739 struct drm_device *drm_dev = priv->ddev;
741 drm_mode_config_helper_resume(drm_dev);
743 return omap_gem_resume(drm_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");