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