pxafb: fix various coding style issues for pxafb
[linux-2.6] / drivers / video / pxafb.c
1 /*
2  *  linux/drivers/video/pxafb.c
3  *
4  *  Copyright (C) 1999 Eric A. Thomas.
5  *  Copyright (C) 2004 Jean-Frederic Clere.
6  *  Copyright (C) 2004 Ian Campbell.
7  *  Copyright (C) 2004 Jeff Lackey.
8  *   Based on sa1100fb.c Copyright (C) 1999 Eric A. Thomas
9  *  which in turn is
10  *   Based on acornfb.c Copyright (C) Russell King.
11  *
12  * This file is subject to the terms and conditions of the GNU General Public
13  * License.  See the file COPYING in the main directory of this archive for
14  * more details.
15  *
16  *              Intel PXA250/210 LCD Controller Frame Buffer Driver
17  *
18  * Please direct your questions and comments on this driver to the following
19  * email address:
20  *
21  *      linux-arm-kernel@lists.arm.linux.org.uk
22  *
23  */
24
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/kernel.h>
28 #include <linux/sched.h>
29 #include <linux/errno.h>
30 #include <linux/string.h>
31 #include <linux/interrupt.h>
32 #include <linux/slab.h>
33 #include <linux/fb.h>
34 #include <linux/delay.h>
35 #include <linux/init.h>
36 #include <linux/ioport.h>
37 #include <linux/cpufreq.h>
38 #include <linux/platform_device.h>
39 #include <linux/dma-mapping.h>
40 #include <linux/clk.h>
41 #include <linux/err.h>
42
43 #include <asm/hardware.h>
44 #include <asm/io.h>
45 #include <asm/irq.h>
46 #include <asm/div64.h>
47 #include <asm/arch/pxa-regs.h>
48 #include <asm/arch/pxa2xx-gpio.h>
49 #include <asm/arch/bitfield.h>
50 #include <asm/arch/pxafb.h>
51
52 /*
53  * Complain if VAR is out of range.
54  */
55 #define DEBUG_VAR 1
56
57 #include "pxafb.h"
58
59 /* Bits which should not be set in machine configuration structures */
60 #define LCCR0_INVALID_CONFIG_MASK       (LCCR0_OUM | LCCR0_BM | LCCR0_QDM |\
61                                          LCCR0_DIS | LCCR0_EFM | LCCR0_IUM |\
62                                          LCCR0_SFM | LCCR0_LDM | LCCR0_ENB)
63
64 #define LCCR3_INVALID_CONFIG_MASK       (LCCR3_HSP | LCCR3_VSP |\
65                                          LCCR3_PCD | LCCR3_BPP)
66
67 static void (*pxafb_backlight_power)(int);
68 static void (*pxafb_lcd_power)(int, struct fb_var_screeninfo *);
69
70 static int pxafb_activate_var(struct fb_var_screeninfo *var,
71                                 struct pxafb_info *);
72 static void set_ctrlr_state(struct pxafb_info *fbi, u_int state);
73
74 #ifdef CONFIG_FB_PXA_PARAMETERS
75 #define PXAFB_OPTIONS_SIZE 256
76 static char g_options[PXAFB_OPTIONS_SIZE] __devinitdata = "";
77 #endif
78
79 static inline void pxafb_schedule_work(struct pxafb_info *fbi, u_int state)
80 {
81         unsigned long flags;
82
83         local_irq_save(flags);
84         /*
85          * We need to handle two requests being made at the same time.
86          * There are two important cases:
87          *  1. When we are changing VT (C_REENABLE) while unblanking
88          *     (C_ENABLE) We must perform the unblanking, which will
89          *     do our REENABLE for us.
90          *  2. When we are blanking, but immediately unblank before
91          *     we have blanked.  We do the "REENABLE" thing here as
92          *     well, just to be sure.
93          */
94         if (fbi->task_state == C_ENABLE && state == C_REENABLE)
95                 state = (u_int) -1;
96         if (fbi->task_state == C_DISABLE && state == C_ENABLE)
97                 state = C_REENABLE;
98
99         if (state != (u_int)-1) {
100                 fbi->task_state = state;
101                 schedule_work(&fbi->task);
102         }
103         local_irq_restore(flags);
104 }
105
106 static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
107 {
108         chan &= 0xffff;
109         chan >>= 16 - bf->length;
110         return chan << bf->offset;
111 }
112
113 static int
114 pxafb_setpalettereg(u_int regno, u_int red, u_int green, u_int blue,
115                        u_int trans, struct fb_info *info)
116 {
117         struct pxafb_info *fbi = (struct pxafb_info *)info;
118         u_int val;
119
120         if (regno >= fbi->palette_size)
121                 return 1;
122
123         if (fbi->fb.var.grayscale) {
124                 fbi->palette_cpu[regno] = ((blue >> 8) & 0x00ff);
125                 return 0;
126         }
127
128         switch (fbi->lccr4 & LCCR4_PAL_FOR_MASK) {
129         case LCCR4_PAL_FOR_0:
130                 val  = ((red   >>  0) & 0xf800);
131                 val |= ((green >>  5) & 0x07e0);
132                 val |= ((blue  >> 11) & 0x001f);
133                 fbi->palette_cpu[regno] = val;
134                 break;
135         case LCCR4_PAL_FOR_1:
136                 val  = ((red   << 8) & 0x00f80000);
137                 val |= ((green >> 0) & 0x0000fc00);
138                 val |= ((blue  >> 8) & 0x000000f8);
139                 ((u32 *)(fbi->palette_cpu))[regno] = val;
140                 break;
141         case LCCR4_PAL_FOR_2:
142                 val  = ((red   << 8) & 0x00fc0000);
143                 val |= ((green >> 0) & 0x0000fc00);
144                 val |= ((blue  >> 8) & 0x000000fc);
145                 ((u32 *)(fbi->palette_cpu))[regno] = val;
146                 break;
147         }
148
149         return 0;
150 }
151
152 static int
153 pxafb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
154                    u_int trans, struct fb_info *info)
155 {
156         struct pxafb_info *fbi = (struct pxafb_info *)info;
157         unsigned int val;
158         int ret = 1;
159
160         /*
161          * If inverse mode was selected, invert all the colours
162          * rather than the register number.  The register number
163          * is what you poke into the framebuffer to produce the
164          * colour you requested.
165          */
166         if (fbi->cmap_inverse) {
167                 red   = 0xffff - red;
168                 green = 0xffff - green;
169                 blue  = 0xffff - blue;
170         }
171
172         /*
173          * If greyscale is true, then we convert the RGB value
174          * to greyscale no matter what visual we are using.
175          */
176         if (fbi->fb.var.grayscale)
177                 red = green = blue = (19595 * red + 38470 * green +
178                                         7471 * blue) >> 16;
179
180         switch (fbi->fb.fix.visual) {
181         case FB_VISUAL_TRUECOLOR:
182                 /*
183                  * 16-bit True Colour.  We encode the RGB value
184                  * according to the RGB bitfield information.
185                  */
186                 if (regno < 16) {
187                         u32 *pal = fbi->fb.pseudo_palette;
188
189                         val  = chan_to_field(red, &fbi->fb.var.red);
190                         val |= chan_to_field(green, &fbi->fb.var.green);
191                         val |= chan_to_field(blue, &fbi->fb.var.blue);
192
193                         pal[regno] = val;
194                         ret = 0;
195                 }
196                 break;
197
198         case FB_VISUAL_STATIC_PSEUDOCOLOR:
199         case FB_VISUAL_PSEUDOCOLOR:
200                 ret = pxafb_setpalettereg(regno, red, green, blue, trans, info);
201                 break;
202         }
203
204         return ret;
205 }
206
207 /*
208  *  pxafb_bpp_to_lccr3():
209  *    Convert a bits per pixel value to the correct bit pattern for LCCR3
210  */
211 static int pxafb_bpp_to_lccr3(struct fb_var_screeninfo *var)
212 {
213         int ret = 0;
214         switch (var->bits_per_pixel) {
215         case 1:  ret = LCCR3_1BPP; break;
216         case 2:  ret = LCCR3_2BPP; break;
217         case 4:  ret = LCCR3_4BPP; break;
218         case 8:  ret = LCCR3_8BPP; break;
219         case 16: ret = LCCR3_16BPP; break;
220         }
221         return ret;
222 }
223
224 #ifdef CONFIG_CPU_FREQ
225 /*
226  *  pxafb_display_dma_period()
227  *    Calculate the minimum period (in picoseconds) between two DMA
228  *    requests for the LCD controller.  If we hit this, it means we're
229  *    doing nothing but LCD DMA.
230  */
231 static unsigned int pxafb_display_dma_period(struct fb_var_screeninfo *var)
232 {
233         /*
234          * Period = pixclock * bits_per_byte * bytes_per_transfer
235          *              / memory_bits_per_pixel;
236          */
237         return var->pixclock * 8 * 16 / var->bits_per_pixel;
238 }
239 #endif
240
241 /*
242  * Select the smallest mode that allows the desired resolution to be
243  * displayed. If desired parameters can be rounded up.
244  */
245 static struct pxafb_mode_info *pxafb_getmode(struct pxafb_mach_info *mach,
246                                              struct fb_var_screeninfo *var)
247 {
248         struct pxafb_mode_info *mode = NULL;
249         struct pxafb_mode_info *modelist = mach->modes;
250         unsigned int best_x = 0xffffffff, best_y = 0xffffffff;
251         unsigned int i;
252
253         for (i = 0; i < mach->num_modes; i++) {
254                 if (modelist[i].xres >= var->xres &&
255                     modelist[i].yres >= var->yres &&
256                     modelist[i].xres < best_x &&
257                     modelist[i].yres < best_y &&
258                     modelist[i].bpp >= var->bits_per_pixel) {
259                         best_x = modelist[i].xres;
260                         best_y = modelist[i].yres;
261                         mode = &modelist[i];
262                 }
263         }
264
265         return mode;
266 }
267
268 static void pxafb_setmode(struct fb_var_screeninfo *var,
269                           struct pxafb_mode_info *mode)
270 {
271         var->xres               = mode->xres;
272         var->yres               = mode->yres;
273         var->bits_per_pixel     = mode->bpp;
274         var->pixclock           = mode->pixclock;
275         var->hsync_len          = mode->hsync_len;
276         var->left_margin        = mode->left_margin;
277         var->right_margin       = mode->right_margin;
278         var->vsync_len          = mode->vsync_len;
279         var->upper_margin       = mode->upper_margin;
280         var->lower_margin       = mode->lower_margin;
281         var->sync               = mode->sync;
282         var->grayscale          = mode->cmap_greyscale;
283         var->xres_virtual       = var->xres;
284         var->yres_virtual       = var->yres;
285 }
286
287 /*
288  *  pxafb_check_var():
289  *    Get the video params out of 'var'. If a value doesn't fit, round it up,
290  *    if it's too big, return -EINVAL.
291  *
292  *    Round up in the following order: bits_per_pixel, xres,
293  *    yres, xres_virtual, yres_virtual, xoffset, yoffset, grayscale,
294  *    bitfields, horizontal timing, vertical timing.
295  */
296 static int pxafb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
297 {
298         struct pxafb_info *fbi = (struct pxafb_info *)info;
299         struct pxafb_mach_info *inf = fbi->dev->platform_data;
300
301         if (var->xres < MIN_XRES)
302                 var->xres = MIN_XRES;
303         if (var->yres < MIN_YRES)
304                 var->yres = MIN_YRES;
305
306         if (inf->fixed_modes) {
307                 struct pxafb_mode_info *mode;
308
309                 mode = pxafb_getmode(inf, var);
310                 if (!mode)
311                         return -EINVAL;
312                 pxafb_setmode(var, mode);
313         } else {
314                 if (var->xres > inf->modes->xres)
315                         return -EINVAL;
316                 if (var->yres > inf->modes->yres)
317                         return -EINVAL;
318                 if (var->bits_per_pixel > inf->modes->bpp)
319                         return -EINVAL;
320         }
321
322         var->xres_virtual =
323                 max(var->xres_virtual, var->xres);
324         var->yres_virtual =
325                 max(var->yres_virtual, var->yres);
326
327         /*
328          * Setup the RGB parameters for this display.
329          *
330          * The pixel packing format is described on page 7-11 of the
331          * PXA2XX Developer's Manual.
332          */
333         if (var->bits_per_pixel == 16) {
334                 var->red.offset   = 11; var->red.length   = 5;
335                 var->green.offset = 5;  var->green.length = 6;
336                 var->blue.offset  = 0;  var->blue.length  = 5;
337                 var->transp.offset = var->transp.length = 0;
338         } else {
339                 var->red.offset = var->green.offset = 0;
340                 var->blue.offset = var->transp.offset = 0;
341                 var->red.length   = 8;
342                 var->green.length = 8;
343                 var->blue.length  = 8;
344                 var->transp.length = 0;
345         }
346
347 #ifdef CONFIG_CPU_FREQ
348         pr_debug("pxafb: dma period = %d ps, clock = %d kHz\n",
349                  pxafb_display_dma_period(var),
350                  get_clk_frequency_khz(0));
351 #endif
352
353         return 0;
354 }
355
356 static inline void pxafb_set_truecolor(u_int is_true_color)
357 {
358         pr_debug("pxafb: true_color = %d\n", is_true_color);
359         /* do your machine-specific setup if needed */
360 }
361
362 /*
363  * pxafb_set_par():
364  *      Set the user defined part of the display for the specified console
365  */
366 static int pxafb_set_par(struct fb_info *info)
367 {
368         struct pxafb_info *fbi = (struct pxafb_info *)info;
369         struct fb_var_screeninfo *var = &info->var;
370         unsigned long palette_mem_size;
371
372         pr_debug("pxafb: set_par\n");
373
374         if (var->bits_per_pixel == 16)
375                 fbi->fb.fix.visual = FB_VISUAL_TRUECOLOR;
376         else if (!fbi->cmap_static)
377                 fbi->fb.fix.visual = FB_VISUAL_PSEUDOCOLOR;
378         else {
379                 /*
380                  * Some people have weird ideas about wanting static
381                  * pseudocolor maps.  I suspect their user space
382                  * applications are broken.
383                  */
384                 fbi->fb.fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
385         }
386
387         fbi->fb.fix.line_length = var->xres_virtual *
388                                   var->bits_per_pixel / 8;
389         if (var->bits_per_pixel == 16)
390                 fbi->palette_size = 0;
391         else
392                 fbi->palette_size = var->bits_per_pixel == 1 ?
393                                         4 : 1 << var->bits_per_pixel;
394
395         if ((fbi->lccr4 & LCCR4_PAL_FOR_MASK) == LCCR4_PAL_FOR_0)
396                 palette_mem_size = fbi->palette_size * sizeof(u16);
397         else
398                 palette_mem_size = fbi->palette_size * sizeof(u32);
399
400         pr_debug("pxafb: palette_mem_size = 0x%08lx\n", palette_mem_size);
401
402         fbi->palette_cpu = (u16 *)(fbi->map_cpu + PAGE_SIZE - palette_mem_size);
403         fbi->palette_dma = fbi->map_dma + PAGE_SIZE - palette_mem_size;
404
405         /*
406          * Set (any) board control register to handle new color depth
407          */
408         pxafb_set_truecolor(fbi->fb.fix.visual == FB_VISUAL_TRUECOLOR);
409
410         if (fbi->fb.var.bits_per_pixel == 16)
411                 fb_dealloc_cmap(&fbi->fb.cmap);
412         else
413                 fb_alloc_cmap(&fbi->fb.cmap, 1<<fbi->fb.var.bits_per_pixel, 0);
414
415         pxafb_activate_var(var, fbi);
416
417         return 0;
418 }
419
420 /*
421  * Formal definition of the VESA spec:
422  *  On
423  *      This refers to the state of the display when it is in full operation
424  *  Stand-By
425  *      This defines an optional operating state of minimal power reduction with
426  *      the shortest recovery time
427  *  Suspend
428  *      This refers to a level of power management in which substantial power
429  *      reduction is achieved by the display.  The display can have a longer
430  *      recovery time from this state than from the Stand-by state
431  *  Off
432  *      This indicates that the display is consuming the lowest level of power
433  *      and is non-operational. Recovery from this state may optionally require
434  *      the user to manually power on the monitor
435  *
436  *  Now, the fbdev driver adds an additional state, (blank), where they
437  *  turn off the video (maybe by colormap tricks), but don't mess with the
438  *  video itself: think of it semantically between on and Stand-By.
439  *
440  *  So here's what we should do in our fbdev blank routine:
441  *
442  *      VESA_NO_BLANKING (mode 0)       Video on,  front/back light on
443  *      VESA_VSYNC_SUSPEND (mode 1)     Video on,  front/back light off
444  *      VESA_HSYNC_SUSPEND (mode 2)     Video on,  front/back light off
445  *      VESA_POWERDOWN (mode 3)         Video off, front/back light off
446  *
447  *  This will match the matrox implementation.
448  */
449
450 /*
451  * pxafb_blank():
452  *      Blank the display by setting all palette values to zero.  Note, the
453  *      16 bpp mode does not really use the palette, so this will not
454  *      blank the display in all modes.
455  */
456 static int pxafb_blank(int blank, struct fb_info *info)
457 {
458         struct pxafb_info *fbi = (struct pxafb_info *)info;
459         int i;
460
461         pr_debug("pxafb: blank=%d\n", blank);
462
463         switch (blank) {
464         case FB_BLANK_POWERDOWN:
465         case FB_BLANK_VSYNC_SUSPEND:
466         case FB_BLANK_HSYNC_SUSPEND:
467         case FB_BLANK_NORMAL:
468                 if (fbi->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR ||
469                     fbi->fb.fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
470                         for (i = 0; i < fbi->palette_size; i++)
471                                 pxafb_setpalettereg(i, 0, 0, 0, 0, info);
472
473                 pxafb_schedule_work(fbi, C_DISABLE);
474                 /* TODO if (pxafb_blank_helper) pxafb_blank_helper(blank); */
475                 break;
476
477         case FB_BLANK_UNBLANK:
478                 /* TODO if (pxafb_blank_helper) pxafb_blank_helper(blank); */
479                 if (fbi->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR ||
480                     fbi->fb.fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
481                         fb_set_cmap(&fbi->fb.cmap, info);
482                 pxafb_schedule_work(fbi, C_ENABLE);
483         }
484         return 0;
485 }
486
487 static int pxafb_mmap(struct fb_info *info,
488                       struct vm_area_struct *vma)
489 {
490         struct pxafb_info *fbi = (struct pxafb_info *)info;
491         unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
492
493         if (off < info->fix.smem_len) {
494                 vma->vm_pgoff += 1;
495                 return dma_mmap_writecombine(fbi->dev, vma, fbi->map_cpu,
496                                              fbi->map_dma, fbi->map_size);
497         }
498         return -EINVAL;
499 }
500
501 static struct fb_ops pxafb_ops = {
502         .owner          = THIS_MODULE,
503         .fb_check_var   = pxafb_check_var,
504         .fb_set_par     = pxafb_set_par,
505         .fb_setcolreg   = pxafb_setcolreg,
506         .fb_fillrect    = cfb_fillrect,
507         .fb_copyarea    = cfb_copyarea,
508         .fb_imageblit   = cfb_imageblit,
509         .fb_blank       = pxafb_blank,
510         .fb_mmap        = pxafb_mmap,
511 };
512
513 /*
514  * Calculate the PCD value from the clock rate (in picoseconds).
515  * We take account of the PPCR clock setting.
516  * From PXA Developer's Manual:
517  *
518  *   PixelClock =      LCLK
519  *                -------------
520  *                2 ( PCD + 1 )
521  *
522  *   PCD =      LCLK
523  *         ------------- - 1
524  *         2(PixelClock)
525  *
526  * Where:
527  *   LCLK = LCD/Memory Clock
528  *   PCD = LCCR3[7:0]
529  *
530  * PixelClock here is in Hz while the pixclock argument given is the
531  * period in picoseconds. Hence PixelClock = 1 / ( pixclock * 10^-12 )
532  *
533  * The function get_lclk_frequency_10khz returns LCLK in units of
534  * 10khz. Calling the result of this function lclk gives us the
535  * following
536  *
537  *    PCD = (lclk * 10^4 ) * ( pixclock * 10^-12 )
538  *          -------------------------------------- - 1
539  *                          2
540  *
541  * Factoring the 10^4 and 10^-12 out gives 10^-8 == 1 / 100000000 as used below.
542  */
543 static inline unsigned int get_pcd(struct pxafb_info *fbi,
544                                    unsigned int pixclock)
545 {
546         unsigned long long pcd;
547
548         /* FIXME: Need to take into account Double Pixel Clock mode
549          * (DPC) bit? or perhaps set it based on the various clock
550          * speeds */
551         pcd = (unsigned long long)(clk_get_rate(fbi->clk) / 10000);
552         pcd *= pixclock;
553         do_div(pcd, 100000000 * 2);
554         /* no need for this, since we should subtract 1 anyway. they cancel */
555         /* pcd += 1; */ /* make up for integer math truncations */
556         return (unsigned int)pcd;
557 }
558
559 /*
560  * Some touchscreens need hsync information from the video driver to
561  * function correctly. We export it here.  Note that 'hsync_time' and
562  * the value returned from pxafb_get_hsync_time() is the *reciprocal*
563  * of the hsync period in seconds.
564  */
565 static inline void set_hsync_time(struct pxafb_info *fbi, unsigned int pcd)
566 {
567         unsigned long htime;
568
569         if ((pcd == 0) || (fbi->fb.var.hsync_len == 0)) {
570                 fbi->hsync_time = 0;
571                 return;
572         }
573
574         htime = clk_get_rate(fbi->clk) / (pcd * fbi->fb.var.hsync_len);
575
576         fbi->hsync_time = htime;
577 }
578
579 unsigned long pxafb_get_hsync_time(struct device *dev)
580 {
581         struct pxafb_info *fbi = dev_get_drvdata(dev);
582
583         /* If display is blanked/suspended, hsync isn't active */
584         if (!fbi || (fbi->state != C_ENABLE))
585                 return 0;
586
587         return fbi->hsync_time;
588 }
589 EXPORT_SYMBOL(pxafb_get_hsync_time);
590
591 /*
592  * pxafb_activate_var():
593  *      Configures LCD Controller based on entries in var parameter.
594  *      Settings are only written to the controller if changes were made.
595  */
596 static int pxafb_activate_var(struct fb_var_screeninfo *var,
597                               struct pxafb_info *fbi)
598 {
599         struct pxafb_lcd_reg new_regs;
600         u_long flags;
601         u_int lines_per_panel, pcd = get_pcd(fbi, var->pixclock);
602
603         pr_debug("pxafb: Configuring PXA LCD\n");
604
605         pr_debug("var: xres=%d hslen=%d lm=%d rm=%d\n",
606                  var->xres, var->hsync_len,
607                  var->left_margin, var->right_margin);
608         pr_debug("var: yres=%d vslen=%d um=%d bm=%d\n",
609                  var->yres, var->vsync_len,
610                  var->upper_margin, var->lower_margin);
611         pr_debug("var: pixclock=%d pcd=%d\n", var->pixclock, pcd);
612
613 #if DEBUG_VAR
614         if (var->xres < 16 || var->xres > 1024)
615                 printk(KERN_ERR "%s: invalid xres %d\n",
616                         fbi->fb.fix.id, var->xres);
617         switch (var->bits_per_pixel) {
618         case 1:
619         case 2:
620         case 4:
621         case 8:
622         case 16:
623                 break;
624         default:
625                 printk(KERN_ERR "%s: invalid bit depth %d\n",
626                        fbi->fb.fix.id, var->bits_per_pixel);
627                 break;
628         }
629         if (var->hsync_len < 1 || var->hsync_len > 64)
630                 printk(KERN_ERR "%s: invalid hsync_len %d\n",
631                         fbi->fb.fix.id, var->hsync_len);
632         if (var->left_margin < 1 || var->left_margin > 255)
633                 printk(KERN_ERR "%s: invalid left_margin %d\n",
634                         fbi->fb.fix.id, var->left_margin);
635         if (var->right_margin < 1 || var->right_margin > 255)
636                 printk(KERN_ERR "%s: invalid right_margin %d\n",
637                         fbi->fb.fix.id, var->right_margin);
638         if (var->yres < 1 || var->yres > 1024)
639                 printk(KERN_ERR "%s: invalid yres %d\n",
640                         fbi->fb.fix.id, var->yres);
641         if (var->vsync_len < 1 || var->vsync_len > 64)
642                 printk(KERN_ERR "%s: invalid vsync_len %d\n",
643                         fbi->fb.fix.id, var->vsync_len);
644         if (var->upper_margin < 0 || var->upper_margin > 255)
645                 printk(KERN_ERR "%s: invalid upper_margin %d\n",
646                         fbi->fb.fix.id, var->upper_margin);
647         if (var->lower_margin < 0 || var->lower_margin > 255)
648                 printk(KERN_ERR "%s: invalid lower_margin %d\n",
649                         fbi->fb.fix.id, var->lower_margin);
650 #endif
651
652         new_regs.lccr0 = fbi->lccr0 |
653                 (LCCR0_LDM | LCCR0_SFM | LCCR0_IUM | LCCR0_EFM |
654                  LCCR0_QDM | LCCR0_BM  | LCCR0_OUM);
655
656         new_regs.lccr1 =
657                 LCCR1_DisWdth(var->xres) +
658                 LCCR1_HorSnchWdth(var->hsync_len) +
659                 LCCR1_BegLnDel(var->left_margin) +
660                 LCCR1_EndLnDel(var->right_margin);
661
662         /*
663          * If we have a dual scan LCD, we need to halve
664          * the YRES parameter.
665          */
666         lines_per_panel = var->yres;
667         if ((fbi->lccr0 & LCCR0_SDS) == LCCR0_Dual)
668                 lines_per_panel /= 2;
669
670         new_regs.lccr2 =
671                 LCCR2_DisHght(lines_per_panel) +
672                 LCCR2_VrtSnchWdth(var->vsync_len) +
673                 LCCR2_BegFrmDel(var->upper_margin) +
674                 LCCR2_EndFrmDel(var->lower_margin);
675
676         new_regs.lccr3 = fbi->lccr3 |
677                 pxafb_bpp_to_lccr3(var) |
678                 (var->sync & FB_SYNC_HOR_HIGH_ACT ?
679                  LCCR3_HorSnchH : LCCR3_HorSnchL) |
680                 (var->sync & FB_SYNC_VERT_HIGH_ACT ?
681                  LCCR3_VrtSnchH : LCCR3_VrtSnchL);
682
683         if (pcd)
684                 new_regs.lccr3 |= LCCR3_PixClkDiv(pcd);
685
686         pr_debug("nlccr0 = 0x%08x\n", new_regs.lccr0);
687         pr_debug("nlccr1 = 0x%08x\n", new_regs.lccr1);
688         pr_debug("nlccr2 = 0x%08x\n", new_regs.lccr2);
689         pr_debug("nlccr3 = 0x%08x\n", new_regs.lccr3);
690
691         /* Update shadow copy atomically */
692         local_irq_save(flags);
693
694         /* setup dma descriptors */
695         fbi->dmadesc_fblow_cpu = (struct pxafb_dma_descriptor *)
696                                 ((unsigned int)fbi->palette_cpu - 3*16);
697         fbi->dmadesc_fbhigh_cpu = (struct pxafb_dma_descriptor *)
698                                 ((unsigned int)fbi->palette_cpu - 2*16);
699         fbi->dmadesc_palette_cpu = (struct pxafb_dma_descriptor *)
700                                 ((unsigned int)fbi->palette_cpu - 1*16);
701
702         fbi->dmadesc_fblow_dma = fbi->palette_dma - 3*16;
703         fbi->dmadesc_fbhigh_dma = fbi->palette_dma - 2*16;
704         fbi->dmadesc_palette_dma = fbi->palette_dma - 1*16;
705
706 #define BYTES_PER_PANEL (lines_per_panel * fbi->fb.fix.line_length)
707
708         /* populate descriptors */
709         fbi->dmadesc_fblow_cpu->fdadr = fbi->dmadesc_fblow_dma;
710         fbi->dmadesc_fblow_cpu->fsadr = fbi->screen_dma + BYTES_PER_PANEL;
711         fbi->dmadesc_fblow_cpu->fidr  = 0;
712         fbi->dmadesc_fblow_cpu->ldcmd = BYTES_PER_PANEL;
713
714         fbi->fdadr1 = fbi->dmadesc_fblow_dma; /* only used in dual-panel mode */
715
716         fbi->dmadesc_fbhigh_cpu->fsadr = fbi->screen_dma;
717         fbi->dmadesc_fbhigh_cpu->fidr = 0;
718         fbi->dmadesc_fbhigh_cpu->ldcmd = BYTES_PER_PANEL;
719
720         fbi->dmadesc_palette_cpu->fsadr = fbi->palette_dma;
721         fbi->dmadesc_palette_cpu->fidr  = 0;
722         if ((fbi->lccr4 & LCCR4_PAL_FOR_MASK) == LCCR4_PAL_FOR_0)
723                 fbi->dmadesc_palette_cpu->ldcmd = fbi->palette_size *
724                                                         sizeof(u16);
725         else
726                 fbi->dmadesc_palette_cpu->ldcmd = fbi->palette_size *
727                                                         sizeof(u32);
728         fbi->dmadesc_palette_cpu->ldcmd |= LDCMD_PAL;
729
730         if (var->bits_per_pixel == 16) {
731                 /* palette shouldn't be loaded in true-color mode */
732                 fbi->dmadesc_fbhigh_cpu->fdadr = fbi->dmadesc_fbhigh_dma;
733                 fbi->fdadr0 = fbi->dmadesc_fbhigh_dma; /* no pal just fbhigh */
734                 /* init it to something, even though we won't be using it */
735                 fbi->dmadesc_palette_cpu->fdadr = fbi->dmadesc_palette_dma;
736         } else {
737                 /* flips back and forth between pal and fbhigh */
738                 fbi->dmadesc_palette_cpu->fdadr = fbi->dmadesc_fbhigh_dma;
739                 fbi->dmadesc_fbhigh_cpu->fdadr = fbi->dmadesc_palette_dma;
740                 fbi->fdadr0 = fbi->dmadesc_palette_dma;
741         }
742
743         fbi->reg_lccr0 = new_regs.lccr0;
744         fbi->reg_lccr1 = new_regs.lccr1;
745         fbi->reg_lccr2 = new_regs.lccr2;
746         fbi->reg_lccr3 = new_regs.lccr3;
747         fbi->reg_lccr4 = LCCR4 & (~LCCR4_PAL_FOR_MASK);
748         fbi->reg_lccr4 |= (fbi->lccr4 & LCCR4_PAL_FOR_MASK);
749         set_hsync_time(fbi, pcd);
750         local_irq_restore(flags);
751
752         /*
753          * Only update the registers if the controller is enabled
754          * and something has changed.
755          */
756         if ((LCCR0  != fbi->reg_lccr0) || (LCCR1  != fbi->reg_lccr1) ||
757             (LCCR2  != fbi->reg_lccr2) || (LCCR3  != fbi->reg_lccr3) ||
758             (FDADR0 != fbi->fdadr0)    || (FDADR1 != fbi->fdadr1))
759                 pxafb_schedule_work(fbi, C_REENABLE);
760
761         return 0;
762 }
763
764 /*
765  * NOTE!  The following functions are purely helpers for set_ctrlr_state.
766  * Do not call them directly; set_ctrlr_state does the correct serialisation
767  * to ensure that things happen in the right way 100% of time time.
768  *      -- rmk
769  */
770 static inline void __pxafb_backlight_power(struct pxafb_info *fbi, int on)
771 {
772         pr_debug("pxafb: backlight o%s\n", on ? "n" : "ff");
773
774         if (pxafb_backlight_power)
775                 pxafb_backlight_power(on);
776 }
777
778 static inline void __pxafb_lcd_power(struct pxafb_info *fbi, int on)
779 {
780         pr_debug("pxafb: LCD power o%s\n", on ? "n" : "ff");
781
782         if (pxafb_lcd_power)
783                 pxafb_lcd_power(on, &fbi->fb.var);
784 }
785
786 static void pxafb_setup_gpio(struct pxafb_info *fbi)
787 {
788         int gpio, ldd_bits;
789         unsigned int lccr0 = fbi->lccr0;
790
791         /*
792          * setup is based on type of panel supported
793          */
794
795         /* 4 bit interface */
796         if ((lccr0 & LCCR0_CMS) == LCCR0_Mono &&
797             (lccr0 & LCCR0_SDS) == LCCR0_Sngl &&
798             (lccr0 & LCCR0_DPD) == LCCR0_4PixMono)
799                 ldd_bits = 4;
800
801         /* 8 bit interface */
802         else if (((lccr0 & LCCR0_CMS) == LCCR0_Mono &&
803                   ((lccr0 & LCCR0_SDS) == LCCR0_Dual ||
804                    (lccr0 & LCCR0_DPD) == LCCR0_8PixMono)) ||
805                  ((lccr0 & LCCR0_CMS) == LCCR0_Color &&
806                   (lccr0 & LCCR0_PAS) == LCCR0_Pas &&
807                   (lccr0 & LCCR0_SDS) == LCCR0_Sngl))
808                 ldd_bits = 8;
809
810         /* 16 bit interface */
811         else if ((lccr0 & LCCR0_CMS) == LCCR0_Color &&
812                  ((lccr0 & LCCR0_SDS) == LCCR0_Dual ||
813                   (lccr0 & LCCR0_PAS) == LCCR0_Act))
814                 ldd_bits = 16;
815
816         else {
817                 printk(KERN_ERR "pxafb_setup_gpio: unable to determine "
818                                "bits per pixel\n");
819                 return;
820         }
821
822         for (gpio = 58; ldd_bits; gpio++, ldd_bits--)
823                 pxa_gpio_mode(gpio | GPIO_ALT_FN_2_OUT);
824         pxa_gpio_mode(GPIO74_LCD_FCLK_MD);
825         pxa_gpio_mode(GPIO75_LCD_LCLK_MD);
826         pxa_gpio_mode(GPIO76_LCD_PCLK_MD);
827         pxa_gpio_mode(GPIO77_LCD_ACBIAS_MD);
828 }
829
830 static void pxafb_enable_controller(struct pxafb_info *fbi)
831 {
832         pr_debug("pxafb: Enabling LCD controller\n");
833         pr_debug("fdadr0 0x%08x\n", (unsigned int) fbi->fdadr0);
834         pr_debug("fdadr1 0x%08x\n", (unsigned int) fbi->fdadr1);
835         pr_debug("reg_lccr0 0x%08x\n", (unsigned int) fbi->reg_lccr0);
836         pr_debug("reg_lccr1 0x%08x\n", (unsigned int) fbi->reg_lccr1);
837         pr_debug("reg_lccr2 0x%08x\n", (unsigned int) fbi->reg_lccr2);
838         pr_debug("reg_lccr3 0x%08x\n", (unsigned int) fbi->reg_lccr3);
839
840         /* enable LCD controller clock */
841         clk_enable(fbi->clk);
842
843         /* Sequence from 11.7.10 */
844         LCCR3 = fbi->reg_lccr3;
845         LCCR2 = fbi->reg_lccr2;
846         LCCR1 = fbi->reg_lccr1;
847         LCCR0 = fbi->reg_lccr0 & ~LCCR0_ENB;
848
849         FDADR0 = fbi->fdadr0;
850         FDADR1 = fbi->fdadr1;
851         LCCR0 |= LCCR0_ENB;
852
853         pr_debug("FDADR0 0x%08x\n", (unsigned int) FDADR0);
854         pr_debug("FDADR1 0x%08x\n", (unsigned int) FDADR1);
855         pr_debug("LCCR0 0x%08x\n", (unsigned int) LCCR0);
856         pr_debug("LCCR1 0x%08x\n", (unsigned int) LCCR1);
857         pr_debug("LCCR2 0x%08x\n", (unsigned int) LCCR2);
858         pr_debug("LCCR3 0x%08x\n", (unsigned int) LCCR3);
859         pr_debug("LCCR4 0x%08x\n", (unsigned int) LCCR4);
860 }
861
862 static void pxafb_disable_controller(struct pxafb_info *fbi)
863 {
864         DECLARE_WAITQUEUE(wait, current);
865
866         pr_debug("pxafb: disabling LCD controller\n");
867
868         set_current_state(TASK_UNINTERRUPTIBLE);
869         add_wait_queue(&fbi->ctrlr_wait, &wait);
870
871         LCSR = 0xffffffff;      /* Clear LCD Status Register */
872         LCCR0 &= ~LCCR0_LDM;    /* Enable LCD Disable Done Interrupt */
873         LCCR0 |= LCCR0_DIS;     /* Disable LCD Controller */
874
875         schedule_timeout(200 * HZ / 1000);
876         remove_wait_queue(&fbi->ctrlr_wait, &wait);
877
878         /* disable LCD controller clock */
879         clk_disable(fbi->clk);
880 }
881
882 /*
883  *  pxafb_handle_irq: Handle 'LCD DONE' interrupts.
884  */
885 static irqreturn_t pxafb_handle_irq(int irq, void *dev_id)
886 {
887         struct pxafb_info *fbi = dev_id;
888         unsigned int lcsr = LCSR;
889
890         if (lcsr & LCSR_LDD) {
891                 LCCR0 |= LCCR0_LDM;
892                 wake_up(&fbi->ctrlr_wait);
893         }
894
895         LCSR = lcsr;
896         return IRQ_HANDLED;
897 }
898
899 /*
900  * This function must be called from task context only, since it will
901  * sleep when disabling the LCD controller, or if we get two contending
902  * processes trying to alter state.
903  */
904 static void set_ctrlr_state(struct pxafb_info *fbi, u_int state)
905 {
906         u_int old_state;
907
908         down(&fbi->ctrlr_sem);
909
910         old_state = fbi->state;
911
912         /*
913          * Hack around fbcon initialisation.
914          */
915         if (old_state == C_STARTUP && state == C_REENABLE)
916                 state = C_ENABLE;
917
918         switch (state) {
919         case C_DISABLE_CLKCHANGE:
920                 /*
921                  * Disable controller for clock change.  If the
922                  * controller is already disabled, then do nothing.
923                  */
924                 if (old_state != C_DISABLE && old_state != C_DISABLE_PM) {
925                         fbi->state = state;
926                         /* TODO __pxafb_lcd_power(fbi, 0); */
927                         pxafb_disable_controller(fbi);
928                 }
929                 break;
930
931         case C_DISABLE_PM:
932         case C_DISABLE:
933                 /*
934                  * Disable controller
935                  */
936                 if (old_state != C_DISABLE) {
937                         fbi->state = state;
938                         __pxafb_backlight_power(fbi, 0);
939                         __pxafb_lcd_power(fbi, 0);
940                         if (old_state != C_DISABLE_CLKCHANGE)
941                                 pxafb_disable_controller(fbi);
942                 }
943                 break;
944
945         case C_ENABLE_CLKCHANGE:
946                 /*
947                  * Enable the controller after clock change.  Only
948                  * do this if we were disabled for the clock change.
949                  */
950                 if (old_state == C_DISABLE_CLKCHANGE) {
951                         fbi->state = C_ENABLE;
952                         pxafb_enable_controller(fbi);
953                         /* TODO __pxafb_lcd_power(fbi, 1); */
954                 }
955                 break;
956
957         case C_REENABLE:
958                 /*
959                  * Re-enable the controller only if it was already
960                  * enabled.  This is so we reprogram the control
961                  * registers.
962                  */
963                 if (old_state == C_ENABLE) {
964                         __pxafb_lcd_power(fbi, 0);
965                         pxafb_disable_controller(fbi);
966                         pxafb_setup_gpio(fbi);
967                         pxafb_enable_controller(fbi);
968                         __pxafb_lcd_power(fbi, 1);
969                 }
970                 break;
971
972         case C_ENABLE_PM:
973                 /*
974                  * Re-enable the controller after PM.  This is not
975                  * perfect - think about the case where we were doing
976                  * a clock change, and we suspended half-way through.
977                  */
978                 if (old_state != C_DISABLE_PM)
979                         break;
980                 /* fall through */
981
982         case C_ENABLE:
983                 /*
984                  * Power up the LCD screen, enable controller, and
985                  * turn on the backlight.
986                  */
987                 if (old_state != C_ENABLE) {
988                         fbi->state = C_ENABLE;
989                         pxafb_setup_gpio(fbi);
990                         pxafb_enable_controller(fbi);
991                         __pxafb_lcd_power(fbi, 1);
992                         __pxafb_backlight_power(fbi, 1);
993                 }
994                 break;
995         }
996         up(&fbi->ctrlr_sem);
997 }
998
999 /*
1000  * Our LCD controller task (which is called when we blank or unblank)
1001  * via keventd.
1002  */
1003 static void pxafb_task(struct work_struct *work)
1004 {
1005         struct pxafb_info *fbi =
1006                 container_of(work, struct pxafb_info, task);
1007         u_int state = xchg(&fbi->task_state, -1);
1008
1009         set_ctrlr_state(fbi, state);
1010 }
1011
1012 #ifdef CONFIG_CPU_FREQ
1013 /*
1014  * CPU clock speed change handler.  We need to adjust the LCD timing
1015  * parameters when the CPU clock is adjusted by the power management
1016  * subsystem.
1017  *
1018  * TODO: Determine why f->new != 10*get_lclk_frequency_10khz()
1019  */
1020 static int
1021 pxafb_freq_transition(struct notifier_block *nb, unsigned long val, void *data)
1022 {
1023         struct pxafb_info *fbi = TO_INF(nb, freq_transition);
1024         /* TODO struct cpufreq_freqs *f = data; */
1025         u_int pcd;
1026
1027         switch (val) {
1028         case CPUFREQ_PRECHANGE:
1029                 set_ctrlr_state(fbi, C_DISABLE_CLKCHANGE);
1030                 break;
1031
1032         case CPUFREQ_POSTCHANGE:
1033                 pcd = get_pcd(fbi, fbi->fb.var.pixclock);
1034                 set_hsync_time(fbi, pcd);
1035                 fbi->reg_lccr3 = (fbi->reg_lccr3 & ~0xff) |
1036                                   LCCR3_PixClkDiv(pcd);
1037                 set_ctrlr_state(fbi, C_ENABLE_CLKCHANGE);
1038                 break;
1039         }
1040         return 0;
1041 }
1042
1043 static int
1044 pxafb_freq_policy(struct notifier_block *nb, unsigned long val, void *data)
1045 {
1046         struct pxafb_info *fbi = TO_INF(nb, freq_policy);
1047         struct fb_var_screeninfo *var = &fbi->fb.var;
1048         struct cpufreq_policy *policy = data;
1049
1050         switch (val) {
1051         case CPUFREQ_ADJUST:
1052         case CPUFREQ_INCOMPATIBLE:
1053                 pr_debug("min dma period: %d ps, "
1054                         "new clock %d kHz\n", pxafb_display_dma_period(var),
1055                         policy->max);
1056                 /* TODO: fill in min/max values */
1057                 break;
1058         }
1059         return 0;
1060 }
1061 #endif
1062
1063 #ifdef CONFIG_PM
1064 /*
1065  * Power management hooks.  Note that we won't be called from IRQ context,
1066  * unlike the blank functions above, so we may sleep.
1067  */
1068 static int pxafb_suspend(struct platform_device *dev, pm_message_t state)
1069 {
1070         struct pxafb_info *fbi = platform_get_drvdata(dev);
1071
1072         set_ctrlr_state(fbi, C_DISABLE_PM);
1073         return 0;
1074 }
1075
1076 static int pxafb_resume(struct platform_device *dev)
1077 {
1078         struct pxafb_info *fbi = platform_get_drvdata(dev);
1079
1080         set_ctrlr_state(fbi, C_ENABLE_PM);
1081         return 0;
1082 }
1083 #else
1084 #define pxafb_suspend   NULL
1085 #define pxafb_resume    NULL
1086 #endif
1087
1088 /*
1089  * pxafb_map_video_memory():
1090  *      Allocates the DRAM memory for the frame buffer.  This buffer is
1091  *      remapped into a non-cached, non-buffered, memory region to
1092  *      allow palette and pixel writes to occur without flushing the
1093  *      cache.  Once this area is remapped, all virtual memory
1094  *      access to the video memory should occur at the new region.
1095  */
1096 static int __init pxafb_map_video_memory(struct pxafb_info *fbi)
1097 {
1098         u_long palette_mem_size;
1099
1100         /*
1101          * We reserve one page for the palette, plus the size
1102          * of the framebuffer.
1103          */
1104         fbi->map_size = PAGE_ALIGN(fbi->fb.fix.smem_len + PAGE_SIZE);
1105         fbi->map_cpu = dma_alloc_writecombine(fbi->dev, fbi->map_size,
1106                                               &fbi->map_dma, GFP_KERNEL);
1107
1108         if (fbi->map_cpu) {
1109                 /* prevent initial garbage on screen */
1110                 memset(fbi->map_cpu, 0, fbi->map_size);
1111                 fbi->fb.screen_base = fbi->map_cpu + PAGE_SIZE;
1112                 fbi->screen_dma = fbi->map_dma + PAGE_SIZE;
1113                 /*
1114                  * FIXME: this is actually the wrong thing to place in
1115                  * smem_start.  But fbdev suffers from the problem that
1116                  * it needs an API which doesn't exist (in this case,
1117                  * dma_writecombine_mmap)
1118                  */
1119                 fbi->fb.fix.smem_start = fbi->screen_dma;
1120                 fbi->palette_size = fbi->fb.var.bits_per_pixel == 8 ? 256 : 16;
1121
1122                 if ((fbi->lccr4 & LCCR4_PAL_FOR_MASK) == LCCR4_PAL_FOR_0)
1123                         palette_mem_size = fbi->palette_size * sizeof(u16);
1124                 else
1125                         palette_mem_size = fbi->palette_size * sizeof(u32);
1126
1127                 pr_debug("pxafb: palette_mem_size = 0x%08lx\n",
1128                                 palette_mem_size);
1129
1130                 fbi->palette_cpu = (u16 *)(fbi->map_cpu + PAGE_SIZE
1131                                                 - palette_mem_size);
1132                 fbi->palette_dma = fbi->map_dma + PAGE_SIZE - palette_mem_size;
1133         }
1134
1135         return fbi->map_cpu ? 0 : -ENOMEM;
1136 }
1137
1138 static struct pxafb_info * __init pxafb_init_fbinfo(struct device *dev)
1139 {
1140         struct pxafb_info *fbi;
1141         void *addr;
1142         struct pxafb_mach_info *inf = dev->platform_data;
1143         struct pxafb_mode_info *mode = inf->modes;
1144         int i, smemlen;
1145
1146         /* Alloc the pxafb_info and pseudo_palette in one step */
1147         fbi = kmalloc(sizeof(struct pxafb_info) + sizeof(u32) * 16, GFP_KERNEL);
1148         if (!fbi)
1149                 return NULL;
1150
1151         memset(fbi, 0, sizeof(struct pxafb_info));
1152         fbi->dev = dev;
1153
1154         fbi->clk = clk_get(dev, "LCDCLK");
1155         if (IS_ERR(fbi->clk)) {
1156                 kfree(fbi);
1157                 return NULL;
1158         }
1159
1160         strcpy(fbi->fb.fix.id, PXA_NAME);
1161
1162         fbi->fb.fix.type        = FB_TYPE_PACKED_PIXELS;
1163         fbi->fb.fix.type_aux    = 0;
1164         fbi->fb.fix.xpanstep    = 0;
1165         fbi->fb.fix.ypanstep    = 0;
1166         fbi->fb.fix.ywrapstep   = 0;
1167         fbi->fb.fix.accel       = FB_ACCEL_NONE;
1168
1169         fbi->fb.var.nonstd      = 0;
1170         fbi->fb.var.activate    = FB_ACTIVATE_NOW;
1171         fbi->fb.var.height      = -1;
1172         fbi->fb.var.width       = -1;
1173         fbi->fb.var.accel_flags = 0;
1174         fbi->fb.var.vmode       = FB_VMODE_NONINTERLACED;
1175
1176         fbi->fb.fbops           = &pxafb_ops;
1177         fbi->fb.flags           = FBINFO_DEFAULT;
1178         fbi->fb.node            = -1;
1179
1180         addr = fbi;
1181         addr = addr + sizeof(struct pxafb_info);
1182         fbi->fb.pseudo_palette  = addr;
1183
1184         pxafb_setmode(&fbi->fb.var, mode);
1185
1186         fbi->cmap_inverse       = inf->cmap_inverse;
1187         fbi->cmap_static        = inf->cmap_static;
1188
1189         fbi->lccr0              = inf->lccr0;
1190         fbi->lccr3              = inf->lccr3;
1191         fbi->lccr4              = inf->lccr4;
1192         fbi->state              = C_STARTUP;
1193         fbi->task_state         = (u_char)-1;
1194
1195         for (i = 0; i < inf->num_modes; i++) {
1196                 smemlen = mode[i].xres * mode[i].yres * mode[i].bpp / 8;
1197                 if (smemlen > fbi->fb.fix.smem_len)
1198                         fbi->fb.fix.smem_len = smemlen;
1199         }
1200
1201         init_waitqueue_head(&fbi->ctrlr_wait);
1202         INIT_WORK(&fbi->task, pxafb_task);
1203         init_MUTEX(&fbi->ctrlr_sem);
1204
1205         return fbi;
1206 }
1207
1208 #ifdef CONFIG_FB_PXA_PARAMETERS
1209 static int __init parse_opt_mode(struct device *dev, const char *this_opt)
1210 {
1211         struct pxafb_mach_info *inf = dev->platform_data;
1212
1213         const char *name = this_opt+5;
1214         unsigned int namelen = strlen(name);
1215         int res_specified = 0, bpp_specified = 0;
1216         unsigned int xres = 0, yres = 0, bpp = 0;
1217         int yres_specified = 0;
1218         int i;
1219         for (i = namelen-1; i >= 0; i--) {
1220                 switch (name[i]) {
1221                 case '-':
1222                         namelen = i;
1223                         if (!bpp_specified && !yres_specified) {
1224                                 bpp = simple_strtoul(&name[i+1], NULL, 0);
1225                                 bpp_specified = 1;
1226                         } else
1227                                 goto done;
1228                         break;
1229                 case 'x':
1230                         if (!yres_specified) {
1231                                 yres = simple_strtoul(&name[i+1], NULL, 0);
1232                                 yres_specified = 1;
1233                         } else
1234                                 goto done;
1235                         break;
1236                 case '0' ... '9':
1237                         break;
1238                 default:
1239                         goto done;
1240                 }
1241         }
1242         if (i < 0 && yres_specified) {
1243                 xres = simple_strtoul(name, NULL, 0);
1244                 res_specified = 1;
1245         }
1246 done:
1247         if (res_specified) {
1248                 dev_info(dev, "overriding resolution: %dx%d\n", xres, yres);
1249                 inf->modes[0].xres = xres; inf->modes[0].yres = yres;
1250         }
1251         if (bpp_specified)
1252                 switch (bpp) {
1253                 case 1:
1254                 case 2:
1255                 case 4:
1256                 case 8:
1257                 case 16:
1258                         inf->modes[0].bpp = bpp;
1259                         dev_info(dev, "overriding bit depth: %d\n", bpp);
1260                         break;
1261                 default:
1262                         dev_err(dev, "Depth %d is not valid\n", bpp);
1263                         return -EINVAL;
1264                 }
1265         return 0;
1266 }
1267
1268 static int __init parse_opt(struct device *dev, char *this_opt)
1269 {
1270         struct pxafb_mach_info *inf = dev->platform_data;
1271         struct pxafb_mode_info *mode = &inf->modes[0];
1272         char s[64];
1273
1274         s[0] = '\0';
1275
1276         if (!strncmp(this_opt, "mode:", 5)) {
1277                 return parse_opt_mode(dev, this_opt);
1278         } else if (!strncmp(this_opt, "pixclock:", 9)) {
1279                 mode->pixclock = simple_strtoul(this_opt+9, NULL, 0);
1280                 sprintf(s, "pixclock: %ld\n", mode->pixclock);
1281         } else if (!strncmp(this_opt, "left:", 5)) {
1282                 mode->left_margin = simple_strtoul(this_opt+5, NULL, 0);
1283                 sprintf(s, "left: %u\n", mode->left_margin);
1284         } else if (!strncmp(this_opt, "right:", 6)) {
1285                 mode->right_margin = simple_strtoul(this_opt+6, NULL, 0);
1286                 sprintf(s, "right: %u\n", mode->right_margin);
1287         } else if (!strncmp(this_opt, "upper:", 6)) {
1288                 mode->upper_margin = simple_strtoul(this_opt+6, NULL, 0);
1289                 sprintf(s, "upper: %u\n", mode->upper_margin);
1290         } else if (!strncmp(this_opt, "lower:", 6)) {
1291                 mode->lower_margin = simple_strtoul(this_opt+6, NULL, 0);
1292                 sprintf(s, "lower: %u\n", mode->lower_margin);
1293         } else if (!strncmp(this_opt, "hsynclen:", 9)) {
1294                 mode->hsync_len = simple_strtoul(this_opt+9, NULL, 0);
1295                 sprintf(s, "hsynclen: %u\n", mode->hsync_len);
1296         } else if (!strncmp(this_opt, "vsynclen:", 9)) {
1297                 mode->vsync_len = simple_strtoul(this_opt+9, NULL, 0);
1298                 sprintf(s, "vsynclen: %u\n", mode->vsync_len);
1299         } else if (!strncmp(this_opt, "hsync:", 6)) {
1300                 if (simple_strtoul(this_opt+6, NULL, 0) == 0) {
1301                         sprintf(s, "hsync: Active Low\n");
1302                         mode->sync &= ~FB_SYNC_HOR_HIGH_ACT;
1303                 } else {
1304                         sprintf(s, "hsync: Active High\n");
1305                         mode->sync |= FB_SYNC_HOR_HIGH_ACT;
1306                 }
1307         } else if (!strncmp(this_opt, "vsync:", 6)) {
1308                 if (simple_strtoul(this_opt+6, NULL, 0) == 0) {
1309                         sprintf(s, "vsync: Active Low\n");
1310                         mode->sync &= ~FB_SYNC_VERT_HIGH_ACT;
1311                 } else {
1312                         sprintf(s, "vsync: Active High\n");
1313                         mode->sync |= FB_SYNC_VERT_HIGH_ACT;
1314                 }
1315         } else if (!strncmp(this_opt, "dpc:", 4)) {
1316                 if (simple_strtoul(this_opt+4, NULL, 0) == 0) {
1317                         sprintf(s, "double pixel clock: false\n");
1318                         inf->lccr3 &= ~LCCR3_DPC;
1319                 } else {
1320                         sprintf(s, "double pixel clock: true\n");
1321                         inf->lccr3 |= LCCR3_DPC;
1322                 }
1323         } else if (!strncmp(this_opt, "outputen:", 9)) {
1324                 if (simple_strtoul(this_opt+9, NULL, 0) == 0) {
1325                         sprintf(s, "output enable: active low\n");
1326                         inf->lccr3 = (inf->lccr3 & ~LCCR3_OEP) | LCCR3_OutEnL;
1327                 } else {
1328                         sprintf(s, "output enable: active high\n");
1329                         inf->lccr3 = (inf->lccr3 & ~LCCR3_OEP) | LCCR3_OutEnH;
1330                 }
1331         } else if (!strncmp(this_opt, "pixclockpol:", 12)) {
1332                 if (simple_strtoul(this_opt+12, NULL, 0) == 0) {
1333                         sprintf(s, "pixel clock polarity: falling edge\n");
1334                         inf->lccr3 = (inf->lccr3 & ~LCCR3_PCP) | LCCR3_PixFlEdg;
1335                 } else {
1336                         sprintf(s, "pixel clock polarity: rising edge\n");
1337                         inf->lccr3 = (inf->lccr3 & ~LCCR3_PCP) | LCCR3_PixRsEdg;
1338                 }
1339         } else if (!strncmp(this_opt, "color", 5)) {
1340                 inf->lccr0 = (inf->lccr0 & ~LCCR0_CMS) | LCCR0_Color;
1341         } else if (!strncmp(this_opt, "mono", 4)) {
1342                 inf->lccr0 = (inf->lccr0 & ~LCCR0_CMS) | LCCR0_Mono;
1343         } else if (!strncmp(this_opt, "active", 6)) {
1344                 inf->lccr0 = (inf->lccr0 & ~LCCR0_PAS) | LCCR0_Act;
1345         } else if (!strncmp(this_opt, "passive", 7)) {
1346                 inf->lccr0 = (inf->lccr0 & ~LCCR0_PAS) | LCCR0_Pas;
1347         } else if (!strncmp(this_opt, "single", 6)) {
1348                 inf->lccr0 = (inf->lccr0 & ~LCCR0_SDS) | LCCR0_Sngl;
1349         } else if (!strncmp(this_opt, "dual", 4)) {
1350                 inf->lccr0 = (inf->lccr0 & ~LCCR0_SDS) | LCCR0_Dual;
1351         } else if (!strncmp(this_opt, "4pix", 4)) {
1352                 inf->lccr0 = (inf->lccr0 & ~LCCR0_DPD) | LCCR0_4PixMono;
1353         } else if (!strncmp(this_opt, "8pix", 4)) {
1354                 inf->lccr0 = (inf->lccr0 & ~LCCR0_DPD) | LCCR0_8PixMono;
1355         } else {
1356                 dev_err(dev, "unknown option: %s\n", this_opt);
1357                 return -EINVAL;
1358         }
1359
1360         if (s[0] != '\0')
1361                 dev_info(dev, "override %s", s);
1362
1363         return 0;
1364 }
1365
1366 static int __init pxafb_parse_options(struct device *dev, char *options)
1367 {
1368         char *this_opt;
1369         int ret;
1370
1371         if (!options || !*options)
1372                 return 0;
1373
1374         dev_dbg(dev, "options are \"%s\"\n", options ? options : "null");
1375
1376         /* could be made table driven or similar?... */
1377         while ((this_opt = strsep(&options, ",")) != NULL) {
1378                 ret = parse_opt(dev, this_opt);
1379                 if (ret)
1380                         return ret;
1381         }
1382         return 0;
1383 }
1384 #endif
1385
1386 static int __init pxafb_probe(struct platform_device *dev)
1387 {
1388         struct pxafb_info *fbi;
1389         struct pxafb_mach_info *inf;
1390         int ret;
1391
1392         dev_dbg(&dev->dev, "pxafb_probe\n");
1393
1394         inf = dev->dev.platform_data;
1395         ret = -ENOMEM;
1396         fbi = NULL;
1397         if (!inf)
1398                 goto failed;
1399
1400 #ifdef CONFIG_FB_PXA_PARAMETERS
1401         ret = pxafb_parse_options(&dev->dev, g_options);
1402         if (ret < 0)
1403                 goto failed;
1404 #endif
1405
1406 #ifdef DEBUG_VAR
1407         /* Check for various illegal bit-combinations. Currently only
1408          * a warning is given. */
1409
1410         if (inf->lccr0 & LCCR0_INVALID_CONFIG_MASK)
1411                 dev_warn(&dev->dev, "machine LCCR0 setting contains "
1412                                 "illegal bits: %08x\n",
1413                         inf->lccr0 & LCCR0_INVALID_CONFIG_MASK);
1414         if (inf->lccr3 & LCCR3_INVALID_CONFIG_MASK)
1415                 dev_warn(&dev->dev, "machine LCCR3 setting contains "
1416                                 "illegal bits: %08x\n",
1417                         inf->lccr3 & LCCR3_INVALID_CONFIG_MASK);
1418         if (inf->lccr0 & LCCR0_DPD &&
1419             ((inf->lccr0 & LCCR0_PAS) != LCCR0_Pas ||
1420              (inf->lccr0 & LCCR0_SDS) != LCCR0_Sngl ||
1421              (inf->lccr0 & LCCR0_CMS) != LCCR0_Mono))
1422                 dev_warn(&dev->dev, "Double Pixel Data (DPD) mode is "
1423                                 "only valid in passive mono"
1424                                 " single panel mode\n");
1425         if ((inf->lccr0 & LCCR0_PAS) == LCCR0_Act &&
1426             (inf->lccr0 & LCCR0_SDS) == LCCR0_Dual)
1427                 dev_warn(&dev->dev, "Dual panel only valid in passive mode\n");
1428         if ((inf->lccr0 & LCCR0_PAS) == LCCR0_Pas &&
1429              (inf->modes->upper_margin || inf->modes->lower_margin))
1430                 dev_warn(&dev->dev, "Upper and lower margins must be 0 in "
1431                                 "passive mode\n");
1432 #endif
1433
1434         dev_dbg(&dev->dev, "got a %dx%dx%d LCD\n",
1435                         inf->modes->xres,
1436                         inf->modes->yres,
1437                         inf->modes->bpp);
1438         if (inf->modes->xres == 0 ||
1439             inf->modes->yres == 0 ||
1440             inf->modes->bpp == 0) {
1441                 dev_err(&dev->dev, "Invalid resolution or bit depth\n");
1442                 ret = -EINVAL;
1443                 goto failed;
1444         }
1445         pxafb_backlight_power = inf->pxafb_backlight_power;
1446         pxafb_lcd_power = inf->pxafb_lcd_power;
1447         fbi = pxafb_init_fbinfo(&dev->dev);
1448         if (!fbi) {
1449                 /* only reason for pxafb_init_fbinfo to fail is kmalloc */
1450                 dev_err(&dev->dev, "Failed to initialize framebuffer device\n");
1451                 ret = -ENOMEM;
1452                 goto failed;
1453         }
1454
1455         /* Initialize video memory */
1456         ret = pxafb_map_video_memory(fbi);
1457         if (ret) {
1458                 dev_err(&dev->dev, "Failed to allocate video RAM: %d\n", ret);
1459                 ret = -ENOMEM;
1460                 goto failed;
1461         }
1462
1463         ret = request_irq(IRQ_LCD, pxafb_handle_irq, IRQF_DISABLED, "LCD", fbi);
1464         if (ret) {
1465                 dev_err(&dev->dev, "request_irq failed: %d\n", ret);
1466                 ret = -EBUSY;
1467                 goto failed;
1468         }
1469
1470         /*
1471          * This makes sure that our colour bitfield
1472          * descriptors are correctly initialised.
1473          */
1474         pxafb_check_var(&fbi->fb.var, &fbi->fb);
1475         pxafb_set_par(&fbi->fb);
1476
1477         platform_set_drvdata(dev, fbi);
1478
1479         ret = register_framebuffer(&fbi->fb);
1480         if (ret < 0) {
1481                 dev_err(&dev->dev,
1482                         "Failed to register framebuffer device: %d\n", ret);
1483                 goto failed;
1484         }
1485
1486 #ifdef CONFIG_PM
1487         /* TODO */
1488 #endif
1489
1490 #ifdef CONFIG_CPU_FREQ
1491         fbi->freq_transition.notifier_call = pxafb_freq_transition;
1492         fbi->freq_policy.notifier_call = pxafb_freq_policy;
1493         cpufreq_register_notifier(&fbi->freq_transition,
1494                                 CPUFREQ_TRANSITION_NOTIFIER);
1495         cpufreq_register_notifier(&fbi->freq_policy,
1496                                 CPUFREQ_POLICY_NOTIFIER);
1497 #endif
1498
1499         /*
1500          * Ok, now enable the LCD controller
1501          */
1502         set_ctrlr_state(fbi, C_ENABLE);
1503
1504         return 0;
1505
1506 failed:
1507         platform_set_drvdata(dev, NULL);
1508         kfree(fbi);
1509         return ret;
1510 }
1511
1512 static struct platform_driver pxafb_driver = {
1513         .probe          = pxafb_probe,
1514 #ifdef CONFIG_PM
1515         .suspend        = pxafb_suspend,
1516         .resume         = pxafb_resume,
1517 #endif
1518         .driver         = {
1519                 .name   = "pxa2xx-fb",
1520         },
1521 };
1522
1523 #ifndef MODULE
1524 static int __devinit pxafb_setup(char *options)
1525 {
1526 # ifdef CONFIG_FB_PXA_PARAMETERS
1527         if (options)
1528                 strlcpy(g_options, options, sizeof(g_options));
1529 # endif
1530         return 0;
1531 }
1532 #else
1533 # ifdef CONFIG_FB_PXA_PARAMETERS
1534 module_param_string(options, g_options, sizeof(g_options), 0);
1535 MODULE_PARM_DESC(options, "LCD parameters (see Documentation/fb/pxafb.txt)");
1536 # endif
1537 #endif
1538
1539 static int __devinit pxafb_init(void)
1540 {
1541 #ifndef MODULE
1542         char *option = NULL;
1543
1544         if (fb_get_options("pxafb", &option))
1545                 return -ENODEV;
1546         pxafb_setup(option);
1547 #endif
1548         return platform_driver_register(&pxafb_driver);
1549 }
1550
1551 module_init(pxafb_init);
1552
1553 MODULE_DESCRIPTION("loadable framebuffer driver for PXA");
1554 MODULE_LICENSE("GPL");