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