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/module.h>
22 #include <linux/kernel.h>
23 #include <linux/errno.h>
24 #include <linux/string.h>
25 #include <linux/interrupt.h>
26 #include <linux/slab.h>
29 #include <linux/delay.h>
30 #include <linux/init.h>
31 #include <linux/ioport.h>
32 #include <linux/cpufreq.h>
33 #include <linux/platform_device.h>
34 #include <linux/dma-mapping.h>
36 #include <mach/hardware.h>
38 #include <mach/imxfb.h>
41 * Complain if VAR is out of range.
47 static struct imxfb_rgb def_rgb_16 = {
48 .red = { .offset = 8, .length = 4, },
49 .green = { .offset = 4, .length = 4, },
50 .blue = { .offset = 0, .length = 4, },
51 .transp = { .offset = 0, .length = 0, },
54 static struct imxfb_rgb def_rgb_8 = {
55 .red = { .offset = 0, .length = 8, },
56 .green = { .offset = 0, .length = 8, },
57 .blue = { .offset = 0, .length = 8, },
58 .transp = { .offset = 0, .length = 0, },
61 static int imxfb_activate_var(struct fb_var_screeninfo *var, struct fb_info *info);
63 static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
66 chan >>= 16 - bf->length;
67 return chan << bf->offset;
70 #define LCDC_PALETTE(x) __REG2(IMX_LCDC_BASE+0x800, (x)<<2)
72 imxfb_setpalettereg(u_int regno, u_int red, u_int green, u_int blue,
73 u_int trans, struct fb_info *info)
75 struct imxfb_info *fbi = info->par;
78 #define CNVT_TOHW(val,width) ((((val)<<(width))+0x7FFF-(val))>>16)
79 if (regno < fbi->palette_size) {
80 val = (CNVT_TOHW(red, 4) << 8) |
81 (CNVT_TOHW(green,4) << 4) |
84 LCDC_PALETTE(regno) = val;
91 imxfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
92 u_int trans, struct fb_info *info)
94 struct imxfb_info *fbi = info->par;
99 * If inverse mode was selected, invert all the colours
100 * rather than the register number. The register number
101 * is what you poke into the framebuffer to produce the
102 * colour you requested.
104 if (fbi->cmap_inverse) {
106 green = 0xffff - green;
107 blue = 0xffff - blue;
111 * If greyscale is true, then we convert the RGB value
112 * to greyscale no mater what visual we are using.
114 if (info->var.grayscale)
115 red = green = blue = (19595 * red + 38470 * green +
118 switch (info->fix.visual) {
119 case FB_VISUAL_TRUECOLOR:
121 * 12 or 16-bit True Colour. We encode the RGB value
122 * according to the RGB bitfield information.
125 u32 *pal = info->pseudo_palette;
127 val = chan_to_field(red, &info->var.red);
128 val |= chan_to_field(green, &info->var.green);
129 val |= chan_to_field(blue, &info->var.blue);
136 case FB_VISUAL_STATIC_PSEUDOCOLOR:
137 case FB_VISUAL_PSEUDOCOLOR:
138 ret = imxfb_setpalettereg(regno, red, green, blue, trans, info);
147 * Round up in the following order: bits_per_pixel, xres,
148 * yres, xres_virtual, yres_virtual, xoffset, yoffset, grayscale,
149 * bitfields, horizontal timing, vertical timing.
152 imxfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
154 struct imxfb_info *fbi = info->par;
157 if (var->xres < MIN_XRES)
158 var->xres = MIN_XRES;
159 if (var->yres < MIN_YRES)
160 var->yres = MIN_YRES;
161 if (var->xres > fbi->max_xres)
162 var->xres = fbi->max_xres;
163 if (var->yres > fbi->max_yres)
164 var->yres = fbi->max_yres;
165 var->xres_virtual = max(var->xres_virtual, var->xres);
166 var->yres_virtual = max(var->yres_virtual, var->yres);
168 pr_debug("var->bits_per_pixel=%d\n", var->bits_per_pixel);
169 switch (var->bits_per_pixel) {
181 * Copy the RGB parameters for this display
182 * from the machine specific parameters.
184 var->red = fbi->rgb[rgbidx]->red;
185 var->green = fbi->rgb[rgbidx]->green;
186 var->blue = fbi->rgb[rgbidx]->blue;
187 var->transp = fbi->rgb[rgbidx]->transp;
189 pr_debug("RGBT length = %d:%d:%d:%d\n",
190 var->red.length, var->green.length, var->blue.length,
193 pr_debug("RGBT offset = %d:%d:%d:%d\n",
194 var->red.offset, var->green.offset, var->blue.offset,
202 * Set the user defined part of the display for the specified console
204 static int imxfb_set_par(struct fb_info *info)
206 struct imxfb_info *fbi = info->par;
207 struct fb_var_screeninfo *var = &info->var;
209 pr_debug("set_par\n");
211 if (var->bits_per_pixel == 16)
212 info->fix.visual = FB_VISUAL_TRUECOLOR;
213 else if (!fbi->cmap_static)
214 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
217 * Some people have weird ideas about wanting static
218 * pseudocolor maps. I suspect their user space
219 * applications are broken.
221 info->fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
224 info->fix.line_length = var->xres_virtual *
225 var->bits_per_pixel / 8;
226 fbi->palette_size = var->bits_per_pixel == 8 ? 256 : 16;
228 imxfb_activate_var(var, info);
233 static void imxfb_enable_controller(struct imxfb_info *fbi)
235 pr_debug("Enabling LCD controller\n");
237 /* initialize LCDC */
238 LCDC_RMCR &= ~RMCR_LCDC_EN; /* just to be safe... */
240 LCDC_SSA = fbi->screen_dma;
241 /* physical screen start address */
242 LCDC_VPW = VPW_VPW(fbi->max_xres * fbi->max_bpp / 8 / 4);
244 LCDC_POS = 0x00000000; /* panning offset 0 (0 pixel offset) */
246 /* disable hardware cursor */
247 LCDC_CPOS &= ~(CPOS_CC0 | CPOS_CC1);
249 LCDC_RMCR = RMCR_LCDC_EN;
251 if(fbi->backlight_power)
252 fbi->backlight_power(1);
257 static void imxfb_disable_controller(struct imxfb_info *fbi)
259 pr_debug("Disabling LCD controller\n");
261 if(fbi->backlight_power)
262 fbi->backlight_power(0);
269 static int imxfb_blank(int blank, struct fb_info *info)
271 struct imxfb_info *fbi = info->par;
273 pr_debug("imxfb_blank: blank=%d\n", blank);
276 case FB_BLANK_POWERDOWN:
277 case FB_BLANK_VSYNC_SUSPEND:
278 case FB_BLANK_HSYNC_SUSPEND:
279 case FB_BLANK_NORMAL:
280 imxfb_disable_controller(fbi);
283 case FB_BLANK_UNBLANK:
284 imxfb_enable_controller(fbi);
290 static struct fb_ops imxfb_ops = {
291 .owner = THIS_MODULE,
292 .fb_check_var = imxfb_check_var,
293 .fb_set_par = imxfb_set_par,
294 .fb_setcolreg = imxfb_setcolreg,
295 .fb_fillrect = cfb_fillrect,
296 .fb_copyarea = cfb_copyarea,
297 .fb_imageblit = cfb_imageblit,
298 .fb_blank = imxfb_blank,
302 * imxfb_activate_var():
303 * Configures LCD Controller based on entries in var parameter. Settings are
304 * only written to the controller if changes were made.
306 static int imxfb_activate_var(struct fb_var_screeninfo *var, struct fb_info *info)
308 struct imxfb_info *fbi = info->par;
309 pr_debug("var: xres=%d hslen=%d lm=%d rm=%d\n",
310 var->xres, var->hsync_len,
311 var->left_margin, var->right_margin);
312 pr_debug("var: yres=%d vslen=%d um=%d bm=%d\n",
313 var->yres, var->vsync_len,
314 var->upper_margin, var->lower_margin);
317 if (var->xres < 16 || var->xres > 1024)
318 printk(KERN_ERR "%s: invalid xres %d\n",
319 info->fix.id, var->xres);
320 if (var->hsync_len < 1 || var->hsync_len > 64)
321 printk(KERN_ERR "%s: invalid hsync_len %d\n",
322 info->fix.id, var->hsync_len);
323 if (var->left_margin > 255)
324 printk(KERN_ERR "%s: invalid left_margin %d\n",
325 info->fix.id, var->left_margin);
326 if (var->right_margin > 255)
327 printk(KERN_ERR "%s: invalid right_margin %d\n",
328 info->fix.id, var->right_margin);
329 if (var->yres < 1 || var->yres > 511)
330 printk(KERN_ERR "%s: invalid yres %d\n",
331 info->fix.id, var->yres);
332 if (var->vsync_len > 100)
333 printk(KERN_ERR "%s: invalid vsync_len %d\n",
334 info->fix.id, var->vsync_len);
335 if (var->upper_margin > 63)
336 printk(KERN_ERR "%s: invalid upper_margin %d\n",
337 info->fix.id, var->upper_margin);
338 if (var->lower_margin > 255)
339 printk(KERN_ERR "%s: invalid lower_margin %d\n",
340 info->fix.id, var->lower_margin);
343 LCDC_HCR = HCR_H_WIDTH(var->hsync_len) |
344 HCR_H_WAIT_1(var->left_margin) |
345 HCR_H_WAIT_2(var->right_margin);
347 LCDC_VCR = VCR_V_WIDTH(var->vsync_len) |
348 VCR_V_WAIT_1(var->upper_margin) |
349 VCR_V_WAIT_2(var->lower_margin);
351 LCDC_SIZE = SIZE_XMAX(var->xres) | SIZE_YMAX(var->yres);
353 LCDC_PWMR = fbi->pwmr;
354 LCDC_LSCR1 = fbi->lscr1;
355 LCDC_DMACR = fbi->dmacr;
362 * Power management hooks. Note that we won't be called from IRQ context,
363 * unlike the blank functions above, so we may sleep.
365 static int imxfb_suspend(struct platform_device *dev, pm_message_t state)
367 struct imxfb_info *fbi = platform_get_drvdata(dev);
368 pr_debug("%s\n",__func__);
370 imxfb_disable_controller(fbi);
374 static int imxfb_resume(struct platform_device *dev)
376 struct imxfb_info *fbi = platform_get_drvdata(dev);
377 pr_debug("%s\n",__func__);
379 imxfb_enable_controller(fbi);
383 #define imxfb_suspend NULL
384 #define imxfb_resume NULL
387 static int __init imxfb_init_fbinfo(struct device *dev)
389 struct imxfb_mach_info *inf = dev->platform_data;
390 struct fb_info *info = dev_get_drvdata(dev);
391 struct imxfb_info *fbi = info->par;
393 pr_debug("%s\n",__func__);
395 info->pseudo_palette = kmalloc( sizeof(u32) * 16, GFP_KERNEL);
396 if (!info->pseudo_palette)
399 memset(fbi, 0, sizeof(struct imxfb_info));
402 strlcpy(info->fix.id, IMX_NAME, sizeof(info->fix.id));
404 info->fix.type = FB_TYPE_PACKED_PIXELS;
405 info->fix.type_aux = 0;
406 info->fix.xpanstep = 0;
407 info->fix.ypanstep = 0;
408 info->fix.ywrapstep = 0;
409 info->fix.accel = FB_ACCEL_NONE;
411 info->var.nonstd = 0;
412 info->var.activate = FB_ACTIVATE_NOW;
413 info->var.height = -1;
414 info->var.width = -1;
415 info->var.accel_flags = 0;
416 info->var.vmode = FB_VMODE_NONINTERLACED;
418 info->fbops = &imxfb_ops;
419 info->flags = FBINFO_FLAG_DEFAULT | FBINFO_READS_FAST;
421 fbi->rgb[RGB_16] = &def_rgb_16;
422 fbi->rgb[RGB_8] = &def_rgb_8;
424 fbi->max_xres = inf->xres;
425 info->var.xres = inf->xres;
426 info->var.xres_virtual = inf->xres;
427 fbi->max_yres = inf->yres;
428 info->var.yres = inf->yres;
429 info->var.yres_virtual = inf->yres;
430 fbi->max_bpp = inf->bpp;
431 info->var.bits_per_pixel = inf->bpp;
432 info->var.nonstd = inf->nonstd;
433 info->var.pixclock = inf->pixclock;
434 info->var.hsync_len = inf->hsync_len;
435 info->var.left_margin = inf->left_margin;
436 info->var.right_margin = inf->right_margin;
437 info->var.vsync_len = inf->vsync_len;
438 info->var.upper_margin = inf->upper_margin;
439 info->var.lower_margin = inf->lower_margin;
440 info->var.sync = inf->sync;
441 info->var.grayscale = inf->cmap_greyscale;
442 fbi->cmap_inverse = inf->cmap_inverse;
443 fbi->cmap_static = inf->cmap_static;
445 fbi->lscr1 = inf->lscr1;
446 fbi->dmacr = inf->dmacr;
447 fbi->pwmr = inf->pwmr;
448 fbi->lcd_power = inf->lcd_power;
449 fbi->backlight_power = inf->backlight_power;
450 info->fix.smem_len = fbi->max_xres * fbi->max_yres *
457 * Allocates the DRAM memory for the frame buffer. This buffer is
458 * remapped into a non-cached, non-buffered, memory region to
459 * allow pixel writes to occur without flushing the cache.
460 * Once this area is remapped, all virtual memory access to the
461 * video memory should occur at the new region.
463 static int __init imxfb_map_video_memory(struct fb_info *info)
465 struct imxfb_info *fbi = info->par;
467 fbi->map_size = PAGE_ALIGN(info->fix.smem_len);
468 fbi->map_cpu = dma_alloc_writecombine(fbi->dev, fbi->map_size,
469 &fbi->map_dma,GFP_KERNEL);
472 info->screen_base = fbi->map_cpu;
473 fbi->screen_cpu = fbi->map_cpu;
474 fbi->screen_dma = fbi->map_dma;
475 info->fix.smem_start = fbi->screen_dma;
478 return fbi->map_cpu ? 0 : -ENOMEM;
481 static int __init imxfb_probe(struct platform_device *pdev)
483 struct imxfb_info *fbi;
484 struct fb_info *info;
485 struct imxfb_mach_info *inf;
486 struct resource *res;
489 printk("i.MX Framebuffer driver\n");
491 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
495 inf = pdev->dev.platform_data;
497 dev_err(&pdev->dev,"No platform_data available\n");
501 info = framebuffer_alloc(sizeof(struct imxfb_info), &pdev->dev);
507 platform_set_drvdata(pdev, info);
509 ret = imxfb_init_fbinfo(&pdev->dev);
513 res = request_mem_region(res->start, res->end - res->start + 1, "IMXFB");
519 if (!inf->fixed_screen_cpu) {
520 ret = imxfb_map_video_memory(info);
522 dev_err(&pdev->dev, "Failed to allocate video RAM: %d\n", ret);
527 /* Fixed framebuffer mapping enables location of the screen in eSRAM */
528 fbi->map_cpu = inf->fixed_screen_cpu;
529 fbi->map_dma = inf->fixed_screen_dma;
530 info->screen_base = fbi->map_cpu;
531 fbi->screen_cpu = fbi->map_cpu;
532 fbi->screen_dma = fbi->map_dma;
533 info->fix.smem_start = fbi->screen_dma;
537 * This makes sure that our colour bitfield
538 * descriptors are correctly initialised.
540 imxfb_check_var(&info->var, info);
542 ret = fb_alloc_cmap(&info->cmap, 1<<info->var.bits_per_pixel, 0);
547 ret = register_framebuffer(info);
549 dev_err(&pdev->dev, "failed to register framebuffer\n");
550 goto failed_register;
553 imxfb_enable_controller(fbi);
558 fb_dealloc_cmap(&info->cmap);
560 if (!inf->fixed_screen_cpu)
561 dma_free_writecombine(&pdev->dev,fbi->map_size,fbi->map_cpu,
564 kfree(info->pseudo_palette);
566 release_mem_region(res->start, res->end - res->start);
568 platform_set_drvdata(pdev, NULL);
569 framebuffer_release(info);
573 static int imxfb_remove(struct platform_device *pdev)
575 struct fb_info *info = platform_get_drvdata(pdev);
576 struct imxfb_info *fbi = info->par;
577 struct resource *res;
579 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
581 imxfb_disable_controller(fbi);
583 unregister_framebuffer(info);
585 fb_dealloc_cmap(&info->cmap);
586 kfree(info->pseudo_palette);
587 framebuffer_release(info);
589 release_mem_region(res->start, res->end - res->start + 1);
590 platform_set_drvdata(pdev, NULL);
595 void imxfb_shutdown(struct platform_device * dev)
597 struct fb_info *info = platform_get_drvdata(dev);
598 struct imxfb_info *fbi = info->par;
599 imxfb_disable_controller(fbi);
602 static struct platform_driver imxfb_driver = {
603 .probe = imxfb_probe,
604 .suspend = imxfb_suspend,
605 .resume = imxfb_resume,
606 .remove = imxfb_remove,
607 .shutdown = imxfb_shutdown,
613 int __init imxfb_init(void)
615 return platform_driver_register(&imxfb_driver);
618 static void __exit imxfb_cleanup(void)
620 platform_driver_unregister(&imxfb_driver);
623 module_init(imxfb_init);
624 module_exit(imxfb_cleanup);
626 MODULE_DESCRIPTION("Motorola i.MX framebuffer driver");
627 MODULE_AUTHOR("Sascha Hauer, Pengutronix");
628 MODULE_LICENSE("GPL");