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