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