blob: 1faf52642f5fc81e94d99fef0c3bdfc45a428792 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/video/pxafb.c
3 *
4 * Copyright (C) 1999 Eric A. Thomas.
5 * Copyright (C) 2004 Jean-Frederic Clere.
6 * Copyright (C) 2004 Ian Campbell.
7 * Copyright (C) 2004 Jeff Lackey.
8 * Based on sa1100fb.c Copyright (C) 1999 Eric A. Thomas
9 * which in turn is
10 * Based on acornfb.c Copyright (C) Russell King.
11 *
12 * This file is subject to the terms and conditions of the GNU General Public
13 * License. See the file COPYING in the main directory of this archive for
14 * more details.
15 *
16 * Intel PXA250/210 LCD Controller Frame Buffer Driver
17 *
18 * Please direct your questions and comments on this driver to the following
19 * email address:
20 *
21 * linux-arm-kernel@lists.arm.linux.org.uk
22 *
23 */
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/kernel.h>
28#include <linux/sched.h>
29#include <linux/errno.h>
30#include <linux/string.h>
31#include <linux/interrupt.h>
32#include <linux/slab.h>
Andrea Righi27ac7922008-07-23 21:28:13 -070033#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/fb.h>
35#include <linux/delay.h>
36#include <linux/init.h>
37#include <linux/ioport.h>
38#include <linux/cpufreq.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010039#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/dma-mapping.h>
Russell King72e35242007-08-20 10:18:42 +010041#include <linux/clk.h>
42#include <linux/err.h>
Eric Miao2ba162b2008-04-30 00:52:24 -070043#include <linux/completion.h>
Matthias Kaehlckeb91dbce2008-07-23 21:31:14 -070044#include <linux/mutex.h>
Eric Miao3c42a442008-04-30 00:52:26 -070045#include <linux/kthread.h>
46#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Russell Kinga09e64f2008-08-05 16:14:15 +010048#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/io.h>
50#include <asm/irq.h>
Nicolas Pitrebf1b8ab2005-06-23 21:56:45 +010051#include <asm/div64.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010052#include <mach/pxa-regs.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010053#include <mach/bitfield.h>
54#include <mach/pxafb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/*
57 * Complain if VAR is out of range.
58 */
59#define DEBUG_VAR 1
60
61#include "pxafb.h"
62
63/* Bits which should not be set in machine configuration structures */
eric miaob0086ef2008-04-30 00:52:19 -070064#define LCCR0_INVALID_CONFIG_MASK (LCCR0_OUM | LCCR0_BM | LCCR0_QDM |\
65 LCCR0_DIS | LCCR0_EFM | LCCR0_IUM |\
66 LCCR0_SFM | LCCR0_LDM | LCCR0_ENB)
67
68#define LCCR3_INVALID_CONFIG_MASK (LCCR3_HSP | LCCR3_VSP |\
Eric Miao878f5782008-12-18 22:36:26 +080069 LCCR3_PCD | LCCR3_BPP(0xf))
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
eric miaob0086ef2008-04-30 00:52:19 -070071static int pxafb_activate_var(struct fb_var_screeninfo *var,
72 struct pxafb_info *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static void set_ctrlr_state(struct pxafb_info *fbi, u_int state);
Eric Miao6e354842008-12-17 16:50:43 +080074static void setup_base_frame(struct pxafb_info *fbi, int branch);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Eric Miao77e19672008-12-16 11:54:34 +080076static unsigned long video_mem_size = 0;
77
Eric Miaoa7535ba2008-04-30 00:52:24 -070078static inline unsigned long
79lcd_readl(struct pxafb_info *fbi, unsigned int off)
80{
81 return __raw_readl(fbi->mmio_base + off);
82}
83
84static inline void
85lcd_writel(struct pxafb_info *fbi, unsigned int off, unsigned long val)
86{
87 __raw_writel(val, fbi->mmio_base + off);
88}
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static inline void pxafb_schedule_work(struct pxafb_info *fbi, u_int state)
91{
92 unsigned long flags;
93
94 local_irq_save(flags);
95 /*
96 * We need to handle two requests being made at the same time.
97 * There are two important cases:
eric miaob0086ef2008-04-30 00:52:19 -070098 * 1. When we are changing VT (C_REENABLE) while unblanking
99 * (C_ENABLE) We must perform the unblanking, which will
100 * do our REENABLE for us.
101 * 2. When we are blanking, but immediately unblank before
102 * we have blanked. We do the "REENABLE" thing here as
103 * well, just to be sure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 */
105 if (fbi->task_state == C_ENABLE && state == C_REENABLE)
106 state = (u_int) -1;
107 if (fbi->task_state == C_DISABLE && state == C_ENABLE)
108 state = C_REENABLE;
109
110 if (state != (u_int)-1) {
111 fbi->task_state = state;
112 schedule_work(&fbi->task);
113 }
114 local_irq_restore(flags);
115}
116
117static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
118{
119 chan &= 0xffff;
120 chan >>= 16 - bf->length;
121 return chan << bf->offset;
122}
123
124static int
125pxafb_setpalettereg(u_int regno, u_int red, u_int green, u_int blue,
126 u_int trans, struct fb_info *info)
127{
128 struct pxafb_info *fbi = (struct pxafb_info *)info;
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700129 u_int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700131 if (regno >= fbi->palette_size)
132 return 1;
133
134 if (fbi->fb.var.grayscale) {
135 fbi->palette_cpu[regno] = ((blue >> 8) & 0x00ff);
136 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700138
139 switch (fbi->lccr4 & LCCR4_PAL_FOR_MASK) {
140 case LCCR4_PAL_FOR_0:
141 val = ((red >> 0) & 0xf800);
142 val |= ((green >> 5) & 0x07e0);
143 val |= ((blue >> 11) & 0x001f);
144 fbi->palette_cpu[regno] = val;
145 break;
146 case LCCR4_PAL_FOR_1:
147 val = ((red << 8) & 0x00f80000);
148 val |= ((green >> 0) & 0x0000fc00);
149 val |= ((blue >> 8) & 0x000000f8);
eric miaob0086ef2008-04-30 00:52:19 -0700150 ((u32 *)(fbi->palette_cpu))[regno] = val;
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700151 break;
152 case LCCR4_PAL_FOR_2:
153 val = ((red << 8) & 0x00fc0000);
154 val |= ((green >> 0) & 0x0000fc00);
155 val |= ((blue >> 8) & 0x000000fc);
eric miaob0086ef2008-04-30 00:52:19 -0700156 ((u32 *)(fbi->palette_cpu))[regno] = val;
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700157 break;
Eric Miaoa0427502008-12-18 22:10:00 +0800158 case LCCR4_PAL_FOR_3:
159 val = ((red << 8) & 0x00ff0000);
160 val |= ((green >> 0) & 0x0000ff00);
161 val |= ((blue >> 8) & 0x000000ff);
162 ((u32 *)(fbi->palette_cpu))[regno] = val;
163 break;
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700164 }
165
166 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
169static int
170pxafb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
171 u_int trans, struct fb_info *info)
172{
173 struct pxafb_info *fbi = (struct pxafb_info *)info;
174 unsigned int val;
175 int ret = 1;
176
177 /*
178 * If inverse mode was selected, invert all the colours
179 * rather than the register number. The register number
180 * is what you poke into the framebuffer to produce the
181 * colour you requested.
182 */
183 if (fbi->cmap_inverse) {
184 red = 0xffff - red;
185 green = 0xffff - green;
186 blue = 0xffff - blue;
187 }
188
189 /*
190 * If greyscale is true, then we convert the RGB value
191 * to greyscale no matter what visual we are using.
192 */
193 if (fbi->fb.var.grayscale)
194 red = green = blue = (19595 * red + 38470 * green +
195 7471 * blue) >> 16;
196
197 switch (fbi->fb.fix.visual) {
198 case FB_VISUAL_TRUECOLOR:
199 /*
200 * 16-bit True Colour. We encode the RGB value
201 * according to the RGB bitfield information.
202 */
203 if (regno < 16) {
204 u32 *pal = fbi->fb.pseudo_palette;
205
206 val = chan_to_field(red, &fbi->fb.var.red);
207 val |= chan_to_field(green, &fbi->fb.var.green);
208 val |= chan_to_field(blue, &fbi->fb.var.blue);
209
210 pal[regno] = val;
211 ret = 0;
212 }
213 break;
214
215 case FB_VISUAL_STATIC_PSEUDOCOLOR:
216 case FB_VISUAL_PSEUDOCOLOR:
217 ret = pxafb_setpalettereg(regno, red, green, blue, trans, info);
218 break;
219 }
220
221 return ret;
222}
223
Eric Miao878f5782008-12-18 22:36:26 +0800224/* calculate pixel depth, transparency bit included, >=16bpp formats _only_ */
225static inline int var_to_depth(struct fb_var_screeninfo *var)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
Eric Miao878f5782008-12-18 22:36:26 +0800227 return var->red.length + var->green.length +
228 var->blue.length + var->transp.length;
229}
230
231/* calculate 4-bit BPP value for LCCR3 and OVLxC1 */
232static int pxafb_var_to_bpp(struct fb_var_screeninfo *var)
233{
234 int bpp = -EINVAL;
235
eric miaob0086ef2008-04-30 00:52:19 -0700236 switch (var->bits_per_pixel) {
Eric Miao878f5782008-12-18 22:36:26 +0800237 case 1: bpp = 0; break;
238 case 2: bpp = 1; break;
239 case 4: bpp = 2; break;
240 case 8: bpp = 3; break;
241 case 16: bpp = 4; break;
Stefan Schmidtc1450f12008-07-09 08:06:32 +0100242 case 24:
Eric Miao878f5782008-12-18 22:36:26 +0800243 switch (var_to_depth(var)) {
244 case 18: bpp = 6; break; /* 18-bits/pixel packed */
245 case 19: bpp = 8; break; /* 19-bits/pixel packed */
246 case 24: bpp = 9; break;
Stefan Schmidtc1450f12008-07-09 08:06:32 +0100247 }
248 break;
249 case 32:
Eric Miao878f5782008-12-18 22:36:26 +0800250 switch (var_to_depth(var)) {
251 case 18: bpp = 5; break; /* 18-bits/pixel unpacked */
252 case 19: bpp = 7; break; /* 19-bits/pixel unpacked */
253 case 25: bpp = 10; break;
Stefan Schmidtc1450f12008-07-09 08:06:32 +0100254 }
255 break;
eric miaob0086ef2008-04-30 00:52:19 -0700256 }
Eric Miao878f5782008-12-18 22:36:26 +0800257 return bpp;
258}
259
260/*
261 * pxafb_var_to_lccr3():
262 * Convert a bits per pixel value to the correct bit pattern for LCCR3
263 *
264 * NOTE: for PXA27x with overlays support, the LCCR3_PDFOR_x bits have an
265 * implication of the acutal use of transparency bit, which we handle it
266 * here separatedly. See PXA27x Developer's Manual, Section <<7.4.6 Pixel
267 * Formats>> for the valid combination of PDFOR, PAL_FOR for various BPP.
268 *
269 * Transparency for palette pixel formats is not supported at the moment.
270 */
271static uint32_t pxafb_var_to_lccr3(struct fb_var_screeninfo *var)
272{
273 int bpp = pxafb_var_to_bpp(var);
274 uint32_t lccr3;
275
276 if (bpp < 0)
277 return 0;
278
279 lccr3 = LCCR3_BPP(bpp);
280
281 switch (var_to_depth(var)) {
282 case 16: lccr3 |= var->transp.length ? LCCR3_PDFOR_3 : 0; break;
283 case 18: lccr3 |= LCCR3_PDFOR_3; break;
284 case 24: lccr3 |= var->transp.length ? LCCR3_PDFOR_2 : LCCR3_PDFOR_3;
285 break;
286 case 19:
287 case 25: lccr3 |= LCCR3_PDFOR_0; break;
288 }
289 return lccr3;
290}
291
292#define SET_PIXFMT(v, r, g, b, t) \
293({ \
294 (v)->transp.offset = (t) ? (r) + (g) + (b) : 0; \
295 (v)->transp.length = (t) ? (t) : 0; \
296 (v)->blue.length = (b); (v)->blue.offset = 0; \
297 (v)->green.length = (g); (v)->green.offset = (b); \
298 (v)->red.length = (r); (v)->red.offset = (b) + (g); \
299})
300
301/* set the RGBT bitfields of fb_var_screeninf according to
302 * var->bits_per_pixel and given depth
303 */
304static void pxafb_set_pixfmt(struct fb_var_screeninfo *var, int depth)
305{
306 if (depth == 0)
307 depth = var->bits_per_pixel;
308
309 if (var->bits_per_pixel < 16) {
310 /* indexed pixel formats */
311 var->red.offset = 0; var->red.length = 8;
312 var->green.offset = 0; var->green.length = 8;
313 var->blue.offset = 0; var->blue.length = 8;
314 var->transp.offset = 0; var->transp.length = 8;
315 }
316
317 switch (depth) {
318 case 16: var->transp.length ?
319 SET_PIXFMT(var, 5, 5, 5, 1) : /* RGBT555 */
320 SET_PIXFMT(var, 5, 6, 5, 0); break; /* RGB565 */
321 case 18: SET_PIXFMT(var, 6, 6, 6, 0); break; /* RGB666 */
322 case 19: SET_PIXFMT(var, 6, 6, 6, 1); break; /* RGBT666 */
323 case 24: var->transp.length ?
324 SET_PIXFMT(var, 8, 8, 7, 1) : /* RGBT887 */
325 SET_PIXFMT(var, 8, 8, 8, 0); break; /* RGB888 */
326 case 25: SET_PIXFMT(var, 8, 8, 8, 1); break; /* RGBT888 */
327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
330#ifdef CONFIG_CPU_FREQ
331/*
332 * pxafb_display_dma_period()
333 * Calculate the minimum period (in picoseconds) between two DMA
334 * requests for the LCD controller. If we hit this, it means we're
335 * doing nothing but LCD DMA.
336 */
337static unsigned int pxafb_display_dma_period(struct fb_var_screeninfo *var)
338{
eric miaob0086ef2008-04-30 00:52:19 -0700339 /*
340 * Period = pixclock * bits_per_byte * bytes_per_transfer
341 * / memory_bits_per_pixel;
342 */
343 return var->pixclock * 8 * 16 / var->bits_per_pixel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345#endif
346
347/*
Richard Purdied14b2722006-09-20 22:54:21 +0100348 * Select the smallest mode that allows the desired resolution to be
349 * displayed. If desired parameters can be rounded up.
350 */
eric miaob0086ef2008-04-30 00:52:19 -0700351static struct pxafb_mode_info *pxafb_getmode(struct pxafb_mach_info *mach,
352 struct fb_var_screeninfo *var)
Richard Purdied14b2722006-09-20 22:54:21 +0100353{
354 struct pxafb_mode_info *mode = NULL;
355 struct pxafb_mode_info *modelist = mach->modes;
356 unsigned int best_x = 0xffffffff, best_y = 0xffffffff;
357 unsigned int i;
358
eric miaob0086ef2008-04-30 00:52:19 -0700359 for (i = 0; i < mach->num_modes; i++) {
360 if (modelist[i].xres >= var->xres &&
361 modelist[i].yres >= var->yres &&
362 modelist[i].xres < best_x &&
363 modelist[i].yres < best_y &&
364 modelist[i].bpp >= var->bits_per_pixel) {
Richard Purdied14b2722006-09-20 22:54:21 +0100365 best_x = modelist[i].xres;
366 best_y = modelist[i].yres;
367 mode = &modelist[i];
368 }
369 }
370
371 return mode;
372}
373
eric miaob0086ef2008-04-30 00:52:19 -0700374static void pxafb_setmode(struct fb_var_screeninfo *var,
375 struct pxafb_mode_info *mode)
Richard Purdied14b2722006-09-20 22:54:21 +0100376{
377 var->xres = mode->xres;
378 var->yres = mode->yres;
379 var->bits_per_pixel = mode->bpp;
380 var->pixclock = mode->pixclock;
381 var->hsync_len = mode->hsync_len;
382 var->left_margin = mode->left_margin;
383 var->right_margin = mode->right_margin;
384 var->vsync_len = mode->vsync_len;
385 var->upper_margin = mode->upper_margin;
386 var->lower_margin = mode->lower_margin;
387 var->sync = mode->sync;
388 var->grayscale = mode->cmap_greyscale;
Eric Miao878f5782008-12-18 22:36:26 +0800389
390 /* set the initial RGBA bitfields */
391 pxafb_set_pixfmt(var, mode->depth);
Richard Purdied14b2722006-09-20 22:54:21 +0100392}
393
394/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 * pxafb_check_var():
396 * Get the video params out of 'var'. If a value doesn't fit, round it up,
397 * if it's too big, return -EINVAL.
398 *
399 * Round up in the following order: bits_per_pixel, xres,
400 * yres, xres_virtual, yres_virtual, xoffset, yoffset, grayscale,
401 * bitfields, horizontal timing, vertical timing.
402 */
403static int pxafb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
404{
405 struct pxafb_info *fbi = (struct pxafb_info *)info;
Richard Purdied14b2722006-09-20 22:54:21 +0100406 struct pxafb_mach_info *inf = fbi->dev->platform_data;
Eric Miao878f5782008-12-18 22:36:26 +0800407 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 if (var->xres < MIN_XRES)
410 var->xres = MIN_XRES;
411 if (var->yres < MIN_YRES)
412 var->yres = MIN_YRES;
Richard Purdied14b2722006-09-20 22:54:21 +0100413
414 if (inf->fixed_modes) {
415 struct pxafb_mode_info *mode;
416
417 mode = pxafb_getmode(inf, var);
418 if (!mode)
419 return -EINVAL;
420 pxafb_setmode(var, mode);
421 } else {
422 if (var->xres > inf->modes->xres)
423 return -EINVAL;
424 if (var->yres > inf->modes->yres)
425 return -EINVAL;
426 if (var->bits_per_pixel > inf->modes->bpp)
427 return -EINVAL;
428 }
429
Eric Miao7e4b19c2008-12-17 14:56:54 +0800430 /* we don't support xpan, force xres_virtual to be equal to xres */
431 var->xres_virtual = var->xres;
432
433 if (var->accel_flags & FB_ACCELF_TEXT)
434 var->yres_virtual = fbi->fb.fix.smem_len /
435 (var->xres_virtual * var->bits_per_pixel / 8);
436 else
437 var->yres_virtual = max(var->yres_virtual, var->yres);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Eric Miao878f5782008-12-18 22:36:26 +0800439 /* do a test conversion to BPP fields to check the color formats */
440 err = pxafb_var_to_bpp(var);
441 if (err < 0)
442 return err;
Stefan Schmidtc1450f12008-07-09 08:06:32 +0100443
Eric Miao878f5782008-12-18 22:36:26 +0800444 pxafb_set_pixfmt(var, var_to_depth(var));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446#ifdef CONFIG_CPU_FREQ
Russell King78d3cfd2008-05-17 22:51:14 +0100447 pr_debug("pxafb: dma period = %d ps\n",
448 pxafb_display_dma_period(var));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449#endif
450
451 return 0;
452}
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454/*
455 * pxafb_set_par():
456 * Set the user defined part of the display for the specified console
457 */
458static int pxafb_set_par(struct fb_info *info)
459{
460 struct pxafb_info *fbi = (struct pxafb_info *)info;
461 struct fb_var_screeninfo *var = &info->var;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Stefan Schmidtc1450f12008-07-09 08:06:32 +0100463 if (var->bits_per_pixel >= 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 fbi->fb.fix.visual = FB_VISUAL_TRUECOLOR;
465 else if (!fbi->cmap_static)
466 fbi->fb.fix.visual = FB_VISUAL_PSEUDOCOLOR;
467 else {
468 /*
469 * Some people have weird ideas about wanting static
470 * pseudocolor maps. I suspect their user space
471 * applications are broken.
472 */
473 fbi->fb.fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
474 }
475
476 fbi->fb.fix.line_length = var->xres_virtual *
477 var->bits_per_pixel / 8;
Stefan Schmidtc1450f12008-07-09 08:06:32 +0100478 if (var->bits_per_pixel >= 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 fbi->palette_size = 0;
480 else
eric miaob0086ef2008-04-30 00:52:19 -0700481 fbi->palette_size = var->bits_per_pixel == 1 ?
482 4 : 1 << var->bits_per_pixel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
eric miao2c42dd82008-04-30 00:52:21 -0700484 fbi->palette_cpu = (u16 *)&fbi->dma_buff->palette[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Stefan Schmidtc1450f12008-07-09 08:06:32 +0100486 if (fbi->fb.var.bits_per_pixel >= 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 fb_dealloc_cmap(&fbi->fb.cmap);
488 else
489 fb_alloc_cmap(&fbi->fb.cmap, 1<<fbi->fb.var.bits_per_pixel, 0);
490
491 pxafb_activate_var(var, fbi);
492
493 return 0;
494}
495
Eric Miao6e354842008-12-17 16:50:43 +0800496static int pxafb_pan_display(struct fb_var_screeninfo *var,
497 struct fb_info *info)
498{
499 struct pxafb_info *fbi = (struct pxafb_info *)info;
500 int dma = DMA_MAX + DMA_BASE;
501
502 if (fbi->state != C_ENABLE)
503 return 0;
504
505 setup_base_frame(fbi, 1);
506
507 if (fbi->lccr0 & LCCR0_SDS)
508 lcd_writel(fbi, FBR1, fbi->fdadr[dma + 1] | 0x1);
509
510 lcd_writel(fbi, FBR0, fbi->fdadr[dma] | 0x1);
511 return 0;
512}
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 * pxafb_blank():
516 * Blank the display by setting all palette values to zero. Note, the
517 * 16 bpp mode does not really use the palette, so this will not
518 * blank the display in all modes.
519 */
520static int pxafb_blank(int blank, struct fb_info *info)
521{
522 struct pxafb_info *fbi = (struct pxafb_info *)info;
523 int i;
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 switch (blank) {
526 case FB_BLANK_POWERDOWN:
527 case FB_BLANK_VSYNC_SUSPEND:
528 case FB_BLANK_HSYNC_SUSPEND:
529 case FB_BLANK_NORMAL:
530 if (fbi->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR ||
531 fbi->fb.fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
532 for (i = 0; i < fbi->palette_size; i++)
533 pxafb_setpalettereg(i, 0, 0, 0, 0, info);
534
535 pxafb_schedule_work(fbi, C_DISABLE);
eric miaob0086ef2008-04-30 00:52:19 -0700536 /* TODO if (pxafb_blank_helper) pxafb_blank_helper(blank); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 break;
538
539 case FB_BLANK_UNBLANK:
eric miaob0086ef2008-04-30 00:52:19 -0700540 /* TODO if (pxafb_blank_helper) pxafb_blank_helper(blank); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (fbi->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR ||
542 fbi->fb.fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
543 fb_set_cmap(&fbi->fb.cmap, info);
544 pxafb_schedule_work(fbi, C_ENABLE);
545 }
546 return 0;
547}
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549static struct fb_ops pxafb_ops = {
550 .owner = THIS_MODULE,
551 .fb_check_var = pxafb_check_var,
552 .fb_set_par = pxafb_set_par,
Eric Miao6e354842008-12-17 16:50:43 +0800553 .fb_pan_display = pxafb_pan_display,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 .fb_setcolreg = pxafb_setcolreg,
555 .fb_fillrect = cfb_fillrect,
556 .fb_copyarea = cfb_copyarea,
557 .fb_imageblit = cfb_imageblit,
558 .fb_blank = pxafb_blank,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559};
560
561/*
562 * Calculate the PCD value from the clock rate (in picoseconds).
563 * We take account of the PPCR clock setting.
564 * From PXA Developer's Manual:
565 *
566 * PixelClock = LCLK
567 * -------------
568 * 2 ( PCD + 1 )
569 *
570 * PCD = LCLK
571 * ------------- - 1
572 * 2(PixelClock)
573 *
574 * Where:
575 * LCLK = LCD/Memory Clock
576 * PCD = LCCR3[7:0]
577 *
578 * PixelClock here is in Hz while the pixclock argument given is the
579 * period in picoseconds. Hence PixelClock = 1 / ( pixclock * 10^-12 )
580 *
581 * The function get_lclk_frequency_10khz returns LCLK in units of
582 * 10khz. Calling the result of this function lclk gives us the
583 * following
584 *
585 * PCD = (lclk * 10^4 ) * ( pixclock * 10^-12 )
586 * -------------------------------------- - 1
587 * 2
588 *
589 * Factoring the 10^4 and 10^-12 out gives 10^-8 == 1 / 100000000 as used below.
590 */
eric miaob0086ef2008-04-30 00:52:19 -0700591static inline unsigned int get_pcd(struct pxafb_info *fbi,
592 unsigned int pixclock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
594 unsigned long long pcd;
595
596 /* FIXME: Need to take into account Double Pixel Clock mode
Russell King72e35242007-08-20 10:18:42 +0100597 * (DPC) bit? or perhaps set it based on the various clock
598 * speeds */
599 pcd = (unsigned long long)(clk_get_rate(fbi->clk) / 10000);
600 pcd *= pixclock;
Nicolas Pitrebf1b8ab2005-06-23 21:56:45 +0100601 do_div(pcd, 100000000 * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 /* no need for this, since we should subtract 1 anyway. they cancel */
603 /* pcd += 1; */ /* make up for integer math truncations */
604 return (unsigned int)pcd;
605}
606
607/*
Richard Purdieba44cd22005-09-09 13:10:03 -0700608 * Some touchscreens need hsync information from the video driver to
Russell King72e35242007-08-20 10:18:42 +0100609 * function correctly. We export it here. Note that 'hsync_time' and
610 * the value returned from pxafb_get_hsync_time() is the *reciprocal*
611 * of the hsync period in seconds.
Richard Purdieba44cd22005-09-09 13:10:03 -0700612 */
613static inline void set_hsync_time(struct pxafb_info *fbi, unsigned int pcd)
614{
Russell King72e35242007-08-20 10:18:42 +0100615 unsigned long htime;
Richard Purdieba44cd22005-09-09 13:10:03 -0700616
617 if ((pcd == 0) || (fbi->fb.var.hsync_len == 0)) {
eric miaob0086ef2008-04-30 00:52:19 -0700618 fbi->hsync_time = 0;
Richard Purdieba44cd22005-09-09 13:10:03 -0700619 return;
620 }
621
Russell King72e35242007-08-20 10:18:42 +0100622 htime = clk_get_rate(fbi->clk) / (pcd * fbi->fb.var.hsync_len);
623
Richard Purdieba44cd22005-09-09 13:10:03 -0700624 fbi->hsync_time = htime;
625}
626
627unsigned long pxafb_get_hsync_time(struct device *dev)
628{
629 struct pxafb_info *fbi = dev_get_drvdata(dev);
630
631 /* If display is blanked/suspended, hsync isn't active */
632 if (!fbi || (fbi->state != C_ENABLE))
633 return 0;
634
635 return fbi->hsync_time;
636}
637EXPORT_SYMBOL(pxafb_get_hsync_time);
638
eric miao2c42dd82008-04-30 00:52:21 -0700639static int setup_frame_dma(struct pxafb_info *fbi, int dma, int pal,
640 unsigned int offset, size_t size)
641{
642 struct pxafb_dma_descriptor *dma_desc, *pal_desc;
643 unsigned int dma_desc_off, pal_desc_off;
644
Eric Miao6e354842008-12-17 16:50:43 +0800645 if (dma < 0 || dma >= DMA_MAX * 2)
eric miao2c42dd82008-04-30 00:52:21 -0700646 return -EINVAL;
647
648 dma_desc = &fbi->dma_buff->dma_desc[dma];
649 dma_desc_off = offsetof(struct pxafb_dma_buff, dma_desc[dma]);
650
Eric Miao77e19672008-12-16 11:54:34 +0800651 dma_desc->fsadr = fbi->video_mem_phys + offset;
eric miao2c42dd82008-04-30 00:52:21 -0700652 dma_desc->fidr = 0;
653 dma_desc->ldcmd = size;
654
Eric Miao6e354842008-12-17 16:50:43 +0800655 if (pal < 0 || pal >= PAL_MAX * 2) {
eric miao2c42dd82008-04-30 00:52:21 -0700656 dma_desc->fdadr = fbi->dma_buff_phys + dma_desc_off;
657 fbi->fdadr[dma] = fbi->dma_buff_phys + dma_desc_off;
658 } else {
Jürgen Schindele62cfcf42008-06-11 19:56:06 +0100659 pal_desc = &fbi->dma_buff->pal_desc[pal];
660 pal_desc_off = offsetof(struct pxafb_dma_buff, pal_desc[pal]);
eric miao2c42dd82008-04-30 00:52:21 -0700661
662 pal_desc->fsadr = fbi->dma_buff_phys + pal * PALETTE_SIZE;
663 pal_desc->fidr = 0;
664
665 if ((fbi->lccr4 & LCCR4_PAL_FOR_MASK) == LCCR4_PAL_FOR_0)
666 pal_desc->ldcmd = fbi->palette_size * sizeof(u16);
667 else
668 pal_desc->ldcmd = fbi->palette_size * sizeof(u32);
669
670 pal_desc->ldcmd |= LDCMD_PAL;
671
672 /* flip back and forth between palette and frame buffer */
673 pal_desc->fdadr = fbi->dma_buff_phys + dma_desc_off;
674 dma_desc->fdadr = fbi->dma_buff_phys + pal_desc_off;
675 fbi->fdadr[dma] = fbi->dma_buff_phys + dma_desc_off;
676 }
677
678 return 0;
679}
680
Eric Miao6e354842008-12-17 16:50:43 +0800681static void setup_base_frame(struct pxafb_info *fbi, int branch)
682{
683 struct fb_var_screeninfo *var = &fbi->fb.var;
684 struct fb_fix_screeninfo *fix = &fbi->fb.fix;
685 unsigned int nbytes, offset;
686 int dma, pal, bpp = var->bits_per_pixel;
687
688 dma = DMA_BASE + (branch ? DMA_MAX : 0);
689 pal = (bpp >= 16) ? PAL_NONE : PAL_BASE + (branch ? PAL_MAX : 0);
690
691 nbytes = fix->line_length * var->yres;
692 offset = fix->line_length * var->yoffset;
693
694 if (fbi->lccr0 & LCCR0_SDS) {
695 nbytes = nbytes / 2;
696 setup_frame_dma(fbi, dma + 1, PAL_NONE, offset + nbytes, nbytes);
697 }
698
699 setup_frame_dma(fbi, dma, pal, offset, nbytes);
700}
701
Eric Miao3c42a442008-04-30 00:52:26 -0700702#ifdef CONFIG_FB_PXA_SMARTPANEL
703static int setup_smart_dma(struct pxafb_info *fbi)
704{
705 struct pxafb_dma_descriptor *dma_desc;
706 unsigned long dma_desc_off, cmd_buff_off;
707
708 dma_desc = &fbi->dma_buff->dma_desc[DMA_CMD];
709 dma_desc_off = offsetof(struct pxafb_dma_buff, dma_desc[DMA_CMD]);
710 cmd_buff_off = offsetof(struct pxafb_dma_buff, cmd_buff);
711
712 dma_desc->fdadr = fbi->dma_buff_phys + dma_desc_off;
713 dma_desc->fsadr = fbi->dma_buff_phys + cmd_buff_off;
714 dma_desc->fidr = 0;
715 dma_desc->ldcmd = fbi->n_smart_cmds * sizeof(uint16_t);
716
717 fbi->fdadr[DMA_CMD] = dma_desc->fdadr;
718 return 0;
719}
720
721int pxafb_smart_flush(struct fb_info *info)
722{
723 struct pxafb_info *fbi = container_of(info, struct pxafb_info, fb);
724 uint32_t prsr;
725 int ret = 0;
726
727 /* disable controller until all registers are set up */
728 lcd_writel(fbi, LCCR0, fbi->reg_lccr0 & ~LCCR0_ENB);
729
730 /* 1. make it an even number of commands to align on 32-bit boundary
731 * 2. add the interrupt command to the end of the chain so we can
732 * keep track of the end of the transfer
733 */
734
735 while (fbi->n_smart_cmds & 1)
736 fbi->smart_cmds[fbi->n_smart_cmds++] = SMART_CMD_NOOP;
737
738 fbi->smart_cmds[fbi->n_smart_cmds++] = SMART_CMD_INTERRUPT;
739 fbi->smart_cmds[fbi->n_smart_cmds++] = SMART_CMD_WAIT_FOR_VSYNC;
740 setup_smart_dma(fbi);
741
742 /* continue to execute next command */
743 prsr = lcd_readl(fbi, PRSR) | PRSR_ST_OK | PRSR_CON_NT;
744 lcd_writel(fbi, PRSR, prsr);
745
746 /* stop the processor in case it executed "wait for sync" cmd */
747 lcd_writel(fbi, CMDCR, 0x0001);
748
749 /* don't send interrupts for fifo underruns on channel 6 */
750 lcd_writel(fbi, LCCR5, LCCR5_IUM(6));
751
752 lcd_writel(fbi, LCCR1, fbi->reg_lccr1);
753 lcd_writel(fbi, LCCR2, fbi->reg_lccr2);
754 lcd_writel(fbi, LCCR3, fbi->reg_lccr3);
Eric Miaoa0427502008-12-18 22:10:00 +0800755 lcd_writel(fbi, LCCR4, fbi->reg_lccr4);
Eric Miao3c42a442008-04-30 00:52:26 -0700756 lcd_writel(fbi, FDADR0, fbi->fdadr[0]);
757 lcd_writel(fbi, FDADR6, fbi->fdadr[6]);
758
759 /* begin sending */
760 lcd_writel(fbi, LCCR0, fbi->reg_lccr0 | LCCR0_ENB);
761
762 if (wait_for_completion_timeout(&fbi->command_done, HZ/2) == 0) {
763 pr_warning("%s: timeout waiting for command done\n",
764 __func__);
765 ret = -ETIMEDOUT;
766 }
767
768 /* quick disable */
769 prsr = lcd_readl(fbi, PRSR) & ~(PRSR_ST_OK | PRSR_CON_NT);
770 lcd_writel(fbi, PRSR, prsr);
771 lcd_writel(fbi, LCCR0, fbi->reg_lccr0 & ~LCCR0_ENB);
772 lcd_writel(fbi, FDADR6, 0);
773 fbi->n_smart_cmds = 0;
774 return ret;
775}
776
777int pxafb_smart_queue(struct fb_info *info, uint16_t *cmds, int n_cmds)
778{
779 int i;
780 struct pxafb_info *fbi = container_of(info, struct pxafb_info, fb);
781
Eric Miao69bdea72008-12-08 18:46:00 +0800782 for (i = 0; i < n_cmds; i++, cmds++) {
783 /* if it is a software delay, flush and delay */
784 if ((*cmds & 0xff00) == SMART_CMD_DELAY) {
785 pxafb_smart_flush(info);
786 mdelay(*cmds & 0xff);
787 continue;
788 }
789
790 /* leave 2 commands for INTERRUPT and WAIT_FOR_SYNC */
Eric Miao3c42a442008-04-30 00:52:26 -0700791 if (fbi->n_smart_cmds == CMD_BUFF_SIZE - 8)
792 pxafb_smart_flush(info);
793
Eric Miao69bdea72008-12-08 18:46:00 +0800794 fbi->smart_cmds[fbi->n_smart_cmds++] = *cmds;
Eric Miao3c42a442008-04-30 00:52:26 -0700795 }
796
797 return 0;
798}
799
800static unsigned int __smart_timing(unsigned time_ns, unsigned long lcd_clk)
801{
802 unsigned int t = (time_ns * (lcd_clk / 1000000) / 1000);
803 return (t == 0) ? 1 : t;
804}
805
806static void setup_smart_timing(struct pxafb_info *fbi,
807 struct fb_var_screeninfo *var)
808{
809 struct pxafb_mach_info *inf = fbi->dev->platform_data;
810 struct pxafb_mode_info *mode = &inf->modes[0];
811 unsigned long lclk = clk_get_rate(fbi->clk);
812 unsigned t1, t2, t3, t4;
813
814 t1 = max(mode->a0csrd_set_hld, mode->a0cswr_set_hld);
815 t2 = max(mode->rd_pulse_width, mode->wr_pulse_width);
816 t3 = mode->op_hold_time;
817 t4 = mode->cmd_inh_time;
818
819 fbi->reg_lccr1 =
820 LCCR1_DisWdth(var->xres) |
821 LCCR1_BegLnDel(__smart_timing(t1, lclk)) |
822 LCCR1_EndLnDel(__smart_timing(t2, lclk)) |
823 LCCR1_HorSnchWdth(__smart_timing(t3, lclk));
824
825 fbi->reg_lccr2 = LCCR2_DisHght(var->yres);
Eric Miaoc1f99c22008-12-08 18:35:03 +0800826 fbi->reg_lccr3 = fbi->lccr3 | LCCR3_PixClkDiv(__smart_timing(t4, lclk));
827 fbi->reg_lccr3 |= (var->sync & FB_SYNC_HOR_HIGH_ACT) ? LCCR3_HSP : 0;
828 fbi->reg_lccr3 |= (var->sync & FB_SYNC_VERT_HIGH_ACT) ? LCCR3_VSP : 0;
Eric Miao3c42a442008-04-30 00:52:26 -0700829
830 /* FIXME: make this configurable */
831 fbi->reg_cmdcr = 1;
832}
833
834static int pxafb_smart_thread(void *arg)
835{
Eric Miao7f1133c2008-04-30 00:52:27 -0700836 struct pxafb_info *fbi = arg;
Eric Miao3c42a442008-04-30 00:52:26 -0700837 struct pxafb_mach_info *inf = fbi->dev->platform_data;
838
839 if (!fbi || !inf->smart_update) {
840 pr_err("%s: not properly initialized, thread terminated\n",
841 __func__);
842 return -EINVAL;
843 }
844
845 pr_debug("%s(): task starting\n", __func__);
846
847 set_freezable();
848 while (!kthread_should_stop()) {
849
850 if (try_to_freeze())
851 continue;
852
Eric Miao07f651c2008-12-08 18:51:01 +0800853 mutex_lock(&fbi->ctrlr_lock);
854
Eric Miao3c42a442008-04-30 00:52:26 -0700855 if (fbi->state == C_ENABLE) {
856 inf->smart_update(&fbi->fb);
857 complete(&fbi->refresh_done);
858 }
859
Eric Miao07f651c2008-12-08 18:51:01 +0800860 mutex_unlock(&fbi->ctrlr_lock);
861
Eric Miao3c42a442008-04-30 00:52:26 -0700862 set_current_state(TASK_INTERRUPTIBLE);
863 schedule_timeout(30 * HZ / 1000);
864 }
865
866 pr_debug("%s(): task ending\n", __func__);
867 return 0;
868}
869
870static int pxafb_smart_init(struct pxafb_info *fbi)
871{
Eric Miao07df1c42008-12-04 11:14:11 +0800872 if (!(fbi->lccr0 & LCCR0_LCDT))
Eric Miao6cc4abe2008-11-11 21:47:07 +0800873 return 0;
874
Eric Miao07df1c42008-12-04 11:14:11 +0800875 fbi->smart_cmds = (uint16_t *) fbi->dma_buff->cmd_buff;
876 fbi->n_smart_cmds = 0;
877
878 init_completion(&fbi->command_done);
879 init_completion(&fbi->refresh_done);
880
Eric Miao3c42a442008-04-30 00:52:26 -0700881 fbi->smart_thread = kthread_run(pxafb_smart_thread, fbi,
882 "lcd_refresh");
883 if (IS_ERR(fbi->smart_thread)) {
Eric Miao07df1c42008-12-04 11:14:11 +0800884 pr_err("%s: unable to create kernel thread\n", __func__);
Eric Miao3c42a442008-04-30 00:52:26 -0700885 return PTR_ERR(fbi->smart_thread);
886 }
Eric Miaoa5718a12008-11-11 21:50:39 +0800887
Eric Miao3c42a442008-04-30 00:52:26 -0700888 return 0;
889}
890#else
891int pxafb_smart_queue(struct fb_info *info, uint16_t *cmds, int n_cmds)
892{
893 return 0;
894}
895
896int pxafb_smart_flush(struct fb_info *info)
897{
898 return 0;
899}
Eric Miao07df1c42008-12-04 11:14:11 +0800900
901static inline int pxafb_smart_init(struct pxafb_info *fbi) { return 0; }
902#endif /* CONFIG_FB_PXA_SMARTPANEL */
Eric Miao3c42a442008-04-30 00:52:26 -0700903
Eric Miao90eabbf2008-04-30 00:52:25 -0700904static void setup_parallel_timing(struct pxafb_info *fbi,
905 struct fb_var_screeninfo *var)
906{
907 unsigned int lines_per_panel, pcd = get_pcd(fbi, var->pixclock);
908
909 fbi->reg_lccr1 =
910 LCCR1_DisWdth(var->xres) +
911 LCCR1_HorSnchWdth(var->hsync_len) +
912 LCCR1_BegLnDel(var->left_margin) +
913 LCCR1_EndLnDel(var->right_margin);
914
915 /*
916 * If we have a dual scan LCD, we need to halve
917 * the YRES parameter.
918 */
919 lines_per_panel = var->yres;
920 if ((fbi->lccr0 & LCCR0_SDS) == LCCR0_Dual)
921 lines_per_panel /= 2;
922
923 fbi->reg_lccr2 =
924 LCCR2_DisHght(lines_per_panel) +
925 LCCR2_VrtSnchWdth(var->vsync_len) +
926 LCCR2_BegFrmDel(var->upper_margin) +
927 LCCR2_EndFrmDel(var->lower_margin);
928
929 fbi->reg_lccr3 = fbi->lccr3 |
930 (var->sync & FB_SYNC_HOR_HIGH_ACT ?
931 LCCR3_HorSnchH : LCCR3_HorSnchL) |
932 (var->sync & FB_SYNC_VERT_HIGH_ACT ?
933 LCCR3_VrtSnchH : LCCR3_VrtSnchL);
934
935 if (pcd) {
936 fbi->reg_lccr3 |= LCCR3_PixClkDiv(pcd);
937 set_hsync_time(fbi, pcd);
938 }
939}
940
Richard Purdieba44cd22005-09-09 13:10:03 -0700941/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 * pxafb_activate_var():
eric miaob0086ef2008-04-30 00:52:19 -0700943 * Configures LCD Controller based on entries in var parameter.
944 * Settings are only written to the controller if changes were made.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 */
eric miaob0086ef2008-04-30 00:52:19 -0700946static int pxafb_activate_var(struct fb_var_screeninfo *var,
947 struct pxafb_info *fbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 u_long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951#if DEBUG_VAR
Eric Miao3c42a442008-04-30 00:52:26 -0700952 if (!(fbi->lccr0 & LCCR0_LCDT)) {
953 if (var->xres < 16 || var->xres > 1024)
954 printk(KERN_ERR "%s: invalid xres %d\n",
955 fbi->fb.fix.id, var->xres);
956 switch (var->bits_per_pixel) {
957 case 1:
958 case 2:
959 case 4:
960 case 8:
961 case 16:
Stefan Schmidtc1450f12008-07-09 08:06:32 +0100962 case 24:
963 case 32:
Eric Miao3c42a442008-04-30 00:52:26 -0700964 break;
965 default:
966 printk(KERN_ERR "%s: invalid bit depth %d\n",
967 fbi->fb.fix.id, var->bits_per_pixel);
968 break;
969 }
970
971 if (var->hsync_len < 1 || var->hsync_len > 64)
972 printk(KERN_ERR "%s: invalid hsync_len %d\n",
973 fbi->fb.fix.id, var->hsync_len);
974 if (var->left_margin < 1 || var->left_margin > 255)
975 printk(KERN_ERR "%s: invalid left_margin %d\n",
976 fbi->fb.fix.id, var->left_margin);
977 if (var->right_margin < 1 || var->right_margin > 255)
978 printk(KERN_ERR "%s: invalid right_margin %d\n",
979 fbi->fb.fix.id, var->right_margin);
980 if (var->yres < 1 || var->yres > 1024)
981 printk(KERN_ERR "%s: invalid yres %d\n",
982 fbi->fb.fix.id, var->yres);
983 if (var->vsync_len < 1 || var->vsync_len > 64)
984 printk(KERN_ERR "%s: invalid vsync_len %d\n",
985 fbi->fb.fix.id, var->vsync_len);
986 if (var->upper_margin < 0 || var->upper_margin > 255)
987 printk(KERN_ERR "%s: invalid upper_margin %d\n",
988 fbi->fb.fix.id, var->upper_margin);
989 if (var->lower_margin < 0 || var->lower_margin > 255)
990 printk(KERN_ERR "%s: invalid lower_margin %d\n",
991 fbi->fb.fix.id, var->lower_margin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 /* Update shadow copy atomically */
995 local_irq_save(flags);
996
Eric Miao3c42a442008-04-30 00:52:26 -0700997#ifdef CONFIG_FB_PXA_SMARTPANEL
998 if (fbi->lccr0 & LCCR0_LCDT)
999 setup_smart_timing(fbi, var);
1000 else
1001#endif
1002 setup_parallel_timing(fbi, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Eric Miao6e354842008-12-17 16:50:43 +08001004 setup_base_frame(fbi, 0);
1005
Eric Miao90eabbf2008-04-30 00:52:25 -07001006 fbi->reg_lccr0 = fbi->lccr0 |
1007 (LCCR0_LDM | LCCR0_SFM | LCCR0_IUM | LCCR0_EFM |
1008 LCCR0_QDM | LCCR0_BM | LCCR0_OUM);
1009
Eric Miao878f5782008-12-18 22:36:26 +08001010 fbi->reg_lccr3 |= pxafb_var_to_lccr3(var);
Eric Miao90eabbf2008-04-30 00:52:25 -07001011
Eric Miaoa7535ba2008-04-30 00:52:24 -07001012 fbi->reg_lccr4 = lcd_readl(fbi, LCCR4) & ~LCCR4_PAL_FOR_MASK;
Hans J. Koch9ffa7392007-10-16 01:28:41 -07001013 fbi->reg_lccr4 |= (fbi->lccr4 & LCCR4_PAL_FOR_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 local_irq_restore(flags);
1015
1016 /*
1017 * Only update the registers if the controller is enabled
1018 * and something has changed.
1019 */
Eric Miaoa7535ba2008-04-30 00:52:24 -07001020 if ((lcd_readl(fbi, LCCR0) != fbi->reg_lccr0) ||
1021 (lcd_readl(fbi, LCCR1) != fbi->reg_lccr1) ||
1022 (lcd_readl(fbi, LCCR2) != fbi->reg_lccr2) ||
1023 (lcd_readl(fbi, LCCR3) != fbi->reg_lccr3) ||
Eric Miaoa0427502008-12-18 22:10:00 +08001024 (lcd_readl(fbi, LCCR4) != fbi->reg_lccr4) ||
Eric Miaoa7535ba2008-04-30 00:52:24 -07001025 (lcd_readl(fbi, FDADR0) != fbi->fdadr[0]) ||
1026 (lcd_readl(fbi, FDADR1) != fbi->fdadr[1]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 pxafb_schedule_work(fbi, C_REENABLE);
1028
1029 return 0;
1030}
1031
1032/*
1033 * NOTE! The following functions are purely helpers for set_ctrlr_state.
1034 * Do not call them directly; set_ctrlr_state does the correct serialisation
1035 * to ensure that things happen in the right way 100% of time time.
1036 * -- rmk
1037 */
1038static inline void __pxafb_backlight_power(struct pxafb_info *fbi, int on)
1039{
Russell Kingca5da712005-09-29 09:44:54 +01001040 pr_debug("pxafb: backlight o%s\n", on ? "n" : "ff");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Eric Miaoa5718a12008-11-11 21:50:39 +08001042 if (fbi->backlight_power)
1043 fbi->backlight_power(on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044}
1045
1046static inline void __pxafb_lcd_power(struct pxafb_info *fbi, int on)
1047{
Russell Kingca5da712005-09-29 09:44:54 +01001048 pr_debug("pxafb: LCD power o%s\n", on ? "n" : "ff");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Eric Miaoa5718a12008-11-11 21:50:39 +08001050 if (fbi->lcd_power)
1051 fbi->lcd_power(on, &fbi->fb.var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054static void pxafb_enable_controller(struct pxafb_info *fbi)
1055{
Russell Kingca5da712005-09-29 09:44:54 +01001056 pr_debug("pxafb: Enabling LCD controller\n");
eric miao2c42dd82008-04-30 00:52:21 -07001057 pr_debug("fdadr0 0x%08x\n", (unsigned int) fbi->fdadr[0]);
1058 pr_debug("fdadr1 0x%08x\n", (unsigned int) fbi->fdadr[1]);
Russell Kingca5da712005-09-29 09:44:54 +01001059 pr_debug("reg_lccr0 0x%08x\n", (unsigned int) fbi->reg_lccr0);
1060 pr_debug("reg_lccr1 0x%08x\n", (unsigned int) fbi->reg_lccr1);
1061 pr_debug("reg_lccr2 0x%08x\n", (unsigned int) fbi->reg_lccr2);
1062 pr_debug("reg_lccr3 0x%08x\n", (unsigned int) fbi->reg_lccr3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Nicolas Pitre8d372262005-08-10 16:45:13 +01001064 /* enable LCD controller clock */
Russell King72e35242007-08-20 10:18:42 +01001065 clk_enable(fbi->clk);
Nicolas Pitre8d372262005-08-10 16:45:13 +01001066
Eric Miao3c42a442008-04-30 00:52:26 -07001067 if (fbi->lccr0 & LCCR0_LCDT)
1068 return;
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 /* Sequence from 11.7.10 */
Eric Miaoa0427502008-12-18 22:10:00 +08001071 lcd_writel(fbi, LCCR4, fbi->reg_lccr4);
Eric Miaoa7535ba2008-04-30 00:52:24 -07001072 lcd_writel(fbi, LCCR3, fbi->reg_lccr3);
1073 lcd_writel(fbi, LCCR2, fbi->reg_lccr2);
1074 lcd_writel(fbi, LCCR1, fbi->reg_lccr1);
1075 lcd_writel(fbi, LCCR0, fbi->reg_lccr0 & ~LCCR0_ENB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Eric Miaoa7535ba2008-04-30 00:52:24 -07001077 lcd_writel(fbi, FDADR0, fbi->fdadr[0]);
1078 lcd_writel(fbi, FDADR1, fbi->fdadr[1]);
1079 lcd_writel(fbi, LCCR0, fbi->reg_lccr0 | LCCR0_ENB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
1082static void pxafb_disable_controller(struct pxafb_info *fbi)
1083{
eric miaoce4fb7b2008-04-30 00:52:21 -07001084 uint32_t lccr0;
1085
Eric Miao3c42a442008-04-30 00:52:26 -07001086#ifdef CONFIG_FB_PXA_SMARTPANEL
1087 if (fbi->lccr0 & LCCR0_LCDT) {
1088 wait_for_completion_timeout(&fbi->refresh_done,
1089 200 * HZ / 1000);
1090 return;
1091 }
1092#endif
1093
eric miaoce4fb7b2008-04-30 00:52:21 -07001094 /* Clear LCD Status Register */
Eric Miaoa7535ba2008-04-30 00:52:24 -07001095 lcd_writel(fbi, LCSR, 0xffffffff);
eric miaoce4fb7b2008-04-30 00:52:21 -07001096
Eric Miaoa7535ba2008-04-30 00:52:24 -07001097 lccr0 = lcd_readl(fbi, LCCR0) & ~LCCR0_LDM;
1098 lcd_writel(fbi, LCCR0, lccr0);
1099 lcd_writel(fbi, LCCR0, lccr0 | LCCR0_DIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Eric Miao2ba162b2008-04-30 00:52:24 -07001101 wait_for_completion_timeout(&fbi->disable_done, 200 * HZ / 1000);
Nicolas Pitre8d372262005-08-10 16:45:13 +01001102
1103 /* disable LCD controller clock */
Russell King72e35242007-08-20 10:18:42 +01001104 clk_disable(fbi->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
1107/*
1108 * pxafb_handle_irq: Handle 'LCD DONE' interrupts.
1109 */
David Howells7d12e782006-10-05 14:55:46 +01001110static irqreturn_t pxafb_handle_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
1112 struct pxafb_info *fbi = dev_id;
Eric Miaoa7535ba2008-04-30 00:52:24 -07001113 unsigned int lccr0, lcsr = lcd_readl(fbi, LCSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 if (lcsr & LCSR_LDD) {
Eric Miaoa7535ba2008-04-30 00:52:24 -07001116 lccr0 = lcd_readl(fbi, LCCR0);
1117 lcd_writel(fbi, LCCR0, lccr0 | LCCR0_LDM);
Eric Miao2ba162b2008-04-30 00:52:24 -07001118 complete(&fbi->disable_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 }
1120
Eric Miao3c42a442008-04-30 00:52:26 -07001121#ifdef CONFIG_FB_PXA_SMARTPANEL
1122 if (lcsr & LCSR_CMD_INT)
1123 complete(&fbi->command_done);
1124#endif
1125
Eric Miaoa7535ba2008-04-30 00:52:24 -07001126 lcd_writel(fbi, LCSR, lcsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return IRQ_HANDLED;
1128}
1129
1130/*
1131 * This function must be called from task context only, since it will
1132 * sleep when disabling the LCD controller, or if we get two contending
1133 * processes trying to alter state.
1134 */
1135static void set_ctrlr_state(struct pxafb_info *fbi, u_int state)
1136{
1137 u_int old_state;
1138
Matthias Kaehlckeb91dbce2008-07-23 21:31:14 -07001139 mutex_lock(&fbi->ctrlr_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 old_state = fbi->state;
1142
1143 /*
1144 * Hack around fbcon initialisation.
1145 */
1146 if (old_state == C_STARTUP && state == C_REENABLE)
1147 state = C_ENABLE;
1148
1149 switch (state) {
1150 case C_DISABLE_CLKCHANGE:
1151 /*
1152 * Disable controller for clock change. If the
1153 * controller is already disabled, then do nothing.
1154 */
1155 if (old_state != C_DISABLE && old_state != C_DISABLE_PM) {
1156 fbi->state = state;
eric miaob0086ef2008-04-30 00:52:19 -07001157 /* TODO __pxafb_lcd_power(fbi, 0); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 pxafb_disable_controller(fbi);
1159 }
1160 break;
1161
1162 case C_DISABLE_PM:
1163 case C_DISABLE:
1164 /*
1165 * Disable controller
1166 */
1167 if (old_state != C_DISABLE) {
1168 fbi->state = state;
1169 __pxafb_backlight_power(fbi, 0);
1170 __pxafb_lcd_power(fbi, 0);
1171 if (old_state != C_DISABLE_CLKCHANGE)
1172 pxafb_disable_controller(fbi);
1173 }
1174 break;
1175
1176 case C_ENABLE_CLKCHANGE:
1177 /*
1178 * Enable the controller after clock change. Only
1179 * do this if we were disabled for the clock change.
1180 */
1181 if (old_state == C_DISABLE_CLKCHANGE) {
1182 fbi->state = C_ENABLE;
1183 pxafb_enable_controller(fbi);
eric miaob0086ef2008-04-30 00:52:19 -07001184 /* TODO __pxafb_lcd_power(fbi, 1); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
1186 break;
1187
1188 case C_REENABLE:
1189 /*
1190 * Re-enable the controller only if it was already
1191 * enabled. This is so we reprogram the control
1192 * registers.
1193 */
1194 if (old_state == C_ENABLE) {
Richard Purdied14b2722006-09-20 22:54:21 +01001195 __pxafb_lcd_power(fbi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 pxafb_disable_controller(fbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 pxafb_enable_controller(fbi);
Richard Purdied14b2722006-09-20 22:54:21 +01001198 __pxafb_lcd_power(fbi, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 }
1200 break;
1201
1202 case C_ENABLE_PM:
1203 /*
1204 * Re-enable the controller after PM. This is not
1205 * perfect - think about the case where we were doing
1206 * a clock change, and we suspended half-way through.
1207 */
1208 if (old_state != C_DISABLE_PM)
1209 break;
1210 /* fall through */
1211
1212 case C_ENABLE:
1213 /*
1214 * Power up the LCD screen, enable controller, and
1215 * turn on the backlight.
1216 */
1217 if (old_state != C_ENABLE) {
1218 fbi->state = C_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 pxafb_enable_controller(fbi);
1220 __pxafb_lcd_power(fbi, 1);
1221 __pxafb_backlight_power(fbi, 1);
1222 }
1223 break;
1224 }
Matthias Kaehlckeb91dbce2008-07-23 21:31:14 -07001225 mutex_unlock(&fbi->ctrlr_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226}
1227
1228/*
1229 * Our LCD controller task (which is called when we blank or unblank)
1230 * via keventd.
1231 */
David Howells6d5aefb2006-12-05 19:36:26 +00001232static void pxafb_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
David Howells6d5aefb2006-12-05 19:36:26 +00001234 struct pxafb_info *fbi =
1235 container_of(work, struct pxafb_info, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 u_int state = xchg(&fbi->task_state, -1);
1237
1238 set_ctrlr_state(fbi, state);
1239}
1240
1241#ifdef CONFIG_CPU_FREQ
1242/*
1243 * CPU clock speed change handler. We need to adjust the LCD timing
1244 * parameters when the CPU clock is adjusted by the power management
1245 * subsystem.
1246 *
1247 * TODO: Determine why f->new != 10*get_lclk_frequency_10khz()
1248 */
1249static int
1250pxafb_freq_transition(struct notifier_block *nb, unsigned long val, void *data)
1251{
1252 struct pxafb_info *fbi = TO_INF(nb, freq_transition);
eric miaob0086ef2008-04-30 00:52:19 -07001253 /* TODO struct cpufreq_freqs *f = data; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 u_int pcd;
1255
1256 switch (val) {
1257 case CPUFREQ_PRECHANGE:
1258 set_ctrlr_state(fbi, C_DISABLE_CLKCHANGE);
1259 break;
1260
1261 case CPUFREQ_POSTCHANGE:
Russell King72e35242007-08-20 10:18:42 +01001262 pcd = get_pcd(fbi, fbi->fb.var.pixclock);
Richard Purdieba44cd22005-09-09 13:10:03 -07001263 set_hsync_time(fbi, pcd);
eric miaob0086ef2008-04-30 00:52:19 -07001264 fbi->reg_lccr3 = (fbi->reg_lccr3 & ~0xff) |
1265 LCCR3_PixClkDiv(pcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 set_ctrlr_state(fbi, C_ENABLE_CLKCHANGE);
1267 break;
1268 }
1269 return 0;
1270}
1271
1272static int
1273pxafb_freq_policy(struct notifier_block *nb, unsigned long val, void *data)
1274{
1275 struct pxafb_info *fbi = TO_INF(nb, freq_policy);
1276 struct fb_var_screeninfo *var = &fbi->fb.var;
1277 struct cpufreq_policy *policy = data;
1278
1279 switch (val) {
1280 case CPUFREQ_ADJUST:
1281 case CPUFREQ_INCOMPATIBLE:
Holger Schurigac2bf5b2008-02-11 16:52:30 +01001282 pr_debug("min dma period: %d ps, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 "new clock %d kHz\n", pxafb_display_dma_period(var),
1284 policy->max);
eric miaob0086ef2008-04-30 00:52:19 -07001285 /* TODO: fill in min/max values */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 }
1288 return 0;
1289}
1290#endif
1291
1292#ifdef CONFIG_PM
1293/*
1294 * Power management hooks. Note that we won't be called from IRQ context,
1295 * unlike the blank functions above, so we may sleep.
1296 */
Russell King3ae5eae2005-11-09 22:32:44 +00001297static int pxafb_suspend(struct platform_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298{
Russell King3ae5eae2005-11-09 22:32:44 +00001299 struct pxafb_info *fbi = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Russell King9480e302005-10-28 09:52:56 -07001301 set_ctrlr_state(fbi, C_DISABLE_PM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 return 0;
1303}
1304
Russell King3ae5eae2005-11-09 22:32:44 +00001305static int pxafb_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306{
Russell King3ae5eae2005-11-09 22:32:44 +00001307 struct pxafb_info *fbi = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Russell King9480e302005-10-28 09:52:56 -07001309 set_ctrlr_state(fbi, C_ENABLE_PM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 return 0;
1311}
1312#else
1313#define pxafb_suspend NULL
1314#define pxafb_resume NULL
1315#endif
1316
Eric Miao77e19672008-12-16 11:54:34 +08001317static int __devinit pxafb_init_video_memory(struct pxafb_info *fbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
Eric Miao77e19672008-12-16 11:54:34 +08001319 int size = PAGE_ALIGN(fbi->video_mem_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Eric Miao77e19672008-12-16 11:54:34 +08001321 fbi->video_mem = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
1322 if (fbi->video_mem == NULL)
1323 return -ENOMEM;
Eric Miao3c42a442008-04-30 00:52:26 -07001324
Eric Miao77e19672008-12-16 11:54:34 +08001325 fbi->video_mem_phys = virt_to_phys(fbi->video_mem);
1326 fbi->video_mem_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Eric Miao77e19672008-12-16 11:54:34 +08001328 fbi->fb.fix.smem_start = fbi->video_mem_phys;
1329 fbi->fb.fix.smem_len = fbi->video_mem_size;
1330 fbi->fb.screen_base = fbi->video_mem;
Eric Miao3c42a442008-04-30 00:52:26 -07001331
Eric Miao77e19672008-12-16 11:54:34 +08001332 return fbi->video_mem ? 0 : -ENOMEM;
eric miao84f43c32008-04-30 00:52:22 -07001333}
1334
Guennadi Liakhovetskiebdf9822008-05-05 15:31:44 +01001335static void pxafb_decode_mach_info(struct pxafb_info *fbi,
1336 struct pxafb_mach_info *inf)
eric miao84f43c32008-04-30 00:52:22 -07001337{
1338 unsigned int lcd_conn = inf->lcd_conn;
Eric Miao77e19672008-12-16 11:54:34 +08001339 struct pxafb_mode_info *m;
1340 int i;
eric miao84f43c32008-04-30 00:52:22 -07001341
1342 fbi->cmap_inverse = inf->cmap_inverse;
1343 fbi->cmap_static = inf->cmap_static;
Eric Miaoa0427502008-12-18 22:10:00 +08001344 fbi->lccr4 = inf->lccr4;
eric miao84f43c32008-04-30 00:52:22 -07001345
Eric Miao1ec26db2008-11-11 21:45:57 +08001346 switch (lcd_conn & LCD_TYPE_MASK) {
eric miao84f43c32008-04-30 00:52:22 -07001347 case LCD_TYPE_MONO_STN:
1348 fbi->lccr0 = LCCR0_CMS;
1349 break;
1350 case LCD_TYPE_MONO_DSTN:
1351 fbi->lccr0 = LCCR0_CMS | LCCR0_SDS;
1352 break;
1353 case LCD_TYPE_COLOR_STN:
1354 fbi->lccr0 = 0;
1355 break;
1356 case LCD_TYPE_COLOR_DSTN:
1357 fbi->lccr0 = LCCR0_SDS;
1358 break;
1359 case LCD_TYPE_COLOR_TFT:
1360 fbi->lccr0 = LCCR0_PAS;
1361 break;
1362 case LCD_TYPE_SMART_PANEL:
1363 fbi->lccr0 = LCCR0_LCDT | LCCR0_PAS;
1364 break;
1365 default:
1366 /* fall back to backward compatibility way */
1367 fbi->lccr0 = inf->lccr0;
1368 fbi->lccr3 = inf->lccr3;
Guennadi Liakhovetskiebdf9822008-05-05 15:31:44 +01001369 goto decode_mode;
eric miao84f43c32008-04-30 00:52:22 -07001370 }
1371
1372 if (lcd_conn == LCD_MONO_STN_8BPP)
1373 fbi->lccr0 |= LCCR0_DPD;
1374
Eric Miao9a1ac7e2008-08-15 02:50:44 -04001375 fbi->lccr0 |= (lcd_conn & LCD_ALTERNATE_MAPPING) ? LCCR0_LDDALT : 0;
1376
eric miao84f43c32008-04-30 00:52:22 -07001377 fbi->lccr3 = LCCR3_Acb((inf->lcd_conn >> 10) & 0xff);
1378 fbi->lccr3 |= (lcd_conn & LCD_BIAS_ACTIVE_LOW) ? LCCR3_OEP : 0;
1379 fbi->lccr3 |= (lcd_conn & LCD_PCLK_EDGE_FALL) ? LCCR3_PCP : 0;
1380
Guennadi Liakhovetskiebdf9822008-05-05 15:31:44 +01001381decode_mode:
Eric Miao77e19672008-12-16 11:54:34 +08001382 pxafb_setmode(&fbi->fb.var, &inf->modes[0]);
1383
1384 /* decide video memory size as follows:
1385 * 1. default to mode of maximum resolution
1386 * 2. allow platform to override
1387 * 3. allow module parameter to override
1388 */
1389 for (i = 0, m = &inf->modes[0]; i < inf->num_modes; i++, m++)
1390 fbi->video_mem_size = max_t(size_t, fbi->video_mem_size,
1391 m->xres * m->yres * m->bpp / 8);
1392
1393 if (inf->video_mem_size > fbi->video_mem_size)
1394 fbi->video_mem_size = inf->video_mem_size;
1395
1396 if (video_mem_size > fbi->video_mem_size)
1397 fbi->video_mem_size = video_mem_size;
eric miao84f43c32008-04-30 00:52:22 -07001398}
1399
Jaya Kumar9e6c2972008-06-22 04:27:27 +01001400static struct pxafb_info * __devinit pxafb_init_fbinfo(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
1402 struct pxafb_info *fbi;
1403 void *addr;
1404 struct pxafb_mach_info *inf = dev->platform_data;
1405
1406 /* Alloc the pxafb_info and pseudo_palette in one step */
1407 fbi = kmalloc(sizeof(struct pxafb_info) + sizeof(u32) * 16, GFP_KERNEL);
1408 if (!fbi)
1409 return NULL;
1410
1411 memset(fbi, 0, sizeof(struct pxafb_info));
1412 fbi->dev = dev;
1413
Russell King72e35242007-08-20 10:18:42 +01001414 fbi->clk = clk_get(dev, "LCDCLK");
1415 if (IS_ERR(fbi->clk)) {
1416 kfree(fbi);
1417 return NULL;
1418 }
1419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 strcpy(fbi->fb.fix.id, PXA_NAME);
1421
1422 fbi->fb.fix.type = FB_TYPE_PACKED_PIXELS;
1423 fbi->fb.fix.type_aux = 0;
1424 fbi->fb.fix.xpanstep = 0;
Eric Miao7e4b19c2008-12-17 14:56:54 +08001425 fbi->fb.fix.ypanstep = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 fbi->fb.fix.ywrapstep = 0;
1427 fbi->fb.fix.accel = FB_ACCEL_NONE;
1428
1429 fbi->fb.var.nonstd = 0;
1430 fbi->fb.var.activate = FB_ACTIVATE_NOW;
1431 fbi->fb.var.height = -1;
1432 fbi->fb.var.width = -1;
Eric Miao7e4b19c2008-12-17 14:56:54 +08001433 fbi->fb.var.accel_flags = FB_ACCELF_TEXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 fbi->fb.var.vmode = FB_VMODE_NONINTERLACED;
1435
1436 fbi->fb.fbops = &pxafb_ops;
1437 fbi->fb.flags = FBINFO_DEFAULT;
1438 fbi->fb.node = -1;
1439
1440 addr = fbi;
1441 addr = addr + sizeof(struct pxafb_info);
1442 fbi->fb.pseudo_palette = addr;
1443
eric miaob0086ef2008-04-30 00:52:19 -07001444 fbi->state = C_STARTUP;
1445 fbi->task_state = (u_char)-1;
Richard Purdied14b2722006-09-20 22:54:21 +01001446
eric miao84f43c32008-04-30 00:52:22 -07001447 pxafb_decode_mach_info(fbi, inf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 init_waitqueue_head(&fbi->ctrlr_wait);
David Howells6d5aefb2006-12-05 19:36:26 +00001450 INIT_WORK(&fbi->task, pxafb_task);
Matthias Kaehlckeb91dbce2008-07-23 21:31:14 -07001451 mutex_init(&fbi->ctrlr_lock);
Eric Miao2ba162b2008-04-30 00:52:24 -07001452 init_completion(&fbi->disable_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454 return fbi;
1455}
1456
1457#ifdef CONFIG_FB_PXA_PARAMETERS
Jaya Kumar9e6c2972008-06-22 04:27:27 +01001458static int __devinit parse_opt_mode(struct device *dev, const char *this_opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
1460 struct pxafb_mach_info *inf = dev->platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
eric miao817daf12008-04-30 00:52:18 -07001462 const char *name = this_opt+5;
1463 unsigned int namelen = strlen(name);
1464 int res_specified = 0, bpp_specified = 0;
1465 unsigned int xres = 0, yres = 0, bpp = 0;
1466 int yres_specified = 0;
1467 int i;
1468 for (i = namelen-1; i >= 0; i--) {
1469 switch (name[i]) {
1470 case '-':
1471 namelen = i;
1472 if (!bpp_specified && !yres_specified) {
1473 bpp = simple_strtoul(&name[i+1], NULL, 0);
1474 bpp_specified = 1;
1475 } else
1476 goto done;
1477 break;
1478 case 'x':
1479 if (!yres_specified) {
1480 yres = simple_strtoul(&name[i+1], NULL, 0);
1481 yres_specified = 1;
1482 } else
1483 goto done;
1484 break;
1485 case '0' ... '9':
1486 break;
1487 default:
1488 goto done;
1489 }
1490 }
1491 if (i < 0 && yres_specified) {
1492 xres = simple_strtoul(name, NULL, 0);
1493 res_specified = 1;
1494 }
1495done:
1496 if (res_specified) {
1497 dev_info(dev, "overriding resolution: %dx%d\n", xres, yres);
1498 inf->modes[0].xres = xres; inf->modes[0].yres = yres;
1499 }
1500 if (bpp_specified)
1501 switch (bpp) {
1502 case 1:
1503 case 2:
1504 case 4:
1505 case 8:
1506 case 16:
1507 inf->modes[0].bpp = bpp;
1508 dev_info(dev, "overriding bit depth: %d\n", bpp);
1509 break;
1510 default:
1511 dev_err(dev, "Depth %d is not valid\n", bpp);
1512 return -EINVAL;
1513 }
1514 return 0;
1515}
1516
Jaya Kumar9e6c2972008-06-22 04:27:27 +01001517static int __devinit parse_opt(struct device *dev, char *this_opt)
eric miao817daf12008-04-30 00:52:18 -07001518{
1519 struct pxafb_mach_info *inf = dev->platform_data;
1520 struct pxafb_mode_info *mode = &inf->modes[0];
1521 char s[64];
1522
1523 s[0] = '\0';
1524
Eric Miao77e19672008-12-16 11:54:34 +08001525 if (!strncmp(this_opt, "vmem:", 5)) {
1526 video_mem_size = memparse(this_opt + 5, NULL);
1527 } else if (!strncmp(this_opt, "mode:", 5)) {
eric miao817daf12008-04-30 00:52:18 -07001528 return parse_opt_mode(dev, this_opt);
1529 } else if (!strncmp(this_opt, "pixclock:", 9)) {
1530 mode->pixclock = simple_strtoul(this_opt+9, NULL, 0);
1531 sprintf(s, "pixclock: %ld\n", mode->pixclock);
1532 } else if (!strncmp(this_opt, "left:", 5)) {
1533 mode->left_margin = simple_strtoul(this_opt+5, NULL, 0);
1534 sprintf(s, "left: %u\n", mode->left_margin);
1535 } else if (!strncmp(this_opt, "right:", 6)) {
1536 mode->right_margin = simple_strtoul(this_opt+6, NULL, 0);
1537 sprintf(s, "right: %u\n", mode->right_margin);
1538 } else if (!strncmp(this_opt, "upper:", 6)) {
1539 mode->upper_margin = simple_strtoul(this_opt+6, NULL, 0);
1540 sprintf(s, "upper: %u\n", mode->upper_margin);
1541 } else if (!strncmp(this_opt, "lower:", 6)) {
1542 mode->lower_margin = simple_strtoul(this_opt+6, NULL, 0);
1543 sprintf(s, "lower: %u\n", mode->lower_margin);
1544 } else if (!strncmp(this_opt, "hsynclen:", 9)) {
1545 mode->hsync_len = simple_strtoul(this_opt+9, NULL, 0);
1546 sprintf(s, "hsynclen: %u\n", mode->hsync_len);
1547 } else if (!strncmp(this_opt, "vsynclen:", 9)) {
1548 mode->vsync_len = simple_strtoul(this_opt+9, NULL, 0);
1549 sprintf(s, "vsynclen: %u\n", mode->vsync_len);
1550 } else if (!strncmp(this_opt, "hsync:", 6)) {
1551 if (simple_strtoul(this_opt+6, NULL, 0) == 0) {
1552 sprintf(s, "hsync: Active Low\n");
1553 mode->sync &= ~FB_SYNC_HOR_HIGH_ACT;
1554 } else {
1555 sprintf(s, "hsync: Active High\n");
1556 mode->sync |= FB_SYNC_HOR_HIGH_ACT;
1557 }
1558 } else if (!strncmp(this_opt, "vsync:", 6)) {
1559 if (simple_strtoul(this_opt+6, NULL, 0) == 0) {
1560 sprintf(s, "vsync: Active Low\n");
1561 mode->sync &= ~FB_SYNC_VERT_HIGH_ACT;
1562 } else {
1563 sprintf(s, "vsync: Active High\n");
1564 mode->sync |= FB_SYNC_VERT_HIGH_ACT;
1565 }
1566 } else if (!strncmp(this_opt, "dpc:", 4)) {
1567 if (simple_strtoul(this_opt+4, NULL, 0) == 0) {
1568 sprintf(s, "double pixel clock: false\n");
1569 inf->lccr3 &= ~LCCR3_DPC;
1570 } else {
1571 sprintf(s, "double pixel clock: true\n");
1572 inf->lccr3 |= LCCR3_DPC;
1573 }
1574 } else if (!strncmp(this_opt, "outputen:", 9)) {
1575 if (simple_strtoul(this_opt+9, NULL, 0) == 0) {
1576 sprintf(s, "output enable: active low\n");
1577 inf->lccr3 = (inf->lccr3 & ~LCCR3_OEP) | LCCR3_OutEnL;
1578 } else {
1579 sprintf(s, "output enable: active high\n");
1580 inf->lccr3 = (inf->lccr3 & ~LCCR3_OEP) | LCCR3_OutEnH;
1581 }
1582 } else if (!strncmp(this_opt, "pixclockpol:", 12)) {
1583 if (simple_strtoul(this_opt+12, NULL, 0) == 0) {
1584 sprintf(s, "pixel clock polarity: falling edge\n");
1585 inf->lccr3 = (inf->lccr3 & ~LCCR3_PCP) | LCCR3_PixFlEdg;
1586 } else {
1587 sprintf(s, "pixel clock polarity: rising edge\n");
1588 inf->lccr3 = (inf->lccr3 & ~LCCR3_PCP) | LCCR3_PixRsEdg;
1589 }
1590 } else if (!strncmp(this_opt, "color", 5)) {
1591 inf->lccr0 = (inf->lccr0 & ~LCCR0_CMS) | LCCR0_Color;
1592 } else if (!strncmp(this_opt, "mono", 4)) {
1593 inf->lccr0 = (inf->lccr0 & ~LCCR0_CMS) | LCCR0_Mono;
1594 } else if (!strncmp(this_opt, "active", 6)) {
1595 inf->lccr0 = (inf->lccr0 & ~LCCR0_PAS) | LCCR0_Act;
1596 } else if (!strncmp(this_opt, "passive", 7)) {
1597 inf->lccr0 = (inf->lccr0 & ~LCCR0_PAS) | LCCR0_Pas;
1598 } else if (!strncmp(this_opt, "single", 6)) {
1599 inf->lccr0 = (inf->lccr0 & ~LCCR0_SDS) | LCCR0_Sngl;
1600 } else if (!strncmp(this_opt, "dual", 4)) {
1601 inf->lccr0 = (inf->lccr0 & ~LCCR0_SDS) | LCCR0_Dual;
1602 } else if (!strncmp(this_opt, "4pix", 4)) {
1603 inf->lccr0 = (inf->lccr0 & ~LCCR0_DPD) | LCCR0_4PixMono;
1604 } else if (!strncmp(this_opt, "8pix", 4)) {
1605 inf->lccr0 = (inf->lccr0 & ~LCCR0_DPD) | LCCR0_8PixMono;
1606 } else {
1607 dev_err(dev, "unknown option: %s\n", this_opt);
1608 return -EINVAL;
1609 }
1610
1611 if (s[0] != '\0')
1612 dev_info(dev, "override %s", s);
1613
1614 return 0;
1615}
1616
Jaya Kumar9e6c2972008-06-22 04:27:27 +01001617static int __devinit pxafb_parse_options(struct device *dev, char *options)
eric miao817daf12008-04-30 00:52:18 -07001618{
1619 char *this_opt;
1620 int ret;
1621
1622 if (!options || !*options)
1623 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
1625 dev_dbg(dev, "options are \"%s\"\n", options ? options : "null");
1626
1627 /* could be made table driven or similar?... */
eric miao817daf12008-04-30 00:52:18 -07001628 while ((this_opt = strsep(&options, ",")) != NULL) {
1629 ret = parse_opt(dev, this_opt);
1630 if (ret)
1631 return ret;
1632 }
1633 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634}
eric miao92ac73c2008-04-30 00:52:20 -07001635
1636static char g_options[256] __devinitdata = "";
1637
Jaya Kumarf1edfc42008-06-22 04:27:25 +01001638#ifndef MODULE
Jaya Kumar9e6c2972008-06-22 04:27:27 +01001639static int __init pxafb_setup_options(void)
eric miao92ac73c2008-04-30 00:52:20 -07001640{
1641 char *options = NULL;
1642
1643 if (fb_get_options("pxafb", &options))
1644 return -ENODEV;
1645
1646 if (options)
1647 strlcpy(g_options, options, sizeof(g_options));
1648
1649 return 0;
1650}
1651#else
1652#define pxafb_setup_options() (0)
1653
1654module_param_string(options, g_options, sizeof(g_options), 0);
1655MODULE_PARM_DESC(options, "LCD parameters (see Documentation/fb/pxafb.txt)");
1656#endif
1657
1658#else
1659#define pxafb_parse_options(...) (0)
1660#define pxafb_setup_options() (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661#endif
1662
Eric Miao4f3e2662008-08-16 03:50:51 -04001663#ifdef DEBUG_VAR
1664/* Check for various illegal bit-combinations. Currently only
1665 * a warning is given. */
1666static void __devinit pxafb_check_options(struct device *dev,
1667 struct pxafb_mach_info *inf)
1668{
1669 if (inf->lcd_conn)
1670 return;
1671
1672 if (inf->lccr0 & LCCR0_INVALID_CONFIG_MASK)
1673 dev_warn(dev, "machine LCCR0 setting contains "
1674 "illegal bits: %08x\n",
1675 inf->lccr0 & LCCR0_INVALID_CONFIG_MASK);
1676 if (inf->lccr3 & LCCR3_INVALID_CONFIG_MASK)
1677 dev_warn(dev, "machine LCCR3 setting contains "
1678 "illegal bits: %08x\n",
1679 inf->lccr3 & LCCR3_INVALID_CONFIG_MASK);
1680 if (inf->lccr0 & LCCR0_DPD &&
1681 ((inf->lccr0 & LCCR0_PAS) != LCCR0_Pas ||
1682 (inf->lccr0 & LCCR0_SDS) != LCCR0_Sngl ||
1683 (inf->lccr0 & LCCR0_CMS) != LCCR0_Mono))
1684 dev_warn(dev, "Double Pixel Data (DPD) mode is "
1685 "only valid in passive mono"
1686 " single panel mode\n");
1687 if ((inf->lccr0 & LCCR0_PAS) == LCCR0_Act &&
1688 (inf->lccr0 & LCCR0_SDS) == LCCR0_Dual)
1689 dev_warn(dev, "Dual panel only valid in passive mode\n");
1690 if ((inf->lccr0 & LCCR0_PAS) == LCCR0_Pas &&
1691 (inf->modes->upper_margin || inf->modes->lower_margin))
1692 dev_warn(dev, "Upper and lower margins must be 0 in "
1693 "passive mode\n");
1694}
1695#else
1696#define pxafb_check_options(...) do {} while (0)
1697#endif
1698
Jaya Kumar9e6c2972008-06-22 04:27:27 +01001699static int __devinit pxafb_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
1701 struct pxafb_info *fbi;
1702 struct pxafb_mach_info *inf;
eric miaoce4fb7b2008-04-30 00:52:21 -07001703 struct resource *r;
1704 int irq, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
Richard Purdie2cbbb3b2006-03-31 02:31:53 -08001706 dev_dbg(&dev->dev, "pxafb_probe\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Russell King3ae5eae2005-11-09 22:32:44 +00001708 inf = dev->dev.platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 ret = -ENOMEM;
1710 fbi = NULL;
1711 if (!inf)
1712 goto failed;
1713
Russell King3ae5eae2005-11-09 22:32:44 +00001714 ret = pxafb_parse_options(&dev->dev, g_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 if (ret < 0)
1716 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Eric Miao4f3e2662008-08-16 03:50:51 -04001718 pxafb_check_options(&dev->dev, inf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
eric miaob0086ef2008-04-30 00:52:19 -07001720 dev_dbg(&dev->dev, "got a %dx%dx%d LCD\n",
1721 inf->modes->xres,
1722 inf->modes->yres,
1723 inf->modes->bpp);
1724 if (inf->modes->xres == 0 ||
1725 inf->modes->yres == 0 ||
1726 inf->modes->bpp == 0) {
Russell King3ae5eae2005-11-09 22:32:44 +00001727 dev_err(&dev->dev, "Invalid resolution or bit depth\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 ret = -EINVAL;
1729 goto failed;
1730 }
Eric Miaoa5718a12008-11-11 21:50:39 +08001731
Russell King3ae5eae2005-11-09 22:32:44 +00001732 fbi = pxafb_init_fbinfo(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 if (!fbi) {
eric miaob0086ef2008-04-30 00:52:19 -07001734 /* only reason for pxafb_init_fbinfo to fail is kmalloc */
Russell King3ae5eae2005-11-09 22:32:44 +00001735 dev_err(&dev->dev, "Failed to initialize framebuffer device\n");
eric miaob0086ef2008-04-30 00:52:19 -07001736 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 goto failed;
1738 }
1739
Eric Miaoa5718a12008-11-11 21:50:39 +08001740 fbi->backlight_power = inf->pxafb_backlight_power;
1741 fbi->lcd_power = inf->pxafb_lcd_power;
1742
eric miaoce4fb7b2008-04-30 00:52:21 -07001743 r = platform_get_resource(dev, IORESOURCE_MEM, 0);
1744 if (r == NULL) {
1745 dev_err(&dev->dev, "no I/O memory resource defined\n");
1746 ret = -ENODEV;
Jaya Kumaree984762008-06-22 04:27:26 +01001747 goto failed_fbi;
eric miaoce4fb7b2008-04-30 00:52:21 -07001748 }
1749
1750 r = request_mem_region(r->start, r->end - r->start + 1, dev->name);
1751 if (r == NULL) {
1752 dev_err(&dev->dev, "failed to request I/O memory\n");
1753 ret = -EBUSY;
Jaya Kumaree984762008-06-22 04:27:26 +01001754 goto failed_fbi;
eric miaoce4fb7b2008-04-30 00:52:21 -07001755 }
1756
1757 fbi->mmio_base = ioremap(r->start, r->end - r->start + 1);
1758 if (fbi->mmio_base == NULL) {
1759 dev_err(&dev->dev, "failed to map I/O memory\n");
1760 ret = -EBUSY;
1761 goto failed_free_res;
1762 }
1763
Eric Miao77e19672008-12-16 11:54:34 +08001764 fbi->dma_buff_size = PAGE_ALIGN(sizeof(struct pxafb_dma_buff));
1765 fbi->dma_buff = dma_alloc_coherent(fbi->dev, fbi->dma_buff_size,
1766 &fbi->dma_buff_phys, GFP_KERNEL);
1767 if (fbi->dma_buff == NULL) {
1768 dev_err(&dev->dev, "failed to allocate memory for DMA\n");
1769 ret = -ENOMEM;
1770 goto failed_free_io;
1771 }
1772
1773 ret = pxafb_init_video_memory(fbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 if (ret) {
Russell King3ae5eae2005-11-09 22:32:44 +00001775 dev_err(&dev->dev, "Failed to allocate video RAM: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 ret = -ENOMEM;
Eric Miao77e19672008-12-16 11:54:34 +08001777 goto failed_free_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
eric miaoce4fb7b2008-04-30 00:52:21 -07001780 irq = platform_get_irq(dev, 0);
1781 if (irq < 0) {
1782 dev_err(&dev->dev, "no IRQ defined\n");
1783 ret = -ENODEV;
1784 goto failed_free_mem;
1785 }
1786
1787 ret = request_irq(irq, pxafb_handle_irq, IRQF_DISABLED, "LCD", fbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 if (ret) {
Russell King3ae5eae2005-11-09 22:32:44 +00001789 dev_err(&dev->dev, "request_irq failed: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 ret = -EBUSY;
eric miaoce4fb7b2008-04-30 00:52:21 -07001791 goto failed_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 }
1793
Eric Miao3c42a442008-04-30 00:52:26 -07001794 ret = pxafb_smart_init(fbi);
1795 if (ret) {
1796 dev_err(&dev->dev, "failed to initialize smartpanel\n");
1797 goto failed_free_irq;
1798 }
Eric Miao07df1c42008-12-04 11:14:11 +08001799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 /*
1801 * This makes sure that our colour bitfield
1802 * descriptors are correctly initialised.
1803 */
Jaya Kumaree984762008-06-22 04:27:26 +01001804 ret = pxafb_check_var(&fbi->fb.var, &fbi->fb);
1805 if (ret) {
1806 dev_err(&dev->dev, "failed to get suitable mode\n");
1807 goto failed_free_irq;
1808 }
1809
1810 ret = pxafb_set_par(&fbi->fb);
1811 if (ret) {
1812 dev_err(&dev->dev, "Failed to set parameters\n");
1813 goto failed_free_irq;
1814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Russell King3ae5eae2005-11-09 22:32:44 +00001816 platform_set_drvdata(dev, fbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
1818 ret = register_framebuffer(&fbi->fb);
1819 if (ret < 0) {
eric miaob0086ef2008-04-30 00:52:19 -07001820 dev_err(&dev->dev,
1821 "Failed to register framebuffer device: %d\n", ret);
Jaya Kumaree984762008-06-22 04:27:26 +01001822 goto failed_free_cmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 }
1824
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825#ifdef CONFIG_CPU_FREQ
1826 fbi->freq_transition.notifier_call = pxafb_freq_transition;
1827 fbi->freq_policy.notifier_call = pxafb_freq_policy;
eric miaob0086ef2008-04-30 00:52:19 -07001828 cpufreq_register_notifier(&fbi->freq_transition,
1829 CPUFREQ_TRANSITION_NOTIFIER);
1830 cpufreq_register_notifier(&fbi->freq_policy,
1831 CPUFREQ_POLICY_NOTIFIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832#endif
1833
1834 /*
1835 * Ok, now enable the LCD controller
1836 */
1837 set_ctrlr_state(fbi, C_ENABLE);
1838
1839 return 0;
1840
Jaya Kumaree984762008-06-22 04:27:26 +01001841failed_free_cmap:
1842 if (fbi->fb.cmap.len)
1843 fb_dealloc_cmap(&fbi->fb.cmap);
eric miaoce4fb7b2008-04-30 00:52:21 -07001844failed_free_irq:
1845 free_irq(irq, fbi);
eric miaoce4fb7b2008-04-30 00:52:21 -07001846failed_free_mem:
Eric Miao77e19672008-12-16 11:54:34 +08001847 free_pages_exact(fbi->video_mem, fbi->video_mem_size);
1848failed_free_dma:
1849 dma_free_coherent(&dev->dev, fbi->dma_buff_size,
1850 fbi->dma_buff, fbi->dma_buff_phys);
Jaya Kumaree984762008-06-22 04:27:26 +01001851failed_free_io:
1852 iounmap(fbi->mmio_base);
1853failed_free_res:
1854 release_mem_region(r->start, r->end - r->start + 1);
1855failed_fbi:
1856 clk_put(fbi->clk);
Russell King3ae5eae2005-11-09 22:32:44 +00001857 platform_set_drvdata(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 kfree(fbi);
Jaya Kumaree984762008-06-22 04:27:26 +01001859failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 return ret;
1861}
1862
Jaya Kumar9f17f282008-06-22 04:27:28 +01001863static int __devexit pxafb_remove(struct platform_device *dev)
1864{
1865 struct pxafb_info *fbi = platform_get_drvdata(dev);
1866 struct resource *r;
1867 int irq;
1868 struct fb_info *info;
1869
1870 if (!fbi)
1871 return 0;
1872
1873 info = &fbi->fb;
1874
1875 unregister_framebuffer(info);
1876
1877 pxafb_disable_controller(fbi);
1878
1879 if (fbi->fb.cmap.len)
1880 fb_dealloc_cmap(&fbi->fb.cmap);
1881
1882 irq = platform_get_irq(dev, 0);
1883 free_irq(irq, fbi);
1884
Eric Miao77e19672008-12-16 11:54:34 +08001885 free_pages_exact(fbi->video_mem, fbi->video_mem_size);
1886
1887 dma_free_writecombine(&dev->dev, fbi->dma_buff_size,
1888 fbi->dma_buff, fbi->dma_buff_phys);
Jaya Kumar9f17f282008-06-22 04:27:28 +01001889
1890 iounmap(fbi->mmio_base);
1891
1892 r = platform_get_resource(dev, IORESOURCE_MEM, 0);
1893 release_mem_region(r->start, r->end - r->start + 1);
1894
1895 clk_put(fbi->clk);
1896 kfree(fbi);
1897
1898 return 0;
1899}
1900
Russell King3ae5eae2005-11-09 22:32:44 +00001901static struct platform_driver pxafb_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 .probe = pxafb_probe,
Jaya Kumar9f17f282008-06-22 04:27:28 +01001903 .remove = pxafb_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 .suspend = pxafb_suspend,
1905 .resume = pxafb_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001906 .driver = {
Jaya Kumar9f17f282008-06-22 04:27:28 +01001907 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00001908 .name = "pxa2xx-fb",
1909 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910};
1911
Jaya Kumar9e6c2972008-06-22 04:27:27 +01001912static int __init pxafb_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
eric miao92ac73c2008-04-30 00:52:20 -07001914 if (pxafb_setup_options())
1915 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Russell King3ae5eae2005-11-09 22:32:44 +00001917 return platform_driver_register(&pxafb_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918}
1919
Jaya Kumar9f17f282008-06-22 04:27:28 +01001920static void __exit pxafb_exit(void)
1921{
1922 platform_driver_unregister(&pxafb_driver);
1923}
1924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925module_init(pxafb_init);
Jaya Kumar9f17f282008-06-22 04:27:28 +01001926module_exit(pxafb_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
1928MODULE_DESCRIPTION("loadable framebuffer driver for PXA");
1929MODULE_LICENSE("GPL");