Merge branch 'linus' into perf/urgent
[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_xres = 640;
42 static unsigned int viafb_second_yres = 480;
43 static unsigned int viafb_second_offset;
44 static int viafb_second_size;
45
46 static int viafb_accel = 1;
47
48 /* Added for specifying active devices.*/
49 static char *viafb_active_dev;
50
51 /*Added for specify lcd output port*/
52 static char *viafb_lcd_port = "";
53 static char *viafb_dvi_port = "";
54
55 static void retrieve_device_setting(struct viafb_ioctl_setting
56         *setting_info);
57 static int viafb_pan_display(struct fb_var_screeninfo *var,
58         struct fb_info *info);
59
60 static struct fb_ops viafb_ops;
61
62 /* supported output devices on each IGP
63  * only CX700, VX800, VX855, VX900 were documented
64  * VIA_CRT should be everywhere
65  * VIA_6C can be onle pre-CX700 (probably only on CLE266) as 6C is used for PLL
66  * source selection on CX700 and later
67  * K400 seems to support VIA_96, VIA_DVP1, VIA_LVDS{1,2} as in viamode.c
68  */
69 static const u32 supported_odev_map[] = {
70         [UNICHROME_CLE266]      = VIA_CRT | VIA_LDVP0 | VIA_LDVP1,
71         [UNICHROME_K400]        = VIA_CRT | VIA_DVP0 | VIA_DVP1 | VIA_LVDS1
72                                 | VIA_LVDS2,
73         [UNICHROME_K800]        = VIA_CRT | VIA_DVP0 | VIA_DVP1 | VIA_LVDS1
74                                 | VIA_LVDS2,
75         [UNICHROME_PM800]       = VIA_CRT | VIA_DVP0 | VIA_DVP1 | VIA_LVDS1
76                                 | VIA_LVDS2,
77         [UNICHROME_CN700]       = VIA_CRT | VIA_DVP0 | VIA_DVP1 | VIA_LVDS1
78                                 | VIA_LVDS2,
79         [UNICHROME_CX700]       = VIA_CRT | VIA_DVP1 | VIA_LVDS1 | VIA_LVDS2,
80         [UNICHROME_CN750]       = VIA_CRT | VIA_DVP1 | VIA_LVDS1 | VIA_LVDS2,
81         [UNICHROME_K8M890]      = VIA_CRT | VIA_DVP1 | VIA_LVDS1 | VIA_LVDS2,
82         [UNICHROME_P4M890]      = VIA_CRT | VIA_DVP1 | VIA_LVDS1 | VIA_LVDS2,
83         [UNICHROME_P4M900]      = VIA_CRT | VIA_DVP1 | VIA_LVDS1 | VIA_LVDS2,
84         [UNICHROME_VX800]       = VIA_CRT | VIA_DVP1 | VIA_LVDS1 | VIA_LVDS2,
85         [UNICHROME_VX855]       = VIA_CRT | VIA_DVP1 | VIA_LVDS1 | VIA_LVDS2,
86         [UNICHROME_VX900]       = VIA_CRT | VIA_DVP1 | VIA_LVDS1 | VIA_LVDS2,
87 };
88
89 static void viafb_fill_var_color_info(struct fb_var_screeninfo *var, u8 depth)
90 {
91         var->grayscale = 0;
92         var->red.msb_right = 0;
93         var->green.msb_right = 0;
94         var->blue.msb_right = 0;
95         var->transp.offset = 0;
96         var->transp.length = 0;
97         var->transp.msb_right = 0;
98         var->nonstd = 0;
99         switch (depth) {
100         case 8:
101                 var->bits_per_pixel = 8;
102                 var->red.offset = 0;
103                 var->green.offset = 0;
104                 var->blue.offset = 0;
105                 var->red.length = 8;
106                 var->green.length = 8;
107                 var->blue.length = 8;
108                 break;
109         case 15:
110                 var->bits_per_pixel = 16;
111                 var->red.offset = 10;
112                 var->green.offset = 5;
113                 var->blue.offset = 0;
114                 var->red.length = 5;
115                 var->green.length = 5;
116                 var->blue.length = 5;
117                 break;
118         case 16:
119                 var->bits_per_pixel = 16;
120                 var->red.offset = 11;
121                 var->green.offset = 5;
122                 var->blue.offset = 0;
123                 var->red.length = 5;
124                 var->green.length = 6;
125                 var->blue.length = 5;
126                 break;
127         case 24:
128                 var->bits_per_pixel = 32;
129                 var->red.offset = 16;
130                 var->green.offset = 8;
131                 var->blue.offset = 0;
132                 var->red.length = 8;
133                 var->green.length = 8;
134                 var->blue.length = 8;
135                 break;
136         case 30:
137                 var->bits_per_pixel = 32;
138                 var->red.offset = 20;
139                 var->green.offset = 10;
140                 var->blue.offset = 0;
141                 var->red.length = 10;
142                 var->green.length = 10;
143                 var->blue.length = 10;
144                 break;
145         }
146 }
147
148 static void viafb_update_fix(struct fb_info *info)
149 {
150         u32 bpp = info->var.bits_per_pixel;
151
152         info->fix.visual =
153                 bpp == 8 ? FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
154         info->fix.line_length = (info->var.xres_virtual * bpp / 8 + 7) & ~7;
155 }
156
157 static void viafb_setup_fixinfo(struct fb_fix_screeninfo *fix,
158         struct viafb_par *viaparinfo)
159 {
160         memset(fix, 0, sizeof(struct fb_fix_screeninfo));
161         strcpy(fix->id, viafb_name);
162
163         fix->smem_start = viaparinfo->fbmem;
164         fix->smem_len = viaparinfo->fbmem_free;
165
166         fix->type = FB_TYPE_PACKED_PIXELS;
167         fix->type_aux = 0;
168         fix->visual = FB_VISUAL_TRUECOLOR;
169
170         fix->xpanstep = fix->ywrapstep = 0;
171         fix->ypanstep = 1;
172
173         /* Just tell the accel name */
174         viafbinfo->fix.accel = FB_ACCEL_VIA_UNICHROME;
175 }
176 static int viafb_open(struct fb_info *info, int user)
177 {
178         DEBUG_MSG(KERN_INFO "viafb_open!\n");
179         return 0;
180 }
181
182 static int viafb_release(struct fb_info *info, int user)
183 {
184         DEBUG_MSG(KERN_INFO "viafb_release!\n");
185         return 0;
186 }
187
188 static inline int get_var_refresh(struct fb_var_screeninfo *var)
189 {
190         u32 htotal, vtotal;
191
192         htotal = var->left_margin + var->xres + var->right_margin
193                 + var->hsync_len;
194         vtotal = var->upper_margin + var->yres + var->lower_margin
195                 + var->vsync_len;
196         return PICOS2KHZ(var->pixclock) * 1000 / (htotal * vtotal);
197 }
198
199 static int viafb_check_var(struct fb_var_screeninfo *var,
200         struct fb_info *info)
201 {
202         int depth, refresh;
203         struct VideoModeTable *vmode_entry;
204         struct viafb_par *ppar = info->par;
205         u32 line;
206
207         DEBUG_MSG(KERN_INFO "viafb_check_var!\n");
208         /* Sanity check */
209         /* HW neither support interlacte nor double-scaned mode */
210         if (var->vmode & FB_VMODE_INTERLACED || var->vmode & FB_VMODE_DOUBLE)
211                 return -EINVAL;
212
213         vmode_entry = viafb_get_mode(var->xres, var->yres);
214         if (!vmode_entry) {
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         line = (var->xres_virtual * var->bits_per_pixel / 8 + 7) & ~7;
242         if (line * var->yres_virtual > ppar->memsize)
243                 return -EINVAL;
244
245         /* Based on var passed in to calculate the refresh,
246          * because our driver use some modes special.
247          */
248         refresh = viafb_get_refresh(var->xres, var->yres,
249                 get_var_refresh(var));
250
251         /* Adjust var according to our driver's own table */
252         viafb_fill_var_timing_info(var, refresh, vmode_entry);
253         if (var->accel_flags & FB_ACCELF_TEXT &&
254                 !ppar->shared->vdev->engine_mmio)
255                 var->accel_flags = 0;
256
257         return 0;
258 }
259
260 static int viafb_set_par(struct fb_info *info)
261 {
262         struct viafb_par *viapar = info->par;
263         struct VideoModeTable *vmode_entry, *vmode_entry1 = NULL;
264         int refresh;
265         DEBUG_MSG(KERN_INFO "viafb_set_par!\n");
266
267         viafb_update_fix(info);
268         viapar->depth = fb_get_color_depth(&info->var, &info->fix);
269         viafb_update_device_setting(viafbinfo->var.xres, viafbinfo->var.yres,
270                 viafbinfo->var.bits_per_pixel, 0);
271
272         vmode_entry = viafb_get_mode(viafbinfo->var.xres, viafbinfo->var.yres);
273         if (viafb_dual_fb) {
274                 vmode_entry1 = viafb_get_mode(viafbinfo1->var.xres,
275                         viafbinfo1->var.yres);
276                 viafb_update_device_setting(viafbinfo1->var.xres,
277                         viafbinfo1->var.yres, viafbinfo1->var.bits_per_pixel,
278                         1);
279         } else if (viafb_SAMM_ON == 1) {
280                 DEBUG_MSG(KERN_INFO
281                 "viafb_second_xres = %d, viafb_second_yres = %d, bpp = %d\n",
282                           viafb_second_xres, viafb_second_yres, viafb_bpp1);
283                 vmode_entry1 = viafb_get_mode(viafb_second_xres,
284                         viafb_second_yres);
285
286                 viafb_update_device_setting(viafb_second_xres,
287                         viafb_second_yres, viafb_bpp1, 1);
288         }
289
290         refresh = viafb_get_refresh(info->var.xres, info->var.yres,
291                 get_var_refresh(&info->var));
292         if (vmode_entry) {
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(vmode_entry, info->var.bits_per_pixel,
306                         vmode_entry1, viafb_bpp1);
307                 viafb_pan_display(&info->var, info);
308         }
309
310         return 0;
311 }
312
313 /* Set one color register */
314 static int viafb_setcolreg(unsigned regno, unsigned red, unsigned green,
315 unsigned blue, unsigned transp, struct fb_info *info)
316 {
317         struct viafb_par *viapar = info->par;
318         u32 r, g, b;
319
320         if (info->fix.visual == FB_VISUAL_PSEUDOCOLOR) {
321                 if (regno > 255)
322                         return -EINVAL;
323
324                 if (!viafb_dual_fb || viapar->iga_path == IGA1)
325                         viafb_set_primary_color_register(regno, red >> 8,
326                                 green >> 8, blue >> 8);
327
328                 if (!viafb_dual_fb || viapar->iga_path == IGA2)
329                         viafb_set_secondary_color_register(regno, red >> 8,
330                                 green >> 8, blue >> 8);
331         } else {
332                 if (regno > 15)
333                         return -EINVAL;
334
335                 r = (red >> (16 - info->var.red.length))
336                         << info->var.red.offset;
337                 b = (blue >> (16 - info->var.blue.length))
338                         << info->var.blue.offset;
339                 g = (green >> (16 - info->var.green.length))
340                         << info->var.green.offset;
341                 ((u32 *) info->pseudo_palette)[regno] = r | g | b;
342         }
343
344         return 0;
345 }
346
347 static int viafb_pan_display(struct fb_var_screeninfo *var,
348         struct fb_info *info)
349 {
350         struct viafb_par *viapar = info->par;
351         u32 vram_addr = (var->yoffset * var->xres_virtual + var->xoffset)
352                 * (var->bits_per_pixel / 8) + viapar->vram_addr;
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                         strict_strtoul(value, 0, (unsigned long *)&reg_val);
1162                         DEBUG_MSG(KERN_INFO "DVP0:reg_val[%l]=:%x\n", i,
1163                                   reg_val);
1164                         switch (i) {
1165                         case 0:
1166                                 viafb_write_reg_mask(CR96, VIACR,
1167                                         reg_val, 0x0f);
1168                                 break;
1169                         case 1:
1170                                 viafb_write_reg_mask(SR2A, VIASR,
1171                                         reg_val << 4, BIT5);
1172                                 viafb_write_reg_mask(SR1B, VIASR,
1173                                         reg_val << 1, BIT1);
1174                                 break;
1175                         case 2:
1176                                 viafb_write_reg_mask(SR2A, VIASR,
1177                                         reg_val << 3, BIT4);
1178                                 viafb_write_reg_mask(SR1E, VIASR,
1179                                         reg_val << 2, BIT2);
1180                                 break;
1181                         default:
1182                                 break;
1183                         }
1184                 } else {
1185                         break;
1186                 }
1187         }
1188         return count;
1189 }
1190
1191 static const struct file_operations viafb_dvp0_proc_fops = {
1192         .owner          = THIS_MODULE,
1193         .open           = viafb_dvp0_proc_open,
1194         .read           = seq_read,
1195         .llseek         = seq_lseek,
1196         .release        = single_release,
1197         .write          = viafb_dvp0_proc_write,
1198 };
1199
1200 static int viafb_dvp1_proc_show(struct seq_file *m, void *v)
1201 {
1202         u8 dvp1 = 0, dvp1_data_dri = 0, dvp1_clk_dri = 0;
1203         dvp1 = viafb_read_reg(VIACR, CR9B) & 0x0f;
1204         dvp1_data_dri = (viafb_read_reg(VIASR, SR65) & 0x0c) >> 2;
1205         dvp1_clk_dri = viafb_read_reg(VIASR, SR65) & 0x03;
1206         seq_printf(m, "%x %x %x\n", dvp1, dvp1_data_dri, dvp1_clk_dri);
1207         return 0;
1208 }
1209
1210 static int viafb_dvp1_proc_open(struct inode *inode, struct file *file)
1211 {
1212         return single_open(file, viafb_dvp1_proc_show, NULL);
1213 }
1214
1215 static ssize_t viafb_dvp1_proc_write(struct file *file,
1216         const char __user *buffer, size_t count, loff_t *pos)
1217 {
1218         char buf[20], *value, *pbuf;
1219         u8 reg_val = 0;
1220         unsigned long length, i;
1221         if (count < 1)
1222                 return -EINVAL;
1223         length = count > 20 ? 20 : count;
1224         if (copy_from_user(&buf[0], buffer, length))
1225                 return -EFAULT;
1226         buf[length - 1] = '\0'; /*Ensure end string */
1227         pbuf = &buf[0];
1228         for (i = 0; i < 3; i++) {
1229                 value = strsep(&pbuf, " ");
1230                 if (value != NULL) {
1231                         strict_strtoul(value, 0, (unsigned long *)&reg_val);
1232                         switch (i) {
1233                         case 0:
1234                                 viafb_write_reg_mask(CR9B, VIACR,
1235                                         reg_val, 0x0f);
1236                                 break;
1237                         case 1:
1238                                 viafb_write_reg_mask(SR65, VIASR,
1239                                         reg_val << 2, 0x0c);
1240                                 break;
1241                         case 2:
1242                                 viafb_write_reg_mask(SR65, VIASR,
1243                                         reg_val, 0x03);
1244                                 break;
1245                         default:
1246                                 break;
1247                         }
1248                 } else {
1249                         break;
1250                 }
1251         }
1252         return count;
1253 }
1254
1255 static const struct file_operations viafb_dvp1_proc_fops = {
1256         .owner          = THIS_MODULE,
1257         .open           = viafb_dvp1_proc_open,
1258         .read           = seq_read,
1259         .llseek         = seq_lseek,
1260         .release        = single_release,
1261         .write          = viafb_dvp1_proc_write,
1262 };
1263
1264 static int viafb_dfph_proc_show(struct seq_file *m, void *v)
1265 {
1266         u8 dfp_high = 0;
1267         dfp_high = viafb_read_reg(VIACR, CR97) & 0x0f;
1268         seq_printf(m, "%x\n", dfp_high);
1269         return 0;
1270 }
1271
1272 static int viafb_dfph_proc_open(struct inode *inode, struct file *file)
1273 {
1274         return single_open(file, viafb_dfph_proc_show, NULL);
1275 }
1276
1277 static ssize_t viafb_dfph_proc_write(struct file *file,
1278         const char __user *buffer, size_t count, loff_t *pos)
1279 {
1280         char buf[20];
1281         u8 reg_val = 0;
1282         unsigned long length;
1283         if (count < 1)
1284                 return -EINVAL;
1285         length = count > 20 ? 20 : count;
1286         if (copy_from_user(&buf[0], buffer, length))
1287                 return -EFAULT;
1288         buf[length - 1] = '\0'; /*Ensure end string */
1289         strict_strtoul(&buf[0], 0, (unsigned long *)&reg_val);
1290         viafb_write_reg_mask(CR97, VIACR, reg_val, 0x0f);
1291         return count;
1292 }
1293
1294 static const struct file_operations viafb_dfph_proc_fops = {
1295         .owner          = THIS_MODULE,
1296         .open           = viafb_dfph_proc_open,
1297         .read           = seq_read,
1298         .llseek         = seq_lseek,
1299         .release        = single_release,
1300         .write          = viafb_dfph_proc_write,
1301 };
1302
1303 static int viafb_dfpl_proc_show(struct seq_file *m, void *v)
1304 {
1305         u8 dfp_low = 0;
1306         dfp_low = viafb_read_reg(VIACR, CR99) & 0x0f;
1307         seq_printf(m, "%x\n", dfp_low);
1308         return 0;
1309 }
1310
1311 static int viafb_dfpl_proc_open(struct inode *inode, struct file *file)
1312 {
1313         return single_open(file, viafb_dfpl_proc_show, NULL);
1314 }
1315
1316 static ssize_t viafb_dfpl_proc_write(struct file *file,
1317         const char __user *buffer, size_t count, loff_t *pos)
1318 {
1319         char buf[20];
1320         u8 reg_val = 0;
1321         unsigned long length;
1322         if (count < 1)
1323                 return -EINVAL;
1324         length = count > 20 ? 20 : count;
1325         if (copy_from_user(&buf[0], buffer, length))
1326                 return -EFAULT;
1327         buf[length - 1] = '\0'; /*Ensure end string */
1328         strict_strtoul(&buf[0], 0, (unsigned long *)&reg_val);
1329         viafb_write_reg_mask(CR99, VIACR, reg_val, 0x0f);
1330         return count;
1331 }
1332
1333 static const struct file_operations viafb_dfpl_proc_fops = {
1334         .owner          = THIS_MODULE,
1335         .open           = viafb_dfpl_proc_open,
1336         .read           = seq_read,
1337         .llseek         = seq_lseek,
1338         .release        = single_release,
1339         .write          = viafb_dfpl_proc_write,
1340 };
1341
1342 static int viafb_vt1636_proc_show(struct seq_file *m, void *v)
1343 {
1344         u8 vt1636_08 = 0, vt1636_09 = 0;
1345         switch (viaparinfo->chip_info->lvds_chip_info.lvds_chip_name) {
1346         case VT1636_LVDS:
1347                 vt1636_08 =
1348                     viafb_gpio_i2c_read_lvds(viaparinfo->lvds_setting_info,
1349                     &viaparinfo->chip_info->lvds_chip_info, 0x08) & 0x0f;
1350                 vt1636_09 =
1351                     viafb_gpio_i2c_read_lvds(viaparinfo->lvds_setting_info,
1352                     &viaparinfo->chip_info->lvds_chip_info, 0x09) & 0x1f;
1353                 seq_printf(m, "%x %x\n", vt1636_08, vt1636_09);
1354                 break;
1355         default:
1356                 break;
1357         }
1358         switch (viaparinfo->chip_info->lvds_chip_info2.lvds_chip_name) {
1359         case VT1636_LVDS:
1360                 vt1636_08 =
1361                     viafb_gpio_i2c_read_lvds(viaparinfo->lvds_setting_info2,
1362                         &viaparinfo->chip_info->lvds_chip_info2, 0x08) & 0x0f;
1363                 vt1636_09 =
1364                     viafb_gpio_i2c_read_lvds(viaparinfo->lvds_setting_info2,
1365                         &viaparinfo->chip_info->lvds_chip_info2, 0x09) & 0x1f;
1366                 seq_printf(m, " %x %x\n", vt1636_08, vt1636_09);
1367                 break;
1368         default:
1369                 break;
1370         }
1371         return 0;
1372 }
1373
1374 static int viafb_vt1636_proc_open(struct inode *inode, struct file *file)
1375 {
1376         return single_open(file, viafb_vt1636_proc_show, NULL);
1377 }
1378
1379 static ssize_t viafb_vt1636_proc_write(struct file *file,
1380         const char __user *buffer, size_t count, loff_t *pos)
1381 {
1382         char buf[30], *value, *pbuf;
1383         struct IODATA reg_val;
1384         unsigned long length, i;
1385         if (count < 1)
1386                 return -EINVAL;
1387         length = count > 30 ? 30 : count;
1388         if (copy_from_user(&buf[0], buffer, length))
1389                 return -EFAULT;
1390         buf[length - 1] = '\0'; /*Ensure end string */
1391         pbuf = &buf[0];
1392         switch (viaparinfo->chip_info->lvds_chip_info.lvds_chip_name) {
1393         case VT1636_LVDS:
1394                 for (i = 0; i < 2; i++) {
1395                         value = strsep(&pbuf, " ");
1396                         if (value != NULL) {
1397                                 strict_strtoul(value, 0,
1398                                         (unsigned long *)&reg_val.Data);
1399                                 switch (i) {
1400                                 case 0:
1401                                         reg_val.Index = 0x08;
1402                                         reg_val.Mask = 0x0f;
1403                                         viafb_gpio_i2c_write_mask_lvds
1404                                             (viaparinfo->lvds_setting_info,
1405                                             &viaparinfo->
1406                                             chip_info->lvds_chip_info,
1407                                              reg_val);
1408                                         break;
1409                                 case 1:
1410                                         reg_val.Index = 0x09;
1411                                         reg_val.Mask = 0x1f;
1412                                         viafb_gpio_i2c_write_mask_lvds
1413                                             (viaparinfo->lvds_setting_info,
1414                                             &viaparinfo->
1415                                             chip_info->lvds_chip_info,
1416                                              reg_val);
1417                                         break;
1418                                 default:
1419                                         break;
1420                                 }
1421                         } else {
1422                                 break;
1423                         }
1424                 }
1425                 break;
1426         default:
1427                 break;
1428         }
1429         switch (viaparinfo->chip_info->lvds_chip_info2.lvds_chip_name) {
1430         case VT1636_LVDS:
1431                 for (i = 0; i < 2; i++) {
1432                         value = strsep(&pbuf, " ");
1433                         if (value != NULL) {
1434                                 strict_strtoul(value, 0,
1435                                         (unsigned long *)&reg_val.Data);
1436                                 switch (i) {
1437                                 case 0:
1438                                         reg_val.Index = 0x08;
1439                                         reg_val.Mask = 0x0f;
1440                                         viafb_gpio_i2c_write_mask_lvds
1441                                             (viaparinfo->lvds_setting_info2,
1442                                             &viaparinfo->
1443                                             chip_info->lvds_chip_info2,
1444                                              reg_val);
1445                                         break;
1446                                 case 1:
1447                                         reg_val.Index = 0x09;
1448                                         reg_val.Mask = 0x1f;
1449                                         viafb_gpio_i2c_write_mask_lvds
1450                                             (viaparinfo->lvds_setting_info2,
1451                                             &viaparinfo->
1452                                             chip_info->lvds_chip_info2,
1453                                              reg_val);
1454                                         break;
1455                                 default:
1456                                         break;
1457                                 }
1458                         } else {
1459                                 break;
1460                         }
1461                 }
1462                 break;
1463         default:
1464                 break;
1465         }
1466         return count;
1467 }
1468
1469 static const struct file_operations viafb_vt1636_proc_fops = {
1470         .owner          = THIS_MODULE,
1471         .open           = viafb_vt1636_proc_open,
1472         .read           = seq_read,
1473         .llseek         = seq_lseek,
1474         .release        = single_release,
1475         .write          = viafb_vt1636_proc_write,
1476 };
1477
1478 #endif /* CONFIG_FB_VIA_DIRECT_PROCFS */
1479
1480 static int viafb_sup_odev_proc_show(struct seq_file *m, void *v)
1481 {
1482         via_odev_to_seq(m, supported_odev_map[
1483                 viaparinfo->shared->chip_info.gfx_chip_name]);
1484         return 0;
1485 }
1486
1487 static int viafb_sup_odev_proc_open(struct inode *inode, struct file *file)
1488 {
1489         return single_open(file, viafb_sup_odev_proc_show, NULL);
1490 }
1491
1492 static const struct file_operations viafb_sup_odev_proc_fops = {
1493         .owner          = THIS_MODULE,
1494         .open           = viafb_sup_odev_proc_open,
1495         .read           = seq_read,
1496         .llseek         = seq_lseek,
1497         .release        = single_release,
1498 };
1499
1500 static ssize_t odev_update(const char __user *buffer, size_t count, u32 *odev)
1501 {
1502         char buf[64], *ptr = buf;
1503         u32 devices;
1504         bool add, sub;
1505
1506         if (count < 1 || count > 63)
1507                 return -EINVAL;
1508         if (copy_from_user(&buf[0], buffer, count))
1509                 return -EFAULT;
1510         buf[count] = '\0';
1511         add = buf[0] == '+';
1512         sub = buf[0] == '-';
1513         if (add || sub)
1514                 ptr++;
1515         devices = via_parse_odev(ptr, &ptr);
1516         if (*ptr == '\n')
1517                 ptr++;
1518         if (*ptr != 0)
1519                 return -EINVAL;
1520         if (add)
1521                 *odev |= devices;
1522         else if (sub)
1523                 *odev &= ~devices;
1524         else
1525                 *odev = devices;
1526         return count;
1527 }
1528
1529 static int viafb_iga1_odev_proc_show(struct seq_file *m, void *v)
1530 {
1531         via_odev_to_seq(m, viaparinfo->shared->iga1_devices);
1532         return 0;
1533 }
1534
1535 static int viafb_iga1_odev_proc_open(struct inode *inode, struct file *file)
1536 {
1537         return single_open(file, viafb_iga1_odev_proc_show, NULL);
1538 }
1539
1540 static ssize_t viafb_iga1_odev_proc_write(struct file *file,
1541         const char __user *buffer, size_t count, loff_t *pos)
1542 {
1543         u32 dev_on, dev_off, dev_old, dev_new;
1544         ssize_t res;
1545
1546         dev_old = dev_new = viaparinfo->shared->iga1_devices;
1547         res = odev_update(buffer, count, &dev_new);
1548         if (res != count)
1549                 return res;
1550         dev_off = dev_old & ~dev_new;
1551         dev_on = dev_new & ~dev_old;
1552         viaparinfo->shared->iga1_devices = dev_new;
1553         viaparinfo->shared->iga2_devices &= ~dev_new;
1554         via_set_state(dev_off, VIA_STATE_OFF);
1555         via_set_source(dev_new, IGA1);
1556         via_set_state(dev_on, VIA_STATE_ON);
1557         return res;
1558 }
1559
1560 static const struct file_operations viafb_iga1_odev_proc_fops = {
1561         .owner          = THIS_MODULE,
1562         .open           = viafb_iga1_odev_proc_open,
1563         .read           = seq_read,
1564         .llseek         = seq_lseek,
1565         .release        = single_release,
1566         .write          = viafb_iga1_odev_proc_write,
1567 };
1568
1569 static int viafb_iga2_odev_proc_show(struct seq_file *m, void *v)
1570 {
1571         via_odev_to_seq(m, viaparinfo->shared->iga2_devices);
1572         return 0;
1573 }
1574
1575 static int viafb_iga2_odev_proc_open(struct inode *inode, struct file *file)
1576 {
1577         return single_open(file, viafb_iga2_odev_proc_show, NULL);
1578 }
1579
1580 static ssize_t viafb_iga2_odev_proc_write(struct file *file,
1581         const char __user *buffer, size_t count, loff_t *pos)
1582 {
1583         u32 dev_on, dev_off, dev_old, dev_new;
1584         ssize_t res;
1585
1586         dev_old = dev_new = viaparinfo->shared->iga2_devices;
1587         res = odev_update(buffer, count, &dev_new);
1588         if (res != count)
1589                 return res;
1590         dev_off = dev_old & ~dev_new;
1591         dev_on = dev_new & ~dev_old;
1592         viaparinfo->shared->iga2_devices = dev_new;
1593         viaparinfo->shared->iga1_devices &= ~dev_new;
1594         via_set_state(dev_off, VIA_STATE_OFF);
1595         via_set_source(dev_new, IGA2);
1596         via_set_state(dev_on, VIA_STATE_ON);
1597         return res;
1598 }
1599
1600 static const struct file_operations viafb_iga2_odev_proc_fops = {
1601         .owner          = THIS_MODULE,
1602         .open           = viafb_iga2_odev_proc_open,
1603         .read           = seq_read,
1604         .llseek         = seq_lseek,
1605         .release        = single_release,
1606         .write          = viafb_iga2_odev_proc_write,
1607 };
1608
1609 #define IS_VT1636(lvds_chip)    ((lvds_chip).lvds_chip_name == VT1636_LVDS)
1610 static void viafb_init_proc(struct viafb_shared *shared)
1611 {
1612         struct proc_dir_entry *iga1_entry, *iga2_entry,
1613                 *viafb_entry = proc_mkdir("viafb", NULL);
1614
1615         shared->proc_entry = viafb_entry;
1616         if (viafb_entry) {
1617 #ifdef CONFIG_FB_VIA_DIRECT_PROCFS
1618                 proc_create("dvp0", 0, viafb_entry, &viafb_dvp0_proc_fops);
1619                 proc_create("dvp1", 0, viafb_entry, &viafb_dvp1_proc_fops);
1620                 proc_create("dfph", 0, viafb_entry, &viafb_dfph_proc_fops);
1621                 proc_create("dfpl", 0, viafb_entry, &viafb_dfpl_proc_fops);
1622                 if (IS_VT1636(shared->chip_info.lvds_chip_info)
1623                         || IS_VT1636(shared->chip_info.lvds_chip_info2))
1624                         proc_create("vt1636", 0, viafb_entry,
1625                                 &viafb_vt1636_proc_fops);
1626 #endif /* CONFIG_FB_VIA_DIRECT_PROCFS */
1627
1628                 proc_create("supported_output_devices", 0, viafb_entry,
1629                         &viafb_sup_odev_proc_fops);
1630                 iga1_entry = proc_mkdir("iga1", viafb_entry);
1631                 shared->iga1_proc_entry = iga1_entry;
1632                 proc_create("output_devices", 0, iga1_entry,
1633                         &viafb_iga1_odev_proc_fops);
1634                 iga2_entry = proc_mkdir("iga2", viafb_entry);
1635                 shared->iga2_proc_entry = iga2_entry;
1636                 proc_create("output_devices", 0, iga2_entry,
1637                         &viafb_iga2_odev_proc_fops);
1638         }
1639 }
1640 static void viafb_remove_proc(struct viafb_shared *shared)
1641 {
1642         struct proc_dir_entry *viafb_entry = shared->proc_entry,
1643                 *iga1_entry = shared->iga1_proc_entry,
1644                 *iga2_entry = shared->iga2_proc_entry;
1645
1646         if (!viafb_entry)
1647                 return;
1648
1649         remove_proc_entry("output_devices", iga2_entry);
1650         remove_proc_entry("iga2", viafb_entry);
1651         remove_proc_entry("output_devices", iga1_entry);
1652         remove_proc_entry("iga1", viafb_entry);
1653         remove_proc_entry("supported_output_devices", viafb_entry);
1654
1655 #ifdef CONFIG_FB_VIA_DIRECT_PROCFS
1656         remove_proc_entry("dvp0", viafb_entry);/* parent dir */
1657         remove_proc_entry("dvp1", viafb_entry);
1658         remove_proc_entry("dfph", viafb_entry);
1659         remove_proc_entry("dfpl", viafb_entry);
1660         if (IS_VT1636(shared->chip_info.lvds_chip_info)
1661                 || IS_VT1636(shared->chip_info.lvds_chip_info2))
1662                 remove_proc_entry("vt1636", viafb_entry);
1663 #endif /* CONFIG_FB_VIA_DIRECT_PROCFS */
1664
1665         remove_proc_entry("viafb", NULL);
1666 }
1667 #undef IS_VT1636
1668
1669 static int parse_mode(const char *str, u32 *xres, u32 *yres)
1670 {
1671         char *ptr;
1672
1673         if (!str) {
1674                 if (machine_is_olpc()) {
1675                         *xres = 1200;
1676                         *yres = 900;
1677                 } else {
1678                         *xres = 640;
1679                         *yres = 480;
1680                 }
1681                 return 0;
1682         }
1683
1684         *xres = simple_strtoul(str, &ptr, 10);
1685         if (ptr[0] != 'x')
1686                 return -EINVAL;
1687
1688         *yres = simple_strtoul(&ptr[1], &ptr, 10);
1689         if (ptr[0])
1690                 return -EINVAL;
1691
1692         return 0;
1693 }
1694
1695
1696 #ifdef CONFIG_PM
1697 static int viafb_suspend(void *unused)
1698 {
1699         console_lock();
1700         fb_set_suspend(viafbinfo, 1);
1701         viafb_sync(viafbinfo);
1702         console_unlock();
1703
1704         return 0;
1705 }
1706
1707 static int viafb_resume(void *unused)
1708 {
1709         console_lock();
1710         if (viaparinfo->shared->vdev->engine_mmio)
1711                 viafb_reset_engine(viaparinfo);
1712         viafb_set_par(viafbinfo);
1713         if (viafb_dual_fb)
1714                 viafb_set_par(viafbinfo1);
1715         fb_set_suspend(viafbinfo, 0);
1716
1717         console_unlock();
1718         return 0;
1719 }
1720
1721 static struct viafb_pm_hooks viafb_fb_pm_hooks = {
1722         .suspend = viafb_suspend,
1723         .resume = viafb_resume
1724 };
1725
1726 #endif
1727
1728
1729 int __devinit via_fb_pci_probe(struct viafb_dev *vdev)
1730 {
1731         u32 default_xres, default_yres;
1732         struct VideoModeTable *vmode_entry;
1733         struct fb_var_screeninfo default_var;
1734         int rc;
1735         u32 viafb_par_length;
1736
1737         DEBUG_MSG(KERN_INFO "VIAFB PCI Probe!!\n");
1738         memset(&default_var, 0, sizeof(default_var));
1739         viafb_par_length = ALIGN(sizeof(struct viafb_par), BITS_PER_LONG/8);
1740
1741         /* Allocate fb_info and ***_par here, also including some other needed
1742          * variables
1743         */
1744         viafbinfo = framebuffer_alloc(viafb_par_length +
1745                 ALIGN(sizeof(struct viafb_shared), BITS_PER_LONG/8),
1746                 &vdev->pdev->dev);
1747         if (!viafbinfo) {
1748                 printk(KERN_ERR"Could not allocate memory for viafb_info.\n");
1749                 return -ENOMEM;
1750         }
1751
1752         viaparinfo = (struct viafb_par *)viafbinfo->par;
1753         viaparinfo->shared = viafbinfo->par + viafb_par_length;
1754         viaparinfo->shared->vdev = vdev;
1755         viaparinfo->vram_addr = 0;
1756         viaparinfo->tmds_setting_info = &viaparinfo->shared->tmds_setting_info;
1757         viaparinfo->lvds_setting_info = &viaparinfo->shared->lvds_setting_info;
1758         viaparinfo->lvds_setting_info2 =
1759                 &viaparinfo->shared->lvds_setting_info2;
1760         viaparinfo->chip_info = &viaparinfo->shared->chip_info;
1761
1762         if (viafb_dual_fb)
1763                 viafb_SAMM_ON = 1;
1764         parse_lcd_port();
1765         parse_dvi_port();
1766
1767         viafb_init_chip_info(vdev->chip_type);
1768         /*
1769          * The framebuffer will have been successfully mapped by
1770          * the core (or we'd not be here), but we still need to
1771          * set up our own accounting.
1772          */
1773         viaparinfo->fbmem = vdev->fbmem_start;
1774         viaparinfo->memsize = vdev->fbmem_len;
1775         viaparinfo->fbmem_free = viaparinfo->memsize;
1776         viaparinfo->fbmem_used = 0;
1777         viafbinfo->screen_base = vdev->fbmem;
1778
1779         viafbinfo->fix.mmio_start = vdev->engine_start;
1780         viafbinfo->fix.mmio_len = vdev->engine_len;
1781         viafbinfo->node = 0;
1782         viafbinfo->fbops = &viafb_ops;
1783         viafbinfo->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
1784
1785         viafbinfo->pseudo_palette = pseudo_pal;
1786         if (viafb_accel && !viafb_setup_engine(viafbinfo)) {
1787                 viafbinfo->flags |= FBINFO_HWACCEL_COPYAREA |
1788                         FBINFO_HWACCEL_FILLRECT |  FBINFO_HWACCEL_IMAGEBLIT;
1789                 default_var.accel_flags = FB_ACCELF_TEXT;
1790         } else {
1791                 viafbinfo->flags |= FBINFO_HWACCEL_DISABLED;
1792                 default_var.accel_flags = 0;
1793         }
1794
1795         if (viafb_second_size && (viafb_second_size < 8)) {
1796                 viafb_second_offset = viaparinfo->fbmem_free -
1797                         viafb_second_size * 1024 * 1024;
1798         } else {
1799                 viafb_second_size = 8;
1800                 viafb_second_offset = viaparinfo->fbmem_free -
1801                         viafb_second_size * 1024 * 1024;
1802         }
1803
1804         parse_mode(viafb_mode, &default_xres, &default_yres);
1805         vmode_entry = viafb_get_mode(default_xres, default_yres);
1806         if (viafb_SAMM_ON == 1)
1807                 parse_mode(viafb_mode1, &viafb_second_xres,
1808                         &viafb_second_yres);
1809
1810         default_var.xres = default_xres;
1811         default_var.yres = default_yres;
1812         default_var.xres_virtual = default_xres;
1813         default_var.yres_virtual = default_yres;
1814         default_var.bits_per_pixel = viafb_bpp;
1815         viafb_fill_var_timing_info(&default_var, viafb_get_refresh(
1816                 default_var.xres, default_var.yres, viafb_refresh),
1817                 viafb_get_mode(default_var.xres, default_var.yres));
1818         viafb_setup_fixinfo(&viafbinfo->fix, viaparinfo);
1819         viafbinfo->var = default_var;
1820
1821         if (viafb_dual_fb) {
1822                 viafbinfo1 = framebuffer_alloc(viafb_par_length,
1823                                 &vdev->pdev->dev);
1824                 if (!viafbinfo1) {
1825                         printk(KERN_ERR
1826                         "allocate the second framebuffer struct error\n");
1827                         rc = -ENOMEM;
1828                         goto out_fb_release;
1829                 }
1830                 viaparinfo1 = viafbinfo1->par;
1831                 memcpy(viaparinfo1, viaparinfo, viafb_par_length);
1832                 viaparinfo1->vram_addr = viafb_second_offset;
1833                 viaparinfo1->memsize = viaparinfo->memsize -
1834                         viafb_second_offset;
1835                 viaparinfo->memsize = viafb_second_offset;
1836                 viaparinfo1->fbmem = viaparinfo->fbmem + viafb_second_offset;
1837
1838                 viaparinfo1->fbmem_used = viaparinfo->fbmem_used;
1839                 viaparinfo1->fbmem_free = viaparinfo1->memsize -
1840                         viaparinfo1->fbmem_used;
1841                 viaparinfo->fbmem_free = viaparinfo->memsize;
1842                 viaparinfo->fbmem_used = 0;
1843
1844                 viaparinfo->iga_path = IGA1;
1845                 viaparinfo1->iga_path = IGA2;
1846                 memcpy(viafbinfo1, viafbinfo, sizeof(struct fb_info));
1847                 viafbinfo1->par = viaparinfo1;
1848                 viafbinfo1->screen_base = viafbinfo->screen_base +
1849                         viafb_second_offset;
1850
1851                 default_var.xres = viafb_second_xres;
1852                 default_var.yres = viafb_second_yres;
1853                 default_var.xres_virtual = viafb_second_xres;
1854                 default_var.yres_virtual = viafb_second_yres;
1855                 default_var.bits_per_pixel = viafb_bpp1;
1856                 viafb_fill_var_timing_info(&default_var, viafb_get_refresh(
1857                         default_var.xres, default_var.yres, viafb_refresh1),
1858                         viafb_get_mode(default_var.xres, default_var.yres));
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                         strict_strtoul(this_opt + 11, 0,
1959                                 (unsigned long *)&viafb_bpp1);
1960                 else if (!strncmp(this_opt, "viafb_bpp=", 10))
1961                         strict_strtoul(this_opt + 10, 0,
1962                                 (unsigned long *)&viafb_bpp);
1963                 else if (!strncmp(this_opt, "viafb_refresh1=", 15))
1964                         strict_strtoul(this_opt + 15, 0,
1965                                 (unsigned long *)&viafb_refresh1);
1966                 else if (!strncmp(this_opt, "viafb_refresh=", 14))
1967                         strict_strtoul(this_opt + 14, 0,
1968                                 (unsigned long *)&viafb_refresh);
1969                 else if (!strncmp(this_opt, "viafb_lcd_dsp_method=", 21))
1970                         strict_strtoul(this_opt + 21, 0,
1971                                 (unsigned long *)&viafb_lcd_dsp_method);
1972                 else if (!strncmp(this_opt, "viafb_lcd_panel_id=", 19))
1973                         strict_strtoul(this_opt + 19, 0,
1974                                 (unsigned long *)&viafb_lcd_panel_id);
1975                 else if (!strncmp(this_opt, "viafb_accel=", 12))
1976                         strict_strtoul(this_opt + 12, 0,
1977                                 (unsigned long *)&viafb_accel);
1978                 else if (!strncmp(this_opt, "viafb_SAMM_ON=", 14))
1979                         strict_strtoul(this_opt + 14, 0,
1980                                 (unsigned long *)&viafb_SAMM_ON);
1981                 else if (!strncmp(this_opt, "viafb_active_dev=", 17))
1982                         viafb_active_dev = kstrdup(this_opt + 17, GFP_KERNEL);
1983                 else if (!strncmp(this_opt,
1984                         "viafb_display_hardware_layout=", 30))
1985                         strict_strtoul(this_opt + 30, 0,
1986                         (unsigned long *)&viafb_display_hardware_layout);
1987                 else if (!strncmp(this_opt, "viafb_second_size=", 18))
1988                         strict_strtoul(this_opt + 18, 0,
1989                                 (unsigned long *)&viafb_second_size);
1990                 else if (!strncmp(this_opt,
1991                         "viafb_platform_epia_dvi=", 24))
1992                         strict_strtoul(this_opt + 24, 0,
1993                                 (unsigned long *)&viafb_platform_epia_dvi);
1994                 else if (!strncmp(this_opt,
1995                         "viafb_device_lcd_dualedge=", 26))
1996                         strict_strtoul(this_opt + 26, 0,
1997                                 (unsigned long *)&viafb_device_lcd_dualedge);
1998                 else if (!strncmp(this_opt, "viafb_bus_width=", 16))
1999                         strict_strtoul(this_opt + 16, 0,
2000                                 (unsigned long *)&viafb_bus_width);
2001                 else if (!strncmp(this_opt, "viafb_lcd_mode=", 15))
2002                         strict_strtoul(this_opt + 15, 0,
2003                                 (unsigned long *)&viafb_lcd_mode);
2004                 else if (!strncmp(this_opt, "viafb_lcd_port=", 15))
2005                         viafb_lcd_port = kstrdup(this_opt + 15, GFP_KERNEL);
2006                 else if (!strncmp(this_opt, "viafb_dvi_port=", 15))
2007                         viafb_dvi_port = kstrdup(this_opt + 15, GFP_KERNEL);
2008         }
2009         return 0;
2010 }
2011 #endif
2012
2013 /*
2014  * These are called out of via-core for now.
2015  */
2016 int __init viafb_init(void)
2017 {
2018         u32 dummy_x, dummy_y;
2019         int r = 0;
2020
2021         if (machine_is_olpc())
2022                 /* Apply XO-1.5-specific configuration. */
2023                 viafb_lcd_panel_id = 23;
2024
2025 #ifndef MODULE
2026         r = viafb_setup();
2027         if (r < 0)
2028                 return r;
2029 #endif
2030         if (parse_mode(viafb_mode, &dummy_x, &dummy_y)
2031                 || !viafb_get_mode(dummy_x, dummy_y)
2032                 || parse_mode(viafb_mode1, &dummy_x, &dummy_y)
2033                 || !viafb_get_mode(dummy_x, dummy_y)
2034                 || viafb_bpp < 0 || viafb_bpp > 32
2035                 || viafb_bpp1 < 0 || viafb_bpp1 > 32
2036                 || parse_active_dev())
2037                 return -EINVAL;
2038
2039         printk(KERN_INFO
2040        "VIA Graphics Integration Chipset framebuffer %d.%d initializing\n",
2041                VERSION_MAJOR, VERSION_MINOR);
2042         return r;
2043 }
2044
2045 void __exit viafb_exit(void)
2046 {
2047         DEBUG_MSG(KERN_INFO "viafb_exit!\n");
2048 }
2049
2050 static struct fb_ops viafb_ops = {
2051         .owner = THIS_MODULE,
2052         .fb_open = viafb_open,
2053         .fb_release = viafb_release,
2054         .fb_check_var = viafb_check_var,
2055         .fb_set_par = viafb_set_par,
2056         .fb_setcolreg = viafb_setcolreg,
2057         .fb_pan_display = viafb_pan_display,
2058         .fb_blank = viafb_blank,
2059         .fb_fillrect = viafb_fillrect,
2060         .fb_copyarea = viafb_copyarea,
2061         .fb_imageblit = viafb_imageblit,
2062         .fb_cursor = viafb_cursor,
2063         .fb_ioctl = viafb_ioctl,
2064         .fb_sync = viafb_sync,
2065 };
2066
2067
2068 #ifdef MODULE
2069 module_param(viafb_mode, charp, S_IRUSR);
2070 MODULE_PARM_DESC(viafb_mode, "Set resolution (default=640x480)");
2071
2072 module_param(viafb_mode1, charp, S_IRUSR);
2073 MODULE_PARM_DESC(viafb_mode1, "Set resolution (default=640x480)");
2074
2075 module_param(viafb_bpp, int, S_IRUSR);
2076 MODULE_PARM_DESC(viafb_bpp, "Set color depth (default=32bpp)");
2077
2078 module_param(viafb_bpp1, int, S_IRUSR);
2079 MODULE_PARM_DESC(viafb_bpp1, "Set color depth (default=32bpp)");
2080
2081 module_param(viafb_refresh, int, S_IRUSR);
2082 MODULE_PARM_DESC(viafb_refresh,
2083         "Set CRT viafb_refresh rate (default = 60)");
2084
2085 module_param(viafb_refresh1, int, S_IRUSR);
2086 MODULE_PARM_DESC(viafb_refresh1,
2087         "Set CRT refresh rate (default = 60)");
2088
2089 module_param(viafb_lcd_panel_id, int, S_IRUSR);
2090 MODULE_PARM_DESC(viafb_lcd_panel_id,
2091         "Set Flat Panel type(Default=1024x768)");
2092
2093 module_param(viafb_lcd_dsp_method, int, S_IRUSR);
2094 MODULE_PARM_DESC(viafb_lcd_dsp_method,
2095         "Set Flat Panel display scaling method.(Default=Expandsion)");
2096
2097 module_param(viafb_SAMM_ON, int, S_IRUSR);
2098 MODULE_PARM_DESC(viafb_SAMM_ON,
2099         "Turn on/off flag of SAMM(Default=OFF)");
2100
2101 module_param(viafb_accel, int, S_IRUSR);
2102 MODULE_PARM_DESC(viafb_accel,
2103         "Set 2D Hardware Acceleration: 0 = OFF, 1 = ON (default)");
2104
2105 module_param(viafb_active_dev, charp, S_IRUSR);
2106 MODULE_PARM_DESC(viafb_active_dev, "Specify active devices.");
2107
2108 module_param(viafb_display_hardware_layout, int, S_IRUSR);
2109 MODULE_PARM_DESC(viafb_display_hardware_layout,
2110         "Display Hardware Layout (LCD Only, DVI Only...,etc)");
2111
2112 module_param(viafb_second_size, int, S_IRUSR);
2113 MODULE_PARM_DESC(viafb_second_size,
2114         "Set secondary device memory size");
2115
2116 module_param(viafb_dual_fb, int, S_IRUSR);
2117 MODULE_PARM_DESC(viafb_dual_fb,
2118         "Turn on/off flag of dual framebuffer devices.(Default = OFF)");
2119
2120 module_param(viafb_platform_epia_dvi, int, S_IRUSR);
2121 MODULE_PARM_DESC(viafb_platform_epia_dvi,
2122         "Turn on/off flag of DVI devices on EPIA board.(Default = OFF)");
2123
2124 module_param(viafb_device_lcd_dualedge, int, S_IRUSR);
2125 MODULE_PARM_DESC(viafb_device_lcd_dualedge,
2126         "Turn on/off flag of dual edge panel.(Default = OFF)");
2127
2128 module_param(viafb_bus_width, int, S_IRUSR);
2129 MODULE_PARM_DESC(viafb_bus_width,
2130         "Set bus width of panel.(Default = 12)");
2131
2132 module_param(viafb_lcd_mode, int, S_IRUSR);
2133 MODULE_PARM_DESC(viafb_lcd_mode,
2134         "Set Flat Panel mode(Default=OPENLDI)");
2135
2136 module_param(viafb_lcd_port, charp, S_IRUSR);
2137 MODULE_PARM_DESC(viafb_lcd_port, "Specify LCD output port.");
2138
2139 module_param(viafb_dvi_port, charp, S_IRUSR);
2140 MODULE_PARM_DESC(viafb_dvi_port, "Specify DVI output port.");
2141
2142 MODULE_LICENSE("GPL");
2143 #endif