blob: 84c10121de483856fb64e6f1ac7b38a035326245 [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>
Tony Lindgren7d7e1eb2012-08-27 17:43:01 -070034#include <plat/cpu.h>
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +030035#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;
Rusty Russell90ab5ee2012-01-13 09:32:20 +103046static bool def_vrfb;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +030047static int def_rotate;
Rusty Russell90ab5ee2012-01-13 09:32:20 +103048static bool def_mirror;
Tomi Valkeinen27cc2132011-04-30 16:55:12 +030049static bool auto_update;
50static unsigned int auto_update_freq;
51module_param(auto_update, bool, 0);
52module_param(auto_update_freq, uint, 0644);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +030053
54#ifdef DEBUG
Rusty Russell90ab5ee2012-01-13 09:32:20 +103055bool omapfb_debug;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +030056module_param_named(debug, omapfb_debug, bool, 0644);
Rusty Russell90ab5ee2012-01-13 09:32:20 +103057static bool omapfb_test_pattern;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +030058module_param_named(test, omapfb_test_pattern, bool, 0644);
59#endif
60
61static int omapfb_fb_init(struct omapfb2_device *fbdev, struct fb_info *fbi);
Tomi Valkeinena2699502010-01-11 14:33:40 +020062static int omapfb_get_recommended_bpp(struct omapfb2_device *fbdev,
63 struct omap_dss_device *dssdev);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +030064
65#ifdef DEBUG
66static void draw_pixel(struct fb_info *fbi, int x, int y, unsigned color)
67{
68 struct fb_var_screeninfo *var = &fbi->var;
69 struct fb_fix_screeninfo *fix = &fbi->fix;
70 void __iomem *addr = fbi->screen_base;
71 const unsigned bytespp = var->bits_per_pixel >> 3;
72 const unsigned line_len = fix->line_length / bytespp;
73
74 int r = (color >> 16) & 0xff;
75 int g = (color >> 8) & 0xff;
76 int b = (color >> 0) & 0xff;
77
78 if (var->bits_per_pixel == 16) {
79 u16 __iomem *p = (u16 __iomem *)addr;
80 p += y * line_len + x;
81
82 r = r * 32 / 256;
83 g = g * 64 / 256;
84 b = b * 32 / 256;
85
86 __raw_writew((r << 11) | (g << 5) | (b << 0), p);
87 } else if (var->bits_per_pixel == 24) {
88 u8 __iomem *p = (u8 __iomem *)addr;
89 p += (y * line_len + x) * 3;
90
91 __raw_writeb(b, p + 0);
92 __raw_writeb(g, p + 1);
93 __raw_writeb(r, p + 2);
94 } else if (var->bits_per_pixel == 32) {
95 u32 __iomem *p = (u32 __iomem *)addr;
96 p += y * line_len + x;
97 __raw_writel(color, p);
98 }
99}
100
101static void fill_fb(struct fb_info *fbi)
102{
103 struct fb_var_screeninfo *var = &fbi->var;
104 const short w = var->xres_virtual;
105 const short h = var->yres_virtual;
106 void __iomem *addr = fbi->screen_base;
107 int y, x;
108
109 if (!addr)
110 return;
111
112 DBG("fill_fb %dx%d, line_len %d bytes\n", w, h, fbi->fix.line_length);
113
114 for (y = 0; y < h; y++) {
115 for (x = 0; x < w; x++) {
116 if (x < 20 && y < 20)
117 draw_pixel(fbi, x, y, 0xffffff);
118 else if (x < 20 && (y > 20 && y < h - 20))
119 draw_pixel(fbi, x, y, 0xff);
120 else if (y < 20 && (x > 20 && x < w - 20))
121 draw_pixel(fbi, x, y, 0xff00);
122 else if (x > w - 20 && (y > 20 && y < h - 20))
123 draw_pixel(fbi, x, y, 0xff0000);
124 else if (y > h - 20 && (x > 20 && x < w - 20))
125 draw_pixel(fbi, x, y, 0xffff00);
126 else if (x == 20 || x == w - 20 ||
127 y == 20 || y == h - 20)
128 draw_pixel(fbi, x, y, 0xffffff);
129 else if (x == y || w - x == h - y)
130 draw_pixel(fbi, x, y, 0xff00ff);
131 else if (w - x == y || x == h - y)
132 draw_pixel(fbi, x, y, 0x00ffff);
133 else if (x > 20 && y > 20 && x < w - 20 && y < h - 20) {
134 int t = x * 3 / w;
135 unsigned r = 0, g = 0, b = 0;
136 unsigned c;
137 if (var->bits_per_pixel == 16) {
138 if (t == 0)
139 b = (y % 32) * 256 / 32;
140 else if (t == 1)
141 g = (y % 64) * 256 / 64;
142 else if (t == 2)
143 r = (y % 32) * 256 / 32;
144 } else {
145 if (t == 0)
146 b = (y % 256);
147 else if (t == 1)
148 g = (y % 256);
149 else if (t == 2)
150 r = (y % 256);
151 }
152 c = (r << 16) | (g << 8) | (b << 0);
153 draw_pixel(fbi, x, y, c);
154 } else {
155 draw_pixel(fbi, x, y, 0);
156 }
157 }
158 }
159}
160#endif
161
Ville Syrjäläa4c1a142010-02-23 23:36:26 +0100162static unsigned omapfb_get_vrfb_offset(const struct omapfb_info *ofbi, int rot)
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300163{
Ville Syrjälä078ff542010-03-17 20:36:51 +0200164 const struct vrfb *vrfb = &ofbi->region->vrfb;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300165 unsigned offset;
166
167 switch (rot) {
168 case FB_ROTATE_UR:
169 offset = 0;
170 break;
171 case FB_ROTATE_CW:
172 offset = vrfb->yoffset;
173 break;
174 case FB_ROTATE_UD:
175 offset = vrfb->yoffset * OMAP_VRFB_LINE_LEN + vrfb->xoffset;
176 break;
177 case FB_ROTATE_CCW:
178 offset = vrfb->xoffset * OMAP_VRFB_LINE_LEN;
179 break;
180 default:
181 BUG();
Tomi Valkeinen4a75cb82012-05-18 11:48:28 +0300182 return 0;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300183 }
184
185 offset *= vrfb->bytespp;
186
187 return offset;
188}
189
Ville Syrjäläa4c1a142010-02-23 23:36:26 +0100190static u32 omapfb_get_region_rot_paddr(const struct omapfb_info *ofbi, int rot)
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300191{
192 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
Ville Syrjälä078ff542010-03-17 20:36:51 +0200193 return ofbi->region->vrfb.paddr[rot]
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300194 + omapfb_get_vrfb_offset(ofbi, rot);
195 } else {
Ville Syrjälä078ff542010-03-17 20:36:51 +0200196 return ofbi->region->paddr;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300197 }
198}
199
Ville Syrjäläa4c1a142010-02-23 23:36:26 +0100200static u32 omapfb_get_region_paddr(const struct omapfb_info *ofbi)
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300201{
202 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
Ville Syrjälä078ff542010-03-17 20:36:51 +0200203 return ofbi->region->vrfb.paddr[0];
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300204 else
Ville Syrjälä078ff542010-03-17 20:36:51 +0200205 return ofbi->region->paddr;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300206}
207
Ville Syrjäläa4c1a142010-02-23 23:36:26 +0100208static void __iomem *omapfb_get_region_vaddr(const struct omapfb_info *ofbi)
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300209{
210 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
Ville Syrjälä078ff542010-03-17 20:36:51 +0200211 return ofbi->region->vrfb.vaddr[0];
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300212 else
Ville Syrjälä078ff542010-03-17 20:36:51 +0200213 return ofbi->region->vaddr;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300214}
215
216static struct omapfb_colormode omapfb_colormodes[] = {
217 {
218 .dssmode = OMAP_DSS_COLOR_UYVY,
219 .bits_per_pixel = 16,
220 .nonstd = OMAPFB_COLOR_YUV422,
221 }, {
222 .dssmode = OMAP_DSS_COLOR_YUV2,
223 .bits_per_pixel = 16,
224 .nonstd = OMAPFB_COLOR_YUY422,
225 }, {
226 .dssmode = OMAP_DSS_COLOR_ARGB16,
227 .bits_per_pixel = 16,
228 .red = { .length = 4, .offset = 8, .msb_right = 0 },
229 .green = { .length = 4, .offset = 4, .msb_right = 0 },
230 .blue = { .length = 4, .offset = 0, .msb_right = 0 },
231 .transp = { .length = 4, .offset = 12, .msb_right = 0 },
232 }, {
233 .dssmode = OMAP_DSS_COLOR_RGB16,
234 .bits_per_pixel = 16,
235 .red = { .length = 5, .offset = 11, .msb_right = 0 },
236 .green = { .length = 6, .offset = 5, .msb_right = 0 },
237 .blue = { .length = 5, .offset = 0, .msb_right = 0 },
238 .transp = { .length = 0, .offset = 0, .msb_right = 0 },
239 }, {
240 .dssmode = OMAP_DSS_COLOR_RGB24P,
241 .bits_per_pixel = 24,
242 .red = { .length = 8, .offset = 16, .msb_right = 0 },
243 .green = { .length = 8, .offset = 8, .msb_right = 0 },
244 .blue = { .length = 8, .offset = 0, .msb_right = 0 },
245 .transp = { .length = 0, .offset = 0, .msb_right = 0 },
246 }, {
247 .dssmode = OMAP_DSS_COLOR_RGB24U,
248 .bits_per_pixel = 32,
249 .red = { .length = 8, .offset = 16, .msb_right = 0 },
250 .green = { .length = 8, .offset = 8, .msb_right = 0 },
251 .blue = { .length = 8, .offset = 0, .msb_right = 0 },
252 .transp = { .length = 0, .offset = 0, .msb_right = 0 },
253 }, {
254 .dssmode = OMAP_DSS_COLOR_ARGB32,
255 .bits_per_pixel = 32,
256 .red = { .length = 8, .offset = 16, .msb_right = 0 },
257 .green = { .length = 8, .offset = 8, .msb_right = 0 },
258 .blue = { .length = 8, .offset = 0, .msb_right = 0 },
259 .transp = { .length = 8, .offset = 24, .msb_right = 0 },
260 }, {
261 .dssmode = OMAP_DSS_COLOR_RGBA32,
262 .bits_per_pixel = 32,
263 .red = { .length = 8, .offset = 24, .msb_right = 0 },
264 .green = { .length = 8, .offset = 16, .msb_right = 0 },
265 .blue = { .length = 8, .offset = 8, .msb_right = 0 },
266 .transp = { .length = 8, .offset = 0, .msb_right = 0 },
267 }, {
268 .dssmode = OMAP_DSS_COLOR_RGBX32,
269 .bits_per_pixel = 32,
270 .red = { .length = 8, .offset = 24, .msb_right = 0 },
271 .green = { .length = 8, .offset = 16, .msb_right = 0 },
272 .blue = { .length = 8, .offset = 8, .msb_right = 0 },
273 .transp = { .length = 0, .offset = 0, .msb_right = 0 },
274 },
275};
276
277static bool cmp_var_to_colormode(struct fb_var_screeninfo *var,
278 struct omapfb_colormode *color)
279{
280 bool cmp_component(struct fb_bitfield *f1, struct fb_bitfield *f2)
281 {
282 return f1->length == f2->length &&
283 f1->offset == f2->offset &&
284 f1->msb_right == f2->msb_right;
285 }
286
287 if (var->bits_per_pixel == 0 ||
288 var->red.length == 0 ||
289 var->blue.length == 0 ||
290 var->green.length == 0)
291 return 0;
292
293 return var->bits_per_pixel == color->bits_per_pixel &&
294 cmp_component(&var->red, &color->red) &&
295 cmp_component(&var->green, &color->green) &&
296 cmp_component(&var->blue, &color->blue) &&
297 cmp_component(&var->transp, &color->transp);
298}
299
300static void assign_colormode_to_var(struct fb_var_screeninfo *var,
301 struct omapfb_colormode *color)
302{
303 var->bits_per_pixel = color->bits_per_pixel;
304 var->nonstd = color->nonstd;
305 var->red = color->red;
306 var->green = color->green;
307 var->blue = color->blue;
308 var->transp = color->transp;
309}
310
311static int fb_mode_to_dss_mode(struct fb_var_screeninfo *var,
312 enum omap_color_mode *mode)
313{
314 enum omap_color_mode dssmode;
315 int i;
316
317 /* first match with nonstd field */
318 if (var->nonstd) {
319 for (i = 0; i < ARRAY_SIZE(omapfb_colormodes); ++i) {
320 struct omapfb_colormode *m = &omapfb_colormodes[i];
321 if (var->nonstd == m->nonstd) {
322 assign_colormode_to_var(var, m);
323 *mode = m->dssmode;
324 return 0;
325 }
326 }
327
328 return -EINVAL;
329 }
330
331 /* then try exact match of bpp and colors */
332 for (i = 0; i < ARRAY_SIZE(omapfb_colormodes); ++i) {
333 struct omapfb_colormode *m = &omapfb_colormodes[i];
334 if (cmp_var_to_colormode(var, m)) {
335 assign_colormode_to_var(var, m);
336 *mode = m->dssmode;
337 return 0;
338 }
339 }
340
341 /* match with bpp if user has not filled color fields
342 * properly */
343 switch (var->bits_per_pixel) {
344 case 1:
345 dssmode = OMAP_DSS_COLOR_CLUT1;
346 break;
347 case 2:
348 dssmode = OMAP_DSS_COLOR_CLUT2;
349 break;
350 case 4:
351 dssmode = OMAP_DSS_COLOR_CLUT4;
352 break;
353 case 8:
354 dssmode = OMAP_DSS_COLOR_CLUT8;
355 break;
356 case 12:
357 dssmode = OMAP_DSS_COLOR_RGB12U;
358 break;
359 case 16:
360 dssmode = OMAP_DSS_COLOR_RGB16;
361 break;
362 case 24:
363 dssmode = OMAP_DSS_COLOR_RGB24P;
364 break;
365 case 32:
366 dssmode = OMAP_DSS_COLOR_RGB24U;
367 break;
368 default:
369 return -EINVAL;
370 }
371
372 for (i = 0; i < ARRAY_SIZE(omapfb_colormodes); ++i) {
373 struct omapfb_colormode *m = &omapfb_colormodes[i];
374 if (dssmode == m->dssmode) {
375 assign_colormode_to_var(var, m);
376 *mode = m->dssmode;
377 return 0;
378 }
379 }
380
381 return -EINVAL;
382}
383
384static int check_fb_res_bounds(struct fb_var_screeninfo *var)
385{
386 int xres_min = OMAPFB_PLANE_XRES_MIN;
387 int xres_max = 2048;
388 int yres_min = OMAPFB_PLANE_YRES_MIN;
389 int yres_max = 2048;
390
391 /* XXX: some applications seem to set virtual res to 0. */
392 if (var->xres_virtual == 0)
393 var->xres_virtual = var->xres;
394
395 if (var->yres_virtual == 0)
396 var->yres_virtual = var->yres;
397
398 if (var->xres_virtual < xres_min || var->yres_virtual < yres_min)
399 return -EINVAL;
400
401 if (var->xres < xres_min)
402 var->xres = xres_min;
403 if (var->yres < yres_min)
404 var->yres = yres_min;
405 if (var->xres > xres_max)
406 var->xres = xres_max;
407 if (var->yres > yres_max)
408 var->yres = yres_max;
409
410 if (var->xres > var->xres_virtual)
411 var->xres = var->xres_virtual;
412 if (var->yres > var->yres_virtual)
413 var->yres = var->yres_virtual;
414
415 return 0;
416}
417
418static void shrink_height(unsigned long max_frame_size,
419 struct fb_var_screeninfo *var)
420{
421 DBG("can't fit FB into memory, reducing y\n");
422 var->yres_virtual = max_frame_size /
423 (var->xres_virtual * var->bits_per_pixel >> 3);
424
425 if (var->yres_virtual < OMAPFB_PLANE_YRES_MIN)
426 var->yres_virtual = OMAPFB_PLANE_YRES_MIN;
427
428 if (var->yres > var->yres_virtual)
429 var->yres = var->yres_virtual;
430}
431
432static void shrink_width(unsigned long max_frame_size,
433 struct fb_var_screeninfo *var)
434{
435 DBG("can't fit FB into memory, reducing x\n");
436 var->xres_virtual = max_frame_size / var->yres_virtual /
437 (var->bits_per_pixel >> 3);
438
439 if (var->xres_virtual < OMAPFB_PLANE_XRES_MIN)
440 var->xres_virtual = OMAPFB_PLANE_XRES_MIN;
441
442 if (var->xres > var->xres_virtual)
443 var->xres = var->xres_virtual;
444}
445
446static int check_vrfb_fb_size(unsigned long region_size,
447 const struct fb_var_screeninfo *var)
448{
449 unsigned long min_phys_size = omap_vrfb_min_phys_size(var->xres_virtual,
450 var->yres_virtual, var->bits_per_pixel >> 3);
451
452 return min_phys_size > region_size ? -EINVAL : 0;
453}
454
455static int check_fb_size(const struct omapfb_info *ofbi,
456 struct fb_var_screeninfo *var)
457{
Ville Syrjälä078ff542010-03-17 20:36:51 +0200458 unsigned long max_frame_size = ofbi->region->size;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300459 int bytespp = var->bits_per_pixel >> 3;
460 unsigned long line_size = var->xres_virtual * bytespp;
461
462 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
463 /* One needs to check for both VRFB and OMAPFB limitations. */
464 if (check_vrfb_fb_size(max_frame_size, var))
465 shrink_height(omap_vrfb_max_height(
466 max_frame_size, var->xres_virtual, bytespp) *
467 line_size, var);
468
469 if (check_vrfb_fb_size(max_frame_size, var)) {
470 DBG("cannot fit FB to memory\n");
471 return -EINVAL;
472 }
473
474 return 0;
475 }
476
477 DBG("max frame size %lu, line size %lu\n", max_frame_size, line_size);
478
479 if (line_size * var->yres_virtual > max_frame_size)
480 shrink_height(max_frame_size, var);
481
482 if (line_size * var->yres_virtual > max_frame_size) {
483 shrink_width(max_frame_size, var);
484 line_size = var->xres_virtual * bytespp;
485 }
486
487 if (line_size * var->yres_virtual > max_frame_size) {
488 DBG("cannot fit FB to memory\n");
489 return -EINVAL;
490 }
491
492 return 0;
493}
494
495/*
496 * Consider if VRFB assisted rotation is in use and if the virtual space for
497 * the zero degree view needs to be mapped. The need for mapping also acts as
498 * the trigger for setting up the hardware on the context in question. This
499 * ensures that one does not attempt to access the virtual view before the
500 * hardware is serving the address translations.
501 */
502static int setup_vrfb_rotation(struct fb_info *fbi)
503{
504 struct omapfb_info *ofbi = FB2OFB(fbi);
Ville Syrjälä078ff542010-03-17 20:36:51 +0200505 struct omapfb2_mem_region *rg = ofbi->region;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300506 struct vrfb *vrfb = &rg->vrfb;
507 struct fb_var_screeninfo *var = &fbi->var;
508 struct fb_fix_screeninfo *fix = &fbi->fix;
509 unsigned bytespp;
510 bool yuv_mode;
511 enum omap_color_mode mode;
512 int r;
513 bool reconf;
514
515 if (!rg->size || ofbi->rotation_type != OMAP_DSS_ROT_VRFB)
516 return 0;
517
518 DBG("setup_vrfb_rotation\n");
519
520 r = fb_mode_to_dss_mode(var, &mode);
521 if (r)
522 return r;
523
524 bytespp = var->bits_per_pixel >> 3;
525
526 yuv_mode = mode == OMAP_DSS_COLOR_YUV2 || mode == OMAP_DSS_COLOR_UYVY;
527
528 /* We need to reconfigure VRFB if the resolution changes, if yuv mode
529 * is enabled/disabled, or if bytes per pixel changes */
530
531 /* XXX we shouldn't allow this when framebuffer is mmapped */
532
533 reconf = false;
534
535 if (yuv_mode != vrfb->yuv_mode)
536 reconf = true;
537 else if (bytespp != vrfb->bytespp)
538 reconf = true;
539 else if (vrfb->xres != var->xres_virtual ||
540 vrfb->yres != var->yres_virtual)
541 reconf = true;
542
543 if (vrfb->vaddr[0] && reconf) {
544 fbi->screen_base = NULL;
545 fix->smem_start = 0;
546 fix->smem_len = 0;
547 iounmap(vrfb->vaddr[0]);
548 vrfb->vaddr[0] = NULL;
549 DBG("setup_vrfb_rotation: reset fb\n");
550 }
551
552 if (vrfb->vaddr[0])
553 return 0;
554
555 omap_vrfb_setup(&rg->vrfb, rg->paddr,
556 var->xres_virtual,
557 var->yres_virtual,
558 bytespp, yuv_mode);
559
560 /* Now one can ioremap the 0 angle view */
561 r = omap_vrfb_map_angle(vrfb, var->yres_virtual, 0);
562 if (r)
563 return r;
564
565 /* used by open/write in fbmem.c */
Ville Syrjälä078ff542010-03-17 20:36:51 +0200566 fbi->screen_base = ofbi->region->vrfb.vaddr[0];
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300567
Ville Syrjälä078ff542010-03-17 20:36:51 +0200568 fix->smem_start = ofbi->region->vrfb.paddr[0];
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300569
570 switch (var->nonstd) {
571 case OMAPFB_COLOR_YUV422:
572 case OMAPFB_COLOR_YUY422:
573 fix->line_length =
574 (OMAP_VRFB_LINE_LEN * var->bits_per_pixel) >> 2;
575 break;
576 default:
577 fix->line_length =
578 (OMAP_VRFB_LINE_LEN * var->bits_per_pixel) >> 3;
579 break;
580 }
581
582 fix->smem_len = var->yres_virtual * fix->line_length;
583
584 return 0;
585}
586
587int dss_mode_to_fb_mode(enum omap_color_mode dssmode,
588 struct fb_var_screeninfo *var)
589{
590 int i;
591
592 for (i = 0; i < ARRAY_SIZE(omapfb_colormodes); ++i) {
593 struct omapfb_colormode *mode = &omapfb_colormodes[i];
594 if (dssmode == mode->dssmode) {
595 assign_colormode_to_var(var, mode);
596 return 0;
597 }
598 }
599 return -ENOENT;
600}
601
602void set_fb_fix(struct fb_info *fbi)
603{
604 struct fb_fix_screeninfo *fix = &fbi->fix;
605 struct fb_var_screeninfo *var = &fbi->var;
606 struct omapfb_info *ofbi = FB2OFB(fbi);
Ville Syrjälä078ff542010-03-17 20:36:51 +0200607 struct omapfb2_mem_region *rg = ofbi->region;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300608
609 DBG("set_fb_fix\n");
610
611 /* used by open/write in fbmem.c */
612 fbi->screen_base = (char __iomem *)omapfb_get_region_vaddr(ofbi);
613
614 /* used by mmap in fbmem.c */
615 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
616 switch (var->nonstd) {
617 case OMAPFB_COLOR_YUV422:
618 case OMAPFB_COLOR_YUY422:
619 fix->line_length =
620 (OMAP_VRFB_LINE_LEN * var->bits_per_pixel) >> 2;
621 break;
622 default:
623 fix->line_length =
624 (OMAP_VRFB_LINE_LEN * var->bits_per_pixel) >> 3;
625 break;
626 }
627
628 fix->smem_len = var->yres_virtual * fix->line_length;
629 } else {
630 fix->line_length =
631 (var->xres_virtual * var->bits_per_pixel) >> 3;
632 fix->smem_len = rg->size;
633 }
634
635 fix->smem_start = omapfb_get_region_paddr(ofbi);
636
637 fix->type = FB_TYPE_PACKED_PIXELS;
638
639 if (var->nonstd)
640 fix->visual = FB_VISUAL_PSEUDOCOLOR;
641 else {
642 switch (var->bits_per_pixel) {
643 case 32:
644 case 24:
645 case 16:
646 case 12:
647 fix->visual = FB_VISUAL_TRUECOLOR;
648 /* 12bpp is stored in 16 bits */
649 break;
650 case 1:
651 case 2:
652 case 4:
653 case 8:
654 fix->visual = FB_VISUAL_PSEUDOCOLOR;
655 break;
656 }
657 }
658
659 fix->accel = FB_ACCEL_NONE;
660
661 fix->xpanstep = 1;
662 fix->ypanstep = 1;
663}
664
665/* check new var and possibly modify it to be ok */
666int check_fb_var(struct fb_info *fbi, struct fb_var_screeninfo *var)
667{
668 struct omapfb_info *ofbi = FB2OFB(fbi);
669 struct omap_dss_device *display = fb2display(fbi);
670 enum omap_color_mode mode = 0;
671 int i;
672 int r;
673
674 DBG("check_fb_var %d\n", ofbi->id);
675
Ville Syrjälä1ceafc02010-03-17 21:28:50 +0200676 WARN_ON(!atomic_read(&ofbi->region->lock_count));
677
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300678 r = fb_mode_to_dss_mode(var, &mode);
679 if (r) {
680 DBG("cannot convert var to omap dss mode\n");
681 return r;
682 }
683
684 for (i = 0; i < ofbi->num_overlays; ++i) {
685 if ((ofbi->overlays[i]->supported_modes & mode) == 0) {
686 DBG("invalid mode\n");
687 return -EINVAL;
688 }
689 }
690
Jani Nikula86f2d7d2010-06-01 17:25:10 +0300691 if (var->rotate > 3)
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300692 return -EINVAL;
693
694 if (check_fb_res_bounds(var))
695 return -EINVAL;
696
Ville Syrjälä276a1d42010-03-17 20:05:38 +0200697 /* When no memory is allocated ignore the size check */
Ville Syrjälä078ff542010-03-17 20:36:51 +0200698 if (ofbi->region->size != 0 && check_fb_size(ofbi, var))
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300699 return -EINVAL;
700
701 if (var->xres + var->xoffset > var->xres_virtual)
702 var->xoffset = var->xres_virtual - var->xres;
703 if (var->yres + var->yoffset > var->yres_virtual)
704 var->yoffset = var->yres_virtual - var->yres;
705
706 DBG("xres = %d, yres = %d, vxres = %d, vyres = %d\n",
707 var->xres, var->yres,
708 var->xres_virtual, var->yres_virtual);
709
Jani Nikula7a0987b2010-06-16 15:26:36 +0300710 if (display && display->driver->get_dimensions) {
711 u32 w, h;
712 display->driver->get_dimensions(display, &w, &h);
713 var->width = DIV_ROUND_CLOSEST(w, 1000);
714 var->height = DIV_ROUND_CLOSEST(h, 1000);
715 } else {
716 var->height = -1;
717 var->width = -1;
718 }
719
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300720 var->grayscale = 0;
721
Tomi Valkeinen69b20482010-01-20 12:11:25 +0200722 if (display && display->driver->get_timings) {
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300723 struct omap_video_timings timings;
Tomi Valkeinen69b20482010-01-20 12:11:25 +0200724 display->driver->get_timings(display, &timings);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300725
726 /* pixclock in ps, the rest in pixclock */
727 var->pixclock = timings.pixel_clock != 0 ?
728 KHZ2PICOS(timings.pixel_clock) :
729 0;
Tasslehoff Kjappfot87ba8282010-09-08 12:46:14 +0200730 var->left_margin = timings.hbp;
731 var->right_margin = timings.hfp;
732 var->upper_margin = timings.vbp;
733 var->lower_margin = timings.vfp;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300734 var->hsync_len = timings.hsw;
735 var->vsync_len = timings.vsw;
Archit Taneja783babf2012-06-15 07:39:47 +0530736 var->sync |= timings.hsync_level == OMAPDSS_SIG_ACTIVE_HIGH ?
737 FB_SYNC_HOR_HIGH_ACT : 0;
738 var->sync |= timings.vsync_level == OMAPDSS_SIG_ACTIVE_HIGH ?
739 FB_SYNC_VERT_HIGH_ACT : 0;
Archit Taneja23bae3a2012-06-28 18:23:18 +0530740 var->vmode = timings.interlace ?
741 FB_VMODE_INTERLACED : FB_VMODE_NONINTERLACED;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300742 } else {
743 var->pixclock = 0;
744 var->left_margin = 0;
745 var->right_margin = 0;
746 var->upper_margin = 0;
747 var->lower_margin = 0;
748 var->hsync_len = 0;
749 var->vsync_len = 0;
Archit Taneja783babf2012-06-15 07:39:47 +0530750 var->sync = 0;
Archit Taneja23bae3a2012-06-28 18:23:18 +0530751 var->vmode = FB_VMODE_NONINTERLACED;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300752 }
753
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300754 return 0;
755}
756
757/*
758 * ---------------------------------------------------------------------------
759 * fbdev framework callbacks
760 * ---------------------------------------------------------------------------
761 */
762static int omapfb_open(struct fb_info *fbi, int user)
763{
764 return 0;
765}
766
767static int omapfb_release(struct fb_info *fbi, int user)
768{
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300769 return 0;
770}
771
Ville Syrjäläa4c1a142010-02-23 23:36:26 +0100772static unsigned calc_rotation_offset_dma(const struct fb_var_screeninfo *var,
773 const struct fb_fix_screeninfo *fix, int rotation)
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300774{
775 unsigned offset;
776
777 offset = var->yoffset * fix->line_length +
778 var->xoffset * (var->bits_per_pixel >> 3);
779
780 return offset;
781}
782
Ville Syrjäläa4c1a142010-02-23 23:36:26 +0100783static unsigned calc_rotation_offset_vrfb(const struct fb_var_screeninfo *var,
784 const struct fb_fix_screeninfo *fix, int rotation)
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300785{
786 unsigned offset;
787
788 if (rotation == FB_ROTATE_UD)
789 offset = (var->yres_virtual - var->yres) *
790 fix->line_length;
791 else if (rotation == FB_ROTATE_CW)
792 offset = (var->yres_virtual - var->yres) *
793 (var->bits_per_pixel >> 3);
794 else
795 offset = 0;
796
797 if (rotation == FB_ROTATE_UR)
798 offset += var->yoffset * fix->line_length +
799 var->xoffset * (var->bits_per_pixel >> 3);
800 else if (rotation == FB_ROTATE_UD)
801 offset -= var->yoffset * fix->line_length +
802 var->xoffset * (var->bits_per_pixel >> 3);
803 else if (rotation == FB_ROTATE_CW)
804 offset -= var->xoffset * fix->line_length +
805 var->yoffset * (var->bits_per_pixel >> 3);
806 else if (rotation == FB_ROTATE_CCW)
807 offset += var->xoffset * fix->line_length +
808 var->yoffset * (var->bits_per_pixel >> 3);
809
810 return offset;
811}
812
Ville Syrjälä46d35242010-03-17 19:59:26 +0200813static void omapfb_calc_addr(const struct omapfb_info *ofbi,
814 const struct fb_var_screeninfo *var,
815 const struct fb_fix_screeninfo *fix,
Tomi Valkeinen212b0d52011-09-26 19:16:59 +0300816 int rotation, u32 *paddr)
Ville Syrjälä46d35242010-03-17 19:59:26 +0200817{
818 u32 data_start_p;
Ville Syrjälä46d35242010-03-17 19:59:26 +0200819 int offset;
820
Tomi Valkeinen212b0d52011-09-26 19:16:59 +0300821 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
Ville Syrjälä46d35242010-03-17 19:59:26 +0200822 data_start_p = omapfb_get_region_rot_paddr(ofbi, rotation);
Tomi Valkeinen212b0d52011-09-26 19:16:59 +0300823 else
Ville Syrjälä46d35242010-03-17 19:59:26 +0200824 data_start_p = omapfb_get_region_paddr(ofbi);
Ville Syrjälä46d35242010-03-17 19:59:26 +0200825
826 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
827 offset = calc_rotation_offset_vrfb(var, fix, rotation);
828 else
829 offset = calc_rotation_offset_dma(var, fix, rotation);
830
831 data_start_p += offset;
Ville Syrjälä46d35242010-03-17 19:59:26 +0200832
833 if (offset)
834 DBG("offset %d, %d = %d\n",
835 var->xoffset, var->yoffset, offset);
836
Tomi Valkeinen212b0d52011-09-26 19:16:59 +0300837 DBG("paddr %x\n", data_start_p);
Ville Syrjälä46d35242010-03-17 19:59:26 +0200838
839 *paddr = data_start_p;
Ville Syrjälä46d35242010-03-17 19:59:26 +0200840}
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300841
842/* setup overlay according to the fb */
Ville Syrjälä078ff542010-03-17 20:36:51 +0200843int omapfb_setup_overlay(struct fb_info *fbi, struct omap_overlay *ovl,
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300844 u16 posx, u16 posy, u16 outw, u16 outh)
845{
846 int r = 0;
847 struct omapfb_info *ofbi = FB2OFB(fbi);
848 struct fb_var_screeninfo *var = &fbi->var;
849 struct fb_fix_screeninfo *fix = &fbi->fix;
850 enum omap_color_mode mode = 0;
Ville Syrjälä46d35242010-03-17 19:59:26 +0200851 u32 data_start_p = 0;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300852 struct omap_overlay_info info;
853 int xres, yres;
854 int screen_width;
855 int mirror;
856 int rotation = var->rotate;
857 int i;
858
Ville Syrjälä1ceafc02010-03-17 21:28:50 +0200859 WARN_ON(!atomic_read(&ofbi->region->lock_count));
860
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300861 for (i = 0; i < ofbi->num_overlays; i++) {
862 if (ovl != ofbi->overlays[i])
863 continue;
864
865 rotation = (rotation + ofbi->rotation[i]) % 4;
866 break;
867 }
868
869 DBG("setup_overlay %d, posx %d, posy %d, outw %d, outh %d\n", ofbi->id,
870 posx, posy, outw, outh);
871
872 if (rotation == FB_ROTATE_CW || rotation == FB_ROTATE_CCW) {
873 xres = var->yres;
874 yres = var->xres;
875 } else {
876 xres = var->xres;
877 yres = var->yres;
878 }
879
Ville Syrjälä078ff542010-03-17 20:36:51 +0200880 if (ofbi->region->size)
Tomi Valkeinen212b0d52011-09-26 19:16:59 +0300881 omapfb_calc_addr(ofbi, var, fix, rotation, &data_start_p);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300882
883 r = fb_mode_to_dss_mode(var, &mode);
884 if (r) {
885 DBG("fb_mode_to_dss_mode failed");
886 goto err;
887 }
888
889 switch (var->nonstd) {
890 case OMAPFB_COLOR_YUV422:
891 case OMAPFB_COLOR_YUY422:
892 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
893 screen_width = fix->line_length
894 / (var->bits_per_pixel >> 2);
895 break;
896 }
897 default:
898 screen_width = fix->line_length / (var->bits_per_pixel >> 3);
899 break;
900 }
901
902 ovl->get_overlay_info(ovl, &info);
903
904 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
905 mirror = 0;
906 else
907 mirror = ofbi->mirror;
908
909 info.paddr = data_start_p;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300910 info.screen_width = screen_width;
911 info.width = xres;
912 info.height = yres;
913 info.color_mode = mode;
914 info.rotation_type = ofbi->rotation_type;
915 info.rotation = rotation;
916 info.mirror = mirror;
917
918 info.pos_x = posx;
919 info.pos_y = posy;
920 info.out_width = outw;
921 info.out_height = outh;
922
923 r = ovl->set_overlay_info(ovl, &info);
924 if (r) {
925 DBG("ovl->setup_overlay_info failed\n");
926 goto err;
927 }
928
929 return 0;
930
931err:
932 DBG("setup_overlay failed\n");
933 return r;
934}
935
936/* apply var to the overlay */
937int omapfb_apply_changes(struct fb_info *fbi, int init)
938{
939 int r = 0;
940 struct omapfb_info *ofbi = FB2OFB(fbi);
941 struct fb_var_screeninfo *var = &fbi->var;
942 struct omap_overlay *ovl;
943 u16 posx, posy;
944 u16 outw, outh;
945 int i;
946
947#ifdef DEBUG
948 if (omapfb_test_pattern)
949 fill_fb(fbi);
950#endif
951
Ville Syrjälä1ceafc02010-03-17 21:28:50 +0200952 WARN_ON(!atomic_read(&ofbi->region->lock_count));
953
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300954 for (i = 0; i < ofbi->num_overlays; i++) {
955 ovl = ofbi->overlays[i];
956
957 DBG("apply_changes, fb %d, ovl %d\n", ofbi->id, ovl->id);
958
Ville Syrjälä078ff542010-03-17 20:36:51 +0200959 if (ofbi->region->size == 0) {
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300960 /* the fb is not available. disable the overlay */
961 omapfb_overlay_enable(ovl, 0);
962 if (!init && ovl->manager)
963 ovl->manager->apply(ovl->manager);
964 continue;
965 }
966
967 if (init || (ovl->caps & OMAP_DSS_OVL_CAP_SCALE) == 0) {
968 int rotation = (var->rotate + ofbi->rotation[i]) % 4;
969 if (rotation == FB_ROTATE_CW ||
970 rotation == FB_ROTATE_CCW) {
971 outw = var->yres;
972 outh = var->xres;
973 } else {
974 outw = var->xres;
975 outh = var->yres;
976 }
977 } else {
Tomi Valkeinenc1a9feb2011-11-16 14:11:56 +0200978 struct omap_overlay_info info;
979 ovl->get_overlay_info(ovl, &info);
980 outw = info.out_width;
981 outh = info.out_height;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300982 }
983
984 if (init) {
985 posx = 0;
986 posy = 0;
987 } else {
Tomi Valkeinenc1a9feb2011-11-16 14:11:56 +0200988 struct omap_overlay_info info;
989 ovl->get_overlay_info(ovl, &info);
990 posx = info.pos_x;
991 posy = info.pos_y;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300992 }
993
994 r = omapfb_setup_overlay(fbi, ovl, posx, posy, outw, outh);
995 if (r)
996 goto err;
997
998 if (!init && ovl->manager)
999 ovl->manager->apply(ovl->manager);
1000 }
1001 return 0;
1002err:
1003 DBG("apply_changes failed\n");
1004 return r;
1005}
1006
1007/* checks var and eventually tweaks it to something supported,
1008 * DO NOT MODIFY PAR */
1009static int omapfb_check_var(struct fb_var_screeninfo *var, struct fb_info *fbi)
1010{
Ville Syrjälä430571d2010-03-17 20:43:23 +02001011 struct omapfb_info *ofbi = FB2OFB(fbi);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001012 int r;
1013
1014 DBG("check_var(%d)\n", FB2OFB(fbi)->id);
1015
Ville Syrjälä430571d2010-03-17 20:43:23 +02001016 omapfb_get_mem_region(ofbi->region);
1017
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001018 r = check_fb_var(fbi, var);
1019
Ville Syrjälä430571d2010-03-17 20:43:23 +02001020 omapfb_put_mem_region(ofbi->region);
1021
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001022 return r;
1023}
1024
1025/* set the video mode according to info->var */
1026static int omapfb_set_par(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("set_par(%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 set_fb_fix(fbi);
1036
1037 r = setup_vrfb_rotation(fbi);
1038 if (r)
Ville Syrjälä430571d2010-03-17 20:43:23 +02001039 goto out;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001040
1041 r = omapfb_apply_changes(fbi, 0);
1042
Ville Syrjälä430571d2010-03-17 20:43:23 +02001043 out:
1044 omapfb_put_mem_region(ofbi->region);
1045
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001046 return r;
1047}
1048
1049static int omapfb_pan_display(struct fb_var_screeninfo *var,
1050 struct fb_info *fbi)
1051{
Ville Syrjälä430571d2010-03-17 20:43:23 +02001052 struct omapfb_info *ofbi = FB2OFB(fbi);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001053 struct fb_var_screeninfo new_var;
1054 int r;
1055
1056 DBG("pan_display(%d)\n", FB2OFB(fbi)->id);
1057
1058 if (var->xoffset == fbi->var.xoffset &&
1059 var->yoffset == fbi->var.yoffset)
1060 return 0;
1061
1062 new_var = fbi->var;
1063 new_var.xoffset = var->xoffset;
1064 new_var.yoffset = var->yoffset;
1065
1066 fbi->var = new_var;
1067
Ville Syrjälä430571d2010-03-17 20:43:23 +02001068 omapfb_get_mem_region(ofbi->region);
1069
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001070 r = omapfb_apply_changes(fbi, 0);
1071
Ville Syrjälä430571d2010-03-17 20:43:23 +02001072 omapfb_put_mem_region(ofbi->region);
1073
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001074 return r;
1075}
1076
1077static void mmap_user_open(struct vm_area_struct *vma)
1078{
Ville Syrjälä078ff542010-03-17 20:36:51 +02001079 struct omapfb2_mem_region *rg = vma->vm_private_data;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001080
Ville Syrjälä430571d2010-03-17 20:43:23 +02001081 omapfb_get_mem_region(rg);
Ville Syrjälä078ff542010-03-17 20:36:51 +02001082 atomic_inc(&rg->map_count);
Ville Syrjälä430571d2010-03-17 20:43:23 +02001083 omapfb_put_mem_region(rg);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001084}
1085
1086static void mmap_user_close(struct vm_area_struct *vma)
1087{
Ville Syrjälä078ff542010-03-17 20:36:51 +02001088 struct omapfb2_mem_region *rg = vma->vm_private_data;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001089
Ville Syrjälä430571d2010-03-17 20:43:23 +02001090 omapfb_get_mem_region(rg);
Ville Syrjälä078ff542010-03-17 20:36:51 +02001091 atomic_dec(&rg->map_count);
Ville Syrjälä430571d2010-03-17 20:43:23 +02001092 omapfb_put_mem_region(rg);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001093}
1094
1095static struct vm_operations_struct mmap_user_ops = {
1096 .open = mmap_user_open,
1097 .close = mmap_user_close,
1098};
1099
1100static int omapfb_mmap(struct fb_info *fbi, struct vm_area_struct *vma)
1101{
1102 struct omapfb_info *ofbi = FB2OFB(fbi);
1103 struct fb_fix_screeninfo *fix = &fbi->fix;
Ville Syrjälä078ff542010-03-17 20:36:51 +02001104 struct omapfb2_mem_region *rg;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001105 unsigned long off;
1106 unsigned long start;
1107 u32 len;
Ville Syrjälä430571d2010-03-17 20:43:23 +02001108 int r = -EINVAL;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001109
1110 if (vma->vm_end - vma->vm_start == 0)
1111 return 0;
1112 if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
1113 return -EINVAL;
1114 off = vma->vm_pgoff << PAGE_SHIFT;
1115
Ville Syrjälä430571d2010-03-17 20:43:23 +02001116 rg = omapfb_get_mem_region(ofbi->region);
Ville Syrjälä078ff542010-03-17 20:36:51 +02001117
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001118 start = omapfb_get_region_paddr(ofbi);
1119 len = fix->smem_len;
1120 if (off >= len)
Ville Syrjälä430571d2010-03-17 20:43:23 +02001121 goto error;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001122 if ((vma->vm_end - vma->vm_start + off) > len)
Ville Syrjälä430571d2010-03-17 20:43:23 +02001123 goto error;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001124
1125 off += start;
1126
1127 DBG("user mmap region start %lx, len %d, off %lx\n", start, len, off);
1128
1129 vma->vm_pgoff = off >> PAGE_SHIFT;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07001130 /* VM_IO | VM_DONTEXPAND | VM_DONTDUMP are set by remap_pfn_range() */
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001131 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
1132 vma->vm_ops = &mmap_user_ops;
Ville Syrjälä078ff542010-03-17 20:36:51 +02001133 vma->vm_private_data = rg;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001134 if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
Ville Syrjälä430571d2010-03-17 20:43:23 +02001135 vma->vm_end - vma->vm_start,
1136 vma->vm_page_prot)) {
1137 r = -EAGAIN;
1138 goto error;
1139 }
1140
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001141 /* vm_ops.open won't be called for mmap itself. */
Ville Syrjälä078ff542010-03-17 20:36:51 +02001142 atomic_inc(&rg->map_count);
Ville Syrjälä430571d2010-03-17 20:43:23 +02001143
1144 omapfb_put_mem_region(rg);
1145
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001146 return 0;
Ville Syrjälä430571d2010-03-17 20:43:23 +02001147
1148 error:
1149 omapfb_put_mem_region(ofbi->region);
1150
1151 return r;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001152}
1153
1154/* Store a single color palette entry into a pseudo palette or the hardware
1155 * palette if one is available. For now we support only 16bpp and thus store
1156 * the entry only to the pseudo palette.
1157 */
1158static int _setcolreg(struct fb_info *fbi, u_int regno, u_int red, u_int green,
1159 u_int blue, u_int transp, int update_hw_pal)
1160{
1161 /*struct omapfb_info *ofbi = FB2OFB(fbi);*/
1162 /*struct omapfb2_device *fbdev = ofbi->fbdev;*/
1163 struct fb_var_screeninfo *var = &fbi->var;
1164 int r = 0;
1165
1166 enum omapfb_color_format mode = OMAPFB_COLOR_RGB24U; /* XXX */
1167
1168 /*switch (plane->color_mode) {*/
1169 switch (mode) {
1170 case OMAPFB_COLOR_YUV422:
1171 case OMAPFB_COLOR_YUV420:
1172 case OMAPFB_COLOR_YUY422:
1173 r = -EINVAL;
1174 break;
1175 case OMAPFB_COLOR_CLUT_8BPP:
1176 case OMAPFB_COLOR_CLUT_4BPP:
1177 case OMAPFB_COLOR_CLUT_2BPP:
1178 case OMAPFB_COLOR_CLUT_1BPP:
1179 /*
1180 if (fbdev->ctrl->setcolreg)
1181 r = fbdev->ctrl->setcolreg(regno, red, green, blue,
1182 transp, update_hw_pal);
1183 */
1184 /* Fallthrough */
1185 r = -EINVAL;
1186 break;
1187 case OMAPFB_COLOR_RGB565:
1188 case OMAPFB_COLOR_RGB444:
1189 case OMAPFB_COLOR_RGB24P:
1190 case OMAPFB_COLOR_RGB24U:
1191 if (r != 0)
1192 break;
1193
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001194 if (regno < 16) {
Grazvydas Ignotasc1c52842012-08-21 09:09:48 +03001195 u32 pal;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001196 pal = ((red >> (16 - var->red.length)) <<
1197 var->red.offset) |
1198 ((green >> (16 - var->green.length)) <<
1199 var->green.offset) |
1200 (blue >> (16 - var->blue.length));
1201 ((u32 *)(fbi->pseudo_palette))[regno] = pal;
1202 }
1203 break;
1204 default:
1205 BUG();
1206 }
1207 return r;
1208}
1209
1210static int omapfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
1211 u_int transp, struct fb_info *info)
1212{
1213 DBG("setcolreg\n");
1214
1215 return _setcolreg(info, regno, red, green, blue, transp, 1);
1216}
1217
1218static int omapfb_setcmap(struct fb_cmap *cmap, struct fb_info *info)
1219{
1220 int count, index, r;
1221 u16 *red, *green, *blue, *transp;
1222 u16 trans = 0xffff;
1223
1224 DBG("setcmap\n");
1225
1226 red = cmap->red;
1227 green = cmap->green;
1228 blue = cmap->blue;
1229 transp = cmap->transp;
1230 index = cmap->start;
1231
1232 for (count = 0; count < cmap->len; count++) {
1233 if (transp)
1234 trans = *transp++;
1235 r = _setcolreg(info, index++, *red++, *green++, *blue++, trans,
1236 count == cmap->len - 1);
1237 if (r != 0)
1238 return r;
1239 }
1240
1241 return 0;
1242}
1243
1244static int omapfb_blank(int blank, struct fb_info *fbi)
1245{
1246 struct omapfb_info *ofbi = FB2OFB(fbi);
1247 struct omapfb2_device *fbdev = ofbi->fbdev;
1248 struct omap_dss_device *display = fb2display(fbi);
Tomi Valkeinen27cc2132011-04-30 16:55:12 +03001249 struct omapfb_display_data *d;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001250 int r = 0;
1251
Jani Nikula93255882010-06-01 15:12:12 +03001252 if (!display)
1253 return -EINVAL;
1254
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001255 omapfb_lock(fbdev);
1256
Tomi Valkeinen27cc2132011-04-30 16:55:12 +03001257 d = get_display_data(fbdev, display);
1258
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001259 switch (blank) {
1260 case FB_BLANK_UNBLANK:
1261 if (display->state != OMAP_DSS_DISPLAY_SUSPENDED)
1262 goto exit;
1263
Tomi Valkeinen37ac60e2010-01-12 15:12:07 +02001264 if (display->driver->resume)
1265 r = display->driver->resume(display);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001266
Tomi Valkeinen27cc2132011-04-30 16:55:12 +03001267 if ((display->caps & OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE) &&
1268 d->update_mode == OMAPFB_AUTO_UPDATE &&
1269 !d->auto_update_work_enabled)
1270 omapfb_start_auto_update(fbdev, display);
1271
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001272 break;
1273
1274 case FB_BLANK_NORMAL:
1275 /* FB_BLANK_NORMAL could be implemented.
1276 * Needs DSS additions. */
1277 case FB_BLANK_VSYNC_SUSPEND:
1278 case FB_BLANK_HSYNC_SUSPEND:
1279 case FB_BLANK_POWERDOWN:
1280 if (display->state != OMAP_DSS_DISPLAY_ACTIVE)
1281 goto exit;
1282
Tomi Valkeinen27cc2132011-04-30 16:55:12 +03001283 if (d->auto_update_work_enabled)
1284 omapfb_stop_auto_update(fbdev, display);
1285
Tomi Valkeinen37ac60e2010-01-12 15:12:07 +02001286 if (display->driver->suspend)
1287 r = display->driver->suspend(display);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001288
1289 break;
1290
1291 default:
1292 r = -EINVAL;
1293 }
1294
1295exit:
1296 omapfb_unlock(fbdev);
1297
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001298 return r;
1299}
1300
1301#if 0
1302/* XXX fb_read and fb_write are needed for VRFB */
1303ssize_t omapfb_write(struct fb_info *info, const char __user *buf,
1304 size_t count, loff_t *ppos)
1305{
1306 DBG("omapfb_write %d, %lu\n", count, (unsigned long)*ppos);
1307 /* XXX needed for VRFB */
1308 return count;
1309}
1310#endif
1311
1312static struct fb_ops omapfb_ops = {
1313 .owner = THIS_MODULE,
1314 .fb_open = omapfb_open,
1315 .fb_release = omapfb_release,
1316 .fb_fillrect = cfb_fillrect,
1317 .fb_copyarea = cfb_copyarea,
1318 .fb_imageblit = cfb_imageblit,
1319 .fb_blank = omapfb_blank,
1320 .fb_ioctl = omapfb_ioctl,
1321 .fb_check_var = omapfb_check_var,
1322 .fb_set_par = omapfb_set_par,
1323 .fb_pan_display = omapfb_pan_display,
1324 .fb_mmap = omapfb_mmap,
1325 .fb_setcolreg = omapfb_setcolreg,
1326 .fb_setcmap = omapfb_setcmap,
1327 /*.fb_write = omapfb_write,*/
1328};
1329
1330static void omapfb_free_fbmem(struct fb_info *fbi)
1331{
1332 struct omapfb_info *ofbi = FB2OFB(fbi);
1333 struct omapfb2_device *fbdev = ofbi->fbdev;
1334 struct omapfb2_mem_region *rg;
1335
Ville Syrjälä078ff542010-03-17 20:36:51 +02001336 rg = ofbi->region;
1337
Tomi Valkeinen0049fb22012-11-09 15:52:20 +02001338 if (rg->token == NULL)
1339 return;
1340
Ville Syrjälä078ff542010-03-17 20:36:51 +02001341 WARN_ON(atomic_read(&rg->map_count));
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001342
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001343 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
1344 /* unmap the 0 angle rotation */
1345 if (rg->vrfb.vaddr[0]) {
1346 iounmap(rg->vrfb.vaddr[0]);
Tomi Valkeinenf3a82d12010-01-07 13:37:30 +02001347 rg->vrfb.vaddr[0] = NULL;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001348 }
Tomi Valkeinen0049fb22012-11-09 15:52:20 +02001349
1350 omap_vrfb_release_ctx(&rg->vrfb);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001351 }
1352
Tomi Valkeinen0049fb22012-11-09 15:52:20 +02001353 dma_free_attrs(fbdev->dev, rg->size, rg->token, rg->dma_handle,
1354 &rg->attrs);
1355
1356 rg->token = NULL;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001357 rg->vaddr = NULL;
1358 rg->paddr = 0;
1359 rg->alloc = 0;
1360 rg->size = 0;
1361}
1362
1363static void clear_fb_info(struct fb_info *fbi)
1364{
1365 memset(&fbi->var, 0, sizeof(fbi->var));
1366 memset(&fbi->fix, 0, sizeof(fbi->fix));
1367 strlcpy(fbi->fix.id, MODULE_NAME, sizeof(fbi->fix.id));
1368}
1369
1370static int omapfb_free_all_fbmem(struct omapfb2_device *fbdev)
1371{
1372 int i;
1373
1374 DBG("free all fbmem\n");
1375
1376 for (i = 0; i < fbdev->num_fbs; i++) {
1377 struct fb_info *fbi = fbdev->fbs[i];
1378 omapfb_free_fbmem(fbi);
1379 clear_fb_info(fbi);
1380 }
1381
1382 return 0;
1383}
1384
1385static int omapfb_alloc_fbmem(struct fb_info *fbi, unsigned long size,
1386 unsigned long paddr)
1387{
1388 struct omapfb_info *ofbi = FB2OFB(fbi);
1389 struct omapfb2_device *fbdev = ofbi->fbdev;
1390 struct omapfb2_mem_region *rg;
Tomi Valkeinen0049fb22012-11-09 15:52:20 +02001391 void *token;
1392 DEFINE_DMA_ATTRS(attrs);
1393 dma_addr_t dma_handle;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001394 int r;
1395
Ville Syrjälä078ff542010-03-17 20:36:51 +02001396 rg = ofbi->region;
1397
1398 rg->paddr = 0;
1399 rg->vaddr = NULL;
1400 memset(&rg->vrfb, 0, sizeof rg->vrfb);
1401 rg->size = 0;
1402 rg->type = 0;
1403 rg->alloc = false;
1404 rg->map = false;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001405
1406 size = PAGE_ALIGN(size);
1407
Tomi Valkeinen0049fb22012-11-09 15:52:20 +02001408 WARN_ONCE(paddr,
1409 "reserving memory at predefined address not supported\n");
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001410
Tomi Valkeinen0049fb22012-11-09 15:52:20 +02001411 dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs);
1412
1413 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
1414 dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &attrs);
1415
1416 DBG("allocating %lu bytes for fb %d\n", size, ofbi->id);
1417
1418 token = dma_alloc_attrs(fbdev->dev, size, &dma_handle,
1419 GFP_KERNEL, &attrs);
1420
1421 if (token == NULL) {
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001422 dev_err(fbdev->dev, "failed to allocate framebuffer\n");
1423 return -ENOMEM;
1424 }
1425
Tomi Valkeinen0049fb22012-11-09 15:52:20 +02001426 DBG("allocated VRAM paddr %lx, vaddr %p\n",
1427 (unsigned long)dma_handle, token);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001428
Tomi Valkeinen0049fb22012-11-09 15:52:20 +02001429 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001430 r = omap_vrfb_request_ctx(&rg->vrfb);
1431 if (r) {
Tomi Valkeinen0049fb22012-11-09 15:52:20 +02001432 dma_free_attrs(fbdev->dev, size, token, dma_handle,
1433 &attrs);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001434 dev_err(fbdev->dev, "vrfb create ctx failed\n");
1435 return r;
1436 }
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001437 }
1438
Tomi Valkeinen0049fb22012-11-09 15:52:20 +02001439 rg->attrs = attrs;
1440 rg->token = token;
1441 rg->dma_handle = dma_handle;
1442
1443 rg->paddr = (unsigned long)dma_handle;
1444 rg->vaddr = (void __iomem *)token;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001445 rg->size = size;
1446 rg->alloc = 1;
1447
1448 return 0;
1449}
1450
1451/* allocate fbmem using display resolution as reference */
1452static int omapfb_alloc_fbmem_display(struct fb_info *fbi, unsigned long size,
1453 unsigned long paddr)
1454{
1455 struct omapfb_info *ofbi = FB2OFB(fbi);
Tomi Valkeinena2699502010-01-11 14:33:40 +02001456 struct omapfb2_device *fbdev = ofbi->fbdev;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001457 struct omap_dss_device *display;
1458 int bytespp;
1459
1460 display = fb2display(fbi);
1461
1462 if (!display)
1463 return 0;
1464
Tomi Valkeinena2699502010-01-11 14:33:40 +02001465 switch (omapfb_get_recommended_bpp(fbdev, display)) {
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001466 case 16:
1467 bytespp = 2;
1468 break;
1469 case 24:
1470 bytespp = 4;
1471 break;
1472 default:
1473 bytespp = 4;
1474 break;
1475 }
1476
1477 if (!size) {
1478 u16 w, h;
1479
Tomi Valkeinen96adcec2010-01-11 13:54:33 +02001480 display->driver->get_resolution(display, &w, &h);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001481
1482 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
1483 size = max(omap_vrfb_min_phys_size(w, h, bytespp),
1484 omap_vrfb_min_phys_size(h, w, bytespp));
1485
1486 DBG("adjusting fb mem size for VRFB, %u -> %lu\n",
1487 w * h * bytespp, size);
1488 } else {
1489 size = w * h * bytespp;
1490 }
1491 }
1492
1493 if (!size)
1494 return 0;
1495
1496 return omapfb_alloc_fbmem(fbi, size, paddr);
1497}
1498
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001499static int omapfb_parse_vram_param(const char *param, int max_entries,
1500 unsigned long *sizes, unsigned long *paddrs)
1501{
1502 int fbnum;
1503 unsigned long size;
1504 unsigned long paddr = 0;
1505 char *p, *start;
1506
1507 start = (char *)param;
1508
1509 while (1) {
1510 p = start;
1511
1512 fbnum = simple_strtoul(p, &p, 10);
1513
Tomi Valkeinen3a028bb2012-05-10 11:15:14 +03001514 if (p == start)
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001515 return -EINVAL;
1516
1517 if (*p != ':')
1518 return -EINVAL;
1519
1520 if (fbnum >= max_entries)
1521 return -EINVAL;
1522
1523 size = memparse(p + 1, &p);
1524
1525 if (!size)
1526 return -EINVAL;
1527
1528 paddr = 0;
1529
1530 if (*p == '@') {
1531 paddr = simple_strtoul(p + 1, &p, 16);
1532
1533 if (!paddr)
1534 return -EINVAL;
1535
1536 }
1537
1538 paddrs[fbnum] = paddr;
1539 sizes[fbnum] = size;
1540
1541 if (*p == 0)
1542 break;
1543
1544 if (*p != ',')
1545 return -EINVAL;
1546
1547 ++p;
1548
1549 start = p;
1550 }
1551
1552 return 0;
1553}
1554
1555static int omapfb_allocate_all_fbs(struct omapfb2_device *fbdev)
1556{
1557 int i, r;
1558 unsigned long vram_sizes[10];
1559 unsigned long vram_paddrs[10];
1560
1561 memset(&vram_sizes, 0, sizeof(vram_sizes));
1562 memset(&vram_paddrs, 0, sizeof(vram_paddrs));
1563
1564 if (def_vram && omapfb_parse_vram_param(def_vram, 10,
1565 vram_sizes, vram_paddrs)) {
1566 dev_err(fbdev->dev, "failed to parse vram parameter\n");
1567
1568 memset(&vram_sizes, 0, sizeof(vram_sizes));
1569 memset(&vram_paddrs, 0, sizeof(vram_paddrs));
1570 }
1571
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001572 for (i = 0; i < fbdev->num_fbs; i++) {
1573 /* allocate memory automatically only for fb0, or if
1574 * excplicitly defined with vram or plat data option */
1575 if (i == 0 || vram_sizes[i] != 0) {
1576 r = omapfb_alloc_fbmem_display(fbdev->fbs[i],
1577 vram_sizes[i], vram_paddrs[i]);
1578
1579 if (r)
1580 return r;
1581 }
1582 }
1583
1584 for (i = 0; i < fbdev->num_fbs; i++) {
1585 struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[i]);
1586 struct omapfb2_mem_region *rg;
Ville Syrjälä078ff542010-03-17 20:36:51 +02001587 rg = ofbi->region;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001588
1589 DBG("region%d phys %08x virt %p size=%lu\n",
1590 i,
1591 rg->paddr,
1592 rg->vaddr,
1593 rg->size);
1594 }
1595
1596 return 0;
1597}
1598
Tomi Valkeinenca444152012-08-30 16:53:29 +03001599static void omapfb_clear_fb(struct fb_info *fbi)
1600{
1601 const struct fb_fillrect rect = {
1602 .dx = 0,
1603 .dy = 0,
1604 .width = fbi->var.xres_virtual,
1605 .height = fbi->var.yres_virtual,
1606 .color = 0,
1607 .rop = ROP_COPY,
1608 };
1609
1610 cfb_fillrect(fbi, &rect);
1611}
1612
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001613int omapfb_realloc_fbmem(struct fb_info *fbi, unsigned long size, int type)
1614{
1615 struct omapfb_info *ofbi = FB2OFB(fbi);
1616 struct omapfb2_device *fbdev = ofbi->fbdev;
1617 struct omap_dss_device *display = fb2display(fbi);
Ville Syrjälä078ff542010-03-17 20:36:51 +02001618 struct omapfb2_mem_region *rg = ofbi->region;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001619 unsigned long old_size = rg->size;
1620 unsigned long old_paddr = rg->paddr;
1621 int old_type = rg->type;
1622 int r;
1623
Tomi Valkeinen2a803c82011-04-18 13:18:20 +03001624 if (type != OMAPFB_MEMTYPE_SDRAM)
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001625 return -EINVAL;
1626
1627 size = PAGE_ALIGN(size);
1628
1629 if (old_size == size && old_type == type)
1630 return 0;
1631
Tomi Valkeinen18946f62010-01-12 14:16:41 +02001632 if (display && display->driver->sync)
1633 display->driver->sync(display);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001634
1635 omapfb_free_fbmem(fbi);
1636
1637 if (size == 0) {
1638 clear_fb_info(fbi);
1639 return 0;
1640 }
1641
1642 r = omapfb_alloc_fbmem(fbi, size, 0);
1643
1644 if (r) {
1645 if (old_size)
1646 omapfb_alloc_fbmem(fbi, old_size, old_paddr);
1647
1648 if (rg->size == 0)
1649 clear_fb_info(fbi);
1650
1651 return r;
1652 }
1653
1654 if (old_size == size)
1655 return 0;
1656
1657 if (old_size == 0) {
1658 DBG("initializing fb %d\n", ofbi->id);
1659 r = omapfb_fb_init(fbdev, fbi);
1660 if (r) {
1661 DBG("omapfb_fb_init failed\n");
1662 goto err;
1663 }
1664 r = omapfb_apply_changes(fbi, 1);
1665 if (r) {
1666 DBG("omapfb_apply_changes failed\n");
1667 goto err;
1668 }
1669 } else {
1670 struct fb_var_screeninfo new_var;
1671 memcpy(&new_var, &fbi->var, sizeof(new_var));
1672 r = check_fb_var(fbi, &new_var);
1673 if (r)
1674 goto err;
1675 memcpy(&fbi->var, &new_var, sizeof(fbi->var));
1676 set_fb_fix(fbi);
1677 r = setup_vrfb_rotation(fbi);
1678 if (r)
1679 goto err;
1680 }
1681
Tomi Valkeinenca444152012-08-30 16:53:29 +03001682 omapfb_clear_fb(fbi);
1683
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001684 return 0;
1685err:
1686 omapfb_free_fbmem(fbi);
1687 clear_fb_info(fbi);
1688 return r;
1689}
1690
Tomi Valkeinen27cc2132011-04-30 16:55:12 +03001691static void omapfb_auto_update_work(struct work_struct *work)
1692{
1693 struct omap_dss_device *dssdev;
1694 struct omap_dss_driver *dssdrv;
1695 struct omapfb_display_data *d;
1696 u16 w, h;
1697 unsigned int freq;
1698 struct omapfb2_device *fbdev;
1699
1700 d = container_of(work, struct omapfb_display_data,
1701 auto_update_work.work);
1702
1703 dssdev = d->dssdev;
1704 dssdrv = dssdev->driver;
1705 fbdev = d->fbdev;
1706
1707 if (!dssdrv || !dssdrv->update)
1708 return;
1709
1710 if (dssdrv->sync)
1711 dssdrv->sync(dssdev);
1712
1713 dssdrv->get_resolution(dssdev, &w, &h);
1714 dssdrv->update(dssdev, 0, 0, w, h);
1715
1716 freq = auto_update_freq;
1717 if (freq == 0)
1718 freq = 20;
1719 queue_delayed_work(fbdev->auto_update_wq,
1720 &d->auto_update_work, HZ / freq);
1721}
1722
1723void omapfb_start_auto_update(struct omapfb2_device *fbdev,
1724 struct omap_dss_device *display)
1725{
1726 struct omapfb_display_data *d;
1727
1728 if (fbdev->auto_update_wq == NULL) {
1729 struct workqueue_struct *wq;
1730
1731 wq = create_singlethread_workqueue("omapfb_auto_update");
1732
1733 if (wq == NULL) {
1734 dev_err(fbdev->dev, "Failed to create workqueue for "
1735 "auto-update\n");
1736 return;
1737 }
1738
1739 fbdev->auto_update_wq = wq;
1740 }
1741
1742 d = get_display_data(fbdev, display);
1743
1744 INIT_DELAYED_WORK(&d->auto_update_work, omapfb_auto_update_work);
1745
1746 d->auto_update_work_enabled = true;
1747
1748 omapfb_auto_update_work(&d->auto_update_work.work);
1749}
1750
1751void omapfb_stop_auto_update(struct omapfb2_device *fbdev,
1752 struct omap_dss_device *display)
1753{
1754 struct omapfb_display_data *d;
1755
1756 d = get_display_data(fbdev, display);
1757
1758 cancel_delayed_work_sync(&d->auto_update_work);
1759
1760 d->auto_update_work_enabled = false;
1761}
1762
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001763/* initialize fb_info, var, fix to something sane based on the display */
1764static int omapfb_fb_init(struct omapfb2_device *fbdev, struct fb_info *fbi)
1765{
1766 struct fb_var_screeninfo *var = &fbi->var;
1767 struct omap_dss_device *display = fb2display(fbi);
1768 struct omapfb_info *ofbi = FB2OFB(fbi);
1769 int r = 0;
1770
1771 fbi->fbops = &omapfb_ops;
1772 fbi->flags = FBINFO_FLAG_DEFAULT;
1773 fbi->pseudo_palette = fbdev->pseudo_palette;
1774
Ville Syrjälä078ff542010-03-17 20:36:51 +02001775 if (ofbi->region->size == 0) {
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001776 clear_fb_info(fbi);
1777 return 0;
1778 }
1779
1780 var->nonstd = 0;
1781 var->bits_per_pixel = 0;
1782
1783 var->rotate = def_rotate;
1784
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001785 if (display) {
1786 u16 w, h;
1787 int rotation = (var->rotate + ofbi->rotation[0]) % 4;
1788
Tomi Valkeinen96adcec2010-01-11 13:54:33 +02001789 display->driver->get_resolution(display, &w, &h);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001790
1791 if (rotation == FB_ROTATE_CW ||
1792 rotation == FB_ROTATE_CCW) {
1793 var->xres = h;
1794 var->yres = w;
1795 } else {
1796 var->xres = w;
1797 var->yres = h;
1798 }
1799
1800 var->xres_virtual = var->xres;
1801 var->yres_virtual = var->yres;
1802
1803 if (!var->bits_per_pixel) {
Tomi Valkeinena2699502010-01-11 14:33:40 +02001804 switch (omapfb_get_recommended_bpp(fbdev, display)) {
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001805 case 16:
1806 var->bits_per_pixel = 16;
1807 break;
1808 case 24:
1809 var->bits_per_pixel = 32;
1810 break;
1811 default:
1812 dev_err(fbdev->dev, "illegal display "
1813 "bpp\n");
1814 return -EINVAL;
1815 }
1816 }
1817 } else {
1818 /* if there's no display, let's just guess some basic values */
1819 var->xres = 320;
1820 var->yres = 240;
1821 var->xres_virtual = var->xres;
1822 var->yres_virtual = var->yres;
1823 if (!var->bits_per_pixel)
1824 var->bits_per_pixel = 16;
1825 }
1826
1827 r = check_fb_var(fbi, var);
1828 if (r)
1829 goto err;
1830
1831 set_fb_fix(fbi);
1832 r = setup_vrfb_rotation(fbi);
1833 if (r)
1834 goto err;
1835
1836 r = fb_alloc_cmap(&fbi->cmap, 256, 0);
1837 if (r)
1838 dev_err(fbdev->dev, "unable to allocate color map memory\n");
1839
1840err:
1841 return r;
1842}
1843
1844static void fbinfo_cleanup(struct omapfb2_device *fbdev, struct fb_info *fbi)
1845{
1846 fb_dealloc_cmap(&fbi->cmap);
1847}
1848
1849
1850static void omapfb_free_resources(struct omapfb2_device *fbdev)
1851{
1852 int i;
1853
1854 DBG("free_resources\n");
1855
1856 if (fbdev == NULL)
1857 return;
1858
1859 for (i = 0; i < fbdev->num_fbs; i++)
1860 unregister_framebuffer(fbdev->fbs[i]);
1861
1862 /* free the reserved fbmem */
1863 omapfb_free_all_fbmem(fbdev);
1864
1865 for (i = 0; i < fbdev->num_fbs; i++) {
1866 fbinfo_cleanup(fbdev, fbdev->fbs[i]);
1867 framebuffer_release(fbdev->fbs[i]);
1868 }
1869
1870 for (i = 0; i < fbdev->num_displays; i++) {
Tomi Valkeinen065a40b2011-04-30 12:13:14 +03001871 struct omap_dss_device *dssdev = fbdev->displays[i].dssdev;
Tomi Valkeinen27cc2132011-04-30 16:55:12 +03001872
1873 if (fbdev->displays[i].auto_update_work_enabled)
1874 omapfb_stop_auto_update(fbdev, dssdev);
1875
Tomi Valkeinen065a40b2011-04-30 12:13:14 +03001876 if (dssdev->state != OMAP_DSS_DISPLAY_DISABLED)
1877 dssdev->driver->disable(dssdev);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001878
Tomi Valkeinen065a40b2011-04-30 12:13:14 +03001879 omap_dss_put_device(dssdev);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001880 }
1881
Tomi Valkeinen27cc2132011-04-30 16:55:12 +03001882 if (fbdev->auto_update_wq != NULL) {
1883 flush_workqueue(fbdev->auto_update_wq);
1884 destroy_workqueue(fbdev->auto_update_wq);
1885 fbdev->auto_update_wq = NULL;
1886 }
1887
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001888 dev_set_drvdata(fbdev->dev, NULL);
1889 kfree(fbdev);
1890}
1891
1892static int omapfb_create_framebuffers(struct omapfb2_device *fbdev)
1893{
1894 int r, i;
1895
1896 fbdev->num_fbs = 0;
1897
1898 DBG("create %d framebuffers\n", CONFIG_FB_OMAP2_NUM_FBS);
1899
1900 /* allocate fb_infos */
1901 for (i = 0; i < CONFIG_FB_OMAP2_NUM_FBS; i++) {
1902 struct fb_info *fbi;
1903 struct omapfb_info *ofbi;
1904
1905 fbi = framebuffer_alloc(sizeof(struct omapfb_info),
1906 fbdev->dev);
1907
1908 if (fbi == NULL) {
1909 dev_err(fbdev->dev,
1910 "unable to allocate memory for plane info\n");
1911 return -ENOMEM;
1912 }
1913
1914 clear_fb_info(fbi);
1915
1916 fbdev->fbs[i] = fbi;
1917
1918 ofbi = FB2OFB(fbi);
1919 ofbi->fbdev = fbdev;
1920 ofbi->id = i;
1921
Ville Syrjälä078ff542010-03-17 20:36:51 +02001922 ofbi->region = &fbdev->regions[i];
1923 ofbi->region->id = i;
Ville Syrjälä2f642a12010-03-17 20:58:03 +02001924 init_rwsem(&ofbi->region->lock);
Ville Syrjälä078ff542010-03-17 20:36:51 +02001925
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001926 /* assign these early, so that fb alloc can use them */
1927 ofbi->rotation_type = def_vrfb ? OMAP_DSS_ROT_VRFB :
1928 OMAP_DSS_ROT_DMA;
1929 ofbi->mirror = def_mirror;
1930
1931 fbdev->num_fbs++;
1932 }
1933
1934 DBG("fb_infos allocated\n");
1935
1936 /* assign overlays for the fbs */
1937 for (i = 0; i < min(fbdev->num_fbs, fbdev->num_overlays); i++) {
1938 struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[i]);
1939
1940 ofbi->overlays[0] = fbdev->overlays[i];
1941 ofbi->num_overlays = 1;
1942 }
1943
1944 /* allocate fb memories */
1945 r = omapfb_allocate_all_fbs(fbdev);
1946 if (r) {
1947 dev_err(fbdev->dev, "failed to allocate fbmem\n");
1948 return r;
1949 }
1950
1951 DBG("fbmems allocated\n");
1952
1953 /* setup fb_infos */
1954 for (i = 0; i < fbdev->num_fbs; i++) {
Ville Syrjälä430571d2010-03-17 20:43:23 +02001955 struct fb_info *fbi = fbdev->fbs[i];
1956 struct omapfb_info *ofbi = FB2OFB(fbi);
1957
1958 omapfb_get_mem_region(ofbi->region);
1959 r = omapfb_fb_init(fbdev, fbi);
1960 omapfb_put_mem_region(ofbi->region);
1961
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001962 if (r) {
1963 dev_err(fbdev->dev, "failed to setup fb_info\n");
1964 return r;
1965 }
1966 }
1967
Archit Tanejafda7c362012-09-11 15:34:08 +05301968 for (i = 0; i < fbdev->num_fbs; i++) {
1969 struct fb_info *fbi = fbdev->fbs[i];
1970 struct omapfb_info *ofbi = FB2OFB(fbi);
1971
1972 if (ofbi->region->size == 0)
1973 continue;
1974
1975 omapfb_clear_fb(fbi);
1976 }
1977
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001978 DBG("fb_infos initialized\n");
1979
1980 for (i = 0; i < fbdev->num_fbs; i++) {
1981 r = register_framebuffer(fbdev->fbs[i]);
1982 if (r != 0) {
1983 dev_err(fbdev->dev,
1984 "registering framebuffer %d failed\n", i);
1985 return r;
1986 }
1987 }
1988
1989 DBG("framebuffers registered\n");
1990
1991 for (i = 0; i < fbdev->num_fbs; i++) {
Ville Syrjälä430571d2010-03-17 20:43:23 +02001992 struct fb_info *fbi = fbdev->fbs[i];
1993 struct omapfb_info *ofbi = FB2OFB(fbi);
1994
1995 omapfb_get_mem_region(ofbi->region);
1996 r = omapfb_apply_changes(fbi, 1);
1997 omapfb_put_mem_region(ofbi->region);
1998
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001999 if (r) {
2000 dev_err(fbdev->dev, "failed to change mode\n");
2001 return r;
2002 }
2003 }
2004
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002005 /* Enable fb0 */
2006 if (fbdev->num_fbs > 0) {
2007 struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[0]);
2008
2009 if (ofbi->num_overlays > 0) {
2010 struct omap_overlay *ovl = ofbi->overlays[0];
2011
Tomi Valkeinenaaa874a2011-11-15 16:37:53 +02002012 ovl->manager->apply(ovl->manager);
2013
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002014 r = omapfb_overlay_enable(ovl, 1);
2015
2016 if (r) {
2017 dev_err(fbdev->dev,
2018 "failed to enable overlay\n");
2019 return r;
2020 }
2021 }
2022 }
2023
2024 DBG("create_framebuffers done\n");
2025
2026 return 0;
2027}
2028
2029static int omapfb_mode_to_timings(const char *mode_str,
Archit Taneja783babf2012-06-15 07:39:47 +05302030 struct omap_dss_device *display,
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002031 struct omap_video_timings *timings, u8 *bpp)
2032{
Tomi Valkeinen897044e2011-04-30 15:26:40 +03002033 struct fb_info *fbi;
2034 struct fb_var_screeninfo *var;
2035 struct fb_ops *fbops;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002036 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
Tomi Valkeinen897044e2011-04-30 15:26:40 +03002053 *bpp = 0;
2054 fbi = NULL;
2055 var = NULL;
2056 fbops = NULL;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002057
Tomi Valkeinen897044e2011-04-30 15:26:40 +03002058 fbi = kzalloc(sizeof(*fbi), GFP_KERNEL);
2059 if (fbi == NULL) {
2060 r = -ENOMEM;
2061 goto err;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002062 }
Tomi Valkeinen897044e2011-04-30 15:26:40 +03002063
2064 var = kzalloc(sizeof(*var), GFP_KERNEL);
2065 if (var == NULL) {
2066 r = -ENOMEM;
2067 goto err;
2068 }
2069
2070 fbops = kzalloc(sizeof(*fbops), GFP_KERNEL);
2071 if (fbops == NULL) {
2072 r = -ENOMEM;
2073 goto err;
2074 }
2075
2076 fbi->fbops = fbops;
2077
2078 r = fb_find_mode(var, fbi, mode_str, NULL, 0, NULL, 24);
2079 if (r == 0) {
2080 r = -EINVAL;
2081 goto err;
2082 }
2083
Archit Taneja783babf2012-06-15 07:39:47 +05302084 if (display->driver->get_timings) {
2085 display->driver->get_timings(display, timings);
2086 } else {
2087 timings->data_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE;
2088 timings->de_level = OMAPDSS_SIG_ACTIVE_HIGH;
2089 timings->sync_pclk_edge = OMAPDSS_DRIVE_SIG_OPPOSITE_EDGES;
2090 }
2091
Tomi Valkeinen897044e2011-04-30 15:26:40 +03002092 timings->pixel_clock = PICOS2KHZ(var->pixclock);
2093 timings->hbp = var->left_margin;
2094 timings->hfp = var->right_margin;
2095 timings->vbp = var->upper_margin;
2096 timings->vfp = var->lower_margin;
2097 timings->hsw = var->hsync_len;
2098 timings->vsw = var->vsync_len;
2099 timings->x_res = var->xres;
2100 timings->y_res = var->yres;
Archit Taneja783babf2012-06-15 07:39:47 +05302101 timings->hsync_level = var->sync & FB_SYNC_HOR_HIGH_ACT ?
2102 OMAPDSS_SIG_ACTIVE_HIGH :
2103 OMAPDSS_SIG_ACTIVE_LOW;
2104 timings->vsync_level = var->sync & FB_SYNC_VERT_HIGH_ACT ?
2105 OMAPDSS_SIG_ACTIVE_HIGH :
2106 OMAPDSS_SIG_ACTIVE_LOW;
Archit Taneja23bae3a2012-06-28 18:23:18 +05302107 timings->interlace = var->vmode & FB_VMODE_INTERLACED;
Tomi Valkeinen897044e2011-04-30 15:26:40 +03002108
2109 switch (var->bits_per_pixel) {
2110 case 16:
2111 *bpp = 16;
2112 break;
2113 case 24:
2114 case 32:
2115 default:
2116 *bpp = 24;
2117 break;
2118 }
2119
2120 r = 0;
2121
2122err:
2123 kfree(fbi);
2124 kfree(var);
2125 kfree(fbops);
2126
2127 return r;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002128}
2129
Tomi Valkeinena2699502010-01-11 14:33:40 +02002130static int omapfb_set_def_mode(struct omapfb2_device *fbdev,
2131 struct omap_dss_device *display, char *mode_str)
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002132{
2133 int r;
2134 u8 bpp;
Janorkar, Mayuresh371e2082011-02-22 07:35:13 -06002135 struct omap_video_timings timings, temp_timings;
Tomi Valkeinen065a40b2011-04-30 12:13:14 +03002136 struct omapfb_display_data *d;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002137
Archit Taneja783babf2012-06-15 07:39:47 +05302138 r = omapfb_mode_to_timings(mode_str, display, &timings, &bpp);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002139 if (r)
2140 return r;
2141
Tomi Valkeinen065a40b2011-04-30 12:13:14 +03002142 d = get_display_data(fbdev, display);
2143 d->bpp_override = bpp;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002144
Janorkar, Mayuresh371e2082011-02-22 07:35:13 -06002145 if (display->driver->check_timings) {
2146 r = display->driver->check_timings(display, &timings);
2147 if (r)
2148 return r;
2149 } else {
2150 /* If check_timings is not present compare xres and yres */
2151 if (display->driver->get_timings) {
2152 display->driver->get_timings(display, &temp_timings);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002153
Janorkar, Mayuresh371e2082011-02-22 07:35:13 -06002154 if (temp_timings.x_res != timings.x_res ||
2155 temp_timings.y_res != timings.y_res)
2156 return -EINVAL;
2157 }
2158 }
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002159
Janorkar, Mayuresh371e2082011-02-22 07:35:13 -06002160 if (display->driver->set_timings)
2161 display->driver->set_timings(display, &timings);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002162
2163 return 0;
2164}
2165
Tomi Valkeinena2699502010-01-11 14:33:40 +02002166static int omapfb_get_recommended_bpp(struct omapfb2_device *fbdev,
2167 struct omap_dss_device *dssdev)
2168{
Tomi Valkeinen065a40b2011-04-30 12:13:14 +03002169 struct omapfb_display_data *d;
Tomi Valkeinena2699502010-01-11 14:33:40 +02002170
2171 BUG_ON(dssdev->driver->get_recommended_bpp == NULL);
2172
Tomi Valkeinen065a40b2011-04-30 12:13:14 +03002173 d = get_display_data(fbdev, dssdev);
2174
2175 if (d->bpp_override != 0)
2176 return d->bpp_override;
Tomi Valkeinena2699502010-01-11 14:33:40 +02002177
2178 return dssdev->driver->get_recommended_bpp(dssdev);
2179}
2180
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002181static int omapfb_parse_def_modes(struct omapfb2_device *fbdev)
2182{
2183 char *str, *options, *this_opt;
2184 int r = 0;
2185
Samreen36e8c272010-11-16 12:49:07 +01002186 str = kstrdup(def_mode, GFP_KERNEL);
2187 if (!str)
2188 return -ENOMEM;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002189 options = str;
2190
2191 while (!r && (this_opt = strsep(&options, ",")) != NULL) {
2192 char *p, *display_str, *mode_str;
2193 struct omap_dss_device *display;
2194 int i;
2195
2196 p = strchr(this_opt, ':');
2197 if (!p) {
2198 r = -EINVAL;
2199 break;
2200 }
2201
2202 *p = 0;
2203 display_str = this_opt;
2204 mode_str = p + 1;
2205
2206 display = NULL;
2207 for (i = 0; i < fbdev->num_displays; ++i) {
Tomi Valkeinen065a40b2011-04-30 12:13:14 +03002208 if (strcmp(fbdev->displays[i].dssdev->name,
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002209 display_str) == 0) {
Tomi Valkeinen065a40b2011-04-30 12:13:14 +03002210 display = fbdev->displays[i].dssdev;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002211 break;
2212 }
2213 }
2214
2215 if (!display) {
2216 r = -EINVAL;
2217 break;
2218 }
2219
Tomi Valkeinena2699502010-01-11 14:33:40 +02002220 r = omapfb_set_def_mode(fbdev, display, mode_str);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002221 if (r)
2222 break;
2223 }
2224
2225 kfree(str);
2226
2227 return r;
2228}
2229
Tomi Valkeinendc891fa2011-08-25 17:15:14 +03002230static void fb_videomode_to_omap_timings(struct fb_videomode *m,
Archit Taneja783babf2012-06-15 07:39:47 +05302231 struct omap_dss_device *display,
Tomi Valkeinendc891fa2011-08-25 17:15:14 +03002232 struct omap_video_timings *t)
2233{
Archit Taneja783babf2012-06-15 07:39:47 +05302234 if (display->driver->get_timings) {
2235 display->driver->get_timings(display, t);
2236 } else {
2237 t->data_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE;
2238 t->de_level = OMAPDSS_SIG_ACTIVE_HIGH;
2239 t->sync_pclk_edge = OMAPDSS_DRIVE_SIG_OPPOSITE_EDGES;
2240 }
2241
Tomi Valkeinendc891fa2011-08-25 17:15:14 +03002242 t->x_res = m->xres;
2243 t->y_res = m->yres;
2244 t->pixel_clock = PICOS2KHZ(m->pixclock);
2245 t->hsw = m->hsync_len;
2246 t->hfp = m->right_margin;
2247 t->hbp = m->left_margin;
2248 t->vsw = m->vsync_len;
2249 t->vfp = m->lower_margin;
2250 t->vbp = m->upper_margin;
Archit Taneja783babf2012-06-15 07:39:47 +05302251 t->hsync_level = m->sync & FB_SYNC_HOR_HIGH_ACT ?
2252 OMAPDSS_SIG_ACTIVE_HIGH :
2253 OMAPDSS_SIG_ACTIVE_LOW;
2254 t->vsync_level = m->sync & FB_SYNC_VERT_HIGH_ACT ?
2255 OMAPDSS_SIG_ACTIVE_HIGH :
2256 OMAPDSS_SIG_ACTIVE_LOW;
Archit Taneja23bae3a2012-06-28 18:23:18 +05302257 t->interlace = m->vmode & FB_VMODE_INTERLACED;
Tomi Valkeinendc891fa2011-08-25 17:15:14 +03002258}
2259
2260static int omapfb_find_best_mode(struct omap_dss_device *display,
2261 struct omap_video_timings *timings)
2262{
2263 struct fb_monspecs *specs;
2264 u8 *edid;
2265 int r, i, best_xres, best_idx, len;
2266
2267 if (!display->driver->read_edid)
2268 return -ENODEV;
2269
2270 len = 0x80 * 2;
2271 edid = kmalloc(len, GFP_KERNEL);
2272
2273 r = display->driver->read_edid(display, edid, len);
2274 if (r < 0)
2275 goto err1;
2276
2277 specs = kzalloc(sizeof(*specs), GFP_KERNEL);
2278
2279 fb_edid_to_monspecs(edid, specs);
2280
2281 if (edid[126] > 0)
2282 fb_edid_add_monspecs(edid + 0x80, specs);
2283
2284 best_xres = 0;
2285 best_idx = -1;
2286
2287 for (i = 0; i < specs->modedb_len; ++i) {
2288 struct fb_videomode *m;
2289 struct omap_video_timings t;
2290
2291 m = &specs->modedb[i];
2292
2293 if (m->pixclock == 0)
2294 continue;
2295
2296 /* skip repeated pixel modes */
2297 if (m->xres == 2880 || m->xres == 1440)
2298 continue;
2299
Archit Taneja783babf2012-06-15 07:39:47 +05302300 fb_videomode_to_omap_timings(m, display, &t);
Tomi Valkeinendc891fa2011-08-25 17:15:14 +03002301
2302 r = display->driver->check_timings(display, &t);
2303 if (r == 0 && best_xres < m->xres) {
2304 best_xres = m->xres;
2305 best_idx = i;
2306 }
2307 }
2308
2309 if (best_xres == 0) {
2310 r = -ENOENT;
2311 goto err2;
2312 }
2313
Archit Taneja783babf2012-06-15 07:39:47 +05302314 fb_videomode_to_omap_timings(&specs->modedb[best_idx], display,
2315 timings);
Tomi Valkeinendc891fa2011-08-25 17:15:14 +03002316
2317 r = 0;
2318
2319err2:
2320 fb_destroy_modedb(specs->modedb);
2321 kfree(specs);
2322err1:
2323 kfree(edid);
2324
2325 return r;
2326}
2327
Tomi Valkeinen91ac27a2010-09-23 11:18:44 +03002328static int omapfb_init_display(struct omapfb2_device *fbdev,
2329 struct omap_dss_device *dssdev)
2330{
2331 struct omap_dss_driver *dssdrv = dssdev->driver;
Tomi Valkeinen27cc2132011-04-30 16:55:12 +03002332 struct omapfb_display_data *d;
Tomi Valkeinen91ac27a2010-09-23 11:18:44 +03002333 int r;
2334
2335 r = dssdrv->enable(dssdev);
2336 if (r) {
2337 dev_warn(fbdev->dev, "Failed to enable display '%s'\n",
2338 dssdev->name);
2339 return r;
2340 }
2341
Tomi Valkeinen27cc2132011-04-30 16:55:12 +03002342 d = get_display_data(fbdev, dssdev);
2343
2344 d->fbdev = fbdev;
2345
Tomi Valkeinen91ac27a2010-09-23 11:18:44 +03002346 if (dssdev->caps & OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE) {
2347 u16 w, h;
Tomi Valkeinen27cc2132011-04-30 16:55:12 +03002348
2349 if (auto_update) {
2350 omapfb_start_auto_update(fbdev, dssdev);
2351 d->update_mode = OMAPFB_AUTO_UPDATE;
2352 } else {
2353 d->update_mode = OMAPFB_MANUAL_UPDATE;
2354 }
2355
Tomi Valkeinen91ac27a2010-09-23 11:18:44 +03002356 if (dssdrv->enable_te) {
2357 r = dssdrv->enable_te(dssdev, 1);
2358 if (r) {
2359 dev_err(fbdev->dev, "Failed to set TE\n");
2360 return r;
2361 }
2362 }
2363
Tomi Valkeinen91ac27a2010-09-23 11:18:44 +03002364 dssdrv->get_resolution(dssdev, &w, &h);
2365 r = dssdrv->update(dssdev, 0, 0, w, h);
2366 if (r) {
2367 dev_err(fbdev->dev,
2368 "Failed to update display\n");
2369 return r;
2370 }
2371 } else {
Tomi Valkeinen27cc2132011-04-30 16:55:12 +03002372 d->update_mode = OMAPFB_AUTO_UPDATE;
Tomi Valkeinen91ac27a2010-09-23 11:18:44 +03002373 }
2374
2375 return 0;
2376}
2377
Tomi Valkeinend64f14e2012-02-17 17:43:03 +02002378static int __init omapfb_probe(struct platform_device *pdev)
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002379{
2380 struct omapfb2_device *fbdev = NULL;
2381 int r = 0;
2382 int i;
2383 struct omap_overlay *ovl;
2384 struct omap_dss_device *def_display;
2385 struct omap_dss_device *dssdev;
Archit Taneja4249e612012-07-20 18:26:56 +05302386 struct omap_dss_device *ovl_device;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002387
2388 DBG("omapfb_probe\n");
2389
2390 if (pdev->num_resources != 0) {
2391 dev_err(&pdev->dev, "probed for an unknown device\n");
2392 r = -ENODEV;
2393 goto err0;
2394 }
2395
2396 fbdev = kzalloc(sizeof(struct omapfb2_device), GFP_KERNEL);
2397 if (fbdev == NULL) {
2398 r = -ENOMEM;
2399 goto err0;
2400 }
2401
Senthilvadivu Guruswamy41814cf2010-10-08 08:44:33 +02002402 /* TODO : Replace cpu check with omap_has_vrfb once HAS_FEATURE
2403 * available for OMAP2 and OMAP3
2404 */
2405 if (def_vrfb && !cpu_is_omap24xx() && !cpu_is_omap34xx()) {
2406 def_vrfb = 0;
2407 dev_warn(&pdev->dev, "VRFB is not supported on this hardware, "
2408 "ignoring the module parameter vrfb=y\n");
2409 }
2410
2411
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002412 mutex_init(&fbdev->mtx);
2413
2414 fbdev->dev = &pdev->dev;
2415 platform_set_drvdata(pdev, fbdev);
2416
Tomi Valkeinenb3f91eb2010-02-17 12:00:01 +02002417 r = 0;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002418 fbdev->num_displays = 0;
2419 dssdev = NULL;
2420 for_each_dss_dev(dssdev) {
Tomi Valkeinen27cc2132011-04-30 16:55:12 +03002421 struct omapfb_display_data *d;
2422
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002423 omap_dss_get_device(dssdev);
Tomi Valkeinenb3f91eb2010-02-17 12:00:01 +02002424
Tomi Valkeinen807a7512010-01-07 17:45:03 +02002425 if (!dssdev->driver) {
Tomi Valkeinenbab59b42011-08-04 14:37:29 +03002426 dev_warn(&pdev->dev, "no driver for display: %s\n",
Andy Doanc5f18d72011-07-06 12:08:29 -05002427 dssdev->name);
Tomi Valkeinenbab59b42011-08-04 14:37:29 +03002428 omap_dss_put_device(dssdev);
2429 continue;
Tomi Valkeinen807a7512010-01-07 17:45:03 +02002430 }
Tomi Valkeinenb3f91eb2010-02-17 12:00:01 +02002431
Tomi Valkeinen27cc2132011-04-30 16:55:12 +03002432 d = &fbdev->displays[fbdev->num_displays++];
2433 d->dssdev = dssdev;
2434 if (dssdev->caps & OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE)
2435 d->update_mode = OMAPFB_MANUAL_UPDATE;
2436 else
2437 d->update_mode = OMAPFB_AUTO_UPDATE;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002438 }
2439
Tomi Valkeinenb3f91eb2010-02-17 12:00:01 +02002440 if (r)
2441 goto cleanup;
2442
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002443 if (fbdev->num_displays == 0) {
2444 dev_err(&pdev->dev, "no displays\n");
2445 r = -EINVAL;
2446 goto cleanup;
2447 }
2448
2449 fbdev->num_overlays = omap_dss_get_num_overlays();
2450 for (i = 0; i < fbdev->num_overlays; i++)
2451 fbdev->overlays[i] = omap_dss_get_overlay(i);
2452
2453 fbdev->num_managers = omap_dss_get_num_overlay_managers();
2454 for (i = 0; i < fbdev->num_managers; i++)
2455 fbdev->managers[i] = omap_dss_get_overlay_manager(i);
2456
Tomi Valkeinendc891fa2011-08-25 17:15:14 +03002457 /* gfx overlay should be the default one. find a display
2458 * connected to that, and use it as default display */
2459 ovl = omap_dss_get_overlay(0);
Archit Taneja4249e612012-07-20 18:26:56 +05302460 ovl_device = ovl->get_device(ovl);
2461 if (ovl_device) {
2462 def_display = ovl_device;
Tomi Valkeinendc891fa2011-08-25 17:15:14 +03002463 } else {
2464 dev_warn(&pdev->dev, "cannot find default display\n");
2465 def_display = NULL;
2466 }
2467
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002468 if (def_mode && strlen(def_mode) > 0) {
2469 if (omapfb_parse_def_modes(fbdev))
2470 dev_warn(&pdev->dev, "cannot parse default modes\n");
Tomi Valkeinendc891fa2011-08-25 17:15:14 +03002471 } else if (def_display && def_display->driver->set_timings &&
2472 def_display->driver->check_timings) {
2473 struct omap_video_timings t;
2474
2475 r = omapfb_find_best_mode(def_display, &t);
2476
2477 if (r == 0)
2478 def_display->driver->set_timings(def_display, &t);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002479 }
2480
2481 r = omapfb_create_framebuffers(fbdev);
2482 if (r)
2483 goto cleanup;
2484
2485 for (i = 0; i < fbdev->num_managers; i++) {
2486 struct omap_overlay_manager *mgr;
2487 mgr = fbdev->managers[i];
2488 r = mgr->apply(mgr);
2489 if (r)
2490 dev_warn(fbdev->dev, "failed to apply dispc config\n");
2491 }
2492
2493 DBG("mgr->apply'ed\n");
2494
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002495 if (def_display) {
Tomi Valkeinen91ac27a2010-09-23 11:18:44 +03002496 r = omapfb_init_display(fbdev, def_display);
Tomi Valkeinen6d2e0bd2010-02-17 13:38:08 +02002497 if (r) {
Tomi Valkeinen91ac27a2010-09-23 11:18:44 +03002498 dev_err(fbdev->dev,
2499 "failed to initialize default "
2500 "display\n");
Tomi Valkeinen6d2e0bd2010-02-17 13:38:08 +02002501 goto cleanup;
2502 }
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002503 }
2504
Afzal Mohammede26ed442010-07-01 15:40:01 +02002505 DBG("create sysfs for fbs\n");
2506 r = omapfb_create_sysfs(fbdev);
2507 if (r) {
2508 dev_err(fbdev->dev, "failed to create sysfs entries\n");
2509 goto cleanup;
2510 }
2511
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002512 return 0;
2513
2514cleanup:
2515 omapfb_free_resources(fbdev);
2516err0:
2517 dev_err(&pdev->dev, "failed to setup omapfb\n");
2518 return r;
2519}
2520
Tomi Valkeinend64f14e2012-02-17 17:43:03 +02002521static int __exit omapfb_remove(struct platform_device *pdev)
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002522{
2523 struct omapfb2_device *fbdev = platform_get_drvdata(pdev);
2524
2525 /* FIXME: wait till completion of pending events */
2526
2527 omapfb_remove_sysfs(fbdev);
2528
2529 omapfb_free_resources(fbdev);
2530
2531 return 0;
2532}
2533
2534static struct platform_driver omapfb_driver = {
Tomi Valkeinend64f14e2012-02-17 17:43:03 +02002535 .remove = __exit_p(omapfb_remove),
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002536 .driver = {
2537 .name = "omapfb",
2538 .owner = THIS_MODULE,
2539 },
2540};
2541
2542static int __init omapfb_init(void)
2543{
2544 DBG("omapfb_init\n");
2545
Tomi Valkeinend64f14e2012-02-17 17:43:03 +02002546 if (platform_driver_probe(&omapfb_driver, omapfb_probe)) {
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002547 printk(KERN_ERR "failed to register omapfb driver\n");
2548 return -ENODEV;
2549 }
2550
2551 return 0;
2552}
2553
2554static void __exit omapfb_exit(void)
2555{
2556 DBG("omapfb_exit\n");
2557 platform_driver_unregister(&omapfb_driver);
2558}
2559
2560module_param_named(mode, def_mode, charp, 0);
2561module_param_named(vram, def_vram, charp, 0);
2562module_param_named(rotate, def_rotate, int, 0);
2563module_param_named(vrfb, def_vrfb, bool, 0);
2564module_param_named(mirror, def_mirror, bool, 0);
2565
2566/* late_initcall to let panel/ctrl drivers loaded first.
2567 * I guess better option would be a more dynamic approach,
2568 * so that omapfb reacts to new panels when they are loaded */
2569late_initcall(omapfb_init);
2570/*module_init(omapfb_init);*/
2571module_exit(omapfb_exit);
2572
2573MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@nokia.com>");
2574MODULE_DESCRIPTION("OMAP2/3 Framebuffer");
2575MODULE_LICENSE("GPL v2");