i2c-eg20t: change timeout value 50msec to 1000msec
[zen-stable.git] / drivers / video / omap2 / dss / overlay.c
blob6e821810deec092324a0f1013fbb3cb32bba72dd
1 /*
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
8 * by Imre Deak.
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
17 * more details.
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>
35 #include <plat/cpu.h>
37 #include "dss.h"
38 #include "dss_features.h"
40 static int num_overlays;
41 static struct omap_overlay *overlays;
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,
55 size_t size)
57 int i, r;
58 struct omap_overlay_manager *mgr = NULL;
59 struct omap_overlay_manager *old_mgr;
60 int len = size;
62 if (buf[size-1] == '\n')
63 --len;
65 if (len > 0) {
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))
70 break;
72 mgr = NULL;
76 if (len > 0 && mgr == NULL)
77 return -EINVAL;
79 if (mgr)
80 DSSDBG("manager %s found\n", mgr->name);
82 if (mgr == ovl->manager)
83 return size;
85 old_mgr = ovl->manager;
87 r = dispc_runtime_get();
88 if (r)
89 return r;
91 /* detach old manager */
92 if (old_mgr) {
93 r = ovl->unset_manager(ovl);
94 if (r) {
95 DSSERR("detach failed\n");
96 goto err;
99 r = old_mgr->apply(old_mgr);
100 if (r)
101 goto err;
104 if (mgr) {
105 r = ovl->set_manager(ovl, mgr);
106 if (r) {
107 DSSERR("Failed to attach overlay\n");
108 goto err;
111 r = mgr->apply(mgr);
112 if (r)
113 goto err;
116 dispc_runtime_put();
118 return size;
120 err:
121 dispc_runtime_put();
122 return r;
125 static ssize_t overlay_input_size_show(struct omap_overlay *ovl, char *buf)
127 struct omap_overlay_info info;
129 ovl->get_overlay_info(ovl, &info);
131 return snprintf(buf, PAGE_SIZE, "%d,%d\n",
132 info.width, info.height);
135 static ssize_t overlay_screen_width_show(struct omap_overlay *ovl, char *buf)
137 struct omap_overlay_info info;
139 ovl->get_overlay_info(ovl, &info);
141 return snprintf(buf, PAGE_SIZE, "%d\n", info.screen_width);
144 static ssize_t overlay_position_show(struct omap_overlay *ovl, char *buf)
146 struct omap_overlay_info info;
148 ovl->get_overlay_info(ovl, &info);
150 return snprintf(buf, PAGE_SIZE, "%d,%d\n",
151 info.pos_x, info.pos_y);
154 static ssize_t overlay_position_store(struct omap_overlay *ovl,
155 const char *buf, size_t size)
157 int r;
158 char *last;
159 struct omap_overlay_info info;
161 ovl->get_overlay_info(ovl, &info);
163 info.pos_x = simple_strtoul(buf, &last, 10);
164 ++last;
165 if (last - buf >= size)
166 return -EINVAL;
168 info.pos_y = simple_strtoul(last, &last, 10);
170 r = ovl->set_overlay_info(ovl, &info);
171 if (r)
172 return r;
174 if (ovl->manager) {
175 r = ovl->manager->apply(ovl->manager);
176 if (r)
177 return r;
180 return size;
183 static ssize_t overlay_output_size_show(struct omap_overlay *ovl, char *buf)
185 struct omap_overlay_info info;
187 ovl->get_overlay_info(ovl, &info);
189 return snprintf(buf, PAGE_SIZE, "%d,%d\n",
190 info.out_width, info.out_height);
193 static ssize_t overlay_output_size_store(struct omap_overlay *ovl,
194 const char *buf, size_t size)
196 int r;
197 char *last;
198 struct omap_overlay_info info;
200 ovl->get_overlay_info(ovl, &info);
202 info.out_width = simple_strtoul(buf, &last, 10);
203 ++last;
204 if (last - buf >= size)
205 return -EINVAL;
207 info.out_height = simple_strtoul(last, &last, 10);
209 r = ovl->set_overlay_info(ovl, &info);
210 if (r)
211 return r;
213 if (ovl->manager) {
214 r = ovl->manager->apply(ovl->manager);
215 if (r)
216 return r;
219 return size;
222 static ssize_t overlay_enabled_show(struct omap_overlay *ovl, char *buf)
224 return snprintf(buf, PAGE_SIZE, "%d\n", ovl->is_enabled(ovl));
227 static ssize_t overlay_enabled_store(struct omap_overlay *ovl, const char *buf,
228 size_t size)
230 int r;
231 bool enable;
233 r = strtobool(buf, &enable);
234 if (r)
235 return r;
237 if (enable)
238 r = ovl->enable(ovl);
239 else
240 r = ovl->disable(ovl);
242 if (r)
243 return r;
245 return size;
248 static ssize_t overlay_global_alpha_show(struct omap_overlay *ovl, char *buf)
250 struct omap_overlay_info info;
252 ovl->get_overlay_info(ovl, &info);
254 return snprintf(buf, PAGE_SIZE, "%d\n",
255 info.global_alpha);
258 static ssize_t overlay_global_alpha_store(struct omap_overlay *ovl,
259 const char *buf, size_t size)
261 int r;
262 u8 alpha;
263 struct omap_overlay_info info;
265 if ((ovl->caps & OMAP_DSS_OVL_CAP_GLOBAL_ALPHA) == 0)
266 return -ENODEV;
268 r = kstrtou8(buf, 0, &alpha);
269 if (r)
270 return r;
272 ovl->get_overlay_info(ovl, &info);
274 info.global_alpha = alpha;
276 r = ovl->set_overlay_info(ovl, &info);
277 if (r)
278 return r;
280 if (ovl->manager) {
281 r = ovl->manager->apply(ovl->manager);
282 if (r)
283 return r;
286 return size;
289 static ssize_t overlay_pre_mult_alpha_show(struct omap_overlay *ovl,
290 char *buf)
292 struct omap_overlay_info info;
294 ovl->get_overlay_info(ovl, &info);
296 return snprintf(buf, PAGE_SIZE, "%d\n",
297 info.pre_mult_alpha);
300 static ssize_t overlay_pre_mult_alpha_store(struct omap_overlay *ovl,
301 const char *buf, size_t size)
303 int r;
304 u8 alpha;
305 struct omap_overlay_info info;
307 if ((ovl->caps & OMAP_DSS_OVL_CAP_PRE_MULT_ALPHA) == 0)
308 return -ENODEV;
310 r = kstrtou8(buf, 0, &alpha);
311 if (r)
312 return r;
314 ovl->get_overlay_info(ovl, &info);
316 info.pre_mult_alpha = alpha;
318 r = ovl->set_overlay_info(ovl, &info);
319 if (r)
320 return r;
322 if (ovl->manager) {
323 r = ovl->manager->apply(ovl->manager);
324 if (r)
325 return r;
328 return size;
331 static ssize_t overlay_zorder_show(struct omap_overlay *ovl, char *buf)
333 struct omap_overlay_info info;
335 ovl->get_overlay_info(ovl, &info);
337 return snprintf(buf, PAGE_SIZE, "%d\n", info.zorder);
340 static ssize_t overlay_zorder_store(struct omap_overlay *ovl,
341 const char *buf, size_t size)
343 int r;
344 u8 zorder;
345 struct omap_overlay_info info;
347 if ((ovl->caps & OMAP_DSS_OVL_CAP_ZORDER) == 0)
348 return -ENODEV;
350 r = kstrtou8(buf, 0, &zorder);
351 if (r)
352 return r;
354 ovl->get_overlay_info(ovl, &info);
356 info.zorder = zorder;
358 r = ovl->set_overlay_info(ovl, &info);
359 if (r)
360 return r;
362 if (ovl->manager) {
363 r = ovl->manager->apply(ovl->manager);
364 if (r)
365 return r;
368 return size;
371 struct overlay_attribute {
372 struct attribute attr;
373 ssize_t (*show)(struct omap_overlay *, char *);
374 ssize_t (*store)(struct omap_overlay *, const char *, size_t);
377 #define OVERLAY_ATTR(_name, _mode, _show, _store) \
378 struct overlay_attribute overlay_attr_##_name = \
379 __ATTR(_name, _mode, _show, _store)
381 static OVERLAY_ATTR(name, S_IRUGO, overlay_name_show, NULL);
382 static OVERLAY_ATTR(manager, S_IRUGO|S_IWUSR,
383 overlay_manager_show, overlay_manager_store);
384 static OVERLAY_ATTR(input_size, S_IRUGO, overlay_input_size_show, NULL);
385 static OVERLAY_ATTR(screen_width, S_IRUGO, overlay_screen_width_show, NULL);
386 static OVERLAY_ATTR(position, S_IRUGO|S_IWUSR,
387 overlay_position_show, overlay_position_store);
388 static OVERLAY_ATTR(output_size, S_IRUGO|S_IWUSR,
389 overlay_output_size_show, overlay_output_size_store);
390 static OVERLAY_ATTR(enabled, S_IRUGO|S_IWUSR,
391 overlay_enabled_show, overlay_enabled_store);
392 static OVERLAY_ATTR(global_alpha, S_IRUGO|S_IWUSR,
393 overlay_global_alpha_show, overlay_global_alpha_store);
394 static OVERLAY_ATTR(pre_mult_alpha, S_IRUGO|S_IWUSR,
395 overlay_pre_mult_alpha_show,
396 overlay_pre_mult_alpha_store);
397 static OVERLAY_ATTR(zorder, S_IRUGO|S_IWUSR,
398 overlay_zorder_show, overlay_zorder_store);
400 static struct attribute *overlay_sysfs_attrs[] = {
401 &overlay_attr_name.attr,
402 &overlay_attr_manager.attr,
403 &overlay_attr_input_size.attr,
404 &overlay_attr_screen_width.attr,
405 &overlay_attr_position.attr,
406 &overlay_attr_output_size.attr,
407 &overlay_attr_enabled.attr,
408 &overlay_attr_global_alpha.attr,
409 &overlay_attr_pre_mult_alpha.attr,
410 &overlay_attr_zorder.attr,
411 NULL
414 static ssize_t overlay_attr_show(struct kobject *kobj, struct attribute *attr,
415 char *buf)
417 struct omap_overlay *overlay;
418 struct overlay_attribute *overlay_attr;
420 overlay = container_of(kobj, struct omap_overlay, kobj);
421 overlay_attr = container_of(attr, struct overlay_attribute, attr);
423 if (!overlay_attr->show)
424 return -ENOENT;
426 return overlay_attr->show(overlay, buf);
429 static ssize_t overlay_attr_store(struct kobject *kobj, struct attribute *attr,
430 const char *buf, size_t size)
432 struct omap_overlay *overlay;
433 struct overlay_attribute *overlay_attr;
435 overlay = container_of(kobj, struct omap_overlay, kobj);
436 overlay_attr = container_of(attr, struct overlay_attribute, attr);
438 if (!overlay_attr->store)
439 return -ENOENT;
441 return overlay_attr->store(overlay, buf, size);
444 static const struct sysfs_ops overlay_sysfs_ops = {
445 .show = overlay_attr_show,
446 .store = overlay_attr_store,
449 static struct kobj_type overlay_ktype = {
450 .sysfs_ops = &overlay_sysfs_ops,
451 .default_attrs = overlay_sysfs_attrs,
454 int omap_dss_get_num_overlays(void)
456 return num_overlays;
458 EXPORT_SYMBOL(omap_dss_get_num_overlays);
460 struct omap_overlay *omap_dss_get_overlay(int num)
462 if (num >= num_overlays)
463 return NULL;
465 return &overlays[num];
467 EXPORT_SYMBOL(omap_dss_get_overlay);
469 void dss_init_overlays(struct platform_device *pdev)
471 int i, r;
473 num_overlays = dss_feat_get_num_ovls();
475 overlays = kzalloc(sizeof(struct omap_overlay) * num_overlays,
476 GFP_KERNEL);
478 BUG_ON(overlays == NULL);
480 for (i = 0; i < num_overlays; ++i) {
481 struct omap_overlay *ovl = &overlays[i];
483 switch (i) {
484 case 0:
485 ovl->name = "gfx";
486 ovl->id = OMAP_DSS_GFX;
487 break;
488 case 1:
489 ovl->name = "vid1";
490 ovl->id = OMAP_DSS_VIDEO1;
491 break;
492 case 2:
493 ovl->name = "vid2";
494 ovl->id = OMAP_DSS_VIDEO2;
495 break;
496 case 3:
497 ovl->name = "vid3";
498 ovl->id = OMAP_DSS_VIDEO3;
499 break;
502 ovl->is_enabled = &dss_ovl_is_enabled;
503 ovl->enable = &dss_ovl_enable;
504 ovl->disable = &dss_ovl_disable;
505 ovl->set_manager = &dss_ovl_set_manager;
506 ovl->unset_manager = &dss_ovl_unset_manager;
507 ovl->set_overlay_info = &dss_ovl_set_info;
508 ovl->get_overlay_info = &dss_ovl_get_info;
509 ovl->wait_for_go = &dss_mgr_wait_for_go_ovl;
511 ovl->caps = dss_feat_get_overlay_caps(ovl->id);
512 ovl->supported_modes =
513 dss_feat_get_supported_color_modes(ovl->id);
515 r = kobject_init_and_add(&ovl->kobj, &overlay_ktype,
516 &pdev->dev.kobj, "overlay%d", i);
518 if (r)
519 DSSERR("failed to create sysfs file\n");
523 /* connect overlays to the new device, if not already connected. if force
524 * selected, connect always. */
525 void dss_recheck_connections(struct omap_dss_device *dssdev, bool force)
527 int i;
528 struct omap_overlay_manager *lcd_mgr;
529 struct omap_overlay_manager *tv_mgr;
530 struct omap_overlay_manager *lcd2_mgr = NULL;
531 struct omap_overlay_manager *mgr = NULL;
533 lcd_mgr = omap_dss_get_overlay_manager(OMAP_DSS_OVL_MGR_LCD);
534 tv_mgr = omap_dss_get_overlay_manager(OMAP_DSS_OVL_MGR_TV);
535 if (dss_has_feature(FEAT_MGR_LCD2))
536 lcd2_mgr = omap_dss_get_overlay_manager(OMAP_DSS_OVL_MGR_LCD2);
538 if (dssdev->channel == OMAP_DSS_CHANNEL_LCD2) {
539 if (!lcd2_mgr->device || force) {
540 if (lcd2_mgr->device)
541 lcd2_mgr->unset_device(lcd2_mgr);
542 lcd2_mgr->set_device(lcd2_mgr, dssdev);
543 mgr = lcd2_mgr;
545 } else if (dssdev->type != OMAP_DISPLAY_TYPE_VENC
546 && dssdev->type != OMAP_DISPLAY_TYPE_HDMI) {
547 if (!lcd_mgr->device || force) {
548 if (lcd_mgr->device)
549 lcd_mgr->unset_device(lcd_mgr);
550 lcd_mgr->set_device(lcd_mgr, dssdev);
551 mgr = lcd_mgr;
555 if (dssdev->type == OMAP_DISPLAY_TYPE_VENC
556 || dssdev->type == OMAP_DISPLAY_TYPE_HDMI) {
557 if (!tv_mgr->device || force) {
558 if (tv_mgr->device)
559 tv_mgr->unset_device(tv_mgr);
560 tv_mgr->set_device(tv_mgr, dssdev);
561 mgr = tv_mgr;
565 if (mgr) {
566 dispc_runtime_get();
568 for (i = 0; i < dss_feat_get_num_ovls(); i++) {
569 struct omap_overlay *ovl;
570 ovl = omap_dss_get_overlay(i);
571 if (!ovl->manager || force) {
572 if (ovl->manager)
573 ovl->unset_manager(ovl);
574 ovl->set_manager(ovl, mgr);
578 dispc_runtime_put();
582 void dss_uninit_overlays(struct platform_device *pdev)
584 int i;
586 for (i = 0; i < num_overlays; ++i) {
587 struct omap_overlay *ovl = &overlays[i];
589 kobject_del(&ovl->kobj);
590 kobject_put(&ovl->kobj);
593 kfree(overlays);
594 overlays = NULL;
595 num_overlays = 0;
598 int dss_ovl_simple_check(struct omap_overlay *ovl,
599 const struct omap_overlay_info *info)
601 if (info->paddr == 0) {
602 DSSERR("check_overlay: paddr cannot be 0\n");
603 return -EINVAL;
606 if ((ovl->caps & OMAP_DSS_OVL_CAP_SCALE) == 0) {
607 if (info->out_width != 0 && info->width != info->out_width) {
608 DSSERR("check_overlay: overlay %d doesn't support "
609 "scaling\n", ovl->id);
610 return -EINVAL;
613 if (info->out_height != 0 && info->height != info->out_height) {
614 DSSERR("check_overlay: overlay %d doesn't support "
615 "scaling\n", ovl->id);
616 return -EINVAL;
620 if ((ovl->supported_modes & info->color_mode) == 0) {
621 DSSERR("check_overlay: overlay %d doesn't support mode %d\n",
622 ovl->id, info->color_mode);
623 return -EINVAL;
626 if (info->zorder >= omap_dss_get_num_overlays()) {
627 DSSERR("check_overlay: zorder %d too high\n", info->zorder);
628 return -EINVAL;
631 return 0;
634 int dss_ovl_check(struct omap_overlay *ovl,
635 struct omap_overlay_info *info, struct omap_dss_device *dssdev)
637 u16 outw, outh;
638 u16 dw, dh;
640 if (dssdev == NULL)
641 return 0;
643 dssdev->driver->get_resolution(dssdev, &dw, &dh);
645 if ((ovl->caps & OMAP_DSS_OVL_CAP_SCALE) == 0) {
646 outw = info->width;
647 outh = info->height;
648 } else {
649 if (info->out_width == 0)
650 outw = info->width;
651 else
652 outw = info->out_width;
654 if (info->out_height == 0)
655 outh = info->height;
656 else
657 outh = info->out_height;
660 if (dw < info->pos_x + outw) {
661 DSSERR("overlay %d horizontally not inside the display area "
662 "(%d + %d >= %d)\n",
663 ovl->id, info->pos_x, outw, dw);
664 return -EINVAL;
667 if (dh < info->pos_y + outh) {
668 DSSERR("overlay %d vertically not inside the display area "
669 "(%d + %d >= %d)\n",
670 ovl->id, info->pos_y, outh, dh);
671 return -EINVAL;
674 return 0;