FB: sa1100: combine RGB bitfield overrides into sa1100fb_mach_info
[pandora-kernel.git] / drivers / video / sa1100fb.c
1 /*
2  *  linux/drivers/video/sa1100fb.c
3  *
4  *  Copyright (C) 1999 Eric A. Thomas
5  *   Based on acornfb.c Copyright (C) Russell King.
6  *
7  * This file is subject to the terms and conditions of the GNU General Public
8  * License.  See the file COPYING in the main directory of this archive for
9  * more details.
10  *
11  *              StrongARM 1100 LCD Controller Frame Buffer Driver
12  *
13  * Please direct your questions and comments on this driver to the following
14  * email address:
15  *
16  *      linux-arm-kernel@lists.arm.linux.org.uk
17  *
18  * Clean patches should be sent to the ARM Linux Patch System.  Please see the
19  * following web page for more information:
20  *
21  *      http://www.arm.linux.org.uk/developer/patches/info.shtml
22  *
23  * Thank you.
24  *
25  * Known problems:
26  *      - With the Neponset plugged into an Assabet, LCD powerdown
27  *        doesn't work (LCD stays powered up).  Therefore we shouldn't
28  *        blank the screen.
29  *      - We don't limit the CPU clock rate nor the mode selection
30  *        according to the available SDRAM bandwidth.
31  *
32  * Other notes:
33  *      - Linear grayscale palettes and the kernel.
34  *        Such code does not belong in the kernel.  The kernel frame buffer
35  *        drivers do not expect a linear colourmap, but a colourmap based on
36  *        the VT100 standard mapping.
37  *
38  *        If your _userspace_ requires a linear colourmap, then the setup of
39  *        such a colourmap belongs _in userspace_, not in the kernel.  Code
40  *        to set the colourmap correctly from user space has been sent to
41  *        David Neuer.  It's around 8 lines of C code, plus another 4 to
42  *        detect if we are using grayscale.
43  *
44  *      - The following must never be specified in a panel definition:
45  *           LCCR0_LtlEnd, LCCR3_PixClkDiv, LCCR3_VrtSnchL, LCCR3_HorSnchL
46  *
47  *      - The following should be specified:
48  *           either LCCR0_Color or LCCR0_Mono
49  *           either LCCR0_Sngl or LCCR0_Dual
50  *           either LCCR0_Act or LCCR0_Pas
51  *           either LCCR3_OutEnH or LCCD3_OutEnL
52  *           either LCCR3_PixRsEdg or LCCR3_PixFlEdg
53  *           either LCCR3_ACBsDiv or LCCR3_ACBsCntOff
54  *
55  * Code Status:
56  * 1999/04/01:
57  *      - Driver appears to be working for Brutus 320x200x8bpp mode.  Other
58  *        resolutions are working, but only the 8bpp mode is supported.
59  *        Changes need to be made to the palette encode and decode routines
60  *        to support 4 and 16 bpp modes.  
61  *        Driver is not designed to be a module.  The FrameBuffer is statically
62  *        allocated since dynamic allocation of a 300k buffer cannot be 
63  *        guaranteed. 
64  *
65  * 1999/06/17:
66  *      - FrameBuffer memory is now allocated at run-time when the
67  *        driver is initialized.    
68  *
69  * 2000/04/10: Nicolas Pitre <nico@fluxnic.net>
70  *      - Big cleanup for dynamic selection of machine type at run time.
71  *
72  * 2000/07/19: Jamey Hicks <jamey@crl.dec.com>
73  *      - Support for Bitsy aka Compaq iPAQ H3600 added.
74  *
75  * 2000/08/07: Tak-Shing Chan <tchan.rd@idthk.com>
76  *             Jeff Sutherland <jsutherland@accelent.com>
77  *      - Resolved an issue caused by a change made to the Assabet's PLD 
78  *        earlier this year which broke the framebuffer driver for newer 
79  *        Phase 4 Assabets.  Some other parameters were changed to optimize
80  *        for the Sharp display.
81  *
82  * 2000/08/09: Kunihiko IMAI <imai@vasara.co.jp>
83  *      - XP860 support added
84  *
85  * 2000/08/19: Mark Huang <mhuang@livetoy.com>
86  *      - Allows standard options to be passed on the kernel command line
87  *        for most common passive displays.
88  *
89  * 2000/08/29:
90  *      - s/save_flags_cli/local_irq_save/
91  *      - remove unneeded extra save_flags_cli in sa1100fb_enable_lcd_controller
92  *
93  * 2000/10/10: Erik Mouw <J.A.K.Mouw@its.tudelft.nl>
94  *      - Updated LART stuff. Fixed some minor bugs.
95  *
96  * 2000/10/30: Murphy Chen <murphy@mail.dialogue.com.tw>
97  *      - Pangolin support added
98  *
99  * 2000/10/31: Roman Jordan <jor@hoeft-wessel.de>
100  *      - Huw Webpanel support added
101  *
102  * 2000/11/23: Eric Peng <ericpeng@coventive.com>
103  *      - Freebird add
104  *
105  * 2001/02/07: Jamey Hicks <jamey.hicks@compaq.com> 
106  *             Cliff Brake <cbrake@accelent.com>
107  *      - Added PM callback
108  *
109  * 2001/05/26: <rmk@arm.linux.org.uk>
110  *      - Fix 16bpp so that (a) we use the right colours rather than some
111  *        totally random colour depending on what was in page 0, and (b)
112  *        we don't de-reference a NULL pointer.
113  *      - remove duplicated implementation of consistent_alloc()
114  *      - convert dma address types to dma_addr_t
115  *      - remove unused 'montype' stuff
116  *      - remove redundant zero inits of init_var after the initial
117  *        memset.
118  *      - remove allow_modeset (acornfb idea does not belong here)
119  *
120  * 2001/05/28: <rmk@arm.linux.org.uk>
121  *      - massive cleanup - move machine dependent data into structures
122  *      - I've left various #warnings in - if you see one, and know
123  *        the hardware concerned, please get in contact with me.
124  *
125  * 2001/05/31: <rmk@arm.linux.org.uk>
126  *      - Fix LCCR1 HSW value, fix all machine type specifications to
127  *        keep values in line.  (Please check your machine type specs)
128  *
129  * 2001/06/10: <rmk@arm.linux.org.uk>
130  *      - Fiddle with the LCD controller from task context only; mainly
131  *        so that we can run with interrupts on, and sleep.
132  *      - Convert #warnings into #errors.  No pain, no gain. ;)
133  *
134  * 2001/06/14: <rmk@arm.linux.org.uk>
135  *      - Make the palette BPS value for 12bpp come out correctly.
136  *      - Take notice of "greyscale" on any colour depth.
137  *      - Make truecolor visuals use the RGB channel encoding information.
138  *
139  * 2001/07/02: <rmk@arm.linux.org.uk>
140  *      - Fix colourmap problems.
141  *
142  * 2001/07/13: <abraham@2d3d.co.za>
143  *      - Added support for the ICP LCD-Kit01 on LART. This LCD is
144  *        manufactured by Prime View, model no V16C6448AB
145  *
146  * 2001/07/23: <rmk@arm.linux.org.uk>
147  *      - Hand merge version from handhelds.org CVS tree.  See patch
148  *        notes for 595/1 for more information.
149  *      - Drop 12bpp (it's 16bpp with different colour register mappings).
150  *      - This hardware can not do direct colour.  Therefore we don't
151  *        support it.
152  *
153  * 2001/07/27: <rmk@arm.linux.org.uk>
154  *      - Halve YRES on dual scan LCDs.
155  *
156  * 2001/08/22: <rmk@arm.linux.org.uk>
157  *      - Add b/w iPAQ pixclock value.
158  *
159  * 2001/10/12: <rmk@arm.linux.org.uk>
160  *      - Add patch 681/1 and clean up stork definitions.
161  */
162
163 #include <linux/module.h>
164 #include <linux/kernel.h>
165 #include <linux/sched.h>
166 #include <linux/errno.h>
167 #include <linux/string.h>
168 #include <linux/interrupt.h>
169 #include <linux/slab.h>
170 #include <linux/mm.h>
171 #include <linux/fb.h>
172 #include <linux/delay.h>
173 #include <linux/init.h>
174 #include <linux/ioport.h>
175 #include <linux/cpufreq.h>
176 #include <linux/platform_device.h>
177 #include <linux/dma-mapping.h>
178 #include <linux/mutex.h>
179 #include <linux/io.h>
180
181 #include <mach/hardware.h>
182 #include <asm/mach-types.h>
183 #include <mach/assabet.h>
184 #include <mach/shannon.h>
185
186 /*
187  * Complain if VAR is out of range.
188  */
189 #define DEBUG_VAR 1
190
191 #undef ASSABET_PAL_VIDEO
192
193 #include "sa1100fb.h"
194
195 extern void (*sa1100fb_backlight_power)(int on);
196 extern void (*sa1100fb_lcd_power)(int on);
197
198 static const struct sa1100fb_rgb rgb_4 = {
199         .red    = { .offset = 0,  .length = 4, },
200         .green  = { .offset = 0,  .length = 4, },
201         .blue   = { .offset = 0,  .length = 4, },
202         .transp = { .offset = 0,  .length = 0, },
203 };
204
205 static const struct sa1100fb_rgb rgb_8 = {
206         .red    = { .offset = 0,  .length = 8, },
207         .green  = { .offset = 0,  .length = 8, },
208         .blue   = { .offset = 0,  .length = 8, },
209         .transp = { .offset = 0,  .length = 0, },
210 };
211
212 static const struct sa1100fb_rgb def_rgb_16 = {
213         .red    = { .offset = 11, .length = 5, },
214         .green  = { .offset = 5,  .length = 6, },
215         .blue   = { .offset = 0,  .length = 5, },
216         .transp = { .offset = 0,  .length = 0, },
217 };
218
219 #ifdef CONFIG_SA1100_ASSABET
220 #ifndef ASSABET_PAL_VIDEO
221 /*
222  * The assabet uses a sharp LQ039Q2DS54 LCD module.  It is actually
223  * takes an RGB666 signal, but we provide it with an RGB565 signal
224  * instead (def_rgb_16).
225  */
226 static struct sa1100fb_mach_info lq039q2ds54_info __devinitdata = {
227         .pixclock       = 171521,       .bpp            = 16,
228         .xres           = 320,          .yres           = 240,
229
230         .hsync_len      = 5,            .vsync_len      = 1,
231         .left_margin    = 61,           .upper_margin   = 3,
232         .right_margin   = 9,            .lower_margin   = 0,
233
234         .sync           = FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
235
236         .lccr0          = LCCR0_Color | LCCR0_Sngl | LCCR0_Act,
237         .lccr3          = LCCR3_OutEnH | LCCR3_PixRsEdg | LCCR3_ACBsDiv(2),
238 };
239 #else
240 static struct sa1100fb_mach_info pal_info __devinitdata = {
241         .pixclock       = 67797,        .bpp            = 16,
242         .xres           = 640,          .yres           = 512,
243
244         .hsync_len      = 64,           .vsync_len      = 6,
245         .left_margin    = 125,          .upper_margin   = 70,
246         .right_margin   = 115,          .lower_margin   = 36,
247
248         .lccr0          = LCCR0_Color | LCCR0_Sngl | LCCR0_Act,
249         .lccr3          = LCCR3_OutEnH | LCCR3_PixRsEdg | LCCR3_ACBsDiv(512),
250 };
251 #endif
252 #endif
253
254 #ifdef CONFIG_SA1100_H3600
255 static const struct sa1100fb_rgb h3600_rgb_16 = {
256         .red    = { .offset = 12, .length = 4, },
257         .green  = { .offset = 7,  .length = 4, },
258         .blue   = { .offset = 1,  .length = 4, },
259         .transp = { .offset = 0,  .length = 0, },
260 };
261
262 static struct sa1100fb_mach_info h3600_info __devinitdata = {
263         .pixclock       = 174757,       .bpp            = 16,
264         .xres           = 320,          .yres           = 240,
265
266         .hsync_len      = 3,            .vsync_len      = 3,
267         .left_margin    = 12,           .upper_margin   = 10,
268         .right_margin   = 17,           .lower_margin   = 1,
269
270         .cmap_static    = 1,
271
272         .lccr0          = LCCR0_Color | LCCR0_Sngl | LCCR0_Act,
273         .lccr3          = LCCR3_OutEnH | LCCR3_PixRsEdg | LCCR3_ACBsDiv(2),
274
275         .rgb[RGB_16] = &h3600_rgb_16,
276 };
277 #endif
278
279 #ifdef CONFIG_SA1100_H3100
280 static struct sa1100fb_mach_info h3100_info __devinitdata = {
281         .pixclock       = 406977,       .bpp            = 4,
282         .xres           = 320,          .yres           = 240,
283
284         .hsync_len      = 26,           .vsync_len      = 41,
285         .left_margin    = 4,            .upper_margin   = 0,
286         .right_margin   = 4,            .lower_margin   = 0,
287
288         .sync           = FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
289         .cmap_greyscale = 1,
290         .cmap_inverse   = 1,
291
292         .lccr0          = LCCR0_Mono | LCCR0_4PixMono | LCCR0_Sngl | LCCR0_Pas,
293         .lccr3          = LCCR3_OutEnH | LCCR3_PixRsEdg | LCCR3_ACBsDiv(2),
294 };
295 #endif
296
297 #ifdef CONFIG_SA1100_COLLIE
298 static struct sa1100fb_mach_info collie_info __devinitdata = {
299         .pixclock       = 171521,       .bpp            = 16,
300         .xres           = 320,          .yres           = 240,
301
302         .hsync_len      = 5,            .vsync_len      = 1,
303         .left_margin    = 11,           .upper_margin   = 2,
304         .right_margin   = 30,           .lower_margin   = 0,
305
306         .sync           = FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
307
308         .lccr0          = LCCR0_Color | LCCR0_Sngl | LCCR0_Act,
309         .lccr3          = LCCR3_OutEnH | LCCR3_PixRsEdg | LCCR3_ACBsDiv(2),
310 };
311 #endif
312
313 #ifdef LART_GREY_LCD
314 static struct sa1100fb_mach_info lart_grey_info __devinitdata = {
315         .pixclock       = 150000,       .bpp            = 4,
316         .xres           = 320,          .yres           = 240,
317
318         .hsync_len      = 1,            .vsync_len      = 1,
319         .left_margin    = 4,            .upper_margin   = 0,
320         .right_margin   = 2,            .lower_margin   = 0,
321
322         .cmap_greyscale = 1,
323         .sync           = FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
324
325         .lccr0          = LCCR0_Mono | LCCR0_Sngl | LCCR0_Pas | LCCR0_4PixMono,
326         .lccr3          = LCCR3_OutEnH | LCCR3_PixRsEdg | LCCR3_ACBsDiv(512),
327 };
328 #endif
329 #ifdef LART_COLOR_LCD
330 static struct sa1100fb_mach_info lart_color_info __devinitdata = {
331         .pixclock       = 150000,       .bpp            = 16,
332         .xres           = 320,          .yres           = 240,
333
334         .hsync_len      = 2,            .vsync_len      = 3,
335         .left_margin    = 69,           .upper_margin   = 14,
336         .right_margin   = 8,            .lower_margin   = 4,
337
338         .lccr0          = LCCR0_Color | LCCR0_Sngl | LCCR0_Act,
339         .lccr3          = LCCR3_OutEnH | LCCR3_PixFlEdg | LCCR3_ACBsDiv(512),
340 };
341 #endif
342 #ifdef LART_VIDEO_OUT
343 static struct sa1100fb_mach_info lart_video_info __devinitdata = {
344         .pixclock       = 39721,        .bpp            = 16,
345         .xres           = 640,          .yres           = 480,
346
347         .hsync_len      = 95,           .vsync_len      = 2,
348         .left_margin    = 40,           .upper_margin   = 32,
349         .right_margin   = 24,           .lower_margin   = 11,
350
351         .sync           = FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
352
353         .lccr0          = LCCR0_Color | LCCR0_Sngl | LCCR0_Act,
354         .lccr3          = LCCR3_OutEnL | LCCR3_PixFlEdg | LCCR3_ACBsDiv(512),
355 };
356 #endif
357
358 #ifdef LART_KIT01_LCD
359 static struct sa1100fb_mach_info lart_kit01_info __devinitdata = {
360         .pixclock       = 63291,        .bpp            = 16,
361         .xres           = 640,          .yres           = 480,
362
363         .hsync_len      = 64,           .vsync_len      = 3,
364         .left_margin    = 122,          .upper_margin   = 45,
365         .right_margin   = 10,           .lower_margin   = 10,
366
367         .lccr0          = LCCR0_Color | LCCR0_Sngl | LCCR0_Act,
368         .lccr3          = LCCR3_OutEnH | LCCR3_PixFlEdg
369 };
370 #endif
371
372 #ifdef CONFIG_SA1100_SHANNON
373 static struct sa1100fb_mach_info shannon_info __devinitdata = {
374         .pixclock       = 152500,       .bpp            = 8,
375         .xres           = 640,          .yres           = 480,
376
377         .hsync_len      = 4,            .vsync_len      = 3,
378         .left_margin    = 2,            .upper_margin   = 0,
379         .right_margin   = 1,            .lower_margin   = 0,
380
381         .sync           = FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
382
383         .lccr0          = LCCR0_Color | LCCR0_Dual | LCCR0_Pas,
384         .lccr3          = LCCR3_ACBsDiv(512),
385 };
386 #endif
387
388
389
390 static struct sa1100fb_mach_info * __devinit
391 sa1100fb_get_machine_info(struct sa1100fb_info *fbi)
392 {
393         struct sa1100fb_mach_info *inf = NULL;
394
395         /*
396          *            R        G       B       T
397          * default  {11,5}, { 5,6}, { 0,5}, { 0,0}
398          * h3600    {12,4}, { 7,4}, { 1,4}, { 0,0}
399          * freebird { 8,4}, { 4,4}, { 0,4}, {12,4}
400          */
401 #ifdef CONFIG_SA1100_ASSABET
402         if (machine_is_assabet()) {
403 #ifndef ASSABET_PAL_VIDEO
404                 inf = &lq039q2ds54_info;
405 #else
406                 inf = &pal_info;
407 #endif
408         }
409 #endif
410 #ifdef CONFIG_SA1100_H3100
411         if (machine_is_h3100()) {
412                 inf = &h3100_info;
413         }
414 #endif
415 #ifdef CONFIG_SA1100_H3600
416         if (machine_is_h3600()) {
417                 inf = &h3600_info;
418         }
419 #endif
420 #ifdef CONFIG_SA1100_COLLIE
421         if (machine_is_collie()) {
422                 inf = &collie_info;
423         }
424 #endif
425 #ifdef CONFIG_SA1100_LART
426         if (machine_is_lart()) {
427 #ifdef LART_GREY_LCD
428                 inf = &lart_grey_info;
429 #endif
430 #ifdef LART_COLOR_LCD
431                 inf = &lart_color_info;
432 #endif
433 #ifdef LART_VIDEO_OUT
434                 inf = &lart_video_info;
435 #endif
436 #ifdef LART_KIT01_LCD
437                 inf = &lart_kit01_info;
438 #endif
439         }
440 #endif
441 #ifdef CONFIG_SA1100_SHANNON
442         if (machine_is_shannon()) {
443                 inf = &shannon_info;
444         }
445 #endif
446         return inf;
447 }
448
449 static int sa1100fb_activate_var(struct fb_var_screeninfo *var, struct sa1100fb_info *);
450 static void set_ctrlr_state(struct sa1100fb_info *fbi, u_int state);
451
452 static inline void sa1100fb_schedule_work(struct sa1100fb_info *fbi, u_int state)
453 {
454         unsigned long flags;
455
456         local_irq_save(flags);
457         /*
458          * We need to handle two requests being made at the same time.
459          * There are two important cases:
460          *  1. When we are changing VT (C_REENABLE) while unblanking (C_ENABLE)
461          *     We must perform the unblanking, which will do our REENABLE for us.
462          *  2. When we are blanking, but immediately unblank before we have
463          *     blanked.  We do the "REENABLE" thing here as well, just to be sure.
464          */
465         if (fbi->task_state == C_ENABLE && state == C_REENABLE)
466                 state = (u_int) -1;
467         if (fbi->task_state == C_DISABLE && state == C_ENABLE)
468                 state = C_REENABLE;
469
470         if (state != (u_int)-1) {
471                 fbi->task_state = state;
472                 schedule_work(&fbi->task);
473         }
474         local_irq_restore(flags);
475 }
476
477 static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
478 {
479         chan &= 0xffff;
480         chan >>= 16 - bf->length;
481         return chan << bf->offset;
482 }
483
484 /*
485  * Convert bits-per-pixel to a hardware palette PBS value.
486  */
487 static inline u_int palette_pbs(struct fb_var_screeninfo *var)
488 {
489         int ret = 0;
490         switch (var->bits_per_pixel) {
491         case 4:  ret = 0 << 12; break;
492         case 8:  ret = 1 << 12; break;
493         case 16: ret = 2 << 12; break;
494         }
495         return ret;
496 }
497
498 static int
499 sa1100fb_setpalettereg(u_int regno, u_int red, u_int green, u_int blue,
500                        u_int trans, struct fb_info *info)
501 {
502         struct sa1100fb_info *fbi = (struct sa1100fb_info *)info;
503         u_int val, ret = 1;
504
505         if (regno < fbi->palette_size) {
506                 val = ((red >> 4) & 0xf00);
507                 val |= ((green >> 8) & 0x0f0);
508                 val |= ((blue >> 12) & 0x00f);
509
510                 if (regno == 0)
511                         val |= palette_pbs(&fbi->fb.var);
512
513                 fbi->palette_cpu[regno] = val;
514                 ret = 0;
515         }
516         return ret;
517 }
518
519 static int
520 sa1100fb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
521                    u_int trans, struct fb_info *info)
522 {
523         struct sa1100fb_info *fbi = (struct sa1100fb_info *)info;
524         unsigned int val;
525         int ret = 1;
526
527         /*
528          * If inverse mode was selected, invert all the colours
529          * rather than the register number.  The register number
530          * is what you poke into the framebuffer to produce the
531          * colour you requested.
532          */
533         if (fbi->cmap_inverse) {
534                 red   = 0xffff - red;
535                 green = 0xffff - green;
536                 blue  = 0xffff - blue;
537         }
538
539         /*
540          * If greyscale is true, then we convert the RGB value
541          * to greyscale no mater what visual we are using.
542          */
543         if (fbi->fb.var.grayscale)
544                 red = green = blue = (19595 * red + 38470 * green +
545                                         7471 * blue) >> 16;
546
547         switch (fbi->fb.fix.visual) {
548         case FB_VISUAL_TRUECOLOR:
549                 /*
550                  * 12 or 16-bit True Colour.  We encode the RGB value
551                  * according to the RGB bitfield information.
552                  */
553                 if (regno < 16) {
554                         u32 *pal = fbi->fb.pseudo_palette;
555
556                         val  = chan_to_field(red, &fbi->fb.var.red);
557                         val |= chan_to_field(green, &fbi->fb.var.green);
558                         val |= chan_to_field(blue, &fbi->fb.var.blue);
559
560                         pal[regno] = val;
561                         ret = 0;
562                 }
563                 break;
564
565         case FB_VISUAL_STATIC_PSEUDOCOLOR:
566         case FB_VISUAL_PSEUDOCOLOR:
567                 ret = sa1100fb_setpalettereg(regno, red, green, blue, trans, info);
568                 break;
569         }
570
571         return ret;
572 }
573
574 #ifdef CONFIG_CPU_FREQ
575 /*
576  *  sa1100fb_display_dma_period()
577  *    Calculate the minimum period (in picoseconds) between two DMA
578  *    requests for the LCD controller.  If we hit this, it means we're
579  *    doing nothing but LCD DMA.
580  */
581 static inline unsigned int sa1100fb_display_dma_period(struct fb_var_screeninfo *var)
582 {
583         /*
584          * Period = pixclock * bits_per_byte * bytes_per_transfer
585          *              / memory_bits_per_pixel;
586          */
587         return var->pixclock * 8 * 16 / var->bits_per_pixel;
588 }
589 #endif
590
591 /*
592  *  sa1100fb_check_var():
593  *    Round up in the following order: bits_per_pixel, xres,
594  *    yres, xres_virtual, yres_virtual, xoffset, yoffset, grayscale,
595  *    bitfields, horizontal timing, vertical timing.
596  */
597 static int
598 sa1100fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
599 {
600         struct sa1100fb_info *fbi = (struct sa1100fb_info *)info;
601         int rgbidx;
602
603         if (var->xres < MIN_XRES)
604                 var->xres = MIN_XRES;
605         if (var->yres < MIN_YRES)
606                 var->yres = MIN_YRES;
607         if (var->xres > fbi->max_xres)
608                 var->xres = fbi->max_xres;
609         if (var->yres > fbi->max_yres)
610                 var->yres = fbi->max_yres;
611         var->xres_virtual = max(var->xres_virtual, var->xres);
612         var->yres_virtual = max(var->yres_virtual, var->yres);
613
614         dev_dbg(fbi->dev, "var->bits_per_pixel=%d\n", var->bits_per_pixel);
615         switch (var->bits_per_pixel) {
616         case 4:
617                 rgbidx = RGB_4;
618                 break;
619         case 8:
620                 rgbidx = RGB_8;
621                 break;
622         case 16:
623                 rgbidx = RGB_16;
624                 break;
625         default:
626                 return -EINVAL;
627         }
628
629         /*
630          * Copy the RGB parameters for this display
631          * from the machine specific parameters.
632          */
633         var->red    = fbi->rgb[rgbidx]->red;
634         var->green  = fbi->rgb[rgbidx]->green;
635         var->blue   = fbi->rgb[rgbidx]->blue;
636         var->transp = fbi->rgb[rgbidx]->transp;
637
638         dev_dbg(fbi->dev, "RGBT length = %d:%d:%d:%d\n",
639                 var->red.length, var->green.length, var->blue.length,
640                 var->transp.length);
641
642         dev_dbg(fbi->dev, "RGBT offset = %d:%d:%d:%d\n",
643                 var->red.offset, var->green.offset, var->blue.offset,
644                 var->transp.offset);
645
646 #ifdef CONFIG_CPU_FREQ
647         dev_dbg(fbi->dev, "dma period = %d ps, clock = %d kHz\n",
648                 sa1100fb_display_dma_period(var),
649                 cpufreq_get(smp_processor_id()));
650 #endif
651
652         return 0;
653 }
654
655 static inline void sa1100fb_set_truecolor(u_int is_true_color)
656 {
657         if (machine_is_assabet()) {
658 #if 1           // phase 4 or newer Assabet's
659                 if (is_true_color)
660                         ASSABET_BCR_set(ASSABET_BCR_LCD_12RGB);
661                 else
662                         ASSABET_BCR_clear(ASSABET_BCR_LCD_12RGB);
663 #else
664                 // older Assabet's
665                 if (is_true_color)
666                         ASSABET_BCR_clear(ASSABET_BCR_LCD_12RGB);
667                 else
668                         ASSABET_BCR_set(ASSABET_BCR_LCD_12RGB);
669 #endif
670         }
671 }
672
673 /*
674  * sa1100fb_set_par():
675  *      Set the user defined part of the display for the specified console
676  */
677 static int sa1100fb_set_par(struct fb_info *info)
678 {
679         struct sa1100fb_info *fbi = (struct sa1100fb_info *)info;
680         struct fb_var_screeninfo *var = &info->var;
681         unsigned long palette_mem_size;
682
683         dev_dbg(fbi->dev, "set_par\n");
684
685         if (var->bits_per_pixel == 16)
686                 fbi->fb.fix.visual = FB_VISUAL_TRUECOLOR;
687         else if (!fbi->cmap_static)
688                 fbi->fb.fix.visual = FB_VISUAL_PSEUDOCOLOR;
689         else {
690                 /*
691                  * Some people have weird ideas about wanting static
692                  * pseudocolor maps.  I suspect their user space
693                  * applications are broken.
694                  */
695                 fbi->fb.fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
696         }
697
698         fbi->fb.fix.line_length = var->xres_virtual *
699                                   var->bits_per_pixel / 8;
700         fbi->palette_size = var->bits_per_pixel == 8 ? 256 : 16;
701
702         palette_mem_size = fbi->palette_size * sizeof(u16);
703
704         dev_dbg(fbi->dev, "palette_mem_size = 0x%08lx\n", palette_mem_size);
705
706         fbi->palette_cpu = (u16 *)(fbi->map_cpu + PAGE_SIZE - palette_mem_size);
707         fbi->palette_dma = fbi->map_dma + PAGE_SIZE - palette_mem_size;
708
709         /*
710          * Set (any) board control register to handle new color depth
711          */
712         sa1100fb_set_truecolor(fbi->fb.fix.visual == FB_VISUAL_TRUECOLOR);
713         sa1100fb_activate_var(var, fbi);
714
715         return 0;
716 }
717
718 #if 0
719 static int
720 sa1100fb_set_cmap(struct fb_cmap *cmap, int kspc, int con,
721                   struct fb_info *info)
722 {
723         struct sa1100fb_info *fbi = (struct sa1100fb_info *)info;
724
725         /*
726          * Make sure the user isn't doing something stupid.
727          */
728         if (!kspc && (fbi->fb.var.bits_per_pixel == 16 || fbi->cmap_static))
729                 return -EINVAL;
730
731         return gen_set_cmap(cmap, kspc, con, info);
732 }
733 #endif
734
735 /*
736  * Formal definition of the VESA spec:
737  *  On
738  *      This refers to the state of the display when it is in full operation
739  *  Stand-By
740  *      This defines an optional operating state of minimal power reduction with
741  *      the shortest recovery time
742  *  Suspend
743  *      This refers to a level of power management in which substantial power
744  *      reduction is achieved by the display.  The display can have a longer 
745  *      recovery time from this state than from the Stand-by state
746  *  Off
747  *      This indicates that the display is consuming the lowest level of power
748  *      and is non-operational. Recovery from this state may optionally require
749  *      the user to manually power on the monitor
750  *
751  *  Now, the fbdev driver adds an additional state, (blank), where they
752  *  turn off the video (maybe by colormap tricks), but don't mess with the
753  *  video itself: think of it semantically between on and Stand-By.
754  *
755  *  So here's what we should do in our fbdev blank routine:
756  *
757  *      VESA_NO_BLANKING (mode 0)       Video on,  front/back light on
758  *      VESA_VSYNC_SUSPEND (mode 1)     Video on,  front/back light off
759  *      VESA_HSYNC_SUSPEND (mode 2)     Video on,  front/back light off
760  *      VESA_POWERDOWN (mode 3)         Video off, front/back light off
761  *
762  *  This will match the matrox implementation.
763  */
764 /*
765  * sa1100fb_blank():
766  *      Blank the display by setting all palette values to zero.  Note, the 
767  *      12 and 16 bpp modes don't really use the palette, so this will not
768  *      blank the display in all modes.  
769  */
770 static int sa1100fb_blank(int blank, struct fb_info *info)
771 {
772         struct sa1100fb_info *fbi = (struct sa1100fb_info *)info;
773         int i;
774
775         dev_dbg(fbi->dev, "sa1100fb_blank: blank=%d\n", blank);
776
777         switch (blank) {
778         case FB_BLANK_POWERDOWN:
779         case FB_BLANK_VSYNC_SUSPEND:
780         case FB_BLANK_HSYNC_SUSPEND:
781         case FB_BLANK_NORMAL:
782                 if (fbi->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR ||
783                     fbi->fb.fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
784                         for (i = 0; i < fbi->palette_size; i++)
785                                 sa1100fb_setpalettereg(i, 0, 0, 0, 0, info);
786                 sa1100fb_schedule_work(fbi, C_DISABLE);
787                 break;
788
789         case FB_BLANK_UNBLANK:
790                 if (fbi->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR ||
791                     fbi->fb.fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
792                         fb_set_cmap(&fbi->fb.cmap, info);
793                 sa1100fb_schedule_work(fbi, C_ENABLE);
794         }
795         return 0;
796 }
797
798 static int sa1100fb_mmap(struct fb_info *info,
799                          struct vm_area_struct *vma)
800 {
801         struct sa1100fb_info *fbi = (struct sa1100fb_info *)info;
802         unsigned long start, len, off = vma->vm_pgoff << PAGE_SHIFT;
803
804         if (off < info->fix.smem_len) {
805                 vma->vm_pgoff += 1; /* skip over the palette */
806                 return dma_mmap_writecombine(fbi->dev, vma, fbi->map_cpu,
807                                              fbi->map_dma, fbi->map_size);
808         }
809
810         start = info->fix.mmio_start;
811         len = PAGE_ALIGN((start & ~PAGE_MASK) + info->fix.mmio_len);
812
813         if ((vma->vm_end - vma->vm_start + off) > len)
814                 return -EINVAL;
815
816         off += start & PAGE_MASK;
817         vma->vm_pgoff = off >> PAGE_SHIFT;
818         vma->vm_flags |= VM_IO;
819         vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
820         return io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
821                                    vma->vm_end - vma->vm_start,
822                                    vma->vm_page_prot);
823 }
824
825 static struct fb_ops sa1100fb_ops = {
826         .owner          = THIS_MODULE,
827         .fb_check_var   = sa1100fb_check_var,
828         .fb_set_par     = sa1100fb_set_par,
829 //      .fb_set_cmap    = sa1100fb_set_cmap,
830         .fb_setcolreg   = sa1100fb_setcolreg,
831         .fb_fillrect    = cfb_fillrect,
832         .fb_copyarea    = cfb_copyarea,
833         .fb_imageblit   = cfb_imageblit,
834         .fb_blank       = sa1100fb_blank,
835         .fb_mmap        = sa1100fb_mmap,
836 };
837
838 /*
839  * Calculate the PCD value from the clock rate (in picoseconds).
840  * We take account of the PPCR clock setting.
841  */
842 static inline unsigned int get_pcd(unsigned int pixclock, unsigned int cpuclock)
843 {
844         unsigned int pcd = cpuclock / 100;
845
846         pcd *= pixclock;
847         pcd /= 10000000;
848
849         return pcd + 1; /* make up for integer math truncations */
850 }
851
852 /*
853  * sa1100fb_activate_var():
854  *      Configures LCD Controller based on entries in var parameter.  Settings are      
855  *      only written to the controller if changes were made.  
856  */
857 static int sa1100fb_activate_var(struct fb_var_screeninfo *var, struct sa1100fb_info *fbi)
858 {
859         struct sa1100fb_lcd_reg new_regs;
860         u_int half_screen_size, yres, pcd;
861         u_long flags;
862
863         dev_dbg(fbi->dev, "Configuring SA1100 LCD\n");
864
865         dev_dbg(fbi->dev, "var: xres=%d hslen=%d lm=%d rm=%d\n",
866                 var->xres, var->hsync_len,
867                 var->left_margin, var->right_margin);
868         dev_dbg(fbi->dev, "var: yres=%d vslen=%d um=%d bm=%d\n",
869                 var->yres, var->vsync_len,
870                 var->upper_margin, var->lower_margin);
871
872 #if DEBUG_VAR
873         if (var->xres < 16        || var->xres > 1024)
874                 dev_err(fbi->dev, "%s: invalid xres %d\n",
875                         fbi->fb.fix.id, var->xres);
876         if (var->hsync_len < 1    || var->hsync_len > 64)
877                 dev_err(fbi->dev, "%s: invalid hsync_len %d\n",
878                         fbi->fb.fix.id, var->hsync_len);
879         if (var->left_margin < 1  || var->left_margin > 255)
880                 dev_err(fbi->dev, "%s: invalid left_margin %d\n",
881                         fbi->fb.fix.id, var->left_margin);
882         if (var->right_margin < 1 || var->right_margin > 255)
883                 dev_err(fbi->dev, "%s: invalid right_margin %d\n",
884                         fbi->fb.fix.id, var->right_margin);
885         if (var->yres < 1         || var->yres > 1024)
886                 dev_err(fbi->dev, "%s: invalid yres %d\n",
887                         fbi->fb.fix.id, var->yres);
888         if (var->vsync_len < 1    || var->vsync_len > 64)
889                 dev_err(fbi->dev, "%s: invalid vsync_len %d\n",
890                         fbi->fb.fix.id, var->vsync_len);
891         if (var->upper_margin < 0 || var->upper_margin > 255)
892                 dev_err(fbi->dev, "%s: invalid upper_margin %d\n",
893                         fbi->fb.fix.id, var->upper_margin);
894         if (var->lower_margin < 0 || var->lower_margin > 255)
895                 dev_err(fbi->dev, "%s: invalid lower_margin %d\n",
896                         fbi->fb.fix.id, var->lower_margin);
897 #endif
898
899         new_regs.lccr0 = fbi->lccr0 |
900                 LCCR0_LEN | LCCR0_LDM | LCCR0_BAM |
901                 LCCR0_ERM | LCCR0_LtlEnd | LCCR0_DMADel(0);
902
903         new_regs.lccr1 =
904                 LCCR1_DisWdth(var->xres) +
905                 LCCR1_HorSnchWdth(var->hsync_len) +
906                 LCCR1_BegLnDel(var->left_margin) +
907                 LCCR1_EndLnDel(var->right_margin);
908
909         /*
910          * If we have a dual scan LCD, then we need to halve
911          * the YRES parameter.
912          */
913         yres = var->yres;
914         if (fbi->lccr0 & LCCR0_Dual)
915                 yres /= 2;
916
917         new_regs.lccr2 =
918                 LCCR2_DisHght(yres) +
919                 LCCR2_VrtSnchWdth(var->vsync_len) +
920                 LCCR2_BegFrmDel(var->upper_margin) +
921                 LCCR2_EndFrmDel(var->lower_margin);
922
923         pcd = get_pcd(var->pixclock, cpufreq_get(0));
924         new_regs.lccr3 = LCCR3_PixClkDiv(pcd) | fbi->lccr3 |
925                 (var->sync & FB_SYNC_HOR_HIGH_ACT ? LCCR3_HorSnchH : LCCR3_HorSnchL) |
926                 (var->sync & FB_SYNC_VERT_HIGH_ACT ? LCCR3_VrtSnchH : LCCR3_VrtSnchL);
927
928         dev_dbg(fbi->dev, "nlccr0 = 0x%08lx\n", new_regs.lccr0);
929         dev_dbg(fbi->dev, "nlccr1 = 0x%08lx\n", new_regs.lccr1);
930         dev_dbg(fbi->dev, "nlccr2 = 0x%08lx\n", new_regs.lccr2);
931         dev_dbg(fbi->dev, "nlccr3 = 0x%08lx\n", new_regs.lccr3);
932
933         half_screen_size = var->bits_per_pixel;
934         half_screen_size = half_screen_size * var->xres * var->yres / 16;
935
936         /* Update shadow copy atomically */
937         local_irq_save(flags);
938         fbi->dbar1 = fbi->palette_dma;
939         fbi->dbar2 = fbi->screen_dma + half_screen_size;
940
941         fbi->reg_lccr0 = new_regs.lccr0;
942         fbi->reg_lccr1 = new_regs.lccr1;
943         fbi->reg_lccr2 = new_regs.lccr2;
944         fbi->reg_lccr3 = new_regs.lccr3;
945         local_irq_restore(flags);
946
947         /*
948          * Only update the registers if the controller is enabled
949          * and something has changed.
950          */
951         if ((LCCR0 != fbi->reg_lccr0)       || (LCCR1 != fbi->reg_lccr1) ||
952             (LCCR2 != fbi->reg_lccr2)       || (LCCR3 != fbi->reg_lccr3) ||
953             (DBAR1 != fbi->dbar1) || (DBAR2 != fbi->dbar2))
954                 sa1100fb_schedule_work(fbi, C_REENABLE);
955
956         return 0;
957 }
958
959 /*
960  * NOTE!  The following functions are purely helpers for set_ctrlr_state.
961  * Do not call them directly; set_ctrlr_state does the correct serialisation
962  * to ensure that things happen in the right way 100% of time time.
963  *      -- rmk
964  */
965 static inline void __sa1100fb_backlight_power(struct sa1100fb_info *fbi, int on)
966 {
967         dev_dbg(fbi->dev, "backlight o%s\n", on ? "n" : "ff");
968
969         if (sa1100fb_backlight_power)
970                 sa1100fb_backlight_power(on);
971 }
972
973 static inline void __sa1100fb_lcd_power(struct sa1100fb_info *fbi, int on)
974 {
975         dev_dbg(fbi->dev, "LCD power o%s\n", on ? "n" : "ff");
976
977         if (sa1100fb_lcd_power)
978                 sa1100fb_lcd_power(on);
979 }
980
981 static void sa1100fb_setup_gpio(struct sa1100fb_info *fbi)
982 {
983         u_int mask = 0;
984
985         /*
986          * Enable GPIO<9:2> for LCD use if:
987          *  1. Active display, or
988          *  2. Color Dual Passive display
989          *
990          * see table 11.8 on page 11-27 in the SA1100 manual
991          *   -- Erik.
992          *
993          * SA1110 spec update nr. 25 says we can and should
994          * clear LDD15 to 12 for 4 or 8bpp modes with active
995          * panels.  
996          */
997         if ((fbi->reg_lccr0 & LCCR0_CMS) == LCCR0_Color &&
998             (fbi->reg_lccr0 & (LCCR0_Dual|LCCR0_Act)) != 0) {
999                 mask = GPIO_LDD11 | GPIO_LDD10 | GPIO_LDD9  | GPIO_LDD8;
1000
1001                 if (fbi->fb.var.bits_per_pixel > 8 ||
1002                     (fbi->reg_lccr0 & (LCCR0_Dual|LCCR0_Act)) == LCCR0_Dual)
1003                         mask |= GPIO_LDD15 | GPIO_LDD14 | GPIO_LDD13 | GPIO_LDD12;
1004
1005         }
1006
1007         if (mask) {
1008                 GPDR |= mask;
1009                 GAFR |= mask;
1010         }
1011 }
1012
1013 static void sa1100fb_enable_controller(struct sa1100fb_info *fbi)
1014 {
1015         dev_dbg(fbi->dev, "Enabling LCD controller\n");
1016
1017         /*
1018          * Make sure the mode bits are present in the first palette entry
1019          */
1020         fbi->palette_cpu[0] &= 0xcfff;
1021         fbi->palette_cpu[0] |= palette_pbs(&fbi->fb.var);
1022
1023         /* Sequence from 11.7.10 */
1024         LCCR3 = fbi->reg_lccr3;
1025         LCCR2 = fbi->reg_lccr2;
1026         LCCR1 = fbi->reg_lccr1;
1027         LCCR0 = fbi->reg_lccr0 & ~LCCR0_LEN;
1028         DBAR1 = fbi->dbar1;
1029         DBAR2 = fbi->dbar2;
1030         LCCR0 |= LCCR0_LEN;
1031
1032         if (machine_is_shannon()) {
1033                 GPDR |= SHANNON_GPIO_DISP_EN;
1034                 GPSR |= SHANNON_GPIO_DISP_EN;
1035         }
1036
1037         dev_dbg(fbi->dev, "DBAR1 = 0x%08lx\n", DBAR1);
1038         dev_dbg(fbi->dev, "DBAR2 = 0x%08lx\n", DBAR2);
1039         dev_dbg(fbi->dev, "LCCR0 = 0x%08lx\n", LCCR0);
1040         dev_dbg(fbi->dev, "LCCR1 = 0x%08lx\n", LCCR1);
1041         dev_dbg(fbi->dev, "LCCR2 = 0x%08lx\n", LCCR2);
1042         dev_dbg(fbi->dev, "LCCR3 = 0x%08lx\n", LCCR3);
1043 }
1044
1045 static void sa1100fb_disable_controller(struct sa1100fb_info *fbi)
1046 {
1047         DECLARE_WAITQUEUE(wait, current);
1048
1049         dev_dbg(fbi->dev, "Disabling LCD controller\n");
1050
1051         if (machine_is_shannon()) {
1052                 GPCR |= SHANNON_GPIO_DISP_EN;
1053         }       
1054
1055         set_current_state(TASK_UNINTERRUPTIBLE);
1056         add_wait_queue(&fbi->ctrlr_wait, &wait);
1057
1058         LCSR = 0xffffffff;      /* Clear LCD Status Register */
1059         LCCR0 &= ~LCCR0_LDM;    /* Enable LCD Disable Done Interrupt */
1060         LCCR0 &= ~LCCR0_LEN;    /* Disable LCD Controller */
1061
1062         schedule_timeout(20 * HZ / 1000);
1063         remove_wait_queue(&fbi->ctrlr_wait, &wait);
1064 }
1065
1066 /*
1067  *  sa1100fb_handle_irq: Handle 'LCD DONE' interrupts.
1068  */
1069 static irqreturn_t sa1100fb_handle_irq(int irq, void *dev_id)
1070 {
1071         struct sa1100fb_info *fbi = dev_id;
1072         unsigned int lcsr = LCSR;
1073
1074         if (lcsr & LCSR_LDD) {
1075                 LCCR0 |= LCCR0_LDM;
1076                 wake_up(&fbi->ctrlr_wait);
1077         }
1078
1079         LCSR = lcsr;
1080         return IRQ_HANDLED;
1081 }
1082
1083 /*
1084  * This function must be called from task context only, since it will
1085  * sleep when disabling the LCD controller, or if we get two contending
1086  * processes trying to alter state.
1087  */
1088 static void set_ctrlr_state(struct sa1100fb_info *fbi, u_int state)
1089 {
1090         u_int old_state;
1091
1092         mutex_lock(&fbi->ctrlr_lock);
1093
1094         old_state = fbi->state;
1095
1096         /*
1097          * Hack around fbcon initialisation.
1098          */
1099         if (old_state == C_STARTUP && state == C_REENABLE)
1100                 state = C_ENABLE;
1101
1102         switch (state) {
1103         case C_DISABLE_CLKCHANGE:
1104                 /*
1105                  * Disable controller for clock change.  If the
1106                  * controller is already disabled, then do nothing.
1107                  */
1108                 if (old_state != C_DISABLE && old_state != C_DISABLE_PM) {
1109                         fbi->state = state;
1110                         sa1100fb_disable_controller(fbi);
1111                 }
1112                 break;
1113
1114         case C_DISABLE_PM:
1115         case C_DISABLE:
1116                 /*
1117                  * Disable controller
1118                  */
1119                 if (old_state != C_DISABLE) {
1120                         fbi->state = state;
1121
1122                         __sa1100fb_backlight_power(fbi, 0);
1123                         if (old_state != C_DISABLE_CLKCHANGE)
1124                                 sa1100fb_disable_controller(fbi);
1125                         __sa1100fb_lcd_power(fbi, 0);
1126                 }
1127                 break;
1128
1129         case C_ENABLE_CLKCHANGE:
1130                 /*
1131                  * Enable the controller after clock change.  Only
1132                  * do this if we were disabled for the clock change.
1133                  */
1134                 if (old_state == C_DISABLE_CLKCHANGE) {
1135                         fbi->state = C_ENABLE;
1136                         sa1100fb_enable_controller(fbi);
1137                 }
1138                 break;
1139
1140         case C_REENABLE:
1141                 /*
1142                  * Re-enable the controller only if it was already
1143                  * enabled.  This is so we reprogram the control
1144                  * registers.
1145                  */
1146                 if (old_state == C_ENABLE) {
1147                         sa1100fb_disable_controller(fbi);
1148                         sa1100fb_setup_gpio(fbi);
1149                         sa1100fb_enable_controller(fbi);
1150                 }
1151                 break;
1152
1153         case C_ENABLE_PM:
1154                 /*
1155                  * Re-enable the controller after PM.  This is not
1156                  * perfect - think about the case where we were doing
1157                  * a clock change, and we suspended half-way through.
1158                  */
1159                 if (old_state != C_DISABLE_PM)
1160                         break;
1161                 /* fall through */
1162
1163         case C_ENABLE:
1164                 /*
1165                  * Power up the LCD screen, enable controller, and
1166                  * turn on the backlight.
1167                  */
1168                 if (old_state != C_ENABLE) {
1169                         fbi->state = C_ENABLE;
1170                         sa1100fb_setup_gpio(fbi);
1171                         __sa1100fb_lcd_power(fbi, 1);
1172                         sa1100fb_enable_controller(fbi);
1173                         __sa1100fb_backlight_power(fbi, 1);
1174                 }
1175                 break;
1176         }
1177         mutex_unlock(&fbi->ctrlr_lock);
1178 }
1179
1180 /*
1181  * Our LCD controller task (which is called when we blank or unblank)
1182  * via keventd.
1183  */
1184 static void sa1100fb_task(struct work_struct *w)
1185 {
1186         struct sa1100fb_info *fbi = container_of(w, struct sa1100fb_info, task);
1187         u_int state = xchg(&fbi->task_state, -1);
1188
1189         set_ctrlr_state(fbi, state);
1190 }
1191
1192 #ifdef CONFIG_CPU_FREQ
1193 /*
1194  * Calculate the minimum DMA period over all displays that we own.
1195  * This, together with the SDRAM bandwidth defines the slowest CPU
1196  * frequency that can be selected.
1197  */
1198 static unsigned int sa1100fb_min_dma_period(struct sa1100fb_info *fbi)
1199 {
1200 #if 0
1201         unsigned int min_period = (unsigned int)-1;
1202         int i;
1203
1204         for (i = 0; i < MAX_NR_CONSOLES; i++) {
1205                 struct display *disp = &fb_display[i];
1206                 unsigned int period;
1207
1208                 /*
1209                  * Do we own this display?
1210                  */
1211                 if (disp->fb_info != &fbi->fb)
1212                         continue;
1213
1214                 /*
1215                  * Ok, calculate its DMA period
1216                  */
1217                 period = sa1100fb_display_dma_period(&disp->var);
1218                 if (period < min_period)
1219                         min_period = period;
1220         }
1221
1222         return min_period;
1223 #else
1224         /*
1225          * FIXME: we need to verify _all_ consoles.
1226          */
1227         return sa1100fb_display_dma_period(&fbi->fb.var);
1228 #endif
1229 }
1230
1231 /*
1232  * CPU clock speed change handler.  We need to adjust the LCD timing
1233  * parameters when the CPU clock is adjusted by the power management
1234  * subsystem.
1235  */
1236 static int
1237 sa1100fb_freq_transition(struct notifier_block *nb, unsigned long val,
1238                          void *data)
1239 {
1240         struct sa1100fb_info *fbi = TO_INF(nb, freq_transition);
1241         struct cpufreq_freqs *f = data;
1242         u_int pcd;
1243
1244         switch (val) {
1245         case CPUFREQ_PRECHANGE:
1246                 set_ctrlr_state(fbi, C_DISABLE_CLKCHANGE);
1247                 break;
1248
1249         case CPUFREQ_POSTCHANGE:
1250                 pcd = get_pcd(fbi->fb.var.pixclock, f->new);
1251                 fbi->reg_lccr3 = (fbi->reg_lccr3 & ~0xff) | LCCR3_PixClkDiv(pcd);
1252                 set_ctrlr_state(fbi, C_ENABLE_CLKCHANGE);
1253                 break;
1254         }
1255         return 0;
1256 }
1257
1258 static int
1259 sa1100fb_freq_policy(struct notifier_block *nb, unsigned long val,
1260                      void *data)
1261 {
1262         struct sa1100fb_info *fbi = TO_INF(nb, freq_policy);
1263         struct cpufreq_policy *policy = data;
1264
1265         switch (val) {
1266         case CPUFREQ_ADJUST:
1267         case CPUFREQ_INCOMPATIBLE:
1268                 dev_dbg(fbi->dev, "min dma period: %d ps, "
1269                         "new clock %d kHz\n", sa1100fb_min_dma_period(fbi),
1270                         policy->max);
1271                 /* todo: fill in min/max values */
1272                 break;
1273         case CPUFREQ_NOTIFY:
1274                 do {} while(0);
1275                 /* todo: panic if min/max values aren't fulfilled 
1276                  * [can't really happen unless there's a bug in the
1277                  * CPU policy verififcation process *
1278                  */
1279                 break;
1280         }
1281         return 0;
1282 }
1283 #endif
1284
1285 #ifdef CONFIG_PM
1286 /*
1287  * Power management hooks.  Note that we won't be called from IRQ context,
1288  * unlike the blank functions above, so we may sleep.
1289  */
1290 static int sa1100fb_suspend(struct platform_device *dev, pm_message_t state)
1291 {
1292         struct sa1100fb_info *fbi = platform_get_drvdata(dev);
1293
1294         set_ctrlr_state(fbi, C_DISABLE_PM);
1295         return 0;
1296 }
1297
1298 static int sa1100fb_resume(struct platform_device *dev)
1299 {
1300         struct sa1100fb_info *fbi = platform_get_drvdata(dev);
1301
1302         set_ctrlr_state(fbi, C_ENABLE_PM);
1303         return 0;
1304 }
1305 #else
1306 #define sa1100fb_suspend        NULL
1307 #define sa1100fb_resume         NULL
1308 #endif
1309
1310 /*
1311  * sa1100fb_map_video_memory():
1312  *      Allocates the DRAM memory for the frame buffer.  This buffer is  
1313  *      remapped into a non-cached, non-buffered, memory region to  
1314  *      allow palette and pixel writes to occur without flushing the 
1315  *      cache.  Once this area is remapped, all virtual memory
1316  *      access to the video memory should occur at the new region.
1317  */
1318 static int __devinit sa1100fb_map_video_memory(struct sa1100fb_info *fbi)
1319 {
1320         /*
1321          * We reserve one page for the palette, plus the size
1322          * of the framebuffer.
1323          */
1324         fbi->map_size = PAGE_ALIGN(fbi->fb.fix.smem_len + PAGE_SIZE);
1325         fbi->map_cpu = dma_alloc_writecombine(fbi->dev, fbi->map_size,
1326                                               &fbi->map_dma, GFP_KERNEL);
1327
1328         if (fbi->map_cpu) {
1329                 fbi->fb.screen_base = fbi->map_cpu + PAGE_SIZE;
1330                 fbi->screen_dma = fbi->map_dma + PAGE_SIZE;
1331                 /*
1332                  * FIXME: this is actually the wrong thing to place in
1333                  * smem_start.  But fbdev suffers from the problem that
1334                  * it needs an API which doesn't exist (in this case,
1335                  * dma_writecombine_mmap)
1336                  */
1337                 fbi->fb.fix.smem_start = fbi->screen_dma;
1338         }
1339
1340         return fbi->map_cpu ? 0 : -ENOMEM;
1341 }
1342
1343 /* Fake monspecs to fill in fbinfo structure */
1344 static struct fb_monspecs monspecs __devinitdata = {
1345         .hfmin  = 30000,
1346         .hfmax  = 70000,
1347         .vfmin  = 50,
1348         .vfmax  = 65,
1349 };
1350
1351
1352 static struct sa1100fb_info * __devinit sa1100fb_init_fbinfo(struct device *dev)
1353 {
1354         struct sa1100fb_mach_info *inf;
1355         struct sa1100fb_info *fbi;
1356         unsigned i;
1357
1358         fbi = kmalloc(sizeof(struct sa1100fb_info) + sizeof(u32) * 16,
1359                       GFP_KERNEL);
1360         if (!fbi)
1361                 return NULL;
1362
1363         memset(fbi, 0, sizeof(struct sa1100fb_info));
1364         fbi->dev = dev;
1365
1366         strcpy(fbi->fb.fix.id, SA1100_NAME);
1367
1368         fbi->fb.fix.type        = FB_TYPE_PACKED_PIXELS;
1369         fbi->fb.fix.type_aux    = 0;
1370         fbi->fb.fix.xpanstep    = 0;
1371         fbi->fb.fix.ypanstep    = 0;
1372         fbi->fb.fix.ywrapstep   = 0;
1373         fbi->fb.fix.accel       = FB_ACCEL_NONE;
1374
1375         fbi->fb.var.nonstd      = 0;
1376         fbi->fb.var.activate    = FB_ACTIVATE_NOW;
1377         fbi->fb.var.height      = -1;
1378         fbi->fb.var.width       = -1;
1379         fbi->fb.var.accel_flags = 0;
1380         fbi->fb.var.vmode       = FB_VMODE_NONINTERLACED;
1381
1382         fbi->fb.fbops           = &sa1100fb_ops;
1383         fbi->fb.flags           = FBINFO_DEFAULT;
1384         fbi->fb.monspecs        = monspecs;
1385         fbi->fb.pseudo_palette  = (fbi + 1);
1386
1387         fbi->rgb[RGB_4]         = &rgb_4;
1388         fbi->rgb[RGB_8]         = &rgb_8;
1389         fbi->rgb[RGB_16]        = &def_rgb_16;
1390
1391         inf = sa1100fb_get_machine_info(fbi);
1392
1393         /*
1394          * People just don't seem to get this.  We don't support
1395          * anything but correct entries now, so panic if someone
1396          * does something stupid.
1397          */
1398         if (inf->lccr3 & (LCCR3_VrtSnchL|LCCR3_HorSnchL|0xff) ||
1399             inf->pixclock == 0)
1400                 panic("sa1100fb error: invalid LCCR3 fields set or zero "
1401                         "pixclock.");
1402
1403         fbi->max_xres                   = inf->xres;
1404         fbi->fb.var.xres                = inf->xres;
1405         fbi->fb.var.xres_virtual        = inf->xres;
1406         fbi->max_yres                   = inf->yres;
1407         fbi->fb.var.yres                = inf->yres;
1408         fbi->fb.var.yres_virtual        = inf->yres;
1409         fbi->max_bpp                    = inf->bpp;
1410         fbi->fb.var.bits_per_pixel      = inf->bpp;
1411         fbi->fb.var.pixclock            = inf->pixclock;
1412         fbi->fb.var.hsync_len           = inf->hsync_len;
1413         fbi->fb.var.left_margin         = inf->left_margin;
1414         fbi->fb.var.right_margin        = inf->right_margin;
1415         fbi->fb.var.vsync_len           = inf->vsync_len;
1416         fbi->fb.var.upper_margin        = inf->upper_margin;
1417         fbi->fb.var.lower_margin        = inf->lower_margin;
1418         fbi->fb.var.sync                = inf->sync;
1419         fbi->fb.var.grayscale           = inf->cmap_greyscale;
1420         fbi->cmap_inverse               = inf->cmap_inverse;
1421         fbi->cmap_static                = inf->cmap_static;
1422         fbi->lccr0                      = inf->lccr0;
1423         fbi->lccr3                      = inf->lccr3;
1424         fbi->state                      = C_STARTUP;
1425         fbi->task_state                 = (u_char)-1;
1426         fbi->fb.fix.smem_len            = fbi->max_xres * fbi->max_yres *
1427                                           fbi->max_bpp / 8;
1428
1429         /* Copy the RGB bitfield overrides */
1430         for (i = 0; i < NR_RGB; i++)
1431                 if (inf->rgb[i])
1432                         fbi->rgb[i] = inf->rgb[i];
1433
1434         init_waitqueue_head(&fbi->ctrlr_wait);
1435         INIT_WORK(&fbi->task, sa1100fb_task);
1436         mutex_init(&fbi->ctrlr_lock);
1437
1438         return fbi;
1439 }
1440
1441 static int __devinit sa1100fb_probe(struct platform_device *pdev)
1442 {
1443         struct sa1100fb_info *fbi;
1444         int ret, irq;
1445
1446         irq = platform_get_irq(pdev, 0);
1447         if (irq < 0)
1448                 return -EINVAL;
1449
1450         if (!request_mem_region(0xb0100000, 0x10000, "LCD"))
1451                 return -EBUSY;
1452
1453         fbi = sa1100fb_init_fbinfo(&pdev->dev);
1454         ret = -ENOMEM;
1455         if (!fbi)
1456                 goto failed;
1457
1458         /* Initialize video memory */
1459         ret = sa1100fb_map_video_memory(fbi);
1460         if (ret)
1461                 goto failed;
1462
1463         ret = request_irq(irq, sa1100fb_handle_irq, 0, "LCD", fbi);
1464         if (ret) {
1465                 dev_err(&pdev->dev, "request_irq failed: %d\n", ret);
1466                 goto failed;
1467         }
1468
1469 #ifdef ASSABET_PAL_VIDEO
1470         if (machine_is_assabet())
1471                 ASSABET_BCR_clear(ASSABET_BCR_LCD_ON);
1472 #endif
1473
1474         /*
1475          * This makes sure that our colour bitfield
1476          * descriptors are correctly initialised.
1477          */
1478         sa1100fb_check_var(&fbi->fb.var, &fbi->fb);
1479
1480         platform_set_drvdata(pdev, fbi);
1481
1482         ret = register_framebuffer(&fbi->fb);
1483         if (ret < 0)
1484                 goto err_free_irq;
1485
1486 #ifdef CONFIG_CPU_FREQ
1487         fbi->freq_transition.notifier_call = sa1100fb_freq_transition;
1488         fbi->freq_policy.notifier_call = sa1100fb_freq_policy;
1489         cpufreq_register_notifier(&fbi->freq_transition, CPUFREQ_TRANSITION_NOTIFIER);
1490         cpufreq_register_notifier(&fbi->freq_policy, CPUFREQ_POLICY_NOTIFIER);
1491 #endif
1492
1493         /* This driver cannot be unloaded at the moment */
1494         return 0;
1495
1496  err_free_irq:
1497         free_irq(irq, fbi);
1498  failed:
1499         platform_set_drvdata(pdev, NULL);
1500         kfree(fbi);
1501         release_mem_region(0xb0100000, 0x10000);
1502         return ret;
1503 }
1504
1505 static struct platform_driver sa1100fb_driver = {
1506         .probe          = sa1100fb_probe,
1507         .suspend        = sa1100fb_suspend,
1508         .resume         = sa1100fb_resume,
1509         .driver         = {
1510                 .name   = "sa11x0-fb",
1511                 .owner  = THIS_MODULE,
1512         },
1513 };
1514
1515 int __init sa1100fb_init(void)
1516 {
1517         if (fb_get_options("sa1100fb", NULL))
1518                 return -ENODEV;
1519
1520         return platform_driver_register(&sa1100fb_driver);
1521 }
1522
1523 int __init sa1100fb_setup(char *options)
1524 {
1525 #if 0
1526         char *this_opt;
1527
1528         if (!options || !*options)
1529                 return 0;
1530
1531         while ((this_opt = strsep(&options, ",")) != NULL) {
1532
1533                 if (!strncmp(this_opt, "bpp:", 4))
1534                         current_par.max_bpp =
1535                             simple_strtoul(this_opt + 4, NULL, 0);
1536
1537                 if (!strncmp(this_opt, "lccr0:", 6))
1538                         lcd_shadow.lccr0 =
1539                             simple_strtoul(this_opt + 6, NULL, 0);
1540                 if (!strncmp(this_opt, "lccr1:", 6)) {
1541                         lcd_shadow.lccr1 =
1542                             simple_strtoul(this_opt + 6, NULL, 0);
1543                         current_par.max_xres =
1544                             (lcd_shadow.lccr1 & 0x3ff) + 16;
1545                 }
1546                 if (!strncmp(this_opt, "lccr2:", 6)) {
1547                         lcd_shadow.lccr2 =
1548                             simple_strtoul(this_opt + 6, NULL, 0);
1549                         current_par.max_yres =
1550                             (lcd_shadow.
1551                              lccr0 & LCCR0_SDS) ? ((lcd_shadow.
1552                                                     lccr2 & 0x3ff) +
1553                                                    1) *
1554                             2 : ((lcd_shadow.lccr2 & 0x3ff) + 1);
1555                 }
1556                 if (!strncmp(this_opt, "lccr3:", 6))
1557                         lcd_shadow.lccr3 =
1558                             simple_strtoul(this_opt + 6, NULL, 0);
1559         }
1560 #endif
1561         return 0;
1562 }
1563
1564 module_init(sa1100fb_init);
1565 MODULE_DESCRIPTION("StrongARM-1100/1110 framebuffer driver");
1566 MODULE_LICENSE("GPL");