intelfb: add p divisor increments for i9xx.
[linux-2.6] / 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
45 #include <asm/mach-au1x00/au1000.h>
46 #include "au1200fb.h"
47
48 #ifdef CONFIG_PM
49 #include <asm/mach-au1x00/au1xxx_pm.h>
50 #endif
51
52 #ifndef CONFIG_FB_AU1200_DEVS
53 #define CONFIG_FB_AU1200_DEVS 4
54 #endif
55
56 #define DRIVER_NAME "au1200fb"
57 #define DRIVER_DESC "LCD controller driver for AU1200 processors"
58
59 #define DEBUG 1
60
61 #define print_err(f, arg...) printk(KERN_ERR DRIVER_NAME ": " f "\n", ## arg)
62 #define print_warn(f, arg...) printk(KERN_WARNING DRIVER_NAME ": " f "\n", ## arg)
63 #define print_info(f, arg...) printk(KERN_INFO DRIVER_NAME ": " f "\n", ## arg)
64
65 #if DEBUG
66 #define print_dbg(f, arg...) printk(KERN_DEBUG __FILE__ ": " f "\n", ## arg)
67 #else
68 #define print_dbg(f, arg...) do {} while (0)
69 #endif
70
71
72 #define AU1200_LCD_FB_IOCTL 0x46FF
73
74 #define AU1200_LCD_SET_SCREEN 1
75 #define AU1200_LCD_GET_SCREEN 2
76 #define AU1200_LCD_SET_WINDOW 3
77 #define AU1200_LCD_GET_WINDOW 4
78 #define AU1200_LCD_SET_PANEL  5
79 #define AU1200_LCD_GET_PANEL  6
80
81 #define SCREEN_SIZE                 (1<< 1)
82 #define SCREEN_BACKCOLOR    (1<< 2)
83 #define SCREEN_BRIGHTNESS   (1<< 3)
84 #define SCREEN_COLORKEY     (1<< 4)
85 #define SCREEN_MASK         (1<< 5)
86
87 struct au1200_lcd_global_regs_t {
88         unsigned int flags;
89         unsigned int xsize;
90         unsigned int ysize;
91         unsigned int backcolor;
92         unsigned int brightness;
93         unsigned int colorkey;
94         unsigned int mask;
95         unsigned int panel_choice;
96         char panel_desc[80];
97
98 };
99
100 #define WIN_POSITION            (1<< 0)
101 #define WIN_ALPHA_COLOR         (1<< 1)
102 #define WIN_ALPHA_MODE          (1<< 2)
103 #define WIN_PRIORITY            (1<< 3)
104 #define WIN_CHANNEL             (1<< 4)
105 #define WIN_BUFFER_FORMAT       (1<< 5)
106 #define WIN_COLOR_ORDER         (1<< 6)
107 #define WIN_PIXEL_ORDER         (1<< 7)
108 #define WIN_SIZE                (1<< 8)
109 #define WIN_COLORKEY_MODE       (1<< 9)
110 #define WIN_DOUBLE_BUFFER_MODE  (1<< 10)
111 #define WIN_RAM_ARRAY_MODE      (1<< 11)
112 #define WIN_BUFFER_SCALE        (1<< 12)
113 #define WIN_ENABLE                  (1<< 13)
114
115 struct au1200_lcd_window_regs_t {
116         unsigned int flags;
117         unsigned int xpos;
118         unsigned int ypos;
119         unsigned int alpha_color;
120         unsigned int alpha_mode;
121         unsigned int priority;
122         unsigned int channel;
123         unsigned int buffer_format;
124         unsigned int color_order;
125         unsigned int pixel_order;
126         unsigned int xsize;
127         unsigned int ysize;
128         unsigned int colorkey_mode;
129         unsigned int double_buffer_mode;
130         unsigned int ram_array_mode;
131         unsigned int xscale;
132         unsigned int yscale;
133         unsigned int enable;
134 };
135
136
137 struct au1200_lcd_iodata_t {
138         unsigned int subcmd;
139         struct au1200_lcd_global_regs_t global;
140         struct au1200_lcd_window_regs_t window;
141 };
142
143 #if defined(__BIG_ENDIAN)
144 #define LCD_CONTROL_DEFAULT_PO LCD_CONTROL_PO_11
145 #else
146 #define LCD_CONTROL_DEFAULT_PO LCD_CONTROL_PO_00
147 #endif
148 #define LCD_CONTROL_DEFAULT_SBPPF LCD_CONTROL_SBPPF_565
149
150 /* Private, per-framebuffer management information (independent of the panel itself) */
151 struct au1200fb_device {
152         struct fb_info fb_info;                 /* FB driver info record */
153
154         int                                     plane;
155         unsigned char*          fb_mem;         /* FrameBuffer memory map */
156         unsigned int            fb_len;
157         dma_addr_t              fb_phys;
158 };
159
160 static struct au1200fb_device _au1200fb_devices[CONFIG_FB_AU1200_DEVS];
161 /********************************************************************/
162
163 /* LCD controller restrictions */
164 #define AU1200_LCD_MAX_XRES     1280
165 #define AU1200_LCD_MAX_YRES     1024
166 #define AU1200_LCD_MAX_BPP      32
167 #define AU1200_LCD_MAX_CLK      96000000 /* fixme: this needs to go away ? */
168 #define AU1200_LCD_NBR_PALETTE_ENTRIES 256
169
170 /* Default number of visible screen buffer to allocate */
171 #define AU1200FB_NBR_VIDEO_BUFFERS 1
172
173 /********************************************************************/
174
175 static struct au1200_lcd *lcd = (struct au1200_lcd *) AU1200_LCD_ADDR;
176 static int window_index = 2; /* default is zero */
177 static int panel_index = 2; /* default is zero */
178 static struct window_settings *win;
179 static struct panel_settings *panel;
180 static int noblanking = 1;
181 static int nohwcursor = 0;
182
183 struct window_settings {
184         unsigned char name[64];
185         uint32 mode_backcolor;
186         uint32 mode_colorkey;
187         uint32 mode_colorkeymsk;
188         struct {
189                 int xres;
190                 int yres;
191                 int xpos;
192                 int ypos;
193                 uint32 mode_winctrl1; /* winctrl1[FRM,CCO,PO,PIPE] */
194                 uint32 mode_winenable;
195         } w[4];
196 };
197
198 #if defined(__BIG_ENDIAN)
199 #define LCD_WINCTRL1_PO_16BPP LCD_WINCTRL1_PO_00
200 #else
201 #define LCD_WINCTRL1_PO_16BPP LCD_WINCTRL1_PO_01
202 #endif
203
204 extern int board_au1200fb_panel_init (void);
205 extern int board_au1200fb_panel_shutdown (void);
206
207 #ifdef CONFIG_PM
208 int au1200fb_pm_callback(au1xxx_power_dev_t *dev,
209                 au1xxx_request_t request, void *data);
210 au1xxx_power_dev_t *LCD_pm_dev;
211 #endif
212
213 /*
214  * Default window configurations
215  */
216 static struct window_settings windows[] = {
217         { /* Index 0 */
218                 "0-FS gfx, 1-video, 2-ovly gfx, 3-ovly gfx",
219                 /* mode_backcolor       */ 0x006600ff,
220                 /* mode_colorkey,msk*/ 0, 0,
221                 {
222                         {
223                         /* xres, yres, xpos, ypos */ 0, 0, 0, 0,
224                         /* mode_winctrl1 */ LCD_WINCTRL1_FRM_16BPP565 |
225                                 LCD_WINCTRL1_PO_16BPP,
226                         /* mode_winenable*/ LCD_WINENABLE_WEN0,
227                         },
228                         {
229                         /* xres, yres, xpos, ypos */ 100, 100, 100, 100,
230                         /* mode_winctrl1 */ LCD_WINCTRL1_FRM_16BPP565 |
231                                 LCD_WINCTRL1_PO_16BPP |
232                                 LCD_WINCTRL1_PIPE,
233                         /* mode_winenable*/ LCD_WINENABLE_WEN1,
234                         },
235                         {
236                         /* xres, yres, xpos, ypos */ 0, 0, 0, 0,
237                         /* mode_winctrl1 */ LCD_WINCTRL1_FRM_16BPP565 |
238                                 LCD_WINCTRL1_PO_16BPP,
239                         /* mode_winenable*/ 0,
240                         },
241                         {
242                         /* xres, yres, xpos, ypos */ 0, 0, 0, 0,
243                         /* mode_winctrl1 */ LCD_WINCTRL1_FRM_16BPP565 |
244                                 LCD_WINCTRL1_PO_16BPP |
245                                 LCD_WINCTRL1_PIPE,
246                         /* mode_winenable*/ 0,
247                         },
248                 },
249         },
250
251         { /* Index 1 */
252                 "0-FS gfx, 1-video, 2-ovly gfx, 3-ovly gfx",
253                 /* mode_backcolor       */ 0x006600ff,
254                 /* mode_colorkey,msk*/ 0, 0,
255                 {
256                         {
257                         /* xres, yres, xpos, ypos */ 320, 240, 5, 5,
258                         /* mode_winctrl1 */ LCD_WINCTRL1_FRM_24BPP |
259                                 LCD_WINCTRL1_PO_00,
260                         /* mode_winenable*/ LCD_WINENABLE_WEN0,
261                         },
262                         {
263                         /* xres, yres, xpos, ypos */ 0, 0, 0, 0,
264                         /* mode_winctrl1 */ LCD_WINCTRL1_FRM_16BPP565
265                                 | LCD_WINCTRL1_PO_16BPP,
266                         /* mode_winenable*/ 0,
267                         },
268                         {
269                         /* xres, yres, xpos, ypos */ 100, 100, 0, 0,
270                         /* mode_winctrl1 */ LCD_WINCTRL1_FRM_16BPP565 |
271                                 LCD_WINCTRL1_PO_16BPP |
272                                 LCD_WINCTRL1_PIPE,
273                         /* mode_winenable*/ 0/*LCD_WINENABLE_WEN2*/,
274                         },
275                         {
276                         /* xres, yres, xpos, ypos */ 200, 25, 0, 0,
277                         /* mode_winctrl1 */ LCD_WINCTRL1_FRM_16BPP565 |
278                                 LCD_WINCTRL1_PO_16BPP |
279                                 LCD_WINCTRL1_PIPE,
280                         /* mode_winenable*/ 0,
281                         },
282                 },
283         },
284         { /* Index 2 */
285                 "0-FS gfx, 1-video, 2-ovly gfx, 3-ovly gfx",
286                 /* mode_backcolor       */ 0x006600ff,
287                 /* mode_colorkey,msk*/ 0, 0,
288                 {
289                         {
290                         /* xres, yres, xpos, ypos */ 0, 0, 0, 0,
291                         /* mode_winctrl1 */ LCD_WINCTRL1_FRM_16BPP565 |
292                                 LCD_WINCTRL1_PO_16BPP,
293                         /* mode_winenable*/ LCD_WINENABLE_WEN0,
294                         },
295                         {
296                         /* xres, yres, xpos, ypos */ 0, 0, 0, 0,
297                         /* mode_winctrl1 */ LCD_WINCTRL1_FRM_16BPP565 |
298                                 LCD_WINCTRL1_PO_16BPP,
299                         /* mode_winenable*/ 0,
300                         },
301                         {
302                         /* xres, yres, xpos, ypos */ 0, 0, 0, 0,
303                         /* mode_winctrl1 */ LCD_WINCTRL1_FRM_32BPP |
304                                 LCD_WINCTRL1_PO_00|LCD_WINCTRL1_PIPE,
305                         /* mode_winenable*/ 0/*LCD_WINENABLE_WEN2*/,
306                         },
307                         {
308                         /* xres, yres, xpos, ypos */ 0, 0, 0, 0,
309                         /* mode_winctrl1 */ LCD_WINCTRL1_FRM_16BPP565 |
310                                 LCD_WINCTRL1_PO_16BPP |
311                                 LCD_WINCTRL1_PIPE,
312                         /* mode_winenable*/ 0,
313                         },
314                 },
315         },
316         /* Need VGA 640 @ 24bpp, @ 32bpp */
317         /* Need VGA 800 @ 24bpp, @ 32bpp */
318         /* Need VGA 1024 @ 24bpp, @ 32bpp */
319 };
320
321 /*
322  * Controller configurations for various panels.
323  */
324
325 struct panel_settings
326 {
327         const char name[25];            /* Full name <vendor>_<model> */
328
329         struct  fb_monspecs monspecs;   /* FB monitor specs */
330
331         /* panel timings */
332         uint32 mode_screen;
333         uint32 mode_horztiming;
334         uint32 mode_verttiming;
335         uint32 mode_clkcontrol;
336         uint32 mode_pwmdiv;
337         uint32 mode_pwmhi;
338         uint32 mode_outmask;
339         uint32 mode_fifoctrl;
340         uint32 mode_toyclksrc;
341         uint32 mode_backlight;
342         uint32 mode_auxpll;
343         int (*device_init)(void);
344         int (*device_shutdown)(void);
345 #define Xres min_xres
346 #define Yres min_yres
347         u32     min_xres;               /* Minimum horizontal resolution */
348         u32     max_xres;               /* Maximum horizontal resolution */
349         u32     min_yres;               /* Minimum vertical resolution */
350         u32     max_yres;               /* Maximum vertical resolution */
351 };
352
353 /********************************************************************/
354 /* fixme: Maybe a modedb for the CRT ? otherwise panels should be as-is */
355
356 /* List of panels known to work with the AU1200 LCD controller.
357  * To add a new panel, enter the same specifications as the
358  * Generic_TFT one, and MAKE SURE that it doesn't conflicts
359  * with the controller restrictions. Restrictions are:
360  *
361  * STN color panels: max_bpp <= 12
362  * STN mono panels: max_bpp <= 4
363  * TFT panels: max_bpp <= 16
364  * max_xres <= 800
365  * max_yres <= 600
366  */
367 static struct panel_settings known_lcd_panels[] =
368 {
369         [0] = { /* QVGA 320x240 H:33.3kHz V:110Hz */
370                 .name = "QVGA_320x240",
371                 .monspecs = {
372                         .modedb = NULL,
373                         .modedb_len = 0,
374                         .hfmin = 30000,
375                         .hfmax = 70000,
376                         .vfmin = 60,
377                         .vfmax = 60,
378                         .dclkmin = 6000000,
379                         .dclkmax = 28000000,
380                         .input = FB_DISP_RGB,
381                 },
382                 .mode_screen            = LCD_SCREEN_SX_N(320) |
383                         LCD_SCREEN_SY_N(240),
384                 .mode_horztiming        = 0x00c4623b,
385                 .mode_verttiming        = 0x00502814,
386                 .mode_clkcontrol        = 0x00020002, /* /4=24Mhz */
387                 .mode_pwmdiv            = 0x00000000,
388                 .mode_pwmhi             = 0x00000000,
389                 .mode_outmask   = 0x00FFFFFF,
390                 .mode_fifoctrl  = 0x2f2f2f2f,
391                 .mode_toyclksrc = 0x00000004, /* AUXPLL directly */
392                 .mode_backlight = 0x00000000,
393                 .mode_auxpll            = 8, /* 96MHz AUXPLL */
394                 .device_init            = NULL,
395                 .device_shutdown        = NULL,
396                 320, 320,
397                 240, 240,
398         },
399
400         [1] = { /* VGA 640x480 H:30.3kHz V:58Hz */
401                 .name = "VGA_640x480",
402                 .monspecs = {
403                         .modedb = NULL,
404                         .modedb_len = 0,
405                         .hfmin = 30000,
406                         .hfmax = 70000,
407                         .vfmin = 60,
408                         .vfmax = 60,
409                         .dclkmin = 6000000,
410                         .dclkmax = 28000000,
411                         .input = FB_DISP_RGB,
412                 },
413                 .mode_screen            = 0x13f9df80,
414                 .mode_horztiming        = 0x003c5859,
415                 .mode_verttiming        = 0x00741201,
416                 .mode_clkcontrol        = 0x00020001, /* /4=24Mhz */
417                 .mode_pwmdiv            = 0x00000000,
418                 .mode_pwmhi             = 0x00000000,
419                 .mode_outmask   = 0x00FFFFFF,
420                 .mode_fifoctrl  = 0x2f2f2f2f,
421                 .mode_toyclksrc = 0x00000004, /* AUXPLL directly */
422                 .mode_backlight = 0x00000000,
423                 .mode_auxpll            = 8, /* 96MHz AUXPLL */
424                 .device_init            = NULL,
425                 .device_shutdown        = NULL,
426                 640, 480,
427                 640, 480,
428         },
429
430         [2] = { /* SVGA 800x600 H:46.1kHz V:69Hz */
431                 .name = "SVGA_800x600",
432                 .monspecs = {
433                         .modedb = NULL,
434                         .modedb_len = 0,
435                         .hfmin = 30000,
436                         .hfmax = 70000,
437                         .vfmin = 60,
438                         .vfmax = 60,
439                         .dclkmin = 6000000,
440                         .dclkmax = 28000000,
441                         .input = FB_DISP_RGB,
442                 },
443                 .mode_screen            = 0x18fa5780,
444                 .mode_horztiming        = 0x00dc7e77,
445                 .mode_verttiming        = 0x00584805,
446                 .mode_clkcontrol        = 0x00020000, /* /2=48Mhz */
447                 .mode_pwmdiv            = 0x00000000,
448                 .mode_pwmhi             = 0x00000000,
449                 .mode_outmask   = 0x00FFFFFF,
450                 .mode_fifoctrl  = 0x2f2f2f2f,
451                 .mode_toyclksrc = 0x00000004, /* AUXPLL directly */
452                 .mode_backlight = 0x00000000,
453                 .mode_auxpll            = 8, /* 96MHz AUXPLL */
454                 .device_init            = NULL,
455                 .device_shutdown        = NULL,
456                 800, 800,
457                 600, 600,
458         },
459
460         [3] = { /* XVGA 1024x768 H:56.2kHz V:70Hz */
461                 .name = "XVGA_1024x768",
462                 .monspecs = {
463                         .modedb = NULL,
464                         .modedb_len = 0,
465                         .hfmin = 30000,
466                         .hfmax = 70000,
467                         .vfmin = 60,
468                         .vfmax = 60,
469                         .dclkmin = 6000000,
470                         .dclkmax = 28000000,
471                         .input = FB_DISP_RGB,
472                 },
473                 .mode_screen            = 0x1ffaff80,
474                 .mode_horztiming        = 0x007d0e57,
475                 .mode_verttiming        = 0x00740a01,
476                 .mode_clkcontrol        = 0x000A0000, /* /1 */
477                 .mode_pwmdiv            = 0x00000000,
478                 .mode_pwmhi             = 0x00000000,
479                 .mode_outmask   = 0x00FFFFFF,
480                 .mode_fifoctrl  = 0x2f2f2f2f,
481                 .mode_toyclksrc = 0x00000004, /* AUXPLL directly */
482                 .mode_backlight = 0x00000000,
483                 .mode_auxpll            = 6, /* 72MHz AUXPLL */
484                 .device_init            = NULL,
485                 .device_shutdown        = NULL,
486                 1024, 1024,
487                 768, 768,
488         },
489
490         [4] = { /* XVGA XVGA 1280x1024 H:68.5kHz V:65Hz */
491                 .name = "XVGA_1280x1024",
492                 .monspecs = {
493                         .modedb = NULL,
494                         .modedb_len = 0,
495                         .hfmin = 30000,
496                         .hfmax = 70000,
497                         .vfmin = 60,
498                         .vfmax = 60,
499                         .dclkmin = 6000000,
500                         .dclkmax = 28000000,
501                         .input = FB_DISP_RGB,
502                 },
503                 .mode_screen            = 0x27fbff80,
504                 .mode_horztiming        = 0x00cdb2c7,
505                 .mode_verttiming        = 0x00600002,
506                 .mode_clkcontrol        = 0x000A0000, /* /1 */
507                 .mode_pwmdiv            = 0x00000000,
508                 .mode_pwmhi             = 0x00000000,
509                 .mode_outmask   = 0x00FFFFFF,
510                 .mode_fifoctrl  = 0x2f2f2f2f,
511                 .mode_toyclksrc = 0x00000004, /* AUXPLL directly */
512                 .mode_backlight = 0x00000000,
513                 .mode_auxpll            = 10, /* 120MHz AUXPLL */
514                 .device_init            = NULL,
515                 .device_shutdown        = NULL,
516                 1280, 1280,
517                 1024, 1024,
518         },
519
520         [5] = { /* Samsung 1024x768 TFT */
521                 .name = "Samsung_1024x768_TFT",
522                 .monspecs = {
523                         .modedb = NULL,
524                         .modedb_len = 0,
525                         .hfmin = 30000,
526                         .hfmax = 70000,
527                         .vfmin = 60,
528                         .vfmax = 60,
529                         .dclkmin = 6000000,
530                         .dclkmax = 28000000,
531                         .input = FB_DISP_RGB,
532                 },
533                 .mode_screen            = 0x1ffaff80,
534                 .mode_horztiming        = 0x018cc677,
535                 .mode_verttiming        = 0x00241217,
536                 .mode_clkcontrol        = 0x00000000, /* SCB 0x1 /4=24Mhz */
537                 .mode_pwmdiv            = 0x8000063f, /* SCB 0x0 */
538                 .mode_pwmhi             = 0x03400000, /* SCB 0x0 */
539                 .mode_outmask   = 0x00FFFFFF,
540                 .mode_fifoctrl  = 0x2f2f2f2f,
541                 .mode_toyclksrc = 0x00000004, /* AUXPLL directly */
542                 .mode_backlight = 0x00000000,
543                 .mode_auxpll            = 8, /* 96MHz AUXPLL */
544                 .device_init            = board_au1200fb_panel_init,
545                 .device_shutdown        = board_au1200fb_panel_shutdown,
546                 1024, 1024,
547                 768, 768,
548         },
549
550         [6] = { /* Toshiba 640x480 TFT */
551                 .name = "Toshiba_640x480_TFT",
552                 .monspecs = {
553                         .modedb = NULL,
554                         .modedb_len = 0,
555                         .hfmin = 30000,
556                         .hfmax = 70000,
557                         .vfmin = 60,
558                         .vfmax = 60,
559                         .dclkmin = 6000000,
560                         .dclkmax = 28000000,
561                         .input = FB_DISP_RGB,
562                 },
563                 .mode_screen            = LCD_SCREEN_SX_N(640) |
564                         LCD_SCREEN_SY_N(480),
565                 .mode_horztiming        = LCD_HORZTIMING_HPW_N(96) |
566                         LCD_HORZTIMING_HND1_N(13) | LCD_HORZTIMING_HND2_N(51),
567                 .mode_verttiming        = LCD_VERTTIMING_VPW_N(2) |
568                         LCD_VERTTIMING_VND1_N(11) | LCD_VERTTIMING_VND2_N(32),
569                 .mode_clkcontrol        = 0x00000000, /* /4=24Mhz */
570                 .mode_pwmdiv            = 0x8000063f,
571                 .mode_pwmhi             = 0x03400000,
572                 .mode_outmask   = 0x00fcfcfc,
573                 .mode_fifoctrl  = 0x2f2f2f2f,
574                 .mode_toyclksrc = 0x00000004, /* AUXPLL directly */
575                 .mode_backlight = 0x00000000,
576                 .mode_auxpll            = 8, /* 96MHz AUXPLL */
577                 .device_init            = board_au1200fb_panel_init,
578                 .device_shutdown        = board_au1200fb_panel_shutdown,
579                 640, 480,
580                 640, 480,
581         },
582
583         [7] = { /* Sharp 320x240 TFT */
584                 .name = "Sharp_320x240_TFT",
585                 .monspecs = {
586                         .modedb = NULL,
587                         .modedb_len = 0,
588                         .hfmin = 12500,
589                         .hfmax = 20000,
590                         .vfmin = 38,
591                         .vfmax = 81,
592                         .dclkmin = 4500000,
593                         .dclkmax = 6800000,
594                         .input = FB_DISP_RGB,
595                 },
596                 .mode_screen            = LCD_SCREEN_SX_N(320) |
597                         LCD_SCREEN_SY_N(240),
598                 .mode_horztiming        = LCD_HORZTIMING_HPW_N(60) |
599                         LCD_HORZTIMING_HND1_N(13) | LCD_HORZTIMING_HND2_N(2),
600                 .mode_verttiming        = LCD_VERTTIMING_VPW_N(2) |
601                         LCD_VERTTIMING_VND1_N(2) | LCD_VERTTIMING_VND2_N(5),
602                 .mode_clkcontrol        = LCD_CLKCONTROL_PCD_N(7), /*16=6Mhz*/
603                 .mode_pwmdiv            = 0x8000063f,
604                 .mode_pwmhi             = 0x03400000,
605                 .mode_outmask   = 0x00fcfcfc,
606                 .mode_fifoctrl  = 0x2f2f2f2f,
607                 .mode_toyclksrc = 0x00000004, /* AUXPLL directly */
608                 .mode_backlight = 0x00000000,
609                 .mode_auxpll            = 8, /* 96MHz AUXPLL */
610                 .device_init            = board_au1200fb_panel_init,
611                 .device_shutdown        = board_au1200fb_panel_shutdown,
612                 320, 320,
613                 240, 240,
614         },
615
616         [8] = { /* Toppoly TD070WGCB2 7" 856x480 TFT */
617                 .name = "Toppoly_TD070WGCB2",
618                 .monspecs = {
619                         .modedb = NULL,
620                         .modedb_len = 0,
621                         .hfmin = 30000,
622                         .hfmax = 70000,
623                         .vfmin = 60,
624                         .vfmax = 60,
625                         .dclkmin = 6000000,
626                         .dclkmax = 28000000,
627                         .input = FB_DISP_RGB,
628                 },
629                 .mode_screen            = LCD_SCREEN_SX_N(856) |
630                         LCD_SCREEN_SY_N(480),
631                 .mode_horztiming        = LCD_HORZTIMING_HND2_N(43) |
632                         LCD_HORZTIMING_HND1_N(43) | LCD_HORZTIMING_HPW_N(114),
633                 .mode_verttiming        = LCD_VERTTIMING_VND2_N(20) |
634                         LCD_VERTTIMING_VND1_N(21) | LCD_VERTTIMING_VPW_N(4),
635                 .mode_clkcontrol        = 0x00020001, /* /4=24Mhz */
636                 .mode_pwmdiv            = 0x8000063f,
637                 .mode_pwmhi             = 0x03400000,
638                 .mode_outmask   = 0x00fcfcfc,
639                 .mode_fifoctrl  = 0x2f2f2f2f,
640                 .mode_toyclksrc = 0x00000004, /* AUXPLL directly */
641                 .mode_backlight = 0x00000000,
642                 .mode_auxpll            = 8, /* 96MHz AUXPLL */
643                 .device_init            = board_au1200fb_panel_init,
644                 .device_shutdown        = board_au1200fb_panel_shutdown,
645                 856, 856,
646                 480, 480,
647         },
648 };
649
650 #define NUM_PANELS (ARRAY_SIZE(known_lcd_panels))
651
652 /********************************************************************/
653
654 #ifdef CONFIG_PM
655 static int set_brightness(unsigned int brightness)
656 {
657         unsigned int hi1, divider;
658
659         /* limit brightness pwm duty to >= 30/1600 */
660         if (brightness < 30) {
661                 brightness = 30;
662         }
663         divider = (lcd->pwmdiv & 0x3FFFF) + 1;
664         hi1 = (lcd->pwmhi >> 16) + 1;
665         hi1 = (((brightness & 0xFF) + 1) * divider >> 8);
666         lcd->pwmhi &= 0xFFFF;
667         lcd->pwmhi |= (hi1 << 16);
668
669         return brightness;
670 }
671 #endif /* CONFIG_PM */
672
673 static int winbpp (unsigned int winctrl1)
674 {
675         int bits = 0;
676
677         /* how many bits are needed for each pixel format */
678         switch (winctrl1 & LCD_WINCTRL1_FRM) {
679         case LCD_WINCTRL1_FRM_1BPP:
680                 bits = 1;
681                 break;
682         case LCD_WINCTRL1_FRM_2BPP:
683                 bits = 2;
684                 break;
685         case LCD_WINCTRL1_FRM_4BPP:
686                 bits = 4;
687                 break;
688         case LCD_WINCTRL1_FRM_8BPP:
689                 bits = 8;
690                 break;
691         case LCD_WINCTRL1_FRM_12BPP:
692         case LCD_WINCTRL1_FRM_16BPP655:
693         case LCD_WINCTRL1_FRM_16BPP565:
694         case LCD_WINCTRL1_FRM_16BPP556:
695         case LCD_WINCTRL1_FRM_16BPPI1555:
696         case LCD_WINCTRL1_FRM_16BPPI5551:
697         case LCD_WINCTRL1_FRM_16BPPA1555:
698         case LCD_WINCTRL1_FRM_16BPPA5551:
699                 bits = 16;
700                 break;
701         case LCD_WINCTRL1_FRM_24BPP:
702         case LCD_WINCTRL1_FRM_32BPP:
703                 bits = 32;
704                 break;
705         }
706
707         return bits;
708 }
709
710 static int fbinfo2index (struct fb_info *fb_info)
711 {
712         int i;
713
714         for (i = 0; i < CONFIG_FB_AU1200_DEVS; ++i) {
715                 if (fb_info == (struct fb_info *)(&_au1200fb_devices[i].fb_info))
716                         return i;
717         }
718         printk("au1200fb: ERROR: fbinfo2index failed!\n");
719         return -1;
720 }
721
722 static int au1200_setlocation (struct au1200fb_device *fbdev, int plane,
723         int xpos, int ypos)
724 {
725         uint32 winctrl0, winctrl1, winenable, fb_offset = 0;
726         int xsz, ysz;
727
728         /* FIX!!! NOT CHECKING FOR COMPLETE OFFSCREEN YET */
729
730         winctrl0 = lcd->window[plane].winctrl0;
731         winctrl1 = lcd->window[plane].winctrl1;
732         winctrl0 &= (LCD_WINCTRL0_A | LCD_WINCTRL0_AEN);
733         winctrl1 &= ~(LCD_WINCTRL1_SZX | LCD_WINCTRL1_SZY);
734
735         /* Check for off-screen adjustments */
736         xsz = win->w[plane].xres;
737         ysz = win->w[plane].yres;
738         if ((xpos + win->w[plane].xres) > panel->Xres) {
739                 /* Off-screen to the right */
740                 xsz = panel->Xres - xpos; /* off by 1 ??? */
741                 /*printk("off screen right\n");*/
742         }
743
744         if ((ypos + win->w[plane].yres) > panel->Yres) {
745                 /* Off-screen to the bottom */
746                 ysz = panel->Yres - ypos; /* off by 1 ??? */
747                 /*printk("off screen bottom\n");*/
748         }
749
750         if (xpos < 0) {
751                 /* Off-screen to the left */
752                 xsz = win->w[plane].xres + xpos;
753                 fb_offset += (((0 - xpos) * winbpp(lcd->window[plane].winctrl1))/8);
754                 xpos = 0;
755                 /*printk("off screen left\n");*/
756         }
757
758         if (ypos < 0) {
759                 /* Off-screen to the top */
760                 ysz = win->w[plane].yres + ypos;
761                 /* fixme: fb_offset += ((0-ypos)*fb_pars[plane].line_length); */
762                 ypos = 0;
763                 /*printk("off screen top\n");*/
764         }
765
766         /* record settings */
767         win->w[plane].xpos = xpos;
768         win->w[plane].ypos = ypos;
769
770         xsz -= 1;
771         ysz -= 1;
772         winctrl0 |= (xpos << 21);
773         winctrl0 |= (ypos << 10);
774         winctrl1 |= (xsz << 11);
775         winctrl1 |= (ysz << 0);
776
777         /* Disable the window while making changes, then restore WINEN */
778         winenable = lcd->winenable & (1 << plane);
779         au_sync();
780         lcd->winenable &= ~(1 << plane);
781         lcd->window[plane].winctrl0 = winctrl0;
782         lcd->window[plane].winctrl1 = winctrl1;
783         lcd->window[plane].winbuf0 =
784         lcd->window[plane].winbuf1 = fbdev->fb_phys;
785         lcd->window[plane].winbufctrl = 0; /* select winbuf0 */
786         lcd->winenable |= winenable;
787         au_sync();
788
789         return 0;
790 }
791
792 static void au1200_setpanel (struct panel_settings *newpanel)
793 {
794         /*
795          * Perform global setup/init of LCD controller
796          */
797         uint32 winenable;
798
799         /* Make sure all windows disabled */
800         winenable = lcd->winenable;
801         lcd->winenable = 0;
802         au_sync();
803         /*
804          * Ensure everything is disabled before reconfiguring
805          */
806         if (lcd->screen & LCD_SCREEN_SEN) {
807                 /* Wait for vertical sync period */
808                 lcd->intstatus = LCD_INT_SS;
809                 while ((lcd->intstatus & LCD_INT_SS) == 0) {
810                         au_sync();
811                 }
812
813                 lcd->screen &= ~LCD_SCREEN_SEN; /*disable the controller*/
814
815                 do {
816                         lcd->intstatus = lcd->intstatus; /*clear interrupts*/
817                         au_sync();
818                 /*wait for controller to shut down*/
819                 } while ((lcd->intstatus & LCD_INT_SD) == 0);
820
821                 /* Call shutdown of current panel (if up) */
822                 /* this must occur last, because if an external clock is driving
823                     the controller, the clock cannot be turned off before first
824                         shutting down the controller.
825                  */
826                 if (panel->device_shutdown != NULL)
827                         panel->device_shutdown();
828         }
829
830         /* Newpanel == NULL indicates a shutdown operation only */
831         if (newpanel == NULL)
832                 return;
833
834         panel = newpanel;
835
836         printk("Panel(%s), %dx%d\n", panel->name, panel->Xres, panel->Yres);
837
838         /*
839          * Setup clocking if internal LCD clock source (assumes sys_auxpll valid)
840          */
841         if (!(panel->mode_clkcontrol & LCD_CLKCONTROL_EXT))
842         {
843                 uint32 sys_clksrc;
844                 au_writel(panel->mode_auxpll, SYS_AUXPLL);
845                 sys_clksrc = au_readl(SYS_CLKSRC) & ~0x0000001f;
846                 sys_clksrc |= panel->mode_toyclksrc;
847                 au_writel(sys_clksrc, SYS_CLKSRC);
848         }
849
850         /*
851          * Configure panel timings
852          */
853         lcd->screen = panel->mode_screen;
854         lcd->horztiming = panel->mode_horztiming;
855         lcd->verttiming = panel->mode_verttiming;
856         lcd->clkcontrol = panel->mode_clkcontrol;
857         lcd->pwmdiv = panel->mode_pwmdiv;
858         lcd->pwmhi = panel->mode_pwmhi;
859         lcd->outmask = panel->mode_outmask;
860         lcd->fifoctrl = panel->mode_fifoctrl;
861         au_sync();
862
863         /* fixme: Check window settings to make sure still valid
864          * for new geometry */
865 #if 0
866         au1200_setlocation(fbdev, 0, win->w[0].xpos, win->w[0].ypos);
867         au1200_setlocation(fbdev, 1, win->w[1].xpos, win->w[1].ypos);
868         au1200_setlocation(fbdev, 2, win->w[2].xpos, win->w[2].ypos);
869         au1200_setlocation(fbdev, 3, win->w[3].xpos, win->w[3].ypos);
870 #endif
871         lcd->winenable = winenable;
872
873         /*
874          * Re-enable screen now that it is configured
875          */
876         lcd->screen |= LCD_SCREEN_SEN;
877         au_sync();
878
879         /* Call init of panel */
880         if (panel->device_init != NULL) panel->device_init();
881
882         /* FIX!!!! not appropriate on panel change!!! Global setup/init */
883         lcd->intenable = 0;
884         lcd->intstatus = ~0;
885         lcd->backcolor = win->mode_backcolor;
886
887         /* Setup Color Key - FIX!!! */
888         lcd->colorkey = win->mode_colorkey;
889         lcd->colorkeymsk = win->mode_colorkeymsk;
890
891         /* Setup HWCursor - FIX!!! Need to support this eventually */
892         lcd->hwc.cursorctrl = 0;
893         lcd->hwc.cursorpos = 0;
894         lcd->hwc.cursorcolor0 = 0;
895         lcd->hwc.cursorcolor1 = 0;
896         lcd->hwc.cursorcolor2 = 0;
897         lcd->hwc.cursorcolor3 = 0;
898
899
900 #if 0
901 #define D(X) printk("%25s: %08X\n", #X, X)
902         D(lcd->screen);
903         D(lcd->horztiming);
904         D(lcd->verttiming);
905         D(lcd->clkcontrol);
906         D(lcd->pwmdiv);
907         D(lcd->pwmhi);
908         D(lcd->outmask);
909         D(lcd->fifoctrl);
910         D(lcd->window[0].winctrl0);
911         D(lcd->window[0].winctrl1);
912         D(lcd->window[0].winctrl2);
913         D(lcd->window[0].winbuf0);
914         D(lcd->window[0].winbuf1);
915         D(lcd->window[0].winbufctrl);
916         D(lcd->window[1].winctrl0);
917         D(lcd->window[1].winctrl1);
918         D(lcd->window[1].winctrl2);
919         D(lcd->window[1].winbuf0);
920         D(lcd->window[1].winbuf1);
921         D(lcd->window[1].winbufctrl);
922         D(lcd->window[2].winctrl0);
923         D(lcd->window[2].winctrl1);
924         D(lcd->window[2].winctrl2);
925         D(lcd->window[2].winbuf0);
926         D(lcd->window[2].winbuf1);
927         D(lcd->window[2].winbufctrl);
928         D(lcd->window[3].winctrl0);
929         D(lcd->window[3].winctrl1);
930         D(lcd->window[3].winctrl2);
931         D(lcd->window[3].winbuf0);
932         D(lcd->window[3].winbuf1);
933         D(lcd->window[3].winbufctrl);
934         D(lcd->winenable);
935         D(lcd->intenable);
936         D(lcd->intstatus);
937         D(lcd->backcolor);
938         D(lcd->winenable);
939         D(lcd->colorkey);
940     D(lcd->colorkeymsk);
941         D(lcd->hwc.cursorctrl);
942         D(lcd->hwc.cursorpos);
943         D(lcd->hwc.cursorcolor0);
944         D(lcd->hwc.cursorcolor1);
945         D(lcd->hwc.cursorcolor2);
946         D(lcd->hwc.cursorcolor3);
947 #endif
948 }
949
950 static void au1200_setmode(struct au1200fb_device *fbdev)
951 {
952         int plane = fbdev->plane;
953         /* Window/plane setup */
954         lcd->window[plane].winctrl1 = ( 0
955                 | LCD_WINCTRL1_PRI_N(plane)
956                 | win->w[plane].mode_winctrl1 /* FRM,CCO,PO,PIPE */
957                 ) ;
958
959         au1200_setlocation(fbdev, plane, win->w[plane].xpos, win->w[plane].ypos);
960
961         lcd->window[plane].winctrl2 = ( 0
962                 | LCD_WINCTRL2_CKMODE_00
963                 | LCD_WINCTRL2_DBM
964                 | LCD_WINCTRL2_BX_N( fbdev->fb_info.fix.line_length)
965                 | LCD_WINCTRL2_SCX_1
966                 | LCD_WINCTRL2_SCY_1
967                 ) ;
968         lcd->winenable |= win->w[plane].mode_winenable;
969         au_sync();
970 }
971
972
973 /* Inline helpers */
974
975 /*#define panel_is_dual(panel)  ((panel->mode_screen & LCD_SCREEN_PT) == LCD_SCREEN_PT_010)*/
976 /*#define panel_is_active(panel)((panel->mode_screen & LCD_SCREEN_PT) == LCD_SCREEN_PT_010)*/
977
978 #define panel_is_color(panel) ((panel->mode_screen & LCD_SCREEN_PT) <= LCD_SCREEN_PT_CDSTN)
979
980 /* Bitfields format supported by the controller. */
981 static struct fb_bitfield rgb_bitfields[][4] = {
982         /*     Red,        Green,        Blue,       Transp   */
983         [LCD_WINCTRL1_FRM_16BPP655 >> 25] =
984                 { { 10, 6, 0 }, { 5, 5, 0 }, { 0, 5, 0 }, { 0, 0, 0 } },
985
986         [LCD_WINCTRL1_FRM_16BPP565 >> 25] =
987                 { { 11, 5, 0 }, { 5, 6, 0 }, { 0, 5, 0 }, { 0, 0, 0 } },
988
989         [LCD_WINCTRL1_FRM_16BPP556 >> 25] =
990                 { { 11, 5, 0 }, { 6, 5, 0 }, { 0, 6, 0 }, { 0, 0, 0 } },
991
992         [LCD_WINCTRL1_FRM_16BPPI1555 >> 25] =
993                 { { 10, 5, 0 }, { 5, 5, 0 }, { 0, 5, 0 }, { 0, 0, 0 } },
994
995         [LCD_WINCTRL1_FRM_16BPPI5551 >> 25] =
996                 { { 11, 5, 0 }, { 6, 5, 0 }, { 1, 5, 0 }, { 0, 0, 0 } },
997
998         [LCD_WINCTRL1_FRM_16BPPA1555 >> 25] =
999                 { { 10, 5, 0 }, { 5, 5, 0 }, { 0, 5, 0 }, { 15, 1, 0 } },
1000
1001         [LCD_WINCTRL1_FRM_16BPPA5551 >> 25] =
1002                 { { 11, 5, 0 }, { 6, 5, 0 }, { 1, 5, 0 }, { 0, 1, 0 } },
1003
1004         [LCD_WINCTRL1_FRM_24BPP >> 25] =
1005                 { { 16, 8, 0 }, { 8, 8, 0 }, { 0, 8, 0 }, { 0, 0, 0 } },
1006
1007         [LCD_WINCTRL1_FRM_32BPP >> 25] =
1008                 { { 16, 8, 0 }, { 8, 8, 0 }, { 0, 8, 0 }, { 24, 0, 0 } },
1009 };
1010
1011 /*-------------------------------------------------------------------------*/
1012
1013 /* Helpers */
1014
1015 static void au1200fb_update_fbinfo(struct fb_info *fbi)
1016 {
1017         /* FIX!!!! This also needs to take the window pixel format into account!!! */
1018
1019         /* Update var-dependent FB info */
1020         if (panel_is_color(panel)) {
1021                 if (fbi->var.bits_per_pixel <= 8) {
1022                         /* palettized */
1023                         fbi->fix.visual = FB_VISUAL_PSEUDOCOLOR;
1024                         fbi->fix.line_length = fbi->var.xres_virtual /
1025                                 (8/fbi->var.bits_per_pixel);
1026                 } else {
1027                         /* non-palettized */
1028                         fbi->fix.visual = FB_VISUAL_TRUECOLOR;
1029                         fbi->fix.line_length = fbi->var.xres_virtual * (fbi->var.bits_per_pixel / 8);
1030                 }
1031         } else {
1032                 /* mono FIX!!! mono 8 and 4 bits */
1033                 fbi->fix.visual = FB_VISUAL_MONO10;
1034                 fbi->fix.line_length = fbi->var.xres_virtual / 8;
1035         }
1036
1037         fbi->screen_size = fbi->fix.line_length * fbi->var.yres_virtual;
1038         print_dbg("line length: %d\n", fbi->fix.line_length);
1039         print_dbg("bits_per_pixel: %d\n", fbi->var.bits_per_pixel);
1040 }
1041
1042 /*-------------------------------------------------------------------------*/
1043
1044 /* AU1200 framebuffer driver */
1045
1046 /* fb_check_var
1047  * Validate var settings with hardware restrictions and modify it if necessary
1048  */
1049 static int au1200fb_fb_check_var(struct fb_var_screeninfo *var,
1050         struct fb_info *fbi)
1051 {
1052         struct au1200fb_device *fbdev = (struct au1200fb_device *)fbi;
1053         u32 pixclock;
1054         int screen_size, plane;
1055
1056         plane = fbdev->plane;
1057
1058         /* Make sure that the mode respect all LCD controller and
1059          * panel restrictions. */
1060         var->xres = win->w[plane].xres;
1061         var->yres = win->w[plane].yres;
1062
1063         /* No need for virtual resolution support */
1064         var->xres_virtual = var->xres;
1065         var->yres_virtual = var->yres;
1066
1067         var->bits_per_pixel = winbpp(win->w[plane].mode_winctrl1);
1068
1069         screen_size = var->xres_virtual * var->yres_virtual;
1070         if (var->bits_per_pixel > 8) screen_size *= (var->bits_per_pixel / 8);
1071         else screen_size /= (8/var->bits_per_pixel);
1072
1073         if (fbdev->fb_len < screen_size)
1074                 return -EINVAL; /* Virtual screen is to big, abort */
1075
1076         /* FIX!!!! what are the implicaitons of ignoring this for windows ??? */
1077         /* The max LCD clock is fixed to 48MHz (value of AUX_CLK). The pixel
1078          * clock can only be obtain by dividing this value by an even integer.
1079          * Fallback to a slower pixel clock if necessary. */
1080         pixclock = max((u32)(PICOS2KHZ(var->pixclock) * 1000), fbi->monspecs.dclkmin);
1081         pixclock = min(pixclock, min(fbi->monspecs.dclkmax, (u32)AU1200_LCD_MAX_CLK/2));
1082
1083         if (AU1200_LCD_MAX_CLK % pixclock) {
1084                 int diff = AU1200_LCD_MAX_CLK % pixclock;
1085                 pixclock -= diff;
1086         }
1087
1088         var->pixclock = KHZ2PICOS(pixclock/1000);
1089 #if 0
1090         if (!panel_is_active(panel)) {
1091                 int pcd = AU1200_LCD_MAX_CLK / (pixclock * 2) - 1;
1092
1093                 if (!panel_is_color(panel)
1094                         && (panel->control_base & LCD_CONTROL_MPI) && (pcd < 3)) {
1095                         /* STN 8bit mono panel support is up to 6MHz pixclock */
1096                         var->pixclock = KHZ2PICOS(6000);
1097                 } else if (!pcd) {
1098                         /* Other STN panel support is up to 12MHz  */
1099                         var->pixclock = KHZ2PICOS(12000);
1100                 }
1101         }
1102 #endif
1103         /* Set bitfield accordingly */
1104         switch (var->bits_per_pixel) {
1105                 case 16:
1106                 {
1107                         /* 16bpp True color.
1108                          * These must be set to MATCH WINCTRL[FORM] */
1109                         int idx;
1110                         idx = (win->w[0].mode_winctrl1 & LCD_WINCTRL1_FRM) >> 25;
1111                         var->red    = rgb_bitfields[idx][0];
1112                         var->green  = rgb_bitfields[idx][1];
1113                         var->blue   = rgb_bitfields[idx][2];
1114                         var->transp = rgb_bitfields[idx][3];
1115                         break;
1116                 }
1117
1118                 case 32:
1119                 {
1120                         /* 32bpp True color.
1121                          * These must be set to MATCH WINCTRL[FORM] */
1122                         int idx;
1123                         idx = (win->w[0].mode_winctrl1 & LCD_WINCTRL1_FRM) >> 25;
1124                         var->red    = rgb_bitfields[idx][0];
1125                         var->green  = rgb_bitfields[idx][1];
1126                         var->blue   = rgb_bitfields[idx][2];
1127                         var->transp = rgb_bitfields[idx][3];
1128                         break;
1129                 }
1130                 default:
1131                         print_dbg("Unsupported depth %dbpp", var->bits_per_pixel);
1132                         return -EINVAL;
1133         }
1134
1135         return 0;
1136 }
1137
1138 /* fb_set_par
1139  * Set hardware with var settings. This will enable the controller with a
1140  * specific mode, normally validated with the fb_check_var method
1141  */
1142 static int au1200fb_fb_set_par(struct fb_info *fbi)
1143 {
1144         struct au1200fb_device *fbdev = (struct au1200fb_device *)fbi;
1145
1146         au1200fb_update_fbinfo(fbi);
1147         au1200_setmode(fbdev);
1148
1149         return 0;
1150 }
1151
1152 /* fb_setcolreg
1153  * Set color in LCD palette.
1154  */
1155 static int au1200fb_fb_setcolreg(unsigned regno, unsigned red, unsigned green,
1156         unsigned blue, unsigned transp, struct fb_info *fbi)
1157 {
1158         volatile u32 *palette = lcd->palette;
1159         u32 value;
1160
1161         if (regno > (AU1200_LCD_NBR_PALETTE_ENTRIES - 1))
1162                 return -EINVAL;
1163
1164         if (fbi->var.grayscale) {
1165                 /* Convert color to grayscale */
1166                 red = green = blue =
1167                         (19595 * red + 38470 * green + 7471 * blue) >> 16;
1168         }
1169
1170         if (fbi->fix.visual == FB_VISUAL_TRUECOLOR) {
1171                 /* Place color in the pseudopalette */
1172                 if (regno > 16)
1173                         return -EINVAL;
1174
1175                 palette = (u32*) fbi->pseudo_palette;
1176
1177                 red   >>= (16 - fbi->var.red.length);
1178                 green >>= (16 - fbi->var.green.length);
1179                 blue  >>= (16 - fbi->var.blue.length);
1180
1181                 value = (red   << fbi->var.red.offset)  |
1182                         (green << fbi->var.green.offset)|
1183                         (blue  << fbi->var.blue.offset);
1184                 value &= 0xFFFF;
1185
1186         } else if (1 /*FIX!!! panel_is_active(fbdev->panel)*/) {
1187                 /* COLOR TFT PALLETTIZED (use RGB 565) */
1188                 value = (red & 0xF800)|((green >> 5) &
1189                                 0x07E0)|((blue >> 11) & 0x001F);
1190                 value &= 0xFFFF;
1191
1192         } else if (0 /*panel_is_color(fbdev->panel)*/) {
1193                 /* COLOR STN MODE */
1194                 value = 0x1234;
1195                 value &= 0xFFF;
1196         } else {
1197                 /* MONOCHROME MODE */
1198                 value = (green >> 12) & 0x000F;
1199                 value &= 0xF;
1200         }
1201
1202         palette[regno] = value;
1203
1204         return 0;
1205 }
1206
1207 /* fb_blank
1208  * Blank the screen. Depending on the mode, the screen will be
1209  * activated with the backlight color, or desactivated
1210  */
1211 static int au1200fb_fb_blank(int blank_mode, struct fb_info *fbi)
1212 {
1213         /* Short-circuit screen blanking */
1214         if (noblanking)
1215                 return 0;
1216
1217         switch (blank_mode) {
1218
1219         case FB_BLANK_UNBLANK:
1220         case FB_BLANK_NORMAL:
1221                 /* printk("turn on panel\n"); */
1222                 au1200_setpanel(panel);
1223                 break;
1224         case FB_BLANK_VSYNC_SUSPEND:
1225         case FB_BLANK_HSYNC_SUSPEND:
1226         case FB_BLANK_POWERDOWN:
1227                 /* printk("turn off panel\n"); */
1228                 au1200_setpanel(NULL);
1229                 break;
1230         default:
1231                 break;
1232
1233         }
1234
1235         /* FB_BLANK_NORMAL is a soft blank */
1236         return (blank_mode == FB_BLANK_NORMAL) ? -EINVAL : 0;
1237 }
1238
1239 /* fb_mmap
1240  * Map video memory in user space. We don't use the generic fb_mmap
1241  * method mainly to allow the use of the TLB streaming flag (CCA=6)
1242  */
1243 static int au1200fb_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
1244
1245 {
1246         unsigned int len;
1247         unsigned long start=0, off;
1248         struct au1200fb_device *fbdev = (struct au1200fb_device *) info;
1249
1250 #ifdef CONFIG_PM
1251         au1xxx_pm_access(LCD_pm_dev);
1252 #endif
1253
1254         if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT)) {
1255                 return -EINVAL;
1256         }
1257
1258         start = fbdev->fb_phys & PAGE_MASK;
1259         len = PAGE_ALIGN((start & ~PAGE_MASK) + fbdev->fb_len);
1260
1261         off = vma->vm_pgoff << PAGE_SHIFT;
1262
1263         if ((vma->vm_end - vma->vm_start + off) > len) {
1264                 return -EINVAL;
1265         }
1266
1267         off += start;
1268         vma->vm_pgoff = off >> PAGE_SHIFT;
1269
1270         vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1271         pgprot_val(vma->vm_page_prot) |= _CACHE_MASK; /* CCA=7 */
1272
1273         vma->vm_flags |= VM_IO;
1274
1275         return io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
1276                                   vma->vm_end - vma->vm_start,
1277                                   vma->vm_page_prot);
1278
1279         return 0;
1280 }
1281
1282 static void set_global(u_int cmd, struct au1200_lcd_global_regs_t *pdata)
1283 {
1284
1285         unsigned int hi1, divider;
1286
1287         /* SCREEN_SIZE: user cannot reset size, must switch panel choice */
1288
1289         if (pdata->flags & SCREEN_BACKCOLOR)
1290                 lcd->backcolor = pdata->backcolor;
1291
1292         if (pdata->flags & SCREEN_BRIGHTNESS) {
1293
1294                 // limit brightness pwm duty to >= 30/1600
1295                 if (pdata->brightness < 30) {
1296                         pdata->brightness = 30;
1297                 }
1298                 divider = (lcd->pwmdiv & 0x3FFFF) + 1;
1299                 hi1 = (lcd->pwmhi >> 16) + 1;
1300                 hi1 = (((pdata->brightness & 0xFF)+1) * divider >> 8);
1301                 lcd->pwmhi &= 0xFFFF;
1302                 lcd->pwmhi |= (hi1 << 16);
1303         }
1304
1305         if (pdata->flags & SCREEN_COLORKEY)
1306                 lcd->colorkey = pdata->colorkey;
1307
1308         if (pdata->flags & SCREEN_MASK)
1309                 lcd->colorkeymsk = pdata->mask;
1310         au_sync();
1311 }
1312
1313 static void get_global(u_int cmd, struct au1200_lcd_global_regs_t *pdata)
1314 {
1315         unsigned int hi1, divider;
1316
1317         pdata->xsize = ((lcd->screen & LCD_SCREEN_SX) >> 19) + 1;
1318         pdata->ysize = ((lcd->screen & LCD_SCREEN_SY) >> 8) + 1;
1319
1320         pdata->backcolor = lcd->backcolor;
1321         pdata->colorkey = lcd->colorkey;
1322         pdata->mask = lcd->colorkeymsk;
1323
1324         // brightness
1325         hi1 = (lcd->pwmhi >> 16) + 1;
1326         divider = (lcd->pwmdiv & 0x3FFFF) + 1;
1327         pdata->brightness = ((hi1 << 8) / divider) - 1;
1328         au_sync();
1329 }
1330
1331 static void set_window(unsigned int plane,
1332         struct au1200_lcd_window_regs_t *pdata)
1333 {
1334         unsigned int val, bpp;
1335
1336         /* Window control register 0 */
1337         if (pdata->flags & WIN_POSITION) {
1338                 val = lcd->window[plane].winctrl0 & ~(LCD_WINCTRL0_OX |
1339                                 LCD_WINCTRL0_OY);
1340                 val |= ((pdata->xpos << 21) & LCD_WINCTRL0_OX);
1341                 val |= ((pdata->ypos << 10) & LCD_WINCTRL0_OY);
1342                 lcd->window[plane].winctrl0 = val;
1343         }
1344         if (pdata->flags & WIN_ALPHA_COLOR) {
1345                 val = lcd->window[plane].winctrl0 & ~(LCD_WINCTRL0_A);
1346                 val |= ((pdata->alpha_color << 2) & LCD_WINCTRL0_A);
1347                 lcd->window[plane].winctrl0 = val;
1348         }
1349         if (pdata->flags & WIN_ALPHA_MODE) {
1350                 val = lcd->window[plane].winctrl0 & ~(LCD_WINCTRL0_AEN);
1351                 val |= ((pdata->alpha_mode << 1) & LCD_WINCTRL0_AEN);
1352                 lcd->window[plane].winctrl0 = val;
1353         }
1354
1355         /* Window control register 1 */
1356         if (pdata->flags & WIN_PRIORITY) {
1357                 val = lcd->window[plane].winctrl1 & ~(LCD_WINCTRL1_PRI);
1358                 val |= ((pdata->priority << 30) & LCD_WINCTRL1_PRI);
1359                 lcd->window[plane].winctrl1 = val;
1360         }
1361         if (pdata->flags & WIN_CHANNEL) {
1362                 val = lcd->window[plane].winctrl1 & ~(LCD_WINCTRL1_PIPE);
1363                 val |= ((pdata->channel << 29) & LCD_WINCTRL1_PIPE);
1364                 lcd->window[plane].winctrl1 = val;
1365         }
1366         if (pdata->flags & WIN_BUFFER_FORMAT) {
1367                 val = lcd->window[plane].winctrl1 & ~(LCD_WINCTRL1_FRM);
1368                 val |= ((pdata->buffer_format << 25) & LCD_WINCTRL1_FRM);
1369                 lcd->window[plane].winctrl1 = val;
1370         }
1371         if (pdata->flags & WIN_COLOR_ORDER) {
1372                 val = lcd->window[plane].winctrl1 & ~(LCD_WINCTRL1_CCO);
1373                 val |= ((pdata->color_order << 24) & LCD_WINCTRL1_CCO);
1374                 lcd->window[plane].winctrl1 = val;
1375         }
1376         if (pdata->flags & WIN_PIXEL_ORDER) {
1377                 val = lcd->window[plane].winctrl1 & ~(LCD_WINCTRL1_PO);
1378                 val |= ((pdata->pixel_order << 22) & LCD_WINCTRL1_PO);
1379                 lcd->window[plane].winctrl1 = val;
1380         }
1381         if (pdata->flags & WIN_SIZE) {
1382                 val = lcd->window[plane].winctrl1 & ~(LCD_WINCTRL1_SZX |
1383                                 LCD_WINCTRL1_SZY);
1384                 val |= (((pdata->xsize << 11) - 1) & LCD_WINCTRL1_SZX);
1385                 val |= (((pdata->ysize) - 1) & LCD_WINCTRL1_SZY);
1386                 lcd->window[plane].winctrl1 = val;
1387                 /* program buffer line width */
1388                 bpp = winbpp(val) / 8;
1389                 val = lcd->window[plane].winctrl2 & ~(LCD_WINCTRL2_BX);
1390                 val |= (((pdata->xsize * bpp) << 8) & LCD_WINCTRL2_BX);
1391                 lcd->window[plane].winctrl2 = val;
1392         }
1393
1394         /* Window control register 2 */
1395         if (pdata->flags & WIN_COLORKEY_MODE) {
1396                 val = lcd->window[plane].winctrl2 & ~(LCD_WINCTRL2_CKMODE);
1397                 val |= ((pdata->colorkey_mode << 24) & LCD_WINCTRL2_CKMODE);
1398                 lcd->window[plane].winctrl2 = val;
1399         }
1400         if (pdata->flags & WIN_DOUBLE_BUFFER_MODE) {
1401                 val = lcd->window[plane].winctrl2 & ~(LCD_WINCTRL2_DBM);
1402                 val |= ((pdata->double_buffer_mode << 23) & LCD_WINCTRL2_DBM);
1403                 lcd->window[plane].winctrl2 = val;
1404         }
1405         if (pdata->flags & WIN_RAM_ARRAY_MODE) {
1406                 val = lcd->window[plane].winctrl2 & ~(LCD_WINCTRL2_RAM);
1407                 val |= ((pdata->ram_array_mode << 21) & LCD_WINCTRL2_RAM);
1408                 lcd->window[plane].winctrl2 = val;
1409         }
1410
1411         /* Buffer line width programmed with WIN_SIZE */
1412
1413         if (pdata->flags & WIN_BUFFER_SCALE) {
1414                 val = lcd->window[plane].winctrl2 & ~(LCD_WINCTRL2_SCX |
1415                                 LCD_WINCTRL2_SCY);
1416                 val |= ((pdata->xsize << 11) & LCD_WINCTRL2_SCX);
1417                 val |= ((pdata->ysize) & LCD_WINCTRL2_SCY);
1418                 lcd->window[plane].winctrl2 = val;
1419         }
1420
1421         if (pdata->flags & WIN_ENABLE) {
1422                 val = lcd->winenable;
1423                 val &= ~(1<<plane);
1424                 val |= (pdata->enable & 1) << plane;
1425                 lcd->winenable = val;
1426         }
1427         au_sync();
1428 }
1429
1430 static void get_window(unsigned int plane,
1431         struct au1200_lcd_window_regs_t *pdata)
1432 {
1433         /* Window control register 0 */
1434         pdata->xpos = (lcd->window[plane].winctrl0 & LCD_WINCTRL0_OX) >> 21;
1435         pdata->ypos = (lcd->window[plane].winctrl0 & LCD_WINCTRL0_OY) >> 10;
1436         pdata->alpha_color = (lcd->window[plane].winctrl0 & LCD_WINCTRL0_A) >> 2;
1437         pdata->alpha_mode = (lcd->window[plane].winctrl0 & LCD_WINCTRL0_AEN) >> 1;
1438
1439         /* Window control register 1 */
1440         pdata->priority = (lcd->window[plane].winctrl1& LCD_WINCTRL1_PRI) >> 30;
1441         pdata->channel = (lcd->window[plane].winctrl1 & LCD_WINCTRL1_PIPE) >> 29;
1442         pdata->buffer_format = (lcd->window[plane].winctrl1 & LCD_WINCTRL1_FRM) >> 25;
1443         pdata->color_order = (lcd->window[plane].winctrl1 & LCD_WINCTRL1_CCO) >> 24;
1444         pdata->pixel_order = (lcd->window[plane].winctrl1 & LCD_WINCTRL1_PO) >> 22;
1445         pdata->xsize = ((lcd->window[plane].winctrl1 & LCD_WINCTRL1_SZX) >> 11) + 1;
1446         pdata->ysize = (lcd->window[plane].winctrl1 & LCD_WINCTRL1_SZY) + 1;
1447
1448         /* Window control register 2 */
1449         pdata->colorkey_mode = (lcd->window[plane].winctrl2 & LCD_WINCTRL2_CKMODE) >> 24;
1450         pdata->double_buffer_mode = (lcd->window[plane].winctrl2 & LCD_WINCTRL2_DBM) >> 23;
1451         pdata->ram_array_mode = (lcd->window[plane].winctrl2 & LCD_WINCTRL2_RAM) >> 21;
1452
1453         pdata->enable = (lcd->winenable >> plane) & 1;
1454         au_sync();
1455 }
1456
1457 static int au1200fb_ioctl(struct fb_info *info, unsigned int cmd,
1458                           unsigned long arg)
1459 {
1460         int plane;
1461         int val;
1462
1463 #ifdef CONFIG_PM
1464         au1xxx_pm_access(LCD_pm_dev);
1465 #endif
1466
1467         plane = fbinfo2index(info);
1468         print_dbg("au1200fb: ioctl %d on plane %d\n", cmd, plane);
1469
1470         if (cmd == AU1200_LCD_FB_IOCTL) {
1471                 struct au1200_lcd_iodata_t iodata;
1472
1473                 if (copy_from_user(&iodata, (void __user *) arg, sizeof(iodata)))
1474                         return -EFAULT;
1475
1476                 print_dbg("FB IOCTL called\n");
1477
1478                 switch (iodata.subcmd) {
1479                 case AU1200_LCD_SET_SCREEN:
1480                         print_dbg("AU1200_LCD_SET_SCREEN\n");
1481                         set_global(cmd, &iodata.global);
1482                         break;
1483
1484                 case AU1200_LCD_GET_SCREEN:
1485                         print_dbg("AU1200_LCD_GET_SCREEN\n");
1486                         get_global(cmd, &iodata.global);
1487                         break;
1488
1489                 case AU1200_LCD_SET_WINDOW:
1490                         print_dbg("AU1200_LCD_SET_WINDOW\n");
1491                         set_window(plane, &iodata.window);
1492                         break;
1493
1494                 case AU1200_LCD_GET_WINDOW:
1495                         print_dbg("AU1200_LCD_GET_WINDOW\n");
1496                         get_window(plane, &iodata.window);
1497                         break;
1498
1499                 case AU1200_LCD_SET_PANEL:
1500                         print_dbg("AU1200_LCD_SET_PANEL\n");
1501                         if ((iodata.global.panel_choice >= 0) &&
1502                                         (iodata.global.panel_choice <
1503                                          NUM_PANELS))
1504                         {
1505                                 struct panel_settings *newpanel;
1506                                 panel_index = iodata.global.panel_choice;
1507                                 newpanel = &known_lcd_panels[panel_index];
1508                                 au1200_setpanel(newpanel);
1509                         }
1510                         break;
1511
1512                 case AU1200_LCD_GET_PANEL:
1513                         print_dbg("AU1200_LCD_GET_PANEL\n");
1514                         iodata.global.panel_choice = panel_index;
1515                         break;
1516
1517                 default:
1518                         return -EINVAL;
1519                 }
1520
1521                 val = copy_to_user((void __user *) arg, &iodata, sizeof(iodata));
1522                 if (val) {
1523                         print_dbg("error: could not copy %d bytes\n", val);
1524                         return -EFAULT;
1525                 }
1526         }
1527
1528         return 0;
1529 }
1530
1531
1532 static struct fb_ops au1200fb_fb_ops = {
1533         .owner          = THIS_MODULE,
1534         .fb_check_var   = au1200fb_fb_check_var,
1535         .fb_set_par     = au1200fb_fb_set_par,
1536         .fb_setcolreg   = au1200fb_fb_setcolreg,
1537         .fb_blank       = au1200fb_fb_blank,
1538         .fb_fillrect    = cfb_fillrect,
1539         .fb_copyarea    = cfb_copyarea,
1540         .fb_imageblit   = cfb_imageblit,
1541         .fb_sync        = NULL,
1542         .fb_ioctl       = au1200fb_ioctl,
1543         .fb_mmap        = au1200fb_fb_mmap,
1544 };
1545
1546 /*-------------------------------------------------------------------------*/
1547
1548 static irqreturn_t au1200fb_handle_irq(int irq, void* dev_id, struct pt_regs *regs)
1549 {
1550         /* Nothing to do for now, just clear any pending interrupt */
1551         lcd->intstatus = lcd->intstatus;
1552         au_sync();
1553
1554         return IRQ_HANDLED;
1555 }
1556
1557 /*-------------------------------------------------------------------------*/
1558
1559 /* AU1200 LCD device probe helpers */
1560
1561 static int au1200fb_init_fbinfo(struct au1200fb_device *fbdev)
1562 {
1563         struct fb_info *fbi = &fbdev->fb_info;
1564         int bpp;
1565
1566         memset(fbi, 0, sizeof(struct fb_info));
1567         fbi->fbops = &au1200fb_fb_ops;
1568
1569         bpp = winbpp(win->w[fbdev->plane].mode_winctrl1);
1570
1571         /* Copy monitor specs from panel data */
1572         /* fixme: we're setting up LCD controller windows, so these dont give a
1573         damn as to what the monitor specs are (the panel itself does, but that
1574         isnt done here...so maybe need a generic catchall monitor setting??? */
1575         memcpy(&fbi->monspecs, &panel->monspecs, sizeof(struct fb_monspecs));
1576
1577         /* We first try the user mode passed in argument. If that failed,
1578          * or if no one has been specified, we default to the first mode of the
1579          * panel list. Note that after this call, var data will be set */
1580         if (!fb_find_mode(&fbi->var,
1581                           fbi,
1582                           NULL, /* drv_info.opt_mode, */
1583                           fbi->monspecs.modedb,
1584                           fbi->monspecs.modedb_len,
1585                           fbi->monspecs.modedb,
1586                           bpp)) {
1587
1588                 print_err("Cannot find valid mode for panel %s", panel->name);
1589                 return -EFAULT;
1590         }
1591
1592         fbi->pseudo_palette = kmalloc(sizeof(u32) * 16, GFP_KERNEL);
1593         if (!fbi->pseudo_palette) {
1594                 return -ENOMEM;
1595         }
1596         memset(fbi->pseudo_palette, 0, sizeof(u32) * 16);
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 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,
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                           SA_INTERRUPT | SA_SHIRQ, "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 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, PAGE_ALIGN(fbdev->fb_len),
1738                                         fbdev->fb_mem, fbdev->fb_phys);
1739                 if (fbdev->fb_info.cmap.len != 0)
1740                         fb_dealloc_cmap(&fbdev->fb_info.cmap);
1741                 if (fbdev->fb_info.pseudo_palette)
1742                         kfree(fbdev->fb_info.pseudo_palette);
1743         }
1744
1745         free_irq(AU1200_LCD_INT, (void *)dev);
1746
1747         return 0;
1748 }
1749
1750 #ifdef CONFIG_PM
1751 static int au1200fb_drv_suspend(struct device *dev, u32 state, u32 level)
1752 {
1753         /* TODO */
1754         return 0;
1755 }
1756
1757 static int au1200fb_drv_resume(struct device *dev, u32 level)
1758 {
1759         /* TODO */
1760         return 0;
1761 }
1762 #endif /* CONFIG_PM */
1763
1764 static struct device_driver au1200fb_driver = {
1765         .name           = "au1200-lcd",
1766         .bus            = &platform_bus_type,
1767         .probe          = au1200fb_drv_probe,
1768         .remove         = au1200fb_drv_remove,
1769 #ifdef CONFIG_PM
1770         .suspend        = au1200fb_drv_suspend,
1771         .resume         = au1200fb_drv_resume,
1772 #endif
1773 };
1774
1775 /*-------------------------------------------------------------------------*/
1776
1777 /* Kernel driver */
1778
1779 static void au1200fb_setup(void)
1780 {
1781         char* options = NULL;
1782         char* this_opt;
1783         int num_panels = ARRAY_SIZE(known_lcd_panels);
1784         int panel_idx = -1;
1785
1786         fb_get_options(DRIVER_NAME, &options);
1787
1788         if (options) {
1789                 while ((this_opt = strsep(&options,",")) != NULL) {
1790                         /* Panel option - can be panel name,
1791                          * "bs" for board-switch, or number/index */
1792                         if (!strncmp(this_opt, "panel:", 6)) {
1793                                 int i;
1794                                 long int li;
1795                                 char *endptr;
1796                                 this_opt += 6;
1797                                 /* First check for index, which allows
1798                                  * to short circuit this mess */
1799                                 li = simple_strtol(this_opt, &endptr, 0);
1800                                 if (*endptr == '\0') {
1801                                         panel_idx = (int)li;
1802                                 }
1803                                 else if (strcmp(this_opt, "bs") == 0) {
1804                                         extern int board_au1200fb_panel(void);
1805                                         panel_idx = board_au1200fb_panel();
1806                                 }
1807
1808                                 else
1809                                 for (i = 0; i < num_panels; i++) {
1810                                         if (!strcmp(this_opt, known_lcd_panels[i].name)) {
1811                                                 panel_idx = i;
1812                                                 break;
1813                                         }
1814                                 }
1815
1816                                 if ((panel_idx < 0) || (panel_idx >= num_panels)) {
1817                                                 print_warn("Panel %s not supported!", this_opt);
1818                                 }
1819                                 else
1820                                         panel_index = panel_idx;
1821                         }
1822
1823                         else if (strncmp(this_opt, "nohwcursor", 10) == 0) {
1824                                 nohwcursor = 1;
1825                         }
1826
1827                         /* Unsupported option */
1828                         else {
1829                                 print_warn("Unsupported option \"%s\"", this_opt);
1830                         }
1831                 }
1832         }
1833 }
1834
1835 #ifdef CONFIG_PM
1836 static int au1200fb_pm_callback(au1xxx_power_dev_t *dev,
1837                 au1xxx_request_t request, void *data) {
1838         int retval = -1;
1839         unsigned int d = 0;
1840         unsigned int brightness = 0;
1841
1842         if (request == AU1XXX_PM_SLEEP) {
1843                 board_au1200fb_panel_shutdown();
1844         }
1845         else if (request == AU1XXX_PM_WAKEUP) {
1846                 if(dev->prev_state == SLEEP_STATE)
1847                 {
1848                         int plane;
1849                         au1200_setpanel(panel);
1850                         for (plane = 0; plane < CONFIG_FB_AU1200_DEVS; ++plane)         {
1851                                 struct au1200fb_device *fbdev;
1852                                 fbdev = &_au1200fb_devices[plane];
1853                                 au1200fb_fb_set_par(&fbdev->fb_info);
1854                         }
1855                 }
1856
1857                 d = *((unsigned int*)data);
1858                 if(d <=10) brightness = 26;
1859                 else if(d<=20) brightness = 51;
1860                 else if(d<=30) brightness = 77;
1861                 else if(d<=40) brightness = 102;
1862                 else if(d<=50) brightness = 128;
1863                 else if(d<=60) brightness = 153;
1864                 else if(d<=70) brightness = 179;
1865                 else if(d<=80) brightness = 204;
1866                 else if(d<=90) brightness = 230;
1867                 else brightness = 255;
1868                 set_brightness(brightness);
1869         } else if (request == AU1XXX_PM_GETSTATUS) {
1870                 return dev->cur_state;
1871         } else if (request == AU1XXX_PM_ACCESS) {
1872                 if (dev->cur_state != SLEEP_STATE)
1873                         return retval;
1874                 else {
1875                         au1200_setpanel(panel);
1876                 }
1877         } else if (request == AU1XXX_PM_IDLE) {
1878         } else if (request == AU1XXX_PM_CLEANUP) {
1879         }
1880
1881         return retval;
1882 }
1883 #endif
1884
1885 static int __init au1200fb_init(void)
1886 {
1887         print_info("" DRIVER_DESC "");
1888
1889         /* Setup driver with options */
1890         au1200fb_setup();
1891
1892         /* Point to the panel selected */
1893         panel = &known_lcd_panels[panel_index];
1894         win = &windows[window_index];
1895
1896         printk(DRIVER_NAME ": Panel %d %s\n", panel_index, panel->name);
1897         printk(DRIVER_NAME ": Win %d %s\n", window_index, win->name);
1898
1899         /* Kickstart the panel, the framebuffers/windows come soon enough */
1900         au1200_setpanel(panel);
1901
1902         #ifdef CONFIG_PM
1903         LCD_pm_dev = new_au1xxx_power_device("LCD", &au1200fb_pm_callback, NULL);
1904         if ( LCD_pm_dev == NULL)
1905                 printk(KERN_INFO "Unable to create a power management device entry for the au1200fb.\n");
1906         else
1907                 printk(KERN_INFO "Power management device entry for the au1200fb loaded.\n");
1908         #endif
1909
1910         return driver_register(&au1200fb_driver);
1911 }
1912
1913 static void __exit au1200fb_cleanup(void)
1914 {
1915         driver_unregister(&au1200fb_driver);
1916 }
1917
1918 module_init(au1200fb_init);
1919 module_exit(au1200fb_cleanup);
1920
1921 MODULE_DESCRIPTION(DRIVER_DESC);
1922 MODULE_LICENSE("GPL");
1923 /*
1924  * BRIEF MODULE DESCRIPTION
1925  *      Au1200 LCD Driver.
1926  *
1927  * Copyright 2004-2005 AMD
1928  * Author: AMD
1929  *
1930  * Based on:
1931  * linux/drivers/video/skeletonfb.c -- Skeleton for a frame buffer device
1932  *  Created 28 Dec 1997 by Geert Uytterhoeven
1933  *
1934  *  This program is free software; you can redistribute  it and/or modify it
1935  *  under  the terms of  the GNU General  Public License as published by the
1936  *  Free Software Foundation;  either version 2 of the  License, or (at your
1937  *  option) any later version.
1938  *
1939  *  THIS  SOFTWARE  IS PROVIDED   ``AS  IS'' AND   ANY  EXPRESS OR IMPLIED
1940  *  WARRANTIES,   INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
1941  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
1942  *  NO  EVENT  SHALL   THE AUTHOR  BE    LIABLE FOR ANY   DIRECT, INDIRECT,
1943  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1944  *  NOT LIMITED   TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
1945  *  USE, DATA,  OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
1946  *  ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT
1947  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
1948  *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1949  *
1950  *  You should have received a copy of the  GNU General Public License along
1951  *  with this program; if not, write  to the Free Software Foundation, Inc.,
1952  *  675 Mass Ave, Cambridge, MA 02139, USA.
1953  */
1954
1955 #include <linux/module.h>
1956 #include <linux/platform_device.h>
1957 #include <linux/kernel.h>
1958 #include <linux/errno.h>
1959 #include <linux/string.h>
1960 #include <linux/mm.h>
1961 #include <linux/fb.h>
1962 #include <linux/init.h>
1963 #include <linux/interrupt.h>
1964 #include <linux/ctype.h>
1965 #include <linux/dma-mapping.h>
1966
1967 #include <asm/mach-au1x00/au1000.h>
1968 #include "au1200fb.h"
1969
1970 #ifdef CONFIG_PM
1971 #include <asm/mach-au1x00/au1xxx_pm.h>
1972 #endif
1973
1974 #ifndef CONFIG_FB_AU1200_DEVS
1975 #define CONFIG_FB_AU1200_DEVS 4
1976 #endif
1977
1978 #define DRIVER_NAME "au1200fb"
1979 #define DRIVER_DESC "LCD controller driver for AU1200 processors"
1980
1981 #define DEBUG 1
1982
1983 #define print_err(f, arg...) printk(KERN_ERR DRIVER_NAME ": " f "\n", ## arg)
1984 #define print_warn(f, arg...) printk(KERN_WARNING DRIVER_NAME ": " f "\n", ## arg)
1985 #define print_info(f, arg...) printk(KERN_INFO DRIVER_NAME ": " f "\n", ## arg)
1986
1987 #if DEBUG
1988 #define print_dbg(f, arg...) printk(KERN_DEBUG __FILE__ ": " f "\n", ## arg)
1989 #else
1990 #define print_dbg(f, arg...) do {} while (0)
1991 #endif
1992
1993
1994 #define AU1200_LCD_FB_IOCTL 0x46FF
1995
1996 #define AU1200_LCD_SET_SCREEN 1
1997 #define AU1200_LCD_GET_SCREEN 2
1998 #define AU1200_LCD_SET_WINDOW 3
1999 #define AU1200_LCD_GET_WINDOW 4
2000 #define AU1200_LCD_SET_PANEL  5
2001 #define AU1200_LCD_GET_PANEL  6
2002
2003 #define SCREEN_SIZE                 (1<< 1)
2004 #define SCREEN_BACKCOLOR    (1<< 2)
2005 #define SCREEN_BRIGHTNESS   (1<< 3)
2006 #define SCREEN_COLORKEY     (1<< 4)
2007 #define SCREEN_MASK         (1<< 5)
2008
2009 struct au1200_lcd_global_regs_t {
2010         unsigned int flags;
2011         unsigned int xsize;
2012         unsigned int ysize;
2013         unsigned int backcolor;
2014         unsigned int brightness;
2015         unsigned int colorkey;
2016         unsigned int mask;
2017         unsigned int panel_choice;
2018         char panel_desc[80];
2019
2020 };
2021
2022 #define WIN_POSITION            (1<< 0)
2023 #define WIN_ALPHA_COLOR         (1<< 1)
2024 #define WIN_ALPHA_MODE          (1<< 2)
2025 #define WIN_PRIORITY            (1<< 3)
2026 #define WIN_CHANNEL             (1<< 4)
2027 #define WIN_BUFFER_FORMAT       (1<< 5)
2028 #define WIN_COLOR_ORDER         (1<< 6)
2029 #define WIN_PIXEL_ORDER         (1<< 7)
2030 #define WIN_SIZE                (1<< 8)
2031 #define WIN_COLORKEY_MODE       (1<< 9)
2032 #define WIN_DOUBLE_BUFFER_MODE  (1<< 10)
2033 #define WIN_RAM_ARRAY_MODE      (1<< 11)
2034 #define WIN_BUFFER_SCALE        (1<< 12)
2035 #define WIN_ENABLE                  (1<< 13)
2036
2037 struct au1200_lcd_window_regs_t {
2038         unsigned int flags;
2039         unsigned int xpos;
2040         unsigned int ypos;
2041         unsigned int alpha_color;
2042         unsigned int alpha_mode;
2043         unsigned int priority;
2044         unsigned int channel;
2045         unsigned int buffer_format;
2046         unsigned int color_order;
2047         unsigned int pixel_order;
2048         unsigned int xsize;
2049         unsigned int ysize;
2050         unsigned int colorkey_mode;
2051         unsigned int double_buffer_mode;
2052         unsigned int ram_array_mode;
2053         unsigned int xscale;
2054         unsigned int yscale;
2055         unsigned int enable;
2056 };
2057
2058
2059 struct au1200_lcd_iodata_t {
2060         unsigned int subcmd;
2061         struct au1200_lcd_global_regs_t global;
2062         struct au1200_lcd_window_regs_t window;
2063 };
2064
2065 #if defined(__BIG_ENDIAN)
2066 #define LCD_CONTROL_DEFAULT_PO LCD_CONTROL_PO_11
2067 #else
2068 #define LCD_CONTROL_DEFAULT_PO LCD_CONTROL_PO_00
2069 #endif
2070 #define LCD_CONTROL_DEFAULT_SBPPF LCD_CONTROL_SBPPF_565
2071
2072 /* Private, per-framebuffer management information (independent of the panel itself) */
2073 struct au1200fb_device {
2074         struct fb_info fb_info;                 /* FB driver info record */
2075
2076         int                                     plane;
2077         unsigned char*          fb_mem;         /* FrameBuffer memory map */
2078         unsigned int            fb_len;
2079         dma_addr_t              fb_phys;
2080 };
2081
2082 static struct au1200fb_device _au1200fb_devices[CONFIG_FB_AU1200_DEVS];
2083 /********************************************************************/
2084
2085 /* LCD controller restrictions */
2086 #define AU1200_LCD_MAX_XRES     1280
2087 #define AU1200_LCD_MAX_YRES     1024
2088 #define AU1200_LCD_MAX_BPP      32
2089 #define AU1200_LCD_MAX_CLK      96000000 /* fixme: this needs to go away ? */
2090 #define AU1200_LCD_NBR_PALETTE_ENTRIES 256
2091
2092 /* Default number of visible screen buffer to allocate */
2093 #define AU1200FB_NBR_VIDEO_BUFFERS 1
2094
2095 /********************************************************************/
2096
2097 static struct au1200_lcd *lcd = (struct au1200_lcd *) AU1200_LCD_ADDR;
2098 static int window_index = 2; /* default is zero */
2099 static int panel_index = 2; /* default is zero */
2100 static struct window_settings *win;
2101 static struct panel_settings *panel;
2102 static int noblanking = 1;
2103 static int nohwcursor = 0;
2104
2105 struct window_settings {
2106         unsigned char name[64];
2107         uint32 mode_backcolor;
2108         uint32 mode_colorkey;
2109         uint32 mode_colorkeymsk;
2110         struct {
2111                 int xres;
2112                 int yres;
2113                 int xpos;
2114                 int ypos;
2115                 uint32 mode_winctrl1; /* winctrl1[FRM,CCO,PO,PIPE] */
2116                 uint32 mode_winenable;
2117         } w[4];
2118 };
2119
2120 #if defined(__BIG_ENDIAN)
2121 #define LCD_WINCTRL1_PO_16BPP LCD_WINCTRL1_PO_00
2122 #else
2123 #define LCD_WINCTRL1_PO_16BPP LCD_WINCTRL1_PO_01
2124 #endif
2125
2126 extern int board_au1200fb_panel_init (void);
2127 extern int board_au1200fb_panel_shutdown (void);
2128
2129 #ifdef CONFIG_PM
2130 int au1200fb_pm_callback(au1xxx_power_dev_t *dev,
2131                 au1xxx_request_t request, void *data);
2132 au1xxx_power_dev_t *LCD_pm_dev;
2133 #endif
2134
2135 /*
2136  * Default window configurations
2137  */
2138 static struct window_settings windows[] = {
2139         { /* Index 0 */
2140                 "0-FS gfx, 1-video, 2-ovly gfx, 3-ovly gfx",
2141                 /* mode_backcolor       */ 0x006600ff,
2142                 /* mode_colorkey,msk*/ 0, 0,
2143                 {
2144                         {
2145                         /* xres, yres, xpos, ypos */ 0, 0, 0, 0,
2146                         /* mode_winctrl1 */ LCD_WINCTRL1_FRM_16BPP565 |
2147                                 LCD_WINCTRL1_PO_16BPP,
2148                         /* mode_winenable*/ LCD_WINENABLE_WEN0,
2149                         },
2150                         {
2151                         /* xres, yres, xpos, ypos */ 100, 100, 100, 100,
2152                         /* mode_winctrl1 */ LCD_WINCTRL1_FRM_16BPP565 |
2153                                 LCD_WINCTRL1_PO_16BPP |
2154                                 LCD_WINCTRL1_PIPE,
2155                         /* mode_winenable*/ LCD_WINENABLE_WEN1,
2156                         },
2157                         {
2158                         /* xres, yres, xpos, ypos */ 0, 0, 0, 0,
2159                         /* mode_winctrl1 */ LCD_WINCTRL1_FRM_16BPP565 |
2160                                 LCD_WINCTRL1_PO_16BPP,
2161                         /* mode_winenable*/ 0,
2162                         },
2163                         {
2164                         /* xres, yres, xpos, ypos */ 0, 0, 0, 0,
2165                         /* mode_winctrl1 */ LCD_WINCTRL1_FRM_16BPP565 |
2166                                 LCD_WINCTRL1_PO_16BPP |
2167                                 LCD_WINCTRL1_PIPE,
2168                         /* mode_winenable*/ 0,
2169                         },
2170                 },
2171         },
2172
2173         { /* Index 1 */
2174                 "0-FS gfx, 1-video, 2-ovly gfx, 3-ovly gfx",
2175                 /* mode_backcolor       */ 0x006600ff,
2176                 /* mode_colorkey,msk*/ 0, 0,
2177                 {
2178                         {
2179                         /* xres, yres, xpos, ypos */ 320, 240, 5, 5,
2180                         /* mode_winctrl1 */ LCD_WINCTRL1_FRM_24BPP |
2181                                 LCD_WINCTRL1_PO_00,
2182                         /* mode_winenable*/ LCD_WINENABLE_WEN0,
2183                         },
2184                         {
2185                         /* xres, yres, xpos, ypos */ 0, 0, 0, 0,
2186                         /* mode_winctrl1 */ LCD_WINCTRL1_FRM_16BPP565
2187                                 | LCD_WINCTRL1_PO_16BPP,
2188                         /* mode_winenable*/ 0,
2189                         },
2190                         {
2191                         /* xres, yres, xpos, ypos */ 100, 100, 0, 0,
2192                         /* mode_winctrl1 */ LCD_WINCTRL1_FRM_16BPP565 |
2193                                 LCD_WINCTRL1_PO_16BPP |
2194                                 LCD_WINCTRL1_PIPE,
2195                         /* mode_winenable*/ 0/*LCD_WINENABLE_WEN2*/,
2196                         },
2197                         {
2198                         /* xres, yres, xpos, ypos */ 200, 25, 0, 0,
2199                         /* mode_winctrl1 */ LCD_WINCTRL1_FRM_16BPP565 |
2200                                 LCD_WINCTRL1_PO_16BPP |
2201                                 LCD_WINCTRL1_PIPE,
2202                         /* mode_winenable*/ 0,
2203                         },
2204                 },
2205         },
2206         { /* Index 2 */
2207                 "0-FS gfx, 1-video, 2-ovly gfx, 3-ovly gfx",
2208                 /* mode_backcolor       */ 0x006600ff,
2209                 /* mode_colorkey,msk*/ 0, 0,
2210                 {
2211                         {
2212                         /* xres, yres, xpos, ypos */ 0, 0, 0, 0,
2213                         /* mode_winctrl1 */ LCD_WINCTRL1_FRM_16BPP565 |
2214                                 LCD_WINCTRL1_PO_16BPP,
2215                         /* mode_winenable*/ LCD_WINENABLE_WEN0,
2216                         },
2217                         {
2218                         /* xres, yres, xpos, ypos */ 0, 0, 0, 0,
2219                         /* mode_winctrl1 */ LCD_WINCTRL1_FRM_16BPP565 |
2220                                 LCD_WINCTRL1_PO_16BPP,
2221                         /* mode_winenable*/ 0,
2222                         },
2223                         {
2224                         /* xres, yres, xpos, ypos */ 0, 0, 0, 0,
2225                         /* mode_winctrl1 */ LCD_WINCTRL1_FRM_32BPP |
2226                                 LCD_WINCTRL1_PO_00|LCD_WINCTRL1_PIPE,
2227                         /* mode_winenable*/ 0/*LCD_WINENABLE_WEN2*/,
2228                         },
2229                         {
2230                         /* xres, yres, xpos, ypos */ 0, 0, 0, 0,
2231                         /* mode_winctrl1 */ LCD_WINCTRL1_FRM_16BPP565 |
2232                                 LCD_WINCTRL1_PO_16BPP |
2233                                 LCD_WINCTRL1_PIPE,
2234                         /* mode_winenable*/ 0,
2235                         },
2236                 },
2237         },
2238         /* Need VGA 640 @ 24bpp, @ 32bpp */
2239         /* Need VGA 800 @ 24bpp, @ 32bpp */
2240         /* Need VGA 1024 @ 24bpp, @ 32bpp */
2241 };
2242
2243 /*
2244  * Controller configurations for various panels.
2245  */
2246
2247 struct panel_settings
2248 {
2249         const char name[25];            /* Full name <vendor>_<model> */
2250
2251         struct  fb_monspecs monspecs;   /* FB monitor specs */
2252
2253         /* panel timings */
2254         uint32 mode_screen;
2255         uint32 mode_horztiming;
2256         uint32 mode_verttiming;
2257         uint32 mode_clkcontrol;
2258         uint32 mode_pwmdiv;
2259         uint32 mode_pwmhi;
2260         uint32 mode_outmask;
2261         uint32 mode_fifoctrl;
2262         uint32 mode_toyclksrc;
2263         uint32 mode_backlight;
2264         uint32 mode_auxpll;
2265         int (*device_init)(void);
2266         int (*device_shutdown)(void);
2267 #define Xres min_xres
2268 #define Yres min_yres
2269         u32     min_xres;               /* Minimum horizontal resolution */
2270         u32     max_xres;               /* Maximum horizontal resolution */
2271         u32     min_yres;               /* Minimum vertical resolution */
2272         u32     max_yres;               /* Maximum vertical resolution */
2273 };
2274
2275 /********************************************************************/
2276 /* fixme: Maybe a modedb for the CRT ? otherwise panels should be as-is */
2277
2278 /* List of panels known to work with the AU1200 LCD controller.
2279  * To add a new panel, enter the same specifications as the
2280  * Generic_TFT one, and MAKE SURE that it doesn't conflicts
2281  * with the controller restrictions. Restrictions are:
2282  *
2283  * STN color panels: max_bpp <= 12
2284  * STN mono panels: max_bpp <= 4
2285  * TFT panels: max_bpp <= 16
2286  * max_xres <= 800
2287  * max_yres <= 600
2288  */
2289 static struct panel_settings known_lcd_panels[] =
2290 {
2291         [0] = { /* QVGA 320x240 H:33.3kHz V:110Hz */
2292                 .name = "QVGA_320x240",
2293                 .monspecs = {
2294                         .modedb = NULL,
2295                         .modedb_len = 0,
2296                         .hfmin = 30000,
2297                         .hfmax = 70000,
2298                         .vfmin = 60,
2299                         .vfmax = 60,
2300                         .dclkmin = 6000000,
2301                         .dclkmax = 28000000,
2302                         .input = FB_DISP_RGB,
2303                 },
2304                 .mode_screen            = LCD_SCREEN_SX_N(320) |
2305                         LCD_SCREEN_SY_N(240),
2306                 .mode_horztiming        = 0x00c4623b,
2307                 .mode_verttiming        = 0x00502814,
2308                 .mode_clkcontrol        = 0x00020002, /* /4=24Mhz */
2309                 .mode_pwmdiv            = 0x00000000,
2310                 .mode_pwmhi             = 0x00000000,
2311                 .mode_outmask   = 0x00FFFFFF,
2312                 .mode_fifoctrl  = 0x2f2f2f2f,
2313                 .mode_toyclksrc = 0x00000004, /* AUXPLL directly */
2314                 .mode_backlight = 0x00000000,
2315                 .mode_auxpll            = 8, /* 96MHz AUXPLL */
2316                 .device_init            = NULL,
2317                 .device_shutdown        = NULL,
2318                 320, 320,
2319                 240, 240,
2320         },
2321
2322         [1] = { /* VGA 640x480 H:30.3kHz V:58Hz */
2323                 .name = "VGA_640x480",
2324                 .monspecs = {
2325                         .modedb = NULL,
2326                         .modedb_len = 0,
2327                         .hfmin = 30000,
2328                         .hfmax = 70000,
2329                         .vfmin = 60,
2330                         .vfmax = 60,
2331                         .dclkmin = 6000000,
2332                         .dclkmax = 28000000,
2333                         .input = FB_DISP_RGB,
2334                 },
2335                 .mode_screen            = 0x13f9df80,
2336                 .mode_horztiming        = 0x003c5859,
2337                 .mode_verttiming        = 0x00741201,
2338                 .mode_clkcontrol        = 0x00020001, /* /4=24Mhz */
2339                 .mode_pwmdiv            = 0x00000000,
2340                 .mode_pwmhi             = 0x00000000,
2341                 .mode_outmask   = 0x00FFFFFF,
2342                 .mode_fifoctrl  = 0x2f2f2f2f,
2343                 .mode_toyclksrc = 0x00000004, /* AUXPLL directly */
2344                 .mode_backlight = 0x00000000,
2345                 .mode_auxpll            = 8, /* 96MHz AUXPLL */
2346                 .device_init            = NULL,
2347                 .device_shutdown        = NULL,
2348                 640, 480,
2349                 640, 480,
2350         },
2351
2352         [2] = { /* SVGA 800x600 H:46.1kHz V:69Hz */
2353                 .name = "SVGA_800x600",
2354                 .monspecs = {
2355                         .modedb = NULL,
2356                         .modedb_len = 0,
2357                         .hfmin = 30000,
2358                         .hfmax = 70000,
2359                         .vfmin = 60,
2360                         .vfmax = 60,
2361                         .dclkmin = 6000000,
2362                         .dclkmax = 28000000,
2363                         .input = FB_DISP_RGB,
2364                 },
2365                 .mode_screen            = 0x18fa5780,
2366                 .mode_horztiming        = 0x00dc7e77,
2367                 .mode_verttiming        = 0x00584805,
2368                 .mode_clkcontrol        = 0x00020000, /* /2=48Mhz */
2369                 .mode_pwmdiv            = 0x00000000,
2370                 .mode_pwmhi             = 0x00000000,
2371                 .mode_outmask   = 0x00FFFFFF,
2372                 .mode_fifoctrl  = 0x2f2f2f2f,
2373                 .mode_toyclksrc = 0x00000004, /* AUXPLL directly */
2374                 .mode_backlight = 0x00000000,
2375                 .mode_auxpll            = 8, /* 96MHz AUXPLL */
2376                 .device_init            = NULL,
2377                 .device_shutdown        = NULL,
2378                 800, 800,
2379                 600, 600,
2380         },
2381
2382         [3] = { /* XVGA 1024x768 H:56.2kHz V:70Hz */
2383                 .name = "XVGA_1024x768",
2384                 .monspecs = {
2385                         .modedb = NULL,
2386                         .modedb_len = 0,
2387                         .hfmin = 30000,
2388                         .hfmax = 70000,
2389                         .vfmin = 60,
2390                         .vfmax = 60,
2391                         .dclkmin = 6000000,
2392                         .dclkmax = 28000000,
2393                         .input = FB_DISP_RGB,
2394                 },
2395                 .mode_screen            = 0x1ffaff80,
2396                 .mode_horztiming        = 0x007d0e57,
2397                 .mode_verttiming        = 0x00740a01,
2398                 .mode_clkcontrol        = 0x000A0000, /* /1 */
2399                 .mode_pwmdiv            = 0x00000000,
2400                 .mode_pwmhi             = 0x00000000,
2401                 .mode_outmask   = 0x00FFFFFF,
2402                 .mode_fifoctrl  = 0x2f2f2f2f,
2403                 .mode_toyclksrc = 0x00000004, /* AUXPLL directly */
2404                 .mode_backlight = 0x00000000,
2405                 .mode_auxpll            = 6, /* 72MHz AUXPLL */
2406                 .device_init            = NULL,
2407                 .device_shutdown        = NULL,
2408                 1024, 1024,
2409                 768, 768,
2410         },
2411
2412         [4] = { /* XVGA XVGA 1280x1024 H:68.5kHz V:65Hz */
2413                 .name = "XVGA_1280x1024",
2414                 .monspecs = {
2415                         .modedb = NULL,
2416                         .modedb_len = 0,
2417                         .hfmin = 30000,
2418                         .hfmax = 70000,
2419                         .vfmin = 60,
2420                         .vfmax = 60,
2421                         .dclkmin = 6000000,
2422                         .dclkmax = 28000000,
2423                         .input = FB_DISP_RGB,
2424                 },
2425                 .mode_screen            = 0x27fbff80,
2426                 .mode_horztiming        = 0x00cdb2c7,
2427                 .mode_verttiming        = 0x00600002,
2428                 .mode_clkcontrol        = 0x000A0000, /* /1 */
2429                 .mode_pwmdiv            = 0x00000000,
2430                 .mode_pwmhi             = 0x00000000,
2431                 .mode_outmask   = 0x00FFFFFF,
2432                 .mode_fifoctrl  = 0x2f2f2f2f,
2433                 .mode_toyclksrc = 0x00000004, /* AUXPLL directly */
2434                 .mode_backlight = 0x00000000,
2435                 .mode_auxpll            = 10, /* 120MHz AUXPLL */
2436                 .device_init            = NULL,
2437                 .device_shutdown        = NULL,
2438                 1280, 1280,
2439                 1024, 1024,
2440         },
2441
2442         [5] = { /* Samsung 1024x768 TFT */
2443                 .name = "Samsung_1024x768_TFT",
2444                 .monspecs = {
2445                         .modedb = NULL,
2446                         .modedb_len = 0,
2447                         .hfmin = 30000,
2448                         .hfmax = 70000,
2449                         .vfmin = 60,
2450                         .vfmax = 60,
2451                         .dclkmin = 6000000,
2452                         .dclkmax = 28000000,
2453                         .input = FB_DISP_RGB,
2454                 },
2455                 .mode_screen            = 0x1ffaff80,
2456                 .mode_horztiming        = 0x018cc677,
2457                 .mode_verttiming        = 0x00241217,
2458                 .mode_clkcontrol        = 0x00000000, /* SCB 0x1 /4=24Mhz */
2459                 .mode_pwmdiv            = 0x8000063f, /* SCB 0x0 */
2460                 .mode_pwmhi             = 0x03400000, /* SCB 0x0 */
2461                 .mode_outmask   = 0x00FFFFFF,
2462                 .mode_fifoctrl  = 0x2f2f2f2f,
2463                 .mode_toyclksrc = 0x00000004, /* AUXPLL directly */
2464                 .mode_backlight = 0x00000000,
2465                 .mode_auxpll            = 8, /* 96MHz AUXPLL */
2466                 .device_init            = board_au1200fb_panel_init,
2467                 .device_shutdown        = board_au1200fb_panel_shutdown,
2468                 1024, 1024,
2469                 768, 768,
2470         },
2471
2472         [6] = { /* Toshiba 640x480 TFT */
2473                 .name = "Toshiba_640x480_TFT",
2474                 .monspecs = {
2475                         .modedb = NULL,
2476                         .modedb_len = 0,
2477                         .hfmin = 30000,
2478                         .hfmax = 70000,
2479                         .vfmin = 60,
2480                         .vfmax = 60,
2481                         .dclkmin = 6000000,
2482                         .dclkmax = 28000000,
2483                         .input = FB_DISP_RGB,
2484                 },
2485                 .mode_screen            = LCD_SCREEN_SX_N(640) |
2486                         LCD_SCREEN_SY_N(480),
2487                 .mode_horztiming        = LCD_HORZTIMING_HPW_N(96) |
2488                         LCD_HORZTIMING_HND1_N(13) | LCD_HORZTIMING_HND2_N(51),
2489                 .mode_verttiming        = LCD_VERTTIMING_VPW_N(2) |
2490                         LCD_VERTTIMING_VND1_N(11) | LCD_VERTTIMING_VND2_N(32),
2491                 .mode_clkcontrol        = 0x00000000, /* /4=24Mhz */
2492                 .mode_pwmdiv            = 0x8000063f,
2493                 .mode_pwmhi             = 0x03400000,
2494                 .mode_outmask   = 0x00fcfcfc,
2495                 .mode_fifoctrl  = 0x2f2f2f2f,
2496                 .mode_toyclksrc = 0x00000004, /* AUXPLL directly */
2497                 .mode_backlight = 0x00000000,
2498                 .mode_auxpll            = 8, /* 96MHz AUXPLL */
2499                 .device_init            = board_au1200fb_panel_init,
2500                 .device_shutdown        = board_au1200fb_panel_shutdown,
2501                 640, 480,
2502                 640, 480,
2503         },
2504
2505         [7] = { /* Sharp 320x240 TFT */
2506                 .name = "Sharp_320x240_TFT",
2507                 .monspecs = {
2508                         .modedb = NULL,
2509                         .modedb_len = 0,
2510                         .hfmin = 12500,
2511                         .hfmax = 20000,
2512                         .vfmin = 38,
2513                         .vfmax = 81,
2514                         .dclkmin = 4500000,
2515                         .dclkmax = 6800000,
2516                         .input = FB_DISP_RGB,
2517                 },
2518                 .mode_screen            = LCD_SCREEN_SX_N(320) |
2519                         LCD_SCREEN_SY_N(240),
2520                 .mode_horztiming        = LCD_HORZTIMING_HPW_N(60) |
2521                         LCD_HORZTIMING_HND1_N(13) | LCD_HORZTIMING_HND2_N(2),
2522                 .mode_verttiming        = LCD_VERTTIMING_VPW_N(2) |
2523                         LCD_VERTTIMING_VND1_N(2) | LCD_VERTTIMING_VND2_N(5),
2524                 .mode_clkcontrol        = LCD_CLKCONTROL_PCD_N(7), /*16=6Mhz*/
2525                 .mode_pwmdiv            = 0x8000063f,
2526                 .mode_pwmhi             = 0x03400000,
2527                 .mode_outmask   = 0x00fcfcfc,
2528                 .mode_fifoctrl  = 0x2f2f2f2f,
2529                 .mode_toyclksrc = 0x00000004, /* AUXPLL directly */
2530                 .mode_backlight = 0x00000000,
2531                 .mode_auxpll            = 8, /* 96MHz AUXPLL */
2532                 .device_init            = board_au1200fb_panel_init,
2533                 .device_shutdown        = board_au1200fb_panel_shutdown,
2534                 320, 320,
2535                 240, 240,
2536         },
2537
2538         [8] = { /* Toppoly TD070WGCB2 7" 856x480 TFT */
2539                 .name = "Toppoly_TD070WGCB2",
2540                 .monspecs = {
2541                         .modedb = NULL,
2542                         .modedb_len = 0,
2543                         .hfmin = 30000,
2544                         .hfmax = 70000,
2545                         .vfmin = 60,
2546                         .vfmax = 60,
2547                         .dclkmin = 6000000,
2548                         .dclkmax = 28000000,
2549                         .input = FB_DISP_RGB,
2550                 },
2551                 .mode_screen            = LCD_SCREEN_SX_N(856) |
2552                         LCD_SCREEN_SY_N(480),
2553                 .mode_horztiming        = LCD_HORZTIMING_HND2_N(43) |
2554                         LCD_HORZTIMING_HND1_N(43) | LCD_HORZTIMING_HPW_N(114),
2555                 .mode_verttiming        = LCD_VERTTIMING_VND2_N(20) |
2556                         LCD_VERTTIMING_VND1_N(21) | LCD_VERTTIMING_VPW_N(4),
2557                 .mode_clkcontrol        = 0x00020001, /* /4=24Mhz */
2558                 .mode_pwmdiv            = 0x8000063f,
2559                 .mode_pwmhi             = 0x03400000,
2560                 .mode_outmask   = 0x00fcfcfc,
2561                 .mode_fifoctrl  = 0x2f2f2f2f,
2562                 .mode_toyclksrc = 0x00000004, /* AUXPLL directly */
2563                 .mode_backlight = 0x00000000,
2564                 .mode_auxpll            = 8, /* 96MHz AUXPLL */
2565                 .device_init            = board_au1200fb_panel_init,
2566                 .device_shutdown        = board_au1200fb_panel_shutdown,
2567                 856, 856,
2568                 480, 480,
2569         },
2570 };
2571
2572 #define NUM_PANELS (ARRAY_SIZE(known_lcd_panels))
2573
2574 /********************************************************************/
2575
2576 #ifdef CONFIG_PM
2577 static int set_brightness(unsigned int brightness)
2578 {
2579         unsigned int hi1, divider;
2580
2581         /* limit brightness pwm duty to >= 30/1600 */
2582         if (brightness < 30) {
2583                 brightness = 30;
2584         }
2585         divider = (lcd->pwmdiv & 0x3FFFF) + 1;
2586         hi1 = (lcd->pwmhi >> 16) + 1;
2587         hi1 = (((brightness & 0xFF) + 1) * divider >> 8);
2588         lcd->pwmhi &= 0xFFFF;
2589         lcd->pwmhi |= (hi1 << 16);
2590
2591         return brightness;
2592 }
2593 #endif /* CONFIG_PM */
2594
2595 static int winbpp (unsigned int winctrl1)
2596 {
2597         int bits = 0;
2598
2599         /* how many bits are needed for each pixel format */
2600         switch (winctrl1 & LCD_WINCTRL1_FRM) {
2601         case LCD_WINCTRL1_FRM_1BPP:
2602                 bits = 1;
2603                 break;
2604         case LCD_WINCTRL1_FRM_2BPP:
2605                 bits = 2;
2606                 break;
2607         case LCD_WINCTRL1_FRM_4BPP:
2608                 bits = 4;
2609                 break;
2610         case LCD_WINCTRL1_FRM_8BPP:
2611                 bits = 8;
2612                 break;
2613         case LCD_WINCTRL1_FRM_12BPP:
2614         case LCD_WINCTRL1_FRM_16BPP655:
2615         case LCD_WINCTRL1_FRM_16BPP565:
2616         case LCD_WINCTRL1_FRM_16BPP556:
2617         case LCD_WINCTRL1_FRM_16BPPI1555:
2618         case LCD_WINCTRL1_FRM_16BPPI5551:
2619         case LCD_WINCTRL1_FRM_16BPPA1555:
2620         case LCD_WINCTRL1_FRM_16BPPA5551:
2621                 bits = 16;
2622                 break;
2623         case LCD_WINCTRL1_FRM_24BPP:
2624         case LCD_WINCTRL1_FRM_32BPP:
2625                 bits = 32;
2626                 break;
2627         }
2628
2629         return bits;
2630 }
2631
2632 static int fbinfo2index (struct fb_info *fb_info)
2633 {
2634         int i;
2635
2636         for (i = 0; i < CONFIG_FB_AU1200_DEVS; ++i) {
2637                 if (fb_info == (struct fb_info *)(&_au1200fb_devices[i].fb_info))
2638                         return i;
2639         }
2640         printk("au1200fb: ERROR: fbinfo2index failed!\n");
2641         return -1;
2642 }
2643
2644 static int au1200_setlocation (struct au1200fb_device *fbdev, int plane,
2645         int xpos, int ypos)
2646 {
2647         uint32 winctrl0, winctrl1, winenable, fb_offset = 0;
2648         int xsz, ysz;
2649
2650         /* FIX!!! NOT CHECKING FOR COMPLETE OFFSCREEN YET */
2651
2652         winctrl0 = lcd->window[plane].winctrl0;
2653         winctrl1 = lcd->window[plane].winctrl1;
2654         winctrl0 &= (LCD_WINCTRL0_A | LCD_WINCTRL0_AEN);
2655         winctrl1 &= ~(LCD_WINCTRL1_SZX | LCD_WINCTRL1_SZY);
2656
2657         /* Check for off-screen adjustments */
2658         xsz = win->w[plane].xres;
2659         ysz = win->w[plane].yres;
2660         if ((xpos + win->w[plane].xres) > panel->Xres) {
2661                 /* Off-screen to the right */
2662                 xsz = panel->Xres - xpos; /* off by 1 ??? */
2663                 /*printk("off screen right\n");*/
2664         }
2665
2666         if ((ypos + win->w[plane].yres) > panel->Yres) {
2667                 /* Off-screen to the bottom */
2668                 ysz = panel->Yres - ypos; /* off by 1 ??? */
2669                 /*printk("off screen bottom\n");*/
2670         }
2671
2672         if (xpos < 0) {
2673                 /* Off-screen to the left */
2674                 xsz = win->w[plane].xres + xpos;
2675                 fb_offset += (((0 - xpos) * winbpp(lcd->window[plane].winctrl1))/8);
2676                 xpos = 0;
2677                 /*printk("off screen left\n");*/
2678         }
2679
2680         if (ypos < 0) {
2681                 /* Off-screen to the top */
2682                 ysz = win->w[plane].yres + ypos;
2683                 /* fixme: fb_offset += ((0-ypos)*fb_pars[plane].line_length); */
2684                 ypos = 0;
2685                 /*printk("off screen top\n");*/
2686         }
2687
2688         /* record settings */
2689         win->w[plane].xpos = xpos;
2690         win->w[plane].ypos = ypos;
2691
2692         xsz -= 1;
2693         ysz -= 1;
2694         winctrl0 |= (xpos << 21);
2695         winctrl0 |= (ypos << 10);
2696         winctrl1 |= (xsz << 11);
2697         winctrl1 |= (ysz << 0);
2698
2699         /* Disable the window while making changes, then restore WINEN */
2700         winenable = lcd->winenable & (1 << plane);
2701         au_sync();
2702         lcd->winenable &= ~(1 << plane);
2703         lcd->window[plane].winctrl0 = winctrl0;
2704         lcd->window[plane].winctrl1 = winctrl1;
2705         lcd->window[plane].winbuf0 =
2706         lcd->window[plane].winbuf1 = fbdev->fb_phys;
2707         lcd->window[plane].winbufctrl = 0; /* select winbuf0 */
2708         lcd->winenable |= winenable;
2709         au_sync();
2710
2711         return 0;
2712 }
2713
2714 static void au1200_setpanel (struct panel_settings *newpanel)
2715 {
2716         /*
2717          * Perform global setup/init of LCD controller
2718          */
2719         uint32 winenable;
2720
2721         /* Make sure all windows disabled */
2722         winenable = lcd->winenable;
2723         lcd->winenable = 0;
2724         au_sync();
2725         /*
2726          * Ensure everything is disabled before reconfiguring
2727          */
2728         if (lcd->screen & LCD_SCREEN_SEN) {
2729                 /* Wait for vertical sync period */
2730                 lcd->intstatus = LCD_INT_SS;
2731                 while ((lcd->intstatus & LCD_INT_SS) == 0) {
2732                         au_sync();
2733                 }
2734
2735                 lcd->screen &= ~LCD_SCREEN_SEN; /*disable the controller*/
2736
2737                 do {
2738                         lcd->intstatus = lcd->intstatus; /*clear interrupts*/
2739                         au_sync();
2740                 /*wait for controller to shut down*/
2741                 } while ((lcd->intstatus & LCD_INT_SD) == 0);
2742
2743                 /* Call shutdown of current panel (if up) */
2744                 /* this must occur last, because if an external clock is driving
2745                     the controller, the clock cannot be turned off before first
2746                         shutting down the controller.
2747                  */
2748                 if (panel->device_shutdown != NULL)
2749                         panel->device_shutdown();
2750         }
2751
2752         /* Newpanel == NULL indicates a shutdown operation only */
2753         if (newpanel == NULL)
2754                 return;
2755
2756         panel = newpanel;
2757
2758         printk("Panel(%s), %dx%d\n", panel->name, panel->Xres, panel->Yres);
2759
2760         /*
2761          * Setup clocking if internal LCD clock source (assumes sys_auxpll valid)
2762          */
2763         if (!(panel->mode_clkcontrol & LCD_CLKCONTROL_EXT))
2764         {
2765                 uint32 sys_clksrc;
2766                 au_writel(panel->mode_auxpll, SYS_AUXPLL);
2767                 sys_clksrc = au_readl(SYS_CLKSRC) & ~0x0000001f;
2768                 sys_clksrc |= panel->mode_toyclksrc;
2769                 au_writel(sys_clksrc, SYS_CLKSRC);
2770         }
2771
2772         /*
2773          * Configure panel timings
2774          */
2775         lcd->screen = panel->mode_screen;
2776         lcd->horztiming = panel->mode_horztiming;
2777         lcd->verttiming = panel->mode_verttiming;
2778         lcd->clkcontrol = panel->mode_clkcontrol;
2779         lcd->pwmdiv = panel->mode_pwmdiv;
2780         lcd->pwmhi = panel->mode_pwmhi;
2781         lcd->outmask = panel->mode_outmask;
2782         lcd->fifoctrl = panel->mode_fifoctrl;
2783         au_sync();
2784
2785         /* fixme: Check window settings to make sure still valid
2786          * for new geometry */
2787 #if 0
2788         au1200_setlocation(fbdev, 0, win->w[0].xpos, win->w[0].ypos);
2789         au1200_setlocation(fbdev, 1, win->w[1].xpos, win->w[1].ypos);
2790         au1200_setlocation(fbdev, 2, win->w[2].xpos, win->w[2].ypos);
2791         au1200_setlocation(fbdev, 3, win->w[3].xpos, win->w[3].ypos);
2792 #endif
2793         lcd->winenable = winenable;
2794
2795         /*
2796          * Re-enable screen now that it is configured
2797          */
2798         lcd->screen |= LCD_SCREEN_SEN;
2799         au_sync();
2800
2801         /* Call init of panel */
2802         if (panel->device_init != NULL) panel->device_init();
2803
2804         /* FIX!!!! not appropriate on panel change!!! Global setup/init */
2805         lcd->intenable = 0;
2806         lcd->intstatus = ~0;
2807         lcd->backcolor = win->mode_backcolor;
2808
2809         /* Setup Color Key - FIX!!! */
2810         lcd->colorkey = win->mode_colorkey;
2811         lcd->colorkeymsk = win->mode_colorkeymsk;
2812
2813         /* Setup HWCursor - FIX!!! Need to support this eventually */
2814         lcd->hwc.cursorctrl = 0;
2815         lcd->hwc.cursorpos = 0;
2816         lcd->hwc.cursorcolor0 = 0;
2817         lcd->hwc.cursorcolor1 = 0;
2818         lcd->hwc.cursorcolor2 = 0;
2819         lcd->hwc.cursorcolor3 = 0;
2820
2821
2822 #if 0
2823 #define D(X) printk("%25s: %08X\n", #X, X)
2824         D(lcd->screen);
2825         D(lcd->horztiming);
2826         D(lcd->verttiming);
2827         D(lcd->clkcontrol);
2828         D(lcd->pwmdiv);
2829         D(lcd->pwmhi);
2830         D(lcd->outmask);
2831         D(lcd->fifoctrl);
2832         D(lcd->window[0].winctrl0);
2833         D(lcd->window[0].winctrl1);
2834         D(lcd->window[0].winctrl2);
2835         D(lcd->window[0].winbuf0);
2836         D(lcd->window[0].winbuf1);
2837         D(lcd->window[0].winbufctrl);
2838         D(lcd->window[1].winctrl0);
2839         D(lcd->window[1].winctrl1);
2840         D(lcd->window[1].winctrl2);
2841         D(lcd->window[1].winbuf0);
2842         D(lcd->window[1].winbuf1);
2843         D(lcd->window[1].winbufctrl);
2844         D(lcd->window[2].winctrl0);
2845         D(lcd->window[2].winctrl1);
2846         D(lcd->window[2].winctrl2);
2847         D(lcd->window[2].winbuf0);
2848         D(lcd->window[2].winbuf1);
2849         D(lcd->window[2].winbufctrl);
2850         D(lcd->window[3].winctrl0);
2851         D(lcd->window[3].winctrl1);
2852         D(lcd->window[3].winctrl2);
2853         D(lcd->window[3].winbuf0);
2854         D(lcd->window[3].winbuf1);
2855         D(lcd->window[3].winbufctrl);
2856         D(lcd->winenable);
2857         D(lcd->intenable);
2858         D(lcd->intstatus);
2859         D(lcd->backcolor);
2860         D(lcd->winenable);
2861         D(lcd->colorkey);
2862     D(lcd->colorkeymsk);
2863         D(lcd->hwc.cursorctrl);
2864         D(lcd->hwc.cursorpos);
2865         D(lcd->hwc.cursorcolor0);
2866         D(lcd->hwc.cursorcolor1);
2867         D(lcd->hwc.cursorcolor2);
2868         D(lcd->hwc.cursorcolor3);
2869 #endif
2870 }
2871
2872 static void au1200_setmode(struct au1200fb_device *fbdev)
2873 {
2874         int plane = fbdev->plane;
2875         /* Window/plane setup */
2876         lcd->window[plane].winctrl1 = ( 0
2877                 | LCD_WINCTRL1_PRI_N(plane)
2878                 | win->w[plane].mode_winctrl1 /* FRM,CCO,PO,PIPE */
2879                 ) ;
2880
2881         au1200_setlocation(fbdev, plane, win->w[plane].xpos, win->w[plane].ypos);
2882
2883         lcd->window[plane].winctrl2 = ( 0
2884                 | LCD_WINCTRL2_CKMODE_00
2885                 | LCD_WINCTRL2_DBM
2886                 | LCD_WINCTRL2_BX_N( fbdev->fb_info.fix.line_length)
2887                 | LCD_WINCTRL2_SCX_1
2888                 | LCD_WINCTRL2_SCY_1
2889                 ) ;
2890         lcd->winenable |= win->w[plane].mode_winenable;
2891         au_sync();
2892 }
2893
2894
2895 /* Inline helpers */
2896
2897 /*#define panel_is_dual(panel)  ((panel->mode_screen & LCD_SCREEN_PT) == LCD_SCREEN_PT_010)*/
2898 /*#define panel_is_active(panel)((panel->mode_screen & LCD_SCREEN_PT) == LCD_SCREEN_PT_010)*/
2899
2900 #define panel_is_color(panel) ((panel->mode_screen & LCD_SCREEN_PT) <= LCD_SCREEN_PT_CDSTN)
2901
2902 /* Bitfields format supported by the controller. */
2903 static struct fb_bitfield rgb_bitfields[][4] = {
2904         /*     Red,        Green,        Blue,       Transp   */
2905         [LCD_WINCTRL1_FRM_16BPP655 >> 25] =
2906                 { { 10, 6, 0 }, { 5, 5, 0 }, { 0, 5, 0 }, { 0, 0, 0 } },
2907
2908         [LCD_WINCTRL1_FRM_16BPP565 >> 25] =
2909                 { { 11, 5, 0 }, { 5, 6, 0 }, { 0, 5, 0 }, { 0, 0, 0 } },
2910
2911         [LCD_WINCTRL1_FRM_16BPP556 >> 25] =
2912                 { { 11, 5, 0 }, { 6, 5, 0 }, { 0, 6, 0 }, { 0, 0, 0 } },
2913
2914         [LCD_WINCTRL1_FRM_16BPPI1555 >> 25] =
2915                 { { 10, 5, 0 }, { 5, 5, 0 }, { 0, 5, 0 }, { 0, 0, 0 } },
2916
2917         [LCD_WINCTRL1_FRM_16BPPI5551 >> 25] =
2918                 { { 11, 5, 0 }, { 6, 5, 0 }, { 1, 5, 0 }, { 0, 0, 0 } },
2919
2920         [LCD_WINCTRL1_FRM_16BPPA1555 >> 25] =
2921                 { { 10, 5, 0 }, { 5, 5, 0 }, { 0, 5, 0 }, { 15, 1, 0 } },
2922
2923         [LCD_WINCTRL1_FRM_16BPPA5551 >> 25] =
2924                 { { 11, 5, 0 }, { 6, 5, 0 }, { 1, 5, 0 }, { 0, 1, 0 } },
2925
2926         [LCD_WINCTRL1_FRM_24BPP >> 25] =
2927                 { { 16, 8, 0 }, { 8, 8, 0 }, { 0, 8, 0 }, { 0, 0, 0 } },
2928
2929         [LCD_WINCTRL1_FRM_32BPP >> 25] =
2930                 { { 16, 8, 0 }, { 8, 8, 0 }, { 0, 8, 0 }, { 24, 0, 0 } },
2931 };
2932
2933 /*-------------------------------------------------------------------------*/
2934
2935 /* Helpers */
2936
2937 static void au1200fb_update_fbinfo(struct fb_info *fbi)
2938 {
2939         /* FIX!!!! This also needs to take the window pixel format into account!!! */
2940
2941         /* Update var-dependent FB info */
2942         if (panel_is_color(panel)) {
2943                 if (fbi->var.bits_per_pixel <= 8) {
2944                         /* palettized */
2945                         fbi->fix.visual = FB_VISUAL_PSEUDOCOLOR;
2946                         fbi->fix.line_length = fbi->var.xres_virtual /
2947                                 (8/fbi->var.bits_per_pixel);
2948                 } else {
2949                         /* non-palettized */
2950                         fbi->fix.visual = FB_VISUAL_TRUECOLOR;
2951                         fbi->fix.line_length = fbi->var.xres_virtual * (fbi->var.bits_per_pixel / 8);
2952                 }
2953         } else {
2954                 /* mono FIX!!! mono 8 and 4 bits */
2955                 fbi->fix.visual = FB_VISUAL_MONO10;
2956                 fbi->fix.line_length = fbi->var.xres_virtual / 8;
2957         }
2958
2959         fbi->screen_size = fbi->fix.line_length * fbi->var.yres_virtual;
2960         print_dbg("line length: %d\n", fbi->fix.line_length);
2961         print_dbg("bits_per_pixel: %d\n", fbi->var.bits_per_pixel);
2962 }
2963
2964 /*-------------------------------------------------------------------------*/
2965
2966 /* AU1200 framebuffer driver */
2967
2968 /* fb_check_var
2969  * Validate var settings with hardware restrictions and modify it if necessary
2970  */
2971 static int au1200fb_fb_check_var(struct fb_var_screeninfo *var,
2972         struct fb_info *fbi)
2973 {
2974         struct au1200fb_device *fbdev = (struct au1200fb_device *)fbi;
2975         u32 pixclock;
2976         int screen_size, plane;
2977
2978         plane = fbdev->plane;
2979
2980         /* Make sure that the mode respect all LCD controller and
2981          * panel restrictions. */
2982         var->xres = win->w[plane].xres;
2983         var->yres = win->w[plane].yres;
2984
2985         /* No need for virtual resolution support */
2986         var->xres_virtual = var->xres;
2987         var->yres_virtual = var->yres;
2988
2989         var->bits_per_pixel = winbpp(win->w[plane].mode_winctrl1);
2990
2991         screen_size = var->xres_virtual * var->yres_virtual;
2992         if (var->bits_per_pixel > 8) screen_size *= (var->bits_per_pixel / 8);
2993         else screen_size /= (8/var->bits_per_pixel);
2994
2995         if (fbdev->fb_len < screen_size)
2996                 return -EINVAL; /* Virtual screen is to big, abort */
2997
2998         /* FIX!!!! what are the implicaitons of ignoring this for windows ??? */
2999         /* The max LCD clock is fixed to 48MHz (value of AUX_CLK). The pixel
3000          * clock can only be obtain by dividing this value by an even integer.
3001          * Fallback to a slower pixel clock if necessary. */
3002         pixclock = max((u32)(PICOS2KHZ(var->pixclock) * 1000), fbi->monspecs.dclkmin);
3003         pixclock = min(pixclock, min(fbi->monspecs.dclkmax, (u32)AU1200_LCD_MAX_CLK/2));
3004
3005         if (AU1200_LCD_MAX_CLK % pixclock) {
3006                 int diff = AU1200_LCD_MAX_CLK % pixclock;
3007                 pixclock -= diff;
3008         }
3009
3010         var->pixclock = KHZ2PICOS(pixclock/1000);
3011 #if 0
3012         if (!panel_is_active(panel)) {
3013                 int pcd = AU1200_LCD_MAX_CLK / (pixclock * 2) - 1;
3014
3015                 if (!panel_is_color(panel)
3016                         && (panel->control_base & LCD_CONTROL_MPI) && (pcd < 3)) {
3017                         /* STN 8bit mono panel support is up to 6MHz pixclock */
3018                         var->pixclock = KHZ2PICOS(6000);
3019                 } else if (!pcd) {
3020                         /* Other STN panel support is up to 12MHz  */
3021                         var->pixclock = KHZ2PICOS(12000);
3022                 }
3023         }
3024 #endif
3025         /* Set bitfield accordingly */
3026         switch (var->bits_per_pixel) {
3027                 case 16:
3028                 {
3029                         /* 16bpp True color.
3030                          * These must be set to MATCH WINCTRL[FORM] */
3031                         int idx;
3032                         idx = (win->w[0].mode_winctrl1 & LCD_WINCTRL1_FRM) >> 25;
3033                         var->red    = rgb_bitfields[idx][0];
3034                         var->green  = rgb_bitfields[idx][1];
3035                         var->blue   = rgb_bitfields[idx][2];
3036                         var->transp = rgb_bitfields[idx][3];
3037                         break;
3038                 }
3039
3040                 case 32:
3041                 {
3042                         /* 32bpp True color.
3043                          * These must be set to MATCH WINCTRL[FORM] */
3044                         int idx;
3045                         idx = (win->w[0].mode_winctrl1 & LCD_WINCTRL1_FRM) >> 25;
3046                         var->red    = rgb_bitfields[idx][0];
3047                         var->green  = rgb_bitfields[idx][1];
3048                         var->blue   = rgb_bitfields[idx][2];
3049                         var->transp = rgb_bitfields[idx][3];
3050                         break;
3051                 }
3052                 default:
3053                         print_dbg("Unsupported depth %dbpp", var->bits_per_pixel);
3054                         return -EINVAL;
3055         }
3056
3057         return 0;
3058 }
3059
3060 /* fb_set_par
3061  * Set hardware with var settings. This will enable the controller with a
3062  * specific mode, normally validated with the fb_check_var method
3063  */
3064 static int au1200fb_fb_set_par(struct fb_info *fbi)
3065 {
3066         struct au1200fb_device *fbdev = (struct au1200fb_device *)fbi;
3067
3068         au1200fb_update_fbinfo(fbi);
3069         au1200_setmode(fbdev);
3070
3071         return 0;
3072 }
3073
3074 /* fb_setcolreg
3075  * Set color in LCD palette.
3076  */
3077 static int au1200fb_fb_setcolreg(unsigned regno, unsigned red, unsigned green,
3078         unsigned blue, unsigned transp, struct fb_info *fbi)
3079 {
3080         volatile u32 *palette = lcd->palette;
3081         u32 value;
3082
3083         if (regno > (AU1200_LCD_NBR_PALETTE_ENTRIES - 1))
3084                 return -EINVAL;
3085
3086         if (fbi->var.grayscale) {
3087                 /* Convert color to grayscale */
3088                 red = green = blue =
3089                         (19595 * red + 38470 * green + 7471 * blue) >> 16;
3090         }
3091
3092         if (fbi->fix.visual == FB_VISUAL_TRUECOLOR) {
3093                 /* Place color in the pseudopalette */
3094                 if (regno > 16)
3095                         return -EINVAL;
3096
3097                 palette = (u32*) fbi->pseudo_palette;
3098
3099                 red   >>= (16 - fbi->var.red.length);
3100                 green >>= (16 - fbi->var.green.length);
3101                 blue  >>= (16 - fbi->var.blue.length);
3102
3103                 value = (red   << fbi->var.red.offset)  |
3104                         (green << fbi->var.green.offset)|
3105                         (blue  << fbi->var.blue.offset);
3106                 value &= 0xFFFF;
3107
3108         } else if (1 /*FIX!!! panel_is_active(fbdev->panel)*/) {
3109                 /* COLOR TFT PALLETTIZED (use RGB 565) */
3110                 value = (red & 0xF800)|((green >> 5) &
3111                                 0x07E0)|((blue >> 11) & 0x001F);
3112                 value &= 0xFFFF;
3113
3114         } else if (0 /*panel_is_color(fbdev->panel)*/) {
3115                 /* COLOR STN MODE */
3116                 value = 0x1234;
3117                 value &= 0xFFF;
3118         } else {
3119                 /* MONOCHROME MODE */
3120                 value = (green >> 12) & 0x000F;
3121                 value &= 0xF;
3122         }
3123
3124         palette[regno] = value;
3125
3126         return 0;
3127 }
3128
3129 /* fb_blank
3130  * Blank the screen. Depending on the mode, the screen will be
3131  * activated with the backlight color, or desactivated
3132  */
3133 static int au1200fb_fb_blank(int blank_mode, struct fb_info *fbi)
3134 {
3135         /* Short-circuit screen blanking */
3136         if (noblanking)
3137                 return 0;
3138
3139         switch (blank_mode) {
3140
3141         case FB_BLANK_UNBLANK:
3142         case FB_BLANK_NORMAL:
3143                 /* printk("turn on panel\n"); */
3144                 au1200_setpanel(panel);
3145                 break;
3146         case FB_BLANK_VSYNC_SUSPEND:
3147         case FB_BLANK_HSYNC_SUSPEND:
3148         case FB_BLANK_POWERDOWN:
3149                 /* printk("turn off panel\n"); */
3150                 au1200_setpanel(NULL);
3151                 break;
3152         default:
3153                 break;
3154
3155         }
3156
3157         /* FB_BLANK_NORMAL is a soft blank */
3158         return (blank_mode == FB_BLANK_NORMAL) ? -EINVAL : 0;
3159 }
3160
3161 /* fb_mmap
3162  * Map video memory in user space. We don't use the generic fb_mmap
3163  * method mainly to allow the use of the TLB streaming flag (CCA=6)
3164  */
3165 static int au1200fb_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
3166
3167 {
3168         unsigned int len;
3169         unsigned long start=0, off;
3170         struct au1200fb_device *fbdev = (struct au1200fb_device *) info;
3171
3172 #ifdef CONFIG_PM
3173         au1xxx_pm_access(LCD_pm_dev);
3174 #endif
3175
3176         if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT)) {
3177                 return -EINVAL;
3178         }
3179
3180         start = fbdev->fb_phys & PAGE_MASK;
3181         len = PAGE_ALIGN((start & ~PAGE_MASK) + fbdev->fb_len);
3182
3183         off = vma->vm_pgoff << PAGE_SHIFT;
3184
3185         if ((vma->vm_end - vma->vm_start + off) > len) {
3186                 return -EINVAL;
3187         }
3188
3189         off += start;
3190         vma->vm_pgoff = off >> PAGE_SHIFT;
3191
3192         vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
3193         pgprot_val(vma->vm_page_prot) |= _CACHE_MASK; /* CCA=7 */
3194
3195         vma->vm_flags |= VM_IO;
3196
3197         return io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
3198                                   vma->vm_end - vma->vm_start,
3199                                   vma->vm_page_prot);
3200
3201         return 0;
3202 }
3203
3204 static void set_global(u_int cmd, struct au1200_lcd_global_regs_t *pdata)
3205 {
3206
3207         unsigned int hi1, divider;
3208
3209         /* SCREEN_SIZE: user cannot reset size, must switch panel choice */
3210
3211         if (pdata->flags & SCREEN_BACKCOLOR)
3212                 lcd->backcolor = pdata->backcolor;
3213
3214         if (pdata->flags & SCREEN_BRIGHTNESS) {
3215
3216                 // limit brightness pwm duty to >= 30/1600
3217                 if (pdata->brightness < 30) {
3218                         pdata->brightness = 30;
3219                 }
3220                 divider = (lcd->pwmdiv & 0x3FFFF) + 1;
3221                 hi1 = (lcd->pwmhi >> 16) + 1;
3222                 hi1 = (((pdata->brightness & 0xFF)+1) * divider >> 8);
3223                 lcd->pwmhi &= 0xFFFF;
3224                 lcd->pwmhi |= (hi1 << 16);
3225         }
3226
3227         if (pdata->flags & SCREEN_COLORKEY)
3228                 lcd->colorkey = pdata->colorkey;
3229
3230         if (pdata->flags & SCREEN_MASK)
3231                 lcd->colorkeymsk = pdata->mask;
3232         au_sync();
3233 }
3234
3235 static void get_global(u_int cmd, struct au1200_lcd_global_regs_t *pdata)
3236 {
3237         unsigned int hi1, divider;
3238
3239         pdata->xsize = ((lcd->screen & LCD_SCREEN_SX) >> 19) + 1;
3240         pdata->ysize = ((lcd->screen & LCD_SCREEN_SY) >> 8) + 1;
3241
3242         pdata->backcolor = lcd->backcolor;
3243         pdata->colorkey = lcd->colorkey;
3244         pdata->mask = lcd->colorkeymsk;
3245
3246         // brightness
3247         hi1 = (lcd->pwmhi >> 16) + 1;
3248         divider = (lcd->pwmdiv & 0x3FFFF) + 1;
3249         pdata->brightness = ((hi1 << 8) / divider) - 1;
3250         au_sync();
3251 }
3252
3253 static void set_window(unsigned int plane,
3254         struct au1200_lcd_window_regs_t *pdata)
3255 {
3256         unsigned int val, bpp;
3257
3258         /* Window control register 0 */
3259         if (pdata->flags & WIN_POSITION) {
3260                 val = lcd->window[plane].winctrl0 & ~(LCD_WINCTRL0_OX |
3261                                 LCD_WINCTRL0_OY);
3262                 val |= ((pdata->xpos << 21) & LCD_WINCTRL0_OX);
3263                 val |= ((pdata->ypos << 10) & LCD_WINCTRL0_OY);
3264                 lcd->window[plane].winctrl0 = val;
3265         }
3266         if (pdata->flags & WIN_ALPHA_COLOR) {
3267                 val = lcd->window[plane].winctrl0 & ~(LCD_WINCTRL0_A);
3268                 val |= ((pdata->alpha_color << 2) & LCD_WINCTRL0_A);
3269                 lcd->window[plane].winctrl0 = val;
3270         }
3271         if (pdata->flags & WIN_ALPHA_MODE) {
3272                 val = lcd->window[plane].winctrl0 & ~(LCD_WINCTRL0_AEN);
3273                 val |= ((pdata->alpha_mode << 1) & LCD_WINCTRL0_AEN);
3274                 lcd->window[plane].winctrl0 = val;
3275         }
3276
3277         /* Window control register 1 */
3278         if (pdata->flags & WIN_PRIORITY) {
3279                 val = lcd->window[plane].winctrl1 & ~(LCD_WINCTRL1_PRI);
3280                 val |= ((pdata->priority << 30) & LCD_WINCTRL1_PRI);
3281                 lcd->window[plane].winctrl1 = val;
3282         }
3283         if (pdata->flags & WIN_CHANNEL) {
3284                 val = lcd->window[plane].winctrl1 & ~(LCD_WINCTRL1_PIPE);
3285                 val |= ((pdata->channel << 29) & LCD_WINCTRL1_PIPE);
3286                 lcd->window[plane].winctrl1 = val;
3287         }
3288         if (pdata->flags & WIN_BUFFER_FORMAT) {
3289                 val = lcd->window[plane].winctrl1 & ~(LCD_WINCTRL1_FRM);
3290                 val |= ((pdata->buffer_format << 25) & LCD_WINCTRL1_FRM);
3291                 lcd->window[plane].winctrl1 = val;
3292         }
3293         if (pdata->flags & WIN_COLOR_ORDER) {
3294                 val = lcd->window[plane].winctrl1 & ~(LCD_WINCTRL1_CCO);
3295                 val |= ((pdata->color_order << 24) & LCD_WINCTRL1_CCO);
3296                 lcd->window[plane].winctrl1 = val;
3297         }
3298         if (pdata->flags & WIN_PIXEL_ORDER) {
3299                 val = lcd->window[plane].winctrl1 & ~(LCD_WINCTRL1_PO);
3300                 val |= ((pdata->pixel_order << 22) & LCD_WINCTRL1_PO);
3301                 lcd->window[plane].winctrl1 = val;
3302         }
3303         if (pdata->flags & WIN_SIZE) {
3304                 val = lcd->window[plane].winctrl1 & ~(LCD_WINCTRL1_SZX |
3305                                 LCD_WINCTRL1_SZY);
3306                 val |= (((pdata->xsize << 11) - 1) & LCD_WINCTRL1_SZX);
3307                 val |= (((pdata->ysize) - 1) & LCD_WINCTRL1_SZY);
3308                 lcd->window[plane].winctrl1 = val;
3309                 /* program buffer line width */
3310                 bpp = winbpp(val) / 8;
3311                 val = lcd->window[plane].winctrl2 & ~(LCD_WINCTRL2_BX);
3312                 val |= (((pdata->xsize * bpp) << 8) & LCD_WINCTRL2_BX);
3313                 lcd->window[plane].winctrl2 = val;
3314         }
3315
3316         /* Window control register 2 */
3317         if (pdata->flags & WIN_COLORKEY_MODE) {
3318                 val = lcd->window[plane].winctrl2 & ~(LCD_WINCTRL2_CKMODE);
3319                 val |= ((pdata->colorkey_mode << 24) & LCD_WINCTRL2_CKMODE);
3320                 lcd->window[plane].winctrl2 = val;
3321         }
3322         if (pdata->flags & WIN_DOUBLE_BUFFER_MODE) {
3323                 val = lcd->window[plane].winctrl2 & ~(LCD_WINCTRL2_DBM);
3324                 val |= ((pdata->double_buffer_mode << 23) & LCD_WINCTRL2_DBM);
3325                 lcd->window[plane].winctrl2 = val;
3326         }
3327         if (pdata->flags & WIN_RAM_ARRAY_MODE) {
3328                 val = lcd->window[plane].winctrl2 & ~(LCD_WINCTRL2_RAM);
3329                 val |= ((pdata->ram_array_mode << 21) & LCD_WINCTRL2_RAM);
3330                 lcd->window[plane].winctrl2 = val;
3331         }
3332
3333         /* Buffer line width programmed with WIN_SIZE */
3334
3335         if (pdata->flags & WIN_BUFFER_SCALE) {
3336                 val = lcd->window[plane].winctrl2 & ~(LCD_WINCTRL2_SCX |
3337                                 LCD_WINCTRL2_SCY);
3338                 val |= ((pdata->xsize << 11) & LCD_WINCTRL2_SCX);
3339                 val |= ((pdata->ysize) & LCD_WINCTRL2_SCY);
3340                 lcd->window[plane].winctrl2 = val;
3341         }
3342
3343         if (pdata->flags & WIN_ENABLE) {
3344                 val = lcd->winenable;
3345                 val &= ~(1<<plane);
3346                 val |= (pdata->enable & 1) << plane;
3347                 lcd->winenable = val;
3348         }
3349         au_sync();
3350 }
3351
3352 static void get_window(unsigned int plane,
3353         struct au1200_lcd_window_regs_t *pdata)
3354 {
3355         /* Window control register 0 */
3356         pdata->xpos = (lcd->window[plane].winctrl0 & LCD_WINCTRL0_OX) >> 21;
3357         pdata->ypos = (lcd->window[plane].winctrl0 & LCD_WINCTRL0_OY) >> 10;
3358         pdata->alpha_color = (lcd->window[plane].winctrl0 & LCD_WINCTRL0_A) >> 2;
3359         pdata->alpha_mode = (lcd->window[plane].winctrl0 & LCD_WINCTRL0_AEN) >> 1;
3360
3361         /* Window control register 1 */
3362         pdata->priority = (lcd->window[plane].winctrl1& LCD_WINCTRL1_PRI) >> 30;
3363         pdata->channel = (lcd->window[plane].winctrl1 & LCD_WINCTRL1_PIPE) >> 29;
3364         pdata->buffer_format = (lcd->window[plane].winctrl1 & LCD_WINCTRL1_FRM) >> 25;
3365         pdata->color_order = (lcd->window[plane].winctrl1 & LCD_WINCTRL1_CCO) >> 24;
3366         pdata->pixel_order = (lcd->window[plane].winctrl1 & LCD_WINCTRL1_PO) >> 22;
3367         pdata->xsize = ((lcd->window[plane].winctrl1 & LCD_WINCTRL1_SZX) >> 11) + 1;
3368         pdata->ysize = (lcd->window[plane].winctrl1 & LCD_WINCTRL1_SZY) + 1;
3369
3370         /* Window control register 2 */
3371         pdata->colorkey_mode = (lcd->window[plane].winctrl2 & LCD_WINCTRL2_CKMODE) >> 24;
3372         pdata->double_buffer_mode = (lcd->window[plane].winctrl2 & LCD_WINCTRL2_DBM) >> 23;
3373         pdata->ram_array_mode = (lcd->window[plane].winctrl2 & LCD_WINCTRL2_RAM) >> 21;
3374
3375         pdata->enable = (lcd->winenable >> plane) & 1;
3376         au_sync();
3377 }
3378
3379 static int au1200fb_ioctl(struct fb_info *info, unsigned int cmd,
3380                           unsigned long arg)
3381 {
3382         int plane;
3383         int val;
3384
3385 #ifdef CONFIG_PM
3386         au1xxx_pm_access(LCD_pm_dev);
3387 #endif
3388
3389         plane = fbinfo2index(info);
3390         print_dbg("au1200fb: ioctl %d on plane %d\n", cmd, plane);
3391
3392         if (cmd == AU1200_LCD_FB_IOCTL) {
3393                 struct au1200_lcd_iodata_t iodata;
3394
3395                 if (copy_from_user(&iodata, (void __user *) arg, sizeof(iodata)))
3396                         return -EFAULT;
3397
3398                 print_dbg("FB IOCTL called\n");
3399
3400                 switch (iodata.subcmd) {
3401                 case AU1200_LCD_SET_SCREEN:
3402                         print_dbg("AU1200_LCD_SET_SCREEN\n");
3403                         set_global(cmd, &iodata.global);
3404                         break;
3405
3406                 case AU1200_LCD_GET_SCREEN:
3407                         print_dbg("AU1200_LCD_GET_SCREEN\n");
3408                         get_global(cmd, &iodata.global);
3409                         break;
3410
3411                 case AU1200_LCD_SET_WINDOW:
3412                         print_dbg("AU1200_LCD_SET_WINDOW\n");
3413                         set_window(plane, &iodata.window);
3414                         break;
3415
3416                 case AU1200_LCD_GET_WINDOW:
3417                         print_dbg("AU1200_LCD_GET_WINDOW\n");
3418                         get_window(plane, &iodata.window);
3419                         break;
3420
3421                 case AU1200_LCD_SET_PANEL:
3422                         print_dbg("AU1200_LCD_SET_PANEL\n");
3423                         if ((iodata.global.panel_choice >= 0) &&
3424                                         (iodata.global.panel_choice <
3425                                          NUM_PANELS))
3426                         {
3427                                 struct panel_settings *newpanel;
3428                                 panel_index = iodata.global.panel_choice;
3429                                 newpanel = &known_lcd_panels[panel_index];
3430                                 au1200_setpanel(newpanel);
3431                         }
3432                         break;
3433
3434                 case AU1200_LCD_GET_PANEL:
3435                         print_dbg("AU1200_LCD_GET_PANEL\n");
3436                         iodata.global.panel_choice = panel_index;
3437                         break;
3438
3439                 default:
3440                         return -EINVAL;
3441                 }
3442
3443                 val = copy_to_user((void __user *) arg, &iodata, sizeof(iodata));
3444                 if (val) {
3445                         print_dbg("error: could not copy %d bytes\n", val);
3446                         return -EFAULT;
3447                 }
3448         }
3449
3450         return 0;
3451 }
3452
3453
3454 static struct fb_ops au1200fb_fb_ops = {
3455         .owner          = THIS_MODULE,
3456         .fb_check_var   = au1200fb_fb_check_var,
3457         .fb_set_par     = au1200fb_fb_set_par,
3458         .fb_setcolreg   = au1200fb_fb_setcolreg,
3459         .fb_blank       = au1200fb_fb_blank,
3460         .fb_fillrect    = cfb_fillrect,
3461         .fb_copyarea    = cfb_copyarea,
3462         .fb_imageblit   = cfb_imageblit,
3463         .fb_sync        = NULL,
3464         .fb_ioctl       = au1200fb_ioctl,
3465         .fb_mmap        = au1200fb_fb_mmap,
3466 };
3467
3468 /*-------------------------------------------------------------------------*/
3469
3470 static irqreturn_t au1200fb_handle_irq(int irq, void* dev_id, struct pt_regs *regs)
3471 {
3472         /* Nothing to do for now, just clear any pending interrupt */
3473         lcd->intstatus = lcd->intstatus;
3474         au_sync();
3475
3476         return IRQ_HANDLED;
3477 }
3478
3479 /*-------------------------------------------------------------------------*/
3480
3481 /* AU1200 LCD device probe helpers */
3482
3483 static int au1200fb_init_fbinfo(struct au1200fb_device *fbdev)
3484 {
3485         struct fb_info *fbi = &fbdev->fb_info;
3486         int bpp;
3487
3488         memset(fbi, 0, sizeof(struct fb_info));
3489         fbi->fbops = &au1200fb_fb_ops;
3490
3491         bpp = winbpp(win->w[fbdev->plane].mode_winctrl1);
3492
3493         /* Copy monitor specs from panel data */
3494         /* fixme: we're setting up LCD controller windows, so these dont give a
3495         damn as to what the monitor specs are (the panel itself does, but that
3496         isnt done here...so maybe need a generic catchall monitor setting??? */
3497         memcpy(&fbi->monspecs, &panel->monspecs, sizeof(struct fb_monspecs));
3498
3499         /* We first try the user mode passed in argument. If that failed,
3500          * or if no one has been specified, we default to the first mode of the
3501          * panel list. Note that after this call, var data will be set */
3502         if (!fb_find_mode(&fbi->var,
3503                           fbi,
3504                           NULL, /* drv_info.opt_mode, */
3505                           fbi->monspecs.modedb,
3506                           fbi->monspecs.modedb_len,
3507                           fbi->monspecs.modedb,
3508                           bpp)) {
3509
3510                 print_err("Cannot find valid mode for panel %s", panel->name);
3511                 return -EFAULT;
3512         }
3513
3514         fbi->pseudo_palette = kmalloc(sizeof(u32) * 16, GFP_KERNEL);
3515         if (!fbi->pseudo_palette) {
3516                 return -ENOMEM;
3517         }
3518         memset(fbi->pseudo_palette, 0, sizeof(u32) * 16);
3519
3520         if (fb_alloc_cmap(&fbi->cmap, AU1200_LCD_NBR_PALETTE_ENTRIES, 0) < 0) {
3521                 print_err("Fail to allocate colormap (%d entries)",
3522                            AU1200_LCD_NBR_PALETTE_ENTRIES);
3523                 kfree(fbi->pseudo_palette);
3524                 return -EFAULT;
3525         }
3526
3527         strncpy(fbi->fix.id, "AU1200", sizeof(fbi->fix.id));
3528         fbi->fix.smem_start = fbdev->fb_phys;
3529         fbi->fix.smem_len = fbdev->fb_len;
3530         fbi->fix.type = FB_TYPE_PACKED_PIXELS;
3531         fbi->fix.xpanstep = 0;
3532         fbi->fix.ypanstep = 0;
3533         fbi->fix.mmio_start = 0;
3534         fbi->fix.mmio_len = 0;
3535         fbi->fix.accel = FB_ACCEL_NONE;
3536
3537         fbi->screen_base = (char __iomem *) fbdev->fb_mem;
3538
3539         au1200fb_update_fbinfo(fbi);
3540
3541         return 0;
3542 }
3543
3544 /*-------------------------------------------------------------------------*/
3545
3546 /* AU1200 LCD controller device driver */
3547
3548 static int au1200fb_drv_probe(struct device *dev)
3549 {
3550         struct au1200fb_device *fbdev;
3551         unsigned long page;
3552         int bpp, plane, ret;
3553
3554         if (!dev)
3555                 return -EINVAL;
3556
3557         for (plane = 0; plane < CONFIG_FB_AU1200_DEVS; ++plane) {
3558                 bpp = winbpp(win->w[plane].mode_winctrl1);
3559                 if (win->w[plane].xres == 0)
3560                         win->w[plane].xres = panel->Xres;
3561                 if (win->w[plane].yres == 0)
3562                         win->w[plane].yres = panel->Yres;
3563
3564                 fbdev = &_au1200fb_devices[plane];
3565                 memset(fbdev, 0, sizeof(struct au1200fb_device));
3566                 fbdev->plane = plane;
3567
3568                 /* Allocate the framebuffer to the maximum screen size */
3569                 fbdev->fb_len = (win->w[plane].xres * win->w[plane].yres * bpp) / 8;
3570
3571                 fbdev->fb_mem = dma_alloc_noncoherent(dev,
3572                                 PAGE_ALIGN(fbdev->fb_len),
3573                                 &fbdev->fb_phys, GFP_KERNEL);
3574                 if (!fbdev->fb_mem) {
3575                         print_err("fail to allocate frambuffer (size: %dK))",
3576                                   fbdev->fb_len / 1024);
3577                         return -ENOMEM;
3578                 }
3579
3580                 /*
3581                  * Set page reserved so that mmap will work. This is necessary
3582                  * since we'll be remapping normal memory.
3583                  */
3584                 for (page = (unsigned long)fbdev->fb_phys;
3585                      page < PAGE_ALIGN((unsigned long)fbdev->fb_phys +
3586                              fbdev->fb_len);
3587                      page += PAGE_SIZE) {
3588                         SetPageReserved(pfn_to_page(page >> PAGE_SHIFT)); /* LCD DMA is NOT coherent on Au1200 */
3589                 }
3590                 print_dbg("Framebuffer memory map at %p", fbdev->fb_mem);
3591                 print_dbg("phys=0x%08x, size=%dK", fbdev->fb_phys, fbdev->fb_len / 1024);
3592
3593                 /* Init FB data */
3594                 if ((ret = au1200fb_init_fbinfo(fbdev)) < 0)
3595                         goto failed;
3596
3597                 /* Register new framebuffer */
3598                 if ((ret = register_framebuffer(&fbdev->fb_info)) < 0) {
3599                         print_err("cannot register new framebuffer");
3600                         goto failed;
3601                 }
3602
3603                 au1200fb_fb_set_par(&fbdev->fb_info);
3604
3605 #if !defined(CONFIG_FRAMEBUFFER_CONSOLE) && defined(CONFIG_LOGO)
3606                 if (plane == 0)
3607                         if (fb_prepare_logo(&fbdev->fb_info, FB_ROTATE_UR)) {
3608                                 /* Start display and show logo on boot */
3609                                 fb_set_cmap(&fbdev->fb_info.cmap,
3610                                                 &fbdev->fb_info);
3611
3612                                 fb_show_logo(&fbdev->fb_info, FB_ROTATE_UR);
3613                         }
3614 #endif
3615         }
3616
3617         /* Now hook interrupt too */
3618         if ((ret = request_irq(AU1200_LCD_INT, au1200fb_handle_irq,
3619                           SA_INTERRUPT | SA_SHIRQ, "lcd", (void *)dev)) < 0) {
3620                 print_err("fail to request interrupt line %d (err: %d)",
3621                           AU1200_LCD_INT, ret);
3622                 goto failed;
3623         }
3624
3625         return 0;
3626
3627 failed:
3628         /* NOTE: This only does the current plane/window that failed; others are still active */
3629         if (fbdev->fb_mem)
3630                 dma_free_noncoherent(dev, PAGE_ALIGN(fbdev->fb_len),
3631                                 fbdev->fb_mem, fbdev->fb_phys);
3632         if (fbdev->fb_info.cmap.len != 0)
3633                 fb_dealloc_cmap(&fbdev->fb_info.cmap);
3634         if (fbdev->fb_info.pseudo_palette)
3635                 kfree(fbdev->fb_info.pseudo_palette);
3636         if (plane == 0)
3637                 free_irq(AU1200_LCD_INT, (void*)dev);
3638         return ret;
3639 }
3640
3641 static int au1200fb_drv_remove(struct device *dev)
3642 {
3643         struct au1200fb_device *fbdev;
3644         int plane;
3645
3646         if (!dev)
3647                 return -ENODEV;
3648
3649         /* Turn off the panel */
3650         au1200_setpanel(NULL);
3651
3652         for (plane = 0; plane < CONFIG_FB_AU1200_DEVS; ++plane)
3653         {
3654                 fbdev = &_au1200fb_devices[plane];
3655
3656                 /* Clean up all probe data */
3657                 unregister_framebuffer(&fbdev->fb_info);
3658                 if (fbdev->fb_mem)
3659                         dma_free_noncoherent(dev, PAGE_ALIGN(fbdev->fb_len),
3660                                         fbdev->fb_mem, fbdev->fb_phys);
3661                 if (fbdev->fb_info.cmap.len != 0)
3662                         fb_dealloc_cmap(&fbdev->fb_info.cmap);
3663                 if (fbdev->fb_info.pseudo_palette)
3664                         kfree(fbdev->fb_info.pseudo_palette);
3665         }
3666
3667         free_irq(AU1200_LCD_INT, (void *)dev);
3668
3669         return 0;
3670 }
3671
3672 #ifdef CONFIG_PM
3673 static int au1200fb_drv_suspend(struct device *dev, u32 state, u32 level)
3674 {
3675         /* TODO */
3676         return 0;
3677 }
3678
3679 static int au1200fb_drv_resume(struct device *dev, u32 level)
3680 {
3681         /* TODO */
3682         return 0;
3683 }
3684 #endif /* CONFIG_PM */
3685
3686 static struct device_driver au1200fb_driver = {
3687         .name           = "au1200-lcd",
3688         .bus            = &platform_bus_type,
3689         .probe          = au1200fb_drv_probe,
3690         .remove         = au1200fb_drv_remove,
3691 #ifdef CONFIG_PM
3692         .suspend        = au1200fb_drv_suspend,
3693         .resume         = au1200fb_drv_resume,
3694 #endif
3695 };
3696
3697 /*-------------------------------------------------------------------------*/
3698
3699 /* Kernel driver */
3700
3701 static void au1200fb_setup(void)
3702 {
3703         char* options = NULL;
3704         char* this_opt;
3705         int num_panels = ARRAY_SIZE(known_lcd_panels);
3706         int panel_idx = -1;
3707
3708         fb_get_options(DRIVER_NAME, &options);
3709
3710         if (options) {
3711                 while ((this_opt = strsep(&options,",")) != NULL) {
3712                         /* Panel option - can be panel name,
3713                          * "bs" for board-switch, or number/index */
3714                         if (!strncmp(this_opt, "panel:", 6)) {
3715                                 int i;
3716                                 long int li;
3717                                 char *endptr;
3718                                 this_opt += 6;
3719                                 /* First check for index, which allows
3720                                  * to short circuit this mess */
3721                                 li = simple_strtol(this_opt, &endptr, 0);
3722                                 if (*endptr == '\0') {
3723                                         panel_idx = (int)li;
3724                                 }
3725                                 else if (strcmp(this_opt, "bs") == 0) {
3726                                         extern int board_au1200fb_panel(void);
3727                                         panel_idx = board_au1200fb_panel();
3728                                 }
3729
3730                                 else
3731                                 for (i = 0; i < num_panels; i++) {
3732                                         if (!strcmp(this_opt, known_lcd_panels[i].name)) {
3733                                                 panel_idx = i;
3734                                                 break;
3735                                         }
3736                                 }
3737
3738                                 if ((panel_idx < 0) || (panel_idx >= num_panels)) {
3739                                                 print_warn("Panel %s not supported!", this_opt);
3740                                 }
3741                                 else
3742                                         panel_index = panel_idx;
3743                         }
3744
3745                         else if (strncmp(this_opt, "nohwcursor", 10) == 0) {
3746                                 nohwcursor = 1;
3747                         }
3748
3749                         /* Unsupported option */
3750                         else {
3751                                 print_warn("Unsupported option \"%s\"", this_opt);
3752                         }
3753                 }
3754         }
3755 }
3756
3757 #ifdef CONFIG_PM
3758 static int au1200fb_pm_callback(au1xxx_power_dev_t *dev,
3759                 au1xxx_request_t request, void *data) {
3760         int retval = -1;
3761         unsigned int d = 0;
3762         unsigned int brightness = 0;
3763
3764         if (request == AU1XXX_PM_SLEEP) {
3765                 board_au1200fb_panel_shutdown();
3766         }
3767         else if (request == AU1XXX_PM_WAKEUP) {
3768                 if(dev->prev_state == SLEEP_STATE)
3769                 {
3770                         int plane;
3771                         au1200_setpanel(panel);
3772                         for (plane = 0; plane < CONFIG_FB_AU1200_DEVS; ++plane)         {
3773                                 struct au1200fb_device *fbdev;
3774                                 fbdev = &_au1200fb_devices[plane];
3775                                 au1200fb_fb_set_par(&fbdev->fb_info);
3776                         }
3777                 }
3778
3779                 d = *((unsigned int*)data);
3780                 if(d <=10) brightness = 26;
3781                 else if(d<=20) brightness = 51;
3782                 else if(d<=30) brightness = 77;
3783                 else if(d<=40) brightness = 102;
3784                 else if(d<=50) brightness = 128;
3785                 else if(d<=60) brightness = 153;
3786                 else if(d<=70) brightness = 179;
3787                 else if(d<=80) brightness = 204;
3788                 else if(d<=90) brightness = 230;
3789                 else brightness = 255;
3790                 set_brightness(brightness);
3791         } else if (request == AU1XXX_PM_GETSTATUS) {
3792                 return dev->cur_state;
3793         } else if (request == AU1XXX_PM_ACCESS) {
3794                 if (dev->cur_state != SLEEP_STATE)
3795                         return retval;
3796                 else {
3797                         au1200_setpanel(panel);
3798                 }
3799         } else if (request == AU1XXX_PM_IDLE) {
3800         } else if (request == AU1XXX_PM_CLEANUP) {
3801         }
3802
3803         return retval;
3804 }
3805 #endif
3806
3807 static int __init au1200fb_init(void)
3808 {
3809         print_info("" DRIVER_DESC "");
3810
3811         /* Setup driver with options */
3812         au1200fb_setup();
3813
3814         /* Point to the panel selected */
3815         panel = &known_lcd_panels[panel_index];
3816         win = &windows[window_index];
3817
3818         printk(DRIVER_NAME ": Panel %d %s\n", panel_index, panel->name);
3819         printk(DRIVER_NAME ": Win %d %s\n", window_index, win->name);
3820
3821         /* Kickstart the panel, the framebuffers/windows come soon enough */
3822         au1200_setpanel(panel);
3823
3824         #ifdef CONFIG_PM
3825         LCD_pm_dev = new_au1xxx_power_device("LCD", &au1200fb_pm_callback, NULL);
3826         if ( LCD_pm_dev == NULL)
3827                 printk(KERN_INFO "Unable to create a power management device entry for the au1200fb.\n");
3828         else
3829                 printk(KERN_INFO "Power management device entry for the au1200fb loaded.\n");
3830         #endif
3831
3832         return driver_register(&au1200fb_driver);
3833 }
3834
3835 static void __exit au1200fb_cleanup(void)
3836 {
3837         driver_unregister(&au1200fb_driver);
3838 }
3839
3840 module_init(au1200fb_init);
3841 module_exit(au1200fb_cleanup);
3842
3843 MODULE_DESCRIPTION(DRIVER_DESC);
3844 MODULE_LICENSE("GPL");