blob: 505ec667204906130eb4457e6d96d75b885f670b [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
33#include <plat/display.h>
34#include <plat/vram.h>
35#include <plat/vrfb.h>
36
37#include "omapfb.h"
38
39#define MODULE_NAME "omapfb"
40
41#define OMAPFB_PLANE_XRES_MIN 8
42#define OMAPFB_PLANE_YRES_MIN 8
43
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
705 var->height = -1;
706 var->width = -1;
707 var->grayscale = 0;
708
Tomi Valkeinen69b20482010-01-20 12:11:25 +0200709 if (display && display->driver->get_timings) {
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300710 struct omap_video_timings timings;
Tomi Valkeinen69b20482010-01-20 12:11:25 +0200711 display->driver->get_timings(display, &timings);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300712
713 /* pixclock in ps, the rest in pixclock */
714 var->pixclock = timings.pixel_clock != 0 ?
715 KHZ2PICOS(timings.pixel_clock) :
716 0;
Tasslehoff Kjappfot87ba8282010-09-08 12:46:14 +0200717 var->left_margin = timings.hbp;
718 var->right_margin = timings.hfp;
719 var->upper_margin = timings.vbp;
720 var->lower_margin = timings.vfp;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300721 var->hsync_len = timings.hsw;
722 var->vsync_len = timings.vsw;
723 } else {
724 var->pixclock = 0;
725 var->left_margin = 0;
726 var->right_margin = 0;
727 var->upper_margin = 0;
728 var->lower_margin = 0;
729 var->hsync_len = 0;
730 var->vsync_len = 0;
731 }
732
733 /* TODO: get these from panel->config */
734 var->vmode = FB_VMODE_NONINTERLACED;
735 var->sync = 0;
736
737 return 0;
738}
739
740/*
741 * ---------------------------------------------------------------------------
742 * fbdev framework callbacks
743 * ---------------------------------------------------------------------------
744 */
745static int omapfb_open(struct fb_info *fbi, int user)
746{
747 return 0;
748}
749
750static int omapfb_release(struct fb_info *fbi, int user)
751{
752#if 0
753 struct omapfb_info *ofbi = FB2OFB(fbi);
754 struct omapfb2_device *fbdev = ofbi->fbdev;
755 struct omap_dss_device *display = fb2display(fbi);
756
757 DBG("Closing fb with plane index %d\n", ofbi->id);
758
759 omapfb_lock(fbdev);
760
761 if (display && display->get_update_mode && display->update) {
762 /* XXX this update should be removed, I think. But it's
763 * good for debugging */
764 if (display->get_update_mode(display) ==
765 OMAP_DSS_UPDATE_MANUAL) {
766 u16 w, h;
767
768 if (display->sync)
769 display->sync(display);
770
771 display->get_resolution(display, &w, &h);
772 display->update(display, 0, 0, w, h);
773 }
774 }
775
776 if (display && display->sync)
777 display->sync(display);
778
779 omapfb_unlock(fbdev);
780#endif
781 return 0;
782}
783
Ville Syrjäläa4c1a142010-02-23 23:36:26 +0100784static unsigned calc_rotation_offset_dma(const struct fb_var_screeninfo *var,
785 const struct fb_fix_screeninfo *fix, int rotation)
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300786{
787 unsigned offset;
788
789 offset = var->yoffset * fix->line_length +
790 var->xoffset * (var->bits_per_pixel >> 3);
791
792 return offset;
793}
794
Ville Syrjäläa4c1a142010-02-23 23:36:26 +0100795static unsigned calc_rotation_offset_vrfb(const struct fb_var_screeninfo *var,
796 const struct fb_fix_screeninfo *fix, int rotation)
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300797{
798 unsigned offset;
799
800 if (rotation == FB_ROTATE_UD)
801 offset = (var->yres_virtual - var->yres) *
802 fix->line_length;
803 else if (rotation == FB_ROTATE_CW)
804 offset = (var->yres_virtual - var->yres) *
805 (var->bits_per_pixel >> 3);
806 else
807 offset = 0;
808
809 if (rotation == FB_ROTATE_UR)
810 offset += var->yoffset * fix->line_length +
811 var->xoffset * (var->bits_per_pixel >> 3);
812 else if (rotation == FB_ROTATE_UD)
813 offset -= var->yoffset * fix->line_length +
814 var->xoffset * (var->bits_per_pixel >> 3);
815 else if (rotation == FB_ROTATE_CW)
816 offset -= var->xoffset * fix->line_length +
817 var->yoffset * (var->bits_per_pixel >> 3);
818 else if (rotation == FB_ROTATE_CCW)
819 offset += var->xoffset * fix->line_length +
820 var->yoffset * (var->bits_per_pixel >> 3);
821
822 return offset;
823}
824
Ville Syrjälä46d35242010-03-17 19:59:26 +0200825static void omapfb_calc_addr(const struct omapfb_info *ofbi,
826 const struct fb_var_screeninfo *var,
827 const struct fb_fix_screeninfo *fix,
828 int rotation, u32 *paddr, void __iomem **vaddr)
829{
830 u32 data_start_p;
831 void __iomem *data_start_v;
832 int offset;
833
834 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
835 data_start_p = omapfb_get_region_rot_paddr(ofbi, rotation);
836 data_start_v = NULL;
837 } else {
838 data_start_p = omapfb_get_region_paddr(ofbi);
839 data_start_v = omapfb_get_region_vaddr(ofbi);
840 }
841
842 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
843 offset = calc_rotation_offset_vrfb(var, fix, rotation);
844 else
845 offset = calc_rotation_offset_dma(var, fix, rotation);
846
847 data_start_p += offset;
848 data_start_v += offset;
849
850 if (offset)
851 DBG("offset %d, %d = %d\n",
852 var->xoffset, var->yoffset, offset);
853
854 DBG("paddr %x, vaddr %p\n", data_start_p, data_start_v);
855
856 *paddr = data_start_p;
857 *vaddr = data_start_v;
858}
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300859
860/* setup overlay according to the fb */
Ville Syrjälä078ff542010-03-17 20:36:51 +0200861int omapfb_setup_overlay(struct fb_info *fbi, struct omap_overlay *ovl,
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300862 u16 posx, u16 posy, u16 outw, u16 outh)
863{
864 int r = 0;
865 struct omapfb_info *ofbi = FB2OFB(fbi);
866 struct fb_var_screeninfo *var = &fbi->var;
867 struct fb_fix_screeninfo *fix = &fbi->fix;
868 enum omap_color_mode mode = 0;
Ville Syrjälä46d35242010-03-17 19:59:26 +0200869 u32 data_start_p = 0;
870 void __iomem *data_start_v = NULL;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300871 struct omap_overlay_info info;
872 int xres, yres;
873 int screen_width;
874 int mirror;
875 int rotation = var->rotate;
876 int i;
877
Ville Syrjälä1ceafc02010-03-17 21:28:50 +0200878 WARN_ON(!atomic_read(&ofbi->region->lock_count));
879
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300880 for (i = 0; i < ofbi->num_overlays; i++) {
881 if (ovl != ofbi->overlays[i])
882 continue;
883
884 rotation = (rotation + ofbi->rotation[i]) % 4;
885 break;
886 }
887
888 DBG("setup_overlay %d, posx %d, posy %d, outw %d, outh %d\n", ofbi->id,
889 posx, posy, outw, outh);
890
891 if (rotation == FB_ROTATE_CW || rotation == FB_ROTATE_CCW) {
892 xres = var->yres;
893 yres = var->xres;
894 } else {
895 xres = var->xres;
896 yres = var->yres;
897 }
898
Ville Syrjälä078ff542010-03-17 20:36:51 +0200899 if (ofbi->region->size)
Ville Syrjälä276a1d42010-03-17 20:05:38 +0200900 omapfb_calc_addr(ofbi, var, fix, rotation,
901 &data_start_p, &data_start_v);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300902
903 r = fb_mode_to_dss_mode(var, &mode);
904 if (r) {
905 DBG("fb_mode_to_dss_mode failed");
906 goto err;
907 }
908
909 switch (var->nonstd) {
910 case OMAPFB_COLOR_YUV422:
911 case OMAPFB_COLOR_YUY422:
912 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
913 screen_width = fix->line_length
914 / (var->bits_per_pixel >> 2);
915 break;
916 }
917 default:
918 screen_width = fix->line_length / (var->bits_per_pixel >> 3);
919 break;
920 }
921
922 ovl->get_overlay_info(ovl, &info);
923
924 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
925 mirror = 0;
926 else
927 mirror = ofbi->mirror;
928
929 info.paddr = data_start_p;
930 info.vaddr = data_start_v;
931 info.screen_width = screen_width;
932 info.width = xres;
933 info.height = yres;
934 info.color_mode = mode;
935 info.rotation_type = ofbi->rotation_type;
936 info.rotation = rotation;
937 info.mirror = mirror;
938
939 info.pos_x = posx;
940 info.pos_y = posy;
941 info.out_width = outw;
942 info.out_height = outh;
943
944 r = ovl->set_overlay_info(ovl, &info);
945 if (r) {
946 DBG("ovl->setup_overlay_info failed\n");
947 goto err;
948 }
949
950 return 0;
951
952err:
953 DBG("setup_overlay failed\n");
954 return r;
955}
956
957/* apply var to the overlay */
958int omapfb_apply_changes(struct fb_info *fbi, int init)
959{
960 int r = 0;
961 struct omapfb_info *ofbi = FB2OFB(fbi);
962 struct fb_var_screeninfo *var = &fbi->var;
963 struct omap_overlay *ovl;
964 u16 posx, posy;
965 u16 outw, outh;
966 int i;
967
968#ifdef DEBUG
969 if (omapfb_test_pattern)
970 fill_fb(fbi);
971#endif
972
Ville Syrjälä1ceafc02010-03-17 21:28:50 +0200973 WARN_ON(!atomic_read(&ofbi->region->lock_count));
974
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300975 for (i = 0; i < ofbi->num_overlays; i++) {
976 ovl = ofbi->overlays[i];
977
978 DBG("apply_changes, fb %d, ovl %d\n", ofbi->id, ovl->id);
979
Ville Syrjälä078ff542010-03-17 20:36:51 +0200980 if (ofbi->region->size == 0) {
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300981 /* the fb is not available. disable the overlay */
982 omapfb_overlay_enable(ovl, 0);
983 if (!init && ovl->manager)
984 ovl->manager->apply(ovl->manager);
985 continue;
986 }
987
988 if (init || (ovl->caps & OMAP_DSS_OVL_CAP_SCALE) == 0) {
989 int rotation = (var->rotate + ofbi->rotation[i]) % 4;
990 if (rotation == FB_ROTATE_CW ||
991 rotation == FB_ROTATE_CCW) {
992 outw = var->yres;
993 outh = var->xres;
994 } else {
995 outw = var->xres;
996 outh = var->yres;
997 }
998 } else {
999 outw = ovl->info.out_width;
1000 outh = ovl->info.out_height;
1001 }
1002
1003 if (init) {
1004 posx = 0;
1005 posy = 0;
1006 } else {
1007 posx = ovl->info.pos_x;
1008 posy = ovl->info.pos_y;
1009 }
1010
1011 r = omapfb_setup_overlay(fbi, ovl, posx, posy, outw, outh);
1012 if (r)
1013 goto err;
1014
1015 if (!init && ovl->manager)
1016 ovl->manager->apply(ovl->manager);
1017 }
1018 return 0;
1019err:
1020 DBG("apply_changes failed\n");
1021 return r;
1022}
1023
1024/* checks var and eventually tweaks it to something supported,
1025 * DO NOT MODIFY PAR */
1026static int omapfb_check_var(struct fb_var_screeninfo *var, struct fb_info *fbi)
1027{
Ville Syrjälä430571d2010-03-17 20:43:23 +02001028 struct omapfb_info *ofbi = FB2OFB(fbi);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001029 int r;
1030
1031 DBG("check_var(%d)\n", FB2OFB(fbi)->id);
1032
Ville Syrjälä430571d2010-03-17 20:43:23 +02001033 omapfb_get_mem_region(ofbi->region);
1034
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001035 r = check_fb_var(fbi, var);
1036
Ville Syrjälä430571d2010-03-17 20:43:23 +02001037 omapfb_put_mem_region(ofbi->region);
1038
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001039 return r;
1040}
1041
1042/* set the video mode according to info->var */
1043static int omapfb_set_par(struct fb_info *fbi)
1044{
Ville Syrjälä430571d2010-03-17 20:43:23 +02001045 struct omapfb_info *ofbi = FB2OFB(fbi);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001046 int r;
1047
1048 DBG("set_par(%d)\n", FB2OFB(fbi)->id);
1049
Ville Syrjälä430571d2010-03-17 20:43:23 +02001050 omapfb_get_mem_region(ofbi->region);
1051
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001052 set_fb_fix(fbi);
1053
1054 r = setup_vrfb_rotation(fbi);
1055 if (r)
Ville Syrjälä430571d2010-03-17 20:43:23 +02001056 goto out;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001057
1058 r = omapfb_apply_changes(fbi, 0);
1059
Ville Syrjälä430571d2010-03-17 20:43:23 +02001060 out:
1061 omapfb_put_mem_region(ofbi->region);
1062
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001063 return r;
1064}
1065
1066static int omapfb_pan_display(struct fb_var_screeninfo *var,
1067 struct fb_info *fbi)
1068{
Ville Syrjälä430571d2010-03-17 20:43:23 +02001069 struct omapfb_info *ofbi = FB2OFB(fbi);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001070 struct fb_var_screeninfo new_var;
1071 int r;
1072
1073 DBG("pan_display(%d)\n", FB2OFB(fbi)->id);
1074
1075 if (var->xoffset == fbi->var.xoffset &&
1076 var->yoffset == fbi->var.yoffset)
1077 return 0;
1078
1079 new_var = fbi->var;
1080 new_var.xoffset = var->xoffset;
1081 new_var.yoffset = var->yoffset;
1082
1083 fbi->var = new_var;
1084
Ville Syrjälä430571d2010-03-17 20:43:23 +02001085 omapfb_get_mem_region(ofbi->region);
1086
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001087 r = omapfb_apply_changes(fbi, 0);
1088
Ville Syrjälä430571d2010-03-17 20:43:23 +02001089 omapfb_put_mem_region(ofbi->region);
1090
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001091 return r;
1092}
1093
1094static void mmap_user_open(struct vm_area_struct *vma)
1095{
Ville Syrjälä078ff542010-03-17 20:36:51 +02001096 struct omapfb2_mem_region *rg = vma->vm_private_data;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001097
Ville Syrjälä430571d2010-03-17 20:43:23 +02001098 omapfb_get_mem_region(rg);
Ville Syrjälä078ff542010-03-17 20:36:51 +02001099 atomic_inc(&rg->map_count);
Ville Syrjälä430571d2010-03-17 20:43:23 +02001100 omapfb_put_mem_region(rg);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001101}
1102
1103static void mmap_user_close(struct vm_area_struct *vma)
1104{
Ville Syrjälä078ff542010-03-17 20:36:51 +02001105 struct omapfb2_mem_region *rg = vma->vm_private_data;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001106
Ville Syrjälä430571d2010-03-17 20:43:23 +02001107 omapfb_get_mem_region(rg);
Ville Syrjälä078ff542010-03-17 20:36:51 +02001108 atomic_dec(&rg->map_count);
Ville Syrjälä430571d2010-03-17 20:43:23 +02001109 omapfb_put_mem_region(rg);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001110}
1111
1112static struct vm_operations_struct mmap_user_ops = {
1113 .open = mmap_user_open,
1114 .close = mmap_user_close,
1115};
1116
1117static int omapfb_mmap(struct fb_info *fbi, struct vm_area_struct *vma)
1118{
1119 struct omapfb_info *ofbi = FB2OFB(fbi);
1120 struct fb_fix_screeninfo *fix = &fbi->fix;
Ville Syrjälä078ff542010-03-17 20:36:51 +02001121 struct omapfb2_mem_region *rg;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001122 unsigned long off;
1123 unsigned long start;
1124 u32 len;
Ville Syrjälä430571d2010-03-17 20:43:23 +02001125 int r = -EINVAL;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001126
1127 if (vma->vm_end - vma->vm_start == 0)
1128 return 0;
1129 if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
1130 return -EINVAL;
1131 off = vma->vm_pgoff << PAGE_SHIFT;
1132
Ville Syrjälä430571d2010-03-17 20:43:23 +02001133 rg = omapfb_get_mem_region(ofbi->region);
Ville Syrjälä078ff542010-03-17 20:36:51 +02001134
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001135 start = omapfb_get_region_paddr(ofbi);
1136 len = fix->smem_len;
1137 if (off >= len)
Ville Syrjälä430571d2010-03-17 20:43:23 +02001138 goto error;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001139 if ((vma->vm_end - vma->vm_start + off) > len)
Ville Syrjälä430571d2010-03-17 20:43:23 +02001140 goto error;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001141
1142 off += start;
1143
1144 DBG("user mmap region start %lx, len %d, off %lx\n", start, len, off);
1145
1146 vma->vm_pgoff = off >> PAGE_SHIFT;
1147 vma->vm_flags |= VM_IO | VM_RESERVED;
1148 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
1149 vma->vm_ops = &mmap_user_ops;
Ville Syrjälä078ff542010-03-17 20:36:51 +02001150 vma->vm_private_data = rg;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001151 if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
Ville Syrjälä430571d2010-03-17 20:43:23 +02001152 vma->vm_end - vma->vm_start,
1153 vma->vm_page_prot)) {
1154 r = -EAGAIN;
1155 goto error;
1156 }
1157
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001158 /* vm_ops.open won't be called for mmap itself. */
Ville Syrjälä078ff542010-03-17 20:36:51 +02001159 atomic_inc(&rg->map_count);
Ville Syrjälä430571d2010-03-17 20:43:23 +02001160
1161 omapfb_put_mem_region(rg);
1162
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001163 return 0;
Ville Syrjälä430571d2010-03-17 20:43:23 +02001164
1165 error:
1166 omapfb_put_mem_region(ofbi->region);
1167
1168 return r;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001169}
1170
1171/* Store a single color palette entry into a pseudo palette or the hardware
1172 * palette if one is available. For now we support only 16bpp and thus store
1173 * the entry only to the pseudo palette.
1174 */
1175static int _setcolreg(struct fb_info *fbi, u_int regno, u_int red, u_int green,
1176 u_int blue, u_int transp, int update_hw_pal)
1177{
1178 /*struct omapfb_info *ofbi = FB2OFB(fbi);*/
1179 /*struct omapfb2_device *fbdev = ofbi->fbdev;*/
1180 struct fb_var_screeninfo *var = &fbi->var;
1181 int r = 0;
1182
1183 enum omapfb_color_format mode = OMAPFB_COLOR_RGB24U; /* XXX */
1184
1185 /*switch (plane->color_mode) {*/
1186 switch (mode) {
1187 case OMAPFB_COLOR_YUV422:
1188 case OMAPFB_COLOR_YUV420:
1189 case OMAPFB_COLOR_YUY422:
1190 r = -EINVAL;
1191 break;
1192 case OMAPFB_COLOR_CLUT_8BPP:
1193 case OMAPFB_COLOR_CLUT_4BPP:
1194 case OMAPFB_COLOR_CLUT_2BPP:
1195 case OMAPFB_COLOR_CLUT_1BPP:
1196 /*
1197 if (fbdev->ctrl->setcolreg)
1198 r = fbdev->ctrl->setcolreg(regno, red, green, blue,
1199 transp, update_hw_pal);
1200 */
1201 /* Fallthrough */
1202 r = -EINVAL;
1203 break;
1204 case OMAPFB_COLOR_RGB565:
1205 case OMAPFB_COLOR_RGB444:
1206 case OMAPFB_COLOR_RGB24P:
1207 case OMAPFB_COLOR_RGB24U:
1208 if (r != 0)
1209 break;
1210
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001211 if (regno < 16) {
1212 u16 pal;
1213 pal = ((red >> (16 - var->red.length)) <<
1214 var->red.offset) |
1215 ((green >> (16 - var->green.length)) <<
1216 var->green.offset) |
1217 (blue >> (16 - var->blue.length));
1218 ((u32 *)(fbi->pseudo_palette))[regno] = pal;
1219 }
1220 break;
1221 default:
1222 BUG();
1223 }
1224 return r;
1225}
1226
1227static int omapfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
1228 u_int transp, struct fb_info *info)
1229{
1230 DBG("setcolreg\n");
1231
1232 return _setcolreg(info, regno, red, green, blue, transp, 1);
1233}
1234
1235static int omapfb_setcmap(struct fb_cmap *cmap, struct fb_info *info)
1236{
1237 int count, index, r;
1238 u16 *red, *green, *blue, *transp;
1239 u16 trans = 0xffff;
1240
1241 DBG("setcmap\n");
1242
1243 red = cmap->red;
1244 green = cmap->green;
1245 blue = cmap->blue;
1246 transp = cmap->transp;
1247 index = cmap->start;
1248
1249 for (count = 0; count < cmap->len; count++) {
1250 if (transp)
1251 trans = *transp++;
1252 r = _setcolreg(info, index++, *red++, *green++, *blue++, trans,
1253 count == cmap->len - 1);
1254 if (r != 0)
1255 return r;
1256 }
1257
1258 return 0;
1259}
1260
1261static int omapfb_blank(int blank, struct fb_info *fbi)
1262{
1263 struct omapfb_info *ofbi = FB2OFB(fbi);
1264 struct omapfb2_device *fbdev = ofbi->fbdev;
1265 struct omap_dss_device *display = fb2display(fbi);
1266 int do_update = 0;
1267 int r = 0;
1268
Jani Nikula93255882010-06-01 15:12:12 +03001269 if (!display)
1270 return -EINVAL;
1271
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001272 omapfb_lock(fbdev);
1273
1274 switch (blank) {
1275 case FB_BLANK_UNBLANK:
1276 if (display->state != OMAP_DSS_DISPLAY_SUSPENDED)
1277 goto exit;
1278
Tomi Valkeinen37ac60e2010-01-12 15:12:07 +02001279 if (display->driver->resume)
1280 r = display->driver->resume(display);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001281
Tomi Valkeinen446f7bf2010-01-11 16:12:31 +02001282 if (r == 0 && display->driver->get_update_mode &&
1283 display->driver->get_update_mode(display) ==
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001284 OMAP_DSS_UPDATE_MANUAL)
1285 do_update = 1;
1286
1287 break;
1288
1289 case FB_BLANK_NORMAL:
1290 /* FB_BLANK_NORMAL could be implemented.
1291 * Needs DSS additions. */
1292 case FB_BLANK_VSYNC_SUSPEND:
1293 case FB_BLANK_HSYNC_SUSPEND:
1294 case FB_BLANK_POWERDOWN:
1295 if (display->state != OMAP_DSS_DISPLAY_ACTIVE)
1296 goto exit;
1297
Tomi Valkeinen37ac60e2010-01-12 15:12:07 +02001298 if (display->driver->suspend)
1299 r = display->driver->suspend(display);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001300
1301 break;
1302
1303 default:
1304 r = -EINVAL;
1305 }
1306
1307exit:
1308 omapfb_unlock(fbdev);
1309
Tomi Valkeinen18946f62010-01-12 14:16:41 +02001310 if (r == 0 && do_update && display->driver->update) {
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001311 u16 w, h;
Tomi Valkeinen96adcec2010-01-11 13:54:33 +02001312 display->driver->get_resolution(display, &w, &h);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001313
Tomi Valkeinen18946f62010-01-12 14:16:41 +02001314 r = display->driver->update(display, 0, 0, w, h);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001315 }
1316
1317 return r;
1318}
1319
1320#if 0
1321/* XXX fb_read and fb_write are needed for VRFB */
1322ssize_t omapfb_write(struct fb_info *info, const char __user *buf,
1323 size_t count, loff_t *ppos)
1324{
1325 DBG("omapfb_write %d, %lu\n", count, (unsigned long)*ppos);
1326 /* XXX needed for VRFB */
1327 return count;
1328}
1329#endif
1330
1331static struct fb_ops omapfb_ops = {
1332 .owner = THIS_MODULE,
1333 .fb_open = omapfb_open,
1334 .fb_release = omapfb_release,
1335 .fb_fillrect = cfb_fillrect,
1336 .fb_copyarea = cfb_copyarea,
1337 .fb_imageblit = cfb_imageblit,
1338 .fb_blank = omapfb_blank,
1339 .fb_ioctl = omapfb_ioctl,
1340 .fb_check_var = omapfb_check_var,
1341 .fb_set_par = omapfb_set_par,
1342 .fb_pan_display = omapfb_pan_display,
1343 .fb_mmap = omapfb_mmap,
1344 .fb_setcolreg = omapfb_setcolreg,
1345 .fb_setcmap = omapfb_setcmap,
1346 /*.fb_write = omapfb_write,*/
1347};
1348
1349static void omapfb_free_fbmem(struct fb_info *fbi)
1350{
1351 struct omapfb_info *ofbi = FB2OFB(fbi);
1352 struct omapfb2_device *fbdev = ofbi->fbdev;
1353 struct omapfb2_mem_region *rg;
1354
Ville Syrjälä078ff542010-03-17 20:36:51 +02001355 rg = ofbi->region;
1356
1357 WARN_ON(atomic_read(&rg->map_count));
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001358
1359 if (rg->paddr)
1360 if (omap_vram_free(rg->paddr, rg->size))
1361 dev_err(fbdev->dev, "VRAM FREE failed\n");
1362
1363 if (rg->vaddr)
1364 iounmap(rg->vaddr);
1365
1366 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
1367 /* unmap the 0 angle rotation */
1368 if (rg->vrfb.vaddr[0]) {
1369 iounmap(rg->vrfb.vaddr[0]);
1370 omap_vrfb_release_ctx(&rg->vrfb);
Tomi Valkeinenf3a82d12010-01-07 13:37:30 +02001371 rg->vrfb.vaddr[0] = NULL;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001372 }
1373 }
1374
1375 rg->vaddr = NULL;
1376 rg->paddr = 0;
1377 rg->alloc = 0;
1378 rg->size = 0;
1379}
1380
1381static void clear_fb_info(struct fb_info *fbi)
1382{
1383 memset(&fbi->var, 0, sizeof(fbi->var));
1384 memset(&fbi->fix, 0, sizeof(fbi->fix));
1385 strlcpy(fbi->fix.id, MODULE_NAME, sizeof(fbi->fix.id));
1386}
1387
1388static int omapfb_free_all_fbmem(struct omapfb2_device *fbdev)
1389{
1390 int i;
1391
1392 DBG("free all fbmem\n");
1393
1394 for (i = 0; i < fbdev->num_fbs; i++) {
1395 struct fb_info *fbi = fbdev->fbs[i];
1396 omapfb_free_fbmem(fbi);
1397 clear_fb_info(fbi);
1398 }
1399
1400 return 0;
1401}
1402
1403static int omapfb_alloc_fbmem(struct fb_info *fbi, unsigned long size,
1404 unsigned long paddr)
1405{
1406 struct omapfb_info *ofbi = FB2OFB(fbi);
1407 struct omapfb2_device *fbdev = ofbi->fbdev;
1408 struct omapfb2_mem_region *rg;
1409 void __iomem *vaddr;
1410 int r;
1411
Ville Syrjälä078ff542010-03-17 20:36:51 +02001412 rg = ofbi->region;
1413
1414 rg->paddr = 0;
1415 rg->vaddr = NULL;
1416 memset(&rg->vrfb, 0, sizeof rg->vrfb);
1417 rg->size = 0;
1418 rg->type = 0;
1419 rg->alloc = false;
1420 rg->map = false;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001421
1422 size = PAGE_ALIGN(size);
1423
1424 if (!paddr) {
1425 DBG("allocating %lu bytes for fb %d\n", size, ofbi->id);
1426 r = omap_vram_alloc(OMAP_VRAM_MEMTYPE_SDRAM, size, &paddr);
1427 } else {
1428 DBG("reserving %lu bytes at %lx for fb %d\n", size, paddr,
1429 ofbi->id);
1430 r = omap_vram_reserve(paddr, size);
1431 }
1432
1433 if (r) {
1434 dev_err(fbdev->dev, "failed to allocate framebuffer\n");
1435 return -ENOMEM;
1436 }
1437
1438 if (ofbi->rotation_type != OMAP_DSS_ROT_VRFB) {
1439 vaddr = ioremap_wc(paddr, size);
1440
1441 if (!vaddr) {
1442 dev_err(fbdev->dev, "failed to ioremap framebuffer\n");
1443 omap_vram_free(paddr, size);
1444 return -ENOMEM;
1445 }
1446
1447 DBG("allocated VRAM paddr %lx, vaddr %p\n", paddr, vaddr);
1448 } else {
1449 r = omap_vrfb_request_ctx(&rg->vrfb);
1450 if (r) {
1451 dev_err(fbdev->dev, "vrfb create ctx failed\n");
1452 return r;
1453 }
1454
1455 vaddr = NULL;
1456 }
1457
1458 rg->paddr = paddr;
1459 rg->vaddr = vaddr;
1460 rg->size = size;
1461 rg->alloc = 1;
1462
1463 return 0;
1464}
1465
1466/* allocate fbmem using display resolution as reference */
1467static int omapfb_alloc_fbmem_display(struct fb_info *fbi, unsigned long size,
1468 unsigned long paddr)
1469{
1470 struct omapfb_info *ofbi = FB2OFB(fbi);
Tomi Valkeinena2699502010-01-11 14:33:40 +02001471 struct omapfb2_device *fbdev = ofbi->fbdev;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001472 struct omap_dss_device *display;
1473 int bytespp;
1474
1475 display = fb2display(fbi);
1476
1477 if (!display)
1478 return 0;
1479
Tomi Valkeinena2699502010-01-11 14:33:40 +02001480 switch (omapfb_get_recommended_bpp(fbdev, display)) {
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001481 case 16:
1482 bytespp = 2;
1483 break;
1484 case 24:
1485 bytespp = 4;
1486 break;
1487 default:
1488 bytespp = 4;
1489 break;
1490 }
1491
1492 if (!size) {
1493 u16 w, h;
1494
Tomi Valkeinen96adcec2010-01-11 13:54:33 +02001495 display->driver->get_resolution(display, &w, &h);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001496
1497 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
1498 size = max(omap_vrfb_min_phys_size(w, h, bytespp),
1499 omap_vrfb_min_phys_size(h, w, bytespp));
1500
1501 DBG("adjusting fb mem size for VRFB, %u -> %lu\n",
1502 w * h * bytespp, size);
1503 } else {
1504 size = w * h * bytespp;
1505 }
1506 }
1507
1508 if (!size)
1509 return 0;
1510
1511 return omapfb_alloc_fbmem(fbi, size, paddr);
1512}
1513
1514static enum omap_color_mode fb_format_to_dss_mode(enum omapfb_color_format fmt)
1515{
1516 enum omap_color_mode mode;
1517
1518 switch (fmt) {
1519 case OMAPFB_COLOR_RGB565:
1520 mode = OMAP_DSS_COLOR_RGB16;
1521 break;
1522 case OMAPFB_COLOR_YUV422:
1523 mode = OMAP_DSS_COLOR_YUV2;
1524 break;
1525 case OMAPFB_COLOR_CLUT_8BPP:
1526 mode = OMAP_DSS_COLOR_CLUT8;
1527 break;
1528 case OMAPFB_COLOR_CLUT_4BPP:
1529 mode = OMAP_DSS_COLOR_CLUT4;
1530 break;
1531 case OMAPFB_COLOR_CLUT_2BPP:
1532 mode = OMAP_DSS_COLOR_CLUT2;
1533 break;
1534 case OMAPFB_COLOR_CLUT_1BPP:
1535 mode = OMAP_DSS_COLOR_CLUT1;
1536 break;
1537 case OMAPFB_COLOR_RGB444:
1538 mode = OMAP_DSS_COLOR_RGB12U;
1539 break;
1540 case OMAPFB_COLOR_YUY422:
1541 mode = OMAP_DSS_COLOR_UYVY;
1542 break;
1543 case OMAPFB_COLOR_ARGB16:
1544 mode = OMAP_DSS_COLOR_ARGB16;
1545 break;
1546 case OMAPFB_COLOR_RGB24U:
1547 mode = OMAP_DSS_COLOR_RGB24U;
1548 break;
1549 case OMAPFB_COLOR_RGB24P:
1550 mode = OMAP_DSS_COLOR_RGB24P;
1551 break;
1552 case OMAPFB_COLOR_ARGB32:
1553 mode = OMAP_DSS_COLOR_ARGB32;
1554 break;
1555 case OMAPFB_COLOR_RGBA32:
1556 mode = OMAP_DSS_COLOR_RGBA32;
1557 break;
1558 case OMAPFB_COLOR_RGBX32:
1559 mode = OMAP_DSS_COLOR_RGBX32;
1560 break;
1561 default:
1562 mode = -EINVAL;
1563 }
1564
1565 return mode;
1566}
1567
1568static int omapfb_parse_vram_param(const char *param, int max_entries,
1569 unsigned long *sizes, unsigned long *paddrs)
1570{
1571 int fbnum;
1572 unsigned long size;
1573 unsigned long paddr = 0;
1574 char *p, *start;
1575
1576 start = (char *)param;
1577
1578 while (1) {
1579 p = start;
1580
1581 fbnum = simple_strtoul(p, &p, 10);
1582
1583 if (p == param)
1584 return -EINVAL;
1585
1586 if (*p != ':')
1587 return -EINVAL;
1588
1589 if (fbnum >= max_entries)
1590 return -EINVAL;
1591
1592 size = memparse(p + 1, &p);
1593
1594 if (!size)
1595 return -EINVAL;
1596
1597 paddr = 0;
1598
1599 if (*p == '@') {
1600 paddr = simple_strtoul(p + 1, &p, 16);
1601
1602 if (!paddr)
1603 return -EINVAL;
1604
1605 }
1606
1607 paddrs[fbnum] = paddr;
1608 sizes[fbnum] = size;
1609
1610 if (*p == 0)
1611 break;
1612
1613 if (*p != ',')
1614 return -EINVAL;
1615
1616 ++p;
1617
1618 start = p;
1619 }
1620
1621 return 0;
1622}
1623
1624static int omapfb_allocate_all_fbs(struct omapfb2_device *fbdev)
1625{
1626 int i, r;
1627 unsigned long vram_sizes[10];
1628 unsigned long vram_paddrs[10];
1629
1630 memset(&vram_sizes, 0, sizeof(vram_sizes));
1631 memset(&vram_paddrs, 0, sizeof(vram_paddrs));
1632
1633 if (def_vram && omapfb_parse_vram_param(def_vram, 10,
1634 vram_sizes, vram_paddrs)) {
1635 dev_err(fbdev->dev, "failed to parse vram parameter\n");
1636
1637 memset(&vram_sizes, 0, sizeof(vram_sizes));
1638 memset(&vram_paddrs, 0, sizeof(vram_paddrs));
1639 }
1640
1641 if (fbdev->dev->platform_data) {
1642 struct omapfb_platform_data *opd;
1643 opd = fbdev->dev->platform_data;
1644 for (i = 0; i < opd->mem_desc.region_cnt; ++i) {
1645 if (!vram_sizes[i]) {
1646 unsigned long size;
1647 unsigned long paddr;
1648
1649 size = opd->mem_desc.region[i].size;
1650 paddr = opd->mem_desc.region[i].paddr;
1651
1652 vram_sizes[i] = size;
1653 vram_paddrs[i] = paddr;
1654 }
1655 }
1656 }
1657
1658 for (i = 0; i < fbdev->num_fbs; i++) {
1659 /* allocate memory automatically only for fb0, or if
1660 * excplicitly defined with vram or plat data option */
1661 if (i == 0 || vram_sizes[i] != 0) {
1662 r = omapfb_alloc_fbmem_display(fbdev->fbs[i],
1663 vram_sizes[i], vram_paddrs[i]);
1664
1665 if (r)
1666 return r;
1667 }
1668 }
1669
1670 for (i = 0; i < fbdev->num_fbs; i++) {
1671 struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[i]);
1672 struct omapfb2_mem_region *rg;
Ville Syrjälä078ff542010-03-17 20:36:51 +02001673 rg = ofbi->region;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001674
1675 DBG("region%d phys %08x virt %p size=%lu\n",
1676 i,
1677 rg->paddr,
1678 rg->vaddr,
1679 rg->size);
1680 }
1681
1682 return 0;
1683}
1684
1685int omapfb_realloc_fbmem(struct fb_info *fbi, unsigned long size, int type)
1686{
1687 struct omapfb_info *ofbi = FB2OFB(fbi);
1688 struct omapfb2_device *fbdev = ofbi->fbdev;
1689 struct omap_dss_device *display = fb2display(fbi);
Ville Syrjälä078ff542010-03-17 20:36:51 +02001690 struct omapfb2_mem_region *rg = ofbi->region;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001691 unsigned long old_size = rg->size;
1692 unsigned long old_paddr = rg->paddr;
1693 int old_type = rg->type;
1694 int r;
1695
1696 if (type > OMAPFB_MEMTYPE_MAX)
1697 return -EINVAL;
1698
1699 size = PAGE_ALIGN(size);
1700
1701 if (old_size == size && old_type == type)
1702 return 0;
1703
Tomi Valkeinen18946f62010-01-12 14:16:41 +02001704 if (display && display->driver->sync)
1705 display->driver->sync(display);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001706
1707 omapfb_free_fbmem(fbi);
1708
1709 if (size == 0) {
1710 clear_fb_info(fbi);
1711 return 0;
1712 }
1713
1714 r = omapfb_alloc_fbmem(fbi, size, 0);
1715
1716 if (r) {
1717 if (old_size)
1718 omapfb_alloc_fbmem(fbi, old_size, old_paddr);
1719
1720 if (rg->size == 0)
1721 clear_fb_info(fbi);
1722
1723 return r;
1724 }
1725
1726 if (old_size == size)
1727 return 0;
1728
1729 if (old_size == 0) {
1730 DBG("initializing fb %d\n", ofbi->id);
1731 r = omapfb_fb_init(fbdev, fbi);
1732 if (r) {
1733 DBG("omapfb_fb_init failed\n");
1734 goto err;
1735 }
1736 r = omapfb_apply_changes(fbi, 1);
1737 if (r) {
1738 DBG("omapfb_apply_changes failed\n");
1739 goto err;
1740 }
1741 } else {
1742 struct fb_var_screeninfo new_var;
1743 memcpy(&new_var, &fbi->var, sizeof(new_var));
1744 r = check_fb_var(fbi, &new_var);
1745 if (r)
1746 goto err;
1747 memcpy(&fbi->var, &new_var, sizeof(fbi->var));
1748 set_fb_fix(fbi);
1749 r = setup_vrfb_rotation(fbi);
1750 if (r)
1751 goto err;
1752 }
1753
1754 return 0;
1755err:
1756 omapfb_free_fbmem(fbi);
1757 clear_fb_info(fbi);
1758 return r;
1759}
1760
1761/* initialize fb_info, var, fix to something sane based on the display */
1762static int omapfb_fb_init(struct omapfb2_device *fbdev, struct fb_info *fbi)
1763{
1764 struct fb_var_screeninfo *var = &fbi->var;
1765 struct omap_dss_device *display = fb2display(fbi);
1766 struct omapfb_info *ofbi = FB2OFB(fbi);
1767 int r = 0;
1768
1769 fbi->fbops = &omapfb_ops;
1770 fbi->flags = FBINFO_FLAG_DEFAULT;
1771 fbi->pseudo_palette = fbdev->pseudo_palette;
1772
Ville Syrjälä078ff542010-03-17 20:36:51 +02001773 if (ofbi->region->size == 0) {
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001774 clear_fb_info(fbi);
1775 return 0;
1776 }
1777
1778 var->nonstd = 0;
1779 var->bits_per_pixel = 0;
1780
1781 var->rotate = def_rotate;
1782
1783 /*
1784 * Check if there is a default color format set in the board file,
1785 * and use this format instead the default deducted from the
1786 * display bpp.
1787 */
1788 if (fbdev->dev->platform_data) {
1789 struct omapfb_platform_data *opd;
1790 int id = ofbi->id;
1791
1792 opd = fbdev->dev->platform_data;
1793 if (opd->mem_desc.region[id].format_used) {
1794 enum omap_color_mode mode;
1795 enum omapfb_color_format format;
1796
1797 format = opd->mem_desc.region[id].format;
1798 mode = fb_format_to_dss_mode(format);
1799 if (mode < 0) {
1800 r = mode;
1801 goto err;
1802 }
1803 r = dss_mode_to_fb_mode(mode, var);
1804 if (r < 0)
1805 goto err;
1806 }
1807 }
1808
1809 if (display) {
1810 u16 w, h;
1811 int rotation = (var->rotate + ofbi->rotation[0]) % 4;
1812
Tomi Valkeinen96adcec2010-01-11 13:54:33 +02001813 display->driver->get_resolution(display, &w, &h);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001814
1815 if (rotation == FB_ROTATE_CW ||
1816 rotation == FB_ROTATE_CCW) {
1817 var->xres = h;
1818 var->yres = w;
1819 } else {
1820 var->xres = w;
1821 var->yres = h;
1822 }
1823
1824 var->xres_virtual = var->xres;
1825 var->yres_virtual = var->yres;
1826
1827 if (!var->bits_per_pixel) {
Tomi Valkeinena2699502010-01-11 14:33:40 +02001828 switch (omapfb_get_recommended_bpp(fbdev, display)) {
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001829 case 16:
1830 var->bits_per_pixel = 16;
1831 break;
1832 case 24:
1833 var->bits_per_pixel = 32;
1834 break;
1835 default:
1836 dev_err(fbdev->dev, "illegal display "
1837 "bpp\n");
1838 return -EINVAL;
1839 }
1840 }
1841 } else {
1842 /* if there's no display, let's just guess some basic values */
1843 var->xres = 320;
1844 var->yres = 240;
1845 var->xres_virtual = var->xres;
1846 var->yres_virtual = var->yres;
1847 if (!var->bits_per_pixel)
1848 var->bits_per_pixel = 16;
1849 }
1850
1851 r = check_fb_var(fbi, var);
1852 if (r)
1853 goto err;
1854
1855 set_fb_fix(fbi);
1856 r = setup_vrfb_rotation(fbi);
1857 if (r)
1858 goto err;
1859
1860 r = fb_alloc_cmap(&fbi->cmap, 256, 0);
1861 if (r)
1862 dev_err(fbdev->dev, "unable to allocate color map memory\n");
1863
1864err:
1865 return r;
1866}
1867
1868static void fbinfo_cleanup(struct omapfb2_device *fbdev, struct fb_info *fbi)
1869{
1870 fb_dealloc_cmap(&fbi->cmap);
1871}
1872
1873
1874static void omapfb_free_resources(struct omapfb2_device *fbdev)
1875{
1876 int i;
1877
1878 DBG("free_resources\n");
1879
1880 if (fbdev == NULL)
1881 return;
1882
1883 for (i = 0; i < fbdev->num_fbs; i++)
1884 unregister_framebuffer(fbdev->fbs[i]);
1885
1886 /* free the reserved fbmem */
1887 omapfb_free_all_fbmem(fbdev);
1888
1889 for (i = 0; i < fbdev->num_fbs; i++) {
1890 fbinfo_cleanup(fbdev, fbdev->fbs[i]);
1891 framebuffer_release(fbdev->fbs[i]);
1892 }
1893
1894 for (i = 0; i < fbdev->num_displays; i++) {
1895 if (fbdev->displays[i]->state != OMAP_DSS_DISPLAY_DISABLED)
Tomi Valkeinen37ac60e2010-01-12 15:12:07 +02001896 fbdev->displays[i]->driver->disable(fbdev->displays[i]);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001897
1898 omap_dss_put_device(fbdev->displays[i]);
1899 }
1900
1901 dev_set_drvdata(fbdev->dev, NULL);
1902 kfree(fbdev);
1903}
1904
1905static int omapfb_create_framebuffers(struct omapfb2_device *fbdev)
1906{
1907 int r, i;
1908
1909 fbdev->num_fbs = 0;
1910
1911 DBG("create %d framebuffers\n", CONFIG_FB_OMAP2_NUM_FBS);
1912
1913 /* allocate fb_infos */
1914 for (i = 0; i < CONFIG_FB_OMAP2_NUM_FBS; i++) {
1915 struct fb_info *fbi;
1916 struct omapfb_info *ofbi;
1917
1918 fbi = framebuffer_alloc(sizeof(struct omapfb_info),
1919 fbdev->dev);
1920
1921 if (fbi == NULL) {
1922 dev_err(fbdev->dev,
1923 "unable to allocate memory for plane info\n");
1924 return -ENOMEM;
1925 }
1926
1927 clear_fb_info(fbi);
1928
1929 fbdev->fbs[i] = fbi;
1930
1931 ofbi = FB2OFB(fbi);
1932 ofbi->fbdev = fbdev;
1933 ofbi->id = i;
1934
Ville Syrjälä078ff542010-03-17 20:36:51 +02001935 ofbi->region = &fbdev->regions[i];
1936 ofbi->region->id = i;
Ville Syrjälä2f642a12010-03-17 20:58:03 +02001937 init_rwsem(&ofbi->region->lock);
Ville Syrjälä078ff542010-03-17 20:36:51 +02001938
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001939 /* assign these early, so that fb alloc can use them */
1940 ofbi->rotation_type = def_vrfb ? OMAP_DSS_ROT_VRFB :
1941 OMAP_DSS_ROT_DMA;
1942 ofbi->mirror = def_mirror;
1943
1944 fbdev->num_fbs++;
1945 }
1946
1947 DBG("fb_infos allocated\n");
1948
1949 /* assign overlays for the fbs */
1950 for (i = 0; i < min(fbdev->num_fbs, fbdev->num_overlays); i++) {
1951 struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[i]);
1952
1953 ofbi->overlays[0] = fbdev->overlays[i];
1954 ofbi->num_overlays = 1;
1955 }
1956
1957 /* allocate fb memories */
1958 r = omapfb_allocate_all_fbs(fbdev);
1959 if (r) {
1960 dev_err(fbdev->dev, "failed to allocate fbmem\n");
1961 return r;
1962 }
1963
1964 DBG("fbmems allocated\n");
1965
1966 /* setup fb_infos */
1967 for (i = 0; i < fbdev->num_fbs; i++) {
Ville Syrjälä430571d2010-03-17 20:43:23 +02001968 struct fb_info *fbi = fbdev->fbs[i];
1969 struct omapfb_info *ofbi = FB2OFB(fbi);
1970
1971 omapfb_get_mem_region(ofbi->region);
1972 r = omapfb_fb_init(fbdev, fbi);
1973 omapfb_put_mem_region(ofbi->region);
1974
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001975 if (r) {
1976 dev_err(fbdev->dev, "failed to setup fb_info\n");
1977 return r;
1978 }
1979 }
1980
1981 DBG("fb_infos initialized\n");
1982
1983 for (i = 0; i < fbdev->num_fbs; i++) {
1984 r = register_framebuffer(fbdev->fbs[i]);
1985 if (r != 0) {
1986 dev_err(fbdev->dev,
1987 "registering framebuffer %d failed\n", i);
1988 return r;
1989 }
1990 }
1991
1992 DBG("framebuffers registered\n");
1993
1994 for (i = 0; i < fbdev->num_fbs; i++) {
Ville Syrjälä430571d2010-03-17 20:43:23 +02001995 struct fb_info *fbi = fbdev->fbs[i];
1996 struct omapfb_info *ofbi = FB2OFB(fbi);
1997
1998 omapfb_get_mem_region(ofbi->region);
1999 r = omapfb_apply_changes(fbi, 1);
2000 omapfb_put_mem_region(ofbi->region);
2001
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002002 if (r) {
2003 dev_err(fbdev->dev, "failed to change mode\n");
2004 return r;
2005 }
2006 }
2007
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002008 /* Enable fb0 */
2009 if (fbdev->num_fbs > 0) {
2010 struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[0]);
2011
2012 if (ofbi->num_overlays > 0) {
2013 struct omap_overlay *ovl = ofbi->overlays[0];
2014
2015 r = omapfb_overlay_enable(ovl, 1);
2016
2017 if (r) {
2018 dev_err(fbdev->dev,
2019 "failed to enable overlay\n");
2020 return r;
2021 }
2022 }
2023 }
2024
2025 DBG("create_framebuffers done\n");
2026
2027 return 0;
2028}
2029
2030static int omapfb_mode_to_timings(const char *mode_str,
2031 struct omap_video_timings *timings, u8 *bpp)
2032{
2033 struct fb_info fbi;
2034 struct fb_var_screeninfo var;
2035 struct fb_ops fbops;
2036 int r;
2037
2038#ifdef CONFIG_OMAP2_DSS_VENC
2039 if (strcmp(mode_str, "pal") == 0) {
2040 *timings = omap_dss_pal_timings;
Maurus Cuelenaeree8c66dc2010-07-22 00:40:58 +02002041 *bpp = 24;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002042 return 0;
2043 } else if (strcmp(mode_str, "ntsc") == 0) {
2044 *timings = omap_dss_ntsc_timings;
Maurus Cuelenaeree8c66dc2010-07-22 00:40:58 +02002045 *bpp = 24;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002046 return 0;
2047 }
2048#endif
2049
2050 /* this is quite a hack, but I wanted to use the modedb and for
2051 * that we need fb_info and var, so we create dummy ones */
2052
2053 memset(&fbi, 0, sizeof(fbi));
2054 memset(&var, 0, sizeof(var));
2055 memset(&fbops, 0, sizeof(fbops));
2056 fbi.fbops = &fbops;
2057
2058 r = fb_find_mode(&var, &fbi, mode_str, NULL, 0, NULL, 24);
2059
2060 if (r != 0) {
2061 timings->pixel_clock = PICOS2KHZ(var.pixclock);
Tasslehoff Kjappfot87ba8282010-09-08 12:46:14 +02002062 timings->hbp = var.left_margin;
2063 timings->hfp = var.right_margin;
2064 timings->vbp = var.upper_margin;
2065 timings->vfp = var.lower_margin;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002066 timings->hsw = var.hsync_len;
2067 timings->vsw = var.vsync_len;
2068 timings->x_res = var.xres;
2069 timings->y_res = var.yres;
2070
2071 switch (var.bits_per_pixel) {
2072 case 16:
2073 *bpp = 16;
2074 break;
2075 case 24:
2076 case 32:
2077 default:
2078 *bpp = 24;
2079 break;
2080 }
2081
2082 return 0;
2083 } else {
2084 return -EINVAL;
2085 }
2086}
2087
Tomi Valkeinena2699502010-01-11 14:33:40 +02002088static int omapfb_set_def_mode(struct omapfb2_device *fbdev,
2089 struct omap_dss_device *display, char *mode_str)
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002090{
2091 int r;
2092 u8 bpp;
Janorkar, Mayuresh371e2082011-02-22 07:35:13 -06002093 struct omap_video_timings timings, temp_timings;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002094
2095 r = omapfb_mode_to_timings(mode_str, &timings, &bpp);
2096 if (r)
2097 return r;
2098
Tomi Valkeinena2699502010-01-11 14:33:40 +02002099 fbdev->bpp_overrides[fbdev->num_bpp_overrides].dssdev = display;
2100 fbdev->bpp_overrides[fbdev->num_bpp_overrides].bpp = bpp;
2101 ++fbdev->num_bpp_overrides;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002102
Janorkar, Mayuresh371e2082011-02-22 07:35:13 -06002103 if (display->driver->check_timings) {
2104 r = display->driver->check_timings(display, &timings);
2105 if (r)
2106 return r;
2107 } else {
2108 /* If check_timings is not present compare xres and yres */
2109 if (display->driver->get_timings) {
2110 display->driver->get_timings(display, &temp_timings);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002111
Janorkar, Mayuresh371e2082011-02-22 07:35:13 -06002112 if (temp_timings.x_res != timings.x_res ||
2113 temp_timings.y_res != timings.y_res)
2114 return -EINVAL;
2115 }
2116 }
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002117
Janorkar, Mayuresh371e2082011-02-22 07:35:13 -06002118 if (display->driver->set_timings)
2119 display->driver->set_timings(display, &timings);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002120
2121 return 0;
2122}
2123
Tomi Valkeinena2699502010-01-11 14:33:40 +02002124static int omapfb_get_recommended_bpp(struct omapfb2_device *fbdev,
2125 struct omap_dss_device *dssdev)
2126{
2127 int i;
2128
2129 BUG_ON(dssdev->driver->get_recommended_bpp == NULL);
2130
2131 for (i = 0; i < fbdev->num_bpp_overrides; ++i) {
2132 if (dssdev == fbdev->bpp_overrides[i].dssdev)
2133 return fbdev->bpp_overrides[i].bpp;
2134 }
2135
2136 return dssdev->driver->get_recommended_bpp(dssdev);
2137}
2138
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002139static int omapfb_parse_def_modes(struct omapfb2_device *fbdev)
2140{
2141 char *str, *options, *this_opt;
2142 int r = 0;
2143
Samreen36e8c272010-11-16 12:49:07 +01002144 str = kstrdup(def_mode, GFP_KERNEL);
2145 if (!str)
2146 return -ENOMEM;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002147 options = str;
2148
2149 while (!r && (this_opt = strsep(&options, ",")) != NULL) {
2150 char *p, *display_str, *mode_str;
2151 struct omap_dss_device *display;
2152 int i;
2153
2154 p = strchr(this_opt, ':');
2155 if (!p) {
2156 r = -EINVAL;
2157 break;
2158 }
2159
2160 *p = 0;
2161 display_str = this_opt;
2162 mode_str = p + 1;
2163
2164 display = NULL;
2165 for (i = 0; i < fbdev->num_displays; ++i) {
2166 if (strcmp(fbdev->displays[i]->name,
2167 display_str) == 0) {
2168 display = fbdev->displays[i];
2169 break;
2170 }
2171 }
2172
2173 if (!display) {
2174 r = -EINVAL;
2175 break;
2176 }
2177
Tomi Valkeinena2699502010-01-11 14:33:40 +02002178 r = omapfb_set_def_mode(fbdev, display, mode_str);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002179 if (r)
2180 break;
2181 }
2182
2183 kfree(str);
2184
2185 return r;
2186}
2187
2188static int omapfb_probe(struct platform_device *pdev)
2189{
2190 struct omapfb2_device *fbdev = NULL;
2191 int r = 0;
2192 int i;
2193 struct omap_overlay *ovl;
2194 struct omap_dss_device *def_display;
2195 struct omap_dss_device *dssdev;
2196
2197 DBG("omapfb_probe\n");
2198
2199 if (pdev->num_resources != 0) {
2200 dev_err(&pdev->dev, "probed for an unknown device\n");
2201 r = -ENODEV;
2202 goto err0;
2203 }
2204
2205 fbdev = kzalloc(sizeof(struct omapfb2_device), GFP_KERNEL);
2206 if (fbdev == NULL) {
2207 r = -ENOMEM;
2208 goto err0;
2209 }
2210
Senthilvadivu Guruswamy41814cf2010-10-08 08:44:33 +02002211 /* TODO : Replace cpu check with omap_has_vrfb once HAS_FEATURE
2212 * available for OMAP2 and OMAP3
2213 */
2214 if (def_vrfb && !cpu_is_omap24xx() && !cpu_is_omap34xx()) {
2215 def_vrfb = 0;
2216 dev_warn(&pdev->dev, "VRFB is not supported on this hardware, "
2217 "ignoring the module parameter vrfb=y\n");
2218 }
2219
2220
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002221 mutex_init(&fbdev->mtx);
2222
2223 fbdev->dev = &pdev->dev;
2224 platform_set_drvdata(pdev, fbdev);
2225
Tomi Valkeinenb3f91eb2010-02-17 12:00:01 +02002226 r = 0;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002227 fbdev->num_displays = 0;
2228 dssdev = NULL;
2229 for_each_dss_dev(dssdev) {
2230 omap_dss_get_device(dssdev);
Tomi Valkeinenb3f91eb2010-02-17 12:00:01 +02002231
Tomi Valkeinen807a7512010-01-07 17:45:03 +02002232 if (!dssdev->driver) {
2233 dev_err(&pdev->dev, "no driver for display\n");
Tomi Valkeinenb3f91eb2010-02-17 12:00:01 +02002234 r = -ENODEV;
Tomi Valkeinen807a7512010-01-07 17:45:03 +02002235 }
Tomi Valkeinenb3f91eb2010-02-17 12:00:01 +02002236
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002237 fbdev->displays[fbdev->num_displays++] = dssdev;
2238 }
2239
Tomi Valkeinenb3f91eb2010-02-17 12:00:01 +02002240 if (r)
2241 goto cleanup;
2242
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002243 if (fbdev->num_displays == 0) {
2244 dev_err(&pdev->dev, "no displays\n");
2245 r = -EINVAL;
2246 goto cleanup;
2247 }
2248
2249 fbdev->num_overlays = omap_dss_get_num_overlays();
2250 for (i = 0; i < fbdev->num_overlays; i++)
2251 fbdev->overlays[i] = omap_dss_get_overlay(i);
2252
2253 fbdev->num_managers = omap_dss_get_num_overlay_managers();
2254 for (i = 0; i < fbdev->num_managers; i++)
2255 fbdev->managers[i] = omap_dss_get_overlay_manager(i);
2256
2257 if (def_mode && strlen(def_mode) > 0) {
2258 if (omapfb_parse_def_modes(fbdev))
2259 dev_warn(&pdev->dev, "cannot parse default modes\n");
2260 }
2261
2262 r = omapfb_create_framebuffers(fbdev);
2263 if (r)
2264 goto cleanup;
2265
2266 for (i = 0; i < fbdev->num_managers; i++) {
2267 struct omap_overlay_manager *mgr;
2268 mgr = fbdev->managers[i];
2269 r = mgr->apply(mgr);
2270 if (r)
2271 dev_warn(fbdev->dev, "failed to apply dispc config\n");
2272 }
2273
2274 DBG("mgr->apply'ed\n");
2275
2276 /* gfx overlay should be the default one. find a display
2277 * connected to that, and use it as default display */
2278 ovl = omap_dss_get_overlay(0);
2279 if (ovl->manager && ovl->manager->device) {
2280 def_display = ovl->manager->device;
2281 } else {
2282 dev_warn(&pdev->dev, "cannot find default display\n");
2283 def_display = NULL;
2284 }
2285
2286 if (def_display) {
Tomi Valkeinen446f7bf2010-01-11 16:12:31 +02002287 struct omap_dss_driver *dssdrv = def_display->driver;
Tomi Valkeinenddbfeb32010-02-17 15:01:50 +02002288
Tomi Valkeinen37ac60e2010-01-12 15:12:07 +02002289 r = def_display->driver->enable(def_display);
Tomi Valkeinen6d2e0bd2010-02-17 13:38:08 +02002290 if (r) {
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002291 dev_warn(fbdev->dev, "Failed to enable display '%s'\n",
2292 def_display->name);
Tomi Valkeinen6d2e0bd2010-02-17 13:38:08 +02002293 goto cleanup;
2294 }
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002295
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002296 if (def_display->caps & OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE) {
Tomi Valkeinenddbfeb32010-02-17 15:01:50 +02002297 u16 w, h;
Tomi Valkeinen446f7bf2010-01-11 16:12:31 +02002298 if (dssdrv->enable_te)
2299 dssdrv->enable_te(def_display, 1);
2300 if (dssdrv->set_update_mode)
2301 dssdrv->set_update_mode(def_display,
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002302 OMAP_DSS_UPDATE_MANUAL);
2303
Tomi Valkeinenddbfeb32010-02-17 15:01:50 +02002304 dssdrv->get_resolution(def_display, &w, &h);
Tomi Valkeinen18946f62010-01-12 14:16:41 +02002305 def_display->driver->update(def_display, 0, 0, w, h);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002306 } else {
Tomi Valkeinen446f7bf2010-01-11 16:12:31 +02002307 if (dssdrv->set_update_mode)
2308 dssdrv->set_update_mode(def_display,
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002309 OMAP_DSS_UPDATE_AUTO);
2310 }
2311 }
2312
Afzal Mohammede26ed442010-07-01 15:40:01 +02002313 DBG("create sysfs for fbs\n");
2314 r = omapfb_create_sysfs(fbdev);
2315 if (r) {
2316 dev_err(fbdev->dev, "failed to create sysfs entries\n");
2317 goto cleanup;
2318 }
2319
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002320 return 0;
2321
2322cleanup:
2323 omapfb_free_resources(fbdev);
2324err0:
2325 dev_err(&pdev->dev, "failed to setup omapfb\n");
2326 return r;
2327}
2328
2329static int omapfb_remove(struct platform_device *pdev)
2330{
2331 struct omapfb2_device *fbdev = platform_get_drvdata(pdev);
2332
2333 /* FIXME: wait till completion of pending events */
2334
2335 omapfb_remove_sysfs(fbdev);
2336
2337 omapfb_free_resources(fbdev);
2338
2339 return 0;
2340}
2341
2342static struct platform_driver omapfb_driver = {
2343 .probe = omapfb_probe,
2344 .remove = omapfb_remove,
2345 .driver = {
2346 .name = "omapfb",
2347 .owner = THIS_MODULE,
2348 },
2349};
2350
2351static int __init omapfb_init(void)
2352{
2353 DBG("omapfb_init\n");
2354
2355 if (platform_driver_register(&omapfb_driver)) {
2356 printk(KERN_ERR "failed to register omapfb driver\n");
2357 return -ENODEV;
2358 }
2359
2360 return 0;
2361}
2362
2363static void __exit omapfb_exit(void)
2364{
2365 DBG("omapfb_exit\n");
2366 platform_driver_unregister(&omapfb_driver);
2367}
2368
2369module_param_named(mode, def_mode, charp, 0);
2370module_param_named(vram, def_vram, charp, 0);
2371module_param_named(rotate, def_rotate, int, 0);
2372module_param_named(vrfb, def_vrfb, bool, 0);
2373module_param_named(mirror, def_mirror, bool, 0);
2374
2375/* late_initcall to let panel/ctrl drivers loaded first.
2376 * I guess better option would be a more dynamic approach,
2377 * so that omapfb reacts to new panels when they are loaded */
2378late_initcall(omapfb_init);
2379/*module_init(omapfb_init);*/
2380module_exit(omapfb_exit);
2381
2382MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@nokia.com>");
2383MODULE_DESCRIPTION("OMAP2/3 Framebuffer");
2384MODULE_LICENSE("GPL v2");