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