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>
 
  28 #include <linux/delay.h>
 
  29 #include <linux/init.h>
 
  30 #include <linux/ioport.h>
 
  31 #include <linux/cpufreq.h>
 
  32 #include <linux/platform_device.h>
 
  33 #include <linux/dma-mapping.h>
 
  35 #include <asm/hardware.h>
 
  37 #include <asm/arch/imxfb.h>
 
  40  * Complain if VAR is out of range.
 
  46 static struct imxfb_rgb def_rgb_16 = {
 
  47         .red    = { .offset = 8,  .length = 4, },
 
  48         .green  = { .offset = 4,  .length = 4, },
 
  49         .blue   = { .offset = 0,  .length = 4, },
 
  50         .transp = { .offset = 0,  .length = 0, },
 
  53 static struct imxfb_rgb def_rgb_8 = {
 
  54         .red    = { .offset = 0,  .length = 8, },
 
  55         .green  = { .offset = 0,  .length = 8, },
 
  56         .blue   = { .offset = 0,  .length = 8, },
 
  57         .transp = { .offset = 0,  .length = 0, },
 
  60 static int imxfb_activate_var(struct fb_var_screeninfo *var, struct fb_info *info);
 
  62 static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
 
  65         chan >>= 16 - bf->length;
 
  66         return chan << bf->offset;
 
  69 #define LCDC_PALETTE(x) __REG2(IMX_LCDC_BASE+0x800, (x)<<2)
 
  71 imxfb_setpalettereg(u_int regno, u_int red, u_int green, u_int blue,
 
  72                        u_int trans, struct fb_info *info)
 
  74         struct imxfb_info *fbi = info->par;
 
  77 #define CNVT_TOHW(val,width) ((((val)<<(width))+0x7FFF-(val))>>16)
 
  78         if (regno < fbi->palette_size) {
 
  79                 val = (CNVT_TOHW(red, 4) << 8) |
 
  80                       (CNVT_TOHW(green,4) << 4) |
 
  83                 LCDC_PALETTE(regno) = val;
 
  90 imxfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
 
  91                    u_int trans, struct fb_info *info)
 
  93         struct imxfb_info *fbi = info->par;
 
  98          * If inverse mode was selected, invert all the colours
 
  99          * rather than the register number.  The register number
 
 100          * is what you poke into the framebuffer to produce the
 
 101          * colour you requested.
 
 103         if (fbi->cmap_inverse) {
 
 105                 green = 0xffff - green;
 
 106                 blue  = 0xffff - blue;
 
 110          * If greyscale is true, then we convert the RGB value
 
 111          * to greyscale no mater what visual we are using.
 
 113         if (info->var.grayscale)
 
 114                 red = green = blue = (19595 * red + 38470 * green +
 
 117         switch (info->fix.visual) {
 
 118         case FB_VISUAL_TRUECOLOR:
 
 120                  * 12 or 16-bit True Colour.  We encode the RGB value
 
 121                  * according to the RGB bitfield information.
 
 124                         u32 *pal = info->pseudo_palette;
 
 126                         val  = chan_to_field(red, &info->var.red);
 
 127                         val |= chan_to_field(green, &info->var.green);
 
 128                         val |= chan_to_field(blue, &info->var.blue);
 
 135         case FB_VISUAL_STATIC_PSEUDOCOLOR:
 
 136         case FB_VISUAL_PSEUDOCOLOR:
 
 137                 ret = imxfb_setpalettereg(regno, red, green, blue, trans, info);
 
 146  *    Round up in the following order: bits_per_pixel, xres,
 
 147  *    yres, xres_virtual, yres_virtual, xoffset, yoffset, grayscale,
 
 148  *    bitfields, horizontal timing, vertical timing.
 
 151 imxfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
 
 153         struct imxfb_info *fbi = info->par;
 
 156         if (var->xres < MIN_XRES)
 
 157                 var->xres = MIN_XRES;
 
 158         if (var->yres < MIN_YRES)
 
 159                 var->yres = MIN_YRES;
 
 160         if (var->xres > fbi->max_xres)
 
 161                 var->xres = fbi->max_xres;
 
 162         if (var->yres > fbi->max_yres)
 
 163                 var->yres = fbi->max_yres;
 
 164         var->xres_virtual = max(var->xres_virtual, var->xres);
 
 165         var->yres_virtual = max(var->yres_virtual, var->yres);
 
 167         pr_debug("var->bits_per_pixel=%d\n", var->bits_per_pixel);
 
 168         switch (var->bits_per_pixel) {
 
 180          * Copy the RGB parameters for this display
 
 181          * from the machine specific parameters.
 
 183         var->red    = fbi->rgb[rgbidx]->red;
 
 184         var->green  = fbi->rgb[rgbidx]->green;
 
 185         var->blue   = fbi->rgb[rgbidx]->blue;
 
 186         var->transp = fbi->rgb[rgbidx]->transp;
 
 188         pr_debug("RGBT length = %d:%d:%d:%d\n",
 
 189                 var->red.length, var->green.length, var->blue.length,
 
 192         pr_debug("RGBT offset = %d:%d:%d:%d\n",
 
 193                 var->red.offset, var->green.offset, var->blue.offset,
 
 201  *      Set the user defined part of the display for the specified console
 
 203 static int imxfb_set_par(struct fb_info *info)
 
 205         struct imxfb_info *fbi = info->par;
 
 206         struct fb_var_screeninfo *var = &info->var;
 
 208         pr_debug("set_par\n");
 
 210         if (var->bits_per_pixel == 16)
 
 211                 info->fix.visual = FB_VISUAL_TRUECOLOR;
 
 212         else if (!fbi->cmap_static)
 
 213                 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
 
 216                  * Some people have weird ideas about wanting static
 
 217                  * pseudocolor maps.  I suspect their user space
 
 218                  * applications are broken.
 
 220                 info->fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
 
 223         info->fix.line_length = var->xres_virtual *
 
 224                                   var->bits_per_pixel / 8;
 
 225         fbi->palette_size = var->bits_per_pixel == 8 ? 256 : 16;
 
 227         imxfb_activate_var(var, info);
 
 232 static void imxfb_enable_controller(struct imxfb_info *fbi)
 
 234         pr_debug("Enabling LCD controller\n");
 
 236         /* initialize LCDC */
 
 237         LCDC_RMCR &= ~RMCR_LCDC_EN;             /* just to be safe... */
 
 239         LCDC_SSA        = fbi->screen_dma;
 
 240         /* physical screen start address            */
 
 241         LCDC_VPW        = VPW_VPW(fbi->max_xres * fbi->max_bpp / 8 / 4);
 
 243         LCDC_POS        = 0x00000000;   /* panning offset 0 (0 pixel offset)        */
 
 245         /* disable hardware cursor */
 
 246         LCDC_CPOS       &= ~(CPOS_CC0 | CPOS_CC1);
 
 248         LCDC_RMCR = RMCR_LCDC_EN;
 
 250         if(fbi->backlight_power)
 
 251                 fbi->backlight_power(1);
 
 256 static void imxfb_disable_controller(struct imxfb_info *fbi)
 
 258         pr_debug("Disabling LCD controller\n");
 
 260         if(fbi->backlight_power)
 
 261                 fbi->backlight_power(0);
 
 268 static int imxfb_blank(int blank, struct fb_info *info)
 
 270         struct imxfb_info *fbi = info->par;
 
 272         pr_debug("imxfb_blank: blank=%d\n", blank);
 
 275         case FB_BLANK_POWERDOWN:
 
 276         case FB_BLANK_VSYNC_SUSPEND:
 
 277         case FB_BLANK_HSYNC_SUSPEND:
 
 278         case FB_BLANK_NORMAL:
 
 279                 imxfb_disable_controller(fbi);
 
 282         case FB_BLANK_UNBLANK:
 
 283                 imxfb_enable_controller(fbi);
 
 289 static struct fb_ops imxfb_ops = {
 
 290         .owner          = THIS_MODULE,
 
 291         .fb_check_var   = imxfb_check_var,
 
 292         .fb_set_par     = imxfb_set_par,
 
 293         .fb_setcolreg   = imxfb_setcolreg,
 
 294         .fb_fillrect    = cfb_fillrect,
 
 295         .fb_copyarea    = cfb_copyarea,
 
 296         .fb_imageblit   = cfb_imageblit,
 
 297         .fb_blank       = imxfb_blank,
 
 301  * imxfb_activate_var():
 
 302  *      Configures LCD Controller based on entries in var parameter.  Settings are
 
 303  *      only written to the controller if changes were made.
 
 305 static int imxfb_activate_var(struct fb_var_screeninfo *var, struct fb_info *info)
 
 307         struct imxfb_info *fbi = info->par;
 
 308         pr_debug("var: xres=%d hslen=%d lm=%d rm=%d\n",
 
 309                 var->xres, var->hsync_len,
 
 310                 var->left_margin, var->right_margin);
 
 311         pr_debug("var: yres=%d vslen=%d um=%d bm=%d\n",
 
 312                 var->yres, var->vsync_len,
 
 313                 var->upper_margin, var->lower_margin);
 
 316         if (var->xres < 16        || var->xres > 1024)
 
 317                 printk(KERN_ERR "%s: invalid xres %d\n",
 
 318                         info->fix.id, var->xres);
 
 319         if (var->hsync_len < 1    || var->hsync_len > 64)
 
 320                 printk(KERN_ERR "%s: invalid hsync_len %d\n",
 
 321                         info->fix.id, var->hsync_len);
 
 322         if (var->left_margin > 255)
 
 323                 printk(KERN_ERR "%s: invalid left_margin %d\n",
 
 324                         info->fix.id, var->left_margin);
 
 325         if (var->right_margin > 255)
 
 326                 printk(KERN_ERR "%s: invalid right_margin %d\n",
 
 327                         info->fix.id, var->right_margin);
 
 328         if (var->yres < 1 || var->yres > 511)
 
 329                 printk(KERN_ERR "%s: invalid yres %d\n",
 
 330                         info->fix.id, var->yres);
 
 331         if (var->vsync_len > 100)
 
 332                 printk(KERN_ERR "%s: invalid vsync_len %d\n",
 
 333                         info->fix.id, var->vsync_len);
 
 334         if (var->upper_margin > 63)
 
 335                 printk(KERN_ERR "%s: invalid upper_margin %d\n",
 
 336                         info->fix.id, var->upper_margin);
 
 337         if (var->lower_margin > 255)
 
 338                 printk(KERN_ERR "%s: invalid lower_margin %d\n",
 
 339                         info->fix.id, var->lower_margin);
 
 342         LCDC_HCR        = HCR_H_WIDTH(var->hsync_len) |
 
 343                           HCR_H_WAIT_1(var->left_margin) |
 
 344                           HCR_H_WAIT_2(var->right_margin);
 
 346         LCDC_VCR        = VCR_V_WIDTH(var->vsync_len) |
 
 347                           VCR_V_WAIT_1(var->upper_margin) |
 
 348                           VCR_V_WAIT_2(var->lower_margin);
 
 350         LCDC_SIZE       = SIZE_XMAX(var->xres) | SIZE_YMAX(var->yres);
 
 352         LCDC_PWMR       = fbi->pwmr;
 
 353         LCDC_LSCR1      = fbi->lscr1;
 
 354         LCDC_DMACR      = fbi->dmacr;
 
 359 static void imxfb_setup_gpio(struct imxfb_info *fbi)
 
 363         LCDC_RMCR       &= ~(RMCR_LCDC_EN | RMCR_SELF_REF);
 
 365         if( fbi->pcr & PCR_TFT )
 
 368                 width = 1 << ((fbi->pcr >> 28) & 0x3);
 
 372                 imx_gpio_mode(PD30_PF_LD15);
 
 373                 imx_gpio_mode(PD29_PF_LD14);
 
 374                 imx_gpio_mode(PD28_PF_LD13);
 
 375                 imx_gpio_mode(PD27_PF_LD12);
 
 376                 imx_gpio_mode(PD26_PF_LD11);
 
 377                 imx_gpio_mode(PD25_PF_LD10);
 
 378                 imx_gpio_mode(PD24_PF_LD9);
 
 379                 imx_gpio_mode(PD23_PF_LD8);
 
 381                 imx_gpio_mode(PD22_PF_LD7);
 
 382                 imx_gpio_mode(PD21_PF_LD6);
 
 383                 imx_gpio_mode(PD20_PF_LD5);
 
 384                 imx_gpio_mode(PD19_PF_LD4);
 
 386                 imx_gpio_mode(PD18_PF_LD3);
 
 387                 imx_gpio_mode(PD17_PF_LD2);
 
 389                 imx_gpio_mode(PD16_PF_LD1);
 
 391                 imx_gpio_mode(PD15_PF_LD0);
 
 394         /* initialize GPIOs */
 
 395         imx_gpio_mode(PD6_PF_LSCLK);
 
 396         imx_gpio_mode(PD11_PF_CONTRAST);
 
 397         imx_gpio_mode(PD14_PF_FLM_VSYNC);
 
 398         imx_gpio_mode(PD13_PF_LP_HSYNC);
 
 399         imx_gpio_mode(PD12_PF_ACD_OE);
 
 401         /* These are only needed for Sharp HR TFT displays */
 
 402         if (fbi->pcr & PCR_SHARP) {
 
 403                 imx_gpio_mode(PD7_PF_REV);
 
 404                 imx_gpio_mode(PD8_PF_CLS);
 
 405                 imx_gpio_mode(PD9_PF_PS);
 
 406                 imx_gpio_mode(PD10_PF_SPL_SPR);
 
 412  * Power management hooks.  Note that we won't be called from IRQ context,
 
 413  * unlike the blank functions above, so we may sleep.
 
 415 static int imxfb_suspend(struct platform_device *dev, pm_message_t state)
 
 417         struct imxfb_info *fbi = platform_get_drvdata(dev);
 
 418         pr_debug("%s\n",__FUNCTION__);
 
 420         imxfb_disable_controller(fbi);
 
 424 static int imxfb_resume(struct platform_device *dev)
 
 426         struct imxfb_info *fbi = platform_get_drvdata(dev);
 
 427         pr_debug("%s\n",__FUNCTION__);
 
 429         imxfb_enable_controller(fbi);
 
 433 #define imxfb_suspend   NULL
 
 434 #define imxfb_resume    NULL
 
 437 static int __init imxfb_init_fbinfo(struct device *dev)
 
 439         struct imxfb_mach_info *inf = dev->platform_data;
 
 440         struct fb_info *info = dev_get_drvdata(dev);
 
 441         struct imxfb_info *fbi = info->par;
 
 443         pr_debug("%s\n",__FUNCTION__);
 
 445         info->pseudo_palette = kmalloc( sizeof(u32) * 16, GFP_KERNEL);
 
 446         if (!info->pseudo_palette)
 
 449         memset(fbi, 0, sizeof(struct imxfb_info));
 
 452         strlcpy(info->fix.id, IMX_NAME, sizeof(info->fix.id));
 
 454         info->fix.type  = FB_TYPE_PACKED_PIXELS;
 
 455         info->fix.type_aux              = 0;
 
 456         info->fix.xpanstep              = 0;
 
 457         info->fix.ypanstep              = 0;
 
 458         info->fix.ywrapstep             = 0;
 
 459         info->fix.accel = FB_ACCEL_NONE;
 
 461         info->var.nonstd                = 0;
 
 462         info->var.activate              = FB_ACTIVATE_NOW;
 
 463         info->var.height                = -1;
 
 464         info->var.width = -1;
 
 465         info->var.accel_flags           = 0;
 
 466         info->var.vmode = FB_VMODE_NONINTERLACED;
 
 468         info->fbops                     = &imxfb_ops;
 
 469         info->flags                     = FBINFO_FLAG_DEFAULT | FBINFO_READS_FAST;
 
 471         fbi->rgb[RGB_16]                = &def_rgb_16;
 
 472         fbi->rgb[RGB_8]                 = &def_rgb_8;
 
 474         fbi->max_xres                   = inf->xres;
 
 475         info->var.xres                  = inf->xres;
 
 476         info->var.xres_virtual          = inf->xres;
 
 477         fbi->max_yres                   = inf->yres;
 
 478         info->var.yres                  = inf->yres;
 
 479         info->var.yres_virtual          = inf->yres;
 
 480         fbi->max_bpp                    = inf->bpp;
 
 481         info->var.bits_per_pixel        = inf->bpp;
 
 482         info->var.nonstd                = inf->nonstd;
 
 483         info->var.pixclock              = inf->pixclock;
 
 484         info->var.hsync_len             = inf->hsync_len;
 
 485         info->var.left_margin           = inf->left_margin;
 
 486         info->var.right_margin          = inf->right_margin;
 
 487         info->var.vsync_len             = inf->vsync_len;
 
 488         info->var.upper_margin          = inf->upper_margin;
 
 489         info->var.lower_margin          = inf->lower_margin;
 
 490         info->var.sync                  = inf->sync;
 
 491         info->var.grayscale             = inf->cmap_greyscale;
 
 492         fbi->cmap_inverse               = inf->cmap_inverse;
 
 493         fbi->cmap_static                = inf->cmap_static;
 
 495         fbi->lscr1                      = inf->lscr1;
 
 496         fbi->dmacr                      = inf->dmacr;
 
 497         fbi->pwmr                       = inf->pwmr;
 
 498         fbi->lcd_power                  = inf->lcd_power;
 
 499         fbi->backlight_power            = inf->backlight_power;
 
 500         info->fix.smem_len              = fbi->max_xres * fbi->max_yres *
 
 507  *      Allocates the DRAM memory for the frame buffer.  This buffer is
 
 508  *      remapped into a non-cached, non-buffered, memory region to
 
 509  *      allow pixel writes to occur without flushing the cache.
 
 510  *      Once this area is remapped, all virtual memory access to the
 
 511  *      video memory should occur at the new region.
 
 513 static int __init imxfb_map_video_memory(struct fb_info *info)
 
 515         struct imxfb_info *fbi = info->par;
 
 517         fbi->map_size = PAGE_ALIGN(info->fix.smem_len);
 
 518         fbi->map_cpu = dma_alloc_writecombine(fbi->dev, fbi->map_size,
 
 519                                         &fbi->map_dma,GFP_KERNEL);
 
 522                 info->screen_base = fbi->map_cpu;
 
 523                 fbi->screen_cpu = fbi->map_cpu;
 
 524                 fbi->screen_dma = fbi->map_dma;
 
 525                 info->fix.smem_start = fbi->screen_dma;
 
 528         return fbi->map_cpu ? 0 : -ENOMEM;
 
 531 static int __init imxfb_probe(struct platform_device *pdev)
 
 533         struct imxfb_info *fbi;
 
 534         struct fb_info *info;
 
 535         struct imxfb_mach_info *inf;
 
 536         struct resource *res;
 
 539         printk("i.MX Framebuffer driver\n");
 
 541         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 
 545         inf = pdev->dev.platform_data;
 
 547                 dev_err(&pdev->dev,"No platform_data available\n");
 
 551         info = framebuffer_alloc(sizeof(struct imxfb_info), &pdev->dev);
 
 557         platform_set_drvdata(pdev, info);
 
 559         ret = imxfb_init_fbinfo(&pdev->dev);
 
 563         res = request_mem_region(res->start, res->end - res->start + 1, "IMXFB");
 
 569         if (!inf->fixed_screen_cpu) {
 
 570                 ret = imxfb_map_video_memory(info);
 
 572                         dev_err(&pdev->dev, "Failed to allocate video RAM: %d\n", ret);
 
 577                 /* Fixed framebuffer mapping enables location of the screen in eSRAM */
 
 578                 fbi->map_cpu = inf->fixed_screen_cpu;
 
 579                 fbi->map_dma = inf->fixed_screen_dma;
 
 580                 info->screen_base = fbi->map_cpu;
 
 581                 fbi->screen_cpu = fbi->map_cpu;
 
 582                 fbi->screen_dma = fbi->map_dma;
 
 583                 info->fix.smem_start = fbi->screen_dma;
 
 587          * This makes sure that our colour bitfield
 
 588          * descriptors are correctly initialised.
 
 590         imxfb_check_var(&info->var, info);
 
 592         ret = fb_alloc_cmap(&info->cmap, 1<<info->var.bits_per_pixel, 0);
 
 596         imxfb_setup_gpio(fbi);
 
 599         ret = register_framebuffer(info);
 
 601                 dev_err(&pdev->dev, "failed to register framebuffer\n");
 
 602                 goto failed_register;
 
 605         imxfb_enable_controller(fbi);
 
 610         fb_dealloc_cmap(&info->cmap);
 
 612         if (!inf->fixed_screen_cpu)
 
 613                 dma_free_writecombine(&pdev->dev,fbi->map_size,fbi->map_cpu,
 
 616         kfree(info->pseudo_palette);
 
 618         release_mem_region(res->start, res->end - res->start);
 
 620         platform_set_drvdata(pdev, NULL);
 
 621         framebuffer_release(info);
 
 625 static int imxfb_remove(struct platform_device *pdev)
 
 627         struct fb_info *info = platform_get_drvdata(pdev);
 
 628         struct imxfb_info *fbi = info->par;
 
 629         struct resource *res;
 
 631         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 
 633         imxfb_disable_controller(fbi);
 
 635         unregister_framebuffer(info);
 
 637         fb_dealloc_cmap(&info->cmap);
 
 638         kfree(info->pseudo_palette);
 
 639         framebuffer_release(info);
 
 641         release_mem_region(res->start, res->end - res->start + 1);
 
 642         platform_set_drvdata(pdev, NULL);
 
 647 void  imxfb_shutdown(struct platform_device * dev)
 
 649         struct fb_info *info = platform_get_drvdata(dev);
 
 650         struct imxfb_info *fbi = info->par;
 
 651         imxfb_disable_controller(fbi);
 
 654 static struct platform_driver imxfb_driver = {
 
 655         .probe          = imxfb_probe,
 
 656         .suspend        = imxfb_suspend,
 
 657         .resume         = imxfb_resume,
 
 658         .remove         = imxfb_remove,
 
 659         .shutdown       = imxfb_shutdown,
 
 665 int __init imxfb_init(void)
 
 667         return platform_driver_register(&imxfb_driver);
 
 670 static void __exit imxfb_cleanup(void)
 
 672         platform_driver_unregister(&imxfb_driver);
 
 675 module_init(imxfb_init);
 
 676 module_exit(imxfb_cleanup);
 
 678 MODULE_DESCRIPTION("Motorola i.MX framebuffer driver");
 
 679 MODULE_AUTHOR("Sascha Hauer, Pengutronix");
 
 680 MODULE_LICENSE("GPL");