]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/video/omap2/omapfb/omapfb-main.c
OMAP: DSS2: OMAPFB: Add support for switching memory regions
[karo-tx-linux.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 <plat/display.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         r = fb_mode_to_dss_mode(var, &mode);
672         if (r) {
673                 DBG("cannot convert var to omap dss mode\n");
674                 return r;
675         }
676
677         for (i = 0; i < ofbi->num_overlays; ++i) {
678                 if ((ofbi->overlays[i]->supported_modes & mode) == 0) {
679                         DBG("invalid mode\n");
680                         return -EINVAL;
681                 }
682         }
683
684         if (var->rotate < 0 || var->rotate > 3)
685                 return -EINVAL;
686
687         if (check_fb_res_bounds(var))
688                 return -EINVAL;
689
690         /* When no memory is allocated ignore the size check */
691         if (ofbi->region->size != 0 && check_fb_size(ofbi, var))
692                 return -EINVAL;
693
694         if (var->xres + var->xoffset > var->xres_virtual)
695                 var->xoffset = var->xres_virtual - var->xres;
696         if (var->yres + var->yoffset > var->yres_virtual)
697                 var->yoffset = var->yres_virtual - var->yres;
698
699         DBG("xres = %d, yres = %d, vxres = %d, vyres = %d\n",
700                         var->xres, var->yres,
701                         var->xres_virtual, var->yres_virtual);
702
703         var->height             = -1;
704         var->width              = -1;
705         var->grayscale          = 0;
706
707         if (display && display->driver->get_timings) {
708                 struct omap_video_timings timings;
709                 display->driver->get_timings(display, &timings);
710
711                 /* pixclock in ps, the rest in pixclock */
712                 var->pixclock = timings.pixel_clock != 0 ?
713                         KHZ2PICOS(timings.pixel_clock) :
714                         0;
715                 var->left_margin = timings.hfp;
716                 var->right_margin = timings.hbp;
717                 var->upper_margin = timings.vfp;
718                 var->lower_margin = timings.vbp;
719                 var->hsync_len = timings.hsw;
720                 var->vsync_len = timings.vsw;
721         } else {
722                 var->pixclock = 0;
723                 var->left_margin = 0;
724                 var->right_margin = 0;
725                 var->upper_margin = 0;
726                 var->lower_margin = 0;
727                 var->hsync_len = 0;
728                 var->vsync_len = 0;
729         }
730
731         /* TODO: get these from panel->config */
732         var->vmode              = FB_VMODE_NONINTERLACED;
733         var->sync               = 0;
734
735         return 0;
736 }
737
738 /*
739  * ---------------------------------------------------------------------------
740  * fbdev framework callbacks
741  * ---------------------------------------------------------------------------
742  */
743 static int omapfb_open(struct fb_info *fbi, int user)
744 {
745         return 0;
746 }
747
748 static int omapfb_release(struct fb_info *fbi, int user)
749 {
750 #if 0
751         struct omapfb_info *ofbi = FB2OFB(fbi);
752         struct omapfb2_device *fbdev = ofbi->fbdev;
753         struct omap_dss_device *display = fb2display(fbi);
754
755         DBG("Closing fb with plane index %d\n", ofbi->id);
756
757         omapfb_lock(fbdev);
758
759         if (display && display->get_update_mode && display->update) {
760                 /* XXX this update should be removed, I think. But it's
761                  * good for debugging */
762                 if (display->get_update_mode(display) ==
763                                 OMAP_DSS_UPDATE_MANUAL) {
764                         u16 w, h;
765
766                         if (display->sync)
767                                 display->sync(display);
768
769                         display->get_resolution(display, &w, &h);
770                         display->update(display, 0, 0, w, h);
771                 }
772         }
773
774         if (display && display->sync)
775                 display->sync(display);
776
777         omapfb_unlock(fbdev);
778 #endif
779         return 0;
780 }
781
782 static unsigned calc_rotation_offset_dma(const struct fb_var_screeninfo *var,
783                 const struct fb_fix_screeninfo *fix, int rotation)
784 {
785         unsigned offset;
786
787         offset = var->yoffset * fix->line_length +
788                 var->xoffset * (var->bits_per_pixel >> 3);
789
790         return offset;
791 }
792
793 static unsigned calc_rotation_offset_vrfb(const struct fb_var_screeninfo *var,
794                 const struct fb_fix_screeninfo *fix, int rotation)
795 {
796         unsigned offset;
797
798         if (rotation == FB_ROTATE_UD)
799                 offset = (var->yres_virtual - var->yres) *
800                         fix->line_length;
801         else if (rotation == FB_ROTATE_CW)
802                 offset = (var->yres_virtual - var->yres) *
803                         (var->bits_per_pixel >> 3);
804         else
805                 offset = 0;
806
807         if (rotation == FB_ROTATE_UR)
808                 offset += var->yoffset * fix->line_length +
809                         var->xoffset * (var->bits_per_pixel >> 3);
810         else if (rotation == FB_ROTATE_UD)
811                 offset -= var->yoffset * fix->line_length +
812                         var->xoffset * (var->bits_per_pixel >> 3);
813         else if (rotation == FB_ROTATE_CW)
814                 offset -= var->xoffset * fix->line_length +
815                         var->yoffset * (var->bits_per_pixel >> 3);
816         else if (rotation == FB_ROTATE_CCW)
817                 offset += var->xoffset * fix->line_length +
818                         var->yoffset * (var->bits_per_pixel >> 3);
819
820         return offset;
821 }
822
823 static void omapfb_calc_addr(const struct omapfb_info *ofbi,
824                              const struct fb_var_screeninfo *var,
825                              const struct fb_fix_screeninfo *fix,
826                              int rotation, u32 *paddr, void __iomem **vaddr)
827 {
828         u32 data_start_p;
829         void __iomem *data_start_v;
830         int offset;
831
832         if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
833                 data_start_p = omapfb_get_region_rot_paddr(ofbi, rotation);
834                 data_start_v = NULL;
835         } else {
836                 data_start_p = omapfb_get_region_paddr(ofbi);
837                 data_start_v = omapfb_get_region_vaddr(ofbi);
838         }
839
840         if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
841                 offset = calc_rotation_offset_vrfb(var, fix, rotation);
842         else
843                 offset = calc_rotation_offset_dma(var, fix, rotation);
844
845         data_start_p += offset;
846         data_start_v += offset;
847
848         if (offset)
849                 DBG("offset %d, %d = %d\n",
850                     var->xoffset, var->yoffset, offset);
851
852         DBG("paddr %x, vaddr %p\n", data_start_p, data_start_v);
853
854         *paddr = data_start_p;
855         *vaddr = data_start_v;
856 }
857
858 /* setup overlay according to the fb */
859 int omapfb_setup_overlay(struct fb_info *fbi, struct omap_overlay *ovl,
860                 u16 posx, u16 posy, u16 outw, u16 outh)
861 {
862         int r = 0;
863         struct omapfb_info *ofbi = FB2OFB(fbi);
864         struct fb_var_screeninfo *var = &fbi->var;
865         struct fb_fix_screeninfo *fix = &fbi->fix;
866         enum omap_color_mode mode = 0;
867         u32 data_start_p = 0;
868         void __iomem *data_start_v = NULL;
869         struct omap_overlay_info info;
870         int xres, yres;
871         int screen_width;
872         int mirror;
873         int rotation = var->rotate;
874         int i;
875
876         for (i = 0; i < ofbi->num_overlays; i++) {
877                 if (ovl != ofbi->overlays[i])
878                         continue;
879
880                 rotation = (rotation + ofbi->rotation[i]) % 4;
881                 break;
882         }
883
884         DBG("setup_overlay %d, posx %d, posy %d, outw %d, outh %d\n", ofbi->id,
885                         posx, posy, outw, outh);
886
887         if (rotation == FB_ROTATE_CW || rotation == FB_ROTATE_CCW) {
888                 xres = var->yres;
889                 yres = var->xres;
890         } else {
891                 xres = var->xres;
892                 yres = var->yres;
893         }
894
895         if (ofbi->region->size)
896                 omapfb_calc_addr(ofbi, var, fix, rotation,
897                                  &data_start_p, &data_start_v);
898
899         r = fb_mode_to_dss_mode(var, &mode);
900         if (r) {
901                 DBG("fb_mode_to_dss_mode failed");
902                 goto err;
903         }
904
905         switch (var->nonstd) {
906         case OMAPFB_COLOR_YUV422:
907         case OMAPFB_COLOR_YUY422:
908                 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
909                         screen_width = fix->line_length
910                                 / (var->bits_per_pixel >> 2);
911                         break;
912                 }
913         default:
914                 screen_width = fix->line_length / (var->bits_per_pixel >> 3);
915                 break;
916         }
917
918         ovl->get_overlay_info(ovl, &info);
919
920         if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
921                 mirror = 0;
922         else
923                 mirror = ofbi->mirror;
924
925         info.paddr = data_start_p;
926         info.vaddr = data_start_v;
927         info.screen_width = screen_width;
928         info.width = xres;
929         info.height = yres;
930         info.color_mode = mode;
931         info.rotation_type = ofbi->rotation_type;
932         info.rotation = rotation;
933         info.mirror = mirror;
934
935         info.pos_x = posx;
936         info.pos_y = posy;
937         info.out_width = outw;
938         info.out_height = outh;
939
940         r = ovl->set_overlay_info(ovl, &info);
941         if (r) {
942                 DBG("ovl->setup_overlay_info failed\n");
943                 goto err;
944         }
945
946         return 0;
947
948 err:
949         DBG("setup_overlay failed\n");
950         return r;
951 }
952
953 /* apply var to the overlay */
954 int omapfb_apply_changes(struct fb_info *fbi, int init)
955 {
956         int r = 0;
957         struct omapfb_info *ofbi = FB2OFB(fbi);
958         struct fb_var_screeninfo *var = &fbi->var;
959         struct omap_overlay *ovl;
960         u16 posx, posy;
961         u16 outw, outh;
962         int i;
963
964 #ifdef DEBUG
965         if (omapfb_test_pattern)
966                 fill_fb(fbi);
967 #endif
968
969         for (i = 0; i < ofbi->num_overlays; i++) {
970                 ovl = ofbi->overlays[i];
971
972                 DBG("apply_changes, fb %d, ovl %d\n", ofbi->id, ovl->id);
973
974                 if (ofbi->region->size == 0) {
975                         /* the fb is not available. disable the overlay */
976                         omapfb_overlay_enable(ovl, 0);
977                         if (!init && ovl->manager)
978                                 ovl->manager->apply(ovl->manager);
979                         continue;
980                 }
981
982                 if (init || (ovl->caps & OMAP_DSS_OVL_CAP_SCALE) == 0) {
983                         int rotation = (var->rotate + ofbi->rotation[i]) % 4;
984                         if (rotation == FB_ROTATE_CW ||
985                                         rotation == FB_ROTATE_CCW) {
986                                 outw = var->yres;
987                                 outh = var->xres;
988                         } else {
989                                 outw = var->xres;
990                                 outh = var->yres;
991                         }
992                 } else {
993                         outw = ovl->info.out_width;
994                         outh = ovl->info.out_height;
995                 }
996
997                 if (init) {
998                         posx = 0;
999                         posy = 0;
1000                 } else {
1001                         posx = ovl->info.pos_x;
1002                         posy = ovl->info.pos_y;
1003                 }
1004
1005                 r = omapfb_setup_overlay(fbi, ovl, posx, posy, outw, outh);
1006                 if (r)
1007                         goto err;
1008
1009                 if (!init && ovl->manager)
1010                         ovl->manager->apply(ovl->manager);
1011         }
1012         return 0;
1013 err:
1014         DBG("apply_changes failed\n");
1015         return r;
1016 }
1017
1018 /* checks var and eventually tweaks it to something supported,
1019  * DO NOT MODIFY PAR */
1020 static int omapfb_check_var(struct fb_var_screeninfo *var, struct fb_info *fbi)
1021 {
1022         int r;
1023
1024         DBG("check_var(%d)\n", FB2OFB(fbi)->id);
1025
1026         r = check_fb_var(fbi, var);
1027
1028         return r;
1029 }
1030
1031 /* set the video mode according to info->var */
1032 static int omapfb_set_par(struct fb_info *fbi)
1033 {
1034         int r;
1035
1036         DBG("set_par(%d)\n", FB2OFB(fbi)->id);
1037
1038         set_fb_fix(fbi);
1039
1040         r = setup_vrfb_rotation(fbi);
1041         if (r)
1042                 return r;
1043
1044         r = omapfb_apply_changes(fbi, 0);
1045
1046         return r;
1047 }
1048
1049 static int omapfb_pan_display(struct fb_var_screeninfo *var,
1050                 struct fb_info *fbi)
1051 {
1052         struct fb_var_screeninfo new_var;
1053         int r;
1054
1055         DBG("pan_display(%d)\n", FB2OFB(fbi)->id);
1056
1057         if (var->xoffset == fbi->var.xoffset &&
1058             var->yoffset == fbi->var.yoffset)
1059                 return 0;
1060
1061         new_var = fbi->var;
1062         new_var.xoffset = var->xoffset;
1063         new_var.yoffset = var->yoffset;
1064
1065         fbi->var = new_var;
1066
1067         r = omapfb_apply_changes(fbi, 0);
1068
1069         return r;
1070 }
1071
1072 static void mmap_user_open(struct vm_area_struct *vma)
1073 {
1074         struct omapfb2_mem_region *rg = vma->vm_private_data;
1075
1076         atomic_inc(&rg->map_count);
1077 }
1078
1079 static void mmap_user_close(struct vm_area_struct *vma)
1080 {
1081         struct omapfb2_mem_region *rg = vma->vm_private_data;
1082
1083         atomic_dec(&rg->map_count);
1084 }
1085
1086 static struct vm_operations_struct mmap_user_ops = {
1087         .open = mmap_user_open,
1088         .close = mmap_user_close,
1089 };
1090
1091 static int omapfb_mmap(struct fb_info *fbi, struct vm_area_struct *vma)
1092 {
1093         struct omapfb_info *ofbi = FB2OFB(fbi);
1094         struct fb_fix_screeninfo *fix = &fbi->fix;
1095         struct omapfb2_mem_region *rg;
1096         unsigned long off;
1097         unsigned long start;
1098         u32 len;
1099
1100         if (vma->vm_end - vma->vm_start == 0)
1101                 return 0;
1102         if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
1103                 return -EINVAL;
1104         off = vma->vm_pgoff << PAGE_SHIFT;
1105
1106         rg = ofbi->region;
1107
1108         start = omapfb_get_region_paddr(ofbi);
1109         len = fix->smem_len;
1110         if (off >= len)
1111                 return -EINVAL;
1112         if ((vma->vm_end - vma->vm_start + off) > len)
1113                 return -EINVAL;
1114
1115         off += start;
1116
1117         DBG("user mmap region start %lx, len %d, off %lx\n", start, len, off);
1118
1119         vma->vm_pgoff = off >> PAGE_SHIFT;
1120         vma->vm_flags |= VM_IO | VM_RESERVED;
1121         vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
1122         vma->vm_ops = &mmap_user_ops;
1123         vma->vm_private_data = rg;
1124         if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
1125                              vma->vm_end - vma->vm_start, vma->vm_page_prot))
1126                 return -EAGAIN;
1127         /* vm_ops.open won't be called for mmap itself. */
1128         atomic_inc(&rg->map_count);
1129         return 0;
1130 }
1131
1132 /* Store a single color palette entry into a pseudo palette or the hardware
1133  * palette if one is available. For now we support only 16bpp and thus store
1134  * the entry only to the pseudo palette.
1135  */
1136 static int _setcolreg(struct fb_info *fbi, u_int regno, u_int red, u_int green,
1137                 u_int blue, u_int transp, int update_hw_pal)
1138 {
1139         /*struct omapfb_info *ofbi = FB2OFB(fbi);*/
1140         /*struct omapfb2_device *fbdev = ofbi->fbdev;*/
1141         struct fb_var_screeninfo *var = &fbi->var;
1142         int r = 0;
1143
1144         enum omapfb_color_format mode = OMAPFB_COLOR_RGB24U; /* XXX */
1145
1146         /*switch (plane->color_mode) {*/
1147         switch (mode) {
1148         case OMAPFB_COLOR_YUV422:
1149         case OMAPFB_COLOR_YUV420:
1150         case OMAPFB_COLOR_YUY422:
1151                 r = -EINVAL;
1152                 break;
1153         case OMAPFB_COLOR_CLUT_8BPP:
1154         case OMAPFB_COLOR_CLUT_4BPP:
1155         case OMAPFB_COLOR_CLUT_2BPP:
1156         case OMAPFB_COLOR_CLUT_1BPP:
1157                 /*
1158                    if (fbdev->ctrl->setcolreg)
1159                    r = fbdev->ctrl->setcolreg(regno, red, green, blue,
1160                    transp, update_hw_pal);
1161                    */
1162                 /* Fallthrough */
1163                 r = -EINVAL;
1164                 break;
1165         case OMAPFB_COLOR_RGB565:
1166         case OMAPFB_COLOR_RGB444:
1167         case OMAPFB_COLOR_RGB24P:
1168         case OMAPFB_COLOR_RGB24U:
1169                 if (r != 0)
1170                         break;
1171
1172                 if (regno < 0) {
1173                         r = -EINVAL;
1174                         break;
1175                 }
1176
1177                 if (regno < 16) {
1178                         u16 pal;
1179                         pal = ((red >> (16 - var->red.length)) <<
1180                                         var->red.offset) |
1181                                 ((green >> (16 - var->green.length)) <<
1182                                  var->green.offset) |
1183                                 (blue >> (16 - var->blue.length));
1184                         ((u32 *)(fbi->pseudo_palette))[regno] = pal;
1185                 }
1186                 break;
1187         default:
1188                 BUG();
1189         }
1190         return r;
1191 }
1192
1193 static int omapfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
1194                 u_int transp, struct fb_info *info)
1195 {
1196         DBG("setcolreg\n");
1197
1198         return _setcolreg(info, regno, red, green, blue, transp, 1);
1199 }
1200
1201 static int omapfb_setcmap(struct fb_cmap *cmap, struct fb_info *info)
1202 {
1203         int count, index, r;
1204         u16 *red, *green, *blue, *transp;
1205         u16 trans = 0xffff;
1206
1207         DBG("setcmap\n");
1208
1209         red     = cmap->red;
1210         green   = cmap->green;
1211         blue    = cmap->blue;
1212         transp  = cmap->transp;
1213         index   = cmap->start;
1214
1215         for (count = 0; count < cmap->len; count++) {
1216                 if (transp)
1217                         trans = *transp++;
1218                 r = _setcolreg(info, index++, *red++, *green++, *blue++, trans,
1219                                 count == cmap->len - 1);
1220                 if (r != 0)
1221                         return r;
1222         }
1223
1224         return 0;
1225 }
1226
1227 static int omapfb_blank(int blank, struct fb_info *fbi)
1228 {
1229         struct omapfb_info *ofbi = FB2OFB(fbi);
1230         struct omapfb2_device *fbdev = ofbi->fbdev;
1231         struct omap_dss_device *display = fb2display(fbi);
1232         int do_update = 0;
1233         int r = 0;
1234
1235         omapfb_lock(fbdev);
1236
1237         switch (blank) {
1238         case FB_BLANK_UNBLANK:
1239                 if (display->state != OMAP_DSS_DISPLAY_SUSPENDED)
1240                         goto exit;
1241
1242                 if (display->driver->resume)
1243                         r = display->driver->resume(display);
1244
1245                 if (r == 0 && display->driver->get_update_mode &&
1246                                 display->driver->get_update_mode(display) ==
1247                                 OMAP_DSS_UPDATE_MANUAL)
1248                         do_update = 1;
1249
1250                 break;
1251
1252         case FB_BLANK_NORMAL:
1253                 /* FB_BLANK_NORMAL could be implemented.
1254                  * Needs DSS additions. */
1255         case FB_BLANK_VSYNC_SUSPEND:
1256         case FB_BLANK_HSYNC_SUSPEND:
1257         case FB_BLANK_POWERDOWN:
1258                 if (display->state != OMAP_DSS_DISPLAY_ACTIVE)
1259                         goto exit;
1260
1261                 if (display->driver->suspend)
1262                         r = display->driver->suspend(display);
1263
1264                 break;
1265
1266         default:
1267                 r = -EINVAL;
1268         }
1269
1270 exit:
1271         omapfb_unlock(fbdev);
1272
1273         if (r == 0 && do_update && display->driver->update) {
1274                 u16 w, h;
1275                 display->driver->get_resolution(display, &w, &h);
1276
1277                 r = display->driver->update(display, 0, 0, w, h);
1278         }
1279
1280         return r;
1281 }
1282
1283 #if 0
1284 /* XXX fb_read and fb_write are needed for VRFB */
1285 ssize_t omapfb_write(struct fb_info *info, const char __user *buf,
1286                 size_t count, loff_t *ppos)
1287 {
1288         DBG("omapfb_write %d, %lu\n", count, (unsigned long)*ppos);
1289         /* XXX needed for VRFB */
1290         return count;
1291 }
1292 #endif
1293
1294 static struct fb_ops omapfb_ops = {
1295         .owner          = THIS_MODULE,
1296         .fb_open        = omapfb_open,
1297         .fb_release     = omapfb_release,
1298         .fb_fillrect    = cfb_fillrect,
1299         .fb_copyarea    = cfb_copyarea,
1300         .fb_imageblit   = cfb_imageblit,
1301         .fb_blank       = omapfb_blank,
1302         .fb_ioctl       = omapfb_ioctl,
1303         .fb_check_var   = omapfb_check_var,
1304         .fb_set_par     = omapfb_set_par,
1305         .fb_pan_display = omapfb_pan_display,
1306         .fb_mmap        = omapfb_mmap,
1307         .fb_setcolreg   = omapfb_setcolreg,
1308         .fb_setcmap     = omapfb_setcmap,
1309         /*.fb_write     = omapfb_write,*/
1310 };
1311
1312 static void omapfb_free_fbmem(struct fb_info *fbi)
1313 {
1314         struct omapfb_info *ofbi = FB2OFB(fbi);
1315         struct omapfb2_device *fbdev = ofbi->fbdev;
1316         struct omapfb2_mem_region *rg;
1317
1318         rg = ofbi->region;
1319
1320         WARN_ON(atomic_read(&rg->map_count));
1321
1322         if (rg->paddr)
1323                 if (omap_vram_free(rg->paddr, rg->size))
1324                         dev_err(fbdev->dev, "VRAM FREE failed\n");
1325
1326         if (rg->vaddr)
1327                 iounmap(rg->vaddr);
1328
1329         if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
1330                 /* unmap the 0 angle rotation */
1331                 if (rg->vrfb.vaddr[0]) {
1332                         iounmap(rg->vrfb.vaddr[0]);
1333                         omap_vrfb_release_ctx(&rg->vrfb);
1334                         rg->vrfb.vaddr[0] = NULL;
1335                 }
1336         }
1337
1338         rg->vaddr = NULL;
1339         rg->paddr = 0;
1340         rg->alloc = 0;
1341         rg->size = 0;
1342 }
1343
1344 static void clear_fb_info(struct fb_info *fbi)
1345 {
1346         memset(&fbi->var, 0, sizeof(fbi->var));
1347         memset(&fbi->fix, 0, sizeof(fbi->fix));
1348         strlcpy(fbi->fix.id, MODULE_NAME, sizeof(fbi->fix.id));
1349 }
1350
1351 static int omapfb_free_all_fbmem(struct omapfb2_device *fbdev)
1352 {
1353         int i;
1354
1355         DBG("free all fbmem\n");
1356
1357         for (i = 0; i < fbdev->num_fbs; i++) {
1358                 struct fb_info *fbi = fbdev->fbs[i];
1359                 omapfb_free_fbmem(fbi);
1360                 clear_fb_info(fbi);
1361         }
1362
1363         return 0;
1364 }
1365
1366 static int omapfb_alloc_fbmem(struct fb_info *fbi, unsigned long size,
1367                 unsigned long paddr)
1368 {
1369         struct omapfb_info *ofbi = FB2OFB(fbi);
1370         struct omapfb2_device *fbdev = ofbi->fbdev;
1371         struct omapfb2_mem_region *rg;
1372         void __iomem *vaddr;
1373         int r;
1374
1375         rg = ofbi->region;
1376
1377         rg->paddr = 0;
1378         rg->vaddr = NULL;
1379         memset(&rg->vrfb, 0, sizeof rg->vrfb);
1380         rg->size = 0;
1381         rg->type = 0;
1382         rg->alloc = false;
1383         rg->map = false;
1384
1385         size = PAGE_ALIGN(size);
1386
1387         if (!paddr) {
1388                 DBG("allocating %lu bytes for fb %d\n", size, ofbi->id);
1389                 r = omap_vram_alloc(OMAP_VRAM_MEMTYPE_SDRAM, size, &paddr);
1390         } else {
1391                 DBG("reserving %lu bytes at %lx for fb %d\n", size, paddr,
1392                                 ofbi->id);
1393                 r = omap_vram_reserve(paddr, size);
1394         }
1395
1396         if (r) {
1397                 dev_err(fbdev->dev, "failed to allocate framebuffer\n");
1398                 return -ENOMEM;
1399         }
1400
1401         if (ofbi->rotation_type != OMAP_DSS_ROT_VRFB) {
1402                 vaddr = ioremap_wc(paddr, size);
1403
1404                 if (!vaddr) {
1405                         dev_err(fbdev->dev, "failed to ioremap framebuffer\n");
1406                         omap_vram_free(paddr, size);
1407                         return -ENOMEM;
1408                 }
1409
1410                 DBG("allocated VRAM paddr %lx, vaddr %p\n", paddr, vaddr);
1411         } else {
1412                 r = omap_vrfb_request_ctx(&rg->vrfb);
1413                 if (r) {
1414                         dev_err(fbdev->dev, "vrfb create ctx failed\n");
1415                         return r;
1416                 }
1417
1418                 vaddr = NULL;
1419         }
1420
1421         rg->paddr = paddr;
1422         rg->vaddr = vaddr;
1423         rg->size = size;
1424         rg->alloc = 1;
1425
1426         return 0;
1427 }
1428
1429 /* allocate fbmem using display resolution as reference */
1430 static int omapfb_alloc_fbmem_display(struct fb_info *fbi, unsigned long size,
1431                 unsigned long paddr)
1432 {
1433         struct omapfb_info *ofbi = FB2OFB(fbi);
1434         struct omapfb2_device *fbdev = ofbi->fbdev;
1435         struct omap_dss_device *display;
1436         int bytespp;
1437
1438         display =  fb2display(fbi);
1439
1440         if (!display)
1441                 return 0;
1442
1443         switch (omapfb_get_recommended_bpp(fbdev, display)) {
1444         case 16:
1445                 bytespp = 2;
1446                 break;
1447         case 24:
1448                 bytespp = 4;
1449                 break;
1450         default:
1451                 bytespp = 4;
1452                 break;
1453         }
1454
1455         if (!size) {
1456                 u16 w, h;
1457
1458                 display->driver->get_resolution(display, &w, &h);
1459
1460                 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
1461                         size = max(omap_vrfb_min_phys_size(w, h, bytespp),
1462                                         omap_vrfb_min_phys_size(h, w, bytespp));
1463
1464                         DBG("adjusting fb mem size for VRFB, %u -> %lu\n",
1465                                         w * h * bytespp, size);
1466                 } else {
1467                         size = w * h * bytespp;
1468                 }
1469         }
1470
1471         if (!size)
1472                 return 0;
1473
1474         return omapfb_alloc_fbmem(fbi, size, paddr);
1475 }
1476
1477 static enum omap_color_mode fb_format_to_dss_mode(enum omapfb_color_format fmt)
1478 {
1479         enum omap_color_mode mode;
1480
1481         switch (fmt) {
1482         case OMAPFB_COLOR_RGB565:
1483                 mode = OMAP_DSS_COLOR_RGB16;
1484                 break;
1485         case OMAPFB_COLOR_YUV422:
1486                 mode = OMAP_DSS_COLOR_YUV2;
1487                 break;
1488         case OMAPFB_COLOR_CLUT_8BPP:
1489                 mode = OMAP_DSS_COLOR_CLUT8;
1490                 break;
1491         case OMAPFB_COLOR_CLUT_4BPP:
1492                 mode = OMAP_DSS_COLOR_CLUT4;
1493                 break;
1494         case OMAPFB_COLOR_CLUT_2BPP:
1495                 mode = OMAP_DSS_COLOR_CLUT2;
1496                 break;
1497         case OMAPFB_COLOR_CLUT_1BPP:
1498                 mode = OMAP_DSS_COLOR_CLUT1;
1499                 break;
1500         case OMAPFB_COLOR_RGB444:
1501                 mode = OMAP_DSS_COLOR_RGB12U;
1502                 break;
1503         case OMAPFB_COLOR_YUY422:
1504                 mode = OMAP_DSS_COLOR_UYVY;
1505                 break;
1506         case OMAPFB_COLOR_ARGB16:
1507                 mode = OMAP_DSS_COLOR_ARGB16;
1508                 break;
1509         case OMAPFB_COLOR_RGB24U:
1510                 mode = OMAP_DSS_COLOR_RGB24U;
1511                 break;
1512         case OMAPFB_COLOR_RGB24P:
1513                 mode = OMAP_DSS_COLOR_RGB24P;
1514                 break;
1515         case OMAPFB_COLOR_ARGB32:
1516                 mode = OMAP_DSS_COLOR_ARGB32;
1517                 break;
1518         case OMAPFB_COLOR_RGBA32:
1519                 mode = OMAP_DSS_COLOR_RGBA32;
1520                 break;
1521         case OMAPFB_COLOR_RGBX32:
1522                 mode = OMAP_DSS_COLOR_RGBX32;
1523                 break;
1524         default:
1525                 mode = -EINVAL;
1526         }
1527
1528         return mode;
1529 }
1530
1531 static int omapfb_parse_vram_param(const char *param, int max_entries,
1532                 unsigned long *sizes, unsigned long *paddrs)
1533 {
1534         int fbnum;
1535         unsigned long size;
1536         unsigned long paddr = 0;
1537         char *p, *start;
1538
1539         start = (char *)param;
1540
1541         while (1) {
1542                 p = start;
1543
1544                 fbnum = simple_strtoul(p, &p, 10);
1545
1546                 if (p == param)
1547                         return -EINVAL;
1548
1549                 if (*p != ':')
1550                         return -EINVAL;
1551
1552                 if (fbnum >= max_entries)
1553                         return -EINVAL;
1554
1555                 size = memparse(p + 1, &p);
1556
1557                 if (!size)
1558                         return -EINVAL;
1559
1560                 paddr = 0;
1561
1562                 if (*p == '@') {
1563                         paddr = simple_strtoul(p + 1, &p, 16);
1564
1565                         if (!paddr)
1566                                 return -EINVAL;
1567
1568                 }
1569
1570                 paddrs[fbnum] = paddr;
1571                 sizes[fbnum] = size;
1572
1573                 if (*p == 0)
1574                         break;
1575
1576                 if (*p != ',')
1577                         return -EINVAL;
1578
1579                 ++p;
1580
1581                 start = p;
1582         }
1583
1584         return 0;
1585 }
1586
1587 static int omapfb_allocate_all_fbs(struct omapfb2_device *fbdev)
1588 {
1589         int i, r;
1590         unsigned long vram_sizes[10];
1591         unsigned long vram_paddrs[10];
1592
1593         memset(&vram_sizes, 0, sizeof(vram_sizes));
1594         memset(&vram_paddrs, 0, sizeof(vram_paddrs));
1595
1596         if (def_vram && omapfb_parse_vram_param(def_vram, 10,
1597                                 vram_sizes, vram_paddrs)) {
1598                 dev_err(fbdev->dev, "failed to parse vram parameter\n");
1599
1600                 memset(&vram_sizes, 0, sizeof(vram_sizes));
1601                 memset(&vram_paddrs, 0, sizeof(vram_paddrs));
1602         }
1603
1604         if (fbdev->dev->platform_data) {
1605                 struct omapfb_platform_data *opd;
1606                 opd = fbdev->dev->platform_data;
1607                 for (i = 0; i < opd->mem_desc.region_cnt; ++i) {
1608                         if (!vram_sizes[i]) {
1609                                 unsigned long size;
1610                                 unsigned long paddr;
1611
1612                                 size = opd->mem_desc.region[i].size;
1613                                 paddr = opd->mem_desc.region[i].paddr;
1614
1615                                 vram_sizes[i] = size;
1616                                 vram_paddrs[i] = paddr;
1617                         }
1618                 }
1619         }
1620
1621         for (i = 0; i < fbdev->num_fbs; i++) {
1622                 /* allocate memory automatically only for fb0, or if
1623                  * excplicitly defined with vram or plat data option */
1624                 if (i == 0 || vram_sizes[i] != 0) {
1625                         r = omapfb_alloc_fbmem_display(fbdev->fbs[i],
1626                                         vram_sizes[i], vram_paddrs[i]);
1627
1628                         if (r)
1629                                 return r;
1630                 }
1631         }
1632
1633         for (i = 0; i < fbdev->num_fbs; i++) {
1634                 struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[i]);
1635                 struct omapfb2_mem_region *rg;
1636                 rg = ofbi->region;
1637
1638                 DBG("region%d phys %08x virt %p size=%lu\n",
1639                                 i,
1640                                 rg->paddr,
1641                                 rg->vaddr,
1642                                 rg->size);
1643         }
1644
1645         return 0;
1646 }
1647
1648 int omapfb_realloc_fbmem(struct fb_info *fbi, unsigned long size, int type)
1649 {
1650         struct omapfb_info *ofbi = FB2OFB(fbi);
1651         struct omapfb2_device *fbdev = ofbi->fbdev;
1652         struct omap_dss_device *display = fb2display(fbi);
1653         struct omapfb2_mem_region *rg = ofbi->region;
1654         unsigned long old_size = rg->size;
1655         unsigned long old_paddr = rg->paddr;
1656         int old_type = rg->type;
1657         int r;
1658
1659         if (type > OMAPFB_MEMTYPE_MAX)
1660                 return -EINVAL;
1661
1662         size = PAGE_ALIGN(size);
1663
1664         if (old_size == size && old_type == type)
1665                 return 0;
1666
1667         if (display && display->driver->sync)
1668                         display->driver->sync(display);
1669
1670         omapfb_free_fbmem(fbi);
1671
1672         if (size == 0) {
1673                 clear_fb_info(fbi);
1674                 return 0;
1675         }
1676
1677         r = omapfb_alloc_fbmem(fbi, size, 0);
1678
1679         if (r) {
1680                 if (old_size)
1681                         omapfb_alloc_fbmem(fbi, old_size, old_paddr);
1682
1683                 if (rg->size == 0)
1684                         clear_fb_info(fbi);
1685
1686                 return r;
1687         }
1688
1689         if (old_size == size)
1690                 return 0;
1691
1692         if (old_size == 0) {
1693                 DBG("initializing fb %d\n", ofbi->id);
1694                 r = omapfb_fb_init(fbdev, fbi);
1695                 if (r) {
1696                         DBG("omapfb_fb_init failed\n");
1697                         goto err;
1698                 }
1699                 r = omapfb_apply_changes(fbi, 1);
1700                 if (r) {
1701                         DBG("omapfb_apply_changes failed\n");
1702                         goto err;
1703                 }
1704         } else {
1705                 struct fb_var_screeninfo new_var;
1706                 memcpy(&new_var, &fbi->var, sizeof(new_var));
1707                 r = check_fb_var(fbi, &new_var);
1708                 if (r)
1709                         goto err;
1710                 memcpy(&fbi->var, &new_var, sizeof(fbi->var));
1711                 set_fb_fix(fbi);
1712                 r = setup_vrfb_rotation(fbi);
1713                 if (r)
1714                         goto err;
1715         }
1716
1717         return 0;
1718 err:
1719         omapfb_free_fbmem(fbi);
1720         clear_fb_info(fbi);
1721         return r;
1722 }
1723
1724 /* initialize fb_info, var, fix to something sane based on the display */
1725 static int omapfb_fb_init(struct omapfb2_device *fbdev, struct fb_info *fbi)
1726 {
1727         struct fb_var_screeninfo *var = &fbi->var;
1728         struct omap_dss_device *display = fb2display(fbi);
1729         struct omapfb_info *ofbi = FB2OFB(fbi);
1730         int r = 0;
1731
1732         fbi->fbops = &omapfb_ops;
1733         fbi->flags = FBINFO_FLAG_DEFAULT;
1734         fbi->pseudo_palette = fbdev->pseudo_palette;
1735
1736         if (ofbi->region->size == 0) {
1737                 clear_fb_info(fbi);
1738                 return 0;
1739         }
1740
1741         var->nonstd = 0;
1742         var->bits_per_pixel = 0;
1743
1744         var->rotate = def_rotate;
1745
1746         /*
1747          * Check if there is a default color format set in the board file,
1748          * and use this format instead the default deducted from the
1749          * display bpp.
1750          */
1751         if (fbdev->dev->platform_data) {
1752                 struct omapfb_platform_data *opd;
1753                 int id = ofbi->id;
1754
1755                 opd = fbdev->dev->platform_data;
1756                 if (opd->mem_desc.region[id].format_used) {
1757                         enum omap_color_mode mode;
1758                         enum omapfb_color_format format;
1759
1760                         format = opd->mem_desc.region[id].format;
1761                         mode = fb_format_to_dss_mode(format);
1762                         if (mode < 0) {
1763                                 r = mode;
1764                                 goto err;
1765                         }
1766                         r = dss_mode_to_fb_mode(mode, var);
1767                         if (r < 0)
1768                                 goto err;
1769                 }
1770         }
1771
1772         if (display) {
1773                 u16 w, h;
1774                 int rotation = (var->rotate + ofbi->rotation[0]) % 4;
1775
1776                 display->driver->get_resolution(display, &w, &h);
1777
1778                 if (rotation == FB_ROTATE_CW ||
1779                                 rotation == FB_ROTATE_CCW) {
1780                         var->xres = h;
1781                         var->yres = w;
1782                 } else {
1783                         var->xres = w;
1784                         var->yres = h;
1785                 }
1786
1787                 var->xres_virtual = var->xres;
1788                 var->yres_virtual = var->yres;
1789
1790                 if (!var->bits_per_pixel) {
1791                         switch (omapfb_get_recommended_bpp(fbdev, display)) {
1792                         case 16:
1793                                 var->bits_per_pixel = 16;
1794                                 break;
1795                         case 24:
1796                                 var->bits_per_pixel = 32;
1797                                 break;
1798                         default:
1799                                 dev_err(fbdev->dev, "illegal display "
1800                                                 "bpp\n");
1801                                 return -EINVAL;
1802                         }
1803                 }
1804         } else {
1805                 /* if there's no display, let's just guess some basic values */
1806                 var->xres = 320;
1807                 var->yres = 240;
1808                 var->xres_virtual = var->xres;
1809                 var->yres_virtual = var->yres;
1810                 if (!var->bits_per_pixel)
1811                         var->bits_per_pixel = 16;
1812         }
1813
1814         r = check_fb_var(fbi, var);
1815         if (r)
1816                 goto err;
1817
1818         set_fb_fix(fbi);
1819         r = setup_vrfb_rotation(fbi);
1820         if (r)
1821                 goto err;
1822
1823         r = fb_alloc_cmap(&fbi->cmap, 256, 0);
1824         if (r)
1825                 dev_err(fbdev->dev, "unable to allocate color map memory\n");
1826
1827 err:
1828         return r;
1829 }
1830
1831 static void fbinfo_cleanup(struct omapfb2_device *fbdev, struct fb_info *fbi)
1832 {
1833         fb_dealloc_cmap(&fbi->cmap);
1834 }
1835
1836
1837 static void omapfb_free_resources(struct omapfb2_device *fbdev)
1838 {
1839         int i;
1840
1841         DBG("free_resources\n");
1842
1843         if (fbdev == NULL)
1844                 return;
1845
1846         for (i = 0; i < fbdev->num_fbs; i++)
1847                 unregister_framebuffer(fbdev->fbs[i]);
1848
1849         /* free the reserved fbmem */
1850         omapfb_free_all_fbmem(fbdev);
1851
1852         for (i = 0; i < fbdev->num_fbs; i++) {
1853                 fbinfo_cleanup(fbdev, fbdev->fbs[i]);
1854                 framebuffer_release(fbdev->fbs[i]);
1855         }
1856
1857         for (i = 0; i < fbdev->num_displays; i++) {
1858                 if (fbdev->displays[i]->state != OMAP_DSS_DISPLAY_DISABLED)
1859                         fbdev->displays[i]->driver->disable(fbdev->displays[i]);
1860
1861                 omap_dss_put_device(fbdev->displays[i]);
1862         }
1863
1864         dev_set_drvdata(fbdev->dev, NULL);
1865         kfree(fbdev);
1866 }
1867
1868 static int omapfb_create_framebuffers(struct omapfb2_device *fbdev)
1869 {
1870         int r, i;
1871
1872         fbdev->num_fbs = 0;
1873
1874         DBG("create %d framebuffers\n", CONFIG_FB_OMAP2_NUM_FBS);
1875
1876         /* allocate fb_infos */
1877         for (i = 0; i < CONFIG_FB_OMAP2_NUM_FBS; i++) {
1878                 struct fb_info *fbi;
1879                 struct omapfb_info *ofbi;
1880
1881                 fbi = framebuffer_alloc(sizeof(struct omapfb_info),
1882                                 fbdev->dev);
1883
1884                 if (fbi == NULL) {
1885                         dev_err(fbdev->dev,
1886                                 "unable to allocate memory for plane info\n");
1887                         return -ENOMEM;
1888                 }
1889
1890                 clear_fb_info(fbi);
1891
1892                 fbdev->fbs[i] = fbi;
1893
1894                 ofbi = FB2OFB(fbi);
1895                 ofbi->fbdev = fbdev;
1896                 ofbi->id = i;
1897
1898                 ofbi->region = &fbdev->regions[i];
1899                 ofbi->region->id = i;
1900
1901                 /* assign these early, so that fb alloc can use them */
1902                 ofbi->rotation_type = def_vrfb ? OMAP_DSS_ROT_VRFB :
1903                         OMAP_DSS_ROT_DMA;
1904                 ofbi->mirror = def_mirror;
1905
1906                 fbdev->num_fbs++;
1907         }
1908
1909         DBG("fb_infos allocated\n");
1910
1911         /* assign overlays for the fbs */
1912         for (i = 0; i < min(fbdev->num_fbs, fbdev->num_overlays); i++) {
1913                 struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[i]);
1914
1915                 ofbi->overlays[0] = fbdev->overlays[i];
1916                 ofbi->num_overlays = 1;
1917         }
1918
1919         /* allocate fb memories */
1920         r = omapfb_allocate_all_fbs(fbdev);
1921         if (r) {
1922                 dev_err(fbdev->dev, "failed to allocate fbmem\n");
1923                 return r;
1924         }
1925
1926         DBG("fbmems allocated\n");
1927
1928         /* setup fb_infos */
1929         for (i = 0; i < fbdev->num_fbs; i++) {
1930                 r = omapfb_fb_init(fbdev, fbdev->fbs[i]);
1931                 if (r) {
1932                         dev_err(fbdev->dev, "failed to setup fb_info\n");
1933                         return r;
1934                 }
1935         }
1936
1937         DBG("fb_infos initialized\n");
1938
1939         for (i = 0; i < fbdev->num_fbs; i++) {
1940                 r = register_framebuffer(fbdev->fbs[i]);
1941                 if (r != 0) {
1942                         dev_err(fbdev->dev,
1943                                 "registering framebuffer %d failed\n", i);
1944                         return r;
1945                 }
1946         }
1947
1948         DBG("framebuffers registered\n");
1949
1950         for (i = 0; i < fbdev->num_fbs; i++) {
1951                 r = omapfb_apply_changes(fbdev->fbs[i], 1);
1952                 if (r) {
1953                         dev_err(fbdev->dev, "failed to change mode\n");
1954                         return r;
1955                 }
1956         }
1957
1958         DBG("create sysfs for fbs\n");
1959         r = omapfb_create_sysfs(fbdev);
1960         if (r) {
1961                 dev_err(fbdev->dev, "failed to create sysfs entries\n");
1962                 return r;
1963         }
1964
1965         /* Enable fb0 */
1966         if (fbdev->num_fbs > 0) {
1967                 struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[0]);
1968
1969                 if (ofbi->num_overlays > 0) {
1970                         struct omap_overlay *ovl = ofbi->overlays[0];
1971
1972                         r = omapfb_overlay_enable(ovl, 1);
1973
1974                         if (r) {
1975                                 dev_err(fbdev->dev,
1976                                                 "failed to enable overlay\n");
1977                                 return r;
1978                         }
1979                 }
1980         }
1981
1982         DBG("create_framebuffers done\n");
1983
1984         return 0;
1985 }
1986
1987 static int omapfb_mode_to_timings(const char *mode_str,
1988                 struct omap_video_timings *timings, u8 *bpp)
1989 {
1990         struct fb_info fbi;
1991         struct fb_var_screeninfo var;
1992         struct fb_ops fbops;
1993         int r;
1994
1995 #ifdef CONFIG_OMAP2_DSS_VENC
1996         if (strcmp(mode_str, "pal") == 0) {
1997                 *timings = omap_dss_pal_timings;
1998                 *bpp = 0;
1999                 return 0;
2000         } else if (strcmp(mode_str, "ntsc") == 0) {
2001                 *timings = omap_dss_ntsc_timings;
2002                 *bpp = 0;
2003                 return 0;
2004         }
2005 #endif
2006
2007         /* this is quite a hack, but I wanted to use the modedb and for
2008          * that we need fb_info and var, so we create dummy ones */
2009
2010         memset(&fbi, 0, sizeof(fbi));
2011         memset(&var, 0, sizeof(var));
2012         memset(&fbops, 0, sizeof(fbops));
2013         fbi.fbops = &fbops;
2014
2015         r = fb_find_mode(&var, &fbi, mode_str, NULL, 0, NULL, 24);
2016
2017         if (r != 0) {
2018                 timings->pixel_clock = PICOS2KHZ(var.pixclock);
2019                 timings->hfp = var.left_margin;
2020                 timings->hbp = var.right_margin;
2021                 timings->vfp = var.upper_margin;
2022                 timings->vbp = var.lower_margin;
2023                 timings->hsw = var.hsync_len;
2024                 timings->vsw = var.vsync_len;
2025                 timings->x_res = var.xres;
2026                 timings->y_res = var.yres;
2027
2028                 switch (var.bits_per_pixel) {
2029                 case 16:
2030                         *bpp = 16;
2031                         break;
2032                 case 24:
2033                 case 32:
2034                 default:
2035                         *bpp = 24;
2036                         break;
2037                 }
2038
2039                 return 0;
2040         } else {
2041                 return -EINVAL;
2042         }
2043 }
2044
2045 static int omapfb_set_def_mode(struct omapfb2_device *fbdev,
2046                 struct omap_dss_device *display, char *mode_str)
2047 {
2048         int r;
2049         u8 bpp;
2050         struct omap_video_timings timings;
2051
2052         r = omapfb_mode_to_timings(mode_str, &timings, &bpp);
2053         if (r)
2054                 return r;
2055
2056         fbdev->bpp_overrides[fbdev->num_bpp_overrides].dssdev = display;
2057         fbdev->bpp_overrides[fbdev->num_bpp_overrides].bpp = bpp;
2058         ++fbdev->num_bpp_overrides;
2059
2060         if (!display->driver->check_timings || !display->driver->set_timings)
2061                 return -EINVAL;
2062
2063         r = display->driver->check_timings(display, &timings);
2064         if (r)
2065                 return r;
2066
2067         display->driver->set_timings(display, &timings);
2068
2069         return 0;
2070 }
2071
2072 static int omapfb_get_recommended_bpp(struct omapfb2_device *fbdev,
2073                 struct omap_dss_device *dssdev)
2074 {
2075         int i;
2076
2077         BUG_ON(dssdev->driver->get_recommended_bpp == NULL);
2078
2079         for (i = 0; i < fbdev->num_bpp_overrides; ++i) {
2080                 if (dssdev == fbdev->bpp_overrides[i].dssdev)
2081                         return fbdev->bpp_overrides[i].bpp;
2082         }
2083
2084         return dssdev->driver->get_recommended_bpp(dssdev);
2085 }
2086
2087 static int omapfb_parse_def_modes(struct omapfb2_device *fbdev)
2088 {
2089         char *str, *options, *this_opt;
2090         int r = 0;
2091
2092         str = kmalloc(strlen(def_mode) + 1, GFP_KERNEL);
2093         strcpy(str, def_mode);
2094         options = str;
2095
2096         while (!r && (this_opt = strsep(&options, ",")) != NULL) {
2097                 char *p, *display_str, *mode_str;
2098                 struct omap_dss_device *display;
2099                 int i;
2100
2101                 p = strchr(this_opt, ':');
2102                 if (!p) {
2103                         r = -EINVAL;
2104                         break;
2105                 }
2106
2107                 *p = 0;
2108                 display_str = this_opt;
2109                 mode_str = p + 1;
2110
2111                 display = NULL;
2112                 for (i = 0; i < fbdev->num_displays; ++i) {
2113                         if (strcmp(fbdev->displays[i]->name,
2114                                                 display_str) == 0) {
2115                                 display = fbdev->displays[i];
2116                                 break;
2117                         }
2118                 }
2119
2120                 if (!display) {
2121                         r = -EINVAL;
2122                         break;
2123                 }
2124
2125                 r = omapfb_set_def_mode(fbdev, display, mode_str);
2126                 if (r)
2127                         break;
2128         }
2129
2130         kfree(str);
2131
2132         return r;
2133 }
2134
2135 static int omapfb_probe(struct platform_device *pdev)
2136 {
2137         struct omapfb2_device *fbdev = NULL;
2138         int r = 0;
2139         int i;
2140         struct omap_overlay *ovl;
2141         struct omap_dss_device *def_display;
2142         struct omap_dss_device *dssdev;
2143
2144         DBG("omapfb_probe\n");
2145
2146         if (pdev->num_resources != 0) {
2147                 dev_err(&pdev->dev, "probed for an unknown device\n");
2148                 r = -ENODEV;
2149                 goto err0;
2150         }
2151
2152         fbdev = kzalloc(sizeof(struct omapfb2_device), GFP_KERNEL);
2153         if (fbdev == NULL) {
2154                 r = -ENOMEM;
2155                 goto err0;
2156         }
2157
2158         mutex_init(&fbdev->mtx);
2159
2160         fbdev->dev = &pdev->dev;
2161         platform_set_drvdata(pdev, fbdev);
2162
2163         r = 0;
2164         fbdev->num_displays = 0;
2165         dssdev = NULL;
2166         for_each_dss_dev(dssdev) {
2167                 omap_dss_get_device(dssdev);
2168
2169                 if (!dssdev->driver) {
2170                         dev_err(&pdev->dev, "no driver for display\n");
2171                         r = -ENODEV;
2172                 }
2173
2174                 fbdev->displays[fbdev->num_displays++] = dssdev;
2175         }
2176
2177         if (r)
2178                 goto cleanup;
2179
2180         if (fbdev->num_displays == 0) {
2181                 dev_err(&pdev->dev, "no displays\n");
2182                 r = -EINVAL;
2183                 goto cleanup;
2184         }
2185
2186         fbdev->num_overlays = omap_dss_get_num_overlays();
2187         for (i = 0; i < fbdev->num_overlays; i++)
2188                 fbdev->overlays[i] = omap_dss_get_overlay(i);
2189
2190         fbdev->num_managers = omap_dss_get_num_overlay_managers();
2191         for (i = 0; i < fbdev->num_managers; i++)
2192                 fbdev->managers[i] = omap_dss_get_overlay_manager(i);
2193
2194         if (def_mode && strlen(def_mode) > 0) {
2195                 if (omapfb_parse_def_modes(fbdev))
2196                         dev_warn(&pdev->dev, "cannot parse default modes\n");
2197         }
2198
2199         r = omapfb_create_framebuffers(fbdev);
2200         if (r)
2201                 goto cleanup;
2202
2203         for (i = 0; i < fbdev->num_managers; i++) {
2204                 struct omap_overlay_manager *mgr;
2205                 mgr = fbdev->managers[i];
2206                 r = mgr->apply(mgr);
2207                 if (r)
2208                         dev_warn(fbdev->dev, "failed to apply dispc config\n");
2209         }
2210
2211         DBG("mgr->apply'ed\n");
2212
2213         /* gfx overlay should be the default one. find a display
2214          * connected to that, and use it as default display */
2215         ovl = omap_dss_get_overlay(0);
2216         if (ovl->manager && ovl->manager->device) {
2217                 def_display = ovl->manager->device;
2218         } else {
2219                 dev_warn(&pdev->dev, "cannot find default display\n");
2220                 def_display = NULL;
2221         }
2222
2223         if (def_display) {
2224                 struct omap_dss_driver *dssdrv = def_display->driver;
2225
2226                 r = def_display->driver->enable(def_display);
2227                 if (r) {
2228                         dev_warn(fbdev->dev, "Failed to enable display '%s'\n",
2229                                         def_display->name);
2230                         goto cleanup;
2231                 }
2232
2233                 if (def_display->caps & OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE) {
2234                         u16 w, h;
2235                         if (dssdrv->enable_te)
2236                                 dssdrv->enable_te(def_display, 1);
2237                         if (dssdrv->set_update_mode)
2238                                 dssdrv->set_update_mode(def_display,
2239                                                 OMAP_DSS_UPDATE_MANUAL);
2240
2241                         dssdrv->get_resolution(def_display, &w, &h);
2242                         def_display->driver->update(def_display, 0, 0, w, h);
2243                 } else {
2244                         if (dssdrv->set_update_mode)
2245                                 dssdrv->set_update_mode(def_display,
2246                                                 OMAP_DSS_UPDATE_AUTO);
2247                 }
2248         }
2249
2250         return 0;
2251
2252 cleanup:
2253         omapfb_free_resources(fbdev);
2254 err0:
2255         dev_err(&pdev->dev, "failed to setup omapfb\n");
2256         return r;
2257 }
2258
2259 static int omapfb_remove(struct platform_device *pdev)
2260 {
2261         struct omapfb2_device *fbdev = platform_get_drvdata(pdev);
2262
2263         /* FIXME: wait till completion of pending events */
2264
2265         omapfb_remove_sysfs(fbdev);
2266
2267         omapfb_free_resources(fbdev);
2268
2269         return 0;
2270 }
2271
2272 static struct platform_driver omapfb_driver = {
2273         .probe          = omapfb_probe,
2274         .remove         = omapfb_remove,
2275         .driver         = {
2276                 .name   = "omapfb",
2277                 .owner  = THIS_MODULE,
2278         },
2279 };
2280
2281 static int __init omapfb_init(void)
2282 {
2283         DBG("omapfb_init\n");
2284
2285         if (platform_driver_register(&omapfb_driver)) {
2286                 printk(KERN_ERR "failed to register omapfb driver\n");
2287                 return -ENODEV;
2288         }
2289
2290         return 0;
2291 }
2292
2293 static void __exit omapfb_exit(void)
2294 {
2295         DBG("omapfb_exit\n");
2296         platform_driver_unregister(&omapfb_driver);
2297 }
2298
2299 module_param_named(mode, def_mode, charp, 0);
2300 module_param_named(vram, def_vram, charp, 0);
2301 module_param_named(rotate, def_rotate, int, 0);
2302 module_param_named(vrfb, def_vrfb, bool, 0);
2303 module_param_named(mirror, def_mirror, bool, 0);
2304
2305 /* late_initcall to let panel/ctrl drivers loaded first.
2306  * I guess better option would be a more dynamic approach,
2307  * so that omapfb reacts to new panels when they are loaded */
2308 late_initcall(omapfb_init);
2309 /*module_init(omapfb_init);*/
2310 module_exit(omapfb_exit);
2311
2312 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@nokia.com>");
2313 MODULE_DESCRIPTION("OMAP2/3 Framebuffer");
2314 MODULE_LICENSE("GPL v2");