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 old_info = ovl->info;
338 r = dss_check_overlay(ovl, ovl->manager->display);
340 ovl->info = old_info;
348 static void dss_ovl_get_overlay_info(struct omap_overlay *ovl,
349 struct omap_overlay_info *info)
354 static int omap_dss_set_manager(struct omap_overlay *ovl,
355 struct omap_overlay_manager *mgr)
360 DSSERR("overlay '%s' already has a manager '%s'\n",
361 ovl->name, ovl->manager->name);
364 r = dss_check_overlay(ovl, mgr->display);
373 static int omap_dss_unset_manager(struct omap_overlay *ovl)
376 DSSERR("failed to detach overlay: manager not set\n");
385 int omap_dss_get_num_overlays(void)
389 EXPORT_SYMBOL(omap_dss_get_num_overlays);
391 struct omap_overlay *omap_dss_get_overlay(int num)
394 struct omap_overlay *ovl;
396 list_for_each_entry(ovl, &overlay_list, list) {
403 EXPORT_SYMBOL(omap_dss_get_overlay);
405 static void omap_dss_add_overlay(struct omap_overlay *overlay)
408 list_add_tail(&overlay->list, &overlay_list);
411 static struct omap_overlay *dispc_overlays[3];
413 void dss_overlay_setup_dispc_manager(struct omap_overlay_manager *mgr)
415 mgr->num_overlays = 3;
416 mgr->overlays = dispc_overlays;
419 void dss_init_overlays(struct platform_device *pdev, const char *def_disp_name)
422 struct omap_overlay_manager *lcd_mgr;
423 struct omap_overlay_manager *tv_mgr;
424 struct omap_overlay_manager *def_mgr = NULL;
426 INIT_LIST_HEAD(&overlay_list);
430 for (i = 0; i < 3; ++i) {
431 struct omap_overlay *ovl;
432 ovl = kzalloc(sizeof(*ovl), GFP_KERNEL);
439 ovl->id = OMAP_DSS_GFX;
440 ovl->supported_modes = OMAP_DSS_COLOR_GFX_OMAP3;
441 ovl->caps = OMAP_DSS_OVL_CAP_DISPC;
445 ovl->id = OMAP_DSS_VIDEO1;
446 ovl->supported_modes = OMAP_DSS_COLOR_VID_OMAP3;
447 ovl->caps = OMAP_DSS_OVL_CAP_SCALE |
448 OMAP_DSS_OVL_CAP_DISPC;
452 ovl->id = OMAP_DSS_VIDEO2;
453 ovl->supported_modes = OMAP_DSS_COLOR_VID_OMAP3;
454 ovl->caps = OMAP_DSS_OVL_CAP_SCALE |
455 OMAP_DSS_OVL_CAP_DISPC;
459 ovl->set_manager = &omap_dss_set_manager;
460 ovl->unset_manager = &omap_dss_unset_manager;
461 ovl->set_overlay_info = &dss_ovl_set_overlay_info;
462 ovl->get_overlay_info = &dss_ovl_get_overlay_info;
464 omap_dss_add_overlay(ovl);
466 r = kobject_init_and_add(&ovl->kobj, &overlay_ktype,
467 &pdev->dev.kobj, "overlay%d", i);
470 DSSERR("failed to create sysfs file\n");
474 dispc_overlays[i] = ovl;
477 lcd_mgr = omap_dss_get_overlay_manager(OMAP_DSS_OVL_MGR_LCD);
478 tv_mgr = omap_dss_get_overlay_manager(OMAP_DSS_OVL_MGR_TV);
481 for (i = 0; i < omap_dss_get_num_displays() ; i++) {
482 struct omap_display *display = dss_get_display(i);
484 if (strcmp(display->name, def_disp_name) == 0) {
485 if (display->type != OMAP_DISPLAY_TYPE_VENC) {
486 lcd_mgr->set_display(lcd_mgr, display);
489 lcd_mgr->set_display(tv_mgr, display);
498 DSSWARN("default display %s not found\n",
502 if (def_mgr != lcd_mgr) {
503 /* connect lcd manager to first non-VENC display found */
504 for (i = 0; i < omap_dss_get_num_displays(); i++) {
505 struct omap_display *display = dss_get_display(i);
506 if (display->type != OMAP_DISPLAY_TYPE_VENC) {
507 lcd_mgr->set_display(lcd_mgr, display);
517 if (def_mgr != tv_mgr) {
518 /* connect tv manager to first VENC display found */
519 for (i = 0; i < omap_dss_get_num_displays(); i++) {
520 struct omap_display *display = dss_get_display(i);
521 if (display->type == OMAP_DISPLAY_TYPE_VENC) {
522 tv_mgr->set_display(tv_mgr, display);
532 /* connect all dispc overlays to def_mgr */
534 for (i = 0; i < 3; i++) {
535 struct omap_overlay *ovl;
536 ovl = omap_dss_get_overlay(i);
537 omap_dss_set_manager(ovl, def_mgr);
542 /* setup L4 overlay as an example */
544 static struct omap_overlay ovl = {
546 .supported_modes = OMAP_DSS_COLOR_RGB24U,
547 .set_manager = &omap_dss_set_manager,
548 .unset_manager = &omap_dss_unset_manager,
549 .setup_input = &omap_dss_setup_overlay_input,
550 .setup_output = &omap_dss_setup_overlay_output,
551 .enable = &omap_dss_enable_overlay,
554 static struct omap_overlay_manager mgr = {
558 .set_display = &omap_dss_set_display,
559 .unset_display = &omap_dss_unset_display,
560 .apply = &ovl_mgr_apply_l4,
561 .supported_displays =
562 OMAP_DISPLAY_TYPE_DBI | OMAP_DISPLAY_TYPE_DSI,
565 omap_dss_add_overlay(&ovl);
566 omap_dss_add_overlay_manager(&mgr);
567 omap_dss_set_manager(&ovl, &mgr);
572 void dss_uninit_overlays(struct platform_device *pdev)
574 struct omap_overlay *ovl;
576 while (!list_empty(&overlay_list)) {
577 ovl = list_first_entry(&overlay_list,
578 struct omap_overlay, list);
579 list_del(&ovl->list);
580 kobject_del(&ovl->kobj);
581 kobject_put(&ovl->kobj);