Merge branch 'for-rmk' of git://git.kernel.org/pub/scm/linux/kernel/git/ycmiao/pxa...
[safe/jmp/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/mm.h>
34 #include <linux/fb.h>
35 #include <linux/delay.h>
36 #include <linux/init.h>
37 #include <linux/ioport.h>
38 #include <linux/cpufreq.h>
39 #include <linux/platform_device.h>
40 #include <linux/dma-mapping.h>
41 #include <linux/clk.h>
42 #include <linux/err.h>
43 #include <linux/completion.h>
44 #include <linux/mutex.h>
45 #include <linux/kthread.h>
46 #include <linux/freezer.h>
47
48 #include <mach/hardware.h>
49 #include <asm/io.h>
50 #include <asm/irq.h>
51 #include <asm/div64.h>
52 #include <mach/pxa-regs.h>
53 #include <mach/pxa2xx-gpio.h>
54 #include <mach/bitfield.h>
55 #include <mach/pxafb.h>
56
57 /*
58  * Complain if VAR is out of range.
59  */
60 #define DEBUG_VAR 1
61
62 #include "pxafb.h"
63
64 /* Bits which should not be set in machine configuration structures */
65 #define LCCR0_INVALID_CONFIG_MASK       (LCCR0_OUM | LCCR0_BM | LCCR0_QDM |\
66                                          LCCR0_DIS | LCCR0_EFM | LCCR0_IUM |\
67                                          LCCR0_SFM | LCCR0_LDM | LCCR0_ENB)
68
69 #define LCCR3_INVALID_CONFIG_MASK       (LCCR3_HSP | LCCR3_VSP |\
70                                          LCCR3_PCD | LCCR3_BPP)
71
72 static int pxafb_activate_var(struct fb_var_screeninfo *var,
73                                 struct pxafb_info *);
74 static void set_ctrlr_state(struct pxafb_info *fbi, u_int state);
75
76 static inline unsigned long
77 lcd_readl(struct pxafb_info *fbi, unsigned int off)
78 {
79         return __raw_readl(fbi->mmio_base + off);
80 }
81
82 static inline void
83 lcd_writel(struct pxafb_info *fbi, unsigned int off, unsigned long val)
84 {
85         __raw_writel(val, fbi->mmio_base + off);
86 }
87
88 static inline void pxafb_schedule_work(struct pxafb_info *fbi, u_int state)
89 {
90         unsigned long flags;
91
92         local_irq_save(flags);
93         /*
94          * We need to handle two requests being made at the same time.
95          * There are two important cases:
96          *  1. When we are changing VT (C_REENABLE) while unblanking
97          *     (C_ENABLE) We must perform the unblanking, which will
98          *     do our REENABLE for us.
99          *  2. When we are blanking, but immediately unblank before
100          *     we have blanked.  We do the "REENABLE" thing here as
101          *     well, just to be sure.
102          */
103         if (fbi->task_state == C_ENABLE && state == C_REENABLE)
104                 state = (u_int) -1;
105         if (fbi->task_state == C_DISABLE && state == C_ENABLE)
106                 state = C_REENABLE;
107
108         if (state != (u_int)-1) {
109                 fbi->task_state = state;
110                 schedule_work(&fbi->task);
111         }
112         local_irq_restore(flags);
113 }
114
115 static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
116 {
117         chan &= 0xffff;
118         chan >>= 16 - bf->length;
119         return chan << bf->offset;
120 }
121
122 static int
123 pxafb_setpalettereg(u_int regno, u_int red, u_int green, u_int blue,
124                        u_int trans, struct fb_info *info)
125 {
126         struct pxafb_info *fbi = (struct pxafb_info *)info;
127         u_int val;
128
129         if (regno >= fbi->palette_size)
130                 return 1;
131
132         if (fbi->fb.var.grayscale) {
133                 fbi->palette_cpu[regno] = ((blue >> 8) & 0x00ff);
134                 return 0;
135         }
136
137         switch (fbi->lccr4 & LCCR4_PAL_FOR_MASK) {
138         case LCCR4_PAL_FOR_0:
139                 val  = ((red   >>  0) & 0xf800);
140                 val |= ((green >>  5) & 0x07e0);
141                 val |= ((blue  >> 11) & 0x001f);
142                 fbi->palette_cpu[regno] = val;
143                 break;
144         case LCCR4_PAL_FOR_1:
145                 val  = ((red   << 8) & 0x00f80000);
146                 val |= ((green >> 0) & 0x0000fc00);
147                 val |= ((blue  >> 8) & 0x000000f8);
148                 ((u32 *)(fbi->palette_cpu))[regno] = val;
149                 break;
150         case LCCR4_PAL_FOR_2:
151                 val  = ((red   << 8) & 0x00fc0000);
152                 val |= ((green >> 0) & 0x0000fc00);
153                 val |= ((blue  >> 8) & 0x000000fc);
154                 ((u32 *)(fbi->palette_cpu))[regno] = val;
155                 break;
156         }
157
158         return 0;
159 }
160
161 static int
162 pxafb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
163                    u_int trans, struct fb_info *info)
164 {
165         struct pxafb_info *fbi = (struct pxafb_info *)info;
166         unsigned int val;
167         int ret = 1;
168
169         /*
170          * If inverse mode was selected, invert all the colours
171          * rather than the register number.  The register number
172          * is what you poke into the framebuffer to produce the
173          * colour you requested.
174          */
175         if (fbi->cmap_inverse) {
176                 red   = 0xffff - red;
177                 green = 0xffff - green;
178                 blue  = 0xffff - blue;
179         }
180
181         /*
182          * If greyscale is true, then we convert the RGB value
183          * to greyscale no matter what visual we are using.
184          */
185         if (fbi->fb.var.grayscale)
186                 red = green = blue = (19595 * red + 38470 * green +
187                                         7471 * blue) >> 16;
188
189         switch (fbi->fb.fix.visual) {
190         case FB_VISUAL_TRUECOLOR:
191                 /*
192                  * 16-bit True Colour.  We encode the RGB value
193                  * according to the RGB bitfield information.
194                  */
195                 if (regno < 16) {
196                         u32 *pal = fbi->fb.pseudo_palette;
197
198                         val  = chan_to_field(red, &fbi->fb.var.red);
199                         val |= chan_to_field(green, &fbi->fb.var.green);
200                         val |= chan_to_field(blue, &fbi->fb.var.blue);
201
202                         pal[regno] = val;
203                         ret = 0;
204                 }
205                 break;
206
207         case FB_VISUAL_STATIC_PSEUDOCOLOR:
208         case FB_VISUAL_PSEUDOCOLOR:
209                 ret = pxafb_setpalettereg(regno, red, green, blue, trans, info);
210                 break;
211         }
212
213         return ret;
214 }
215
216 /*
217  *  pxafb_bpp_to_lccr3():
218  *    Convert a bits per pixel value to the correct bit pattern for LCCR3
219  */
220 static int pxafb_bpp_to_lccr3(struct fb_var_screeninfo *var)
221 {
222         int ret = 0;
223         switch (var->bits_per_pixel) {
224         case 1:  ret = LCCR3_1BPP; break;
225         case 2:  ret = LCCR3_2BPP; break;
226         case 4:  ret = LCCR3_4BPP; break;
227         case 8:  ret = LCCR3_8BPP; break;
228         case 16: ret = LCCR3_16BPP; break;
229         case 24:
230                 switch (var->red.length + var->green.length +
231                                 var->blue.length + var->transp.length) {
232                 case 18: ret = LCCR3_18BPP_P | LCCR3_PDFOR_3; break;
233                 case 19: ret = LCCR3_19BPP_P; break;
234                 }
235                 break;
236         case 32:
237                 switch (var->red.length + var->green.length +
238                                 var->blue.length + var->transp.length) {
239                 case 18: ret = LCCR3_18BPP | LCCR3_PDFOR_3; break;
240                 case 19: ret = LCCR3_19BPP; break;
241                 case 24: ret = LCCR3_24BPP | LCCR3_PDFOR_3; break;
242                 case 25: ret = LCCR3_25BPP; break;
243                 }
244                 break;
245         }
246         return ret;
247 }
248
249 #ifdef CONFIG_CPU_FREQ
250 /*
251  *  pxafb_display_dma_period()
252  *    Calculate the minimum period (in picoseconds) between two DMA
253  *    requests for the LCD controller.  If we hit this, it means we're
254  *    doing nothing but LCD DMA.
255  */
256 static unsigned int pxafb_display_dma_period(struct fb_var_screeninfo *var)
257 {
258         /*
259          * Period = pixclock * bits_per_byte * bytes_per_transfer
260          *              / memory_bits_per_pixel;
261          */
262         return var->pixclock * 8 * 16 / var->bits_per_pixel;
263 }
264 #endif
265
266 /*
267  * Select the smallest mode that allows the desired resolution to be
268  * displayed. If desired parameters can be rounded up.
269  */
270 static struct pxafb_mode_info *pxafb_getmode(struct pxafb_mach_info *mach,
271                                              struct fb_var_screeninfo *var)
272 {
273         struct pxafb_mode_info *mode = NULL;
274         struct pxafb_mode_info *modelist = mach->modes;
275         unsigned int best_x = 0xffffffff, best_y = 0xffffffff;
276         unsigned int i;
277
278         for (i = 0; i < mach->num_modes; i++) {
279                 if (modelist[i].xres >= var->xres &&
280                     modelist[i].yres >= var->yres &&
281                     modelist[i].xres < best_x &&
282                     modelist[i].yres < best_y &&
283                     modelist[i].bpp >= var->bits_per_pixel) {
284                         best_x = modelist[i].xres;
285                         best_y = modelist[i].yres;
286                         mode = &modelist[i];
287                 }
288         }
289
290         return mode;
291 }
292
293 static void pxafb_setmode(struct fb_var_screeninfo *var,
294                           struct pxafb_mode_info *mode)
295 {
296         var->xres               = mode->xres;
297         var->yres               = mode->yres;
298         var->bits_per_pixel     = mode->bpp;
299         var->pixclock           = mode->pixclock;
300         var->hsync_len          = mode->hsync_len;
301         var->left_margin        = mode->left_margin;
302         var->right_margin       = mode->right_margin;
303         var->vsync_len          = mode->vsync_len;
304         var->upper_margin       = mode->upper_margin;
305         var->lower_margin       = mode->lower_margin;
306         var->sync               = mode->sync;
307         var->grayscale          = mode->cmap_greyscale;
308         var->xres_virtual       = var->xres;
309         var->yres_virtual       = var->yres;
310 }
311
312 /*
313  *  pxafb_check_var():
314  *    Get the video params out of 'var'. If a value doesn't fit, round it up,
315  *    if it's too big, return -EINVAL.
316  *
317  *    Round up in the following order: bits_per_pixel, xres,
318  *    yres, xres_virtual, yres_virtual, xoffset, yoffset, grayscale,
319  *    bitfields, horizontal timing, vertical timing.
320  */
321 static int pxafb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
322 {
323         struct pxafb_info *fbi = (struct pxafb_info *)info;
324         struct pxafb_mach_info *inf = fbi->dev->platform_data;
325
326         if (var->xres < MIN_XRES)
327                 var->xres = MIN_XRES;
328         if (var->yres < MIN_YRES)
329                 var->yres = MIN_YRES;
330
331         if (inf->fixed_modes) {
332                 struct pxafb_mode_info *mode;
333
334                 mode = pxafb_getmode(inf, var);
335                 if (!mode)
336                         return -EINVAL;
337                 pxafb_setmode(var, mode);
338         } else {
339                 if (var->xres > inf->modes->xres)
340                         return -EINVAL;
341                 if (var->yres > inf->modes->yres)
342                         return -EINVAL;
343                 if (var->bits_per_pixel > inf->modes->bpp)
344                         return -EINVAL;
345         }
346
347         var->xres_virtual =
348                 max(var->xres_virtual, var->xres);
349         var->yres_virtual =
350                 max(var->yres_virtual, var->yres);
351
352         /*
353          * Setup the RGB parameters for this display.
354          *
355          * The pixel packing format is described on page 7-11 of the
356          * PXA2XX Developer's Manual.
357          */
358         if (var->bits_per_pixel == 16) {
359                 var->red.offset   = 11; var->red.length   = 5;
360                 var->green.offset = 5;  var->green.length = 6;
361                 var->blue.offset  = 0;  var->blue.length  = 5;
362                 var->transp.offset = var->transp.length = 0;
363         } else if (var->bits_per_pixel > 16) {
364                 struct pxafb_mode_info *mode;
365
366                 mode = pxafb_getmode(inf, var);
367                 if (!mode)
368                         return -EINVAL;
369
370                 switch (mode->depth) {
371                 case 18: /* RGB666 */
372                         var->transp.offset = var->transp.length     = 0;
373                         var->red.offset    = 12; var->red.length    = 6;
374                         var->green.offset  = 6;  var->green.length  = 6;
375                         var->blue.offset   = 0;  var->blue.length   = 6;
376                         break;
377                 case 19: /* RGBT666 */
378                         var->transp.offset = 18; var->transp.length = 1;
379                         var->red.offset    = 12; var->red.length    = 6;
380                         var->green.offset  = 6;  var->green.length  = 6;
381                         var->blue.offset   = 0;  var->blue.length   = 6;
382                         break;
383                 case 24: /* RGB888 */
384                         var->transp.offset = var->transp.length     = 0;
385                         var->red.offset    = 16; var->red.length    = 8;
386                         var->green.offset  = 8;  var->green.length  = 8;
387                         var->blue.offset   = 0;  var->blue.length   = 8;
388                         break;
389                 case 25: /* RGBT888 */
390                         var->transp.offset = 24; var->transp.length = 1;
391                         var->red.offset    = 16; var->red.length    = 8;
392                         var->green.offset  = 8;  var->green.length  = 8;
393                         var->blue.offset   = 0;  var->blue.length   = 8;
394                         break;
395                 default:
396                         return -EINVAL;
397                 }
398         } else {
399                 var->red.offset = var->green.offset = 0;
400                 var->blue.offset = var->transp.offset = 0;
401                 var->red.length   = 8;
402                 var->green.length = 8;
403                 var->blue.length  = 8;
404                 var->transp.length = 0;
405         }
406
407 #ifdef CONFIG_CPU_FREQ
408         pr_debug("pxafb: dma period = %d ps\n",
409                  pxafb_display_dma_period(var));
410 #endif
411
412         return 0;
413 }
414
415 static inline void pxafb_set_truecolor(u_int is_true_color)
416 {
417         /* do your machine-specific setup if needed */
418 }
419
420 /*
421  * pxafb_set_par():
422  *      Set the user defined part of the display for the specified console
423  */
424 static int pxafb_set_par(struct fb_info *info)
425 {
426         struct pxafb_info *fbi = (struct pxafb_info *)info;
427         struct fb_var_screeninfo *var = &info->var;
428
429         if (var->bits_per_pixel >= 16)
430                 fbi->fb.fix.visual = FB_VISUAL_TRUECOLOR;
431         else if (!fbi->cmap_static)
432                 fbi->fb.fix.visual = FB_VISUAL_PSEUDOCOLOR;
433         else {
434                 /*
435                  * Some people have weird ideas about wanting static
436                  * pseudocolor maps.  I suspect their user space
437                  * applications are broken.
438                  */
439                 fbi->fb.fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
440         }
441
442         fbi->fb.fix.line_length = var->xres_virtual *
443                                   var->bits_per_pixel / 8;
444         if (var->bits_per_pixel >= 16)
445                 fbi->palette_size = 0;
446         else
447                 fbi->palette_size = var->bits_per_pixel == 1 ?
448                                         4 : 1 << var->bits_per_pixel;
449
450         fbi->palette_cpu = (u16 *)&fbi->dma_buff->palette[0];
451
452         /*
453          * Set (any) board control register to handle new color depth
454          */
455         pxafb_set_truecolor(fbi->fb.fix.visual == FB_VISUAL_TRUECOLOR);
456
457         if (fbi->fb.var.bits_per_pixel >= 16)
458                 fb_dealloc_cmap(&fbi->fb.cmap);
459         else
460                 fb_alloc_cmap(&fbi->fb.cmap, 1<<fbi->fb.var.bits_per_pixel, 0);
461
462         pxafb_activate_var(var, fbi);
463
464         return 0;
465 }
466
467 /*
468  * pxafb_blank():
469  *      Blank the display by setting all palette values to zero.  Note, the
470  *      16 bpp mode does not really use the palette, so this will not
471  *      blank the display in all modes.
472  */
473 static int pxafb_blank(int blank, struct fb_info *info)
474 {
475         struct pxafb_info *fbi = (struct pxafb_info *)info;
476         int i;
477
478         switch (blank) {
479         case FB_BLANK_POWERDOWN:
480         case FB_BLANK_VSYNC_SUSPEND:
481         case FB_BLANK_HSYNC_SUSPEND:
482         case FB_BLANK_NORMAL:
483                 if (fbi->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR ||
484                     fbi->fb.fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
485                         for (i = 0; i < fbi->palette_size; i++)
486                                 pxafb_setpalettereg(i, 0, 0, 0, 0, info);
487
488                 pxafb_schedule_work(fbi, C_DISABLE);
489                 /* TODO if (pxafb_blank_helper) pxafb_blank_helper(blank); */
490                 break;
491
492         case FB_BLANK_UNBLANK:
493                 /* TODO if (pxafb_blank_helper) pxafb_blank_helper(blank); */
494                 if (fbi->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR ||
495                     fbi->fb.fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
496                         fb_set_cmap(&fbi->fb.cmap, info);
497                 pxafb_schedule_work(fbi, C_ENABLE);
498         }
499         return 0;
500 }
501
502 static int pxafb_mmap(struct fb_info *info,
503                       struct vm_area_struct *vma)
504 {
505         struct pxafb_info *fbi = (struct pxafb_info *)info;
506         unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
507
508         if (off < info->fix.smem_len) {
509                 vma->vm_pgoff += fbi->video_offset / PAGE_SIZE;
510                 return dma_mmap_writecombine(fbi->dev, vma, fbi->map_cpu,
511                                              fbi->map_dma, fbi->map_size);
512         }
513         return -EINVAL;
514 }
515
516 static struct fb_ops pxafb_ops = {
517         .owner          = THIS_MODULE,
518         .fb_check_var   = pxafb_check_var,
519         .fb_set_par     = pxafb_set_par,
520         .fb_setcolreg   = pxafb_setcolreg,
521         .fb_fillrect    = cfb_fillrect,
522         .fb_copyarea    = cfb_copyarea,
523         .fb_imageblit   = cfb_imageblit,
524         .fb_blank       = pxafb_blank,
525         .fb_mmap        = pxafb_mmap,
526 };
527
528 /*
529  * Calculate the PCD value from the clock rate (in picoseconds).
530  * We take account of the PPCR clock setting.
531  * From PXA Developer's Manual:
532  *
533  *   PixelClock =      LCLK
534  *                -------------
535  *                2 ( PCD + 1 )
536  *
537  *   PCD =      LCLK
538  *         ------------- - 1
539  *         2(PixelClock)
540  *
541  * Where:
542  *   LCLK = LCD/Memory Clock
543  *   PCD = LCCR3[7:0]
544  *
545  * PixelClock here is in Hz while the pixclock argument given is the
546  * period in picoseconds. Hence PixelClock = 1 / ( pixclock * 10^-12 )
547  *
548  * The function get_lclk_frequency_10khz returns LCLK in units of
549  * 10khz. Calling the result of this function lclk gives us the
550  * following
551  *
552  *    PCD = (lclk * 10^4 ) * ( pixclock * 10^-12 )
553  *          -------------------------------------- - 1
554  *                          2
555  *
556  * Factoring the 10^4 and 10^-12 out gives 10^-8 == 1 / 100000000 as used below.
557  */
558 static inline unsigned int get_pcd(struct pxafb_info *fbi,
559                                    unsigned int pixclock)
560 {
561         unsigned long long pcd;
562
563         /* FIXME: Need to take into account Double Pixel Clock mode
564          * (DPC) bit? or perhaps set it based on the various clock
565          * speeds */
566         pcd = (unsigned long long)(clk_get_rate(fbi->clk) / 10000);
567         pcd *= pixclock;
568         do_div(pcd, 100000000 * 2);
569         /* no need for this, since we should subtract 1 anyway. they cancel */
570         /* pcd += 1; */ /* make up for integer math truncations */
571         return (unsigned int)pcd;
572 }
573
574 /*
575  * Some touchscreens need hsync information from the video driver to
576  * function correctly. We export it here.  Note that 'hsync_time' and
577  * the value returned from pxafb_get_hsync_time() is the *reciprocal*
578  * of the hsync period in seconds.
579  */
580 static inline void set_hsync_time(struct pxafb_info *fbi, unsigned int pcd)
581 {
582         unsigned long htime;
583
584         if ((pcd == 0) || (fbi->fb.var.hsync_len == 0)) {
585                 fbi->hsync_time = 0;
586                 return;
587         }
588
589         htime = clk_get_rate(fbi->clk) / (pcd * fbi->fb.var.hsync_len);
590
591         fbi->hsync_time = htime;
592 }
593
594 unsigned long pxafb_get_hsync_time(struct device *dev)
595 {
596         struct pxafb_info *fbi = dev_get_drvdata(dev);
597
598         /* If display is blanked/suspended, hsync isn't active */
599         if (!fbi || (fbi->state != C_ENABLE))
600                 return 0;
601
602         return fbi->hsync_time;
603 }
604 EXPORT_SYMBOL(pxafb_get_hsync_time);
605
606 static int setup_frame_dma(struct pxafb_info *fbi, int dma, int pal,
607                 unsigned int offset, size_t size)
608 {
609         struct pxafb_dma_descriptor *dma_desc, *pal_desc;
610         unsigned int dma_desc_off, pal_desc_off;
611
612         if (dma < 0 || dma >= DMA_MAX)
613                 return -EINVAL;
614
615         dma_desc = &fbi->dma_buff->dma_desc[dma];
616         dma_desc_off = offsetof(struct pxafb_dma_buff, dma_desc[dma]);
617
618         dma_desc->fsadr = fbi->screen_dma + offset;
619         dma_desc->fidr  = 0;
620         dma_desc->ldcmd = size;
621
622         if (pal < 0 || pal >= PAL_MAX) {
623                 dma_desc->fdadr = fbi->dma_buff_phys + dma_desc_off;
624                 fbi->fdadr[dma] = fbi->dma_buff_phys + dma_desc_off;
625         } else {
626                 pal_desc = &fbi->dma_buff->pal_desc[pal];
627                 pal_desc_off = offsetof(struct pxafb_dma_buff, pal_desc[pal]);
628
629                 pal_desc->fsadr = fbi->dma_buff_phys + pal * PALETTE_SIZE;
630                 pal_desc->fidr  = 0;
631
632                 if ((fbi->lccr4 & LCCR4_PAL_FOR_MASK) == LCCR4_PAL_FOR_0)
633                         pal_desc->ldcmd = fbi->palette_size * sizeof(u16);
634                 else
635                         pal_desc->ldcmd = fbi->palette_size * sizeof(u32);
636
637                 pal_desc->ldcmd |= LDCMD_PAL;
638
639                 /* flip back and forth between palette and frame buffer */
640                 pal_desc->fdadr = fbi->dma_buff_phys + dma_desc_off;
641                 dma_desc->fdadr = fbi->dma_buff_phys + pal_desc_off;
642                 fbi->fdadr[dma] = fbi->dma_buff_phys + dma_desc_off;
643         }
644
645         return 0;
646 }
647
648 #ifdef CONFIG_FB_PXA_SMARTPANEL
649 static int setup_smart_dma(struct pxafb_info *fbi)
650 {
651         struct pxafb_dma_descriptor *dma_desc;
652         unsigned long dma_desc_off, cmd_buff_off;
653
654         dma_desc = &fbi->dma_buff->dma_desc[DMA_CMD];
655         dma_desc_off = offsetof(struct pxafb_dma_buff, dma_desc[DMA_CMD]);
656         cmd_buff_off = offsetof(struct pxafb_dma_buff, cmd_buff);
657
658         dma_desc->fdadr = fbi->dma_buff_phys + dma_desc_off;
659         dma_desc->fsadr = fbi->dma_buff_phys + cmd_buff_off;
660         dma_desc->fidr  = 0;
661         dma_desc->ldcmd = fbi->n_smart_cmds * sizeof(uint16_t);
662
663         fbi->fdadr[DMA_CMD] = dma_desc->fdadr;
664         return 0;
665 }
666
667 int pxafb_smart_flush(struct fb_info *info)
668 {
669         struct pxafb_info *fbi = container_of(info, struct pxafb_info, fb);
670         uint32_t prsr;
671         int ret = 0;
672
673         /* disable controller until all registers are set up */
674         lcd_writel(fbi, LCCR0, fbi->reg_lccr0 & ~LCCR0_ENB);
675
676         /* 1. make it an even number of commands to align on 32-bit boundary
677          * 2. add the interrupt command to the end of the chain so we can
678          *    keep track of the end of the transfer
679          */
680
681         while (fbi->n_smart_cmds & 1)
682                 fbi->smart_cmds[fbi->n_smart_cmds++] = SMART_CMD_NOOP;
683
684         fbi->smart_cmds[fbi->n_smart_cmds++] = SMART_CMD_INTERRUPT;
685         fbi->smart_cmds[fbi->n_smart_cmds++] = SMART_CMD_WAIT_FOR_VSYNC;
686         setup_smart_dma(fbi);
687
688         /* continue to execute next command */
689         prsr = lcd_readl(fbi, PRSR) | PRSR_ST_OK | PRSR_CON_NT;
690         lcd_writel(fbi, PRSR, prsr);
691
692         /* stop the processor in case it executed "wait for sync" cmd */
693         lcd_writel(fbi, CMDCR, 0x0001);
694
695         /* don't send interrupts for fifo underruns on channel 6 */
696         lcd_writel(fbi, LCCR5, LCCR5_IUM(6));
697
698         lcd_writel(fbi, LCCR1, fbi->reg_lccr1);
699         lcd_writel(fbi, LCCR2, fbi->reg_lccr2);
700         lcd_writel(fbi, LCCR3, fbi->reg_lccr3);
701         lcd_writel(fbi, FDADR0, fbi->fdadr[0]);
702         lcd_writel(fbi, FDADR6, fbi->fdadr[6]);
703
704         /* begin sending */
705         lcd_writel(fbi, LCCR0, fbi->reg_lccr0 | LCCR0_ENB);
706
707         if (wait_for_completion_timeout(&fbi->command_done, HZ/2) == 0) {
708                 pr_warning("%s: timeout waiting for command done\n",
709                                 __func__);
710                 ret = -ETIMEDOUT;
711         }
712
713         /* quick disable */
714         prsr = lcd_readl(fbi, PRSR) & ~(PRSR_ST_OK | PRSR_CON_NT);
715         lcd_writel(fbi, PRSR, prsr);
716         lcd_writel(fbi, LCCR0, fbi->reg_lccr0 & ~LCCR0_ENB);
717         lcd_writel(fbi, FDADR6, 0);
718         fbi->n_smart_cmds = 0;
719         return ret;
720 }
721
722 int pxafb_smart_queue(struct fb_info *info, uint16_t *cmds, int n_cmds)
723 {
724         int i;
725         struct pxafb_info *fbi = container_of(info, struct pxafb_info, fb);
726
727         /* leave 2 commands for INTERRUPT and WAIT_FOR_SYNC */
728         for (i = 0; i < n_cmds; i++) {
729                 if (fbi->n_smart_cmds == CMD_BUFF_SIZE - 8)
730                         pxafb_smart_flush(info);
731
732                 fbi->smart_cmds[fbi->n_smart_cmds++] = *cmds++;
733         }
734
735         return 0;
736 }
737
738 static unsigned int __smart_timing(unsigned time_ns, unsigned long lcd_clk)
739 {
740         unsigned int t = (time_ns * (lcd_clk / 1000000) / 1000);
741         return (t == 0) ? 1 : t;
742 }
743
744 static void setup_smart_timing(struct pxafb_info *fbi,
745                                 struct fb_var_screeninfo *var)
746 {
747         struct pxafb_mach_info *inf = fbi->dev->platform_data;
748         struct pxafb_mode_info *mode = &inf->modes[0];
749         unsigned long lclk = clk_get_rate(fbi->clk);
750         unsigned t1, t2, t3, t4;
751
752         t1 = max(mode->a0csrd_set_hld, mode->a0cswr_set_hld);
753         t2 = max(mode->rd_pulse_width, mode->wr_pulse_width);
754         t3 = mode->op_hold_time;
755         t4 = mode->cmd_inh_time;
756
757         fbi->reg_lccr1 =
758                 LCCR1_DisWdth(var->xres) |
759                 LCCR1_BegLnDel(__smart_timing(t1, lclk)) |
760                 LCCR1_EndLnDel(__smart_timing(t2, lclk)) |
761                 LCCR1_HorSnchWdth(__smart_timing(t3, lclk));
762
763         fbi->reg_lccr2 = LCCR2_DisHght(var->yres);
764         fbi->reg_lccr3 = LCCR3_PixClkDiv(__smart_timing(t4, lclk));
765
766         /* FIXME: make this configurable */
767         fbi->reg_cmdcr = 1;
768 }
769
770 static int pxafb_smart_thread(void *arg)
771 {
772         struct pxafb_info *fbi = arg;
773         struct pxafb_mach_info *inf = fbi->dev->platform_data;
774
775         if (!fbi || !inf->smart_update) {
776                 pr_err("%s: not properly initialized, thread terminated\n",
777                                 __func__);
778                 return -EINVAL;
779         }
780
781         pr_debug("%s(): task starting\n", __func__);
782
783         set_freezable();
784         while (!kthread_should_stop()) {
785
786                 if (try_to_freeze())
787                         continue;
788
789                 if (fbi->state == C_ENABLE) {
790                         inf->smart_update(&fbi->fb);
791                         complete(&fbi->refresh_done);
792                 }
793
794                 set_current_state(TASK_INTERRUPTIBLE);
795                 schedule_timeout(30 * HZ / 1000);
796         }
797
798         pr_debug("%s(): task ending\n", __func__);
799         return 0;
800 }
801
802 static int pxafb_smart_init(struct pxafb_info *fbi)
803 {
804         if (!(fbi->lccr0 | LCCR0_LCDT))
805                 return 0;
806
807         fbi->smart_thread = kthread_run(pxafb_smart_thread, fbi,
808                                         "lcd_refresh");
809         if (IS_ERR(fbi->smart_thread)) {
810                 printk(KERN_ERR "%s: unable to create kernel thread\n",
811                                 __func__);
812                 return PTR_ERR(fbi->smart_thread);
813         }
814
815         return 0;
816 }
817 #else
818 int pxafb_smart_queue(struct fb_info *info, uint16_t *cmds, int n_cmds)
819 {
820         return 0;
821 }
822
823 int pxafb_smart_flush(struct fb_info *info)
824 {
825         return 0;
826 }
827 #endif /* CONFIG_FB_SMART_PANEL */
828
829 static void setup_parallel_timing(struct pxafb_info *fbi,
830                                   struct fb_var_screeninfo *var)
831 {
832         unsigned int lines_per_panel, pcd = get_pcd(fbi, var->pixclock);
833
834         fbi->reg_lccr1 =
835                 LCCR1_DisWdth(var->xres) +
836                 LCCR1_HorSnchWdth(var->hsync_len) +
837                 LCCR1_BegLnDel(var->left_margin) +
838                 LCCR1_EndLnDel(var->right_margin);
839
840         /*
841          * If we have a dual scan LCD, we need to halve
842          * the YRES parameter.
843          */
844         lines_per_panel = var->yres;
845         if ((fbi->lccr0 & LCCR0_SDS) == LCCR0_Dual)
846                 lines_per_panel /= 2;
847
848         fbi->reg_lccr2 =
849                 LCCR2_DisHght(lines_per_panel) +
850                 LCCR2_VrtSnchWdth(var->vsync_len) +
851                 LCCR2_BegFrmDel(var->upper_margin) +
852                 LCCR2_EndFrmDel(var->lower_margin);
853
854         fbi->reg_lccr3 = fbi->lccr3 |
855                 (var->sync & FB_SYNC_HOR_HIGH_ACT ?
856                  LCCR3_HorSnchH : LCCR3_HorSnchL) |
857                 (var->sync & FB_SYNC_VERT_HIGH_ACT ?
858                  LCCR3_VrtSnchH : LCCR3_VrtSnchL);
859
860         if (pcd) {
861                 fbi->reg_lccr3 |= LCCR3_PixClkDiv(pcd);
862                 set_hsync_time(fbi, pcd);
863         }
864 }
865
866 /*
867  * pxafb_activate_var():
868  *      Configures LCD Controller based on entries in var parameter.
869  *      Settings are only written to the controller if changes were made.
870  */
871 static int pxafb_activate_var(struct fb_var_screeninfo *var,
872                               struct pxafb_info *fbi)
873 {
874         u_long flags;
875         size_t nbytes;
876
877 #if DEBUG_VAR
878         if (!(fbi->lccr0 & LCCR0_LCDT)) {
879                 if (var->xres < 16 || var->xres > 1024)
880                         printk(KERN_ERR "%s: invalid xres %d\n",
881                                 fbi->fb.fix.id, var->xres);
882                 switch (var->bits_per_pixel) {
883                 case 1:
884                 case 2:
885                 case 4:
886                 case 8:
887                 case 16:
888                 case 24:
889                 case 32:
890                         break;
891                 default:
892                         printk(KERN_ERR "%s: invalid bit depth %d\n",
893                                fbi->fb.fix.id, var->bits_per_pixel);
894                         break;
895                 }
896
897                 if (var->hsync_len < 1 || var->hsync_len > 64)
898                         printk(KERN_ERR "%s: invalid hsync_len %d\n",
899                                 fbi->fb.fix.id, var->hsync_len);
900                 if (var->left_margin < 1 || var->left_margin > 255)
901                         printk(KERN_ERR "%s: invalid left_margin %d\n",
902                                 fbi->fb.fix.id, var->left_margin);
903                 if (var->right_margin < 1 || var->right_margin > 255)
904                         printk(KERN_ERR "%s: invalid right_margin %d\n",
905                                 fbi->fb.fix.id, var->right_margin);
906                 if (var->yres < 1 || var->yres > 1024)
907                         printk(KERN_ERR "%s: invalid yres %d\n",
908                                 fbi->fb.fix.id, var->yres);
909                 if (var->vsync_len < 1 || var->vsync_len > 64)
910                         printk(KERN_ERR "%s: invalid vsync_len %d\n",
911                                 fbi->fb.fix.id, var->vsync_len);
912                 if (var->upper_margin < 0 || var->upper_margin > 255)
913                         printk(KERN_ERR "%s: invalid upper_margin %d\n",
914                                 fbi->fb.fix.id, var->upper_margin);
915                 if (var->lower_margin < 0 || var->lower_margin > 255)
916                         printk(KERN_ERR "%s: invalid lower_margin %d\n",
917                                 fbi->fb.fix.id, var->lower_margin);
918         }
919 #endif
920         /* Update shadow copy atomically */
921         local_irq_save(flags);
922
923 #ifdef CONFIG_FB_PXA_SMARTPANEL
924         if (fbi->lccr0 & LCCR0_LCDT)
925                 setup_smart_timing(fbi, var);
926         else
927 #endif
928                 setup_parallel_timing(fbi, var);
929
930         fbi->reg_lccr0 = fbi->lccr0 |
931                 (LCCR0_LDM | LCCR0_SFM | LCCR0_IUM | LCCR0_EFM |
932                  LCCR0_QDM | LCCR0_BM  | LCCR0_OUM);
933
934         fbi->reg_lccr3 |= pxafb_bpp_to_lccr3(var);
935
936         nbytes = var->yres * fbi->fb.fix.line_length;
937
938         if ((fbi->lccr0 & LCCR0_SDS) == LCCR0_Dual) {
939                 nbytes = nbytes / 2;
940                 setup_frame_dma(fbi, DMA_LOWER, PAL_NONE, nbytes, nbytes);
941         }
942
943         if ((var->bits_per_pixel >= 16) || (fbi->lccr0 & LCCR0_LCDT))
944                 setup_frame_dma(fbi, DMA_BASE, PAL_NONE, 0, nbytes);
945         else
946                 setup_frame_dma(fbi, DMA_BASE, PAL_BASE, 0, nbytes);
947
948         fbi->reg_lccr4 = lcd_readl(fbi, LCCR4) & ~LCCR4_PAL_FOR_MASK;
949         fbi->reg_lccr4 |= (fbi->lccr4 & LCCR4_PAL_FOR_MASK);
950         local_irq_restore(flags);
951
952         /*
953          * Only update the registers if the controller is enabled
954          * and something has changed.
955          */
956         if ((lcd_readl(fbi, LCCR0) != fbi->reg_lccr0) ||
957             (lcd_readl(fbi, LCCR1) != fbi->reg_lccr1) ||
958             (lcd_readl(fbi, LCCR2) != fbi->reg_lccr2) ||
959             (lcd_readl(fbi, LCCR3) != fbi->reg_lccr3) ||
960             (lcd_readl(fbi, FDADR0) != fbi->fdadr[0]) ||
961             (lcd_readl(fbi, FDADR1) != fbi->fdadr[1]))
962                 pxafb_schedule_work(fbi, C_REENABLE);
963
964         return 0;
965 }
966
967 /*
968  * NOTE!  The following functions are purely helpers for set_ctrlr_state.
969  * Do not call them directly; set_ctrlr_state does the correct serialisation
970  * to ensure that things happen in the right way 100% of time time.
971  *      -- rmk
972  */
973 static inline void __pxafb_backlight_power(struct pxafb_info *fbi, int on)
974 {
975         pr_debug("pxafb: backlight o%s\n", on ? "n" : "ff");
976
977         if (fbi->backlight_power)
978                 fbi->backlight_power(on);
979 }
980
981 static inline void __pxafb_lcd_power(struct pxafb_info *fbi, int on)
982 {
983         pr_debug("pxafb: LCD power o%s\n", on ? "n" : "ff");
984
985         if (fbi->lcd_power)
986                 fbi->lcd_power(on, &fbi->fb.var);
987 }
988
989 static void pxafb_setup_gpio(struct pxafb_info *fbi)
990 {
991         int gpio, ldd_bits;
992         unsigned int lccr0 = fbi->lccr0;
993
994         /*
995          * setup is based on type of panel supported
996          */
997
998         /* 4 bit interface */
999         if ((lccr0 & LCCR0_CMS) == LCCR0_Mono &&
1000             (lccr0 & LCCR0_SDS) == LCCR0_Sngl &&
1001             (lccr0 & LCCR0_DPD) == LCCR0_4PixMono)
1002                 ldd_bits = 4;
1003
1004         /* 8 bit interface */
1005         else if (((lccr0 & LCCR0_CMS) == LCCR0_Mono &&
1006                   ((lccr0 & LCCR0_SDS) == LCCR0_Dual ||
1007                    (lccr0 & LCCR0_DPD) == LCCR0_8PixMono)) ||
1008                  ((lccr0 & LCCR0_CMS) == LCCR0_Color &&
1009                   (lccr0 & LCCR0_PAS) == LCCR0_Pas &&
1010                   (lccr0 & LCCR0_SDS) == LCCR0_Sngl))
1011                 ldd_bits = 8;
1012
1013         /* 16 bit interface */
1014         else if ((lccr0 & LCCR0_CMS) == LCCR0_Color &&
1015                  ((lccr0 & LCCR0_SDS) == LCCR0_Dual ||
1016                   (lccr0 & LCCR0_PAS) == LCCR0_Act))
1017                 ldd_bits = 16;
1018
1019         else {
1020                 printk(KERN_ERR "pxafb_setup_gpio: unable to determine "
1021                                "bits per pixel\n");
1022                 return;
1023         }
1024
1025         for (gpio = 58; ldd_bits; gpio++, ldd_bits--)
1026                 pxa_gpio_mode(gpio | GPIO_ALT_FN_2_OUT);
1027         /* 18 bit interface */
1028         if (fbi->fb.var.bits_per_pixel > 16) {
1029                 pxa_gpio_mode(86 | GPIO_ALT_FN_2_OUT);
1030                 pxa_gpio_mode(87 | GPIO_ALT_FN_2_OUT);
1031         }
1032         pxa_gpio_mode(GPIO74_LCD_FCLK_MD);
1033         pxa_gpio_mode(GPIO75_LCD_LCLK_MD);
1034         pxa_gpio_mode(GPIO76_LCD_PCLK_MD);
1035
1036         if ((lccr0 & LCCR0_PAS) == 0)
1037                 pxa_gpio_mode(GPIO77_LCD_ACBIAS_MD);
1038 }
1039
1040 static void pxafb_enable_controller(struct pxafb_info *fbi)
1041 {
1042         pr_debug("pxafb: Enabling LCD controller\n");
1043         pr_debug("fdadr0 0x%08x\n", (unsigned int) fbi->fdadr[0]);
1044         pr_debug("fdadr1 0x%08x\n", (unsigned int) fbi->fdadr[1]);
1045         pr_debug("reg_lccr0 0x%08x\n", (unsigned int) fbi->reg_lccr0);
1046         pr_debug("reg_lccr1 0x%08x\n", (unsigned int) fbi->reg_lccr1);
1047         pr_debug("reg_lccr2 0x%08x\n", (unsigned int) fbi->reg_lccr2);
1048         pr_debug("reg_lccr3 0x%08x\n", (unsigned int) fbi->reg_lccr3);
1049
1050         /* enable LCD controller clock */
1051         clk_enable(fbi->clk);
1052
1053         if (fbi->lccr0 & LCCR0_LCDT)
1054                 return;
1055
1056         /* Sequence from 11.7.10 */
1057         lcd_writel(fbi, LCCR3, fbi->reg_lccr3);
1058         lcd_writel(fbi, LCCR2, fbi->reg_lccr2);
1059         lcd_writel(fbi, LCCR1, fbi->reg_lccr1);
1060         lcd_writel(fbi, LCCR0, fbi->reg_lccr0 & ~LCCR0_ENB);
1061
1062         lcd_writel(fbi, FDADR0, fbi->fdadr[0]);
1063         lcd_writel(fbi, FDADR1, fbi->fdadr[1]);
1064         lcd_writel(fbi, LCCR0, fbi->reg_lccr0 | LCCR0_ENB);
1065 }
1066
1067 static void pxafb_disable_controller(struct pxafb_info *fbi)
1068 {
1069         uint32_t lccr0;
1070
1071 #ifdef CONFIG_FB_PXA_SMARTPANEL
1072         if (fbi->lccr0 & LCCR0_LCDT) {
1073                 wait_for_completion_timeout(&fbi->refresh_done,
1074                                 200 * HZ / 1000);
1075                 return;
1076         }
1077 #endif
1078
1079         /* Clear LCD Status Register */
1080         lcd_writel(fbi, LCSR, 0xffffffff);
1081
1082         lccr0 = lcd_readl(fbi, LCCR0) & ~LCCR0_LDM;
1083         lcd_writel(fbi, LCCR0, lccr0);
1084         lcd_writel(fbi, LCCR0, lccr0 | LCCR0_DIS);
1085
1086         wait_for_completion_timeout(&fbi->disable_done, 200 * HZ / 1000);
1087
1088         /* disable LCD controller clock */
1089         clk_disable(fbi->clk);
1090 }
1091
1092 /*
1093  *  pxafb_handle_irq: Handle 'LCD DONE' interrupts.
1094  */
1095 static irqreturn_t pxafb_handle_irq(int irq, void *dev_id)
1096 {
1097         struct pxafb_info *fbi = dev_id;
1098         unsigned int lccr0, lcsr = lcd_readl(fbi, LCSR);
1099
1100         if (lcsr & LCSR_LDD) {
1101                 lccr0 = lcd_readl(fbi, LCCR0);
1102                 lcd_writel(fbi, LCCR0, lccr0 | LCCR0_LDM);
1103                 complete(&fbi->disable_done);
1104         }
1105
1106 #ifdef CONFIG_FB_PXA_SMARTPANEL
1107         if (lcsr & LCSR_CMD_INT)
1108                 complete(&fbi->command_done);
1109 #endif
1110
1111         lcd_writel(fbi, LCSR, lcsr);
1112         return IRQ_HANDLED;
1113 }
1114
1115 /*
1116  * This function must be called from task context only, since it will
1117  * sleep when disabling the LCD controller, or if we get two contending
1118  * processes trying to alter state.
1119  */
1120 static void set_ctrlr_state(struct pxafb_info *fbi, u_int state)
1121 {
1122         u_int old_state;
1123
1124         mutex_lock(&fbi->ctrlr_lock);
1125
1126         old_state = fbi->state;
1127
1128         /*
1129          * Hack around fbcon initialisation.
1130          */
1131         if (old_state == C_STARTUP && state == C_REENABLE)
1132                 state = C_ENABLE;
1133
1134         switch (state) {
1135         case C_DISABLE_CLKCHANGE:
1136                 /*
1137                  * Disable controller for clock change.  If the
1138                  * controller is already disabled, then do nothing.
1139                  */
1140                 if (old_state != C_DISABLE && old_state != C_DISABLE_PM) {
1141                         fbi->state = state;
1142                         /* TODO __pxafb_lcd_power(fbi, 0); */
1143                         pxafb_disable_controller(fbi);
1144                 }
1145                 break;
1146
1147         case C_DISABLE_PM:
1148         case C_DISABLE:
1149                 /*
1150                  * Disable controller
1151                  */
1152                 if (old_state != C_DISABLE) {
1153                         fbi->state = state;
1154                         __pxafb_backlight_power(fbi, 0);
1155                         __pxafb_lcd_power(fbi, 0);
1156                         if (old_state != C_DISABLE_CLKCHANGE)
1157                                 pxafb_disable_controller(fbi);
1158                 }
1159                 break;
1160
1161         case C_ENABLE_CLKCHANGE:
1162                 /*
1163                  * Enable the controller after clock change.  Only
1164                  * do this if we were disabled for the clock change.
1165                  */
1166                 if (old_state == C_DISABLE_CLKCHANGE) {
1167                         fbi->state = C_ENABLE;
1168                         pxafb_enable_controller(fbi);
1169                         /* TODO __pxafb_lcd_power(fbi, 1); */
1170                 }
1171                 break;
1172
1173         case C_REENABLE:
1174                 /*
1175                  * Re-enable the controller only if it was already
1176                  * enabled.  This is so we reprogram the control
1177                  * registers.
1178                  */
1179                 if (old_state == C_ENABLE) {
1180                         __pxafb_lcd_power(fbi, 0);
1181                         pxafb_disable_controller(fbi);
1182                         pxafb_setup_gpio(fbi);
1183                         pxafb_enable_controller(fbi);
1184                         __pxafb_lcd_power(fbi, 1);
1185                 }
1186                 break;
1187
1188         case C_ENABLE_PM:
1189                 /*
1190                  * Re-enable the controller after PM.  This is not
1191                  * perfect - think about the case where we were doing
1192                  * a clock change, and we suspended half-way through.
1193                  */
1194                 if (old_state != C_DISABLE_PM)
1195                         break;
1196                 /* fall through */
1197
1198         case C_ENABLE:
1199                 /*
1200                  * Power up the LCD screen, enable controller, and
1201                  * turn on the backlight.
1202                  */
1203                 if (old_state != C_ENABLE) {
1204                         fbi->state = C_ENABLE;
1205                         pxafb_setup_gpio(fbi);
1206                         pxafb_enable_controller(fbi);
1207                         __pxafb_lcd_power(fbi, 1);
1208                         __pxafb_backlight_power(fbi, 1);
1209                 }
1210                 break;
1211         }
1212         mutex_unlock(&fbi->ctrlr_lock);
1213 }
1214
1215 /*
1216  * Our LCD controller task (which is called when we blank or unblank)
1217  * via keventd.
1218  */
1219 static void pxafb_task(struct work_struct *work)
1220 {
1221         struct pxafb_info *fbi =
1222                 container_of(work, struct pxafb_info, task);
1223         u_int state = xchg(&fbi->task_state, -1);
1224
1225         set_ctrlr_state(fbi, state);
1226 }
1227
1228 #ifdef CONFIG_CPU_FREQ
1229 /*
1230  * CPU clock speed change handler.  We need to adjust the LCD timing
1231  * parameters when the CPU clock is adjusted by the power management
1232  * subsystem.
1233  *
1234  * TODO: Determine why f->new != 10*get_lclk_frequency_10khz()
1235  */
1236 static int
1237 pxafb_freq_transition(struct notifier_block *nb, unsigned long val, void *data)
1238 {
1239         struct pxafb_info *fbi = TO_INF(nb, freq_transition);
1240         /* TODO struct cpufreq_freqs *f = data; */
1241         u_int pcd;
1242
1243         switch (val) {
1244         case CPUFREQ_PRECHANGE:
1245                 set_ctrlr_state(fbi, C_DISABLE_CLKCHANGE);
1246                 break;
1247
1248         case CPUFREQ_POSTCHANGE:
1249                 pcd = get_pcd(fbi, fbi->fb.var.pixclock);
1250                 set_hsync_time(fbi, pcd);
1251                 fbi->reg_lccr3 = (fbi->reg_lccr3 & ~0xff) |
1252                                   LCCR3_PixClkDiv(pcd);
1253                 set_ctrlr_state(fbi, C_ENABLE_CLKCHANGE);
1254                 break;
1255         }
1256         return 0;
1257 }
1258
1259 static int
1260 pxafb_freq_policy(struct notifier_block *nb, unsigned long val, void *data)
1261 {
1262         struct pxafb_info *fbi = TO_INF(nb, freq_policy);
1263         struct fb_var_screeninfo *var = &fbi->fb.var;
1264         struct cpufreq_policy *policy = data;
1265
1266         switch (val) {
1267         case CPUFREQ_ADJUST:
1268         case CPUFREQ_INCOMPATIBLE:
1269                 pr_debug("min dma period: %d ps, "
1270                         "new clock %d kHz\n", pxafb_display_dma_period(var),
1271                         policy->max);
1272                 /* TODO: fill in min/max values */
1273                 break;
1274         }
1275         return 0;
1276 }
1277 #endif
1278
1279 #ifdef CONFIG_PM
1280 /*
1281  * Power management hooks.  Note that we won't be called from IRQ context,
1282  * unlike the blank functions above, so we may sleep.
1283  */
1284 static int pxafb_suspend(struct platform_device *dev, pm_message_t state)
1285 {
1286         struct pxafb_info *fbi = platform_get_drvdata(dev);
1287
1288         set_ctrlr_state(fbi, C_DISABLE_PM);
1289         return 0;
1290 }
1291
1292 static int pxafb_resume(struct platform_device *dev)
1293 {
1294         struct pxafb_info *fbi = platform_get_drvdata(dev);
1295
1296         set_ctrlr_state(fbi, C_ENABLE_PM);
1297         return 0;
1298 }
1299 #else
1300 #define pxafb_suspend   NULL
1301 #define pxafb_resume    NULL
1302 #endif
1303
1304 /*
1305  * pxafb_map_video_memory():
1306  *      Allocates the DRAM memory for the frame buffer.  This buffer is
1307  *      remapped into a non-cached, non-buffered, memory region to
1308  *      allow palette and pixel writes to occur without flushing the
1309  *      cache.  Once this area is remapped, all virtual memory
1310  *      access to the video memory should occur at the new region.
1311  */
1312 static int __devinit pxafb_map_video_memory(struct pxafb_info *fbi)
1313 {
1314         /*
1315          * We reserve one page for the palette, plus the size
1316          * of the framebuffer.
1317          */
1318         fbi->video_offset = PAGE_ALIGN(sizeof(struct pxafb_dma_buff));
1319         fbi->map_size = PAGE_ALIGN(fbi->fb.fix.smem_len + fbi->video_offset);
1320         fbi->map_cpu = dma_alloc_writecombine(fbi->dev, fbi->map_size,
1321                                               &fbi->map_dma, GFP_KERNEL);
1322
1323         if (fbi->map_cpu) {
1324                 /* prevent initial garbage on screen */
1325                 memset(fbi->map_cpu, 0, fbi->map_size);
1326                 fbi->fb.screen_base = fbi->map_cpu + fbi->video_offset;
1327                 fbi->screen_dma = fbi->map_dma + fbi->video_offset;
1328
1329                 /*
1330                  * FIXME: this is actually the wrong thing to place in
1331                  * smem_start.  But fbdev suffers from the problem that
1332                  * it needs an API which doesn't exist (in this case,
1333                  * dma_writecombine_mmap)
1334                  */
1335                 fbi->fb.fix.smem_start = fbi->screen_dma;
1336                 fbi->palette_size = fbi->fb.var.bits_per_pixel == 8 ? 256 : 16;
1337
1338                 fbi->dma_buff = (void *) fbi->map_cpu;
1339                 fbi->dma_buff_phys = fbi->map_dma;
1340                 fbi->palette_cpu = (u16 *) fbi->dma_buff->palette;
1341
1342                 pr_debug("pxafb: palette_mem_size = 0x%08x\n", fbi->palette_size*sizeof(u16));
1343
1344 #ifdef CONFIG_FB_PXA_SMARTPANEL
1345                 fbi->smart_cmds = (uint16_t *) fbi->dma_buff->cmd_buff;
1346                 fbi->n_smart_cmds = 0;
1347 #endif
1348         }
1349
1350         return fbi->map_cpu ? 0 : -ENOMEM;
1351 }
1352
1353 static void pxafb_decode_mode_info(struct pxafb_info *fbi,
1354                                    struct pxafb_mode_info *modes,
1355                                    unsigned int num_modes)
1356 {
1357         unsigned int i, smemlen;
1358
1359         pxafb_setmode(&fbi->fb.var, &modes[0]);
1360
1361         for (i = 0; i < num_modes; i++) {
1362                 smemlen = modes[i].xres * modes[i].yres * modes[i].bpp / 8;
1363                 if (smemlen > fbi->fb.fix.smem_len)
1364                         fbi->fb.fix.smem_len = smemlen;
1365         }
1366 }
1367
1368 static void pxafb_decode_mach_info(struct pxafb_info *fbi,
1369                                    struct pxafb_mach_info *inf)
1370 {
1371         unsigned int lcd_conn = inf->lcd_conn;
1372
1373         fbi->cmap_inverse       = inf->cmap_inverse;
1374         fbi->cmap_static        = inf->cmap_static;
1375
1376         switch (lcd_conn & LCD_TYPE_MASK) {
1377         case LCD_TYPE_MONO_STN:
1378                 fbi->lccr0 = LCCR0_CMS;
1379                 break;
1380         case LCD_TYPE_MONO_DSTN:
1381                 fbi->lccr0 = LCCR0_CMS | LCCR0_SDS;
1382                 break;
1383         case LCD_TYPE_COLOR_STN:
1384                 fbi->lccr0 = 0;
1385                 break;
1386         case LCD_TYPE_COLOR_DSTN:
1387                 fbi->lccr0 = LCCR0_SDS;
1388                 break;
1389         case LCD_TYPE_COLOR_TFT:
1390                 fbi->lccr0 = LCCR0_PAS;
1391                 break;
1392         case LCD_TYPE_SMART_PANEL:
1393                 fbi->lccr0 = LCCR0_LCDT | LCCR0_PAS;
1394                 break;
1395         default:
1396                 /* fall back to backward compatibility way */
1397                 fbi->lccr0 = inf->lccr0;
1398                 fbi->lccr3 = inf->lccr3;
1399                 fbi->lccr4 = inf->lccr4;
1400                 goto decode_mode;
1401         }
1402
1403         if (lcd_conn == LCD_MONO_STN_8BPP)
1404                 fbi->lccr0 |= LCCR0_DPD;
1405
1406         fbi->lccr0 |= (lcd_conn & LCD_ALTERNATE_MAPPING) ? LCCR0_LDDALT : 0;
1407
1408         fbi->lccr3 = LCCR3_Acb((inf->lcd_conn >> 10) & 0xff);
1409         fbi->lccr3 |= (lcd_conn & LCD_BIAS_ACTIVE_LOW) ? LCCR3_OEP : 0;
1410         fbi->lccr3 |= (lcd_conn & LCD_PCLK_EDGE_FALL)  ? LCCR3_PCP : 0;
1411
1412 decode_mode:
1413         pxafb_decode_mode_info(fbi, inf->modes, inf->num_modes);
1414 }
1415
1416 static struct pxafb_info * __devinit pxafb_init_fbinfo(struct device *dev)
1417 {
1418         struct pxafb_info *fbi;
1419         void *addr;
1420         struct pxafb_mach_info *inf = dev->platform_data;
1421
1422         /* Alloc the pxafb_info and pseudo_palette in one step */
1423         fbi = kmalloc(sizeof(struct pxafb_info) + sizeof(u32) * 16, GFP_KERNEL);
1424         if (!fbi)
1425                 return NULL;
1426
1427         memset(fbi, 0, sizeof(struct pxafb_info));
1428         fbi->dev = dev;
1429
1430         fbi->clk = clk_get(dev, NULL);
1431         if (IS_ERR(fbi->clk)) {
1432                 kfree(fbi);
1433                 return NULL;
1434         }
1435
1436         strcpy(fbi->fb.fix.id, PXA_NAME);
1437
1438         fbi->fb.fix.type        = FB_TYPE_PACKED_PIXELS;
1439         fbi->fb.fix.type_aux    = 0;
1440         fbi->fb.fix.xpanstep    = 0;
1441         fbi->fb.fix.ypanstep    = 0;
1442         fbi->fb.fix.ywrapstep   = 0;
1443         fbi->fb.fix.accel       = FB_ACCEL_NONE;
1444
1445         fbi->fb.var.nonstd      = 0;
1446         fbi->fb.var.activate    = FB_ACTIVATE_NOW;
1447         fbi->fb.var.height      = -1;
1448         fbi->fb.var.width       = -1;
1449         fbi->fb.var.accel_flags = 0;
1450         fbi->fb.var.vmode       = FB_VMODE_NONINTERLACED;
1451
1452         fbi->fb.fbops           = &pxafb_ops;
1453         fbi->fb.flags           = FBINFO_DEFAULT;
1454         fbi->fb.node            = -1;
1455
1456         addr = fbi;
1457         addr = addr + sizeof(struct pxafb_info);
1458         fbi->fb.pseudo_palette  = addr;
1459
1460         fbi->state              = C_STARTUP;
1461         fbi->task_state         = (u_char)-1;
1462
1463         pxafb_decode_mach_info(fbi, inf);
1464
1465         init_waitqueue_head(&fbi->ctrlr_wait);
1466         INIT_WORK(&fbi->task, pxafb_task);
1467         mutex_init(&fbi->ctrlr_lock);
1468         init_completion(&fbi->disable_done);
1469 #ifdef CONFIG_FB_PXA_SMARTPANEL
1470         init_completion(&fbi->command_done);
1471         init_completion(&fbi->refresh_done);
1472 #endif
1473
1474         return fbi;
1475 }
1476
1477 #ifdef CONFIG_FB_PXA_PARAMETERS
1478 static int __devinit parse_opt_mode(struct device *dev, const char *this_opt)
1479 {
1480         struct pxafb_mach_info *inf = dev->platform_data;
1481
1482         const char *name = this_opt+5;
1483         unsigned int namelen = strlen(name);
1484         int res_specified = 0, bpp_specified = 0;
1485         unsigned int xres = 0, yres = 0, bpp = 0;
1486         int yres_specified = 0;
1487         int i;
1488         for (i = namelen-1; i >= 0; i--) {
1489                 switch (name[i]) {
1490                 case '-':
1491                         namelen = i;
1492                         if (!bpp_specified && !yres_specified) {
1493                                 bpp = simple_strtoul(&name[i+1], NULL, 0);
1494                                 bpp_specified = 1;
1495                         } else
1496                                 goto done;
1497                         break;
1498                 case 'x':
1499                         if (!yres_specified) {
1500                                 yres = simple_strtoul(&name[i+1], NULL, 0);
1501                                 yres_specified = 1;
1502                         } else
1503                                 goto done;
1504                         break;
1505                 case '0' ... '9':
1506                         break;
1507                 default:
1508                         goto done;
1509                 }
1510         }
1511         if (i < 0 && yres_specified) {
1512                 xres = simple_strtoul(name, NULL, 0);
1513                 res_specified = 1;
1514         }
1515 done:
1516         if (res_specified) {
1517                 dev_info(dev, "overriding resolution: %dx%d\n", xres, yres);
1518                 inf->modes[0].xres = xres; inf->modes[0].yres = yres;
1519         }
1520         if (bpp_specified)
1521                 switch (bpp) {
1522                 case 1:
1523                 case 2:
1524                 case 4:
1525                 case 8:
1526                 case 16:
1527                         inf->modes[0].bpp = bpp;
1528                         dev_info(dev, "overriding bit depth: %d\n", bpp);
1529                         break;
1530                 default:
1531                         dev_err(dev, "Depth %d is not valid\n", bpp);
1532                         return -EINVAL;
1533                 }
1534         return 0;
1535 }
1536
1537 static int __devinit parse_opt(struct device *dev, char *this_opt)
1538 {
1539         struct pxafb_mach_info *inf = dev->platform_data;
1540         struct pxafb_mode_info *mode = &inf->modes[0];
1541         char s[64];
1542
1543         s[0] = '\0';
1544
1545         if (!strncmp(this_opt, "mode:", 5)) {
1546                 return parse_opt_mode(dev, this_opt);
1547         } else if (!strncmp(this_opt, "pixclock:", 9)) {
1548                 mode->pixclock = simple_strtoul(this_opt+9, NULL, 0);
1549                 sprintf(s, "pixclock: %ld\n", mode->pixclock);
1550         } else if (!strncmp(this_opt, "left:", 5)) {
1551                 mode->left_margin = simple_strtoul(this_opt+5, NULL, 0);
1552                 sprintf(s, "left: %u\n", mode->left_margin);
1553         } else if (!strncmp(this_opt, "right:", 6)) {
1554                 mode->right_margin = simple_strtoul(this_opt+6, NULL, 0);
1555                 sprintf(s, "right: %u\n", mode->right_margin);
1556         } else if (!strncmp(this_opt, "upper:", 6)) {
1557                 mode->upper_margin = simple_strtoul(this_opt+6, NULL, 0);
1558                 sprintf(s, "upper: %u\n", mode->upper_margin);
1559         } else if (!strncmp(this_opt, "lower:", 6)) {
1560                 mode->lower_margin = simple_strtoul(this_opt+6, NULL, 0);
1561                 sprintf(s, "lower: %u\n", mode->lower_margin);
1562         } else if (!strncmp(this_opt, "hsynclen:", 9)) {
1563                 mode->hsync_len = simple_strtoul(this_opt+9, NULL, 0);
1564                 sprintf(s, "hsynclen: %u\n", mode->hsync_len);
1565         } else if (!strncmp(this_opt, "vsynclen:", 9)) {
1566                 mode->vsync_len = simple_strtoul(this_opt+9, NULL, 0);
1567                 sprintf(s, "vsynclen: %u\n", mode->vsync_len);
1568         } else if (!strncmp(this_opt, "hsync:", 6)) {
1569                 if (simple_strtoul(this_opt+6, NULL, 0) == 0) {
1570                         sprintf(s, "hsync: Active Low\n");
1571                         mode->sync &= ~FB_SYNC_HOR_HIGH_ACT;
1572                 } else {
1573                         sprintf(s, "hsync: Active High\n");
1574                         mode->sync |= FB_SYNC_HOR_HIGH_ACT;
1575                 }
1576         } else if (!strncmp(this_opt, "vsync:", 6)) {
1577                 if (simple_strtoul(this_opt+6, NULL, 0) == 0) {
1578                         sprintf(s, "vsync: Active Low\n");
1579                         mode->sync &= ~FB_SYNC_VERT_HIGH_ACT;
1580                 } else {
1581                         sprintf(s, "vsync: Active High\n");
1582                         mode->sync |= FB_SYNC_VERT_HIGH_ACT;
1583                 }
1584         } else if (!strncmp(this_opt, "dpc:", 4)) {
1585                 if (simple_strtoul(this_opt+4, NULL, 0) == 0) {
1586                         sprintf(s, "double pixel clock: false\n");
1587                         inf->lccr3 &= ~LCCR3_DPC;
1588                 } else {
1589                         sprintf(s, "double pixel clock: true\n");
1590                         inf->lccr3 |= LCCR3_DPC;
1591                 }
1592         } else if (!strncmp(this_opt, "outputen:", 9)) {
1593                 if (simple_strtoul(this_opt+9, NULL, 0) == 0) {
1594                         sprintf(s, "output enable: active low\n");
1595                         inf->lccr3 = (inf->lccr3 & ~LCCR3_OEP) | LCCR3_OutEnL;
1596                 } else {
1597                         sprintf(s, "output enable: active high\n");
1598                         inf->lccr3 = (inf->lccr3 & ~LCCR3_OEP) | LCCR3_OutEnH;
1599                 }
1600         } else if (!strncmp(this_opt, "pixclockpol:", 12)) {
1601                 if (simple_strtoul(this_opt+12, NULL, 0) == 0) {
1602                         sprintf(s, "pixel clock polarity: falling edge\n");
1603                         inf->lccr3 = (inf->lccr3 & ~LCCR3_PCP) | LCCR3_PixFlEdg;
1604                 } else {
1605                         sprintf(s, "pixel clock polarity: rising edge\n");
1606                         inf->lccr3 = (inf->lccr3 & ~LCCR3_PCP) | LCCR3_PixRsEdg;
1607                 }
1608         } else if (!strncmp(this_opt, "color", 5)) {
1609                 inf->lccr0 = (inf->lccr0 & ~LCCR0_CMS) | LCCR0_Color;
1610         } else if (!strncmp(this_opt, "mono", 4)) {
1611                 inf->lccr0 = (inf->lccr0 & ~LCCR0_CMS) | LCCR0_Mono;
1612         } else if (!strncmp(this_opt, "active", 6)) {
1613                 inf->lccr0 = (inf->lccr0 & ~LCCR0_PAS) | LCCR0_Act;
1614         } else if (!strncmp(this_opt, "passive", 7)) {
1615                 inf->lccr0 = (inf->lccr0 & ~LCCR0_PAS) | LCCR0_Pas;
1616         } else if (!strncmp(this_opt, "single", 6)) {
1617                 inf->lccr0 = (inf->lccr0 & ~LCCR0_SDS) | LCCR0_Sngl;
1618         } else if (!strncmp(this_opt, "dual", 4)) {
1619                 inf->lccr0 = (inf->lccr0 & ~LCCR0_SDS) | LCCR0_Dual;
1620         } else if (!strncmp(this_opt, "4pix", 4)) {
1621                 inf->lccr0 = (inf->lccr0 & ~LCCR0_DPD) | LCCR0_4PixMono;
1622         } else if (!strncmp(this_opt, "8pix", 4)) {
1623                 inf->lccr0 = (inf->lccr0 & ~LCCR0_DPD) | LCCR0_8PixMono;
1624         } else {
1625                 dev_err(dev, "unknown option: %s\n", this_opt);
1626                 return -EINVAL;
1627         }
1628
1629         if (s[0] != '\0')
1630                 dev_info(dev, "override %s", s);
1631
1632         return 0;
1633 }
1634
1635 static int __devinit pxafb_parse_options(struct device *dev, char *options)
1636 {
1637         char *this_opt;
1638         int ret;
1639
1640         if (!options || !*options)
1641                 return 0;
1642
1643         dev_dbg(dev, "options are \"%s\"\n", options ? options : "null");
1644
1645         /* could be made table driven or similar?... */
1646         while ((this_opt = strsep(&options, ",")) != NULL) {
1647                 ret = parse_opt(dev, this_opt);
1648                 if (ret)
1649                         return ret;
1650         }
1651         return 0;
1652 }
1653
1654 static char g_options[256] __devinitdata = "";
1655
1656 #ifndef MODULE
1657 static int __init pxafb_setup_options(void)
1658 {
1659         char *options = NULL;
1660
1661         if (fb_get_options("pxafb", &options))
1662                 return -ENODEV;
1663
1664         if (options)
1665                 strlcpy(g_options, options, sizeof(g_options));
1666
1667         return 0;
1668 }
1669 #else
1670 #define pxafb_setup_options()           (0)
1671
1672 module_param_string(options, g_options, sizeof(g_options), 0);
1673 MODULE_PARM_DESC(options, "LCD parameters (see Documentation/fb/pxafb.txt)");
1674 #endif
1675
1676 #else
1677 #define pxafb_parse_options(...)        (0)
1678 #define pxafb_setup_options()           (0)
1679 #endif
1680
1681 #ifdef DEBUG_VAR
1682 /* Check for various illegal bit-combinations. Currently only
1683  * a warning is given. */
1684 static void __devinit pxafb_check_options(struct device *dev,
1685                                           struct pxafb_mach_info *inf)
1686 {
1687         if (inf->lcd_conn)
1688                 return;
1689
1690         if (inf->lccr0 & LCCR0_INVALID_CONFIG_MASK)
1691                 dev_warn(dev, "machine LCCR0 setting contains "
1692                                 "illegal bits: %08x\n",
1693                         inf->lccr0 & LCCR0_INVALID_CONFIG_MASK);
1694         if (inf->lccr3 & LCCR3_INVALID_CONFIG_MASK)
1695                 dev_warn(dev, "machine LCCR3 setting contains "
1696                                 "illegal bits: %08x\n",
1697                         inf->lccr3 & LCCR3_INVALID_CONFIG_MASK);
1698         if (inf->lccr0 & LCCR0_DPD &&
1699             ((inf->lccr0 & LCCR0_PAS) != LCCR0_Pas ||
1700              (inf->lccr0 & LCCR0_SDS) != LCCR0_Sngl ||
1701              (inf->lccr0 & LCCR0_CMS) != LCCR0_Mono))
1702                 dev_warn(dev, "Double Pixel Data (DPD) mode is "
1703                                 "only valid in passive mono"
1704                                 " single panel mode\n");
1705         if ((inf->lccr0 & LCCR0_PAS) == LCCR0_Act &&
1706             (inf->lccr0 & LCCR0_SDS) == LCCR0_Dual)
1707                 dev_warn(dev, "Dual panel only valid in passive mode\n");
1708         if ((inf->lccr0 & LCCR0_PAS) == LCCR0_Pas &&
1709              (inf->modes->upper_margin || inf->modes->lower_margin))
1710                 dev_warn(dev, "Upper and lower margins must be 0 in "
1711                                 "passive mode\n");
1712 }
1713 #else
1714 #define pxafb_check_options(...)        do {} while (0)
1715 #endif
1716
1717 static int __devinit pxafb_probe(struct platform_device *dev)
1718 {
1719         struct pxafb_info *fbi;
1720         struct pxafb_mach_info *inf;
1721         struct resource *r;
1722         int irq, ret;
1723
1724         dev_dbg(&dev->dev, "pxafb_probe\n");
1725
1726         inf = dev->dev.platform_data;
1727         ret = -ENOMEM;
1728         fbi = NULL;
1729         if (!inf)
1730                 goto failed;
1731
1732         ret = pxafb_parse_options(&dev->dev, g_options);
1733         if (ret < 0)
1734                 goto failed;
1735
1736         pxafb_check_options(&dev->dev, inf);
1737
1738         dev_dbg(&dev->dev, "got a %dx%dx%d LCD\n",
1739                         inf->modes->xres,
1740                         inf->modes->yres,
1741                         inf->modes->bpp);
1742         if (inf->modes->xres == 0 ||
1743             inf->modes->yres == 0 ||
1744             inf->modes->bpp == 0) {
1745                 dev_err(&dev->dev, "Invalid resolution or bit depth\n");
1746                 ret = -EINVAL;
1747                 goto failed;
1748         }
1749
1750         fbi = pxafb_init_fbinfo(&dev->dev);
1751         if (!fbi) {
1752                 /* only reason for pxafb_init_fbinfo to fail is kmalloc */
1753                 dev_err(&dev->dev, "Failed to initialize framebuffer device\n");
1754                 ret = -ENOMEM;
1755                 goto failed;
1756         }
1757
1758         fbi->backlight_power = inf->pxafb_backlight_power;
1759         fbi->lcd_power = inf->pxafb_lcd_power;
1760
1761         r = platform_get_resource(dev, IORESOURCE_MEM, 0);
1762         if (r == NULL) {
1763                 dev_err(&dev->dev, "no I/O memory resource defined\n");
1764                 ret = -ENODEV;
1765                 goto failed_fbi;
1766         }
1767
1768         r = request_mem_region(r->start, r->end - r->start + 1, dev->name);
1769         if (r == NULL) {
1770                 dev_err(&dev->dev, "failed to request I/O memory\n");
1771                 ret = -EBUSY;
1772                 goto failed_fbi;
1773         }
1774
1775         fbi->mmio_base = ioremap(r->start, r->end - r->start + 1);
1776         if (fbi->mmio_base == NULL) {
1777                 dev_err(&dev->dev, "failed to map I/O memory\n");
1778                 ret = -EBUSY;
1779                 goto failed_free_res;
1780         }
1781
1782         /* Initialize video memory */
1783         ret = pxafb_map_video_memory(fbi);
1784         if (ret) {
1785                 dev_err(&dev->dev, "Failed to allocate video RAM: %d\n", ret);
1786                 ret = -ENOMEM;
1787                 goto failed_free_io;
1788         }
1789
1790         irq = platform_get_irq(dev, 0);
1791         if (irq < 0) {
1792                 dev_err(&dev->dev, "no IRQ defined\n");
1793                 ret = -ENODEV;
1794                 goto failed_free_mem;
1795         }
1796
1797         ret = request_irq(irq, pxafb_handle_irq, IRQF_DISABLED, "LCD", fbi);
1798         if (ret) {
1799                 dev_err(&dev->dev, "request_irq failed: %d\n", ret);
1800                 ret = -EBUSY;
1801                 goto failed_free_mem;
1802         }
1803
1804 #ifdef CONFIG_FB_PXA_SMARTPANEL
1805         ret = pxafb_smart_init(fbi);
1806         if (ret) {
1807                 dev_err(&dev->dev, "failed to initialize smartpanel\n");
1808                 goto failed_free_irq;
1809         }
1810 #endif
1811         /*
1812          * This makes sure that our colour bitfield
1813          * descriptors are correctly initialised.
1814          */
1815         ret = pxafb_check_var(&fbi->fb.var, &fbi->fb);
1816         if (ret) {
1817                 dev_err(&dev->dev, "failed to get suitable mode\n");
1818                 goto failed_free_irq;
1819         }
1820
1821         ret = pxafb_set_par(&fbi->fb);
1822         if (ret) {
1823                 dev_err(&dev->dev, "Failed to set parameters\n");
1824                 goto failed_free_irq;
1825         }
1826
1827         platform_set_drvdata(dev, fbi);
1828
1829         ret = register_framebuffer(&fbi->fb);
1830         if (ret < 0) {
1831                 dev_err(&dev->dev,
1832                         "Failed to register framebuffer device: %d\n", ret);
1833                 goto failed_free_cmap;
1834         }
1835
1836 #ifdef CONFIG_CPU_FREQ
1837         fbi->freq_transition.notifier_call = pxafb_freq_transition;
1838         fbi->freq_policy.notifier_call = pxafb_freq_policy;
1839         cpufreq_register_notifier(&fbi->freq_transition,
1840                                 CPUFREQ_TRANSITION_NOTIFIER);
1841         cpufreq_register_notifier(&fbi->freq_policy,
1842                                 CPUFREQ_POLICY_NOTIFIER);
1843 #endif
1844
1845         /*
1846          * Ok, now enable the LCD controller
1847          */
1848         set_ctrlr_state(fbi, C_ENABLE);
1849
1850         return 0;
1851
1852 failed_free_cmap:
1853         if (fbi->fb.cmap.len)
1854                 fb_dealloc_cmap(&fbi->fb.cmap);
1855 failed_free_irq:
1856         free_irq(irq, fbi);
1857 failed_free_mem:
1858         dma_free_writecombine(&dev->dev, fbi->map_size,
1859                         fbi->map_cpu, fbi->map_dma);
1860 failed_free_io:
1861         iounmap(fbi->mmio_base);
1862 failed_free_res:
1863         release_mem_region(r->start, r->end - r->start + 1);
1864 failed_fbi:
1865         clk_put(fbi->clk);
1866         platform_set_drvdata(dev, NULL);
1867         kfree(fbi);
1868 failed:
1869         return ret;
1870 }
1871
1872 static int __devexit pxafb_remove(struct platform_device *dev)
1873 {
1874         struct pxafb_info *fbi = platform_get_drvdata(dev);
1875         struct resource *r;
1876         int irq;
1877         struct fb_info *info;
1878
1879         if (!fbi)
1880                 return 0;
1881
1882         info = &fbi->fb;
1883
1884         unregister_framebuffer(info);
1885
1886         pxafb_disable_controller(fbi);
1887
1888         if (fbi->fb.cmap.len)
1889                 fb_dealloc_cmap(&fbi->fb.cmap);
1890
1891         irq = platform_get_irq(dev, 0);
1892         free_irq(irq, fbi);
1893
1894         dma_free_writecombine(&dev->dev, fbi->map_size,
1895                                         fbi->map_cpu, fbi->map_dma);
1896
1897         iounmap(fbi->mmio_base);
1898
1899         r = platform_get_resource(dev, IORESOURCE_MEM, 0);
1900         release_mem_region(r->start, r->end - r->start + 1);
1901
1902         clk_put(fbi->clk);
1903         kfree(fbi);
1904
1905         return 0;
1906 }
1907
1908 static struct platform_driver pxafb_driver = {
1909         .probe          = pxafb_probe,
1910         .remove         = pxafb_remove,
1911         .suspend        = pxafb_suspend,
1912         .resume         = pxafb_resume,
1913         .driver         = {
1914                 .owner  = THIS_MODULE,
1915                 .name   = "pxa2xx-fb",
1916         },
1917 };
1918
1919 static int __init pxafb_init(void)
1920 {
1921         if (pxafb_setup_options())
1922                 return -EINVAL;
1923
1924         return platform_driver_register(&pxafb_driver);
1925 }
1926
1927 static void __exit pxafb_exit(void)
1928 {
1929         platform_driver_unregister(&pxafb_driver);
1930 }
1931
1932 module_init(pxafb_init);
1933 module_exit(pxafb_exit);
1934
1935 MODULE_DESCRIPTION("loadable framebuffer driver for PXA");
1936 MODULE_LICENSE("GPL");