Merge branch 'misc' of git://git.kernel.org/pub/scm/linux/kernel/git/mmarek/kbuild-2.6
[pandora-kernel.git] / drivers / video / omap2 / dss / overlay.c
1 /*
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>
32 #include <linux/slab.h>
33
34 #include <plat/display.h>
35 #include <plat/cpu.h>
36
37 #include "dss.h"
38 #include "dss_features.h"
39
40 static int num_overlays;
41 static struct list_head overlay_list;
42
43 static ssize_t overlay_name_show(struct omap_overlay *ovl, char *buf)
44 {
45         return snprintf(buf, PAGE_SIZE, "%s\n", ovl->name);
46 }
47
48 static 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
54 static 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
69                         if (sysfs_streq(buf, mgr->name))
70                                 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
115 static 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
121 static 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
126 static 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
132 static 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
161 static 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
167 static 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
196 static 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
201 static 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
224 static 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
230 static 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          */
241         if (!dss_has_feature(FEAT_GLOBAL_ALPHA_VID1) &&
242                         ovl->id == OMAP_DSS_VIDEO1)
243                 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
260 static ssize_t overlay_pre_mult_alpha_show(struct omap_overlay *ovl,
261                 char *buf)
262 {
263         return snprintf(buf, PAGE_SIZE, "%d\n",
264                         ovl->info.pre_mult_alpha);
265 }
266
267 static ssize_t overlay_pre_mult_alpha_store(struct omap_overlay *ovl,
268                 const char *buf, size_t size)
269 {
270         int r;
271         struct omap_overlay_info info;
272
273         ovl->get_overlay_info(ovl, &info);
274
275         /* only GFX and Video2 plane support pre alpha multiplied
276          * set zero for Video1 plane
277          */
278         if (!dss_has_feature(FEAT_GLOBAL_ALPHA_VID1) &&
279                 ovl->id == OMAP_DSS_VIDEO1)
280                 info.pre_mult_alpha = 0;
281         else
282                 info.pre_mult_alpha = simple_strtoul(buf, NULL, 10);
283
284         r = ovl->set_overlay_info(ovl, &info);
285         if (r)
286                 return r;
287
288         if (ovl->manager) {
289                 r = ovl->manager->apply(ovl->manager);
290                 if (r)
291                         return r;
292         }
293
294         return size;
295 }
296
297 struct overlay_attribute {
298         struct attribute attr;
299         ssize_t (*show)(struct omap_overlay *, char *);
300         ssize_t (*store)(struct omap_overlay *, const char *, size_t);
301 };
302
303 #define OVERLAY_ATTR(_name, _mode, _show, _store) \
304         struct overlay_attribute overlay_attr_##_name = \
305         __ATTR(_name, _mode, _show, _store)
306
307 static OVERLAY_ATTR(name, S_IRUGO, overlay_name_show, NULL);
308 static OVERLAY_ATTR(manager, S_IRUGO|S_IWUSR,
309                 overlay_manager_show, overlay_manager_store);
310 static OVERLAY_ATTR(input_size, S_IRUGO, overlay_input_size_show, NULL);
311 static OVERLAY_ATTR(screen_width, S_IRUGO, overlay_screen_width_show, NULL);
312 static OVERLAY_ATTR(position, S_IRUGO|S_IWUSR,
313                 overlay_position_show, overlay_position_store);
314 static OVERLAY_ATTR(output_size, S_IRUGO|S_IWUSR,
315                 overlay_output_size_show, overlay_output_size_store);
316 static OVERLAY_ATTR(enabled, S_IRUGO|S_IWUSR,
317                 overlay_enabled_show, overlay_enabled_store);
318 static OVERLAY_ATTR(global_alpha, S_IRUGO|S_IWUSR,
319                 overlay_global_alpha_show, overlay_global_alpha_store);
320 static OVERLAY_ATTR(pre_mult_alpha, S_IRUGO|S_IWUSR,
321                 overlay_pre_mult_alpha_show,
322                 overlay_pre_mult_alpha_store);
323
324 static struct attribute *overlay_sysfs_attrs[] = {
325         &overlay_attr_name.attr,
326         &overlay_attr_manager.attr,
327         &overlay_attr_input_size.attr,
328         &overlay_attr_screen_width.attr,
329         &overlay_attr_position.attr,
330         &overlay_attr_output_size.attr,
331         &overlay_attr_enabled.attr,
332         &overlay_attr_global_alpha.attr,
333         &overlay_attr_pre_mult_alpha.attr,
334         NULL
335 };
336
337 static ssize_t overlay_attr_show(struct kobject *kobj, struct attribute *attr,
338                 char *buf)
339 {
340         struct omap_overlay *overlay;
341         struct overlay_attribute *overlay_attr;
342
343         overlay = container_of(kobj, struct omap_overlay, kobj);
344         overlay_attr = container_of(attr, struct overlay_attribute, attr);
345
346         if (!overlay_attr->show)
347                 return -ENOENT;
348
349         return overlay_attr->show(overlay, buf);
350 }
351
352 static ssize_t overlay_attr_store(struct kobject *kobj, struct attribute *attr,
353                 const char *buf, size_t size)
354 {
355         struct omap_overlay *overlay;
356         struct overlay_attribute *overlay_attr;
357
358         overlay = container_of(kobj, struct omap_overlay, kobj);
359         overlay_attr = container_of(attr, struct overlay_attribute, attr);
360
361         if (!overlay_attr->store)
362                 return -ENOENT;
363
364         return overlay_attr->store(overlay, buf, size);
365 }
366
367 static const struct sysfs_ops overlay_sysfs_ops = {
368         .show = overlay_attr_show,
369         .store = overlay_attr_store,
370 };
371
372 static struct kobj_type overlay_ktype = {
373         .sysfs_ops = &overlay_sysfs_ops,
374         .default_attrs = overlay_sysfs_attrs,
375 };
376
377 /* Check if overlay parameters are compatible with display */
378 int dss_check_overlay(struct omap_overlay *ovl, struct omap_dss_device *dssdev)
379 {
380         struct omap_overlay_info *info;
381         u16 outw, outh;
382         u16 dw, dh;
383
384         if (!dssdev)
385                 return 0;
386
387         if (!ovl->info.enabled)
388                 return 0;
389
390         info = &ovl->info;
391
392         if (info->paddr == 0) {
393                 DSSDBG("check_overlay failed: paddr 0\n");
394                 return -EINVAL;
395         }
396
397         dssdev->driver->get_resolution(dssdev, &dw, &dh);
398
399         DSSDBG("check_overlay %d: (%d,%d %dx%d -> %dx%d) disp (%dx%d)\n",
400                         ovl->id,
401                         info->pos_x, info->pos_y,
402                         info->width, info->height,
403                         info->out_width, info->out_height,
404                         dw, dh);
405
406         if ((ovl->caps & OMAP_DSS_OVL_CAP_SCALE) == 0) {
407                 outw = info->width;
408                 outh = info->height;
409         } else {
410                 if (info->out_width == 0)
411                         outw = info->width;
412                 else
413                         outw = info->out_width;
414
415                 if (info->out_height == 0)
416                         outh = info->height;
417                 else
418                         outh = info->out_height;
419         }
420
421         if (dw < info->pos_x + outw) {
422                 DSSDBG("check_overlay failed 1: %d < %d + %d\n",
423                                 dw, info->pos_x, outw);
424                 return -EINVAL;
425         }
426
427         if (dh < info->pos_y + outh) {
428                 DSSDBG("check_overlay failed 2: %d < %d + %d\n",
429                                 dh, info->pos_y, outh);
430                 return -EINVAL;
431         }
432
433         if ((ovl->supported_modes & info->color_mode) == 0) {
434                 DSSERR("overlay doesn't support mode %d\n", info->color_mode);
435                 return -EINVAL;
436         }
437
438         return 0;
439 }
440
441 static int dss_ovl_set_overlay_info(struct omap_overlay *ovl,
442                 struct omap_overlay_info *info)
443 {
444         int r;
445         struct omap_overlay_info old_info;
446
447         old_info = ovl->info;
448         ovl->info = *info;
449
450         if (ovl->manager) {
451                 r = dss_check_overlay(ovl, ovl->manager->device);
452                 if (r) {
453                         ovl->info = old_info;
454                         return r;
455                 }
456         }
457
458         ovl->info_dirty = true;
459
460         return 0;
461 }
462
463 static void dss_ovl_get_overlay_info(struct omap_overlay *ovl,
464                 struct omap_overlay_info *info)
465 {
466         *info = ovl->info;
467 }
468
469 static int dss_ovl_wait_for_go(struct omap_overlay *ovl)
470 {
471         return dss_mgr_wait_for_go_ovl(ovl);
472 }
473
474 static int omap_dss_set_manager(struct omap_overlay *ovl,
475                 struct omap_overlay_manager *mgr)
476 {
477         if (!mgr)
478                 return -EINVAL;
479
480         if (ovl->manager) {
481                 DSSERR("overlay '%s' already has a manager '%s'\n",
482                                 ovl->name, ovl->manager->name);
483                 return -EINVAL;
484         }
485
486         if (ovl->info.enabled) {
487                 DSSERR("overlay has to be disabled to change the manager\n");
488                 return -EINVAL;
489         }
490
491         ovl->manager = mgr;
492
493         dss_clk_enable(DSS_CLK_ICK | DSS_CLK_FCK1);
494         /* XXX: on manual update display, in auto update mode, a bug happens
495          * here. When an overlay is first enabled on LCD, then it's disabled,
496          * and the manager is changed to TV, we sometimes get SYNC_LOST_DIGIT
497          * errors. Waiting before changing the channel_out fixes it. I'm
498          * guessing that the overlay is still somehow being used for the LCD,
499          * but I don't understand how or why. */
500         msleep(40);
501         dispc_set_channel_out(ovl->id, mgr->id);
502         dss_clk_disable(DSS_CLK_ICK | DSS_CLK_FCK1);
503
504         return 0;
505 }
506
507 static int omap_dss_unset_manager(struct omap_overlay *ovl)
508 {
509         int r;
510
511         if (!ovl->manager) {
512                 DSSERR("failed to detach overlay: manager not set\n");
513                 return -EINVAL;
514         }
515
516         if (ovl->info.enabled) {
517                 DSSERR("overlay has to be disabled to unset the manager\n");
518                 return -EINVAL;
519         }
520
521         r = ovl->wait_for_go(ovl);
522         if (r)
523                 return r;
524
525         ovl->manager = NULL;
526
527         return 0;
528 }
529
530 int omap_dss_get_num_overlays(void)
531 {
532         return num_overlays;
533 }
534 EXPORT_SYMBOL(omap_dss_get_num_overlays);
535
536 struct omap_overlay *omap_dss_get_overlay(int num)
537 {
538         int i = 0;
539         struct omap_overlay *ovl;
540
541         list_for_each_entry(ovl, &overlay_list, list) {
542                 if (i++ == num)
543                         return ovl;
544         }
545
546         return NULL;
547 }
548 EXPORT_SYMBOL(omap_dss_get_overlay);
549
550 static void omap_dss_add_overlay(struct omap_overlay *overlay)
551 {
552         ++num_overlays;
553         list_add_tail(&overlay->list, &overlay_list);
554 }
555
556 static struct omap_overlay *dispc_overlays[MAX_DSS_OVERLAYS];
557
558 void dss_overlay_setup_dispc_manager(struct omap_overlay_manager *mgr)
559 {
560         mgr->num_overlays = dss_feat_get_num_ovls();
561         mgr->overlays = dispc_overlays;
562 }
563
564 #ifdef L4_EXAMPLE
565 static struct omap_overlay *l4_overlays[1];
566 void dss_overlay_setup_l4_manager(struct omap_overlay_manager *mgr)
567 {
568         mgr->num_overlays = 1;
569         mgr->overlays = l4_overlays;
570 }
571 #endif
572
573 void dss_init_overlays(struct platform_device *pdev)
574 {
575         int i, r;
576
577         INIT_LIST_HEAD(&overlay_list);
578
579         num_overlays = 0;
580
581         for (i = 0; i < dss_feat_get_num_ovls(); ++i) {
582                 struct omap_overlay *ovl;
583                 ovl = kzalloc(sizeof(*ovl), GFP_KERNEL);
584
585                 BUG_ON(ovl == NULL);
586
587                 switch (i) {
588                 case 0:
589                         ovl->name = "gfx";
590                         ovl->id = OMAP_DSS_GFX;
591                         ovl->caps = OMAP_DSS_OVL_CAP_DISPC;
592                         ovl->info.global_alpha = 255;
593                         break;
594                 case 1:
595                         ovl->name = "vid1";
596                         ovl->id = OMAP_DSS_VIDEO1;
597                         ovl->caps = OMAP_DSS_OVL_CAP_SCALE |
598                                 OMAP_DSS_OVL_CAP_DISPC;
599                         ovl->info.global_alpha = 255;
600                         break;
601                 case 2:
602                         ovl->name = "vid2";
603                         ovl->id = OMAP_DSS_VIDEO2;
604                         ovl->caps = OMAP_DSS_OVL_CAP_SCALE |
605                                 OMAP_DSS_OVL_CAP_DISPC;
606                         ovl->info.global_alpha = 255;
607                         break;
608                 }
609
610                 ovl->set_manager = &omap_dss_set_manager;
611                 ovl->unset_manager = &omap_dss_unset_manager;
612                 ovl->set_overlay_info = &dss_ovl_set_overlay_info;
613                 ovl->get_overlay_info = &dss_ovl_get_overlay_info;
614                 ovl->wait_for_go = &dss_ovl_wait_for_go;
615
616                 ovl->supported_modes =
617                         dss_feat_get_supported_color_modes(ovl->id);
618
619                 omap_dss_add_overlay(ovl);
620
621                 r = kobject_init_and_add(&ovl->kobj, &overlay_ktype,
622                                 &pdev->dev.kobj, "overlay%d", i);
623
624                 if (r) {
625                         DSSERR("failed to create sysfs file\n");
626                         continue;
627                 }
628
629                 dispc_overlays[i] = ovl;
630         }
631
632 #ifdef L4_EXAMPLE
633         {
634                 struct omap_overlay *ovl;
635                 ovl = kzalloc(sizeof(*ovl), GFP_KERNEL);
636
637                 BUG_ON(ovl == NULL);
638
639                 ovl->name = "l4";
640                 ovl->supported_modes = OMAP_DSS_COLOR_RGB24U;
641
642                 ovl->set_manager = &omap_dss_set_manager;
643                 ovl->unset_manager = &omap_dss_unset_manager;
644                 ovl->set_overlay_info = &dss_ovl_set_overlay_info;
645                 ovl->get_overlay_info = &dss_ovl_get_overlay_info;
646
647                 omap_dss_add_overlay(ovl);
648
649                 r = kobject_init_and_add(&ovl->kobj, &overlay_ktype,
650                                 &pdev->dev.kobj, "overlayl4");
651
652                 if (r)
653                         DSSERR("failed to create sysfs file\n");
654
655                 l4_overlays[0] = ovl;
656         }
657 #endif
658 }
659
660 /* connect overlays to the new device, if not already connected. if force
661  * selected, connect always. */
662 void dss_recheck_connections(struct omap_dss_device *dssdev, bool force)
663 {
664         int i;
665         struct omap_overlay_manager *lcd_mgr;
666         struct omap_overlay_manager *tv_mgr;
667         struct omap_overlay_manager *lcd2_mgr = NULL;
668         struct omap_overlay_manager *mgr = NULL;
669
670         lcd_mgr = omap_dss_get_overlay_manager(OMAP_DSS_OVL_MGR_LCD);
671         tv_mgr = omap_dss_get_overlay_manager(OMAP_DSS_OVL_MGR_TV);
672         if (dss_has_feature(FEAT_MGR_LCD2))
673                 lcd2_mgr = omap_dss_get_overlay_manager(OMAP_DSS_OVL_MGR_LCD2);
674
675         if (dssdev->channel == OMAP_DSS_CHANNEL_LCD2) {
676                 if (!lcd2_mgr->device || force) {
677                         if (lcd2_mgr->device)
678                                 lcd2_mgr->unset_device(lcd2_mgr);
679                         lcd2_mgr->set_device(lcd2_mgr, dssdev);
680                         mgr = lcd2_mgr;
681                 }
682         } else if (dssdev->type != OMAP_DISPLAY_TYPE_VENC) {
683                 if (!lcd_mgr->device || force) {
684                         if (lcd_mgr->device)
685                                 lcd_mgr->unset_device(lcd_mgr);
686                         lcd_mgr->set_device(lcd_mgr, dssdev);
687                         mgr = lcd_mgr;
688                 }
689         }
690
691         if (dssdev->type == OMAP_DISPLAY_TYPE_VENC) {
692                 if (!tv_mgr->device || force) {
693                         if (tv_mgr->device)
694                                 tv_mgr->unset_device(tv_mgr);
695                         tv_mgr->set_device(tv_mgr, dssdev);
696                         mgr = tv_mgr;
697                 }
698         }
699
700         if (mgr) {
701                 for (i = 0; i < dss_feat_get_num_ovls(); i++) {
702                         struct omap_overlay *ovl;
703                         ovl = omap_dss_get_overlay(i);
704                         if (!ovl->manager || force) {
705                                 if (ovl->manager)
706                                         omap_dss_unset_manager(ovl);
707                                 omap_dss_set_manager(ovl, mgr);
708                         }
709                 }
710         }
711 }
712
713 void dss_uninit_overlays(struct platform_device *pdev)
714 {
715         struct omap_overlay *ovl;
716
717         while (!list_empty(&overlay_list)) {
718                 ovl = list_first_entry(&overlay_list,
719                                 struct omap_overlay, list);
720                 list_del(&ovl->list);
721                 kobject_del(&ovl->kobj);
722                 kobject_put(&ovl->kobj);
723                 kfree(ovl);
724         }
725
726         num_overlays = 0;
727 }
728