2 * linux/drivers/video/imxfb.c
4 * Freescale i.MX Frame Buffer device driver
6 * Copyright (C) 2004 Sascha Hauer, Pengutronix
7 * Based on acornfb.c Copyright (C) Russell King.
9 * This file is subject to the terms and conditions of the GNU General Public
10 * License. See the file COPYING in the main directory of this archive for
13 * Please direct your questions and comments on this driver to the following
16 * linux-arm-kernel@lists.arm.linux.org.uk
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/errno.h>
23 #include <linux/string.h>
24 #include <linux/interrupt.h>
25 #include <linux/slab.h>
28 #include <linux/delay.h>
29 #include <linux/init.h>
30 #include <linux/ioport.h>
31 #include <linux/cpufreq.h>
32 #include <linux/platform_device.h>
33 #include <linux/dma-mapping.h>
36 #include <mach/imxfb.h>
39 * Complain if VAR is out of range.
45 #define DRIVER_NAME "imx-fb"
49 #define LCDC_SIZE 0x04
50 #define SIZE_XMAX(x) ((((x) >> 4) & 0x3f) << 20)
51 #define SIZE_YMAX(y) ((y) & 0x1ff)
54 #define VPW_VPW(x) ((x) & 0x3ff)
56 #define LCDC_CPOS 0x0C
57 #define CPOS_CC1 (1<<31)
58 #define CPOS_CC0 (1<<30)
59 #define CPOS_OP (1<<28)
60 #define CPOS_CXP(x) (((x) & 3ff) << 16)
61 #define CPOS_CYP(y) ((y) & 0x1ff)
63 #define LCDC_LCWHB 0x10
64 #define LCWHB_BK_EN (1<<31)
65 #define LCWHB_CW(w) (((w) & 0x1f) << 24)
66 #define LCWHB_CH(h) (((h) & 0x1f) << 16)
67 #define LCWHB_BD(x) ((x) & 0xff)
69 #define LCDC_LCHCC 0x14
70 #define LCHCC_CUR_COL_R(r) (((r) & 0x1f) << 11)
71 #define LCHCC_CUR_COL_G(g) (((g) & 0x3f) << 5)
72 #define LCHCC_CUR_COL_B(b) ((b) & 0x1f)
77 #define HCR_H_WIDTH(x) (((x) & 0x3f) << 26)
78 #define HCR_H_WAIT_1(x) (((x) & 0xff) << 8)
79 #define HCR_H_WAIT_2(x) ((x) & 0xff)
82 #define VCR_V_WIDTH(x) (((x) & 0x3f) << 26)
83 #define VCR_V_WAIT_1(x) (((x) & 0xff) << 8)
84 #define VCR_V_WAIT_2(x) ((x) & 0xff)
87 #define POS_POS(x) ((x) & 1f)
89 #define LCDC_LSCR1 0x28
90 /* bit fields in imxfb.h */
92 #define LCDC_PWMR 0x2C
93 /* bit fields in imxfb.h */
95 #define LCDC_DMACR 0x30
96 /* bit fields in imxfb.h */
98 #define LCDC_RMCR 0x34
99 #define RMCR_LCDC_EN (1<<1)
100 #define RMCR_SELF_REF (1<<0)
102 #define LCDC_LCDICR 0x38
103 #define LCDICR_INT_SYN (1<<2)
104 #define LCDICR_INT_CON (1)
106 #define LCDC_LCDISR 0x40
107 #define LCDISR_UDR_ERR (1<<3)
108 #define LCDISR_ERR_RES (1<<2)
109 #define LCDISR_EOF (1<<1)
110 #define LCDISR_BOF (1<<0)
112 static struct imxfb_rgb def_rgb_16 = {
113 .red = { .offset = 8, .length = 4, },
114 .green = { .offset = 4, .length = 4, },
115 .blue = { .offset = 0, .length = 4, },
116 .transp = { .offset = 0, .length = 0, },
119 static struct imxfb_rgb def_rgb_8 = {
120 .red = { .offset = 0, .length = 8, },
121 .green = { .offset = 0, .length = 8, },
122 .blue = { .offset = 0, .length = 8, },
123 .transp = { .offset = 0, .length = 0, },
126 static int imxfb_activate_var(struct fb_var_screeninfo *var, struct fb_info *info);
128 static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
131 chan >>= 16 - bf->length;
132 return chan << bf->offset;
136 imxfb_setpalettereg(u_int regno, u_int red, u_int green, u_int blue,
137 u_int trans, struct fb_info *info)
139 struct imxfb_info *fbi = info->par;
142 #define CNVT_TOHW(val,width) ((((val)<<(width))+0x7FFF-(val))>>16)
143 if (regno < fbi->palette_size) {
144 val = (CNVT_TOHW(red, 4) << 8) |
145 (CNVT_TOHW(green,4) << 4) |
148 writel(val, fbi->regs + 0x800 + (regno << 2));
155 imxfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
156 u_int trans, struct fb_info *info)
158 struct imxfb_info *fbi = info->par;
163 * If inverse mode was selected, invert all the colours
164 * rather than the register number. The register number
165 * is what you poke into the framebuffer to produce the
166 * colour you requested.
168 if (fbi->cmap_inverse) {
170 green = 0xffff - green;
171 blue = 0xffff - blue;
175 * If greyscale is true, then we convert the RGB value
176 * to greyscale no mater what visual we are using.
178 if (info->var.grayscale)
179 red = green = blue = (19595 * red + 38470 * green +
182 switch (info->fix.visual) {
183 case FB_VISUAL_TRUECOLOR:
185 * 12 or 16-bit True Colour. We encode the RGB value
186 * according to the RGB bitfield information.
189 u32 *pal = info->pseudo_palette;
191 val = chan_to_field(red, &info->var.red);
192 val |= chan_to_field(green, &info->var.green);
193 val |= chan_to_field(blue, &info->var.blue);
200 case FB_VISUAL_STATIC_PSEUDOCOLOR:
201 case FB_VISUAL_PSEUDOCOLOR:
202 ret = imxfb_setpalettereg(regno, red, green, blue, trans, info);
211 * Round up in the following order: bits_per_pixel, xres,
212 * yres, xres_virtual, yres_virtual, xoffset, yoffset, grayscale,
213 * bitfields, horizontal timing, vertical timing.
216 imxfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
218 struct imxfb_info *fbi = info->par;
221 if (var->xres < MIN_XRES)
222 var->xres = MIN_XRES;
223 if (var->yres < MIN_YRES)
224 var->yres = MIN_YRES;
225 if (var->xres > fbi->max_xres)
226 var->xres = fbi->max_xres;
227 if (var->yres > fbi->max_yres)
228 var->yres = fbi->max_yres;
229 var->xres_virtual = max(var->xres_virtual, var->xres);
230 var->yres_virtual = max(var->yres_virtual, var->yres);
232 pr_debug("var->bits_per_pixel=%d\n", var->bits_per_pixel);
233 switch (var->bits_per_pixel) {
245 * Copy the RGB parameters for this display
246 * from the machine specific parameters.
248 var->red = fbi->rgb[rgbidx]->red;
249 var->green = fbi->rgb[rgbidx]->green;
250 var->blue = fbi->rgb[rgbidx]->blue;
251 var->transp = fbi->rgb[rgbidx]->transp;
253 pr_debug("RGBT length = %d:%d:%d:%d\n",
254 var->red.length, var->green.length, var->blue.length,
257 pr_debug("RGBT offset = %d:%d:%d:%d\n",
258 var->red.offset, var->green.offset, var->blue.offset,
266 * Set the user defined part of the display for the specified console
268 static int imxfb_set_par(struct fb_info *info)
270 struct imxfb_info *fbi = info->par;
271 struct fb_var_screeninfo *var = &info->var;
273 pr_debug("set_par\n");
275 if (var->bits_per_pixel == 16)
276 info->fix.visual = FB_VISUAL_TRUECOLOR;
277 else if (!fbi->cmap_static)
278 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
281 * Some people have weird ideas about wanting static
282 * pseudocolor maps. I suspect their user space
283 * applications are broken.
285 info->fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
288 info->fix.line_length = var->xres_virtual *
289 var->bits_per_pixel / 8;
290 fbi->palette_size = var->bits_per_pixel == 8 ? 256 : 16;
292 imxfb_activate_var(var, info);
297 static void imxfb_enable_controller(struct imxfb_info *fbi)
299 pr_debug("Enabling LCD controller\n");
301 /* initialize LCDC */
302 writel(readl(fbi->regs + LCDC_RMCR) & ~RMCR_LCDC_EN,
303 fbi->regs + LCDC_RMCR); /* just to be safe... */
305 writel(fbi->screen_dma, fbi->regs + LCDC_SSA);
307 /* physical screen start address */
308 writel(VPW_VPW(fbi->max_xres * fbi->max_bpp / 8 / 4),
309 fbi->regs + LCDC_VPW);
311 /* panning offset 0 (0 pixel offset) */
312 writel(0x00000000, fbi->regs + LCDC_POS);
314 /* disable hardware cursor */
315 writel(readl(fbi->regs + LCDC_CPOS) & ~(CPOS_CC0 | CPOS_CC1),
316 fbi->regs + LCDC_CPOS);
318 writel(RMCR_LCDC_EN, fbi->regs + LCDC_RMCR);
320 if(fbi->backlight_power)
321 fbi->backlight_power(1);
326 static void imxfb_disable_controller(struct imxfb_info *fbi)
328 pr_debug("Disabling LCD controller\n");
330 if(fbi->backlight_power)
331 fbi->backlight_power(0);
335 writel(0, fbi->regs + LCDC_RMCR);
338 static int imxfb_blank(int blank, struct fb_info *info)
340 struct imxfb_info *fbi = info->par;
342 pr_debug("imxfb_blank: blank=%d\n", blank);
345 case FB_BLANK_POWERDOWN:
346 case FB_BLANK_VSYNC_SUSPEND:
347 case FB_BLANK_HSYNC_SUSPEND:
348 case FB_BLANK_NORMAL:
349 imxfb_disable_controller(fbi);
352 case FB_BLANK_UNBLANK:
353 imxfb_enable_controller(fbi);
359 static struct fb_ops imxfb_ops = {
360 .owner = THIS_MODULE,
361 .fb_check_var = imxfb_check_var,
362 .fb_set_par = imxfb_set_par,
363 .fb_setcolreg = imxfb_setcolreg,
364 .fb_fillrect = cfb_fillrect,
365 .fb_copyarea = cfb_copyarea,
366 .fb_imageblit = cfb_imageblit,
367 .fb_blank = imxfb_blank,
371 * imxfb_activate_var():
372 * Configures LCD Controller based on entries in var parameter. Settings are
373 * only written to the controller if changes were made.
375 static int imxfb_activate_var(struct fb_var_screeninfo *var, struct fb_info *info)
377 struct imxfb_info *fbi = info->par;
378 pr_debug("var: xres=%d hslen=%d lm=%d rm=%d\n",
379 var->xres, var->hsync_len,
380 var->left_margin, var->right_margin);
381 pr_debug("var: yres=%d vslen=%d um=%d bm=%d\n",
382 var->yres, var->vsync_len,
383 var->upper_margin, var->lower_margin);
386 if (var->xres < 16 || var->xres > 1024)
387 printk(KERN_ERR "%s: invalid xres %d\n",
388 info->fix.id, var->xres);
389 if (var->hsync_len < 1 || var->hsync_len > 64)
390 printk(KERN_ERR "%s: invalid hsync_len %d\n",
391 info->fix.id, var->hsync_len);
392 if (var->left_margin > 255)
393 printk(KERN_ERR "%s: invalid left_margin %d\n",
394 info->fix.id, var->left_margin);
395 if (var->right_margin > 255)
396 printk(KERN_ERR "%s: invalid right_margin %d\n",
397 info->fix.id, var->right_margin);
398 if (var->yres < 1 || var->yres > 511)
399 printk(KERN_ERR "%s: invalid yres %d\n",
400 info->fix.id, var->yres);
401 if (var->vsync_len > 100)
402 printk(KERN_ERR "%s: invalid vsync_len %d\n",
403 info->fix.id, var->vsync_len);
404 if (var->upper_margin > 63)
405 printk(KERN_ERR "%s: invalid upper_margin %d\n",
406 info->fix.id, var->upper_margin);
407 if (var->lower_margin > 255)
408 printk(KERN_ERR "%s: invalid lower_margin %d\n",
409 info->fix.id, var->lower_margin);
412 writel(HCR_H_WIDTH(var->hsync_len) |
413 HCR_H_WAIT_1(var->left_margin) |
414 HCR_H_WAIT_2(var->right_margin),
415 fbi->regs + LCDC_HCR);
417 writel(VCR_V_WIDTH(var->vsync_len) |
418 VCR_V_WAIT_1(var->upper_margin) |
419 VCR_V_WAIT_2(var->lower_margin),
420 fbi->regs + LCDC_VCR);
422 writel(SIZE_XMAX(var->xres) | SIZE_YMAX(var->yres),
423 fbi->regs + LCDC_SIZE);
424 writel(fbi->pcr, fbi->regs + LCDC_PCR);
425 writel(fbi->pwmr, fbi->regs + LCDC_PWMR);
426 writel(fbi->lscr1, fbi->regs + LCDC_LSCR1);
427 writel(fbi->dmacr, fbi->regs + LCDC_DMACR);
434 * Power management hooks. Note that we won't be called from IRQ context,
435 * unlike the blank functions above, so we may sleep.
437 static int imxfb_suspend(struct platform_device *dev, pm_message_t state)
439 struct imxfb_info *fbi = platform_get_drvdata(dev);
440 pr_debug("%s\n",__func__);
442 imxfb_disable_controller(fbi);
446 static int imxfb_resume(struct platform_device *dev)
448 struct imxfb_info *fbi = platform_get_drvdata(dev);
449 pr_debug("%s\n",__func__);
451 imxfb_enable_controller(fbi);
455 #define imxfb_suspend NULL
456 #define imxfb_resume NULL
459 static int __init imxfb_init_fbinfo(struct platform_device *pdev)
461 struct imxfb_mach_info *inf = pdev->dev.platform_data;
462 struct fb_info *info = dev_get_drvdata(&pdev->dev);
463 struct imxfb_info *fbi = info->par;
465 pr_debug("%s\n",__func__);
467 info->pseudo_palette = kmalloc( sizeof(u32) * 16, GFP_KERNEL);
468 if (!info->pseudo_palette)
471 memset(fbi, 0, sizeof(struct imxfb_info));
473 strlcpy(info->fix.id, IMX_NAME, sizeof(info->fix.id));
475 info->fix.type = FB_TYPE_PACKED_PIXELS;
476 info->fix.type_aux = 0;
477 info->fix.xpanstep = 0;
478 info->fix.ypanstep = 0;
479 info->fix.ywrapstep = 0;
480 info->fix.accel = FB_ACCEL_NONE;
482 info->var.nonstd = 0;
483 info->var.activate = FB_ACTIVATE_NOW;
484 info->var.height = -1;
485 info->var.width = -1;
486 info->var.accel_flags = 0;
487 info->var.vmode = FB_VMODE_NONINTERLACED;
489 info->fbops = &imxfb_ops;
490 info->flags = FBINFO_FLAG_DEFAULT | FBINFO_READS_FAST;
492 fbi->rgb[RGB_16] = &def_rgb_16;
493 fbi->rgb[RGB_8] = &def_rgb_8;
495 fbi->max_xres = inf->xres;
496 info->var.xres = inf->xres;
497 info->var.xres_virtual = inf->xres;
498 fbi->max_yres = inf->yres;
499 info->var.yres = inf->yres;
500 info->var.yres_virtual = inf->yres;
501 fbi->max_bpp = inf->bpp;
502 info->var.bits_per_pixel = inf->bpp;
503 info->var.nonstd = inf->nonstd;
504 info->var.pixclock = inf->pixclock;
505 info->var.hsync_len = inf->hsync_len;
506 info->var.left_margin = inf->left_margin;
507 info->var.right_margin = inf->right_margin;
508 info->var.vsync_len = inf->vsync_len;
509 info->var.upper_margin = inf->upper_margin;
510 info->var.lower_margin = inf->lower_margin;
511 info->var.sync = inf->sync;
512 info->var.grayscale = inf->cmap_greyscale;
513 fbi->cmap_inverse = inf->cmap_inverse;
514 fbi->cmap_static = inf->cmap_static;
516 fbi->lscr1 = inf->lscr1;
517 fbi->dmacr = inf->dmacr;
518 fbi->pwmr = inf->pwmr;
519 fbi->lcd_power = inf->lcd_power;
520 fbi->backlight_power = inf->backlight_power;
521 info->fix.smem_len = fbi->max_xres * fbi->max_yres *
527 static int __init imxfb_probe(struct platform_device *pdev)
529 struct imxfb_info *fbi;
530 struct fb_info *info;
531 struct imxfb_mach_info *inf;
532 struct resource *res;
535 printk("i.MX Framebuffer driver\n");
537 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
541 inf = pdev->dev.platform_data;
543 dev_err(&pdev->dev,"No platform_data available\n");
547 info = framebuffer_alloc(sizeof(struct imxfb_info), &pdev->dev);
553 platform_set_drvdata(pdev, info);
555 ret = imxfb_init_fbinfo(pdev);
559 res = request_mem_region(res->start, resource_size(res),
566 fbi->regs = ioremap(res->start, resource_size(res));
567 if (fbi->regs == NULL) {
568 printk(KERN_ERR"Cannot map frame buffer registers\n");
572 if (!inf->fixed_screen_cpu) {
573 fbi->map_size = PAGE_ALIGN(info->fix.smem_len);
574 fbi->map_cpu = dma_alloc_writecombine(&pdev->dev,
575 fbi->map_size, &fbi->map_dma, GFP_KERNEL);
578 dev_err(&pdev->dev, "Failed to allocate video RAM: %d\n", ret);
583 info->screen_base = fbi->map_cpu;
584 fbi->screen_cpu = fbi->map_cpu;
585 fbi->screen_dma = fbi->map_dma;
586 info->fix.smem_start = fbi->screen_dma;
588 /* Fixed framebuffer mapping enables location of the screen in eSRAM */
589 fbi->map_cpu = inf->fixed_screen_cpu;
590 fbi->map_dma = inf->fixed_screen_dma;
591 info->screen_base = fbi->map_cpu;
592 fbi->screen_cpu = fbi->map_cpu;
593 fbi->screen_dma = fbi->map_dma;
594 info->fix.smem_start = fbi->screen_dma;
598 * This makes sure that our colour bitfield
599 * descriptors are correctly initialised.
601 imxfb_check_var(&info->var, info);
603 ret = fb_alloc_cmap(&info->cmap, 1<<info->var.bits_per_pixel, 0);
608 ret = register_framebuffer(info);
610 dev_err(&pdev->dev, "failed to register framebuffer\n");
611 goto failed_register;
614 imxfb_enable_controller(fbi);
619 fb_dealloc_cmap(&info->cmap);
621 if (!inf->fixed_screen_cpu)
622 dma_free_writecombine(&pdev->dev,fbi->map_size,fbi->map_cpu,
627 release_mem_region(res->start, res->end - res->start);
629 kfree(info->pseudo_palette);
631 platform_set_drvdata(pdev, NULL);
632 framebuffer_release(info);
636 static int __devexit imxfb_remove(struct platform_device *pdev)
638 struct fb_info *info = platform_get_drvdata(pdev);
639 struct imxfb_info *fbi = info->par;
640 struct resource *res;
642 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
644 imxfb_disable_controller(fbi);
646 unregister_framebuffer(info);
648 fb_dealloc_cmap(&info->cmap);
649 kfree(info->pseudo_palette);
650 framebuffer_release(info);
653 release_mem_region(res->start, res->end - res->start + 1);
654 platform_set_drvdata(pdev, NULL);
659 void imxfb_shutdown(struct platform_device * dev)
661 struct fb_info *info = platform_get_drvdata(dev);
662 struct imxfb_info *fbi = info->par;
663 imxfb_disable_controller(fbi);
666 static struct platform_driver imxfb_driver = {
667 .suspend = imxfb_suspend,
668 .resume = imxfb_resume,
669 .remove = __devexit_p(imxfb_remove),
670 .shutdown = imxfb_shutdown,
676 int __init imxfb_init(void)
678 return platform_driver_probe(&imxfb_driver, imxfb_probe);
681 static void __exit imxfb_cleanup(void)
683 platform_driver_unregister(&imxfb_driver);
686 module_init(imxfb_init);
687 module_exit(imxfb_cleanup);
689 MODULE_DESCRIPTION("Motorola i.MX framebuffer driver");
690 MODULE_AUTHOR("Sascha Hauer, Pengutronix");
691 MODULE_LICENSE("GPL");