5b07053bbd013caf6991ec40d83192e3fc79bd0a
[pandora-kernel.git] / drivers / video / imxfb.c
1 /*
2  *  Freescale i.MX Frame Buffer device driver
3  *
4  *  Copyright (C) 2004 Sascha Hauer, Pengutronix
5  *   Based on acornfb.c Copyright (C) Russell King.
6  *
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
9  * more details.
10  *
11  * Please direct your questions and comments on this driver to the following
12  * email address:
13  *
14  *      linux-arm-kernel@lists.arm.linux.org.uk
15  */
16
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>
23 #include <linux/mm.h>
24 #include <linux/fb.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>
32 #include <linux/io.h>
33 #include <linux/lcd.h>
34 #include <linux/math64.h>
35 #include <linux/of.h>
36 #include <linux/of_device.h>
37
38 #include <linux/regulator/consumer.h>
39
40 #include <video/of_display_timing.h>
41 #include <video/of_videomode.h>
42 #include <video/videomode.h>
43
44 #include <linux/platform_data/video-imxfb.h>
45
46 /*
47  * Complain if VAR is out of range.
48  */
49 #define DEBUG_VAR 1
50
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
55 #endif
56
57 #define DRIVER_NAME "imx-fb"
58
59 #define LCDC_SSA        0x00
60
61 #define LCDC_SIZE       0x04
62 #define SIZE_XMAX(x)    ((((x) >> 4) & 0x3f) << 20)
63
64 #define YMAX_MASK_IMX1  0x1ff
65 #define YMAX_MASK_IMX21 0x3ff
66
67 #define LCDC_VPW        0x08
68 #define VPW_VPW(x)      ((x) & 0x3ff)
69
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)
75
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)
81
82 #define LCDC_LCHCC      0x14
83
84 #define LCDC_PCR        0x18
85
86 #define LCDC_HCR        0x1C
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)
90
91 #define LCDC_VCR        0x20
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)
95
96 #define LCDC_POS        0x24
97 #define POS_POS(x)      ((x) & 1f)
98
99 #define LCDC_LSCR1      0x28
100 /* bit fields in imxfb.h */
101
102 #define LCDC_PWMR       0x2C
103 /* bit fields in imxfb.h */
104
105 #define LCDC_DMACR      0x30
106 /* bit fields in imxfb.h */
107
108 #define LCDC_RMCR       0x34
109
110 #define RMCR_LCDC_EN_MX1        (1<<1)
111
112 #define RMCR_SELF_REF   (1<<0)
113
114 #define LCDC_LCDICR     0x38
115 #define LCDICR_INT_SYN  (1<<2)
116 #define LCDICR_INT_CON  (1)
117
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)
123
124 #define IMXFB_LSCR1_DEFAULT 0x00120300
125
126 /* Used fb-mode. Can be set on kernel command line, therefore file-static. */
127 static const char *fb_mode;
128
129 /*
130  * These are the bitfields for each
131  * display depth that we support.
132  */
133 struct imxfb_rgb {
134         struct fb_bitfield      red;
135         struct fb_bitfield      green;
136         struct fb_bitfield      blue;
137         struct fb_bitfield      transp;
138 };
139
140 enum imxfb_type {
141         IMX1_FB,
142         IMX21_FB,
143 };
144
145 struct imxfb_info {
146         struct platform_device  *pdev;
147         void __iomem            *regs;
148         struct clk              *clk_ipg;
149         struct clk              *clk_ahb;
150         struct clk              *clk_per;
151         enum imxfb_type         devtype;
152         bool                    enabled;
153
154         /*
155          * These are the addresses we mapped
156          * the framebuffer memory region to.
157          */
158         dma_addr_t              map_dma;
159         u_int                   map_size;
160
161         u_int                   palette_size;
162
163         dma_addr_t              dbar1;
164         dma_addr_t              dbar2;
165
166         u_int                   pcr;
167         u_int                   pwmr;
168         u_int                   lscr1;
169         u_int                   dmacr;
170         bool                    cmap_inverse;
171         bool                    cmap_static;
172
173         struct imx_fb_videomode *mode;
174         int                     num_modes;
175 #ifdef PWMR_BACKLIGHT_AVAILABLE
176         struct backlight_device *bl;
177 #endif
178
179         struct regulator        *lcd_pwr;
180 };
181
182 static struct platform_device_id imxfb_devtype[] = {
183         {
184                 .name = "imx1-fb",
185                 .driver_data = IMX1_FB,
186         }, {
187                 .name = "imx21-fb",
188                 .driver_data = IMX21_FB,
189         }, {
190                 /* sentinel */
191         }
192 };
193 MODULE_DEVICE_TABLE(platform, imxfb_devtype);
194
195 static struct of_device_id imxfb_of_dev_id[] = {
196         {
197                 .compatible = "fsl,imx1-fb",
198                 .data = &imxfb_devtype[IMX1_FB],
199         }, {
200                 .compatible = "fsl,imx21-fb",
201                 .data = &imxfb_devtype[IMX21_FB],
202         }, {
203                 /* sentinel */
204         }
205 };
206 MODULE_DEVICE_TABLE(of, imxfb_of_dev_id);
207
208 static inline int is_imx1_fb(struct imxfb_info *fbi)
209 {
210         return fbi->devtype == IMX1_FB;
211 }
212
213 #define IMX_NAME        "IMX"
214
215 /*
216  * Minimum X and Y resolutions
217  */
218 #define MIN_XRES        64
219 #define MIN_YRES        64
220
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.
224  */
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,},
230 };
231
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,},
237 };
238
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,},
244 };
245
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,},
251 };
252
253 static int imxfb_activate_var(struct fb_var_screeninfo *var,
254                 struct fb_info *info);
255
256 static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
257 {
258         chan &= 0xffff;
259         chan >>= 16 - bf->length;
260         return chan << bf->offset;
261 }
262
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)
265 {
266         struct imxfb_info *fbi = info->par;
267         u_int val, ret = 1;
268
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) |
273                       CNVT_TOHW(blue,  4);
274
275                 writel(val, fbi->regs + 0x800 + (regno << 2));
276                 ret = 0;
277         }
278         return ret;
279 }
280
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)
283 {
284         struct imxfb_info *fbi = info->par;
285         unsigned int val;
286         int ret = 1;
287
288         /*
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.
293          */
294         if (fbi->cmap_inverse) {
295                 red   = 0xffff - red;
296                 green = 0xffff - green;
297                 blue  = 0xffff - blue;
298         }
299
300         /*
301          * If greyscale is true, then we convert the RGB value
302          * to greyscale no mater what visual we are using.
303          */
304         if (info->var.grayscale)
305                 red = green = blue = (19595 * red + 38470 * green +
306                                         7471 * blue) >> 16;
307
308         switch (info->fix.visual) {
309         case FB_VISUAL_TRUECOLOR:
310                 /*
311                  * 12 or 16-bit True Colour.  We encode the RGB value
312                  * according to the RGB bitfield information.
313                  */
314                 if (regno < 16) {
315                         u32 *pal = info->pseudo_palette;
316
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);
320
321                         pal[regno] = val;
322                         ret = 0;
323                 }
324                 break;
325
326         case FB_VISUAL_STATIC_PSEUDOCOLOR:
327         case FB_VISUAL_PSEUDOCOLOR:
328                 ret = imxfb_setpalettereg(regno, red, green, blue, trans, info);
329                 break;
330         }
331
332         return ret;
333 }
334
335 static const struct imx_fb_videomode *imxfb_find_mode(struct imxfb_info *fbi)
336 {
337         struct imx_fb_videomode *m;
338         int i;
339
340         if (!fb_mode)
341                 return &fbi->mode[0];
342
343         for (i = 0, m = &fbi->mode[0]; i < fbi->num_modes; i++, m++) {
344                 if (!strcmp(m->mode.name, fb_mode))
345                         return m;
346         }
347         return NULL;
348 }
349
350 /*
351  *  imxfb_check_var():
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.
355  */
356 static int imxfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
357 {
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;
363         u32 pcr = 0;
364
365         if (var->xres < MIN_XRES)
366                 var->xres = MIN_XRES;
367         if (var->yres < MIN_YRES)
368                 var->yres = MIN_YRES;
369
370         imxfb_mode = imxfb_find_mode(fbi);
371         if (!imxfb_mode)
372                 return -EINVAL;
373
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);
387
388         pr_debug("var->bits_per_pixel=%d\n", var->bits_per_pixel);
389
390         lcd_clk = clk_get_rate(fbi->clk_per);
391
392         tmp = var->pixclock * (unsigned long long)lcd_clk;
393
394         do_div(tmp, 1000000);
395
396         if (do_div(tmp, 1000000) > 500000)
397                 tmp++;
398
399         pcr = (unsigned int)tmp;
400
401         if (--pcr > 0x3F) {
402                 pcr = 0x3F;
403                 printk(KERN_WARNING "Must limit pixel clock to %luHz\n",
404                                 lcd_clk / pcr);
405         }
406
407         switch (var->bits_per_pixel) {
408         case 32:
409                 pcr |= PCR_BPIX_18;
410                 rgb = &def_rgb_18;
411                 break;
412         case 16:
413         default:
414                 if (is_imx1_fb(fbi))
415                         pcr |= PCR_BPIX_12;
416                 else
417                         pcr |= PCR_BPIX_16;
418
419                 if (imxfb_mode->pcr & PCR_TFT)
420                         rgb = &def_rgb_16_tft;
421                 else
422                         rgb = &def_rgb_16_stn;
423                 break;
424         case 8:
425                 pcr |= PCR_BPIX_8;
426                 rgb = &def_rgb_8;
427                 break;
428         }
429
430         /* add sync polarities */
431         pcr |= imxfb_mode->pcr & ~(0x3f | (7 << 25));
432
433         fbi->pcr = pcr;
434
435         /*
436          * Copy the RGB parameters for this display
437          * from the machine specific parameters.
438          */
439         var->red    = rgb->red;
440         var->green  = rgb->green;
441         var->blue   = rgb->blue;
442         var->transp = rgb->transp;
443
444         pr_debug("RGBT length = %d:%d:%d:%d\n",
445                 var->red.length, var->green.length, var->blue.length,
446                 var->transp.length);
447
448         pr_debug("RGBT offset = %d:%d:%d:%d\n",
449                 var->red.offset, var->green.offset, var->blue.offset,
450                 var->transp.offset);
451
452         return 0;
453 }
454
455 /*
456  * imxfb_set_par():
457  *      Set the user defined part of the display for the specified console
458  */
459 static int imxfb_set_par(struct fb_info *info)
460 {
461         struct imxfb_info *fbi = info->par;
462         struct fb_var_screeninfo *var = &info->var;
463
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;
468         else {
469                 /*
470                  * Some people have weird ideas about wanting static
471                  * pseudocolor maps.  I suspect their user space
472                  * applications are broken.
473                  */
474                 info->fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
475         }
476
477         info->fix.line_length = var->xres_virtual * var->bits_per_pixel / 8;
478         fbi->palette_size = var->bits_per_pixel == 8 ? 256 : 16;
479
480         imxfb_activate_var(var, info);
481
482         return 0;
483 }
484
485 #ifdef PWMR_BACKLIGHT_AVAILABLE
486 static int imxfb_bl_get_brightness(struct backlight_device *bl)
487 {
488         struct imxfb_info *fbi = bl_get_data(bl);
489
490         return readl(fbi->regs + LCDC_PWMR) & 0xFF;
491 }
492
493 static int imxfb_bl_update_status(struct backlight_device *bl)
494 {
495         struct imxfb_info *fbi = bl_get_data(bl);
496         int brightness = bl->props.brightness;
497
498         if (!fbi->pwmr)
499                 return 0;
500
501         if (bl->props.power != FB_BLANK_UNBLANK)
502                 brightness = 0;
503         if (bl->props.fb_blank != FB_BLANK_UNBLANK)
504                 brightness = 0;
505
506         fbi->pwmr = (fbi->pwmr & ~0xFF) | brightness;
507
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);
512         }
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);
518         }
519
520         return 0;
521 }
522
523 static const struct backlight_ops imxfb_lcdc_bl_ops = {
524         .update_status = imxfb_bl_update_status,
525         .get_brightness = imxfb_bl_get_brightness,
526 };
527
528 static void imxfb_init_backlight(struct imxfb_info *fbi)
529 {
530         struct backlight_properties props;
531         struct backlight_device *bl;
532
533         if (fbi->bl)
534                 return;
535
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);
540
541         bl = backlight_device_register("imxfb-bl", &fbi->pdev->dev, fbi,
542                                        &imxfb_lcdc_bl_ops, &props);
543         if (IS_ERR(bl)) {
544                 dev_err(&fbi->pdev->dev, "error %ld on backlight register\n",
545                                 PTR_ERR(bl));
546                 return;
547         }
548
549         fbi->bl = bl;
550         bl->props.power = FB_BLANK_UNBLANK;
551         bl->props.fb_blank = FB_BLANK_UNBLANK;
552         bl->props.brightness = imxfb_bl_get_brightness(bl);
553 }
554
555 static void imxfb_exit_backlight(struct imxfb_info *fbi)
556 {
557         if (fbi->bl)
558                 backlight_device_unregister(fbi->bl);
559 }
560 #endif
561
562 static void imxfb_enable_controller(struct imxfb_info *fbi)
563 {
564
565         if (fbi->enabled)
566                 return;
567
568         pr_debug("Enabling LCD controller\n");
569
570         writel(fbi->map_dma, fbi->regs + LCDC_SSA);
571
572         /* panning offset 0 (0 pixel offset)        */
573         writel(0x00000000, fbi->regs + LCDC_POS);
574
575         /* disable hardware cursor */
576         writel(readl(fbi->regs + LCDC_CPOS) & ~(CPOS_CC0 | CPOS_CC1),
577                 fbi->regs + LCDC_CPOS);
578
579         /*
580          * RMCR_LCDC_EN_MX1 is present on i.MX1 only, but doesn't hurt
581          * on other SoCs
582          */
583         writel(RMCR_LCDC_EN_MX1, fbi->regs + LCDC_RMCR);
584
585         clk_prepare_enable(fbi->clk_ipg);
586         clk_prepare_enable(fbi->clk_ahb);
587         clk_prepare_enable(fbi->clk_per);
588         fbi->enabled = true;
589 }
590
591 static void imxfb_disable_controller(struct imxfb_info *fbi)
592 {
593         if (!fbi->enabled)
594                 return;
595
596         pr_debug("Disabling LCD controller\n");
597
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;
602
603         writel(0, fbi->regs + LCDC_RMCR);
604 }
605
606 static int imxfb_blank(int blank, struct fb_info *info)
607 {
608         struct imxfb_info *fbi = info->par;
609
610         pr_debug("imxfb_blank: blank=%d\n", blank);
611
612         switch (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);
618                 break;
619
620         case FB_BLANK_UNBLANK:
621                 imxfb_enable_controller(fbi);
622                 break;
623         }
624         return 0;
625 }
626
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,
636 };
637
638 /*
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.
642  */
643 static int imxfb_activate_var(struct fb_var_screeninfo *var, struct fb_info *info)
644 {
645         struct imxfb_info *fbi = info->par;
646         u32 ymax_mask = is_imx1_fb(fbi) ? YMAX_MASK_IMX1 : YMAX_MASK_IMX21;
647
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);
654
655 #if DEBUG_VAR
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);
680 #endif
681
682         /* physical screen start address            */
683         writel(VPW_VPW(var->xres * var->bits_per_pixel / 8 / 4),
684                 fbi->regs + LCDC_VPW);
685
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);
690
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);
695
696         writel(SIZE_XMAX(var->xres) | (var->yres & ymax_mask),
697                         fbi->regs + LCDC_SIZE);
698
699         writel(fbi->pcr, fbi->regs + LCDC_PCR);
700 #ifndef PWMR_BACKLIGHT_AVAILABLE
701         if (fbi->pwmr)
702                 writel(fbi->pwmr, fbi->regs + LCDC_PWMR);
703 #endif
704         writel(fbi->lscr1, fbi->regs + LCDC_LSCR1);
705
706         /* dmacr = 0 is no valid value, as we need DMA control marks. */
707         if (fbi->dmacr)
708                 writel(fbi->dmacr, fbi->regs + LCDC_DMACR);
709
710         return 0;
711 }
712
713 #ifdef CONFIG_PM
714 /*
715  * Power management hooks.  Note that we won't be called from IRQ context,
716  * unlike the blank functions above, so we may sleep.
717  */
718 static int imxfb_suspend(struct platform_device *dev, pm_message_t state)
719 {
720         struct fb_info *info = platform_get_drvdata(dev);
721         struct imxfb_info *fbi = info->par;
722
723         pr_debug("%s\n", __func__);
724
725         imxfb_disable_controller(fbi);
726         return 0;
727 }
728
729 static int imxfb_resume(struct platform_device *dev)
730 {
731         struct fb_info *info = platform_get_drvdata(dev);
732         struct imxfb_info *fbi = info->par;
733
734         pr_debug("%s\n", __func__);
735
736         imxfb_enable_controller(fbi);
737         return 0;
738 }
739 #else
740 #define imxfb_suspend   NULL
741 #define imxfb_resume    NULL
742 #endif
743
744 static int imxfb_init_fbinfo(struct platform_device *pdev)
745 {
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;
750
751         pr_debug("%s\n",__func__);
752
753         info->pseudo_palette = kmalloc(sizeof(u32) * 16, GFP_KERNEL);
754         if (!info->pseudo_palette)
755                 return -ENOMEM;
756
757         memset(fbi, 0, sizeof(struct imxfb_info));
758
759         fbi->devtype = pdev->id_entry->driver_data;
760
761         strlcpy(info->fix.id, IMX_NAME, sizeof(info->fix.id));
762
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;
769
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;
776
777         info->fbops                     = &imxfb_ops;
778         info->flags                     = FBINFO_FLAG_DEFAULT |
779                                           FBINFO_READS_FAST;
780         if (pdata) {
781                 fbi->lscr1                      = pdata->lscr1;
782                 fbi->dmacr                      = pdata->dmacr;
783                 fbi->pwmr                       = pdata->pwmr;
784         } else {
785                 np = pdev->dev.of_node;
786                 info->var.grayscale = of_property_read_bool(np,
787                                                 "cmap-greyscale");
788                 fbi->cmap_inverse = of_property_read_bool(np, "cmap-inverse");
789                 fbi->cmap_static = of_property_read_bool(np, "cmap-static");
790
791                 fbi->lscr1 = IMXFB_LSCR1_DEFAULT;
792                 of_property_read_u32(np, "fsl,lscr1", &fbi->lscr1);
793
794                 of_property_read_u32(np, "fsl,dmacr", &fbi->dmacr);
795         }
796
797         return 0;
798 }
799
800 static int imxfb_of_read_mode(struct device *dev, struct device_node *np,
801                 struct imx_fb_videomode *imxfb_mode)
802 {
803         int ret;
804         struct fb_videomode *of_mode = &imxfb_mode->mode;
805         u32 bpp;
806         u32 pcr;
807
808         ret = of_property_read_string(np, "model", &of_mode->name);
809         if (ret)
810                 of_mode->name = NULL;
811
812         ret = of_get_fb_videomode(np, of_mode, OF_USE_NATIVE_MODE);
813         if (ret) {
814                 dev_err(dev, "Failed to get videomode from DT\n");
815                 return ret;
816         }
817
818         ret = of_property_read_u32(np, "bits-per-pixel", &bpp);
819         ret |= of_property_read_u32(np, "fsl,pcr", &pcr);
820
821         if (ret) {
822                 dev_err(dev, "Failed to read bpp and pcr from DT\n");
823                 return -EINVAL;
824         }
825
826         if (bpp < 1 || bpp > 255) {
827                 dev_err(dev, "Bits per pixel have to be between 1 and 255\n");
828                 return -EINVAL;
829         }
830
831         imxfb_mode->bpp = bpp;
832         imxfb_mode->pcr = pcr;
833
834         return 0;
835 }
836
837 static int imxfb_lcd_check_fb(struct lcd_device *lcddev, struct fb_info *fi)
838 {
839         struct imxfb_info *fbi = dev_get_drvdata(&lcddev->dev);
840
841         if (!fi || fi->par == fbi)
842                 return 1;
843
844         return 0;
845 }
846
847 static int imxfb_lcd_get_power(struct lcd_device *lcddev)
848 {
849         struct imxfb_info *fbi = dev_get_drvdata(&lcddev->dev);
850
851         if (!IS_ERR(fbi->lcd_pwr))
852                 return regulator_is_enabled(fbi->lcd_pwr);
853
854         return 1;
855 }
856
857 static int imxfb_lcd_set_power(struct lcd_device *lcddev, int power)
858 {
859         struct imxfb_info *fbi = dev_get_drvdata(&lcddev->dev);
860
861         if (!IS_ERR(fbi->lcd_pwr)) {
862                 if (power)
863                         return regulator_enable(fbi->lcd_pwr);
864                 else
865                         return regulator_disable(fbi->lcd_pwr);
866         }
867
868         return 0;
869 }
870
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,
875 };
876
877 static int imxfb_probe(struct platform_device *pdev)
878 {
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;
886         int ret, i;
887         int bytes_per_pixel;
888
889         dev_info(&pdev->dev, "i.MX Framebuffer driver\n");
890
891         of_id = of_match_device(imxfb_of_dev_id, &pdev->dev);
892         if (of_id)
893                 pdev->id_entry = of_id->data;
894
895         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
896         if (!res)
897                 return -ENODEV;
898
899         pdata = dev_get_platdata(&pdev->dev);
900
901         info = framebuffer_alloc(sizeof(struct imxfb_info), &pdev->dev);
902         if (!info)
903                 return -ENOMEM;
904
905         fbi = info->par;
906
907         platform_set_drvdata(pdev, info);
908
909         ret = imxfb_init_fbinfo(pdev);
910         if (ret < 0)
911                 goto failed_init;
912
913         if (pdata) {
914                 if (!fb_mode)
915                         fb_mode = pdata->mode[0].mode.name;
916
917                 fbi->mode = pdata->mode;
918                 fbi->num_modes = pdata->num_modes;
919         } else {
920                 struct device_node *display_np;
921                 fb_mode = NULL;
922
923                 display_np = of_parse_phandle(pdev->dev.of_node, "display", 0);
924                 if (!display_np) {
925                         dev_err(&pdev->dev, "No display defined in devicetree\n");
926                         ret = -EINVAL;
927                         goto failed_of_parse;
928                 }
929
930                 /*
931                  * imxfb does not support more modes, we choose only the native
932                  * mode.
933                  */
934                 fbi->num_modes = 1;
935
936                 fbi->mode = devm_kzalloc(&pdev->dev,
937                                 sizeof(struct imx_fb_videomode), GFP_KERNEL);
938                 if (!fbi->mode) {
939                         ret = -ENOMEM;
940                         goto failed_of_parse;
941                 }
942
943                 ret = imxfb_of_read_mode(&pdev->dev, display_np, fbi->mode);
944                 if (ret)
945                         goto failed_of_parse;
946         }
947
948         /* Calculate maximum bytes used per pixel. In most cases this should
949          * be the same as m->bpp/8 */
950         m = &fbi->mode[0];
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);
955
956         res = request_mem_region(res->start, resource_size(res),
957                                 DRIVER_NAME);
958         if (!res) {
959                 ret = -EBUSY;
960                 goto failed_req;
961         }
962
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;
967         }
968
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;
973         }
974
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;
979         }
980
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");
984                 ret = -ENOMEM;
985                 goto failed_ioremap;
986         }
987
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);
991
992         if (!info->screen_base) {
993                 dev_err(&pdev->dev, "Failed to allocate video RAM: %d\n", ret);
994                 ret = -ENOMEM;
995                 goto failed_map;
996         }
997
998         info->fix.smem_start = fbi->map_dma;
999
1000         if (pdata && pdata->init) {
1001                 ret = pdata->init(fbi->pdev);
1002                 if (ret)
1003                         goto failed_platform_init;
1004         }
1005
1006
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);
1010
1011         /*
1012          * This makes sure that our colour bitfield
1013          * descriptors are correctly initialised.
1014          */
1015         imxfb_check_var(&info->var, info);
1016
1017         ret = fb_alloc_cmap(&info->cmap, 1 << info->var.bits_per_pixel, 0);
1018         if (ret < 0)
1019                 goto failed_cmap;
1020
1021         imxfb_set_par(info);
1022         ret = register_framebuffer(info);
1023         if (ret < 0) {
1024                 dev_err(&pdev->dev, "failed to register framebuffer\n");
1025                 goto failed_register;
1026         }
1027
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;
1031                 goto failed_lcd;
1032         }
1033
1034         lcd = devm_lcd_device_register(&pdev->dev, "imxfb-lcd", &pdev->dev, fbi,
1035                                        &imxfb_lcd_ops);
1036         if (IS_ERR(lcd)) {
1037                 ret = PTR_ERR(lcd);
1038                 goto failed_lcd;
1039         }
1040
1041         imxfb_enable_controller(fbi);
1042         fbi->pdev = pdev;
1043 #ifdef PWMR_BACKLIGHT_AVAILABLE
1044         imxfb_init_backlight(fbi);
1045 #endif
1046
1047         return 0;
1048
1049 failed_lcd:
1050         unregister_framebuffer(info);
1051
1052 failed_register:
1053         fb_dealloc_cmap(&info->cmap);
1054 failed_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,
1059                               fbi->map_dma);
1060 failed_map:
1061         iounmap(fbi->regs);
1062 failed_ioremap:
1063 failed_getclock:
1064         release_mem_region(res->start, resource_size(res));
1065 failed_req:
1066 failed_of_parse:
1067         kfree(info->pseudo_palette);
1068 failed_init:
1069         framebuffer_release(info);
1070         return ret;
1071 }
1072
1073 static int imxfb_remove(struct platform_device *pdev)
1074 {
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;
1079
1080         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1081
1082         imxfb_disable_controller(fbi);
1083
1084 #ifdef PWMR_BACKLIGHT_AVAILABLE
1085         imxfb_exit_backlight(fbi);
1086 #endif
1087         unregister_framebuffer(info);
1088
1089         pdata = dev_get_platdata(&pdev->dev);
1090         if (pdata && pdata->exit)
1091                 pdata->exit(fbi->pdev);
1092
1093         fb_dealloc_cmap(&info->cmap);
1094         kfree(info->pseudo_palette);
1095         framebuffer_release(info);
1096
1097         dma_free_writecombine(&pdev->dev, fbi->map_size, info->screen_base,
1098                               fbi->map_dma);
1099
1100         iounmap(fbi->regs);
1101         release_mem_region(res->start, resource_size(res));
1102
1103         return 0;
1104 }
1105
1106 static void imxfb_shutdown(struct platform_device *dev)
1107 {
1108         struct fb_info *info = platform_get_drvdata(dev);
1109         struct imxfb_info *fbi = info->par;
1110         imxfb_disable_controller(fbi);
1111 }
1112
1113 static struct platform_driver imxfb_driver = {
1114         .suspend        = imxfb_suspend,
1115         .resume         = imxfb_resume,
1116         .remove         = imxfb_remove,
1117         .shutdown       = imxfb_shutdown,
1118         .driver         = {
1119                 .name   = DRIVER_NAME,
1120                 .of_match_table = imxfb_of_dev_id,
1121         },
1122         .id_table       = imxfb_devtype,
1123 };
1124
1125 static int imxfb_setup(void)
1126 {
1127 #ifndef MODULE
1128         char *opt, *options = NULL;
1129
1130         if (fb_get_options("imxfb", &options))
1131                 return -ENODEV;
1132
1133         if (!options || !*options)
1134                 return 0;
1135
1136         while ((opt = strsep(&options, ",")) != NULL) {
1137                 if (!*opt)
1138                         continue;
1139                 else
1140                         fb_mode = opt;
1141         }
1142 #endif
1143         return 0;
1144 }
1145
1146 static int __init imxfb_init(void)
1147 {
1148         int ret = imxfb_setup();
1149
1150         if (ret < 0)
1151                 return ret;
1152
1153         return platform_driver_probe(&imxfb_driver, imxfb_probe);
1154 }
1155
1156 static void __exit imxfb_cleanup(void)
1157 {
1158         platform_driver_unregister(&imxfb_driver);
1159 }
1160
1161 module_init(imxfb_init);
1162 module_exit(imxfb_cleanup);
1163
1164 MODULE_DESCRIPTION("Freescale i.MX framebuffer driver");
1165 MODULE_AUTHOR("Sascha Hauer, Pengutronix");
1166 MODULE_LICENSE("GPL");