patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / video / 68328fb.c
1 /*
2  *  linux/drivers/video/68328fb.c -- Low level implementation of the
3  *                                   mc68x328 LCD frame buffer device
4  *
5  *      Copyright (C) 2003 Georges Menie
6  *
7  *  This driver assumes an already configured controller (e.g. from config.c)
8  *  Keep the code clean of board specific initialization.
9  *
10  *  This code has not been tested with colors, colormap management functions
11  *  are minimal (no colormap data written to the 68328 registers...)
12  *
13  *  initial version of this driver:
14  *    Copyright (C) 1998,1999 Kenneth Albanowski <kjahds@kjahds.com>,
15  *                            The Silver Hammer Group, Ltd.
16  *
17  *  this version is based on :
18  *
19  *  linux/drivers/video/vfb.c -- Virtual frame buffer device
20  *
21  *      Copyright (C) 2002 James Simmons
22  *
23  *      Copyright (C) 1997 Geert Uytterhoeven
24  *
25  *  This file is subject to the terms and conditions of the GNU General Public
26  *  License. See the file COPYING in the main directory of this archive for
27  *  more details.
28  */
29
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/errno.h>
33 #include <linux/string.h>
34 #include <linux/mm.h>
35 #include <linux/tty.h>
36 #include <linux/slab.h>
37 #include <linux/vmalloc.h>
38 #include <linux/delay.h>
39 #include <linux/interrupt.h>
40 #include <asm/uaccess.h>
41 #include <linux/fb.h>
42 #include <linux/init.h>
43
44 #if defined(CONFIG_M68VZ328)
45 #include <asm/MC68VZ328.h>
46 #elif defined(CONFIG_M68EZ328)
47 #include <asm/MC68EZ328.h>
48 #elif defined(CONFIG_M68328)
49 #include <asm/MC68328.h>
50 #else
51 #error wrong architecture for the MC68x328 frame buffer device
52 #endif
53
54 #if defined(CONFIG_FB_68328_INVERT)
55 #define MC68X328FB_MONO_VISUAL FB_VISUAL_MONO01
56 #else
57 #define MC68X328FB_MONO_VISUAL FB_VISUAL_MONO10
58 #endif
59
60 static u_long videomemory;
61 static u_long videomemorysize;
62
63 static struct fb_info fb_info;
64 static u32 mc68x328fb_pseudo_palette[17];
65
66 static struct fb_var_screeninfo mc68x328fb_default __initdata = {
67         .red =          { 0, 8, 0 },
68         .green =        { 0, 8, 0 },
69         .blue =         { 0, 8, 0 },
70         .activate =     FB_ACTIVATE_TEST,
71         .height =       -1,
72         .width =        -1,
73         .pixclock =     20000,
74         .left_margin =  64,
75         .right_margin = 64,
76         .upper_margin = 32,
77         .lower_margin = 32,
78         .hsync_len =    64,
79         .vsync_len =    2,
80         .vmode =        FB_VMODE_NONINTERLACED,
81 };
82
83 static struct fb_fix_screeninfo mc68x328fb_fix __initdata = {
84         .id =           "68328fb",
85         .type =         FB_TYPE_PACKED_PIXELS,
86         .xpanstep =     1,
87         .ypanstep =     1,
88         .ywrapstep =    1,
89         .accel =        FB_ACCEL_NONE,
90 };
91
92     /*
93      *  Interface used by the world
94      */
95 int mc68x328fb_init(void);
96 int mc68x328fb_setup(char *);
97
98 static int mc68x328fb_check_var(struct fb_var_screeninfo *var,
99                          struct fb_info *info);
100 static int mc68x328fb_set_par(struct fb_info *info);
101 static int mc68x328fb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
102                          u_int transp, struct fb_info *info);
103 static int mc68x328fb_pan_display(struct fb_var_screeninfo *var,
104                            struct fb_info *info);
105 static int mc68x328fb_mmap(struct fb_info *info, struct file *file,
106                     struct vm_area_struct *vma);
107
108 static struct fb_ops mc68x328fb_ops = {
109         .fb_check_var   = mc68x328fb_check_var,
110         .fb_set_par     = mc68x328fb_set_par,
111         .fb_setcolreg   = mc68x328fb_setcolreg,
112         .fb_pan_display = mc68x328fb_pan_display,
113         .fb_fillrect    = cfb_fillrect,
114         .fb_copyarea    = cfb_copyarea,
115         .fb_imageblit   = cfb_imageblit,
116         .fb_cursor      = soft_cursor,
117         .fb_mmap        = mc68x328fb_mmap,
118 };
119
120     /*
121      *  Internal routines
122      */
123
124 static u_long get_line_length(int xres_virtual, int bpp)
125 {
126         u_long length;
127
128         length = xres_virtual * bpp;
129         length = (length + 31) & ~31;
130         length >>= 3;
131         return (length);
132 }
133
134     /*
135      *  Setting the video mode has been split into two parts.
136      *  First part, xxxfb_check_var, must not write anything
137      *  to hardware, it should only verify and adjust var.
138      *  This means it doesn't alter par but it does use hardware
139      *  data from it to check this var. 
140      */
141
142 static int mc68x328fb_check_var(struct fb_var_screeninfo *var,
143                          struct fb_info *info)
144 {
145         u_long line_length;
146
147         /*
148          *  FB_VMODE_CONUPDATE and FB_VMODE_SMOOTH_XPAN are equal!
149          *  as FB_VMODE_SMOOTH_XPAN is only used internally
150          */
151
152         if (var->vmode & FB_VMODE_CONUPDATE) {
153                 var->vmode |= FB_VMODE_YWRAP;
154                 var->xoffset = info->var.xoffset;
155                 var->yoffset = info->var.yoffset;
156         }
157
158         /*
159          *  Some very basic checks
160          */
161         if (!var->xres)
162                 var->xres = 1;
163         if (!var->yres)
164                 var->yres = 1;
165         if (var->xres > var->xres_virtual)
166                 var->xres_virtual = var->xres;
167         if (var->yres > var->yres_virtual)
168                 var->yres_virtual = var->yres;
169         if (var->bits_per_pixel <= 1)
170                 var->bits_per_pixel = 1;
171         else if (var->bits_per_pixel <= 8)
172                 var->bits_per_pixel = 8;
173         else if (var->bits_per_pixel <= 16)
174                 var->bits_per_pixel = 16;
175         else if (var->bits_per_pixel <= 24)
176                 var->bits_per_pixel = 24;
177         else if (var->bits_per_pixel <= 32)
178                 var->bits_per_pixel = 32;
179         else
180                 return -EINVAL;
181
182         if (var->xres_virtual < var->xoffset + var->xres)
183                 var->xres_virtual = var->xoffset + var->xres;
184         if (var->yres_virtual < var->yoffset + var->yres)
185                 var->yres_virtual = var->yoffset + var->yres;
186
187         /*
188          *  Memory limit
189          */
190         line_length =
191             get_line_length(var->xres_virtual, var->bits_per_pixel);
192         if (line_length * var->yres_virtual > videomemorysize)
193                 return -ENOMEM;
194
195         /*
196          * Now that we checked it we alter var. The reason being is that the video
197          * mode passed in might not work but slight changes to it might make it 
198          * work. This way we let the user know what is acceptable.
199          */
200         switch (var->bits_per_pixel) {
201         case 1:
202         case 8:
203                 var->red.offset = 0;
204                 var->red.length = 8;
205                 var->green.offset = 0;
206                 var->green.length = 8;
207                 var->blue.offset = 0;
208                 var->blue.length = 8;
209                 var->transp.offset = 0;
210                 var->transp.length = 0;
211                 break;
212         case 16:                /* RGBA 5551 */
213                 if (var->transp.length) {
214                         var->red.offset = 0;
215                         var->red.length = 5;
216                         var->green.offset = 5;
217                         var->green.length = 5;
218                         var->blue.offset = 10;
219                         var->blue.length = 5;
220                         var->transp.offset = 15;
221                         var->transp.length = 1;
222                 } else {        /* RGB 565 */
223                         var->red.offset = 0;
224                         var->red.length = 5;
225                         var->green.offset = 5;
226                         var->green.length = 6;
227                         var->blue.offset = 11;
228                         var->blue.length = 5;
229                         var->transp.offset = 0;
230                         var->transp.length = 0;
231                 }
232                 break;
233         case 24:                /* RGB 888 */
234                 var->red.offset = 0;
235                 var->red.length = 8;
236                 var->green.offset = 8;
237                 var->green.length = 8;
238                 var->blue.offset = 16;
239                 var->blue.length = 8;
240                 var->transp.offset = 0;
241                 var->transp.length = 0;
242                 break;
243         case 32:                /* RGBA 8888 */
244                 var->red.offset = 0;
245                 var->red.length = 8;
246                 var->green.offset = 8;
247                 var->green.length = 8;
248                 var->blue.offset = 16;
249                 var->blue.length = 8;
250                 var->transp.offset = 24;
251                 var->transp.length = 8;
252                 break;
253         }
254         var->red.msb_right = 0;
255         var->green.msb_right = 0;
256         var->blue.msb_right = 0;
257         var->transp.msb_right = 0;
258
259         return 0;
260 }
261
262 /* This routine actually sets the video mode. It's in here where we
263  * the hardware state info->par and fix which can be affected by the 
264  * change in par. For this driver it doesn't do much. 
265  */
266 static int mc68x328fb_set_par(struct fb_info *info)
267 {
268         info->fix.line_length = get_line_length(info->var.xres_virtual,
269                                                 info->var.bits_per_pixel);
270         return 0;
271 }
272
273     /*
274      *  Set a single color register. The values supplied are already
275      *  rounded down to the hardware's capabilities (according to the
276      *  entries in the var structure). Return != 0 for invalid regno.
277      */
278
279 static int mc68x328fb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
280                          u_int transp, struct fb_info *info)
281 {
282         if (regno >= 256)       /* no. of hw registers */
283                 return 1;
284         /*
285          * Program hardware... do anything you want with transp
286          */
287
288         /* grayscale works only partially under directcolor */
289         if (info->var.grayscale) {
290                 /* grayscale = 0.30*R + 0.59*G + 0.11*B */
291                 red = green = blue =
292                     (red * 77 + green * 151 + blue * 28) >> 8;
293         }
294
295         /* Directcolor:
296          *   var->{color}.offset contains start of bitfield
297          *   var->{color}.length contains length of bitfield
298          *   {hardwarespecific} contains width of RAMDAC
299          *   cmap[X] is programmed to (X << red.offset) | (X << green.offset) | (X << blue.offset)
300          *   RAMDAC[X] is programmed to (red, green, blue)
301          * 
302          * Pseudocolor:
303          *    uses offset = 0 && length = RAMDAC register width.
304          *    var->{color}.offset is 0
305          *    var->{color}.length contains widht of DAC
306          *    cmap is not used
307          *    RAMDAC[X] is programmed to (red, green, blue)
308          * Truecolor:
309          *    does not use DAC. Usually 3 are present.
310          *    var->{color}.offset contains start of bitfield
311          *    var->{color}.length contains length of bitfield
312          *    cmap is programmed to (red << red.offset) | (green << green.offset) |
313          *                      (blue << blue.offset) | (transp << transp.offset)
314          *    RAMDAC does not exist
315          */
316 #define CNVT_TOHW(val,width) ((((val)<<(width))+0x7FFF-(val))>>16)
317         switch (info->fix.visual) {
318         case FB_VISUAL_TRUECOLOR:
319         case FB_VISUAL_PSEUDOCOLOR:
320                 red = CNVT_TOHW(red, info->var.red.length);
321                 green = CNVT_TOHW(green, info->var.green.length);
322                 blue = CNVT_TOHW(blue, info->var.blue.length);
323                 transp = CNVT_TOHW(transp, info->var.transp.length);
324                 break;
325         case FB_VISUAL_DIRECTCOLOR:
326                 red = CNVT_TOHW(red, 8);        /* expect 8 bit DAC */
327                 green = CNVT_TOHW(green, 8);
328                 blue = CNVT_TOHW(blue, 8);
329                 /* hey, there is bug in transp handling... */
330                 transp = CNVT_TOHW(transp, 8);
331                 break;
332         }
333 #undef CNVT_TOHW
334         /* Truecolor has hardware independent palette */
335         if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
336                 u32 v;
337
338                 if (regno >= 16)
339                         return 1;
340
341                 v = (red << info->var.red.offset) |
342                     (green << info->var.green.offset) |
343                     (blue << info->var.blue.offset) |
344                     (transp << info->var.transp.offset);
345                 switch (info->var.bits_per_pixel) {
346                 case 8:
347                         break;
348                 case 16:
349                         ((u32 *) (info->pseudo_palette))[regno] = v;
350                         break;
351                 case 24:
352                 case 32:
353                         ((u32 *) (info->pseudo_palette))[regno] = v;
354                         break;
355                 }
356                 return 0;
357         }
358         return 0;
359 }
360
361     /*
362      *  Pan or Wrap the Display
363      *
364      *  This call looks only at xoffset, yoffset and the FB_VMODE_YWRAP flag
365      */
366
367 static int mc68x328fb_pan_display(struct fb_var_screeninfo *var,
368                            struct fb_info *info)
369 {
370         if (var->vmode & FB_VMODE_YWRAP) {
371                 if (var->yoffset < 0
372                     || var->yoffset >= info->var.yres_virtual
373                     || var->xoffset)
374                         return -EINVAL;
375         } else {
376                 if (var->xoffset + var->xres > info->var.xres_virtual ||
377                     var->yoffset + var->yres > info->var.yres_virtual)
378                         return -EINVAL;
379         }
380         info->var.xoffset = var->xoffset;
381         info->var.yoffset = var->yoffset;
382         if (var->vmode & FB_VMODE_YWRAP)
383                 info->var.vmode |= FB_VMODE_YWRAP;
384         else
385                 info->var.vmode &= ~FB_VMODE_YWRAP;
386         return 0;
387 }
388
389     /*
390      *  Most drivers don't need their own mmap function 
391      */
392
393 static int mc68x328fb_mmap(struct fb_info *info, struct file *file,
394                     struct vm_area_struct *vma)
395 {
396 #ifndef MMU
397         /* this is uClinux (no MMU) specific code */
398
399         vma->vm_flags |= VM_RESERVED;
400         vma->vm_start = videomemory;
401
402         return 0;
403 #else
404         return -EINVAL;
405 #endif
406 }
407
408 int __init mc68x328fb_setup(char *options)
409 {
410 #if 0
411         char *this_opt;
412 #endif
413
414         if (!options || !*options)
415                 return 1;
416 #if 0
417         while ((this_opt = strsep(&options, ",")) != NULL) {
418                 if (!*this_opt)
419                         continue;
420                 if (!strncmp(this_opt, "disable", 7))
421                         mc68x328fb_enable = 0;
422         }
423 #endif
424         return 1;
425 }
426
427     /*
428      *  Initialisation
429      */
430
431 int __init mc68x328fb_init(void)
432 {
433         /*
434          *  initialize the default mode from the LCD controller registers
435          */
436         mc68x328fb_default.xres = LXMAX;
437         mc68x328fb_default.yres = LYMAX+1;
438         mc68x328fb_default.xres_virtual = mc68x328fb_default.xres;
439         mc68x328fb_default.yres_virtual = mc68x328fb_default.yres;
440         mc68x328fb_default.bits_per_pixel = 1 + (LPICF & 0x01);
441         videomemory = LSSA;
442         videomemorysize = (mc68x328fb_default.xres_virtual+7) / 8 *
443                 mc68x328fb_default.yres_virtual * mc68x328fb_default.bits_per_pixel;
444
445         fb_info.screen_base = (void *)videomemory;
446         fb_info.fbops = &mc68x328fb_ops;
447         fb_info.var = mc68x328fb_default;
448         fb_info.fix = mc68x328fb_fix;
449         fb_info.fix.smem_start = videomemory;
450         fb_info.fix.smem_len = videomemorysize;
451         fb_info.fix.line_length =
452                 get_line_length(mc68x328fb_default.xres_virtual, mc68x328fb_default.bits_per_pixel);
453         fb_info.fix.visual = (mc68x328fb_default.bits_per_pixel) == 1 ?
454                 MC68X328FB_MONO_VISUAL : FB_VISUAL_PSEUDOCOLOR;
455         fb_info.pseudo_palette = &mc68x328fb_pseudo_palette;
456         fb_info.flags = FBINFO_FLAG_DEFAULT;
457
458         fb_alloc_cmap(&fb_info.cmap, 256, 0);
459
460         if (register_framebuffer(&fb_info) < 0) {
461                 return -EINVAL;
462         }
463
464         printk(KERN_INFO
465                 "fb%d: %s frame buffer device\n", fb_info.node, fb_info.fix.id);
466         printk(KERN_INFO
467                 "fb%d: %dx%dx%d at 0x%08lx\n", fb_info.node,
468                 mc68x328fb_default.xres_virtual, mc68x328fb_default.yres_virtual,
469                 1 << mc68x328fb_default.bits_per_pixel, videomemory);
470
471         return 0;
472 }
473
474 #ifdef MODULE
475
476 static void __exit mc68x328fb_cleanup(void)
477 {
478         unregister_framebuffer(&fb_info);
479 }
480
481 module_init(mc68x328fb_init);
482 module_exit(mc68x328fb_cleanup);
483
484 MODULE_LICENSE("GPL");
485 #endif                          /* MODULE */