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