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