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/clk.h>
30 #include <linux/platform_device.h>
31 #include <linux/dma-mapping.h>
33 #include <linux/math64.h>
35 #include <mach/imxfb.h>
38 * Complain if VAR is out of range.
42 #define DRIVER_NAME "imx-fb"
46 #define LCDC_SIZE 0x04
47 #define SIZE_XMAX(x) ((((x) >> 4) & 0x3f) << 20)
49 #ifdef CONFIG_ARCH_MX1
50 #define SIZE_YMAX(y) ((y) & 0x1ff)
52 #define SIZE_YMAX(y) ((y) & 0x3ff)
56 #define VPW_VPW(x) ((x) & 0x3ff)
58 #define LCDC_CPOS 0x0C
59 #define CPOS_CC1 (1<<31)
60 #define CPOS_CC0 (1<<30)
61 #define CPOS_OP (1<<28)
62 #define CPOS_CXP(x) (((x) & 3ff) << 16)
64 #ifdef CONFIG_ARCH_MX1
65 #define CPOS_CYP(y) ((y) & 0x1ff)
67 #define CPOS_CYP(y) ((y) & 0x3ff)
70 #define LCDC_LCWHB 0x10
71 #define LCWHB_BK_EN (1<<31)
72 #define LCWHB_CW(w) (((w) & 0x1f) << 24)
73 #define LCWHB_CH(h) (((h) & 0x1f) << 16)
74 #define LCWHB_BD(x) ((x) & 0xff)
76 #define LCDC_LCHCC 0x14
78 #ifdef CONFIG_ARCH_MX1
79 #define LCHCC_CUR_COL_R(r) (((r) & 0x1f) << 11)
80 #define LCHCC_CUR_COL_G(g) (((g) & 0x3f) << 5)
81 #define LCHCC_CUR_COL_B(b) ((b) & 0x1f)
83 #define LCHCC_CUR_COL_R(r) (((r) & 0x3f) << 12)
84 #define LCHCC_CUR_COL_G(g) (((g) & 0x3f) << 6)
85 #define LCHCC_CUR_COL_B(b) ((b) & 0x3f)
91 #define HCR_H_WIDTH(x) (((x) & 0x3f) << 26)
92 #define HCR_H_WAIT_1(x) (((x) & 0xff) << 8)
93 #define HCR_H_WAIT_2(x) ((x) & 0xff)
96 #define VCR_V_WIDTH(x) (((x) & 0x3f) << 26)
97 #define VCR_V_WAIT_1(x) (((x) & 0xff) << 8)
98 #define VCR_V_WAIT_2(x) ((x) & 0xff)
100 #define LCDC_POS 0x24
101 #define POS_POS(x) ((x) & 1f)
103 #define LCDC_LSCR1 0x28
104 /* bit fields in imxfb.h */
106 #define LCDC_PWMR 0x2C
107 /* bit fields in imxfb.h */
109 #define LCDC_DMACR 0x30
110 /* bit fields in imxfb.h */
112 #define LCDC_RMCR 0x34
114 #ifdef CONFIG_ARCH_MX1
115 #define RMCR_LCDC_EN (1<<1)
117 #define RMCR_LCDC_EN 0
120 #define RMCR_SELF_REF (1<<0)
122 #define LCDC_LCDICR 0x38
123 #define LCDICR_INT_SYN (1<<2)
124 #define LCDICR_INT_CON (1)
126 #define LCDC_LCDISR 0x40
127 #define LCDISR_UDR_ERR (1<<3)
128 #define LCDISR_ERR_RES (1<<2)
129 #define LCDISR_EOF (1<<1)
130 #define LCDISR_BOF (1<<0)
133 * These are the bitfields for each
134 * display depth that we support.
137 struct fb_bitfield red;
138 struct fb_bitfield green;
139 struct fb_bitfield blue;
140 struct fb_bitfield transp;
144 struct platform_device *pdev;
153 * These are the addresses we mapped
154 * the framebuffer memory region to.
161 dma_addr_t screen_dma;
171 u_int cmap_inverse:1,
175 void (*lcd_power)(int);
176 void (*backlight_power)(int);
179 #define IMX_NAME "IMX"
182 * Minimum X and Y resolutions
187 /* Actually this really is 18bit support, the lowest 2 bits of each colour
188 * are unused in hardware. We claim to have 24bit support to make software
189 * like X work, which does not support 18bit.
191 static struct imxfb_rgb def_rgb_18 = {
192 .red = {.offset = 16, .length = 8,},
193 .green = {.offset = 8, .length = 8,},
194 .blue = {.offset = 0, .length = 8,},
195 .transp = {.offset = 0, .length = 0,},
198 static struct imxfb_rgb def_rgb_16_tft = {
199 .red = {.offset = 11, .length = 5,},
200 .green = {.offset = 5, .length = 6,},
201 .blue = {.offset = 0, .length = 5,},
202 .transp = {.offset = 0, .length = 0,},
205 static struct imxfb_rgb def_rgb_16_stn = {
206 .red = {.offset = 8, .length = 4,},
207 .green = {.offset = 4, .length = 4,},
208 .blue = {.offset = 0, .length = 4,},
209 .transp = {.offset = 0, .length = 0,},
212 static struct imxfb_rgb def_rgb_8 = {
213 .red = {.offset = 0, .length = 8,},
214 .green = {.offset = 0, .length = 8,},
215 .blue = {.offset = 0, .length = 8,},
216 .transp = {.offset = 0, .length = 0,},
219 static int imxfb_activate_var(struct fb_var_screeninfo *var,
220 struct fb_info *info);
222 static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
225 chan >>= 16 - bf->length;
226 return chan << bf->offset;
229 static int imxfb_setpalettereg(u_int regno, u_int red, u_int green, u_int blue,
230 u_int trans, struct fb_info *info)
232 struct imxfb_info *fbi = info->par;
235 #define CNVT_TOHW(val,width) ((((val)<<(width))+0x7FFF-(val))>>16)
236 if (regno < fbi->palette_size) {
237 val = (CNVT_TOHW(red, 4) << 8) |
238 (CNVT_TOHW(green,4) << 4) |
241 writel(val, fbi->regs + 0x800 + (regno << 2));
247 static int imxfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
248 u_int trans, struct fb_info *info)
250 struct imxfb_info *fbi = info->par;
255 * If inverse mode was selected, invert all the colours
256 * rather than the register number. The register number
257 * is what you poke into the framebuffer to produce the
258 * colour you requested.
260 if (fbi->cmap_inverse) {
262 green = 0xffff - green;
263 blue = 0xffff - blue;
267 * If greyscale is true, then we convert the RGB value
268 * to greyscale no mater what visual we are using.
270 if (info->var.grayscale)
271 red = green = blue = (19595 * red + 38470 * green +
274 switch (info->fix.visual) {
275 case FB_VISUAL_TRUECOLOR:
277 * 12 or 16-bit True Colour. We encode the RGB value
278 * according to the RGB bitfield information.
281 u32 *pal = info->pseudo_palette;
283 val = chan_to_field(red, &info->var.red);
284 val |= chan_to_field(green, &info->var.green);
285 val |= chan_to_field(blue, &info->var.blue);
292 case FB_VISUAL_STATIC_PSEUDOCOLOR:
293 case FB_VISUAL_PSEUDOCOLOR:
294 ret = imxfb_setpalettereg(regno, red, green, blue, trans, info);
303 * Round up in the following order: bits_per_pixel, xres,
304 * yres, xres_virtual, yres_virtual, xoffset, yoffset, grayscale,
305 * bitfields, horizontal timing, vertical timing.
307 static int imxfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
309 struct imxfb_info *fbi = info->par;
310 struct imxfb_rgb *rgb;
312 if (var->xres < MIN_XRES)
313 var->xres = MIN_XRES;
314 if (var->yres < MIN_YRES)
315 var->yres = MIN_YRES;
316 if (var->xres > fbi->max_xres)
317 var->xres = fbi->max_xres;
318 if (var->yres > fbi->max_yres)
319 var->yres = fbi->max_yres;
320 var->xres_virtual = max(var->xres_virtual, var->xres);
321 var->yres_virtual = max(var->yres_virtual, var->yres);
323 pr_debug("var->bits_per_pixel=%d\n", var->bits_per_pixel);
324 switch (var->bits_per_pixel) {
330 if (fbi->pcr & PCR_TFT)
331 rgb = &def_rgb_16_tft;
333 rgb = &def_rgb_16_stn;
341 * Copy the RGB parameters for this display
342 * from the machine specific parameters.
345 var->green = rgb->green;
346 var->blue = rgb->blue;
347 var->transp = rgb->transp;
349 pr_debug("RGBT length = %d:%d:%d:%d\n",
350 var->red.length, var->green.length, var->blue.length,
353 pr_debug("RGBT offset = %d:%d:%d:%d\n",
354 var->red.offset, var->green.offset, var->blue.offset,
362 * Set the user defined part of the display for the specified console
364 static int imxfb_set_par(struct fb_info *info)
366 struct imxfb_info *fbi = info->par;
367 struct fb_var_screeninfo *var = &info->var;
369 if (var->bits_per_pixel == 16 || var->bits_per_pixel == 32)
370 info->fix.visual = FB_VISUAL_TRUECOLOR;
371 else if (!fbi->cmap_static)
372 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
375 * Some people have weird ideas about wanting static
376 * pseudocolor maps. I suspect their user space
377 * applications are broken.
379 info->fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
382 info->fix.line_length = var->xres_virtual * var->bits_per_pixel / 8;
383 fbi->palette_size = var->bits_per_pixel == 8 ? 256 : 16;
385 imxfb_activate_var(var, info);
390 static void imxfb_enable_controller(struct imxfb_info *fbi)
392 pr_debug("Enabling LCD controller\n");
394 writel(fbi->screen_dma, fbi->regs + LCDC_SSA);
396 /* physical screen start address */
397 writel(VPW_VPW(fbi->max_xres * fbi->max_bpp / 8 / 4),
398 fbi->regs + LCDC_VPW);
400 /* panning offset 0 (0 pixel offset) */
401 writel(0x00000000, fbi->regs + LCDC_POS);
403 /* disable hardware cursor */
404 writel(readl(fbi->regs + LCDC_CPOS) & ~(CPOS_CC0 | CPOS_CC1),
405 fbi->regs + LCDC_CPOS);
407 writel(RMCR_LCDC_EN, fbi->regs + LCDC_RMCR);
409 clk_enable(fbi->clk);
411 if (fbi->backlight_power)
412 fbi->backlight_power(1);
417 static void imxfb_disable_controller(struct imxfb_info *fbi)
419 pr_debug("Disabling LCD controller\n");
421 if (fbi->backlight_power)
422 fbi->backlight_power(0);
426 clk_disable(fbi->clk);
428 writel(0, fbi->regs + LCDC_RMCR);
431 static int imxfb_blank(int blank, struct fb_info *info)
433 struct imxfb_info *fbi = info->par;
435 pr_debug("imxfb_blank: blank=%d\n", blank);
438 case FB_BLANK_POWERDOWN:
439 case FB_BLANK_VSYNC_SUSPEND:
440 case FB_BLANK_HSYNC_SUSPEND:
441 case FB_BLANK_NORMAL:
442 imxfb_disable_controller(fbi);
445 case FB_BLANK_UNBLANK:
446 imxfb_enable_controller(fbi);
452 static struct fb_ops imxfb_ops = {
453 .owner = THIS_MODULE,
454 .fb_check_var = imxfb_check_var,
455 .fb_set_par = imxfb_set_par,
456 .fb_setcolreg = imxfb_setcolreg,
457 .fb_fillrect = cfb_fillrect,
458 .fb_copyarea = cfb_copyarea,
459 .fb_imageblit = cfb_imageblit,
460 .fb_blank = imxfb_blank,
464 * imxfb_activate_var():
465 * Configures LCD Controller based on entries in var parameter. Settings are
466 * only written to the controller if changes were made.
468 static int imxfb_activate_var(struct fb_var_screeninfo *var, struct fb_info *info)
470 struct imxfb_info *fbi = info->par;
471 unsigned int pcr, lcd_clk;
472 unsigned long long tmp;
474 pr_debug("var: xres=%d hslen=%d lm=%d rm=%d\n",
475 var->xres, var->hsync_len,
476 var->left_margin, var->right_margin);
477 pr_debug("var: yres=%d vslen=%d um=%d bm=%d\n",
478 var->yres, var->vsync_len,
479 var->upper_margin, var->lower_margin);
482 if (var->xres < 16 || var->xres > 1024)
483 printk(KERN_ERR "%s: invalid xres %d\n",
484 info->fix.id, var->xres);
485 if (var->hsync_len < 1 || var->hsync_len > 64)
486 printk(KERN_ERR "%s: invalid hsync_len %d\n",
487 info->fix.id, var->hsync_len);
488 if (var->left_margin > 255)
489 printk(KERN_ERR "%s: invalid left_margin %d\n",
490 info->fix.id, var->left_margin);
491 if (var->right_margin > 255)
492 printk(KERN_ERR "%s: invalid right_margin %d\n",
493 info->fix.id, var->right_margin);
494 if (var->yres < 1 || var->yres > 511)
495 printk(KERN_ERR "%s: invalid yres %d\n",
496 info->fix.id, var->yres);
497 if (var->vsync_len > 100)
498 printk(KERN_ERR "%s: invalid vsync_len %d\n",
499 info->fix.id, var->vsync_len);
500 if (var->upper_margin > 63)
501 printk(KERN_ERR "%s: invalid upper_margin %d\n",
502 info->fix.id, var->upper_margin);
503 if (var->lower_margin > 255)
504 printk(KERN_ERR "%s: invalid lower_margin %d\n",
505 info->fix.id, var->lower_margin);
508 writel(HCR_H_WIDTH(var->hsync_len - 1) |
509 HCR_H_WAIT_1(var->right_margin - 1) |
510 HCR_H_WAIT_2(var->left_margin - 3),
511 fbi->regs + LCDC_HCR);
513 writel(VCR_V_WIDTH(var->vsync_len) |
514 VCR_V_WAIT_1(var->lower_margin) |
515 VCR_V_WAIT_2(var->upper_margin),
516 fbi->regs + LCDC_VCR);
518 writel(SIZE_XMAX(var->xres) | SIZE_YMAX(var->yres),
519 fbi->regs + LCDC_SIZE);
521 lcd_clk = clk_get_rate(fbi->clk);
522 tmp = var->pixclock * (unsigned long long)lcd_clk;
523 do_div(tmp, 1000000);
524 if (do_div(tmp, 1000000) > 500000)
526 pcr = (unsigned int)tmp;
529 printk(KERN_WARNING "Must limit pixel clock to %uHz\n",
533 /* add sync polarities */
534 pcr |= fbi->pcr & ~0x3F;
536 writel(pcr, fbi->regs + LCDC_PCR);
537 writel(fbi->pwmr, fbi->regs + LCDC_PWMR);
538 writel(fbi->lscr1, fbi->regs + LCDC_LSCR1);
539 writel(fbi->dmacr, fbi->regs + LCDC_DMACR);
546 * Power management hooks. Note that we won't be called from IRQ context,
547 * unlike the blank functions above, so we may sleep.
549 static int imxfb_suspend(struct platform_device *dev, pm_message_t state)
551 struct imxfb_info *fbi = platform_get_drvdata(dev);
553 pr_debug("%s\n", __func__);
555 imxfb_disable_controller(fbi);
559 static int imxfb_resume(struct platform_device *dev)
561 struct imxfb_info *fbi = platform_get_drvdata(dev);
563 pr_debug("%s\n", __func__);
565 imxfb_enable_controller(fbi);
569 #define imxfb_suspend NULL
570 #define imxfb_resume NULL
573 static int __init imxfb_init_fbinfo(struct platform_device *pdev)
575 struct imx_fb_platform_data *pdata = pdev->dev.platform_data;
576 struct fb_info *info = dev_get_drvdata(&pdev->dev);
577 struct imxfb_info *fbi = info->par;
579 pr_debug("%s\n",__func__);
581 info->pseudo_palette = kmalloc(sizeof(u32) * 16, GFP_KERNEL);
582 if (!info->pseudo_palette)
585 memset(fbi, 0, sizeof(struct imxfb_info));
587 strlcpy(info->fix.id, IMX_NAME, sizeof(info->fix.id));
589 info->fix.type = FB_TYPE_PACKED_PIXELS;
590 info->fix.type_aux = 0;
591 info->fix.xpanstep = 0;
592 info->fix.ypanstep = 0;
593 info->fix.ywrapstep = 0;
594 info->fix.accel = FB_ACCEL_NONE;
596 info->var.nonstd = 0;
597 info->var.activate = FB_ACTIVATE_NOW;
598 info->var.height = -1;
599 info->var.width = -1;
600 info->var.accel_flags = 0;
601 info->var.vmode = FB_VMODE_NONINTERLACED;
603 info->fbops = &imxfb_ops;
604 info->flags = FBINFO_FLAG_DEFAULT |
607 fbi->max_xres = pdata->xres;
608 info->var.xres = pdata->xres;
609 info->var.xres_virtual = pdata->xres;
610 fbi->max_yres = pdata->yres;
611 info->var.yres = pdata->yres;
612 info->var.yres_virtual = pdata->yres;
613 fbi->max_bpp = pdata->bpp;
614 info->var.bits_per_pixel = pdata->bpp;
615 info->var.nonstd = pdata->nonstd;
616 info->var.pixclock = pdata->pixclock;
617 info->var.hsync_len = pdata->hsync_len;
618 info->var.left_margin = pdata->left_margin;
619 info->var.right_margin = pdata->right_margin;
620 info->var.vsync_len = pdata->vsync_len;
621 info->var.upper_margin = pdata->upper_margin;
622 info->var.lower_margin = pdata->lower_margin;
623 info->var.sync = pdata->sync;
624 info->var.grayscale = pdata->cmap_greyscale;
625 fbi->cmap_inverse = pdata->cmap_inverse;
626 fbi->cmap_static = pdata->cmap_static;
627 fbi->pcr = pdata->pcr;
628 fbi->lscr1 = pdata->lscr1;
629 fbi->dmacr = pdata->dmacr;
630 fbi->pwmr = pdata->pwmr;
631 fbi->lcd_power = pdata->lcd_power;
632 fbi->backlight_power = pdata->backlight_power;
633 info->fix.smem_len = fbi->max_xres * fbi->max_yres *
639 static int __init imxfb_probe(struct platform_device *pdev)
641 struct imxfb_info *fbi;
642 struct fb_info *info;
643 struct imx_fb_platform_data *pdata;
644 struct resource *res;
647 printk("i.MX Framebuffer driver\n");
649 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
653 pdata = pdev->dev.platform_data;
655 dev_err(&pdev->dev,"No platform_data available\n");
659 info = framebuffer_alloc(sizeof(struct imxfb_info), &pdev->dev);
665 platform_set_drvdata(pdev, info);
667 ret = imxfb_init_fbinfo(pdev);
671 res = request_mem_region(res->start, resource_size(res),
678 fbi->clk = clk_get(&pdev->dev, NULL);
679 if (IS_ERR(fbi->clk)) {
680 ret = PTR_ERR(fbi->clk);;
681 dev_err(&pdev->dev, "unable to get clock: %d\n", ret);
682 goto failed_getclock;
685 fbi->regs = ioremap(res->start, resource_size(res));
686 if (fbi->regs == NULL) {
687 printk(KERN_ERR"Cannot map frame buffer registers\n");
691 if (!pdata->fixed_screen_cpu) {
692 fbi->map_size = PAGE_ALIGN(info->fix.smem_len);
693 fbi->map_cpu = dma_alloc_writecombine(&pdev->dev,
694 fbi->map_size, &fbi->map_dma, GFP_KERNEL);
697 dev_err(&pdev->dev, "Failed to allocate video RAM: %d\n", ret);
702 info->screen_base = fbi->map_cpu;
703 fbi->screen_cpu = fbi->map_cpu;
704 fbi->screen_dma = fbi->map_dma;
705 info->fix.smem_start = fbi->screen_dma;
707 /* Fixed framebuffer mapping enables location of the screen in eSRAM */
708 fbi->map_cpu = pdata->fixed_screen_cpu;
709 fbi->map_dma = pdata->fixed_screen_dma;
710 info->screen_base = fbi->map_cpu;
711 fbi->screen_cpu = fbi->map_cpu;
712 fbi->screen_dma = fbi->map_dma;
713 info->fix.smem_start = fbi->screen_dma;
717 ret = pdata->init(fbi->pdev);
719 goto failed_platform_init;
723 * This makes sure that our colour bitfield
724 * descriptors are correctly initialised.
726 imxfb_check_var(&info->var, info);
728 ret = fb_alloc_cmap(&info->cmap, 1 << info->var.bits_per_pixel, 0);
733 ret = register_framebuffer(info);
735 dev_err(&pdev->dev, "failed to register framebuffer\n");
736 goto failed_register;
739 imxfb_enable_controller(fbi);
744 fb_dealloc_cmap(&info->cmap);
747 pdata->exit(fbi->pdev);
748 failed_platform_init:
749 if (!pdata->fixed_screen_cpu)
750 dma_free_writecombine(&pdev->dev,fbi->map_size,fbi->map_cpu,
757 release_mem_region(res->start, res->end - res->start);
759 kfree(info->pseudo_palette);
761 platform_set_drvdata(pdev, NULL);
762 framebuffer_release(info);
766 static int __devexit imxfb_remove(struct platform_device *pdev)
768 struct imx_fb_platform_data *pdata;
769 struct fb_info *info = platform_get_drvdata(pdev);
770 struct imxfb_info *fbi = info->par;
771 struct resource *res;
773 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
775 imxfb_disable_controller(fbi);
777 unregister_framebuffer(info);
779 pdata = pdev->dev.platform_data;
781 pdata->exit(fbi->pdev);
783 fb_dealloc_cmap(&info->cmap);
784 kfree(info->pseudo_palette);
785 framebuffer_release(info);
788 release_mem_region(res->start, res->end - res->start + 1);
789 clk_disable(fbi->clk);
792 platform_set_drvdata(pdev, NULL);
797 void imxfb_shutdown(struct platform_device * dev)
799 struct fb_info *info = platform_get_drvdata(dev);
800 struct imxfb_info *fbi = info->par;
801 imxfb_disable_controller(fbi);
804 static struct platform_driver imxfb_driver = {
805 .suspend = imxfb_suspend,
806 .resume = imxfb_resume,
807 .remove = __devexit_p(imxfb_remove),
808 .shutdown = imxfb_shutdown,
814 int __init imxfb_init(void)
816 return platform_driver_probe(&imxfb_driver, imxfb_probe);
819 static void __exit imxfb_cleanup(void)
821 platform_driver_unregister(&imxfb_driver);
824 module_init(imxfb_init);
825 module_exit(imxfb_cleanup);
827 MODULE_DESCRIPTION("Motorola i.MX framebuffer driver");
828 MODULE_AUTHOR("Sascha Hauer, Pengutronix");
829 MODULE_LICENSE("GPL");