blob: 6e6128c51d0370c86d40f987af6350f1c4d5b015 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/video/pxafb.c
3 *
4 * Copyright (C) 1999 Eric A. Thomas.
5 * Copyright (C) 2004 Jean-Frederic Clere.
6 * Copyright (C) 2004 Ian Campbell.
7 * Copyright (C) 2004 Jeff Lackey.
8 * Based on sa1100fb.c Copyright (C) 1999 Eric A. Thomas
9 * which in turn is
10 * Based on acornfb.c Copyright (C) Russell King.
11 *
12 * This file is subject to the terms and conditions of the GNU General Public
13 * License. See the file COPYING in the main directory of this archive for
14 * more details.
15 *
16 * Intel PXA250/210 LCD Controller Frame Buffer Driver
17 *
18 * Please direct your questions and comments on this driver to the following
19 * email address:
20 *
21 * linux-arm-kernel@lists.arm.linux.org.uk
22 *
23 */
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/kernel.h>
28#include <linux/sched.h>
29#include <linux/errno.h>
30#include <linux/string.h>
31#include <linux/interrupt.h>
32#include <linux/slab.h>
33#include <linux/fb.h>
34#include <linux/delay.h>
35#include <linux/init.h>
36#include <linux/ioport.h>
37#include <linux/cpufreq.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010038#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/dma-mapping.h>
Russell King72e35242007-08-20 10:18:42 +010040#include <linux/clk.h>
41#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <asm/hardware.h>
44#include <asm/io.h>
45#include <asm/irq.h>
Nicolas Pitrebf1b8ab2005-06-23 21:56:45 +010046#include <asm/div64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/arch/pxa-regs.h>
eric miaoa683b142008-03-03 09:44:25 +080048#include <asm/arch/pxa2xx-gpio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/arch/bitfield.h>
50#include <asm/arch/pxafb.h>
51
52/*
53 * Complain if VAR is out of range.
54 */
55#define DEBUG_VAR 1
56
57#include "pxafb.h"
58
59/* Bits which should not be set in machine configuration structures */
eric miaob0086ef2008-04-30 00:52:19 -070060#define LCCR0_INVALID_CONFIG_MASK (LCCR0_OUM | LCCR0_BM | LCCR0_QDM |\
61 LCCR0_DIS | LCCR0_EFM | LCCR0_IUM |\
62 LCCR0_SFM | LCCR0_LDM | LCCR0_ENB)
63
64#define LCCR3_INVALID_CONFIG_MASK (LCCR3_HSP | LCCR3_VSP |\
65 LCCR3_PCD | LCCR3_BPP)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67static void (*pxafb_backlight_power)(int);
Richard Purdied14b2722006-09-20 22:54:21 +010068static void (*pxafb_lcd_power)(int, struct fb_var_screeninfo *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
eric miaob0086ef2008-04-30 00:52:19 -070070static int pxafb_activate_var(struct fb_var_screeninfo *var,
71 struct pxafb_info *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static void set_ctrlr_state(struct pxafb_info *fbi, u_int state);
73
74#ifdef CONFIG_FB_PXA_PARAMETERS
75#define PXAFB_OPTIONS_SIZE 256
Sam Ravnborg1e6a20c2007-08-07 19:03:27 +010076static char g_options[PXAFB_OPTIONS_SIZE] __devinitdata = "";
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#endif
78
79static inline void pxafb_schedule_work(struct pxafb_info *fbi, u_int state)
80{
81 unsigned long flags;
82
83 local_irq_save(flags);
84 /*
85 * We need to handle two requests being made at the same time.
86 * There are two important cases:
eric miaob0086ef2008-04-30 00:52:19 -070087 * 1. When we are changing VT (C_REENABLE) while unblanking
88 * (C_ENABLE) We must perform the unblanking, which will
89 * do our REENABLE for us.
90 * 2. When we are blanking, but immediately unblank before
91 * we have blanked. We do the "REENABLE" thing here as
92 * well, just to be sure.
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 */
94 if (fbi->task_state == C_ENABLE && state == C_REENABLE)
95 state = (u_int) -1;
96 if (fbi->task_state == C_DISABLE && state == C_ENABLE)
97 state = C_REENABLE;
98
99 if (state != (u_int)-1) {
100 fbi->task_state = state;
101 schedule_work(&fbi->task);
102 }
103 local_irq_restore(flags);
104}
105
106static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
107{
108 chan &= 0xffff;
109 chan >>= 16 - bf->length;
110 return chan << bf->offset;
111}
112
113static int
114pxafb_setpalettereg(u_int regno, u_int red, u_int green, u_int blue,
115 u_int trans, struct fb_info *info)
116{
117 struct pxafb_info *fbi = (struct pxafb_info *)info;
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700118 u_int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700120 if (regno >= fbi->palette_size)
121 return 1;
122
123 if (fbi->fb.var.grayscale) {
124 fbi->palette_cpu[regno] = ((blue >> 8) & 0x00ff);
125 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 }
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700127
128 switch (fbi->lccr4 & LCCR4_PAL_FOR_MASK) {
129 case LCCR4_PAL_FOR_0:
130 val = ((red >> 0) & 0xf800);
131 val |= ((green >> 5) & 0x07e0);
132 val |= ((blue >> 11) & 0x001f);
133 fbi->palette_cpu[regno] = val;
134 break;
135 case LCCR4_PAL_FOR_1:
136 val = ((red << 8) & 0x00f80000);
137 val |= ((green >> 0) & 0x0000fc00);
138 val |= ((blue >> 8) & 0x000000f8);
eric miaob0086ef2008-04-30 00:52:19 -0700139 ((u32 *)(fbi->palette_cpu))[regno] = val;
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700140 break;
141 case LCCR4_PAL_FOR_2:
142 val = ((red << 8) & 0x00fc0000);
143 val |= ((green >> 0) & 0x0000fc00);
144 val |= ((blue >> 8) & 0x000000fc);
eric miaob0086ef2008-04-30 00:52:19 -0700145 ((u32 *)(fbi->palette_cpu))[regno] = val;
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700146 break;
147 }
148
149 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
152static int
153pxafb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
154 u_int trans, struct fb_info *info)
155{
156 struct pxafb_info *fbi = (struct pxafb_info *)info;
157 unsigned int val;
158 int ret = 1;
159
160 /*
161 * If inverse mode was selected, invert all the colours
162 * rather than the register number. The register number
163 * is what you poke into the framebuffer to produce the
164 * colour you requested.
165 */
166 if (fbi->cmap_inverse) {
167 red = 0xffff - red;
168 green = 0xffff - green;
169 blue = 0xffff - blue;
170 }
171
172 /*
173 * If greyscale is true, then we convert the RGB value
174 * to greyscale no matter what visual we are using.
175 */
176 if (fbi->fb.var.grayscale)
177 red = green = blue = (19595 * red + 38470 * green +
178 7471 * blue) >> 16;
179
180 switch (fbi->fb.fix.visual) {
181 case FB_VISUAL_TRUECOLOR:
182 /*
183 * 16-bit True Colour. We encode the RGB value
184 * according to the RGB bitfield information.
185 */
186 if (regno < 16) {
187 u32 *pal = fbi->fb.pseudo_palette;
188
189 val = chan_to_field(red, &fbi->fb.var.red);
190 val |= chan_to_field(green, &fbi->fb.var.green);
191 val |= chan_to_field(blue, &fbi->fb.var.blue);
192
193 pal[regno] = val;
194 ret = 0;
195 }
196 break;
197
198 case FB_VISUAL_STATIC_PSEUDOCOLOR:
199 case FB_VISUAL_PSEUDOCOLOR:
200 ret = pxafb_setpalettereg(regno, red, green, blue, trans, info);
201 break;
202 }
203
204 return ret;
205}
206
207/*
208 * pxafb_bpp_to_lccr3():
209 * Convert a bits per pixel value to the correct bit pattern for LCCR3
210 */
211static int pxafb_bpp_to_lccr3(struct fb_var_screeninfo *var)
212{
eric miaob0086ef2008-04-30 00:52:19 -0700213 int ret = 0;
214 switch (var->bits_per_pixel) {
215 case 1: ret = LCCR3_1BPP; break;
216 case 2: ret = LCCR3_2BPP; break;
217 case 4: ret = LCCR3_4BPP; break;
218 case 8: ret = LCCR3_8BPP; break;
219 case 16: ret = LCCR3_16BPP; break;
220 }
221 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
224#ifdef CONFIG_CPU_FREQ
225/*
226 * pxafb_display_dma_period()
227 * Calculate the minimum period (in picoseconds) between two DMA
228 * requests for the LCD controller. If we hit this, it means we're
229 * doing nothing but LCD DMA.
230 */
231static unsigned int pxafb_display_dma_period(struct fb_var_screeninfo *var)
232{
eric miaob0086ef2008-04-30 00:52:19 -0700233 /*
234 * Period = pixclock * bits_per_byte * bytes_per_transfer
235 * / memory_bits_per_pixel;
236 */
237 return var->pixclock * 8 * 16 / var->bits_per_pixel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239#endif
240
241/*
Richard Purdied14b2722006-09-20 22:54:21 +0100242 * Select the smallest mode that allows the desired resolution to be
243 * displayed. If desired parameters can be rounded up.
244 */
eric miaob0086ef2008-04-30 00:52:19 -0700245static struct pxafb_mode_info *pxafb_getmode(struct pxafb_mach_info *mach,
246 struct fb_var_screeninfo *var)
Richard Purdied14b2722006-09-20 22:54:21 +0100247{
248 struct pxafb_mode_info *mode = NULL;
249 struct pxafb_mode_info *modelist = mach->modes;
250 unsigned int best_x = 0xffffffff, best_y = 0xffffffff;
251 unsigned int i;
252
eric miaob0086ef2008-04-30 00:52:19 -0700253 for (i = 0; i < mach->num_modes; i++) {
254 if (modelist[i].xres >= var->xres &&
255 modelist[i].yres >= var->yres &&
256 modelist[i].xres < best_x &&
257 modelist[i].yres < best_y &&
258 modelist[i].bpp >= var->bits_per_pixel) {
Richard Purdied14b2722006-09-20 22:54:21 +0100259 best_x = modelist[i].xres;
260 best_y = modelist[i].yres;
261 mode = &modelist[i];
262 }
263 }
264
265 return mode;
266}
267
eric miaob0086ef2008-04-30 00:52:19 -0700268static void pxafb_setmode(struct fb_var_screeninfo *var,
269 struct pxafb_mode_info *mode)
Richard Purdied14b2722006-09-20 22:54:21 +0100270{
271 var->xres = mode->xres;
272 var->yres = mode->yres;
273 var->bits_per_pixel = mode->bpp;
274 var->pixclock = mode->pixclock;
275 var->hsync_len = mode->hsync_len;
276 var->left_margin = mode->left_margin;
277 var->right_margin = mode->right_margin;
278 var->vsync_len = mode->vsync_len;
279 var->upper_margin = mode->upper_margin;
280 var->lower_margin = mode->lower_margin;
281 var->sync = mode->sync;
282 var->grayscale = mode->cmap_greyscale;
283 var->xres_virtual = var->xres;
284 var->yres_virtual = var->yres;
285}
286
287/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 * pxafb_check_var():
289 * Get the video params out of 'var'. If a value doesn't fit, round it up,
290 * if it's too big, return -EINVAL.
291 *
292 * Round up in the following order: bits_per_pixel, xres,
293 * yres, xres_virtual, yres_virtual, xoffset, yoffset, grayscale,
294 * bitfields, horizontal timing, vertical timing.
295 */
296static int pxafb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
297{
298 struct pxafb_info *fbi = (struct pxafb_info *)info;
Richard Purdied14b2722006-09-20 22:54:21 +0100299 struct pxafb_mach_info *inf = fbi->dev->platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 if (var->xres < MIN_XRES)
302 var->xres = MIN_XRES;
303 if (var->yres < MIN_YRES)
304 var->yres = MIN_YRES;
Richard Purdied14b2722006-09-20 22:54:21 +0100305
306 if (inf->fixed_modes) {
307 struct pxafb_mode_info *mode;
308
309 mode = pxafb_getmode(inf, var);
310 if (!mode)
311 return -EINVAL;
312 pxafb_setmode(var, mode);
313 } else {
314 if (var->xres > inf->modes->xres)
315 return -EINVAL;
316 if (var->yres > inf->modes->yres)
317 return -EINVAL;
318 if (var->bits_per_pixel > inf->modes->bpp)
319 return -EINVAL;
320 }
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 var->xres_virtual =
323 max(var->xres_virtual, var->xres);
324 var->yres_virtual =
325 max(var->yres_virtual, var->yres);
326
eric miaob0086ef2008-04-30 00:52:19 -0700327 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 * Setup the RGB parameters for this display.
329 *
330 * The pixel packing format is described on page 7-11 of the
331 * PXA2XX Developer's Manual.
eric miaob0086ef2008-04-30 00:52:19 -0700332 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (var->bits_per_pixel == 16) {
334 var->red.offset = 11; var->red.length = 5;
335 var->green.offset = 5; var->green.length = 6;
336 var->blue.offset = 0; var->blue.length = 5;
337 var->transp.offset = var->transp.length = 0;
338 } else {
eric miaob0086ef2008-04-30 00:52:19 -0700339 var->red.offset = var->green.offset = 0;
340 var->blue.offset = var->transp.offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 var->red.length = 8;
342 var->green.length = 8;
343 var->blue.length = 8;
344 var->transp.length = 0;
345 }
346
347#ifdef CONFIG_CPU_FREQ
Russell Kingca5da712005-09-29 09:44:54 +0100348 pr_debug("pxafb: dma period = %d ps, clock = %d kHz\n",
349 pxafb_display_dma_period(var),
350 get_clk_frequency_khz(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351#endif
352
353 return 0;
354}
355
356static inline void pxafb_set_truecolor(u_int is_true_color)
357{
Russell Kingca5da712005-09-29 09:44:54 +0100358 pr_debug("pxafb: true_color = %d\n", is_true_color);
eric miaob0086ef2008-04-30 00:52:19 -0700359 /* do your machine-specific setup if needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
362/*
363 * pxafb_set_par():
364 * Set the user defined part of the display for the specified console
365 */
366static int pxafb_set_par(struct fb_info *info)
367{
368 struct pxafb_info *fbi = (struct pxafb_info *)info;
369 struct fb_var_screeninfo *var = &info->var;
370 unsigned long palette_mem_size;
371
Russell Kingca5da712005-09-29 09:44:54 +0100372 pr_debug("pxafb: set_par\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 if (var->bits_per_pixel == 16)
375 fbi->fb.fix.visual = FB_VISUAL_TRUECOLOR;
376 else if (!fbi->cmap_static)
377 fbi->fb.fix.visual = FB_VISUAL_PSEUDOCOLOR;
378 else {
379 /*
380 * Some people have weird ideas about wanting static
381 * pseudocolor maps. I suspect their user space
382 * applications are broken.
383 */
384 fbi->fb.fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
385 }
386
387 fbi->fb.fix.line_length = var->xres_virtual *
388 var->bits_per_pixel / 8;
389 if (var->bits_per_pixel == 16)
390 fbi->palette_size = 0;
391 else
eric miaob0086ef2008-04-30 00:52:19 -0700392 fbi->palette_size = var->bits_per_pixel == 1 ?
393 4 : 1 << var->bits_per_pixel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700395 if ((fbi->lccr4 & LCCR4_PAL_FOR_MASK) == LCCR4_PAL_FOR_0)
396 palette_mem_size = fbi->palette_size * sizeof(u16);
397 else
398 palette_mem_size = fbi->palette_size * sizeof(u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Russell Kingca5da712005-09-29 09:44:54 +0100400 pr_debug("pxafb: palette_mem_size = 0x%08lx\n", palette_mem_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 fbi->palette_cpu = (u16 *)(fbi->map_cpu + PAGE_SIZE - palette_mem_size);
403 fbi->palette_dma = fbi->map_dma + PAGE_SIZE - palette_mem_size;
404
405 /*
406 * Set (any) board control register to handle new color depth
407 */
408 pxafb_set_truecolor(fbi->fb.fix.visual == FB_VISUAL_TRUECOLOR);
409
410 if (fbi->fb.var.bits_per_pixel == 16)
411 fb_dealloc_cmap(&fbi->fb.cmap);
412 else
413 fb_alloc_cmap(&fbi->fb.cmap, 1<<fbi->fb.var.bits_per_pixel, 0);
414
415 pxafb_activate_var(var, fbi);
416
417 return 0;
418}
419
420/*
421 * Formal definition of the VESA spec:
422 * On
423 * This refers to the state of the display when it is in full operation
424 * Stand-By
425 * This defines an optional operating state of minimal power reduction with
426 * the shortest recovery time
427 * Suspend
428 * This refers to a level of power management in which substantial power
429 * reduction is achieved by the display. The display can have a longer
430 * recovery time from this state than from the Stand-by state
431 * Off
432 * This indicates that the display is consuming the lowest level of power
433 * and is non-operational. Recovery from this state may optionally require
434 * the user to manually power on the monitor
435 *
436 * Now, the fbdev driver adds an additional state, (blank), where they
437 * turn off the video (maybe by colormap tricks), but don't mess with the
438 * video itself: think of it semantically between on and Stand-By.
439 *
440 * So here's what we should do in our fbdev blank routine:
441 *
442 * VESA_NO_BLANKING (mode 0) Video on, front/back light on
443 * VESA_VSYNC_SUSPEND (mode 1) Video on, front/back light off
444 * VESA_HSYNC_SUSPEND (mode 2) Video on, front/back light off
445 * VESA_POWERDOWN (mode 3) Video off, front/back light off
446 *
447 * This will match the matrox implementation.
448 */
449
450/*
451 * pxafb_blank():
452 * Blank the display by setting all palette values to zero. Note, the
453 * 16 bpp mode does not really use the palette, so this will not
454 * blank the display in all modes.
455 */
456static int pxafb_blank(int blank, struct fb_info *info)
457{
458 struct pxafb_info *fbi = (struct pxafb_info *)info;
459 int i;
460
Russell Kingca5da712005-09-29 09:44:54 +0100461 pr_debug("pxafb: blank=%d\n", blank);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 switch (blank) {
464 case FB_BLANK_POWERDOWN:
465 case FB_BLANK_VSYNC_SUSPEND:
466 case FB_BLANK_HSYNC_SUSPEND:
467 case FB_BLANK_NORMAL:
468 if (fbi->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR ||
469 fbi->fb.fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
470 for (i = 0; i < fbi->palette_size; i++)
471 pxafb_setpalettereg(i, 0, 0, 0, 0, info);
472
473 pxafb_schedule_work(fbi, C_DISABLE);
eric miaob0086ef2008-04-30 00:52:19 -0700474 /* TODO if (pxafb_blank_helper) pxafb_blank_helper(blank); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 break;
476
477 case FB_BLANK_UNBLANK:
eric miaob0086ef2008-04-30 00:52:19 -0700478 /* TODO if (pxafb_blank_helper) pxafb_blank_helper(blank); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (fbi->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR ||
480 fbi->fb.fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
481 fb_set_cmap(&fbi->fb.cmap, info);
482 pxafb_schedule_work(fbi, C_ENABLE);
483 }
484 return 0;
485}
486
Christoph Hellwig216d5262006-01-14 13:21:25 -0800487static int pxafb_mmap(struct fb_info *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 struct vm_area_struct *vma)
489{
490 struct pxafb_info *fbi = (struct pxafb_info *)info;
491 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
492
493 if (off < info->fix.smem_len) {
494 vma->vm_pgoff += 1;
495 return dma_mmap_writecombine(fbi->dev, vma, fbi->map_cpu,
496 fbi->map_dma, fbi->map_size);
497 }
498 return -EINVAL;
499}
500
501static struct fb_ops pxafb_ops = {
502 .owner = THIS_MODULE,
503 .fb_check_var = pxafb_check_var,
504 .fb_set_par = pxafb_set_par,
505 .fb_setcolreg = pxafb_setcolreg,
506 .fb_fillrect = cfb_fillrect,
507 .fb_copyarea = cfb_copyarea,
508 .fb_imageblit = cfb_imageblit,
509 .fb_blank = pxafb_blank,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 .fb_mmap = pxafb_mmap,
511};
512
513/*
514 * Calculate the PCD value from the clock rate (in picoseconds).
515 * We take account of the PPCR clock setting.
516 * From PXA Developer's Manual:
517 *
518 * PixelClock = LCLK
519 * -------------
520 * 2 ( PCD + 1 )
521 *
522 * PCD = LCLK
523 * ------------- - 1
524 * 2(PixelClock)
525 *
526 * Where:
527 * LCLK = LCD/Memory Clock
528 * PCD = LCCR3[7:0]
529 *
530 * PixelClock here is in Hz while the pixclock argument given is the
531 * period in picoseconds. Hence PixelClock = 1 / ( pixclock * 10^-12 )
532 *
533 * The function get_lclk_frequency_10khz returns LCLK in units of
534 * 10khz. Calling the result of this function lclk gives us the
535 * following
536 *
537 * PCD = (lclk * 10^4 ) * ( pixclock * 10^-12 )
538 * -------------------------------------- - 1
539 * 2
540 *
541 * Factoring the 10^4 and 10^-12 out gives 10^-8 == 1 / 100000000 as used below.
542 */
eric miaob0086ef2008-04-30 00:52:19 -0700543static inline unsigned int get_pcd(struct pxafb_info *fbi,
544 unsigned int pixclock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
546 unsigned long long pcd;
547
548 /* FIXME: Need to take into account Double Pixel Clock mode
Russell King72e35242007-08-20 10:18:42 +0100549 * (DPC) bit? or perhaps set it based on the various clock
550 * speeds */
551 pcd = (unsigned long long)(clk_get_rate(fbi->clk) / 10000);
552 pcd *= pixclock;
Nicolas Pitrebf1b8ab2005-06-23 21:56:45 +0100553 do_div(pcd, 100000000 * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 /* no need for this, since we should subtract 1 anyway. they cancel */
555 /* pcd += 1; */ /* make up for integer math truncations */
556 return (unsigned int)pcd;
557}
558
559/*
Richard Purdieba44cd22005-09-09 13:10:03 -0700560 * Some touchscreens need hsync information from the video driver to
Russell King72e35242007-08-20 10:18:42 +0100561 * function correctly. We export it here. Note that 'hsync_time' and
562 * the value returned from pxafb_get_hsync_time() is the *reciprocal*
563 * of the hsync period in seconds.
Richard Purdieba44cd22005-09-09 13:10:03 -0700564 */
565static inline void set_hsync_time(struct pxafb_info *fbi, unsigned int pcd)
566{
Russell King72e35242007-08-20 10:18:42 +0100567 unsigned long htime;
Richard Purdieba44cd22005-09-09 13:10:03 -0700568
569 if ((pcd == 0) || (fbi->fb.var.hsync_len == 0)) {
eric miaob0086ef2008-04-30 00:52:19 -0700570 fbi->hsync_time = 0;
Richard Purdieba44cd22005-09-09 13:10:03 -0700571 return;
572 }
573
Russell King72e35242007-08-20 10:18:42 +0100574 htime = clk_get_rate(fbi->clk) / (pcd * fbi->fb.var.hsync_len);
575
Richard Purdieba44cd22005-09-09 13:10:03 -0700576 fbi->hsync_time = htime;
577}
578
579unsigned long pxafb_get_hsync_time(struct device *dev)
580{
581 struct pxafb_info *fbi = dev_get_drvdata(dev);
582
583 /* If display is blanked/suspended, hsync isn't active */
584 if (!fbi || (fbi->state != C_ENABLE))
585 return 0;
586
587 return fbi->hsync_time;
588}
589EXPORT_SYMBOL(pxafb_get_hsync_time);
590
591/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 * pxafb_activate_var():
eric miaob0086ef2008-04-30 00:52:19 -0700593 * Configures LCD Controller based on entries in var parameter.
594 * Settings are only written to the controller if changes were made.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 */
eric miaob0086ef2008-04-30 00:52:19 -0700596static int pxafb_activate_var(struct fb_var_screeninfo *var,
597 struct pxafb_info *fbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
599 struct pxafb_lcd_reg new_regs;
600 u_long flags;
Russell King72e35242007-08-20 10:18:42 +0100601 u_int lines_per_panel, pcd = get_pcd(fbi, var->pixclock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Russell Kingca5da712005-09-29 09:44:54 +0100603 pr_debug("pxafb: Configuring PXA LCD\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Russell Kingca5da712005-09-29 09:44:54 +0100605 pr_debug("var: xres=%d hslen=%d lm=%d rm=%d\n",
606 var->xres, var->hsync_len,
607 var->left_margin, var->right_margin);
608 pr_debug("var: yres=%d vslen=%d um=%d bm=%d\n",
609 var->yres, var->vsync_len,
610 var->upper_margin, var->lower_margin);
611 pr_debug("var: pixclock=%d pcd=%d\n", var->pixclock, pcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613#if DEBUG_VAR
eric miaob0086ef2008-04-30 00:52:19 -0700614 if (var->xres < 16 || var->xres > 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 printk(KERN_ERR "%s: invalid xres %d\n",
616 fbi->fb.fix.id, var->xres);
eric miaob0086ef2008-04-30 00:52:19 -0700617 switch (var->bits_per_pixel) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 case 1:
619 case 2:
620 case 4:
621 case 8:
622 case 16:
623 break;
624 default:
625 printk(KERN_ERR "%s: invalid bit depth %d\n",
626 fbi->fb.fix.id, var->bits_per_pixel);
627 break;
628 }
eric miaob0086ef2008-04-30 00:52:19 -0700629 if (var->hsync_len < 1 || var->hsync_len > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 printk(KERN_ERR "%s: invalid hsync_len %d\n",
631 fbi->fb.fix.id, var->hsync_len);
eric miaob0086ef2008-04-30 00:52:19 -0700632 if (var->left_margin < 1 || var->left_margin > 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 printk(KERN_ERR "%s: invalid left_margin %d\n",
634 fbi->fb.fix.id, var->left_margin);
635 if (var->right_margin < 1 || var->right_margin > 255)
636 printk(KERN_ERR "%s: invalid right_margin %d\n",
637 fbi->fb.fix.id, var->right_margin);
eric miaob0086ef2008-04-30 00:52:19 -0700638 if (var->yres < 1 || var->yres > 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 printk(KERN_ERR "%s: invalid yres %d\n",
640 fbi->fb.fix.id, var->yres);
eric miaob0086ef2008-04-30 00:52:19 -0700641 if (var->vsync_len < 1 || var->vsync_len > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 printk(KERN_ERR "%s: invalid vsync_len %d\n",
643 fbi->fb.fix.id, var->vsync_len);
644 if (var->upper_margin < 0 || var->upper_margin > 255)
645 printk(KERN_ERR "%s: invalid upper_margin %d\n",
646 fbi->fb.fix.id, var->upper_margin);
647 if (var->lower_margin < 0 || var->lower_margin > 255)
648 printk(KERN_ERR "%s: invalid lower_margin %d\n",
649 fbi->fb.fix.id, var->lower_margin);
650#endif
651
652 new_regs.lccr0 = fbi->lccr0 |
653 (LCCR0_LDM | LCCR0_SFM | LCCR0_IUM | LCCR0_EFM |
eric miaob0086ef2008-04-30 00:52:19 -0700654 LCCR0_QDM | LCCR0_BM | LCCR0_OUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 new_regs.lccr1 =
657 LCCR1_DisWdth(var->xres) +
658 LCCR1_HorSnchWdth(var->hsync_len) +
659 LCCR1_BegLnDel(var->left_margin) +
660 LCCR1_EndLnDel(var->right_margin);
661
662 /*
663 * If we have a dual scan LCD, we need to halve
664 * the YRES parameter.
665 */
666 lines_per_panel = var->yres;
667 if ((fbi->lccr0 & LCCR0_SDS) == LCCR0_Dual)
668 lines_per_panel /= 2;
669
670 new_regs.lccr2 =
671 LCCR2_DisHght(lines_per_panel) +
672 LCCR2_VrtSnchWdth(var->vsync_len) +
673 LCCR2_BegFrmDel(var->upper_margin) +
674 LCCR2_EndFrmDel(var->lower_margin);
675
676 new_regs.lccr3 = fbi->lccr3 |
677 pxafb_bpp_to_lccr3(var) |
eric miaob0086ef2008-04-30 00:52:19 -0700678 (var->sync & FB_SYNC_HOR_HIGH_ACT ?
679 LCCR3_HorSnchH : LCCR3_HorSnchL) |
680 (var->sync & FB_SYNC_VERT_HIGH_ACT ?
681 LCCR3_VrtSnchH : LCCR3_VrtSnchL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 if (pcd)
684 new_regs.lccr3 |= LCCR3_PixClkDiv(pcd);
685
Russell Kingca5da712005-09-29 09:44:54 +0100686 pr_debug("nlccr0 = 0x%08x\n", new_regs.lccr0);
687 pr_debug("nlccr1 = 0x%08x\n", new_regs.lccr1);
688 pr_debug("nlccr2 = 0x%08x\n", new_regs.lccr2);
689 pr_debug("nlccr3 = 0x%08x\n", new_regs.lccr3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 /* Update shadow copy atomically */
692 local_irq_save(flags);
693
694 /* setup dma descriptors */
eric miaob0086ef2008-04-30 00:52:19 -0700695 fbi->dmadesc_fblow_cpu = (struct pxafb_dma_descriptor *)
696 ((unsigned int)fbi->palette_cpu - 3*16);
697 fbi->dmadesc_fbhigh_cpu = (struct pxafb_dma_descriptor *)
698 ((unsigned int)fbi->palette_cpu - 2*16);
699 fbi->dmadesc_palette_cpu = (struct pxafb_dma_descriptor *)
700 ((unsigned int)fbi->palette_cpu - 1*16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 fbi->dmadesc_fblow_dma = fbi->palette_dma - 3*16;
703 fbi->dmadesc_fbhigh_dma = fbi->palette_dma - 2*16;
704 fbi->dmadesc_palette_dma = fbi->palette_dma - 1*16;
705
706#define BYTES_PER_PANEL (lines_per_panel * fbi->fb.fix.line_length)
707
708 /* populate descriptors */
709 fbi->dmadesc_fblow_cpu->fdadr = fbi->dmadesc_fblow_dma;
710 fbi->dmadesc_fblow_cpu->fsadr = fbi->screen_dma + BYTES_PER_PANEL;
711 fbi->dmadesc_fblow_cpu->fidr = 0;
712 fbi->dmadesc_fblow_cpu->ldcmd = BYTES_PER_PANEL;
713
714 fbi->fdadr1 = fbi->dmadesc_fblow_dma; /* only used in dual-panel mode */
715
716 fbi->dmadesc_fbhigh_cpu->fsadr = fbi->screen_dma;
717 fbi->dmadesc_fbhigh_cpu->fidr = 0;
718 fbi->dmadesc_fbhigh_cpu->ldcmd = BYTES_PER_PANEL;
719
720 fbi->dmadesc_palette_cpu->fsadr = fbi->palette_dma;
721 fbi->dmadesc_palette_cpu->fidr = 0;
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700722 if ((fbi->lccr4 & LCCR4_PAL_FOR_MASK) == LCCR4_PAL_FOR_0)
723 fbi->dmadesc_palette_cpu->ldcmd = fbi->palette_size *
724 sizeof(u16);
725 else
726 fbi->dmadesc_palette_cpu->ldcmd = fbi->palette_size *
727 sizeof(u32);
728 fbi->dmadesc_palette_cpu->ldcmd |= LDCMD_PAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 if (var->bits_per_pixel == 16) {
731 /* palette shouldn't be loaded in true-color mode */
732 fbi->dmadesc_fbhigh_cpu->fdadr = fbi->dmadesc_fbhigh_dma;
733 fbi->fdadr0 = fbi->dmadesc_fbhigh_dma; /* no pal just fbhigh */
734 /* init it to something, even though we won't be using it */
735 fbi->dmadesc_palette_cpu->fdadr = fbi->dmadesc_palette_dma;
736 } else {
eric miaob0086ef2008-04-30 00:52:19 -0700737 /* flips back and forth between pal and fbhigh */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 fbi->dmadesc_palette_cpu->fdadr = fbi->dmadesc_fbhigh_dma;
739 fbi->dmadesc_fbhigh_cpu->fdadr = fbi->dmadesc_palette_dma;
eric miaob0086ef2008-04-30 00:52:19 -0700740 fbi->fdadr0 = fbi->dmadesc_palette_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 fbi->reg_lccr0 = new_regs.lccr0;
744 fbi->reg_lccr1 = new_regs.lccr1;
745 fbi->reg_lccr2 = new_regs.lccr2;
746 fbi->reg_lccr3 = new_regs.lccr3;
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700747 fbi->reg_lccr4 = LCCR4 & (~LCCR4_PAL_FOR_MASK);
748 fbi->reg_lccr4 |= (fbi->lccr4 & LCCR4_PAL_FOR_MASK);
Richard Purdieba44cd22005-09-09 13:10:03 -0700749 set_hsync_time(fbi, pcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 local_irq_restore(flags);
751
752 /*
753 * Only update the registers if the controller is enabled
754 * and something has changed.
755 */
756 if ((LCCR0 != fbi->reg_lccr0) || (LCCR1 != fbi->reg_lccr1) ||
757 (LCCR2 != fbi->reg_lccr2) || (LCCR3 != fbi->reg_lccr3) ||
758 (FDADR0 != fbi->fdadr0) || (FDADR1 != fbi->fdadr1))
759 pxafb_schedule_work(fbi, C_REENABLE);
760
761 return 0;
762}
763
764/*
765 * NOTE! The following functions are purely helpers for set_ctrlr_state.
766 * Do not call them directly; set_ctrlr_state does the correct serialisation
767 * to ensure that things happen in the right way 100% of time time.
768 * -- rmk
769 */
770static inline void __pxafb_backlight_power(struct pxafb_info *fbi, int on)
771{
Russell Kingca5da712005-09-29 09:44:54 +0100772 pr_debug("pxafb: backlight o%s\n", on ? "n" : "ff");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
eric miaob0086ef2008-04-30 00:52:19 -0700774 if (pxafb_backlight_power)
775 pxafb_backlight_power(on);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
777
778static inline void __pxafb_lcd_power(struct pxafb_info *fbi, int on)
779{
Russell Kingca5da712005-09-29 09:44:54 +0100780 pr_debug("pxafb: LCD power o%s\n", on ? "n" : "ff");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 if (pxafb_lcd_power)
Richard Purdied14b2722006-09-20 22:54:21 +0100783 pxafb_lcd_power(on, &fbi->fb.var);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
786static void pxafb_setup_gpio(struct pxafb_info *fbi)
787{
788 int gpio, ldd_bits;
eric miaob0086ef2008-04-30 00:52:19 -0700789 unsigned int lccr0 = fbi->lccr0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 /*
792 * setup is based on type of panel supported
eric miaob0086ef2008-04-30 00:52:19 -0700793 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 /* 4 bit interface */
796 if ((lccr0 & LCCR0_CMS) == LCCR0_Mono &&
797 (lccr0 & LCCR0_SDS) == LCCR0_Sngl &&
798 (lccr0 & LCCR0_DPD) == LCCR0_4PixMono)
799 ldd_bits = 4;
800
801 /* 8 bit interface */
eric miaob0086ef2008-04-30 00:52:19 -0700802 else if (((lccr0 & LCCR0_CMS) == LCCR0_Mono &&
803 ((lccr0 & LCCR0_SDS) == LCCR0_Dual ||
804 (lccr0 & LCCR0_DPD) == LCCR0_8PixMono)) ||
805 ((lccr0 & LCCR0_CMS) == LCCR0_Color &&
806 (lccr0 & LCCR0_PAS) == LCCR0_Pas &&
807 (lccr0 & LCCR0_SDS) == LCCR0_Sngl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 ldd_bits = 8;
809
810 /* 16 bit interface */
811 else if ((lccr0 & LCCR0_CMS) == LCCR0_Color &&
eric miaob0086ef2008-04-30 00:52:19 -0700812 ((lccr0 & LCCR0_SDS) == LCCR0_Dual ||
813 (lccr0 & LCCR0_PAS) == LCCR0_Act))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 ldd_bits = 16;
815
816 else {
eric miaob0086ef2008-04-30 00:52:19 -0700817 printk(KERN_ERR "pxafb_setup_gpio: unable to determine "
818 "bits per pixel\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return;
eric miaob0086ef2008-04-30 00:52:19 -0700820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 for (gpio = 58; ldd_bits; gpio++, ldd_bits--)
823 pxa_gpio_mode(gpio | GPIO_ALT_FN_2_OUT);
824 pxa_gpio_mode(GPIO74_LCD_FCLK_MD);
825 pxa_gpio_mode(GPIO75_LCD_LCLK_MD);
826 pxa_gpio_mode(GPIO76_LCD_PCLK_MD);
827 pxa_gpio_mode(GPIO77_LCD_ACBIAS_MD);
828}
829
830static void pxafb_enable_controller(struct pxafb_info *fbi)
831{
Russell Kingca5da712005-09-29 09:44:54 +0100832 pr_debug("pxafb: Enabling LCD controller\n");
833 pr_debug("fdadr0 0x%08x\n", (unsigned int) fbi->fdadr0);
834 pr_debug("fdadr1 0x%08x\n", (unsigned int) fbi->fdadr1);
835 pr_debug("reg_lccr0 0x%08x\n", (unsigned int) fbi->reg_lccr0);
836 pr_debug("reg_lccr1 0x%08x\n", (unsigned int) fbi->reg_lccr1);
837 pr_debug("reg_lccr2 0x%08x\n", (unsigned int) fbi->reg_lccr2);
838 pr_debug("reg_lccr3 0x%08x\n", (unsigned int) fbi->reg_lccr3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Nicolas Pitre8d372262005-08-10 16:45:13 +0100840 /* enable LCD controller clock */
Russell King72e35242007-08-20 10:18:42 +0100841 clk_enable(fbi->clk);
Nicolas Pitre8d372262005-08-10 16:45:13 +0100842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 /* Sequence from 11.7.10 */
844 LCCR3 = fbi->reg_lccr3;
845 LCCR2 = fbi->reg_lccr2;
846 LCCR1 = fbi->reg_lccr1;
847 LCCR0 = fbi->reg_lccr0 & ~LCCR0_ENB;
848
849 FDADR0 = fbi->fdadr0;
850 FDADR1 = fbi->fdadr1;
851 LCCR0 |= LCCR0_ENB;
852
Russell Kingca5da712005-09-29 09:44:54 +0100853 pr_debug("FDADR0 0x%08x\n", (unsigned int) FDADR0);
854 pr_debug("FDADR1 0x%08x\n", (unsigned int) FDADR1);
855 pr_debug("LCCR0 0x%08x\n", (unsigned int) LCCR0);
856 pr_debug("LCCR1 0x%08x\n", (unsigned int) LCCR1);
857 pr_debug("LCCR2 0x%08x\n", (unsigned int) LCCR2);
858 pr_debug("LCCR3 0x%08x\n", (unsigned int) LCCR3);
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700859 pr_debug("LCCR4 0x%08x\n", (unsigned int) LCCR4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860}
861
862static void pxafb_disable_controller(struct pxafb_info *fbi)
863{
864 DECLARE_WAITQUEUE(wait, current);
865
Russell Kingca5da712005-09-29 09:44:54 +0100866 pr_debug("pxafb: disabling LCD controller\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 set_current_state(TASK_UNINTERRUPTIBLE);
869 add_wait_queue(&fbi->ctrlr_wait, &wait);
870
871 LCSR = 0xffffffff; /* Clear LCD Status Register */
872 LCCR0 &= ~LCCR0_LDM; /* Enable LCD Disable Done Interrupt */
873 LCCR0 |= LCCR0_DIS; /* Disable LCD Controller */
874
Richard Purdie2cbbb3b2006-03-31 02:31:53 -0800875 schedule_timeout(200 * HZ / 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 remove_wait_queue(&fbi->ctrlr_wait, &wait);
Nicolas Pitre8d372262005-08-10 16:45:13 +0100877
878 /* disable LCD controller clock */
Russell King72e35242007-08-20 10:18:42 +0100879 clk_disable(fbi->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
881
882/*
883 * pxafb_handle_irq: Handle 'LCD DONE' interrupts.
884 */
David Howells7d12e782006-10-05 14:55:46 +0100885static irqreturn_t pxafb_handle_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
887 struct pxafb_info *fbi = dev_id;
888 unsigned int lcsr = LCSR;
889
890 if (lcsr & LCSR_LDD) {
891 LCCR0 |= LCCR0_LDM;
892 wake_up(&fbi->ctrlr_wait);
893 }
894
895 LCSR = lcsr;
896 return IRQ_HANDLED;
897}
898
899/*
900 * This function must be called from task context only, since it will
901 * sleep when disabling the LCD controller, or if we get two contending
902 * processes trying to alter state.
903 */
904static void set_ctrlr_state(struct pxafb_info *fbi, u_int state)
905{
906 u_int old_state;
907
908 down(&fbi->ctrlr_sem);
909
910 old_state = fbi->state;
911
912 /*
913 * Hack around fbcon initialisation.
914 */
915 if (old_state == C_STARTUP && state == C_REENABLE)
916 state = C_ENABLE;
917
918 switch (state) {
919 case C_DISABLE_CLKCHANGE:
920 /*
921 * Disable controller for clock change. If the
922 * controller is already disabled, then do nothing.
923 */
924 if (old_state != C_DISABLE && old_state != C_DISABLE_PM) {
925 fbi->state = state;
eric miaob0086ef2008-04-30 00:52:19 -0700926 /* TODO __pxafb_lcd_power(fbi, 0); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 pxafb_disable_controller(fbi);
928 }
929 break;
930
931 case C_DISABLE_PM:
932 case C_DISABLE:
933 /*
934 * Disable controller
935 */
936 if (old_state != C_DISABLE) {
937 fbi->state = state;
938 __pxafb_backlight_power(fbi, 0);
939 __pxafb_lcd_power(fbi, 0);
940 if (old_state != C_DISABLE_CLKCHANGE)
941 pxafb_disable_controller(fbi);
942 }
943 break;
944
945 case C_ENABLE_CLKCHANGE:
946 /*
947 * Enable the controller after clock change. Only
948 * do this if we were disabled for the clock change.
949 */
950 if (old_state == C_DISABLE_CLKCHANGE) {
951 fbi->state = C_ENABLE;
952 pxafb_enable_controller(fbi);
eric miaob0086ef2008-04-30 00:52:19 -0700953 /* TODO __pxafb_lcd_power(fbi, 1); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 }
955 break;
956
957 case C_REENABLE:
958 /*
959 * Re-enable the controller only if it was already
960 * enabled. This is so we reprogram the control
961 * registers.
962 */
963 if (old_state == C_ENABLE) {
Richard Purdied14b2722006-09-20 22:54:21 +0100964 __pxafb_lcd_power(fbi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 pxafb_disable_controller(fbi);
966 pxafb_setup_gpio(fbi);
967 pxafb_enable_controller(fbi);
Richard Purdied14b2722006-09-20 22:54:21 +0100968 __pxafb_lcd_power(fbi, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
970 break;
971
972 case C_ENABLE_PM:
973 /*
974 * Re-enable the controller after PM. This is not
975 * perfect - think about the case where we were doing
976 * a clock change, and we suspended half-way through.
977 */
978 if (old_state != C_DISABLE_PM)
979 break;
980 /* fall through */
981
982 case C_ENABLE:
983 /*
984 * Power up the LCD screen, enable controller, and
985 * turn on the backlight.
986 */
987 if (old_state != C_ENABLE) {
988 fbi->state = C_ENABLE;
989 pxafb_setup_gpio(fbi);
990 pxafb_enable_controller(fbi);
991 __pxafb_lcd_power(fbi, 1);
992 __pxafb_backlight_power(fbi, 1);
993 }
994 break;
995 }
996 up(&fbi->ctrlr_sem);
997}
998
999/*
1000 * Our LCD controller task (which is called when we blank or unblank)
1001 * via keventd.
1002 */
David Howells6d5aefb2006-12-05 19:36:26 +00001003static void pxafb_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
David Howells6d5aefb2006-12-05 19:36:26 +00001005 struct pxafb_info *fbi =
1006 container_of(work, struct pxafb_info, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 u_int state = xchg(&fbi->task_state, -1);
1008
1009 set_ctrlr_state(fbi, state);
1010}
1011
1012#ifdef CONFIG_CPU_FREQ
1013/*
1014 * CPU clock speed change handler. We need to adjust the LCD timing
1015 * parameters when the CPU clock is adjusted by the power management
1016 * subsystem.
1017 *
1018 * TODO: Determine why f->new != 10*get_lclk_frequency_10khz()
1019 */
1020static int
1021pxafb_freq_transition(struct notifier_block *nb, unsigned long val, void *data)
1022{
1023 struct pxafb_info *fbi = TO_INF(nb, freq_transition);
eric miaob0086ef2008-04-30 00:52:19 -07001024 /* TODO struct cpufreq_freqs *f = data; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 u_int pcd;
1026
1027 switch (val) {
1028 case CPUFREQ_PRECHANGE:
1029 set_ctrlr_state(fbi, C_DISABLE_CLKCHANGE);
1030 break;
1031
1032 case CPUFREQ_POSTCHANGE:
Russell King72e35242007-08-20 10:18:42 +01001033 pcd = get_pcd(fbi, fbi->fb.var.pixclock);
Richard Purdieba44cd22005-09-09 13:10:03 -07001034 set_hsync_time(fbi, pcd);
eric miaob0086ef2008-04-30 00:52:19 -07001035 fbi->reg_lccr3 = (fbi->reg_lccr3 & ~0xff) |
1036 LCCR3_PixClkDiv(pcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 set_ctrlr_state(fbi, C_ENABLE_CLKCHANGE);
1038 break;
1039 }
1040 return 0;
1041}
1042
1043static int
1044pxafb_freq_policy(struct notifier_block *nb, unsigned long val, void *data)
1045{
1046 struct pxafb_info *fbi = TO_INF(nb, freq_policy);
1047 struct fb_var_screeninfo *var = &fbi->fb.var;
1048 struct cpufreq_policy *policy = data;
1049
1050 switch (val) {
1051 case CPUFREQ_ADJUST:
1052 case CPUFREQ_INCOMPATIBLE:
Holger Schurigac2bf5b2008-02-11 16:52:30 +01001053 pr_debug("min dma period: %d ps, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 "new clock %d kHz\n", pxafb_display_dma_period(var),
1055 policy->max);
eric miaob0086ef2008-04-30 00:52:19 -07001056 /* TODO: fill in min/max values */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
1059 return 0;
1060}
1061#endif
1062
1063#ifdef CONFIG_PM
1064/*
1065 * Power management hooks. Note that we won't be called from IRQ context,
1066 * unlike the blank functions above, so we may sleep.
1067 */
Russell King3ae5eae2005-11-09 22:32:44 +00001068static int pxafb_suspend(struct platform_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
Russell King3ae5eae2005-11-09 22:32:44 +00001070 struct pxafb_info *fbi = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Russell King9480e302005-10-28 09:52:56 -07001072 set_ctrlr_state(fbi, C_DISABLE_PM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 return 0;
1074}
1075
Russell King3ae5eae2005-11-09 22:32:44 +00001076static int pxafb_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
Russell King3ae5eae2005-11-09 22:32:44 +00001078 struct pxafb_info *fbi = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Russell King9480e302005-10-28 09:52:56 -07001080 set_ctrlr_state(fbi, C_ENABLE_PM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return 0;
1082}
1083#else
1084#define pxafb_suspend NULL
1085#define pxafb_resume NULL
1086#endif
1087
1088/*
1089 * pxafb_map_video_memory():
1090 * Allocates the DRAM memory for the frame buffer. This buffer is
1091 * remapped into a non-cached, non-buffered, memory region to
1092 * allow palette and pixel writes to occur without flushing the
1093 * cache. Once this area is remapped, all virtual memory
1094 * access to the video memory should occur at the new region.
1095 */
1096static int __init pxafb_map_video_memory(struct pxafb_info *fbi)
1097{
1098 u_long palette_mem_size;
1099
1100 /*
1101 * We reserve one page for the palette, plus the size
1102 * of the framebuffer.
1103 */
1104 fbi->map_size = PAGE_ALIGN(fbi->fb.fix.smem_len + PAGE_SIZE);
1105 fbi->map_cpu = dma_alloc_writecombine(fbi->dev, fbi->map_size,
1106 &fbi->map_dma, GFP_KERNEL);
1107
1108 if (fbi->map_cpu) {
1109 /* prevent initial garbage on screen */
1110 memset(fbi->map_cpu, 0, fbi->map_size);
1111 fbi->fb.screen_base = fbi->map_cpu + PAGE_SIZE;
1112 fbi->screen_dma = fbi->map_dma + PAGE_SIZE;
1113 /*
1114 * FIXME: this is actually the wrong thing to place in
1115 * smem_start. But fbdev suffers from the problem that
1116 * it needs an API which doesn't exist (in this case,
1117 * dma_writecombine_mmap)
1118 */
1119 fbi->fb.fix.smem_start = fbi->screen_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 fbi->palette_size = fbi->fb.var.bits_per_pixel == 8 ? 256 : 16;
1121
Hans J. Koch9ffa7392007-10-16 01:28:41 -07001122 if ((fbi->lccr4 & LCCR4_PAL_FOR_MASK) == LCCR4_PAL_FOR_0)
1123 palette_mem_size = fbi->palette_size * sizeof(u16);
1124 else
1125 palette_mem_size = fbi->palette_size * sizeof(u32);
1126
eric miaob0086ef2008-04-30 00:52:19 -07001127 pr_debug("pxafb: palette_mem_size = 0x%08lx\n",
1128 palette_mem_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
eric miaob0086ef2008-04-30 00:52:19 -07001130 fbi->palette_cpu = (u16 *)(fbi->map_cpu + PAGE_SIZE
1131 - palette_mem_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 fbi->palette_dma = fbi->map_dma + PAGE_SIZE - palette_mem_size;
1133 }
1134
1135 return fbi->map_cpu ? 0 : -ENOMEM;
1136}
1137
1138static struct pxafb_info * __init pxafb_init_fbinfo(struct device *dev)
1139{
1140 struct pxafb_info *fbi;
1141 void *addr;
1142 struct pxafb_mach_info *inf = dev->platform_data;
Richard Purdied14b2722006-09-20 22:54:21 +01001143 struct pxafb_mode_info *mode = inf->modes;
1144 int i, smemlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 /* Alloc the pxafb_info and pseudo_palette in one step */
1147 fbi = kmalloc(sizeof(struct pxafb_info) + sizeof(u32) * 16, GFP_KERNEL);
1148 if (!fbi)
1149 return NULL;
1150
1151 memset(fbi, 0, sizeof(struct pxafb_info));
1152 fbi->dev = dev;
1153
Russell King72e35242007-08-20 10:18:42 +01001154 fbi->clk = clk_get(dev, "LCDCLK");
1155 if (IS_ERR(fbi->clk)) {
1156 kfree(fbi);
1157 return NULL;
1158 }
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 strcpy(fbi->fb.fix.id, PXA_NAME);
1161
1162 fbi->fb.fix.type = FB_TYPE_PACKED_PIXELS;
1163 fbi->fb.fix.type_aux = 0;
1164 fbi->fb.fix.xpanstep = 0;
1165 fbi->fb.fix.ypanstep = 0;
1166 fbi->fb.fix.ywrapstep = 0;
1167 fbi->fb.fix.accel = FB_ACCEL_NONE;
1168
1169 fbi->fb.var.nonstd = 0;
1170 fbi->fb.var.activate = FB_ACTIVATE_NOW;
1171 fbi->fb.var.height = -1;
1172 fbi->fb.var.width = -1;
1173 fbi->fb.var.accel_flags = 0;
1174 fbi->fb.var.vmode = FB_VMODE_NONINTERLACED;
1175
1176 fbi->fb.fbops = &pxafb_ops;
1177 fbi->fb.flags = FBINFO_DEFAULT;
1178 fbi->fb.node = -1;
1179
1180 addr = fbi;
1181 addr = addr + sizeof(struct pxafb_info);
1182 fbi->fb.pseudo_palette = addr;
1183
Richard Purdied14b2722006-09-20 22:54:21 +01001184 pxafb_setmode(&fbi->fb.var, mode);
1185
eric miaob0086ef2008-04-30 00:52:19 -07001186 fbi->cmap_inverse = inf->cmap_inverse;
1187 fbi->cmap_static = inf->cmap_static;
Richard Purdied14b2722006-09-20 22:54:21 +01001188
eric miaob0086ef2008-04-30 00:52:19 -07001189 fbi->lccr0 = inf->lccr0;
1190 fbi->lccr3 = inf->lccr3;
1191 fbi->lccr4 = inf->lccr4;
1192 fbi->state = C_STARTUP;
1193 fbi->task_state = (u_char)-1;
Richard Purdied14b2722006-09-20 22:54:21 +01001194
1195 for (i = 0; i < inf->num_modes; i++) {
1196 smemlen = mode[i].xres * mode[i].yres * mode[i].bpp / 8;
1197 if (smemlen > fbi->fb.fix.smem_len)
1198 fbi->fb.fix.smem_len = smemlen;
1199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 init_waitqueue_head(&fbi->ctrlr_wait);
David Howells6d5aefb2006-12-05 19:36:26 +00001202 INIT_WORK(&fbi->task, pxafb_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 init_MUTEX(&fbi->ctrlr_sem);
1204
1205 return fbi;
1206}
1207
1208#ifdef CONFIG_FB_PXA_PARAMETERS
eric miaob0086ef2008-04-30 00:52:19 -07001209static int __init parse_opt_mode(struct device *dev, const char *this_opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
1211 struct pxafb_mach_info *inf = dev->platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
eric miao817daf12008-04-30 00:52:18 -07001213 const char *name = this_opt+5;
1214 unsigned int namelen = strlen(name);
1215 int res_specified = 0, bpp_specified = 0;
1216 unsigned int xres = 0, yres = 0, bpp = 0;
1217 int yres_specified = 0;
1218 int i;
1219 for (i = namelen-1; i >= 0; i--) {
1220 switch (name[i]) {
1221 case '-':
1222 namelen = i;
1223 if (!bpp_specified && !yres_specified) {
1224 bpp = simple_strtoul(&name[i+1], NULL, 0);
1225 bpp_specified = 1;
1226 } else
1227 goto done;
1228 break;
1229 case 'x':
1230 if (!yres_specified) {
1231 yres = simple_strtoul(&name[i+1], NULL, 0);
1232 yres_specified = 1;
1233 } else
1234 goto done;
1235 break;
1236 case '0' ... '9':
1237 break;
1238 default:
1239 goto done;
1240 }
1241 }
1242 if (i < 0 && yres_specified) {
1243 xres = simple_strtoul(name, NULL, 0);
1244 res_specified = 1;
1245 }
1246done:
1247 if (res_specified) {
1248 dev_info(dev, "overriding resolution: %dx%d\n", xres, yres);
1249 inf->modes[0].xres = xres; inf->modes[0].yres = yres;
1250 }
1251 if (bpp_specified)
1252 switch (bpp) {
1253 case 1:
1254 case 2:
1255 case 4:
1256 case 8:
1257 case 16:
1258 inf->modes[0].bpp = bpp;
1259 dev_info(dev, "overriding bit depth: %d\n", bpp);
1260 break;
1261 default:
1262 dev_err(dev, "Depth %d is not valid\n", bpp);
1263 return -EINVAL;
1264 }
1265 return 0;
1266}
1267
eric miaob0086ef2008-04-30 00:52:19 -07001268static int __init parse_opt(struct device *dev, char *this_opt)
eric miao817daf12008-04-30 00:52:18 -07001269{
1270 struct pxafb_mach_info *inf = dev->platform_data;
1271 struct pxafb_mode_info *mode = &inf->modes[0];
1272 char s[64];
1273
1274 s[0] = '\0';
1275
1276 if (!strncmp(this_opt, "mode:", 5)) {
1277 return parse_opt_mode(dev, this_opt);
1278 } else if (!strncmp(this_opt, "pixclock:", 9)) {
1279 mode->pixclock = simple_strtoul(this_opt+9, NULL, 0);
1280 sprintf(s, "pixclock: %ld\n", mode->pixclock);
1281 } else if (!strncmp(this_opt, "left:", 5)) {
1282 mode->left_margin = simple_strtoul(this_opt+5, NULL, 0);
1283 sprintf(s, "left: %u\n", mode->left_margin);
1284 } else if (!strncmp(this_opt, "right:", 6)) {
1285 mode->right_margin = simple_strtoul(this_opt+6, NULL, 0);
1286 sprintf(s, "right: %u\n", mode->right_margin);
1287 } else if (!strncmp(this_opt, "upper:", 6)) {
1288 mode->upper_margin = simple_strtoul(this_opt+6, NULL, 0);
1289 sprintf(s, "upper: %u\n", mode->upper_margin);
1290 } else if (!strncmp(this_opt, "lower:", 6)) {
1291 mode->lower_margin = simple_strtoul(this_opt+6, NULL, 0);
1292 sprintf(s, "lower: %u\n", mode->lower_margin);
1293 } else if (!strncmp(this_opt, "hsynclen:", 9)) {
1294 mode->hsync_len = simple_strtoul(this_opt+9, NULL, 0);
1295 sprintf(s, "hsynclen: %u\n", mode->hsync_len);
1296 } else if (!strncmp(this_opt, "vsynclen:", 9)) {
1297 mode->vsync_len = simple_strtoul(this_opt+9, NULL, 0);
1298 sprintf(s, "vsynclen: %u\n", mode->vsync_len);
1299 } else if (!strncmp(this_opt, "hsync:", 6)) {
1300 if (simple_strtoul(this_opt+6, NULL, 0) == 0) {
1301 sprintf(s, "hsync: Active Low\n");
1302 mode->sync &= ~FB_SYNC_HOR_HIGH_ACT;
1303 } else {
1304 sprintf(s, "hsync: Active High\n");
1305 mode->sync |= FB_SYNC_HOR_HIGH_ACT;
1306 }
1307 } else if (!strncmp(this_opt, "vsync:", 6)) {
1308 if (simple_strtoul(this_opt+6, NULL, 0) == 0) {
1309 sprintf(s, "vsync: Active Low\n");
1310 mode->sync &= ~FB_SYNC_VERT_HIGH_ACT;
1311 } else {
1312 sprintf(s, "vsync: Active High\n");
1313 mode->sync |= FB_SYNC_VERT_HIGH_ACT;
1314 }
1315 } else if (!strncmp(this_opt, "dpc:", 4)) {
1316 if (simple_strtoul(this_opt+4, NULL, 0) == 0) {
1317 sprintf(s, "double pixel clock: false\n");
1318 inf->lccr3 &= ~LCCR3_DPC;
1319 } else {
1320 sprintf(s, "double pixel clock: true\n");
1321 inf->lccr3 |= LCCR3_DPC;
1322 }
1323 } else if (!strncmp(this_opt, "outputen:", 9)) {
1324 if (simple_strtoul(this_opt+9, NULL, 0) == 0) {
1325 sprintf(s, "output enable: active low\n");
1326 inf->lccr3 = (inf->lccr3 & ~LCCR3_OEP) | LCCR3_OutEnL;
1327 } else {
1328 sprintf(s, "output enable: active high\n");
1329 inf->lccr3 = (inf->lccr3 & ~LCCR3_OEP) | LCCR3_OutEnH;
1330 }
1331 } else if (!strncmp(this_opt, "pixclockpol:", 12)) {
1332 if (simple_strtoul(this_opt+12, NULL, 0) == 0) {
1333 sprintf(s, "pixel clock polarity: falling edge\n");
1334 inf->lccr3 = (inf->lccr3 & ~LCCR3_PCP) | LCCR3_PixFlEdg;
1335 } else {
1336 sprintf(s, "pixel clock polarity: rising edge\n");
1337 inf->lccr3 = (inf->lccr3 & ~LCCR3_PCP) | LCCR3_PixRsEdg;
1338 }
1339 } else if (!strncmp(this_opt, "color", 5)) {
1340 inf->lccr0 = (inf->lccr0 & ~LCCR0_CMS) | LCCR0_Color;
1341 } else if (!strncmp(this_opt, "mono", 4)) {
1342 inf->lccr0 = (inf->lccr0 & ~LCCR0_CMS) | LCCR0_Mono;
1343 } else if (!strncmp(this_opt, "active", 6)) {
1344 inf->lccr0 = (inf->lccr0 & ~LCCR0_PAS) | LCCR0_Act;
1345 } else if (!strncmp(this_opt, "passive", 7)) {
1346 inf->lccr0 = (inf->lccr0 & ~LCCR0_PAS) | LCCR0_Pas;
1347 } else if (!strncmp(this_opt, "single", 6)) {
1348 inf->lccr0 = (inf->lccr0 & ~LCCR0_SDS) | LCCR0_Sngl;
1349 } else if (!strncmp(this_opt, "dual", 4)) {
1350 inf->lccr0 = (inf->lccr0 & ~LCCR0_SDS) | LCCR0_Dual;
1351 } else if (!strncmp(this_opt, "4pix", 4)) {
1352 inf->lccr0 = (inf->lccr0 & ~LCCR0_DPD) | LCCR0_4PixMono;
1353 } else if (!strncmp(this_opt, "8pix", 4)) {
1354 inf->lccr0 = (inf->lccr0 & ~LCCR0_DPD) | LCCR0_8PixMono;
1355 } else {
1356 dev_err(dev, "unknown option: %s\n", this_opt);
1357 return -EINVAL;
1358 }
1359
1360 if (s[0] != '\0')
1361 dev_info(dev, "override %s", s);
1362
1363 return 0;
1364}
1365
1366static int __init pxafb_parse_options(struct device *dev, char *options)
1367{
1368 char *this_opt;
1369 int ret;
1370
1371 if (!options || !*options)
1372 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 dev_dbg(dev, "options are \"%s\"\n", options ? options : "null");
1375
1376 /* could be made table driven or similar?... */
eric miao817daf12008-04-30 00:52:18 -07001377 while ((this_opt = strsep(&options, ",")) != NULL) {
1378 ret = parse_opt(dev, this_opt);
1379 if (ret)
1380 return ret;
1381 }
1382 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383}
1384#endif
1385
Holger Schurigac2bf5b2008-02-11 16:52:30 +01001386static int __init pxafb_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
1388 struct pxafb_info *fbi;
1389 struct pxafb_mach_info *inf;
1390 int ret;
1391
Richard Purdie2cbbb3b2006-03-31 02:31:53 -08001392 dev_dbg(&dev->dev, "pxafb_probe\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Russell King3ae5eae2005-11-09 22:32:44 +00001394 inf = dev->dev.platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 ret = -ENOMEM;
1396 fbi = NULL;
1397 if (!inf)
1398 goto failed;
1399
1400#ifdef CONFIG_FB_PXA_PARAMETERS
Russell King3ae5eae2005-11-09 22:32:44 +00001401 ret = pxafb_parse_options(&dev->dev, g_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 if (ret < 0)
1403 goto failed;
1404#endif
1405
1406#ifdef DEBUG_VAR
eric miaob0086ef2008-04-30 00:52:19 -07001407 /* Check for various illegal bit-combinations. Currently only
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 * a warning is given. */
1409
eric miaob0086ef2008-04-30 00:52:19 -07001410 if (inf->lccr0 & LCCR0_INVALID_CONFIG_MASK)
1411 dev_warn(&dev->dev, "machine LCCR0 setting contains "
1412 "illegal bits: %08x\n",
1413 inf->lccr0 & LCCR0_INVALID_CONFIG_MASK);
1414 if (inf->lccr3 & LCCR3_INVALID_CONFIG_MASK)
1415 dev_warn(&dev->dev, "machine LCCR3 setting contains "
1416 "illegal bits: %08x\n",
1417 inf->lccr3 & LCCR3_INVALID_CONFIG_MASK);
1418 if (inf->lccr0 & LCCR0_DPD &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 ((inf->lccr0 & LCCR0_PAS) != LCCR0_Pas ||
1420 (inf->lccr0 & LCCR0_SDS) != LCCR0_Sngl ||
1421 (inf->lccr0 & LCCR0_CMS) != LCCR0_Mono))
eric miaob0086ef2008-04-30 00:52:19 -07001422 dev_warn(&dev->dev, "Double Pixel Data (DPD) mode is "
1423 "only valid in passive mono"
1424 " single panel mode\n");
1425 if ((inf->lccr0 & LCCR0_PAS) == LCCR0_Act &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 (inf->lccr0 & LCCR0_SDS) == LCCR0_Dual)
eric miaob0086ef2008-04-30 00:52:19 -07001427 dev_warn(&dev->dev, "Dual panel only valid in passive mode\n");
1428 if ((inf->lccr0 & LCCR0_PAS) == LCCR0_Pas &&
1429 (inf->modes->upper_margin || inf->modes->lower_margin))
1430 dev_warn(&dev->dev, "Upper and lower margins must be 0 in "
1431 "passive mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432#endif
1433
eric miaob0086ef2008-04-30 00:52:19 -07001434 dev_dbg(&dev->dev, "got a %dx%dx%d LCD\n",
1435 inf->modes->xres,
1436 inf->modes->yres,
1437 inf->modes->bpp);
1438 if (inf->modes->xres == 0 ||
1439 inf->modes->yres == 0 ||
1440 inf->modes->bpp == 0) {
Russell King3ae5eae2005-11-09 22:32:44 +00001441 dev_err(&dev->dev, "Invalid resolution or bit depth\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 ret = -EINVAL;
1443 goto failed;
1444 }
1445 pxafb_backlight_power = inf->pxafb_backlight_power;
1446 pxafb_lcd_power = inf->pxafb_lcd_power;
Russell King3ae5eae2005-11-09 22:32:44 +00001447 fbi = pxafb_init_fbinfo(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 if (!fbi) {
eric miaob0086ef2008-04-30 00:52:19 -07001449 /* only reason for pxafb_init_fbinfo to fail is kmalloc */
Russell King3ae5eae2005-11-09 22:32:44 +00001450 dev_err(&dev->dev, "Failed to initialize framebuffer device\n");
eric miaob0086ef2008-04-30 00:52:19 -07001451 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 goto failed;
1453 }
1454
1455 /* Initialize video memory */
1456 ret = pxafb_map_video_memory(fbi);
1457 if (ret) {
Russell King3ae5eae2005-11-09 22:32:44 +00001458 dev_err(&dev->dev, "Failed to allocate video RAM: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 ret = -ENOMEM;
1460 goto failed;
1461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Thomas Gleixner63a43392006-07-01 19:29:45 -07001463 ret = request_irq(IRQ_LCD, pxafb_handle_irq, IRQF_DISABLED, "LCD", fbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 if (ret) {
Russell King3ae5eae2005-11-09 22:32:44 +00001465 dev_err(&dev->dev, "request_irq failed: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 ret = -EBUSY;
1467 goto failed;
1468 }
1469
1470 /*
1471 * This makes sure that our colour bitfield
1472 * descriptors are correctly initialised.
1473 */
1474 pxafb_check_var(&fbi->fb.var, &fbi->fb);
1475 pxafb_set_par(&fbi->fb);
1476
Russell King3ae5eae2005-11-09 22:32:44 +00001477 platform_set_drvdata(dev, fbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
1479 ret = register_framebuffer(&fbi->fb);
1480 if (ret < 0) {
eric miaob0086ef2008-04-30 00:52:19 -07001481 dev_err(&dev->dev,
1482 "Failed to register framebuffer device: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 goto failed;
1484 }
1485
1486#ifdef CONFIG_PM
eric miaob0086ef2008-04-30 00:52:19 -07001487 /* TODO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488#endif
1489
1490#ifdef CONFIG_CPU_FREQ
1491 fbi->freq_transition.notifier_call = pxafb_freq_transition;
1492 fbi->freq_policy.notifier_call = pxafb_freq_policy;
eric miaob0086ef2008-04-30 00:52:19 -07001493 cpufreq_register_notifier(&fbi->freq_transition,
1494 CPUFREQ_TRANSITION_NOTIFIER);
1495 cpufreq_register_notifier(&fbi->freq_policy,
1496 CPUFREQ_POLICY_NOTIFIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497#endif
1498
1499 /*
1500 * Ok, now enable the LCD controller
1501 */
1502 set_ctrlr_state(fbi, C_ENABLE);
1503
1504 return 0;
1505
1506failed:
Russell King3ae5eae2005-11-09 22:32:44 +00001507 platform_set_drvdata(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 kfree(fbi);
1509 return ret;
1510}
1511
Russell King3ae5eae2005-11-09 22:32:44 +00001512static struct platform_driver pxafb_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 .probe = pxafb_probe,
1514#ifdef CONFIG_PM
1515 .suspend = pxafb_suspend,
1516 .resume = pxafb_resume,
1517#endif
Russell King3ae5eae2005-11-09 22:32:44 +00001518 .driver = {
1519 .name = "pxa2xx-fb",
1520 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521};
1522
1523#ifndef MODULE
Holger Schurigac2bf5b2008-02-11 16:52:30 +01001524static int __devinit pxafb_setup(char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
1526# ifdef CONFIG_FB_PXA_PARAMETERS
Ole Reinhardtfb79ffa2005-12-13 17:03:38 -08001527 if (options)
1528 strlcpy(g_options, options, sizeof(g_options));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529# endif
1530 return 0;
1531}
1532#else
1533# ifdef CONFIG_FB_PXA_PARAMETERS
1534module_param_string(options, g_options, sizeof(g_options), 0);
1535MODULE_PARM_DESC(options, "LCD parameters (see Documentation/fb/pxafb.txt)");
1536# endif
1537#endif
1538
Holger Schurigac2bf5b2008-02-11 16:52:30 +01001539static int __devinit pxafb_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
1541#ifndef MODULE
1542 char *option = NULL;
1543
1544 if (fb_get_options("pxafb", &option))
1545 return -ENODEV;
1546 pxafb_setup(option);
1547#endif
Russell King3ae5eae2005-11-09 22:32:44 +00001548 return platform_driver_register(&pxafb_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549}
1550
1551module_init(pxafb_init);
1552
1553MODULE_DESCRIPTION("loadable framebuffer driver for PXA");
1554MODULE_LICENSE("GPL");