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 omap_overlay
*overlays
;
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 struct omap_overlay_info info
;
129 ovl
->get_overlay_info(ovl
, &info
);
131 return snprintf(buf
, PAGE_SIZE
, "%d,%d\n",
132 info
.width
, info
.height
);
135 static ssize_t
overlay_screen_width_show(struct omap_overlay
*ovl
, char *buf
)
137 struct omap_overlay_info info
;
139 ovl
->get_overlay_info(ovl
, &info
);
141 return snprintf(buf
, PAGE_SIZE
, "%d\n", info
.screen_width
);
144 static ssize_t
overlay_position_show(struct omap_overlay
*ovl
, char *buf
)
146 struct omap_overlay_info info
;
148 ovl
->get_overlay_info(ovl
, &info
);
150 return snprintf(buf
, PAGE_SIZE
, "%d,%d\n",
151 info
.pos_x
, info
.pos_y
);
154 static ssize_t
overlay_position_store(struct omap_overlay
*ovl
,
155 const char *buf
, size_t size
)
159 struct omap_overlay_info info
;
161 ovl
->get_overlay_info(ovl
, &info
);
163 info
.pos_x
= simple_strtoul(buf
, &last
, 10);
165 if (last
- buf
>= size
)
168 info
.pos_y
= simple_strtoul(last
, &last
, 10);
170 r
= ovl
->set_overlay_info(ovl
, &info
);
175 r
= ovl
->manager
->apply(ovl
->manager
);
183 static ssize_t
overlay_output_size_show(struct omap_overlay
*ovl
, char *buf
)
185 struct omap_overlay_info info
;
187 ovl
->get_overlay_info(ovl
, &info
);
189 return snprintf(buf
, PAGE_SIZE
, "%d,%d\n",
190 info
.out_width
, info
.out_height
);
193 static ssize_t
overlay_output_size_store(struct omap_overlay
*ovl
,
194 const char *buf
, size_t size
)
198 struct omap_overlay_info info
;
200 ovl
->get_overlay_info(ovl
, &info
);
202 info
.out_width
= simple_strtoul(buf
, &last
, 10);
204 if (last
- buf
>= size
)
207 info
.out_height
= simple_strtoul(last
, &last
, 10);
209 r
= ovl
->set_overlay_info(ovl
, &info
);
214 r
= ovl
->manager
->apply(ovl
->manager
);
222 static ssize_t
overlay_enabled_show(struct omap_overlay
*ovl
, char *buf
)
224 return snprintf(buf
, PAGE_SIZE
, "%d\n", ovl
->is_enabled(ovl
));
227 static ssize_t
overlay_enabled_store(struct omap_overlay
*ovl
, const char *buf
,
233 r
= strtobool(buf
, &enable
);
238 r
= ovl
->enable(ovl
);
240 r
= ovl
->disable(ovl
);
248 static ssize_t
overlay_global_alpha_show(struct omap_overlay
*ovl
, char *buf
)
250 struct omap_overlay_info info
;
252 ovl
->get_overlay_info(ovl
, &info
);
254 return snprintf(buf
, PAGE_SIZE
, "%d\n",
258 static ssize_t
overlay_global_alpha_store(struct omap_overlay
*ovl
,
259 const char *buf
, size_t size
)
263 struct omap_overlay_info info
;
265 if ((ovl
->caps
& OMAP_DSS_OVL_CAP_GLOBAL_ALPHA
) == 0)
268 r
= kstrtou8(buf
, 0, &alpha
);
272 ovl
->get_overlay_info(ovl
, &info
);
274 info
.global_alpha
= alpha
;
276 r
= ovl
->set_overlay_info(ovl
, &info
);
281 r
= ovl
->manager
->apply(ovl
->manager
);
289 static ssize_t
overlay_pre_mult_alpha_show(struct omap_overlay
*ovl
,
292 struct omap_overlay_info info
;
294 ovl
->get_overlay_info(ovl
, &info
);
296 return snprintf(buf
, PAGE_SIZE
, "%d\n",
297 info
.pre_mult_alpha
);
300 static ssize_t
overlay_pre_mult_alpha_store(struct omap_overlay
*ovl
,
301 const char *buf
, size_t size
)
305 struct omap_overlay_info info
;
307 if ((ovl
->caps
& OMAP_DSS_OVL_CAP_PRE_MULT_ALPHA
) == 0)
310 r
= kstrtou8(buf
, 0, &alpha
);
314 ovl
->get_overlay_info(ovl
, &info
);
316 info
.pre_mult_alpha
= alpha
;
318 r
= ovl
->set_overlay_info(ovl
, &info
);
323 r
= ovl
->manager
->apply(ovl
->manager
);
331 static ssize_t
overlay_zorder_show(struct omap_overlay
*ovl
, char *buf
)
333 struct omap_overlay_info info
;
335 ovl
->get_overlay_info(ovl
, &info
);
337 return snprintf(buf
, PAGE_SIZE
, "%d\n", info
.zorder
);
340 static ssize_t
overlay_zorder_store(struct omap_overlay
*ovl
,
341 const char *buf
, size_t size
)
345 struct omap_overlay_info info
;
347 if ((ovl
->caps
& OMAP_DSS_OVL_CAP_ZORDER
) == 0)
350 r
= kstrtou8(buf
, 0, &zorder
);
354 ovl
->get_overlay_info(ovl
, &info
);
356 info
.zorder
= zorder
;
358 r
= ovl
->set_overlay_info(ovl
, &info
);
363 r
= ovl
->manager
->apply(ovl
->manager
);
371 struct overlay_attribute
{
372 struct attribute attr
;
373 ssize_t (*show
)(struct omap_overlay
*, char *);
374 ssize_t (*store
)(struct omap_overlay
*, const char *, size_t);
377 #define OVERLAY_ATTR(_name, _mode, _show, _store) \
378 struct overlay_attribute overlay_attr_##_name = \
379 __ATTR(_name, _mode, _show, _store)
381 static OVERLAY_ATTR(name
, S_IRUGO
, overlay_name_show
, NULL
);
382 static OVERLAY_ATTR(manager
, S_IRUGO
|S_IWUSR
,
383 overlay_manager_show
, overlay_manager_store
);
384 static OVERLAY_ATTR(input_size
, S_IRUGO
, overlay_input_size_show
, NULL
);
385 static OVERLAY_ATTR(screen_width
, S_IRUGO
, overlay_screen_width_show
, NULL
);
386 static OVERLAY_ATTR(position
, S_IRUGO
|S_IWUSR
,
387 overlay_position_show
, overlay_position_store
);
388 static OVERLAY_ATTR(output_size
, S_IRUGO
|S_IWUSR
,
389 overlay_output_size_show
, overlay_output_size_store
);
390 static OVERLAY_ATTR(enabled
, S_IRUGO
|S_IWUSR
,
391 overlay_enabled_show
, overlay_enabled_store
);
392 static OVERLAY_ATTR(global_alpha
, S_IRUGO
|S_IWUSR
,
393 overlay_global_alpha_show
, overlay_global_alpha_store
);
394 static OVERLAY_ATTR(pre_mult_alpha
, S_IRUGO
|S_IWUSR
,
395 overlay_pre_mult_alpha_show
,
396 overlay_pre_mult_alpha_store
);
397 static OVERLAY_ATTR(zorder
, S_IRUGO
|S_IWUSR
,
398 overlay_zorder_show
, overlay_zorder_store
);
400 static struct attribute
*overlay_sysfs_attrs
[] = {
401 &overlay_attr_name
.attr
,
402 &overlay_attr_manager
.attr
,
403 &overlay_attr_input_size
.attr
,
404 &overlay_attr_screen_width
.attr
,
405 &overlay_attr_position
.attr
,
406 &overlay_attr_output_size
.attr
,
407 &overlay_attr_enabled
.attr
,
408 &overlay_attr_global_alpha
.attr
,
409 &overlay_attr_pre_mult_alpha
.attr
,
410 &overlay_attr_zorder
.attr
,
414 static ssize_t
overlay_attr_show(struct kobject
*kobj
, struct attribute
*attr
,
417 struct omap_overlay
*overlay
;
418 struct overlay_attribute
*overlay_attr
;
420 overlay
= container_of(kobj
, struct omap_overlay
, kobj
);
421 overlay_attr
= container_of(attr
, struct overlay_attribute
, attr
);
423 if (!overlay_attr
->show
)
426 return overlay_attr
->show(overlay
, buf
);
429 static ssize_t
overlay_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
430 const char *buf
, size_t size
)
432 struct omap_overlay
*overlay
;
433 struct overlay_attribute
*overlay_attr
;
435 overlay
= container_of(kobj
, struct omap_overlay
, kobj
);
436 overlay_attr
= container_of(attr
, struct overlay_attribute
, attr
);
438 if (!overlay_attr
->store
)
441 return overlay_attr
->store(overlay
, buf
, size
);
444 static const struct sysfs_ops overlay_sysfs_ops
= {
445 .show
= overlay_attr_show
,
446 .store
= overlay_attr_store
,
449 static struct kobj_type overlay_ktype
= {
450 .sysfs_ops
= &overlay_sysfs_ops
,
451 .default_attrs
= overlay_sysfs_attrs
,
454 int omap_dss_get_num_overlays(void)
458 EXPORT_SYMBOL(omap_dss_get_num_overlays
);
460 struct omap_overlay
*omap_dss_get_overlay(int num
)
462 if (num
>= num_overlays
)
465 return &overlays
[num
];
467 EXPORT_SYMBOL(omap_dss_get_overlay
);
469 void dss_init_overlays(struct platform_device
*pdev
)
473 num_overlays
= dss_feat_get_num_ovls();
475 overlays
= kzalloc(sizeof(struct omap_overlay
) * num_overlays
,
478 BUG_ON(overlays
== NULL
);
480 for (i
= 0; i
< num_overlays
; ++i
) {
481 struct omap_overlay
*ovl
= &overlays
[i
];
486 ovl
->id
= OMAP_DSS_GFX
;
490 ovl
->id
= OMAP_DSS_VIDEO1
;
494 ovl
->id
= OMAP_DSS_VIDEO2
;
498 ovl
->id
= OMAP_DSS_VIDEO3
;
502 ovl
->is_enabled
= &dss_ovl_is_enabled
;
503 ovl
->enable
= &dss_ovl_enable
;
504 ovl
->disable
= &dss_ovl_disable
;
505 ovl
->set_manager
= &dss_ovl_set_manager
;
506 ovl
->unset_manager
= &dss_ovl_unset_manager
;
507 ovl
->set_overlay_info
= &dss_ovl_set_info
;
508 ovl
->get_overlay_info
= &dss_ovl_get_info
;
509 ovl
->wait_for_go
= &dss_mgr_wait_for_go_ovl
;
511 ovl
->caps
= dss_feat_get_overlay_caps(ovl
->id
);
512 ovl
->supported_modes
=
513 dss_feat_get_supported_color_modes(ovl
->id
);
515 r
= kobject_init_and_add(&ovl
->kobj
, &overlay_ktype
,
516 &pdev
->dev
.kobj
, "overlay%d", i
);
519 DSSERR("failed to create sysfs file\n");
523 /* connect overlays to the new device, if not already connected. if force
524 * selected, connect always. */
525 void dss_recheck_connections(struct omap_dss_device
*dssdev
, bool force
)
528 struct omap_overlay_manager
*lcd_mgr
;
529 struct omap_overlay_manager
*tv_mgr
;
530 struct omap_overlay_manager
*lcd2_mgr
= NULL
;
531 struct omap_overlay_manager
*mgr
= NULL
;
533 lcd_mgr
= omap_dss_get_overlay_manager(OMAP_DSS_OVL_MGR_LCD
);
534 tv_mgr
= omap_dss_get_overlay_manager(OMAP_DSS_OVL_MGR_TV
);
535 if (dss_has_feature(FEAT_MGR_LCD2
))
536 lcd2_mgr
= omap_dss_get_overlay_manager(OMAP_DSS_OVL_MGR_LCD2
);
538 if (dssdev
->channel
== OMAP_DSS_CHANNEL_LCD2
) {
539 if (!lcd2_mgr
->device
|| force
) {
540 if (lcd2_mgr
->device
)
541 lcd2_mgr
->unset_device(lcd2_mgr
);
542 lcd2_mgr
->set_device(lcd2_mgr
, dssdev
);
545 } else if (dssdev
->type
!= OMAP_DISPLAY_TYPE_VENC
546 && dssdev
->type
!= OMAP_DISPLAY_TYPE_HDMI
) {
547 if (!lcd_mgr
->device
|| force
) {
549 lcd_mgr
->unset_device(lcd_mgr
);
550 lcd_mgr
->set_device(lcd_mgr
, dssdev
);
555 if (dssdev
->type
== OMAP_DISPLAY_TYPE_VENC
556 || dssdev
->type
== OMAP_DISPLAY_TYPE_HDMI
) {
557 if (!tv_mgr
->device
|| force
) {
559 tv_mgr
->unset_device(tv_mgr
);
560 tv_mgr
->set_device(tv_mgr
, dssdev
);
568 for (i
= 0; i
< dss_feat_get_num_ovls(); i
++) {
569 struct omap_overlay
*ovl
;
570 ovl
= omap_dss_get_overlay(i
);
571 if (!ovl
->manager
|| force
) {
573 ovl
->unset_manager(ovl
);
574 ovl
->set_manager(ovl
, mgr
);
582 void dss_uninit_overlays(struct platform_device
*pdev
)
586 for (i
= 0; i
< num_overlays
; ++i
) {
587 struct omap_overlay
*ovl
= &overlays
[i
];
589 kobject_del(&ovl
->kobj
);
590 kobject_put(&ovl
->kobj
);
598 int dss_ovl_simple_check(struct omap_overlay
*ovl
,
599 const struct omap_overlay_info
*info
)
601 if (info
->paddr
== 0) {
602 DSSERR("check_overlay: paddr cannot be 0\n");
606 if ((ovl
->caps
& OMAP_DSS_OVL_CAP_SCALE
) == 0) {
607 if (info
->out_width
!= 0 && info
->width
!= info
->out_width
) {
608 DSSERR("check_overlay: overlay %d doesn't support "
609 "scaling\n", ovl
->id
);
613 if (info
->out_height
!= 0 && info
->height
!= info
->out_height
) {
614 DSSERR("check_overlay: overlay %d doesn't support "
615 "scaling\n", ovl
->id
);
620 if ((ovl
->supported_modes
& info
->color_mode
) == 0) {
621 DSSERR("check_overlay: overlay %d doesn't support mode %d\n",
622 ovl
->id
, info
->color_mode
);
626 if (info
->zorder
>= omap_dss_get_num_overlays()) {
627 DSSERR("check_overlay: zorder %d too high\n", info
->zorder
);
634 int dss_ovl_check(struct omap_overlay
*ovl
,
635 struct omap_overlay_info
*info
, struct omap_dss_device
*dssdev
)
643 dssdev
->driver
->get_resolution(dssdev
, &dw
, &dh
);
645 if ((ovl
->caps
& OMAP_DSS_OVL_CAP_SCALE
) == 0) {
649 if (info
->out_width
== 0)
652 outw
= info
->out_width
;
654 if (info
->out_height
== 0)
657 outh
= info
->out_height
;
660 if (dw
< info
->pos_x
+ outw
) {
661 DSSERR("overlay %d horizontally not inside the display area "
663 ovl
->id
, info
->pos_x
, outw
, dw
);
667 if (dh
< info
->pos_y
+ outh
) {
668 DSSERR("overlay %d vertically not inside the display area "
670 ovl
->id
, info
->pos_y
, outh
, dh
);