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