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 drm_mode_config_init(dev
);
103 omap_drm_irq_install(dev
);
106 * We usually don't want to create a CRTC for each manager, at least
107 * not until we have a way to expose private planes to userspace.
108 * Otherwise there would not be enough video pipes left for drm planes.
109 * We use the num_crtc argument to limit the number of crtcs we create.
111 num_crtcs
= min3(num_crtc
, num_mgrs
, num_ovls
);
115 for_each_dss_dev(dssdev
) {
116 struct drm_connector
*connector
;
117 struct drm_encoder
*encoder
;
118 enum omap_channel channel
;
120 if (!dssdev
->driver
) {
121 dev_warn(dev
->dev
, "%s has no driver.. skipping it\n",
126 if (!(dssdev
->driver
->get_timings
||
127 dssdev
->driver
->read_edid
)) {
128 dev_warn(dev
->dev
, "%s driver does not support "
129 "get_timings or read_edid.. skipping it!\n",
134 encoder
= omap_encoder_init(dev
, dssdev
);
137 dev_err(dev
->dev
, "could not create encoder: %s\n",
142 connector
= omap_connector_init(dev
,
143 get_connector_type(dssdev
), dssdev
, encoder
);
146 dev_err(dev
->dev
, "could not create connector: %s\n",
151 BUG_ON(priv
->num_encoders
>= ARRAY_SIZE(priv
->encoders
));
152 BUG_ON(priv
->num_connectors
>= ARRAY_SIZE(priv
->connectors
));
154 priv
->encoders
[priv
->num_encoders
++] = encoder
;
155 priv
->connectors
[priv
->num_connectors
++] = connector
;
157 drm_mode_connector_attach_encoder(connector
, encoder
);
160 * if we have reached the limit of the crtcs we are allowed to
161 * create, let's not try to look for a crtc for this
162 * panel/encoder and onwards, we will, of course, populate the
163 * the possible_crtcs field for all the encoders with the final
164 * set of crtcs we create
170 * get the recommended DISPC channel for this encoder. For now,
171 * we only try to get create a crtc out of the recommended, the
172 * other possible channels to which the encoder can connect are
175 channel
= dssdev
->output
->dispc_channel
;
178 * if this channel hasn't already been taken by a previously
179 * allocated crtc, we create a new crtc for it
181 if (!channel_used(dev
, channel
)) {
182 struct drm_plane
*plane
;
183 struct drm_crtc
*crtc
;
185 plane
= omap_plane_init(dev
, id
, true);
186 crtc
= omap_crtc_init(dev
, plane
, channel
, id
);
188 BUG_ON(priv
->num_crtcs
>= ARRAY_SIZE(priv
->crtcs
));
189 priv
->crtcs
[id
] = crtc
;
192 priv
->planes
[id
] = plane
;
200 * we have allocated crtcs according to the need of the panels/encoders,
201 * adding more crtcs here if needed
203 for (; id
< num_crtcs
; id
++) {
205 /* find a free manager for this crtc */
206 for (i
= 0; i
< num_mgrs
; i
++) {
207 if (!channel_used(dev
, i
)) {
208 struct drm_plane
*plane
;
209 struct drm_crtc
*crtc
;
211 plane
= omap_plane_init(dev
, id
, true);
212 crtc
= omap_crtc_init(dev
, plane
, i
, id
);
214 BUG_ON(priv
->num_crtcs
>=
215 ARRAY_SIZE(priv
->crtcs
));
217 priv
->crtcs
[id
] = crtc
;
220 priv
->planes
[id
] = plane
;
230 /* this shouldn't really happen */
231 dev_err(dev
->dev
, "no managers left for crtc\n");
237 * Create normal planes for the remaining overlays:
239 for (; id
< num_ovls
; id
++) {
240 struct drm_plane
*plane
= omap_plane_init(dev
, id
, false);
242 BUG_ON(priv
->num_planes
>= ARRAY_SIZE(priv
->planes
));
243 priv
->planes
[priv
->num_planes
++] = plane
;
246 for (i
= 0; i
< priv
->num_encoders
; i
++) {
247 struct drm_encoder
*encoder
= priv
->encoders
[i
];
248 struct omap_dss_device
*dssdev
=
249 omap_encoder_get_dssdev(encoder
);
251 /* figure out which crtc's we can connect the encoder to: */
252 encoder
->possible_crtcs
= 0;
253 for (id
= 0; id
< priv
->num_crtcs
; id
++) {
254 struct drm_crtc
*crtc
= priv
->crtcs
[id
];
255 enum omap_channel crtc_channel
;
256 enum omap_dss_output_id supported_outputs
;
258 crtc_channel
= omap_crtc_channel(crtc
);
260 dss_feat_get_supported_outputs(crtc_channel
);
262 if (supported_outputs
& dssdev
->output
->id
)
263 encoder
->possible_crtcs
|= (1 << id
);
267 DBG("registered %d planes, %d crtcs, %d encoders and %d connectors\n",
268 priv
->num_planes
, priv
->num_crtcs
, priv
->num_encoders
,
269 priv
->num_connectors
);
271 dev
->mode_config
.min_width
= 32;
272 dev
->mode_config
.min_height
= 32;
274 /* note: eventually will need some cpu_is_omapXYZ() type stuff here
275 * to fill in these limits properly on different OMAP generations..
277 dev
->mode_config
.max_width
= 2048;
278 dev
->mode_config
.max_height
= 2048;
280 dev
->mode_config
.funcs
= &omap_mode_config_funcs
;
285 static void omap_modeset_free(struct drm_device
*dev
)
287 drm_mode_config_cleanup(dev
);
295 static int ioctl_get_param(struct drm_device
*dev
, void *data
,
296 struct drm_file
*file_priv
)
298 struct omap_drm_private
*priv
= dev
->dev_private
;
299 struct drm_omap_param
*args
= data
;
301 DBG("%p: param=%llu", dev
, args
->param
);
303 switch (args
->param
) {
304 case OMAP_PARAM_CHIPSET_ID
:
305 args
->value
= priv
->omaprev
;
308 DBG("unknown parameter %lld", args
->param
);
315 static int ioctl_set_param(struct drm_device
*dev
, void *data
,
316 struct drm_file
*file_priv
)
318 struct drm_omap_param
*args
= data
;
320 switch (args
->param
) {
322 DBG("unknown parameter %lld", args
->param
);
329 static int ioctl_gem_new(struct drm_device
*dev
, void *data
,
330 struct drm_file
*file_priv
)
332 struct drm_omap_gem_new
*args
= data
;
333 VERB("%p:%p: size=0x%08x, flags=%08x", dev
, file_priv
,
334 args
->size
.bytes
, args
->flags
);
335 return omap_gem_new_handle(dev
, file_priv
, args
->size
,
336 args
->flags
, &args
->handle
);
339 static int ioctl_gem_cpu_prep(struct drm_device
*dev
, void *data
,
340 struct drm_file
*file_priv
)
342 struct drm_omap_gem_cpu_prep
*args
= data
;
343 struct drm_gem_object
*obj
;
346 VERB("%p:%p: handle=%d, op=%x", dev
, file_priv
, args
->handle
, args
->op
);
348 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
352 ret
= omap_gem_op_sync(obj
, args
->op
);
355 ret
= omap_gem_op_start(obj
, args
->op
);
357 drm_gem_object_unreference_unlocked(obj
);
362 static int ioctl_gem_cpu_fini(struct drm_device
*dev
, void *data
,
363 struct drm_file
*file_priv
)
365 struct drm_omap_gem_cpu_fini
*args
= data
;
366 struct drm_gem_object
*obj
;
369 VERB("%p:%p: handle=%d", dev
, file_priv
, args
->handle
);
371 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
375 /* XXX flushy, flushy */
379 ret
= omap_gem_op_finish(obj
, args
->op
);
381 drm_gem_object_unreference_unlocked(obj
);
386 static int ioctl_gem_info(struct drm_device
*dev
, void *data
,
387 struct drm_file
*file_priv
)
389 struct drm_omap_gem_info
*args
= data
;
390 struct drm_gem_object
*obj
;
393 VERB("%p:%p: handle=%d", dev
, file_priv
, args
->handle
);
395 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
399 args
->size
= omap_gem_mmap_size(obj
);
400 args
->offset
= omap_gem_mmap_offset(obj
);
402 drm_gem_object_unreference_unlocked(obj
);
407 static struct drm_ioctl_desc ioctls
[DRM_COMMAND_END
- DRM_COMMAND_BASE
] = {
408 DRM_IOCTL_DEF_DRV(OMAP_GET_PARAM
, ioctl_get_param
, DRM_UNLOCKED
|DRM_AUTH
),
409 DRM_IOCTL_DEF_DRV(OMAP_SET_PARAM
, ioctl_set_param
, DRM_UNLOCKED
|DRM_AUTH
|DRM_MASTER
|DRM_ROOT_ONLY
),
410 DRM_IOCTL_DEF_DRV(OMAP_GEM_NEW
, ioctl_gem_new
, DRM_UNLOCKED
|DRM_AUTH
),
411 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_PREP
, ioctl_gem_cpu_prep
, DRM_UNLOCKED
|DRM_AUTH
),
412 DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_FINI
, ioctl_gem_cpu_fini
, DRM_UNLOCKED
|DRM_AUTH
),
413 DRM_IOCTL_DEF_DRV(OMAP_GEM_INFO
, ioctl_gem_info
, DRM_UNLOCKED
|DRM_AUTH
),
421 * load - setup chip and create an initial config
423 * @flags: startup flags
425 * The driver load routine has to do several things:
426 * - initialize the memory manager
427 * - allocate initial config memory
428 * - setup the DRM framebuffer with the allocated memory
430 static int dev_load(struct drm_device
*dev
, unsigned long flags
)
432 struct omap_drm_platform_data
*pdata
= dev
->dev
->platform_data
;
433 struct omap_drm_private
*priv
;
436 DBG("load: dev=%p", dev
);
438 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
442 priv
->omaprev
= pdata
->omaprev
;
444 dev
->dev_private
= priv
;
446 priv
->wq
= alloc_ordered_workqueue("omapdrm", 0);
448 INIT_LIST_HEAD(&priv
->obj_list
);
452 ret
= omap_modeset_init(dev
);
454 dev_err(dev
->dev
, "omap_modeset_init failed: ret=%d\n", ret
);
455 dev
->dev_private
= NULL
;
460 ret
= drm_vblank_init(dev
, priv
->num_crtcs
);
462 dev_warn(dev
->dev
, "could not init vblank\n");
464 priv
->fbdev
= omap_fbdev_init(dev
);
466 dev_warn(dev
->dev
, "omap_fbdev_init failed\n");
467 /* well, limp along without an fbdev.. maybe X11 will work? */
470 /* store off drm_device for use in pm ops */
471 dev_set_drvdata(dev
->dev
, dev
);
473 drm_kms_helper_poll_init(dev
);
478 static int dev_unload(struct drm_device
*dev
)
480 struct omap_drm_private
*priv
= dev
->dev_private
;
482 DBG("unload: dev=%p", dev
);
484 drm_kms_helper_poll_fini(dev
);
485 drm_vblank_cleanup(dev
);
486 omap_drm_irq_uninstall(dev
);
488 omap_fbdev_free(dev
);
489 omap_modeset_free(dev
);
490 omap_gem_deinit(dev
);
492 flush_workqueue(priv
->wq
);
493 destroy_workqueue(priv
->wq
);
495 kfree(dev
->dev_private
);
496 dev
->dev_private
= NULL
;
498 dev_set_drvdata(dev
->dev
, NULL
);
503 static int dev_open(struct drm_device
*dev
, struct drm_file
*file
)
505 file
->driver_priv
= NULL
;
507 DBG("open: dev=%p, file=%p", dev
, file
);
512 static int dev_firstopen(struct drm_device
*dev
)
514 DBG("firstopen: dev=%p", dev
);
519 * lastclose - clean up after all DRM clients have exited
522 * Take care of cleaning up after all DRM clients have exited. In the
523 * mode setting case, we want to restore the kernel's initial mode (just
524 * in case the last client left us in a bad state).
526 static void dev_lastclose(struct drm_device
*dev
)
530 /* we don't support vga-switcheroo.. so just make sure the fbdev
533 struct omap_drm_private
*priv
= dev
->dev_private
;
536 DBG("lastclose: dev=%p", dev
);
538 if (priv
->rotation_prop
) {
539 /* need to restore default rotation state.. not sure
540 * if there is a cleaner way to restore properties to
541 * default state? Maybe a flag that properties should
542 * automatically be restored to default state on
545 for (i
= 0; i
< priv
->num_crtcs
; i
++) {
546 drm_object_property_set_value(&priv
->crtcs
[i
]->base
,
547 priv
->rotation_prop
, 0);
550 for (i
= 0; i
< priv
->num_planes
; i
++) {
551 drm_object_property_set_value(&priv
->planes
[i
]->base
,
552 priv
->rotation_prop
, 0);
556 drm_modeset_lock_all(dev
);
557 ret
= drm_fb_helper_restore_fbdev_mode(priv
->fbdev
);
558 drm_modeset_unlock_all(dev
);
560 DBG("failed to restore crtc mode");
563 static void dev_preclose(struct drm_device
*dev
, struct drm_file
*file
)
565 DBG("preclose: dev=%p", dev
);
568 static void dev_postclose(struct drm_device
*dev
, struct drm_file
*file
)
570 DBG("postclose: dev=%p, file=%p", dev
, file
);
573 static const struct vm_operations_struct omap_gem_vm_ops
= {
574 .fault
= omap_gem_fault
,
575 .open
= drm_gem_vm_open
,
576 .close
= drm_gem_vm_close
,
579 static const struct file_operations omapdriver_fops
= {
580 .owner
= THIS_MODULE
,
582 .unlocked_ioctl
= drm_ioctl
,
583 .release
= drm_release
,
584 .mmap
= omap_gem_mmap
,
586 .fasync
= drm_fasync
,
588 .llseek
= noop_llseek
,
591 static struct drm_driver omap_drm_driver
= {
593 DRIVER_HAVE_IRQ
| DRIVER_MODESET
| DRIVER_GEM
| DRIVER_PRIME
,
595 .unload
= dev_unload
,
597 .firstopen
= dev_firstopen
,
598 .lastclose
= dev_lastclose
,
599 .preclose
= dev_preclose
,
600 .postclose
= dev_postclose
,
601 .get_vblank_counter
= drm_vblank_count
,
602 .enable_vblank
= omap_irq_enable_vblank
,
603 .disable_vblank
= omap_irq_disable_vblank
,
604 .irq_preinstall
= omap_irq_preinstall
,
605 .irq_postinstall
= omap_irq_postinstall
,
606 .irq_uninstall
= omap_irq_uninstall
,
607 .irq_handler
= omap_irq_handler
,
608 #ifdef CONFIG_DEBUG_FS
609 .debugfs_init
= omap_debugfs_init
,
610 .debugfs_cleanup
= omap_debugfs_cleanup
,
612 .prime_handle_to_fd
= drm_gem_prime_handle_to_fd
,
613 .prime_fd_to_handle
= drm_gem_prime_fd_to_handle
,
614 .gem_prime_export
= omap_gem_prime_export
,
615 .gem_prime_import
= omap_gem_prime_import
,
616 .gem_init_object
= omap_gem_init_object
,
617 .gem_free_object
= omap_gem_free_object
,
618 .gem_vm_ops
= &omap_gem_vm_ops
,
619 .dumb_create
= omap_gem_dumb_create
,
620 .dumb_map_offset
= omap_gem_dumb_map_offset
,
621 .dumb_destroy
= omap_gem_dumb_destroy
,
623 .num_ioctls
= DRM_OMAP_NUM_IOCTLS
,
624 .fops
= &omapdriver_fops
,
628 .major
= DRIVER_MAJOR
,
629 .minor
= DRIVER_MINOR
,
630 .patchlevel
= DRIVER_PATCHLEVEL
,
633 static int pdev_suspend(struct platform_device
*pDevice
, pm_message_t state
)
639 static int pdev_resume(struct platform_device
*device
)
645 static void pdev_shutdown(struct platform_device
*device
)
650 static int pdev_probe(struct platform_device
*device
)
652 if (omapdss_is_initialized() == false)
653 return -EPROBE_DEFER
;
655 DBG("%s", device
->name
);
656 return drm_platform_init(&omap_drm_driver
, device
);
659 static int pdev_remove(struct platform_device
*device
)
662 drm_platform_exit(&omap_drm_driver
, device
);
664 platform_driver_unregister(&omap_dmm_driver
);
669 static const struct dev_pm_ops omapdrm_pm_ops
= {
670 .resume
= omap_gem_resume
,
674 static struct platform_driver pdev
= {
677 .owner
= THIS_MODULE
,
679 .pm
= &omapdrm_pm_ops
,
683 .remove
= pdev_remove
,
684 .suspend
= pdev_suspend
,
685 .resume
= pdev_resume
,
686 .shutdown
= pdev_shutdown
,
689 static int __init
omap_drm_init(void)
692 if (platform_driver_register(&omap_dmm_driver
)) {
693 /* we can continue on without DMM.. so not fatal */
694 dev_err(NULL
, "DMM registration failed\n");
696 return platform_driver_register(&pdev
);
699 static void __exit
omap_drm_fini(void)
702 platform_driver_unregister(&pdev
);
705 /* need late_initcall() so we load after dss_driver's are loaded */
706 late_initcall(omap_drm_init
);
707 module_exit(omap_drm_fini
);
709 MODULE_AUTHOR("Rob Clark <rob@ti.com>");
710 MODULE_DESCRIPTION("OMAP DRM Display Driver");
711 MODULE_ALIAS("platform:" DRIVER_NAME
);
712 MODULE_LICENSE("GPL v2");