2 * Freescale i.MX Frame Buffer device driver
4 * Copyright (C) 2004 Sascha Hauer, Pengutronix
5 * Based on acornfb.c Copyright (C) Russell King.
7 * This file is subject to the terms and conditions of the GNU General Public
8 * License. See the file COPYING in the main directory of this archive for
11 * Please direct your questions and comments on this driver to the following
14 * linux-arm-kernel@lists.arm.linux.org.uk
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/errno.h>
20 #include <linux/string.h>
21 #include <linux/interrupt.h>
22 #include <linux/slab.h>
25 #include <linux/delay.h>
26 #include <linux/init.h>
27 #include <linux/ioport.h>
28 #include <linux/cpufreq.h>
29 #include <linux/platform_device.h>
30 #include <linux/dma-mapping.h>
33 #include <mach/imxfb.h>
36 * Complain if VAR is out of range.
40 #define DRIVER_NAME "imx-fb"
44 #define LCDC_SIZE 0x04
45 #define SIZE_XMAX(x) ((((x) >> 4) & 0x3f) << 20)
47 #ifdef CONFIG_ARCH_MX1
48 #define SIZE_YMAX(y) ((y) & 0x1ff)
50 #define SIZE_YMAX(y) ((y) & 0x3ff)
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)
62 #ifdef CONFIG_ARCH_MX1
63 #define CPOS_CYP(y) ((y) & 0x1ff)
65 #define CPOS_CYP(y) ((y) & 0x3ff)
68 #define LCDC_LCWHB 0x10
69 #define LCWHB_BK_EN (1<<31)
70 #define LCWHB_CW(w) (((w) & 0x1f) << 24)
71 #define LCWHB_CH(h) (((h) & 0x1f) << 16)
72 #define LCWHB_BD(x) ((x) & 0xff)
74 #define LCDC_LCHCC 0x14
76 #ifdef CONFIG_ARCH_MX1
77 #define LCHCC_CUR_COL_R(r) (((r) & 0x1f) << 11)
78 #define LCHCC_CUR_COL_G(g) (((g) & 0x3f) << 5)
79 #define LCHCC_CUR_COL_B(b) ((b) & 0x1f)
81 #define LCHCC_CUR_COL_R(r) (((r) & 0x3f) << 12)
82 #define LCHCC_CUR_COL_G(g) (((g) & 0x3f) << 6)
83 #define LCHCC_CUR_COL_B(b) ((b) & 0x3f)
89 #define HCR_H_WIDTH(x) (((x) & 0x3f) << 26)
90 #define HCR_H_WAIT_1(x) (((x) & 0xff) << 8)
91 #define HCR_H_WAIT_2(x) ((x) & 0xff)
94 #define VCR_V_WIDTH(x) (((x) & 0x3f) << 26)
95 #define VCR_V_WAIT_1(x) (((x) & 0xff) << 8)
96 #define VCR_V_WAIT_2(x) ((x) & 0xff)
99 #define POS_POS(x) ((x) & 1f)
101 #define LCDC_LSCR1 0x28
102 /* bit fields in imxfb.h */
104 #define LCDC_PWMR 0x2C
105 /* bit fields in imxfb.h */
107 #define LCDC_DMACR 0x30
108 /* bit fields in imxfb.h */
110 #define LCDC_RMCR 0x34
112 #ifdef CONFIG_ARCH_MX1
113 #define RMCR_LCDC_EN (1<<1)
115 #define RMCR_LCDC_EN 0
118 #define RMCR_SELF_REF (1<<0)
120 #define LCDC_LCDICR 0x38
121 #define LCDICR_INT_SYN (1<<2)
122 #define LCDICR_INT_CON (1)
124 #define LCDC_LCDISR 0x40
125 #define LCDISR_UDR_ERR (1<<3)
126 #define LCDISR_ERR_RES (1<<2)
127 #define LCDISR_EOF (1<<1)
128 #define LCDISR_BOF (1<<0)
131 * These are the bitfields for each
132 * display depth that we support.
135 struct fb_bitfield red;
136 struct fb_bitfield green;
137 struct fb_bitfield blue;
138 struct fb_bitfield transp;
142 struct platform_device *pdev;
150 * These are the addresses we mapped
151 * the framebuffer memory region to.
158 dma_addr_t screen_dma;
168 u_int cmap_inverse:1,
172 void (*lcd_power)(int);
173 void (*backlight_power)(int);
176 #define IMX_NAME "IMX"
179 * Minimum X and Y resolutions
184 /* Actually this really is 18bit support, the lowest 2 bits of each colour
185 * are unused in hardware. We claim to have 24bit support to make software
186 * like X work, which does not support 18bit.
188 static struct imxfb_rgb def_rgb_18 = {
189 .red = {.offset = 16, .length = 8,},
190 .green = {.offset = 8, .length = 8,},
191 .blue = {.offset = 0, .length = 8,},
192 .transp = {.offset = 0, .length = 0,},
195 static struct imxfb_rgb def_rgb_16_tft = {
196 .red = {.offset = 11, .length = 5,},
197 .green = {.offset = 5, .length = 6,},
198 .blue = {.offset = 0, .length = 5,},
199 .transp = {.offset = 0, .length = 0,},
202 static struct imxfb_rgb def_rgb_16_stn = {
203 .red = {.offset = 8, .length = 4,},
204 .green = {.offset = 4, .length = 4,},
205 .blue = {.offset = 0, .length = 4,},
206 .transp = {.offset = 0, .length = 0,},
209 static struct imxfb_rgb def_rgb_8 = {
210 .red = {.offset = 0, .length = 8,},
211 .green = {.offset = 0, .length = 8,},
212 .blue = {.offset = 0, .length = 8,},
213 .transp = {.offset = 0, .length = 0,},
216 static int imxfb_activate_var(struct fb_var_screeninfo *var,
217 struct fb_info *info);
219 static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
222 chan >>= 16 - bf->length;
223 return chan << bf->offset;
226 static int imxfb_setpalettereg(u_int regno, u_int red, u_int green, u_int blue,
227 u_int trans, struct fb_info *info)
229 struct imxfb_info *fbi = info->par;
232 #define CNVT_TOHW(val,width) ((((val)<<(width))+0x7FFF-(val))>>16)
233 if (regno < fbi->palette_size) {
234 val = (CNVT_TOHW(red, 4) << 8) |
235 (CNVT_TOHW(green,4) << 4) |
238 writel(val, fbi->regs + 0x800 + (regno << 2));
244 static int imxfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
245 u_int trans, struct fb_info *info)
247 struct imxfb_info *fbi = info->par;
252 * If inverse mode was selected, invert all the colours
253 * rather than the register number. The register number
254 * is what you poke into the framebuffer to produce the
255 * colour you requested.
257 if (fbi->cmap_inverse) {
259 green = 0xffff - green;
260 blue = 0xffff - blue;
264 * If greyscale is true, then we convert the RGB value
265 * to greyscale no mater what visual we are using.
267 if (info->var.grayscale)
268 red = green = blue = (19595 * red + 38470 * green +
271 switch (info->fix.visual) {
272 case FB_VISUAL_TRUECOLOR:
274 * 12 or 16-bit True Colour. We encode the RGB value
275 * according to the RGB bitfield information.
278 u32 *pal = info->pseudo_palette;
280 val = chan_to_field(red, &info->var.red);
281 val |= chan_to_field(green, &info->var.green);
282 val |= chan_to_field(blue, &info->var.blue);
289 case FB_VISUAL_STATIC_PSEUDOCOLOR:
290 case FB_VISUAL_PSEUDOCOLOR:
291 ret = imxfb_setpalettereg(regno, red, green, blue, trans, info);
300 * Round up in the following order: bits_per_pixel, xres,
301 * yres, xres_virtual, yres_virtual, xoffset, yoffset, grayscale,
302 * bitfields, horizontal timing, vertical timing.
304 static int imxfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
306 struct imxfb_info *fbi = info->par;
307 struct imxfb_rgb *rgb;
309 if (var->xres < MIN_XRES)
310 var->xres = MIN_XRES;
311 if (var->yres < MIN_YRES)
312 var->yres = MIN_YRES;
313 if (var->xres > fbi->max_xres)
314 var->xres = fbi->max_xres;
315 if (var->yres > fbi->max_yres)
316 var->yres = fbi->max_yres;
317 var->xres_virtual = max(var->xres_virtual, var->xres);
318 var->yres_virtual = max(var->yres_virtual, var->yres);
320 pr_debug("var->bits_per_pixel=%d\n", var->bits_per_pixel);
321 switch (var->bits_per_pixel) {
327 if (readl(fbi->regs + LCDC_PCR) & PCR_TFT)
328 rgb = &def_rgb_16_tft;
330 rgb = &def_rgb_16_stn;
338 * Copy the RGB parameters for this display
339 * from the machine specific parameters.
342 var->green = rgb->green;
343 var->blue = rgb->blue;
344 var->transp = rgb->transp;
346 pr_debug("RGBT length = %d:%d:%d:%d\n",
347 var->red.length, var->green.length, var->blue.length,
350 pr_debug("RGBT offset = %d:%d:%d:%d\n",
351 var->red.offset, var->green.offset, var->blue.offset,
359 * Set the user defined part of the display for the specified console
361 static int imxfb_set_par(struct fb_info *info)
363 struct imxfb_info *fbi = info->par;
364 struct fb_var_screeninfo *var = &info->var;
366 if (var->bits_per_pixel == 16 || var->bits_per_pixel == 32)
367 info->fix.visual = FB_VISUAL_TRUECOLOR;
368 else if (!fbi->cmap_static)
369 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
372 * Some people have weird ideas about wanting static
373 * pseudocolor maps. I suspect their user space
374 * applications are broken.
376 info->fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
379 info->fix.line_length = var->xres_virtual * var->bits_per_pixel / 8;
380 fbi->palette_size = var->bits_per_pixel == 8 ? 256 : 16;
382 imxfb_activate_var(var, info);
387 static void imxfb_enable_controller(struct imxfb_info *fbi)
389 pr_debug("Enabling LCD controller\n");
391 writel(fbi->screen_dma, fbi->regs + LCDC_SSA);
393 /* physical screen start address */
394 writel(VPW_VPW(fbi->max_xres * fbi->max_bpp / 8 / 4),
395 fbi->regs + LCDC_VPW);
397 /* panning offset 0 (0 pixel offset) */
398 writel(0x00000000, fbi->regs + LCDC_POS);
400 /* disable hardware cursor */
401 writel(readl(fbi->regs + LCDC_CPOS) & ~(CPOS_CC0 | CPOS_CC1),
402 fbi->regs + LCDC_CPOS);
404 writel(RMCR_LCDC_EN, fbi->regs + LCDC_RMCR);
406 if (fbi->backlight_power)
407 fbi->backlight_power(1);
412 static void imxfb_disable_controller(struct imxfb_info *fbi)
414 pr_debug("Disabling LCD controller\n");
416 if (fbi->backlight_power)
417 fbi->backlight_power(0);
421 writel(0, fbi->regs + LCDC_RMCR);
424 static int imxfb_blank(int blank, struct fb_info *info)
426 struct imxfb_info *fbi = info->par;
428 pr_debug("imxfb_blank: blank=%d\n", blank);
431 case FB_BLANK_POWERDOWN:
432 case FB_BLANK_VSYNC_SUSPEND:
433 case FB_BLANK_HSYNC_SUSPEND:
434 case FB_BLANK_NORMAL:
435 imxfb_disable_controller(fbi);
438 case FB_BLANK_UNBLANK:
439 imxfb_enable_controller(fbi);
445 static struct fb_ops imxfb_ops = {
446 .owner = THIS_MODULE,
447 .fb_check_var = imxfb_check_var,
448 .fb_set_par = imxfb_set_par,
449 .fb_setcolreg = imxfb_setcolreg,
450 .fb_fillrect = cfb_fillrect,
451 .fb_copyarea = cfb_copyarea,
452 .fb_imageblit = cfb_imageblit,
453 .fb_blank = imxfb_blank,
457 * imxfb_activate_var():
458 * Configures LCD Controller based on entries in var parameter. Settings are
459 * only written to the controller if changes were made.
461 static int imxfb_activate_var(struct fb_var_screeninfo *var, struct fb_info *info)
463 struct imxfb_info *fbi = info->par;
464 pr_debug("var: xres=%d hslen=%d lm=%d rm=%d\n",
465 var->xres, var->hsync_len,
466 var->left_margin, var->right_margin);
467 pr_debug("var: yres=%d vslen=%d um=%d bm=%d\n",
468 var->yres, var->vsync_len,
469 var->upper_margin, var->lower_margin);
472 if (var->xres < 16 || var->xres > 1024)
473 printk(KERN_ERR "%s: invalid xres %d\n",
474 info->fix.id, var->xres);
475 if (var->hsync_len < 1 || var->hsync_len > 64)
476 printk(KERN_ERR "%s: invalid hsync_len %d\n",
477 info->fix.id, var->hsync_len);
478 if (var->left_margin > 255)
479 printk(KERN_ERR "%s: invalid left_margin %d\n",
480 info->fix.id, var->left_margin);
481 if (var->right_margin > 255)
482 printk(KERN_ERR "%s: invalid right_margin %d\n",
483 info->fix.id, var->right_margin);
484 if (var->yres < 1 || var->yres > 511)
485 printk(KERN_ERR "%s: invalid yres %d\n",
486 info->fix.id, var->yres);
487 if (var->vsync_len > 100)
488 printk(KERN_ERR "%s: invalid vsync_len %d\n",
489 info->fix.id, var->vsync_len);
490 if (var->upper_margin > 63)
491 printk(KERN_ERR "%s: invalid upper_margin %d\n",
492 info->fix.id, var->upper_margin);
493 if (var->lower_margin > 255)
494 printk(KERN_ERR "%s: invalid lower_margin %d\n",
495 info->fix.id, var->lower_margin);
498 writel(HCR_H_WIDTH(var->hsync_len - 1) |
499 HCR_H_WAIT_1(var->right_margin - 1) |
500 HCR_H_WAIT_2(var->left_margin - 3),
501 fbi->regs + LCDC_HCR);
503 writel(VCR_V_WIDTH(var->vsync_len) |
504 VCR_V_WAIT_1(var->lower_margin) |
505 VCR_V_WAIT_2(var->upper_margin),
506 fbi->regs + LCDC_VCR);
508 writel(SIZE_XMAX(var->xres) | SIZE_YMAX(var->yres),
509 fbi->regs + LCDC_SIZE);
510 writel(fbi->pcr, fbi->regs + LCDC_PCR);
511 writel(fbi->pwmr, fbi->regs + LCDC_PWMR);
512 writel(fbi->lscr1, fbi->regs + LCDC_LSCR1);
513 writel(fbi->dmacr, fbi->regs + LCDC_DMACR);
520 * Power management hooks. Note that we won't be called from IRQ context,
521 * unlike the blank functions above, so we may sleep.
523 static int imxfb_suspend(struct platform_device *dev, pm_message_t state)
525 struct imxfb_info *fbi = platform_get_drvdata(dev);
527 pr_debug("%s\n", __func__);
529 imxfb_disable_controller(fbi);
533 static int imxfb_resume(struct platform_device *dev)
535 struct imxfb_info *fbi = platform_get_drvdata(dev);
537 pr_debug("%s\n", __func__);
539 imxfb_enable_controller(fbi);
543 #define imxfb_suspend NULL
544 #define imxfb_resume NULL
547 static int __init imxfb_init_fbinfo(struct platform_device *pdev)
549 struct imx_fb_platform_data *pdata = pdev->dev.platform_data;
550 struct fb_info *info = dev_get_drvdata(&pdev->dev);
551 struct imxfb_info *fbi = info->par;
553 pr_debug("%s\n",__func__);
555 info->pseudo_palette = kmalloc(sizeof(u32) * 16, GFP_KERNEL);
556 if (!info->pseudo_palette)
559 memset(fbi, 0, sizeof(struct imxfb_info));
561 strlcpy(info->fix.id, IMX_NAME, sizeof(info->fix.id));
563 info->fix.type = FB_TYPE_PACKED_PIXELS;
564 info->fix.type_aux = 0;
565 info->fix.xpanstep = 0;
566 info->fix.ypanstep = 0;
567 info->fix.ywrapstep = 0;
568 info->fix.accel = FB_ACCEL_NONE;
570 info->var.nonstd = 0;
571 info->var.activate = FB_ACTIVATE_NOW;
572 info->var.height = -1;
573 info->var.width = -1;
574 info->var.accel_flags = 0;
575 info->var.vmode = FB_VMODE_NONINTERLACED;
577 info->fbops = &imxfb_ops;
578 info->flags = FBINFO_FLAG_DEFAULT |
581 fbi->max_xres = pdata->xres;
582 info->var.xres = pdata->xres;
583 info->var.xres_virtual = pdata->xres;
584 fbi->max_yres = pdata->yres;
585 info->var.yres = pdata->yres;
586 info->var.yres_virtual = pdata->yres;
587 fbi->max_bpp = pdata->bpp;
588 info->var.bits_per_pixel = pdata->bpp;
589 info->var.nonstd = pdata->nonstd;
590 info->var.pixclock = pdata->pixclock;
591 info->var.hsync_len = pdata->hsync_len;
592 info->var.left_margin = pdata->left_margin;
593 info->var.right_margin = pdata->right_margin;
594 info->var.vsync_len = pdata->vsync_len;
595 info->var.upper_margin = pdata->upper_margin;
596 info->var.lower_margin = pdata->lower_margin;
597 info->var.sync = pdata->sync;
598 info->var.grayscale = pdata->cmap_greyscale;
599 fbi->cmap_inverse = pdata->cmap_inverse;
600 fbi->cmap_static = pdata->cmap_static;
601 fbi->pcr = pdata->pcr;
602 fbi->lscr1 = pdata->lscr1;
603 fbi->dmacr = pdata->dmacr;
604 fbi->pwmr = pdata->pwmr;
605 fbi->lcd_power = pdata->lcd_power;
606 fbi->backlight_power = pdata->backlight_power;
607 info->fix.smem_len = fbi->max_xres * fbi->max_yres *
613 static int __init imxfb_probe(struct platform_device *pdev)
615 struct imxfb_info *fbi;
616 struct fb_info *info;
617 struct imx_fb_platform_data *pdata;
618 struct resource *res;
621 printk("i.MX Framebuffer driver\n");
623 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
627 pdata = pdev->dev.platform_data;
629 dev_err(&pdev->dev,"No platform_data available\n");
633 info = framebuffer_alloc(sizeof(struct imxfb_info), &pdev->dev);
639 platform_set_drvdata(pdev, info);
641 ret = imxfb_init_fbinfo(pdev);
645 res = request_mem_region(res->start, resource_size(res),
652 fbi->regs = ioremap(res->start, resource_size(res));
653 if (fbi->regs == NULL) {
654 printk(KERN_ERR"Cannot map frame buffer registers\n");
658 if (!pdata->fixed_screen_cpu) {
659 fbi->map_size = PAGE_ALIGN(info->fix.smem_len);
660 fbi->map_cpu = dma_alloc_writecombine(&pdev->dev,
661 fbi->map_size, &fbi->map_dma, GFP_KERNEL);
664 dev_err(&pdev->dev, "Failed to allocate video RAM: %d\n", ret);
669 info->screen_base = fbi->map_cpu;
670 fbi->screen_cpu = fbi->map_cpu;
671 fbi->screen_dma = fbi->map_dma;
672 info->fix.smem_start = fbi->screen_dma;
674 /* Fixed framebuffer mapping enables location of the screen in eSRAM */
675 fbi->map_cpu = pdata->fixed_screen_cpu;
676 fbi->map_dma = pdata->fixed_screen_dma;
677 info->screen_base = fbi->map_cpu;
678 fbi->screen_cpu = fbi->map_cpu;
679 fbi->screen_dma = fbi->map_dma;
680 info->fix.smem_start = fbi->screen_dma;
684 ret = pdata->init(fbi->pdev);
686 goto failed_platform_init;
690 * This makes sure that our colour bitfield
691 * descriptors are correctly initialised.
693 imxfb_check_var(&info->var, info);
695 ret = fb_alloc_cmap(&info->cmap, 1 << info->var.bits_per_pixel, 0);
700 ret = register_framebuffer(info);
702 dev_err(&pdev->dev, "failed to register framebuffer\n");
703 goto failed_register;
706 imxfb_enable_controller(fbi);
711 fb_dealloc_cmap(&info->cmap);
714 pdata->exit(fbi->pdev);
715 failed_platform_init:
716 if (!pdata->fixed_screen_cpu)
717 dma_free_writecombine(&pdev->dev,fbi->map_size,fbi->map_cpu,
722 release_mem_region(res->start, res->end - res->start);
724 kfree(info->pseudo_palette);
726 platform_set_drvdata(pdev, NULL);
727 framebuffer_release(info);
731 static int __devexit imxfb_remove(struct platform_device *pdev)
733 struct imx_fb_platform_data *pdata;
734 struct fb_info *info = platform_get_drvdata(pdev);
735 struct imxfb_info *fbi = info->par;
736 struct resource *res;
738 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
740 imxfb_disable_controller(fbi);
742 unregister_framebuffer(info);
744 pdata = pdev->dev.platform_data;
746 pdata->exit(fbi->pdev);
748 fb_dealloc_cmap(&info->cmap);
749 kfree(info->pseudo_palette);
750 framebuffer_release(info);
753 release_mem_region(res->start, res->end - res->start + 1);
754 platform_set_drvdata(pdev, NULL);
759 void imxfb_shutdown(struct platform_device * dev)
761 struct fb_info *info = platform_get_drvdata(dev);
762 struct imxfb_info *fbi = info->par;
763 imxfb_disable_controller(fbi);
766 static struct platform_driver imxfb_driver = {
767 .suspend = imxfb_suspend,
768 .resume = imxfb_resume,
769 .remove = __devexit_p(imxfb_remove),
770 .shutdown = imxfb_shutdown,
776 int __init imxfb_init(void)
778 return platform_driver_probe(&imxfb_driver, imxfb_probe);
781 static void __exit imxfb_cleanup(void)
783 platform_driver_unregister(&imxfb_driver);
786 module_init(imxfb_init);
787 module_exit(imxfb_cleanup);
789 MODULE_DESCRIPTION("Motorola i.MX framebuffer driver");
790 MODULE_AUTHOR("Sascha Hauer, Pengutronix");
791 MODULE_LICENSE("GPL");