Merge branch 'stable-3.2' into pandora-3.2
[pandora-kernel.git] / drivers / video / omap2 / omapfb / omapfb-main.c
1 /*
2  * linux/drivers/video/omap2/omapfb-main.c
3  *
4  * Copyright (C) 2008 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 #include <linux/module.h>
24 #include <linux/delay.h>
25 #include <linux/slab.h>
26 #include <linux/fb.h>
27 #include <linux/dma-mapping.h>
28 #include <linux/vmalloc.h>
29 #include <linux/device.h>
30 #include <linux/platform_device.h>
31 #include <linux/omapfb.h>
32
33 #include <video/omapdss.h>
34 #include <plat/vram.h>
35 #include <plat/vrfb.h>
36
37 #include "omapfb.h"
38
39 #define MODULE_NAME     "omapfb"
40
41 #define OMAPFB_PLANE_XRES_MIN           8
42 #define OMAPFB_PLANE_YRES_MIN           8
43
44 static char *def_mode;
45 static char *def_vram;
46 static int def_vrfb;
47 static int def_rotate;
48 static int def_mirror;
49 static bool auto_update;
50 static unsigned int auto_update_freq;
51 static bool def_vram_cache = true;
52 module_param(auto_update, bool, 0);
53 module_param(auto_update_freq, uint, 0644);
54
55 #ifdef DEBUG
56 unsigned int omapfb_debug;
57 module_param_named(debug, omapfb_debug, bool, 0644);
58 static unsigned int omapfb_test_pattern;
59 module_param_named(test, omapfb_test_pattern, bool, 0644);
60 #endif
61
62 static int omapfb_fb_init(struct omapfb2_device *fbdev, struct fb_info *fbi);
63 static int omapfb_get_recommended_bpp(struct omapfb2_device *fbdev,
64                 struct omap_dss_device *dssdev);
65
66 #ifdef DEBUG
67 static void draw_pixel(struct fb_info *fbi, int x, int y, unsigned color)
68 {
69         struct fb_var_screeninfo *var = &fbi->var;
70         struct fb_fix_screeninfo *fix = &fbi->fix;
71         void __iomem *addr = fbi->screen_base;
72         const unsigned bytespp = var->bits_per_pixel >> 3;
73         const unsigned line_len = fix->line_length / bytespp;
74
75         int r = (color >> 16) & 0xff;
76         int g = (color >> 8) & 0xff;
77         int b = (color >> 0) & 0xff;
78
79         if (var->bits_per_pixel == 16) {
80                 u16 __iomem *p = (u16 __iomem *)addr;
81                 p += y * line_len + x;
82
83                 r = r * 32 / 256;
84                 g = g * 64 / 256;
85                 b = b * 32 / 256;
86
87                 __raw_writew((r << 11) | (g << 5) | (b << 0), p);
88         } else if (var->bits_per_pixel == 24) {
89                 u8 __iomem *p = (u8 __iomem *)addr;
90                 p += (y * line_len + x) * 3;
91
92                 __raw_writeb(b, p + 0);
93                 __raw_writeb(g, p + 1);
94                 __raw_writeb(r, p + 2);
95         } else if (var->bits_per_pixel == 32) {
96                 u32 __iomem *p = (u32 __iomem *)addr;
97                 p += y * line_len + x;
98                 __raw_writel(color, p);
99         }
100 }
101
102 static void fill_fb(struct fb_info *fbi)
103 {
104         struct fb_var_screeninfo *var = &fbi->var;
105         const short w = var->xres_virtual;
106         const short h = var->yres_virtual;
107         void __iomem *addr = fbi->screen_base;
108         int y, x;
109
110         if (!addr)
111                 return;
112
113         DBG("fill_fb %dx%d, line_len %d bytes\n", w, h, fbi->fix.line_length);
114
115         for (y = 0; y < h; y++) {
116                 for (x = 0; x < w; x++) {
117                         if (x < 20 && y < 20)
118                                 draw_pixel(fbi, x, y, 0xffffff);
119                         else if (x < 20 && (y > 20 && y < h - 20))
120                                 draw_pixel(fbi, x, y, 0xff);
121                         else if (y < 20 && (x > 20 && x < w - 20))
122                                 draw_pixel(fbi, x, y, 0xff00);
123                         else if (x > w - 20 && (y > 20 && y < h - 20))
124                                 draw_pixel(fbi, x, y, 0xff0000);
125                         else if (y > h - 20 && (x > 20 && x < w - 20))
126                                 draw_pixel(fbi, x, y, 0xffff00);
127                         else if (x == 20 || x == w - 20 ||
128                                         y == 20 || y == h - 20)
129                                 draw_pixel(fbi, x, y, 0xffffff);
130                         else if (x == y || w - x == h - y)
131                                 draw_pixel(fbi, x, y, 0xff00ff);
132                         else if (w - x == y || x == h - y)
133                                 draw_pixel(fbi, x, y, 0x00ffff);
134                         else if (x > 20 && y > 20 && x < w - 20 && y < h - 20) {
135                                 int t = x * 3 / w;
136                                 unsigned r = 0, g = 0, b = 0;
137                                 unsigned c;
138                                 if (var->bits_per_pixel == 16) {
139                                         if (t == 0)
140                                                 b = (y % 32) * 256 / 32;
141                                         else if (t == 1)
142                                                 g = (y % 64) * 256 / 64;
143                                         else if (t == 2)
144                                                 r = (y % 32) * 256 / 32;
145                                 } else {
146                                         if (t == 0)
147                                                 b = (y % 256);
148                                         else if (t == 1)
149                                                 g = (y % 256);
150                                         else if (t == 2)
151                                                 r = (y % 256);
152                                 }
153                                 c = (r << 16) | (g << 8) | (b << 0);
154                                 draw_pixel(fbi, x, y, c);
155                         } else {
156                                 draw_pixel(fbi, x, y, 0);
157                         }
158                 }
159         }
160 }
161 #endif
162
163 static unsigned omapfb_get_vrfb_offset(const struct omapfb_info *ofbi, int rot)
164 {
165         const struct vrfb *vrfb = &ofbi->region->vrfb;
166         unsigned offset;
167
168         switch (rot) {
169         case FB_ROTATE_UR:
170                 offset = 0;
171                 break;
172         case FB_ROTATE_CW:
173                 offset = vrfb->yoffset;
174                 break;
175         case FB_ROTATE_UD:
176                 offset = vrfb->yoffset * OMAP_VRFB_LINE_LEN + vrfb->xoffset;
177                 break;
178         case FB_ROTATE_CCW:
179                 offset = vrfb->xoffset * OMAP_VRFB_LINE_LEN;
180                 break;
181         default:
182                 BUG();
183         }
184
185         offset *= vrfb->bytespp;
186
187         return offset;
188 }
189
190 static u32 omapfb_get_region_rot_paddr(const struct omapfb_info *ofbi, int rot)
191 {
192         if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
193                 return ofbi->region->vrfb.paddr[rot]
194                         + omapfb_get_vrfb_offset(ofbi, rot);
195         } else {
196                 return ofbi->region->paddr;
197         }
198 }
199
200 static u32 omapfb_get_region_paddr(const struct omapfb_info *ofbi)
201 {
202         if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
203                 return ofbi->region->vrfb.paddr[0];
204         else
205                 return ofbi->region->paddr;
206 }
207
208 static void __iomem *omapfb_get_region_vaddr(const struct omapfb_info *ofbi)
209 {
210         if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
211                 return ofbi->region->vrfb.vaddr[0];
212         else
213                 return ofbi->region->vaddr;
214 }
215
216 static struct omapfb_colormode omapfb_colormodes[] = {
217         {
218                 .dssmode = OMAP_DSS_COLOR_UYVY,
219                 .bits_per_pixel = 16,
220                 .nonstd = OMAPFB_COLOR_YUV422,
221         }, {
222                 .dssmode = OMAP_DSS_COLOR_YUV2,
223                 .bits_per_pixel = 16,
224                 .nonstd = OMAPFB_COLOR_YUY422,
225         }, {
226                 .dssmode = OMAP_DSS_COLOR_ARGB16,
227                 .bits_per_pixel = 16,
228                 .red    = { .length = 4, .offset = 8, .msb_right = 0 },
229                 .green  = { .length = 4, .offset = 4, .msb_right = 0 },
230                 .blue   = { .length = 4, .offset = 0, .msb_right = 0 },
231                 .transp = { .length = 4, .offset = 12, .msb_right = 0 },
232         }, {
233                 .dssmode = OMAP_DSS_COLOR_RGB16,
234                 .bits_per_pixel = 16,
235                 .red    = { .length = 5, .offset = 11, .msb_right = 0 },
236                 .green  = { .length = 6, .offset = 5, .msb_right = 0 },
237                 .blue   = { .length = 5, .offset = 0, .msb_right = 0 },
238                 .transp = { .length = 0, .offset = 0, .msb_right = 0 },
239         }, {
240                 .dssmode = OMAP_DSS_COLOR_RGB24P,
241                 .bits_per_pixel = 24,
242                 .red    = { .length = 8, .offset = 16, .msb_right = 0 },
243                 .green  = { .length = 8, .offset = 8, .msb_right = 0 },
244                 .blue   = { .length = 8, .offset = 0, .msb_right = 0 },
245                 .transp = { .length = 0, .offset = 0, .msb_right = 0 },
246         }, {
247                 .dssmode = OMAP_DSS_COLOR_RGB24U,
248                 .bits_per_pixel = 32,
249                 .red    = { .length = 8, .offset = 16, .msb_right = 0 },
250                 .green  = { .length = 8, .offset = 8, .msb_right = 0 },
251                 .blue   = { .length = 8, .offset = 0, .msb_right = 0 },
252                 .transp = { .length = 0, .offset = 0, .msb_right = 0 },
253         }, {
254                 .dssmode = OMAP_DSS_COLOR_ARGB32,
255                 .bits_per_pixel = 32,
256                 .red    = { .length = 8, .offset = 16, .msb_right = 0 },
257                 .green  = { .length = 8, .offset = 8, .msb_right = 0 },
258                 .blue   = { .length = 8, .offset = 0, .msb_right = 0 },
259                 .transp = { .length = 8, .offset = 24, .msb_right = 0 },
260         }, {
261                 .dssmode = OMAP_DSS_COLOR_RGBA32,
262                 .bits_per_pixel = 32,
263                 .red    = { .length = 8, .offset = 24, .msb_right = 0 },
264                 .green  = { .length = 8, .offset = 16, .msb_right = 0 },
265                 .blue   = { .length = 8, .offset = 8, .msb_right = 0 },
266                 .transp = { .length = 8, .offset = 0, .msb_right = 0 },
267         }, {
268                 .dssmode = OMAP_DSS_COLOR_RGBX32,
269                 .bits_per_pixel = 32,
270                 .red    = { .length = 8, .offset = 24, .msb_right = 0 },
271                 .green  = { .length = 8, .offset = 16, .msb_right = 0 },
272                 .blue   = { .length = 8, .offset = 8, .msb_right = 0 },
273                 .transp = { .length = 0, .offset = 0, .msb_right = 0 },
274         },
275 };
276
277 static bool cmp_var_to_colormode(struct fb_var_screeninfo *var,
278                 struct omapfb_colormode *color)
279 {
280         bool cmp_component(struct fb_bitfield *f1, struct fb_bitfield *f2)
281         {
282                 return f1->length == f2->length &&
283                         f1->offset == f2->offset &&
284                         f1->msb_right == f2->msb_right;
285         }
286
287         if (var->bits_per_pixel == 0 ||
288                         var->red.length == 0 ||
289                         var->blue.length == 0 ||
290                         var->green.length == 0)
291                 return 0;
292
293         return var->bits_per_pixel == color->bits_per_pixel &&
294                 cmp_component(&var->red, &color->red) &&
295                 cmp_component(&var->green, &color->green) &&
296                 cmp_component(&var->blue, &color->blue) &&
297                 cmp_component(&var->transp, &color->transp);
298 }
299
300 static void assign_colormode_to_var(struct fb_var_screeninfo *var,
301                 struct omapfb_colormode *color)
302 {
303         var->bits_per_pixel = color->bits_per_pixel;
304         var->nonstd = color->nonstd;
305         var->red = color->red;
306         var->green = color->green;
307         var->blue = color->blue;
308         var->transp = color->transp;
309 }
310
311 static int fb_mode_to_dss_mode(struct fb_var_screeninfo *var,
312                 enum omap_color_mode *mode)
313 {
314         enum omap_color_mode dssmode;
315         int i;
316
317         /* first match with nonstd field */
318         if (var->nonstd) {
319                 for (i = 0; i < ARRAY_SIZE(omapfb_colormodes); ++i) {
320                         struct omapfb_colormode *m = &omapfb_colormodes[i];
321                         if (var->nonstd == m->nonstd) {
322                                 assign_colormode_to_var(var, m);
323                                 *mode = m->dssmode;
324                                 return 0;
325                         }
326                 }
327
328                 return -EINVAL;
329         }
330
331         /* then try exact match of bpp and colors */
332         for (i = 0; i < ARRAY_SIZE(omapfb_colormodes); ++i) {
333                 struct omapfb_colormode *m = &omapfb_colormodes[i];
334                 if (cmp_var_to_colormode(var, m)) {
335                         assign_colormode_to_var(var, m);
336                         *mode = m->dssmode;
337                         return 0;
338                 }
339         }
340
341         /* match with bpp if user has not filled color fields
342          * properly */
343         switch (var->bits_per_pixel) {
344         case 1:
345                 dssmode = OMAP_DSS_COLOR_CLUT1;
346                 break;
347         case 2:
348                 dssmode = OMAP_DSS_COLOR_CLUT2;
349                 break;
350         case 4:
351                 dssmode = OMAP_DSS_COLOR_CLUT4;
352                 break;
353         case 8:
354                 dssmode = OMAP_DSS_COLOR_CLUT8;
355                 break;
356         case 12:
357                 dssmode = OMAP_DSS_COLOR_RGB12U;
358                 break;
359         case 16:
360                 dssmode = OMAP_DSS_COLOR_RGB16;
361                 break;
362         case 24:
363                 dssmode = OMAP_DSS_COLOR_RGB24P;
364                 break;
365         case 32:
366                 dssmode = OMAP_DSS_COLOR_RGB24U;
367                 break;
368         default:
369                 return -EINVAL;
370         }
371
372         for (i = 0; i < ARRAY_SIZE(omapfb_colormodes); ++i) {
373                 struct omapfb_colormode *m = &omapfb_colormodes[i];
374                 if (dssmode == m->dssmode) {
375                         assign_colormode_to_var(var, m);
376                         *mode = m->dssmode;
377                         return 0;
378                 }
379         }
380
381         return -EINVAL;
382 }
383
384 static int check_fb_res_bounds(struct fb_var_screeninfo *var)
385 {
386         int xres_min = OMAPFB_PLANE_XRES_MIN;
387         int xres_max = 2048;
388         int yres_min = OMAPFB_PLANE_YRES_MIN;
389         int yres_max = 2048;
390
391         /* XXX: some applications seem to set virtual res to 0. */
392         if (var->xres_virtual == 0)
393                 var->xres_virtual = var->xres;
394
395         if (var->yres_virtual == 0)
396                 var->yres_virtual = var->yres;
397
398         if (var->xres_virtual < xres_min || var->yres_virtual < yres_min)
399                 return -EINVAL;
400
401         if (var->xres < xres_min)
402                 var->xres = xres_min;
403         if (var->yres < yres_min)
404                 var->yres = yres_min;
405         if (var->xres > xres_max)
406                 var->xres = xres_max;
407         if (var->yres > yres_max)
408                 var->yres = yres_max;
409
410         if (var->xres > var->xres_virtual)
411                 var->xres = var->xres_virtual;
412         if (var->yres > var->yres_virtual)
413                 var->yres = var->yres_virtual;
414
415         return 0;
416 }
417
418 static void shrink_height(unsigned long max_frame_size,
419                 struct fb_var_screeninfo *var)
420 {
421         DBG("can't fit FB into memory, reducing y\n");
422         var->yres_virtual = max_frame_size /
423                 (var->xres_virtual * var->bits_per_pixel >> 3);
424
425         if (var->yres_virtual < OMAPFB_PLANE_YRES_MIN)
426                 var->yres_virtual = OMAPFB_PLANE_YRES_MIN;
427
428         if (var->yres > var->yres_virtual)
429                 var->yres = var->yres_virtual;
430 }
431
432 static void shrink_width(unsigned long max_frame_size,
433                 struct fb_var_screeninfo *var)
434 {
435         DBG("can't fit FB into memory, reducing x\n");
436         var->xres_virtual = max_frame_size / var->yres_virtual /
437                 (var->bits_per_pixel >> 3);
438
439         if (var->xres_virtual < OMAPFB_PLANE_XRES_MIN)
440                 var->xres_virtual = OMAPFB_PLANE_XRES_MIN;
441
442         if (var->xres > var->xres_virtual)
443                 var->xres = var->xres_virtual;
444 }
445
446 static int check_vrfb_fb_size(unsigned long region_size,
447                 const struct fb_var_screeninfo *var)
448 {
449         unsigned long min_phys_size = omap_vrfb_min_phys_size(var->xres_virtual,
450                 var->yres_virtual, var->bits_per_pixel >> 3);
451
452         return min_phys_size > region_size ? -EINVAL : 0;
453 }
454
455 static int check_fb_size(const struct omapfb_info *ofbi,
456                 struct fb_var_screeninfo *var)
457 {
458         unsigned long max_frame_size = ofbi->region->size;
459         int bytespp = var->bits_per_pixel >> 3;
460         unsigned long line_size = var->xres_virtual * bytespp;
461
462         if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
463                 /* One needs to check for both VRFB and OMAPFB limitations. */
464                 if (check_vrfb_fb_size(max_frame_size, var))
465                         shrink_height(omap_vrfb_max_height(
466                                 max_frame_size, var->xres_virtual, bytespp) *
467                                 line_size, var);
468
469                 if (check_vrfb_fb_size(max_frame_size, var)) {
470                         DBG("cannot fit FB to memory\n");
471                         return -EINVAL;
472                 }
473
474                 return 0;
475         }
476
477         DBG("max frame size %lu, line size %lu\n", max_frame_size, line_size);
478
479         if (line_size * var->yres_virtual > max_frame_size)
480                 shrink_height(max_frame_size, var);
481
482         if (line_size * var->yres_virtual > max_frame_size) {
483                 shrink_width(max_frame_size, var);
484                 line_size = var->xres_virtual * bytespp;
485         }
486
487         if (line_size * var->yres_virtual > max_frame_size) {
488                 DBG("cannot fit FB to memory\n");
489                 return -EINVAL;
490         }
491
492         return 0;
493 }
494
495 /*
496  * Consider if VRFB assisted rotation is in use and if the virtual space for
497  * the zero degree view needs to be mapped. The need for mapping also acts as
498  * the trigger for setting up the hardware on the context in question. This
499  * ensures that one does not attempt to access the virtual view before the
500  * hardware is serving the address translations.
501  */
502 static int setup_vrfb_rotation(struct fb_info *fbi)
503 {
504         struct omapfb_info *ofbi = FB2OFB(fbi);
505         struct omapfb2_mem_region *rg = ofbi->region;
506         struct vrfb *vrfb = &rg->vrfb;
507         struct fb_var_screeninfo *var = &fbi->var;
508         struct fb_fix_screeninfo *fix = &fbi->fix;
509         unsigned bytespp;
510         bool yuv_mode;
511         enum omap_color_mode mode;
512         int r;
513         bool reconf;
514
515         if (!rg->size || ofbi->rotation_type != OMAP_DSS_ROT_VRFB)
516                 return 0;
517
518         DBG("setup_vrfb_rotation\n");
519
520         r = fb_mode_to_dss_mode(var, &mode);
521         if (r)
522                 return r;
523
524         bytespp = var->bits_per_pixel >> 3;
525
526         yuv_mode = mode == OMAP_DSS_COLOR_YUV2 || mode == OMAP_DSS_COLOR_UYVY;
527
528         /* We need to reconfigure VRFB if the resolution changes, if yuv mode
529          * is enabled/disabled, or if bytes per pixel changes */
530
531         /* XXX we shouldn't allow this when framebuffer is mmapped */
532
533         reconf = false;
534
535         if (yuv_mode != vrfb->yuv_mode)
536                 reconf = true;
537         else if (bytespp != vrfb->bytespp)
538                 reconf = true;
539         else if (vrfb->xres != var->xres_virtual ||
540                         vrfb->yres != var->yres_virtual)
541                 reconf = true;
542
543         if (vrfb->vaddr[0] && reconf) {
544                 fbi->screen_base = NULL;
545                 fix->smem_start = 0;
546                 fix->smem_len = 0;
547                 iounmap(vrfb->vaddr[0]);
548                 vrfb->vaddr[0] = NULL;
549                 DBG("setup_vrfb_rotation: reset fb\n");
550         }
551
552         if (vrfb->vaddr[0])
553                 return 0;
554
555         omap_vrfb_setup(&rg->vrfb, rg->paddr,
556                         var->xres_virtual,
557                         var->yres_virtual,
558                         bytespp, yuv_mode);
559
560         /* Now one can ioremap the 0 angle view */
561         r = omap_vrfb_map_angle(vrfb, var->yres_virtual, 0);
562         if (r)
563                 return r;
564
565         /* used by open/write in fbmem.c */
566         fbi->screen_base = ofbi->region->vrfb.vaddr[0];
567
568         fix->smem_start = ofbi->region->vrfb.paddr[0];
569
570         switch (var->nonstd) {
571         case OMAPFB_COLOR_YUV422:
572         case OMAPFB_COLOR_YUY422:
573                 fix->line_length =
574                         (OMAP_VRFB_LINE_LEN * var->bits_per_pixel) >> 2;
575                 break;
576         default:
577                 fix->line_length =
578                         (OMAP_VRFB_LINE_LEN * var->bits_per_pixel) >> 3;
579                 break;
580         }
581
582         fix->smem_len = var->yres_virtual * fix->line_length;
583
584         return 0;
585 }
586
587 int dss_mode_to_fb_mode(enum omap_color_mode dssmode,
588                         struct fb_var_screeninfo *var)
589 {
590         int i;
591
592         for (i = 0; i < ARRAY_SIZE(omapfb_colormodes); ++i) {
593                 struct omapfb_colormode *mode = &omapfb_colormodes[i];
594                 if (dssmode == mode->dssmode) {
595                         assign_colormode_to_var(var, mode);
596                         return 0;
597                 }
598         }
599         return -ENOENT;
600 }
601
602 void set_fb_fix(struct fb_info *fbi)
603 {
604         struct fb_fix_screeninfo *fix = &fbi->fix;
605         struct fb_var_screeninfo *var = &fbi->var;
606         struct omapfb_info *ofbi = FB2OFB(fbi);
607         struct omapfb2_mem_region *rg = ofbi->region;
608
609         DBG("set_fb_fix\n");
610
611         /* used by open/write in fbmem.c */
612         fbi->screen_base = (char __iomem *)omapfb_get_region_vaddr(ofbi);
613
614         /* used by mmap in fbmem.c */
615         if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
616                 switch (var->nonstd) {
617                 case OMAPFB_COLOR_YUV422:
618                 case OMAPFB_COLOR_YUY422:
619                         fix->line_length =
620                                 (OMAP_VRFB_LINE_LEN * var->bits_per_pixel) >> 2;
621                         break;
622                 default:
623                         fix->line_length =
624                                 (OMAP_VRFB_LINE_LEN * var->bits_per_pixel) >> 3;
625                         break;
626                 }
627
628                 fix->smem_len = var->yres_virtual * fix->line_length;
629         } else {
630                 fix->line_length =
631                         (var->xres_virtual * var->bits_per_pixel) >> 3;
632                 fix->smem_len = rg->size;
633         }
634
635         fix->smem_start = omapfb_get_region_paddr(ofbi);
636
637         fix->type = FB_TYPE_PACKED_PIXELS;
638
639         if (var->nonstd)
640                 fix->visual = FB_VISUAL_PSEUDOCOLOR;
641         else {
642                 switch (var->bits_per_pixel) {
643                 case 32:
644                 case 24:
645                 case 16:
646                 case 12:
647                         fix->visual = FB_VISUAL_TRUECOLOR;
648                         /* 12bpp is stored in 16 bits */
649                         break;
650                 case 1:
651                 case 2:
652                 case 4:
653                 case 8:
654                         fix->visual = FB_VISUAL_PSEUDOCOLOR;
655                         break;
656                 }
657         }
658
659         fix->accel = FB_ACCEL_NONE;
660
661         fix->xpanstep = 1;
662         fix->ypanstep = 1;
663 }
664
665 /* check new var and possibly modify it to be ok */
666 int check_fb_var(struct fb_info *fbi, struct fb_var_screeninfo *var)
667 {
668         struct omapfb_info *ofbi = FB2OFB(fbi);
669         struct omap_dss_device *display = fb2display(fbi);
670         enum omap_color_mode mode = 0;
671         int i;
672         int r;
673
674         DBG("check_fb_var %d\n", ofbi->id);
675
676         WARN_ON(!atomic_read(&ofbi->region->lock_count));
677
678         r = fb_mode_to_dss_mode(var, &mode);
679         if (r) {
680                 DBG("cannot convert var to omap dss mode\n");
681                 return r;
682         }
683
684         for (i = 0; i < ofbi->num_overlays; ++i) {
685                 if ((ofbi->overlays[i]->supported_modes & mode) == 0) {
686                         DBG("invalid mode\n");
687                         return -EINVAL;
688                 }
689         }
690
691         if (var->rotate > 3)
692                 return -EINVAL;
693
694         if (check_fb_res_bounds(var))
695                 return -EINVAL;
696
697         /* When no memory is allocated ignore the size check */
698         if (ofbi->region->size != 0 && check_fb_size(ofbi, var))
699                 return -EINVAL;
700
701         if (var->xres + var->xoffset > var->xres_virtual)
702                 var->xoffset = var->xres_virtual - var->xres;
703         if (var->yres + var->yoffset > var->yres_virtual)
704                 var->yoffset = var->yres_virtual - var->yres;
705
706         DBG("xres = %d, yres = %d, vxres = %d, vyres = %d\n",
707                         var->xres, var->yres,
708                         var->xres_virtual, var->yres_virtual);
709
710         if (display && display->driver->get_dimensions) {
711                 u32 w, h;
712                 display->driver->get_dimensions(display, &w, &h);
713                 var->width = DIV_ROUND_CLOSEST(w, 1000);
714                 var->height = DIV_ROUND_CLOSEST(h, 1000);
715         } else {
716                 var->height = -1;
717                 var->width = -1;
718         }
719
720         var->grayscale          = 0;
721
722         if (display && display->driver->get_timings) {
723                 struct omap_video_timings timings;
724                 display->driver->get_timings(display, &timings);
725
726                 /* pixclock in ps, the rest in pixclock */
727                 var->pixclock = timings.pixel_clock != 0 ?
728                         KHZ2PICOS(timings.pixel_clock) :
729                         0;
730                 var->left_margin = timings.hbp;
731                 var->right_margin = timings.hfp;
732                 var->upper_margin = timings.vbp;
733                 var->lower_margin = timings.vfp;
734                 var->hsync_len = timings.hsw;
735                 var->vsync_len = timings.vsw;
736         } else {
737                 var->pixclock = 0;
738                 var->left_margin = 0;
739                 var->right_margin = 0;
740                 var->upper_margin = 0;
741                 var->lower_margin = 0;
742                 var->hsync_len = 0;
743                 var->vsync_len = 0;
744         }
745
746         /* TODO: get these from panel->config */
747         var->vmode              = FB_VMODE_NONINTERLACED;
748         var->sync               = 0;
749
750         return 0;
751 }
752
753 /*
754  * ---------------------------------------------------------------------------
755  * fbdev framework callbacks
756  * ---------------------------------------------------------------------------
757  */
758 static int omapfb_open(struct fb_info *fbi, int user)
759 {
760         return 0;
761 }
762
763 static int omapfb_release(struct fb_info *fbi, int user)
764 {
765         return 0;
766 }
767
768 static unsigned calc_rotation_offset_dma(const struct fb_var_screeninfo *var,
769                 const struct fb_fix_screeninfo *fix, int rotation)
770 {
771         unsigned offset;
772
773         offset = var->yoffset * fix->line_length +
774                 var->xoffset * (var->bits_per_pixel >> 3);
775
776         return offset;
777 }
778
779 static unsigned calc_rotation_offset_vrfb(const struct fb_var_screeninfo *var,
780                 const struct fb_fix_screeninfo *fix, int rotation)
781 {
782         unsigned offset;
783
784         if (rotation == FB_ROTATE_UD)
785                 offset = (var->yres_virtual - var->yres) *
786                         fix->line_length;
787         else if (rotation == FB_ROTATE_CW)
788                 offset = (var->yres_virtual - var->yres) *
789                         (var->bits_per_pixel >> 3);
790         else
791                 offset = 0;
792
793         if (rotation == FB_ROTATE_UR)
794                 offset += var->yoffset * fix->line_length +
795                         var->xoffset * (var->bits_per_pixel >> 3);
796         else if (rotation == FB_ROTATE_UD)
797                 offset -= var->yoffset * fix->line_length +
798                         var->xoffset * (var->bits_per_pixel >> 3);
799         else if (rotation == FB_ROTATE_CW)
800                 offset -= var->xoffset * fix->line_length +
801                         var->yoffset * (var->bits_per_pixel >> 3);
802         else if (rotation == FB_ROTATE_CCW)
803                 offset += var->xoffset * fix->line_length +
804                         var->yoffset * (var->bits_per_pixel >> 3);
805
806         return offset;
807 }
808
809 static void omapfb_calc_addr(const struct omapfb_info *ofbi,
810                              const struct fb_var_screeninfo *var,
811                              const struct fb_fix_screeninfo *fix,
812                              int rotation, u32 *paddr)
813 {
814         u32 data_start_p;
815         int offset;
816
817         if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
818                 data_start_p = omapfb_get_region_rot_paddr(ofbi, rotation);
819         else
820                 data_start_p = omapfb_get_region_paddr(ofbi);
821
822         if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
823                 offset = calc_rotation_offset_vrfb(var, fix, rotation);
824         else
825                 offset = calc_rotation_offset_dma(var, fix, rotation);
826
827         data_start_p += offset;
828
829         if (offset)
830                 DBG("offset %d, %d = %d\n",
831                     var->xoffset, var->yoffset, offset);
832
833         DBG("paddr %x\n", data_start_p);
834
835         *paddr = data_start_p;
836 }
837
838 /* setup overlay according to the fb */
839 int omapfb_setup_overlay(struct fb_info *fbi, struct omap_overlay *ovl,
840                 u16 posx, u16 posy, u16 outw, u16 outh)
841 {
842         int r = 0;
843         struct omapfb_info *ofbi = FB2OFB(fbi);
844         struct fb_var_screeninfo *var = &fbi->var;
845         struct fb_fix_screeninfo *fix = &fbi->fix;
846         enum omap_color_mode mode = 0;
847         u32 data_start_p = 0;
848         struct omap_overlay_info info;
849         int xres, yres;
850         int screen_width;
851         int mirror;
852         int rotation = var->rotate;
853         int i;
854
855         WARN_ON(!atomic_read(&ofbi->region->lock_count));
856
857         for (i = 0; i < ofbi->num_overlays; i++) {
858                 if (ovl != ofbi->overlays[i])
859                         continue;
860
861                 rotation = (rotation + ofbi->rotation[i]) % 4;
862                 break;
863         }
864
865         DBG("setup_overlay %d, posx %d, posy %d, outw %d, outh %d\n", ofbi->id,
866                         posx, posy, outw, outh);
867
868         if (rotation == FB_ROTATE_CW || rotation == FB_ROTATE_CCW) {
869                 xres = var->yres;
870                 yres = var->xres;
871         } else {
872                 xres = var->xres;
873                 yres = var->yres;
874         }
875
876         if (ofbi->region->size)
877                 omapfb_calc_addr(ofbi, var, fix, rotation, &data_start_p);
878
879         r = fb_mode_to_dss_mode(var, &mode);
880         if (r) {
881                 DBG("fb_mode_to_dss_mode failed");
882                 goto err;
883         }
884
885         switch (var->nonstd) {
886         case OMAPFB_COLOR_YUV422:
887         case OMAPFB_COLOR_YUY422:
888                 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
889                         screen_width = fix->line_length
890                                 / (var->bits_per_pixel >> 2);
891                         break;
892                 }
893         default:
894                 screen_width = fix->line_length / (var->bits_per_pixel >> 3);
895                 break;
896         }
897
898         ovl->get_overlay_info(ovl, &info);
899
900         if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
901                 mirror = 0;
902         else
903                 mirror = ofbi->mirror;
904
905         info.paddr = data_start_p;
906         info.screen_width = screen_width;
907         info.width = xres;
908         info.height = yres;
909         info.color_mode = mode;
910         info.rotation_type = ofbi->rotation_type;
911         info.rotation = rotation;
912         info.mirror = mirror;
913
914         info.pos_x = posx;
915         info.pos_y = posy;
916         info.out_width = outw;
917         info.out_height = outh;
918
919         r = ovl->set_overlay_info(ovl, &info);
920         if (r) {
921                 DBG("ovl->setup_overlay_info failed\n");
922                 goto err;
923         }
924
925         return 0;
926
927 err:
928         DBG("setup_overlay failed\n");
929         return r;
930 }
931
932 /* apply var to the overlay */
933 int omapfb_apply_changes(struct fb_info *fbi, int init)
934 {
935         int r = 0;
936         struct omapfb_info *ofbi = FB2OFB(fbi);
937         struct fb_var_screeninfo *var = &fbi->var;
938         struct omap_overlay *ovl;
939         u16 posx, posy;
940         u16 outw, outh;
941         int i;
942
943 #ifdef DEBUG
944         if (omapfb_test_pattern)
945                 fill_fb(fbi);
946 #endif
947
948         WARN_ON(!atomic_read(&ofbi->region->lock_count));
949
950         for (i = 0; i < ofbi->num_overlays; i++) {
951                 ovl = ofbi->overlays[i];
952
953                 DBG("apply_changes, fb %d, ovl %d\n", ofbi->id, ovl->id);
954
955                 if (ofbi->region->size == 0) {
956                         /* the fb is not available. disable the overlay */
957                         omapfb_overlay_enable(ovl, 0);
958                         if (!init && ovl->manager)
959                                 ovl->manager->apply(ovl->manager);
960                         continue;
961                 }
962
963                 if (init || (ovl->caps & OMAP_DSS_OVL_CAP_SCALE) == 0) {
964                         int rotation = (var->rotate + ofbi->rotation[i]) % 4;
965                         if (rotation == FB_ROTATE_CW ||
966                                         rotation == FB_ROTATE_CCW) {
967                                 outw = var->yres;
968                                 outh = var->xres;
969                         } else {
970                                 outw = var->xres;
971                                 outh = var->yres;
972                         }
973                 } else {
974                         outw = ovl->info.out_width;
975                         outh = ovl->info.out_height;
976                 }
977
978                 if (init) {
979                         posx = 0;
980                         posy = 0;
981                 } else {
982                         posx = ovl->info.pos_x;
983                         posy = ovl->info.pos_y;
984                 }
985
986                 r = omapfb_setup_overlay(fbi, ovl, posx, posy, outw, outh);
987                 if (r)
988                         goto err;
989
990                 if (!init && ovl->manager)
991                         ovl->manager->apply(ovl->manager);
992         }
993         return 0;
994 err:
995         DBG("apply_changes failed\n");
996         return r;
997 }
998
999 /* checks var and eventually tweaks it to something supported,
1000  * DO NOT MODIFY PAR */
1001 static int omapfb_check_var(struct fb_var_screeninfo *var, struct fb_info *fbi)
1002 {
1003         struct omapfb_info *ofbi = FB2OFB(fbi);
1004         int r;
1005
1006         DBG("check_var(%d)\n", FB2OFB(fbi)->id);
1007
1008         omapfb_get_mem_region(ofbi->region);
1009
1010         r = check_fb_var(fbi, var);
1011
1012         omapfb_put_mem_region(ofbi->region);
1013
1014         return r;
1015 }
1016
1017 /* set the video mode according to info->var */
1018 static int omapfb_set_par(struct fb_info *fbi)
1019 {
1020         struct omapfb_info *ofbi = FB2OFB(fbi);
1021         int r;
1022
1023         DBG("set_par(%d)\n", FB2OFB(fbi)->id);
1024
1025         omapfb_get_mem_region(ofbi->region);
1026
1027         set_fb_fix(fbi);
1028
1029         r = setup_vrfb_rotation(fbi);
1030         if (r)
1031                 goto out;
1032
1033         r = omapfb_apply_changes(fbi, 0);
1034
1035  out:
1036         omapfb_put_mem_region(ofbi->region);
1037
1038         return r;
1039 }
1040
1041 static int omapfb_pan_display(struct fb_var_screeninfo *var,
1042                 struct fb_info *fbi)
1043 {
1044         struct omapfb_info *ofbi = FB2OFB(fbi);
1045         struct fb_var_screeninfo new_var;
1046         int r;
1047
1048         DBG("pan_display(%d)\n", FB2OFB(fbi)->id);
1049
1050         if (var->xoffset == fbi->var.xoffset &&
1051             var->yoffset == fbi->var.yoffset)
1052                 return 0;
1053
1054         new_var = fbi->var;
1055         new_var.xoffset = var->xoffset;
1056         new_var.yoffset = var->yoffset;
1057
1058         fbi->var = new_var;
1059
1060         omapfb_get_mem_region(ofbi->region);
1061
1062         r = omapfb_apply_changes(fbi, 0);
1063
1064         omapfb_put_mem_region(ofbi->region);
1065
1066         return r;
1067 }
1068
1069 static void mmap_user_open(struct vm_area_struct *vma)
1070 {
1071         struct omapfb2_mem_region *rg = vma->vm_private_data;
1072
1073         omapfb_get_mem_region(rg);
1074         atomic_inc(&rg->map_count);
1075         omapfb_put_mem_region(rg);
1076 }
1077
1078 static void mmap_user_close(struct vm_area_struct *vma)
1079 {
1080         struct omapfb2_mem_region *rg = vma->vm_private_data;
1081
1082         omapfb_get_mem_region(rg);
1083         atomic_dec(&rg->map_count);
1084         omapfb_put_mem_region(rg);
1085 }
1086
1087 static struct vm_operations_struct mmap_user_ops = {
1088         .open = mmap_user_open,
1089         .close = mmap_user_close,
1090 };
1091
1092 static int omapfb_mmap(struct fb_info *fbi, struct vm_area_struct *vma)
1093 {
1094         struct omapfb_info *ofbi = FB2OFB(fbi);
1095         struct fb_fix_screeninfo *fix = &fbi->fix;
1096         struct omapfb2_mem_region *rg;
1097         unsigned long off;
1098         unsigned long start;
1099         u32 len;
1100         int r = -EINVAL;
1101
1102         if (vma->vm_end - vma->vm_start == 0)
1103                 return 0;
1104         if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
1105                 return -EINVAL;
1106         off = vma->vm_pgoff << PAGE_SHIFT;
1107
1108         rg = omapfb_get_mem_region(ofbi->region);
1109
1110         start = omapfb_get_region_paddr(ofbi);
1111         len = fix->smem_len;
1112         if (off >= len)
1113                 goto error;
1114         if ((vma->vm_end - vma->vm_start + off) > len)
1115                 goto error;
1116
1117         off += start;
1118
1119         DBG("user mmap region start %lx, len %d, off %lx\n", start, len, off);
1120
1121         vma->vm_pgoff = off >> PAGE_SHIFT;
1122         vma->vm_flags |= VM_IO | VM_RESERVED;
1123         if (def_vram_cache)
1124                 vma->vm_page_prot = pgprot_writethrough(vma->vm_page_prot);
1125         else
1126                 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
1127         vma->vm_ops = &mmap_user_ops;
1128         vma->vm_private_data = rg;
1129         if (remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
1130                             vma->vm_end - vma->vm_start,
1131                             vma->vm_page_prot)) {
1132                 r = -EAGAIN;
1133                 goto error;
1134         }
1135
1136         /* not IO memory */
1137         vma->vm_flags &= ~VM_IO;
1138
1139         /* vm_ops.open won't be called for mmap itself. */
1140         atomic_inc(&rg->map_count);
1141
1142         omapfb_put_mem_region(rg);
1143
1144         return 0;
1145
1146  error:
1147         omapfb_put_mem_region(ofbi->region);
1148
1149         return r;
1150 }
1151
1152 /* Store a single color palette entry into a pseudo palette or the hardware
1153  * palette if one is available. For now we support only 16bpp and thus store
1154  * the entry only to the pseudo palette.
1155  */
1156 static int _setcolreg(struct fb_info *fbi, u_int regno, u_int red, u_int green,
1157                 u_int blue, u_int transp, int update_hw_pal)
1158 {
1159         /*struct omapfb_info *ofbi = FB2OFB(fbi);*/
1160         /*struct omapfb2_device *fbdev = ofbi->fbdev;*/
1161         struct fb_var_screeninfo *var = &fbi->var;
1162         int r = 0;
1163
1164         enum omapfb_color_format mode = OMAPFB_COLOR_RGB24U; /* XXX */
1165
1166         /*switch (plane->color_mode) {*/
1167         switch (mode) {
1168         case OMAPFB_COLOR_YUV422:
1169         case OMAPFB_COLOR_YUV420:
1170         case OMAPFB_COLOR_YUY422:
1171                 r = -EINVAL;
1172                 break;
1173         case OMAPFB_COLOR_CLUT_8BPP:
1174         case OMAPFB_COLOR_CLUT_4BPP:
1175         case OMAPFB_COLOR_CLUT_2BPP:
1176         case OMAPFB_COLOR_CLUT_1BPP:
1177                 /*
1178                    if (fbdev->ctrl->setcolreg)
1179                    r = fbdev->ctrl->setcolreg(regno, red, green, blue,
1180                    transp, update_hw_pal);
1181                    */
1182                 /* Fallthrough */
1183                 r = -EINVAL;
1184                 break;
1185         case OMAPFB_COLOR_RGB565:
1186         case OMAPFB_COLOR_RGB444:
1187         case OMAPFB_COLOR_RGB24P:
1188         case OMAPFB_COLOR_RGB24U:
1189                 if (r != 0)
1190                         break;
1191
1192                 if (regno < 16) {
1193                         u32 pal;
1194                         pal = ((red >> (16 - var->red.length)) <<
1195                                         var->red.offset) |
1196                                 ((green >> (16 - var->green.length)) <<
1197                                  var->green.offset) |
1198                                 (blue >> (16 - var->blue.length));
1199                         ((u32 *)(fbi->pseudo_palette))[regno] = pal;
1200                 }
1201                 break;
1202         default:
1203                 BUG();
1204         }
1205         return r;
1206 }
1207
1208 static int omapfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
1209                 u_int transp, struct fb_info *info)
1210 {
1211         DBG("setcolreg\n");
1212
1213         return _setcolreg(info, regno, red, green, blue, transp, 1);
1214 }
1215
1216 static int omapfb_setcmap(struct fb_cmap *cmap, struct fb_info *info)
1217 {
1218         int count, index, r;
1219         u16 *red, *green, *blue, *transp;
1220         u16 trans = 0xffff;
1221
1222         DBG("setcmap\n");
1223
1224         red     = cmap->red;
1225         green   = cmap->green;
1226         blue    = cmap->blue;
1227         transp  = cmap->transp;
1228         index   = cmap->start;
1229
1230         for (count = 0; count < cmap->len; count++) {
1231                 if (transp)
1232                         trans = *transp++;
1233                 r = _setcolreg(info, index++, *red++, *green++, *blue++, trans,
1234                                 count == cmap->len - 1);
1235                 if (r != 0)
1236                         return r;
1237         }
1238
1239         return 0;
1240 }
1241
1242 static int omapfb_blank(int blank, struct fb_info *fbi)
1243 {
1244         struct omapfb_info *ofbi = FB2OFB(fbi);
1245         struct omapfb2_device *fbdev = ofbi->fbdev;
1246         struct omap_dss_device *display = fb2display(fbi);
1247         struct omapfb_display_data *d;
1248         int r = 0;
1249
1250         if (!display)
1251                 return -EINVAL;
1252
1253         omapfb_lock(fbdev);
1254
1255         d = get_display_data(fbdev, display);
1256
1257         switch (blank) {
1258         case FB_BLANK_UNBLANK:
1259                 if (display->state != OMAP_DSS_DISPLAY_SUSPENDED)
1260                         goto exit;
1261
1262                 if (display->driver->resume)
1263                         r = display->driver->resume(display);
1264
1265                 if ((display->caps & OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE) &&
1266                                 d->update_mode == OMAPFB_AUTO_UPDATE &&
1267                                 !d->auto_update_work_enabled)
1268                         omapfb_start_auto_update(fbdev, display);
1269
1270                 break;
1271
1272         case FB_BLANK_NORMAL:
1273                 /* FB_BLANK_NORMAL could be implemented.
1274                  * Needs DSS additions. */
1275         case FB_BLANK_VSYNC_SUSPEND:
1276         case FB_BLANK_HSYNC_SUSPEND:
1277         case FB_BLANK_POWERDOWN:
1278                 if (display->state != OMAP_DSS_DISPLAY_ACTIVE)
1279                         goto exit;
1280
1281                 if (d->auto_update_work_enabled)
1282                         omapfb_stop_auto_update(fbdev, display);
1283
1284                 if (display->driver->suspend)
1285                         r = display->driver->suspend(display);
1286
1287                 break;
1288
1289         default:
1290                 r = -EINVAL;
1291         }
1292
1293 exit:
1294         omapfb_unlock(fbdev);
1295
1296         return r;
1297 }
1298
1299 #if 0
1300 /* XXX fb_read and fb_write are needed for VRFB */
1301 ssize_t omapfb_write(struct fb_info *info, const char __user *buf,
1302                 size_t count, loff_t *ppos)
1303 {
1304         DBG("omapfb_write %d, %lu\n", count, (unsigned long)*ppos);
1305         /* XXX needed for VRFB */
1306         return count;
1307 }
1308 #endif
1309
1310 static struct fb_ops omapfb_ops = {
1311         .owner          = THIS_MODULE,
1312         .fb_open        = omapfb_open,
1313         .fb_release     = omapfb_release,
1314         .fb_fillrect    = cfb_fillrect,
1315         .fb_copyarea    = cfb_copyarea,
1316         .fb_imageblit   = cfb_imageblit,
1317         .fb_blank       = omapfb_blank,
1318         .fb_ioctl       = omapfb_ioctl,
1319         .fb_check_var   = omapfb_check_var,
1320         .fb_set_par     = omapfb_set_par,
1321         .fb_pan_display = omapfb_pan_display,
1322         .fb_mmap        = omapfb_mmap,
1323         .fb_setcolreg   = omapfb_setcolreg,
1324         .fb_setcmap     = omapfb_setcmap,
1325         /*.fb_write     = omapfb_write,*/
1326 };
1327
1328 static void omapfb_free_fbmem(struct fb_info *fbi)
1329 {
1330         struct omapfb_info *ofbi = FB2OFB(fbi);
1331         struct omapfb2_device *fbdev = ofbi->fbdev;
1332         struct omapfb2_mem_region *rg;
1333
1334         rg = ofbi->region;
1335
1336         if (rg->token == NULL)
1337                 return;
1338
1339         WARN_ON(atomic_read(&rg->map_count));
1340
1341         if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
1342                 /* unmap the 0 angle rotation */
1343                 if (rg->vrfb.vaddr[0]) {
1344                         iounmap(rg->vrfb.vaddr[0]);
1345                         rg->vrfb.vaddr[0] = NULL;
1346                 }
1347
1348                 omap_vrfb_release_ctx(&rg->vrfb);
1349         }
1350
1351         dma_free_attrs(fbdev->dev, rg->size, rg->token, rg->dma_handle,
1352                         &rg->attrs);
1353
1354         rg->token = NULL;
1355         rg->vaddr = NULL;
1356         rg->paddr = 0;
1357         rg->alloc = 0;
1358         rg->size = 0;
1359 }
1360
1361 static void clear_fb_info(struct fb_info *fbi)
1362 {
1363         memset(&fbi->var, 0, sizeof(fbi->var));
1364         memset(&fbi->fix, 0, sizeof(fbi->fix));
1365         strlcpy(fbi->fix.id, MODULE_NAME, sizeof(fbi->fix.id));
1366 }
1367
1368 static int omapfb_free_all_fbmem(struct omapfb2_device *fbdev)
1369 {
1370         int i;
1371
1372         DBG("free all fbmem\n");
1373
1374         for (i = 0; i < fbdev->num_fbs; i++) {
1375                 struct fb_info *fbi = fbdev->fbs[i];
1376                 omapfb_free_fbmem(fbi);
1377                 clear_fb_info(fbi);
1378         }
1379
1380         return 0;
1381 }
1382
1383 static int omapfb_alloc_fbmem(struct fb_info *fbi, unsigned long size,
1384                 unsigned long paddr)
1385 {
1386         struct omapfb_info *ofbi = FB2OFB(fbi);
1387         struct omapfb2_device *fbdev = ofbi->fbdev;
1388         struct omapfb2_mem_region *rg;
1389         void *token;
1390         DEFINE_DMA_ATTRS(attrs);
1391         dma_addr_t dma_handle;
1392         int r;
1393
1394         rg = ofbi->region;
1395
1396         rg->paddr = 0;
1397         rg->vaddr = NULL;
1398         memset(&rg->vrfb, 0, sizeof rg->vrfb);
1399         rg->size = 0;
1400         rg->type = 0;
1401         rg->alloc = false;
1402         rg->map = false;
1403
1404         size = PAGE_ALIGN(size);
1405
1406         dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs);
1407
1408         if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
1409                 dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &attrs);
1410
1411         DBG("allocating %lu bytes for fb %d\n", size, ofbi->id);
1412
1413         token = dma_alloc_attrs(fbdev->dev, size, &dma_handle,
1414                         GFP_KERNEL, &attrs);
1415
1416         if (token == NULL) {
1417                 dev_err(fbdev->dev, "failed to allocate framebuffer\n");
1418                 return -ENOMEM;
1419         }
1420
1421         DBG("allocated VRAM paddr %lx, vaddr %p\n",
1422                         (unsigned long)dma_handle, token);
1423
1424         if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
1425                 r = omap_vrfb_request_ctx(&rg->vrfb);
1426                 if (r) {
1427                         dma_free_attrs(fbdev->dev, size, token, dma_handle,
1428                                         &attrs);
1429                         dev_err(fbdev->dev, "vrfb create ctx failed\n");
1430                         return r;
1431                 }
1432         }
1433
1434         rg->attrs = attrs;
1435         rg->token = token;
1436         rg->dma_handle = dma_handle;
1437
1438         rg->paddr = (unsigned long)dma_handle;
1439         rg->vaddr = (void __iomem *)token;
1440         rg->size = size;
1441         rg->alloc = 1;
1442
1443         return 0;
1444 }
1445
1446 /* allocate fbmem using display resolution as reference */
1447 static int omapfb_alloc_fbmem_display(struct fb_info *fbi, unsigned long size,
1448                 unsigned long paddr)
1449 {
1450         struct omapfb_info *ofbi = FB2OFB(fbi);
1451         struct omapfb2_device *fbdev = ofbi->fbdev;
1452         struct omap_dss_device *display;
1453         int bytespp;
1454
1455         display =  fb2display(fbi);
1456
1457         if (!display)
1458                 return 0;
1459
1460         switch (omapfb_get_recommended_bpp(fbdev, display)) {
1461         case 16:
1462                 bytespp = 2;
1463                 break;
1464         case 24:
1465                 bytespp = 4;
1466                 break;
1467         default:
1468                 bytespp = 4;
1469                 break;
1470         }
1471
1472         if (!size) {
1473                 u16 w, h;
1474
1475                 display->driver->get_resolution(display, &w, &h);
1476
1477                 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
1478                         size = max(omap_vrfb_min_phys_size(w, h, bytespp),
1479                                         omap_vrfb_min_phys_size(h, w, bytespp));
1480
1481                         DBG("adjusting fb mem size for VRFB, %u -> %lu\n",
1482                                         w * h * bytespp, size);
1483                 } else {
1484                         size = w * h * bytespp;
1485                 }
1486         }
1487
1488         if (!size)
1489                 return 0;
1490
1491         return omapfb_alloc_fbmem(fbi, size, paddr);
1492 }
1493
1494 static enum omap_color_mode fb_format_to_dss_mode(enum omapfb_color_format fmt)
1495 {
1496         enum omap_color_mode mode;
1497
1498         switch (fmt) {
1499         case OMAPFB_COLOR_RGB565:
1500                 mode = OMAP_DSS_COLOR_RGB16;
1501                 break;
1502         case OMAPFB_COLOR_YUV422:
1503                 mode = OMAP_DSS_COLOR_YUV2;
1504                 break;
1505         case OMAPFB_COLOR_CLUT_8BPP:
1506                 mode = OMAP_DSS_COLOR_CLUT8;
1507                 break;
1508         case OMAPFB_COLOR_CLUT_4BPP:
1509                 mode = OMAP_DSS_COLOR_CLUT4;
1510                 break;
1511         case OMAPFB_COLOR_CLUT_2BPP:
1512                 mode = OMAP_DSS_COLOR_CLUT2;
1513                 break;
1514         case OMAPFB_COLOR_CLUT_1BPP:
1515                 mode = OMAP_DSS_COLOR_CLUT1;
1516                 break;
1517         case OMAPFB_COLOR_RGB444:
1518                 mode = OMAP_DSS_COLOR_RGB12U;
1519                 break;
1520         case OMAPFB_COLOR_YUY422:
1521                 mode = OMAP_DSS_COLOR_UYVY;
1522                 break;
1523         case OMAPFB_COLOR_ARGB16:
1524                 mode = OMAP_DSS_COLOR_ARGB16;
1525                 break;
1526         case OMAPFB_COLOR_RGB24U:
1527                 mode = OMAP_DSS_COLOR_RGB24U;
1528                 break;
1529         case OMAPFB_COLOR_RGB24P:
1530                 mode = OMAP_DSS_COLOR_RGB24P;
1531                 break;
1532         case OMAPFB_COLOR_ARGB32:
1533                 mode = OMAP_DSS_COLOR_ARGB32;
1534                 break;
1535         case OMAPFB_COLOR_RGBA32:
1536                 mode = OMAP_DSS_COLOR_RGBA32;
1537                 break;
1538         case OMAPFB_COLOR_RGBX32:
1539                 mode = OMAP_DSS_COLOR_RGBX32;
1540                 break;
1541         default:
1542                 mode = -EINVAL;
1543         }
1544
1545         return mode;
1546 }
1547
1548 static int omapfb_parse_vram_param(const char *param, int max_entries,
1549                 unsigned long *sizes, unsigned long *paddrs)
1550 {
1551         int fbnum;
1552         unsigned long size;
1553         unsigned long paddr = 0;
1554         char *p, *start;
1555
1556         start = (char *)param;
1557
1558         while (1) {
1559                 p = start;
1560
1561                 fbnum = simple_strtoul(p, &p, 10);
1562
1563                 if (p == param)
1564                         return -EINVAL;
1565
1566                 if (*p != ':')
1567                         return -EINVAL;
1568
1569                 if (fbnum >= max_entries)
1570                         return -EINVAL;
1571
1572                 size = memparse(p + 1, &p);
1573
1574                 if (!size)
1575                         return -EINVAL;
1576
1577                 paddr = 0;
1578
1579                 if (*p == '@') {
1580                         paddr = simple_strtoul(p + 1, &p, 16);
1581
1582                         if (!paddr)
1583                                 return -EINVAL;
1584
1585                 }
1586
1587                 WARN_ONCE(paddr,
1588                         "reserving memory at predefined address not supported\n");
1589
1590                 paddrs[fbnum] = paddr;
1591                 sizes[fbnum] = size;
1592
1593                 if (*p == 0)
1594                         break;
1595
1596                 if (*p != ',')
1597                         return -EINVAL;
1598
1599                 ++p;
1600
1601                 start = p;
1602         }
1603
1604         return 0;
1605 }
1606
1607 static int omapfb_allocate_all_fbs(struct omapfb2_device *fbdev)
1608 {
1609         int i, r;
1610         unsigned long vram_sizes[10];
1611         unsigned long vram_paddrs[10];
1612
1613         memset(&vram_sizes, 0, sizeof(vram_sizes));
1614         memset(&vram_paddrs, 0, sizeof(vram_paddrs));
1615
1616         if (def_vram && omapfb_parse_vram_param(def_vram, 10,
1617                                 vram_sizes, vram_paddrs)) {
1618                 dev_err(fbdev->dev, "failed to parse vram parameter\n");
1619
1620                 memset(&vram_sizes, 0, sizeof(vram_sizes));
1621                 memset(&vram_paddrs, 0, sizeof(vram_paddrs));
1622         }
1623
1624         if (fbdev->dev->platform_data) {
1625                 struct omapfb_platform_data *opd;
1626                 opd = fbdev->dev->platform_data;
1627                 for (i = 0; i < opd->mem_desc.region_cnt; ++i) {
1628                         if (!vram_sizes[i]) {
1629                                 unsigned long size;
1630                                 unsigned long paddr;
1631
1632                                 size = opd->mem_desc.region[i].size;
1633                                 paddr = opd->mem_desc.region[i].paddr;
1634
1635                                 vram_sizes[i] = size;
1636                                 vram_paddrs[i] = paddr;
1637                         }
1638                 }
1639         }
1640
1641         for (i = 0; i < fbdev->num_fbs; i++) {
1642                 /* allocate memory automatically only for fb0, or if
1643                  * excplicitly defined with vram or plat data option */
1644                 if (i == 0 || vram_sizes[i] != 0) {
1645                         r = omapfb_alloc_fbmem_display(fbdev->fbs[i],
1646                                         vram_sizes[i], vram_paddrs[i]);
1647
1648                         if (r)
1649                                 return r;
1650                 }
1651         }
1652
1653         for (i = 0; i < fbdev->num_fbs; i++) {
1654                 struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[i]);
1655                 struct omapfb2_mem_region *rg;
1656                 rg = ofbi->region;
1657
1658                 DBG("region%d phys %08x virt %p size=%lu\n",
1659                                 i,
1660                                 rg->paddr,
1661                                 rg->vaddr,
1662                                 rg->size);
1663         }
1664
1665         return 0;
1666 }
1667
1668 int omapfb_realloc_fbmem(struct fb_info *fbi, unsigned long size, int type)
1669 {
1670         struct omapfb_info *ofbi = FB2OFB(fbi);
1671         struct omapfb2_device *fbdev = ofbi->fbdev;
1672         struct omap_dss_device *display = fb2display(fbi);
1673         struct omapfb2_mem_region *rg = ofbi->region;
1674         unsigned long old_size = rg->size;
1675         unsigned long old_paddr = rg->paddr;
1676         int old_type = rg->type;
1677         int r;
1678
1679         if (type > OMAPFB_MEMTYPE_MAX)
1680                 return -EINVAL;
1681
1682         size = PAGE_ALIGN(size);
1683
1684         if (old_size == size && old_type == type)
1685                 return 0;
1686
1687         if (display && display->driver->sync)
1688                         display->driver->sync(display);
1689
1690         omapfb_free_fbmem(fbi);
1691
1692         if (size == 0) {
1693                 clear_fb_info(fbi);
1694                 return 0;
1695         }
1696
1697         r = omapfb_alloc_fbmem(fbi, size, 0);
1698
1699         if (r) {
1700                 if (old_size)
1701                         omapfb_alloc_fbmem(fbi, old_size, old_paddr);
1702
1703                 if (rg->size == 0)
1704                         clear_fb_info(fbi);
1705
1706                 return r;
1707         }
1708
1709         if (old_size == size)
1710                 return 0;
1711
1712         if (old_size == 0) {
1713                 DBG("initializing fb %d\n", ofbi->id);
1714                 r = omapfb_fb_init(fbdev, fbi);
1715                 if (r) {
1716                         DBG("omapfb_fb_init failed\n");
1717                         goto err;
1718                 }
1719                 r = omapfb_apply_changes(fbi, 1);
1720                 if (r) {
1721                         DBG("omapfb_apply_changes failed\n");
1722                         goto err;
1723                 }
1724         } else {
1725                 struct fb_var_screeninfo new_var;
1726                 memcpy(&new_var, &fbi->var, sizeof(new_var));
1727                 r = check_fb_var(fbi, &new_var);
1728                 if (r)
1729                         goto err;
1730                 memcpy(&fbi->var, &new_var, sizeof(fbi->var));
1731                 set_fb_fix(fbi);
1732                 r = setup_vrfb_rotation(fbi);
1733                 if (r)
1734                         goto err;
1735         }
1736
1737         return 0;
1738 err:
1739         omapfb_free_fbmem(fbi);
1740         clear_fb_info(fbi);
1741         return r;
1742 }
1743
1744 static void omapfb_auto_update_work(struct work_struct *work)
1745 {
1746         struct omap_dss_device *dssdev;
1747         struct omap_dss_driver *dssdrv;
1748         struct omapfb_display_data *d;
1749         u16 w, h;
1750         unsigned int freq;
1751         struct omapfb2_device *fbdev;
1752
1753         d = container_of(work, struct omapfb_display_data,
1754                         auto_update_work.work);
1755
1756         dssdev = d->dssdev;
1757         dssdrv = dssdev->driver;
1758         fbdev = d->fbdev;
1759
1760         if (!dssdrv || !dssdrv->update)
1761                 return;
1762
1763         if (dssdrv->sync)
1764                 dssdrv->sync(dssdev);
1765
1766         dssdrv->get_resolution(dssdev, &w, &h);
1767         dssdrv->update(dssdev, 0, 0, w, h);
1768
1769         freq = auto_update_freq;
1770         if (freq == 0)
1771                 freq = 20;
1772         queue_delayed_work(fbdev->auto_update_wq,
1773                         &d->auto_update_work, HZ / freq);
1774 }
1775
1776 void omapfb_start_auto_update(struct omapfb2_device *fbdev,
1777                 struct omap_dss_device *display)
1778 {
1779         struct omapfb_display_data *d;
1780
1781         if (fbdev->auto_update_wq == NULL) {
1782                 struct workqueue_struct *wq;
1783
1784                 wq = create_singlethread_workqueue("omapfb_auto_update");
1785
1786                 if (wq == NULL) {
1787                         dev_err(fbdev->dev, "Failed to create workqueue for "
1788                                         "auto-update\n");
1789                         return;
1790                 }
1791
1792                 fbdev->auto_update_wq = wq;
1793         }
1794
1795         d = get_display_data(fbdev, display);
1796
1797         INIT_DELAYED_WORK(&d->auto_update_work, omapfb_auto_update_work);
1798
1799         d->auto_update_work_enabled = true;
1800
1801         omapfb_auto_update_work(&d->auto_update_work.work);
1802 }
1803
1804 void omapfb_stop_auto_update(struct omapfb2_device *fbdev,
1805                 struct omap_dss_device *display)
1806 {
1807         struct omapfb_display_data *d;
1808
1809         d = get_display_data(fbdev, display);
1810
1811         cancel_delayed_work_sync(&d->auto_update_work);
1812
1813         d->auto_update_work_enabled = false;
1814 }
1815
1816 /* initialize fb_info, var, fix to something sane based on the display */
1817 static int omapfb_fb_init(struct omapfb2_device *fbdev, struct fb_info *fbi)
1818 {
1819         struct fb_var_screeninfo *var = &fbi->var;
1820         struct omap_dss_device *display = fb2display(fbi);
1821         struct omapfb_info *ofbi = FB2OFB(fbi);
1822         int r = 0;
1823
1824         fbi->fbops = &omapfb_ops;
1825         fbi->flags = FBINFO_FLAG_DEFAULT;
1826         fbi->pseudo_palette = fbdev->pseudo_palette;
1827
1828         if (ofbi->region->size == 0) {
1829                 clear_fb_info(fbi);
1830                 return 0;
1831         }
1832
1833         var->nonstd = 0;
1834         var->bits_per_pixel = 0;
1835
1836         var->rotate = def_rotate;
1837
1838         /*
1839          * Check if there is a default color format set in the board file,
1840          * and use this format instead the default deducted from the
1841          * display bpp.
1842          */
1843         if (fbdev->dev->platform_data) {
1844                 struct omapfb_platform_data *opd;
1845                 int id = ofbi->id;
1846
1847                 opd = fbdev->dev->platform_data;
1848                 if (opd->mem_desc.region[id].format_used) {
1849                         enum omap_color_mode mode;
1850                         enum omapfb_color_format format;
1851
1852                         format = opd->mem_desc.region[id].format;
1853                         mode = fb_format_to_dss_mode(format);
1854                         if (mode < 0) {
1855                                 r = mode;
1856                                 goto err;
1857                         }
1858                         r = dss_mode_to_fb_mode(mode, var);
1859                         if (r < 0)
1860                                 goto err;
1861                 }
1862         }
1863
1864         if (display) {
1865                 u16 w, h;
1866                 int rotation = (var->rotate + ofbi->rotation[0]) % 4;
1867
1868                 display->driver->get_resolution(display, &w, &h);
1869
1870                 if (rotation == FB_ROTATE_CW ||
1871                                 rotation == FB_ROTATE_CCW) {
1872                         var->xres = h;
1873                         var->yres = w;
1874                 } else {
1875                         var->xres = w;
1876                         var->yres = h;
1877                 }
1878
1879                 var->xres_virtual = var->xres;
1880                 var->yres_virtual = var->yres;
1881
1882                 if (!var->bits_per_pixel) {
1883                         switch (omapfb_get_recommended_bpp(fbdev, display)) {
1884                         case 16:
1885                                 var->bits_per_pixel = 16;
1886                                 break;
1887                         case 24:
1888                                 var->bits_per_pixel = 32;
1889                                 break;
1890                         default:
1891                                 dev_err(fbdev->dev, "illegal display "
1892                                                 "bpp\n");
1893                                 return -EINVAL;
1894                         }
1895                 }
1896         } else {
1897                 /* if there's no display, let's just guess some basic values */
1898                 var->xres = 320;
1899                 var->yres = 240;
1900                 var->xres_virtual = var->xres;
1901                 var->yres_virtual = var->yres;
1902                 if (!var->bits_per_pixel)
1903                         var->bits_per_pixel = 16;
1904         }
1905
1906         r = check_fb_var(fbi, var);
1907         if (r)
1908                 goto err;
1909
1910         set_fb_fix(fbi);
1911         r = setup_vrfb_rotation(fbi);
1912         if (r)
1913                 goto err;
1914
1915         r = fb_alloc_cmap(&fbi->cmap, 256, 0);
1916         if (r)
1917                 dev_err(fbdev->dev, "unable to allocate color map memory\n");
1918
1919 err:
1920         return r;
1921 }
1922
1923 static void fbinfo_cleanup(struct omapfb2_device *fbdev, struct fb_info *fbi)
1924 {
1925         fb_dealloc_cmap(&fbi->cmap);
1926 }
1927
1928
1929 static void omapfb_free_resources(struct omapfb2_device *fbdev)
1930 {
1931         int i;
1932
1933         DBG("free_resources\n");
1934
1935         if (fbdev == NULL)
1936                 return;
1937
1938         for (i = 0; i < fbdev->num_fbs; i++)
1939                 unregister_framebuffer(fbdev->fbs[i]);
1940
1941         /* free the reserved fbmem */
1942         omapfb_free_all_fbmem(fbdev);
1943
1944         for (i = 0; i < fbdev->num_fbs; i++) {
1945                 fbinfo_cleanup(fbdev, fbdev->fbs[i]);
1946                 framebuffer_release(fbdev->fbs[i]);
1947         }
1948
1949         for (i = 0; i < fbdev->num_displays; i++) {
1950                 struct omap_dss_device *dssdev = fbdev->displays[i].dssdev;
1951
1952                 if (fbdev->displays[i].auto_update_work_enabled)
1953                         omapfb_stop_auto_update(fbdev, dssdev);
1954
1955                 if (dssdev->state != OMAP_DSS_DISPLAY_DISABLED)
1956                         dssdev->driver->disable(dssdev);
1957
1958                 omap_dss_put_device(dssdev);
1959         }
1960
1961         if (fbdev->auto_update_wq != NULL) {
1962                 flush_workqueue(fbdev->auto_update_wq);
1963                 destroy_workqueue(fbdev->auto_update_wq);
1964                 fbdev->auto_update_wq = NULL;
1965         }
1966
1967         dev_set_drvdata(fbdev->dev, NULL);
1968         kfree(fbdev);
1969 }
1970
1971 static int omapfb_create_framebuffers(struct omapfb2_device *fbdev)
1972 {
1973         int r, i;
1974
1975         fbdev->num_fbs = 0;
1976
1977         DBG("create %d framebuffers\n", CONFIG_FB_OMAP2_NUM_FBS);
1978
1979         /* allocate fb_infos */
1980         for (i = 0; i < CONFIG_FB_OMAP2_NUM_FBS; i++) {
1981                 struct fb_info *fbi;
1982                 struct omapfb_info *ofbi;
1983
1984                 fbi = framebuffer_alloc(sizeof(struct omapfb_info),
1985                                 fbdev->dev);
1986
1987                 if (fbi == NULL) {
1988                         dev_err(fbdev->dev,
1989                                 "unable to allocate memory for plane info\n");
1990                         return -ENOMEM;
1991                 }
1992
1993                 clear_fb_info(fbi);
1994
1995                 fbdev->fbs[i] = fbi;
1996
1997                 ofbi = FB2OFB(fbi);
1998                 ofbi->fbdev = fbdev;
1999                 ofbi->id = i;
2000
2001                 ofbi->region = &fbdev->regions[i];
2002                 ofbi->region->id = i;
2003                 init_rwsem(&ofbi->region->lock);
2004
2005                 /* assign these early, so that fb alloc can use them */
2006                 ofbi->rotation_type = def_vrfb ? OMAP_DSS_ROT_VRFB :
2007                         OMAP_DSS_ROT_DMA;
2008                 ofbi->mirror = def_mirror;
2009
2010                 fbdev->num_fbs++;
2011         }
2012
2013         DBG("fb_infos allocated\n");
2014
2015         /* assign overlays for the fbs */
2016         for (i = 0; i < min(fbdev->num_fbs, fbdev->num_overlays); i++) {
2017                 struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[i]);
2018
2019                 ofbi->overlays[0] = fbdev->overlays[i];
2020                 ofbi->num_overlays = 1;
2021         }
2022
2023         /* allocate fb memories */
2024         r = omapfb_allocate_all_fbs(fbdev);
2025         if (r) {
2026                 dev_err(fbdev->dev, "failed to allocate fbmem\n");
2027                 return r;
2028         }
2029
2030         DBG("fbmems allocated\n");
2031
2032         /* setup fb_infos */
2033         for (i = 0; i < fbdev->num_fbs; i++) {
2034                 struct fb_info *fbi = fbdev->fbs[i];
2035                 struct omapfb_info *ofbi = FB2OFB(fbi);
2036
2037                 omapfb_get_mem_region(ofbi->region);
2038                 r = omapfb_fb_init(fbdev, fbi);
2039                 omapfb_put_mem_region(ofbi->region);
2040
2041                 if (r) {
2042                         dev_err(fbdev->dev, "failed to setup fb_info\n");
2043                         return r;
2044                 }
2045         }
2046
2047         DBG("fb_infos initialized\n");
2048
2049         for (i = 0; i < fbdev->num_fbs; i++) {
2050                 r = register_framebuffer(fbdev->fbs[i]);
2051                 if (r != 0) {
2052                         dev_err(fbdev->dev,
2053                                 "registering framebuffer %d failed\n", i);
2054                         return r;
2055                 }
2056         }
2057
2058         DBG("framebuffers registered\n");
2059
2060         for (i = 0; i < fbdev->num_fbs; i++) {
2061                 struct fb_info *fbi = fbdev->fbs[i];
2062                 struct omapfb_info *ofbi = FB2OFB(fbi);
2063
2064                 omapfb_get_mem_region(ofbi->region);
2065                 r = omapfb_apply_changes(fbi, 1);
2066                 omapfb_put_mem_region(ofbi->region);
2067
2068                 if (r) {
2069                         dev_err(fbdev->dev, "failed to change mode\n");
2070                         return r;
2071                 }
2072         }
2073
2074         /* Enable fb0 */
2075         if (fbdev->num_fbs > 0) {
2076                 struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[0]);
2077
2078                 if (ofbi->num_overlays > 0) {
2079                         struct omap_overlay *ovl = ofbi->overlays[0];
2080
2081                         r = omapfb_overlay_enable(ovl, 1);
2082
2083                         if (r) {
2084                                 dev_err(fbdev->dev,
2085                                                 "failed to enable overlay\n");
2086                                 return r;
2087                         }
2088                 }
2089         }
2090
2091         DBG("create_framebuffers done\n");
2092
2093         return 0;
2094 }
2095
2096 static int omapfb_mode_to_timings(const char *mode_str,
2097                 struct omap_video_timings *timings, u8 *bpp)
2098 {
2099         struct fb_info *fbi;
2100         struct fb_var_screeninfo *var;
2101         struct fb_ops *fbops;
2102         int r;
2103
2104 #ifdef CONFIG_OMAP2_DSS_VENC
2105         if (strcmp(mode_str, "pal") == 0) {
2106                 *timings = omap_dss_pal_timings;
2107                 *bpp = 24;
2108                 return 0;
2109         } else if (strcmp(mode_str, "ntsc") == 0) {
2110                 *timings = omap_dss_ntsc_timings;
2111                 *bpp = 24;
2112                 return 0;
2113         }
2114 #endif
2115
2116         /* this is quite a hack, but I wanted to use the modedb and for
2117          * that we need fb_info and var, so we create dummy ones */
2118
2119         *bpp = 0;
2120         fbi = NULL;
2121         var = NULL;
2122         fbops = NULL;
2123
2124         fbi = kzalloc(sizeof(*fbi), GFP_KERNEL);
2125         if (fbi == NULL) {
2126                 r = -ENOMEM;
2127                 goto err;
2128         }
2129
2130         var = kzalloc(sizeof(*var), GFP_KERNEL);
2131         if (var == NULL) {
2132                 r = -ENOMEM;
2133                 goto err;
2134         }
2135
2136         fbops = kzalloc(sizeof(*fbops), GFP_KERNEL);
2137         if (fbops == NULL) {
2138                 r = -ENOMEM;
2139                 goto err;
2140         }
2141
2142         fbi->fbops = fbops;
2143
2144         r = fb_find_mode(var, fbi, mode_str, NULL, 0, NULL, 24);
2145         if (r == 0) {
2146                 r = -EINVAL;
2147                 goto err;
2148         }
2149
2150         timings->pixel_clock = PICOS2KHZ(var->pixclock);
2151         timings->hbp = var->left_margin;
2152         timings->hfp = var->right_margin;
2153         timings->vbp = var->upper_margin;
2154         timings->vfp = var->lower_margin;
2155         timings->hsw = var->hsync_len;
2156         timings->vsw = var->vsync_len;
2157         timings->x_res = var->xres;
2158         timings->y_res = var->yres;
2159
2160         switch (var->bits_per_pixel) {
2161         case 16:
2162                 *bpp = 16;
2163                 break;
2164         case 24:
2165         case 32:
2166         default:
2167                 *bpp = 24;
2168                 break;
2169         }
2170
2171         r = 0;
2172
2173 err:
2174         kfree(fbi);
2175         kfree(var);
2176         kfree(fbops);
2177
2178         return r;
2179 }
2180
2181 static int omapfb_set_def_mode(struct omapfb2_device *fbdev,
2182                 struct omap_dss_device *display, char *mode_str)
2183 {
2184         int r;
2185         u8 bpp;
2186         struct omap_video_timings timings, temp_timings;
2187         struct omapfb_display_data *d;
2188
2189         r = omapfb_mode_to_timings(mode_str, &timings, &bpp);
2190         if (r)
2191                 return r;
2192
2193         d = get_display_data(fbdev, display);
2194         d->bpp_override = bpp;
2195
2196         if (display->driver->check_timings) {
2197                 r = display->driver->check_timings(display, &timings);
2198                 if (r)
2199                         return r;
2200         } else {
2201                 /* If check_timings is not present compare xres and yres */
2202                 if (display->driver->get_timings) {
2203                         display->driver->get_timings(display, &temp_timings);
2204
2205                         if (temp_timings.x_res != timings.x_res ||
2206                                 temp_timings.y_res != timings.y_res)
2207                                 return -EINVAL;
2208                 }
2209         }
2210
2211         if (display->driver->set_timings)
2212                         display->driver->set_timings(display, &timings);
2213
2214         return 0;
2215 }
2216
2217 static int omapfb_get_recommended_bpp(struct omapfb2_device *fbdev,
2218                 struct omap_dss_device *dssdev)
2219 {
2220         struct omapfb_display_data *d;
2221
2222         BUG_ON(dssdev->driver->get_recommended_bpp == NULL);
2223
2224         d = get_display_data(fbdev, dssdev);
2225
2226         if (d->bpp_override != 0)
2227                 return d->bpp_override;
2228
2229         return dssdev->driver->get_recommended_bpp(dssdev);
2230 }
2231
2232 static int omapfb_parse_def_modes(struct omapfb2_device *fbdev)
2233 {
2234         char *str, *options, *this_opt;
2235         int r = 0;
2236
2237         str = kstrdup(def_mode, GFP_KERNEL);
2238         if (!str)
2239                 return -ENOMEM;
2240         options = str;
2241
2242         while (!r && (this_opt = strsep(&options, ",")) != NULL) {
2243                 char *p, *display_str, *mode_str;
2244                 struct omap_dss_device *display;
2245                 int i;
2246
2247                 p = strchr(this_opt, ':');
2248                 if (!p) {
2249                         r = -EINVAL;
2250                         break;
2251                 }
2252
2253                 *p = 0;
2254                 display_str = this_opt;
2255                 mode_str = p + 1;
2256
2257                 display = NULL;
2258                 for (i = 0; i < fbdev->num_displays; ++i) {
2259                         if (strcmp(fbdev->displays[i].dssdev->name,
2260                                                 display_str) == 0) {
2261                                 display = fbdev->displays[i].dssdev;
2262                                 break;
2263                         }
2264                 }
2265
2266                 if (!display) {
2267                         r = -EINVAL;
2268                         break;
2269                 }
2270
2271                 r = omapfb_set_def_mode(fbdev, display, mode_str);
2272                 if (r)
2273                         break;
2274         }
2275
2276         kfree(str);
2277
2278         return r;
2279 }
2280
2281 static void fb_videomode_to_omap_timings(struct fb_videomode *m,
2282                 struct omap_video_timings *t)
2283 {
2284         t->x_res = m->xres;
2285         t->y_res = m->yres;
2286         t->pixel_clock = PICOS2KHZ(m->pixclock);
2287         t->hsw = m->hsync_len;
2288         t->hfp = m->right_margin;
2289         t->hbp = m->left_margin;
2290         t->vsw = m->vsync_len;
2291         t->vfp = m->lower_margin;
2292         t->vbp = m->upper_margin;
2293 }
2294
2295 static int omapfb_find_best_mode(struct omap_dss_device *display,
2296                 struct omap_video_timings *timings)
2297 {
2298         struct fb_monspecs *specs;
2299         u8 *edid;
2300         int r, i, best_xres, best_idx, len;
2301
2302         if (!display->driver->read_edid)
2303                 return -ENODEV;
2304
2305         len = 0x80 * 2;
2306         edid = kmalloc(len, GFP_KERNEL);
2307
2308         r = display->driver->read_edid(display, edid, len);
2309         if (r < 0)
2310                 goto err1;
2311
2312         specs = kzalloc(sizeof(*specs), GFP_KERNEL);
2313
2314         fb_edid_to_monspecs(edid, specs);
2315
2316         if (edid[126] > 0)
2317                 fb_edid_add_monspecs(edid + 0x80, specs);
2318
2319         best_xres = 0;
2320         best_idx = -1;
2321
2322         for (i = 0; i < specs->modedb_len; ++i) {
2323                 struct fb_videomode *m;
2324                 struct omap_video_timings t;
2325
2326                 m = &specs->modedb[i];
2327
2328                 if (m->pixclock == 0)
2329                         continue;
2330
2331                 /* skip repeated pixel modes */
2332                 if (m->xres == 2880 || m->xres == 1440)
2333                         continue;
2334
2335                 fb_videomode_to_omap_timings(m, &t);
2336
2337                 r = display->driver->check_timings(display, &t);
2338                 if (r == 0 && best_xres < m->xres) {
2339                         best_xres = m->xres;
2340                         best_idx = i;
2341                 }
2342         }
2343
2344         if (best_xres == 0) {
2345                 r = -ENOENT;
2346                 goto err2;
2347         }
2348
2349         fb_videomode_to_omap_timings(&specs->modedb[best_idx], timings);
2350
2351         r = 0;
2352
2353 err2:
2354         fb_destroy_modedb(specs->modedb);
2355         kfree(specs);
2356 err1:
2357         kfree(edid);
2358
2359         return r;
2360 }
2361
2362 static int omapfb_init_display(struct omapfb2_device *fbdev,
2363                 struct omap_dss_device *dssdev)
2364 {
2365         struct omap_dss_driver *dssdrv = dssdev->driver;
2366         struct omapfb_display_data *d;
2367         int r;
2368
2369         r = dssdrv->enable(dssdev);
2370         if (r) {
2371                 dev_warn(fbdev->dev, "Failed to enable display '%s'\n",
2372                                 dssdev->name);
2373                 return r;
2374         }
2375
2376         d = get_display_data(fbdev, dssdev);
2377
2378         d->fbdev = fbdev;
2379
2380         if (dssdev->caps & OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE) {
2381                 u16 w, h;
2382
2383                 if (auto_update) {
2384                         omapfb_start_auto_update(fbdev, dssdev);
2385                         d->update_mode = OMAPFB_AUTO_UPDATE;
2386                 } else {
2387                         d->update_mode = OMAPFB_MANUAL_UPDATE;
2388                 }
2389
2390                 if (dssdrv->enable_te) {
2391                         r = dssdrv->enable_te(dssdev, 1);
2392                         if (r) {
2393                                 dev_err(fbdev->dev, "Failed to set TE\n");
2394                                 return r;
2395                         }
2396                 }
2397
2398                 dssdrv->get_resolution(dssdev, &w, &h);
2399                 r = dssdrv->update(dssdev, 0, 0, w, h);
2400                 if (r) {
2401                         dev_err(fbdev->dev,
2402                                         "Failed to update display\n");
2403                         return r;
2404                 }
2405         } else {
2406                 d->update_mode = OMAPFB_AUTO_UPDATE;
2407         }
2408
2409         return 0;
2410 }
2411
2412 static int omapfb_probe(struct platform_device *pdev)
2413 {
2414         struct omapfb2_device *fbdev = NULL;
2415         int r = 0;
2416         int i;
2417         struct omap_overlay *ovl;
2418         struct omap_dss_device *def_display;
2419         struct omap_dss_device *dssdev;
2420
2421         DBG("omapfb_probe\n");
2422
2423         if (pdev->num_resources != 0) {
2424                 dev_err(&pdev->dev, "probed for an unknown device\n");
2425                 r = -ENODEV;
2426                 goto err0;
2427         }
2428
2429         fbdev = kzalloc(sizeof(struct omapfb2_device), GFP_KERNEL);
2430         if (fbdev == NULL) {
2431                 r = -ENOMEM;
2432                 goto err0;
2433         }
2434
2435         /* TODO : Replace cpu check with omap_has_vrfb once HAS_FEATURE
2436         *        available for OMAP2 and OMAP3
2437         */
2438         if (def_vrfb && !cpu_is_omap24xx() && !cpu_is_omap34xx()) {
2439                 def_vrfb = 0;
2440                 dev_warn(&pdev->dev, "VRFB is not supported on this hardware, "
2441                                 "ignoring the module parameter vrfb=y\n");
2442         }
2443
2444
2445         mutex_init(&fbdev->mtx);
2446
2447         fbdev->dev = &pdev->dev;
2448         platform_set_drvdata(pdev, fbdev);
2449
2450         r = 0;
2451         fbdev->num_displays = 0;
2452         dssdev = NULL;
2453         for_each_dss_dev(dssdev) {
2454                 struct omapfb_display_data *d;
2455
2456                 omap_dss_get_device(dssdev);
2457
2458                 if (!dssdev->driver) {
2459                         dev_warn(&pdev->dev, "no driver for display: %s\n",
2460                                 dssdev->name);
2461                         omap_dss_put_device(dssdev);
2462                         continue;
2463                 }
2464
2465                 d = &fbdev->displays[fbdev->num_displays++];
2466                 d->dssdev = dssdev;
2467                 if (dssdev->caps & OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE)
2468                         d->update_mode = OMAPFB_MANUAL_UPDATE;
2469                 else
2470                         d->update_mode = OMAPFB_AUTO_UPDATE;
2471         }
2472
2473         if (r)
2474                 goto cleanup;
2475
2476         if (fbdev->num_displays == 0) {
2477                 dev_err(&pdev->dev, "no displays\n");
2478                 r = -EINVAL;
2479                 goto cleanup;
2480         }
2481
2482         fbdev->num_overlays = omap_dss_get_num_overlays();
2483         for (i = 0; i < fbdev->num_overlays; i++)
2484                 fbdev->overlays[i] = omap_dss_get_overlay(i);
2485
2486         fbdev->num_managers = omap_dss_get_num_overlay_managers();
2487         for (i = 0; i < fbdev->num_managers; i++)
2488                 fbdev->managers[i] = omap_dss_get_overlay_manager(i);
2489
2490         /* gfx overlay should be the default one. find a display
2491          * connected to that, and use it as default display */
2492         ovl = omap_dss_get_overlay(0);
2493         if (ovl->manager && ovl->manager->device) {
2494                 def_display = ovl->manager->device;
2495         } else {
2496                 dev_warn(&pdev->dev, "cannot find default display\n");
2497                 def_display = NULL;
2498         }
2499
2500         if (def_mode && strlen(def_mode) > 0) {
2501                 if (omapfb_parse_def_modes(fbdev))
2502                         dev_warn(&pdev->dev, "cannot parse default modes\n");
2503         } else if (def_display && def_display->driver->set_timings &&
2504                         def_display->driver->check_timings) {
2505                 struct omap_video_timings t;
2506
2507                 r = omapfb_find_best_mode(def_display, &t);
2508
2509                 if (r == 0)
2510                         def_display->driver->set_timings(def_display, &t);
2511         }
2512
2513         r = omapfb_create_framebuffers(fbdev);
2514         if (r)
2515                 goto cleanup;
2516
2517         for (i = 0; i < fbdev->num_managers; i++) {
2518                 struct omap_overlay_manager *mgr;
2519                 mgr = fbdev->managers[i];
2520                 r = mgr->apply(mgr);
2521                 if (r)
2522                         dev_warn(fbdev->dev, "failed to apply dispc config\n");
2523         }
2524
2525         DBG("mgr->apply'ed\n");
2526
2527         if (def_display) {
2528                 r = omapfb_init_display(fbdev, def_display);
2529                 if (r) {
2530                         dev_err(fbdev->dev,
2531                                         "failed to initialize default "
2532                                         "display\n");
2533                         goto cleanup;
2534                 }
2535         }
2536
2537         DBG("create sysfs for fbs\n");
2538         r = omapfb_create_sysfs(fbdev);
2539         if (r) {
2540                 dev_err(fbdev->dev, "failed to create sysfs entries\n");
2541                 goto cleanup;
2542         }
2543
2544         return 0;
2545
2546 cleanup:
2547         omapfb_free_resources(fbdev);
2548 err0:
2549         dev_err(&pdev->dev, "failed to setup omapfb\n");
2550         return r;
2551 }
2552
2553 static int omapfb_remove(struct platform_device *pdev)
2554 {
2555         struct omapfb2_device *fbdev = platform_get_drvdata(pdev);
2556
2557         /* FIXME: wait till completion of pending events */
2558
2559         omapfb_remove_sysfs(fbdev);
2560
2561         omapfb_free_resources(fbdev);
2562
2563         return 0;
2564 }
2565
2566 static struct platform_driver omapfb_driver = {
2567         .probe          = omapfb_probe,
2568         .remove         = omapfb_remove,
2569         .driver         = {
2570                 .name   = "omapfb",
2571                 .owner  = THIS_MODULE,
2572         },
2573 };
2574
2575 static int __init omapfb_init(void)
2576 {
2577         DBG("omapfb_init\n");
2578
2579         if (platform_driver_register(&omapfb_driver)) {
2580                 printk(KERN_ERR "failed to register omapfb driver\n");
2581                 return -ENODEV;
2582         }
2583
2584         return 0;
2585 }
2586
2587 static void __exit omapfb_exit(void)
2588 {
2589         DBG("omapfb_exit\n");
2590         platform_driver_unregister(&omapfb_driver);
2591 }
2592
2593 module_param_named(mode, def_mode, charp, 0);
2594 module_param_named(vram, def_vram, charp, 0);
2595 module_param_named(rotate, def_rotate, int, 0);
2596 module_param_named(vrfb, def_vrfb, bool, 0);
2597 module_param_named(mirror, def_mirror, bool, 0);
2598 module_param_named(vram_cache, def_vram_cache, bool, 0644);
2599
2600 /* late_initcall to let panel/ctrl drivers loaded first.
2601  * I guess better option would be a more dynamic approach,
2602  * so that omapfb reacts to new panels when they are loaded */
2603 late_initcall(omapfb_init);
2604 /*module_init(omapfb_init);*/
2605 module_exit(omapfb_exit);
2606
2607 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@nokia.com>");
2608 MODULE_DESCRIPTION("OMAP2/3 Framebuffer");
2609 MODULE_LICENSE("GPL v2");