[PATCH] intelfb: extend partial support of i915G to include i915GM
[safe/jmp/linux-2.6] / drivers / video / vfb.c
1 /*
2  *  linux/drivers/video/vfb.c -- Virtual frame buffer device
3  *
4  *      Copyright (C) 2002 James Simmons
5  *
6  *      Copyright (C) 1997 Geert Uytterhoeven
7  *
8  *  This file is subject to the terms and conditions of the GNU General Public
9  *  License. See the file COPYING in the main directory of this archive for
10  *  more details.
11  */
12
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/errno.h>
16 #include <linux/string.h>
17 #include <linux/mm.h>
18 #include <linux/tty.h>
19 #include <linux/slab.h>
20 #include <linux/vmalloc.h>
21 #include <linux/delay.h>
22 #include <linux/interrupt.h>
23 #include <linux/platform_device.h>
24
25 #include <asm/uaccess.h>
26 #include <linux/fb.h>
27 #include <linux/init.h>
28
29     /*
30      *  RAM we reserve for the frame buffer. This defines the maximum screen
31      *  size
32      *
33      *  The default can be overridden if the driver is compiled as a module
34      */
35
36 #define VIDEOMEMSIZE    (1*1024*1024)   /* 1 MB */
37
38 static void *videomemory;
39 static u_long videomemorysize = VIDEOMEMSIZE;
40 module_param(videomemorysize, ulong, 0);
41
42 static struct fb_var_screeninfo vfb_default __initdata = {
43         .xres =         640,
44         .yres =         480,
45         .xres_virtual = 640,
46         .yres_virtual = 480,
47         .bits_per_pixel = 8,
48         .red =          { 0, 8, 0 },
49         .green =        { 0, 8, 0 },
50         .blue =         { 0, 8, 0 },
51         .activate =     FB_ACTIVATE_TEST,
52         .height =       -1,
53         .width =        -1,
54         .pixclock =     20000,
55         .left_margin =  64,
56         .right_margin = 64,
57         .upper_margin = 32,
58         .lower_margin = 32,
59         .hsync_len =    64,
60         .vsync_len =    2,
61         .vmode =        FB_VMODE_NONINTERLACED,
62 };
63
64 static struct fb_fix_screeninfo vfb_fix __initdata = {
65         .id =           "Virtual FB",
66         .type =         FB_TYPE_PACKED_PIXELS,
67         .visual =       FB_VISUAL_PSEUDOCOLOR,
68         .xpanstep =     1,
69         .ypanstep =     1,
70         .ywrapstep =    1,
71         .accel =        FB_ACCEL_NONE,
72 };
73
74 static int vfb_enable __initdata = 0;   /* disabled by default */
75 module_param(vfb_enable, bool, 0);
76
77 static int vfb_check_var(struct fb_var_screeninfo *var,
78                          struct fb_info *info);
79 static int vfb_set_par(struct fb_info *info);
80 static int vfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
81                          u_int transp, struct fb_info *info);
82 static int vfb_pan_display(struct fb_var_screeninfo *var,
83                            struct fb_info *info);
84 static int vfb_mmap(struct fb_info *info, struct file *file,
85                     struct vm_area_struct *vma);
86
87 static struct fb_ops vfb_ops = {
88         .fb_check_var   = vfb_check_var,
89         .fb_set_par     = vfb_set_par,
90         .fb_setcolreg   = vfb_setcolreg,
91         .fb_pan_display = vfb_pan_display,
92         .fb_fillrect    = cfb_fillrect,
93         .fb_copyarea    = cfb_copyarea,
94         .fb_imageblit   = cfb_imageblit,
95         .fb_cursor      = soft_cursor,
96         .fb_mmap        = vfb_mmap,
97 };
98
99     /*
100      *  Internal routines
101      */
102
103 static u_long get_line_length(int xres_virtual, int bpp)
104 {
105         u_long length;
106
107         length = xres_virtual * bpp;
108         length = (length + 31) & ~31;
109         length >>= 3;
110         return (length);
111 }
112
113     /*
114      *  Setting the video mode has been split into two parts.
115      *  First part, xxxfb_check_var, must not write anything
116      *  to hardware, it should only verify and adjust var.
117      *  This means it doesn't alter par but it does use hardware
118      *  data from it to check this var. 
119      */
120
121 static int vfb_check_var(struct fb_var_screeninfo *var,
122                          struct fb_info *info)
123 {
124         u_long line_length;
125
126         /*
127          *  FB_VMODE_CONUPDATE and FB_VMODE_SMOOTH_XPAN are equal!
128          *  as FB_VMODE_SMOOTH_XPAN is only used internally
129          */
130
131         if (var->vmode & FB_VMODE_CONUPDATE) {
132                 var->vmode |= FB_VMODE_YWRAP;
133                 var->xoffset = info->var.xoffset;
134                 var->yoffset = info->var.yoffset;
135         }
136
137         /*
138          *  Some very basic checks
139          */
140         if (!var->xres)
141                 var->xres = 1;
142         if (!var->yres)
143                 var->yres = 1;
144         if (var->xres > var->xres_virtual)
145                 var->xres_virtual = var->xres;
146         if (var->yres > var->yres_virtual)
147                 var->yres_virtual = var->yres;
148         if (var->bits_per_pixel <= 1)
149                 var->bits_per_pixel = 1;
150         else if (var->bits_per_pixel <= 8)
151                 var->bits_per_pixel = 8;
152         else if (var->bits_per_pixel <= 16)
153                 var->bits_per_pixel = 16;
154         else if (var->bits_per_pixel <= 24)
155                 var->bits_per_pixel = 24;
156         else if (var->bits_per_pixel <= 32)
157                 var->bits_per_pixel = 32;
158         else
159                 return -EINVAL;
160
161         if (var->xres_virtual < var->xoffset + var->xres)
162                 var->xres_virtual = var->xoffset + var->xres;
163         if (var->yres_virtual < var->yoffset + var->yres)
164                 var->yres_virtual = var->yoffset + var->yres;
165
166         /*
167          *  Memory limit
168          */
169         line_length =
170             get_line_length(var->xres_virtual, var->bits_per_pixel);
171         if (line_length * var->yres_virtual > videomemorysize)
172                 return -ENOMEM;
173
174         /*
175          * Now that we checked it we alter var. The reason being is that the video
176          * mode passed in might not work but slight changes to it might make it 
177          * work. This way we let the user know what is acceptable.
178          */
179         switch (var->bits_per_pixel) {
180         case 1:
181         case 8:
182                 var->red.offset = 0;
183                 var->red.length = 8;
184                 var->green.offset = 0;
185                 var->green.length = 8;
186                 var->blue.offset = 0;
187                 var->blue.length = 8;
188                 var->transp.offset = 0;
189                 var->transp.length = 0;
190                 break;
191         case 16:                /* RGBA 5551 */
192                 if (var->transp.length) {
193                         var->red.offset = 0;
194                         var->red.length = 5;
195                         var->green.offset = 5;
196                         var->green.length = 5;
197                         var->blue.offset = 10;
198                         var->blue.length = 5;
199                         var->transp.offset = 15;
200                         var->transp.length = 1;
201                 } else {        /* RGB 565 */
202                         var->red.offset = 0;
203                         var->red.length = 5;
204                         var->green.offset = 5;
205                         var->green.length = 6;
206                         var->blue.offset = 11;
207                         var->blue.length = 5;
208                         var->transp.offset = 0;
209                         var->transp.length = 0;
210                 }
211                 break;
212         case 24:                /* RGB 888 */
213                 var->red.offset = 0;
214                 var->red.length = 8;
215                 var->green.offset = 8;
216                 var->green.length = 8;
217                 var->blue.offset = 16;
218                 var->blue.length = 8;
219                 var->transp.offset = 0;
220                 var->transp.length = 0;
221                 break;
222         case 32:                /* RGBA 8888 */
223                 var->red.offset = 0;
224                 var->red.length = 8;
225                 var->green.offset = 8;
226                 var->green.length = 8;
227                 var->blue.offset = 16;
228                 var->blue.length = 8;
229                 var->transp.offset = 24;
230                 var->transp.length = 8;
231                 break;
232         }
233         var->red.msb_right = 0;
234         var->green.msb_right = 0;
235         var->blue.msb_right = 0;
236         var->transp.msb_right = 0;
237
238         return 0;
239 }
240
241 /* This routine actually sets the video mode. It's in here where we
242  * the hardware state info->par and fix which can be affected by the 
243  * change in par. For this driver it doesn't do much. 
244  */
245 static int vfb_set_par(struct fb_info *info)
246 {
247         info->fix.line_length = get_line_length(info->var.xres_virtual,
248                                                 info->var.bits_per_pixel);
249         return 0;
250 }
251
252     /*
253      *  Set a single color register. The values supplied are already
254      *  rounded down to the hardware's capabilities (according to the
255      *  entries in the var structure). Return != 0 for invalid regno.
256      */
257
258 static int vfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
259                          u_int transp, struct fb_info *info)
260 {
261         if (regno >= 256)       /* no. of hw registers */
262                 return 1;
263         /*
264          * Program hardware... do anything you want with transp
265          */
266
267         /* grayscale works only partially under directcolor */
268         if (info->var.grayscale) {
269                 /* grayscale = 0.30*R + 0.59*G + 0.11*B */
270                 red = green = blue =
271                     (red * 77 + green * 151 + blue * 28) >> 8;
272         }
273
274         /* Directcolor:
275          *   var->{color}.offset contains start of bitfield
276          *   var->{color}.length contains length of bitfield
277          *   {hardwarespecific} contains width of RAMDAC
278          *   cmap[X] is programmed to (X << red.offset) | (X << green.offset) | (X << blue.offset)
279          *   RAMDAC[X] is programmed to (red, green, blue)
280          * 
281          * Pseudocolor:
282          *    uses offset = 0 && length = RAMDAC register width.
283          *    var->{color}.offset is 0
284          *    var->{color}.length contains widht of DAC
285          *    cmap is not used
286          *    RAMDAC[X] is programmed to (red, green, blue)
287          * Truecolor:
288          *    does not use DAC. Usually 3 are present.
289          *    var->{color}.offset contains start of bitfield
290          *    var->{color}.length contains length of bitfield
291          *    cmap is programmed to (red << red.offset) | (green << green.offset) |
292          *                      (blue << blue.offset) | (transp << transp.offset)
293          *    RAMDAC does not exist
294          */
295 #define CNVT_TOHW(val,width) ((((val)<<(width))+0x7FFF-(val))>>16)
296         switch (info->fix.visual) {
297         case FB_VISUAL_TRUECOLOR:
298         case FB_VISUAL_PSEUDOCOLOR:
299                 red = CNVT_TOHW(red, info->var.red.length);
300                 green = CNVT_TOHW(green, info->var.green.length);
301                 blue = CNVT_TOHW(blue, info->var.blue.length);
302                 transp = CNVT_TOHW(transp, info->var.transp.length);
303                 break;
304         case FB_VISUAL_DIRECTCOLOR:
305                 red = CNVT_TOHW(red, 8);        /* expect 8 bit DAC */
306                 green = CNVT_TOHW(green, 8);
307                 blue = CNVT_TOHW(blue, 8);
308                 /* hey, there is bug in transp handling... */
309                 transp = CNVT_TOHW(transp, 8);
310                 break;
311         }
312 #undef CNVT_TOHW
313         /* Truecolor has hardware independent palette */
314         if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
315                 u32 v;
316
317                 if (regno >= 16)
318                         return 1;
319
320                 v = (red << info->var.red.offset) |
321                     (green << info->var.green.offset) |
322                     (blue << info->var.blue.offset) |
323                     (transp << info->var.transp.offset);
324                 switch (info->var.bits_per_pixel) {
325                 case 8:
326                         break;
327                 case 16:
328                         ((u32 *) (info->pseudo_palette))[regno] = v;
329                         break;
330                 case 24:
331                 case 32:
332                         ((u32 *) (info->pseudo_palette))[regno] = v;
333                         break;
334                 }
335                 return 0;
336         }
337         return 0;
338 }
339
340     /*
341      *  Pan or Wrap the Display
342      *
343      *  This call looks only at xoffset, yoffset and the FB_VMODE_YWRAP flag
344      */
345
346 static int vfb_pan_display(struct fb_var_screeninfo *var,
347                            struct fb_info *info)
348 {
349         if (var->vmode & FB_VMODE_YWRAP) {
350                 if (var->yoffset < 0
351                     || var->yoffset >= info->var.yres_virtual
352                     || var->xoffset)
353                         return -EINVAL;
354         } else {
355                 if (var->xoffset + var->xres > info->var.xres_virtual ||
356                     var->yoffset + var->yres > info->var.yres_virtual)
357                         return -EINVAL;
358         }
359         info->var.xoffset = var->xoffset;
360         info->var.yoffset = var->yoffset;
361         if (var->vmode & FB_VMODE_YWRAP)
362                 info->var.vmode |= FB_VMODE_YWRAP;
363         else
364                 info->var.vmode &= ~FB_VMODE_YWRAP;
365         return 0;
366 }
367
368     /*
369      *  Most drivers don't need their own mmap function 
370      */
371
372 static int vfb_mmap(struct fb_info *info, struct file *file,
373                     struct vm_area_struct *vma)
374 {
375         return -EINVAL;
376 }
377
378 #ifndef MODULE
379 static int __init vfb_setup(char *options)
380 {
381         char *this_opt;
382
383         vfb_enable = 1;
384
385         if (!options || !*options)
386                 return 1;
387
388         while ((this_opt = strsep(&options, ",")) != NULL) {
389                 if (!*this_opt)
390                         continue;
391                 if (!strncmp(this_opt, "disable", 7))
392                         vfb_enable = 0;
393         }
394         return 1;
395 }
396 #endif  /*  MODULE  */
397
398     /*
399      *  Initialisation
400      */
401
402 static void vfb_platform_release(struct device *device)
403 {
404         // This is called when the reference count goes to zero.
405 }
406
407 static int __init vfb_probe(struct device *device)
408 {
409         struct platform_device *dev = to_platform_device(device);
410         struct fb_info *info;
411         int retval = -ENOMEM;
412
413         /*
414          * For real video cards we use ioremap.
415          */
416         if (!(videomemory = vmalloc(videomemorysize)))
417                 return retval;
418
419         /*
420          * VFB must clear memory to prevent kernel info
421          * leakage into userspace
422          * VGA-based drivers MUST NOT clear memory if
423          * they want to be able to take over vgacon
424          */
425         memset(videomemory, 0, videomemorysize);
426
427         info = framebuffer_alloc(sizeof(u32) * 256, &dev->dev);
428         if (!info)
429                 goto err;
430
431         info->screen_base = (char __iomem *)videomemory;
432         info->fbops = &vfb_ops;
433
434         retval = fb_find_mode(&info->var, info, NULL,
435                               NULL, 0, NULL, 8);
436
437         if (!retval || (retval == 4))
438                 info->var = vfb_default;
439         info->fix = vfb_fix;
440         info->pseudo_palette = info->par;
441         info->par = NULL;
442         info->flags = FBINFO_FLAG_DEFAULT;
443
444         retval = fb_alloc_cmap(&info->cmap, 256, 0);
445         if (retval < 0)
446                 goto err1;
447
448         retval = register_framebuffer(info);
449         if (retval < 0)
450                 goto err2;
451         dev_set_drvdata(&dev->dev, info);
452
453         printk(KERN_INFO
454                "fb%d: Virtual frame buffer device, using %ldK of video memory\n",
455                info->node, videomemorysize >> 10);
456         return 0;
457 err2:
458         fb_dealloc_cmap(&info->cmap);
459 err1:
460         framebuffer_release(info);
461 err:
462         vfree(videomemory);
463         return retval;
464 }
465
466 static int vfb_remove(struct device *device)
467 {
468         struct fb_info *info = dev_get_drvdata(device);
469
470         if (info) {
471                 unregister_framebuffer(info);
472                 vfree(videomemory);
473                 framebuffer_release(info);
474         }
475         return 0;
476 }
477
478 static struct device_driver vfb_driver = {
479         .name   = "vfb",
480         .bus    = &platform_bus_type,
481         .probe  = vfb_probe,
482         .remove = vfb_remove,
483 };
484
485 static struct platform_device vfb_device = {
486         .name   = "vfb",
487         .id     = 0,
488         .dev    = {
489                 .release = vfb_platform_release,
490         }
491 };
492
493 static int __init vfb_init(void)
494 {
495         int ret = 0;
496
497 #ifndef MODULE
498         char *option = NULL;
499
500         if (fb_get_options("vfb", &option))
501                 return -ENODEV;
502         vfb_setup(option);
503 #endif
504
505         if (!vfb_enable)
506                 return -ENXIO;
507
508         ret = driver_register(&vfb_driver);
509
510         if (!ret) {
511                 ret = platform_device_register(&vfb_device);
512                 if (ret)
513                         driver_unregister(&vfb_driver);
514         }
515         return ret;
516 }
517
518 module_init(vfb_init);
519
520 #ifdef MODULE
521 static void __exit vfb_exit(void)
522 {
523         platform_device_unregister(&vfb_device);
524         driver_unregister(&vfb_driver);
525 }
526
527 module_exit(vfb_exit);
528
529 MODULE_LICENSE("GPL");
530 #endif                          /* MODULE */