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