blob: f38348ea33757eac5ed3af01e1145e8b246c721a [file] [log] [blame]
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001/*
2 * linux/drivers/video/omap2/omapfb-main.c
3 *
4 * Copyright (C) 2008 Nokia Corporation
5 * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
6 *
7 * Some code and ideas taken from drivers/video/omap/ driver
8 * by Imre Deak.
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License version 2 as published by
12 * the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23#include <linux/module.h>
24#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +030026#include <linux/fb.h>
27#include <linux/dma-mapping.h>
28#include <linux/vmalloc.h>
29#include <linux/device.h>
30#include <linux/platform_device.h>
31#include <linux/omapfb.h>
32
Tomi Valkeinena0b38cc2011-05-11 14:05:07 +030033#include <video/omapdss.h>
Tomi Valkeinen6a1c9f62012-10-08 14:52:24 +030034#include <video/omapvrfb.h>
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +030035
36#include "omapfb.h"
37
38#define MODULE_NAME "omapfb"
39
40#define OMAPFB_PLANE_XRES_MIN 8
41#define OMAPFB_PLANE_YRES_MIN 8
42
43static char *def_mode;
44static char *def_vram;
Rusty Russell90ab5ee2012-01-13 09:32:20 +103045static bool def_vrfb;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +030046static int def_rotate;
Rusty Russell90ab5ee2012-01-13 09:32:20 +103047static bool def_mirror;
Tomi Valkeinen27cc2132011-04-30 16:55:12 +030048static bool auto_update;
49static unsigned int auto_update_freq;
50module_param(auto_update, bool, 0);
51module_param(auto_update_freq, uint, 0644);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +030052
53#ifdef DEBUG
Rusty Russell90ab5ee2012-01-13 09:32:20 +103054bool omapfb_debug;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +030055module_param_named(debug, omapfb_debug, bool, 0644);
Rusty Russell90ab5ee2012-01-13 09:32:20 +103056static bool omapfb_test_pattern;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +030057module_param_named(test, omapfb_test_pattern, bool, 0644);
58#endif
59
60static int omapfb_fb_init(struct omapfb2_device *fbdev, struct fb_info *fbi);
Tomi Valkeinena2699502010-01-11 14:33:40 +020061static int omapfb_get_recommended_bpp(struct omapfb2_device *fbdev,
62 struct omap_dss_device *dssdev);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +030063
64#ifdef DEBUG
65static void draw_pixel(struct fb_info *fbi, int x, int y, unsigned color)
66{
67 struct fb_var_screeninfo *var = &fbi->var;
68 struct fb_fix_screeninfo *fix = &fbi->fix;
69 void __iomem *addr = fbi->screen_base;
70 const unsigned bytespp = var->bits_per_pixel >> 3;
71 const unsigned line_len = fix->line_length / bytespp;
72
73 int r = (color >> 16) & 0xff;
74 int g = (color >> 8) & 0xff;
75 int b = (color >> 0) & 0xff;
76
77 if (var->bits_per_pixel == 16) {
78 u16 __iomem *p = (u16 __iomem *)addr;
79 p += y * line_len + x;
80
81 r = r * 32 / 256;
82 g = g * 64 / 256;
83 b = b * 32 / 256;
84
85 __raw_writew((r << 11) | (g << 5) | (b << 0), p);
86 } else if (var->bits_per_pixel == 24) {
87 u8 __iomem *p = (u8 __iomem *)addr;
88 p += (y * line_len + x) * 3;
89
90 __raw_writeb(b, p + 0);
91 __raw_writeb(g, p + 1);
92 __raw_writeb(r, p + 2);
93 } else if (var->bits_per_pixel == 32) {
94 u32 __iomem *p = (u32 __iomem *)addr;
95 p += y * line_len + x;
96 __raw_writel(color, p);
97 }
98}
99
100static void fill_fb(struct fb_info *fbi)
101{
102 struct fb_var_screeninfo *var = &fbi->var;
103 const short w = var->xres_virtual;
104 const short h = var->yres_virtual;
105 void __iomem *addr = fbi->screen_base;
106 int y, x;
107
108 if (!addr)
109 return;
110
111 DBG("fill_fb %dx%d, line_len %d bytes\n", w, h, fbi->fix.line_length);
112
113 for (y = 0; y < h; y++) {
114 for (x = 0; x < w; x++) {
115 if (x < 20 && y < 20)
116 draw_pixel(fbi, x, y, 0xffffff);
117 else if (x < 20 && (y > 20 && y < h - 20))
118 draw_pixel(fbi, x, y, 0xff);
119 else if (y < 20 && (x > 20 && x < w - 20))
120 draw_pixel(fbi, x, y, 0xff00);
121 else if (x > w - 20 && (y > 20 && y < h - 20))
122 draw_pixel(fbi, x, y, 0xff0000);
123 else if (y > h - 20 && (x > 20 && x < w - 20))
124 draw_pixel(fbi, x, y, 0xffff00);
125 else if (x == 20 || x == w - 20 ||
126 y == 20 || y == h - 20)
127 draw_pixel(fbi, x, y, 0xffffff);
128 else if (x == y || w - x == h - y)
129 draw_pixel(fbi, x, y, 0xff00ff);
130 else if (w - x == y || x == h - y)
131 draw_pixel(fbi, x, y, 0x00ffff);
132 else if (x > 20 && y > 20 && x < w - 20 && y < h - 20) {
133 int t = x * 3 / w;
134 unsigned r = 0, g = 0, b = 0;
135 unsigned c;
136 if (var->bits_per_pixel == 16) {
137 if (t == 0)
138 b = (y % 32) * 256 / 32;
139 else if (t == 1)
140 g = (y % 64) * 256 / 64;
141 else if (t == 2)
142 r = (y % 32) * 256 / 32;
143 } else {
144 if (t == 0)
145 b = (y % 256);
146 else if (t == 1)
147 g = (y % 256);
148 else if (t == 2)
149 r = (y % 256);
150 }
151 c = (r << 16) | (g << 8) | (b << 0);
152 draw_pixel(fbi, x, y, c);
153 } else {
154 draw_pixel(fbi, x, y, 0);
155 }
156 }
157 }
158}
159#endif
160
Ville Syrjäläa4c1a142010-02-23 23:36:26 +0100161static unsigned omapfb_get_vrfb_offset(const struct omapfb_info *ofbi, int rot)
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300162{
Ville Syrjälä078ff542010-03-17 20:36:51 +0200163 const struct vrfb *vrfb = &ofbi->region->vrfb;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300164 unsigned offset;
165
166 switch (rot) {
167 case FB_ROTATE_UR:
168 offset = 0;
169 break;
170 case FB_ROTATE_CW:
171 offset = vrfb->yoffset;
172 break;
173 case FB_ROTATE_UD:
174 offset = vrfb->yoffset * OMAP_VRFB_LINE_LEN + vrfb->xoffset;
175 break;
176 case FB_ROTATE_CCW:
177 offset = vrfb->xoffset * OMAP_VRFB_LINE_LEN;
178 break;
179 default:
180 BUG();
Tomi Valkeinen4a75cb82012-05-18 11:48:28 +0300181 return 0;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300182 }
183
184 offset *= vrfb->bytespp;
185
186 return offset;
187}
188
Ville Syrjäläa4c1a142010-02-23 23:36:26 +0100189static u32 omapfb_get_region_rot_paddr(const struct omapfb_info *ofbi, int rot)
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300190{
191 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
Ville Syrjälä078ff542010-03-17 20:36:51 +0200192 return ofbi->region->vrfb.paddr[rot]
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300193 + omapfb_get_vrfb_offset(ofbi, rot);
194 } else {
Ville Syrjälä078ff542010-03-17 20:36:51 +0200195 return ofbi->region->paddr;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300196 }
197}
198
Ville Syrjäläa4c1a142010-02-23 23:36:26 +0100199static u32 omapfb_get_region_paddr(const struct omapfb_info *ofbi)
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300200{
201 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
Ville Syrjälä078ff542010-03-17 20:36:51 +0200202 return ofbi->region->vrfb.paddr[0];
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300203 else
Ville Syrjälä078ff542010-03-17 20:36:51 +0200204 return ofbi->region->paddr;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300205}
206
Ville Syrjäläa4c1a142010-02-23 23:36:26 +0100207static void __iomem *omapfb_get_region_vaddr(const struct omapfb_info *ofbi)
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300208{
209 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
Ville Syrjälä078ff542010-03-17 20:36:51 +0200210 return ofbi->region->vrfb.vaddr[0];
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300211 else
Ville Syrjälä078ff542010-03-17 20:36:51 +0200212 return ofbi->region->vaddr;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300213}
214
215static struct omapfb_colormode omapfb_colormodes[] = {
216 {
217 .dssmode = OMAP_DSS_COLOR_UYVY,
218 .bits_per_pixel = 16,
219 .nonstd = OMAPFB_COLOR_YUV422,
220 }, {
221 .dssmode = OMAP_DSS_COLOR_YUV2,
222 .bits_per_pixel = 16,
223 .nonstd = OMAPFB_COLOR_YUY422,
224 }, {
225 .dssmode = OMAP_DSS_COLOR_ARGB16,
226 .bits_per_pixel = 16,
227 .red = { .length = 4, .offset = 8, .msb_right = 0 },
228 .green = { .length = 4, .offset = 4, .msb_right = 0 },
229 .blue = { .length = 4, .offset = 0, .msb_right = 0 },
230 .transp = { .length = 4, .offset = 12, .msb_right = 0 },
231 }, {
232 .dssmode = OMAP_DSS_COLOR_RGB16,
233 .bits_per_pixel = 16,
234 .red = { .length = 5, .offset = 11, .msb_right = 0 },
235 .green = { .length = 6, .offset = 5, .msb_right = 0 },
236 .blue = { .length = 5, .offset = 0, .msb_right = 0 },
237 .transp = { .length = 0, .offset = 0, .msb_right = 0 },
238 }, {
239 .dssmode = OMAP_DSS_COLOR_RGB24P,
240 .bits_per_pixel = 24,
241 .red = { .length = 8, .offset = 16, .msb_right = 0 },
242 .green = { .length = 8, .offset = 8, .msb_right = 0 },
243 .blue = { .length = 8, .offset = 0, .msb_right = 0 },
244 .transp = { .length = 0, .offset = 0, .msb_right = 0 },
245 }, {
246 .dssmode = OMAP_DSS_COLOR_RGB24U,
247 .bits_per_pixel = 32,
248 .red = { .length = 8, .offset = 16, .msb_right = 0 },
249 .green = { .length = 8, .offset = 8, .msb_right = 0 },
250 .blue = { .length = 8, .offset = 0, .msb_right = 0 },
251 .transp = { .length = 0, .offset = 0, .msb_right = 0 },
252 }, {
253 .dssmode = OMAP_DSS_COLOR_ARGB32,
254 .bits_per_pixel = 32,
255 .red = { .length = 8, .offset = 16, .msb_right = 0 },
256 .green = { .length = 8, .offset = 8, .msb_right = 0 },
257 .blue = { .length = 8, .offset = 0, .msb_right = 0 },
258 .transp = { .length = 8, .offset = 24, .msb_right = 0 },
259 }, {
260 .dssmode = OMAP_DSS_COLOR_RGBA32,
261 .bits_per_pixel = 32,
262 .red = { .length = 8, .offset = 24, .msb_right = 0 },
263 .green = { .length = 8, .offset = 16, .msb_right = 0 },
264 .blue = { .length = 8, .offset = 8, .msb_right = 0 },
265 .transp = { .length = 8, .offset = 0, .msb_right = 0 },
266 }, {
267 .dssmode = OMAP_DSS_COLOR_RGBX32,
268 .bits_per_pixel = 32,
269 .red = { .length = 8, .offset = 24, .msb_right = 0 },
270 .green = { .length = 8, .offset = 16, .msb_right = 0 },
271 .blue = { .length = 8, .offset = 8, .msb_right = 0 },
272 .transp = { .length = 0, .offset = 0, .msb_right = 0 },
273 },
274};
275
276static bool cmp_var_to_colormode(struct fb_var_screeninfo *var,
277 struct omapfb_colormode *color)
278{
279 bool cmp_component(struct fb_bitfield *f1, struct fb_bitfield *f2)
280 {
281 return f1->length == f2->length &&
282 f1->offset == f2->offset &&
283 f1->msb_right == f2->msb_right;
284 }
285
286 if (var->bits_per_pixel == 0 ||
287 var->red.length == 0 ||
288 var->blue.length == 0 ||
289 var->green.length == 0)
290 return 0;
291
292 return var->bits_per_pixel == color->bits_per_pixel &&
293 cmp_component(&var->red, &color->red) &&
294 cmp_component(&var->green, &color->green) &&
295 cmp_component(&var->blue, &color->blue) &&
296 cmp_component(&var->transp, &color->transp);
297}
298
299static void assign_colormode_to_var(struct fb_var_screeninfo *var,
300 struct omapfb_colormode *color)
301{
302 var->bits_per_pixel = color->bits_per_pixel;
303 var->nonstd = color->nonstd;
304 var->red = color->red;
305 var->green = color->green;
306 var->blue = color->blue;
307 var->transp = color->transp;
308}
309
310static int fb_mode_to_dss_mode(struct fb_var_screeninfo *var,
311 enum omap_color_mode *mode)
312{
313 enum omap_color_mode dssmode;
314 int i;
315
316 /* first match with nonstd field */
317 if (var->nonstd) {
318 for (i = 0; i < ARRAY_SIZE(omapfb_colormodes); ++i) {
319 struct omapfb_colormode *m = &omapfb_colormodes[i];
320 if (var->nonstd == m->nonstd) {
321 assign_colormode_to_var(var, m);
322 *mode = m->dssmode;
323 return 0;
324 }
325 }
326
327 return -EINVAL;
328 }
329
330 /* then try exact match of bpp and colors */
331 for (i = 0; i < ARRAY_SIZE(omapfb_colormodes); ++i) {
332 struct omapfb_colormode *m = &omapfb_colormodes[i];
333 if (cmp_var_to_colormode(var, m)) {
334 assign_colormode_to_var(var, m);
335 *mode = m->dssmode;
336 return 0;
337 }
338 }
339
340 /* match with bpp if user has not filled color fields
341 * properly */
342 switch (var->bits_per_pixel) {
343 case 1:
344 dssmode = OMAP_DSS_COLOR_CLUT1;
345 break;
346 case 2:
347 dssmode = OMAP_DSS_COLOR_CLUT2;
348 break;
349 case 4:
350 dssmode = OMAP_DSS_COLOR_CLUT4;
351 break;
352 case 8:
353 dssmode = OMAP_DSS_COLOR_CLUT8;
354 break;
355 case 12:
356 dssmode = OMAP_DSS_COLOR_RGB12U;
357 break;
358 case 16:
359 dssmode = OMAP_DSS_COLOR_RGB16;
360 break;
361 case 24:
362 dssmode = OMAP_DSS_COLOR_RGB24P;
363 break;
364 case 32:
365 dssmode = OMAP_DSS_COLOR_RGB24U;
366 break;
367 default:
368 return -EINVAL;
369 }
370
371 for (i = 0; i < ARRAY_SIZE(omapfb_colormodes); ++i) {
372 struct omapfb_colormode *m = &omapfb_colormodes[i];
373 if (dssmode == m->dssmode) {
374 assign_colormode_to_var(var, m);
375 *mode = m->dssmode;
376 return 0;
377 }
378 }
379
380 return -EINVAL;
381}
382
383static int check_fb_res_bounds(struct fb_var_screeninfo *var)
384{
385 int xres_min = OMAPFB_PLANE_XRES_MIN;
386 int xres_max = 2048;
387 int yres_min = OMAPFB_PLANE_YRES_MIN;
388 int yres_max = 2048;
389
390 /* XXX: some applications seem to set virtual res to 0. */
391 if (var->xres_virtual == 0)
392 var->xres_virtual = var->xres;
393
394 if (var->yres_virtual == 0)
395 var->yres_virtual = var->yres;
396
397 if (var->xres_virtual < xres_min || var->yres_virtual < yres_min)
398 return -EINVAL;
399
400 if (var->xres < xres_min)
401 var->xres = xres_min;
402 if (var->yres < yres_min)
403 var->yres = yres_min;
404 if (var->xres > xres_max)
405 var->xres = xres_max;
406 if (var->yres > yres_max)
407 var->yres = yres_max;
408
409 if (var->xres > var->xres_virtual)
410 var->xres = var->xres_virtual;
411 if (var->yres > var->yres_virtual)
412 var->yres = var->yres_virtual;
413
414 return 0;
415}
416
417static void shrink_height(unsigned long max_frame_size,
418 struct fb_var_screeninfo *var)
419{
420 DBG("can't fit FB into memory, reducing y\n");
421 var->yres_virtual = max_frame_size /
422 (var->xres_virtual * var->bits_per_pixel >> 3);
423
424 if (var->yres_virtual < OMAPFB_PLANE_YRES_MIN)
425 var->yres_virtual = OMAPFB_PLANE_YRES_MIN;
426
427 if (var->yres > var->yres_virtual)
428 var->yres = var->yres_virtual;
429}
430
431static void shrink_width(unsigned long max_frame_size,
432 struct fb_var_screeninfo *var)
433{
434 DBG("can't fit FB into memory, reducing x\n");
435 var->xres_virtual = max_frame_size / var->yres_virtual /
436 (var->bits_per_pixel >> 3);
437
438 if (var->xres_virtual < OMAPFB_PLANE_XRES_MIN)
439 var->xres_virtual = OMAPFB_PLANE_XRES_MIN;
440
441 if (var->xres > var->xres_virtual)
442 var->xres = var->xres_virtual;
443}
444
445static int check_vrfb_fb_size(unsigned long region_size,
446 const struct fb_var_screeninfo *var)
447{
448 unsigned long min_phys_size = omap_vrfb_min_phys_size(var->xres_virtual,
449 var->yres_virtual, var->bits_per_pixel >> 3);
450
451 return min_phys_size > region_size ? -EINVAL : 0;
452}
453
454static int check_fb_size(const struct omapfb_info *ofbi,
455 struct fb_var_screeninfo *var)
456{
Ville Syrjälä078ff542010-03-17 20:36:51 +0200457 unsigned long max_frame_size = ofbi->region->size;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300458 int bytespp = var->bits_per_pixel >> 3;
459 unsigned long line_size = var->xres_virtual * bytespp;
460
461 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
462 /* One needs to check for both VRFB and OMAPFB limitations. */
463 if (check_vrfb_fb_size(max_frame_size, var))
464 shrink_height(omap_vrfb_max_height(
465 max_frame_size, var->xres_virtual, bytespp) *
466 line_size, var);
467
468 if (check_vrfb_fb_size(max_frame_size, var)) {
469 DBG("cannot fit FB to memory\n");
470 return -EINVAL;
471 }
472
473 return 0;
474 }
475
476 DBG("max frame size %lu, line size %lu\n", max_frame_size, line_size);
477
478 if (line_size * var->yres_virtual > max_frame_size)
479 shrink_height(max_frame_size, var);
480
481 if (line_size * var->yres_virtual > max_frame_size) {
482 shrink_width(max_frame_size, var);
483 line_size = var->xres_virtual * bytespp;
484 }
485
486 if (line_size * var->yres_virtual > max_frame_size) {
487 DBG("cannot fit FB to memory\n");
488 return -EINVAL;
489 }
490
491 return 0;
492}
493
494/*
495 * Consider if VRFB assisted rotation is in use and if the virtual space for
496 * the zero degree view needs to be mapped. The need for mapping also acts as
497 * the trigger for setting up the hardware on the context in question. This
498 * ensures that one does not attempt to access the virtual view before the
499 * hardware is serving the address translations.
500 */
501static int setup_vrfb_rotation(struct fb_info *fbi)
502{
503 struct omapfb_info *ofbi = FB2OFB(fbi);
Ville Syrjälä078ff542010-03-17 20:36:51 +0200504 struct omapfb2_mem_region *rg = ofbi->region;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300505 struct vrfb *vrfb = &rg->vrfb;
506 struct fb_var_screeninfo *var = &fbi->var;
507 struct fb_fix_screeninfo *fix = &fbi->fix;
508 unsigned bytespp;
509 bool yuv_mode;
510 enum omap_color_mode mode;
511 int r;
512 bool reconf;
513
514 if (!rg->size || ofbi->rotation_type != OMAP_DSS_ROT_VRFB)
515 return 0;
516
517 DBG("setup_vrfb_rotation\n");
518
519 r = fb_mode_to_dss_mode(var, &mode);
520 if (r)
521 return r;
522
523 bytespp = var->bits_per_pixel >> 3;
524
525 yuv_mode = mode == OMAP_DSS_COLOR_YUV2 || mode == OMAP_DSS_COLOR_UYVY;
526
527 /* We need to reconfigure VRFB if the resolution changes, if yuv mode
528 * is enabled/disabled, or if bytes per pixel changes */
529
530 /* XXX we shouldn't allow this when framebuffer is mmapped */
531
532 reconf = false;
533
534 if (yuv_mode != vrfb->yuv_mode)
535 reconf = true;
536 else if (bytespp != vrfb->bytespp)
537 reconf = true;
538 else if (vrfb->xres != var->xres_virtual ||
539 vrfb->yres != var->yres_virtual)
540 reconf = true;
541
542 if (vrfb->vaddr[0] && reconf) {
543 fbi->screen_base = NULL;
544 fix->smem_start = 0;
545 fix->smem_len = 0;
546 iounmap(vrfb->vaddr[0]);
547 vrfb->vaddr[0] = NULL;
548 DBG("setup_vrfb_rotation: reset fb\n");
549 }
550
551 if (vrfb->vaddr[0])
552 return 0;
553
554 omap_vrfb_setup(&rg->vrfb, rg->paddr,
555 var->xres_virtual,
556 var->yres_virtual,
557 bytespp, yuv_mode);
558
559 /* Now one can ioremap the 0 angle view */
560 r = omap_vrfb_map_angle(vrfb, var->yres_virtual, 0);
561 if (r)
562 return r;
563
564 /* used by open/write in fbmem.c */
Ville Syrjälä078ff542010-03-17 20:36:51 +0200565 fbi->screen_base = ofbi->region->vrfb.vaddr[0];
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300566
Ville Syrjälä078ff542010-03-17 20:36:51 +0200567 fix->smem_start = ofbi->region->vrfb.paddr[0];
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300568
569 switch (var->nonstd) {
570 case OMAPFB_COLOR_YUV422:
571 case OMAPFB_COLOR_YUY422:
572 fix->line_length =
573 (OMAP_VRFB_LINE_LEN * var->bits_per_pixel) >> 2;
574 break;
575 default:
576 fix->line_length =
577 (OMAP_VRFB_LINE_LEN * var->bits_per_pixel) >> 3;
578 break;
579 }
580
581 fix->smem_len = var->yres_virtual * fix->line_length;
582
583 return 0;
584}
585
586int dss_mode_to_fb_mode(enum omap_color_mode dssmode,
587 struct fb_var_screeninfo *var)
588{
589 int i;
590
591 for (i = 0; i < ARRAY_SIZE(omapfb_colormodes); ++i) {
592 struct omapfb_colormode *mode = &omapfb_colormodes[i];
593 if (dssmode == mode->dssmode) {
594 assign_colormode_to_var(var, mode);
595 return 0;
596 }
597 }
598 return -ENOENT;
599}
600
601void set_fb_fix(struct fb_info *fbi)
602{
603 struct fb_fix_screeninfo *fix = &fbi->fix;
604 struct fb_var_screeninfo *var = &fbi->var;
605 struct omapfb_info *ofbi = FB2OFB(fbi);
Ville Syrjälä078ff542010-03-17 20:36:51 +0200606 struct omapfb2_mem_region *rg = ofbi->region;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300607
608 DBG("set_fb_fix\n");
609
610 /* used by open/write in fbmem.c */
611 fbi->screen_base = (char __iomem *)omapfb_get_region_vaddr(ofbi);
612
613 /* used by mmap in fbmem.c */
614 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
615 switch (var->nonstd) {
616 case OMAPFB_COLOR_YUV422:
617 case OMAPFB_COLOR_YUY422:
618 fix->line_length =
619 (OMAP_VRFB_LINE_LEN * var->bits_per_pixel) >> 2;
620 break;
621 default:
622 fix->line_length =
623 (OMAP_VRFB_LINE_LEN * var->bits_per_pixel) >> 3;
624 break;
625 }
626
627 fix->smem_len = var->yres_virtual * fix->line_length;
628 } else {
629 fix->line_length =
630 (var->xres_virtual * var->bits_per_pixel) >> 3;
631 fix->smem_len = rg->size;
632 }
633
634 fix->smem_start = omapfb_get_region_paddr(ofbi);
635
636 fix->type = FB_TYPE_PACKED_PIXELS;
637
638 if (var->nonstd)
639 fix->visual = FB_VISUAL_PSEUDOCOLOR;
640 else {
641 switch (var->bits_per_pixel) {
642 case 32:
643 case 24:
644 case 16:
645 case 12:
646 fix->visual = FB_VISUAL_TRUECOLOR;
647 /* 12bpp is stored in 16 bits */
648 break;
649 case 1:
650 case 2:
651 case 4:
652 case 8:
653 fix->visual = FB_VISUAL_PSEUDOCOLOR;
654 break;
655 }
656 }
657
658 fix->accel = FB_ACCEL_NONE;
659
660 fix->xpanstep = 1;
661 fix->ypanstep = 1;
662}
663
664/* check new var and possibly modify it to be ok */
665int check_fb_var(struct fb_info *fbi, struct fb_var_screeninfo *var)
666{
667 struct omapfb_info *ofbi = FB2OFB(fbi);
668 struct omap_dss_device *display = fb2display(fbi);
669 enum omap_color_mode mode = 0;
670 int i;
671 int r;
672
673 DBG("check_fb_var %d\n", ofbi->id);
674
Tomi Valkeinen3ed37d92012-12-13 13:19:05 +0200675 WARN_ON(!atomic_read(&ofbi->region->lock_count));
676
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300677 r = fb_mode_to_dss_mode(var, &mode);
678 if (r) {
679 DBG("cannot convert var to omap dss mode\n");
680 return r;
681 }
682
683 for (i = 0; i < ofbi->num_overlays; ++i) {
684 if ((ofbi->overlays[i]->supported_modes & mode) == 0) {
685 DBG("invalid mode\n");
686 return -EINVAL;
687 }
688 }
689
Jani Nikula86f2d7d2010-06-01 17:25:10 +0300690 if (var->rotate > 3)
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300691 return -EINVAL;
692
693 if (check_fb_res_bounds(var))
694 return -EINVAL;
695
Ville Syrjälä276a1d42010-03-17 20:05:38 +0200696 /* When no memory is allocated ignore the size check */
Ville Syrjälä078ff542010-03-17 20:36:51 +0200697 if (ofbi->region->size != 0 && check_fb_size(ofbi, var))
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300698 return -EINVAL;
699
700 if (var->xres + var->xoffset > var->xres_virtual)
701 var->xoffset = var->xres_virtual - var->xres;
702 if (var->yres + var->yoffset > var->yres_virtual)
703 var->yoffset = var->yres_virtual - var->yres;
704
705 DBG("xres = %d, yres = %d, vxres = %d, vyres = %d\n",
706 var->xres, var->yres,
707 var->xres_virtual, var->yres_virtual);
708
Jani Nikula7a0987b2010-06-16 15:26:36 +0300709 if (display && display->driver->get_dimensions) {
710 u32 w, h;
711 display->driver->get_dimensions(display, &w, &h);
712 var->width = DIV_ROUND_CLOSEST(w, 1000);
713 var->height = DIV_ROUND_CLOSEST(h, 1000);
714 } else {
715 var->height = -1;
716 var->width = -1;
717 }
718
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300719 var->grayscale = 0;
720
Tomi Valkeinen69b20482010-01-20 12:11:25 +0200721 if (display && display->driver->get_timings) {
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300722 struct omap_video_timings timings;
Tomi Valkeinen69b20482010-01-20 12:11:25 +0200723 display->driver->get_timings(display, &timings);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300724
725 /* pixclock in ps, the rest in pixclock */
726 var->pixclock = timings.pixel_clock != 0 ?
727 KHZ2PICOS(timings.pixel_clock) :
728 0;
Tasslehoff Kjappfot87ba8282010-09-08 12:46:14 +0200729 var->left_margin = timings.hbp;
730 var->right_margin = timings.hfp;
731 var->upper_margin = timings.vbp;
732 var->lower_margin = timings.vfp;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300733 var->hsync_len = timings.hsw;
734 var->vsync_len = timings.vsw;
Archit Taneja783babf2012-06-15 07:39:47 +0530735 var->sync |= timings.hsync_level == OMAPDSS_SIG_ACTIVE_HIGH ?
736 FB_SYNC_HOR_HIGH_ACT : 0;
737 var->sync |= timings.vsync_level == OMAPDSS_SIG_ACTIVE_HIGH ?
738 FB_SYNC_VERT_HIGH_ACT : 0;
Archit Taneja23bae3a2012-06-28 18:23:18 +0530739 var->vmode = timings.interlace ?
740 FB_VMODE_INTERLACED : FB_VMODE_NONINTERLACED;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300741 } else {
742 var->pixclock = 0;
743 var->left_margin = 0;
744 var->right_margin = 0;
745 var->upper_margin = 0;
746 var->lower_margin = 0;
747 var->hsync_len = 0;
748 var->vsync_len = 0;
Archit Taneja783babf2012-06-15 07:39:47 +0530749 var->sync = 0;
Archit Taneja23bae3a2012-06-28 18:23:18 +0530750 var->vmode = FB_VMODE_NONINTERLACED;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300751 }
752
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300753 return 0;
754}
755
756/*
757 * ---------------------------------------------------------------------------
758 * fbdev framework callbacks
759 * ---------------------------------------------------------------------------
760 */
761static int omapfb_open(struct fb_info *fbi, int user)
762{
763 return 0;
764}
765
766static int omapfb_release(struct fb_info *fbi, int user)
767{
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300768 return 0;
769}
770
Ville Syrjäläa4c1a142010-02-23 23:36:26 +0100771static unsigned calc_rotation_offset_dma(const struct fb_var_screeninfo *var,
772 const struct fb_fix_screeninfo *fix, int rotation)
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300773{
774 unsigned offset;
775
776 offset = var->yoffset * fix->line_length +
777 var->xoffset * (var->bits_per_pixel >> 3);
778
779 return offset;
780}
781
Ville Syrjäläa4c1a142010-02-23 23:36:26 +0100782static unsigned calc_rotation_offset_vrfb(const struct fb_var_screeninfo *var,
783 const struct fb_fix_screeninfo *fix, int rotation)
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300784{
785 unsigned offset;
786
787 if (rotation == FB_ROTATE_UD)
788 offset = (var->yres_virtual - var->yres) *
789 fix->line_length;
790 else if (rotation == FB_ROTATE_CW)
791 offset = (var->yres_virtual - var->yres) *
792 (var->bits_per_pixel >> 3);
793 else
794 offset = 0;
795
796 if (rotation == FB_ROTATE_UR)
797 offset += var->yoffset * fix->line_length +
798 var->xoffset * (var->bits_per_pixel >> 3);
799 else if (rotation == FB_ROTATE_UD)
800 offset -= var->yoffset * fix->line_length +
801 var->xoffset * (var->bits_per_pixel >> 3);
802 else if (rotation == FB_ROTATE_CW)
803 offset -= var->xoffset * fix->line_length +
804 var->yoffset * (var->bits_per_pixel >> 3);
805 else if (rotation == FB_ROTATE_CCW)
806 offset += var->xoffset * fix->line_length +
807 var->yoffset * (var->bits_per_pixel >> 3);
808
809 return offset;
810}
811
Ville Syrjälä46d35242010-03-17 19:59:26 +0200812static void omapfb_calc_addr(const struct omapfb_info *ofbi,
813 const struct fb_var_screeninfo *var,
814 const struct fb_fix_screeninfo *fix,
Tomi Valkeinen212b0d52011-09-26 19:16:59 +0300815 int rotation, u32 *paddr)
Ville Syrjälä46d35242010-03-17 19:59:26 +0200816{
817 u32 data_start_p;
Ville Syrjälä46d35242010-03-17 19:59:26 +0200818 int offset;
819
Tomi Valkeinen212b0d52011-09-26 19:16:59 +0300820 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
Ville Syrjälä46d35242010-03-17 19:59:26 +0200821 data_start_p = omapfb_get_region_rot_paddr(ofbi, rotation);
Tomi Valkeinen212b0d52011-09-26 19:16:59 +0300822 else
Ville Syrjälä46d35242010-03-17 19:59:26 +0200823 data_start_p = omapfb_get_region_paddr(ofbi);
Ville Syrjälä46d35242010-03-17 19:59:26 +0200824
825 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
826 offset = calc_rotation_offset_vrfb(var, fix, rotation);
827 else
828 offset = calc_rotation_offset_dma(var, fix, rotation);
829
830 data_start_p += offset;
Ville Syrjälä46d35242010-03-17 19:59:26 +0200831
832 if (offset)
833 DBG("offset %d, %d = %d\n",
834 var->xoffset, var->yoffset, offset);
835
Tomi Valkeinen212b0d52011-09-26 19:16:59 +0300836 DBG("paddr %x\n", data_start_p);
Ville Syrjälä46d35242010-03-17 19:59:26 +0200837
838 *paddr = data_start_p;
Ville Syrjälä46d35242010-03-17 19:59:26 +0200839}
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300840
841/* setup overlay according to the fb */
Ville Syrjälä078ff542010-03-17 20:36:51 +0200842int omapfb_setup_overlay(struct fb_info *fbi, struct omap_overlay *ovl,
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300843 u16 posx, u16 posy, u16 outw, u16 outh)
844{
845 int r = 0;
846 struct omapfb_info *ofbi = FB2OFB(fbi);
847 struct fb_var_screeninfo *var = &fbi->var;
848 struct fb_fix_screeninfo *fix = &fbi->fix;
849 enum omap_color_mode mode = 0;
Ville Syrjälä46d35242010-03-17 19:59:26 +0200850 u32 data_start_p = 0;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300851 struct omap_overlay_info info;
852 int xres, yres;
853 int screen_width;
854 int mirror;
855 int rotation = var->rotate;
856 int i;
857
Tomi Valkeinen3ed37d92012-12-13 13:19:05 +0200858 WARN_ON(!atomic_read(&ofbi->region->lock_count));
859
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300860 for (i = 0; i < ofbi->num_overlays; i++) {
861 if (ovl != ofbi->overlays[i])
862 continue;
863
864 rotation = (rotation + ofbi->rotation[i]) % 4;
865 break;
866 }
867
868 DBG("setup_overlay %d, posx %d, posy %d, outw %d, outh %d\n", ofbi->id,
869 posx, posy, outw, outh);
870
871 if (rotation == FB_ROTATE_CW || rotation == FB_ROTATE_CCW) {
872 xres = var->yres;
873 yres = var->xres;
874 } else {
875 xres = var->xres;
876 yres = var->yres;
877 }
878
Ville Syrjälä078ff542010-03-17 20:36:51 +0200879 if (ofbi->region->size)
Tomi Valkeinen212b0d52011-09-26 19:16:59 +0300880 omapfb_calc_addr(ofbi, var, fix, rotation, &data_start_p);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300881
882 r = fb_mode_to_dss_mode(var, &mode);
883 if (r) {
884 DBG("fb_mode_to_dss_mode failed");
885 goto err;
886 }
887
888 switch (var->nonstd) {
889 case OMAPFB_COLOR_YUV422:
890 case OMAPFB_COLOR_YUY422:
891 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
892 screen_width = fix->line_length
893 / (var->bits_per_pixel >> 2);
894 break;
895 }
896 default:
897 screen_width = fix->line_length / (var->bits_per_pixel >> 3);
898 break;
899 }
900
901 ovl->get_overlay_info(ovl, &info);
902
903 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
904 mirror = 0;
905 else
906 mirror = ofbi->mirror;
907
908 info.paddr = data_start_p;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300909 info.screen_width = screen_width;
910 info.width = xres;
911 info.height = yres;
912 info.color_mode = mode;
913 info.rotation_type = ofbi->rotation_type;
914 info.rotation = rotation;
915 info.mirror = mirror;
916
917 info.pos_x = posx;
918 info.pos_y = posy;
919 info.out_width = outw;
920 info.out_height = outh;
921
922 r = ovl->set_overlay_info(ovl, &info);
923 if (r) {
924 DBG("ovl->setup_overlay_info failed\n");
925 goto err;
926 }
927
928 return 0;
929
930err:
931 DBG("setup_overlay failed\n");
932 return r;
933}
934
935/* apply var to the overlay */
936int omapfb_apply_changes(struct fb_info *fbi, int init)
937{
938 int r = 0;
939 struct omapfb_info *ofbi = FB2OFB(fbi);
940 struct fb_var_screeninfo *var = &fbi->var;
941 struct omap_overlay *ovl;
942 u16 posx, posy;
943 u16 outw, outh;
944 int i;
945
946#ifdef DEBUG
947 if (omapfb_test_pattern)
948 fill_fb(fbi);
949#endif
950
Tomi Valkeinen3ed37d92012-12-13 13:19:05 +0200951 WARN_ON(!atomic_read(&ofbi->region->lock_count));
952
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300953 for (i = 0; i < ofbi->num_overlays; i++) {
954 ovl = ofbi->overlays[i];
955
956 DBG("apply_changes, fb %d, ovl %d\n", ofbi->id, ovl->id);
957
Ville Syrjälä078ff542010-03-17 20:36:51 +0200958 if (ofbi->region->size == 0) {
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300959 /* the fb is not available. disable the overlay */
960 omapfb_overlay_enable(ovl, 0);
961 if (!init && ovl->manager)
962 ovl->manager->apply(ovl->manager);
963 continue;
964 }
965
966 if (init || (ovl->caps & OMAP_DSS_OVL_CAP_SCALE) == 0) {
967 int rotation = (var->rotate + ofbi->rotation[i]) % 4;
968 if (rotation == FB_ROTATE_CW ||
969 rotation == FB_ROTATE_CCW) {
970 outw = var->yres;
971 outh = var->xres;
972 } else {
973 outw = var->xres;
974 outh = var->yres;
975 }
976 } else {
Tomi Valkeinenc1a9feb2011-11-16 14:11:56 +0200977 struct omap_overlay_info info;
978 ovl->get_overlay_info(ovl, &info);
979 outw = info.out_width;
980 outh = info.out_height;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300981 }
982
983 if (init) {
984 posx = 0;
985 posy = 0;
986 } else {
Tomi Valkeinenc1a9feb2011-11-16 14:11:56 +0200987 struct omap_overlay_info info;
988 ovl->get_overlay_info(ovl, &info);
989 posx = info.pos_x;
990 posy = info.pos_y;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300991 }
992
993 r = omapfb_setup_overlay(fbi, ovl, posx, posy, outw, outh);
994 if (r)
995 goto err;
996
997 if (!init && ovl->manager)
998 ovl->manager->apply(ovl->manager);
999 }
1000 return 0;
1001err:
1002 DBG("apply_changes failed\n");
1003 return r;
1004}
1005
1006/* checks var and eventually tweaks it to something supported,
1007 * DO NOT MODIFY PAR */
1008static int omapfb_check_var(struct fb_var_screeninfo *var, struct fb_info *fbi)
1009{
Ville Syrjälä430571d2010-03-17 20:43:23 +02001010 struct omapfb_info *ofbi = FB2OFB(fbi);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001011 int r;
1012
1013 DBG("check_var(%d)\n", FB2OFB(fbi)->id);
1014
Tomi Valkeinen3ed37d92012-12-13 13:19:05 +02001015 omapfb_get_mem_region(ofbi->region);
Ville Syrjälä430571d2010-03-17 20:43:23 +02001016
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001017 r = check_fb_var(fbi, var);
1018
Tomi Valkeinen3ed37d92012-12-13 13:19:05 +02001019 omapfb_put_mem_region(ofbi->region);
Ville Syrjälä430571d2010-03-17 20:43:23 +02001020
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001021 return r;
1022}
1023
1024/* set the video mode according to info->var */
1025static int omapfb_set_par(struct fb_info *fbi)
1026{
Ville Syrjälä430571d2010-03-17 20:43:23 +02001027 struct omapfb_info *ofbi = FB2OFB(fbi);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001028 int r;
1029
1030 DBG("set_par(%d)\n", FB2OFB(fbi)->id);
1031
Tomi Valkeinen3ed37d92012-12-13 13:19:05 +02001032 omapfb_get_mem_region(ofbi->region);
Ville Syrjälä430571d2010-03-17 20:43:23 +02001033
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001034 set_fb_fix(fbi);
1035
1036 r = setup_vrfb_rotation(fbi);
1037 if (r)
Ville Syrjälä430571d2010-03-17 20:43:23 +02001038 goto out;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001039
1040 r = omapfb_apply_changes(fbi, 0);
1041
Ville Syrjälä430571d2010-03-17 20:43:23 +02001042 out:
Tomi Valkeinen3ed37d92012-12-13 13:19:05 +02001043 omapfb_put_mem_region(ofbi->region);
Ville Syrjälä430571d2010-03-17 20:43:23 +02001044
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001045 return r;
1046}
1047
1048static int omapfb_pan_display(struct fb_var_screeninfo *var,
1049 struct fb_info *fbi)
1050{
Ville Syrjälä430571d2010-03-17 20:43:23 +02001051 struct omapfb_info *ofbi = FB2OFB(fbi);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001052 struct fb_var_screeninfo new_var;
1053 int r;
1054
1055 DBG("pan_display(%d)\n", FB2OFB(fbi)->id);
1056
1057 if (var->xoffset == fbi->var.xoffset &&
1058 var->yoffset == fbi->var.yoffset)
1059 return 0;
1060
1061 new_var = fbi->var;
1062 new_var.xoffset = var->xoffset;
1063 new_var.yoffset = var->yoffset;
1064
1065 fbi->var = new_var;
1066
Tomi Valkeinen3ed37d92012-12-13 13:19:05 +02001067 omapfb_get_mem_region(ofbi->region);
Ville Syrjälä430571d2010-03-17 20:43:23 +02001068
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001069 r = omapfb_apply_changes(fbi, 0);
1070
Tomi Valkeinen3ed37d92012-12-13 13:19:05 +02001071 omapfb_put_mem_region(ofbi->region);
Ville Syrjälä430571d2010-03-17 20:43:23 +02001072
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001073 return r;
1074}
1075
1076static void mmap_user_open(struct vm_area_struct *vma)
1077{
Ville Syrjälä078ff542010-03-17 20:36:51 +02001078 struct omapfb2_mem_region *rg = vma->vm_private_data;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001079
Tomi Valkeinen3ed37d92012-12-13 13:19:05 +02001080 omapfb_get_mem_region(rg);
Ville Syrjälä078ff542010-03-17 20:36:51 +02001081 atomic_inc(&rg->map_count);
Tomi Valkeinen3ed37d92012-12-13 13:19:05 +02001082 omapfb_put_mem_region(rg);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001083}
1084
1085static void mmap_user_close(struct vm_area_struct *vma)
1086{
Ville Syrjälä078ff542010-03-17 20:36:51 +02001087 struct omapfb2_mem_region *rg = vma->vm_private_data;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001088
Tomi Valkeinen3ed37d92012-12-13 13:19:05 +02001089 omapfb_get_mem_region(rg);
Ville Syrjälä078ff542010-03-17 20:36:51 +02001090 atomic_dec(&rg->map_count);
Tomi Valkeinen3ed37d92012-12-13 13:19:05 +02001091 omapfb_put_mem_region(rg);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001092}
1093
1094static struct vm_operations_struct mmap_user_ops = {
1095 .open = mmap_user_open,
1096 .close = mmap_user_close,
1097};
1098
1099static int omapfb_mmap(struct fb_info *fbi, struct vm_area_struct *vma)
1100{
1101 struct omapfb_info *ofbi = FB2OFB(fbi);
1102 struct fb_fix_screeninfo *fix = &fbi->fix;
Ville Syrjälä078ff542010-03-17 20:36:51 +02001103 struct omapfb2_mem_region *rg;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001104 unsigned long off;
1105 unsigned long start;
1106 u32 len;
Ville Syrjälä430571d2010-03-17 20:43:23 +02001107 int r = -EINVAL;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001108
1109 if (vma->vm_end - vma->vm_start == 0)
1110 return 0;
1111 if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
1112 return -EINVAL;
1113 off = vma->vm_pgoff << PAGE_SHIFT;
1114
Tomi Valkeinen3ed37d92012-12-13 13:19:05 +02001115 rg = omapfb_get_mem_region(ofbi->region);
Ville Syrjälä078ff542010-03-17 20:36:51 +02001116
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001117 start = omapfb_get_region_paddr(ofbi);
1118 len = fix->smem_len;
1119 if (off >= len)
Ville Syrjälä430571d2010-03-17 20:43:23 +02001120 goto error;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001121 if ((vma->vm_end - vma->vm_start + off) > len)
Ville Syrjälä430571d2010-03-17 20:43:23 +02001122 goto error;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001123
1124 off += start;
1125
1126 DBG("user mmap region start %lx, len %d, off %lx\n", start, len, off);
1127
1128 vma->vm_pgoff = off >> PAGE_SHIFT;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07001129 /* VM_IO | VM_DONTEXPAND | VM_DONTDUMP are set by remap_pfn_range() */
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001130 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
1131 vma->vm_ops = &mmap_user_ops;
Ville Syrjälä078ff542010-03-17 20:36:51 +02001132 vma->vm_private_data = rg;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001133 if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
Ville Syrjälä430571d2010-03-17 20:43:23 +02001134 vma->vm_end - vma->vm_start,
1135 vma->vm_page_prot)) {
1136 r = -EAGAIN;
1137 goto error;
1138 }
1139
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001140 /* vm_ops.open won't be called for mmap itself. */
Ville Syrjälä078ff542010-03-17 20:36:51 +02001141 atomic_inc(&rg->map_count);
Ville Syrjälä430571d2010-03-17 20:43:23 +02001142
Tomi Valkeinen3ed37d92012-12-13 13:19:05 +02001143 omapfb_put_mem_region(rg);
1144
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001145 return 0;
Ville Syrjälä430571d2010-03-17 20:43:23 +02001146
1147 error:
Tomi Valkeinen3ed37d92012-12-13 13:19:05 +02001148 omapfb_put_mem_region(ofbi->region);
1149
Ville Syrjälä430571d2010-03-17 20:43:23 +02001150 return r;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001151}
1152
1153/* Store a single color palette entry into a pseudo palette or the hardware
1154 * palette if one is available. For now we support only 16bpp and thus store
1155 * the entry only to the pseudo palette.
1156 */
1157static int _setcolreg(struct fb_info *fbi, u_int regno, u_int red, u_int green,
1158 u_int blue, u_int transp, int update_hw_pal)
1159{
1160 /*struct omapfb_info *ofbi = FB2OFB(fbi);*/
1161 /*struct omapfb2_device *fbdev = ofbi->fbdev;*/
1162 struct fb_var_screeninfo *var = &fbi->var;
1163 int r = 0;
1164
1165 enum omapfb_color_format mode = OMAPFB_COLOR_RGB24U; /* XXX */
1166
1167 /*switch (plane->color_mode) {*/
1168 switch (mode) {
1169 case OMAPFB_COLOR_YUV422:
1170 case OMAPFB_COLOR_YUV420:
1171 case OMAPFB_COLOR_YUY422:
1172 r = -EINVAL;
1173 break;
1174 case OMAPFB_COLOR_CLUT_8BPP:
1175 case OMAPFB_COLOR_CLUT_4BPP:
1176 case OMAPFB_COLOR_CLUT_2BPP:
1177 case OMAPFB_COLOR_CLUT_1BPP:
1178 /*
1179 if (fbdev->ctrl->setcolreg)
1180 r = fbdev->ctrl->setcolreg(regno, red, green, blue,
1181 transp, update_hw_pal);
1182 */
1183 /* Fallthrough */
1184 r = -EINVAL;
1185 break;
1186 case OMAPFB_COLOR_RGB565:
1187 case OMAPFB_COLOR_RGB444:
1188 case OMAPFB_COLOR_RGB24P:
1189 case OMAPFB_COLOR_RGB24U:
1190 if (r != 0)
1191 break;
1192
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001193 if (regno < 16) {
Grazvydas Ignotasc1c52842012-08-21 09:09:48 +03001194 u32 pal;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001195 pal = ((red >> (16 - var->red.length)) <<
1196 var->red.offset) |
1197 ((green >> (16 - var->green.length)) <<
1198 var->green.offset) |
1199 (blue >> (16 - var->blue.length));
1200 ((u32 *)(fbi->pseudo_palette))[regno] = pal;
1201 }
1202 break;
1203 default:
1204 BUG();
1205 }
1206 return r;
1207}
1208
1209static int omapfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
1210 u_int transp, struct fb_info *info)
1211{
1212 DBG("setcolreg\n");
1213
1214 return _setcolreg(info, regno, red, green, blue, transp, 1);
1215}
1216
1217static int omapfb_setcmap(struct fb_cmap *cmap, struct fb_info *info)
1218{
1219 int count, index, r;
1220 u16 *red, *green, *blue, *transp;
1221 u16 trans = 0xffff;
1222
1223 DBG("setcmap\n");
1224
1225 red = cmap->red;
1226 green = cmap->green;
1227 blue = cmap->blue;
1228 transp = cmap->transp;
1229 index = cmap->start;
1230
1231 for (count = 0; count < cmap->len; count++) {
1232 if (transp)
1233 trans = *transp++;
1234 r = _setcolreg(info, index++, *red++, *green++, *blue++, trans,
1235 count == cmap->len - 1);
1236 if (r != 0)
1237 return r;
1238 }
1239
1240 return 0;
1241}
1242
1243static int omapfb_blank(int blank, struct fb_info *fbi)
1244{
1245 struct omapfb_info *ofbi = FB2OFB(fbi);
1246 struct omapfb2_device *fbdev = ofbi->fbdev;
1247 struct omap_dss_device *display = fb2display(fbi);
Tomi Valkeinen27cc2132011-04-30 16:55:12 +03001248 struct omapfb_display_data *d;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001249 int r = 0;
1250
Jani Nikula93255882010-06-01 15:12:12 +03001251 if (!display)
1252 return -EINVAL;
1253
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001254 omapfb_lock(fbdev);
1255
Tomi Valkeinen27cc2132011-04-30 16:55:12 +03001256 d = get_display_data(fbdev, display);
1257
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001258 switch (blank) {
1259 case FB_BLANK_UNBLANK:
Tomi Valkeinen998c3362012-05-30 13:26:00 +03001260 if (display->state == OMAP_DSS_DISPLAY_ACTIVE)
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001261 goto exit;
1262
Tomi Valkeinen998c3362012-05-30 13:26:00 +03001263 r = display->driver->enable(display);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001264
Tomi Valkeinen27cc2132011-04-30 16:55:12 +03001265 if ((display->caps & OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE) &&
1266 d->update_mode == OMAPFB_AUTO_UPDATE &&
1267 !d->auto_update_work_enabled)
1268 omapfb_start_auto_update(fbdev, display);
1269
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001270 break;
1271
1272 case FB_BLANK_NORMAL:
1273 /* FB_BLANK_NORMAL could be implemented.
1274 * Needs DSS additions. */
1275 case FB_BLANK_VSYNC_SUSPEND:
1276 case FB_BLANK_HSYNC_SUSPEND:
1277 case FB_BLANK_POWERDOWN:
1278 if (display->state != OMAP_DSS_DISPLAY_ACTIVE)
1279 goto exit;
1280
Tomi Valkeinen27cc2132011-04-30 16:55:12 +03001281 if (d->auto_update_work_enabled)
1282 omapfb_stop_auto_update(fbdev, display);
1283
Tomi Valkeinen998c3362012-05-30 13:26:00 +03001284 display->driver->disable(display);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001285
1286 break;
1287
1288 default:
1289 r = -EINVAL;
1290 }
1291
1292exit:
1293 omapfb_unlock(fbdev);
1294
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001295 return r;
1296}
1297
1298#if 0
1299/* XXX fb_read and fb_write are needed for VRFB */
1300ssize_t omapfb_write(struct fb_info *info, const char __user *buf,
1301 size_t count, loff_t *ppos)
1302{
1303 DBG("omapfb_write %d, %lu\n", count, (unsigned long)*ppos);
1304 /* XXX needed for VRFB */
1305 return count;
1306}
1307#endif
1308
1309static struct fb_ops omapfb_ops = {
1310 .owner = THIS_MODULE,
1311 .fb_open = omapfb_open,
1312 .fb_release = omapfb_release,
1313 .fb_fillrect = cfb_fillrect,
1314 .fb_copyarea = cfb_copyarea,
1315 .fb_imageblit = cfb_imageblit,
1316 .fb_blank = omapfb_blank,
1317 .fb_ioctl = omapfb_ioctl,
1318 .fb_check_var = omapfb_check_var,
1319 .fb_set_par = omapfb_set_par,
1320 .fb_pan_display = omapfb_pan_display,
1321 .fb_mmap = omapfb_mmap,
1322 .fb_setcolreg = omapfb_setcolreg,
1323 .fb_setcmap = omapfb_setcmap,
1324 /*.fb_write = omapfb_write,*/
1325};
1326
1327static void omapfb_free_fbmem(struct fb_info *fbi)
1328{
1329 struct omapfb_info *ofbi = FB2OFB(fbi);
1330 struct omapfb2_device *fbdev = ofbi->fbdev;
1331 struct omapfb2_mem_region *rg;
1332
Ville Syrjälä078ff542010-03-17 20:36:51 +02001333 rg = ofbi->region;
1334
Tomi Valkeinen0049fb22012-11-09 15:52:20 +02001335 if (rg->token == NULL)
1336 return;
1337
Ville Syrjälä078ff542010-03-17 20:36:51 +02001338 WARN_ON(atomic_read(&rg->map_count));
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001339
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001340 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
1341 /* unmap the 0 angle rotation */
1342 if (rg->vrfb.vaddr[0]) {
1343 iounmap(rg->vrfb.vaddr[0]);
Tomi Valkeinenf3a82d12010-01-07 13:37:30 +02001344 rg->vrfb.vaddr[0] = NULL;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001345 }
Tomi Valkeinen0049fb22012-11-09 15:52:20 +02001346
1347 omap_vrfb_release_ctx(&rg->vrfb);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001348 }
1349
Tomi Valkeinen0049fb22012-11-09 15:52:20 +02001350 dma_free_attrs(fbdev->dev, rg->size, rg->token, rg->dma_handle,
1351 &rg->attrs);
1352
1353 rg->token = NULL;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001354 rg->vaddr = NULL;
1355 rg->paddr = 0;
1356 rg->alloc = 0;
1357 rg->size = 0;
1358}
1359
1360static void clear_fb_info(struct fb_info *fbi)
1361{
1362 memset(&fbi->var, 0, sizeof(fbi->var));
1363 memset(&fbi->fix, 0, sizeof(fbi->fix));
1364 strlcpy(fbi->fix.id, MODULE_NAME, sizeof(fbi->fix.id));
1365}
1366
1367static int omapfb_free_all_fbmem(struct omapfb2_device *fbdev)
1368{
1369 int i;
1370
1371 DBG("free all fbmem\n");
1372
1373 for (i = 0; i < fbdev->num_fbs; i++) {
1374 struct fb_info *fbi = fbdev->fbs[i];
1375 omapfb_free_fbmem(fbi);
1376 clear_fb_info(fbi);
1377 }
1378
1379 return 0;
1380}
1381
1382static int omapfb_alloc_fbmem(struct fb_info *fbi, unsigned long size,
1383 unsigned long paddr)
1384{
1385 struct omapfb_info *ofbi = FB2OFB(fbi);
1386 struct omapfb2_device *fbdev = ofbi->fbdev;
1387 struct omapfb2_mem_region *rg;
Tomi Valkeinen0049fb22012-11-09 15:52:20 +02001388 void *token;
1389 DEFINE_DMA_ATTRS(attrs);
1390 dma_addr_t dma_handle;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001391 int r;
1392
Ville Syrjälä078ff542010-03-17 20:36:51 +02001393 rg = ofbi->region;
1394
1395 rg->paddr = 0;
1396 rg->vaddr = NULL;
1397 memset(&rg->vrfb, 0, sizeof rg->vrfb);
1398 rg->size = 0;
1399 rg->type = 0;
1400 rg->alloc = false;
1401 rg->map = false;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001402
1403 size = PAGE_ALIGN(size);
1404
Tomi Valkeinen0049fb22012-11-09 15:52:20 +02001405 dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs);
1406
1407 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
1408 dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &attrs);
1409
1410 DBG("allocating %lu bytes for fb %d\n", size, ofbi->id);
1411
1412 token = dma_alloc_attrs(fbdev->dev, size, &dma_handle,
1413 GFP_KERNEL, &attrs);
1414
1415 if (token == NULL) {
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001416 dev_err(fbdev->dev, "failed to allocate framebuffer\n");
1417 return -ENOMEM;
1418 }
1419
Tomi Valkeinen0049fb22012-11-09 15:52:20 +02001420 DBG("allocated VRAM paddr %lx, vaddr %p\n",
1421 (unsigned long)dma_handle, token);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001422
Tomi Valkeinen0049fb22012-11-09 15:52:20 +02001423 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001424 r = omap_vrfb_request_ctx(&rg->vrfb);
1425 if (r) {
Tomi Valkeinen0049fb22012-11-09 15:52:20 +02001426 dma_free_attrs(fbdev->dev, size, token, dma_handle,
1427 &attrs);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001428 dev_err(fbdev->dev, "vrfb create ctx failed\n");
1429 return r;
1430 }
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001431 }
1432
Tomi Valkeinen0049fb22012-11-09 15:52:20 +02001433 rg->attrs = attrs;
1434 rg->token = token;
1435 rg->dma_handle = dma_handle;
1436
1437 rg->paddr = (unsigned long)dma_handle;
1438 rg->vaddr = (void __iomem *)token;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001439 rg->size = size;
1440 rg->alloc = 1;
1441
1442 return 0;
1443}
1444
1445/* allocate fbmem using display resolution as reference */
1446static int omapfb_alloc_fbmem_display(struct fb_info *fbi, unsigned long size,
1447 unsigned long paddr)
1448{
1449 struct omapfb_info *ofbi = FB2OFB(fbi);
Tomi Valkeinena2699502010-01-11 14:33:40 +02001450 struct omapfb2_device *fbdev = ofbi->fbdev;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001451 struct omap_dss_device *display;
1452 int bytespp;
1453
1454 display = fb2display(fbi);
1455
1456 if (!display)
1457 return 0;
1458
Tomi Valkeinena2699502010-01-11 14:33:40 +02001459 switch (omapfb_get_recommended_bpp(fbdev, display)) {
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001460 case 16:
1461 bytespp = 2;
1462 break;
1463 case 24:
1464 bytespp = 4;
1465 break;
1466 default:
1467 bytespp = 4;
1468 break;
1469 }
1470
1471 if (!size) {
1472 u16 w, h;
1473
Tomi Valkeinen96adcec2010-01-11 13:54:33 +02001474 display->driver->get_resolution(display, &w, &h);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001475
1476 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
1477 size = max(omap_vrfb_min_phys_size(w, h, bytespp),
1478 omap_vrfb_min_phys_size(h, w, bytespp));
1479
1480 DBG("adjusting fb mem size for VRFB, %u -> %lu\n",
1481 w * h * bytespp, size);
1482 } else {
1483 size = w * h * bytespp;
1484 }
1485 }
1486
1487 if (!size)
1488 return 0;
1489
1490 return omapfb_alloc_fbmem(fbi, size, paddr);
1491}
1492
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001493static int omapfb_parse_vram_param(const char *param, int max_entries,
1494 unsigned long *sizes, unsigned long *paddrs)
1495{
1496 int fbnum;
1497 unsigned long size;
1498 unsigned long paddr = 0;
1499 char *p, *start;
1500
1501 start = (char *)param;
1502
1503 while (1) {
1504 p = start;
1505
1506 fbnum = simple_strtoul(p, &p, 10);
1507
Tomi Valkeinen3a028bb2012-05-10 11:15:14 +03001508 if (p == start)
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001509 return -EINVAL;
1510
1511 if (*p != ':')
1512 return -EINVAL;
1513
1514 if (fbnum >= max_entries)
1515 return -EINVAL;
1516
1517 size = memparse(p + 1, &p);
1518
1519 if (!size)
1520 return -EINVAL;
1521
1522 paddr = 0;
1523
1524 if (*p == '@') {
1525 paddr = simple_strtoul(p + 1, &p, 16);
1526
1527 if (!paddr)
1528 return -EINVAL;
1529
1530 }
1531
Tomi Valkeinen09a8c452012-12-04 16:26:09 +02001532 WARN_ONCE(paddr,
1533 "reserving memory at predefined address not supported\n");
1534
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001535 paddrs[fbnum] = paddr;
1536 sizes[fbnum] = size;
1537
1538 if (*p == 0)
1539 break;
1540
1541 if (*p != ',')
1542 return -EINVAL;
1543
1544 ++p;
1545
1546 start = p;
1547 }
1548
1549 return 0;
1550}
1551
1552static int omapfb_allocate_all_fbs(struct omapfb2_device *fbdev)
1553{
1554 int i, r;
1555 unsigned long vram_sizes[10];
1556 unsigned long vram_paddrs[10];
1557
1558 memset(&vram_sizes, 0, sizeof(vram_sizes));
1559 memset(&vram_paddrs, 0, sizeof(vram_paddrs));
1560
1561 if (def_vram && omapfb_parse_vram_param(def_vram, 10,
1562 vram_sizes, vram_paddrs)) {
1563 dev_err(fbdev->dev, "failed to parse vram parameter\n");
1564
1565 memset(&vram_sizes, 0, sizeof(vram_sizes));
1566 memset(&vram_paddrs, 0, sizeof(vram_paddrs));
1567 }
1568
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001569 for (i = 0; i < fbdev->num_fbs; i++) {
1570 /* allocate memory automatically only for fb0, or if
1571 * excplicitly defined with vram or plat data option */
1572 if (i == 0 || vram_sizes[i] != 0) {
1573 r = omapfb_alloc_fbmem_display(fbdev->fbs[i],
1574 vram_sizes[i], vram_paddrs[i]);
1575
1576 if (r)
1577 return r;
1578 }
1579 }
1580
1581 for (i = 0; i < fbdev->num_fbs; i++) {
1582 struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[i]);
1583 struct omapfb2_mem_region *rg;
Ville Syrjälä078ff542010-03-17 20:36:51 +02001584 rg = ofbi->region;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001585
1586 DBG("region%d phys %08x virt %p size=%lu\n",
1587 i,
1588 rg->paddr,
1589 rg->vaddr,
1590 rg->size);
1591 }
1592
1593 return 0;
1594}
1595
Tomi Valkeinenca444152012-08-30 16:53:29 +03001596static void omapfb_clear_fb(struct fb_info *fbi)
1597{
1598 const struct fb_fillrect rect = {
1599 .dx = 0,
1600 .dy = 0,
1601 .width = fbi->var.xres_virtual,
1602 .height = fbi->var.yres_virtual,
1603 .color = 0,
1604 .rop = ROP_COPY,
1605 };
1606
1607 cfb_fillrect(fbi, &rect);
1608}
1609
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001610int omapfb_realloc_fbmem(struct fb_info *fbi, unsigned long size, int type)
1611{
1612 struct omapfb_info *ofbi = FB2OFB(fbi);
1613 struct omapfb2_device *fbdev = ofbi->fbdev;
Ville Syrjälä078ff542010-03-17 20:36:51 +02001614 struct omapfb2_mem_region *rg = ofbi->region;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001615 unsigned long old_size = rg->size;
1616 unsigned long old_paddr = rg->paddr;
1617 int old_type = rg->type;
1618 int r;
1619
Tomi Valkeinen2a803c82011-04-18 13:18:20 +03001620 if (type != OMAPFB_MEMTYPE_SDRAM)
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001621 return -EINVAL;
1622
1623 size = PAGE_ALIGN(size);
1624
1625 if (old_size == size && old_type == type)
1626 return 0;
1627
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001628 omapfb_free_fbmem(fbi);
1629
1630 if (size == 0) {
1631 clear_fb_info(fbi);
1632 return 0;
1633 }
1634
1635 r = omapfb_alloc_fbmem(fbi, size, 0);
1636
1637 if (r) {
1638 if (old_size)
1639 omapfb_alloc_fbmem(fbi, old_size, old_paddr);
1640
1641 if (rg->size == 0)
1642 clear_fb_info(fbi);
1643
1644 return r;
1645 }
1646
1647 if (old_size == size)
1648 return 0;
1649
1650 if (old_size == 0) {
1651 DBG("initializing fb %d\n", ofbi->id);
1652 r = omapfb_fb_init(fbdev, fbi);
1653 if (r) {
1654 DBG("omapfb_fb_init failed\n");
1655 goto err;
1656 }
1657 r = omapfb_apply_changes(fbi, 1);
1658 if (r) {
1659 DBG("omapfb_apply_changes failed\n");
1660 goto err;
1661 }
1662 } else {
1663 struct fb_var_screeninfo new_var;
1664 memcpy(&new_var, &fbi->var, sizeof(new_var));
1665 r = check_fb_var(fbi, &new_var);
1666 if (r)
1667 goto err;
1668 memcpy(&fbi->var, &new_var, sizeof(fbi->var));
1669 set_fb_fix(fbi);
1670 r = setup_vrfb_rotation(fbi);
1671 if (r)
1672 goto err;
1673 }
1674
Tomi Valkeinenca444152012-08-30 16:53:29 +03001675 omapfb_clear_fb(fbi);
1676
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001677 return 0;
1678err:
1679 omapfb_free_fbmem(fbi);
1680 clear_fb_info(fbi);
1681 return r;
1682}
1683
Tomi Valkeinen27cc2132011-04-30 16:55:12 +03001684static void omapfb_auto_update_work(struct work_struct *work)
1685{
1686 struct omap_dss_device *dssdev;
1687 struct omap_dss_driver *dssdrv;
1688 struct omapfb_display_data *d;
1689 u16 w, h;
1690 unsigned int freq;
1691 struct omapfb2_device *fbdev;
1692
1693 d = container_of(work, struct omapfb_display_data,
1694 auto_update_work.work);
1695
1696 dssdev = d->dssdev;
1697 dssdrv = dssdev->driver;
1698 fbdev = d->fbdev;
1699
1700 if (!dssdrv || !dssdrv->update)
1701 return;
1702
1703 if (dssdrv->sync)
1704 dssdrv->sync(dssdev);
1705
1706 dssdrv->get_resolution(dssdev, &w, &h);
1707 dssdrv->update(dssdev, 0, 0, w, h);
1708
1709 freq = auto_update_freq;
1710 if (freq == 0)
1711 freq = 20;
1712 queue_delayed_work(fbdev->auto_update_wq,
1713 &d->auto_update_work, HZ / freq);
1714}
1715
1716void omapfb_start_auto_update(struct omapfb2_device *fbdev,
1717 struct omap_dss_device *display)
1718{
1719 struct omapfb_display_data *d;
1720
1721 if (fbdev->auto_update_wq == NULL) {
1722 struct workqueue_struct *wq;
1723
1724 wq = create_singlethread_workqueue("omapfb_auto_update");
1725
1726 if (wq == NULL) {
1727 dev_err(fbdev->dev, "Failed to create workqueue for "
1728 "auto-update\n");
1729 return;
1730 }
1731
1732 fbdev->auto_update_wq = wq;
1733 }
1734
1735 d = get_display_data(fbdev, display);
1736
1737 INIT_DELAYED_WORK(&d->auto_update_work, omapfb_auto_update_work);
1738
1739 d->auto_update_work_enabled = true;
1740
1741 omapfb_auto_update_work(&d->auto_update_work.work);
1742}
1743
1744void omapfb_stop_auto_update(struct omapfb2_device *fbdev,
1745 struct omap_dss_device *display)
1746{
1747 struct omapfb_display_data *d;
1748
1749 d = get_display_data(fbdev, display);
1750
1751 cancel_delayed_work_sync(&d->auto_update_work);
1752
1753 d->auto_update_work_enabled = false;
1754}
1755
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001756/* initialize fb_info, var, fix to something sane based on the display */
1757static int omapfb_fb_init(struct omapfb2_device *fbdev, struct fb_info *fbi)
1758{
1759 struct fb_var_screeninfo *var = &fbi->var;
1760 struct omap_dss_device *display = fb2display(fbi);
1761 struct omapfb_info *ofbi = FB2OFB(fbi);
1762 int r = 0;
1763
1764 fbi->fbops = &omapfb_ops;
1765 fbi->flags = FBINFO_FLAG_DEFAULT;
1766 fbi->pseudo_palette = fbdev->pseudo_palette;
1767
Ville Syrjälä078ff542010-03-17 20:36:51 +02001768 if (ofbi->region->size == 0) {
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001769 clear_fb_info(fbi);
1770 return 0;
1771 }
1772
1773 var->nonstd = 0;
1774 var->bits_per_pixel = 0;
1775
1776 var->rotate = def_rotate;
1777
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001778 if (display) {
1779 u16 w, h;
1780 int rotation = (var->rotate + ofbi->rotation[0]) % 4;
1781
Tomi Valkeinen96adcec2010-01-11 13:54:33 +02001782 display->driver->get_resolution(display, &w, &h);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001783
1784 if (rotation == FB_ROTATE_CW ||
1785 rotation == FB_ROTATE_CCW) {
1786 var->xres = h;
1787 var->yres = w;
1788 } else {
1789 var->xres = w;
1790 var->yres = h;
1791 }
1792
1793 var->xres_virtual = var->xres;
1794 var->yres_virtual = var->yres;
1795
1796 if (!var->bits_per_pixel) {
Tomi Valkeinena2699502010-01-11 14:33:40 +02001797 switch (omapfb_get_recommended_bpp(fbdev, display)) {
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001798 case 16:
1799 var->bits_per_pixel = 16;
1800 break;
1801 case 24:
1802 var->bits_per_pixel = 32;
1803 break;
1804 default:
1805 dev_err(fbdev->dev, "illegal display "
1806 "bpp\n");
1807 return -EINVAL;
1808 }
1809 }
1810 } else {
1811 /* if there's no display, let's just guess some basic values */
1812 var->xres = 320;
1813 var->yres = 240;
1814 var->xres_virtual = var->xres;
1815 var->yres_virtual = var->yres;
1816 if (!var->bits_per_pixel)
1817 var->bits_per_pixel = 16;
1818 }
1819
1820 r = check_fb_var(fbi, var);
1821 if (r)
1822 goto err;
1823
1824 set_fb_fix(fbi);
1825 r = setup_vrfb_rotation(fbi);
1826 if (r)
1827 goto err;
1828
1829 r = fb_alloc_cmap(&fbi->cmap, 256, 0);
1830 if (r)
1831 dev_err(fbdev->dev, "unable to allocate color map memory\n");
1832
1833err:
1834 return r;
1835}
1836
1837static void fbinfo_cleanup(struct omapfb2_device *fbdev, struct fb_info *fbi)
1838{
1839 fb_dealloc_cmap(&fbi->cmap);
1840}
1841
1842
1843static void omapfb_free_resources(struct omapfb2_device *fbdev)
1844{
1845 int i;
1846
1847 DBG("free_resources\n");
1848
1849 if (fbdev == NULL)
1850 return;
1851
1852 for (i = 0; i < fbdev->num_fbs; i++)
1853 unregister_framebuffer(fbdev->fbs[i]);
1854
1855 /* free the reserved fbmem */
1856 omapfb_free_all_fbmem(fbdev);
1857
1858 for (i = 0; i < fbdev->num_fbs; i++) {
1859 fbinfo_cleanup(fbdev, fbdev->fbs[i]);
1860 framebuffer_release(fbdev->fbs[i]);
1861 }
1862
1863 for (i = 0; i < fbdev->num_displays; i++) {
Tomi Valkeinen065a40b2011-04-30 12:13:14 +03001864 struct omap_dss_device *dssdev = fbdev->displays[i].dssdev;
Tomi Valkeinen27cc2132011-04-30 16:55:12 +03001865
1866 if (fbdev->displays[i].auto_update_work_enabled)
1867 omapfb_stop_auto_update(fbdev, dssdev);
1868
Tomi Valkeinen065a40b2011-04-30 12:13:14 +03001869 if (dssdev->state != OMAP_DSS_DISPLAY_DISABLED)
1870 dssdev->driver->disable(dssdev);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001871
Tomi Valkeinen065a40b2011-04-30 12:13:14 +03001872 omap_dss_put_device(dssdev);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001873 }
1874
Tomi Valkeinen27cc2132011-04-30 16:55:12 +03001875 if (fbdev->auto_update_wq != NULL) {
1876 flush_workqueue(fbdev->auto_update_wq);
1877 destroy_workqueue(fbdev->auto_update_wq);
1878 fbdev->auto_update_wq = NULL;
1879 }
1880
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001881 dev_set_drvdata(fbdev->dev, NULL);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001882}
1883
1884static int omapfb_create_framebuffers(struct omapfb2_device *fbdev)
1885{
1886 int r, i;
1887
1888 fbdev->num_fbs = 0;
1889
1890 DBG("create %d framebuffers\n", CONFIG_FB_OMAP2_NUM_FBS);
1891
1892 /* allocate fb_infos */
1893 for (i = 0; i < CONFIG_FB_OMAP2_NUM_FBS; i++) {
1894 struct fb_info *fbi;
1895 struct omapfb_info *ofbi;
1896
1897 fbi = framebuffer_alloc(sizeof(struct omapfb_info),
1898 fbdev->dev);
1899
1900 if (fbi == NULL) {
1901 dev_err(fbdev->dev,
1902 "unable to allocate memory for plane info\n");
1903 return -ENOMEM;
1904 }
1905
1906 clear_fb_info(fbi);
1907
1908 fbdev->fbs[i] = fbi;
1909
1910 ofbi = FB2OFB(fbi);
1911 ofbi->fbdev = fbdev;
1912 ofbi->id = i;
1913
Ville Syrjälä078ff542010-03-17 20:36:51 +02001914 ofbi->region = &fbdev->regions[i];
1915 ofbi->region->id = i;
Tomi Valkeinen3ed37d92012-12-13 13:19:05 +02001916 init_rwsem(&ofbi->region->lock);
Ville Syrjälä078ff542010-03-17 20:36:51 +02001917
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001918 /* assign these early, so that fb alloc can use them */
1919 ofbi->rotation_type = def_vrfb ? OMAP_DSS_ROT_VRFB :
1920 OMAP_DSS_ROT_DMA;
1921 ofbi->mirror = def_mirror;
1922
1923 fbdev->num_fbs++;
1924 }
1925
1926 DBG("fb_infos allocated\n");
1927
1928 /* assign overlays for the fbs */
1929 for (i = 0; i < min(fbdev->num_fbs, fbdev->num_overlays); i++) {
1930 struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[i]);
1931
1932 ofbi->overlays[0] = fbdev->overlays[i];
1933 ofbi->num_overlays = 1;
1934 }
1935
1936 /* allocate fb memories */
1937 r = omapfb_allocate_all_fbs(fbdev);
1938 if (r) {
1939 dev_err(fbdev->dev, "failed to allocate fbmem\n");
1940 return r;
1941 }
1942
1943 DBG("fbmems allocated\n");
1944
1945 /* setup fb_infos */
1946 for (i = 0; i < fbdev->num_fbs; i++) {
Ville Syrjälä430571d2010-03-17 20:43:23 +02001947 struct fb_info *fbi = fbdev->fbs[i];
Tomi Valkeinen3ed37d92012-12-13 13:19:05 +02001948 struct omapfb_info *ofbi = FB2OFB(fbi);
Ville Syrjälä430571d2010-03-17 20:43:23 +02001949
Tomi Valkeinen3ed37d92012-12-13 13:19:05 +02001950 omapfb_get_mem_region(ofbi->region);
Ville Syrjälä430571d2010-03-17 20:43:23 +02001951 r = omapfb_fb_init(fbdev, fbi);
Tomi Valkeinen3ed37d92012-12-13 13:19:05 +02001952 omapfb_put_mem_region(ofbi->region);
1953
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001954 if (r) {
1955 dev_err(fbdev->dev, "failed to setup fb_info\n");
1956 return r;
1957 }
1958 }
1959
Archit Tanejafda7c362012-09-11 15:34:08 +05301960 for (i = 0; i < fbdev->num_fbs; i++) {
1961 struct fb_info *fbi = fbdev->fbs[i];
1962 struct omapfb_info *ofbi = FB2OFB(fbi);
1963
1964 if (ofbi->region->size == 0)
1965 continue;
1966
1967 omapfb_clear_fb(fbi);
1968 }
1969
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001970 DBG("fb_infos initialized\n");
1971
1972 for (i = 0; i < fbdev->num_fbs; i++) {
1973 r = register_framebuffer(fbdev->fbs[i]);
1974 if (r != 0) {
1975 dev_err(fbdev->dev,
1976 "registering framebuffer %d failed\n", i);
1977 return r;
1978 }
1979 }
1980
1981 DBG("framebuffers registered\n");
1982
1983 for (i = 0; i < fbdev->num_fbs; i++) {
Ville Syrjälä430571d2010-03-17 20:43:23 +02001984 struct fb_info *fbi = fbdev->fbs[i];
Tomi Valkeinen3ed37d92012-12-13 13:19:05 +02001985 struct omapfb_info *ofbi = FB2OFB(fbi);
Ville Syrjälä430571d2010-03-17 20:43:23 +02001986
Tomi Valkeinen3ed37d92012-12-13 13:19:05 +02001987 omapfb_get_mem_region(ofbi->region);
Ville Syrjälä430571d2010-03-17 20:43:23 +02001988 r = omapfb_apply_changes(fbi, 1);
Tomi Valkeinen3ed37d92012-12-13 13:19:05 +02001989 omapfb_put_mem_region(ofbi->region);
1990
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001991 if (r) {
1992 dev_err(fbdev->dev, "failed to change mode\n");
1993 return r;
1994 }
1995 }
1996
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03001997 /* Enable fb0 */
1998 if (fbdev->num_fbs > 0) {
1999 struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[0]);
2000
2001 if (ofbi->num_overlays > 0) {
2002 struct omap_overlay *ovl = ofbi->overlays[0];
2003
Tomi Valkeinenaaa874a2011-11-15 16:37:53 +02002004 ovl->manager->apply(ovl->manager);
2005
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002006 r = omapfb_overlay_enable(ovl, 1);
2007
2008 if (r) {
2009 dev_err(fbdev->dev,
2010 "failed to enable overlay\n");
2011 return r;
2012 }
2013 }
2014 }
2015
2016 DBG("create_framebuffers done\n");
2017
2018 return 0;
2019}
2020
2021static int omapfb_mode_to_timings(const char *mode_str,
Archit Taneja783babf2012-06-15 07:39:47 +05302022 struct omap_dss_device *display,
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002023 struct omap_video_timings *timings, u8 *bpp)
2024{
Tomi Valkeinen897044e2011-04-30 15:26:40 +03002025 struct fb_info *fbi;
2026 struct fb_var_screeninfo *var;
2027 struct fb_ops *fbops;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002028 int r;
2029
2030#ifdef CONFIG_OMAP2_DSS_VENC
2031 if (strcmp(mode_str, "pal") == 0) {
2032 *timings = omap_dss_pal_timings;
Maurus Cuelenaeree8c66dc2010-07-22 00:40:58 +02002033 *bpp = 24;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002034 return 0;
2035 } else if (strcmp(mode_str, "ntsc") == 0) {
2036 *timings = omap_dss_ntsc_timings;
Maurus Cuelenaeree8c66dc2010-07-22 00:40:58 +02002037 *bpp = 24;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002038 return 0;
2039 }
2040#endif
2041
2042 /* this is quite a hack, but I wanted to use the modedb and for
2043 * that we need fb_info and var, so we create dummy ones */
2044
Tomi Valkeinen897044e2011-04-30 15:26:40 +03002045 *bpp = 0;
2046 fbi = NULL;
2047 var = NULL;
2048 fbops = NULL;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002049
Tomi Valkeinen897044e2011-04-30 15:26:40 +03002050 fbi = kzalloc(sizeof(*fbi), GFP_KERNEL);
2051 if (fbi == NULL) {
2052 r = -ENOMEM;
2053 goto err;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002054 }
Tomi Valkeinen897044e2011-04-30 15:26:40 +03002055
2056 var = kzalloc(sizeof(*var), GFP_KERNEL);
2057 if (var == NULL) {
2058 r = -ENOMEM;
2059 goto err;
2060 }
2061
2062 fbops = kzalloc(sizeof(*fbops), GFP_KERNEL);
2063 if (fbops == NULL) {
2064 r = -ENOMEM;
2065 goto err;
2066 }
2067
2068 fbi->fbops = fbops;
2069
2070 r = fb_find_mode(var, fbi, mode_str, NULL, 0, NULL, 24);
2071 if (r == 0) {
2072 r = -EINVAL;
2073 goto err;
2074 }
2075
Archit Taneja783babf2012-06-15 07:39:47 +05302076 if (display->driver->get_timings) {
2077 display->driver->get_timings(display, timings);
2078 } else {
2079 timings->data_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE;
2080 timings->de_level = OMAPDSS_SIG_ACTIVE_HIGH;
2081 timings->sync_pclk_edge = OMAPDSS_DRIVE_SIG_OPPOSITE_EDGES;
2082 }
2083
Tomi Valkeinen897044e2011-04-30 15:26:40 +03002084 timings->pixel_clock = PICOS2KHZ(var->pixclock);
2085 timings->hbp = var->left_margin;
2086 timings->hfp = var->right_margin;
2087 timings->vbp = var->upper_margin;
2088 timings->vfp = var->lower_margin;
2089 timings->hsw = var->hsync_len;
2090 timings->vsw = var->vsync_len;
2091 timings->x_res = var->xres;
2092 timings->y_res = var->yres;
Archit Taneja783babf2012-06-15 07:39:47 +05302093 timings->hsync_level = var->sync & FB_SYNC_HOR_HIGH_ACT ?
2094 OMAPDSS_SIG_ACTIVE_HIGH :
2095 OMAPDSS_SIG_ACTIVE_LOW;
2096 timings->vsync_level = var->sync & FB_SYNC_VERT_HIGH_ACT ?
2097 OMAPDSS_SIG_ACTIVE_HIGH :
2098 OMAPDSS_SIG_ACTIVE_LOW;
Archit Taneja23bae3a2012-06-28 18:23:18 +05302099 timings->interlace = var->vmode & FB_VMODE_INTERLACED;
Tomi Valkeinen897044e2011-04-30 15:26:40 +03002100
2101 switch (var->bits_per_pixel) {
2102 case 16:
2103 *bpp = 16;
2104 break;
2105 case 24:
2106 case 32:
2107 default:
2108 *bpp = 24;
2109 break;
2110 }
2111
2112 r = 0;
2113
2114err:
2115 kfree(fbi);
2116 kfree(var);
2117 kfree(fbops);
2118
2119 return r;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002120}
2121
Tomi Valkeinena2699502010-01-11 14:33:40 +02002122static int omapfb_set_def_mode(struct omapfb2_device *fbdev,
2123 struct omap_dss_device *display, char *mode_str)
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002124{
2125 int r;
2126 u8 bpp;
Janorkar, Mayuresh371e2082011-02-22 07:35:13 -06002127 struct omap_video_timings timings, temp_timings;
Tomi Valkeinen065a40b2011-04-30 12:13:14 +03002128 struct omapfb_display_data *d;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002129
Archit Taneja783babf2012-06-15 07:39:47 +05302130 r = omapfb_mode_to_timings(mode_str, display, &timings, &bpp);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002131 if (r)
2132 return r;
2133
Tomi Valkeinen065a40b2011-04-30 12:13:14 +03002134 d = get_display_data(fbdev, display);
2135 d->bpp_override = bpp;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002136
Janorkar, Mayuresh371e2082011-02-22 07:35:13 -06002137 if (display->driver->check_timings) {
2138 r = display->driver->check_timings(display, &timings);
2139 if (r)
2140 return r;
2141 } else {
2142 /* If check_timings is not present compare xres and yres */
2143 if (display->driver->get_timings) {
2144 display->driver->get_timings(display, &temp_timings);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002145
Janorkar, Mayuresh371e2082011-02-22 07:35:13 -06002146 if (temp_timings.x_res != timings.x_res ||
2147 temp_timings.y_res != timings.y_res)
2148 return -EINVAL;
2149 }
2150 }
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002151
Janorkar, Mayuresh371e2082011-02-22 07:35:13 -06002152 if (display->driver->set_timings)
2153 display->driver->set_timings(display, &timings);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002154
2155 return 0;
2156}
2157
Tomi Valkeinena2699502010-01-11 14:33:40 +02002158static int omapfb_get_recommended_bpp(struct omapfb2_device *fbdev,
2159 struct omap_dss_device *dssdev)
2160{
Tomi Valkeinen065a40b2011-04-30 12:13:14 +03002161 struct omapfb_display_data *d;
Tomi Valkeinena2699502010-01-11 14:33:40 +02002162
2163 BUG_ON(dssdev->driver->get_recommended_bpp == NULL);
2164
Tomi Valkeinen065a40b2011-04-30 12:13:14 +03002165 d = get_display_data(fbdev, dssdev);
2166
2167 if (d->bpp_override != 0)
2168 return d->bpp_override;
Tomi Valkeinena2699502010-01-11 14:33:40 +02002169
2170 return dssdev->driver->get_recommended_bpp(dssdev);
2171}
2172
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002173static int omapfb_parse_def_modes(struct omapfb2_device *fbdev)
2174{
2175 char *str, *options, *this_opt;
2176 int r = 0;
2177
Samreen36e8c272010-11-16 12:49:07 +01002178 str = kstrdup(def_mode, GFP_KERNEL);
2179 if (!str)
2180 return -ENOMEM;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002181 options = str;
2182
2183 while (!r && (this_opt = strsep(&options, ",")) != NULL) {
2184 char *p, *display_str, *mode_str;
2185 struct omap_dss_device *display;
2186 int i;
2187
2188 p = strchr(this_opt, ':');
2189 if (!p) {
2190 r = -EINVAL;
2191 break;
2192 }
2193
2194 *p = 0;
2195 display_str = this_opt;
2196 mode_str = p + 1;
2197
2198 display = NULL;
2199 for (i = 0; i < fbdev->num_displays; ++i) {
Tomi Valkeinen065a40b2011-04-30 12:13:14 +03002200 if (strcmp(fbdev->displays[i].dssdev->name,
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002201 display_str) == 0) {
Tomi Valkeinen065a40b2011-04-30 12:13:14 +03002202 display = fbdev->displays[i].dssdev;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002203 break;
2204 }
2205 }
2206
2207 if (!display) {
2208 r = -EINVAL;
2209 break;
2210 }
2211
Tomi Valkeinena2699502010-01-11 14:33:40 +02002212 r = omapfb_set_def_mode(fbdev, display, mode_str);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002213 if (r)
2214 break;
2215 }
2216
2217 kfree(str);
2218
2219 return r;
2220}
2221
Tomi Valkeinendc891fa2011-08-25 17:15:14 +03002222static void fb_videomode_to_omap_timings(struct fb_videomode *m,
Archit Taneja783babf2012-06-15 07:39:47 +05302223 struct omap_dss_device *display,
Tomi Valkeinendc891fa2011-08-25 17:15:14 +03002224 struct omap_video_timings *t)
2225{
Archit Taneja783babf2012-06-15 07:39:47 +05302226 if (display->driver->get_timings) {
2227 display->driver->get_timings(display, t);
2228 } else {
2229 t->data_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE;
2230 t->de_level = OMAPDSS_SIG_ACTIVE_HIGH;
2231 t->sync_pclk_edge = OMAPDSS_DRIVE_SIG_OPPOSITE_EDGES;
2232 }
2233
Tomi Valkeinendc891fa2011-08-25 17:15:14 +03002234 t->x_res = m->xres;
2235 t->y_res = m->yres;
2236 t->pixel_clock = PICOS2KHZ(m->pixclock);
2237 t->hsw = m->hsync_len;
2238 t->hfp = m->right_margin;
2239 t->hbp = m->left_margin;
2240 t->vsw = m->vsync_len;
2241 t->vfp = m->lower_margin;
2242 t->vbp = m->upper_margin;
Archit Taneja783babf2012-06-15 07:39:47 +05302243 t->hsync_level = m->sync & FB_SYNC_HOR_HIGH_ACT ?
2244 OMAPDSS_SIG_ACTIVE_HIGH :
2245 OMAPDSS_SIG_ACTIVE_LOW;
2246 t->vsync_level = m->sync & FB_SYNC_VERT_HIGH_ACT ?
2247 OMAPDSS_SIG_ACTIVE_HIGH :
2248 OMAPDSS_SIG_ACTIVE_LOW;
Archit Taneja23bae3a2012-06-28 18:23:18 +05302249 t->interlace = m->vmode & FB_VMODE_INTERLACED;
Tomi Valkeinendc891fa2011-08-25 17:15:14 +03002250}
2251
2252static int omapfb_find_best_mode(struct omap_dss_device *display,
2253 struct omap_video_timings *timings)
2254{
2255 struct fb_monspecs *specs;
2256 u8 *edid;
Tomi Valkeinen5e18e352012-10-12 15:44:29 +03002257 int r, i, best_idx, len;
Tomi Valkeinendc891fa2011-08-25 17:15:14 +03002258
2259 if (!display->driver->read_edid)
2260 return -ENODEV;
2261
2262 len = 0x80 * 2;
2263 edid = kmalloc(len, GFP_KERNEL);
Tomi Valkeinenfa0c5e72012-12-13 12:13:51 +02002264 if (edid == NULL)
2265 return -ENOMEM;
Tomi Valkeinendc891fa2011-08-25 17:15:14 +03002266
2267 r = display->driver->read_edid(display, edid, len);
2268 if (r < 0)
2269 goto err1;
2270
2271 specs = kzalloc(sizeof(*specs), GFP_KERNEL);
Tomi Valkeinenfa0c5e72012-12-13 12:13:51 +02002272 if (specs == NULL) {
2273 r = -ENOMEM;
2274 goto err1;
2275 }
Tomi Valkeinendc891fa2011-08-25 17:15:14 +03002276
2277 fb_edid_to_monspecs(edid, specs);
2278
Tomi Valkeinendc891fa2011-08-25 17:15:14 +03002279 best_idx = -1;
2280
2281 for (i = 0; i < specs->modedb_len; ++i) {
2282 struct fb_videomode *m;
2283 struct omap_video_timings t;
2284
2285 m = &specs->modedb[i];
2286
2287 if (m->pixclock == 0)
2288 continue;
2289
2290 /* skip repeated pixel modes */
2291 if (m->xres == 2880 || m->xres == 1440)
2292 continue;
2293
Tomi Valkeinen5e18e352012-10-12 15:44:29 +03002294 if (m->vmode & FB_VMODE_INTERLACED ||
2295 m->vmode & FB_VMODE_DOUBLE)
2296 continue;
2297
Archit Taneja783babf2012-06-15 07:39:47 +05302298 fb_videomode_to_omap_timings(m, display, &t);
Tomi Valkeinendc891fa2011-08-25 17:15:14 +03002299
2300 r = display->driver->check_timings(display, &t);
Tomi Valkeinen5e18e352012-10-12 15:44:29 +03002301 if (r == 0) {
Tomi Valkeinendc891fa2011-08-25 17:15:14 +03002302 best_idx = i;
Tomi Valkeinen5e18e352012-10-12 15:44:29 +03002303 break;
Tomi Valkeinendc891fa2011-08-25 17:15:14 +03002304 }
2305 }
2306
Tomi Valkeinen5e18e352012-10-12 15:44:29 +03002307 if (best_idx == -1) {
Tomi Valkeinendc891fa2011-08-25 17:15:14 +03002308 r = -ENOENT;
2309 goto err2;
2310 }
2311
Archit Taneja783babf2012-06-15 07:39:47 +05302312 fb_videomode_to_omap_timings(&specs->modedb[best_idx], display,
2313 timings);
Tomi Valkeinendc891fa2011-08-25 17:15:14 +03002314
2315 r = 0;
2316
2317err2:
2318 fb_destroy_modedb(specs->modedb);
2319 kfree(specs);
2320err1:
2321 kfree(edid);
2322
2323 return r;
2324}
2325
Tomi Valkeinen91ac27a2010-09-23 11:18:44 +03002326static int omapfb_init_display(struct omapfb2_device *fbdev,
2327 struct omap_dss_device *dssdev)
2328{
2329 struct omap_dss_driver *dssdrv = dssdev->driver;
Tomi Valkeinen27cc2132011-04-30 16:55:12 +03002330 struct omapfb_display_data *d;
Tomi Valkeinen91ac27a2010-09-23 11:18:44 +03002331 int r;
2332
2333 r = dssdrv->enable(dssdev);
2334 if (r) {
2335 dev_warn(fbdev->dev, "Failed to enable display '%s'\n",
2336 dssdev->name);
2337 return r;
2338 }
2339
Tomi Valkeinen27cc2132011-04-30 16:55:12 +03002340 d = get_display_data(fbdev, dssdev);
2341
2342 d->fbdev = fbdev;
2343
Tomi Valkeinen91ac27a2010-09-23 11:18:44 +03002344 if (dssdev->caps & OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE) {
2345 u16 w, h;
Tomi Valkeinen27cc2132011-04-30 16:55:12 +03002346
2347 if (auto_update) {
2348 omapfb_start_auto_update(fbdev, dssdev);
2349 d->update_mode = OMAPFB_AUTO_UPDATE;
2350 } else {
2351 d->update_mode = OMAPFB_MANUAL_UPDATE;
2352 }
2353
Tomi Valkeinen91ac27a2010-09-23 11:18:44 +03002354 if (dssdrv->enable_te) {
2355 r = dssdrv->enable_te(dssdev, 1);
2356 if (r) {
2357 dev_err(fbdev->dev, "Failed to set TE\n");
2358 return r;
2359 }
2360 }
2361
Tomi Valkeinen91ac27a2010-09-23 11:18:44 +03002362 dssdrv->get_resolution(dssdev, &w, &h);
2363 r = dssdrv->update(dssdev, 0, 0, w, h);
2364 if (r) {
2365 dev_err(fbdev->dev,
2366 "Failed to update display\n");
2367 return r;
2368 }
2369 } else {
Tomi Valkeinen27cc2132011-04-30 16:55:12 +03002370 d->update_mode = OMAPFB_AUTO_UPDATE;
Tomi Valkeinen91ac27a2010-09-23 11:18:44 +03002371 }
2372
2373 return 0;
2374}
2375
Tomi Valkeinen5d89bcc2012-08-06 15:27:17 +03002376static int omapfb_init_connections(struct omapfb2_device *fbdev,
Tomi Valkeinen6b6f1ed2012-12-04 13:12:39 +02002377 struct omap_dss_device *def_dssdev)
Tomi Valkeinen5d89bcc2012-08-06 15:27:17 +03002378{
2379 int i, r;
Tomi Valkeinen6b6f1ed2012-12-04 13:12:39 +02002380 struct omap_overlay_manager *mgr;
Tomi Valkeinen5d89bcc2012-08-06 15:27:17 +03002381
Tomi Valkeinen6b6f1ed2012-12-04 13:12:39 +02002382 if (!def_dssdev->output) {
2383 dev_err(fbdev->dev, "no output for the default display\n");
2384 return -EINVAL;
Tomi Valkeinen5d89bcc2012-08-06 15:27:17 +03002385 }
2386
Tomi Valkeinen6b6f1ed2012-12-04 13:12:39 +02002387 for (i = 0; i < fbdev->num_displays; ++i) {
2388 struct omap_dss_device *dssdev = fbdev->displays[i].dssdev;
2389 struct omap_dss_output *out = dssdev->output;
Tomi Valkeinen5d89bcc2012-08-06 15:27:17 +03002390
Tomi Valkeinen2eea5ae2013-02-13 11:23:54 +02002391 mgr = omap_dss_get_overlay_manager(out->dispc_channel);
Tomi Valkeinen5d89bcc2012-08-06 15:27:17 +03002392
Tomi Valkeinen6b6f1ed2012-12-04 13:12:39 +02002393 if (!mgr || !out)
2394 continue;
2395
2396 if (mgr->output)
2397 mgr->unset_output(mgr);
2398
2399 mgr->set_output(mgr, out);
2400 }
2401
2402 mgr = def_dssdev->output->manager;
2403
2404 if (!mgr) {
2405 dev_err(fbdev->dev, "no ovl manager for the default display\n");
2406 return -EINVAL;
2407 }
Tomi Valkeinen5d89bcc2012-08-06 15:27:17 +03002408
2409 for (i = 0; i < fbdev->num_overlays; i++) {
2410 struct omap_overlay *ovl = fbdev->overlays[i];
2411
2412 if (ovl->manager)
2413 ovl->unset_manager(ovl);
2414
2415 r = ovl->set_manager(ovl, mgr);
2416 if (r)
2417 dev_warn(fbdev->dev,
2418 "failed to connect overlay %s to manager %s\n",
2419 ovl->name, mgr->name);
2420 }
2421
2422 return 0;
2423}
2424
Tomi Valkeinend64f14e2012-02-17 17:43:03 +02002425static int __init omapfb_probe(struct platform_device *pdev)
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002426{
2427 struct omapfb2_device *fbdev = NULL;
2428 int r = 0;
2429 int i;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002430 struct omap_dss_device *def_display;
2431 struct omap_dss_device *dssdev;
2432
2433 DBG("omapfb_probe\n");
2434
2435 if (pdev->num_resources != 0) {
2436 dev_err(&pdev->dev, "probed for an unknown device\n");
2437 r = -ENODEV;
2438 goto err0;
2439 }
2440
Tomi Valkeinen057eeae2012-12-13 12:08:21 +02002441 fbdev = devm_kzalloc(&pdev->dev, sizeof(struct omapfb2_device),
2442 GFP_KERNEL);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002443 if (fbdev == NULL) {
2444 r = -ENOMEM;
2445 goto err0;
2446 }
2447
Tomi Valkeinena89d1a82012-10-17 12:16:50 +03002448 if (def_vrfb && !omap_vrfb_supported()) {
Senthilvadivu Guruswamy41814cf2010-10-08 08:44:33 +02002449 def_vrfb = 0;
2450 dev_warn(&pdev->dev, "VRFB is not supported on this hardware, "
2451 "ignoring the module parameter vrfb=y\n");
2452 }
2453
Tomi Valkeinena9ee9f02012-10-10 10:26:45 +03002454 r = omapdss_compat_init();
2455 if (r)
2456 goto err0;
Senthilvadivu Guruswamy41814cf2010-10-08 08:44:33 +02002457
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002458 mutex_init(&fbdev->mtx);
2459
2460 fbdev->dev = &pdev->dev;
2461 platform_set_drvdata(pdev, fbdev);
2462
2463 fbdev->num_displays = 0;
2464 dssdev = NULL;
2465 for_each_dss_dev(dssdev) {
Tomi Valkeinen27cc2132011-04-30 16:55:12 +03002466 struct omapfb_display_data *d;
2467
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002468 omap_dss_get_device(dssdev);
Tomi Valkeinenb3f91eb2010-02-17 12:00:01 +02002469
Tomi Valkeinen807a7512010-01-07 17:45:03 +02002470 if (!dssdev->driver) {
Tomi Valkeinenbab59b42011-08-04 14:37:29 +03002471 dev_warn(&pdev->dev, "no driver for display: %s\n",
Andy Doanc5f18d72011-07-06 12:08:29 -05002472 dssdev->name);
Tomi Valkeinenbab59b42011-08-04 14:37:29 +03002473 omap_dss_put_device(dssdev);
2474 continue;
Tomi Valkeinen807a7512010-01-07 17:45:03 +02002475 }
Tomi Valkeinenb3f91eb2010-02-17 12:00:01 +02002476
Tomi Valkeinen27cc2132011-04-30 16:55:12 +03002477 d = &fbdev->displays[fbdev->num_displays++];
2478 d->dssdev = dssdev;
2479 if (dssdev->caps & OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE)
2480 d->update_mode = OMAPFB_MANUAL_UPDATE;
2481 else
2482 d->update_mode = OMAPFB_AUTO_UPDATE;
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002483 }
2484
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002485 if (fbdev->num_displays == 0) {
2486 dev_err(&pdev->dev, "no displays\n");
2487 r = -EINVAL;
2488 goto cleanup;
2489 }
2490
2491 fbdev->num_overlays = omap_dss_get_num_overlays();
2492 for (i = 0; i < fbdev->num_overlays; i++)
2493 fbdev->overlays[i] = omap_dss_get_overlay(i);
2494
2495 fbdev->num_managers = omap_dss_get_num_overlay_managers();
2496 for (i = 0; i < fbdev->num_managers; i++)
2497 fbdev->managers[i] = omap_dss_get_overlay_manager(i);
2498
Tomi Valkeinen5d89bcc2012-08-06 15:27:17 +03002499 def_display = NULL;
2500
2501 for (i = 0; i < fbdev->num_displays; ++i) {
2502 struct omap_dss_device *dssdev;
2503 const char *def_name;
2504
2505 def_name = omapdss_get_default_display_name();
2506
2507 dssdev = fbdev->displays[i].dssdev;
2508
2509 if (def_name == NULL ||
2510 (dssdev->name && strcmp(def_name, dssdev->name) == 0)) {
2511 def_display = dssdev;
2512 break;
2513 }
2514 }
2515
2516 if (def_display == NULL) {
2517 dev_err(fbdev->dev, "failed to find default display\n");
2518 r = -EINVAL;
2519 goto cleanup;
2520 }
2521
2522 r = omapfb_init_connections(fbdev, def_display);
2523 if (r) {
2524 dev_err(fbdev->dev, "failed to init overlay connections\n");
2525 goto cleanup;
Tomi Valkeinendc891fa2011-08-25 17:15:14 +03002526 }
2527
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002528 if (def_mode && strlen(def_mode) > 0) {
2529 if (omapfb_parse_def_modes(fbdev))
2530 dev_warn(&pdev->dev, "cannot parse default modes\n");
Tomi Valkeinendc891fa2011-08-25 17:15:14 +03002531 } else if (def_display && def_display->driver->set_timings &&
2532 def_display->driver->check_timings) {
2533 struct omap_video_timings t;
2534
2535 r = omapfb_find_best_mode(def_display, &t);
2536
2537 if (r == 0)
2538 def_display->driver->set_timings(def_display, &t);
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002539 }
2540
2541 r = omapfb_create_framebuffers(fbdev);
2542 if (r)
2543 goto cleanup;
2544
2545 for (i = 0; i < fbdev->num_managers; i++) {
2546 struct omap_overlay_manager *mgr;
2547 mgr = fbdev->managers[i];
2548 r = mgr->apply(mgr);
2549 if (r)
2550 dev_warn(fbdev->dev, "failed to apply dispc config\n");
2551 }
2552
2553 DBG("mgr->apply'ed\n");
2554
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002555 if (def_display) {
Tomi Valkeinen91ac27a2010-09-23 11:18:44 +03002556 r = omapfb_init_display(fbdev, def_display);
Tomi Valkeinen6d2e0bd2010-02-17 13:38:08 +02002557 if (r) {
Tomi Valkeinen91ac27a2010-09-23 11:18:44 +03002558 dev_err(fbdev->dev,
2559 "failed to initialize default "
2560 "display\n");
Tomi Valkeinen6d2e0bd2010-02-17 13:38:08 +02002561 goto cleanup;
2562 }
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002563 }
2564
Afzal Mohammede26ed442010-07-01 15:40:01 +02002565 DBG("create sysfs for fbs\n");
2566 r = omapfb_create_sysfs(fbdev);
2567 if (r) {
2568 dev_err(fbdev->dev, "failed to create sysfs entries\n");
2569 goto cleanup;
2570 }
2571
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002572 return 0;
2573
2574cleanup:
2575 omapfb_free_resources(fbdev);
Tomi Valkeinena9ee9f02012-10-10 10:26:45 +03002576 omapdss_compat_uninit();
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002577err0:
2578 dev_err(&pdev->dev, "failed to setup omapfb\n");
2579 return r;
2580}
2581
Tomi Valkeinend64f14e2012-02-17 17:43:03 +02002582static int __exit omapfb_remove(struct platform_device *pdev)
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002583{
2584 struct omapfb2_device *fbdev = platform_get_drvdata(pdev);
2585
2586 /* FIXME: wait till completion of pending events */
2587
2588 omapfb_remove_sysfs(fbdev);
2589
2590 omapfb_free_resources(fbdev);
2591
Tomi Valkeinena9ee9f02012-10-10 10:26:45 +03002592 omapdss_compat_uninit();
2593
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002594 return 0;
2595}
2596
2597static struct platform_driver omapfb_driver = {
Tomi Valkeinend64f14e2012-02-17 17:43:03 +02002598 .remove = __exit_p(omapfb_remove),
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002599 .driver = {
2600 .name = "omapfb",
2601 .owner = THIS_MODULE,
2602 },
2603};
2604
2605static int __init omapfb_init(void)
2606{
2607 DBG("omapfb_init\n");
2608
Tomi Valkeinend64f14e2012-02-17 17:43:03 +02002609 if (platform_driver_probe(&omapfb_driver, omapfb_probe)) {
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +03002610 printk(KERN_ERR "failed to register omapfb driver\n");
2611 return -ENODEV;
2612 }
2613
2614 return 0;
2615}
2616
2617static void __exit omapfb_exit(void)
2618{
2619 DBG("omapfb_exit\n");
2620 platform_driver_unregister(&omapfb_driver);
2621}
2622
2623module_param_named(mode, def_mode, charp, 0);
2624module_param_named(vram, def_vram, charp, 0);
2625module_param_named(rotate, def_rotate, int, 0);
2626module_param_named(vrfb, def_vrfb, bool, 0);
2627module_param_named(mirror, def_mirror, bool, 0);
2628
2629/* late_initcall to let panel/ctrl drivers loaded first.
2630 * I guess better option would be a more dynamic approach,
2631 * so that omapfb reacts to new panels when they are loaded */
2632late_initcall(omapfb_init);
2633/*module_init(omapfb_init);*/
2634module_exit(omapfb_exit);
2635
2636MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@nokia.com>");
2637MODULE_DESCRIPTION("OMAP2/3 Framebuffer");
2638MODULE_LICENSE("GPL v2");