2 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
3 * Author: Rob Clark <rob@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>
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_atomic_wait_for_completion(struct drm_device *dev,
50 struct drm_atomic_state *old_state)
52 struct drm_crtc_state *new_crtc_state;
53 struct drm_crtc *crtc;
57 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
58 if (!new_crtc_state->active)
61 ret = omap_crtc_wait_pending(crtc);
65 "atomic complete timeout (pipe %u)!\n", i);
69 static void omap_atomic_commit_tail(struct drm_atomic_state *old_state)
71 struct drm_device *dev = old_state->dev;
72 struct omap_drm_private *priv = dev->dev_private;
74 priv->dispc_ops->runtime_get(priv->dispc);
76 /* Apply the atomic update. */
77 drm_atomic_helper_commit_modeset_disables(dev, old_state);
79 if (priv->omaprev != 0x3430) {
80 /* With the current dss dispc implementation we have to enable
81 * the new modeset before we can commit planes. The dispc ovl
82 * configuration relies on the video mode configuration been
83 * written into the HW when the ovl configuration is
86 * This approach is not ideal because after a mode change the
87 * plane update is executed only after the first vblank
88 * interrupt. The dispc implementation should be fixed so that
89 * it is able use uncommitted drm state information.
91 drm_atomic_helper_commit_modeset_enables(dev, old_state);
92 omap_atomic_wait_for_completion(dev, old_state);
94 drm_atomic_helper_commit_planes(dev, old_state, 0);
96 drm_atomic_helper_commit_hw_done(old_state);
99 * OMAP3 DSS seems to have issues with the work-around above,
100 * resulting in endless sync losts if a crtc is enabled without
101 * a plane. For now, skip the WA for OMAP3.
103 drm_atomic_helper_commit_planes(dev, old_state, 0);
105 drm_atomic_helper_commit_modeset_enables(dev, old_state);
107 drm_atomic_helper_commit_hw_done(old_state);
111 * Wait for completion of the page flips to ensure that old buffers
112 * can't be touched by the hardware anymore before cleaning up planes.
114 omap_atomic_wait_for_completion(dev, old_state);
116 drm_atomic_helper_cleanup_planes(dev, old_state);
118 priv->dispc_ops->runtime_put(priv->dispc);
121 static const struct drm_mode_config_helper_funcs omap_mode_config_helper_funcs = {
122 .atomic_commit_tail = omap_atomic_commit_tail,
125 static const struct drm_mode_config_funcs omap_mode_config_funcs = {
126 .fb_create = omap_framebuffer_create,
127 .output_poll_changed = drm_fb_helper_output_poll_changed,
128 .atomic_check = drm_atomic_helper_check,
129 .atomic_commit = drm_atomic_helper_commit,
132 static void omap_disconnect_pipelines(struct drm_device *ddev)
134 struct omap_drm_private *priv = ddev->dev_private;
137 for (i = 0; i < priv->num_pipes; i++) {
138 struct omap_drm_pipeline *pipe = &priv->pipes[i];
140 omapdss_device_disconnect(NULL, pipe->output);
142 omapdss_device_put(pipe->output);
143 omapdss_device_put(pipe->display);
145 pipe->display = NULL;
148 memset(&priv->channels, 0, sizeof(priv->channels));
153 static int omap_compare_pipes(const void *a, const void *b)
155 const struct omap_drm_pipeline *pipe1 = a;
156 const struct omap_drm_pipeline *pipe2 = b;
158 if (pipe1->alias_id > pipe2->alias_id)
160 else if (pipe1->alias_id < pipe2->alias_id)
165 static int omap_connect_pipelines(struct drm_device *ddev)
167 struct omap_drm_private *priv = ddev->dev_private;
168 struct omap_dss_device *output = NULL;
172 if (!omapdss_stack_is_ready())
173 return -EPROBE_DEFER;
175 for_each_dss_output(output) {
176 r = omapdss_device_connect(priv->dss, NULL, output);
177 if (r == -EPROBE_DEFER) {
178 omapdss_device_put(output);
181 dev_warn(output->dev, "could not connect output %s\n",
184 struct omap_drm_pipeline *pipe;
187 pipe = &priv->pipes[priv->num_pipes++];
188 pipe->output = omapdss_device_get(output);
189 pipe->display = omapdss_display_get(output);
191 id = of_alias_get_id(pipe->display->dev->of_node,
193 pipe->alias_id = id >= 0 ? id : priv->num_pipes - 1;
195 if (priv->num_pipes == ARRAY_SIZE(priv->pipes)) {
196 /* To balance the 'for_each_dss_output' loop */
197 omapdss_device_put(output);
203 /* Sort the list by DT aliases */
204 sort(priv->pipes, priv->num_pipes, sizeof(priv->pipes[0]),
205 omap_compare_pipes, NULL);
208 * Populate the pipeline lookup table by DISPC channel. Only one display
209 * is allowed per channel.
211 for (i = 0; i < priv->num_pipes; ++i) {
212 struct omap_drm_pipeline *pipe = &priv->pipes[i];
213 enum omap_channel channel = pipe->output->dispc_channel;
215 if (WARN_ON(priv->channels[channel] != NULL)) {
220 priv->channels[channel] = pipe;
227 * if we are deferring probe, we disconnect the devices we previously
230 omap_disconnect_pipelines(ddev);
235 static int omap_modeset_init_properties(struct drm_device *dev)
237 struct omap_drm_private *priv = dev->dev_private;
238 unsigned int num_planes = priv->dispc_ops->get_num_ovls(priv->dispc);
240 priv->zorder_prop = drm_property_create_range(dev, 0, "zorder", 0,
242 if (!priv->zorder_prop)
248 static int omap_modeset_init(struct drm_device *dev)
250 struct omap_drm_private *priv = dev->dev_private;
251 int num_ovls = priv->dispc_ops->get_num_ovls(priv->dispc);
252 int num_mgrs = priv->dispc_ops->get_num_mgrs(priv->dispc);
257 drm_mode_config_init(dev);
259 ret = omap_modeset_init_properties(dev);
264 * This function creates exactly one connector, encoder, crtc,
265 * and primary plane per each connected dss-device. Each
266 * connector->encoder->crtc chain is expected to be separate
267 * and each crtc is connect to a single dss-channel. If the
268 * configuration does not match the expectations or exceeds
269 * the available resources, the configuration is rejected.
271 if (priv->num_pipes > num_mgrs || priv->num_pipes > num_ovls) {
272 dev_err(dev->dev, "%s(): Too many connected displays\n",
277 /* Create all planes first. They can all be put to any CRTC. */
278 plane_crtc_mask = (1 << priv->num_pipes) - 1;
280 for (i = 0; i < num_ovls; i++) {
281 enum drm_plane_type type = i < priv->num_pipes
282 ? DRM_PLANE_TYPE_PRIMARY
283 : DRM_PLANE_TYPE_OVERLAY;
284 struct drm_plane *plane;
286 if (WARN_ON(priv->num_planes >= ARRAY_SIZE(priv->planes)))
289 plane = omap_plane_init(dev, i, type, plane_crtc_mask);
291 return PTR_ERR(plane);
293 priv->planes[priv->num_planes++] = plane;
296 /* Create the CRTCs, encoders and connectors. */
297 for (i = 0; i < priv->num_pipes; i++) {
298 struct omap_drm_pipeline *pipe = &priv->pipes[i];
299 struct omap_dss_device *display = pipe->display;
300 struct drm_connector *connector;
301 struct drm_encoder *encoder;
302 struct drm_crtc *crtc;
304 encoder = omap_encoder_init(dev, pipe->output);
308 connector = omap_connector_init(dev, pipe->output, display,
313 crtc = omap_crtc_init(dev, pipe, priv->planes[i]);
315 return PTR_ERR(crtc);
317 drm_connector_attach_encoder(connector, encoder);
318 encoder->possible_crtcs = 1 << i;
321 pipe->encoder = encoder;
322 pipe->connector = connector;
325 DBG("registered %u planes, %u crtcs/encoders/connectors\n",
326 priv->num_planes, priv->num_pipes);
328 dev->mode_config.min_width = 8;
329 dev->mode_config.min_height = 2;
332 * Note: these values are used for multiple independent things:
333 * connector mode filtering, buffer sizes, crtc sizes...
334 * Use big enough values here to cover all use cases, and do more
335 * specific checking in the respective code paths.
337 dev->mode_config.max_width = 8192;
338 dev->mode_config.max_height = 8192;
340 /* We want the zpos to be normalized */
341 dev->mode_config.normalize_zpos = true;
343 dev->mode_config.funcs = &omap_mode_config_funcs;
344 dev->mode_config.helper_private = &omap_mode_config_helper_funcs;
346 drm_mode_config_reset(dev);
348 omap_drm_irq_install(dev);
354 * Enable the HPD in external components if supported
356 static void omap_modeset_enable_external_hpd(struct drm_device *ddev)
358 struct omap_drm_private *priv = ddev->dev_private;
361 for (i = 0; i < priv->num_pipes; i++)
362 omap_connector_enable_hpd(priv->pipes[i].connector);
366 * Disable the HPD in external components if supported
368 static void omap_modeset_disable_external_hpd(struct drm_device *ddev)
370 struct omap_drm_private *priv = ddev->dev_private;
373 for (i = 0; i < priv->num_pipes; i++)
374 omap_connector_disable_hpd(priv->pipes[i].connector);
382 static int ioctl_get_param(struct drm_device *dev, void *data,
383 struct drm_file *file_priv)
385 struct omap_drm_private *priv = dev->dev_private;
386 struct drm_omap_param *args = data;
388 DBG("%p: param=%llu", dev, args->param);
390 switch (args->param) {
391 case OMAP_PARAM_CHIPSET_ID:
392 args->value = priv->omaprev;
395 DBG("unknown parameter %lld", args->param);
402 static int ioctl_set_param(struct drm_device *dev, void *data,
403 struct drm_file *file_priv)
405 struct drm_omap_param *args = data;
407 switch (args->param) {
409 DBG("unknown parameter %lld", args->param);
416 #define OMAP_BO_USER_MASK 0x00ffffff /* flags settable by userspace */
418 static int ioctl_gem_new(struct drm_device *dev, void *data,
419 struct drm_file *file_priv)
421 struct drm_omap_gem_new *args = data;
422 u32 flags = args->flags & OMAP_BO_USER_MASK;
424 VERB("%p:%p: size=0x%08x, flags=%08x", dev, file_priv,
425 args->size.bytes, flags);
427 return omap_gem_new_handle(dev, file_priv, args->size, flags,
431 static int ioctl_gem_info(struct drm_device *dev, void *data,
432 struct drm_file *file_priv)
434 struct drm_omap_gem_info *args = data;
435 struct drm_gem_object *obj;
438 VERB("%p:%p: handle=%d", dev, file_priv, args->handle);
440 obj = drm_gem_object_lookup(file_priv, args->handle);
444 args->size = omap_gem_mmap_size(obj);
445 args->offset = omap_gem_mmap_offset(obj);
447 drm_gem_object_put_unlocked(obj);
452 static const struct drm_ioctl_desc ioctls[DRM_COMMAND_END - DRM_COMMAND_BASE] = {
453 DRM_IOCTL_DEF_DRV(OMAP_GET_PARAM, ioctl_get_param,
454 DRM_AUTH | DRM_RENDER_ALLOW),
455 DRM_IOCTL_DEF_DRV(OMAP_SET_PARAM, ioctl_set_param,
456 DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY),
457 DRM_IOCTL_DEF_DRV(OMAP_GEM_NEW, ioctl_gem_new,
458 DRM_AUTH | DRM_RENDER_ALLOW),
459 /* Deprecated, to be removed. */
460 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_PREP, drm_noop,
461 DRM_AUTH | DRM_RENDER_ALLOW),
462 /* Deprecated, to be removed. */
463 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_FINI, drm_noop,
464 DRM_AUTH | DRM_RENDER_ALLOW),
465 DRM_IOCTL_DEF_DRV(OMAP_GEM_INFO, ioctl_gem_info,
466 DRM_AUTH | DRM_RENDER_ALLOW),
473 static int dev_open(struct drm_device *dev, struct drm_file *file)
475 file->driver_priv = NULL;
477 DBG("open: dev=%p, file=%p", dev, file);
482 static const struct vm_operations_struct omap_gem_vm_ops = {
483 .fault = omap_gem_fault,
484 .open = drm_gem_vm_open,
485 .close = drm_gem_vm_close,
488 static const struct file_operations omapdriver_fops = {
489 .owner = THIS_MODULE,
491 .unlocked_ioctl = drm_ioctl,
492 .compat_ioctl = drm_compat_ioctl,
493 .release = drm_release,
494 .mmap = omap_gem_mmap,
497 .llseek = noop_llseek,
500 static struct drm_driver omap_drm_driver = {
501 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
502 DRIVER_ATOMIC | DRIVER_RENDER,
504 .lastclose = drm_fb_helper_lastclose,
505 #ifdef CONFIG_DEBUG_FS
506 .debugfs_init = omap_debugfs_init,
508 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
509 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
510 .gem_prime_export = omap_gem_prime_export,
511 .gem_prime_import = omap_gem_prime_import,
512 .gem_free_object_unlocked = omap_gem_free_object,
513 .gem_vm_ops = &omap_gem_vm_ops,
514 .dumb_create = omap_gem_dumb_create,
515 .dumb_map_offset = omap_gem_dumb_map_offset,
517 .num_ioctls = DRM_OMAP_NUM_IOCTLS,
518 .fops = &omapdriver_fops,
522 .major = DRIVER_MAJOR,
523 .minor = DRIVER_MINOR,
524 .patchlevel = DRIVER_PATCHLEVEL,
527 static const struct soc_device_attribute omapdrm_soc_devices[] = {
528 { .family = "OMAP3", .data = (void *)0x3430 },
529 { .family = "OMAP4", .data = (void *)0x4430 },
530 { .family = "OMAP5", .data = (void *)0x5430 },
531 { .family = "DRA7", .data = (void *)0x0752 },
535 static int omapdrm_init(struct omap_drm_private *priv, struct device *dev)
537 const struct soc_device_attribute *soc;
538 struct drm_device *ddev;
542 DBG("%s", dev_name(dev));
544 /* Allocate and initialize the DRM device. */
545 ddev = drm_dev_alloc(&omap_drm_driver, dev);
547 return PTR_ERR(ddev);
550 ddev->dev_private = priv;
553 priv->dss = omapdss_get_dss();
554 priv->dispc = dispc_get_dispc(priv->dss);
555 priv->dispc_ops = dispc_get_ops(priv->dss);
557 omap_crtc_pre_init(priv);
559 ret = omap_connect_pipelines(ddev);
561 goto err_crtc_uninit;
563 soc = soc_device_match(omapdrm_soc_devices);
564 priv->omaprev = soc ? (unsigned int)soc->data : 0;
565 priv->wq = alloc_ordered_workqueue("omapdrm", 0);
567 mutex_init(&priv->list_lock);
568 INIT_LIST_HEAD(&priv->obj_list);
570 /* Get memory bandwidth limits */
571 if (priv->dispc_ops->get_memory_bandwidth_limit)
572 priv->max_bandwidth =
573 priv->dispc_ops->get_memory_bandwidth_limit(priv->dispc);
577 ret = omap_modeset_init(ddev);
579 dev_err(priv->dev, "omap_modeset_init failed: ret=%d\n", ret);
583 /* Initialize vblank handling, start with all CRTCs disabled. */
584 ret = drm_vblank_init(ddev, priv->num_pipes);
586 dev_err(priv->dev, "could not init vblank\n");
587 goto err_cleanup_modeset;
590 for (i = 0; i < priv->num_pipes; i++)
591 drm_crtc_vblank_off(priv->pipes[i].crtc);
593 omap_fbdev_init(ddev);
595 drm_kms_helper_poll_init(ddev);
596 omap_modeset_enable_external_hpd(ddev);
599 * Register the DRM device with the core and the connectors with
602 ret = drm_dev_register(ddev, 0);
604 goto err_cleanup_helpers;
609 omap_modeset_disable_external_hpd(ddev);
610 drm_kms_helper_poll_fini(ddev);
612 omap_fbdev_fini(ddev);
614 drm_mode_config_cleanup(ddev);
615 omap_drm_irq_uninstall(ddev);
617 omap_gem_deinit(ddev);
618 destroy_workqueue(priv->wq);
619 omap_disconnect_pipelines(ddev);
621 omap_crtc_pre_uninit(priv);
626 static void omapdrm_cleanup(struct omap_drm_private *priv)
628 struct drm_device *ddev = priv->ddev;
632 drm_dev_unregister(ddev);
634 omap_modeset_disable_external_hpd(ddev);
635 drm_kms_helper_poll_fini(ddev);
637 omap_fbdev_fini(ddev);
639 drm_atomic_helper_shutdown(ddev);
641 drm_mode_config_cleanup(ddev);
643 omap_drm_irq_uninstall(ddev);
644 omap_gem_deinit(ddev);
646 destroy_workqueue(priv->wq);
648 omap_disconnect_pipelines(ddev);
649 omap_crtc_pre_uninit(priv);
654 static int pdev_probe(struct platform_device *pdev)
656 struct omap_drm_private *priv;
659 if (omapdss_is_initialized() == false)
660 return -EPROBE_DEFER;
662 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
664 dev_err(&pdev->dev, "Failed to set the DMA mask\n");
668 /* Allocate and initialize the driver private structure. */
669 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
673 platform_set_drvdata(pdev, priv);
675 ret = omapdrm_init(priv, &pdev->dev);
682 static int pdev_remove(struct platform_device *pdev)
684 struct omap_drm_private *priv = platform_get_drvdata(pdev);
686 omapdrm_cleanup(priv);
692 #ifdef CONFIG_PM_SLEEP
693 static int omap_drm_suspend(struct device *dev)
695 struct omap_drm_private *priv = dev_get_drvdata(dev);
696 struct drm_device *drm_dev = priv->ddev;
698 return drm_mode_config_helper_suspend(drm_dev);
701 static int omap_drm_resume(struct device *dev)
703 struct omap_drm_private *priv = dev_get_drvdata(dev);
704 struct drm_device *drm_dev = priv->ddev;
706 drm_mode_config_helper_resume(drm_dev);
708 return omap_gem_resume(drm_dev);
712 static SIMPLE_DEV_PM_OPS(omapdrm_pm_ops, omap_drm_suspend, omap_drm_resume);
714 static struct platform_driver pdev = {
717 .pm = &omapdrm_pm_ops,
720 .remove = pdev_remove,
723 static struct platform_driver * const drivers[] = {
728 static int __init omap_drm_init(void)
732 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
735 static void __exit omap_drm_fini(void)
739 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
742 /* need late_initcall() so we load after dss_driver's are loaded */
743 late_initcall(omap_drm_init);
744 module_exit(omap_drm_fini);
746 MODULE_AUTHOR("Rob Clark <rob@ti.com>");
747 MODULE_DESCRIPTION("OMAP DRM Display Driver");
748 MODULE_ALIAS("platform:" DRIVER_NAME);
749 MODULE_LICENSE("GPL v2");