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,
216 struct omap_overlay_info info;
218 ovl->get_overlay_info(ovl, &info);
220 r = strtobool(buf, &enable);
224 info.enabled = enable;
226 r = ovl->set_overlay_info(ovl, &info);
231 r = ovl->manager->apply(ovl->manager);
239 static ssize_t overlay_global_alpha_show(struct omap_overlay *ovl, char *buf)
241 return snprintf(buf, PAGE_SIZE, "%d\n",
242 ovl->info.global_alpha);
245 static ssize_t overlay_global_alpha_store(struct omap_overlay *ovl,
246 const char *buf, size_t size)
250 struct omap_overlay_info info;
252 if ((ovl->caps & OMAP_DSS_OVL_CAP_GLOBAL_ALPHA) == 0)
255 r = kstrtou8(buf, 0, &alpha);
259 ovl->get_overlay_info(ovl, &info);
261 info.global_alpha = alpha;
263 r = ovl->set_overlay_info(ovl, &info);
268 r = ovl->manager->apply(ovl->manager);
276 static ssize_t overlay_pre_mult_alpha_show(struct omap_overlay *ovl,
279 return snprintf(buf, PAGE_SIZE, "%d\n",
280 ovl->info.pre_mult_alpha);
283 static ssize_t overlay_pre_mult_alpha_store(struct omap_overlay *ovl,
284 const char *buf, size_t size)
288 struct omap_overlay_info info;
290 if ((ovl->caps & OMAP_DSS_OVL_CAP_PRE_MULT_ALPHA) == 0)
293 r = kstrtou8(buf, 0, &alpha);
297 ovl->get_overlay_info(ovl, &info);
299 info.pre_mult_alpha = alpha;
301 r = ovl->set_overlay_info(ovl, &info);
306 r = ovl->manager->apply(ovl->manager);
314 static ssize_t overlay_zorder_show(struct omap_overlay *ovl, char *buf)
316 return snprintf(buf, PAGE_SIZE, "%d\n", ovl->info.zorder);
319 static ssize_t overlay_zorder_store(struct omap_overlay *ovl,
320 const char *buf, size_t size)
324 struct omap_overlay_info info;
326 if ((ovl->caps & OMAP_DSS_OVL_CAP_ZORDER) == 0)
329 r = kstrtou8(buf, 0, &zorder);
333 ovl->get_overlay_info(ovl, &info);
335 info.zorder = zorder;
337 r = ovl->set_overlay_info(ovl, &info);
342 r = ovl->manager->apply(ovl->manager);
350 struct overlay_attribute {
351 struct attribute attr;
352 ssize_t (*show)(struct omap_overlay *, char *);
353 ssize_t (*store)(struct omap_overlay *, const char *, size_t);
356 #define OVERLAY_ATTR(_name, _mode, _show, _store) \
357 struct overlay_attribute overlay_attr_##_name = \
358 __ATTR(_name, _mode, _show, _store)
360 static OVERLAY_ATTR(name, S_IRUGO, overlay_name_show, NULL);
361 static OVERLAY_ATTR(manager, S_IRUGO|S_IWUSR,
362 overlay_manager_show, overlay_manager_store);
363 static OVERLAY_ATTR(input_size, S_IRUGO, overlay_input_size_show, NULL);
364 static OVERLAY_ATTR(screen_width, S_IRUGO, overlay_screen_width_show, NULL);
365 static OVERLAY_ATTR(position, S_IRUGO|S_IWUSR,
366 overlay_position_show, overlay_position_store);
367 static OVERLAY_ATTR(output_size, S_IRUGO|S_IWUSR,
368 overlay_output_size_show, overlay_output_size_store);
369 static OVERLAY_ATTR(enabled, S_IRUGO|S_IWUSR,
370 overlay_enabled_show, overlay_enabled_store);
371 static OVERLAY_ATTR(global_alpha, S_IRUGO|S_IWUSR,
372 overlay_global_alpha_show, overlay_global_alpha_store);
373 static OVERLAY_ATTR(pre_mult_alpha, S_IRUGO|S_IWUSR,
374 overlay_pre_mult_alpha_show,
375 overlay_pre_mult_alpha_store);
376 static OVERLAY_ATTR(zorder, S_IRUGO|S_IWUSR,
377 overlay_zorder_show, overlay_zorder_store);
379 static struct attribute *overlay_sysfs_attrs[] = {
380 &overlay_attr_name.attr,
381 &overlay_attr_manager.attr,
382 &overlay_attr_input_size.attr,
383 &overlay_attr_screen_width.attr,
384 &overlay_attr_position.attr,
385 &overlay_attr_output_size.attr,
386 &overlay_attr_enabled.attr,
387 &overlay_attr_global_alpha.attr,
388 &overlay_attr_pre_mult_alpha.attr,
389 &overlay_attr_zorder.attr,
393 static ssize_t overlay_attr_show(struct kobject *kobj, struct attribute *attr,
396 struct omap_overlay *overlay;
397 struct overlay_attribute *overlay_attr;
399 overlay = container_of(kobj, struct omap_overlay, kobj);
400 overlay_attr = container_of(attr, struct overlay_attribute, attr);
402 if (!overlay_attr->show)
405 return overlay_attr->show(overlay, buf);
408 static ssize_t overlay_attr_store(struct kobject *kobj, struct attribute *attr,
409 const char *buf, size_t size)
411 struct omap_overlay *overlay;
412 struct overlay_attribute *overlay_attr;
414 overlay = container_of(kobj, struct omap_overlay, kobj);
415 overlay_attr = container_of(attr, struct overlay_attribute, attr);
417 if (!overlay_attr->store)
420 return overlay_attr->store(overlay, buf, size);
423 static const struct sysfs_ops overlay_sysfs_ops = {
424 .show = overlay_attr_show,
425 .store = overlay_attr_store,
428 static struct kobj_type overlay_ktype = {
429 .sysfs_ops = &overlay_sysfs_ops,
430 .default_attrs = overlay_sysfs_attrs,
433 /* Check if overlay parameters are compatible with display */
434 int dss_check_overlay(struct omap_overlay *ovl, struct omap_dss_device *dssdev)
436 struct omap_overlay_info *info;
444 if (!ovl->info.enabled)
449 if (info->paddr == 0) {
450 DSSDBG("check_overlay failed: paddr 0\n");
454 dssdev->driver->get_resolution(dssdev, &dw, &dh);
456 DSSDBG("check_overlay %d: (%d,%d %dx%d -> %dx%d) disp (%dx%d)\n",
458 info->pos_x, info->pos_y,
459 info->width, info->height,
460 info->out_width, info->out_height,
463 if ((ovl->caps & OMAP_DSS_OVL_CAP_SCALE) == 0) {
467 if (info->out_width == 0)
470 outw = info->out_width;
472 if (info->out_height == 0)
475 outh = info->out_height;
478 if (dw < info->pos_x + outw) {
479 DSSDBG("check_overlay failed 1: %d < %d + %d\n",
480 dw, info->pos_x, outw);
484 if (dh < info->pos_y + outh) {
485 DSSDBG("check_overlay failed 2: %d < %d + %d\n",
486 dh, info->pos_y, outh);
490 if ((ovl->supported_modes & info->color_mode) == 0) {
491 DSSERR("overlay doesn't support mode %d\n", info->color_mode);
495 if (ovl->caps & OMAP_DSS_OVL_CAP_ZORDER) {
496 if (info->zorder < 0 || info->zorder > 3) {
497 DSSERR("zorder out of range: %d\n",
502 * Check that zorder doesn't match with zorder of any other
503 * overlay which is enabled and is also connected to the same
506 for (i = 0; i < omap_dss_get_num_overlays(); i++) {
507 struct omap_overlay *tmp_ovl = omap_dss_get_overlay(i);
509 if (tmp_ovl->id != ovl->id &&
510 tmp_ovl->manager == ovl->manager &&
511 tmp_ovl->info.enabled == true &&
512 tmp_ovl->info.zorder == info->zorder) {
513 DSSERR("%s and %s have same zorder: %d\n",
514 ovl->name, tmp_ovl->name, info->zorder);
523 static int dss_ovl_set_overlay_info(struct omap_overlay *ovl,
524 struct omap_overlay_info *info)
527 struct omap_overlay_info old_info;
529 old_info = ovl->info;
533 r = dss_check_overlay(ovl, ovl->manager->device);
535 ovl->info = old_info;
540 ovl->info_dirty = true;
545 static void dss_ovl_get_overlay_info(struct omap_overlay *ovl,
546 struct omap_overlay_info *info)
551 static int dss_ovl_wait_for_go(struct omap_overlay *ovl)
553 return dss_mgr_wait_for_go_ovl(ovl);
556 static int omap_dss_set_manager(struct omap_overlay *ovl,
557 struct omap_overlay_manager *mgr)
563 DSSERR("overlay '%s' already has a manager '%s'\n",
564 ovl->name, ovl->manager->name);
568 if (ovl->info.enabled) {
569 DSSERR("overlay has to be disabled to change the manager\n");
574 ovl->manager_changed = true;
576 /* XXX: When there is an overlay on a DSI manual update display, and
577 * the overlay is first disabled, then moved to tv, and enabled, we
578 * seem to get SYNC_LOST_DIGIT error.
580 * Waiting doesn't seem to help, but updating the manual update display
581 * after disabling the overlay seems to fix this. This hints that the
582 * overlay is perhaps somehow tied to the LCD output until the output
585 * Userspace workaround for this is to update the LCD after disabling
586 * the overlay, but before moving the overlay to TV.
592 static int omap_dss_unset_manager(struct omap_overlay *ovl)
595 DSSERR("failed to detach overlay: manager not set\n");
599 if (ovl->info.enabled) {
600 DSSERR("overlay has to be disabled to unset the manager\n");
605 ovl->manager_changed = true;
610 int omap_dss_get_num_overlays(void)
614 EXPORT_SYMBOL(omap_dss_get_num_overlays);
616 struct omap_overlay *omap_dss_get_overlay(int num)
619 struct omap_overlay *ovl;
621 list_for_each_entry(ovl, &overlay_list, list) {
628 EXPORT_SYMBOL(omap_dss_get_overlay);
630 static void omap_dss_add_overlay(struct omap_overlay *overlay)
633 list_add_tail(&overlay->list, &overlay_list);
636 static struct omap_overlay *dispc_overlays[MAX_DSS_OVERLAYS];
638 void dss_overlay_setup_dispc_manager(struct omap_overlay_manager *mgr)
640 mgr->num_overlays = dss_feat_get_num_ovls();
641 mgr->overlays = dispc_overlays;
645 static struct omap_overlay *l4_overlays[1];
646 void dss_overlay_setup_l4_manager(struct omap_overlay_manager *mgr)
648 mgr->num_overlays = 1;
649 mgr->overlays = l4_overlays;
653 void dss_init_overlays(struct platform_device *pdev)
657 INIT_LIST_HEAD(&overlay_list);
661 for (i = 0; i < dss_feat_get_num_ovls(); ++i) {
662 struct omap_overlay *ovl;
663 ovl = kzalloc(sizeof(*ovl), GFP_KERNEL);
670 ovl->id = OMAP_DSS_GFX;
671 ovl->info.global_alpha = 255;
672 ovl->info.zorder = 0;
676 ovl->id = OMAP_DSS_VIDEO1;
677 ovl->info.global_alpha = 255;
679 dss_has_feature(FEAT_ALPHA_FREE_ZORDER) ? 3 : 0;
683 ovl->id = OMAP_DSS_VIDEO2;
684 ovl->info.global_alpha = 255;
686 dss_has_feature(FEAT_ALPHA_FREE_ZORDER) ? 2 : 0;
690 ovl->id = OMAP_DSS_VIDEO3;
691 ovl->info.global_alpha = 255;
693 dss_has_feature(FEAT_ALPHA_FREE_ZORDER) ? 1 : 0;
697 ovl->set_manager = &omap_dss_set_manager;
698 ovl->unset_manager = &omap_dss_unset_manager;
699 ovl->set_overlay_info = &dss_ovl_set_overlay_info;
700 ovl->get_overlay_info = &dss_ovl_get_overlay_info;
701 ovl->wait_for_go = &dss_ovl_wait_for_go;
703 ovl->caps = dss_feat_get_overlay_caps(ovl->id);
704 ovl->supported_modes =
705 dss_feat_get_supported_color_modes(ovl->id);
707 omap_dss_add_overlay(ovl);
709 r = kobject_init_and_add(&ovl->kobj, &overlay_ktype,
710 &pdev->dev.kobj, "overlay%d", i);
713 DSSERR("failed to create sysfs file\n");
717 dispc_overlays[i] = ovl;
722 struct omap_overlay *ovl;
723 ovl = kzalloc(sizeof(*ovl), GFP_KERNEL);
728 ovl->supported_modes = OMAP_DSS_COLOR_RGB24U;
730 ovl->set_manager = &omap_dss_set_manager;
731 ovl->unset_manager = &omap_dss_unset_manager;
732 ovl->set_overlay_info = &dss_ovl_set_overlay_info;
733 ovl->get_overlay_info = &dss_ovl_get_overlay_info;
735 omap_dss_add_overlay(ovl);
737 r = kobject_init_and_add(&ovl->kobj, &overlay_ktype,
738 &pdev->dev.kobj, "overlayl4");
741 DSSERR("failed to create sysfs file\n");
743 l4_overlays[0] = ovl;
748 /* connect overlays to the new device, if not already connected. if force
749 * selected, connect always. */
750 void dss_recheck_connections(struct omap_dss_device *dssdev, bool force)
753 struct omap_overlay_manager *lcd_mgr;
754 struct omap_overlay_manager *tv_mgr;
755 struct omap_overlay_manager *lcd2_mgr = NULL;
756 struct omap_overlay_manager *mgr = NULL;
758 lcd_mgr = omap_dss_get_overlay_manager(OMAP_DSS_OVL_MGR_LCD);
759 tv_mgr = omap_dss_get_overlay_manager(OMAP_DSS_OVL_MGR_TV);
760 if (dss_has_feature(FEAT_MGR_LCD2))
761 lcd2_mgr = omap_dss_get_overlay_manager(OMAP_DSS_OVL_MGR_LCD2);
763 if (dssdev->channel == OMAP_DSS_CHANNEL_LCD2) {
764 if (!lcd2_mgr->device || force) {
765 if (lcd2_mgr->device)
766 lcd2_mgr->unset_device(lcd2_mgr);
767 lcd2_mgr->set_device(lcd2_mgr, dssdev);
770 } else if (dssdev->type != OMAP_DISPLAY_TYPE_VENC
771 && dssdev->type != OMAP_DISPLAY_TYPE_HDMI) {
772 if (!lcd_mgr->device || force) {
774 lcd_mgr->unset_device(lcd_mgr);
775 lcd_mgr->set_device(lcd_mgr, dssdev);
780 if (dssdev->type == OMAP_DISPLAY_TYPE_VENC
781 || dssdev->type == OMAP_DISPLAY_TYPE_HDMI) {
782 if (!tv_mgr->device || force) {
784 tv_mgr->unset_device(tv_mgr);
785 tv_mgr->set_device(tv_mgr, dssdev);
793 for (i = 0; i < dss_feat_get_num_ovls(); i++) {
794 struct omap_overlay *ovl;
795 ovl = omap_dss_get_overlay(i);
796 if (!ovl->manager || force) {
798 omap_dss_unset_manager(ovl);
799 omap_dss_set_manager(ovl, mgr);
807 void dss_uninit_overlays(struct platform_device *pdev)
809 struct omap_overlay *ovl;
811 while (!list_empty(&overlay_list)) {
812 ovl = list_first_entry(&overlay_list,
813 struct omap_overlay, list);
814 list_del(&ovl->list);
815 kobject_del(&ovl->kobj);
816 kobject_put(&ovl->kobj);