xref: /openbmc/linux/drivers/video/fbdev/via/viafbdev.c (revision ae40e94f)
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 
30 #define _MASTER_FILE
31 #include "global.h"
32 
33 static char *viafb_name = "Via";
34 static u32 pseudo_pal[17];
35 
36 /* video mode */
37 static char *viafb_mode;
38 static char *viafb_mode1;
39 static int viafb_bpp = 32;
40 static int viafb_bpp1 = 32;
41 
42 static unsigned int viafb_second_offset;
43 static int viafb_second_size;
44 
45 static int viafb_accel = 1;
46 
47 /* Added for specifying active devices.*/
48 static char *viafb_active_dev;
49 
50 /*Added for specify lcd output port*/
51 static char *viafb_lcd_port = "";
52 static char *viafb_dvi_port = "";
53 
54 static void retrieve_device_setting(struct viafb_ioctl_setting
55 	*setting_info);
56 static int viafb_pan_display(struct fb_var_screeninfo *var,
57 	struct fb_info *info);
58 
59 static struct fb_ops viafb_ops;
60 
61 /* supported output devices on each IGP
62  * only CX700, VX800, VX855, VX900 were documented
63  * VIA_CRT should be everywhere
64  * VIA_6C can be onle pre-CX700 (probably only on CLE266) as 6C is used for PLL
65  * source selection on CX700 and later
66  * K400 seems to support VIA_96, VIA_DVP1, VIA_LVDS{1,2} as in viamode.c
67  */
68 static const u32 supported_odev_map[] = {
69 	[UNICHROME_CLE266]	= VIA_CRT | VIA_LDVP0 | VIA_LDVP1,
70 	[UNICHROME_K400]	= VIA_CRT | VIA_DVP0 | VIA_DVP1 | VIA_LVDS1
71 				| VIA_LVDS2,
72 	[UNICHROME_K800]	= VIA_CRT | VIA_DVP0 | VIA_DVP1 | VIA_LVDS1
73 				| VIA_LVDS2,
74 	[UNICHROME_PM800]	= VIA_CRT | VIA_DVP0 | VIA_DVP1 | VIA_LVDS1
75 				| VIA_LVDS2,
76 	[UNICHROME_CN700]	= VIA_CRT | VIA_DVP0 | VIA_DVP1 | VIA_LVDS1
77 				| VIA_LVDS2,
78 	[UNICHROME_CX700]	= VIA_CRT | VIA_DVP1 | VIA_LVDS1 | VIA_LVDS2,
79 	[UNICHROME_CN750]	= VIA_CRT | VIA_DVP1 | VIA_LVDS1 | VIA_LVDS2,
80 	[UNICHROME_K8M890]	= VIA_CRT | VIA_DVP1 | VIA_LVDS1 | VIA_LVDS2,
81 	[UNICHROME_P4M890]	= VIA_CRT | VIA_DVP1 | VIA_LVDS1 | VIA_LVDS2,
82 	[UNICHROME_P4M900]	= VIA_CRT | VIA_DVP1 | VIA_LVDS1 | VIA_LVDS2,
83 	[UNICHROME_VX800]	= VIA_CRT | VIA_DVP1 | VIA_LVDS1 | VIA_LVDS2,
84 	[UNICHROME_VX855]	= VIA_CRT | VIA_DVP1 | VIA_LVDS1 | VIA_LVDS2,
85 	[UNICHROME_VX900]	= VIA_CRT | VIA_DVP1 | VIA_LVDS1 | VIA_LVDS2,
86 };
87 
88 static void viafb_fill_var_color_info(struct fb_var_screeninfo *var, u8 depth)
89 {
90 	var->grayscale = 0;
91 	var->red.msb_right = 0;
92 	var->green.msb_right = 0;
93 	var->blue.msb_right = 0;
94 	var->transp.offset = 0;
95 	var->transp.length = 0;
96 	var->transp.msb_right = 0;
97 	var->nonstd = 0;
98 	switch (depth) {
99 	case 8:
100 		var->bits_per_pixel = 8;
101 		var->red.offset = 0;
102 		var->green.offset = 0;
103 		var->blue.offset = 0;
104 		var->red.length = 8;
105 		var->green.length = 8;
106 		var->blue.length = 8;
107 		break;
108 	case 15:
109 		var->bits_per_pixel = 16;
110 		var->red.offset = 10;
111 		var->green.offset = 5;
112 		var->blue.offset = 0;
113 		var->red.length = 5;
114 		var->green.length = 5;
115 		var->blue.length = 5;
116 		break;
117 	case 16:
118 		var->bits_per_pixel = 16;
119 		var->red.offset = 11;
120 		var->green.offset = 5;
121 		var->blue.offset = 0;
122 		var->red.length = 5;
123 		var->green.length = 6;
124 		var->blue.length = 5;
125 		break;
126 	case 24:
127 		var->bits_per_pixel = 32;
128 		var->red.offset = 16;
129 		var->green.offset = 8;
130 		var->blue.offset = 0;
131 		var->red.length = 8;
132 		var->green.length = 8;
133 		var->blue.length = 8;
134 		break;
135 	case 30:
136 		var->bits_per_pixel = 32;
137 		var->red.offset = 20;
138 		var->green.offset = 10;
139 		var->blue.offset = 0;
140 		var->red.length = 10;
141 		var->green.length = 10;
142 		var->blue.length = 10;
143 		break;
144 	}
145 }
146 
147 static void viafb_update_fix(struct fb_info *info)
148 {
149 	u32 bpp = info->var.bits_per_pixel;
150 
151 	info->fix.visual =
152 		bpp == 8 ? FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
153 	info->fix.line_length = ALIGN(info->var.xres_virtual * bpp / 8,
154 		VIA_PITCH_SIZE);
155 }
156 
157 static void viafb_setup_fixinfo(struct fb_fix_screeninfo *fix,
158 	struct viafb_par *viaparinfo)
159 {
160 	memset(fix, 0, sizeof(struct fb_fix_screeninfo));
161 	strcpy(fix->id, viafb_name);
162 
163 	fix->smem_start = viaparinfo->fbmem;
164 	fix->smem_len = viaparinfo->fbmem_free;
165 
166 	fix->type = FB_TYPE_PACKED_PIXELS;
167 	fix->type_aux = 0;
168 	fix->visual = FB_VISUAL_TRUECOLOR;
169 
170 	fix->xpanstep = fix->ywrapstep = 0;
171 	fix->ypanstep = 1;
172 
173 	/* Just tell the accel name */
174 	viafbinfo->fix.accel = FB_ACCEL_VIA_UNICHROME;
175 }
176 static int viafb_open(struct fb_info *info, int user)
177 {
178 	DEBUG_MSG(KERN_INFO "viafb_open!\n");
179 	return 0;
180 }
181 
182 static int viafb_release(struct fb_info *info, int user)
183 {
184 	DEBUG_MSG(KERN_INFO "viafb_release!\n");
185 	return 0;
186 }
187 
188 static inline int get_var_refresh(struct fb_var_screeninfo *var)
189 {
190 	u32 htotal, vtotal;
191 
192 	htotal = var->left_margin + var->xres + var->right_margin
193 		+ var->hsync_len;
194 	vtotal = var->upper_margin + var->yres + var->lower_margin
195 		+ var->vsync_len;
196 	return PICOS2KHZ(var->pixclock) * 1000 / (htotal * vtotal);
197 }
198 
199 static int viafb_check_var(struct fb_var_screeninfo *var,
200 	struct fb_info *info)
201 {
202 	int depth, refresh;
203 	struct viafb_par *ppar = info->par;
204 	u32 line;
205 
206 	DEBUG_MSG(KERN_INFO "viafb_check_var!\n");
207 	/* Sanity check */
208 	/* HW neither support interlacte nor double-scaned mode */
209 	if (var->vmode & FB_VMODE_INTERLACED || var->vmode & FB_VMODE_DOUBLE)
210 		return -EINVAL;
211 
212 	/* the refresh rate is not important here, as we only want to know
213 	 * whether the resolution exists
214 	 */
215 	if (!viafb_get_best_mode(var->xres, var->yres, 60)) {
216 		DEBUG_MSG(KERN_INFO
217 			  "viafb: Mode %dx%dx%d not supported!!\n",
218 			  var->xres, var->yres, var->bits_per_pixel);
219 		return -EINVAL;
220 	}
221 
222 	depth = fb_get_color_depth(var, &info->fix);
223 	if (!depth)
224 		depth = var->bits_per_pixel;
225 
226 	if (depth < 0 || depth > 32)
227 		return -EINVAL;
228 	else if (!depth)
229 		depth = 24;
230 	else if (depth == 15 && viafb_dual_fb && ppar->iga_path == IGA1)
231 		depth = 15;
232 	else if (depth == 30)
233 		depth = 30;
234 	else if (depth <= 8)
235 		depth = 8;
236 	else if (depth <= 16)
237 		depth = 16;
238 	else
239 		depth = 24;
240 
241 	viafb_fill_var_color_info(var, depth);
242 	if (var->xres_virtual < var->xres)
243 		var->xres_virtual = var->xres;
244 
245 	line = ALIGN(var->xres_virtual * var->bits_per_pixel / 8,
246 		VIA_PITCH_SIZE);
247 	if (line > VIA_PITCH_MAX || line * var->yres_virtual > ppar->memsize)
248 		return -EINVAL;
249 
250 	/* Based on var passed in to calculate the refresh,
251 	 * because our driver use some modes special.
252 	 */
253 	refresh = viafb_get_refresh(var->xres, var->yres,
254 		get_var_refresh(var));
255 
256 	/* Adjust var according to our driver's own table */
257 	viafb_fill_var_timing_info(var,
258 		viafb_get_best_mode(var->xres, var->yres, refresh));
259 	if (var->accel_flags & FB_ACCELF_TEXT &&
260 		!ppar->shared->vdev->engine_mmio)
261 		var->accel_flags = 0;
262 
263 	return 0;
264 }
265 
266 static int viafb_set_par(struct fb_info *info)
267 {
268 	struct viafb_par *viapar = info->par;
269 	int refresh;
270 	DEBUG_MSG(KERN_INFO "viafb_set_par!\n");
271 
272 	viafb_update_fix(info);
273 	viapar->depth = fb_get_color_depth(&info->var, &info->fix);
274 	viafb_update_device_setting(viafbinfo->var.xres, viafbinfo->var.yres,
275 		viafbinfo->var.bits_per_pixel, 0);
276 
277 	if (viafb_dual_fb) {
278 		viafb_update_device_setting(viafbinfo1->var.xres,
279 			viafbinfo1->var.yres, viafbinfo1->var.bits_per_pixel,
280 			1);
281 	} else if (viafb_SAMM_ON == 1) {
282 		DEBUG_MSG(KERN_INFO
283 		"viafb_second_xres = %d, viafb_second_yres = %d, bpp = %d\n",
284 			  viafb_second_xres, viafb_second_yres, viafb_bpp1);
285 
286 		viafb_update_device_setting(viafb_second_xres,
287 			viafb_second_yres, viafb_bpp1, 1);
288 	}
289 
290 	refresh = get_var_refresh(&info->var);
291 	if (viafb_dual_fb && viapar->iga_path == IGA2) {
292 		viafb_bpp1 = info->var.bits_per_pixel;
293 		viafb_refresh1 = refresh;
294 	} else {
295 		viafb_bpp = info->var.bits_per_pixel;
296 		viafb_refresh = refresh;
297 	}
298 
299 	if (info->var.accel_flags & FB_ACCELF_TEXT)
300 		info->flags &= ~FBINFO_HWACCEL_DISABLED;
301 	else
302 		info->flags |= FBINFO_HWACCEL_DISABLED;
303 	viafb_setmode();
304 	viafb_pan_display(&info->var, info);
305 
306 	return 0;
307 }
308 
309 /* Set one color register */
310 static int viafb_setcolreg(unsigned regno, unsigned red, unsigned green,
311 unsigned blue, unsigned transp, struct fb_info *info)
312 {
313 	struct viafb_par *viapar = info->par;
314 	u32 r, g, b;
315 
316 	if (info->fix.visual == FB_VISUAL_PSEUDOCOLOR) {
317 		if (regno > 255)
318 			return -EINVAL;
319 
320 		if (!viafb_dual_fb || viapar->iga_path == IGA1)
321 			viafb_set_primary_color_register(regno, red >> 8,
322 				green >> 8, blue >> 8);
323 
324 		if (!viafb_dual_fb || viapar->iga_path == IGA2)
325 			viafb_set_secondary_color_register(regno, red >> 8,
326 				green >> 8, blue >> 8);
327 	} else {
328 		if (regno > 15)
329 			return -EINVAL;
330 
331 		r = (red >> (16 - info->var.red.length))
332 			<< info->var.red.offset;
333 		b = (blue >> (16 - info->var.blue.length))
334 			<< info->var.blue.offset;
335 		g = (green >> (16 - info->var.green.length))
336 			<< info->var.green.offset;
337 		((u32 *) info->pseudo_palette)[regno] = r | g | b;
338 	}
339 
340 	return 0;
341 }
342 
343 static int viafb_pan_display(struct fb_var_screeninfo *var,
344 	struct fb_info *info)
345 {
346 	struct viafb_par *viapar = info->par;
347 	u32 vram_addr = viapar->vram_addr
348 		+ var->yoffset * info->fix.line_length
349 		+ var->xoffset * info->var.bits_per_pixel / 8;
350 
351 	DEBUG_MSG(KERN_DEBUG "viafb_pan_display, address = %d\n", vram_addr);
352 	if (!viafb_dual_fb) {
353 		via_set_primary_address(vram_addr);
354 		via_set_secondary_address(vram_addr);
355 	} else if (viapar->iga_path == IGA1)
356 		via_set_primary_address(vram_addr);
357 	else
358 		via_set_secondary_address(vram_addr);
359 
360 	return 0;
361 }
362 
363 static int viafb_blank(int blank_mode, struct fb_info *info)
364 {
365 	DEBUG_MSG(KERN_INFO "viafb_blank!\n");
366 	/* clear DPMS setting */
367 
368 	switch (blank_mode) {
369 	case FB_BLANK_UNBLANK:
370 		/* Screen: On, HSync: On, VSync: On */
371 		/* control CRT monitor power management */
372 		via_set_state(VIA_CRT, VIA_STATE_ON);
373 		break;
374 	case FB_BLANK_HSYNC_SUSPEND:
375 		/* Screen: Off, HSync: Off, VSync: On */
376 		/* control CRT monitor power management */
377 		via_set_state(VIA_CRT, VIA_STATE_STANDBY);
378 		break;
379 	case FB_BLANK_VSYNC_SUSPEND:
380 		/* Screen: Off, HSync: On, VSync: Off */
381 		/* control CRT monitor power management */
382 		via_set_state(VIA_CRT, VIA_STATE_SUSPEND);
383 		break;
384 	case FB_BLANK_POWERDOWN:
385 		/* Screen: Off, HSync: Off, VSync: Off */
386 		/* control CRT monitor power management */
387 		via_set_state(VIA_CRT, VIA_STATE_OFF);
388 		break;
389 	}
390 
391 	return 0;
392 }
393 
394 static int viafb_ioctl(struct fb_info *info, u_int cmd, u_long arg)
395 {
396 	union {
397 		struct viafb_ioctl_mode viamode;
398 		struct viafb_ioctl_samm viasamm;
399 		struct viafb_driver_version driver_version;
400 		struct fb_var_screeninfo sec_var;
401 		struct _panel_size_pos_info panel_pos_size_para;
402 		struct viafb_ioctl_setting viafb_setting;
403 		struct device_t active_dev;
404 	} u;
405 	u32 state_info = 0;
406 	u32 *viafb_gamma_table;
407 	char driver_name[] = "viafb";
408 
409 	u32 __user *argp = (u32 __user *) arg;
410 	u32 gpu32;
411 
412 	DEBUG_MSG(KERN_INFO "viafb_ioctl: 0x%X !!\n", cmd);
413 	printk(KERN_WARNING "viafb_ioctl: Please avoid this interface as it is unstable and might change or vanish at any time!\n");
414 	memset(&u, 0, sizeof(u));
415 
416 	switch (cmd) {
417 	case VIAFB_GET_CHIP_INFO:
418 		if (copy_to_user(argp, viaparinfo->chip_info,
419 				sizeof(struct chip_information)))
420 			return -EFAULT;
421 		break;
422 	case VIAFB_GET_INFO_SIZE:
423 		return put_user((u32)sizeof(struct viafb_ioctl_info), argp);
424 	case VIAFB_GET_INFO:
425 		return viafb_ioctl_get_viafb_info(arg);
426 	case VIAFB_HOTPLUG:
427 		return put_user(viafb_ioctl_hotplug(info->var.xres,
428 					      info->var.yres,
429 					      info->var.bits_per_pixel), argp);
430 	case VIAFB_SET_HOTPLUG_FLAG:
431 		if (copy_from_user(&gpu32, argp, sizeof(gpu32)))
432 			return -EFAULT;
433 		viafb_hotplug = (gpu32) ? 1 : 0;
434 		break;
435 	case VIAFB_GET_RESOLUTION:
436 		u.viamode.xres = (u32) viafb_hotplug_Xres;
437 		u.viamode.yres = (u32) viafb_hotplug_Yres;
438 		u.viamode.refresh = (u32) viafb_hotplug_refresh;
439 		u.viamode.bpp = (u32) viafb_hotplug_bpp;
440 		if (viafb_SAMM_ON == 1) {
441 			u.viamode.xres_sec = viafb_second_xres;
442 			u.viamode.yres_sec = viafb_second_yres;
443 			u.viamode.virtual_xres_sec = viafb_dual_fb ? viafbinfo1->var.xres_virtual : viafbinfo->var.xres_virtual;
444 			u.viamode.virtual_yres_sec = viafb_dual_fb ? viafbinfo1->var.yres_virtual : viafbinfo->var.yres_virtual;
445 			u.viamode.refresh_sec = viafb_refresh1;
446 			u.viamode.bpp_sec = viafb_bpp1;
447 		} else {
448 			u.viamode.xres_sec = 0;
449 			u.viamode.yres_sec = 0;
450 			u.viamode.virtual_xres_sec = 0;
451 			u.viamode.virtual_yres_sec = 0;
452 			u.viamode.refresh_sec = 0;
453 			u.viamode.bpp_sec = 0;
454 		}
455 		if (copy_to_user(argp, &u.viamode, sizeof(u.viamode)))
456 			return -EFAULT;
457 		break;
458 	case VIAFB_GET_SAMM_INFO:
459 		u.viasamm.samm_status = viafb_SAMM_ON;
460 
461 		if (viafb_SAMM_ON == 1) {
462 			if (viafb_dual_fb) {
463 				u.viasamm.size_prim = viaparinfo->fbmem_free;
464 				u.viasamm.size_sec = viaparinfo1->fbmem_free;
465 			} else {
466 				if (viafb_second_size) {
467 					u.viasamm.size_prim =
468 					    viaparinfo->fbmem_free -
469 					    viafb_second_size * 1024 * 1024;
470 					u.viasamm.size_sec =
471 					    viafb_second_size * 1024 * 1024;
472 				} else {
473 					u.viasamm.size_prim =
474 					    viaparinfo->fbmem_free >> 1;
475 					u.viasamm.size_sec =
476 					    (viaparinfo->fbmem_free >> 1);
477 				}
478 			}
479 			u.viasamm.mem_base = viaparinfo->fbmem;
480 			u.viasamm.offset_sec = viafb_second_offset;
481 		} else {
482 			u.viasamm.size_prim =
483 			    viaparinfo->memsize - viaparinfo->fbmem_used;
484 			u.viasamm.size_sec = 0;
485 			u.viasamm.mem_base = viaparinfo->fbmem;
486 			u.viasamm.offset_sec = 0;
487 		}
488 
489 		if (copy_to_user(argp, &u.viasamm, sizeof(u.viasamm)))
490 			return -EFAULT;
491 
492 		break;
493 	case VIAFB_TURN_ON_OUTPUT_DEVICE:
494 		if (copy_from_user(&gpu32, argp, sizeof(gpu32)))
495 			return -EFAULT;
496 		if (gpu32 & CRT_Device)
497 			via_set_state(VIA_CRT, VIA_STATE_ON);
498 		if (gpu32 & DVI_Device)
499 			viafb_dvi_enable();
500 		if (gpu32 & LCD_Device)
501 			viafb_lcd_enable();
502 		break;
503 	case VIAFB_TURN_OFF_OUTPUT_DEVICE:
504 		if (copy_from_user(&gpu32, argp, sizeof(gpu32)))
505 			return -EFAULT;
506 		if (gpu32 & CRT_Device)
507 			via_set_state(VIA_CRT, VIA_STATE_OFF);
508 		if (gpu32 & DVI_Device)
509 			viafb_dvi_disable();
510 		if (gpu32 & LCD_Device)
511 			viafb_lcd_disable();
512 		break;
513 	case VIAFB_GET_DEVICE:
514 		u.active_dev.crt = viafb_CRT_ON;
515 		u.active_dev.dvi = viafb_DVI_ON;
516 		u.active_dev.lcd = viafb_LCD_ON;
517 		u.active_dev.samm = viafb_SAMM_ON;
518 		u.active_dev.primary_dev = viafb_primary_dev;
519 
520 		u.active_dev.lcd_dsp_cent = viafb_lcd_dsp_method;
521 		u.active_dev.lcd_panel_id = viafb_lcd_panel_id;
522 		u.active_dev.lcd_mode = viafb_lcd_mode;
523 
524 		u.active_dev.xres = viafb_hotplug_Xres;
525 		u.active_dev.yres = viafb_hotplug_Yres;
526 
527 		u.active_dev.xres1 = viafb_second_xres;
528 		u.active_dev.yres1 = viafb_second_yres;
529 
530 		u.active_dev.bpp = viafb_bpp;
531 		u.active_dev.bpp1 = viafb_bpp1;
532 		u.active_dev.refresh = viafb_refresh;
533 		u.active_dev.refresh1 = viafb_refresh1;
534 
535 		u.active_dev.epia_dvi = viafb_platform_epia_dvi;
536 		u.active_dev.lcd_dual_edge = viafb_device_lcd_dualedge;
537 		u.active_dev.bus_width = viafb_bus_width;
538 
539 		if (copy_to_user(argp, &u.active_dev, sizeof(u.active_dev)))
540 			return -EFAULT;
541 		break;
542 
543 	case VIAFB_GET_DRIVER_VERSION:
544 		u.driver_version.iMajorNum = VERSION_MAJOR;
545 		u.driver_version.iKernelNum = VERSION_KERNEL;
546 		u.driver_version.iOSNum = VERSION_OS;
547 		u.driver_version.iMinorNum = VERSION_MINOR;
548 
549 		if (copy_to_user(argp, &u.driver_version,
550 			sizeof(u.driver_version)))
551 			return -EFAULT;
552 
553 		break;
554 
555 	case VIAFB_GET_DEVICE_INFO:
556 
557 		retrieve_device_setting(&u.viafb_setting);
558 
559 		if (copy_to_user(argp, &u.viafb_setting,
560 				 sizeof(u.viafb_setting)))
561 			return -EFAULT;
562 
563 		break;
564 
565 	case VIAFB_GET_DEVICE_SUPPORT:
566 		viafb_get_device_support_state(&state_info);
567 		if (put_user(state_info, argp))
568 			return -EFAULT;
569 		break;
570 
571 	case VIAFB_GET_DEVICE_CONNECT:
572 		viafb_get_device_connect_state(&state_info);
573 		if (put_user(state_info, argp))
574 			return -EFAULT;
575 		break;
576 
577 	case VIAFB_GET_PANEL_SUPPORT_EXPAND:
578 		state_info =
579 		    viafb_lcd_get_support_expand_state(info->var.xres,
580 						 info->var.yres);
581 		if (put_user(state_info, argp))
582 			return -EFAULT;
583 		break;
584 
585 	case VIAFB_GET_DRIVER_NAME:
586 		if (copy_to_user(argp, driver_name, sizeof(driver_name)))
587 			return -EFAULT;
588 		break;
589 
590 	case VIAFB_SET_GAMMA_LUT:
591 		viafb_gamma_table = memdup_user(argp, 256 * sizeof(u32));
592 		if (IS_ERR(viafb_gamma_table))
593 			return PTR_ERR(viafb_gamma_table);
594 		viafb_set_gamma_table(viafb_bpp, viafb_gamma_table);
595 		kfree(viafb_gamma_table);
596 		break;
597 
598 	case VIAFB_GET_GAMMA_LUT:
599 		viafb_gamma_table = kmalloc_array(256, sizeof(u32),
600 						  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 ssize_t odev_update(const char __user *buffer, size_t count, u32 *odev)
1480 {
1481 	char buf[64], *ptr = buf;
1482 	u32 devices;
1483 	bool add, sub;
1484 
1485 	if (count < 1 || count > 63)
1486 		return -EINVAL;
1487 	if (copy_from_user(&buf[0], buffer, count))
1488 		return -EFAULT;
1489 	buf[count] = '\0';
1490 	add = buf[0] == '+';
1491 	sub = buf[0] == '-';
1492 	if (add || sub)
1493 		ptr++;
1494 	devices = via_parse_odev(ptr, &ptr);
1495 	if (*ptr == '\n')
1496 		ptr++;
1497 	if (*ptr != 0)
1498 		return -EINVAL;
1499 	if (add)
1500 		*odev |= devices;
1501 	else if (sub)
1502 		*odev &= ~devices;
1503 	else
1504 		*odev = devices;
1505 	return count;
1506 }
1507 
1508 static int viafb_iga1_odev_proc_show(struct seq_file *m, void *v)
1509 {
1510 	via_odev_to_seq(m, viaparinfo->shared->iga1_devices);
1511 	return 0;
1512 }
1513 
1514 static int viafb_iga1_odev_proc_open(struct inode *inode, struct file *file)
1515 {
1516 	return single_open(file, viafb_iga1_odev_proc_show, NULL);
1517 }
1518 
1519 static ssize_t viafb_iga1_odev_proc_write(struct file *file,
1520 	const char __user *buffer, size_t count, loff_t *pos)
1521 {
1522 	u32 dev_on, dev_off, dev_old, dev_new;
1523 	ssize_t res;
1524 
1525 	dev_old = dev_new = viaparinfo->shared->iga1_devices;
1526 	res = odev_update(buffer, count, &dev_new);
1527 	if (res != count)
1528 		return res;
1529 	dev_off = dev_old & ~dev_new;
1530 	dev_on = dev_new & ~dev_old;
1531 	viaparinfo->shared->iga1_devices = dev_new;
1532 	viaparinfo->shared->iga2_devices &= ~dev_new;
1533 	via_set_state(dev_off, VIA_STATE_OFF);
1534 	via_set_source(dev_new, IGA1);
1535 	via_set_state(dev_on, VIA_STATE_ON);
1536 	return res;
1537 }
1538 
1539 static const struct file_operations viafb_iga1_odev_proc_fops = {
1540 	.owner		= THIS_MODULE,
1541 	.open		= viafb_iga1_odev_proc_open,
1542 	.read		= seq_read,
1543 	.llseek		= seq_lseek,
1544 	.release	= single_release,
1545 	.write		= viafb_iga1_odev_proc_write,
1546 };
1547 
1548 static int viafb_iga2_odev_proc_show(struct seq_file *m, void *v)
1549 {
1550 	via_odev_to_seq(m, viaparinfo->shared->iga2_devices);
1551 	return 0;
1552 }
1553 
1554 static int viafb_iga2_odev_proc_open(struct inode *inode, struct file *file)
1555 {
1556 	return single_open(file, viafb_iga2_odev_proc_show, NULL);
1557 }
1558 
1559 static ssize_t viafb_iga2_odev_proc_write(struct file *file,
1560 	const char __user *buffer, size_t count, loff_t *pos)
1561 {
1562 	u32 dev_on, dev_off, dev_old, dev_new;
1563 	ssize_t res;
1564 
1565 	dev_old = dev_new = viaparinfo->shared->iga2_devices;
1566 	res = odev_update(buffer, count, &dev_new);
1567 	if (res != count)
1568 		return res;
1569 	dev_off = dev_old & ~dev_new;
1570 	dev_on = dev_new & ~dev_old;
1571 	viaparinfo->shared->iga2_devices = dev_new;
1572 	viaparinfo->shared->iga1_devices &= ~dev_new;
1573 	via_set_state(dev_off, VIA_STATE_OFF);
1574 	via_set_source(dev_new, IGA2);
1575 	via_set_state(dev_on, VIA_STATE_ON);
1576 	return res;
1577 }
1578 
1579 static const struct file_operations viafb_iga2_odev_proc_fops = {
1580 	.owner		= THIS_MODULE,
1581 	.open		= viafb_iga2_odev_proc_open,
1582 	.read		= seq_read,
1583 	.llseek		= seq_lseek,
1584 	.release	= single_release,
1585 	.write		= viafb_iga2_odev_proc_write,
1586 };
1587 
1588 #define IS_VT1636(lvds_chip)	((lvds_chip).lvds_chip_name == VT1636_LVDS)
1589 static void viafb_init_proc(struct viafb_shared *shared)
1590 {
1591 	struct proc_dir_entry *iga1_entry, *iga2_entry,
1592 		*viafb_entry = proc_mkdir("viafb", NULL);
1593 
1594 	shared->proc_entry = viafb_entry;
1595 	if (viafb_entry) {
1596 #ifdef CONFIG_FB_VIA_DIRECT_PROCFS
1597 		proc_create("dvp0", 0, viafb_entry, &viafb_dvp0_proc_fops);
1598 		proc_create("dvp1", 0, viafb_entry, &viafb_dvp1_proc_fops);
1599 		proc_create("dfph", 0, viafb_entry, &viafb_dfph_proc_fops);
1600 		proc_create("dfpl", 0, viafb_entry, &viafb_dfpl_proc_fops);
1601 		if (IS_VT1636(shared->chip_info.lvds_chip_info)
1602 			|| IS_VT1636(shared->chip_info.lvds_chip_info2))
1603 			proc_create("vt1636", 0, viafb_entry,
1604 				&viafb_vt1636_proc_fops);
1605 #endif /* CONFIG_FB_VIA_DIRECT_PROCFS */
1606 
1607 		proc_create_single("supported_output_devices", 0, viafb_entry,
1608 			viafb_sup_odev_proc_show);
1609 		iga1_entry = proc_mkdir("iga1", viafb_entry);
1610 		shared->iga1_proc_entry = iga1_entry;
1611 		proc_create("output_devices", 0, iga1_entry,
1612 			&viafb_iga1_odev_proc_fops);
1613 		iga2_entry = proc_mkdir("iga2", viafb_entry);
1614 		shared->iga2_proc_entry = iga2_entry;
1615 		proc_create("output_devices", 0, iga2_entry,
1616 			&viafb_iga2_odev_proc_fops);
1617 	}
1618 }
1619 static void viafb_remove_proc(struct viafb_shared *shared)
1620 {
1621 	struct proc_dir_entry *viafb_entry = shared->proc_entry;
1622 
1623 	if (!viafb_entry)
1624 		return;
1625 
1626 	remove_proc_entry("output_devices", shared->iga2_proc_entry);
1627 	remove_proc_entry("iga2", viafb_entry);
1628 	remove_proc_entry("output_devices", shared->iga1_proc_entry);
1629 	remove_proc_entry("iga1", viafb_entry);
1630 	remove_proc_entry("supported_output_devices", viafb_entry);
1631 
1632 #ifdef CONFIG_FB_VIA_DIRECT_PROCFS
1633 	remove_proc_entry("dvp0", viafb_entry);/* parent dir */
1634 	remove_proc_entry("dvp1", viafb_entry);
1635 	remove_proc_entry("dfph", viafb_entry);
1636 	remove_proc_entry("dfpl", viafb_entry);
1637 	if (IS_VT1636(shared->chip_info.lvds_chip_info)
1638 		|| IS_VT1636(shared->chip_info.lvds_chip_info2))
1639 		remove_proc_entry("vt1636", viafb_entry);
1640 #endif /* CONFIG_FB_VIA_DIRECT_PROCFS */
1641 
1642 	remove_proc_entry("viafb", NULL);
1643 }
1644 #undef IS_VT1636
1645 
1646 static int parse_mode(const char *str, u32 devices, u32 *xres, u32 *yres)
1647 {
1648 	const struct fb_videomode *mode = NULL;
1649 	char *ptr;
1650 
1651 	if (!str) {
1652 		if (devices == VIA_CRT)
1653 			mode = via_aux_get_preferred_mode(
1654 				viaparinfo->shared->i2c_26);
1655 		else if (devices == VIA_DVP1)
1656 			mode = via_aux_get_preferred_mode(
1657 				viaparinfo->shared->i2c_31);
1658 
1659 		if (mode) {
1660 			*xres = mode->xres;
1661 			*yres = mode->yres;
1662 		} else if (machine_is_olpc()) {
1663 			*xres = 1200;
1664 			*yres = 900;
1665 		} else {
1666 			*xres = 640;
1667 			*yres = 480;
1668 		}
1669 		return 0;
1670 	}
1671 
1672 	*xres = simple_strtoul(str, &ptr, 10);
1673 	if (ptr[0] != 'x')
1674 		return -EINVAL;
1675 
1676 	*yres = simple_strtoul(&ptr[1], &ptr, 10);
1677 	if (ptr[0])
1678 		return -EINVAL;
1679 
1680 	return 0;
1681 }
1682 
1683 
1684 #ifdef CONFIG_PM
1685 static int viafb_suspend(void *unused)
1686 {
1687 	console_lock();
1688 	fb_set_suspend(viafbinfo, 1);
1689 	viafb_sync(viafbinfo);
1690 	console_unlock();
1691 
1692 	return 0;
1693 }
1694 
1695 static int viafb_resume(void *unused)
1696 {
1697 	console_lock();
1698 	if (viaparinfo->shared->vdev->engine_mmio)
1699 		viafb_reset_engine(viaparinfo);
1700 	viafb_set_par(viafbinfo);
1701 	if (viafb_dual_fb)
1702 		viafb_set_par(viafbinfo1);
1703 	fb_set_suspend(viafbinfo, 0);
1704 
1705 	console_unlock();
1706 	return 0;
1707 }
1708 
1709 static struct viafb_pm_hooks viafb_fb_pm_hooks = {
1710 	.suspend = viafb_suspend,
1711 	.resume = viafb_resume
1712 };
1713 
1714 #endif
1715 
1716 static void i2c_bus_probe(struct viafb_shared *shared)
1717 {
1718 	/* should be always CRT */
1719 	printk(KERN_INFO "viafb: Probing I2C bus 0x26\n");
1720 	shared->i2c_26 = via_aux_probe(viafb_find_i2c_adapter(VIA_PORT_26));
1721 
1722 	/* seems to be usually DVP1 */
1723 	printk(KERN_INFO "viafb: Probing I2C bus 0x31\n");
1724 	shared->i2c_31 = via_aux_probe(viafb_find_i2c_adapter(VIA_PORT_31));
1725 
1726 	/* FIXME: what is this? */
1727 	if (!machine_is_olpc()) {
1728 		printk(KERN_INFO "viafb: Probing I2C bus 0x2C\n");
1729 		shared->i2c_2C = via_aux_probe(viafb_find_i2c_adapter(VIA_PORT_2C));
1730 	}
1731 
1732 	printk(KERN_INFO "viafb: Finished I2C bus probing");
1733 }
1734 
1735 static void i2c_bus_free(struct viafb_shared *shared)
1736 {
1737 	via_aux_free(shared->i2c_26);
1738 	via_aux_free(shared->i2c_31);
1739 	via_aux_free(shared->i2c_2C);
1740 }
1741 
1742 int via_fb_pci_probe(struct viafb_dev *vdev)
1743 {
1744 	u32 default_xres, default_yres;
1745 	struct fb_var_screeninfo default_var;
1746 	int rc;
1747 	u32 viafb_par_length;
1748 
1749 	DEBUG_MSG(KERN_INFO "VIAFB PCI Probe!!\n");
1750 	memset(&default_var, 0, sizeof(default_var));
1751 	viafb_par_length = ALIGN(sizeof(struct viafb_par), BITS_PER_LONG/8);
1752 
1753 	/* Allocate fb_info and ***_par here, also including some other needed
1754 	 * variables
1755 	*/
1756 	viafbinfo = framebuffer_alloc(viafb_par_length +
1757 		ALIGN(sizeof(struct viafb_shared), BITS_PER_LONG/8),
1758 		&vdev->pdev->dev);
1759 	if (!viafbinfo) {
1760 		printk(KERN_ERR"Could not allocate memory for viafb_info.\n");
1761 		return -ENOMEM;
1762 	}
1763 
1764 	viaparinfo = (struct viafb_par *)viafbinfo->par;
1765 	viaparinfo->shared = viafbinfo->par + viafb_par_length;
1766 	viaparinfo->shared->vdev = vdev;
1767 	viaparinfo->vram_addr = 0;
1768 	viaparinfo->tmds_setting_info = &viaparinfo->shared->tmds_setting_info;
1769 	viaparinfo->lvds_setting_info = &viaparinfo->shared->lvds_setting_info;
1770 	viaparinfo->lvds_setting_info2 =
1771 		&viaparinfo->shared->lvds_setting_info2;
1772 	viaparinfo->chip_info = &viaparinfo->shared->chip_info;
1773 
1774 	i2c_bus_probe(viaparinfo->shared);
1775 	if (viafb_dual_fb)
1776 		viafb_SAMM_ON = 1;
1777 	parse_lcd_port();
1778 	parse_dvi_port();
1779 
1780 	viafb_init_chip_info(vdev->chip_type);
1781 	/*
1782 	 * The framebuffer will have been successfully mapped by
1783 	 * the core (or we'd not be here), but we still need to
1784 	 * set up our own accounting.
1785 	 */
1786 	viaparinfo->fbmem = vdev->fbmem_start;
1787 	viaparinfo->memsize = vdev->fbmem_len;
1788 	viaparinfo->fbmem_free = viaparinfo->memsize;
1789 	viaparinfo->fbmem_used = 0;
1790 	viafbinfo->screen_base = vdev->fbmem;
1791 
1792 	viafbinfo->fix.mmio_start = vdev->engine_start;
1793 	viafbinfo->fix.mmio_len = vdev->engine_len;
1794 	viafbinfo->node = 0;
1795 	viafbinfo->fbops = &viafb_ops;
1796 	viafbinfo->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
1797 
1798 	viafbinfo->pseudo_palette = pseudo_pal;
1799 	if (viafb_accel && !viafb_setup_engine(viafbinfo)) {
1800 		viafbinfo->flags |= FBINFO_HWACCEL_COPYAREA |
1801 			FBINFO_HWACCEL_FILLRECT |  FBINFO_HWACCEL_IMAGEBLIT;
1802 		default_var.accel_flags = FB_ACCELF_TEXT;
1803 	} else {
1804 		viafbinfo->flags |= FBINFO_HWACCEL_DISABLED;
1805 		default_var.accel_flags = 0;
1806 	}
1807 
1808 	if (viafb_second_size && (viafb_second_size < 8)) {
1809 		viafb_second_offset = viaparinfo->fbmem_free -
1810 			viafb_second_size * 1024 * 1024;
1811 	} else {
1812 		viafb_second_size = 8;
1813 		viafb_second_offset = viaparinfo->fbmem_free -
1814 			viafb_second_size * 1024 * 1024;
1815 	}
1816 
1817 	parse_mode(viafb_mode, viaparinfo->shared->iga1_devices,
1818 		&default_xres, &default_yres);
1819 	if (viafb_SAMM_ON == 1)
1820 		parse_mode(viafb_mode1, viaparinfo->shared->iga2_devices,
1821 			&viafb_second_xres, &viafb_second_yres);
1822 
1823 	default_var.xres = default_xres;
1824 	default_var.yres = default_yres;
1825 	default_var.xres_virtual = default_xres;
1826 	default_var.yres_virtual = default_yres;
1827 	default_var.bits_per_pixel = viafb_bpp;
1828 	viafb_fill_var_timing_info(&default_var, viafb_get_best_mode(
1829 		default_var.xres, default_var.yres, viafb_refresh));
1830 	viafb_setup_fixinfo(&viafbinfo->fix, viaparinfo);
1831 	viafbinfo->var = default_var;
1832 
1833 	if (viafb_dual_fb) {
1834 		viafbinfo1 = framebuffer_alloc(viafb_par_length,
1835 				&vdev->pdev->dev);
1836 		if (!viafbinfo1) {
1837 			printk(KERN_ERR
1838 			"allocate the second framebuffer struct error\n");
1839 			rc = -ENOMEM;
1840 			goto out_fb_release;
1841 		}
1842 		viaparinfo1 = viafbinfo1->par;
1843 		memcpy(viaparinfo1, viaparinfo, viafb_par_length);
1844 		viaparinfo1->vram_addr = viafb_second_offset;
1845 		viaparinfo1->memsize = viaparinfo->memsize -
1846 			viafb_second_offset;
1847 		viaparinfo->memsize = viafb_second_offset;
1848 		viaparinfo1->fbmem = viaparinfo->fbmem + viafb_second_offset;
1849 
1850 		viaparinfo1->fbmem_used = viaparinfo->fbmem_used;
1851 		viaparinfo1->fbmem_free = viaparinfo1->memsize -
1852 			viaparinfo1->fbmem_used;
1853 		viaparinfo->fbmem_free = viaparinfo->memsize;
1854 		viaparinfo->fbmem_used = 0;
1855 
1856 		viaparinfo->iga_path = IGA1;
1857 		viaparinfo1->iga_path = IGA2;
1858 		memcpy(viafbinfo1, viafbinfo, sizeof(struct fb_info));
1859 		viafbinfo1->par = viaparinfo1;
1860 		viafbinfo1->screen_base = viafbinfo->screen_base +
1861 			viafb_second_offset;
1862 
1863 		default_var.xres = viafb_second_xres;
1864 		default_var.yres = viafb_second_yres;
1865 		default_var.xres_virtual = viafb_second_xres;
1866 		default_var.yres_virtual = viafb_second_yres;
1867 		default_var.bits_per_pixel = viafb_bpp1;
1868 		viafb_fill_var_timing_info(&default_var, viafb_get_best_mode(
1869 			default_var.xres, default_var.yres, viafb_refresh1));
1870 
1871 		viafb_setup_fixinfo(&viafbinfo1->fix, viaparinfo1);
1872 		viafb_check_var(&default_var, viafbinfo1);
1873 		viafbinfo1->var = default_var;
1874 		viafb_update_fix(viafbinfo1);
1875 		viaparinfo1->depth = fb_get_color_depth(&viafbinfo1->var,
1876 			&viafbinfo1->fix);
1877 	}
1878 
1879 	viafb_check_var(&viafbinfo->var, viafbinfo);
1880 	viafb_update_fix(viafbinfo);
1881 	viaparinfo->depth = fb_get_color_depth(&viafbinfo->var,
1882 		&viafbinfo->fix);
1883 	default_var.activate = FB_ACTIVATE_NOW;
1884 	rc = fb_alloc_cmap(&viafbinfo->cmap, 256, 0);
1885 	if (rc)
1886 		goto out_fb1_release;
1887 
1888 	if (viafb_dual_fb && (viafb_primary_dev == LCD_Device)
1889 	    && (viaparinfo->chip_info->gfx_chip_name == UNICHROME_CLE266)) {
1890 		rc = register_framebuffer(viafbinfo1);
1891 		if (rc)
1892 			goto out_dealloc_cmap;
1893 	}
1894 	rc = register_framebuffer(viafbinfo);
1895 	if (rc)
1896 		goto out_fb1_unreg_lcd_cle266;
1897 
1898 	if (viafb_dual_fb && ((viafb_primary_dev != LCD_Device)
1899 			|| (viaparinfo->chip_info->gfx_chip_name !=
1900 			UNICHROME_CLE266))) {
1901 		rc = register_framebuffer(viafbinfo1);
1902 		if (rc)
1903 			goto out_fb_unreg;
1904 	}
1905 	DEBUG_MSG(KERN_INFO "fb%d: %s frame buffer device %dx%d-%dbpp\n",
1906 		  viafbinfo->node, viafbinfo->fix.id, default_var.xres,
1907 		  default_var.yres, default_var.bits_per_pixel);
1908 
1909 	viafb_init_proc(viaparinfo->shared);
1910 	viafb_init_dac(IGA2);
1911 
1912 #ifdef CONFIG_PM
1913 	viafb_pm_register(&viafb_fb_pm_hooks);
1914 #endif
1915 	return 0;
1916 
1917 out_fb_unreg:
1918 	unregister_framebuffer(viafbinfo);
1919 out_fb1_unreg_lcd_cle266:
1920 	if (viafb_dual_fb && (viafb_primary_dev == LCD_Device)
1921 	    && (viaparinfo->chip_info->gfx_chip_name == UNICHROME_CLE266))
1922 		unregister_framebuffer(viafbinfo1);
1923 out_dealloc_cmap:
1924 	fb_dealloc_cmap(&viafbinfo->cmap);
1925 out_fb1_release:
1926 	framebuffer_release(viafbinfo1);
1927 out_fb_release:
1928 	i2c_bus_free(viaparinfo->shared);
1929 	framebuffer_release(viafbinfo);
1930 	return rc;
1931 }
1932 
1933 void via_fb_pci_remove(struct pci_dev *pdev)
1934 {
1935 	DEBUG_MSG(KERN_INFO "via_pci_remove!\n");
1936 	fb_dealloc_cmap(&viafbinfo->cmap);
1937 	unregister_framebuffer(viafbinfo);
1938 	if (viafb_dual_fb)
1939 		unregister_framebuffer(viafbinfo1);
1940 	viafb_remove_proc(viaparinfo->shared);
1941 	i2c_bus_free(viaparinfo->shared);
1942 	framebuffer_release(viafbinfo);
1943 	if (viafb_dual_fb)
1944 		framebuffer_release(viafbinfo1);
1945 }
1946 
1947 #ifndef MODULE
1948 static int __init viafb_setup(void)
1949 {
1950 	char *this_opt;
1951 	char *options;
1952 
1953 	DEBUG_MSG(KERN_INFO "viafb_setup!\n");
1954 
1955 	if (fb_get_options("viafb", &options))
1956 		return -ENODEV;
1957 
1958 	if (!options || !*options)
1959 		return 0;
1960 
1961 	while ((this_opt = strsep(&options, ",")) != NULL) {
1962 		if (!*this_opt)
1963 			continue;
1964 
1965 		if (!strncmp(this_opt, "viafb_mode1=", 12)) {
1966 			viafb_mode1 = kstrdup(this_opt + 12, GFP_KERNEL);
1967 		} else if (!strncmp(this_opt, "viafb_mode=", 11)) {
1968 			viafb_mode = kstrdup(this_opt + 11, GFP_KERNEL);
1969 		} else if (!strncmp(this_opt, "viafb_bpp1=", 11)) {
1970 			if (kstrtouint(this_opt + 11, 0, &viafb_bpp1) < 0)
1971 				return -EINVAL;
1972 		} else if (!strncmp(this_opt, "viafb_bpp=", 10)) {
1973 			if (kstrtouint(this_opt + 10, 0, &viafb_bpp) < 0)
1974 				return -EINVAL;
1975 		} else if (!strncmp(this_opt, "viafb_refresh1=", 15)) {
1976 			if (kstrtoint(this_opt + 15, 0, &viafb_refresh1) < 0)
1977 				return -EINVAL;
1978 		} else if (!strncmp(this_opt, "viafb_refresh=", 14)) {
1979 			if (kstrtoint(this_opt + 14, 0, &viafb_refresh) < 0)
1980 				return -EINVAL;
1981 		} else if (!strncmp(this_opt, "viafb_lcd_dsp_method=", 21)) {
1982 			if (kstrtoint(this_opt + 21, 0,
1983 				      &viafb_lcd_dsp_method) < 0)
1984 				return -EINVAL;
1985 		} else if (!strncmp(this_opt, "viafb_lcd_panel_id=", 19)) {
1986 			if (kstrtoint(this_opt + 19, 0,
1987 				      &viafb_lcd_panel_id) < 0)
1988 				return -EINVAL;
1989 		} else if (!strncmp(this_opt, "viafb_accel=", 12)) {
1990 			if (kstrtoint(this_opt + 12, 0, &viafb_accel) < 0)
1991 				return -EINVAL;
1992 		} else if (!strncmp(this_opt, "viafb_SAMM_ON=", 14)) {
1993 			if (kstrtoint(this_opt + 14, 0, &viafb_SAMM_ON) < 0)
1994 				return -EINVAL;
1995 		} else if (!strncmp(this_opt, "viafb_active_dev=", 17)) {
1996 			viafb_active_dev = kstrdup(this_opt + 17, GFP_KERNEL);
1997 		} else if (!strncmp(this_opt,
1998 			"viafb_display_hardware_layout=", 30)) {
1999 			if (kstrtoint(this_opt + 30, 0,
2000 				      &viafb_display_hardware_layout) < 0)
2001 				return -EINVAL;
2002 		} else if (!strncmp(this_opt, "viafb_second_size=", 18)) {
2003 			if (kstrtoint(this_opt + 18, 0, &viafb_second_size) < 0)
2004 				return -EINVAL;
2005 		} else if (!strncmp(this_opt,
2006 			"viafb_platform_epia_dvi=", 24)) {
2007 			if (kstrtoint(this_opt + 24, 0,
2008 				      &viafb_platform_epia_dvi) < 0)
2009 				return -EINVAL;
2010 		} else if (!strncmp(this_opt,
2011 			"viafb_device_lcd_dualedge=", 26)) {
2012 			if (kstrtoint(this_opt + 26, 0,
2013 				      &viafb_device_lcd_dualedge) < 0)
2014 				return -EINVAL;
2015 		} else if (!strncmp(this_opt, "viafb_bus_width=", 16)) {
2016 			if (kstrtoint(this_opt + 16, 0, &viafb_bus_width) < 0)
2017 				return -EINVAL;
2018 		} else if (!strncmp(this_opt, "viafb_lcd_mode=", 15)) {
2019 			if (kstrtoint(this_opt + 15, 0, &viafb_lcd_mode) < 0)
2020 				return -EINVAL;
2021 		} else if (!strncmp(this_opt, "viafb_lcd_port=", 15)) {
2022 			viafb_lcd_port = kstrdup(this_opt + 15, GFP_KERNEL);
2023 		} else if (!strncmp(this_opt, "viafb_dvi_port=", 15)) {
2024 			viafb_dvi_port = kstrdup(this_opt + 15, GFP_KERNEL);
2025 		}
2026 	}
2027 	return 0;
2028 }
2029 #endif
2030 
2031 /*
2032  * These are called out of via-core for now.
2033  */
2034 int __init viafb_init(void)
2035 {
2036 	u32 dummy_x, dummy_y;
2037 	int r = 0;
2038 
2039 	if (machine_is_olpc())
2040 		/* Apply XO-1.5-specific configuration. */
2041 		viafb_lcd_panel_id = 23;
2042 
2043 #ifndef MODULE
2044 	r = viafb_setup();
2045 	if (r < 0)
2046 		return r;
2047 #endif
2048 	if (parse_mode(viafb_mode, 0, &dummy_x, &dummy_y)
2049 		|| !viafb_get_best_mode(dummy_x, dummy_y, viafb_refresh)
2050 		|| parse_mode(viafb_mode1, 0, &dummy_x, &dummy_y)
2051 		|| !viafb_get_best_mode(dummy_x, dummy_y, viafb_refresh1)
2052 		|| viafb_bpp < 0 || viafb_bpp > 32
2053 		|| viafb_bpp1 < 0 || viafb_bpp1 > 32
2054 		|| parse_active_dev())
2055 		return -EINVAL;
2056 
2057 	printk(KERN_INFO
2058        "VIA Graphics Integration Chipset framebuffer %d.%d initializing\n",
2059 	       VERSION_MAJOR, VERSION_MINOR);
2060 	return r;
2061 }
2062 
2063 void __exit viafb_exit(void)
2064 {
2065 	DEBUG_MSG(KERN_INFO "viafb_exit!\n");
2066 }
2067 
2068 static struct fb_ops viafb_ops = {
2069 	.owner = THIS_MODULE,
2070 	.fb_open = viafb_open,
2071 	.fb_release = viafb_release,
2072 	.fb_check_var = viafb_check_var,
2073 	.fb_set_par = viafb_set_par,
2074 	.fb_setcolreg = viafb_setcolreg,
2075 	.fb_pan_display = viafb_pan_display,
2076 	.fb_blank = viafb_blank,
2077 	.fb_fillrect = viafb_fillrect,
2078 	.fb_copyarea = viafb_copyarea,
2079 	.fb_imageblit = viafb_imageblit,
2080 	.fb_cursor = viafb_cursor,
2081 	.fb_ioctl = viafb_ioctl,
2082 	.fb_sync = viafb_sync,
2083 };
2084 
2085 
2086 #ifdef MODULE
2087 module_param(viafb_mode, charp, S_IRUSR);
2088 MODULE_PARM_DESC(viafb_mode, "Set resolution (default=640x480)");
2089 
2090 module_param(viafb_mode1, charp, S_IRUSR);
2091 MODULE_PARM_DESC(viafb_mode1, "Set resolution (default=640x480)");
2092 
2093 module_param(viafb_bpp, int, S_IRUSR);
2094 MODULE_PARM_DESC(viafb_bpp, "Set color depth (default=32bpp)");
2095 
2096 module_param(viafb_bpp1, int, S_IRUSR);
2097 MODULE_PARM_DESC(viafb_bpp1, "Set color depth (default=32bpp)");
2098 
2099 module_param(viafb_refresh, int, S_IRUSR);
2100 MODULE_PARM_DESC(viafb_refresh,
2101 	"Set CRT viafb_refresh rate (default = 60)");
2102 
2103 module_param(viafb_refresh1, int, S_IRUSR);
2104 MODULE_PARM_DESC(viafb_refresh1,
2105 	"Set CRT refresh rate (default = 60)");
2106 
2107 module_param(viafb_lcd_panel_id, int, S_IRUSR);
2108 MODULE_PARM_DESC(viafb_lcd_panel_id,
2109 	"Set Flat Panel type(Default=1024x768)");
2110 
2111 module_param(viafb_lcd_dsp_method, int, S_IRUSR);
2112 MODULE_PARM_DESC(viafb_lcd_dsp_method,
2113 	"Set Flat Panel display scaling method.(Default=Expansion)");
2114 
2115 module_param(viafb_SAMM_ON, int, S_IRUSR);
2116 MODULE_PARM_DESC(viafb_SAMM_ON,
2117 	"Turn on/off flag of SAMM(Default=OFF)");
2118 
2119 module_param(viafb_accel, int, S_IRUSR);
2120 MODULE_PARM_DESC(viafb_accel,
2121 	"Set 2D Hardware Acceleration: 0 = OFF, 1 = ON (default)");
2122 
2123 module_param(viafb_active_dev, charp, S_IRUSR);
2124 MODULE_PARM_DESC(viafb_active_dev, "Specify active devices.");
2125 
2126 module_param(viafb_display_hardware_layout, int, S_IRUSR);
2127 MODULE_PARM_DESC(viafb_display_hardware_layout,
2128 	"Display Hardware Layout (LCD Only, DVI Only...,etc)");
2129 
2130 module_param(viafb_second_size, int, S_IRUSR);
2131 MODULE_PARM_DESC(viafb_second_size,
2132 	"Set secondary device memory size");
2133 
2134 module_param(viafb_dual_fb, int, S_IRUSR);
2135 MODULE_PARM_DESC(viafb_dual_fb,
2136 	"Turn on/off flag of dual framebuffer devices.(Default = OFF)");
2137 
2138 module_param(viafb_platform_epia_dvi, int, S_IRUSR);
2139 MODULE_PARM_DESC(viafb_platform_epia_dvi,
2140 	"Turn on/off flag of DVI devices on EPIA board.(Default = OFF)");
2141 
2142 module_param(viafb_device_lcd_dualedge, int, S_IRUSR);
2143 MODULE_PARM_DESC(viafb_device_lcd_dualedge,
2144 	"Turn on/off flag of dual edge panel.(Default = OFF)");
2145 
2146 module_param(viafb_bus_width, int, S_IRUSR);
2147 MODULE_PARM_DESC(viafb_bus_width,
2148 	"Set bus width of panel.(Default = 12)");
2149 
2150 module_param(viafb_lcd_mode, int, S_IRUSR);
2151 MODULE_PARM_DESC(viafb_lcd_mode,
2152 	"Set Flat Panel mode(Default=OPENLDI)");
2153 
2154 module_param(viafb_lcd_port, charp, S_IRUSR);
2155 MODULE_PARM_DESC(viafb_lcd_port, "Specify LCD output port.");
2156 
2157 module_param(viafb_dvi_port, charp, S_IRUSR);
2158 MODULE_PARM_DESC(viafb_dvi_port, "Specify DVI output port.");
2159 
2160 MODULE_LICENSE("GPL");
2161 #endif
2162