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