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