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 <asm/global_data.h>
17 #include <linux/string.h>
18 #include <linux/list.h>
23 #include "videomodes.h"
28 DECLARE_GLOBAL_DATA_PTR;
30 static int mxcfb_map_video_memory(struct fb_info *fbi);
31 static int mxcfb_unmap_video_memory(struct fb_info *fbi);
34 static GraphicDevice panel;
35 static struct fb_videomode const *gmode;
37 static uint32_t gpixfmt;
39 void fb_videomode_to_var(struct fb_var_screeninfo *var,
40 const struct fb_videomode *mode)
42 var->xres = mode->xres;
43 var->yres = mode->yres;
44 var->xres_virtual = mode->xres;
45 var->yres_virtual = mode->yres;
48 var->pixclock = mode->pixclock;
49 var->left_margin = mode->left_margin;
50 var->right_margin = mode->right_margin;
51 var->upper_margin = mode->upper_margin;
52 var->lower_margin = mode->lower_margin;
53 var->hsync_len = mode->hsync_len;
54 var->vsync_len = mode->vsync_len;
55 var->sync = mode->sync;
56 var->vmode = mode->vmode & FB_VMODE_MASK;
60 * Structure containing the MXC specific framebuffer information.
67 unsigned char overlay;
68 unsigned char alpha_chan_en;
69 dma_addr_t alpha_phy_addr0;
70 dma_addr_t alpha_phy_addr1;
71 void *alpha_virt_addr0;
72 void *alpha_virt_addr1;
73 uint32_t alpha_mem_len;
75 uint32_t cur_ipu_alpha_buf;
77 u32 pseudo_palette[16];
87 static unsigned long default_bpp = 16;
88 static unsigned char g_dp_in_use;
89 static struct fb_info *mxcfb_info[3];
90 static int ext_clk_used;
92 static uint32_t bpp_to_pixfmt(struct fb_info *fbi)
96 debug("bpp_to_pixfmt: %d\n", fbi->var.bits_per_pixel);
99 return fbi->var.nonstd;
101 switch (fbi->var.bits_per_pixel) {
103 pixfmt = IPU_PIX_FMT_BGR24;
106 pixfmt = IPU_PIX_FMT_BGR32;
109 pixfmt = IPU_PIX_FMT_RGB565;
116 * Set fixed framebuffer parameters based on variable settings.
118 * @param info framebuffer information pointer
120 static int mxcfb_set_fix(struct fb_info *info)
122 struct fb_fix_screeninfo *fix = &info->fix;
123 struct fb_var_screeninfo *var = &info->var;
125 fix->line_length = var->xres_virtual * var->bits_per_pixel / 8;
127 fix->type = FB_TYPE_PACKED_PIXELS;
128 fix->accel = FB_ACCEL_NONE;
129 fix->visual = FB_VISUAL_TRUECOLOR;
136 static int setup_disp_channel1(struct fb_info *fbi)
138 ipu_channel_params_t params;
139 struct mxcfb_info *mxc_fbi = (struct mxcfb_info *)fbi->par;
141 memset(¶ms, 0, sizeof(params));
142 params.mem_dp_bg_sync.di = mxc_fbi->ipu_di;
144 debug("%s called\n", __func__);
146 * Assuming interlaced means yuv output, below setting also
147 * valid for mem_dc_sync. FG should have the same vmode as BG.
149 if (fbi->var.vmode & FB_VMODE_INTERLACED) {
150 params.mem_dp_bg_sync.interlaced = 1;
151 params.mem_dp_bg_sync.out_pixel_fmt =
154 if (mxc_fbi->ipu_di_pix_fmt) {
155 params.mem_dp_bg_sync.out_pixel_fmt =
156 mxc_fbi->ipu_di_pix_fmt;
158 params.mem_dp_bg_sync.out_pixel_fmt =
162 params.mem_dp_bg_sync.in_pixel_fmt = bpp_to_pixfmt(fbi);
163 if (mxc_fbi->alpha_chan_en)
164 params.mem_dp_bg_sync.alpha_chan_en = 1;
166 ipu_init_channel(mxc_fbi->ipu_ch, ¶ms);
171 static int setup_disp_channel2(struct fb_info *fbi)
174 struct mxcfb_info *mxc_fbi = (struct mxcfb_info *)fbi->par;
176 mxc_fbi->cur_ipu_buf = 1;
177 if (mxc_fbi->alpha_chan_en)
178 mxc_fbi->cur_ipu_alpha_buf = 1;
180 fbi->var.xoffset = fbi->var.yoffset = 0;
182 debug("%s: %x %d %d %d %lx %lx\n",
187 fbi->fix.line_length,
189 fbi->fix.smem_start +
190 (fbi->fix.line_length * fbi->var.yres));
192 retval = ipu_init_channel_buffer(mxc_fbi->ipu_ch, IPU_INPUT_BUFFER,
194 fbi->var.xres, fbi->var.yres,
195 fbi->fix.line_length,
196 fbi->fix.smem_start +
197 (fbi->fix.line_length * fbi->var.yres),
201 printf("ipu_init_channel_buffer error %d\n", retval);
207 * Set framebuffer parameters and change the operating mode.
209 * @param info framebuffer information pointer
211 static int mxcfb_set_par(struct fb_info *fbi)
215 ipu_di_signal_cfg_t sig_cfg;
216 struct mxcfb_info *mxc_fbi = (struct mxcfb_info *)fbi->par;
217 uint32_t out_pixel_fmt;
219 ipu_disable_channel(mxc_fbi->ipu_ch);
220 ipu_uninit_channel(mxc_fbi->ipu_ch);
223 mem_len = fbi->var.yres_virtual * fbi->fix.line_length;
224 if (!fbi->fix.smem_start || (mem_len > fbi->fix.smem_len)) {
225 if (fbi->fix.smem_start)
226 mxcfb_unmap_video_memory(fbi);
228 if (mxcfb_map_video_memory(fbi) < 0)
232 setup_disp_channel1(fbi);
234 memset(&sig_cfg, 0, sizeof(sig_cfg));
235 if (fbi->var.vmode & FB_VMODE_INTERLACED) {
236 sig_cfg.interlaced = 1;
237 out_pixel_fmt = IPU_PIX_FMT_YUV444;
239 if (mxc_fbi->ipu_di_pix_fmt)
240 out_pixel_fmt = mxc_fbi->ipu_di_pix_fmt;
242 out_pixel_fmt = IPU_PIX_FMT_RGB666;
244 if (fbi->var.vmode & FB_VMODE_ODD_FLD_FIRST) /* PAL */
245 sig_cfg.odd_field_first = 1;
246 if ((fbi->var.sync & FB_SYNC_EXT) || ext_clk_used)
248 if (fbi->var.sync & FB_SYNC_HOR_HIGH_ACT)
249 sig_cfg.Hsync_pol = 1;
250 if (fbi->var.sync & FB_SYNC_VERT_HIGH_ACT)
251 sig_cfg.Vsync_pol = 1;
252 if (!(fbi->var.sync & FB_SYNC_CLK_LAT_FALL))
254 if (fbi->var.sync & FB_SYNC_DATA_INVERT)
255 sig_cfg.data_pol = 1;
256 if (!(fbi->var.sync & FB_SYNC_OE_LOW_ACT))
257 sig_cfg.enable_pol = 1;
258 if (fbi->var.sync & FB_SYNC_CLK_IDLE_EN)
259 sig_cfg.clkidle_en = 1;
261 debug("pixclock = %ul Hz\n",
262 (u32) (PICOS2KHZ(fbi->var.pixclock) * 1000UL));
264 if (ipu_init_sync_panel(mxc_fbi->ipu_di,
265 (PICOS2KHZ(fbi->var.pixclock)) * 1000UL,
266 fbi->var.xres, fbi->var.yres,
268 fbi->var.left_margin,
270 fbi->var.right_margin,
271 fbi->var.upper_margin,
273 fbi->var.lower_margin,
275 puts("mxcfb: Error initializing panel.\n");
279 retval = setup_disp_channel2(fbi);
283 if (mxc_fbi->blank == FB_BLANK_UNBLANK)
284 ipu_enable_channel(mxc_fbi->ipu_ch);
290 * Check framebuffer variable parameters and adjust to valid values.
292 * @param var framebuffer variable parameters
294 * @param info framebuffer information pointer
296 static int mxcfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
301 if (var->xres_virtual < var->xres)
302 var->xres_virtual = var->xres;
303 if (var->yres_virtual < var->yres)
304 var->yres_virtual = var->yres;
306 if ((var->bits_per_pixel != 32) && (var->bits_per_pixel != 24) &&
307 (var->bits_per_pixel != 16) && (var->bits_per_pixel != 8))
308 var->bits_per_pixel = default_bpp;
310 switch (var->bits_per_pixel) {
314 var->red.msb_right = 0;
316 var->green.length = 3;
317 var->green.offset = 2;
318 var->green.msb_right = 0;
320 var->blue.length = 2;
321 var->blue.offset = 0;
322 var->blue.msb_right = 0;
324 var->transp.length = 0;
325 var->transp.offset = 0;
326 var->transp.msb_right = 0;
330 var->red.offset = 11;
331 var->red.msb_right = 0;
333 var->green.length = 6;
334 var->green.offset = 5;
335 var->green.msb_right = 0;
337 var->blue.length = 5;
338 var->blue.offset = 0;
339 var->blue.msb_right = 0;
341 var->transp.length = 0;
342 var->transp.offset = 0;
343 var->transp.msb_right = 0;
347 var->red.offset = 16;
348 var->red.msb_right = 0;
350 var->green.length = 8;
351 var->green.offset = 8;
352 var->green.msb_right = 0;
354 var->blue.length = 8;
355 var->blue.offset = 0;
356 var->blue.msb_right = 0;
358 var->transp.length = 0;
359 var->transp.offset = 0;
360 var->transp.msb_right = 0;
364 var->red.offset = 16;
365 var->red.msb_right = 0;
367 var->green.length = 8;
368 var->green.offset = 8;
369 var->green.msb_right = 0;
371 var->blue.length = 8;
372 var->blue.offset = 0;
373 var->blue.msb_right = 0;
375 var->transp.length = 8;
376 var->transp.offset = 24;
377 var->transp.msb_right = 0;
381 if (var->pixclock < 1000) {
382 htotal = var->xres + var->right_margin + var->hsync_len +
384 vtotal = var->yres + var->lower_margin + var->vsync_len +
386 var->pixclock = (vtotal * htotal * 6UL) / 100UL;
387 var->pixclock = KHZ2PICOS(var->pixclock);
388 printf("pixclock set for 60Hz refresh = %u ps\n",
399 static int mxcfb_map_video_memory(struct fb_info *fbi)
401 if (fbi->fix.smem_len < fbi->var.yres_virtual * fbi->fix.line_length) {
402 fbi->fix.smem_len = fbi->var.yres_virtual *
403 fbi->fix.line_length;
405 fbi->fix.smem_len = roundup(fbi->fix.smem_len, ARCH_DMA_MINALIGN);
406 fbi->screen_base = (char *)memalign(ARCH_DMA_MINALIGN,
408 fbi->fix.smem_start = (unsigned long)fbi->screen_base;
409 if (fbi->screen_base == 0) {
410 puts("Unable to allocate framebuffer memory\n");
411 fbi->fix.smem_len = 0;
412 fbi->fix.smem_start = 0;
416 debug("allocated fb @ paddr=0x%08X, size=%d.\n",
417 (uint32_t) fbi->fix.smem_start, fbi->fix.smem_len);
419 fbi->screen_size = fbi->fix.smem_len;
421 gd->fb_base = fbi->fix.smem_start;
423 /* Clear the screen */
424 memset((char *)fbi->screen_base, 0, fbi->fix.smem_len);
429 static int mxcfb_unmap_video_memory(struct fb_info *fbi)
431 fbi->screen_base = 0;
432 fbi->fix.smem_start = 0;
433 fbi->fix.smem_len = 0;
438 * Initializes the framebuffer information pointer. After allocating
439 * sufficient memory for the framebuffer structure, the fields are
440 * filled with custom information passed in from the configurable
441 * structures. This includes information such as bits per pixel,
442 * color maps, screen width/height and RGBA offsets.
444 * @return Framebuffer structure initialized with our information
446 static struct fb_info *mxcfb_init_fbinfo(void)
448 #define BYTES_PER_LONG 4
449 #define PADDING (BYTES_PER_LONG - (sizeof(struct fb_info) % BYTES_PER_LONG))
451 struct mxcfb_info *mxcfbi;
453 int size = sizeof(struct mxcfb_info) + PADDING +
454 sizeof(struct fb_info);
456 debug("%s: %d %d %d %d\n",
460 sizeof(struct mxcfb_info),
461 sizeof(struct fb_info));
463 * Allocate sufficient memory for the fb structure
472 fbi = (struct fb_info *)p;
473 fbi->par = p + sizeof(struct fb_info) + PADDING;
475 mxcfbi = (struct mxcfb_info *)fbi->par;
476 debug("Framebuffer structures at: fbi=0x%x mxcfbi=0x%x\n",
477 (unsigned int)fbi, (unsigned int)mxcfbi);
479 fbi->var.activate = FB_ACTIVATE_NOW;
481 fbi->flags = FBINFO_FLAG_DEFAULT;
482 fbi->pseudo_palette = mxcfbi->pseudo_palette;
488 * Probe routine for the framebuffer driver. It is called during the
489 * driver binding process. The following functions are performed in
490 * this routine: Framebuffer initialization, Memory allocation and
491 * mapping, Framebuffer registration, IPU initialization.
493 * @return Appropriate error code to the kernel common code
495 static int mxcfb_probe(u32 interface_pix_fmt, uint8_t disp,
496 struct fb_videomode const *mode)
499 struct mxcfb_info *mxcfbi;
503 * Initialize FB structures
505 fbi = mxcfb_init_fbinfo();
510 mxcfbi = (struct mxcfb_info *)fbi->par;
513 mxcfbi->ipu_ch = MEM_BG_SYNC;
514 mxcfbi->blank = FB_BLANK_UNBLANK;
516 mxcfbi->ipu_ch = MEM_DC_SYNC;
517 mxcfbi->blank = FB_BLANK_POWERDOWN;
520 mxcfbi->ipu_di = disp;
522 ipu_disp_set_global_alpha(mxcfbi->ipu_ch, 1, 0x80);
523 ipu_disp_set_color_key(mxcfbi->ipu_ch, 0, 0);
524 strcpy(fbi->fix.id, "DISP3 BG");
528 mxcfb_info[mxcfbi->ipu_di] = fbi;
530 /* Need dummy values until real panel is configured */
532 mxcfbi->ipu_di_pix_fmt = interface_pix_fmt;
533 fb_videomode_to_var(&fbi->var, mode);
534 fbi->var.bits_per_pixel = 16;
535 fbi->fix.line_length = fbi->var.xres * (fbi->var.bits_per_pixel / 8);
536 fbi->fix.smem_len = fbi->var.yres_virtual * fbi->fix.line_length;
538 mxcfb_check_var(&fbi->var, fbi);
540 /* Default Y virtual size is 2x panel size */
541 fbi->var.yres_virtual = fbi->var.yres * 2;
545 /* alocate fb first */
546 if (mxcfb_map_video_memory(fbi) < 0)
551 panel.winSizeX = mode->xres;
552 panel.winSizeY = mode->yres;
553 panel.plnSizeX = mode->xres;
554 panel.plnSizeY = mode->yres;
556 panel.frameAdrs = (u32)fbi->screen_base;
557 panel.memSize = fbi->screen_size;
559 panel.gdfBytesPP = 2;
560 panel.gdfIndex = GDF_16BIT_565RGB;
562 ipu_dump_registers();
570 void ipuv3_fb_shutdown(void)
573 struct ipu_stat *stat = (struct ipu_stat *)IPU_STAT;
575 for (i = 0; i < ARRAY_SIZE(mxcfb_info); i++) {
576 struct fb_info *fbi = mxcfb_info[i];
578 struct mxcfb_info *mxc_fbi = fbi->par;
579 ipu_disable_channel(mxc_fbi->ipu_ch);
580 ipu_uninit_channel(mxc_fbi->ipu_ch);
583 for (i = 0; i < ARRAY_SIZE(stat->int_stat); i++) {
584 __raw_writel(__raw_readl(&stat->int_stat[i]),
589 void *video_hw_init(void)
595 puts("Error initializing IPU\n");
597 ret = mxcfb_probe(gpixfmt, gdisp, gmode);
598 debug("Framebuffer at 0x%x\n", (unsigned int)panel.frameAdrs);
600 return (void *)&panel;
603 void video_set_lut(unsigned int index, /* color number */
604 unsigned char r, /* red */
605 unsigned char g, /* green */
606 unsigned char b /* blue */
612 int ipuv3_fb_init(struct fb_videomode const *mode,