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/lcd.h>
34 #include <linux/math64.h>
36 #include <linux/of_device.h>
38 #include <linux/regulator/consumer.h>
40 #include <video/of_display_timing.h>
41 #include <video/of_videomode.h>
42 #include <video/videomode.h>
44 #include <linux/platform_data/video-imxfb.h>
47 * Complain if VAR is out of range.
51 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || \
52 (defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE) && \
53 defined(CONFIG_FB_IMX_MODULE))
54 #define PWMR_BACKLIGHT_AVAILABLE
57 #define DRIVER_NAME "imx-fb"
61 #define LCDC_SIZE 0x04
62 #define SIZE_XMAX(x) ((((x) >> 4) & 0x3f) << 20)
64 #define YMAX_MASK_IMX1 0x1ff
65 #define YMAX_MASK_IMX21 0x3ff
68 #define VPW_VPW(x) ((x) & 0x3ff)
70 #define LCDC_CPOS 0x0C
71 #define CPOS_CC1 (1<<31)
72 #define CPOS_CC0 (1<<30)
73 #define CPOS_OP (1<<28)
74 #define CPOS_CXP(x) (((x) & 3ff) << 16)
76 #define LCDC_LCWHB 0x10
77 #define LCWHB_BK_EN (1<<31)
78 #define LCWHB_CW(w) (((w) & 0x1f) << 24)
79 #define LCWHB_CH(h) (((h) & 0x1f) << 16)
80 #define LCWHB_BD(x) ((x) & 0xff)
82 #define LCDC_LCHCC 0x14
87 #define HCR_H_WIDTH(x) (((x) & 0x3f) << 26)
88 #define HCR_H_WAIT_1(x) (((x) & 0xff) << 8)
89 #define HCR_H_WAIT_2(x) ((x) & 0xff)
92 #define VCR_V_WIDTH(x) (((x) & 0x3f) << 26)
93 #define VCR_V_WAIT_1(x) (((x) & 0xff) << 8)
94 #define VCR_V_WAIT_2(x) ((x) & 0xff)
97 #define POS_POS(x) ((x) & 1f)
99 #define LCDC_LSCR1 0x28
100 /* bit fields in imxfb.h */
102 #define LCDC_PWMR 0x2C
103 /* bit fields in imxfb.h */
105 #define LCDC_DMACR 0x30
106 /* bit fields in imxfb.h */
108 #define LCDC_RMCR 0x34
110 #define RMCR_LCDC_EN_MX1 (1<<1)
112 #define RMCR_SELF_REF (1<<0)
114 #define LCDC_LCDICR 0x38
115 #define LCDICR_INT_SYN (1<<2)
116 #define LCDICR_INT_CON (1)
118 #define LCDC_LCDISR 0x40
119 #define LCDISR_UDR_ERR (1<<3)
120 #define LCDISR_ERR_RES (1<<2)
121 #define LCDISR_EOF (1<<1)
122 #define LCDISR_BOF (1<<0)
124 #define IMXFB_LSCR1_DEFAULT 0x00120300
126 /* Used fb-mode. Can be set on kernel command line, therefore file-static. */
127 static const char *fb_mode;
130 * These are the bitfields for each
131 * display depth that we support.
134 struct fb_bitfield red;
135 struct fb_bitfield green;
136 struct fb_bitfield blue;
137 struct fb_bitfield transp;
146 struct platform_device *pdev;
151 enum imxfb_type devtype;
155 * These are the addresses we mapped
156 * the framebuffer memory region to.
173 struct imx_fb_videomode *mode;
175 #ifdef PWMR_BACKLIGHT_AVAILABLE
176 struct backlight_device *bl;
179 struct regulator *lcd_pwr;
182 static struct platform_device_id imxfb_devtype[] = {
185 .driver_data = IMX1_FB,
188 .driver_data = IMX21_FB,
193 MODULE_DEVICE_TABLE(platform, imxfb_devtype);
195 static struct of_device_id imxfb_of_dev_id[] = {
197 .compatible = "fsl,imx1-fb",
198 .data = &imxfb_devtype[IMX1_FB],
200 .compatible = "fsl,imx21-fb",
201 .data = &imxfb_devtype[IMX21_FB],
206 MODULE_DEVICE_TABLE(of, imxfb_of_dev_id);
208 static inline int is_imx1_fb(struct imxfb_info *fbi)
210 return fbi->devtype == IMX1_FB;
213 #define IMX_NAME "IMX"
216 * Minimum X and Y resolutions
221 /* Actually this really is 18bit support, the lowest 2 bits of each colour
222 * are unused in hardware. We claim to have 24bit support to make software
223 * like X work, which does not support 18bit.
225 static struct imxfb_rgb def_rgb_18 = {
226 .red = {.offset = 16, .length = 8,},
227 .green = {.offset = 8, .length = 8,},
228 .blue = {.offset = 0, .length = 8,},
229 .transp = {.offset = 0, .length = 0,},
232 static struct imxfb_rgb def_rgb_16_tft = {
233 .red = {.offset = 11, .length = 5,},
234 .green = {.offset = 5, .length = 6,},
235 .blue = {.offset = 0, .length = 5,},
236 .transp = {.offset = 0, .length = 0,},
239 static struct imxfb_rgb def_rgb_16_stn = {
240 .red = {.offset = 8, .length = 4,},
241 .green = {.offset = 4, .length = 4,},
242 .blue = {.offset = 0, .length = 4,},
243 .transp = {.offset = 0, .length = 0,},
246 static struct imxfb_rgb def_rgb_8 = {
247 .red = {.offset = 0, .length = 8,},
248 .green = {.offset = 0, .length = 8,},
249 .blue = {.offset = 0, .length = 8,},
250 .transp = {.offset = 0, .length = 0,},
253 static int imxfb_activate_var(struct fb_var_screeninfo *var,
254 struct fb_info *info);
256 static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
259 chan >>= 16 - bf->length;
260 return chan << bf->offset;
263 static int imxfb_setpalettereg(u_int regno, u_int red, u_int green, u_int blue,
264 u_int trans, struct fb_info *info)
266 struct imxfb_info *fbi = info->par;
269 #define CNVT_TOHW(val,width) ((((val)<<(width))+0x7FFF-(val))>>16)
270 if (regno < fbi->palette_size) {
271 val = (CNVT_TOHW(red, 4) << 8) |
272 (CNVT_TOHW(green,4) << 4) |
275 writel(val, fbi->regs + 0x800 + (regno << 2));
281 static int imxfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
282 u_int trans, struct fb_info *info)
284 struct imxfb_info *fbi = info->par;
289 * If inverse mode was selected, invert all the colours
290 * rather than the register number. The register number
291 * is what you poke into the framebuffer to produce the
292 * colour you requested.
294 if (fbi->cmap_inverse) {
296 green = 0xffff - green;
297 blue = 0xffff - blue;
301 * If greyscale is true, then we convert the RGB value
302 * to greyscale no mater what visual we are using.
304 if (info->var.grayscale)
305 red = green = blue = (19595 * red + 38470 * green +
308 switch (info->fix.visual) {
309 case FB_VISUAL_TRUECOLOR:
311 * 12 or 16-bit True Colour. We encode the RGB value
312 * according to the RGB bitfield information.
315 u32 *pal = info->pseudo_palette;
317 val = chan_to_field(red, &info->var.red);
318 val |= chan_to_field(green, &info->var.green);
319 val |= chan_to_field(blue, &info->var.blue);
326 case FB_VISUAL_STATIC_PSEUDOCOLOR:
327 case FB_VISUAL_PSEUDOCOLOR:
328 ret = imxfb_setpalettereg(regno, red, green, blue, trans, info);
335 static const struct imx_fb_videomode *imxfb_find_mode(struct imxfb_info *fbi)
337 struct imx_fb_videomode *m;
341 return &fbi->mode[0];
343 for (i = 0, m = &fbi->mode[0]; i < fbi->num_modes; i++, m++) {
344 if (!strcmp(m->mode.name, fb_mode))
352 * Round up in the following order: bits_per_pixel, xres,
353 * yres, xres_virtual, yres_virtual, xoffset, yoffset, grayscale,
354 * bitfields, horizontal timing, vertical timing.
356 static int imxfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
358 struct imxfb_info *fbi = info->par;
359 struct imxfb_rgb *rgb;
360 const struct imx_fb_videomode *imxfb_mode;
361 unsigned long lcd_clk;
362 unsigned long long tmp;
365 if (var->xres < MIN_XRES)
366 var->xres = MIN_XRES;
367 if (var->yres < MIN_YRES)
368 var->yres = MIN_YRES;
370 imxfb_mode = imxfb_find_mode(fbi);
374 var->xres = imxfb_mode->mode.xres;
375 var->yres = imxfb_mode->mode.yres;
376 var->bits_per_pixel = imxfb_mode->bpp;
377 var->pixclock = imxfb_mode->mode.pixclock;
378 var->hsync_len = imxfb_mode->mode.hsync_len;
379 var->left_margin = imxfb_mode->mode.left_margin;
380 var->right_margin = imxfb_mode->mode.right_margin;
381 var->vsync_len = imxfb_mode->mode.vsync_len;
382 var->upper_margin = imxfb_mode->mode.upper_margin;
383 var->lower_margin = imxfb_mode->mode.lower_margin;
384 var->sync = imxfb_mode->mode.sync;
385 var->xres_virtual = max(var->xres_virtual, var->xres);
386 var->yres_virtual = max(var->yres_virtual, var->yres);
388 pr_debug("var->bits_per_pixel=%d\n", var->bits_per_pixel);
390 lcd_clk = clk_get_rate(fbi->clk_per);
392 tmp = var->pixclock * (unsigned long long)lcd_clk;
394 do_div(tmp, 1000000);
396 if (do_div(tmp, 1000000) > 500000)
399 pcr = (unsigned int)tmp;
403 printk(KERN_WARNING "Must limit pixel clock to %luHz\n",
407 switch (var->bits_per_pixel) {
419 if (imxfb_mode->pcr & PCR_TFT)
420 rgb = &def_rgb_16_tft;
422 rgb = &def_rgb_16_stn;
430 /* add sync polarities */
431 pcr |= imxfb_mode->pcr & ~(0x3f | (7 << 25));
436 * Copy the RGB parameters for this display
437 * from the machine specific parameters.
440 var->green = rgb->green;
441 var->blue = rgb->blue;
442 var->transp = rgb->transp;
444 pr_debug("RGBT length = %d:%d:%d:%d\n",
445 var->red.length, var->green.length, var->blue.length,
448 pr_debug("RGBT offset = %d:%d:%d:%d\n",
449 var->red.offset, var->green.offset, var->blue.offset,
457 * Set the user defined part of the display for the specified console
459 static int imxfb_set_par(struct fb_info *info)
461 struct imxfb_info *fbi = info->par;
462 struct fb_var_screeninfo *var = &info->var;
464 if (var->bits_per_pixel == 16 || var->bits_per_pixel == 32)
465 info->fix.visual = FB_VISUAL_TRUECOLOR;
466 else if (!fbi->cmap_static)
467 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
470 * Some people have weird ideas about wanting static
471 * pseudocolor maps. I suspect their user space
472 * applications are broken.
474 info->fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
477 info->fix.line_length = var->xres_virtual * var->bits_per_pixel / 8;
478 fbi->palette_size = var->bits_per_pixel == 8 ? 256 : 16;
480 imxfb_activate_var(var, info);
485 #ifdef PWMR_BACKLIGHT_AVAILABLE
486 static int imxfb_bl_get_brightness(struct backlight_device *bl)
488 struct imxfb_info *fbi = bl_get_data(bl);
490 return readl(fbi->regs + LCDC_PWMR) & 0xFF;
493 static int imxfb_bl_update_status(struct backlight_device *bl)
495 struct imxfb_info *fbi = bl_get_data(bl);
496 int brightness = bl->props.brightness;
501 if (bl->props.power != FB_BLANK_UNBLANK)
503 if (bl->props.fb_blank != FB_BLANK_UNBLANK)
506 fbi->pwmr = (fbi->pwmr & ~0xFF) | brightness;
508 if (bl->props.fb_blank != FB_BLANK_UNBLANK) {
509 clk_prepare_enable(fbi->clk_ipg);
510 clk_prepare_enable(fbi->clk_ahb);
511 clk_prepare_enable(fbi->clk_per);
513 writel(fbi->pwmr, fbi->regs + LCDC_PWMR);
514 if (bl->props.fb_blank != FB_BLANK_UNBLANK) {
515 clk_disable_unprepare(fbi->clk_per);
516 clk_disable_unprepare(fbi->clk_ahb);
517 clk_disable_unprepare(fbi->clk_ipg);
523 static const struct backlight_ops imxfb_lcdc_bl_ops = {
524 .update_status = imxfb_bl_update_status,
525 .get_brightness = imxfb_bl_get_brightness,
528 static void imxfb_init_backlight(struct imxfb_info *fbi)
530 struct backlight_properties props;
531 struct backlight_device *bl;
536 memset(&props, 0, sizeof(struct backlight_properties));
537 props.max_brightness = 0xff;
538 props.type = BACKLIGHT_RAW;
539 writel(fbi->pwmr, fbi->regs + LCDC_PWMR);
541 bl = backlight_device_register("imxfb-bl", &fbi->pdev->dev, fbi,
542 &imxfb_lcdc_bl_ops, &props);
544 dev_err(&fbi->pdev->dev, "error %ld on backlight register\n",
550 bl->props.power = FB_BLANK_UNBLANK;
551 bl->props.fb_blank = FB_BLANK_UNBLANK;
552 bl->props.brightness = imxfb_bl_get_brightness(bl);
555 static void imxfb_exit_backlight(struct imxfb_info *fbi)
558 backlight_device_unregister(fbi->bl);
562 static void imxfb_enable_controller(struct imxfb_info *fbi)
568 pr_debug("Enabling LCD controller\n");
570 writel(fbi->map_dma, fbi->regs + LCDC_SSA);
572 /* panning offset 0 (0 pixel offset) */
573 writel(0x00000000, fbi->regs + LCDC_POS);
575 /* disable hardware cursor */
576 writel(readl(fbi->regs + LCDC_CPOS) & ~(CPOS_CC0 | CPOS_CC1),
577 fbi->regs + LCDC_CPOS);
580 * RMCR_LCDC_EN_MX1 is present on i.MX1 only, but doesn't hurt
583 writel(RMCR_LCDC_EN_MX1, fbi->regs + LCDC_RMCR);
585 clk_prepare_enable(fbi->clk_ipg);
586 clk_prepare_enable(fbi->clk_ahb);
587 clk_prepare_enable(fbi->clk_per);
591 static void imxfb_disable_controller(struct imxfb_info *fbi)
596 pr_debug("Disabling LCD controller\n");
598 clk_disable_unprepare(fbi->clk_per);
599 clk_disable_unprepare(fbi->clk_ipg);
600 clk_disable_unprepare(fbi->clk_ahb);
601 fbi->enabled = false;
603 writel(0, fbi->regs + LCDC_RMCR);
606 static int imxfb_blank(int blank, struct fb_info *info)
608 struct imxfb_info *fbi = info->par;
610 pr_debug("imxfb_blank: blank=%d\n", blank);
613 case FB_BLANK_POWERDOWN:
614 case FB_BLANK_VSYNC_SUSPEND:
615 case FB_BLANK_HSYNC_SUSPEND:
616 case FB_BLANK_NORMAL:
617 imxfb_disable_controller(fbi);
620 case FB_BLANK_UNBLANK:
621 imxfb_enable_controller(fbi);
627 static struct fb_ops imxfb_ops = {
628 .owner = THIS_MODULE,
629 .fb_check_var = imxfb_check_var,
630 .fb_set_par = imxfb_set_par,
631 .fb_setcolreg = imxfb_setcolreg,
632 .fb_fillrect = cfb_fillrect,
633 .fb_copyarea = cfb_copyarea,
634 .fb_imageblit = cfb_imageblit,
635 .fb_blank = imxfb_blank,
639 * imxfb_activate_var():
640 * Configures LCD Controller based on entries in var parameter. Settings are
641 * only written to the controller if changes were made.
643 static int imxfb_activate_var(struct fb_var_screeninfo *var, struct fb_info *info)
645 struct imxfb_info *fbi = info->par;
646 u32 ymax_mask = is_imx1_fb(fbi) ? YMAX_MASK_IMX1 : YMAX_MASK_IMX21;
648 pr_debug("var: xres=%d hslen=%d lm=%d rm=%d\n",
649 var->xres, var->hsync_len,
650 var->left_margin, var->right_margin);
651 pr_debug("var: yres=%d vslen=%d um=%d bm=%d\n",
652 var->yres, var->vsync_len,
653 var->upper_margin, var->lower_margin);
656 if (var->xres < 16 || var->xres > 1024)
657 printk(KERN_ERR "%s: invalid xres %d\n",
658 info->fix.id, var->xres);
659 if (var->hsync_len < 1 || var->hsync_len > 64)
660 printk(KERN_ERR "%s: invalid hsync_len %d\n",
661 info->fix.id, var->hsync_len);
662 if (var->left_margin > 255)
663 printk(KERN_ERR "%s: invalid left_margin %d\n",
664 info->fix.id, var->left_margin);
665 if (var->right_margin > 255)
666 printk(KERN_ERR "%s: invalid right_margin %d\n",
667 info->fix.id, var->right_margin);
668 if (var->yres < 1 || var->yres > ymax_mask)
669 printk(KERN_ERR "%s: invalid yres %d\n",
670 info->fix.id, var->yres);
671 if (var->vsync_len > 100)
672 printk(KERN_ERR "%s: invalid vsync_len %d\n",
673 info->fix.id, var->vsync_len);
674 if (var->upper_margin > 63)
675 printk(KERN_ERR "%s: invalid upper_margin %d\n",
676 info->fix.id, var->upper_margin);
677 if (var->lower_margin > 255)
678 printk(KERN_ERR "%s: invalid lower_margin %d\n",
679 info->fix.id, var->lower_margin);
682 /* physical screen start address */
683 writel(VPW_VPW(var->xres * var->bits_per_pixel / 8 / 4),
684 fbi->regs + LCDC_VPW);
686 writel(HCR_H_WIDTH(var->hsync_len - 1) |
687 HCR_H_WAIT_1(var->right_margin - 1) |
688 HCR_H_WAIT_2(var->left_margin - 3),
689 fbi->regs + LCDC_HCR);
691 writel(VCR_V_WIDTH(var->vsync_len) |
692 VCR_V_WAIT_1(var->lower_margin) |
693 VCR_V_WAIT_2(var->upper_margin),
694 fbi->regs + LCDC_VCR);
696 writel(SIZE_XMAX(var->xres) | (var->yres & ymax_mask),
697 fbi->regs + LCDC_SIZE);
699 writel(fbi->pcr, fbi->regs + LCDC_PCR);
700 #ifndef PWMR_BACKLIGHT_AVAILABLE
702 writel(fbi->pwmr, fbi->regs + LCDC_PWMR);
704 writel(fbi->lscr1, fbi->regs + LCDC_LSCR1);
706 /* dmacr = 0 is no valid value, as we need DMA control marks. */
708 writel(fbi->dmacr, fbi->regs + LCDC_DMACR);
715 * Power management hooks. Note that we won't be called from IRQ context,
716 * unlike the blank functions above, so we may sleep.
718 static int imxfb_suspend(struct platform_device *dev, pm_message_t state)
720 struct fb_info *info = platform_get_drvdata(dev);
721 struct imxfb_info *fbi = info->par;
723 pr_debug("%s\n", __func__);
725 imxfb_disable_controller(fbi);
729 static int imxfb_resume(struct platform_device *dev)
731 struct fb_info *info = platform_get_drvdata(dev);
732 struct imxfb_info *fbi = info->par;
734 pr_debug("%s\n", __func__);
736 imxfb_enable_controller(fbi);
740 #define imxfb_suspend NULL
741 #define imxfb_resume NULL
744 static int imxfb_init_fbinfo(struct platform_device *pdev)
746 struct imx_fb_platform_data *pdata = dev_get_platdata(&pdev->dev);
747 struct fb_info *info = dev_get_drvdata(&pdev->dev);
748 struct imxfb_info *fbi = info->par;
749 struct device_node *np;
751 pr_debug("%s\n",__func__);
753 info->pseudo_palette = kmalloc(sizeof(u32) * 16, GFP_KERNEL);
754 if (!info->pseudo_palette)
757 memset(fbi, 0, sizeof(struct imxfb_info));
759 fbi->devtype = pdev->id_entry->driver_data;
761 strlcpy(info->fix.id, IMX_NAME, sizeof(info->fix.id));
763 info->fix.type = FB_TYPE_PACKED_PIXELS;
764 info->fix.type_aux = 0;
765 info->fix.xpanstep = 0;
766 info->fix.ypanstep = 0;
767 info->fix.ywrapstep = 0;
768 info->fix.accel = FB_ACCEL_NONE;
770 info->var.nonstd = 0;
771 info->var.activate = FB_ACTIVATE_NOW;
772 info->var.height = -1;
773 info->var.width = -1;
774 info->var.accel_flags = 0;
775 info->var.vmode = FB_VMODE_NONINTERLACED;
777 info->fbops = &imxfb_ops;
778 info->flags = FBINFO_FLAG_DEFAULT |
781 fbi->lscr1 = pdata->lscr1;
782 fbi->dmacr = pdata->dmacr;
783 fbi->pwmr = pdata->pwmr;
785 np = pdev->dev.of_node;
786 info->var.grayscale = of_property_read_bool(np,
788 fbi->cmap_inverse = of_property_read_bool(np, "cmap-inverse");
789 fbi->cmap_static = of_property_read_bool(np, "cmap-static");
791 fbi->lscr1 = IMXFB_LSCR1_DEFAULT;
792 of_property_read_u32(np, "fsl,lscr1", &fbi->lscr1);
794 of_property_read_u32(np, "fsl,dmacr", &fbi->dmacr);
800 static int imxfb_of_read_mode(struct device *dev, struct device_node *np,
801 struct imx_fb_videomode *imxfb_mode)
804 struct fb_videomode *of_mode = &imxfb_mode->mode;
808 ret = of_property_read_string(np, "model", &of_mode->name);
810 of_mode->name = NULL;
812 ret = of_get_fb_videomode(np, of_mode, OF_USE_NATIVE_MODE);
814 dev_err(dev, "Failed to get videomode from DT\n");
818 ret = of_property_read_u32(np, "bits-per-pixel", &bpp);
819 ret |= of_property_read_u32(np, "fsl,pcr", &pcr);
822 dev_err(dev, "Failed to read bpp and pcr from DT\n");
826 if (bpp < 1 || bpp > 255) {
827 dev_err(dev, "Bits per pixel have to be between 1 and 255\n");
831 imxfb_mode->bpp = bpp;
832 imxfb_mode->pcr = pcr;
837 static int imxfb_lcd_check_fb(struct lcd_device *lcddev, struct fb_info *fi)
839 struct imxfb_info *fbi = dev_get_drvdata(&lcddev->dev);
841 if (!fi || fi->par == fbi)
847 static int imxfb_lcd_get_power(struct lcd_device *lcddev)
849 struct imxfb_info *fbi = dev_get_drvdata(&lcddev->dev);
851 if (!IS_ERR(fbi->lcd_pwr))
852 return regulator_is_enabled(fbi->lcd_pwr);
857 static int imxfb_lcd_set_power(struct lcd_device *lcddev, int power)
859 struct imxfb_info *fbi = dev_get_drvdata(&lcddev->dev);
861 if (!IS_ERR(fbi->lcd_pwr)) {
863 return regulator_enable(fbi->lcd_pwr);
865 return regulator_disable(fbi->lcd_pwr);
871 static struct lcd_ops imxfb_lcd_ops = {
872 .check_fb = imxfb_lcd_check_fb,
873 .get_power = imxfb_lcd_get_power,
874 .set_power = imxfb_lcd_set_power,
877 static int imxfb_probe(struct platform_device *pdev)
879 struct imxfb_info *fbi;
880 struct lcd_device *lcd;
881 struct fb_info *info;
882 struct imx_fb_platform_data *pdata;
883 struct resource *res;
884 struct imx_fb_videomode *m;
885 const struct of_device_id *of_id;
889 dev_info(&pdev->dev, "i.MX Framebuffer driver\n");
891 of_id = of_match_device(imxfb_of_dev_id, &pdev->dev);
893 pdev->id_entry = of_id->data;
895 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
899 pdata = dev_get_platdata(&pdev->dev);
901 info = framebuffer_alloc(sizeof(struct imxfb_info), &pdev->dev);
907 platform_set_drvdata(pdev, info);
909 ret = imxfb_init_fbinfo(pdev);
915 fb_mode = pdata->mode[0].mode.name;
917 fbi->mode = pdata->mode;
918 fbi->num_modes = pdata->num_modes;
920 struct device_node *display_np;
923 display_np = of_parse_phandle(pdev->dev.of_node, "display", 0);
925 dev_err(&pdev->dev, "No display defined in devicetree\n");
927 goto failed_of_parse;
931 * imxfb does not support more modes, we choose only the native
936 fbi->mode = devm_kzalloc(&pdev->dev,
937 sizeof(struct imx_fb_videomode), GFP_KERNEL);
940 goto failed_of_parse;
943 ret = imxfb_of_read_mode(&pdev->dev, display_np, fbi->mode);
945 goto failed_of_parse;
948 /* Calculate maximum bytes used per pixel. In most cases this should
949 * be the same as m->bpp/8 */
951 bytes_per_pixel = (m->bpp + 7) / 8;
952 for (i = 0; i < fbi->num_modes; i++, m++)
953 info->fix.smem_len = max_t(size_t, info->fix.smem_len,
954 m->mode.xres * m->mode.yres * bytes_per_pixel);
956 res = request_mem_region(res->start, resource_size(res),
963 fbi->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
964 if (IS_ERR(fbi->clk_ipg)) {
965 ret = PTR_ERR(fbi->clk_ipg);
966 goto failed_getclock;
969 fbi->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
970 if (IS_ERR(fbi->clk_ahb)) {
971 ret = PTR_ERR(fbi->clk_ahb);
972 goto failed_getclock;
975 fbi->clk_per = devm_clk_get(&pdev->dev, "per");
976 if (IS_ERR(fbi->clk_per)) {
977 ret = PTR_ERR(fbi->clk_per);
978 goto failed_getclock;
981 fbi->regs = ioremap(res->start, resource_size(res));
982 if (fbi->regs == NULL) {
983 dev_err(&pdev->dev, "Cannot map frame buffer registers\n");
988 fbi->map_size = PAGE_ALIGN(info->fix.smem_len);
989 info->screen_base = dma_alloc_writecombine(&pdev->dev, fbi->map_size,
990 &fbi->map_dma, GFP_KERNEL);
992 if (!info->screen_base) {
993 dev_err(&pdev->dev, "Failed to allocate video RAM: %d\n", ret);
998 info->fix.smem_start = fbi->map_dma;
1000 if (pdata && pdata->init) {
1001 ret = pdata->init(fbi->pdev);
1003 goto failed_platform_init;
1007 INIT_LIST_HEAD(&info->modelist);
1008 for (i = 0; i < fbi->num_modes; i++)
1009 fb_add_videomode(&fbi->mode[i].mode, &info->modelist);
1012 * This makes sure that our colour bitfield
1013 * descriptors are correctly initialised.
1015 imxfb_check_var(&info->var, info);
1017 ret = fb_alloc_cmap(&info->cmap, 1 << info->var.bits_per_pixel, 0);
1021 imxfb_set_par(info);
1022 ret = register_framebuffer(info);
1024 dev_err(&pdev->dev, "failed to register framebuffer\n");
1025 goto failed_register;
1028 fbi->lcd_pwr = devm_regulator_get(&pdev->dev, "lcd");
1029 if (IS_ERR(fbi->lcd_pwr) && (PTR_ERR(fbi->lcd_pwr) == -EPROBE_DEFER)) {
1030 ret = -EPROBE_DEFER;
1034 lcd = devm_lcd_device_register(&pdev->dev, "imxfb-lcd", &pdev->dev, fbi,
1041 imxfb_enable_controller(fbi);
1043 #ifdef PWMR_BACKLIGHT_AVAILABLE
1044 imxfb_init_backlight(fbi);
1050 unregister_framebuffer(info);
1053 fb_dealloc_cmap(&info->cmap);
1055 if (pdata && pdata->exit)
1056 pdata->exit(fbi->pdev);
1057 failed_platform_init:
1058 dma_free_writecombine(&pdev->dev, fbi->map_size, info->screen_base,
1064 release_mem_region(res->start, resource_size(res));
1067 kfree(info->pseudo_palette);
1069 framebuffer_release(info);
1073 static int imxfb_remove(struct platform_device *pdev)
1075 struct imx_fb_platform_data *pdata;
1076 struct fb_info *info = platform_get_drvdata(pdev);
1077 struct imxfb_info *fbi = info->par;
1078 struct resource *res;
1080 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1082 imxfb_disable_controller(fbi);
1084 #ifdef PWMR_BACKLIGHT_AVAILABLE
1085 imxfb_exit_backlight(fbi);
1087 unregister_framebuffer(info);
1089 pdata = dev_get_platdata(&pdev->dev);
1090 if (pdata && pdata->exit)
1091 pdata->exit(fbi->pdev);
1093 fb_dealloc_cmap(&info->cmap);
1094 kfree(info->pseudo_palette);
1095 framebuffer_release(info);
1097 dma_free_writecombine(&pdev->dev, fbi->map_size, info->screen_base,
1101 release_mem_region(res->start, resource_size(res));
1106 static void imxfb_shutdown(struct platform_device *dev)
1108 struct fb_info *info = platform_get_drvdata(dev);
1109 struct imxfb_info *fbi = info->par;
1110 imxfb_disable_controller(fbi);
1113 static struct platform_driver imxfb_driver = {
1114 .suspend = imxfb_suspend,
1115 .resume = imxfb_resume,
1116 .remove = imxfb_remove,
1117 .shutdown = imxfb_shutdown,
1119 .name = DRIVER_NAME,
1120 .of_match_table = imxfb_of_dev_id,
1122 .id_table = imxfb_devtype,
1125 static int imxfb_setup(void)
1128 char *opt, *options = NULL;
1130 if (fb_get_options("imxfb", &options))
1133 if (!options || !*options)
1136 while ((opt = strsep(&options, ",")) != NULL) {
1146 static int __init imxfb_init(void)
1148 int ret = imxfb_setup();
1153 return platform_driver_probe(&imxfb_driver, imxfb_probe);
1156 static void __exit imxfb_cleanup(void)
1158 platform_driver_unregister(&imxfb_driver);
1161 module_init(imxfb_init);
1162 module_exit(imxfb_cleanup);
1164 MODULE_DESCRIPTION("Freescale i.MX framebuffer driver");
1165 MODULE_AUTHOR("Sascha Hauer, Pengutronix");
1166 MODULE_LICENSE("GPL");