1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/drivers/video/omap2/omapfb-main.c
5 * Copyright (C) 2008 Nokia Corporation
6 * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
8 * Some code and ideas taken from drivers/video/omap/ driver
12 #include <linux/module.h>
13 #include <linux/delay.h>
14 #include <linux/slab.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/vmalloc.h>
18 #include <linux/device.h>
19 #include <linux/platform_device.h>
20 #include <linux/omapfb.h>
22 #include <video/omapfb_dss.h>
23 #include <video/omapvrfb.h>
27 #define MODULE_NAME "omapfb"
29 #define OMAPFB_PLANE_XRES_MIN 8
30 #define OMAPFB_PLANE_YRES_MIN 8
32 static char *def_mode;
33 static char *def_vram;
35 static int def_rotate;
36 static bool def_mirror;
37 static bool auto_update;
38 static unsigned int auto_update_freq;
39 module_param(auto_update, bool, 0);
40 module_param(auto_update_freq, uint, 0644);
44 module_param_named(debug, omapfb_debug, bool, 0644);
45 static bool omapfb_test_pattern;
46 module_param_named(test, omapfb_test_pattern, bool, 0644);
49 static int omapfb_fb_init(struct omapfb2_device *fbdev, struct fb_info *fbi);
50 static int omapfb_get_recommended_bpp(struct omapfb2_device *fbdev,
51 struct omap_dss_device *dssdev);
54 static void draw_pixel(struct fb_info *fbi, int x, int y, unsigned color)
56 struct fb_var_screeninfo *var = &fbi->var;
57 struct fb_fix_screeninfo *fix = &fbi->fix;
58 void __iomem *addr = fbi->screen_base;
59 const unsigned bytespp = var->bits_per_pixel >> 3;
60 const unsigned line_len = fix->line_length / bytespp;
62 int r = (color >> 16) & 0xff;
63 int g = (color >> 8) & 0xff;
64 int b = (color >> 0) & 0xff;
66 if (var->bits_per_pixel == 16) {
67 u16 __iomem *p = (u16 __iomem *)addr;
68 p += y * line_len + x;
74 __raw_writew((r << 11) | (g << 5) | (b << 0), p);
75 } else if (var->bits_per_pixel == 24) {
76 u8 __iomem *p = (u8 __iomem *)addr;
77 p += (y * line_len + x) * 3;
79 __raw_writeb(b, p + 0);
80 __raw_writeb(g, p + 1);
81 __raw_writeb(r, p + 2);
82 } else if (var->bits_per_pixel == 32) {
83 u32 __iomem *p = (u32 __iomem *)addr;
84 p += y * line_len + x;
85 __raw_writel(color, p);
89 static void fill_fb(struct fb_info *fbi)
91 struct fb_var_screeninfo *var = &fbi->var;
92 const short w = var->xres_virtual;
93 const short h = var->yres_virtual;
94 void __iomem *addr = fbi->screen_base;
100 DBG("fill_fb %dx%d, line_len %d bytes\n", w, h, fbi->fix.line_length);
102 for (y = 0; y < h; y++) {
103 for (x = 0; x < w; x++) {
104 if (x < 20 && y < 20)
105 draw_pixel(fbi, x, y, 0xffffff);
106 else if (x < 20 && (y > 20 && y < h - 20))
107 draw_pixel(fbi, x, y, 0xff);
108 else if (y < 20 && (x > 20 && x < w - 20))
109 draw_pixel(fbi, x, y, 0xff00);
110 else if (x > w - 20 && (y > 20 && y < h - 20))
111 draw_pixel(fbi, x, y, 0xff0000);
112 else if (y > h - 20 && (x > 20 && x < w - 20))
113 draw_pixel(fbi, x, y, 0xffff00);
114 else if (x == 20 || x == w - 20 ||
115 y == 20 || y == h - 20)
116 draw_pixel(fbi, x, y, 0xffffff);
117 else if (x == y || w - x == h - y)
118 draw_pixel(fbi, x, y, 0xff00ff);
119 else if (w - x == y || x == h - y)
120 draw_pixel(fbi, x, y, 0x00ffff);
121 else if (x > 20 && y > 20 && x < w - 20 && y < h - 20) {
123 unsigned r = 0, g = 0, b = 0;
125 if (var->bits_per_pixel == 16) {
127 b = (y % 32) * 256 / 32;
129 g = (y % 64) * 256 / 64;
131 r = (y % 32) * 256 / 32;
140 c = (r << 16) | (g << 8) | (b << 0);
141 draw_pixel(fbi, x, y, c);
143 draw_pixel(fbi, x, y, 0);
150 static unsigned omapfb_get_vrfb_offset(const struct omapfb_info *ofbi, int rot)
152 const struct vrfb *vrfb = &ofbi->region->vrfb;
160 offset = vrfb->yoffset;
163 offset = vrfb->yoffset * OMAP_VRFB_LINE_LEN + vrfb->xoffset;
166 offset = vrfb->xoffset * OMAP_VRFB_LINE_LEN;
173 offset *= vrfb->bytespp;
178 static u32 omapfb_get_region_rot_paddr(const struct omapfb_info *ofbi, int rot)
180 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
181 return ofbi->region->vrfb.paddr[rot]
182 + omapfb_get_vrfb_offset(ofbi, rot);
184 return ofbi->region->paddr;
188 static u32 omapfb_get_region_paddr(const struct omapfb_info *ofbi)
190 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
191 return ofbi->region->vrfb.paddr[0];
193 return ofbi->region->paddr;
196 static void __iomem *omapfb_get_region_vaddr(const struct omapfb_info *ofbi)
198 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
199 return ofbi->region->vrfb.vaddr[0];
201 return ofbi->region->vaddr;
204 static struct omapfb_colormode omapfb_colormodes[] = {
206 .dssmode = OMAP_DSS_COLOR_UYVY,
207 .bits_per_pixel = 16,
208 .nonstd = OMAPFB_COLOR_YUV422,
210 .dssmode = OMAP_DSS_COLOR_YUV2,
211 .bits_per_pixel = 16,
212 .nonstd = OMAPFB_COLOR_YUY422,
214 .dssmode = OMAP_DSS_COLOR_ARGB16,
215 .bits_per_pixel = 16,
216 .red = { .length = 4, .offset = 8, .msb_right = 0 },
217 .green = { .length = 4, .offset = 4, .msb_right = 0 },
218 .blue = { .length = 4, .offset = 0, .msb_right = 0 },
219 .transp = { .length = 4, .offset = 12, .msb_right = 0 },
221 .dssmode = OMAP_DSS_COLOR_RGB16,
222 .bits_per_pixel = 16,
223 .red = { .length = 5, .offset = 11, .msb_right = 0 },
224 .green = { .length = 6, .offset = 5, .msb_right = 0 },
225 .blue = { .length = 5, .offset = 0, .msb_right = 0 },
226 .transp = { .length = 0, .offset = 0, .msb_right = 0 },
228 .dssmode = OMAP_DSS_COLOR_RGB24P,
229 .bits_per_pixel = 24,
230 .red = { .length = 8, .offset = 16, .msb_right = 0 },
231 .green = { .length = 8, .offset = 8, .msb_right = 0 },
232 .blue = { .length = 8, .offset = 0, .msb_right = 0 },
233 .transp = { .length = 0, .offset = 0, .msb_right = 0 },
235 .dssmode = OMAP_DSS_COLOR_RGB24U,
236 .bits_per_pixel = 32,
237 .red = { .length = 8, .offset = 16, .msb_right = 0 },
238 .green = { .length = 8, .offset = 8, .msb_right = 0 },
239 .blue = { .length = 8, .offset = 0, .msb_right = 0 },
240 .transp = { .length = 0, .offset = 0, .msb_right = 0 },
242 .dssmode = OMAP_DSS_COLOR_ARGB32,
243 .bits_per_pixel = 32,
244 .red = { .length = 8, .offset = 16, .msb_right = 0 },
245 .green = { .length = 8, .offset = 8, .msb_right = 0 },
246 .blue = { .length = 8, .offset = 0, .msb_right = 0 },
247 .transp = { .length = 8, .offset = 24, .msb_right = 0 },
249 .dssmode = OMAP_DSS_COLOR_RGBA32,
250 .bits_per_pixel = 32,
251 .red = { .length = 8, .offset = 24, .msb_right = 0 },
252 .green = { .length = 8, .offset = 16, .msb_right = 0 },
253 .blue = { .length = 8, .offset = 8, .msb_right = 0 },
254 .transp = { .length = 8, .offset = 0, .msb_right = 0 },
256 .dssmode = OMAP_DSS_COLOR_RGBX32,
257 .bits_per_pixel = 32,
258 .red = { .length = 8, .offset = 24, .msb_right = 0 },
259 .green = { .length = 8, .offset = 16, .msb_right = 0 },
260 .blue = { .length = 8, .offset = 8, .msb_right = 0 },
261 .transp = { .length = 0, .offset = 0, .msb_right = 0 },
265 static bool cmp_component(struct fb_bitfield *f1, struct fb_bitfield *f2)
267 return f1->length == f2->length &&
268 f1->offset == f2->offset &&
269 f1->msb_right == f2->msb_right;
272 static bool cmp_var_to_colormode(struct fb_var_screeninfo *var,
273 struct omapfb_colormode *color)
275 if (var->bits_per_pixel == 0 ||
276 var->red.length == 0 ||
277 var->blue.length == 0 ||
278 var->green.length == 0)
281 return var->bits_per_pixel == color->bits_per_pixel &&
282 cmp_component(&var->red, &color->red) &&
283 cmp_component(&var->green, &color->green) &&
284 cmp_component(&var->blue, &color->blue) &&
285 cmp_component(&var->transp, &color->transp);
288 static void assign_colormode_to_var(struct fb_var_screeninfo *var,
289 struct omapfb_colormode *color)
291 var->bits_per_pixel = color->bits_per_pixel;
292 var->nonstd = color->nonstd;
293 var->red = color->red;
294 var->green = color->green;
295 var->blue = color->blue;
296 var->transp = color->transp;
299 static int fb_mode_to_dss_mode(struct fb_var_screeninfo *var,
300 enum omap_color_mode *mode)
302 enum omap_color_mode dssmode;
305 /* first match with nonstd field */
307 for (i = 0; i < ARRAY_SIZE(omapfb_colormodes); ++i) {
308 struct omapfb_colormode *m = &omapfb_colormodes[i];
309 if (var->nonstd == m->nonstd) {
310 assign_colormode_to_var(var, m);
319 /* then try exact match of bpp and colors */
320 for (i = 0; i < ARRAY_SIZE(omapfb_colormodes); ++i) {
321 struct omapfb_colormode *m = &omapfb_colormodes[i];
322 if (cmp_var_to_colormode(var, m)) {
323 assign_colormode_to_var(var, m);
329 /* match with bpp if user has not filled color fields
331 switch (var->bits_per_pixel) {
333 dssmode = OMAP_DSS_COLOR_CLUT1;
336 dssmode = OMAP_DSS_COLOR_CLUT2;
339 dssmode = OMAP_DSS_COLOR_CLUT4;
342 dssmode = OMAP_DSS_COLOR_CLUT8;
345 dssmode = OMAP_DSS_COLOR_RGB12U;
348 dssmode = OMAP_DSS_COLOR_RGB16;
351 dssmode = OMAP_DSS_COLOR_RGB24P;
354 dssmode = OMAP_DSS_COLOR_RGB24U;
360 for (i = 0; i < ARRAY_SIZE(omapfb_colormodes); ++i) {
361 struct omapfb_colormode *m = &omapfb_colormodes[i];
362 if (dssmode == m->dssmode) {
363 assign_colormode_to_var(var, m);
372 static int check_fb_res_bounds(struct fb_var_screeninfo *var)
374 int xres_min = OMAPFB_PLANE_XRES_MIN;
376 int yres_min = OMAPFB_PLANE_YRES_MIN;
379 /* XXX: some applications seem to set virtual res to 0. */
380 if (var->xres_virtual == 0)
381 var->xres_virtual = var->xres;
383 if (var->yres_virtual == 0)
384 var->yres_virtual = var->yres;
386 if (var->xres_virtual < xres_min || var->yres_virtual < yres_min)
389 if (var->xres < xres_min)
390 var->xres = xres_min;
391 if (var->yres < yres_min)
392 var->yres = yres_min;
393 if (var->xres > xres_max)
394 var->xres = xres_max;
395 if (var->yres > yres_max)
396 var->yres = yres_max;
398 if (var->xres > var->xres_virtual)
399 var->xres = var->xres_virtual;
400 if (var->yres > var->yres_virtual)
401 var->yres = var->yres_virtual;
406 static void shrink_height(unsigned long max_frame_size,
407 struct fb_var_screeninfo *var)
409 DBG("can't fit FB into memory, reducing y\n");
410 var->yres_virtual = max_frame_size /
411 (var->xres_virtual * var->bits_per_pixel >> 3);
413 if (var->yres_virtual < OMAPFB_PLANE_YRES_MIN)
414 var->yres_virtual = OMAPFB_PLANE_YRES_MIN;
416 if (var->yres > var->yres_virtual)
417 var->yres = var->yres_virtual;
420 static void shrink_width(unsigned long max_frame_size,
421 struct fb_var_screeninfo *var)
423 DBG("can't fit FB into memory, reducing x\n");
424 var->xres_virtual = max_frame_size / var->yres_virtual /
425 (var->bits_per_pixel >> 3);
427 if (var->xres_virtual < OMAPFB_PLANE_XRES_MIN)
428 var->xres_virtual = OMAPFB_PLANE_XRES_MIN;
430 if (var->xres > var->xres_virtual)
431 var->xres = var->xres_virtual;
434 static int check_vrfb_fb_size(unsigned long region_size,
435 const struct fb_var_screeninfo *var)
437 unsigned long min_phys_size = omap_vrfb_min_phys_size(var->xres_virtual,
438 var->yres_virtual, var->bits_per_pixel >> 3);
440 return min_phys_size > region_size ? -EINVAL : 0;
443 static int check_fb_size(const struct omapfb_info *ofbi,
444 struct fb_var_screeninfo *var)
446 unsigned long max_frame_size = ofbi->region->size;
447 int bytespp = var->bits_per_pixel >> 3;
448 unsigned long line_size = var->xres_virtual * bytespp;
450 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
451 /* One needs to check for both VRFB and OMAPFB limitations. */
452 if (check_vrfb_fb_size(max_frame_size, var))
453 shrink_height(omap_vrfb_max_height(
454 max_frame_size, var->xres_virtual, bytespp) *
457 if (check_vrfb_fb_size(max_frame_size, var)) {
458 DBG("cannot fit FB to memory\n");
465 DBG("max frame size %lu, line size %lu\n", max_frame_size, line_size);
467 if (line_size * var->yres_virtual > max_frame_size)
468 shrink_height(max_frame_size, var);
470 if (line_size * var->yres_virtual > max_frame_size) {
471 shrink_width(max_frame_size, var);
472 line_size = var->xres_virtual * bytespp;
475 if (line_size * var->yres_virtual > max_frame_size) {
476 DBG("cannot fit FB to memory\n");
484 * Consider if VRFB assisted rotation is in use and if the virtual space for
485 * the zero degree view needs to be mapped. The need for mapping also acts as
486 * the trigger for setting up the hardware on the context in question. This
487 * ensures that one does not attempt to access the virtual view before the
488 * hardware is serving the address translations.
490 static int setup_vrfb_rotation(struct fb_info *fbi)
492 struct omapfb_info *ofbi = FB2OFB(fbi);
493 struct omapfb2_mem_region *rg = ofbi->region;
494 struct vrfb *vrfb = &rg->vrfb;
495 struct fb_var_screeninfo *var = &fbi->var;
496 struct fb_fix_screeninfo *fix = &fbi->fix;
499 enum omap_color_mode mode;
503 if (!rg->size || ofbi->rotation_type != OMAP_DSS_ROT_VRFB)
506 DBG("setup_vrfb_rotation\n");
508 r = fb_mode_to_dss_mode(var, &mode);
512 bytespp = var->bits_per_pixel >> 3;
514 yuv_mode = mode == OMAP_DSS_COLOR_YUV2 || mode == OMAP_DSS_COLOR_UYVY;
516 /* We need to reconfigure VRFB if the resolution changes, if yuv mode
517 * is enabled/disabled, or if bytes per pixel changes */
519 /* XXX we shouldn't allow this when framebuffer is mmapped */
523 if (yuv_mode != vrfb->yuv_mode)
525 else if (bytespp != vrfb->bytespp)
527 else if (vrfb->xres != var->xres_virtual ||
528 vrfb->yres != var->yres_virtual)
531 if (vrfb->vaddr[0] && reconf) {
532 fbi->screen_base = NULL;
535 iounmap(vrfb->vaddr[0]);
536 vrfb->vaddr[0] = NULL;
537 DBG("setup_vrfb_rotation: reset fb\n");
543 omap_vrfb_setup(&rg->vrfb, rg->paddr,
548 /* Now one can ioremap the 0 angle view */
549 r = omap_vrfb_map_angle(vrfb, var->yres_virtual, 0);
553 /* used by open/write in fbmem.c */
554 fbi->screen_base = ofbi->region->vrfb.vaddr[0];
556 fix->smem_start = ofbi->region->vrfb.paddr[0];
558 switch (var->nonstd) {
559 case OMAPFB_COLOR_YUV422:
560 case OMAPFB_COLOR_YUY422:
562 (OMAP_VRFB_LINE_LEN * var->bits_per_pixel) >> 2;
566 (OMAP_VRFB_LINE_LEN * var->bits_per_pixel) >> 3;
570 fix->smem_len = var->yres_virtual * fix->line_length;
575 int dss_mode_to_fb_mode(enum omap_color_mode dssmode,
576 struct fb_var_screeninfo *var)
580 for (i = 0; i < ARRAY_SIZE(omapfb_colormodes); ++i) {
581 struct omapfb_colormode *mode = &omapfb_colormodes[i];
582 if (dssmode == mode->dssmode) {
583 assign_colormode_to_var(var, mode);
590 void set_fb_fix(struct fb_info *fbi)
592 struct fb_fix_screeninfo *fix = &fbi->fix;
593 struct fb_var_screeninfo *var = &fbi->var;
594 struct omapfb_info *ofbi = FB2OFB(fbi);
595 struct omapfb2_mem_region *rg = ofbi->region;
599 /* used by open/write in fbmem.c */
600 fbi->screen_base = (char __iomem *)omapfb_get_region_vaddr(ofbi);
602 /* used by mmap in fbmem.c */
603 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
604 switch (var->nonstd) {
605 case OMAPFB_COLOR_YUV422:
606 case OMAPFB_COLOR_YUY422:
608 (OMAP_VRFB_LINE_LEN * var->bits_per_pixel) >> 2;
612 (OMAP_VRFB_LINE_LEN * var->bits_per_pixel) >> 3;
616 fix->smem_len = var->yres_virtual * fix->line_length;
619 (var->xres_virtual * var->bits_per_pixel) >> 3;
620 fix->smem_len = rg->size;
623 fix->smem_start = omapfb_get_region_paddr(ofbi);
625 fix->type = FB_TYPE_PACKED_PIXELS;
628 fix->visual = FB_VISUAL_PSEUDOCOLOR;
630 switch (var->bits_per_pixel) {
635 fix->visual = FB_VISUAL_TRUECOLOR;
636 /* 12bpp is stored in 16 bits */
642 fix->visual = FB_VISUAL_PSEUDOCOLOR;
647 fix->accel = FB_ACCEL_NONE;
653 /* check new var and possibly modify it to be ok */
654 int check_fb_var(struct fb_info *fbi, struct fb_var_screeninfo *var)
656 struct omapfb_info *ofbi = FB2OFB(fbi);
657 struct omap_dss_device *display = fb2display(fbi);
658 enum omap_color_mode mode = 0;
662 DBG("check_fb_var %d\n", ofbi->id);
664 WARN_ON(!atomic_read(&ofbi->region->lock_count));
666 r = fb_mode_to_dss_mode(var, &mode);
668 DBG("cannot convert var to omap dss mode\n");
672 for (i = 0; i < ofbi->num_overlays; ++i) {
673 if ((ofbi->overlays[i]->supported_modes & mode) == 0) {
674 DBG("invalid mode\n");
682 if (check_fb_res_bounds(var))
685 /* When no memory is allocated ignore the size check */
686 if (ofbi->region->size != 0 && check_fb_size(ofbi, var))
689 if (var->xres + var->xoffset > var->xres_virtual)
690 var->xoffset = var->xres_virtual - var->xres;
691 if (var->yres + var->yoffset > var->yres_virtual)
692 var->yoffset = var->yres_virtual - var->yres;
694 DBG("xres = %d, yres = %d, vxres = %d, vyres = %d\n",
695 var->xres, var->yres,
696 var->xres_virtual, var->yres_virtual);
698 if (display && display->driver->get_dimensions) {
700 display->driver->get_dimensions(display, &w, &h);
701 var->width = DIV_ROUND_CLOSEST(w, 1000);
702 var->height = DIV_ROUND_CLOSEST(h, 1000);
710 if (display && display->driver->get_timings) {
711 struct omap_video_timings timings;
712 display->driver->get_timings(display, &timings);
714 /* pixclock in ps, the rest in pixclock */
715 var->pixclock = timings.pixelclock != 0 ?
716 KHZ2PICOS(timings.pixelclock / 1000) :
718 var->left_margin = timings.hbp;
719 var->right_margin = timings.hfp;
720 var->upper_margin = timings.vbp;
721 var->lower_margin = timings.vfp;
722 var->hsync_len = timings.hsw;
723 var->vsync_len = timings.vsw;
724 var->sync |= timings.hsync_level == OMAPDSS_SIG_ACTIVE_HIGH ?
725 FB_SYNC_HOR_HIGH_ACT : 0;
726 var->sync |= timings.vsync_level == OMAPDSS_SIG_ACTIVE_HIGH ?
727 FB_SYNC_VERT_HIGH_ACT : 0;
728 var->vmode = timings.interlace ?
729 FB_VMODE_INTERLACED : FB_VMODE_NONINTERLACED;
732 var->left_margin = 0;
733 var->right_margin = 0;
734 var->upper_margin = 0;
735 var->lower_margin = 0;
739 var->vmode = FB_VMODE_NONINTERLACED;
746 * ---------------------------------------------------------------------------
747 * fbdev framework callbacks
748 * ---------------------------------------------------------------------------
750 static int omapfb_open(struct fb_info *fbi, int user)
755 static int omapfb_release(struct fb_info *fbi, int user)
760 static unsigned calc_rotation_offset_dma(const struct fb_var_screeninfo *var,
761 const struct fb_fix_screeninfo *fix, int rotation)
765 offset = var->yoffset * fix->line_length +
766 var->xoffset * (var->bits_per_pixel >> 3);
771 static unsigned calc_rotation_offset_vrfb(const struct fb_var_screeninfo *var,
772 const struct fb_fix_screeninfo *fix, int rotation)
776 if (rotation == FB_ROTATE_UD)
777 offset = (var->yres_virtual - var->yres) *
779 else if (rotation == FB_ROTATE_CW)
780 offset = (var->yres_virtual - var->yres) *
781 (var->bits_per_pixel >> 3);
785 if (rotation == FB_ROTATE_UR)
786 offset += var->yoffset * fix->line_length +
787 var->xoffset * (var->bits_per_pixel >> 3);
788 else if (rotation == FB_ROTATE_UD)
789 offset -= var->yoffset * fix->line_length +
790 var->xoffset * (var->bits_per_pixel >> 3);
791 else if (rotation == FB_ROTATE_CW)
792 offset -= var->xoffset * fix->line_length +
793 var->yoffset * (var->bits_per_pixel >> 3);
794 else if (rotation == FB_ROTATE_CCW)
795 offset += var->xoffset * fix->line_length +
796 var->yoffset * (var->bits_per_pixel >> 3);
801 static void omapfb_calc_addr(const struct omapfb_info *ofbi,
802 const struct fb_var_screeninfo *var,
803 const struct fb_fix_screeninfo *fix,
804 int rotation, u32 *paddr)
809 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
810 data_start_p = omapfb_get_region_rot_paddr(ofbi, rotation);
812 data_start_p = omapfb_get_region_paddr(ofbi);
814 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
815 offset = calc_rotation_offset_vrfb(var, fix, rotation);
817 offset = calc_rotation_offset_dma(var, fix, rotation);
819 data_start_p += offset;
822 DBG("offset %d, %d = %d\n",
823 var->xoffset, var->yoffset, offset);
825 DBG("paddr %x\n", data_start_p);
827 *paddr = data_start_p;
830 /* setup overlay according to the fb */
831 int omapfb_setup_overlay(struct fb_info *fbi, struct omap_overlay *ovl,
832 u16 posx, u16 posy, u16 outw, u16 outh)
835 struct omapfb_info *ofbi = FB2OFB(fbi);
836 struct fb_var_screeninfo *var = &fbi->var;
837 struct fb_fix_screeninfo *fix = &fbi->fix;
838 enum omap_color_mode mode = 0;
839 u32 data_start_p = 0;
840 struct omap_overlay_info info;
844 int rotation = var->rotate;
847 WARN_ON(!atomic_read(&ofbi->region->lock_count));
849 for (i = 0; i < ofbi->num_overlays; i++) {
850 if (ovl != ofbi->overlays[i])
853 rotation = (rotation + ofbi->rotation[i]) % 4;
857 DBG("setup_overlay %d, posx %d, posy %d, outw %d, outh %d\n", ofbi->id,
858 posx, posy, outw, outh);
860 if (rotation == FB_ROTATE_CW || rotation == FB_ROTATE_CCW) {
868 if (ofbi->region->size)
869 omapfb_calc_addr(ofbi, var, fix, rotation, &data_start_p);
871 r = fb_mode_to_dss_mode(var, &mode);
873 DBG("fb_mode_to_dss_mode failed");
877 switch (var->nonstd) {
878 case OMAPFB_COLOR_YUV422:
879 case OMAPFB_COLOR_YUY422:
880 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
881 screen_width = fix->line_length
882 / (var->bits_per_pixel >> 2);
887 screen_width = fix->line_length / (var->bits_per_pixel >> 3);
891 ovl->get_overlay_info(ovl, &info);
893 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
896 mirror = ofbi->mirror;
898 info.paddr = data_start_p;
899 info.screen_width = screen_width;
902 info.color_mode = mode;
903 info.rotation_type = ofbi->rotation_type;
904 info.rotation = rotation;
905 info.mirror = mirror;
909 info.out_width = outw;
910 info.out_height = outh;
912 r = ovl->set_overlay_info(ovl, &info);
914 DBG("ovl->setup_overlay_info failed\n");
921 DBG("setup_overlay failed\n");
925 /* apply var to the overlay */
926 int omapfb_apply_changes(struct fb_info *fbi, int init)
929 struct omapfb_info *ofbi = FB2OFB(fbi);
930 struct fb_var_screeninfo *var = &fbi->var;
931 struct omap_overlay *ovl;
937 if (omapfb_test_pattern)
941 WARN_ON(!atomic_read(&ofbi->region->lock_count));
943 for (i = 0; i < ofbi->num_overlays; i++) {
944 ovl = ofbi->overlays[i];
946 DBG("apply_changes, fb %d, ovl %d\n", ofbi->id, ovl->id);
948 if (ofbi->region->size == 0) {
949 /* the fb is not available. disable the overlay */
950 omapfb_overlay_enable(ovl, 0);
951 if (!init && ovl->manager)
952 ovl->manager->apply(ovl->manager);
956 if (init || (ovl->caps & OMAP_DSS_OVL_CAP_SCALE) == 0) {
957 int rotation = (var->rotate + ofbi->rotation[i]) % 4;
958 if (rotation == FB_ROTATE_CW ||
959 rotation == FB_ROTATE_CCW) {
967 struct omap_overlay_info info;
968 ovl->get_overlay_info(ovl, &info);
969 outw = info.out_width;
970 outh = info.out_height;
977 struct omap_overlay_info info;
978 ovl->get_overlay_info(ovl, &info);
983 r = omapfb_setup_overlay(fbi, ovl, posx, posy, outw, outh);
987 if (!init && ovl->manager)
988 ovl->manager->apply(ovl->manager);
992 DBG("apply_changes failed\n");
996 /* checks var and eventually tweaks it to something supported,
997 * DO NOT MODIFY PAR */
998 static int omapfb_check_var(struct fb_var_screeninfo *var, struct fb_info *fbi)
1000 struct omapfb_info *ofbi = FB2OFB(fbi);
1003 DBG("check_var(%d)\n", FB2OFB(fbi)->id);
1005 omapfb_get_mem_region(ofbi->region);
1007 r = check_fb_var(fbi, var);
1009 omapfb_put_mem_region(ofbi->region);
1014 /* set the video mode according to info->var */
1015 static int omapfb_set_par(struct fb_info *fbi)
1017 struct omapfb_info *ofbi = FB2OFB(fbi);
1020 DBG("set_par(%d)\n", FB2OFB(fbi)->id);
1022 omapfb_get_mem_region(ofbi->region);
1026 r = setup_vrfb_rotation(fbi);
1030 r = omapfb_apply_changes(fbi, 0);
1033 omapfb_put_mem_region(ofbi->region);
1038 static int omapfb_pan_display(struct fb_var_screeninfo *var,
1039 struct fb_info *fbi)
1041 struct omapfb_info *ofbi = FB2OFB(fbi);
1042 struct fb_var_screeninfo new_var;
1045 DBG("pan_display(%d)\n", FB2OFB(fbi)->id);
1047 if (var->xoffset == fbi->var.xoffset &&
1048 var->yoffset == fbi->var.yoffset)
1052 new_var.xoffset = var->xoffset;
1053 new_var.yoffset = var->yoffset;
1057 omapfb_get_mem_region(ofbi->region);
1059 r = omapfb_apply_changes(fbi, 0);
1061 omapfb_put_mem_region(ofbi->region);
1066 static void mmap_user_open(struct vm_area_struct *vma)
1068 struct omapfb2_mem_region *rg = vma->vm_private_data;
1070 omapfb_get_mem_region(rg);
1071 atomic_inc(&rg->map_count);
1072 omapfb_put_mem_region(rg);
1075 static void mmap_user_close(struct vm_area_struct *vma)
1077 struct omapfb2_mem_region *rg = vma->vm_private_data;
1079 omapfb_get_mem_region(rg);
1080 atomic_dec(&rg->map_count);
1081 omapfb_put_mem_region(rg);
1084 static const struct vm_operations_struct mmap_user_ops = {
1085 .open = mmap_user_open,
1086 .close = mmap_user_close,
1089 static int omapfb_mmap(struct fb_info *fbi, struct vm_area_struct *vma)
1091 struct omapfb_info *ofbi = FB2OFB(fbi);
1092 struct fb_fix_screeninfo *fix = &fbi->fix;
1093 struct omapfb2_mem_region *rg;
1094 unsigned long start;
1098 rg = omapfb_get_mem_region(ofbi->region);
1100 start = omapfb_get_region_paddr(ofbi);
1101 len = fix->smem_len;
1103 DBG("user mmap region start %lx, len %d, off %lx\n", start, len,
1104 vma->vm_pgoff << PAGE_SHIFT);
1106 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
1107 vma->vm_ops = &mmap_user_ops;
1108 vma->vm_private_data = rg;
1110 r = vm_iomap_memory(vma, start, len);
1114 /* vm_ops.open won't be called for mmap itself. */
1115 atomic_inc(&rg->map_count);
1117 omapfb_put_mem_region(rg);
1122 omapfb_put_mem_region(ofbi->region);
1127 /* Store a single color palette entry into a pseudo palette or the hardware
1128 * palette if one is available. For now we support only 16bpp and thus store
1129 * the entry only to the pseudo palette.
1131 static int _setcolreg(struct fb_info *fbi, u_int regno, u_int red, u_int green,
1132 u_int blue, u_int transp, int update_hw_pal)
1134 /*struct omapfb_info *ofbi = FB2OFB(fbi);*/
1135 /*struct omapfb2_device *fbdev = ofbi->fbdev;*/
1136 struct fb_var_screeninfo *var = &fbi->var;
1139 enum omapfb_color_format mode = OMAPFB_COLOR_RGB24U; /* XXX */
1141 /*switch (plane->color_mode) {*/
1143 case OMAPFB_COLOR_YUV422:
1144 case OMAPFB_COLOR_YUV420:
1145 case OMAPFB_COLOR_YUY422:
1148 case OMAPFB_COLOR_CLUT_8BPP:
1149 case OMAPFB_COLOR_CLUT_4BPP:
1150 case OMAPFB_COLOR_CLUT_2BPP:
1151 case OMAPFB_COLOR_CLUT_1BPP:
1153 if (fbdev->ctrl->setcolreg)
1154 r = fbdev->ctrl->setcolreg(regno, red, green, blue,
1155 transp, update_hw_pal);
1159 case OMAPFB_COLOR_RGB565:
1160 case OMAPFB_COLOR_RGB444:
1161 case OMAPFB_COLOR_RGB24P:
1162 case OMAPFB_COLOR_RGB24U:
1165 pal = ((red >> (16 - var->red.length)) <<
1167 ((green >> (16 - var->green.length)) <<
1168 var->green.offset) |
1169 (blue >> (16 - var->blue.length));
1170 ((u32 *)(fbi->pseudo_palette))[regno] = pal;
1179 static int omapfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
1180 u_int transp, struct fb_info *info)
1184 return _setcolreg(info, regno, red, green, blue, transp, 1);
1187 static int omapfb_setcmap(struct fb_cmap *cmap, struct fb_info *info)
1189 int count, index, r;
1190 u16 *red, *green, *blue, *transp;
1196 green = cmap->green;
1198 transp = cmap->transp;
1199 index = cmap->start;
1201 for (count = 0; count < cmap->len; count++) {
1204 r = _setcolreg(info, index++, *red++, *green++, *blue++, trans,
1205 count == cmap->len - 1);
1213 static int omapfb_blank(int blank, struct fb_info *fbi)
1215 struct omapfb_info *ofbi = FB2OFB(fbi);
1216 struct omapfb2_device *fbdev = ofbi->fbdev;
1217 struct omap_dss_device *display = fb2display(fbi);
1218 struct omapfb_display_data *d;
1226 d = get_display_data(fbdev, display);
1229 case FB_BLANK_UNBLANK:
1230 if (display->state == OMAP_DSS_DISPLAY_ACTIVE)
1233 r = display->driver->enable(display);
1235 if ((display->caps & OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE) &&
1236 d->update_mode == OMAPFB_AUTO_UPDATE &&
1237 !d->auto_update_work_enabled)
1238 omapfb_start_auto_update(fbdev, display);
1242 case FB_BLANK_NORMAL:
1243 /* FB_BLANK_NORMAL could be implemented.
1244 * Needs DSS additions. */
1245 case FB_BLANK_VSYNC_SUSPEND:
1246 case FB_BLANK_HSYNC_SUSPEND:
1247 case FB_BLANK_POWERDOWN:
1248 if (display->state != OMAP_DSS_DISPLAY_ACTIVE)
1251 if (d->auto_update_work_enabled)
1252 omapfb_stop_auto_update(fbdev, display);
1254 display->driver->disable(display);
1263 omapfb_unlock(fbdev);
1269 /* XXX fb_read and fb_write are needed for VRFB */
1270 ssize_t omapfb_write(struct fb_info *info, const char __user *buf,
1271 size_t count, loff_t *ppos)
1273 DBG("omapfb_write %d, %lu\n", count, (unsigned long)*ppos);
1274 /* XXX needed for VRFB */
1279 static const struct fb_ops omapfb_ops = {
1280 .owner = THIS_MODULE,
1281 .fb_open = omapfb_open,
1282 .fb_release = omapfb_release,
1283 .fb_fillrect = cfb_fillrect,
1284 .fb_copyarea = cfb_copyarea,
1285 .fb_imageblit = cfb_imageblit,
1286 .fb_blank = omapfb_blank,
1287 .fb_ioctl = omapfb_ioctl,
1288 .fb_check_var = omapfb_check_var,
1289 .fb_set_par = omapfb_set_par,
1290 .fb_pan_display = omapfb_pan_display,
1291 .fb_mmap = omapfb_mmap,
1292 .fb_setcolreg = omapfb_setcolreg,
1293 .fb_setcmap = omapfb_setcmap,
1294 /*.fb_write = omapfb_write,*/
1297 static void omapfb_free_fbmem(struct fb_info *fbi)
1299 struct omapfb_info *ofbi = FB2OFB(fbi);
1300 struct omapfb2_device *fbdev = ofbi->fbdev;
1301 struct omapfb2_mem_region *rg;
1305 if (rg->token == NULL)
1308 WARN_ON(atomic_read(&rg->map_count));
1310 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
1311 /* unmap the 0 angle rotation */
1312 if (rg->vrfb.vaddr[0]) {
1313 iounmap(rg->vrfb.vaddr[0]);
1314 rg->vrfb.vaddr[0] = NULL;
1317 omap_vrfb_release_ctx(&rg->vrfb);
1320 dma_free_attrs(fbdev->dev, rg->size, rg->token, rg->dma_handle,
1330 static void clear_fb_info(struct fb_info *fbi)
1332 memset(&fbi->var, 0, sizeof(fbi->var));
1333 memset(&fbi->fix, 0, sizeof(fbi->fix));
1334 strscpy(fbi->fix.id, MODULE_NAME, sizeof(fbi->fix.id));
1337 static int omapfb_free_all_fbmem(struct omapfb2_device *fbdev)
1341 DBG("free all fbmem\n");
1343 for (i = 0; i < fbdev->num_fbs; i++) {
1344 struct fb_info *fbi = fbdev->fbs[i];
1345 omapfb_free_fbmem(fbi);
1352 static int omapfb_alloc_fbmem(struct fb_info *fbi, unsigned long size,
1353 unsigned long paddr)
1355 struct omapfb_info *ofbi = FB2OFB(fbi);
1356 struct omapfb2_device *fbdev = ofbi->fbdev;
1357 struct omapfb2_mem_region *rg;
1359 unsigned long attrs;
1360 dma_addr_t dma_handle;
1367 memset(&rg->vrfb, 0, sizeof rg->vrfb);
1373 size = PAGE_ALIGN(size);
1375 attrs = DMA_ATTR_WRITE_COMBINE;
1377 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB)
1378 attrs |= DMA_ATTR_NO_KERNEL_MAPPING;
1380 DBG("allocating %lu bytes for fb %d\n", size, ofbi->id);
1382 token = dma_alloc_attrs(fbdev->dev, size, &dma_handle,
1385 if (token == NULL) {
1386 dev_err(fbdev->dev, "failed to allocate framebuffer\n");
1390 DBG("allocated VRAM paddr %lx, vaddr %p\n",
1391 (unsigned long)dma_handle, token);
1393 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
1394 r = omap_vrfb_request_ctx(&rg->vrfb);
1396 dma_free_attrs(fbdev->dev, size, token, dma_handle,
1398 dev_err(fbdev->dev, "vrfb create ctx failed\n");
1405 rg->dma_handle = dma_handle;
1407 rg->paddr = (unsigned long)dma_handle;
1408 rg->vaddr = (void __iomem *)token;
1415 /* allocate fbmem using display resolution as reference */
1416 static int omapfb_alloc_fbmem_display(struct fb_info *fbi, unsigned long size,
1417 unsigned long paddr)
1419 struct omapfb_info *ofbi = FB2OFB(fbi);
1420 struct omapfb2_device *fbdev = ofbi->fbdev;
1421 struct omap_dss_device *display;
1424 display = fb2display(fbi);
1429 switch (omapfb_get_recommended_bpp(fbdev, display)) {
1444 display->driver->get_resolution(display, &w, &h);
1446 if (ofbi->rotation_type == OMAP_DSS_ROT_VRFB) {
1447 size = max(omap_vrfb_min_phys_size(w, h, bytespp),
1448 omap_vrfb_min_phys_size(h, w, bytespp));
1450 DBG("adjusting fb mem size for VRFB, %u -> %lu\n",
1451 w * h * bytespp, size);
1453 size = w * h * bytespp;
1460 return omapfb_alloc_fbmem(fbi, size, paddr);
1463 static int omapfb_parse_vram_param(const char *param, int max_entries,
1464 unsigned long *sizes, unsigned long *paddrs)
1468 unsigned long paddr = 0;
1471 start = (char *)param;
1476 fbnum = simple_strtoul(p, &p, 10);
1484 if (fbnum >= max_entries)
1487 size = memparse(p + 1, &p);
1495 paddr = simple_strtoul(p + 1, &p, 16);
1503 "reserving memory at predefined address not supported\n");
1505 paddrs[fbnum] = paddr;
1506 sizes[fbnum] = size;
1522 static int omapfb_allocate_all_fbs(struct omapfb2_device *fbdev)
1525 unsigned long vram_sizes[10];
1526 unsigned long vram_paddrs[10];
1528 memset(&vram_sizes, 0, sizeof(vram_sizes));
1529 memset(&vram_paddrs, 0, sizeof(vram_paddrs));
1531 if (def_vram && omapfb_parse_vram_param(def_vram, 10,
1532 vram_sizes, vram_paddrs)) {
1533 dev_err(fbdev->dev, "failed to parse vram parameter\n");
1535 memset(&vram_sizes, 0, sizeof(vram_sizes));
1536 memset(&vram_paddrs, 0, sizeof(vram_paddrs));
1539 for (i = 0; i < fbdev->num_fbs; i++) {
1540 /* allocate memory automatically only for fb0, or if
1541 * excplicitly defined with vram or plat data option */
1542 if (i == 0 || vram_sizes[i] != 0) {
1543 r = omapfb_alloc_fbmem_display(fbdev->fbs[i],
1544 vram_sizes[i], vram_paddrs[i]);
1551 for (i = 0; i < fbdev->num_fbs; i++) {
1552 struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[i]);
1553 struct omapfb2_mem_region *rg;
1556 DBG("region%d phys %08x virt %p size=%lu\n",
1566 static void omapfb_clear_fb(struct fb_info *fbi)
1568 const struct fb_fillrect rect = {
1571 .width = fbi->var.xres_virtual,
1572 .height = fbi->var.yres_virtual,
1577 cfb_fillrect(fbi, &rect);
1580 int omapfb_realloc_fbmem(struct fb_info *fbi, unsigned long size, int type)
1582 struct omapfb_info *ofbi = FB2OFB(fbi);
1583 struct omapfb2_device *fbdev = ofbi->fbdev;
1584 struct omapfb2_mem_region *rg = ofbi->region;
1585 unsigned long old_size = rg->size;
1586 unsigned long old_paddr = rg->paddr;
1587 int old_type = rg->type;
1590 if (type != OMAPFB_MEMTYPE_SDRAM)
1593 size = PAGE_ALIGN(size);
1595 if (old_size == size && old_type == type)
1598 omapfb_free_fbmem(fbi);
1605 r = omapfb_alloc_fbmem(fbi, size, 0);
1609 omapfb_alloc_fbmem(fbi, old_size, old_paddr);
1617 if (old_size == size)
1620 if (old_size == 0) {
1621 DBG("initializing fb %d\n", ofbi->id);
1622 r = omapfb_fb_init(fbdev, fbi);
1624 DBG("omapfb_fb_init failed\n");
1627 r = omapfb_apply_changes(fbi, 1);
1629 DBG("omapfb_apply_changes failed\n");
1633 struct fb_var_screeninfo new_var;
1634 memcpy(&new_var, &fbi->var, sizeof(new_var));
1635 r = check_fb_var(fbi, &new_var);
1638 memcpy(&fbi->var, &new_var, sizeof(fbi->var));
1640 r = setup_vrfb_rotation(fbi);
1645 omapfb_clear_fb(fbi);
1649 omapfb_free_fbmem(fbi);
1654 static void omapfb_auto_update_work(struct work_struct *work)
1656 struct omap_dss_device *dssdev;
1657 struct omap_dss_driver *dssdrv;
1658 struct omapfb_display_data *d;
1661 struct omapfb2_device *fbdev;
1663 d = container_of(work, struct omapfb_display_data,
1664 auto_update_work.work);
1667 dssdrv = dssdev->driver;
1670 if (!dssdrv || !dssdrv->update)
1674 dssdrv->sync(dssdev);
1676 dssdrv->get_resolution(dssdev, &w, &h);
1677 dssdrv->update(dssdev, 0, 0, w, h);
1679 freq = auto_update_freq;
1682 queue_delayed_work(fbdev->auto_update_wq,
1683 &d->auto_update_work, HZ / freq);
1686 void omapfb_start_auto_update(struct omapfb2_device *fbdev,
1687 struct omap_dss_device *display)
1689 struct omapfb_display_data *d;
1691 if (fbdev->auto_update_wq == NULL) {
1692 struct workqueue_struct *wq;
1694 wq = create_singlethread_workqueue("omapfb_auto_update");
1697 dev_err(fbdev->dev, "Failed to create workqueue for "
1702 fbdev->auto_update_wq = wq;
1705 d = get_display_data(fbdev, display);
1707 INIT_DELAYED_WORK(&d->auto_update_work, omapfb_auto_update_work);
1709 d->auto_update_work_enabled = true;
1711 omapfb_auto_update_work(&d->auto_update_work.work);
1714 void omapfb_stop_auto_update(struct omapfb2_device *fbdev,
1715 struct omap_dss_device *display)
1717 struct omapfb_display_data *d;
1719 d = get_display_data(fbdev, display);
1721 cancel_delayed_work_sync(&d->auto_update_work);
1723 d->auto_update_work_enabled = false;
1726 /* initialize fb_info, var, fix to something sane based on the display */
1727 static int omapfb_fb_init(struct omapfb2_device *fbdev, struct fb_info *fbi)
1729 struct fb_var_screeninfo *var = &fbi->var;
1730 struct omap_dss_device *display = fb2display(fbi);
1731 struct omapfb_info *ofbi = FB2OFB(fbi);
1734 fbi->fbops = &omapfb_ops;
1735 fbi->flags = FBINFO_FLAG_DEFAULT;
1736 fbi->pseudo_palette = fbdev->pseudo_palette;
1738 if (ofbi->region->size == 0) {
1744 var->bits_per_pixel = 0;
1746 var->rotate = def_rotate;
1750 int rotation = (var->rotate + ofbi->rotation[0]) % 4;
1752 display->driver->get_resolution(display, &w, &h);
1754 if (rotation == FB_ROTATE_CW ||
1755 rotation == FB_ROTATE_CCW) {
1763 var->xres_virtual = var->xres;
1764 var->yres_virtual = var->yres;
1766 if (!var->bits_per_pixel) {
1767 switch (omapfb_get_recommended_bpp(fbdev, display)) {
1769 var->bits_per_pixel = 16;
1772 var->bits_per_pixel = 32;
1775 dev_err(fbdev->dev, "illegal display "
1781 /* if there's no display, let's just guess some basic values */
1784 var->xres_virtual = var->xres;
1785 var->yres_virtual = var->yres;
1786 if (!var->bits_per_pixel)
1787 var->bits_per_pixel = 16;
1790 r = check_fb_var(fbi, var);
1795 r = setup_vrfb_rotation(fbi);
1799 r = fb_alloc_cmap(&fbi->cmap, 256, 0);
1801 dev_err(fbdev->dev, "unable to allocate color map memory\n");
1807 static void fbinfo_cleanup(struct omapfb2_device *fbdev, struct fb_info *fbi)
1809 fb_dealloc_cmap(&fbi->cmap);
1813 static void omapfb_free_resources(struct omapfb2_device *fbdev)
1817 DBG("free_resources\n");
1822 for (i = 0; i < fbdev->num_overlays; i++) {
1823 struct omap_overlay *ovl = fbdev->overlays[i];
1828 ovl->unset_manager(ovl);
1831 for (i = 0; i < fbdev->num_fbs; i++)
1832 unregister_framebuffer(fbdev->fbs[i]);
1834 /* free the reserved fbmem */
1835 omapfb_free_all_fbmem(fbdev);
1837 for (i = 0; i < fbdev->num_fbs; i++) {
1838 fbinfo_cleanup(fbdev, fbdev->fbs[i]);
1839 framebuffer_release(fbdev->fbs[i]);
1842 for (i = 0; i < fbdev->num_displays; i++) {
1843 struct omap_dss_device *dssdev = fbdev->displays[i].dssdev;
1845 if (fbdev->displays[i].auto_update_work_enabled)
1846 omapfb_stop_auto_update(fbdev, dssdev);
1848 if (dssdev->state != OMAP_DSS_DISPLAY_DISABLED)
1849 dssdev->driver->disable(dssdev);
1851 dssdev->driver->disconnect(dssdev);
1853 omap_dss_put_device(dssdev);
1856 if (fbdev->auto_update_wq != NULL) {
1857 destroy_workqueue(fbdev->auto_update_wq);
1858 fbdev->auto_update_wq = NULL;
1861 dev_set_drvdata(fbdev->dev, NULL);
1864 static int omapfb_create_framebuffers(struct omapfb2_device *fbdev)
1870 DBG("create %d framebuffers\n", CONFIG_FB_OMAP2_NUM_FBS);
1872 /* allocate fb_infos */
1873 for (i = 0; i < CONFIG_FB_OMAP2_NUM_FBS; i++) {
1874 struct fb_info *fbi;
1875 struct omapfb_info *ofbi;
1877 fbi = framebuffer_alloc(sizeof(struct omapfb_info),
1884 fbdev->fbs[i] = fbi;
1887 ofbi->fbdev = fbdev;
1890 ofbi->region = &fbdev->regions[i];
1891 ofbi->region->id = i;
1892 init_rwsem(&ofbi->region->lock);
1894 /* assign these early, so that fb alloc can use them */
1895 ofbi->rotation_type = def_vrfb ? OMAP_DSS_ROT_VRFB :
1897 ofbi->mirror = def_mirror;
1902 DBG("fb_infos allocated\n");
1904 /* assign overlays for the fbs */
1905 for (i = 0; i < min(fbdev->num_fbs, fbdev->num_overlays); i++) {
1906 struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[i]);
1908 ofbi->overlays[0] = fbdev->overlays[i];
1909 ofbi->num_overlays = 1;
1912 /* allocate fb memories */
1913 r = omapfb_allocate_all_fbs(fbdev);
1915 dev_err(fbdev->dev, "failed to allocate fbmem\n");
1919 DBG("fbmems allocated\n");
1921 /* setup fb_infos */
1922 for (i = 0; i < fbdev->num_fbs; i++) {
1923 struct fb_info *fbi = fbdev->fbs[i];
1924 struct omapfb_info *ofbi = FB2OFB(fbi);
1926 omapfb_get_mem_region(ofbi->region);
1927 r = omapfb_fb_init(fbdev, fbi);
1928 omapfb_put_mem_region(ofbi->region);
1931 dev_err(fbdev->dev, "failed to setup fb_info\n");
1936 for (i = 0; i < fbdev->num_fbs; i++) {
1937 struct fb_info *fbi = fbdev->fbs[i];
1938 struct omapfb_info *ofbi = FB2OFB(fbi);
1940 if (ofbi->region->size == 0)
1943 omapfb_clear_fb(fbi);
1946 DBG("fb_infos initialized\n");
1948 for (i = 0; i < fbdev->num_fbs; i++) {
1949 r = register_framebuffer(fbdev->fbs[i]);
1952 "registering framebuffer %d failed\n", i);
1957 DBG("framebuffers registered\n");
1959 for (i = 0; i < fbdev->num_fbs; i++) {
1960 struct fb_info *fbi = fbdev->fbs[i];
1961 struct omapfb_info *ofbi = FB2OFB(fbi);
1963 omapfb_get_mem_region(ofbi->region);
1964 r = omapfb_apply_changes(fbi, 1);
1965 omapfb_put_mem_region(ofbi->region);
1968 dev_err(fbdev->dev, "failed to change mode\n");
1974 if (fbdev->num_fbs > 0) {
1975 struct omapfb_info *ofbi = FB2OFB(fbdev->fbs[0]);
1977 if (ofbi->num_overlays > 0) {
1978 struct omap_overlay *ovl = ofbi->overlays[0];
1980 ovl->manager->apply(ovl->manager);
1982 r = omapfb_overlay_enable(ovl, 1);
1986 "failed to enable overlay\n");
1992 DBG("create_framebuffers done\n");
1997 static int omapfb_mode_to_timings(const char *mode_str,
1998 struct omap_dss_device *display,
1999 struct omap_video_timings *timings, u8 *bpp)
2001 struct fb_info *fbi;
2002 struct fb_var_screeninfo *var;
2003 struct fb_ops *fbops;
2006 #ifdef CONFIG_OMAP2_DSS_VENC
2007 if (strcmp(mode_str, "pal") == 0) {
2008 *timings = omap_dss_pal_timings;
2011 } else if (strcmp(mode_str, "ntsc") == 0) {
2012 *timings = omap_dss_ntsc_timings;
2018 /* this is quite a hack, but I wanted to use the modedb and for
2019 * that we need fb_info and var, so we create dummy ones */
2026 fbi = kzalloc(sizeof(*fbi), GFP_KERNEL);
2032 var = kzalloc(sizeof(*var), GFP_KERNEL);
2038 fbops = kzalloc(sizeof(*fbops), GFP_KERNEL);
2039 if (fbops == NULL) {
2046 r = fb_find_mode(var, fbi, mode_str, NULL, 0, NULL, 24);
2052 if (display->driver->get_timings) {
2053 display->driver->get_timings(display, timings);
2055 timings->data_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE;
2056 timings->de_level = OMAPDSS_SIG_ACTIVE_HIGH;
2057 timings->sync_pclk_edge = OMAPDSS_DRIVE_SIG_FALLING_EDGE;
2060 timings->pixelclock = PICOS2KHZ(var->pixclock) * 1000;
2061 timings->hbp = var->left_margin;
2062 timings->hfp = var->right_margin;
2063 timings->vbp = var->upper_margin;
2064 timings->vfp = var->lower_margin;
2065 timings->hsw = var->hsync_len;
2066 timings->vsw = var->vsync_len;
2067 timings->x_res = var->xres;
2068 timings->y_res = var->yres;
2069 timings->hsync_level = var->sync & FB_SYNC_HOR_HIGH_ACT ?
2070 OMAPDSS_SIG_ACTIVE_HIGH :
2071 OMAPDSS_SIG_ACTIVE_LOW;
2072 timings->vsync_level = var->sync & FB_SYNC_VERT_HIGH_ACT ?
2073 OMAPDSS_SIG_ACTIVE_HIGH :
2074 OMAPDSS_SIG_ACTIVE_LOW;
2075 timings->interlace = var->vmode & FB_VMODE_INTERLACED;
2077 switch (var->bits_per_pixel) {
2098 static int omapfb_set_def_mode(struct omapfb2_device *fbdev,
2099 struct omap_dss_device *display, char *mode_str)
2103 struct omap_video_timings timings, temp_timings;
2104 struct omapfb_display_data *d;
2106 r = omapfb_mode_to_timings(mode_str, display, &timings, &bpp);
2110 d = get_display_data(fbdev, display);
2111 d->bpp_override = bpp;
2113 if (display->driver->check_timings) {
2114 r = display->driver->check_timings(display, &timings);
2118 /* If check_timings is not present compare xres and yres */
2119 if (display->driver->get_timings) {
2120 display->driver->get_timings(display, &temp_timings);
2122 if (temp_timings.x_res != timings.x_res ||
2123 temp_timings.y_res != timings.y_res)
2128 if (display->driver->set_timings)
2129 display->driver->set_timings(display, &timings);
2134 static int omapfb_get_recommended_bpp(struct omapfb2_device *fbdev,
2135 struct omap_dss_device *dssdev)
2137 struct omapfb_display_data *d;
2139 BUG_ON(dssdev->driver->get_recommended_bpp == NULL);
2141 d = get_display_data(fbdev, dssdev);
2143 if (d->bpp_override != 0)
2144 return d->bpp_override;
2146 return dssdev->driver->get_recommended_bpp(dssdev);
2149 static int omapfb_parse_def_modes(struct omapfb2_device *fbdev)
2151 char *str, *options, *this_opt;
2154 str = kstrdup(def_mode, GFP_KERNEL);
2159 while (!r && (this_opt = strsep(&options, ",")) != NULL) {
2160 char *p, *display_str, *mode_str;
2161 struct omap_dss_device *display;
2164 p = strchr(this_opt, ':');
2171 display_str = this_opt;
2175 for (i = 0; i < fbdev->num_displays; ++i) {
2176 if (strcmp(fbdev->displays[i].dssdev->name,
2177 display_str) == 0) {
2178 display = fbdev->displays[i].dssdev;
2188 r = omapfb_set_def_mode(fbdev, display, mode_str);
2198 static void fb_videomode_to_omap_timings(struct fb_videomode *m,
2199 struct omap_dss_device *display,
2200 struct omap_video_timings *t)
2202 if (display->driver->get_timings) {
2203 display->driver->get_timings(display, t);
2205 t->data_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE;
2206 t->de_level = OMAPDSS_SIG_ACTIVE_HIGH;
2207 t->sync_pclk_edge = OMAPDSS_DRIVE_SIG_FALLING_EDGE;
2212 t->pixelclock = PICOS2KHZ(m->pixclock) * 1000;
2213 t->hsw = m->hsync_len;
2214 t->hfp = m->right_margin;
2215 t->hbp = m->left_margin;
2216 t->vsw = m->vsync_len;
2217 t->vfp = m->lower_margin;
2218 t->vbp = m->upper_margin;
2219 t->hsync_level = m->sync & FB_SYNC_HOR_HIGH_ACT ?
2220 OMAPDSS_SIG_ACTIVE_HIGH :
2221 OMAPDSS_SIG_ACTIVE_LOW;
2222 t->vsync_level = m->sync & FB_SYNC_VERT_HIGH_ACT ?
2223 OMAPDSS_SIG_ACTIVE_HIGH :
2224 OMAPDSS_SIG_ACTIVE_LOW;
2225 t->interlace = m->vmode & FB_VMODE_INTERLACED;
2228 static int omapfb_find_best_mode(struct omap_dss_device *display,
2229 struct omap_video_timings *timings)
2231 struct fb_monspecs *specs;
2233 int r, i, best_idx, len;
2235 if (!display->driver->read_edid)
2239 edid = kmalloc(len, GFP_KERNEL);
2243 r = display->driver->read_edid(display, edid, len);
2247 specs = kzalloc(sizeof(*specs), GFP_KERNEL);
2248 if (specs == NULL) {
2253 fb_edid_to_monspecs(edid, specs);
2257 for (i = 0; i < specs->modedb_len; ++i) {
2258 struct fb_videomode *m;
2259 struct omap_video_timings t;
2261 m = &specs->modedb[i];
2263 if (m->pixclock == 0)
2266 /* skip repeated pixel modes */
2267 if (m->xres == 2880 || m->xres == 1440)
2270 if (m->vmode & FB_VMODE_INTERLACED ||
2271 m->vmode & FB_VMODE_DOUBLE)
2274 fb_videomode_to_omap_timings(m, display, &t);
2276 r = display->driver->check_timings(display, &t);
2283 if (best_idx == -1) {
2288 fb_videomode_to_omap_timings(&specs->modedb[best_idx], display,
2294 fb_destroy_modedb(specs->modedb);
2302 static int omapfb_init_display(struct omapfb2_device *fbdev,
2303 struct omap_dss_device *dssdev)
2305 struct omap_dss_driver *dssdrv = dssdev->driver;
2306 struct omapfb_display_data *d;
2309 r = dssdrv->enable(dssdev);
2311 dev_warn(fbdev->dev, "Failed to enable display '%s'\n",
2316 d = get_display_data(fbdev, dssdev);
2320 if (dssdev->caps & OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE) {
2324 omapfb_start_auto_update(fbdev, dssdev);
2325 d->update_mode = OMAPFB_AUTO_UPDATE;
2327 d->update_mode = OMAPFB_MANUAL_UPDATE;
2330 if (dssdrv->enable_te) {
2331 r = dssdrv->enable_te(dssdev, 1);
2333 dev_err(fbdev->dev, "Failed to set TE\n");
2338 dssdrv->get_resolution(dssdev, &w, &h);
2339 r = dssdrv->update(dssdev, 0, 0, w, h);
2342 "Failed to update display\n");
2346 d->update_mode = OMAPFB_AUTO_UPDATE;
2352 static int omapfb_init_connections(struct omapfb2_device *fbdev,
2353 struct omap_dss_device *def_dssdev)
2356 struct omap_overlay_manager *mgr;
2358 r = def_dssdev->driver->connect(def_dssdev);
2360 dev_err(fbdev->dev, "failed to connect default display\n");
2364 for (i = 0; i < fbdev->num_displays; ++i) {
2365 struct omap_dss_device *dssdev = fbdev->displays[i].dssdev;
2367 if (dssdev == def_dssdev)
2371 * We don't care if the connect succeeds or not. We just want to
2372 * connect as many displays as possible.
2374 dssdev->driver->connect(dssdev);
2377 mgr = omapdss_find_mgr_from_display(def_dssdev);
2380 dev_err(fbdev->dev, "no ovl manager for the default display\n");
2384 for (i = 0; i < fbdev->num_overlays; i++) {
2385 struct omap_overlay *ovl = fbdev->overlays[i];
2388 ovl->unset_manager(ovl);
2390 r = ovl->set_manager(ovl, mgr);
2392 dev_warn(fbdev->dev,
2393 "failed to connect overlay %s to manager %s\n",
2394 ovl->name, mgr->name);
2400 static struct omap_dss_device *
2401 omapfb_find_default_display(struct omapfb2_device *fbdev)
2403 const char *def_name;
2407 * Search with the display name from the user or the board file,
2408 * comparing to display names and aliases
2411 def_name = omapdss_get_default_display_name();
2414 for (i = 0; i < fbdev->num_displays; ++i) {
2415 struct omap_dss_device *dssdev;
2417 dssdev = fbdev->displays[i].dssdev;
2419 if (dssdev->name && strcmp(def_name, dssdev->name) == 0)
2422 if (strcmp(def_name, dssdev->alias) == 0)
2426 /* def_name given but not found */
2430 /* then look for DT alias display0 */
2431 for (i = 0; i < fbdev->num_displays; ++i) {
2432 struct omap_dss_device *dssdev;
2435 dssdev = fbdev->displays[i].dssdev;
2437 if (dssdev->dev->of_node == NULL)
2440 id = of_alias_get_id(dssdev->dev->of_node, "display");
2445 /* return the first display we have in the list */
2446 return fbdev->displays[0].dssdev;
2449 static int omapfb_probe(struct platform_device *pdev)
2451 struct omapfb2_device *fbdev = NULL;
2454 struct omap_dss_device *def_display;
2455 struct omap_dss_device *dssdev;
2457 DBG("omapfb_probe\n");
2459 if (omapdss_is_initialized() == false)
2460 return -EPROBE_DEFER;
2462 if (pdev->num_resources != 0) {
2463 dev_err(&pdev->dev, "probed for an unknown device\n");
2468 fbdev = devm_kzalloc(&pdev->dev, sizeof(struct omapfb2_device),
2470 if (fbdev == NULL) {
2475 if (def_vrfb && !omap_vrfb_supported()) {
2477 dev_warn(&pdev->dev, "VRFB is not supported on this hardware, "
2478 "ignoring the module parameter vrfb=y\n");
2481 r = omapdss_compat_init();
2485 mutex_init(&fbdev->mtx);
2487 fbdev->dev = &pdev->dev;
2488 platform_set_drvdata(pdev, fbdev);
2490 fbdev->num_displays = 0;
2492 for_each_dss_dev(dssdev) {
2493 struct omapfb_display_data *d;
2495 omap_dss_get_device(dssdev);
2497 if (!dssdev->driver) {
2498 dev_warn(&pdev->dev, "no driver for display: %s\n",
2500 omap_dss_put_device(dssdev);
2504 d = &fbdev->displays[fbdev->num_displays++];
2506 if (dssdev->caps & OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE)
2507 d->update_mode = OMAPFB_MANUAL_UPDATE;
2509 d->update_mode = OMAPFB_AUTO_UPDATE;
2512 if (fbdev->num_displays == 0) {
2513 dev_err(&pdev->dev, "no displays\n");
2518 fbdev->num_overlays = omap_dss_get_num_overlays();
2519 for (i = 0; i < fbdev->num_overlays; i++)
2520 fbdev->overlays[i] = omap_dss_get_overlay(i);
2522 fbdev->num_managers = omap_dss_get_num_overlay_managers();
2523 for (i = 0; i < fbdev->num_managers; i++)
2524 fbdev->managers[i] = omap_dss_get_overlay_manager(i);
2526 def_display = omapfb_find_default_display(fbdev);
2527 if (def_display == NULL) {
2528 dev_err(fbdev->dev, "failed to find default display\n");
2533 r = omapfb_init_connections(fbdev, def_display);
2535 dev_err(fbdev->dev, "failed to init overlay connections\n");
2539 if (def_mode && strlen(def_mode) > 0) {
2540 if (omapfb_parse_def_modes(fbdev))
2541 dev_warn(&pdev->dev, "cannot parse default modes\n");
2542 } else if (def_display && def_display->driver->set_timings &&
2543 def_display->driver->check_timings) {
2544 struct omap_video_timings t;
2546 r = omapfb_find_best_mode(def_display, &t);
2549 def_display->driver->set_timings(def_display, &t);
2552 r = omapfb_create_framebuffers(fbdev);
2556 for (i = 0; i < fbdev->num_managers; i++) {
2557 struct omap_overlay_manager *mgr;
2558 mgr = fbdev->managers[i];
2559 r = mgr->apply(mgr);
2561 dev_warn(fbdev->dev, "failed to apply dispc config\n");
2564 DBG("mgr->apply'ed\n");
2567 r = omapfb_init_display(fbdev, def_display);
2570 "failed to initialize default "
2576 DBG("create sysfs for fbs\n");
2577 r = omapfb_create_sysfs(fbdev);
2579 dev_err(fbdev->dev, "failed to create sysfs entries\n");
2586 def_display->driver->get_resolution(def_display, &w, &h);
2588 dev_info(fbdev->dev, "using display '%s' mode %dx%d\n",
2589 def_display->name, w, h);
2595 omapfb_free_resources(fbdev);
2596 omapdss_compat_uninit();
2598 dev_err(&pdev->dev, "failed to setup omapfb\n");
2602 static int omapfb_remove(struct platform_device *pdev)
2604 struct omapfb2_device *fbdev = platform_get_drvdata(pdev);
2606 /* FIXME: wait till completion of pending events */
2608 omapfb_remove_sysfs(fbdev);
2610 omapfb_free_resources(fbdev);
2612 omapdss_compat_uninit();
2617 static struct platform_driver omapfb_driver = {
2618 .probe = omapfb_probe,
2619 .remove = omapfb_remove,
2625 module_param_named(mode, def_mode, charp, 0);
2626 module_param_named(vram, def_vram, charp, 0);
2627 module_param_named(rotate, def_rotate, int, 0);
2628 module_param_named(vrfb, def_vrfb, bool, 0);
2629 module_param_named(mirror, def_mirror, bool, 0);
2631 module_platform_driver(omapfb_driver);
2633 MODULE_ALIAS("platform:omapfb");
2634 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@nokia.com>");
2635 MODULE_DESCRIPTION("OMAP2/3 Framebuffer");
2636 MODULE_LICENSE("GPL v2");