[PATCH] Corgi Keyboard: Add some power management code
[linux-2.6.22.y-op.git] / drivers / video / imxfb.c
blobcabd53cec9913884981ddb38b05487d85e78e17f
1 /*
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
11 * more details.
13 * Please direct your questions and comments on this driver to the following
14 * email address:
16 * linux-arm-kernel@lists.arm.linux.org.uk
19 //#define DEBUG 1
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>
29 #include <linux/fb.h>
30 #include <linux/delay.h>
31 #include <linux/init.h>
32 #include <linux/ioport.h>
33 #include <linux/cpufreq.h>
34 #include <linux/device.h>
35 #include <linux/dma-mapping.h>
37 #include <asm/hardware.h>
38 #include <asm/io.h>
39 #include <asm/mach-types.h>
40 #include <asm/uaccess.h>
41 #include <asm/arch/imxfb.h>
44 * Complain if VAR is out of range.
46 #define DEBUG_VAR 1
48 #include "imxfb.h"
50 static struct imxfb_rgb def_rgb_16 = {
51 .red = { .offset = 8, .length = 4, },
52 .green = { .offset = 4, .length = 4, },
53 .blue = { .offset = 0, .length = 4, },
54 .transp = { .offset = 0, .length = 0, },
57 static struct imxfb_rgb def_rgb_8 = {
58 .red = { .offset = 0, .length = 8, },
59 .green = { .offset = 0, .length = 8, },
60 .blue = { .offset = 0, .length = 8, },
61 .transp = { .offset = 0, .length = 0, },
64 static int imxfb_activate_var(struct fb_var_screeninfo *var, struct fb_info *info);
66 static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
68 chan &= 0xffff;
69 chan >>= 16 - bf->length;
70 return chan << bf->offset;
73 #define LCDC_PALETTE(x) __REG2(IMX_LCDC_BASE+0x800, (x)<<2)
74 static int
75 imxfb_setpalettereg(u_int regno, u_int red, u_int green, u_int blue,
76 u_int trans, struct fb_info *info)
78 struct imxfb_info *fbi = info->par;
79 u_int val, ret = 1;
81 #define CNVT_TOHW(val,width) ((((val)<<(width))+0x7FFF-(val))>>16)
82 if (regno < fbi->palette_size) {
83 val = (CNVT_TOHW(red, 4) << 8) |
84 (CNVT_TOHW(green,4) << 4) |
85 CNVT_TOHW(blue, 4);
87 LCDC_PALETTE(regno) = val;
88 ret = 0;
90 return ret;
93 static int
94 imxfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
95 u_int trans, struct fb_info *info)
97 struct imxfb_info *fbi = info->par;
98 unsigned int val;
99 int ret = 1;
102 * If inverse mode was selected, invert all the colours
103 * rather than the register number. The register number
104 * is what you poke into the framebuffer to produce the
105 * colour you requested.
107 if (fbi->cmap_inverse) {
108 red = 0xffff - red;
109 green = 0xffff - green;
110 blue = 0xffff - blue;
114 * If greyscale is true, then we convert the RGB value
115 * to greyscale no mater what visual we are using.
117 if (info->var.grayscale)
118 red = green = blue = (19595 * red + 38470 * green +
119 7471 * blue) >> 16;
121 switch (info->fix.visual) {
122 case FB_VISUAL_TRUECOLOR:
124 * 12 or 16-bit True Colour. We encode the RGB value
125 * according to the RGB bitfield information.
127 if (regno < 16) {
128 u32 *pal = info->pseudo_palette;
130 val = chan_to_field(red, &info->var.red);
131 val |= chan_to_field(green, &info->var.green);
132 val |= chan_to_field(blue, &info->var.blue);
134 pal[regno] = val;
135 ret = 0;
137 break;
139 case FB_VISUAL_STATIC_PSEUDOCOLOR:
140 case FB_VISUAL_PSEUDOCOLOR:
141 ret = imxfb_setpalettereg(regno, red, green, blue, trans, info);
142 break;
145 return ret;
149 * imxfb_check_var():
150 * Round up in the following order: bits_per_pixel, xres,
151 * yres, xres_virtual, yres_virtual, xoffset, yoffset, grayscale,
152 * bitfields, horizontal timing, vertical timing.
154 static int
155 imxfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
157 struct imxfb_info *fbi = info->par;
158 int rgbidx;
160 if (var->xres < MIN_XRES)
161 var->xres = MIN_XRES;
162 if (var->yres < MIN_YRES)
163 var->yres = MIN_YRES;
164 if (var->xres > fbi->max_xres)
165 var->xres = fbi->max_xres;
166 if (var->yres > fbi->max_yres)
167 var->yres = fbi->max_yres;
168 var->xres_virtual = max(var->xres_virtual, var->xres);
169 var->yres_virtual = max(var->yres_virtual, var->yres);
171 pr_debug("var->bits_per_pixel=%d\n", var->bits_per_pixel);
172 switch (var->bits_per_pixel) {
173 case 16:
174 rgbidx = RGB_16;
175 break;
176 case 8:
177 rgbidx = RGB_8;
178 break;
179 default:
180 rgbidx = RGB_16;
184 * Copy the RGB parameters for this display
185 * from the machine specific parameters.
187 var->red = fbi->rgb[rgbidx]->red;
188 var->green = fbi->rgb[rgbidx]->green;
189 var->blue = fbi->rgb[rgbidx]->blue;
190 var->transp = fbi->rgb[rgbidx]->transp;
192 pr_debug("RGBT length = %d:%d:%d:%d\n",
193 var->red.length, var->green.length, var->blue.length,
194 var->transp.length);
196 pr_debug("RGBT offset = %d:%d:%d:%d\n",
197 var->red.offset, var->green.offset, var->blue.offset,
198 var->transp.offset);
200 return 0;
204 * imxfb_set_par():
205 * Set the user defined part of the display for the specified console
207 static int imxfb_set_par(struct fb_info *info)
209 struct imxfb_info *fbi = info->par;
210 struct fb_var_screeninfo *var = &info->var;
212 pr_debug("set_par\n");
214 if (var->bits_per_pixel == 16)
215 info->fix.visual = FB_VISUAL_TRUECOLOR;
216 else if (!fbi->cmap_static)
217 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
218 else {
220 * Some people have weird ideas about wanting static
221 * pseudocolor maps. I suspect their user space
222 * applications are broken.
224 info->fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
227 info->fix.line_length = var->xres_virtual *
228 var->bits_per_pixel / 8;
229 fbi->palette_size = var->bits_per_pixel == 8 ? 256 : 16;
231 imxfb_activate_var(var, info);
233 return 0;
236 static void imxfb_enable_controller(struct imxfb_info *fbi)
238 pr_debug("Enabling LCD controller\n");
240 /* initialize LCDC */
241 LCDC_RMCR &= ~RMCR_LCDC_EN; /* just to be safe... */
243 LCDC_SSA = fbi->screen_dma;
244 /* physical screen start address */
245 LCDC_VPW = VPW_VPW(fbi->max_xres * fbi->max_bpp / 8 / 4);
247 LCDC_POS = 0x00000000; /* panning offset 0 (0 pixel offset) */
249 /* disable hardware cursor */
250 LCDC_CPOS &= ~(CPOS_CC0 | CPOS_CC1);
252 LCDC_RMCR = RMCR_LCDC_EN;
254 if(fbi->backlight_power)
255 fbi->backlight_power(1);
256 if(fbi->lcd_power)
257 fbi->lcd_power(1);
260 static void imxfb_disable_controller(struct imxfb_info *fbi)
262 pr_debug("Disabling LCD controller\n");
264 if(fbi->backlight_power)
265 fbi->backlight_power(0);
266 if(fbi->lcd_power)
267 fbi->lcd_power(0);
269 LCDC_RMCR = 0;
272 static int imxfb_blank(int blank, struct fb_info *info)
274 struct imxfb_info *fbi = info->par;
276 pr_debug("imxfb_blank: blank=%d\n", blank);
278 switch (blank) {
279 case FB_BLANK_POWERDOWN:
280 case FB_BLANK_VSYNC_SUSPEND:
281 case FB_BLANK_HSYNC_SUSPEND:
282 case FB_BLANK_NORMAL:
283 imxfb_disable_controller(fbi);
284 break;
286 case FB_BLANK_UNBLANK:
287 imxfb_enable_controller(fbi);
288 break;
290 return 0;
293 static struct fb_ops imxfb_ops = {
294 .owner = THIS_MODULE,
295 .fb_check_var = imxfb_check_var,
296 .fb_set_par = imxfb_set_par,
297 .fb_setcolreg = imxfb_setcolreg,
298 .fb_fillrect = cfb_fillrect,
299 .fb_copyarea = cfb_copyarea,
300 .fb_imageblit = cfb_imageblit,
301 .fb_blank = imxfb_blank,
302 .fb_cursor = soft_cursor, /* FIXME: i.MX can do hardware cursor */
306 * imxfb_activate_var():
307 * Configures LCD Controller based on entries in var parameter. Settings are
308 * only written to the controller if changes were made.
310 static int imxfb_activate_var(struct fb_var_screeninfo *var, struct fb_info *info)
312 struct imxfb_info *fbi = info->par;
313 pr_debug("var: xres=%d hslen=%d lm=%d rm=%d\n",
314 var->xres, var->hsync_len,
315 var->left_margin, var->right_margin);
316 pr_debug("var: yres=%d vslen=%d um=%d bm=%d\n",
317 var->yres, var->vsync_len,
318 var->upper_margin, var->lower_margin);
320 #if DEBUG_VAR
321 if (var->xres < 16 || var->xres > 1024)
322 printk(KERN_ERR "%s: invalid xres %d\n",
323 info->fix.id, var->xres);
324 if (var->hsync_len < 1 || var->hsync_len > 64)
325 printk(KERN_ERR "%s: invalid hsync_len %d\n",
326 info->fix.id, var->hsync_len);
327 if (var->left_margin > 255)
328 printk(KERN_ERR "%s: invalid left_margin %d\n",
329 info->fix.id, var->left_margin);
330 if (var->right_margin > 255)
331 printk(KERN_ERR "%s: invalid right_margin %d\n",
332 info->fix.id, var->right_margin);
333 if (var->yres < 1 || var->yres > 511)
334 printk(KERN_ERR "%s: invalid yres %d\n",
335 info->fix.id, var->yres);
336 if (var->vsync_len > 100)
337 printk(KERN_ERR "%s: invalid vsync_len %d\n",
338 info->fix.id, var->vsync_len);
339 if (var->upper_margin > 63)
340 printk(KERN_ERR "%s: invalid upper_margin %d\n",
341 info->fix.id, var->upper_margin);
342 if (var->lower_margin > 255)
343 printk(KERN_ERR "%s: invalid lower_margin %d\n",
344 info->fix.id, var->lower_margin);
345 #endif
347 LCDC_HCR = HCR_H_WIDTH(var->hsync_len) |
348 HCR_H_WAIT_1(var->left_margin) |
349 HCR_H_WAIT_2(var->right_margin);
351 LCDC_VCR = VCR_V_WIDTH(var->vsync_len) |
352 VCR_V_WAIT_1(var->upper_margin) |
353 VCR_V_WAIT_2(var->lower_margin);
355 LCDC_SIZE = SIZE_XMAX(var->xres) | SIZE_YMAX(var->yres);
356 LCDC_PCR = fbi->pcr;
357 LCDC_PWMR = fbi->pwmr;
358 LCDC_LSCR1 = fbi->lscr1;
359 LCDC_DMACR = fbi->dmacr;
361 return 0;
364 static void imxfb_setup_gpio(struct imxfb_info *fbi)
366 int width;
368 LCDC_RMCR &= ~(RMCR_LCDC_EN | RMCR_SELF_REF);
370 if( fbi->pcr & PCR_TFT )
371 width = 16;
372 else
373 width = 1 << ((fbi->pcr >> 28) & 0x3);
375 switch(width) {
376 case 16:
377 imx_gpio_mode(PD30_PF_LD15);
378 imx_gpio_mode(PD29_PF_LD14);
379 imx_gpio_mode(PD28_PF_LD13);
380 imx_gpio_mode(PD27_PF_LD12);
381 imx_gpio_mode(PD26_PF_LD11);
382 imx_gpio_mode(PD25_PF_LD10);
383 imx_gpio_mode(PD24_PF_LD9);
384 imx_gpio_mode(PD23_PF_LD8);
385 case 8:
386 imx_gpio_mode(PD22_PF_LD7);
387 imx_gpio_mode(PD21_PF_LD6);
388 imx_gpio_mode(PD20_PF_LD5);
389 imx_gpio_mode(PD19_PF_LD4);
390 case 4:
391 imx_gpio_mode(PD18_PF_LD3);
392 imx_gpio_mode(PD17_PF_LD2);
393 case 2:
394 imx_gpio_mode(PD16_PF_LD1);
395 case 1:
396 imx_gpio_mode(PD15_PF_LD0);
399 /* initialize GPIOs */
400 imx_gpio_mode(PD6_PF_LSCLK);
401 imx_gpio_mode(PD10_PF_SPL_SPR);
402 imx_gpio_mode(PD11_PF_CONTRAST);
403 imx_gpio_mode(PD14_PF_FLM_VSYNC);
404 imx_gpio_mode(PD13_PF_LP_HSYNC);
405 imx_gpio_mode(PD7_PF_REV);
406 imx_gpio_mode(PD8_PF_CLS);
408 #ifndef CONFIG_MACH_PIMX1
409 /* on PiMX1 used as buffers enable signal
411 imx_gpio_mode(PD9_PF_PS);
412 #endif
414 #ifndef CONFIG_MACH_MX1FS2
415 /* on mx1fs2 this pin is used to (de)activate the display, so we need
416 * it as a normal gpio
418 imx_gpio_mode(PD12_PF_ACD_OE);
419 #endif
423 #ifdef CONFIG_PM
425 * Power management hooks. Note that we won't be called from IRQ context,
426 * unlike the blank functions above, so we may sleep.
428 static int imxfb_suspend(struct device *dev, u32 state, u32 level)
430 struct imxfb_info *fbi = dev_get_drvdata(dev);
431 pr_debug("%s\n",__FUNCTION__);
433 if (level == SUSPEND_DISABLE || level == SUSPEND_POWER_DOWN)
434 imxfb_disable_controller(fbi);
435 return 0;
438 static int imxfb_resume(struct device *dev, u32 level)
440 struct imxfb_info *fbi = dev_get_drvdata(dev);
441 pr_debug("%s\n",__FUNCTION__);
443 if (level == RESUME_ENABLE)
444 imxfb_enable_controller(fbi);
445 return 0;
447 #else
448 #define imxfb_suspend NULL
449 #define imxfb_resume NULL
450 #endif
452 static int __init imxfb_init_fbinfo(struct device *dev)
454 struct imxfb_mach_info *inf = dev->platform_data;
455 struct fb_info *info = dev_get_drvdata(dev);
456 struct imxfb_info *fbi = info->par;
458 pr_debug("%s\n",__FUNCTION__);
460 info->pseudo_palette = kmalloc( sizeof(u32) * 16, GFP_KERNEL);
461 if (!info->pseudo_palette)
462 return -ENOMEM;
464 memset(fbi, 0, sizeof(struct imxfb_info));
465 fbi->dev = dev;
467 strlcpy(info->fix.id, IMX_NAME, sizeof(info->fix.id));
469 info->fix.type = FB_TYPE_PACKED_PIXELS;
470 info->fix.type_aux = 0;
471 info->fix.xpanstep = 0;
472 info->fix.ypanstep = 0;
473 info->fix.ywrapstep = 0;
474 info->fix.accel = FB_ACCEL_NONE;
476 info->var.nonstd = 0;
477 info->var.activate = FB_ACTIVATE_NOW;
478 info->var.height = -1;
479 info->var.width = -1;
480 info->var.accel_flags = 0;
481 info->var.vmode = FB_VMODE_NONINTERLACED;
483 info->fbops = &imxfb_ops;
484 info->flags = FBINFO_FLAG_DEFAULT;
485 info->pseudo_palette = (fbi + 1);
487 fbi->rgb[RGB_16] = &def_rgb_16;
488 fbi->rgb[RGB_8] = &def_rgb_8;
490 fbi->max_xres = inf->xres;
491 info->var.xres = inf->xres;
492 info->var.xres_virtual = inf->xres;
493 fbi->max_yres = inf->yres;
494 info->var.yres = inf->yres;
495 info->var.yres_virtual = inf->yres;
496 fbi->max_bpp = inf->bpp;
497 info->var.bits_per_pixel = inf->bpp;
498 info->var.pixclock = inf->pixclock;
499 info->var.hsync_len = inf->hsync_len;
500 info->var.left_margin = inf->left_margin;
501 info->var.right_margin = inf->right_margin;
502 info->var.vsync_len = inf->vsync_len;
503 info->var.upper_margin = inf->upper_margin;
504 info->var.lower_margin = inf->lower_margin;
505 info->var.sync = inf->sync;
506 info->var.grayscale = inf->cmap_greyscale;
507 fbi->cmap_inverse = inf->cmap_inverse;
508 fbi->pcr = inf->pcr;
509 fbi->lscr1 = inf->lscr1;
510 fbi->dmacr = inf->dmacr;
511 fbi->pwmr = inf->pwmr;
512 fbi->lcd_power = inf->lcd_power;
513 fbi->backlight_power = inf->backlight_power;
514 info->fix.smem_len = fbi->max_xres * fbi->max_yres *
515 fbi->max_bpp / 8;
517 return 0;
521 * Allocates the DRAM memory for the frame buffer. This buffer is
522 * remapped into a non-cached, non-buffered, memory region to
523 * allow pixel writes to occur without flushing the cache.
524 * Once this area is remapped, all virtual memory access to the
525 * video memory should occur at the new region.
527 static int __init imxfb_map_video_memory(struct fb_info *info)
529 struct imxfb_info *fbi = info->par;
531 fbi->map_size = PAGE_ALIGN(info->fix.smem_len);
532 fbi->map_cpu = dma_alloc_writecombine(fbi->dev, fbi->map_size,
533 &fbi->map_dma,GFP_KERNEL);
535 if (fbi->map_cpu) {
536 info->screen_base = fbi->map_cpu;
537 fbi->screen_cpu = fbi->map_cpu;
538 fbi->screen_dma = fbi->map_dma;
539 info->fix.smem_start = fbi->screen_dma;
542 return fbi->map_cpu ? 0 : -ENOMEM;
545 static int __init imxfb_probe(struct device *dev)
547 struct platform_device *pdev = to_platform_device(dev);
548 struct imxfb_info *fbi;
549 struct fb_info *info;
550 struct imxfb_mach_info *inf;
551 struct resource *res;
552 int ret;
554 printk("i.MX Framebuffer driver\n");
556 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
557 if(!res)
558 return -ENODEV;
560 inf = dev->platform_data;
561 if(!inf) {
562 dev_err(dev,"No platform_data available\n");
563 return -ENOMEM;
566 info = framebuffer_alloc(sizeof(struct imxfb_info), dev);
567 if(!info)
568 return -ENOMEM;
570 fbi = info->par;
572 dev_set_drvdata(dev, info);
574 ret = imxfb_init_fbinfo(dev);
575 if( ret < 0 )
576 goto failed_init;
578 res = request_mem_region(res->start, res->end - res->start + 1, "IMXFB");
579 if (!res) {
580 ret = -EBUSY;
581 goto failed_regs;
584 if (!inf->fixed_screen_cpu) {
585 ret = imxfb_map_video_memory(info);
586 if (ret) {
587 dev_err(dev, "Failed to allocate video RAM: %d\n", ret);
588 ret = -ENOMEM;
589 goto failed_map;
591 } else {
592 /* Fixed framebuffer mapping enables location of the screen in eSRAM */
593 fbi->map_cpu = inf->fixed_screen_cpu;
594 fbi->map_dma = inf->fixed_screen_dma;
595 info->screen_base = fbi->map_cpu;
596 fbi->screen_cpu = fbi->map_cpu;
597 fbi->screen_dma = fbi->map_dma;
598 info->fix.smem_start = fbi->screen_dma;
602 * This makes sure that our colour bitfield
603 * descriptors are correctly initialised.
605 imxfb_check_var(&info->var, info);
607 ret = fb_alloc_cmap(&info->cmap, 1<<info->var.bits_per_pixel, 0);
608 if (ret < 0)
609 goto failed_cmap;
611 imxfb_setup_gpio(fbi);
613 imxfb_set_par(info);
614 ret = register_framebuffer(info);
615 if (ret < 0) {
616 dev_err(dev, "failed to register framebuffer\n");
617 goto failed_register;
620 imxfb_enable_controller(fbi);
622 return 0;
624 failed_register:
625 fb_dealloc_cmap(&info->cmap);
626 failed_cmap:
627 if (!inf->fixed_screen_cpu)
628 dma_free_writecombine(dev,fbi->map_size,fbi->map_cpu,
629 fbi->map_dma);
630 failed_map:
631 kfree(info->pseudo_palette);
632 failed_regs:
633 release_mem_region(res->start, res->end - res->start);
634 failed_init:
635 dev_set_drvdata(dev, NULL);
636 framebuffer_release(info);
637 return ret;
640 static int imxfb_remove(struct device *dev)
642 struct platform_device *pdev = to_platform_device(dev);
643 struct fb_info *info = dev_get_drvdata(dev);
644 struct imxfb_info *fbi = info->par;
645 struct resource *res;
647 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
649 imxfb_disable_controller(fbi);
651 unregister_framebuffer(info);
653 fb_dealloc_cmap(&info->cmap);
654 kfree(info->pseudo_palette);
655 framebuffer_release(info);
657 release_mem_region(res->start, res->end - res->start + 1);
658 dev_set_drvdata(dev, NULL);
660 return 0;
663 void imxfb_shutdown(struct device * dev)
665 struct fb_info *info = dev_get_drvdata(dev);
666 struct imxfb_info *fbi = info->par;
667 imxfb_disable_controller(fbi);
670 static struct device_driver imxfb_driver = {
671 .name = "imx-fb",
672 .bus = &platform_bus_type,
673 .probe = imxfb_probe,
674 .suspend = imxfb_suspend,
675 .resume = imxfb_resume,
676 .remove = imxfb_remove,
677 .shutdown = imxfb_shutdown,
680 int __init imxfb_init(void)
682 return driver_register(&imxfb_driver);
685 static void __exit imxfb_cleanup(void)
687 driver_unregister(&imxfb_driver);
690 module_init(imxfb_init);
691 module_exit(imxfb_cleanup);
693 MODULE_DESCRIPTION("Motorola i.MX framebuffer driver");
694 MODULE_AUTHOR("Sascha Hauer, Pengutronix");
695 MODULE_LICENSE("GPL");