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
->display
->alias_id
> pipe2
->display
->alias_id
)
160 else if (pipe1
->display
->alias_id
< pipe2
->display
->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
;
186 pipe
= &priv
->pipes
[priv
->num_pipes
++];
187 pipe
->output
= omapdss_device_get(output
);
188 pipe
->display
= omapdss_display_get(output
);
190 if (priv
->num_pipes
== ARRAY_SIZE(priv
->pipes
)) {
191 /* To balance the 'for_each_dss_output' loop */
192 omapdss_device_put(output
);
198 /* Sort the list by DT aliases */
199 sort(priv
->pipes
, priv
->num_pipes
, sizeof(priv
->pipes
[0]),
200 omap_compare_pipes
, NULL
);
203 * Populate the pipeline lookup table by DISPC channel. Only one display
204 * is allowed per channel.
206 for (i
= 0; i
< priv
->num_pipes
; ++i
) {
207 struct omap_drm_pipeline
*pipe
= &priv
->pipes
[i
];
208 enum omap_channel channel
= pipe
->output
->dispc_channel
;
210 if (WARN_ON(priv
->channels
[channel
] != NULL
)) {
215 priv
->channels
[channel
] = pipe
;
222 * if we are deferring probe, we disconnect the devices we previously
225 omap_disconnect_pipelines(ddev
);
230 static int omap_modeset_init_properties(struct drm_device
*dev
)
232 struct omap_drm_private
*priv
= dev
->dev_private
;
233 unsigned int num_planes
= priv
->dispc_ops
->get_num_ovls(priv
->dispc
);
235 priv
->zorder_prop
= drm_property_create_range(dev
, 0, "zorder", 0,
237 if (!priv
->zorder_prop
)
243 static int omap_modeset_init(struct drm_device
*dev
)
245 struct omap_drm_private
*priv
= dev
->dev_private
;
246 int num_ovls
= priv
->dispc_ops
->get_num_ovls(priv
->dispc
);
247 int num_mgrs
= priv
->dispc_ops
->get_num_mgrs(priv
->dispc
);
252 drm_mode_config_init(dev
);
254 ret
= omap_modeset_init_properties(dev
);
259 * This function creates exactly one connector, encoder, crtc,
260 * and primary plane per each connected dss-device. Each
261 * connector->encoder->crtc chain is expected to be separate
262 * and each crtc is connect to a single dss-channel. If the
263 * configuration does not match the expectations or exceeds
264 * the available resources, the configuration is rejected.
266 if (priv
->num_pipes
> num_mgrs
|| priv
->num_pipes
> num_ovls
) {
267 dev_err(dev
->dev
, "%s(): Too many connected displays\n",
272 /* Create all planes first. They can all be put to any CRTC. */
273 plane_crtc_mask
= (1 << priv
->num_pipes
) - 1;
275 for (i
= 0; i
< num_ovls
; i
++) {
276 enum drm_plane_type type
= i
< priv
->num_pipes
277 ? DRM_PLANE_TYPE_PRIMARY
278 : DRM_PLANE_TYPE_OVERLAY
;
279 struct drm_plane
*plane
;
281 if (WARN_ON(priv
->num_planes
>= ARRAY_SIZE(priv
->planes
)))
284 plane
= omap_plane_init(dev
, i
, type
, plane_crtc_mask
);
286 return PTR_ERR(plane
);
288 priv
->planes
[priv
->num_planes
++] = plane
;
291 /* Create the CRTCs, encoders and connectors. */
292 for (i
= 0; i
< priv
->num_pipes
; i
++) {
293 struct omap_drm_pipeline
*pipe
= &priv
->pipes
[i
];
294 struct omap_dss_device
*display
= pipe
->display
;
295 struct drm_connector
*connector
;
296 struct drm_encoder
*encoder
;
297 struct drm_crtc
*crtc
;
299 encoder
= omap_encoder_init(dev
, pipe
->output
, display
);
303 connector
= omap_connector_init(dev
, pipe
->output
, display
,
308 crtc
= omap_crtc_init(dev
, pipe
, priv
->planes
[i
]);
310 return PTR_ERR(crtc
);
312 drm_connector_attach_encoder(connector
, encoder
);
313 encoder
->possible_crtcs
= 1 << i
;
316 pipe
->encoder
= encoder
;
317 pipe
->connector
= connector
;
320 DBG("registered %u planes, %u crtcs/encoders/connectors\n",
321 priv
->num_planes
, priv
->num_pipes
);
323 dev
->mode_config
.min_width
= 8;
324 dev
->mode_config
.min_height
= 2;
327 * Note: these values are used for multiple independent things:
328 * connector mode filtering, buffer sizes, crtc sizes...
329 * Use big enough values here to cover all use cases, and do more
330 * specific checking in the respective code paths.
332 dev
->mode_config
.max_width
= 8192;
333 dev
->mode_config
.max_height
= 8192;
335 /* We want the zpos to be normalized */
336 dev
->mode_config
.normalize_zpos
= true;
338 dev
->mode_config
.funcs
= &omap_mode_config_funcs
;
339 dev
->mode_config
.helper_private
= &omap_mode_config_helper_funcs
;
341 drm_mode_config_reset(dev
);
343 omap_drm_irq_install(dev
);
349 * Enable the HPD in external components if supported
351 static void omap_modeset_enable_external_hpd(struct drm_device
*ddev
)
353 struct omap_drm_private
*priv
= ddev
->dev_private
;
356 for (i
= 0; i
< priv
->num_pipes
; i
++)
357 omap_connector_enable_hpd(priv
->pipes
[i
].connector
);
361 * Disable the HPD in external components if supported
363 static void omap_modeset_disable_external_hpd(struct drm_device
*ddev
)
365 struct omap_drm_private
*priv
= ddev
->dev_private
;
368 for (i
= 0; i
< priv
->num_pipes
; i
++)
369 omap_connector_disable_hpd(priv
->pipes
[i
].connector
);
377 static int ioctl_get_param(struct drm_device
*dev
, void *data
,
378 struct drm_file
*file_priv
)
380 struct omap_drm_private
*priv
= dev
->dev_private
;
381 struct drm_omap_param
*args
= data
;
383 DBG("%p: param=%llu", dev
, args
->param
);
385 switch (args
->param
) {
386 case OMAP_PARAM_CHIPSET_ID
:
387 args
->value
= priv
->omaprev
;
390 DBG("unknown parameter %lld", args
->param
);
397 static int ioctl_set_param(struct drm_device
*dev
, void *data
,
398 struct drm_file
*file_priv
)
400 struct drm_omap_param
*args
= data
;
402 switch (args
->param
) {
404 DBG("unknown parameter %lld", args
->param
);
411 #define OMAP_BO_USER_MASK 0x00ffffff /* flags settable by userspace */
413 static int ioctl_gem_new(struct drm_device
*dev
, void *data
,
414 struct drm_file
*file_priv
)
416 struct drm_omap_gem_new
*args
= data
;
417 u32 flags
= args
->flags
& OMAP_BO_USER_MASK
;
419 VERB("%p:%p: size=0x%08x, flags=%08x", dev
, file_priv
,
420 args
->size
.bytes
, flags
);
422 return omap_gem_new_handle(dev
, file_priv
, args
->size
, flags
,
426 static int ioctl_gem_info(struct drm_device
*dev
, void *data
,
427 struct drm_file
*file_priv
)
429 struct drm_omap_gem_info
*args
= data
;
430 struct drm_gem_object
*obj
;
433 VERB("%p:%p: handle=%d", dev
, file_priv
, args
->handle
);
435 obj
= drm_gem_object_lookup(file_priv
, args
->handle
);
439 args
->size
= omap_gem_mmap_size(obj
);
440 args
->offset
= omap_gem_mmap_offset(obj
);
442 drm_gem_object_put_unlocked(obj
);
447 static const struct drm_ioctl_desc ioctls
[DRM_COMMAND_END
- DRM_COMMAND_BASE
] = {
448 DRM_IOCTL_DEF_DRV(OMAP_GET_PARAM
, ioctl_get_param
,
449 DRM_AUTH
| DRM_RENDER_ALLOW
),
450 DRM_IOCTL_DEF_DRV(OMAP_SET_PARAM
, ioctl_set_param
,
451 DRM_AUTH
| DRM_MASTER
| DRM_ROOT_ONLY
),
452 DRM_IOCTL_DEF_DRV(OMAP_GEM_NEW
, ioctl_gem_new
,
453 DRM_AUTH
| DRM_RENDER_ALLOW
),
454 /* Deprecated, to be removed. */
455 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_PREP
, drm_noop
,
456 DRM_AUTH
| DRM_RENDER_ALLOW
),
457 /* Deprecated, to be removed. */
458 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_FINI
, drm_noop
,
459 DRM_AUTH
| DRM_RENDER_ALLOW
),
460 DRM_IOCTL_DEF_DRV(OMAP_GEM_INFO
, ioctl_gem_info
,
461 DRM_AUTH
| DRM_RENDER_ALLOW
),
468 static int dev_open(struct drm_device
*dev
, struct drm_file
*file
)
470 file
->driver_priv
= NULL
;
472 DBG("open: dev=%p, file=%p", dev
, file
);
477 static const struct vm_operations_struct omap_gem_vm_ops
= {
478 .fault
= omap_gem_fault
,
479 .open
= drm_gem_vm_open
,
480 .close
= drm_gem_vm_close
,
483 static const struct file_operations omapdriver_fops
= {
484 .owner
= THIS_MODULE
,
486 .unlocked_ioctl
= drm_ioctl
,
487 .compat_ioctl
= drm_compat_ioctl
,
488 .release
= drm_release
,
489 .mmap
= omap_gem_mmap
,
492 .llseek
= noop_llseek
,
495 static struct drm_driver omap_drm_driver
= {
496 .driver_features
= DRIVER_MODESET
| DRIVER_GEM
| DRIVER_PRIME
|
497 DRIVER_ATOMIC
| DRIVER_RENDER
,
499 .lastclose
= drm_fb_helper_lastclose
,
500 #ifdef CONFIG_DEBUG_FS
501 .debugfs_init
= omap_debugfs_init
,
503 .prime_handle_to_fd
= drm_gem_prime_handle_to_fd
,
504 .prime_fd_to_handle
= drm_gem_prime_fd_to_handle
,
505 .gem_prime_export
= omap_gem_prime_export
,
506 .gem_prime_import
= omap_gem_prime_import
,
507 .gem_free_object_unlocked
= omap_gem_free_object
,
508 .gem_vm_ops
= &omap_gem_vm_ops
,
509 .dumb_create
= omap_gem_dumb_create
,
510 .dumb_map_offset
= omap_gem_dumb_map_offset
,
512 .num_ioctls
= DRM_OMAP_NUM_IOCTLS
,
513 .fops
= &omapdriver_fops
,
517 .major
= DRIVER_MAJOR
,
518 .minor
= DRIVER_MINOR
,
519 .patchlevel
= DRIVER_PATCHLEVEL
,
522 static const struct soc_device_attribute omapdrm_soc_devices
[] = {
523 { .family
= "OMAP3", .data
= (void *)0x3430 },
524 { .family
= "OMAP4", .data
= (void *)0x4430 },
525 { .family
= "OMAP5", .data
= (void *)0x5430 },
526 { .family
= "DRA7", .data
= (void *)0x0752 },
530 static int omapdrm_init(struct omap_drm_private
*priv
, struct device
*dev
)
532 const struct soc_device_attribute
*soc
;
533 struct drm_device
*ddev
;
537 DBG("%s", dev_name(dev
));
539 /* Allocate and initialize the DRM device. */
540 ddev
= drm_dev_alloc(&omap_drm_driver
, dev
);
542 return PTR_ERR(ddev
);
545 ddev
->dev_private
= priv
;
548 priv
->dss
= omapdss_get_dss();
549 priv
->dispc
= dispc_get_dispc(priv
->dss
);
550 priv
->dispc_ops
= dispc_get_ops(priv
->dss
);
552 omap_crtc_pre_init(priv
);
554 ret
= omap_connect_pipelines(ddev
);
556 goto err_crtc_uninit
;
558 soc
= soc_device_match(omapdrm_soc_devices
);
559 priv
->omaprev
= soc
? (unsigned int)soc
->data
: 0;
560 priv
->wq
= alloc_ordered_workqueue("omapdrm", 0);
562 mutex_init(&priv
->list_lock
);
563 INIT_LIST_HEAD(&priv
->obj_list
);
565 /* Get memory bandwidth limits */
566 if (priv
->dispc_ops
->get_memory_bandwidth_limit
)
567 priv
->max_bandwidth
=
568 priv
->dispc_ops
->get_memory_bandwidth_limit(priv
->dispc
);
572 ret
= omap_modeset_init(ddev
);
574 dev_err(priv
->dev
, "omap_modeset_init failed: ret=%d\n", ret
);
578 /* Initialize vblank handling, start with all CRTCs disabled. */
579 ret
= drm_vblank_init(ddev
, priv
->num_pipes
);
581 dev_err(priv
->dev
, "could not init vblank\n");
582 goto err_cleanup_modeset
;
585 for (i
= 0; i
< priv
->num_pipes
; i
++)
586 drm_crtc_vblank_off(priv
->pipes
[i
].crtc
);
588 omap_fbdev_init(ddev
);
590 drm_kms_helper_poll_init(ddev
);
591 omap_modeset_enable_external_hpd(ddev
);
594 * Register the DRM device with the core and the connectors with
597 ret
= drm_dev_register(ddev
, 0);
599 goto err_cleanup_helpers
;
604 omap_modeset_disable_external_hpd(ddev
);
605 drm_kms_helper_poll_fini(ddev
);
607 omap_fbdev_fini(ddev
);
609 drm_mode_config_cleanup(ddev
);
610 omap_drm_irq_uninstall(ddev
);
612 omap_gem_deinit(ddev
);
613 destroy_workqueue(priv
->wq
);
614 omap_disconnect_pipelines(ddev
);
616 omap_crtc_pre_uninit(priv
);
621 static void omapdrm_cleanup(struct omap_drm_private
*priv
)
623 struct drm_device
*ddev
= priv
->ddev
;
627 drm_dev_unregister(ddev
);
629 omap_modeset_disable_external_hpd(ddev
);
630 drm_kms_helper_poll_fini(ddev
);
632 omap_fbdev_fini(ddev
);
634 drm_atomic_helper_shutdown(ddev
);
636 drm_mode_config_cleanup(ddev
);
638 omap_drm_irq_uninstall(ddev
);
639 omap_gem_deinit(ddev
);
641 destroy_workqueue(priv
->wq
);
643 omap_disconnect_pipelines(ddev
);
644 omap_crtc_pre_uninit(priv
);
649 static int pdev_probe(struct platform_device
*pdev
)
651 struct omap_drm_private
*priv
;
654 if (omapdss_is_initialized() == false)
655 return -EPROBE_DEFER
;
657 ret
= dma_set_coherent_mask(&pdev
->dev
, DMA_BIT_MASK(32));
659 dev_err(&pdev
->dev
, "Failed to set the DMA mask\n");
663 /* Allocate and initialize the driver private structure. */
664 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
668 platform_set_drvdata(pdev
, priv
);
670 ret
= omapdrm_init(priv
, &pdev
->dev
);
677 static int pdev_remove(struct platform_device
*pdev
)
679 struct omap_drm_private
*priv
= platform_get_drvdata(pdev
);
681 omapdrm_cleanup(priv
);
687 #ifdef CONFIG_PM_SLEEP
688 static int omap_drm_suspend_all_displays(struct drm_device
*ddev
)
690 struct omap_drm_private
*priv
= ddev
->dev_private
;
693 for (i
= 0; i
< priv
->num_pipes
; i
++) {
694 struct omap_dss_device
*display
= priv
->pipes
[i
].display
;
696 if (display
->state
== OMAP_DSS_DISPLAY_ACTIVE
) {
697 display
->ops
->disable(display
);
698 display
->activate_after_resume
= true;
700 display
->activate_after_resume
= false;
707 static int omap_drm_resume_all_displays(struct drm_device
*ddev
)
709 struct omap_drm_private
*priv
= ddev
->dev_private
;
712 for (i
= 0; i
< priv
->num_pipes
; i
++) {
713 struct omap_dss_device
*display
= priv
->pipes
[i
].display
;
715 if (display
->activate_after_resume
) {
716 display
->ops
->enable(display
);
717 display
->activate_after_resume
= false;
724 static int omap_drm_suspend(struct device
*dev
)
726 struct omap_drm_private
*priv
= dev_get_drvdata(dev
);
727 struct drm_device
*drm_dev
= priv
->ddev
;
729 drm_kms_helper_poll_disable(drm_dev
);
731 drm_modeset_lock_all(drm_dev
);
732 omap_drm_suspend_all_displays(drm_dev
);
733 drm_modeset_unlock_all(drm_dev
);
738 static int omap_drm_resume(struct device
*dev
)
740 struct omap_drm_private
*priv
= dev_get_drvdata(dev
);
741 struct drm_device
*drm_dev
= priv
->ddev
;
743 drm_modeset_lock_all(drm_dev
);
744 omap_drm_resume_all_displays(drm_dev
);
745 drm_modeset_unlock_all(drm_dev
);
747 drm_kms_helper_poll_enable(drm_dev
);
749 return omap_gem_resume(drm_dev
);
753 static SIMPLE_DEV_PM_OPS(omapdrm_pm_ops
, omap_drm_suspend
, omap_drm_resume
);
755 static struct platform_driver pdev
= {
758 .pm
= &omapdrm_pm_ops
,
761 .remove
= pdev_remove
,
764 static struct platform_driver
* const drivers
[] = {
769 static int __init
omap_drm_init(void)
773 return platform_register_drivers(drivers
, ARRAY_SIZE(drivers
));
776 static void __exit
omap_drm_fini(void)
780 platform_unregister_drivers(drivers
, ARRAY_SIZE(drivers
));
783 /* need late_initcall() so we load after dss_driver's are loaded */
784 late_initcall(omap_drm_init
);
785 module_exit(omap_drm_fini
);
787 MODULE_AUTHOR("Rob Clark <rob@ti.com>");
788 MODULE_DESCRIPTION("OMAP DRM Display Driver");
789 MODULE_ALIAS("platform:" DRIVER_NAME
);
790 MODULE_LICENSE("GPL v2");