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