2 * linux/drivers/video/omap2/dss/overlay.c
4 * Copyright (C) 2009 Nokia Corporation
5 * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
7 * Some code and ideas taken from drivers/video/omap/ driver
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License version 2 as published by
12 * the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
23 #define DSS_SUBSYS_NAME "OVERLAY"
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/err.h>
28 #include <linux/sysfs.h>
29 #include <linux/kobject.h>
30 #include <linux/platform_device.h>
31 #include <linux/delay.h>
32 #include <linux/slab.h>
34 #include <video/omapdss.h>
38 #include "dss_features.h"
40 static int num_overlays
;
41 static struct list_head overlay_list
;
43 static ssize_t
overlay_name_show(struct omap_overlay
*ovl
, char *buf
)
45 return snprintf(buf
, PAGE_SIZE
, "%s\n", ovl
->name
);
48 static ssize_t
overlay_manager_show(struct omap_overlay
*ovl
, char *buf
)
50 return snprintf(buf
, PAGE_SIZE
, "%s\n",
51 ovl
->manager
? ovl
->manager
->name
: "<none>");
54 static ssize_t
overlay_manager_store(struct omap_overlay
*ovl
, const char *buf
,
58 struct omap_overlay_manager
*mgr
= NULL
;
59 struct omap_overlay_manager
*old_mgr
;
62 if (buf
[size
-1] == '\n')
66 for (i
= 0; i
< omap_dss_get_num_overlay_managers(); ++i
) {
67 mgr
= omap_dss_get_overlay_manager(i
);
69 if (sysfs_streq(buf
, mgr
->name
))
76 if (len
> 0 && mgr
== NULL
)
80 DSSDBG("manager %s found\n", mgr
->name
);
82 if (mgr
== ovl
->manager
)
85 old_mgr
= ovl
->manager
;
87 r
= dispc_runtime_get();
91 /* detach old manager */
93 r
= ovl
->unset_manager(ovl
);
95 DSSERR("detach failed\n");
99 r
= old_mgr
->apply(old_mgr
);
105 r
= ovl
->set_manager(ovl
, mgr
);
107 DSSERR("Failed to attach overlay\n");
125 static ssize_t
overlay_input_size_show(struct omap_overlay
*ovl
, char *buf
)
127 return snprintf(buf
, PAGE_SIZE
, "%d,%d\n",
128 ovl
->info
.width
, ovl
->info
.height
);
131 static ssize_t
overlay_screen_width_show(struct omap_overlay
*ovl
, char *buf
)
133 return snprintf(buf
, PAGE_SIZE
, "%d\n", ovl
->info
.screen_width
);
136 static ssize_t
overlay_position_show(struct omap_overlay
*ovl
, char *buf
)
138 return snprintf(buf
, PAGE_SIZE
, "%d,%d\n",
139 ovl
->info
.pos_x
, ovl
->info
.pos_y
);
142 static ssize_t
overlay_position_store(struct omap_overlay
*ovl
,
143 const char *buf
, size_t size
)
147 struct omap_overlay_info info
;
149 ovl
->get_overlay_info(ovl
, &info
);
151 info
.pos_x
= simple_strtoul(buf
, &last
, 10);
153 if (last
- buf
>= size
)
156 info
.pos_y
= simple_strtoul(last
, &last
, 10);
158 r
= ovl
->set_overlay_info(ovl
, &info
);
163 r
= ovl
->manager
->apply(ovl
->manager
);
171 static ssize_t
overlay_output_size_show(struct omap_overlay
*ovl
, char *buf
)
173 return snprintf(buf
, PAGE_SIZE
, "%d,%d\n",
174 ovl
->info
.out_width
, ovl
->info
.out_height
);
177 static ssize_t
overlay_output_size_store(struct omap_overlay
*ovl
,
178 const char *buf
, size_t size
)
182 struct omap_overlay_info info
;
184 ovl
->get_overlay_info(ovl
, &info
);
186 info
.out_width
= simple_strtoul(buf
, &last
, 10);
188 if (last
- buf
>= size
)
191 info
.out_height
= simple_strtoul(last
, &last
, 10);
193 r
= ovl
->set_overlay_info(ovl
, &info
);
198 r
= ovl
->manager
->apply(ovl
->manager
);
206 static ssize_t
overlay_enabled_show(struct omap_overlay
*ovl
, char *buf
)
208 return snprintf(buf
, PAGE_SIZE
, "%d\n", ovl
->info
.enabled
);
211 static ssize_t
overlay_enabled_store(struct omap_overlay
*ovl
, const char *buf
,
215 struct omap_overlay_info info
;
217 ovl
->get_overlay_info(ovl
, &info
);
219 r
= kstrtoint(buf
, 0, &enable
);
223 info
.enabled
= !!enable
;
225 r
= ovl
->set_overlay_info(ovl
, &info
);
230 r
= ovl
->manager
->apply(ovl
->manager
);
238 static ssize_t
overlay_global_alpha_show(struct omap_overlay
*ovl
, char *buf
)
240 return snprintf(buf
, PAGE_SIZE
, "%d\n",
241 ovl
->info
.global_alpha
);
244 static ssize_t
overlay_global_alpha_store(struct omap_overlay
*ovl
,
245 const char *buf
, size_t size
)
249 struct omap_overlay_info info
;
251 if (!dss_has_feature(FEAT_GLOBAL_ALPHA
))
254 r
= kstrtou8(buf
, 0, &alpha
);
258 ovl
->get_overlay_info(ovl
, &info
);
260 /* Video1 plane does not support global alpha
261 * to always make it 255 completely opaque
263 if (!dss_has_feature(FEAT_GLOBAL_ALPHA_VID1
) &&
264 ovl
->id
== OMAP_DSS_VIDEO1
)
265 info
.global_alpha
= 255;
267 info
.global_alpha
= alpha
;
269 r
= ovl
->set_overlay_info(ovl
, &info
);
274 r
= ovl
->manager
->apply(ovl
->manager
);
282 static ssize_t
overlay_pre_mult_alpha_show(struct omap_overlay
*ovl
,
285 return snprintf(buf
, PAGE_SIZE
, "%d\n",
286 ovl
->info
.pre_mult_alpha
);
289 static ssize_t
overlay_pre_mult_alpha_store(struct omap_overlay
*ovl
,
290 const char *buf
, size_t size
)
294 struct omap_overlay_info info
;
296 r
= kstrtou8(buf
, 0, &alpha
);
300 ovl
->get_overlay_info(ovl
, &info
);
302 /* only GFX and Video2 plane support pre alpha multiplied
303 * set zero for Video1 plane
305 if (!dss_has_feature(FEAT_GLOBAL_ALPHA_VID1
) &&
306 ovl
->id
== OMAP_DSS_VIDEO1
)
307 info
.pre_mult_alpha
= 0;
309 info
.pre_mult_alpha
= alpha
;
311 r
= ovl
->set_overlay_info(ovl
, &info
);
316 r
= ovl
->manager
->apply(ovl
->manager
);
324 struct overlay_attribute
{
325 struct attribute attr
;
326 ssize_t (*show
)(struct omap_overlay
*, char *);
327 ssize_t (*store
)(struct omap_overlay
*, const char *, size_t);
330 #define OVERLAY_ATTR(_name, _mode, _show, _store) \
331 struct overlay_attribute overlay_attr_##_name = \
332 __ATTR(_name, _mode, _show, _store)
334 static OVERLAY_ATTR(name
, S_IRUGO
, overlay_name_show
, NULL
);
335 static OVERLAY_ATTR(manager
, S_IRUGO
|S_IWUSR
,
336 overlay_manager_show
, overlay_manager_store
);
337 static OVERLAY_ATTR(input_size
, S_IRUGO
, overlay_input_size_show
, NULL
);
338 static OVERLAY_ATTR(screen_width
, S_IRUGO
, overlay_screen_width_show
, NULL
);
339 static OVERLAY_ATTR(position
, S_IRUGO
|S_IWUSR
,
340 overlay_position_show
, overlay_position_store
);
341 static OVERLAY_ATTR(output_size
, S_IRUGO
|S_IWUSR
,
342 overlay_output_size_show
, overlay_output_size_store
);
343 static OVERLAY_ATTR(enabled
, S_IRUGO
|S_IWUSR
,
344 overlay_enabled_show
, overlay_enabled_store
);
345 static OVERLAY_ATTR(global_alpha
, S_IRUGO
|S_IWUSR
,
346 overlay_global_alpha_show
, overlay_global_alpha_store
);
347 static OVERLAY_ATTR(pre_mult_alpha
, S_IRUGO
|S_IWUSR
,
348 overlay_pre_mult_alpha_show
,
349 overlay_pre_mult_alpha_store
);
351 static struct attribute
*overlay_sysfs_attrs
[] = {
352 &overlay_attr_name
.attr
,
353 &overlay_attr_manager
.attr
,
354 &overlay_attr_input_size
.attr
,
355 &overlay_attr_screen_width
.attr
,
356 &overlay_attr_position
.attr
,
357 &overlay_attr_output_size
.attr
,
358 &overlay_attr_enabled
.attr
,
359 &overlay_attr_global_alpha
.attr
,
360 &overlay_attr_pre_mult_alpha
.attr
,
364 static ssize_t
overlay_attr_show(struct kobject
*kobj
, struct attribute
*attr
,
367 struct omap_overlay
*overlay
;
368 struct overlay_attribute
*overlay_attr
;
370 overlay
= container_of(kobj
, struct omap_overlay
, kobj
);
371 overlay_attr
= container_of(attr
, struct overlay_attribute
, attr
);
373 if (!overlay_attr
->show
)
376 return overlay_attr
->show(overlay
, buf
);
379 static ssize_t
overlay_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
380 const char *buf
, size_t size
)
382 struct omap_overlay
*overlay
;
383 struct overlay_attribute
*overlay_attr
;
385 overlay
= container_of(kobj
, struct omap_overlay
, kobj
);
386 overlay_attr
= container_of(attr
, struct overlay_attribute
, attr
);
388 if (!overlay_attr
->store
)
391 return overlay_attr
->store(overlay
, buf
, size
);
394 static const struct sysfs_ops overlay_sysfs_ops
= {
395 .show
= overlay_attr_show
,
396 .store
= overlay_attr_store
,
399 static struct kobj_type overlay_ktype
= {
400 .sysfs_ops
= &overlay_sysfs_ops
,
401 .default_attrs
= overlay_sysfs_attrs
,
404 /* Check if overlay parameters are compatible with display */
405 int dss_check_overlay(struct omap_overlay
*ovl
, struct omap_dss_device
*dssdev
)
407 struct omap_overlay_info
*info
;
414 if (!ovl
->info
.enabled
)
419 if (info
->paddr
== 0) {
420 DSSDBG("check_overlay failed: paddr 0\n");
424 dssdev
->driver
->get_resolution(dssdev
, &dw
, &dh
);
426 DSSDBG("check_overlay %d: (%d,%d %dx%d -> %dx%d) disp (%dx%d)\n",
428 info
->pos_x
, info
->pos_y
,
429 info
->width
, info
->height
,
430 info
->out_width
, info
->out_height
,
433 if ((ovl
->caps
& OMAP_DSS_OVL_CAP_SCALE
) == 0) {
437 if (info
->out_width
== 0)
440 outw
= info
->out_width
;
442 if (info
->out_height
== 0)
445 outh
= info
->out_height
;
448 if (dw
< info
->pos_x
+ outw
) {
449 DSSDBG("check_overlay failed 1: %d < %d + %d\n",
450 dw
, info
->pos_x
, outw
);
454 if (dh
< info
->pos_y
+ outh
) {
455 DSSDBG("check_overlay failed 2: %d < %d + %d\n",
456 dh
, info
->pos_y
, outh
);
460 if ((ovl
->supported_modes
& info
->color_mode
) == 0) {
461 DSSERR("overlay doesn't support mode %d\n", info
->color_mode
);
468 static int dss_ovl_set_overlay_info(struct omap_overlay
*ovl
,
469 struct omap_overlay_info
*info
)
472 struct omap_overlay_info old_info
;
474 old_info
= ovl
->info
;
478 r
= dss_check_overlay(ovl
, ovl
->manager
->device
);
480 ovl
->info
= old_info
;
485 ovl
->info_dirty
= true;
490 static void dss_ovl_get_overlay_info(struct omap_overlay
*ovl
,
491 struct omap_overlay_info
*info
)
496 static int dss_ovl_wait_for_go(struct omap_overlay
*ovl
)
498 return dss_mgr_wait_for_go_ovl(ovl
);
501 static int omap_dss_set_manager(struct omap_overlay
*ovl
,
502 struct omap_overlay_manager
*mgr
)
508 DSSERR("overlay '%s' already has a manager '%s'\n",
509 ovl
->name
, ovl
->manager
->name
);
513 if (ovl
->info
.enabled
) {
514 DSSERR("overlay has to be disabled to change the manager\n");
520 /* XXX: When there is an overlay on a DSI manual update display, and
521 * the overlay is first disabled, then moved to tv, and enabled, we
522 * seem to get SYNC_LOST_DIGIT error.
524 * Waiting doesn't seem to help, but updating the manual update display
525 * after disabling the overlay seems to fix this. This hints that the
526 * overlay is perhaps somehow tied to the LCD output until the output
529 * Userspace workaround for this is to update the LCD after disabling
530 * the overlay, but before moving the overlay to TV.
532 dispc_set_channel_out(ovl
->id
, mgr
->id
);
537 static int omap_dss_unset_manager(struct omap_overlay
*ovl
)
542 DSSERR("failed to detach overlay: manager not set\n");
546 if (ovl
->info
.enabled
) {
547 DSSERR("overlay has to be disabled to unset the manager\n");
551 r
= ovl
->wait_for_go(ovl
);
560 int omap_dss_get_num_overlays(void)
564 EXPORT_SYMBOL(omap_dss_get_num_overlays
);
566 struct omap_overlay
*omap_dss_get_overlay(int num
)
569 struct omap_overlay
*ovl
;
571 list_for_each_entry(ovl
, &overlay_list
, list
) {
578 EXPORT_SYMBOL(omap_dss_get_overlay
);
580 static void omap_dss_add_overlay(struct omap_overlay
*overlay
)
583 list_add_tail(&overlay
->list
, &overlay_list
);
586 static struct omap_overlay
*dispc_overlays
[MAX_DSS_OVERLAYS
];
588 void dss_overlay_setup_dispc_manager(struct omap_overlay_manager
*mgr
)
590 mgr
->num_overlays
= dss_feat_get_num_ovls();
591 mgr
->overlays
= dispc_overlays
;
595 static struct omap_overlay
*l4_overlays
[1];
596 void dss_overlay_setup_l4_manager(struct omap_overlay_manager
*mgr
)
598 mgr
->num_overlays
= 1;
599 mgr
->overlays
= l4_overlays
;
603 void dss_init_overlays(struct platform_device
*pdev
)
607 INIT_LIST_HEAD(&overlay_list
);
611 for (i
= 0; i
< dss_feat_get_num_ovls(); ++i
) {
612 struct omap_overlay
*ovl
;
613 ovl
= kzalloc(sizeof(*ovl
), GFP_KERNEL
);
620 ovl
->id
= OMAP_DSS_GFX
;
621 ovl
->caps
= OMAP_DSS_OVL_CAP_DISPC
;
622 ovl
->info
.global_alpha
= 255;
626 ovl
->id
= OMAP_DSS_VIDEO1
;
627 ovl
->caps
= OMAP_DSS_OVL_CAP_SCALE
|
628 OMAP_DSS_OVL_CAP_DISPC
;
629 ovl
->info
.global_alpha
= 255;
633 ovl
->id
= OMAP_DSS_VIDEO2
;
634 ovl
->caps
= OMAP_DSS_OVL_CAP_SCALE
|
635 OMAP_DSS_OVL_CAP_DISPC
;
636 ovl
->info
.global_alpha
= 255;
640 ovl
->set_manager
= &omap_dss_set_manager
;
641 ovl
->unset_manager
= &omap_dss_unset_manager
;
642 ovl
->set_overlay_info
= &dss_ovl_set_overlay_info
;
643 ovl
->get_overlay_info
= &dss_ovl_get_overlay_info
;
644 ovl
->wait_for_go
= &dss_ovl_wait_for_go
;
646 ovl
->supported_modes
=
647 dss_feat_get_supported_color_modes(ovl
->id
);
649 omap_dss_add_overlay(ovl
);
651 r
= kobject_init_and_add(&ovl
->kobj
, &overlay_ktype
,
652 &pdev
->dev
.kobj
, "overlay%d", i
);
655 DSSERR("failed to create sysfs file\n");
659 dispc_overlays
[i
] = ovl
;
664 struct omap_overlay
*ovl
;
665 ovl
= kzalloc(sizeof(*ovl
), GFP_KERNEL
);
670 ovl
->supported_modes
= OMAP_DSS_COLOR_RGB24U
;
672 ovl
->set_manager
= &omap_dss_set_manager
;
673 ovl
->unset_manager
= &omap_dss_unset_manager
;
674 ovl
->set_overlay_info
= &dss_ovl_set_overlay_info
;
675 ovl
->get_overlay_info
= &dss_ovl_get_overlay_info
;
677 omap_dss_add_overlay(ovl
);
679 r
= kobject_init_and_add(&ovl
->kobj
, &overlay_ktype
,
680 &pdev
->dev
.kobj
, "overlayl4");
683 DSSERR("failed to create sysfs file\n");
685 l4_overlays
[0] = ovl
;
690 /* connect overlays to the new device, if not already connected. if force
691 * selected, connect always. */
692 void dss_recheck_connections(struct omap_dss_device
*dssdev
, bool force
)
695 struct omap_overlay_manager
*lcd_mgr
;
696 struct omap_overlay_manager
*tv_mgr
;
697 struct omap_overlay_manager
*lcd2_mgr
= NULL
;
698 struct omap_overlay_manager
*mgr
= NULL
;
700 lcd_mgr
= omap_dss_get_overlay_manager(OMAP_DSS_OVL_MGR_LCD
);
701 tv_mgr
= omap_dss_get_overlay_manager(OMAP_DSS_OVL_MGR_TV
);
702 if (dss_has_feature(FEAT_MGR_LCD2
))
703 lcd2_mgr
= omap_dss_get_overlay_manager(OMAP_DSS_OVL_MGR_LCD2
);
705 if (dssdev
->channel
== OMAP_DSS_CHANNEL_LCD2
) {
706 if (!lcd2_mgr
->device
|| force
) {
707 if (lcd2_mgr
->device
)
708 lcd2_mgr
->unset_device(lcd2_mgr
);
709 lcd2_mgr
->set_device(lcd2_mgr
, dssdev
);
712 } else if (dssdev
->type
!= OMAP_DISPLAY_TYPE_VENC
713 && dssdev
->type
!= OMAP_DISPLAY_TYPE_HDMI
) {
714 if (!lcd_mgr
->device
|| force
) {
716 lcd_mgr
->unset_device(lcd_mgr
);
717 lcd_mgr
->set_device(lcd_mgr
, dssdev
);
722 if (dssdev
->type
== OMAP_DISPLAY_TYPE_VENC
723 || dssdev
->type
== OMAP_DISPLAY_TYPE_HDMI
) {
724 if (!tv_mgr
->device
|| force
) {
726 tv_mgr
->unset_device(tv_mgr
);
727 tv_mgr
->set_device(tv_mgr
, dssdev
);
735 for (i
= 0; i
< dss_feat_get_num_ovls(); i
++) {
736 struct omap_overlay
*ovl
;
737 ovl
= omap_dss_get_overlay(i
);
738 if (!ovl
->manager
|| force
) {
740 omap_dss_unset_manager(ovl
);
741 omap_dss_set_manager(ovl
, mgr
);
749 void dss_uninit_overlays(struct platform_device
*pdev
)
751 struct omap_overlay
*ovl
;
753 while (!list_empty(&overlay_list
)) {
754 ovl
= list_first_entry(&overlay_list
,
755 struct omap_overlay
, list
);
756 list_del(&ovl
->list
);
757 kobject_del(&ovl
->kobj
);
758 kobject_put(&ovl
->kobj
);