]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android-sdk/kernel-video.git/blob - drivers/video/fbdev/omap2/omapfb/omapfb-main.c
Merge branch 'p-android-3.14' into p-ti-linux-3.14.y-android
[android-sdk/kernel-video.git] / drivers / video / fbdev / omap2 / omapfb / omapfb-main.c
1 /*
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  */
23 #include <linux/module.h>
24 #include <linux/delay.h>
25 #include <linux/slab.h>
26 #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 #include <linux/suspend.h>
34 #include <video/omapdss.h>
35 #include <video/omapvrfb.h>
37 #include "omapfb.h"
39 #define MODULE_NAME     "omapfb"
41 #define OMAPFB_PLANE_XRES_MIN           8
42 #define OMAPFB_PLANE_YRES_MIN           8
44 static char *def_mode;
45 static char *def_vram;
46 static bool def_vrfb;
47 static int def_rotate;
48 static bool def_mirror;
49 static bool auto_update;
50 static unsigned int auto_update_freq;
51 module_param(auto_update, bool, 0);
52 module_param(auto_update_freq, uint, 0644);
54 #ifdef DEBUG
55 bool omapfb_debug;
56 module_param_named(debug, omapfb_debug, bool, 0644);
57 static bool omapfb_test_pattern;
58 module_param_named(test, omapfb_test_pattern, bool, 0644);
59 #endif
61 static int omapfb_fb_init(struct omapfb2_device *fbdev, struct fb_info *fbi);
62 static int omapfb_get_recommended_bpp(struct omapfb2_device *fbdev,
63                 struct omap_dss_device *dssdev);
65 #ifdef DEBUG
66 static 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;
74         int r = (color >> 16) & 0xff;
75         int g = (color >> 8) & 0xff;
76         int b = (color >> 0) & 0xff;
78         if (var->bits_per_pixel == 16) {
79                 u16 __iomem *p = (u16 __iomem *)addr;
80                 p += y * line_len + x;
82                 r = r * 32 / 256;
83                 g = g * 64 / 256;
84                 b = b * 32 / 256;
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;
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 }
101 static void fill_fb(struct fb_info *fbi)
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;
109         if (!addr)
110                 return;
112         DBG("fill_fb %dx%d, line_len %d bytes\n", w, h, fbi->fix.line_length);
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         }
160 #endif
162 static unsigned omapfb_get_vrfb_offset(const struct omapfb_info *ofbi, int rot)
164         const struct vrfb *vrfb = &ofbi->region->vrfb;
165         unsigned offset;
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();
182                 return 0;
183         }
185         offset *= vrfb->bytespp;
187         return offset;
190 static u32 omapfb_get_region_rot_paddr(const struct omapfb_info *ofbi, int rot)
192         if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
193                 return ofbi->region->vrfb.paddr[rot]
194                         + omapfb_get_vrfb_offset(ofbi, rot);
195         } else {
196                 return ofbi->region->paddr;
197         }
200 static u32 omapfb_get_region_paddr(const struct omapfb_info *ofbi)
202         if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
203                 return ofbi->region->vrfb.paddr[0];
204         else
205                 return ofbi->region->paddr;
208 static void __iomem *omapfb_get_region_vaddr(const struct omapfb_info *ofbi)
210         if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
211                 return ofbi->region->vrfb.vaddr[0];
212         else
213                 return ofbi->region->vaddr;
216 static 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 };
277 static bool cmp_var_to_colormode(struct fb_var_screeninfo *var,
278                 struct omapfb_colormode *color)
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         }
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;
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);
300 static void assign_colormode_to_var(struct fb_var_screeninfo *var,
301                 struct omapfb_colormode *color)
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;
311 static int fb_mode_to_dss_mode(struct fb_var_screeninfo *var,
312                 enum omap_color_mode *mode)
314         enum omap_color_mode dssmode;
315         int i;
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                 }
328                 return -EINVAL;
329         }
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         }
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         }
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         }
381         return -EINVAL;
384 static int check_fb_res_bounds(struct fb_var_screeninfo *var)
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;
391         /* XXX: some applications seem to set virtual res to 0. */
392         if (var->xres_virtual == 0)
393                 var->xres_virtual = var->xres;
395         if (var->yres_virtual == 0)
396                 var->yres_virtual = var->yres;
398         if (var->xres_virtual < xres_min || var->yres_virtual < yres_min)
399                 return -EINVAL;
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;
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;
415         return 0;
418 static void shrink_height(unsigned long max_frame_size,
419                 struct fb_var_screeninfo *var)
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);
425         if (var->yres_virtual < OMAPFB_PLANE_YRES_MIN)
426                 var->yres_virtual = OMAPFB_PLANE_YRES_MIN;
428         if (var->yres > var->yres_virtual)
429                 var->yres = var->yres_virtual;
432 static void shrink_width(unsigned long max_frame_size,
433                 struct fb_var_screeninfo *var)
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);
439         if (var->xres_virtual < OMAPFB_PLANE_XRES_MIN)
440                 var->xres_virtual = OMAPFB_PLANE_XRES_MIN;
442         if (var->xres > var->xres_virtual)
443                 var->xres = var->xres_virtual;
446 static int check_vrfb_fb_size(unsigned long region_size,
447                 const struct fb_var_screeninfo *var)
449         unsigned long min_phys_size = omap_vrfb_min_phys_size(var->xres_virtual,
450                 var->yres_virtual, var->bits_per_pixel >> 3);
452         return min_phys_size > region_size ? -EINVAL : 0;
455 static int check_fb_size(const struct omapfb_info *ofbi,
456                 struct fb_var_screeninfo *var)
458         unsigned long max_frame_size = ofbi->region->size;
459         int bytespp = var->bits_per_pixel >> 3;
460         unsigned long line_size = var->xres_virtual * bytespp;
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);
469                 if (check_vrfb_fb_size(max_frame_size, var)) {
470                         DBG("cannot fit FB to memory\n");
471                         return -EINVAL;
472                 }
474                 return 0;
475         }
477         DBG("max frame size %lu, line size %lu\n", max_frame_size, line_size);
479         if (line_size * var->yres_virtual > max_frame_size)
480                 shrink_height(max_frame_size, var);
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         }
487         if (line_size * var->yres_virtual > max_frame_size) {
488                 DBG("cannot fit FB to memory\n");
489                 return -EINVAL;
490         }
492         return 0;
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  */
502 static int setup_vrfb_rotation(struct fb_info *fbi)
504         struct omapfb_info *ofbi = FB2OFB(fbi);
505         struct omapfb2_mem_region *rg = ofbi->region;
506         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;
515         if (!rg->size || ofbi->rotation_type != OMAP_DSS_ROT_VRFB)
516                 return 0;
518         DBG("setup_vrfb_rotation\n");
520         r = fb_mode_to_dss_mode(var, &mode);
521         if (r)
522                 return r;
524         bytespp = var->bits_per_pixel >> 3;
526         yuv_mode = mode == OMAP_DSS_COLOR_YUV2 || mode == OMAP_DSS_COLOR_UYVY;
528         /* We need to reconfigure VRFB if the resolution changes, if yuv mode
529          * is enabled/disabled, or if bytes per pixel changes */
531         /* XXX we shouldn't allow this when framebuffer is mmapped */
533         reconf = false;
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;
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         }
552         if (vrfb->vaddr[0])
553                 return 0;
555         omap_vrfb_setup(&rg->vrfb, rg->paddr,
556                         var->xres_virtual,
557                         var->yres_virtual,
558                         bytespp, yuv_mode);
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;
565         /* used by open/write in fbmem.c */
566         fbi->screen_base = ofbi->region->vrfb.vaddr[0];
568         fix->smem_start = ofbi->region->vrfb.paddr[0];
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         }
582         fix->smem_len = var->yres_virtual * fix->line_length;
584         return 0;
587 int dss_mode_to_fb_mode(enum omap_color_mode dssmode,
588                         struct fb_var_screeninfo *var)
590         int i;
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;
602 void set_fb_fix(struct fb_info *fbi)
604         struct fb_fix_screeninfo *fix = &fbi->fix;
605         struct fb_var_screeninfo *var = &fbi->var;
606         struct omapfb_info *ofbi = FB2OFB(fbi);
607         struct omapfb2_mem_region *rg = ofbi->region;
609         DBG("set_fb_fix\n");
611         /* used by open/write in fbmem.c */
612         fbi->screen_base = (char __iomem *)omapfb_get_region_vaddr(ofbi);
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                 }
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         }
635         fix->smem_start = omapfb_get_region_paddr(ofbi);
637         fix->type = FB_TYPE_PACKED_PIXELS;
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         }
659         fix->accel = FB_ACCEL_NONE;
661         fix->xpanstep = 1;
662         fix->ypanstep = 1;
665 /* check new var and possibly modify it to be ok */
666 int check_fb_var(struct fb_info *fbi, struct fb_var_screeninfo *var)
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;
674         DBG("check_fb_var %d\n", ofbi->id);
676         WARN_ON(!atomic_read(&ofbi->region->lock_count));
678         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         }
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         }
691         if (var->rotate > 3)
692                 return -EINVAL;
694         if (check_fb_res_bounds(var))
695                 return -EINVAL;
697         /* When no memory is allocated ignore the size check */
698         if (ofbi->region->size != 0 && check_fb_size(ofbi, var))
699                 return -EINVAL;
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;
706         DBG("xres = %d, yres = %d, vxres = %d, vyres = %d\n",
707                         var->xres, var->yres,
708                         var->xres_virtual, var->yres_virtual);
710         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         }
720         var->grayscale          = 0;
722         if (display && display->driver->get_timings) {
723                 struct omap_video_timings timings;
724                 display->driver->get_timings(display, &timings);
726                 /* pixclock in ps, the rest in pixclock */
727                 var->pixclock = timings.pixelclock != 0 ?
728                         KHZ2PICOS(timings.pixelclock / 1000) :
729                         0;
730                 var->left_margin = timings.hbp;
731                 var->right_margin = timings.hfp;
732                 var->upper_margin = timings.vbp;
733                 var->lower_margin = timings.vfp;
734                 var->hsync_len = timings.hsw;
735                 var->vsync_len = timings.vsw;
736                 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;
740                 var->vmode = timings.interlace ?
741                                 FB_VMODE_INTERLACED : FB_VMODE_NONINTERLACED;
742         } 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;
750                 var->sync = 0;
751                 var->vmode = FB_VMODE_NONINTERLACED;
752         }
754         return 0;
757 /*
758  * ---------------------------------------------------------------------------
759  * fbdev framework callbacks
760  * ---------------------------------------------------------------------------
761  */
762 static int omapfb_open(struct fb_info *fbi, int user)
764         return 0;
767 static int omapfb_release(struct fb_info *fbi, int user)
769         return 0;
772 static unsigned calc_rotation_offset_dma(const struct fb_var_screeninfo *var,
773                 const struct fb_fix_screeninfo *fix, int rotation)
775         unsigned offset;
777         offset = var->yoffset * fix->line_length +
778                 var->xoffset * (var->bits_per_pixel >> 3);
780         return offset;
783 static unsigned calc_rotation_offset_vrfb(const struct fb_var_screeninfo *var,
784                 const struct fb_fix_screeninfo *fix, int rotation)
786         unsigned offset;
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;
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);
810         return offset;
813 static void omapfb_calc_addr(const struct omapfb_info *ofbi,
814                              const struct fb_var_screeninfo *var,
815                              const struct fb_fix_screeninfo *fix,
816                              int rotation, u32 *paddr)
818         u32 data_start_p;
819         int offset;
821         if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
822                 data_start_p = omapfb_get_region_rot_paddr(ofbi, rotation);
823         else
824                 data_start_p = omapfb_get_region_paddr(ofbi);
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);
831         data_start_p += offset;
833         if (offset)
834                 DBG("offset %d, %d = %d\n",
835                     var->xoffset, var->yoffset, offset);
837         DBG("paddr %x\n", data_start_p);
839         *paddr = data_start_p;
842 /* setup overlay according to the fb */
843 int omapfb_setup_overlay(struct fb_info *fbi, struct omap_overlay *ovl,
844                 u16 posx, u16 posy, u16 outw, u16 outh)
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;
851         u32 data_start_p = 0;
852         struct omap_overlay_info info;
853         int xres, yres;
854         int screen_width;
855         int mirror;
856         int rotation = var->rotate;
857         int i;
859         WARN_ON(!atomic_read(&ofbi->region->lock_count));
861         for (i = 0; i < ofbi->num_overlays; i++) {
862                 if (ovl != ofbi->overlays[i])
863                         continue;
865                 rotation = (rotation + ofbi->rotation[i]) % 4;
866                 break;
867         }
869         DBG("setup_overlay %d, posx %d, posy %d, outw %d, outh %d\n", ofbi->id,
870                         posx, posy, outw, outh);
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         }
880         if (ofbi->region->size)
881                 omapfb_calc_addr(ofbi, var, fix, rotation, &data_start_p);
883         r = fb_mode_to_dss_mode(var, &mode);
884         if (r) {
885                 DBG("fb_mode_to_dss_mode failed");
886                 goto err;
887         }
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         }
902         ovl->get_overlay_info(ovl, &info);
904         if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
905                 mirror = 0;
906         else
907                 mirror = ofbi->mirror;
909         info.paddr = data_start_p;
910         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;
918         info.pos_x = posx;
919         info.pos_y = posy;
920         info.out_width = outw;
921         info.out_height = outh;
923         r = ovl->set_overlay_info(ovl, &info);
924         if (r) {
925                 DBG("ovl->setup_overlay_info failed\n");
926                 goto err;
927         }
929         return 0;
931 err:
932         DBG("setup_overlay failed\n");
933         return r;
936 /* apply var to the overlay */
937 int omapfb_apply_changes(struct fb_info *fbi, int init)
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;
947 #ifdef DEBUG
948         if (omapfb_test_pattern)
949                 fill_fb(fbi);
950 #endif
952         WARN_ON(!atomic_read(&ofbi->region->lock_count));
954         for (i = 0; i < ofbi->num_overlays; i++) {
955                 ovl = ofbi->overlays[i];
957                 DBG("apply_changes, fb %d, ovl %d\n", ofbi->id, ovl->id);
959                 if (ofbi->region->size == 0) {
960                         /* 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                 }
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 {
978                         struct omap_overlay_info info;
979                         ovl->get_overlay_info(ovl, &info);
980                         outw = info.out_width;
981                         outh = info.out_height;
982                 }
984                 if (init) {
985                         posx = 0;
986                         posy = 0;
987                 } else {
988                         struct omap_overlay_info info;
989                         ovl->get_overlay_info(ovl, &info);
990                         posx = info.pos_x;
991                         posy = info.pos_y;
992                 }
994                 r = omapfb_setup_overlay(fbi, ovl, posx, posy, outw, outh);
995                 if (r)
996                         goto err;
998                 if (!init && ovl->manager)
999                         ovl->manager->apply(ovl->manager);
1000         }
1001         return 0;
1002 err:
1003         DBG("apply_changes failed\n");
1004         return r;
1007 /* checks var and eventually tweaks it to something supported,
1008  * DO NOT MODIFY PAR */
1009 static int omapfb_check_var(struct fb_var_screeninfo *var, struct fb_info *fbi)
1011         struct omapfb_info *ofbi = FB2OFB(fbi);
1012         int r;
1014         DBG("check_var(%d)\n", FB2OFB(fbi)->id);
1016         omapfb_get_mem_region(ofbi->region);
1018         r = check_fb_var(fbi, var);
1020         omapfb_put_mem_region(ofbi->region);
1022         return r;
1025 /* set the video mode according to info->var */
1026 static int omapfb_set_par(struct fb_info *fbi)
1028         struct omapfb_info *ofbi = FB2OFB(fbi);
1029         int r;
1031         DBG("set_par(%d)\n", FB2OFB(fbi)->id);
1033         omapfb_get_mem_region(ofbi->region);
1035         set_fb_fix(fbi);
1037         r = setup_vrfb_rotation(fbi);
1038         if (r)
1039                 goto out;
1041         r = omapfb_apply_changes(fbi, 0);
1043  out:
1044         omapfb_put_mem_region(ofbi->region);
1046         return r;
1049 static int omapfb_pan_display(struct fb_var_screeninfo *var,
1050                 struct fb_info *fbi)
1052         struct omapfb_info *ofbi = FB2OFB(fbi);
1053         struct fb_var_screeninfo new_var;
1054         int r;
1056         DBG("pan_display(%d)\n", FB2OFB(fbi)->id);
1058         if (var->xoffset == fbi->var.xoffset &&
1059             var->yoffset == fbi->var.yoffset)
1060                 return 0;
1062         new_var = fbi->var;
1063         new_var.xoffset = var->xoffset;
1064         new_var.yoffset = var->yoffset;
1066         fbi->var = new_var;
1068         omapfb_get_mem_region(ofbi->region);
1070         r = omapfb_apply_changes(fbi, 0);
1072         omapfb_put_mem_region(ofbi->region);
1074         return r;
1077 static void mmap_user_open(struct vm_area_struct *vma)
1079         struct omapfb2_mem_region *rg = vma->vm_private_data;
1081         omapfb_get_mem_region(rg);
1082         atomic_inc(&rg->map_count);
1083         omapfb_put_mem_region(rg);
1086 static void mmap_user_close(struct vm_area_struct *vma)
1088         struct omapfb2_mem_region *rg = vma->vm_private_data;
1090         omapfb_get_mem_region(rg);
1091         atomic_dec(&rg->map_count);
1092         omapfb_put_mem_region(rg);
1095 static struct vm_operations_struct mmap_user_ops = {
1096         .open = mmap_user_open,
1097         .close = mmap_user_close,
1098 };
1100 static int omapfb_mmap(struct fb_info *fbi, struct vm_area_struct *vma)
1102         struct omapfb_info *ofbi = FB2OFB(fbi);
1103         struct fb_fix_screeninfo *fix = &fbi->fix;
1104         struct omapfb2_mem_region *rg;
1105         unsigned long start;
1106         u32 len;
1107         int r;
1109         rg = omapfb_get_mem_region(ofbi->region);
1111         start = omapfb_get_region_paddr(ofbi);
1112         len = fix->smem_len;
1114         DBG("user mmap region start %lx, len %d, off %lx\n", start, len,
1115                         vma->vm_pgoff << PAGE_SHIFT);
1117         vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
1118         vma->vm_ops = &mmap_user_ops;
1119         vma->vm_private_data = rg;
1121         r = vm_iomap_memory(vma, start, len);
1122         if (r)
1123                 goto error;
1125         /* vm_ops.open won't be called for mmap itself. */
1126         atomic_inc(&rg->map_count);
1128         omapfb_put_mem_region(rg);
1130         return 0;
1132 error:
1133         omapfb_put_mem_region(ofbi->region);
1135         return r;
1138 /* Store a single color palette entry into a pseudo palette or the hardware
1139  * palette if one is available. For now we support only 16bpp and thus store
1140  * the entry only to the pseudo palette.
1141  */
1142 static int _setcolreg(struct fb_info *fbi, u_int regno, u_int red, u_int green,
1143                 u_int blue, u_int transp, int update_hw_pal)
1145         /*struct omapfb_info *ofbi = FB2OFB(fbi);*/
1146         /*struct omapfb2_device *fbdev = ofbi->fbdev;*/
1147         struct fb_var_screeninfo *var = &fbi->var;
1148         int r = 0;
1150         enum omapfb_color_format mode = OMAPFB_COLOR_RGB24U; /* XXX */
1152         /*switch (plane->color_mode) {*/
1153         switch (mode) {
1154         case OMAPFB_COLOR_YUV422:
1155         case OMAPFB_COLOR_YUV420:
1156         case OMAPFB_COLOR_YUY422:
1157                 r = -EINVAL;
1158                 break;
1159         case OMAPFB_COLOR_CLUT_8BPP:
1160         case OMAPFB_COLOR_CLUT_4BPP:
1161         case OMAPFB_COLOR_CLUT_2BPP:
1162         case OMAPFB_COLOR_CLUT_1BPP:
1163                 /*
1164                    if (fbdev->ctrl->setcolreg)
1165                    r = fbdev->ctrl->setcolreg(regno, red, green, blue,
1166                    transp, update_hw_pal);
1167                    */
1168                 /* Fallthrough */
1169                 r = -EINVAL;
1170                 break;
1171         case OMAPFB_COLOR_RGB565:
1172         case OMAPFB_COLOR_RGB444:
1173         case OMAPFB_COLOR_RGB24P:
1174         case OMAPFB_COLOR_RGB24U:
1175                 if (r != 0)
1176                         break;
1178                 if (regno < 16) {
1179                         u32 pal;
1180                         pal = ((red >> (16 - var->red.length)) <<
1181                                         var->red.offset) |
1182                                 ((green >> (16 - var->green.length)) <<
1183                                  var->green.offset) |
1184                                 (blue >> (16 - var->blue.length));
1185                         ((u32 *)(fbi->pseudo_palette))[regno] = pal;
1186                 }
1187                 break;
1188         default:
1189                 BUG();
1190         }
1191         return r;
1194 static int omapfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
1195                 u_int transp, struct fb_info *info)
1197         DBG("setcolreg\n");
1199         return _setcolreg(info, regno, red, green, blue, transp, 1);
1202 static int omapfb_setcmap(struct fb_cmap *cmap, struct fb_info *info)
1204         int count, index, r;
1205         u16 *red, *green, *blue, *transp;
1206         u16 trans = 0xffff;
1208         DBG("setcmap\n");
1210         red     = cmap->red;
1211         green   = cmap->green;
1212         blue    = cmap->blue;
1213         transp  = cmap->transp;
1214         index   = cmap->start;
1216         for (count = 0; count < cmap->len; count++) {
1217                 if (transp)
1218                         trans = *transp++;
1219                 r = _setcolreg(info, index++, *red++, *green++, *blue++, trans,
1220                                 count == cmap->len - 1);
1221                 if (r != 0)
1222                         return r;
1223         }
1225         return 0;
1228 static int omapfb_blank(int blank, struct fb_info *fbi)
1230         struct omapfb_info *ofbi = FB2OFB(fbi);
1231         struct omapfb2_device *fbdev = ofbi->fbdev;
1232         struct omap_dss_device *display = fb2display(fbi);
1233         struct omapfb_display_data *d;
1234         int r = 0;
1236         if (!display)
1237                 return -EINVAL;
1239         omapfb_lock(fbdev);
1241         d = get_display_data(fbdev, display);
1243         switch (blank) {
1244         case FB_BLANK_UNBLANK:
1245                 if (display->state == OMAP_DSS_DISPLAY_ACTIVE)
1246                         goto exit;
1248                 r = display->driver->enable(display);
1250                 if ((display->caps & OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE) &&
1251                                 d->update_mode == OMAPFB_AUTO_UPDATE &&
1252                                 !d->auto_update_work_enabled)
1253                         omapfb_start_auto_update(fbdev, display);
1255                 break;
1257         case FB_BLANK_NORMAL:
1258                 /* FB_BLANK_NORMAL could be implemented.
1259                  * Needs DSS additions. */
1260         case FB_BLANK_VSYNC_SUSPEND:
1261         case FB_BLANK_HSYNC_SUSPEND:
1262         case FB_BLANK_POWERDOWN:
1263                 if (display->state != OMAP_DSS_DISPLAY_ACTIVE)
1264                         goto exit;
1266                 if (d->auto_update_work_enabled)
1267                         omapfb_stop_auto_update(fbdev, display);
1269                 display->driver->disable(display);
1271                 break;
1273         default:
1274                 r = -EINVAL;
1275         }
1277 exit:
1278         omapfb_unlock(fbdev);
1280         return r;
1283 #if 0
1284 /* XXX fb_read and fb_write are needed for VRFB */
1285 ssize_t omapfb_write(struct fb_info *info, const char __user *buf,
1286                 size_t count, loff_t *ppos)
1288         DBG("omapfb_write %d, %lu\n", count, (unsigned long)*ppos);
1289         /* XXX needed for VRFB */
1290         return count;
1292 #endif
1294 static struct fb_ops omapfb_ops = {
1295         .owner          = THIS_MODULE,
1296         .fb_open        = omapfb_open,
1297         .fb_release     = omapfb_release,
1298         .fb_fillrect    = cfb_fillrect,
1299         .fb_copyarea    = cfb_copyarea,
1300         .fb_imageblit   = cfb_imageblit,
1301         .fb_blank       = omapfb_blank,
1302         .fb_ioctl       = omapfb_ioctl,
1303         .fb_check_var   = omapfb_check_var,
1304         .fb_set_par     = omapfb_set_par,
1305         .fb_pan_display = omapfb_pan_display,
1306         .fb_mmap        = omapfb_mmap,
1307         .fb_setcolreg   = omapfb_setcolreg,
1308         .fb_setcmap     = omapfb_setcmap,
1309         /*.fb_write     = omapfb_write,*/
1310 };
1312 static void omapfb_free_fbmem(struct fb_info *fbi)
1314         struct omapfb_info *ofbi = FB2OFB(fbi);
1315         struct omapfb2_device *fbdev = ofbi->fbdev;
1316         struct omapfb2_mem_region *rg;
1318         rg = ofbi->region;
1320         if (rg->token == NULL)
1321                 return;
1323         WARN_ON(atomic_read(&rg->map_count));
1325         if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
1326                 /* unmap the 0 angle rotation */
1327                 if (rg->vrfb.vaddr[0]) {
1328                         iounmap(rg->vrfb.vaddr[0]);
1329                         rg->vrfb.vaddr[0] = NULL;
1330                 }
1332                 omap_vrfb_release_ctx(&rg->vrfb);
1333         }
1335         dma_free_attrs(fbdev->dev, rg->size, rg->token, rg->dma_handle,
1336                         &rg->attrs);
1338         rg->token = NULL;
1339         rg->vaddr = NULL;
1340         rg->paddr = 0;
1341         rg->alloc = 0;
1342         rg->size = 0;
1345 static void clear_fb_info(struct fb_info *fbi)
1347         memset(&fbi->var, 0, sizeof(fbi->var));
1348         memset(&fbi->fix, 0, sizeof(fbi->fix));
1349         strlcpy(fbi->fix.id, MODULE_NAME, sizeof(fbi->fix.id));
1352 static int omapfb_free_all_fbmem(struct omapfb2_device *fbdev)
1354         int i;
1356         DBG("free all fbmem\n");
1358         for (i = 0; i < fbdev->num_fbs; i++) {
1359                 struct fb_info *fbi = fbdev->fbs[i];
1360                 omapfb_free_fbmem(fbi);
1361                 clear_fb_info(fbi);
1362         }
1364         return 0;
1367 static int omapfb_alloc_fbmem(struct fb_info *fbi, unsigned long size,
1368                 unsigned long paddr)
1370         struct omapfb_info *ofbi = FB2OFB(fbi);
1371         struct omapfb2_device *fbdev = ofbi->fbdev;
1372         struct omapfb2_mem_region *rg;
1373         void *token;
1374         DEFINE_DMA_ATTRS(attrs);
1375         dma_addr_t dma_handle;
1376         int r;
1378         rg = ofbi->region;
1380         rg->paddr = 0;
1381         rg->vaddr = NULL;
1382         memset(&rg->vrfb, 0, sizeof rg->vrfb);
1383         rg->size = 0;
1384         rg->type = 0;
1385         rg->alloc = false;
1386         rg->map = false;
1388         size = PAGE_ALIGN(size);
1390         dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs);
1392         if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
1393                 dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &attrs);
1395         DBG("allocating %lu bytes for fb %d\n", size, ofbi->id);
1397         token = dma_alloc_attrs(fbdev->dev, size, &dma_handle,
1398                         GFP_KERNEL, &attrs);
1400         if (token == NULL) {
1401                 dev_err(fbdev->dev, "failed to allocate framebuffer\n");
1402                 return -ENOMEM;
1403         }
1405         DBG("allocated VRAM paddr %lx, vaddr %p\n",
1406                         (unsigned long)dma_handle, token);
1408         if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
1409                 r = omap_vrfb_request_ctx(&rg->vrfb);
1410                 if (r) {
1411                         dma_free_attrs(fbdev->dev, size, token, dma_handle,
1412                                         &attrs);
1413                         dev_err(fbdev->dev, "vrfb create ctx failed\n");
1414                         return r;
1415                 }
1416         }
1418         rg->attrs = attrs;
1419         rg->token = token;
1420         rg->dma_handle = dma_handle;
1422         rg->paddr = (unsigned long)dma_handle;
1423         rg->vaddr = (void __iomem *)token;
1424         rg->size = size;
1425         rg->alloc = 1;
1427         return 0;
1430 /* allocate fbmem using display resolution as reference */
1431 static int omapfb_alloc_fbmem_display(struct fb_info *fbi, unsigned long size,
1432                 unsigned long paddr)
1434         struct omapfb_info *ofbi = FB2OFB(fbi);
1435         struct omapfb2_device *fbdev = ofbi->fbdev;
1436         struct omap_dss_device *display;
1437         int bytespp;
1439         display =  fb2display(fbi);
1441         if (!display)
1442                 return 0;
1444         switch (omapfb_get_recommended_bpp(fbdev, display)) {
1445         case 16:
1446                 bytespp = 2;
1447                 break;
1448         case 24:
1449                 bytespp = 4;
1450                 break;
1451         default:
1452                 bytespp = 4;
1453                 break;
1454         }
1456         if (!size) {
1457                 u16 w, h;
1459                 display->driver->get_resolution(display, &w, &h);
1461                 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
1462                         size = max(omap_vrfb_min_phys_size(w, h, bytespp),
1463                                         omap_vrfb_min_phys_size(h, w, bytespp));
1465                         DBG("adjusting fb mem size for VRFB, %u -> %lu\n",
1466                                         w * h * bytespp, size);
1467                 } else {
1468                         size = w * h * bytespp;
1469                 }
1470         }
1472         if (!size)
1473                 return 0;
1475         return omapfb_alloc_fbmem(fbi, size, paddr);
1478 static int omapfb_parse_vram_param(const char *param, int max_entries,
1479                 unsigned long *sizes, unsigned long *paddrs)
1481         int fbnum;
1482         unsigned long size;
1483         unsigned long paddr = 0;
1484         char *p, *start;
1486         start = (char *)param;
1488         while (1) {
1489                 p = start;
1491                 fbnum = simple_strtoul(p, &p, 10);
1493                 if (p == start)
1494                         return -EINVAL;
1496                 if (*p != ':')
1497                         return -EINVAL;
1499                 if (fbnum >= max_entries)
1500                         return -EINVAL;
1502                 size = memparse(p + 1, &p);
1504                 if (!size)
1505                         return -EINVAL;
1507                 paddr = 0;
1509                 if (*p == '@') {
1510                         paddr = simple_strtoul(p + 1, &p, 16);
1512                         if (!paddr)
1513                                 return -EINVAL;
1515                 }
1517                 WARN_ONCE(paddr,
1518                         "reserving memory at predefined address not supported\n");
1520                 paddrs[fbnum] = paddr;
1521                 sizes[fbnum] = size;
1523                 if (*p == 0)
1524                         break;
1526                 if (*p != ',')
1527                         return -EINVAL;
1529                 ++p;
1531                 start = p;
1532         }
1534         return 0;
1537 static int omapfb_allocate_all_fbs(struct omapfb2_device *fbdev)
1539         int i, r;
1540         unsigned long vram_sizes[10];
1541         unsigned long vram_paddrs[10];
1543         memset(&vram_sizes, 0, sizeof(vram_sizes));
1544         memset(&vram_paddrs, 0, sizeof(vram_paddrs));
1546         if (def_vram && omapfb_parse_vram_param(def_vram, 10,
1547                                 vram_sizes, vram_paddrs)) {
1548                 dev_err(fbdev->dev, "failed to parse vram parameter\n");
1550                 memset(&vram_sizes, 0, sizeof(vram_sizes));
1551                 memset(&vram_paddrs, 0, sizeof(vram_paddrs));
1552         }
1554         for (i = 0; i < fbdev->num_fbs; i++) {
1555                 /* allocate memory automatically only for fb0, or if
1556                  * excplicitly defined with vram or plat data option */
1557                 if (i == 0 || vram_sizes[i] != 0) {
1558                         r = omapfb_alloc_fbmem_display(fbdev->fbs[i],
1559                                         vram_sizes[i], vram_paddrs[i]);
1561                         if (r)
1562                                 return r;
1563                 }
1564         }
1566         for (i = 0; i < fbdev->num_fbs; i++) {
1567                 struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[i]);
1568                 struct omapfb2_mem_region *rg;
1569                 rg = ofbi->region;
1571                 DBG("region%d phys %08x virt %p size=%lu\n",
1572                                 i,
1573                                 rg->paddr,
1574                                 rg->vaddr,
1575                                 rg->size);
1576         }
1578         return 0;
1581 static void omapfb_clear_fb(struct fb_info *fbi)
1583         const struct fb_fillrect rect = {
1584                 .dx = 0,
1585                 .dy = 0,
1586                 .width = fbi->var.xres_virtual,
1587                 .height = fbi->var.yres_virtual,
1588                 .color = 0,
1589                 .rop = ROP_COPY,
1590         };
1592         cfb_fillrect(fbi, &rect);
1595 int omapfb_realloc_fbmem(struct fb_info *fbi, unsigned long size, int type)
1597         struct omapfb_info *ofbi = FB2OFB(fbi);
1598         struct omapfb2_device *fbdev = ofbi->fbdev;
1599         struct omapfb2_mem_region *rg = ofbi->region;
1600         unsigned long old_size = rg->size;
1601         unsigned long old_paddr = rg->paddr;
1602         int old_type = rg->type;
1603         int r;
1605         if (type != OMAPFB_MEMTYPE_SDRAM)
1606                 return -EINVAL;
1608         size = PAGE_ALIGN(size);
1610         if (old_size == size && old_type == type)
1611                 return 0;
1613         omapfb_free_fbmem(fbi);
1615         if (size == 0) {
1616                 clear_fb_info(fbi);
1617                 return 0;
1618         }
1620         r = omapfb_alloc_fbmem(fbi, size, 0);
1622         if (r) {
1623                 if (old_size)
1624                         omapfb_alloc_fbmem(fbi, old_size, old_paddr);
1626                 if (rg->size == 0)
1627                         clear_fb_info(fbi);
1629                 return r;
1630         }
1632         if (old_size == size)
1633                 return 0;
1635         if (old_size == 0) {
1636                 DBG("initializing fb %d\n", ofbi->id);
1637                 r = omapfb_fb_init(fbdev, fbi);
1638                 if (r) {
1639                         DBG("omapfb_fb_init failed\n");
1640                         goto err;
1641                 }
1642                 r = omapfb_apply_changes(fbi, 1);
1643                 if (r) {
1644                         DBG("omapfb_apply_changes failed\n");
1645                         goto err;
1646                 }
1647         } else {
1648                 struct fb_var_screeninfo new_var;
1649                 memcpy(&new_var, &fbi->var, sizeof(new_var));
1650                 r = check_fb_var(fbi, &new_var);
1651                 if (r)
1652                         goto err;
1653                 memcpy(&fbi->var, &new_var, sizeof(fbi->var));
1654                 set_fb_fix(fbi);
1655                 r = setup_vrfb_rotation(fbi);
1656                 if (r)
1657                         goto err;
1658         }
1660         omapfb_clear_fb(fbi);
1662         return 0;
1663 err:
1664         omapfb_free_fbmem(fbi);
1665         clear_fb_info(fbi);
1666         return r;
1669 static void omapfb_auto_update_work(struct work_struct *work)
1671         struct omap_dss_device *dssdev;
1672         struct omap_dss_driver *dssdrv;
1673         struct omapfb_display_data *d;
1674         u16 w, h;
1675         unsigned int freq;
1676         struct omapfb2_device *fbdev;
1678         d = container_of(work, struct omapfb_display_data,
1679                         auto_update_work.work);
1681         dssdev = d->dssdev;
1682         dssdrv = dssdev->driver;
1683         fbdev = d->fbdev;
1685         if (!dssdrv || !dssdrv->update)
1686                 return;
1688         if (dssdrv->sync)
1689                 dssdrv->sync(dssdev);
1691         dssdrv->get_resolution(dssdev, &w, &h);
1692         dssdrv->update(dssdev, 0, 0, w, h);
1694         freq = auto_update_freq;
1695         if (freq == 0)
1696                 freq = 20;
1697         queue_delayed_work(fbdev->auto_update_wq,
1698                         &d->auto_update_work, HZ / freq);
1701 void omapfb_start_auto_update(struct omapfb2_device *fbdev,
1702                 struct omap_dss_device *display)
1704         struct omapfb_display_data *d;
1706         if (fbdev->auto_update_wq == NULL) {
1707                 struct workqueue_struct *wq;
1709                 wq = create_singlethread_workqueue("omapfb_auto_update");
1711                 if (wq == NULL) {
1712                         dev_err(fbdev->dev, "Failed to create workqueue for "
1713                                         "auto-update\n");
1714                         return;
1715                 }
1717                 fbdev->auto_update_wq = wq;
1718         }
1720         d = get_display_data(fbdev, display);
1722         INIT_DELAYED_WORK(&d->auto_update_work, omapfb_auto_update_work);
1724         d->auto_update_work_enabled = true;
1726         omapfb_auto_update_work(&d->auto_update_work.work);
1729 void omapfb_stop_auto_update(struct omapfb2_device *fbdev,
1730                 struct omap_dss_device *display)
1732         struct omapfb_display_data *d;
1734         d = get_display_data(fbdev, display);
1736         cancel_delayed_work_sync(&d->auto_update_work);
1738         d->auto_update_work_enabled = false;
1741 /* initialize fb_info, var, fix to something sane based on the display */
1742 static int omapfb_fb_init(struct omapfb2_device *fbdev, struct fb_info *fbi)
1744         struct fb_var_screeninfo *var = &fbi->var;
1745         struct omap_dss_device *display = fb2display(fbi);
1746         struct omapfb_info *ofbi = FB2OFB(fbi);
1747         int r = 0;
1749         fbi->fbops = &omapfb_ops;
1750         fbi->flags = FBINFO_FLAG_DEFAULT;
1751         fbi->pseudo_palette = fbdev->pseudo_palette;
1753         if (ofbi->region->size == 0) {
1754                 clear_fb_info(fbi);
1755                 return 0;
1756         }
1758         var->nonstd = 0;
1759         var->bits_per_pixel = 0;
1761         var->rotate = def_rotate;
1763         if (display) {
1764                 u16 w, h;
1765                 int rotation = (var->rotate + ofbi->rotation[0]) % 4;
1767                 display->driver->get_resolution(display, &w, &h);
1769                 if (rotation == FB_ROTATE_CW ||
1770                                 rotation == FB_ROTATE_CCW) {
1771                         var->xres = h;
1772                         var->yres = w;
1773                 } else {
1774                         var->xres = w;
1775                         var->yres = h;
1776                 }
1778                 var->xres_virtual = var->xres;
1779                 var->yres_virtual = var->yres;
1781                 if (!var->bits_per_pixel) {
1782                         switch (omapfb_get_recommended_bpp(fbdev, display)) {
1783                         case 16:
1784                                 var->bits_per_pixel = 16;
1785                                 break;
1786                         case 24:
1787                                 var->bits_per_pixel = 32;
1788                                 break;
1789                         default:
1790                                 dev_err(fbdev->dev, "illegal display "
1791                                                 "bpp\n");
1792                                 return -EINVAL;
1793                         }
1794                 }
1795         } else {
1796                 /* if there's no display, let's just guess some basic values */
1797                 var->xres = 320;
1798                 var->yres = 240;
1799                 var->xres_virtual = var->xres;
1800                 var->yres_virtual = var->yres;
1801                 if (!var->bits_per_pixel)
1802                         var->bits_per_pixel = 16;
1803         }
1805         r = check_fb_var(fbi, var);
1806         if (r)
1807                 goto err;
1809         set_fb_fix(fbi);
1810         r = setup_vrfb_rotation(fbi);
1811         if (r)
1812                 goto err;
1814         r = fb_alloc_cmap(&fbi->cmap, 256, 0);
1815         if (r)
1816                 dev_err(fbdev->dev, "unable to allocate color map memory\n");
1818 err:
1819         return r;
1822 static void fbinfo_cleanup(struct omapfb2_device *fbdev, struct fb_info *fbi)
1824         fb_dealloc_cmap(&fbi->cmap);
1828 static void omapfb_free_resources(struct omapfb2_device *fbdev)
1830         int i;
1832         DBG("free_resources\n");
1834         if (fbdev == NULL)
1835                 return;
1837         for (i = 0; i < fbdev->num_fbs; i++) {
1838                 struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[i]);
1839                 int j;
1841                 for (j = 0; j < ofbi->num_overlays; j++) {
1842                         struct omap_overlay *ovl = ofbi->overlays[j];
1843                         ovl->disable(ovl);
1844                 }
1845         }
1847         for (i = 0; i < fbdev->num_fbs; i++)
1848                 unregister_framebuffer(fbdev->fbs[i]);
1850         /* free the reserved fbmem */
1851         omapfb_free_all_fbmem(fbdev);
1853         for (i = 0; i < fbdev->num_fbs; i++) {
1854                 fbinfo_cleanup(fbdev, fbdev->fbs[i]);
1855                 framebuffer_release(fbdev->fbs[i]);
1856         }
1858         for (i = 0; i < fbdev->num_displays; i++) {
1859                 struct omap_dss_device *dssdev = fbdev->displays[i].dssdev;
1861                 if (fbdev->displays[i].auto_update_work_enabled)
1862                         omapfb_stop_auto_update(fbdev, dssdev);
1864                 if (dssdev->state != OMAP_DSS_DISPLAY_DISABLED)
1865                         dssdev->driver->disable(dssdev);
1867                 dssdev->driver->disconnect(dssdev);
1869                 omap_dss_put_device(dssdev);
1870         }
1872         if (fbdev->auto_update_wq != NULL) {
1873                 flush_workqueue(fbdev->auto_update_wq);
1874                 destroy_workqueue(fbdev->auto_update_wq);
1875                 fbdev->auto_update_wq = NULL;
1876         }
1878         dev_set_drvdata(fbdev->dev, NULL);
1881 static int omapfb_create_framebuffers(struct omapfb2_device *fbdev)
1883         int r, i;
1885         fbdev->num_fbs = 0;
1887         DBG("create %d framebuffers\n", CONFIG_FB_OMAP2_NUM_FBS);
1889         /* allocate fb_infos */
1890         for (i = 0; i < CONFIG_FB_OMAP2_NUM_FBS; i++) {
1891                 struct fb_info *fbi;
1892                 struct omapfb_info *ofbi;
1894                 fbi = framebuffer_alloc(sizeof(struct omapfb_info),
1895                                 fbdev->dev);
1897                 if (fbi == NULL) {
1898                         dev_err(fbdev->dev,
1899                                 "unable to allocate memory for plane info\n");
1900                         return -ENOMEM;
1901                 }
1903                 clear_fb_info(fbi);
1905                 fbdev->fbs[i] = fbi;
1907                 ofbi = FB2OFB(fbi);
1908                 ofbi->fbdev = fbdev;
1909                 ofbi->id = i;
1911                 ofbi->region = &fbdev->regions[i];
1912                 ofbi->region->id = i;
1913                 init_rwsem(&ofbi->region->lock);
1915                 /* assign these early, so that fb alloc can use them */
1916                 ofbi->rotation_type = def_vrfb ? OMAP_DSS_ROT_VRFB :
1917                         OMAP_DSS_ROT_DMA;
1918                 ofbi->mirror = def_mirror;
1920                 fbdev->num_fbs++;
1921         }
1923         DBG("fb_infos allocated\n");
1925         /* assign overlays for the fbs */
1926         for (i = 0; i < min(fbdev->num_fbs, fbdev->num_overlays); i++) {
1927                 struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[i]);
1929                 ofbi->overlays[0] = fbdev->overlays[i];
1930                 ofbi->num_overlays = 1;
1931         }
1933         /* allocate fb memories */
1934         r = omapfb_allocate_all_fbs(fbdev);
1935         if (r) {
1936                 dev_err(fbdev->dev, "failed to allocate fbmem\n");
1937                 return r;
1938         }
1940         DBG("fbmems allocated\n");
1942         /* setup fb_infos */
1943         for (i = 0; i < fbdev->num_fbs; i++) {
1944                 struct fb_info *fbi = fbdev->fbs[i];
1945                 struct omapfb_info *ofbi = FB2OFB(fbi);
1947                 omapfb_get_mem_region(ofbi->region);
1948                 r = omapfb_fb_init(fbdev, fbi);
1949                 omapfb_put_mem_region(ofbi->region);
1951                 if (r) {
1952                         dev_err(fbdev->dev, "failed to setup fb_info\n");
1953                         return r;
1954                 }
1955         }
1957         for (i = 0; i < fbdev->num_fbs; i++) {
1958                 struct fb_info *fbi = fbdev->fbs[i];
1959                 struct omapfb_info *ofbi = FB2OFB(fbi);
1961                 if (ofbi->region->size == 0)
1962                         continue;
1964                 omapfb_clear_fb(fbi);
1965         }
1967         DBG("fb_infos initialized\n");
1969         for (i = 0; i < fbdev->num_fbs; i++) {
1970                 r = register_framebuffer(fbdev->fbs[i]);
1971                 if (r != 0) {
1972                         dev_err(fbdev->dev,
1973                                 "registering framebuffer %d failed\n", i);
1974                         return r;
1975                 }
1976         }
1978         DBG("framebuffers registered\n");
1980         for (i = 0; i < fbdev->num_fbs; i++) {
1981                 struct fb_info *fbi = fbdev->fbs[i];
1982                 struct omapfb_info *ofbi = FB2OFB(fbi);
1984                 omapfb_get_mem_region(ofbi->region);
1985                 r = omapfb_apply_changes(fbi, 1);
1986                 omapfb_put_mem_region(ofbi->region);
1988                 if (r) {
1989                         dev_err(fbdev->dev, "failed to change mode\n");
1990                         return r;
1991                 }
1992         }
1994         /* Enable fb0 */
1995         if (fbdev->num_fbs > 0) {
1996                 struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[0]);
1998                 if (ofbi->num_overlays > 0) {
1999                         struct omap_overlay *ovl = ofbi->overlays[0];
2001                         ovl->manager->apply(ovl->manager);
2003                         r = omapfb_overlay_enable(ovl, 1);
2005                         if (r) {
2006                                 dev_err(fbdev->dev,
2007                                                 "failed to enable overlay\n");
2008                                 return r;
2009                         }
2010                 }
2011         }
2013         DBG("create_framebuffers done\n");
2015         return 0;
2018 static int omapfb_mode_to_timings(const char *mode_str,
2019                 struct omap_dss_device *display,
2020                 struct omap_video_timings *timings, u8 *bpp)
2022         struct fb_info *fbi;
2023         struct fb_var_screeninfo *var;
2024         struct fb_ops *fbops;
2025         int r;
2027 #ifdef CONFIG_OMAP2_DSS_VENC
2028         if (strcmp(mode_str, "pal") == 0) {
2029                 *timings = omap_dss_pal_timings;
2030                 *bpp = 24;
2031                 return 0;
2032         } else if (strcmp(mode_str, "ntsc") == 0) {
2033                 *timings = omap_dss_ntsc_timings;
2034                 *bpp = 24;
2035                 return 0;
2036         }
2037 #endif
2039         /* this is quite a hack, but I wanted to use the modedb and for
2040          * that we need fb_info and var, so we create dummy ones */
2042         *bpp = 0;
2043         fbi = NULL;
2044         var = NULL;
2045         fbops = NULL;
2047         fbi = kzalloc(sizeof(*fbi), GFP_KERNEL);
2048         if (fbi == NULL) {
2049                 r = -ENOMEM;
2050                 goto err;
2051         }
2053         var = kzalloc(sizeof(*var), GFP_KERNEL);
2054         if (var == NULL) {
2055                 r = -ENOMEM;
2056                 goto err;
2057         }
2059         fbops = kzalloc(sizeof(*fbops), GFP_KERNEL);
2060         if (fbops == NULL) {
2061                 r = -ENOMEM;
2062                 goto err;
2063         }
2065         fbi->fbops = fbops;
2067         r = fb_find_mode(var, fbi, mode_str, NULL, 0, NULL, 24);
2068         if (r == 0) {
2069                 r = -EINVAL;
2070                 goto err;
2071         }
2073         if (display->driver->get_timings) {
2074                 display->driver->get_timings(display, timings);
2075         } else {
2076                 timings->data_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE;
2077                 timings->de_level = OMAPDSS_SIG_ACTIVE_HIGH;
2078                 timings->sync_pclk_edge = OMAPDSS_DRIVE_SIG_FALLING_EDGE;
2079         }
2081         timings->pixelclock = PICOS2KHZ(var->pixclock) * 1000;
2082         timings->hbp = var->left_margin;
2083         timings->hfp = var->right_margin;
2084         timings->vbp = var->upper_margin;
2085         timings->vfp = var->lower_margin;
2086         timings->hsw = var->hsync_len;
2087         timings->vsw = var->vsync_len;
2088         timings->x_res = var->xres;
2089         timings->y_res = var->yres;
2090         timings->hsync_level = var->sync & FB_SYNC_HOR_HIGH_ACT ?
2091                                 OMAPDSS_SIG_ACTIVE_HIGH :
2092                                 OMAPDSS_SIG_ACTIVE_LOW;
2093         timings->vsync_level = var->sync & FB_SYNC_VERT_HIGH_ACT ?
2094                                 OMAPDSS_SIG_ACTIVE_HIGH :
2095                                 OMAPDSS_SIG_ACTIVE_LOW;
2096         timings->interlace = var->vmode & FB_VMODE_INTERLACED;
2098         switch (var->bits_per_pixel) {
2099         case 16:
2100                 *bpp = 16;
2101                 break;
2102         case 24:
2103         case 32:
2104         default:
2105                 *bpp = 24;
2106                 break;
2107         }
2109         r = 0;
2111 err:
2112         kfree(fbi);
2113         kfree(var);
2114         kfree(fbops);
2116         return r;
2119 static int omapfb_set_def_mode(struct omapfb2_device *fbdev,
2120                 struct omap_dss_device *display, char *mode_str)
2122         int r;
2123         u8 bpp;
2124         struct omap_video_timings timings, temp_timings;
2125         struct omapfb_display_data *d;
2127         r = omapfb_mode_to_timings(mode_str, display, &timings, &bpp);
2128         if (r)
2129                 return r;
2131         d = get_display_data(fbdev, display);
2132         d->bpp_override = bpp;
2134         if (display->driver->check_timings) {
2135                 r = display->driver->check_timings(display, &timings);
2136                 if (r)
2137                         return r;
2138         } else {
2139                 /* If check_timings is not present compare xres and yres */
2140                 if (display->driver->get_timings) {
2141                         display->driver->get_timings(display, &temp_timings);
2143                         if (temp_timings.x_res != timings.x_res ||
2144                                 temp_timings.y_res != timings.y_res)
2145                                 return -EINVAL;
2146                 }
2147         }
2149         if (display->driver->set_timings)
2150                         display->driver->set_timings(display, &timings);
2152         return 0;
2155 static int omapfb_get_recommended_bpp(struct omapfb2_device *fbdev,
2156                 struct omap_dss_device *dssdev)
2158         struct omapfb_display_data *d;
2160         BUG_ON(dssdev->driver->get_recommended_bpp == NULL);
2162         d = get_display_data(fbdev, dssdev);
2164         if (d->bpp_override != 0)
2165                 return d->bpp_override;
2167         return dssdev->driver->get_recommended_bpp(dssdev);
2170 static int omapfb_parse_def_modes(struct omapfb2_device *fbdev)
2172         char *str, *options, *this_opt;
2173         int r = 0;
2175         str = kstrdup(def_mode, GFP_KERNEL);
2176         if (!str)
2177                 return -ENOMEM;
2178         options = str;
2180         while (!r && (this_opt = strsep(&options, ",")) != NULL) {
2181                 char *p, *display_str, *mode_str;
2182                 struct omap_dss_device *display;
2183                 int i;
2185                 p = strchr(this_opt, ':');
2186                 if (!p) {
2187                         r = -EINVAL;
2188                         break;
2189                 }
2191                 *p = 0;
2192                 display_str = this_opt;
2193                 mode_str = p + 1;
2195                 display = NULL;
2196                 for (i = 0; i < fbdev->num_displays; ++i) {
2197                         if (strcmp(fbdev->displays[i].dssdev->name,
2198                                                 display_str) == 0) {
2199                                 display = fbdev->displays[i].dssdev;
2200                                 break;
2201                         }
2202                 }
2204                 if (!display) {
2205                         r = -EINVAL;
2206                         break;
2207                 }
2209                 r = omapfb_set_def_mode(fbdev, display, mode_str);
2210                 if (r)
2211                         break;
2212         }
2214         kfree(str);
2216         return r;
2219 static void fb_videomode_to_omap_timings(struct fb_videomode *m,
2220                 struct omap_dss_device *display,
2221                 struct omap_video_timings *t)
2223         if (display->driver->get_timings) {
2224                 display->driver->get_timings(display, t);
2225         } else {
2226                 t->data_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE;
2227                 t->de_level = OMAPDSS_SIG_ACTIVE_HIGH;
2228                 t->sync_pclk_edge = OMAPDSS_DRIVE_SIG_FALLING_EDGE;
2229         }
2231         t->x_res = m->xres;
2232         t->y_res = m->yres;
2233         t->pixelclock = PICOS2KHZ(m->pixclock) * 1000;
2234         t->hsw = m->hsync_len;
2235         t->hfp = m->right_margin;
2236         t->hbp = m->left_margin;
2237         t->vsw = m->vsync_len;
2238         t->vfp = m->lower_margin;
2239         t->vbp = m->upper_margin;
2240         t->hsync_level = m->sync & FB_SYNC_HOR_HIGH_ACT ?
2241                                 OMAPDSS_SIG_ACTIVE_HIGH :
2242                                 OMAPDSS_SIG_ACTIVE_LOW;
2243         t->vsync_level = m->sync & FB_SYNC_VERT_HIGH_ACT ?
2244                                 OMAPDSS_SIG_ACTIVE_HIGH :
2245                                 OMAPDSS_SIG_ACTIVE_LOW;
2246         t->interlace = m->vmode & FB_VMODE_INTERLACED;
2249 static int omapfb_find_best_mode(struct omap_dss_device *display,
2250                 struct omap_video_timings *timings)
2252         struct fb_monspecs *specs;
2253         u8 *edid;
2254         int r, i, best_idx, len;
2256         if (!display->driver->read_edid)
2257                 return -ENODEV;
2259         len = 0x80 * 2;
2260         edid = kmalloc(len, GFP_KERNEL);
2261         if (edid == NULL)
2262                 return -ENOMEM;
2264         r = display->driver->read_edid(display, edid, len);
2265         if (r < 0)
2266                 goto err1;
2268         specs = kzalloc(sizeof(*specs), GFP_KERNEL);
2269         if (specs == NULL) {
2270                 r = -ENOMEM;
2271                 goto err1;
2272         }
2274         fb_edid_to_monspecs(edid, specs);
2276         best_idx = -1;
2278         for (i = 0; i < specs->modedb_len; ++i) {
2279                 struct fb_videomode *m;
2280                 struct omap_video_timings t;
2282                 m = &specs->modedb[i];
2284                 if (m->pixclock == 0)
2285                         continue;
2287                 /* skip repeated pixel modes */
2288                 if (m->xres == 2880 || m->xres == 1440)
2289                         continue;
2291                 if (m->vmode & FB_VMODE_INTERLACED ||
2292                                 m->vmode & FB_VMODE_DOUBLE)
2293                         continue;
2295                 fb_videomode_to_omap_timings(m, display, &t);
2297                 r = display->driver->check_timings(display, &t);
2298                 if (r == 0) {
2299                         best_idx = i;
2300                         break;
2301                 }
2302         }
2304         if (best_idx == -1) {
2305                 r = -ENOENT;
2306                 goto err2;
2307         }
2309         fb_videomode_to_omap_timings(&specs->modedb[best_idx], display,
2310                 timings);
2312         r = 0;
2314 err2:
2315         fb_destroy_modedb(specs->modedb);
2316         kfree(specs);
2317 err1:
2318         kfree(edid);
2320         return r;
2323 static int omapfb_init_display(struct omapfb2_device *fbdev,
2324                 struct omap_dss_device *dssdev)
2326         struct omap_dss_driver *dssdrv = dssdev->driver;
2327         struct omapfb_display_data *d;
2328         int r;
2330         r = dssdrv->enable(dssdev);
2331         if (r) {
2332                 dev_warn(fbdev->dev, "Failed to enable display '%s'\n",
2333                                 dssdev->name);
2334                 return r;
2335         }
2337         d = get_display_data(fbdev, dssdev);
2339         d->fbdev = fbdev;
2341         if (dssdev->caps & OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE) {
2342                 u16 w, h;
2344                 if (auto_update) {
2345                         omapfb_start_auto_update(fbdev, dssdev);
2346                         d->update_mode = OMAPFB_AUTO_UPDATE;
2347                 } else {
2348                         d->update_mode = OMAPFB_MANUAL_UPDATE;
2349                 }
2351                 if (dssdrv->enable_te) {
2352                         r = dssdrv->enable_te(dssdev, 1);
2353                         if (r) {
2354                                 dev_err(fbdev->dev, "Failed to set TE\n");
2355                                 return r;
2356                         }
2357                 }
2359                 dssdrv->get_resolution(dssdev, &w, &h);
2360                 r = dssdrv->update(dssdev, 0, 0, w, h);
2361                 if (r) {
2362                         dev_err(fbdev->dev,
2363                                         "Failed to update display\n");
2364                         return r;
2365                 }
2366         } else {
2367                 d->update_mode = OMAPFB_AUTO_UPDATE;
2368         }
2370         return 0;
2373 static int omapfb_init_connections(struct omapfb2_device *fbdev,
2374                 struct omap_dss_device *def_dssdev)
2376         int i, r;
2377         struct omap_overlay_manager *mgr;
2379         r = def_dssdev->driver->connect(def_dssdev);
2380         if (r) {
2381                 dev_err(fbdev->dev, "failed to connect default display\n");
2382                 return r;
2383         }
2385         for (i = 0; i < fbdev->num_displays; ++i) {
2386                 struct omap_dss_device *dssdev = fbdev->displays[i].dssdev;
2388                 if (dssdev == def_dssdev)
2389                         continue;
2391                 /*
2392                  * We don't care if the connect succeeds or not. We just want to
2393                  * connect as many displays as possible.
2394                  */
2395                 dssdev->driver->connect(dssdev);
2396         }
2398         mgr = omapdss_find_mgr_from_display(def_dssdev);
2400         if (!mgr) {
2401                 dev_err(fbdev->dev, "no ovl manager for the default display\n");
2402                 return -EINVAL;
2403         }
2405         for (i = 0; i < fbdev->num_overlays; i++) {
2406                 struct omap_overlay *ovl = fbdev->overlays[i];
2408                 if (ovl->manager)
2409                         ovl->unset_manager(ovl);
2411                 r = ovl->set_manager(ovl, mgr);
2412                 if (r)
2413                         dev_warn(fbdev->dev,
2414                                         "failed to connect overlay %s to manager %s\n",
2415                                         ovl->name, mgr->name);
2416         }
2418         return 0;
2421 static struct omap_dss_device *
2422 omapfb_find_default_display(struct omapfb2_device *fbdev)
2424         const char *def_name;
2425         int i;
2427         /*
2428          * Search with the display name from the user or the board file,
2429          * comparing to display names and aliases
2430          */
2432         def_name = omapdss_get_default_display_name();
2434         if (def_name) {
2435                 for (i = 0; i < fbdev->num_displays; ++i) {
2436                         struct omap_dss_device *dssdev;
2438                         dssdev = fbdev->displays[i].dssdev;
2440                         if (dssdev->name && strcmp(def_name, dssdev->name) == 0)
2441                                 return dssdev;
2443                         if (strcmp(def_name, dssdev->alias) == 0)
2444                                 return dssdev;
2445                 }
2447                 /* def_name given but not found */
2448                 return NULL;
2449         }
2451         /* then look for DT alias display0 */
2452         for (i = 0; i < fbdev->num_displays; ++i) {
2453                 struct omap_dss_device *dssdev;
2454                 int id;
2456                 dssdev = fbdev->displays[i].dssdev;
2458                 if (dssdev->dev->of_node == NULL)
2459                         continue;
2461                 id = of_alias_get_id(dssdev->dev->of_node, "display");
2462                 if (id == 0)
2463                         return dssdev;
2464         }
2466         /* return the first display we have in the list */
2467         return fbdev->displays[0].dssdev;
2470 static int omapfb_probe(struct platform_device *pdev)
2472         struct omapfb2_device *fbdev = NULL;
2473         int r = 0;
2474         int i;
2475         struct omap_dss_device *def_display;
2476         struct omap_dss_device *dssdev;
2478         DBG("omapfb_probe\n");
2480         if (omapdss_is_initialized() == false)
2481                 return -EPROBE_DEFER;
2483         if (pdev->num_resources != 0) {
2484                 dev_err(&pdev->dev, "probed for an unknown device\n");
2485                 r = -ENODEV;
2486                 goto err0;
2487         }
2489         fbdev = devm_kzalloc(&pdev->dev, sizeof(struct omapfb2_device),
2490                         GFP_KERNEL);
2491         if (fbdev == NULL) {
2492                 r = -ENOMEM;
2493                 goto err0;
2494         }
2496         if (def_vrfb && !omap_vrfb_supported()) {
2497                 def_vrfb = 0;
2498                 dev_warn(&pdev->dev, "VRFB is not supported on this hardware, "
2499                                 "ignoring the module parameter vrfb=y\n");
2500         }
2502         r = omapdss_compat_init();
2503         if (r)
2504                 goto err0;
2506         mutex_init(&fbdev->mtx);
2508         fbdev->dev = &pdev->dev;
2509         platform_set_drvdata(pdev, fbdev);
2511         fbdev->num_displays = 0;
2512         dssdev = NULL;
2513         for_each_dss_dev(dssdev) {
2514                 struct omapfb_display_data *d;
2516                 omap_dss_get_device(dssdev);
2518                 if (!dssdev->driver) {
2519                         dev_warn(&pdev->dev, "no driver for display: %s\n",
2520                                 dssdev->name);
2521                         omap_dss_put_device(dssdev);
2522                         continue;
2523                 }
2525                 d = &fbdev->displays[fbdev->num_displays++];
2526                 d->dssdev = dssdev;
2527                 if (dssdev->caps & OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE)
2528                         d->update_mode = OMAPFB_MANUAL_UPDATE;
2529                 else
2530                         d->update_mode = OMAPFB_AUTO_UPDATE;
2531         }
2533         if (fbdev->num_displays == 0) {
2534                 dev_err(&pdev->dev, "no displays\n");
2535                 r = -EPROBE_DEFER;
2536                 goto cleanup;
2537         }
2539         fbdev->num_overlays = omap_dss_get_num_overlays();
2540         for (i = 0; i < fbdev->num_overlays; i++)
2541                 fbdev->overlays[i] = omap_dss_get_overlay(i);
2543         fbdev->num_managers = omap_dss_get_num_overlay_managers();
2544         for (i = 0; i < fbdev->num_managers; i++)
2545                 fbdev->managers[i] = omap_dss_get_overlay_manager(i);
2547         def_display = omapfb_find_default_display(fbdev);
2548         if (def_display == NULL) {
2549                 dev_err(fbdev->dev, "failed to find default display\n");
2550                 r = -EPROBE_DEFER;
2551                 goto cleanup;
2552         }
2554         r = omapfb_init_connections(fbdev, def_display);
2555         if (r) {
2556                 dev_err(fbdev->dev, "failed to init overlay connections\n");
2557                 goto cleanup;
2558         }
2560         if (def_mode && strlen(def_mode) > 0) {
2561                 if (omapfb_parse_def_modes(fbdev))
2562                         dev_warn(&pdev->dev, "cannot parse default modes\n");
2563         } else if (def_display && def_display->driver->set_timings &&
2564                         def_display->driver->check_timings) {
2565                 struct omap_video_timings t;
2567                 r = omapfb_find_best_mode(def_display, &t);
2569                 if (r == 0)
2570                         def_display->driver->set_timings(def_display, &t);
2571         }
2573         r = omapfb_create_framebuffers(fbdev);
2574         if (r)
2575                 goto cleanup;
2577         for (i = 0; i < fbdev->num_managers; i++) {
2578                 struct omap_overlay_manager *mgr;
2579                 mgr = fbdev->managers[i];
2580                 r = mgr->apply(mgr);
2581                 if (r)
2582                         dev_warn(fbdev->dev, "failed to apply dispc config\n");
2583         }
2585         DBG("mgr->apply'ed\n");
2587         if (def_display) {
2588                 r = omapfb_init_display(fbdev, def_display);
2589                 if (r) {
2590                         dev_err(fbdev->dev,
2591                                         "failed to initialize default "
2592                                         "display\n");
2593                         goto cleanup;
2594                 }
2595         }
2597         DBG("create sysfs for fbs\n");
2598         r = omapfb_create_sysfs(fbdev);
2599         if (r) {
2600                 dev_err(fbdev->dev, "failed to create sysfs entries\n");
2601                 goto cleanup;
2602         }
2604         if (def_display) {
2605                 u16 w, h;
2607                 def_display->driver->get_resolution(def_display, &w, &h);
2609                 dev_info(fbdev->dev, "using display '%s' mode %dx%d\n",
2610                         def_display->name, w, h);
2611         }
2613         /*
2614          * disable creation of new console during suspend.
2615          * this works around a problem where a ctrl-c is needed
2616          * to be entered on the VT to actually get the device
2617          * to continue into the suspend state.
2618          */
2619         pm_set_vt_switch(0);
2621         return 0;
2623 cleanup:
2624         omapfb_free_resources(fbdev);
2625         omapdss_compat_uninit();
2626 err0:
2627         dev_err(&pdev->dev, "failed to setup omapfb\n");
2628         return r;
2631 static int __exit omapfb_remove(struct platform_device *pdev)
2633         struct omapfb2_device *fbdev = platform_get_drvdata(pdev);
2635         /* FIXME: wait till completion of pending events */
2637         omapfb_remove_sysfs(fbdev);
2639         omapfb_free_resources(fbdev);
2641         omapdss_compat_uninit();
2643         return 0;
2646 static struct platform_driver omapfb_driver = {
2647         .probe          = omapfb_probe,
2648         .remove         = __exit_p(omapfb_remove),
2649         .driver         = {
2650                 .name   = "omapfb",
2651                 .owner  = THIS_MODULE,
2652         },
2653 };
2655 module_param_named(mode, def_mode, charp, 0);
2656 module_param_named(vram, def_vram, charp, 0);
2657 module_param_named(rotate, def_rotate, int, 0);
2658 module_param_named(vrfb, def_vrfb, bool, 0);
2659 module_param_named(mirror, def_mirror, bool, 0);
2661 module_platform_driver(omapfb_driver);
2663 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@nokia.com>");
2664 MODULE_DESCRIPTION("OMAP2/3 Framebuffer");
2665 MODULE_LICENSE("GPL v2");