xref: /openbmc/linux/drivers/video/fbdev/au1200fb.c (revision a99237af)
1 /*
2  * BRIEF MODULE DESCRIPTION
3  *	Au1200 LCD Driver.
4  *
5  * Copyright 2004-2005 AMD
6  * Author: AMD
7  *
8  * Based on:
9  * linux/drivers/video/skeletonfb.c -- Skeleton for a frame buffer device
10  *  Created 28 Dec 1997 by Geert Uytterhoeven
11  *
12  *  This program is free software; you can redistribute	 it and/or modify it
13  *  under  the terms of	 the GNU General  Public License as published by the
14  *  Free Software Foundation;  either version 2 of the	License, or (at your
15  *  option) any later version.
16  *
17  *  THIS  SOFTWARE  IS PROVIDED	  ``AS	IS'' AND   ANY	EXPRESS OR IMPLIED
18  *  WARRANTIES,	  INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
19  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
20  *  NO	EVENT  SHALL   THE AUTHOR  BE	 LIABLE FOR ANY	  DIRECT, INDIRECT,
21  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  *  NOT LIMITED	  TO, PROCUREMENT OF  SUBSTITUTE GOODS	OR SERVICES; LOSS OF
23  *  USE, DATA,	OR PROFITS; OR	BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  *  ANY THEORY OF LIABILITY, WHETHER IN	 CONTRACT, STRICT LIABILITY, OR TORT
25  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  *  You should have received a copy of the  GNU General Public License along
29  *  with this program; if not, write  to the Free Software Foundation, Inc.,
30  *  675 Mass Ave, Cambridge, MA 02139, USA.
31  */
32 
33 #include <linux/clk.h>
34 #include <linux/module.h>
35 #include <linux/platform_device.h>
36 #include <linux/kernel.h>
37 #include <linux/errno.h>
38 #include <linux/string.h>
39 #include <linux/mm.h>
40 #include <linux/fb.h>
41 #include <linux/init.h>
42 #include <linux/interrupt.h>
43 #include <linux/ctype.h>
44 #include <linux/dma-mapping.h>
45 #include <linux/slab.h>
46 #include <linux/uaccess.h>
47 
48 #include <asm/mach-au1x00/au1000.h>
49 #include <asm/mach-au1x00/au1200fb.h>	/* platform_data */
50 #include "au1200fb.h"
51 
52 #define DRIVER_NAME "au1200fb"
53 #define DRIVER_DESC "LCD controller driver for AU1200 processors"
54 
55 #define DEBUG 0
56 
57 #define print_err(f, arg...) printk(KERN_ERR DRIVER_NAME ": " f "\n", ## arg)
58 #define print_warn(f, arg...) printk(KERN_WARNING DRIVER_NAME ": " f "\n", ## arg)
59 #define print_info(f, arg...) printk(KERN_INFO DRIVER_NAME ": " f "\n", ## arg)
60 
61 #if DEBUG
62 #define print_dbg(f, arg...) printk(KERN_DEBUG __FILE__ ": " f "\n", ## arg)
63 #else
64 #define print_dbg(f, arg...) do {} while (0)
65 #endif
66 
67 
68 #define AU1200_LCD_FB_IOCTL 0x46FF
69 
70 #define AU1200_LCD_SET_SCREEN 1
71 #define AU1200_LCD_GET_SCREEN 2
72 #define AU1200_LCD_SET_WINDOW 3
73 #define AU1200_LCD_GET_WINDOW 4
74 #define AU1200_LCD_SET_PANEL  5
75 #define AU1200_LCD_GET_PANEL  6
76 
77 #define SCREEN_SIZE		    (1<< 1)
78 #define SCREEN_BACKCOLOR    (1<< 2)
79 #define SCREEN_BRIGHTNESS   (1<< 3)
80 #define SCREEN_COLORKEY     (1<< 4)
81 #define SCREEN_MASK         (1<< 5)
82 
83 struct au1200_lcd_global_regs_t {
84 	unsigned int flags;
85 	unsigned int xsize;
86 	unsigned int ysize;
87 	unsigned int backcolor;
88 	unsigned int brightness;
89 	unsigned int colorkey;
90 	unsigned int mask;
91 	unsigned int panel_choice;
92 	char panel_desc[80];
93 
94 };
95 
96 #define WIN_POSITION            (1<< 0)
97 #define WIN_ALPHA_COLOR         (1<< 1)
98 #define WIN_ALPHA_MODE          (1<< 2)
99 #define WIN_PRIORITY            (1<< 3)
100 #define WIN_CHANNEL             (1<< 4)
101 #define WIN_BUFFER_FORMAT       (1<< 5)
102 #define WIN_COLOR_ORDER         (1<< 6)
103 #define WIN_PIXEL_ORDER         (1<< 7)
104 #define WIN_SIZE                (1<< 8)
105 #define WIN_COLORKEY_MODE       (1<< 9)
106 #define WIN_DOUBLE_BUFFER_MODE  (1<< 10)
107 #define WIN_RAM_ARRAY_MODE      (1<< 11)
108 #define WIN_BUFFER_SCALE        (1<< 12)
109 #define WIN_ENABLE	            (1<< 13)
110 
111 struct au1200_lcd_window_regs_t {
112 	unsigned int flags;
113 	unsigned int xpos;
114 	unsigned int ypos;
115 	unsigned int alpha_color;
116 	unsigned int alpha_mode;
117 	unsigned int priority;
118 	unsigned int channel;
119 	unsigned int buffer_format;
120 	unsigned int color_order;
121 	unsigned int pixel_order;
122 	unsigned int xsize;
123 	unsigned int ysize;
124 	unsigned int colorkey_mode;
125 	unsigned int double_buffer_mode;
126 	unsigned int ram_array_mode;
127 	unsigned int xscale;
128 	unsigned int yscale;
129 	unsigned int enable;
130 };
131 
132 
133 struct au1200_lcd_iodata_t {
134 	unsigned int subcmd;
135 	struct au1200_lcd_global_regs_t global;
136 	struct au1200_lcd_window_regs_t window;
137 };
138 
139 #if defined(__BIG_ENDIAN)
140 #define LCD_CONTROL_DEFAULT_PO LCD_CONTROL_PO_11
141 #else
142 #define LCD_CONTROL_DEFAULT_PO LCD_CONTROL_PO_00
143 #endif
144 #define LCD_CONTROL_DEFAULT_SBPPF LCD_CONTROL_SBPPF_565
145 
146 /* Private, per-framebuffer management information (independent of the panel itself) */
147 struct au1200fb_device {
148 	struct fb_info *fb_info;		/* FB driver info record */
149 	struct au1200fb_platdata *pd;
150 
151 	int					plane;
152 	unsigned char* 		fb_mem;		/* FrameBuffer memory map */
153 	unsigned int		fb_len;
154 	dma_addr_t    		fb_phys;
155 };
156 
157 /********************************************************************/
158 
159 /* LCD controller restrictions */
160 #define AU1200_LCD_MAX_XRES	1280
161 #define AU1200_LCD_MAX_YRES	1024
162 #define AU1200_LCD_MAX_BPP	32
163 #define AU1200_LCD_MAX_CLK	96000000 /* fixme: this needs to go away ? */
164 #define AU1200_LCD_NBR_PALETTE_ENTRIES 256
165 
166 /* Default number of visible screen buffer to allocate */
167 #define AU1200FB_NBR_VIDEO_BUFFERS 1
168 
169 /* Default maximum number of fb devices to create */
170 #define MAX_DEVICE_COUNT	4
171 
172 /* Default window configuration entry to use (see windows[]) */
173 #define DEFAULT_WINDOW_INDEX	2
174 
175 /********************************************************************/
176 
177 static struct fb_info *_au1200fb_infos[MAX_DEVICE_COUNT];
178 static struct au1200_lcd *lcd = (struct au1200_lcd *) AU1200_LCD_ADDR;
179 static int device_count = MAX_DEVICE_COUNT;
180 static int window_index = DEFAULT_WINDOW_INDEX;	/* default is zero */
181 static int panel_index = 2; /* default is zero */
182 static struct window_settings *win;
183 static struct panel_settings *panel;
184 static int noblanking = 1;
185 static int nohwcursor = 0;
186 
187 struct window_settings {
188 	unsigned char name[64];
189 	uint32 mode_backcolor;
190 	uint32 mode_colorkey;
191 	uint32 mode_colorkeymsk;
192 	struct {
193 		int xres;
194 		int yres;
195 		int xpos;
196 		int ypos;
197 		uint32 mode_winctrl1; /* winctrl1[FRM,CCO,PO,PIPE] */
198 		uint32 mode_winenable;
199 	} w[4];
200 };
201 
202 #if defined(__BIG_ENDIAN)
203 #define LCD_WINCTRL1_PO_16BPP LCD_WINCTRL1_PO_00
204 #else
205 #define LCD_WINCTRL1_PO_16BPP LCD_WINCTRL1_PO_01
206 #endif
207 
208 /*
209  * Default window configurations
210  */
211 static struct window_settings windows[] = {
212 	{ /* Index 0 */
213 		"0-FS gfx, 1-video, 2-ovly gfx, 3-ovly gfx",
214 		/* mode_backcolor	*/ 0x006600ff,
215 		/* mode_colorkey,msk*/ 0, 0,
216 		{
217 			{
218 			/* xres, yres, xpos, ypos */ 0, 0, 0, 0,
219 			/* mode_winctrl1 */ LCD_WINCTRL1_FRM_16BPP565 |
220 				LCD_WINCTRL1_PO_16BPP,
221 			/* mode_winenable*/ LCD_WINENABLE_WEN0,
222 			},
223 			{
224 			/* xres, yres, xpos, ypos */ 100, 100, 100, 100,
225 			/* mode_winctrl1 */ LCD_WINCTRL1_FRM_16BPP565 |
226 				LCD_WINCTRL1_PO_16BPP |
227 				LCD_WINCTRL1_PIPE,
228 			/* mode_winenable*/ LCD_WINENABLE_WEN1,
229 			},
230 			{
231 			/* xres, yres, xpos, ypos */ 0, 0, 0, 0,
232 			/* mode_winctrl1 */ LCD_WINCTRL1_FRM_16BPP565 |
233 				LCD_WINCTRL1_PO_16BPP,
234 			/* mode_winenable*/ 0,
235 			},
236 			{
237 			/* xres, yres, xpos, ypos */ 0, 0, 0, 0,
238 			/* mode_winctrl1 */ LCD_WINCTRL1_FRM_16BPP565 |
239 				LCD_WINCTRL1_PO_16BPP |
240 				LCD_WINCTRL1_PIPE,
241 			/* mode_winenable*/ 0,
242 			},
243 		},
244 	},
245 
246 	{ /* Index 1 */
247 		"0-FS gfx, 1-video, 2-ovly gfx, 3-ovly gfx",
248 		/* mode_backcolor	*/ 0x006600ff,
249 		/* mode_colorkey,msk*/ 0, 0,
250 		{
251 			{
252 			/* xres, yres, xpos, ypos */ 320, 240, 5, 5,
253 			/* mode_winctrl1 */ LCD_WINCTRL1_FRM_24BPP |
254 				LCD_WINCTRL1_PO_00,
255 			/* mode_winenable*/ LCD_WINENABLE_WEN0,
256 			},
257 			{
258 			/* xres, yres, xpos, ypos */ 0, 0, 0, 0,
259 			/* mode_winctrl1 */ LCD_WINCTRL1_FRM_16BPP565
260 				| LCD_WINCTRL1_PO_16BPP,
261 			/* mode_winenable*/ 0,
262 			},
263 			{
264 			/* xres, yres, xpos, ypos */ 100, 100, 0, 0,
265 			/* mode_winctrl1 */ LCD_WINCTRL1_FRM_16BPP565 |
266 				LCD_WINCTRL1_PO_16BPP |
267 				LCD_WINCTRL1_PIPE,
268 			/* mode_winenable*/ 0/*LCD_WINENABLE_WEN2*/,
269 			},
270 			{
271 			/* xres, yres, xpos, ypos */ 200, 25, 0, 0,
272 			/* mode_winctrl1 */ LCD_WINCTRL1_FRM_16BPP565 |
273 				LCD_WINCTRL1_PO_16BPP |
274 				LCD_WINCTRL1_PIPE,
275 			/* mode_winenable*/ 0,
276 			},
277 		},
278 	},
279 	{ /* Index 2 */
280 		"0-FS gfx, 1-video, 2-ovly gfx, 3-ovly gfx",
281 		/* mode_backcolor	*/ 0x006600ff,
282 		/* mode_colorkey,msk*/ 0, 0,
283 		{
284 			{
285 			/* xres, yres, xpos, ypos */ 0, 0, 0, 0,
286 			/* mode_winctrl1 */ LCD_WINCTRL1_FRM_16BPP565 |
287 				LCD_WINCTRL1_PO_16BPP,
288 			/* mode_winenable*/ LCD_WINENABLE_WEN0,
289 			},
290 			{
291 			/* xres, yres, xpos, ypos */ 0, 0, 0, 0,
292 			/* mode_winctrl1 */ LCD_WINCTRL1_FRM_16BPP565 |
293 				LCD_WINCTRL1_PO_16BPP,
294 			/* mode_winenable*/ 0,
295 			},
296 			{
297 			/* xres, yres, xpos, ypos */ 0, 0, 0, 0,
298 			/* mode_winctrl1 */ LCD_WINCTRL1_FRM_32BPP |
299 				LCD_WINCTRL1_PO_00|LCD_WINCTRL1_PIPE,
300 			/* mode_winenable*/ 0/*LCD_WINENABLE_WEN2*/,
301 			},
302 			{
303 			/* xres, yres, xpos, ypos */ 0, 0, 0, 0,
304 			/* mode_winctrl1 */ LCD_WINCTRL1_FRM_16BPP565 |
305 				LCD_WINCTRL1_PO_16BPP |
306 				LCD_WINCTRL1_PIPE,
307 			/* mode_winenable*/ 0,
308 			},
309 		},
310 	},
311 	/* Need VGA 640 @ 24bpp, @ 32bpp */
312 	/* Need VGA 800 @ 24bpp, @ 32bpp */
313 	/* Need VGA 1024 @ 24bpp, @ 32bpp */
314 };
315 
316 /*
317  * Controller configurations for various panels.
318  */
319 
320 struct panel_settings
321 {
322 	const char name[25];		/* Full name <vendor>_<model> */
323 
324 	struct 	fb_monspecs monspecs; 	/* FB monitor specs */
325 
326 	/* panel timings */
327 	uint32 mode_screen;
328 	uint32 mode_horztiming;
329 	uint32 mode_verttiming;
330 	uint32 mode_clkcontrol;
331 	uint32 mode_pwmdiv;
332 	uint32 mode_pwmhi;
333 	uint32 mode_outmask;
334 	uint32 mode_fifoctrl;
335 	uint32 mode_backlight;
336 	uint32 lcdclk;
337 #define Xres min_xres
338 #define Yres min_yres
339 	u32	min_xres;		/* Minimum horizontal resolution */
340 	u32	max_xres;		/* Maximum horizontal resolution */
341 	u32 	min_yres;		/* Minimum vertical resolution */
342 	u32 	max_yres;		/* Maximum vertical resolution */
343 };
344 
345 /********************************************************************/
346 /* fixme: Maybe a modedb for the CRT ? otherwise panels should be as-is */
347 
348 /* List of panels known to work with the AU1200 LCD controller.
349  * To add a new panel, enter the same specifications as the
350  * Generic_TFT one, and MAKE SURE that it doesn't conflicts
351  * with the controller restrictions. Restrictions are:
352  *
353  * STN color panels: max_bpp <= 12
354  * STN mono panels: max_bpp <= 4
355  * TFT panels: max_bpp <= 16
356  * max_xres <= 800
357  * max_yres <= 600
358  */
359 static struct panel_settings known_lcd_panels[] =
360 {
361 	[0] = { /* QVGA 320x240 H:33.3kHz V:110Hz */
362 		.name = "QVGA_320x240",
363 		.monspecs = {
364 			.modedb = NULL,
365 			.modedb_len = 0,
366 			.hfmin = 30000,
367 			.hfmax = 70000,
368 			.vfmin = 60,
369 			.vfmax = 60,
370 			.dclkmin = 6000000,
371 			.dclkmax = 28000000,
372 			.input = FB_DISP_RGB,
373 		},
374 		.mode_screen		= LCD_SCREEN_SX_N(320) |
375 			LCD_SCREEN_SY_N(240),
376 		.mode_horztiming	= 0x00c4623b,
377 		.mode_verttiming	= 0x00502814,
378 		.mode_clkcontrol	= 0x00020002, /* /4=24Mhz */
379 		.mode_pwmdiv		= 0x00000000,
380 		.mode_pwmhi		= 0x00000000,
381 		.mode_outmask	= 0x00FFFFFF,
382 		.mode_fifoctrl	= 0x2f2f2f2f,
383 		.mode_backlight	= 0x00000000,
384 		.lcdclk		= 96,
385 		320, 320,
386 		240, 240,
387 	},
388 
389 	[1] = { /* VGA 640x480 H:30.3kHz V:58Hz */
390 		.name = "VGA_640x480",
391 		.monspecs = {
392 			.modedb = NULL,
393 			.modedb_len = 0,
394 			.hfmin = 30000,
395 			.hfmax = 70000,
396 			.vfmin = 60,
397 			.vfmax = 60,
398 			.dclkmin = 6000000,
399 			.dclkmax = 28000000,
400 			.input = FB_DISP_RGB,
401 		},
402 		.mode_screen		= 0x13f9df80,
403 		.mode_horztiming	= 0x003c5859,
404 		.mode_verttiming	= 0x00741201,
405 		.mode_clkcontrol	= 0x00020001, /* /4=24Mhz */
406 		.mode_pwmdiv		= 0x00000000,
407 		.mode_pwmhi		= 0x00000000,
408 		.mode_outmask	= 0x00FFFFFF,
409 		.mode_fifoctrl	= 0x2f2f2f2f,
410 		.mode_backlight	= 0x00000000,
411 		.lcdclk		= 96,
412 		640, 480,
413 		640, 480,
414 	},
415 
416 	[2] = { /* SVGA 800x600 H:46.1kHz V:69Hz */
417 		.name = "SVGA_800x600",
418 		.monspecs = {
419 			.modedb = NULL,
420 			.modedb_len = 0,
421 			.hfmin = 30000,
422 			.hfmax = 70000,
423 			.vfmin = 60,
424 			.vfmax = 60,
425 			.dclkmin = 6000000,
426 			.dclkmax = 28000000,
427 			.input = FB_DISP_RGB,
428 		},
429 		.mode_screen		= 0x18fa5780,
430 		.mode_horztiming	= 0x00dc7e77,
431 		.mode_verttiming	= 0x00584805,
432 		.mode_clkcontrol	= 0x00020000, /* /2=48Mhz */
433 		.mode_pwmdiv		= 0x00000000,
434 		.mode_pwmhi		= 0x00000000,
435 		.mode_outmask	= 0x00FFFFFF,
436 		.mode_fifoctrl	= 0x2f2f2f2f,
437 		.mode_backlight	= 0x00000000,
438 		.lcdclk		= 96,
439 		800, 800,
440 		600, 600,
441 	},
442 
443 	[3] = { /* XVGA 1024x768 H:56.2kHz V:70Hz */
444 		.name = "XVGA_1024x768",
445 		.monspecs = {
446 			.modedb = NULL,
447 			.modedb_len = 0,
448 			.hfmin = 30000,
449 			.hfmax = 70000,
450 			.vfmin = 60,
451 			.vfmax = 60,
452 			.dclkmin = 6000000,
453 			.dclkmax = 28000000,
454 			.input = FB_DISP_RGB,
455 		},
456 		.mode_screen		= 0x1ffaff80,
457 		.mode_horztiming	= 0x007d0e57,
458 		.mode_verttiming	= 0x00740a01,
459 		.mode_clkcontrol	= 0x000A0000, /* /1 */
460 		.mode_pwmdiv		= 0x00000000,
461 		.mode_pwmhi		= 0x00000000,
462 		.mode_outmask	= 0x00FFFFFF,
463 		.mode_fifoctrl	= 0x2f2f2f2f,
464 		.mode_backlight	= 0x00000000,
465 		.lcdclk		= 72,
466 		1024, 1024,
467 		768, 768,
468 	},
469 
470 	[4] = { /* XVGA XVGA 1280x1024 H:68.5kHz V:65Hz */
471 		.name = "XVGA_1280x1024",
472 		.monspecs = {
473 			.modedb = NULL,
474 			.modedb_len = 0,
475 			.hfmin = 30000,
476 			.hfmax = 70000,
477 			.vfmin = 60,
478 			.vfmax = 60,
479 			.dclkmin = 6000000,
480 			.dclkmax = 28000000,
481 			.input = FB_DISP_RGB,
482 		},
483 		.mode_screen		= 0x27fbff80,
484 		.mode_horztiming	= 0x00cdb2c7,
485 		.mode_verttiming	= 0x00600002,
486 		.mode_clkcontrol	= 0x000A0000, /* /1 */
487 		.mode_pwmdiv		= 0x00000000,
488 		.mode_pwmhi		= 0x00000000,
489 		.mode_outmask	= 0x00FFFFFF,
490 		.mode_fifoctrl	= 0x2f2f2f2f,
491 		.mode_backlight	= 0x00000000,
492 		.lcdclk		= 120,
493 		1280, 1280,
494 		1024, 1024,
495 	},
496 
497 	[5] = { /* Samsung 1024x768 TFT */
498 		.name = "Samsung_1024x768_TFT",
499 		.monspecs = {
500 			.modedb = NULL,
501 			.modedb_len = 0,
502 			.hfmin = 30000,
503 			.hfmax = 70000,
504 			.vfmin = 60,
505 			.vfmax = 60,
506 			.dclkmin = 6000000,
507 			.dclkmax = 28000000,
508 			.input = FB_DISP_RGB,
509 		},
510 		.mode_screen		= 0x1ffaff80,
511 		.mode_horztiming	= 0x018cc677,
512 		.mode_verttiming	= 0x00241217,
513 		.mode_clkcontrol	= 0x00000000, /* SCB 0x1 /4=24Mhz */
514 		.mode_pwmdiv		= 0x8000063f, /* SCB 0x0 */
515 		.mode_pwmhi		= 0x03400000, /* SCB 0x0 */
516 		.mode_outmask	= 0x00FFFFFF,
517 		.mode_fifoctrl	= 0x2f2f2f2f,
518 		.mode_backlight	= 0x00000000,
519 		.lcdclk		= 96,
520 		1024, 1024,
521 		768, 768,
522 	},
523 
524 	[6] = { /* Toshiba 640x480 TFT */
525 		.name = "Toshiba_640x480_TFT",
526 		.monspecs = {
527 			.modedb = NULL,
528 			.modedb_len = 0,
529 			.hfmin = 30000,
530 			.hfmax = 70000,
531 			.vfmin = 60,
532 			.vfmax = 60,
533 			.dclkmin = 6000000,
534 			.dclkmax = 28000000,
535 			.input = FB_DISP_RGB,
536 		},
537 		.mode_screen		= LCD_SCREEN_SX_N(640) |
538 			LCD_SCREEN_SY_N(480),
539 		.mode_horztiming	= LCD_HORZTIMING_HPW_N(96) |
540 			LCD_HORZTIMING_HND1_N(13) | LCD_HORZTIMING_HND2_N(51),
541 		.mode_verttiming	= LCD_VERTTIMING_VPW_N(2) |
542 			LCD_VERTTIMING_VND1_N(11) | LCD_VERTTIMING_VND2_N(32),
543 		.mode_clkcontrol	= 0x00000000, /* /4=24Mhz */
544 		.mode_pwmdiv		= 0x8000063f,
545 		.mode_pwmhi		= 0x03400000,
546 		.mode_outmask	= 0x00fcfcfc,
547 		.mode_fifoctrl	= 0x2f2f2f2f,
548 		.mode_backlight	= 0x00000000,
549 		.lcdclk		= 96,
550 		640, 480,
551 		640, 480,
552 	},
553 
554 	[7] = { /* Sharp 320x240 TFT */
555 		.name = "Sharp_320x240_TFT",
556 		.monspecs = {
557 			.modedb = NULL,
558 			.modedb_len = 0,
559 			.hfmin = 12500,
560 			.hfmax = 20000,
561 			.vfmin = 38,
562 			.vfmax = 81,
563 			.dclkmin = 4500000,
564 			.dclkmax = 6800000,
565 			.input = FB_DISP_RGB,
566 		},
567 		.mode_screen		= LCD_SCREEN_SX_N(320) |
568 			LCD_SCREEN_SY_N(240),
569 		.mode_horztiming	= LCD_HORZTIMING_HPW_N(60) |
570 			LCD_HORZTIMING_HND1_N(13) | LCD_HORZTIMING_HND2_N(2),
571 		.mode_verttiming	= LCD_VERTTIMING_VPW_N(2) |
572 			LCD_VERTTIMING_VND1_N(2) | LCD_VERTTIMING_VND2_N(5),
573 		.mode_clkcontrol	= LCD_CLKCONTROL_PCD_N(7), /*16=6Mhz*/
574 		.mode_pwmdiv		= 0x8000063f,
575 		.mode_pwmhi		= 0x03400000,
576 		.mode_outmask	= 0x00fcfcfc,
577 		.mode_fifoctrl	= 0x2f2f2f2f,
578 		.mode_backlight	= 0x00000000,
579 		.lcdclk		= 96, /* 96MHz AUXPLL */
580 		320, 320,
581 		240, 240,
582 	},
583 
584 	[8] = { /* Toppoly TD070WGCB2 7" 856x480 TFT */
585 		.name = "Toppoly_TD070WGCB2",
586 		.monspecs = {
587 			.modedb = NULL,
588 			.modedb_len = 0,
589 			.hfmin = 30000,
590 			.hfmax = 70000,
591 			.vfmin = 60,
592 			.vfmax = 60,
593 			.dclkmin = 6000000,
594 			.dclkmax = 28000000,
595 			.input = FB_DISP_RGB,
596 		},
597 		.mode_screen		= LCD_SCREEN_SX_N(856) |
598 			LCD_SCREEN_SY_N(480),
599 		.mode_horztiming	= LCD_HORZTIMING_HND2_N(43) |
600 			LCD_HORZTIMING_HND1_N(43) | LCD_HORZTIMING_HPW_N(114),
601 		.mode_verttiming	= LCD_VERTTIMING_VND2_N(20) |
602 			LCD_VERTTIMING_VND1_N(21) | LCD_VERTTIMING_VPW_N(4),
603 		.mode_clkcontrol	= 0x00020001, /* /4=24Mhz */
604 		.mode_pwmdiv		= 0x8000063f,
605 		.mode_pwmhi		= 0x03400000,
606 		.mode_outmask	= 0x00fcfcfc,
607 		.mode_fifoctrl	= 0x2f2f2f2f,
608 		.mode_backlight	= 0x00000000,
609 		.lcdclk		= 96,
610 		856, 856,
611 		480, 480,
612 	},
613 	[9] = {
614 		.name = "DB1300_800x480",
615 		.monspecs = {
616 			.modedb = NULL,
617 			.modedb_len = 0,
618 			.hfmin = 30000,
619 			.hfmax = 70000,
620 			.vfmin = 60,
621 			.vfmax = 60,
622 			.dclkmin = 6000000,
623 			.dclkmax = 28000000,
624 			.input = FB_DISP_RGB,
625 		},
626 		.mode_screen		= LCD_SCREEN_SX_N(800) |
627 					  LCD_SCREEN_SY_N(480),
628 		.mode_horztiming	= LCD_HORZTIMING_HPW_N(5) |
629 					  LCD_HORZTIMING_HND1_N(16) |
630 					  LCD_HORZTIMING_HND2_N(8),
631 		.mode_verttiming	= LCD_VERTTIMING_VPW_N(4) |
632 					  LCD_VERTTIMING_VND1_N(8) |
633 					  LCD_VERTTIMING_VND2_N(5),
634 		.mode_clkcontrol	= LCD_CLKCONTROL_PCD_N(1) |
635 					  LCD_CLKCONTROL_IV |
636 					  LCD_CLKCONTROL_IH,
637 		.mode_pwmdiv		= 0x00000000,
638 		.mode_pwmhi		= 0x00000000,
639 		.mode_outmask		= 0x00FFFFFF,
640 		.mode_fifoctrl		= 0x2f2f2f2f,
641 		.mode_backlight		= 0x00000000,
642 		.lcdclk			= 96,
643 		800, 800,
644 		480, 480,
645 	},
646 };
647 
648 #define NUM_PANELS (ARRAY_SIZE(known_lcd_panels))
649 
650 /********************************************************************/
651 
652 static int winbpp (unsigned int winctrl1)
653 {
654 	int bits = 0;
655 
656 	/* how many bits are needed for each pixel format */
657 	switch (winctrl1 & LCD_WINCTRL1_FRM) {
658 	case LCD_WINCTRL1_FRM_1BPP:
659 		bits = 1;
660 		break;
661 	case LCD_WINCTRL1_FRM_2BPP:
662 		bits = 2;
663 		break;
664 	case LCD_WINCTRL1_FRM_4BPP:
665 		bits = 4;
666 		break;
667 	case LCD_WINCTRL1_FRM_8BPP:
668 		bits = 8;
669 		break;
670 	case LCD_WINCTRL1_FRM_12BPP:
671 	case LCD_WINCTRL1_FRM_16BPP655:
672 	case LCD_WINCTRL1_FRM_16BPP565:
673 	case LCD_WINCTRL1_FRM_16BPP556:
674 	case LCD_WINCTRL1_FRM_16BPPI1555:
675 	case LCD_WINCTRL1_FRM_16BPPI5551:
676 	case LCD_WINCTRL1_FRM_16BPPA1555:
677 	case LCD_WINCTRL1_FRM_16BPPA5551:
678 		bits = 16;
679 		break;
680 	case LCD_WINCTRL1_FRM_24BPP:
681 	case LCD_WINCTRL1_FRM_32BPP:
682 		bits = 32;
683 		break;
684 	}
685 
686 	return bits;
687 }
688 
689 static int fbinfo2index (struct fb_info *fb_info)
690 {
691 	int i;
692 
693 	for (i = 0; i < device_count; ++i) {
694 		if (fb_info == _au1200fb_infos[i])
695 			return i;
696 	}
697 	printk("au1200fb: ERROR: fbinfo2index failed!\n");
698 	return -1;
699 }
700 
701 static int au1200_setlocation (struct au1200fb_device *fbdev, int plane,
702 	int xpos, int ypos)
703 {
704 	uint32 winctrl0, winctrl1, winenable, fb_offset = 0;
705 	int xsz, ysz;
706 
707 	/* FIX!!! NOT CHECKING FOR COMPLETE OFFSCREEN YET */
708 
709 	winctrl0 = lcd->window[plane].winctrl0;
710 	winctrl1 = lcd->window[plane].winctrl1;
711 	winctrl0 &= (LCD_WINCTRL0_A | LCD_WINCTRL0_AEN);
712 	winctrl1 &= ~(LCD_WINCTRL1_SZX | LCD_WINCTRL1_SZY);
713 
714 	/* Check for off-screen adjustments */
715 	xsz = win->w[plane].xres;
716 	ysz = win->w[plane].yres;
717 	if ((xpos + win->w[plane].xres) > panel->Xres) {
718 		/* Off-screen to the right */
719 		xsz = panel->Xres - xpos; /* off by 1 ??? */
720 		/*printk("off screen right\n");*/
721 	}
722 
723 	if ((ypos + win->w[plane].yres) > panel->Yres) {
724 		/* Off-screen to the bottom */
725 		ysz = panel->Yres - ypos; /* off by 1 ??? */
726 		/*printk("off screen bottom\n");*/
727 	}
728 
729 	if (xpos < 0) {
730 		/* Off-screen to the left */
731 		xsz = win->w[plane].xres + xpos;
732 		fb_offset += (((0 - xpos) * winbpp(lcd->window[plane].winctrl1))/8);
733 		xpos = 0;
734 		/*printk("off screen left\n");*/
735 	}
736 
737 	if (ypos < 0) {
738 		/* Off-screen to the top */
739 		ysz = win->w[plane].yres + ypos;
740 		/* fixme: fb_offset += ((0-ypos)*fb_pars[plane].line_length); */
741 		ypos = 0;
742 		/*printk("off screen top\n");*/
743 	}
744 
745 	/* record settings */
746 	win->w[plane].xpos = xpos;
747 	win->w[plane].ypos = ypos;
748 
749 	xsz -= 1;
750 	ysz -= 1;
751 	winctrl0 |= (xpos << 21);
752 	winctrl0 |= (ypos << 10);
753 	winctrl1 |= (xsz << 11);
754 	winctrl1 |= (ysz << 0);
755 
756 	/* Disable the window while making changes, then restore WINEN */
757 	winenable = lcd->winenable & (1 << plane);
758 	wmb(); /* drain writebuffer */
759 	lcd->winenable &= ~(1 << plane);
760 	lcd->window[plane].winctrl0 = winctrl0;
761 	lcd->window[plane].winctrl1 = winctrl1;
762 	lcd->window[plane].winbuf0 =
763 	lcd->window[plane].winbuf1 = fbdev->fb_phys;
764 	lcd->window[plane].winbufctrl = 0; /* select winbuf0 */
765 	lcd->winenable |= winenable;
766 	wmb(); /* drain writebuffer */
767 
768 	return 0;
769 }
770 
771 static void au1200_setpanel(struct panel_settings *newpanel,
772 			    struct au1200fb_platdata *pd)
773 {
774 	/*
775 	 * Perform global setup/init of LCD controller
776 	 */
777 	uint32 winenable;
778 
779 	/* Make sure all windows disabled */
780 	winenable = lcd->winenable;
781 	lcd->winenable = 0;
782 	wmb(); /* drain writebuffer */
783 	/*
784 	 * Ensure everything is disabled before reconfiguring
785 	 */
786 	if (lcd->screen & LCD_SCREEN_SEN) {
787 		/* Wait for vertical sync period */
788 		lcd->intstatus = LCD_INT_SS;
789 		while ((lcd->intstatus & LCD_INT_SS) == 0)
790 			;
791 
792 		lcd->screen &= ~LCD_SCREEN_SEN;	/*disable the controller*/
793 
794 		do {
795 			lcd->intstatus = lcd->intstatus; /*clear interrupts*/
796 			wmb(); /* drain writebuffer */
797 		/*wait for controller to shut down*/
798 		} while ((lcd->intstatus & LCD_INT_SD) == 0);
799 
800 		/* Call shutdown of current panel (if up) */
801 		/* this must occur last, because if an external clock is driving
802 		    the controller, the clock cannot be turned off before first
803 			shutting down the controller.
804 		 */
805 		if (pd->panel_shutdown)
806 			pd->panel_shutdown();
807 	}
808 
809 	/* Newpanel == NULL indicates a shutdown operation only */
810 	if (newpanel == NULL)
811 		return;
812 
813 	panel = newpanel;
814 
815 	printk("Panel(%s), %dx%d\n", panel->name, panel->Xres, panel->Yres);
816 
817 	/*
818 	 * Setup clocking if internal LCD clock source (assumes sys_auxpll valid)
819 	 */
820 	if (!(panel->mode_clkcontrol & LCD_CLKCONTROL_EXT))
821 	{
822 		struct clk *c = clk_get(NULL, "lcd_intclk");
823 		long r, pc = panel->lcdclk * 1000000;
824 
825 		if (!IS_ERR(c)) {
826 			r = clk_round_rate(c, pc);
827 			if ((pc - r) < (pc / 10)) {	/* 10% slack */
828 				clk_set_rate(c, r);
829 				clk_prepare_enable(c);
830 			}
831 			clk_put(c);
832 		}
833 	}
834 
835 	/*
836 	 * Configure panel timings
837 	 */
838 	lcd->screen = panel->mode_screen;
839 	lcd->horztiming = panel->mode_horztiming;
840 	lcd->verttiming = panel->mode_verttiming;
841 	lcd->clkcontrol = panel->mode_clkcontrol;
842 	lcd->pwmdiv = panel->mode_pwmdiv;
843 	lcd->pwmhi = panel->mode_pwmhi;
844 	lcd->outmask = panel->mode_outmask;
845 	lcd->fifoctrl = panel->mode_fifoctrl;
846 	wmb(); /* drain writebuffer */
847 
848 	/* fixme: Check window settings to make sure still valid
849 	 * for new geometry */
850 #if 0
851 	au1200_setlocation(fbdev, 0, win->w[0].xpos, win->w[0].ypos);
852 	au1200_setlocation(fbdev, 1, win->w[1].xpos, win->w[1].ypos);
853 	au1200_setlocation(fbdev, 2, win->w[2].xpos, win->w[2].ypos);
854 	au1200_setlocation(fbdev, 3, win->w[3].xpos, win->w[3].ypos);
855 #endif
856 	lcd->winenable = winenable;
857 
858 	/*
859 	 * Re-enable screen now that it is configured
860 	 */
861 	lcd->screen |= LCD_SCREEN_SEN;
862 	wmb(); /* drain writebuffer */
863 
864 	/* Call init of panel */
865 	if (pd->panel_init)
866 		pd->panel_init();
867 
868 	/* FIX!!!! not appropriate on panel change!!! Global setup/init */
869 	lcd->intenable = 0;
870 	lcd->intstatus = ~0;
871 	lcd->backcolor = win->mode_backcolor;
872 
873 	/* Setup Color Key - FIX!!! */
874 	lcd->colorkey = win->mode_colorkey;
875 	lcd->colorkeymsk = win->mode_colorkeymsk;
876 
877 	/* Setup HWCursor - FIX!!! Need to support this eventually */
878 	lcd->hwc.cursorctrl = 0;
879 	lcd->hwc.cursorpos = 0;
880 	lcd->hwc.cursorcolor0 = 0;
881 	lcd->hwc.cursorcolor1 = 0;
882 	lcd->hwc.cursorcolor2 = 0;
883 	lcd->hwc.cursorcolor3 = 0;
884 
885 
886 #if 0
887 #define D(X) printk("%25s: %08X\n", #X, X)
888 	D(lcd->screen);
889 	D(lcd->horztiming);
890 	D(lcd->verttiming);
891 	D(lcd->clkcontrol);
892 	D(lcd->pwmdiv);
893 	D(lcd->pwmhi);
894 	D(lcd->outmask);
895 	D(lcd->fifoctrl);
896 	D(lcd->window[0].winctrl0);
897 	D(lcd->window[0].winctrl1);
898 	D(lcd->window[0].winctrl2);
899 	D(lcd->window[0].winbuf0);
900 	D(lcd->window[0].winbuf1);
901 	D(lcd->window[0].winbufctrl);
902 	D(lcd->window[1].winctrl0);
903 	D(lcd->window[1].winctrl1);
904 	D(lcd->window[1].winctrl2);
905 	D(lcd->window[1].winbuf0);
906 	D(lcd->window[1].winbuf1);
907 	D(lcd->window[1].winbufctrl);
908 	D(lcd->window[2].winctrl0);
909 	D(lcd->window[2].winctrl1);
910 	D(lcd->window[2].winctrl2);
911 	D(lcd->window[2].winbuf0);
912 	D(lcd->window[2].winbuf1);
913 	D(lcd->window[2].winbufctrl);
914 	D(lcd->window[3].winctrl0);
915 	D(lcd->window[3].winctrl1);
916 	D(lcd->window[3].winctrl2);
917 	D(lcd->window[3].winbuf0);
918 	D(lcd->window[3].winbuf1);
919 	D(lcd->window[3].winbufctrl);
920 	D(lcd->winenable);
921 	D(lcd->intenable);
922 	D(lcd->intstatus);
923 	D(lcd->backcolor);
924 	D(lcd->winenable);
925 	D(lcd->colorkey);
926     D(lcd->colorkeymsk);
927 	D(lcd->hwc.cursorctrl);
928 	D(lcd->hwc.cursorpos);
929 	D(lcd->hwc.cursorcolor0);
930 	D(lcd->hwc.cursorcolor1);
931 	D(lcd->hwc.cursorcolor2);
932 	D(lcd->hwc.cursorcolor3);
933 #endif
934 }
935 
936 static void au1200_setmode(struct au1200fb_device *fbdev)
937 {
938 	int plane = fbdev->plane;
939 	/* Window/plane setup */
940 	lcd->window[plane].winctrl1 = ( 0
941 		| LCD_WINCTRL1_PRI_N(plane)
942 		| win->w[plane].mode_winctrl1 /* FRM,CCO,PO,PIPE */
943 		) ;
944 
945 	au1200_setlocation(fbdev, plane, win->w[plane].xpos, win->w[plane].ypos);
946 
947 	lcd->window[plane].winctrl2 = ( 0
948 		| LCD_WINCTRL2_CKMODE_00
949 		| LCD_WINCTRL2_DBM
950 		| LCD_WINCTRL2_BX_N(fbdev->fb_info->fix.line_length)
951 		| LCD_WINCTRL2_SCX_1
952 		| LCD_WINCTRL2_SCY_1
953 		) ;
954 	lcd->winenable |= win->w[plane].mode_winenable;
955 	wmb(); /* drain writebuffer */
956 }
957 
958 
959 /* Inline helpers */
960 
961 /*#define panel_is_dual(panel)  ((panel->mode_screen & LCD_SCREEN_PT) == LCD_SCREEN_PT_010)*/
962 /*#define panel_is_active(panel)((panel->mode_screen & LCD_SCREEN_PT) == LCD_SCREEN_PT_010)*/
963 
964 #define panel_is_color(panel) ((panel->mode_screen & LCD_SCREEN_PT) <= LCD_SCREEN_PT_CDSTN)
965 
966 /* Bitfields format supported by the controller. */
967 static struct fb_bitfield rgb_bitfields[][4] = {
968   	/*     Red, 	   Green, 	 Blue, 	     Transp   */
969 	[LCD_WINCTRL1_FRM_16BPP655 >> 25] =
970 		{ { 10, 6, 0 }, { 5, 5, 0 }, { 0, 5, 0 }, { 0, 0, 0 } },
971 
972 	[LCD_WINCTRL1_FRM_16BPP565 >> 25] =
973 		{ { 11, 5, 0 }, { 5, 6, 0 }, { 0, 5, 0 }, { 0, 0, 0 } },
974 
975 	[LCD_WINCTRL1_FRM_16BPP556 >> 25] =
976 		{ { 11, 5, 0 }, { 6, 5, 0 }, { 0, 6, 0 }, { 0, 0, 0 } },
977 
978 	[LCD_WINCTRL1_FRM_16BPPI1555 >> 25] =
979 		{ { 10, 5, 0 }, { 5, 5, 0 }, { 0, 5, 0 }, { 0, 0, 0 } },
980 
981 	[LCD_WINCTRL1_FRM_16BPPI5551 >> 25] =
982 		{ { 11, 5, 0 }, { 6, 5, 0 }, { 1, 5, 0 }, { 0, 0, 0 } },
983 
984 	[LCD_WINCTRL1_FRM_16BPPA1555 >> 25] =
985 		{ { 10, 5, 0 }, { 5, 5, 0 }, { 0, 5, 0 }, { 15, 1, 0 } },
986 
987 	[LCD_WINCTRL1_FRM_16BPPA5551 >> 25] =
988 		{ { 11, 5, 0 }, { 6, 5, 0 }, { 1, 5, 0 }, { 0, 1, 0 } },
989 
990 	[LCD_WINCTRL1_FRM_24BPP >> 25] =
991 		{ { 16, 8, 0 }, { 8, 8, 0 }, { 0, 8, 0 }, { 0, 0, 0 } },
992 
993 	[LCD_WINCTRL1_FRM_32BPP >> 25] =
994 		{ { 16, 8, 0 }, { 8, 8, 0 }, { 0, 8, 0 }, { 24, 0, 0 } },
995 };
996 
997 /*-------------------------------------------------------------------------*/
998 
999 /* Helpers */
1000 
1001 static void au1200fb_update_fbinfo(struct fb_info *fbi)
1002 {
1003 	/* FIX!!!! This also needs to take the window pixel format into account!!! */
1004 
1005 	/* Update var-dependent FB info */
1006 	if (panel_is_color(panel)) {
1007 		if (fbi->var.bits_per_pixel <= 8) {
1008 			/* palettized */
1009 			fbi->fix.visual = FB_VISUAL_PSEUDOCOLOR;
1010 			fbi->fix.line_length = fbi->var.xres_virtual /
1011 				(8/fbi->var.bits_per_pixel);
1012 		} else {
1013 			/* non-palettized */
1014 			fbi->fix.visual = FB_VISUAL_TRUECOLOR;
1015 			fbi->fix.line_length = fbi->var.xres_virtual * (fbi->var.bits_per_pixel / 8);
1016 		}
1017 	} else {
1018 		/* mono FIX!!! mono 8 and 4 bits */
1019 		fbi->fix.visual = FB_VISUAL_MONO10;
1020 		fbi->fix.line_length = fbi->var.xres_virtual / 8;
1021 	}
1022 
1023 	fbi->screen_size = fbi->fix.line_length * fbi->var.yres_virtual;
1024 	print_dbg("line length: %d\n", fbi->fix.line_length);
1025 	print_dbg("bits_per_pixel: %d\n", fbi->var.bits_per_pixel);
1026 }
1027 
1028 /*-------------------------------------------------------------------------*/
1029 
1030 /* AU1200 framebuffer driver */
1031 
1032 /* fb_check_var
1033  * Validate var settings with hardware restrictions and modify it if necessary
1034  */
1035 static int au1200fb_fb_check_var(struct fb_var_screeninfo *var,
1036 	struct fb_info *fbi)
1037 {
1038 	struct au1200fb_device *fbdev = fbi->par;
1039 	u32 pixclock;
1040 	int screen_size, plane;
1041 
1042 	plane = fbdev->plane;
1043 
1044 	/* Make sure that the mode respect all LCD controller and
1045 	 * panel restrictions. */
1046 	var->xres = win->w[plane].xres;
1047 	var->yres = win->w[plane].yres;
1048 
1049 	/* No need for virtual resolution support */
1050 	var->xres_virtual = var->xres;
1051 	var->yres_virtual = var->yres;
1052 
1053 	var->bits_per_pixel = winbpp(win->w[plane].mode_winctrl1);
1054 
1055 	screen_size = var->xres_virtual * var->yres_virtual;
1056 	if (var->bits_per_pixel > 8) screen_size *= (var->bits_per_pixel / 8);
1057 	else screen_size /= (8/var->bits_per_pixel);
1058 
1059 	if (fbdev->fb_len < screen_size)
1060 		return -EINVAL; /* Virtual screen is to big, abort */
1061 
1062 	/* FIX!!!! what are the implicaitons of ignoring this for windows ??? */
1063 	/* The max LCD clock is fixed to 48MHz (value of AUX_CLK). The pixel
1064 	 * clock can only be obtain by dividing this value by an even integer.
1065 	 * Fallback to a slower pixel clock if necessary. */
1066 	pixclock = max((u32)(PICOS2KHZ(var->pixclock) * 1000), fbi->monspecs.dclkmin);
1067 	pixclock = min3(pixclock, fbi->monspecs.dclkmax, (u32)AU1200_LCD_MAX_CLK/2);
1068 
1069 	if (AU1200_LCD_MAX_CLK % pixclock) {
1070 		int diff = AU1200_LCD_MAX_CLK % pixclock;
1071 		pixclock -= diff;
1072 	}
1073 
1074 	var->pixclock = KHZ2PICOS(pixclock/1000);
1075 #if 0
1076 	if (!panel_is_active(panel)) {
1077 		int pcd = AU1200_LCD_MAX_CLK / (pixclock * 2) - 1;
1078 
1079 		if (!panel_is_color(panel)
1080 			&& (panel->control_base & LCD_CONTROL_MPI) && (pcd < 3)) {
1081 			/* STN 8bit mono panel support is up to 6MHz pixclock */
1082 			var->pixclock = KHZ2PICOS(6000);
1083 		} else if (!pcd) {
1084 			/* Other STN panel support is up to 12MHz  */
1085 			var->pixclock = KHZ2PICOS(12000);
1086 		}
1087 	}
1088 #endif
1089 	/* Set bitfield accordingly */
1090 	switch (var->bits_per_pixel) {
1091 		case 16:
1092 		{
1093 			/* 16bpp True color.
1094 			 * These must be set to MATCH WINCTRL[FORM] */
1095 			int idx;
1096 			idx = (win->w[0].mode_winctrl1 & LCD_WINCTRL1_FRM) >> 25;
1097 			var->red    = rgb_bitfields[idx][0];
1098 			var->green  = rgb_bitfields[idx][1];
1099 			var->blue   = rgb_bitfields[idx][2];
1100 			var->transp = rgb_bitfields[idx][3];
1101 			break;
1102 		}
1103 
1104 		case 32:
1105 		{
1106 			/* 32bpp True color.
1107 			 * These must be set to MATCH WINCTRL[FORM] */
1108 			int idx;
1109 			idx = (win->w[0].mode_winctrl1 & LCD_WINCTRL1_FRM) >> 25;
1110 			var->red    = rgb_bitfields[idx][0];
1111 			var->green  = rgb_bitfields[idx][1];
1112 			var->blue   = rgb_bitfields[idx][2];
1113 			var->transp = rgb_bitfields[idx][3];
1114 			break;
1115 		}
1116 		default:
1117 			print_dbg("Unsupported depth %dbpp", var->bits_per_pixel);
1118 			return -EINVAL;
1119 	}
1120 
1121 	return 0;
1122 }
1123 
1124 /* fb_set_par
1125  * Set hardware with var settings. This will enable the controller with a
1126  * specific mode, normally validated with the fb_check_var method
1127  */
1128 static int au1200fb_fb_set_par(struct fb_info *fbi)
1129 {
1130 	struct au1200fb_device *fbdev = fbi->par;
1131 
1132 	au1200fb_update_fbinfo(fbi);
1133 	au1200_setmode(fbdev);
1134 
1135 	return 0;
1136 }
1137 
1138 /* fb_setcolreg
1139  * Set color in LCD palette.
1140  */
1141 static int au1200fb_fb_setcolreg(unsigned regno, unsigned red, unsigned green,
1142 	unsigned blue, unsigned transp, struct fb_info *fbi)
1143 {
1144 	volatile u32 *palette = lcd->palette;
1145 	u32 value;
1146 
1147 	if (regno > (AU1200_LCD_NBR_PALETTE_ENTRIES - 1))
1148 		return -EINVAL;
1149 
1150 	if (fbi->var.grayscale) {
1151 		/* Convert color to grayscale */
1152 		red = green = blue =
1153 			(19595 * red + 38470 * green + 7471 * blue) >> 16;
1154 	}
1155 
1156 	if (fbi->fix.visual == FB_VISUAL_TRUECOLOR) {
1157 		/* Place color in the pseudopalette */
1158 		if (regno > 16)
1159 			return -EINVAL;
1160 
1161 		palette = (u32*) fbi->pseudo_palette;
1162 
1163 		red   >>= (16 - fbi->var.red.length);
1164 		green >>= (16 - fbi->var.green.length);
1165 		blue  >>= (16 - fbi->var.blue.length);
1166 
1167 		value = (red   << fbi->var.red.offset) 	|
1168 			(green << fbi->var.green.offset)|
1169 			(blue  << fbi->var.blue.offset);
1170 		value &= 0xFFFF;
1171 
1172 	} else if (1 /*FIX!!! panel_is_active(fbdev->panel)*/) {
1173 		/* COLOR TFT PALLETTIZED (use RGB 565) */
1174 		value = (red & 0xF800)|((green >> 5) &
1175 				0x07E0)|((blue >> 11) & 0x001F);
1176 		value &= 0xFFFF;
1177 
1178 	} else if (0 /*panel_is_color(fbdev->panel)*/) {
1179 		/* COLOR STN MODE */
1180 		value = 0x1234;
1181 		value &= 0xFFF;
1182 	} else {
1183 		/* MONOCHROME MODE */
1184 		value = (green >> 12) & 0x000F;
1185 		value &= 0xF;
1186 	}
1187 
1188 	palette[regno] = value;
1189 
1190 	return 0;
1191 }
1192 
1193 /* fb_blank
1194  * Blank the screen. Depending on the mode, the screen will be
1195  * activated with the backlight color, or desactivated
1196  */
1197 static int au1200fb_fb_blank(int blank_mode, struct fb_info *fbi)
1198 {
1199 	struct au1200fb_device *fbdev = fbi->par;
1200 
1201 	/* Short-circuit screen blanking */
1202 	if (noblanking)
1203 		return 0;
1204 
1205 	switch (blank_mode) {
1206 
1207 	case FB_BLANK_UNBLANK:
1208 	case FB_BLANK_NORMAL:
1209 		/* printk("turn on panel\n"); */
1210 		au1200_setpanel(panel, fbdev->pd);
1211 		break;
1212 	case FB_BLANK_VSYNC_SUSPEND:
1213 	case FB_BLANK_HSYNC_SUSPEND:
1214 	case FB_BLANK_POWERDOWN:
1215 		/* printk("turn off panel\n"); */
1216 		au1200_setpanel(NULL, fbdev->pd);
1217 		break;
1218 	default:
1219 		break;
1220 
1221 	}
1222 
1223 	/* FB_BLANK_NORMAL is a soft blank */
1224 	return (blank_mode == FB_BLANK_NORMAL) ? -EINVAL : 0;
1225 }
1226 
1227 /* fb_mmap
1228  * Map video memory in user space. We don't use the generic fb_mmap
1229  * method mainly to allow the use of the TLB streaming flag (CCA=6)
1230  */
1231 static int au1200fb_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
1232 {
1233 	struct au1200fb_device *fbdev = info->par;
1234 
1235 	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1236 	pgprot_val(vma->vm_page_prot) |= _CACHE_MASK; /* CCA=7 */
1237 
1238 	return vm_iomap_memory(vma, fbdev->fb_phys, fbdev->fb_len);
1239 }
1240 
1241 static void set_global(u_int cmd, struct au1200_lcd_global_regs_t *pdata)
1242 {
1243 
1244 	unsigned int hi1, divider;
1245 
1246 	/* SCREEN_SIZE: user cannot reset size, must switch panel choice */
1247 
1248 	if (pdata->flags & SCREEN_BACKCOLOR)
1249 		lcd->backcolor = pdata->backcolor;
1250 
1251 	if (pdata->flags & SCREEN_BRIGHTNESS) {
1252 
1253 		// limit brightness pwm duty to >= 30/1600
1254 		if (pdata->brightness < 30) {
1255 			pdata->brightness = 30;
1256 		}
1257 		divider = (lcd->pwmdiv & 0x3FFFF) + 1;
1258 		hi1 = (((pdata->brightness & 0xFF)+1) * divider >> 8);
1259 		lcd->pwmhi &= 0xFFFF;
1260 		lcd->pwmhi |= (hi1 << 16);
1261 	}
1262 
1263 	if (pdata->flags & SCREEN_COLORKEY)
1264 		lcd->colorkey = pdata->colorkey;
1265 
1266 	if (pdata->flags & SCREEN_MASK)
1267 		lcd->colorkeymsk = pdata->mask;
1268 	wmb(); /* drain writebuffer */
1269 }
1270 
1271 static void get_global(u_int cmd, struct au1200_lcd_global_regs_t *pdata)
1272 {
1273 	unsigned int hi1, divider;
1274 
1275 	pdata->xsize = ((lcd->screen & LCD_SCREEN_SX) >> 19) + 1;
1276 	pdata->ysize = ((lcd->screen & LCD_SCREEN_SY) >> 8) + 1;
1277 
1278 	pdata->backcolor = lcd->backcolor;
1279 	pdata->colorkey = lcd->colorkey;
1280 	pdata->mask = lcd->colorkeymsk;
1281 
1282 	// brightness
1283 	hi1 = (lcd->pwmhi >> 16) + 1;
1284 	divider = (lcd->pwmdiv & 0x3FFFF) + 1;
1285 	pdata->brightness = ((hi1 << 8) / divider) - 1;
1286 	wmb(); /* drain writebuffer */
1287 }
1288 
1289 static void set_window(unsigned int plane,
1290 	struct au1200_lcd_window_regs_t *pdata)
1291 {
1292 	unsigned int val, bpp;
1293 
1294 	/* Window control register 0 */
1295 	if (pdata->flags & WIN_POSITION) {
1296 		val = lcd->window[plane].winctrl0 & ~(LCD_WINCTRL0_OX |
1297 				LCD_WINCTRL0_OY);
1298 		val |= ((pdata->xpos << 21) & LCD_WINCTRL0_OX);
1299 		val |= ((pdata->ypos << 10) & LCD_WINCTRL0_OY);
1300 		lcd->window[plane].winctrl0 = val;
1301 	}
1302 	if (pdata->flags & WIN_ALPHA_COLOR) {
1303 		val = lcd->window[plane].winctrl0 & ~(LCD_WINCTRL0_A);
1304 		val |= ((pdata->alpha_color << 2) & LCD_WINCTRL0_A);
1305 		lcd->window[plane].winctrl0 = val;
1306 	}
1307 	if (pdata->flags & WIN_ALPHA_MODE) {
1308 		val = lcd->window[plane].winctrl0 & ~(LCD_WINCTRL0_AEN);
1309 		val |= ((pdata->alpha_mode << 1) & LCD_WINCTRL0_AEN);
1310 		lcd->window[plane].winctrl0 = val;
1311 	}
1312 
1313 	/* Window control register 1 */
1314 	if (pdata->flags & WIN_PRIORITY) {
1315 		val = lcd->window[plane].winctrl1 & ~(LCD_WINCTRL1_PRI);
1316 		val |= ((pdata->priority << 30) & LCD_WINCTRL1_PRI);
1317 		lcd->window[plane].winctrl1 = val;
1318 	}
1319 	if (pdata->flags & WIN_CHANNEL) {
1320 		val = lcd->window[plane].winctrl1 & ~(LCD_WINCTRL1_PIPE);
1321 		val |= ((pdata->channel << 29) & LCD_WINCTRL1_PIPE);
1322 		lcd->window[plane].winctrl1 = val;
1323 	}
1324 	if (pdata->flags & WIN_BUFFER_FORMAT) {
1325 		val = lcd->window[plane].winctrl1 & ~(LCD_WINCTRL1_FRM);
1326 		val |= ((pdata->buffer_format << 25) & LCD_WINCTRL1_FRM);
1327 		lcd->window[plane].winctrl1 = val;
1328 	}
1329 	if (pdata->flags & WIN_COLOR_ORDER) {
1330 		val = lcd->window[plane].winctrl1 & ~(LCD_WINCTRL1_CCO);
1331 		val |= ((pdata->color_order << 24) & LCD_WINCTRL1_CCO);
1332 		lcd->window[plane].winctrl1 = val;
1333 	}
1334 	if (pdata->flags & WIN_PIXEL_ORDER) {
1335 		val = lcd->window[plane].winctrl1 & ~(LCD_WINCTRL1_PO);
1336 		val |= ((pdata->pixel_order << 22) & LCD_WINCTRL1_PO);
1337 		lcd->window[plane].winctrl1 = val;
1338 	}
1339 	if (pdata->flags & WIN_SIZE) {
1340 		val = lcd->window[plane].winctrl1 & ~(LCD_WINCTRL1_SZX |
1341 				LCD_WINCTRL1_SZY);
1342 		val |= (((pdata->xsize << 11) - 1) & LCD_WINCTRL1_SZX);
1343 		val |= (((pdata->ysize) - 1) & LCD_WINCTRL1_SZY);
1344 		lcd->window[plane].winctrl1 = val;
1345 		/* program buffer line width */
1346 		bpp = winbpp(val) / 8;
1347 		val = lcd->window[plane].winctrl2 & ~(LCD_WINCTRL2_BX);
1348 		val |= (((pdata->xsize * bpp) << 8) & LCD_WINCTRL2_BX);
1349 		lcd->window[plane].winctrl2 = val;
1350 	}
1351 
1352 	/* Window control register 2 */
1353 	if (pdata->flags & WIN_COLORKEY_MODE) {
1354 		val = lcd->window[plane].winctrl2 & ~(LCD_WINCTRL2_CKMODE);
1355 		val |= ((pdata->colorkey_mode << 24) & LCD_WINCTRL2_CKMODE);
1356 		lcd->window[plane].winctrl2 = val;
1357 	}
1358 	if (pdata->flags & WIN_DOUBLE_BUFFER_MODE) {
1359 		val = lcd->window[plane].winctrl2 & ~(LCD_WINCTRL2_DBM);
1360 		val |= ((pdata->double_buffer_mode << 23) & LCD_WINCTRL2_DBM);
1361 		lcd->window[plane].winctrl2 = val;
1362 	}
1363 	if (pdata->flags & WIN_RAM_ARRAY_MODE) {
1364 		val = lcd->window[plane].winctrl2 & ~(LCD_WINCTRL2_RAM);
1365 		val |= ((pdata->ram_array_mode << 21) & LCD_WINCTRL2_RAM);
1366 		lcd->window[plane].winctrl2 = val;
1367 	}
1368 
1369 	/* Buffer line width programmed with WIN_SIZE */
1370 
1371 	if (pdata->flags & WIN_BUFFER_SCALE) {
1372 		val = lcd->window[plane].winctrl2 & ~(LCD_WINCTRL2_SCX |
1373 				LCD_WINCTRL2_SCY);
1374 		val |= ((pdata->xsize << 11) & LCD_WINCTRL2_SCX);
1375 		val |= ((pdata->ysize) & LCD_WINCTRL2_SCY);
1376 		lcd->window[plane].winctrl2 = val;
1377 	}
1378 
1379 	if (pdata->flags & WIN_ENABLE) {
1380 		val = lcd->winenable;
1381 		val &= ~(1<<plane);
1382 		val |= (pdata->enable & 1) << plane;
1383 		lcd->winenable = val;
1384 	}
1385 	wmb(); /* drain writebuffer */
1386 }
1387 
1388 static void get_window(unsigned int plane,
1389 	struct au1200_lcd_window_regs_t *pdata)
1390 {
1391 	/* Window control register 0 */
1392 	pdata->xpos = (lcd->window[plane].winctrl0 & LCD_WINCTRL0_OX) >> 21;
1393 	pdata->ypos = (lcd->window[plane].winctrl0 & LCD_WINCTRL0_OY) >> 10;
1394 	pdata->alpha_color = (lcd->window[plane].winctrl0 & LCD_WINCTRL0_A) >> 2;
1395 	pdata->alpha_mode = (lcd->window[plane].winctrl0 & LCD_WINCTRL0_AEN) >> 1;
1396 
1397 	/* Window control register 1 */
1398 	pdata->priority = (lcd->window[plane].winctrl1& LCD_WINCTRL1_PRI) >> 30;
1399 	pdata->channel = (lcd->window[plane].winctrl1 & LCD_WINCTRL1_PIPE) >> 29;
1400 	pdata->buffer_format = (lcd->window[plane].winctrl1 & LCD_WINCTRL1_FRM) >> 25;
1401 	pdata->color_order = (lcd->window[plane].winctrl1 & LCD_WINCTRL1_CCO) >> 24;
1402 	pdata->pixel_order = (lcd->window[plane].winctrl1 & LCD_WINCTRL1_PO) >> 22;
1403 	pdata->xsize = ((lcd->window[plane].winctrl1 & LCD_WINCTRL1_SZX) >> 11) + 1;
1404 	pdata->ysize = (lcd->window[plane].winctrl1 & LCD_WINCTRL1_SZY) + 1;
1405 
1406 	/* Window control register 2 */
1407 	pdata->colorkey_mode = (lcd->window[plane].winctrl2 & LCD_WINCTRL2_CKMODE) >> 24;
1408 	pdata->double_buffer_mode = (lcd->window[plane].winctrl2 & LCD_WINCTRL2_DBM) >> 23;
1409 	pdata->ram_array_mode = (lcd->window[plane].winctrl2 & LCD_WINCTRL2_RAM) >> 21;
1410 
1411 	pdata->enable = (lcd->winenable >> plane) & 1;
1412 	wmb(); /* drain writebuffer */
1413 }
1414 
1415 static int au1200fb_ioctl(struct fb_info *info, unsigned int cmd,
1416                           unsigned long arg)
1417 {
1418 	struct au1200fb_device *fbdev = info->par;
1419 	int plane;
1420 	int val;
1421 
1422 	plane = fbinfo2index(info);
1423 	print_dbg("au1200fb: ioctl %d on plane %d\n", cmd, plane);
1424 
1425 	if (cmd == AU1200_LCD_FB_IOCTL) {
1426 		struct au1200_lcd_iodata_t iodata;
1427 
1428 		if (copy_from_user(&iodata, (void __user *) arg, sizeof(iodata)))
1429 			return -EFAULT;
1430 
1431 		print_dbg("FB IOCTL called\n");
1432 
1433 		switch (iodata.subcmd) {
1434 		case AU1200_LCD_SET_SCREEN:
1435 			print_dbg("AU1200_LCD_SET_SCREEN\n");
1436 			set_global(cmd, &iodata.global);
1437 			break;
1438 
1439 		case AU1200_LCD_GET_SCREEN:
1440 			print_dbg("AU1200_LCD_GET_SCREEN\n");
1441 			get_global(cmd, &iodata.global);
1442 			break;
1443 
1444 		case AU1200_LCD_SET_WINDOW:
1445 			print_dbg("AU1200_LCD_SET_WINDOW\n");
1446 			set_window(plane, &iodata.window);
1447 			break;
1448 
1449 		case AU1200_LCD_GET_WINDOW:
1450 			print_dbg("AU1200_LCD_GET_WINDOW\n");
1451 			get_window(plane, &iodata.window);
1452 			break;
1453 
1454 		case AU1200_LCD_SET_PANEL:
1455 			print_dbg("AU1200_LCD_SET_PANEL\n");
1456 			if ((iodata.global.panel_choice >= 0) &&
1457 					(iodata.global.panel_choice <
1458 					 NUM_PANELS))
1459 			{
1460 				struct panel_settings *newpanel;
1461 				panel_index = iodata.global.panel_choice;
1462 				newpanel = &known_lcd_panels[panel_index];
1463 				au1200_setpanel(newpanel, fbdev->pd);
1464 			}
1465 			break;
1466 
1467 		case AU1200_LCD_GET_PANEL:
1468 			print_dbg("AU1200_LCD_GET_PANEL\n");
1469 			iodata.global.panel_choice = panel_index;
1470 			break;
1471 
1472 		default:
1473 			return -EINVAL;
1474 		}
1475 
1476 		val = copy_to_user((void __user *) arg, &iodata, sizeof(iodata));
1477 		if (val) {
1478 			print_dbg("error: could not copy %d bytes\n", val);
1479 			return -EFAULT;
1480 		}
1481 	}
1482 
1483 	return 0;
1484 }
1485 
1486 
1487 static struct fb_ops au1200fb_fb_ops = {
1488 	.owner		= THIS_MODULE,
1489 	.fb_check_var	= au1200fb_fb_check_var,
1490 	.fb_set_par	= au1200fb_fb_set_par,
1491 	.fb_setcolreg	= au1200fb_fb_setcolreg,
1492 	.fb_blank	= au1200fb_fb_blank,
1493 	.fb_fillrect	= sys_fillrect,
1494 	.fb_copyarea	= sys_copyarea,
1495 	.fb_imageblit	= sys_imageblit,
1496 	.fb_read	= fb_sys_read,
1497 	.fb_write	= fb_sys_write,
1498 	.fb_sync	= NULL,
1499 	.fb_ioctl	= au1200fb_ioctl,
1500 	.fb_mmap	= au1200fb_fb_mmap,
1501 };
1502 
1503 /*-------------------------------------------------------------------------*/
1504 
1505 static irqreturn_t au1200fb_handle_irq(int irq, void* dev_id)
1506 {
1507 	/* Nothing to do for now, just clear any pending interrupt */
1508 	lcd->intstatus = lcd->intstatus;
1509 	wmb(); /* drain writebuffer */
1510 
1511 	return IRQ_HANDLED;
1512 }
1513 
1514 /*-------------------------------------------------------------------------*/
1515 
1516 /* AU1200 LCD device probe helpers */
1517 
1518 static int au1200fb_init_fbinfo(struct au1200fb_device *fbdev)
1519 {
1520 	struct fb_info *fbi = fbdev->fb_info;
1521 	int bpp, ret;
1522 
1523 	fbi->fbops = &au1200fb_fb_ops;
1524 
1525 	bpp = winbpp(win->w[fbdev->plane].mode_winctrl1);
1526 
1527 	/* Copy monitor specs from panel data */
1528 	/* fixme: we're setting up LCD controller windows, so these dont give a
1529 	damn as to what the monitor specs are (the panel itself does, but that
1530 	isn't done here...so maybe need a generic catchall monitor setting??? */
1531 	memcpy(&fbi->monspecs, &panel->monspecs, sizeof(struct fb_monspecs));
1532 
1533 	/* We first try the user mode passed in argument. If that failed,
1534 	 * or if no one has been specified, we default to the first mode of the
1535 	 * panel list. Note that after this call, var data will be set */
1536 	if (!fb_find_mode(&fbi->var,
1537 			  fbi,
1538 			  NULL, /* drv_info.opt_mode, */
1539 			  fbi->monspecs.modedb,
1540 			  fbi->monspecs.modedb_len,
1541 			  fbi->monspecs.modedb,
1542 			  bpp)) {
1543 
1544 		print_err("Cannot find valid mode for panel %s", panel->name);
1545 		return -EFAULT;
1546 	}
1547 
1548 	fbi->pseudo_palette = kcalloc(16, sizeof(u32), GFP_KERNEL);
1549 	if (!fbi->pseudo_palette)
1550 		return -ENOMEM;
1551 
1552 	ret = fb_alloc_cmap(&fbi->cmap, AU1200_LCD_NBR_PALETTE_ENTRIES, 0);
1553 	if (ret < 0) {
1554 		print_err("Fail to allocate colormap (%d entries)",
1555 			  AU1200_LCD_NBR_PALETTE_ENTRIES);
1556 		return ret;
1557 	}
1558 
1559 	strncpy(fbi->fix.id, "AU1200", sizeof(fbi->fix.id));
1560 	fbi->fix.smem_start = fbdev->fb_phys;
1561 	fbi->fix.smem_len = fbdev->fb_len;
1562 	fbi->fix.type = FB_TYPE_PACKED_PIXELS;
1563 	fbi->fix.xpanstep = 0;
1564 	fbi->fix.ypanstep = 0;
1565 	fbi->fix.mmio_start = 0;
1566 	fbi->fix.mmio_len = 0;
1567 	fbi->fix.accel = FB_ACCEL_NONE;
1568 
1569 	fbi->screen_base = (char __iomem *) fbdev->fb_mem;
1570 
1571 	au1200fb_update_fbinfo(fbi);
1572 
1573 	return 0;
1574 }
1575 
1576 /*-------------------------------------------------------------------------*/
1577 
1578 
1579 static int au1200fb_setup(struct au1200fb_platdata *pd)
1580 {
1581 	char *options = NULL;
1582 	char *this_opt, *endptr;
1583 	int num_panels = ARRAY_SIZE(known_lcd_panels);
1584 	int panel_idx = -1;
1585 
1586 	fb_get_options(DRIVER_NAME, &options);
1587 
1588 	if (!options)
1589 		goto out;
1590 
1591 	while ((this_opt = strsep(&options, ",")) != NULL) {
1592 		/* Panel option - can be panel name,
1593 		 * "bs" for board-switch, or number/index */
1594 		if (!strncmp(this_opt, "panel:", 6)) {
1595 			int i;
1596 			long int li;
1597 			char *endptr;
1598 			this_opt += 6;
1599 			/* First check for index, which allows
1600 			 * to short circuit this mess */
1601 			li = simple_strtol(this_opt, &endptr, 0);
1602 			if (*endptr == '\0')
1603 				panel_idx = (int)li;
1604 			else if (strcmp(this_opt, "bs") == 0)
1605 				panel_idx = pd->panel_index();
1606 			else {
1607 				for (i = 0; i < num_panels; i++) {
1608 					if (!strcmp(this_opt,
1609 						    known_lcd_panels[i].name)) {
1610 						panel_idx = i;
1611 						break;
1612 					}
1613 				}
1614 			}
1615 			if ((panel_idx < 0) || (panel_idx >= num_panels))
1616 				print_warn("Panel %s not supported!", this_opt);
1617 			else
1618 				panel_index = panel_idx;
1619 
1620 		} else if (strncmp(this_opt, "nohwcursor", 10) == 0)
1621 			nohwcursor = 1;
1622 		else if (strncmp(this_opt, "devices:", 8) == 0) {
1623 			this_opt += 8;
1624 			device_count = simple_strtol(this_opt, &endptr, 0);
1625 			if ((device_count < 0) ||
1626 			    (device_count > MAX_DEVICE_COUNT))
1627 				device_count = MAX_DEVICE_COUNT;
1628 		} else if (strncmp(this_opt, "wincfg:", 7) == 0) {
1629 			this_opt += 7;
1630 			window_index = simple_strtol(this_opt, &endptr, 0);
1631 			if ((window_index < 0) ||
1632 			    (window_index >= ARRAY_SIZE(windows)))
1633 				window_index = DEFAULT_WINDOW_INDEX;
1634 		} else if (strncmp(this_opt, "off", 3) == 0)
1635 			return 1;
1636 		else
1637 			print_warn("Unsupported option \"%s\"", this_opt);
1638 	}
1639 
1640 out:
1641 	return 0;
1642 }
1643 
1644 /* AU1200 LCD controller device driver */
1645 static int au1200fb_drv_probe(struct platform_device *dev)
1646 {
1647 	struct au1200fb_device *fbdev;
1648 	struct au1200fb_platdata *pd;
1649 	struct fb_info *fbi = NULL;
1650 	unsigned long page;
1651 	int bpp, plane, ret, irq;
1652 
1653 	print_info("" DRIVER_DESC "");
1654 
1655 	pd = dev->dev.platform_data;
1656 	if (!pd)
1657 		return -ENODEV;
1658 
1659 	/* Setup driver with options */
1660 	if (au1200fb_setup(pd))
1661 		return -ENODEV;
1662 
1663 	/* Point to the panel selected */
1664 	panel = &known_lcd_panels[panel_index];
1665 	win = &windows[window_index];
1666 
1667 	printk(DRIVER_NAME ": Panel %d %s\n", panel_index, panel->name);
1668 	printk(DRIVER_NAME ": Win %d %s\n", window_index, win->name);
1669 
1670 	for (plane = 0; plane < device_count; ++plane) {
1671 		bpp = winbpp(win->w[plane].mode_winctrl1);
1672 		if (win->w[plane].xres == 0)
1673 			win->w[plane].xres = panel->Xres;
1674 		if (win->w[plane].yres == 0)
1675 			win->w[plane].yres = panel->Yres;
1676 
1677 		fbi = framebuffer_alloc(sizeof(struct au1200fb_device),
1678 					&dev->dev);
1679 		if (!fbi) {
1680 			ret = -ENOMEM;
1681 			goto failed;
1682 		}
1683 
1684 		_au1200fb_infos[plane] = fbi;
1685 		fbdev = fbi->par;
1686 		fbdev->fb_info = fbi;
1687 		fbdev->pd = pd;
1688 
1689 		fbdev->plane = plane;
1690 
1691 		/* Allocate the framebuffer to the maximum screen size */
1692 		fbdev->fb_len = (win->w[plane].xres * win->w[plane].yres * bpp) / 8;
1693 
1694 		fbdev->fb_mem = dmam_alloc_attrs(&dev->dev,
1695 				PAGE_ALIGN(fbdev->fb_len),
1696 				&fbdev->fb_phys, GFP_KERNEL,
1697 				DMA_ATTR_NON_CONSISTENT);
1698 		if (!fbdev->fb_mem) {
1699 			print_err("fail to allocate framebuffer (size: %dK))",
1700 				  fbdev->fb_len / 1024);
1701 			ret = -ENOMEM;
1702 			goto failed;
1703 		}
1704 
1705 		/*
1706 		 * Set page reserved so that mmap will work. This is necessary
1707 		 * since we'll be remapping normal memory.
1708 		 */
1709 		for (page = (unsigned long)fbdev->fb_phys;
1710 		     page < PAGE_ALIGN((unsigned long)fbdev->fb_phys +
1711 			     fbdev->fb_len);
1712 		     page += PAGE_SIZE) {
1713 			SetPageReserved(pfn_to_page(page >> PAGE_SHIFT)); /* LCD DMA is NOT coherent on Au1200 */
1714 		}
1715 		print_dbg("Framebuffer memory map at %p", fbdev->fb_mem);
1716 		print_dbg("phys=0x%08x, size=%dK", fbdev->fb_phys, fbdev->fb_len / 1024);
1717 
1718 		/* Init FB data */
1719 		ret = au1200fb_init_fbinfo(fbdev);
1720 		if (ret < 0)
1721 			goto failed;
1722 
1723 		/* Register new framebuffer */
1724 		ret = register_framebuffer(fbi);
1725 		if (ret < 0) {
1726 			print_err("cannot register new framebuffer");
1727 			goto failed;
1728 		}
1729 
1730 		au1200fb_fb_set_par(fbi);
1731 
1732 #if !defined(CONFIG_FRAMEBUFFER_CONSOLE) && defined(CONFIG_LOGO)
1733 		if (plane == 0)
1734 			if (fb_prepare_logo(fbi, FB_ROTATE_UR)) {
1735 				/* Start display and show logo on boot */
1736 				fb_set_cmap(&fbi->cmap, fbi);
1737 				fb_show_logo(fbi, FB_ROTATE_UR);
1738 			}
1739 #endif
1740 	}
1741 
1742 	/* Now hook interrupt too */
1743 	irq = platform_get_irq(dev, 0);
1744 	ret = request_irq(irq, au1200fb_handle_irq,
1745 			  IRQF_SHARED, "lcd", (void *)dev);
1746 	if (ret) {
1747 		print_err("fail to request interrupt line %d (err: %d)",
1748 			  irq, ret);
1749 		goto failed;
1750 	}
1751 
1752 	platform_set_drvdata(dev, pd);
1753 
1754 	/* Kickstart the panel */
1755 	au1200_setpanel(panel, pd);
1756 
1757 	return 0;
1758 
1759 failed:
1760 	for (plane = 0; plane < device_count; ++plane) {
1761 		fbi = _au1200fb_infos[plane];
1762 		if (!fbi)
1763 			break;
1764 
1765 		/* Clean up all probe data */
1766 		unregister_framebuffer(fbi);
1767 		if (fbi->cmap.len != 0)
1768 			fb_dealloc_cmap(&fbi->cmap);
1769 		kfree(fbi->pseudo_palette);
1770 
1771 		framebuffer_release(fbi);
1772 		_au1200fb_infos[plane] = NULL;
1773 	}
1774 	return ret;
1775 }
1776 
1777 static int au1200fb_drv_remove(struct platform_device *dev)
1778 {
1779 	struct au1200fb_platdata *pd = platform_get_drvdata(dev);
1780 	struct fb_info *fbi;
1781 	int plane;
1782 
1783 	/* Turn off the panel */
1784 	au1200_setpanel(NULL, pd);
1785 
1786 	for (plane = 0; plane < device_count; ++plane)	{
1787 		fbi = _au1200fb_infos[plane];
1788 
1789 		/* Clean up all probe data */
1790 		unregister_framebuffer(fbi);
1791 		if (fbi->cmap.len != 0)
1792 			fb_dealloc_cmap(&fbi->cmap);
1793 		kfree(fbi->pseudo_palette);
1794 
1795 		framebuffer_release(fbi);
1796 		_au1200fb_infos[plane] = NULL;
1797 	}
1798 
1799 	free_irq(platform_get_irq(dev, 0), (void *)dev);
1800 
1801 	return 0;
1802 }
1803 
1804 #ifdef CONFIG_PM
1805 static int au1200fb_drv_suspend(struct device *dev)
1806 {
1807 	struct au1200fb_platdata *pd = dev_get_drvdata(dev);
1808 	au1200_setpanel(NULL, pd);
1809 
1810 	lcd->outmask = 0;
1811 	wmb(); /* drain writebuffer */
1812 
1813 	return 0;
1814 }
1815 
1816 static int au1200fb_drv_resume(struct device *dev)
1817 {
1818 	struct au1200fb_platdata *pd = dev_get_drvdata(dev);
1819 	struct fb_info *fbi;
1820 	int i;
1821 
1822 	/* Kickstart the panel */
1823 	au1200_setpanel(panel, pd);
1824 
1825 	for (i = 0; i < device_count; i++) {
1826 		fbi = _au1200fb_infos[i];
1827 		au1200fb_fb_set_par(fbi);
1828 	}
1829 
1830 	return 0;
1831 }
1832 
1833 static const struct dev_pm_ops au1200fb_pmops = {
1834 	.suspend	= au1200fb_drv_suspend,
1835 	.resume		= au1200fb_drv_resume,
1836 	.freeze		= au1200fb_drv_suspend,
1837 	.thaw		= au1200fb_drv_resume,
1838 };
1839 
1840 #define AU1200FB_PMOPS	(&au1200fb_pmops)
1841 
1842 #else
1843 #define AU1200FB_PMOPS	NULL
1844 #endif /* CONFIG_PM */
1845 
1846 static struct platform_driver au1200fb_driver = {
1847 	.driver = {
1848 		.name	= "au1200-lcd",
1849 		.pm	= AU1200FB_PMOPS,
1850 	},
1851 	.probe		= au1200fb_drv_probe,
1852 	.remove		= au1200fb_drv_remove,
1853 };
1854 module_platform_driver(au1200fb_driver);
1855 
1856 MODULE_DESCRIPTION(DRIVER_DESC);
1857 MODULE_LICENSE("GPL");
1858