ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 #endif
64         .activate =     FB_ACTIVATE_NOW,
65         .width =        -1,
66         .height =       -1,
67         .pixclock =     20000,
68         .left_margin =  64,
69         .right_margin = 64,
70         .upper_margin = 32,
71         .lower_margin = 32,
72         .hsync_len =    64,
73         .vsync_len =    2,
74         .vmode =        FB_VMODE_NONINTERLACED,
75 };
76
77 /*
78  * Interface used by the world
79  */
80 int tx3912fb_init(void);
81
82 static int tx3912fb_setcolreg(u_int regno, u_int red, u_int green,
83                               u_int blue, u_int transp,
84                               struct fb_info *info);
85
86 /*
87  * Macros
88  */
89 #define get_line_length(xres_virtual, bpp) \
90                 (u_long) (((int) xres_virtual * (int) bpp + 7) >> 3)
91
92 /*
93  * Frame buffer operations structure used by console driver
94  */
95 static struct fb_ops tx3912fb_ops = {
96         .owner          = THIS_MODULE,
97         .fb_setcolreg   = tx3912fb_setcolreg,
98         .fb_fillrect    = cfb_fillrect,
99         .fb_copyarea    = cfb_copyarea,
100         .fb_imageblit   = cfb_imageblit,
101         .fb_cursor      = soft_cursor,
102 };
103
104 static int tx3912fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
105 {
106         /*
107          * Memory limit
108          */
109         line_length =
110             get_line_length(var->xres_virtual, var->bits_per_pixel);
111         if ((line_length * var->yres_virtual) > info->fix.smem_len)
112                 return -ENOMEM;
113
114         return 0;
115 }
116
117 static int tx3912fb_set_par(struct fb_info *info)
118 {
119         u_long tx3912fb_paddr = 0;
120
121         /* Disable the video logic */
122         outl(inl(TX3912_VIDEO_CTRL1) &
123              ~(TX3912_VIDEO_CTRL1_ENVID | TX3912_VIDEO_CTRL1_DISPON),
124              TX3912_VIDEO_CTRL1);
125         udelay(200);
126
127         /* Set start address for DMA transfer */
128         outl(tx3912fb_paddr, TX3912_VIDEO_CTRL3);
129
130         /* Set end address for DMA transfer */
131         outl((tx3912fb_paddr + tx3912fb_fix.smem_len + 1), TX3912_VIDEO_CTRL4);
132
133         /* Set the pixel depth */
134         switch (info->var.bits_per_pixel) {
135         case 1:
136                 /* Monochrome */
137                 outl(inl(TX3912_VIDEO_CTRL1) &
138                      ~TX3912_VIDEO_CTRL1_BITSEL_MASK, TX3912_VIDEO_CTRL1);
139                 info->fix.visual = FB_VISUAL_MONO10;
140                 break;
141         case 4:
142                 /* 4-bit gray */
143                 outl(inl(TX3912_VIDEO_CTRL1) &
144                      ~TX3912_VIDEO_CTRL1_BITSEL_MASK, TX3912_VIDEO_CTRL1);
145                 outl(inl(TX3912_VIDEO_CTRL1) |
146                      TX3912_VIDEO_CTRL1_BITSEL_4BIT_GRAY,
147                      TX3912_VIDEO_CTRL1);
148                 info->fix.visual = FB_VISUAL_TRUECOLOR;
149                 break;
150         case 8:
151                 /* 8-bit color */
152                 outl(inl(TX3912_VIDEO_CTRL1) &
153                      ~TX3912_VIDEO_CTRL1_BITSEL_MASK, TX3912_VIDEO_CTRL1);
154                 outl(inl(TX3912_VIDEO_CTRL1) |
155                      TX3912_VIDEO_CTRL1_BITSEL_8BIT_COLOR,
156                      TX3912_VIDEO_CTRL1);
157                 info->fix.visual = FB_VISUAL_TRUECOLOR;
158                 break;
159         case 2:
160         default:
161                 /* 2-bit gray */
162                 outl(inl(TX3912_VIDEO_CTRL1) &
163                      ~TX3912_VIDEO_CTRL1_BITSEL_MASK, TX3912_VIDEO_CTRL1);
164                 outl(inl(TX3912_VIDEO_CTRL1) |
165                      TX3912_VIDEO_CTRL1_BITSEL_2BIT_GRAY,
166                      TX3912_VIDEO_CTRL1);
167                 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
168                 break;
169         }
170
171         /* Enable the video clock */
172         outl(inl(TX3912_CLK_CTRL) | TX3912_CLK_CTRL_ENVIDCLK,
173              TX3912_CLK_CTRL);
174
175         /* Unfreeze video logic and enable DF toggle */
176         outl(inl(TX3912_VIDEO_CTRL1) &
177              ~(TX3912_VIDEO_CTRL1_ENFREEZEFRAME |
178                TX3912_VIDEO_CTRL1_DFMODE)
179              , TX3912_VIDEO_CTRL1);
180         udelay(200);
181
182         /* Enable the video logic */
183         outl(inl(TX3912_VIDEO_CTRL1) |
184              (TX3912_VIDEO_CTRL1_ENVID | TX3912_VIDEO_CTRL1_DISPON),
185              TX3912_VIDEO_CTRL1);
186
187         info->fix.line_length = get_line_length(var->xres_virtual,
188                                             var->bits_per_pixel);
189 }
190
191 /*
192  * Set a single color register
193  */
194 static int tx3912fb_setcolreg(u_int regno, u_int red, u_int green,
195                               u_int blue, u_int transp,
196                               struct fb_info *info)
197 {
198         if (regno > 255)
199                 return 1;
200
201         if (regno < 16)
202                 ((u32 *)(info->pseudo_palette))[regno] = ((red & 0xe000) >> 8)
203                     | ((green & 0xe000) >> 11)
204                     | ((blue & 0xc000) >> 14);
205         return 0;
206 }
207
208 /*
209  * Initialization of the framebuffer
210  */
211 int __init tx3912fb_init(void)
212 {
213         u_long tx3912fb_paddr = 0;
214         int size = (info->var.bits_per_pixel == 8) ? 256 : 16;
215
216         /* Disable the video logic */
217         outl(inl(TX3912_VIDEO_CTRL1) &
218              ~(TX3912_VIDEO_CTRL1_ENVID | TX3912_VIDEO_CTRL1_DISPON),
219              TX3912_VIDEO_CTRL1);
220         udelay(200);
221
222         /* Set start address for DMA transfer */
223         outl(tx3912fb_paddr, TX3912_VIDEO_CTRL3);
224
225         /* Set end address for DMA transfer */
226         outl((tx3912fb_paddr + tx3912fb_fix.smem_len + 1), TX3912_VIDEO_CTRL4);
227
228         /* Set the pixel depth */
229         switch (tx3912fb_var.bits_per_pixel) {
230         case 1:
231                 /* Monochrome */
232                 outl(inl(TX3912_VIDEO_CTRL1) &
233                      ~TX3912_VIDEO_CTRL1_BITSEL_MASK, TX3912_VIDEO_CTRL1);
234                 tx3912fb_fix.visual = FB_VISUAL_MONO10;
235                 break;
236         case 4:
237                 /* 4-bit gray */
238                 outl(inl(TX3912_VIDEO_CTRL1) &
239                      ~TX3912_VIDEO_CTRL1_BITSEL_MASK, TX3912_VIDEO_CTRL1);
240                 outl(inl(TX3912_VIDEO_CTRL1) |
241                      TX3912_VIDEO_CTRL1_BITSEL_4BIT_GRAY,
242                      TX3912_VIDEO_CTRL1);
243                 tx3912fb_fix.visual = FB_VISUAL_TRUECOLOR;
244                 tx3912fb_fix.grayscale = 1;
245                 break;
246         case 8:
247                 /* 8-bit color */
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_8BIT_COLOR,
252                      TX3912_VIDEO_CTRL1);
253                 tx3912fb_fix.visual = FB_VISUAL_TRUECOLOR;
254                 break;
255         case 2:
256         default:
257                 /* 2-bit gray */
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_2BIT_GRAY,
262                      TX3912_VIDEO_CTRL1);
263                 tx3912fb_fix.visual = FB_VISUAL_PSEUDOCOLOR;
264                 tx3912fb_fix.grayscale = 1;
265                 break;
266         }
267
268         /* Enable the video clock */
269         outl(inl(TX3912_CLK_CTRL) | TX3912_CLK_CTRL_ENVIDCLK,
270                 TX3912_CLK_CTRL);
271
272         /* Unfreeze video logic and enable DF toggle */
273         outl(inl(TX3912_VIDEO_CTRL1) &
274                 ~(TX3912_VIDEO_CTRL1_ENFREEZEFRAME | TX3912_VIDEO_CTRL1_DFMODE),
275                 TX3912_VIDEO_CTRL1);
276         udelay(200);
277
278         /* Clear the framebuffer */
279         memset((void *) tx3912fb_fix.smem_start, 0xff, tx3912fb_fix.smem_len);
280         udelay(200);
281
282         /* Enable the video logic */
283         outl(inl(TX3912_VIDEO_CTRL1) |
284                 (TX3912_VIDEO_CTRL1_ENVID | TX3912_VIDEO_CTRL1_DISPON),
285                 TX3912_VIDEO_CTRL1);
286
287         /*
288          * Memory limit
289          */
290         tx3912fb_fix.line_length =
291             get_line_length(tx3912fb_var.xres_virtual, tx3912fb_var.bits_per_pixel);
292         if ((tx3912fb_fix.line_length * tx3912fb_var.yres_virtual) > tx3912fb_fix.smem_len)
293                 return -ENOMEM;
294
295         fb_info.fbops = &tx3912fb_ops;
296         fb_info.var = tx3912fb_var;
297         fb_info.fix = tx3912fb_fix;
298         fb_info.pseudo_palette = pseudo_palette;
299         fb_info.flags = FBINFO_FLAG_DEFAULT;
300
301         /* Clear the framebuffer */
302         memset((void *) fb_info.fix.smem_start, 0xff, fb_info.fix.smem_len);
303         udelay(200);
304
305         fb_alloc_cmap(&info->cmap, size, 0);
306
307         if (register_framebuffer(&fb_info) < 0)
308                 return -1;
309
310         printk(KERN_INFO "fb%d: TX3912 frame buffer using %uKB.\n",
311                fb_info.node, (u_int) (fb_info.fix.smem_len >> 10));
312         return 0;
313 }
314
315 int __init tx3912fb_setup(char *options)
316 {
317         char *this_opt;
318
319         if (!options || !*options)
320                 return 0;
321
322         while ((this_opt = strsep(&options, ","))) {
323                 if (!strncmp(options, "bpp:", 4))       
324                         tx3912fb_var.bits_per_pixel = simple_strtoul(options+4, NULL, 0);
325         }       
326         return 0;
327 }
328
329 MODULE_LICENSE("GPL");