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