2 * drivers/gpu/drm/omapdrm/omap_drv.c
4 * Copyright (C) 2011 Texas Instruments
5 * Author: Rob Clark <rob@ti.com>
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/wait.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
37 static int num_crtc = CONFIG_DRM_OMAP_NUM_CRTCS;
39 MODULE_PARM_DESC(num_crtc, "Number of overlays to use as CRTCs");
40 module_param(num_crtc, int, 0600);
46 /* Notes about mapping DSS and DRM entities:
48 * encoder: manager.. with some extension to allow one primary CRTC
49 * and zero or more video CRTC's to be mapped to one encoder?
50 * connector: dssdev.. manager can be attached/detached from different
54 static void omap_fb_output_poll_changed(struct drm_device *dev)
56 struct omap_drm_private *priv = dev->dev_private;
59 drm_fb_helper_hotplug_event(priv->fbdev);
62 struct omap_atomic_state_commit {
63 struct work_struct work;
64 struct drm_device *dev;
65 struct drm_atomic_state *state;
69 static void omap_atomic_complete(struct omap_atomic_state_commit *commit)
71 struct drm_device *dev = commit->dev;
72 struct omap_drm_private *priv = dev->dev_private;
73 struct drm_atomic_state *old_state = commit->state;
75 /* Apply the atomic update. */
76 drm_atomic_helper_commit_modeset_disables(dev, old_state);
77 drm_atomic_helper_commit_planes(dev, old_state);
78 drm_atomic_helper_commit_modeset_enables(dev, old_state);
80 drm_atomic_helper_wait_for_vblanks(dev, old_state);
82 drm_atomic_helper_cleanup_planes(dev, old_state);
84 drm_atomic_state_free(old_state);
86 /* Complete the commit, wake up any waiter. */
87 spin_lock(&priv->commit.lock);
88 priv->commit.pending &= ~commit->crtcs;
89 spin_unlock(&priv->commit.lock);
91 wake_up_all(&priv->commit.wait);
96 static void omap_atomic_work(struct work_struct *work)
98 struct omap_atomic_state_commit *commit =
99 container_of(work, struct omap_atomic_state_commit, work);
101 omap_atomic_complete(commit);
104 static bool omap_atomic_is_pending(struct omap_drm_private *priv,
105 struct omap_atomic_state_commit *commit)
109 spin_lock(&priv->commit.lock);
110 pending = priv->commit.pending & commit->crtcs;
111 spin_unlock(&priv->commit.lock);
116 static int omap_atomic_commit(struct drm_device *dev,
117 struct drm_atomic_state *state, bool async)
119 struct omap_drm_private *priv = dev->dev_private;
120 struct omap_atomic_state_commit *commit;
124 ret = drm_atomic_helper_prepare_planes(dev, state);
128 /* Allocate the commit object. */
129 commit = kzalloc(sizeof(*commit), GFP_KERNEL);
130 if (commit == NULL) {
135 INIT_WORK(&commit->work, omap_atomic_work);
137 commit->state = state;
139 /* Wait until all affected CRTCs have completed previous commits and
140 * mark them as pending.
142 for (i = 0; i < dev->mode_config.num_crtc; ++i) {
144 commit->crtcs |= 1 << drm_crtc_index(state->crtcs[i]);
147 wait_event(priv->commit.wait, !omap_atomic_is_pending(priv, commit));
149 spin_lock(&priv->commit.lock);
150 priv->commit.pending |= commit->crtcs;
151 spin_unlock(&priv->commit.lock);
153 /* Swap the state, this is the point of no return. */
154 drm_atomic_helper_swap_state(dev, state);
157 schedule_work(&commit->work);
159 omap_atomic_complete(commit);
164 drm_atomic_helper_cleanup_planes(dev, state);
168 static const struct drm_mode_config_funcs omap_mode_config_funcs = {
169 .fb_create = omap_framebuffer_create,
170 .output_poll_changed = omap_fb_output_poll_changed,
171 .atomic_check = drm_atomic_helper_check,
172 .atomic_commit = omap_atomic_commit,
175 static int get_connector_type(struct omap_dss_device *dssdev)
177 switch (dssdev->type) {
178 case OMAP_DISPLAY_TYPE_HDMI:
179 return DRM_MODE_CONNECTOR_HDMIA;
180 case OMAP_DISPLAY_TYPE_DVI:
181 return DRM_MODE_CONNECTOR_DVID;
183 return DRM_MODE_CONNECTOR_Unknown;
187 static bool channel_used(struct drm_device *dev, enum omap_channel channel)
189 struct omap_drm_private *priv = dev->dev_private;
192 for (i = 0; i < priv->num_crtcs; i++) {
193 struct drm_crtc *crtc = priv->crtcs[i];
195 if (omap_crtc_channel(crtc) == channel)
201 static void omap_disconnect_dssdevs(void)
203 struct omap_dss_device *dssdev = NULL;
205 for_each_dss_dev(dssdev)
206 dssdev->driver->disconnect(dssdev);
209 static int omap_connect_dssdevs(void)
212 struct omap_dss_device *dssdev = NULL;
213 bool no_displays = true;
215 for_each_dss_dev(dssdev) {
216 r = dssdev->driver->connect(dssdev);
217 if (r == -EPROBE_DEFER) {
218 omap_dss_put_device(dssdev);
221 dev_warn(dssdev->dev, "could not connect display: %s\n",
229 return -EPROBE_DEFER;
235 * if we are deferring probe, we disconnect the devices we previously
238 omap_disconnect_dssdevs();
243 static int omap_modeset_create_crtc(struct drm_device *dev, int id,
244 enum omap_channel channel)
246 struct omap_drm_private *priv = dev->dev_private;
247 struct drm_plane *plane;
248 struct drm_crtc *crtc;
250 plane = omap_plane_init(dev, id, DRM_PLANE_TYPE_PRIMARY);
252 return PTR_ERR(plane);
254 crtc = omap_crtc_init(dev, plane, channel, id);
256 BUG_ON(priv->num_crtcs >= ARRAY_SIZE(priv->crtcs));
257 priv->crtcs[id] = crtc;
260 priv->planes[id] = plane;
266 static int omap_modeset_init_properties(struct drm_device *dev)
268 struct omap_drm_private *priv = dev->dev_private;
271 dev->mode_config.rotation_property =
272 drm_mode_create_rotation_property(dev,
273 BIT(DRM_ROTATE_0) | BIT(DRM_ROTATE_90) |
274 BIT(DRM_ROTATE_180) | BIT(DRM_ROTATE_270) |
275 BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y));
276 if (!dev->mode_config.rotation_property)
280 priv->zorder_prop = drm_property_create_range(dev, 0, "zorder", 0, 3);
281 if (!priv->zorder_prop)
287 static int omap_modeset_init(struct drm_device *dev)
289 struct omap_drm_private *priv = dev->dev_private;
290 struct omap_dss_device *dssdev = NULL;
291 int num_ovls = dss_feat_get_num_ovls();
292 int num_mgrs = dss_feat_get_num_mgrs();
297 drm_mode_config_init(dev);
299 omap_drm_irq_install(dev);
301 ret = omap_modeset_init_properties(dev);
306 * We usually don't want to create a CRTC for each manager, at least
307 * not until we have a way to expose private planes to userspace.
308 * Otherwise there would not be enough video pipes left for drm planes.
309 * We use the num_crtc argument to limit the number of crtcs we create.
311 num_crtcs = min3(num_crtc, num_mgrs, num_ovls);
315 for_each_dss_dev(dssdev) {
316 struct drm_connector *connector;
317 struct drm_encoder *encoder;
318 enum omap_channel channel;
319 struct omap_overlay_manager *mgr;
321 if (!omapdss_device_is_connected(dssdev))
324 encoder = omap_encoder_init(dev, dssdev);
327 dev_err(dev->dev, "could not create encoder: %s\n",
332 connector = omap_connector_init(dev,
333 get_connector_type(dssdev), dssdev, encoder);
336 dev_err(dev->dev, "could not create connector: %s\n",
341 BUG_ON(priv->num_encoders >= ARRAY_SIZE(priv->encoders));
342 BUG_ON(priv->num_connectors >= ARRAY_SIZE(priv->connectors));
344 priv->encoders[priv->num_encoders++] = encoder;
345 priv->connectors[priv->num_connectors++] = connector;
347 drm_mode_connector_attach_encoder(connector, encoder);
350 * if we have reached the limit of the crtcs we are allowed to
351 * create, let's not try to look for a crtc for this
352 * panel/encoder and onwards, we will, of course, populate the
353 * the possible_crtcs field for all the encoders with the final
354 * set of crtcs we create
360 * get the recommended DISPC channel for this encoder. For now,
361 * we only try to get create a crtc out of the recommended, the
362 * other possible channels to which the encoder can connect are
366 mgr = omapdss_find_mgr_from_display(dssdev);
369 * if this channel hasn't already been taken by a previously
370 * allocated crtc, we create a new crtc for it
372 if (!channel_used(dev, channel)) {
373 ret = omap_modeset_create_crtc(dev, id, channel);
376 "could not create CRTC (channel %u)\n",
386 * we have allocated crtcs according to the need of the panels/encoders,
387 * adding more crtcs here if needed
389 for (; id < num_crtcs; id++) {
391 /* find a free manager for this crtc */
392 for (i = 0; i < num_mgrs; i++) {
393 if (!channel_used(dev, i))
398 /* this shouldn't really happen */
399 dev_err(dev->dev, "no managers left for crtc\n");
403 ret = omap_modeset_create_crtc(dev, id, i);
406 "could not create CRTC (channel %u)\n", i);
412 * Create normal planes for the remaining overlays:
414 for (; id < num_ovls; id++) {
415 struct drm_plane *plane;
417 plane = omap_plane_init(dev, id, DRM_PLANE_TYPE_OVERLAY);
419 return PTR_ERR(plane);
421 BUG_ON(priv->num_planes >= ARRAY_SIZE(priv->planes));
422 priv->planes[priv->num_planes++] = plane;
425 for (i = 0; i < priv->num_encoders; i++) {
426 struct drm_encoder *encoder = priv->encoders[i];
427 struct omap_dss_device *dssdev =
428 omap_encoder_get_dssdev(encoder);
429 struct omap_dss_device *output;
431 output = omapdss_find_output_from_display(dssdev);
433 /* figure out which crtc's we can connect the encoder to: */
434 encoder->possible_crtcs = 0;
435 for (id = 0; id < priv->num_crtcs; id++) {
436 struct drm_crtc *crtc = priv->crtcs[id];
437 enum omap_channel crtc_channel;
439 crtc_channel = omap_crtc_channel(crtc);
441 if (output->dispc_channel == crtc_channel) {
442 encoder->possible_crtcs |= (1 << id);
447 omap_dss_put_device(output);
450 DBG("registered %d planes, %d crtcs, %d encoders and %d connectors\n",
451 priv->num_planes, priv->num_crtcs, priv->num_encoders,
452 priv->num_connectors);
454 dev->mode_config.min_width = 32;
455 dev->mode_config.min_height = 32;
457 /* note: eventually will need some cpu_is_omapXYZ() type stuff here
458 * to fill in these limits properly on different OMAP generations..
460 dev->mode_config.max_width = 2048;
461 dev->mode_config.max_height = 2048;
463 dev->mode_config.funcs = &omap_mode_config_funcs;
465 drm_mode_config_reset(dev);
470 static void omap_modeset_free(struct drm_device *dev)
472 drm_mode_config_cleanup(dev);
480 static int ioctl_get_param(struct drm_device *dev, void *data,
481 struct drm_file *file_priv)
483 struct omap_drm_private *priv = dev->dev_private;
484 struct drm_omap_param *args = data;
486 DBG("%p: param=%llu", dev, args->param);
488 switch (args->param) {
489 case OMAP_PARAM_CHIPSET_ID:
490 args->value = priv->omaprev;
493 DBG("unknown parameter %lld", args->param);
500 static int ioctl_set_param(struct drm_device *dev, void *data,
501 struct drm_file *file_priv)
503 struct drm_omap_param *args = data;
505 switch (args->param) {
507 DBG("unknown parameter %lld", args->param);
514 static int ioctl_gem_new(struct drm_device *dev, void *data,
515 struct drm_file *file_priv)
517 struct drm_omap_gem_new *args = data;
518 VERB("%p:%p: size=0x%08x, flags=%08x", dev, file_priv,
519 args->size.bytes, args->flags);
520 return omap_gem_new_handle(dev, file_priv, args->size,
521 args->flags, &args->handle);
524 static int ioctl_gem_cpu_prep(struct drm_device *dev, void *data,
525 struct drm_file *file_priv)
527 struct drm_omap_gem_cpu_prep *args = data;
528 struct drm_gem_object *obj;
531 VERB("%p:%p: handle=%d, op=%x", dev, file_priv, args->handle, args->op);
533 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
537 ret = omap_gem_op_sync(obj, args->op);
540 ret = omap_gem_op_start(obj, args->op);
542 drm_gem_object_unreference_unlocked(obj);
547 static int ioctl_gem_cpu_fini(struct drm_device *dev, void *data,
548 struct drm_file *file_priv)
550 struct drm_omap_gem_cpu_fini *args = data;
551 struct drm_gem_object *obj;
554 VERB("%p:%p: handle=%d", dev, file_priv, args->handle);
556 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
560 /* XXX flushy, flushy */
564 ret = omap_gem_op_finish(obj, args->op);
566 drm_gem_object_unreference_unlocked(obj);
571 static int ioctl_gem_info(struct drm_device *dev, void *data,
572 struct drm_file *file_priv)
574 struct drm_omap_gem_info *args = data;
575 struct drm_gem_object *obj;
578 VERB("%p:%p: handle=%d", dev, file_priv, args->handle);
580 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
584 args->size = omap_gem_mmap_size(obj);
585 args->offset = omap_gem_mmap_offset(obj);
587 drm_gem_object_unreference_unlocked(obj);
592 static const struct drm_ioctl_desc ioctls[DRM_COMMAND_END - DRM_COMMAND_BASE] = {
593 DRM_IOCTL_DEF_DRV(OMAP_GET_PARAM, ioctl_get_param, DRM_UNLOCKED|DRM_AUTH),
594 DRM_IOCTL_DEF_DRV(OMAP_SET_PARAM, ioctl_set_param, DRM_UNLOCKED|DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
595 DRM_IOCTL_DEF_DRV(OMAP_GEM_NEW, ioctl_gem_new, DRM_UNLOCKED|DRM_AUTH),
596 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_PREP, ioctl_gem_cpu_prep, DRM_UNLOCKED|DRM_AUTH),
597 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_FINI, ioctl_gem_cpu_fini, DRM_UNLOCKED|DRM_AUTH),
598 DRM_IOCTL_DEF_DRV(OMAP_GEM_INFO, ioctl_gem_info, DRM_UNLOCKED|DRM_AUTH),
606 * load - setup chip and create an initial config
608 * @flags: startup flags
610 * The driver load routine has to do several things:
611 * - initialize the memory manager
612 * - allocate initial config memory
613 * - setup the DRM framebuffer with the allocated memory
615 static int dev_load(struct drm_device *dev, unsigned long flags)
617 struct omap_drm_platform_data *pdata = dev->dev->platform_data;
618 struct omap_drm_private *priv;
622 DBG("load: dev=%p", dev);
624 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
628 priv->omaprev = pdata->omaprev;
630 dev->dev_private = priv;
632 priv->wq = alloc_ordered_workqueue("omapdrm", 0);
633 init_waitqueue_head(&priv->commit.wait);
634 spin_lock_init(&priv->commit.lock);
636 spin_lock_init(&priv->list_lock);
637 INIT_LIST_HEAD(&priv->obj_list);
641 ret = omap_modeset_init(dev);
643 dev_err(dev->dev, "omap_modeset_init failed: ret=%d\n", ret);
644 dev->dev_private = NULL;
649 /* Initialize vblank handling, start with all CRTCs disabled. */
650 ret = drm_vblank_init(dev, priv->num_crtcs);
652 dev_warn(dev->dev, "could not init vblank\n");
654 for (i = 0; i < priv->num_crtcs; i++)
655 drm_crtc_vblank_off(priv->crtcs[i]);
657 priv->fbdev = omap_fbdev_init(dev);
659 dev_warn(dev->dev, "omap_fbdev_init failed\n");
660 /* well, limp along without an fbdev.. maybe X11 will work? */
663 /* store off drm_device for use in pm ops */
664 dev_set_drvdata(dev->dev, dev);
666 drm_kms_helper_poll_init(dev);
671 static int dev_unload(struct drm_device *dev)
673 struct omap_drm_private *priv = dev->dev_private;
675 DBG("unload: dev=%p", dev);
677 drm_kms_helper_poll_fini(dev);
680 omap_fbdev_free(dev);
682 omap_modeset_free(dev);
683 omap_gem_deinit(dev);
685 destroy_workqueue(priv->wq);
687 drm_vblank_cleanup(dev);
688 omap_drm_irq_uninstall(dev);
690 kfree(dev->dev_private);
691 dev->dev_private = NULL;
693 dev_set_drvdata(dev->dev, NULL);
698 static int dev_open(struct drm_device *dev, struct drm_file *file)
700 file->driver_priv = NULL;
702 DBG("open: dev=%p, file=%p", dev, file);
708 * lastclose - clean up after all DRM clients have exited
711 * Take care of cleaning up after all DRM clients have exited. In the
712 * mode setting case, we want to restore the kernel's initial mode (just
713 * in case the last client left us in a bad state).
715 static void dev_lastclose(struct drm_device *dev)
719 /* we don't support vga-switcheroo.. so just make sure the fbdev
722 struct omap_drm_private *priv = dev->dev_private;
725 DBG("lastclose: dev=%p", dev);
727 if (dev->mode_config.rotation_property) {
728 /* need to restore default rotation state.. not sure
729 * if there is a cleaner way to restore properties to
730 * default state? Maybe a flag that properties should
731 * automatically be restored to default state on
734 for (i = 0; i < priv->num_crtcs; i++) {
735 drm_object_property_set_value(&priv->crtcs[i]->base,
736 dev->mode_config.rotation_property, 0);
739 for (i = 0; i < priv->num_planes; i++) {
740 drm_object_property_set_value(&priv->planes[i]->base,
741 dev->mode_config.rotation_property, 0);
746 ret = drm_fb_helper_restore_fbdev_mode_unlocked(priv->fbdev);
748 DBG("failed to restore crtc mode");
752 static void dev_preclose(struct drm_device *dev, struct drm_file *file)
754 struct omap_drm_private *priv = dev->dev_private;
757 DBG("preclose: dev=%p", dev);
759 for (i = 0; i < priv->num_crtcs; ++i)
760 omap_crtc_cancel_page_flip(priv->crtcs[i], file);
763 static void dev_postclose(struct drm_device *dev, struct drm_file *file)
765 DBG("postclose: dev=%p, file=%p", dev, file);
768 static const struct vm_operations_struct omap_gem_vm_ops = {
769 .fault = omap_gem_fault,
770 .open = drm_gem_vm_open,
771 .close = drm_gem_vm_close,
774 static const struct file_operations omapdriver_fops = {
775 .owner = THIS_MODULE,
777 .unlocked_ioctl = drm_ioctl,
778 .release = drm_release,
779 .mmap = omap_gem_mmap,
782 .llseek = noop_llseek,
785 static struct drm_driver omap_drm_driver = {
786 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME,
788 .unload = dev_unload,
790 .lastclose = dev_lastclose,
791 .preclose = dev_preclose,
792 .postclose = dev_postclose,
793 .set_busid = drm_platform_set_busid,
794 .get_vblank_counter = drm_vblank_count,
795 .enable_vblank = omap_irq_enable_vblank,
796 .disable_vblank = omap_irq_disable_vblank,
797 #ifdef CONFIG_DEBUG_FS
798 .debugfs_init = omap_debugfs_init,
799 .debugfs_cleanup = omap_debugfs_cleanup,
801 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
802 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
803 .gem_prime_export = omap_gem_prime_export,
804 .gem_prime_import = omap_gem_prime_import,
805 .gem_free_object = omap_gem_free_object,
806 .gem_vm_ops = &omap_gem_vm_ops,
807 .dumb_create = omap_gem_dumb_create,
808 .dumb_map_offset = omap_gem_dumb_map_offset,
809 .dumb_destroy = drm_gem_dumb_destroy,
811 .num_ioctls = DRM_OMAP_NUM_IOCTLS,
812 .fops = &omapdriver_fops,
816 .major = DRIVER_MAJOR,
817 .minor = DRIVER_MINOR,
818 .patchlevel = DRIVER_PATCHLEVEL,
821 static int pdev_probe(struct platform_device *device)
825 if (omapdss_is_initialized() == false)
826 return -EPROBE_DEFER;
828 omap_crtc_pre_init();
830 r = omap_connect_dssdevs();
832 omap_crtc_pre_uninit();
836 DBG("%s", device->name);
837 return drm_platform_init(&omap_drm_driver, device);
840 static int pdev_remove(struct platform_device *device)
844 drm_put_dev(platform_get_drvdata(device));
846 omap_disconnect_dssdevs();
847 omap_crtc_pre_uninit();
852 #ifdef CONFIG_PM_SLEEP
853 static int omap_drm_suspend(struct device *dev)
855 struct drm_device *drm_dev = dev_get_drvdata(dev);
857 drm_kms_helper_poll_disable(drm_dev);
862 static int omap_drm_resume(struct device *dev)
864 struct drm_device *drm_dev = dev_get_drvdata(dev);
866 drm_kms_helper_poll_enable(drm_dev);
868 return omap_gem_resume(dev);
872 static SIMPLE_DEV_PM_OPS(omapdrm_pm_ops, omap_drm_suspend, omap_drm_resume);
874 static struct platform_driver pdev = {
877 .pm = &omapdrm_pm_ops,
880 .remove = pdev_remove,
883 static int __init omap_drm_init(void)
889 r = platform_driver_register(&omap_dmm_driver);
891 pr_err("DMM driver registration failed\n");
895 r = platform_driver_register(&pdev);
897 pr_err("omapdrm driver registration failed\n");
898 platform_driver_unregister(&omap_dmm_driver);
905 static void __exit omap_drm_fini(void)
909 platform_driver_unregister(&pdev);
911 platform_driver_unregister(&omap_dmm_driver);
914 /* need late_initcall() so we load after dss_driver's are loaded */
915 late_initcall(omap_drm_init);
916 module_exit(omap_drm_fini);
918 MODULE_AUTHOR("Rob Clark <rob@ti.com>");
919 MODULE_DESCRIPTION("OMAP DRM Display Driver");
920 MODULE_ALIAS("platform:" DRIVER_NAME);
921 MODULE_LICENSE("GPL v2");