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