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/>.
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 static bool channel_used(struct drm_device
*dev
, enum omap_channel channel
)
79 struct omap_drm_private
*priv
= dev
->dev_private
;
82 for (i
= 0; i
< priv
->num_crtcs
; i
++) {
83 struct drm_crtc
*crtc
= priv
->crtcs
[i
];
85 if (omap_crtc_channel(crtc
) == channel
)
92 static int omap_modeset_init(struct drm_device
*dev
)
94 struct omap_drm_private
*priv
= dev
->dev_private
;
95 struct omap_dss_device
*dssdev
= NULL
;
96 int num_ovls
= dss_feat_get_num_ovls();
97 int num_mgrs
= dss_feat_get_num_mgrs();
101 omap_crtc_pre_init();
103 drm_mode_config_init(dev
);
105 omap_drm_irq_install(dev
);
108 * We usually don't want to create a CRTC for each manager, at least
109 * not until we have a way to expose private planes to userspace.
110 * Otherwise there would not be enough video pipes left for drm planes.
111 * We use the num_crtc argument to limit the number of crtcs we create.
113 num_crtcs
= min3(num_crtc
, num_mgrs
, num_ovls
);
117 for_each_dss_dev(dssdev
) {
118 struct drm_connector
*connector
;
119 struct drm_encoder
*encoder
;
120 enum omap_channel channel
;
122 if (!dssdev
->driver
) {
123 dev_warn(dev
->dev
, "%s has no driver.. skipping it\n",
128 if (!(dssdev
->driver
->get_timings
||
129 dssdev
->driver
->read_edid
)) {
130 dev_warn(dev
->dev
, "%s driver does not support "
131 "get_timings or read_edid.. skipping it!\n",
136 encoder
= omap_encoder_init(dev
, dssdev
);
139 dev_err(dev
->dev
, "could not create encoder: %s\n",
144 connector
= omap_connector_init(dev
,
145 get_connector_type(dssdev
), dssdev
, encoder
);
148 dev_err(dev
->dev
, "could not create connector: %s\n",
153 BUG_ON(priv
->num_encoders
>= ARRAY_SIZE(priv
->encoders
));
154 BUG_ON(priv
->num_connectors
>= ARRAY_SIZE(priv
->connectors
));
156 priv
->encoders
[priv
->num_encoders
++] = encoder
;
157 priv
->connectors
[priv
->num_connectors
++] = connector
;
159 drm_mode_connector_attach_encoder(connector
, encoder
);
162 * if we have reached the limit of the crtcs we are allowed to
163 * create, let's not try to look for a crtc for this
164 * panel/encoder and onwards, we will, of course, populate the
165 * the possible_crtcs field for all the encoders with the final
166 * set of crtcs we create
172 * get the recommended DISPC channel for this encoder. For now,
173 * we only try to get create a crtc out of the recommended, the
174 * other possible channels to which the encoder can connect are
177 channel
= dssdev
->output
->dispc_channel
;
180 * if this channel hasn't already been taken by a previously
181 * allocated crtc, we create a new crtc for it
183 if (!channel_used(dev
, channel
)) {
184 struct drm_plane
*plane
;
185 struct drm_crtc
*crtc
;
187 plane
= omap_plane_init(dev
, id
, true);
188 crtc
= omap_crtc_init(dev
, plane
, channel
, id
);
190 BUG_ON(priv
->num_crtcs
>= ARRAY_SIZE(priv
->crtcs
));
191 priv
->crtcs
[id
] = crtc
;
194 priv
->planes
[id
] = plane
;
202 * we have allocated crtcs according to the need of the panels/encoders,
203 * adding more crtcs here if needed
205 for (; id
< num_crtcs
; id
++) {
207 /* find a free manager for this crtc */
208 for (i
= 0; i
< num_mgrs
; i
++) {
209 if (!channel_used(dev
, i
)) {
210 struct drm_plane
*plane
;
211 struct drm_crtc
*crtc
;
213 plane
= omap_plane_init(dev
, id
, true);
214 crtc
= omap_crtc_init(dev
, plane
, i
, id
);
216 BUG_ON(priv
->num_crtcs
>=
217 ARRAY_SIZE(priv
->crtcs
));
219 priv
->crtcs
[id
] = crtc
;
222 priv
->planes
[id
] = plane
;
232 /* this shouldn't really happen */
233 dev_err(dev
->dev
, "no managers left for crtc\n");
239 * Create normal planes for the remaining overlays:
241 for (; id
< num_ovls
; id
++) {
242 struct drm_plane
*plane
= omap_plane_init(dev
, id
, false);
244 BUG_ON(priv
->num_planes
>= ARRAY_SIZE(priv
->planes
));
245 priv
->planes
[priv
->num_planes
++] = plane
;
248 for (i
= 0; i
< priv
->num_encoders
; i
++) {
249 struct drm_encoder
*encoder
= priv
->encoders
[i
];
250 struct omap_dss_device
*dssdev
=
251 omap_encoder_get_dssdev(encoder
);
252 struct omap_dss_output
*output
;
254 output
= omapdss_find_output_from_display(dssdev
);
256 /* figure out which crtc's we can connect the encoder to: */
257 encoder
->possible_crtcs
= 0;
258 for (id
= 0; id
< priv
->num_crtcs
; id
++) {
259 struct drm_crtc
*crtc
= priv
->crtcs
[id
];
260 enum omap_channel crtc_channel
;
261 enum omap_dss_output_id supported_outputs
;
263 crtc_channel
= omap_crtc_channel(crtc
);
265 dss_feat_get_supported_outputs(crtc_channel
);
267 if (supported_outputs
& output
->id
)
268 encoder
->possible_crtcs
|= (1 << id
);
272 DBG("registered %d planes, %d crtcs, %d encoders and %d connectors\n",
273 priv
->num_planes
, priv
->num_crtcs
, priv
->num_encoders
,
274 priv
->num_connectors
);
276 dev
->mode_config
.min_width
= 32;
277 dev
->mode_config
.min_height
= 32;
279 /* note: eventually will need some cpu_is_omapXYZ() type stuff here
280 * to fill in these limits properly on different OMAP generations..
282 dev
->mode_config
.max_width
= 2048;
283 dev
->mode_config
.max_height
= 2048;
285 dev
->mode_config
.funcs
= &omap_mode_config_funcs
;
290 static void omap_modeset_free(struct drm_device
*dev
)
292 drm_mode_config_cleanup(dev
);
300 static int ioctl_get_param(struct drm_device
*dev
, void *data
,
301 struct drm_file
*file_priv
)
303 struct omap_drm_private
*priv
= dev
->dev_private
;
304 struct drm_omap_param
*args
= data
;
306 DBG("%p: param=%llu", dev
, args
->param
);
308 switch (args
->param
) {
309 case OMAP_PARAM_CHIPSET_ID
:
310 args
->value
= priv
->omaprev
;
313 DBG("unknown parameter %lld", args
->param
);
320 static int ioctl_set_param(struct drm_device
*dev
, void *data
,
321 struct drm_file
*file_priv
)
323 struct drm_omap_param
*args
= data
;
325 switch (args
->param
) {
327 DBG("unknown parameter %lld", args
->param
);
334 static int ioctl_gem_new(struct drm_device
*dev
, void *data
,
335 struct drm_file
*file_priv
)
337 struct drm_omap_gem_new
*args
= data
;
338 VERB("%p:%p: size=0x%08x, flags=%08x", dev
, file_priv
,
339 args
->size
.bytes
, args
->flags
);
340 return omap_gem_new_handle(dev
, file_priv
, args
->size
,
341 args
->flags
, &args
->handle
);
344 static int ioctl_gem_cpu_prep(struct drm_device
*dev
, void *data
,
345 struct drm_file
*file_priv
)
347 struct drm_omap_gem_cpu_prep
*args
= data
;
348 struct drm_gem_object
*obj
;
351 VERB("%p:%p: handle=%d, op=%x", dev
, file_priv
, args
->handle
, args
->op
);
353 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
357 ret
= omap_gem_op_sync(obj
, args
->op
);
360 ret
= omap_gem_op_start(obj
, args
->op
);
362 drm_gem_object_unreference_unlocked(obj
);
367 static int ioctl_gem_cpu_fini(struct drm_device
*dev
, void *data
,
368 struct drm_file
*file_priv
)
370 struct drm_omap_gem_cpu_fini
*args
= data
;
371 struct drm_gem_object
*obj
;
374 VERB("%p:%p: handle=%d", dev
, file_priv
, args
->handle
);
376 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
380 /* XXX flushy, flushy */
384 ret
= omap_gem_op_finish(obj
, args
->op
);
386 drm_gem_object_unreference_unlocked(obj
);
391 static int ioctl_gem_info(struct drm_device
*dev
, void *data
,
392 struct drm_file
*file_priv
)
394 struct drm_omap_gem_info
*args
= data
;
395 struct drm_gem_object
*obj
;
398 VERB("%p:%p: handle=%d", dev
, file_priv
, args
->handle
);
400 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
404 args
->size
= omap_gem_mmap_size(obj
);
405 args
->offset
= omap_gem_mmap_offset(obj
);
407 drm_gem_object_unreference_unlocked(obj
);
412 static struct drm_ioctl_desc ioctls
[DRM_COMMAND_END
- DRM_COMMAND_BASE
] = {
413 DRM_IOCTL_DEF_DRV(OMAP_GET_PARAM
, ioctl_get_param
, DRM_UNLOCKED
|DRM_AUTH
),
414 DRM_IOCTL_DEF_DRV(OMAP_SET_PARAM
, ioctl_set_param
, DRM_UNLOCKED
|DRM_AUTH
|DRM_MASTER
|DRM_ROOT_ONLY
),
415 DRM_IOCTL_DEF_DRV(OMAP_GEM_NEW
, ioctl_gem_new
, DRM_UNLOCKED
|DRM_AUTH
),
416 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_PREP
, ioctl_gem_cpu_prep
, DRM_UNLOCKED
|DRM_AUTH
),
417 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_FINI
, ioctl_gem_cpu_fini
, DRM_UNLOCKED
|DRM_AUTH
),
418 DRM_IOCTL_DEF_DRV(OMAP_GEM_INFO
, ioctl_gem_info
, DRM_UNLOCKED
|DRM_AUTH
),
426 * load - setup chip and create an initial config
428 * @flags: startup flags
430 * The driver load routine has to do several things:
431 * - initialize the memory manager
432 * - allocate initial config memory
433 * - setup the DRM framebuffer with the allocated memory
435 static int dev_load(struct drm_device
*dev
, unsigned long flags
)
437 struct omap_drm_platform_data
*pdata
= dev
->dev
->platform_data
;
438 struct omap_drm_private
*priv
;
441 DBG("load: dev=%p", dev
);
443 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
447 priv
->omaprev
= pdata
->omaprev
;
449 dev
->dev_private
= priv
;
451 priv
->wq
= alloc_ordered_workqueue("omapdrm", 0);
453 INIT_LIST_HEAD(&priv
->obj_list
);
457 ret
= omap_modeset_init(dev
);
459 dev_err(dev
->dev
, "omap_modeset_init failed: ret=%d\n", ret
);
460 dev
->dev_private
= NULL
;
465 ret
= drm_vblank_init(dev
, priv
->num_crtcs
);
467 dev_warn(dev
->dev
, "could not init vblank\n");
469 priv
->fbdev
= omap_fbdev_init(dev
);
471 dev_warn(dev
->dev
, "omap_fbdev_init failed\n");
472 /* well, limp along without an fbdev.. maybe X11 will work? */
475 /* store off drm_device for use in pm ops */
476 dev_set_drvdata(dev
->dev
, dev
);
478 drm_kms_helper_poll_init(dev
);
483 static int dev_unload(struct drm_device
*dev
)
485 struct omap_drm_private
*priv
= dev
->dev_private
;
487 DBG("unload: dev=%p", dev
);
489 drm_kms_helper_poll_fini(dev
);
490 drm_vblank_cleanup(dev
);
491 omap_drm_irq_uninstall(dev
);
493 omap_fbdev_free(dev
);
494 omap_modeset_free(dev
);
495 omap_gem_deinit(dev
);
497 flush_workqueue(priv
->wq
);
498 destroy_workqueue(priv
->wq
);
500 kfree(dev
->dev_private
);
501 dev
->dev_private
= NULL
;
503 dev_set_drvdata(dev
->dev
, NULL
);
508 static int dev_open(struct drm_device
*dev
, struct drm_file
*file
)
510 file
->driver_priv
= NULL
;
512 DBG("open: dev=%p, file=%p", dev
, file
);
517 static int dev_firstopen(struct drm_device
*dev
)
519 DBG("firstopen: dev=%p", dev
);
524 * lastclose - clean up after all DRM clients have exited
527 * Take care of cleaning up after all DRM clients have exited. In the
528 * mode setting case, we want to restore the kernel's initial mode (just
529 * in case the last client left us in a bad state).
531 static void dev_lastclose(struct drm_device
*dev
)
535 /* we don't support vga-switcheroo.. so just make sure the fbdev
538 struct omap_drm_private
*priv
= dev
->dev_private
;
541 DBG("lastclose: dev=%p", dev
);
543 if (priv
->rotation_prop
) {
544 /* need to restore default rotation state.. not sure
545 * if there is a cleaner way to restore properties to
546 * default state? Maybe a flag that properties should
547 * automatically be restored to default state on
550 for (i
= 0; i
< priv
->num_crtcs
; i
++) {
551 drm_object_property_set_value(&priv
->crtcs
[i
]->base
,
552 priv
->rotation_prop
, 0);
555 for (i
= 0; i
< priv
->num_planes
; i
++) {
556 drm_object_property_set_value(&priv
->planes
[i
]->base
,
557 priv
->rotation_prop
, 0);
561 drm_modeset_lock_all(dev
);
562 ret
= drm_fb_helper_restore_fbdev_mode(priv
->fbdev
);
563 drm_modeset_unlock_all(dev
);
565 DBG("failed to restore crtc mode");
568 static void dev_preclose(struct drm_device
*dev
, struct drm_file
*file
)
570 DBG("preclose: dev=%p", dev
);
573 static void dev_postclose(struct drm_device
*dev
, struct drm_file
*file
)
575 DBG("postclose: dev=%p, file=%p", dev
, file
);
578 static const struct vm_operations_struct omap_gem_vm_ops
= {
579 .fault
= omap_gem_fault
,
580 .open
= drm_gem_vm_open
,
581 .close
= drm_gem_vm_close
,
584 static const struct file_operations omapdriver_fops
= {
585 .owner
= THIS_MODULE
,
587 .unlocked_ioctl
= drm_ioctl
,
588 .release
= drm_release
,
589 .mmap
= omap_gem_mmap
,
591 .fasync
= drm_fasync
,
593 .llseek
= noop_llseek
,
596 static struct drm_driver omap_drm_driver
= {
598 DRIVER_HAVE_IRQ
| DRIVER_MODESET
| DRIVER_GEM
| DRIVER_PRIME
,
600 .unload
= dev_unload
,
602 .firstopen
= dev_firstopen
,
603 .lastclose
= dev_lastclose
,
604 .preclose
= dev_preclose
,
605 .postclose
= dev_postclose
,
606 .get_vblank_counter
= drm_vblank_count
,
607 .enable_vblank
= omap_irq_enable_vblank
,
608 .disable_vblank
= omap_irq_disable_vblank
,
609 .irq_preinstall
= omap_irq_preinstall
,
610 .irq_postinstall
= omap_irq_postinstall
,
611 .irq_uninstall
= omap_irq_uninstall
,
612 .irq_handler
= omap_irq_handler
,
613 #ifdef CONFIG_DEBUG_FS
614 .debugfs_init
= omap_debugfs_init
,
615 .debugfs_cleanup
= omap_debugfs_cleanup
,
617 .prime_handle_to_fd
= drm_gem_prime_handle_to_fd
,
618 .prime_fd_to_handle
= drm_gem_prime_fd_to_handle
,
619 .gem_prime_export
= omap_gem_prime_export
,
620 .gem_prime_import
= omap_gem_prime_import
,
621 .gem_init_object
= omap_gem_init_object
,
622 .gem_free_object
= omap_gem_free_object
,
623 .gem_vm_ops
= &omap_gem_vm_ops
,
624 .dumb_create
= omap_gem_dumb_create
,
625 .dumb_map_offset
= omap_gem_dumb_map_offset
,
626 .dumb_destroy
= omap_gem_dumb_destroy
,
628 .num_ioctls
= DRM_OMAP_NUM_IOCTLS
,
629 .fops
= &omapdriver_fops
,
633 .major
= DRIVER_MAJOR
,
634 .minor
= DRIVER_MINOR
,
635 .patchlevel
= DRIVER_PATCHLEVEL
,
638 static int pdev_suspend(struct platform_device
*pDevice
, pm_message_t state
)
644 static int pdev_resume(struct platform_device
*device
)
650 static void pdev_shutdown(struct platform_device
*device
)
655 static int pdev_probe(struct platform_device
*device
)
657 if (omapdss_is_initialized() == false)
658 return -EPROBE_DEFER
;
660 DBG("%s", device
->name
);
661 return drm_platform_init(&omap_drm_driver
, device
);
664 static int pdev_remove(struct platform_device
*device
)
667 drm_platform_exit(&omap_drm_driver
, device
);
669 platform_driver_unregister(&omap_dmm_driver
);
674 static const struct dev_pm_ops omapdrm_pm_ops
= {
675 .resume
= omap_gem_resume
,
679 static struct platform_driver pdev
= {
682 .owner
= THIS_MODULE
,
684 .pm
= &omapdrm_pm_ops
,
688 .remove
= pdev_remove
,
689 .suspend
= pdev_suspend
,
690 .resume
= pdev_resume
,
691 .shutdown
= pdev_shutdown
,
694 static int __init
omap_drm_init(void)
697 if (platform_driver_register(&omap_dmm_driver
)) {
698 /* we can continue on without DMM.. so not fatal */
699 dev_err(NULL
, "DMM registration failed\n");
701 return platform_driver_register(&pdev
);
704 static void __exit
omap_drm_fini(void)
707 platform_driver_unregister(&pdev
);
710 /* need late_initcall() so we load after dss_driver's are loaded */
711 late_initcall(omap_drm_init
);
712 module_exit(omap_drm_fini
);
714 MODULE_AUTHOR("Rob Clark <rob@ti.com>");
715 MODULE_DESCRIPTION("OMAP DRM Display Driver");
716 MODULE_ALIAS("platform:" DRIVER_NAME
);
717 MODULE_LICENSE("GPL v2");