Merge branch 'drm-intel-next' of git://git.kernel.org/pub/scm/linux/kernel/git/anholt...
[linux-2.6] / drivers / video / via / viafbdev.c
1 /*
2  * Copyright 1998-2008 VIA Technologies, Inc. All Rights Reserved.
3  * Copyright 2001-2008 S3 Graphics, Inc. All Rights Reserved.
4
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public
7  * License as published by the Free Software Foundation;
8  * either version 2, or (at your option) any later version.
9
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTIES OR REPRESENTATIONS; without even
12  * the implied warranty of MERCHANTABILITY or FITNESS FOR
13  * A PARTICULAR PURPOSE.See the GNU General Public License
14  * for more details.
15
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc.,
19  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21
22 #include <linux/module.h>
23 #define _MASTER_FILE
24
25 #include "global.h"
26
27 static int MAX_CURS = 32;
28 static struct fb_var_screeninfo default_var;
29 static char *viafb_name = "Via";
30 static u32 pseudo_pal[17];
31
32 /* video mode */
33 static char *viafb_mode = "640x480";
34 static char *viafb_mode1 = "640x480";
35 static int viafb_resMode = VIA_RES_640X480;
36
37 /* Added for specifying active devices.*/
38 char *viafb_active_dev = "";
39
40 /* Added for specifying video on devices.*/
41 char *viafb_video_dev = "";
42
43 /*Added for specify lcd output port*/
44 char *viafb_lcd_port = "";
45 char *viafb_dvi_port = "";
46
47 static void viafb_set_device(struct device_t active_dev);
48 static int apply_device_setting(struct viafb_ioctl_setting setting_info,
49                          struct fb_info *info);
50 static void apply_second_mode_setting(struct fb_var_screeninfo
51         *sec_var);
52 static void retrieve_device_setting(struct viafb_ioctl_setting
53         *setting_info);
54 static void viafb_set_video_device(u32 video_dev_info);
55 static void viafb_get_video_device(u32 *video_dev_info);
56
57 /* Mode information */
58 static const struct viafb_modeinfo viafb_modentry[] = {
59         {480, 640, VIA_RES_480X640, "480x640"},
60         {640, 480, VIA_RES_640X480, "640x480"},
61         {800, 480, VIA_RES_800X480, "800x480"},
62         {800, 600, VIA_RES_800X600, "800x600"},
63         {1024, 768, VIA_RES_1024X768, "1024x768"},
64         {1152, 864, VIA_RES_1152X864, "1152x864"},
65         {1280, 1024, VIA_RES_1280X1024, "1280x1024"},
66         {1600, 1200, VIA_RES_1600X1200, "1600x1200"},
67         {1440, 1050, VIA_RES_1440X1050, "1440x1050"},
68         {1280, 768, VIA_RES_1280X768, "1280x768"},
69         {1280, 800, VIA_RES_1280X800, "1280x800"},
70         {1280, 960, VIA_RES_1280X960, "1280x960"},
71         {1920, 1440, VIA_RES_1920X1440, "1920x1440"},
72         {848, 480, VIA_RES_848X480, "848x480"},
73         {1400, 1050, VIA_RES_1400X1050, "1400x1050"},
74         {720, 480, VIA_RES_720X480, "720x480"},
75         {720, 576, VIA_RES_720X576, "720x576"},
76         {1024, 512, VIA_RES_1024X512, "1024x512"},
77         {1024, 576, VIA_RES_1024X576, "1024x576"},
78         {1024, 600, VIA_RES_1024X600, "1024x600"},
79         {1280, 720, VIA_RES_1280X720, "1280x720"},
80         {1920, 1080, VIA_RES_1920X1080, "1920x1080"},
81         {1366, 768, VIA_RES_1368X768, "1368x768"},
82         {1680, 1050, VIA_RES_1680X1050, "1680x1050"},
83         {960, 600, VIA_RES_960X600, "960x600"},
84         {1000, 600, VIA_RES_1000X600, "1000x600"},
85         {1024, 576, VIA_RES_1024X576, "1024x576"},
86         {1024, 600, VIA_RES_1024X600, "1024x600"},
87         {1088, 612, VIA_RES_1088X612, "1088x612"},
88         {1152, 720, VIA_RES_1152X720, "1152x720"},
89         {1200, 720, VIA_RES_1200X720, "1200x720"},
90         {1280, 600, VIA_RES_1280X600, "1280x600"},
91         {1360, 768, VIA_RES_1360X768, "1360x768"},
92         {1440, 900, VIA_RES_1440X900, "1440x900"},
93         {1600, 900, VIA_RES_1600X900, "1600x900"},
94         {1600, 1024, VIA_RES_1600X1024, "1600x1024"},
95         {1792, 1344, VIA_RES_1792X1344, "1792x1344"},
96         {1856, 1392, VIA_RES_1856X1392, "1856x1392"},
97         {1920, 1200, VIA_RES_1920X1200, "1920x1200"},
98         {2048, 1536, VIA_RES_2048X1536, "2048x1536"},
99         {0, 0, VIA_RES_INVALID, "640x480"}
100 };
101
102 static struct fb_ops viafb_ops;
103
104 static int viafb_update_fix(struct fb_fix_screeninfo *fix, struct fb_info *info)
105 {
106         struct viafb_par *ppar;
107         ppar = info->par;
108
109         DEBUG_MSG(KERN_INFO "viafb_update_fix!\n");
110
111         fix->visual =
112             ppar->bpp == 8 ? FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
113         fix->line_length = ppar->linelength;
114
115         return 0;
116 }
117
118
119 static void viafb_setup_fixinfo(struct fb_fix_screeninfo *fix,
120         struct viafb_par *viaparinfo)
121 {
122         memset(fix, 0, sizeof(struct fb_fix_screeninfo));
123         strcpy(fix->id, viafb_name);
124
125         fix->smem_start = viaparinfo->fbmem;
126         fix->smem_len = viaparinfo->fbmem_free;
127         fix->mmio_start = viaparinfo->mmio_base;
128         fix->mmio_len = viaparinfo->mmio_len;
129
130         fix->type = FB_TYPE_PACKED_PIXELS;
131         fix->type_aux = 0;
132
133         fix->xpanstep = fix->ywrapstep = 0;
134         fix->ypanstep = 1;
135
136         /* Just tell the accel name */
137         viafbinfo->fix.accel = FB_ACCEL_VIA_UNICHROME;
138 }
139 static int viafb_open(struct fb_info *info, int user)
140 {
141         DEBUG_MSG(KERN_INFO "viafb_open!\n");
142         return 0;
143 }
144
145 static int viafb_release(struct fb_info *info, int user)
146 {
147         DEBUG_MSG(KERN_INFO "viafb_release!\n");
148         return 0;
149 }
150
151 static void viafb_update_viafb_par(struct fb_info *info)
152 {
153         struct viafb_par *ppar;
154
155         ppar = info->par;
156         ppar->bpp = info->var.bits_per_pixel;
157         ppar->linelength = ((info->var.xres_virtual + 7) & ~7) * ppar->bpp / 8;
158         ppar->hres = info->var.xres;
159         ppar->vres = info->var.yres;
160         ppar->xoffset = info->var.xoffset;
161         ppar->yoffset = info->var.yoffset;
162 }
163
164 static int viafb_check_var(struct fb_var_screeninfo *var,
165         struct fb_info *info)
166 {
167         int vmode_index, htotal, vtotal;
168         struct viafb_par *ppar;
169         u32 long_refresh;
170         struct viafb_par *p_viafb_par;
171         ppar = info->par;
172
173
174         DEBUG_MSG(KERN_INFO "viafb_check_var!\n");
175         /* Sanity check */
176         /* HW neither support interlacte nor double-scaned mode */
177         if (var->vmode & FB_VMODE_INTERLACED || var->vmode & FB_VMODE_DOUBLE)
178                 return -EINVAL;
179
180         vmode_index = viafb_get_mode_index(var->xres, var->yres, 0);
181         if (vmode_index == VIA_RES_INVALID) {
182                 DEBUG_MSG(KERN_INFO
183                           "viafb: Mode %dx%dx%d not supported!!\n",
184                           var->xres, var->yres, var->bits_per_pixel);
185                 return -EINVAL;
186         }
187
188         if (24 == var->bits_per_pixel)
189                 var->bits_per_pixel = 32;
190
191         if (var->bits_per_pixel != 8 && var->bits_per_pixel != 16 &&
192                 var->bits_per_pixel != 32)
193                 return -EINVAL;
194
195         if ((var->xres_virtual * (var->bits_per_pixel >> 3)) & 0x1F)
196                 /*32 pixel alignment */
197                 var->xres_virtual = (var->xres_virtual + 31) & ~31;
198         if (var->xres_virtual * var->yres_virtual * var->bits_per_pixel / 8 >
199                 ppar->memsize)
200                 return -EINVAL;
201
202         /* Based on var passed in to calculate the refresh,
203          * because our driver use some modes special.
204          */
205         htotal = var->xres + var->left_margin +
206         var->right_margin + var->hsync_len;
207         vtotal = var->yres + var->upper_margin +
208                 var->lower_margin + var->vsync_len;
209         long_refresh = 1000000000UL / var->pixclock * 1000;
210         long_refresh /= (htotal * vtotal);
211
212         viafb_refresh = viafb_get_refresh(var->xres, var->yres, long_refresh);
213
214         /* Adjust var according to our driver's own table */
215         viafb_fill_var_timing_info(var, viafb_refresh, vmode_index);
216
217         /* This is indeed a patch for VT3353 */
218         if (!info->par)
219                 return -1;
220         p_viafb_par = (struct viafb_par *)info->par;
221         if (p_viafb_par->chip_info->gfx_chip_name == UNICHROME_VX800)
222                 var->accel_flags = 0;
223
224         return 0;
225 }
226
227 static int viafb_set_par(struct fb_info *info)
228 {
229         int vmode_index;
230         int vmode_index1 = 0;
231         DEBUG_MSG(KERN_INFO "viafb_set_par!\n");
232
233         viafb_update_device_setting(info->var.xres, info->var.yres,
234                               info->var.bits_per_pixel, viafb_refresh, 0);
235
236         vmode_index = viafb_get_mode_index(info->var.xres, info->var.yres, 0);
237
238         if (viafb_SAMM_ON == 1) {
239                 DEBUG_MSG(KERN_INFO
240                 "viafb_second_xres = %d, viafb_second_yres = %d, bpp = %d\n",
241                           viafb_second_xres, viafb_second_yres, viafb_bpp1);
242                 vmode_index1 = viafb_get_mode_index(viafb_second_xres,
243                         viafb_second_yres, 1);
244                 DEBUG_MSG(KERN_INFO "->viafb_SAMM_ON: index=%d\n",
245                         vmode_index1);
246
247                 viafb_update_device_setting(viafb_second_xres,
248                         viafb_second_yres, viafb_bpp1, viafb_refresh1, 1);
249         }
250
251         if (vmode_index != VIA_RES_INVALID) {
252                 viafb_setmode(vmode_index, info->var.xres, info->var.yres,
253                         info->var.bits_per_pixel, vmode_index1,
254                         viafb_second_xres, viafb_second_yres, viafb_bpp1);
255
256                 /*We should set memory offset according virtual_x */
257                 /*Fix me:put this function into viafb_setmode */
258                 viafb_memory_pitch_patch(info);
259
260                 /* Update ***fb_par information */
261                 viafb_update_viafb_par(info);
262
263                 /* Update other fixed information */
264                 viafb_update_fix(&info->fix, info);
265                 viafb_bpp = info->var.bits_per_pixel;
266                 /* Update viafb_accel, it is necessary to our 2D accelerate */
267                 viafb_accel = info->var.accel_flags;
268
269                 if (viafb_accel)
270                         viafb_set_2d_color_depth(info->var.bits_per_pixel);
271         }
272
273         return 0;
274 }
275
276 /* Set one color register */
277 static int viafb_setcolreg(unsigned regno, unsigned red, unsigned green,
278 unsigned blue, unsigned transp, struct fb_info *info)
279 {
280         u8 sr1a, sr1b, cr67, cr6a, rev = 0, shift = 10;
281         unsigned cmap_entries = (info->var.bits_per_pixel == 8) ? 256 : 16;
282         DEBUG_MSG(KERN_INFO "viafb_setcolreg!\n");
283         if (regno >= cmap_entries)
284                 return 1;
285         if (UNICHROME_CLE266 == viaparinfo->chip_info->gfx_chip_name) {
286                 /*
287                  * Read PCI bus 0,dev 0,function 0,index 0xF6 to get chip rev.
288                  */
289                 outl(0x80000000 | (0xf6 & ~3), (unsigned long)0xCF8);
290                 rev = (inl((unsigned long)0xCFC) >> ((0xf6 & 3) * 8)) & 0xff;
291         }
292         switch (info->var.bits_per_pixel) {
293         case 8:
294                 outb(0x1A, 0x3C4);
295                 sr1a = inb(0x3C5);
296                 outb(0x1B, 0x3C4);
297                 sr1b = inb(0x3C5);
298                 outb(0x67, 0x3D4);
299                 cr67 = inb(0x3D5);
300                 outb(0x6A, 0x3D4);
301                 cr6a = inb(0x3D5);
302
303                 /* Map the 3C6/7/8/9 to the IGA2 */
304                 outb(0x1A, 0x3C4);
305                 outb(sr1a | 0x01, 0x3C5);
306                 /* Second Display Engine colck always on */
307                 outb(0x1B, 0x3C4);
308                 outb(sr1b | 0x80, 0x3C5);
309                 /* Second Display Color Depth 8 */
310                 outb(0x67, 0x3D4);
311                 outb(cr67 & 0x3F, 0x3D5);
312                 outb(0x6A, 0x3D4);
313                 /* Second Display Channel Reset CR6A[6]) */
314                 outb(cr6a & 0xBF, 0x3D5);
315                 /* Second Display Channel Enable CR6A[7] */
316                 outb(cr6a | 0x80, 0x3D5);
317                 /* Second Display Channel stop reset) */
318                 outb(cr6a | 0x40, 0x3D5);
319
320                 /* Bit mask of palette */
321                 outb(0xFF, 0x3c6);
322                 /* Write one register of IGA2 */
323                 outb(regno, 0x3C8);
324                 if (UNICHROME_CLE266 == viaparinfo->chip_info->gfx_chip_name &&
325                         rev >= 15) {
326                         shift = 8;
327                         viafb_write_reg_mask(CR6A, VIACR, BIT5, BIT5);
328                         viafb_write_reg_mask(SR15, VIASR, BIT7, BIT7);
329                 } else {
330                         shift = 10;
331                         viafb_write_reg_mask(CR6A, VIACR, 0, BIT5);
332                         viafb_write_reg_mask(SR15, VIASR, 0, BIT7);
333                 }
334                 outb(red >> shift, 0x3C9);
335                 outb(green >> shift, 0x3C9);
336                 outb(blue >> shift, 0x3C9);
337
338                 /* Map the 3C6/7/8/9 to the IGA1 */
339                 outb(0x1A, 0x3C4);
340                 outb(sr1a & 0xFE, 0x3C5);
341                 /* Bit mask of palette */
342                 outb(0xFF, 0x3c6);
343                 /* Write one register of IGA1 */
344                 outb(regno, 0x3C8);
345                 outb(red >> shift, 0x3C9);
346                 outb(green >> shift, 0x3C9);
347                 outb(blue >> shift, 0x3C9);
348
349                 outb(0x1A, 0x3C4);
350                 outb(sr1a, 0x3C5);
351                 outb(0x1B, 0x3C4);
352                 outb(sr1b, 0x3C5);
353                 outb(0x67, 0x3D4);
354                 outb(cr67, 0x3D5);
355                 outb(0x6A, 0x3D4);
356                 outb(cr6a, 0x3D5);
357                 break;
358         case 16:
359                 ((u32 *) info->pseudo_palette)[regno] = (red & 0xF800) |
360                     ((green & 0xFC00) >> 5) | ((blue & 0xF800) >> 11);
361                 break;
362         case 32:
363                 ((u32 *) info->pseudo_palette)[regno] =
364                     ((transp & 0xFF00) << 16) |
365                     ((red & 0xFF00) << 8) |
366                     ((green & 0xFF00)) | ((blue & 0xFF00) >> 8);
367                 break;
368         }
369
370         return 0;
371
372 }
373
374 /*CALLED BY: fb_set_cmap */
375 /*           fb_set_var, pass 256 colors */
376 /*CALLED BY: fb_set_cmap */
377 /*           fbcon_set_palette, pass 16 colors */
378 static int viafb_setcmap(struct fb_cmap *cmap, struct fb_info *info)
379 {
380         u32 len = cmap->len;
381         u32 i;
382         u16 *pred = cmap->red;
383         u16 *pgreen = cmap->green;
384         u16 *pblue = cmap->blue;
385         u16 *ptransp = cmap->transp;
386         u8 sr1a, sr1b, cr67, cr6a, rev = 0, shift = 10;
387         if (len > 256)
388                 return 1;
389         if (UNICHROME_CLE266 == viaparinfo->chip_info->gfx_chip_name) {
390                 /*
391                  * Read PCI bus 0, dev 0, function 0, index 0xF6 to get chip
392                  * rev.
393                  */
394                 outl(0x80000000 | (0xf6 & ~3), (unsigned long)0xCF8);
395                 rev = (inl((unsigned long)0xCFC) >> ((0xf6 & 3) * 8)) & 0xff;
396         }
397         switch (info->var.bits_per_pixel) {
398         case 8:
399                 outb(0x1A, 0x3C4);
400                 sr1a = inb(0x3C5);
401                 outb(0x1B, 0x3C4);
402                 sr1b = inb(0x3C5);
403                 outb(0x67, 0x3D4);
404                 cr67 = inb(0x3D5);
405                 outb(0x6A, 0x3D4);
406                 cr6a = inb(0x3D5);
407                 /* Map the 3C6/7/8/9 to the IGA2 */
408                 outb(0x1A, 0x3C4);
409                 outb(sr1a | 0x01, 0x3C5);
410                 outb(0x1B, 0x3C4);
411                 /* Second Display Engine colck always on */
412                 outb(sr1b | 0x80, 0x3C5);
413                 outb(0x67, 0x3D4);
414                 /* Second Display Color Depth 8 */
415                 outb(cr67 & 0x3F, 0x3D5);
416                 outb(0x6A, 0x3D4);
417                 /* Second Display Channel Reset CR6A[6]) */
418                 outb(cr6a & 0xBF, 0x3D5);
419                 /* Second Display Channel Enable CR6A[7] */
420                 outb(cr6a | 0x80, 0x3D5);
421                 /* Second Display Channel stop reset) */
422                 outb(cr6a | 0xC0, 0x3D5);
423
424                 /* Bit mask of palette */
425                 outb(0xFF, 0x3c6);
426                 outb(0x00, 0x3C8);
427                 if (UNICHROME_CLE266 == viaparinfo->chip_info->gfx_chip_name &&
428                         rev >= 15) {
429                         shift = 8;
430                         viafb_write_reg_mask(CR6A, VIACR, BIT5, BIT5);
431                         viafb_write_reg_mask(SR15, VIASR, BIT7, BIT7);
432                 } else {
433                         shift = 10;
434                         viafb_write_reg_mask(CR6A, VIACR, 0, BIT5);
435                         viafb_write_reg_mask(SR15, VIASR, 0, BIT7);
436                 }
437                 for (i = 0; i < len; i++) {
438                         outb((*(pred + i)) >> shift, 0x3C9);
439                         outb((*(pgreen + i)) >> shift, 0x3C9);
440                         outb((*(pblue + i)) >> shift, 0x3C9);
441                 }
442
443                 outb(0x1A, 0x3C4);
444                 /* Map the 3C6/7/8/9 to the IGA1 */
445                 outb(sr1a & 0xFE, 0x3C5);
446                 /* Bit mask of palette */
447                 outb(0xFF, 0x3c6);
448                 outb(0x00, 0x3C8);
449                 for (i = 0; i < len; i++) {
450                         outb((*(pred + i)) >> shift, 0x3C9);
451                         outb((*(pgreen + i)) >> shift, 0x3C9);
452                         outb((*(pblue + i)) >> shift, 0x3C9);
453                 }
454
455                 outb(0x1A, 0x3C4);
456                 outb(sr1a, 0x3C5);
457                 outb(0x1B, 0x3C4);
458                 outb(sr1b, 0x3C5);
459                 outb(0x67, 0x3D4);
460                 outb(cr67, 0x3D5);
461                 outb(0x6A, 0x3D4);
462                 outb(cr6a, 0x3D5);
463                 break;
464         case 16:
465                 if (len > 17)
466                         return 0;       /* Because static u32 pseudo_pal[17]; */
467                 for (i = 0; i < len; i++)
468                         ((u32 *) info->pseudo_palette)[i] =
469                             (*(pred + i) & 0xF800) |
470                             ((*(pgreen + i) & 0xFC00) >> 5) |
471                             ((*(pblue + i) & 0xF800) >> 11);
472                 break;
473         case 32:
474                 if (len > 17)
475                         return 0;
476                 if (ptransp) {
477                         for (i = 0; i < len; i++)
478                                 ((u32 *) info->pseudo_palette)[i] =
479                                     ((*(ptransp + i) & 0xFF00) << 16) |
480                                     ((*(pred + i) & 0xFF00) << 8) |
481                                     ((*(pgreen + i) & 0xFF00)) |
482                                     ((*(pblue + i) & 0xFF00) >> 8);
483                 } else {
484                         for (i = 0; i < len; i++)
485                                 ((u32 *) info->pseudo_palette)[i] =
486                                     0x00000000 |
487                                     ((*(pred + i) & 0xFF00) << 8) |
488                                     ((*(pgreen + i) & 0xFF00)) |
489                                     ((*(pblue + i) & 0xFF00) >> 8);
490                 }
491                 break;
492         }
493         return 0;
494 }
495
496 static int viafb_pan_display(struct fb_var_screeninfo *var,
497         struct fb_info *info)
498 {
499         unsigned int offset;
500
501         DEBUG_MSG(KERN_INFO "viafb_pan_display!\n");
502
503         offset = (var->xoffset + (var->yoffset * var->xres_virtual)) *
504             var->bits_per_pixel / 16;
505
506         DEBUG_MSG(KERN_INFO "\nviafb_pan_display,offset =%d ", offset);
507
508         viafb_write_reg_mask(0x48, 0x3d4, ((offset >> 24) & 0x3), 0x3);
509         viafb_write_reg_mask(0x34, 0x3d4, ((offset >> 16) & 0xff), 0xff);
510         viafb_write_reg_mask(0x0c, 0x3d4, ((offset >> 8) & 0xff), 0xff);
511         viafb_write_reg_mask(0x0d, 0x3d4, (offset & 0xff), 0xff);
512
513         return 0;
514 }
515
516 static int viafb_blank(int blank_mode, struct fb_info *info)
517 {
518         DEBUG_MSG(KERN_INFO "viafb_blank!\n");
519         /* clear DPMS setting */
520
521         switch (blank_mode) {
522         case FB_BLANK_UNBLANK:
523                 /* Screen: On, HSync: On, VSync: On */
524                 /* control CRT monitor power management */
525                 viafb_write_reg_mask(CR36, VIACR, 0x00, BIT4 + BIT5);
526                 break;
527         case FB_BLANK_HSYNC_SUSPEND:
528                 /* Screen: Off, HSync: Off, VSync: On */
529                 /* control CRT monitor power management */
530                 viafb_write_reg_mask(CR36, VIACR, 0x10, BIT4 + BIT5);
531                 break;
532         case FB_BLANK_VSYNC_SUSPEND:
533                 /* Screen: Off, HSync: On, VSync: Off */
534                 /* control CRT monitor power management */
535                 viafb_write_reg_mask(CR36, VIACR, 0x20, BIT4 + BIT5);
536                 break;
537         case FB_BLANK_POWERDOWN:
538                 /* Screen: Off, HSync: Off, VSync: Off */
539                 /* control CRT monitor power management */
540                 viafb_write_reg_mask(CR36, VIACR, 0x30, BIT4 + BIT5);
541                 break;
542         }
543
544         return 0;
545 }
546
547 static int viafb_ioctl(struct fb_info *info, u_int cmd, u_long arg)
548 {
549         union {
550                 struct viafb_ioctl_mode viamode;
551                 struct viafb_ioctl_samm viasamm;
552                 struct viafb_driver_version driver_version;
553                 struct fb_var_screeninfo sec_var;
554                 struct _panel_size_pos_info panel_pos_size_para;
555                 struct viafb_ioctl_setting viafb_setting;
556                 struct device_t active_dev;
557         } u;
558         u32 state_info = 0;
559         u32 *viafb_gamma_table;
560         char driver_name[] = "viafb";
561
562         u32 __user *argp = (u32 __user *) arg;
563         u32 gpu32;
564         u32 video_dev_info = 0;
565
566         DEBUG_MSG(KERN_INFO "viafb_ioctl: 0x%X !!\n", cmd);
567         memset(&u, 0, sizeof(u));
568
569         switch (cmd) {
570         case VIAFB_GET_CHIP_INFO:
571                 if (copy_to_user(argp, viaparinfo->chip_info,
572                                 sizeof(struct chip_information)))
573                         return -EFAULT;
574                 break;
575         case VIAFB_GET_INFO_SIZE:
576                 return put_user((u32)sizeof(struct viafb_ioctl_info), argp);
577         case VIAFB_GET_INFO:
578                 return viafb_ioctl_get_viafb_info(arg);
579         case VIAFB_HOTPLUG:
580                 return put_user(viafb_ioctl_hotplug(info->var.xres,
581                                               info->var.yres,
582                                               info->var.bits_per_pixel), argp);
583         case VIAFB_SET_HOTPLUG_FLAG:
584                 if (copy_from_user(&gpu32, argp, sizeof(gpu32)))
585                         return -EFAULT;
586                 viafb_hotplug = (gpu32) ? 1 : 0;
587                 break;
588         case VIAFB_GET_RESOLUTION:
589                 u.viamode.xres = (u32) viafb_hotplug_Xres;
590                 u.viamode.yres = (u32) viafb_hotplug_Yres;
591                 u.viamode.refresh = (u32) viafb_hotplug_refresh;
592                 u.viamode.bpp = (u32) viafb_hotplug_bpp;
593                 if (viafb_SAMM_ON == 1) {
594                         u.viamode.xres_sec = viafb_second_xres;
595                         u.viamode.yres_sec = viafb_second_yres;
596                         u.viamode.virtual_xres_sec = viafb_second_virtual_xres;
597                         u.viamode.virtual_yres_sec = viafb_second_virtual_yres;
598                         u.viamode.refresh_sec = viafb_refresh1;
599                         u.viamode.bpp_sec = viafb_bpp1;
600                 } else {
601                         u.viamode.xres_sec = 0;
602                         u.viamode.yres_sec = 0;
603                         u.viamode.virtual_xres_sec = 0;
604                         u.viamode.virtual_yres_sec = 0;
605                         u.viamode.refresh_sec = 0;
606                         u.viamode.bpp_sec = 0;
607                 }
608                 if (copy_to_user(argp, &u.viamode, sizeof(u.viamode)))
609                         return -EFAULT;
610                 break;
611         case VIAFB_GET_SAMM_INFO:
612                 u.viasamm.samm_status = viafb_SAMM_ON;
613
614                 if (viafb_SAMM_ON == 1) {
615                         if (viafb_dual_fb) {
616                                 u.viasamm.size_prim = viaparinfo->fbmem_free;
617                                 u.viasamm.size_sec = viaparinfo1->fbmem_free;
618                         } else {
619                                 if (viafb_second_size) {
620                                         u.viasamm.size_prim =
621                                             viaparinfo->fbmem_free -
622                                             viafb_second_size * 1024 * 1024;
623                                         u.viasamm.size_sec =
624                                             viafb_second_size * 1024 * 1024;
625                                 } else {
626                                         u.viasamm.size_prim =
627                                             viaparinfo->fbmem_free >> 1;
628                                         u.viasamm.size_sec =
629                                             (viaparinfo->fbmem_free >> 1);
630                                 }
631                         }
632                         u.viasamm.mem_base = viaparinfo->fbmem;
633                         u.viasamm.offset_sec = viafb_second_offset;
634                 } else {
635                         u.viasamm.size_prim =
636                             viaparinfo->memsize - viaparinfo->fbmem_used;
637                         u.viasamm.size_sec = 0;
638                         u.viasamm.mem_base = viaparinfo->fbmem;
639                         u.viasamm.offset_sec = 0;
640                 }
641
642                 if (copy_to_user(argp, &u.viasamm, sizeof(u.viasamm)))
643                         return -EFAULT;
644
645                 break;
646         case VIAFB_TURN_ON_OUTPUT_DEVICE:
647                 if (copy_from_user(&gpu32, argp, sizeof(gpu32)))
648                         return -EFAULT;
649                 if (gpu32 & CRT_Device)
650                         viafb_crt_enable();
651                 if (gpu32 & DVI_Device)
652                         viafb_dvi_enable();
653                 if (gpu32 & LCD_Device)
654                         viafb_lcd_enable();
655                 break;
656         case VIAFB_TURN_OFF_OUTPUT_DEVICE:
657                 if (copy_from_user(&gpu32, argp, sizeof(gpu32)))
658                         return -EFAULT;
659                 if (gpu32 & CRT_Device)
660                         viafb_crt_disable();
661                 if (gpu32 & DVI_Device)
662                         viafb_dvi_disable();
663                 if (gpu32 & LCD_Device)
664                         viafb_lcd_disable();
665                 break;
666         case VIAFB_SET_DEVICE:
667                 if (copy_from_user(&u.active_dev, (void *)argp,
668                         sizeof(u.active_dev)))
669                         return -EFAULT;
670                 viafb_set_device(u.active_dev);
671                 viafb_set_par(info);
672                 break;
673         case VIAFB_GET_DEVICE:
674                 u.active_dev.crt = viafb_CRT_ON;
675                 u.active_dev.dvi = viafb_DVI_ON;
676                 u.active_dev.lcd = viafb_LCD_ON;
677                 u.active_dev.samm = viafb_SAMM_ON;
678                 u.active_dev.primary_dev = viafb_primary_dev;
679
680                 u.active_dev.lcd_dsp_cent = viafb_lcd_dsp_method;
681                 u.active_dev.lcd_panel_id = viafb_lcd_panel_id;
682                 u.active_dev.lcd_mode = viafb_lcd_mode;
683
684                 u.active_dev.xres = viafb_hotplug_Xres;
685                 u.active_dev.yres = viafb_hotplug_Yres;
686
687                 u.active_dev.xres1 = viafb_second_xres;
688                 u.active_dev.yres1 = viafb_second_yres;
689
690                 u.active_dev.bpp = viafb_bpp;
691                 u.active_dev.bpp1 = viafb_bpp1;
692                 u.active_dev.refresh = viafb_refresh;
693                 u.active_dev.refresh1 = viafb_refresh1;
694
695                 u.active_dev.epia_dvi = viafb_platform_epia_dvi;
696                 u.active_dev.lcd_dual_edge = viafb_device_lcd_dualedge;
697                 u.active_dev.bus_width = viafb_bus_width;
698
699                 if (copy_to_user(argp, &u.active_dev, sizeof(u.active_dev)))
700                         return -EFAULT;
701                 break;
702
703         case VIAFB_GET_DRIVER_VERSION:
704                 u.driver_version.iMajorNum = VERSION_MAJOR;
705                 u.driver_version.iKernelNum = VERSION_KERNEL;
706                 u.driver_version.iOSNum = VERSION_OS;
707                 u.driver_version.iMinorNum = VERSION_MINOR;
708
709                 if (copy_to_user(argp, &u.driver_version,
710                         sizeof(u.driver_version)))
711                         return -EFAULT;
712
713                 break;
714
715         case VIAFB_SET_DEVICE_INFO:
716                 if (copy_from_user(&u.viafb_setting,
717                         argp, sizeof(u.viafb_setting)))
718                         return -EFAULT;
719                 if (apply_device_setting(u.viafb_setting, info) < 0)
720                         return -EINVAL;
721
722                 break;
723
724         case VIAFB_SET_SECOND_MODE:
725                 if (copy_from_user(&u.sec_var, argp, sizeof(u.sec_var)))
726                         return -EFAULT;
727                 apply_second_mode_setting(&u.sec_var);
728                 break;
729
730         case VIAFB_GET_DEVICE_INFO:
731
732                 retrieve_device_setting(&u.viafb_setting);
733
734                 if (copy_to_user(argp, &u.viafb_setting,
735                                  sizeof(u.viafb_setting)))
736                         return -EFAULT;
737
738                 break;
739
740         case VIAFB_GET_DEVICE_SUPPORT:
741                 viafb_get_device_support_state(&state_info);
742                 if (put_user(state_info, argp))
743                         return -EFAULT;
744                 break;
745
746         case VIAFB_GET_DEVICE_CONNECT:
747                 viafb_get_device_connect_state(&state_info);
748                 if (put_user(state_info, argp))
749                         return -EFAULT;
750                 break;
751
752         case VIAFB_GET_PANEL_SUPPORT_EXPAND:
753                 state_info =
754                     viafb_lcd_get_support_expand_state(info->var.xres,
755                                                  info->var.yres);
756                 if (put_user(state_info, argp))
757                         return -EFAULT;
758                 break;
759
760         case VIAFB_GET_DRIVER_NAME:
761                 if (copy_to_user(argp, driver_name, sizeof(driver_name)))
762                         return -EFAULT;
763                 break;
764
765         case VIAFB_SET_GAMMA_LUT:
766                 viafb_gamma_table = kmalloc(256 * sizeof(u32), GFP_KERNEL);
767                 if (!viafb_gamma_table)
768                         return -ENOMEM;
769                 if (copy_from_user(viafb_gamma_table, argp,
770                                 sizeof(viafb_gamma_table))) {
771                         kfree(viafb_gamma_table);
772                         return -EFAULT;
773                 }
774                 viafb_set_gamma_table(viafb_bpp, viafb_gamma_table);
775                 kfree(viafb_gamma_table);
776                 break;
777
778         case VIAFB_GET_GAMMA_LUT:
779                 viafb_gamma_table = kmalloc(256 * sizeof(u32), GFP_KERNEL);
780                 if (!viafb_gamma_table)
781                         return -ENOMEM;
782                 viafb_get_gamma_table(viafb_gamma_table);
783                 if (copy_to_user(argp, viafb_gamma_table,
784                         sizeof(viafb_gamma_table))) {
785                         kfree(viafb_gamma_table);
786                         return -EFAULT;
787                 }
788                 kfree(viafb_gamma_table);
789                 break;
790
791         case VIAFB_GET_GAMMA_SUPPORT_STATE:
792                 viafb_get_gamma_support_state(viafb_bpp, &state_info);
793                 if (put_user(state_info, argp))
794                         return -EFAULT;
795                 break;
796         case VIAFB_SET_VIDEO_DEVICE:
797                 get_user(video_dev_info, argp);
798                 viafb_set_video_device(video_dev_info);
799                 break;
800         case VIAFB_GET_VIDEO_DEVICE:
801                 viafb_get_video_device(&video_dev_info);
802                 if (put_user(video_dev_info, argp))
803                         return -EFAULT;
804                 break;
805         case VIAFB_SYNC_SURFACE:
806                 DEBUG_MSG(KERN_INFO "lobo VIAFB_SYNC_SURFACE\n");
807                 break;
808         case VIAFB_GET_DRIVER_CAPS:
809                 break;
810
811         case VIAFB_GET_PANEL_MAX_SIZE:
812                 if (copy_from_user(&u.panel_pos_size_para, argp,
813                                    sizeof(u.panel_pos_size_para)))
814                         return -EFAULT;
815                 u.panel_pos_size_para.x = u.panel_pos_size_para.y = 0;
816                 if (copy_to_user(argp, &u.panel_pos_size_para,
817                      sizeof(u.panel_pos_size_para)))
818                         return -EFAULT;
819                 break;
820         case VIAFB_GET_PANEL_MAX_POSITION:
821                 if (copy_from_user(&u.panel_pos_size_para, argp,
822                                    sizeof(u.panel_pos_size_para)))
823                         return -EFAULT;
824                 u.panel_pos_size_para.x = u.panel_pos_size_para.y = 0;
825                 if (copy_to_user(argp, &u.panel_pos_size_para,
826                                  sizeof(u.panel_pos_size_para)))
827                         return -EFAULT;
828                 break;
829
830         case VIAFB_GET_PANEL_POSITION:
831                 if (copy_from_user(&u.panel_pos_size_para, argp,
832                                    sizeof(u.panel_pos_size_para)))
833                         return -EFAULT;
834                 u.panel_pos_size_para.x = u.panel_pos_size_para.y = 0;
835                 if (copy_to_user(argp, &u.panel_pos_size_para,
836                                  sizeof(u.panel_pos_size_para)))
837                         return -EFAULT;
838                 break;
839         case VIAFB_GET_PANEL_SIZE:
840                 if (copy_from_user(&u.panel_pos_size_para, argp,
841                                    sizeof(u.panel_pos_size_para)))
842                         return -EFAULT;
843                 u.panel_pos_size_para.x = u.panel_pos_size_para.y = 0;
844                 if (copy_to_user(argp, &u.panel_pos_size_para,
845                                  sizeof(u.panel_pos_size_para)))
846                         return -EFAULT;
847                 break;
848
849         case VIAFB_SET_PANEL_POSITION:
850                 if (copy_from_user(&u.panel_pos_size_para, argp,
851                                    sizeof(u.panel_pos_size_para)))
852                         return -EFAULT;
853                 break;
854         case VIAFB_SET_PANEL_SIZE:
855                 if (copy_from_user(&u.panel_pos_size_para, argp,
856                                    sizeof(u.panel_pos_size_para)))
857                         return -EFAULT;
858                 break;
859
860         default:
861                 return -EINVAL;
862         }
863
864         return 0;
865 }
866
867 static void viafb_fillrect(struct fb_info *info,
868         const struct fb_fillrect *rect)
869 {
870         u32 col = 0, rop = 0;
871         int pitch;
872
873         if (!viafb_accel) {
874                 cfb_fillrect(info, rect);
875                 return;
876         }
877
878         if (!rect->width || !rect->height)
879                 return;
880
881         switch (rect->rop) {
882         case ROP_XOR:
883                 rop = 0x5A;
884                 break;
885         case ROP_COPY:
886         default:
887                 rop = 0xF0;
888                 break;
889         }
890
891         switch (info->var.bits_per_pixel) {
892         case 8:
893                 col = rect->color;
894                 break;
895         case 16:
896                 col = ((u32 *) (info->pseudo_palette))[rect->color];
897                 break;
898         case 32:
899                 col = ((u32 *) (info->pseudo_palette))[rect->color];
900                 break;
901         }
902
903         /* BitBlt Source Address */
904         writel(0x0, viaparinfo->io_virt + VIA_REG_SRCPOS);
905         /* Source Base Address */
906         writel(0x0, viaparinfo->io_virt + VIA_REG_SRCBASE);
907         /* Destination Base Address */
908         writel(((unsigned long) (info->screen_base) -
909                    (unsigned long) viafb_FB_MM) >> 3,
910                    viaparinfo->io_virt + VIA_REG_DSTBASE);
911         /* Pitch */
912         pitch = (info->var.xres_virtual + 7) & ~7;
913         writel(VIA_PITCH_ENABLE |
914                    (((pitch *
915                       info->var.bits_per_pixel >> 3) >> 3) |
916                       (((pitch * info->
917                       var.bits_per_pixel >> 3) >> 3) << 16)),
918                       viaparinfo->io_virt + VIA_REG_PITCH);
919         /* BitBlt Destination Address */
920         writel(((rect->dy << 16) | rect->dx),
921                 viaparinfo->io_virt + VIA_REG_DSTPOS);
922         /* Dimension: width & height */
923         writel((((rect->height - 1) << 16) | (rect->width - 1)),
924                 viaparinfo->io_virt + VIA_REG_DIMENSION);
925         /* Forground color or Destination color */
926         writel(col, viaparinfo->io_virt + VIA_REG_FGCOLOR);
927         /* GE Command */
928         writel((0x01 | 0x2000 | (rop << 24)),
929                 viaparinfo->io_virt + VIA_REG_GECMD);
930
931 }
932
933 static void viafb_copyarea(struct fb_info *info,
934         const struct fb_copyarea *area)
935 {
936         u32 dy = area->dy, sy = area->sy, direction = 0x0;
937         u32 sx = area->sx, dx = area->dx, width = area->width;
938         int pitch;
939
940         DEBUG_MSG(KERN_INFO "viafb_copyarea!!\n");
941
942         if (!viafb_accel) {
943                 cfb_copyarea(info, area);
944                 return;
945         }
946
947         if (!area->width || !area->height)
948                 return;
949
950         if (sy < dy) {
951                 dy += area->height - 1;
952                 sy += area->height - 1;
953                 direction |= 0x4000;
954         }
955
956         if (sx < dx) {
957                 dx += width - 1;
958                 sx += width - 1;
959                 direction |= 0x8000;
960         }
961
962         /* Source Base Address */
963         writel(((unsigned long) (info->screen_base) -
964                    (unsigned long) viafb_FB_MM) >> 3,
965                    viaparinfo->io_virt + VIA_REG_SRCBASE);
966         /* Destination Base Address */
967         writel(((unsigned long) (info->screen_base) -
968                    (unsigned long) viafb_FB_MM) >> 3,
969                    viaparinfo->io_virt + VIA_REG_DSTBASE);
970         /* Pitch */
971         pitch = (info->var.xres_virtual + 7) & ~7;
972         /* VIA_PITCH_ENABLE can be omitted now. */
973         writel(VIA_PITCH_ENABLE |
974                    (((pitch *
975                       info->var.bits_per_pixel >> 3) >> 3) | (((pitch *
976                                                                 info->var.
977                                                                 bits_per_pixel
978                                                                 >> 3) >> 3)
979                                                               << 16)),
980                                 viaparinfo->io_virt + VIA_REG_PITCH);
981         /* BitBlt Source Address */
982         writel(((sy << 16) | sx), viaparinfo->io_virt + VIA_REG_SRCPOS);
983         /* BitBlt Destination Address */
984         writel(((dy << 16) | dx), viaparinfo->io_virt + VIA_REG_DSTPOS);
985         /* Dimension: width & height */
986         writel((((area->height - 1) << 16) | (area->width - 1)),
987                    viaparinfo->io_virt + VIA_REG_DIMENSION);
988         /* GE Command */
989         writel((0x01 | direction | (0xCC << 24)),
990                 viaparinfo->io_virt + VIA_REG_GECMD);
991
992 }
993
994 static void viafb_imageblit(struct fb_info *info,
995         const struct fb_image *image)
996 {
997         u32 size, bg_col = 0, fg_col = 0, *udata;
998         int i;
999         int pitch;
1000
1001         if (!viafb_accel) {
1002                 cfb_imageblit(info, image);
1003                 return;
1004         }
1005
1006         udata = (u32 *) image->data;
1007
1008         switch (info->var.bits_per_pixel) {
1009         case 8:
1010                 bg_col = image->bg_color;
1011                 fg_col = image->fg_color;
1012                 break;
1013         case 16:
1014                 bg_col = ((u32 *) (info->pseudo_palette))[image->bg_color];
1015                 fg_col = ((u32 *) (info->pseudo_palette))[image->fg_color];
1016                 break;
1017         case 32:
1018                 bg_col = ((u32 *) (info->pseudo_palette))[image->bg_color];
1019                 fg_col = ((u32 *) (info->pseudo_palette))[image->fg_color];
1020                 break;
1021         }
1022         size = image->width * image->height;
1023
1024         /* Source Base Address */
1025         writel(0x0, viaparinfo->io_virt + VIA_REG_SRCBASE);
1026         /* Destination Base Address */
1027         writel(((unsigned long) (info->screen_base) -
1028                    (unsigned long) viafb_FB_MM) >> 3,
1029                    viaparinfo->io_virt + VIA_REG_DSTBASE);
1030         /* Pitch */
1031         pitch = (info->var.xres_virtual + 7) & ~7;
1032         writel(VIA_PITCH_ENABLE |
1033                    (((pitch *
1034                       info->var.bits_per_pixel >> 3) >> 3) | (((pitch *
1035                                                                 info->var.
1036                                                                 bits_per_pixel
1037                                                                 >> 3) >> 3)
1038                                                               << 16)),
1039                                 viaparinfo->io_virt + VIA_REG_PITCH);
1040         /* BitBlt Source Address */
1041         writel(0x0, viaparinfo->io_virt + VIA_REG_SRCPOS);
1042         /* BitBlt Destination Address */
1043         writel(((image->dy << 16) | image->dx),
1044                 viaparinfo->io_virt + VIA_REG_DSTPOS);
1045         /* Dimension: width & height */
1046         writel((((image->height - 1) << 16) | (image->width - 1)),
1047                    viaparinfo->io_virt + VIA_REG_DIMENSION);
1048         /* fb color */
1049         writel(fg_col, viaparinfo->io_virt + VIA_REG_FGCOLOR);
1050         /* bg color */
1051         writel(bg_col, viaparinfo->io_virt + VIA_REG_BGCOLOR);
1052         /* GE Command */
1053         writel(0xCC020142, viaparinfo->io_virt + VIA_REG_GECMD);
1054
1055         for (i = 0; i < size / 4; i++) {
1056                 writel(*udata, viaparinfo->io_virt + VIA_MMIO_BLTBASE);
1057                 udata++;
1058         }
1059
1060 }
1061
1062 static int viafb_cursor(struct fb_info *info, struct fb_cursor *cursor)
1063 {
1064         u32 temp, xx, yy, bg_col = 0, fg_col = 0;
1065         int i, j = 0;
1066         static int hw_cursor;
1067         struct viafb_par *p_viafb_par;
1068
1069         if (viafb_accel)
1070                 hw_cursor = 1;
1071
1072         if (!viafb_accel) {
1073                 if (hw_cursor) {
1074                         viafb_show_hw_cursor(info, HW_Cursor_OFF);
1075                         hw_cursor = 0;
1076                 }
1077                 return -ENODEV;
1078         }
1079
1080         if ((((struct viafb_par *)(info->par))->iga_path == IGA2)
1081             && (viaparinfo->chip_info->gfx_chip_name == UNICHROME_CLE266))
1082                 return -ENODEV;
1083
1084         /* When duoview and using lcd , use soft cursor */
1085         if (viafb_LCD_ON || ((struct viafb_par *)(info->par))->duoview)
1086                 return -ENODEV;
1087
1088         viafb_show_hw_cursor(info, HW_Cursor_OFF);
1089         viacursor = *cursor;
1090
1091         if (cursor->set & FB_CUR_SETHOT) {
1092                 viacursor.hot = cursor->hot;
1093                 temp = ((viacursor.hot.x) << 16) + viacursor.hot.y;
1094                 writel(temp, viaparinfo->io_virt + VIA_REG_CURSOR_ORG);
1095         }
1096
1097         if (cursor->set & FB_CUR_SETPOS) {
1098                 viacursor.image.dx = cursor->image.dx;
1099                 viacursor.image.dy = cursor->image.dy;
1100                 yy = cursor->image.dy - info->var.yoffset;
1101                 xx = cursor->image.dx - info->var.xoffset;
1102                 temp = yy & 0xFFFF;
1103                 temp |= (xx << 16);
1104                 writel(temp, viaparinfo->io_virt + VIA_REG_CURSOR_POS);
1105         }
1106
1107         if (cursor->set & FB_CUR_SETSIZE) {
1108                 temp = readl(viaparinfo->io_virt + VIA_REG_CURSOR_MODE);
1109
1110                 if ((cursor->image.width <= 32)
1111                     && (cursor->image.height <= 32)) {
1112                         MAX_CURS = 32;
1113                         temp |= 0x2;
1114                 } else if ((cursor->image.width <= 64)
1115                            && (cursor->image.height <= 64)) {
1116                         MAX_CURS = 64;
1117                         temp &= 0xFFFFFFFD;
1118                 } else {
1119                         DEBUG_MSG(KERN_INFO
1120                         "The cursor image is biger than 64x64 bits...\n");
1121                         return -ENXIO;
1122                 }
1123                 writel(temp, viaparinfo->io_virt + VIA_REG_CURSOR_MODE);
1124
1125                 viacursor.image.height = cursor->image.height;
1126                 viacursor.image.width = cursor->image.width;
1127         }
1128
1129         if (cursor->set & FB_CUR_SETCMAP) {
1130                 viacursor.image.fg_color = cursor->image.fg_color;
1131                 viacursor.image.bg_color = cursor->image.bg_color;
1132
1133                 switch (info->var.bits_per_pixel) {
1134                 case 8:
1135                 case 16:
1136                 case 32:
1137                         bg_col =
1138                             (0xFF << 24) |
1139                             (((info->cmap.red)[viacursor.image.bg_color] &
1140                             0xFF00) << 8) |
1141                             ((info->cmap.green)[viacursor.image.bg_color] &
1142                             0xFF00) |
1143                             (((info->cmap.blue)[viacursor.image.bg_color] &
1144                             0xFF00) >> 8);
1145                         fg_col =
1146                             (0xFF << 24) |
1147                             (((info->cmap.red)[viacursor.image.fg_color] &
1148                             0xFF00) << 8) |
1149                             ((info->cmap.green)[viacursor.image.fg_color] &
1150                             0xFF00) |
1151                             (((info->cmap.blue)[viacursor.image.fg_color] &
1152                             0xFF00) >> 8);
1153                         break;
1154                 default:
1155                         return 0;
1156                 }
1157
1158                 /* This is indeed a patch for VT3324/VT3353 */
1159                 if (!info->par)
1160                         return 0;
1161                 p_viafb_par = (struct viafb_par *)info->par;
1162
1163                 if ((p_viafb_par->chip_info->gfx_chip_name ==
1164                         UNICHROME_CX700) ||
1165                         ((p_viafb_par->chip_info->gfx_chip_name ==
1166                         UNICHROME_VX800))) {
1167                         bg_col =
1168                             (((info->cmap.red)[viacursor.image.bg_color] &
1169                             0xFFC0) << 14) |
1170                             (((info->cmap.green)[viacursor.image.bg_color] &
1171                             0xFFC0) << 4) |
1172                             (((info->cmap.blue)[viacursor.image.bg_color] &
1173                             0xFFC0) >> 6);
1174                         fg_col =
1175                             (((info->cmap.red)[viacursor.image.fg_color] &
1176                             0xFFC0) << 14) |
1177                             (((info->cmap.green)[viacursor.image.fg_color] &
1178                             0xFFC0) << 4) |
1179                             (((info->cmap.blue)[viacursor.image.fg_color] &
1180                             0xFFC0) >> 6);
1181                 }
1182
1183                 writel(bg_col, viaparinfo->io_virt + VIA_REG_CURSOR_BG);
1184                 writel(fg_col, viaparinfo->io_virt + VIA_REG_CURSOR_FG);
1185         }
1186
1187         if (cursor->set & FB_CUR_SETSHAPE) {
1188                 struct {
1189                         u8 data[CURSOR_SIZE / 8];
1190                         u32 bak[CURSOR_SIZE / 32];
1191                 } *cr_data = kzalloc(sizeof(*cr_data), GFP_ATOMIC);
1192                 int size =
1193                     ((viacursor.image.width + 7) >> 3) *
1194                     viacursor.image.height;
1195
1196                 if (cr_data == NULL)
1197                         goto out;
1198
1199                 if (MAX_CURS == 32) {
1200                         for (i = 0; i < (CURSOR_SIZE / 32); i++) {
1201                                 cr_data->bak[i] = 0x0;
1202                                 cr_data->bak[i + 1] = 0xFFFFFFFF;
1203                                 i += 1;
1204                         }
1205                 } else if (MAX_CURS == 64) {
1206                         for (i = 0; i < (CURSOR_SIZE / 32); i++) {
1207                                 cr_data->bak[i] = 0x0;
1208                                 cr_data->bak[i + 1] = 0x0;
1209                                 cr_data->bak[i + 2] = 0xFFFFFFFF;
1210                                 cr_data->bak[i + 3] = 0xFFFFFFFF;
1211                                 i += 3;
1212                         }
1213                 }
1214
1215                 switch (viacursor.rop) {
1216                 case ROP_XOR:
1217                         for (i = 0; i < size; i++)
1218                                 cr_data->data[i] = viacursor.mask[i];
1219                         break;
1220                 case ROP_COPY:
1221
1222                         for (i = 0; i < size; i++)
1223                                 cr_data->data[i] = viacursor.mask[i];
1224                         break;
1225                 default:
1226                         break;
1227                 }
1228
1229                 if (MAX_CURS == 32) {
1230                         for (i = 0; i < size; i++) {
1231                                 cr_data->bak[j] = (u32) cr_data->data[i];
1232                                 cr_data->bak[j + 1] = ~cr_data->bak[j];
1233                                 j += 2;
1234                         }
1235                 } else if (MAX_CURS == 64) {
1236                         for (i = 0; i < size; i++) {
1237                                 cr_data->bak[j] = (u32) cr_data->data[i];
1238                                 cr_data->bak[j + 1] = 0x0;
1239                                 cr_data->bak[j + 2] = ~cr_data->bak[j];
1240                                 cr_data->bak[j + 3] = ~cr_data->bak[j + 1];
1241                                 j += 4;
1242                         }
1243                 }
1244
1245                 memcpy(((struct viafb_par *)(info->par))->fbmem_virt +
1246                        ((struct viafb_par *)(info->par))->cursor_start,
1247                        cr_data->bak, CURSOR_SIZE);
1248 out:
1249                 kfree(cr_data);
1250         }
1251
1252         if (viacursor.enable)
1253                 viafb_show_hw_cursor(info, HW_Cursor_ON);
1254
1255         return 0;
1256 }
1257
1258 static int viafb_sync(struct fb_info *info)
1259 {
1260         if (viafb_accel)
1261                 viafb_wait_engine_idle();
1262         return 0;
1263 }
1264
1265 int viafb_get_mode_index(int hres, int vres, int flag)
1266 {
1267         u32 i;
1268         DEBUG_MSG(KERN_INFO "viafb_get_mode_index!\n");
1269
1270         for (i = 0; viafb_modentry[i].mode_index != VIA_RES_INVALID; i++)
1271                 if (viafb_modentry[i].xres == hres &&
1272                         viafb_modentry[i].yres == vres)
1273                         break;
1274
1275         viafb_resMode = viafb_modentry[i].mode_index;
1276         if (flag)
1277                 viafb_mode1 = viafb_modentry[i].mode_res;
1278         else
1279                 viafb_mode = viafb_modentry[i].mode_res;
1280
1281         return viafb_resMode;
1282 }
1283
1284 static void check_available_device_to_enable(int device_id)
1285 {
1286         int device_num = 0;
1287
1288         /* Initialize: */
1289         viafb_CRT_ON = STATE_OFF;
1290         viafb_DVI_ON = STATE_OFF;
1291         viafb_LCD_ON = STATE_OFF;
1292         viafb_LCD2_ON = STATE_OFF;
1293         viafb_DeviceStatus = None_Device;
1294
1295         if ((device_id & CRT_Device) && (device_num < MAX_ACTIVE_DEV_NUM)) {
1296                 viafb_CRT_ON = STATE_ON;
1297                 device_num++;
1298                 viafb_DeviceStatus |= CRT_Device;
1299         }
1300
1301         if ((device_id & DVI_Device) && (device_num < MAX_ACTIVE_DEV_NUM)) {
1302                 viafb_DVI_ON = STATE_ON;
1303                 device_num++;
1304                 viafb_DeviceStatus |= DVI_Device;
1305         }
1306
1307         if ((device_id & LCD_Device) && (device_num < MAX_ACTIVE_DEV_NUM)) {
1308                 viafb_LCD_ON = STATE_ON;
1309                 device_num++;
1310                 viafb_DeviceStatus |= LCD_Device;
1311         }
1312
1313         if ((device_id & LCD2_Device) && (device_num < MAX_ACTIVE_DEV_NUM)) {
1314                 viafb_LCD2_ON = STATE_ON;
1315                 device_num++;
1316                 viafb_DeviceStatus |= LCD2_Device;
1317         }
1318
1319         if (viafb_DeviceStatus == None_Device) {
1320                 /* Use CRT as default active device: */
1321                 viafb_CRT_ON = STATE_ON;
1322                 viafb_DeviceStatus = CRT_Device;
1323         }
1324         DEBUG_MSG(KERN_INFO "Device Status:%x", viafb_DeviceStatus);
1325 }
1326
1327 static void viafb_set_device(struct device_t active_dev)
1328 {
1329         /* Check available device to enable: */
1330         int device_id = None_Device;
1331         if (active_dev.crt)
1332                 device_id |= CRT_Device;
1333         if (active_dev.dvi)
1334                 device_id |= DVI_Device;
1335         if (active_dev.lcd)
1336                 device_id |= LCD_Device;
1337
1338         check_available_device_to_enable(device_id);
1339
1340         /* Check property of LCD: */
1341         if (viafb_LCD_ON) {
1342                 if (active_dev.lcd_dsp_cent) {
1343                         viaparinfo->lvds_setting_info->display_method =
1344                                 viafb_lcd_dsp_method = LCD_CENTERING;
1345                 } else {
1346                         viaparinfo->lvds_setting_info->display_method =
1347                                 viafb_lcd_dsp_method = LCD_EXPANDSION;
1348                 }
1349
1350                 if (active_dev.lcd_mode == LCD_SPWG) {
1351                         viaparinfo->lvds_setting_info->lcd_mode =
1352                                 viafb_lcd_mode = LCD_SPWG;
1353                 } else {
1354                         viaparinfo->lvds_setting_info->lcd_mode =
1355                                 viafb_lcd_mode = LCD_OPENLDI;
1356                 }
1357
1358                 if (active_dev.lcd_panel_id <= LCD_PANEL_ID_MAXIMUM) {
1359                         viafb_lcd_panel_id = active_dev.lcd_panel_id;
1360                         viafb_init_lcd_size();
1361                 }
1362         }
1363
1364         /* Check property of mode: */
1365         if (!active_dev.xres1)
1366                 viafb_second_xres = 640;
1367         else
1368                 viafb_second_xres = active_dev.xres1;
1369         if (!active_dev.yres1)
1370                 viafb_second_yres = 480;
1371         else
1372                 viafb_second_yres = active_dev.yres1;
1373         if (active_dev.bpp != 0)
1374                 viafb_bpp = active_dev.bpp;
1375         if (active_dev.bpp1 != 0)
1376                 viafb_bpp1 = active_dev.bpp1;
1377         if (active_dev.refresh != 0)
1378                 viafb_refresh = active_dev.refresh;
1379         if (active_dev.refresh1 != 0)
1380                 viafb_refresh1 = active_dev.refresh1;
1381         if ((active_dev.samm == STATE_OFF) || (active_dev.samm == STATE_ON))
1382                 viafb_SAMM_ON = active_dev.samm;
1383         viafb_primary_dev = active_dev.primary_dev;
1384
1385         viafb_set_start_addr();
1386         viafb_set_iga_path();
1387 }
1388
1389 static void viafb_set_video_device(u32 video_dev_info)
1390 {
1391         viaparinfo->video_on_crt = STATE_OFF;
1392         viaparinfo->video_on_dvi = STATE_OFF;
1393         viaparinfo->video_on_lcd = STATE_OFF;
1394
1395         /* Check available device to enable: */
1396         if ((video_dev_info & CRT_Device) == CRT_Device)
1397                 viaparinfo->video_on_crt = STATE_ON;
1398         else if ((video_dev_info & DVI_Device) == DVI_Device)
1399                 viaparinfo->video_on_dvi = STATE_ON;
1400         else if ((video_dev_info & LCD_Device) == LCD_Device)
1401                 viaparinfo->video_on_lcd = STATE_ON;
1402 }
1403
1404 static void viafb_get_video_device(u32 *video_dev_info)
1405 {
1406         *video_dev_info = None_Device;
1407         if (viaparinfo->video_on_crt == STATE_ON)
1408                 *video_dev_info |= CRT_Device;
1409         else if (viaparinfo->video_on_dvi == STATE_ON)
1410                 *video_dev_info |= DVI_Device;
1411         else if (viaparinfo->video_on_lcd == STATE_ON)
1412                 *video_dev_info |= LCD_Device;
1413 }
1414
1415 static int get_primary_device(void)
1416 {
1417         int primary_device = 0;
1418         /* Rule: device on iga1 path are the primary device. */
1419         if (viafb_SAMM_ON) {
1420                 if (viafb_CRT_ON) {
1421                         if (viaparinfo->crt_setting_info->iga_path == IGA1) {
1422                                 DEBUG_MSG(KERN_INFO "CRT IGA Path:%d\n",
1423                                         viaparinfo->
1424                                         crt_setting_info->iga_path);
1425                                 primary_device = CRT_Device;
1426                         }
1427                 }
1428                 if (viafb_DVI_ON) {
1429                         if (viaparinfo->tmds_setting_info->iga_path == IGA1) {
1430                                 DEBUG_MSG(KERN_INFO "DVI IGA Path:%d\n",
1431                                         viaparinfo->
1432                                         tmds_setting_info->iga_path);
1433                                 primary_device = DVI_Device;
1434                         }
1435                 }
1436                 if (viafb_LCD_ON) {
1437                         if (viaparinfo->lvds_setting_info->iga_path == IGA1) {
1438                                 DEBUG_MSG(KERN_INFO "LCD IGA Path:%d\n",
1439                                         viaparinfo->
1440                                         lvds_setting_info->iga_path);
1441                                 primary_device = LCD_Device;
1442                         }
1443                 }
1444                 if (viafb_LCD2_ON) {
1445                         if (viaparinfo->lvds_setting_info2->iga_path == IGA1) {
1446                                 DEBUG_MSG(KERN_INFO "LCD2 IGA Path:%d\n",
1447                                         viaparinfo->
1448                                         lvds_setting_info2->iga_path);
1449                                 primary_device = LCD2_Device;
1450                         }
1451                 }
1452         }
1453         return primary_device;
1454 }
1455
1456 static u8 is_duoview(void)
1457 {
1458         if (0 == viafb_SAMM_ON) {
1459                 if (viafb_LCD_ON + viafb_LCD2_ON +
1460                         viafb_DVI_ON + viafb_CRT_ON == 2)
1461                         return true;
1462                 return false;
1463         } else {
1464                 return false;
1465         }
1466 }
1467
1468 static void apply_second_mode_setting(struct fb_var_screeninfo
1469         *sec_var)
1470 {
1471         u32 htotal, vtotal, long_refresh;
1472
1473         htotal = sec_var->xres + sec_var->left_margin +
1474                 sec_var->right_margin + sec_var->hsync_len;
1475         vtotal = sec_var->yres + sec_var->upper_margin +
1476                 sec_var->lower_margin + sec_var->vsync_len;
1477         if ((sec_var->xres_virtual * (sec_var->bits_per_pixel >> 3)) & 0x1F) {
1478                 /*Is 32 bytes alignment? */
1479                 /*32 pixel alignment */
1480                 sec_var->xres_virtual = (sec_var->xres_virtual + 31) & ~31;
1481         }
1482
1483         htotal = sec_var->xres + sec_var->left_margin +
1484                 sec_var->right_margin + sec_var->hsync_len;
1485         vtotal = sec_var->yres + sec_var->upper_margin +
1486                 sec_var->lower_margin + sec_var->vsync_len;
1487         long_refresh = 1000000000UL / sec_var->pixclock * 1000;
1488         long_refresh /= (htotal * vtotal);
1489
1490         viafb_second_xres = sec_var->xres;
1491         viafb_second_yres = sec_var->yres;
1492         viafb_second_virtual_xres = sec_var->xres_virtual;
1493         viafb_second_virtual_yres = sec_var->yres_virtual;
1494         viafb_bpp1 = sec_var->bits_per_pixel;
1495         viafb_refresh1 = viafb_get_refresh(sec_var->xres, sec_var->yres,
1496                 long_refresh);
1497 }
1498
1499 static int apply_device_setting(struct viafb_ioctl_setting setting_info,
1500         struct fb_info *info)
1501 {
1502         int need_set_mode = 0;
1503         DEBUG_MSG(KERN_INFO "apply_device_setting\n");
1504
1505         if (setting_info.device_flag) {
1506                 need_set_mode = 1;
1507                 check_available_device_to_enable(setting_info.device_status);
1508         }
1509
1510         /* Unlock LCD's operation according to LCD flag
1511            and check if the setting value is valid. */
1512         /* If the value is valid, apply the new setting value to the device. */
1513         if (viafb_LCD_ON) {
1514                 if (setting_info.lcd_operation_flag & OP_LCD_CENTERING) {
1515                         need_set_mode = 1;
1516                         if (setting_info.lcd_attributes.display_center) {
1517                                 /* Centering */
1518                                 viaparinfo->lvds_setting_info->display_method =
1519                                     LCD_CENTERING;
1520                                 viafb_lcd_dsp_method = LCD_CENTERING;
1521                                 viaparinfo->lvds_setting_info2->display_method =
1522                                     viafb_lcd_dsp_method = LCD_CENTERING;
1523                         } else {
1524                                 /* expandsion */
1525                                 viaparinfo->lvds_setting_info->display_method =
1526                                     LCD_EXPANDSION;
1527                                 viafb_lcd_dsp_method = LCD_EXPANDSION;
1528                                 viaparinfo->lvds_setting_info2->display_method =
1529                                     LCD_EXPANDSION;
1530                                 viafb_lcd_dsp_method = LCD_EXPANDSION;
1531                         }
1532                 }
1533
1534                 if (setting_info.lcd_operation_flag & OP_LCD_MODE) {
1535                         need_set_mode = 1;
1536                         if (setting_info.lcd_attributes.lcd_mode ==
1537                                 LCD_SPWG) {
1538                                 viaparinfo->lvds_setting_info->lcd_mode =
1539                                         viafb_lcd_mode = LCD_SPWG;
1540                         } else {
1541                                 viaparinfo->lvds_setting_info->lcd_mode =
1542                                         viafb_lcd_mode = LCD_OPENLDI;
1543                         }
1544                         viaparinfo->lvds_setting_info2->lcd_mode =
1545                             viaparinfo->lvds_setting_info->lcd_mode;
1546                 }
1547
1548                 if (setting_info.lcd_operation_flag & OP_LCD_PANEL_ID) {
1549                         need_set_mode = 1;
1550                         if (setting_info.lcd_attributes.panel_id <=
1551                             LCD_PANEL_ID_MAXIMUM) {
1552                                 viafb_lcd_panel_id =
1553                                     setting_info.lcd_attributes.panel_id;
1554                                 viafb_init_lcd_size();
1555                         }
1556                 }
1557         }
1558
1559         if (0 != (setting_info.samm_status & OP_SAMM)) {
1560                 setting_info.samm_status =
1561                     setting_info.samm_status & (~OP_SAMM);
1562                 if (setting_info.samm_status == 0
1563                     || setting_info.samm_status == 1) {
1564                         viafb_SAMM_ON = setting_info.samm_status;
1565
1566                         if (viafb_SAMM_ON)
1567                                 viafb_primary_dev = setting_info.primary_device;
1568
1569                         viafb_set_start_addr();
1570                         viafb_set_iga_path();
1571                 }
1572                 need_set_mode = 1;
1573         }
1574
1575         viaparinfo->duoview = is_duoview();
1576
1577         if (!need_set_mode) {
1578                 ;
1579         } else {
1580                 viafb_set_iga_path();
1581                 viafb_set_par(info);
1582         }
1583         return true;
1584 }
1585
1586 static void retrieve_device_setting(struct viafb_ioctl_setting
1587         *setting_info)
1588 {
1589
1590         /* get device status */
1591         if (viafb_CRT_ON == 1)
1592                 setting_info->device_status = CRT_Device;
1593         if (viafb_DVI_ON == 1)
1594                 setting_info->device_status |= DVI_Device;
1595         if (viafb_LCD_ON == 1)
1596                 setting_info->device_status |= LCD_Device;
1597         if (viafb_LCD2_ON == 1)
1598                 setting_info->device_status |= LCD2_Device;
1599         if ((viaparinfo->video_on_crt == 1) && (viafb_CRT_ON == 1)) {
1600                 setting_info->video_device_status =
1601                         viaparinfo->crt_setting_info->iga_path;
1602         } else if ((viaparinfo->video_on_dvi == 1) && (viafb_DVI_ON == 1)) {
1603                 setting_info->video_device_status =
1604                         viaparinfo->tmds_setting_info->iga_path;
1605         } else if ((viaparinfo->video_on_lcd == 1) && (viafb_LCD_ON == 1)) {
1606                 setting_info->video_device_status =
1607                         viaparinfo->lvds_setting_info->iga_path;
1608         } else {
1609                 setting_info->video_device_status = 0;
1610         }
1611
1612         setting_info->samm_status = viafb_SAMM_ON;
1613         setting_info->primary_device = get_primary_device();
1614
1615         setting_info->first_dev_bpp = viafb_bpp;
1616         setting_info->second_dev_bpp = viafb_bpp1;
1617
1618         setting_info->first_dev_refresh = viafb_refresh;
1619         setting_info->second_dev_refresh = viafb_refresh1;
1620
1621         setting_info->first_dev_hor_res = viafb_hotplug_Xres;
1622         setting_info->first_dev_ver_res = viafb_hotplug_Yres;
1623         setting_info->second_dev_hor_res = viafb_second_xres;
1624         setting_info->second_dev_ver_res = viafb_second_yres;
1625
1626         /* Get lcd attributes */
1627         setting_info->lcd_attributes.display_center = viafb_lcd_dsp_method;
1628         setting_info->lcd_attributes.panel_id = viafb_lcd_panel_id;
1629         setting_info->lcd_attributes.lcd_mode = viafb_lcd_mode;
1630 }
1631
1632 static void parse_active_dev(void)
1633 {
1634         viafb_CRT_ON = STATE_OFF;
1635         viafb_DVI_ON = STATE_OFF;
1636         viafb_LCD_ON = STATE_OFF;
1637         viafb_LCD2_ON = STATE_OFF;
1638         /* 1. Modify the active status of devices. */
1639         /* 2. Keep the order of devices, so we can set corresponding
1640            IGA path to devices in SAMM case. */
1641         /*    Note: The previous of active_dev is primary device,
1642            and the following is secondary device. */
1643         if (!strncmp(viafb_active_dev, "CRT+DVI", 7)) {
1644                 /* CRT+DVI */
1645                 viafb_CRT_ON = STATE_ON;
1646                 viafb_DVI_ON = STATE_ON;
1647                 viafb_primary_dev = CRT_Device;
1648         } else if (!strncmp(viafb_active_dev, "DVI+CRT", 7)) {
1649                 /* DVI+CRT */
1650                 viafb_CRT_ON = STATE_ON;
1651                 viafb_DVI_ON = STATE_ON;
1652                 viafb_primary_dev = DVI_Device;
1653         } else if (!strncmp(viafb_active_dev, "CRT+LCD", 7)) {
1654                 /* CRT+LCD */
1655                 viafb_CRT_ON = STATE_ON;
1656                 viafb_LCD_ON = STATE_ON;
1657                 viafb_primary_dev = CRT_Device;
1658         } else if (!strncmp(viafb_active_dev, "LCD+CRT", 7)) {
1659                 /* LCD+CRT */
1660                 viafb_CRT_ON = STATE_ON;
1661                 viafb_LCD_ON = STATE_ON;
1662                 viafb_primary_dev = LCD_Device;
1663         } else if (!strncmp(viafb_active_dev, "DVI+LCD", 7)) {
1664                 /* DVI+LCD */
1665                 viafb_DVI_ON = STATE_ON;
1666                 viafb_LCD_ON = STATE_ON;
1667                 viafb_primary_dev = DVI_Device;
1668         } else if (!strncmp(viafb_active_dev, "LCD+DVI", 7)) {
1669                 /* LCD+DVI */
1670                 viafb_DVI_ON = STATE_ON;
1671                 viafb_LCD_ON = STATE_ON;
1672                 viafb_primary_dev = LCD_Device;
1673         } else if (!strncmp(viafb_active_dev, "LCD+LCD2", 8)) {
1674                 viafb_LCD_ON = STATE_ON;
1675                 viafb_LCD2_ON = STATE_ON;
1676                 viafb_primary_dev = LCD_Device;
1677         } else if (!strncmp(viafb_active_dev, "LCD2+LCD", 8)) {
1678                 viafb_LCD_ON = STATE_ON;
1679                 viafb_LCD2_ON = STATE_ON;
1680                 viafb_primary_dev = LCD2_Device;
1681         } else if (!strncmp(viafb_active_dev, "CRT", 3)) {
1682                 /* CRT only */
1683                 viafb_CRT_ON = STATE_ON;
1684                 viafb_SAMM_ON = STATE_OFF;
1685         } else if (!strncmp(viafb_active_dev, "DVI", 3)) {
1686                 /* DVI only */
1687                 viafb_DVI_ON = STATE_ON;
1688                 viafb_SAMM_ON = STATE_OFF;
1689         } else if (!strncmp(viafb_active_dev, "LCD", 3)) {
1690                 /* LCD only */
1691                 viafb_LCD_ON = STATE_ON;
1692                 viafb_SAMM_ON = STATE_OFF;
1693         } else {
1694                 viafb_CRT_ON = STATE_ON;
1695                 viafb_SAMM_ON = STATE_OFF;
1696         }
1697         viaparinfo->duoview = is_duoview();
1698 }
1699
1700 static void parse_video_dev(void)
1701 {
1702         viaparinfo->video_on_crt = STATE_OFF;
1703         viaparinfo->video_on_dvi = STATE_OFF;
1704         viaparinfo->video_on_lcd = STATE_OFF;
1705
1706         if (!strncmp(viafb_video_dev, "CRT", 3)) {
1707                 /* Video on CRT */
1708                 viaparinfo->video_on_crt = STATE_ON;
1709         } else if (!strncmp(viafb_video_dev, "DVI", 3)) {
1710                 /* Video on DVI */
1711                 viaparinfo->video_on_dvi = STATE_ON;
1712         } else if (!strncmp(viafb_video_dev, "LCD", 3)) {
1713                 /* Video on LCD */
1714                 viaparinfo->video_on_lcd = STATE_ON;
1715         }
1716 }
1717
1718 static int parse_port(char *opt_str, int *output_interface)
1719 {
1720         if (!strncmp(opt_str, "DVP0", 4))
1721                 *output_interface = INTERFACE_DVP0;
1722         else if (!strncmp(opt_str, "DVP1", 4))
1723                 *output_interface = INTERFACE_DVP1;
1724         else if (!strncmp(opt_str, "DFP_HIGHLOW", 11))
1725                 *output_interface = INTERFACE_DFP;
1726         else if (!strncmp(opt_str, "DFP_HIGH", 8))
1727                 *output_interface = INTERFACE_DFP_HIGH;
1728         else if (!strncmp(opt_str, "DFP_LOW", 7))
1729                 *output_interface = INTERFACE_DFP_LOW;
1730         else
1731                 *output_interface = INTERFACE_NONE;
1732         return 0;
1733 }
1734
1735 static void parse_lcd_port(void)
1736 {
1737         parse_port(viafb_lcd_port, &viaparinfo->chip_info->lvds_chip_info.
1738                 output_interface);
1739         /*Initialize to avoid unexpected behavior */
1740         viaparinfo->chip_info->lvds_chip_info2.output_interface =
1741         INTERFACE_NONE;
1742
1743         DEBUG_MSG(KERN_INFO "parse_lcd_port: viafb_lcd_port:%s,interface:%d\n",
1744                   viafb_lcd_port, viaparinfo->chip_info->lvds_chip_info.
1745                   output_interface);
1746 }
1747
1748 static void parse_dvi_port(void)
1749 {
1750         parse_port(viafb_dvi_port, &viaparinfo->chip_info->tmds_chip_info.
1751                 output_interface);
1752
1753         DEBUG_MSG(KERN_INFO "parse_dvi_port: viafb_dvi_port:%s,interface:%d\n",
1754                   viafb_dvi_port, viaparinfo->chip_info->tmds_chip_info.
1755                   output_interface);
1756 }
1757
1758 /*
1759  * The proc filesystem read/write function, a simple proc implement to
1760  * get/set the value of DPA  DVP0,   DVP0DataDriving,  DVP0ClockDriving, DVP1,
1761  * DVP1Driving, DFPHigh, DFPLow CR96,   SR2A[5], SR1B[1], SR2A[4], SR1E[2],
1762  * CR9B,    SR65,    CR97,    CR99
1763  */
1764 static int viafb_dvp0_proc_read(char *buf, char **start, off_t offset,
1765 int count, int *eof, void *data)
1766 {
1767         int len = 0;
1768         u8 dvp0_data_dri = 0, dvp0_clk_dri = 0, dvp0 = 0;
1769         dvp0_data_dri =
1770             (viafb_read_reg(VIASR, SR2A) & BIT5) >> 4 |
1771             (viafb_read_reg(VIASR, SR1B) & BIT1) >> 1;
1772         dvp0_clk_dri =
1773             (viafb_read_reg(VIASR, SR2A) & BIT4) >> 3 |
1774             (viafb_read_reg(VIASR, SR1E) & BIT2) >> 2;
1775         dvp0 = viafb_read_reg(VIACR, CR96) & 0x0f;
1776         len +=
1777             sprintf(buf + len, "%x %x %x\n", dvp0, dvp0_data_dri, dvp0_clk_dri);
1778         *eof = 1;               /*Inform kernel end of data */
1779         return len;
1780 }
1781 static int viafb_dvp0_proc_write(struct file *file,
1782         const char __user *buffer, unsigned long count, void *data)
1783 {
1784         char buf[20], *value, *pbuf;
1785         u8 reg_val = 0;
1786         unsigned long length, i;
1787         if (count < 1)
1788                 return -EINVAL;
1789         length = count > 20 ? 20 : count;
1790         if (copy_from_user(&buf[0], buffer, length))
1791                 return -EFAULT;
1792         buf[length - 1] = '\0'; /*Ensure end string */
1793         pbuf = &buf[0];
1794         for (i = 0; i < 3; i++) {
1795                 value = strsep(&pbuf, " ");
1796                 if (value != NULL) {
1797                         strict_strtoul(value, 0, (unsigned long *)&reg_val);
1798                         DEBUG_MSG(KERN_INFO "DVP0:reg_val[%l]=:%x\n", i,
1799                                   reg_val);
1800                         switch (i) {
1801                         case 0:
1802                                 viafb_write_reg_mask(CR96, VIACR,
1803                                         reg_val, 0x0f);
1804                                 break;
1805                         case 1:
1806                                 viafb_write_reg_mask(SR2A, VIASR,
1807                                         reg_val << 4, BIT5);
1808                                 viafb_write_reg_mask(SR1B, VIASR,
1809                                         reg_val << 1, BIT1);
1810                                 break;
1811                         case 2:
1812                                 viafb_write_reg_mask(SR2A, VIASR,
1813                                         reg_val << 3, BIT4);
1814                                 viafb_write_reg_mask(SR1E, VIASR,
1815                                         reg_val << 2, BIT2);
1816                                 break;
1817                         default:
1818                                 break;
1819                         }
1820                 } else {
1821                         break;
1822                 }
1823         }
1824         return count;
1825 }
1826 static int viafb_dvp1_proc_read(char *buf, char **start, off_t offset,
1827         int count, int *eof, void *data)
1828 {
1829         int len = 0;
1830         u8 dvp1 = 0, dvp1_data_dri = 0, dvp1_clk_dri = 0;
1831         dvp1 = viafb_read_reg(VIACR, CR9B) & 0x0f;
1832         dvp1_data_dri = (viafb_read_reg(VIASR, SR65) & 0x0c) >> 2;
1833         dvp1_clk_dri = viafb_read_reg(VIASR, SR65) & 0x03;
1834         len +=
1835             sprintf(buf + len, "%x %x %x\n", dvp1, dvp1_data_dri, dvp1_clk_dri);
1836         *eof = 1;               /*Inform kernel end of data */
1837         return len;
1838 }
1839 static int viafb_dvp1_proc_write(struct file *file,
1840         const char __user *buffer, unsigned long count, void *data)
1841 {
1842         char buf[20], *value, *pbuf;
1843         u8 reg_val = 0;
1844         unsigned long length, i;
1845         if (count < 1)
1846                 return -EINVAL;
1847         length = count > 20 ? 20 : count;
1848         if (copy_from_user(&buf[0], buffer, length))
1849                 return -EFAULT;
1850         buf[length - 1] = '\0'; /*Ensure end string */
1851         pbuf = &buf[0];
1852         for (i = 0; i < 3; i++) {
1853                 value = strsep(&pbuf, " ");
1854                 if (value != NULL) {
1855                         strict_strtoul(value, 0, (unsigned long *)&reg_val);
1856                         switch (i) {
1857                         case 0:
1858                                 viafb_write_reg_mask(CR9B, VIACR,
1859                                         reg_val, 0x0f);
1860                                 break;
1861                         case 1:
1862                                 viafb_write_reg_mask(SR65, VIASR,
1863                                         reg_val << 2, 0x0c);
1864                                 break;
1865                         case 2:
1866                                 viafb_write_reg_mask(SR65, VIASR,
1867                                         reg_val, 0x03);
1868                                 break;
1869                         default:
1870                                 break;
1871                         }
1872                 } else {
1873                         break;
1874                 }
1875         }
1876         return count;
1877 }
1878
1879 static int viafb_dfph_proc_read(char *buf, char **start, off_t offset,
1880         int count, int *eof, void *data)
1881 {
1882         int len = 0;
1883         u8 dfp_high = 0;
1884         dfp_high = viafb_read_reg(VIACR, CR97) & 0x0f;
1885         len += sprintf(buf + len, "%x\n", dfp_high);
1886         *eof = 1;               /*Inform kernel end of data */
1887         return len;
1888 }
1889 static int viafb_dfph_proc_write(struct file *file,
1890         const char __user *buffer, unsigned long count, void *data)
1891 {
1892         char buf[20];
1893         u8 reg_val = 0;
1894         unsigned long length;
1895         if (count < 1)
1896                 return -EINVAL;
1897         length = count > 20 ? 20 : count;
1898         if (copy_from_user(&buf[0], buffer, length))
1899                 return -EFAULT;
1900         buf[length - 1] = '\0'; /*Ensure end string */
1901         strict_strtoul(&buf[0], 0, (unsigned long *)&reg_val);
1902         viafb_write_reg_mask(CR97, VIACR, reg_val, 0x0f);
1903         return count;
1904 }
1905 static int viafb_dfpl_proc_read(char *buf, char **start, off_t offset,
1906         int count, int *eof, void *data)
1907 {
1908         int len = 0;
1909         u8 dfp_low = 0;
1910         dfp_low = viafb_read_reg(VIACR, CR99) & 0x0f;
1911         len += sprintf(buf + len, "%x\n", dfp_low);
1912         *eof = 1;               /*Inform kernel end of data */
1913         return len;
1914 }
1915 static int viafb_dfpl_proc_write(struct file *file,
1916         const char __user *buffer, unsigned long count, void *data)
1917 {
1918         char buf[20];
1919         u8 reg_val = 0;
1920         unsigned long length;
1921         if (count < 1)
1922                 return -EINVAL;
1923         length = count > 20 ? 20 : count;
1924         if (copy_from_user(&buf[0], buffer, length))
1925                 return -EFAULT;
1926         buf[length - 1] = '\0'; /*Ensure end string */
1927         strict_strtoul(&buf[0], 0, (unsigned long *)&reg_val);
1928         viafb_write_reg_mask(CR99, VIACR, reg_val, 0x0f);
1929         return count;
1930 }
1931 static int viafb_vt1636_proc_read(char *buf, char **start,
1932         off_t offset, int count, int *eof, void *data)
1933 {
1934         int len = 0;
1935         u8 vt1636_08 = 0, vt1636_09 = 0;
1936         switch (viaparinfo->chip_info->lvds_chip_info.lvds_chip_name) {
1937         case VT1636_LVDS:
1938                 vt1636_08 =
1939                     viafb_gpio_i2c_read_lvds(viaparinfo->lvds_setting_info,
1940                     &viaparinfo->chip_info->lvds_chip_info, 0x08) & 0x0f;
1941                 vt1636_09 =
1942                     viafb_gpio_i2c_read_lvds(viaparinfo->lvds_setting_info,
1943                     &viaparinfo->chip_info->lvds_chip_info, 0x09) & 0x1f;
1944                 len += sprintf(buf + len, "%x %x\n", vt1636_08, vt1636_09);
1945                 break;
1946         default:
1947                 break;
1948         }
1949         switch (viaparinfo->chip_info->lvds_chip_info2.lvds_chip_name) {
1950         case VT1636_LVDS:
1951                 vt1636_08 =
1952                     viafb_gpio_i2c_read_lvds(viaparinfo->lvds_setting_info2,
1953                         &viaparinfo->chip_info->lvds_chip_info2, 0x08) & 0x0f;
1954                 vt1636_09 =
1955                     viafb_gpio_i2c_read_lvds(viaparinfo->lvds_setting_info2,
1956                         &viaparinfo->chip_info->lvds_chip_info2, 0x09) & 0x1f;
1957                 len += sprintf(buf + len, " %x %x\n", vt1636_08, vt1636_09);
1958                 break;
1959         default:
1960                 break;
1961         }
1962         *eof = 1;               /*Inform kernel end of data */
1963         return len;
1964 }
1965 static int viafb_vt1636_proc_write(struct file *file,
1966         const char __user *buffer, unsigned long count, void *data)
1967 {
1968         char buf[30], *value, *pbuf;
1969         struct IODATA reg_val;
1970         unsigned long length, i;
1971         if (count < 1)
1972                 return -EINVAL;
1973         length = count > 30 ? 30 : count;
1974         if (copy_from_user(&buf[0], buffer, length))
1975                 return -EFAULT;
1976         buf[length - 1] = '\0'; /*Ensure end string */
1977         pbuf = &buf[0];
1978         switch (viaparinfo->chip_info->lvds_chip_info.lvds_chip_name) {
1979         case VT1636_LVDS:
1980                 for (i = 0; i < 2; i++) {
1981                         value = strsep(&pbuf, " ");
1982                         if (value != NULL) {
1983                                 strict_strtoul(value, 0,
1984                                         (unsigned long *)&reg_val.Data);
1985                                 switch (i) {
1986                                 case 0:
1987                                         reg_val.Index = 0x08;
1988                                         reg_val.Mask = 0x0f;
1989                                         viafb_gpio_i2c_write_mask_lvds
1990                                             (viaparinfo->lvds_setting_info,
1991                                             &viaparinfo->
1992                                             chip_info->lvds_chip_info,
1993                                              reg_val);
1994                                         break;
1995                                 case 1:
1996                                         reg_val.Index = 0x09;
1997                                         reg_val.Mask = 0x1f;
1998                                         viafb_gpio_i2c_write_mask_lvds
1999                                             (viaparinfo->lvds_setting_info,
2000                                             &viaparinfo->
2001                                             chip_info->lvds_chip_info,
2002                                              reg_val);
2003                                         break;
2004                                 default:
2005                                         break;
2006                                 }
2007                         } else {
2008                                 break;
2009                         }
2010                 }
2011                 break;
2012         default:
2013                 break;
2014         }
2015         switch (viaparinfo->chip_info->lvds_chip_info2.lvds_chip_name) {
2016         case VT1636_LVDS:
2017                 for (i = 0; i < 2; i++) {
2018                         value = strsep(&pbuf, " ");
2019                         if (value != NULL) {
2020                                 strict_strtoul(value, 0,
2021                                         (unsigned long *)&reg_val.Data);
2022                                 switch (i) {
2023                                 case 0:
2024                                         reg_val.Index = 0x08;
2025                                         reg_val.Mask = 0x0f;
2026                                         viafb_gpio_i2c_write_mask_lvds
2027                                             (viaparinfo->lvds_setting_info2,
2028                                             &viaparinfo->
2029                                             chip_info->lvds_chip_info2,
2030                                              reg_val);
2031                                         break;
2032                                 case 1:
2033                                         reg_val.Index = 0x09;
2034                                         reg_val.Mask = 0x1f;
2035                                         viafb_gpio_i2c_write_mask_lvds
2036                                             (viaparinfo->lvds_setting_info2,
2037                                             &viaparinfo->
2038                                             chip_info->lvds_chip_info2,
2039                                              reg_val);
2040                                         break;
2041                                 default:
2042                                         break;
2043                                 }
2044                         } else {
2045                                 break;
2046                         }
2047                 }
2048                 break;
2049         default:
2050                 break;
2051         }
2052         return count;
2053 }
2054
2055 static void viafb_init_proc(struct proc_dir_entry **viafb_entry)
2056 {
2057         struct proc_dir_entry *entry;
2058         *viafb_entry = proc_mkdir("viafb", NULL);
2059         if (viafb_entry) {
2060                 entry = create_proc_entry("dvp0", 0, *viafb_entry);
2061                 if (entry) {
2062                         entry->read_proc = viafb_dvp0_proc_read;
2063                         entry->write_proc = viafb_dvp0_proc_write;
2064                 }
2065                 entry = create_proc_entry("dvp1", 0, *viafb_entry);
2066                 if (entry) {
2067                         entry->read_proc = viafb_dvp1_proc_read;
2068                         entry->write_proc = viafb_dvp1_proc_write;
2069                 }
2070                 entry = create_proc_entry("dfph", 0, *viafb_entry);
2071                 if (entry) {
2072                         entry->read_proc = viafb_dfph_proc_read;
2073                         entry->write_proc = viafb_dfph_proc_write;
2074                 }
2075                 entry = create_proc_entry("dfpl", 0, *viafb_entry);
2076                 if (entry) {
2077                         entry->read_proc = viafb_dfpl_proc_read;
2078                         entry->write_proc = viafb_dfpl_proc_write;
2079                 }
2080                 if (VT1636_LVDS == viaparinfo->chip_info->lvds_chip_info.
2081                         lvds_chip_name || VT1636_LVDS ==
2082                     viaparinfo->chip_info->lvds_chip_info2.lvds_chip_name) {
2083                         entry = create_proc_entry("vt1636", 0, *viafb_entry);
2084                         if (entry) {
2085                                 entry->read_proc = viafb_vt1636_proc_read;
2086                                 entry->write_proc = viafb_vt1636_proc_write;
2087                         }
2088                 }
2089
2090         }
2091 }
2092 static void viafb_remove_proc(struct proc_dir_entry *viafb_entry)
2093 {
2094         /* no problem if it was not registered */
2095         remove_proc_entry("dvp0", viafb_entry);/* parent dir */
2096         remove_proc_entry("dvp1", viafb_entry);
2097         remove_proc_entry("dfph", viafb_entry);
2098         remove_proc_entry("dfpl", viafb_entry);
2099         remove_proc_entry("vt1636", viafb_entry);
2100         remove_proc_entry("vt1625", viafb_entry);
2101         remove_proc_entry("viafb", NULL);
2102 }
2103
2104 static int __devinit via_pci_probe(void)
2105 {
2106         unsigned long default_xres, default_yres;
2107         char *tmpc, *tmpm;
2108         char *tmpc_sec, *tmpm_sec;
2109         int vmode_index;
2110         u32 tmds_length, lvds_length, crt_length, chip_length, viafb_par_length;
2111
2112         DEBUG_MSG(KERN_INFO "VIAFB PCI Probe!!\n");
2113
2114         viafb_par_length = ALIGN(sizeof(struct viafb_par), BITS_PER_LONG/8);
2115         tmds_length = ALIGN(sizeof(struct tmds_setting_information),
2116                 BITS_PER_LONG/8);
2117         lvds_length = ALIGN(sizeof(struct lvds_setting_information),
2118                 BITS_PER_LONG/8);
2119         crt_length = ALIGN(sizeof(struct lvds_setting_information),
2120                 BITS_PER_LONG/8);
2121         chip_length = ALIGN(sizeof(struct chip_information), BITS_PER_LONG/8);
2122
2123         /* Allocate fb_info and ***_par here, also including some other needed
2124          * variables
2125         */
2126         viafbinfo = framebuffer_alloc(viafb_par_length + 2 * lvds_length +
2127         tmds_length + crt_length + chip_length, NULL);
2128         if (!viafbinfo) {
2129                 printk(KERN_ERR"Could not allocate memory for viafb_info.\n");
2130                 return -ENODEV;
2131         }
2132
2133         viaparinfo = (struct viafb_par *)viafbinfo->par;
2134         viaparinfo->tmds_setting_info = (struct tmds_setting_information *)
2135                 ((unsigned long)viaparinfo + viafb_par_length);
2136         viaparinfo->lvds_setting_info = (struct lvds_setting_information *)
2137                 ((unsigned long)viaparinfo->tmds_setting_info + tmds_length);
2138         viaparinfo->lvds_setting_info2 = (struct lvds_setting_information *)
2139                 ((unsigned long)viaparinfo->lvds_setting_info + lvds_length);
2140         viaparinfo->crt_setting_info = (struct crt_setting_information *)
2141                 ((unsigned long)viaparinfo->lvds_setting_info2 + lvds_length);
2142         viaparinfo->chip_info = (struct chip_information *)
2143                 ((unsigned long)viaparinfo->crt_setting_info + crt_length);
2144
2145         if (viafb_dual_fb)
2146                 viafb_SAMM_ON = 1;
2147         parse_active_dev();
2148         parse_video_dev();
2149         parse_lcd_port();
2150         parse_dvi_port();
2151
2152         /* for dual-fb must viafb_SAMM_ON=1 and viafb_dual_fb=1 */
2153         if (!viafb_SAMM_ON)
2154                 viafb_dual_fb = 0;
2155
2156         /* Set up I2C bus stuff */
2157         viafb_create_i2c_bus(viaparinfo);
2158
2159         viafb_init_chip_info();
2160         viafb_get_fb_info(&viaparinfo->fbmem, &viaparinfo->memsize);
2161         viaparinfo->fbmem_free = viaparinfo->memsize;
2162         viaparinfo->fbmem_used = 0;
2163         viaparinfo->fbmem_virt = ioremap_nocache(viaparinfo->fbmem,
2164                 viaparinfo->memsize);
2165         viafbinfo->screen_base = (char *)viaparinfo->fbmem_virt;
2166
2167         if (!viaparinfo->fbmem_virt) {
2168                 printk(KERN_INFO "ioremap failed\n");
2169                 return -1;
2170         }
2171
2172         viafb_get_mmio_info(&viaparinfo->mmio_base, &viaparinfo->mmio_len);
2173         viaparinfo->io_virt = ioremap_nocache(viaparinfo->mmio_base,
2174                 viaparinfo->mmio_len);
2175
2176         viafbinfo->node = 0;
2177         viafbinfo->fbops = &viafb_ops;
2178         viafbinfo->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
2179
2180         viafbinfo->pseudo_palette = pseudo_pal;
2181         if (viafb_accel) {
2182                 viafb_init_accel();
2183                 viafb_init_2d_engine();
2184                 viafb_hw_cursor_init();
2185         }
2186
2187         if (viafb_second_size && (viafb_second_size < 8)) {
2188                 viafb_second_offset = viaparinfo->fbmem_free -
2189                         viafb_second_size * 1024 * 1024;
2190         } else {
2191                 viafb_second_size = 8;
2192                 viafb_second_offset = viaparinfo->fbmem_free -
2193                         viafb_second_size * 1024 * 1024;
2194         }
2195
2196         viafb_FB_MM = viaparinfo->fbmem_virt;
2197         tmpm = viafb_mode;
2198         tmpc = strsep(&tmpm, "x");
2199         strict_strtoul(tmpc, 0, &default_xres);
2200         strict_strtoul(tmpm, 0, &default_yres);
2201
2202         vmode_index = viafb_get_mode_index(default_xres, default_yres, 0);
2203         DEBUG_MSG(KERN_INFO "0->index=%d\n", vmode_index);
2204
2205         if (viafb_SAMM_ON == 1) {
2206                 if (strcmp(viafb_mode, viafb_mode1)) {
2207                         tmpm_sec = viafb_mode1;
2208                         tmpc_sec = strsep(&tmpm_sec, "x");
2209                         strict_strtoul(tmpc_sec, 0,
2210                                 (unsigned long *)&viafb_second_xres);
2211                         strict_strtoul(tmpm_sec, 0,
2212                                 (unsigned long *)&viafb_second_yres);
2213                 } else {
2214                         viafb_second_xres = default_xres;
2215                         viafb_second_yres = default_yres;
2216                 }
2217                 if (0 == viafb_second_virtual_xres) {
2218                         switch (viafb_second_xres) {
2219                         case 1400:
2220                                 viafb_second_virtual_xres = 1408;
2221                                 break;
2222                         default:
2223                                 viafb_second_virtual_xres = viafb_second_xres;
2224                                 break;
2225                         }
2226                 }
2227                 if (0 == viafb_second_virtual_yres)
2228                         viafb_second_virtual_yres = viafb_second_yres;
2229         }
2230
2231         switch (viafb_bpp) {
2232         case 0 ... 8:
2233                 viafb_bpp = 8;
2234                 break;
2235         case 9 ... 16:
2236                 viafb_bpp = 16;
2237                 break;
2238         case 17 ... 32:
2239                 viafb_bpp = 32;
2240                 break;
2241         default:
2242                 viafb_bpp = 8;
2243         }
2244         default_var.xres = default_xres;
2245         default_var.yres = default_yres;
2246         switch (default_xres) {
2247         case 1400:
2248                 default_var.xres_virtual = 1408;
2249                 break;
2250         default:
2251                 default_var.xres_virtual = default_xres;
2252                 break;
2253         }
2254         default_var.yres_virtual = default_yres;
2255         default_var.bits_per_pixel = viafb_bpp;
2256         if (default_var.bits_per_pixel == 15)
2257                 default_var.bits_per_pixel = 16;
2258         default_var.pixclock =
2259             viafb_get_pixclock(default_xres, default_yres, viafb_refresh);
2260         default_var.left_margin = (default_xres >> 3) & 0xf8;
2261         default_var.right_margin = 32;
2262         default_var.upper_margin = 16;
2263         default_var.lower_margin = 4;
2264         default_var.hsync_len = default_var.left_margin;
2265         default_var.vsync_len = 4;
2266         default_var.accel_flags = 0;
2267
2268         if (viafb_accel) {
2269                 viafbinfo->flags |=
2270                     (FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT |
2271                      FBINFO_HWACCEL_IMAGEBLIT);
2272                 default_var.accel_flags |= FB_ACCELF_TEXT;
2273         } else
2274                 viafbinfo->flags |= FBINFO_HWACCEL_DISABLED;
2275
2276         if (viafb_dual_fb) {
2277                 viafbinfo1 = framebuffer_alloc(viafb_par_length, NULL);
2278                 if (!viafbinfo1) {
2279                         printk(KERN_ERR
2280                         "allocate the second framebuffer struct error\n");
2281                         framebuffer_release(viafbinfo);
2282                         return -ENOMEM;
2283                 }
2284                 viaparinfo1 = viafbinfo1->par;
2285                 memcpy(viaparinfo1, viaparinfo, viafb_par_length);
2286                 viaparinfo1->memsize = viaparinfo->memsize -
2287                         viafb_second_offset;
2288                 viaparinfo->memsize = viafb_second_offset;
2289                 viaparinfo1->fbmem_virt = viaparinfo->fbmem_virt +
2290                         viafb_second_offset;
2291                 viaparinfo1->fbmem = viaparinfo->fbmem + viafb_second_offset;
2292
2293                 viaparinfo1->fbmem_used = viaparinfo->fbmem_used;
2294                 viaparinfo1->fbmem_free = viaparinfo1->memsize -
2295                         viaparinfo1->fbmem_used;
2296                 viaparinfo->fbmem_free = viaparinfo->memsize;
2297                 viaparinfo->fbmem_used = 0;
2298                 if (viafb_accel) {
2299                         viaparinfo1->cursor_start =
2300                             viaparinfo->cursor_start - viafb_second_offset;
2301                         viaparinfo1->VQ_start = viaparinfo->VQ_start -
2302                                 viafb_second_offset;
2303                         viaparinfo1->VQ_end = viaparinfo->VQ_end -
2304                                 viafb_second_offset;
2305                 }
2306
2307                 memcpy(viafbinfo1, viafbinfo, sizeof(struct fb_info));
2308                 viafbinfo1->screen_base = viafbinfo->screen_base +
2309                         viafb_second_offset;
2310                 viafbinfo1->fix.smem_start = viaparinfo1->fbmem;
2311                 viafbinfo1->fix.smem_len = viaparinfo1->fbmem_free;
2312
2313                 default_var.xres = viafb_second_xres;
2314                 default_var.yres = viafb_second_yres;
2315                 default_var.xres_virtual = viafb_second_virtual_xres;
2316                 default_var.yres_virtual = viafb_second_virtual_yres;
2317                 if (viafb_bpp1 != viafb_bpp)
2318                         viafb_bpp1 = viafb_bpp;
2319                 default_var.bits_per_pixel = viafb_bpp1;
2320                 default_var.pixclock =
2321                     viafb_get_pixclock(viafb_second_xres, viafb_second_yres,
2322                     viafb_refresh);
2323                 default_var.left_margin = (viafb_second_xres >> 3) & 0xf8;
2324                 default_var.right_margin = 32;
2325                 default_var.upper_margin = 16;
2326                 default_var.lower_margin = 4;
2327                 default_var.hsync_len = default_var.left_margin;
2328                 default_var.vsync_len = 4;
2329
2330                 viafb_setup_fixinfo(&viafbinfo1->fix, viaparinfo1);
2331                 viafb_check_var(&default_var, viafbinfo1);
2332                 viafbinfo1->var = default_var;
2333                 viafb_update_viafb_par(viafbinfo);
2334                 viafb_update_fix(&viafbinfo1->fix, viafbinfo1);
2335         }
2336
2337         viafb_setup_fixinfo(&viafbinfo->fix, viaparinfo);
2338         viafb_check_var(&default_var, viafbinfo);
2339         viafbinfo->var = default_var;
2340         viafb_update_viafb_par(viafbinfo);
2341         viafb_update_fix(&viafbinfo->fix, viafbinfo);
2342         default_var.activate = FB_ACTIVATE_NOW;
2343         fb_alloc_cmap(&viafbinfo->cmap, 256, 0);
2344
2345         if (viafb_dual_fb && (viafb_primary_dev == LCD_Device)
2346             && (viaparinfo->chip_info->gfx_chip_name == UNICHROME_CLE266)) {
2347                 if (register_framebuffer(viafbinfo1) < 0)
2348                         return -EINVAL;
2349         }
2350         if (register_framebuffer(viafbinfo) < 0)
2351                 return -EINVAL;
2352
2353         if (viafb_dual_fb && ((viafb_primary_dev != LCD_Device)
2354                         || (viaparinfo->chip_info->gfx_chip_name !=
2355                         UNICHROME_CLE266))) {
2356                 if (register_framebuffer(viafbinfo1) < 0)
2357                         return -EINVAL;
2358         }
2359         DEBUG_MSG(KERN_INFO "fb%d: %s frame buffer device %dx%d-%dbpp\n",
2360                   viafbinfo->node, viafbinfo->fix.id, default_var.xres,
2361                   default_var.yres, default_var.bits_per_pixel);
2362
2363         viafb_init_proc(&viaparinfo->proc_entry);
2364         viafb_init_dac(IGA2);
2365         return 0;
2366 }
2367
2368 static void __devexit via_pci_remove(void)
2369 {
2370         DEBUG_MSG(KERN_INFO "via_pci_remove!\n");
2371         fb_dealloc_cmap(&viafbinfo->cmap);
2372         unregister_framebuffer(viafbinfo);
2373         if (viafb_dual_fb)
2374                 unregister_framebuffer(viafbinfo1);
2375         iounmap((void *)viaparinfo->fbmem_virt);
2376         iounmap(viaparinfo->io_virt);
2377
2378         viafb_delete_i2c_buss(viaparinfo);
2379
2380         framebuffer_release(viafbinfo);
2381         if (viafb_dual_fb)
2382                 framebuffer_release(viafbinfo1);
2383
2384         viafb_remove_proc(viaparinfo->proc_entry);
2385 }
2386
2387 #ifndef MODULE
2388 static int __init viafb_setup(char *options)
2389 {
2390         char *this_opt;
2391         DEBUG_MSG(KERN_INFO "viafb_setup!\n");
2392
2393         if (!options || !*options)
2394                 return 0;
2395
2396         while ((this_opt = strsep(&options, ",")) != NULL) {
2397                 if (!*this_opt)
2398                         continue;
2399
2400                 if (!strncmp(this_opt, "viafb_mode1=", 12))
2401                         viafb_mode1 = kstrdup(this_opt + 12, GFP_KERNEL);
2402                 else if (!strncmp(this_opt, "viafb_mode=", 11))
2403                         viafb_mode = kstrdup(this_opt + 11, GFP_KERNEL);
2404                 else if (!strncmp(this_opt, "viafb_bpp1=", 11))
2405                         strict_strtoul(this_opt + 11, 0,
2406                                 (unsigned long *)&viafb_bpp1);
2407                 else if (!strncmp(this_opt, "viafb_bpp=", 10))
2408                         strict_strtoul(this_opt + 10, 0,
2409                                 (unsigned long *)&viafb_bpp);
2410                 else if (!strncmp(this_opt, "viafb_refresh1=", 15))
2411                         strict_strtoul(this_opt + 15, 0,
2412                                 (unsigned long *)&viafb_refresh1);
2413                 else if (!strncmp(this_opt, "viafb_refresh=", 14))
2414                         strict_strtoul(this_opt + 14, 0,
2415                                 (unsigned long *)&viafb_refresh);
2416                 else if (!strncmp(this_opt, "viafb_lcd_dsp_method=", 21))
2417                         strict_strtoul(this_opt + 21, 0,
2418                                 (unsigned long *)&viafb_lcd_dsp_method);
2419                 else if (!strncmp(this_opt, "viafb_lcd_panel_id=", 19))
2420                         strict_strtoul(this_opt + 19, 0,
2421                                 (unsigned long *)&viafb_lcd_panel_id);
2422                 else if (!strncmp(this_opt, "viafb_accel=", 12))
2423                         strict_strtoul(this_opt + 12, 0,
2424                                 (unsigned long *)&viafb_accel);
2425                 else if (!strncmp(this_opt, "viafb_SAMM_ON=", 14))
2426                         strict_strtoul(this_opt + 14, 0,
2427                                 (unsigned long *)&viafb_SAMM_ON);
2428                 else if (!strncmp(this_opt, "viafb_active_dev=", 17))
2429                         viafb_active_dev = kstrdup(this_opt + 17, GFP_KERNEL);
2430                 else if (!strncmp(this_opt,
2431                         "viafb_display_hardware_layout=", 30))
2432                         strict_strtoul(this_opt + 30, 0,
2433                         (unsigned long *)&viafb_display_hardware_layout);
2434                 else if (!strncmp(this_opt, "viafb_second_size=", 18))
2435                         strict_strtoul(this_opt + 18, 0,
2436                                 (unsigned long *)&viafb_second_size);
2437                 else if (!strncmp(this_opt,
2438                         "viafb_platform_epia_dvi=", 24))
2439                         strict_strtoul(this_opt + 24, 0,
2440                                 (unsigned long *)&viafb_platform_epia_dvi);
2441                 else if (!strncmp(this_opt,
2442                         "viafb_device_lcd_dualedge=", 26))
2443                         strict_strtoul(this_opt + 26, 0,
2444                                 (unsigned long *)&viafb_device_lcd_dualedge);
2445                 else if (!strncmp(this_opt, "viafb_bus_width=", 16))
2446                         strict_strtoul(this_opt + 16, 0,
2447                                 (unsigned long *)&viafb_bus_width);
2448                 else if (!strncmp(this_opt, "viafb_lcd_mode=", 15))
2449                         strict_strtoul(this_opt + 15, 0,
2450                                 (unsigned long *)&viafb_lcd_mode);
2451                 else if (!strncmp(this_opt, "viafb_video_dev=", 16))
2452                         viafb_video_dev = kstrdup(this_opt + 16, GFP_KERNEL);
2453                 else if (!strncmp(this_opt, "viafb_lcd_port=", 15))
2454                         viafb_lcd_port = kstrdup(this_opt + 15, GFP_KERNEL);
2455                 else if (!strncmp(this_opt, "viafb_dvi_port=", 15))
2456                         viafb_dvi_port = kstrdup(this_opt + 15, GFP_KERNEL);
2457         }
2458         return 0;
2459 }
2460 #endif
2461
2462 static int __init viafb_init(void)
2463 {
2464 #ifndef MODULE
2465         char *option = NULL;
2466         if (fb_get_options("viafb", &option))
2467                 return -ENODEV;
2468         viafb_setup(option);
2469 #endif
2470         printk(KERN_INFO
2471        "VIA Graphics Intergration Chipset framebuffer %d.%d initializing\n",
2472                VERSION_MAJOR, VERSION_MINOR);
2473         return via_pci_probe();
2474 }
2475
2476 static void __exit viafb_exit(void)
2477 {
2478         DEBUG_MSG(KERN_INFO "viafb_exit!\n");
2479         via_pci_remove();
2480 }
2481
2482 static struct fb_ops viafb_ops = {
2483         .owner = THIS_MODULE,
2484         .fb_open = viafb_open,
2485         .fb_release = viafb_release,
2486         .fb_check_var = viafb_check_var,
2487         .fb_set_par = viafb_set_par,
2488         .fb_setcolreg = viafb_setcolreg,
2489         .fb_pan_display = viafb_pan_display,
2490         .fb_blank = viafb_blank,
2491         .fb_fillrect = viafb_fillrect,
2492         .fb_copyarea = viafb_copyarea,
2493         .fb_imageblit = viafb_imageblit,
2494         .fb_cursor = viafb_cursor,
2495         .fb_ioctl = viafb_ioctl,
2496         .fb_sync = viafb_sync,
2497         .fb_setcmap = viafb_setcmap,
2498 };
2499
2500 module_init(viafb_init);
2501 module_exit(viafb_exit);
2502
2503 #ifdef MODULE
2504 module_param(viafb_memsize, int, 0);
2505
2506 module_param(viafb_mode, charp, 0);
2507 MODULE_PARM_DESC(viafb_mode, "Set resolution (default=640x480)");
2508
2509 module_param(viafb_mode1, charp, 0);
2510 MODULE_PARM_DESC(viafb_mode1, "Set resolution (default=640x480)");
2511
2512 module_param(viafb_bpp, int, 0);
2513 MODULE_PARM_DESC(viafb_bpp, "Set color depth (default=32bpp)");
2514
2515 module_param(viafb_bpp1, int, 0);
2516 MODULE_PARM_DESC(viafb_bpp1, "Set color depth (default=32bpp)");
2517
2518 module_param(viafb_refresh, int, 0);
2519 MODULE_PARM_DESC(viafb_refresh,
2520         "Set CRT viafb_refresh rate (default = 60)");
2521
2522 module_param(viafb_refresh1, int, 0);
2523 MODULE_PARM_DESC(viafb_refresh1,
2524         "Set CRT refresh rate (default = 60)");
2525
2526 module_param(viafb_lcd_panel_id, int, 0);
2527 MODULE_PARM_DESC(viafb_lcd_panel_id,
2528         "Set Flat Panel type(Default=1024x768)");
2529
2530 module_param(viafb_lcd_dsp_method, int, 0);
2531 MODULE_PARM_DESC(viafb_lcd_dsp_method,
2532         "Set Flat Panel display scaling method.(Default=Expandsion)");
2533
2534 module_param(viafb_SAMM_ON, int, 0);
2535 MODULE_PARM_DESC(viafb_SAMM_ON,
2536         "Turn on/off flag of SAMM(Default=OFF)");
2537
2538 module_param(viafb_accel, int, 0);
2539 MODULE_PARM_DESC(viafb_accel,
2540         "Set 2D Hardware Acceleration.(Default = OFF)");
2541
2542 module_param(viafb_active_dev, charp, 0);
2543 MODULE_PARM_DESC(viafb_active_dev, "Specify active devices.");
2544
2545 module_param(viafb_display_hardware_layout, int, 0);
2546 MODULE_PARM_DESC(viafb_display_hardware_layout,
2547         "Display Hardware Layout (LCD Only, DVI Only...,etc)");
2548
2549 module_param(viafb_second_size, int, 0);
2550 MODULE_PARM_DESC(viafb_second_size,
2551         "Set secondary device memory size");
2552
2553 module_param(viafb_dual_fb, int, 0);
2554 MODULE_PARM_DESC(viafb_dual_fb,
2555         "Turn on/off flag of dual framebuffer devices.(Default = OFF)");
2556
2557 module_param(viafb_platform_epia_dvi, int, 0);
2558 MODULE_PARM_DESC(viafb_platform_epia_dvi,
2559         "Turn on/off flag of DVI devices on EPIA board.(Default = OFF)");
2560
2561 module_param(viafb_device_lcd_dualedge, int, 0);
2562 MODULE_PARM_DESC(viafb_device_lcd_dualedge,
2563         "Turn on/off flag of dual edge panel.(Default = OFF)");
2564
2565 module_param(viafb_bus_width, int, 0);
2566 MODULE_PARM_DESC(viafb_bus_width,
2567         "Set bus width of panel.(Default = 12)");
2568
2569 module_param(viafb_lcd_mode, int, 0);
2570 MODULE_PARM_DESC(viafb_lcd_mode,
2571         "Set Flat Panel mode(Default=OPENLDI)");
2572
2573 module_param(viafb_video_dev, charp, 0);
2574 MODULE_PARM_DESC(viafb_video_dev, "Specify video devices.");
2575
2576 module_param(viafb_lcd_port, charp, 0);
2577 MODULE_PARM_DESC(viafb_lcd_port, "Specify LCD output port.");
2578
2579 module_param(viafb_dvi_port, charp, 0);
2580 MODULE_PARM_DESC(viafb_dvi_port, "Specify DVI output port.");
2581
2582 MODULE_LICENSE("GPL");
2583 #endif