blob: d11ea78a06d59b97b9116bc86b201306672ee9e0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#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>
Russell Kingd052d1b2005-10-29 19:07:23 +010038#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/dma-mapping.h>
Russell King72e35242007-08-20 10:18:42 +010040#include <linux/clk.h>
41#include <linux/err.h>
Eric Miao2ba162b2008-04-30 00:52:24 -070042#include <linux/completion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#include <asm/hardware.h>
45#include <asm/io.h>
46#include <asm/irq.h>
Nicolas Pitrebf1b8ab2005-06-23 21:56:45 +010047#include <asm/div64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/arch/pxa-regs.h>
eric miaoa683b142008-03-03 09:44:25 +080049#include <asm/arch/pxa2xx-gpio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <asm/arch/bitfield.h>
51#include <asm/arch/pxafb.h>
52
53/*
54 * Complain if VAR is out of range.
55 */
56#define DEBUG_VAR 1
57
58#include "pxafb.h"
59
60/* Bits which should not be set in machine configuration structures */
eric miaob0086ef2008-04-30 00:52:19 -070061#define LCCR0_INVALID_CONFIG_MASK (LCCR0_OUM | LCCR0_BM | LCCR0_QDM |\
62 LCCR0_DIS | LCCR0_EFM | LCCR0_IUM |\
63 LCCR0_SFM | LCCR0_LDM | LCCR0_ENB)
64
65#define LCCR3_INVALID_CONFIG_MASK (LCCR3_HSP | LCCR3_VSP |\
66 LCCR3_PCD | LCCR3_BPP)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68static void (*pxafb_backlight_power)(int);
Richard Purdied14b2722006-09-20 22:54:21 +010069static void (*pxafb_lcd_power)(int, struct fb_var_screeninfo *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
eric miaob0086ef2008-04-30 00:52:19 -070071static int pxafb_activate_var(struct fb_var_screeninfo *var,
72 struct pxafb_info *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static void set_ctrlr_state(struct pxafb_info *fbi, u_int state);
74
Eric Miaoa7535ba2008-04-30 00:52:24 -070075static inline unsigned long
76lcd_readl(struct pxafb_info *fbi, unsigned int off)
77{
78 return __raw_readl(fbi->mmio_base + off);
79}
80
81static inline void
82lcd_writel(struct pxafb_info *fbi, unsigned int off, unsigned long val)
83{
84 __raw_writel(val, fbi->mmio_base + off);
85}
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087static inline void pxafb_schedule_work(struct pxafb_info *fbi, u_int state)
88{
89 unsigned long flags;
90
91 local_irq_save(flags);
92 /*
93 * We need to handle two requests being made at the same time.
94 * There are two important cases:
eric miaob0086ef2008-04-30 00:52:19 -070095 * 1. When we are changing VT (C_REENABLE) while unblanking
96 * (C_ENABLE) We must perform the unblanking, which will
97 * do our REENABLE for us.
98 * 2. When we are blanking, but immediately unblank before
99 * we have blanked. We do the "REENABLE" thing here as
100 * well, just to be sure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 */
102 if (fbi->task_state == C_ENABLE && state == C_REENABLE)
103 state = (u_int) -1;
104 if (fbi->task_state == C_DISABLE && state == C_ENABLE)
105 state = C_REENABLE;
106
107 if (state != (u_int)-1) {
108 fbi->task_state = state;
109 schedule_work(&fbi->task);
110 }
111 local_irq_restore(flags);
112}
113
114static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
115{
116 chan &= 0xffff;
117 chan >>= 16 - bf->length;
118 return chan << bf->offset;
119}
120
121static int
122pxafb_setpalettereg(u_int regno, u_int red, u_int green, u_int blue,
123 u_int trans, struct fb_info *info)
124{
125 struct pxafb_info *fbi = (struct pxafb_info *)info;
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700126 u_int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700128 if (regno >= fbi->palette_size)
129 return 1;
130
131 if (fbi->fb.var.grayscale) {
132 fbi->palette_cpu[regno] = ((blue >> 8) & 0x00ff);
133 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700135
136 switch (fbi->lccr4 & LCCR4_PAL_FOR_MASK) {
137 case LCCR4_PAL_FOR_0:
138 val = ((red >> 0) & 0xf800);
139 val |= ((green >> 5) & 0x07e0);
140 val |= ((blue >> 11) & 0x001f);
141 fbi->palette_cpu[regno] = val;
142 break;
143 case LCCR4_PAL_FOR_1:
144 val = ((red << 8) & 0x00f80000);
145 val |= ((green >> 0) & 0x0000fc00);
146 val |= ((blue >> 8) & 0x000000f8);
eric miaob0086ef2008-04-30 00:52:19 -0700147 ((u32 *)(fbi->palette_cpu))[regno] = val;
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700148 break;
149 case LCCR4_PAL_FOR_2:
150 val = ((red << 8) & 0x00fc0000);
151 val |= ((green >> 0) & 0x0000fc00);
152 val |= ((blue >> 8) & 0x000000fc);
eric miaob0086ef2008-04-30 00:52:19 -0700153 ((u32 *)(fbi->palette_cpu))[regno] = val;
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700154 break;
155 }
156
157 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
160static int
161pxafb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
162 u_int trans, struct fb_info *info)
163{
164 struct pxafb_info *fbi = (struct pxafb_info *)info;
165 unsigned int val;
166 int ret = 1;
167
168 /*
169 * If inverse mode was selected, invert all the colours
170 * rather than the register number. The register number
171 * is what you poke into the framebuffer to produce the
172 * colour you requested.
173 */
174 if (fbi->cmap_inverse) {
175 red = 0xffff - red;
176 green = 0xffff - green;
177 blue = 0xffff - blue;
178 }
179
180 /*
181 * If greyscale is true, then we convert the RGB value
182 * to greyscale no matter what visual we are using.
183 */
184 if (fbi->fb.var.grayscale)
185 red = green = blue = (19595 * red + 38470 * green +
186 7471 * blue) >> 16;
187
188 switch (fbi->fb.fix.visual) {
189 case FB_VISUAL_TRUECOLOR:
190 /*
191 * 16-bit True Colour. We encode the RGB value
192 * according to the RGB bitfield information.
193 */
194 if (regno < 16) {
195 u32 *pal = fbi->fb.pseudo_palette;
196
197 val = chan_to_field(red, &fbi->fb.var.red);
198 val |= chan_to_field(green, &fbi->fb.var.green);
199 val |= chan_to_field(blue, &fbi->fb.var.blue);
200
201 pal[regno] = val;
202 ret = 0;
203 }
204 break;
205
206 case FB_VISUAL_STATIC_PSEUDOCOLOR:
207 case FB_VISUAL_PSEUDOCOLOR:
208 ret = pxafb_setpalettereg(regno, red, green, blue, trans, info);
209 break;
210 }
211
212 return ret;
213}
214
215/*
216 * pxafb_bpp_to_lccr3():
217 * Convert a bits per pixel value to the correct bit pattern for LCCR3
218 */
219static int pxafb_bpp_to_lccr3(struct fb_var_screeninfo *var)
220{
eric miaob0086ef2008-04-30 00:52:19 -0700221 int ret = 0;
222 switch (var->bits_per_pixel) {
223 case 1: ret = LCCR3_1BPP; break;
224 case 2: ret = LCCR3_2BPP; break;
225 case 4: ret = LCCR3_4BPP; break;
226 case 8: ret = LCCR3_8BPP; break;
227 case 16: ret = LCCR3_16BPP; break;
228 }
229 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
232#ifdef CONFIG_CPU_FREQ
233/*
234 * pxafb_display_dma_period()
235 * Calculate the minimum period (in picoseconds) between two DMA
236 * requests for the LCD controller. If we hit this, it means we're
237 * doing nothing but LCD DMA.
238 */
239static unsigned int pxafb_display_dma_period(struct fb_var_screeninfo *var)
240{
eric miaob0086ef2008-04-30 00:52:19 -0700241 /*
242 * Period = pixclock * bits_per_byte * bytes_per_transfer
243 * / memory_bits_per_pixel;
244 */
245 return var->pixclock * 8 * 16 / var->bits_per_pixel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247#endif
248
249/*
Richard Purdied14b2722006-09-20 22:54:21 +0100250 * Select the smallest mode that allows the desired resolution to be
251 * displayed. If desired parameters can be rounded up.
252 */
eric miaob0086ef2008-04-30 00:52:19 -0700253static struct pxafb_mode_info *pxafb_getmode(struct pxafb_mach_info *mach,
254 struct fb_var_screeninfo *var)
Richard Purdied14b2722006-09-20 22:54:21 +0100255{
256 struct pxafb_mode_info *mode = NULL;
257 struct pxafb_mode_info *modelist = mach->modes;
258 unsigned int best_x = 0xffffffff, best_y = 0xffffffff;
259 unsigned int i;
260
eric miaob0086ef2008-04-30 00:52:19 -0700261 for (i = 0; i < mach->num_modes; i++) {
262 if (modelist[i].xres >= var->xres &&
263 modelist[i].yres >= var->yres &&
264 modelist[i].xres < best_x &&
265 modelist[i].yres < best_y &&
266 modelist[i].bpp >= var->bits_per_pixel) {
Richard Purdied14b2722006-09-20 22:54:21 +0100267 best_x = modelist[i].xres;
268 best_y = modelist[i].yres;
269 mode = &modelist[i];
270 }
271 }
272
273 return mode;
274}
275
eric miaob0086ef2008-04-30 00:52:19 -0700276static void pxafb_setmode(struct fb_var_screeninfo *var,
277 struct pxafb_mode_info *mode)
Richard Purdied14b2722006-09-20 22:54:21 +0100278{
279 var->xres = mode->xres;
280 var->yres = mode->yres;
281 var->bits_per_pixel = mode->bpp;
282 var->pixclock = mode->pixclock;
283 var->hsync_len = mode->hsync_len;
284 var->left_margin = mode->left_margin;
285 var->right_margin = mode->right_margin;
286 var->vsync_len = mode->vsync_len;
287 var->upper_margin = mode->upper_margin;
288 var->lower_margin = mode->lower_margin;
289 var->sync = mode->sync;
290 var->grayscale = mode->cmap_greyscale;
291 var->xres_virtual = var->xres;
292 var->yres_virtual = var->yres;
293}
294
295/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 * pxafb_check_var():
297 * Get the video params out of 'var'. If a value doesn't fit, round it up,
298 * if it's too big, return -EINVAL.
299 *
300 * Round up in the following order: bits_per_pixel, xres,
301 * yres, xres_virtual, yres_virtual, xoffset, yoffset, grayscale,
302 * bitfields, horizontal timing, vertical timing.
303 */
304static int pxafb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
305{
306 struct pxafb_info *fbi = (struct pxafb_info *)info;
Richard Purdied14b2722006-09-20 22:54:21 +0100307 struct pxafb_mach_info *inf = fbi->dev->platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 if (var->xres < MIN_XRES)
310 var->xres = MIN_XRES;
311 if (var->yres < MIN_YRES)
312 var->yres = MIN_YRES;
Richard Purdied14b2722006-09-20 22:54:21 +0100313
314 if (inf->fixed_modes) {
315 struct pxafb_mode_info *mode;
316
317 mode = pxafb_getmode(inf, var);
318 if (!mode)
319 return -EINVAL;
320 pxafb_setmode(var, mode);
321 } else {
322 if (var->xres > inf->modes->xres)
323 return -EINVAL;
324 if (var->yres > inf->modes->yres)
325 return -EINVAL;
326 if (var->bits_per_pixel > inf->modes->bpp)
327 return -EINVAL;
328 }
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 var->xres_virtual =
331 max(var->xres_virtual, var->xres);
332 var->yres_virtual =
333 max(var->yres_virtual, var->yres);
334
eric miaob0086ef2008-04-30 00:52:19 -0700335 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 * Setup the RGB parameters for this display.
337 *
338 * The pixel packing format is described on page 7-11 of the
339 * PXA2XX Developer's Manual.
eric miaob0086ef2008-04-30 00:52:19 -0700340 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (var->bits_per_pixel == 16) {
342 var->red.offset = 11; var->red.length = 5;
343 var->green.offset = 5; var->green.length = 6;
344 var->blue.offset = 0; var->blue.length = 5;
345 var->transp.offset = var->transp.length = 0;
346 } else {
eric miaob0086ef2008-04-30 00:52:19 -0700347 var->red.offset = var->green.offset = 0;
348 var->blue.offset = var->transp.offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 var->red.length = 8;
350 var->green.length = 8;
351 var->blue.length = 8;
352 var->transp.length = 0;
353 }
354
355#ifdef CONFIG_CPU_FREQ
Russell Kingca5da712005-09-29 09:44:54 +0100356 pr_debug("pxafb: dma period = %d ps, clock = %d kHz\n",
357 pxafb_display_dma_period(var),
358 get_clk_frequency_khz(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359#endif
360
361 return 0;
362}
363
364static inline void pxafb_set_truecolor(u_int is_true_color)
365{
eric miaob0086ef2008-04-30 00:52:19 -0700366 /* do your machine-specific setup if needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
369/*
370 * pxafb_set_par():
371 * Set the user defined part of the display for the specified console
372 */
373static int pxafb_set_par(struct fb_info *info)
374{
375 struct pxafb_info *fbi = (struct pxafb_info *)info;
376 struct fb_var_screeninfo *var = &info->var;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (var->bits_per_pixel == 16)
379 fbi->fb.fix.visual = FB_VISUAL_TRUECOLOR;
380 else if (!fbi->cmap_static)
381 fbi->fb.fix.visual = FB_VISUAL_PSEUDOCOLOR;
382 else {
383 /*
384 * Some people have weird ideas about wanting static
385 * pseudocolor maps. I suspect their user space
386 * applications are broken.
387 */
388 fbi->fb.fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
389 }
390
391 fbi->fb.fix.line_length = var->xres_virtual *
392 var->bits_per_pixel / 8;
393 if (var->bits_per_pixel == 16)
394 fbi->palette_size = 0;
395 else
eric miaob0086ef2008-04-30 00:52:19 -0700396 fbi->palette_size = var->bits_per_pixel == 1 ?
397 4 : 1 << var->bits_per_pixel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
eric miao2c42dd82008-04-30 00:52:21 -0700399 fbi->palette_cpu = (u16 *)&fbi->dma_buff->palette[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 /*
402 * Set (any) board control register to handle new color depth
403 */
404 pxafb_set_truecolor(fbi->fb.fix.visual == FB_VISUAL_TRUECOLOR);
405
406 if (fbi->fb.var.bits_per_pixel == 16)
407 fb_dealloc_cmap(&fbi->fb.cmap);
408 else
409 fb_alloc_cmap(&fbi->fb.cmap, 1<<fbi->fb.var.bits_per_pixel, 0);
410
411 pxafb_activate_var(var, fbi);
412
413 return 0;
414}
415
416/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 * pxafb_blank():
418 * Blank the display by setting all palette values to zero. Note, the
419 * 16 bpp mode does not really use the palette, so this will not
420 * blank the display in all modes.
421 */
422static int pxafb_blank(int blank, struct fb_info *info)
423{
424 struct pxafb_info *fbi = (struct pxafb_info *)info;
425 int i;
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 switch (blank) {
428 case FB_BLANK_POWERDOWN:
429 case FB_BLANK_VSYNC_SUSPEND:
430 case FB_BLANK_HSYNC_SUSPEND:
431 case FB_BLANK_NORMAL:
432 if (fbi->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR ||
433 fbi->fb.fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
434 for (i = 0; i < fbi->palette_size; i++)
435 pxafb_setpalettereg(i, 0, 0, 0, 0, info);
436
437 pxafb_schedule_work(fbi, C_DISABLE);
eric miaob0086ef2008-04-30 00:52:19 -0700438 /* TODO if (pxafb_blank_helper) pxafb_blank_helper(blank); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 break;
440
441 case FB_BLANK_UNBLANK:
eric miaob0086ef2008-04-30 00:52:19 -0700442 /* TODO if (pxafb_blank_helper) pxafb_blank_helper(blank); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (fbi->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR ||
444 fbi->fb.fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
445 fb_set_cmap(&fbi->fb.cmap, info);
446 pxafb_schedule_work(fbi, C_ENABLE);
447 }
448 return 0;
449}
450
Christoph Hellwig216d5262006-01-14 13:21:25 -0800451static int pxafb_mmap(struct fb_info *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 struct vm_area_struct *vma)
453{
454 struct pxafb_info *fbi = (struct pxafb_info *)info;
455 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
456
457 if (off < info->fix.smem_len) {
458 vma->vm_pgoff += 1;
459 return dma_mmap_writecombine(fbi->dev, vma, fbi->map_cpu,
460 fbi->map_dma, fbi->map_size);
461 }
462 return -EINVAL;
463}
464
465static struct fb_ops pxafb_ops = {
466 .owner = THIS_MODULE,
467 .fb_check_var = pxafb_check_var,
468 .fb_set_par = pxafb_set_par,
469 .fb_setcolreg = pxafb_setcolreg,
470 .fb_fillrect = cfb_fillrect,
471 .fb_copyarea = cfb_copyarea,
472 .fb_imageblit = cfb_imageblit,
473 .fb_blank = pxafb_blank,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 .fb_mmap = pxafb_mmap,
475};
476
477/*
478 * Calculate the PCD value from the clock rate (in picoseconds).
479 * We take account of the PPCR clock setting.
480 * From PXA Developer's Manual:
481 *
482 * PixelClock = LCLK
483 * -------------
484 * 2 ( PCD + 1 )
485 *
486 * PCD = LCLK
487 * ------------- - 1
488 * 2(PixelClock)
489 *
490 * Where:
491 * LCLK = LCD/Memory Clock
492 * PCD = LCCR3[7:0]
493 *
494 * PixelClock here is in Hz while the pixclock argument given is the
495 * period in picoseconds. Hence PixelClock = 1 / ( pixclock * 10^-12 )
496 *
497 * The function get_lclk_frequency_10khz returns LCLK in units of
498 * 10khz. Calling the result of this function lclk gives us the
499 * following
500 *
501 * PCD = (lclk * 10^4 ) * ( pixclock * 10^-12 )
502 * -------------------------------------- - 1
503 * 2
504 *
505 * Factoring the 10^4 and 10^-12 out gives 10^-8 == 1 / 100000000 as used below.
506 */
eric miaob0086ef2008-04-30 00:52:19 -0700507static inline unsigned int get_pcd(struct pxafb_info *fbi,
508 unsigned int pixclock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
510 unsigned long long pcd;
511
512 /* FIXME: Need to take into account Double Pixel Clock mode
Russell King72e35242007-08-20 10:18:42 +0100513 * (DPC) bit? or perhaps set it based on the various clock
514 * speeds */
515 pcd = (unsigned long long)(clk_get_rate(fbi->clk) / 10000);
516 pcd *= pixclock;
Nicolas Pitrebf1b8ab2005-06-23 21:56:45 +0100517 do_div(pcd, 100000000 * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 /* no need for this, since we should subtract 1 anyway. they cancel */
519 /* pcd += 1; */ /* make up for integer math truncations */
520 return (unsigned int)pcd;
521}
522
523/*
Richard Purdieba44cd22005-09-09 13:10:03 -0700524 * Some touchscreens need hsync information from the video driver to
Russell King72e35242007-08-20 10:18:42 +0100525 * function correctly. We export it here. Note that 'hsync_time' and
526 * the value returned from pxafb_get_hsync_time() is the *reciprocal*
527 * of the hsync period in seconds.
Richard Purdieba44cd22005-09-09 13:10:03 -0700528 */
529static inline void set_hsync_time(struct pxafb_info *fbi, unsigned int pcd)
530{
Russell King72e35242007-08-20 10:18:42 +0100531 unsigned long htime;
Richard Purdieba44cd22005-09-09 13:10:03 -0700532
533 if ((pcd == 0) || (fbi->fb.var.hsync_len == 0)) {
eric miaob0086ef2008-04-30 00:52:19 -0700534 fbi->hsync_time = 0;
Richard Purdieba44cd22005-09-09 13:10:03 -0700535 return;
536 }
537
Russell King72e35242007-08-20 10:18:42 +0100538 htime = clk_get_rate(fbi->clk) / (pcd * fbi->fb.var.hsync_len);
539
Richard Purdieba44cd22005-09-09 13:10:03 -0700540 fbi->hsync_time = htime;
541}
542
543unsigned long pxafb_get_hsync_time(struct device *dev)
544{
545 struct pxafb_info *fbi = dev_get_drvdata(dev);
546
547 /* If display is blanked/suspended, hsync isn't active */
548 if (!fbi || (fbi->state != C_ENABLE))
549 return 0;
550
551 return fbi->hsync_time;
552}
553EXPORT_SYMBOL(pxafb_get_hsync_time);
554
eric miao2c42dd82008-04-30 00:52:21 -0700555static int setup_frame_dma(struct pxafb_info *fbi, int dma, int pal,
556 unsigned int offset, size_t size)
557{
558 struct pxafb_dma_descriptor *dma_desc, *pal_desc;
559 unsigned int dma_desc_off, pal_desc_off;
560
561 if (dma < 0 || dma >= DMA_MAX)
562 return -EINVAL;
563
564 dma_desc = &fbi->dma_buff->dma_desc[dma];
565 dma_desc_off = offsetof(struct pxafb_dma_buff, dma_desc[dma]);
566
567 dma_desc->fsadr = fbi->screen_dma + offset;
568 dma_desc->fidr = 0;
569 dma_desc->ldcmd = size;
570
571 if (pal < 0 || pal >= PAL_MAX) {
572 dma_desc->fdadr = fbi->dma_buff_phys + dma_desc_off;
573 fbi->fdadr[dma] = fbi->dma_buff_phys + dma_desc_off;
574 } else {
575 pal_desc = &fbi->dma_buff->pal_desc[dma];
576 pal_desc_off = offsetof(struct pxafb_dma_buff, dma_desc[pal]);
577
578 pal_desc->fsadr = fbi->dma_buff_phys + pal * PALETTE_SIZE;
579 pal_desc->fidr = 0;
580
581 if ((fbi->lccr4 & LCCR4_PAL_FOR_MASK) == LCCR4_PAL_FOR_0)
582 pal_desc->ldcmd = fbi->palette_size * sizeof(u16);
583 else
584 pal_desc->ldcmd = fbi->palette_size * sizeof(u32);
585
586 pal_desc->ldcmd |= LDCMD_PAL;
587
588 /* flip back and forth between palette and frame buffer */
589 pal_desc->fdadr = fbi->dma_buff_phys + dma_desc_off;
590 dma_desc->fdadr = fbi->dma_buff_phys + pal_desc_off;
591 fbi->fdadr[dma] = fbi->dma_buff_phys + dma_desc_off;
592 }
593
594 return 0;
595}
596
Eric Miao90eabbf2008-04-30 00:52:25 -0700597static void setup_parallel_timing(struct pxafb_info *fbi,
598 struct fb_var_screeninfo *var)
599{
600 unsigned int lines_per_panel, pcd = get_pcd(fbi, var->pixclock);
601
602 fbi->reg_lccr1 =
603 LCCR1_DisWdth(var->xres) +
604 LCCR1_HorSnchWdth(var->hsync_len) +
605 LCCR1_BegLnDel(var->left_margin) +
606 LCCR1_EndLnDel(var->right_margin);
607
608 /*
609 * If we have a dual scan LCD, we need to halve
610 * the YRES parameter.
611 */
612 lines_per_panel = var->yres;
613 if ((fbi->lccr0 & LCCR0_SDS) == LCCR0_Dual)
614 lines_per_panel /= 2;
615
616 fbi->reg_lccr2 =
617 LCCR2_DisHght(lines_per_panel) +
618 LCCR2_VrtSnchWdth(var->vsync_len) +
619 LCCR2_BegFrmDel(var->upper_margin) +
620 LCCR2_EndFrmDel(var->lower_margin);
621
622 fbi->reg_lccr3 = fbi->lccr3 |
623 (var->sync & FB_SYNC_HOR_HIGH_ACT ?
624 LCCR3_HorSnchH : LCCR3_HorSnchL) |
625 (var->sync & FB_SYNC_VERT_HIGH_ACT ?
626 LCCR3_VrtSnchH : LCCR3_VrtSnchL);
627
628 if (pcd) {
629 fbi->reg_lccr3 |= LCCR3_PixClkDiv(pcd);
630 set_hsync_time(fbi, pcd);
631 }
632}
633
Richard Purdieba44cd22005-09-09 13:10:03 -0700634/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 * pxafb_activate_var():
eric miaob0086ef2008-04-30 00:52:19 -0700636 * Configures LCD Controller based on entries in var parameter.
637 * Settings are only written to the controller if changes were made.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 */
eric miaob0086ef2008-04-30 00:52:19 -0700639static int pxafb_activate_var(struct fb_var_screeninfo *var,
640 struct pxafb_info *fbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 u_long flags;
eric miao2c42dd82008-04-30 00:52:21 -0700643 size_t nbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645#if DEBUG_VAR
eric miaob0086ef2008-04-30 00:52:19 -0700646 if (var->xres < 16 || var->xres > 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 printk(KERN_ERR "%s: invalid xres %d\n",
648 fbi->fb.fix.id, var->xres);
eric miaob0086ef2008-04-30 00:52:19 -0700649 switch (var->bits_per_pixel) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 case 1:
651 case 2:
652 case 4:
653 case 8:
654 case 16:
655 break;
656 default:
657 printk(KERN_ERR "%s: invalid bit depth %d\n",
658 fbi->fb.fix.id, var->bits_per_pixel);
659 break;
660 }
eric miaob0086ef2008-04-30 00:52:19 -0700661 if (var->hsync_len < 1 || var->hsync_len > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 printk(KERN_ERR "%s: invalid hsync_len %d\n",
663 fbi->fb.fix.id, var->hsync_len);
eric miaob0086ef2008-04-30 00:52:19 -0700664 if (var->left_margin < 1 || var->left_margin > 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 printk(KERN_ERR "%s: invalid left_margin %d\n",
666 fbi->fb.fix.id, var->left_margin);
667 if (var->right_margin < 1 || var->right_margin > 255)
668 printk(KERN_ERR "%s: invalid right_margin %d\n",
669 fbi->fb.fix.id, var->right_margin);
eric miaob0086ef2008-04-30 00:52:19 -0700670 if (var->yres < 1 || var->yres > 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 printk(KERN_ERR "%s: invalid yres %d\n",
672 fbi->fb.fix.id, var->yres);
eric miaob0086ef2008-04-30 00:52:19 -0700673 if (var->vsync_len < 1 || var->vsync_len > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 printk(KERN_ERR "%s: invalid vsync_len %d\n",
675 fbi->fb.fix.id, var->vsync_len);
676 if (var->upper_margin < 0 || var->upper_margin > 255)
677 printk(KERN_ERR "%s: invalid upper_margin %d\n",
678 fbi->fb.fix.id, var->upper_margin);
679 if (var->lower_margin < 0 || var->lower_margin > 255)
680 printk(KERN_ERR "%s: invalid lower_margin %d\n",
681 fbi->fb.fix.id, var->lower_margin);
682#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 /* Update shadow copy atomically */
684 local_irq_save(flags);
685
Eric Miao90eabbf2008-04-30 00:52:25 -0700686 setup_parallel_timing(fbi, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Eric Miao90eabbf2008-04-30 00:52:25 -0700688 fbi->reg_lccr0 = fbi->lccr0 |
689 (LCCR0_LDM | LCCR0_SFM | LCCR0_IUM | LCCR0_EFM |
690 LCCR0_QDM | LCCR0_BM | LCCR0_OUM);
691
692 fbi->reg_lccr3 |= pxafb_bpp_to_lccr3(var);
693
694 nbytes = var->yres * fbi->fb.fix.line_length;
695
696 if ((fbi->lccr0 & LCCR0_SDS) == LCCR0_Dual) {
697 nbytes = nbytes / 2;
eric miao2c42dd82008-04-30 00:52:21 -0700698 setup_frame_dma(fbi, DMA_LOWER, PAL_NONE, nbytes, nbytes);
Eric Miao90eabbf2008-04-30 00:52:25 -0700699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
eric miao2c42dd82008-04-30 00:52:21 -0700701 if (var->bits_per_pixel >= 16)
702 setup_frame_dma(fbi, DMA_BASE, PAL_NONE, 0, nbytes);
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700703 else
eric miao2c42dd82008-04-30 00:52:21 -0700704 setup_frame_dma(fbi, DMA_BASE, PAL_BASE, 0, nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Eric Miaoa7535ba2008-04-30 00:52:24 -0700706 fbi->reg_lccr4 = lcd_readl(fbi, LCCR4) & ~LCCR4_PAL_FOR_MASK;
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700707 fbi->reg_lccr4 |= (fbi->lccr4 & LCCR4_PAL_FOR_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 local_irq_restore(flags);
709
710 /*
711 * Only update the registers if the controller is enabled
712 * and something has changed.
713 */
Eric Miaoa7535ba2008-04-30 00:52:24 -0700714 if ((lcd_readl(fbi, LCCR0) != fbi->reg_lccr0) ||
715 (lcd_readl(fbi, LCCR1) != fbi->reg_lccr1) ||
716 (lcd_readl(fbi, LCCR2) != fbi->reg_lccr2) ||
717 (lcd_readl(fbi, LCCR3) != fbi->reg_lccr3) ||
718 (lcd_readl(fbi, FDADR0) != fbi->fdadr[0]) ||
719 (lcd_readl(fbi, FDADR1) != fbi->fdadr[1]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 pxafb_schedule_work(fbi, C_REENABLE);
721
722 return 0;
723}
724
725/*
726 * NOTE! The following functions are purely helpers for set_ctrlr_state.
727 * Do not call them directly; set_ctrlr_state does the correct serialisation
728 * to ensure that things happen in the right way 100% of time time.
729 * -- rmk
730 */
731static inline void __pxafb_backlight_power(struct pxafb_info *fbi, int on)
732{
Russell Kingca5da712005-09-29 09:44:54 +0100733 pr_debug("pxafb: backlight o%s\n", on ? "n" : "ff");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
eric miaob0086ef2008-04-30 00:52:19 -0700735 if (pxafb_backlight_power)
736 pxafb_backlight_power(on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738
739static inline void __pxafb_lcd_power(struct pxafb_info *fbi, int on)
740{
Russell Kingca5da712005-09-29 09:44:54 +0100741 pr_debug("pxafb: LCD power o%s\n", on ? "n" : "ff");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 if (pxafb_lcd_power)
Richard Purdied14b2722006-09-20 22:54:21 +0100744 pxafb_lcd_power(on, &fbi->fb.var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
747static void pxafb_setup_gpio(struct pxafb_info *fbi)
748{
749 int gpio, ldd_bits;
eric miaob0086ef2008-04-30 00:52:19 -0700750 unsigned int lccr0 = fbi->lccr0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 /*
753 * setup is based on type of panel supported
eric miaob0086ef2008-04-30 00:52:19 -0700754 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 /* 4 bit interface */
757 if ((lccr0 & LCCR0_CMS) == LCCR0_Mono &&
758 (lccr0 & LCCR0_SDS) == LCCR0_Sngl &&
759 (lccr0 & LCCR0_DPD) == LCCR0_4PixMono)
760 ldd_bits = 4;
761
762 /* 8 bit interface */
eric miaob0086ef2008-04-30 00:52:19 -0700763 else if (((lccr0 & LCCR0_CMS) == LCCR0_Mono &&
764 ((lccr0 & LCCR0_SDS) == LCCR0_Dual ||
765 (lccr0 & LCCR0_DPD) == LCCR0_8PixMono)) ||
766 ((lccr0 & LCCR0_CMS) == LCCR0_Color &&
767 (lccr0 & LCCR0_PAS) == LCCR0_Pas &&
768 (lccr0 & LCCR0_SDS) == LCCR0_Sngl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 ldd_bits = 8;
770
771 /* 16 bit interface */
772 else if ((lccr0 & LCCR0_CMS) == LCCR0_Color &&
eric miaob0086ef2008-04-30 00:52:19 -0700773 ((lccr0 & LCCR0_SDS) == LCCR0_Dual ||
774 (lccr0 & LCCR0_PAS) == LCCR0_Act))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 ldd_bits = 16;
776
777 else {
eric miaob0086ef2008-04-30 00:52:19 -0700778 printk(KERN_ERR "pxafb_setup_gpio: unable to determine "
779 "bits per pixel\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return;
eric miaob0086ef2008-04-30 00:52:19 -0700781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 for (gpio = 58; ldd_bits; gpio++, ldd_bits--)
784 pxa_gpio_mode(gpio | GPIO_ALT_FN_2_OUT);
785 pxa_gpio_mode(GPIO74_LCD_FCLK_MD);
786 pxa_gpio_mode(GPIO75_LCD_LCLK_MD);
787 pxa_gpio_mode(GPIO76_LCD_PCLK_MD);
788 pxa_gpio_mode(GPIO77_LCD_ACBIAS_MD);
789}
790
791static void pxafb_enable_controller(struct pxafb_info *fbi)
792{
Russell Kingca5da712005-09-29 09:44:54 +0100793 pr_debug("pxafb: Enabling LCD controller\n");
eric miao2c42dd82008-04-30 00:52:21 -0700794 pr_debug("fdadr0 0x%08x\n", (unsigned int) fbi->fdadr[0]);
795 pr_debug("fdadr1 0x%08x\n", (unsigned int) fbi->fdadr[1]);
Russell Kingca5da712005-09-29 09:44:54 +0100796 pr_debug("reg_lccr0 0x%08x\n", (unsigned int) fbi->reg_lccr0);
797 pr_debug("reg_lccr1 0x%08x\n", (unsigned int) fbi->reg_lccr1);
798 pr_debug("reg_lccr2 0x%08x\n", (unsigned int) fbi->reg_lccr2);
799 pr_debug("reg_lccr3 0x%08x\n", (unsigned int) fbi->reg_lccr3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Nicolas Pitre8d372262005-08-10 16:45:13 +0100801 /* enable LCD controller clock */
Russell King72e35242007-08-20 10:18:42 +0100802 clk_enable(fbi->clk);
Nicolas Pitre8d372262005-08-10 16:45:13 +0100803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 /* Sequence from 11.7.10 */
Eric Miaoa7535ba2008-04-30 00:52:24 -0700805 lcd_writel(fbi, LCCR3, fbi->reg_lccr3);
806 lcd_writel(fbi, LCCR2, fbi->reg_lccr2);
807 lcd_writel(fbi, LCCR1, fbi->reg_lccr1);
808 lcd_writel(fbi, LCCR0, fbi->reg_lccr0 & ~LCCR0_ENB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Eric Miaoa7535ba2008-04-30 00:52:24 -0700810 lcd_writel(fbi, FDADR0, fbi->fdadr[0]);
811 lcd_writel(fbi, FDADR1, fbi->fdadr[1]);
812 lcd_writel(fbi, LCCR0, fbi->reg_lccr0 | LCCR0_ENB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813}
814
815static void pxafb_disable_controller(struct pxafb_info *fbi)
816{
eric miaoce4fb7b2008-04-30 00:52:21 -0700817 uint32_t lccr0;
818
eric miaoce4fb7b2008-04-30 00:52:21 -0700819 /* Clear LCD Status Register */
Eric Miaoa7535ba2008-04-30 00:52:24 -0700820 lcd_writel(fbi, LCSR, 0xffffffff);
eric miaoce4fb7b2008-04-30 00:52:21 -0700821
Eric Miaoa7535ba2008-04-30 00:52:24 -0700822 lccr0 = lcd_readl(fbi, LCCR0) & ~LCCR0_LDM;
823 lcd_writel(fbi, LCCR0, lccr0);
824 lcd_writel(fbi, LCCR0, lccr0 | LCCR0_DIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Eric Miao2ba162b2008-04-30 00:52:24 -0700826 wait_for_completion_timeout(&fbi->disable_done, 200 * HZ / 1000);
Nicolas Pitre8d372262005-08-10 16:45:13 +0100827
828 /* disable LCD controller clock */
Russell King72e35242007-08-20 10:18:42 +0100829 clk_disable(fbi->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
831
832/*
833 * pxafb_handle_irq: Handle 'LCD DONE' interrupts.
834 */
David Howells7d12e782006-10-05 14:55:46 +0100835static irqreturn_t pxafb_handle_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
837 struct pxafb_info *fbi = dev_id;
Eric Miaoa7535ba2008-04-30 00:52:24 -0700838 unsigned int lccr0, lcsr = lcd_readl(fbi, LCSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 if (lcsr & LCSR_LDD) {
Eric Miaoa7535ba2008-04-30 00:52:24 -0700841 lccr0 = lcd_readl(fbi, LCCR0);
842 lcd_writel(fbi, LCCR0, lccr0 | LCCR0_LDM);
Eric Miao2ba162b2008-04-30 00:52:24 -0700843 complete(&fbi->disable_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
845
Eric Miaoa7535ba2008-04-30 00:52:24 -0700846 lcd_writel(fbi, LCSR, lcsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return IRQ_HANDLED;
848}
849
850/*
851 * This function must be called from task context only, since it will
852 * sleep when disabling the LCD controller, or if we get two contending
853 * processes trying to alter state.
854 */
855static void set_ctrlr_state(struct pxafb_info *fbi, u_int state)
856{
857 u_int old_state;
858
859 down(&fbi->ctrlr_sem);
860
861 old_state = fbi->state;
862
863 /*
864 * Hack around fbcon initialisation.
865 */
866 if (old_state == C_STARTUP && state == C_REENABLE)
867 state = C_ENABLE;
868
869 switch (state) {
870 case C_DISABLE_CLKCHANGE:
871 /*
872 * Disable controller for clock change. If the
873 * controller is already disabled, then do nothing.
874 */
875 if (old_state != C_DISABLE && old_state != C_DISABLE_PM) {
876 fbi->state = state;
eric miaob0086ef2008-04-30 00:52:19 -0700877 /* TODO __pxafb_lcd_power(fbi, 0); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 pxafb_disable_controller(fbi);
879 }
880 break;
881
882 case C_DISABLE_PM:
883 case C_DISABLE:
884 /*
885 * Disable controller
886 */
887 if (old_state != C_DISABLE) {
888 fbi->state = state;
889 __pxafb_backlight_power(fbi, 0);
890 __pxafb_lcd_power(fbi, 0);
891 if (old_state != C_DISABLE_CLKCHANGE)
892 pxafb_disable_controller(fbi);
893 }
894 break;
895
896 case C_ENABLE_CLKCHANGE:
897 /*
898 * Enable the controller after clock change. Only
899 * do this if we were disabled for the clock change.
900 */
901 if (old_state == C_DISABLE_CLKCHANGE) {
902 fbi->state = C_ENABLE;
903 pxafb_enable_controller(fbi);
eric miaob0086ef2008-04-30 00:52:19 -0700904 /* TODO __pxafb_lcd_power(fbi, 1); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 }
906 break;
907
908 case C_REENABLE:
909 /*
910 * Re-enable the controller only if it was already
911 * enabled. This is so we reprogram the control
912 * registers.
913 */
914 if (old_state == C_ENABLE) {
Richard Purdied14b2722006-09-20 22:54:21 +0100915 __pxafb_lcd_power(fbi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 pxafb_disable_controller(fbi);
917 pxafb_setup_gpio(fbi);
918 pxafb_enable_controller(fbi);
Richard Purdied14b2722006-09-20 22:54:21 +0100919 __pxafb_lcd_power(fbi, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
921 break;
922
923 case C_ENABLE_PM:
924 /*
925 * Re-enable the controller after PM. This is not
926 * perfect - think about the case where we were doing
927 * a clock change, and we suspended half-way through.
928 */
929 if (old_state != C_DISABLE_PM)
930 break;
931 /* fall through */
932
933 case C_ENABLE:
934 /*
935 * Power up the LCD screen, enable controller, and
936 * turn on the backlight.
937 */
938 if (old_state != C_ENABLE) {
939 fbi->state = C_ENABLE;
940 pxafb_setup_gpio(fbi);
941 pxafb_enable_controller(fbi);
942 __pxafb_lcd_power(fbi, 1);
943 __pxafb_backlight_power(fbi, 1);
944 }
945 break;
946 }
947 up(&fbi->ctrlr_sem);
948}
949
950/*
951 * Our LCD controller task (which is called when we blank or unblank)
952 * via keventd.
953 */
David Howells6d5aefb2006-12-05 19:36:26 +0000954static void pxafb_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
David Howells6d5aefb2006-12-05 19:36:26 +0000956 struct pxafb_info *fbi =
957 container_of(work, struct pxafb_info, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 u_int state = xchg(&fbi->task_state, -1);
959
960 set_ctrlr_state(fbi, state);
961}
962
963#ifdef CONFIG_CPU_FREQ
964/*
965 * CPU clock speed change handler. We need to adjust the LCD timing
966 * parameters when the CPU clock is adjusted by the power management
967 * subsystem.
968 *
969 * TODO: Determine why f->new != 10*get_lclk_frequency_10khz()
970 */
971static int
972pxafb_freq_transition(struct notifier_block *nb, unsigned long val, void *data)
973{
974 struct pxafb_info *fbi = TO_INF(nb, freq_transition);
eric miaob0086ef2008-04-30 00:52:19 -0700975 /* TODO struct cpufreq_freqs *f = data; */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 u_int pcd;
977
978 switch (val) {
979 case CPUFREQ_PRECHANGE:
980 set_ctrlr_state(fbi, C_DISABLE_CLKCHANGE);
981 break;
982
983 case CPUFREQ_POSTCHANGE:
Russell King72e35242007-08-20 10:18:42 +0100984 pcd = get_pcd(fbi, fbi->fb.var.pixclock);
Richard Purdieba44cd22005-09-09 13:10:03 -0700985 set_hsync_time(fbi, pcd);
eric miaob0086ef2008-04-30 00:52:19 -0700986 fbi->reg_lccr3 = (fbi->reg_lccr3 & ~0xff) |
987 LCCR3_PixClkDiv(pcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 set_ctrlr_state(fbi, C_ENABLE_CLKCHANGE);
989 break;
990 }
991 return 0;
992}
993
994static int
995pxafb_freq_policy(struct notifier_block *nb, unsigned long val, void *data)
996{
997 struct pxafb_info *fbi = TO_INF(nb, freq_policy);
998 struct fb_var_screeninfo *var = &fbi->fb.var;
999 struct cpufreq_policy *policy = data;
1000
1001 switch (val) {
1002 case CPUFREQ_ADJUST:
1003 case CPUFREQ_INCOMPATIBLE:
Holger Schurigac2bf5b2008-02-11 16:52:30 +01001004 pr_debug("min dma period: %d ps, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 "new clock %d kHz\n", pxafb_display_dma_period(var),
1006 policy->max);
eric miaob0086ef2008-04-30 00:52:19 -07001007 /* TODO: fill in min/max values */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 }
1010 return 0;
1011}
1012#endif
1013
1014#ifdef CONFIG_PM
1015/*
1016 * Power management hooks. Note that we won't be called from IRQ context,
1017 * unlike the blank functions above, so we may sleep.
1018 */
Russell King3ae5eae2005-11-09 22:32:44 +00001019static int pxafb_suspend(struct platform_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
Russell King3ae5eae2005-11-09 22:32:44 +00001021 struct pxafb_info *fbi = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Russell King9480e302005-10-28 09:52:56 -07001023 set_ctrlr_state(fbi, C_DISABLE_PM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return 0;
1025}
1026
Russell King3ae5eae2005-11-09 22:32:44 +00001027static int pxafb_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
Russell King3ae5eae2005-11-09 22:32:44 +00001029 struct pxafb_info *fbi = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Russell King9480e302005-10-28 09:52:56 -07001031 set_ctrlr_state(fbi, C_ENABLE_PM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 return 0;
1033}
1034#else
1035#define pxafb_suspend NULL
1036#define pxafb_resume NULL
1037#endif
1038
1039/*
1040 * pxafb_map_video_memory():
1041 * Allocates the DRAM memory for the frame buffer. This buffer is
1042 * remapped into a non-cached, non-buffered, memory region to
1043 * allow palette and pixel writes to occur without flushing the
1044 * cache. Once this area is remapped, all virtual memory
1045 * access to the video memory should occur at the new region.
1046 */
1047static int __init pxafb_map_video_memory(struct pxafb_info *fbi)
1048{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 /*
1050 * We reserve one page for the palette, plus the size
1051 * of the framebuffer.
1052 */
1053 fbi->map_size = PAGE_ALIGN(fbi->fb.fix.smem_len + PAGE_SIZE);
1054 fbi->map_cpu = dma_alloc_writecombine(fbi->dev, fbi->map_size,
1055 &fbi->map_dma, GFP_KERNEL);
1056
1057 if (fbi->map_cpu) {
1058 /* prevent initial garbage on screen */
1059 memset(fbi->map_cpu, 0, fbi->map_size);
1060 fbi->fb.screen_base = fbi->map_cpu + PAGE_SIZE;
1061 fbi->screen_dma = fbi->map_dma + PAGE_SIZE;
1062 /*
1063 * FIXME: this is actually the wrong thing to place in
1064 * smem_start. But fbdev suffers from the problem that
1065 * it needs an API which doesn't exist (in this case,
1066 * dma_writecombine_mmap)
1067 */
1068 fbi->fb.fix.smem_start = fbi->screen_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 fbi->palette_size = fbi->fb.var.bits_per_pixel == 8 ? 256 : 16;
1070
eric miao2c42dd82008-04-30 00:52:21 -07001071 fbi->dma_buff = (void *)fbi->map_cpu;
1072 fbi->dma_buff_phys = fbi->map_dma;
1073 fbi->palette_cpu = (u16 *)&fbi->dma_buff->palette[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 }
1075
1076 return fbi->map_cpu ? 0 : -ENOMEM;
1077}
1078
eric miao84f43c32008-04-30 00:52:22 -07001079static void pxafb_decode_mode_info(struct pxafb_info *fbi,
1080 struct pxafb_mode_info *modes,
1081 unsigned int num_modes)
1082{
1083 unsigned int i, smemlen;
1084
1085 pxafb_setmode(&fbi->fb.var, &modes[0]);
1086
1087 for (i = 0; i < num_modes; i++) {
1088 smemlen = modes[i].xres * modes[i].yres * modes[i].bpp / 8;
1089 if (smemlen > fbi->fb.fix.smem_len)
1090 fbi->fb.fix.smem_len = smemlen;
1091 }
1092}
1093
1094static int pxafb_decode_mach_info(struct pxafb_info *fbi,
1095 struct pxafb_mach_info *inf)
1096{
1097 unsigned int lcd_conn = inf->lcd_conn;
1098
1099 fbi->cmap_inverse = inf->cmap_inverse;
1100 fbi->cmap_static = inf->cmap_static;
1101
1102 switch (lcd_conn & 0xf) {
1103 case LCD_TYPE_MONO_STN:
1104 fbi->lccr0 = LCCR0_CMS;
1105 break;
1106 case LCD_TYPE_MONO_DSTN:
1107 fbi->lccr0 = LCCR0_CMS | LCCR0_SDS;
1108 break;
1109 case LCD_TYPE_COLOR_STN:
1110 fbi->lccr0 = 0;
1111 break;
1112 case LCD_TYPE_COLOR_DSTN:
1113 fbi->lccr0 = LCCR0_SDS;
1114 break;
1115 case LCD_TYPE_COLOR_TFT:
1116 fbi->lccr0 = LCCR0_PAS;
1117 break;
1118 case LCD_TYPE_SMART_PANEL:
1119 fbi->lccr0 = LCCR0_LCDT | LCCR0_PAS;
1120 break;
1121 default:
1122 /* fall back to backward compatibility way */
1123 fbi->lccr0 = inf->lccr0;
1124 fbi->lccr3 = inf->lccr3;
1125 fbi->lccr4 = inf->lccr4;
1126 return -EINVAL;
1127 }
1128
1129 if (lcd_conn == LCD_MONO_STN_8BPP)
1130 fbi->lccr0 |= LCCR0_DPD;
1131
1132 fbi->lccr3 = LCCR3_Acb((inf->lcd_conn >> 10) & 0xff);
1133 fbi->lccr3 |= (lcd_conn & LCD_BIAS_ACTIVE_LOW) ? LCCR3_OEP : 0;
1134 fbi->lccr3 |= (lcd_conn & LCD_PCLK_EDGE_FALL) ? LCCR3_PCP : 0;
1135
1136 pxafb_decode_mode_info(fbi, inf->modes, inf->num_modes);
1137 return 0;
1138}
1139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140static struct pxafb_info * __init pxafb_init_fbinfo(struct device *dev)
1141{
1142 struct pxafb_info *fbi;
1143 void *addr;
1144 struct pxafb_mach_info *inf = dev->platform_data;
Richard Purdied14b2722006-09-20 22:54:21 +01001145 struct pxafb_mode_info *mode = inf->modes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147 /* Alloc the pxafb_info and pseudo_palette in one step */
1148 fbi = kmalloc(sizeof(struct pxafb_info) + sizeof(u32) * 16, GFP_KERNEL);
1149 if (!fbi)
1150 return NULL;
1151
1152 memset(fbi, 0, sizeof(struct pxafb_info));
1153 fbi->dev = dev;
1154
Russell King72e35242007-08-20 10:18:42 +01001155 fbi->clk = clk_get(dev, "LCDCLK");
1156 if (IS_ERR(fbi->clk)) {
1157 kfree(fbi);
1158 return NULL;
1159 }
1160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 strcpy(fbi->fb.fix.id, PXA_NAME);
1162
1163 fbi->fb.fix.type = FB_TYPE_PACKED_PIXELS;
1164 fbi->fb.fix.type_aux = 0;
1165 fbi->fb.fix.xpanstep = 0;
1166 fbi->fb.fix.ypanstep = 0;
1167 fbi->fb.fix.ywrapstep = 0;
1168 fbi->fb.fix.accel = FB_ACCEL_NONE;
1169
1170 fbi->fb.var.nonstd = 0;
1171 fbi->fb.var.activate = FB_ACTIVATE_NOW;
1172 fbi->fb.var.height = -1;
1173 fbi->fb.var.width = -1;
1174 fbi->fb.var.accel_flags = 0;
1175 fbi->fb.var.vmode = FB_VMODE_NONINTERLACED;
1176
1177 fbi->fb.fbops = &pxafb_ops;
1178 fbi->fb.flags = FBINFO_DEFAULT;
1179 fbi->fb.node = -1;
1180
1181 addr = fbi;
1182 addr = addr + sizeof(struct pxafb_info);
1183 fbi->fb.pseudo_palette = addr;
1184
eric miaob0086ef2008-04-30 00:52:19 -07001185 fbi->state = C_STARTUP;
1186 fbi->task_state = (u_char)-1;
Richard Purdied14b2722006-09-20 22:54:21 +01001187
eric miao84f43c32008-04-30 00:52:22 -07001188 pxafb_decode_mach_info(fbi, inf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 init_waitqueue_head(&fbi->ctrlr_wait);
David Howells6d5aefb2006-12-05 19:36:26 +00001191 INIT_WORK(&fbi->task, pxafb_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 init_MUTEX(&fbi->ctrlr_sem);
Eric Miao2ba162b2008-04-30 00:52:24 -07001193 init_completion(&fbi->disable_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195 return fbi;
1196}
1197
1198#ifdef CONFIG_FB_PXA_PARAMETERS
eric miaob0086ef2008-04-30 00:52:19 -07001199static int __init parse_opt_mode(struct device *dev, const char *this_opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
1201 struct pxafb_mach_info *inf = dev->platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
eric miao817daf12008-04-30 00:52:18 -07001203 const char *name = this_opt+5;
1204 unsigned int namelen = strlen(name);
1205 int res_specified = 0, bpp_specified = 0;
1206 unsigned int xres = 0, yres = 0, bpp = 0;
1207 int yres_specified = 0;
1208 int i;
1209 for (i = namelen-1; i >= 0; i--) {
1210 switch (name[i]) {
1211 case '-':
1212 namelen = i;
1213 if (!bpp_specified && !yres_specified) {
1214 bpp = simple_strtoul(&name[i+1], NULL, 0);
1215 bpp_specified = 1;
1216 } else
1217 goto done;
1218 break;
1219 case 'x':
1220 if (!yres_specified) {
1221 yres = simple_strtoul(&name[i+1], NULL, 0);
1222 yres_specified = 1;
1223 } else
1224 goto done;
1225 break;
1226 case '0' ... '9':
1227 break;
1228 default:
1229 goto done;
1230 }
1231 }
1232 if (i < 0 && yres_specified) {
1233 xres = simple_strtoul(name, NULL, 0);
1234 res_specified = 1;
1235 }
1236done:
1237 if (res_specified) {
1238 dev_info(dev, "overriding resolution: %dx%d\n", xres, yres);
1239 inf->modes[0].xres = xres; inf->modes[0].yres = yres;
1240 }
1241 if (bpp_specified)
1242 switch (bpp) {
1243 case 1:
1244 case 2:
1245 case 4:
1246 case 8:
1247 case 16:
1248 inf->modes[0].bpp = bpp;
1249 dev_info(dev, "overriding bit depth: %d\n", bpp);
1250 break;
1251 default:
1252 dev_err(dev, "Depth %d is not valid\n", bpp);
1253 return -EINVAL;
1254 }
1255 return 0;
1256}
1257
eric miaob0086ef2008-04-30 00:52:19 -07001258static int __init parse_opt(struct device *dev, char *this_opt)
eric miao817daf12008-04-30 00:52:18 -07001259{
1260 struct pxafb_mach_info *inf = dev->platform_data;
1261 struct pxafb_mode_info *mode = &inf->modes[0];
1262 char s[64];
1263
1264 s[0] = '\0';
1265
1266 if (!strncmp(this_opt, "mode:", 5)) {
1267 return parse_opt_mode(dev, this_opt);
1268 } else if (!strncmp(this_opt, "pixclock:", 9)) {
1269 mode->pixclock = simple_strtoul(this_opt+9, NULL, 0);
1270 sprintf(s, "pixclock: %ld\n", mode->pixclock);
1271 } else if (!strncmp(this_opt, "left:", 5)) {
1272 mode->left_margin = simple_strtoul(this_opt+5, NULL, 0);
1273 sprintf(s, "left: %u\n", mode->left_margin);
1274 } else if (!strncmp(this_opt, "right:", 6)) {
1275 mode->right_margin = simple_strtoul(this_opt+6, NULL, 0);
1276 sprintf(s, "right: %u\n", mode->right_margin);
1277 } else if (!strncmp(this_opt, "upper:", 6)) {
1278 mode->upper_margin = simple_strtoul(this_opt+6, NULL, 0);
1279 sprintf(s, "upper: %u\n", mode->upper_margin);
1280 } else if (!strncmp(this_opt, "lower:", 6)) {
1281 mode->lower_margin = simple_strtoul(this_opt+6, NULL, 0);
1282 sprintf(s, "lower: %u\n", mode->lower_margin);
1283 } else if (!strncmp(this_opt, "hsynclen:", 9)) {
1284 mode->hsync_len = simple_strtoul(this_opt+9, NULL, 0);
1285 sprintf(s, "hsynclen: %u\n", mode->hsync_len);
1286 } else if (!strncmp(this_opt, "vsynclen:", 9)) {
1287 mode->vsync_len = simple_strtoul(this_opt+9, NULL, 0);
1288 sprintf(s, "vsynclen: %u\n", mode->vsync_len);
1289 } else if (!strncmp(this_opt, "hsync:", 6)) {
1290 if (simple_strtoul(this_opt+6, NULL, 0) == 0) {
1291 sprintf(s, "hsync: Active Low\n");
1292 mode->sync &= ~FB_SYNC_HOR_HIGH_ACT;
1293 } else {
1294 sprintf(s, "hsync: Active High\n");
1295 mode->sync |= FB_SYNC_HOR_HIGH_ACT;
1296 }
1297 } else if (!strncmp(this_opt, "vsync:", 6)) {
1298 if (simple_strtoul(this_opt+6, NULL, 0) == 0) {
1299 sprintf(s, "vsync: Active Low\n");
1300 mode->sync &= ~FB_SYNC_VERT_HIGH_ACT;
1301 } else {
1302 sprintf(s, "vsync: Active High\n");
1303 mode->sync |= FB_SYNC_VERT_HIGH_ACT;
1304 }
1305 } else if (!strncmp(this_opt, "dpc:", 4)) {
1306 if (simple_strtoul(this_opt+4, NULL, 0) == 0) {
1307 sprintf(s, "double pixel clock: false\n");
1308 inf->lccr3 &= ~LCCR3_DPC;
1309 } else {
1310 sprintf(s, "double pixel clock: true\n");
1311 inf->lccr3 |= LCCR3_DPC;
1312 }
1313 } else if (!strncmp(this_opt, "outputen:", 9)) {
1314 if (simple_strtoul(this_opt+9, NULL, 0) == 0) {
1315 sprintf(s, "output enable: active low\n");
1316 inf->lccr3 = (inf->lccr3 & ~LCCR3_OEP) | LCCR3_OutEnL;
1317 } else {
1318 sprintf(s, "output enable: active high\n");
1319 inf->lccr3 = (inf->lccr3 & ~LCCR3_OEP) | LCCR3_OutEnH;
1320 }
1321 } else if (!strncmp(this_opt, "pixclockpol:", 12)) {
1322 if (simple_strtoul(this_opt+12, NULL, 0) == 0) {
1323 sprintf(s, "pixel clock polarity: falling edge\n");
1324 inf->lccr3 = (inf->lccr3 & ~LCCR3_PCP) | LCCR3_PixFlEdg;
1325 } else {
1326 sprintf(s, "pixel clock polarity: rising edge\n");
1327 inf->lccr3 = (inf->lccr3 & ~LCCR3_PCP) | LCCR3_PixRsEdg;
1328 }
1329 } else if (!strncmp(this_opt, "color", 5)) {
1330 inf->lccr0 = (inf->lccr0 & ~LCCR0_CMS) | LCCR0_Color;
1331 } else if (!strncmp(this_opt, "mono", 4)) {
1332 inf->lccr0 = (inf->lccr0 & ~LCCR0_CMS) | LCCR0_Mono;
1333 } else if (!strncmp(this_opt, "active", 6)) {
1334 inf->lccr0 = (inf->lccr0 & ~LCCR0_PAS) | LCCR0_Act;
1335 } else if (!strncmp(this_opt, "passive", 7)) {
1336 inf->lccr0 = (inf->lccr0 & ~LCCR0_PAS) | LCCR0_Pas;
1337 } else if (!strncmp(this_opt, "single", 6)) {
1338 inf->lccr0 = (inf->lccr0 & ~LCCR0_SDS) | LCCR0_Sngl;
1339 } else if (!strncmp(this_opt, "dual", 4)) {
1340 inf->lccr0 = (inf->lccr0 & ~LCCR0_SDS) | LCCR0_Dual;
1341 } else if (!strncmp(this_opt, "4pix", 4)) {
1342 inf->lccr0 = (inf->lccr0 & ~LCCR0_DPD) | LCCR0_4PixMono;
1343 } else if (!strncmp(this_opt, "8pix", 4)) {
1344 inf->lccr0 = (inf->lccr0 & ~LCCR0_DPD) | LCCR0_8PixMono;
1345 } else {
1346 dev_err(dev, "unknown option: %s\n", this_opt);
1347 return -EINVAL;
1348 }
1349
1350 if (s[0] != '\0')
1351 dev_info(dev, "override %s", s);
1352
1353 return 0;
1354}
1355
1356static int __init pxafb_parse_options(struct device *dev, char *options)
1357{
1358 char *this_opt;
1359 int ret;
1360
1361 if (!options || !*options)
1362 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364 dev_dbg(dev, "options are \"%s\"\n", options ? options : "null");
1365
1366 /* could be made table driven or similar?... */
eric miao817daf12008-04-30 00:52:18 -07001367 while ((this_opt = strsep(&options, ",")) != NULL) {
1368 ret = parse_opt(dev, this_opt);
1369 if (ret)
1370 return ret;
1371 }
1372 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373}
eric miao92ac73c2008-04-30 00:52:20 -07001374
1375static char g_options[256] __devinitdata = "";
1376
1377#ifndef CONFIG_MODULES
1378static int __devinit pxafb_setup_options(void)
1379{
1380 char *options = NULL;
1381
1382 if (fb_get_options("pxafb", &options))
1383 return -ENODEV;
1384
1385 if (options)
1386 strlcpy(g_options, options, sizeof(g_options));
1387
1388 return 0;
1389}
1390#else
1391#define pxafb_setup_options() (0)
1392
1393module_param_string(options, g_options, sizeof(g_options), 0);
1394MODULE_PARM_DESC(options, "LCD parameters (see Documentation/fb/pxafb.txt)");
1395#endif
1396
1397#else
1398#define pxafb_parse_options(...) (0)
1399#define pxafb_setup_options() (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400#endif
1401
Holger Schurigac2bf5b2008-02-11 16:52:30 +01001402static int __init pxafb_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
1404 struct pxafb_info *fbi;
1405 struct pxafb_mach_info *inf;
eric miaoce4fb7b2008-04-30 00:52:21 -07001406 struct resource *r;
1407 int irq, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Richard Purdie2cbbb3b2006-03-31 02:31:53 -08001409 dev_dbg(&dev->dev, "pxafb_probe\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Russell King3ae5eae2005-11-09 22:32:44 +00001411 inf = dev->dev.platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 ret = -ENOMEM;
1413 fbi = NULL;
1414 if (!inf)
1415 goto failed;
1416
Russell King3ae5eae2005-11-09 22:32:44 +00001417 ret = pxafb_parse_options(&dev->dev, g_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 if (ret < 0)
1419 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421#ifdef DEBUG_VAR
eric miaob0086ef2008-04-30 00:52:19 -07001422 /* Check for various illegal bit-combinations. Currently only
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 * a warning is given. */
1424
eric miaob0086ef2008-04-30 00:52:19 -07001425 if (inf->lccr0 & LCCR0_INVALID_CONFIG_MASK)
1426 dev_warn(&dev->dev, "machine LCCR0 setting contains "
1427 "illegal bits: %08x\n",
1428 inf->lccr0 & LCCR0_INVALID_CONFIG_MASK);
1429 if (inf->lccr3 & LCCR3_INVALID_CONFIG_MASK)
1430 dev_warn(&dev->dev, "machine LCCR3 setting contains "
1431 "illegal bits: %08x\n",
1432 inf->lccr3 & LCCR3_INVALID_CONFIG_MASK);
1433 if (inf->lccr0 & LCCR0_DPD &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 ((inf->lccr0 & LCCR0_PAS) != LCCR0_Pas ||
1435 (inf->lccr0 & LCCR0_SDS) != LCCR0_Sngl ||
1436 (inf->lccr0 & LCCR0_CMS) != LCCR0_Mono))
eric miaob0086ef2008-04-30 00:52:19 -07001437 dev_warn(&dev->dev, "Double Pixel Data (DPD) mode is "
1438 "only valid in passive mono"
1439 " single panel mode\n");
1440 if ((inf->lccr0 & LCCR0_PAS) == LCCR0_Act &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 (inf->lccr0 & LCCR0_SDS) == LCCR0_Dual)
eric miaob0086ef2008-04-30 00:52:19 -07001442 dev_warn(&dev->dev, "Dual panel only valid in passive mode\n");
1443 if ((inf->lccr0 & LCCR0_PAS) == LCCR0_Pas &&
1444 (inf->modes->upper_margin || inf->modes->lower_margin))
1445 dev_warn(&dev->dev, "Upper and lower margins must be 0 in "
1446 "passive mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447#endif
1448
eric miaob0086ef2008-04-30 00:52:19 -07001449 dev_dbg(&dev->dev, "got a %dx%dx%d LCD\n",
1450 inf->modes->xres,
1451 inf->modes->yres,
1452 inf->modes->bpp);
1453 if (inf->modes->xres == 0 ||
1454 inf->modes->yres == 0 ||
1455 inf->modes->bpp == 0) {
Russell King3ae5eae2005-11-09 22:32:44 +00001456 dev_err(&dev->dev, "Invalid resolution or bit depth\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 ret = -EINVAL;
1458 goto failed;
1459 }
1460 pxafb_backlight_power = inf->pxafb_backlight_power;
1461 pxafb_lcd_power = inf->pxafb_lcd_power;
Russell King3ae5eae2005-11-09 22:32:44 +00001462 fbi = pxafb_init_fbinfo(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 if (!fbi) {
eric miaob0086ef2008-04-30 00:52:19 -07001464 /* only reason for pxafb_init_fbinfo to fail is kmalloc */
Russell King3ae5eae2005-11-09 22:32:44 +00001465 dev_err(&dev->dev, "Failed to initialize framebuffer device\n");
eric miaob0086ef2008-04-30 00:52:19 -07001466 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 goto failed;
1468 }
1469
eric miaoce4fb7b2008-04-30 00:52:21 -07001470 r = platform_get_resource(dev, IORESOURCE_MEM, 0);
1471 if (r == NULL) {
1472 dev_err(&dev->dev, "no I/O memory resource defined\n");
1473 ret = -ENODEV;
1474 goto failed;
1475 }
1476
1477 r = request_mem_region(r->start, r->end - r->start + 1, dev->name);
1478 if (r == NULL) {
1479 dev_err(&dev->dev, "failed to request I/O memory\n");
1480 ret = -EBUSY;
1481 goto failed;
1482 }
1483
1484 fbi->mmio_base = ioremap(r->start, r->end - r->start + 1);
1485 if (fbi->mmio_base == NULL) {
1486 dev_err(&dev->dev, "failed to map I/O memory\n");
1487 ret = -EBUSY;
1488 goto failed_free_res;
1489 }
1490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 /* Initialize video memory */
1492 ret = pxafb_map_video_memory(fbi);
1493 if (ret) {
Russell King3ae5eae2005-11-09 22:32:44 +00001494 dev_err(&dev->dev, "Failed to allocate video RAM: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 ret = -ENOMEM;
eric miaoce4fb7b2008-04-30 00:52:21 -07001496 goto failed_free_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
eric miaoce4fb7b2008-04-30 00:52:21 -07001499 irq = platform_get_irq(dev, 0);
1500 if (irq < 0) {
1501 dev_err(&dev->dev, "no IRQ defined\n");
1502 ret = -ENODEV;
1503 goto failed_free_mem;
1504 }
1505
1506 ret = request_irq(irq, pxafb_handle_irq, IRQF_DISABLED, "LCD", fbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 if (ret) {
Russell King3ae5eae2005-11-09 22:32:44 +00001508 dev_err(&dev->dev, "request_irq failed: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 ret = -EBUSY;
eric miaoce4fb7b2008-04-30 00:52:21 -07001510 goto failed_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 }
1512
1513 /*
1514 * This makes sure that our colour bitfield
1515 * descriptors are correctly initialised.
1516 */
1517 pxafb_check_var(&fbi->fb.var, &fbi->fb);
1518 pxafb_set_par(&fbi->fb);
1519
Russell King3ae5eae2005-11-09 22:32:44 +00001520 platform_set_drvdata(dev, fbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522 ret = register_framebuffer(&fbi->fb);
1523 if (ret < 0) {
eric miaob0086ef2008-04-30 00:52:19 -07001524 dev_err(&dev->dev,
1525 "Failed to register framebuffer device: %d\n", ret);
eric miaoce4fb7b2008-04-30 00:52:21 -07001526 goto failed_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 }
1528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529#ifdef CONFIG_CPU_FREQ
1530 fbi->freq_transition.notifier_call = pxafb_freq_transition;
1531 fbi->freq_policy.notifier_call = pxafb_freq_policy;
eric miaob0086ef2008-04-30 00:52:19 -07001532 cpufreq_register_notifier(&fbi->freq_transition,
1533 CPUFREQ_TRANSITION_NOTIFIER);
1534 cpufreq_register_notifier(&fbi->freq_policy,
1535 CPUFREQ_POLICY_NOTIFIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536#endif
1537
1538 /*
1539 * Ok, now enable the LCD controller
1540 */
1541 set_ctrlr_state(fbi, C_ENABLE);
1542
1543 return 0;
1544
eric miaoce4fb7b2008-04-30 00:52:21 -07001545failed_free_irq:
1546 free_irq(irq, fbi);
1547failed_free_res:
1548 release_mem_region(r->start, r->end - r->start + 1);
1549failed_free_io:
1550 iounmap(fbi->mmio_base);
1551failed_free_mem:
1552 dma_free_writecombine(&dev->dev, fbi->map_size,
1553 fbi->map_cpu, fbi->map_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554failed:
Russell King3ae5eae2005-11-09 22:32:44 +00001555 platform_set_drvdata(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 kfree(fbi);
1557 return ret;
1558}
1559
Russell King3ae5eae2005-11-09 22:32:44 +00001560static struct platform_driver pxafb_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 .probe = pxafb_probe,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 .suspend = pxafb_suspend,
1563 .resume = pxafb_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001564 .driver = {
1565 .name = "pxa2xx-fb",
1566 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567};
1568
Holger Schurigac2bf5b2008-02-11 16:52:30 +01001569static int __devinit pxafb_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570{
eric miao92ac73c2008-04-30 00:52:20 -07001571 if (pxafb_setup_options())
1572 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Russell King3ae5eae2005-11-09 22:32:44 +00001574 return platform_driver_register(&pxafb_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575}
1576
1577module_init(pxafb_init);
1578
1579MODULE_DESCRIPTION("loadable framebuffer driver for PXA");
1580MODULE_LICENSE("GPL");