Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[pandora-kernel.git] / drivers / video / clps711xfb.c
1 /*
2  *  linux/drivers/video/clps711xfb.c
3  *
4  *  Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  Framebuffer driver for the CLPS7111 and EP7212 processors.
21  */
22 #include <linux/mm.h>
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/slab.h>
26 #include <linux/fb.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
29
30 #include <mach/hardware.h>
31 #include <asm/mach-types.h>
32 #include <linux/uaccess.h>
33
34 struct fb_info  *cfb;
35
36 #define CMAP_MAX_SIZE   16
37
38 /*
39  * LCD AC Prescale.  This comes from the LCD panel manufacturers specifications.
40  * This determines how many clocks + 1 of CL1 before the M signal toggles.
41  * The number of lines on the display must not be divisible by this number.
42  */
43 static unsigned int lcd_ac_prescale = 13;
44
45 /*
46  *    Set a single color register. Return != 0 for invalid regno.
47  */
48 static int
49 clps7111fb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
50                      u_int transp, struct fb_info *info)
51 {
52         unsigned int level, mask, shift, pal;
53
54         if (regno >= (1 << info->var.bits_per_pixel))
55                 return 1;
56
57         /* gray = 0.30*R + 0.58*G + 0.11*B */
58         level = (red * 77 + green * 151 + blue * 28) >> 20;
59
60         /*
61          * On an LCD, a high value is dark, while a low value is light. 
62          * So we invert the level.
63          *
64          * This isn't true on all machines, so we only do it on EDB7211.
65          *  --rmk
66          */
67         if (machine_is_edb7211()) {
68                 level = 15 - level;
69         }
70
71         shift = 4 * (regno & 7);
72         level <<= shift;
73         mask  = 15 << shift;
74         level &= mask;
75
76         regno = regno < 8 ? PALLSW : PALMSW;
77
78         pal = clps_readl(regno);
79         pal = (pal & ~mask) | level;
80         clps_writel(pal, regno);
81
82         return 0;
83 }
84
85 /*
86  * Validate the purposed mode.
87  */     
88 static int
89 clps7111fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
90 {
91         var->transp.msb_right   = 0;
92         var->transp.offset      = 0;
93         var->transp.length      = 0;
94         var->red.msb_right      = 0;
95         var->red.offset         = 0;
96         var->red.length         = var->bits_per_pixel;
97         var->green              = var->red;
98         var->blue               = var->red;
99
100         if (var->bits_per_pixel > 4) 
101                 return -EINVAL;
102
103         return 0;
104 }
105
106 /*
107  * Set the hardware state.
108  */ 
109 static int 
110 clps7111fb_set_par(struct fb_info *info)
111 {
112         unsigned int lcdcon, syscon, pixclock;
113
114         switch (info->var.bits_per_pixel) {
115         case 1:
116                 info->fix.visual = FB_VISUAL_MONO01;
117                 break;
118         case 2:
119                 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
120                 break;
121         case 4:
122                 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
123                 break;
124         }
125
126         info->fix.line_length = info->var.xres_virtual * info->var.bits_per_pixel / 8;
127
128         lcdcon = (info->var.xres_virtual * info->var.yres_virtual * info->var.bits_per_pixel) / 128 - 1;
129         lcdcon |= ((info->var.xres_virtual / 16) - 1) << 13;
130         lcdcon |= lcd_ac_prescale << 25;
131
132         /*
133          * Calculate pixel prescale value from the pixclock.  This is:
134          *  36.864MHz / pixclock_mhz - 1.
135          * However, pixclock is in picoseconds, so this ends up being:
136          *  36864000 * pixclock_ps / 10^12 - 1
137          * and this will overflow the 32-bit math.  We perform this as
138          * (9 * 4096000 == 36864000):
139          *  pixclock_ps * 9 * (4096000 / 10^12) - 1
140          */
141         pixclock = 9 * info->var.pixclock / 244140 - 1;
142         lcdcon |= pixclock << 19;
143
144         if (info->var.bits_per_pixel == 4)
145                 lcdcon |= LCDCON_GSMD;
146         if (info->var.bits_per_pixel >= 2)
147                 lcdcon |= LCDCON_GSEN;
148
149         /*
150          * LCDCON must only be changed while the LCD is disabled
151          */
152         syscon = clps_readl(SYSCON1);
153         clps_writel(syscon & ~SYSCON1_LCDEN, SYSCON1);
154         clps_writel(lcdcon, LCDCON);
155         clps_writel(syscon | SYSCON1_LCDEN, SYSCON1);
156         return 0;
157 }
158
159 static int clps7111fb_blank(int blank, struct fb_info *info)
160 {
161         /* Enable/Disable LCD controller. */
162         if (blank)
163                 clps_writel(clps_readl(SYSCON1) & ~SYSCON1_LCDEN, SYSCON1);
164         else
165                 clps_writel(clps_readl(SYSCON1) | SYSCON1_LCDEN, SYSCON1);
166
167         return 0;
168 }
169
170 static struct fb_ops clps7111fb_ops = {
171         .owner          = THIS_MODULE,
172         .fb_check_var   = clps7111fb_check_var,
173         .fb_set_par     = clps7111fb_set_par,
174         .fb_setcolreg   = clps7111fb_setcolreg,
175         .fb_blank       = clps7111fb_blank,
176         .fb_fillrect    = cfb_fillrect,
177         .fb_copyarea    = cfb_copyarea,
178         .fb_imageblit   = cfb_imageblit,
179 };
180
181 static void clps711x_guess_lcd_params(struct fb_info *info)
182 {
183         unsigned int lcdcon, syscon, size;
184         unsigned long phys_base = PAGE_OFFSET;
185         void *virt_base = (void *)PAGE_OFFSET;
186
187         info->var.xres_virtual   = 640;
188         info->var.yres_virtual   = 240;
189         info->var.bits_per_pixel = 4;
190         info->var.activate       = FB_ACTIVATE_NOW;
191         info->var.height         = -1;
192         info->var.width          = -1;
193         info->var.pixclock       = 93006; /* 10.752MHz pixel clock */
194
195         /*
196          * If the LCD controller is already running, decode the values
197          * in LCDCON to xres/yres/bpp/pixclock/acprescale
198          */
199         syscon = clps_readl(SYSCON1);
200         if (syscon & SYSCON1_LCDEN) {
201                 lcdcon = clps_readl(LCDCON);
202
203                 /*
204                  * Decode GSMD and GSEN bits to bits per pixel
205                  */
206                 switch (lcdcon & (LCDCON_GSMD | LCDCON_GSEN)) {
207                 case LCDCON_GSMD | LCDCON_GSEN:
208                         info->var.bits_per_pixel = 4;
209                         break;
210
211                 case LCDCON_GSEN:
212                         info->var.bits_per_pixel = 2;
213                         break;
214
215                 default:
216                         info->var.bits_per_pixel = 1;
217                         break;
218                 }
219
220                 /*
221                  * Decode xres/yres
222                  */
223                 info->var.xres_virtual = (((lcdcon >> 13) & 0x3f) + 1) * 16;
224                 info->var.yres_virtual = (((lcdcon & 0x1fff) + 1) * 128) /
225                                           (info->var.xres_virtual *
226                                            info->var.bits_per_pixel);
227
228                 /*
229                  * Calculate pixclock
230                  */
231                 info->var.pixclock = (((lcdcon >> 19) & 0x3f) + 1) * 244140 / 9;
232
233                 /*
234                  * Grab AC prescale
235                  */
236                 lcd_ac_prescale = (lcdcon >> 25) & 0x1f;
237         }
238
239         info->var.xres = info->var.xres_virtual;
240         info->var.yres = info->var.yres_virtual;
241         info->var.grayscale = info->var.bits_per_pixel > 1;
242
243         size = info->var.xres * info->var.yres * info->var.bits_per_pixel / 8;
244
245         /*
246          * Might be worth checking to see if we can use the on-board
247          * RAM if size here...
248          * CLPS7110 - no on-board SRAM
249          * EP7212   - 38400 bytes
250          */
251         if (size <= 38400) {
252                 printk(KERN_INFO "CLPS711xFB: could use on-board SRAM?\n");
253         }
254
255         if ((syscon & SYSCON1_LCDEN) == 0) {
256                 /*
257                  * The display isn't running.  Ensure that
258                  * the display memory is empty.
259                  */
260                 memset(virt_base, 0, size);
261         }
262
263         info->screen_base    = virt_base;
264         info->fix.smem_start = phys_base;
265         info->fix.smem_len   = PAGE_ALIGN(size);
266         info->fix.type       = FB_TYPE_PACKED_PIXELS;
267 }
268
269 static int clps711x_fb_probe(struct platform_device *pdev)
270 {
271         int err = -ENOMEM;
272
273         if (fb_get_options("clps711xfb", NULL))
274                 return -ENODEV;
275
276         cfb = kzalloc(sizeof(*cfb), GFP_KERNEL);
277         if (!cfb)
278                 goto out;
279
280         strcpy(cfb->fix.id, "clps711x");
281
282         cfb->fbops              = &clps7111fb_ops;
283         cfb->flags              = FBINFO_DEFAULT;
284
285         clps711x_guess_lcd_params(cfb);
286
287         fb_alloc_cmap(&cfb->cmap, CMAP_MAX_SIZE, 0);
288
289         err = register_framebuffer(cfb);
290
291 out:    return err;
292 }
293
294 static int clps711x_fb_remove(struct platform_device *pdev)
295 {
296         unregister_framebuffer(cfb);
297         kfree(cfb);
298
299         return 0;
300 }
301
302 static struct platform_driver clps711x_fb_driver = {
303         .driver = {
304                 .name   = "video-clps711x",
305                 .owner  = THIS_MODULE,
306         },
307         .probe  = clps711x_fb_probe,
308         .remove = clps711x_fb_remove,
309 };
310 module_platform_driver(clps711x_fb_driver);
311
312 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
313 MODULE_DESCRIPTION("CLPS711X framebuffer driver");
314 MODULE_LICENSE("GPL");