5 * Stefano Babic, DENX Software Engineering, sbabic@denx.de
7 * MX51 Linux framebuffer:
9 * (C) Copyright 2004-2010 Freescale Semiconductor, Inc.
11 * SPDX-License-Identifier: GPL-2.0+
15 #include <asm/errno.h>
16 #include <linux/string.h>
17 #include <linux/list.h>
22 #include "videomodes.h"
27 static int mxcfb_map_video_memory(struct fb_info *fbi);
28 static int mxcfb_unmap_video_memory(struct fb_info *fbi);
31 static GraphicDevice panel;
32 static struct fb_videomode const *gmode;
34 static uint32_t gpixfmt;
36 void fb_videomode_to_var(struct fb_var_screeninfo *var,
37 const struct fb_videomode *mode)
39 var->xres = mode->xres;
40 var->yres = mode->yres;
41 var->xres_virtual = mode->xres;
42 var->yres_virtual = mode->yres;
45 var->pixclock = mode->pixclock;
46 var->left_margin = mode->left_margin;
47 var->right_margin = mode->right_margin;
48 var->upper_margin = mode->upper_margin;
49 var->lower_margin = mode->lower_margin;
50 var->hsync_len = mode->hsync_len;
51 var->vsync_len = mode->vsync_len;
52 var->sync = mode->sync;
53 var->vmode = mode->vmode & FB_VMODE_MASK;
57 * Structure containing the MXC specific framebuffer information.
64 unsigned char overlay;
65 unsigned char alpha_chan_en;
66 dma_addr_t alpha_phy_addr0;
67 dma_addr_t alpha_phy_addr1;
68 void *alpha_virt_addr0;
69 void *alpha_virt_addr1;
70 uint32_t alpha_mem_len;
72 uint32_t cur_ipu_alpha_buf;
74 u32 pseudo_palette[16];
84 static unsigned long default_bpp = 16;
85 static unsigned char g_dp_in_use;
86 static struct fb_info *mxcfb_info[3];
87 static int ext_clk_used;
89 static uint32_t bpp_to_pixfmt(struct fb_info *fbi)
93 debug("bpp_to_pixfmt: %d\n", fbi->var.bits_per_pixel);
96 return fbi->var.nonstd;
98 switch (fbi->var.bits_per_pixel) {
100 pixfmt = IPU_PIX_FMT_BGR24;
103 pixfmt = IPU_PIX_FMT_BGR32;
106 pixfmt = IPU_PIX_FMT_RGB565;
113 * Set fixed framebuffer parameters based on variable settings.
115 * @param info framebuffer information pointer
117 static int mxcfb_set_fix(struct fb_info *info)
119 struct fb_fix_screeninfo *fix = &info->fix;
120 struct fb_var_screeninfo *var = &info->var;
122 fix->line_length = var->xres_virtual * var->bits_per_pixel / 8;
124 fix->type = FB_TYPE_PACKED_PIXELS;
125 fix->accel = FB_ACCEL_NONE;
126 fix->visual = FB_VISUAL_TRUECOLOR;
133 static int setup_disp_channel1(struct fb_info *fbi)
135 ipu_channel_params_t params;
136 struct mxcfb_info *mxc_fbi = (struct mxcfb_info *)fbi->par;
138 memset(¶ms, 0, sizeof(params));
139 params.mem_dp_bg_sync.di = mxc_fbi->ipu_di;
141 debug("%s called\n", __func__);
143 * Assuming interlaced means yuv output, below setting also
144 * valid for mem_dc_sync. FG should have the same vmode as BG.
146 if (fbi->var.vmode & FB_VMODE_INTERLACED) {
147 params.mem_dp_bg_sync.interlaced = 1;
148 params.mem_dp_bg_sync.out_pixel_fmt =
151 if (mxc_fbi->ipu_di_pix_fmt) {
152 params.mem_dp_bg_sync.out_pixel_fmt =
153 mxc_fbi->ipu_di_pix_fmt;
155 params.mem_dp_bg_sync.out_pixel_fmt =
159 params.mem_dp_bg_sync.in_pixel_fmt = bpp_to_pixfmt(fbi);
160 if (mxc_fbi->alpha_chan_en)
161 params.mem_dp_bg_sync.alpha_chan_en = 1;
163 ipu_init_channel(mxc_fbi->ipu_ch, ¶ms);
168 static int setup_disp_channel2(struct fb_info *fbi)
171 struct mxcfb_info *mxc_fbi = (struct mxcfb_info *)fbi->par;
173 mxc_fbi->cur_ipu_buf = 1;
174 if (mxc_fbi->alpha_chan_en)
175 mxc_fbi->cur_ipu_alpha_buf = 1;
177 fbi->var.xoffset = fbi->var.yoffset = 0;
179 debug("%s: %x %d %d %d %lx %lx\n",
184 fbi->fix.line_length,
186 fbi->fix.smem_start +
187 (fbi->fix.line_length * fbi->var.yres));
189 retval = ipu_init_channel_buffer(mxc_fbi->ipu_ch, IPU_INPUT_BUFFER,
191 fbi->var.xres, fbi->var.yres,
192 fbi->fix.line_length,
193 fbi->fix.smem_start +
194 (fbi->fix.line_length * fbi->var.yres),
198 printf("ipu_init_channel_buffer error %d\n", retval);
204 * Set framebuffer parameters and change the operating mode.
206 * @param info framebuffer information pointer
208 static int mxcfb_set_par(struct fb_info *fbi)
212 ipu_di_signal_cfg_t sig_cfg;
213 struct mxcfb_info *mxc_fbi = (struct mxcfb_info *)fbi->par;
214 uint32_t out_pixel_fmt;
216 ipu_disable_channel(mxc_fbi->ipu_ch);
217 ipu_uninit_channel(mxc_fbi->ipu_ch);
220 mem_len = fbi->var.yres_virtual * fbi->fix.line_length;
221 if (!fbi->fix.smem_start || (mem_len > fbi->fix.smem_len)) {
222 if (fbi->fix.smem_start)
223 mxcfb_unmap_video_memory(fbi);
225 if (mxcfb_map_video_memory(fbi) < 0)
229 setup_disp_channel1(fbi);
231 memset(&sig_cfg, 0, sizeof(sig_cfg));
232 if (fbi->var.vmode & FB_VMODE_INTERLACED) {
233 sig_cfg.interlaced = 1;
234 out_pixel_fmt = IPU_PIX_FMT_YUV444;
236 if (mxc_fbi->ipu_di_pix_fmt)
237 out_pixel_fmt = mxc_fbi->ipu_di_pix_fmt;
239 out_pixel_fmt = IPU_PIX_FMT_RGB666;
241 if (fbi->var.vmode & FB_VMODE_ODD_FLD_FIRST) /* PAL */
242 sig_cfg.odd_field_first = 1;
243 if ((fbi->var.sync & FB_SYNC_EXT) || ext_clk_used)
245 if (fbi->var.sync & FB_SYNC_HOR_HIGH_ACT)
246 sig_cfg.Hsync_pol = 1;
247 if (fbi->var.sync & FB_SYNC_VERT_HIGH_ACT)
248 sig_cfg.Vsync_pol = 1;
249 if (!(fbi->var.sync & FB_SYNC_CLK_LAT_FALL))
251 if (fbi->var.sync & FB_SYNC_DATA_INVERT)
252 sig_cfg.data_pol = 1;
253 if (!(fbi->var.sync & FB_SYNC_OE_LOW_ACT))
254 sig_cfg.enable_pol = 1;
255 if (fbi->var.sync & FB_SYNC_CLK_IDLE_EN)
256 sig_cfg.clkidle_en = 1;
258 debug("pixclock = %ul Hz\n",
259 (u32) (PICOS2KHZ(fbi->var.pixclock) * 1000UL));
261 if (ipu_init_sync_panel(mxc_fbi->ipu_di,
262 (PICOS2KHZ(fbi->var.pixclock)) * 1000UL,
263 fbi->var.xres, fbi->var.yres,
265 fbi->var.left_margin,
267 fbi->var.right_margin,
268 fbi->var.upper_margin,
270 fbi->var.lower_margin,
272 puts("mxcfb: Error initializing panel.\n");
276 retval = setup_disp_channel2(fbi);
280 if (mxc_fbi->blank == FB_BLANK_UNBLANK)
281 ipu_enable_channel(mxc_fbi->ipu_ch);
287 * Check framebuffer variable parameters and adjust to valid values.
289 * @param var framebuffer variable parameters
291 * @param info framebuffer information pointer
293 static int mxcfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
298 if (var->xres_virtual < var->xres)
299 var->xres_virtual = var->xres;
300 if (var->yres_virtual < var->yres)
301 var->yres_virtual = var->yres;
303 if ((var->bits_per_pixel != 32) && (var->bits_per_pixel != 24) &&
304 (var->bits_per_pixel != 16) && (var->bits_per_pixel != 8))
305 var->bits_per_pixel = default_bpp;
307 switch (var->bits_per_pixel) {
311 var->red.msb_right = 0;
313 var->green.length = 3;
314 var->green.offset = 2;
315 var->green.msb_right = 0;
317 var->blue.length = 2;
318 var->blue.offset = 0;
319 var->blue.msb_right = 0;
321 var->transp.length = 0;
322 var->transp.offset = 0;
323 var->transp.msb_right = 0;
327 var->red.offset = 11;
328 var->red.msb_right = 0;
330 var->green.length = 6;
331 var->green.offset = 5;
332 var->green.msb_right = 0;
334 var->blue.length = 5;
335 var->blue.offset = 0;
336 var->blue.msb_right = 0;
338 var->transp.length = 0;
339 var->transp.offset = 0;
340 var->transp.msb_right = 0;
344 var->red.offset = 16;
345 var->red.msb_right = 0;
347 var->green.length = 8;
348 var->green.offset = 8;
349 var->green.msb_right = 0;
351 var->blue.length = 8;
352 var->blue.offset = 0;
353 var->blue.msb_right = 0;
355 var->transp.length = 0;
356 var->transp.offset = 0;
357 var->transp.msb_right = 0;
361 var->red.offset = 16;
362 var->red.msb_right = 0;
364 var->green.length = 8;
365 var->green.offset = 8;
366 var->green.msb_right = 0;
368 var->blue.length = 8;
369 var->blue.offset = 0;
370 var->blue.msb_right = 0;
372 var->transp.length = 8;
373 var->transp.offset = 24;
374 var->transp.msb_right = 0;
378 if (var->pixclock < 1000) {
379 htotal = var->xres + var->right_margin + var->hsync_len +
381 vtotal = var->yres + var->lower_margin + var->vsync_len +
383 var->pixclock = (vtotal * htotal * 6UL) / 100UL;
384 var->pixclock = KHZ2PICOS(var->pixclock);
385 printf("pixclock set for 60Hz refresh = %u ps\n",
396 static int mxcfb_map_video_memory(struct fb_info *fbi)
398 if (fbi->fix.smem_len < fbi->var.yres_virtual * fbi->fix.line_length) {
399 fbi->fix.smem_len = fbi->var.yres_virtual *
400 fbi->fix.line_length;
402 fbi->fix.smem_len = roundup(fbi->fix.smem_len, ARCH_DMA_MINALIGN);
403 fbi->screen_base = (char *)memalign(ARCH_DMA_MINALIGN,
405 fbi->fix.smem_start = (unsigned long)fbi->screen_base;
406 if (fbi->screen_base == 0) {
407 puts("Unable to allocate framebuffer memory\n");
408 fbi->fix.smem_len = 0;
409 fbi->fix.smem_start = 0;
413 debug("allocated fb @ paddr=0x%08X, size=%d.\n",
414 (uint32_t) fbi->fix.smem_start, fbi->fix.smem_len);
416 fbi->screen_size = fbi->fix.smem_len;
418 /* Clear the screen */
419 memset((char *)fbi->screen_base, 0, fbi->fix.smem_len);
424 static int mxcfb_unmap_video_memory(struct fb_info *fbi)
426 fbi->screen_base = 0;
427 fbi->fix.smem_start = 0;
428 fbi->fix.smem_len = 0;
433 * Initializes the framebuffer information pointer. After allocating
434 * sufficient memory for the framebuffer structure, the fields are
435 * filled with custom information passed in from the configurable
436 * structures. This includes information such as bits per pixel,
437 * color maps, screen width/height and RGBA offsets.
439 * @return Framebuffer structure initialized with our information
441 static struct fb_info *mxcfb_init_fbinfo(void)
443 #define BYTES_PER_LONG 4
444 #define PADDING (BYTES_PER_LONG - (sizeof(struct fb_info) % BYTES_PER_LONG))
446 struct mxcfb_info *mxcfbi;
448 int size = sizeof(struct mxcfb_info) + PADDING +
449 sizeof(struct fb_info);
451 debug("%s: %d %d %d %d\n",
455 sizeof(struct mxcfb_info),
456 sizeof(struct fb_info));
458 * Allocate sufficient memory for the fb structure
467 fbi = (struct fb_info *)p;
468 fbi->par = p + sizeof(struct fb_info) + PADDING;
470 mxcfbi = (struct mxcfb_info *)fbi->par;
471 debug("Framebuffer structures at: fbi=0x%x mxcfbi=0x%x\n",
472 (unsigned int)fbi, (unsigned int)mxcfbi);
474 fbi->var.activate = FB_ACTIVATE_NOW;
476 fbi->flags = FBINFO_FLAG_DEFAULT;
477 fbi->pseudo_palette = mxcfbi->pseudo_palette;
483 * Probe routine for the framebuffer driver. It is called during the
484 * driver binding process. The following functions are performed in
485 * this routine: Framebuffer initialization, Memory allocation and
486 * mapping, Framebuffer registration, IPU initialization.
488 * @return Appropriate error code to the kernel common code
490 static int mxcfb_probe(u32 interface_pix_fmt, uint8_t disp,
491 struct fb_videomode const *mode)
494 struct mxcfb_info *mxcfbi;
498 * Initialize FB structures
500 fbi = mxcfb_init_fbinfo();
505 mxcfbi = (struct mxcfb_info *)fbi->par;
508 mxcfbi->ipu_ch = MEM_BG_SYNC;
509 mxcfbi->blank = FB_BLANK_UNBLANK;
511 mxcfbi->ipu_ch = MEM_DC_SYNC;
512 mxcfbi->blank = FB_BLANK_POWERDOWN;
515 mxcfbi->ipu_di = disp;
517 ipu_disp_set_global_alpha(mxcfbi->ipu_ch, 1, 0x80);
518 ipu_disp_set_color_key(mxcfbi->ipu_ch, 0, 0);
519 strcpy(fbi->fix.id, "DISP3 BG");
523 mxcfb_info[mxcfbi->ipu_di] = fbi;
525 /* Need dummy values until real panel is configured */
527 mxcfbi->ipu_di_pix_fmt = interface_pix_fmt;
528 fb_videomode_to_var(&fbi->var, mode);
529 fbi->var.bits_per_pixel = 16;
530 fbi->fix.line_length = fbi->var.xres * (fbi->var.bits_per_pixel / 8);
531 fbi->fix.smem_len = fbi->var.yres_virtual * fbi->fix.line_length;
533 mxcfb_check_var(&fbi->var, fbi);
535 /* Default Y virtual size is 2x panel size */
536 fbi->var.yres_virtual = fbi->var.yres * 2;
540 /* alocate fb first */
541 if (mxcfb_map_video_memory(fbi) < 0)
546 panel.winSizeX = mode->xres;
547 panel.winSizeY = mode->yres;
548 panel.plnSizeX = mode->xres;
549 panel.plnSizeY = mode->yres;
551 panel.frameAdrs = (u32)fbi->screen_base;
552 panel.memSize = fbi->screen_size;
554 panel.gdfBytesPP = 2;
555 panel.gdfIndex = GDF_16BIT_565RGB;
557 ipu_dump_registers();
565 void ipuv3_fb_shutdown(void)
568 struct ipu_stat *stat = (struct ipu_stat *)IPU_STAT;
570 for (i = 0; i < ARRAY_SIZE(mxcfb_info); i++) {
571 struct fb_info *fbi = mxcfb_info[i];
573 struct mxcfb_info *mxc_fbi = fbi->par;
574 ipu_disable_channel(mxc_fbi->ipu_ch);
575 ipu_uninit_channel(mxc_fbi->ipu_ch);
578 for (i = 0; i < ARRAY_SIZE(stat->int_stat); i++) {
579 __raw_writel(__raw_readl(&stat->int_stat[i]),
584 void *video_hw_init(void)
590 puts("Error initializing IPU\n");
592 ret = mxcfb_probe(gpixfmt, gdisp, gmode);
593 debug("Framebuffer at 0x%x\n", (unsigned int)panel.frameAdrs);
595 return (void *)&panel;
598 void video_set_lut(unsigned int index, /* color number */
599 unsigned char r, /* red */
600 unsigned char g, /* green */
601 unsigned char b /* blue */
607 int ipuv3_fb_init(struct fb_videomode const *mode,