Merge branch 'kvm-updates/2.6.28' of git://git.kernel.org/pub/scm/linux/kernel/git...
[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         struct viafb_ioctl_mode viamode;
550         struct viafb_ioctl_samm viasamm;
551         struct viafb_driver_version driver_version;
552         struct fb_var_screeninfo sec_var;
553         struct _panel_size_pos_info panel_pos_size_para;
554         u32 state_info = 0;
555         u32 viainfo_size = sizeof(struct viafb_ioctl_info);
556         u32 *viafb_gamma_table;
557         char driver_name[] = "viafb";
558
559         u32 __user *argp = (u32 __user *) arg;
560         u32 gpu32;
561         u32 video_dev_info = 0;
562         struct viafb_ioctl_setting viafb_setting = {};
563         struct device_t active_dev = {};
564
565         DEBUG_MSG(KERN_INFO "viafb_ioctl: 0x%X !!\n", cmd);
566
567         switch (cmd) {
568         case VIAFB_GET_CHIP_INFO:
569                 if (copy_to_user(argp, viaparinfo->chip_info,
570                                 sizeof(struct chip_information)))
571                         return -EFAULT;
572                 break;
573         case VIAFB_GET_INFO_SIZE:
574                 return put_user(viainfo_size, argp);
575         case VIAFB_GET_INFO:
576                 return viafb_ioctl_get_viafb_info(arg);
577         case VIAFB_HOTPLUG:
578                 return put_user(viafb_ioctl_hotplug(info->var.xres,
579                                               info->var.yres,
580                                               info->var.bits_per_pixel), argp);
581         case VIAFB_SET_HOTPLUG_FLAG:
582                 if (copy_from_user(&gpu32, argp, sizeof(gpu32)))
583                         return -EFAULT;
584                 viafb_hotplug = (gpu32) ? 1 : 0;
585                 break;
586         case VIAFB_GET_RESOLUTION:
587                 viamode.xres = (u32) viafb_hotplug_Xres;
588                 viamode.yres = (u32) viafb_hotplug_Yres;
589                 viamode.refresh = (u32) viafb_hotplug_refresh;
590                 viamode.bpp = (u32) viafb_hotplug_bpp;
591                 if (viafb_SAMM_ON == 1) {
592                         viamode.xres_sec = viafb_second_xres;
593                         viamode.yres_sec = viafb_second_yres;
594                         viamode.virtual_xres_sec = viafb_second_virtual_xres;
595                         viamode.virtual_yres_sec = viafb_second_virtual_yres;
596                         viamode.refresh_sec = viafb_refresh1;
597                         viamode.bpp_sec = viafb_bpp1;
598                 } else {
599                         viamode.xres_sec = 0;
600                         viamode.yres_sec = 0;
601                         viamode.virtual_xres_sec = 0;
602                         viamode.virtual_yres_sec = 0;
603                         viamode.refresh_sec = 0;
604                         viamode.bpp_sec = 0;
605                 }
606                 if (copy_to_user(argp, &viamode, sizeof(viamode)))
607                         return -EFAULT;
608                 break;
609         case VIAFB_GET_SAMM_INFO:
610                 viasamm.samm_status = viafb_SAMM_ON;
611
612                 if (viafb_SAMM_ON == 1) {
613                         if (viafb_dual_fb) {
614                                 viasamm.size_prim = viaparinfo->fbmem_free;
615                                 viasamm.size_sec = viaparinfo1->fbmem_free;
616                         } else {
617                                 if (viafb_second_size) {
618                                         viasamm.size_prim =
619                                             viaparinfo->fbmem_free -
620                                             viafb_second_size * 1024 * 1024;
621                                         viasamm.size_sec =
622                                             viafb_second_size * 1024 * 1024;
623                                 } else {
624                                         viasamm.size_prim =
625                                             viaparinfo->fbmem_free >> 1;
626                                         viasamm.size_sec =
627                                             (viaparinfo->fbmem_free >> 1);
628                                 }
629                         }
630                         viasamm.mem_base = viaparinfo->fbmem;
631                         viasamm.offset_sec = viafb_second_offset;
632                 } else {
633                         viasamm.size_prim =
634                             viaparinfo->memsize - viaparinfo->fbmem_used;
635                         viasamm.size_sec = 0;
636                         viasamm.mem_base = viaparinfo->fbmem;
637                         viasamm.offset_sec = 0;
638                 }
639
640                 if (copy_to_user(argp, &viasamm, sizeof(viasamm)))
641                         return -EFAULT;
642
643                 break;
644         case VIAFB_TURN_ON_OUTPUT_DEVICE:
645                 if (copy_from_user(&gpu32, argp, sizeof(gpu32)))
646                         return -EFAULT;
647                 if (gpu32 & CRT_Device)
648                         viafb_crt_enable();
649                 if (gpu32 & DVI_Device)
650                         viafb_dvi_enable();
651                 if (gpu32 & LCD_Device)
652                         viafb_lcd_enable();
653                 break;
654         case VIAFB_TURN_OFF_OUTPUT_DEVICE:
655                 if (copy_from_user(&gpu32, argp, sizeof(gpu32)))
656                         return -EFAULT;
657                 if (gpu32 & CRT_Device)
658                         viafb_crt_disable();
659                 if (gpu32 & DVI_Device)
660                         viafb_dvi_disable();
661                 if (gpu32 & LCD_Device)
662                         viafb_lcd_disable();
663                 break;
664         case VIAFB_SET_DEVICE:
665                 if (copy_from_user(&active_dev, (void *)argp,
666                         sizeof(active_dev)))
667                         return -EFAULT;
668                 viafb_set_device(active_dev);
669                 viafb_set_par(info);
670                 break;
671         case VIAFB_GET_DEVICE:
672                 active_dev.crt = viafb_CRT_ON;
673                 active_dev.dvi = viafb_DVI_ON;
674                 active_dev.lcd = viafb_LCD_ON;
675                 active_dev.samm = viafb_SAMM_ON;
676                 active_dev.primary_dev = viafb_primary_dev;
677
678                 active_dev.lcd_dsp_cent = viafb_lcd_dsp_method;
679                 active_dev.lcd_panel_id = viafb_lcd_panel_id;
680                 active_dev.lcd_mode = viafb_lcd_mode;
681
682                 active_dev.xres = viafb_hotplug_Xres;
683                 active_dev.yres = viafb_hotplug_Yres;
684
685                 active_dev.xres1 = viafb_second_xres;
686                 active_dev.yres1 = viafb_second_yres;
687
688                 active_dev.bpp = viafb_bpp;
689                 active_dev.bpp1 = viafb_bpp1;
690                 active_dev.refresh = viafb_refresh;
691                 active_dev.refresh1 = viafb_refresh1;
692
693                 active_dev.epia_dvi = viafb_platform_epia_dvi;
694                 active_dev.lcd_dual_edge = viafb_device_lcd_dualedge;
695                 active_dev.bus_width = viafb_bus_width;
696
697                 if (copy_to_user(argp, &active_dev, sizeof(active_dev)))
698                         return -EFAULT;
699                 break;
700
701         case VIAFB_GET_DRIVER_VERSION:
702                 driver_version.iMajorNum = VERSION_MAJOR;
703                 driver_version.iKernelNum = VERSION_KERNEL;
704                 driver_version.iOSNum = VERSION_OS;
705                 driver_version.iMinorNum = VERSION_MINOR;
706
707                 if (copy_to_user(argp, &driver_version,
708                         sizeof(driver_version)))
709                         return -EFAULT;
710
711                 break;
712
713         case VIAFB_SET_DEVICE_INFO:
714                 if (copy_from_user(&viafb_setting,
715                         argp, sizeof(viafb_setting)))
716                         return -EFAULT;
717                 if (apply_device_setting(viafb_setting, info) < 0)
718                         return -EINVAL;
719
720                 break;
721
722         case VIAFB_SET_SECOND_MODE:
723                 if (copy_from_user(&sec_var, argp, sizeof(sec_var)))
724                         return -EFAULT;
725                 apply_second_mode_setting(&sec_var);
726                 break;
727
728         case VIAFB_GET_DEVICE_INFO:
729
730                 retrieve_device_setting(&viafb_setting);
731
732                 if (copy_to_user(argp, &viafb_setting, sizeof(viafb_setting)))
733                         return -EFAULT;
734
735                 break;
736
737         case VIAFB_GET_DEVICE_SUPPORT:
738                 viafb_get_device_support_state(&state_info);
739                 if (put_user(state_info, argp))
740                         return -EFAULT;
741                 break;
742
743         case VIAFB_GET_DEVICE_CONNECT:
744                 viafb_get_device_connect_state(&state_info);
745                 if (put_user(state_info, argp))
746                         return -EFAULT;
747                 break;
748
749         case VIAFB_GET_PANEL_SUPPORT_EXPAND:
750                 state_info =
751                     viafb_lcd_get_support_expand_state(info->var.xres,
752                                                  info->var.yres);
753                 if (put_user(state_info, argp))
754                         return -EFAULT;
755                 break;
756
757         case VIAFB_GET_DRIVER_NAME:
758                 if (copy_to_user(argp, driver_name, sizeof(driver_name)))
759                         return -EFAULT;
760                 break;
761
762         case VIAFB_SET_GAMMA_LUT:
763                 viafb_gamma_table = kmalloc(256 * sizeof(u32), GFP_KERNEL);
764                 if (!viafb_gamma_table)
765                         return -ENOMEM;
766                 if (copy_from_user(viafb_gamma_table, argp,
767                                 sizeof(viafb_gamma_table))) {
768                         kfree(viafb_gamma_table);
769                         return -EFAULT;
770                 }
771                 viafb_set_gamma_table(viafb_bpp, viafb_gamma_table);
772                 kfree(viafb_gamma_table);
773                 break;
774
775         case VIAFB_GET_GAMMA_LUT:
776                 viafb_gamma_table = kmalloc(256 * sizeof(u32), GFP_KERNEL);
777                 if (!viafb_gamma_table)
778                         return -ENOMEM;
779                 viafb_get_gamma_table(viafb_gamma_table);
780                 if (copy_to_user(argp, viafb_gamma_table,
781                         sizeof(viafb_gamma_table))) {
782                         kfree(viafb_gamma_table);
783                         return -EFAULT;
784                 }
785                 kfree(viafb_gamma_table);
786                 break;
787
788         case VIAFB_GET_GAMMA_SUPPORT_STATE:
789                 viafb_get_gamma_support_state(viafb_bpp, &state_info);
790                 if (put_user(state_info, argp))
791                         return -EFAULT;
792                 break;
793         case VIAFB_SET_VIDEO_DEVICE:
794                 get_user(video_dev_info, argp);
795                 viafb_set_video_device(video_dev_info);
796                 break;
797         case VIAFB_GET_VIDEO_DEVICE:
798                 viafb_get_video_device(&video_dev_info);
799                 if (put_user(video_dev_info, argp))
800                         return -EFAULT;
801                 break;
802         case VIAFB_SYNC_SURFACE:
803                 DEBUG_MSG(KERN_INFO "lobo VIAFB_SYNC_SURFACE\n");
804                 break;
805         case VIAFB_GET_DRIVER_CAPS:
806                 break;
807
808         case VIAFB_GET_PANEL_MAX_SIZE:
809                 if (copy_from_user
810                     (&panel_pos_size_para, argp, sizeof(panel_pos_size_para)))
811                         return -EFAULT;
812                 panel_pos_size_para.x = panel_pos_size_para.y = 0;
813                 if (copy_to_user(argp, &panel_pos_size_para,
814                      sizeof(panel_pos_size_para)))
815                         return -EFAULT;
816                 break;
817         case VIAFB_GET_PANEL_MAX_POSITION:
818                 if (copy_from_user
819                     (&panel_pos_size_para, argp, sizeof(panel_pos_size_para)))
820                         return -EFAULT;
821                 panel_pos_size_para.x = panel_pos_size_para.y = 0;
822                 if (copy_to_user(argp, &panel_pos_size_para,
823                      sizeof(panel_pos_size_para)))
824                         return -EFAULT;
825                 break;
826
827         case VIAFB_GET_PANEL_POSITION:
828                 if (copy_from_user
829                     (&panel_pos_size_para, argp, sizeof(panel_pos_size_para)))
830                         return -EFAULT;
831                 panel_pos_size_para.x = panel_pos_size_para.y = 0;
832                 if (copy_to_user(argp, &panel_pos_size_para,
833                      sizeof(panel_pos_size_para)))
834                         return -EFAULT;
835                 break;
836         case VIAFB_GET_PANEL_SIZE:
837                 if (copy_from_user
838                     (&panel_pos_size_para, argp, sizeof(panel_pos_size_para)))
839                         return -EFAULT;
840                 panel_pos_size_para.x = panel_pos_size_para.y = 0;
841                 if (copy_to_user(argp, &panel_pos_size_para,
842                      sizeof(panel_pos_size_para)))
843                         return -EFAULT;
844                 break;
845
846         case VIAFB_SET_PANEL_POSITION:
847                 if (copy_from_user
848                     (&panel_pos_size_para, argp, sizeof(panel_pos_size_para)))
849                         return -EFAULT;
850                 break;
851         case VIAFB_SET_PANEL_SIZE:
852                 if (copy_from_user
853                     (&panel_pos_size_para, argp, sizeof(panel_pos_size_para)))
854                         return -EFAULT;
855                 break;
856
857         default:
858                 return -EINVAL;
859         }
860
861         return 0;
862 }
863
864 static void viafb_fillrect(struct fb_info *info,
865         const struct fb_fillrect *rect)
866 {
867         u32 col = 0, rop = 0;
868         int pitch;
869
870         if (!viafb_accel)
871                 return cfb_fillrect(info, rect);
872
873         if (!rect->width || !rect->height)
874                 return;
875
876         switch (rect->rop) {
877         case ROP_XOR:
878                 rop = 0x5A;
879                 break;
880         case ROP_COPY:
881         default:
882                 rop = 0xF0;
883                 break;
884         }
885
886         switch (info->var.bits_per_pixel) {
887         case 8:
888                 col = rect->color;
889                 break;
890         case 16:
891                 col = ((u32 *) (info->pseudo_palette))[rect->color];
892                 break;
893         case 32:
894                 col = ((u32 *) (info->pseudo_palette))[rect->color];
895                 break;
896         }
897
898         /* BitBlt Source Address */
899         writel(0x0, viaparinfo->io_virt + VIA_REG_SRCPOS);
900         /* Source Base Address */
901         writel(0x0, viaparinfo->io_virt + VIA_REG_SRCBASE);
902         /* Destination Base Address */
903         writel(((unsigned long) (info->screen_base) -
904                    (unsigned long) viafb_FB_MM) >> 3,
905                    viaparinfo->io_virt + VIA_REG_DSTBASE);
906         /* Pitch */
907         pitch = (info->var.xres_virtual + 7) & ~7;
908         writel(VIA_PITCH_ENABLE |
909                    (((pitch *
910                       info->var.bits_per_pixel >> 3) >> 3) |
911                       (((pitch * info->
912                       var.bits_per_pixel >> 3) >> 3) << 16)),
913                       viaparinfo->io_virt + VIA_REG_PITCH);
914         /* BitBlt Destination Address */
915         writel(((rect->dy << 16) | rect->dx),
916                 viaparinfo->io_virt + VIA_REG_DSTPOS);
917         /* Dimension: width & height */
918         writel((((rect->height - 1) << 16) | (rect->width - 1)),
919                 viaparinfo->io_virt + VIA_REG_DIMENSION);
920         /* Forground color or Destination color */
921         writel(col, viaparinfo->io_virt + VIA_REG_FGCOLOR);
922         /* GE Command */
923         writel((0x01 | 0x2000 | (rop << 24)),
924                 viaparinfo->io_virt + VIA_REG_GECMD);
925
926 }
927
928 static void viafb_copyarea(struct fb_info *info,
929         const struct fb_copyarea *area)
930 {
931         u32 dy = area->dy, sy = area->sy, direction = 0x0;
932         u32 sx = area->sx, dx = area->dx, width = area->width;
933         int pitch;
934
935         DEBUG_MSG(KERN_INFO "viafb_copyarea!!\n");
936
937         if (!viafb_accel)
938                 return cfb_copyarea(info, area);
939
940         if (!area->width || !area->height)
941                 return;
942
943         if (sy < dy) {
944                 dy += area->height - 1;
945                 sy += area->height - 1;
946                 direction |= 0x4000;
947         }
948
949         if (sx < dx) {
950                 dx += width - 1;
951                 sx += width - 1;
952                 direction |= 0x8000;
953         }
954
955         /* Source Base Address */
956         writel(((unsigned long) (info->screen_base) -
957                    (unsigned long) viafb_FB_MM) >> 3,
958                    viaparinfo->io_virt + VIA_REG_SRCBASE);
959         /* Destination Base Address */
960         writel(((unsigned long) (info->screen_base) -
961                    (unsigned long) viafb_FB_MM) >> 3,
962                    viaparinfo->io_virt + VIA_REG_DSTBASE);
963         /* Pitch */
964         pitch = (info->var.xres_virtual + 7) & ~7;
965         /* VIA_PITCH_ENABLE can be omitted now. */
966         writel(VIA_PITCH_ENABLE |
967                    (((pitch *
968                       info->var.bits_per_pixel >> 3) >> 3) | (((pitch *
969                                                                 info->var.
970                                                                 bits_per_pixel
971                                                                 >> 3) >> 3)
972                                                               << 16)),
973                                 viaparinfo->io_virt + VIA_REG_PITCH);
974         /* BitBlt Source Address */
975         writel(((sy << 16) | sx), viaparinfo->io_virt + VIA_REG_SRCPOS);
976         /* BitBlt Destination Address */
977         writel(((dy << 16) | dx), viaparinfo->io_virt + VIA_REG_DSTPOS);
978         /* Dimension: width & height */
979         writel((((area->height - 1) << 16) | (area->width - 1)),
980                    viaparinfo->io_virt + VIA_REG_DIMENSION);
981         /* GE Command */
982         writel((0x01 | direction | (0xCC << 24)),
983                 viaparinfo->io_virt + VIA_REG_GECMD);
984
985 }
986
987 static void viafb_imageblit(struct fb_info *info,
988         const struct fb_image *image)
989 {
990         u32 size, bg_col = 0, fg_col = 0, *udata;
991         int i;
992         int pitch;
993
994         if (!viafb_accel)
995                 return cfb_imageblit(info, image);
996
997         udata = (u32 *) image->data;
998
999         switch (info->var.bits_per_pixel) {
1000         case 8:
1001                 bg_col = image->bg_color;
1002                 fg_col = image->fg_color;
1003                 break;
1004         case 16:
1005                 bg_col = ((u32 *) (info->pseudo_palette))[image->bg_color];
1006                 fg_col = ((u32 *) (info->pseudo_palette))[image->fg_color];
1007                 break;
1008         case 32:
1009                 bg_col = ((u32 *) (info->pseudo_palette))[image->bg_color];
1010                 fg_col = ((u32 *) (info->pseudo_palette))[image->fg_color];
1011                 break;
1012         }
1013         size = image->width * image->height;
1014
1015         /* Source Base Address */
1016         writel(0x0, viaparinfo->io_virt + VIA_REG_SRCBASE);
1017         /* Destination Base Address */
1018         writel(((unsigned long) (info->screen_base) -
1019                    (unsigned long) viafb_FB_MM) >> 3,
1020                    viaparinfo->io_virt + VIA_REG_DSTBASE);
1021         /* Pitch */
1022         pitch = (info->var.xres_virtual + 7) & ~7;
1023         writel(VIA_PITCH_ENABLE |
1024                    (((pitch *
1025                       info->var.bits_per_pixel >> 3) >> 3) | (((pitch *
1026                                                                 info->var.
1027                                                                 bits_per_pixel
1028                                                                 >> 3) >> 3)
1029                                                               << 16)),
1030                                 viaparinfo->io_virt + VIA_REG_PITCH);
1031         /* BitBlt Source Address */
1032         writel(0x0, viaparinfo->io_virt + VIA_REG_SRCPOS);
1033         /* BitBlt Destination Address */
1034         writel(((image->dy << 16) | image->dx),
1035                 viaparinfo->io_virt + VIA_REG_DSTPOS);
1036         /* Dimension: width & height */
1037         writel((((image->height - 1) << 16) | (image->width - 1)),
1038                    viaparinfo->io_virt + VIA_REG_DIMENSION);
1039         /* fb color */
1040         writel(fg_col, viaparinfo->io_virt + VIA_REG_FGCOLOR);
1041         /* bg color */
1042         writel(bg_col, viaparinfo->io_virt + VIA_REG_BGCOLOR);
1043         /* GE Command */
1044         writel(0xCC020142, viaparinfo->io_virt + VIA_REG_GECMD);
1045
1046         for (i = 0; i < size / 4; i++) {
1047                 writel(*udata, viaparinfo->io_virt + VIA_MMIO_BLTBASE);
1048                 udata++;
1049         }
1050
1051 }
1052
1053 static int viafb_cursor(struct fb_info *info, struct fb_cursor *cursor)
1054 {
1055         u8 data[CURSOR_SIZE / 8];
1056         u32 data_bak[CURSOR_SIZE / 32];
1057         u32 temp, xx, yy, bg_col = 0, fg_col = 0;
1058         int size, i, j = 0;
1059         static int hw_cursor;
1060         struct viafb_par *p_viafb_par;
1061
1062         if (viafb_accel)
1063                 hw_cursor = 1;
1064
1065         if (!viafb_accel) {
1066                 if (hw_cursor) {
1067                         viafb_show_hw_cursor(info, HW_Cursor_OFF);
1068                         hw_cursor = 0;
1069                 }
1070                 return -ENODEV;
1071         }
1072
1073         if ((((struct viafb_par *)(info->par))->iga_path == IGA2)
1074             && (viaparinfo->chip_info->gfx_chip_name == UNICHROME_CLE266))
1075                 return -ENODEV;
1076
1077         /* When duoview and using lcd , use soft cursor */
1078         if (viafb_LCD_ON || ((struct viafb_par *)(info->par))->duoview)
1079                 return -ENODEV;
1080
1081         viafb_show_hw_cursor(info, HW_Cursor_OFF);
1082         viacursor = *cursor;
1083
1084         if (cursor->set & FB_CUR_SETHOT) {
1085                 viacursor.hot = cursor->hot;
1086                 temp = ((viacursor.hot.x) << 16) + viacursor.hot.y;
1087                 writel(temp, viaparinfo->io_virt + VIA_REG_CURSOR_ORG);
1088         }
1089
1090         if (cursor->set & FB_CUR_SETPOS) {
1091                 viacursor.image.dx = cursor->image.dx;
1092                 viacursor.image.dy = cursor->image.dy;
1093                 yy = cursor->image.dy - info->var.yoffset;
1094                 xx = cursor->image.dx - info->var.xoffset;
1095                 temp = yy & 0xFFFF;
1096                 temp |= (xx << 16);
1097                 writel(temp, viaparinfo->io_virt + VIA_REG_CURSOR_POS);
1098         }
1099
1100         if (cursor->set & FB_CUR_SETSIZE) {
1101                 temp = readl(viaparinfo->io_virt + VIA_REG_CURSOR_MODE);
1102
1103                 if ((cursor->image.width <= 32)
1104                     && (cursor->image.height <= 32)) {
1105                         MAX_CURS = 32;
1106                         temp |= 0x2;
1107                 } else if ((cursor->image.width <= 64)
1108                            && (cursor->image.height <= 64)) {
1109                         MAX_CURS = 64;
1110                         temp &= 0xFFFFFFFD;
1111                 } else {
1112                         DEBUG_MSG(KERN_INFO
1113                         "The cursor image is biger than 64x64 bits...\n");
1114                         return -ENXIO;
1115                 }
1116                 writel(temp, viaparinfo->io_virt + VIA_REG_CURSOR_MODE);
1117
1118                 viacursor.image.height = cursor->image.height;
1119                 viacursor.image.width = cursor->image.width;
1120         }
1121
1122         if (cursor->set & FB_CUR_SETCMAP) {
1123                 viacursor.image.fg_color = cursor->image.fg_color;
1124                 viacursor.image.bg_color = cursor->image.bg_color;
1125
1126                 switch (info->var.bits_per_pixel) {
1127                 case 8:
1128                 case 16:
1129                 case 32:
1130                         bg_col =
1131                             (0xFF << 24) |
1132                             (((info->cmap.red)[viacursor.image.bg_color] &
1133                             0xFF00) << 8) |
1134                             ((info->cmap.green)[viacursor.image.bg_color] &
1135                             0xFF00) |
1136                             (((info->cmap.blue)[viacursor.image.bg_color] &
1137                             0xFF00) >> 8);
1138                         fg_col =
1139                             (0xFF << 24) |
1140                             (((info->cmap.red)[viacursor.image.fg_color] &
1141                             0xFF00) << 8) |
1142                             ((info->cmap.green)[viacursor.image.fg_color] &
1143                             0xFF00) |
1144                             (((info->cmap.blue)[viacursor.image.fg_color] &
1145                             0xFF00) >> 8);
1146                         break;
1147                 default:
1148                         return 0;
1149                 }
1150
1151                 /* This is indeed a patch for VT3324/VT3353 */
1152                 if (!info->par)
1153                         return 0;
1154                 p_viafb_par = (struct viafb_par *)info->par;
1155
1156                 if ((p_viafb_par->chip_info->gfx_chip_name ==
1157                         UNICHROME_CX700) ||
1158                         ((p_viafb_par->chip_info->gfx_chip_name ==
1159                         UNICHROME_VX800))) {
1160                         bg_col =
1161                             (((info->cmap.red)[viacursor.image.bg_color] &
1162                             0xFFC0) << 14) |
1163                             (((info->cmap.green)[viacursor.image.bg_color] &
1164                             0xFFC0) << 4) |
1165                             (((info->cmap.blue)[viacursor.image.bg_color] &
1166                             0xFFC0) >> 6);
1167                         fg_col =
1168                             (((info->cmap.red)[viacursor.image.fg_color] &
1169                             0xFFC0) << 14) |
1170                             (((info->cmap.green)[viacursor.image.fg_color] &
1171                             0xFFC0) << 4) |
1172                             (((info->cmap.blue)[viacursor.image.fg_color] &
1173                             0xFFC0) >> 6);
1174                 }
1175
1176                 writel(bg_col, viaparinfo->io_virt + VIA_REG_CURSOR_BG);
1177                 writel(fg_col, viaparinfo->io_virt + VIA_REG_CURSOR_FG);
1178         }
1179
1180         if (cursor->set & FB_CUR_SETSHAPE) {
1181                 size =
1182                     ((viacursor.image.width + 7) >> 3) *
1183                     viacursor.image.height;
1184
1185                 if (MAX_CURS == 32) {
1186                         for (i = 0; i < (CURSOR_SIZE / 32); i++) {
1187                                 data_bak[i] = 0x0;
1188                                 data_bak[i + 1] = 0xFFFFFFFF;
1189                                 i += 1;
1190                         }
1191                 } else if (MAX_CURS == 64) {
1192                         for (i = 0; i < (CURSOR_SIZE / 32); i++) {
1193                                 data_bak[i] = 0x0;
1194                                 data_bak[i + 1] = 0x0;
1195                                 data_bak[i + 2] = 0xFFFFFFFF;
1196                                 data_bak[i + 3] = 0xFFFFFFFF;
1197                                 i += 3;
1198                         }
1199                 }
1200
1201                 switch (viacursor.rop) {
1202                 case ROP_XOR:
1203                         for (i = 0; i < size; i++)
1204                                 data[i] = viacursor.mask[i];
1205                         break;
1206                 case ROP_COPY:
1207
1208                         for (i = 0; i < size; i++)
1209                                 data[i] = viacursor.mask[i];
1210                         break;
1211                 default:
1212                         break;
1213                 }
1214
1215                 if (MAX_CURS == 32) {
1216                         for (i = 0; i < size; i++) {
1217                                 data_bak[j] = (u32) data[i];
1218                                 data_bak[j + 1] = ~data_bak[j];
1219                                 j += 2;
1220                         }
1221                 } else if (MAX_CURS == 64) {
1222                         for (i = 0; i < size; i++) {
1223                                 data_bak[j] = (u32) data[i];
1224                                 data_bak[j + 1] = 0x0;
1225                                 data_bak[j + 2] = ~data_bak[j];
1226                                 data_bak[j + 3] = ~data_bak[j + 1];
1227                                 j += 4;
1228                         }
1229                 }
1230
1231                 memcpy(((struct viafb_par *)(info->par))->fbmem_virt +
1232                        ((struct viafb_par *)(info->par))->cursor_start,
1233                        data_bak, CURSOR_SIZE);
1234         }
1235
1236         if (viacursor.enable)
1237                 viafb_show_hw_cursor(info, HW_Cursor_ON);
1238
1239         return 0;
1240 }
1241
1242 static int viafb_sync(struct fb_info *info)
1243 {
1244         if (viafb_accel)
1245                 viafb_wait_engine_idle();
1246         return 0;
1247 }
1248
1249 int viafb_get_mode_index(int hres, int vres, int flag)
1250 {
1251         u32 i;
1252         DEBUG_MSG(KERN_INFO "viafb_get_mode_index!\n");
1253
1254         for (i = 0; viafb_modentry[i].mode_index != VIA_RES_INVALID; i++)
1255                 if (viafb_modentry[i].xres == hres &&
1256                         viafb_modentry[i].yres == vres)
1257                         break;
1258
1259         viafb_resMode = viafb_modentry[i].mode_index;
1260         if (flag)
1261                 viafb_mode1 = viafb_modentry[i].mode_res;
1262         else
1263                 viafb_mode = viafb_modentry[i].mode_res;
1264
1265         return viafb_resMode;
1266 }
1267
1268 static void check_available_device_to_enable(int device_id)
1269 {
1270         int device_num = 0;
1271
1272         /* Initialize: */
1273         viafb_CRT_ON = STATE_OFF;
1274         viafb_DVI_ON = STATE_OFF;
1275         viafb_LCD_ON = STATE_OFF;
1276         viafb_LCD2_ON = STATE_OFF;
1277         viafb_DeviceStatus = None_Device;
1278
1279         if ((device_id & CRT_Device) && (device_num < MAX_ACTIVE_DEV_NUM)) {
1280                 viafb_CRT_ON = STATE_ON;
1281                 device_num++;
1282                 viafb_DeviceStatus |= CRT_Device;
1283         }
1284
1285         if ((device_id & DVI_Device) && (device_num < MAX_ACTIVE_DEV_NUM)) {
1286                 viafb_DVI_ON = STATE_ON;
1287                 device_num++;
1288                 viafb_DeviceStatus |= DVI_Device;
1289         }
1290
1291         if ((device_id & LCD_Device) && (device_num < MAX_ACTIVE_DEV_NUM)) {
1292                 viafb_LCD_ON = STATE_ON;
1293                 device_num++;
1294                 viafb_DeviceStatus |= LCD_Device;
1295         }
1296
1297         if ((device_id & LCD2_Device) && (device_num < MAX_ACTIVE_DEV_NUM)) {
1298                 viafb_LCD2_ON = STATE_ON;
1299                 device_num++;
1300                 viafb_DeviceStatus |= LCD2_Device;
1301         }
1302
1303         if (viafb_DeviceStatus == None_Device) {
1304                 /* Use CRT as default active device: */
1305                 viafb_CRT_ON = STATE_ON;
1306                 viafb_DeviceStatus = CRT_Device;
1307         }
1308         DEBUG_MSG(KERN_INFO "Device Status:%x", viafb_DeviceStatus);
1309 }
1310
1311 static void viafb_set_device(struct device_t active_dev)
1312 {
1313         /* Check available device to enable: */
1314         int device_id = None_Device;
1315         if (active_dev.crt)
1316                 device_id |= CRT_Device;
1317         if (active_dev.dvi)
1318                 device_id |= DVI_Device;
1319         if (active_dev.lcd)
1320                 device_id |= LCD_Device;
1321
1322         check_available_device_to_enable(device_id);
1323
1324         /* Check property of LCD: */
1325         if (viafb_LCD_ON) {
1326                 if (active_dev.lcd_dsp_cent) {
1327                         viaparinfo->lvds_setting_info->display_method =
1328                                 viafb_lcd_dsp_method = LCD_CENTERING;
1329                 } else {
1330                         viaparinfo->lvds_setting_info->display_method =
1331                                 viafb_lcd_dsp_method = LCD_EXPANDSION;
1332                 }
1333
1334                 if (active_dev.lcd_mode == LCD_SPWG) {
1335                         viaparinfo->lvds_setting_info->lcd_mode =
1336                                 viafb_lcd_mode = LCD_SPWG;
1337                 } else {
1338                         viaparinfo->lvds_setting_info->lcd_mode =
1339                                 viafb_lcd_mode = LCD_OPENLDI;
1340                 }
1341
1342                 if (active_dev.lcd_panel_id <= LCD_PANEL_ID_MAXIMUM) {
1343                         viafb_lcd_panel_id = active_dev.lcd_panel_id;
1344                         viafb_init_lcd_size();
1345                 }
1346         }
1347
1348         /* Check property of mode: */
1349         if (!active_dev.xres1)
1350                 viafb_second_xres = 640;
1351         else
1352                 viafb_second_xres = active_dev.xres1;
1353         if (!active_dev.yres1)
1354                 viafb_second_yres = 480;
1355         else
1356                 viafb_second_yres = active_dev.yres1;
1357         if (active_dev.bpp != 0)
1358                 viafb_bpp = active_dev.bpp;
1359         if (active_dev.bpp1 != 0)
1360                 viafb_bpp1 = active_dev.bpp1;
1361         if (active_dev.refresh != 0)
1362                 viafb_refresh = active_dev.refresh;
1363         if (active_dev.refresh1 != 0)
1364                 viafb_refresh1 = active_dev.refresh1;
1365         if ((active_dev.samm == STATE_OFF) || (active_dev.samm == STATE_ON))
1366                 viafb_SAMM_ON = active_dev.samm;
1367         viafb_primary_dev = active_dev.primary_dev;
1368
1369         viafb_set_start_addr();
1370         viafb_set_iga_path();
1371 }
1372
1373 static void viafb_set_video_device(u32 video_dev_info)
1374 {
1375         viaparinfo->video_on_crt = STATE_OFF;
1376         viaparinfo->video_on_dvi = STATE_OFF;
1377         viaparinfo->video_on_lcd = STATE_OFF;
1378
1379         /* Check available device to enable: */
1380         if ((video_dev_info & CRT_Device) == CRT_Device)
1381                 viaparinfo->video_on_crt = STATE_ON;
1382         else if ((video_dev_info & DVI_Device) == DVI_Device)
1383                 viaparinfo->video_on_dvi = STATE_ON;
1384         else if ((video_dev_info & LCD_Device) == LCD_Device)
1385                 viaparinfo->video_on_lcd = STATE_ON;
1386 }
1387
1388 static void viafb_get_video_device(u32 *video_dev_info)
1389 {
1390         *video_dev_info = None_Device;
1391         if (viaparinfo->video_on_crt == STATE_ON)
1392                 *video_dev_info |= CRT_Device;
1393         else if (viaparinfo->video_on_dvi == STATE_ON)
1394                 *video_dev_info |= DVI_Device;
1395         else if (viaparinfo->video_on_lcd == STATE_ON)
1396                 *video_dev_info |= LCD_Device;
1397 }
1398
1399 static int get_primary_device(void)
1400 {
1401         int primary_device = 0;
1402         /* Rule: device on iga1 path are the primary device. */
1403         if (viafb_SAMM_ON) {
1404                 if (viafb_CRT_ON) {
1405                         if (viaparinfo->crt_setting_info->iga_path == IGA1) {
1406                                 DEBUG_MSG(KERN_INFO "CRT IGA Path:%d\n",
1407                                         viaparinfo->
1408                                         crt_setting_info->iga_path);
1409                                 primary_device = CRT_Device;
1410                         }
1411                 }
1412                 if (viafb_DVI_ON) {
1413                         if (viaparinfo->tmds_setting_info->iga_path == IGA1) {
1414                                 DEBUG_MSG(KERN_INFO "DVI IGA Path:%d\n",
1415                                         viaparinfo->
1416                                         tmds_setting_info->iga_path);
1417                                 primary_device = DVI_Device;
1418                         }
1419                 }
1420                 if (viafb_LCD_ON) {
1421                         if (viaparinfo->lvds_setting_info->iga_path == IGA1) {
1422                                 DEBUG_MSG(KERN_INFO "LCD IGA Path:%d\n",
1423                                         viaparinfo->
1424                                         lvds_setting_info->iga_path);
1425                                 primary_device = LCD_Device;
1426                         }
1427                 }
1428                 if (viafb_LCD2_ON) {
1429                         if (viaparinfo->lvds_setting_info2->iga_path == IGA1) {
1430                                 DEBUG_MSG(KERN_INFO "LCD2 IGA Path:%d\n",
1431                                         viaparinfo->
1432                                         lvds_setting_info2->iga_path);
1433                                 primary_device = LCD2_Device;
1434                         }
1435                 }
1436         }
1437         return primary_device;
1438 }
1439
1440 static u8 is_duoview(void)
1441 {
1442         if (0 == viafb_SAMM_ON) {
1443                 if (viafb_LCD_ON + viafb_LCD2_ON +
1444                         viafb_DVI_ON + viafb_CRT_ON == 2)
1445                         return true;
1446                 return false;
1447         } else {
1448                 return false;
1449         }
1450 }
1451
1452 static void apply_second_mode_setting(struct fb_var_screeninfo
1453         *sec_var)
1454 {
1455         u32 htotal, vtotal, long_refresh;
1456
1457         htotal = sec_var->xres + sec_var->left_margin +
1458                 sec_var->right_margin + sec_var->hsync_len;
1459         vtotal = sec_var->yres + sec_var->upper_margin +
1460                 sec_var->lower_margin + sec_var->vsync_len;
1461         if ((sec_var->xres_virtual * (sec_var->bits_per_pixel >> 3)) & 0x1F) {
1462                 /*Is 32 bytes alignment? */
1463                 /*32 pixel alignment */
1464                 sec_var->xres_virtual = (sec_var->xres_virtual + 31) & ~31;
1465         }
1466
1467         htotal = sec_var->xres + sec_var->left_margin +
1468                 sec_var->right_margin + sec_var->hsync_len;
1469         vtotal = sec_var->yres + sec_var->upper_margin +
1470                 sec_var->lower_margin + sec_var->vsync_len;
1471         long_refresh = 1000000000UL / sec_var->pixclock * 1000;
1472         long_refresh /= (htotal * vtotal);
1473
1474         viafb_second_xres = sec_var->xres;
1475         viafb_second_yres = sec_var->yres;
1476         viafb_second_virtual_xres = sec_var->xres_virtual;
1477         viafb_second_virtual_yres = sec_var->yres_virtual;
1478         viafb_bpp1 = sec_var->bits_per_pixel;
1479         viafb_refresh1 = viafb_get_refresh(sec_var->xres, sec_var->yres,
1480                 long_refresh);
1481 }
1482
1483 static int apply_device_setting(struct viafb_ioctl_setting setting_info,
1484         struct fb_info *info)
1485 {
1486         int need_set_mode = 0;
1487         DEBUG_MSG(KERN_INFO "apply_device_setting\n");
1488
1489         if (setting_info.device_flag) {
1490                 need_set_mode = 1;
1491                 check_available_device_to_enable(setting_info.device_status);
1492         }
1493
1494         /* Unlock LCD's operation according to LCD flag
1495            and check if the setting value is valid. */
1496         /* If the value is valid, apply the new setting value to the device. */
1497         if (viafb_LCD_ON) {
1498                 if (setting_info.lcd_operation_flag & OP_LCD_CENTERING) {
1499                         need_set_mode = 1;
1500                         if (setting_info.lcd_attributes.display_center) {
1501                                 /* Centering */
1502                                 viaparinfo->lvds_setting_info->display_method =
1503                                     LCD_CENTERING;
1504                                 viafb_lcd_dsp_method = LCD_CENTERING;
1505                                 viaparinfo->lvds_setting_info2->display_method =
1506                                     viafb_lcd_dsp_method = LCD_CENTERING;
1507                         } else {
1508                                 /* expandsion */
1509                                 viaparinfo->lvds_setting_info->display_method =
1510                                     LCD_EXPANDSION;
1511                                 viafb_lcd_dsp_method = LCD_EXPANDSION;
1512                                 viaparinfo->lvds_setting_info2->display_method =
1513                                     LCD_EXPANDSION;
1514                                 viafb_lcd_dsp_method = LCD_EXPANDSION;
1515                         }
1516                 }
1517
1518                 if (setting_info.lcd_operation_flag & OP_LCD_MODE) {
1519                         need_set_mode = 1;
1520                         if (setting_info.lcd_attributes.lcd_mode ==
1521                                 LCD_SPWG) {
1522                                 viaparinfo->lvds_setting_info->lcd_mode =
1523                                         viafb_lcd_mode = LCD_SPWG;
1524                         } else {
1525                                 viaparinfo->lvds_setting_info->lcd_mode =
1526                                         viafb_lcd_mode = LCD_OPENLDI;
1527                         }
1528                         viaparinfo->lvds_setting_info2->lcd_mode =
1529                             viaparinfo->lvds_setting_info->lcd_mode;
1530                 }
1531
1532                 if (setting_info.lcd_operation_flag & OP_LCD_PANEL_ID) {
1533                         need_set_mode = 1;
1534                         if (setting_info.lcd_attributes.panel_id <=
1535                             LCD_PANEL_ID_MAXIMUM) {
1536                                 viafb_lcd_panel_id =
1537                                     setting_info.lcd_attributes.panel_id;
1538                                 viafb_init_lcd_size();
1539                         }
1540                 }
1541         }
1542
1543         if (0 != (setting_info.samm_status & OP_SAMM)) {
1544                 setting_info.samm_status =
1545                     setting_info.samm_status & (~OP_SAMM);
1546                 if (setting_info.samm_status == 0
1547                     || setting_info.samm_status == 1) {
1548                         viafb_SAMM_ON = setting_info.samm_status;
1549
1550                         if (viafb_SAMM_ON)
1551                                 viafb_primary_dev = setting_info.primary_device;
1552
1553                         viafb_set_start_addr();
1554                         viafb_set_iga_path();
1555                 }
1556                 need_set_mode = 1;
1557         }
1558
1559         viaparinfo->duoview = is_duoview();
1560
1561         if (!need_set_mode) {
1562                 ;
1563         } else {
1564                 viafb_set_iga_path();
1565                 viafb_set_par(info);
1566         }
1567         return true;
1568 }
1569
1570 static void retrieve_device_setting(struct viafb_ioctl_setting
1571         *setting_info)
1572 {
1573
1574         /* get device status */
1575         if (viafb_CRT_ON == 1)
1576                 setting_info->device_status = CRT_Device;
1577         if (viafb_DVI_ON == 1)
1578                 setting_info->device_status |= DVI_Device;
1579         if (viafb_LCD_ON == 1)
1580                 setting_info->device_status |= LCD_Device;
1581         if (viafb_LCD2_ON == 1)
1582                 setting_info->device_status |= LCD2_Device;
1583         if ((viaparinfo->video_on_crt == 1) && (viafb_CRT_ON == 1)) {
1584                 setting_info->video_device_status =
1585                         viaparinfo->crt_setting_info->iga_path;
1586         } else if ((viaparinfo->video_on_dvi == 1) && (viafb_DVI_ON == 1)) {
1587                 setting_info->video_device_status =
1588                         viaparinfo->tmds_setting_info->iga_path;
1589         } else if ((viaparinfo->video_on_lcd == 1) && (viafb_LCD_ON == 1)) {
1590                 setting_info->video_device_status =
1591                         viaparinfo->lvds_setting_info->iga_path;
1592         } else {
1593                 setting_info->video_device_status = 0;
1594         }
1595
1596         setting_info->samm_status = viafb_SAMM_ON;
1597         setting_info->primary_device = get_primary_device();
1598
1599         setting_info->first_dev_bpp = viafb_bpp;
1600         setting_info->second_dev_bpp = viafb_bpp1;
1601
1602         setting_info->first_dev_refresh = viafb_refresh;
1603         setting_info->second_dev_refresh = viafb_refresh1;
1604
1605         setting_info->first_dev_hor_res = viafb_hotplug_Xres;
1606         setting_info->first_dev_ver_res = viafb_hotplug_Yres;
1607         setting_info->second_dev_hor_res = viafb_second_xres;
1608         setting_info->second_dev_ver_res = viafb_second_yres;
1609
1610         /* Get lcd attributes */
1611         setting_info->lcd_attributes.display_center = viafb_lcd_dsp_method;
1612         setting_info->lcd_attributes.panel_id = viafb_lcd_panel_id;
1613         setting_info->lcd_attributes.lcd_mode = viafb_lcd_mode;
1614 }
1615
1616 static void parse_active_dev(void)
1617 {
1618         viafb_CRT_ON = STATE_OFF;
1619         viafb_DVI_ON = STATE_OFF;
1620         viafb_LCD_ON = STATE_OFF;
1621         viafb_LCD2_ON = STATE_OFF;
1622         /* 1. Modify the active status of devices. */
1623         /* 2. Keep the order of devices, so we can set corresponding
1624            IGA path to devices in SAMM case. */
1625         /*    Note: The previous of active_dev is primary device,
1626            and the following is secondary device. */
1627         if (!strncmp(viafb_active_dev, "CRT+DVI", 7)) {
1628                 /* CRT+DVI */
1629                 viafb_CRT_ON = STATE_ON;
1630                 viafb_DVI_ON = STATE_ON;
1631                 viafb_primary_dev = CRT_Device;
1632         } else if (!strncmp(viafb_active_dev, "DVI+CRT", 7)) {
1633                 /* DVI+CRT */
1634                 viafb_CRT_ON = STATE_ON;
1635                 viafb_DVI_ON = STATE_ON;
1636                 viafb_primary_dev = DVI_Device;
1637         } else if (!strncmp(viafb_active_dev, "CRT+LCD", 7)) {
1638                 /* CRT+LCD */
1639                 viafb_CRT_ON = STATE_ON;
1640                 viafb_LCD_ON = STATE_ON;
1641                 viafb_primary_dev = CRT_Device;
1642         } else if (!strncmp(viafb_active_dev, "LCD+CRT", 7)) {
1643                 /* LCD+CRT */
1644                 viafb_CRT_ON = STATE_ON;
1645                 viafb_LCD_ON = STATE_ON;
1646                 viafb_primary_dev = LCD_Device;
1647         } else if (!strncmp(viafb_active_dev, "DVI+LCD", 7)) {
1648                 /* DVI+LCD */
1649                 viafb_DVI_ON = STATE_ON;
1650                 viafb_LCD_ON = STATE_ON;
1651                 viafb_primary_dev = DVI_Device;
1652         } else if (!strncmp(viafb_active_dev, "LCD+DVI", 7)) {
1653                 /* LCD+DVI */
1654                 viafb_DVI_ON = STATE_ON;
1655                 viafb_LCD_ON = STATE_ON;
1656                 viafb_primary_dev = LCD_Device;
1657         } else if (!strncmp(viafb_active_dev, "LCD+LCD2", 8)) {
1658                 viafb_LCD_ON = STATE_ON;
1659                 viafb_LCD2_ON = STATE_ON;
1660                 viafb_primary_dev = LCD_Device;
1661         } else if (!strncmp(viafb_active_dev, "LCD2+LCD", 8)) {
1662                 viafb_LCD_ON = STATE_ON;
1663                 viafb_LCD2_ON = STATE_ON;
1664                 viafb_primary_dev = LCD2_Device;
1665         } else if (!strncmp(viafb_active_dev, "CRT", 3)) {
1666                 /* CRT only */
1667                 viafb_CRT_ON = STATE_ON;
1668                 viafb_SAMM_ON = STATE_OFF;
1669         } else if (!strncmp(viafb_active_dev, "DVI", 3)) {
1670                 /* DVI only */
1671                 viafb_DVI_ON = STATE_ON;
1672                 viafb_SAMM_ON = STATE_OFF;
1673         } else if (!strncmp(viafb_active_dev, "LCD", 3)) {
1674                 /* LCD only */
1675                 viafb_LCD_ON = STATE_ON;
1676                 viafb_SAMM_ON = STATE_OFF;
1677         } else {
1678                 viafb_CRT_ON = STATE_ON;
1679                 viafb_SAMM_ON = STATE_OFF;
1680         }
1681         viaparinfo->duoview = is_duoview();
1682 }
1683
1684 static void parse_video_dev(void)
1685 {
1686         viaparinfo->video_on_crt = STATE_OFF;
1687         viaparinfo->video_on_dvi = STATE_OFF;
1688         viaparinfo->video_on_lcd = STATE_OFF;
1689
1690         if (!strncmp(viafb_video_dev, "CRT", 3)) {
1691                 /* Video on CRT */
1692                 viaparinfo->video_on_crt = STATE_ON;
1693         } else if (!strncmp(viafb_video_dev, "DVI", 3)) {
1694                 /* Video on DVI */
1695                 viaparinfo->video_on_dvi = STATE_ON;
1696         } else if (!strncmp(viafb_video_dev, "LCD", 3)) {
1697                 /* Video on LCD */
1698                 viaparinfo->video_on_lcd = STATE_ON;
1699         }
1700 }
1701
1702 static int parse_port(char *opt_str, int *output_interface)
1703 {
1704         if (!strncmp(opt_str, "DVP0", 4))
1705                 *output_interface = INTERFACE_DVP0;
1706         else if (!strncmp(opt_str, "DVP1", 4))
1707                 *output_interface = INTERFACE_DVP1;
1708         else if (!strncmp(opt_str, "DFP_HIGHLOW", 11))
1709                 *output_interface = INTERFACE_DFP;
1710         else if (!strncmp(opt_str, "DFP_HIGH", 8))
1711                 *output_interface = INTERFACE_DFP_HIGH;
1712         else if (!strncmp(opt_str, "DFP_LOW", 7))
1713                 *output_interface = INTERFACE_DFP_LOW;
1714         else
1715                 *output_interface = INTERFACE_NONE;
1716         return 0;
1717 }
1718
1719 static void parse_lcd_port(void)
1720 {
1721         parse_port(viafb_lcd_port, &viaparinfo->chip_info->lvds_chip_info.
1722                 output_interface);
1723         /*Initialize to avoid unexpected behavior */
1724         viaparinfo->chip_info->lvds_chip_info2.output_interface =
1725         INTERFACE_NONE;
1726
1727         DEBUG_MSG(KERN_INFO "parse_lcd_port: viafb_lcd_port:%s,interface:%d\n",
1728                   viafb_lcd_port, viaparinfo->chip_info->lvds_chip_info.
1729                   output_interface);
1730 }
1731
1732 static void parse_dvi_port(void)
1733 {
1734         parse_port(viafb_dvi_port, &viaparinfo->chip_info->tmds_chip_info.
1735                 output_interface);
1736
1737         DEBUG_MSG(KERN_INFO "parse_dvi_port: viafb_dvi_port:%s,interface:%d\n",
1738                   viafb_dvi_port, viaparinfo->chip_info->tmds_chip_info.
1739                   output_interface);
1740 }
1741
1742 /*
1743  * The proc filesystem read/write function, a simple proc implement to
1744  * get/set the value of DPA  DVP0,   DVP0DataDriving,  DVP0ClockDriving, DVP1,
1745  * DVP1Driving, DFPHigh, DFPLow CR96,   SR2A[5], SR1B[1], SR2A[4], SR1E[2],
1746  * CR9B,    SR65,    CR97,    CR99
1747  */
1748 static int viafb_dvp0_proc_read(char *buf, char **start, off_t offset,
1749 int count, int *eof, void *data)
1750 {
1751         int len = 0;
1752         u8 dvp0_data_dri = 0, dvp0_clk_dri = 0, dvp0 = 0;
1753         dvp0_data_dri =
1754             (viafb_read_reg(VIASR, SR2A) & BIT5) >> 4 |
1755             (viafb_read_reg(VIASR, SR1B) & BIT1) >> 1;
1756         dvp0_clk_dri =
1757             (viafb_read_reg(VIASR, SR2A) & BIT4) >> 3 |
1758             (viafb_read_reg(VIASR, SR1E) & BIT2) >> 2;
1759         dvp0 = viafb_read_reg(VIACR, CR96) & 0x0f;
1760         len +=
1761             sprintf(buf + len, "%x %x %x\n", dvp0, dvp0_data_dri, dvp0_clk_dri);
1762         *eof = 1;               /*Inform kernel end of data */
1763         return len;
1764 }
1765 static int viafb_dvp0_proc_write(struct file *file,
1766         const char __user *buffer, unsigned long count, void *data)
1767 {
1768         char buf[20], *value, *pbuf;
1769         u8 reg_val = 0;
1770         unsigned long length, i;
1771         if (count < 1)
1772                 return -EINVAL;
1773         length = count > 20 ? 20 : count;
1774         if (copy_from_user(&buf[0], buffer, length))
1775                 return -EFAULT;
1776         buf[length - 1] = '\0'; /*Ensure end string */
1777         pbuf = &buf[0];
1778         for (i = 0; i < 3; i++) {
1779                 value = strsep(&pbuf, " ");
1780                 if (value != NULL) {
1781                         strict_strtoul(value, 0, (unsigned long *)&reg_val);
1782                         DEBUG_MSG(KERN_INFO "DVP0:reg_val[%l]=:%x\n", i,
1783                                   reg_val);
1784                         switch (i) {
1785                         case 0:
1786                                 viafb_write_reg_mask(CR96, VIACR,
1787                                         reg_val, 0x0f);
1788                                 break;
1789                         case 1:
1790                                 viafb_write_reg_mask(SR2A, VIASR,
1791                                         reg_val << 4, BIT5);
1792                                 viafb_write_reg_mask(SR1B, VIASR,
1793                                         reg_val << 1, BIT1);
1794                                 break;
1795                         case 2:
1796                                 viafb_write_reg_mask(SR2A, VIASR,
1797                                         reg_val << 3, BIT4);
1798                                 viafb_write_reg_mask(SR1E, VIASR,
1799                                         reg_val << 2, BIT2);
1800                                 break;
1801                         default:
1802                                 break;
1803                         }
1804                 } else {
1805                         break;
1806                 }
1807         }
1808         return count;
1809 }
1810 static int viafb_dvp1_proc_read(char *buf, char **start, off_t offset,
1811         int count, int *eof, void *data)
1812 {
1813         int len = 0;
1814         u8 dvp1 = 0, dvp1_data_dri = 0, dvp1_clk_dri = 0;
1815         dvp1 = viafb_read_reg(VIACR, CR9B) & 0x0f;
1816         dvp1_data_dri = (viafb_read_reg(VIASR, SR65) & 0x0c) >> 2;
1817         dvp1_clk_dri = viafb_read_reg(VIASR, SR65) & 0x03;
1818         len +=
1819             sprintf(buf + len, "%x %x %x\n", dvp1, dvp1_data_dri, dvp1_clk_dri);
1820         *eof = 1;               /*Inform kernel end of data */
1821         return len;
1822 }
1823 static int viafb_dvp1_proc_write(struct file *file,
1824         const char __user *buffer, unsigned long count, void *data)
1825 {
1826         char buf[20], *value, *pbuf;
1827         u8 reg_val = 0;
1828         unsigned long length, i;
1829         if (count < 1)
1830                 return -EINVAL;
1831         length = count > 20 ? 20 : count;
1832         if (copy_from_user(&buf[0], buffer, length))
1833                 return -EFAULT;
1834         buf[length - 1] = '\0'; /*Ensure end string */
1835         pbuf = &buf[0];
1836         for (i = 0; i < 3; i++) {
1837                 value = strsep(&pbuf, " ");
1838                 if (value != NULL) {
1839                         strict_strtoul(value, 0, (unsigned long *)&reg_val);
1840                         switch (i) {
1841                         case 0:
1842                                 viafb_write_reg_mask(CR9B, VIACR,
1843                                         reg_val, 0x0f);
1844                                 break;
1845                         case 1:
1846                                 viafb_write_reg_mask(SR65, VIASR,
1847                                         reg_val << 2, 0x0c);
1848                                 break;
1849                         case 2:
1850                                 viafb_write_reg_mask(SR65, VIASR,
1851                                         reg_val, 0x03);
1852                                 break;
1853                         default:
1854                                 break;
1855                         }
1856                 } else {
1857                         break;
1858                 }
1859         }
1860         return count;
1861 }
1862
1863 static int viafb_dfph_proc_read(char *buf, char **start, off_t offset,
1864         int count, int *eof, void *data)
1865 {
1866         int len = 0;
1867         u8 dfp_high = 0;
1868         dfp_high = viafb_read_reg(VIACR, CR97) & 0x0f;
1869         len += sprintf(buf + len, "%x\n", dfp_high);
1870         *eof = 1;               /*Inform kernel end of data */
1871         return len;
1872 }
1873 static int viafb_dfph_proc_write(struct file *file,
1874         const char __user *buffer, unsigned long count, void *data)
1875 {
1876         char buf[20];
1877         u8 reg_val = 0;
1878         unsigned long length;
1879         if (count < 1)
1880                 return -EINVAL;
1881         length = count > 20 ? 20 : count;
1882         if (copy_from_user(&buf[0], buffer, length))
1883                 return -EFAULT;
1884         buf[length - 1] = '\0'; /*Ensure end string */
1885         strict_strtoul(&buf[0], 0, (unsigned long *)&reg_val);
1886         viafb_write_reg_mask(CR97, VIACR, reg_val, 0x0f);
1887         return count;
1888 }
1889 static int viafb_dfpl_proc_read(char *buf, char **start, off_t offset,
1890         int count, int *eof, void *data)
1891 {
1892         int len = 0;
1893         u8 dfp_low = 0;
1894         dfp_low = viafb_read_reg(VIACR, CR99) & 0x0f;
1895         len += sprintf(buf + len, "%x\n", dfp_low);
1896         *eof = 1;               /*Inform kernel end of data */
1897         return len;
1898 }
1899 static int viafb_dfpl_proc_write(struct file *file,
1900         const char __user *buffer, unsigned long count, void *data)
1901 {
1902         char buf[20];
1903         u8 reg_val = 0;
1904         unsigned long length;
1905         if (count < 1)
1906                 return -EINVAL;
1907         length = count > 20 ? 20 : count;
1908         if (copy_from_user(&buf[0], buffer, length))
1909                 return -EFAULT;
1910         buf[length - 1] = '\0'; /*Ensure end string */
1911         strict_strtoul(&buf[0], 0, (unsigned long *)&reg_val);
1912         viafb_write_reg_mask(CR99, VIACR, reg_val, 0x0f);
1913         return count;
1914 }
1915 static int viafb_vt1636_proc_read(char *buf, char **start,
1916         off_t offset, int count, int *eof, void *data)
1917 {
1918         int len = 0;
1919         u8 vt1636_08 = 0, vt1636_09 = 0;
1920         switch (viaparinfo->chip_info->lvds_chip_info.lvds_chip_name) {
1921         case VT1636_LVDS:
1922                 vt1636_08 =
1923                     viafb_gpio_i2c_read_lvds(viaparinfo->lvds_setting_info,
1924                     &viaparinfo->chip_info->lvds_chip_info, 0x08) & 0x0f;
1925                 vt1636_09 =
1926                     viafb_gpio_i2c_read_lvds(viaparinfo->lvds_setting_info,
1927                     &viaparinfo->chip_info->lvds_chip_info, 0x09) & 0x1f;
1928                 len += sprintf(buf + len, "%x %x\n", vt1636_08, vt1636_09);
1929                 break;
1930         default:
1931                 break;
1932         }
1933         switch (viaparinfo->chip_info->lvds_chip_info2.lvds_chip_name) {
1934         case VT1636_LVDS:
1935                 vt1636_08 =
1936                     viafb_gpio_i2c_read_lvds(viaparinfo->lvds_setting_info2,
1937                         &viaparinfo->chip_info->lvds_chip_info2, 0x08) & 0x0f;
1938                 vt1636_09 =
1939                     viafb_gpio_i2c_read_lvds(viaparinfo->lvds_setting_info2,
1940                         &viaparinfo->chip_info->lvds_chip_info2, 0x09) & 0x1f;
1941                 len += sprintf(buf + len, " %x %x\n", vt1636_08, vt1636_09);
1942                 break;
1943         default:
1944                 break;
1945         }
1946         *eof = 1;               /*Inform kernel end of data */
1947         return len;
1948 }
1949 static int viafb_vt1636_proc_write(struct file *file,
1950         const char __user *buffer, unsigned long count, void *data)
1951 {
1952         char buf[30], *value, *pbuf;
1953         struct IODATA reg_val;
1954         unsigned long length, i;
1955         if (count < 1)
1956                 return -EINVAL;
1957         length = count > 30 ? 30 : count;
1958         if (copy_from_user(&buf[0], buffer, length))
1959                 return -EFAULT;
1960         buf[length - 1] = '\0'; /*Ensure end string */
1961         pbuf = &buf[0];
1962         switch (viaparinfo->chip_info->lvds_chip_info.lvds_chip_name) {
1963         case VT1636_LVDS:
1964                 for (i = 0; i < 2; i++) {
1965                         value = strsep(&pbuf, " ");
1966                         if (value != NULL) {
1967                                 strict_strtoul(value, 0,
1968                                         (unsigned long *)&reg_val.Data);
1969                                 switch (i) {
1970                                 case 0:
1971                                         reg_val.Index = 0x08;
1972                                         reg_val.Mask = 0x0f;
1973                                         viafb_gpio_i2c_write_mask_lvds
1974                                             (viaparinfo->lvds_setting_info,
1975                                             &viaparinfo->
1976                                             chip_info->lvds_chip_info,
1977                                              reg_val);
1978                                         break;
1979                                 case 1:
1980                                         reg_val.Index = 0x09;
1981                                         reg_val.Mask = 0x1f;
1982                                         viafb_gpio_i2c_write_mask_lvds
1983                                             (viaparinfo->lvds_setting_info,
1984                                             &viaparinfo->
1985                                             chip_info->lvds_chip_info,
1986                                              reg_val);
1987                                         break;
1988                                 default:
1989                                         break;
1990                                 }
1991                         } else {
1992                                 break;
1993                         }
1994                 }
1995                 break;
1996         default:
1997                 break;
1998         }
1999         switch (viaparinfo->chip_info->lvds_chip_info2.lvds_chip_name) {
2000         case VT1636_LVDS:
2001                 for (i = 0; i < 2; i++) {
2002                         value = strsep(&pbuf, " ");
2003                         if (value != NULL) {
2004                                 strict_strtoul(value, 0,
2005                                         (unsigned long *)&reg_val.Data);
2006                                 switch (i) {
2007                                 case 0:
2008                                         reg_val.Index = 0x08;
2009                                         reg_val.Mask = 0x0f;
2010                                         viafb_gpio_i2c_write_mask_lvds
2011                                             (viaparinfo->lvds_setting_info2,
2012                                             &viaparinfo->
2013                                             chip_info->lvds_chip_info2,
2014                                              reg_val);
2015                                         break;
2016                                 case 1:
2017                                         reg_val.Index = 0x09;
2018                                         reg_val.Mask = 0x1f;
2019                                         viafb_gpio_i2c_write_mask_lvds
2020                                             (viaparinfo->lvds_setting_info2,
2021                                             &viaparinfo->
2022                                             chip_info->lvds_chip_info2,
2023                                              reg_val);
2024                                         break;
2025                                 default:
2026                                         break;
2027                                 }
2028                         } else {
2029                                 break;
2030                         }
2031                 }
2032                 break;
2033         default:
2034                 break;
2035         }
2036         return count;
2037 }
2038
2039 static void viafb_init_proc(struct proc_dir_entry *viafb_entry)
2040 {
2041         struct proc_dir_entry *entry;
2042         viafb_entry = proc_mkdir("viafb", NULL);
2043         if (viafb_entry) {
2044                 entry = create_proc_entry("dvp0", 0, viafb_entry);
2045                 if (entry) {
2046                         entry->owner = THIS_MODULE;
2047                         entry->read_proc = viafb_dvp0_proc_read;
2048                         entry->write_proc = viafb_dvp0_proc_write;
2049                 }
2050                 entry = create_proc_entry("dvp1", 0, viafb_entry);
2051                 if (entry) {
2052                         entry->owner = THIS_MODULE;
2053                         entry->read_proc = viafb_dvp1_proc_read;
2054                         entry->write_proc = viafb_dvp1_proc_write;
2055                 }
2056                 entry = create_proc_entry("dfph", 0, viafb_entry);
2057                 if (entry) {
2058                         entry->owner = THIS_MODULE;
2059                         entry->read_proc = viafb_dfph_proc_read;
2060                         entry->write_proc = viafb_dfph_proc_write;
2061                 }
2062                 entry = create_proc_entry("dfpl", 0, viafb_entry);
2063                 if (entry) {
2064                         entry->owner = THIS_MODULE;
2065                         entry->read_proc = viafb_dfpl_proc_read;
2066                         entry->write_proc = viafb_dfpl_proc_write;
2067                 }
2068                 if (VT1636_LVDS == viaparinfo->chip_info->lvds_chip_info.
2069                         lvds_chip_name || VT1636_LVDS ==
2070                     viaparinfo->chip_info->lvds_chip_info2.lvds_chip_name) {
2071                         entry = create_proc_entry("vt1636", 0, viafb_entry);
2072                         if (entry) {
2073                                 entry->owner = THIS_MODULE;
2074                                 entry->read_proc = viafb_vt1636_proc_read;
2075                                 entry->write_proc = viafb_vt1636_proc_write;
2076                         }
2077                 }
2078
2079         }
2080 }
2081 static void viafb_remove_proc(struct proc_dir_entry *viafb_entry)
2082 {
2083         /* no problem if it was not registered */
2084         remove_proc_entry("dvp0", viafb_entry);/* parent dir */
2085         remove_proc_entry("dvp1", viafb_entry);
2086         remove_proc_entry("dfph", viafb_entry);
2087         remove_proc_entry("dfpl", viafb_entry);
2088         remove_proc_entry("vt1636", viafb_entry);
2089         remove_proc_entry("vt1625", viafb_entry);
2090 }
2091
2092 static int __devinit via_pci_probe(void)
2093 {
2094         unsigned int default_xres, default_yres;
2095         char *tmpc, *tmpm;
2096         char *tmpc_sec, *tmpm_sec;
2097         int vmode_index;
2098         u32 tmds_length, lvds_length, crt_length, chip_length, viafb_par_length;
2099
2100         DEBUG_MSG(KERN_INFO "VIAFB PCI Probe!!\n");
2101
2102         viafb_par_length = ALIGN(sizeof(struct viafb_par), BITS_PER_LONG/8);
2103         tmds_length = ALIGN(sizeof(struct tmds_setting_information),
2104                 BITS_PER_LONG/8);
2105         lvds_length = ALIGN(sizeof(struct lvds_setting_information),
2106                 BITS_PER_LONG/8);
2107         crt_length = ALIGN(sizeof(struct lvds_setting_information),
2108                 BITS_PER_LONG/8);
2109         chip_length = ALIGN(sizeof(struct chip_information), BITS_PER_LONG/8);
2110
2111         /* Allocate fb_info and ***_par here, also including some other needed
2112          * variables
2113         */
2114         viafbinfo = framebuffer_alloc(viafb_par_length + 2 * lvds_length +
2115         tmds_length + crt_length + chip_length, NULL);
2116         if (!viafbinfo) {
2117                 printk(KERN_ERR"Could not allocate memory for viafb_info.\n");
2118                 return -ENODEV;
2119         }
2120
2121         viaparinfo = (struct viafb_par *)viafbinfo->par;
2122         viaparinfo->tmds_setting_info = (struct tmds_setting_information *)
2123                 ((unsigned long)viaparinfo + viafb_par_length);
2124         viaparinfo->lvds_setting_info = (struct lvds_setting_information *)
2125                 ((unsigned long)viaparinfo->tmds_setting_info + tmds_length);
2126         viaparinfo->lvds_setting_info2 = (struct lvds_setting_information *)
2127                 ((unsigned long)viaparinfo->lvds_setting_info + lvds_length);
2128         viaparinfo->crt_setting_info = (struct crt_setting_information *)
2129                 ((unsigned long)viaparinfo->lvds_setting_info2 + lvds_length);
2130         viaparinfo->chip_info = (struct chip_information *)
2131                 ((unsigned long)viaparinfo->crt_setting_info + crt_length);
2132
2133         if (viafb_dual_fb)
2134                 viafb_SAMM_ON = 1;
2135         parse_active_dev();
2136         parse_video_dev();
2137         parse_lcd_port();
2138         parse_dvi_port();
2139
2140         /* for dual-fb must viafb_SAMM_ON=1 and viafb_dual_fb=1 */
2141         if (!viafb_SAMM_ON)
2142                 viafb_dual_fb = 0;
2143
2144         /* Set up I2C bus stuff */
2145         viafb_create_i2c_bus(viaparinfo);
2146
2147         viafb_init_chip_info();
2148         viafb_get_fb_info(&viaparinfo->fbmem, &viaparinfo->memsize);
2149         viaparinfo->fbmem_free = viaparinfo->memsize;
2150         viaparinfo->fbmem_used = 0;
2151         viaparinfo->fbmem_virt = ioremap_nocache(viaparinfo->fbmem,
2152                 viaparinfo->memsize);
2153         viafbinfo->screen_base = (char *)viaparinfo->fbmem_virt;
2154
2155         if (!viaparinfo->fbmem_virt) {
2156                 printk(KERN_INFO "ioremap failed\n");
2157                 return -1;
2158         }
2159
2160         viafb_get_mmio_info(&viaparinfo->mmio_base, &viaparinfo->mmio_len);
2161         viaparinfo->io_virt = ioremap_nocache(viaparinfo->mmio_base,
2162                 viaparinfo->mmio_len);
2163
2164         viafbinfo->node = 0;
2165         viafbinfo->fbops = &viafb_ops;
2166         viafbinfo->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
2167
2168         viafbinfo->pseudo_palette = pseudo_pal;
2169         if (viafb_accel) {
2170                 viafb_init_accel();
2171                 viafb_init_2d_engine();
2172                 viafb_hw_cursor_init();
2173         }
2174
2175         if (viafb_second_size && (viafb_second_size < 8)) {
2176                 viafb_second_offset = viaparinfo->fbmem_free -
2177                         viafb_second_size * 1024 * 1024;
2178         } else {
2179                 viafb_second_size = 8;
2180                 viafb_second_offset = viaparinfo->fbmem_free -
2181                         viafb_second_size * 1024 * 1024;
2182         }
2183
2184         viafb_FB_MM = viaparinfo->fbmem_virt;
2185         tmpm = viafb_mode;
2186         tmpc = strsep(&tmpm, "x");
2187         strict_strtoul(tmpc, 0, (unsigned long *)&default_xres);
2188         strict_strtoul(tmpm, 0, (unsigned long *)&default_yres);
2189
2190         vmode_index = viafb_get_mode_index(default_xres, default_yres, 0);
2191         DEBUG_MSG(KERN_INFO "0->index=%d\n", vmode_index);
2192
2193         if (viafb_SAMM_ON == 1) {
2194                 if (strcmp(viafb_mode, viafb_mode1)) {
2195                         tmpm_sec = viafb_mode1;
2196                         tmpc_sec = strsep(&tmpm_sec, "x");
2197                         strict_strtoul(tmpc_sec, 0,
2198                                 (unsigned long *)&viafb_second_xres);
2199                         strict_strtoul(tmpm_sec, 0,
2200                                 (unsigned long *)&viafb_second_yres);
2201                 } else {
2202                         viafb_second_xres = default_xres;
2203                         viafb_second_yres = default_yres;
2204                 }
2205                 if (0 == viafb_second_virtual_xres) {
2206                         switch (viafb_second_xres) {
2207                         case 1400:
2208                                 viafb_second_virtual_xres = 1408;
2209                                 break;
2210                         default:
2211                                 viafb_second_virtual_xres = viafb_second_xres;
2212                                 break;
2213                         }
2214                 }
2215                 if (0 == viafb_second_virtual_yres)
2216                         viafb_second_virtual_yres = viafb_second_yres;
2217         }
2218
2219         switch (viafb_bpp) {
2220         case 0 ... 8:
2221                 viafb_bpp = 8;
2222                 break;
2223         case 9 ... 16:
2224                 viafb_bpp = 16;
2225                 break;
2226         case 17 ... 32:
2227                 viafb_bpp = 32;
2228                 break;
2229         default:
2230                 viafb_bpp = 8;
2231         }
2232         default_var.xres = default_xres;
2233         default_var.yres = default_yres;
2234         switch (default_xres) {
2235         case 1400:
2236                 default_var.xres_virtual = 1408;
2237                 break;
2238         default:
2239                 default_var.xres_virtual = default_xres;
2240                 break;
2241         }
2242         default_var.yres_virtual = default_yres;
2243         default_var.bits_per_pixel = viafb_bpp;
2244         if (default_var.bits_per_pixel == 15)
2245                 default_var.bits_per_pixel = 16;
2246         default_var.pixclock =
2247             viafb_get_pixclock(default_xres, default_yres, viafb_refresh);
2248         default_var.left_margin = (default_xres >> 3) & 0xf8;
2249         default_var.right_margin = 32;
2250         default_var.upper_margin = 16;
2251         default_var.lower_margin = 4;
2252         default_var.hsync_len = default_var.left_margin;
2253         default_var.vsync_len = 4;
2254         default_var.accel_flags = 0;
2255
2256         if (viafb_accel) {
2257                 viafbinfo->flags |=
2258                     (FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT |
2259                      FBINFO_HWACCEL_IMAGEBLIT);
2260                 default_var.accel_flags |= FB_ACCELF_TEXT;
2261         } else
2262                 viafbinfo->flags |= FBINFO_HWACCEL_DISABLED;
2263
2264         if (viafb_dual_fb) {
2265                 viafbinfo1 = framebuffer_alloc(viafb_par_length, NULL);
2266                 if (!viafbinfo1) {
2267                         printk(KERN_ERR
2268                         "allocate the second framebuffer struct error\n");
2269                         framebuffer_release(viafbinfo);
2270                         return -ENOMEM;
2271                 }
2272                 viaparinfo1 = viafbinfo1->par;
2273                 memcpy(viaparinfo1, viaparinfo, viafb_par_length);
2274                 viaparinfo1->memsize = viaparinfo->memsize -
2275                         viafb_second_offset;
2276                 viaparinfo->memsize = viafb_second_offset;
2277                 viaparinfo1->fbmem_virt = viaparinfo->fbmem_virt +
2278                         viafb_second_offset;
2279                 viaparinfo1->fbmem = viaparinfo->fbmem + viafb_second_offset;
2280
2281                 viaparinfo1->fbmem_used = viaparinfo->fbmem_used;
2282                 viaparinfo1->fbmem_free = viaparinfo1->memsize -
2283                         viaparinfo1->fbmem_used;
2284                 viaparinfo->fbmem_free = viaparinfo->memsize;
2285                 viaparinfo->fbmem_used = 0;
2286                 if (viafb_accel) {
2287                         viaparinfo1->cursor_start =
2288                             viaparinfo->cursor_start - viafb_second_offset;
2289                         viaparinfo1->VQ_start = viaparinfo->VQ_start -
2290                                 viafb_second_offset;
2291                         viaparinfo1->VQ_end = viaparinfo->VQ_end -
2292                                 viafb_second_offset;
2293                 }
2294
2295                 memcpy(viafbinfo1, viafbinfo, sizeof(struct fb_info));
2296                 viafbinfo1->screen_base = viafbinfo->screen_base +
2297                         viafb_second_offset;
2298                 viafbinfo1->fix.smem_start = viaparinfo1->fbmem;
2299                 viafbinfo1->fix.smem_len = viaparinfo1->fbmem_free;
2300
2301                 default_var.xres = viafb_second_xres;
2302                 default_var.yres = viafb_second_yres;
2303                 default_var.xres_virtual = viafb_second_virtual_xres;
2304                 default_var.yres_virtual = viafb_second_virtual_yres;
2305                 if (viafb_bpp1 != viafb_bpp)
2306                         viafb_bpp1 = viafb_bpp;
2307                 default_var.bits_per_pixel = viafb_bpp1;
2308                 default_var.pixclock =
2309                     viafb_get_pixclock(viafb_second_xres, viafb_second_yres,
2310                     viafb_refresh);
2311                 default_var.left_margin = (viafb_second_xres >> 3) & 0xf8;
2312                 default_var.right_margin = 32;
2313                 default_var.upper_margin = 16;
2314                 default_var.lower_margin = 4;
2315                 default_var.hsync_len = default_var.left_margin;
2316                 default_var.vsync_len = 4;
2317
2318                 viafb_setup_fixinfo(&viafbinfo1->fix, viaparinfo1);
2319                 viafb_check_var(&default_var, viafbinfo1);
2320                 viafbinfo1->var = default_var;
2321                 viafb_update_viafb_par(viafbinfo);
2322                 viafb_update_fix(&viafbinfo1->fix, viafbinfo1);
2323         }
2324
2325         viafb_setup_fixinfo(&viafbinfo->fix, viaparinfo);
2326         viafb_check_var(&default_var, viafbinfo);
2327         viafbinfo->var = default_var;
2328         viafb_update_viafb_par(viafbinfo);
2329         viafb_update_fix(&viafbinfo->fix, viafbinfo);
2330         default_var.activate = FB_ACTIVATE_NOW;
2331         fb_alloc_cmap(&viafbinfo->cmap, 256, 0);
2332
2333         if (viafb_dual_fb && (viafb_primary_dev == LCD_Device)
2334             && (viaparinfo->chip_info->gfx_chip_name == UNICHROME_CLE266)) {
2335                 if (register_framebuffer(viafbinfo1) < 0)
2336                         return -EINVAL;
2337         }
2338         if (register_framebuffer(viafbinfo) < 0)
2339                 return -EINVAL;
2340
2341         if (viafb_dual_fb && ((viafb_primary_dev != LCD_Device)
2342                         || (viaparinfo->chip_info->gfx_chip_name !=
2343                         UNICHROME_CLE266))) {
2344                 if (register_framebuffer(viafbinfo1) < 0)
2345                         return -EINVAL;
2346         }
2347         DEBUG_MSG(KERN_INFO "fb%d: %s frame buffer device %dx%d-%dbpp\n",
2348                   viafbinfo->node, viafbinfo->fix.id, default_var.xres,
2349                   default_var.yres, default_var.bits_per_pixel);
2350
2351         viafb_init_proc(viaparinfo->proc_entry);
2352         viafb_init_dac(IGA2);
2353         return 0;
2354 }
2355
2356 static void __devexit via_pci_remove(void)
2357 {
2358         DEBUG_MSG(KERN_INFO "via_pci_remove!\n");
2359         fb_dealloc_cmap(&viafbinfo->cmap);
2360         unregister_framebuffer(viafbinfo);
2361         if (viafb_dual_fb)
2362                 unregister_framebuffer(viafbinfo1);
2363         iounmap((void *)viaparinfo->fbmem_virt);
2364         iounmap(viaparinfo->io_virt);
2365
2366         viafb_delete_i2c_buss(viaparinfo);
2367
2368         framebuffer_release(viafbinfo);
2369         if (viafb_dual_fb)
2370                 framebuffer_release(viafbinfo1);
2371
2372         viafb_remove_proc(viaparinfo->proc_entry);
2373 }
2374
2375 #ifndef MODULE
2376 static int __init viafb_setup(char *options)
2377 {
2378         char *this_opt;
2379         DEBUG_MSG(KERN_INFO "viafb_setup!\n");
2380
2381         if (!options || !*options)
2382                 return 0;
2383
2384         while ((this_opt = strsep(&options, ",")) != NULL) {
2385                 if (!*this_opt)
2386                         continue;
2387
2388                 if (!strncmp(this_opt, "viafb_mode1=", 12))
2389                         viafb_mode1 = kstrdup(this_opt + 12, GFP_KERNEL);
2390                 else if (!strncmp(this_opt, "viafb_mode=", 11))
2391                         viafb_mode = kstrdup(this_opt + 11, GFP_KERNEL);
2392                 else if (!strncmp(this_opt, "viafb_bpp1=", 11))
2393                         strict_strtoul(this_opt + 11, 0,
2394                                 (unsigned long *)&viafb_bpp1);
2395                 else if (!strncmp(this_opt, "viafb_bpp=", 10))
2396                         strict_strtoul(this_opt + 10, 0,
2397                                 (unsigned long *)&viafb_bpp);
2398                 else if (!strncmp(this_opt, "viafb_refresh1=", 15))
2399                         strict_strtoul(this_opt + 15, 0,
2400                                 (unsigned long *)&viafb_refresh1);
2401                 else if (!strncmp(this_opt, "viafb_refresh=", 14))
2402                         strict_strtoul(this_opt + 14, 0,
2403                                 (unsigned long *)&viafb_refresh);
2404                 else if (!strncmp(this_opt, "viafb_lcd_dsp_method=", 21))
2405                         strict_strtoul(this_opt + 21, 0,
2406                                 (unsigned long *)&viafb_lcd_dsp_method);
2407                 else if (!strncmp(this_opt, "viafb_lcd_panel_id=", 19))
2408                         strict_strtoul(this_opt + 19, 0,
2409                                 (unsigned long *)&viafb_lcd_panel_id);
2410                 else if (!strncmp(this_opt, "viafb_accel=", 12))
2411                         strict_strtoul(this_opt + 12, 0,
2412                                 (unsigned long *)&viafb_accel);
2413                 else if (!strncmp(this_opt, "viafb_SAMM_ON=", 14))
2414                         strict_strtoul(this_opt + 14, 0,
2415                                 (unsigned long *)&viafb_SAMM_ON);
2416                 else if (!strncmp(this_opt, "viafb_active_dev=", 17))
2417                         viafb_active_dev = kstrdup(this_opt + 17, GFP_KERNEL);
2418                 else if (!strncmp(this_opt,
2419                         "viafb_display_hardware_layout=", 30))
2420                         strict_strtoul(this_opt + 30, 0,
2421                         (unsigned long *)&viafb_display_hardware_layout);
2422                 else if (!strncmp(this_opt, "viafb_second_size=", 18))
2423                         strict_strtoul(this_opt + 18, 0,
2424                                 (unsigned long *)&viafb_second_size);
2425                 else if (!strncmp(this_opt,
2426                         "viafb_platform_epia_dvi=", 24))
2427                         strict_strtoul(this_opt + 24, 0,
2428                                 (unsigned long *)&viafb_platform_epia_dvi);
2429                 else if (!strncmp(this_opt,
2430                         "viafb_device_lcd_dualedge=", 26))
2431                         strict_strtoul(this_opt + 26, 0,
2432                                 (unsigned long *)&viafb_device_lcd_dualedge);
2433                 else if (!strncmp(this_opt, "viafb_bus_width=", 16))
2434                         strict_strtoul(this_opt + 16, 0,
2435                                 (unsigned long *)&viafb_bus_width);
2436                 else if (!strncmp(this_opt, "viafb_lcd_mode=", 15))
2437                         strict_strtoul(this_opt + 15, 0,
2438                                 (unsigned long *)&viafb_lcd_mode);
2439                 else if (!strncmp(this_opt, "viafb_video_dev=", 16))
2440                         viafb_video_dev = kstrdup(this_opt + 16, GFP_KERNEL);
2441                 else if (!strncmp(this_opt, "viafb_lcd_port=", 15))
2442                         viafb_lcd_port = kstrdup(this_opt + 15, GFP_KERNEL);
2443                 else if (!strncmp(this_opt, "viafb_dvi_port=", 15))
2444                         viafb_dvi_port = kstrdup(this_opt + 15, GFP_KERNEL);
2445         }
2446         return 0;
2447 }
2448 #endif
2449
2450 static int __init viafb_init(void)
2451 {
2452 #ifndef MODULE
2453         char *option = NULL;
2454         if (fb_get_options("viafb", &option))
2455                 return -ENODEV;
2456         viafb_setup(option);
2457 #endif
2458         printk(KERN_INFO
2459        "VIA Graphics Intergration Chipset framebuffer %d.%d initializing\n",
2460                VERSION_MAJOR, VERSION_MINOR);
2461         return via_pci_probe();
2462 }
2463
2464 static void __exit viafb_exit(void)
2465 {
2466         DEBUG_MSG(KERN_INFO "viafb_exit!\n");
2467         via_pci_remove();
2468 }
2469
2470 static struct fb_ops viafb_ops = {
2471         .owner = THIS_MODULE,
2472         .fb_open = viafb_open,
2473         .fb_release = viafb_release,
2474         .fb_check_var = viafb_check_var,
2475         .fb_set_par = viafb_set_par,
2476         .fb_setcolreg = viafb_setcolreg,
2477         .fb_pan_display = viafb_pan_display,
2478         .fb_blank = viafb_blank,
2479         .fb_fillrect = viafb_fillrect,
2480         .fb_copyarea = viafb_copyarea,
2481         .fb_imageblit = viafb_imageblit,
2482         .fb_cursor = viafb_cursor,
2483         .fb_ioctl = viafb_ioctl,
2484         .fb_sync = viafb_sync,
2485         .fb_setcmap = viafb_setcmap,
2486 };
2487
2488 module_init(viafb_init);
2489 module_exit(viafb_exit);
2490
2491 #ifdef MODULE
2492 module_param(viafb_memsize, int, 0);
2493
2494 module_param(viafb_mode, charp, 0);
2495 MODULE_PARM_DESC(viafb_mode, "Set resolution (default=640x480)");
2496
2497 module_param(viafb_mode1, charp, 0);
2498 MODULE_PARM_DESC(viafb_mode1, "Set resolution (default=640x480)");
2499
2500 module_param(viafb_bpp, int, 0);
2501 MODULE_PARM_DESC(viafb_bpp, "Set color depth (default=32bpp)");
2502
2503 module_param(viafb_bpp1, int, 0);
2504 MODULE_PARM_DESC(viafb_bpp1, "Set color depth (default=32bpp)");
2505
2506 module_param(viafb_refresh, int, 0);
2507 MODULE_PARM_DESC(viafb_refresh,
2508         "Set CRT viafb_refresh rate (default = 60)");
2509
2510 module_param(viafb_refresh1, int, 0);
2511 MODULE_PARM_DESC(viafb_refresh1,
2512         "Set CRT refresh rate (default = 60)");
2513
2514 module_param(viafb_lcd_panel_id, int, 0);
2515 MODULE_PARM_DESC(viafb_lcd_panel_id,
2516         "Set Flat Panel type(Default=1024x768)");
2517
2518 module_param(viafb_lcd_dsp_method, int, 0);
2519 MODULE_PARM_DESC(viafb_lcd_dsp_method,
2520         "Set Flat Panel display scaling method.(Default=Expandsion)");
2521
2522 module_param(viafb_SAMM_ON, int, 0);
2523 MODULE_PARM_DESC(viafb_SAMM_ON,
2524         "Turn on/off flag of SAMM(Default=OFF)");
2525
2526 module_param(viafb_accel, int, 0);
2527 MODULE_PARM_DESC(viafb_accel,
2528         "Set 2D Hardware Acceleration.(Default = OFF)");
2529
2530 module_param(viafb_active_dev, charp, 0);
2531 MODULE_PARM_DESC(viafb_active_dev, "Specify active devices.");
2532
2533 module_param(viafb_display_hardware_layout, int, 0);
2534 MODULE_PARM_DESC(viafb_display_hardware_layout,
2535         "Display Hardware Layout (LCD Only, DVI Only...,etc)");
2536
2537 module_param(viafb_second_size, int, 0);
2538 MODULE_PARM_DESC(viafb_second_size,
2539         "Set secondary device memory size");
2540
2541 module_param(viafb_dual_fb, int, 0);
2542 MODULE_PARM_DESC(viafb_dual_fb,
2543         "Turn on/off flag of dual framebuffer devices.(Default = OFF)");
2544
2545 module_param(viafb_platform_epia_dvi, int, 0);
2546 MODULE_PARM_DESC(viafb_platform_epia_dvi,
2547         "Turn on/off flag of DVI devices on EPIA board.(Default = OFF)");
2548
2549 module_param(viafb_device_lcd_dualedge, int, 0);
2550 MODULE_PARM_DESC(viafb_device_lcd_dualedge,
2551         "Turn on/off flag of dual edge panel.(Default = OFF)");
2552
2553 module_param(viafb_bus_width, int, 0);
2554 MODULE_PARM_DESC(viafb_bus_width,
2555         "Set bus width of panel.(Default = 12)");
2556
2557 module_param(viafb_lcd_mode, int, 0);
2558 MODULE_PARM_DESC(viafb_lcd_mode,
2559         "Set Flat Panel mode(Default=OPENLDI)");
2560
2561 module_param(viafb_video_dev, charp, 0);
2562 MODULE_PARM_DESC(viafb_video_dev, "Specify video devices.");
2563
2564 module_param(viafb_lcd_port, charp, 0);
2565 MODULE_PARM_DESC(viafb_lcd_port, "Specify LCD output port.");
2566
2567 module_param(viafb_dvi_port, charp, 0);
2568 MODULE_PARM_DESC(viafb_dvi_port, "Specify DVI output port.");
2569
2570 MODULE_LICENSE("GPL");
2571 #endif