blob: 295ff4e1955ee9006d23831bf071e4ae95fcc5b4 [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>
Eric Miao3c42a442008-04-30 00:52:26 -070043#include <linux/kthread.h>
44#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#include <asm/hardware.h>
47#include <asm/io.h>
48#include <asm/irq.h>
Nicolas Pitrebf1b8ab2005-06-23 21:56:45 +010049#include <asm/div64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <asm/arch/pxa-regs.h>
eric miaoa683b142008-03-03 09:44:25 +080051#include <asm/arch/pxa2xx-gpio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <asm/arch/bitfield.h>
53#include <asm/arch/pxafb.h>
54
55/*
56 * Complain if VAR is out of range.
57 */
58#define DEBUG_VAR 1
59
60#include "pxafb.h"
61
62/* Bits which should not be set in machine configuration structures */
eric miaob0086ef2008-04-30 00:52:19 -070063#define LCCR0_INVALID_CONFIG_MASK (LCCR0_OUM | LCCR0_BM | LCCR0_QDM |\
64 LCCR0_DIS | LCCR0_EFM | LCCR0_IUM |\
65 LCCR0_SFM | LCCR0_LDM | LCCR0_ENB)
66
67#define LCCR3_INVALID_CONFIG_MASK (LCCR3_HSP | LCCR3_VSP |\
68 LCCR3_PCD | LCCR3_BPP)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70static void (*pxafb_backlight_power)(int);
Richard Purdied14b2722006-09-20 22:54:21 +010071static void (*pxafb_lcd_power)(int, struct fb_var_screeninfo *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
eric miaob0086ef2008-04-30 00:52:19 -070073static int pxafb_activate_var(struct fb_var_screeninfo *var,
74 struct pxafb_info *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static void set_ctrlr_state(struct pxafb_info *fbi, u_int state);
76
Eric Miaoa7535ba2008-04-30 00:52:24 -070077static inline unsigned long
78lcd_readl(struct pxafb_info *fbi, unsigned int off)
79{
80 return __raw_readl(fbi->mmio_base + off);
81}
82
83static inline void
84lcd_writel(struct pxafb_info *fbi, unsigned int off, unsigned long val)
85{
86 __raw_writel(val, fbi->mmio_base + off);
87}
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089static inline void pxafb_schedule_work(struct pxafb_info *fbi, u_int state)
90{
91 unsigned long flags;
92
93 local_irq_save(flags);
94 /*
95 * We need to handle two requests being made at the same time.
96 * There are two important cases:
eric miaob0086ef2008-04-30 00:52:19 -070097 * 1. When we are changing VT (C_REENABLE) while unblanking
98 * (C_ENABLE) We must perform the unblanking, which will
99 * do our REENABLE for us.
100 * 2. When we are blanking, but immediately unblank before
101 * we have blanked. We do the "REENABLE" thing here as
102 * well, just to be sure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 */
104 if (fbi->task_state == C_ENABLE && state == C_REENABLE)
105 state = (u_int) -1;
106 if (fbi->task_state == C_DISABLE && state == C_ENABLE)
107 state = C_REENABLE;
108
109 if (state != (u_int)-1) {
110 fbi->task_state = state;
111 schedule_work(&fbi->task);
112 }
113 local_irq_restore(flags);
114}
115
116static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
117{
118 chan &= 0xffff;
119 chan >>= 16 - bf->length;
120 return chan << bf->offset;
121}
122
123static int
124pxafb_setpalettereg(u_int regno, u_int red, u_int green, u_int blue,
125 u_int trans, struct fb_info *info)
126{
127 struct pxafb_info *fbi = (struct pxafb_info *)info;
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700128 u_int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700130 if (regno >= fbi->palette_size)
131 return 1;
132
133 if (fbi->fb.var.grayscale) {
134 fbi->palette_cpu[regno] = ((blue >> 8) & 0x00ff);
135 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700137
138 switch (fbi->lccr4 & LCCR4_PAL_FOR_MASK) {
139 case LCCR4_PAL_FOR_0:
140 val = ((red >> 0) & 0xf800);
141 val |= ((green >> 5) & 0x07e0);
142 val |= ((blue >> 11) & 0x001f);
143 fbi->palette_cpu[regno] = val;
144 break;
145 case LCCR4_PAL_FOR_1:
146 val = ((red << 8) & 0x00f80000);
147 val |= ((green >> 0) & 0x0000fc00);
148 val |= ((blue >> 8) & 0x000000f8);
eric miaob0086ef2008-04-30 00:52:19 -0700149 ((u32 *)(fbi->palette_cpu))[regno] = val;
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700150 break;
151 case LCCR4_PAL_FOR_2:
152 val = ((red << 8) & 0x00fc0000);
153 val |= ((green >> 0) & 0x0000fc00);
154 val |= ((blue >> 8) & 0x000000fc);
eric miaob0086ef2008-04-30 00:52:19 -0700155 ((u32 *)(fbi->palette_cpu))[regno] = val;
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700156 break;
157 }
158
159 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
162static int
163pxafb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
164 u_int trans, struct fb_info *info)
165{
166 struct pxafb_info *fbi = (struct pxafb_info *)info;
167 unsigned int val;
168 int ret = 1;
169
170 /*
171 * If inverse mode was selected, invert all the colours
172 * rather than the register number. The register number
173 * is what you poke into the framebuffer to produce the
174 * colour you requested.
175 */
176 if (fbi->cmap_inverse) {
177 red = 0xffff - red;
178 green = 0xffff - green;
179 blue = 0xffff - blue;
180 }
181
182 /*
183 * If greyscale is true, then we convert the RGB value
184 * to greyscale no matter what visual we are using.
185 */
186 if (fbi->fb.var.grayscale)
187 red = green = blue = (19595 * red + 38470 * green +
188 7471 * blue) >> 16;
189
190 switch (fbi->fb.fix.visual) {
191 case FB_VISUAL_TRUECOLOR:
192 /*
193 * 16-bit True Colour. We encode the RGB value
194 * according to the RGB bitfield information.
195 */
196 if (regno < 16) {
197 u32 *pal = fbi->fb.pseudo_palette;
198
199 val = chan_to_field(red, &fbi->fb.var.red);
200 val |= chan_to_field(green, &fbi->fb.var.green);
201 val |= chan_to_field(blue, &fbi->fb.var.blue);
202
203 pal[regno] = val;
204 ret = 0;
205 }
206 break;
207
208 case FB_VISUAL_STATIC_PSEUDOCOLOR:
209 case FB_VISUAL_PSEUDOCOLOR:
210 ret = pxafb_setpalettereg(regno, red, green, blue, trans, info);
211 break;
212 }
213
214 return ret;
215}
216
217/*
218 * pxafb_bpp_to_lccr3():
219 * Convert a bits per pixel value to the correct bit pattern for LCCR3
220 */
221static int pxafb_bpp_to_lccr3(struct fb_var_screeninfo *var)
222{
eric miaob0086ef2008-04-30 00:52:19 -0700223 int ret = 0;
224 switch (var->bits_per_pixel) {
225 case 1: ret = LCCR3_1BPP; break;
226 case 2: ret = LCCR3_2BPP; break;
227 case 4: ret = LCCR3_4BPP; break;
228 case 8: ret = LCCR3_8BPP; break;
229 case 16: ret = LCCR3_16BPP; break;
Stefan Schmidtc1450f12008-07-09 08:06:32 +0100230 case 24:
231 switch (var->red.length + var->green.length +
232 var->blue.length + var->transp.length) {
233 case 18: ret = LCCR3_18BPP_P | LCCR3_PDFOR_3; break;
234 case 19: ret = LCCR3_19BPP_P; break;
235 }
236 break;
237 case 32:
238 switch (var->red.length + var->green.length +
239 var->blue.length + var->transp.length) {
240 case 18: ret = LCCR3_18BPP | LCCR3_PDFOR_3; break;
241 case 19: ret = LCCR3_19BPP; break;
242 case 24: ret = LCCR3_24BPP | LCCR3_PDFOR_3; break;
243 case 25: ret = LCCR3_25BPP; break;
244 }
245 break;
eric miaob0086ef2008-04-30 00:52:19 -0700246 }
247 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
250#ifdef CONFIG_CPU_FREQ
251/*
252 * pxafb_display_dma_period()
253 * Calculate the minimum period (in picoseconds) between two DMA
254 * requests for the LCD controller. If we hit this, it means we're
255 * doing nothing but LCD DMA.
256 */
257static unsigned int pxafb_display_dma_period(struct fb_var_screeninfo *var)
258{
eric miaob0086ef2008-04-30 00:52:19 -0700259 /*
260 * Period = pixclock * bits_per_byte * bytes_per_transfer
261 * / memory_bits_per_pixel;
262 */
263 return var->pixclock * 8 * 16 / var->bits_per_pixel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265#endif
266
267/*
Richard Purdied14b2722006-09-20 22:54:21 +0100268 * Select the smallest mode that allows the desired resolution to be
269 * displayed. If desired parameters can be rounded up.
270 */
eric miaob0086ef2008-04-30 00:52:19 -0700271static struct pxafb_mode_info *pxafb_getmode(struct pxafb_mach_info *mach,
272 struct fb_var_screeninfo *var)
Richard Purdied14b2722006-09-20 22:54:21 +0100273{
274 struct pxafb_mode_info *mode = NULL;
275 struct pxafb_mode_info *modelist = mach->modes;
276 unsigned int best_x = 0xffffffff, best_y = 0xffffffff;
277 unsigned int i;
278
eric miaob0086ef2008-04-30 00:52:19 -0700279 for (i = 0; i < mach->num_modes; i++) {
280 if (modelist[i].xres >= var->xres &&
281 modelist[i].yres >= var->yres &&
282 modelist[i].xres < best_x &&
283 modelist[i].yres < best_y &&
284 modelist[i].bpp >= var->bits_per_pixel) {
Richard Purdied14b2722006-09-20 22:54:21 +0100285 best_x = modelist[i].xres;
286 best_y = modelist[i].yres;
287 mode = &modelist[i];
288 }
289 }
290
291 return mode;
292}
293
eric miaob0086ef2008-04-30 00:52:19 -0700294static void pxafb_setmode(struct fb_var_screeninfo *var,
295 struct pxafb_mode_info *mode)
Richard Purdied14b2722006-09-20 22:54:21 +0100296{
297 var->xres = mode->xres;
298 var->yres = mode->yres;
299 var->bits_per_pixel = mode->bpp;
300 var->pixclock = mode->pixclock;
301 var->hsync_len = mode->hsync_len;
302 var->left_margin = mode->left_margin;
303 var->right_margin = mode->right_margin;
304 var->vsync_len = mode->vsync_len;
305 var->upper_margin = mode->upper_margin;
306 var->lower_margin = mode->lower_margin;
307 var->sync = mode->sync;
308 var->grayscale = mode->cmap_greyscale;
309 var->xres_virtual = var->xres;
310 var->yres_virtual = var->yres;
311}
312
313/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 * pxafb_check_var():
315 * Get the video params out of 'var'. If a value doesn't fit, round it up,
316 * if it's too big, return -EINVAL.
317 *
318 * Round up in the following order: bits_per_pixel, xres,
319 * yres, xres_virtual, yres_virtual, xoffset, yoffset, grayscale,
320 * bitfields, horizontal timing, vertical timing.
321 */
322static int pxafb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
323{
324 struct pxafb_info *fbi = (struct pxafb_info *)info;
Richard Purdied14b2722006-09-20 22:54:21 +0100325 struct pxafb_mach_info *inf = fbi->dev->platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 if (var->xres < MIN_XRES)
328 var->xres = MIN_XRES;
329 if (var->yres < MIN_YRES)
330 var->yres = MIN_YRES;
Richard Purdied14b2722006-09-20 22:54:21 +0100331
332 if (inf->fixed_modes) {
333 struct pxafb_mode_info *mode;
334
335 mode = pxafb_getmode(inf, var);
336 if (!mode)
337 return -EINVAL;
338 pxafb_setmode(var, mode);
339 } else {
340 if (var->xres > inf->modes->xres)
341 return -EINVAL;
342 if (var->yres > inf->modes->yres)
343 return -EINVAL;
344 if (var->bits_per_pixel > inf->modes->bpp)
345 return -EINVAL;
346 }
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 var->xres_virtual =
349 max(var->xres_virtual, var->xres);
350 var->yres_virtual =
351 max(var->yres_virtual, var->yres);
352
eric miaob0086ef2008-04-30 00:52:19 -0700353 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 * Setup the RGB parameters for this display.
355 *
356 * The pixel packing format is described on page 7-11 of the
357 * PXA2XX Developer's Manual.
eric miaob0086ef2008-04-30 00:52:19 -0700358 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (var->bits_per_pixel == 16) {
360 var->red.offset = 11; var->red.length = 5;
361 var->green.offset = 5; var->green.length = 6;
362 var->blue.offset = 0; var->blue.length = 5;
363 var->transp.offset = var->transp.length = 0;
Stefan Schmidtc1450f12008-07-09 08:06:32 +0100364 } else if (var->bits_per_pixel > 16) {
365 struct pxafb_mode_info *mode;
366
367 mode = pxafb_getmode(inf, var);
368 if (!mode)
369 return -EINVAL;
370
371 switch (mode->depth) {
372 case 18: /* RGB666 */
373 var->transp.offset = var->transp.length = 0;
374 var->red.offset = 12; var->red.length = 6;
375 var->green.offset = 6; var->green.length = 6;
376 var->blue.offset = 0; var->blue.length = 6;
377 break;
378 case 19: /* RGBT666 */
379 var->transp.offset = 18; var->transp.length = 1;
380 var->red.offset = 12; var->red.length = 6;
381 var->green.offset = 6; var->green.length = 6;
382 var->blue.offset = 0; var->blue.length = 6;
383 break;
384 case 24: /* RGB888 */
385 var->transp.offset = var->transp.length = 0;
386 var->red.offset = 16; var->red.length = 8;
387 var->green.offset = 8; var->green.length = 8;
388 var->blue.offset = 0; var->blue.length = 8;
389 break;
390 case 25: /* RGBT888 */
391 var->transp.offset = 24; var->transp.length = 1;
392 var->red.offset = 16; var->red.length = 8;
393 var->green.offset = 8; var->green.length = 8;
394 var->blue.offset = 0; var->blue.length = 8;
395 break;
396 default:
397 return -EINVAL;
398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 } else {
eric miaob0086ef2008-04-30 00:52:19 -0700400 var->red.offset = var->green.offset = 0;
401 var->blue.offset = var->transp.offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 var->red.length = 8;
403 var->green.length = 8;
404 var->blue.length = 8;
405 var->transp.length = 0;
406 }
407
408#ifdef CONFIG_CPU_FREQ
Russell King78d3cfd2008-05-17 22:51:14 +0100409 pr_debug("pxafb: dma period = %d ps\n",
410 pxafb_display_dma_period(var));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411#endif
412
413 return 0;
414}
415
416static inline void pxafb_set_truecolor(u_int is_true_color)
417{
eric miaob0086ef2008-04-30 00:52:19 -0700418 /* do your machine-specific setup if needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
421/*
422 * pxafb_set_par():
423 * Set the user defined part of the display for the specified console
424 */
425static int pxafb_set_par(struct fb_info *info)
426{
427 struct pxafb_info *fbi = (struct pxafb_info *)info;
428 struct fb_var_screeninfo *var = &info->var;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Stefan Schmidtc1450f12008-07-09 08:06:32 +0100430 if (var->bits_per_pixel >= 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 fbi->fb.fix.visual = FB_VISUAL_TRUECOLOR;
432 else if (!fbi->cmap_static)
433 fbi->fb.fix.visual = FB_VISUAL_PSEUDOCOLOR;
434 else {
435 /*
436 * Some people have weird ideas about wanting static
437 * pseudocolor maps. I suspect their user space
438 * applications are broken.
439 */
440 fbi->fb.fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
441 }
442
443 fbi->fb.fix.line_length = var->xres_virtual *
444 var->bits_per_pixel / 8;
Stefan Schmidtc1450f12008-07-09 08:06:32 +0100445 if (var->bits_per_pixel >= 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 fbi->palette_size = 0;
447 else
eric miaob0086ef2008-04-30 00:52:19 -0700448 fbi->palette_size = var->bits_per_pixel == 1 ?
449 4 : 1 << var->bits_per_pixel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
eric miao2c42dd82008-04-30 00:52:21 -0700451 fbi->palette_cpu = (u16 *)&fbi->dma_buff->palette[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 /*
454 * Set (any) board control register to handle new color depth
455 */
456 pxafb_set_truecolor(fbi->fb.fix.visual == FB_VISUAL_TRUECOLOR);
457
Stefan Schmidtc1450f12008-07-09 08:06:32 +0100458 if (fbi->fb.var.bits_per_pixel >= 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 fb_dealloc_cmap(&fbi->fb.cmap);
460 else
461 fb_alloc_cmap(&fbi->fb.cmap, 1<<fbi->fb.var.bits_per_pixel, 0);
462
463 pxafb_activate_var(var, fbi);
464
465 return 0;
466}
467
468/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 * pxafb_blank():
470 * Blank the display by setting all palette values to zero. Note, the
471 * 16 bpp mode does not really use the palette, so this will not
472 * blank the display in all modes.
473 */
474static int pxafb_blank(int blank, struct fb_info *info)
475{
476 struct pxafb_info *fbi = (struct pxafb_info *)info;
477 int i;
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 switch (blank) {
480 case FB_BLANK_POWERDOWN:
481 case FB_BLANK_VSYNC_SUSPEND:
482 case FB_BLANK_HSYNC_SUSPEND:
483 case FB_BLANK_NORMAL:
484 if (fbi->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR ||
485 fbi->fb.fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
486 for (i = 0; i < fbi->palette_size; i++)
487 pxafb_setpalettereg(i, 0, 0, 0, 0, info);
488
489 pxafb_schedule_work(fbi, C_DISABLE);
eric miaob0086ef2008-04-30 00:52:19 -0700490 /* TODO if (pxafb_blank_helper) pxafb_blank_helper(blank); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 break;
492
493 case FB_BLANK_UNBLANK:
eric miaob0086ef2008-04-30 00:52:19 -0700494 /* TODO if (pxafb_blank_helper) pxafb_blank_helper(blank); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (fbi->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR ||
496 fbi->fb.fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
497 fb_set_cmap(&fbi->fb.cmap, info);
498 pxafb_schedule_work(fbi, C_ENABLE);
499 }
500 return 0;
501}
502
Christoph Hellwig216d5262006-01-14 13:21:25 -0800503static int pxafb_mmap(struct fb_info *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 struct vm_area_struct *vma)
505{
506 struct pxafb_info *fbi = (struct pxafb_info *)info;
507 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
508
509 if (off < info->fix.smem_len) {
Eric Miao3c42a442008-04-30 00:52:26 -0700510 vma->vm_pgoff += fbi->video_offset / PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return dma_mmap_writecombine(fbi->dev, vma, fbi->map_cpu,
512 fbi->map_dma, fbi->map_size);
513 }
514 return -EINVAL;
515}
516
517static struct fb_ops pxafb_ops = {
518 .owner = THIS_MODULE,
519 .fb_check_var = pxafb_check_var,
520 .fb_set_par = pxafb_set_par,
521 .fb_setcolreg = pxafb_setcolreg,
522 .fb_fillrect = cfb_fillrect,
523 .fb_copyarea = cfb_copyarea,
524 .fb_imageblit = cfb_imageblit,
525 .fb_blank = pxafb_blank,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 .fb_mmap = pxafb_mmap,
527};
528
529/*
530 * Calculate the PCD value from the clock rate (in picoseconds).
531 * We take account of the PPCR clock setting.
532 * From PXA Developer's Manual:
533 *
534 * PixelClock = LCLK
535 * -------------
536 * 2 ( PCD + 1 )
537 *
538 * PCD = LCLK
539 * ------------- - 1
540 * 2(PixelClock)
541 *
542 * Where:
543 * LCLK = LCD/Memory Clock
544 * PCD = LCCR3[7:0]
545 *
546 * PixelClock here is in Hz while the pixclock argument given is the
547 * period in picoseconds. Hence PixelClock = 1 / ( pixclock * 10^-12 )
548 *
549 * The function get_lclk_frequency_10khz returns LCLK in units of
550 * 10khz. Calling the result of this function lclk gives us the
551 * following
552 *
553 * PCD = (lclk * 10^4 ) * ( pixclock * 10^-12 )
554 * -------------------------------------- - 1
555 * 2
556 *
557 * Factoring the 10^4 and 10^-12 out gives 10^-8 == 1 / 100000000 as used below.
558 */
eric miaob0086ef2008-04-30 00:52:19 -0700559static inline unsigned int get_pcd(struct pxafb_info *fbi,
560 unsigned int pixclock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
562 unsigned long long pcd;
563
564 /* FIXME: Need to take into account Double Pixel Clock mode
Russell King72e35242007-08-20 10:18:42 +0100565 * (DPC) bit? or perhaps set it based on the various clock
566 * speeds */
567 pcd = (unsigned long long)(clk_get_rate(fbi->clk) / 10000);
568 pcd *= pixclock;
Nicolas Pitrebf1b8ab2005-06-23 21:56:45 +0100569 do_div(pcd, 100000000 * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 /* no need for this, since we should subtract 1 anyway. they cancel */
571 /* pcd += 1; */ /* make up for integer math truncations */
572 return (unsigned int)pcd;
573}
574
575/*
Richard Purdieba44cd22005-09-09 13:10:03 -0700576 * Some touchscreens need hsync information from the video driver to
Russell King72e35242007-08-20 10:18:42 +0100577 * function correctly. We export it here. Note that 'hsync_time' and
578 * the value returned from pxafb_get_hsync_time() is the *reciprocal*
579 * of the hsync period in seconds.
Richard Purdieba44cd22005-09-09 13:10:03 -0700580 */
581static inline void set_hsync_time(struct pxafb_info *fbi, unsigned int pcd)
582{
Russell King72e35242007-08-20 10:18:42 +0100583 unsigned long htime;
Richard Purdieba44cd22005-09-09 13:10:03 -0700584
585 if ((pcd == 0) || (fbi->fb.var.hsync_len == 0)) {
eric miaob0086ef2008-04-30 00:52:19 -0700586 fbi->hsync_time = 0;
Richard Purdieba44cd22005-09-09 13:10:03 -0700587 return;
588 }
589
Russell King72e35242007-08-20 10:18:42 +0100590 htime = clk_get_rate(fbi->clk) / (pcd * fbi->fb.var.hsync_len);
591
Richard Purdieba44cd22005-09-09 13:10:03 -0700592 fbi->hsync_time = htime;
593}
594
595unsigned long pxafb_get_hsync_time(struct device *dev)
596{
597 struct pxafb_info *fbi = dev_get_drvdata(dev);
598
599 /* If display is blanked/suspended, hsync isn't active */
600 if (!fbi || (fbi->state != C_ENABLE))
601 return 0;
602
603 return fbi->hsync_time;
604}
605EXPORT_SYMBOL(pxafb_get_hsync_time);
606
eric miao2c42dd82008-04-30 00:52:21 -0700607static int setup_frame_dma(struct pxafb_info *fbi, int dma, int pal,
608 unsigned int offset, size_t size)
609{
610 struct pxafb_dma_descriptor *dma_desc, *pal_desc;
611 unsigned int dma_desc_off, pal_desc_off;
612
613 if (dma < 0 || dma >= DMA_MAX)
614 return -EINVAL;
615
616 dma_desc = &fbi->dma_buff->dma_desc[dma];
617 dma_desc_off = offsetof(struct pxafb_dma_buff, dma_desc[dma]);
618
619 dma_desc->fsadr = fbi->screen_dma + offset;
620 dma_desc->fidr = 0;
621 dma_desc->ldcmd = size;
622
623 if (pal < 0 || pal >= PAL_MAX) {
624 dma_desc->fdadr = fbi->dma_buff_phys + dma_desc_off;
625 fbi->fdadr[dma] = fbi->dma_buff_phys + dma_desc_off;
626 } else {
627 pal_desc = &fbi->dma_buff->pal_desc[dma];
628 pal_desc_off = offsetof(struct pxafb_dma_buff, dma_desc[pal]);
629
630 pal_desc->fsadr = fbi->dma_buff_phys + pal * PALETTE_SIZE;
631 pal_desc->fidr = 0;
632
633 if ((fbi->lccr4 & LCCR4_PAL_FOR_MASK) == LCCR4_PAL_FOR_0)
634 pal_desc->ldcmd = fbi->palette_size * sizeof(u16);
635 else
636 pal_desc->ldcmd = fbi->palette_size * sizeof(u32);
637
638 pal_desc->ldcmd |= LDCMD_PAL;
639
640 /* flip back and forth between palette and frame buffer */
641 pal_desc->fdadr = fbi->dma_buff_phys + dma_desc_off;
642 dma_desc->fdadr = fbi->dma_buff_phys + pal_desc_off;
643 fbi->fdadr[dma] = fbi->dma_buff_phys + dma_desc_off;
644 }
645
646 return 0;
647}
648
Eric Miao3c42a442008-04-30 00:52:26 -0700649#ifdef CONFIG_FB_PXA_SMARTPANEL
650static int setup_smart_dma(struct pxafb_info *fbi)
651{
652 struct pxafb_dma_descriptor *dma_desc;
653 unsigned long dma_desc_off, cmd_buff_off;
654
655 dma_desc = &fbi->dma_buff->dma_desc[DMA_CMD];
656 dma_desc_off = offsetof(struct pxafb_dma_buff, dma_desc[DMA_CMD]);
657 cmd_buff_off = offsetof(struct pxafb_dma_buff, cmd_buff);
658
659 dma_desc->fdadr = fbi->dma_buff_phys + dma_desc_off;
660 dma_desc->fsadr = fbi->dma_buff_phys + cmd_buff_off;
661 dma_desc->fidr = 0;
662 dma_desc->ldcmd = fbi->n_smart_cmds * sizeof(uint16_t);
663
664 fbi->fdadr[DMA_CMD] = dma_desc->fdadr;
665 return 0;
666}
667
668int pxafb_smart_flush(struct fb_info *info)
669{
670 struct pxafb_info *fbi = container_of(info, struct pxafb_info, fb);
671 uint32_t prsr;
672 int ret = 0;
673
674 /* disable controller until all registers are set up */
675 lcd_writel(fbi, LCCR0, fbi->reg_lccr0 & ~LCCR0_ENB);
676
677 /* 1. make it an even number of commands to align on 32-bit boundary
678 * 2. add the interrupt command to the end of the chain so we can
679 * keep track of the end of the transfer
680 */
681
682 while (fbi->n_smart_cmds & 1)
683 fbi->smart_cmds[fbi->n_smart_cmds++] = SMART_CMD_NOOP;
684
685 fbi->smart_cmds[fbi->n_smart_cmds++] = SMART_CMD_INTERRUPT;
686 fbi->smart_cmds[fbi->n_smart_cmds++] = SMART_CMD_WAIT_FOR_VSYNC;
687 setup_smart_dma(fbi);
688
689 /* continue to execute next command */
690 prsr = lcd_readl(fbi, PRSR) | PRSR_ST_OK | PRSR_CON_NT;
691 lcd_writel(fbi, PRSR, prsr);
692
693 /* stop the processor in case it executed "wait for sync" cmd */
694 lcd_writel(fbi, CMDCR, 0x0001);
695
696 /* don't send interrupts for fifo underruns on channel 6 */
697 lcd_writel(fbi, LCCR5, LCCR5_IUM(6));
698
699 lcd_writel(fbi, LCCR1, fbi->reg_lccr1);
700 lcd_writel(fbi, LCCR2, fbi->reg_lccr2);
701 lcd_writel(fbi, LCCR3, fbi->reg_lccr3);
702 lcd_writel(fbi, FDADR0, fbi->fdadr[0]);
703 lcd_writel(fbi, FDADR6, fbi->fdadr[6]);
704
705 /* begin sending */
706 lcd_writel(fbi, LCCR0, fbi->reg_lccr0 | LCCR0_ENB);
707
708 if (wait_for_completion_timeout(&fbi->command_done, HZ/2) == 0) {
709 pr_warning("%s: timeout waiting for command done\n",
710 __func__);
711 ret = -ETIMEDOUT;
712 }
713
714 /* quick disable */
715 prsr = lcd_readl(fbi, PRSR) & ~(PRSR_ST_OK | PRSR_CON_NT);
716 lcd_writel(fbi, PRSR, prsr);
717 lcd_writel(fbi, LCCR0, fbi->reg_lccr0 & ~LCCR0_ENB);
718 lcd_writel(fbi, FDADR6, 0);
719 fbi->n_smart_cmds = 0;
720 return ret;
721}
722
723int pxafb_smart_queue(struct fb_info *info, uint16_t *cmds, int n_cmds)
724{
725 int i;
726 struct pxafb_info *fbi = container_of(info, struct pxafb_info, fb);
727
728 /* leave 2 commands for INTERRUPT and WAIT_FOR_SYNC */
729 for (i = 0; i < n_cmds; i++) {
730 if (fbi->n_smart_cmds == CMD_BUFF_SIZE - 8)
731 pxafb_smart_flush(info);
732
733 fbi->smart_cmds[fbi->n_smart_cmds++] = *cmds++;
734 }
735
736 return 0;
737}
738
739static unsigned int __smart_timing(unsigned time_ns, unsigned long lcd_clk)
740{
741 unsigned int t = (time_ns * (lcd_clk / 1000000) / 1000);
742 return (t == 0) ? 1 : t;
743}
744
745static void setup_smart_timing(struct pxafb_info *fbi,
746 struct fb_var_screeninfo *var)
747{
748 struct pxafb_mach_info *inf = fbi->dev->platform_data;
749 struct pxafb_mode_info *mode = &inf->modes[0];
750 unsigned long lclk = clk_get_rate(fbi->clk);
751 unsigned t1, t2, t3, t4;
752
753 t1 = max(mode->a0csrd_set_hld, mode->a0cswr_set_hld);
754 t2 = max(mode->rd_pulse_width, mode->wr_pulse_width);
755 t3 = mode->op_hold_time;
756 t4 = mode->cmd_inh_time;
757
758 fbi->reg_lccr1 =
759 LCCR1_DisWdth(var->xres) |
760 LCCR1_BegLnDel(__smart_timing(t1, lclk)) |
761 LCCR1_EndLnDel(__smart_timing(t2, lclk)) |
762 LCCR1_HorSnchWdth(__smart_timing(t3, lclk));
763
764 fbi->reg_lccr2 = LCCR2_DisHght(var->yres);
765 fbi->reg_lccr3 = LCCR3_PixClkDiv(__smart_timing(t4, lclk));
766
767 /* FIXME: make this configurable */
768 fbi->reg_cmdcr = 1;
769}
770
771static int pxafb_smart_thread(void *arg)
772{
Eric Miao7f1133c2008-04-30 00:52:27 -0700773 struct pxafb_info *fbi = arg;
Eric Miao3c42a442008-04-30 00:52:26 -0700774 struct pxafb_mach_info *inf = fbi->dev->platform_data;
775
776 if (!fbi || !inf->smart_update) {
777 pr_err("%s: not properly initialized, thread terminated\n",
778 __func__);
779 return -EINVAL;
780 }
781
782 pr_debug("%s(): task starting\n", __func__);
783
784 set_freezable();
785 while (!kthread_should_stop()) {
786
787 if (try_to_freeze())
788 continue;
789
790 if (fbi->state == C_ENABLE) {
791 inf->smart_update(&fbi->fb);
792 complete(&fbi->refresh_done);
793 }
794
795 set_current_state(TASK_INTERRUPTIBLE);
796 schedule_timeout(30 * HZ / 1000);
797 }
798
799 pr_debug("%s(): task ending\n", __func__);
800 return 0;
801}
802
803static int pxafb_smart_init(struct pxafb_info *fbi)
804{
805 fbi->smart_thread = kthread_run(pxafb_smart_thread, fbi,
806 "lcd_refresh");
807 if (IS_ERR(fbi->smart_thread)) {
808 printk(KERN_ERR "%s: unable to create kernel thread\n",
809 __func__);
810 return PTR_ERR(fbi->smart_thread);
811 }
812 return 0;
813}
814#else
815int pxafb_smart_queue(struct fb_info *info, uint16_t *cmds, int n_cmds)
816{
817 return 0;
818}
819
820int pxafb_smart_flush(struct fb_info *info)
821{
822 return 0;
823}
824#endif /* CONFIG_FB_SMART_PANEL */
825
Eric Miao90eabbf2008-04-30 00:52:25 -0700826static void setup_parallel_timing(struct pxafb_info *fbi,
827 struct fb_var_screeninfo *var)
828{
829 unsigned int lines_per_panel, pcd = get_pcd(fbi, var->pixclock);
830
831 fbi->reg_lccr1 =
832 LCCR1_DisWdth(var->xres) +
833 LCCR1_HorSnchWdth(var->hsync_len) +
834 LCCR1_BegLnDel(var->left_margin) +
835 LCCR1_EndLnDel(var->right_margin);
836
837 /*
838 * If we have a dual scan LCD, we need to halve
839 * the YRES parameter.
840 */
841 lines_per_panel = var->yres;
842 if ((fbi->lccr0 & LCCR0_SDS) == LCCR0_Dual)
843 lines_per_panel /= 2;
844
845 fbi->reg_lccr2 =
846 LCCR2_DisHght(lines_per_panel) +
847 LCCR2_VrtSnchWdth(var->vsync_len) +
848 LCCR2_BegFrmDel(var->upper_margin) +
849 LCCR2_EndFrmDel(var->lower_margin);
850
851 fbi->reg_lccr3 = fbi->lccr3 |
852 (var->sync & FB_SYNC_HOR_HIGH_ACT ?
853 LCCR3_HorSnchH : LCCR3_HorSnchL) |
854 (var->sync & FB_SYNC_VERT_HIGH_ACT ?
855 LCCR3_VrtSnchH : LCCR3_VrtSnchL);
856
857 if (pcd) {
858 fbi->reg_lccr3 |= LCCR3_PixClkDiv(pcd);
859 set_hsync_time(fbi, pcd);
860 }
861}
862
Richard Purdieba44cd22005-09-09 13:10:03 -0700863/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 * pxafb_activate_var():
eric miaob0086ef2008-04-30 00:52:19 -0700865 * Configures LCD Controller based on entries in var parameter.
866 * Settings are only written to the controller if changes were made.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 */
eric miaob0086ef2008-04-30 00:52:19 -0700868static int pxafb_activate_var(struct fb_var_screeninfo *var,
869 struct pxafb_info *fbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 u_long flags;
eric miao2c42dd82008-04-30 00:52:21 -0700872 size_t nbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874#if DEBUG_VAR
Eric Miao3c42a442008-04-30 00:52:26 -0700875 if (!(fbi->lccr0 & LCCR0_LCDT)) {
876 if (var->xres < 16 || var->xres > 1024)
877 printk(KERN_ERR "%s: invalid xres %d\n",
878 fbi->fb.fix.id, var->xres);
879 switch (var->bits_per_pixel) {
880 case 1:
881 case 2:
882 case 4:
883 case 8:
884 case 16:
Stefan Schmidtc1450f12008-07-09 08:06:32 +0100885 case 24:
886 case 32:
Eric Miao3c42a442008-04-30 00:52:26 -0700887 break;
888 default:
889 printk(KERN_ERR "%s: invalid bit depth %d\n",
890 fbi->fb.fix.id, var->bits_per_pixel);
891 break;
892 }
893
894 if (var->hsync_len < 1 || var->hsync_len > 64)
895 printk(KERN_ERR "%s: invalid hsync_len %d\n",
896 fbi->fb.fix.id, var->hsync_len);
897 if (var->left_margin < 1 || var->left_margin > 255)
898 printk(KERN_ERR "%s: invalid left_margin %d\n",
899 fbi->fb.fix.id, var->left_margin);
900 if (var->right_margin < 1 || var->right_margin > 255)
901 printk(KERN_ERR "%s: invalid right_margin %d\n",
902 fbi->fb.fix.id, var->right_margin);
903 if (var->yres < 1 || var->yres > 1024)
904 printk(KERN_ERR "%s: invalid yres %d\n",
905 fbi->fb.fix.id, var->yres);
906 if (var->vsync_len < 1 || var->vsync_len > 64)
907 printk(KERN_ERR "%s: invalid vsync_len %d\n",
908 fbi->fb.fix.id, var->vsync_len);
909 if (var->upper_margin < 0 || var->upper_margin > 255)
910 printk(KERN_ERR "%s: invalid upper_margin %d\n",
911 fbi->fb.fix.id, var->upper_margin);
912 if (var->lower_margin < 0 || var->lower_margin > 255)
913 printk(KERN_ERR "%s: invalid lower_margin %d\n",
914 fbi->fb.fix.id, var->lower_margin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 /* Update shadow copy atomically */
918 local_irq_save(flags);
919
Eric Miao3c42a442008-04-30 00:52:26 -0700920#ifdef CONFIG_FB_PXA_SMARTPANEL
921 if (fbi->lccr0 & LCCR0_LCDT)
922 setup_smart_timing(fbi, var);
923 else
924#endif
925 setup_parallel_timing(fbi, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Eric Miao90eabbf2008-04-30 00:52:25 -0700927 fbi->reg_lccr0 = fbi->lccr0 |
928 (LCCR0_LDM | LCCR0_SFM | LCCR0_IUM | LCCR0_EFM |
929 LCCR0_QDM | LCCR0_BM | LCCR0_OUM);
930
931 fbi->reg_lccr3 |= pxafb_bpp_to_lccr3(var);
932
933 nbytes = var->yres * fbi->fb.fix.line_length;
934
935 if ((fbi->lccr0 & LCCR0_SDS) == LCCR0_Dual) {
936 nbytes = nbytes / 2;
eric miao2c42dd82008-04-30 00:52:21 -0700937 setup_frame_dma(fbi, DMA_LOWER, PAL_NONE, nbytes, nbytes);
Eric Miao90eabbf2008-04-30 00:52:25 -0700938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Eric Miao3c42a442008-04-30 00:52:26 -0700940 if ((var->bits_per_pixel >= 16) || (fbi->lccr0 & LCCR0_LCDT))
eric miao2c42dd82008-04-30 00:52:21 -0700941 setup_frame_dma(fbi, DMA_BASE, PAL_NONE, 0, nbytes);
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700942 else
eric miao2c42dd82008-04-30 00:52:21 -0700943 setup_frame_dma(fbi, DMA_BASE, PAL_BASE, 0, nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Eric Miaoa7535ba2008-04-30 00:52:24 -0700945 fbi->reg_lccr4 = lcd_readl(fbi, LCCR4) & ~LCCR4_PAL_FOR_MASK;
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700946 fbi->reg_lccr4 |= (fbi->lccr4 & LCCR4_PAL_FOR_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 local_irq_restore(flags);
948
949 /*
950 * Only update the registers if the controller is enabled
951 * and something has changed.
952 */
Eric Miaoa7535ba2008-04-30 00:52:24 -0700953 if ((lcd_readl(fbi, LCCR0) != fbi->reg_lccr0) ||
954 (lcd_readl(fbi, LCCR1) != fbi->reg_lccr1) ||
955 (lcd_readl(fbi, LCCR2) != fbi->reg_lccr2) ||
956 (lcd_readl(fbi, LCCR3) != fbi->reg_lccr3) ||
957 (lcd_readl(fbi, FDADR0) != fbi->fdadr[0]) ||
958 (lcd_readl(fbi, FDADR1) != fbi->fdadr[1]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 pxafb_schedule_work(fbi, C_REENABLE);
960
961 return 0;
962}
963
964/*
965 * NOTE! The following functions are purely helpers for set_ctrlr_state.
966 * Do not call them directly; set_ctrlr_state does the correct serialisation
967 * to ensure that things happen in the right way 100% of time time.
968 * -- rmk
969 */
970static inline void __pxafb_backlight_power(struct pxafb_info *fbi, int on)
971{
Russell Kingca5da712005-09-29 09:44:54 +0100972 pr_debug("pxafb: backlight o%s\n", on ? "n" : "ff");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
eric miaob0086ef2008-04-30 00:52:19 -0700974 if (pxafb_backlight_power)
975 pxafb_backlight_power(on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976}
977
978static inline void __pxafb_lcd_power(struct pxafb_info *fbi, int on)
979{
Russell Kingca5da712005-09-29 09:44:54 +0100980 pr_debug("pxafb: LCD power o%s\n", on ? "n" : "ff");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 if (pxafb_lcd_power)
Richard Purdied14b2722006-09-20 22:54:21 +0100983 pxafb_lcd_power(on, &fbi->fb.var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
985
986static void pxafb_setup_gpio(struct pxafb_info *fbi)
987{
988 int gpio, ldd_bits;
eric miaob0086ef2008-04-30 00:52:19 -0700989 unsigned int lccr0 = fbi->lccr0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 /*
992 * setup is based on type of panel supported
eric miaob0086ef2008-04-30 00:52:19 -0700993 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 /* 4 bit interface */
996 if ((lccr0 & LCCR0_CMS) == LCCR0_Mono &&
997 (lccr0 & LCCR0_SDS) == LCCR0_Sngl &&
998 (lccr0 & LCCR0_DPD) == LCCR0_4PixMono)
999 ldd_bits = 4;
1000
1001 /* 8 bit interface */
eric miaob0086ef2008-04-30 00:52:19 -07001002 else if (((lccr0 & LCCR0_CMS) == LCCR0_Mono &&
1003 ((lccr0 & LCCR0_SDS) == LCCR0_Dual ||
1004 (lccr0 & LCCR0_DPD) == LCCR0_8PixMono)) ||
1005 ((lccr0 & LCCR0_CMS) == LCCR0_Color &&
1006 (lccr0 & LCCR0_PAS) == LCCR0_Pas &&
1007 (lccr0 & LCCR0_SDS) == LCCR0_Sngl))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 ldd_bits = 8;
1009
1010 /* 16 bit interface */
1011 else if ((lccr0 & LCCR0_CMS) == LCCR0_Color &&
eric miaob0086ef2008-04-30 00:52:19 -07001012 ((lccr0 & LCCR0_SDS) == LCCR0_Dual ||
1013 (lccr0 & LCCR0_PAS) == LCCR0_Act))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 ldd_bits = 16;
1015
1016 else {
eric miaob0086ef2008-04-30 00:52:19 -07001017 printk(KERN_ERR "pxafb_setup_gpio: unable to determine "
1018 "bits per pixel\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return;
eric miaob0086ef2008-04-30 00:52:19 -07001020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 for (gpio = 58; ldd_bits; gpio++, ldd_bits--)
1023 pxa_gpio_mode(gpio | GPIO_ALT_FN_2_OUT);
Stefan Schmidtc1450f12008-07-09 08:06:32 +01001024 /* 18 bit interface */
1025 if (fbi->fb.var.bits_per_pixel > 16) {
1026 pxa_gpio_mode(86 | GPIO_ALT_FN_2_OUT);
1027 pxa_gpio_mode(87 | GPIO_ALT_FN_2_OUT);
1028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 pxa_gpio_mode(GPIO74_LCD_FCLK_MD);
1030 pxa_gpio_mode(GPIO75_LCD_LCLK_MD);
1031 pxa_gpio_mode(GPIO76_LCD_PCLK_MD);
1032 pxa_gpio_mode(GPIO77_LCD_ACBIAS_MD);
1033}
1034
1035static void pxafb_enable_controller(struct pxafb_info *fbi)
1036{
Russell Kingca5da712005-09-29 09:44:54 +01001037 pr_debug("pxafb: Enabling LCD controller\n");
eric miao2c42dd82008-04-30 00:52:21 -07001038 pr_debug("fdadr0 0x%08x\n", (unsigned int) fbi->fdadr[0]);
1039 pr_debug("fdadr1 0x%08x\n", (unsigned int) fbi->fdadr[1]);
Russell Kingca5da712005-09-29 09:44:54 +01001040 pr_debug("reg_lccr0 0x%08x\n", (unsigned int) fbi->reg_lccr0);
1041 pr_debug("reg_lccr1 0x%08x\n", (unsigned int) fbi->reg_lccr1);
1042 pr_debug("reg_lccr2 0x%08x\n", (unsigned int) fbi->reg_lccr2);
1043 pr_debug("reg_lccr3 0x%08x\n", (unsigned int) fbi->reg_lccr3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Nicolas Pitre8d372262005-08-10 16:45:13 +01001045 /* enable LCD controller clock */
Russell King72e35242007-08-20 10:18:42 +01001046 clk_enable(fbi->clk);
Nicolas Pitre8d372262005-08-10 16:45:13 +01001047
Eric Miao3c42a442008-04-30 00:52:26 -07001048 if (fbi->lccr0 & LCCR0_LCDT)
1049 return;
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 /* Sequence from 11.7.10 */
Eric Miaoa7535ba2008-04-30 00:52:24 -07001052 lcd_writel(fbi, LCCR3, fbi->reg_lccr3);
1053 lcd_writel(fbi, LCCR2, fbi->reg_lccr2);
1054 lcd_writel(fbi, LCCR1, fbi->reg_lccr1);
1055 lcd_writel(fbi, LCCR0, fbi->reg_lccr0 & ~LCCR0_ENB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Eric Miaoa7535ba2008-04-30 00:52:24 -07001057 lcd_writel(fbi, FDADR0, fbi->fdadr[0]);
1058 lcd_writel(fbi, FDADR1, fbi->fdadr[1]);
1059 lcd_writel(fbi, LCCR0, fbi->reg_lccr0 | LCCR0_ENB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061
1062static void pxafb_disable_controller(struct pxafb_info *fbi)
1063{
eric miaoce4fb7b2008-04-30 00:52:21 -07001064 uint32_t lccr0;
1065
Eric Miao3c42a442008-04-30 00:52:26 -07001066#ifdef CONFIG_FB_PXA_SMARTPANEL
1067 if (fbi->lccr0 & LCCR0_LCDT) {
1068 wait_for_completion_timeout(&fbi->refresh_done,
1069 200 * HZ / 1000);
1070 return;
1071 }
1072#endif
1073
eric miaoce4fb7b2008-04-30 00:52:21 -07001074 /* Clear LCD Status Register */
Eric Miaoa7535ba2008-04-30 00:52:24 -07001075 lcd_writel(fbi, LCSR, 0xffffffff);
eric miaoce4fb7b2008-04-30 00:52:21 -07001076
Eric Miaoa7535ba2008-04-30 00:52:24 -07001077 lccr0 = lcd_readl(fbi, LCCR0) & ~LCCR0_LDM;
1078 lcd_writel(fbi, LCCR0, lccr0);
1079 lcd_writel(fbi, LCCR0, lccr0 | LCCR0_DIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Eric Miao2ba162b2008-04-30 00:52:24 -07001081 wait_for_completion_timeout(&fbi->disable_done, 200 * HZ / 1000);
Nicolas Pitre8d372262005-08-10 16:45:13 +01001082
1083 /* disable LCD controller clock */
Russell King72e35242007-08-20 10:18:42 +01001084 clk_disable(fbi->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085}
1086
1087/*
1088 * pxafb_handle_irq: Handle 'LCD DONE' interrupts.
1089 */
David Howells7d12e782006-10-05 14:55:46 +01001090static irqreturn_t pxafb_handle_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
1092 struct pxafb_info *fbi = dev_id;
Eric Miaoa7535ba2008-04-30 00:52:24 -07001093 unsigned int lccr0, lcsr = lcd_readl(fbi, LCSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095 if (lcsr & LCSR_LDD) {
Eric Miaoa7535ba2008-04-30 00:52:24 -07001096 lccr0 = lcd_readl(fbi, LCCR0);
1097 lcd_writel(fbi, LCCR0, lccr0 | LCCR0_LDM);
Eric Miao2ba162b2008-04-30 00:52:24 -07001098 complete(&fbi->disable_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
1100
Eric Miao3c42a442008-04-30 00:52:26 -07001101#ifdef CONFIG_FB_PXA_SMARTPANEL
1102 if (lcsr & LCSR_CMD_INT)
1103 complete(&fbi->command_done);
1104#endif
1105
Eric Miaoa7535ba2008-04-30 00:52:24 -07001106 lcd_writel(fbi, LCSR, lcsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return IRQ_HANDLED;
1108}
1109
1110/*
1111 * This function must be called from task context only, since it will
1112 * sleep when disabling the LCD controller, or if we get two contending
1113 * processes trying to alter state.
1114 */
1115static void set_ctrlr_state(struct pxafb_info *fbi, u_int state)
1116{
1117 u_int old_state;
1118
1119 down(&fbi->ctrlr_sem);
1120
1121 old_state = fbi->state;
1122
1123 /*
1124 * Hack around fbcon initialisation.
1125 */
1126 if (old_state == C_STARTUP && state == C_REENABLE)
1127 state = C_ENABLE;
1128
1129 switch (state) {
1130 case C_DISABLE_CLKCHANGE:
1131 /*
1132 * Disable controller for clock change. If the
1133 * controller is already disabled, then do nothing.
1134 */
1135 if (old_state != C_DISABLE && old_state != C_DISABLE_PM) {
1136 fbi->state = state;
eric miaob0086ef2008-04-30 00:52:19 -07001137 /* TODO __pxafb_lcd_power(fbi, 0); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 pxafb_disable_controller(fbi);
1139 }
1140 break;
1141
1142 case C_DISABLE_PM:
1143 case C_DISABLE:
1144 /*
1145 * Disable controller
1146 */
1147 if (old_state != C_DISABLE) {
1148 fbi->state = state;
1149 __pxafb_backlight_power(fbi, 0);
1150 __pxafb_lcd_power(fbi, 0);
1151 if (old_state != C_DISABLE_CLKCHANGE)
1152 pxafb_disable_controller(fbi);
1153 }
1154 break;
1155
1156 case C_ENABLE_CLKCHANGE:
1157 /*
1158 * Enable the controller after clock change. Only
1159 * do this if we were disabled for the clock change.
1160 */
1161 if (old_state == C_DISABLE_CLKCHANGE) {
1162 fbi->state = C_ENABLE;
1163 pxafb_enable_controller(fbi);
eric miaob0086ef2008-04-30 00:52:19 -07001164 /* TODO __pxafb_lcd_power(fbi, 1); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
1166 break;
1167
1168 case C_REENABLE:
1169 /*
1170 * Re-enable the controller only if it was already
1171 * enabled. This is so we reprogram the control
1172 * registers.
1173 */
1174 if (old_state == C_ENABLE) {
Richard Purdied14b2722006-09-20 22:54:21 +01001175 __pxafb_lcd_power(fbi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 pxafb_disable_controller(fbi);
1177 pxafb_setup_gpio(fbi);
1178 pxafb_enable_controller(fbi);
Richard Purdied14b2722006-09-20 22:54:21 +01001179 __pxafb_lcd_power(fbi, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
1181 break;
1182
1183 case C_ENABLE_PM:
1184 /*
1185 * Re-enable the controller after PM. This is not
1186 * perfect - think about the case where we were doing
1187 * a clock change, and we suspended half-way through.
1188 */
1189 if (old_state != C_DISABLE_PM)
1190 break;
1191 /* fall through */
1192
1193 case C_ENABLE:
1194 /*
1195 * Power up the LCD screen, enable controller, and
1196 * turn on the backlight.
1197 */
1198 if (old_state != C_ENABLE) {
1199 fbi->state = C_ENABLE;
1200 pxafb_setup_gpio(fbi);
1201 pxafb_enable_controller(fbi);
1202 __pxafb_lcd_power(fbi, 1);
1203 __pxafb_backlight_power(fbi, 1);
1204 }
1205 break;
1206 }
1207 up(&fbi->ctrlr_sem);
1208}
1209
1210/*
1211 * Our LCD controller task (which is called when we blank or unblank)
1212 * via keventd.
1213 */
David Howells6d5aefb2006-12-05 19:36:26 +00001214static void pxafb_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
David Howells6d5aefb2006-12-05 19:36:26 +00001216 struct pxafb_info *fbi =
1217 container_of(work, struct pxafb_info, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 u_int state = xchg(&fbi->task_state, -1);
1219
1220 set_ctrlr_state(fbi, state);
1221}
1222
1223#ifdef CONFIG_CPU_FREQ
1224/*
1225 * CPU clock speed change handler. We need to adjust the LCD timing
1226 * parameters when the CPU clock is adjusted by the power management
1227 * subsystem.
1228 *
1229 * TODO: Determine why f->new != 10*get_lclk_frequency_10khz()
1230 */
1231static int
1232pxafb_freq_transition(struct notifier_block *nb, unsigned long val, void *data)
1233{
1234 struct pxafb_info *fbi = TO_INF(nb, freq_transition);
eric miaob0086ef2008-04-30 00:52:19 -07001235 /* TODO struct cpufreq_freqs *f = data; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 u_int pcd;
1237
1238 switch (val) {
1239 case CPUFREQ_PRECHANGE:
1240 set_ctrlr_state(fbi, C_DISABLE_CLKCHANGE);
1241 break;
1242
1243 case CPUFREQ_POSTCHANGE:
Russell King72e35242007-08-20 10:18:42 +01001244 pcd = get_pcd(fbi, fbi->fb.var.pixclock);
Richard Purdieba44cd22005-09-09 13:10:03 -07001245 set_hsync_time(fbi, pcd);
eric miaob0086ef2008-04-30 00:52:19 -07001246 fbi->reg_lccr3 = (fbi->reg_lccr3 & ~0xff) |
1247 LCCR3_PixClkDiv(pcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 set_ctrlr_state(fbi, C_ENABLE_CLKCHANGE);
1249 break;
1250 }
1251 return 0;
1252}
1253
1254static int
1255pxafb_freq_policy(struct notifier_block *nb, unsigned long val, void *data)
1256{
1257 struct pxafb_info *fbi = TO_INF(nb, freq_policy);
1258 struct fb_var_screeninfo *var = &fbi->fb.var;
1259 struct cpufreq_policy *policy = data;
1260
1261 switch (val) {
1262 case CPUFREQ_ADJUST:
1263 case CPUFREQ_INCOMPATIBLE:
Holger Schurigac2bf5b2008-02-11 16:52:30 +01001264 pr_debug("min dma period: %d ps, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 "new clock %d kHz\n", pxafb_display_dma_period(var),
1266 policy->max);
eric miaob0086ef2008-04-30 00:52:19 -07001267 /* TODO: fill in min/max values */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 }
1270 return 0;
1271}
1272#endif
1273
1274#ifdef CONFIG_PM
1275/*
1276 * Power management hooks. Note that we won't be called from IRQ context,
1277 * unlike the blank functions above, so we may sleep.
1278 */
Russell King3ae5eae2005-11-09 22:32:44 +00001279static int pxafb_suspend(struct platform_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
Russell King3ae5eae2005-11-09 22:32:44 +00001281 struct pxafb_info *fbi = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Russell King9480e302005-10-28 09:52:56 -07001283 set_ctrlr_state(fbi, C_DISABLE_PM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return 0;
1285}
1286
Russell King3ae5eae2005-11-09 22:32:44 +00001287static int pxafb_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
Russell King3ae5eae2005-11-09 22:32:44 +00001289 struct pxafb_info *fbi = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Russell King9480e302005-10-28 09:52:56 -07001291 set_ctrlr_state(fbi, C_ENABLE_PM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 return 0;
1293}
1294#else
1295#define pxafb_suspend NULL
1296#define pxafb_resume NULL
1297#endif
1298
1299/*
1300 * pxafb_map_video_memory():
1301 * Allocates the DRAM memory for the frame buffer. This buffer is
1302 * remapped into a non-cached, non-buffered, memory region to
1303 * allow palette and pixel writes to occur without flushing the
1304 * cache. Once this area is remapped, all virtual memory
1305 * access to the video memory should occur at the new region.
1306 */
1307static int __init pxafb_map_video_memory(struct pxafb_info *fbi)
1308{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 /*
1310 * We reserve one page for the palette, plus the size
1311 * of the framebuffer.
1312 */
Eric Miao3c42a442008-04-30 00:52:26 -07001313 fbi->video_offset = PAGE_ALIGN(sizeof(struct pxafb_dma_buff));
1314 fbi->map_size = PAGE_ALIGN(fbi->fb.fix.smem_len + fbi->video_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 fbi->map_cpu = dma_alloc_writecombine(fbi->dev, fbi->map_size,
1316 &fbi->map_dma, GFP_KERNEL);
1317
1318 if (fbi->map_cpu) {
1319 /* prevent initial garbage on screen */
1320 memset(fbi->map_cpu, 0, fbi->map_size);
Eric Miao3c42a442008-04-30 00:52:26 -07001321 fbi->fb.screen_base = fbi->map_cpu + fbi->video_offset;
1322 fbi->screen_dma = fbi->map_dma + fbi->video_offset;
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 /*
1325 * FIXME: this is actually the wrong thing to place in
1326 * smem_start. But fbdev suffers from the problem that
1327 * it needs an API which doesn't exist (in this case,
1328 * dma_writecombine_mmap)
1329 */
1330 fbi->fb.fix.smem_start = fbi->screen_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 fbi->palette_size = fbi->fb.var.bits_per_pixel == 8 ? 256 : 16;
1332
Eric Miao3c42a442008-04-30 00:52:26 -07001333 fbi->dma_buff = (void *) fbi->map_cpu;
eric miao2c42dd82008-04-30 00:52:21 -07001334 fbi->dma_buff_phys = fbi->map_dma;
Eric Miao3c42a442008-04-30 00:52:26 -07001335 fbi->palette_cpu = (u16 *) fbi->dma_buff->palette;
1336
1337#ifdef CONFIG_FB_PXA_SMARTPANEL
1338 fbi->smart_cmds = (uint16_t *) fbi->dma_buff->cmd_buff;
1339 fbi->n_smart_cmds = 0;
1340#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
1342
1343 return fbi->map_cpu ? 0 : -ENOMEM;
1344}
1345
eric miao84f43c32008-04-30 00:52:22 -07001346static void pxafb_decode_mode_info(struct pxafb_info *fbi,
1347 struct pxafb_mode_info *modes,
1348 unsigned int num_modes)
1349{
1350 unsigned int i, smemlen;
1351
1352 pxafb_setmode(&fbi->fb.var, &modes[0]);
1353
1354 for (i = 0; i < num_modes; i++) {
1355 smemlen = modes[i].xres * modes[i].yres * modes[i].bpp / 8;
1356 if (smemlen > fbi->fb.fix.smem_len)
1357 fbi->fb.fix.smem_len = smemlen;
1358 }
1359}
1360
Guennadi Liakhovetskiebdf9822008-05-05 15:31:44 +01001361static void pxafb_decode_mach_info(struct pxafb_info *fbi,
1362 struct pxafb_mach_info *inf)
eric miao84f43c32008-04-30 00:52:22 -07001363{
1364 unsigned int lcd_conn = inf->lcd_conn;
1365
1366 fbi->cmap_inverse = inf->cmap_inverse;
1367 fbi->cmap_static = inf->cmap_static;
1368
1369 switch (lcd_conn & 0xf) {
1370 case LCD_TYPE_MONO_STN:
1371 fbi->lccr0 = LCCR0_CMS;
1372 break;
1373 case LCD_TYPE_MONO_DSTN:
1374 fbi->lccr0 = LCCR0_CMS | LCCR0_SDS;
1375 break;
1376 case LCD_TYPE_COLOR_STN:
1377 fbi->lccr0 = 0;
1378 break;
1379 case LCD_TYPE_COLOR_DSTN:
1380 fbi->lccr0 = LCCR0_SDS;
1381 break;
1382 case LCD_TYPE_COLOR_TFT:
1383 fbi->lccr0 = LCCR0_PAS;
1384 break;
1385 case LCD_TYPE_SMART_PANEL:
1386 fbi->lccr0 = LCCR0_LCDT | LCCR0_PAS;
1387 break;
1388 default:
1389 /* fall back to backward compatibility way */
1390 fbi->lccr0 = inf->lccr0;
1391 fbi->lccr3 = inf->lccr3;
1392 fbi->lccr4 = inf->lccr4;
Guennadi Liakhovetskiebdf9822008-05-05 15:31:44 +01001393 goto decode_mode;
eric miao84f43c32008-04-30 00:52:22 -07001394 }
1395
1396 if (lcd_conn == LCD_MONO_STN_8BPP)
1397 fbi->lccr0 |= LCCR0_DPD;
1398
1399 fbi->lccr3 = LCCR3_Acb((inf->lcd_conn >> 10) & 0xff);
1400 fbi->lccr3 |= (lcd_conn & LCD_BIAS_ACTIVE_LOW) ? LCCR3_OEP : 0;
1401 fbi->lccr3 |= (lcd_conn & LCD_PCLK_EDGE_FALL) ? LCCR3_PCP : 0;
1402
Guennadi Liakhovetskiebdf9822008-05-05 15:31:44 +01001403decode_mode:
eric miao84f43c32008-04-30 00:52:22 -07001404 pxafb_decode_mode_info(fbi, inf->modes, inf->num_modes);
eric miao84f43c32008-04-30 00:52:22 -07001405}
1406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407static struct pxafb_info * __init pxafb_init_fbinfo(struct device *dev)
1408{
1409 struct pxafb_info *fbi;
1410 void *addr;
1411 struct pxafb_mach_info *inf = dev->platform_data;
Richard Purdied14b2722006-09-20 22:54:21 +01001412 struct pxafb_mode_info *mode = inf->modes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414 /* Alloc the pxafb_info and pseudo_palette in one step */
1415 fbi = kmalloc(sizeof(struct pxafb_info) + sizeof(u32) * 16, GFP_KERNEL);
1416 if (!fbi)
1417 return NULL;
1418
1419 memset(fbi, 0, sizeof(struct pxafb_info));
1420 fbi->dev = dev;
1421
Russell King72e35242007-08-20 10:18:42 +01001422 fbi->clk = clk_get(dev, "LCDCLK");
1423 if (IS_ERR(fbi->clk)) {
1424 kfree(fbi);
1425 return NULL;
1426 }
1427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 strcpy(fbi->fb.fix.id, PXA_NAME);
1429
1430 fbi->fb.fix.type = FB_TYPE_PACKED_PIXELS;
1431 fbi->fb.fix.type_aux = 0;
1432 fbi->fb.fix.xpanstep = 0;
1433 fbi->fb.fix.ypanstep = 0;
1434 fbi->fb.fix.ywrapstep = 0;
1435 fbi->fb.fix.accel = FB_ACCEL_NONE;
1436
1437 fbi->fb.var.nonstd = 0;
1438 fbi->fb.var.activate = FB_ACTIVATE_NOW;
1439 fbi->fb.var.height = -1;
1440 fbi->fb.var.width = -1;
1441 fbi->fb.var.accel_flags = 0;
1442 fbi->fb.var.vmode = FB_VMODE_NONINTERLACED;
1443
1444 fbi->fb.fbops = &pxafb_ops;
1445 fbi->fb.flags = FBINFO_DEFAULT;
1446 fbi->fb.node = -1;
1447
1448 addr = fbi;
1449 addr = addr + sizeof(struct pxafb_info);
1450 fbi->fb.pseudo_palette = addr;
1451
eric miaob0086ef2008-04-30 00:52:19 -07001452 fbi->state = C_STARTUP;
1453 fbi->task_state = (u_char)-1;
Richard Purdied14b2722006-09-20 22:54:21 +01001454
eric miao84f43c32008-04-30 00:52:22 -07001455 pxafb_decode_mach_info(fbi, inf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 init_waitqueue_head(&fbi->ctrlr_wait);
David Howells6d5aefb2006-12-05 19:36:26 +00001458 INIT_WORK(&fbi->task, pxafb_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 init_MUTEX(&fbi->ctrlr_sem);
Eric Miao2ba162b2008-04-30 00:52:24 -07001460 init_completion(&fbi->disable_done);
Eric Miao3c42a442008-04-30 00:52:26 -07001461#ifdef CONFIG_FB_PXA_SMARTPANEL
1462 init_completion(&fbi->command_done);
1463 init_completion(&fbi->refresh_done);
1464#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
1466 return fbi;
1467}
1468
1469#ifdef CONFIG_FB_PXA_PARAMETERS
eric miaob0086ef2008-04-30 00:52:19 -07001470static int __init parse_opt_mode(struct device *dev, const char *this_opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
1472 struct pxafb_mach_info *inf = dev->platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
eric miao817daf12008-04-30 00:52:18 -07001474 const char *name = this_opt+5;
1475 unsigned int namelen = strlen(name);
1476 int res_specified = 0, bpp_specified = 0;
1477 unsigned int xres = 0, yres = 0, bpp = 0;
1478 int yres_specified = 0;
1479 int i;
1480 for (i = namelen-1; i >= 0; i--) {
1481 switch (name[i]) {
1482 case '-':
1483 namelen = i;
1484 if (!bpp_specified && !yres_specified) {
1485 bpp = simple_strtoul(&name[i+1], NULL, 0);
1486 bpp_specified = 1;
1487 } else
1488 goto done;
1489 break;
1490 case 'x':
1491 if (!yres_specified) {
1492 yres = simple_strtoul(&name[i+1], NULL, 0);
1493 yres_specified = 1;
1494 } else
1495 goto done;
1496 break;
1497 case '0' ... '9':
1498 break;
1499 default:
1500 goto done;
1501 }
1502 }
1503 if (i < 0 && yres_specified) {
1504 xres = simple_strtoul(name, NULL, 0);
1505 res_specified = 1;
1506 }
1507done:
1508 if (res_specified) {
1509 dev_info(dev, "overriding resolution: %dx%d\n", xres, yres);
1510 inf->modes[0].xres = xres; inf->modes[0].yres = yres;
1511 }
1512 if (bpp_specified)
1513 switch (bpp) {
1514 case 1:
1515 case 2:
1516 case 4:
1517 case 8:
1518 case 16:
1519 inf->modes[0].bpp = bpp;
1520 dev_info(dev, "overriding bit depth: %d\n", bpp);
1521 break;
1522 default:
1523 dev_err(dev, "Depth %d is not valid\n", bpp);
1524 return -EINVAL;
1525 }
1526 return 0;
1527}
1528
eric miaob0086ef2008-04-30 00:52:19 -07001529static int __init parse_opt(struct device *dev, char *this_opt)
eric miao817daf12008-04-30 00:52:18 -07001530{
1531 struct pxafb_mach_info *inf = dev->platform_data;
1532 struct pxafb_mode_info *mode = &inf->modes[0];
1533 char s[64];
1534
1535 s[0] = '\0';
1536
1537 if (!strncmp(this_opt, "mode:", 5)) {
1538 return parse_opt_mode(dev, this_opt);
1539 } else if (!strncmp(this_opt, "pixclock:", 9)) {
1540 mode->pixclock = simple_strtoul(this_opt+9, NULL, 0);
1541 sprintf(s, "pixclock: %ld\n", mode->pixclock);
1542 } else if (!strncmp(this_opt, "left:", 5)) {
1543 mode->left_margin = simple_strtoul(this_opt+5, NULL, 0);
1544 sprintf(s, "left: %u\n", mode->left_margin);
1545 } else if (!strncmp(this_opt, "right:", 6)) {
1546 mode->right_margin = simple_strtoul(this_opt+6, NULL, 0);
1547 sprintf(s, "right: %u\n", mode->right_margin);
1548 } else if (!strncmp(this_opt, "upper:", 6)) {
1549 mode->upper_margin = simple_strtoul(this_opt+6, NULL, 0);
1550 sprintf(s, "upper: %u\n", mode->upper_margin);
1551 } else if (!strncmp(this_opt, "lower:", 6)) {
1552 mode->lower_margin = simple_strtoul(this_opt+6, NULL, 0);
1553 sprintf(s, "lower: %u\n", mode->lower_margin);
1554 } else if (!strncmp(this_opt, "hsynclen:", 9)) {
1555 mode->hsync_len = simple_strtoul(this_opt+9, NULL, 0);
1556 sprintf(s, "hsynclen: %u\n", mode->hsync_len);
1557 } else if (!strncmp(this_opt, "vsynclen:", 9)) {
1558 mode->vsync_len = simple_strtoul(this_opt+9, NULL, 0);
1559 sprintf(s, "vsynclen: %u\n", mode->vsync_len);
1560 } else if (!strncmp(this_opt, "hsync:", 6)) {
1561 if (simple_strtoul(this_opt+6, NULL, 0) == 0) {
1562 sprintf(s, "hsync: Active Low\n");
1563 mode->sync &= ~FB_SYNC_HOR_HIGH_ACT;
1564 } else {
1565 sprintf(s, "hsync: Active High\n");
1566 mode->sync |= FB_SYNC_HOR_HIGH_ACT;
1567 }
1568 } else if (!strncmp(this_opt, "vsync:", 6)) {
1569 if (simple_strtoul(this_opt+6, NULL, 0) == 0) {
1570 sprintf(s, "vsync: Active Low\n");
1571 mode->sync &= ~FB_SYNC_VERT_HIGH_ACT;
1572 } else {
1573 sprintf(s, "vsync: Active High\n");
1574 mode->sync |= FB_SYNC_VERT_HIGH_ACT;
1575 }
1576 } else if (!strncmp(this_opt, "dpc:", 4)) {
1577 if (simple_strtoul(this_opt+4, NULL, 0) == 0) {
1578 sprintf(s, "double pixel clock: false\n");
1579 inf->lccr3 &= ~LCCR3_DPC;
1580 } else {
1581 sprintf(s, "double pixel clock: true\n");
1582 inf->lccr3 |= LCCR3_DPC;
1583 }
1584 } else if (!strncmp(this_opt, "outputen:", 9)) {
1585 if (simple_strtoul(this_opt+9, NULL, 0) == 0) {
1586 sprintf(s, "output enable: active low\n");
1587 inf->lccr3 = (inf->lccr3 & ~LCCR3_OEP) | LCCR3_OutEnL;
1588 } else {
1589 sprintf(s, "output enable: active high\n");
1590 inf->lccr3 = (inf->lccr3 & ~LCCR3_OEP) | LCCR3_OutEnH;
1591 }
1592 } else if (!strncmp(this_opt, "pixclockpol:", 12)) {
1593 if (simple_strtoul(this_opt+12, NULL, 0) == 0) {
1594 sprintf(s, "pixel clock polarity: falling edge\n");
1595 inf->lccr3 = (inf->lccr3 & ~LCCR3_PCP) | LCCR3_PixFlEdg;
1596 } else {
1597 sprintf(s, "pixel clock polarity: rising edge\n");
1598 inf->lccr3 = (inf->lccr3 & ~LCCR3_PCP) | LCCR3_PixRsEdg;
1599 }
1600 } else if (!strncmp(this_opt, "color", 5)) {
1601 inf->lccr0 = (inf->lccr0 & ~LCCR0_CMS) | LCCR0_Color;
1602 } else if (!strncmp(this_opt, "mono", 4)) {
1603 inf->lccr0 = (inf->lccr0 & ~LCCR0_CMS) | LCCR0_Mono;
1604 } else if (!strncmp(this_opt, "active", 6)) {
1605 inf->lccr0 = (inf->lccr0 & ~LCCR0_PAS) | LCCR0_Act;
1606 } else if (!strncmp(this_opt, "passive", 7)) {
1607 inf->lccr0 = (inf->lccr0 & ~LCCR0_PAS) | LCCR0_Pas;
1608 } else if (!strncmp(this_opt, "single", 6)) {
1609 inf->lccr0 = (inf->lccr0 & ~LCCR0_SDS) | LCCR0_Sngl;
1610 } else if (!strncmp(this_opt, "dual", 4)) {
1611 inf->lccr0 = (inf->lccr0 & ~LCCR0_SDS) | LCCR0_Dual;
1612 } else if (!strncmp(this_opt, "4pix", 4)) {
1613 inf->lccr0 = (inf->lccr0 & ~LCCR0_DPD) | LCCR0_4PixMono;
1614 } else if (!strncmp(this_opt, "8pix", 4)) {
1615 inf->lccr0 = (inf->lccr0 & ~LCCR0_DPD) | LCCR0_8PixMono;
1616 } else {
1617 dev_err(dev, "unknown option: %s\n", this_opt);
1618 return -EINVAL;
1619 }
1620
1621 if (s[0] != '\0')
1622 dev_info(dev, "override %s", s);
1623
1624 return 0;
1625}
1626
1627static int __init pxafb_parse_options(struct device *dev, char *options)
1628{
1629 char *this_opt;
1630 int ret;
1631
1632 if (!options || !*options)
1633 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
1635 dev_dbg(dev, "options are \"%s\"\n", options ? options : "null");
1636
1637 /* could be made table driven or similar?... */
eric miao817daf12008-04-30 00:52:18 -07001638 while ((this_opt = strsep(&options, ",")) != NULL) {
1639 ret = parse_opt(dev, this_opt);
1640 if (ret)
1641 return ret;
1642 }
1643 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644}
eric miao92ac73c2008-04-30 00:52:20 -07001645
1646static char g_options[256] __devinitdata = "";
1647
1648#ifndef CONFIG_MODULES
1649static int __devinit pxafb_setup_options(void)
1650{
1651 char *options = NULL;
1652
1653 if (fb_get_options("pxafb", &options))
1654 return -ENODEV;
1655
1656 if (options)
1657 strlcpy(g_options, options, sizeof(g_options));
1658
1659 return 0;
1660}
1661#else
1662#define pxafb_setup_options() (0)
1663
1664module_param_string(options, g_options, sizeof(g_options), 0);
1665MODULE_PARM_DESC(options, "LCD parameters (see Documentation/fb/pxafb.txt)");
1666#endif
1667
1668#else
1669#define pxafb_parse_options(...) (0)
1670#define pxafb_setup_options() (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671#endif
1672
Holger Schurigac2bf5b2008-02-11 16:52:30 +01001673static int __init pxafb_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
1675 struct pxafb_info *fbi;
1676 struct pxafb_mach_info *inf;
eric miaoce4fb7b2008-04-30 00:52:21 -07001677 struct resource *r;
1678 int irq, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Richard Purdie2cbbb3b2006-03-31 02:31:53 -08001680 dev_dbg(&dev->dev, "pxafb_probe\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Russell King3ae5eae2005-11-09 22:32:44 +00001682 inf = dev->dev.platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 ret = -ENOMEM;
1684 fbi = NULL;
1685 if (!inf)
1686 goto failed;
1687
Russell King3ae5eae2005-11-09 22:32:44 +00001688 ret = pxafb_parse_options(&dev->dev, g_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 if (ret < 0)
1690 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
1692#ifdef DEBUG_VAR
eric miaob0086ef2008-04-30 00:52:19 -07001693 /* Check for various illegal bit-combinations. Currently only
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 * a warning is given. */
1695
eric miaob0086ef2008-04-30 00:52:19 -07001696 if (inf->lccr0 & LCCR0_INVALID_CONFIG_MASK)
1697 dev_warn(&dev->dev, "machine LCCR0 setting contains "
1698 "illegal bits: %08x\n",
1699 inf->lccr0 & LCCR0_INVALID_CONFIG_MASK);
1700 if (inf->lccr3 & LCCR3_INVALID_CONFIG_MASK)
1701 dev_warn(&dev->dev, "machine LCCR3 setting contains "
1702 "illegal bits: %08x\n",
1703 inf->lccr3 & LCCR3_INVALID_CONFIG_MASK);
1704 if (inf->lccr0 & LCCR0_DPD &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 ((inf->lccr0 & LCCR0_PAS) != LCCR0_Pas ||
1706 (inf->lccr0 & LCCR0_SDS) != LCCR0_Sngl ||
1707 (inf->lccr0 & LCCR0_CMS) != LCCR0_Mono))
eric miaob0086ef2008-04-30 00:52:19 -07001708 dev_warn(&dev->dev, "Double Pixel Data (DPD) mode is "
1709 "only valid in passive mono"
1710 " single panel mode\n");
1711 if ((inf->lccr0 & LCCR0_PAS) == LCCR0_Act &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 (inf->lccr0 & LCCR0_SDS) == LCCR0_Dual)
eric miaob0086ef2008-04-30 00:52:19 -07001713 dev_warn(&dev->dev, "Dual panel only valid in passive mode\n");
1714 if ((inf->lccr0 & LCCR0_PAS) == LCCR0_Pas &&
1715 (inf->modes->upper_margin || inf->modes->lower_margin))
1716 dev_warn(&dev->dev, "Upper and lower margins must be 0 in "
1717 "passive mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718#endif
1719
eric miaob0086ef2008-04-30 00:52:19 -07001720 dev_dbg(&dev->dev, "got a %dx%dx%d LCD\n",
1721 inf->modes->xres,
1722 inf->modes->yres,
1723 inf->modes->bpp);
1724 if (inf->modes->xres == 0 ||
1725 inf->modes->yres == 0 ||
1726 inf->modes->bpp == 0) {
Russell King3ae5eae2005-11-09 22:32:44 +00001727 dev_err(&dev->dev, "Invalid resolution or bit depth\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 ret = -EINVAL;
1729 goto failed;
1730 }
1731 pxafb_backlight_power = inf->pxafb_backlight_power;
1732 pxafb_lcd_power = inf->pxafb_lcd_power;
Russell King3ae5eae2005-11-09 22:32:44 +00001733 fbi = pxafb_init_fbinfo(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 if (!fbi) {
eric miaob0086ef2008-04-30 00:52:19 -07001735 /* only reason for pxafb_init_fbinfo to fail is kmalloc */
Russell King3ae5eae2005-11-09 22:32:44 +00001736 dev_err(&dev->dev, "Failed to initialize framebuffer device\n");
eric miaob0086ef2008-04-30 00:52:19 -07001737 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 goto failed;
1739 }
1740
eric miaoce4fb7b2008-04-30 00:52:21 -07001741 r = platform_get_resource(dev, IORESOURCE_MEM, 0);
1742 if (r == NULL) {
1743 dev_err(&dev->dev, "no I/O memory resource defined\n");
1744 ret = -ENODEV;
1745 goto failed;
1746 }
1747
1748 r = request_mem_region(r->start, r->end - r->start + 1, dev->name);
1749 if (r == NULL) {
1750 dev_err(&dev->dev, "failed to request I/O memory\n");
1751 ret = -EBUSY;
1752 goto failed;
1753 }
1754
1755 fbi->mmio_base = ioremap(r->start, r->end - r->start + 1);
1756 if (fbi->mmio_base == NULL) {
1757 dev_err(&dev->dev, "failed to map I/O memory\n");
1758 ret = -EBUSY;
1759 goto failed_free_res;
1760 }
1761
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 /* Initialize video memory */
1763 ret = pxafb_map_video_memory(fbi);
1764 if (ret) {
Russell King3ae5eae2005-11-09 22:32:44 +00001765 dev_err(&dev->dev, "Failed to allocate video RAM: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 ret = -ENOMEM;
eric miaoce4fb7b2008-04-30 00:52:21 -07001767 goto failed_free_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
eric miaoce4fb7b2008-04-30 00:52:21 -07001770 irq = platform_get_irq(dev, 0);
1771 if (irq < 0) {
1772 dev_err(&dev->dev, "no IRQ defined\n");
1773 ret = -ENODEV;
1774 goto failed_free_mem;
1775 }
1776
1777 ret = request_irq(irq, pxafb_handle_irq, IRQF_DISABLED, "LCD", fbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 if (ret) {
Russell King3ae5eae2005-11-09 22:32:44 +00001779 dev_err(&dev->dev, "request_irq failed: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 ret = -EBUSY;
eric miaoce4fb7b2008-04-30 00:52:21 -07001781 goto failed_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 }
1783
Eric Miao3c42a442008-04-30 00:52:26 -07001784#ifdef CONFIG_FB_PXA_SMARTPANEL
1785 ret = pxafb_smart_init(fbi);
1786 if (ret) {
1787 dev_err(&dev->dev, "failed to initialize smartpanel\n");
1788 goto failed_free_irq;
1789 }
1790#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 /*
1792 * This makes sure that our colour bitfield
1793 * descriptors are correctly initialised.
1794 */
1795 pxafb_check_var(&fbi->fb.var, &fbi->fb);
1796 pxafb_set_par(&fbi->fb);
1797
Russell King3ae5eae2005-11-09 22:32:44 +00001798 platform_set_drvdata(dev, fbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
1800 ret = register_framebuffer(&fbi->fb);
1801 if (ret < 0) {
eric miaob0086ef2008-04-30 00:52:19 -07001802 dev_err(&dev->dev,
1803 "Failed to register framebuffer device: %d\n", ret);
eric miaoce4fb7b2008-04-30 00:52:21 -07001804 goto failed_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 }
1806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807#ifdef CONFIG_CPU_FREQ
1808 fbi->freq_transition.notifier_call = pxafb_freq_transition;
1809 fbi->freq_policy.notifier_call = pxafb_freq_policy;
eric miaob0086ef2008-04-30 00:52:19 -07001810 cpufreq_register_notifier(&fbi->freq_transition,
1811 CPUFREQ_TRANSITION_NOTIFIER);
1812 cpufreq_register_notifier(&fbi->freq_policy,
1813 CPUFREQ_POLICY_NOTIFIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814#endif
1815
1816 /*
1817 * Ok, now enable the LCD controller
1818 */
1819 set_ctrlr_state(fbi, C_ENABLE);
1820
1821 return 0;
1822
eric miaoce4fb7b2008-04-30 00:52:21 -07001823failed_free_irq:
1824 free_irq(irq, fbi);
1825failed_free_res:
1826 release_mem_region(r->start, r->end - r->start + 1);
1827failed_free_io:
1828 iounmap(fbi->mmio_base);
1829failed_free_mem:
1830 dma_free_writecombine(&dev->dev, fbi->map_size,
1831 fbi->map_cpu, fbi->map_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832failed:
Russell King3ae5eae2005-11-09 22:32:44 +00001833 platform_set_drvdata(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 kfree(fbi);
1835 return ret;
1836}
1837
Jaya Kumar9f17f282008-06-22 04:27:28 +01001838static int __devexit pxafb_remove(struct platform_device *dev)
1839{
1840 struct pxafb_info *fbi = platform_get_drvdata(dev);
1841 struct resource *r;
1842 int irq;
1843 struct fb_info *info;
1844
1845 if (!fbi)
1846 return 0;
1847
1848 info = &fbi->fb;
1849
1850 unregister_framebuffer(info);
1851
1852 pxafb_disable_controller(fbi);
1853
1854 if (fbi->fb.cmap.len)
1855 fb_dealloc_cmap(&fbi->fb.cmap);
1856
1857 irq = platform_get_irq(dev, 0);
1858 free_irq(irq, fbi);
1859
1860 dma_free_writecombine(&dev->dev, fbi->map_size,
1861 fbi->map_cpu, fbi->map_dma);
1862
1863 iounmap(fbi->mmio_base);
1864
1865 r = platform_get_resource(dev, IORESOURCE_MEM, 0);
1866 release_mem_region(r->start, r->end - r->start + 1);
1867
1868 clk_put(fbi->clk);
1869 kfree(fbi);
1870
1871 return 0;
1872}
1873
Russell King3ae5eae2005-11-09 22:32:44 +00001874static struct platform_driver pxafb_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 .probe = pxafb_probe,
Jaya Kumar9f17f282008-06-22 04:27:28 +01001876 .remove = pxafb_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 .suspend = pxafb_suspend,
1878 .resume = pxafb_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001879 .driver = {
Jaya Kumar9f17f282008-06-22 04:27:28 +01001880 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00001881 .name = "pxa2xx-fb",
1882 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883};
1884
Holger Schurigac2bf5b2008-02-11 16:52:30 +01001885static int __devinit pxafb_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
eric miao92ac73c2008-04-30 00:52:20 -07001887 if (pxafb_setup_options())
1888 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Russell King3ae5eae2005-11-09 22:32:44 +00001890 return platform_driver_register(&pxafb_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891}
1892
Jaya Kumar9f17f282008-06-22 04:27:28 +01001893static void __exit pxafb_exit(void)
1894{
1895 platform_driver_unregister(&pxafb_driver);
1896}
1897
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898module_init(pxafb_init);
Jaya Kumar9f17f282008-06-22 04:27:28 +01001899module_exit(pxafb_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
1901MODULE_DESCRIPTION("loadable framebuffer driver for PXA");
1902MODULE_LICENSE("GPL");