2 * drivers/staging/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/>.
22 #include "drm_crtc_helper.h"
23 #include "drm_fb_helper.h"
24 #include "omap_dmm_tiler.h"
26 #define DRIVER_NAME MODULE_NAME
27 #define DRIVER_DESC "OMAP DRM"
28 #define DRIVER_DATE "20110917"
29 #define DRIVER_MAJOR 1
30 #define DRIVER_MINOR 0
31 #define DRIVER_PATCHLEVEL 0
33 static int num_crtc = CONFIG_DRM_OMAP_NUM_CRTCS;
35 MODULE_PARM_DESC(num_crtc, "Number of overlays to use as CRTCs");
36 module_param(num_crtc, int, 0600);
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_fb_output_poll_changed(struct drm_device *dev)
52 struct omap_drm_private *priv = dev->dev_private;
55 drm_fb_helper_hotplug_event(priv->fbdev);
58 static const struct drm_mode_config_funcs omap_mode_config_funcs = {
59 .fb_create = omap_framebuffer_create,
60 .output_poll_changed = omap_fb_output_poll_changed,
63 static int get_connector_type(struct omap_dss_device *dssdev)
65 switch (dssdev->type) {
66 case OMAP_DISPLAY_TYPE_HDMI:
67 return DRM_MODE_CONNECTOR_HDMIA;
68 case OMAP_DISPLAY_TYPE_DPI:
69 if (!strcmp(dssdev->name, "dvi"))
70 return DRM_MODE_CONNECTOR_DVID;
73 return DRM_MODE_CONNECTOR_Unknown;
77 #if 0 /* enable when dss2 supports hotplug */
78 static int omap_drm_notifier(struct notifier_block *nb,
79 unsigned long evt, void *arg)
82 case OMAP_DSS_SIZE_CHANGE:
83 case OMAP_DSS_HOTPLUG_CONNECT:
84 case OMAP_DSS_HOTPLUG_DISCONNECT: {
85 struct drm_device *dev = drm_device;
86 DBG("hotplug event: evt=%d, dev=%p", evt, dev);
88 drm_sysfs_hotplug_event(dev);
92 default: /* don't care about other events for now */
98 static void dump_video_chains(void)
102 DBG("dumping video chains: ");
103 for (i = 0; i < omap_dss_get_num_overlays(); i++) {
104 struct omap_overlay *ovl = omap_dss_get_overlay(i);
105 struct omap_overlay_manager *mgr = ovl->manager;
106 struct omap_dss_device *dssdev = mgr ?
107 mgr->get_device(mgr) : NULL;
109 DBG("%d: %s -> %s -> %s", i, ovl->name, mgr->name,
112 DBG("%d: %s -> %s", i, ovl->name, mgr->name);
114 DBG("%d: %s", i, ovl->name);
119 /* create encoders for each manager */
120 static int create_encoder(struct drm_device *dev,
121 struct omap_overlay_manager *mgr)
123 struct omap_drm_private *priv = dev->dev_private;
124 struct drm_encoder *encoder = omap_encoder_init(dev, mgr);
127 dev_err(dev->dev, "could not create encoder: %s\n",
132 BUG_ON(priv->num_encoders >= ARRAY_SIZE(priv->encoders));
134 priv->encoders[priv->num_encoders++] = encoder;
139 /* create connectors for each display device */
140 static int create_connector(struct drm_device *dev,
141 struct omap_dss_device *dssdev)
143 struct omap_drm_private *priv = dev->dev_private;
144 static struct notifier_block *notifier;
145 struct drm_connector *connector;
148 if (!dssdev->driver) {
149 dev_warn(dev->dev, "%s has no driver.. skipping it\n",
154 if (!(dssdev->driver->get_timings ||
155 dssdev->driver->read_edid)) {
156 dev_warn(dev->dev, "%s driver does not support "
157 "get_timings or read_edid.. skipping it!\n",
162 connector = omap_connector_init(dev,
163 get_connector_type(dssdev), dssdev);
166 dev_err(dev->dev, "could not create connector: %s\n",
171 BUG_ON(priv->num_connectors >= ARRAY_SIZE(priv->connectors));
173 priv->connectors[priv->num_connectors++] = connector;
175 #if 0 /* enable when dss2 supports hotplug */
176 notifier = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
177 notifier->notifier_call = omap_drm_notifier;
178 omap_dss_add_notify(dssdev, notifier);
183 for (j = 0; j < priv->num_encoders; j++) {
184 struct omap_overlay_manager *mgr =
185 omap_encoder_get_manager(priv->encoders[j]);
186 if (mgr->get_device(mgr) == dssdev) {
187 drm_mode_connector_attach_encoder(connector,
195 /* create up to max_overlays CRTCs mapping to overlays.. by default,
196 * connect the overlays to different managers/encoders, giving priority
197 * to encoders connected to connectors with a detected connection
199 static int create_crtc(struct drm_device *dev, struct omap_overlay *ovl,
200 int *j, unsigned int connected_connectors)
202 struct omap_drm_private *priv = dev->dev_private;
203 struct omap_overlay_manager *mgr = NULL;
204 struct drm_crtc *crtc;
206 /* find next best connector, ones with detected connection first
208 while (*j < priv->num_connectors && !mgr) {
209 if (connected_connectors & (1 << *j)) {
210 struct drm_encoder *encoder =
211 omap_connector_attached_encoder(
212 priv->connectors[*j]);
214 mgr = omap_encoder_get_manager(encoder);
220 /* if we couldn't find another connected connector, lets start
221 * looking at the unconnected connectors:
223 * note: it might not be immediately apparent, but thanks to
224 * the !mgr check in both this loop and the one above, the only
225 * way to enter this loop is with *j == priv->num_connectors,
226 * so idx can never go negative.
228 while (*j < 2 * priv->num_connectors && !mgr) {
229 int idx = *j - priv->num_connectors;
230 if (!(connected_connectors & (1 << idx))) {
231 struct drm_encoder *encoder =
232 omap_connector_attached_encoder(
233 priv->connectors[idx]);
235 mgr = omap_encoder_get_manager(encoder);
241 crtc = omap_crtc_init(dev, ovl, priv->num_crtcs);
244 dev_err(dev->dev, "could not create CRTC: %s\n",
249 BUG_ON(priv->num_crtcs >= ARRAY_SIZE(priv->crtcs));
251 priv->crtcs[priv->num_crtcs++] = crtc;
256 static int create_plane(struct drm_device *dev, struct omap_overlay *ovl,
257 unsigned int possible_crtcs)
259 struct omap_drm_private *priv = dev->dev_private;
260 struct drm_plane *plane =
261 omap_plane_init(dev, ovl, possible_crtcs, false);
264 dev_err(dev->dev, "could not create plane: %s\n",
269 BUG_ON(priv->num_planes >= ARRAY_SIZE(priv->planes));
271 priv->planes[priv->num_planes++] = plane;
276 static int match_dev_name(struct omap_dss_device *dssdev, void *data)
278 return !strcmp(dssdev->name, data);
281 static unsigned int detect_connectors(struct drm_device *dev)
283 struct omap_drm_private *priv = dev->dev_private;
284 unsigned int connected_connectors = 0;
287 for (i = 0; i < priv->num_connectors; i++) {
288 struct drm_connector *connector = priv->connectors[i];
289 if (omap_connector_detect(connector, true) ==
290 connector_status_connected) {
291 connected_connectors |= (1 << i);
295 return connected_connectors;
298 static int omap_modeset_init(struct drm_device *dev)
300 const struct omap_drm_platform_data *pdata = dev->dev->platform_data;
301 struct omap_kms_platform_data *kms_pdata = NULL;
302 struct omap_drm_private *priv = dev->dev_private;
303 struct omap_dss_device *dssdev = NULL;
305 unsigned int connected_connectors = 0;
307 drm_mode_config_init(dev);
309 if (pdata && pdata->kms_pdata) {
310 kms_pdata = pdata->kms_pdata;
312 /* if platform data is provided by the board file, use it to
313 * control which overlays, managers, and devices we own.
315 for (i = 0; i < kms_pdata->mgr_cnt; i++) {
316 struct omap_overlay_manager *mgr =
317 omap_dss_get_overlay_manager(
318 kms_pdata->mgr_ids[i]);
319 create_encoder(dev, mgr);
322 for (i = 0; i < kms_pdata->dev_cnt; i++) {
323 struct omap_dss_device *dssdev =
324 omap_dss_find_device(
325 (void *)kms_pdata->dev_names[i],
328 dev_warn(dev->dev, "no such dssdev: %s\n",
329 kms_pdata->dev_names[i]);
332 create_connector(dev, dssdev);
335 connected_connectors = detect_connectors(dev);
338 for (i = 0; i < kms_pdata->ovl_cnt; i++) {
339 struct omap_overlay *ovl =
340 omap_dss_get_overlay(kms_pdata->ovl_ids[i]);
341 create_crtc(dev, ovl, &j, connected_connectors);
344 for (i = 0; i < kms_pdata->pln_cnt; i++) {
345 struct omap_overlay *ovl =
346 omap_dss_get_overlay(kms_pdata->pln_ids[i]);
347 create_plane(dev, ovl, (1 << priv->num_crtcs) - 1);
350 /* otherwise just grab up to CONFIG_DRM_OMAP_NUM_CRTCS and try
351 * to make educated guesses about everything else
353 int max_overlays = min(omap_dss_get_num_overlays(), num_crtc);
355 for (i = 0; i < omap_dss_get_num_overlay_managers(); i++)
356 create_encoder(dev, omap_dss_get_overlay_manager(i));
358 for_each_dss_dev(dssdev) {
359 create_connector(dev, dssdev);
362 connected_connectors = detect_connectors(dev);
365 for (i = 0; i < max_overlays; i++) {
366 create_crtc(dev, omap_dss_get_overlay(i),
367 &j, connected_connectors);
370 /* use any remaining overlays as drm planes */
371 for (; i < omap_dss_get_num_overlays(); i++) {
372 struct omap_overlay *ovl = omap_dss_get_overlay(i);
373 create_plane(dev, ovl, (1 << priv->num_crtcs) - 1);
377 /* for now keep the mapping of CRTCs and encoders static.. */
378 for (i = 0; i < priv->num_encoders; i++) {
379 struct drm_encoder *encoder = priv->encoders[i];
380 struct omap_overlay_manager *mgr =
381 omap_encoder_get_manager(encoder);
383 encoder->possible_crtcs = (1 << priv->num_crtcs) - 1;
385 DBG("%s: possible_crtcs=%08x", mgr->name,
386 encoder->possible_crtcs);
391 dev->mode_config.min_width = 32;
392 dev->mode_config.min_height = 32;
394 /* note: eventually will need some cpu_is_omapXYZ() type stuff here
395 * to fill in these limits properly on different OMAP generations..
397 dev->mode_config.max_width = 2048;
398 dev->mode_config.max_height = 2048;
400 dev->mode_config.funcs = &omap_mode_config_funcs;
405 static void omap_modeset_free(struct drm_device *dev)
407 drm_mode_config_cleanup(dev);
415 static int ioctl_get_param(struct drm_device *dev, void *data,
416 struct drm_file *file_priv)
418 struct omap_drm_private *priv = dev->dev_private;
419 struct drm_omap_param *args = data;
421 DBG("%p: param=%llu", dev, args->param);
423 switch (args->param) {
424 case OMAP_PARAM_CHIPSET_ID:
425 args->value = priv->omaprev;
428 DBG("unknown parameter %lld", args->param);
435 static int ioctl_set_param(struct drm_device *dev, void *data,
436 struct drm_file *file_priv)
438 struct drm_omap_param *args = data;
440 switch (args->param) {
442 DBG("unknown parameter %lld", args->param);
449 static int ioctl_gem_new(struct drm_device *dev, void *data,
450 struct drm_file *file_priv)
452 struct drm_omap_gem_new *args = data;
453 DBG("%p:%p: size=0x%08x, flags=%08x", dev, file_priv,
454 args->size.bytes, args->flags);
455 return omap_gem_new_handle(dev, file_priv, args->size,
456 args->flags, &args->handle);
459 static int ioctl_gem_cpu_prep(struct drm_device *dev, void *data,
460 struct drm_file *file_priv)
462 struct drm_omap_gem_cpu_prep *args = data;
463 struct drm_gem_object *obj;
466 VERB("%p:%p: handle=%d, op=%x", dev, file_priv, args->handle, args->op);
468 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
472 ret = omap_gem_op_sync(obj, args->op);
475 ret = omap_gem_op_start(obj, args->op);
477 drm_gem_object_unreference_unlocked(obj);
482 static int ioctl_gem_cpu_fini(struct drm_device *dev, void *data,
483 struct drm_file *file_priv)
485 struct drm_omap_gem_cpu_fini *args = data;
486 struct drm_gem_object *obj;
489 VERB("%p:%p: handle=%d", dev, file_priv, args->handle);
491 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
495 /* XXX flushy, flushy */
499 ret = omap_gem_op_finish(obj, args->op);
501 drm_gem_object_unreference_unlocked(obj);
506 static int ioctl_gem_info(struct drm_device *dev, void *data,
507 struct drm_file *file_priv)
509 struct drm_omap_gem_info *args = data;
510 struct drm_gem_object *obj;
513 DBG("%p:%p: handle=%d", dev, file_priv, args->handle);
515 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
519 args->size = omap_gem_mmap_size(obj);
520 args->offset = omap_gem_mmap_offset(obj);
522 drm_gem_object_unreference_unlocked(obj);
527 struct drm_ioctl_desc ioctls[DRM_COMMAND_END - DRM_COMMAND_BASE] = {
528 DRM_IOCTL_DEF_DRV(OMAP_GET_PARAM, ioctl_get_param, DRM_UNLOCKED|DRM_AUTH),
529 DRM_IOCTL_DEF_DRV(OMAP_SET_PARAM, ioctl_set_param, DRM_UNLOCKED|DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
530 DRM_IOCTL_DEF_DRV(OMAP_GEM_NEW, ioctl_gem_new, DRM_UNLOCKED|DRM_AUTH),
531 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_PREP, ioctl_gem_cpu_prep, DRM_UNLOCKED|DRM_AUTH),
532 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_FINI, ioctl_gem_cpu_fini, DRM_UNLOCKED|DRM_AUTH),
533 DRM_IOCTL_DEF_DRV(OMAP_GEM_INFO, ioctl_gem_info, DRM_UNLOCKED|DRM_AUTH),
541 * load - setup chip and create an initial config
543 * @flags: startup flags
545 * The driver load routine has to do several things:
546 * - initialize the memory manager
547 * - allocate initial config memory
548 * - setup the DRM framebuffer with the allocated memory
550 static int dev_load(struct drm_device *dev, unsigned long flags)
552 struct omap_drm_platform_data *pdata = dev->dev->platform_data;
553 struct omap_drm_private *priv;
556 DBG("load: dev=%p", dev);
558 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
560 dev_err(dev->dev, "could not allocate priv\n");
564 priv->omaprev = pdata->omaprev;
566 dev->dev_private = priv;
568 ret = omapdss_compat_init();
570 dev_err(dev->dev, "coult not init omapdss\n");
571 dev->dev_private = NULL;
576 priv->wq = alloc_ordered_workqueue("omapdrm", 0);
578 INIT_LIST_HEAD(&priv->obj_list);
582 ret = omap_modeset_init(dev);
584 dev_err(dev->dev, "omap_modeset_init failed: ret=%d\n", ret);
585 dev->dev_private = NULL;
587 omapdss_compat_uninit();
591 priv->fbdev = omap_fbdev_init(dev);
593 dev_warn(dev->dev, "omap_fbdev_init failed\n");
594 /* well, limp along without an fbdev.. maybe X11 will work? */
597 drm_kms_helper_poll_init(dev);
599 ret = drm_vblank_init(dev, priv->num_crtcs);
601 dev_warn(dev->dev, "could not init vblank\n");
606 static int dev_unload(struct drm_device *dev)
608 struct omap_drm_private *priv = dev->dev_private;
610 DBG("unload: dev=%p", dev);
612 drm_vblank_cleanup(dev);
613 drm_kms_helper_poll_fini(dev);
615 omap_fbdev_free(dev);
616 omap_modeset_free(dev);
617 omap_gem_deinit(dev);
619 flush_workqueue(priv->wq);
620 destroy_workqueue(priv->wq);
622 omapdss_compat_uninit();
624 kfree(dev->dev_private);
625 dev->dev_private = NULL;
630 static int dev_open(struct drm_device *dev, struct drm_file *file)
632 file->driver_priv = NULL;
634 DBG("open: dev=%p, file=%p", dev, file);
639 static int dev_firstopen(struct drm_device *dev)
641 DBG("firstopen: dev=%p", dev);
646 * lastclose - clean up after all DRM clients have exited
649 * Take care of cleaning up after all DRM clients have exited. In the
650 * mode setting case, we want to restore the kernel's initial mode (just
651 * in case the last client left us in a bad state).
653 static void dev_lastclose(struct drm_device *dev)
657 /* we don't support vga-switcheroo.. so just make sure the fbdev
660 struct omap_drm_private *priv = dev->dev_private;
663 DBG("lastclose: dev=%p", dev);
665 if (priv->rotation_prop) {
666 /* need to restore default rotation state.. not sure
667 * if there is a cleaner way to restore properties to
668 * default state? Maybe a flag that properties should
669 * automatically be restored to default state on
672 for (i = 0; i < priv->num_crtcs; i++) {
673 drm_object_property_set_value(&priv->crtcs[i]->base,
674 priv->rotation_prop, 0);
677 for (i = 0; i < priv->num_planes; i++) {
678 drm_object_property_set_value(&priv->planes[i]->base,
679 priv->rotation_prop, 0);
683 ret = drm_fb_helper_restore_fbdev_mode(priv->fbdev);
685 DBG("failed to restore crtc mode");
688 static void dev_preclose(struct drm_device *dev, struct drm_file *file)
690 DBG("preclose: dev=%p", dev);
693 static void dev_postclose(struct drm_device *dev, struct drm_file *file)
695 DBG("postclose: dev=%p, file=%p", dev, file);
699 * enable_vblank - enable vblank interrupt events
701 * @crtc: which irq to enable
703 * Enable vblank interrupts for @crtc. If the device doesn't have
704 * a hardware vblank counter, this routine should be a no-op, since
705 * interrupts will have to stay on to keep the count accurate.
708 * Zero on success, appropriate errno if the given @crtc's vblank
709 * interrupt cannot be enabled.
711 static int dev_enable_vblank(struct drm_device *dev, int crtc)
713 DBG("enable_vblank: dev=%p, crtc=%d", dev, crtc);
718 * disable_vblank - disable vblank interrupt events
720 * @crtc: which irq to enable
722 * Disable vblank interrupts for @crtc. If the device doesn't have
723 * a hardware vblank counter, this routine should be a no-op, since
724 * interrupts will have to stay on to keep the count accurate.
726 static void dev_disable_vblank(struct drm_device *dev, int crtc)
728 DBG("disable_vblank: dev=%p, crtc=%d", dev, crtc);
731 static irqreturn_t dev_irq_handler(DRM_IRQ_ARGS)
736 static void dev_irq_preinstall(struct drm_device *dev)
738 DBG("irq_preinstall: dev=%p", dev);
741 static int dev_irq_postinstall(struct drm_device *dev)
743 DBG("irq_postinstall: dev=%p", dev);
747 static void dev_irq_uninstall(struct drm_device *dev)
749 DBG("irq_uninstall: dev=%p", dev);
752 static const struct vm_operations_struct omap_gem_vm_ops = {
753 .fault = omap_gem_fault,
754 .open = drm_gem_vm_open,
755 .close = drm_gem_vm_close,
758 static const struct file_operations omapdriver_fops = {
759 .owner = THIS_MODULE,
761 .unlocked_ioctl = drm_ioctl,
762 .release = drm_release,
763 .mmap = omap_gem_mmap,
765 .fasync = drm_fasync,
767 .llseek = noop_llseek,
770 static struct drm_driver omap_drm_driver = {
772 DRIVER_HAVE_IRQ | DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME,
774 .unload = dev_unload,
776 .firstopen = dev_firstopen,
777 .lastclose = dev_lastclose,
778 .preclose = dev_preclose,
779 .postclose = dev_postclose,
780 .get_vblank_counter = drm_vblank_count,
781 .enable_vblank = dev_enable_vblank,
782 .disable_vblank = dev_disable_vblank,
783 .irq_preinstall = dev_irq_preinstall,
784 .irq_postinstall = dev_irq_postinstall,
785 .irq_uninstall = dev_irq_uninstall,
786 .irq_handler = dev_irq_handler,
787 #ifdef CONFIG_DEBUG_FS
788 .debugfs_init = omap_debugfs_init,
789 .debugfs_cleanup = omap_debugfs_cleanup,
791 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
792 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
793 .gem_prime_export = omap_gem_prime_export,
794 .gem_prime_import = omap_gem_prime_import,
795 .gem_init_object = omap_gem_init_object,
796 .gem_free_object = omap_gem_free_object,
797 .gem_vm_ops = &omap_gem_vm_ops,
798 .dumb_create = omap_gem_dumb_create,
799 .dumb_map_offset = omap_gem_dumb_map_offset,
800 .dumb_destroy = omap_gem_dumb_destroy,
802 .num_ioctls = DRM_OMAP_NUM_IOCTLS,
803 .fops = &omapdriver_fops,
807 .major = DRIVER_MAJOR,
808 .minor = DRIVER_MINOR,
809 .patchlevel = DRIVER_PATCHLEVEL,
812 static int pdev_suspend(struct platform_device *pDevice, pm_message_t state)
818 static int pdev_resume(struct platform_device *device)
824 static void pdev_shutdown(struct platform_device *device)
829 static int pdev_probe(struct platform_device *device)
831 DBG("%s", device->name);
832 return drm_platform_init(&omap_drm_driver, device);
835 static int pdev_remove(struct platform_device *device)
838 drm_platform_exit(&omap_drm_driver, device);
840 platform_driver_unregister(&omap_dmm_driver);
844 struct platform_driver pdev = {
847 .owner = THIS_MODULE,
850 .remove = pdev_remove,
851 .suspend = pdev_suspend,
852 .resume = pdev_resume,
853 .shutdown = pdev_shutdown,
856 static int __init omap_drm_init(void)
859 if (platform_driver_register(&omap_dmm_driver)) {
860 /* we can continue on without DMM.. so not fatal */
861 dev_err(NULL, "DMM registration failed\n");
863 return platform_driver_register(&pdev);
866 static void __exit omap_drm_fini(void)
869 platform_driver_unregister(&pdev);
872 /* need late_initcall() so we load after dss_driver's are loaded */
873 late_initcall(omap_drm_init);
874 module_exit(omap_drm_fini);
876 MODULE_AUTHOR("Rob Clark <rob@ti.com>");
877 MODULE_DESCRIPTION("OMAP DRM Display Driver");
878 MODULE_ALIAS("platform:" DRIVER_NAME);
879 MODULE_LICENSE("GPL v2");