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