5 * Stefano Babic, DENX Software Engineering, sbabic@denx.de
7 * MX51 Linux framebuffer:
9 * (C) Copyright 2004-2010 Freescale Semiconductor, Inc.
11 * See file CREDITS for list of people who contributed to this
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32 #include <asm/errno.h>
33 #include <linux/string.h>
34 #include <linux/list.h>
39 #include "videomodes.h"
43 DECLARE_GLOBAL_DATA_PTR;
45 void *lcd_base; /* Start of framebuffer memory */
46 void *lcd_console_address; /* Start of console buffer */
57 static int mxcfb_map_video_memory(struct fb_info *fbi);
58 static int mxcfb_unmap_video_memory(struct fb_info *fbi);
60 void lcd_initcolregs(void)
64 void lcd_setcolreg(ushort regno, ushort red, ushort green, ushort blue)
68 void lcd_disable(void)
72 void lcd_panel_disable(void)
76 void fb_videomode_to_var(struct fb_var_screeninfo *var,
77 const struct fb_videomode *mode)
79 var->xres = mode->xres;
80 var->yres = mode->yres;
81 var->xres_virtual = mode->xres;
82 var->yres_virtual = mode->yres;
85 var->pixclock = mode->pixclock;
86 var->left_margin = mode->left_margin;
87 var->right_margin = mode->right_margin;
88 var->upper_margin = mode->upper_margin;
89 var->lower_margin = mode->lower_margin;
90 var->hsync_len = mode->hsync_len;
91 var->vsync_len = mode->vsync_len;
92 var->sync = mode->sync;
93 var->vmode = mode->vmode & FB_VMODE_MASK;
97 * Structure containing the MXC specific framebuffer information.
101 ipu_channel_t ipu_ch;
104 unsigned char overlay;
105 unsigned char alpha_chan_en;
106 dma_addr_t alpha_phy_addr0;
107 dma_addr_t alpha_phy_addr1;
108 void *alpha_virt_addr0;
109 void *alpha_virt_addr1;
110 uint32_t alpha_mem_len;
111 uint32_t cur_ipu_buf;
112 uint32_t cur_ipu_alpha_buf;
114 u32 pseudo_palette[16];
124 static unsigned long default_bpp = 16;
125 static unsigned char g_dp_in_use;
126 static struct fb_info *mxcfb_info[3];
127 static int ext_clk_used;
129 static uint32_t bpp_to_pixfmt(struct fb_info *fbi)
133 debug("bpp_to_pixfmt: %d\n", fbi->var.bits_per_pixel);
136 return fbi->var.nonstd;
138 switch (fbi->var.bits_per_pixel) {
140 pixfmt = IPU_PIX_FMT_BGR24;
143 pixfmt = IPU_PIX_FMT_BGR32;
146 pixfmt = IPU_PIX_FMT_RGB565;
153 * Set fixed framebuffer parameters based on variable settings.
155 * @param info framebuffer information pointer
157 static int mxcfb_set_fix(struct fb_info *info)
159 struct fb_fix_screeninfo *fix = &info->fix;
160 struct fb_var_screeninfo *var = &info->var;
162 fix->line_length = var->xres_virtual * var->bits_per_pixel / 8;
164 fix->type = FB_TYPE_PACKED_PIXELS;
165 fix->accel = FB_ACCEL_NONE;
166 fix->visual = FB_VISUAL_TRUECOLOR;
173 static int setup_disp_channel1(struct fb_info *fbi)
175 ipu_channel_params_t params;
176 struct mxcfb_info *mxc_fbi = (struct mxcfb_info *)fbi->par;
178 memset(¶ms, 0, sizeof(params));
179 params.mem_dp_bg_sync.di = mxc_fbi->ipu_di;
181 debug("%s called\n", __func__);
183 * Assuming interlaced means yuv output, below setting also
184 * valid for mem_dc_sync. FG should have the same vmode as BG.
186 if (fbi->var.vmode & FB_VMODE_INTERLACED) {
187 params.mem_dp_bg_sync.interlaced = 1;
188 params.mem_dp_bg_sync.out_pixel_fmt =
191 if (mxc_fbi->ipu_di_pix_fmt) {
192 params.mem_dp_bg_sync.out_pixel_fmt =
193 mxc_fbi->ipu_di_pix_fmt;
195 params.mem_dp_bg_sync.out_pixel_fmt =
199 params.mem_dp_bg_sync.in_pixel_fmt = bpp_to_pixfmt(fbi);
200 if (mxc_fbi->alpha_chan_en)
201 params.mem_dp_bg_sync.alpha_chan_en = 1;
203 ipu_init_channel(mxc_fbi->ipu_ch, ¶ms);
208 static int setup_disp_channel2(struct fb_info *fbi)
211 struct mxcfb_info *mxc_fbi = (struct mxcfb_info *)fbi->par;
213 mxc_fbi->cur_ipu_buf = 1;
214 if (mxc_fbi->alpha_chan_en)
215 mxc_fbi->cur_ipu_alpha_buf = 1;
217 fbi->var.xoffset = fbi->var.yoffset = 0;
219 debug("%s: %x %d %d %d %lx %lx\n",
224 fbi->fix.line_length,
226 fbi->fix.smem_start +
227 (fbi->fix.line_length * fbi->var.yres));
229 retval = ipu_init_channel_buffer(mxc_fbi->ipu_ch, IPU_INPUT_BUFFER,
231 fbi->var.xres, fbi->var.yres,
232 fbi->fix.line_length,
233 fbi->fix.smem_start +
234 (fbi->fix.line_length * fbi->var.yres),
238 printf("ipu_init_channel_buffer error %d\n", retval);
244 * Set framebuffer parameters and change the operating mode.
246 * @param info framebuffer information pointer
248 static int mxcfb_set_par(struct fb_info *fbi)
252 ipu_di_signal_cfg_t sig_cfg;
253 struct mxcfb_info *mxc_fbi = (struct mxcfb_info *)fbi->par;
254 uint32_t out_pixel_fmt;
256 ipu_disable_channel(mxc_fbi->ipu_ch);
257 ipu_uninit_channel(mxc_fbi->ipu_ch);
260 mem_len = fbi->var.yres_virtual * fbi->fix.line_length;
261 if (!fbi->fix.smem_start || (mem_len > fbi->fix.smem_len)) {
262 if (fbi->fix.smem_start)
263 mxcfb_unmap_video_memory(fbi);
265 if (mxcfb_map_video_memory(fbi) < 0)
269 setup_disp_channel1(fbi);
271 memset(&sig_cfg, 0, sizeof(sig_cfg));
272 if (fbi->var.vmode & FB_VMODE_INTERLACED) {
273 sig_cfg.interlaced = 1;
274 out_pixel_fmt = IPU_PIX_FMT_YUV444;
276 if (mxc_fbi->ipu_di_pix_fmt)
277 out_pixel_fmt = mxc_fbi->ipu_di_pix_fmt;
279 out_pixel_fmt = IPU_PIX_FMT_RGB666;
281 if (fbi->var.vmode & FB_VMODE_ODD_FLD_FIRST) /* PAL */
282 sig_cfg.odd_field_first = 1;
283 if ((fbi->var.sync & FB_SYNC_EXT) || ext_clk_used)
285 if (fbi->var.sync & FB_SYNC_HOR_HIGH_ACT)
286 sig_cfg.Hsync_pol = 1;
287 if (fbi->var.sync & FB_SYNC_VERT_HIGH_ACT)
288 sig_cfg.Vsync_pol = 1;
289 if (!(fbi->var.sync & FB_SYNC_CLK_LAT_FALL))
291 if (fbi->var.sync & FB_SYNC_DATA_INVERT)
292 sig_cfg.data_pol = 1;
293 if (!(fbi->var.sync & FB_SYNC_OE_LOW_ACT))
294 sig_cfg.enable_pol = 1;
295 if (fbi->var.sync & FB_SYNC_CLK_IDLE_EN)
296 sig_cfg.clkidle_en = 1;
298 debug("pixclock = %ul Hz\n",
299 (u32) (PICOS2KHZ(fbi->var.pixclock) * 1000UL));
301 if (ipu_init_sync_panel(mxc_fbi->ipu_di,
302 (PICOS2KHZ(fbi->var.pixclock)) * 1000UL,
303 fbi->var.xres, fbi->var.yres,
305 fbi->var.left_margin,
307 fbi->var.right_margin,
308 fbi->var.upper_margin,
310 fbi->var.lower_margin,
312 puts("mxcfb: Error initializing panel.\n");
316 retval = setup_disp_channel2(fbi);
320 if (mxc_fbi->blank == FB_BLANK_UNBLANK)
321 ipu_enable_channel(mxc_fbi->ipu_ch);
327 * Check framebuffer variable parameters and adjust to valid values.
329 * @param var framebuffer variable parameters
331 * @param info framebuffer information pointer
333 static int mxcfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
338 if (var->xres_virtual < var->xres)
339 var->xres_virtual = var->xres;
340 if (var->yres_virtual < var->yres)
341 var->yres_virtual = var->yres;
343 if ((var->bits_per_pixel != 32) && (var->bits_per_pixel != 24) &&
344 (var->bits_per_pixel != 16) && (var->bits_per_pixel != 8))
345 var->bits_per_pixel = default_bpp;
347 switch (var->bits_per_pixel) {
351 var->red.msb_right = 0;
353 var->green.length = 3;
354 var->green.offset = 2;
355 var->green.msb_right = 0;
357 var->blue.length = 2;
358 var->blue.offset = 0;
359 var->blue.msb_right = 0;
361 var->transp.length = 0;
362 var->transp.offset = 0;
363 var->transp.msb_right = 0;
367 var->red.offset = 11;
368 var->red.msb_right = 0;
370 var->green.length = 6;
371 var->green.offset = 5;
372 var->green.msb_right = 0;
374 var->blue.length = 5;
375 var->blue.offset = 0;
376 var->blue.msb_right = 0;
378 var->transp.length = 0;
379 var->transp.offset = 0;
380 var->transp.msb_right = 0;
384 var->red.offset = 16;
385 var->red.msb_right = 0;
387 var->green.length = 8;
388 var->green.offset = 8;
389 var->green.msb_right = 0;
391 var->blue.length = 8;
392 var->blue.offset = 0;
393 var->blue.msb_right = 0;
395 var->transp.length = 0;
396 var->transp.offset = 0;
397 var->transp.msb_right = 0;
401 var->red.offset = 16;
402 var->red.msb_right = 0;
404 var->green.length = 8;
405 var->green.offset = 8;
406 var->green.msb_right = 0;
408 var->blue.length = 8;
409 var->blue.offset = 0;
410 var->blue.msb_right = 0;
412 var->transp.length = 8;
413 var->transp.offset = 24;
414 var->transp.msb_right = 0;
418 if (var->pixclock < 1000) {
419 htotal = var->xres + var->right_margin + var->hsync_len +
421 vtotal = var->yres + var->lower_margin + var->vsync_len +
423 var->pixclock = (vtotal * htotal * 6UL) / 100UL;
424 var->pixclock = KHZ2PICOS(var->pixclock);
425 printf("pixclock set for 60Hz refresh = %u ps\n",
436 static int mxcfb_map_video_memory(struct fb_info *fbi)
438 if (fbi->fix.smem_len < fbi->var.yres_virtual * fbi->fix.line_length) {
439 fbi->fix.smem_len = fbi->var.yres_virtual *
440 fbi->fix.line_length;
443 fbi->screen_base = (char *)lcd_base;
444 fbi->fix.smem_start = (unsigned long)lcd_base;
445 if (fbi->screen_base == 0) {
446 puts("Unable to allocate framebuffer memory\n");
447 fbi->fix.smem_len = 0;
448 fbi->fix.smem_start = 0;
452 debug("allocated fb @ paddr=0x%08X, size=%d.\n",
453 (uint32_t) fbi->fix.smem_start, fbi->fix.smem_len);
455 fbi->screen_size = fbi->fix.smem_len;
457 /* Clear the screen */
458 memset((char *)fbi->screen_base, 0, fbi->fix.smem_len);
463 static int mxcfb_unmap_video_memory(struct fb_info *fbi)
465 fbi->screen_base = 0;
466 fbi->fix.smem_start = 0;
467 fbi->fix.smem_len = 0;
472 * Initializes the framebuffer information pointer. After allocating
473 * sufficient memory for the framebuffer structure, the fields are
474 * filled with custom information passed in from the configurable
475 * structures. This includes information such as bits per pixel,
476 * color maps, screen width/height and RGBA offsets.
478 * @return Framebuffer structure initialized with our information
480 static struct fb_info *mxcfb_init_fbinfo(void)
482 #define BYTES_PER_LONG 4
483 #define PADDING (BYTES_PER_LONG - (sizeof(struct fb_info) % BYTES_PER_LONG))
485 struct mxcfb_info *mxcfbi;
487 int size = sizeof(struct mxcfb_info) + PADDING +
488 sizeof(struct fb_info);
490 debug("%s: %d %d %d %d\n",
494 sizeof(struct mxcfb_info),
495 sizeof(struct fb_info));
497 * Allocate sufficient memory for the fb structure
506 fbi = (struct fb_info *)p;
507 fbi->par = p + sizeof(struct fb_info) + PADDING;
509 mxcfbi = (struct mxcfb_info *)fbi->par;
510 debug("Framebuffer structures at: fbi=0x%x mxcfbi=0x%x\n",
511 (unsigned int)fbi, (unsigned int)mxcfbi);
513 fbi->var.activate = FB_ACTIVATE_NOW;
515 fbi->flags = FBINFO_FLAG_DEFAULT;
516 fbi->pseudo_palette = mxcfbi->pseudo_palette;
522 * Probe routine for the framebuffer driver. It is called during the
523 * driver binding process. The following functions are performed in
524 * this routine: Framebuffer initialization, Memory allocation and
525 * mapping, Framebuffer registration, IPU initialization.
527 * @return Appropriate error code to the kernel common code
529 static int mxcfb_probe(u32 interface_pix_fmt, struct fb_videomode *mode)
532 struct mxcfb_info *mxcfbi;
536 * Initialize FB structures
538 fbi = mxcfb_init_fbinfo();
543 mxcfbi = (struct mxcfb_info *)fbi->par;
546 mxcfbi->ipu_ch = MEM_BG_SYNC;
547 mxcfbi->blank = FB_BLANK_UNBLANK;
549 mxcfbi->ipu_ch = MEM_DC_SYNC;
550 mxcfbi->blank = FB_BLANK_POWERDOWN;
555 ipu_disp_set_global_alpha(mxcfbi->ipu_ch, 1, 0x80);
556 ipu_disp_set_color_key(mxcfbi->ipu_ch, 0, 0);
557 strcpy(fbi->fix.id, "DISP3 BG");
561 mxcfb_info[mxcfbi->ipu_di] = fbi;
563 /* Need dummy values until real panel is configured */
566 fbi->var.bits_per_pixel = 16;
568 mxcfbi->ipu_di_pix_fmt = interface_pix_fmt;
569 fb_videomode_to_var(&fbi->var, mode);
571 mxcfb_check_var(&fbi->var, fbi);
573 /* Default Y virtual size is 2x panel size */
574 fbi->var.yres_virtual = fbi->var.yres * 2;
578 /* alocate fb first */
579 if (mxcfb_map_video_memory(fbi) < 0)
584 /* Setting panel_info for lcd */
585 panel_info.cmap = NULL;
586 panel_info.vl_col = fbi->var.xres;
587 panel_info.vl_row = fbi->var.yres;
588 panel_info.vl_bpix = LCD_BPP;
590 lcd_line_length = (panel_info.vl_col * NBITS(panel_info.vl_bpix)) / 8;
592 debug("MXC IPUV3 configured\n"
593 "XRES = %d YRES = %d BitsXpixel = %d\n",
598 ipu_dump_registers();
606 int overwrite_console(void)
608 /* Keep stdout / stderr on serial, our LCD is for splashscreen only */
612 void lcd_ctrl_init(void *lcdbase)
614 u32 mem_len = panel_info.vl_col *
616 NBITS(panel_info.vl_bpix) / 8;
619 * We rely on lcdbase being a physical address, i.e., either MMU off,
620 * or 1-to-1 mapping. Might want to add some virt2phys here.
625 memset(lcdbase, 0, mem_len);
628 int mx51_fb_init(struct fb_videomode *mode)
634 puts("Error initializing IPU\n");
638 debug("Framebuffer at 0x%x\n", (unsigned int)lcd_base);
639 ret = mxcfb_probe(IPU_PIX_FMT_RGB666, mode);