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