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>
32 #include <mach/display.h>
36 static int num_overlays;
37 static struct list_head overlay_list;
39 static ssize_t overlay_name_show(struct omap_overlay *ovl, char *buf)
41 return snprintf(buf, PAGE_SIZE, "%s\n", ovl->name);
44 static ssize_t overlay_manager_show(struct omap_overlay *ovl, char *buf)
46 return snprintf(buf, PAGE_SIZE, "%s\n",
47 ovl->manager ? ovl->manager->name : "<none>");
50 static ssize_t overlay_manager_store(struct omap_overlay *ovl, const char *buf, size_t size)
53 struct omap_overlay_manager *mgr = NULL;
56 if (buf[size-1] == '\n')
60 for (i = 0; i < omap_dss_get_num_overlay_managers(); ++i) {
61 mgr = omap_dss_get_overlay_manager(i);
63 if (strncmp(buf, mgr->name, len) == 0)
70 if (len > 0 && mgr == NULL)
74 DSSDBG("manager %s found\n", mgr->name);
76 if (mgr != ovl->manager) {
77 /* detach old manager */
79 r = ovl->unset_manager(ovl);
81 DSSERR("detach failed\n");
87 r = ovl->set_manager(ovl, mgr);
89 DSSERR("Failed to attach overlay\n");
95 if (ovl->manager && (r = ovl->manager->apply(ovl->manager)))
101 static ssize_t overlay_input_size_show(struct omap_overlay *ovl, char *buf)
103 return snprintf(buf, PAGE_SIZE, "%d,%d\n",
104 ovl->info.width, ovl->info.height);
107 static ssize_t overlay_screen_width_show(struct omap_overlay *ovl, char *buf)
109 return snprintf(buf, PAGE_SIZE, "%d\n", ovl->info.screen_width);
112 static ssize_t overlay_position_show(struct omap_overlay *ovl, char *buf)
114 return snprintf(buf, PAGE_SIZE, "%d,%d\n",
115 ovl->info.pos_x, ovl->info.pos_y);
118 static ssize_t overlay_position_store(struct omap_overlay *ovl,
119 const char *buf, size_t size)
123 struct omap_overlay_info info;
125 ovl->get_overlay_info(ovl, &info);
127 info.pos_x = simple_strtoul(buf, &last, 10);
129 if (last - buf >= size)
132 info.pos_y = simple_strtoul(last, &last, 10);
134 if ((r = ovl->set_overlay_info(ovl, &info)))
137 if (ovl->manager && (r = ovl->manager->apply(ovl->manager)))
143 static ssize_t overlay_output_size_show(struct omap_overlay *ovl, char *buf)
145 return snprintf(buf, PAGE_SIZE, "%d,%d\n",
146 ovl->info.out_width, ovl->info.out_height);
149 static ssize_t overlay_output_size_store(struct omap_overlay *ovl,
150 const char *buf, size_t size)
154 struct omap_overlay_info info;
156 ovl->get_overlay_info(ovl, &info);
158 info.out_width = simple_strtoul(buf, &last, 10);
160 if (last - buf >= size)
163 info.out_height = simple_strtoul(last, &last, 10);
165 if ((r = ovl->set_overlay_info(ovl, &info)))
168 if (ovl->manager && (r = ovl->manager->apply(ovl->manager)))
174 static ssize_t overlay_enabled_show(struct omap_overlay *ovl, char *buf)
176 return snprintf(buf, PAGE_SIZE, "%d\n", ovl->info.enabled);
179 static ssize_t overlay_enabled_store(struct omap_overlay *ovl, const char *buf, size_t size)
182 struct omap_overlay_info info;
184 ovl->get_overlay_info(ovl, &info);
186 info.enabled = simple_strtoul(buf, NULL, 10);
188 if ((r = ovl->set_overlay_info(ovl, &info)))
191 if (ovl->manager && (r = ovl->manager->apply(ovl->manager)))
197 struct overlay_attribute {
198 struct attribute attr;
199 ssize_t (*show)(struct omap_overlay *, char *);
200 ssize_t (*store)(struct omap_overlay *, const char *, size_t);
203 #define OVERLAY_ATTR(_name, _mode, _show, _store) \
204 struct overlay_attribute overlay_attr_##_name = \
205 __ATTR(_name, _mode, _show, _store)
207 static OVERLAY_ATTR(name, S_IRUGO, overlay_name_show, NULL);
208 static OVERLAY_ATTR(manager, S_IRUGO|S_IWUSR,
209 overlay_manager_show, overlay_manager_store);
210 static OVERLAY_ATTR(input_size, S_IRUGO, overlay_input_size_show, NULL);
211 static OVERLAY_ATTR(screen_width, S_IRUGO, overlay_screen_width_show, NULL);
212 static OVERLAY_ATTR(position, S_IRUGO|S_IWUSR,
213 overlay_position_show, overlay_position_store);
214 static OVERLAY_ATTR(output_size, S_IRUGO|S_IWUSR,
215 overlay_output_size_show, overlay_output_size_store);
216 static OVERLAY_ATTR(enabled, S_IRUGO|S_IWUSR,
217 overlay_enabled_show, overlay_enabled_store);
219 static struct attribute *overlay_sysfs_attrs[] = {
220 &overlay_attr_name.attr,
221 &overlay_attr_manager.attr,
222 &overlay_attr_input_size.attr,
223 &overlay_attr_screen_width.attr,
224 &overlay_attr_position.attr,
225 &overlay_attr_output_size.attr,
226 &overlay_attr_enabled.attr,
230 static ssize_t overlay_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
232 struct omap_overlay *overlay;
233 struct overlay_attribute *overlay_attr;
235 overlay = container_of(kobj, struct omap_overlay, kobj);
236 overlay_attr = container_of(attr, struct overlay_attribute, attr);
238 if (!overlay_attr->show)
241 return overlay_attr->show(overlay, buf);
244 static ssize_t overlay_attr_store(struct kobject *kobj, struct attribute *attr,
245 const char *buf, size_t size)
247 struct omap_overlay *overlay;
248 struct overlay_attribute *overlay_attr;
250 overlay = container_of(kobj, struct omap_overlay, kobj);
251 overlay_attr = container_of(attr, struct overlay_attribute, attr);
253 if (!overlay_attr->store)
256 return overlay_attr->store(overlay, buf, size);
259 static struct sysfs_ops overlay_sysfs_ops = {
260 .show = overlay_attr_show,
261 .store = overlay_attr_store,
264 static struct kobj_type overlay_ktype = {
265 .sysfs_ops = &overlay_sysfs_ops,
266 .default_attrs = overlay_sysfs_attrs,
269 /* Check if overlay parameters are compatible with display */
270 int dss_check_overlay(struct omap_overlay *ovl, struct omap_display *display)
272 struct omap_overlay_info *info;
279 if (!ovl->info.enabled)
284 if (info->paddr == 0) {
285 DSSDBG("check_overlay failed: paddr 0\n");
289 display->get_resolution(display, &dw, &dh);
291 DSSDBG("check_overlay %d: (%d,%d %dx%d -> %dx%d) disp (%dx%d)\n",
293 info->pos_x, info->pos_y,
294 info->width, info->height,
295 info->out_width, info->out_height,
298 if ((ovl->caps & OMAP_DSS_OVL_CAP_SCALE) == 0) {
302 if (info->out_width == 0)
305 outw = info->out_width;
307 if (info->out_height == 0)
310 outh = info->out_height;
313 if (dw < info->pos_x + outw) {
314 DSSDBG("check_overlay failed 1: %d < %d + %d\n",
315 dw, info->pos_x, outw);
319 if (dh < info->pos_y + outh) {
320 DSSDBG("check_overlay failed 2: %d < %d + %d\n",
321 dh, info->pos_y, outh);
325 if ((ovl->supported_modes & info->color_mode) == 0) {
326 DSSERR("overlay doesn't support mode %d\n", info->color_mode);
333 static int dss_ovl_set_overlay_info(struct omap_overlay *ovl,
334 struct omap_overlay_info *info)
337 struct omap_overlay_info old_info;
339 old_info = ovl->info;
343 r = dss_check_overlay(ovl, ovl->manager->display);
345 ovl->info = old_info;
353 static void dss_ovl_get_overlay_info(struct omap_overlay *ovl,
354 struct omap_overlay_info *info)
359 static int omap_dss_set_manager(struct omap_overlay *ovl,
360 struct omap_overlay_manager *mgr)
365 DSSERR("overlay '%s' already has a manager '%s'\n",
366 ovl->name, ovl->manager->name);
369 r = dss_check_overlay(ovl, mgr->display);
378 static int omap_dss_unset_manager(struct omap_overlay *ovl)
381 DSSERR("failed to detach overlay: manager not set\n");
390 int omap_dss_get_num_overlays(void)
394 EXPORT_SYMBOL(omap_dss_get_num_overlays);
396 struct omap_overlay *omap_dss_get_overlay(int num)
399 struct omap_overlay *ovl;
401 list_for_each_entry(ovl, &overlay_list, list) {
408 EXPORT_SYMBOL(omap_dss_get_overlay);
410 static void omap_dss_add_overlay(struct omap_overlay *overlay)
413 list_add_tail(&overlay->list, &overlay_list);
416 static struct omap_overlay *dispc_overlays[3];
418 void dss_overlay_setup_dispc_manager(struct omap_overlay_manager *mgr)
420 mgr->num_overlays = 3;
421 mgr->overlays = dispc_overlays;
424 void dss_init_overlays(struct platform_device *pdev, const char *def_disp_name)
427 struct omap_overlay_manager *lcd_mgr;
428 struct omap_overlay_manager *tv_mgr;
429 struct omap_overlay_manager *def_mgr = NULL;
431 INIT_LIST_HEAD(&overlay_list);
435 for (i = 0; i < 3; ++i) {
436 struct omap_overlay *ovl;
437 ovl = kzalloc(sizeof(*ovl), GFP_KERNEL);
444 ovl->id = OMAP_DSS_GFX;
445 ovl->supported_modes = OMAP_DSS_COLOR_GFX_OMAP3;
446 ovl->caps = OMAP_DSS_OVL_CAP_DISPC;
450 ovl->id = OMAP_DSS_VIDEO1;
451 ovl->supported_modes = OMAP_DSS_COLOR_VID_OMAP3;
452 ovl->caps = OMAP_DSS_OVL_CAP_SCALE |
453 OMAP_DSS_OVL_CAP_DISPC;
457 ovl->id = OMAP_DSS_VIDEO2;
458 ovl->supported_modes = OMAP_DSS_COLOR_VID_OMAP3;
459 ovl->caps = OMAP_DSS_OVL_CAP_SCALE |
460 OMAP_DSS_OVL_CAP_DISPC;
464 ovl->set_manager = &omap_dss_set_manager;
465 ovl->unset_manager = &omap_dss_unset_manager;
466 ovl->set_overlay_info = &dss_ovl_set_overlay_info;
467 ovl->get_overlay_info = &dss_ovl_get_overlay_info;
469 omap_dss_add_overlay(ovl);
471 r = kobject_init_and_add(&ovl->kobj, &overlay_ktype,
472 &pdev->dev.kobj, "overlay%d", i);
475 DSSERR("failed to create sysfs file\n");
479 dispc_overlays[i] = ovl;
482 lcd_mgr = omap_dss_get_overlay_manager(OMAP_DSS_OVL_MGR_LCD);
483 tv_mgr = omap_dss_get_overlay_manager(OMAP_DSS_OVL_MGR_TV);
486 for (i = 0; i < omap_dss_get_num_displays() ; i++) {
487 struct omap_display *display = dss_get_display(i);
489 if (strcmp(display->name, def_disp_name) == 0) {
490 if (display->type != OMAP_DISPLAY_TYPE_VENC) {
491 lcd_mgr->set_display(lcd_mgr, display);
494 lcd_mgr->set_display(tv_mgr, display);
503 DSSWARN("default display %s not found\n",
507 if (def_mgr != lcd_mgr) {
508 /* connect lcd manager to first non-VENC display found */
509 for (i = 0; i < omap_dss_get_num_displays(); i++) {
510 struct omap_display *display = dss_get_display(i);
511 if (display->type != OMAP_DISPLAY_TYPE_VENC) {
512 lcd_mgr->set_display(lcd_mgr, display);
522 if (def_mgr != tv_mgr) {
523 /* connect tv manager to first VENC display found */
524 for (i = 0; i < omap_dss_get_num_displays(); i++) {
525 struct omap_display *display = dss_get_display(i);
526 if (display->type == OMAP_DISPLAY_TYPE_VENC) {
527 tv_mgr->set_display(tv_mgr, display);
537 /* connect all dispc overlays to def_mgr */
539 for (i = 0; i < 3; i++) {
540 struct omap_overlay *ovl;
541 ovl = omap_dss_get_overlay(i);
542 omap_dss_set_manager(ovl, def_mgr);
547 /* setup L4 overlay as an example */
549 static struct omap_overlay ovl = {
551 .supported_modes = OMAP_DSS_COLOR_RGB24U,
552 .set_manager = &omap_dss_set_manager,
553 .unset_manager = &omap_dss_unset_manager,
554 .setup_input = &omap_dss_setup_overlay_input,
555 .setup_output = &omap_dss_setup_overlay_output,
556 .enable = &omap_dss_enable_overlay,
559 static struct omap_overlay_manager mgr = {
563 .set_display = &omap_dss_set_display,
564 .unset_display = &omap_dss_unset_display,
565 .apply = &ovl_mgr_apply_l4,
566 .supported_displays =
567 OMAP_DISPLAY_TYPE_DBI | OMAP_DISPLAY_TYPE_DSI,
570 omap_dss_add_overlay(&ovl);
571 omap_dss_add_overlay_manager(&mgr);
572 omap_dss_set_manager(&ovl, &mgr);
577 void dss_uninit_overlays(struct platform_device *pdev)
579 struct omap_overlay *ovl;
581 while (!list_empty(&overlay_list)) {
582 ovl = list_first_entry(&overlay_list,
583 struct omap_overlay, list);
584 list_del(&ovl->list);
585 kobject_del(&ovl->kobj);
586 kobject_put(&ovl->kobj);