]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/video/vt8623fb.c
fbdev: add removed option "mode" to keep compatibility
[linux-2.6-omap-h63xx.git] / drivers / video / vt8623fb.c
1 /*
2  * linux/drivers/video/vt8623fb.c - fbdev driver for
3  * integrated graphic core in VIA VT8623 [CLE266] chipset
4  *
5  * Copyright (c) 2006-2007 Ondrej Zajicek <santiago@crfreenet.org>
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  * Code is based on s3fb, some parts are from David Boucher's viafb
12  * (http://davesdomain.org.uk/viafb/)
13  */
14
15 #include <linux/version.h>
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
19 #include <linux/string.h>
20 #include <linux/mm.h>
21 #include <linux/tty.h>
22 #include <linux/slab.h>
23 #include <linux/delay.h>
24 #include <linux/fb.h>
25 #include <linux/svga.h>
26 #include <linux/init.h>
27 #include <linux/pci.h>
28 #include <linux/console.h> /* Why should fb driver call console functions? because acquire_console_sem() */
29 #include <video/vga.h>
30
31 #ifdef CONFIG_MTRR
32 #include <asm/mtrr.h>
33 #endif
34
35 struct vt8623fb_info {
36         char __iomem *mmio_base;
37         int mtrr_reg;
38         struct vgastate state;
39         struct mutex open_lock;
40         unsigned int ref_count;
41         u32 pseudo_palette[16];
42 };
43
44
45
46 /* ------------------------------------------------------------------------- */
47
48 static const struct svga_fb_format vt8623fb_formats[] = {
49         { 0,  {0, 6, 0},  {0, 6, 0},  {0, 6, 0}, {0, 0, 0}, 0,
50                 FB_TYPE_TEXT, FB_AUX_TEXT_SVGA_STEP8,   FB_VISUAL_PSEUDOCOLOR, 16, 16},
51         { 4,  {0, 6, 0},  {0, 6, 0},  {0, 6, 0}, {0, 0, 0}, 0,
52                 FB_TYPE_PACKED_PIXELS, 0,               FB_VISUAL_PSEUDOCOLOR, 16, 16},
53         { 4,  {0, 6, 0},  {0, 6, 0},  {0, 6, 0}, {0, 0, 0}, 1,
54                 FB_TYPE_INTERLEAVED_PLANES, 1,          FB_VISUAL_PSEUDOCOLOR, 16, 16},
55         { 8,  {0, 6, 0},  {0, 6, 0},  {0, 6, 0}, {0, 0, 0}, 0,
56                 FB_TYPE_PACKED_PIXELS, 0,               FB_VISUAL_PSEUDOCOLOR, 8, 8},
57 /*      {16,  {10, 5, 0}, {5, 5, 0},  {0, 5, 0}, {0, 0, 0}, 0,
58                 FB_TYPE_PACKED_PIXELS, 0,               FB_VISUAL_TRUECOLOR, 4, 4},     */
59         {16,  {11, 5, 0}, {5, 6, 0},  {0, 5, 0}, {0, 0, 0}, 0,
60                 FB_TYPE_PACKED_PIXELS, 0,               FB_VISUAL_TRUECOLOR, 4, 4},
61         {32,  {16, 8, 0}, {8, 8, 0},  {0, 8, 0}, {0, 0, 0}, 0,
62                 FB_TYPE_PACKED_PIXELS, 0,               FB_VISUAL_TRUECOLOR, 2, 2},
63         SVGA_FORMAT_END
64 };
65
66 static const struct svga_pll vt8623_pll = {2, 127, 2, 7, 0, 3,
67         60000, 300000, 14318};
68
69 /* CRT timing register sets */
70
71 static struct vga_regset vt8623_h_total_regs[]       = {{0x00, 0, 7}, {0x36, 3, 3}, VGA_REGSET_END};
72 static struct vga_regset vt8623_h_display_regs[]     = {{0x01, 0, 7}, VGA_REGSET_END};
73 static struct vga_regset vt8623_h_blank_start_regs[] = {{0x02, 0, 7}, VGA_REGSET_END};
74 static struct vga_regset vt8623_h_blank_end_regs[]   = {{0x03, 0, 4}, {0x05, 7, 7}, {0x33, 5, 5}, VGA_REGSET_END};
75 static struct vga_regset vt8623_h_sync_start_regs[]  = {{0x04, 0, 7}, {0x33, 4, 4}, VGA_REGSET_END};
76 static struct vga_regset vt8623_h_sync_end_regs[]    = {{0x05, 0, 4}, VGA_REGSET_END};
77
78 static struct vga_regset vt8623_v_total_regs[]       = {{0x06, 0, 7}, {0x07, 0, 0}, {0x07, 5, 5}, {0x35, 0, 0}, VGA_REGSET_END};
79 static struct vga_regset vt8623_v_display_regs[]     = {{0x12, 0, 7}, {0x07, 1, 1}, {0x07, 6, 6}, {0x35, 2, 2}, VGA_REGSET_END};
80 static struct vga_regset vt8623_v_blank_start_regs[] = {{0x15, 0, 7}, {0x07, 3, 3}, {0x09, 5, 5}, {0x35, 3, 3}, VGA_REGSET_END};
81 static struct vga_regset vt8623_v_blank_end_regs[]   = {{0x16, 0, 7}, VGA_REGSET_END};
82 static struct vga_regset vt8623_v_sync_start_regs[]  = {{0x10, 0, 7}, {0x07, 2, 2}, {0x07, 7, 7}, {0x35, 1, 1}, VGA_REGSET_END};
83 static struct vga_regset vt8623_v_sync_end_regs[]    = {{0x11, 0, 3}, VGA_REGSET_END};
84
85 static struct vga_regset vt8623_offset_regs[]        = {{0x13, 0, 7}, {0x35, 5, 7}, VGA_REGSET_END};
86 static struct vga_regset vt8623_line_compare_regs[]  = {{0x18, 0, 7}, {0x07, 4, 4}, {0x09, 6, 6}, {0x33, 0, 2}, {0x35, 4, 4}, VGA_REGSET_END};
87 static struct vga_regset vt8623_fetch_count_regs[]   = {{0x1C, 0, 7}, {0x1D, 0, 1}, VGA_REGSET_END};
88 static struct vga_regset vt8623_start_address_regs[] = {{0x0d, 0, 7}, {0x0c, 0, 7}, {0x34, 0, 7}, {0x48, 0, 1}, VGA_REGSET_END};
89
90 static struct svga_timing_regs vt8623_timing_regs     = {
91         vt8623_h_total_regs, vt8623_h_display_regs, vt8623_h_blank_start_regs,
92         vt8623_h_blank_end_regs, vt8623_h_sync_start_regs, vt8623_h_sync_end_regs,
93         vt8623_v_total_regs, vt8623_v_display_regs, vt8623_v_blank_start_regs,
94         vt8623_v_blank_end_regs, vt8623_v_sync_start_regs, vt8623_v_sync_end_regs,
95 };
96
97
98 /* ------------------------------------------------------------------------- */
99
100
101 /* Module parameters */
102
103 static char *mode_option = "640x480-8@60";
104
105 #ifdef CONFIG_MTRR
106 static int mtrr = 1;
107 #endif
108
109 MODULE_AUTHOR("(c) 2006 Ondrej Zajicek <santiago@crfreenet.org>");
110 MODULE_LICENSE("GPL");
111 MODULE_DESCRIPTION("fbdev driver for integrated graphics core in VIA VT8623 [CLE266]");
112
113 module_param(mode_option, charp, 0644);
114 MODULE_PARM_DESC(mode_option, "Default video mode ('640x480-8@60', etc)");
115 module_param_named(mode, mode_option, charp, 0);
116 MODULE_PARM_DESC(mode, "Default video mode e.g. '648x480-8@60' (deprecated)");
117
118 #ifdef CONFIG_MTRR
119 module_param(mtrr, int, 0444);
120 MODULE_PARM_DESC(mtrr, "Enable write-combining with MTRR (1=enable, 0=disable, default=1)");
121 #endif
122
123
124 /* ------------------------------------------------------------------------- */
125
126
127 static struct fb_tile_ops vt8623fb_tile_ops = {
128         .fb_settile     = svga_settile,
129         .fb_tilecopy    = svga_tilecopy,
130         .fb_tilefill    = svga_tilefill,
131         .fb_tileblit    = svga_tileblit,
132         .fb_tilecursor  = svga_tilecursor,
133         .fb_get_tilemax = svga_get_tilemax,
134 };
135
136
137 /* ------------------------------------------------------------------------- */
138
139
140 /* image data is MSB-first, fb structure is MSB-first too */
141 static inline u32 expand_color(u32 c)
142 {
143         return ((c & 1) | ((c & 2) << 7) | ((c & 4) << 14) | ((c & 8) << 21)) * 0xFF;
144 }
145
146 /* vt8623fb_iplan_imageblit silently assumes that almost everything is 8-pixel aligned */
147 static void vt8623fb_iplan_imageblit(struct fb_info *info, const struct fb_image *image)
148 {
149         u32 fg = expand_color(image->fg_color);
150         u32 bg = expand_color(image->bg_color);
151         const u8 *src1, *src;
152         u8 __iomem *dst1;
153         u32 __iomem *dst;
154         u32 val;
155         int x, y;
156
157         src1 = image->data;
158         dst1 = info->screen_base + (image->dy * info->fix.line_length)
159                  + ((image->dx / 8) * 4);
160
161         for (y = 0; y < image->height; y++) {
162                 src = src1;
163                 dst = (u32 __iomem *) dst1;
164                 for (x = 0; x < image->width; x += 8) {
165                         val = *(src++) * 0x01010101;
166                         val = (val & fg) | (~val & bg);
167                         fb_writel(val, dst++);
168                 }
169                 src1 += image->width / 8;
170                 dst1 += info->fix.line_length;
171         }
172 }
173
174 /* vt8623fb_iplan_fillrect silently assumes that almost everything is 8-pixel aligned */
175 static void vt8623fb_iplan_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
176 {
177         u32 fg = expand_color(rect->color);
178         u8 __iomem *dst1;
179         u32 __iomem *dst;
180         int x, y;
181
182         dst1 = info->screen_base + (rect->dy * info->fix.line_length)
183                  + ((rect->dx / 8) * 4);
184
185         for (y = 0; y < rect->height; y++) {
186                 dst = (u32 __iomem *) dst1;
187                 for (x = 0; x < rect->width; x += 8) {
188                         fb_writel(fg, dst++);
189                 }
190                 dst1 += info->fix.line_length;
191         }
192 }
193
194
195 /* image data is MSB-first, fb structure is high-nibble-in-low-byte-first */
196 static inline u32 expand_pixel(u32 c)
197 {
198         return (((c &  1) << 24) | ((c &  2) << 27) | ((c &  4) << 14) | ((c &   8) << 17) |
199                 ((c & 16) <<  4) | ((c & 32) <<  7) | ((c & 64) >>  6) | ((c & 128) >>  3)) * 0xF;
200 }
201
202 /* vt8623fb_cfb4_imageblit silently assumes that almost everything is 8-pixel aligned */
203 static void vt8623fb_cfb4_imageblit(struct fb_info *info, const struct fb_image *image)
204 {
205         u32 fg = image->fg_color * 0x11111111;
206         u32 bg = image->bg_color * 0x11111111;
207         const u8 *src1, *src;
208         u8 __iomem *dst1;
209         u32 __iomem *dst;
210         u32 val;
211         int x, y;
212
213         src1 = image->data;
214         dst1 = info->screen_base + (image->dy * info->fix.line_length)
215                  + ((image->dx / 8) * 4);
216
217         for (y = 0; y < image->height; y++) {
218                 src = src1;
219                 dst = (u32 __iomem *) dst1;
220                 for (x = 0; x < image->width; x += 8) {
221                         val = expand_pixel(*(src++));
222                         val = (val & fg) | (~val & bg);
223                         fb_writel(val, dst++);
224                 }
225                 src1 += image->width / 8;
226                 dst1 += info->fix.line_length;
227         }
228 }
229
230 static void vt8623fb_imageblit(struct fb_info *info, const struct fb_image *image)
231 {
232         if ((info->var.bits_per_pixel == 4) && (image->depth == 1)
233             && ((image->width % 8) == 0) && ((image->dx % 8) == 0)) {
234                 if (info->fix.type == FB_TYPE_INTERLEAVED_PLANES)
235                         vt8623fb_iplan_imageblit(info, image);
236                 else
237                         vt8623fb_cfb4_imageblit(info, image);
238         } else
239                 cfb_imageblit(info, image);
240 }
241
242 static void vt8623fb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
243 {
244         if ((info->var.bits_per_pixel == 4)
245             && ((rect->width % 8) == 0) && ((rect->dx % 8) == 0)
246             && (info->fix.type == FB_TYPE_INTERLEAVED_PLANES))
247                 vt8623fb_iplan_fillrect(info, rect);
248          else
249                 cfb_fillrect(info, rect);
250 }
251
252
253 /* ------------------------------------------------------------------------- */
254
255
256 static void vt8623_set_pixclock(struct fb_info *info, u32 pixclock)
257 {
258         u16 m, n, r;
259         u8 regval;
260         int rv;
261
262         rv = svga_compute_pll(&vt8623_pll, 1000000000 / pixclock, &m, &n, &r, info->node);
263         if (rv < 0) {
264                 printk(KERN_ERR "fb%d: cannot set requested pixclock, keeping old value\n", info->node);
265                 return;
266         }
267
268         /* Set VGA misc register  */
269         regval = vga_r(NULL, VGA_MIS_R);
270         vga_w(NULL, VGA_MIS_W, regval | VGA_MIS_ENB_PLL_LOAD);
271
272         /* Set clock registers */
273         vga_wseq(NULL, 0x46, (n  | (r << 6)));
274         vga_wseq(NULL, 0x47, m);
275
276         udelay(1000);
277
278         /* PLL reset */
279         svga_wseq_mask(0x40, 0x02, 0x02);
280         svga_wseq_mask(0x40, 0x00, 0x02);
281 }
282
283
284 static int vt8623fb_open(struct fb_info *info, int user)
285 {
286         struct vt8623fb_info *par = info->par;
287
288         mutex_lock(&(par->open_lock));
289         if (par->ref_count == 0) {
290                 memset(&(par->state), 0, sizeof(struct vgastate));
291                 par->state.flags = VGA_SAVE_MODE | VGA_SAVE_FONTS | VGA_SAVE_CMAP;
292                 par->state.num_crtc = 0xA2;
293                 par->state.num_seq = 0x50;
294                 save_vga(&(par->state));
295         }
296
297         par->ref_count++;
298         mutex_unlock(&(par->open_lock));
299
300         return 0;
301 }
302
303 static int vt8623fb_release(struct fb_info *info, int user)
304 {
305         struct vt8623fb_info *par = info->par;
306
307         mutex_lock(&(par->open_lock));
308         if (par->ref_count == 0) {
309                 mutex_unlock(&(par->open_lock));
310                 return -EINVAL;
311         }
312
313         if (par->ref_count == 1)
314                 restore_vga(&(par->state));
315
316         par->ref_count--;
317         mutex_unlock(&(par->open_lock));
318
319         return 0;
320 }
321
322 static int vt8623fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
323 {
324         int rv, mem, step;
325
326         /* Find appropriate format */
327         rv = svga_match_format (vt8623fb_formats, var, NULL);
328         if (rv < 0)
329         {
330                 printk(KERN_ERR "fb%d: unsupported mode requested\n", info->node);
331                 return rv;
332         }
333
334         /* Do not allow to have real resoulution larger than virtual */
335         if (var->xres > var->xres_virtual)
336                 var->xres_virtual = var->xres;
337
338         if (var->yres > var->yres_virtual)
339                 var->yres_virtual = var->yres;
340
341         /* Round up xres_virtual to have proper alignment of lines */
342         step = vt8623fb_formats[rv].xresstep - 1;
343         var->xres_virtual = (var->xres_virtual+step) & ~step;
344
345         /* Check whether have enough memory */
346         mem = ((var->bits_per_pixel * var->xres_virtual) >> 3) * var->yres_virtual;
347         if (mem > info->screen_size)
348         {
349                 printk(KERN_ERR "fb%d: not enough framebuffer memory (%d kB requested , %d kB available)\n", info->node, mem >> 10, (unsigned int) (info->screen_size >> 10));
350                 return -EINVAL;
351         }
352
353         /* Text mode is limited to 256 kB of memory */
354         if ((var->bits_per_pixel == 0) && (mem > (256*1024)))
355         {
356                 printk(KERN_ERR "fb%d: text framebuffer size too large (%d kB requested, 256 kB possible)\n", info->node, mem >> 10);
357                 return -EINVAL;
358         }
359
360         rv = svga_check_timings (&vt8623_timing_regs, var, info->node);
361         if (rv < 0)
362         {
363                 printk(KERN_ERR "fb%d: invalid timings requested\n", info->node);
364                 return rv;
365         }
366
367         /* Interlaced mode not supported */
368         if (var->vmode & FB_VMODE_INTERLACED)
369                 return -EINVAL;
370
371         return 0;
372 }
373
374
375 static int vt8623fb_set_par(struct fb_info *info)
376 {
377         u32 mode, offset_value, fetch_value, screen_size;
378         u32 bpp = info->var.bits_per_pixel;
379
380         if (bpp != 0) {
381                 info->fix.ypanstep = 1;
382                 info->fix.line_length = (info->var.xres_virtual * bpp) / 8;
383
384                 info->flags &= ~FBINFO_MISC_TILEBLITTING;
385                 info->tileops = NULL;
386
387                 /* in 4bpp supports 8p wide tiles only, any tiles otherwise */
388                 info->pixmap.blit_x = (bpp == 4) ? (1 << (8 - 1)) : (~(u32)0);
389                 info->pixmap.blit_y = ~(u32)0;
390
391                 offset_value = (info->var.xres_virtual * bpp) / 64;
392                 fetch_value  = ((info->var.xres * bpp) / 128) + 4;
393
394                 if (bpp == 4)
395                         fetch_value  = (info->var.xres / 8) + 8; /* + 0 is OK */
396
397                 screen_size  = info->var.yres_virtual * info->fix.line_length;
398         } else {
399                 info->fix.ypanstep = 16;
400                 info->fix.line_length = 0;
401
402                 info->flags |= FBINFO_MISC_TILEBLITTING;
403                 info->tileops = &vt8623fb_tile_ops;
404
405                 /* supports 8x16 tiles only */
406                 info->pixmap.blit_x = 1 << (8 - 1);
407                 info->pixmap.blit_y = 1 << (16 - 1);
408
409                 offset_value = info->var.xres_virtual / 16;
410                 fetch_value  = (info->var.xres / 8) + 8;
411                 screen_size  = (info->var.xres_virtual * info->var.yres_virtual) / 64;
412         }
413
414         info->var.xoffset = 0;
415         info->var.yoffset = 0;
416         info->var.activate = FB_ACTIVATE_NOW;
417
418         /* Unlock registers */
419         svga_wseq_mask(0x10, 0x01, 0x01);
420         svga_wcrt_mask(0x11, 0x00, 0x80);
421         svga_wcrt_mask(0x47, 0x00, 0x01);
422
423         /* Device, screen and sync off */
424         svga_wseq_mask(0x01, 0x20, 0x20);
425         svga_wcrt_mask(0x36, 0x30, 0x30);
426         svga_wcrt_mask(0x17, 0x00, 0x80);
427
428         /* Set default values */
429         svga_set_default_gfx_regs();
430         svga_set_default_atc_regs();
431         svga_set_default_seq_regs();
432         svga_set_default_crt_regs();
433         svga_wcrt_multi(vt8623_line_compare_regs, 0xFFFFFFFF);
434         svga_wcrt_multi(vt8623_start_address_regs, 0);
435
436         svga_wcrt_multi(vt8623_offset_regs, offset_value);
437         svga_wseq_multi(vt8623_fetch_count_regs, fetch_value);
438
439         if (info->var.vmode & FB_VMODE_DOUBLE)
440                 svga_wcrt_mask(0x09, 0x80, 0x80);
441         else
442                 svga_wcrt_mask(0x09, 0x00, 0x80);
443
444         svga_wseq_mask(0x1E, 0xF0, 0xF0); // DI/DVP bus
445         svga_wseq_mask(0x2A, 0x0F, 0x0F); // DI/DVP bus
446         svga_wseq_mask(0x16, 0x08, 0xBF); // FIFO read treshold
447         vga_wseq(NULL, 0x17, 0x1F);       // FIFO depth
448         vga_wseq(NULL, 0x18, 0x4E);
449         svga_wseq_mask(0x1A, 0x08, 0x08); // enable MMIO ?
450
451         vga_wcrt(NULL, 0x32, 0x00);
452         vga_wcrt(NULL, 0x34, 0x00);
453         vga_wcrt(NULL, 0x6A, 0x80);
454         vga_wcrt(NULL, 0x6A, 0xC0);
455
456         vga_wgfx(NULL, 0x20, 0x00);
457         vga_wgfx(NULL, 0x21, 0x00);
458         vga_wgfx(NULL, 0x22, 0x00);
459
460         /* Set SR15 according to number of bits per pixel */
461         mode = svga_match_format(vt8623fb_formats, &(info->var), &(info->fix));
462         switch (mode) {
463         case 0:
464                 pr_debug("fb%d: text mode\n", info->node);
465                 svga_set_textmode_vga_regs();
466                 svga_wseq_mask(0x15, 0x00, 0xFE);
467                 svga_wcrt_mask(0x11, 0x60, 0x70);
468                 break;
469         case 1:
470                 pr_debug("fb%d: 4 bit pseudocolor\n", info->node);
471                 vga_wgfx(NULL, VGA_GFX_MODE, 0x40);
472                 svga_wseq_mask(0x15, 0x20, 0xFE);
473                 svga_wcrt_mask(0x11, 0x00, 0x70);
474                 break;
475         case 2:
476                 pr_debug("fb%d: 4 bit pseudocolor, planar\n", info->node);
477                 svga_wseq_mask(0x15, 0x00, 0xFE);
478                 svga_wcrt_mask(0x11, 0x00, 0x70);
479                 break;
480         case 3:
481                 pr_debug("fb%d: 8 bit pseudocolor\n", info->node);
482                 svga_wseq_mask(0x15, 0x22, 0xFE);
483                 break;
484         case 4:
485                 pr_debug("fb%d: 5/6/5 truecolor\n", info->node);
486                 svga_wseq_mask(0x15, 0xB6, 0xFE);
487                 break;
488         case 5:
489                 pr_debug("fb%d: 8/8/8 truecolor\n", info->node);
490                 svga_wseq_mask(0x15, 0xAE, 0xFE);
491                 break;
492         default:
493                 printk(KERN_ERR "vt8623fb: unsupported mode - bug\n");
494                 return (-EINVAL);
495         }
496
497         vt8623_set_pixclock(info, info->var.pixclock);
498         svga_set_timings(&vt8623_timing_regs, &(info->var), 1, 1,
499                          (info->var.vmode & FB_VMODE_DOUBLE) ? 2 : 1, 1,
500                          1, info->node);
501
502         memset_io(info->screen_base, 0x00, screen_size);
503
504         /* Device and screen back on */
505         svga_wcrt_mask(0x17, 0x80, 0x80);
506         svga_wcrt_mask(0x36, 0x00, 0x30);
507         svga_wseq_mask(0x01, 0x00, 0x20);
508
509         return 0;
510 }
511
512
513 static int vt8623fb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
514                                 u_int transp, struct fb_info *fb)
515 {
516         switch (fb->var.bits_per_pixel) {
517         case 0:
518         case 4:
519                 if (regno >= 16)
520                         return -EINVAL;
521
522                 outb(0x0F, VGA_PEL_MSK);
523                 outb(regno, VGA_PEL_IW);
524                 outb(red >> 10, VGA_PEL_D);
525                 outb(green >> 10, VGA_PEL_D);
526                 outb(blue >> 10, VGA_PEL_D);
527                 break;
528         case 8:
529                 if (regno >= 256)
530                         return -EINVAL;
531
532                 outb(0xFF, VGA_PEL_MSK);
533                 outb(regno, VGA_PEL_IW);
534                 outb(red >> 10, VGA_PEL_D);
535                 outb(green >> 10, VGA_PEL_D);
536                 outb(blue >> 10, VGA_PEL_D);
537                 break;
538         case 16:
539                 if (regno >= 16)
540                         return 0;
541
542                 if (fb->var.green.length == 5)
543                         ((u32*)fb->pseudo_palette)[regno] = ((red & 0xF800) >> 1) |
544                                 ((green & 0xF800) >> 6) | ((blue & 0xF800) >> 11);
545                 else if (fb->var.green.length == 6)
546                         ((u32*)fb->pseudo_palette)[regno] = (red & 0xF800) |
547                                 ((green & 0xFC00) >> 5) | ((blue & 0xF800) >> 11);
548                 else
549                         return -EINVAL;
550                 break;
551         case 24:
552         case 32:
553                 if (regno >= 16)
554                         return 0;
555
556                 /* ((transp & 0xFF00) << 16) */
557                 ((u32*)fb->pseudo_palette)[regno] = ((red & 0xFF00) << 8) |
558                         (green & 0xFF00) | ((blue & 0xFF00) >> 8);
559                 break;
560         default:
561                 return -EINVAL;
562         }
563
564         return 0;
565 }
566
567
568 static int vt8623fb_blank(int blank_mode, struct fb_info *info)
569 {
570         switch (blank_mode) {
571         case FB_BLANK_UNBLANK:
572                 pr_debug("fb%d: unblank\n", info->node);
573                 svga_wcrt_mask(0x36, 0x00, 0x30);
574                 svga_wseq_mask(0x01, 0x00, 0x20);
575                 break;
576         case FB_BLANK_NORMAL:
577                 pr_debug("fb%d: blank\n", info->node);
578                 svga_wcrt_mask(0x36, 0x00, 0x30);
579                 svga_wseq_mask(0x01, 0x20, 0x20);
580                 break;
581         case FB_BLANK_HSYNC_SUSPEND:
582                 pr_debug("fb%d: DPMS standby (hsync off)\n", info->node);
583                 svga_wcrt_mask(0x36, 0x10, 0x30);
584                 svga_wseq_mask(0x01, 0x20, 0x20);
585                 break;
586         case FB_BLANK_VSYNC_SUSPEND:
587                 pr_debug("fb%d: DPMS suspend (vsync off)\n", info->node);
588                 svga_wcrt_mask(0x36, 0x20, 0x30);
589                 svga_wseq_mask(0x01, 0x20, 0x20);
590                 break;
591         case FB_BLANK_POWERDOWN:
592                 pr_debug("fb%d: DPMS off (no sync)\n", info->node);
593                 svga_wcrt_mask(0x36, 0x30, 0x30);
594                 svga_wseq_mask(0x01, 0x20, 0x20);
595                 break;
596         }
597
598         return 0;
599 }
600
601
602 static int vt8623fb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
603 {
604         unsigned int offset;
605
606         /* Calculate the offset */
607         if (var->bits_per_pixel == 0) {
608                 offset = (var->yoffset / 16) * var->xres_virtual + var->xoffset;
609                 offset = offset >> 3;
610         } else {
611                 offset = (var->yoffset * info->fix.line_length) +
612                          (var->xoffset * var->bits_per_pixel / 8);
613                 offset = offset >> ((var->bits_per_pixel == 4) ? 2 : 1);
614         }
615
616         /* Set the offset */
617         svga_wcrt_multi(vt8623_start_address_regs, offset);
618
619         return 0;
620 }
621
622
623 /* ------------------------------------------------------------------------- */
624
625
626 /* Frame buffer operations */
627
628 static struct fb_ops vt8623fb_ops = {
629         .owner          = THIS_MODULE,
630         .fb_open        = vt8623fb_open,
631         .fb_release     = vt8623fb_release,
632         .fb_check_var   = vt8623fb_check_var,
633         .fb_set_par     = vt8623fb_set_par,
634         .fb_setcolreg   = vt8623fb_setcolreg,
635         .fb_blank       = vt8623fb_blank,
636         .fb_pan_display = vt8623fb_pan_display,
637         .fb_fillrect    = vt8623fb_fillrect,
638         .fb_copyarea    = cfb_copyarea,
639         .fb_imageblit   = vt8623fb_imageblit,
640         .fb_get_caps    = svga_get_caps,
641 };
642
643
644 /* PCI probe */
645
646 static int __devinit vt8623_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
647 {
648         struct fb_info *info;
649         struct vt8623fb_info *par;
650         unsigned int memsize1, memsize2;
651         int rc;
652
653         /* Ignore secondary VGA device because there is no VGA arbitration */
654         if (! svga_primary_device(dev)) {
655                 dev_info(&(dev->dev), "ignoring secondary device\n");
656                 return -ENODEV;
657         }
658
659         /* Allocate and fill driver data structure */
660         info = framebuffer_alloc(sizeof(struct vt8623fb_info), NULL);
661         if (! info) {
662                 dev_err(&(dev->dev), "cannot allocate memory\n");
663                 return -ENOMEM;
664         }
665
666         par = info->par;
667         mutex_init(&par->open_lock);
668
669         info->flags = FBINFO_PARTIAL_PAN_OK | FBINFO_HWACCEL_YPAN;
670         info->fbops = &vt8623fb_ops;
671
672         /* Prepare PCI device */
673
674         rc = pci_enable_device(dev);
675         if (rc < 0) {
676                 dev_err(&(dev->dev), "cannot enable PCI device\n");
677                 goto err_enable_device;
678         }
679
680         rc = pci_request_regions(dev, "vt8623fb");
681         if (rc < 0) {
682                 dev_err(&(dev->dev), "cannot reserve framebuffer region\n");
683                 goto err_request_regions;
684         }
685
686         info->fix.smem_start = pci_resource_start(dev, 0);
687         info->fix.smem_len = pci_resource_len(dev, 0);
688         info->fix.mmio_start = pci_resource_start(dev, 1);
689         info->fix.mmio_len = pci_resource_len(dev, 1);
690
691         /* Map physical IO memory address into kernel space */
692         info->screen_base = pci_iomap(dev, 0, 0);
693         if (! info->screen_base) {
694                 rc = -ENOMEM;
695                 dev_err(&(dev->dev), "iomap for framebuffer failed\n");
696                 goto err_iomap_1;
697         }
698
699         par->mmio_base = pci_iomap(dev, 1, 0);
700         if (! par->mmio_base) {
701                 rc = -ENOMEM;
702                 dev_err(&(dev->dev), "iomap for MMIO failed\n");
703                 goto err_iomap_2;
704         }
705
706         /* Find how many physical memory there is on card */
707         memsize1 = (vga_rseq(NULL, 0x34) + 1) >> 1;
708         memsize2 = vga_rseq(NULL, 0x39) << 2;
709
710         if ((16 <= memsize1) && (memsize1 <= 64) && (memsize1 == memsize2))
711                 info->screen_size = memsize1 << 20;
712         else {
713                 dev_err(&(dev->dev), "memory size detection failed (%x %x), suppose 16 MB\n", memsize1, memsize2);
714                 info->screen_size = 16 << 20;
715         }
716
717         info->fix.smem_len = info->screen_size;
718         strcpy(info->fix.id, "VIA VT8623");
719         info->fix.type = FB_TYPE_PACKED_PIXELS;
720         info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
721         info->fix.ypanstep = 0;
722         info->fix.accel = FB_ACCEL_NONE;
723         info->pseudo_palette = (void*)par->pseudo_palette;
724
725         /* Prepare startup mode */
726
727         rc = fb_find_mode(&(info->var), info, mode_option, NULL, 0, NULL, 8);
728         if (! ((rc == 1) || (rc == 2))) {
729                 rc = -EINVAL;
730                 dev_err(&(dev->dev), "mode %s not found\n", mode_option);
731                 goto err_find_mode;
732         }
733
734         rc = fb_alloc_cmap(&info->cmap, 256, 0);
735         if (rc < 0) {
736                 dev_err(&(dev->dev), "cannot allocate colormap\n");
737                 goto err_alloc_cmap;
738         }
739
740         rc = register_framebuffer(info);
741         if (rc < 0) {
742                 dev_err(&(dev->dev), "cannot register framebugger\n");
743                 goto err_reg_fb;
744         }
745
746         printk(KERN_INFO "fb%d: %s on %s, %d MB RAM\n", info->node, info->fix.id,
747                  pci_name(dev), info->fix.smem_len >> 20);
748
749         /* Record a reference to the driver data */
750         pci_set_drvdata(dev, info);
751
752 #ifdef CONFIG_MTRR
753         if (mtrr) {
754                 par->mtrr_reg = -1;
755                 par->mtrr_reg = mtrr_add(info->fix.smem_start, info->fix.smem_len, MTRR_TYPE_WRCOMB, 1);
756         }
757 #endif
758
759         return 0;
760
761         /* Error handling */
762 err_reg_fb:
763         fb_dealloc_cmap(&info->cmap);
764 err_alloc_cmap:
765 err_find_mode:
766         pci_iounmap(dev, par->mmio_base);
767 err_iomap_2:
768         pci_iounmap(dev, info->screen_base);
769 err_iomap_1:
770         pci_release_regions(dev);
771 err_request_regions:
772 /*      pci_disable_device(dev); */
773 err_enable_device:
774         framebuffer_release(info);
775         return rc;
776 }
777
778 /* PCI remove */
779
780 static void __devexit vt8623_pci_remove(struct pci_dev *dev)
781 {
782         struct fb_info *info = pci_get_drvdata(dev);
783
784         if (info) {
785                 struct vt8623fb_info *par = info->par;
786
787 #ifdef CONFIG_MTRR
788                 if (par->mtrr_reg >= 0) {
789                         mtrr_del(par->mtrr_reg, 0, 0);
790                         par->mtrr_reg = -1;
791                 }
792 #endif
793
794                 unregister_framebuffer(info);
795                 fb_dealloc_cmap(&info->cmap);
796
797                 pci_iounmap(dev, info->screen_base);
798                 pci_iounmap(dev, par->mmio_base);
799                 pci_release_regions(dev);
800 /*              pci_disable_device(dev); */
801
802                 pci_set_drvdata(dev, NULL);
803                 framebuffer_release(info);
804         }
805 }
806
807
808 #ifdef CONFIG_PM
809 /* PCI suspend */
810
811 static int vt8623_pci_suspend(struct pci_dev* dev, pm_message_t state)
812 {
813         struct fb_info *info = pci_get_drvdata(dev);
814         struct vt8623fb_info *par = info->par;
815
816         dev_info(&(dev->dev), "suspend\n");
817
818         acquire_console_sem();
819         mutex_lock(&(par->open_lock));
820
821         if ((state.event == PM_EVENT_FREEZE) || (par->ref_count == 0)) {
822                 mutex_unlock(&(par->open_lock));
823                 release_console_sem();
824                 return 0;
825         }
826
827         fb_set_suspend(info, 1);
828
829         pci_save_state(dev);
830         pci_disable_device(dev);
831         pci_set_power_state(dev, pci_choose_state(dev, state));
832
833         mutex_unlock(&(par->open_lock));
834         release_console_sem();
835
836         return 0;
837 }
838
839
840 /* PCI resume */
841
842 static int vt8623_pci_resume(struct pci_dev* dev)
843 {
844         struct fb_info *info = pci_get_drvdata(dev);
845         struct vt8623fb_info *par = info->par;
846
847         dev_info(&(dev->dev), "resume\n");
848
849         acquire_console_sem();
850         mutex_lock(&(par->open_lock));
851
852         if (par->ref_count == 0) {
853                 mutex_unlock(&(par->open_lock));
854                 release_console_sem();
855                 return 0;
856         }
857
858         pci_set_power_state(dev, PCI_D0);
859         pci_restore_state(dev);
860
861         if (pci_enable_device(dev))
862                 goto fail;
863
864         pci_set_master(dev);
865
866         vt8623fb_set_par(info);
867         fb_set_suspend(info, 0);
868
869         mutex_unlock(&(par->open_lock));
870 fail:
871         release_console_sem();
872
873         return 0;
874 }
875 #else
876 #define vt8623_pci_suspend NULL
877 #define vt8623_pci_resume NULL
878 #endif /* CONFIG_PM */
879
880 /* List of boards that we are trying to support */
881
882 static struct pci_device_id vt8623_devices[] __devinitdata = {
883         {PCI_DEVICE(PCI_VENDOR_ID_VIA, 0x3122)},
884         {0, 0, 0, 0, 0, 0, 0}
885 };
886
887 MODULE_DEVICE_TABLE(pci, vt8623_devices);
888
889 static struct pci_driver vt8623fb_pci_driver = {
890         .name           = "vt8623fb",
891         .id_table       = vt8623_devices,
892         .probe          = vt8623_pci_probe,
893         .remove         = __devexit_p(vt8623_pci_remove),
894         .suspend        = vt8623_pci_suspend,
895         .resume         = vt8623_pci_resume,
896 };
897
898 /* Cleanup */
899
900 static void __exit vt8623fb_cleanup(void)
901 {
902         pr_debug("vt8623fb: cleaning up\n");
903         pci_unregister_driver(&vt8623fb_pci_driver);
904 }
905
906 /* Driver Initialisation */
907
908 static int __init vt8623fb_init(void)
909 {
910
911 #ifndef MODULE
912         char *option = NULL;
913
914         if (fb_get_options("vt8623fb", &option))
915                 return -ENODEV;
916
917         if (option && *option)
918                 mode_option = option;
919 #endif
920
921         pr_debug("vt8623fb: initializing\n");
922         return pci_register_driver(&vt8623fb_pci_driver);
923 }
924
925 /* ------------------------------------------------------------------------- */
926
927 /* Modularization */
928
929 module_init(vt8623fb_init);
930 module_exit(vt8623fb_cleanup);