]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/video/mxc_ipuv3_fb.c
TX6 Release 2013-04-22
[karo-tx-uboot.git] / drivers / video / mxc_ipuv3_fb.c
1 /*
2  * Porting to u-boot:
3  *
4  * (C) Copyright 2010
5  * Stefano Babic, DENX Software Engineering, sbabic@denx.de
6  *
7  * IPUv3 Linux framebuffer:
8  *
9  * (C) Copyright 2004-2011 Freescale Semiconductor, Inc.
10  *
11  * See file CREDITS for list of people who contributed to this
12  * project.
13  *
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.
18  *
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.
23  *
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,
27  * MA 02111-1307 USA
28  */
29
30 /* #define DEBUG */
31 // #define DEBUG
32 #include <common.h>
33 #include <asm/errno.h>
34 #include <linux/string.h>
35 #include <linux/list.h>
36 #include <linux/fb.h>
37 #include <asm/io.h>
38 #include <malloc.h>
39 #include <lcd.h>
40 #include <ipu.h>
41 #include <video_fb.h>
42 #include "videomodes.h"
43 #include "mxcfb.h"
44
45 DECLARE_GLOBAL_DATA_PTR;
46
47 extern vidinfo_t panel_info;
48
49 void *lcd_base;                 /* Start of framebuffer memory  */
50 void *lcd_console_address;      /* Start of console buffer      */
51
52 int lcd_line_length;
53 int lcd_color_fg;
54 int lcd_color_bg;
55
56 short console_col;
57 short console_row;
58
59 static int mxcfb_map_video_memory(struct fb_info *fbi);
60 static int mxcfb_unmap_video_memory(struct fb_info *fbi);
61
62 void lcd_initcolregs(void)
63 {
64 }
65
66 void lcd_setcolreg(ushort regno, ushort red, ushort green, ushort blue)
67 {
68 }
69
70 void lcd_disable(void)
71 {
72 }
73
74 void lcd_panel_disable(void)
75 {
76 }
77
78 static void fb_videomode_to_var(struct fb_var_screeninfo *var,
79                          const struct fb_videomode *mode)
80 {
81         var->xres = mode->xres;
82         var->yres = mode->yres;
83         var->xres_virtual = mode->xres;
84         var->yres_virtual = mode->yres;
85         var->xoffset = 0;
86         var->yoffset = 0;
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;
96 }
97
98 /*
99  * Structure containing the MXC specific framebuffer information.
100  */
101 struct mxcfb_info {
102         int blank;
103         ipu_channel_t ipu_ch;
104         int ipu_di;
105         u32 ipu_di_pix_fmt;
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;
115
116         u32 pseudo_palette[16];
117 };
118
119 enum {
120         BOTH_ON,
121         SRC_ON,
122         TGT_ON,
123         BOTH_OFF
124 };
125
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;
130
131 static uint32_t bpp_to_pixfmt(struct fb_info *fbi)
132 {
133         uint32_t pixfmt = 0;
134
135         debug("bpp_to_pixfmt: %d\n", fbi->var.bits_per_pixel);
136
137         if (fbi->var.nonstd)
138                 return fbi->var.nonstd;
139
140         switch (fbi->var.bits_per_pixel) {
141         case 24:
142                 pixfmt = IPU_PIX_FMT_BGR24;
143                 break;
144         case 32:
145                 pixfmt = IPU_PIX_FMT_BGR32;
146                 break;
147         case 16:
148                 pixfmt = IPU_PIX_FMT_RGB565;
149                 break;
150         case 8:
151                 pixfmt = IPU_PIX_FMT_GENERIC;
152         }
153         return pixfmt;
154 }
155
156 /*
157  * Set fixed framebuffer parameters based on variable settings.
158  *
159  * @param       info     framebuffer information pointer
160  */
161 static int mxcfb_set_fix(struct fb_info *info)
162 {
163         struct fb_fix_screeninfo *fix = &info->fix;
164         struct fb_var_screeninfo *var = &info->var;
165
166         fix->line_length = var->xres_virtual * var->bits_per_pixel / 8;
167
168         fix->type = FB_TYPE_PACKED_PIXELS;
169         fix->accel = FB_ACCEL_NONE;
170         fix->visual = FB_VISUAL_TRUECOLOR;
171         fix->xpanstep = 1;
172         fix->ypanstep = 1;
173
174         return 0;
175 }
176
177 static int setup_disp_channel1(struct fb_info *fbi)
178 {
179         ipu_channel_params_t params;
180         struct mxcfb_info *mxc_fbi = fbi->par;
181
182         memset(&params, 0, sizeof(params));
183         params.mem_dp_bg_sync.di = mxc_fbi->ipu_di;
184
185         debug("%s called\n", __func__);
186         /*
187          * Assuming interlaced means yuv output, below setting also
188          * valid for mem_dc_sync. FG should have the same vmode as BG.
189          */
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 =
193                         IPU_PIX_FMT_YUV444;
194         } else {
195                 if (mxc_fbi->ipu_di_pix_fmt) {
196                         params.mem_dp_bg_sync.out_pixel_fmt =
197                                 mxc_fbi->ipu_di_pix_fmt;
198                 } else {
199                         params.mem_dp_bg_sync.out_pixel_fmt =
200                                 IPU_PIX_FMT_RGB666;
201                 }
202         }
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;
206
207         ipu_init_channel(mxc_fbi->ipu_ch, &params);
208
209         return 0;
210 }
211
212 static int setup_disp_channel2(struct fb_info *fbi)
213 {
214         int retval = 0;
215         struct mxcfb_info *mxc_fbi = fbi->par;
216
217         mxc_fbi->cur_ipu_buf = 1;
218         if (mxc_fbi->alpha_chan_en)
219                 mxc_fbi->cur_ipu_alpha_buf = 1;
220
221         fbi->var.xoffset = fbi->var.yoffset = 0;
222
223         debug("%s: ch: %08x xres: %d yres: %d line_length: %d mem: %08lx .. %08lx\n",
224                 __func__,
225                 mxc_fbi->ipu_ch,
226                 fbi->var.xres,
227                 fbi->var.yres,
228                 fbi->fix.line_length,
229                 fbi->fix.smem_start,
230                 fbi->fix.smem_start +
231                 (fbi->fix.line_length * fbi->var.yres) - 1);
232
233         retval = ipu_init_channel_buffer(mxc_fbi->ipu_ch, IPU_INPUT_BUFFER,
234                                          bpp_to_pixfmt(fbi),
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),
239                                          fbi->fix.smem_start,
240                                          0, 0);
241         if (retval)
242                 printf("ipu_init_channel_buffer error %d\n", retval);
243
244         return retval;
245 }
246
247 /*
248  * Set framebuffer parameters and change the operating mode.
249  *
250  * @param       info     framebuffer information pointer
251  */
252 static int mxcfb_set_par(struct fb_info *fbi)
253 {
254         int retval = 0;
255         u32 mem_len;
256         ipu_di_signal_cfg_t sig_cfg;
257         struct mxcfb_info *mxc_fbi = fbi->par;
258         uint32_t out_pixel_fmt;
259
260         ipu_disable_channel(mxc_fbi->ipu_ch);
261         ipu_uninit_channel(mxc_fbi->ipu_ch);
262         mxcfb_set_fix(fbi);
263
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);
268
269                 if (mxcfb_map_video_memory(fbi) < 0)
270                         return -ENOMEM;
271         }
272
273         setup_disp_channel1(fbi);
274
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;
279         } else {
280                 if (mxc_fbi->ipu_di_pix_fmt)
281                         out_pixel_fmt = mxc_fbi->ipu_di_pix_fmt;
282                 else
283                         out_pixel_fmt = IPU_PIX_FMT_RGB666;
284         }
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)
288                 sig_cfg.ext_clk = 1;
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))
294                 sig_cfg.clk_pol = 1;
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;
301
302         debug("pixclock = %lu.%03lu MHz\n",
303                 PICOS2KHZ(fbi->var.pixclock) / 1000,
304                 PICOS2KHZ(fbi->var.pixclock) % 1000);
305
306         retval = ipu_init_sync_panel(mxc_fbi->ipu_di,
307                                 (PICOS2KHZ(fbi->var.pixclock)) * 1000UL,
308                                 fbi->var.xres, fbi->var.yres,
309                                 out_pixel_fmt,
310                                 fbi->var.left_margin,
311                                 fbi->var.hsync_len,
312                                 fbi->var.right_margin,
313                                 fbi->var.upper_margin,
314                                 fbi->var.vsync_len,
315                                 fbi->var.lower_margin,
316                                 0, sig_cfg);
317         if (retval != 0) {
318                 printf("mxc_ipuv3_fb: Error %d initializing panel\n", retval);
319                 return retval;
320         }
321
322         retval = setup_disp_channel2(fbi);
323         if (retval)
324                 return retval;
325
326         if (mxc_fbi->blank == FB_BLANK_UNBLANK)
327                 ipu_enable_channel(mxc_fbi->ipu_ch);
328
329         return retval;
330 }
331
332 /*
333  * Check framebuffer variable parameters and adjust to valid values.
334  *
335  * @param       var      framebuffer variable parameters
336  *
337  * @param       info     framebuffer information pointer
338  */
339 static int mxcfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
340 {
341         u32 vtotal;
342         u32 htotal;
343
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;
348
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;
352
353         switch (var->bits_per_pixel) {
354         case 8:
355                 var->red.length = 3;
356                 var->red.offset = 5;
357                 var->red.msb_right = 0;
358
359                 var->green.length = 3;
360                 var->green.offset = 2;
361                 var->green.msb_right = 0;
362
363                 var->blue.length = 2;
364                 var->blue.offset = 0;
365                 var->blue.msb_right = 0;
366
367                 var->transp.length = 0;
368                 var->transp.offset = 0;
369                 var->transp.msb_right = 0;
370                 break;
371         case 16:
372                 var->red.length = 5;
373                 var->red.offset = 11;
374                 var->red.msb_right = 0;
375
376                 var->green.length = 6;
377                 var->green.offset = 5;
378                 var->green.msb_right = 0;
379
380                 var->blue.length = 5;
381                 var->blue.offset = 0;
382                 var->blue.msb_right = 0;
383
384                 var->transp.length = 0;
385                 var->transp.offset = 0;
386                 var->transp.msb_right = 0;
387                 break;
388         case 24:
389                 var->red.length = 8;
390                 var->red.offset = 16;
391                 var->red.msb_right = 0;
392
393                 var->green.length = 8;
394                 var->green.offset = 8;
395                 var->green.msb_right = 0;
396
397                 var->blue.length = 8;
398                 var->blue.offset = 0;
399                 var->blue.msb_right = 0;
400
401                 var->transp.length = 0;
402                 var->transp.offset = 0;
403                 var->transp.msb_right = 0;
404                 break;
405         case 32:
406                 var->red.length = 8;
407                 var->red.offset = 16;
408                 var->red.msb_right = 0;
409
410                 var->green.length = 8;
411                 var->green.offset = 8;
412                 var->green.msb_right = 0;
413
414                 var->blue.length = 8;
415                 var->blue.offset = 0;
416                 var->blue.msb_right = 0;
417
418                 var->transp.length = 8;
419                 var->transp.offset = 24;
420                 var->transp.msb_right = 0;
421                 break;
422         }
423
424         if (var->pixclock < 1000) {
425                 htotal = var->xres + var->right_margin + var->hsync_len +
426                     var->left_margin;
427                 vtotal = var->yres + var->lower_margin + var->vsync_len +
428                     var->upper_margin;
429                 var->pixclock = (vtotal * htotal * 6UL) / 100UL;
430                 var->pixclock = KHZ2PICOS(var->pixclock);
431                 printf("pixclock set for 60Hz refresh = %u ps\n",
432                         var->pixclock);
433         }
434
435         var->height = -1;
436         var->width = -1;
437         var->grayscale = 0;
438
439         return 0;
440 }
441
442 static int mxcfb_map_video_memory(struct fb_info *fbi)
443 {
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;
447         }
448         if (gd->fb_base)
449                 fbi->screen_base = (void *)gd->fb_base;
450         else
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;
455                 return -EBUSY;
456         }
457         fbi->fix.smem_start = (unsigned long)fbi->screen_base;
458
459         debug("allocated fb @ paddr=0x%08X, size=%d.\n",
460                 (uint32_t) fbi->fix.smem_start, fbi->fix.smem_len);
461
462         fbi->screen_size = fbi->fix.smem_len;
463
464         return 0;
465 }
466
467 static int mxcfb_unmap_video_memory(struct fb_info *fbi)
468 {
469         fbi->screen_base = 0;
470         fbi->fix.smem_start = 0;
471         fbi->fix.smem_len = 0;
472         return 0;
473 }
474
475 /*
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.
481  *
482  * @return      Framebuffer structure initialized with our information
483  */
484 static struct fb_info *mxcfb_init_fbinfo(void)
485 {
486         struct fb_info *fbi;
487         struct mxcfb_info *mxcfbi;
488         void *p;
489         int size = ALIGN(sizeof(struct mxcfb_info), sizeof(long)) +
490                 sizeof(struct fb_info);
491
492         debug("%s: %d %d %d\n",
493                 __func__,
494                 size,
495                 sizeof(struct mxcfb_info),
496                 sizeof(struct fb_info));
497         /*
498          * Allocate sufficient memory for the fb structure
499          */
500
501         p = malloc(size);
502         if (!p)
503                 return NULL;
504
505         memset(p, 0, size);
506
507         fbi = p;
508         fbi->par = p + ALIGN(sizeof(struct fb_info), sizeof(long));
509
510         mxcfbi = fbi->par;
511         debug("Framebuffer structures at: fbi=%p mxcfbi=%p\n",
512                 fbi, mxcfbi);
513
514         fbi->var.activate = FB_ACTIVATE_NOW;
515
516         fbi->flags = FBINFO_FLAG_DEFAULT;
517         fbi->pseudo_palette = mxcfbi->pseudo_palette;
518
519         return fbi;
520 }
521
522 /*
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.
527  *
528  * @return      Appropriate error code to the kernel common code
529  */
530 static int mxcfb_probe(u32 interface_pix_fmt, struct fb_videomode *mode, int di)
531 {
532         struct fb_info *fbi;
533         struct mxcfb_info *mxcfbi;
534
535         /*
536          * Initialize FB structures
537          */
538         fbi = mxcfb_init_fbinfo();
539         if (!fbi)
540                 return -ENOMEM;
541
542         mxcfbi = fbi->par;
543
544         if (!g_dp_in_use) {
545                 mxcfbi->ipu_ch = MEM_BG_SYNC;
546                 mxcfbi->blank = FB_BLANK_UNBLANK;
547         } else {
548                 mxcfbi->ipu_ch = MEM_DC_SYNC;
549                 mxcfbi->blank = FB_BLANK_POWERDOWN;
550         }
551
552         mxcfbi->ipu_di = di;
553
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");
557
558         g_dp_in_use = 1;
559
560         mxcfb_info[mxcfbi->ipu_di] = fbi;
561
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;
565
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;
571
572         mxcfb_check_var(&fbi->var, fbi);
573         mxcfb_set_fix(fbi);
574
575         /* allocate fb first */
576         if (mxcfb_map_video_memory(fbi) < 0)
577                 return -ENOMEM;
578
579         mxcfb_set_par(fbi);
580
581         lcd_line_length = (panel_info.vl_col * NBITS(panel_info.vl_bpix)) / 8;
582
583         debug("MXC IPUV3 configured\n"
584                 "XRES = %d YRES = %d BitsXpixel = %d\n",
585                 panel_info.vl_col,
586                 panel_info.vl_row,
587                 panel_info.vl_bpix);
588
589         ipu_dump_registers();
590
591         return 0;
592 }
593
594 ulong calc_fbsize(void)
595 {
596         return (panel_info.vl_col * panel_info.vl_row *
597                 NBITS(panel_info.vl_bpix)) / 8;
598 }
599
600 int overwrite_console(void)
601 {
602         /* Keep stdout / stderr on serial, our LCD is for splashscreen only */
603         return 1;
604 }
605
606 #if 0
607 void lcd_ctrl_init(void *lcdbase)
608 {
609         u32 mem_len = panel_info.vl_col *
610                 panel_info.vl_row *
611                 NBITS(panel_info.vl_bpix) / 8;
612
613         /*
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.
616          */
617         if (!lcdbase)
618                 return;
619
620         memset(lcdbase, 0, mem_len);
621 }
622 #endif
623
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)
626 {
627         int ret;
628
629 //      default_bpp = bpp;
630
631         ret = ipu_probe(di, di_clk_parent, di_clk_val);
632         if (ret) {
633                 printf("Error initializing IPU\n");
634                 return ret;
635         }
636
637         debug("Framebuffer at %p\n", lcd_base);
638         ret = mxcfb_probe(interface_pix_fmt, mode, di);
639
640         return ret;
641 }