Staging: et131x: Properly disable FC in txmac.
[nv-tegra-linux-2.6.git] / drivers / video / omap2 / omapfb / omapfb-main.c
blob4a76917b7cc8dcd6e0aa6787477ce37168dcfd78
1 /*
2 * linux/drivers/video/omap2/omapfb-main.c
4 * Copyright (C) 2008 Nokia Corporation
5 * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
7 * Some code and ideas taken from drivers/video/omap/ driver
8 * by Imre Deak.
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.
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.
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/>.
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>
32 #include <plat/display.h>
33 #include <plat/vram.h>
34 #include <plat/vrfb.h>
36 #include "omapfb.h"
38 #define MODULE_NAME "omapfb"
40 #define OMAPFB_PLANE_XRES_MIN 8
41 #define OMAPFB_PLANE_YRES_MIN 8
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;
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
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);
60 #ifdef DEBUG
61 static void draw_pixel(struct fb_info *fbi, int x, int y, unsigned color)
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;
69 int r = (color >> 16) & 0xff;
70 int g = (color >> 8) & 0xff;
71 int b = (color >> 0) & 0xff;
73 if (var->bits_per_pixel == 16) {
74 u16 __iomem *p = (u16 __iomem *)addr;
75 p += y * line_len + x;
77 r = r * 32 / 256;
78 g = g * 64 / 256;
79 b = b * 32 / 256;
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;
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);
96 static void fill_fb(struct fb_info *fbi)
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;
104 if (!addr)
105 return;
107 DBG("fill_fb %dx%d, line_len %d bytes\n", w, h, fbi->fix.line_length);
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);
147 c = (r << 16) | (g << 8) | (b << 0);
148 draw_pixel(fbi, x, y, c);
149 } else {
150 draw_pixel(fbi, x, y, 0);
155 #endif
157 static unsigned omapfb_get_vrfb_offset(const struct omapfb_info *ofbi, int rot)
159 const struct vrfb *vrfb = &ofbi->region.vrfb;
160 unsigned offset;
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();
179 offset *= vrfb->bytespp;
181 return offset;
184 static u32 omapfb_get_region_rot_paddr(const struct omapfb_info *ofbi, int rot)
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;
194 static u32 omapfb_get_region_paddr(const struct omapfb_info *ofbi)
196 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
197 return ofbi->region.vrfb.paddr[0];
198 else
199 return ofbi->region.paddr;
202 static void __iomem *omapfb_get_region_vaddr(const struct omapfb_info *ofbi)
204 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
205 return ofbi->region.vrfb.vaddr[0];
206 else
207 return ofbi->region.vaddr;
210 static struct omapfb_colormode omapfb_colormodes[] = {
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 },
271 static bool cmp_var_to_colormode(struct fb_var_screeninfo *var,
272 struct omapfb_colormode *color)
274 bool cmp_component(struct fb_bitfield *f1, struct fb_bitfield *f2)
276 return f1->length == f2->length &&
277 f1->offset == f2->offset &&
278 f1->msb_right == f2->msb_right;
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;
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);
294 static void assign_colormode_to_var(struct fb_var_screeninfo *var,
295 struct omapfb_colormode *color)
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;
305 static int fb_mode_to_dss_mode(struct fb_var_screeninfo *var,
306 enum omap_color_mode *mode)
308 enum omap_color_mode dssmode;
309 int i;
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;
322 return -EINVAL;
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;
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;
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;
375 return -EINVAL;
378 static int check_fb_res_bounds(struct fb_var_screeninfo *var)
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;
385 /* XXX: some applications seem to set virtual res to 0. */
386 if (var->xres_virtual == 0)
387 var->xres_virtual = var->xres;
389 if (var->yres_virtual == 0)
390 var->yres_virtual = var->yres;
392 if (var->xres_virtual < xres_min || var->yres_virtual < yres_min)
393 return -EINVAL;
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;
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;
409 return 0;
412 static void shrink_height(unsigned long max_frame_size,
413 struct fb_var_screeninfo *var)
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);
419 if (var->yres_virtual < OMAPFB_PLANE_YRES_MIN)
420 var->yres_virtual = OMAPFB_PLANE_YRES_MIN;
422 if (var->yres > var->yres_virtual)
423 var->yres = var->yres_virtual;
426 static void shrink_width(unsigned long max_frame_size,
427 struct fb_var_screeninfo *var)
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);
433 if (var->xres_virtual < OMAPFB_PLANE_XRES_MIN)
434 var->xres_virtual = OMAPFB_PLANE_XRES_MIN;
436 if (var->xres > var->xres_virtual)
437 var->xres = var->xres_virtual;
440 static int check_vrfb_fb_size(unsigned long region_size,
441 const struct fb_var_screeninfo *var)
443 unsigned long min_phys_size = omap_vrfb_min_phys_size(var->xres_virtual,
444 var->yres_virtual, var->bits_per_pixel >> 3);
446 return min_phys_size > region_size ? -EINVAL : 0;
449 static int check_fb_size(const struct omapfb_info *ofbi,
450 struct fb_var_screeninfo *var)
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;
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);
463 if (check_vrfb_fb_size(max_frame_size, var)) {
464 DBG("cannot fit FB to memory\n");
465 return -EINVAL;
468 return 0;
471 DBG("max frame size %lu, line size %lu\n", max_frame_size, line_size);
473 if (line_size * var->yres_virtual > max_frame_size)
474 shrink_height(max_frame_size, var);
476 if (line_size * var->yres_virtual > max_frame_size) {
477 shrink_width(max_frame_size, var);
478 line_size = var->xres_virtual * bytespp;
481 if (line_size * var->yres_virtual > max_frame_size) {
482 DBG("cannot fit FB to memory\n");
483 return -EINVAL;
486 return 0;
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.
496 static int setup_vrfb_rotation(struct fb_info *fbi)
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;
509 if (!rg->size || ofbi->rotation_type != OMAP_DSS_ROT_VRFB)
510 return 0;
512 DBG("setup_vrfb_rotation\n");
514 r = fb_mode_to_dss_mode(var, &mode);
515 if (r)
516 return r;
518 bytespp = var->bits_per_pixel >> 3;
520 yuv_mode = mode == OMAP_DSS_COLOR_YUV2 || mode == OMAP_DSS_COLOR_UYVY;
522 /* We need to reconfigure VRFB if the resolution changes, if yuv mode
523 * is enabled/disabled, or if bytes per pixel changes */
525 /* XXX we shouldn't allow this when framebuffer is mmapped */
527 reconf = false;
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;
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");
546 if (vrfb->vaddr[0])
547 return 0;
549 omap_vrfb_setup(&rg->vrfb, rg->paddr,
550 var->xres_virtual,
551 var->yres_virtual,
552 bytespp, yuv_mode);
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;
559 /* used by open/write in fbmem.c */
560 fbi->screen_base = ofbi->region.vrfb.vaddr[0];
562 fix->smem_start = ofbi->region.vrfb.paddr[0];
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;
576 fix->smem_len = var->yres_virtual * fix->line_length;
578 return 0;
581 int dss_mode_to_fb_mode(enum omap_color_mode dssmode,
582 struct fb_var_screeninfo *var)
584 int i;
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;
593 return -ENOENT;
596 void set_fb_fix(struct fb_info *fbi)
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;
603 DBG("set_fb_fix\n");
605 /* used by open/write in fbmem.c */
606 fbi->screen_base = (char __iomem *)omapfb_get_region_vaddr(ofbi);
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;
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;
629 fix->smem_start = omapfb_get_region_paddr(ofbi);
631 fix->type = FB_TYPE_PACKED_PIXELS;
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;
653 fix->accel = FB_ACCEL_NONE;
655 fix->xpanstep = 1;
656 fix->ypanstep = 1;
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)
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;
668 DBG("check_fb_var %d\n", ofbi->id);
670 if (ofbi->region.size == 0)
671 return 0;
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;
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;
686 if (var->rotate < 0 || var->rotate > 3)
687 return -EINVAL;
689 if (check_fb_res_bounds(var))
690 return -EINVAL;
692 if (check_fb_size(ofbi, var))
693 return -EINVAL;
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;
700 DBG("xres = %d, yres = %d, vxres = %d, vyres = %d\n",
701 var->xres, var->yres,
702 var->xres_virtual, var->yres_virtual);
704 var->height = -1;
705 var->width = -1;
706 var->grayscale = 0;
708 if (display && display->driver->get_timings) {
709 struct omap_video_timings timings;
710 display->driver->get_timings(display, &timings);
712 /* pixclock in ps, the rest in pixclock */
713 var->pixclock = timings.pixel_clock != 0 ?
714 KHZ2PICOS(timings.pixel_clock) :
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;
732 /* TODO: get these from panel->config */
733 var->vmode = FB_VMODE_NONINTERLACED;
734 var->sync = 0;
736 return 0;
740 * ---------------------------------------------------------------------------
741 * fbdev framework callbacks
742 * ---------------------------------------------------------------------------
744 static int omapfb_open(struct fb_info *fbi, int user)
746 return 0;
749 static int omapfb_release(struct fb_info *fbi, int user)
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);
756 DBG("Closing fb with plane index %d\n", ofbi->id);
758 omapfb_lock(fbdev);
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;
767 if (display->sync)
768 display->sync(display);
770 display->get_resolution(display, &w, &h);
771 display->update(display, 0, 0, w, h);
775 if (display && display->sync)
776 display->sync(display);
778 omapfb_unlock(fbdev);
779 #endif
780 return 0;
783 static unsigned calc_rotation_offset_dma(const struct fb_var_screeninfo *var,
784 const struct fb_fix_screeninfo *fix, int rotation)
786 unsigned offset;
788 offset = var->yoffset * fix->line_length +
789 var->xoffset * (var->bits_per_pixel >> 3);
791 return offset;
794 static unsigned calc_rotation_offset_vrfb(const struct fb_var_screeninfo *var,
795 const struct fb_fix_screeninfo *fix, int rotation)
797 unsigned offset;
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;
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);
821 return offset;
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)
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;
844 for (i = 0; i < ofbi->num_overlays; i++) {
845 if (ovl != ofbi->overlays[i])
846 continue;
848 rotation = (rotation + ofbi->rotation[i]) % 4;
849 break;
852 DBG("setup_overlay %d, posx %d, posy %d, outw %d, outh %d\n", ofbi->id,
853 posx, posy, outw, outh);
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;
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);
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);
877 data_start_p += offset;
878 data_start_v += offset;
880 if (offset)
881 DBG("offset %d, %d = %d\n",
882 var->xoffset, var->yoffset, offset);
884 DBG("paddr %x, vaddr %p\n", data_start_p, data_start_v);
886 r = fb_mode_to_dss_mode(var, &mode);
887 if (r) {
888 DBG("fb_mode_to_dss_mode failed");
889 goto err;
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;
900 default:
901 screen_width = fix->line_length / (var->bits_per_pixel >> 3);
902 break;
905 ovl->get_overlay_info(ovl, &info);
907 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
908 mirror = 0;
909 else
910 mirror = ofbi->mirror;
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;
922 info.pos_x = posx;
923 info.pos_y = posy;
924 info.out_width = outw;
925 info.out_height = outh;
927 r = ovl->set_overlay_info(ovl, &info);
928 if (r) {
929 DBG("ovl->setup_overlay_info failed\n");
930 goto err;
933 return 0;
935 err:
936 DBG("setup_overlay failed\n");
937 return r;
940 /* apply var to the overlay */
941 int omapfb_apply_changes(struct fb_info *fbi, int init)
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;
951 #ifdef DEBUG
952 if (omapfb_test_pattern)
953 fill_fb(fbi);
954 #endif
956 for (i = 0; i < ofbi->num_overlays; i++) {
957 ovl = ofbi->overlays[i];
959 DBG("apply_changes, fb %d, ovl %d\n", ofbi->id, ovl->id);
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;
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;
979 } else {
980 outw = ovl->info.out_width;
981 outh = ovl->info.out_height;
984 if (init) {
985 posx = 0;
986 posy = 0;
987 } else {
988 posx = ovl->info.pos_x;
989 posy = ovl->info.pos_y;
992 r = omapfb_setup_overlay(fbi, ovl, posx, posy, outw, outh);
993 if (r)
994 goto err;
996 if (!init && ovl->manager)
997 ovl->manager->apply(ovl->manager);
999 return 0;
1000 err:
1001 DBG("apply_changes failed\n");
1002 return r;
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)
1009 int r;
1011 DBG("check_var(%d)\n", FB2OFB(fbi)->id);
1013 r = check_fb_var(fbi, var);
1015 return r;
1018 /* set the video mode according to info->var */
1019 static int omapfb_set_par(struct fb_info *fbi)
1021 int r;
1023 DBG("set_par(%d)\n", FB2OFB(fbi)->id);
1025 set_fb_fix(fbi);
1027 r = setup_vrfb_rotation(fbi);
1028 if (r)
1029 return r;
1031 r = omapfb_apply_changes(fbi, 0);
1033 return r;
1036 static int omapfb_pan_display(struct fb_var_screeninfo *var,
1037 struct fb_info *fbi)
1039 struct fb_var_screeninfo new_var;
1040 int r;
1042 DBG("pan_display(%d)\n", FB2OFB(fbi)->id);
1044 if (var->xoffset == fbi->var.xoffset &&
1045 var->yoffset == fbi->var.yoffset)
1046 return 0;
1048 new_var = fbi->var;
1049 new_var.xoffset = var->xoffset;
1050 new_var.yoffset = var->yoffset;
1052 fbi->var = new_var;
1054 r = omapfb_apply_changes(fbi, 0);
1056 return r;
1059 static void mmap_user_open(struct vm_area_struct *vma)
1061 struct omapfb_info *ofbi = (struct omapfb_info *)vma->vm_private_data;
1063 atomic_inc(&ofbi->map_count);
1066 static void mmap_user_close(struct vm_area_struct *vma)
1068 struct omapfb_info *ofbi = (struct omapfb_info *)vma->vm_private_data;
1070 atomic_dec(&ofbi->map_count);
1073 static struct vm_operations_struct mmap_user_ops = {
1074 .open = mmap_user_open,
1075 .close = mmap_user_close,
1078 static int omapfb_mmap(struct fb_info *fbi, struct vm_area_struct *vma)
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;
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;
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;
1099 off += start;
1101 DBG("user mmap region start %lx, len %d, off %lx\n", start, len, off);
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;
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.
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)
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;
1128 enum omapfb_color_format mode = OMAPFB_COLOR_RGB24U; /* XXX */
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:
1142 if (fbdev->ctrl->setcolreg)
1143 r = fbdev->ctrl->setcolreg(regno, red, green, blue,
1144 transp, update_hw_pal);
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;
1156 if (regno < 0) {
1157 r = -EINVAL;
1158 break;
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;
1170 break;
1171 default:
1172 BUG();
1174 return r;
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)
1180 DBG("setcolreg\n");
1182 return _setcolreg(info, regno, red, green, blue, transp, 1);
1185 static int omapfb_setcmap(struct fb_cmap *cmap, struct fb_info *info)
1187 int count, index, r;
1188 u16 *red, *green, *blue, *transp;
1189 u16 trans = 0xffff;
1191 DBG("setcmap\n");
1193 red = cmap->red;
1194 green = cmap->green;
1195 blue = cmap->blue;
1196 transp = cmap->transp;
1197 index = cmap->start;
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;
1208 return 0;
1211 static int omapfb_blank(int blank, struct fb_info *fbi)
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;
1219 omapfb_lock(fbdev);
1221 switch (blank) {
1222 case FB_BLANK_UNBLANK:
1223 if (display->state != OMAP_DSS_DISPLAY_SUSPENDED)
1224 goto exit;
1226 if (display->driver->resume)
1227 r = display->driver->resume(display);
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;
1234 break;
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;
1245 if (display->driver->suspend)
1246 r = display->driver->suspend(display);
1248 break;
1250 default:
1251 r = -EINVAL;
1254 exit:
1255 omapfb_unlock(fbdev);
1257 if (r == 0 && do_update && display->driver->update) {
1258 u16 w, h;
1259 display->driver->get_resolution(display, &w, &h);
1261 r = display->driver->update(display, 0, 0, w, h);
1264 return r;
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)
1272 DBG("omapfb_write %d, %lu\n", count, (unsigned long)*ppos);
1273 /* XXX needed for VRFB */
1274 return count;
1276 #endif
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,*/
1296 static void omapfb_free_fbmem(struct fb_info *fbi)
1298 struct omapfb_info *ofbi = FB2OFB(fbi);
1299 struct omapfb2_device *fbdev = ofbi->fbdev;
1300 struct omapfb2_mem_region *rg;
1302 rg = &ofbi->region;
1304 if (rg->paddr)
1305 if (omap_vram_free(rg->paddr, rg->size))
1306 dev_err(fbdev->dev, "VRAM FREE failed\n");
1308 if (rg->vaddr)
1309 iounmap(rg->vaddr);
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;
1320 rg->vaddr = NULL;
1321 rg->paddr = 0;
1322 rg->alloc = 0;
1323 rg->size = 0;
1326 static void clear_fb_info(struct fb_info *fbi)
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));
1333 static int omapfb_free_all_fbmem(struct omapfb2_device *fbdev)
1335 int i;
1337 DBG("free all fbmem\n");
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);
1345 return 0;
1348 static int omapfb_alloc_fbmem(struct fb_info *fbi, unsigned long size,
1349 unsigned long paddr)
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;
1357 rg = &ofbi->region;
1358 memset(rg, 0, sizeof(*rg));
1360 size = PAGE_ALIGN(size);
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);
1371 if (r) {
1372 dev_err(fbdev->dev, "failed to allocate framebuffer\n");
1373 return -ENOMEM;
1376 if (ofbi->rotation_type != OMAP_DSS_ROT_VRFB) {
1377 vaddr = ioremap_wc(paddr, size);
1379 if (!vaddr) {
1380 dev_err(fbdev->dev, "failed to ioremap framebuffer\n");
1381 omap_vram_free(paddr, size);
1382 return -ENOMEM;
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;
1393 vaddr = NULL;
1396 rg->paddr = paddr;
1397 rg->vaddr = vaddr;
1398 rg->size = size;
1399 rg->alloc = 1;
1401 return 0;
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)
1408 struct omapfb_info *ofbi = FB2OFB(fbi);
1409 struct omapfb2_device *fbdev = ofbi->fbdev;
1410 struct omap_dss_device *display;
1411 int bytespp;
1413 display = fb2display(fbi);
1415 if (!display)
1416 return 0;
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;
1430 if (!size) {
1431 u16 w, h;
1433 display->driver->get_resolution(display, &w, &h);
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));
1439 DBG("adjusting fb mem size for VRFB, %u -> %lu\n",
1440 w * h * bytespp, size);
1441 } else {
1442 size = w * h * bytespp;
1446 if (!size)
1447 return 0;
1449 return omapfb_alloc_fbmem(fbi, size, paddr);
1452 static enum omap_color_mode fb_format_to_dss_mode(enum omapfb_color_format fmt)
1454 enum omap_color_mode mode;
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;
1503 return mode;
1506 static int omapfb_parse_vram_param(const char *param, int max_entries,
1507 unsigned long *sizes, unsigned long *paddrs)
1509 int fbnum;
1510 unsigned long size;
1511 unsigned long paddr = 0;
1512 char *p, *start;
1514 start = (char *)param;
1516 while (1) {
1517 p = start;
1519 fbnum = simple_strtoul(p, &p, 10);
1521 if (p == param)
1522 return -EINVAL;
1524 if (*p != ':')
1525 return -EINVAL;
1527 if (fbnum >= max_entries)
1528 return -EINVAL;
1530 size = memparse(p + 1, &p);
1532 if (!size)
1533 return -EINVAL;
1535 paddr = 0;
1537 if (*p == '@') {
1538 paddr = simple_strtoul(p + 1, &p, 16);
1540 if (!paddr)
1541 return -EINVAL;
1545 paddrs[fbnum] = paddr;
1546 sizes[fbnum] = size;
1548 if (*p == 0)
1549 break;
1551 if (*p != ',')
1552 return -EINVAL;
1554 ++p;
1556 start = p;
1559 return 0;
1562 static int omapfb_allocate_all_fbs(struct omapfb2_device *fbdev)
1564 int i, r;
1565 unsigned long vram_sizes[10];
1566 unsigned long vram_paddrs[10];
1568 memset(&vram_sizes, 0, sizeof(vram_sizes));
1569 memset(&vram_paddrs, 0, sizeof(vram_paddrs));
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");
1575 memset(&vram_sizes, 0, sizeof(vram_sizes));
1576 memset(&vram_paddrs, 0, sizeof(vram_paddrs));
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;
1587 size = opd->mem_desc.region[i].size;
1588 paddr = opd->mem_desc.region[i].paddr;
1590 vram_sizes[i] = size;
1591 vram_paddrs[i] = paddr;
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]);
1603 if (r)
1604 return r;
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;
1613 DBG("region%d phys %08x virt %p size=%lu\n",
1615 rg->paddr,
1616 rg->vaddr,
1617 rg->size);
1620 return 0;
1623 int omapfb_realloc_fbmem(struct fb_info *fbi, unsigned long size, int type)
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;
1634 if (type > OMAPFB_MEMTYPE_MAX)
1635 return -EINVAL;
1637 size = PAGE_ALIGN(size);
1639 if (old_size == size && old_type == type)
1640 return 0;
1642 if (display && display->driver->sync)
1643 display->driver->sync(display);
1645 omapfb_free_fbmem(fbi);
1647 if (size == 0) {
1648 clear_fb_info(fbi);
1649 return 0;
1652 r = omapfb_alloc_fbmem(fbi, size, 0);
1654 if (r) {
1655 if (old_size)
1656 omapfb_alloc_fbmem(fbi, old_size, old_paddr);
1658 if (rg->size == 0)
1659 clear_fb_info(fbi);
1661 return r;
1664 if (old_size == size)
1665 return 0;
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;
1674 r = omapfb_apply_changes(fbi, 1);
1675 if (r) {
1676 DBG("omapfb_apply_changes failed\n");
1677 goto err;
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;
1692 return 0;
1693 err:
1694 omapfb_free_fbmem(fbi);
1695 clear_fb_info(fbi);
1696 return r;
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)
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;
1707 fbi->fbops = &omapfb_ops;
1708 fbi->flags = FBINFO_FLAG_DEFAULT;
1709 fbi->pseudo_palette = fbdev->pseudo_palette;
1711 if (ofbi->region.size == 0) {
1712 clear_fb_info(fbi);
1713 return 0;
1716 var->nonstd = 0;
1717 var->bits_per_pixel = 0;
1719 var->rotate = def_rotate;
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.
1726 if (fbdev->dev->platform_data) {
1727 struct omapfb_platform_data *opd;
1728 int id = ofbi->id;
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;
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;
1741 r = dss_mode_to_fb_mode(mode, var);
1742 if (r < 0)
1743 goto err;
1747 if (display) {
1748 u16 w, h;
1749 int rotation = (var->rotate + ofbi->rotation[0]) % 4;
1751 display->driver->get_resolution(display, &w, &h);
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;
1762 var->xres_virtual = var->xres;
1763 var->yres_virtual = var->yres;
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;
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;
1789 r = check_fb_var(fbi, var);
1790 if (r)
1791 goto err;
1793 set_fb_fix(fbi);
1794 r = setup_vrfb_rotation(fbi);
1795 if (r)
1796 goto err;
1798 r = fb_alloc_cmap(&fbi->cmap, 256, 0);
1799 if (r)
1800 dev_err(fbdev->dev, "unable to allocate color map memory\n");
1802 err:
1803 return r;
1806 static void fbinfo_cleanup(struct omapfb2_device *fbdev, struct fb_info *fbi)
1808 fb_dealloc_cmap(&fbi->cmap);
1812 static void omapfb_free_resources(struct omapfb2_device *fbdev)
1814 int i;
1816 DBG("free_resources\n");
1818 if (fbdev == NULL)
1819 return;
1821 for (i = 0; i < fbdev->num_fbs; i++)
1822 unregister_framebuffer(fbdev->fbs[i]);
1824 /* free the reserved fbmem */
1825 omapfb_free_all_fbmem(fbdev);
1827 for (i = 0; i < fbdev->num_fbs; i++) {
1828 fbinfo_cleanup(fbdev, fbdev->fbs[i]);
1829 framebuffer_release(fbdev->fbs[i]);
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]);
1836 omap_dss_put_device(fbdev->displays[i]);
1839 dev_set_drvdata(fbdev->dev, NULL);
1840 kfree(fbdev);
1843 static int omapfb_create_framebuffers(struct omapfb2_device *fbdev)
1845 int r, i;
1847 fbdev->num_fbs = 0;
1849 DBG("create %d framebuffers\n", CONFIG_FB_OMAP2_NUM_FBS);
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;
1856 fbi = framebuffer_alloc(sizeof(struct omapfb_info),
1857 fbdev->dev);
1859 if (fbi == NULL) {
1860 dev_err(fbdev->dev,
1861 "unable to allocate memory for plane info\n");
1862 return -ENOMEM;
1865 clear_fb_info(fbi);
1867 fbdev->fbs[i] = fbi;
1869 ofbi = FB2OFB(fbi);
1870 ofbi->fbdev = fbdev;
1871 ofbi->id = i;
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;
1878 fbdev->num_fbs++;
1881 DBG("fb_infos allocated\n");
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]);
1887 ofbi->overlays[0] = fbdev->overlays[i];
1888 ofbi->num_overlays = 1;
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;
1898 DBG("fbmems allocated\n");
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;
1909 DBG("fb_infos initialized\n");
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;
1920 DBG("framebuffers registered\n");
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;
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;
1937 /* Enable fb0 */
1938 if (fbdev->num_fbs > 0) {
1939 struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[0]);
1941 if (ofbi->num_overlays > 0) {
1942 struct omap_overlay *ovl = ofbi->overlays[0];
1944 r = omapfb_overlay_enable(ovl, 1);
1946 if (r) {
1947 dev_err(fbdev->dev,
1948 "failed to enable overlay\n");
1949 return r;
1954 DBG("create_framebuffers done\n");
1956 return 0;
1959 static int omapfb_mode_to_timings(const char *mode_str,
1960 struct omap_video_timings *timings, u8 *bpp)
1962 struct fb_info fbi;
1963 struct fb_var_screeninfo var;
1964 struct fb_ops fbops;
1965 int r;
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;
1977 #endif
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 */
1982 memset(&fbi, 0, sizeof(fbi));
1983 memset(&var, 0, sizeof(var));
1984 memset(&fbops, 0, sizeof(fbops));
1985 fbi.fbops = &fbops;
1987 r = fb_find_mode(&var, &fbi, mode_str, NULL, 0, NULL, 24);
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;
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;
2011 return 0;
2012 } else {
2013 return -EINVAL;
2017 static int omapfb_set_def_mode(struct omapfb2_device *fbdev,
2018 struct omap_dss_device *display, char *mode_str)
2020 int r;
2021 u8 bpp;
2022 struct omap_video_timings timings;
2024 r = omapfb_mode_to_timings(mode_str, &timings, &bpp);
2025 if (r)
2026 return r;
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;
2032 if (!display->driver->check_timings || !display->driver->set_timings)
2033 return -EINVAL;
2035 r = display->driver->check_timings(display, &timings);
2036 if (r)
2037 return r;
2039 display->driver->set_timings(display, &timings);
2041 return 0;
2044 static int omapfb_get_recommended_bpp(struct omapfb2_device *fbdev,
2045 struct omap_dss_device *dssdev)
2047 int i;
2049 BUG_ON(dssdev->driver->get_recommended_bpp == NULL);
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;
2056 return dssdev->driver->get_recommended_bpp(dssdev);
2059 static int omapfb_parse_def_modes(struct omapfb2_device *fbdev)
2061 char *str, *options, *this_opt;
2062 int r = 0;
2064 str = kmalloc(strlen(def_mode) + 1, GFP_KERNEL);
2065 strcpy(str, def_mode);
2066 options = str;
2068 while (!r && (this_opt = strsep(&options, ",")) != NULL) {
2069 char *p, *display_str, *mode_str;
2070 struct omap_dss_device *display;
2071 int i;
2073 p = strchr(this_opt, ':');
2074 if (!p) {
2075 r = -EINVAL;
2076 break;
2079 *p = 0;
2080 display_str = this_opt;
2081 mode_str = p + 1;
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;
2092 if (!display) {
2093 r = -EINVAL;
2094 break;
2097 r = omapfb_set_def_mode(fbdev, display, mode_str);
2098 if (r)
2099 break;
2102 kfree(str);
2104 return r;
2107 static int omapfb_probe(struct platform_device *pdev)
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;
2116 DBG("omapfb_probe\n");
2118 if (pdev->num_resources != 0) {
2119 dev_err(&pdev->dev, "probed for an unknown device\n");
2120 r = -ENODEV;
2121 goto err0;
2124 fbdev = kzalloc(sizeof(struct omapfb2_device), GFP_KERNEL);
2125 if (fbdev == NULL) {
2126 r = -ENOMEM;
2127 goto err0;
2130 mutex_init(&fbdev->mtx);
2132 fbdev->dev = &pdev->dev;
2133 platform_set_drvdata(pdev, fbdev);
2135 r = 0;
2136 fbdev->num_displays = 0;
2137 dssdev = NULL;
2138 for_each_dss_dev(dssdev) {
2139 omap_dss_get_device(dssdev);
2141 if (!dssdev->driver) {
2142 dev_err(&pdev->dev, "no driver for display\n");
2143 r = -ENODEV;
2146 fbdev->displays[fbdev->num_displays++] = dssdev;
2149 if (r)
2150 goto cleanup;
2152 if (fbdev->num_displays == 0) {
2153 dev_err(&pdev->dev, "no displays\n");
2154 r = -EINVAL;
2155 goto cleanup;
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);
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);
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");
2171 r = omapfb_create_framebuffers(fbdev);
2172 if (r)
2173 goto cleanup;
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");
2183 DBG("mgr->apply'ed\n");
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;
2195 if (def_display) {
2196 struct omap_dss_driver *dssdrv = def_display->driver;
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;
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);
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);
2222 return 0;
2224 cleanup:
2225 omapfb_free_resources(fbdev);
2226 err0:
2227 dev_err(&pdev->dev, "failed to setup omapfb\n");
2228 return r;
2231 static int omapfb_remove(struct platform_device *pdev)
2233 struct omapfb2_device *fbdev = platform_get_drvdata(pdev);
2235 /* FIXME: wait till completion of pending events */
2237 omapfb_remove_sysfs(fbdev);
2239 omapfb_free_resources(fbdev);
2241 return 0;
2244 static struct platform_driver omapfb_driver = {
2245 .probe = omapfb_probe,
2246 .remove = omapfb_remove,
2247 .driver = {
2248 .name = "omapfb",
2249 .owner = THIS_MODULE,
2253 static int __init omapfb_init(void)
2255 DBG("omapfb_init\n");
2257 if (platform_driver_register(&omapfb_driver)) {
2258 printk(KERN_ERR "failed to register omapfb driver\n");
2259 return -ENODEV;
2262 return 0;
2265 static void __exit omapfb_exit(void)
2267 DBG("omapfb_exit\n");
2268 platform_driver_unregister(&omapfb_driver);
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);
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);
2284 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@nokia.com>");
2285 MODULE_DESCRIPTION("OMAP2/3 Framebuffer");
2286 MODULE_LICENSE("GPL v2");