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