blob: 0031a5fefa263623f91a98ad87a14af4041f78d9 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <asm/hardware.h>
44#include <asm/io.h>
45#include <asm/irq.h>
Nicolas Pitrebf1b8ab2005-06-23 21:56:45 +010046#include <asm/div64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/arch/pxa-regs.h>
eric miaoa683b142008-03-03 09:44:25 +080048#include <asm/arch/pxa2xx-gpio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/arch/bitfield.h>
50#include <asm/arch/pxafb.h>
51
52/*
53 * Complain if VAR is out of range.
54 */
55#define DEBUG_VAR 1
56
57#include "pxafb.h"
58
59/* Bits which should not be set in machine configuration structures */
eric miaob0086ef2008-04-30 00:52:19 -070060#define LCCR0_INVALID_CONFIG_MASK (LCCR0_OUM | LCCR0_BM | LCCR0_QDM |\
61 LCCR0_DIS | LCCR0_EFM | LCCR0_IUM |\
62 LCCR0_SFM | LCCR0_LDM | LCCR0_ENB)
63
64#define LCCR3_INVALID_CONFIG_MASK (LCCR3_HSP | LCCR3_VSP |\
65 LCCR3_PCD | LCCR3_BPP)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67static void (*pxafb_backlight_power)(int);
Richard Purdied14b2722006-09-20 22:54:21 +010068static void (*pxafb_lcd_power)(int, struct fb_var_screeninfo *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
eric miaob0086ef2008-04-30 00:52:19 -070070static int pxafb_activate_var(struct fb_var_screeninfo *var,
71 struct pxafb_info *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static void set_ctrlr_state(struct pxafb_info *fbi, u_int state);
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static inline void pxafb_schedule_work(struct pxafb_info *fbi, u_int state)
75{
76 unsigned long flags;
77
78 local_irq_save(flags);
79 /*
80 * We need to handle two requests being made at the same time.
81 * There are two important cases:
eric miaob0086ef2008-04-30 00:52:19 -070082 * 1. When we are changing VT (C_REENABLE) while unblanking
83 * (C_ENABLE) We must perform the unblanking, which will
84 * do our REENABLE for us.
85 * 2. When we are blanking, but immediately unblank before
86 * we have blanked. We do the "REENABLE" thing here as
87 * well, just to be sure.
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 */
89 if (fbi->task_state == C_ENABLE && state == C_REENABLE)
90 state = (u_int) -1;
91 if (fbi->task_state == C_DISABLE && state == C_ENABLE)
92 state = C_REENABLE;
93
94 if (state != (u_int)-1) {
95 fbi->task_state = state;
96 schedule_work(&fbi->task);
97 }
98 local_irq_restore(flags);
99}
100
101static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
102{
103 chan &= 0xffff;
104 chan >>= 16 - bf->length;
105 return chan << bf->offset;
106}
107
108static int
109pxafb_setpalettereg(u_int regno, u_int red, u_int green, u_int blue,
110 u_int trans, struct fb_info *info)
111{
112 struct pxafb_info *fbi = (struct pxafb_info *)info;
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700113 u_int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700115 if (regno >= fbi->palette_size)
116 return 1;
117
118 if (fbi->fb.var.grayscale) {
119 fbi->palette_cpu[regno] = ((blue >> 8) & 0x00ff);
120 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700122
123 switch (fbi->lccr4 & LCCR4_PAL_FOR_MASK) {
124 case LCCR4_PAL_FOR_0:
125 val = ((red >> 0) & 0xf800);
126 val |= ((green >> 5) & 0x07e0);
127 val |= ((blue >> 11) & 0x001f);
128 fbi->palette_cpu[regno] = val;
129 break;
130 case LCCR4_PAL_FOR_1:
131 val = ((red << 8) & 0x00f80000);
132 val |= ((green >> 0) & 0x0000fc00);
133 val |= ((blue >> 8) & 0x000000f8);
eric miaob0086ef2008-04-30 00:52:19 -0700134 ((u32 *)(fbi->palette_cpu))[regno] = val;
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700135 break;
136 case LCCR4_PAL_FOR_2:
137 val = ((red << 8) & 0x00fc0000);
138 val |= ((green >> 0) & 0x0000fc00);
139 val |= ((blue >> 8) & 0x000000fc);
eric miaob0086ef2008-04-30 00:52:19 -0700140 ((u32 *)(fbi->palette_cpu))[regno] = val;
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700141 break;
142 }
143
144 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
147static int
148pxafb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
149 u_int trans, struct fb_info *info)
150{
151 struct pxafb_info *fbi = (struct pxafb_info *)info;
152 unsigned int val;
153 int ret = 1;
154
155 /*
156 * If inverse mode was selected, invert all the colours
157 * rather than the register number. The register number
158 * is what you poke into the framebuffer to produce the
159 * colour you requested.
160 */
161 if (fbi->cmap_inverse) {
162 red = 0xffff - red;
163 green = 0xffff - green;
164 blue = 0xffff - blue;
165 }
166
167 /*
168 * If greyscale is true, then we convert the RGB value
169 * to greyscale no matter what visual we are using.
170 */
171 if (fbi->fb.var.grayscale)
172 red = green = blue = (19595 * red + 38470 * green +
173 7471 * blue) >> 16;
174
175 switch (fbi->fb.fix.visual) {
176 case FB_VISUAL_TRUECOLOR:
177 /*
178 * 16-bit True Colour. We encode the RGB value
179 * according to the RGB bitfield information.
180 */
181 if (regno < 16) {
182 u32 *pal = fbi->fb.pseudo_palette;
183
184 val = chan_to_field(red, &fbi->fb.var.red);
185 val |= chan_to_field(green, &fbi->fb.var.green);
186 val |= chan_to_field(blue, &fbi->fb.var.blue);
187
188 pal[regno] = val;
189 ret = 0;
190 }
191 break;
192
193 case FB_VISUAL_STATIC_PSEUDOCOLOR:
194 case FB_VISUAL_PSEUDOCOLOR:
195 ret = pxafb_setpalettereg(regno, red, green, blue, trans, info);
196 break;
197 }
198
199 return ret;
200}
201
202/*
203 * pxafb_bpp_to_lccr3():
204 * Convert a bits per pixel value to the correct bit pattern for LCCR3
205 */
206static int pxafb_bpp_to_lccr3(struct fb_var_screeninfo *var)
207{
eric miaob0086ef2008-04-30 00:52:19 -0700208 int ret = 0;
209 switch (var->bits_per_pixel) {
210 case 1: ret = LCCR3_1BPP; break;
211 case 2: ret = LCCR3_2BPP; break;
212 case 4: ret = LCCR3_4BPP; break;
213 case 8: ret = LCCR3_8BPP; break;
214 case 16: ret = LCCR3_16BPP; break;
215 }
216 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
219#ifdef CONFIG_CPU_FREQ
220/*
221 * pxafb_display_dma_period()
222 * Calculate the minimum period (in picoseconds) between two DMA
223 * requests for the LCD controller. If we hit this, it means we're
224 * doing nothing but LCD DMA.
225 */
226static unsigned int pxafb_display_dma_period(struct fb_var_screeninfo *var)
227{
eric miaob0086ef2008-04-30 00:52:19 -0700228 /*
229 * Period = pixclock * bits_per_byte * bytes_per_transfer
230 * / memory_bits_per_pixel;
231 */
232 return var->pixclock * 8 * 16 / var->bits_per_pixel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234#endif
235
236/*
Richard Purdied14b2722006-09-20 22:54:21 +0100237 * Select the smallest mode that allows the desired resolution to be
238 * displayed. If desired parameters can be rounded up.
239 */
eric miaob0086ef2008-04-30 00:52:19 -0700240static struct pxafb_mode_info *pxafb_getmode(struct pxafb_mach_info *mach,
241 struct fb_var_screeninfo *var)
Richard Purdied14b2722006-09-20 22:54:21 +0100242{
243 struct pxafb_mode_info *mode = NULL;
244 struct pxafb_mode_info *modelist = mach->modes;
245 unsigned int best_x = 0xffffffff, best_y = 0xffffffff;
246 unsigned int i;
247
eric miaob0086ef2008-04-30 00:52:19 -0700248 for (i = 0; i < mach->num_modes; i++) {
249 if (modelist[i].xres >= var->xres &&
250 modelist[i].yres >= var->yres &&
251 modelist[i].xres < best_x &&
252 modelist[i].yres < best_y &&
253 modelist[i].bpp >= var->bits_per_pixel) {
Richard Purdied14b2722006-09-20 22:54:21 +0100254 best_x = modelist[i].xres;
255 best_y = modelist[i].yres;
256 mode = &modelist[i];
257 }
258 }
259
260 return mode;
261}
262
eric miaob0086ef2008-04-30 00:52:19 -0700263static void pxafb_setmode(struct fb_var_screeninfo *var,
264 struct pxafb_mode_info *mode)
Richard Purdied14b2722006-09-20 22:54:21 +0100265{
266 var->xres = mode->xres;
267 var->yres = mode->yres;
268 var->bits_per_pixel = mode->bpp;
269 var->pixclock = mode->pixclock;
270 var->hsync_len = mode->hsync_len;
271 var->left_margin = mode->left_margin;
272 var->right_margin = mode->right_margin;
273 var->vsync_len = mode->vsync_len;
274 var->upper_margin = mode->upper_margin;
275 var->lower_margin = mode->lower_margin;
276 var->sync = mode->sync;
277 var->grayscale = mode->cmap_greyscale;
278 var->xres_virtual = var->xres;
279 var->yres_virtual = var->yres;
280}
281
282/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 * pxafb_check_var():
284 * Get the video params out of 'var'. If a value doesn't fit, round it up,
285 * if it's too big, return -EINVAL.
286 *
287 * Round up in the following order: bits_per_pixel, xres,
288 * yres, xres_virtual, yres_virtual, xoffset, yoffset, grayscale,
289 * bitfields, horizontal timing, vertical timing.
290 */
291static int pxafb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
292{
293 struct pxafb_info *fbi = (struct pxafb_info *)info;
Richard Purdied14b2722006-09-20 22:54:21 +0100294 struct pxafb_mach_info *inf = fbi->dev->platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 if (var->xres < MIN_XRES)
297 var->xres = MIN_XRES;
298 if (var->yres < MIN_YRES)
299 var->yres = MIN_YRES;
Richard Purdied14b2722006-09-20 22:54:21 +0100300
301 if (inf->fixed_modes) {
302 struct pxafb_mode_info *mode;
303
304 mode = pxafb_getmode(inf, var);
305 if (!mode)
306 return -EINVAL;
307 pxafb_setmode(var, mode);
308 } else {
309 if (var->xres > inf->modes->xres)
310 return -EINVAL;
311 if (var->yres > inf->modes->yres)
312 return -EINVAL;
313 if (var->bits_per_pixel > inf->modes->bpp)
314 return -EINVAL;
315 }
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 var->xres_virtual =
318 max(var->xres_virtual, var->xres);
319 var->yres_virtual =
320 max(var->yres_virtual, var->yres);
321
eric miaob0086ef2008-04-30 00:52:19 -0700322 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 * Setup the RGB parameters for this display.
324 *
325 * The pixel packing format is described on page 7-11 of the
326 * PXA2XX Developer's Manual.
eric miaob0086ef2008-04-30 00:52:19 -0700327 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 if (var->bits_per_pixel == 16) {
329 var->red.offset = 11; var->red.length = 5;
330 var->green.offset = 5; var->green.length = 6;
331 var->blue.offset = 0; var->blue.length = 5;
332 var->transp.offset = var->transp.length = 0;
333 } else {
eric miaob0086ef2008-04-30 00:52:19 -0700334 var->red.offset = var->green.offset = 0;
335 var->blue.offset = var->transp.offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 var->red.length = 8;
337 var->green.length = 8;
338 var->blue.length = 8;
339 var->transp.length = 0;
340 }
341
342#ifdef CONFIG_CPU_FREQ
Russell Kingca5da712005-09-29 09:44:54 +0100343 pr_debug("pxafb: dma period = %d ps, clock = %d kHz\n",
344 pxafb_display_dma_period(var),
345 get_clk_frequency_khz(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346#endif
347
348 return 0;
349}
350
351static inline void pxafb_set_truecolor(u_int is_true_color)
352{
eric miaob0086ef2008-04-30 00:52:19 -0700353 /* do your machine-specific setup if needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
356/*
357 * pxafb_set_par():
358 * Set the user defined part of the display for the specified console
359 */
360static int pxafb_set_par(struct fb_info *info)
361{
362 struct pxafb_info *fbi = (struct pxafb_info *)info;
363 struct fb_var_screeninfo *var = &info->var;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (var->bits_per_pixel == 16)
366 fbi->fb.fix.visual = FB_VISUAL_TRUECOLOR;
367 else if (!fbi->cmap_static)
368 fbi->fb.fix.visual = FB_VISUAL_PSEUDOCOLOR;
369 else {
370 /*
371 * Some people have weird ideas about wanting static
372 * pseudocolor maps. I suspect their user space
373 * applications are broken.
374 */
375 fbi->fb.fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
376 }
377
378 fbi->fb.fix.line_length = var->xres_virtual *
379 var->bits_per_pixel / 8;
380 if (var->bits_per_pixel == 16)
381 fbi->palette_size = 0;
382 else
eric miaob0086ef2008-04-30 00:52:19 -0700383 fbi->palette_size = var->bits_per_pixel == 1 ?
384 4 : 1 << var->bits_per_pixel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
eric miao2c42dd82008-04-30 00:52:21 -0700386 fbi->palette_cpu = (u16 *)&fbi->dma_buff->palette[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 /*
389 * Set (any) board control register to handle new color depth
390 */
391 pxafb_set_truecolor(fbi->fb.fix.visual == FB_VISUAL_TRUECOLOR);
392
393 if (fbi->fb.var.bits_per_pixel == 16)
394 fb_dealloc_cmap(&fbi->fb.cmap);
395 else
396 fb_alloc_cmap(&fbi->fb.cmap, 1<<fbi->fb.var.bits_per_pixel, 0);
397
398 pxafb_activate_var(var, fbi);
399
400 return 0;
401}
402
403/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 * pxafb_blank():
405 * Blank the display by setting all palette values to zero. Note, the
406 * 16 bpp mode does not really use the palette, so this will not
407 * blank the display in all modes.
408 */
409static int pxafb_blank(int blank, struct fb_info *info)
410{
411 struct pxafb_info *fbi = (struct pxafb_info *)info;
412 int i;
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 switch (blank) {
415 case FB_BLANK_POWERDOWN:
416 case FB_BLANK_VSYNC_SUSPEND:
417 case FB_BLANK_HSYNC_SUSPEND:
418 case FB_BLANK_NORMAL:
419 if (fbi->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR ||
420 fbi->fb.fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
421 for (i = 0; i < fbi->palette_size; i++)
422 pxafb_setpalettereg(i, 0, 0, 0, 0, info);
423
424 pxafb_schedule_work(fbi, C_DISABLE);
eric miaob0086ef2008-04-30 00:52:19 -0700425 /* TODO if (pxafb_blank_helper) pxafb_blank_helper(blank); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 break;
427
428 case FB_BLANK_UNBLANK:
eric miaob0086ef2008-04-30 00:52:19 -0700429 /* TODO if (pxafb_blank_helper) pxafb_blank_helper(blank); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if (fbi->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR ||
431 fbi->fb.fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
432 fb_set_cmap(&fbi->fb.cmap, info);
433 pxafb_schedule_work(fbi, C_ENABLE);
434 }
435 return 0;
436}
437
Christoph Hellwig216d5262006-01-14 13:21:25 -0800438static int pxafb_mmap(struct fb_info *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 struct vm_area_struct *vma)
440{
441 struct pxafb_info *fbi = (struct pxafb_info *)info;
442 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
443
444 if (off < info->fix.smem_len) {
445 vma->vm_pgoff += 1;
446 return dma_mmap_writecombine(fbi->dev, vma, fbi->map_cpu,
447 fbi->map_dma, fbi->map_size);
448 }
449 return -EINVAL;
450}
451
452static struct fb_ops pxafb_ops = {
453 .owner = THIS_MODULE,
454 .fb_check_var = pxafb_check_var,
455 .fb_set_par = pxafb_set_par,
456 .fb_setcolreg = pxafb_setcolreg,
457 .fb_fillrect = cfb_fillrect,
458 .fb_copyarea = cfb_copyarea,
459 .fb_imageblit = cfb_imageblit,
460 .fb_blank = pxafb_blank,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 .fb_mmap = pxafb_mmap,
462};
463
464/*
465 * Calculate the PCD value from the clock rate (in picoseconds).
466 * We take account of the PPCR clock setting.
467 * From PXA Developer's Manual:
468 *
469 * PixelClock = LCLK
470 * -------------
471 * 2 ( PCD + 1 )
472 *
473 * PCD = LCLK
474 * ------------- - 1
475 * 2(PixelClock)
476 *
477 * Where:
478 * LCLK = LCD/Memory Clock
479 * PCD = LCCR3[7:0]
480 *
481 * PixelClock here is in Hz while the pixclock argument given is the
482 * period in picoseconds. Hence PixelClock = 1 / ( pixclock * 10^-12 )
483 *
484 * The function get_lclk_frequency_10khz returns LCLK in units of
485 * 10khz. Calling the result of this function lclk gives us the
486 * following
487 *
488 * PCD = (lclk * 10^4 ) * ( pixclock * 10^-12 )
489 * -------------------------------------- - 1
490 * 2
491 *
492 * Factoring the 10^4 and 10^-12 out gives 10^-8 == 1 / 100000000 as used below.
493 */
eric miaob0086ef2008-04-30 00:52:19 -0700494static inline unsigned int get_pcd(struct pxafb_info *fbi,
495 unsigned int pixclock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
497 unsigned long long pcd;
498
499 /* FIXME: Need to take into account Double Pixel Clock mode
Russell King72e35242007-08-20 10:18:42 +0100500 * (DPC) bit? or perhaps set it based on the various clock
501 * speeds */
502 pcd = (unsigned long long)(clk_get_rate(fbi->clk) / 10000);
503 pcd *= pixclock;
Nicolas Pitrebf1b8ab2005-06-23 21:56:45 +0100504 do_div(pcd, 100000000 * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 /* no need for this, since we should subtract 1 anyway. they cancel */
506 /* pcd += 1; */ /* make up for integer math truncations */
507 return (unsigned int)pcd;
508}
509
510/*
Richard Purdieba44cd22005-09-09 13:10:03 -0700511 * Some touchscreens need hsync information from the video driver to
Russell King72e35242007-08-20 10:18:42 +0100512 * function correctly. We export it here. Note that 'hsync_time' and
513 * the value returned from pxafb_get_hsync_time() is the *reciprocal*
514 * of the hsync period in seconds.
Richard Purdieba44cd22005-09-09 13:10:03 -0700515 */
516static inline void set_hsync_time(struct pxafb_info *fbi, unsigned int pcd)
517{
Russell King72e35242007-08-20 10:18:42 +0100518 unsigned long htime;
Richard Purdieba44cd22005-09-09 13:10:03 -0700519
520 if ((pcd == 0) || (fbi->fb.var.hsync_len == 0)) {
eric miaob0086ef2008-04-30 00:52:19 -0700521 fbi->hsync_time = 0;
Richard Purdieba44cd22005-09-09 13:10:03 -0700522 return;
523 }
524
Russell King72e35242007-08-20 10:18:42 +0100525 htime = clk_get_rate(fbi->clk) / (pcd * fbi->fb.var.hsync_len);
526
Richard Purdieba44cd22005-09-09 13:10:03 -0700527 fbi->hsync_time = htime;
528}
529
530unsigned long pxafb_get_hsync_time(struct device *dev)
531{
532 struct pxafb_info *fbi = dev_get_drvdata(dev);
533
534 /* If display is blanked/suspended, hsync isn't active */
535 if (!fbi || (fbi->state != C_ENABLE))
536 return 0;
537
538 return fbi->hsync_time;
539}
540EXPORT_SYMBOL(pxafb_get_hsync_time);
541
eric miao2c42dd82008-04-30 00:52:21 -0700542static int setup_frame_dma(struct pxafb_info *fbi, int dma, int pal,
543 unsigned int offset, size_t size)
544{
545 struct pxafb_dma_descriptor *dma_desc, *pal_desc;
546 unsigned int dma_desc_off, pal_desc_off;
547
548 if (dma < 0 || dma >= DMA_MAX)
549 return -EINVAL;
550
551 dma_desc = &fbi->dma_buff->dma_desc[dma];
552 dma_desc_off = offsetof(struct pxafb_dma_buff, dma_desc[dma]);
553
554 dma_desc->fsadr = fbi->screen_dma + offset;
555 dma_desc->fidr = 0;
556 dma_desc->ldcmd = size;
557
558 if (pal < 0 || pal >= PAL_MAX) {
559 dma_desc->fdadr = fbi->dma_buff_phys + dma_desc_off;
560 fbi->fdadr[dma] = fbi->dma_buff_phys + dma_desc_off;
561 } else {
562 pal_desc = &fbi->dma_buff->pal_desc[dma];
563 pal_desc_off = offsetof(struct pxafb_dma_buff, dma_desc[pal]);
564
565 pal_desc->fsadr = fbi->dma_buff_phys + pal * PALETTE_SIZE;
566 pal_desc->fidr = 0;
567
568 if ((fbi->lccr4 & LCCR4_PAL_FOR_MASK) == LCCR4_PAL_FOR_0)
569 pal_desc->ldcmd = fbi->palette_size * sizeof(u16);
570 else
571 pal_desc->ldcmd = fbi->palette_size * sizeof(u32);
572
573 pal_desc->ldcmd |= LDCMD_PAL;
574
575 /* flip back and forth between palette and frame buffer */
576 pal_desc->fdadr = fbi->dma_buff_phys + dma_desc_off;
577 dma_desc->fdadr = fbi->dma_buff_phys + pal_desc_off;
578 fbi->fdadr[dma] = fbi->dma_buff_phys + dma_desc_off;
579 }
580
581 return 0;
582}
583
Richard Purdieba44cd22005-09-09 13:10:03 -0700584/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 * pxafb_activate_var():
eric miaob0086ef2008-04-30 00:52:19 -0700586 * Configures LCD Controller based on entries in var parameter.
587 * Settings are only written to the controller if changes were made.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 */
eric miaob0086ef2008-04-30 00:52:19 -0700589static int pxafb_activate_var(struct fb_var_screeninfo *var,
590 struct pxafb_info *fbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
592 struct pxafb_lcd_reg new_regs;
593 u_long flags;
Russell King72e35242007-08-20 10:18:42 +0100594 u_int lines_per_panel, pcd = get_pcd(fbi, var->pixclock);
eric miao2c42dd82008-04-30 00:52:21 -0700595 size_t nbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597#if DEBUG_VAR
eric miaob0086ef2008-04-30 00:52:19 -0700598 if (var->xres < 16 || var->xres > 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 printk(KERN_ERR "%s: invalid xres %d\n",
600 fbi->fb.fix.id, var->xres);
eric miaob0086ef2008-04-30 00:52:19 -0700601 switch (var->bits_per_pixel) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 case 1:
603 case 2:
604 case 4:
605 case 8:
606 case 16:
607 break;
608 default:
609 printk(KERN_ERR "%s: invalid bit depth %d\n",
610 fbi->fb.fix.id, var->bits_per_pixel);
611 break;
612 }
eric miaob0086ef2008-04-30 00:52:19 -0700613 if (var->hsync_len < 1 || var->hsync_len > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 printk(KERN_ERR "%s: invalid hsync_len %d\n",
615 fbi->fb.fix.id, var->hsync_len);
eric miaob0086ef2008-04-30 00:52:19 -0700616 if (var->left_margin < 1 || var->left_margin > 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 printk(KERN_ERR "%s: invalid left_margin %d\n",
618 fbi->fb.fix.id, var->left_margin);
619 if (var->right_margin < 1 || var->right_margin > 255)
620 printk(KERN_ERR "%s: invalid right_margin %d\n",
621 fbi->fb.fix.id, var->right_margin);
eric miaob0086ef2008-04-30 00:52:19 -0700622 if (var->yres < 1 || var->yres > 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 printk(KERN_ERR "%s: invalid yres %d\n",
624 fbi->fb.fix.id, var->yres);
eric miaob0086ef2008-04-30 00:52:19 -0700625 if (var->vsync_len < 1 || var->vsync_len > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 printk(KERN_ERR "%s: invalid vsync_len %d\n",
627 fbi->fb.fix.id, var->vsync_len);
628 if (var->upper_margin < 0 || var->upper_margin > 255)
629 printk(KERN_ERR "%s: invalid upper_margin %d\n",
630 fbi->fb.fix.id, var->upper_margin);
631 if (var->lower_margin < 0 || var->lower_margin > 255)
632 printk(KERN_ERR "%s: invalid lower_margin %d\n",
633 fbi->fb.fix.id, var->lower_margin);
634#endif
635
636 new_regs.lccr0 = fbi->lccr0 |
637 (LCCR0_LDM | LCCR0_SFM | LCCR0_IUM | LCCR0_EFM |
eric miaob0086ef2008-04-30 00:52:19 -0700638 LCCR0_QDM | LCCR0_BM | LCCR0_OUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 new_regs.lccr1 =
641 LCCR1_DisWdth(var->xres) +
642 LCCR1_HorSnchWdth(var->hsync_len) +
643 LCCR1_BegLnDel(var->left_margin) +
644 LCCR1_EndLnDel(var->right_margin);
645
646 /*
647 * If we have a dual scan LCD, we need to halve
648 * the YRES parameter.
649 */
650 lines_per_panel = var->yres;
651 if ((fbi->lccr0 & LCCR0_SDS) == LCCR0_Dual)
652 lines_per_panel /= 2;
653
654 new_regs.lccr2 =
655 LCCR2_DisHght(lines_per_panel) +
656 LCCR2_VrtSnchWdth(var->vsync_len) +
657 LCCR2_BegFrmDel(var->upper_margin) +
658 LCCR2_EndFrmDel(var->lower_margin);
659
660 new_regs.lccr3 = fbi->lccr3 |
661 pxafb_bpp_to_lccr3(var) |
eric miaob0086ef2008-04-30 00:52:19 -0700662 (var->sync & FB_SYNC_HOR_HIGH_ACT ?
663 LCCR3_HorSnchH : LCCR3_HorSnchL) |
664 (var->sync & FB_SYNC_VERT_HIGH_ACT ?
665 LCCR3_VrtSnchH : LCCR3_VrtSnchL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 if (pcd)
668 new_regs.lccr3 |= LCCR3_PixClkDiv(pcd);
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 /* Update shadow copy atomically */
671 local_irq_save(flags);
672
eric miao2c42dd82008-04-30 00:52:21 -0700673 nbytes = lines_per_panel * fbi->fb.fix.line_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
eric miao2c42dd82008-04-30 00:52:21 -0700675 if ((fbi->lccr0 & LCCR0_SDS) == LCCR0_Dual)
676 setup_frame_dma(fbi, DMA_LOWER, PAL_NONE, nbytes, nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
eric miao2c42dd82008-04-30 00:52:21 -0700678 if (var->bits_per_pixel >= 16)
679 setup_frame_dma(fbi, DMA_BASE, PAL_NONE, 0, nbytes);
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700680 else
eric miao2c42dd82008-04-30 00:52:21 -0700681 setup_frame_dma(fbi, DMA_BASE, PAL_BASE, 0, nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 fbi->reg_lccr0 = new_regs.lccr0;
684 fbi->reg_lccr1 = new_regs.lccr1;
685 fbi->reg_lccr2 = new_regs.lccr2;
686 fbi->reg_lccr3 = new_regs.lccr3;
eric miaoce4fb7b2008-04-30 00:52:21 -0700687 fbi->reg_lccr4 = __raw_readl(fbi->mmio_base + LCCR4) &
688 (~LCCR4_PAL_FOR_MASK);
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700689 fbi->reg_lccr4 |= (fbi->lccr4 & LCCR4_PAL_FOR_MASK);
Richard Purdieba44cd22005-09-09 13:10:03 -0700690 set_hsync_time(fbi, pcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 local_irq_restore(flags);
692
693 /*
694 * Only update the registers if the controller is enabled
695 * and something has changed.
696 */
eric miaoce4fb7b2008-04-30 00:52:21 -0700697 if ((__raw_readl(fbi->mmio_base + LCCR0) != fbi->reg_lccr0) ||
698 (__raw_readl(fbi->mmio_base + LCCR1) != fbi->reg_lccr1) ||
699 (__raw_readl(fbi->mmio_base + LCCR2) != fbi->reg_lccr2) ||
700 (__raw_readl(fbi->mmio_base + LCCR3) != fbi->reg_lccr3) ||
eric miao2c42dd82008-04-30 00:52:21 -0700701 (__raw_readl(fbi->mmio_base + FDADR0) != fbi->fdadr[0]) ||
702 (__raw_readl(fbi->mmio_base + FDADR1) != fbi->fdadr[1]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 pxafb_schedule_work(fbi, C_REENABLE);
704
705 return 0;
706}
707
708/*
709 * NOTE! The following functions are purely helpers for set_ctrlr_state.
710 * Do not call them directly; set_ctrlr_state does the correct serialisation
711 * to ensure that things happen in the right way 100% of time time.
712 * -- rmk
713 */
714static inline void __pxafb_backlight_power(struct pxafb_info *fbi, int on)
715{
Russell Kingca5da712005-09-29 09:44:54 +0100716 pr_debug("pxafb: backlight o%s\n", on ? "n" : "ff");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
eric miaob0086ef2008-04-30 00:52:19 -0700718 if (pxafb_backlight_power)
719 pxafb_backlight_power(on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
722static inline void __pxafb_lcd_power(struct pxafb_info *fbi, int on)
723{
Russell Kingca5da712005-09-29 09:44:54 +0100724 pr_debug("pxafb: LCD power o%s\n", on ? "n" : "ff");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 if (pxafb_lcd_power)
Richard Purdied14b2722006-09-20 22:54:21 +0100727 pxafb_lcd_power(on, &fbi->fb.var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
730static void pxafb_setup_gpio(struct pxafb_info *fbi)
731{
732 int gpio, ldd_bits;
eric miaob0086ef2008-04-30 00:52:19 -0700733 unsigned int lccr0 = fbi->lccr0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 /*
736 * setup is based on type of panel supported
eric miaob0086ef2008-04-30 00:52:19 -0700737 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 /* 4 bit interface */
740 if ((lccr0 & LCCR0_CMS) == LCCR0_Mono &&
741 (lccr0 & LCCR0_SDS) == LCCR0_Sngl &&
742 (lccr0 & LCCR0_DPD) == LCCR0_4PixMono)
743 ldd_bits = 4;
744
745 /* 8 bit interface */
eric miaob0086ef2008-04-30 00:52:19 -0700746 else if (((lccr0 & LCCR0_CMS) == LCCR0_Mono &&
747 ((lccr0 & LCCR0_SDS) == LCCR0_Dual ||
748 (lccr0 & LCCR0_DPD) == LCCR0_8PixMono)) ||
749 ((lccr0 & LCCR0_CMS) == LCCR0_Color &&
750 (lccr0 & LCCR0_PAS) == LCCR0_Pas &&
751 (lccr0 & LCCR0_SDS) == LCCR0_Sngl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 ldd_bits = 8;
753
754 /* 16 bit interface */
755 else if ((lccr0 & LCCR0_CMS) == LCCR0_Color &&
eric miaob0086ef2008-04-30 00:52:19 -0700756 ((lccr0 & LCCR0_SDS) == LCCR0_Dual ||
757 (lccr0 & LCCR0_PAS) == LCCR0_Act))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 ldd_bits = 16;
759
760 else {
eric miaob0086ef2008-04-30 00:52:19 -0700761 printk(KERN_ERR "pxafb_setup_gpio: unable to determine "
762 "bits per pixel\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return;
eric miaob0086ef2008-04-30 00:52:19 -0700764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 for (gpio = 58; ldd_bits; gpio++, ldd_bits--)
767 pxa_gpio_mode(gpio | GPIO_ALT_FN_2_OUT);
768 pxa_gpio_mode(GPIO74_LCD_FCLK_MD);
769 pxa_gpio_mode(GPIO75_LCD_LCLK_MD);
770 pxa_gpio_mode(GPIO76_LCD_PCLK_MD);
771 pxa_gpio_mode(GPIO77_LCD_ACBIAS_MD);
772}
773
774static void pxafb_enable_controller(struct pxafb_info *fbi)
775{
Russell Kingca5da712005-09-29 09:44:54 +0100776 pr_debug("pxafb: Enabling LCD controller\n");
eric miao2c42dd82008-04-30 00:52:21 -0700777 pr_debug("fdadr0 0x%08x\n", (unsigned int) fbi->fdadr[0]);
778 pr_debug("fdadr1 0x%08x\n", (unsigned int) fbi->fdadr[1]);
Russell Kingca5da712005-09-29 09:44:54 +0100779 pr_debug("reg_lccr0 0x%08x\n", (unsigned int) fbi->reg_lccr0);
780 pr_debug("reg_lccr1 0x%08x\n", (unsigned int) fbi->reg_lccr1);
781 pr_debug("reg_lccr2 0x%08x\n", (unsigned int) fbi->reg_lccr2);
782 pr_debug("reg_lccr3 0x%08x\n", (unsigned int) fbi->reg_lccr3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Nicolas Pitre8d372262005-08-10 16:45:13 +0100784 /* enable LCD controller clock */
Russell King72e35242007-08-20 10:18:42 +0100785 clk_enable(fbi->clk);
Nicolas Pitre8d372262005-08-10 16:45:13 +0100786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 /* Sequence from 11.7.10 */
eric miaoce4fb7b2008-04-30 00:52:21 -0700788 __raw_writel(fbi->reg_lccr3, fbi->mmio_base + LCCR3);
789 __raw_writel(fbi->reg_lccr2, fbi->mmio_base + LCCR2);
790 __raw_writel(fbi->reg_lccr1, fbi->mmio_base + LCCR1);
791 __raw_writel(fbi->reg_lccr0 & ~LCCR0_ENB, fbi->mmio_base + LCCR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
eric miao2c42dd82008-04-30 00:52:21 -0700793 __raw_writel(fbi->fdadr[0], fbi->mmio_base + FDADR0);
794 __raw_writel(fbi->fdadr[1], fbi->mmio_base + FDADR1);
eric miaoce4fb7b2008-04-30 00:52:21 -0700795 __raw_writel(fbi->reg_lccr0 | LCCR0_ENB, fbi->mmio_base + LCCR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
797
798static void pxafb_disable_controller(struct pxafb_info *fbi)
799{
eric miaoce4fb7b2008-04-30 00:52:21 -0700800 uint32_t lccr0;
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 DECLARE_WAITQUEUE(wait, current);
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 set_current_state(TASK_UNINTERRUPTIBLE);
805 add_wait_queue(&fbi->ctrlr_wait, &wait);
806
eric miaoce4fb7b2008-04-30 00:52:21 -0700807 /* Clear LCD Status Register */
808 __raw_writel(0xffffffff, fbi->mmio_base + LCSR);
809
810 lccr0 = __raw_readl(fbi->mmio_base + LCCR0) & ~LCCR0_LDM;
811 __raw_writel(lccr0, fbi->mmio_base + LCCR0);
812 __raw_writel(lccr0 | LCCR0_DIS, fbi->mmio_base + LCCR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Richard Purdie2cbbb3b2006-03-31 02:31:53 -0800814 schedule_timeout(200 * HZ / 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 remove_wait_queue(&fbi->ctrlr_wait, &wait);
Nicolas Pitre8d372262005-08-10 16:45:13 +0100816
817 /* disable LCD controller clock */
Russell King72e35242007-08-20 10:18:42 +0100818 clk_disable(fbi->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
821/*
822 * pxafb_handle_irq: Handle 'LCD DONE' interrupts.
823 */
David Howells7d12e782006-10-05 14:55:46 +0100824static irqreturn_t pxafb_handle_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
826 struct pxafb_info *fbi = dev_id;
eric miaoce4fb7b2008-04-30 00:52:21 -0700827 unsigned int lccr0, lcsr = __raw_readl(fbi->mmio_base + LCSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 if (lcsr & LCSR_LDD) {
eric miaoce4fb7b2008-04-30 00:52:21 -0700830 lccr0 = __raw_readl(fbi->mmio_base + LCCR0) | LCCR0_LDM;
831 __raw_writel(lccr0, fbi->mmio_base + LCCR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 wake_up(&fbi->ctrlr_wait);
833 }
834
eric miaoce4fb7b2008-04-30 00:52:21 -0700835 __raw_writel(lcsr, fbi->mmio_base + LCSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return IRQ_HANDLED;
837}
838
839/*
840 * This function must be called from task context only, since it will
841 * sleep when disabling the LCD controller, or if we get two contending
842 * processes trying to alter state.
843 */
844static void set_ctrlr_state(struct pxafb_info *fbi, u_int state)
845{
846 u_int old_state;
847
848 down(&fbi->ctrlr_sem);
849
850 old_state = fbi->state;
851
852 /*
853 * Hack around fbcon initialisation.
854 */
855 if (old_state == C_STARTUP && state == C_REENABLE)
856 state = C_ENABLE;
857
858 switch (state) {
859 case C_DISABLE_CLKCHANGE:
860 /*
861 * Disable controller for clock change. If the
862 * controller is already disabled, then do nothing.
863 */
864 if (old_state != C_DISABLE && old_state != C_DISABLE_PM) {
865 fbi->state = state;
eric miaob0086ef2008-04-30 00:52:19 -0700866 /* TODO __pxafb_lcd_power(fbi, 0); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 pxafb_disable_controller(fbi);
868 }
869 break;
870
871 case C_DISABLE_PM:
872 case C_DISABLE:
873 /*
874 * Disable controller
875 */
876 if (old_state != C_DISABLE) {
877 fbi->state = state;
878 __pxafb_backlight_power(fbi, 0);
879 __pxafb_lcd_power(fbi, 0);
880 if (old_state != C_DISABLE_CLKCHANGE)
881 pxafb_disable_controller(fbi);
882 }
883 break;
884
885 case C_ENABLE_CLKCHANGE:
886 /*
887 * Enable the controller after clock change. Only
888 * do this if we were disabled for the clock change.
889 */
890 if (old_state == C_DISABLE_CLKCHANGE) {
891 fbi->state = C_ENABLE;
892 pxafb_enable_controller(fbi);
eric miaob0086ef2008-04-30 00:52:19 -0700893 /* TODO __pxafb_lcd_power(fbi, 1); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
895 break;
896
897 case C_REENABLE:
898 /*
899 * Re-enable the controller only if it was already
900 * enabled. This is so we reprogram the control
901 * registers.
902 */
903 if (old_state == C_ENABLE) {
Richard Purdied14b2722006-09-20 22:54:21 +0100904 __pxafb_lcd_power(fbi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 pxafb_disable_controller(fbi);
906 pxafb_setup_gpio(fbi);
907 pxafb_enable_controller(fbi);
Richard Purdied14b2722006-09-20 22:54:21 +0100908 __pxafb_lcd_power(fbi, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
910 break;
911
912 case C_ENABLE_PM:
913 /*
914 * Re-enable the controller after PM. This is not
915 * perfect - think about the case where we were doing
916 * a clock change, and we suspended half-way through.
917 */
918 if (old_state != C_DISABLE_PM)
919 break;
920 /* fall through */
921
922 case C_ENABLE:
923 /*
924 * Power up the LCD screen, enable controller, and
925 * turn on the backlight.
926 */
927 if (old_state != C_ENABLE) {
928 fbi->state = C_ENABLE;
929 pxafb_setup_gpio(fbi);
930 pxafb_enable_controller(fbi);
931 __pxafb_lcd_power(fbi, 1);
932 __pxafb_backlight_power(fbi, 1);
933 }
934 break;
935 }
936 up(&fbi->ctrlr_sem);
937}
938
939/*
940 * Our LCD controller task (which is called when we blank or unblank)
941 * via keventd.
942 */
David Howells6d5aefb2006-12-05 19:36:26 +0000943static void pxafb_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
David Howells6d5aefb2006-12-05 19:36:26 +0000945 struct pxafb_info *fbi =
946 container_of(work, struct pxafb_info, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 u_int state = xchg(&fbi->task_state, -1);
948
949 set_ctrlr_state(fbi, state);
950}
951
952#ifdef CONFIG_CPU_FREQ
953/*
954 * CPU clock speed change handler. We need to adjust the LCD timing
955 * parameters when the CPU clock is adjusted by the power management
956 * subsystem.
957 *
958 * TODO: Determine why f->new != 10*get_lclk_frequency_10khz()
959 */
960static int
961pxafb_freq_transition(struct notifier_block *nb, unsigned long val, void *data)
962{
963 struct pxafb_info *fbi = TO_INF(nb, freq_transition);
eric miaob0086ef2008-04-30 00:52:19 -0700964 /* TODO struct cpufreq_freqs *f = data; */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 u_int pcd;
966
967 switch (val) {
968 case CPUFREQ_PRECHANGE:
969 set_ctrlr_state(fbi, C_DISABLE_CLKCHANGE);
970 break;
971
972 case CPUFREQ_POSTCHANGE:
Russell King72e35242007-08-20 10:18:42 +0100973 pcd = get_pcd(fbi, fbi->fb.var.pixclock);
Richard Purdieba44cd22005-09-09 13:10:03 -0700974 set_hsync_time(fbi, pcd);
eric miaob0086ef2008-04-30 00:52:19 -0700975 fbi->reg_lccr3 = (fbi->reg_lccr3 & ~0xff) |
976 LCCR3_PixClkDiv(pcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 set_ctrlr_state(fbi, C_ENABLE_CLKCHANGE);
978 break;
979 }
980 return 0;
981}
982
983static int
984pxafb_freq_policy(struct notifier_block *nb, unsigned long val, void *data)
985{
986 struct pxafb_info *fbi = TO_INF(nb, freq_policy);
987 struct fb_var_screeninfo *var = &fbi->fb.var;
988 struct cpufreq_policy *policy = data;
989
990 switch (val) {
991 case CPUFREQ_ADJUST:
992 case CPUFREQ_INCOMPATIBLE:
Holger Schurigac2bf5b2008-02-11 16:52:30 +0100993 pr_debug("min dma period: %d ps, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 "new clock %d kHz\n", pxafb_display_dma_period(var),
995 policy->max);
eric miaob0086ef2008-04-30 00:52:19 -0700996 /* TODO: fill in min/max values */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
999 return 0;
1000}
1001#endif
1002
1003#ifdef CONFIG_PM
1004/*
1005 * Power management hooks. Note that we won't be called from IRQ context,
1006 * unlike the blank functions above, so we may sleep.
1007 */
Russell King3ae5eae2005-11-09 22:32:44 +00001008static int pxafb_suspend(struct platform_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
Russell King3ae5eae2005-11-09 22:32:44 +00001010 struct pxafb_info *fbi = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Russell King9480e302005-10-28 09:52:56 -07001012 set_ctrlr_state(fbi, C_DISABLE_PM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return 0;
1014}
1015
Russell King3ae5eae2005-11-09 22:32:44 +00001016static int pxafb_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
Russell King3ae5eae2005-11-09 22:32:44 +00001018 struct pxafb_info *fbi = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Russell King9480e302005-10-28 09:52:56 -07001020 set_ctrlr_state(fbi, C_ENABLE_PM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return 0;
1022}
1023#else
1024#define pxafb_suspend NULL
1025#define pxafb_resume NULL
1026#endif
1027
1028/*
1029 * pxafb_map_video_memory():
1030 * Allocates the DRAM memory for the frame buffer. This buffer is
1031 * remapped into a non-cached, non-buffered, memory region to
1032 * allow palette and pixel writes to occur without flushing the
1033 * cache. Once this area is remapped, all virtual memory
1034 * access to the video memory should occur at the new region.
1035 */
1036static int __init pxafb_map_video_memory(struct pxafb_info *fbi)
1037{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 /*
1039 * We reserve one page for the palette, plus the size
1040 * of the framebuffer.
1041 */
1042 fbi->map_size = PAGE_ALIGN(fbi->fb.fix.smem_len + PAGE_SIZE);
1043 fbi->map_cpu = dma_alloc_writecombine(fbi->dev, fbi->map_size,
1044 &fbi->map_dma, GFP_KERNEL);
1045
1046 if (fbi->map_cpu) {
1047 /* prevent initial garbage on screen */
1048 memset(fbi->map_cpu, 0, fbi->map_size);
1049 fbi->fb.screen_base = fbi->map_cpu + PAGE_SIZE;
1050 fbi->screen_dma = fbi->map_dma + PAGE_SIZE;
1051 /*
1052 * FIXME: this is actually the wrong thing to place in
1053 * smem_start. But fbdev suffers from the problem that
1054 * it needs an API which doesn't exist (in this case,
1055 * dma_writecombine_mmap)
1056 */
1057 fbi->fb.fix.smem_start = fbi->screen_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 fbi->palette_size = fbi->fb.var.bits_per_pixel == 8 ? 256 : 16;
1059
eric miao2c42dd82008-04-30 00:52:21 -07001060 fbi->dma_buff = (void *)fbi->map_cpu;
1061 fbi->dma_buff_phys = fbi->map_dma;
1062 fbi->palette_cpu = (u16 *)&fbi->dma_buff->palette[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 }
1064
1065 return fbi->map_cpu ? 0 : -ENOMEM;
1066}
1067
eric miao84f43c32008-04-30 00:52:22 -07001068static void pxafb_decode_mode_info(struct pxafb_info *fbi,
1069 struct pxafb_mode_info *modes,
1070 unsigned int num_modes)
1071{
1072 unsigned int i, smemlen;
1073
1074 pxafb_setmode(&fbi->fb.var, &modes[0]);
1075
1076 for (i = 0; i < num_modes; i++) {
1077 smemlen = modes[i].xres * modes[i].yres * modes[i].bpp / 8;
1078 if (smemlen > fbi->fb.fix.smem_len)
1079 fbi->fb.fix.smem_len = smemlen;
1080 }
1081}
1082
1083static int pxafb_decode_mach_info(struct pxafb_info *fbi,
1084 struct pxafb_mach_info *inf)
1085{
1086 unsigned int lcd_conn = inf->lcd_conn;
1087
1088 fbi->cmap_inverse = inf->cmap_inverse;
1089 fbi->cmap_static = inf->cmap_static;
1090
1091 switch (lcd_conn & 0xf) {
1092 case LCD_TYPE_MONO_STN:
1093 fbi->lccr0 = LCCR0_CMS;
1094 break;
1095 case LCD_TYPE_MONO_DSTN:
1096 fbi->lccr0 = LCCR0_CMS | LCCR0_SDS;
1097 break;
1098 case LCD_TYPE_COLOR_STN:
1099 fbi->lccr0 = 0;
1100 break;
1101 case LCD_TYPE_COLOR_DSTN:
1102 fbi->lccr0 = LCCR0_SDS;
1103 break;
1104 case LCD_TYPE_COLOR_TFT:
1105 fbi->lccr0 = LCCR0_PAS;
1106 break;
1107 case LCD_TYPE_SMART_PANEL:
1108 fbi->lccr0 = LCCR0_LCDT | LCCR0_PAS;
1109 break;
1110 default:
1111 /* fall back to backward compatibility way */
1112 fbi->lccr0 = inf->lccr0;
1113 fbi->lccr3 = inf->lccr3;
1114 fbi->lccr4 = inf->lccr4;
1115 return -EINVAL;
1116 }
1117
1118 if (lcd_conn == LCD_MONO_STN_8BPP)
1119 fbi->lccr0 |= LCCR0_DPD;
1120
1121 fbi->lccr3 = LCCR3_Acb((inf->lcd_conn >> 10) & 0xff);
1122 fbi->lccr3 |= (lcd_conn & LCD_BIAS_ACTIVE_LOW) ? LCCR3_OEP : 0;
1123 fbi->lccr3 |= (lcd_conn & LCD_PCLK_EDGE_FALL) ? LCCR3_PCP : 0;
1124
1125 pxafb_decode_mode_info(fbi, inf->modes, inf->num_modes);
1126 return 0;
1127}
1128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129static struct pxafb_info * __init pxafb_init_fbinfo(struct device *dev)
1130{
1131 struct pxafb_info *fbi;
1132 void *addr;
1133 struct pxafb_mach_info *inf = dev->platform_data;
Richard Purdied14b2722006-09-20 22:54:21 +01001134 struct pxafb_mode_info *mode = inf->modes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136 /* Alloc the pxafb_info and pseudo_palette in one step */
1137 fbi = kmalloc(sizeof(struct pxafb_info) + sizeof(u32) * 16, GFP_KERNEL);
1138 if (!fbi)
1139 return NULL;
1140
1141 memset(fbi, 0, sizeof(struct pxafb_info));
1142 fbi->dev = dev;
1143
Russell King72e35242007-08-20 10:18:42 +01001144 fbi->clk = clk_get(dev, "LCDCLK");
1145 if (IS_ERR(fbi->clk)) {
1146 kfree(fbi);
1147 return NULL;
1148 }
1149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 strcpy(fbi->fb.fix.id, PXA_NAME);
1151
1152 fbi->fb.fix.type = FB_TYPE_PACKED_PIXELS;
1153 fbi->fb.fix.type_aux = 0;
1154 fbi->fb.fix.xpanstep = 0;
1155 fbi->fb.fix.ypanstep = 0;
1156 fbi->fb.fix.ywrapstep = 0;
1157 fbi->fb.fix.accel = FB_ACCEL_NONE;
1158
1159 fbi->fb.var.nonstd = 0;
1160 fbi->fb.var.activate = FB_ACTIVATE_NOW;
1161 fbi->fb.var.height = -1;
1162 fbi->fb.var.width = -1;
1163 fbi->fb.var.accel_flags = 0;
1164 fbi->fb.var.vmode = FB_VMODE_NONINTERLACED;
1165
1166 fbi->fb.fbops = &pxafb_ops;
1167 fbi->fb.flags = FBINFO_DEFAULT;
1168 fbi->fb.node = -1;
1169
1170 addr = fbi;
1171 addr = addr + sizeof(struct pxafb_info);
1172 fbi->fb.pseudo_palette = addr;
1173
eric miaob0086ef2008-04-30 00:52:19 -07001174 fbi->state = C_STARTUP;
1175 fbi->task_state = (u_char)-1;
Richard Purdied14b2722006-09-20 22:54:21 +01001176
eric miao84f43c32008-04-30 00:52:22 -07001177 pxafb_decode_mach_info(fbi, inf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 init_waitqueue_head(&fbi->ctrlr_wait);
David Howells6d5aefb2006-12-05 19:36:26 +00001180 INIT_WORK(&fbi->task, pxafb_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 init_MUTEX(&fbi->ctrlr_sem);
1182
1183 return fbi;
1184}
1185
1186#ifdef CONFIG_FB_PXA_PARAMETERS
eric miaob0086ef2008-04-30 00:52:19 -07001187static int __init parse_opt_mode(struct device *dev, const char *this_opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
1189 struct pxafb_mach_info *inf = dev->platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
eric miao817daf12008-04-30 00:52:18 -07001191 const char *name = this_opt+5;
1192 unsigned int namelen = strlen(name);
1193 int res_specified = 0, bpp_specified = 0;
1194 unsigned int xres = 0, yres = 0, bpp = 0;
1195 int yres_specified = 0;
1196 int i;
1197 for (i = namelen-1; i >= 0; i--) {
1198 switch (name[i]) {
1199 case '-':
1200 namelen = i;
1201 if (!bpp_specified && !yres_specified) {
1202 bpp = simple_strtoul(&name[i+1], NULL, 0);
1203 bpp_specified = 1;
1204 } else
1205 goto done;
1206 break;
1207 case 'x':
1208 if (!yres_specified) {
1209 yres = simple_strtoul(&name[i+1], NULL, 0);
1210 yres_specified = 1;
1211 } else
1212 goto done;
1213 break;
1214 case '0' ... '9':
1215 break;
1216 default:
1217 goto done;
1218 }
1219 }
1220 if (i < 0 && yres_specified) {
1221 xres = simple_strtoul(name, NULL, 0);
1222 res_specified = 1;
1223 }
1224done:
1225 if (res_specified) {
1226 dev_info(dev, "overriding resolution: %dx%d\n", xres, yres);
1227 inf->modes[0].xres = xres; inf->modes[0].yres = yres;
1228 }
1229 if (bpp_specified)
1230 switch (bpp) {
1231 case 1:
1232 case 2:
1233 case 4:
1234 case 8:
1235 case 16:
1236 inf->modes[0].bpp = bpp;
1237 dev_info(dev, "overriding bit depth: %d\n", bpp);
1238 break;
1239 default:
1240 dev_err(dev, "Depth %d is not valid\n", bpp);
1241 return -EINVAL;
1242 }
1243 return 0;
1244}
1245
eric miaob0086ef2008-04-30 00:52:19 -07001246static int __init parse_opt(struct device *dev, char *this_opt)
eric miao817daf12008-04-30 00:52:18 -07001247{
1248 struct pxafb_mach_info *inf = dev->platform_data;
1249 struct pxafb_mode_info *mode = &inf->modes[0];
1250 char s[64];
1251
1252 s[0] = '\0';
1253
1254 if (!strncmp(this_opt, "mode:", 5)) {
1255 return parse_opt_mode(dev, this_opt);
1256 } else if (!strncmp(this_opt, "pixclock:", 9)) {
1257 mode->pixclock = simple_strtoul(this_opt+9, NULL, 0);
1258 sprintf(s, "pixclock: %ld\n", mode->pixclock);
1259 } else if (!strncmp(this_opt, "left:", 5)) {
1260 mode->left_margin = simple_strtoul(this_opt+5, NULL, 0);
1261 sprintf(s, "left: %u\n", mode->left_margin);
1262 } else if (!strncmp(this_opt, "right:", 6)) {
1263 mode->right_margin = simple_strtoul(this_opt+6, NULL, 0);
1264 sprintf(s, "right: %u\n", mode->right_margin);
1265 } else if (!strncmp(this_opt, "upper:", 6)) {
1266 mode->upper_margin = simple_strtoul(this_opt+6, NULL, 0);
1267 sprintf(s, "upper: %u\n", mode->upper_margin);
1268 } else if (!strncmp(this_opt, "lower:", 6)) {
1269 mode->lower_margin = simple_strtoul(this_opt+6, NULL, 0);
1270 sprintf(s, "lower: %u\n", mode->lower_margin);
1271 } else if (!strncmp(this_opt, "hsynclen:", 9)) {
1272 mode->hsync_len = simple_strtoul(this_opt+9, NULL, 0);
1273 sprintf(s, "hsynclen: %u\n", mode->hsync_len);
1274 } else if (!strncmp(this_opt, "vsynclen:", 9)) {
1275 mode->vsync_len = simple_strtoul(this_opt+9, NULL, 0);
1276 sprintf(s, "vsynclen: %u\n", mode->vsync_len);
1277 } else if (!strncmp(this_opt, "hsync:", 6)) {
1278 if (simple_strtoul(this_opt+6, NULL, 0) == 0) {
1279 sprintf(s, "hsync: Active Low\n");
1280 mode->sync &= ~FB_SYNC_HOR_HIGH_ACT;
1281 } else {
1282 sprintf(s, "hsync: Active High\n");
1283 mode->sync |= FB_SYNC_HOR_HIGH_ACT;
1284 }
1285 } else if (!strncmp(this_opt, "vsync:", 6)) {
1286 if (simple_strtoul(this_opt+6, NULL, 0) == 0) {
1287 sprintf(s, "vsync: Active Low\n");
1288 mode->sync &= ~FB_SYNC_VERT_HIGH_ACT;
1289 } else {
1290 sprintf(s, "vsync: Active High\n");
1291 mode->sync |= FB_SYNC_VERT_HIGH_ACT;
1292 }
1293 } else if (!strncmp(this_opt, "dpc:", 4)) {
1294 if (simple_strtoul(this_opt+4, NULL, 0) == 0) {
1295 sprintf(s, "double pixel clock: false\n");
1296 inf->lccr3 &= ~LCCR3_DPC;
1297 } else {
1298 sprintf(s, "double pixel clock: true\n");
1299 inf->lccr3 |= LCCR3_DPC;
1300 }
1301 } else if (!strncmp(this_opt, "outputen:", 9)) {
1302 if (simple_strtoul(this_opt+9, NULL, 0) == 0) {
1303 sprintf(s, "output enable: active low\n");
1304 inf->lccr3 = (inf->lccr3 & ~LCCR3_OEP) | LCCR3_OutEnL;
1305 } else {
1306 sprintf(s, "output enable: active high\n");
1307 inf->lccr3 = (inf->lccr3 & ~LCCR3_OEP) | LCCR3_OutEnH;
1308 }
1309 } else if (!strncmp(this_opt, "pixclockpol:", 12)) {
1310 if (simple_strtoul(this_opt+12, NULL, 0) == 0) {
1311 sprintf(s, "pixel clock polarity: falling edge\n");
1312 inf->lccr3 = (inf->lccr3 & ~LCCR3_PCP) | LCCR3_PixFlEdg;
1313 } else {
1314 sprintf(s, "pixel clock polarity: rising edge\n");
1315 inf->lccr3 = (inf->lccr3 & ~LCCR3_PCP) | LCCR3_PixRsEdg;
1316 }
1317 } else if (!strncmp(this_opt, "color", 5)) {
1318 inf->lccr0 = (inf->lccr0 & ~LCCR0_CMS) | LCCR0_Color;
1319 } else if (!strncmp(this_opt, "mono", 4)) {
1320 inf->lccr0 = (inf->lccr0 & ~LCCR0_CMS) | LCCR0_Mono;
1321 } else if (!strncmp(this_opt, "active", 6)) {
1322 inf->lccr0 = (inf->lccr0 & ~LCCR0_PAS) | LCCR0_Act;
1323 } else if (!strncmp(this_opt, "passive", 7)) {
1324 inf->lccr0 = (inf->lccr0 & ~LCCR0_PAS) | LCCR0_Pas;
1325 } else if (!strncmp(this_opt, "single", 6)) {
1326 inf->lccr0 = (inf->lccr0 & ~LCCR0_SDS) | LCCR0_Sngl;
1327 } else if (!strncmp(this_opt, "dual", 4)) {
1328 inf->lccr0 = (inf->lccr0 & ~LCCR0_SDS) | LCCR0_Dual;
1329 } else if (!strncmp(this_opt, "4pix", 4)) {
1330 inf->lccr0 = (inf->lccr0 & ~LCCR0_DPD) | LCCR0_4PixMono;
1331 } else if (!strncmp(this_opt, "8pix", 4)) {
1332 inf->lccr0 = (inf->lccr0 & ~LCCR0_DPD) | LCCR0_8PixMono;
1333 } else {
1334 dev_err(dev, "unknown option: %s\n", this_opt);
1335 return -EINVAL;
1336 }
1337
1338 if (s[0] != '\0')
1339 dev_info(dev, "override %s", s);
1340
1341 return 0;
1342}
1343
1344static int __init pxafb_parse_options(struct device *dev, char *options)
1345{
1346 char *this_opt;
1347 int ret;
1348
1349 if (!options || !*options)
1350 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352 dev_dbg(dev, "options are \"%s\"\n", options ? options : "null");
1353
1354 /* could be made table driven or similar?... */
eric miao817daf12008-04-30 00:52:18 -07001355 while ((this_opt = strsep(&options, ",")) != NULL) {
1356 ret = parse_opt(dev, this_opt);
1357 if (ret)
1358 return ret;
1359 }
1360 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361}
eric miao92ac73c2008-04-30 00:52:20 -07001362
1363static char g_options[256] __devinitdata = "";
1364
1365#ifndef CONFIG_MODULES
1366static int __devinit pxafb_setup_options(void)
1367{
1368 char *options = NULL;
1369
1370 if (fb_get_options("pxafb", &options))
1371 return -ENODEV;
1372
1373 if (options)
1374 strlcpy(g_options, options, sizeof(g_options));
1375
1376 return 0;
1377}
1378#else
1379#define pxafb_setup_options() (0)
1380
1381module_param_string(options, g_options, sizeof(g_options), 0);
1382MODULE_PARM_DESC(options, "LCD parameters (see Documentation/fb/pxafb.txt)");
1383#endif
1384
1385#else
1386#define pxafb_parse_options(...) (0)
1387#define pxafb_setup_options() (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388#endif
1389
Holger Schurigac2bf5b2008-02-11 16:52:30 +01001390static int __init pxafb_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
1392 struct pxafb_info *fbi;
1393 struct pxafb_mach_info *inf;
eric miaoce4fb7b2008-04-30 00:52:21 -07001394 struct resource *r;
1395 int irq, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Richard Purdie2cbbb3b2006-03-31 02:31:53 -08001397 dev_dbg(&dev->dev, "pxafb_probe\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Russell King3ae5eae2005-11-09 22:32:44 +00001399 inf = dev->dev.platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 ret = -ENOMEM;
1401 fbi = NULL;
1402 if (!inf)
1403 goto failed;
1404
Russell King3ae5eae2005-11-09 22:32:44 +00001405 ret = pxafb_parse_options(&dev->dev, g_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 if (ret < 0)
1407 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409#ifdef DEBUG_VAR
eric miaob0086ef2008-04-30 00:52:19 -07001410 /* Check for various illegal bit-combinations. Currently only
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 * a warning is given. */
1412
eric miaob0086ef2008-04-30 00:52:19 -07001413 if (inf->lccr0 & LCCR0_INVALID_CONFIG_MASK)
1414 dev_warn(&dev->dev, "machine LCCR0 setting contains "
1415 "illegal bits: %08x\n",
1416 inf->lccr0 & LCCR0_INVALID_CONFIG_MASK);
1417 if (inf->lccr3 & LCCR3_INVALID_CONFIG_MASK)
1418 dev_warn(&dev->dev, "machine LCCR3 setting contains "
1419 "illegal bits: %08x\n",
1420 inf->lccr3 & LCCR3_INVALID_CONFIG_MASK);
1421 if (inf->lccr0 & LCCR0_DPD &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 ((inf->lccr0 & LCCR0_PAS) != LCCR0_Pas ||
1423 (inf->lccr0 & LCCR0_SDS) != LCCR0_Sngl ||
1424 (inf->lccr0 & LCCR0_CMS) != LCCR0_Mono))
eric miaob0086ef2008-04-30 00:52:19 -07001425 dev_warn(&dev->dev, "Double Pixel Data (DPD) mode is "
1426 "only valid in passive mono"
1427 " single panel mode\n");
1428 if ((inf->lccr0 & LCCR0_PAS) == LCCR0_Act &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 (inf->lccr0 & LCCR0_SDS) == LCCR0_Dual)
eric miaob0086ef2008-04-30 00:52:19 -07001430 dev_warn(&dev->dev, "Dual panel only valid in passive mode\n");
1431 if ((inf->lccr0 & LCCR0_PAS) == LCCR0_Pas &&
1432 (inf->modes->upper_margin || inf->modes->lower_margin))
1433 dev_warn(&dev->dev, "Upper and lower margins must be 0 in "
1434 "passive mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435#endif
1436
eric miaob0086ef2008-04-30 00:52:19 -07001437 dev_dbg(&dev->dev, "got a %dx%dx%d LCD\n",
1438 inf->modes->xres,
1439 inf->modes->yres,
1440 inf->modes->bpp);
1441 if (inf->modes->xres == 0 ||
1442 inf->modes->yres == 0 ||
1443 inf->modes->bpp == 0) {
Russell King3ae5eae2005-11-09 22:32:44 +00001444 dev_err(&dev->dev, "Invalid resolution or bit depth\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 ret = -EINVAL;
1446 goto failed;
1447 }
1448 pxafb_backlight_power = inf->pxafb_backlight_power;
1449 pxafb_lcd_power = inf->pxafb_lcd_power;
Russell King3ae5eae2005-11-09 22:32:44 +00001450 fbi = pxafb_init_fbinfo(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 if (!fbi) {
eric miaob0086ef2008-04-30 00:52:19 -07001452 /* only reason for pxafb_init_fbinfo to fail is kmalloc */
Russell King3ae5eae2005-11-09 22:32:44 +00001453 dev_err(&dev->dev, "Failed to initialize framebuffer device\n");
eric miaob0086ef2008-04-30 00:52:19 -07001454 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 goto failed;
1456 }
1457
eric miaoce4fb7b2008-04-30 00:52:21 -07001458 r = platform_get_resource(dev, IORESOURCE_MEM, 0);
1459 if (r == NULL) {
1460 dev_err(&dev->dev, "no I/O memory resource defined\n");
1461 ret = -ENODEV;
1462 goto failed;
1463 }
1464
1465 r = request_mem_region(r->start, r->end - r->start + 1, dev->name);
1466 if (r == NULL) {
1467 dev_err(&dev->dev, "failed to request I/O memory\n");
1468 ret = -EBUSY;
1469 goto failed;
1470 }
1471
1472 fbi->mmio_base = ioremap(r->start, r->end - r->start + 1);
1473 if (fbi->mmio_base == NULL) {
1474 dev_err(&dev->dev, "failed to map I/O memory\n");
1475 ret = -EBUSY;
1476 goto failed_free_res;
1477 }
1478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 /* Initialize video memory */
1480 ret = pxafb_map_video_memory(fbi);
1481 if (ret) {
Russell King3ae5eae2005-11-09 22:32:44 +00001482 dev_err(&dev->dev, "Failed to allocate video RAM: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 ret = -ENOMEM;
eric miaoce4fb7b2008-04-30 00:52:21 -07001484 goto failed_free_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
eric miaoce4fb7b2008-04-30 00:52:21 -07001487 irq = platform_get_irq(dev, 0);
1488 if (irq < 0) {
1489 dev_err(&dev->dev, "no IRQ defined\n");
1490 ret = -ENODEV;
1491 goto failed_free_mem;
1492 }
1493
1494 ret = request_irq(irq, pxafb_handle_irq, IRQF_DISABLED, "LCD", fbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if (ret) {
Russell King3ae5eae2005-11-09 22:32:44 +00001496 dev_err(&dev->dev, "request_irq failed: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 ret = -EBUSY;
eric miaoce4fb7b2008-04-30 00:52:21 -07001498 goto failed_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 }
1500
1501 /*
1502 * This makes sure that our colour bitfield
1503 * descriptors are correctly initialised.
1504 */
1505 pxafb_check_var(&fbi->fb.var, &fbi->fb);
1506 pxafb_set_par(&fbi->fb);
1507
Russell King3ae5eae2005-11-09 22:32:44 +00001508 platform_set_drvdata(dev, fbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
1510 ret = register_framebuffer(&fbi->fb);
1511 if (ret < 0) {
eric miaob0086ef2008-04-30 00:52:19 -07001512 dev_err(&dev->dev,
1513 "Failed to register framebuffer device: %d\n", ret);
eric miaoce4fb7b2008-04-30 00:52:21 -07001514 goto failed_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 }
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517#ifdef CONFIG_CPU_FREQ
1518 fbi->freq_transition.notifier_call = pxafb_freq_transition;
1519 fbi->freq_policy.notifier_call = pxafb_freq_policy;
eric miaob0086ef2008-04-30 00:52:19 -07001520 cpufreq_register_notifier(&fbi->freq_transition,
1521 CPUFREQ_TRANSITION_NOTIFIER);
1522 cpufreq_register_notifier(&fbi->freq_policy,
1523 CPUFREQ_POLICY_NOTIFIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524#endif
1525
1526 /*
1527 * Ok, now enable the LCD controller
1528 */
1529 set_ctrlr_state(fbi, C_ENABLE);
1530
1531 return 0;
1532
eric miaoce4fb7b2008-04-30 00:52:21 -07001533failed_free_irq:
1534 free_irq(irq, fbi);
1535failed_free_res:
1536 release_mem_region(r->start, r->end - r->start + 1);
1537failed_free_io:
1538 iounmap(fbi->mmio_base);
1539failed_free_mem:
1540 dma_free_writecombine(&dev->dev, fbi->map_size,
1541 fbi->map_cpu, fbi->map_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542failed:
Russell King3ae5eae2005-11-09 22:32:44 +00001543 platform_set_drvdata(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 kfree(fbi);
1545 return ret;
1546}
1547
Russell King3ae5eae2005-11-09 22:32:44 +00001548static struct platform_driver pxafb_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 .probe = pxafb_probe,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 .suspend = pxafb_suspend,
1551 .resume = pxafb_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001552 .driver = {
1553 .name = "pxa2xx-fb",
1554 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555};
1556
Holger Schurigac2bf5b2008-02-11 16:52:30 +01001557static int __devinit pxafb_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
eric miao92ac73c2008-04-30 00:52:20 -07001559 if (pxafb_setup_options())
1560 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Russell King3ae5eae2005-11-09 22:32:44 +00001562 return platform_driver_register(&pxafb_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563}
1564
1565module_init(pxafb_init);
1566
1567MODULE_DESCRIPTION("loadable framebuffer driver for PXA");
1568MODULE_LICENSE("GPL");