blob: 75642c22cac7f50724879ff3b8ef699b52aee14f [file] [log] [blame]
Tomi Valkeineneed07e02009-08-07 13:43:20 +03001/*
2 * linux/drivers/video/omap2/dss/overlay.c
3 *
4 * Copyright (C) 2009 Nokia Corporation
5 * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
6 *
7 * Some code and ideas taken from drivers/video/omap/ driver
8 * by Imre Deak.
9 *
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.
13 *
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.
18 *
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/>.
21 */
22
23#define DSS_SUBSYS_NAME "OVERLAY"
24
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Tomi Valkeineneed07e02009-08-07 13:43:20 +030033
34#include <plat/display.h>
35#include <plat/cpu.h>
36
37#include "dss.h"
Archit Tanejaa0acb552010-09-15 19:20:00 +053038#include "dss_features.h"
Tomi Valkeineneed07e02009-08-07 13:43:20 +030039
40static int num_overlays;
41static struct list_head overlay_list;
42
43static ssize_t overlay_name_show(struct omap_overlay *ovl, char *buf)
44{
45 return snprintf(buf, PAGE_SIZE, "%s\n", ovl->name);
46}
47
48static ssize_t overlay_manager_show(struct omap_overlay *ovl, char *buf)
49{
50 return snprintf(buf, PAGE_SIZE, "%s\n",
51 ovl->manager ? ovl->manager->name : "<none>");
52}
53
54static ssize_t overlay_manager_store(struct omap_overlay *ovl, const char *buf,
55 size_t size)
56{
57 int i, r;
58 struct omap_overlay_manager *mgr = NULL;
59 struct omap_overlay_manager *old_mgr;
60 int len = size;
61
62 if (buf[size-1] == '\n')
63 --len;
64
65 if (len > 0) {
66 for (i = 0; i < omap_dss_get_num_overlay_managers(); ++i) {
67 mgr = omap_dss_get_overlay_manager(i);
68
Archit Tanejaf3c77d62010-08-02 15:14:11 +020069 if (sysfs_streq(buf, mgr->name))
Tomi Valkeineneed07e02009-08-07 13:43:20 +030070 break;
71
72 mgr = NULL;
73 }
74 }
75
76 if (len > 0 && mgr == NULL)
77 return -EINVAL;
78
79 if (mgr)
80 DSSDBG("manager %s found\n", mgr->name);
81
82 if (mgr == ovl->manager)
83 return size;
84
85 old_mgr = ovl->manager;
86
87 /* detach old manager */
88 if (old_mgr) {
89 r = ovl->unset_manager(ovl);
90 if (r) {
91 DSSERR("detach failed\n");
92 return r;
93 }
94
95 r = old_mgr->apply(old_mgr);
96 if (r)
97 return r;
98 }
99
100 if (mgr) {
101 r = ovl->set_manager(ovl, mgr);
102 if (r) {
103 DSSERR("Failed to attach overlay\n");
104 return r;
105 }
106
107 r = mgr->apply(mgr);
108 if (r)
109 return r;
110 }
111
112 return size;
113}
114
115static ssize_t overlay_input_size_show(struct omap_overlay *ovl, char *buf)
116{
117 return snprintf(buf, PAGE_SIZE, "%d,%d\n",
118 ovl->info.width, ovl->info.height);
119}
120
121static ssize_t overlay_screen_width_show(struct omap_overlay *ovl, char *buf)
122{
123 return snprintf(buf, PAGE_SIZE, "%d\n", ovl->info.screen_width);
124}
125
126static ssize_t overlay_position_show(struct omap_overlay *ovl, char *buf)
127{
128 return snprintf(buf, PAGE_SIZE, "%d,%d\n",
129 ovl->info.pos_x, ovl->info.pos_y);
130}
131
132static ssize_t overlay_position_store(struct omap_overlay *ovl,
133 const char *buf, size_t size)
134{
135 int r;
136 char *last;
137 struct omap_overlay_info info;
138
139 ovl->get_overlay_info(ovl, &info);
140
141 info.pos_x = simple_strtoul(buf, &last, 10);
142 ++last;
143 if (last - buf >= size)
144 return -EINVAL;
145
146 info.pos_y = simple_strtoul(last, &last, 10);
147
148 r = ovl->set_overlay_info(ovl, &info);
149 if (r)
150 return r;
151
152 if (ovl->manager) {
153 r = ovl->manager->apply(ovl->manager);
154 if (r)
155 return r;
156 }
157
158 return size;
159}
160
161static ssize_t overlay_output_size_show(struct omap_overlay *ovl, char *buf)
162{
163 return snprintf(buf, PAGE_SIZE, "%d,%d\n",
164 ovl->info.out_width, ovl->info.out_height);
165}
166
167static ssize_t overlay_output_size_store(struct omap_overlay *ovl,
168 const char *buf, size_t size)
169{
170 int r;
171 char *last;
172 struct omap_overlay_info info;
173
174 ovl->get_overlay_info(ovl, &info);
175
176 info.out_width = simple_strtoul(buf, &last, 10);
177 ++last;
178 if (last - buf >= size)
179 return -EINVAL;
180
181 info.out_height = simple_strtoul(last, &last, 10);
182
183 r = ovl->set_overlay_info(ovl, &info);
184 if (r)
185 return r;
186
187 if (ovl->manager) {
188 r = ovl->manager->apply(ovl->manager);
189 if (r)
190 return r;
191 }
192
193 return size;
194}
195
196static ssize_t overlay_enabled_show(struct omap_overlay *ovl, char *buf)
197{
198 return snprintf(buf, PAGE_SIZE, "%d\n", ovl->info.enabled);
199}
200
201static ssize_t overlay_enabled_store(struct omap_overlay *ovl, const char *buf,
202 size_t size)
203{
204 int r;
205 struct omap_overlay_info info;
206
207 ovl->get_overlay_info(ovl, &info);
208
209 info.enabled = simple_strtoul(buf, NULL, 10);
210
211 r = ovl->set_overlay_info(ovl, &info);
212 if (r)
213 return r;
214
215 if (ovl->manager) {
216 r = ovl->manager->apply(ovl->manager);
217 if (r)
218 return r;
219 }
220
221 return size;
222}
223
224static ssize_t overlay_global_alpha_show(struct omap_overlay *ovl, char *buf)
225{
226 return snprintf(buf, PAGE_SIZE, "%d\n",
227 ovl->info.global_alpha);
228}
229
230static ssize_t overlay_global_alpha_store(struct omap_overlay *ovl,
231 const char *buf, size_t size)
232{
233 int r;
234 struct omap_overlay_info info;
235
236 ovl->get_overlay_info(ovl, &info);
237
238 /* Video1 plane does not support global alpha
239 * to always make it 255 completely opaque
240 */
Archit Tanejaa0acb552010-09-15 19:20:00 +0530241 if (!dss_has_feature(FEAT_GLOBAL_ALPHA_VID1) &&
242 ovl->id == OMAP_DSS_VIDEO1)
Tomi Valkeineneed07e02009-08-07 13:43:20 +0300243 info.global_alpha = 255;
244 else
245 info.global_alpha = simple_strtoul(buf, NULL, 10);
246
247 r = ovl->set_overlay_info(ovl, &info);
248 if (r)
249 return r;
250
251 if (ovl->manager) {
252 r = ovl->manager->apply(ovl->manager);
253 if (r)
254 return r;
255 }
256
257 return size;
258}
259
260struct overlay_attribute {
261 struct attribute attr;
262 ssize_t (*show)(struct omap_overlay *, char *);
263 ssize_t (*store)(struct omap_overlay *, const char *, size_t);
264};
265
266#define OVERLAY_ATTR(_name, _mode, _show, _store) \
267 struct overlay_attribute overlay_attr_##_name = \
268 __ATTR(_name, _mode, _show, _store)
269
270static OVERLAY_ATTR(name, S_IRUGO, overlay_name_show, NULL);
271static OVERLAY_ATTR(manager, S_IRUGO|S_IWUSR,
272 overlay_manager_show, overlay_manager_store);
273static OVERLAY_ATTR(input_size, S_IRUGO, overlay_input_size_show, NULL);
274static OVERLAY_ATTR(screen_width, S_IRUGO, overlay_screen_width_show, NULL);
275static OVERLAY_ATTR(position, S_IRUGO|S_IWUSR,
276 overlay_position_show, overlay_position_store);
277static OVERLAY_ATTR(output_size, S_IRUGO|S_IWUSR,
278 overlay_output_size_show, overlay_output_size_store);
279static OVERLAY_ATTR(enabled, S_IRUGO|S_IWUSR,
280 overlay_enabled_show, overlay_enabled_store);
281static OVERLAY_ATTR(global_alpha, S_IRUGO|S_IWUSR,
282 overlay_global_alpha_show, overlay_global_alpha_store);
283
284static struct attribute *overlay_sysfs_attrs[] = {
285 &overlay_attr_name.attr,
286 &overlay_attr_manager.attr,
287 &overlay_attr_input_size.attr,
288 &overlay_attr_screen_width.attr,
289 &overlay_attr_position.attr,
290 &overlay_attr_output_size.attr,
291 &overlay_attr_enabled.attr,
292 &overlay_attr_global_alpha.attr,
293 NULL
294};
295
296static ssize_t overlay_attr_show(struct kobject *kobj, struct attribute *attr,
297 char *buf)
298{
299 struct omap_overlay *overlay;
300 struct overlay_attribute *overlay_attr;
301
302 overlay = container_of(kobj, struct omap_overlay, kobj);
303 overlay_attr = container_of(attr, struct overlay_attribute, attr);
304
305 if (!overlay_attr->show)
306 return -ENOENT;
307
308 return overlay_attr->show(overlay, buf);
309}
310
311static ssize_t overlay_attr_store(struct kobject *kobj, struct attribute *attr,
312 const char *buf, size_t size)
313{
314 struct omap_overlay *overlay;
315 struct overlay_attribute *overlay_attr;
316
317 overlay = container_of(kobj, struct omap_overlay, kobj);
318 overlay_attr = container_of(attr, struct overlay_attribute, attr);
319
320 if (!overlay_attr->store)
321 return -ENOENT;
322
323 return overlay_attr->store(overlay, buf, size);
324}
325
Emese Revfy52cf25d2010-01-19 02:58:23 +0100326static const struct sysfs_ops overlay_sysfs_ops = {
Tomi Valkeineneed07e02009-08-07 13:43:20 +0300327 .show = overlay_attr_show,
328 .store = overlay_attr_store,
329};
330
331static struct kobj_type overlay_ktype = {
332 .sysfs_ops = &overlay_sysfs_ops,
333 .default_attrs = overlay_sysfs_attrs,
334};
335
336/* Check if overlay parameters are compatible with display */
337int dss_check_overlay(struct omap_overlay *ovl, struct omap_dss_device *dssdev)
338{
339 struct omap_overlay_info *info;
340 u16 outw, outh;
341 u16 dw, dh;
342
343 if (!dssdev)
344 return 0;
345
346 if (!ovl->info.enabled)
347 return 0;
348
349 info = &ovl->info;
350
351 if (info->paddr == 0) {
352 DSSDBG("check_overlay failed: paddr 0\n");
353 return -EINVAL;
354 }
355
Tomi Valkeinen96adcec2010-01-11 13:54:33 +0200356 dssdev->driver->get_resolution(dssdev, &dw, &dh);
Tomi Valkeineneed07e02009-08-07 13:43:20 +0300357
358 DSSDBG("check_overlay %d: (%d,%d %dx%d -> %dx%d) disp (%dx%d)\n",
359 ovl->id,
360 info->pos_x, info->pos_y,
361 info->width, info->height,
362 info->out_width, info->out_height,
363 dw, dh);
364
365 if ((ovl->caps & OMAP_DSS_OVL_CAP_SCALE) == 0) {
366 outw = info->width;
367 outh = info->height;
368 } else {
369 if (info->out_width == 0)
370 outw = info->width;
371 else
372 outw = info->out_width;
373
374 if (info->out_height == 0)
375 outh = info->height;
376 else
377 outh = info->out_height;
378 }
379
380 if (dw < info->pos_x + outw) {
381 DSSDBG("check_overlay failed 1: %d < %d + %d\n",
382 dw, info->pos_x, outw);
383 return -EINVAL;
384 }
385
386 if (dh < info->pos_y + outh) {
387 DSSDBG("check_overlay failed 2: %d < %d + %d\n",
388 dh, info->pos_y, outh);
389 return -EINVAL;
390 }
391
392 if ((ovl->supported_modes & info->color_mode) == 0) {
393 DSSERR("overlay doesn't support mode %d\n", info->color_mode);
394 return -EINVAL;
395 }
396
397 return 0;
398}
399
400static int dss_ovl_set_overlay_info(struct omap_overlay *ovl,
401 struct omap_overlay_info *info)
402{
403 int r;
404 struct omap_overlay_info old_info;
405
406 old_info = ovl->info;
407 ovl->info = *info;
408
409 if (ovl->manager) {
410 r = dss_check_overlay(ovl, ovl->manager->device);
411 if (r) {
412 ovl->info = old_info;
413 return r;
414 }
415 }
416
417 ovl->info_dirty = true;
418
419 return 0;
420}
421
422static void dss_ovl_get_overlay_info(struct omap_overlay *ovl,
423 struct omap_overlay_info *info)
424{
425 *info = ovl->info;
426}
427
428static int dss_ovl_wait_for_go(struct omap_overlay *ovl)
429{
430 return dss_mgr_wait_for_go_ovl(ovl);
431}
432
433static int omap_dss_set_manager(struct omap_overlay *ovl,
434 struct omap_overlay_manager *mgr)
435{
436 if (!mgr)
437 return -EINVAL;
438
439 if (ovl->manager) {
440 DSSERR("overlay '%s' already has a manager '%s'\n",
441 ovl->name, ovl->manager->name);
442 return -EINVAL;
443 }
444
445 if (ovl->info.enabled) {
446 DSSERR("overlay has to be disabled to change the manager\n");
447 return -EINVAL;
448 }
449
450 ovl->manager = mgr;
451
452 dss_clk_enable(DSS_CLK_ICK | DSS_CLK_FCK1);
453 /* XXX: on manual update display, in auto update mode, a bug happens
454 * here. When an overlay is first enabled on LCD, then it's disabled,
455 * and the manager is changed to TV, we sometimes get SYNC_LOST_DIGIT
456 * errors. Waiting before changing the channel_out fixes it. I'm
457 * guessing that the overlay is still somehow being used for the LCD,
458 * but I don't understand how or why. */
459 msleep(40);
460 dispc_set_channel_out(ovl->id, mgr->id);
461 dss_clk_disable(DSS_CLK_ICK | DSS_CLK_FCK1);
462
463 return 0;
464}
465
466static int omap_dss_unset_manager(struct omap_overlay *ovl)
467{
468 int r;
469
470 if (!ovl->manager) {
471 DSSERR("failed to detach overlay: manager not set\n");
472 return -EINVAL;
473 }
474
475 if (ovl->info.enabled) {
476 DSSERR("overlay has to be disabled to unset the manager\n");
477 return -EINVAL;
478 }
479
480 r = ovl->wait_for_go(ovl);
481 if (r)
482 return r;
483
484 ovl->manager = NULL;
485
486 return 0;
487}
488
489int omap_dss_get_num_overlays(void)
490{
491 return num_overlays;
492}
493EXPORT_SYMBOL(omap_dss_get_num_overlays);
494
495struct omap_overlay *omap_dss_get_overlay(int num)
496{
497 int i = 0;
498 struct omap_overlay *ovl;
499
500 list_for_each_entry(ovl, &overlay_list, list) {
501 if (i++ == num)
502 return ovl;
503 }
504
505 return NULL;
506}
507EXPORT_SYMBOL(omap_dss_get_overlay);
508
509static void omap_dss_add_overlay(struct omap_overlay *overlay)
510{
511 ++num_overlays;
512 list_add_tail(&overlay->list, &overlay_list);
513}
514
Archit Tanejaa0acb552010-09-15 19:20:00 +0530515static struct omap_overlay *dispc_overlays[MAX_DSS_OVERLAYS];
Tomi Valkeineneed07e02009-08-07 13:43:20 +0300516
517void dss_overlay_setup_dispc_manager(struct omap_overlay_manager *mgr)
518{
Archit Tanejaa0acb552010-09-15 19:20:00 +0530519 mgr->num_overlays = dss_feat_get_num_ovls();
Tomi Valkeineneed07e02009-08-07 13:43:20 +0300520 mgr->overlays = dispc_overlays;
521}
522
523#ifdef L4_EXAMPLE
524static struct omap_overlay *l4_overlays[1];
525void dss_overlay_setup_l4_manager(struct omap_overlay_manager *mgr)
526{
527 mgr->num_overlays = 1;
528 mgr->overlays = l4_overlays;
529}
530#endif
531
532void dss_init_overlays(struct platform_device *pdev)
533{
534 int i, r;
535
536 INIT_LIST_HEAD(&overlay_list);
537
538 num_overlays = 0;
539
Archit Tanejaa0acb552010-09-15 19:20:00 +0530540 for (i = 0; i < dss_feat_get_num_ovls(); ++i) {
Tomi Valkeineneed07e02009-08-07 13:43:20 +0300541 struct omap_overlay *ovl;
542 ovl = kzalloc(sizeof(*ovl), GFP_KERNEL);
543
544 BUG_ON(ovl == NULL);
545
546 switch (i) {
547 case 0:
548 ovl->name = "gfx";
549 ovl->id = OMAP_DSS_GFX;
Tomi Valkeineneed07e02009-08-07 13:43:20 +0300550 ovl->caps = OMAP_DSS_OVL_CAP_DISPC;
551 ovl->info.global_alpha = 255;
552 break;
553 case 1:
554 ovl->name = "vid1";
555 ovl->id = OMAP_DSS_VIDEO1;
Tomi Valkeineneed07e02009-08-07 13:43:20 +0300556 ovl->caps = OMAP_DSS_OVL_CAP_SCALE |
557 OMAP_DSS_OVL_CAP_DISPC;
558 ovl->info.global_alpha = 255;
559 break;
560 case 2:
561 ovl->name = "vid2";
562 ovl->id = OMAP_DSS_VIDEO2;
Tomi Valkeineneed07e02009-08-07 13:43:20 +0300563 ovl->caps = OMAP_DSS_OVL_CAP_SCALE |
564 OMAP_DSS_OVL_CAP_DISPC;
565 ovl->info.global_alpha = 255;
566 break;
567 }
568
569 ovl->set_manager = &omap_dss_set_manager;
570 ovl->unset_manager = &omap_dss_unset_manager;
571 ovl->set_overlay_info = &dss_ovl_set_overlay_info;
572 ovl->get_overlay_info = &dss_ovl_get_overlay_info;
573 ovl->wait_for_go = &dss_ovl_wait_for_go;
574
Archit Tanejaa0acb552010-09-15 19:20:00 +0530575 ovl->supported_modes =
576 dss_feat_get_supported_color_modes(ovl->id);
577
Tomi Valkeineneed07e02009-08-07 13:43:20 +0300578 omap_dss_add_overlay(ovl);
579
580 r = kobject_init_and_add(&ovl->kobj, &overlay_ktype,
581 &pdev->dev.kobj, "overlay%d", i);
582
583 if (r) {
584 DSSERR("failed to create sysfs file\n");
585 continue;
586 }
587
588 dispc_overlays[i] = ovl;
589 }
590
591#ifdef L4_EXAMPLE
592 {
593 struct omap_overlay *ovl;
594 ovl = kzalloc(sizeof(*ovl), GFP_KERNEL);
595
596 BUG_ON(ovl == NULL);
597
598 ovl->name = "l4";
599 ovl->supported_modes = OMAP_DSS_COLOR_RGB24U;
600
601 ovl->set_manager = &omap_dss_set_manager;
602 ovl->unset_manager = &omap_dss_unset_manager;
603 ovl->set_overlay_info = &dss_ovl_set_overlay_info;
604 ovl->get_overlay_info = &dss_ovl_get_overlay_info;
605
606 omap_dss_add_overlay(ovl);
607
608 r = kobject_init_and_add(&ovl->kobj, &overlay_ktype,
609 &pdev->dev.kobj, "overlayl4");
610
611 if (r)
612 DSSERR("failed to create sysfs file\n");
613
614 l4_overlays[0] = ovl;
615 }
616#endif
617}
618
619/* connect overlays to the new device, if not already connected. if force
620 * selected, connect always. */
621void dss_recheck_connections(struct omap_dss_device *dssdev, bool force)
622{
623 int i;
624 struct omap_overlay_manager *lcd_mgr;
625 struct omap_overlay_manager *tv_mgr;
626 struct omap_overlay_manager *mgr = NULL;
627
628 lcd_mgr = omap_dss_get_overlay_manager(OMAP_DSS_OVL_MGR_LCD);
629 tv_mgr = omap_dss_get_overlay_manager(OMAP_DSS_OVL_MGR_TV);
630
631 if (dssdev->type != OMAP_DISPLAY_TYPE_VENC) {
632 if (!lcd_mgr->device || force) {
633 if (lcd_mgr->device)
634 lcd_mgr->unset_device(lcd_mgr);
635 lcd_mgr->set_device(lcd_mgr, dssdev);
636 mgr = lcd_mgr;
637 }
638 }
639
640 if (dssdev->type == OMAP_DISPLAY_TYPE_VENC) {
641 if (!tv_mgr->device || force) {
642 if (tv_mgr->device)
643 tv_mgr->unset_device(tv_mgr);
644 tv_mgr->set_device(tv_mgr, dssdev);
645 mgr = tv_mgr;
646 }
647 }
648
649 if (mgr) {
Archit Tanejaa0acb552010-09-15 19:20:00 +0530650 for (i = 0; i < dss_feat_get_num_ovls(); i++) {
Tomi Valkeineneed07e02009-08-07 13:43:20 +0300651 struct omap_overlay *ovl;
652 ovl = omap_dss_get_overlay(i);
653 if (!ovl->manager || force) {
654 if (ovl->manager)
655 omap_dss_unset_manager(ovl);
656 omap_dss_set_manager(ovl, mgr);
657 }
658 }
659 }
660}
661
662void dss_uninit_overlays(struct platform_device *pdev)
663{
664 struct omap_overlay *ovl;
665
666 while (!list_empty(&overlay_list)) {
667 ovl = list_first_entry(&overlay_list,
668 struct omap_overlay, list);
669 list_del(&ovl->list);
670 kobject_del(&ovl->kobj);
671 kobject_put(&ovl->kobj);
672 kfree(ovl);
673 }
674
675 num_overlays = 0;
676}
677