2 * linux/drivers/video/omap2/omapfb-main.c
4 * Copyright (C) 2008 Nokia Corporation
5 * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
7 * Some code and ideas taken from drivers/video/omap/ driver
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.
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
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/>.
23 #include <linux/module.h>
24 #include <linux/delay.h>
25 #include <linux/slab.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>
33 #include <video/omapdss.h>
34 #include <plat/vram.h>
35 #include <plat/vrfb.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;
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);
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);
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);
66 static void draw_pixel(struct fb_info *fbi, int x, int y, unsigned color)
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;
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);
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;
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) {
135 unsigned r = 0, g = 0, b = 0;
137 if (var->bits_per_pixel == 16) {
139 b = (y % 32) * 256 / 32;
141 g = (y % 64) * 256 / 64;
143 r = (y % 32) * 256 / 32;
152 c = (r << 16) | (g << 8) | (b << 0);
153 draw_pixel(fbi, x, y, c);
155 draw_pixel(fbi, x, y, 0);
162 static unsigned omapfb_get_vrfb_offset(const struct omapfb_info *ofbi, int rot)
164 const struct vrfb *vrfb = &ofbi->region->vrfb;
172 offset = vrfb->yoffset;
175 offset = vrfb->yoffset * OMAP_VRFB_LINE_LEN + vrfb->xoffset;
178 offset = vrfb->xoffset * OMAP_VRFB_LINE_LEN;
185 offset *= vrfb->bytespp;
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);
196 return ofbi->region->paddr;
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];
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];
213 return ofbi->region->vaddr;
216 static struct omapfb_colormode omapfb_colormodes[] = {
218 .dssmode = OMAP_DSS_COLOR_UYVY,
219 .bits_per_pixel = 16,
220 .nonstd = OMAPFB_COLOR_YUV422,
222 .dssmode = OMAP_DSS_COLOR_YUV2,
223 .bits_per_pixel = 16,
224 .nonstd = OMAPFB_COLOR_YUY422,
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 },
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 },
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 },
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 },
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 },
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 },
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 },
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)
282 return f1->length == f2->length &&
283 f1->offset == f2->offset &&
284 f1->msb_right == f2->msb_right;
287 if (var->bits_per_pixel == 0 ||
288 var->red.length == 0 ||
289 var->blue.length == 0 ||
290 var->green.length == 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;
317 /* first match with nonstd field */
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);
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);
341 /* match with bpp if user has not filled color fields
343 switch (var->bits_per_pixel) {
345 dssmode = OMAP_DSS_COLOR_CLUT1;
348 dssmode = OMAP_DSS_COLOR_CLUT2;
351 dssmode = OMAP_DSS_COLOR_CLUT4;
354 dssmode = OMAP_DSS_COLOR_CLUT8;
357 dssmode = OMAP_DSS_COLOR_RGB12U;
360 dssmode = OMAP_DSS_COLOR_RGB16;
363 dssmode = OMAP_DSS_COLOR_RGB24P;
366 dssmode = OMAP_DSS_COLOR_RGB24U;
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);
384 static int check_fb_res_bounds(struct fb_var_screeninfo *var)
386 int xres_min = OMAPFB_PLANE_XRES_MIN;
388 int yres_min = OMAPFB_PLANE_YRES_MIN;
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)
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;
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) *
469 if (check_vrfb_fb_size(max_frame_size, var)) {
470 DBG("cannot fit FB to memory\n");
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;
487 if (line_size * var->yres_virtual > max_frame_size) {
488 DBG("cannot fit FB to memory\n");
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.
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;
511 enum omap_color_mode mode;
515 if (!rg->size || ofbi->rotation_type != OMAP_DSS_ROT_VRFB)
518 DBG("setup_vrfb_rotation\n");
520 r = fb_mode_to_dss_mode(var, &mode);
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 */
535 if (yuv_mode != vrfb->yuv_mode)
537 else if (bytespp != vrfb->bytespp)
539 else if (vrfb->xres != var->xres_virtual ||
540 vrfb->yres != var->yres_virtual)
543 if (vrfb->vaddr[0] && reconf) {
544 fbi->screen_base = NULL;
547 iounmap(vrfb->vaddr[0]);
548 vrfb->vaddr[0] = NULL;
549 DBG("setup_vrfb_rotation: reset fb\n");
555 omap_vrfb_setup(&rg->vrfb, rg->paddr,
560 /* Now one can ioremap the 0 angle view */
561 r = omap_vrfb_map_angle(vrfb, var->yres_virtual, 0);
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:
574 (OMAP_VRFB_LINE_LEN * var->bits_per_pixel) >> 2;
578 (OMAP_VRFB_LINE_LEN * var->bits_per_pixel) >> 3;
582 fix->smem_len = var->yres_virtual * fix->line_length;
587 int dss_mode_to_fb_mode(enum omap_color_mode dssmode,
588 struct fb_var_screeninfo *var)
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);
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;
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:
620 (OMAP_VRFB_LINE_LEN * var->bits_per_pixel) >> 2;
624 (OMAP_VRFB_LINE_LEN * var->bits_per_pixel) >> 3;
628 fix->smem_len = var->yres_virtual * fix->line_length;
631 (var->xres_virtual * var->bits_per_pixel) >> 3;
632 fix->smem_len = rg->size;
635 fix->smem_start = omapfb_get_region_paddr(ofbi);
637 fix->type = FB_TYPE_PACKED_PIXELS;
640 fix->visual = FB_VISUAL_PSEUDOCOLOR;
642 switch (var->bits_per_pixel) {
647 fix->visual = FB_VISUAL_TRUECOLOR;
648 /* 12bpp is stored in 16 bits */
654 fix->visual = FB_VISUAL_PSEUDOCOLOR;
659 fix->accel = FB_ACCEL_NONE;
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;
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);
680 DBG("cannot convert var to omap dss mode\n");
684 for (i = 0; i < ofbi->num_overlays; ++i) {
685 if ((ofbi->overlays[i]->supported_modes & mode) == 0) {
686 DBG("invalid mode\n");
694 if (check_fb_res_bounds(var))
697 /* When no memory is allocated ignore the size check */
698 if (ofbi->region->size != 0 && check_fb_size(ofbi, var))
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) {
712 display->driver->get_dimensions(display, &w, &h);
713 var->width = DIV_ROUND_CLOSEST(w, 1000);
714 var->height = DIV_ROUND_CLOSEST(h, 1000);
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.pixel_clock != 0 ?
728 KHZ2PICOS(timings.pixel_clock) :
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;
744 var->left_margin = 0;
745 var->right_margin = 0;
746 var->upper_margin = 0;
747 var->lower_margin = 0;
751 var->vmode = FB_VMODE_NONINTERLACED;
758 * ---------------------------------------------------------------------------
759 * fbdev framework callbacks
760 * ---------------------------------------------------------------------------
762 static int omapfb_open(struct fb_info *fbi, int user)
767 static int omapfb_release(struct fb_info *fbi, int user)
772 static unsigned calc_rotation_offset_dma(const struct fb_var_screeninfo *var,
773 const struct fb_fix_screeninfo *fix, int rotation)
777 offset = var->yoffset * fix->line_length +
778 var->xoffset * (var->bits_per_pixel >> 3);
783 static unsigned calc_rotation_offset_vrfb(const struct fb_var_screeninfo *var,
784 const struct fb_fix_screeninfo *fix, int rotation)
788 if (rotation == FB_ROTATE_UD)
789 offset = (var->yres_virtual - var->yres) *
791 else if (rotation == FB_ROTATE_CW)
792 offset = (var->yres_virtual - var->yres) *
793 (var->bits_per_pixel >> 3);
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);
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)
821 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
822 data_start_p = omapfb_get_region_rot_paddr(ofbi, rotation);
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);
829 offset = calc_rotation_offset_dma(var, fix, rotation);
831 data_start_p += 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)
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;
856 int rotation = var->rotate;
859 WARN_ON(!atomic_read(&ofbi->region->lock_count));
861 for (i = 0; i < ofbi->num_overlays; i++) {
862 if (ovl != ofbi->overlays[i])
865 rotation = (rotation + ofbi->rotation[i]) % 4;
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) {
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);
885 DBG("fb_mode_to_dss_mode failed");
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);
898 screen_width = fix->line_length / (var->bits_per_pixel >> 3);
902 ovl->get_overlay_info(ovl, &info);
904 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
907 mirror = ofbi->mirror;
909 info.paddr = data_start_p;
910 info.screen_width = screen_width;
913 info.color_mode = mode;
914 info.rotation_type = ofbi->rotation_type;
915 info.rotation = rotation;
916 info.mirror = mirror;
920 info.out_width = outw;
921 info.out_height = outh;
923 r = ovl->set_overlay_info(ovl, &info);
925 DBG("ovl->setup_overlay_info failed\n");
932 DBG("setup_overlay failed\n");
936 /* apply var to the overlay */
937 int omapfb_apply_changes(struct fb_info *fbi, int init)
940 struct omapfb_info *ofbi = FB2OFB(fbi);
941 struct fb_var_screeninfo *var = &fbi->var;
942 struct omap_overlay *ovl;
948 if (omapfb_test_pattern)
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);
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) {
978 struct omap_overlay_info info;
979 ovl->get_overlay_info(ovl, &info);
980 outw = info.out_width;
981 outh = info.out_height;
988 struct omap_overlay_info info;
989 ovl->get_overlay_info(ovl, &info);
994 r = omapfb_setup_overlay(fbi, ovl, posx, posy, outw, outh);
998 if (!init && ovl->manager)
999 ovl->manager->apply(ovl->manager);
1003 DBG("apply_changes failed\n");
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);
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);
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);
1031 DBG("set_par(%d)\n", FB2OFB(fbi)->id);
1033 omapfb_get_mem_region(ofbi->region);
1037 r = setup_vrfb_rotation(fbi);
1041 r = omapfb_apply_changes(fbi, 0);
1044 omapfb_put_mem_region(ofbi->region);
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;
1056 DBG("pan_display(%d)\n", FB2OFB(fbi)->id);
1058 if (var->xoffset == fbi->var.xoffset &&
1059 var->yoffset == fbi->var.yoffset)
1063 new_var.xoffset = var->xoffset;
1064 new_var.yoffset = var->yoffset;
1068 omapfb_get_mem_region(ofbi->region);
1070 r = omapfb_apply_changes(fbi, 0);
1072 omapfb_put_mem_region(ofbi->region);
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,
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;
1106 unsigned long start;
1110 if (vma->vm_end - vma->vm_start == 0)
1112 if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
1114 off = vma->vm_pgoff << PAGE_SHIFT;
1116 rg = omapfb_get_mem_region(ofbi->region);
1118 start = omapfb_get_region_paddr(ofbi);
1119 len = fix->smem_len;
1122 if ((vma->vm_end - vma->vm_start + off) > len)
1127 DBG("user mmap region start %lx, len %d, off %lx\n", start, len, off);
1129 vma->vm_pgoff = off >> PAGE_SHIFT;
1130 /* VM_IO | VM_DONTEXPAND | VM_DONTDUMP are set by remap_pfn_range() */
1131 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
1132 vma->vm_ops = &mmap_user_ops;
1133 vma->vm_private_data = rg;
1134 if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
1135 vma->vm_end - vma->vm_start,
1136 vma->vm_page_prot)) {
1141 /* vm_ops.open won't be called for mmap itself. */
1142 atomic_inc(&rg->map_count);
1144 omapfb_put_mem_region(rg);
1149 omapfb_put_mem_region(ofbi->region);
1154 /* Store a single color palette entry into a pseudo palette or the hardware
1155 * palette if one is available. For now we support only 16bpp and thus store
1156 * the entry only to the pseudo palette.
1158 static int _setcolreg(struct fb_info *fbi, u_int regno, u_int red, u_int green,
1159 u_int blue, u_int transp, int update_hw_pal)
1161 /*struct omapfb_info *ofbi = FB2OFB(fbi);*/
1162 /*struct omapfb2_device *fbdev = ofbi->fbdev;*/
1163 struct fb_var_screeninfo *var = &fbi->var;
1166 enum omapfb_color_format mode = OMAPFB_COLOR_RGB24U; /* XXX */
1168 /*switch (plane->color_mode) {*/
1170 case OMAPFB_COLOR_YUV422:
1171 case OMAPFB_COLOR_YUV420:
1172 case OMAPFB_COLOR_YUY422:
1175 case OMAPFB_COLOR_CLUT_8BPP:
1176 case OMAPFB_COLOR_CLUT_4BPP:
1177 case OMAPFB_COLOR_CLUT_2BPP:
1178 case OMAPFB_COLOR_CLUT_1BPP:
1180 if (fbdev->ctrl->setcolreg)
1181 r = fbdev->ctrl->setcolreg(regno, red, green, blue,
1182 transp, update_hw_pal);
1187 case OMAPFB_COLOR_RGB565:
1188 case OMAPFB_COLOR_RGB444:
1189 case OMAPFB_COLOR_RGB24P:
1190 case OMAPFB_COLOR_RGB24U:
1196 pal = ((red >> (16 - var->red.length)) <<
1198 ((green >> (16 - var->green.length)) <<
1199 var->green.offset) |
1200 (blue >> (16 - var->blue.length));
1201 ((u32 *)(fbi->pseudo_palette))[regno] = pal;
1210 static int omapfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
1211 u_int transp, struct fb_info *info)
1215 return _setcolreg(info, regno, red, green, blue, transp, 1);
1218 static int omapfb_setcmap(struct fb_cmap *cmap, struct fb_info *info)
1220 int count, index, r;
1221 u16 *red, *green, *blue, *transp;
1227 green = cmap->green;
1229 transp = cmap->transp;
1230 index = cmap->start;
1232 for (count = 0; count < cmap->len; count++) {
1235 r = _setcolreg(info, index++, *red++, *green++, *blue++, trans,
1236 count == cmap->len - 1);
1244 static int omapfb_blank(int blank, struct fb_info *fbi)
1246 struct omapfb_info *ofbi = FB2OFB(fbi);
1247 struct omapfb2_device *fbdev = ofbi->fbdev;
1248 struct omap_dss_device *display = fb2display(fbi);
1249 struct omapfb_display_data *d;
1257 d = get_display_data(fbdev, display);
1260 case FB_BLANK_UNBLANK:
1261 if (display->state != OMAP_DSS_DISPLAY_SUSPENDED)
1264 if (display->driver->resume)
1265 r = display->driver->resume(display);
1267 if ((display->caps & OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE) &&
1268 d->update_mode == OMAPFB_AUTO_UPDATE &&
1269 !d->auto_update_work_enabled)
1270 omapfb_start_auto_update(fbdev, display);
1274 case FB_BLANK_NORMAL:
1275 /* FB_BLANK_NORMAL could be implemented.
1276 * Needs DSS additions. */
1277 case FB_BLANK_VSYNC_SUSPEND:
1278 case FB_BLANK_HSYNC_SUSPEND:
1279 case FB_BLANK_POWERDOWN:
1280 if (display->state != OMAP_DSS_DISPLAY_ACTIVE)
1283 if (d->auto_update_work_enabled)
1284 omapfb_stop_auto_update(fbdev, display);
1286 if (display->driver->suspend)
1287 r = display->driver->suspend(display);
1296 omapfb_unlock(fbdev);
1302 /* XXX fb_read and fb_write are needed for VRFB */
1303 ssize_t omapfb_write(struct fb_info *info, const char __user *buf,
1304 size_t count, loff_t *ppos)
1306 DBG("omapfb_write %d, %lu\n", count, (unsigned long)*ppos);
1307 /* XXX needed for VRFB */
1312 static struct fb_ops omapfb_ops = {
1313 .owner = THIS_MODULE,
1314 .fb_open = omapfb_open,
1315 .fb_release = omapfb_release,
1316 .fb_fillrect = cfb_fillrect,
1317 .fb_copyarea = cfb_copyarea,
1318 .fb_imageblit = cfb_imageblit,
1319 .fb_blank = omapfb_blank,
1320 .fb_ioctl = omapfb_ioctl,
1321 .fb_check_var = omapfb_check_var,
1322 .fb_set_par = omapfb_set_par,
1323 .fb_pan_display = omapfb_pan_display,
1324 .fb_mmap = omapfb_mmap,
1325 .fb_setcolreg = omapfb_setcolreg,
1326 .fb_setcmap = omapfb_setcmap,
1327 /*.fb_write = omapfb_write,*/
1330 static void omapfb_free_fbmem(struct fb_info *fbi)
1332 struct omapfb_info *ofbi = FB2OFB(fbi);
1333 struct omapfb2_device *fbdev = ofbi->fbdev;
1334 struct omapfb2_mem_region *rg;
1338 WARN_ON(atomic_read(&rg->map_count));
1341 if (omap_vram_free(rg->paddr, rg->size))
1342 dev_err(fbdev->dev, "VRAM FREE failed\n");
1347 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
1348 /* unmap the 0 angle rotation */
1349 if (rg->vrfb.vaddr[0]) {
1350 iounmap(rg->vrfb.vaddr[0]);
1351 omap_vrfb_release_ctx(&rg->vrfb);
1352 rg->vrfb.vaddr[0] = NULL;
1362 static void clear_fb_info(struct fb_info *fbi)
1364 memset(&fbi->var, 0, sizeof(fbi->var));
1365 memset(&fbi->fix, 0, sizeof(fbi->fix));
1366 strlcpy(fbi->fix.id, MODULE_NAME, sizeof(fbi->fix.id));
1369 static int omapfb_free_all_fbmem(struct omapfb2_device *fbdev)
1373 DBG("free all fbmem\n");
1375 for (i = 0; i < fbdev->num_fbs; i++) {
1376 struct fb_info *fbi = fbdev->fbs[i];
1377 omapfb_free_fbmem(fbi);
1384 static int omapfb_alloc_fbmem(struct fb_info *fbi, unsigned long size,
1385 unsigned long paddr)
1387 struct omapfb_info *ofbi = FB2OFB(fbi);
1388 struct omapfb2_device *fbdev = ofbi->fbdev;
1389 struct omapfb2_mem_region *rg;
1390 void __iomem *vaddr;
1397 memset(&rg->vrfb, 0, sizeof rg->vrfb);
1403 size = PAGE_ALIGN(size);
1406 DBG("allocating %lu bytes for fb %d\n", size, ofbi->id);
1407 r = omap_vram_alloc(size, &paddr);
1409 DBG("reserving %lu bytes at %lx for fb %d\n", size, paddr,
1411 r = omap_vram_reserve(paddr, size);
1415 dev_err(fbdev->dev, "failed to allocate framebuffer\n");
1419 if (ofbi->rotation_type != OMAP_DSS_ROT_VRFB) {
1420 vaddr = ioremap_wc(paddr, size);
1423 dev_err(fbdev->dev, "failed to ioremap framebuffer\n");
1424 omap_vram_free(paddr, size);
1428 DBG("allocated VRAM paddr %lx, vaddr %p\n", paddr, vaddr);
1430 r = omap_vrfb_request_ctx(&rg->vrfb);
1432 dev_err(fbdev->dev, "vrfb create ctx failed\n");
1447 /* allocate fbmem using display resolution as reference */
1448 static int omapfb_alloc_fbmem_display(struct fb_info *fbi, unsigned long size,
1449 unsigned long paddr)
1451 struct omapfb_info *ofbi = FB2OFB(fbi);
1452 struct omapfb2_device *fbdev = ofbi->fbdev;
1453 struct omap_dss_device *display;
1456 display = fb2display(fbi);
1461 switch (omapfb_get_recommended_bpp(fbdev, display)) {
1476 display->driver->get_resolution(display, &w, &h);
1478 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
1479 size = max(omap_vrfb_min_phys_size(w, h, bytespp),
1480 omap_vrfb_min_phys_size(h, w, bytespp));
1482 DBG("adjusting fb mem size for VRFB, %u -> %lu\n",
1483 w * h * bytespp, size);
1485 size = w * h * bytespp;
1492 return omapfb_alloc_fbmem(fbi, size, paddr);
1495 static int omapfb_parse_vram_param(const char *param, int max_entries,
1496 unsigned long *sizes, unsigned long *paddrs)
1500 unsigned long paddr = 0;
1503 start = (char *)param;
1508 fbnum = simple_strtoul(p, &p, 10);
1516 if (fbnum >= max_entries)
1519 size = memparse(p + 1, &p);
1527 paddr = simple_strtoul(p + 1, &p, 16);
1534 paddrs[fbnum] = paddr;
1535 sizes[fbnum] = size;
1551 static int omapfb_allocate_all_fbs(struct omapfb2_device *fbdev)
1554 unsigned long vram_sizes[10];
1555 unsigned long vram_paddrs[10];
1557 memset(&vram_sizes, 0, sizeof(vram_sizes));
1558 memset(&vram_paddrs, 0, sizeof(vram_paddrs));
1560 if (def_vram && omapfb_parse_vram_param(def_vram, 10,
1561 vram_sizes, vram_paddrs)) {
1562 dev_err(fbdev->dev, "failed to parse vram parameter\n");
1564 memset(&vram_sizes, 0, sizeof(vram_sizes));
1565 memset(&vram_paddrs, 0, sizeof(vram_paddrs));
1568 for (i = 0; i < fbdev->num_fbs; i++) {
1569 /* allocate memory automatically only for fb0, or if
1570 * excplicitly defined with vram or plat data option */
1571 if (i == 0 || vram_sizes[i] != 0) {
1572 r = omapfb_alloc_fbmem_display(fbdev->fbs[i],
1573 vram_sizes[i], vram_paddrs[i]);
1580 for (i = 0; i < fbdev->num_fbs; i++) {
1581 struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[i]);
1582 struct omapfb2_mem_region *rg;
1585 DBG("region%d phys %08x virt %p size=%lu\n",
1595 static void omapfb_clear_fb(struct fb_info *fbi)
1597 const struct fb_fillrect rect = {
1600 .width = fbi->var.xres_virtual,
1601 .height = fbi->var.yres_virtual,
1606 cfb_fillrect(fbi, &rect);
1609 int omapfb_realloc_fbmem(struct fb_info *fbi, unsigned long size, int type)
1611 struct omapfb_info *ofbi = FB2OFB(fbi);
1612 struct omapfb2_device *fbdev = ofbi->fbdev;
1613 struct omap_dss_device *display = fb2display(fbi);
1614 struct omapfb2_mem_region *rg = ofbi->region;
1615 unsigned long old_size = rg->size;
1616 unsigned long old_paddr = rg->paddr;
1617 int old_type = rg->type;
1620 if (type != OMAPFB_MEMTYPE_SDRAM)
1623 size = PAGE_ALIGN(size);
1625 if (old_size == size && old_type == type)
1628 if (display && display->driver->sync)
1629 display->driver->sync(display);
1631 omapfb_free_fbmem(fbi);
1638 r = omapfb_alloc_fbmem(fbi, size, 0);
1642 omapfb_alloc_fbmem(fbi, old_size, old_paddr);
1650 if (old_size == size)
1653 if (old_size == 0) {
1654 DBG("initializing fb %d\n", ofbi->id);
1655 r = omapfb_fb_init(fbdev, fbi);
1657 DBG("omapfb_fb_init failed\n");
1660 r = omapfb_apply_changes(fbi, 1);
1662 DBG("omapfb_apply_changes failed\n");
1666 struct fb_var_screeninfo new_var;
1667 memcpy(&new_var, &fbi->var, sizeof(new_var));
1668 r = check_fb_var(fbi, &new_var);
1671 memcpy(&fbi->var, &new_var, sizeof(fbi->var));
1673 r = setup_vrfb_rotation(fbi);
1678 omapfb_clear_fb(fbi);
1682 omapfb_free_fbmem(fbi);
1687 static void omapfb_auto_update_work(struct work_struct *work)
1689 struct omap_dss_device *dssdev;
1690 struct omap_dss_driver *dssdrv;
1691 struct omapfb_display_data *d;
1694 struct omapfb2_device *fbdev;
1696 d = container_of(work, struct omapfb_display_data,
1697 auto_update_work.work);
1700 dssdrv = dssdev->driver;
1703 if (!dssdrv || !dssdrv->update)
1707 dssdrv->sync(dssdev);
1709 dssdrv->get_resolution(dssdev, &w, &h);
1710 dssdrv->update(dssdev, 0, 0, w, h);
1712 freq = auto_update_freq;
1715 queue_delayed_work(fbdev->auto_update_wq,
1716 &d->auto_update_work, HZ / freq);
1719 void omapfb_start_auto_update(struct omapfb2_device *fbdev,
1720 struct omap_dss_device *display)
1722 struct omapfb_display_data *d;
1724 if (fbdev->auto_update_wq == NULL) {
1725 struct workqueue_struct *wq;
1727 wq = create_singlethread_workqueue("omapfb_auto_update");
1730 dev_err(fbdev->dev, "Failed to create workqueue for "
1735 fbdev->auto_update_wq = wq;
1738 d = get_display_data(fbdev, display);
1740 INIT_DELAYED_WORK(&d->auto_update_work, omapfb_auto_update_work);
1742 d->auto_update_work_enabled = true;
1744 omapfb_auto_update_work(&d->auto_update_work.work);
1747 void omapfb_stop_auto_update(struct omapfb2_device *fbdev,
1748 struct omap_dss_device *display)
1750 struct omapfb_display_data *d;
1752 d = get_display_data(fbdev, display);
1754 cancel_delayed_work_sync(&d->auto_update_work);
1756 d->auto_update_work_enabled = false;
1759 /* initialize fb_info, var, fix to something sane based on the display */
1760 static int omapfb_fb_init(struct omapfb2_device *fbdev, struct fb_info *fbi)
1762 struct fb_var_screeninfo *var = &fbi->var;
1763 struct omap_dss_device *display = fb2display(fbi);
1764 struct omapfb_info *ofbi = FB2OFB(fbi);
1767 fbi->fbops = &omapfb_ops;
1768 fbi->flags = FBINFO_FLAG_DEFAULT;
1769 fbi->pseudo_palette = fbdev->pseudo_palette;
1771 if (ofbi->region->size == 0) {
1777 var->bits_per_pixel = 0;
1779 var->rotate = def_rotate;
1783 int rotation = (var->rotate + ofbi->rotation[0]) % 4;
1785 display->driver->get_resolution(display, &w, &h);
1787 if (rotation == FB_ROTATE_CW ||
1788 rotation == FB_ROTATE_CCW) {
1796 var->xres_virtual = var->xres;
1797 var->yres_virtual = var->yres;
1799 if (!var->bits_per_pixel) {
1800 switch (omapfb_get_recommended_bpp(fbdev, display)) {
1802 var->bits_per_pixel = 16;
1805 var->bits_per_pixel = 32;
1808 dev_err(fbdev->dev, "illegal display "
1814 /* if there's no display, let's just guess some basic values */
1817 var->xres_virtual = var->xres;
1818 var->yres_virtual = var->yres;
1819 if (!var->bits_per_pixel)
1820 var->bits_per_pixel = 16;
1823 r = check_fb_var(fbi, var);
1828 r = setup_vrfb_rotation(fbi);
1832 r = fb_alloc_cmap(&fbi->cmap, 256, 0);
1834 dev_err(fbdev->dev, "unable to allocate color map memory\n");
1840 static void fbinfo_cleanup(struct omapfb2_device *fbdev, struct fb_info *fbi)
1842 fb_dealloc_cmap(&fbi->cmap);
1846 static void omapfb_free_resources(struct omapfb2_device *fbdev)
1850 DBG("free_resources\n");
1855 for (i = 0; i < fbdev->num_fbs; i++)
1856 unregister_framebuffer(fbdev->fbs[i]);
1858 /* free the reserved fbmem */
1859 omapfb_free_all_fbmem(fbdev);
1861 for (i = 0; i < fbdev->num_fbs; i++) {
1862 fbinfo_cleanup(fbdev, fbdev->fbs[i]);
1863 framebuffer_release(fbdev->fbs[i]);
1866 for (i = 0; i < fbdev->num_displays; i++) {
1867 struct omap_dss_device *dssdev = fbdev->displays[i].dssdev;
1869 if (fbdev->displays[i].auto_update_work_enabled)
1870 omapfb_stop_auto_update(fbdev, dssdev);
1872 if (dssdev->state != OMAP_DSS_DISPLAY_DISABLED)
1873 dssdev->driver->disable(dssdev);
1875 omap_dss_put_device(dssdev);
1878 if (fbdev->auto_update_wq != NULL) {
1879 flush_workqueue(fbdev->auto_update_wq);
1880 destroy_workqueue(fbdev->auto_update_wq);
1881 fbdev->auto_update_wq = NULL;
1884 dev_set_drvdata(fbdev->dev, NULL);
1888 static int omapfb_create_framebuffers(struct omapfb2_device *fbdev)
1894 DBG("create %d framebuffers\n", CONFIG_FB_OMAP2_NUM_FBS);
1896 /* allocate fb_infos */
1897 for (i = 0; i < CONFIG_FB_OMAP2_NUM_FBS; i++) {
1898 struct fb_info *fbi;
1899 struct omapfb_info *ofbi;
1901 fbi = framebuffer_alloc(sizeof(struct omapfb_info),
1906 "unable to allocate memory for plane info\n");
1912 fbdev->fbs[i] = fbi;
1915 ofbi->fbdev = fbdev;
1918 ofbi->region = &fbdev->regions[i];
1919 ofbi->region->id = i;
1920 init_rwsem(&ofbi->region->lock);
1922 /* assign these early, so that fb alloc can use them */
1923 ofbi->rotation_type = def_vrfb ? OMAP_DSS_ROT_VRFB :
1925 ofbi->mirror = def_mirror;
1930 DBG("fb_infos allocated\n");
1932 /* assign overlays for the fbs */
1933 for (i = 0; i < min(fbdev->num_fbs, fbdev->num_overlays); i++) {
1934 struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[i]);
1936 ofbi->overlays[0] = fbdev->overlays[i];
1937 ofbi->num_overlays = 1;
1940 /* allocate fb memories */
1941 r = omapfb_allocate_all_fbs(fbdev);
1943 dev_err(fbdev->dev, "failed to allocate fbmem\n");
1947 DBG("fbmems allocated\n");
1949 /* setup fb_infos */
1950 for (i = 0; i < fbdev->num_fbs; i++) {
1951 struct fb_info *fbi = fbdev->fbs[i];
1952 struct omapfb_info *ofbi = FB2OFB(fbi);
1954 omapfb_get_mem_region(ofbi->region);
1955 r = omapfb_fb_init(fbdev, fbi);
1956 omapfb_put_mem_region(ofbi->region);
1959 dev_err(fbdev->dev, "failed to setup fb_info\n");
1964 DBG("fb_infos initialized\n");
1966 for (i = 0; i < fbdev->num_fbs; i++) {
1967 r = register_framebuffer(fbdev->fbs[i]);
1970 "registering framebuffer %d failed\n", i);
1975 DBG("framebuffers registered\n");
1977 for (i = 0; i < fbdev->num_fbs; i++) {
1978 struct fb_info *fbi = fbdev->fbs[i];
1979 struct omapfb_info *ofbi = FB2OFB(fbi);
1981 omapfb_get_mem_region(ofbi->region);
1982 r = omapfb_apply_changes(fbi, 1);
1983 omapfb_put_mem_region(ofbi->region);
1986 dev_err(fbdev->dev, "failed to change mode\n");
1991 for (i = 0; i < fbdev->num_fbs; i++) {
1992 struct fb_info *fbi = fbdev->fbs[i];
1993 struct omapfb_info *ofbi = FB2OFB(fbi);
1995 if (ofbi->region->size == 0)
1998 omapfb_clear_fb(fbi);
2002 if (fbdev->num_fbs > 0) {
2003 struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[0]);
2005 if (ofbi->num_overlays > 0) {
2006 struct omap_overlay *ovl = ofbi->overlays[0];
2008 ovl->manager->apply(ovl->manager);
2010 r = omapfb_overlay_enable(ovl, 1);
2014 "failed to enable overlay\n");
2020 DBG("create_framebuffers done\n");
2025 static int omapfb_mode_to_timings(const char *mode_str,
2026 struct omap_dss_device *display,
2027 struct omap_video_timings *timings, u8 *bpp)
2029 struct fb_info *fbi;
2030 struct fb_var_screeninfo *var;
2031 struct fb_ops *fbops;
2034 #ifdef CONFIG_OMAP2_DSS_VENC
2035 if (strcmp(mode_str, "pal") == 0) {
2036 *timings = omap_dss_pal_timings;
2039 } else if (strcmp(mode_str, "ntsc") == 0) {
2040 *timings = omap_dss_ntsc_timings;
2046 /* this is quite a hack, but I wanted to use the modedb and for
2047 * that we need fb_info and var, so we create dummy ones */
2054 fbi = kzalloc(sizeof(*fbi), GFP_KERNEL);
2060 var = kzalloc(sizeof(*var), GFP_KERNEL);
2066 fbops = kzalloc(sizeof(*fbops), GFP_KERNEL);
2067 if (fbops == NULL) {
2074 r = fb_find_mode(var, fbi, mode_str, NULL, 0, NULL, 24);
2080 if (display->driver->get_timings) {
2081 display->driver->get_timings(display, timings);
2083 timings->data_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE;
2084 timings->de_level = OMAPDSS_SIG_ACTIVE_HIGH;
2085 timings->sync_pclk_edge = OMAPDSS_DRIVE_SIG_OPPOSITE_EDGES;
2088 timings->pixel_clock = PICOS2KHZ(var->pixclock);
2089 timings->hbp = var->left_margin;
2090 timings->hfp = var->right_margin;
2091 timings->vbp = var->upper_margin;
2092 timings->vfp = var->lower_margin;
2093 timings->hsw = var->hsync_len;
2094 timings->vsw = var->vsync_len;
2095 timings->x_res = var->xres;
2096 timings->y_res = var->yres;
2097 timings->hsync_level = var->sync & FB_SYNC_HOR_HIGH_ACT ?
2098 OMAPDSS_SIG_ACTIVE_HIGH :
2099 OMAPDSS_SIG_ACTIVE_LOW;
2100 timings->vsync_level = var->sync & FB_SYNC_VERT_HIGH_ACT ?
2101 OMAPDSS_SIG_ACTIVE_HIGH :
2102 OMAPDSS_SIG_ACTIVE_LOW;
2103 timings->interlace = var->vmode & FB_VMODE_INTERLACED;
2105 switch (var->bits_per_pixel) {
2126 static int omapfb_set_def_mode(struct omapfb2_device *fbdev,
2127 struct omap_dss_device *display, char *mode_str)
2131 struct omap_video_timings timings, temp_timings;
2132 struct omapfb_display_data *d;
2134 r = omapfb_mode_to_timings(mode_str, display, &timings, &bpp);
2138 d = get_display_data(fbdev, display);
2139 d->bpp_override = bpp;
2141 if (display->driver->check_timings) {
2142 r = display->driver->check_timings(display, &timings);
2146 /* If check_timings is not present compare xres and yres */
2147 if (display->driver->get_timings) {
2148 display->driver->get_timings(display, &temp_timings);
2150 if (temp_timings.x_res != timings.x_res ||
2151 temp_timings.y_res != timings.y_res)
2156 if (display->driver->set_timings)
2157 display->driver->set_timings(display, &timings);
2162 static int omapfb_get_recommended_bpp(struct omapfb2_device *fbdev,
2163 struct omap_dss_device *dssdev)
2165 struct omapfb_display_data *d;
2167 BUG_ON(dssdev->driver->get_recommended_bpp == NULL);
2169 d = get_display_data(fbdev, dssdev);
2171 if (d->bpp_override != 0)
2172 return d->bpp_override;
2174 return dssdev->driver->get_recommended_bpp(dssdev);
2177 static int omapfb_parse_def_modes(struct omapfb2_device *fbdev)
2179 char *str, *options, *this_opt;
2182 str = kstrdup(def_mode, GFP_KERNEL);
2187 while (!r && (this_opt = strsep(&options, ",")) != NULL) {
2188 char *p, *display_str, *mode_str;
2189 struct omap_dss_device *display;
2192 p = strchr(this_opt, ':');
2199 display_str = this_opt;
2203 for (i = 0; i < fbdev->num_displays; ++i) {
2204 if (strcmp(fbdev->displays[i].dssdev->name,
2205 display_str) == 0) {
2206 display = fbdev->displays[i].dssdev;
2216 r = omapfb_set_def_mode(fbdev, display, mode_str);
2226 static void fb_videomode_to_omap_timings(struct fb_videomode *m,
2227 struct omap_dss_device *display,
2228 struct omap_video_timings *t)
2230 if (display->driver->get_timings) {
2231 display->driver->get_timings(display, t);
2233 t->data_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE;
2234 t->de_level = OMAPDSS_SIG_ACTIVE_HIGH;
2235 t->sync_pclk_edge = OMAPDSS_DRIVE_SIG_OPPOSITE_EDGES;
2240 t->pixel_clock = PICOS2KHZ(m->pixclock);
2241 t->hsw = m->hsync_len;
2242 t->hfp = m->right_margin;
2243 t->hbp = m->left_margin;
2244 t->vsw = m->vsync_len;
2245 t->vfp = m->lower_margin;
2246 t->vbp = m->upper_margin;
2247 t->hsync_level = m->sync & FB_SYNC_HOR_HIGH_ACT ?
2248 OMAPDSS_SIG_ACTIVE_HIGH :
2249 OMAPDSS_SIG_ACTIVE_LOW;
2250 t->vsync_level = m->sync & FB_SYNC_VERT_HIGH_ACT ?
2251 OMAPDSS_SIG_ACTIVE_HIGH :
2252 OMAPDSS_SIG_ACTIVE_LOW;
2253 t->interlace = m->vmode & FB_VMODE_INTERLACED;
2256 static int omapfb_find_best_mode(struct omap_dss_device *display,
2257 struct omap_video_timings *timings)
2259 struct fb_monspecs *specs;
2261 int r, i, best_xres, best_idx, len;
2263 if (!display->driver->read_edid)
2267 edid = kmalloc(len, GFP_KERNEL);
2269 r = display->driver->read_edid(display, edid, len);
2273 specs = kzalloc(sizeof(*specs), GFP_KERNEL);
2275 fb_edid_to_monspecs(edid, specs);
2278 fb_edid_add_monspecs(edid + 0x80, specs);
2283 for (i = 0; i < specs->modedb_len; ++i) {
2284 struct fb_videomode *m;
2285 struct omap_video_timings t;
2287 m = &specs->modedb[i];
2289 if (m->pixclock == 0)
2292 /* skip repeated pixel modes */
2293 if (m->xres == 2880 || m->xres == 1440)
2296 fb_videomode_to_omap_timings(m, display, &t);
2298 r = display->driver->check_timings(display, &t);
2299 if (r == 0 && best_xres < m->xres) {
2300 best_xres = m->xres;
2305 if (best_xres == 0) {
2310 fb_videomode_to_omap_timings(&specs->modedb[best_idx], display,
2316 fb_destroy_modedb(specs->modedb);
2324 static int omapfb_init_display(struct omapfb2_device *fbdev,
2325 struct omap_dss_device *dssdev)
2327 struct omap_dss_driver *dssdrv = dssdev->driver;
2328 struct omapfb_display_data *d;
2331 r = dssdrv->enable(dssdev);
2333 dev_warn(fbdev->dev, "Failed to enable display '%s'\n",
2338 d = get_display_data(fbdev, dssdev);
2342 if (dssdev->caps & OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE) {
2346 omapfb_start_auto_update(fbdev, dssdev);
2347 d->update_mode = OMAPFB_AUTO_UPDATE;
2349 d->update_mode = OMAPFB_MANUAL_UPDATE;
2352 if (dssdrv->enable_te) {
2353 r = dssdrv->enable_te(dssdev, 1);
2355 dev_err(fbdev->dev, "Failed to set TE\n");
2360 dssdrv->get_resolution(dssdev, &w, &h);
2361 r = dssdrv->update(dssdev, 0, 0, w, h);
2364 "Failed to update display\n");
2368 d->update_mode = OMAPFB_AUTO_UPDATE;
2374 static int __init omapfb_probe(struct platform_device *pdev)
2376 struct omapfb2_device *fbdev = NULL;
2379 struct omap_overlay *ovl;
2380 struct omap_dss_device *def_display;
2381 struct omap_dss_device *dssdev;
2383 DBG("omapfb_probe\n");
2385 if (pdev->num_resources != 0) {
2386 dev_err(&pdev->dev, "probed for an unknown device\n");
2391 fbdev = kzalloc(sizeof(struct omapfb2_device), GFP_KERNEL);
2392 if (fbdev == NULL) {
2397 /* TODO : Replace cpu check with omap_has_vrfb once HAS_FEATURE
2398 * available for OMAP2 and OMAP3
2400 if (def_vrfb && !cpu_is_omap24xx() && !cpu_is_omap34xx()) {
2402 dev_warn(&pdev->dev, "VRFB is not supported on this hardware, "
2403 "ignoring the module parameter vrfb=y\n");
2407 mutex_init(&fbdev->mtx);
2409 fbdev->dev = &pdev->dev;
2410 platform_set_drvdata(pdev, fbdev);
2413 fbdev->num_displays = 0;
2415 for_each_dss_dev(dssdev) {
2416 struct omapfb_display_data *d;
2418 omap_dss_get_device(dssdev);
2420 if (!dssdev->driver) {
2421 dev_warn(&pdev->dev, "no driver for display: %s\n",
2423 omap_dss_put_device(dssdev);
2427 d = &fbdev->displays[fbdev->num_displays++];
2429 if (dssdev->caps & OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE)
2430 d->update_mode = OMAPFB_MANUAL_UPDATE;
2432 d->update_mode = OMAPFB_AUTO_UPDATE;
2438 if (fbdev->num_displays == 0) {
2439 dev_err(&pdev->dev, "no displays\n");
2444 fbdev->num_overlays = omap_dss_get_num_overlays();
2445 for (i = 0; i < fbdev->num_overlays; i++)
2446 fbdev->overlays[i] = omap_dss_get_overlay(i);
2448 fbdev->num_managers = omap_dss_get_num_overlay_managers();
2449 for (i = 0; i < fbdev->num_managers; i++)
2450 fbdev->managers[i] = omap_dss_get_overlay_manager(i);
2452 /* gfx overlay should be the default one. find a display
2453 * connected to that, and use it as default display */
2454 ovl = omap_dss_get_overlay(0);
2455 if (ovl->manager && ovl->manager->device) {
2456 def_display = ovl->manager->device;
2458 dev_warn(&pdev->dev, "cannot find default display\n");
2462 if (def_mode && strlen(def_mode) > 0) {
2463 if (omapfb_parse_def_modes(fbdev))
2464 dev_warn(&pdev->dev, "cannot parse default modes\n");
2465 } else if (def_display && def_display->driver->set_timings &&
2466 def_display->driver->check_timings) {
2467 struct omap_video_timings t;
2469 r = omapfb_find_best_mode(def_display, &t);
2472 def_display->driver->set_timings(def_display, &t);
2475 r = omapfb_create_framebuffers(fbdev);
2479 for (i = 0; i < fbdev->num_managers; i++) {
2480 struct omap_overlay_manager *mgr;
2481 mgr = fbdev->managers[i];
2482 r = mgr->apply(mgr);
2484 dev_warn(fbdev->dev, "failed to apply dispc config\n");
2487 DBG("mgr->apply'ed\n");
2490 r = omapfb_init_display(fbdev, def_display);
2493 "failed to initialize default "
2499 DBG("create sysfs for fbs\n");
2500 r = omapfb_create_sysfs(fbdev);
2502 dev_err(fbdev->dev, "failed to create sysfs entries\n");
2509 omapfb_free_resources(fbdev);
2511 dev_err(&pdev->dev, "failed to setup omapfb\n");
2515 static int __exit omapfb_remove(struct platform_device *pdev)
2517 struct omapfb2_device *fbdev = platform_get_drvdata(pdev);
2519 /* FIXME: wait till completion of pending events */
2521 omapfb_remove_sysfs(fbdev);
2523 omapfb_free_resources(fbdev);
2528 static struct platform_driver omapfb_driver = {
2529 .remove = __exit_p(omapfb_remove),
2532 .owner = THIS_MODULE,
2536 static int __init omapfb_init(void)
2538 DBG("omapfb_init\n");
2540 if (platform_driver_probe(&omapfb_driver, omapfb_probe)) {
2541 printk(KERN_ERR "failed to register omapfb driver\n");
2548 static void __exit omapfb_exit(void)
2550 DBG("omapfb_exit\n");
2551 platform_driver_unregister(&omapfb_driver);
2554 module_param_named(mode, def_mode, charp, 0);
2555 module_param_named(vram, def_vram, charp, 0);
2556 module_param_named(rotate, def_rotate, int, 0);
2557 module_param_named(vrfb, def_vrfb, bool, 0);
2558 module_param_named(mirror, def_mirror, bool, 0);
2560 /* late_initcall to let panel/ctrl drivers loaded first.
2561 * I guess better option would be a more dynamic approach,
2562 * so that omapfb reacts to new panels when they are loaded */
2563 late_initcall(omapfb_init);
2564 /*module_init(omapfb_init);*/
2565 module_exit(omapfb_exit);
2567 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@nokia.com>");
2568 MODULE_DESCRIPTION("OMAP2/3 Framebuffer");
2569 MODULE_LICENSE("GPL v2");