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/>.
18 #include <linux/dma-mapping.h>
19 #include <linux/platform_device.h>
20 #include <linux/sort.h>
21 #include <linux/sys_soc.h>
23 #include <drm/drm_atomic.h>
24 #include <drm/drm_atomic_helper.h>
25 #include <drm/drm_drv.h>
26 #include <drm/drm_fb_helper.h>
27 #include <drm/drm_file.h>
28 #include <drm/drm_ioctl.h>
29 #include <drm/drm_panel.h>
30 #include <drm/drm_prime.h>
31 #include <drm/drm_probe_helper.h>
32 #include <drm/drm_vblank.h>
34 #include "omap_dmm_tiler.h"
37 #define DRIVER_NAME MODULE_NAME
38 #define DRIVER_DESC "OMAP DRM"
39 #define DRIVER_DATE "20110917"
40 #define DRIVER_MAJOR 1
41 #define DRIVER_MINOR 0
42 #define DRIVER_PATCHLEVEL 0
48 /* Notes about mapping DSS and DRM entities:
50 * encoder: manager.. with some extension to allow one primary CRTC
51 * and zero or more video CRTC's to be mapped to one encoder?
52 * connector: dssdev.. manager can be attached/detached from different
56 static void omap_atomic_wait_for_completion(struct drm_device *dev,
57 struct drm_atomic_state *old_state)
59 struct drm_crtc_state *new_crtc_state;
60 struct drm_crtc *crtc;
64 for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
65 if (!new_crtc_state->active)
68 ret = omap_crtc_wait_pending(crtc);
72 "atomic complete timeout (pipe %u)!\n", i);
76 static void omap_atomic_commit_tail(struct drm_atomic_state *old_state)
78 struct drm_device *dev = old_state->dev;
79 struct omap_drm_private *priv = dev->dev_private;
81 priv->dispc_ops->runtime_get(priv->dispc);
83 /* Apply the atomic update. */
84 drm_atomic_helper_commit_modeset_disables(dev, old_state);
86 if (priv->omaprev != 0x3430) {
87 /* With the current dss dispc implementation we have to enable
88 * the new modeset before we can commit planes. The dispc ovl
89 * configuration relies on the video mode configuration been
90 * written into the HW when the ovl configuration is
93 * This approach is not ideal because after a mode change the
94 * plane update is executed only after the first vblank
95 * interrupt. The dispc implementation should be fixed so that
96 * it is able use uncommitted drm state information.
98 drm_atomic_helper_commit_modeset_enables(dev, old_state);
99 omap_atomic_wait_for_completion(dev, old_state);
101 drm_atomic_helper_commit_planes(dev, old_state, 0);
103 drm_atomic_helper_commit_hw_done(old_state);
106 * OMAP3 DSS seems to have issues with the work-around above,
107 * resulting in endless sync losts if a crtc is enabled without
108 * a plane. For now, skip the WA for OMAP3.
110 drm_atomic_helper_commit_planes(dev, old_state, 0);
112 drm_atomic_helper_commit_modeset_enables(dev, old_state);
114 drm_atomic_helper_commit_hw_done(old_state);
118 * Wait for completion of the page flips to ensure that old buffers
119 * can't be touched by the hardware anymore before cleaning up planes.
121 omap_atomic_wait_for_completion(dev, old_state);
123 drm_atomic_helper_cleanup_planes(dev, old_state);
125 priv->dispc_ops->runtime_put(priv->dispc);
128 static const struct drm_mode_config_helper_funcs omap_mode_config_helper_funcs = {
129 .atomic_commit_tail = omap_atomic_commit_tail,
132 static const struct drm_mode_config_funcs omap_mode_config_funcs = {
133 .fb_create = omap_framebuffer_create,
134 .output_poll_changed = drm_fb_helper_output_poll_changed,
135 .atomic_check = drm_atomic_helper_check,
136 .atomic_commit = drm_atomic_helper_commit,
139 static void omap_disconnect_pipelines(struct drm_device *ddev)
141 struct omap_drm_private *priv = ddev->dev_private;
144 for (i = 0; i < priv->num_pipes; i++) {
145 struct omap_drm_pipeline *pipe = &priv->pipes[i];
147 if (pipe->output->panel)
148 drm_panel_detach(pipe->output->panel);
150 omapdss_device_disconnect(NULL, pipe->output);
152 omapdss_device_put(pipe->output);
156 memset(&priv->channels, 0, sizeof(priv->channels));
161 static int omap_connect_pipelines(struct drm_device *ddev)
163 struct omap_drm_private *priv = ddev->dev_private;
164 struct omap_dss_device *output = NULL;
167 for_each_dss_output(output) {
168 r = omapdss_device_connect(priv->dss, NULL, output);
169 if (r == -EPROBE_DEFER) {
170 omapdss_device_put(output);
173 dev_warn(output->dev, "could not connect output %s\n",
176 struct omap_drm_pipeline *pipe;
178 pipe = &priv->pipes[priv->num_pipes++];
179 pipe->output = omapdss_device_get(output);
181 if (priv->num_pipes == ARRAY_SIZE(priv->pipes)) {
182 /* To balance the 'for_each_dss_output' loop */
183 omapdss_device_put(output);
192 static int omap_compare_pipelines(const void *a, const void *b)
194 const struct omap_drm_pipeline *pipe1 = a;
195 const struct omap_drm_pipeline *pipe2 = b;
197 if (pipe1->alias_id > pipe2->alias_id)
199 else if (pipe1->alias_id < pipe2->alias_id)
204 static int omap_modeset_init_properties(struct drm_device *dev)
206 struct omap_drm_private *priv = dev->dev_private;
207 unsigned int num_planes = priv->dispc_ops->get_num_ovls(priv->dispc);
209 priv->zorder_prop = drm_property_create_range(dev, 0, "zorder", 0,
211 if (!priv->zorder_prop)
217 static int omap_display_id(struct omap_dss_device *output)
219 struct device_node *node = NULL;
222 struct omap_dss_device *display;
224 display = omapdss_display_get(output);
225 node = display->dev->of_node;
226 omapdss_device_put(display);
227 } else if (output->bridge) {
228 struct drm_bridge *bridge = output->bridge;
231 bridge = bridge->next;
233 node = bridge->of_node;
234 } else if (output->panel) {
235 node = output->panel->dev->of_node;
238 return node ? of_alias_get_id(node, "display") : -ENODEV;
241 static int omap_modeset_init(struct drm_device *dev)
243 struct omap_drm_private *priv = dev->dev_private;
244 int num_ovls = priv->dispc_ops->get_num_ovls(priv->dispc);
245 int num_mgrs = priv->dispc_ops->get_num_mgrs(priv->dispc);
250 if (!omapdss_stack_is_ready())
251 return -EPROBE_DEFER;
253 drm_mode_config_init(dev);
255 ret = omap_modeset_init_properties(dev);
260 * This function creates exactly one connector, encoder, crtc,
261 * and primary plane per each connected dss-device. Each
262 * connector->encoder->crtc chain is expected to be separate
263 * and each crtc is connect to a single dss-channel. If the
264 * configuration does not match the expectations or exceeds
265 * the available resources, the configuration is rejected.
267 ret = omap_connect_pipelines(dev);
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;
297 * Create the encoders, attach the bridges and get the pipeline alias
300 for (i = 0; i < priv->num_pipes; i++) {
301 struct omap_drm_pipeline *pipe = &priv->pipes[i];
304 pipe->encoder = omap_encoder_init(dev, pipe->output);
308 if (pipe->output->bridge) {
309 ret = drm_bridge_attach(pipe->encoder,
310 pipe->output->bridge, NULL);
315 id = omap_display_id(pipe->output);
316 pipe->alias_id = id >= 0 ? id : i;
319 /* Sort the pipelines by DT aliases. */
320 sort(priv->pipes, priv->num_pipes, sizeof(priv->pipes[0]),
321 omap_compare_pipelines, NULL);
324 * Populate the pipeline lookup table by DISPC channel. Only one display
325 * is allowed per channel.
327 for (i = 0; i < priv->num_pipes; ++i) {
328 struct omap_drm_pipeline *pipe = &priv->pipes[i];
329 enum omap_channel channel = pipe->output->dispc_channel;
331 if (WARN_ON(priv->channels[channel] != NULL))
334 priv->channels[channel] = pipe;
337 /* Create the connectors and CRTCs. */
338 for (i = 0; i < priv->num_pipes; i++) {
339 struct omap_drm_pipeline *pipe = &priv->pipes[i];
340 struct drm_encoder *encoder = pipe->encoder;
341 struct drm_crtc *crtc;
343 if (!pipe->output->bridge) {
344 pipe->connector = omap_connector_init(dev, pipe->output,
346 if (!pipe->connector)
349 drm_connector_attach_encoder(pipe->connector, encoder);
351 if (pipe->output->panel) {
352 ret = drm_panel_attach(pipe->output->panel,
359 crtc = omap_crtc_init(dev, pipe, priv->planes[i]);
361 return PTR_ERR(crtc);
363 encoder->possible_crtcs = 1 << i;
367 DBG("registered %u planes, %u crtcs/encoders/connectors\n",
368 priv->num_planes, priv->num_pipes);
370 dev->mode_config.min_width = 8;
371 dev->mode_config.min_height = 2;
374 * Note: these values are used for multiple independent things:
375 * connector mode filtering, buffer sizes, crtc sizes...
376 * Use big enough values here to cover all use cases, and do more
377 * specific checking in the respective code paths.
379 dev->mode_config.max_width = 8192;
380 dev->mode_config.max_height = 8192;
382 /* We want the zpos to be normalized */
383 dev->mode_config.normalize_zpos = true;
385 dev->mode_config.funcs = &omap_mode_config_funcs;
386 dev->mode_config.helper_private = &omap_mode_config_helper_funcs;
388 drm_mode_config_reset(dev);
390 omap_drm_irq_install(dev);
396 * Enable the HPD in external components if supported
398 static void omap_modeset_enable_external_hpd(struct drm_device *ddev)
400 struct omap_drm_private *priv = ddev->dev_private;
403 for (i = 0; i < priv->num_pipes; i++) {
404 if (priv->pipes[i].connector)
405 omap_connector_enable_hpd(priv->pipes[i].connector);
410 * Disable the HPD in external components if supported
412 static void omap_modeset_disable_external_hpd(struct drm_device *ddev)
414 struct omap_drm_private *priv = ddev->dev_private;
417 for (i = 0; i < priv->num_pipes; i++) {
418 if (priv->pipes[i].connector)
419 omap_connector_disable_hpd(priv->pipes[i].connector);
428 static int ioctl_get_param(struct drm_device *dev, void *data,
429 struct drm_file *file_priv)
431 struct omap_drm_private *priv = dev->dev_private;
432 struct drm_omap_param *args = data;
434 DBG("%p: param=%llu", dev, args->param);
436 switch (args->param) {
437 case OMAP_PARAM_CHIPSET_ID:
438 args->value = priv->omaprev;
441 DBG("unknown parameter %lld", args->param);
448 #define OMAP_BO_USER_MASK 0x00ffffff /* flags settable by userspace */
450 static int ioctl_gem_new(struct drm_device *dev, void *data,
451 struct drm_file *file_priv)
453 struct drm_omap_gem_new *args = data;
454 u32 flags = args->flags & OMAP_BO_USER_MASK;
456 VERB("%p:%p: size=0x%08x, flags=%08x", dev, file_priv,
457 args->size.bytes, flags);
459 return omap_gem_new_handle(dev, file_priv, args->size, flags,
463 static int ioctl_gem_info(struct drm_device *dev, void *data,
464 struct drm_file *file_priv)
466 struct drm_omap_gem_info *args = data;
467 struct drm_gem_object *obj;
470 VERB("%p:%p: handle=%d", dev, file_priv, args->handle);
472 obj = drm_gem_object_lookup(file_priv, args->handle);
476 args->size = omap_gem_mmap_size(obj);
477 args->offset = omap_gem_mmap_offset(obj);
479 drm_gem_object_put_unlocked(obj);
484 static const struct drm_ioctl_desc ioctls[DRM_COMMAND_END - DRM_COMMAND_BASE] = {
485 DRM_IOCTL_DEF_DRV(OMAP_GET_PARAM, ioctl_get_param,
487 DRM_IOCTL_DEF_DRV(OMAP_SET_PARAM, drm_invalid_op,
488 DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY),
489 DRM_IOCTL_DEF_DRV(OMAP_GEM_NEW, ioctl_gem_new,
491 /* Deprecated, to be removed. */
492 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_PREP, drm_noop,
494 /* Deprecated, to be removed. */
495 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_FINI, drm_noop,
497 DRM_IOCTL_DEF_DRV(OMAP_GEM_INFO, ioctl_gem_info,
505 static int dev_open(struct drm_device *dev, struct drm_file *file)
507 file->driver_priv = NULL;
509 DBG("open: dev=%p, file=%p", dev, file);
514 static const struct vm_operations_struct omap_gem_vm_ops = {
515 .fault = omap_gem_fault,
516 .open = drm_gem_vm_open,
517 .close = drm_gem_vm_close,
520 static const struct file_operations omapdriver_fops = {
521 .owner = THIS_MODULE,
523 .unlocked_ioctl = drm_ioctl,
524 .compat_ioctl = drm_compat_ioctl,
525 .release = drm_release,
526 .mmap = omap_gem_mmap,
529 .llseek = noop_llseek,
532 static struct drm_driver omap_drm_driver = {
533 .driver_features = DRIVER_MODESET | DRIVER_GEM |
534 DRIVER_ATOMIC | DRIVER_RENDER,
536 .lastclose = drm_fb_helper_lastclose,
537 #ifdef CONFIG_DEBUG_FS
538 .debugfs_init = omap_debugfs_init,
540 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
541 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
542 .gem_prime_export = omap_gem_prime_export,
543 .gem_prime_import = omap_gem_prime_import,
544 .gem_free_object_unlocked = omap_gem_free_object,
545 .gem_vm_ops = &omap_gem_vm_ops,
546 .dumb_create = omap_gem_dumb_create,
547 .dumb_map_offset = omap_gem_dumb_map_offset,
549 .num_ioctls = DRM_OMAP_NUM_IOCTLS,
550 .fops = &omapdriver_fops,
554 .major = DRIVER_MAJOR,
555 .minor = DRIVER_MINOR,
556 .patchlevel = DRIVER_PATCHLEVEL,
559 static const struct soc_device_attribute omapdrm_soc_devices[] = {
560 { .family = "OMAP3", .data = (void *)0x3430 },
561 { .family = "OMAP4", .data = (void *)0x4430 },
562 { .family = "OMAP5", .data = (void *)0x5430 },
563 { .family = "DRA7", .data = (void *)0x0752 },
567 static int omapdrm_init(struct omap_drm_private *priv, struct device *dev)
569 const struct soc_device_attribute *soc;
570 struct drm_device *ddev;
574 DBG("%s", dev_name(dev));
576 /* Allocate and initialize the DRM device. */
577 ddev = drm_dev_alloc(&omap_drm_driver, dev);
579 return PTR_ERR(ddev);
582 ddev->dev_private = priv;
585 priv->dss = omapdss_get_dss();
586 priv->dispc = dispc_get_dispc(priv->dss);
587 priv->dispc_ops = dispc_get_ops(priv->dss);
589 omap_crtc_pre_init(priv);
591 soc = soc_device_match(omapdrm_soc_devices);
592 priv->omaprev = soc ? (unsigned int)soc->data : 0;
593 priv->wq = alloc_ordered_workqueue("omapdrm", 0);
595 mutex_init(&priv->list_lock);
596 INIT_LIST_HEAD(&priv->obj_list);
598 /* Get memory bandwidth limits */
599 if (priv->dispc_ops->get_memory_bandwidth_limit)
600 priv->max_bandwidth =
601 priv->dispc_ops->get_memory_bandwidth_limit(priv->dispc);
605 ret = omap_modeset_init(ddev);
607 dev_err(priv->dev, "omap_modeset_init failed: ret=%d\n", ret);
611 /* Initialize vblank handling, start with all CRTCs disabled. */
612 ret = drm_vblank_init(ddev, priv->num_pipes);
614 dev_err(priv->dev, "could not init vblank\n");
615 goto err_cleanup_modeset;
618 for (i = 0; i < priv->num_pipes; i++)
619 drm_crtc_vblank_off(priv->pipes[i].crtc);
621 omap_fbdev_init(ddev);
623 drm_kms_helper_poll_init(ddev);
624 omap_modeset_enable_external_hpd(ddev);
627 * Register the DRM device with the core and the connectors with
630 ret = drm_dev_register(ddev, 0);
632 goto err_cleanup_helpers;
637 omap_modeset_disable_external_hpd(ddev);
638 drm_kms_helper_poll_fini(ddev);
640 omap_fbdev_fini(ddev);
642 drm_mode_config_cleanup(ddev);
643 omap_drm_irq_uninstall(ddev);
645 omap_gem_deinit(ddev);
646 destroy_workqueue(priv->wq);
647 omap_disconnect_pipelines(ddev);
648 omap_crtc_pre_uninit(priv);
653 static void omapdrm_cleanup(struct omap_drm_private *priv)
655 struct drm_device *ddev = priv->ddev;
659 drm_dev_unregister(ddev);
661 omap_modeset_disable_external_hpd(ddev);
662 drm_kms_helper_poll_fini(ddev);
664 omap_fbdev_fini(ddev);
666 drm_atomic_helper_shutdown(ddev);
668 drm_mode_config_cleanup(ddev);
670 omap_drm_irq_uninstall(ddev);
671 omap_gem_deinit(ddev);
673 destroy_workqueue(priv->wq);
675 omap_disconnect_pipelines(ddev);
676 omap_crtc_pre_uninit(priv);
681 static int pdev_probe(struct platform_device *pdev)
683 struct omap_drm_private *priv;
686 if (omapdss_is_initialized() == false)
687 return -EPROBE_DEFER;
689 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
691 dev_err(&pdev->dev, "Failed to set the DMA mask\n");
695 /* Allocate and initialize the driver private structure. */
696 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
700 platform_set_drvdata(pdev, priv);
702 ret = omapdrm_init(priv, &pdev->dev);
709 static int pdev_remove(struct platform_device *pdev)
711 struct omap_drm_private *priv = platform_get_drvdata(pdev);
713 omapdrm_cleanup(priv);
719 #ifdef CONFIG_PM_SLEEP
720 static int omap_drm_suspend(struct device *dev)
722 struct omap_drm_private *priv = dev_get_drvdata(dev);
723 struct drm_device *drm_dev = priv->ddev;
725 return drm_mode_config_helper_suspend(drm_dev);
728 static int omap_drm_resume(struct device *dev)
730 struct omap_drm_private *priv = dev_get_drvdata(dev);
731 struct drm_device *drm_dev = priv->ddev;
733 drm_mode_config_helper_resume(drm_dev);
735 return omap_gem_resume(drm_dev);
739 static SIMPLE_DEV_PM_OPS(omapdrm_pm_ops, omap_drm_suspend, omap_drm_resume);
741 static struct platform_driver pdev = {
744 .pm = &omapdrm_pm_ops,
747 .remove = pdev_remove,
750 static struct platform_driver * const drivers[] = {
755 static int __init omap_drm_init(void)
759 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
762 static void __exit omap_drm_fini(void)
766 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
769 /* need late_initcall() so we load after dss_driver's are loaded */
770 late_initcall(omap_drm_init);
771 module_exit(omap_drm_fini);
773 MODULE_AUTHOR("Rob Clark <rob@ti.com>");
774 MODULE_DESCRIPTION("OMAP DRM Display Driver");
775 MODULE_ALIAS("platform:" DRIVER_NAME);
776 MODULE_LICENSE("GPL v2");