vserver 1.9.3
[linux-2.6.git] / drivers / video / tx3912fb.c
1 /*
2  *  drivers/video/tx3912fb.c
3  *
4  *  Copyright (C) 1999 Harald Koerfgen
5  *  Copyright (C) 2001 Steven Hill (sjhill@realitydiluted.com)
6  *
7  * This file is subject to the terms and conditions of the GNU General Public
8  * License. See the file COPYING in the main directory of this archive for
9  * more details.
10  *
11  *  Framebuffer for LCD controller in TMPR3912/05 and PR31700 processors
12  */
13 #include <linux/config.h>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/errno.h>
17 #include <linux/string.h>
18 #include <linux/tty.h>
19 #include <linux/delay.h>
20 #include <linux/interrupt.h>
21 #include <linux/init.h>
22 #include <linux/pm.h>
23 #include <linux/fb.h>
24 #include <asm/io.h>
25 #include <asm/bootinfo.h>
26 #include <asm/uaccess.h>
27 #include <asm/tx3912.h>
28 #include <video/tx3912.h>
29
30 /*
31  * Frame buffer, palette and console structures
32  */
33 static struct fb_info fb_info;
34 static u32 cfb8[16];
35
36 static struct fb_fix_screeninfo tx3912fb_fix __initdata = {
37         .id =           "tx3912fb",
38 #ifdef CONFIG_NINO_16MB
39         .smem_len =     (240 * 320),
40 #else
41         .smem_len =     ((240 * 320)/2),
42 #endif
43         .type =         FB_TYPE_PACKED_PIXELS,
44         .visual =       FB_VISUAL_TRUECOLOR, 
45         .xpanstep =     1,
46         .ypanstep =     1,
47         .ywrapstep =    1,
48         .accel =        FB_ACCEL_NONE,
49 };
50
51 static struct fb_var_screeninfo tx3912fb_var = {
52         .xres =         240,
53         .yres =         320,
54         .xres_virtual = 240,
55         .yres_virtual = 320,
56 #ifdef CONFIG_NINO_16MB
57         .bits_per_pixel =8,
58         .red =          { 5, 3, 0 },    /* RGB 332 */
59         .green =        { 2, 3, 0 },
60         .blue =         { 0, 2, 0 },
61 #else
62         .bits_per_pixel =4,
63         .red =          { 0, 4, 0 },    /* ??? */
64         .green =        { 0, 4, 0 },
65         .blue =         { 0, 4, 0 },
66 #endif
67         .activate =     FB_ACTIVATE_NOW,
68         .width =        -1,
69         .height =       -1,
70         .pixclock =     20000,
71         .left_margin =  64,
72         .right_margin = 64,
73         .upper_margin = 32,
74         .lower_margin = 32,
75         .hsync_len =    64,
76         .vsync_len =    2,
77         .vmode =        FB_VMODE_NONINTERLACED,
78 };
79
80 /*
81  * Interface used by the world
82  */
83 int tx3912fb_init(void);
84
85 static int tx3912fb_setcolreg(u_int regno, u_int red, u_int green,
86                               u_int blue, u_int transp,
87                               struct fb_info *info);
88
89 /*
90  * Macros
91  */
92 #define get_line_length(xres_virtual, bpp) \
93                 (u_long) (((int) xres_virtual * (int) bpp + 7) >> 3)
94
95 /*
96  * Frame buffer operations structure used by console driver
97  */
98 static struct fb_ops tx3912fb_ops = {
99         .owner          = THIS_MODULE,
100         .fb_setcolreg   = tx3912fb_setcolreg,
101         .fb_fillrect    = cfb_fillrect,
102         .fb_copyarea    = cfb_copyarea,
103         .fb_imageblit   = cfb_imageblit,
104         .fb_cursor      = soft_cursor,
105 };
106
107 static int tx3912fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
108 {
109         /*
110          * Memory limit
111          */
112         line_length =
113             get_line_length(var->xres_virtual, var->bits_per_pixel);
114         if ((line_length * var->yres_virtual) > info->fix.smem_len)
115                 return -ENOMEM;
116
117         return 0;
118 }
119
120 static int tx3912fb_set_par(struct fb_info *info)
121 {
122         u_long tx3912fb_paddr = 0;
123
124         /* Disable the video logic */
125         outl(inl(TX3912_VIDEO_CTRL1) &
126              ~(TX3912_VIDEO_CTRL1_ENVID | TX3912_VIDEO_CTRL1_DISPON),
127              TX3912_VIDEO_CTRL1);
128         udelay(200);
129
130         /* Set start address for DMA transfer */
131         outl(tx3912fb_paddr, TX3912_VIDEO_CTRL3);
132
133         /* Set end address for DMA transfer */
134         outl((tx3912fb_paddr + tx3912fb_fix.smem_len + 1), TX3912_VIDEO_CTRL4);
135
136         /* Set the pixel depth */
137         switch (info->var.bits_per_pixel) {
138         case 1:
139                 /* Monochrome */
140                 outl(inl(TX3912_VIDEO_CTRL1) &
141                      ~TX3912_VIDEO_CTRL1_BITSEL_MASK, TX3912_VIDEO_CTRL1);
142                 info->fix.visual = FB_VISUAL_MONO10;
143                 break;
144         case 4:
145                 /* 4-bit gray */
146                 outl(inl(TX3912_VIDEO_CTRL1) &
147                      ~TX3912_VIDEO_CTRL1_BITSEL_MASK, TX3912_VIDEO_CTRL1);
148                 outl(inl(TX3912_VIDEO_CTRL1) |
149                      TX3912_VIDEO_CTRL1_BITSEL_4BIT_GRAY,
150                      TX3912_VIDEO_CTRL1);
151                 info->fix.visual = FB_VISUAL_TRUECOLOR;
152                 break;
153         case 8:
154                 /* 8-bit color */
155                 outl(inl(TX3912_VIDEO_CTRL1) &
156                      ~TX3912_VIDEO_CTRL1_BITSEL_MASK, TX3912_VIDEO_CTRL1);
157                 outl(inl(TX3912_VIDEO_CTRL1) |
158                      TX3912_VIDEO_CTRL1_BITSEL_8BIT_COLOR,
159                      TX3912_VIDEO_CTRL1);
160                 info->fix.visual = FB_VISUAL_TRUECOLOR;
161                 break;
162         case 2:
163         default:
164                 /* 2-bit gray */
165                 outl(inl(TX3912_VIDEO_CTRL1) &
166                      ~TX3912_VIDEO_CTRL1_BITSEL_MASK, TX3912_VIDEO_CTRL1);
167                 outl(inl(TX3912_VIDEO_CTRL1) |
168                      TX3912_VIDEO_CTRL1_BITSEL_2BIT_GRAY,
169                      TX3912_VIDEO_CTRL1);
170                 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
171                 break;
172         }
173
174         /* Enable the video clock */
175         outl(inl(TX3912_CLK_CTRL) | TX3912_CLK_CTRL_ENVIDCLK,
176              TX3912_CLK_CTRL);
177
178         /* Unfreeze video logic and enable DF toggle */
179         outl(inl(TX3912_VIDEO_CTRL1) &
180              ~(TX3912_VIDEO_CTRL1_ENFREEZEFRAME |
181                TX3912_VIDEO_CTRL1_DFMODE)
182              , TX3912_VIDEO_CTRL1);
183         udelay(200);
184
185         /* Enable the video logic */
186         outl(inl(TX3912_VIDEO_CTRL1) |
187              (TX3912_VIDEO_CTRL1_ENVID | TX3912_VIDEO_CTRL1_DISPON),
188              TX3912_VIDEO_CTRL1);
189
190         info->fix.line_length = get_line_length(var->xres_virtual,
191                                             var->bits_per_pixel);
192 }
193
194 /*
195  * Set a single color register
196  */
197 static int tx3912fb_setcolreg(u_int regno, u_int red, u_int green,
198                               u_int blue, u_int transp,
199                               struct fb_info *info)
200 {
201         if (regno > 255)
202                 return 1;
203
204         if (regno < 16)
205                 ((u32 *)(info->pseudo_palette))[regno] = ((red & 0xe000) >> 8)
206                     | ((green & 0xe000) >> 11)
207                     | ((blue & 0xc000) >> 14);
208         return 0;
209 }
210
211 int __init tx3912fb_setup(char *options);
212
213 /*
214  * Initialization of the framebuffer
215  */
216 int __init tx3912fb_init(void)
217 {
218         u_long tx3912fb_paddr = 0;
219         int size = (info->var.bits_per_pixel == 8) ? 256 : 16;
220         char *option = NULL;
221
222         if (fb_get_options("tx3912fb", &option))
223                 return -ENODEV;
224         tx3912fb_setup(option);
225
226         /* Disable the video logic */
227         outl(inl(TX3912_VIDEO_CTRL1) &
228              ~(TX3912_VIDEO_CTRL1_ENVID | TX3912_VIDEO_CTRL1_DISPON),
229              TX3912_VIDEO_CTRL1);
230         udelay(200);
231
232         /* Set start address for DMA transfer */
233         outl(tx3912fb_paddr, TX3912_VIDEO_CTRL3);
234
235         /* Set end address for DMA transfer */
236         outl((tx3912fb_paddr + tx3912fb_fix.smem_len + 1), TX3912_VIDEO_CTRL4);
237
238         /* Set the pixel depth */
239         switch (tx3912fb_var.bits_per_pixel) {
240         case 1:
241                 /* Monochrome */
242                 outl(inl(TX3912_VIDEO_CTRL1) &
243                      ~TX3912_VIDEO_CTRL1_BITSEL_MASK, TX3912_VIDEO_CTRL1);
244                 tx3912fb_fix.visual = FB_VISUAL_MONO10;
245                 break;
246         case 4:
247                 /* 4-bit gray */
248                 outl(inl(TX3912_VIDEO_CTRL1) &
249                      ~TX3912_VIDEO_CTRL1_BITSEL_MASK, TX3912_VIDEO_CTRL1);
250                 outl(inl(TX3912_VIDEO_CTRL1) |
251                      TX3912_VIDEO_CTRL1_BITSEL_4BIT_GRAY,
252                      TX3912_VIDEO_CTRL1);
253                 tx3912fb_fix.visual = FB_VISUAL_TRUECOLOR;
254                 tx3912fb_fix.grayscale = 1;
255                 break;
256         case 8:
257                 /* 8-bit color */
258                 outl(inl(TX3912_VIDEO_CTRL1) &
259                      ~TX3912_VIDEO_CTRL1_BITSEL_MASK, TX3912_VIDEO_CTRL1);
260                 outl(inl(TX3912_VIDEO_CTRL1) |
261                      TX3912_VIDEO_CTRL1_BITSEL_8BIT_COLOR,
262                      TX3912_VIDEO_CTRL1);
263                 tx3912fb_fix.visual = FB_VISUAL_TRUECOLOR;
264                 break;
265         case 2:
266         default:
267                 /* 2-bit gray */
268                 outl(inl(TX3912_VIDEO_CTRL1) &
269                      ~TX3912_VIDEO_CTRL1_BITSEL_MASK, TX3912_VIDEO_CTRL1);
270                 outl(inl(TX3912_VIDEO_CTRL1) |
271                      TX3912_VIDEO_CTRL1_BITSEL_2BIT_GRAY,
272                      TX3912_VIDEO_CTRL1);
273                 tx3912fb_fix.visual = FB_VISUAL_PSEUDOCOLOR;
274                 tx3912fb_fix.grayscale = 1;
275                 break;
276         }
277
278         /* Enable the video clock */
279         outl(inl(TX3912_CLK_CTRL) | TX3912_CLK_CTRL_ENVIDCLK,
280                 TX3912_CLK_CTRL);
281
282         /* Unfreeze video logic and enable DF toggle */
283         outl(inl(TX3912_VIDEO_CTRL1) &
284                 ~(TX3912_VIDEO_CTRL1_ENFREEZEFRAME | TX3912_VIDEO_CTRL1_DFMODE),
285                 TX3912_VIDEO_CTRL1);
286         udelay(200);
287
288         /* Clear the framebuffer */
289         memset((void *) tx3912fb_fix.smem_start, 0xff, tx3912fb_fix.smem_len);
290         udelay(200);
291
292         /* Enable the video logic */
293         outl(inl(TX3912_VIDEO_CTRL1) |
294                 (TX3912_VIDEO_CTRL1_ENVID | TX3912_VIDEO_CTRL1_DISPON),
295                 TX3912_VIDEO_CTRL1);
296
297         /*
298          * Memory limit
299          */
300         tx3912fb_fix.line_length =
301             get_line_length(tx3912fb_var.xres_virtual, tx3912fb_var.bits_per_pixel);
302         if ((tx3912fb_fix.line_length * tx3912fb_var.yres_virtual) > tx3912fb_fix.smem_len)
303                 return -ENOMEM;
304
305         fb_info.fbops = &tx3912fb_ops;
306         fb_info.var = tx3912fb_var;
307         fb_info.fix = tx3912fb_fix;
308         fb_info.pseudo_palette = pseudo_palette;
309         fb_info.flags = FBINFO_DEFAULT;
310
311         /* Clear the framebuffer */
312         memset((void *) fb_info.fix.smem_start, 0xff, fb_info.fix.smem_len);
313         udelay(200);
314
315         fb_alloc_cmap(&info->cmap, size, 0);
316
317         if (register_framebuffer(&fb_info) < 0)
318                 return -1;
319
320         printk(KERN_INFO "fb%d: TX3912 frame buffer using %uKB.\n",
321                fb_info.node, (u_int) (fb_info.fix.smem_len >> 10));
322         return 0;
323 }
324
325 int __init tx3912fb_setup(char *options)
326 {
327         char *this_opt;
328
329         if (!options || !*options)
330                 return 0;
331
332         while ((this_opt = strsep(&options, ","))) {
333                 if (!strncmp(options, "bpp:", 4))       
334                         tx3912fb_var.bits_per_pixel = simple_strtoul(options+4, NULL, 0);
335         }       
336         return 0;
337 }
338
339 module_init(tx3912fb_init);
340 MODULE_LICENSE("GPL");