blob: 25bf4b8b6b5325684e0f905599b81730575d3d91 [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>
Andrea Righi27ac7922008-07-23 21:28:13 -070033#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/fb.h>
35#include <linux/delay.h>
36#include <linux/init.h>
37#include <linux/ioport.h>
38#include <linux/cpufreq.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010039#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/dma-mapping.h>
Russell King72e35242007-08-20 10:18:42 +010041#include <linux/clk.h>
42#include <linux/err.h>
Eric Miao2ba162b2008-04-30 00:52:24 -070043#include <linux/completion.h>
Matthias Kaehlckeb91dbce2008-07-23 21:31:14 -070044#include <linux/mutex.h>
Eric Miao3c42a442008-04-30 00:52:26 -070045#include <linux/kthread.h>
46#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Russell Kinga09e64f2008-08-05 16:14:15 +010048#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/io.h>
50#include <asm/irq.h>
Nicolas Pitrebf1b8ab2005-06-23 21:56:45 +010051#include <asm/div64.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010052#include <mach/pxa-regs.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010053#include <mach/bitfield.h>
54#include <mach/pxafb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/*
57 * Complain if VAR is out of range.
58 */
59#define DEBUG_VAR 1
60
61#include "pxafb.h"
62
63/* Bits which should not be set in machine configuration structures */
eric miaob0086ef2008-04-30 00:52:19 -070064#define LCCR0_INVALID_CONFIG_MASK (LCCR0_OUM | LCCR0_BM | LCCR0_QDM |\
65 LCCR0_DIS | LCCR0_EFM | LCCR0_IUM |\
66 LCCR0_SFM | LCCR0_LDM | LCCR0_ENB)
67
68#define LCCR3_INVALID_CONFIG_MASK (LCCR3_HSP | LCCR3_VSP |\
69 LCCR3_PCD | LCCR3_BPP)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
eric miaob0086ef2008-04-30 00:52:19 -070071static int pxafb_activate_var(struct fb_var_screeninfo *var,
72 struct pxafb_info *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static void set_ctrlr_state(struct pxafb_info *fbi, u_int state);
74
Eric Miao77e19672008-12-16 11:54:34 +080075static unsigned long video_mem_size = 0;
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503static struct fb_ops pxafb_ops = {
504 .owner = THIS_MODULE,
505 .fb_check_var = pxafb_check_var,
506 .fb_set_par = pxafb_set_par,
507 .fb_setcolreg = pxafb_setcolreg,
508 .fb_fillrect = cfb_fillrect,
509 .fb_copyarea = cfb_copyarea,
510 .fb_imageblit = cfb_imageblit,
511 .fb_blank = pxafb_blank,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512};
513
514/*
515 * Calculate the PCD value from the clock rate (in picoseconds).
516 * We take account of the PPCR clock setting.
517 * From PXA Developer's Manual:
518 *
519 * PixelClock = LCLK
520 * -------------
521 * 2 ( PCD + 1 )
522 *
523 * PCD = LCLK
524 * ------------- - 1
525 * 2(PixelClock)
526 *
527 * Where:
528 * LCLK = LCD/Memory Clock
529 * PCD = LCCR3[7:0]
530 *
531 * PixelClock here is in Hz while the pixclock argument given is the
532 * period in picoseconds. Hence PixelClock = 1 / ( pixclock * 10^-12 )
533 *
534 * The function get_lclk_frequency_10khz returns LCLK in units of
535 * 10khz. Calling the result of this function lclk gives us the
536 * following
537 *
538 * PCD = (lclk * 10^4 ) * ( pixclock * 10^-12 )
539 * -------------------------------------- - 1
540 * 2
541 *
542 * Factoring the 10^4 and 10^-12 out gives 10^-8 == 1 / 100000000 as used below.
543 */
eric miaob0086ef2008-04-30 00:52:19 -0700544static inline unsigned int get_pcd(struct pxafb_info *fbi,
545 unsigned int pixclock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
547 unsigned long long pcd;
548
549 /* FIXME: Need to take into account Double Pixel Clock mode
Russell King72e35242007-08-20 10:18:42 +0100550 * (DPC) bit? or perhaps set it based on the various clock
551 * speeds */
552 pcd = (unsigned long long)(clk_get_rate(fbi->clk) / 10000);
553 pcd *= pixclock;
Nicolas Pitrebf1b8ab2005-06-23 21:56:45 +0100554 do_div(pcd, 100000000 * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 /* no need for this, since we should subtract 1 anyway. they cancel */
556 /* pcd += 1; */ /* make up for integer math truncations */
557 return (unsigned int)pcd;
558}
559
560/*
Richard Purdieba44cd22005-09-09 13:10:03 -0700561 * Some touchscreens need hsync information from the video driver to
Russell King72e35242007-08-20 10:18:42 +0100562 * function correctly. We export it here. Note that 'hsync_time' and
563 * the value returned from pxafb_get_hsync_time() is the *reciprocal*
564 * of the hsync period in seconds.
Richard Purdieba44cd22005-09-09 13:10:03 -0700565 */
566static inline void set_hsync_time(struct pxafb_info *fbi, unsigned int pcd)
567{
Russell King72e35242007-08-20 10:18:42 +0100568 unsigned long htime;
Richard Purdieba44cd22005-09-09 13:10:03 -0700569
570 if ((pcd == 0) || (fbi->fb.var.hsync_len == 0)) {
eric miaob0086ef2008-04-30 00:52:19 -0700571 fbi->hsync_time = 0;
Richard Purdieba44cd22005-09-09 13:10:03 -0700572 return;
573 }
574
Russell King72e35242007-08-20 10:18:42 +0100575 htime = clk_get_rate(fbi->clk) / (pcd * fbi->fb.var.hsync_len);
576
Richard Purdieba44cd22005-09-09 13:10:03 -0700577 fbi->hsync_time = htime;
578}
579
580unsigned long pxafb_get_hsync_time(struct device *dev)
581{
582 struct pxafb_info *fbi = dev_get_drvdata(dev);
583
584 /* If display is blanked/suspended, hsync isn't active */
585 if (!fbi || (fbi->state != C_ENABLE))
586 return 0;
587
588 return fbi->hsync_time;
589}
590EXPORT_SYMBOL(pxafb_get_hsync_time);
591
eric miao2c42dd82008-04-30 00:52:21 -0700592static int setup_frame_dma(struct pxafb_info *fbi, int dma, int pal,
593 unsigned int offset, size_t size)
594{
595 struct pxafb_dma_descriptor *dma_desc, *pal_desc;
596 unsigned int dma_desc_off, pal_desc_off;
597
598 if (dma < 0 || dma >= DMA_MAX)
599 return -EINVAL;
600
601 dma_desc = &fbi->dma_buff->dma_desc[dma];
602 dma_desc_off = offsetof(struct pxafb_dma_buff, dma_desc[dma]);
603
Eric Miao77e19672008-12-16 11:54:34 +0800604 dma_desc->fsadr = fbi->video_mem_phys + offset;
eric miao2c42dd82008-04-30 00:52:21 -0700605 dma_desc->fidr = 0;
606 dma_desc->ldcmd = size;
607
608 if (pal < 0 || pal >= PAL_MAX) {
609 dma_desc->fdadr = fbi->dma_buff_phys + dma_desc_off;
610 fbi->fdadr[dma] = fbi->dma_buff_phys + dma_desc_off;
611 } else {
Jürgen Schindele62cfcf42008-06-11 19:56:06 +0100612 pal_desc = &fbi->dma_buff->pal_desc[pal];
613 pal_desc_off = offsetof(struct pxafb_dma_buff, pal_desc[pal]);
eric miao2c42dd82008-04-30 00:52:21 -0700614
615 pal_desc->fsadr = fbi->dma_buff_phys + pal * PALETTE_SIZE;
616 pal_desc->fidr = 0;
617
618 if ((fbi->lccr4 & LCCR4_PAL_FOR_MASK) == LCCR4_PAL_FOR_0)
619 pal_desc->ldcmd = fbi->palette_size * sizeof(u16);
620 else
621 pal_desc->ldcmd = fbi->palette_size * sizeof(u32);
622
623 pal_desc->ldcmd |= LDCMD_PAL;
624
625 /* flip back and forth between palette and frame buffer */
626 pal_desc->fdadr = fbi->dma_buff_phys + dma_desc_off;
627 dma_desc->fdadr = fbi->dma_buff_phys + pal_desc_off;
628 fbi->fdadr[dma] = fbi->dma_buff_phys + dma_desc_off;
629 }
630
631 return 0;
632}
633
Eric Miao3c42a442008-04-30 00:52:26 -0700634#ifdef CONFIG_FB_PXA_SMARTPANEL
635static int setup_smart_dma(struct pxafb_info *fbi)
636{
637 struct pxafb_dma_descriptor *dma_desc;
638 unsigned long dma_desc_off, cmd_buff_off;
639
640 dma_desc = &fbi->dma_buff->dma_desc[DMA_CMD];
641 dma_desc_off = offsetof(struct pxafb_dma_buff, dma_desc[DMA_CMD]);
642 cmd_buff_off = offsetof(struct pxafb_dma_buff, cmd_buff);
643
644 dma_desc->fdadr = fbi->dma_buff_phys + dma_desc_off;
645 dma_desc->fsadr = fbi->dma_buff_phys + cmd_buff_off;
646 dma_desc->fidr = 0;
647 dma_desc->ldcmd = fbi->n_smart_cmds * sizeof(uint16_t);
648
649 fbi->fdadr[DMA_CMD] = dma_desc->fdadr;
650 return 0;
651}
652
653int pxafb_smart_flush(struct fb_info *info)
654{
655 struct pxafb_info *fbi = container_of(info, struct pxafb_info, fb);
656 uint32_t prsr;
657 int ret = 0;
658
659 /* disable controller until all registers are set up */
660 lcd_writel(fbi, LCCR0, fbi->reg_lccr0 & ~LCCR0_ENB);
661
662 /* 1. make it an even number of commands to align on 32-bit boundary
663 * 2. add the interrupt command to the end of the chain so we can
664 * keep track of the end of the transfer
665 */
666
667 while (fbi->n_smart_cmds & 1)
668 fbi->smart_cmds[fbi->n_smart_cmds++] = SMART_CMD_NOOP;
669
670 fbi->smart_cmds[fbi->n_smart_cmds++] = SMART_CMD_INTERRUPT;
671 fbi->smart_cmds[fbi->n_smart_cmds++] = SMART_CMD_WAIT_FOR_VSYNC;
672 setup_smart_dma(fbi);
673
674 /* continue to execute next command */
675 prsr = lcd_readl(fbi, PRSR) | PRSR_ST_OK | PRSR_CON_NT;
676 lcd_writel(fbi, PRSR, prsr);
677
678 /* stop the processor in case it executed "wait for sync" cmd */
679 lcd_writel(fbi, CMDCR, 0x0001);
680
681 /* don't send interrupts for fifo underruns on channel 6 */
682 lcd_writel(fbi, LCCR5, LCCR5_IUM(6));
683
684 lcd_writel(fbi, LCCR1, fbi->reg_lccr1);
685 lcd_writel(fbi, LCCR2, fbi->reg_lccr2);
686 lcd_writel(fbi, LCCR3, fbi->reg_lccr3);
687 lcd_writel(fbi, FDADR0, fbi->fdadr[0]);
688 lcd_writel(fbi, FDADR6, fbi->fdadr[6]);
689
690 /* begin sending */
691 lcd_writel(fbi, LCCR0, fbi->reg_lccr0 | LCCR0_ENB);
692
693 if (wait_for_completion_timeout(&fbi->command_done, HZ/2) == 0) {
694 pr_warning("%s: timeout waiting for command done\n",
695 __func__);
696 ret = -ETIMEDOUT;
697 }
698
699 /* quick disable */
700 prsr = lcd_readl(fbi, PRSR) & ~(PRSR_ST_OK | PRSR_CON_NT);
701 lcd_writel(fbi, PRSR, prsr);
702 lcd_writel(fbi, LCCR0, fbi->reg_lccr0 & ~LCCR0_ENB);
703 lcd_writel(fbi, FDADR6, 0);
704 fbi->n_smart_cmds = 0;
705 return ret;
706}
707
708int pxafb_smart_queue(struct fb_info *info, uint16_t *cmds, int n_cmds)
709{
710 int i;
711 struct pxafb_info *fbi = container_of(info, struct pxafb_info, fb);
712
Eric Miao69bdea72008-12-08 18:46:00 +0800713 for (i = 0; i < n_cmds; i++, cmds++) {
714 /* if it is a software delay, flush and delay */
715 if ((*cmds & 0xff00) == SMART_CMD_DELAY) {
716 pxafb_smart_flush(info);
717 mdelay(*cmds & 0xff);
718 continue;
719 }
720
721 /* leave 2 commands for INTERRUPT and WAIT_FOR_SYNC */
Eric Miao3c42a442008-04-30 00:52:26 -0700722 if (fbi->n_smart_cmds == CMD_BUFF_SIZE - 8)
723 pxafb_smart_flush(info);
724
Eric Miao69bdea72008-12-08 18:46:00 +0800725 fbi->smart_cmds[fbi->n_smart_cmds++] = *cmds;
Eric Miao3c42a442008-04-30 00:52:26 -0700726 }
727
728 return 0;
729}
730
731static unsigned int __smart_timing(unsigned time_ns, unsigned long lcd_clk)
732{
733 unsigned int t = (time_ns * (lcd_clk / 1000000) / 1000);
734 return (t == 0) ? 1 : t;
735}
736
737static void setup_smart_timing(struct pxafb_info *fbi,
738 struct fb_var_screeninfo *var)
739{
740 struct pxafb_mach_info *inf = fbi->dev->platform_data;
741 struct pxafb_mode_info *mode = &inf->modes[0];
742 unsigned long lclk = clk_get_rate(fbi->clk);
743 unsigned t1, t2, t3, t4;
744
745 t1 = max(mode->a0csrd_set_hld, mode->a0cswr_set_hld);
746 t2 = max(mode->rd_pulse_width, mode->wr_pulse_width);
747 t3 = mode->op_hold_time;
748 t4 = mode->cmd_inh_time;
749
750 fbi->reg_lccr1 =
751 LCCR1_DisWdth(var->xres) |
752 LCCR1_BegLnDel(__smart_timing(t1, lclk)) |
753 LCCR1_EndLnDel(__smart_timing(t2, lclk)) |
754 LCCR1_HorSnchWdth(__smart_timing(t3, lclk));
755
756 fbi->reg_lccr2 = LCCR2_DisHght(var->yres);
Eric Miaoc1f99c22008-12-08 18:35:03 +0800757 fbi->reg_lccr3 = fbi->lccr3 | LCCR3_PixClkDiv(__smart_timing(t4, lclk));
758 fbi->reg_lccr3 |= (var->sync & FB_SYNC_HOR_HIGH_ACT) ? LCCR3_HSP : 0;
759 fbi->reg_lccr3 |= (var->sync & FB_SYNC_VERT_HIGH_ACT) ? LCCR3_VSP : 0;
Eric Miao3c42a442008-04-30 00:52:26 -0700760
761 /* FIXME: make this configurable */
762 fbi->reg_cmdcr = 1;
763}
764
765static int pxafb_smart_thread(void *arg)
766{
Eric Miao7f1133c2008-04-30 00:52:27 -0700767 struct pxafb_info *fbi = arg;
Eric Miao3c42a442008-04-30 00:52:26 -0700768 struct pxafb_mach_info *inf = fbi->dev->platform_data;
769
770 if (!fbi || !inf->smart_update) {
771 pr_err("%s: not properly initialized, thread terminated\n",
772 __func__);
773 return -EINVAL;
774 }
775
776 pr_debug("%s(): task starting\n", __func__);
777
778 set_freezable();
779 while (!kthread_should_stop()) {
780
781 if (try_to_freeze())
782 continue;
783
Eric Miao07f651c2008-12-08 18:51:01 +0800784 mutex_lock(&fbi->ctrlr_lock);
785
Eric Miao3c42a442008-04-30 00:52:26 -0700786 if (fbi->state == C_ENABLE) {
787 inf->smart_update(&fbi->fb);
788 complete(&fbi->refresh_done);
789 }
790
Eric Miao07f651c2008-12-08 18:51:01 +0800791 mutex_unlock(&fbi->ctrlr_lock);
792
Eric Miao3c42a442008-04-30 00:52:26 -0700793 set_current_state(TASK_INTERRUPTIBLE);
794 schedule_timeout(30 * HZ / 1000);
795 }
796
797 pr_debug("%s(): task ending\n", __func__);
798 return 0;
799}
800
801static int pxafb_smart_init(struct pxafb_info *fbi)
802{
Eric Miao07df1c42008-12-04 11:14:11 +0800803 if (!(fbi->lccr0 & LCCR0_LCDT))
Eric Miao6cc4abe2008-11-11 21:47:07 +0800804 return 0;
805
Eric Miao07df1c42008-12-04 11:14:11 +0800806 fbi->smart_cmds = (uint16_t *) fbi->dma_buff->cmd_buff;
807 fbi->n_smart_cmds = 0;
808
809 init_completion(&fbi->command_done);
810 init_completion(&fbi->refresh_done);
811
Eric Miao3c42a442008-04-30 00:52:26 -0700812 fbi->smart_thread = kthread_run(pxafb_smart_thread, fbi,
813 "lcd_refresh");
814 if (IS_ERR(fbi->smart_thread)) {
Eric Miao07df1c42008-12-04 11:14:11 +0800815 pr_err("%s: unable to create kernel thread\n", __func__);
Eric Miao3c42a442008-04-30 00:52:26 -0700816 return PTR_ERR(fbi->smart_thread);
817 }
Eric Miaoa5718a12008-11-11 21:50:39 +0800818
Eric Miao3c42a442008-04-30 00:52:26 -0700819 return 0;
820}
821#else
822int pxafb_smart_queue(struct fb_info *info, uint16_t *cmds, int n_cmds)
823{
824 return 0;
825}
826
827int pxafb_smart_flush(struct fb_info *info)
828{
829 return 0;
830}
Eric Miao07df1c42008-12-04 11:14:11 +0800831
832static inline int pxafb_smart_init(struct pxafb_info *fbi) { return 0; }
833#endif /* CONFIG_FB_PXA_SMARTPANEL */
Eric Miao3c42a442008-04-30 00:52:26 -0700834
Eric Miao90eabbf2008-04-30 00:52:25 -0700835static void setup_parallel_timing(struct pxafb_info *fbi,
836 struct fb_var_screeninfo *var)
837{
838 unsigned int lines_per_panel, pcd = get_pcd(fbi, var->pixclock);
839
840 fbi->reg_lccr1 =
841 LCCR1_DisWdth(var->xres) +
842 LCCR1_HorSnchWdth(var->hsync_len) +
843 LCCR1_BegLnDel(var->left_margin) +
844 LCCR1_EndLnDel(var->right_margin);
845
846 /*
847 * If we have a dual scan LCD, we need to halve
848 * the YRES parameter.
849 */
850 lines_per_panel = var->yres;
851 if ((fbi->lccr0 & LCCR0_SDS) == LCCR0_Dual)
852 lines_per_panel /= 2;
853
854 fbi->reg_lccr2 =
855 LCCR2_DisHght(lines_per_panel) +
856 LCCR2_VrtSnchWdth(var->vsync_len) +
857 LCCR2_BegFrmDel(var->upper_margin) +
858 LCCR2_EndFrmDel(var->lower_margin);
859
860 fbi->reg_lccr3 = fbi->lccr3 |
861 (var->sync & FB_SYNC_HOR_HIGH_ACT ?
862 LCCR3_HorSnchH : LCCR3_HorSnchL) |
863 (var->sync & FB_SYNC_VERT_HIGH_ACT ?
864 LCCR3_VrtSnchH : LCCR3_VrtSnchL);
865
866 if (pcd) {
867 fbi->reg_lccr3 |= LCCR3_PixClkDiv(pcd);
868 set_hsync_time(fbi, pcd);
869 }
870}
871
Richard Purdieba44cd22005-09-09 13:10:03 -0700872/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 * pxafb_activate_var():
eric miaob0086ef2008-04-30 00:52:19 -0700874 * Configures LCD Controller based on entries in var parameter.
875 * Settings are only written to the controller if changes were made.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 */
eric miaob0086ef2008-04-30 00:52:19 -0700877static int pxafb_activate_var(struct fb_var_screeninfo *var,
878 struct pxafb_info *fbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 u_long flags;
eric miao2c42dd82008-04-30 00:52:21 -0700881 size_t nbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883#if DEBUG_VAR
Eric Miao3c42a442008-04-30 00:52:26 -0700884 if (!(fbi->lccr0 & LCCR0_LCDT)) {
885 if (var->xres < 16 || var->xres > 1024)
886 printk(KERN_ERR "%s: invalid xres %d\n",
887 fbi->fb.fix.id, var->xres);
888 switch (var->bits_per_pixel) {
889 case 1:
890 case 2:
891 case 4:
892 case 8:
893 case 16:
Stefan Schmidtc1450f12008-07-09 08:06:32 +0100894 case 24:
895 case 32:
Eric Miao3c42a442008-04-30 00:52:26 -0700896 break;
897 default:
898 printk(KERN_ERR "%s: invalid bit depth %d\n",
899 fbi->fb.fix.id, var->bits_per_pixel);
900 break;
901 }
902
903 if (var->hsync_len < 1 || var->hsync_len > 64)
904 printk(KERN_ERR "%s: invalid hsync_len %d\n",
905 fbi->fb.fix.id, var->hsync_len);
906 if (var->left_margin < 1 || var->left_margin > 255)
907 printk(KERN_ERR "%s: invalid left_margin %d\n",
908 fbi->fb.fix.id, var->left_margin);
909 if (var->right_margin < 1 || var->right_margin > 255)
910 printk(KERN_ERR "%s: invalid right_margin %d\n",
911 fbi->fb.fix.id, var->right_margin);
912 if (var->yres < 1 || var->yres > 1024)
913 printk(KERN_ERR "%s: invalid yres %d\n",
914 fbi->fb.fix.id, var->yres);
915 if (var->vsync_len < 1 || var->vsync_len > 64)
916 printk(KERN_ERR "%s: invalid vsync_len %d\n",
917 fbi->fb.fix.id, var->vsync_len);
918 if (var->upper_margin < 0 || var->upper_margin > 255)
919 printk(KERN_ERR "%s: invalid upper_margin %d\n",
920 fbi->fb.fix.id, var->upper_margin);
921 if (var->lower_margin < 0 || var->lower_margin > 255)
922 printk(KERN_ERR "%s: invalid lower_margin %d\n",
923 fbi->fb.fix.id, var->lower_margin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 /* Update shadow copy atomically */
927 local_irq_save(flags);
928
Eric Miao3c42a442008-04-30 00:52:26 -0700929#ifdef CONFIG_FB_PXA_SMARTPANEL
930 if (fbi->lccr0 & LCCR0_LCDT)
931 setup_smart_timing(fbi, var);
932 else
933#endif
934 setup_parallel_timing(fbi, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Eric Miao90eabbf2008-04-30 00:52:25 -0700936 fbi->reg_lccr0 = fbi->lccr0 |
937 (LCCR0_LDM | LCCR0_SFM | LCCR0_IUM | LCCR0_EFM |
938 LCCR0_QDM | LCCR0_BM | LCCR0_OUM);
939
940 fbi->reg_lccr3 |= pxafb_bpp_to_lccr3(var);
941
942 nbytes = var->yres * fbi->fb.fix.line_length;
943
944 if ((fbi->lccr0 & LCCR0_SDS) == LCCR0_Dual) {
945 nbytes = nbytes / 2;
eric miao2c42dd82008-04-30 00:52:21 -0700946 setup_frame_dma(fbi, DMA_LOWER, PAL_NONE, nbytes, nbytes);
Eric Miao90eabbf2008-04-30 00:52:25 -0700947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Eric Miao3c42a442008-04-30 00:52:26 -0700949 if ((var->bits_per_pixel >= 16) || (fbi->lccr0 & LCCR0_LCDT))
eric miao2c42dd82008-04-30 00:52:21 -0700950 setup_frame_dma(fbi, DMA_BASE, PAL_NONE, 0, nbytes);
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700951 else
eric miao2c42dd82008-04-30 00:52:21 -0700952 setup_frame_dma(fbi, DMA_BASE, PAL_BASE, 0, nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Eric Miaoa7535ba2008-04-30 00:52:24 -0700954 fbi->reg_lccr4 = lcd_readl(fbi, LCCR4) & ~LCCR4_PAL_FOR_MASK;
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700955 fbi->reg_lccr4 |= (fbi->lccr4 & LCCR4_PAL_FOR_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 local_irq_restore(flags);
957
958 /*
959 * Only update the registers if the controller is enabled
960 * and something has changed.
961 */
Eric Miaoa7535ba2008-04-30 00:52:24 -0700962 if ((lcd_readl(fbi, LCCR0) != fbi->reg_lccr0) ||
963 (lcd_readl(fbi, LCCR1) != fbi->reg_lccr1) ||
964 (lcd_readl(fbi, LCCR2) != fbi->reg_lccr2) ||
965 (lcd_readl(fbi, LCCR3) != fbi->reg_lccr3) ||
966 (lcd_readl(fbi, FDADR0) != fbi->fdadr[0]) ||
967 (lcd_readl(fbi, FDADR1) != fbi->fdadr[1]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 pxafb_schedule_work(fbi, C_REENABLE);
969
970 return 0;
971}
972
973/*
974 * NOTE! The following functions are purely helpers for set_ctrlr_state.
975 * Do not call them directly; set_ctrlr_state does the correct serialisation
976 * to ensure that things happen in the right way 100% of time time.
977 * -- rmk
978 */
979static inline void __pxafb_backlight_power(struct pxafb_info *fbi, int on)
980{
Russell Kingca5da712005-09-29 09:44:54 +0100981 pr_debug("pxafb: backlight o%s\n", on ? "n" : "ff");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Eric Miaoa5718a12008-11-11 21:50:39 +0800983 if (fbi->backlight_power)
984 fbi->backlight_power(on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986
987static inline void __pxafb_lcd_power(struct pxafb_info *fbi, int on)
988{
Russell Kingca5da712005-09-29 09:44:54 +0100989 pr_debug("pxafb: LCD power o%s\n", on ? "n" : "ff");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Eric Miaoa5718a12008-11-11 21:50:39 +0800991 if (fbi->lcd_power)
992 fbi->lcd_power(on, &fbi->fb.var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993}
994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995static void pxafb_enable_controller(struct pxafb_info *fbi)
996{
Russell Kingca5da712005-09-29 09:44:54 +0100997 pr_debug("pxafb: Enabling LCD controller\n");
eric miao2c42dd82008-04-30 00:52:21 -0700998 pr_debug("fdadr0 0x%08x\n", (unsigned int) fbi->fdadr[0]);
999 pr_debug("fdadr1 0x%08x\n", (unsigned int) fbi->fdadr[1]);
Russell Kingca5da712005-09-29 09:44:54 +01001000 pr_debug("reg_lccr0 0x%08x\n", (unsigned int) fbi->reg_lccr0);
1001 pr_debug("reg_lccr1 0x%08x\n", (unsigned int) fbi->reg_lccr1);
1002 pr_debug("reg_lccr2 0x%08x\n", (unsigned int) fbi->reg_lccr2);
1003 pr_debug("reg_lccr3 0x%08x\n", (unsigned int) fbi->reg_lccr3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Nicolas Pitre8d372262005-08-10 16:45:13 +01001005 /* enable LCD controller clock */
Russell King72e35242007-08-20 10:18:42 +01001006 clk_enable(fbi->clk);
Nicolas Pitre8d372262005-08-10 16:45:13 +01001007
Eric Miao3c42a442008-04-30 00:52:26 -07001008 if (fbi->lccr0 & LCCR0_LCDT)
1009 return;
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 /* Sequence from 11.7.10 */
Eric Miaoa7535ba2008-04-30 00:52:24 -07001012 lcd_writel(fbi, LCCR3, fbi->reg_lccr3);
1013 lcd_writel(fbi, LCCR2, fbi->reg_lccr2);
1014 lcd_writel(fbi, LCCR1, fbi->reg_lccr1);
1015 lcd_writel(fbi, LCCR0, fbi->reg_lccr0 & ~LCCR0_ENB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Eric Miaoa7535ba2008-04-30 00:52:24 -07001017 lcd_writel(fbi, FDADR0, fbi->fdadr[0]);
1018 lcd_writel(fbi, FDADR1, fbi->fdadr[1]);
1019 lcd_writel(fbi, LCCR0, fbi->reg_lccr0 | LCCR0_ENB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
1021
1022static void pxafb_disable_controller(struct pxafb_info *fbi)
1023{
eric miaoce4fb7b2008-04-30 00:52:21 -07001024 uint32_t lccr0;
1025
Eric Miao3c42a442008-04-30 00:52:26 -07001026#ifdef CONFIG_FB_PXA_SMARTPANEL
1027 if (fbi->lccr0 & LCCR0_LCDT) {
1028 wait_for_completion_timeout(&fbi->refresh_done,
1029 200 * HZ / 1000);
1030 return;
1031 }
1032#endif
1033
eric miaoce4fb7b2008-04-30 00:52:21 -07001034 /* Clear LCD Status Register */
Eric Miaoa7535ba2008-04-30 00:52:24 -07001035 lcd_writel(fbi, LCSR, 0xffffffff);
eric miaoce4fb7b2008-04-30 00:52:21 -07001036
Eric Miaoa7535ba2008-04-30 00:52:24 -07001037 lccr0 = lcd_readl(fbi, LCCR0) & ~LCCR0_LDM;
1038 lcd_writel(fbi, LCCR0, lccr0);
1039 lcd_writel(fbi, LCCR0, lccr0 | LCCR0_DIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Eric Miao2ba162b2008-04-30 00:52:24 -07001041 wait_for_completion_timeout(&fbi->disable_done, 200 * HZ / 1000);
Nicolas Pitre8d372262005-08-10 16:45:13 +01001042
1043 /* disable LCD controller clock */
Russell King72e35242007-08-20 10:18:42 +01001044 clk_disable(fbi->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
1046
1047/*
1048 * pxafb_handle_irq: Handle 'LCD DONE' interrupts.
1049 */
David Howells7d12e782006-10-05 14:55:46 +01001050static irqreturn_t pxafb_handle_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
1052 struct pxafb_info *fbi = dev_id;
Eric Miaoa7535ba2008-04-30 00:52:24 -07001053 unsigned int lccr0, lcsr = lcd_readl(fbi, LCSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 if (lcsr & LCSR_LDD) {
Eric Miaoa7535ba2008-04-30 00:52:24 -07001056 lccr0 = lcd_readl(fbi, LCCR0);
1057 lcd_writel(fbi, LCCR0, lccr0 | LCCR0_LDM);
Eric Miao2ba162b2008-04-30 00:52:24 -07001058 complete(&fbi->disable_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
1060
Eric Miao3c42a442008-04-30 00:52:26 -07001061#ifdef CONFIG_FB_PXA_SMARTPANEL
1062 if (lcsr & LCSR_CMD_INT)
1063 complete(&fbi->command_done);
1064#endif
1065
Eric Miaoa7535ba2008-04-30 00:52:24 -07001066 lcd_writel(fbi, LCSR, lcsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 return IRQ_HANDLED;
1068}
1069
1070/*
1071 * This function must be called from task context only, since it will
1072 * sleep when disabling the LCD controller, or if we get two contending
1073 * processes trying to alter state.
1074 */
1075static void set_ctrlr_state(struct pxafb_info *fbi, u_int state)
1076{
1077 u_int old_state;
1078
Matthias Kaehlckeb91dbce2008-07-23 21:31:14 -07001079 mutex_lock(&fbi->ctrlr_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 old_state = fbi->state;
1082
1083 /*
1084 * Hack around fbcon initialisation.
1085 */
1086 if (old_state == C_STARTUP && state == C_REENABLE)
1087 state = C_ENABLE;
1088
1089 switch (state) {
1090 case C_DISABLE_CLKCHANGE:
1091 /*
1092 * Disable controller for clock change. If the
1093 * controller is already disabled, then do nothing.
1094 */
1095 if (old_state != C_DISABLE && old_state != C_DISABLE_PM) {
1096 fbi->state = state;
eric miaob0086ef2008-04-30 00:52:19 -07001097 /* TODO __pxafb_lcd_power(fbi, 0); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 pxafb_disable_controller(fbi);
1099 }
1100 break;
1101
1102 case C_DISABLE_PM:
1103 case C_DISABLE:
1104 /*
1105 * Disable controller
1106 */
1107 if (old_state != C_DISABLE) {
1108 fbi->state = state;
1109 __pxafb_backlight_power(fbi, 0);
1110 __pxafb_lcd_power(fbi, 0);
1111 if (old_state != C_DISABLE_CLKCHANGE)
1112 pxafb_disable_controller(fbi);
1113 }
1114 break;
1115
1116 case C_ENABLE_CLKCHANGE:
1117 /*
1118 * Enable the controller after clock change. Only
1119 * do this if we were disabled for the clock change.
1120 */
1121 if (old_state == C_DISABLE_CLKCHANGE) {
1122 fbi->state = C_ENABLE;
1123 pxafb_enable_controller(fbi);
eric miaob0086ef2008-04-30 00:52:19 -07001124 /* TODO __pxafb_lcd_power(fbi, 1); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 }
1126 break;
1127
1128 case C_REENABLE:
1129 /*
1130 * Re-enable the controller only if it was already
1131 * enabled. This is so we reprogram the control
1132 * registers.
1133 */
1134 if (old_state == C_ENABLE) {
Richard Purdied14b2722006-09-20 22:54:21 +01001135 __pxafb_lcd_power(fbi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 pxafb_disable_controller(fbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 pxafb_enable_controller(fbi);
Richard Purdied14b2722006-09-20 22:54:21 +01001138 __pxafb_lcd_power(fbi, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
1140 break;
1141
1142 case C_ENABLE_PM:
1143 /*
1144 * Re-enable the controller after PM. This is not
1145 * perfect - think about the case where we were doing
1146 * a clock change, and we suspended half-way through.
1147 */
1148 if (old_state != C_DISABLE_PM)
1149 break;
1150 /* fall through */
1151
1152 case C_ENABLE:
1153 /*
1154 * Power up the LCD screen, enable controller, and
1155 * turn on the backlight.
1156 */
1157 if (old_state != C_ENABLE) {
1158 fbi->state = C_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 pxafb_enable_controller(fbi);
1160 __pxafb_lcd_power(fbi, 1);
1161 __pxafb_backlight_power(fbi, 1);
1162 }
1163 break;
1164 }
Matthias Kaehlckeb91dbce2008-07-23 21:31:14 -07001165 mutex_unlock(&fbi->ctrlr_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166}
1167
1168/*
1169 * Our LCD controller task (which is called when we blank or unblank)
1170 * via keventd.
1171 */
David Howells6d5aefb2006-12-05 19:36:26 +00001172static void pxafb_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
David Howells6d5aefb2006-12-05 19:36:26 +00001174 struct pxafb_info *fbi =
1175 container_of(work, struct pxafb_info, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 u_int state = xchg(&fbi->task_state, -1);
1177
1178 set_ctrlr_state(fbi, state);
1179}
1180
1181#ifdef CONFIG_CPU_FREQ
1182/*
1183 * CPU clock speed change handler. We need to adjust the LCD timing
1184 * parameters when the CPU clock is adjusted by the power management
1185 * subsystem.
1186 *
1187 * TODO: Determine why f->new != 10*get_lclk_frequency_10khz()
1188 */
1189static int
1190pxafb_freq_transition(struct notifier_block *nb, unsigned long val, void *data)
1191{
1192 struct pxafb_info *fbi = TO_INF(nb, freq_transition);
eric miaob0086ef2008-04-30 00:52:19 -07001193 /* TODO struct cpufreq_freqs *f = data; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 u_int pcd;
1195
1196 switch (val) {
1197 case CPUFREQ_PRECHANGE:
1198 set_ctrlr_state(fbi, C_DISABLE_CLKCHANGE);
1199 break;
1200
1201 case CPUFREQ_POSTCHANGE:
Russell King72e35242007-08-20 10:18:42 +01001202 pcd = get_pcd(fbi, fbi->fb.var.pixclock);
Richard Purdieba44cd22005-09-09 13:10:03 -07001203 set_hsync_time(fbi, pcd);
eric miaob0086ef2008-04-30 00:52:19 -07001204 fbi->reg_lccr3 = (fbi->reg_lccr3 & ~0xff) |
1205 LCCR3_PixClkDiv(pcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 set_ctrlr_state(fbi, C_ENABLE_CLKCHANGE);
1207 break;
1208 }
1209 return 0;
1210}
1211
1212static int
1213pxafb_freq_policy(struct notifier_block *nb, unsigned long val, void *data)
1214{
1215 struct pxafb_info *fbi = TO_INF(nb, freq_policy);
1216 struct fb_var_screeninfo *var = &fbi->fb.var;
1217 struct cpufreq_policy *policy = data;
1218
1219 switch (val) {
1220 case CPUFREQ_ADJUST:
1221 case CPUFREQ_INCOMPATIBLE:
Holger Schurigac2bf5b2008-02-11 16:52:30 +01001222 pr_debug("min dma period: %d ps, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 "new clock %d kHz\n", pxafb_display_dma_period(var),
1224 policy->max);
eric miaob0086ef2008-04-30 00:52:19 -07001225 /* TODO: fill in min/max values */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 }
1228 return 0;
1229}
1230#endif
1231
1232#ifdef CONFIG_PM
1233/*
1234 * Power management hooks. Note that we won't be called from IRQ context,
1235 * unlike the blank functions above, so we may sleep.
1236 */
Russell King3ae5eae2005-11-09 22:32:44 +00001237static int pxafb_suspend(struct platform_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
Russell King3ae5eae2005-11-09 22:32:44 +00001239 struct pxafb_info *fbi = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Russell King9480e302005-10-28 09:52:56 -07001241 set_ctrlr_state(fbi, C_DISABLE_PM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return 0;
1243}
1244
Russell King3ae5eae2005-11-09 22:32:44 +00001245static int pxafb_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
Russell King3ae5eae2005-11-09 22:32:44 +00001247 struct pxafb_info *fbi = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Russell King9480e302005-10-28 09:52:56 -07001249 set_ctrlr_state(fbi, C_ENABLE_PM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 return 0;
1251}
1252#else
1253#define pxafb_suspend NULL
1254#define pxafb_resume NULL
1255#endif
1256
Eric Miao77e19672008-12-16 11:54:34 +08001257static int __devinit pxafb_init_video_memory(struct pxafb_info *fbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
Eric Miao77e19672008-12-16 11:54:34 +08001259 int size = PAGE_ALIGN(fbi->video_mem_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Eric Miao77e19672008-12-16 11:54:34 +08001261 fbi->video_mem = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
1262 if (fbi->video_mem == NULL)
1263 return -ENOMEM;
Eric Miao3c42a442008-04-30 00:52:26 -07001264
Eric Miao77e19672008-12-16 11:54:34 +08001265 fbi->video_mem_phys = virt_to_phys(fbi->video_mem);
1266 fbi->video_mem_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Eric Miao77e19672008-12-16 11:54:34 +08001268 fbi->fb.fix.smem_start = fbi->video_mem_phys;
1269 fbi->fb.fix.smem_len = fbi->video_mem_size;
1270 fbi->fb.screen_base = fbi->video_mem;
Eric Miao3c42a442008-04-30 00:52:26 -07001271
Eric Miao77e19672008-12-16 11:54:34 +08001272 return fbi->video_mem ? 0 : -ENOMEM;
eric miao84f43c32008-04-30 00:52:22 -07001273}
1274
Guennadi Liakhovetskiebdf9822008-05-05 15:31:44 +01001275static void pxafb_decode_mach_info(struct pxafb_info *fbi,
1276 struct pxafb_mach_info *inf)
eric miao84f43c32008-04-30 00:52:22 -07001277{
1278 unsigned int lcd_conn = inf->lcd_conn;
Eric Miao77e19672008-12-16 11:54:34 +08001279 struct pxafb_mode_info *m;
1280 int i;
eric miao84f43c32008-04-30 00:52:22 -07001281
1282 fbi->cmap_inverse = inf->cmap_inverse;
1283 fbi->cmap_static = inf->cmap_static;
1284
Eric Miao1ec26db2008-11-11 21:45:57 +08001285 switch (lcd_conn & LCD_TYPE_MASK) {
eric miao84f43c32008-04-30 00:52:22 -07001286 case LCD_TYPE_MONO_STN:
1287 fbi->lccr0 = LCCR0_CMS;
1288 break;
1289 case LCD_TYPE_MONO_DSTN:
1290 fbi->lccr0 = LCCR0_CMS | LCCR0_SDS;
1291 break;
1292 case LCD_TYPE_COLOR_STN:
1293 fbi->lccr0 = 0;
1294 break;
1295 case LCD_TYPE_COLOR_DSTN:
1296 fbi->lccr0 = LCCR0_SDS;
1297 break;
1298 case LCD_TYPE_COLOR_TFT:
1299 fbi->lccr0 = LCCR0_PAS;
1300 break;
1301 case LCD_TYPE_SMART_PANEL:
1302 fbi->lccr0 = LCCR0_LCDT | LCCR0_PAS;
1303 break;
1304 default:
1305 /* fall back to backward compatibility way */
1306 fbi->lccr0 = inf->lccr0;
1307 fbi->lccr3 = inf->lccr3;
1308 fbi->lccr4 = inf->lccr4;
Guennadi Liakhovetskiebdf9822008-05-05 15:31:44 +01001309 goto decode_mode;
eric miao84f43c32008-04-30 00:52:22 -07001310 }
1311
1312 if (lcd_conn == LCD_MONO_STN_8BPP)
1313 fbi->lccr0 |= LCCR0_DPD;
1314
Eric Miao9a1ac7e2008-08-15 02:50:44 -04001315 fbi->lccr0 |= (lcd_conn & LCD_ALTERNATE_MAPPING) ? LCCR0_LDDALT : 0;
1316
eric miao84f43c32008-04-30 00:52:22 -07001317 fbi->lccr3 = LCCR3_Acb((inf->lcd_conn >> 10) & 0xff);
1318 fbi->lccr3 |= (lcd_conn & LCD_BIAS_ACTIVE_LOW) ? LCCR3_OEP : 0;
1319 fbi->lccr3 |= (lcd_conn & LCD_PCLK_EDGE_FALL) ? LCCR3_PCP : 0;
1320
Guennadi Liakhovetskiebdf9822008-05-05 15:31:44 +01001321decode_mode:
Eric Miao77e19672008-12-16 11:54:34 +08001322 pxafb_setmode(&fbi->fb.var, &inf->modes[0]);
1323
1324 /* decide video memory size as follows:
1325 * 1. default to mode of maximum resolution
1326 * 2. allow platform to override
1327 * 3. allow module parameter to override
1328 */
1329 for (i = 0, m = &inf->modes[0]; i < inf->num_modes; i++, m++)
1330 fbi->video_mem_size = max_t(size_t, fbi->video_mem_size,
1331 m->xres * m->yres * m->bpp / 8);
1332
1333 if (inf->video_mem_size > fbi->video_mem_size)
1334 fbi->video_mem_size = inf->video_mem_size;
1335
1336 if (video_mem_size > fbi->video_mem_size)
1337 fbi->video_mem_size = video_mem_size;
eric miao84f43c32008-04-30 00:52:22 -07001338}
1339
Jaya Kumar9e6c2972008-06-22 04:27:27 +01001340static struct pxafb_info * __devinit pxafb_init_fbinfo(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
1342 struct pxafb_info *fbi;
1343 void *addr;
1344 struct pxafb_mach_info *inf = dev->platform_data;
1345
1346 /* Alloc the pxafb_info and pseudo_palette in one step */
1347 fbi = kmalloc(sizeof(struct pxafb_info) + sizeof(u32) * 16, GFP_KERNEL);
1348 if (!fbi)
1349 return NULL;
1350
1351 memset(fbi, 0, sizeof(struct pxafb_info));
1352 fbi->dev = dev;
1353
Russell King72e35242007-08-20 10:18:42 +01001354 fbi->clk = clk_get(dev, "LCDCLK");
1355 if (IS_ERR(fbi->clk)) {
1356 kfree(fbi);
1357 return NULL;
1358 }
1359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 strcpy(fbi->fb.fix.id, PXA_NAME);
1361
1362 fbi->fb.fix.type = FB_TYPE_PACKED_PIXELS;
1363 fbi->fb.fix.type_aux = 0;
1364 fbi->fb.fix.xpanstep = 0;
1365 fbi->fb.fix.ypanstep = 0;
1366 fbi->fb.fix.ywrapstep = 0;
1367 fbi->fb.fix.accel = FB_ACCEL_NONE;
1368
1369 fbi->fb.var.nonstd = 0;
1370 fbi->fb.var.activate = FB_ACTIVATE_NOW;
1371 fbi->fb.var.height = -1;
1372 fbi->fb.var.width = -1;
1373 fbi->fb.var.accel_flags = 0;
1374 fbi->fb.var.vmode = FB_VMODE_NONINTERLACED;
1375
1376 fbi->fb.fbops = &pxafb_ops;
1377 fbi->fb.flags = FBINFO_DEFAULT;
1378 fbi->fb.node = -1;
1379
1380 addr = fbi;
1381 addr = addr + sizeof(struct pxafb_info);
1382 fbi->fb.pseudo_palette = addr;
1383
eric miaob0086ef2008-04-30 00:52:19 -07001384 fbi->state = C_STARTUP;
1385 fbi->task_state = (u_char)-1;
Richard Purdied14b2722006-09-20 22:54:21 +01001386
eric miao84f43c32008-04-30 00:52:22 -07001387 pxafb_decode_mach_info(fbi, inf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389 init_waitqueue_head(&fbi->ctrlr_wait);
David Howells6d5aefb2006-12-05 19:36:26 +00001390 INIT_WORK(&fbi->task, pxafb_task);
Matthias Kaehlckeb91dbce2008-07-23 21:31:14 -07001391 mutex_init(&fbi->ctrlr_lock);
Eric Miao2ba162b2008-04-30 00:52:24 -07001392 init_completion(&fbi->disable_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 return fbi;
1395}
1396
1397#ifdef CONFIG_FB_PXA_PARAMETERS
Jaya Kumar9e6c2972008-06-22 04:27:27 +01001398static int __devinit parse_opt_mode(struct device *dev, const char *this_opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
1400 struct pxafb_mach_info *inf = dev->platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
eric miao817daf12008-04-30 00:52:18 -07001402 const char *name = this_opt+5;
1403 unsigned int namelen = strlen(name);
1404 int res_specified = 0, bpp_specified = 0;
1405 unsigned int xres = 0, yres = 0, bpp = 0;
1406 int yres_specified = 0;
1407 int i;
1408 for (i = namelen-1; i >= 0; i--) {
1409 switch (name[i]) {
1410 case '-':
1411 namelen = i;
1412 if (!bpp_specified && !yres_specified) {
1413 bpp = simple_strtoul(&name[i+1], NULL, 0);
1414 bpp_specified = 1;
1415 } else
1416 goto done;
1417 break;
1418 case 'x':
1419 if (!yres_specified) {
1420 yres = simple_strtoul(&name[i+1], NULL, 0);
1421 yres_specified = 1;
1422 } else
1423 goto done;
1424 break;
1425 case '0' ... '9':
1426 break;
1427 default:
1428 goto done;
1429 }
1430 }
1431 if (i < 0 && yres_specified) {
1432 xres = simple_strtoul(name, NULL, 0);
1433 res_specified = 1;
1434 }
1435done:
1436 if (res_specified) {
1437 dev_info(dev, "overriding resolution: %dx%d\n", xres, yres);
1438 inf->modes[0].xres = xres; inf->modes[0].yres = yres;
1439 }
1440 if (bpp_specified)
1441 switch (bpp) {
1442 case 1:
1443 case 2:
1444 case 4:
1445 case 8:
1446 case 16:
1447 inf->modes[0].bpp = bpp;
1448 dev_info(dev, "overriding bit depth: %d\n", bpp);
1449 break;
1450 default:
1451 dev_err(dev, "Depth %d is not valid\n", bpp);
1452 return -EINVAL;
1453 }
1454 return 0;
1455}
1456
Jaya Kumar9e6c2972008-06-22 04:27:27 +01001457static int __devinit parse_opt(struct device *dev, char *this_opt)
eric miao817daf12008-04-30 00:52:18 -07001458{
1459 struct pxafb_mach_info *inf = dev->platform_data;
1460 struct pxafb_mode_info *mode = &inf->modes[0];
1461 char s[64];
1462
1463 s[0] = '\0';
1464
Eric Miao77e19672008-12-16 11:54:34 +08001465 if (!strncmp(this_opt, "vmem:", 5)) {
1466 video_mem_size = memparse(this_opt + 5, NULL);
1467 } else if (!strncmp(this_opt, "mode:", 5)) {
eric miao817daf12008-04-30 00:52:18 -07001468 return parse_opt_mode(dev, this_opt);
1469 } else if (!strncmp(this_opt, "pixclock:", 9)) {
1470 mode->pixclock = simple_strtoul(this_opt+9, NULL, 0);
1471 sprintf(s, "pixclock: %ld\n", mode->pixclock);
1472 } else if (!strncmp(this_opt, "left:", 5)) {
1473 mode->left_margin = simple_strtoul(this_opt+5, NULL, 0);
1474 sprintf(s, "left: %u\n", mode->left_margin);
1475 } else if (!strncmp(this_opt, "right:", 6)) {
1476 mode->right_margin = simple_strtoul(this_opt+6, NULL, 0);
1477 sprintf(s, "right: %u\n", mode->right_margin);
1478 } else if (!strncmp(this_opt, "upper:", 6)) {
1479 mode->upper_margin = simple_strtoul(this_opt+6, NULL, 0);
1480 sprintf(s, "upper: %u\n", mode->upper_margin);
1481 } else if (!strncmp(this_opt, "lower:", 6)) {
1482 mode->lower_margin = simple_strtoul(this_opt+6, NULL, 0);
1483 sprintf(s, "lower: %u\n", mode->lower_margin);
1484 } else if (!strncmp(this_opt, "hsynclen:", 9)) {
1485 mode->hsync_len = simple_strtoul(this_opt+9, NULL, 0);
1486 sprintf(s, "hsynclen: %u\n", mode->hsync_len);
1487 } else if (!strncmp(this_opt, "vsynclen:", 9)) {
1488 mode->vsync_len = simple_strtoul(this_opt+9, NULL, 0);
1489 sprintf(s, "vsynclen: %u\n", mode->vsync_len);
1490 } else if (!strncmp(this_opt, "hsync:", 6)) {
1491 if (simple_strtoul(this_opt+6, NULL, 0) == 0) {
1492 sprintf(s, "hsync: Active Low\n");
1493 mode->sync &= ~FB_SYNC_HOR_HIGH_ACT;
1494 } else {
1495 sprintf(s, "hsync: Active High\n");
1496 mode->sync |= FB_SYNC_HOR_HIGH_ACT;
1497 }
1498 } else if (!strncmp(this_opt, "vsync:", 6)) {
1499 if (simple_strtoul(this_opt+6, NULL, 0) == 0) {
1500 sprintf(s, "vsync: Active Low\n");
1501 mode->sync &= ~FB_SYNC_VERT_HIGH_ACT;
1502 } else {
1503 sprintf(s, "vsync: Active High\n");
1504 mode->sync |= FB_SYNC_VERT_HIGH_ACT;
1505 }
1506 } else if (!strncmp(this_opt, "dpc:", 4)) {
1507 if (simple_strtoul(this_opt+4, NULL, 0) == 0) {
1508 sprintf(s, "double pixel clock: false\n");
1509 inf->lccr3 &= ~LCCR3_DPC;
1510 } else {
1511 sprintf(s, "double pixel clock: true\n");
1512 inf->lccr3 |= LCCR3_DPC;
1513 }
1514 } else if (!strncmp(this_opt, "outputen:", 9)) {
1515 if (simple_strtoul(this_opt+9, NULL, 0) == 0) {
1516 sprintf(s, "output enable: active low\n");
1517 inf->lccr3 = (inf->lccr3 & ~LCCR3_OEP) | LCCR3_OutEnL;
1518 } else {
1519 sprintf(s, "output enable: active high\n");
1520 inf->lccr3 = (inf->lccr3 & ~LCCR3_OEP) | LCCR3_OutEnH;
1521 }
1522 } else if (!strncmp(this_opt, "pixclockpol:", 12)) {
1523 if (simple_strtoul(this_opt+12, NULL, 0) == 0) {
1524 sprintf(s, "pixel clock polarity: falling edge\n");
1525 inf->lccr3 = (inf->lccr3 & ~LCCR3_PCP) | LCCR3_PixFlEdg;
1526 } else {
1527 sprintf(s, "pixel clock polarity: rising edge\n");
1528 inf->lccr3 = (inf->lccr3 & ~LCCR3_PCP) | LCCR3_PixRsEdg;
1529 }
1530 } else if (!strncmp(this_opt, "color", 5)) {
1531 inf->lccr0 = (inf->lccr0 & ~LCCR0_CMS) | LCCR0_Color;
1532 } else if (!strncmp(this_opt, "mono", 4)) {
1533 inf->lccr0 = (inf->lccr0 & ~LCCR0_CMS) | LCCR0_Mono;
1534 } else if (!strncmp(this_opt, "active", 6)) {
1535 inf->lccr0 = (inf->lccr0 & ~LCCR0_PAS) | LCCR0_Act;
1536 } else if (!strncmp(this_opt, "passive", 7)) {
1537 inf->lccr0 = (inf->lccr0 & ~LCCR0_PAS) | LCCR0_Pas;
1538 } else if (!strncmp(this_opt, "single", 6)) {
1539 inf->lccr0 = (inf->lccr0 & ~LCCR0_SDS) | LCCR0_Sngl;
1540 } else if (!strncmp(this_opt, "dual", 4)) {
1541 inf->lccr0 = (inf->lccr0 & ~LCCR0_SDS) | LCCR0_Dual;
1542 } else if (!strncmp(this_opt, "4pix", 4)) {
1543 inf->lccr0 = (inf->lccr0 & ~LCCR0_DPD) | LCCR0_4PixMono;
1544 } else if (!strncmp(this_opt, "8pix", 4)) {
1545 inf->lccr0 = (inf->lccr0 & ~LCCR0_DPD) | LCCR0_8PixMono;
1546 } else {
1547 dev_err(dev, "unknown option: %s\n", this_opt);
1548 return -EINVAL;
1549 }
1550
1551 if (s[0] != '\0')
1552 dev_info(dev, "override %s", s);
1553
1554 return 0;
1555}
1556
Jaya Kumar9e6c2972008-06-22 04:27:27 +01001557static int __devinit pxafb_parse_options(struct device *dev, char *options)
eric miao817daf12008-04-30 00:52:18 -07001558{
1559 char *this_opt;
1560 int ret;
1561
1562 if (!options || !*options)
1563 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
1565 dev_dbg(dev, "options are \"%s\"\n", options ? options : "null");
1566
1567 /* could be made table driven or similar?... */
eric miao817daf12008-04-30 00:52:18 -07001568 while ((this_opt = strsep(&options, ",")) != NULL) {
1569 ret = parse_opt(dev, this_opt);
1570 if (ret)
1571 return ret;
1572 }
1573 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574}
eric miao92ac73c2008-04-30 00:52:20 -07001575
1576static char g_options[256] __devinitdata = "";
1577
Jaya Kumarf1edfc42008-06-22 04:27:25 +01001578#ifndef MODULE
Jaya Kumar9e6c2972008-06-22 04:27:27 +01001579static int __init pxafb_setup_options(void)
eric miao92ac73c2008-04-30 00:52:20 -07001580{
1581 char *options = NULL;
1582
1583 if (fb_get_options("pxafb", &options))
1584 return -ENODEV;
1585
1586 if (options)
1587 strlcpy(g_options, options, sizeof(g_options));
1588
1589 return 0;
1590}
1591#else
1592#define pxafb_setup_options() (0)
1593
1594module_param_string(options, g_options, sizeof(g_options), 0);
1595MODULE_PARM_DESC(options, "LCD parameters (see Documentation/fb/pxafb.txt)");
1596#endif
1597
1598#else
1599#define pxafb_parse_options(...) (0)
1600#define pxafb_setup_options() (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601#endif
1602
Eric Miao4f3e2662008-08-16 03:50:51 -04001603#ifdef DEBUG_VAR
1604/* Check for various illegal bit-combinations. Currently only
1605 * a warning is given. */
1606static void __devinit pxafb_check_options(struct device *dev,
1607 struct pxafb_mach_info *inf)
1608{
1609 if (inf->lcd_conn)
1610 return;
1611
1612 if (inf->lccr0 & LCCR0_INVALID_CONFIG_MASK)
1613 dev_warn(dev, "machine LCCR0 setting contains "
1614 "illegal bits: %08x\n",
1615 inf->lccr0 & LCCR0_INVALID_CONFIG_MASK);
1616 if (inf->lccr3 & LCCR3_INVALID_CONFIG_MASK)
1617 dev_warn(dev, "machine LCCR3 setting contains "
1618 "illegal bits: %08x\n",
1619 inf->lccr3 & LCCR3_INVALID_CONFIG_MASK);
1620 if (inf->lccr0 & LCCR0_DPD &&
1621 ((inf->lccr0 & LCCR0_PAS) != LCCR0_Pas ||
1622 (inf->lccr0 & LCCR0_SDS) != LCCR0_Sngl ||
1623 (inf->lccr0 & LCCR0_CMS) != LCCR0_Mono))
1624 dev_warn(dev, "Double Pixel Data (DPD) mode is "
1625 "only valid in passive mono"
1626 " single panel mode\n");
1627 if ((inf->lccr0 & LCCR0_PAS) == LCCR0_Act &&
1628 (inf->lccr0 & LCCR0_SDS) == LCCR0_Dual)
1629 dev_warn(dev, "Dual panel only valid in passive mode\n");
1630 if ((inf->lccr0 & LCCR0_PAS) == LCCR0_Pas &&
1631 (inf->modes->upper_margin || inf->modes->lower_margin))
1632 dev_warn(dev, "Upper and lower margins must be 0 in "
1633 "passive mode\n");
1634}
1635#else
1636#define pxafb_check_options(...) do {} while (0)
1637#endif
1638
Jaya Kumar9e6c2972008-06-22 04:27:27 +01001639static int __devinit pxafb_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
1641 struct pxafb_info *fbi;
1642 struct pxafb_mach_info *inf;
eric miaoce4fb7b2008-04-30 00:52:21 -07001643 struct resource *r;
1644 int irq, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Richard Purdie2cbbb3b2006-03-31 02:31:53 -08001646 dev_dbg(&dev->dev, "pxafb_probe\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Russell King3ae5eae2005-11-09 22:32:44 +00001648 inf = dev->dev.platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 ret = -ENOMEM;
1650 fbi = NULL;
1651 if (!inf)
1652 goto failed;
1653
Russell King3ae5eae2005-11-09 22:32:44 +00001654 ret = pxafb_parse_options(&dev->dev, g_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 if (ret < 0)
1656 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Eric Miao4f3e2662008-08-16 03:50:51 -04001658 pxafb_check_options(&dev->dev, inf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
eric miaob0086ef2008-04-30 00:52:19 -07001660 dev_dbg(&dev->dev, "got a %dx%dx%d LCD\n",
1661 inf->modes->xres,
1662 inf->modes->yres,
1663 inf->modes->bpp);
1664 if (inf->modes->xres == 0 ||
1665 inf->modes->yres == 0 ||
1666 inf->modes->bpp == 0) {
Russell King3ae5eae2005-11-09 22:32:44 +00001667 dev_err(&dev->dev, "Invalid resolution or bit depth\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 ret = -EINVAL;
1669 goto failed;
1670 }
Eric Miaoa5718a12008-11-11 21:50:39 +08001671
Russell King3ae5eae2005-11-09 22:32:44 +00001672 fbi = pxafb_init_fbinfo(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 if (!fbi) {
eric miaob0086ef2008-04-30 00:52:19 -07001674 /* only reason for pxafb_init_fbinfo to fail is kmalloc */
Russell King3ae5eae2005-11-09 22:32:44 +00001675 dev_err(&dev->dev, "Failed to initialize framebuffer device\n");
eric miaob0086ef2008-04-30 00:52:19 -07001676 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 goto failed;
1678 }
1679
Eric Miaoa5718a12008-11-11 21:50:39 +08001680 fbi->backlight_power = inf->pxafb_backlight_power;
1681 fbi->lcd_power = inf->pxafb_lcd_power;
1682
eric miaoce4fb7b2008-04-30 00:52:21 -07001683 r = platform_get_resource(dev, IORESOURCE_MEM, 0);
1684 if (r == NULL) {
1685 dev_err(&dev->dev, "no I/O memory resource defined\n");
1686 ret = -ENODEV;
Jaya Kumaree984762008-06-22 04:27:26 +01001687 goto failed_fbi;
eric miaoce4fb7b2008-04-30 00:52:21 -07001688 }
1689
1690 r = request_mem_region(r->start, r->end - r->start + 1, dev->name);
1691 if (r == NULL) {
1692 dev_err(&dev->dev, "failed to request I/O memory\n");
1693 ret = -EBUSY;
Jaya Kumaree984762008-06-22 04:27:26 +01001694 goto failed_fbi;
eric miaoce4fb7b2008-04-30 00:52:21 -07001695 }
1696
1697 fbi->mmio_base = ioremap(r->start, r->end - r->start + 1);
1698 if (fbi->mmio_base == NULL) {
1699 dev_err(&dev->dev, "failed to map I/O memory\n");
1700 ret = -EBUSY;
1701 goto failed_free_res;
1702 }
1703
Eric Miao77e19672008-12-16 11:54:34 +08001704 fbi->dma_buff_size = PAGE_ALIGN(sizeof(struct pxafb_dma_buff));
1705 fbi->dma_buff = dma_alloc_coherent(fbi->dev, fbi->dma_buff_size,
1706 &fbi->dma_buff_phys, GFP_KERNEL);
1707 if (fbi->dma_buff == NULL) {
1708 dev_err(&dev->dev, "failed to allocate memory for DMA\n");
1709 ret = -ENOMEM;
1710 goto failed_free_io;
1711 }
1712
1713 ret = pxafb_init_video_memory(fbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 if (ret) {
Russell King3ae5eae2005-11-09 22:32:44 +00001715 dev_err(&dev->dev, "Failed to allocate video RAM: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 ret = -ENOMEM;
Eric Miao77e19672008-12-16 11:54:34 +08001717 goto failed_free_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
eric miaoce4fb7b2008-04-30 00:52:21 -07001720 irq = platform_get_irq(dev, 0);
1721 if (irq < 0) {
1722 dev_err(&dev->dev, "no IRQ defined\n");
1723 ret = -ENODEV;
1724 goto failed_free_mem;
1725 }
1726
1727 ret = request_irq(irq, pxafb_handle_irq, IRQF_DISABLED, "LCD", fbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 if (ret) {
Russell King3ae5eae2005-11-09 22:32:44 +00001729 dev_err(&dev->dev, "request_irq failed: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 ret = -EBUSY;
eric miaoce4fb7b2008-04-30 00:52:21 -07001731 goto failed_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 }
1733
Eric Miao3c42a442008-04-30 00:52:26 -07001734 ret = pxafb_smart_init(fbi);
1735 if (ret) {
1736 dev_err(&dev->dev, "failed to initialize smartpanel\n");
1737 goto failed_free_irq;
1738 }
Eric Miao07df1c42008-12-04 11:14:11 +08001739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 /*
1741 * This makes sure that our colour bitfield
1742 * descriptors are correctly initialised.
1743 */
Jaya Kumaree984762008-06-22 04:27:26 +01001744 ret = pxafb_check_var(&fbi->fb.var, &fbi->fb);
1745 if (ret) {
1746 dev_err(&dev->dev, "failed to get suitable mode\n");
1747 goto failed_free_irq;
1748 }
1749
1750 ret = pxafb_set_par(&fbi->fb);
1751 if (ret) {
1752 dev_err(&dev->dev, "Failed to set parameters\n");
1753 goto failed_free_irq;
1754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Russell King3ae5eae2005-11-09 22:32:44 +00001756 platform_set_drvdata(dev, fbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
1758 ret = register_framebuffer(&fbi->fb);
1759 if (ret < 0) {
eric miaob0086ef2008-04-30 00:52:19 -07001760 dev_err(&dev->dev,
1761 "Failed to register framebuffer device: %d\n", ret);
Jaya Kumaree984762008-06-22 04:27:26 +01001762 goto failed_free_cmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 }
1764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765#ifdef CONFIG_CPU_FREQ
1766 fbi->freq_transition.notifier_call = pxafb_freq_transition;
1767 fbi->freq_policy.notifier_call = pxafb_freq_policy;
eric miaob0086ef2008-04-30 00:52:19 -07001768 cpufreq_register_notifier(&fbi->freq_transition,
1769 CPUFREQ_TRANSITION_NOTIFIER);
1770 cpufreq_register_notifier(&fbi->freq_policy,
1771 CPUFREQ_POLICY_NOTIFIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772#endif
1773
1774 /*
1775 * Ok, now enable the LCD controller
1776 */
1777 set_ctrlr_state(fbi, C_ENABLE);
1778
1779 return 0;
1780
Jaya Kumaree984762008-06-22 04:27:26 +01001781failed_free_cmap:
1782 if (fbi->fb.cmap.len)
1783 fb_dealloc_cmap(&fbi->fb.cmap);
eric miaoce4fb7b2008-04-30 00:52:21 -07001784failed_free_irq:
1785 free_irq(irq, fbi);
eric miaoce4fb7b2008-04-30 00:52:21 -07001786failed_free_mem:
Eric Miao77e19672008-12-16 11:54:34 +08001787 free_pages_exact(fbi->video_mem, fbi->video_mem_size);
1788failed_free_dma:
1789 dma_free_coherent(&dev->dev, fbi->dma_buff_size,
1790 fbi->dma_buff, fbi->dma_buff_phys);
Jaya Kumaree984762008-06-22 04:27:26 +01001791failed_free_io:
1792 iounmap(fbi->mmio_base);
1793failed_free_res:
1794 release_mem_region(r->start, r->end - r->start + 1);
1795failed_fbi:
1796 clk_put(fbi->clk);
Russell King3ae5eae2005-11-09 22:32:44 +00001797 platform_set_drvdata(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 kfree(fbi);
Jaya Kumaree984762008-06-22 04:27:26 +01001799failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 return ret;
1801}
1802
Jaya Kumar9f17f282008-06-22 04:27:28 +01001803static int __devexit pxafb_remove(struct platform_device *dev)
1804{
1805 struct pxafb_info *fbi = platform_get_drvdata(dev);
1806 struct resource *r;
1807 int irq;
1808 struct fb_info *info;
1809
1810 if (!fbi)
1811 return 0;
1812
1813 info = &fbi->fb;
1814
1815 unregister_framebuffer(info);
1816
1817 pxafb_disable_controller(fbi);
1818
1819 if (fbi->fb.cmap.len)
1820 fb_dealloc_cmap(&fbi->fb.cmap);
1821
1822 irq = platform_get_irq(dev, 0);
1823 free_irq(irq, fbi);
1824
Eric Miao77e19672008-12-16 11:54:34 +08001825 free_pages_exact(fbi->video_mem, fbi->video_mem_size);
1826
1827 dma_free_writecombine(&dev->dev, fbi->dma_buff_size,
1828 fbi->dma_buff, fbi->dma_buff_phys);
Jaya Kumar9f17f282008-06-22 04:27:28 +01001829
1830 iounmap(fbi->mmio_base);
1831
1832 r = platform_get_resource(dev, IORESOURCE_MEM, 0);
1833 release_mem_region(r->start, r->end - r->start + 1);
1834
1835 clk_put(fbi->clk);
1836 kfree(fbi);
1837
1838 return 0;
1839}
1840
Russell King3ae5eae2005-11-09 22:32:44 +00001841static struct platform_driver pxafb_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 .probe = pxafb_probe,
Jaya Kumar9f17f282008-06-22 04:27:28 +01001843 .remove = pxafb_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 .suspend = pxafb_suspend,
1845 .resume = pxafb_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001846 .driver = {
Jaya Kumar9f17f282008-06-22 04:27:28 +01001847 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00001848 .name = "pxa2xx-fb",
1849 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850};
1851
Jaya Kumar9e6c2972008-06-22 04:27:27 +01001852static int __init pxafb_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853{
eric miao92ac73c2008-04-30 00:52:20 -07001854 if (pxafb_setup_options())
1855 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
Russell King3ae5eae2005-11-09 22:32:44 +00001857 return platform_driver_register(&pxafb_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858}
1859
Jaya Kumar9f17f282008-06-22 04:27:28 +01001860static void __exit pxafb_exit(void)
1861{
1862 platform_driver_unregister(&pxafb_driver);
1863}
1864
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865module_init(pxafb_init);
Jaya Kumar9f17f282008-06-22 04:27:28 +01001866module_exit(pxafb_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
1868MODULE_DESCRIPTION("loadable framebuffer driver for PXA");
1869MODULE_LICENSE("GPL");