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
21 #include <linux/config.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/errno.h>
26 #include <linux/string.h>
27 #include <linux/interrupt.h>
28 #include <linux/slab.h>
30 #include <linux/delay.h>
31 #include <linux/init.h>
32 #include <linux/ioport.h>
33 #include <linux/cpufreq.h>
34 #include <linux/platform_device.h>
35 #include <linux/dma-mapping.h>
37 #include <asm/hardware.h>
39 #include <asm/uaccess.h>
40 #include <asm/arch/imxfb.h>
43 * Complain if VAR is out of range.
49 static struct imxfb_rgb def_rgb_16
= {
50 .red
= { .offset
= 8, .length
= 4, },
51 .green
= { .offset
= 4, .length
= 4, },
52 .blue
= { .offset
= 0, .length
= 4, },
53 .transp
= { .offset
= 0, .length
= 0, },
56 static struct imxfb_rgb def_rgb_8
= {
57 .red
= { .offset
= 0, .length
= 8, },
58 .green
= { .offset
= 0, .length
= 8, },
59 .blue
= { .offset
= 0, .length
= 8, },
60 .transp
= { .offset
= 0, .length
= 0, },
63 static int imxfb_activate_var(struct fb_var_screeninfo
*var
, struct fb_info
*info
);
65 static inline u_int
chan_to_field(u_int chan
, struct fb_bitfield
*bf
)
68 chan
>>= 16 - bf
->length
;
69 return chan
<< bf
->offset
;
72 #define LCDC_PALETTE(x) __REG2(IMX_LCDC_BASE+0x800, (x)<<2)
74 imxfb_setpalettereg(u_int regno
, u_int red
, u_int green
, u_int blue
,
75 u_int trans
, struct fb_info
*info
)
77 struct imxfb_info
*fbi
= info
->par
;
80 #define CNVT_TOHW(val,width) ((((val)<<(width))+0x7FFF-(val))>>16)
81 if (regno
< fbi
->palette_size
) {
82 val
= (CNVT_TOHW(red
, 4) << 8) |
83 (CNVT_TOHW(green
,4) << 4) |
86 LCDC_PALETTE(regno
) = val
;
93 imxfb_setcolreg(u_int regno
, u_int red
, u_int green
, u_int blue
,
94 u_int trans
, struct fb_info
*info
)
96 struct imxfb_info
*fbi
= info
->par
;
101 * If inverse mode was selected, invert all the colours
102 * rather than the register number. The register number
103 * is what you poke into the framebuffer to produce the
104 * colour you requested.
106 if (fbi
->cmap_inverse
) {
108 green
= 0xffff - green
;
109 blue
= 0xffff - blue
;
113 * If greyscale is true, then we convert the RGB value
114 * to greyscale no mater what visual we are using.
116 if (info
->var
.grayscale
)
117 red
= green
= blue
= (19595 * red
+ 38470 * green
+
120 switch (info
->fix
.visual
) {
121 case FB_VISUAL_TRUECOLOR
:
123 * 12 or 16-bit True Colour. We encode the RGB value
124 * according to the RGB bitfield information.
127 u32
*pal
= info
->pseudo_palette
;
129 val
= chan_to_field(red
, &info
->var
.red
);
130 val
|= chan_to_field(green
, &info
->var
.green
);
131 val
|= chan_to_field(blue
, &info
->var
.blue
);
138 case FB_VISUAL_STATIC_PSEUDOCOLOR
:
139 case FB_VISUAL_PSEUDOCOLOR
:
140 ret
= imxfb_setpalettereg(regno
, red
, green
, blue
, trans
, info
);
149 * Round up in the following order: bits_per_pixel, xres,
150 * yres, xres_virtual, yres_virtual, xoffset, yoffset, grayscale,
151 * bitfields, horizontal timing, vertical timing.
154 imxfb_check_var(struct fb_var_screeninfo
*var
, struct fb_info
*info
)
156 struct imxfb_info
*fbi
= info
->par
;
159 if (var
->xres
< MIN_XRES
)
160 var
->xres
= MIN_XRES
;
161 if (var
->yres
< MIN_YRES
)
162 var
->yres
= MIN_YRES
;
163 if (var
->xres
> fbi
->max_xres
)
164 var
->xres
= fbi
->max_xres
;
165 if (var
->yres
> fbi
->max_yres
)
166 var
->yres
= fbi
->max_yres
;
167 var
->xres_virtual
= max(var
->xres_virtual
, var
->xres
);
168 var
->yres_virtual
= max(var
->yres_virtual
, var
->yres
);
170 pr_debug("var->bits_per_pixel=%d\n", var
->bits_per_pixel
);
171 switch (var
->bits_per_pixel
) {
183 * Copy the RGB parameters for this display
184 * from the machine specific parameters.
186 var
->red
= fbi
->rgb
[rgbidx
]->red
;
187 var
->green
= fbi
->rgb
[rgbidx
]->green
;
188 var
->blue
= fbi
->rgb
[rgbidx
]->blue
;
189 var
->transp
= fbi
->rgb
[rgbidx
]->transp
;
191 pr_debug("RGBT length = %d:%d:%d:%d\n",
192 var
->red
.length
, var
->green
.length
, var
->blue
.length
,
195 pr_debug("RGBT offset = %d:%d:%d:%d\n",
196 var
->red
.offset
, var
->green
.offset
, var
->blue
.offset
,
204 * Set the user defined part of the display for the specified console
206 static int imxfb_set_par(struct fb_info
*info
)
208 struct imxfb_info
*fbi
= info
->par
;
209 struct fb_var_screeninfo
*var
= &info
->var
;
211 pr_debug("set_par\n");
213 if (var
->bits_per_pixel
== 16)
214 info
->fix
.visual
= FB_VISUAL_TRUECOLOR
;
215 else if (!fbi
->cmap_static
)
216 info
->fix
.visual
= FB_VISUAL_PSEUDOCOLOR
;
219 * Some people have weird ideas about wanting static
220 * pseudocolor maps. I suspect their user space
221 * applications are broken.
223 info
->fix
.visual
= FB_VISUAL_STATIC_PSEUDOCOLOR
;
226 info
->fix
.line_length
= var
->xres_virtual
*
227 var
->bits_per_pixel
/ 8;
228 fbi
->palette_size
= var
->bits_per_pixel
== 8 ? 256 : 16;
230 imxfb_activate_var(var
, info
);
235 static void imxfb_enable_controller(struct imxfb_info
*fbi
)
237 pr_debug("Enabling LCD controller\n");
239 /* initialize LCDC */
240 LCDC_RMCR
&= ~RMCR_LCDC_EN
; /* just to be safe... */
242 LCDC_SSA
= fbi
->screen_dma
;
243 /* physical screen start address */
244 LCDC_VPW
= VPW_VPW(fbi
->max_xres
* fbi
->max_bpp
/ 8 / 4);
246 LCDC_POS
= 0x00000000; /* panning offset 0 (0 pixel offset) */
248 /* disable hardware cursor */
249 LCDC_CPOS
&= ~(CPOS_CC0
| CPOS_CC1
);
251 LCDC_RMCR
= RMCR_LCDC_EN
;
253 if(fbi
->backlight_power
)
254 fbi
->backlight_power(1);
259 static void imxfb_disable_controller(struct imxfb_info
*fbi
)
261 pr_debug("Disabling LCD controller\n");
263 if(fbi
->backlight_power
)
264 fbi
->backlight_power(0);
271 static int imxfb_blank(int blank
, struct fb_info
*info
)
273 struct imxfb_info
*fbi
= info
->par
;
275 pr_debug("imxfb_blank: blank=%d\n", blank
);
278 case FB_BLANK_POWERDOWN
:
279 case FB_BLANK_VSYNC_SUSPEND
:
280 case FB_BLANK_HSYNC_SUSPEND
:
281 case FB_BLANK_NORMAL
:
282 imxfb_disable_controller(fbi
);
285 case FB_BLANK_UNBLANK
:
286 imxfb_enable_controller(fbi
);
292 static struct fb_ops imxfb_ops
= {
293 .owner
= THIS_MODULE
,
294 .fb_check_var
= imxfb_check_var
,
295 .fb_set_par
= imxfb_set_par
,
296 .fb_setcolreg
= imxfb_setcolreg
,
297 .fb_fillrect
= cfb_fillrect
,
298 .fb_copyarea
= cfb_copyarea
,
299 .fb_imageblit
= cfb_imageblit
,
300 .fb_blank
= imxfb_blank
,
301 .fb_cursor
= soft_cursor
, /* FIXME: i.MX can do hardware cursor */
305 * imxfb_activate_var():
306 * Configures LCD Controller based on entries in var parameter. Settings are
307 * only written to the controller if changes were made.
309 static int imxfb_activate_var(struct fb_var_screeninfo
*var
, struct fb_info
*info
)
311 struct imxfb_info
*fbi
= info
->par
;
312 pr_debug("var: xres=%d hslen=%d lm=%d rm=%d\n",
313 var
->xres
, var
->hsync_len
,
314 var
->left_margin
, var
->right_margin
);
315 pr_debug("var: yres=%d vslen=%d um=%d bm=%d\n",
316 var
->yres
, var
->vsync_len
,
317 var
->upper_margin
, var
->lower_margin
);
320 if (var
->xres
< 16 || var
->xres
> 1024)
321 printk(KERN_ERR
"%s: invalid xres %d\n",
322 info
->fix
.id
, var
->xres
);
323 if (var
->hsync_len
< 1 || var
->hsync_len
> 64)
324 printk(KERN_ERR
"%s: invalid hsync_len %d\n",
325 info
->fix
.id
, var
->hsync_len
);
326 if (var
->left_margin
> 255)
327 printk(KERN_ERR
"%s: invalid left_margin %d\n",
328 info
->fix
.id
, var
->left_margin
);
329 if (var
->right_margin
> 255)
330 printk(KERN_ERR
"%s: invalid right_margin %d\n",
331 info
->fix
.id
, var
->right_margin
);
332 if (var
->yres
< 1 || var
->yres
> 511)
333 printk(KERN_ERR
"%s: invalid yres %d\n",
334 info
->fix
.id
, var
->yres
);
335 if (var
->vsync_len
> 100)
336 printk(KERN_ERR
"%s: invalid vsync_len %d\n",
337 info
->fix
.id
, var
->vsync_len
);
338 if (var
->upper_margin
> 63)
339 printk(KERN_ERR
"%s: invalid upper_margin %d\n",
340 info
->fix
.id
, var
->upper_margin
);
341 if (var
->lower_margin
> 255)
342 printk(KERN_ERR
"%s: invalid lower_margin %d\n",
343 info
->fix
.id
, var
->lower_margin
);
346 LCDC_HCR
= HCR_H_WIDTH(var
->hsync_len
) |
347 HCR_H_WAIT_1(var
->left_margin
) |
348 HCR_H_WAIT_2(var
->right_margin
);
350 LCDC_VCR
= VCR_V_WIDTH(var
->vsync_len
) |
351 VCR_V_WAIT_1(var
->upper_margin
) |
352 VCR_V_WAIT_2(var
->lower_margin
);
354 LCDC_SIZE
= SIZE_XMAX(var
->xres
) | SIZE_YMAX(var
->yres
);
356 LCDC_PWMR
= fbi
->pwmr
;
357 LCDC_LSCR1
= fbi
->lscr1
;
358 LCDC_DMACR
= fbi
->dmacr
;
363 static void imxfb_setup_gpio(struct imxfb_info
*fbi
)
367 LCDC_RMCR
&= ~(RMCR_LCDC_EN
| RMCR_SELF_REF
);
369 if( fbi
->pcr
& PCR_TFT
)
372 width
= 1 << ((fbi
->pcr
>> 28) & 0x3);
376 imx_gpio_mode(PD30_PF_LD15
);
377 imx_gpio_mode(PD29_PF_LD14
);
378 imx_gpio_mode(PD28_PF_LD13
);
379 imx_gpio_mode(PD27_PF_LD12
);
380 imx_gpio_mode(PD26_PF_LD11
);
381 imx_gpio_mode(PD25_PF_LD10
);
382 imx_gpio_mode(PD24_PF_LD9
);
383 imx_gpio_mode(PD23_PF_LD8
);
385 imx_gpio_mode(PD22_PF_LD7
);
386 imx_gpio_mode(PD21_PF_LD6
);
387 imx_gpio_mode(PD20_PF_LD5
);
388 imx_gpio_mode(PD19_PF_LD4
);
390 imx_gpio_mode(PD18_PF_LD3
);
391 imx_gpio_mode(PD17_PF_LD2
);
393 imx_gpio_mode(PD16_PF_LD1
);
395 imx_gpio_mode(PD15_PF_LD0
);
398 /* initialize GPIOs */
399 imx_gpio_mode(PD6_PF_LSCLK
);
400 imx_gpio_mode(PD10_PF_SPL_SPR
);
401 imx_gpio_mode(PD11_PF_CONTRAST
);
402 imx_gpio_mode(PD14_PF_FLM_VSYNC
);
403 imx_gpio_mode(PD13_PF_LP_HSYNC
);
404 imx_gpio_mode(PD7_PF_REV
);
405 imx_gpio_mode(PD8_PF_CLS
);
407 #ifndef CONFIG_MACH_PIMX1
408 /* on PiMX1 used as buffers enable signal
410 imx_gpio_mode(PD9_PF_PS
);
413 #ifndef CONFIG_MACH_MX1FS2
414 /* on mx1fs2 this pin is used to (de)activate the display, so we need
415 * it as a normal gpio
417 imx_gpio_mode(PD12_PF_ACD_OE
);
424 * Power management hooks. Note that we won't be called from IRQ context,
425 * unlike the blank functions above, so we may sleep.
427 static int imxfb_suspend(struct device
*dev
, pm_message_t state
)
429 struct imxfb_info
*fbi
= dev_get_drvdata(dev
);
430 pr_debug("%s\n",__FUNCTION__
);
432 imxfb_disable_controller(fbi
);
436 static int imxfb_resume(struct device
*dev
)
438 struct imxfb_info
*fbi
= dev_get_drvdata(dev
);
439 pr_debug("%s\n",__FUNCTION__
);
441 imxfb_enable_controller(fbi
);
445 #define imxfb_suspend NULL
446 #define imxfb_resume NULL
449 static int __init
imxfb_init_fbinfo(struct device
*dev
)
451 struct imxfb_mach_info
*inf
= dev
->platform_data
;
452 struct fb_info
*info
= dev_get_drvdata(dev
);
453 struct imxfb_info
*fbi
= info
->par
;
455 pr_debug("%s\n",__FUNCTION__
);
457 info
->pseudo_palette
= kmalloc( sizeof(u32
) * 16, GFP_KERNEL
);
458 if (!info
->pseudo_palette
)
461 memset(fbi
, 0, sizeof(struct imxfb_info
));
464 strlcpy(info
->fix
.id
, IMX_NAME
, sizeof(info
->fix
.id
));
466 info
->fix
.type
= FB_TYPE_PACKED_PIXELS
;
467 info
->fix
.type_aux
= 0;
468 info
->fix
.xpanstep
= 0;
469 info
->fix
.ypanstep
= 0;
470 info
->fix
.ywrapstep
= 0;
471 info
->fix
.accel
= FB_ACCEL_NONE
;
473 info
->var
.nonstd
= 0;
474 info
->var
.activate
= FB_ACTIVATE_NOW
;
475 info
->var
.height
= -1;
476 info
->var
.width
= -1;
477 info
->var
.accel_flags
= 0;
478 info
->var
.vmode
= FB_VMODE_NONINTERLACED
;
480 info
->fbops
= &imxfb_ops
;
481 info
->flags
= FBINFO_FLAG_DEFAULT
;
482 info
->pseudo_palette
= (fbi
+ 1);
484 fbi
->rgb
[RGB_16
] = &def_rgb_16
;
485 fbi
->rgb
[RGB_8
] = &def_rgb_8
;
487 fbi
->max_xres
= inf
->xres
;
488 info
->var
.xres
= inf
->xres
;
489 info
->var
.xres_virtual
= inf
->xres
;
490 fbi
->max_yres
= inf
->yres
;
491 info
->var
.yres
= inf
->yres
;
492 info
->var
.yres_virtual
= inf
->yres
;
493 fbi
->max_bpp
= inf
->bpp
;
494 info
->var
.bits_per_pixel
= inf
->bpp
;
495 info
->var
.pixclock
= inf
->pixclock
;
496 info
->var
.hsync_len
= inf
->hsync_len
;
497 info
->var
.left_margin
= inf
->left_margin
;
498 info
->var
.right_margin
= inf
->right_margin
;
499 info
->var
.vsync_len
= inf
->vsync_len
;
500 info
->var
.upper_margin
= inf
->upper_margin
;
501 info
->var
.lower_margin
= inf
->lower_margin
;
502 info
->var
.sync
= inf
->sync
;
503 info
->var
.grayscale
= inf
->cmap_greyscale
;
504 fbi
->cmap_inverse
= inf
->cmap_inverse
;
506 fbi
->lscr1
= inf
->lscr1
;
507 fbi
->dmacr
= inf
->dmacr
;
508 fbi
->pwmr
= inf
->pwmr
;
509 fbi
->lcd_power
= inf
->lcd_power
;
510 fbi
->backlight_power
= inf
->backlight_power
;
511 info
->fix
.smem_len
= fbi
->max_xres
* fbi
->max_yres
*
518 * Allocates the DRAM memory for the frame buffer. This buffer is
519 * remapped into a non-cached, non-buffered, memory region to
520 * allow pixel writes to occur without flushing the cache.
521 * Once this area is remapped, all virtual memory access to the
522 * video memory should occur at the new region.
524 static int __init
imxfb_map_video_memory(struct fb_info
*info
)
526 struct imxfb_info
*fbi
= info
->par
;
528 fbi
->map_size
= PAGE_ALIGN(info
->fix
.smem_len
);
529 fbi
->map_cpu
= dma_alloc_writecombine(fbi
->dev
, fbi
->map_size
,
530 &fbi
->map_dma
,GFP_KERNEL
);
533 info
->screen_base
= fbi
->map_cpu
;
534 fbi
->screen_cpu
= fbi
->map_cpu
;
535 fbi
->screen_dma
= fbi
->map_dma
;
536 info
->fix
.smem_start
= fbi
->screen_dma
;
539 return fbi
->map_cpu
? 0 : -ENOMEM
;
542 static int __init
imxfb_probe(struct device
*dev
)
544 struct platform_device
*pdev
= to_platform_device(dev
);
545 struct imxfb_info
*fbi
;
546 struct fb_info
*info
;
547 struct imxfb_mach_info
*inf
;
548 struct resource
*res
;
551 printk("i.MX Framebuffer driver\n");
553 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
557 inf
= dev
->platform_data
;
559 dev_err(dev
,"No platform_data available\n");
563 info
= framebuffer_alloc(sizeof(struct imxfb_info
), dev
);
569 dev_set_drvdata(dev
, info
);
571 ret
= imxfb_init_fbinfo(dev
);
575 res
= request_mem_region(res
->start
, res
->end
- res
->start
+ 1, "IMXFB");
581 if (!inf
->fixed_screen_cpu
) {
582 ret
= imxfb_map_video_memory(info
);
584 dev_err(dev
, "Failed to allocate video RAM: %d\n", ret
);
589 /* Fixed framebuffer mapping enables location of the screen in eSRAM */
590 fbi
->map_cpu
= inf
->fixed_screen_cpu
;
591 fbi
->map_dma
= inf
->fixed_screen_dma
;
592 info
->screen_base
= fbi
->map_cpu
;
593 fbi
->screen_cpu
= fbi
->map_cpu
;
594 fbi
->screen_dma
= fbi
->map_dma
;
595 info
->fix
.smem_start
= fbi
->screen_dma
;
599 * This makes sure that our colour bitfield
600 * descriptors are correctly initialised.
602 imxfb_check_var(&info
->var
, info
);
604 ret
= fb_alloc_cmap(&info
->cmap
, 1<<info
->var
.bits_per_pixel
, 0);
608 imxfb_setup_gpio(fbi
);
611 ret
= register_framebuffer(info
);
613 dev_err(dev
, "failed to register framebuffer\n");
614 goto failed_register
;
617 imxfb_enable_controller(fbi
);
622 fb_dealloc_cmap(&info
->cmap
);
624 if (!inf
->fixed_screen_cpu
)
625 dma_free_writecombine(dev
,fbi
->map_size
,fbi
->map_cpu
,
628 kfree(info
->pseudo_palette
);
630 release_mem_region(res
->start
, res
->end
- res
->start
);
632 dev_set_drvdata(dev
, NULL
);
633 framebuffer_release(info
);
637 static int imxfb_remove(struct device
*dev
)
639 struct platform_device
*pdev
= to_platform_device(dev
);
640 struct fb_info
*info
= dev_get_drvdata(dev
);
641 struct imxfb_info
*fbi
= info
->par
;
642 struct resource
*res
;
644 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
646 imxfb_disable_controller(fbi
);
648 unregister_framebuffer(info
);
650 fb_dealloc_cmap(&info
->cmap
);
651 kfree(info
->pseudo_palette
);
652 framebuffer_release(info
);
654 release_mem_region(res
->start
, res
->end
- res
->start
+ 1);
655 dev_set_drvdata(dev
, NULL
);
660 void imxfb_shutdown(struct device
* dev
)
662 struct fb_info
*info
= dev_get_drvdata(dev
);
663 struct imxfb_info
*fbi
= info
->par
;
664 imxfb_disable_controller(fbi
);
667 static struct device_driver imxfb_driver
= {
669 .bus
= &platform_bus_type
,
670 .probe
= imxfb_probe
,
671 .suspend
= imxfb_suspend
,
672 .resume
= imxfb_resume
,
673 .remove
= imxfb_remove
,
674 .shutdown
= imxfb_shutdown
,
677 int __init
imxfb_init(void)
679 return driver_register(&imxfb_driver
);
682 static void __exit
imxfb_cleanup(void)
684 driver_unregister(&imxfb_driver
);
687 module_init(imxfb_init
);
688 module_exit(imxfb_cleanup
);
690 MODULE_DESCRIPTION("Motorola i.MX framebuffer driver");
691 MODULE_AUTHOR("Sascha Hauer, Pengutronix");
692 MODULE_LICENSE("GPL");