xref: /openbmc/linux/drivers/video/fbdev/sa1100fb.c (revision e638d371)
1f7018c21STomi Valkeinen /*
2f7018c21STomi Valkeinen  *  linux/drivers/video/sa1100fb.c
3f7018c21STomi Valkeinen  *
4f7018c21STomi Valkeinen  *  Copyright (C) 1999 Eric A. Thomas
5f7018c21STomi Valkeinen  *   Based on acornfb.c Copyright (C) Russell King.
6f7018c21STomi Valkeinen  *
7f7018c21STomi Valkeinen  * This file is subject to the terms and conditions of the GNU General Public
8f7018c21STomi Valkeinen  * License.  See the file COPYING in the main directory of this archive for
9f7018c21STomi Valkeinen  * more details.
10f7018c21STomi Valkeinen  *
11f7018c21STomi Valkeinen  *	        StrongARM 1100 LCD Controller Frame Buffer Driver
12f7018c21STomi Valkeinen  *
13f7018c21STomi Valkeinen  * Please direct your questions and comments on this driver to the following
14f7018c21STomi Valkeinen  * email address:
15f7018c21STomi Valkeinen  *
16f7018c21STomi Valkeinen  *	linux-arm-kernel@lists.arm.linux.org.uk
17f7018c21STomi Valkeinen  *
18f7018c21STomi Valkeinen  * Clean patches should be sent to the ARM Linux Patch System.  Please see the
19f7018c21STomi Valkeinen  * following web page for more information:
20f7018c21STomi Valkeinen  *
217c7b2a35SAlexander A. Klimov  *	https://www.arm.linux.org.uk/developer/patches/info.shtml
22f7018c21STomi Valkeinen  *
23f7018c21STomi Valkeinen  * Thank you.
24f7018c21STomi Valkeinen  *
25f7018c21STomi Valkeinen  * Known problems:
26f7018c21STomi Valkeinen  *	- With the Neponset plugged into an Assabet, LCD powerdown
27f7018c21STomi Valkeinen  *	  doesn't work (LCD stays powered up).  Therefore we shouldn't
28f7018c21STomi Valkeinen  *	  blank the screen.
29f7018c21STomi Valkeinen  *	- We don't limit the CPU clock rate nor the mode selection
30f7018c21STomi Valkeinen  *	  according to the available SDRAM bandwidth.
31f7018c21STomi Valkeinen  *
32f7018c21STomi Valkeinen  * Other notes:
33f7018c21STomi Valkeinen  *	- Linear grayscale palettes and the kernel.
34f7018c21STomi Valkeinen  *	  Such code does not belong in the kernel.  The kernel frame buffer
35f7018c21STomi Valkeinen  *	  drivers do not expect a linear colourmap, but a colourmap based on
36f7018c21STomi Valkeinen  *	  the VT100 standard mapping.
37f7018c21STomi Valkeinen  *
38f7018c21STomi Valkeinen  *	  If your _userspace_ requires a linear colourmap, then the setup of
39f7018c21STomi Valkeinen  *	  such a colourmap belongs _in userspace_, not in the kernel.  Code
40f7018c21STomi Valkeinen  *	  to set the colourmap correctly from user space has been sent to
41f7018c21STomi Valkeinen  *	  David Neuer.  It's around 8 lines of C code, plus another 4 to
42f7018c21STomi Valkeinen  *	  detect if we are using grayscale.
43f7018c21STomi Valkeinen  *
44f7018c21STomi Valkeinen  *	- The following must never be specified in a panel definition:
45f7018c21STomi Valkeinen  *	     LCCR0_LtlEnd, LCCR3_PixClkDiv, LCCR3_VrtSnchL, LCCR3_HorSnchL
46f7018c21STomi Valkeinen  *
47f7018c21STomi Valkeinen  *	- The following should be specified:
48f7018c21STomi Valkeinen  *	     either LCCR0_Color or LCCR0_Mono
49f7018c21STomi Valkeinen  *	     either LCCR0_Sngl or LCCR0_Dual
50f7018c21STomi Valkeinen  *	     either LCCR0_Act or LCCR0_Pas
51f7018c21STomi Valkeinen  *	     either LCCR3_OutEnH or LCCD3_OutEnL
52f7018c21STomi Valkeinen  *	     either LCCR3_PixRsEdg or LCCR3_PixFlEdg
53f7018c21STomi Valkeinen  *	     either LCCR3_ACBsDiv or LCCR3_ACBsCntOff
54f7018c21STomi Valkeinen  *
55f7018c21STomi Valkeinen  * Code Status:
56f7018c21STomi Valkeinen  * 1999/04/01:
57f7018c21STomi Valkeinen  *	- Driver appears to be working for Brutus 320x200x8bpp mode.  Other
58f7018c21STomi Valkeinen  *	  resolutions are working, but only the 8bpp mode is supported.
59f7018c21STomi Valkeinen  *	  Changes need to be made to the palette encode and decode routines
60f7018c21STomi Valkeinen  *	  to support 4 and 16 bpp modes.
61f7018c21STomi Valkeinen  *	  Driver is not designed to be a module.  The FrameBuffer is statically
62f7018c21STomi Valkeinen  *	  allocated since dynamic allocation of a 300k buffer cannot be
63f7018c21STomi Valkeinen  *	  guaranteed.
64f7018c21STomi Valkeinen  *
65f7018c21STomi Valkeinen  * 1999/06/17:
66f7018c21STomi Valkeinen  *	- FrameBuffer memory is now allocated at run-time when the
67f7018c21STomi Valkeinen  *	  driver is initialized.
68f7018c21STomi Valkeinen  *
69f7018c21STomi Valkeinen  * 2000/04/10: Nicolas Pitre <nico@fluxnic.net>
70f7018c21STomi Valkeinen  *	- Big cleanup for dynamic selection of machine type at run time.
71f7018c21STomi Valkeinen  *
72f7018c21STomi Valkeinen  * 2000/07/19: Jamey Hicks <jamey@crl.dec.com>
73f7018c21STomi Valkeinen  *	- Support for Bitsy aka Compaq iPAQ H3600 added.
74f7018c21STomi Valkeinen  *
75f7018c21STomi Valkeinen  * 2000/08/07: Tak-Shing Chan <tchan.rd@idthk.com>
76f7018c21STomi Valkeinen  *	       Jeff Sutherland <jsutherland@accelent.com>
77f7018c21STomi Valkeinen  *	- Resolved an issue caused by a change made to the Assabet's PLD
78f7018c21STomi Valkeinen  *	  earlier this year which broke the framebuffer driver for newer
79f7018c21STomi Valkeinen  *	  Phase 4 Assabets.  Some other parameters were changed to optimize
80f7018c21STomi Valkeinen  *	  for the Sharp display.
81f7018c21STomi Valkeinen  *
82f7018c21STomi Valkeinen  * 2000/08/09: Kunihiko IMAI <imai@vasara.co.jp>
83f7018c21STomi Valkeinen  *	- XP860 support added
84f7018c21STomi Valkeinen  *
85f7018c21STomi Valkeinen  * 2000/08/19: Mark Huang <mhuang@livetoy.com>
86f7018c21STomi Valkeinen  *	- Allows standard options to be passed on the kernel command line
87f7018c21STomi Valkeinen  *	  for most common passive displays.
88f7018c21STomi Valkeinen  *
89f7018c21STomi Valkeinen  * 2000/08/29:
90f7018c21STomi Valkeinen  *	- s/save_flags_cli/local_irq_save/
91f7018c21STomi Valkeinen  *	- remove unneeded extra save_flags_cli in sa1100fb_enable_lcd_controller
92f7018c21STomi Valkeinen  *
93f7018c21STomi Valkeinen  * 2000/10/10: Erik Mouw <J.A.K.Mouw@its.tudelft.nl>
94f7018c21STomi Valkeinen  *	- Updated LART stuff. Fixed some minor bugs.
95f7018c21STomi Valkeinen  *
96f7018c21STomi Valkeinen  * 2000/10/30: Murphy Chen <murphy@mail.dialogue.com.tw>
97f7018c21STomi Valkeinen  *	- Pangolin support added
98f7018c21STomi Valkeinen  *
99f7018c21STomi Valkeinen  * 2000/10/31: Roman Jordan <jor@hoeft-wessel.de>
100f7018c21STomi Valkeinen  *	- Huw Webpanel support added
101f7018c21STomi Valkeinen  *
102f7018c21STomi Valkeinen  * 2000/11/23: Eric Peng <ericpeng@coventive.com>
103f7018c21STomi Valkeinen  *	- Freebird add
104f7018c21STomi Valkeinen  *
105f7018c21STomi Valkeinen  * 2001/02/07: Jamey Hicks <jamey.hicks@compaq.com>
106f7018c21STomi Valkeinen  *	       Cliff Brake <cbrake@accelent.com>
107f7018c21STomi Valkeinen  *	- Added PM callback
108f7018c21STomi Valkeinen  *
109f7018c21STomi Valkeinen  * 2001/05/26: <rmk@arm.linux.org.uk>
110f7018c21STomi Valkeinen  *	- Fix 16bpp so that (a) we use the right colours rather than some
111f7018c21STomi Valkeinen  *	  totally random colour depending on what was in page 0, and (b)
112f7018c21STomi Valkeinen  *	  we don't de-reference a NULL pointer.
113f7018c21STomi Valkeinen  *	- remove duplicated implementation of consistent_alloc()
114f7018c21STomi Valkeinen  *	- convert dma address types to dma_addr_t
115f7018c21STomi Valkeinen  *	- remove unused 'montype' stuff
116f7018c21STomi Valkeinen  *	- remove redundant zero inits of init_var after the initial
117f7018c21STomi Valkeinen  *	  memset.
118f7018c21STomi Valkeinen  *	- remove allow_modeset (acornfb idea does not belong here)
119f7018c21STomi Valkeinen  *
120f7018c21STomi Valkeinen  * 2001/05/28: <rmk@arm.linux.org.uk>
121f7018c21STomi Valkeinen  *	- massive cleanup - move machine dependent data into structures
122f7018c21STomi Valkeinen  *	- I've left various #warnings in - if you see one, and know
123f7018c21STomi Valkeinen  *	  the hardware concerned, please get in contact with me.
124f7018c21STomi Valkeinen  *
125f7018c21STomi Valkeinen  * 2001/05/31: <rmk@arm.linux.org.uk>
126f7018c21STomi Valkeinen  *	- Fix LCCR1 HSW value, fix all machine type specifications to
127f7018c21STomi Valkeinen  *	  keep values in line.  (Please check your machine type specs)
128f7018c21STomi Valkeinen  *
129f7018c21STomi Valkeinen  * 2001/06/10: <rmk@arm.linux.org.uk>
130f7018c21STomi Valkeinen  *	- Fiddle with the LCD controller from task context only; mainly
131f7018c21STomi Valkeinen  *	  so that we can run with interrupts on, and sleep.
132f7018c21STomi Valkeinen  *	- Convert #warnings into #errors.  No pain, no gain. ;)
133f7018c21STomi Valkeinen  *
134f7018c21STomi Valkeinen  * 2001/06/14: <rmk@arm.linux.org.uk>
135f7018c21STomi Valkeinen  *	- Make the palette BPS value for 12bpp come out correctly.
136f7018c21STomi Valkeinen  *	- Take notice of "greyscale" on any colour depth.
137f7018c21STomi Valkeinen  *	- Make truecolor visuals use the RGB channel encoding information.
138f7018c21STomi Valkeinen  *
139f7018c21STomi Valkeinen  * 2001/07/02: <rmk@arm.linux.org.uk>
140f7018c21STomi Valkeinen  *	- Fix colourmap problems.
141f7018c21STomi Valkeinen  *
142f7018c21STomi Valkeinen  * 2001/07/13: <abraham@2d3d.co.za>
143f7018c21STomi Valkeinen  *	- Added support for the ICP LCD-Kit01 on LART. This LCD is
144f7018c21STomi Valkeinen  *	  manufactured by Prime View, model no V16C6448AB
145f7018c21STomi Valkeinen  *
146f7018c21STomi Valkeinen  * 2001/07/23: <rmk@arm.linux.org.uk>
147f7018c21STomi Valkeinen  *	- Hand merge version from handhelds.org CVS tree.  See patch
148f7018c21STomi Valkeinen  *	  notes for 595/1 for more information.
149f7018c21STomi Valkeinen  *	- Drop 12bpp (it's 16bpp with different colour register mappings).
150f7018c21STomi Valkeinen  *	- This hardware can not do direct colour.  Therefore we don't
151f7018c21STomi Valkeinen  *	  support it.
152f7018c21STomi Valkeinen  *
153f7018c21STomi Valkeinen  * 2001/07/27: <rmk@arm.linux.org.uk>
154f7018c21STomi Valkeinen  *	- Halve YRES on dual scan LCDs.
155f7018c21STomi Valkeinen  *
156f7018c21STomi Valkeinen  * 2001/08/22: <rmk@arm.linux.org.uk>
157f7018c21STomi Valkeinen  *	- Add b/w iPAQ pixclock value.
158f7018c21STomi Valkeinen  *
159f7018c21STomi Valkeinen  * 2001/10/12: <rmk@arm.linux.org.uk>
160f7018c21STomi Valkeinen  *	- Add patch 681/1 and clean up stork definitions.
161f7018c21STomi Valkeinen  */
162f7018c21STomi Valkeinen 
163f7018c21STomi Valkeinen #include <linux/module.h>
164f7018c21STomi Valkeinen #include <linux/kernel.h>
165f7018c21STomi Valkeinen #include <linux/sched.h>
166f7018c21STomi Valkeinen #include <linux/errno.h>
167f7018c21STomi Valkeinen #include <linux/string.h>
168f7018c21STomi Valkeinen #include <linux/interrupt.h>
169f7018c21STomi Valkeinen #include <linux/slab.h>
170f7018c21STomi Valkeinen #include <linux/mm.h>
171f7018c21STomi Valkeinen #include <linux/fb.h>
172f7018c21STomi Valkeinen #include <linux/delay.h>
173f7018c21STomi Valkeinen #include <linux/init.h>
174f7018c21STomi Valkeinen #include <linux/ioport.h>
175f7018c21STomi Valkeinen #include <linux/cpufreq.h>
1765e4096efSLinus Walleij #include <linux/gpio/consumer.h>
177f7018c21STomi Valkeinen #include <linux/platform_device.h>
178f7018c21STomi Valkeinen #include <linux/dma-mapping.h>
179f7018c21STomi Valkeinen #include <linux/mutex.h>
180f7018c21STomi Valkeinen #include <linux/io.h>
18123834a41SDmitry Eremin-Solenikov #include <linux/clk.h>
182f7018c21STomi Valkeinen 
183f7018c21STomi Valkeinen #include <video/sa1100fb.h>
184f7018c21STomi Valkeinen 
185f7018c21STomi Valkeinen #include <mach/hardware.h>
186f7018c21STomi Valkeinen #include <asm/mach-types.h>
187f7018c21STomi Valkeinen 
188f7018c21STomi Valkeinen /*
189f7018c21STomi Valkeinen  * Complain if VAR is out of range.
190f7018c21STomi Valkeinen  */
191f7018c21STomi Valkeinen #define DEBUG_VAR 1
192f7018c21STomi Valkeinen 
193f7018c21STomi Valkeinen #include "sa1100fb.h"
194f7018c21STomi Valkeinen 
195f7018c21STomi Valkeinen static const struct sa1100fb_rgb rgb_4 = {
196f7018c21STomi Valkeinen 	.red	= { .offset = 0,  .length = 4, },
197f7018c21STomi Valkeinen 	.green	= { .offset = 0,  .length = 4, },
198f7018c21STomi Valkeinen 	.blue	= { .offset = 0,  .length = 4, },
199f7018c21STomi Valkeinen 	.transp	= { .offset = 0,  .length = 0, },
200f7018c21STomi Valkeinen };
201f7018c21STomi Valkeinen 
202f7018c21STomi Valkeinen static const struct sa1100fb_rgb rgb_8 = {
203f7018c21STomi Valkeinen 	.red	= { .offset = 0,  .length = 8, },
204f7018c21STomi Valkeinen 	.green	= { .offset = 0,  .length = 8, },
205f7018c21STomi Valkeinen 	.blue	= { .offset = 0,  .length = 8, },
206f7018c21STomi Valkeinen 	.transp	= { .offset = 0,  .length = 0, },
207f7018c21STomi Valkeinen };
208f7018c21STomi Valkeinen 
209f7018c21STomi Valkeinen static const struct sa1100fb_rgb def_rgb_16 = {
210f7018c21STomi Valkeinen 	.red	= { .offset = 11, .length = 5, },
211f7018c21STomi Valkeinen 	.green	= { .offset = 5,  .length = 6, },
212f7018c21STomi Valkeinen 	.blue	= { .offset = 0,  .length = 5, },
213f7018c21STomi Valkeinen 	.transp	= { .offset = 0,  .length = 0, },
214f7018c21STomi Valkeinen };
215f7018c21STomi Valkeinen 
216f7018c21STomi Valkeinen 
217f7018c21STomi Valkeinen 
218f7018c21STomi Valkeinen static int sa1100fb_activate_var(struct fb_var_screeninfo *var, struct sa1100fb_info *);
219f7018c21STomi Valkeinen static void set_ctrlr_state(struct sa1100fb_info *fbi, u_int state);
220f7018c21STomi Valkeinen 
sa1100fb_schedule_work(struct sa1100fb_info * fbi,u_int state)221f7018c21STomi Valkeinen static inline void sa1100fb_schedule_work(struct sa1100fb_info *fbi, u_int state)
222f7018c21STomi Valkeinen {
223f7018c21STomi Valkeinen 	unsigned long flags;
224f7018c21STomi Valkeinen 
225f7018c21STomi Valkeinen 	local_irq_save(flags);
226f7018c21STomi Valkeinen 	/*
227f7018c21STomi Valkeinen 	 * We need to handle two requests being made at the same time.
228f7018c21STomi Valkeinen 	 * There are two important cases:
229f7018c21STomi Valkeinen 	 *  1. When we are changing VT (C_REENABLE) while unblanking (C_ENABLE)
230f7018c21STomi Valkeinen 	 *     We must perform the unblanking, which will do our REENABLE for us.
231f7018c21STomi Valkeinen 	 *  2. When we are blanking, but immediately unblank before we have
232f7018c21STomi Valkeinen 	 *     blanked.  We do the "REENABLE" thing here as well, just to be sure.
233f7018c21STomi Valkeinen 	 */
234f7018c21STomi Valkeinen 	if (fbi->task_state == C_ENABLE && state == C_REENABLE)
235f7018c21STomi Valkeinen 		state = (u_int) -1;
236f7018c21STomi Valkeinen 	if (fbi->task_state == C_DISABLE && state == C_ENABLE)
237f7018c21STomi Valkeinen 		state = C_REENABLE;
238f7018c21STomi Valkeinen 
239f7018c21STomi Valkeinen 	if (state != (u_int)-1) {
240f7018c21STomi Valkeinen 		fbi->task_state = state;
241f7018c21STomi Valkeinen 		schedule_work(&fbi->task);
242f7018c21STomi Valkeinen 	}
243f7018c21STomi Valkeinen 	local_irq_restore(flags);
244f7018c21STomi Valkeinen }
245f7018c21STomi Valkeinen 
chan_to_field(u_int chan,struct fb_bitfield * bf)246f7018c21STomi Valkeinen static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
247f7018c21STomi Valkeinen {
248f7018c21STomi Valkeinen 	chan &= 0xffff;
249f7018c21STomi Valkeinen 	chan >>= 16 - bf->length;
250f7018c21STomi Valkeinen 	return chan << bf->offset;
251f7018c21STomi Valkeinen }
252f7018c21STomi Valkeinen 
253f7018c21STomi Valkeinen /*
254f7018c21STomi Valkeinen  * Convert bits-per-pixel to a hardware palette PBS value.
255f7018c21STomi Valkeinen  */
palette_pbs(struct fb_var_screeninfo * var)256f7018c21STomi Valkeinen static inline u_int palette_pbs(struct fb_var_screeninfo *var)
257f7018c21STomi Valkeinen {
258f7018c21STomi Valkeinen 	int ret = 0;
259f7018c21STomi Valkeinen 	switch (var->bits_per_pixel) {
260f7018c21STomi Valkeinen 	case 4:  ret = 0 << 12;	break;
261f7018c21STomi Valkeinen 	case 8:  ret = 1 << 12; break;
262f7018c21STomi Valkeinen 	case 16: ret = 2 << 12; break;
263f7018c21STomi Valkeinen 	}
264f7018c21STomi Valkeinen 	return ret;
265f7018c21STomi Valkeinen }
266f7018c21STomi Valkeinen 
267f7018c21STomi Valkeinen static int
sa1100fb_setpalettereg(u_int regno,u_int red,u_int green,u_int blue,u_int trans,struct fb_info * info)268f7018c21STomi Valkeinen sa1100fb_setpalettereg(u_int regno, u_int red, u_int green, u_int blue,
269f7018c21STomi Valkeinen 		       u_int trans, struct fb_info *info)
270f7018c21STomi Valkeinen {
27121f7c247SFabian Frederick 	struct sa1100fb_info *fbi =
27221f7c247SFabian Frederick 		container_of(info, struct sa1100fb_info, fb);
273f7018c21STomi Valkeinen 	u_int val, ret = 1;
274f7018c21STomi Valkeinen 
275f7018c21STomi Valkeinen 	if (regno < fbi->palette_size) {
276f7018c21STomi Valkeinen 		val = ((red >> 4) & 0xf00);
277f7018c21STomi Valkeinen 		val |= ((green >> 8) & 0x0f0);
278f7018c21STomi Valkeinen 		val |= ((blue >> 12) & 0x00f);
279f7018c21STomi Valkeinen 
280f7018c21STomi Valkeinen 		if (regno == 0)
281f7018c21STomi Valkeinen 			val |= palette_pbs(&fbi->fb.var);
282f7018c21STomi Valkeinen 
283f7018c21STomi Valkeinen 		fbi->palette_cpu[regno] = val;
284f7018c21STomi Valkeinen 		ret = 0;
285f7018c21STomi Valkeinen 	}
286f7018c21STomi Valkeinen 	return ret;
287f7018c21STomi Valkeinen }
288f7018c21STomi Valkeinen 
289f7018c21STomi Valkeinen static int
sa1100fb_setcolreg(u_int regno,u_int red,u_int green,u_int blue,u_int trans,struct fb_info * info)290f7018c21STomi Valkeinen sa1100fb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
291f7018c21STomi Valkeinen 		   u_int trans, struct fb_info *info)
292f7018c21STomi Valkeinen {
29321f7c247SFabian Frederick 	struct sa1100fb_info *fbi =
29421f7c247SFabian Frederick 		container_of(info, struct sa1100fb_info, fb);
295f7018c21STomi Valkeinen 	unsigned int val;
296f7018c21STomi Valkeinen 	int ret = 1;
297f7018c21STomi Valkeinen 
298f7018c21STomi Valkeinen 	/*
299f7018c21STomi Valkeinen 	 * If inverse mode was selected, invert all the colours
300f7018c21STomi Valkeinen 	 * rather than the register number.  The register number
301f7018c21STomi Valkeinen 	 * is what you poke into the framebuffer to produce the
302f7018c21STomi Valkeinen 	 * colour you requested.
303f7018c21STomi Valkeinen 	 */
304f7018c21STomi Valkeinen 	if (fbi->inf->cmap_inverse) {
305f7018c21STomi Valkeinen 		red   = 0xffff - red;
306f7018c21STomi Valkeinen 		green = 0xffff - green;
307f7018c21STomi Valkeinen 		blue  = 0xffff - blue;
308f7018c21STomi Valkeinen 	}
309f7018c21STomi Valkeinen 
310f7018c21STomi Valkeinen 	/*
311f7018c21STomi Valkeinen 	 * If greyscale is true, then we convert the RGB value
312f7018c21STomi Valkeinen 	 * to greyscale no mater what visual we are using.
313f7018c21STomi Valkeinen 	 */
314f7018c21STomi Valkeinen 	if (fbi->fb.var.grayscale)
315f7018c21STomi Valkeinen 		red = green = blue = (19595 * red + 38470 * green +
316f7018c21STomi Valkeinen 					7471 * blue) >> 16;
317f7018c21STomi Valkeinen 
318f7018c21STomi Valkeinen 	switch (fbi->fb.fix.visual) {
319f7018c21STomi Valkeinen 	case FB_VISUAL_TRUECOLOR:
320f7018c21STomi Valkeinen 		/*
321f7018c21STomi Valkeinen 		 * 12 or 16-bit True Colour.  We encode the RGB value
322f7018c21STomi Valkeinen 		 * according to the RGB bitfield information.
323f7018c21STomi Valkeinen 		 */
324f7018c21STomi Valkeinen 		if (regno < 16) {
325f7018c21STomi Valkeinen 			val  = chan_to_field(red, &fbi->fb.var.red);
326f7018c21STomi Valkeinen 			val |= chan_to_field(green, &fbi->fb.var.green);
327f7018c21STomi Valkeinen 			val |= chan_to_field(blue, &fbi->fb.var.blue);
328f7018c21STomi Valkeinen 
329cb6bc3ffSRussell King 			fbi->pseudo_palette[regno] = val;
330f7018c21STomi Valkeinen 			ret = 0;
331f7018c21STomi Valkeinen 		}
332f7018c21STomi Valkeinen 		break;
333f7018c21STomi Valkeinen 
334f7018c21STomi Valkeinen 	case FB_VISUAL_STATIC_PSEUDOCOLOR:
335f7018c21STomi Valkeinen 	case FB_VISUAL_PSEUDOCOLOR:
336f7018c21STomi Valkeinen 		ret = sa1100fb_setpalettereg(regno, red, green, blue, trans, info);
337f7018c21STomi Valkeinen 		break;
338f7018c21STomi Valkeinen 	}
339f7018c21STomi Valkeinen 
340f7018c21STomi Valkeinen 	return ret;
341f7018c21STomi Valkeinen }
342f7018c21STomi Valkeinen 
343f7018c21STomi Valkeinen #ifdef CONFIG_CPU_FREQ
344f7018c21STomi Valkeinen /*
345f7018c21STomi Valkeinen  *  sa1100fb_display_dma_period()
346f7018c21STomi Valkeinen  *    Calculate the minimum period (in picoseconds) between two DMA
347f7018c21STomi Valkeinen  *    requests for the LCD controller.  If we hit this, it means we're
348f7018c21STomi Valkeinen  *    doing nothing but LCD DMA.
349f7018c21STomi Valkeinen  */
sa1100fb_display_dma_period(struct fb_var_screeninfo * var)350f7018c21STomi Valkeinen static inline unsigned int sa1100fb_display_dma_period(struct fb_var_screeninfo *var)
351f7018c21STomi Valkeinen {
352f7018c21STomi Valkeinen 	/*
353f7018c21STomi Valkeinen 	 * Period = pixclock * bits_per_byte * bytes_per_transfer
354f7018c21STomi Valkeinen 	 *		/ memory_bits_per_pixel;
355f7018c21STomi Valkeinen 	 */
356f7018c21STomi Valkeinen 	return var->pixclock * 8 * 16 / var->bits_per_pixel;
357f7018c21STomi Valkeinen }
358f7018c21STomi Valkeinen #endif
359f7018c21STomi Valkeinen 
360f7018c21STomi Valkeinen /*
361f7018c21STomi Valkeinen  *  sa1100fb_check_var():
362f7018c21STomi Valkeinen  *    Round up in the following order: bits_per_pixel, xres,
363f7018c21STomi Valkeinen  *    yres, xres_virtual, yres_virtual, xoffset, yoffset, grayscale,
364f7018c21STomi Valkeinen  *    bitfields, horizontal timing, vertical timing.
365f7018c21STomi Valkeinen  */
366f7018c21STomi Valkeinen static int
sa1100fb_check_var(struct fb_var_screeninfo * var,struct fb_info * info)367f7018c21STomi Valkeinen sa1100fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
368f7018c21STomi Valkeinen {
36921f7c247SFabian Frederick 	struct sa1100fb_info *fbi =
37021f7c247SFabian Frederick 		container_of(info, struct sa1100fb_info, fb);
371f7018c21STomi Valkeinen 	int rgbidx;
372f7018c21STomi Valkeinen 
373f7018c21STomi Valkeinen 	if (var->xres < MIN_XRES)
374f7018c21STomi Valkeinen 		var->xres = MIN_XRES;
375f7018c21STomi Valkeinen 	if (var->yres < MIN_YRES)
376f7018c21STomi Valkeinen 		var->yres = MIN_YRES;
377f7018c21STomi Valkeinen 	if (var->xres > fbi->inf->xres)
378f7018c21STomi Valkeinen 		var->xres = fbi->inf->xres;
379f7018c21STomi Valkeinen 	if (var->yres > fbi->inf->yres)
380f7018c21STomi Valkeinen 		var->yres = fbi->inf->yres;
381f7018c21STomi Valkeinen 	var->xres_virtual = max(var->xres_virtual, var->xres);
382f7018c21STomi Valkeinen 	var->yres_virtual = max(var->yres_virtual, var->yres);
383f7018c21STomi Valkeinen 
384f7018c21STomi Valkeinen 	dev_dbg(fbi->dev, "var->bits_per_pixel=%d\n", var->bits_per_pixel);
385f7018c21STomi Valkeinen 	switch (var->bits_per_pixel) {
386f7018c21STomi Valkeinen 	case 4:
387f7018c21STomi Valkeinen 		rgbidx = RGB_4;
388f7018c21STomi Valkeinen 		break;
389f7018c21STomi Valkeinen 	case 8:
390f7018c21STomi Valkeinen 		rgbidx = RGB_8;
391f7018c21STomi Valkeinen 		break;
392f7018c21STomi Valkeinen 	case 16:
393f7018c21STomi Valkeinen 		rgbidx = RGB_16;
394f7018c21STomi Valkeinen 		break;
395f7018c21STomi Valkeinen 	default:
396f7018c21STomi Valkeinen 		return -EINVAL;
397f7018c21STomi Valkeinen 	}
398f7018c21STomi Valkeinen 
399f7018c21STomi Valkeinen 	/*
400f7018c21STomi Valkeinen 	 * Copy the RGB parameters for this display
401f7018c21STomi Valkeinen 	 * from the machine specific parameters.
402f7018c21STomi Valkeinen 	 */
403f7018c21STomi Valkeinen 	var->red    = fbi->rgb[rgbidx]->red;
404f7018c21STomi Valkeinen 	var->green  = fbi->rgb[rgbidx]->green;
405f7018c21STomi Valkeinen 	var->blue   = fbi->rgb[rgbidx]->blue;
406f7018c21STomi Valkeinen 	var->transp = fbi->rgb[rgbidx]->transp;
407f7018c21STomi Valkeinen 
408f7018c21STomi Valkeinen 	dev_dbg(fbi->dev, "RGBT length = %d:%d:%d:%d\n",
409f7018c21STomi Valkeinen 		var->red.length, var->green.length, var->blue.length,
410f7018c21STomi Valkeinen 		var->transp.length);
411f7018c21STomi Valkeinen 
412f7018c21STomi Valkeinen 	dev_dbg(fbi->dev, "RGBT offset = %d:%d:%d:%d\n",
413f7018c21STomi Valkeinen 		var->red.offset, var->green.offset, var->blue.offset,
414f7018c21STomi Valkeinen 		var->transp.offset);
415f7018c21STomi Valkeinen 
416f7018c21STomi Valkeinen #ifdef CONFIG_CPU_FREQ
41723834a41SDmitry Eremin-Solenikov 	dev_dbg(fbi->dev, "dma period = %d ps, clock = %ld kHz\n",
418f7018c21STomi Valkeinen 		sa1100fb_display_dma_period(var),
41923834a41SDmitry Eremin-Solenikov 		clk_get_rate(fbi->clk) / 1000);
420f7018c21STomi Valkeinen #endif
421f7018c21STomi Valkeinen 
422f7018c21STomi Valkeinen 	return 0;
423f7018c21STomi Valkeinen }
424f7018c21STomi Valkeinen 
sa1100fb_set_visual(struct sa1100fb_info * fbi,u32 visual)425f7018c21STomi Valkeinen static void sa1100fb_set_visual(struct sa1100fb_info *fbi, u32 visual)
426f7018c21STomi Valkeinen {
427f7018c21STomi Valkeinen 	if (fbi->inf->set_visual)
428f7018c21STomi Valkeinen 		fbi->inf->set_visual(visual);
429f7018c21STomi Valkeinen }
430f7018c21STomi Valkeinen 
431f7018c21STomi Valkeinen /*
432f7018c21STomi Valkeinen  * sa1100fb_set_par():
433f7018c21STomi Valkeinen  *	Set the user defined part of the display for the specified console
434f7018c21STomi Valkeinen  */
sa1100fb_set_par(struct fb_info * info)435f7018c21STomi Valkeinen static int sa1100fb_set_par(struct fb_info *info)
436f7018c21STomi Valkeinen {
43721f7c247SFabian Frederick 	struct sa1100fb_info *fbi =
43821f7c247SFabian Frederick 		container_of(info, struct sa1100fb_info, fb);
439f7018c21STomi Valkeinen 	struct fb_var_screeninfo *var = &info->var;
440f7018c21STomi Valkeinen 	unsigned long palette_mem_size;
441f7018c21STomi Valkeinen 
442f7018c21STomi Valkeinen 	dev_dbg(fbi->dev, "set_par\n");
443f7018c21STomi Valkeinen 
444f7018c21STomi Valkeinen 	if (var->bits_per_pixel == 16)
445f7018c21STomi Valkeinen 		fbi->fb.fix.visual = FB_VISUAL_TRUECOLOR;
446f7018c21STomi Valkeinen 	else if (!fbi->inf->cmap_static)
447f7018c21STomi Valkeinen 		fbi->fb.fix.visual = FB_VISUAL_PSEUDOCOLOR;
448f7018c21STomi Valkeinen 	else {
449f7018c21STomi Valkeinen 		/*
450f7018c21STomi Valkeinen 		 * Some people have weird ideas about wanting static
451f7018c21STomi Valkeinen 		 * pseudocolor maps.  I suspect their user space
452f7018c21STomi Valkeinen 		 * applications are broken.
453f7018c21STomi Valkeinen 		 */
454f7018c21STomi Valkeinen 		fbi->fb.fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
455f7018c21STomi Valkeinen 	}
456f7018c21STomi Valkeinen 
457f7018c21STomi Valkeinen 	fbi->fb.fix.line_length = var->xres_virtual *
458f7018c21STomi Valkeinen 				  var->bits_per_pixel / 8;
459f7018c21STomi Valkeinen 	fbi->palette_size = var->bits_per_pixel == 8 ? 256 : 16;
460f7018c21STomi Valkeinen 
461f7018c21STomi Valkeinen 	palette_mem_size = fbi->palette_size * sizeof(u16);
462f7018c21STomi Valkeinen 
463f7018c21STomi Valkeinen 	dev_dbg(fbi->dev, "palette_mem_size = 0x%08lx\n", palette_mem_size);
464f7018c21STomi Valkeinen 
465f7018c21STomi Valkeinen 	fbi->palette_cpu = (u16 *)(fbi->map_cpu + PAGE_SIZE - palette_mem_size);
466f7018c21STomi Valkeinen 	fbi->palette_dma = fbi->map_dma + PAGE_SIZE - palette_mem_size;
467f7018c21STomi Valkeinen 
468f7018c21STomi Valkeinen 	/*
469f7018c21STomi Valkeinen 	 * Set (any) board control register to handle new color depth
470f7018c21STomi Valkeinen 	 */
471f7018c21STomi Valkeinen 	sa1100fb_set_visual(fbi, fbi->fb.fix.visual);
472f7018c21STomi Valkeinen 	sa1100fb_activate_var(var, fbi);
473f7018c21STomi Valkeinen 
474f7018c21STomi Valkeinen 	return 0;
475f7018c21STomi Valkeinen }
476f7018c21STomi Valkeinen 
477f7018c21STomi Valkeinen #if 0
478f7018c21STomi Valkeinen static int
479f7018c21STomi Valkeinen sa1100fb_set_cmap(struct fb_cmap *cmap, int kspc, int con,
480f7018c21STomi Valkeinen 		  struct fb_info *info)
481f7018c21STomi Valkeinen {
482f7018c21STomi Valkeinen 	struct sa1100fb_info *fbi = (struct sa1100fb_info *)info;
483f7018c21STomi Valkeinen 
484f7018c21STomi Valkeinen 	/*
485f7018c21STomi Valkeinen 	 * Make sure the user isn't doing something stupid.
486f7018c21STomi Valkeinen 	 */
487f7018c21STomi Valkeinen 	if (!kspc && (fbi->fb.var.bits_per_pixel == 16 || fbi->inf->cmap_static))
488f7018c21STomi Valkeinen 		return -EINVAL;
489f7018c21STomi Valkeinen 
490f7018c21STomi Valkeinen 	return gen_set_cmap(cmap, kspc, con, info);
491f7018c21STomi Valkeinen }
492f7018c21STomi Valkeinen #endif
493f7018c21STomi Valkeinen 
494f7018c21STomi Valkeinen /*
495f7018c21STomi Valkeinen  * Formal definition of the VESA spec:
496f7018c21STomi Valkeinen  *  On
497f7018c21STomi Valkeinen  *  	This refers to the state of the display when it is in full operation
498f7018c21STomi Valkeinen  *  Stand-By
499f7018c21STomi Valkeinen  *  	This defines an optional operating state of minimal power reduction with
500f7018c21STomi Valkeinen  *  	the shortest recovery time
501f7018c21STomi Valkeinen  *  Suspend
502f7018c21STomi Valkeinen  *  	This refers to a level of power management in which substantial power
503f7018c21STomi Valkeinen  *  	reduction is achieved by the display.  The display can have a longer
504f7018c21STomi Valkeinen  *  	recovery time from this state than from the Stand-by state
505f7018c21STomi Valkeinen  *  Off
506f7018c21STomi Valkeinen  *  	This indicates that the display is consuming the lowest level of power
507f7018c21STomi Valkeinen  *  	and is non-operational. Recovery from this state may optionally require
508f7018c21STomi Valkeinen  *  	the user to manually power on the monitor
509f7018c21STomi Valkeinen  *
510f7018c21STomi Valkeinen  *  Now, the fbdev driver adds an additional state, (blank), where they
511f7018c21STomi Valkeinen  *  turn off the video (maybe by colormap tricks), but don't mess with the
512f7018c21STomi Valkeinen  *  video itself: think of it semantically between on and Stand-By.
513f7018c21STomi Valkeinen  *
514f7018c21STomi Valkeinen  *  So here's what we should do in our fbdev blank routine:
515f7018c21STomi Valkeinen  *
516f7018c21STomi Valkeinen  *  	VESA_NO_BLANKING (mode 0)	Video on,  front/back light on
517f7018c21STomi Valkeinen  *  	VESA_VSYNC_SUSPEND (mode 1)  	Video on,  front/back light off
518f7018c21STomi Valkeinen  *  	VESA_HSYNC_SUSPEND (mode 2)  	Video on,  front/back light off
519f7018c21STomi Valkeinen  *  	VESA_POWERDOWN (mode 3)		Video off, front/back light off
520f7018c21STomi Valkeinen  *
521f7018c21STomi Valkeinen  *  This will match the matrox implementation.
522f7018c21STomi Valkeinen  */
523f7018c21STomi Valkeinen /*
524f7018c21STomi Valkeinen  * sa1100fb_blank():
525f7018c21STomi Valkeinen  *	Blank the display by setting all palette values to zero.  Note, the
526f7018c21STomi Valkeinen  * 	12 and 16 bpp modes don't really use the palette, so this will not
527f7018c21STomi Valkeinen  *      blank the display in all modes.
528f7018c21STomi Valkeinen  */
sa1100fb_blank(int blank,struct fb_info * info)529f7018c21STomi Valkeinen static int sa1100fb_blank(int blank, struct fb_info *info)
530f7018c21STomi Valkeinen {
53121f7c247SFabian Frederick 	struct sa1100fb_info *fbi =
53221f7c247SFabian Frederick 		container_of(info, struct sa1100fb_info, fb);
533f7018c21STomi Valkeinen 	int i;
534f7018c21STomi Valkeinen 
535f7018c21STomi Valkeinen 	dev_dbg(fbi->dev, "sa1100fb_blank: blank=%d\n", blank);
536f7018c21STomi Valkeinen 
537f7018c21STomi Valkeinen 	switch (blank) {
538f7018c21STomi Valkeinen 	case FB_BLANK_POWERDOWN:
539f7018c21STomi Valkeinen 	case FB_BLANK_VSYNC_SUSPEND:
540f7018c21STomi Valkeinen 	case FB_BLANK_HSYNC_SUSPEND:
541f7018c21STomi Valkeinen 	case FB_BLANK_NORMAL:
542f7018c21STomi Valkeinen 		if (fbi->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR ||
543f7018c21STomi Valkeinen 		    fbi->fb.fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
544f7018c21STomi Valkeinen 			for (i = 0; i < fbi->palette_size; i++)
545f7018c21STomi Valkeinen 				sa1100fb_setpalettereg(i, 0, 0, 0, 0, info);
546f7018c21STomi Valkeinen 		sa1100fb_schedule_work(fbi, C_DISABLE);
547f7018c21STomi Valkeinen 		break;
548f7018c21STomi Valkeinen 
549f7018c21STomi Valkeinen 	case FB_BLANK_UNBLANK:
550f7018c21STomi Valkeinen 		if (fbi->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR ||
551f7018c21STomi Valkeinen 		    fbi->fb.fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
552f7018c21STomi Valkeinen 			fb_set_cmap(&fbi->fb.cmap, info);
553f7018c21STomi Valkeinen 		sa1100fb_schedule_work(fbi, C_ENABLE);
554f7018c21STomi Valkeinen 	}
555f7018c21STomi Valkeinen 	return 0;
556f7018c21STomi Valkeinen }
557f7018c21STomi Valkeinen 
sa1100fb_mmap(struct fb_info * info,struct vm_area_struct * vma)558f7018c21STomi Valkeinen static int sa1100fb_mmap(struct fb_info *info,
559f7018c21STomi Valkeinen 			 struct vm_area_struct *vma)
560f7018c21STomi Valkeinen {
56121f7c247SFabian Frederick 	struct sa1100fb_info *fbi =
56221f7c247SFabian Frederick 		container_of(info, struct sa1100fb_info, fb);
563f7018c21STomi Valkeinen 	unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
564f7018c21STomi Valkeinen 
565f7018c21STomi Valkeinen 	if (off < info->fix.smem_len) {
566f7018c21STomi Valkeinen 		vma->vm_pgoff += 1; /* skip over the palette */
567f6e45661SLuis R. Rodriguez 		return dma_mmap_wc(fbi->dev, vma, fbi->map_cpu, fbi->map_dma,
568f6e45661SLuis R. Rodriguez 				   fbi->map_size);
569f7018c21STomi Valkeinen 	}
570f7018c21STomi Valkeinen 
571f7018c21STomi Valkeinen 	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
572f7018c21STomi Valkeinen 
573f7018c21STomi Valkeinen 	return vm_iomap_memory(vma, info->fix.mmio_start, info->fix.mmio_len);
574f7018c21STomi Valkeinen }
575f7018c21STomi Valkeinen 
5768a48ac33SJani Nikula static const struct fb_ops sa1100fb_ops = {
577f7018c21STomi Valkeinen 	.owner		= THIS_MODULE,
578f7018c21STomi Valkeinen 	.fb_check_var	= sa1100fb_check_var,
579f7018c21STomi Valkeinen 	.fb_set_par	= sa1100fb_set_par,
580f7018c21STomi Valkeinen //	.fb_set_cmap	= sa1100fb_set_cmap,
581f7018c21STomi Valkeinen 	.fb_setcolreg	= sa1100fb_setcolreg,
582f7018c21STomi Valkeinen 	.fb_fillrect	= cfb_fillrect,
583f7018c21STomi Valkeinen 	.fb_copyarea	= cfb_copyarea,
584f7018c21STomi Valkeinen 	.fb_imageblit	= cfb_imageblit,
585f7018c21STomi Valkeinen 	.fb_blank	= sa1100fb_blank,
586f7018c21STomi Valkeinen 	.fb_mmap	= sa1100fb_mmap,
587f7018c21STomi Valkeinen };
588f7018c21STomi Valkeinen 
589f7018c21STomi Valkeinen /*
590f7018c21STomi Valkeinen  * Calculate the PCD value from the clock rate (in picoseconds).
591f7018c21STomi Valkeinen  * We take account of the PPCR clock setting.
592f7018c21STomi Valkeinen  */
get_pcd(struct sa1100fb_info * fbi,unsigned int pixclock)59323834a41SDmitry Eremin-Solenikov static inline unsigned int get_pcd(struct sa1100fb_info *fbi,
59423834a41SDmitry Eremin-Solenikov 		unsigned int pixclock)
595f7018c21STomi Valkeinen {
59623834a41SDmitry Eremin-Solenikov 	unsigned int pcd = clk_get_rate(fbi->clk) / 100 / 1000;
597f7018c21STomi Valkeinen 
598f7018c21STomi Valkeinen 	pcd *= pixclock;
599f7018c21STomi Valkeinen 	pcd /= 10000000;
600f7018c21STomi Valkeinen 
601f7018c21STomi Valkeinen 	return pcd + 1;	/* make up for integer math truncations */
602f7018c21STomi Valkeinen }
603f7018c21STomi Valkeinen 
604f7018c21STomi Valkeinen /*
605f7018c21STomi Valkeinen  * sa1100fb_activate_var():
606f7018c21STomi Valkeinen  *	Configures LCD Controller based on entries in var parameter.  Settings are
607f7018c21STomi Valkeinen  *	only written to the controller if changes were made.
608f7018c21STomi Valkeinen  */
sa1100fb_activate_var(struct fb_var_screeninfo * var,struct sa1100fb_info * fbi)609f7018c21STomi Valkeinen static int sa1100fb_activate_var(struct fb_var_screeninfo *var, struct sa1100fb_info *fbi)
610f7018c21STomi Valkeinen {
611f7018c21STomi Valkeinen 	struct sa1100fb_lcd_reg new_regs;
612f7018c21STomi Valkeinen 	u_int half_screen_size, yres, pcd;
613f7018c21STomi Valkeinen 	u_long flags;
614f7018c21STomi Valkeinen 
615f7018c21STomi Valkeinen 	dev_dbg(fbi->dev, "Configuring SA1100 LCD\n");
616f7018c21STomi Valkeinen 
617f7018c21STomi Valkeinen 	dev_dbg(fbi->dev, "var: xres=%d hslen=%d lm=%d rm=%d\n",
618f7018c21STomi Valkeinen 		var->xres, var->hsync_len,
619f7018c21STomi Valkeinen 		var->left_margin, var->right_margin);
620f7018c21STomi Valkeinen 	dev_dbg(fbi->dev, "var: yres=%d vslen=%d um=%d bm=%d\n",
621f7018c21STomi Valkeinen 		var->yres, var->vsync_len,
622f7018c21STomi Valkeinen 		var->upper_margin, var->lower_margin);
623f7018c21STomi Valkeinen 
624f7018c21STomi Valkeinen #if DEBUG_VAR
625f7018c21STomi Valkeinen 	if (var->xres < 16        || var->xres > 1024)
626f7018c21STomi Valkeinen 		dev_err(fbi->dev, "%s: invalid xres %d\n",
627f7018c21STomi Valkeinen 			fbi->fb.fix.id, var->xres);
628f7018c21STomi Valkeinen 	if (var->hsync_len < 1    || var->hsync_len > 64)
629f7018c21STomi Valkeinen 		dev_err(fbi->dev, "%s: invalid hsync_len %d\n",
630f7018c21STomi Valkeinen 			fbi->fb.fix.id, var->hsync_len);
631f7018c21STomi Valkeinen 	if (var->left_margin < 1  || var->left_margin > 255)
632f7018c21STomi Valkeinen 		dev_err(fbi->dev, "%s: invalid left_margin %d\n",
633f7018c21STomi Valkeinen 			fbi->fb.fix.id, var->left_margin);
634f7018c21STomi Valkeinen 	if (var->right_margin < 1 || var->right_margin > 255)
635f7018c21STomi Valkeinen 		dev_err(fbi->dev, "%s: invalid right_margin %d\n",
636f7018c21STomi Valkeinen 			fbi->fb.fix.id, var->right_margin);
637f7018c21STomi Valkeinen 	if (var->yres < 1         || var->yres > 1024)
638f7018c21STomi Valkeinen 		dev_err(fbi->dev, "%s: invalid yres %d\n",
639f7018c21STomi Valkeinen 			fbi->fb.fix.id, var->yres);
640f7018c21STomi Valkeinen 	if (var->vsync_len < 1    || var->vsync_len > 64)
641f7018c21STomi Valkeinen 		dev_err(fbi->dev, "%s: invalid vsync_len %d\n",
642f7018c21STomi Valkeinen 			fbi->fb.fix.id, var->vsync_len);
643f7018c21STomi Valkeinen 	if (var->upper_margin < 0 || var->upper_margin > 255)
644f7018c21STomi Valkeinen 		dev_err(fbi->dev, "%s: invalid upper_margin %d\n",
645f7018c21STomi Valkeinen 			fbi->fb.fix.id, var->upper_margin);
646f7018c21STomi Valkeinen 	if (var->lower_margin < 0 || var->lower_margin > 255)
647f7018c21STomi Valkeinen 		dev_err(fbi->dev, "%s: invalid lower_margin %d\n",
648f7018c21STomi Valkeinen 			fbi->fb.fix.id, var->lower_margin);
649f7018c21STomi Valkeinen #endif
650f7018c21STomi Valkeinen 
651f7018c21STomi Valkeinen 	new_regs.lccr0 = fbi->inf->lccr0 |
652f7018c21STomi Valkeinen 		LCCR0_LEN | LCCR0_LDM | LCCR0_BAM |
653f7018c21STomi Valkeinen 		LCCR0_ERM | LCCR0_LtlEnd | LCCR0_DMADel(0);
654f7018c21STomi Valkeinen 
655f7018c21STomi Valkeinen 	new_regs.lccr1 =
656f7018c21STomi Valkeinen 		LCCR1_DisWdth(var->xres) +
657f7018c21STomi Valkeinen 		LCCR1_HorSnchWdth(var->hsync_len) +
658f7018c21STomi Valkeinen 		LCCR1_BegLnDel(var->left_margin) +
659f7018c21STomi Valkeinen 		LCCR1_EndLnDel(var->right_margin);
660f7018c21STomi Valkeinen 
661f7018c21STomi Valkeinen 	/*
662f7018c21STomi Valkeinen 	 * If we have a dual scan LCD, then we need to halve
663f7018c21STomi Valkeinen 	 * the YRES parameter.
664f7018c21STomi Valkeinen 	 */
665f7018c21STomi Valkeinen 	yres = var->yres;
666f7018c21STomi Valkeinen 	if (fbi->inf->lccr0 & LCCR0_Dual)
667f7018c21STomi Valkeinen 		yres /= 2;
668f7018c21STomi Valkeinen 
669f7018c21STomi Valkeinen 	new_regs.lccr2 =
670f7018c21STomi Valkeinen 		LCCR2_DisHght(yres) +
671f7018c21STomi Valkeinen 		LCCR2_VrtSnchWdth(var->vsync_len) +
672f7018c21STomi Valkeinen 		LCCR2_BegFrmDel(var->upper_margin) +
673f7018c21STomi Valkeinen 		LCCR2_EndFrmDel(var->lower_margin);
674f7018c21STomi Valkeinen 
67523834a41SDmitry Eremin-Solenikov 	pcd = get_pcd(fbi, var->pixclock);
676f7018c21STomi Valkeinen 	new_regs.lccr3 = LCCR3_PixClkDiv(pcd) | fbi->inf->lccr3 |
677f7018c21STomi Valkeinen 		(var->sync & FB_SYNC_HOR_HIGH_ACT ? LCCR3_HorSnchH : LCCR3_HorSnchL) |
678f7018c21STomi Valkeinen 		(var->sync & FB_SYNC_VERT_HIGH_ACT ? LCCR3_VrtSnchH : LCCR3_VrtSnchL);
679f7018c21STomi Valkeinen 
680f7018c21STomi Valkeinen 	dev_dbg(fbi->dev, "nlccr0 = 0x%08lx\n", new_regs.lccr0);
681f7018c21STomi Valkeinen 	dev_dbg(fbi->dev, "nlccr1 = 0x%08lx\n", new_regs.lccr1);
682f7018c21STomi Valkeinen 	dev_dbg(fbi->dev, "nlccr2 = 0x%08lx\n", new_regs.lccr2);
683f7018c21STomi Valkeinen 	dev_dbg(fbi->dev, "nlccr3 = 0x%08lx\n", new_regs.lccr3);
684f7018c21STomi Valkeinen 
685f7018c21STomi Valkeinen 	half_screen_size = var->bits_per_pixel;
686f7018c21STomi Valkeinen 	half_screen_size = half_screen_size * var->xres * var->yres / 16;
687f7018c21STomi Valkeinen 
688f7018c21STomi Valkeinen 	/* Update shadow copy atomically */
689f7018c21STomi Valkeinen 	local_irq_save(flags);
690f7018c21STomi Valkeinen 	fbi->dbar1 = fbi->palette_dma;
691f7018c21STomi Valkeinen 	fbi->dbar2 = fbi->screen_dma + half_screen_size;
692f7018c21STomi Valkeinen 
693f7018c21STomi Valkeinen 	fbi->reg_lccr0 = new_regs.lccr0;
694f7018c21STomi Valkeinen 	fbi->reg_lccr1 = new_regs.lccr1;
695f7018c21STomi Valkeinen 	fbi->reg_lccr2 = new_regs.lccr2;
696f7018c21STomi Valkeinen 	fbi->reg_lccr3 = new_regs.lccr3;
697f7018c21STomi Valkeinen 	local_irq_restore(flags);
698f7018c21STomi Valkeinen 
699f7018c21STomi Valkeinen 	/*
700f7018c21STomi Valkeinen 	 * Only update the registers if the controller is enabled
701f7018c21STomi Valkeinen 	 * and something has changed.
702f7018c21STomi Valkeinen 	 */
703f7018c21STomi Valkeinen 	if (readl_relaxed(fbi->base + LCCR0) != fbi->reg_lccr0 ||
704f7018c21STomi Valkeinen 	    readl_relaxed(fbi->base + LCCR1) != fbi->reg_lccr1 ||
705f7018c21STomi Valkeinen 	    readl_relaxed(fbi->base + LCCR2) != fbi->reg_lccr2 ||
706f7018c21STomi Valkeinen 	    readl_relaxed(fbi->base + LCCR3) != fbi->reg_lccr3 ||
707f7018c21STomi Valkeinen 	    readl_relaxed(fbi->base + DBAR1) != fbi->dbar1 ||
708f7018c21STomi Valkeinen 	    readl_relaxed(fbi->base + DBAR2) != fbi->dbar2)
709f7018c21STomi Valkeinen 		sa1100fb_schedule_work(fbi, C_REENABLE);
710f7018c21STomi Valkeinen 
711f7018c21STomi Valkeinen 	return 0;
712f7018c21STomi Valkeinen }
713f7018c21STomi Valkeinen 
714f7018c21STomi Valkeinen /*
715f7018c21STomi Valkeinen  * NOTE!  The following functions are purely helpers for set_ctrlr_state.
716f7018c21STomi Valkeinen  * Do not call them directly; set_ctrlr_state does the correct serialisation
717f7018c21STomi Valkeinen  * to ensure that things happen in the right way 100% of time time.
718f7018c21STomi Valkeinen  *	-- rmk
719f7018c21STomi Valkeinen  */
__sa1100fb_backlight_power(struct sa1100fb_info * fbi,int on)720f7018c21STomi Valkeinen static inline void __sa1100fb_backlight_power(struct sa1100fb_info *fbi, int on)
721f7018c21STomi Valkeinen {
722f7018c21STomi Valkeinen 	dev_dbg(fbi->dev, "backlight o%s\n", on ? "n" : "ff");
723f7018c21STomi Valkeinen 
724f7018c21STomi Valkeinen 	if (fbi->inf->backlight_power)
725f7018c21STomi Valkeinen 		fbi->inf->backlight_power(on);
726f7018c21STomi Valkeinen }
727f7018c21STomi Valkeinen 
__sa1100fb_lcd_power(struct sa1100fb_info * fbi,int on)728f7018c21STomi Valkeinen static inline void __sa1100fb_lcd_power(struct sa1100fb_info *fbi, int on)
729f7018c21STomi Valkeinen {
730f7018c21STomi Valkeinen 	dev_dbg(fbi->dev, "LCD power o%s\n", on ? "n" : "ff");
731f7018c21STomi Valkeinen 
732f7018c21STomi Valkeinen 	if (fbi->inf->lcd_power)
733f7018c21STomi Valkeinen 		fbi->inf->lcd_power(on);
734f7018c21STomi Valkeinen }
735f7018c21STomi Valkeinen 
sa1100fb_setup_gpio(struct sa1100fb_info * fbi)736f7018c21STomi Valkeinen static void sa1100fb_setup_gpio(struct sa1100fb_info *fbi)
737f7018c21STomi Valkeinen {
738f7018c21STomi Valkeinen 	u_int mask = 0;
739f7018c21STomi Valkeinen 
740f7018c21STomi Valkeinen 	/*
741f7018c21STomi Valkeinen 	 * Enable GPIO<9:2> for LCD use if:
742f7018c21STomi Valkeinen 	 *  1. Active display, or
743f7018c21STomi Valkeinen 	 *  2. Color Dual Passive display
744f7018c21STomi Valkeinen 	 *
745f7018c21STomi Valkeinen 	 * see table 11.8 on page 11-27 in the SA1100 manual
746f7018c21STomi Valkeinen 	 *   -- Erik.
747f7018c21STomi Valkeinen 	 *
748f7018c21STomi Valkeinen 	 * SA1110 spec update nr. 25 says we can and should
749f7018c21STomi Valkeinen 	 * clear LDD15 to 12 for 4 or 8bpp modes with active
750f7018c21STomi Valkeinen 	 * panels.
751f7018c21STomi Valkeinen 	 */
752f7018c21STomi Valkeinen 	if ((fbi->reg_lccr0 & LCCR0_CMS) == LCCR0_Color &&
753f7018c21STomi Valkeinen 	    (fbi->reg_lccr0 & (LCCR0_Dual|LCCR0_Act)) != 0) {
754f7018c21STomi Valkeinen 		mask = GPIO_LDD11 | GPIO_LDD10 | GPIO_LDD9  | GPIO_LDD8;
755f7018c21STomi Valkeinen 
756f7018c21STomi Valkeinen 		if (fbi->fb.var.bits_per_pixel > 8 ||
757f7018c21STomi Valkeinen 		    (fbi->reg_lccr0 & (LCCR0_Dual|LCCR0_Act)) == LCCR0_Dual)
758f7018c21STomi Valkeinen 			mask |= GPIO_LDD15 | GPIO_LDD14 | GPIO_LDD13 | GPIO_LDD12;
759f7018c21STomi Valkeinen 
760f7018c21STomi Valkeinen 	}
761f7018c21STomi Valkeinen 
762f7018c21STomi Valkeinen 	if (mask) {
763f7018c21STomi Valkeinen 		unsigned long flags;
764f7018c21STomi Valkeinen 
765f7018c21STomi Valkeinen 		/*
766f7018c21STomi Valkeinen 		 * SA-1100 requires the GPIO direction register set
767f7018c21STomi Valkeinen 		 * appropriately for the alternate function.  Hence
768f7018c21STomi Valkeinen 		 * we set it here via bitmask rather than excessive
769f7018c21STomi Valkeinen 		 * fiddling via the GPIO subsystem - and even then
770f7018c21STomi Valkeinen 		 * we'll still have to deal with GAFR.
771f7018c21STomi Valkeinen 		 */
772f7018c21STomi Valkeinen 		local_irq_save(flags);
773f7018c21STomi Valkeinen 		GPDR |= mask;
774f7018c21STomi Valkeinen 		GAFR |= mask;
775f7018c21STomi Valkeinen 		local_irq_restore(flags);
776f7018c21STomi Valkeinen 	}
777f7018c21STomi Valkeinen }
778f7018c21STomi Valkeinen 
sa1100fb_enable_controller(struct sa1100fb_info * fbi)779f7018c21STomi Valkeinen static void sa1100fb_enable_controller(struct sa1100fb_info *fbi)
780f7018c21STomi Valkeinen {
781f7018c21STomi Valkeinen 	dev_dbg(fbi->dev, "Enabling LCD controller\n");
782f7018c21STomi Valkeinen 
783f7018c21STomi Valkeinen 	/*
784f7018c21STomi Valkeinen 	 * Make sure the mode bits are present in the first palette entry
785f7018c21STomi Valkeinen 	 */
786f7018c21STomi Valkeinen 	fbi->palette_cpu[0] &= 0xcfff;
787f7018c21STomi Valkeinen 	fbi->palette_cpu[0] |= palette_pbs(&fbi->fb.var);
788f7018c21STomi Valkeinen 
78923834a41SDmitry Eremin-Solenikov 	/* enable LCD controller clock */
79023834a41SDmitry Eremin-Solenikov 	clk_prepare_enable(fbi->clk);
79123834a41SDmitry Eremin-Solenikov 
792f7018c21STomi Valkeinen 	/* Sequence from 11.7.10 */
793f7018c21STomi Valkeinen 	writel_relaxed(fbi->reg_lccr3, fbi->base + LCCR3);
794f7018c21STomi Valkeinen 	writel_relaxed(fbi->reg_lccr2, fbi->base + LCCR2);
795f7018c21STomi Valkeinen 	writel_relaxed(fbi->reg_lccr1, fbi->base + LCCR1);
796f7018c21STomi Valkeinen 	writel_relaxed(fbi->reg_lccr0 & ~LCCR0_LEN, fbi->base + LCCR0);
797f7018c21STomi Valkeinen 	writel_relaxed(fbi->dbar1, fbi->base + DBAR1);
798f7018c21STomi Valkeinen 	writel_relaxed(fbi->dbar2, fbi->base + DBAR2);
799f7018c21STomi Valkeinen 	writel_relaxed(fbi->reg_lccr0 | LCCR0_LEN, fbi->base + LCCR0);
800f7018c21STomi Valkeinen 
8015e4096efSLinus Walleij 	if (fbi->shannon_lcden)
8025e4096efSLinus Walleij 		gpiod_set_value(fbi->shannon_lcden, 1);
803f7018c21STomi Valkeinen 
804f7018c21STomi Valkeinen 	dev_dbg(fbi->dev, "DBAR1: 0x%08x\n", readl_relaxed(fbi->base + DBAR1));
805f7018c21STomi Valkeinen 	dev_dbg(fbi->dev, "DBAR2: 0x%08x\n", readl_relaxed(fbi->base + DBAR2));
806f7018c21STomi Valkeinen 	dev_dbg(fbi->dev, "LCCR0: 0x%08x\n", readl_relaxed(fbi->base + LCCR0));
807f7018c21STomi Valkeinen 	dev_dbg(fbi->dev, "LCCR1: 0x%08x\n", readl_relaxed(fbi->base + LCCR1));
808f7018c21STomi Valkeinen 	dev_dbg(fbi->dev, "LCCR2: 0x%08x\n", readl_relaxed(fbi->base + LCCR2));
809f7018c21STomi Valkeinen 	dev_dbg(fbi->dev, "LCCR3: 0x%08x\n", readl_relaxed(fbi->base + LCCR3));
810f7018c21STomi Valkeinen }
811f7018c21STomi Valkeinen 
sa1100fb_disable_controller(struct sa1100fb_info * fbi)812f7018c21STomi Valkeinen static void sa1100fb_disable_controller(struct sa1100fb_info *fbi)
813f7018c21STomi Valkeinen {
814f7018c21STomi Valkeinen 	DECLARE_WAITQUEUE(wait, current);
815f7018c21STomi Valkeinen 	u32 lccr0;
816f7018c21STomi Valkeinen 
817f7018c21STomi Valkeinen 	dev_dbg(fbi->dev, "Disabling LCD controller\n");
818f7018c21STomi Valkeinen 
8195e4096efSLinus Walleij 	if (fbi->shannon_lcden)
8205e4096efSLinus Walleij 		gpiod_set_value(fbi->shannon_lcden, 0);
821f7018c21STomi Valkeinen 
822f7018c21STomi Valkeinen 	set_current_state(TASK_UNINTERRUPTIBLE);
823f7018c21STomi Valkeinen 	add_wait_queue(&fbi->ctrlr_wait, &wait);
824f7018c21STomi Valkeinen 
825f7018c21STomi Valkeinen 	/* Clear LCD Status Register */
826f7018c21STomi Valkeinen 	writel_relaxed(~0, fbi->base + LCSR);
827f7018c21STomi Valkeinen 
828f7018c21STomi Valkeinen 	lccr0 = readl_relaxed(fbi->base + LCCR0);
829f7018c21STomi Valkeinen 	lccr0 &= ~LCCR0_LDM;	/* Enable LCD Disable Done Interrupt */
830f7018c21STomi Valkeinen 	writel_relaxed(lccr0, fbi->base + LCCR0);
831f7018c21STomi Valkeinen 	lccr0 &= ~LCCR0_LEN;	/* Disable LCD Controller */
832f7018c21STomi Valkeinen 	writel_relaxed(lccr0, fbi->base + LCCR0);
833f7018c21STomi Valkeinen 
834f7018c21STomi Valkeinen 	schedule_timeout(20 * HZ / 1000);
835f7018c21STomi Valkeinen 	remove_wait_queue(&fbi->ctrlr_wait, &wait);
83623834a41SDmitry Eremin-Solenikov 
83723834a41SDmitry Eremin-Solenikov 	/* disable LCD controller clock */
83823834a41SDmitry Eremin-Solenikov 	clk_disable_unprepare(fbi->clk);
839f7018c21STomi Valkeinen }
840f7018c21STomi Valkeinen 
841f7018c21STomi Valkeinen /*
842f7018c21STomi Valkeinen  *  sa1100fb_handle_irq: Handle 'LCD DONE' interrupts.
843f7018c21STomi Valkeinen  */
sa1100fb_handle_irq(int irq,void * dev_id)844f7018c21STomi Valkeinen static irqreturn_t sa1100fb_handle_irq(int irq, void *dev_id)
845f7018c21STomi Valkeinen {
846f7018c21STomi Valkeinen 	struct sa1100fb_info *fbi = dev_id;
847f7018c21STomi Valkeinen 	unsigned int lcsr = readl_relaxed(fbi->base + LCSR);
848f7018c21STomi Valkeinen 
849f7018c21STomi Valkeinen 	if (lcsr & LCSR_LDD) {
850f7018c21STomi Valkeinen 		u32 lccr0 = readl_relaxed(fbi->base + LCCR0) | LCCR0_LDM;
851f7018c21STomi Valkeinen 		writel_relaxed(lccr0, fbi->base + LCCR0);
852f7018c21STomi Valkeinen 		wake_up(&fbi->ctrlr_wait);
853f7018c21STomi Valkeinen 	}
854f7018c21STomi Valkeinen 
855f7018c21STomi Valkeinen 	writel_relaxed(lcsr, fbi->base + LCSR);
856f7018c21STomi Valkeinen 	return IRQ_HANDLED;
857f7018c21STomi Valkeinen }
858f7018c21STomi Valkeinen 
859f7018c21STomi Valkeinen /*
860f7018c21STomi Valkeinen  * This function must be called from task context only, since it will
861f7018c21STomi Valkeinen  * sleep when disabling the LCD controller, or if we get two contending
862f7018c21STomi Valkeinen  * processes trying to alter state.
863f7018c21STomi Valkeinen  */
set_ctrlr_state(struct sa1100fb_info * fbi,u_int state)864f7018c21STomi Valkeinen static void set_ctrlr_state(struct sa1100fb_info *fbi, u_int state)
865f7018c21STomi Valkeinen {
866f7018c21STomi Valkeinen 	u_int old_state;
867f7018c21STomi Valkeinen 
868f7018c21STomi Valkeinen 	mutex_lock(&fbi->ctrlr_lock);
869f7018c21STomi Valkeinen 
870f7018c21STomi Valkeinen 	old_state = fbi->state;
871f7018c21STomi Valkeinen 
872f7018c21STomi Valkeinen 	/*
873f7018c21STomi Valkeinen 	 * Hack around fbcon initialisation.
874f7018c21STomi Valkeinen 	 */
875f7018c21STomi Valkeinen 	if (old_state == C_STARTUP && state == C_REENABLE)
876f7018c21STomi Valkeinen 		state = C_ENABLE;
877f7018c21STomi Valkeinen 
878f7018c21STomi Valkeinen 	switch (state) {
879f7018c21STomi Valkeinen 	case C_DISABLE_CLKCHANGE:
880f7018c21STomi Valkeinen 		/*
881f7018c21STomi Valkeinen 		 * Disable controller for clock change.  If the
882f7018c21STomi Valkeinen 		 * controller is already disabled, then do nothing.
883f7018c21STomi Valkeinen 		 */
884f7018c21STomi Valkeinen 		if (old_state != C_DISABLE && old_state != C_DISABLE_PM) {
885f7018c21STomi Valkeinen 			fbi->state = state;
886f7018c21STomi Valkeinen 			sa1100fb_disable_controller(fbi);
887f7018c21STomi Valkeinen 		}
888f7018c21STomi Valkeinen 		break;
889f7018c21STomi Valkeinen 
890f7018c21STomi Valkeinen 	case C_DISABLE_PM:
891f7018c21STomi Valkeinen 	case C_DISABLE:
892f7018c21STomi Valkeinen 		/*
893f7018c21STomi Valkeinen 		 * Disable controller
894f7018c21STomi Valkeinen 		 */
895f7018c21STomi Valkeinen 		if (old_state != C_DISABLE) {
896f7018c21STomi Valkeinen 			fbi->state = state;
897f7018c21STomi Valkeinen 
898f7018c21STomi Valkeinen 			__sa1100fb_backlight_power(fbi, 0);
899f7018c21STomi Valkeinen 			if (old_state != C_DISABLE_CLKCHANGE)
900f7018c21STomi Valkeinen 				sa1100fb_disable_controller(fbi);
901f7018c21STomi Valkeinen 			__sa1100fb_lcd_power(fbi, 0);
902f7018c21STomi Valkeinen 		}
903f7018c21STomi Valkeinen 		break;
904f7018c21STomi Valkeinen 
905f7018c21STomi Valkeinen 	case C_ENABLE_CLKCHANGE:
906f7018c21STomi Valkeinen 		/*
907f7018c21STomi Valkeinen 		 * Enable the controller after clock change.  Only
908f7018c21STomi Valkeinen 		 * do this if we were disabled for the clock change.
909f7018c21STomi Valkeinen 		 */
910f7018c21STomi Valkeinen 		if (old_state == C_DISABLE_CLKCHANGE) {
911f7018c21STomi Valkeinen 			fbi->state = C_ENABLE;
912f7018c21STomi Valkeinen 			sa1100fb_enable_controller(fbi);
913f7018c21STomi Valkeinen 		}
914f7018c21STomi Valkeinen 		break;
915f7018c21STomi Valkeinen 
916f7018c21STomi Valkeinen 	case C_REENABLE:
917f7018c21STomi Valkeinen 		/*
918f7018c21STomi Valkeinen 		 * Re-enable the controller only if it was already
919f7018c21STomi Valkeinen 		 * enabled.  This is so we reprogram the control
920f7018c21STomi Valkeinen 		 * registers.
921f7018c21STomi Valkeinen 		 */
922f7018c21STomi Valkeinen 		if (old_state == C_ENABLE) {
923f7018c21STomi Valkeinen 			sa1100fb_disable_controller(fbi);
924f7018c21STomi Valkeinen 			sa1100fb_setup_gpio(fbi);
925f7018c21STomi Valkeinen 			sa1100fb_enable_controller(fbi);
926f7018c21STomi Valkeinen 		}
927f7018c21STomi Valkeinen 		break;
928f7018c21STomi Valkeinen 
929f7018c21STomi Valkeinen 	case C_ENABLE_PM:
930f7018c21STomi Valkeinen 		/*
931f7018c21STomi Valkeinen 		 * Re-enable the controller after PM.  This is not
932f7018c21STomi Valkeinen 		 * perfect - think about the case where we were doing
933f7018c21STomi Valkeinen 		 * a clock change, and we suspended half-way through.
934f7018c21STomi Valkeinen 		 */
935f7018c21STomi Valkeinen 		if (old_state != C_DISABLE_PM)
936f7018c21STomi Valkeinen 			break;
937df561f66SGustavo A. R. Silva 		fallthrough;
938f7018c21STomi Valkeinen 
939f7018c21STomi Valkeinen 	case C_ENABLE:
940f7018c21STomi Valkeinen 		/*
941f7018c21STomi Valkeinen 		 * Power up the LCD screen, enable controller, and
942f7018c21STomi Valkeinen 		 * turn on the backlight.
943f7018c21STomi Valkeinen 		 */
944f7018c21STomi Valkeinen 		if (old_state != C_ENABLE) {
945f7018c21STomi Valkeinen 			fbi->state = C_ENABLE;
946f7018c21STomi Valkeinen 			sa1100fb_setup_gpio(fbi);
947f7018c21STomi Valkeinen 			__sa1100fb_lcd_power(fbi, 1);
948f7018c21STomi Valkeinen 			sa1100fb_enable_controller(fbi);
949f7018c21STomi Valkeinen 			__sa1100fb_backlight_power(fbi, 1);
950f7018c21STomi Valkeinen 		}
951f7018c21STomi Valkeinen 		break;
952f7018c21STomi Valkeinen 	}
953f7018c21STomi Valkeinen 	mutex_unlock(&fbi->ctrlr_lock);
954f7018c21STomi Valkeinen }
955f7018c21STomi Valkeinen 
956f7018c21STomi Valkeinen /*
957f7018c21STomi Valkeinen  * Our LCD controller task (which is called when we blank or unblank)
958f7018c21STomi Valkeinen  * via keventd.
959f7018c21STomi Valkeinen  */
sa1100fb_task(struct work_struct * w)960f7018c21STomi Valkeinen static void sa1100fb_task(struct work_struct *w)
961f7018c21STomi Valkeinen {
962f7018c21STomi Valkeinen 	struct sa1100fb_info *fbi = container_of(w, struct sa1100fb_info, task);
963f7018c21STomi Valkeinen 	u_int state = xchg(&fbi->task_state, -1);
964f7018c21STomi Valkeinen 
965f7018c21STomi Valkeinen 	set_ctrlr_state(fbi, state);
966f7018c21STomi Valkeinen }
967f7018c21STomi Valkeinen 
968f7018c21STomi Valkeinen #ifdef CONFIG_CPU_FREQ
969f7018c21STomi Valkeinen /*
970f7018c21STomi Valkeinen  * CPU clock speed change handler.  We need to adjust the LCD timing
971f7018c21STomi Valkeinen  * parameters when the CPU clock is adjusted by the power management
972f7018c21STomi Valkeinen  * subsystem.
973f7018c21STomi Valkeinen  */
974f7018c21STomi Valkeinen static int
sa1100fb_freq_transition(struct notifier_block * nb,unsigned long val,void * data)975f7018c21STomi Valkeinen sa1100fb_freq_transition(struct notifier_block *nb, unsigned long val,
976f7018c21STomi Valkeinen 			 void *data)
977f7018c21STomi Valkeinen {
978f7018c21STomi Valkeinen 	struct sa1100fb_info *fbi = TO_INF(nb, freq_transition);
979f7018c21STomi Valkeinen 	u_int pcd;
980f7018c21STomi Valkeinen 
981f7018c21STomi Valkeinen 	switch (val) {
982f7018c21STomi Valkeinen 	case CPUFREQ_PRECHANGE:
983f7018c21STomi Valkeinen 		set_ctrlr_state(fbi, C_DISABLE_CLKCHANGE);
984f7018c21STomi Valkeinen 		break;
985f7018c21STomi Valkeinen 
986f7018c21STomi Valkeinen 	case CPUFREQ_POSTCHANGE:
98723834a41SDmitry Eremin-Solenikov 		pcd = get_pcd(fbi, fbi->fb.var.pixclock);
988f7018c21STomi Valkeinen 		fbi->reg_lccr3 = (fbi->reg_lccr3 & ~0xff) | LCCR3_PixClkDiv(pcd);
989f7018c21STomi Valkeinen 		set_ctrlr_state(fbi, C_ENABLE_CLKCHANGE);
990f7018c21STomi Valkeinen 		break;
991f7018c21STomi Valkeinen 	}
992f7018c21STomi Valkeinen 	return 0;
993f7018c21STomi Valkeinen }
994f7018c21STomi Valkeinen #endif
995f7018c21STomi Valkeinen 
996f7018c21STomi Valkeinen #ifdef CONFIG_PM
997f7018c21STomi Valkeinen /*
998f7018c21STomi Valkeinen  * Power management hooks.  Note that we won't be called from IRQ context,
999f7018c21STomi Valkeinen  * unlike the blank functions above, so we may sleep.
1000f7018c21STomi Valkeinen  */
sa1100fb_suspend(struct platform_device * dev,pm_message_t state)1001f7018c21STomi Valkeinen static int sa1100fb_suspend(struct platform_device *dev, pm_message_t state)
1002f7018c21STomi Valkeinen {
1003f7018c21STomi Valkeinen 	struct sa1100fb_info *fbi = platform_get_drvdata(dev);
1004f7018c21STomi Valkeinen 
1005f7018c21STomi Valkeinen 	set_ctrlr_state(fbi, C_DISABLE_PM);
1006f7018c21STomi Valkeinen 	return 0;
1007f7018c21STomi Valkeinen }
1008f7018c21STomi Valkeinen 
sa1100fb_resume(struct platform_device * dev)1009f7018c21STomi Valkeinen static int sa1100fb_resume(struct platform_device *dev)
1010f7018c21STomi Valkeinen {
1011f7018c21STomi Valkeinen 	struct sa1100fb_info *fbi = platform_get_drvdata(dev);
1012f7018c21STomi Valkeinen 
1013f7018c21STomi Valkeinen 	set_ctrlr_state(fbi, C_ENABLE_PM);
1014f7018c21STomi Valkeinen 	return 0;
1015f7018c21STomi Valkeinen }
1016f7018c21STomi Valkeinen #else
1017f7018c21STomi Valkeinen #define sa1100fb_suspend	NULL
1018f7018c21STomi Valkeinen #define sa1100fb_resume		NULL
1019f7018c21STomi Valkeinen #endif
1020f7018c21STomi Valkeinen 
1021f7018c21STomi Valkeinen /*
1022f7018c21STomi Valkeinen  * sa1100fb_map_video_memory():
1023f7018c21STomi Valkeinen  *      Allocates the DRAM memory for the frame buffer.  This buffer is
1024f7018c21STomi Valkeinen  *	remapped into a non-cached, non-buffered, memory region to
1025f7018c21STomi Valkeinen  *      allow palette and pixel writes to occur without flushing the
1026f7018c21STomi Valkeinen  *      cache.  Once this area is remapped, all virtual memory
1027f7018c21STomi Valkeinen  *      access to the video memory should occur at the new region.
1028f7018c21STomi Valkeinen  */
sa1100fb_map_video_memory(struct sa1100fb_info * fbi)1029f7018c21STomi Valkeinen static int sa1100fb_map_video_memory(struct sa1100fb_info *fbi)
1030f7018c21STomi Valkeinen {
1031f7018c21STomi Valkeinen 	/*
1032f7018c21STomi Valkeinen 	 * We reserve one page for the palette, plus the size
1033f7018c21STomi Valkeinen 	 * of the framebuffer.
1034f7018c21STomi Valkeinen 	 */
1035f7018c21STomi Valkeinen 	fbi->map_size = PAGE_ALIGN(fbi->fb.fix.smem_len + PAGE_SIZE);
1036f6e45661SLuis R. Rodriguez 	fbi->map_cpu = dma_alloc_wc(fbi->dev, fbi->map_size, &fbi->map_dma,
1037f6e45661SLuis R. Rodriguez 				    GFP_KERNEL);
1038f7018c21STomi Valkeinen 
1039f7018c21STomi Valkeinen 	if (fbi->map_cpu) {
1040f7018c21STomi Valkeinen 		fbi->fb.screen_base = fbi->map_cpu + PAGE_SIZE;
1041f7018c21STomi Valkeinen 		fbi->screen_dma = fbi->map_dma + PAGE_SIZE;
1042f7018c21STomi Valkeinen 		/*
1043f7018c21STomi Valkeinen 		 * FIXME: this is actually the wrong thing to place in
1044f7018c21STomi Valkeinen 		 * smem_start.  But fbdev suffers from the problem that
1045f7018c21STomi Valkeinen 		 * it needs an API which doesn't exist (in this case,
1046f7018c21STomi Valkeinen 		 * dma_writecombine_mmap)
1047f7018c21STomi Valkeinen 		 */
1048f7018c21STomi Valkeinen 		fbi->fb.fix.smem_start = fbi->screen_dma;
1049f7018c21STomi Valkeinen 	}
1050f7018c21STomi Valkeinen 
1051f7018c21STomi Valkeinen 	return fbi->map_cpu ? 0 : -ENOMEM;
1052f7018c21STomi Valkeinen }
1053f7018c21STomi Valkeinen 
1054f7018c21STomi Valkeinen /* Fake monspecs to fill in fbinfo structure */
10553c3c5639SJulia Lawall static const struct fb_monspecs monspecs = {
1056f7018c21STomi Valkeinen 	.hfmin	= 30000,
1057f7018c21STomi Valkeinen 	.hfmax	= 70000,
1058f7018c21STomi Valkeinen 	.vfmin	= 50,
1059f7018c21STomi Valkeinen 	.vfmax	= 65,
1060f7018c21STomi Valkeinen };
1061f7018c21STomi Valkeinen 
1062f7018c21STomi Valkeinen 
sa1100fb_init_fbinfo(struct device * dev)1063f7018c21STomi Valkeinen static struct sa1100fb_info *sa1100fb_init_fbinfo(struct device *dev)
1064f7018c21STomi Valkeinen {
1065f7018c21STomi Valkeinen 	struct sa1100fb_mach_info *inf = dev_get_platdata(dev);
1066f7018c21STomi Valkeinen 	struct sa1100fb_info *fbi;
1067f7018c21STomi Valkeinen 	unsigned i;
1068f7018c21STomi Valkeinen 
1069cb6bc3ffSRussell King 	fbi = devm_kzalloc(dev, sizeof(struct sa1100fb_info), GFP_KERNEL);
1070f7018c21STomi Valkeinen 	if (!fbi)
1071f7018c21STomi Valkeinen 		return NULL;
1072f7018c21STomi Valkeinen 
1073f7018c21STomi Valkeinen 	fbi->dev = dev;
1074f7018c21STomi Valkeinen 
1075f7018c21STomi Valkeinen 	strcpy(fbi->fb.fix.id, SA1100_NAME);
1076f7018c21STomi Valkeinen 
1077f7018c21STomi Valkeinen 	fbi->fb.fix.type	= FB_TYPE_PACKED_PIXELS;
1078f7018c21STomi Valkeinen 	fbi->fb.fix.type_aux	= 0;
1079f7018c21STomi Valkeinen 	fbi->fb.fix.xpanstep	= 0;
1080f7018c21STomi Valkeinen 	fbi->fb.fix.ypanstep	= 0;
1081f7018c21STomi Valkeinen 	fbi->fb.fix.ywrapstep	= 0;
1082f7018c21STomi Valkeinen 	fbi->fb.fix.accel	= FB_ACCEL_NONE;
1083f7018c21STomi Valkeinen 
1084f7018c21STomi Valkeinen 	fbi->fb.var.nonstd	= 0;
1085f7018c21STomi Valkeinen 	fbi->fb.var.activate	= FB_ACTIVATE_NOW;
1086f7018c21STomi Valkeinen 	fbi->fb.var.height	= -1;
1087f7018c21STomi Valkeinen 	fbi->fb.var.width	= -1;
1088f7018c21STomi Valkeinen 	fbi->fb.var.accel_flags	= 0;
1089f7018c21STomi Valkeinen 	fbi->fb.var.vmode	= FB_VMODE_NONINTERLACED;
1090f7018c21STomi Valkeinen 
1091f7018c21STomi Valkeinen 	fbi->fb.fbops		= &sa1100fb_ops;
1092f7018c21STomi Valkeinen 	fbi->fb.monspecs	= monspecs;
1093cb6bc3ffSRussell King 	fbi->fb.pseudo_palette	= fbi->pseudo_palette;
1094f7018c21STomi Valkeinen 
1095f7018c21STomi Valkeinen 	fbi->rgb[RGB_4]		= &rgb_4;
1096f7018c21STomi Valkeinen 	fbi->rgb[RGB_8]		= &rgb_8;
1097f7018c21STomi Valkeinen 	fbi->rgb[RGB_16]	= &def_rgb_16;
1098f7018c21STomi Valkeinen 
1099f7018c21STomi Valkeinen 	/*
1100f7018c21STomi Valkeinen 	 * People just don't seem to get this.  We don't support
1101f7018c21STomi Valkeinen 	 * anything but correct entries now, so panic if someone
1102f7018c21STomi Valkeinen 	 * does something stupid.
1103f7018c21STomi Valkeinen 	 */
1104f7018c21STomi Valkeinen 	if (inf->lccr3 & (LCCR3_VrtSnchL|LCCR3_HorSnchL|0xff) ||
1105f7018c21STomi Valkeinen 	    inf->pixclock == 0)
1106f7018c21STomi Valkeinen 		panic("sa1100fb error: invalid LCCR3 fields set or zero "
1107f7018c21STomi Valkeinen 			"pixclock.");
1108f7018c21STomi Valkeinen 
1109f7018c21STomi Valkeinen 	fbi->fb.var.xres		= inf->xres;
1110f7018c21STomi Valkeinen 	fbi->fb.var.xres_virtual	= inf->xres;
1111f7018c21STomi Valkeinen 	fbi->fb.var.yres		= inf->yres;
1112f7018c21STomi Valkeinen 	fbi->fb.var.yres_virtual	= inf->yres;
1113f7018c21STomi Valkeinen 	fbi->fb.var.bits_per_pixel	= inf->bpp;
1114f7018c21STomi Valkeinen 	fbi->fb.var.pixclock		= inf->pixclock;
1115f7018c21STomi Valkeinen 	fbi->fb.var.hsync_len		= inf->hsync_len;
1116f7018c21STomi Valkeinen 	fbi->fb.var.left_margin		= inf->left_margin;
1117f7018c21STomi Valkeinen 	fbi->fb.var.right_margin	= inf->right_margin;
1118f7018c21STomi Valkeinen 	fbi->fb.var.vsync_len		= inf->vsync_len;
1119f7018c21STomi Valkeinen 	fbi->fb.var.upper_margin	= inf->upper_margin;
1120f7018c21STomi Valkeinen 	fbi->fb.var.lower_margin	= inf->lower_margin;
1121f7018c21STomi Valkeinen 	fbi->fb.var.sync		= inf->sync;
1122f7018c21STomi Valkeinen 	fbi->fb.var.grayscale		= inf->cmap_greyscale;
1123f7018c21STomi Valkeinen 	fbi->state			= C_STARTUP;
1124f7018c21STomi Valkeinen 	fbi->task_state			= (u_char)-1;
1125f7018c21STomi Valkeinen 	fbi->fb.fix.smem_len		= inf->xres * inf->yres *
1126f7018c21STomi Valkeinen 					  inf->bpp / 8;
1127f7018c21STomi Valkeinen 	fbi->inf			= inf;
1128f7018c21STomi Valkeinen 
1129f7018c21STomi Valkeinen 	/* Copy the RGB bitfield overrides */
1130f7018c21STomi Valkeinen 	for (i = 0; i < NR_RGB; i++)
1131f7018c21STomi Valkeinen 		if (inf->rgb[i])
1132f7018c21STomi Valkeinen 			fbi->rgb[i] = inf->rgb[i];
1133f7018c21STomi Valkeinen 
1134f7018c21STomi Valkeinen 	init_waitqueue_head(&fbi->ctrlr_wait);
1135f7018c21STomi Valkeinen 	INIT_WORK(&fbi->task, sa1100fb_task);
1136f7018c21STomi Valkeinen 	mutex_init(&fbi->ctrlr_lock);
1137f7018c21STomi Valkeinen 
1138f7018c21STomi Valkeinen 	return fbi;
1139f7018c21STomi Valkeinen }
1140f7018c21STomi Valkeinen 
sa1100fb_probe(struct platform_device * pdev)1141f7018c21STomi Valkeinen static int sa1100fb_probe(struct platform_device *pdev)
1142f7018c21STomi Valkeinen {
1143f7018c21STomi Valkeinen 	struct sa1100fb_info *fbi;
1144f7018c21STomi Valkeinen 	int ret, irq;
1145f7018c21STomi Valkeinen 
1146f7018c21STomi Valkeinen 	if (!dev_get_platdata(&pdev->dev)) {
1147f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "no platform LCD data\n");
1148f7018c21STomi Valkeinen 		return -EINVAL;
1149f7018c21STomi Valkeinen 	}
1150f7018c21STomi Valkeinen 
1151f7018c21STomi Valkeinen 	irq = platform_get_irq(pdev, 0);
1152df6b2287SRussell King 	if (irq < 0)
1153f7018c21STomi Valkeinen 		return -EINVAL;
1154f7018c21STomi Valkeinen 
1155f7018c21STomi Valkeinen 	fbi = sa1100fb_init_fbinfo(&pdev->dev);
1156f7018c21STomi Valkeinen 	if (!fbi)
1157c244f8e4SRussell King 		return -ENOMEM;
1158f7018c21STomi Valkeinen 
11593149299eSYueHaibing 	fbi->base = devm_platform_ioremap_resource(pdev, 0);
1160c244f8e4SRussell King 	if (IS_ERR(fbi->base))
1161c244f8e4SRussell King 		return PTR_ERR(fbi->base);
1162df6b2287SRussell King 
1163e43064ccSRussell King 	fbi->clk = devm_clk_get(&pdev->dev, NULL);
1164c244f8e4SRussell King 	if (IS_ERR(fbi->clk))
1165c244f8e4SRussell King 		return PTR_ERR(fbi->clk);
116623834a41SDmitry Eremin-Solenikov 
1167f6fc8c9dSRussell King 	ret = devm_request_irq(&pdev->dev, irq, sa1100fb_handle_irq, 0,
1168f6fc8c9dSRussell King 			       "LCD", fbi);
1169f7018c21STomi Valkeinen 	if (ret) {
1170f7018c21STomi Valkeinen 		dev_err(&pdev->dev, "request_irq failed: %d\n", ret);
1171c244f8e4SRussell King 		return ret;
1172f7018c21STomi Valkeinen 	}
1173f7018c21STomi Valkeinen 
11745e4096efSLinus Walleij 	fbi->shannon_lcden = gpiod_get_optional(&pdev->dev, "shannon-lcden",
11755e4096efSLinus Walleij 						GPIOD_OUT_LOW);
11765e4096efSLinus Walleij 	if (IS_ERR(fbi->shannon_lcden))
11775e4096efSLinus Walleij 		return PTR_ERR(fbi->shannon_lcden);
1178f7018c21STomi Valkeinen 
11795634cbabSRussell King 	/* Initialize video memory */
11805634cbabSRussell King 	ret = sa1100fb_map_video_memory(fbi);
11815634cbabSRussell King 	if (ret)
1182c244f8e4SRussell King 		return ret;
11835634cbabSRussell King 
1184f7018c21STomi Valkeinen 	/*
1185f7018c21STomi Valkeinen 	 * This makes sure that our colour bitfield
1186f7018c21STomi Valkeinen 	 * descriptors are correctly initialised.
1187f7018c21STomi Valkeinen 	 */
1188f7018c21STomi Valkeinen 	sa1100fb_check_var(&fbi->fb.var, &fbi->fb);
1189f7018c21STomi Valkeinen 
1190f7018c21STomi Valkeinen 	platform_set_drvdata(pdev, fbi);
1191f7018c21STomi Valkeinen 
1192f7018c21STomi Valkeinen 	ret = register_framebuffer(&fbi->fb);
11930ab76581SRussell King 	if (ret < 0) {
11940ab76581SRussell King 		dma_free_wc(fbi->dev, fbi->map_size, fbi->map_cpu,
11950ab76581SRussell King 			    fbi->map_dma);
11960ab76581SRussell King 		return ret;
11970ab76581SRussell King 	}
1198f7018c21STomi Valkeinen 
1199f7018c21STomi Valkeinen #ifdef CONFIG_CPU_FREQ
1200f7018c21STomi Valkeinen 	fbi->freq_transition.notifier_call = sa1100fb_freq_transition;
1201f7018c21STomi Valkeinen 	cpufreq_register_notifier(&fbi->freq_transition, CPUFREQ_TRANSITION_NOTIFIER);
1202f7018c21STomi Valkeinen #endif
1203f7018c21STomi Valkeinen 
1204f7018c21STomi Valkeinen 	/* This driver cannot be unloaded at the moment */
1205f7018c21STomi Valkeinen 	return 0;
1206f7018c21STomi Valkeinen }
1207f7018c21STomi Valkeinen 
1208f7018c21STomi Valkeinen static struct platform_driver sa1100fb_driver = {
1209f7018c21STomi Valkeinen 	.probe		= sa1100fb_probe,
1210f7018c21STomi Valkeinen 	.suspend	= sa1100fb_suspend,
1211f7018c21STomi Valkeinen 	.resume		= sa1100fb_resume,
1212f7018c21STomi Valkeinen 	.driver		= {
1213f7018c21STomi Valkeinen 		.name	= "sa11x0-fb",
1214f7018c21STomi Valkeinen 	},
1215f7018c21STomi Valkeinen };
1216f7018c21STomi Valkeinen 
sa1100fb_init(void)1217*e638d371SArnd Bergmann static int __init sa1100fb_init(void)
1218f7018c21STomi Valkeinen {
1219f7018c21STomi Valkeinen 	if (fb_get_options("sa1100fb", NULL))
1220f7018c21STomi Valkeinen 		return -ENODEV;
1221f7018c21STomi Valkeinen 
1222f7018c21STomi Valkeinen 	return platform_driver_register(&sa1100fb_driver);
1223f7018c21STomi Valkeinen }
1224f7018c21STomi Valkeinen 
1225f7018c21STomi Valkeinen module_init(sa1100fb_init);
1226f7018c21STomi Valkeinen MODULE_DESCRIPTION("StrongARM-1100/1110 framebuffer driver");
1227f7018c21STomi Valkeinen MODULE_LICENSE("GPL");
1228