d3dd2eb95586db6dff912b45b6863cdbfc720134
[sfrench/cifs-2.6.git] / drivers / video / via / viafbdev.c
1 /*
2  * Copyright 1998-2009 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 #include <linux/seq_file.h>
24 #include <linux/stat.h>
25 #include <linux/via-core.h>
26
27 #define _MASTER_FILE
28 #include "global.h"
29
30 static char *viafb_name = "Via";
31 static u32 pseudo_pal[17];
32
33 /* video mode */
34 static char *viafb_mode;
35 static char *viafb_mode1;
36 static int viafb_bpp = 32;
37 static int viafb_bpp1 = 32;
38
39 static unsigned int viafb_second_offset;
40 static int viafb_second_size;
41
42 static int viafb_accel = 1;
43
44 /* Added for specifying active devices.*/
45 char *viafb_active_dev;
46
47 /*Added for specify lcd output port*/
48 char *viafb_lcd_port = "";
49 char *viafb_dvi_port = "";
50
51 static void viafb_set_device(struct device_t active_dev);
52 static int apply_device_setting(struct viafb_ioctl_setting setting_info,
53                          struct fb_info *info);
54 static void apply_second_mode_setting(struct fb_var_screeninfo
55         *sec_var);
56 static void retrieve_device_setting(struct viafb_ioctl_setting
57         *setting_info);
58 static int viafb_pan_display(struct fb_var_screeninfo *var,
59         struct fb_info *info);
60
61 static struct fb_ops viafb_ops;
62
63
64 static void viafb_fill_var_color_info(struct fb_var_screeninfo *var, u8 depth)
65 {
66         var->grayscale = 0;
67         var->red.msb_right = 0;
68         var->green.msb_right = 0;
69         var->blue.msb_right = 0;
70         var->transp.offset = 0;
71         var->transp.length = 0;
72         var->transp.msb_right = 0;
73         var->nonstd = 0;
74         switch (depth) {
75         case 8:
76                 var->bits_per_pixel = 8;
77                 var->red.offset = 0;
78                 var->green.offset = 0;
79                 var->blue.offset = 0;
80                 var->red.length = 8;
81                 var->green.length = 8;
82                 var->blue.length = 8;
83                 break;
84         case 15:
85                 var->bits_per_pixel = 16;
86                 var->red.offset = 10;
87                 var->green.offset = 5;
88                 var->blue.offset = 0;
89                 var->red.length = 5;
90                 var->green.length = 5;
91                 var->blue.length = 5;
92                 break;
93         case 16:
94                 var->bits_per_pixel = 16;
95                 var->red.offset = 11;
96                 var->green.offset = 5;
97                 var->blue.offset = 0;
98                 var->red.length = 5;
99                 var->green.length = 6;
100                 var->blue.length = 5;
101                 break;
102         case 24:
103                 var->bits_per_pixel = 32;
104                 var->red.offset = 16;
105                 var->green.offset = 8;
106                 var->blue.offset = 0;
107                 var->red.length = 8;
108                 var->green.length = 8;
109                 var->blue.length = 8;
110                 break;
111         case 30:
112                 var->bits_per_pixel = 32;
113                 var->red.offset = 20;
114                 var->green.offset = 10;
115                 var->blue.offset = 0;
116                 var->red.length = 10;
117                 var->green.length = 10;
118                 var->blue.length = 10;
119                 break;
120         }
121 }
122
123 static void viafb_update_fix(struct fb_info *info)
124 {
125         u32 bpp = info->var.bits_per_pixel;
126
127         info->fix.visual =
128                 bpp == 8 ? FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
129         info->fix.line_length = (info->var.xres_virtual * bpp / 8 + 7) & ~7;
130 }
131
132 static void viafb_setup_fixinfo(struct fb_fix_screeninfo *fix,
133         struct viafb_par *viaparinfo)
134 {
135         memset(fix, 0, sizeof(struct fb_fix_screeninfo));
136         strcpy(fix->id, viafb_name);
137
138         fix->smem_start = viaparinfo->fbmem;
139         fix->smem_len = viaparinfo->fbmem_free;
140
141         fix->type = FB_TYPE_PACKED_PIXELS;
142         fix->type_aux = 0;
143         fix->visual = FB_VISUAL_TRUECOLOR;
144
145         fix->xpanstep = fix->ywrapstep = 0;
146         fix->ypanstep = 1;
147
148         /* Just tell the accel name */
149         viafbinfo->fix.accel = FB_ACCEL_VIA_UNICHROME;
150 }
151 static int viafb_open(struct fb_info *info, int user)
152 {
153         DEBUG_MSG(KERN_INFO "viafb_open!\n");
154         return 0;
155 }
156
157 static int viafb_release(struct fb_info *info, int user)
158 {
159         DEBUG_MSG(KERN_INFO "viafb_release!\n");
160         return 0;
161 }
162
163 static int viafb_check_var(struct fb_var_screeninfo *var,
164         struct fb_info *info)
165 {
166         int htotal, vtotal, depth;
167         struct VideoModeTable *vmode_entry;
168         struct viafb_par *ppar = info->par;
169         u32 long_refresh, line;
170
171         DEBUG_MSG(KERN_INFO "viafb_check_var!\n");
172         /* Sanity check */
173         /* HW neither support interlacte nor double-scaned mode */
174         if (var->vmode & FB_VMODE_INTERLACED || var->vmode & FB_VMODE_DOUBLE)
175                 return -EINVAL;
176
177         vmode_entry = viafb_get_mode(var->xres, var->yres);
178         if (!vmode_entry) {
179                 DEBUG_MSG(KERN_INFO
180                           "viafb: Mode %dx%dx%d not supported!!\n",
181                           var->xres, var->yres, var->bits_per_pixel);
182                 return -EINVAL;
183         }
184
185         depth = fb_get_color_depth(var, &info->fix);
186         if (!depth)
187                 depth = var->bits_per_pixel;
188
189         if (depth < 0 || depth > 32)
190                 return -EINVAL;
191         else if (!depth)
192                 depth = 24;
193         else if (depth == 15 && viafb_dual_fb && ppar->iga_path == IGA1)
194                 depth = 15;
195         else if (depth == 30)
196                 depth = 30;
197         else if (depth <= 8)
198                 depth = 8;
199         else if (depth <= 16)
200                 depth = 16;
201         else
202                 depth = 24;
203
204         viafb_fill_var_color_info(var, depth);
205         line = (var->xres_virtual * var->bits_per_pixel / 8 + 7) & ~7;
206         if (line * var->yres_virtual > ppar->memsize)
207                 return -EINVAL;
208
209         /* Based on var passed in to calculate the refresh,
210          * because our driver use some modes special.
211          */
212         htotal = var->xres + var->left_margin +
213         var->right_margin + var->hsync_len;
214         vtotal = var->yres + var->upper_margin +
215                 var->lower_margin + var->vsync_len;
216         long_refresh = 1000000000UL / var->pixclock * 1000;
217         long_refresh /= (htotal * vtotal);
218
219         viafb_refresh = viafb_get_refresh(var->xres, var->yres, long_refresh);
220
221         /* Adjust var according to our driver's own table */
222         viafb_fill_var_timing_info(var, viafb_refresh, vmode_entry);
223         if (info->var.accel_flags & FB_ACCELF_TEXT &&
224                 !ppar->shared->vdev->engine_mmio)
225                 info->var.accel_flags = 0;
226
227         return 0;
228 }
229
230 static int viafb_set_par(struct fb_info *info)
231 {
232         struct viafb_par *viapar = info->par;
233         struct VideoModeTable *vmode_entry, *vmode_entry1 = NULL;
234         DEBUG_MSG(KERN_INFO "viafb_set_par!\n");
235
236         viapar->depth = fb_get_color_depth(&info->var, &info->fix);
237         viafb_update_device_setting(viafbinfo->var.xres, viafbinfo->var.yres,
238                 viafbinfo->var.bits_per_pixel, viafb_refresh, 0);
239
240         vmode_entry = viafb_get_mode(viafbinfo->var.xres, viafbinfo->var.yres);
241         if (viafb_dual_fb) {
242                 vmode_entry1 = viafb_get_mode(viafbinfo1->var.xres,
243                         viafbinfo1->var.yres);
244                 viafb_update_device_setting(viafbinfo1->var.xres,
245                         viafbinfo1->var.yres, viafbinfo1->var.bits_per_pixel,
246                         viafb_refresh1, 1);
247         } else if (viafb_SAMM_ON == 1) {
248                 DEBUG_MSG(KERN_INFO
249                 "viafb_second_xres = %d, viafb_second_yres = %d, bpp = %d\n",
250                           viafb_second_xres, viafb_second_yres, viafb_bpp1);
251                 vmode_entry1 = viafb_get_mode(viafb_second_xres,
252                         viafb_second_yres);
253
254                 viafb_update_device_setting(viafb_second_xres,
255                         viafb_second_yres, viafb_bpp1, viafb_refresh1, 1);
256         }
257
258         if (vmode_entry) {
259                 viafb_update_fix(info);
260                 if (viafb_dual_fb && viapar->iga_path == IGA2)
261                         viafb_bpp1 = info->var.bits_per_pixel;
262                 else
263                         viafb_bpp = info->var.bits_per_pixel;
264
265                 if (info->var.accel_flags & FB_ACCELF_TEXT)
266                         info->flags &= ~FBINFO_HWACCEL_DISABLED;
267                 else
268                         info->flags |= FBINFO_HWACCEL_DISABLED;
269                 viafb_setmode(vmode_entry, info->var.bits_per_pixel,
270                         vmode_entry1, viafb_bpp1);
271                 viafb_pan_display(&info->var, info);
272         }
273
274         return 0;
275 }
276
277 /* Set one color register */
278 static int viafb_setcolreg(unsigned regno, unsigned red, unsigned green,
279 unsigned blue, unsigned transp, struct fb_info *info)
280 {
281         struct viafb_par *viapar = info->par;
282         u32 r, g, b;
283
284         if (info->fix.visual == FB_VISUAL_PSEUDOCOLOR) {
285                 if (regno > 255)
286                         return -EINVAL;
287
288                 if (!viafb_dual_fb || viapar->iga_path == IGA1)
289                         viafb_set_primary_color_register(regno, red >> 8,
290                                 green >> 8, blue >> 8);
291
292                 if (!viafb_dual_fb || viapar->iga_path == IGA2)
293                         viafb_set_secondary_color_register(regno, red >> 8,
294                                 green >> 8, blue >> 8);
295         } else {
296                 if (regno > 15)
297                         return -EINVAL;
298
299                 r = (red >> (16 - info->var.red.length))
300                         << info->var.red.offset;
301                 b = (blue >> (16 - info->var.blue.length))
302                         << info->var.blue.offset;
303                 g = (green >> (16 - info->var.green.length))
304                         << info->var.green.offset;
305                 ((u32 *) info->pseudo_palette)[regno] = r | g | b;
306         }
307
308         return 0;
309 }
310
311 static int viafb_pan_display(struct fb_var_screeninfo *var,
312         struct fb_info *info)
313 {
314         struct viafb_par *viapar = info->par;
315         u32 vram_addr = (var->yoffset * var->xres_virtual + var->xoffset)
316                 * (var->bits_per_pixel / 8) + viapar->vram_addr;
317
318         DEBUG_MSG(KERN_DEBUG "viafb_pan_display, address = %d\n", vram_addr);
319         if (!viafb_dual_fb) {
320                 via_set_primary_address(vram_addr);
321                 via_set_secondary_address(vram_addr);
322         } else if (viapar->iga_path == IGA1)
323                 via_set_primary_address(vram_addr);
324         else
325                 via_set_secondary_address(vram_addr);
326
327         return 0;
328 }
329
330 static int viafb_blank(int blank_mode, struct fb_info *info)
331 {
332         DEBUG_MSG(KERN_INFO "viafb_blank!\n");
333         /* clear DPMS setting */
334
335         switch (blank_mode) {
336         case FB_BLANK_UNBLANK:
337                 /* Screen: On, HSync: On, VSync: On */
338                 /* control CRT monitor power management */
339                 viafb_write_reg_mask(CR36, VIACR, 0x00, BIT4 + BIT5);
340                 break;
341         case FB_BLANK_HSYNC_SUSPEND:
342                 /* Screen: Off, HSync: Off, VSync: On */
343                 /* control CRT monitor power management */
344                 viafb_write_reg_mask(CR36, VIACR, 0x10, BIT4 + BIT5);
345                 break;
346         case FB_BLANK_VSYNC_SUSPEND:
347                 /* Screen: Off, HSync: On, VSync: Off */
348                 /* control CRT monitor power management */
349                 viafb_write_reg_mask(CR36, VIACR, 0x20, BIT4 + BIT5);
350                 break;
351         case FB_BLANK_POWERDOWN:
352                 /* Screen: Off, HSync: Off, VSync: Off */
353                 /* control CRT monitor power management */
354                 viafb_write_reg_mask(CR36, VIACR, 0x30, BIT4 + BIT5);
355                 break;
356         }
357
358         return 0;
359 }
360
361 static int viafb_ioctl(struct fb_info *info, u_int cmd, u_long arg)
362 {
363         union {
364                 struct viafb_ioctl_mode viamode;
365                 struct viafb_ioctl_samm viasamm;
366                 struct viafb_driver_version driver_version;
367                 struct fb_var_screeninfo sec_var;
368                 struct _panel_size_pos_info panel_pos_size_para;
369                 struct viafb_ioctl_setting viafb_setting;
370                 struct device_t active_dev;
371         } u;
372         u32 state_info = 0;
373         u32 *viafb_gamma_table;
374         char driver_name[] = "viafb";
375
376         u32 __user *argp = (u32 __user *) arg;
377         u32 gpu32;
378
379         DEBUG_MSG(KERN_INFO "viafb_ioctl: 0x%X !!\n", cmd);
380         printk(KERN_WARNING "viafb_ioctl: Please avoid this interface as it is unstable and might change or vanish at any time!\n");
381         memset(&u, 0, sizeof(u));
382
383         switch (cmd) {
384         case VIAFB_GET_CHIP_INFO:
385                 if (copy_to_user(argp, viaparinfo->chip_info,
386                                 sizeof(struct chip_information)))
387                         return -EFAULT;
388                 break;
389         case VIAFB_GET_INFO_SIZE:
390                 return put_user((u32)sizeof(struct viafb_ioctl_info), argp);
391         case VIAFB_GET_INFO:
392                 return viafb_ioctl_get_viafb_info(arg);
393         case VIAFB_HOTPLUG:
394                 return put_user(viafb_ioctl_hotplug(info->var.xres,
395                                               info->var.yres,
396                                               info->var.bits_per_pixel), argp);
397         case VIAFB_SET_HOTPLUG_FLAG:
398                 if (copy_from_user(&gpu32, argp, sizeof(gpu32)))
399                         return -EFAULT;
400                 viafb_hotplug = (gpu32) ? 1 : 0;
401                 break;
402         case VIAFB_GET_RESOLUTION:
403                 u.viamode.xres = (u32) viafb_hotplug_Xres;
404                 u.viamode.yres = (u32) viafb_hotplug_Yres;
405                 u.viamode.refresh = (u32) viafb_hotplug_refresh;
406                 u.viamode.bpp = (u32) viafb_hotplug_bpp;
407                 if (viafb_SAMM_ON == 1) {
408                         u.viamode.xres_sec = viafb_second_xres;
409                         u.viamode.yres_sec = viafb_second_yres;
410                         u.viamode.virtual_xres_sec = viafb_second_virtual_xres;
411                         u.viamode.virtual_yres_sec = viafb_second_virtual_yres;
412                         u.viamode.refresh_sec = viafb_refresh1;
413                         u.viamode.bpp_sec = viafb_bpp1;
414                 } else {
415                         u.viamode.xres_sec = 0;
416                         u.viamode.yres_sec = 0;
417                         u.viamode.virtual_xres_sec = 0;
418                         u.viamode.virtual_yres_sec = 0;
419                         u.viamode.refresh_sec = 0;
420                         u.viamode.bpp_sec = 0;
421                 }
422                 if (copy_to_user(argp, &u.viamode, sizeof(u.viamode)))
423                         return -EFAULT;
424                 break;
425         case VIAFB_GET_SAMM_INFO:
426                 u.viasamm.samm_status = viafb_SAMM_ON;
427
428                 if (viafb_SAMM_ON == 1) {
429                         if (viafb_dual_fb) {
430                                 u.viasamm.size_prim = viaparinfo->fbmem_free;
431                                 u.viasamm.size_sec = viaparinfo1->fbmem_free;
432                         } else {
433                                 if (viafb_second_size) {
434                                         u.viasamm.size_prim =
435                                             viaparinfo->fbmem_free -
436                                             viafb_second_size * 1024 * 1024;
437                                         u.viasamm.size_sec =
438                                             viafb_second_size * 1024 * 1024;
439                                 } else {
440                                         u.viasamm.size_prim =
441                                             viaparinfo->fbmem_free >> 1;
442                                         u.viasamm.size_sec =
443                                             (viaparinfo->fbmem_free >> 1);
444                                 }
445                         }
446                         u.viasamm.mem_base = viaparinfo->fbmem;
447                         u.viasamm.offset_sec = viafb_second_offset;
448                 } else {
449                         u.viasamm.size_prim =
450                             viaparinfo->memsize - viaparinfo->fbmem_used;
451                         u.viasamm.size_sec = 0;
452                         u.viasamm.mem_base = viaparinfo->fbmem;
453                         u.viasamm.offset_sec = 0;
454                 }
455
456                 if (copy_to_user(argp, &u.viasamm, sizeof(u.viasamm)))
457                         return -EFAULT;
458
459                 break;
460         case VIAFB_TURN_ON_OUTPUT_DEVICE:
461                 if (copy_from_user(&gpu32, argp, sizeof(gpu32)))
462                         return -EFAULT;
463                 if (gpu32 & CRT_Device)
464                         viafb_crt_enable();
465                 if (gpu32 & DVI_Device)
466                         viafb_dvi_enable();
467                 if (gpu32 & LCD_Device)
468                         viafb_lcd_enable();
469                 break;
470         case VIAFB_TURN_OFF_OUTPUT_DEVICE:
471                 if (copy_from_user(&gpu32, argp, sizeof(gpu32)))
472                         return -EFAULT;
473                 if (gpu32 & CRT_Device)
474                         viafb_crt_disable();
475                 if (gpu32 & DVI_Device)
476                         viafb_dvi_disable();
477                 if (gpu32 & LCD_Device)
478                         viafb_lcd_disable();
479                 break;
480         case VIAFB_SET_DEVICE:
481                 if (copy_from_user(&u.active_dev, (void *)argp,
482                         sizeof(u.active_dev)))
483                         return -EFAULT;
484                 viafb_set_device(u.active_dev);
485                 viafb_set_par(info);
486                 break;
487         case VIAFB_GET_DEVICE:
488                 u.active_dev.crt = viafb_CRT_ON;
489                 u.active_dev.dvi = viafb_DVI_ON;
490                 u.active_dev.lcd = viafb_LCD_ON;
491                 u.active_dev.samm = viafb_SAMM_ON;
492                 u.active_dev.primary_dev = viafb_primary_dev;
493
494                 u.active_dev.lcd_dsp_cent = viafb_lcd_dsp_method;
495                 u.active_dev.lcd_panel_id = viafb_lcd_panel_id;
496                 u.active_dev.lcd_mode = viafb_lcd_mode;
497
498                 u.active_dev.xres = viafb_hotplug_Xres;
499                 u.active_dev.yres = viafb_hotplug_Yres;
500
501                 u.active_dev.xres1 = viafb_second_xres;
502                 u.active_dev.yres1 = viafb_second_yres;
503
504                 u.active_dev.bpp = viafb_bpp;
505                 u.active_dev.bpp1 = viafb_bpp1;
506                 u.active_dev.refresh = viafb_refresh;
507                 u.active_dev.refresh1 = viafb_refresh1;
508
509                 u.active_dev.epia_dvi = viafb_platform_epia_dvi;
510                 u.active_dev.lcd_dual_edge = viafb_device_lcd_dualedge;
511                 u.active_dev.bus_width = viafb_bus_width;
512
513                 if (copy_to_user(argp, &u.active_dev, sizeof(u.active_dev)))
514                         return -EFAULT;
515                 break;
516
517         case VIAFB_GET_DRIVER_VERSION:
518                 u.driver_version.iMajorNum = VERSION_MAJOR;
519                 u.driver_version.iKernelNum = VERSION_KERNEL;
520                 u.driver_version.iOSNum = VERSION_OS;
521                 u.driver_version.iMinorNum = VERSION_MINOR;
522
523                 if (copy_to_user(argp, &u.driver_version,
524                         sizeof(u.driver_version)))
525                         return -EFAULT;
526
527                 break;
528
529         case VIAFB_SET_DEVICE_INFO:
530                 if (copy_from_user(&u.viafb_setting,
531                         argp, sizeof(u.viafb_setting)))
532                         return -EFAULT;
533                 if (apply_device_setting(u.viafb_setting, info) < 0)
534                         return -EINVAL;
535
536                 break;
537
538         case VIAFB_SET_SECOND_MODE:
539                 if (copy_from_user(&u.sec_var, argp, sizeof(u.sec_var)))
540                         return -EFAULT;
541                 apply_second_mode_setting(&u.sec_var);
542                 break;
543
544         case VIAFB_GET_DEVICE_INFO:
545
546                 retrieve_device_setting(&u.viafb_setting);
547
548                 if (copy_to_user(argp, &u.viafb_setting,
549                                  sizeof(u.viafb_setting)))
550                         return -EFAULT;
551
552                 break;
553
554         case VIAFB_GET_DEVICE_SUPPORT:
555                 viafb_get_device_support_state(&state_info);
556                 if (put_user(state_info, argp))
557                         return -EFAULT;
558                 break;
559
560         case VIAFB_GET_DEVICE_CONNECT:
561                 viafb_get_device_connect_state(&state_info);
562                 if (put_user(state_info, argp))
563                         return -EFAULT;
564                 break;
565
566         case VIAFB_GET_PANEL_SUPPORT_EXPAND:
567                 state_info =
568                     viafb_lcd_get_support_expand_state(info->var.xres,
569                                                  info->var.yres);
570                 if (put_user(state_info, argp))
571                         return -EFAULT;
572                 break;
573
574         case VIAFB_GET_DRIVER_NAME:
575                 if (copy_to_user(argp, driver_name, sizeof(driver_name)))
576                         return -EFAULT;
577                 break;
578
579         case VIAFB_SET_GAMMA_LUT:
580                 viafb_gamma_table = kmalloc(256 * sizeof(u32), GFP_KERNEL);
581                 if (!viafb_gamma_table)
582                         return -ENOMEM;
583                 if (copy_from_user(viafb_gamma_table, argp,
584                                 256 * sizeof(u32))) {
585                         kfree(viafb_gamma_table);
586                         return -EFAULT;
587                 }
588                 viafb_set_gamma_table(viafb_bpp, viafb_gamma_table);
589                 kfree(viafb_gamma_table);
590                 break;
591
592         case VIAFB_GET_GAMMA_LUT:
593                 viafb_gamma_table = kmalloc(256 * sizeof(u32), GFP_KERNEL);
594                 if (!viafb_gamma_table)
595                         return -ENOMEM;
596                 viafb_get_gamma_table(viafb_gamma_table);
597                 if (copy_to_user(argp, viafb_gamma_table,
598                         256 * sizeof(u32))) {
599                         kfree(viafb_gamma_table);
600                         return -EFAULT;
601                 }
602                 kfree(viafb_gamma_table);
603                 break;
604
605         case VIAFB_GET_GAMMA_SUPPORT_STATE:
606                 viafb_get_gamma_support_state(viafb_bpp, &state_info);
607                 if (put_user(state_info, argp))
608                         return -EFAULT;
609                 break;
610         case VIAFB_SYNC_SURFACE:
611                 DEBUG_MSG(KERN_INFO "lobo VIAFB_SYNC_SURFACE\n");
612                 break;
613         case VIAFB_GET_DRIVER_CAPS:
614                 break;
615
616         case VIAFB_GET_PANEL_MAX_SIZE:
617                 if (copy_from_user(&u.panel_pos_size_para, argp,
618                                    sizeof(u.panel_pos_size_para)))
619                         return -EFAULT;
620                 u.panel_pos_size_para.x = u.panel_pos_size_para.y = 0;
621                 if (copy_to_user(argp, &u.panel_pos_size_para,
622                      sizeof(u.panel_pos_size_para)))
623                         return -EFAULT;
624                 break;
625         case VIAFB_GET_PANEL_MAX_POSITION:
626                 if (copy_from_user(&u.panel_pos_size_para, argp,
627                                    sizeof(u.panel_pos_size_para)))
628                         return -EFAULT;
629                 u.panel_pos_size_para.x = u.panel_pos_size_para.y = 0;
630                 if (copy_to_user(argp, &u.panel_pos_size_para,
631                                  sizeof(u.panel_pos_size_para)))
632                         return -EFAULT;
633                 break;
634
635         case VIAFB_GET_PANEL_POSITION:
636                 if (copy_from_user(&u.panel_pos_size_para, argp,
637                                    sizeof(u.panel_pos_size_para)))
638                         return -EFAULT;
639                 u.panel_pos_size_para.x = u.panel_pos_size_para.y = 0;
640                 if (copy_to_user(argp, &u.panel_pos_size_para,
641                                  sizeof(u.panel_pos_size_para)))
642                         return -EFAULT;
643                 break;
644         case VIAFB_GET_PANEL_SIZE:
645                 if (copy_from_user(&u.panel_pos_size_para, argp,
646                                    sizeof(u.panel_pos_size_para)))
647                         return -EFAULT;
648                 u.panel_pos_size_para.x = u.panel_pos_size_para.y = 0;
649                 if (copy_to_user(argp, &u.panel_pos_size_para,
650                                  sizeof(u.panel_pos_size_para)))
651                         return -EFAULT;
652                 break;
653
654         case VIAFB_SET_PANEL_POSITION:
655                 if (copy_from_user(&u.panel_pos_size_para, argp,
656                                    sizeof(u.panel_pos_size_para)))
657                         return -EFAULT;
658                 break;
659         case VIAFB_SET_PANEL_SIZE:
660                 if (copy_from_user(&u.panel_pos_size_para, argp,
661                                    sizeof(u.panel_pos_size_para)))
662                         return -EFAULT;
663                 break;
664
665         default:
666                 return -EINVAL;
667         }
668
669         return 0;
670 }
671
672 static void viafb_fillrect(struct fb_info *info,
673         const struct fb_fillrect *rect)
674 {
675         struct viafb_par *viapar = info->par;
676         struct viafb_shared *shared = viapar->shared;
677         u32 fg_color;
678         u8 rop;
679
680         if (info->flags & FBINFO_HWACCEL_DISABLED || !shared->hw_bitblt) {
681                 cfb_fillrect(info, rect);
682                 return;
683         }
684
685         if (!rect->width || !rect->height)
686                 return;
687
688         if (info->fix.visual == FB_VISUAL_TRUECOLOR)
689                 fg_color = ((u32 *)info->pseudo_palette)[rect->color];
690         else
691                 fg_color = rect->color;
692
693         if (rect->rop == ROP_XOR)
694                 rop = 0x5A;
695         else
696                 rop = 0xF0;
697
698         DEBUG_MSG(KERN_DEBUG "viafb 2D engine: fillrect\n");
699         if (shared->hw_bitblt(shared->vdev->engine_mmio, VIA_BITBLT_FILL,
700                 rect->width, rect->height, info->var.bits_per_pixel,
701                 viapar->vram_addr, info->fix.line_length, rect->dx, rect->dy,
702                 NULL, 0, 0, 0, 0, fg_color, 0, rop))
703                 cfb_fillrect(info, rect);
704 }
705
706 static void viafb_copyarea(struct fb_info *info,
707         const struct fb_copyarea *area)
708 {
709         struct viafb_par *viapar = info->par;
710         struct viafb_shared *shared = viapar->shared;
711
712         if (info->flags & FBINFO_HWACCEL_DISABLED || !shared->hw_bitblt) {
713                 cfb_copyarea(info, area);
714                 return;
715         }
716
717         if (!area->width || !area->height)
718                 return;
719
720         DEBUG_MSG(KERN_DEBUG "viafb 2D engine: copyarea\n");
721         if (shared->hw_bitblt(shared->vdev->engine_mmio, VIA_BITBLT_COLOR,
722                 area->width, area->height, info->var.bits_per_pixel,
723                 viapar->vram_addr, info->fix.line_length, area->dx, area->dy,
724                 NULL, viapar->vram_addr, info->fix.line_length,
725                 area->sx, area->sy, 0, 0, 0))
726                 cfb_copyarea(info, area);
727 }
728
729 static void viafb_imageblit(struct fb_info *info,
730         const struct fb_image *image)
731 {
732         struct viafb_par *viapar = info->par;
733         struct viafb_shared *shared = viapar->shared;
734         u32 fg_color = 0, bg_color = 0;
735         u8 op;
736
737         if (info->flags & FBINFO_HWACCEL_DISABLED || !shared->hw_bitblt ||
738                 (image->depth != 1 && image->depth != viapar->depth)) {
739                 cfb_imageblit(info, image);
740                 return;
741         }
742
743         if (image->depth == 1) {
744                 op = VIA_BITBLT_MONO;
745                 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
746                         fg_color =
747                                 ((u32 *)info->pseudo_palette)[image->fg_color];
748                         bg_color =
749                                 ((u32 *)info->pseudo_palette)[image->bg_color];
750                 } else {
751                         fg_color = image->fg_color;
752                         bg_color = image->bg_color;
753                 }
754         } else
755                 op = VIA_BITBLT_COLOR;
756
757         DEBUG_MSG(KERN_DEBUG "viafb 2D engine: imageblit\n");
758         if (shared->hw_bitblt(shared->vdev->engine_mmio, op,
759                 image->width, image->height, info->var.bits_per_pixel,
760                 viapar->vram_addr, info->fix.line_length, image->dx, image->dy,
761                 (u32 *)image->data, 0, 0, 0, 0, fg_color, bg_color, 0))
762                 cfb_imageblit(info, image);
763 }
764
765 static int viafb_cursor(struct fb_info *info, struct fb_cursor *cursor)
766 {
767         struct viafb_par *viapar = info->par;
768         void __iomem *engine = viapar->shared->vdev->engine_mmio;
769         u32 temp, xx, yy, bg_color = 0, fg_color = 0,
770                 chip_name = viapar->shared->chip_info.gfx_chip_name;
771         int i, j = 0, cur_size = 64;
772
773         if (info->flags & FBINFO_HWACCEL_DISABLED || info != viafbinfo)
774                 return -ENODEV;
775
776         /* LCD ouput does not support hw cursors (at least on VN896) */
777         if ((chip_name == UNICHROME_CLE266 && viapar->iga_path == IGA2) ||
778                 viafb_LCD_ON)
779                 return -ENODEV;
780
781         viafb_show_hw_cursor(info, HW_Cursor_OFF);
782
783         if (cursor->set & FB_CUR_SETHOT) {
784                 temp = (cursor->hot.x << 16) + cursor->hot.y;
785                 writel(temp, engine + VIA_REG_CURSOR_ORG);
786         }
787
788         if (cursor->set & FB_CUR_SETPOS) {
789                 yy = cursor->image.dy - info->var.yoffset;
790                 xx = cursor->image.dx - info->var.xoffset;
791                 temp = yy & 0xFFFF;
792                 temp |= (xx << 16);
793                 writel(temp, engine + VIA_REG_CURSOR_POS);
794         }
795
796         if (cursor->image.width <= 32 && cursor->image.height <= 32)
797                 cur_size = 32;
798         else if (cursor->image.width <= 64 && cursor->image.height <= 64)
799                 cur_size = 64;
800         else {
801                 printk(KERN_WARNING "viafb_cursor: The cursor is too large "
802                         "%dx%d", cursor->image.width, cursor->image.height);
803                 return -ENXIO;
804         }
805
806         if (cursor->set & FB_CUR_SETSIZE) {
807                 temp = readl(engine + VIA_REG_CURSOR_MODE);
808                 if (cur_size == 32)
809                         temp |= 0x2;
810                 else
811                         temp &= ~0x2;
812
813                 writel(temp, engine + VIA_REG_CURSOR_MODE);
814         }
815
816         if (cursor->set & FB_CUR_SETCMAP) {
817                 fg_color = cursor->image.fg_color;
818                 bg_color = cursor->image.bg_color;
819                 if (chip_name == UNICHROME_CX700 ||
820                         chip_name == UNICHROME_VX800 ||
821                         chip_name == UNICHROME_VX855) {
822                         fg_color =
823                                 ((info->cmap.red[fg_color] & 0xFFC0) << 14) |
824                                 ((info->cmap.green[fg_color] & 0xFFC0) << 4) |
825                                 ((info->cmap.blue[fg_color] & 0xFFC0) >> 6);
826                         bg_color =
827                                 ((info->cmap.red[bg_color] & 0xFFC0) << 14) |
828                                 ((info->cmap.green[bg_color] & 0xFFC0) << 4) |
829                                 ((info->cmap.blue[bg_color] & 0xFFC0) >> 6);
830                 } else {
831                         fg_color =
832                                 ((info->cmap.red[fg_color] & 0xFF00) << 8) |
833                                 (info->cmap.green[fg_color] & 0xFF00) |
834                                 ((info->cmap.blue[fg_color] & 0xFF00) >> 8);
835                         bg_color =
836                                 ((info->cmap.red[bg_color] & 0xFF00) << 8) |
837                                 (info->cmap.green[bg_color] & 0xFF00) |
838                                 ((info->cmap.blue[bg_color] & 0xFF00) >> 8);
839                 }
840
841                 writel(bg_color, engine + VIA_REG_CURSOR_BG);
842                 writel(fg_color, engine + VIA_REG_CURSOR_FG);
843         }
844
845         if (cursor->set & FB_CUR_SETSHAPE) {
846                 struct {
847                         u8 data[CURSOR_SIZE];
848                         u32 bak[CURSOR_SIZE / 4];
849                 } *cr_data = kzalloc(sizeof(*cr_data), GFP_ATOMIC);
850                 int size = ((cursor->image.width + 7) >> 3) *
851                         cursor->image.height;
852
853                 if (!cr_data)
854                         return -ENOMEM;
855
856                 if (cur_size == 32) {
857                         for (i = 0; i < (CURSOR_SIZE / 4); i++) {
858                                 cr_data->bak[i] = 0x0;
859                                 cr_data->bak[i + 1] = 0xFFFFFFFF;
860                                 i += 1;
861                         }
862                 } else {
863                         for (i = 0; i < (CURSOR_SIZE / 4); i++) {
864                                 cr_data->bak[i] = 0x0;
865                                 cr_data->bak[i + 1] = 0x0;
866                                 cr_data->bak[i + 2] = 0xFFFFFFFF;
867                                 cr_data->bak[i + 3] = 0xFFFFFFFF;
868                                 i += 3;
869                         }
870                 }
871
872                 switch (cursor->rop) {
873                 case ROP_XOR:
874                         for (i = 0; i < size; i++)
875                                 cr_data->data[i] = cursor->mask[i];
876                         break;
877                 case ROP_COPY:
878
879                         for (i = 0; i < size; i++)
880                                 cr_data->data[i] = cursor->mask[i];
881                         break;
882                 default:
883                         break;
884                 }
885
886                 if (cur_size == 32) {
887                         for (i = 0; i < size; i++) {
888                                 cr_data->bak[j] = (u32) cr_data->data[i];
889                                 cr_data->bak[j + 1] = ~cr_data->bak[j];
890                                 j += 2;
891                         }
892                 } else {
893                         for (i = 0; i < size; i++) {
894                                 cr_data->bak[j] = (u32) cr_data->data[i];
895                                 cr_data->bak[j + 1] = 0x0;
896                                 cr_data->bak[j + 2] = ~cr_data->bak[j];
897                                 cr_data->bak[j + 3] = ~cr_data->bak[j + 1];
898                                 j += 4;
899                         }
900                 }
901
902                 memcpy_toio(viafbinfo->screen_base + viapar->shared->
903                         cursor_vram_addr, cr_data->bak, CURSOR_SIZE);
904                 kfree(cr_data);
905         }
906
907         if (cursor->enable)
908                 viafb_show_hw_cursor(info, HW_Cursor_ON);
909
910         return 0;
911 }
912
913 static int viafb_sync(struct fb_info *info)
914 {
915         if (!(info->flags & FBINFO_HWACCEL_DISABLED))
916                 viafb_wait_engine_idle(info);
917         return 0;
918 }
919
920 static void check_available_device_to_enable(int device_id)
921 {
922         int device_num = 0;
923
924         /* Initialize: */
925         viafb_CRT_ON = STATE_OFF;
926         viafb_DVI_ON = STATE_OFF;
927         viafb_LCD_ON = STATE_OFF;
928         viafb_LCD2_ON = STATE_OFF;
929         viafb_DeviceStatus = None_Device;
930
931         if ((device_id & CRT_Device) && (device_num < MAX_ACTIVE_DEV_NUM)) {
932                 viafb_CRT_ON = STATE_ON;
933                 device_num++;
934                 viafb_DeviceStatus |= CRT_Device;
935         }
936
937         if ((device_id & DVI_Device) && (device_num < MAX_ACTIVE_DEV_NUM)) {
938                 viafb_DVI_ON = STATE_ON;
939                 device_num++;
940                 viafb_DeviceStatus |= DVI_Device;
941         }
942
943         if ((device_id & LCD_Device) && (device_num < MAX_ACTIVE_DEV_NUM)) {
944                 viafb_LCD_ON = STATE_ON;
945                 device_num++;
946                 viafb_DeviceStatus |= LCD_Device;
947         }
948
949         if ((device_id & LCD2_Device) && (device_num < MAX_ACTIVE_DEV_NUM)) {
950                 viafb_LCD2_ON = STATE_ON;
951                 device_num++;
952                 viafb_DeviceStatus |= LCD2_Device;
953         }
954
955         if (viafb_DeviceStatus == None_Device) {
956                 /* Use CRT as default active device: */
957                 viafb_CRT_ON = STATE_ON;
958                 viafb_DeviceStatus = CRT_Device;
959         }
960         DEBUG_MSG(KERN_INFO "Device Status:%x", viafb_DeviceStatus);
961 }
962
963 static void viafb_set_device(struct device_t active_dev)
964 {
965         /* Check available device to enable: */
966         int device_id = None_Device;
967         if (active_dev.crt)
968                 device_id |= CRT_Device;
969         if (active_dev.dvi)
970                 device_id |= DVI_Device;
971         if (active_dev.lcd)
972                 device_id |= LCD_Device;
973
974         check_available_device_to_enable(device_id);
975
976         /* Check property of LCD: */
977         if (viafb_LCD_ON) {
978                 if (active_dev.lcd_dsp_cent) {
979                         viaparinfo->lvds_setting_info->display_method =
980                                 viafb_lcd_dsp_method = LCD_CENTERING;
981                 } else {
982                         viaparinfo->lvds_setting_info->display_method =
983                                 viafb_lcd_dsp_method = LCD_EXPANDSION;
984                 }
985
986                 if (active_dev.lcd_mode == LCD_SPWG) {
987                         viaparinfo->lvds_setting_info->lcd_mode =
988                                 viafb_lcd_mode = LCD_SPWG;
989                 } else {
990                         viaparinfo->lvds_setting_info->lcd_mode =
991                                 viafb_lcd_mode = LCD_OPENLDI;
992                 }
993
994                 if (active_dev.lcd_panel_id <= LCD_PANEL_ID_MAXIMUM) {
995                         viafb_lcd_panel_id = active_dev.lcd_panel_id;
996                         viafb_init_lcd_size();
997                 }
998         }
999
1000         /* Check property of mode: */
1001         if (!active_dev.xres1)
1002                 viafb_second_xres = 640;
1003         else
1004                 viafb_second_xres = active_dev.xres1;
1005         if (!active_dev.yres1)
1006                 viafb_second_yres = 480;
1007         else
1008                 viafb_second_yres = active_dev.yres1;
1009         if (active_dev.bpp != 0)
1010                 viafb_bpp = active_dev.bpp;
1011         if (active_dev.bpp1 != 0)
1012                 viafb_bpp1 = active_dev.bpp1;
1013         if (active_dev.refresh != 0)
1014                 viafb_refresh = active_dev.refresh;
1015         if (active_dev.refresh1 != 0)
1016                 viafb_refresh1 = active_dev.refresh1;
1017         if ((active_dev.samm == STATE_OFF) || (active_dev.samm == STATE_ON))
1018                 viafb_SAMM_ON = active_dev.samm;
1019         viafb_primary_dev = active_dev.primary_dev;
1020
1021         via_set_primary_address(0);
1022         via_set_secondary_address(viafb_SAMM_ON ? viafb_second_offset : 0);
1023         viafb_set_iga_path();
1024 }
1025
1026 static int get_primary_device(void)
1027 {
1028         int primary_device = 0;
1029         /* Rule: device on iga1 path are the primary device. */
1030         if (viafb_SAMM_ON) {
1031                 if (viafb_CRT_ON) {
1032                         if (viaparinfo->crt_setting_info->iga_path == IGA1) {
1033                                 DEBUG_MSG(KERN_INFO "CRT IGA Path:%d\n",
1034                                         viaparinfo->
1035                                         crt_setting_info->iga_path);
1036                                 primary_device = CRT_Device;
1037                         }
1038                 }
1039                 if (viafb_DVI_ON) {
1040                         if (viaparinfo->tmds_setting_info->iga_path == IGA1) {
1041                                 DEBUG_MSG(KERN_INFO "DVI IGA Path:%d\n",
1042                                         viaparinfo->
1043                                         tmds_setting_info->iga_path);
1044                                 primary_device = DVI_Device;
1045                         }
1046                 }
1047                 if (viafb_LCD_ON) {
1048                         if (viaparinfo->lvds_setting_info->iga_path == IGA1) {
1049                                 DEBUG_MSG(KERN_INFO "LCD IGA Path:%d\n",
1050                                         viaparinfo->
1051                                         lvds_setting_info->iga_path);
1052                                 primary_device = LCD_Device;
1053                         }
1054                 }
1055                 if (viafb_LCD2_ON) {
1056                         if (viaparinfo->lvds_setting_info2->iga_path == IGA1) {
1057                                 DEBUG_MSG(KERN_INFO "LCD2 IGA Path:%d\n",
1058                                         viaparinfo->
1059                                         lvds_setting_info2->iga_path);
1060                                 primary_device = LCD2_Device;
1061                         }
1062                 }
1063         }
1064         return primary_device;
1065 }
1066
1067 static void apply_second_mode_setting(struct fb_var_screeninfo
1068         *sec_var)
1069 {
1070         u32 htotal, vtotal, long_refresh;
1071
1072         htotal = sec_var->xres + sec_var->left_margin +
1073                 sec_var->right_margin + sec_var->hsync_len;
1074         vtotal = sec_var->yres + sec_var->upper_margin +
1075                 sec_var->lower_margin + sec_var->vsync_len;
1076         if ((sec_var->xres_virtual * (sec_var->bits_per_pixel >> 3)) & 0x1F) {
1077                 /*Is 32 bytes alignment? */
1078                 /*32 pixel alignment */
1079                 sec_var->xres_virtual = (sec_var->xres_virtual + 31) & ~31;
1080         }
1081
1082         htotal = sec_var->xres + sec_var->left_margin +
1083                 sec_var->right_margin + sec_var->hsync_len;
1084         vtotal = sec_var->yres + sec_var->upper_margin +
1085                 sec_var->lower_margin + sec_var->vsync_len;
1086         long_refresh = 1000000000UL / sec_var->pixclock * 1000;
1087         long_refresh /= (htotal * vtotal);
1088
1089         viafb_second_xres = sec_var->xres;
1090         viafb_second_yres = sec_var->yres;
1091         viafb_second_virtual_xres = sec_var->xres_virtual;
1092         viafb_second_virtual_yres = sec_var->yres_virtual;
1093         viafb_bpp1 = sec_var->bits_per_pixel;
1094         viafb_refresh1 = viafb_get_refresh(sec_var->xres, sec_var->yres,
1095                 long_refresh);
1096 }
1097
1098 static int apply_device_setting(struct viafb_ioctl_setting setting_info,
1099         struct fb_info *info)
1100 {
1101         int need_set_mode = 0;
1102         DEBUG_MSG(KERN_INFO "apply_device_setting\n");
1103
1104         if (setting_info.device_flag) {
1105                 need_set_mode = 1;
1106                 check_available_device_to_enable(setting_info.device_status);
1107         }
1108
1109         /* Unlock LCD's operation according to LCD flag
1110            and check if the setting value is valid. */
1111         /* If the value is valid, apply the new setting value to the device. */
1112         if (viafb_LCD_ON) {
1113                 if (setting_info.lcd_operation_flag & OP_LCD_CENTERING) {
1114                         need_set_mode = 1;
1115                         if (setting_info.lcd_attributes.display_center) {
1116                                 /* Centering */
1117                                 viaparinfo->lvds_setting_info->display_method =
1118                                     LCD_CENTERING;
1119                                 viafb_lcd_dsp_method = LCD_CENTERING;
1120                                 viaparinfo->lvds_setting_info2->display_method =
1121                                     viafb_lcd_dsp_method = LCD_CENTERING;
1122                         } else {
1123                                 /* expandsion */
1124                                 viaparinfo->lvds_setting_info->display_method =
1125                                     LCD_EXPANDSION;
1126                                 viafb_lcd_dsp_method = LCD_EXPANDSION;
1127                                 viaparinfo->lvds_setting_info2->display_method =
1128                                     LCD_EXPANDSION;
1129                                 viafb_lcd_dsp_method = LCD_EXPANDSION;
1130                         }
1131                 }
1132
1133                 if (setting_info.lcd_operation_flag & OP_LCD_MODE) {
1134                         need_set_mode = 1;
1135                         if (setting_info.lcd_attributes.lcd_mode ==
1136                                 LCD_SPWG) {
1137                                 viaparinfo->lvds_setting_info->lcd_mode =
1138                                         viafb_lcd_mode = LCD_SPWG;
1139                         } else {
1140                                 viaparinfo->lvds_setting_info->lcd_mode =
1141                                         viafb_lcd_mode = LCD_OPENLDI;
1142                         }
1143                         viaparinfo->lvds_setting_info2->lcd_mode =
1144                             viaparinfo->lvds_setting_info->lcd_mode;
1145                 }
1146
1147                 if (setting_info.lcd_operation_flag & OP_LCD_PANEL_ID) {
1148                         need_set_mode = 1;
1149                         if (setting_info.lcd_attributes.panel_id <=
1150                             LCD_PANEL_ID_MAXIMUM) {
1151                                 viafb_lcd_panel_id =
1152                                     setting_info.lcd_attributes.panel_id;
1153                                 viafb_init_lcd_size();
1154                         }
1155                 }
1156         }
1157
1158         if (0 != (setting_info.samm_status & OP_SAMM)) {
1159                 setting_info.samm_status =
1160                     setting_info.samm_status & (~OP_SAMM);
1161                 if (setting_info.samm_status == 0
1162                     || setting_info.samm_status == 1) {
1163                         viafb_SAMM_ON = setting_info.samm_status;
1164
1165                         if (viafb_SAMM_ON)
1166                                 viafb_primary_dev = setting_info.primary_device;
1167
1168                         via_set_primary_address(0);
1169                         via_set_secondary_address(viafb_SAMM_ON ?
1170                                 viafb_second_offset : 0);
1171                         viafb_set_iga_path();
1172                 }
1173                 need_set_mode = 1;
1174         }
1175
1176         if (!need_set_mode) {
1177                 ;
1178         } else {
1179                 viafb_set_iga_path();
1180                 viafb_set_par(info);
1181         }
1182         return true;
1183 }
1184
1185 static void retrieve_device_setting(struct viafb_ioctl_setting
1186         *setting_info)
1187 {
1188
1189         /* get device status */
1190         if (viafb_CRT_ON == 1)
1191                 setting_info->device_status = CRT_Device;
1192         if (viafb_DVI_ON == 1)
1193                 setting_info->device_status |= DVI_Device;
1194         if (viafb_LCD_ON == 1)
1195                 setting_info->device_status |= LCD_Device;
1196         if (viafb_LCD2_ON == 1)
1197                 setting_info->device_status |= LCD2_Device;
1198
1199         setting_info->samm_status = viafb_SAMM_ON;
1200         setting_info->primary_device = get_primary_device();
1201
1202         setting_info->first_dev_bpp = viafb_bpp;
1203         setting_info->second_dev_bpp = viafb_bpp1;
1204
1205         setting_info->first_dev_refresh = viafb_refresh;
1206         setting_info->second_dev_refresh = viafb_refresh1;
1207
1208         setting_info->first_dev_hor_res = viafb_hotplug_Xres;
1209         setting_info->first_dev_ver_res = viafb_hotplug_Yres;
1210         setting_info->second_dev_hor_res = viafb_second_xres;
1211         setting_info->second_dev_ver_res = viafb_second_yres;
1212
1213         /* Get lcd attributes */
1214         setting_info->lcd_attributes.display_center = viafb_lcd_dsp_method;
1215         setting_info->lcd_attributes.panel_id = viafb_lcd_panel_id;
1216         setting_info->lcd_attributes.lcd_mode = viafb_lcd_mode;
1217 }
1218
1219 static int parse_active_dev(void)
1220 {
1221         viafb_CRT_ON = STATE_OFF;
1222         viafb_DVI_ON = STATE_OFF;
1223         viafb_LCD_ON = STATE_OFF;
1224         viafb_LCD2_ON = STATE_OFF;
1225         /* 1. Modify the active status of devices. */
1226         /* 2. Keep the order of devices, so we can set corresponding
1227            IGA path to devices in SAMM case. */
1228         /*    Note: The previous of active_dev is primary device,
1229            and the following is secondary device. */
1230         if (!viafb_active_dev) {
1231                 viafb_CRT_ON = STATE_ON;
1232                 viafb_SAMM_ON = STATE_OFF;
1233         } else if (!strcmp(viafb_active_dev, "CRT+DVI")) {
1234                 /* CRT+DVI */
1235                 viafb_CRT_ON = STATE_ON;
1236                 viafb_DVI_ON = STATE_ON;
1237                 viafb_primary_dev = CRT_Device;
1238         } else if (!strcmp(viafb_active_dev, "DVI+CRT")) {
1239                 /* DVI+CRT */
1240                 viafb_CRT_ON = STATE_ON;
1241                 viafb_DVI_ON = STATE_ON;
1242                 viafb_primary_dev = DVI_Device;
1243         } else if (!strcmp(viafb_active_dev, "CRT+LCD")) {
1244                 /* CRT+LCD */
1245                 viafb_CRT_ON = STATE_ON;
1246                 viafb_LCD_ON = STATE_ON;
1247                 viafb_primary_dev = CRT_Device;
1248         } else if (!strcmp(viafb_active_dev, "LCD+CRT")) {
1249                 /* LCD+CRT */
1250                 viafb_CRT_ON = STATE_ON;
1251                 viafb_LCD_ON = STATE_ON;
1252                 viafb_primary_dev = LCD_Device;
1253         } else if (!strcmp(viafb_active_dev, "DVI+LCD")) {
1254                 /* DVI+LCD */
1255                 viafb_DVI_ON = STATE_ON;
1256                 viafb_LCD_ON = STATE_ON;
1257                 viafb_primary_dev = DVI_Device;
1258         } else if (!strcmp(viafb_active_dev, "LCD+DVI")) {
1259                 /* LCD+DVI */
1260                 viafb_DVI_ON = STATE_ON;
1261                 viafb_LCD_ON = STATE_ON;
1262                 viafb_primary_dev = LCD_Device;
1263         } else if (!strcmp(viafb_active_dev, "LCD+LCD2")) {
1264                 viafb_LCD_ON = STATE_ON;
1265                 viafb_LCD2_ON = STATE_ON;
1266                 viafb_primary_dev = LCD_Device;
1267         } else if (!strcmp(viafb_active_dev, "LCD2+LCD")) {
1268                 viafb_LCD_ON = STATE_ON;
1269                 viafb_LCD2_ON = STATE_ON;
1270                 viafb_primary_dev = LCD2_Device;
1271         } else if (!strcmp(viafb_active_dev, "CRT")) {
1272                 /* CRT only */
1273                 viafb_CRT_ON = STATE_ON;
1274                 viafb_SAMM_ON = STATE_OFF;
1275         } else if (!strcmp(viafb_active_dev, "DVI")) {
1276                 /* DVI only */
1277                 viafb_DVI_ON = STATE_ON;
1278                 viafb_SAMM_ON = STATE_OFF;
1279         } else if (!strcmp(viafb_active_dev, "LCD")) {
1280                 /* LCD only */
1281                 viafb_LCD_ON = STATE_ON;
1282                 viafb_SAMM_ON = STATE_OFF;
1283         } else
1284                 return -EINVAL;
1285
1286         return 0;
1287 }
1288
1289 static int parse_port(char *opt_str, int *output_interface)
1290 {
1291         if (!strncmp(opt_str, "DVP0", 4))
1292                 *output_interface = INTERFACE_DVP0;
1293         else if (!strncmp(opt_str, "DVP1", 4))
1294                 *output_interface = INTERFACE_DVP1;
1295         else if (!strncmp(opt_str, "DFP_HIGHLOW", 11))
1296                 *output_interface = INTERFACE_DFP;
1297         else if (!strncmp(opt_str, "DFP_HIGH", 8))
1298                 *output_interface = INTERFACE_DFP_HIGH;
1299         else if (!strncmp(opt_str, "DFP_LOW", 7))
1300                 *output_interface = INTERFACE_DFP_LOW;
1301         else
1302                 *output_interface = INTERFACE_NONE;
1303         return 0;
1304 }
1305
1306 static void parse_lcd_port(void)
1307 {
1308         parse_port(viafb_lcd_port, &viaparinfo->chip_info->lvds_chip_info.
1309                 output_interface);
1310         /*Initialize to avoid unexpected behavior */
1311         viaparinfo->chip_info->lvds_chip_info2.output_interface =
1312         INTERFACE_NONE;
1313
1314         DEBUG_MSG(KERN_INFO "parse_lcd_port: viafb_lcd_port:%s,interface:%d\n",
1315                   viafb_lcd_port, viaparinfo->chip_info->lvds_chip_info.
1316                   output_interface);
1317 }
1318
1319 static void parse_dvi_port(void)
1320 {
1321         parse_port(viafb_dvi_port, &viaparinfo->chip_info->tmds_chip_info.
1322                 output_interface);
1323
1324         DEBUG_MSG(KERN_INFO "parse_dvi_port: viafb_dvi_port:%s,interface:%d\n",
1325                   viafb_dvi_port, viaparinfo->chip_info->tmds_chip_info.
1326                   output_interface);
1327 }
1328
1329 #ifdef CONFIG_FB_VIA_DIRECT_PROCFS
1330
1331 /*
1332  * The proc filesystem read/write function, a simple proc implement to
1333  * get/set the value of DPA  DVP0,   DVP0DataDriving,  DVP0ClockDriving, DVP1,
1334  * DVP1Driving, DFPHigh, DFPLow CR96,   SR2A[5], SR1B[1], SR2A[4], SR1E[2],
1335  * CR9B,    SR65,    CR97,    CR99
1336  */
1337 static int viafb_dvp0_proc_show(struct seq_file *m, void *v)
1338 {
1339         u8 dvp0_data_dri = 0, dvp0_clk_dri = 0, dvp0 = 0;
1340         dvp0_data_dri =
1341             (viafb_read_reg(VIASR, SR2A) & BIT5) >> 4 |
1342             (viafb_read_reg(VIASR, SR1B) & BIT1) >> 1;
1343         dvp0_clk_dri =
1344             (viafb_read_reg(VIASR, SR2A) & BIT4) >> 3 |
1345             (viafb_read_reg(VIASR, SR1E) & BIT2) >> 2;
1346         dvp0 = viafb_read_reg(VIACR, CR96) & 0x0f;
1347         seq_printf(m, "%x %x %x\n", dvp0, dvp0_data_dri, dvp0_clk_dri);
1348         return 0;
1349 }
1350
1351 static int viafb_dvp0_proc_open(struct inode *inode, struct file *file)
1352 {
1353         return single_open(file, viafb_dvp0_proc_show, NULL);
1354 }
1355
1356 static ssize_t viafb_dvp0_proc_write(struct file *file,
1357         const char __user *buffer, size_t count, loff_t *pos)
1358 {
1359         char buf[20], *value, *pbuf;
1360         u8 reg_val = 0;
1361         unsigned long length, i;
1362         if (count < 1)
1363                 return -EINVAL;
1364         length = count > 20 ? 20 : count;
1365         if (copy_from_user(&buf[0], buffer, length))
1366                 return -EFAULT;
1367         buf[length - 1] = '\0'; /*Ensure end string */
1368         pbuf = &buf[0];
1369         for (i = 0; i < 3; i++) {
1370                 value = strsep(&pbuf, " ");
1371                 if (value != NULL) {
1372                         strict_strtoul(value, 0, (unsigned long *)&reg_val);
1373                         DEBUG_MSG(KERN_INFO "DVP0:reg_val[%l]=:%x\n", i,
1374                                   reg_val);
1375                         switch (i) {
1376                         case 0:
1377                                 viafb_write_reg_mask(CR96, VIACR,
1378                                         reg_val, 0x0f);
1379                                 break;
1380                         case 1:
1381                                 viafb_write_reg_mask(SR2A, VIASR,
1382                                         reg_val << 4, BIT5);
1383                                 viafb_write_reg_mask(SR1B, VIASR,
1384                                         reg_val << 1, BIT1);
1385                                 break;
1386                         case 2:
1387                                 viafb_write_reg_mask(SR2A, VIASR,
1388                                         reg_val << 3, BIT4);
1389                                 viafb_write_reg_mask(SR1E, VIASR,
1390                                         reg_val << 2, BIT2);
1391                                 break;
1392                         default:
1393                                 break;
1394                         }
1395                 } else {
1396                         break;
1397                 }
1398         }
1399         return count;
1400 }
1401
1402 static const struct file_operations viafb_dvp0_proc_fops = {
1403         .owner          = THIS_MODULE,
1404         .open           = viafb_dvp0_proc_open,
1405         .read           = seq_read,
1406         .llseek         = seq_lseek,
1407         .release        = single_release,
1408         .write          = viafb_dvp0_proc_write,
1409 };
1410
1411 static int viafb_dvp1_proc_show(struct seq_file *m, void *v)
1412 {
1413         u8 dvp1 = 0, dvp1_data_dri = 0, dvp1_clk_dri = 0;
1414         dvp1 = viafb_read_reg(VIACR, CR9B) & 0x0f;
1415         dvp1_data_dri = (viafb_read_reg(VIASR, SR65) & 0x0c) >> 2;
1416         dvp1_clk_dri = viafb_read_reg(VIASR, SR65) & 0x03;
1417         seq_printf(m, "%x %x %x\n", dvp1, dvp1_data_dri, dvp1_clk_dri);
1418         return 0;
1419 }
1420
1421 static int viafb_dvp1_proc_open(struct inode *inode, struct file *file)
1422 {
1423         return single_open(file, viafb_dvp1_proc_show, NULL);
1424 }
1425
1426 static ssize_t viafb_dvp1_proc_write(struct file *file,
1427         const char __user *buffer, size_t count, loff_t *pos)
1428 {
1429         char buf[20], *value, *pbuf;
1430         u8 reg_val = 0;
1431         unsigned long length, i;
1432         if (count < 1)
1433                 return -EINVAL;
1434         length = count > 20 ? 20 : count;
1435         if (copy_from_user(&buf[0], buffer, length))
1436                 return -EFAULT;
1437         buf[length - 1] = '\0'; /*Ensure end string */
1438         pbuf = &buf[0];
1439         for (i = 0; i < 3; i++) {
1440                 value = strsep(&pbuf, " ");
1441                 if (value != NULL) {
1442                         strict_strtoul(value, 0, (unsigned long *)&reg_val);
1443                         switch (i) {
1444                         case 0:
1445                                 viafb_write_reg_mask(CR9B, VIACR,
1446                                         reg_val, 0x0f);
1447                                 break;
1448                         case 1:
1449                                 viafb_write_reg_mask(SR65, VIASR,
1450                                         reg_val << 2, 0x0c);
1451                                 break;
1452                         case 2:
1453                                 viafb_write_reg_mask(SR65, VIASR,
1454                                         reg_val, 0x03);
1455                                 break;
1456                         default:
1457                                 break;
1458                         }
1459                 } else {
1460                         break;
1461                 }
1462         }
1463         return count;
1464 }
1465
1466 static const struct file_operations viafb_dvp1_proc_fops = {
1467         .owner          = THIS_MODULE,
1468         .open           = viafb_dvp1_proc_open,
1469         .read           = seq_read,
1470         .llseek         = seq_lseek,
1471         .release        = single_release,
1472         .write          = viafb_dvp1_proc_write,
1473 };
1474
1475 static int viafb_dfph_proc_show(struct seq_file *m, void *v)
1476 {
1477         u8 dfp_high = 0;
1478         dfp_high = viafb_read_reg(VIACR, CR97) & 0x0f;
1479         seq_printf(m, "%x\n", dfp_high);
1480         return 0;
1481 }
1482
1483 static int viafb_dfph_proc_open(struct inode *inode, struct file *file)
1484 {
1485         return single_open(file, viafb_dfph_proc_show, NULL);
1486 }
1487
1488 static ssize_t viafb_dfph_proc_write(struct file *file,
1489         const char __user *buffer, size_t count, loff_t *pos)
1490 {
1491         char buf[20];
1492         u8 reg_val = 0;
1493         unsigned long length;
1494         if (count < 1)
1495                 return -EINVAL;
1496         length = count > 20 ? 20 : count;
1497         if (copy_from_user(&buf[0], buffer, length))
1498                 return -EFAULT;
1499         buf[length - 1] = '\0'; /*Ensure end string */
1500         strict_strtoul(&buf[0], 0, (unsigned long *)&reg_val);
1501         viafb_write_reg_mask(CR97, VIACR, reg_val, 0x0f);
1502         return count;
1503 }
1504
1505 static const struct file_operations viafb_dfph_proc_fops = {
1506         .owner          = THIS_MODULE,
1507         .open           = viafb_dfph_proc_open,
1508         .read           = seq_read,
1509         .llseek         = seq_lseek,
1510         .release        = single_release,
1511         .write          = viafb_dfph_proc_write,
1512 };
1513
1514 static int viafb_dfpl_proc_show(struct seq_file *m, void *v)
1515 {
1516         u8 dfp_low = 0;
1517         dfp_low = viafb_read_reg(VIACR, CR99) & 0x0f;
1518         seq_printf(m, "%x\n", dfp_low);
1519         return 0;
1520 }
1521
1522 static int viafb_dfpl_proc_open(struct inode *inode, struct file *file)
1523 {
1524         return single_open(file, viafb_dfpl_proc_show, NULL);
1525 }
1526
1527 static ssize_t viafb_dfpl_proc_write(struct file *file,
1528         const char __user *buffer, size_t count, loff_t *pos)
1529 {
1530         char buf[20];
1531         u8 reg_val = 0;
1532         unsigned long length;
1533         if (count < 1)
1534                 return -EINVAL;
1535         length = count > 20 ? 20 : count;
1536         if (copy_from_user(&buf[0], buffer, length))
1537                 return -EFAULT;
1538         buf[length - 1] = '\0'; /*Ensure end string */
1539         strict_strtoul(&buf[0], 0, (unsigned long *)&reg_val);
1540         viafb_write_reg_mask(CR99, VIACR, reg_val, 0x0f);
1541         return count;
1542 }
1543
1544 static const struct file_operations viafb_dfpl_proc_fops = {
1545         .owner          = THIS_MODULE,
1546         .open           = viafb_dfpl_proc_open,
1547         .read           = seq_read,
1548         .llseek         = seq_lseek,
1549         .release        = single_release,
1550         .write          = viafb_dfpl_proc_write,
1551 };
1552
1553 static int viafb_vt1636_proc_show(struct seq_file *m, void *v)
1554 {
1555         u8 vt1636_08 = 0, vt1636_09 = 0;
1556         switch (viaparinfo->chip_info->lvds_chip_info.lvds_chip_name) {
1557         case VT1636_LVDS:
1558                 vt1636_08 =
1559                     viafb_gpio_i2c_read_lvds(viaparinfo->lvds_setting_info,
1560                     &viaparinfo->chip_info->lvds_chip_info, 0x08) & 0x0f;
1561                 vt1636_09 =
1562                     viafb_gpio_i2c_read_lvds(viaparinfo->lvds_setting_info,
1563                     &viaparinfo->chip_info->lvds_chip_info, 0x09) & 0x1f;
1564                 seq_printf(m, "%x %x\n", vt1636_08, vt1636_09);
1565                 break;
1566         default:
1567                 break;
1568         }
1569         switch (viaparinfo->chip_info->lvds_chip_info2.lvds_chip_name) {
1570         case VT1636_LVDS:
1571                 vt1636_08 =
1572                     viafb_gpio_i2c_read_lvds(viaparinfo->lvds_setting_info2,
1573                         &viaparinfo->chip_info->lvds_chip_info2, 0x08) & 0x0f;
1574                 vt1636_09 =
1575                     viafb_gpio_i2c_read_lvds(viaparinfo->lvds_setting_info2,
1576                         &viaparinfo->chip_info->lvds_chip_info2, 0x09) & 0x1f;
1577                 seq_printf(m, " %x %x\n", vt1636_08, vt1636_09);
1578                 break;
1579         default:
1580                 break;
1581         }
1582         return 0;
1583 }
1584
1585 static int viafb_vt1636_proc_open(struct inode *inode, struct file *file)
1586 {
1587         return single_open(file, viafb_vt1636_proc_show, NULL);
1588 }
1589
1590 static ssize_t viafb_vt1636_proc_write(struct file *file,
1591         const char __user *buffer, size_t count, loff_t *pos)
1592 {
1593         char buf[30], *value, *pbuf;
1594         struct IODATA reg_val;
1595         unsigned long length, i;
1596         if (count < 1)
1597                 return -EINVAL;
1598         length = count > 30 ? 30 : count;
1599         if (copy_from_user(&buf[0], buffer, length))
1600                 return -EFAULT;
1601         buf[length - 1] = '\0'; /*Ensure end string */
1602         pbuf = &buf[0];
1603         switch (viaparinfo->chip_info->lvds_chip_info.lvds_chip_name) {
1604         case VT1636_LVDS:
1605                 for (i = 0; i < 2; i++) {
1606                         value = strsep(&pbuf, " ");
1607                         if (value != NULL) {
1608                                 strict_strtoul(value, 0,
1609                                         (unsigned long *)&reg_val.Data);
1610                                 switch (i) {
1611                                 case 0:
1612                                         reg_val.Index = 0x08;
1613                                         reg_val.Mask = 0x0f;
1614                                         viafb_gpio_i2c_write_mask_lvds
1615                                             (viaparinfo->lvds_setting_info,
1616                                             &viaparinfo->
1617                                             chip_info->lvds_chip_info,
1618                                              reg_val);
1619                                         break;
1620                                 case 1:
1621                                         reg_val.Index = 0x09;
1622                                         reg_val.Mask = 0x1f;
1623                                         viafb_gpio_i2c_write_mask_lvds
1624                                             (viaparinfo->lvds_setting_info,
1625                                             &viaparinfo->
1626                                             chip_info->lvds_chip_info,
1627                                              reg_val);
1628                                         break;
1629                                 default:
1630                                         break;
1631                                 }
1632                         } else {
1633                                 break;
1634                         }
1635                 }
1636                 break;
1637         default:
1638                 break;
1639         }
1640         switch (viaparinfo->chip_info->lvds_chip_info2.lvds_chip_name) {
1641         case VT1636_LVDS:
1642                 for (i = 0; i < 2; i++) {
1643                         value = strsep(&pbuf, " ");
1644                         if (value != NULL) {
1645                                 strict_strtoul(value, 0,
1646                                         (unsigned long *)&reg_val.Data);
1647                                 switch (i) {
1648                                 case 0:
1649                                         reg_val.Index = 0x08;
1650                                         reg_val.Mask = 0x0f;
1651                                         viafb_gpio_i2c_write_mask_lvds
1652                                             (viaparinfo->lvds_setting_info2,
1653                                             &viaparinfo->
1654                                             chip_info->lvds_chip_info2,
1655                                              reg_val);
1656                                         break;
1657                                 case 1:
1658                                         reg_val.Index = 0x09;
1659                                         reg_val.Mask = 0x1f;
1660                                         viafb_gpio_i2c_write_mask_lvds
1661                                             (viaparinfo->lvds_setting_info2,
1662                                             &viaparinfo->
1663                                             chip_info->lvds_chip_info2,
1664                                              reg_val);
1665                                         break;
1666                                 default:
1667                                         break;
1668                                 }
1669                         } else {
1670                                 break;
1671                         }
1672                 }
1673                 break;
1674         default:
1675                 break;
1676         }
1677         return count;
1678 }
1679
1680 static const struct file_operations viafb_vt1636_proc_fops = {
1681         .owner          = THIS_MODULE,
1682         .open           = viafb_vt1636_proc_open,
1683         .read           = seq_read,
1684         .llseek         = seq_lseek,
1685         .release        = single_release,
1686         .write          = viafb_vt1636_proc_write,
1687 };
1688
1689 static void viafb_init_proc(struct proc_dir_entry **viafb_entry)
1690 {
1691         *viafb_entry = proc_mkdir("viafb", NULL);
1692         if (*viafb_entry) {
1693                 proc_create("dvp0", 0, *viafb_entry, &viafb_dvp0_proc_fops);
1694                 proc_create("dvp1", 0, *viafb_entry, &viafb_dvp1_proc_fops);
1695                 proc_create("dfph", 0, *viafb_entry, &viafb_dfph_proc_fops);
1696                 proc_create("dfpl", 0, *viafb_entry, &viafb_dfpl_proc_fops);
1697                 if (VT1636_LVDS == viaparinfo->chip_info->lvds_chip_info.
1698                         lvds_chip_name || VT1636_LVDS ==
1699                     viaparinfo->chip_info->lvds_chip_info2.lvds_chip_name) {
1700                         proc_create("vt1636", 0, *viafb_entry, &viafb_vt1636_proc_fops);
1701                 }
1702
1703         }
1704 }
1705 static void viafb_remove_proc(struct proc_dir_entry *viafb_entry)
1706 {
1707         struct chip_information *chip_info = &viaparinfo->shared->chip_info;
1708
1709         remove_proc_entry("dvp0", viafb_entry);/* parent dir */
1710         remove_proc_entry("dvp1", viafb_entry);
1711         remove_proc_entry("dfph", viafb_entry);
1712         remove_proc_entry("dfpl", viafb_entry);
1713         if (chip_info->lvds_chip_info.lvds_chip_name == VT1636_LVDS
1714                 || chip_info->lvds_chip_info2.lvds_chip_name == VT1636_LVDS)
1715                 remove_proc_entry("vt1636", viafb_entry);
1716
1717         remove_proc_entry("viafb", NULL);
1718 }
1719
1720 #endif /* CONFIG_FB_VIA_DIRECT_PROCFS */
1721
1722 static int parse_mode(const char *str, u32 *xres, u32 *yres)
1723 {
1724         char *ptr;
1725
1726         if (!str) {
1727                 *xres = 640;
1728                 *yres = 480;
1729                 return 0;
1730         }
1731
1732         *xres = simple_strtoul(str, &ptr, 10);
1733         if (ptr[0] != 'x')
1734                 return -EINVAL;
1735
1736         *yres = simple_strtoul(&ptr[1], &ptr, 10);
1737         if (ptr[0])
1738                 return -EINVAL;
1739
1740         return 0;
1741 }
1742
1743
1744 int __devinit via_fb_pci_probe(struct viafb_dev *vdev)
1745 {
1746         u32 default_xres, default_yres;
1747         struct VideoModeTable *vmode_entry;
1748         struct fb_var_screeninfo default_var;
1749         int rc;
1750         u32 viafb_par_length;
1751
1752         DEBUG_MSG(KERN_INFO "VIAFB PCI Probe!!\n");
1753         memset(&default_var, 0, sizeof(default_var));
1754         viafb_par_length = ALIGN(sizeof(struct viafb_par), BITS_PER_LONG/8);
1755
1756         /* Allocate fb_info and ***_par here, also including some other needed
1757          * variables
1758         */
1759         viafbinfo = framebuffer_alloc(viafb_par_length +
1760                 ALIGN(sizeof(struct viafb_shared), BITS_PER_LONG/8),
1761                 &vdev->pdev->dev);
1762         if (!viafbinfo) {
1763                 printk(KERN_ERR"Could not allocate memory for viafb_info.\n");
1764                 return -ENOMEM;
1765         }
1766
1767         viaparinfo = (struct viafb_par *)viafbinfo->par;
1768         viaparinfo->shared = viafbinfo->par + viafb_par_length;
1769         viaparinfo->shared->vdev = vdev;
1770         viaparinfo->vram_addr = 0;
1771         viaparinfo->tmds_setting_info = &viaparinfo->shared->tmds_setting_info;
1772         viaparinfo->lvds_setting_info = &viaparinfo->shared->lvds_setting_info;
1773         viaparinfo->lvds_setting_info2 =
1774                 &viaparinfo->shared->lvds_setting_info2;
1775         viaparinfo->crt_setting_info = &viaparinfo->shared->crt_setting_info;
1776         viaparinfo->chip_info = &viaparinfo->shared->chip_info;
1777
1778         if (viafb_dual_fb)
1779                 viafb_SAMM_ON = 1;
1780         parse_lcd_port();
1781         parse_dvi_port();
1782
1783         /* for dual-fb must viafb_SAMM_ON=1 and viafb_dual_fb=1 */
1784         if (!viafb_SAMM_ON)
1785                 viafb_dual_fb = 0;
1786
1787         viafb_init_chip_info(vdev->chip_type);
1788         /*
1789          * The framebuffer will have been successfully mapped by
1790          * the core (or we'd not be here), but we still need to
1791          * set up our own accounting.
1792          */
1793         viaparinfo->fbmem = vdev->fbmem_start;
1794         viaparinfo->memsize = vdev->fbmem_len;
1795         viaparinfo->fbmem_free = viaparinfo->memsize;
1796         viaparinfo->fbmem_used = 0;
1797         viafbinfo->screen_base = vdev->fbmem;
1798
1799         viafbinfo->fix.mmio_start = vdev->engine_start;
1800         viafbinfo->fix.mmio_len = vdev->engine_len;
1801         viafbinfo->node = 0;
1802         viafbinfo->fbops = &viafb_ops;
1803         viafbinfo->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
1804
1805         viafbinfo->pseudo_palette = pseudo_pal;
1806         if (viafb_accel && !viafb_init_engine(viafbinfo)) {
1807                 viafbinfo->flags |= FBINFO_HWACCEL_COPYAREA |
1808                         FBINFO_HWACCEL_FILLRECT |  FBINFO_HWACCEL_IMAGEBLIT;
1809                 default_var.accel_flags = FB_ACCELF_TEXT;
1810         } else {
1811                 viafbinfo->flags |= FBINFO_HWACCEL_DISABLED;
1812                 default_var.accel_flags = 0;
1813         }
1814
1815         if (viafb_second_size && (viafb_second_size < 8)) {
1816                 viafb_second_offset = viaparinfo->fbmem_free -
1817                         viafb_second_size * 1024 * 1024;
1818         } else {
1819                 viafb_second_size = 8;
1820                 viafb_second_offset = viaparinfo->fbmem_free -
1821                         viafb_second_size * 1024 * 1024;
1822         }
1823
1824         parse_mode(viafb_mode, &default_xres, &default_yres);
1825         vmode_entry = viafb_get_mode(default_xres, default_yres);
1826         if (viafb_SAMM_ON == 1) {
1827                 parse_mode(viafb_mode1, &viafb_second_xres,
1828                         &viafb_second_yres);
1829
1830                 if (0 == viafb_second_virtual_xres) {
1831                         switch (viafb_second_xres) {
1832                         case 1400:
1833                                 viafb_second_virtual_xres = 1408;
1834                                 break;
1835                         default:
1836                                 viafb_second_virtual_xres = viafb_second_xres;
1837                                 break;
1838                         }
1839                 }
1840                 if (0 == viafb_second_virtual_yres)
1841                         viafb_second_virtual_yres = viafb_second_yres;
1842         }
1843
1844         default_var.xres = default_xres;
1845         default_var.yres = default_yres;
1846         switch (default_xres) {
1847         case 1400:
1848                 default_var.xres_virtual = 1408;
1849                 break;
1850         default:
1851                 default_var.xres_virtual = default_xres;
1852                 break;
1853         }
1854         default_var.yres_virtual = default_yres;
1855         default_var.bits_per_pixel = viafb_bpp;
1856         default_var.pixclock =
1857             viafb_get_pixclock(default_xres, default_yres, viafb_refresh);
1858         default_var.left_margin = (default_xres >> 3) & 0xf8;
1859         default_var.right_margin = 32;
1860         default_var.upper_margin = 16;
1861         default_var.lower_margin = 4;
1862         default_var.hsync_len = default_var.left_margin;
1863         default_var.vsync_len = 4;
1864         viafb_setup_fixinfo(&viafbinfo->fix, viaparinfo);
1865         viafbinfo->var = default_var;
1866
1867         if (viafb_dual_fb) {
1868                 viafbinfo1 = framebuffer_alloc(viafb_par_length,
1869                                 &vdev->pdev->dev);
1870                 if (!viafbinfo1) {
1871                         printk(KERN_ERR
1872                         "allocate the second framebuffer struct error\n");
1873                         rc = -ENOMEM;
1874                         goto out_fb_release;
1875                 }
1876                 viaparinfo1 = viafbinfo1->par;
1877                 memcpy(viaparinfo1, viaparinfo, viafb_par_length);
1878                 viaparinfo1->vram_addr = viafb_second_offset;
1879                 viaparinfo1->memsize = viaparinfo->memsize -
1880                         viafb_second_offset;
1881                 viaparinfo->memsize = viafb_second_offset;
1882                 viaparinfo1->fbmem = viaparinfo->fbmem + viafb_second_offset;
1883
1884                 viaparinfo1->fbmem_used = viaparinfo->fbmem_used;
1885                 viaparinfo1->fbmem_free = viaparinfo1->memsize -
1886                         viaparinfo1->fbmem_used;
1887                 viaparinfo->fbmem_free = viaparinfo->memsize;
1888                 viaparinfo->fbmem_used = 0;
1889
1890                 viaparinfo->iga_path = IGA1;
1891                 viaparinfo1->iga_path = IGA2;
1892                 memcpy(viafbinfo1, viafbinfo, sizeof(struct fb_info));
1893                 viafbinfo1->par = viaparinfo1;
1894                 viafbinfo1->screen_base = viafbinfo->screen_base +
1895                         viafb_second_offset;
1896
1897                 default_var.xres = viafb_second_xres;
1898                 default_var.yres = viafb_second_yres;
1899                 default_var.xres_virtual = viafb_second_virtual_xres;
1900                 default_var.yres_virtual = viafb_second_virtual_yres;
1901                 default_var.bits_per_pixel = viafb_bpp1;
1902                 default_var.pixclock =
1903                     viafb_get_pixclock(viafb_second_xres, viafb_second_yres,
1904                     viafb_refresh);
1905                 default_var.left_margin = (viafb_second_xres >> 3) & 0xf8;
1906                 default_var.right_margin = 32;
1907                 default_var.upper_margin = 16;
1908                 default_var.lower_margin = 4;
1909                 default_var.hsync_len = default_var.left_margin;
1910                 default_var.vsync_len = 4;
1911
1912                 viafb_setup_fixinfo(&viafbinfo1->fix, viaparinfo1);
1913                 viafb_check_var(&default_var, viafbinfo1);
1914                 viafbinfo1->var = default_var;
1915                 viafb_update_fix(viafbinfo1);
1916                 viaparinfo1->depth = fb_get_color_depth(&viafbinfo1->var,
1917                         &viafbinfo1->fix);
1918         }
1919
1920         viafb_check_var(&viafbinfo->var, viafbinfo);
1921         viafb_update_fix(viafbinfo);
1922         viaparinfo->depth = fb_get_color_depth(&viafbinfo->var,
1923                 &viafbinfo->fix);
1924         default_var.activate = FB_ACTIVATE_NOW;
1925         rc = fb_alloc_cmap(&viafbinfo->cmap, 256, 0);
1926         if (rc)
1927                 goto out_fb1_release;
1928
1929         if (viafb_dual_fb && (viafb_primary_dev == LCD_Device)
1930             && (viaparinfo->chip_info->gfx_chip_name == UNICHROME_CLE266)) {
1931                 rc = register_framebuffer(viafbinfo1);
1932                 if (rc)
1933                         goto out_dealloc_cmap;
1934         }
1935         rc = register_framebuffer(viafbinfo);
1936         if (rc)
1937                 goto out_fb1_unreg_lcd_cle266;
1938
1939         if (viafb_dual_fb && ((viafb_primary_dev != LCD_Device)
1940                         || (viaparinfo->chip_info->gfx_chip_name !=
1941                         UNICHROME_CLE266))) {
1942                 rc = register_framebuffer(viafbinfo1);
1943                 if (rc)
1944                         goto out_fb_unreg;
1945         }
1946         DEBUG_MSG(KERN_INFO "fb%d: %s frame buffer device %dx%d-%dbpp\n",
1947                   viafbinfo->node, viafbinfo->fix.id, default_var.xres,
1948                   default_var.yres, default_var.bits_per_pixel);
1949
1950 #ifdef CONFIG_FB_VIA_DIRECT_PROCFS
1951         viafb_init_proc(&viaparinfo->shared->proc_entry);
1952 #endif
1953         viafb_init_dac(IGA2);
1954         return 0;
1955
1956 out_fb_unreg:
1957         unregister_framebuffer(viafbinfo);
1958 out_fb1_unreg_lcd_cle266:
1959         if (viafb_dual_fb && (viafb_primary_dev == LCD_Device)
1960             && (viaparinfo->chip_info->gfx_chip_name == UNICHROME_CLE266))
1961                 unregister_framebuffer(viafbinfo1);
1962 out_dealloc_cmap:
1963         fb_dealloc_cmap(&viafbinfo->cmap);
1964 out_fb1_release:
1965         if (viafbinfo1)
1966                 framebuffer_release(viafbinfo1);
1967 out_fb_release:
1968         framebuffer_release(viafbinfo);
1969         return rc;
1970 }
1971
1972 void __devexit via_fb_pci_remove(struct pci_dev *pdev)
1973 {
1974         DEBUG_MSG(KERN_INFO "via_pci_remove!\n");
1975         fb_dealloc_cmap(&viafbinfo->cmap);
1976         unregister_framebuffer(viafbinfo);
1977         if (viafb_dual_fb)
1978                 unregister_framebuffer(viafbinfo1);
1979 #ifdef CONFIG_FB_VIA_DIRECT_PROCFS
1980         viafb_remove_proc(viaparinfo->shared->proc_entry);
1981 #endif
1982         framebuffer_release(viafbinfo);
1983         if (viafb_dual_fb)
1984                 framebuffer_release(viafbinfo1);
1985 }
1986
1987 #ifndef MODULE
1988 static int __init viafb_setup(char *options)
1989 {
1990         char *this_opt;
1991         DEBUG_MSG(KERN_INFO "viafb_setup!\n");
1992
1993         if (!options || !*options)
1994                 return 0;
1995
1996         while ((this_opt = strsep(&options, ",")) != NULL) {
1997                 if (!*this_opt)
1998                         continue;
1999
2000                 if (!strncmp(this_opt, "viafb_mode1=", 12))
2001                         viafb_mode1 = kstrdup(this_opt + 12, GFP_KERNEL);
2002                 else if (!strncmp(this_opt, "viafb_mode=", 11))
2003                         viafb_mode = kstrdup(this_opt + 11, GFP_KERNEL);
2004                 else if (!strncmp(this_opt, "viafb_bpp1=", 11))
2005                         strict_strtoul(this_opt + 11, 0,
2006                                 (unsigned long *)&viafb_bpp1);
2007                 else if (!strncmp(this_opt, "viafb_bpp=", 10))
2008                         strict_strtoul(this_opt + 10, 0,
2009                                 (unsigned long *)&viafb_bpp);
2010                 else if (!strncmp(this_opt, "viafb_refresh1=", 15))
2011                         strict_strtoul(this_opt + 15, 0,
2012                                 (unsigned long *)&viafb_refresh1);
2013                 else if (!strncmp(this_opt, "viafb_refresh=", 14))
2014                         strict_strtoul(this_opt + 14, 0,
2015                                 (unsigned long *)&viafb_refresh);
2016                 else if (!strncmp(this_opt, "viafb_lcd_dsp_method=", 21))
2017                         strict_strtoul(this_opt + 21, 0,
2018                                 (unsigned long *)&viafb_lcd_dsp_method);
2019                 else if (!strncmp(this_opt, "viafb_lcd_panel_id=", 19))
2020                         strict_strtoul(this_opt + 19, 0,
2021                                 (unsigned long *)&viafb_lcd_panel_id);
2022                 else if (!strncmp(this_opt, "viafb_accel=", 12))
2023                         strict_strtoul(this_opt + 12, 0,
2024                                 (unsigned long *)&viafb_accel);
2025                 else if (!strncmp(this_opt, "viafb_SAMM_ON=", 14))
2026                         strict_strtoul(this_opt + 14, 0,
2027                                 (unsigned long *)&viafb_SAMM_ON);
2028                 else if (!strncmp(this_opt, "viafb_active_dev=", 17))
2029                         viafb_active_dev = kstrdup(this_opt + 17, GFP_KERNEL);
2030                 else if (!strncmp(this_opt,
2031                         "viafb_display_hardware_layout=", 30))
2032                         strict_strtoul(this_opt + 30, 0,
2033                         (unsigned long *)&viafb_display_hardware_layout);
2034                 else if (!strncmp(this_opt, "viafb_second_size=", 18))
2035                         strict_strtoul(this_opt + 18, 0,
2036                                 (unsigned long *)&viafb_second_size);
2037                 else if (!strncmp(this_opt,
2038                         "viafb_platform_epia_dvi=", 24))
2039                         strict_strtoul(this_opt + 24, 0,
2040                                 (unsigned long *)&viafb_platform_epia_dvi);
2041                 else if (!strncmp(this_opt,
2042                         "viafb_device_lcd_dualedge=", 26))
2043                         strict_strtoul(this_opt + 26, 0,
2044                                 (unsigned long *)&viafb_device_lcd_dualedge);
2045                 else if (!strncmp(this_opt, "viafb_bus_width=", 16))
2046                         strict_strtoul(this_opt + 16, 0,
2047                                 (unsigned long *)&viafb_bus_width);
2048                 else if (!strncmp(this_opt, "viafb_lcd_mode=", 15))
2049                         strict_strtoul(this_opt + 15, 0,
2050                                 (unsigned long *)&viafb_lcd_mode);
2051                 else if (!strncmp(this_opt, "viafb_lcd_port=", 15))
2052                         viafb_lcd_port = kstrdup(this_opt + 15, GFP_KERNEL);
2053                 else if (!strncmp(this_opt, "viafb_dvi_port=", 15))
2054                         viafb_dvi_port = kstrdup(this_opt + 15, GFP_KERNEL);
2055         }
2056         return 0;
2057 }
2058 #endif
2059
2060 /*
2061  * These are called out of via-core for now.
2062  */
2063 int __init viafb_init(void)
2064 {
2065         u32 dummy;
2066 #ifndef MODULE
2067         char *option = NULL;
2068         if (fb_get_options("viafb", &option))
2069                 return -ENODEV;
2070         viafb_setup(option);
2071 #endif
2072         if (parse_mode(viafb_mode, &dummy, &dummy)
2073                 || parse_mode(viafb_mode1, &dummy, &dummy)
2074                 || viafb_bpp < 0 || viafb_bpp > 32
2075                 || viafb_bpp1 < 0 || viafb_bpp1 > 32
2076                 || parse_active_dev())
2077                 return -EINVAL;
2078
2079         printk(KERN_INFO
2080        "VIA Graphics Intergration Chipset framebuffer %d.%d initializing\n",
2081                VERSION_MAJOR, VERSION_MINOR);
2082         return 0;
2083 }
2084
2085 void __exit viafb_exit(void)
2086 {
2087         DEBUG_MSG(KERN_INFO "viafb_exit!\n");
2088 }
2089
2090 static struct fb_ops viafb_ops = {
2091         .owner = THIS_MODULE,
2092         .fb_open = viafb_open,
2093         .fb_release = viafb_release,
2094         .fb_check_var = viafb_check_var,
2095         .fb_set_par = viafb_set_par,
2096         .fb_setcolreg = viafb_setcolreg,
2097         .fb_pan_display = viafb_pan_display,
2098         .fb_blank = viafb_blank,
2099         .fb_fillrect = viafb_fillrect,
2100         .fb_copyarea = viafb_copyarea,
2101         .fb_imageblit = viafb_imageblit,
2102         .fb_cursor = viafb_cursor,
2103         .fb_ioctl = viafb_ioctl,
2104         .fb_sync = viafb_sync,
2105 };
2106
2107
2108 #ifdef MODULE
2109 module_param(viafb_mode, charp, S_IRUSR);
2110 MODULE_PARM_DESC(viafb_mode, "Set resolution (default=640x480)");
2111
2112 module_param(viafb_mode1, charp, S_IRUSR);
2113 MODULE_PARM_DESC(viafb_mode1, "Set resolution (default=640x480)");
2114
2115 module_param(viafb_bpp, int, S_IRUSR);
2116 MODULE_PARM_DESC(viafb_bpp, "Set color depth (default=32bpp)");
2117
2118 module_param(viafb_bpp1, int, S_IRUSR);
2119 MODULE_PARM_DESC(viafb_bpp1, "Set color depth (default=32bpp)");
2120
2121 module_param(viafb_refresh, int, S_IRUSR);
2122 MODULE_PARM_DESC(viafb_refresh,
2123         "Set CRT viafb_refresh rate (default = 60)");
2124
2125 module_param(viafb_refresh1, int, S_IRUSR);
2126 MODULE_PARM_DESC(viafb_refresh1,
2127         "Set CRT refresh rate (default = 60)");
2128
2129 module_param(viafb_lcd_panel_id, int, S_IRUSR);
2130 MODULE_PARM_DESC(viafb_lcd_panel_id,
2131         "Set Flat Panel type(Default=1024x768)");
2132
2133 module_param(viafb_lcd_dsp_method, int, S_IRUSR);
2134 MODULE_PARM_DESC(viafb_lcd_dsp_method,
2135         "Set Flat Panel display scaling method.(Default=Expandsion)");
2136
2137 module_param(viafb_SAMM_ON, int, S_IRUSR);
2138 MODULE_PARM_DESC(viafb_SAMM_ON,
2139         "Turn on/off flag of SAMM(Default=OFF)");
2140
2141 module_param(viafb_accel, int, S_IRUSR);
2142 MODULE_PARM_DESC(viafb_accel,
2143         "Set 2D Hardware Acceleration: 0 = OFF, 1 = ON (default)");
2144
2145 module_param(viafb_active_dev, charp, S_IRUSR);
2146 MODULE_PARM_DESC(viafb_active_dev, "Specify active devices.");
2147
2148 module_param(viafb_display_hardware_layout, int, S_IRUSR);
2149 MODULE_PARM_DESC(viafb_display_hardware_layout,
2150         "Display Hardware Layout (LCD Only, DVI Only...,etc)");
2151
2152 module_param(viafb_second_size, int, S_IRUSR);
2153 MODULE_PARM_DESC(viafb_second_size,
2154         "Set secondary device memory size");
2155
2156 module_param(viafb_dual_fb, int, S_IRUSR);
2157 MODULE_PARM_DESC(viafb_dual_fb,
2158         "Turn on/off flag of dual framebuffer devices.(Default = OFF)");
2159
2160 module_param(viafb_platform_epia_dvi, int, S_IRUSR);
2161 MODULE_PARM_DESC(viafb_platform_epia_dvi,
2162         "Turn on/off flag of DVI devices on EPIA board.(Default = OFF)");
2163
2164 module_param(viafb_device_lcd_dualedge, int, S_IRUSR);
2165 MODULE_PARM_DESC(viafb_device_lcd_dualedge,
2166         "Turn on/off flag of dual edge panel.(Default = OFF)");
2167
2168 module_param(viafb_bus_width, int, S_IRUSR);
2169 MODULE_PARM_DESC(viafb_bus_width,
2170         "Set bus width of panel.(Default = 12)");
2171
2172 module_param(viafb_lcd_mode, int, S_IRUSR);
2173 MODULE_PARM_DESC(viafb_lcd_mode,
2174         "Set Flat Panel mode(Default=OPENLDI)");
2175
2176 module_param(viafb_lcd_port, charp, S_IRUSR);
2177 MODULE_PARM_DESC(viafb_lcd_port, "Specify LCD output port.");
2178
2179 module_param(viafb_dvi_port, charp, S_IRUSR);
2180 MODULE_PARM_DESC(viafb_dvi_port, "Specify DVI output port.");
2181
2182 MODULE_LICENSE("GPL");
2183 #endif