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