blob: 33a6aacfcbe3a6b036669bb4b1b4154b981a7fea [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 *
Eric Miao198fc102008-12-23 17:49:43 +080023 * Add support for overlay1 and overlay2 based on pxafb_overlay.c:
24 *
25 * Copyright (C) 2004, Intel Corporation
26 *
27 * 2003/08/27: <yu.tang@intel.com>
28 * 2004/03/10: <stanley.cai@intel.com>
29 * 2004/10/28: <yan.yin@intel.com>
30 *
31 * Copyright (C) 2006-2008 Marvell International Ltd.
32 * All Rights Reserved
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/module.h>
36#include <linux/moduleparam.h>
37#include <linux/kernel.h>
38#include <linux/sched.h>
39#include <linux/errno.h>
40#include <linux/string.h>
41#include <linux/interrupt.h>
42#include <linux/slab.h>
Andrea Righi27ac7922008-07-23 21:28:13 -070043#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/fb.h>
45#include <linux/delay.h>
46#include <linux/init.h>
47#include <linux/ioport.h>
48#include <linux/cpufreq.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010049#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/dma-mapping.h>
Russell King72e35242007-08-20 10:18:42 +010051#include <linux/clk.h>
52#include <linux/err.h>
Eric Miao2ba162b2008-04-30 00:52:24 -070053#include <linux/completion.h>
Matthias Kaehlckeb91dbce2008-07-23 21:31:14 -070054#include <linux/mutex.h>
Eric Miao3c42a442008-04-30 00:52:26 -070055#include <linux/kthread.h>
56#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Russell Kinga09e64f2008-08-05 16:14:15 +010058#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <asm/io.h>
60#include <asm/irq.h>
Nicolas Pitrebf1b8ab2005-06-23 21:56:45 +010061#include <asm/div64.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010062#include <mach/bitfield.h>
63#include <mach/pxafb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65/*
66 * Complain if VAR is out of range.
67 */
68#define DEBUG_VAR 1
69
70#include "pxafb.h"
71
72/* Bits which should not be set in machine configuration structures */
eric miaob0086ef2008-04-30 00:52:19 -070073#define LCCR0_INVALID_CONFIG_MASK (LCCR0_OUM | LCCR0_BM | LCCR0_QDM |\
74 LCCR0_DIS | LCCR0_EFM | LCCR0_IUM |\
75 LCCR0_SFM | LCCR0_LDM | LCCR0_ENB)
76
77#define LCCR3_INVALID_CONFIG_MASK (LCCR3_HSP | LCCR3_VSP |\
Eric Miao878f5782008-12-18 22:36:26 +080078 LCCR3_PCD | LCCR3_BPP(0xf))
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
eric miaob0086ef2008-04-30 00:52:19 -070080static int pxafb_activate_var(struct fb_var_screeninfo *var,
81 struct pxafb_info *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082static void set_ctrlr_state(struct pxafb_info *fbi, u_int state);
Sven Neumann448ac472009-10-22 08:34:34 +020083static void setup_base_frame(struct pxafb_info *fbi,
84 struct fb_var_screeninfo *var, int branch);
Eric Miao198fc102008-12-23 17:49:43 +080085static int setup_frame_dma(struct pxafb_info *fbi, int dma, int pal,
86 unsigned long offset, size_t size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Eric Miao77e19672008-12-16 11:54:34 +080088static unsigned long video_mem_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Eric Miaoa7535ba2008-04-30 00:52:24 -070090static inline unsigned long
91lcd_readl(struct pxafb_info *fbi, unsigned int off)
92{
93 return __raw_readl(fbi->mmio_base + off);
94}
95
96static inline void
97lcd_writel(struct pxafb_info *fbi, unsigned int off, unsigned long val)
98{
99 __raw_writel(val, fbi->mmio_base + off);
100}
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102static inline void pxafb_schedule_work(struct pxafb_info *fbi, u_int state)
103{
104 unsigned long flags;
105
106 local_irq_save(flags);
107 /*
108 * We need to handle two requests being made at the same time.
109 * There are two important cases:
eric miaob0086ef2008-04-30 00:52:19 -0700110 * 1. When we are changing VT (C_REENABLE) while unblanking
111 * (C_ENABLE) We must perform the unblanking, which will
112 * do our REENABLE for us.
113 * 2. When we are blanking, but immediately unblank before
114 * we have blanked. We do the "REENABLE" thing here as
115 * well, just to be sure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 */
117 if (fbi->task_state == C_ENABLE && state == C_REENABLE)
118 state = (u_int) -1;
119 if (fbi->task_state == C_DISABLE && state == C_ENABLE)
120 state = C_REENABLE;
121
122 if (state != (u_int)-1) {
123 fbi->task_state = state;
124 schedule_work(&fbi->task);
125 }
126 local_irq_restore(flags);
127}
128
129static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
130{
131 chan &= 0xffff;
132 chan >>= 16 - bf->length;
133 return chan << bf->offset;
134}
135
136static int
137pxafb_setpalettereg(u_int regno, u_int red, u_int green, u_int blue,
138 u_int trans, struct fb_info *info)
139{
140 struct pxafb_info *fbi = (struct pxafb_info *)info;
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700141 u_int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700143 if (regno >= fbi->palette_size)
144 return 1;
145
146 if (fbi->fb.var.grayscale) {
147 fbi->palette_cpu[regno] = ((blue >> 8) & 0x00ff);
148 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700150
151 switch (fbi->lccr4 & LCCR4_PAL_FOR_MASK) {
152 case LCCR4_PAL_FOR_0:
153 val = ((red >> 0) & 0xf800);
154 val |= ((green >> 5) & 0x07e0);
155 val |= ((blue >> 11) & 0x001f);
156 fbi->palette_cpu[regno] = val;
157 break;
158 case LCCR4_PAL_FOR_1:
159 val = ((red << 8) & 0x00f80000);
160 val |= ((green >> 0) & 0x0000fc00);
161 val |= ((blue >> 8) & 0x000000f8);
eric miaob0086ef2008-04-30 00:52:19 -0700162 ((u32 *)(fbi->palette_cpu))[regno] = val;
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700163 break;
164 case LCCR4_PAL_FOR_2:
165 val = ((red << 8) & 0x00fc0000);
166 val |= ((green >> 0) & 0x0000fc00);
167 val |= ((blue >> 8) & 0x000000fc);
eric miaob0086ef2008-04-30 00:52:19 -0700168 ((u32 *)(fbi->palette_cpu))[regno] = val;
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700169 break;
Eric Miaoa0427502008-12-18 22:10:00 +0800170 case LCCR4_PAL_FOR_3:
171 val = ((red << 8) & 0x00ff0000);
172 val |= ((green >> 0) & 0x0000ff00);
173 val |= ((blue >> 8) & 0x000000ff);
174 ((u32 *)(fbi->palette_cpu))[regno] = val;
175 break;
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700176 }
177
178 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
181static int
182pxafb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
183 u_int trans, struct fb_info *info)
184{
185 struct pxafb_info *fbi = (struct pxafb_info *)info;
186 unsigned int val;
187 int ret = 1;
188
189 /*
190 * If inverse mode was selected, invert all the colours
191 * rather than the register number. The register number
192 * is what you poke into the framebuffer to produce the
193 * colour you requested.
194 */
195 if (fbi->cmap_inverse) {
196 red = 0xffff - red;
197 green = 0xffff - green;
198 blue = 0xffff - blue;
199 }
200
201 /*
202 * If greyscale is true, then we convert the RGB value
203 * to greyscale no matter what visual we are using.
204 */
205 if (fbi->fb.var.grayscale)
206 red = green = blue = (19595 * red + 38470 * green +
207 7471 * blue) >> 16;
208
209 switch (fbi->fb.fix.visual) {
210 case FB_VISUAL_TRUECOLOR:
211 /*
212 * 16-bit True Colour. We encode the RGB value
213 * according to the RGB bitfield information.
214 */
215 if (regno < 16) {
216 u32 *pal = fbi->fb.pseudo_palette;
217
218 val = chan_to_field(red, &fbi->fb.var.red);
219 val |= chan_to_field(green, &fbi->fb.var.green);
220 val |= chan_to_field(blue, &fbi->fb.var.blue);
221
222 pal[regno] = val;
223 ret = 0;
224 }
225 break;
226
227 case FB_VISUAL_STATIC_PSEUDOCOLOR:
228 case FB_VISUAL_PSEUDOCOLOR:
229 ret = pxafb_setpalettereg(regno, red, green, blue, trans, info);
230 break;
231 }
232
233 return ret;
234}
235
Eric Miao878f5782008-12-18 22:36:26 +0800236/* calculate pixel depth, transparency bit included, >=16bpp formats _only_ */
237static inline int var_to_depth(struct fb_var_screeninfo *var)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Eric Miao878f5782008-12-18 22:36:26 +0800239 return var->red.length + var->green.length +
240 var->blue.length + var->transp.length;
241}
242
243/* calculate 4-bit BPP value for LCCR3 and OVLxC1 */
244static int pxafb_var_to_bpp(struct fb_var_screeninfo *var)
245{
246 int bpp = -EINVAL;
247
eric miaob0086ef2008-04-30 00:52:19 -0700248 switch (var->bits_per_pixel) {
Eric Miao878f5782008-12-18 22:36:26 +0800249 case 1: bpp = 0; break;
250 case 2: bpp = 1; break;
251 case 4: bpp = 2; break;
252 case 8: bpp = 3; break;
253 case 16: bpp = 4; break;
Stefan Schmidtc1450f12008-07-09 08:06:32 +0100254 case 24:
Eric Miao878f5782008-12-18 22:36:26 +0800255 switch (var_to_depth(var)) {
256 case 18: bpp = 6; break; /* 18-bits/pixel packed */
257 case 19: bpp = 8; break; /* 19-bits/pixel packed */
258 case 24: bpp = 9; break;
Stefan Schmidtc1450f12008-07-09 08:06:32 +0100259 }
260 break;
261 case 32:
Eric Miao878f5782008-12-18 22:36:26 +0800262 switch (var_to_depth(var)) {
263 case 18: bpp = 5; break; /* 18-bits/pixel unpacked */
264 case 19: bpp = 7; break; /* 19-bits/pixel unpacked */
265 case 25: bpp = 10; break;
Stefan Schmidtc1450f12008-07-09 08:06:32 +0100266 }
267 break;
eric miaob0086ef2008-04-30 00:52:19 -0700268 }
Eric Miao878f5782008-12-18 22:36:26 +0800269 return bpp;
270}
271
272/*
273 * pxafb_var_to_lccr3():
274 * Convert a bits per pixel value to the correct bit pattern for LCCR3
275 *
276 * NOTE: for PXA27x with overlays support, the LCCR3_PDFOR_x bits have an
277 * implication of the acutal use of transparency bit, which we handle it
278 * here separatedly. See PXA27x Developer's Manual, Section <<7.4.6 Pixel
279 * Formats>> for the valid combination of PDFOR, PAL_FOR for various BPP.
280 *
281 * Transparency for palette pixel formats is not supported at the moment.
282 */
283static uint32_t pxafb_var_to_lccr3(struct fb_var_screeninfo *var)
284{
285 int bpp = pxafb_var_to_bpp(var);
286 uint32_t lccr3;
287
288 if (bpp < 0)
289 return 0;
290
291 lccr3 = LCCR3_BPP(bpp);
292
293 switch (var_to_depth(var)) {
294 case 16: lccr3 |= var->transp.length ? LCCR3_PDFOR_3 : 0; break;
295 case 18: lccr3 |= LCCR3_PDFOR_3; break;
296 case 24: lccr3 |= var->transp.length ? LCCR3_PDFOR_2 : LCCR3_PDFOR_3;
297 break;
298 case 19:
299 case 25: lccr3 |= LCCR3_PDFOR_0; break;
300 }
301 return lccr3;
302}
303
304#define SET_PIXFMT(v, r, g, b, t) \
305({ \
306 (v)->transp.offset = (t) ? (r) + (g) + (b) : 0; \
307 (v)->transp.length = (t) ? (t) : 0; \
308 (v)->blue.length = (b); (v)->blue.offset = 0; \
309 (v)->green.length = (g); (v)->green.offset = (b); \
310 (v)->red.length = (r); (v)->red.offset = (b) + (g); \
311})
312
313/* set the RGBT bitfields of fb_var_screeninf according to
314 * var->bits_per_pixel and given depth
315 */
316static void pxafb_set_pixfmt(struct fb_var_screeninfo *var, int depth)
317{
318 if (depth == 0)
319 depth = var->bits_per_pixel;
320
321 if (var->bits_per_pixel < 16) {
322 /* indexed pixel formats */
323 var->red.offset = 0; var->red.length = 8;
324 var->green.offset = 0; var->green.length = 8;
325 var->blue.offset = 0; var->blue.length = 8;
326 var->transp.offset = 0; var->transp.length = 8;
327 }
328
329 switch (depth) {
330 case 16: var->transp.length ?
331 SET_PIXFMT(var, 5, 5, 5, 1) : /* RGBT555 */
332 SET_PIXFMT(var, 5, 6, 5, 0); break; /* RGB565 */
333 case 18: SET_PIXFMT(var, 6, 6, 6, 0); break; /* RGB666 */
334 case 19: SET_PIXFMT(var, 6, 6, 6, 1); break; /* RGBT666 */
335 case 24: var->transp.length ?
336 SET_PIXFMT(var, 8, 8, 7, 1) : /* RGBT887 */
337 SET_PIXFMT(var, 8, 8, 8, 0); break; /* RGB888 */
338 case 25: SET_PIXFMT(var, 8, 8, 8, 1); break; /* RGBT888 */
339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
342#ifdef CONFIG_CPU_FREQ
343/*
344 * pxafb_display_dma_period()
345 * Calculate the minimum period (in picoseconds) between two DMA
346 * requests for the LCD controller. If we hit this, it means we're
347 * doing nothing but LCD DMA.
348 */
349static unsigned int pxafb_display_dma_period(struct fb_var_screeninfo *var)
350{
eric miaob0086ef2008-04-30 00:52:19 -0700351 /*
352 * Period = pixclock * bits_per_byte * bytes_per_transfer
353 * / memory_bits_per_pixel;
354 */
355 return var->pixclock * 8 * 16 / var->bits_per_pixel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357#endif
358
359/*
Richard Purdied14b2722006-09-20 22:54:21 +0100360 * Select the smallest mode that allows the desired resolution to be
361 * displayed. If desired parameters can be rounded up.
362 */
eric miaob0086ef2008-04-30 00:52:19 -0700363static struct pxafb_mode_info *pxafb_getmode(struct pxafb_mach_info *mach,
364 struct fb_var_screeninfo *var)
Richard Purdied14b2722006-09-20 22:54:21 +0100365{
366 struct pxafb_mode_info *mode = NULL;
367 struct pxafb_mode_info *modelist = mach->modes;
368 unsigned int best_x = 0xffffffff, best_y = 0xffffffff;
369 unsigned int i;
370
eric miaob0086ef2008-04-30 00:52:19 -0700371 for (i = 0; i < mach->num_modes; i++) {
372 if (modelist[i].xres >= var->xres &&
373 modelist[i].yres >= var->yres &&
374 modelist[i].xres < best_x &&
375 modelist[i].yres < best_y &&
376 modelist[i].bpp >= var->bits_per_pixel) {
Richard Purdied14b2722006-09-20 22:54:21 +0100377 best_x = modelist[i].xres;
378 best_y = modelist[i].yres;
379 mode = &modelist[i];
380 }
381 }
382
383 return mode;
384}
385
eric miaob0086ef2008-04-30 00:52:19 -0700386static void pxafb_setmode(struct fb_var_screeninfo *var,
387 struct pxafb_mode_info *mode)
Richard Purdied14b2722006-09-20 22:54:21 +0100388{
389 var->xres = mode->xres;
390 var->yres = mode->yres;
391 var->bits_per_pixel = mode->bpp;
392 var->pixclock = mode->pixclock;
393 var->hsync_len = mode->hsync_len;
394 var->left_margin = mode->left_margin;
395 var->right_margin = mode->right_margin;
396 var->vsync_len = mode->vsync_len;
397 var->upper_margin = mode->upper_margin;
398 var->lower_margin = mode->lower_margin;
399 var->sync = mode->sync;
400 var->grayscale = mode->cmap_greyscale;
Eric Miao878f5782008-12-18 22:36:26 +0800401
402 /* set the initial RGBA bitfields */
403 pxafb_set_pixfmt(var, mode->depth);
Richard Purdied14b2722006-09-20 22:54:21 +0100404}
405
Eric Miao3f16ff62008-12-18 22:51:54 +0800406static int pxafb_adjust_timing(struct pxafb_info *fbi,
407 struct fb_var_screeninfo *var)
408{
409 int line_length;
410
411 var->xres = max_t(int, var->xres, MIN_XRES);
412 var->yres = max_t(int, var->yres, MIN_YRES);
413
414 if (!(fbi->lccr0 & LCCR0_LCDT)) {
415 clamp_val(var->hsync_len, 1, 64);
416 clamp_val(var->vsync_len, 1, 64);
417 clamp_val(var->left_margin, 1, 255);
418 clamp_val(var->right_margin, 1, 255);
419 clamp_val(var->upper_margin, 1, 255);
420 clamp_val(var->lower_margin, 1, 255);
421 }
422
423 /* make sure each line is aligned on word boundary */
424 line_length = var->xres * var->bits_per_pixel / 8;
425 line_length = ALIGN(line_length, 4);
426 var->xres = line_length * 8 / var->bits_per_pixel;
427
428 /* we don't support xpan, force xres_virtual to be equal to xres */
429 var->xres_virtual = var->xres;
430
431 if (var->accel_flags & FB_ACCELF_TEXT)
432 var->yres_virtual = fbi->fb.fix.smem_len / line_length;
433 else
434 var->yres_virtual = max(var->yres_virtual, var->yres);
435
436 /* check for limits */
437 if (var->xres > MAX_XRES || var->yres > MAX_YRES)
438 return -EINVAL;
439
440 if (var->yres > var->yres_virtual)
441 return -EINVAL;
442
443 return 0;
Richard Purdied14b2722006-09-20 22:54:21 +0100444}
445
446/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 * pxafb_check_var():
448 * Get the video params out of 'var'. If a value doesn't fit, round it up,
449 * if it's too big, return -EINVAL.
450 *
451 * Round up in the following order: bits_per_pixel, xres,
452 * yres, xres_virtual, yres_virtual, xoffset, yoffset, grayscale,
453 * bitfields, horizontal timing, vertical timing.
454 */
455static int pxafb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
456{
457 struct pxafb_info *fbi = (struct pxafb_info *)info;
Richard Purdied14b2722006-09-20 22:54:21 +0100458 struct pxafb_mach_info *inf = fbi->dev->platform_data;
Eric Miao878f5782008-12-18 22:36:26 +0800459 int err;
Richard Purdied14b2722006-09-20 22:54:21 +0100460
461 if (inf->fixed_modes) {
462 struct pxafb_mode_info *mode;
463
464 mode = pxafb_getmode(inf, var);
465 if (!mode)
466 return -EINVAL;
467 pxafb_setmode(var, mode);
Richard Purdied14b2722006-09-20 22:54:21 +0100468 }
469
Eric Miao878f5782008-12-18 22:36:26 +0800470 /* do a test conversion to BPP fields to check the color formats */
471 err = pxafb_var_to_bpp(var);
472 if (err < 0)
473 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Eric Miao878f5782008-12-18 22:36:26 +0800475 pxafb_set_pixfmt(var, var_to_depth(var));
Stefan Schmidtc1450f12008-07-09 08:06:32 +0100476
Eric Miao3f16ff62008-12-18 22:51:54 +0800477 err = pxafb_adjust_timing(fbi, var);
478 if (err)
479 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481#ifdef CONFIG_CPU_FREQ
Russell King78d3cfd2008-05-17 22:51:14 +0100482 pr_debug("pxafb: dma period = %d ps\n",
483 pxafb_display_dma_period(var));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484#endif
485
486 return 0;
487}
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489/*
490 * pxafb_set_par():
491 * Set the user defined part of the display for the specified console
492 */
493static int pxafb_set_par(struct fb_info *info)
494{
495 struct pxafb_info *fbi = (struct pxafb_info *)info;
496 struct fb_var_screeninfo *var = &info->var;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Stefan Schmidtc1450f12008-07-09 08:06:32 +0100498 if (var->bits_per_pixel >= 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 fbi->fb.fix.visual = FB_VISUAL_TRUECOLOR;
500 else if (!fbi->cmap_static)
501 fbi->fb.fix.visual = FB_VISUAL_PSEUDOCOLOR;
502 else {
503 /*
504 * Some people have weird ideas about wanting static
505 * pseudocolor maps. I suspect their user space
506 * applications are broken.
507 */
508 fbi->fb.fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
509 }
510
511 fbi->fb.fix.line_length = var->xres_virtual *
512 var->bits_per_pixel / 8;
Stefan Schmidtc1450f12008-07-09 08:06:32 +0100513 if (var->bits_per_pixel >= 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 fbi->palette_size = 0;
515 else
eric miaob0086ef2008-04-30 00:52:19 -0700516 fbi->palette_size = var->bits_per_pixel == 1 ?
517 4 : 1 << var->bits_per_pixel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
eric miao2c42dd82008-04-30 00:52:21 -0700519 fbi->palette_cpu = (u16 *)&fbi->dma_buff->palette[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Stefan Schmidtc1450f12008-07-09 08:06:32 +0100521 if (fbi->fb.var.bits_per_pixel >= 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 fb_dealloc_cmap(&fbi->fb.cmap);
523 else
524 fb_alloc_cmap(&fbi->fb.cmap, 1<<fbi->fb.var.bits_per_pixel, 0);
525
526 pxafb_activate_var(var, fbi);
527
528 return 0;
529}
530
Eric Miao6e354842008-12-17 16:50:43 +0800531static int pxafb_pan_display(struct fb_var_screeninfo *var,
532 struct fb_info *info)
533{
534 struct pxafb_info *fbi = (struct pxafb_info *)info;
Sven Neumann448ac472009-10-22 08:34:34 +0200535 struct fb_var_screeninfo newvar;
Eric Miao6e354842008-12-17 16:50:43 +0800536 int dma = DMA_MAX + DMA_BASE;
537
538 if (fbi->state != C_ENABLE)
539 return 0;
540
Sven Neumann448ac472009-10-22 08:34:34 +0200541 /* Only take .xoffset, .yoffset and .vmode & FB_VMODE_YWRAP from what
542 * was passed in and copy the rest from the old screeninfo.
543 */
544 memcpy(&newvar, &fbi->fb.var, sizeof(newvar));
545 newvar.xoffset = var->xoffset;
546 newvar.yoffset = var->yoffset;
547 newvar.vmode &= ~FB_VMODE_YWRAP;
548 newvar.vmode |= var->vmode & FB_VMODE_YWRAP;
549
550 setup_base_frame(fbi, &newvar, 1);
Eric Miao6e354842008-12-17 16:50:43 +0800551
552 if (fbi->lccr0 & LCCR0_SDS)
553 lcd_writel(fbi, FBR1, fbi->fdadr[dma + 1] | 0x1);
554
555 lcd_writel(fbi, FBR0, fbi->fdadr[dma] | 0x1);
556 return 0;
557}
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 * pxafb_blank():
561 * Blank the display by setting all palette values to zero. Note, the
562 * 16 bpp mode does not really use the palette, so this will not
563 * blank the display in all modes.
564 */
565static int pxafb_blank(int blank, struct fb_info *info)
566{
567 struct pxafb_info *fbi = (struct pxafb_info *)info;
568 int i;
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 switch (blank) {
571 case FB_BLANK_POWERDOWN:
572 case FB_BLANK_VSYNC_SUSPEND:
573 case FB_BLANK_HSYNC_SUSPEND:
574 case FB_BLANK_NORMAL:
575 if (fbi->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR ||
576 fbi->fb.fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
577 for (i = 0; i < fbi->palette_size; i++)
578 pxafb_setpalettereg(i, 0, 0, 0, 0, info);
579
580 pxafb_schedule_work(fbi, C_DISABLE);
eric miaob0086ef2008-04-30 00:52:19 -0700581 /* TODO if (pxafb_blank_helper) pxafb_blank_helper(blank); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 break;
583
584 case FB_BLANK_UNBLANK:
eric miaob0086ef2008-04-30 00:52:19 -0700585 /* TODO if (pxafb_blank_helper) pxafb_blank_helper(blank); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (fbi->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR ||
587 fbi->fb.fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
588 fb_set_cmap(&fbi->fb.cmap, info);
589 pxafb_schedule_work(fbi, C_ENABLE);
590 }
591 return 0;
592}
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594static struct fb_ops pxafb_ops = {
595 .owner = THIS_MODULE,
596 .fb_check_var = pxafb_check_var,
597 .fb_set_par = pxafb_set_par,
Eric Miao6e354842008-12-17 16:50:43 +0800598 .fb_pan_display = pxafb_pan_display,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 .fb_setcolreg = pxafb_setcolreg,
600 .fb_fillrect = cfb_fillrect,
601 .fb_copyarea = cfb_copyarea,
602 .fb_imageblit = cfb_imageblit,
603 .fb_blank = pxafb_blank,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604};
605
Eric Miao198fc102008-12-23 17:49:43 +0800606#ifdef CONFIG_FB_PXA_OVERLAY
607static void overlay1fb_setup(struct pxafb_layer *ofb)
608{
609 int size = ofb->fb.fix.line_length * ofb->fb.var.yres_virtual;
610 unsigned long start = ofb->video_mem_phys;
611 setup_frame_dma(ofb->fbi, DMA_OV1, PAL_NONE, start, size);
612}
613
614/* Depending on the enable status of overlay1/2, the DMA should be
615 * updated from FDADRx (when disabled) or FBRx (when enabled).
616 */
617static void overlay1fb_enable(struct pxafb_layer *ofb)
618{
619 int enabled = lcd_readl(ofb->fbi, OVL1C1) & OVLxC1_OEN;
620 uint32_t fdadr1 = ofb->fbi->fdadr[DMA_OV1] | (enabled ? 0x1 : 0);
621
622 lcd_writel(ofb->fbi, enabled ? FBR1 : FDADR1, fdadr1);
623 lcd_writel(ofb->fbi, OVL1C2, ofb->control[1]);
624 lcd_writel(ofb->fbi, OVL1C1, ofb->control[0] | OVLxC1_OEN);
625}
626
627static void overlay1fb_disable(struct pxafb_layer *ofb)
628{
629 uint32_t lccr5 = lcd_readl(ofb->fbi, LCCR5);
630
631 lcd_writel(ofb->fbi, OVL1C1, ofb->control[0] & ~OVLxC1_OEN);
632
633 lcd_writel(ofb->fbi, LCSR1, LCSR1_BS(1));
634 lcd_writel(ofb->fbi, LCCR5, lccr5 & ~LCSR1_BS(1));
635 lcd_writel(ofb->fbi, FBR1, ofb->fbi->fdadr[DMA_OV1] | 0x3);
636
637 if (wait_for_completion_timeout(&ofb->branch_done, 1 * HZ) == 0)
638 pr_warning("%s: timeout disabling overlay1\n", __func__);
639
640 lcd_writel(ofb->fbi, LCCR5, lccr5);
641}
642
643static void overlay2fb_setup(struct pxafb_layer *ofb)
644{
645 int size, div = 1, pfor = NONSTD_TO_PFOR(ofb->fb.var.nonstd);
646 unsigned long start[3] = { ofb->video_mem_phys, 0, 0 };
647
648 if (pfor == OVERLAY_FORMAT_RGB || pfor == OVERLAY_FORMAT_YUV444_PACKED) {
649 size = ofb->fb.fix.line_length * ofb->fb.var.yres_virtual;
650 setup_frame_dma(ofb->fbi, DMA_OV2_Y, -1, start[0], size);
651 } else {
652 size = ofb->fb.var.xres_virtual * ofb->fb.var.yres_virtual;
653 switch (pfor) {
654 case OVERLAY_FORMAT_YUV444_PLANAR: div = 1; break;
655 case OVERLAY_FORMAT_YUV422_PLANAR: div = 2; break;
656 case OVERLAY_FORMAT_YUV420_PLANAR: div = 4; break;
657 }
658 start[1] = start[0] + size;
659 start[2] = start[1] + size / div;
660 setup_frame_dma(ofb->fbi, DMA_OV2_Y, -1, start[0], size);
661 setup_frame_dma(ofb->fbi, DMA_OV2_Cb, -1, start[1], size / div);
662 setup_frame_dma(ofb->fbi, DMA_OV2_Cr, -1, start[2], size / div);
663 }
664}
665
666static void overlay2fb_enable(struct pxafb_layer *ofb)
667{
668 int pfor = NONSTD_TO_PFOR(ofb->fb.var.nonstd);
669 int enabled = lcd_readl(ofb->fbi, OVL2C1) & OVLxC1_OEN;
670 uint32_t fdadr2 = ofb->fbi->fdadr[DMA_OV2_Y] | (enabled ? 0x1 : 0);
671 uint32_t fdadr3 = ofb->fbi->fdadr[DMA_OV2_Cb] | (enabled ? 0x1 : 0);
672 uint32_t fdadr4 = ofb->fbi->fdadr[DMA_OV2_Cr] | (enabled ? 0x1 : 0);
673
674 if (pfor == OVERLAY_FORMAT_RGB || pfor == OVERLAY_FORMAT_YUV444_PACKED)
675 lcd_writel(ofb->fbi, enabled ? FBR2 : FDADR2, fdadr2);
676 else {
677 lcd_writel(ofb->fbi, enabled ? FBR2 : FDADR2, fdadr2);
678 lcd_writel(ofb->fbi, enabled ? FBR3 : FDADR3, fdadr3);
679 lcd_writel(ofb->fbi, enabled ? FBR4 : FDADR4, fdadr4);
680 }
681 lcd_writel(ofb->fbi, OVL2C2, ofb->control[1]);
682 lcd_writel(ofb->fbi, OVL2C1, ofb->control[0] | OVLxC1_OEN);
683}
684
685static void overlay2fb_disable(struct pxafb_layer *ofb)
686{
687 uint32_t lccr5 = lcd_readl(ofb->fbi, LCCR5);
688
689 lcd_writel(ofb->fbi, OVL2C1, ofb->control[0] & ~OVLxC1_OEN);
690
691 lcd_writel(ofb->fbi, LCSR1, LCSR1_BS(2));
692 lcd_writel(ofb->fbi, LCCR5, lccr5 & ~LCSR1_BS(2));
693 lcd_writel(ofb->fbi, FBR2, ofb->fbi->fdadr[DMA_OV2_Y] | 0x3);
694 lcd_writel(ofb->fbi, FBR3, ofb->fbi->fdadr[DMA_OV2_Cb] | 0x3);
695 lcd_writel(ofb->fbi, FBR4, ofb->fbi->fdadr[DMA_OV2_Cr] | 0x3);
696
697 if (wait_for_completion_timeout(&ofb->branch_done, 1 * HZ) == 0)
698 pr_warning("%s: timeout disabling overlay2\n", __func__);
699}
700
701static struct pxafb_layer_ops ofb_ops[] = {
702 [0] = {
703 .enable = overlay1fb_enable,
704 .disable = overlay1fb_disable,
705 .setup = overlay1fb_setup,
706 },
707 [1] = {
708 .enable = overlay2fb_enable,
709 .disable = overlay2fb_disable,
710 .setup = overlay2fb_setup,
711 },
712};
713
714static int overlayfb_open(struct fb_info *info, int user)
715{
716 struct pxafb_layer *ofb = (struct pxafb_layer *)info;
717
718 /* no support for framebuffer console on overlay */
719 if (user == 0)
720 return -ENODEV;
721
722 /* allow only one user at a time */
723 if (atomic_inc_and_test(&ofb->usage))
724 return -EBUSY;
725
726 /* unblank the base framebuffer */
727 fb_blank(&ofb->fbi->fb, FB_BLANK_UNBLANK);
728 return 0;
729}
730
731static int overlayfb_release(struct fb_info *info, int user)
732{
733 struct pxafb_layer *ofb = (struct pxafb_layer*) info;
734
735 atomic_dec(&ofb->usage);
736 ofb->ops->disable(ofb);
737
738 free_pages_exact(ofb->video_mem, ofb->video_mem_size);
739 ofb->video_mem = NULL;
740 ofb->video_mem_size = 0;
741 return 0;
742}
743
744static int overlayfb_check_var(struct fb_var_screeninfo *var,
745 struct fb_info *info)
746{
747 struct pxafb_layer *ofb = (struct pxafb_layer *)info;
748 struct fb_var_screeninfo *base_var = &ofb->fbi->fb.var;
749 int xpos, ypos, pfor, bpp;
750
751 xpos = NONSTD_TO_XPOS(var->nonstd);
752 ypos = NONSTD_TO_XPOS(var->nonstd);
753 pfor = NONSTD_TO_PFOR(var->nonstd);
754
755 bpp = pxafb_var_to_bpp(var);
756 if (bpp < 0)
757 return -EINVAL;
758
759 /* no support for YUV format on overlay1 */
760 if (ofb->id == OVERLAY1 && pfor != 0)
761 return -EINVAL;
762
763 /* for YUV packed formats, bpp = 'minimum bpp of YUV components' */
764 switch (pfor) {
765 case OVERLAY_FORMAT_RGB:
766 bpp = pxafb_var_to_bpp(var);
767 if (bpp < 0)
768 return -EINVAL;
769
770 pxafb_set_pixfmt(var, var_to_depth(var));
771 break;
772 case OVERLAY_FORMAT_YUV444_PACKED: bpp = 24; break;
773 case OVERLAY_FORMAT_YUV444_PLANAR: bpp = 8; break;
774 case OVERLAY_FORMAT_YUV422_PLANAR: bpp = 4; break;
775 case OVERLAY_FORMAT_YUV420_PLANAR: bpp = 2; break;
776 default:
777 return -EINVAL;
778 }
779
780 /* each line must start at a 32-bit word boundary */
781 if ((xpos * bpp) % 32)
782 return -EINVAL;
783
784 /* xres must align on 32-bit word boundary */
785 var->xres = roundup(var->xres * bpp, 32) / bpp;
786
787 if ((xpos + var->xres > base_var->xres) ||
788 (ypos + var->yres > base_var->yres))
789 return -EINVAL;
790
791 var->xres_virtual = var->xres;
792 var->yres_virtual = max(var->yres, var->yres_virtual);
793 return 0;
794}
795
796static int overlayfb_map_video_memory(struct pxafb_layer *ofb)
797{
798 struct fb_var_screeninfo *var = &ofb->fb.var;
799 int pfor = NONSTD_TO_PFOR(var->nonstd);
800 int size, bpp = 0;
801
802 switch (pfor) {
803 case OVERLAY_FORMAT_RGB: bpp = var->bits_per_pixel; break;
804 case OVERLAY_FORMAT_YUV444_PACKED: bpp = 24; break;
805 case OVERLAY_FORMAT_YUV444_PLANAR: bpp = 24; break;
806 case OVERLAY_FORMAT_YUV422_PLANAR: bpp = 16; break;
807 case OVERLAY_FORMAT_YUV420_PLANAR: bpp = 12; break;
808 }
809
810 ofb->fb.fix.line_length = var->xres_virtual * bpp / 8;
811
812 size = PAGE_ALIGN(ofb->fb.fix.line_length * var->yres_virtual);
813
814 /* don't re-allocate if the original video memory is enough */
815 if (ofb->video_mem) {
816 if (ofb->video_mem_size >= size)
817 return 0;
818
819 free_pages_exact(ofb->video_mem, ofb->video_mem_size);
820 }
821
822 ofb->video_mem = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
823 if (ofb->video_mem == NULL)
824 return -ENOMEM;
825
826 ofb->video_mem_phys = virt_to_phys(ofb->video_mem);
827 ofb->video_mem_size = size;
828
Krzysztof Helt537a1bf2009-06-30 11:41:29 -0700829 mutex_lock(&ofb->fb.mm_lock);
Eric Miao198fc102008-12-23 17:49:43 +0800830 ofb->fb.fix.smem_start = ofb->video_mem_phys;
831 ofb->fb.fix.smem_len = ofb->fb.fix.line_length * var->yres_virtual;
Krzysztof Helt537a1bf2009-06-30 11:41:29 -0700832 mutex_unlock(&ofb->fb.mm_lock);
Eric Miao198fc102008-12-23 17:49:43 +0800833 ofb->fb.screen_base = ofb->video_mem;
834 return 0;
835}
836
837static int overlayfb_set_par(struct fb_info *info)
838{
839 struct pxafb_layer *ofb = (struct pxafb_layer *)info;
840 struct fb_var_screeninfo *var = &info->var;
841 int xpos, ypos, pfor, bpp, ret;
842
843 ret = overlayfb_map_video_memory(ofb);
844 if (ret)
845 return ret;
846
847 bpp = pxafb_var_to_bpp(var);
848 xpos = NONSTD_TO_XPOS(var->nonstd);
849 ypos = NONSTD_TO_XPOS(var->nonstd);
850 pfor = NONSTD_TO_PFOR(var->nonstd);
851
852 ofb->control[0] = OVLxC1_PPL(var->xres) | OVLxC1_LPO(var->yres) |
853 OVLxC1_BPP(bpp);
854 ofb->control[1] = OVLxC2_XPOS(xpos) | OVLxC2_YPOS(ypos);
855
856 if (ofb->id == OVERLAY2)
857 ofb->control[1] |= OVL2C2_PFOR(pfor);
858
859 ofb->ops->setup(ofb);
860 ofb->ops->enable(ofb);
861 return 0;
862}
863
864static struct fb_ops overlay_fb_ops = {
865 .owner = THIS_MODULE,
866 .fb_open = overlayfb_open,
867 .fb_release = overlayfb_release,
868 .fb_check_var = overlayfb_check_var,
869 .fb_set_par = overlayfb_set_par,
870};
871
872static void __devinit init_pxafb_overlay(struct pxafb_info *fbi,
873 struct pxafb_layer *ofb, int id)
874{
875 sprintf(ofb->fb.fix.id, "overlay%d", id + 1);
876
877 ofb->fb.fix.type = FB_TYPE_PACKED_PIXELS;
878 ofb->fb.fix.xpanstep = 0;
879 ofb->fb.fix.ypanstep = 1;
880
881 ofb->fb.var.activate = FB_ACTIVATE_NOW;
882 ofb->fb.var.height = -1;
883 ofb->fb.var.width = -1;
884 ofb->fb.var.vmode = FB_VMODE_NONINTERLACED;
885
886 ofb->fb.fbops = &overlay_fb_ops;
887 ofb->fb.flags = FBINFO_FLAG_DEFAULT;
888 ofb->fb.node = -1;
889 ofb->fb.pseudo_palette = NULL;
890
891 ofb->id = id;
892 ofb->ops = &ofb_ops[id];
893 atomic_set(&ofb->usage, 0);
894 ofb->fbi = fbi;
895 init_completion(&ofb->branch_done);
896}
897
Eric Miao782385a2009-03-19 15:24:30 +0800898static inline int pxafb_overlay_supported(void)
899{
900 if (cpu_is_pxa27x() || cpu_is_pxa3xx())
901 return 1;
902
903 return 0;
904}
905
Eric Miao198fc102008-12-23 17:49:43 +0800906static int __devinit pxafb_overlay_init(struct pxafb_info *fbi)
907{
908 int i, ret;
909
Eric Miao782385a2009-03-19 15:24:30 +0800910 if (!pxafb_overlay_supported())
911 return 0;
912
Eric Miao198fc102008-12-23 17:49:43 +0800913 for (i = 0; i < 2; i++) {
914 init_pxafb_overlay(fbi, &fbi->overlay[i], i);
915 ret = register_framebuffer(&fbi->overlay[i].fb);
916 if (ret) {
917 dev_err(fbi->dev, "failed to register overlay %d\n", i);
918 return ret;
919 }
920 }
921
922 /* mask all IU/BS/EOF/SOF interrupts */
923 lcd_writel(fbi, LCCR5, ~0);
924
925 /* place overlay(s) on top of base */
926 fbi->lccr0 |= LCCR0_OUC;
927 pr_info("PXA Overlay driver loaded successfully!\n");
928 return 0;
929}
930
931static void __devexit pxafb_overlay_exit(struct pxafb_info *fbi)
932{
933 int i;
934
Eric Miao782385a2009-03-19 15:24:30 +0800935 if (!pxafb_overlay_supported())
936 return;
937
Eric Miao198fc102008-12-23 17:49:43 +0800938 for (i = 0; i < 2; i++)
939 unregister_framebuffer(&fbi->overlay[i].fb);
940}
941#else
942static inline void pxafb_overlay_init(struct pxafb_info *fbi) {}
943static inline void pxafb_overlay_exit(struct pxafb_info *fbi) {}
944#endif /* CONFIG_FB_PXA_OVERLAY */
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946/*
947 * Calculate the PCD value from the clock rate (in picoseconds).
948 * We take account of the PPCR clock setting.
949 * From PXA Developer's Manual:
950 *
951 * PixelClock = LCLK
952 * -------------
953 * 2 ( PCD + 1 )
954 *
955 * PCD = LCLK
956 * ------------- - 1
957 * 2(PixelClock)
958 *
959 * Where:
960 * LCLK = LCD/Memory Clock
961 * PCD = LCCR3[7:0]
962 *
963 * PixelClock here is in Hz while the pixclock argument given is the
964 * period in picoseconds. Hence PixelClock = 1 / ( pixclock * 10^-12 )
965 *
966 * The function get_lclk_frequency_10khz returns LCLK in units of
967 * 10khz. Calling the result of this function lclk gives us the
968 * following
969 *
970 * PCD = (lclk * 10^4 ) * ( pixclock * 10^-12 )
971 * -------------------------------------- - 1
972 * 2
973 *
974 * Factoring the 10^4 and 10^-12 out gives 10^-8 == 1 / 100000000 as used below.
975 */
eric miaob0086ef2008-04-30 00:52:19 -0700976static inline unsigned int get_pcd(struct pxafb_info *fbi,
977 unsigned int pixclock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
979 unsigned long long pcd;
980
981 /* FIXME: Need to take into account Double Pixel Clock mode
Russell King72e35242007-08-20 10:18:42 +0100982 * (DPC) bit? or perhaps set it based on the various clock
983 * speeds */
984 pcd = (unsigned long long)(clk_get_rate(fbi->clk) / 10000);
985 pcd *= pixclock;
Nicolas Pitrebf1b8ab2005-06-23 21:56:45 +0100986 do_div(pcd, 100000000 * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 /* no need for this, since we should subtract 1 anyway. they cancel */
988 /* pcd += 1; */ /* make up for integer math truncations */
989 return (unsigned int)pcd;
990}
991
992/*
Richard Purdieba44cd22005-09-09 13:10:03 -0700993 * Some touchscreens need hsync information from the video driver to
Russell King72e35242007-08-20 10:18:42 +0100994 * function correctly. We export it here. Note that 'hsync_time' and
995 * the value returned from pxafb_get_hsync_time() is the *reciprocal*
996 * of the hsync period in seconds.
Richard Purdieba44cd22005-09-09 13:10:03 -0700997 */
998static inline void set_hsync_time(struct pxafb_info *fbi, unsigned int pcd)
999{
Russell King72e35242007-08-20 10:18:42 +01001000 unsigned long htime;
Richard Purdieba44cd22005-09-09 13:10:03 -07001001
1002 if ((pcd == 0) || (fbi->fb.var.hsync_len == 0)) {
eric miaob0086ef2008-04-30 00:52:19 -07001003 fbi->hsync_time = 0;
Richard Purdieba44cd22005-09-09 13:10:03 -07001004 return;
1005 }
1006
Russell King72e35242007-08-20 10:18:42 +01001007 htime = clk_get_rate(fbi->clk) / (pcd * fbi->fb.var.hsync_len);
1008
Richard Purdieba44cd22005-09-09 13:10:03 -07001009 fbi->hsync_time = htime;
1010}
1011
1012unsigned long pxafb_get_hsync_time(struct device *dev)
1013{
1014 struct pxafb_info *fbi = dev_get_drvdata(dev);
1015
1016 /* If display is blanked/suspended, hsync isn't active */
1017 if (!fbi || (fbi->state != C_ENABLE))
1018 return 0;
1019
1020 return fbi->hsync_time;
1021}
1022EXPORT_SYMBOL(pxafb_get_hsync_time);
1023
eric miao2c42dd82008-04-30 00:52:21 -07001024static int setup_frame_dma(struct pxafb_info *fbi, int dma, int pal,
Eric Miao198fc102008-12-23 17:49:43 +08001025 unsigned long start, size_t size)
eric miao2c42dd82008-04-30 00:52:21 -07001026{
1027 struct pxafb_dma_descriptor *dma_desc, *pal_desc;
1028 unsigned int dma_desc_off, pal_desc_off;
1029
Eric Miao6e354842008-12-17 16:50:43 +08001030 if (dma < 0 || dma >= DMA_MAX * 2)
eric miao2c42dd82008-04-30 00:52:21 -07001031 return -EINVAL;
1032
1033 dma_desc = &fbi->dma_buff->dma_desc[dma];
1034 dma_desc_off = offsetof(struct pxafb_dma_buff, dma_desc[dma]);
1035
Eric Miao198fc102008-12-23 17:49:43 +08001036 dma_desc->fsadr = start;
eric miao2c42dd82008-04-30 00:52:21 -07001037 dma_desc->fidr = 0;
1038 dma_desc->ldcmd = size;
1039
Eric Miao6e354842008-12-17 16:50:43 +08001040 if (pal < 0 || pal >= PAL_MAX * 2) {
eric miao2c42dd82008-04-30 00:52:21 -07001041 dma_desc->fdadr = fbi->dma_buff_phys + dma_desc_off;
1042 fbi->fdadr[dma] = fbi->dma_buff_phys + dma_desc_off;
1043 } else {
Jürgen Schindele62cfcf42008-06-11 19:56:06 +01001044 pal_desc = &fbi->dma_buff->pal_desc[pal];
1045 pal_desc_off = offsetof(struct pxafb_dma_buff, pal_desc[pal]);
eric miao2c42dd82008-04-30 00:52:21 -07001046
1047 pal_desc->fsadr = fbi->dma_buff_phys + pal * PALETTE_SIZE;
1048 pal_desc->fidr = 0;
1049
1050 if ((fbi->lccr4 & LCCR4_PAL_FOR_MASK) == LCCR4_PAL_FOR_0)
1051 pal_desc->ldcmd = fbi->palette_size * sizeof(u16);
1052 else
1053 pal_desc->ldcmd = fbi->palette_size * sizeof(u32);
1054
1055 pal_desc->ldcmd |= LDCMD_PAL;
1056
1057 /* flip back and forth between palette and frame buffer */
1058 pal_desc->fdadr = fbi->dma_buff_phys + dma_desc_off;
1059 dma_desc->fdadr = fbi->dma_buff_phys + pal_desc_off;
1060 fbi->fdadr[dma] = fbi->dma_buff_phys + dma_desc_off;
1061 }
1062
1063 return 0;
1064}
1065
Sven Neumann448ac472009-10-22 08:34:34 +02001066static void setup_base_frame(struct pxafb_info *fbi,
1067 struct fb_var_screeninfo *var,
1068 int branch)
Eric Miao6e354842008-12-17 16:50:43 +08001069{
Eric Miao6e354842008-12-17 16:50:43 +08001070 struct fb_fix_screeninfo *fix = &fbi->fb.fix;
Eric Miao198fc102008-12-23 17:49:43 +08001071 int nbytes, dma, pal, bpp = var->bits_per_pixel;
1072 unsigned long offset;
Eric Miao6e354842008-12-17 16:50:43 +08001073
1074 dma = DMA_BASE + (branch ? DMA_MAX : 0);
1075 pal = (bpp >= 16) ? PAL_NONE : PAL_BASE + (branch ? PAL_MAX : 0);
1076
1077 nbytes = fix->line_length * var->yres;
Eric Miao198fc102008-12-23 17:49:43 +08001078 offset = fix->line_length * var->yoffset + fbi->video_mem_phys;
Eric Miao6e354842008-12-17 16:50:43 +08001079
1080 if (fbi->lccr0 & LCCR0_SDS) {
1081 nbytes = nbytes / 2;
1082 setup_frame_dma(fbi, dma + 1, PAL_NONE, offset + nbytes, nbytes);
1083 }
1084
1085 setup_frame_dma(fbi, dma, pal, offset, nbytes);
1086}
1087
Eric Miao3c42a442008-04-30 00:52:26 -07001088#ifdef CONFIG_FB_PXA_SMARTPANEL
1089static int setup_smart_dma(struct pxafb_info *fbi)
1090{
1091 struct pxafb_dma_descriptor *dma_desc;
1092 unsigned long dma_desc_off, cmd_buff_off;
1093
1094 dma_desc = &fbi->dma_buff->dma_desc[DMA_CMD];
1095 dma_desc_off = offsetof(struct pxafb_dma_buff, dma_desc[DMA_CMD]);
1096 cmd_buff_off = offsetof(struct pxafb_dma_buff, cmd_buff);
1097
1098 dma_desc->fdadr = fbi->dma_buff_phys + dma_desc_off;
1099 dma_desc->fsadr = fbi->dma_buff_phys + cmd_buff_off;
1100 dma_desc->fidr = 0;
1101 dma_desc->ldcmd = fbi->n_smart_cmds * sizeof(uint16_t);
1102
1103 fbi->fdadr[DMA_CMD] = dma_desc->fdadr;
1104 return 0;
1105}
1106
1107int pxafb_smart_flush(struct fb_info *info)
1108{
1109 struct pxafb_info *fbi = container_of(info, struct pxafb_info, fb);
1110 uint32_t prsr;
1111 int ret = 0;
1112
1113 /* disable controller until all registers are set up */
1114 lcd_writel(fbi, LCCR0, fbi->reg_lccr0 & ~LCCR0_ENB);
1115
1116 /* 1. make it an even number of commands to align on 32-bit boundary
1117 * 2. add the interrupt command to the end of the chain so we can
1118 * keep track of the end of the transfer
1119 */
1120
1121 while (fbi->n_smart_cmds & 1)
1122 fbi->smart_cmds[fbi->n_smart_cmds++] = SMART_CMD_NOOP;
1123
1124 fbi->smart_cmds[fbi->n_smart_cmds++] = SMART_CMD_INTERRUPT;
1125 fbi->smart_cmds[fbi->n_smart_cmds++] = SMART_CMD_WAIT_FOR_VSYNC;
1126 setup_smart_dma(fbi);
1127
1128 /* continue to execute next command */
1129 prsr = lcd_readl(fbi, PRSR) | PRSR_ST_OK | PRSR_CON_NT;
1130 lcd_writel(fbi, PRSR, prsr);
1131
1132 /* stop the processor in case it executed "wait for sync" cmd */
1133 lcd_writel(fbi, CMDCR, 0x0001);
1134
1135 /* don't send interrupts for fifo underruns on channel 6 */
1136 lcd_writel(fbi, LCCR5, LCCR5_IUM(6));
1137
1138 lcd_writel(fbi, LCCR1, fbi->reg_lccr1);
1139 lcd_writel(fbi, LCCR2, fbi->reg_lccr2);
1140 lcd_writel(fbi, LCCR3, fbi->reg_lccr3);
Eric Miaoa0427502008-12-18 22:10:00 +08001141 lcd_writel(fbi, LCCR4, fbi->reg_lccr4);
Eric Miao3c42a442008-04-30 00:52:26 -07001142 lcd_writel(fbi, FDADR0, fbi->fdadr[0]);
1143 lcd_writel(fbi, FDADR6, fbi->fdadr[6]);
1144
1145 /* begin sending */
1146 lcd_writel(fbi, LCCR0, fbi->reg_lccr0 | LCCR0_ENB);
1147
1148 if (wait_for_completion_timeout(&fbi->command_done, HZ/2) == 0) {
1149 pr_warning("%s: timeout waiting for command done\n",
1150 __func__);
1151 ret = -ETIMEDOUT;
1152 }
1153
1154 /* quick disable */
1155 prsr = lcd_readl(fbi, PRSR) & ~(PRSR_ST_OK | PRSR_CON_NT);
1156 lcd_writel(fbi, PRSR, prsr);
1157 lcd_writel(fbi, LCCR0, fbi->reg_lccr0 & ~LCCR0_ENB);
1158 lcd_writel(fbi, FDADR6, 0);
1159 fbi->n_smart_cmds = 0;
1160 return ret;
1161}
1162
1163int pxafb_smart_queue(struct fb_info *info, uint16_t *cmds, int n_cmds)
1164{
1165 int i;
1166 struct pxafb_info *fbi = container_of(info, struct pxafb_info, fb);
1167
Eric Miao69bdea72008-12-08 18:46:00 +08001168 for (i = 0; i < n_cmds; i++, cmds++) {
1169 /* if it is a software delay, flush and delay */
1170 if ((*cmds & 0xff00) == SMART_CMD_DELAY) {
1171 pxafb_smart_flush(info);
1172 mdelay(*cmds & 0xff);
1173 continue;
1174 }
1175
1176 /* leave 2 commands for INTERRUPT and WAIT_FOR_SYNC */
Eric Miao3c42a442008-04-30 00:52:26 -07001177 if (fbi->n_smart_cmds == CMD_BUFF_SIZE - 8)
1178 pxafb_smart_flush(info);
1179
Eric Miao69bdea72008-12-08 18:46:00 +08001180 fbi->smart_cmds[fbi->n_smart_cmds++] = *cmds;
Eric Miao3c42a442008-04-30 00:52:26 -07001181 }
1182
1183 return 0;
1184}
1185
1186static unsigned int __smart_timing(unsigned time_ns, unsigned long lcd_clk)
1187{
1188 unsigned int t = (time_ns * (lcd_clk / 1000000) / 1000);
1189 return (t == 0) ? 1 : t;
1190}
1191
1192static void setup_smart_timing(struct pxafb_info *fbi,
1193 struct fb_var_screeninfo *var)
1194{
1195 struct pxafb_mach_info *inf = fbi->dev->platform_data;
1196 struct pxafb_mode_info *mode = &inf->modes[0];
1197 unsigned long lclk = clk_get_rate(fbi->clk);
1198 unsigned t1, t2, t3, t4;
1199
1200 t1 = max(mode->a0csrd_set_hld, mode->a0cswr_set_hld);
1201 t2 = max(mode->rd_pulse_width, mode->wr_pulse_width);
1202 t3 = mode->op_hold_time;
1203 t4 = mode->cmd_inh_time;
1204
1205 fbi->reg_lccr1 =
1206 LCCR1_DisWdth(var->xres) |
1207 LCCR1_BegLnDel(__smart_timing(t1, lclk)) |
1208 LCCR1_EndLnDel(__smart_timing(t2, lclk)) |
1209 LCCR1_HorSnchWdth(__smart_timing(t3, lclk));
1210
1211 fbi->reg_lccr2 = LCCR2_DisHght(var->yres);
Eric Miaoc1f99c22008-12-08 18:35:03 +08001212 fbi->reg_lccr3 = fbi->lccr3 | LCCR3_PixClkDiv(__smart_timing(t4, lclk));
1213 fbi->reg_lccr3 |= (var->sync & FB_SYNC_HOR_HIGH_ACT) ? LCCR3_HSP : 0;
1214 fbi->reg_lccr3 |= (var->sync & FB_SYNC_VERT_HIGH_ACT) ? LCCR3_VSP : 0;
Eric Miao3c42a442008-04-30 00:52:26 -07001215
1216 /* FIXME: make this configurable */
1217 fbi->reg_cmdcr = 1;
1218}
1219
1220static int pxafb_smart_thread(void *arg)
1221{
Eric Miao7f1133c2008-04-30 00:52:27 -07001222 struct pxafb_info *fbi = arg;
Eric Miao3c42a442008-04-30 00:52:26 -07001223 struct pxafb_mach_info *inf = fbi->dev->platform_data;
1224
1225 if (!fbi || !inf->smart_update) {
1226 pr_err("%s: not properly initialized, thread terminated\n",
1227 __func__);
1228 return -EINVAL;
1229 }
1230
1231 pr_debug("%s(): task starting\n", __func__);
1232
1233 set_freezable();
1234 while (!kthread_should_stop()) {
1235
1236 if (try_to_freeze())
1237 continue;
1238
Eric Miao07f651c2008-12-08 18:51:01 +08001239 mutex_lock(&fbi->ctrlr_lock);
1240
Eric Miao3c42a442008-04-30 00:52:26 -07001241 if (fbi->state == C_ENABLE) {
1242 inf->smart_update(&fbi->fb);
1243 complete(&fbi->refresh_done);
1244 }
1245
Eric Miao07f651c2008-12-08 18:51:01 +08001246 mutex_unlock(&fbi->ctrlr_lock);
1247
Eric Miao3c42a442008-04-30 00:52:26 -07001248 set_current_state(TASK_INTERRUPTIBLE);
1249 schedule_timeout(30 * HZ / 1000);
1250 }
1251
1252 pr_debug("%s(): task ending\n", __func__);
1253 return 0;
1254}
1255
1256static int pxafb_smart_init(struct pxafb_info *fbi)
1257{
Eric Miao07df1c42008-12-04 11:14:11 +08001258 if (!(fbi->lccr0 & LCCR0_LCDT))
Eric Miao6cc4abe2008-11-11 21:47:07 +08001259 return 0;
1260
Eric Miao07df1c42008-12-04 11:14:11 +08001261 fbi->smart_cmds = (uint16_t *) fbi->dma_buff->cmd_buff;
1262 fbi->n_smart_cmds = 0;
1263
1264 init_completion(&fbi->command_done);
1265 init_completion(&fbi->refresh_done);
1266
Eric Miao3c42a442008-04-30 00:52:26 -07001267 fbi->smart_thread = kthread_run(pxafb_smart_thread, fbi,
1268 "lcd_refresh");
1269 if (IS_ERR(fbi->smart_thread)) {
Eric Miao07df1c42008-12-04 11:14:11 +08001270 pr_err("%s: unable to create kernel thread\n", __func__);
Eric Miao3c42a442008-04-30 00:52:26 -07001271 return PTR_ERR(fbi->smart_thread);
1272 }
Eric Miaoa5718a12008-11-11 21:50:39 +08001273
Eric Miao3c42a442008-04-30 00:52:26 -07001274 return 0;
1275}
1276#else
1277int pxafb_smart_queue(struct fb_info *info, uint16_t *cmds, int n_cmds)
1278{
1279 return 0;
1280}
1281
1282int pxafb_smart_flush(struct fb_info *info)
1283{
1284 return 0;
1285}
Eric Miao07df1c42008-12-04 11:14:11 +08001286
1287static inline int pxafb_smart_init(struct pxafb_info *fbi) { return 0; }
1288#endif /* CONFIG_FB_PXA_SMARTPANEL */
Eric Miao3c42a442008-04-30 00:52:26 -07001289
Eric Miao90eabbf2008-04-30 00:52:25 -07001290static void setup_parallel_timing(struct pxafb_info *fbi,
1291 struct fb_var_screeninfo *var)
1292{
1293 unsigned int lines_per_panel, pcd = get_pcd(fbi, var->pixclock);
1294
1295 fbi->reg_lccr1 =
1296 LCCR1_DisWdth(var->xres) +
1297 LCCR1_HorSnchWdth(var->hsync_len) +
1298 LCCR1_BegLnDel(var->left_margin) +
1299 LCCR1_EndLnDel(var->right_margin);
1300
1301 /*
1302 * If we have a dual scan LCD, we need to halve
1303 * the YRES parameter.
1304 */
1305 lines_per_panel = var->yres;
1306 if ((fbi->lccr0 & LCCR0_SDS) == LCCR0_Dual)
1307 lines_per_panel /= 2;
1308
1309 fbi->reg_lccr2 =
1310 LCCR2_DisHght(lines_per_panel) +
1311 LCCR2_VrtSnchWdth(var->vsync_len) +
1312 LCCR2_BegFrmDel(var->upper_margin) +
1313 LCCR2_EndFrmDel(var->lower_margin);
1314
1315 fbi->reg_lccr3 = fbi->lccr3 |
1316 (var->sync & FB_SYNC_HOR_HIGH_ACT ?
1317 LCCR3_HorSnchH : LCCR3_HorSnchL) |
1318 (var->sync & FB_SYNC_VERT_HIGH_ACT ?
1319 LCCR3_VrtSnchH : LCCR3_VrtSnchL);
1320
1321 if (pcd) {
1322 fbi->reg_lccr3 |= LCCR3_PixClkDiv(pcd);
1323 set_hsync_time(fbi, pcd);
1324 }
1325}
1326
Richard Purdieba44cd22005-09-09 13:10:03 -07001327/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 * pxafb_activate_var():
eric miaob0086ef2008-04-30 00:52:19 -07001329 * Configures LCD Controller based on entries in var parameter.
1330 * Settings are only written to the controller if changes were made.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 */
eric miaob0086ef2008-04-30 00:52:19 -07001332static int pxafb_activate_var(struct fb_var_screeninfo *var,
1333 struct pxafb_info *fbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 u_long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 /* Update shadow copy atomically */
1338 local_irq_save(flags);
1339
Eric Miao3c42a442008-04-30 00:52:26 -07001340#ifdef CONFIG_FB_PXA_SMARTPANEL
1341 if (fbi->lccr0 & LCCR0_LCDT)
1342 setup_smart_timing(fbi, var);
1343 else
1344#endif
1345 setup_parallel_timing(fbi, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Sven Neumann448ac472009-10-22 08:34:34 +02001347 setup_base_frame(fbi, var, 0);
Eric Miao6e354842008-12-17 16:50:43 +08001348
Eric Miao90eabbf2008-04-30 00:52:25 -07001349 fbi->reg_lccr0 = fbi->lccr0 |
1350 (LCCR0_LDM | LCCR0_SFM | LCCR0_IUM | LCCR0_EFM |
1351 LCCR0_QDM | LCCR0_BM | LCCR0_OUM);
1352
Eric Miao878f5782008-12-18 22:36:26 +08001353 fbi->reg_lccr3 |= pxafb_var_to_lccr3(var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Eric Miaoa7535ba2008-04-30 00:52:24 -07001355 fbi->reg_lccr4 = lcd_readl(fbi, LCCR4) & ~LCCR4_PAL_FOR_MASK;
Hans J. Koch9ffa7392007-10-16 01:28:41 -07001356 fbi->reg_lccr4 |= (fbi->lccr4 & LCCR4_PAL_FOR_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 local_irq_restore(flags);
1358
1359 /*
1360 * Only update the registers if the controller is enabled
1361 * and something has changed.
1362 */
Eric Miaoa7535ba2008-04-30 00:52:24 -07001363 if ((lcd_readl(fbi, LCCR0) != fbi->reg_lccr0) ||
1364 (lcd_readl(fbi, LCCR1) != fbi->reg_lccr1) ||
1365 (lcd_readl(fbi, LCCR2) != fbi->reg_lccr2) ||
1366 (lcd_readl(fbi, LCCR3) != fbi->reg_lccr3) ||
Eric Miaoa0427502008-12-18 22:10:00 +08001367 (lcd_readl(fbi, LCCR4) != fbi->reg_lccr4) ||
Eric Miaoa7535ba2008-04-30 00:52:24 -07001368 (lcd_readl(fbi, FDADR0) != fbi->fdadr[0]) ||
1369 (lcd_readl(fbi, FDADR1) != fbi->fdadr[1]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 pxafb_schedule_work(fbi, C_REENABLE);
1371
1372 return 0;
1373}
1374
1375/*
1376 * NOTE! The following functions are purely helpers for set_ctrlr_state.
1377 * Do not call them directly; set_ctrlr_state does the correct serialisation
1378 * to ensure that things happen in the right way 100% of time time.
1379 * -- rmk
1380 */
1381static inline void __pxafb_backlight_power(struct pxafb_info *fbi, int on)
1382{
Russell Kingca5da712005-09-29 09:44:54 +01001383 pr_debug("pxafb: backlight o%s\n", on ? "n" : "ff");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Eric Miaoa5718a12008-11-11 21:50:39 +08001385 if (fbi->backlight_power)
1386 fbi->backlight_power(on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387}
1388
1389static inline void __pxafb_lcd_power(struct pxafb_info *fbi, int on)
1390{
Russell Kingca5da712005-09-29 09:44:54 +01001391 pr_debug("pxafb: LCD power o%s\n", on ? "n" : "ff");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Eric Miaoa5718a12008-11-11 21:50:39 +08001393 if (fbi->lcd_power)
1394 fbi->lcd_power(on, &fbi->fb.var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395}
1396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397static void pxafb_enable_controller(struct pxafb_info *fbi)
1398{
Russell Kingca5da712005-09-29 09:44:54 +01001399 pr_debug("pxafb: Enabling LCD controller\n");
eric miao2c42dd82008-04-30 00:52:21 -07001400 pr_debug("fdadr0 0x%08x\n", (unsigned int) fbi->fdadr[0]);
1401 pr_debug("fdadr1 0x%08x\n", (unsigned int) fbi->fdadr[1]);
Russell Kingca5da712005-09-29 09:44:54 +01001402 pr_debug("reg_lccr0 0x%08x\n", (unsigned int) fbi->reg_lccr0);
1403 pr_debug("reg_lccr1 0x%08x\n", (unsigned int) fbi->reg_lccr1);
1404 pr_debug("reg_lccr2 0x%08x\n", (unsigned int) fbi->reg_lccr2);
1405 pr_debug("reg_lccr3 0x%08x\n", (unsigned int) fbi->reg_lccr3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Nicolas Pitre8d372262005-08-10 16:45:13 +01001407 /* enable LCD controller clock */
Russell King72e35242007-08-20 10:18:42 +01001408 clk_enable(fbi->clk);
Nicolas Pitre8d372262005-08-10 16:45:13 +01001409
Eric Miao3c42a442008-04-30 00:52:26 -07001410 if (fbi->lccr0 & LCCR0_LCDT)
1411 return;
1412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 /* Sequence from 11.7.10 */
Eric Miaoa0427502008-12-18 22:10:00 +08001414 lcd_writel(fbi, LCCR4, fbi->reg_lccr4);
Eric Miaoa7535ba2008-04-30 00:52:24 -07001415 lcd_writel(fbi, LCCR3, fbi->reg_lccr3);
1416 lcd_writel(fbi, LCCR2, fbi->reg_lccr2);
1417 lcd_writel(fbi, LCCR1, fbi->reg_lccr1);
1418 lcd_writel(fbi, LCCR0, fbi->reg_lccr0 & ~LCCR0_ENB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Eric Miaoa7535ba2008-04-30 00:52:24 -07001420 lcd_writel(fbi, FDADR0, fbi->fdadr[0]);
1421 lcd_writel(fbi, FDADR1, fbi->fdadr[1]);
1422 lcd_writel(fbi, LCCR0, fbi->reg_lccr0 | LCCR0_ENB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423}
1424
1425static void pxafb_disable_controller(struct pxafb_info *fbi)
1426{
eric miaoce4fb7b2008-04-30 00:52:21 -07001427 uint32_t lccr0;
1428
Eric Miao3c42a442008-04-30 00:52:26 -07001429#ifdef CONFIG_FB_PXA_SMARTPANEL
1430 if (fbi->lccr0 & LCCR0_LCDT) {
1431 wait_for_completion_timeout(&fbi->refresh_done,
1432 200 * HZ / 1000);
1433 return;
1434 }
1435#endif
1436
eric miaoce4fb7b2008-04-30 00:52:21 -07001437 /* Clear LCD Status Register */
Eric Miaoa7535ba2008-04-30 00:52:24 -07001438 lcd_writel(fbi, LCSR, 0xffffffff);
eric miaoce4fb7b2008-04-30 00:52:21 -07001439
Eric Miaoa7535ba2008-04-30 00:52:24 -07001440 lccr0 = lcd_readl(fbi, LCCR0) & ~LCCR0_LDM;
1441 lcd_writel(fbi, LCCR0, lccr0);
1442 lcd_writel(fbi, LCCR0, lccr0 | LCCR0_DIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Eric Miao2ba162b2008-04-30 00:52:24 -07001444 wait_for_completion_timeout(&fbi->disable_done, 200 * HZ / 1000);
Nicolas Pitre8d372262005-08-10 16:45:13 +01001445
1446 /* disable LCD controller clock */
Russell King72e35242007-08-20 10:18:42 +01001447 clk_disable(fbi->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448}
1449
1450/*
1451 * pxafb_handle_irq: Handle 'LCD DONE' interrupts.
1452 */
David Howells7d12e782006-10-05 14:55:46 +01001453static irqreturn_t pxafb_handle_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454{
1455 struct pxafb_info *fbi = dev_id;
Denis V. Lunevff14ed52009-04-21 12:23:59 -07001456 unsigned int lccr0, lcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Eric Miao198fc102008-12-23 17:49:43 +08001458 lcsr = lcd_readl(fbi, LCSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 if (lcsr & LCSR_LDD) {
Eric Miaoa7535ba2008-04-30 00:52:24 -07001460 lccr0 = lcd_readl(fbi, LCCR0);
1461 lcd_writel(fbi, LCCR0, lccr0 | LCCR0_LDM);
Eric Miao2ba162b2008-04-30 00:52:24 -07001462 complete(&fbi->disable_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 }
1464
Eric Miao3c42a442008-04-30 00:52:26 -07001465#ifdef CONFIG_FB_PXA_SMARTPANEL
1466 if (lcsr & LCSR_CMD_INT)
1467 complete(&fbi->command_done);
1468#endif
Eric Miaoa7535ba2008-04-30 00:52:24 -07001469 lcd_writel(fbi, LCSR, lcsr);
Eric Miao198fc102008-12-23 17:49:43 +08001470
1471#ifdef CONFIG_FB_PXA_OVERLAY
Denis V. Lunevff14ed52009-04-21 12:23:59 -07001472 {
1473 unsigned int lcsr1 = lcd_readl(fbi, LCSR1);
1474 if (lcsr1 & LCSR1_BS(1))
1475 complete(&fbi->overlay[0].branch_done);
Eric Miao198fc102008-12-23 17:49:43 +08001476
Denis V. Lunevff14ed52009-04-21 12:23:59 -07001477 if (lcsr1 & LCSR1_BS(2))
1478 complete(&fbi->overlay[1].branch_done);
Eric Miao198fc102008-12-23 17:49:43 +08001479
Denis V. Lunevff14ed52009-04-21 12:23:59 -07001480 lcd_writel(fbi, LCSR1, lcsr1);
1481 }
Eric Miao198fc102008-12-23 17:49:43 +08001482#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 return IRQ_HANDLED;
1484}
1485
1486/*
1487 * This function must be called from task context only, since it will
1488 * sleep when disabling the LCD controller, or if we get two contending
1489 * processes trying to alter state.
1490 */
1491static void set_ctrlr_state(struct pxafb_info *fbi, u_int state)
1492{
1493 u_int old_state;
1494
Matthias Kaehlckeb91dbce2008-07-23 21:31:14 -07001495 mutex_lock(&fbi->ctrlr_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
1497 old_state = fbi->state;
1498
1499 /*
1500 * Hack around fbcon initialisation.
1501 */
1502 if (old_state == C_STARTUP && state == C_REENABLE)
1503 state = C_ENABLE;
1504
1505 switch (state) {
1506 case C_DISABLE_CLKCHANGE:
1507 /*
1508 * Disable controller for clock change. If the
1509 * controller is already disabled, then do nothing.
1510 */
1511 if (old_state != C_DISABLE && old_state != C_DISABLE_PM) {
1512 fbi->state = state;
eric miaob0086ef2008-04-30 00:52:19 -07001513 /* TODO __pxafb_lcd_power(fbi, 0); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 pxafb_disable_controller(fbi);
1515 }
1516 break;
1517
1518 case C_DISABLE_PM:
1519 case C_DISABLE:
1520 /*
1521 * Disable controller
1522 */
1523 if (old_state != C_DISABLE) {
1524 fbi->state = state;
1525 __pxafb_backlight_power(fbi, 0);
1526 __pxafb_lcd_power(fbi, 0);
1527 if (old_state != C_DISABLE_CLKCHANGE)
1528 pxafb_disable_controller(fbi);
1529 }
1530 break;
1531
1532 case C_ENABLE_CLKCHANGE:
1533 /*
1534 * Enable the controller after clock change. Only
1535 * do this if we were disabled for the clock change.
1536 */
1537 if (old_state == C_DISABLE_CLKCHANGE) {
1538 fbi->state = C_ENABLE;
1539 pxafb_enable_controller(fbi);
eric miaob0086ef2008-04-30 00:52:19 -07001540 /* TODO __pxafb_lcd_power(fbi, 1); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 }
1542 break;
1543
1544 case C_REENABLE:
1545 /*
1546 * Re-enable the controller only if it was already
1547 * enabled. This is so we reprogram the control
1548 * registers.
1549 */
1550 if (old_state == C_ENABLE) {
Richard Purdied14b2722006-09-20 22:54:21 +01001551 __pxafb_lcd_power(fbi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 pxafb_disable_controller(fbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 pxafb_enable_controller(fbi);
Richard Purdied14b2722006-09-20 22:54:21 +01001554 __pxafb_lcd_power(fbi, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 }
1556 break;
1557
1558 case C_ENABLE_PM:
1559 /*
1560 * Re-enable the controller after PM. This is not
1561 * perfect - think about the case where we were doing
1562 * a clock change, and we suspended half-way through.
1563 */
1564 if (old_state != C_DISABLE_PM)
1565 break;
1566 /* fall through */
1567
1568 case C_ENABLE:
1569 /*
1570 * Power up the LCD screen, enable controller, and
1571 * turn on the backlight.
1572 */
1573 if (old_state != C_ENABLE) {
1574 fbi->state = C_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 pxafb_enable_controller(fbi);
1576 __pxafb_lcd_power(fbi, 1);
1577 __pxafb_backlight_power(fbi, 1);
1578 }
1579 break;
1580 }
Matthias Kaehlckeb91dbce2008-07-23 21:31:14 -07001581 mutex_unlock(&fbi->ctrlr_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582}
1583
1584/*
1585 * Our LCD controller task (which is called when we blank or unblank)
1586 * via keventd.
1587 */
David Howells6d5aefb2006-12-05 19:36:26 +00001588static void pxafb_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589{
David Howells6d5aefb2006-12-05 19:36:26 +00001590 struct pxafb_info *fbi =
1591 container_of(work, struct pxafb_info, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 u_int state = xchg(&fbi->task_state, -1);
1593
1594 set_ctrlr_state(fbi, state);
1595}
1596
1597#ifdef CONFIG_CPU_FREQ
1598/*
1599 * CPU clock speed change handler. We need to adjust the LCD timing
1600 * parameters when the CPU clock is adjusted by the power management
1601 * subsystem.
1602 *
1603 * TODO: Determine why f->new != 10*get_lclk_frequency_10khz()
1604 */
1605static int
1606pxafb_freq_transition(struct notifier_block *nb, unsigned long val, void *data)
1607{
1608 struct pxafb_info *fbi = TO_INF(nb, freq_transition);
eric miaob0086ef2008-04-30 00:52:19 -07001609 /* TODO struct cpufreq_freqs *f = data; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 u_int pcd;
1611
1612 switch (val) {
1613 case CPUFREQ_PRECHANGE:
1614 set_ctrlr_state(fbi, C_DISABLE_CLKCHANGE);
1615 break;
1616
1617 case CPUFREQ_POSTCHANGE:
Russell King72e35242007-08-20 10:18:42 +01001618 pcd = get_pcd(fbi, fbi->fb.var.pixclock);
Richard Purdieba44cd22005-09-09 13:10:03 -07001619 set_hsync_time(fbi, pcd);
eric miaob0086ef2008-04-30 00:52:19 -07001620 fbi->reg_lccr3 = (fbi->reg_lccr3 & ~0xff) |
1621 LCCR3_PixClkDiv(pcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 set_ctrlr_state(fbi, C_ENABLE_CLKCHANGE);
1623 break;
1624 }
1625 return 0;
1626}
1627
1628static int
1629pxafb_freq_policy(struct notifier_block *nb, unsigned long val, void *data)
1630{
1631 struct pxafb_info *fbi = TO_INF(nb, freq_policy);
1632 struct fb_var_screeninfo *var = &fbi->fb.var;
1633 struct cpufreq_policy *policy = data;
1634
1635 switch (val) {
1636 case CPUFREQ_ADJUST:
1637 case CPUFREQ_INCOMPATIBLE:
Holger Schurigac2bf5b2008-02-11 16:52:30 +01001638 pr_debug("min dma period: %d ps, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 "new clock %d kHz\n", pxafb_display_dma_period(var),
1640 policy->max);
eric miaob0086ef2008-04-30 00:52:19 -07001641 /* TODO: fill in min/max values */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
1644 return 0;
1645}
1646#endif
1647
1648#ifdef CONFIG_PM
1649/*
1650 * Power management hooks. Note that we won't be called from IRQ context,
1651 * unlike the blank functions above, so we may sleep.
1652 */
Mike Rapoport4f3edfe2009-07-21 17:51:50 +03001653static int pxafb_suspend(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
Mike Rapoport4f3edfe2009-07-21 17:51:50 +03001655 struct pxafb_info *fbi = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Russell King9480e302005-10-28 09:52:56 -07001657 set_ctrlr_state(fbi, C_DISABLE_PM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 return 0;
1659}
1660
Mike Rapoport4f3edfe2009-07-21 17:51:50 +03001661static int pxafb_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662{
Mike Rapoport4f3edfe2009-07-21 17:51:50 +03001663 struct pxafb_info *fbi = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Russell King9480e302005-10-28 09:52:56 -07001665 set_ctrlr_state(fbi, C_ENABLE_PM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 return 0;
1667}
Mike Rapoport4f3edfe2009-07-21 17:51:50 +03001668
1669static struct dev_pm_ops pxafb_pm_ops = {
1670 .suspend = pxafb_suspend,
1671 .resume = pxafb_resume,
1672};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673#endif
1674
Eric Miao77e19672008-12-16 11:54:34 +08001675static int __devinit pxafb_init_video_memory(struct pxafb_info *fbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676{
Eric Miao77e19672008-12-16 11:54:34 +08001677 int size = PAGE_ALIGN(fbi->video_mem_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Eric Miao77e19672008-12-16 11:54:34 +08001679 fbi->video_mem = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
1680 if (fbi->video_mem == NULL)
1681 return -ENOMEM;
Eric Miao3c42a442008-04-30 00:52:26 -07001682
Eric Miao77e19672008-12-16 11:54:34 +08001683 fbi->video_mem_phys = virt_to_phys(fbi->video_mem);
1684 fbi->video_mem_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Eric Miao77e19672008-12-16 11:54:34 +08001686 fbi->fb.fix.smem_start = fbi->video_mem_phys;
1687 fbi->fb.fix.smem_len = fbi->video_mem_size;
1688 fbi->fb.screen_base = fbi->video_mem;
Eric Miao3c42a442008-04-30 00:52:26 -07001689
Eric Miao77e19672008-12-16 11:54:34 +08001690 return fbi->video_mem ? 0 : -ENOMEM;
eric miao84f43c32008-04-30 00:52:22 -07001691}
1692
Guennadi Liakhovetskiebdf9822008-05-05 15:31:44 +01001693static void pxafb_decode_mach_info(struct pxafb_info *fbi,
1694 struct pxafb_mach_info *inf)
eric miao84f43c32008-04-30 00:52:22 -07001695{
1696 unsigned int lcd_conn = inf->lcd_conn;
Eric Miao77e19672008-12-16 11:54:34 +08001697 struct pxafb_mode_info *m;
1698 int i;
eric miao84f43c32008-04-30 00:52:22 -07001699
1700 fbi->cmap_inverse = inf->cmap_inverse;
1701 fbi->cmap_static = inf->cmap_static;
Eric Miaoa0427502008-12-18 22:10:00 +08001702 fbi->lccr4 = inf->lccr4;
eric miao84f43c32008-04-30 00:52:22 -07001703
Eric Miao1ec26db2008-11-11 21:45:57 +08001704 switch (lcd_conn & LCD_TYPE_MASK) {
eric miao84f43c32008-04-30 00:52:22 -07001705 case LCD_TYPE_MONO_STN:
1706 fbi->lccr0 = LCCR0_CMS;
1707 break;
1708 case LCD_TYPE_MONO_DSTN:
1709 fbi->lccr0 = LCCR0_CMS | LCCR0_SDS;
1710 break;
1711 case LCD_TYPE_COLOR_STN:
1712 fbi->lccr0 = 0;
1713 break;
1714 case LCD_TYPE_COLOR_DSTN:
1715 fbi->lccr0 = LCCR0_SDS;
1716 break;
1717 case LCD_TYPE_COLOR_TFT:
1718 fbi->lccr0 = LCCR0_PAS;
1719 break;
1720 case LCD_TYPE_SMART_PANEL:
1721 fbi->lccr0 = LCCR0_LCDT | LCCR0_PAS;
1722 break;
1723 default:
1724 /* fall back to backward compatibility way */
1725 fbi->lccr0 = inf->lccr0;
1726 fbi->lccr3 = inf->lccr3;
Guennadi Liakhovetskiebdf9822008-05-05 15:31:44 +01001727 goto decode_mode;
eric miao84f43c32008-04-30 00:52:22 -07001728 }
1729
1730 if (lcd_conn == LCD_MONO_STN_8BPP)
1731 fbi->lccr0 |= LCCR0_DPD;
1732
Eric Miao9a1ac7e2008-08-15 02:50:44 -04001733 fbi->lccr0 |= (lcd_conn & LCD_ALTERNATE_MAPPING) ? LCCR0_LDDALT : 0;
1734
eric miao84f43c32008-04-30 00:52:22 -07001735 fbi->lccr3 = LCCR3_Acb((inf->lcd_conn >> 10) & 0xff);
1736 fbi->lccr3 |= (lcd_conn & LCD_BIAS_ACTIVE_LOW) ? LCCR3_OEP : 0;
1737 fbi->lccr3 |= (lcd_conn & LCD_PCLK_EDGE_FALL) ? LCCR3_PCP : 0;
1738
Guennadi Liakhovetskiebdf9822008-05-05 15:31:44 +01001739decode_mode:
Eric Miao77e19672008-12-16 11:54:34 +08001740 pxafb_setmode(&fbi->fb.var, &inf->modes[0]);
1741
1742 /* decide video memory size as follows:
1743 * 1. default to mode of maximum resolution
1744 * 2. allow platform to override
1745 * 3. allow module parameter to override
1746 */
1747 for (i = 0, m = &inf->modes[0]; i < inf->num_modes; i++, m++)
1748 fbi->video_mem_size = max_t(size_t, fbi->video_mem_size,
1749 m->xres * m->yres * m->bpp / 8);
1750
1751 if (inf->video_mem_size > fbi->video_mem_size)
1752 fbi->video_mem_size = inf->video_mem_size;
1753
1754 if (video_mem_size > fbi->video_mem_size)
1755 fbi->video_mem_size = video_mem_size;
eric miao84f43c32008-04-30 00:52:22 -07001756}
1757
Jaya Kumar9e6c2972008-06-22 04:27:27 +01001758static struct pxafb_info * __devinit pxafb_init_fbinfo(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
1760 struct pxafb_info *fbi;
1761 void *addr;
1762 struct pxafb_mach_info *inf = dev->platform_data;
1763
1764 /* Alloc the pxafb_info and pseudo_palette in one step */
1765 fbi = kmalloc(sizeof(struct pxafb_info) + sizeof(u32) * 16, GFP_KERNEL);
1766 if (!fbi)
1767 return NULL;
1768
1769 memset(fbi, 0, sizeof(struct pxafb_info));
1770 fbi->dev = dev;
1771
Russell Kinge0d8b132008-11-11 17:52:32 +00001772 fbi->clk = clk_get(dev, NULL);
Russell King72e35242007-08-20 10:18:42 +01001773 if (IS_ERR(fbi->clk)) {
1774 kfree(fbi);
1775 return NULL;
1776 }
1777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 strcpy(fbi->fb.fix.id, PXA_NAME);
1779
1780 fbi->fb.fix.type = FB_TYPE_PACKED_PIXELS;
1781 fbi->fb.fix.type_aux = 0;
1782 fbi->fb.fix.xpanstep = 0;
Eric Miao7e4b19c2008-12-17 14:56:54 +08001783 fbi->fb.fix.ypanstep = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 fbi->fb.fix.ywrapstep = 0;
1785 fbi->fb.fix.accel = FB_ACCEL_NONE;
1786
1787 fbi->fb.var.nonstd = 0;
1788 fbi->fb.var.activate = FB_ACTIVATE_NOW;
1789 fbi->fb.var.height = -1;
1790 fbi->fb.var.width = -1;
Eric Miao7e4b19c2008-12-17 14:56:54 +08001791 fbi->fb.var.accel_flags = FB_ACCELF_TEXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 fbi->fb.var.vmode = FB_VMODE_NONINTERLACED;
1793
1794 fbi->fb.fbops = &pxafb_ops;
1795 fbi->fb.flags = FBINFO_DEFAULT;
1796 fbi->fb.node = -1;
1797
1798 addr = fbi;
1799 addr = addr + sizeof(struct pxafb_info);
1800 fbi->fb.pseudo_palette = addr;
1801
eric miaob0086ef2008-04-30 00:52:19 -07001802 fbi->state = C_STARTUP;
1803 fbi->task_state = (u_char)-1;
Richard Purdied14b2722006-09-20 22:54:21 +01001804
eric miao84f43c32008-04-30 00:52:22 -07001805 pxafb_decode_mach_info(fbi, inf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
1807 init_waitqueue_head(&fbi->ctrlr_wait);
David Howells6d5aefb2006-12-05 19:36:26 +00001808 INIT_WORK(&fbi->task, pxafb_task);
Matthias Kaehlckeb91dbce2008-07-23 21:31:14 -07001809 mutex_init(&fbi->ctrlr_lock);
Eric Miao2ba162b2008-04-30 00:52:24 -07001810 init_completion(&fbi->disable_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
1812 return fbi;
1813}
1814
1815#ifdef CONFIG_FB_PXA_PARAMETERS
Jaya Kumar9e6c2972008-06-22 04:27:27 +01001816static int __devinit parse_opt_mode(struct device *dev, const char *this_opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
1818 struct pxafb_mach_info *inf = dev->platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
eric miao817daf12008-04-30 00:52:18 -07001820 const char *name = this_opt+5;
1821 unsigned int namelen = strlen(name);
1822 int res_specified = 0, bpp_specified = 0;
1823 unsigned int xres = 0, yres = 0, bpp = 0;
1824 int yres_specified = 0;
1825 int i;
1826 for (i = namelen-1; i >= 0; i--) {
1827 switch (name[i]) {
1828 case '-':
1829 namelen = i;
1830 if (!bpp_specified && !yres_specified) {
1831 bpp = simple_strtoul(&name[i+1], NULL, 0);
1832 bpp_specified = 1;
1833 } else
1834 goto done;
1835 break;
1836 case 'x':
1837 if (!yres_specified) {
1838 yres = simple_strtoul(&name[i+1], NULL, 0);
1839 yres_specified = 1;
1840 } else
1841 goto done;
1842 break;
1843 case '0' ... '9':
1844 break;
1845 default:
1846 goto done;
1847 }
1848 }
1849 if (i < 0 && yres_specified) {
1850 xres = simple_strtoul(name, NULL, 0);
1851 res_specified = 1;
1852 }
1853done:
1854 if (res_specified) {
1855 dev_info(dev, "overriding resolution: %dx%d\n", xres, yres);
1856 inf->modes[0].xres = xres; inf->modes[0].yres = yres;
1857 }
1858 if (bpp_specified)
1859 switch (bpp) {
1860 case 1:
1861 case 2:
1862 case 4:
1863 case 8:
1864 case 16:
1865 inf->modes[0].bpp = bpp;
1866 dev_info(dev, "overriding bit depth: %d\n", bpp);
1867 break;
1868 default:
1869 dev_err(dev, "Depth %d is not valid\n", bpp);
1870 return -EINVAL;
1871 }
1872 return 0;
1873}
1874
Jaya Kumar9e6c2972008-06-22 04:27:27 +01001875static int __devinit parse_opt(struct device *dev, char *this_opt)
eric miao817daf12008-04-30 00:52:18 -07001876{
1877 struct pxafb_mach_info *inf = dev->platform_data;
1878 struct pxafb_mode_info *mode = &inf->modes[0];
1879 char s[64];
1880
1881 s[0] = '\0';
1882
Eric Miao77e19672008-12-16 11:54:34 +08001883 if (!strncmp(this_opt, "vmem:", 5)) {
1884 video_mem_size = memparse(this_opt + 5, NULL);
1885 } else if (!strncmp(this_opt, "mode:", 5)) {
eric miao817daf12008-04-30 00:52:18 -07001886 return parse_opt_mode(dev, this_opt);
1887 } else if (!strncmp(this_opt, "pixclock:", 9)) {
1888 mode->pixclock = simple_strtoul(this_opt+9, NULL, 0);
1889 sprintf(s, "pixclock: %ld\n", mode->pixclock);
1890 } else if (!strncmp(this_opt, "left:", 5)) {
1891 mode->left_margin = simple_strtoul(this_opt+5, NULL, 0);
1892 sprintf(s, "left: %u\n", mode->left_margin);
1893 } else if (!strncmp(this_opt, "right:", 6)) {
1894 mode->right_margin = simple_strtoul(this_opt+6, NULL, 0);
1895 sprintf(s, "right: %u\n", mode->right_margin);
1896 } else if (!strncmp(this_opt, "upper:", 6)) {
1897 mode->upper_margin = simple_strtoul(this_opt+6, NULL, 0);
1898 sprintf(s, "upper: %u\n", mode->upper_margin);
1899 } else if (!strncmp(this_opt, "lower:", 6)) {
1900 mode->lower_margin = simple_strtoul(this_opt+6, NULL, 0);
1901 sprintf(s, "lower: %u\n", mode->lower_margin);
1902 } else if (!strncmp(this_opt, "hsynclen:", 9)) {
1903 mode->hsync_len = simple_strtoul(this_opt+9, NULL, 0);
1904 sprintf(s, "hsynclen: %u\n", mode->hsync_len);
1905 } else if (!strncmp(this_opt, "vsynclen:", 9)) {
1906 mode->vsync_len = simple_strtoul(this_opt+9, NULL, 0);
1907 sprintf(s, "vsynclen: %u\n", mode->vsync_len);
1908 } else if (!strncmp(this_opt, "hsync:", 6)) {
1909 if (simple_strtoul(this_opt+6, NULL, 0) == 0) {
1910 sprintf(s, "hsync: Active Low\n");
1911 mode->sync &= ~FB_SYNC_HOR_HIGH_ACT;
1912 } else {
1913 sprintf(s, "hsync: Active High\n");
1914 mode->sync |= FB_SYNC_HOR_HIGH_ACT;
1915 }
1916 } else if (!strncmp(this_opt, "vsync:", 6)) {
1917 if (simple_strtoul(this_opt+6, NULL, 0) == 0) {
1918 sprintf(s, "vsync: Active Low\n");
1919 mode->sync &= ~FB_SYNC_VERT_HIGH_ACT;
1920 } else {
1921 sprintf(s, "vsync: Active High\n");
1922 mode->sync |= FB_SYNC_VERT_HIGH_ACT;
1923 }
1924 } else if (!strncmp(this_opt, "dpc:", 4)) {
1925 if (simple_strtoul(this_opt+4, NULL, 0) == 0) {
1926 sprintf(s, "double pixel clock: false\n");
1927 inf->lccr3 &= ~LCCR3_DPC;
1928 } else {
1929 sprintf(s, "double pixel clock: true\n");
1930 inf->lccr3 |= LCCR3_DPC;
1931 }
1932 } else if (!strncmp(this_opt, "outputen:", 9)) {
1933 if (simple_strtoul(this_opt+9, NULL, 0) == 0) {
1934 sprintf(s, "output enable: active low\n");
1935 inf->lccr3 = (inf->lccr3 & ~LCCR3_OEP) | LCCR3_OutEnL;
1936 } else {
1937 sprintf(s, "output enable: active high\n");
1938 inf->lccr3 = (inf->lccr3 & ~LCCR3_OEP) | LCCR3_OutEnH;
1939 }
1940 } else if (!strncmp(this_opt, "pixclockpol:", 12)) {
1941 if (simple_strtoul(this_opt+12, NULL, 0) == 0) {
1942 sprintf(s, "pixel clock polarity: falling edge\n");
1943 inf->lccr3 = (inf->lccr3 & ~LCCR3_PCP) | LCCR3_PixFlEdg;
1944 } else {
1945 sprintf(s, "pixel clock polarity: rising edge\n");
1946 inf->lccr3 = (inf->lccr3 & ~LCCR3_PCP) | LCCR3_PixRsEdg;
1947 }
1948 } else if (!strncmp(this_opt, "color", 5)) {
1949 inf->lccr0 = (inf->lccr0 & ~LCCR0_CMS) | LCCR0_Color;
1950 } else if (!strncmp(this_opt, "mono", 4)) {
1951 inf->lccr0 = (inf->lccr0 & ~LCCR0_CMS) | LCCR0_Mono;
1952 } else if (!strncmp(this_opt, "active", 6)) {
1953 inf->lccr0 = (inf->lccr0 & ~LCCR0_PAS) | LCCR0_Act;
1954 } else if (!strncmp(this_opt, "passive", 7)) {
1955 inf->lccr0 = (inf->lccr0 & ~LCCR0_PAS) | LCCR0_Pas;
1956 } else if (!strncmp(this_opt, "single", 6)) {
1957 inf->lccr0 = (inf->lccr0 & ~LCCR0_SDS) | LCCR0_Sngl;
1958 } else if (!strncmp(this_opt, "dual", 4)) {
1959 inf->lccr0 = (inf->lccr0 & ~LCCR0_SDS) | LCCR0_Dual;
1960 } else if (!strncmp(this_opt, "4pix", 4)) {
1961 inf->lccr0 = (inf->lccr0 & ~LCCR0_DPD) | LCCR0_4PixMono;
1962 } else if (!strncmp(this_opt, "8pix", 4)) {
1963 inf->lccr0 = (inf->lccr0 & ~LCCR0_DPD) | LCCR0_8PixMono;
1964 } else {
1965 dev_err(dev, "unknown option: %s\n", this_opt);
1966 return -EINVAL;
1967 }
1968
1969 if (s[0] != '\0')
1970 dev_info(dev, "override %s", s);
1971
1972 return 0;
1973}
1974
Jaya Kumar9e6c2972008-06-22 04:27:27 +01001975static int __devinit pxafb_parse_options(struct device *dev, char *options)
eric miao817daf12008-04-30 00:52:18 -07001976{
1977 char *this_opt;
1978 int ret;
1979
1980 if (!options || !*options)
1981 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
1983 dev_dbg(dev, "options are \"%s\"\n", options ? options : "null");
1984
1985 /* could be made table driven or similar?... */
eric miao817daf12008-04-30 00:52:18 -07001986 while ((this_opt = strsep(&options, ",")) != NULL) {
1987 ret = parse_opt(dev, this_opt);
1988 if (ret)
1989 return ret;
1990 }
1991 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992}
eric miao92ac73c2008-04-30 00:52:20 -07001993
1994static char g_options[256] __devinitdata = "";
1995
Jaya Kumarf1edfc42008-06-22 04:27:25 +01001996#ifndef MODULE
Jaya Kumar9e6c2972008-06-22 04:27:27 +01001997static int __init pxafb_setup_options(void)
eric miao92ac73c2008-04-30 00:52:20 -07001998{
1999 char *options = NULL;
2000
2001 if (fb_get_options("pxafb", &options))
2002 return -ENODEV;
2003
2004 if (options)
2005 strlcpy(g_options, options, sizeof(g_options));
2006
2007 return 0;
2008}
2009#else
2010#define pxafb_setup_options() (0)
2011
2012module_param_string(options, g_options, sizeof(g_options), 0);
2013MODULE_PARM_DESC(options, "LCD parameters (see Documentation/fb/pxafb.txt)");
2014#endif
2015
2016#else
2017#define pxafb_parse_options(...) (0)
2018#define pxafb_setup_options() (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019#endif
2020
Eric Miao4f3e2662008-08-16 03:50:51 -04002021#ifdef DEBUG_VAR
2022/* Check for various illegal bit-combinations. Currently only
2023 * a warning is given. */
2024static void __devinit pxafb_check_options(struct device *dev,
2025 struct pxafb_mach_info *inf)
2026{
2027 if (inf->lcd_conn)
2028 return;
2029
2030 if (inf->lccr0 & LCCR0_INVALID_CONFIG_MASK)
2031 dev_warn(dev, "machine LCCR0 setting contains "
2032 "illegal bits: %08x\n",
2033 inf->lccr0 & LCCR0_INVALID_CONFIG_MASK);
2034 if (inf->lccr3 & LCCR3_INVALID_CONFIG_MASK)
2035 dev_warn(dev, "machine LCCR3 setting contains "
2036 "illegal bits: %08x\n",
2037 inf->lccr3 & LCCR3_INVALID_CONFIG_MASK);
2038 if (inf->lccr0 & LCCR0_DPD &&
2039 ((inf->lccr0 & LCCR0_PAS) != LCCR0_Pas ||
2040 (inf->lccr0 & LCCR0_SDS) != LCCR0_Sngl ||
2041 (inf->lccr0 & LCCR0_CMS) != LCCR0_Mono))
2042 dev_warn(dev, "Double Pixel Data (DPD) mode is "
2043 "only valid in passive mono"
2044 " single panel mode\n");
2045 if ((inf->lccr0 & LCCR0_PAS) == LCCR0_Act &&
2046 (inf->lccr0 & LCCR0_SDS) == LCCR0_Dual)
2047 dev_warn(dev, "Dual panel only valid in passive mode\n");
2048 if ((inf->lccr0 & LCCR0_PAS) == LCCR0_Pas &&
2049 (inf->modes->upper_margin || inf->modes->lower_margin))
2050 dev_warn(dev, "Upper and lower margins must be 0 in "
2051 "passive mode\n");
2052}
2053#else
2054#define pxafb_check_options(...) do {} while (0)
2055#endif
2056
Jaya Kumar9e6c2972008-06-22 04:27:27 +01002057static int __devinit pxafb_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
2059 struct pxafb_info *fbi;
2060 struct pxafb_mach_info *inf;
eric miaoce4fb7b2008-04-30 00:52:21 -07002061 struct resource *r;
2062 int irq, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Richard Purdie2cbbb3b2006-03-31 02:31:53 -08002064 dev_dbg(&dev->dev, "pxafb_probe\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Russell King3ae5eae2005-11-09 22:32:44 +00002066 inf = dev->dev.platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 ret = -ENOMEM;
2068 fbi = NULL;
2069 if (!inf)
2070 goto failed;
2071
Russell King3ae5eae2005-11-09 22:32:44 +00002072 ret = pxafb_parse_options(&dev->dev, g_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 if (ret < 0)
2074 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Eric Miao4f3e2662008-08-16 03:50:51 -04002076 pxafb_check_options(&dev->dev, inf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
eric miaob0086ef2008-04-30 00:52:19 -07002078 dev_dbg(&dev->dev, "got a %dx%dx%d LCD\n",
2079 inf->modes->xres,
2080 inf->modes->yres,
2081 inf->modes->bpp);
2082 if (inf->modes->xres == 0 ||
2083 inf->modes->yres == 0 ||
2084 inf->modes->bpp == 0) {
Russell King3ae5eae2005-11-09 22:32:44 +00002085 dev_err(&dev->dev, "Invalid resolution or bit depth\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 ret = -EINVAL;
2087 goto failed;
2088 }
Eric Miaoa5718a12008-11-11 21:50:39 +08002089
Russell King3ae5eae2005-11-09 22:32:44 +00002090 fbi = pxafb_init_fbinfo(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 if (!fbi) {
eric miaob0086ef2008-04-30 00:52:19 -07002092 /* only reason for pxafb_init_fbinfo to fail is kmalloc */
Russell King3ae5eae2005-11-09 22:32:44 +00002093 dev_err(&dev->dev, "Failed to initialize framebuffer device\n");
eric miaob0086ef2008-04-30 00:52:19 -07002094 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 goto failed;
2096 }
2097
Daniel Mack52a7a1c2009-09-10 15:26:30 +02002098 if (cpu_is_pxa3xx() && inf->acceleration_enabled)
2099 fbi->fb.fix.accel = FB_ACCEL_PXA3XX;
2100
Eric Miaoa5718a12008-11-11 21:50:39 +08002101 fbi->backlight_power = inf->pxafb_backlight_power;
2102 fbi->lcd_power = inf->pxafb_lcd_power;
2103
eric miaoce4fb7b2008-04-30 00:52:21 -07002104 r = platform_get_resource(dev, IORESOURCE_MEM, 0);
2105 if (r == NULL) {
2106 dev_err(&dev->dev, "no I/O memory resource defined\n");
2107 ret = -ENODEV;
Jaya Kumaree984762008-06-22 04:27:26 +01002108 goto failed_fbi;
eric miaoce4fb7b2008-04-30 00:52:21 -07002109 }
2110
Daniel Mack53eff412009-06-22 21:08:04 +02002111 r = request_mem_region(r->start, resource_size(r), dev->name);
eric miaoce4fb7b2008-04-30 00:52:21 -07002112 if (r == NULL) {
2113 dev_err(&dev->dev, "failed to request I/O memory\n");
2114 ret = -EBUSY;
Jaya Kumaree984762008-06-22 04:27:26 +01002115 goto failed_fbi;
eric miaoce4fb7b2008-04-30 00:52:21 -07002116 }
2117
Daniel Mack53eff412009-06-22 21:08:04 +02002118 fbi->mmio_base = ioremap(r->start, resource_size(r));
eric miaoce4fb7b2008-04-30 00:52:21 -07002119 if (fbi->mmio_base == NULL) {
2120 dev_err(&dev->dev, "failed to map I/O memory\n");
2121 ret = -EBUSY;
2122 goto failed_free_res;
2123 }
2124
Eric Miao77e19672008-12-16 11:54:34 +08002125 fbi->dma_buff_size = PAGE_ALIGN(sizeof(struct pxafb_dma_buff));
2126 fbi->dma_buff = dma_alloc_coherent(fbi->dev, fbi->dma_buff_size,
2127 &fbi->dma_buff_phys, GFP_KERNEL);
2128 if (fbi->dma_buff == NULL) {
2129 dev_err(&dev->dev, "failed to allocate memory for DMA\n");
2130 ret = -ENOMEM;
2131 goto failed_free_io;
2132 }
2133
2134 ret = pxafb_init_video_memory(fbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 if (ret) {
Russell King3ae5eae2005-11-09 22:32:44 +00002136 dev_err(&dev->dev, "Failed to allocate video RAM: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 ret = -ENOMEM;
Eric Miao77e19672008-12-16 11:54:34 +08002138 goto failed_free_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
eric miaoce4fb7b2008-04-30 00:52:21 -07002141 irq = platform_get_irq(dev, 0);
2142 if (irq < 0) {
2143 dev_err(&dev->dev, "no IRQ defined\n");
2144 ret = -ENODEV;
2145 goto failed_free_mem;
2146 }
2147
2148 ret = request_irq(irq, pxafb_handle_irq, IRQF_DISABLED, "LCD", fbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 if (ret) {
Russell King3ae5eae2005-11-09 22:32:44 +00002150 dev_err(&dev->dev, "request_irq failed: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 ret = -EBUSY;
eric miaoce4fb7b2008-04-30 00:52:21 -07002152 goto failed_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 }
2154
Eric Miao3c42a442008-04-30 00:52:26 -07002155 ret = pxafb_smart_init(fbi);
2156 if (ret) {
2157 dev_err(&dev->dev, "failed to initialize smartpanel\n");
2158 goto failed_free_irq;
2159 }
Eric Miao07df1c42008-12-04 11:14:11 +08002160
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 /*
2162 * This makes sure that our colour bitfield
2163 * descriptors are correctly initialised.
2164 */
Jaya Kumaree984762008-06-22 04:27:26 +01002165 ret = pxafb_check_var(&fbi->fb.var, &fbi->fb);
2166 if (ret) {
2167 dev_err(&dev->dev, "failed to get suitable mode\n");
2168 goto failed_free_irq;
2169 }
2170
2171 ret = pxafb_set_par(&fbi->fb);
2172 if (ret) {
2173 dev_err(&dev->dev, "Failed to set parameters\n");
2174 goto failed_free_irq;
2175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
Russell King3ae5eae2005-11-09 22:32:44 +00002177 platform_set_drvdata(dev, fbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
2179 ret = register_framebuffer(&fbi->fb);
2180 if (ret < 0) {
eric miaob0086ef2008-04-30 00:52:19 -07002181 dev_err(&dev->dev,
2182 "Failed to register framebuffer device: %d\n", ret);
Jaya Kumaree984762008-06-22 04:27:26 +01002183 goto failed_free_cmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 }
2185
Eric Miao198fc102008-12-23 17:49:43 +08002186 pxafb_overlay_init(fbi);
2187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188#ifdef CONFIG_CPU_FREQ
2189 fbi->freq_transition.notifier_call = pxafb_freq_transition;
2190 fbi->freq_policy.notifier_call = pxafb_freq_policy;
eric miaob0086ef2008-04-30 00:52:19 -07002191 cpufreq_register_notifier(&fbi->freq_transition,
2192 CPUFREQ_TRANSITION_NOTIFIER);
2193 cpufreq_register_notifier(&fbi->freq_policy,
2194 CPUFREQ_POLICY_NOTIFIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195#endif
2196
2197 /*
2198 * Ok, now enable the LCD controller
2199 */
2200 set_ctrlr_state(fbi, C_ENABLE);
2201
2202 return 0;
2203
Jaya Kumaree984762008-06-22 04:27:26 +01002204failed_free_cmap:
2205 if (fbi->fb.cmap.len)
2206 fb_dealloc_cmap(&fbi->fb.cmap);
eric miaoce4fb7b2008-04-30 00:52:21 -07002207failed_free_irq:
2208 free_irq(irq, fbi);
eric miaoce4fb7b2008-04-30 00:52:21 -07002209failed_free_mem:
Eric Miao77e19672008-12-16 11:54:34 +08002210 free_pages_exact(fbi->video_mem, fbi->video_mem_size);
2211failed_free_dma:
2212 dma_free_coherent(&dev->dev, fbi->dma_buff_size,
2213 fbi->dma_buff, fbi->dma_buff_phys);
Jaya Kumaree984762008-06-22 04:27:26 +01002214failed_free_io:
2215 iounmap(fbi->mmio_base);
2216failed_free_res:
Daniel Mack53eff412009-06-22 21:08:04 +02002217 release_mem_region(r->start, resource_size(r));
Jaya Kumaree984762008-06-22 04:27:26 +01002218failed_fbi:
2219 clk_put(fbi->clk);
Russell King3ae5eae2005-11-09 22:32:44 +00002220 platform_set_drvdata(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 kfree(fbi);
Jaya Kumaree984762008-06-22 04:27:26 +01002222failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 return ret;
2224}
2225
Jaya Kumar9f17f282008-06-22 04:27:28 +01002226static int __devexit pxafb_remove(struct platform_device *dev)
2227{
2228 struct pxafb_info *fbi = platform_get_drvdata(dev);
2229 struct resource *r;
2230 int irq;
2231 struct fb_info *info;
2232
2233 if (!fbi)
2234 return 0;
2235
2236 info = &fbi->fb;
2237
Eric Miao198fc102008-12-23 17:49:43 +08002238 pxafb_overlay_exit(fbi);
Jaya Kumar9f17f282008-06-22 04:27:28 +01002239 unregister_framebuffer(info);
2240
2241 pxafb_disable_controller(fbi);
2242
2243 if (fbi->fb.cmap.len)
2244 fb_dealloc_cmap(&fbi->fb.cmap);
2245
2246 irq = platform_get_irq(dev, 0);
2247 free_irq(irq, fbi);
2248
Eric Miao77e19672008-12-16 11:54:34 +08002249 free_pages_exact(fbi->video_mem, fbi->video_mem_size);
2250
2251 dma_free_writecombine(&dev->dev, fbi->dma_buff_size,
2252 fbi->dma_buff, fbi->dma_buff_phys);
Jaya Kumar9f17f282008-06-22 04:27:28 +01002253
2254 iounmap(fbi->mmio_base);
2255
2256 r = platform_get_resource(dev, IORESOURCE_MEM, 0);
Daniel Mack53eff412009-06-22 21:08:04 +02002257 release_mem_region(r->start, resource_size(r));
Jaya Kumar9f17f282008-06-22 04:27:28 +01002258
2259 clk_put(fbi->clk);
2260 kfree(fbi);
2261
2262 return 0;
2263}
2264
Russell King3ae5eae2005-11-09 22:32:44 +00002265static struct platform_driver pxafb_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 .probe = pxafb_probe,
Russell Kingbdf602b2009-03-03 13:43:47 +00002267 .remove = __devexit_p(pxafb_remove),
Russell King3ae5eae2005-11-09 22:32:44 +00002268 .driver = {
Jaya Kumar9f17f282008-06-22 04:27:28 +01002269 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00002270 .name = "pxa2xx-fb",
Mike Rapoport4f3edfe2009-07-21 17:51:50 +03002271#ifdef CONFIG_PM
2272 .pm = &pxafb_pm_ops,
2273#endif
Russell King3ae5eae2005-11-09 22:32:44 +00002274 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275};
2276
Jaya Kumar9e6c2972008-06-22 04:27:27 +01002277static int __init pxafb_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278{
eric miao92ac73c2008-04-30 00:52:20 -07002279 if (pxafb_setup_options())
2280 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
Russell King3ae5eae2005-11-09 22:32:44 +00002282 return platform_driver_register(&pxafb_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283}
2284
Jaya Kumar9f17f282008-06-22 04:27:28 +01002285static void __exit pxafb_exit(void)
2286{
2287 platform_driver_unregister(&pxafb_driver);
2288}
2289
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290module_init(pxafb_init);
Jaya Kumar9f17f282008-06-22 04:27:28 +01002291module_exit(pxafb_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
2293MODULE_DESCRIPTION("loadable framebuffer driver for PXA");
2294MODULE_LICENSE("GPL");