5 * Stefano Babic, DENX Software Engineering, sbabic@denx.de
7 * IPUv3 Linux framebuffer:
9 * (C) Copyright 2004-2011 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,
33 #include <asm/errno.h>
34 #include <linux/string.h>
35 #include <linux/list.h>
42 #include "videomodes.h"
45 DECLARE_GLOBAL_DATA_PTR;
47 extern vidinfo_t panel_info;
49 void *lcd_base; /* Start of framebuffer memory */
50 void *lcd_console_address; /* Start of console buffer */
59 static int mxcfb_map_video_memory(struct fb_info *fbi);
60 static int mxcfb_unmap_video_memory(struct fb_info *fbi);
62 void lcd_initcolregs(void)
66 void lcd_setcolreg(ushort regno, ushort red, ushort green, ushort blue)
70 void lcd_disable(void)
74 void lcd_panel_disable(void)
78 static void fb_videomode_to_var(struct fb_var_screeninfo *var,
79 const struct fb_videomode *mode)
81 var->xres = mode->xres;
82 var->yres = mode->yres;
83 var->xres_virtual = mode->xres;
84 var->yres_virtual = mode->yres;
87 var->pixclock = mode->pixclock;
88 var->left_margin = mode->left_margin;
89 var->right_margin = mode->right_margin;
90 var->upper_margin = mode->upper_margin;
91 var->lower_margin = mode->lower_margin;
92 var->hsync_len = mode->hsync_len;
93 var->vsync_len = mode->vsync_len;
94 var->sync = mode->sync;
95 var->vmode = mode->vmode & FB_VMODE_MASK;
99 * Structure containing the MXC specific framebuffer information.
103 ipu_channel_t ipu_ch;
106 unsigned char overlay;
107 unsigned char alpha_chan_en;
108 dma_addr_t alpha_phy_addr0;
109 dma_addr_t alpha_phy_addr1;
110 void *alpha_virt_addr0;
111 void *alpha_virt_addr1;
112 uint32_t alpha_mem_len;
113 uint32_t cur_ipu_buf;
114 uint32_t cur_ipu_alpha_buf;
116 u32 pseudo_palette[16];
126 static unsigned long default_bpp = 16;
127 static unsigned char g_dp_in_use;
128 static struct fb_info *mxcfb_info[3];
129 static int ext_clk_used;
131 static uint32_t bpp_to_pixfmt(struct fb_info *fbi)
135 debug("bpp_to_pixfmt: %d\n", fbi->var.bits_per_pixel);
138 return fbi->var.nonstd;
140 switch (fbi->var.bits_per_pixel) {
142 pixfmt = IPU_PIX_FMT_BGR24;
145 pixfmt = IPU_PIX_FMT_BGR32;
148 pixfmt = IPU_PIX_FMT_RGB565;
151 pixfmt = IPU_PIX_FMT_GENERIC;
157 * Set fixed framebuffer parameters based on variable settings.
159 * @param info framebuffer information pointer
161 static int mxcfb_set_fix(struct fb_info *info)
163 struct fb_fix_screeninfo *fix = &info->fix;
164 struct fb_var_screeninfo *var = &info->var;
166 fix->line_length = var->xres_virtual * var->bits_per_pixel / 8;
168 fix->type = FB_TYPE_PACKED_PIXELS;
169 fix->accel = FB_ACCEL_NONE;
170 fix->visual = FB_VISUAL_TRUECOLOR;
177 static int setup_disp_channel1(struct fb_info *fbi)
179 ipu_channel_params_t params;
180 struct mxcfb_info *mxc_fbi = fbi->par;
182 memset(¶ms, 0, sizeof(params));
183 params.mem_dp_bg_sync.di = mxc_fbi->ipu_di;
185 debug("%s called\n", __func__);
187 * Assuming interlaced means yuv output, below setting also
188 * valid for mem_dc_sync. FG should have the same vmode as BG.
190 if (fbi->var.vmode & FB_VMODE_INTERLACED) {
191 params.mem_dp_bg_sync.interlaced = 1;
192 params.mem_dp_bg_sync.out_pixel_fmt =
195 if (mxc_fbi->ipu_di_pix_fmt) {
196 params.mem_dp_bg_sync.out_pixel_fmt =
197 mxc_fbi->ipu_di_pix_fmt;
199 params.mem_dp_bg_sync.out_pixel_fmt =
203 params.mem_dp_bg_sync.in_pixel_fmt = bpp_to_pixfmt(fbi);
204 if (mxc_fbi->alpha_chan_en)
205 params.mem_dp_bg_sync.alpha_chan_en = 1;
207 ipu_init_channel(mxc_fbi->ipu_ch, ¶ms);
212 static int setup_disp_channel2(struct fb_info *fbi)
215 struct mxcfb_info *mxc_fbi = fbi->par;
217 mxc_fbi->cur_ipu_buf = 1;
218 if (mxc_fbi->alpha_chan_en)
219 mxc_fbi->cur_ipu_alpha_buf = 1;
221 fbi->var.xoffset = fbi->var.yoffset = 0;
223 debug("%s: ch: %08x xres: %d yres: %d line_length: %d mem: %08lx .. %08lx\n",
228 fbi->fix.line_length,
230 fbi->fix.smem_start +
231 (fbi->fix.line_length * fbi->var.yres) - 1);
233 retval = ipu_init_channel_buffer(mxc_fbi->ipu_ch, IPU_INPUT_BUFFER,
235 fbi->var.xres, fbi->var.yres,
236 fbi->fix.line_length,
237 fbi->fix.smem_start +
238 (fbi->fix.line_length * fbi->var.yres),
242 printf("ipu_init_channel_buffer error %d\n", retval);
248 * Set framebuffer parameters and change the operating mode.
250 * @param info framebuffer information pointer
252 static int mxcfb_set_par(struct fb_info *fbi)
256 ipu_di_signal_cfg_t sig_cfg;
257 struct mxcfb_info *mxc_fbi = fbi->par;
258 uint32_t out_pixel_fmt;
260 ipu_disable_channel(mxc_fbi->ipu_ch);
261 ipu_uninit_channel(mxc_fbi->ipu_ch);
264 mem_len = fbi->var.yres_virtual * fbi->fix.line_length;
265 if (!fbi->fix.smem_start || (mem_len > fbi->fix.smem_len)) {
266 if (fbi->fix.smem_start)
267 mxcfb_unmap_video_memory(fbi);
269 if (mxcfb_map_video_memory(fbi) < 0)
273 setup_disp_channel1(fbi);
275 memset(&sig_cfg, 0, sizeof(sig_cfg));
276 if (fbi->var.vmode & FB_VMODE_INTERLACED) {
277 sig_cfg.interlaced = 1;
278 out_pixel_fmt = IPU_PIX_FMT_YUV444;
280 if (mxc_fbi->ipu_di_pix_fmt)
281 out_pixel_fmt = mxc_fbi->ipu_di_pix_fmt;
283 out_pixel_fmt = IPU_PIX_FMT_RGB666;
285 if (fbi->var.vmode & FB_VMODE_ODD_FLD_FIRST) /* PAL */
286 sig_cfg.odd_field_first = 1;
287 if ((fbi->var.sync & FB_SYNC_EXT) || ext_clk_used)
289 if (fbi->var.sync & FB_SYNC_HOR_HIGH_ACT)
290 sig_cfg.Hsync_pol = 1;
291 if (fbi->var.sync & FB_SYNC_VERT_HIGH_ACT)
292 sig_cfg.Vsync_pol = 1;
293 if (!(fbi->var.sync & FB_SYNC_CLK_LAT_FALL))
295 if (fbi->var.sync & FB_SYNC_DATA_INVERT)
296 sig_cfg.data_pol = 1;
297 if (!(fbi->var.sync & FB_SYNC_OE_LOW_ACT))
298 sig_cfg.enable_pol = 1;
299 if (fbi->var.sync & FB_SYNC_CLK_IDLE_EN)
300 sig_cfg.clkidle_en = 1;
302 debug("pixclock = %lu.%03lu MHz\n",
303 PICOS2KHZ(fbi->var.pixclock) / 1000,
304 PICOS2KHZ(fbi->var.pixclock) % 1000);
306 retval = ipu_init_sync_panel(mxc_fbi->ipu_di,
307 (PICOS2KHZ(fbi->var.pixclock)) * 1000UL,
308 fbi->var.xres, fbi->var.yres,
310 fbi->var.left_margin,
312 fbi->var.right_margin,
313 fbi->var.upper_margin,
315 fbi->var.lower_margin,
318 printf("mxc_ipuv3_fb: Error %d initializing panel\n", retval);
322 retval = setup_disp_channel2(fbi);
326 if (mxc_fbi->blank == FB_BLANK_UNBLANK)
327 ipu_enable_channel(mxc_fbi->ipu_ch);
333 * Check framebuffer variable parameters and adjust to valid values.
335 * @param var framebuffer variable parameters
337 * @param info framebuffer information pointer
339 static int mxcfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
344 if (var->xres_virtual < var->xres)
345 var->xres_virtual = var->xres;
346 if (var->yres_virtual < var->yres)
347 var->yres_virtual = var->yres;
349 if ((var->bits_per_pixel != 32) && (var->bits_per_pixel != 24) &&
350 (var->bits_per_pixel != 16) && (var->bits_per_pixel != 8))
351 var->bits_per_pixel = default_bpp;
353 switch (var->bits_per_pixel) {
357 var->red.msb_right = 0;
359 var->green.length = 3;
360 var->green.offset = 2;
361 var->green.msb_right = 0;
363 var->blue.length = 2;
364 var->blue.offset = 0;
365 var->blue.msb_right = 0;
367 var->transp.length = 0;
368 var->transp.offset = 0;
369 var->transp.msb_right = 0;
373 var->red.offset = 11;
374 var->red.msb_right = 0;
376 var->green.length = 6;
377 var->green.offset = 5;
378 var->green.msb_right = 0;
380 var->blue.length = 5;
381 var->blue.offset = 0;
382 var->blue.msb_right = 0;
384 var->transp.length = 0;
385 var->transp.offset = 0;
386 var->transp.msb_right = 0;
390 var->red.offset = 16;
391 var->red.msb_right = 0;
393 var->green.length = 8;
394 var->green.offset = 8;
395 var->green.msb_right = 0;
397 var->blue.length = 8;
398 var->blue.offset = 0;
399 var->blue.msb_right = 0;
401 var->transp.length = 0;
402 var->transp.offset = 0;
403 var->transp.msb_right = 0;
407 var->red.offset = 16;
408 var->red.msb_right = 0;
410 var->green.length = 8;
411 var->green.offset = 8;
412 var->green.msb_right = 0;
414 var->blue.length = 8;
415 var->blue.offset = 0;
416 var->blue.msb_right = 0;
418 var->transp.length = 8;
419 var->transp.offset = 24;
420 var->transp.msb_right = 0;
424 if (var->pixclock < 1000) {
425 htotal = var->xres + var->right_margin + var->hsync_len +
427 vtotal = var->yres + var->lower_margin + var->vsync_len +
429 var->pixclock = (vtotal * htotal * 6UL) / 100UL;
430 var->pixclock = KHZ2PICOS(var->pixclock);
431 printf("pixclock set for 60Hz refresh = %u ps\n",
442 static int mxcfb_map_video_memory(struct fb_info *fbi)
444 if (fbi->fix.smem_len < fbi->var.yres_virtual * fbi->fix.line_length) {
445 fbi->fix.smem_len = fbi->var.yres_virtual *
446 fbi->fix.line_length;
449 fbi->screen_base = (void *)gd->fb_base;
451 fbi->screen_base = malloc(fbi->fix.smem_len);
452 if (fbi->screen_base == NULL) {
453 puts("Unable to allocate framebuffer memory\n");
454 fbi->fix.smem_len = 0;
457 fbi->fix.smem_start = (unsigned long)fbi->screen_base;
459 debug("allocated fb @ paddr=0x%08X, size=%d.\n",
460 (uint32_t) fbi->fix.smem_start, fbi->fix.smem_len);
462 fbi->screen_size = fbi->fix.smem_len;
467 static int mxcfb_unmap_video_memory(struct fb_info *fbi)
469 fbi->screen_base = 0;
470 fbi->fix.smem_start = 0;
471 fbi->fix.smem_len = 0;
476 * Initializes the framebuffer information pointer. After allocating
477 * sufficient memory for the framebuffer structure, the fields are
478 * filled with custom information passed in from the configurable
479 * structures. This includes information such as bits per pixel,
480 * color maps, screen width/height and RGBA offsets.
482 * @return Framebuffer structure initialized with our information
484 static struct fb_info *mxcfb_init_fbinfo(void)
487 struct mxcfb_info *mxcfbi;
489 int size = ALIGN(sizeof(struct mxcfb_info), sizeof(long)) +
490 sizeof(struct fb_info);
492 debug("%s: %d %d %d\n",
495 sizeof(struct mxcfb_info),
496 sizeof(struct fb_info));
498 * Allocate sufficient memory for the fb structure
508 fbi->par = p + ALIGN(sizeof(struct fb_info), sizeof(long));
511 debug("Framebuffer structures at: fbi=%p mxcfbi=%p\n",
514 fbi->var.activate = FB_ACTIVATE_NOW;
516 fbi->flags = FBINFO_FLAG_DEFAULT;
517 fbi->pseudo_palette = mxcfbi->pseudo_palette;
523 * Probe routine for the framebuffer driver. It is called during the
524 * driver binding process. The following functions are performed in
525 * this routine: Framebuffer initialization, Memory allocation and
526 * mapping, Framebuffer registration, IPU initialization.
528 * @return Appropriate error code to the kernel common code
530 static int mxcfb_probe(u32 interface_pix_fmt, struct fb_videomode *mode, int di)
533 struct mxcfb_info *mxcfbi;
536 * Initialize FB structures
538 fbi = mxcfb_init_fbinfo();
545 mxcfbi->ipu_ch = MEM_BG_SYNC;
546 mxcfbi->blank = FB_BLANK_UNBLANK;
548 mxcfbi->ipu_ch = MEM_DC_SYNC;
549 mxcfbi->blank = FB_BLANK_POWERDOWN;
554 ipu_disp_set_global_alpha(mxcfbi->ipu_ch, 1, 0x80);
555 ipu_disp_set_color_key(mxcfbi->ipu_ch, 0, 0);
556 strcpy(fbi->fix.id, "DISP3 BG");
560 mxcfb_info[mxcfbi->ipu_di] = fbi;
562 /* Need dummy values until real panel is configured */
563 fbi->var.xres = panel_info.vl_col;
564 fbi->var.yres = panel_info.vl_row;
566 mxcfbi->ipu_di_pix_fmt = interface_pix_fmt;
567 fb_videomode_to_var(&fbi->var, mode);
568 fbi->var.bits_per_pixel = NBITS(panel_info.vl_bpix);
569 fbi->fix.line_length = fbi->var.xres * (fbi->var.bits_per_pixel / 8);
570 fbi->fix.smem_len = fbi->var.yres_virtual * fbi->fix.line_length;
572 mxcfb_check_var(&fbi->var, fbi);
575 /* allocate fb first */
576 if (mxcfb_map_video_memory(fbi) < 0)
581 lcd_line_length = (panel_info.vl_col * NBITS(panel_info.vl_bpix)) / 8;
583 debug("MXC IPUV3 configured\n"
584 "XRES = %d YRES = %d BitsXpixel = %d\n",
589 ipu_dump_registers();
594 ulong calc_fbsize(void)
596 return (panel_info.vl_col * panel_info.vl_row *
597 NBITS(panel_info.vl_bpix)) / 8;
600 int overwrite_console(void)
602 /* Keep stdout / stderr on serial, our LCD is for splashscreen only */
607 void lcd_ctrl_init(void *lcdbase)
609 u32 mem_len = panel_info.vl_col *
611 NBITS(panel_info.vl_bpix) / 8;
614 * We rely on lcdbase being a physical address, i.e., either MMU off,
615 * or 1-to-1 mapping. Might want to add some virt2phys here.
620 memset(lcdbase, 0, mem_len);
624 int ipuv3_fb_init(struct fb_videomode *mode, int di, unsigned int interface_pix_fmt,
625 ipu_di_clk_parent_t di_clk_parent, unsigned long di_clk_val, int bpp)
629 // default_bpp = bpp;
631 ret = ipu_probe(di, di_clk_parent, di_clk_val);
633 printf("Error initializing IPU\n");
637 debug("Framebuffer at %p\n", lcd_base);
638 ret = mxcfb_probe(interface_pix_fmt, mode, di);