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