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