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