blob: 0889d50c3288e127e97fe604bcdb4fa2d52b6224 [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);
Eric Miao6e354842008-12-17 16:50:43 +080083static void setup_base_frame(struct pxafb_info *fbi, int branch);
Eric Miao198fc102008-12-23 17:49:43 +080084static int setup_frame_dma(struct pxafb_info *fbi, int dma, int pal,
85 unsigned long offset, size_t size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Eric Miao77e19672008-12-16 11:54:34 +080087static unsigned long video_mem_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Eric Miaoa7535ba2008-04-30 00:52:24 -070089static inline unsigned long
90lcd_readl(struct pxafb_info *fbi, unsigned int off)
91{
92 return __raw_readl(fbi->mmio_base + off);
93}
94
95static inline void
96lcd_writel(struct pxafb_info *fbi, unsigned int off, unsigned long val)
97{
98 __raw_writel(val, fbi->mmio_base + off);
99}
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101static inline void pxafb_schedule_work(struct pxafb_info *fbi, u_int state)
102{
103 unsigned long flags;
104
105 local_irq_save(flags);
106 /*
107 * We need to handle two requests being made at the same time.
108 * There are two important cases:
eric miaob0086ef2008-04-30 00:52:19 -0700109 * 1. When we are changing VT (C_REENABLE) while unblanking
110 * (C_ENABLE) We must perform the unblanking, which will
111 * do our REENABLE for us.
112 * 2. When we are blanking, but immediately unblank before
113 * we have blanked. We do the "REENABLE" thing here as
114 * well, just to be sure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 */
116 if (fbi->task_state == C_ENABLE && state == C_REENABLE)
117 state = (u_int) -1;
118 if (fbi->task_state == C_DISABLE && state == C_ENABLE)
119 state = C_REENABLE;
120
121 if (state != (u_int)-1) {
122 fbi->task_state = state;
123 schedule_work(&fbi->task);
124 }
125 local_irq_restore(flags);
126}
127
128static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
129{
130 chan &= 0xffff;
131 chan >>= 16 - bf->length;
132 return chan << bf->offset;
133}
134
135static int
136pxafb_setpalettereg(u_int regno, u_int red, u_int green, u_int blue,
137 u_int trans, struct fb_info *info)
138{
139 struct pxafb_info *fbi = (struct pxafb_info *)info;
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700140 u_int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700142 if (regno >= fbi->palette_size)
143 return 1;
144
145 if (fbi->fb.var.grayscale) {
146 fbi->palette_cpu[regno] = ((blue >> 8) & 0x00ff);
147 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 }
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700149
150 switch (fbi->lccr4 & LCCR4_PAL_FOR_MASK) {
151 case LCCR4_PAL_FOR_0:
152 val = ((red >> 0) & 0xf800);
153 val |= ((green >> 5) & 0x07e0);
154 val |= ((blue >> 11) & 0x001f);
155 fbi->palette_cpu[regno] = val;
156 break;
157 case LCCR4_PAL_FOR_1:
158 val = ((red << 8) & 0x00f80000);
159 val |= ((green >> 0) & 0x0000fc00);
160 val |= ((blue >> 8) & 0x000000f8);
eric miaob0086ef2008-04-30 00:52:19 -0700161 ((u32 *)(fbi->palette_cpu))[regno] = val;
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700162 break;
163 case LCCR4_PAL_FOR_2:
164 val = ((red << 8) & 0x00fc0000);
165 val |= ((green >> 0) & 0x0000fc00);
166 val |= ((blue >> 8) & 0x000000fc);
eric miaob0086ef2008-04-30 00:52:19 -0700167 ((u32 *)(fbi->palette_cpu))[regno] = val;
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700168 break;
Eric Miaoa0427502008-12-18 22:10:00 +0800169 case LCCR4_PAL_FOR_3:
170 val = ((red << 8) & 0x00ff0000);
171 val |= ((green >> 0) & 0x0000ff00);
172 val |= ((blue >> 8) & 0x000000ff);
173 ((u32 *)(fbi->palette_cpu))[regno] = val;
174 break;
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700175 }
176
177 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
180static int
181pxafb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
182 u_int trans, struct fb_info *info)
183{
184 struct pxafb_info *fbi = (struct pxafb_info *)info;
185 unsigned int val;
186 int ret = 1;
187
188 /*
189 * If inverse mode was selected, invert all the colours
190 * rather than the register number. The register number
191 * is what you poke into the framebuffer to produce the
192 * colour you requested.
193 */
194 if (fbi->cmap_inverse) {
195 red = 0xffff - red;
196 green = 0xffff - green;
197 blue = 0xffff - blue;
198 }
199
200 /*
201 * If greyscale is true, then we convert the RGB value
202 * to greyscale no matter what visual we are using.
203 */
204 if (fbi->fb.var.grayscale)
205 red = green = blue = (19595 * red + 38470 * green +
206 7471 * blue) >> 16;
207
208 switch (fbi->fb.fix.visual) {
209 case FB_VISUAL_TRUECOLOR:
210 /*
211 * 16-bit True Colour. We encode the RGB value
212 * according to the RGB bitfield information.
213 */
214 if (regno < 16) {
215 u32 *pal = fbi->fb.pseudo_palette;
216
217 val = chan_to_field(red, &fbi->fb.var.red);
218 val |= chan_to_field(green, &fbi->fb.var.green);
219 val |= chan_to_field(blue, &fbi->fb.var.blue);
220
221 pal[regno] = val;
222 ret = 0;
223 }
224 break;
225
226 case FB_VISUAL_STATIC_PSEUDOCOLOR:
227 case FB_VISUAL_PSEUDOCOLOR:
228 ret = pxafb_setpalettereg(regno, red, green, blue, trans, info);
229 break;
230 }
231
232 return ret;
233}
234
Eric Miao878f5782008-12-18 22:36:26 +0800235/* calculate pixel depth, transparency bit included, >=16bpp formats _only_ */
236static inline int var_to_depth(struct fb_var_screeninfo *var)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Eric Miao878f5782008-12-18 22:36:26 +0800238 return var->red.length + var->green.length +
239 var->blue.length + var->transp.length;
240}
241
242/* calculate 4-bit BPP value for LCCR3 and OVLxC1 */
243static int pxafb_var_to_bpp(struct fb_var_screeninfo *var)
244{
245 int bpp = -EINVAL;
246
eric miaob0086ef2008-04-30 00:52:19 -0700247 switch (var->bits_per_pixel) {
Eric Miao878f5782008-12-18 22:36:26 +0800248 case 1: bpp = 0; break;
249 case 2: bpp = 1; break;
250 case 4: bpp = 2; break;
251 case 8: bpp = 3; break;
252 case 16: bpp = 4; break;
Stefan Schmidtc1450f12008-07-09 08:06:32 +0100253 case 24:
Eric Miao878f5782008-12-18 22:36:26 +0800254 switch (var_to_depth(var)) {
255 case 18: bpp = 6; break; /* 18-bits/pixel packed */
256 case 19: bpp = 8; break; /* 19-bits/pixel packed */
257 case 24: bpp = 9; break;
Stefan Schmidtc1450f12008-07-09 08:06:32 +0100258 }
259 break;
260 case 32:
Eric Miao878f5782008-12-18 22:36:26 +0800261 switch (var_to_depth(var)) {
262 case 18: bpp = 5; break; /* 18-bits/pixel unpacked */
263 case 19: bpp = 7; break; /* 19-bits/pixel unpacked */
264 case 25: bpp = 10; break;
Stefan Schmidtc1450f12008-07-09 08:06:32 +0100265 }
266 break;
eric miaob0086ef2008-04-30 00:52:19 -0700267 }
Eric Miao878f5782008-12-18 22:36:26 +0800268 return bpp;
269}
270
271/*
272 * pxafb_var_to_lccr3():
273 * Convert a bits per pixel value to the correct bit pattern for LCCR3
274 *
275 * NOTE: for PXA27x with overlays support, the LCCR3_PDFOR_x bits have an
276 * implication of the acutal use of transparency bit, which we handle it
277 * here separatedly. See PXA27x Developer's Manual, Section <<7.4.6 Pixel
278 * Formats>> for the valid combination of PDFOR, PAL_FOR for various BPP.
279 *
280 * Transparency for palette pixel formats is not supported at the moment.
281 */
282static uint32_t pxafb_var_to_lccr3(struct fb_var_screeninfo *var)
283{
284 int bpp = pxafb_var_to_bpp(var);
285 uint32_t lccr3;
286
287 if (bpp < 0)
288 return 0;
289
290 lccr3 = LCCR3_BPP(bpp);
291
292 switch (var_to_depth(var)) {
293 case 16: lccr3 |= var->transp.length ? LCCR3_PDFOR_3 : 0; break;
294 case 18: lccr3 |= LCCR3_PDFOR_3; break;
295 case 24: lccr3 |= var->transp.length ? LCCR3_PDFOR_2 : LCCR3_PDFOR_3;
296 break;
297 case 19:
298 case 25: lccr3 |= LCCR3_PDFOR_0; break;
299 }
300 return lccr3;
301}
302
303#define SET_PIXFMT(v, r, g, b, t) \
304({ \
305 (v)->transp.offset = (t) ? (r) + (g) + (b) : 0; \
306 (v)->transp.length = (t) ? (t) : 0; \
307 (v)->blue.length = (b); (v)->blue.offset = 0; \
308 (v)->green.length = (g); (v)->green.offset = (b); \
309 (v)->red.length = (r); (v)->red.offset = (b) + (g); \
310})
311
312/* set the RGBT bitfields of fb_var_screeninf according to
313 * var->bits_per_pixel and given depth
314 */
315static void pxafb_set_pixfmt(struct fb_var_screeninfo *var, int depth)
316{
317 if (depth == 0)
318 depth = var->bits_per_pixel;
319
320 if (var->bits_per_pixel < 16) {
321 /* indexed pixel formats */
322 var->red.offset = 0; var->red.length = 8;
323 var->green.offset = 0; var->green.length = 8;
324 var->blue.offset = 0; var->blue.length = 8;
325 var->transp.offset = 0; var->transp.length = 8;
326 }
327
328 switch (depth) {
329 case 16: var->transp.length ?
330 SET_PIXFMT(var, 5, 5, 5, 1) : /* RGBT555 */
331 SET_PIXFMT(var, 5, 6, 5, 0); break; /* RGB565 */
332 case 18: SET_PIXFMT(var, 6, 6, 6, 0); break; /* RGB666 */
333 case 19: SET_PIXFMT(var, 6, 6, 6, 1); break; /* RGBT666 */
334 case 24: var->transp.length ?
335 SET_PIXFMT(var, 8, 8, 7, 1) : /* RGBT887 */
336 SET_PIXFMT(var, 8, 8, 8, 0); break; /* RGB888 */
337 case 25: SET_PIXFMT(var, 8, 8, 8, 1); break; /* RGBT888 */
338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
341#ifdef CONFIG_CPU_FREQ
342/*
343 * pxafb_display_dma_period()
344 * Calculate the minimum period (in picoseconds) between two DMA
345 * requests for the LCD controller. If we hit this, it means we're
346 * doing nothing but LCD DMA.
347 */
348static unsigned int pxafb_display_dma_period(struct fb_var_screeninfo *var)
349{
eric miaob0086ef2008-04-30 00:52:19 -0700350 /*
351 * Period = pixclock * bits_per_byte * bytes_per_transfer
352 * / memory_bits_per_pixel;
353 */
354 return var->pixclock * 8 * 16 / var->bits_per_pixel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356#endif
357
358/*
Richard Purdied14b2722006-09-20 22:54:21 +0100359 * Select the smallest mode that allows the desired resolution to be
360 * displayed. If desired parameters can be rounded up.
361 */
eric miaob0086ef2008-04-30 00:52:19 -0700362static struct pxafb_mode_info *pxafb_getmode(struct pxafb_mach_info *mach,
363 struct fb_var_screeninfo *var)
Richard Purdied14b2722006-09-20 22:54:21 +0100364{
365 struct pxafb_mode_info *mode = NULL;
366 struct pxafb_mode_info *modelist = mach->modes;
367 unsigned int best_x = 0xffffffff, best_y = 0xffffffff;
368 unsigned int i;
369
eric miaob0086ef2008-04-30 00:52:19 -0700370 for (i = 0; i < mach->num_modes; i++) {
371 if (modelist[i].xres >= var->xres &&
372 modelist[i].yres >= var->yres &&
373 modelist[i].xres < best_x &&
374 modelist[i].yres < best_y &&
375 modelist[i].bpp >= var->bits_per_pixel) {
Richard Purdied14b2722006-09-20 22:54:21 +0100376 best_x = modelist[i].xres;
377 best_y = modelist[i].yres;
378 mode = &modelist[i];
379 }
380 }
381
382 return mode;
383}
384
eric miaob0086ef2008-04-30 00:52:19 -0700385static void pxafb_setmode(struct fb_var_screeninfo *var,
386 struct pxafb_mode_info *mode)
Richard Purdied14b2722006-09-20 22:54:21 +0100387{
388 var->xres = mode->xres;
389 var->yres = mode->yres;
390 var->bits_per_pixel = mode->bpp;
391 var->pixclock = mode->pixclock;
392 var->hsync_len = mode->hsync_len;
393 var->left_margin = mode->left_margin;
394 var->right_margin = mode->right_margin;
395 var->vsync_len = mode->vsync_len;
396 var->upper_margin = mode->upper_margin;
397 var->lower_margin = mode->lower_margin;
398 var->sync = mode->sync;
399 var->grayscale = mode->cmap_greyscale;
Eric Miao878f5782008-12-18 22:36:26 +0800400
401 /* set the initial RGBA bitfields */
402 pxafb_set_pixfmt(var, mode->depth);
Richard Purdied14b2722006-09-20 22:54:21 +0100403}
404
Eric Miao3f16ff62008-12-18 22:51:54 +0800405static int pxafb_adjust_timing(struct pxafb_info *fbi,
406 struct fb_var_screeninfo *var)
407{
408 int line_length;
409
410 var->xres = max_t(int, var->xres, MIN_XRES);
411 var->yres = max_t(int, var->yres, MIN_YRES);
412
413 if (!(fbi->lccr0 & LCCR0_LCDT)) {
414 clamp_val(var->hsync_len, 1, 64);
415 clamp_val(var->vsync_len, 1, 64);
416 clamp_val(var->left_margin, 1, 255);
417 clamp_val(var->right_margin, 1, 255);
418 clamp_val(var->upper_margin, 1, 255);
419 clamp_val(var->lower_margin, 1, 255);
420 }
421
422 /* make sure each line is aligned on word boundary */
423 line_length = var->xres * var->bits_per_pixel / 8;
424 line_length = ALIGN(line_length, 4);
425 var->xres = line_length * 8 / var->bits_per_pixel;
426
427 /* we don't support xpan, force xres_virtual to be equal to xres */
428 var->xres_virtual = var->xres;
429
430 if (var->accel_flags & FB_ACCELF_TEXT)
431 var->yres_virtual = fbi->fb.fix.smem_len / line_length;
432 else
433 var->yres_virtual = max(var->yres_virtual, var->yres);
434
435 /* check for limits */
436 if (var->xres > MAX_XRES || var->yres > MAX_YRES)
437 return -EINVAL;
438
439 if (var->yres > var->yres_virtual)
440 return -EINVAL;
441
442 return 0;
Richard Purdied14b2722006-09-20 22:54:21 +0100443}
444
445/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 * pxafb_check_var():
447 * Get the video params out of 'var'. If a value doesn't fit, round it up,
448 * if it's too big, return -EINVAL.
449 *
450 * Round up in the following order: bits_per_pixel, xres,
451 * yres, xres_virtual, yres_virtual, xoffset, yoffset, grayscale,
452 * bitfields, horizontal timing, vertical timing.
453 */
454static int pxafb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
455{
456 struct pxafb_info *fbi = (struct pxafb_info *)info;
Richard Purdied14b2722006-09-20 22:54:21 +0100457 struct pxafb_mach_info *inf = fbi->dev->platform_data;
Eric Miao878f5782008-12-18 22:36:26 +0800458 int err;
Richard Purdied14b2722006-09-20 22:54:21 +0100459
460 if (inf->fixed_modes) {
461 struct pxafb_mode_info *mode;
462
463 mode = pxafb_getmode(inf, var);
464 if (!mode)
465 return -EINVAL;
466 pxafb_setmode(var, mode);
Richard Purdied14b2722006-09-20 22:54:21 +0100467 }
468
Eric Miao878f5782008-12-18 22:36:26 +0800469 /* do a test conversion to BPP fields to check the color formats */
470 err = pxafb_var_to_bpp(var);
471 if (err < 0)
472 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Eric Miao878f5782008-12-18 22:36:26 +0800474 pxafb_set_pixfmt(var, var_to_depth(var));
Stefan Schmidtc1450f12008-07-09 08:06:32 +0100475
Eric Miao3f16ff62008-12-18 22:51:54 +0800476 err = pxafb_adjust_timing(fbi, var);
477 if (err)
478 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480#ifdef CONFIG_CPU_FREQ
Russell King78d3cfd2008-05-17 22:51:14 +0100481 pr_debug("pxafb: dma period = %d ps\n",
482 pxafb_display_dma_period(var));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483#endif
484
485 return 0;
486}
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488/*
489 * pxafb_set_par():
490 * Set the user defined part of the display for the specified console
491 */
492static int pxafb_set_par(struct fb_info *info)
493{
494 struct pxafb_info *fbi = (struct pxafb_info *)info;
495 struct fb_var_screeninfo *var = &info->var;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Stefan Schmidtc1450f12008-07-09 08:06:32 +0100497 if (var->bits_per_pixel >= 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 fbi->fb.fix.visual = FB_VISUAL_TRUECOLOR;
499 else if (!fbi->cmap_static)
500 fbi->fb.fix.visual = FB_VISUAL_PSEUDOCOLOR;
501 else {
502 /*
503 * Some people have weird ideas about wanting static
504 * pseudocolor maps. I suspect their user space
505 * applications are broken.
506 */
507 fbi->fb.fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
508 }
509
510 fbi->fb.fix.line_length = var->xres_virtual *
511 var->bits_per_pixel / 8;
Stefan Schmidtc1450f12008-07-09 08:06:32 +0100512 if (var->bits_per_pixel >= 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 fbi->palette_size = 0;
514 else
eric miaob0086ef2008-04-30 00:52:19 -0700515 fbi->palette_size = var->bits_per_pixel == 1 ?
516 4 : 1 << var->bits_per_pixel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
eric miao2c42dd82008-04-30 00:52:21 -0700518 fbi->palette_cpu = (u16 *)&fbi->dma_buff->palette[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Stefan Schmidtc1450f12008-07-09 08:06:32 +0100520 if (fbi->fb.var.bits_per_pixel >= 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 fb_dealloc_cmap(&fbi->fb.cmap);
522 else
523 fb_alloc_cmap(&fbi->fb.cmap, 1<<fbi->fb.var.bits_per_pixel, 0);
524
525 pxafb_activate_var(var, fbi);
526
527 return 0;
528}
529
Eric Miao6e354842008-12-17 16:50:43 +0800530static int pxafb_pan_display(struct fb_var_screeninfo *var,
531 struct fb_info *info)
532{
533 struct pxafb_info *fbi = (struct pxafb_info *)info;
534 int dma = DMA_MAX + DMA_BASE;
535
536 if (fbi->state != C_ENABLE)
537 return 0;
538
539 setup_base_frame(fbi, 1);
540
541 if (fbi->lccr0 & LCCR0_SDS)
542 lcd_writel(fbi, FBR1, fbi->fdadr[dma + 1] | 0x1);
543
544 lcd_writel(fbi, FBR0, fbi->fdadr[dma] | 0x1);
545 return 0;
546}
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 * pxafb_blank():
550 * Blank the display by setting all palette values to zero. Note, the
551 * 16 bpp mode does not really use the palette, so this will not
552 * blank the display in all modes.
553 */
554static int pxafb_blank(int blank, struct fb_info *info)
555{
556 struct pxafb_info *fbi = (struct pxafb_info *)info;
557 int i;
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 switch (blank) {
560 case FB_BLANK_POWERDOWN:
561 case FB_BLANK_VSYNC_SUSPEND:
562 case FB_BLANK_HSYNC_SUSPEND:
563 case FB_BLANK_NORMAL:
564 if (fbi->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR ||
565 fbi->fb.fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
566 for (i = 0; i < fbi->palette_size; i++)
567 pxafb_setpalettereg(i, 0, 0, 0, 0, info);
568
569 pxafb_schedule_work(fbi, C_DISABLE);
eric miaob0086ef2008-04-30 00:52:19 -0700570 /* TODO if (pxafb_blank_helper) pxafb_blank_helper(blank); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 break;
572
573 case FB_BLANK_UNBLANK:
eric miaob0086ef2008-04-30 00:52:19 -0700574 /* TODO if (pxafb_blank_helper) pxafb_blank_helper(blank); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (fbi->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR ||
576 fbi->fb.fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
577 fb_set_cmap(&fbi->fb.cmap, info);
578 pxafb_schedule_work(fbi, C_ENABLE);
579 }
580 return 0;
581}
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583static struct fb_ops pxafb_ops = {
584 .owner = THIS_MODULE,
585 .fb_check_var = pxafb_check_var,
586 .fb_set_par = pxafb_set_par,
Eric Miao6e354842008-12-17 16:50:43 +0800587 .fb_pan_display = pxafb_pan_display,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 .fb_setcolreg = pxafb_setcolreg,
589 .fb_fillrect = cfb_fillrect,
590 .fb_copyarea = cfb_copyarea,
591 .fb_imageblit = cfb_imageblit,
592 .fb_blank = pxafb_blank,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593};
594
Eric Miao198fc102008-12-23 17:49:43 +0800595#ifdef CONFIG_FB_PXA_OVERLAY
596static void overlay1fb_setup(struct pxafb_layer *ofb)
597{
598 int size = ofb->fb.fix.line_length * ofb->fb.var.yres_virtual;
599 unsigned long start = ofb->video_mem_phys;
600 setup_frame_dma(ofb->fbi, DMA_OV1, PAL_NONE, start, size);
601}
602
603/* Depending on the enable status of overlay1/2, the DMA should be
604 * updated from FDADRx (when disabled) or FBRx (when enabled).
605 */
606static void overlay1fb_enable(struct pxafb_layer *ofb)
607{
608 int enabled = lcd_readl(ofb->fbi, OVL1C1) & OVLxC1_OEN;
609 uint32_t fdadr1 = ofb->fbi->fdadr[DMA_OV1] | (enabled ? 0x1 : 0);
610
611 lcd_writel(ofb->fbi, enabled ? FBR1 : FDADR1, fdadr1);
612 lcd_writel(ofb->fbi, OVL1C2, ofb->control[1]);
613 lcd_writel(ofb->fbi, OVL1C1, ofb->control[0] | OVLxC1_OEN);
614}
615
616static void overlay1fb_disable(struct pxafb_layer *ofb)
617{
618 uint32_t lccr5 = lcd_readl(ofb->fbi, LCCR5);
619
620 lcd_writel(ofb->fbi, OVL1C1, ofb->control[0] & ~OVLxC1_OEN);
621
622 lcd_writel(ofb->fbi, LCSR1, LCSR1_BS(1));
623 lcd_writel(ofb->fbi, LCCR5, lccr5 & ~LCSR1_BS(1));
624 lcd_writel(ofb->fbi, FBR1, ofb->fbi->fdadr[DMA_OV1] | 0x3);
625
626 if (wait_for_completion_timeout(&ofb->branch_done, 1 * HZ) == 0)
627 pr_warning("%s: timeout disabling overlay1\n", __func__);
628
629 lcd_writel(ofb->fbi, LCCR5, lccr5);
630}
631
632static void overlay2fb_setup(struct pxafb_layer *ofb)
633{
634 int size, div = 1, pfor = NONSTD_TO_PFOR(ofb->fb.var.nonstd);
635 unsigned long start[3] = { ofb->video_mem_phys, 0, 0 };
636
637 if (pfor == OVERLAY_FORMAT_RGB || pfor == OVERLAY_FORMAT_YUV444_PACKED) {
638 size = ofb->fb.fix.line_length * ofb->fb.var.yres_virtual;
639 setup_frame_dma(ofb->fbi, DMA_OV2_Y, -1, start[0], size);
640 } else {
641 size = ofb->fb.var.xres_virtual * ofb->fb.var.yres_virtual;
642 switch (pfor) {
643 case OVERLAY_FORMAT_YUV444_PLANAR: div = 1; break;
644 case OVERLAY_FORMAT_YUV422_PLANAR: div = 2; break;
645 case OVERLAY_FORMAT_YUV420_PLANAR: div = 4; break;
646 }
647 start[1] = start[0] + size;
648 start[2] = start[1] + size / div;
649 setup_frame_dma(ofb->fbi, DMA_OV2_Y, -1, start[0], size);
650 setup_frame_dma(ofb->fbi, DMA_OV2_Cb, -1, start[1], size / div);
651 setup_frame_dma(ofb->fbi, DMA_OV2_Cr, -1, start[2], size / div);
652 }
653}
654
655static void overlay2fb_enable(struct pxafb_layer *ofb)
656{
657 int pfor = NONSTD_TO_PFOR(ofb->fb.var.nonstd);
658 int enabled = lcd_readl(ofb->fbi, OVL2C1) & OVLxC1_OEN;
659 uint32_t fdadr2 = ofb->fbi->fdadr[DMA_OV2_Y] | (enabled ? 0x1 : 0);
660 uint32_t fdadr3 = ofb->fbi->fdadr[DMA_OV2_Cb] | (enabled ? 0x1 : 0);
661 uint32_t fdadr4 = ofb->fbi->fdadr[DMA_OV2_Cr] | (enabled ? 0x1 : 0);
662
663 if (pfor == OVERLAY_FORMAT_RGB || pfor == OVERLAY_FORMAT_YUV444_PACKED)
664 lcd_writel(ofb->fbi, enabled ? FBR2 : FDADR2, fdadr2);
665 else {
666 lcd_writel(ofb->fbi, enabled ? FBR2 : FDADR2, fdadr2);
667 lcd_writel(ofb->fbi, enabled ? FBR3 : FDADR3, fdadr3);
668 lcd_writel(ofb->fbi, enabled ? FBR4 : FDADR4, fdadr4);
669 }
670 lcd_writel(ofb->fbi, OVL2C2, ofb->control[1]);
671 lcd_writel(ofb->fbi, OVL2C1, ofb->control[0] | OVLxC1_OEN);
672}
673
674static void overlay2fb_disable(struct pxafb_layer *ofb)
675{
676 uint32_t lccr5 = lcd_readl(ofb->fbi, LCCR5);
677
678 lcd_writel(ofb->fbi, OVL2C1, ofb->control[0] & ~OVLxC1_OEN);
679
680 lcd_writel(ofb->fbi, LCSR1, LCSR1_BS(2));
681 lcd_writel(ofb->fbi, LCCR5, lccr5 & ~LCSR1_BS(2));
682 lcd_writel(ofb->fbi, FBR2, ofb->fbi->fdadr[DMA_OV2_Y] | 0x3);
683 lcd_writel(ofb->fbi, FBR3, ofb->fbi->fdadr[DMA_OV2_Cb] | 0x3);
684 lcd_writel(ofb->fbi, FBR4, ofb->fbi->fdadr[DMA_OV2_Cr] | 0x3);
685
686 if (wait_for_completion_timeout(&ofb->branch_done, 1 * HZ) == 0)
687 pr_warning("%s: timeout disabling overlay2\n", __func__);
688}
689
690static struct pxafb_layer_ops ofb_ops[] = {
691 [0] = {
692 .enable = overlay1fb_enable,
693 .disable = overlay1fb_disable,
694 .setup = overlay1fb_setup,
695 },
696 [1] = {
697 .enable = overlay2fb_enable,
698 .disable = overlay2fb_disable,
699 .setup = overlay2fb_setup,
700 },
701};
702
703static int overlayfb_open(struct fb_info *info, int user)
704{
705 struct pxafb_layer *ofb = (struct pxafb_layer *)info;
706
707 /* no support for framebuffer console on overlay */
708 if (user == 0)
709 return -ENODEV;
710
711 /* allow only one user at a time */
712 if (atomic_inc_and_test(&ofb->usage))
713 return -EBUSY;
714
715 /* unblank the base framebuffer */
716 fb_blank(&ofb->fbi->fb, FB_BLANK_UNBLANK);
717 return 0;
718}
719
720static int overlayfb_release(struct fb_info *info, int user)
721{
722 struct pxafb_layer *ofb = (struct pxafb_layer*) info;
723
724 atomic_dec(&ofb->usage);
725 ofb->ops->disable(ofb);
726
727 free_pages_exact(ofb->video_mem, ofb->video_mem_size);
728 ofb->video_mem = NULL;
729 ofb->video_mem_size = 0;
730 return 0;
731}
732
733static int overlayfb_check_var(struct fb_var_screeninfo *var,
734 struct fb_info *info)
735{
736 struct pxafb_layer *ofb = (struct pxafb_layer *)info;
737 struct fb_var_screeninfo *base_var = &ofb->fbi->fb.var;
738 int xpos, ypos, pfor, bpp;
739
740 xpos = NONSTD_TO_XPOS(var->nonstd);
741 ypos = NONSTD_TO_XPOS(var->nonstd);
742 pfor = NONSTD_TO_PFOR(var->nonstd);
743
744 bpp = pxafb_var_to_bpp(var);
745 if (bpp < 0)
746 return -EINVAL;
747
748 /* no support for YUV format on overlay1 */
749 if (ofb->id == OVERLAY1 && pfor != 0)
750 return -EINVAL;
751
752 /* for YUV packed formats, bpp = 'minimum bpp of YUV components' */
753 switch (pfor) {
754 case OVERLAY_FORMAT_RGB:
755 bpp = pxafb_var_to_bpp(var);
756 if (bpp < 0)
757 return -EINVAL;
758
759 pxafb_set_pixfmt(var, var_to_depth(var));
760 break;
761 case OVERLAY_FORMAT_YUV444_PACKED: bpp = 24; break;
762 case OVERLAY_FORMAT_YUV444_PLANAR: bpp = 8; break;
763 case OVERLAY_FORMAT_YUV422_PLANAR: bpp = 4; break;
764 case OVERLAY_FORMAT_YUV420_PLANAR: bpp = 2; break;
765 default:
766 return -EINVAL;
767 }
768
769 /* each line must start at a 32-bit word boundary */
770 if ((xpos * bpp) % 32)
771 return -EINVAL;
772
773 /* xres must align on 32-bit word boundary */
774 var->xres = roundup(var->xres * bpp, 32) / bpp;
775
776 if ((xpos + var->xres > base_var->xres) ||
777 (ypos + var->yres > base_var->yres))
778 return -EINVAL;
779
780 var->xres_virtual = var->xres;
781 var->yres_virtual = max(var->yres, var->yres_virtual);
782 return 0;
783}
784
785static int overlayfb_map_video_memory(struct pxafb_layer *ofb)
786{
787 struct fb_var_screeninfo *var = &ofb->fb.var;
788 int pfor = NONSTD_TO_PFOR(var->nonstd);
789 int size, bpp = 0;
790
791 switch (pfor) {
792 case OVERLAY_FORMAT_RGB: bpp = var->bits_per_pixel; break;
793 case OVERLAY_FORMAT_YUV444_PACKED: bpp = 24; break;
794 case OVERLAY_FORMAT_YUV444_PLANAR: bpp = 24; break;
795 case OVERLAY_FORMAT_YUV422_PLANAR: bpp = 16; break;
796 case OVERLAY_FORMAT_YUV420_PLANAR: bpp = 12; break;
797 }
798
799 ofb->fb.fix.line_length = var->xres_virtual * bpp / 8;
800
801 size = PAGE_ALIGN(ofb->fb.fix.line_length * var->yres_virtual);
802
803 /* don't re-allocate if the original video memory is enough */
804 if (ofb->video_mem) {
805 if (ofb->video_mem_size >= size)
806 return 0;
807
808 free_pages_exact(ofb->video_mem, ofb->video_mem_size);
809 }
810
811 ofb->video_mem = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
812 if (ofb->video_mem == NULL)
813 return -ENOMEM;
814
815 ofb->video_mem_phys = virt_to_phys(ofb->video_mem);
816 ofb->video_mem_size = size;
817
818 ofb->fb.fix.smem_start = ofb->video_mem_phys;
819 ofb->fb.fix.smem_len = ofb->fb.fix.line_length * var->yres_virtual;
820 ofb->fb.screen_base = ofb->video_mem;
821 return 0;
822}
823
824static int overlayfb_set_par(struct fb_info *info)
825{
826 struct pxafb_layer *ofb = (struct pxafb_layer *)info;
827 struct fb_var_screeninfo *var = &info->var;
828 int xpos, ypos, pfor, bpp, ret;
829
830 ret = overlayfb_map_video_memory(ofb);
831 if (ret)
832 return ret;
833
834 bpp = pxafb_var_to_bpp(var);
835 xpos = NONSTD_TO_XPOS(var->nonstd);
836 ypos = NONSTD_TO_XPOS(var->nonstd);
837 pfor = NONSTD_TO_PFOR(var->nonstd);
838
839 ofb->control[0] = OVLxC1_PPL(var->xres) | OVLxC1_LPO(var->yres) |
840 OVLxC1_BPP(bpp);
841 ofb->control[1] = OVLxC2_XPOS(xpos) | OVLxC2_YPOS(ypos);
842
843 if (ofb->id == OVERLAY2)
844 ofb->control[1] |= OVL2C2_PFOR(pfor);
845
846 ofb->ops->setup(ofb);
847 ofb->ops->enable(ofb);
848 return 0;
849}
850
851static struct fb_ops overlay_fb_ops = {
852 .owner = THIS_MODULE,
853 .fb_open = overlayfb_open,
854 .fb_release = overlayfb_release,
855 .fb_check_var = overlayfb_check_var,
856 .fb_set_par = overlayfb_set_par,
857};
858
859static void __devinit init_pxafb_overlay(struct pxafb_info *fbi,
860 struct pxafb_layer *ofb, int id)
861{
862 sprintf(ofb->fb.fix.id, "overlay%d", id + 1);
863
864 ofb->fb.fix.type = FB_TYPE_PACKED_PIXELS;
865 ofb->fb.fix.xpanstep = 0;
866 ofb->fb.fix.ypanstep = 1;
867
868 ofb->fb.var.activate = FB_ACTIVATE_NOW;
869 ofb->fb.var.height = -1;
870 ofb->fb.var.width = -1;
871 ofb->fb.var.vmode = FB_VMODE_NONINTERLACED;
872
873 ofb->fb.fbops = &overlay_fb_ops;
874 ofb->fb.flags = FBINFO_FLAG_DEFAULT;
875 ofb->fb.node = -1;
876 ofb->fb.pseudo_palette = NULL;
877
878 ofb->id = id;
879 ofb->ops = &ofb_ops[id];
880 atomic_set(&ofb->usage, 0);
881 ofb->fbi = fbi;
882 init_completion(&ofb->branch_done);
883}
884
Eric Miao782385a2009-03-19 15:24:30 +0800885static inline int pxafb_overlay_supported(void)
886{
887 if (cpu_is_pxa27x() || cpu_is_pxa3xx())
888 return 1;
889
890 return 0;
891}
892
Eric Miao198fc102008-12-23 17:49:43 +0800893static int __devinit pxafb_overlay_init(struct pxafb_info *fbi)
894{
895 int i, ret;
896
Eric Miao782385a2009-03-19 15:24:30 +0800897 if (!pxafb_overlay_supported())
898 return 0;
899
Eric Miao198fc102008-12-23 17:49:43 +0800900 for (i = 0; i < 2; i++) {
901 init_pxafb_overlay(fbi, &fbi->overlay[i], i);
902 ret = register_framebuffer(&fbi->overlay[i].fb);
903 if (ret) {
904 dev_err(fbi->dev, "failed to register overlay %d\n", i);
905 return ret;
906 }
907 }
908
909 /* mask all IU/BS/EOF/SOF interrupts */
910 lcd_writel(fbi, LCCR5, ~0);
911
912 /* place overlay(s) on top of base */
913 fbi->lccr0 |= LCCR0_OUC;
914 pr_info("PXA Overlay driver loaded successfully!\n");
915 return 0;
916}
917
918static void __devexit pxafb_overlay_exit(struct pxafb_info *fbi)
919{
920 int i;
921
Eric Miao782385a2009-03-19 15:24:30 +0800922 if (!pxafb_overlay_supported())
923 return;
924
Eric Miao198fc102008-12-23 17:49:43 +0800925 for (i = 0; i < 2; i++)
926 unregister_framebuffer(&fbi->overlay[i].fb);
927}
928#else
929static inline void pxafb_overlay_init(struct pxafb_info *fbi) {}
930static inline void pxafb_overlay_exit(struct pxafb_info *fbi) {}
931#endif /* CONFIG_FB_PXA_OVERLAY */
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933/*
934 * Calculate the PCD value from the clock rate (in picoseconds).
935 * We take account of the PPCR clock setting.
936 * From PXA Developer's Manual:
937 *
938 * PixelClock = LCLK
939 * -------------
940 * 2 ( PCD + 1 )
941 *
942 * PCD = LCLK
943 * ------------- - 1
944 * 2(PixelClock)
945 *
946 * Where:
947 * LCLK = LCD/Memory Clock
948 * PCD = LCCR3[7:0]
949 *
950 * PixelClock here is in Hz while the pixclock argument given is the
951 * period in picoseconds. Hence PixelClock = 1 / ( pixclock * 10^-12 )
952 *
953 * The function get_lclk_frequency_10khz returns LCLK in units of
954 * 10khz. Calling the result of this function lclk gives us the
955 * following
956 *
957 * PCD = (lclk * 10^4 ) * ( pixclock * 10^-12 )
958 * -------------------------------------- - 1
959 * 2
960 *
961 * Factoring the 10^4 and 10^-12 out gives 10^-8 == 1 / 100000000 as used below.
962 */
eric miaob0086ef2008-04-30 00:52:19 -0700963static inline unsigned int get_pcd(struct pxafb_info *fbi,
964 unsigned int pixclock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
966 unsigned long long pcd;
967
968 /* FIXME: Need to take into account Double Pixel Clock mode
Russell King72e35242007-08-20 10:18:42 +0100969 * (DPC) bit? or perhaps set it based on the various clock
970 * speeds */
971 pcd = (unsigned long long)(clk_get_rate(fbi->clk) / 10000);
972 pcd *= pixclock;
Nicolas Pitrebf1b8ab2005-06-23 21:56:45 +0100973 do_div(pcd, 100000000 * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 /* no need for this, since we should subtract 1 anyway. they cancel */
975 /* pcd += 1; */ /* make up for integer math truncations */
976 return (unsigned int)pcd;
977}
978
979/*
Richard Purdieba44cd22005-09-09 13:10:03 -0700980 * Some touchscreens need hsync information from the video driver to
Russell King72e35242007-08-20 10:18:42 +0100981 * function correctly. We export it here. Note that 'hsync_time' and
982 * the value returned from pxafb_get_hsync_time() is the *reciprocal*
983 * of the hsync period in seconds.
Richard Purdieba44cd22005-09-09 13:10:03 -0700984 */
985static inline void set_hsync_time(struct pxafb_info *fbi, unsigned int pcd)
986{
Russell King72e35242007-08-20 10:18:42 +0100987 unsigned long htime;
Richard Purdieba44cd22005-09-09 13:10:03 -0700988
989 if ((pcd == 0) || (fbi->fb.var.hsync_len == 0)) {
eric miaob0086ef2008-04-30 00:52:19 -0700990 fbi->hsync_time = 0;
Richard Purdieba44cd22005-09-09 13:10:03 -0700991 return;
992 }
993
Russell King72e35242007-08-20 10:18:42 +0100994 htime = clk_get_rate(fbi->clk) / (pcd * fbi->fb.var.hsync_len);
995
Richard Purdieba44cd22005-09-09 13:10:03 -0700996 fbi->hsync_time = htime;
997}
998
999unsigned long pxafb_get_hsync_time(struct device *dev)
1000{
1001 struct pxafb_info *fbi = dev_get_drvdata(dev);
1002
1003 /* If display is blanked/suspended, hsync isn't active */
1004 if (!fbi || (fbi->state != C_ENABLE))
1005 return 0;
1006
1007 return fbi->hsync_time;
1008}
1009EXPORT_SYMBOL(pxafb_get_hsync_time);
1010
eric miao2c42dd82008-04-30 00:52:21 -07001011static int setup_frame_dma(struct pxafb_info *fbi, int dma, int pal,
Eric Miao198fc102008-12-23 17:49:43 +08001012 unsigned long start, size_t size)
eric miao2c42dd82008-04-30 00:52:21 -07001013{
1014 struct pxafb_dma_descriptor *dma_desc, *pal_desc;
1015 unsigned int dma_desc_off, pal_desc_off;
1016
Eric Miao6e354842008-12-17 16:50:43 +08001017 if (dma < 0 || dma >= DMA_MAX * 2)
eric miao2c42dd82008-04-30 00:52:21 -07001018 return -EINVAL;
1019
1020 dma_desc = &fbi->dma_buff->dma_desc[dma];
1021 dma_desc_off = offsetof(struct pxafb_dma_buff, dma_desc[dma]);
1022
Eric Miao198fc102008-12-23 17:49:43 +08001023 dma_desc->fsadr = start;
eric miao2c42dd82008-04-30 00:52:21 -07001024 dma_desc->fidr = 0;
1025 dma_desc->ldcmd = size;
1026
Eric Miao6e354842008-12-17 16:50:43 +08001027 if (pal < 0 || pal >= PAL_MAX * 2) {
eric miao2c42dd82008-04-30 00:52:21 -07001028 dma_desc->fdadr = fbi->dma_buff_phys + dma_desc_off;
1029 fbi->fdadr[dma] = fbi->dma_buff_phys + dma_desc_off;
1030 } else {
Jürgen Schindele62cfcf42008-06-11 19:56:06 +01001031 pal_desc = &fbi->dma_buff->pal_desc[pal];
1032 pal_desc_off = offsetof(struct pxafb_dma_buff, pal_desc[pal]);
eric miao2c42dd82008-04-30 00:52:21 -07001033
1034 pal_desc->fsadr = fbi->dma_buff_phys + pal * PALETTE_SIZE;
1035 pal_desc->fidr = 0;
1036
1037 if ((fbi->lccr4 & LCCR4_PAL_FOR_MASK) == LCCR4_PAL_FOR_0)
1038 pal_desc->ldcmd = fbi->palette_size * sizeof(u16);
1039 else
1040 pal_desc->ldcmd = fbi->palette_size * sizeof(u32);
1041
1042 pal_desc->ldcmd |= LDCMD_PAL;
1043
1044 /* flip back and forth between palette and frame buffer */
1045 pal_desc->fdadr = fbi->dma_buff_phys + dma_desc_off;
1046 dma_desc->fdadr = fbi->dma_buff_phys + pal_desc_off;
1047 fbi->fdadr[dma] = fbi->dma_buff_phys + dma_desc_off;
1048 }
1049
1050 return 0;
1051}
1052
Eric Miao6e354842008-12-17 16:50:43 +08001053static void setup_base_frame(struct pxafb_info *fbi, int branch)
1054{
1055 struct fb_var_screeninfo *var = &fbi->fb.var;
1056 struct fb_fix_screeninfo *fix = &fbi->fb.fix;
Eric Miao198fc102008-12-23 17:49:43 +08001057 int nbytes, dma, pal, bpp = var->bits_per_pixel;
1058 unsigned long offset;
Eric Miao6e354842008-12-17 16:50:43 +08001059
1060 dma = DMA_BASE + (branch ? DMA_MAX : 0);
1061 pal = (bpp >= 16) ? PAL_NONE : PAL_BASE + (branch ? PAL_MAX : 0);
1062
1063 nbytes = fix->line_length * var->yres;
Eric Miao198fc102008-12-23 17:49:43 +08001064 offset = fix->line_length * var->yoffset + fbi->video_mem_phys;
Eric Miao6e354842008-12-17 16:50:43 +08001065
1066 if (fbi->lccr0 & LCCR0_SDS) {
1067 nbytes = nbytes / 2;
1068 setup_frame_dma(fbi, dma + 1, PAL_NONE, offset + nbytes, nbytes);
1069 }
1070
1071 setup_frame_dma(fbi, dma, pal, offset, nbytes);
1072}
1073
Eric Miao3c42a442008-04-30 00:52:26 -07001074#ifdef CONFIG_FB_PXA_SMARTPANEL
1075static int setup_smart_dma(struct pxafb_info *fbi)
1076{
1077 struct pxafb_dma_descriptor *dma_desc;
1078 unsigned long dma_desc_off, cmd_buff_off;
1079
1080 dma_desc = &fbi->dma_buff->dma_desc[DMA_CMD];
1081 dma_desc_off = offsetof(struct pxafb_dma_buff, dma_desc[DMA_CMD]);
1082 cmd_buff_off = offsetof(struct pxafb_dma_buff, cmd_buff);
1083
1084 dma_desc->fdadr = fbi->dma_buff_phys + dma_desc_off;
1085 dma_desc->fsadr = fbi->dma_buff_phys + cmd_buff_off;
1086 dma_desc->fidr = 0;
1087 dma_desc->ldcmd = fbi->n_smart_cmds * sizeof(uint16_t);
1088
1089 fbi->fdadr[DMA_CMD] = dma_desc->fdadr;
1090 return 0;
1091}
1092
1093int pxafb_smart_flush(struct fb_info *info)
1094{
1095 struct pxafb_info *fbi = container_of(info, struct pxafb_info, fb);
1096 uint32_t prsr;
1097 int ret = 0;
1098
1099 /* disable controller until all registers are set up */
1100 lcd_writel(fbi, LCCR0, fbi->reg_lccr0 & ~LCCR0_ENB);
1101
1102 /* 1. make it an even number of commands to align on 32-bit boundary
1103 * 2. add the interrupt command to the end of the chain so we can
1104 * keep track of the end of the transfer
1105 */
1106
1107 while (fbi->n_smart_cmds & 1)
1108 fbi->smart_cmds[fbi->n_smart_cmds++] = SMART_CMD_NOOP;
1109
1110 fbi->smart_cmds[fbi->n_smart_cmds++] = SMART_CMD_INTERRUPT;
1111 fbi->smart_cmds[fbi->n_smart_cmds++] = SMART_CMD_WAIT_FOR_VSYNC;
1112 setup_smart_dma(fbi);
1113
1114 /* continue to execute next command */
1115 prsr = lcd_readl(fbi, PRSR) | PRSR_ST_OK | PRSR_CON_NT;
1116 lcd_writel(fbi, PRSR, prsr);
1117
1118 /* stop the processor in case it executed "wait for sync" cmd */
1119 lcd_writel(fbi, CMDCR, 0x0001);
1120
1121 /* don't send interrupts for fifo underruns on channel 6 */
1122 lcd_writel(fbi, LCCR5, LCCR5_IUM(6));
1123
1124 lcd_writel(fbi, LCCR1, fbi->reg_lccr1);
1125 lcd_writel(fbi, LCCR2, fbi->reg_lccr2);
1126 lcd_writel(fbi, LCCR3, fbi->reg_lccr3);
Eric Miaoa0427502008-12-18 22:10:00 +08001127 lcd_writel(fbi, LCCR4, fbi->reg_lccr4);
Eric Miao3c42a442008-04-30 00:52:26 -07001128 lcd_writel(fbi, FDADR0, fbi->fdadr[0]);
1129 lcd_writel(fbi, FDADR6, fbi->fdadr[6]);
1130
1131 /* begin sending */
1132 lcd_writel(fbi, LCCR0, fbi->reg_lccr0 | LCCR0_ENB);
1133
1134 if (wait_for_completion_timeout(&fbi->command_done, HZ/2) == 0) {
1135 pr_warning("%s: timeout waiting for command done\n",
1136 __func__);
1137 ret = -ETIMEDOUT;
1138 }
1139
1140 /* quick disable */
1141 prsr = lcd_readl(fbi, PRSR) & ~(PRSR_ST_OK | PRSR_CON_NT);
1142 lcd_writel(fbi, PRSR, prsr);
1143 lcd_writel(fbi, LCCR0, fbi->reg_lccr0 & ~LCCR0_ENB);
1144 lcd_writel(fbi, FDADR6, 0);
1145 fbi->n_smart_cmds = 0;
1146 return ret;
1147}
1148
1149int pxafb_smart_queue(struct fb_info *info, uint16_t *cmds, int n_cmds)
1150{
1151 int i;
1152 struct pxafb_info *fbi = container_of(info, struct pxafb_info, fb);
1153
Eric Miao69bdea72008-12-08 18:46:00 +08001154 for (i = 0; i < n_cmds; i++, cmds++) {
1155 /* if it is a software delay, flush and delay */
1156 if ((*cmds & 0xff00) == SMART_CMD_DELAY) {
1157 pxafb_smart_flush(info);
1158 mdelay(*cmds & 0xff);
1159 continue;
1160 }
1161
1162 /* leave 2 commands for INTERRUPT and WAIT_FOR_SYNC */
Eric Miao3c42a442008-04-30 00:52:26 -07001163 if (fbi->n_smart_cmds == CMD_BUFF_SIZE - 8)
1164 pxafb_smart_flush(info);
1165
Eric Miao69bdea72008-12-08 18:46:00 +08001166 fbi->smart_cmds[fbi->n_smart_cmds++] = *cmds;
Eric Miao3c42a442008-04-30 00:52:26 -07001167 }
1168
1169 return 0;
1170}
1171
1172static unsigned int __smart_timing(unsigned time_ns, unsigned long lcd_clk)
1173{
1174 unsigned int t = (time_ns * (lcd_clk / 1000000) / 1000);
1175 return (t == 0) ? 1 : t;
1176}
1177
1178static void setup_smart_timing(struct pxafb_info *fbi,
1179 struct fb_var_screeninfo *var)
1180{
1181 struct pxafb_mach_info *inf = fbi->dev->platform_data;
1182 struct pxafb_mode_info *mode = &inf->modes[0];
1183 unsigned long lclk = clk_get_rate(fbi->clk);
1184 unsigned t1, t2, t3, t4;
1185
1186 t1 = max(mode->a0csrd_set_hld, mode->a0cswr_set_hld);
1187 t2 = max(mode->rd_pulse_width, mode->wr_pulse_width);
1188 t3 = mode->op_hold_time;
1189 t4 = mode->cmd_inh_time;
1190
1191 fbi->reg_lccr1 =
1192 LCCR1_DisWdth(var->xres) |
1193 LCCR1_BegLnDel(__smart_timing(t1, lclk)) |
1194 LCCR1_EndLnDel(__smart_timing(t2, lclk)) |
1195 LCCR1_HorSnchWdth(__smart_timing(t3, lclk));
1196
1197 fbi->reg_lccr2 = LCCR2_DisHght(var->yres);
Eric Miaoc1f99c22008-12-08 18:35:03 +08001198 fbi->reg_lccr3 = fbi->lccr3 | LCCR3_PixClkDiv(__smart_timing(t4, lclk));
1199 fbi->reg_lccr3 |= (var->sync & FB_SYNC_HOR_HIGH_ACT) ? LCCR3_HSP : 0;
1200 fbi->reg_lccr3 |= (var->sync & FB_SYNC_VERT_HIGH_ACT) ? LCCR3_VSP : 0;
Eric Miao3c42a442008-04-30 00:52:26 -07001201
1202 /* FIXME: make this configurable */
1203 fbi->reg_cmdcr = 1;
1204}
1205
1206static int pxafb_smart_thread(void *arg)
1207{
Eric Miao7f1133c2008-04-30 00:52:27 -07001208 struct pxafb_info *fbi = arg;
Eric Miao3c42a442008-04-30 00:52:26 -07001209 struct pxafb_mach_info *inf = fbi->dev->platform_data;
1210
1211 if (!fbi || !inf->smart_update) {
1212 pr_err("%s: not properly initialized, thread terminated\n",
1213 __func__);
1214 return -EINVAL;
1215 }
1216
1217 pr_debug("%s(): task starting\n", __func__);
1218
1219 set_freezable();
1220 while (!kthread_should_stop()) {
1221
1222 if (try_to_freeze())
1223 continue;
1224
Eric Miao07f651c2008-12-08 18:51:01 +08001225 mutex_lock(&fbi->ctrlr_lock);
1226
Eric Miao3c42a442008-04-30 00:52:26 -07001227 if (fbi->state == C_ENABLE) {
1228 inf->smart_update(&fbi->fb);
1229 complete(&fbi->refresh_done);
1230 }
1231
Eric Miao07f651c2008-12-08 18:51:01 +08001232 mutex_unlock(&fbi->ctrlr_lock);
1233
Eric Miao3c42a442008-04-30 00:52:26 -07001234 set_current_state(TASK_INTERRUPTIBLE);
1235 schedule_timeout(30 * HZ / 1000);
1236 }
1237
1238 pr_debug("%s(): task ending\n", __func__);
1239 return 0;
1240}
1241
1242static int pxafb_smart_init(struct pxafb_info *fbi)
1243{
Eric Miao07df1c42008-12-04 11:14:11 +08001244 if (!(fbi->lccr0 & LCCR0_LCDT))
Eric Miao6cc4abe2008-11-11 21:47:07 +08001245 return 0;
1246
Eric Miao07df1c42008-12-04 11:14:11 +08001247 fbi->smart_cmds = (uint16_t *) fbi->dma_buff->cmd_buff;
1248 fbi->n_smart_cmds = 0;
1249
1250 init_completion(&fbi->command_done);
1251 init_completion(&fbi->refresh_done);
1252
Eric Miao3c42a442008-04-30 00:52:26 -07001253 fbi->smart_thread = kthread_run(pxafb_smart_thread, fbi,
1254 "lcd_refresh");
1255 if (IS_ERR(fbi->smart_thread)) {
Eric Miao07df1c42008-12-04 11:14:11 +08001256 pr_err("%s: unable to create kernel thread\n", __func__);
Eric Miao3c42a442008-04-30 00:52:26 -07001257 return PTR_ERR(fbi->smart_thread);
1258 }
Eric Miaoa5718a12008-11-11 21:50:39 +08001259
Eric Miao3c42a442008-04-30 00:52:26 -07001260 return 0;
1261}
1262#else
1263int pxafb_smart_queue(struct fb_info *info, uint16_t *cmds, int n_cmds)
1264{
1265 return 0;
1266}
1267
1268int pxafb_smart_flush(struct fb_info *info)
1269{
1270 return 0;
1271}
Eric Miao07df1c42008-12-04 11:14:11 +08001272
1273static inline int pxafb_smart_init(struct pxafb_info *fbi) { return 0; }
1274#endif /* CONFIG_FB_PXA_SMARTPANEL */
Eric Miao3c42a442008-04-30 00:52:26 -07001275
Eric Miao90eabbf2008-04-30 00:52:25 -07001276static void setup_parallel_timing(struct pxafb_info *fbi,
1277 struct fb_var_screeninfo *var)
1278{
1279 unsigned int lines_per_panel, pcd = get_pcd(fbi, var->pixclock);
1280
1281 fbi->reg_lccr1 =
1282 LCCR1_DisWdth(var->xres) +
1283 LCCR1_HorSnchWdth(var->hsync_len) +
1284 LCCR1_BegLnDel(var->left_margin) +
1285 LCCR1_EndLnDel(var->right_margin);
1286
1287 /*
1288 * If we have a dual scan LCD, we need to halve
1289 * the YRES parameter.
1290 */
1291 lines_per_panel = var->yres;
1292 if ((fbi->lccr0 & LCCR0_SDS) == LCCR0_Dual)
1293 lines_per_panel /= 2;
1294
1295 fbi->reg_lccr2 =
1296 LCCR2_DisHght(lines_per_panel) +
1297 LCCR2_VrtSnchWdth(var->vsync_len) +
1298 LCCR2_BegFrmDel(var->upper_margin) +
1299 LCCR2_EndFrmDel(var->lower_margin);
1300
1301 fbi->reg_lccr3 = fbi->lccr3 |
1302 (var->sync & FB_SYNC_HOR_HIGH_ACT ?
1303 LCCR3_HorSnchH : LCCR3_HorSnchL) |
1304 (var->sync & FB_SYNC_VERT_HIGH_ACT ?
1305 LCCR3_VrtSnchH : LCCR3_VrtSnchL);
1306
1307 if (pcd) {
1308 fbi->reg_lccr3 |= LCCR3_PixClkDiv(pcd);
1309 set_hsync_time(fbi, pcd);
1310 }
1311}
1312
Richard Purdieba44cd22005-09-09 13:10:03 -07001313/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 * pxafb_activate_var():
eric miaob0086ef2008-04-30 00:52:19 -07001315 * Configures LCD Controller based on entries in var parameter.
1316 * Settings are only written to the controller if changes were made.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 */
eric miaob0086ef2008-04-30 00:52:19 -07001318static int pxafb_activate_var(struct fb_var_screeninfo *var,
1319 struct pxafb_info *fbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 u_long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 /* Update shadow copy atomically */
1324 local_irq_save(flags);
1325
Eric Miao3c42a442008-04-30 00:52:26 -07001326#ifdef CONFIG_FB_PXA_SMARTPANEL
1327 if (fbi->lccr0 & LCCR0_LCDT)
1328 setup_smart_timing(fbi, var);
1329 else
1330#endif
1331 setup_parallel_timing(fbi, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Eric Miao6e354842008-12-17 16:50:43 +08001333 setup_base_frame(fbi, 0);
1334
Eric Miao90eabbf2008-04-30 00:52:25 -07001335 fbi->reg_lccr0 = fbi->lccr0 |
1336 (LCCR0_LDM | LCCR0_SFM | LCCR0_IUM | LCCR0_EFM |
1337 LCCR0_QDM | LCCR0_BM | LCCR0_OUM);
1338
Eric Miao878f5782008-12-18 22:36:26 +08001339 fbi->reg_lccr3 |= pxafb_var_to_lccr3(var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Eric Miaoa7535ba2008-04-30 00:52:24 -07001341 fbi->reg_lccr4 = lcd_readl(fbi, LCCR4) & ~LCCR4_PAL_FOR_MASK;
Hans J. Koch9ffa7392007-10-16 01:28:41 -07001342 fbi->reg_lccr4 |= (fbi->lccr4 & LCCR4_PAL_FOR_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 local_irq_restore(flags);
1344
1345 /*
1346 * Only update the registers if the controller is enabled
1347 * and something has changed.
1348 */
Eric Miaoa7535ba2008-04-30 00:52:24 -07001349 if ((lcd_readl(fbi, LCCR0) != fbi->reg_lccr0) ||
1350 (lcd_readl(fbi, LCCR1) != fbi->reg_lccr1) ||
1351 (lcd_readl(fbi, LCCR2) != fbi->reg_lccr2) ||
1352 (lcd_readl(fbi, LCCR3) != fbi->reg_lccr3) ||
Eric Miaoa0427502008-12-18 22:10:00 +08001353 (lcd_readl(fbi, LCCR4) != fbi->reg_lccr4) ||
Eric Miaoa7535ba2008-04-30 00:52:24 -07001354 (lcd_readl(fbi, FDADR0) != fbi->fdadr[0]) ||
1355 (lcd_readl(fbi, FDADR1) != fbi->fdadr[1]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 pxafb_schedule_work(fbi, C_REENABLE);
1357
1358 return 0;
1359}
1360
1361/*
1362 * NOTE! The following functions are purely helpers for set_ctrlr_state.
1363 * Do not call them directly; set_ctrlr_state does the correct serialisation
1364 * to ensure that things happen in the right way 100% of time time.
1365 * -- rmk
1366 */
1367static inline void __pxafb_backlight_power(struct pxafb_info *fbi, int on)
1368{
Russell Kingca5da712005-09-29 09:44:54 +01001369 pr_debug("pxafb: backlight o%s\n", on ? "n" : "ff");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Eric Miaoa5718a12008-11-11 21:50:39 +08001371 if (fbi->backlight_power)
1372 fbi->backlight_power(on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373}
1374
1375static inline void __pxafb_lcd_power(struct pxafb_info *fbi, int on)
1376{
Russell Kingca5da712005-09-29 09:44:54 +01001377 pr_debug("pxafb: LCD power o%s\n", on ? "n" : "ff");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Eric Miaoa5718a12008-11-11 21:50:39 +08001379 if (fbi->lcd_power)
1380 fbi->lcd_power(on, &fbi->fb.var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381}
1382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383static void pxafb_enable_controller(struct pxafb_info *fbi)
1384{
Russell Kingca5da712005-09-29 09:44:54 +01001385 pr_debug("pxafb: Enabling LCD controller\n");
eric miao2c42dd82008-04-30 00:52:21 -07001386 pr_debug("fdadr0 0x%08x\n", (unsigned int) fbi->fdadr[0]);
1387 pr_debug("fdadr1 0x%08x\n", (unsigned int) fbi->fdadr[1]);
Russell Kingca5da712005-09-29 09:44:54 +01001388 pr_debug("reg_lccr0 0x%08x\n", (unsigned int) fbi->reg_lccr0);
1389 pr_debug("reg_lccr1 0x%08x\n", (unsigned int) fbi->reg_lccr1);
1390 pr_debug("reg_lccr2 0x%08x\n", (unsigned int) fbi->reg_lccr2);
1391 pr_debug("reg_lccr3 0x%08x\n", (unsigned int) fbi->reg_lccr3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Nicolas Pitre8d372262005-08-10 16:45:13 +01001393 /* enable LCD controller clock */
Russell King72e35242007-08-20 10:18:42 +01001394 clk_enable(fbi->clk);
Nicolas Pitre8d372262005-08-10 16:45:13 +01001395
Eric Miao3c42a442008-04-30 00:52:26 -07001396 if (fbi->lccr0 & LCCR0_LCDT)
1397 return;
1398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 /* Sequence from 11.7.10 */
Eric Miaoa0427502008-12-18 22:10:00 +08001400 lcd_writel(fbi, LCCR4, fbi->reg_lccr4);
Eric Miaoa7535ba2008-04-30 00:52:24 -07001401 lcd_writel(fbi, LCCR3, fbi->reg_lccr3);
1402 lcd_writel(fbi, LCCR2, fbi->reg_lccr2);
1403 lcd_writel(fbi, LCCR1, fbi->reg_lccr1);
1404 lcd_writel(fbi, LCCR0, fbi->reg_lccr0 & ~LCCR0_ENB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Eric Miaoa7535ba2008-04-30 00:52:24 -07001406 lcd_writel(fbi, FDADR0, fbi->fdadr[0]);
1407 lcd_writel(fbi, FDADR1, fbi->fdadr[1]);
1408 lcd_writel(fbi, LCCR0, fbi->reg_lccr0 | LCCR0_ENB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409}
1410
1411static void pxafb_disable_controller(struct pxafb_info *fbi)
1412{
eric miaoce4fb7b2008-04-30 00:52:21 -07001413 uint32_t lccr0;
1414
Eric Miao3c42a442008-04-30 00:52:26 -07001415#ifdef CONFIG_FB_PXA_SMARTPANEL
1416 if (fbi->lccr0 & LCCR0_LCDT) {
1417 wait_for_completion_timeout(&fbi->refresh_done,
1418 200 * HZ / 1000);
1419 return;
1420 }
1421#endif
1422
eric miaoce4fb7b2008-04-30 00:52:21 -07001423 /* Clear LCD Status Register */
Eric Miaoa7535ba2008-04-30 00:52:24 -07001424 lcd_writel(fbi, LCSR, 0xffffffff);
eric miaoce4fb7b2008-04-30 00:52:21 -07001425
Eric Miaoa7535ba2008-04-30 00:52:24 -07001426 lccr0 = lcd_readl(fbi, LCCR0) & ~LCCR0_LDM;
1427 lcd_writel(fbi, LCCR0, lccr0);
1428 lcd_writel(fbi, LCCR0, lccr0 | LCCR0_DIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Eric Miao2ba162b2008-04-30 00:52:24 -07001430 wait_for_completion_timeout(&fbi->disable_done, 200 * HZ / 1000);
Nicolas Pitre8d372262005-08-10 16:45:13 +01001431
1432 /* disable LCD controller clock */
Russell King72e35242007-08-20 10:18:42 +01001433 clk_disable(fbi->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434}
1435
1436/*
1437 * pxafb_handle_irq: Handle 'LCD DONE' interrupts.
1438 */
David Howells7d12e782006-10-05 14:55:46 +01001439static irqreturn_t pxafb_handle_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
1441 struct pxafb_info *fbi = dev_id;
Denis V. Lunevff14ed52009-04-21 12:23:59 -07001442 unsigned int lccr0, lcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Eric Miao198fc102008-12-23 17:49:43 +08001444 lcsr = lcd_readl(fbi, LCSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 if (lcsr & LCSR_LDD) {
Eric Miaoa7535ba2008-04-30 00:52:24 -07001446 lccr0 = lcd_readl(fbi, LCCR0);
1447 lcd_writel(fbi, LCCR0, lccr0 | LCCR0_LDM);
Eric Miao2ba162b2008-04-30 00:52:24 -07001448 complete(&fbi->disable_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 }
1450
Eric Miao3c42a442008-04-30 00:52:26 -07001451#ifdef CONFIG_FB_PXA_SMARTPANEL
1452 if (lcsr & LCSR_CMD_INT)
1453 complete(&fbi->command_done);
1454#endif
Eric Miaoa7535ba2008-04-30 00:52:24 -07001455 lcd_writel(fbi, LCSR, lcsr);
Eric Miao198fc102008-12-23 17:49:43 +08001456
1457#ifdef CONFIG_FB_PXA_OVERLAY
Denis V. Lunevff14ed52009-04-21 12:23:59 -07001458 {
1459 unsigned int lcsr1 = lcd_readl(fbi, LCSR1);
1460 if (lcsr1 & LCSR1_BS(1))
1461 complete(&fbi->overlay[0].branch_done);
Eric Miao198fc102008-12-23 17:49:43 +08001462
Denis V. Lunevff14ed52009-04-21 12:23:59 -07001463 if (lcsr1 & LCSR1_BS(2))
1464 complete(&fbi->overlay[1].branch_done);
Eric Miao198fc102008-12-23 17:49:43 +08001465
Denis V. Lunevff14ed52009-04-21 12:23:59 -07001466 lcd_writel(fbi, LCSR1, lcsr1);
1467 }
Eric Miao198fc102008-12-23 17:49:43 +08001468#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return IRQ_HANDLED;
1470}
1471
1472/*
1473 * This function must be called from task context only, since it will
1474 * sleep when disabling the LCD controller, or if we get two contending
1475 * processes trying to alter state.
1476 */
1477static void set_ctrlr_state(struct pxafb_info *fbi, u_int state)
1478{
1479 u_int old_state;
1480
Matthias Kaehlckeb91dbce2008-07-23 21:31:14 -07001481 mutex_lock(&fbi->ctrlr_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
1483 old_state = fbi->state;
1484
1485 /*
1486 * Hack around fbcon initialisation.
1487 */
1488 if (old_state == C_STARTUP && state == C_REENABLE)
1489 state = C_ENABLE;
1490
1491 switch (state) {
1492 case C_DISABLE_CLKCHANGE:
1493 /*
1494 * Disable controller for clock change. If the
1495 * controller is already disabled, then do nothing.
1496 */
1497 if (old_state != C_DISABLE && old_state != C_DISABLE_PM) {
1498 fbi->state = state;
eric miaob0086ef2008-04-30 00:52:19 -07001499 /* TODO __pxafb_lcd_power(fbi, 0); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 pxafb_disable_controller(fbi);
1501 }
1502 break;
1503
1504 case C_DISABLE_PM:
1505 case C_DISABLE:
1506 /*
1507 * Disable controller
1508 */
1509 if (old_state != C_DISABLE) {
1510 fbi->state = state;
1511 __pxafb_backlight_power(fbi, 0);
1512 __pxafb_lcd_power(fbi, 0);
1513 if (old_state != C_DISABLE_CLKCHANGE)
1514 pxafb_disable_controller(fbi);
1515 }
1516 break;
1517
1518 case C_ENABLE_CLKCHANGE:
1519 /*
1520 * Enable the controller after clock change. Only
1521 * do this if we were disabled for the clock change.
1522 */
1523 if (old_state == C_DISABLE_CLKCHANGE) {
1524 fbi->state = C_ENABLE;
1525 pxafb_enable_controller(fbi);
eric miaob0086ef2008-04-30 00:52:19 -07001526 /* TODO __pxafb_lcd_power(fbi, 1); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 }
1528 break;
1529
1530 case C_REENABLE:
1531 /*
1532 * Re-enable the controller only if it was already
1533 * enabled. This is so we reprogram the control
1534 * registers.
1535 */
1536 if (old_state == C_ENABLE) {
Richard Purdied14b2722006-09-20 22:54:21 +01001537 __pxafb_lcd_power(fbi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 pxafb_disable_controller(fbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 pxafb_enable_controller(fbi);
Richard Purdied14b2722006-09-20 22:54:21 +01001540 __pxafb_lcd_power(fbi, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 }
1542 break;
1543
1544 case C_ENABLE_PM:
1545 /*
1546 * Re-enable the controller after PM. This is not
1547 * perfect - think about the case where we were doing
1548 * a clock change, and we suspended half-way through.
1549 */
1550 if (old_state != C_DISABLE_PM)
1551 break;
1552 /* fall through */
1553
1554 case C_ENABLE:
1555 /*
1556 * Power up the LCD screen, enable controller, and
1557 * turn on the backlight.
1558 */
1559 if (old_state != C_ENABLE) {
1560 fbi->state = C_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 pxafb_enable_controller(fbi);
1562 __pxafb_lcd_power(fbi, 1);
1563 __pxafb_backlight_power(fbi, 1);
1564 }
1565 break;
1566 }
Matthias Kaehlckeb91dbce2008-07-23 21:31:14 -07001567 mutex_unlock(&fbi->ctrlr_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568}
1569
1570/*
1571 * Our LCD controller task (which is called when we blank or unblank)
1572 * via keventd.
1573 */
David Howells6d5aefb2006-12-05 19:36:26 +00001574static void pxafb_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
David Howells6d5aefb2006-12-05 19:36:26 +00001576 struct pxafb_info *fbi =
1577 container_of(work, struct pxafb_info, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 u_int state = xchg(&fbi->task_state, -1);
1579
1580 set_ctrlr_state(fbi, state);
1581}
1582
1583#ifdef CONFIG_CPU_FREQ
1584/*
1585 * CPU clock speed change handler. We need to adjust the LCD timing
1586 * parameters when the CPU clock is adjusted by the power management
1587 * subsystem.
1588 *
1589 * TODO: Determine why f->new != 10*get_lclk_frequency_10khz()
1590 */
1591static int
1592pxafb_freq_transition(struct notifier_block *nb, unsigned long val, void *data)
1593{
1594 struct pxafb_info *fbi = TO_INF(nb, freq_transition);
eric miaob0086ef2008-04-30 00:52:19 -07001595 /* TODO struct cpufreq_freqs *f = data; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 u_int pcd;
1597
1598 switch (val) {
1599 case CPUFREQ_PRECHANGE:
1600 set_ctrlr_state(fbi, C_DISABLE_CLKCHANGE);
1601 break;
1602
1603 case CPUFREQ_POSTCHANGE:
Russell King72e35242007-08-20 10:18:42 +01001604 pcd = get_pcd(fbi, fbi->fb.var.pixclock);
Richard Purdieba44cd22005-09-09 13:10:03 -07001605 set_hsync_time(fbi, pcd);
eric miaob0086ef2008-04-30 00:52:19 -07001606 fbi->reg_lccr3 = (fbi->reg_lccr3 & ~0xff) |
1607 LCCR3_PixClkDiv(pcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 set_ctrlr_state(fbi, C_ENABLE_CLKCHANGE);
1609 break;
1610 }
1611 return 0;
1612}
1613
1614static int
1615pxafb_freq_policy(struct notifier_block *nb, unsigned long val, void *data)
1616{
1617 struct pxafb_info *fbi = TO_INF(nb, freq_policy);
1618 struct fb_var_screeninfo *var = &fbi->fb.var;
1619 struct cpufreq_policy *policy = data;
1620
1621 switch (val) {
1622 case CPUFREQ_ADJUST:
1623 case CPUFREQ_INCOMPATIBLE:
Holger Schurigac2bf5b2008-02-11 16:52:30 +01001624 pr_debug("min dma period: %d ps, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 "new clock %d kHz\n", pxafb_display_dma_period(var),
1626 policy->max);
eric miaob0086ef2008-04-30 00:52:19 -07001627 /* TODO: fill in min/max values */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 }
1630 return 0;
1631}
1632#endif
1633
1634#ifdef CONFIG_PM
1635/*
1636 * Power management hooks. Note that we won't be called from IRQ context,
1637 * unlike the blank functions above, so we may sleep.
1638 */
Russell King3ae5eae2005-11-09 22:32:44 +00001639static int pxafb_suspend(struct platform_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
Russell King3ae5eae2005-11-09 22:32:44 +00001641 struct pxafb_info *fbi = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Russell King9480e302005-10-28 09:52:56 -07001643 set_ctrlr_state(fbi, C_DISABLE_PM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 return 0;
1645}
1646
Russell King3ae5eae2005-11-09 22:32:44 +00001647static int pxafb_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
Russell King3ae5eae2005-11-09 22:32:44 +00001649 struct pxafb_info *fbi = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Russell King9480e302005-10-28 09:52:56 -07001651 set_ctrlr_state(fbi, C_ENABLE_PM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 return 0;
1653}
1654#else
1655#define pxafb_suspend NULL
1656#define pxafb_resume NULL
1657#endif
1658
Eric Miao77e19672008-12-16 11:54:34 +08001659static int __devinit pxafb_init_video_memory(struct pxafb_info *fbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660{
Eric Miao77e19672008-12-16 11:54:34 +08001661 int size = PAGE_ALIGN(fbi->video_mem_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Eric Miao77e19672008-12-16 11:54:34 +08001663 fbi->video_mem = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
1664 if (fbi->video_mem == NULL)
1665 return -ENOMEM;
Eric Miao3c42a442008-04-30 00:52:26 -07001666
Eric Miao77e19672008-12-16 11:54:34 +08001667 fbi->video_mem_phys = virt_to_phys(fbi->video_mem);
1668 fbi->video_mem_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Eric Miao77e19672008-12-16 11:54:34 +08001670 fbi->fb.fix.smem_start = fbi->video_mem_phys;
1671 fbi->fb.fix.smem_len = fbi->video_mem_size;
1672 fbi->fb.screen_base = fbi->video_mem;
Eric Miao3c42a442008-04-30 00:52:26 -07001673
Eric Miao77e19672008-12-16 11:54:34 +08001674 return fbi->video_mem ? 0 : -ENOMEM;
eric miao84f43c32008-04-30 00:52:22 -07001675}
1676
Guennadi Liakhovetskiebdf9822008-05-05 15:31:44 +01001677static void pxafb_decode_mach_info(struct pxafb_info *fbi,
1678 struct pxafb_mach_info *inf)
eric miao84f43c32008-04-30 00:52:22 -07001679{
1680 unsigned int lcd_conn = inf->lcd_conn;
Eric Miao77e19672008-12-16 11:54:34 +08001681 struct pxafb_mode_info *m;
1682 int i;
eric miao84f43c32008-04-30 00:52:22 -07001683
1684 fbi->cmap_inverse = inf->cmap_inverse;
1685 fbi->cmap_static = inf->cmap_static;
Eric Miaoa0427502008-12-18 22:10:00 +08001686 fbi->lccr4 = inf->lccr4;
eric miao84f43c32008-04-30 00:52:22 -07001687
Eric Miao1ec26db2008-11-11 21:45:57 +08001688 switch (lcd_conn & LCD_TYPE_MASK) {
eric miao84f43c32008-04-30 00:52:22 -07001689 case LCD_TYPE_MONO_STN:
1690 fbi->lccr0 = LCCR0_CMS;
1691 break;
1692 case LCD_TYPE_MONO_DSTN:
1693 fbi->lccr0 = LCCR0_CMS | LCCR0_SDS;
1694 break;
1695 case LCD_TYPE_COLOR_STN:
1696 fbi->lccr0 = 0;
1697 break;
1698 case LCD_TYPE_COLOR_DSTN:
1699 fbi->lccr0 = LCCR0_SDS;
1700 break;
1701 case LCD_TYPE_COLOR_TFT:
1702 fbi->lccr0 = LCCR0_PAS;
1703 break;
1704 case LCD_TYPE_SMART_PANEL:
1705 fbi->lccr0 = LCCR0_LCDT | LCCR0_PAS;
1706 break;
1707 default:
1708 /* fall back to backward compatibility way */
1709 fbi->lccr0 = inf->lccr0;
1710 fbi->lccr3 = inf->lccr3;
Guennadi Liakhovetskiebdf9822008-05-05 15:31:44 +01001711 goto decode_mode;
eric miao84f43c32008-04-30 00:52:22 -07001712 }
1713
1714 if (lcd_conn == LCD_MONO_STN_8BPP)
1715 fbi->lccr0 |= LCCR0_DPD;
1716
Eric Miao9a1ac7e2008-08-15 02:50:44 -04001717 fbi->lccr0 |= (lcd_conn & LCD_ALTERNATE_MAPPING) ? LCCR0_LDDALT : 0;
1718
eric miao84f43c32008-04-30 00:52:22 -07001719 fbi->lccr3 = LCCR3_Acb((inf->lcd_conn >> 10) & 0xff);
1720 fbi->lccr3 |= (lcd_conn & LCD_BIAS_ACTIVE_LOW) ? LCCR3_OEP : 0;
1721 fbi->lccr3 |= (lcd_conn & LCD_PCLK_EDGE_FALL) ? LCCR3_PCP : 0;
1722
Guennadi Liakhovetskiebdf9822008-05-05 15:31:44 +01001723decode_mode:
Eric Miao77e19672008-12-16 11:54:34 +08001724 pxafb_setmode(&fbi->fb.var, &inf->modes[0]);
1725
1726 /* decide video memory size as follows:
1727 * 1. default to mode of maximum resolution
1728 * 2. allow platform to override
1729 * 3. allow module parameter to override
1730 */
1731 for (i = 0, m = &inf->modes[0]; i < inf->num_modes; i++, m++)
1732 fbi->video_mem_size = max_t(size_t, fbi->video_mem_size,
1733 m->xres * m->yres * m->bpp / 8);
1734
1735 if (inf->video_mem_size > fbi->video_mem_size)
1736 fbi->video_mem_size = inf->video_mem_size;
1737
1738 if (video_mem_size > fbi->video_mem_size)
1739 fbi->video_mem_size = video_mem_size;
eric miao84f43c32008-04-30 00:52:22 -07001740}
1741
Jaya Kumar9e6c2972008-06-22 04:27:27 +01001742static struct pxafb_info * __devinit pxafb_init_fbinfo(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
1744 struct pxafb_info *fbi;
1745 void *addr;
1746 struct pxafb_mach_info *inf = dev->platform_data;
1747
1748 /* Alloc the pxafb_info and pseudo_palette in one step */
1749 fbi = kmalloc(sizeof(struct pxafb_info) + sizeof(u32) * 16, GFP_KERNEL);
1750 if (!fbi)
1751 return NULL;
1752
1753 memset(fbi, 0, sizeof(struct pxafb_info));
1754 fbi->dev = dev;
1755
Russell Kinge0d8b132008-11-11 17:52:32 +00001756 fbi->clk = clk_get(dev, NULL);
Russell King72e35242007-08-20 10:18:42 +01001757 if (IS_ERR(fbi->clk)) {
1758 kfree(fbi);
1759 return NULL;
1760 }
1761
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 strcpy(fbi->fb.fix.id, PXA_NAME);
1763
1764 fbi->fb.fix.type = FB_TYPE_PACKED_PIXELS;
1765 fbi->fb.fix.type_aux = 0;
1766 fbi->fb.fix.xpanstep = 0;
Eric Miao7e4b19c2008-12-17 14:56:54 +08001767 fbi->fb.fix.ypanstep = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 fbi->fb.fix.ywrapstep = 0;
1769 fbi->fb.fix.accel = FB_ACCEL_NONE;
1770
1771 fbi->fb.var.nonstd = 0;
1772 fbi->fb.var.activate = FB_ACTIVATE_NOW;
1773 fbi->fb.var.height = -1;
1774 fbi->fb.var.width = -1;
Eric Miao7e4b19c2008-12-17 14:56:54 +08001775 fbi->fb.var.accel_flags = FB_ACCELF_TEXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 fbi->fb.var.vmode = FB_VMODE_NONINTERLACED;
1777
1778 fbi->fb.fbops = &pxafb_ops;
1779 fbi->fb.flags = FBINFO_DEFAULT;
1780 fbi->fb.node = -1;
1781
1782 addr = fbi;
1783 addr = addr + sizeof(struct pxafb_info);
1784 fbi->fb.pseudo_palette = addr;
1785
eric miaob0086ef2008-04-30 00:52:19 -07001786 fbi->state = C_STARTUP;
1787 fbi->task_state = (u_char)-1;
Richard Purdied14b2722006-09-20 22:54:21 +01001788
eric miao84f43c32008-04-30 00:52:22 -07001789 pxafb_decode_mach_info(fbi, inf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
1791 init_waitqueue_head(&fbi->ctrlr_wait);
David Howells6d5aefb2006-12-05 19:36:26 +00001792 INIT_WORK(&fbi->task, pxafb_task);
Matthias Kaehlckeb91dbce2008-07-23 21:31:14 -07001793 mutex_init(&fbi->ctrlr_lock);
Eric Miao2ba162b2008-04-30 00:52:24 -07001794 init_completion(&fbi->disable_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
1796 return fbi;
1797}
1798
1799#ifdef CONFIG_FB_PXA_PARAMETERS
Jaya Kumar9e6c2972008-06-22 04:27:27 +01001800static int __devinit parse_opt_mode(struct device *dev, const char *this_opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
1802 struct pxafb_mach_info *inf = dev->platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
eric miao817daf12008-04-30 00:52:18 -07001804 const char *name = this_opt+5;
1805 unsigned int namelen = strlen(name);
1806 int res_specified = 0, bpp_specified = 0;
1807 unsigned int xres = 0, yres = 0, bpp = 0;
1808 int yres_specified = 0;
1809 int i;
1810 for (i = namelen-1; i >= 0; i--) {
1811 switch (name[i]) {
1812 case '-':
1813 namelen = i;
1814 if (!bpp_specified && !yres_specified) {
1815 bpp = simple_strtoul(&name[i+1], NULL, 0);
1816 bpp_specified = 1;
1817 } else
1818 goto done;
1819 break;
1820 case 'x':
1821 if (!yres_specified) {
1822 yres = simple_strtoul(&name[i+1], NULL, 0);
1823 yres_specified = 1;
1824 } else
1825 goto done;
1826 break;
1827 case '0' ... '9':
1828 break;
1829 default:
1830 goto done;
1831 }
1832 }
1833 if (i < 0 && yres_specified) {
1834 xres = simple_strtoul(name, NULL, 0);
1835 res_specified = 1;
1836 }
1837done:
1838 if (res_specified) {
1839 dev_info(dev, "overriding resolution: %dx%d\n", xres, yres);
1840 inf->modes[0].xres = xres; inf->modes[0].yres = yres;
1841 }
1842 if (bpp_specified)
1843 switch (bpp) {
1844 case 1:
1845 case 2:
1846 case 4:
1847 case 8:
1848 case 16:
1849 inf->modes[0].bpp = bpp;
1850 dev_info(dev, "overriding bit depth: %d\n", bpp);
1851 break;
1852 default:
1853 dev_err(dev, "Depth %d is not valid\n", bpp);
1854 return -EINVAL;
1855 }
1856 return 0;
1857}
1858
Jaya Kumar9e6c2972008-06-22 04:27:27 +01001859static int __devinit parse_opt(struct device *dev, char *this_opt)
eric miao817daf12008-04-30 00:52:18 -07001860{
1861 struct pxafb_mach_info *inf = dev->platform_data;
1862 struct pxafb_mode_info *mode = &inf->modes[0];
1863 char s[64];
1864
1865 s[0] = '\0';
1866
Eric Miao77e19672008-12-16 11:54:34 +08001867 if (!strncmp(this_opt, "vmem:", 5)) {
1868 video_mem_size = memparse(this_opt + 5, NULL);
1869 } else if (!strncmp(this_opt, "mode:", 5)) {
eric miao817daf12008-04-30 00:52:18 -07001870 return parse_opt_mode(dev, this_opt);
1871 } else if (!strncmp(this_opt, "pixclock:", 9)) {
1872 mode->pixclock = simple_strtoul(this_opt+9, NULL, 0);
1873 sprintf(s, "pixclock: %ld\n", mode->pixclock);
1874 } else if (!strncmp(this_opt, "left:", 5)) {
1875 mode->left_margin = simple_strtoul(this_opt+5, NULL, 0);
1876 sprintf(s, "left: %u\n", mode->left_margin);
1877 } else if (!strncmp(this_opt, "right:", 6)) {
1878 mode->right_margin = simple_strtoul(this_opt+6, NULL, 0);
1879 sprintf(s, "right: %u\n", mode->right_margin);
1880 } else if (!strncmp(this_opt, "upper:", 6)) {
1881 mode->upper_margin = simple_strtoul(this_opt+6, NULL, 0);
1882 sprintf(s, "upper: %u\n", mode->upper_margin);
1883 } else if (!strncmp(this_opt, "lower:", 6)) {
1884 mode->lower_margin = simple_strtoul(this_opt+6, NULL, 0);
1885 sprintf(s, "lower: %u\n", mode->lower_margin);
1886 } else if (!strncmp(this_opt, "hsynclen:", 9)) {
1887 mode->hsync_len = simple_strtoul(this_opt+9, NULL, 0);
1888 sprintf(s, "hsynclen: %u\n", mode->hsync_len);
1889 } else if (!strncmp(this_opt, "vsynclen:", 9)) {
1890 mode->vsync_len = simple_strtoul(this_opt+9, NULL, 0);
1891 sprintf(s, "vsynclen: %u\n", mode->vsync_len);
1892 } else if (!strncmp(this_opt, "hsync:", 6)) {
1893 if (simple_strtoul(this_opt+6, NULL, 0) == 0) {
1894 sprintf(s, "hsync: Active Low\n");
1895 mode->sync &= ~FB_SYNC_HOR_HIGH_ACT;
1896 } else {
1897 sprintf(s, "hsync: Active High\n");
1898 mode->sync |= FB_SYNC_HOR_HIGH_ACT;
1899 }
1900 } else if (!strncmp(this_opt, "vsync:", 6)) {
1901 if (simple_strtoul(this_opt+6, NULL, 0) == 0) {
1902 sprintf(s, "vsync: Active Low\n");
1903 mode->sync &= ~FB_SYNC_VERT_HIGH_ACT;
1904 } else {
1905 sprintf(s, "vsync: Active High\n");
1906 mode->sync |= FB_SYNC_VERT_HIGH_ACT;
1907 }
1908 } else if (!strncmp(this_opt, "dpc:", 4)) {
1909 if (simple_strtoul(this_opt+4, NULL, 0) == 0) {
1910 sprintf(s, "double pixel clock: false\n");
1911 inf->lccr3 &= ~LCCR3_DPC;
1912 } else {
1913 sprintf(s, "double pixel clock: true\n");
1914 inf->lccr3 |= LCCR3_DPC;
1915 }
1916 } else if (!strncmp(this_opt, "outputen:", 9)) {
1917 if (simple_strtoul(this_opt+9, NULL, 0) == 0) {
1918 sprintf(s, "output enable: active low\n");
1919 inf->lccr3 = (inf->lccr3 & ~LCCR3_OEP) | LCCR3_OutEnL;
1920 } else {
1921 sprintf(s, "output enable: active high\n");
1922 inf->lccr3 = (inf->lccr3 & ~LCCR3_OEP) | LCCR3_OutEnH;
1923 }
1924 } else if (!strncmp(this_opt, "pixclockpol:", 12)) {
1925 if (simple_strtoul(this_opt+12, NULL, 0) == 0) {
1926 sprintf(s, "pixel clock polarity: falling edge\n");
1927 inf->lccr3 = (inf->lccr3 & ~LCCR3_PCP) | LCCR3_PixFlEdg;
1928 } else {
1929 sprintf(s, "pixel clock polarity: rising edge\n");
1930 inf->lccr3 = (inf->lccr3 & ~LCCR3_PCP) | LCCR3_PixRsEdg;
1931 }
1932 } else if (!strncmp(this_opt, "color", 5)) {
1933 inf->lccr0 = (inf->lccr0 & ~LCCR0_CMS) | LCCR0_Color;
1934 } else if (!strncmp(this_opt, "mono", 4)) {
1935 inf->lccr0 = (inf->lccr0 & ~LCCR0_CMS) | LCCR0_Mono;
1936 } else if (!strncmp(this_opt, "active", 6)) {
1937 inf->lccr0 = (inf->lccr0 & ~LCCR0_PAS) | LCCR0_Act;
1938 } else if (!strncmp(this_opt, "passive", 7)) {
1939 inf->lccr0 = (inf->lccr0 & ~LCCR0_PAS) | LCCR0_Pas;
1940 } else if (!strncmp(this_opt, "single", 6)) {
1941 inf->lccr0 = (inf->lccr0 & ~LCCR0_SDS) | LCCR0_Sngl;
1942 } else if (!strncmp(this_opt, "dual", 4)) {
1943 inf->lccr0 = (inf->lccr0 & ~LCCR0_SDS) | LCCR0_Dual;
1944 } else if (!strncmp(this_opt, "4pix", 4)) {
1945 inf->lccr0 = (inf->lccr0 & ~LCCR0_DPD) | LCCR0_4PixMono;
1946 } else if (!strncmp(this_opt, "8pix", 4)) {
1947 inf->lccr0 = (inf->lccr0 & ~LCCR0_DPD) | LCCR0_8PixMono;
1948 } else {
1949 dev_err(dev, "unknown option: %s\n", this_opt);
1950 return -EINVAL;
1951 }
1952
1953 if (s[0] != '\0')
1954 dev_info(dev, "override %s", s);
1955
1956 return 0;
1957}
1958
Jaya Kumar9e6c2972008-06-22 04:27:27 +01001959static int __devinit pxafb_parse_options(struct device *dev, char *options)
eric miao817daf12008-04-30 00:52:18 -07001960{
1961 char *this_opt;
1962 int ret;
1963
1964 if (!options || !*options)
1965 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
1967 dev_dbg(dev, "options are \"%s\"\n", options ? options : "null");
1968
1969 /* could be made table driven or similar?... */
eric miao817daf12008-04-30 00:52:18 -07001970 while ((this_opt = strsep(&options, ",")) != NULL) {
1971 ret = parse_opt(dev, this_opt);
1972 if (ret)
1973 return ret;
1974 }
1975 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976}
eric miao92ac73c2008-04-30 00:52:20 -07001977
1978static char g_options[256] __devinitdata = "";
1979
Jaya Kumarf1edfc42008-06-22 04:27:25 +01001980#ifndef MODULE
Jaya Kumar9e6c2972008-06-22 04:27:27 +01001981static int __init pxafb_setup_options(void)
eric miao92ac73c2008-04-30 00:52:20 -07001982{
1983 char *options = NULL;
1984
1985 if (fb_get_options("pxafb", &options))
1986 return -ENODEV;
1987
1988 if (options)
1989 strlcpy(g_options, options, sizeof(g_options));
1990
1991 return 0;
1992}
1993#else
1994#define pxafb_setup_options() (0)
1995
1996module_param_string(options, g_options, sizeof(g_options), 0);
1997MODULE_PARM_DESC(options, "LCD parameters (see Documentation/fb/pxafb.txt)");
1998#endif
1999
2000#else
2001#define pxafb_parse_options(...) (0)
2002#define pxafb_setup_options() (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003#endif
2004
Eric Miao4f3e2662008-08-16 03:50:51 -04002005#ifdef DEBUG_VAR
2006/* Check for various illegal bit-combinations. Currently only
2007 * a warning is given. */
2008static void __devinit pxafb_check_options(struct device *dev,
2009 struct pxafb_mach_info *inf)
2010{
2011 if (inf->lcd_conn)
2012 return;
2013
2014 if (inf->lccr0 & LCCR0_INVALID_CONFIG_MASK)
2015 dev_warn(dev, "machine LCCR0 setting contains "
2016 "illegal bits: %08x\n",
2017 inf->lccr0 & LCCR0_INVALID_CONFIG_MASK);
2018 if (inf->lccr3 & LCCR3_INVALID_CONFIG_MASK)
2019 dev_warn(dev, "machine LCCR3 setting contains "
2020 "illegal bits: %08x\n",
2021 inf->lccr3 & LCCR3_INVALID_CONFIG_MASK);
2022 if (inf->lccr0 & LCCR0_DPD &&
2023 ((inf->lccr0 & LCCR0_PAS) != LCCR0_Pas ||
2024 (inf->lccr0 & LCCR0_SDS) != LCCR0_Sngl ||
2025 (inf->lccr0 & LCCR0_CMS) != LCCR0_Mono))
2026 dev_warn(dev, "Double Pixel Data (DPD) mode is "
2027 "only valid in passive mono"
2028 " single panel mode\n");
2029 if ((inf->lccr0 & LCCR0_PAS) == LCCR0_Act &&
2030 (inf->lccr0 & LCCR0_SDS) == LCCR0_Dual)
2031 dev_warn(dev, "Dual panel only valid in passive mode\n");
2032 if ((inf->lccr0 & LCCR0_PAS) == LCCR0_Pas &&
2033 (inf->modes->upper_margin || inf->modes->lower_margin))
2034 dev_warn(dev, "Upper and lower margins must be 0 in "
2035 "passive mode\n");
2036}
2037#else
2038#define pxafb_check_options(...) do {} while (0)
2039#endif
2040
Jaya Kumar9e6c2972008-06-22 04:27:27 +01002041static int __devinit pxafb_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
2043 struct pxafb_info *fbi;
2044 struct pxafb_mach_info *inf;
eric miaoce4fb7b2008-04-30 00:52:21 -07002045 struct resource *r;
2046 int irq, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
Richard Purdie2cbbb3b2006-03-31 02:31:53 -08002048 dev_dbg(&dev->dev, "pxafb_probe\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Russell King3ae5eae2005-11-09 22:32:44 +00002050 inf = dev->dev.platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 ret = -ENOMEM;
2052 fbi = NULL;
2053 if (!inf)
2054 goto failed;
2055
Russell King3ae5eae2005-11-09 22:32:44 +00002056 ret = pxafb_parse_options(&dev->dev, g_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 if (ret < 0)
2058 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Eric Miao4f3e2662008-08-16 03:50:51 -04002060 pxafb_check_options(&dev->dev, inf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
eric miaob0086ef2008-04-30 00:52:19 -07002062 dev_dbg(&dev->dev, "got a %dx%dx%d LCD\n",
2063 inf->modes->xres,
2064 inf->modes->yres,
2065 inf->modes->bpp);
2066 if (inf->modes->xres == 0 ||
2067 inf->modes->yres == 0 ||
2068 inf->modes->bpp == 0) {
Russell King3ae5eae2005-11-09 22:32:44 +00002069 dev_err(&dev->dev, "Invalid resolution or bit depth\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 ret = -EINVAL;
2071 goto failed;
2072 }
Eric Miaoa5718a12008-11-11 21:50:39 +08002073
Russell King3ae5eae2005-11-09 22:32:44 +00002074 fbi = pxafb_init_fbinfo(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 if (!fbi) {
eric miaob0086ef2008-04-30 00:52:19 -07002076 /* only reason for pxafb_init_fbinfo to fail is kmalloc */
Russell King3ae5eae2005-11-09 22:32:44 +00002077 dev_err(&dev->dev, "Failed to initialize framebuffer device\n");
eric miaob0086ef2008-04-30 00:52:19 -07002078 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 goto failed;
2080 }
2081
Eric Miaoa5718a12008-11-11 21:50:39 +08002082 fbi->backlight_power = inf->pxafb_backlight_power;
2083 fbi->lcd_power = inf->pxafb_lcd_power;
2084
eric miaoce4fb7b2008-04-30 00:52:21 -07002085 r = platform_get_resource(dev, IORESOURCE_MEM, 0);
2086 if (r == NULL) {
2087 dev_err(&dev->dev, "no I/O memory resource defined\n");
2088 ret = -ENODEV;
Jaya Kumaree984762008-06-22 04:27:26 +01002089 goto failed_fbi;
eric miaoce4fb7b2008-04-30 00:52:21 -07002090 }
2091
2092 r = request_mem_region(r->start, r->end - r->start + 1, dev->name);
2093 if (r == NULL) {
2094 dev_err(&dev->dev, "failed to request I/O memory\n");
2095 ret = -EBUSY;
Jaya Kumaree984762008-06-22 04:27:26 +01002096 goto failed_fbi;
eric miaoce4fb7b2008-04-30 00:52:21 -07002097 }
2098
2099 fbi->mmio_base = ioremap(r->start, r->end - r->start + 1);
2100 if (fbi->mmio_base == NULL) {
2101 dev_err(&dev->dev, "failed to map I/O memory\n");
2102 ret = -EBUSY;
2103 goto failed_free_res;
2104 }
2105
Eric Miao77e19672008-12-16 11:54:34 +08002106 fbi->dma_buff_size = PAGE_ALIGN(sizeof(struct pxafb_dma_buff));
2107 fbi->dma_buff = dma_alloc_coherent(fbi->dev, fbi->dma_buff_size,
2108 &fbi->dma_buff_phys, GFP_KERNEL);
2109 if (fbi->dma_buff == NULL) {
2110 dev_err(&dev->dev, "failed to allocate memory for DMA\n");
2111 ret = -ENOMEM;
2112 goto failed_free_io;
2113 }
2114
2115 ret = pxafb_init_video_memory(fbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 if (ret) {
Russell King3ae5eae2005-11-09 22:32:44 +00002117 dev_err(&dev->dev, "Failed to allocate video RAM: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 ret = -ENOMEM;
Eric Miao77e19672008-12-16 11:54:34 +08002119 goto failed_free_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
eric miaoce4fb7b2008-04-30 00:52:21 -07002122 irq = platform_get_irq(dev, 0);
2123 if (irq < 0) {
2124 dev_err(&dev->dev, "no IRQ defined\n");
2125 ret = -ENODEV;
2126 goto failed_free_mem;
2127 }
2128
2129 ret = request_irq(irq, pxafb_handle_irq, IRQF_DISABLED, "LCD", fbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 if (ret) {
Russell King3ae5eae2005-11-09 22:32:44 +00002131 dev_err(&dev->dev, "request_irq failed: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 ret = -EBUSY;
eric miaoce4fb7b2008-04-30 00:52:21 -07002133 goto failed_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 }
2135
Eric Miao3c42a442008-04-30 00:52:26 -07002136 ret = pxafb_smart_init(fbi);
2137 if (ret) {
2138 dev_err(&dev->dev, "failed to initialize smartpanel\n");
2139 goto failed_free_irq;
2140 }
Eric Miao07df1c42008-12-04 11:14:11 +08002141
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 /*
2143 * This makes sure that our colour bitfield
2144 * descriptors are correctly initialised.
2145 */
Jaya Kumaree984762008-06-22 04:27:26 +01002146 ret = pxafb_check_var(&fbi->fb.var, &fbi->fb);
2147 if (ret) {
2148 dev_err(&dev->dev, "failed to get suitable mode\n");
2149 goto failed_free_irq;
2150 }
2151
2152 ret = pxafb_set_par(&fbi->fb);
2153 if (ret) {
2154 dev_err(&dev->dev, "Failed to set parameters\n");
2155 goto failed_free_irq;
2156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Russell King3ae5eae2005-11-09 22:32:44 +00002158 platform_set_drvdata(dev, fbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
2160 ret = register_framebuffer(&fbi->fb);
2161 if (ret < 0) {
eric miaob0086ef2008-04-30 00:52:19 -07002162 dev_err(&dev->dev,
2163 "Failed to register framebuffer device: %d\n", ret);
Jaya Kumaree984762008-06-22 04:27:26 +01002164 goto failed_free_cmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 }
2166
Eric Miao198fc102008-12-23 17:49:43 +08002167 pxafb_overlay_init(fbi);
2168
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169#ifdef CONFIG_CPU_FREQ
2170 fbi->freq_transition.notifier_call = pxafb_freq_transition;
2171 fbi->freq_policy.notifier_call = pxafb_freq_policy;
eric miaob0086ef2008-04-30 00:52:19 -07002172 cpufreq_register_notifier(&fbi->freq_transition,
2173 CPUFREQ_TRANSITION_NOTIFIER);
2174 cpufreq_register_notifier(&fbi->freq_policy,
2175 CPUFREQ_POLICY_NOTIFIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176#endif
2177
2178 /*
2179 * Ok, now enable the LCD controller
2180 */
2181 set_ctrlr_state(fbi, C_ENABLE);
2182
2183 return 0;
2184
Jaya Kumaree984762008-06-22 04:27:26 +01002185failed_free_cmap:
2186 if (fbi->fb.cmap.len)
2187 fb_dealloc_cmap(&fbi->fb.cmap);
eric miaoce4fb7b2008-04-30 00:52:21 -07002188failed_free_irq:
2189 free_irq(irq, fbi);
eric miaoce4fb7b2008-04-30 00:52:21 -07002190failed_free_mem:
Eric Miao77e19672008-12-16 11:54:34 +08002191 free_pages_exact(fbi->video_mem, fbi->video_mem_size);
2192failed_free_dma:
2193 dma_free_coherent(&dev->dev, fbi->dma_buff_size,
2194 fbi->dma_buff, fbi->dma_buff_phys);
Jaya Kumaree984762008-06-22 04:27:26 +01002195failed_free_io:
2196 iounmap(fbi->mmio_base);
2197failed_free_res:
2198 release_mem_region(r->start, r->end - r->start + 1);
2199failed_fbi:
2200 clk_put(fbi->clk);
Russell King3ae5eae2005-11-09 22:32:44 +00002201 platform_set_drvdata(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 kfree(fbi);
Jaya Kumaree984762008-06-22 04:27:26 +01002203failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 return ret;
2205}
2206
Jaya Kumar9f17f282008-06-22 04:27:28 +01002207static int __devexit pxafb_remove(struct platform_device *dev)
2208{
2209 struct pxafb_info *fbi = platform_get_drvdata(dev);
2210 struct resource *r;
2211 int irq;
2212 struct fb_info *info;
2213
2214 if (!fbi)
2215 return 0;
2216
2217 info = &fbi->fb;
2218
Eric Miao198fc102008-12-23 17:49:43 +08002219 pxafb_overlay_exit(fbi);
Jaya Kumar9f17f282008-06-22 04:27:28 +01002220 unregister_framebuffer(info);
2221
2222 pxafb_disable_controller(fbi);
2223
2224 if (fbi->fb.cmap.len)
2225 fb_dealloc_cmap(&fbi->fb.cmap);
2226
2227 irq = platform_get_irq(dev, 0);
2228 free_irq(irq, fbi);
2229
Eric Miao77e19672008-12-16 11:54:34 +08002230 free_pages_exact(fbi->video_mem, fbi->video_mem_size);
2231
2232 dma_free_writecombine(&dev->dev, fbi->dma_buff_size,
2233 fbi->dma_buff, fbi->dma_buff_phys);
Jaya Kumar9f17f282008-06-22 04:27:28 +01002234
2235 iounmap(fbi->mmio_base);
2236
2237 r = platform_get_resource(dev, IORESOURCE_MEM, 0);
2238 release_mem_region(r->start, r->end - r->start + 1);
2239
2240 clk_put(fbi->clk);
2241 kfree(fbi);
2242
2243 return 0;
2244}
2245
Russell King3ae5eae2005-11-09 22:32:44 +00002246static struct platform_driver pxafb_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 .probe = pxafb_probe,
Russell Kingbdf602b2009-03-03 13:43:47 +00002248 .remove = __devexit_p(pxafb_remove),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 .suspend = pxafb_suspend,
2250 .resume = pxafb_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00002251 .driver = {
Jaya Kumar9f17f282008-06-22 04:27:28 +01002252 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00002253 .name = "pxa2xx-fb",
2254 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255};
2256
Jaya Kumar9e6c2972008-06-22 04:27:27 +01002257static int __init pxafb_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258{
eric miao92ac73c2008-04-30 00:52:20 -07002259 if (pxafb_setup_options())
2260 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
Russell King3ae5eae2005-11-09 22:32:44 +00002262 return platform_driver_register(&pxafb_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263}
2264
Jaya Kumar9f17f282008-06-22 04:27:28 +01002265static void __exit pxafb_exit(void)
2266{
2267 platform_driver_unregister(&pxafb_driver);
2268}
2269
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270module_init(pxafb_init);
Jaya Kumar9f17f282008-06-22 04:27:28 +01002271module_exit(pxafb_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
2273MODULE_DESCRIPTION("loadable framebuffer driver for PXA");
2274MODULE_LICENSE("GPL");