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 static ssize_t overlay_filter_coef_show(struct omap_overlay *ovl, char *buf,
353 ssize_t ret, len = 0;
357 for (i = 0; i < 8; i++) {
358 dispc_get_scale_coef_phase(ovl->id, which, i, vals);
359 ret = snprintf(buf, PAGE_SIZE - len, "%3d %3d %3d %3d %3d\n",
360 vals[0], vals[1], vals[2], vals[3], vals[4]);
368 static ssize_t overlay_filter_coef_store(struct omap_overlay *ovl,
369 const char *buf, size_t size, int which)
376 for (i = 0; i < 8; i++) {
377 ret = sscanf(p, "%d %d %d %d %d\n", &vals[i][0], &vals[i][1],
378 &vals[i][2], &vals[i][3], &vals[i][4]);
380 DSSWARN("parse err, line %d, ret %d\n", i, ret);
384 while (*p != 0 && *p != '\n')
390 for (i = 0; i < 8; i++)
391 dispc_set_scale_coef_phase(ovl->id, which, i, &vals[i][0]);
393 if (ovl->manager && (ret = ovl->manager->apply(ovl->manager)))
399 static ssize_t overlay_filter_coef_up_h_show(struct omap_overlay *ovl, char *buf)
401 return overlay_filter_coef_show(ovl, buf, OMAP_DSS_FILTER_UP_H);
404 static ssize_t overlay_filter_coef_up_h_store(struct omap_overlay *ovl,
405 const char *buf, size_t size)
407 return overlay_filter_coef_store(ovl, buf, size, OMAP_DSS_FILTER_UP_H);
410 static ssize_t overlay_filter_coef_up_v3_show(struct omap_overlay *ovl, char *buf)
412 return overlay_filter_coef_show(ovl, buf, OMAP_DSS_FILTER_UP_V3);
415 static ssize_t overlay_filter_coef_up_v3_store(struct omap_overlay *ovl,
416 const char *buf, size_t size)
418 return overlay_filter_coef_store(ovl, buf, size, OMAP_DSS_FILTER_UP_V3);
421 static ssize_t overlay_filter_coef_up_v5_show(struct omap_overlay *ovl, char *buf)
423 return overlay_filter_coef_show(ovl, buf, OMAP_DSS_FILTER_UP_V5);
426 static ssize_t overlay_filter_coef_up_v5_store(struct omap_overlay *ovl,
427 const char *buf, size_t size)
429 return overlay_filter_coef_store(ovl, buf, size, OMAP_DSS_FILTER_UP_V5);
432 struct overlay_attribute {
433 struct attribute attr;
434 ssize_t (*show)(struct omap_overlay *, char *);
435 ssize_t (*store)(struct omap_overlay *, const char *, size_t);
438 #define OVERLAY_ATTR(_name, _mode, _show, _store) \
439 struct overlay_attribute overlay_attr_##_name = \
440 __ATTR(_name, _mode, _show, _store)
442 static OVERLAY_ATTR(name, S_IRUGO, overlay_name_show, NULL);
443 static OVERLAY_ATTR(manager, S_IRUGO|S_IWUSR,
444 overlay_manager_show, overlay_manager_store);
445 static OVERLAY_ATTR(input_size, S_IRUGO, overlay_input_size_show, NULL);
446 static OVERLAY_ATTR(screen_width, S_IRUGO, overlay_screen_width_show, NULL);
447 static OVERLAY_ATTR(position, S_IRUGO|S_IWUSR,
448 overlay_position_show, overlay_position_store);
449 static OVERLAY_ATTR(output_size, S_IRUGO|S_IWUSR,
450 overlay_output_size_show, overlay_output_size_store);
451 static OVERLAY_ATTR(enabled, S_IRUGO|S_IWUSR,
452 overlay_enabled_show, overlay_enabled_store);
453 static OVERLAY_ATTR(global_alpha, S_IRUGO|S_IWUSR,
454 overlay_global_alpha_show, overlay_global_alpha_store);
455 static OVERLAY_ATTR(pre_mult_alpha, S_IRUGO|S_IWUSR,
456 overlay_pre_mult_alpha_show,
457 overlay_pre_mult_alpha_store);
458 static OVERLAY_ATTR(zorder, S_IRUGO|S_IWUSR,
459 overlay_zorder_show, overlay_zorder_store);
460 static OVERLAY_ATTR(filter_coef_up_h, S_IRUGO|S_IWUSR,
461 overlay_filter_coef_up_h_show, overlay_filter_coef_up_h_store);
462 static OVERLAY_ATTR(filter_coef_up_v3, S_IRUGO|S_IWUSR,
463 overlay_filter_coef_up_v3_show, overlay_filter_coef_up_v3_store);
464 static OVERLAY_ATTR(filter_coef_up_v5, S_IRUGO|S_IWUSR,
465 overlay_filter_coef_up_v5_show, overlay_filter_coef_up_v5_store);
467 static struct attribute *overlay_sysfs_attrs[] = {
468 &overlay_attr_name.attr,
469 &overlay_attr_manager.attr,
470 &overlay_attr_input_size.attr,
471 &overlay_attr_screen_width.attr,
472 &overlay_attr_position.attr,
473 &overlay_attr_output_size.attr,
474 &overlay_attr_enabled.attr,
475 &overlay_attr_global_alpha.attr,
476 &overlay_attr_pre_mult_alpha.attr,
477 &overlay_attr_zorder.attr,
478 &overlay_attr_filter_coef_up_h.attr,
479 &overlay_attr_filter_coef_up_v3.attr,
480 &overlay_attr_filter_coef_up_v5.attr,
484 static ssize_t overlay_attr_show(struct kobject *kobj, struct attribute *attr,
487 struct omap_overlay *overlay;
488 struct overlay_attribute *overlay_attr;
490 overlay = container_of(kobj, struct omap_overlay, kobj);
491 overlay_attr = container_of(attr, struct overlay_attribute, attr);
493 if (!overlay_attr->show)
496 return overlay_attr->show(overlay, buf);
499 static ssize_t overlay_attr_store(struct kobject *kobj, struct attribute *attr,
500 const char *buf, size_t size)
502 struct omap_overlay *overlay;
503 struct overlay_attribute *overlay_attr;
505 overlay = container_of(kobj, struct omap_overlay, kobj);
506 overlay_attr = container_of(attr, struct overlay_attribute, attr);
508 if (!overlay_attr->store)
511 return overlay_attr->store(overlay, buf, size);
514 static const struct sysfs_ops overlay_sysfs_ops = {
515 .show = overlay_attr_show,
516 .store = overlay_attr_store,
519 static struct kobj_type overlay_ktype = {
520 .sysfs_ops = &overlay_sysfs_ops,
521 .default_attrs = overlay_sysfs_attrs,
524 /* Check if overlay parameters are compatible with display */
525 int dss_check_overlay(struct omap_overlay *ovl, struct omap_dss_device *dssdev)
527 struct omap_overlay_info *info;
535 if (!ovl->info.enabled)
540 if (info->paddr == 0) {
541 DSSDBG("check_overlay failed: paddr 0\n");
545 dssdev->driver->get_resolution(dssdev, &dw, &dh);
547 DSSDBG("check_overlay %d: (%d,%d %dx%d -> %dx%d) disp (%dx%d)\n",
549 info->pos_x, info->pos_y,
550 info->width, info->height,
551 info->out_width, info->out_height,
554 if ((ovl->caps & OMAP_DSS_OVL_CAP_SCALE) == 0) {
558 if (info->out_width == 0)
561 outw = info->out_width;
563 if (info->out_height == 0)
566 outh = info->out_height;
569 if (dw < info->pos_x + outw) {
570 DSSDBG("check_overlay failed 1: %d < %d + %d\n",
571 dw, info->pos_x, outw);
575 if (dh < info->pos_y + outh) {
576 DSSDBG("check_overlay failed 2: %d < %d + %d\n",
577 dh, info->pos_y, outh);
581 if ((ovl->supported_modes & info->color_mode) == 0) {
582 DSSERR("overlay doesn't support mode %d\n", info->color_mode);
586 if (ovl->caps & OMAP_DSS_OVL_CAP_ZORDER) {
587 if (info->zorder < 0 || info->zorder > 3) {
588 DSSERR("zorder out of range: %d\n",
593 * Check that zorder doesn't match with zorder of any other
594 * overlay which is enabled and is also connected to the same
597 for (i = 0; i < omap_dss_get_num_overlays(); i++) {
598 struct omap_overlay *tmp_ovl = omap_dss_get_overlay(i);
600 if (tmp_ovl->id != ovl->id &&
601 tmp_ovl->manager == ovl->manager &&
602 tmp_ovl->info.enabled == true &&
603 tmp_ovl->info.zorder == info->zorder) {
604 DSSERR("%s and %s have same zorder: %d\n",
605 ovl->name, tmp_ovl->name, info->zorder);
614 static int dss_ovl_set_overlay_info(struct omap_overlay *ovl,
615 struct omap_overlay_info *info)
618 struct omap_overlay_info old_info;
620 old_info = ovl->info;
624 r = dss_check_overlay(ovl, ovl->manager->device);
626 ovl->info = old_info;
631 ovl->info_dirty = true;
636 static void dss_ovl_get_overlay_info(struct omap_overlay *ovl,
637 struct omap_overlay_info *info)
642 static int dss_ovl_wait_for_go(struct omap_overlay *ovl)
644 return dss_mgr_wait_for_go_ovl(ovl);
647 static int omap_dss_set_manager(struct omap_overlay *ovl,
648 struct omap_overlay_manager *mgr)
654 DSSERR("overlay '%s' already has a manager '%s'\n",
655 ovl->name, ovl->manager->name);
659 if (ovl->info.enabled) {
660 DSSERR("overlay has to be disabled to change the manager\n");
665 ovl->manager_changed = true;
667 /* XXX: When there is an overlay on a DSI manual update display, and
668 * the overlay is first disabled, then moved to tv, and enabled, we
669 * seem to get SYNC_LOST_DIGIT error.
671 * Waiting doesn't seem to help, but updating the manual update display
672 * after disabling the overlay seems to fix this. This hints that the
673 * overlay is perhaps somehow tied to the LCD output until the output
676 * Userspace workaround for this is to update the LCD after disabling
677 * the overlay, but before moving the overlay to TV.
683 static int omap_dss_unset_manager(struct omap_overlay *ovl)
686 DSSERR("failed to detach overlay: manager not set\n");
690 if (ovl->info.enabled) {
691 DSSERR("overlay has to be disabled to unset the manager\n");
696 ovl->manager_changed = true;
701 int omap_dss_get_num_overlays(void)
705 EXPORT_SYMBOL(omap_dss_get_num_overlays);
707 struct omap_overlay *omap_dss_get_overlay(int num)
710 struct omap_overlay *ovl;
712 list_for_each_entry(ovl, &overlay_list, list) {
719 EXPORT_SYMBOL(omap_dss_get_overlay);
721 static void omap_dss_add_overlay(struct omap_overlay *overlay)
724 list_add_tail(&overlay->list, &overlay_list);
727 static struct omap_overlay *dispc_overlays[MAX_DSS_OVERLAYS];
729 void dss_overlay_setup_dispc_manager(struct omap_overlay_manager *mgr)
731 mgr->num_overlays = dss_feat_get_num_ovls();
732 mgr->overlays = dispc_overlays;
736 static struct omap_overlay *l4_overlays[1];
737 void dss_overlay_setup_l4_manager(struct omap_overlay_manager *mgr)
739 mgr->num_overlays = 1;
740 mgr->overlays = l4_overlays;
744 void dss_init_overlays(struct platform_device *pdev)
748 INIT_LIST_HEAD(&overlay_list);
752 for (i = 0; i < dss_feat_get_num_ovls(); ++i) {
753 struct omap_overlay *ovl;
754 ovl = kzalloc(sizeof(*ovl), GFP_KERNEL);
761 ovl->id = OMAP_DSS_GFX;
762 ovl->info.global_alpha = 255;
763 ovl->info.zorder = 0;
767 ovl->id = OMAP_DSS_VIDEO1;
768 ovl->info.global_alpha = 255;
770 dss_has_feature(FEAT_ALPHA_FREE_ZORDER) ? 3 : 0;
774 ovl->id = OMAP_DSS_VIDEO2;
775 ovl->info.global_alpha = 255;
777 dss_has_feature(FEAT_ALPHA_FREE_ZORDER) ? 2 : 0;
781 ovl->id = OMAP_DSS_VIDEO3;
782 ovl->info.global_alpha = 255;
784 dss_has_feature(FEAT_ALPHA_FREE_ZORDER) ? 1 : 0;
788 ovl->set_manager = &omap_dss_set_manager;
789 ovl->unset_manager = &omap_dss_unset_manager;
790 ovl->set_overlay_info = &dss_ovl_set_overlay_info;
791 ovl->get_overlay_info = &dss_ovl_get_overlay_info;
792 ovl->wait_for_go = &dss_ovl_wait_for_go;
794 ovl->caps = dss_feat_get_overlay_caps(ovl->id);
795 ovl->supported_modes =
796 dss_feat_get_supported_color_modes(ovl->id);
798 omap_dss_add_overlay(ovl);
800 r = kobject_init_and_add(&ovl->kobj, &overlay_ktype,
801 &pdev->dev.kobj, "overlay%d", i);
804 DSSERR("failed to create sysfs file\n");
808 dispc_overlays[i] = ovl;
813 struct omap_overlay *ovl;
814 ovl = kzalloc(sizeof(*ovl), GFP_KERNEL);
819 ovl->supported_modes = OMAP_DSS_COLOR_RGB24U;
821 ovl->set_manager = &omap_dss_set_manager;
822 ovl->unset_manager = &omap_dss_unset_manager;
823 ovl->set_overlay_info = &dss_ovl_set_overlay_info;
824 ovl->get_overlay_info = &dss_ovl_get_overlay_info;
826 omap_dss_add_overlay(ovl);
828 r = kobject_init_and_add(&ovl->kobj, &overlay_ktype,
829 &pdev->dev.kobj, "overlayl4");
832 DSSERR("failed to create sysfs file\n");
834 l4_overlays[0] = ovl;
839 /* connect overlays to the new device, if not already connected. if force
840 * selected, connect always. */
841 void dss_recheck_connections(struct omap_dss_device *dssdev, bool force)
844 struct omap_overlay_manager *lcd_mgr;
845 struct omap_overlay_manager *tv_mgr;
846 struct omap_overlay_manager *lcd2_mgr = NULL;
847 struct omap_overlay_manager *mgr = NULL;
849 lcd_mgr = omap_dss_get_overlay_manager(OMAP_DSS_OVL_MGR_LCD);
850 tv_mgr = omap_dss_get_overlay_manager(OMAP_DSS_OVL_MGR_TV);
851 if (dss_has_feature(FEAT_MGR_LCD2))
852 lcd2_mgr = omap_dss_get_overlay_manager(OMAP_DSS_OVL_MGR_LCD2);
854 if (dssdev->channel == OMAP_DSS_CHANNEL_LCD2) {
855 if (!lcd2_mgr->device || force) {
856 if (lcd2_mgr->device)
857 lcd2_mgr->unset_device(lcd2_mgr);
858 lcd2_mgr->set_device(lcd2_mgr, dssdev);
861 } else if (dssdev->type != OMAP_DISPLAY_TYPE_VENC
862 && dssdev->type != OMAP_DISPLAY_TYPE_HDMI) {
863 if (!lcd_mgr->device || force) {
865 lcd_mgr->unset_device(lcd_mgr);
866 lcd_mgr->set_device(lcd_mgr, dssdev);
871 if (dssdev->type == OMAP_DISPLAY_TYPE_VENC
872 || dssdev->type == OMAP_DISPLAY_TYPE_HDMI) {
873 if (!tv_mgr->device || force) {
875 tv_mgr->unset_device(tv_mgr);
876 tv_mgr->set_device(tv_mgr, dssdev);
884 for (i = 0; i < dss_feat_get_num_ovls(); i++) {
885 struct omap_overlay *ovl;
886 ovl = omap_dss_get_overlay(i);
887 if (!ovl->manager || force) {
889 omap_dss_unset_manager(ovl);
890 omap_dss_set_manager(ovl, mgr);
898 void dss_uninit_overlays(struct platform_device *pdev)
900 struct omap_overlay *ovl;
902 while (!list_empty(&overlay_list)) {
903 ovl = list_first_entry(&overlay_list,
904 struct omap_overlay, list);
905 list_del(&ovl->list);
906 kobject_del(&ovl->kobj);
907 kobject_put(&ovl->kobj);