]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/video/mxc_ipuv3_fb.c
8b3ea8ae6a4af9b73edd68bf3003f71268cd37a5
[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  * MX51 Linux framebuffer:
8  *
9  * (C) Copyright 2004-2010 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 #include <common.h>
31 #include <asm/errno.h>
32 #include <linux/string.h>
33 #include <linux/list.h>
34 #include <linux/fb.h>
35 #include <asm/io.h>
36 #include <malloc.h>
37 #include <video_fb.h>
38 #include "videomodes.h"
39 #include "ipu.h"
40 #include "mxcfb.h"
41
42 DECLARE_GLOBAL_DATA_PTR;
43
44 static int mxcfb_map_video_memory(struct fb_info *fbi);
45 static int mxcfb_unmap_video_memory(struct fb_info *fbi);
46
47 /* graphics setup */
48 static GraphicDevice panel;
49 static struct fb_videomode *gmode;
50 static uint8_t gdisp;
51 static uint32_t gpixfmt;
52
53 void fb_videomode_to_var(struct fb_var_screeninfo *var,
54                          const struct fb_videomode *mode)
55 {
56         var->xres = mode->xres;
57         var->yres = mode->yres;
58         var->xres_virtual = mode->xres;
59         var->yres_virtual = mode->yres;
60         var->xoffset = 0;
61         var->yoffset = 0;
62         var->pixclock = mode->pixclock;
63         var->left_margin = mode->left_margin;
64         var->right_margin = mode->right_margin;
65         var->upper_margin = mode->upper_margin;
66         var->lower_margin = mode->lower_margin;
67         var->hsync_len = mode->hsync_len;
68         var->vsync_len = mode->vsync_len;
69         var->sync = mode->sync;
70         var->vmode = mode->vmode & FB_VMODE_MASK;
71 }
72
73 /*
74  * Structure containing the MXC specific framebuffer information.
75  */
76 struct mxcfb_info {
77         int blank;
78         ipu_channel_t ipu_ch;
79         int ipu_di;
80         u32 ipu_di_pix_fmt;
81         unsigned char overlay;
82         unsigned char alpha_chan_en;
83         dma_addr_t alpha_phy_addr0;
84         dma_addr_t alpha_phy_addr1;
85         void *alpha_virt_addr0;
86         void *alpha_virt_addr1;
87         uint32_t alpha_mem_len;
88         uint32_t cur_ipu_buf;
89         uint32_t cur_ipu_alpha_buf;
90
91         u32 pseudo_palette[16];
92 };
93
94 enum {
95         BOTH_ON,
96         SRC_ON,
97         TGT_ON,
98         BOTH_OFF
99 };
100
101 static unsigned long default_bpp = 16;
102 static unsigned char g_dp_in_use;
103 static struct fb_info *mxcfb_info[3];
104 static int ext_clk_used;
105
106 static uint32_t bpp_to_pixfmt(struct fb_info *fbi)
107 {
108         uint32_t pixfmt = 0;
109
110         debug("bpp_to_pixfmt: %d\n", fbi->var.bits_per_pixel);
111
112         if (fbi->var.nonstd)
113                 return fbi->var.nonstd;
114
115         switch (fbi->var.bits_per_pixel) {
116         case 32:
117                 pixfmt = IPU_PIX_FMT_BGR32;
118                 break;
119         case 16:
120                 pixfmt = IPU_PIX_FMT_RGB565;
121                 break;
122         }
123         return pixfmt;
124 }
125
126 /*
127  * Set fixed framebuffer parameters based on variable settings.
128  *
129  * @param       info     framebuffer information pointer
130  */
131 static int mxcfb_set_fix(struct fb_info *info)
132 {
133         struct fb_fix_screeninfo *fix = &info->fix;
134         struct fb_var_screeninfo *var = &info->var;
135
136         fix->line_length = var->xres_virtual * var->bits_per_pixel / 8;
137
138         fix->type = FB_TYPE_PACKED_PIXELS;
139         fix->accel = FB_ACCEL_NONE;
140         fix->visual = FB_VISUAL_TRUECOLOR;
141         fix->xpanstep = 1;
142         fix->ypanstep = 1;
143
144         return 0;
145 }
146
147 static int setup_disp_channel1(struct fb_info *fbi)
148 {
149         ipu_channel_params_t params;
150         struct mxcfb_info *mxc_fbi = fbi->par;
151
152         memset(&params, 0, sizeof(params));
153         params.mem_dp_bg_sync.di = mxc_fbi->ipu_di;
154
155         debug("%s called\n", __func__);
156         /*
157          * Assuming interlaced means yuv output, below setting also
158          * valid for mem_dc_sync. FG should have the same vmode as BG.
159          */
160         if (fbi->var.vmode & FB_VMODE_INTERLACED) {
161                 params.mem_dp_bg_sync.interlaced = 1;
162                 params.mem_dp_bg_sync.out_pixel_fmt =
163                         IPU_PIX_FMT_YUV444;
164         } else {
165                 if (mxc_fbi->ipu_di_pix_fmt) {
166                         params.mem_dp_bg_sync.out_pixel_fmt =
167                                 mxc_fbi->ipu_di_pix_fmt;
168                 } else {
169                         params.mem_dp_bg_sync.out_pixel_fmt =
170                                 IPU_PIX_FMT_RGB666;
171                 }
172         }
173         params.mem_dp_bg_sync.in_pixel_fmt = bpp_to_pixfmt(fbi);
174         if (mxc_fbi->alpha_chan_en)
175                 params.mem_dp_bg_sync.alpha_chan_en = 1;
176
177         ipu_init_channel(mxc_fbi->ipu_ch, &params);
178
179         return 0;
180 }
181
182 static int setup_disp_channel2(struct fb_info *fbi)
183 {
184         int retval = 0;
185         struct mxcfb_info *mxc_fbi = fbi->par;
186
187         mxc_fbi->cur_ipu_buf = 1;
188         if (mxc_fbi->alpha_chan_en)
189                 mxc_fbi->cur_ipu_alpha_buf = 1;
190
191         fbi->var.xoffset = fbi->var.yoffset = 0;
192
193         debug("%s: ch: %08x xres: %d yres: %d line_length: %d mem: %08lx .. %08lx\n",
194                 __func__,
195                 mxc_fbi->ipu_ch,
196                 fbi->var.xres,
197                 fbi->var.yres,
198                 fbi->fix.line_length,
199                 fbi->fix.smem_start,
200                 fbi->fix.smem_start +
201                 (fbi->fix.line_length * fbi->var.yres) - 1);
202
203         retval = ipu_init_channel_buffer(mxc_fbi->ipu_ch, IPU_INPUT_BUFFER,
204                                          bpp_to_pixfmt(fbi),
205                                          fbi->var.xres, fbi->var.yres,
206                                          fbi->fix.line_length,
207                                          fbi->fix.smem_start +
208                                          (fbi->fix.line_length * fbi->var.yres),
209                                          fbi->fix.smem_start,
210                                          0, 0);
211         if (retval)
212                 printf("ipu_init_channel_buffer error %d\n", retval);
213
214         return retval;
215 }
216
217 /*
218  * Set framebuffer parameters and change the operating mode.
219  *
220  * @param       info     framebuffer information pointer
221  */
222 static int mxcfb_set_par(struct fb_info *fbi)
223 {
224         int retval = 0;
225         u32 mem_len;
226         ipu_di_signal_cfg_t sig_cfg;
227         struct mxcfb_info *mxc_fbi = fbi->par;
228         uint32_t out_pixel_fmt;
229
230         ipu_disable_channel(mxc_fbi->ipu_ch);
231         ipu_uninit_channel(mxc_fbi->ipu_ch);
232         mxcfb_set_fix(fbi);
233
234         mem_len = fbi->var.yres_virtual * fbi->fix.line_length;
235         if (!fbi->fix.smem_start || (mem_len > fbi->fix.smem_len)) {
236                 if (fbi->fix.smem_start)
237                         mxcfb_unmap_video_memory(fbi);
238
239                 if (mxcfb_map_video_memory(fbi) < 0)
240                         return -ENOMEM;
241         }
242
243         setup_disp_channel1(fbi);
244
245         memset(&sig_cfg, 0, sizeof(sig_cfg));
246         if (fbi->var.vmode & FB_VMODE_INTERLACED) {
247                 sig_cfg.interlaced = 1;
248                 out_pixel_fmt = IPU_PIX_FMT_YUV444;
249         } else {
250                 if (mxc_fbi->ipu_di_pix_fmt)
251                         out_pixel_fmt = mxc_fbi->ipu_di_pix_fmt;
252                 else
253                         out_pixel_fmt = IPU_PIX_FMT_RGB666;
254         }
255         if (fbi->var.vmode & FB_VMODE_ODD_FLD_FIRST) /* PAL */
256                 sig_cfg.odd_field_first = 1;
257         if ((fbi->var.sync & FB_SYNC_EXT) || ext_clk_used)
258                 sig_cfg.ext_clk = 1;
259         if (fbi->var.sync & FB_SYNC_HOR_HIGH_ACT)
260                 sig_cfg.Hsync_pol = 1;
261         if (fbi->var.sync & FB_SYNC_VERT_HIGH_ACT)
262                 sig_cfg.Vsync_pol = 1;
263         if (!(fbi->var.sync & FB_SYNC_CLK_LAT_FALL))
264                 sig_cfg.clk_pol = 1;
265         if (fbi->var.sync & FB_SYNC_DATA_INVERT)
266                 sig_cfg.data_pol = 1;
267         if (!(fbi->var.sync & FB_SYNC_OE_LOW_ACT))
268                 sig_cfg.enable_pol = 1;
269         if (fbi->var.sync & FB_SYNC_CLK_IDLE_EN)
270                 sig_cfg.clkidle_en = 1;
271
272         debug("pixclock = %lu.%03lu MHz\n",
273                 PICOS2KHZ(fbi->var.pixclock) / 1000,
274                 PICOS2KHZ(fbi->var.pixclock) % 1000);
275
276         retval = ipu_init_sync_panel(mxc_fbi->ipu_di,
277                                 (PICOS2KHZ(fbi->var.pixclock)) * 1000UL,
278                                 fbi->var.xres, fbi->var.yres,
279                                 out_pixel_fmt,
280                                 fbi->var.left_margin,
281                                 fbi->var.hsync_len,
282                                 fbi->var.right_margin,
283                                 fbi->var.upper_margin,
284                                 fbi->var.vsync_len,
285                                 fbi->var.lower_margin,
286                                 0, sig_cfg);
287         if (retval != 0) {
288                 printf("mxc_ipuv3_fb: Error %d initializing panel\n", retval);
289                 return retval;
290         }
291
292         retval = setup_disp_channel2(fbi);
293         if (retval)
294                 return retval;
295
296         if (mxc_fbi->blank == FB_BLANK_UNBLANK)
297                 ipu_enable_channel(mxc_fbi->ipu_ch);
298
299         return retval;
300 }
301
302 /*
303  * Check framebuffer variable parameters and adjust to valid values.
304  *
305  * @param       var      framebuffer variable parameters
306  *
307  * @param       info     framebuffer information pointer
308  */
309 static int mxcfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
310 {
311         u32 vtotal;
312         u32 htotal;
313
314         if (var->xres_virtual < var->xres)
315                 var->xres_virtual = var->xres;
316         if (var->yres_virtual < var->yres)
317                 var->yres_virtual = var->yres;
318
319         if ((var->bits_per_pixel != 32) && (var->bits_per_pixel != 16) &&
320                 (var->bits_per_pixel != 8))
321                 var->bits_per_pixel = default_bpp;
322
323         switch (var->bits_per_pixel) {
324         case 8:
325                 var->red.length = 3;
326                 var->red.offset = 5;
327                 var->red.msb_right = 0;
328
329                 var->green.length = 3;
330                 var->green.offset = 2;
331                 var->green.msb_right = 0;
332
333                 var->blue.length = 2;
334                 var->blue.offset = 0;
335                 var->blue.msb_right = 0;
336
337                 var->transp.length = 0;
338                 var->transp.offset = 0;
339                 var->transp.msb_right = 0;
340                 break;
341         case 16:
342                 var->red.length = 5;
343                 var->red.offset = 11;
344                 var->red.msb_right = 0;
345
346                 var->green.length = 6;
347                 var->green.offset = 5;
348                 var->green.msb_right = 0;
349
350                 var->blue.length = 5;
351                 var->blue.offset = 0;
352                 var->blue.msb_right = 0;
353
354                 var->transp.length = 0;
355                 var->transp.offset = 0;
356                 var->transp.msb_right = 0;
357                 break;
358
359         case 32:
360                 var->red.length = 8;
361                 var->red.offset = 16;
362                 var->red.msb_right = 0;
363
364                 var->green.length = 8;
365                 var->green.offset = 8;
366                 var->green.msb_right = 0;
367
368                 var->blue.length = 8;
369                 var->blue.offset = 0;
370                 var->blue.msb_right = 0;
371
372                 var->transp.length = 8;
373                 var->transp.offset = 24;
374                 var->transp.msb_right = 0;
375                 break;
376         }
377
378         if (var->pixclock < 1000) {
379                 htotal = var->xres + var->right_margin + var->hsync_len +
380                     var->left_margin;
381                 vtotal = var->yres + var->lower_margin + var->vsync_len +
382                     var->upper_margin;
383                 var->pixclock = (vtotal * htotal * 6UL) / 100UL;
384                 var->pixclock = KHZ2PICOS(var->pixclock);
385                 printf("pixclock set for 60Hz refresh = %u ps\n",
386                         var->pixclock);
387         }
388
389         var->height = -1;
390         var->width = -1;
391         var->grayscale = 0;
392
393         return 0;
394 }
395
396 static int mxcfb_map_video_memory(struct fb_info *fbi)
397 {
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;
401         }
402         if (gd->fb_base)
403                 fbi->screen_base = (void *)gd->fb_base;
404         else
405                 fbi->screen_base = malloc(fbi->fix.smem_len);
406         if (fbi->screen_base == 0) {
407                 puts("Unable to allocate framebuffer memory\n");
408                 fbi->fix.smem_len = 0;
409                 return -EBUSY;
410         }
411         fbi->fix.smem_start = (unsigned long)fbi->screen_base;
412
413         debug("allocated fb @ paddr=0x%08X, size=%d.\n",
414                 (uint32_t) fbi->fix.smem_start, fbi->fix.smem_len);
415
416         fbi->screen_size = fbi->fix.smem_len;
417
418         /* Clear the screen */
419         memset(fbi->screen_base, 0, fbi->fix.smem_len);
420
421         return 0;
422 }
423
424 static int mxcfb_unmap_video_memory(struct fb_info *fbi)
425 {
426         fbi->screen_base = 0;
427         fbi->fix.smem_start = 0;
428         fbi->fix.smem_len = 0;
429         return 0;
430 }
431
432 /*
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.
438  *
439  * @return      Framebuffer structure initialized with our information
440  */
441 static struct fb_info *mxcfb_init_fbinfo(void)
442 {
443         struct fb_info *fbi;
444         struct mxcfb_info *mxcfbi;
445         void *p;
446         int size = ALIGN(sizeof(struct mxcfb_info), sizeof(long)) +
447                 sizeof(struct fb_info);
448
449         debug("%s: %d %d %d\n",
450                 __func__,
451                 size,
452                 sizeof(struct mxcfb_info),
453                 sizeof(struct fb_info));
454         /*
455          * Allocate sufficient memory for the fb structure
456          */
457
458         p = malloc(size);
459         if (!p)
460                 return NULL;
461
462         memset(p, 0, size);
463
464         fbi = p;
465         fbi->par = p + ALIGN(sizeof(struct fb_info), sizeof(long));
466
467         mxcfbi = (struct mxcfb_info *)fbi->par;
468         debug("Framebuffer structures at: fbi=%p mxcfbi=%p\n",
469                 fbi, mxcfbi);
470
471         fbi->var.activate = FB_ACTIVATE_NOW;
472
473         fbi->flags = FBINFO_FLAG_DEFAULT;
474         fbi->pseudo_palette = mxcfbi->pseudo_palette;
475
476         return fbi;
477 }
478
479 /*
480  * Probe routine for the framebuffer driver. It is called during the
481  * driver binding process.      The following functions are performed in
482  * this routine: Framebuffer initialization, Memory allocation and
483  * mapping, Framebuffer registration, IPU initialization.
484  *
485  * @return      Appropriate error code to the kernel common code
486  */
487 static int mxcfb_probe(u32 interface_pix_fmt, uint8_t disp,
488                         struct fb_videomode *mode)
489 {
490         struct fb_info *fbi;
491         struct mxcfb_info *mxcfbi;
492         int ret = 0;
493
494         /*
495          * Initialize FB structures
496          */
497         fbi = mxcfb_init_fbinfo();
498         if (!fbi) {
499                 ret = -ENOMEM;
500                 goto err0;
501         }
502         mxcfbi = (struct mxcfb_info *)fbi->par;
503
504         if (!g_dp_in_use) {
505                 mxcfbi->ipu_ch = MEM_BG_SYNC;
506                 mxcfbi->blank = FB_BLANK_UNBLANK;
507         } else {
508                 mxcfbi->ipu_ch = MEM_DC_SYNC;
509                 mxcfbi->blank = FB_BLANK_POWERDOWN;
510         }
511
512         mxcfbi->ipu_di = disp;
513
514         ipu_disp_set_global_alpha(mxcfbi->ipu_ch, 1, 0x80);
515         ipu_disp_set_color_key(mxcfbi->ipu_ch, 0, 0);
516         strcpy(fbi->fix.id, "DISP3 BG");
517
518         g_dp_in_use = 1;
519
520         mxcfb_info[mxcfbi->ipu_di] = fbi;
521
522         /* Need dummy values until real panel is configured */
523
524         mxcfbi->ipu_di_pix_fmt = interface_pix_fmt;
525         fb_videomode_to_var(&fbi->var, mode);
526         fbi->var.bits_per_pixel = default_bpp;
527         fbi->fix.line_length = fbi->var.xres * (fbi->var.bits_per_pixel / 8);
528         fbi->fix.smem_len = fbi->var.yres_virtual * fbi->fix.line_length;
529
530         mxcfb_check_var(&fbi->var, fbi);
531
532         /* Default Y virtual size is 2x panel size */
533         fbi->var.yres_virtual = fbi->var.yres * 2;
534
535         mxcfb_set_fix(fbi);
536
537         /* alocate fb first */
538         if (mxcfb_map_video_memory(fbi) < 0)
539                 return -ENOMEM;
540
541         mxcfb_set_par(fbi);
542
543         panel.winSizeX = mode->xres;
544         panel.winSizeY = mode->yres;
545         panel.plnSizeX = mode->xres;
546         panel.plnSizeY = mode->yres;
547
548         panel.frameAdrs = (u32)fbi->screen_base;
549         panel.memSize = fbi->screen_size;
550
551         switch (fbi->var.bits_per_pixel) {
552         case 8:
553                 panel.gdfBytesPP = 1;
554                 panel.gdfIndex = GDF__8BIT_INDEX;
555                 break;
556
557         case 16:
558                 panel.gdfBytesPP = 2;
559                 panel.gdfIndex = GDF_16BIT_565RGB;
560                 break;
561
562         case 32:
563                 panel.gdfBytesPP = 4;
564                 panel.gdfIndex = GDF_32BIT_X888RGB;
565                 break;
566
567         default:
568                 hang();
569         }
570
571         ipu_dump_registers();
572
573         return 0;
574
575 err0:
576         return ret;
577 }
578
579 void *video_hw_init(void)
580 {
581         int ret;
582
583         ret = ipu_probe();
584         if (ret)
585                 puts("Error initializing IPU\n");
586
587         ret = mxcfb_probe(gpixfmt, gdisp, gmode);
588         debug("Framebuffer at 0x%08x\n", panel.frameAdrs);
589
590         return &panel;
591 }
592
593 void video_set_lut(unsigned int index, /* color number */
594                         unsigned char r,    /* red */
595                         unsigned char g,    /* green */
596                         unsigned char b     /* blue */
597                         )
598 {
599         return;
600 }
601
602 int mx5_fb_init(struct fb_videomode *mode, uint8_t disp, uint32_t pixfmt, int bpp)
603 {
604         gmode = mode;
605         gdisp = disp;
606         gpixfmt = pixfmt;
607         default_bpp = bpp;
608
609         return 0;
610 }