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 display->get_resolution(display, &dw, &dh);
286 DSSDBG("check_overlay %d: (%d,%d %dx%d -> %dx%d) disp (%dx%d)\n",
288 info->pos_x, info->pos_y,
289 info->width, info->height,
290 info->out_width, info->out_height,
293 if ((ovl->caps & OMAP_DSS_OVL_CAP_SCALE) == 0) {
297 if (info->out_width == 0)
300 outw = info->out_width;
302 if (info->out_height == 0)
305 outh = info->out_height;
308 if (dw < info->pos_x + outw) {
309 DSSDBG("check_overlay failed 1: %d < %d + %d\n",
310 dw, info->pos_x, outw);
314 if (dh < info->pos_y + outh) {
315 DSSDBG("check_overlay failed 2: %d < %d + %d\n",
316 dh, info->pos_y, outh);
320 if ((ovl->supported_modes & info->color_mode) == 0) {
321 DSSERR("overlay doesn't support mode %d\n", info->color_mode);
328 static int dss_ovl_set_overlay_info(struct omap_overlay *ovl,
329 struct omap_overlay_info *info)
332 struct omap_overlay_info old_info;
334 if (info->paddr == 0)
337 old_info = ovl->info;
341 r = dss_check_overlay(ovl, ovl->manager->display);
343 ovl->info = old_info;
351 static void dss_ovl_get_overlay_info(struct omap_overlay *ovl,
352 struct omap_overlay_info *info)
357 static int omap_dss_set_manager(struct omap_overlay *ovl,
358 struct omap_overlay_manager *mgr)
363 DSSERR("overlay '%s' already has a manager '%s'\n",
364 ovl->name, ovl->manager->name);
367 r = dss_check_overlay(ovl, mgr->display);
376 static int omap_dss_unset_manager(struct omap_overlay *ovl)
379 DSSERR("failed to detach overlay: manager not set\n");
388 int omap_dss_get_num_overlays(void)
392 EXPORT_SYMBOL(omap_dss_get_num_overlays);
394 struct omap_overlay *omap_dss_get_overlay(int num)
397 struct omap_overlay *ovl;
399 list_for_each_entry(ovl, &overlay_list, list) {
406 EXPORT_SYMBOL(omap_dss_get_overlay);
408 static void omap_dss_add_overlay(struct omap_overlay *overlay)
411 list_add_tail(&overlay->list, &overlay_list);
414 static struct omap_overlay *dispc_overlays[3];
416 void dss_overlay_setup_dispc_manager(struct omap_overlay_manager *mgr)
418 mgr->num_overlays = 3;
419 mgr->overlays = dispc_overlays;
422 void dss_init_overlays(struct platform_device *pdev, const char *def_disp_name)
425 struct omap_overlay_manager *lcd_mgr;
426 struct omap_overlay_manager *tv_mgr;
427 struct omap_overlay_manager *def_mgr = NULL;
429 INIT_LIST_HEAD(&overlay_list);
433 for (i = 0; i < 3; ++i) {
434 struct omap_overlay *ovl;
435 ovl = kzalloc(sizeof(*ovl), GFP_KERNEL);
442 ovl->id = OMAP_DSS_GFX;
443 ovl->supported_modes = OMAP_DSS_COLOR_GFX_OMAP3;
444 ovl->caps = OMAP_DSS_OVL_CAP_DISPC;
448 ovl->id = OMAP_DSS_VIDEO1;
449 ovl->supported_modes = OMAP_DSS_COLOR_VID_OMAP3;
450 ovl->caps = OMAP_DSS_OVL_CAP_SCALE |
451 OMAP_DSS_OVL_CAP_DISPC;
455 ovl->id = OMAP_DSS_VIDEO2;
456 ovl->supported_modes = OMAP_DSS_COLOR_VID_OMAP3;
457 ovl->caps = OMAP_DSS_OVL_CAP_SCALE |
458 OMAP_DSS_OVL_CAP_DISPC;
462 ovl->set_manager = &omap_dss_set_manager;
463 ovl->unset_manager = &omap_dss_unset_manager;
464 ovl->set_overlay_info = &dss_ovl_set_overlay_info;
465 ovl->get_overlay_info = &dss_ovl_get_overlay_info;
467 omap_dss_add_overlay(ovl);
469 r = kobject_init_and_add(&ovl->kobj, &overlay_ktype,
470 &pdev->dev.kobj, "overlay%d", i);
473 DSSERR("failed to create sysfs file\n");
477 dispc_overlays[i] = ovl;
480 lcd_mgr = omap_dss_get_overlay_manager(OMAP_DSS_OVL_MGR_LCD);
481 tv_mgr = omap_dss_get_overlay_manager(OMAP_DSS_OVL_MGR_TV);
484 for (i = 0; i < omap_dss_get_num_displays() ; i++) {
485 struct omap_display *display = dss_get_display(i);
487 if (strcmp(display->name, def_disp_name) == 0) {
488 if (display->type != OMAP_DISPLAY_TYPE_VENC) {
489 lcd_mgr->set_display(lcd_mgr, display);
492 lcd_mgr->set_display(tv_mgr, display);
501 DSSWARN("default display %s not found\n",
505 if (def_mgr != lcd_mgr) {
506 /* connect lcd manager to first non-VENC display found */
507 for (i = 0; i < omap_dss_get_num_displays(); i++) {
508 struct omap_display *display = dss_get_display(i);
509 if (display->type != OMAP_DISPLAY_TYPE_VENC) {
510 lcd_mgr->set_display(lcd_mgr, display);
520 if (def_mgr != tv_mgr) {
521 /* connect tv manager to first VENC display found */
522 for (i = 0; i < omap_dss_get_num_displays(); i++) {
523 struct omap_display *display = dss_get_display(i);
524 if (display->type == OMAP_DISPLAY_TYPE_VENC) {
525 tv_mgr->set_display(tv_mgr, display);
535 /* connect all dispc overlays to def_mgr */
537 for (i = 0; i < 3; i++) {
538 struct omap_overlay *ovl;
539 ovl = omap_dss_get_overlay(i);
540 omap_dss_set_manager(ovl, def_mgr);
545 /* setup L4 overlay as an example */
547 static struct omap_overlay ovl = {
549 .supported_modes = OMAP_DSS_COLOR_RGB24U,
550 .set_manager = &omap_dss_set_manager,
551 .unset_manager = &omap_dss_unset_manager,
552 .setup_input = &omap_dss_setup_overlay_input,
553 .setup_output = &omap_dss_setup_overlay_output,
554 .enable = &omap_dss_enable_overlay,
557 static struct omap_overlay_manager mgr = {
561 .set_display = &omap_dss_set_display,
562 .unset_display = &omap_dss_unset_display,
563 .apply = &ovl_mgr_apply_l4,
564 .supported_displays =
565 OMAP_DISPLAY_TYPE_DBI | OMAP_DISPLAY_TYPE_DSI,
568 omap_dss_add_overlay(&ovl);
569 omap_dss_add_overlay_manager(&mgr);
570 omap_dss_set_manager(&ovl, &mgr);
575 void dss_uninit_overlays(struct platform_device *pdev)
577 struct omap_overlay *ovl;
579 while (!list_empty(&overlay_list)) {
580 ovl = list_first_entry(&overlay_list,
581 struct omap_overlay, list);
582 list_del(&ovl->list);
583 kobject_del(&ovl->kobj);
584 kobject_put(&ovl->kobj);