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