blob: f25d5d648333cb6665529154aeaec90184da02b5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * BRIEF MODULE DESCRIPTION
3 * Au1100 LCD Driver.
4 *
Pete Popov3b495f22005-04-04 01:06:19 +00005 * Rewritten for 2.6 by Embedded Alley Solutions
6 * <source@embeddedalley.com>, based on submissions by
7 * Karl Lessard <klessard@sunrisetelecom.com>
8 * <c.pellegrin@exadron.com>
9 *
Rodolfo Giomettif77f50c2006-06-26 00:26:49 -070010 * PM support added by Rodolfo Giometti <giometti@linux.it>
11 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Copyright 2002 MontaVista Software
13 * Author: MontaVista Software, Inc.
14 * ppopov@mvista.com or source@mvista.com
15 *
16 * Copyright 2002 Alchemy Semiconductor
17 * Author: Alchemy Semiconductor
18 *
19 * Based on:
20 * linux/drivers/video/skeletonfb.c -- Skeleton for a frame buffer device
21 * Created 28 Dec 1997 by Geert Uytterhoeven
22 *
23 * This program is free software; you can redistribute it and/or modify it
24 * under the terms of the GNU General Public License as published by the
25 * Free Software Foundation; either version 2 of the License, or (at your
26 * option) any later version.
27 *
28 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
29 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
31 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
33 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
34 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
35 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
37 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 *
39 * You should have received a copy of the GNU General Public License along
40 * with this program; if not, write to the Free Software Foundation, Inc.,
41 * 675 Mass Ave, Cambridge, MA 02139, USA.
42 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/module.h>
44#include <linux/kernel.h>
45#include <linux/errno.h>
46#include <linux/string.h>
47#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/fb.h>
49#include <linux/init.h>
Pete Popov3b495f22005-04-04 01:06:19 +000050#include <linux/interrupt.h>
51#include <linux/ctype.h>
52#include <linux/dma-mapping.h>
Freddy Spierenburgcacfc8c2006-02-24 13:04:17 -080053#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Pete Popov3b495f22005-04-04 01:06:19 +000055#include <asm/mach-au1x00/au1000.h>
56
57#define DEBUG 0
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include "au1100fb.h"
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/*
62 * Sanity check. If this is a new Au1100 based board, search for
63 * the PB1100 ifdefs to make sure you modify the code accordingly.
64 */
Pete Popov3b495f22005-04-04 01:06:19 +000065#if defined(CONFIG_MIPS_PB1100)
66 #include <asm/mach-pb1x00/pb1100.h>
67#elif defined(CONFIG_MIPS_DB1100)
68 #include <asm/mach-db1x00/db1x00.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#else
Pete Popov3b495f22005-04-04 01:06:19 +000070 #error "Unknown Au1100 board, Au1100 FB driver not supported"
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#endif
72
Pete Popov3b495f22005-04-04 01:06:19 +000073#define DRIVER_NAME "au1100fb"
74#define DRIVER_DESC "LCD controller driver for AU1100 processors"
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Pete Popov3b495f22005-04-04 01:06:19 +000076#define to_au1100fb_device(_info) \
77 (_info ? container_of(_info, struct au1100fb_device, info) : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Pete Popov3b495f22005-04-04 01:06:19 +000079/* Bitfields format supported by the controller. Note that the order of formats
80 * SHOULD be the same as in the LCD_CONTROL_SBPPF field, so we can retrieve the
81 * right pixel format by doing rgb_bitfields[LCD_CONTROL_SBPPF_XXX >> LCD_CONTROL_SBPPF]
82 */
83struct fb_bitfield rgb_bitfields[][4] =
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
Pete Popov3b495f22005-04-04 01:06:19 +000085 /* Red, Green, Blue, Transp */
86 { { 10, 6, 0 }, { 5, 5, 0 }, { 0, 5, 0 }, { 0, 0, 0 } },
87 { { 11, 5, 0 }, { 5, 6, 0 }, { 0, 5, 0 }, { 0, 0, 0 } },
88 { { 11, 5, 0 }, { 6, 5, 0 }, { 0, 6, 0 }, { 0, 0, 0 } },
89 { { 10, 5, 0 }, { 5, 5, 0 }, { 0, 5, 0 }, { 15, 1, 0 } },
90 { { 11, 5, 0 }, { 6, 5, 0 }, { 1, 5, 0 }, { 0, 1, 0 } },
91
92 /* The last is used to describe 12bpp format */
93 { { 8, 4, 0 }, { 4, 4, 0 }, { 0, 4, 0 }, { 0, 0, 0 } },
94};
95
96static struct fb_fix_screeninfo au1100fb_fix __initdata = {
97 .id = "AU1100 FB",
98 .xpanstep = 1,
99 .ypanstep = 1,
100 .type = FB_TYPE_PACKED_PIXELS,
101 .accel = FB_ACCEL_NONE,
102};
103
104static struct fb_var_screeninfo au1100fb_var __initdata = {
105 .activate = FB_ACTIVATE_NOW,
106 .height = -1,
107 .width = -1,
108 .vmode = FB_VMODE_NONINTERLACED,
109};
110
111static struct au1100fb_drv_info drv_info;
112
113/*
114 * Set hardware with var settings. This will enable the controller with a specific
115 * mode, normally validated with the fb_check_var method
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 */
Pete Popov3b495f22005-04-04 01:06:19 +0000117int au1100fb_setmode(struct au1100fb_device *fbdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Pete Popov3b495f22005-04-04 01:06:19 +0000119 struct fb_info *info = &fbdev->info;
120 u32 words;
121 int index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Pete Popov3b495f22005-04-04 01:06:19 +0000123 if (!fbdev)
124 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Pete Popov3b495f22005-04-04 01:06:19 +0000126 /* Update var-dependent FB info */
127 if (panel_is_active(fbdev->panel) || panel_is_color(fbdev->panel)) {
128 if (info->var.bits_per_pixel <= 8) {
129 /* palettized */
130 info->var.red.offset = 0;
131 info->var.red.length = info->var.bits_per_pixel;
132 info->var.red.msb_right = 0;
133
134 info->var.green.offset = 0;
135 info->var.green.length = info->var.bits_per_pixel;
136 info->var.green.msb_right = 0;
137
138 info->var.blue.offset = 0;
139 info->var.blue.length = info->var.bits_per_pixel;
140 info->var.blue.msb_right = 0;
141
142 info->var.transp.offset = 0;
143 info->var.transp.length = 0;
144 info->var.transp.msb_right = 0;
145
146 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
147 info->fix.line_length = info->var.xres_virtual /
148 (8/info->var.bits_per_pixel);
149 } else {
150 /* non-palettized */
151 index = (fbdev->panel->control_base & LCD_CONTROL_SBPPF_MASK) >> LCD_CONTROL_SBPPF_BIT;
152 info->var.red = rgb_bitfields[index][0];
153 info->var.green = rgb_bitfields[index][1];
154 info->var.blue = rgb_bitfields[index][2];
155 info->var.transp = rgb_bitfields[index][3];
156
157 info->fix.visual = FB_VISUAL_TRUECOLOR;
158 info->fix.line_length = info->var.xres_virtual << 1; /* depth=16 */
Rodolfo Giomettibb39e412006-08-05 12:14:22 -0700159 }
Pete Popov3b495f22005-04-04 01:06:19 +0000160 } else {
161 /* mono */
162 info->fix.visual = FB_VISUAL_MONO10;
163 info->fix.line_length = info->var.xres_virtual / 8;
164 }
165
166 info->screen_size = info->fix.line_length * info->var.yres_virtual;
Rodolfo Giomettifd2d5432006-08-05 12:14:19 -0700167 info->var.rotate = ((fbdev->panel->control_base&LCD_CONTROL_SM_MASK) \
168 >> LCD_CONTROL_SM_BIT) * 90;
Pete Popov3b495f22005-04-04 01:06:19 +0000169
170 /* Determine BPP mode and format */
Rodolfo Giomettifd2d5432006-08-05 12:14:19 -0700171 fbdev->regs->lcd_control = fbdev->panel->control_base;
Rodolfo Giomettibb39e412006-08-05 12:14:22 -0700172 fbdev->regs->lcd_horztiming = fbdev->panel->horztiming;
173 fbdev->regs->lcd_verttiming = fbdev->panel->verttiming;
174 fbdev->regs->lcd_clkcontrol = fbdev->panel->clkcontrol_base;
Pete Popov3b495f22005-04-04 01:06:19 +0000175 fbdev->regs->lcd_intenable = 0;
176 fbdev->regs->lcd_intstatus = 0;
Pete Popov3b495f22005-04-04 01:06:19 +0000177 fbdev->regs->lcd_dmaaddr0 = LCD_DMA_SA_N(fbdev->fb_phys);
178
179 if (panel_is_dual(fbdev->panel)) {
180 /* Second panel display seconf half of screen if possible,
181 * otherwise display the same as the first panel */
182 if (info->var.yres_virtual >= (info->var.yres << 1)) {
183 fbdev->regs->lcd_dmaaddr1 = LCD_DMA_SA_N(fbdev->fb_phys +
184 (info->fix.line_length *
185 (info->var.yres_virtual >> 1)));
186 } else {
187 fbdev->regs->lcd_dmaaddr1 = LCD_DMA_SA_N(fbdev->fb_phys);
188 }
189 }
190
191 words = info->fix.line_length / sizeof(u32);
192 if (!info->var.rotate || (info->var.rotate == 180)) {
193 words *= info->var.yres_virtual;
194 if (info->var.rotate /* 180 */) {
195 words -= (words % 8); /* should be divisable by 8 */
196 }
197 }
198 fbdev->regs->lcd_words = LCD_WRD_WRDS_N(words);
199
200 fbdev->regs->lcd_pwmdiv = 0;
201 fbdev->regs->lcd_pwmhi = 0;
202
203 /* Resume controller */
204 fbdev->regs->lcd_control |= LCD_CONTROL_GO;
Rodolfo Giomettibb39e412006-08-05 12:14:22 -0700205 mdelay(10);
206 au1100fb_fb_blank(VESA_NO_BLANKING, info);
Pete Popov3b495f22005-04-04 01:06:19 +0000207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return 0;
209}
210
Pete Popov3b495f22005-04-04 01:06:19 +0000211/* fb_setcolreg
212 * Set color in LCD palette.
213 */
214int au1100fb_fb_setcolreg(unsigned regno, unsigned red, unsigned green, unsigned blue, unsigned transp, struct fb_info *fbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Rodolfo Giomettic05b7f32006-05-30 21:26:57 -0700216 struct au1100fb_device *fbdev;
217 u32 *palette;
Pete Popov3b495f22005-04-04 01:06:19 +0000218 u32 value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Rodolfo Giomettic05b7f32006-05-30 21:26:57 -0700220 fbdev = to_au1100fb_device(fbi);
221 palette = fbdev->regs->lcd_pallettebase;
222
Pete Popov3b495f22005-04-04 01:06:19 +0000223 if (regno > (AU1100_LCD_NBR_PALETTE_ENTRIES - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return -EINVAL;
Pete Popov3b495f22005-04-04 01:06:19 +0000225
226 if (fbi->var.grayscale) {
227 /* Convert color to grayscale */
228 red = green = blue =
229 (19595 * red + 38470 * green + 7471 * blue) >> 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
231
Pete Popov3b495f22005-04-04 01:06:19 +0000232 if (fbi->fix.visual == FB_VISUAL_TRUECOLOR) {
233 /* Place color in the pseudopalette */
234 if (regno > 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return -EINVAL;
236
Pete Popov3b495f22005-04-04 01:06:19 +0000237 palette = (u32*)fbi->pseudo_palette;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Pete Popov3b495f22005-04-04 01:06:19 +0000239 red >>= (16 - fbi->var.red.length);
240 green >>= (16 - fbi->var.green.length);
241 blue >>= (16 - fbi->var.blue.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Pete Popov3b495f22005-04-04 01:06:19 +0000243 value = (red << fbi->var.red.offset) |
244 (green << fbi->var.green.offset)|
245 (blue << fbi->var.blue.offset);
246 value &= 0xFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Pete Popov3b495f22005-04-04 01:06:19 +0000248 } else if (panel_is_active(fbdev->panel)) {
249 /* COLOR TFT PALLETTIZED (use RGB 565) */
250 value = (red & 0xF800)|((green >> 5) & 0x07E0)|((blue >> 11) & 0x001F);
251 value &= 0xFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Pete Popov3b495f22005-04-04 01:06:19 +0000253 } else if (panel_is_color(fbdev->panel)) {
254 /* COLOR STN MODE */
255 value = (((panel_swap_rgb(fbdev->panel) ? blue : red) >> 12) & 0x000F) |
256 ((green >> 8) & 0x00F0) |
257 (((panel_swap_rgb(fbdev->panel) ? red : blue) >> 4) & 0x0F00);
258 value &= 0xFFF;
259 } else {
260 /* MONOCHROME MODE */
261 value = (green >> 12) & 0x000F;
262 value &= 0xF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
264
Pete Popov3b495f22005-04-04 01:06:19 +0000265 palette[regno] = value;
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return 0;
268}
269
Pete Popov3b495f22005-04-04 01:06:19 +0000270/* fb_blank
271 * Blank the screen. Depending on the mode, the screen will be
272 * activated with the backlight color, or desactivated
273 */
274int au1100fb_fb_blank(int blank_mode, struct fb_info *fbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Pete Popov3b495f22005-04-04 01:06:19 +0000276 struct au1100fb_device *fbdev = to_au1100fb_device(fbi);
277
278 print_dbg("fb_blank %d %p", blank_mode, fbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 switch (blank_mode) {
Pete Popov3b495f22005-04-04 01:06:19 +0000281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 case VESA_NO_BLANKING:
Pete Popov3b495f22005-04-04 01:06:19 +0000283 /* Turn on panel */
284 fbdev->regs->lcd_control |= LCD_CONTROL_GO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285#ifdef CONFIG_MIPS_PB1100
Pete Popov3b495f22005-04-04 01:06:19 +0000286 if (drv_info.panel_idx == 1) {
287 au_writew(au_readw(PB1100_G_CONTROL)
288 | (PB1100_G_CONTROL_BL | PB1100_G_CONTROL_VDD),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 PB1100_G_CONTROL);
Pete Popov3b495f22005-04-04 01:06:19 +0000290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291#endif
292 au_sync();
293 break;
294
295 case VESA_VSYNC_SUSPEND:
296 case VESA_HSYNC_SUSPEND:
297 case VESA_POWERDOWN:
Pete Popov3b495f22005-04-04 01:06:19 +0000298 /* Turn off panel */
299 fbdev->regs->lcd_control &= ~LCD_CONTROL_GO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300#ifdef CONFIG_MIPS_PB1100
Pete Popov3b495f22005-04-04 01:06:19 +0000301 if (drv_info.panel_idx == 1) {
302 au_writew(au_readw(PB1100_G_CONTROL)
303 & ~(PB1100_G_CONTROL_BL | PB1100_G_CONTROL_VDD),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 PB1100_G_CONTROL);
Pete Popov3b495f22005-04-04 01:06:19 +0000305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306#endif
307 au_sync();
308 break;
309 default:
310 break;
311
312 }
313 return 0;
314}
315
Pete Popov3b495f22005-04-04 01:06:19 +0000316/* fb_pan_display
317 * Pan display in x and/or y as specified
318 */
319int au1100fb_fb_pan_display(struct fb_var_screeninfo *var, struct fb_info *fbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Rodolfo Giomettic05b7f32006-05-30 21:26:57 -0700321 struct au1100fb_device *fbdev;
Pete Popov3b495f22005-04-04 01:06:19 +0000322 int dy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Rodolfo Giomettic05b7f32006-05-30 21:26:57 -0700324 fbdev = to_au1100fb_device(fbi);
325
Pete Popov3b495f22005-04-04 01:06:19 +0000326 print_dbg("fb_pan_display %p %p", var, fbi);
327
328 if (!var || !fbdev) {
329 return -EINVAL;
330 }
331
332 if (var->xoffset - fbi->var.xoffset) {
333 /* No support for X panning for now! */
334 return -EINVAL;
335 }
336
337 print_dbg("fb_pan_display 2 %p %p", var, fbi);
338 dy = var->yoffset - fbi->var.yoffset;
339 if (dy) {
340
341 u32 dmaaddr;
342
343 print_dbg("Panning screen of %d lines", dy);
344
345 dmaaddr = fbdev->regs->lcd_dmaaddr0;
346 dmaaddr += (fbi->fix.line_length * dy);
347
348 /* TODO: Wait for current frame to finished */
349 fbdev->regs->lcd_dmaaddr0 = LCD_DMA_SA_N(dmaaddr);
350
351 if (panel_is_dual(fbdev->panel)) {
352 dmaaddr = fbdev->regs->lcd_dmaaddr1;
353 dmaaddr += (fbi->fix.line_length * dy);
354 fbdev->regs->lcd_dmaaddr0 = LCD_DMA_SA_N(dmaaddr);
355 }
356 }
357 print_dbg("fb_pan_display 3 %p %p", var, fbi);
358
359 return 0;
360}
361
362/* fb_rotate
363 * Rotate the display of this angle. This doesn't seems to be used by the core,
364 * but as our hardware supports it, so why not implementing it...
365 */
366void au1100fb_fb_rotate(struct fb_info *fbi, int angle)
367{
368 struct au1100fb_device *fbdev = to_au1100fb_device(fbi);
369
370 print_dbg("fb_rotate %p %d", fbi, angle);
371
372 if (fbdev && (angle > 0) && !(angle % 90)) {
373
374 fbdev->regs->lcd_control &= ~LCD_CONTROL_GO;
375
376 fbdev->regs->lcd_control &= ~(LCD_CONTROL_SM_MASK);
377 fbdev->regs->lcd_control |= ((angle/90) << LCD_CONTROL_SM_BIT);
378
379 fbdev->regs->lcd_control |= LCD_CONTROL_GO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
381}
382
Pete Popov3b495f22005-04-04 01:06:19 +0000383/* fb_mmap
384 * Map video memory in user space. We don't use the generic fb_mmap method mainly
385 * to allow the use of the TLB streaming flag (CCA=6)
386 */
Christoph Hellwig216d5262006-01-14 13:21:25 -0800387int au1100fb_fb_mmap(struct fb_info *fbi, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Rodolfo Giomettic05b7f32006-05-30 21:26:57 -0700389 struct au1100fb_device *fbdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 unsigned int len;
391 unsigned long start=0, off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Rodolfo Giomettic05b7f32006-05-30 21:26:57 -0700393 fbdev = to_au1100fb_device(fbi);
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT)) {
396 return -EINVAL;
397 }
398
Pete Popov3b495f22005-04-04 01:06:19 +0000399 start = fbdev->fb_phys & PAGE_MASK;
400 len = PAGE_ALIGN((start & ~PAGE_MASK) + fbdev->fb_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 off = vma->vm_pgoff << PAGE_SHIFT;
403
404 if ((vma->vm_end - vma->vm_start + off) > len) {
405 return -EINVAL;
406 }
407
408 off += start;
409 vma->vm_pgoff = off >> PAGE_SHIFT;
410
Pete Popov3b495f22005-04-04 01:06:19 +0000411 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 pgprot_val(vma->vm_page_prot) |= (6 << 9); //CCA=6
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 vma->vm_flags |= VM_IO;
415
Freddy Spierenburgcacfc8c2006-02-24 13:04:17 -0800416 if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 vma->vm_end - vma->vm_start,
418 vma->vm_page_prot)) {
419 return -EAGAIN;
420 }
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return 0;
423}
424
Pete Popov3b495f22005-04-04 01:06:19 +0000425static struct fb_ops au1100fb_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Pete Popov3b495f22005-04-04 01:06:19 +0000427 .owner = THIS_MODULE,
428 .fb_setcolreg = au1100fb_fb_setcolreg,
429 .fb_blank = au1100fb_fb_blank,
430 .fb_pan_display = au1100fb_fb_pan_display,
431 .fb_fillrect = cfb_fillrect,
432 .fb_copyarea = cfb_copyarea,
433 .fb_imageblit = cfb_imageblit,
434 .fb_rotate = au1100fb_fb_rotate,
435 .fb_mmap = au1100fb_fb_mmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436};
437
438
Pete Popov3b495f22005-04-04 01:06:19 +0000439/*-------------------------------------------------------------------------*/
440
441/* AU1100 LCD controller device driver */
442
443int au1100fb_drv_probe(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Pete Popov3b495f22005-04-04 01:06:19 +0000445 struct au1100fb_device *fbdev = NULL;
446 struct resource *regs_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 unsigned long page;
Pete Popov3b495f22005-04-04 01:06:19 +0000448 u32 sys_clksrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Pete Popov3b495f22005-04-04 01:06:19 +0000450 if (!dev)
451 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Pete Popov3b495f22005-04-04 01:06:19 +0000453 /* Allocate new device private */
454 if (!(fbdev = kmalloc(sizeof(struct au1100fb_device), GFP_KERNEL))) {
455 print_err("fail to allocate device private record");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return -ENOMEM;
457 }
Pete Popov3b495f22005-04-04 01:06:19 +0000458 memset((void*)fbdev, 0, sizeof(struct au1100fb_device));
459
460 fbdev->panel = &known_lcd_panels[drv_info.panel_idx];
461
462 dev_set_drvdata(dev, (void*)fbdev);
463
464 /* Allocate region for our registers and map them */
465 if (!(regs_res = platform_get_resource(to_platform_device(dev),
466 IORESOURCE_MEM, 0))) {
467 print_err("fail to retrieve registers resource");
468 return -EFAULT;
469 }
470
471 au1100fb_fix.mmio_start = regs_res->start;
472 au1100fb_fix.mmio_len = regs_res->end - regs_res->start + 1;
473
474 if (!request_mem_region(au1100fb_fix.mmio_start, au1100fb_fix.mmio_len,
475 DRIVER_NAME)) {
Rodolfo Giomettic05b7f32006-05-30 21:26:57 -0700476 print_err("fail to lock memory region at 0x%08lx",
Pete Popov3b495f22005-04-04 01:06:19 +0000477 au1100fb_fix.mmio_start);
478 return -EBUSY;
479 }
480
481 fbdev->regs = (struct au1100fb_regs*)KSEG1ADDR(au1100fb_fix.mmio_start);
482
483 print_dbg("Register memory map at %p", fbdev->regs);
484 print_dbg("phys=0x%08x, size=%d", fbdev->regs_phys, fbdev->regs_len);
485
486
487
488 /* Allocate the framebuffer to the maximum screen size * nbr of video buffers */
489 fbdev->fb_len = fbdev->panel->xres * fbdev->panel->yres *
490 (fbdev->panel->bpp >> 3) * AU1100FB_NBR_VIDEO_BUFFERS;
491
492 fbdev->fb_mem = dma_alloc_coherent(dev, PAGE_ALIGN(fbdev->fb_len),
493 &fbdev->fb_phys, GFP_KERNEL);
494 if (!fbdev->fb_mem) {
495 print_err("fail to allocate frambuffer (size: %dK))",
496 fbdev->fb_len / 1024);
497 return -ENOMEM;
498 }
499
500 au1100fb_fix.smem_start = fbdev->fb_phys;
501 au1100fb_fix.smem_len = fbdev->fb_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 /*
504 * Set page reserved so that mmap will work. This is necessary
505 * since we'll be remapping normal memory.
506 */
Pete Popov3b495f22005-04-04 01:06:19 +0000507 for (page = (unsigned long)fbdev->fb_mem;
508 page < PAGE_ALIGN((unsigned long)fbdev->fb_mem + fbdev->fb_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 page += PAGE_SIZE) {
Pete Popov3b495f22005-04-04 01:06:19 +0000510#if CONFIG_DMA_NONCOHERENT
511 SetPageReserved(virt_to_page(CAC_ADDR(page)));
512#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 SetPageReserved(virt_to_page(page));
Pete Popov3b495f22005-04-04 01:06:19 +0000514#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
516
Pete Popov3b495f22005-04-04 01:06:19 +0000517 print_dbg("Framebuffer memory map at %p", fbdev->fb_mem);
518 print_dbg("phys=0x%08x, size=%dK", fbdev->fb_phys, fbdev->fb_len / 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Pete Popov3b495f22005-04-04 01:06:19 +0000520 /* Setup LCD clock to AUX (48 MHz) */
521 sys_clksrc = au_readl(SYS_CLKSRC) & ~(SYS_CS_ML_MASK | SYS_CS_DL | SYS_CS_CL);
522 au_writel((sys_clksrc | (1 << SYS_CS_ML_BIT)), SYS_CLKSRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Pete Popov3b495f22005-04-04 01:06:19 +0000524 /* load the panel info into the var struct */
525 au1100fb_var.bits_per_pixel = fbdev->panel->bpp;
526 au1100fb_var.xres = fbdev->panel->xres;
527 au1100fb_var.xres_virtual = au1100fb_var.xres;
528 au1100fb_var.yres = fbdev->panel->yres;
529 au1100fb_var.yres_virtual = au1100fb_var.yres;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Pete Popov3b495f22005-04-04 01:06:19 +0000531 fbdev->info.screen_base = fbdev->fb_mem;
532 fbdev->info.fbops = &au1100fb_ops;
533 fbdev->info.fix = au1100fb_fix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Pete Popov3b495f22005-04-04 01:06:19 +0000535 if (!(fbdev->info.pseudo_palette = kmalloc(sizeof(u32) * 16, GFP_KERNEL))) {
536 return -ENOMEM;
537 }
538 memset(fbdev->info.pseudo_palette, 0, sizeof(u32) * 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Pete Popov3b495f22005-04-04 01:06:19 +0000540 if (fb_alloc_cmap(&fbdev->info.cmap, AU1100_LCD_NBR_PALETTE_ENTRIES, 0) < 0) {
541 print_err("Fail to allocate colormap (%d entries)",
542 AU1100_LCD_NBR_PALETTE_ENTRIES);
543 kfree(fbdev->info.pseudo_palette);
544 return -EFAULT;
545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Pete Popov3b495f22005-04-04 01:06:19 +0000547 fbdev->info.var = au1100fb_var;
548
549 /* Set h/w registers */
550 au1100fb_setmode(fbdev);
551
552 /* Register new framebuffer */
553 if (register_framebuffer(&fbdev->info) < 0) {
554 print_err("cannot register new framebuffer");
555 goto failed;
556 }
557
558 return 0;
559
560failed:
561 if (fbdev->regs) {
562 release_mem_region(fbdev->regs_phys, fbdev->regs_len);
563 }
564 if (fbdev->fb_mem) {
565 dma_free_noncoherent(dev, fbdev->fb_len, fbdev->fb_mem, fbdev->fb_phys);
566 }
567 if (fbdev->info.cmap.len != 0) {
568 fb_dealloc_cmap(&fbdev->info.cmap);
569 }
570 kfree(fbdev);
571 dev_set_drvdata(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 return 0;
574}
575
Pete Popov3b495f22005-04-04 01:06:19 +0000576int au1100fb_drv_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Pete Popov3b495f22005-04-04 01:06:19 +0000578 struct au1100fb_device *fbdev = NULL;
579
580 if (!dev)
581 return -ENODEV;
582
583 fbdev = (struct au1100fb_device*) dev_get_drvdata(dev);
584
585#if !defined(CONFIG_FRAMEBUFFER_CONSOLE) && defined(CONFIG_LOGO)
586 au1100fb_fb_blank(VESA_POWERDOWN, &fbdev->info);
587#endif
588 fbdev->regs->lcd_control &= ~LCD_CONTROL_GO;
589
590 /* Clean up all probe data */
591 unregister_framebuffer(&fbdev->info);
592
593 release_mem_region(fbdev->regs_phys, fbdev->regs_len);
594
595 dma_free_coherent(dev, PAGE_ALIGN(fbdev->fb_len), fbdev->fb_mem, fbdev->fb_phys);
596
597 fb_dealloc_cmap(&fbdev->info.cmap);
598 kfree(fbdev->info.pseudo_palette);
599 kfree((void*)fbdev);
600
601 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603
Rodolfo Giomettif77f50c2006-06-26 00:26:49 -0700604#ifdef CONFIG_PM
605static u32 sys_clksrc;
606static struct au1100fb_regs fbregs;
607
Rodolfo Giomettic05b7f32006-05-30 21:26:57 -0700608int au1100fb_drv_suspend(struct device *dev, pm_message_t state)
Pete Popov3b495f22005-04-04 01:06:19 +0000609{
Rodolfo Giomettif77f50c2006-06-26 00:26:49 -0700610 struct au1100fb_device *fbdev = dev_get_drvdata(dev);
611
612 if (!fbdev)
613 return 0;
614
615 /* Save the clock source state */
616 sys_clksrc = au_readl(SYS_CLKSRC);
617
618 /* Blank the LCD */
619 au1100fb_fb_blank(VESA_POWERDOWN, &fbdev->info);
620
621 /* Stop LCD clocking */
622 au_writel(sys_clksrc & ~SYS_CS_ML_MASK, SYS_CLKSRC);
623
624 memcpy(&fbregs, fbdev->regs, sizeof(struct au1100fb_regs));
625
Pete Popov3b495f22005-04-04 01:06:19 +0000626 return 0;
627}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Rodolfo Giomettic05b7f32006-05-30 21:26:57 -0700629int au1100fb_drv_resume(struct device *dev)
Pete Popov3b495f22005-04-04 01:06:19 +0000630{
Rodolfo Giomettif77f50c2006-06-26 00:26:49 -0700631 struct au1100fb_device *fbdev = dev_get_drvdata(dev);
632
633 if (!fbdev)
634 return 0;
635
636 memcpy(fbdev->regs, &fbregs, sizeof(struct au1100fb_regs));
637
638 /* Restart LCD clocking */
639 au_writel(sys_clksrc, SYS_CLKSRC);
640
641 /* Unblank the LCD */
642 au1100fb_fb_blank(VESA_NO_BLANKING, &fbdev->info);
643
Pete Popov3b495f22005-04-04 01:06:19 +0000644 return 0;
645}
Rodolfo Giomettif77f50c2006-06-26 00:26:49 -0700646#else
647#define au1100fb_drv_suspend NULL
648#define au1100fb_drv_resume NULL
649#endif
Pete Popov3b495f22005-04-04 01:06:19 +0000650
651static struct device_driver au1100fb_driver = {
652 .name = "au1100-lcd",
653 .bus = &platform_bus_type,
654
655 .probe = au1100fb_drv_probe,
656 .remove = au1100fb_drv_remove,
657 .suspend = au1100fb_drv_suspend,
658 .resume = au1100fb_drv_resume,
659};
660
661/*-------------------------------------------------------------------------*/
662
663/* Kernel driver */
664
665int au1100fb_setup(char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
667 char* this_opt;
Pete Popov3b495f22005-04-04 01:06:19 +0000668 int num_panels = ARRAY_SIZE(known_lcd_panels);
669 char* mode = NULL;
670 int panel_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Pete Popov3b495f22005-04-04 01:06:19 +0000672 if (num_panels <= 0) {
673 print_err("No LCD panels supported by driver!");
674 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
Pete Popov3b495f22005-04-04 01:06:19 +0000676
677 if (options) {
678 while ((this_opt = strsep(&options,",")) != NULL) {
679 /* Panel option */
680 if (!strncmp(this_opt, "panel:", 6)) {
681 int i;
682 this_opt += 6;
683 for (i = 0; i < num_panels; i++) {
684 if (!strncmp(this_opt,
685 known_lcd_panels[i].name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 strlen(this_opt))) {
Pete Popov3b495f22005-04-04 01:06:19 +0000687 panel_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 break;
689 }
690 }
Pete Popov3b495f22005-04-04 01:06:19 +0000691 if (i >= num_panels) {
692 print_warn("Panel %s not supported!", this_opt);
693 }
694 }
695 /* Mode option (only option that start with digit) */
696 else if (isdigit(this_opt[0])) {
697 mode = kmalloc(strlen(this_opt) + 1, GFP_KERNEL);
698 strncpy(mode, this_opt, strlen(this_opt) + 1);
699 }
700 /* Unsupported option */
701 else {
702 print_warn("Unsupported option \"%s\"", this_opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
705 }
706
Pete Popov3b495f22005-04-04 01:06:19 +0000707 drv_info.panel_idx = panel_idx;
708 drv_info.opt_mode = mode;
709
710 print_info("Panel=%s Mode=%s",
711 known_lcd_panels[drv_info.panel_idx].name,
712 drv_info.opt_mode ? drv_info.opt_mode : "default");
713
714 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
Pete Popov3b495f22005-04-04 01:06:19 +0000717int __init au1100fb_init(void)
718{
719 char* options;
720 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Pete Popov3b495f22005-04-04 01:06:19 +0000722 print_info("" DRIVER_DESC "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Pete Popov3b495f22005-04-04 01:06:19 +0000724 memset(&drv_info, 0, sizeof(drv_info));
725
726 if (fb_get_options(DRIVER_NAME, &options))
727 return -ENODEV;
728
729 /* Setup driver with options */
730 ret = au1100fb_setup(options);
731 if (ret < 0) {
732 print_err("Fail to setup driver");
733 return ret;
734 }
735
736 return driver_register(&au1100fb_driver);
737}
738
739void __exit au1100fb_cleanup(void)
740{
741 driver_unregister(&au1100fb_driver);
742
Jesper Juhl8f760782006-06-27 02:55:06 -0700743 kfree(drv_info.opt_mode);
Pete Popov3b495f22005-04-04 01:06:19 +0000744}
745
746module_init(au1100fb_init);
747module_exit(au1100fb_cleanup);
748
749MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750MODULE_LICENSE("GPL");