Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Copyright 1998-2009 VIA Technologies, Inc. All Rights Reserved.
0004  * Copyright 2001-2008 S3 Graphics, Inc. All Rights Reserved.
0005 
0006  */
0007 
0008 #include <linux/compiler.h>
0009 #include <linux/module.h>
0010 #include <linux/seq_file.h>
0011 #include <linux/slab.h>
0012 #include <linux/stat.h>
0013 #include <linux/via-core.h>
0014 #include <linux/via_i2c.h>
0015 
0016 #define _MASTER_FILE
0017 #include "global.h"
0018 
0019 static char *viafb_name = "Via";
0020 static u32 pseudo_pal[17];
0021 
0022 /* video mode */
0023 static char *viafb_mode;
0024 static char *viafb_mode1;
0025 static int viafb_bpp = 32;
0026 static int viafb_bpp1 = 32;
0027 
0028 static unsigned int viafb_second_offset;
0029 static int viafb_second_size;
0030 
0031 static int viafb_accel = 1;
0032 
0033 /* Added for specifying active devices.*/
0034 static char *viafb_active_dev;
0035 
0036 /*Added for specify lcd output port*/
0037 static char *viafb_lcd_port = "";
0038 static char *viafb_dvi_port = "";
0039 
0040 static void retrieve_device_setting(struct viafb_ioctl_setting
0041     *setting_info);
0042 static int viafb_pan_display(struct fb_var_screeninfo *var,
0043     struct fb_info *info);
0044 
0045 static struct fb_ops viafb_ops;
0046 
0047 /* supported output devices on each IGP
0048  * only CX700, VX800, VX855, VX900 were documented
0049  * VIA_CRT should be everywhere
0050  * VIA_6C can be onle pre-CX700 (probably only on CLE266) as 6C is used for PLL
0051  * source selection on CX700 and later
0052  * K400 seems to support VIA_96, VIA_DVP1, VIA_LVDS{1,2} as in viamode.c
0053  */
0054 static const u32 supported_odev_map[] = {
0055     [UNICHROME_CLE266]  = VIA_CRT | VIA_LDVP0 | VIA_LDVP1,
0056     [UNICHROME_K400]    = VIA_CRT | VIA_DVP0 | VIA_DVP1 | VIA_LVDS1
0057                 | VIA_LVDS2,
0058     [UNICHROME_K800]    = VIA_CRT | VIA_DVP0 | VIA_DVP1 | VIA_LVDS1
0059                 | VIA_LVDS2,
0060     [UNICHROME_PM800]   = VIA_CRT | VIA_DVP0 | VIA_DVP1 | VIA_LVDS1
0061                 | VIA_LVDS2,
0062     [UNICHROME_CN700]   = VIA_CRT | VIA_DVP0 | VIA_DVP1 | VIA_LVDS1
0063                 | VIA_LVDS2,
0064     [UNICHROME_CX700]   = VIA_CRT | VIA_DVP1 | VIA_LVDS1 | VIA_LVDS2,
0065     [UNICHROME_CN750]   = VIA_CRT | VIA_DVP1 | VIA_LVDS1 | VIA_LVDS2,
0066     [UNICHROME_K8M890]  = VIA_CRT | VIA_DVP1 | VIA_LVDS1 | VIA_LVDS2,
0067     [UNICHROME_P4M890]  = VIA_CRT | VIA_DVP1 | VIA_LVDS1 | VIA_LVDS2,
0068     [UNICHROME_P4M900]  = VIA_CRT | VIA_DVP1 | VIA_LVDS1 | VIA_LVDS2,
0069     [UNICHROME_VX800]   = VIA_CRT | VIA_DVP1 | VIA_LVDS1 | VIA_LVDS2,
0070     [UNICHROME_VX855]   = VIA_CRT | VIA_DVP1 | VIA_LVDS1 | VIA_LVDS2,
0071     [UNICHROME_VX900]   = VIA_CRT | VIA_DVP1 | VIA_LVDS1 | VIA_LVDS2,
0072 };
0073 
0074 static void viafb_fill_var_color_info(struct fb_var_screeninfo *var, u8 depth)
0075 {
0076     var->grayscale = 0;
0077     var->red.msb_right = 0;
0078     var->green.msb_right = 0;
0079     var->blue.msb_right = 0;
0080     var->transp.offset = 0;
0081     var->transp.length = 0;
0082     var->transp.msb_right = 0;
0083     var->nonstd = 0;
0084     switch (depth) {
0085     case 8:
0086         var->bits_per_pixel = 8;
0087         var->red.offset = 0;
0088         var->green.offset = 0;
0089         var->blue.offset = 0;
0090         var->red.length = 8;
0091         var->green.length = 8;
0092         var->blue.length = 8;
0093         break;
0094     case 15:
0095         var->bits_per_pixel = 16;
0096         var->red.offset = 10;
0097         var->green.offset = 5;
0098         var->blue.offset = 0;
0099         var->red.length = 5;
0100         var->green.length = 5;
0101         var->blue.length = 5;
0102         break;
0103     case 16:
0104         var->bits_per_pixel = 16;
0105         var->red.offset = 11;
0106         var->green.offset = 5;
0107         var->blue.offset = 0;
0108         var->red.length = 5;
0109         var->green.length = 6;
0110         var->blue.length = 5;
0111         break;
0112     case 24:
0113         var->bits_per_pixel = 32;
0114         var->red.offset = 16;
0115         var->green.offset = 8;
0116         var->blue.offset = 0;
0117         var->red.length = 8;
0118         var->green.length = 8;
0119         var->blue.length = 8;
0120         break;
0121     case 30:
0122         var->bits_per_pixel = 32;
0123         var->red.offset = 20;
0124         var->green.offset = 10;
0125         var->blue.offset = 0;
0126         var->red.length = 10;
0127         var->green.length = 10;
0128         var->blue.length = 10;
0129         break;
0130     }
0131 }
0132 
0133 static void viafb_update_fix(struct fb_info *info)
0134 {
0135     u32 bpp = info->var.bits_per_pixel;
0136 
0137     info->fix.visual =
0138         bpp == 8 ? FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
0139     info->fix.line_length = ALIGN(info->var.xres_virtual * bpp / 8,
0140         VIA_PITCH_SIZE);
0141 }
0142 
0143 static void viafb_setup_fixinfo(struct fb_fix_screeninfo *fix,
0144     struct viafb_par *viaparinfo)
0145 {
0146     memset(fix, 0, sizeof(struct fb_fix_screeninfo));
0147     strcpy(fix->id, viafb_name);
0148 
0149     fix->smem_start = viaparinfo->fbmem;
0150     fix->smem_len = viaparinfo->fbmem_free;
0151 
0152     fix->type = FB_TYPE_PACKED_PIXELS;
0153     fix->type_aux = 0;
0154     fix->visual = FB_VISUAL_TRUECOLOR;
0155 
0156     fix->xpanstep = fix->ywrapstep = 0;
0157     fix->ypanstep = 1;
0158 
0159     /* Just tell the accel name */
0160     viafbinfo->fix.accel = FB_ACCEL_VIA_UNICHROME;
0161 }
0162 static int viafb_open(struct fb_info *info, int user)
0163 {
0164     DEBUG_MSG(KERN_INFO "viafb_open!\n");
0165     return 0;
0166 }
0167 
0168 static int viafb_release(struct fb_info *info, int user)
0169 {
0170     DEBUG_MSG(KERN_INFO "viafb_release!\n");
0171     return 0;
0172 }
0173 
0174 static inline int get_var_refresh(struct fb_var_screeninfo *var)
0175 {
0176     u32 htotal, vtotal;
0177 
0178     htotal = var->left_margin + var->xres + var->right_margin
0179         + var->hsync_len;
0180     vtotal = var->upper_margin + var->yres + var->lower_margin
0181         + var->vsync_len;
0182     return PICOS2KHZ(var->pixclock) * 1000 / (htotal * vtotal);
0183 }
0184 
0185 static int viafb_check_var(struct fb_var_screeninfo *var,
0186     struct fb_info *info)
0187 {
0188     int depth, refresh;
0189     struct viafb_par *ppar = info->par;
0190     u32 line;
0191 
0192     DEBUG_MSG(KERN_INFO "viafb_check_var!\n");
0193     /* Sanity check */
0194     /* HW neither support interlacte nor double-scaned mode */
0195     if (var->vmode & FB_VMODE_INTERLACED || var->vmode & FB_VMODE_DOUBLE)
0196         return -EINVAL;
0197 
0198     /* the refresh rate is not important here, as we only want to know
0199      * whether the resolution exists
0200      */
0201     if (!viafb_get_best_mode(var->xres, var->yres, 60)) {
0202         DEBUG_MSG(KERN_INFO
0203               "viafb: Mode %dx%dx%d not supported!!\n",
0204               var->xres, var->yres, var->bits_per_pixel);
0205         return -EINVAL;
0206     }
0207 
0208     depth = fb_get_color_depth(var, &info->fix);
0209     if (!depth)
0210         depth = var->bits_per_pixel;
0211 
0212     if (depth < 0 || depth > 32)
0213         return -EINVAL;
0214     else if (!depth)
0215         depth = 24;
0216     else if (depth == 15 && viafb_dual_fb && ppar->iga_path == IGA1)
0217         depth = 15;
0218     else if (depth == 30)
0219         depth = 30;
0220     else if (depth <= 8)
0221         depth = 8;
0222     else if (depth <= 16)
0223         depth = 16;
0224     else
0225         depth = 24;
0226 
0227     viafb_fill_var_color_info(var, depth);
0228     if (var->xres_virtual < var->xres)
0229         var->xres_virtual = var->xres;
0230 
0231     line = ALIGN(var->xres_virtual * var->bits_per_pixel / 8,
0232         VIA_PITCH_SIZE);
0233     if (line > VIA_PITCH_MAX || line * var->yres_virtual > ppar->memsize)
0234         return -EINVAL;
0235 
0236     /* Based on var passed in to calculate the refresh,
0237      * because our driver use some modes special.
0238      */
0239     refresh = viafb_get_refresh(var->xres, var->yres,
0240         get_var_refresh(var));
0241 
0242     /* Adjust var according to our driver's own table */
0243     viafb_fill_var_timing_info(var,
0244         viafb_get_best_mode(var->xres, var->yres, refresh));
0245     if (var->accel_flags & FB_ACCELF_TEXT &&
0246         !ppar->shared->vdev->engine_mmio)
0247         var->accel_flags = 0;
0248 
0249     return 0;
0250 }
0251 
0252 static int viafb_set_par(struct fb_info *info)
0253 {
0254     struct viafb_par *viapar = info->par;
0255     int refresh;
0256     DEBUG_MSG(KERN_INFO "viafb_set_par!\n");
0257 
0258     viafb_update_fix(info);
0259     viapar->depth = fb_get_color_depth(&info->var, &info->fix);
0260     viafb_update_device_setting(viafbinfo->var.xres, viafbinfo->var.yres,
0261         viafbinfo->var.bits_per_pixel, 0);
0262 
0263     if (viafb_dual_fb) {
0264         viafb_update_device_setting(viafbinfo1->var.xres,
0265             viafbinfo1->var.yres, viafbinfo1->var.bits_per_pixel,
0266             1);
0267     } else if (viafb_SAMM_ON == 1) {
0268         DEBUG_MSG(KERN_INFO
0269         "viafb_second_xres = %d, viafb_second_yres = %d, bpp = %d\n",
0270               viafb_second_xres, viafb_second_yres, viafb_bpp1);
0271 
0272         viafb_update_device_setting(viafb_second_xres,
0273             viafb_second_yres, viafb_bpp1, 1);
0274     }
0275 
0276     refresh = get_var_refresh(&info->var);
0277     if (viafb_dual_fb && viapar->iga_path == IGA2) {
0278         viafb_bpp1 = info->var.bits_per_pixel;
0279         viafb_refresh1 = refresh;
0280     } else {
0281         viafb_bpp = info->var.bits_per_pixel;
0282         viafb_refresh = refresh;
0283     }
0284 
0285     if (info->var.accel_flags & FB_ACCELF_TEXT)
0286         info->flags &= ~FBINFO_HWACCEL_DISABLED;
0287     else
0288         info->flags |= FBINFO_HWACCEL_DISABLED;
0289     viafb_setmode();
0290     viafb_pan_display(&info->var, info);
0291 
0292     return 0;
0293 }
0294 
0295 /* Set one color register */
0296 static int viafb_setcolreg(unsigned regno, unsigned red, unsigned green,
0297 unsigned blue, unsigned transp, struct fb_info *info)
0298 {
0299     struct viafb_par *viapar = info->par;
0300     u32 r, g, b;
0301 
0302     if (info->fix.visual == FB_VISUAL_PSEUDOCOLOR) {
0303         if (regno > 255)
0304             return -EINVAL;
0305 
0306         if (!viafb_dual_fb || viapar->iga_path == IGA1)
0307             viafb_set_primary_color_register(regno, red >> 8,
0308                 green >> 8, blue >> 8);
0309 
0310         if (!viafb_dual_fb || viapar->iga_path == IGA2)
0311             viafb_set_secondary_color_register(regno, red >> 8,
0312                 green >> 8, blue >> 8);
0313     } else {
0314         if (regno > 15)
0315             return -EINVAL;
0316 
0317         r = (red >> (16 - info->var.red.length))
0318             << info->var.red.offset;
0319         b = (blue >> (16 - info->var.blue.length))
0320             << info->var.blue.offset;
0321         g = (green >> (16 - info->var.green.length))
0322             << info->var.green.offset;
0323         ((u32 *) info->pseudo_palette)[regno] = r | g | b;
0324     }
0325 
0326     return 0;
0327 }
0328 
0329 static int viafb_pan_display(struct fb_var_screeninfo *var,
0330     struct fb_info *info)
0331 {
0332     struct viafb_par *viapar = info->par;
0333     u32 vram_addr = viapar->vram_addr
0334         + var->yoffset * info->fix.line_length
0335         + var->xoffset * info->var.bits_per_pixel / 8;
0336 
0337     DEBUG_MSG(KERN_DEBUG "viafb_pan_display, address = %d\n", vram_addr);
0338     if (!viafb_dual_fb) {
0339         via_set_primary_address(vram_addr);
0340         via_set_secondary_address(vram_addr);
0341     } else if (viapar->iga_path == IGA1)
0342         via_set_primary_address(vram_addr);
0343     else
0344         via_set_secondary_address(vram_addr);
0345 
0346     return 0;
0347 }
0348 
0349 static int viafb_blank(int blank_mode, struct fb_info *info)
0350 {
0351     DEBUG_MSG(KERN_INFO "viafb_blank!\n");
0352     /* clear DPMS setting */
0353 
0354     switch (blank_mode) {
0355     case FB_BLANK_UNBLANK:
0356         /* Screen: On, HSync: On, VSync: On */
0357         /* control CRT monitor power management */
0358         via_set_state(VIA_CRT, VIA_STATE_ON);
0359         break;
0360     case FB_BLANK_HSYNC_SUSPEND:
0361         /* Screen: Off, HSync: Off, VSync: On */
0362         /* control CRT monitor power management */
0363         via_set_state(VIA_CRT, VIA_STATE_STANDBY);
0364         break;
0365     case FB_BLANK_VSYNC_SUSPEND:
0366         /* Screen: Off, HSync: On, VSync: Off */
0367         /* control CRT monitor power management */
0368         via_set_state(VIA_CRT, VIA_STATE_SUSPEND);
0369         break;
0370     case FB_BLANK_POWERDOWN:
0371         /* Screen: Off, HSync: Off, VSync: Off */
0372         /* control CRT monitor power management */
0373         via_set_state(VIA_CRT, VIA_STATE_OFF);
0374         break;
0375     }
0376 
0377     return 0;
0378 }
0379 
0380 static int viafb_ioctl(struct fb_info *info, u_int cmd, u_long arg)
0381 {
0382     union {
0383         struct viafb_ioctl_mode viamode;
0384         struct viafb_ioctl_samm viasamm;
0385         struct viafb_driver_version driver_version;
0386         struct fb_var_screeninfo sec_var;
0387         struct _panel_size_pos_info panel_pos_size_para;
0388         struct viafb_ioctl_setting viafb_setting;
0389         struct device_t active_dev;
0390     } u;
0391     u32 state_info = 0;
0392     u32 *viafb_gamma_table;
0393     char driver_name[] = "viafb";
0394 
0395     u32 __user *argp = (u32 __user *) arg;
0396     u32 gpu32;
0397 
0398     DEBUG_MSG(KERN_INFO "viafb_ioctl: 0x%X !!\n", cmd);
0399     printk(KERN_WARNING "viafb_ioctl: Please avoid this interface as it is unstable and might change or vanish at any time!\n");
0400     memset(&u, 0, sizeof(u));
0401 
0402     switch (cmd) {
0403     case VIAFB_GET_CHIP_INFO:
0404         if (copy_to_user(argp, viaparinfo->chip_info,
0405                 sizeof(struct chip_information)))
0406             return -EFAULT;
0407         break;
0408     case VIAFB_GET_INFO_SIZE:
0409         return put_user((u32)sizeof(struct viafb_ioctl_info), argp);
0410     case VIAFB_GET_INFO:
0411         return viafb_ioctl_get_viafb_info(arg);
0412     case VIAFB_HOTPLUG:
0413         return put_user(viafb_ioctl_hotplug(info->var.xres,
0414                           info->var.yres,
0415                           info->var.bits_per_pixel), argp);
0416     case VIAFB_SET_HOTPLUG_FLAG:
0417         if (copy_from_user(&gpu32, argp, sizeof(gpu32)))
0418             return -EFAULT;
0419         viafb_hotplug = (gpu32) ? 1 : 0;
0420         break;
0421     case VIAFB_GET_RESOLUTION:
0422         u.viamode.xres = (u32) viafb_hotplug_Xres;
0423         u.viamode.yres = (u32) viafb_hotplug_Yres;
0424         u.viamode.refresh = (u32) viafb_hotplug_refresh;
0425         u.viamode.bpp = (u32) viafb_hotplug_bpp;
0426         if (viafb_SAMM_ON == 1) {
0427             u.viamode.xres_sec = viafb_second_xres;
0428             u.viamode.yres_sec = viafb_second_yres;
0429             u.viamode.virtual_xres_sec = viafb_dual_fb ? viafbinfo1->var.xres_virtual : viafbinfo->var.xres_virtual;
0430             u.viamode.virtual_yres_sec = viafb_dual_fb ? viafbinfo1->var.yres_virtual : viafbinfo->var.yres_virtual;
0431             u.viamode.refresh_sec = viafb_refresh1;
0432             u.viamode.bpp_sec = viafb_bpp1;
0433         } else {
0434             u.viamode.xres_sec = 0;
0435             u.viamode.yres_sec = 0;
0436             u.viamode.virtual_xres_sec = 0;
0437             u.viamode.virtual_yres_sec = 0;
0438             u.viamode.refresh_sec = 0;
0439             u.viamode.bpp_sec = 0;
0440         }
0441         if (copy_to_user(argp, &u.viamode, sizeof(u.viamode)))
0442             return -EFAULT;
0443         break;
0444     case VIAFB_GET_SAMM_INFO:
0445         u.viasamm.samm_status = viafb_SAMM_ON;
0446 
0447         if (viafb_SAMM_ON == 1) {
0448             if (viafb_dual_fb) {
0449                 u.viasamm.size_prim = viaparinfo->fbmem_free;
0450                 u.viasamm.size_sec = viaparinfo1->fbmem_free;
0451             } else {
0452                 if (viafb_second_size) {
0453                     u.viasamm.size_prim =
0454                         viaparinfo->fbmem_free -
0455                         viafb_second_size * 1024 * 1024;
0456                     u.viasamm.size_sec =
0457                         viafb_second_size * 1024 * 1024;
0458                 } else {
0459                     u.viasamm.size_prim =
0460                         viaparinfo->fbmem_free >> 1;
0461                     u.viasamm.size_sec =
0462                         (viaparinfo->fbmem_free >> 1);
0463                 }
0464             }
0465             u.viasamm.mem_base = viaparinfo->fbmem;
0466             u.viasamm.offset_sec = viafb_second_offset;
0467         } else {
0468             u.viasamm.size_prim =
0469                 viaparinfo->memsize - viaparinfo->fbmem_used;
0470             u.viasamm.size_sec = 0;
0471             u.viasamm.mem_base = viaparinfo->fbmem;
0472             u.viasamm.offset_sec = 0;
0473         }
0474 
0475         if (copy_to_user(argp, &u.viasamm, sizeof(u.viasamm)))
0476             return -EFAULT;
0477 
0478         break;
0479     case VIAFB_TURN_ON_OUTPUT_DEVICE:
0480         if (copy_from_user(&gpu32, argp, sizeof(gpu32)))
0481             return -EFAULT;
0482         if (gpu32 & CRT_Device)
0483             via_set_state(VIA_CRT, VIA_STATE_ON);
0484         if (gpu32 & DVI_Device)
0485             viafb_dvi_enable();
0486         if (gpu32 & LCD_Device)
0487             viafb_lcd_enable();
0488         break;
0489     case VIAFB_TURN_OFF_OUTPUT_DEVICE:
0490         if (copy_from_user(&gpu32, argp, sizeof(gpu32)))
0491             return -EFAULT;
0492         if (gpu32 & CRT_Device)
0493             via_set_state(VIA_CRT, VIA_STATE_OFF);
0494         if (gpu32 & DVI_Device)
0495             viafb_dvi_disable();
0496         if (gpu32 & LCD_Device)
0497             viafb_lcd_disable();
0498         break;
0499     case VIAFB_GET_DEVICE:
0500         u.active_dev.crt = viafb_CRT_ON;
0501         u.active_dev.dvi = viafb_DVI_ON;
0502         u.active_dev.lcd = viafb_LCD_ON;
0503         u.active_dev.samm = viafb_SAMM_ON;
0504         u.active_dev.primary_dev = viafb_primary_dev;
0505 
0506         u.active_dev.lcd_dsp_cent = viafb_lcd_dsp_method;
0507         u.active_dev.lcd_panel_id = viafb_lcd_panel_id;
0508         u.active_dev.lcd_mode = viafb_lcd_mode;
0509 
0510         u.active_dev.xres = viafb_hotplug_Xres;
0511         u.active_dev.yres = viafb_hotplug_Yres;
0512 
0513         u.active_dev.xres1 = viafb_second_xres;
0514         u.active_dev.yres1 = viafb_second_yres;
0515 
0516         u.active_dev.bpp = viafb_bpp;
0517         u.active_dev.bpp1 = viafb_bpp1;
0518         u.active_dev.refresh = viafb_refresh;
0519         u.active_dev.refresh1 = viafb_refresh1;
0520 
0521         u.active_dev.epia_dvi = viafb_platform_epia_dvi;
0522         u.active_dev.lcd_dual_edge = viafb_device_lcd_dualedge;
0523         u.active_dev.bus_width = viafb_bus_width;
0524 
0525         if (copy_to_user(argp, &u.active_dev, sizeof(u.active_dev)))
0526             return -EFAULT;
0527         break;
0528 
0529     case VIAFB_GET_DRIVER_VERSION:
0530         u.driver_version.iMajorNum = VERSION_MAJOR;
0531         u.driver_version.iKernelNum = VERSION_KERNEL;
0532         u.driver_version.iOSNum = VERSION_OS;
0533         u.driver_version.iMinorNum = VERSION_MINOR;
0534 
0535         if (copy_to_user(argp, &u.driver_version,
0536             sizeof(u.driver_version)))
0537             return -EFAULT;
0538 
0539         break;
0540 
0541     case VIAFB_GET_DEVICE_INFO:
0542 
0543         retrieve_device_setting(&u.viafb_setting);
0544 
0545         if (copy_to_user(argp, &u.viafb_setting,
0546                  sizeof(u.viafb_setting)))
0547             return -EFAULT;
0548 
0549         break;
0550 
0551     case VIAFB_GET_DEVICE_SUPPORT:
0552         viafb_get_device_support_state(&state_info);
0553         if (put_user(state_info, argp))
0554             return -EFAULT;
0555         break;
0556 
0557     case VIAFB_GET_DEVICE_CONNECT:
0558         viafb_get_device_connect_state(&state_info);
0559         if (put_user(state_info, argp))
0560             return -EFAULT;
0561         break;
0562 
0563     case VIAFB_GET_PANEL_SUPPORT_EXPAND:
0564         state_info =
0565             viafb_lcd_get_support_expand_state(info->var.xres,
0566                          info->var.yres);
0567         if (put_user(state_info, argp))
0568             return -EFAULT;
0569         break;
0570 
0571     case VIAFB_GET_DRIVER_NAME:
0572         if (copy_to_user(argp, driver_name, sizeof(driver_name)))
0573             return -EFAULT;
0574         break;
0575 
0576     case VIAFB_SET_GAMMA_LUT:
0577         viafb_gamma_table = memdup_user(argp, 256 * sizeof(u32));
0578         if (IS_ERR(viafb_gamma_table))
0579             return PTR_ERR(viafb_gamma_table);
0580         viafb_set_gamma_table(viafb_bpp, viafb_gamma_table);
0581         kfree(viafb_gamma_table);
0582         break;
0583 
0584     case VIAFB_GET_GAMMA_LUT:
0585         viafb_gamma_table = kmalloc_array(256, sizeof(u32),
0586                           GFP_KERNEL);
0587         if (!viafb_gamma_table)
0588             return -ENOMEM;
0589         viafb_get_gamma_table(viafb_gamma_table);
0590         if (copy_to_user(argp, viafb_gamma_table,
0591             256 * sizeof(u32))) {
0592             kfree(viafb_gamma_table);
0593             return -EFAULT;
0594         }
0595         kfree(viafb_gamma_table);
0596         break;
0597 
0598     case VIAFB_GET_GAMMA_SUPPORT_STATE:
0599         viafb_get_gamma_support_state(viafb_bpp, &state_info);
0600         if (put_user(state_info, argp))
0601             return -EFAULT;
0602         break;
0603     case VIAFB_SYNC_SURFACE:
0604         DEBUG_MSG(KERN_INFO "lobo VIAFB_SYNC_SURFACE\n");
0605         break;
0606     case VIAFB_GET_DRIVER_CAPS:
0607         break;
0608 
0609     case VIAFB_GET_PANEL_MAX_SIZE:
0610         if (copy_from_user(&u.panel_pos_size_para, argp,
0611                    sizeof(u.panel_pos_size_para)))
0612             return -EFAULT;
0613         u.panel_pos_size_para.x = u.panel_pos_size_para.y = 0;
0614         if (copy_to_user(argp, &u.panel_pos_size_para,
0615              sizeof(u.panel_pos_size_para)))
0616             return -EFAULT;
0617         break;
0618     case VIAFB_GET_PANEL_MAX_POSITION:
0619         if (copy_from_user(&u.panel_pos_size_para, argp,
0620                    sizeof(u.panel_pos_size_para)))
0621             return -EFAULT;
0622         u.panel_pos_size_para.x = u.panel_pos_size_para.y = 0;
0623         if (copy_to_user(argp, &u.panel_pos_size_para,
0624                  sizeof(u.panel_pos_size_para)))
0625             return -EFAULT;
0626         break;
0627 
0628     case VIAFB_GET_PANEL_POSITION:
0629         if (copy_from_user(&u.panel_pos_size_para, argp,
0630                    sizeof(u.panel_pos_size_para)))
0631             return -EFAULT;
0632         u.panel_pos_size_para.x = u.panel_pos_size_para.y = 0;
0633         if (copy_to_user(argp, &u.panel_pos_size_para,
0634                  sizeof(u.panel_pos_size_para)))
0635             return -EFAULT;
0636         break;
0637     case VIAFB_GET_PANEL_SIZE:
0638         if (copy_from_user(&u.panel_pos_size_para, argp,
0639                    sizeof(u.panel_pos_size_para)))
0640             return -EFAULT;
0641         u.panel_pos_size_para.x = u.panel_pos_size_para.y = 0;
0642         if (copy_to_user(argp, &u.panel_pos_size_para,
0643                  sizeof(u.panel_pos_size_para)))
0644             return -EFAULT;
0645         break;
0646 
0647     case VIAFB_SET_PANEL_POSITION:
0648         if (copy_from_user(&u.panel_pos_size_para, argp,
0649                    sizeof(u.panel_pos_size_para)))
0650             return -EFAULT;
0651         break;
0652     case VIAFB_SET_PANEL_SIZE:
0653         if (copy_from_user(&u.panel_pos_size_para, argp,
0654                    sizeof(u.panel_pos_size_para)))
0655             return -EFAULT;
0656         break;
0657 
0658     default:
0659         return -EINVAL;
0660     }
0661 
0662     return 0;
0663 }
0664 
0665 static void viafb_fillrect(struct fb_info *info,
0666     const struct fb_fillrect *rect)
0667 {
0668     struct viafb_par *viapar = info->par;
0669     struct viafb_shared *shared = viapar->shared;
0670     u32 fg_color;
0671     u8 rop;
0672 
0673     if (info->flags & FBINFO_HWACCEL_DISABLED || !shared->hw_bitblt) {
0674         cfb_fillrect(info, rect);
0675         return;
0676     }
0677 
0678     if (!rect->width || !rect->height)
0679         return;
0680 
0681     if (info->fix.visual == FB_VISUAL_TRUECOLOR)
0682         fg_color = ((u32 *)info->pseudo_palette)[rect->color];
0683     else
0684         fg_color = rect->color;
0685 
0686     if (rect->rop == ROP_XOR)
0687         rop = 0x5A;
0688     else
0689         rop = 0xF0;
0690 
0691     DEBUG_MSG(KERN_DEBUG "viafb 2D engine: fillrect\n");
0692     if (shared->hw_bitblt(shared->vdev->engine_mmio, VIA_BITBLT_FILL,
0693         rect->width, rect->height, info->var.bits_per_pixel,
0694         viapar->vram_addr, info->fix.line_length, rect->dx, rect->dy,
0695         NULL, 0, 0, 0, 0, fg_color, 0, rop))
0696         cfb_fillrect(info, rect);
0697 }
0698 
0699 static void viafb_copyarea(struct fb_info *info,
0700     const struct fb_copyarea *area)
0701 {
0702     struct viafb_par *viapar = info->par;
0703     struct viafb_shared *shared = viapar->shared;
0704 
0705     if (info->flags & FBINFO_HWACCEL_DISABLED || !shared->hw_bitblt) {
0706         cfb_copyarea(info, area);
0707         return;
0708     }
0709 
0710     if (!area->width || !area->height)
0711         return;
0712 
0713     DEBUG_MSG(KERN_DEBUG "viafb 2D engine: copyarea\n");
0714     if (shared->hw_bitblt(shared->vdev->engine_mmio, VIA_BITBLT_COLOR,
0715         area->width, area->height, info->var.bits_per_pixel,
0716         viapar->vram_addr, info->fix.line_length, area->dx, area->dy,
0717         NULL, viapar->vram_addr, info->fix.line_length,
0718         area->sx, area->sy, 0, 0, 0))
0719         cfb_copyarea(info, area);
0720 }
0721 
0722 static void viafb_imageblit(struct fb_info *info,
0723     const struct fb_image *image)
0724 {
0725     struct viafb_par *viapar = info->par;
0726     struct viafb_shared *shared = viapar->shared;
0727     u32 fg_color = 0, bg_color = 0;
0728     u8 op;
0729 
0730     if (info->flags & FBINFO_HWACCEL_DISABLED || !shared->hw_bitblt ||
0731         (image->depth != 1 && image->depth != viapar->depth)) {
0732         cfb_imageblit(info, image);
0733         return;
0734     }
0735 
0736     if (image->depth == 1) {
0737         op = VIA_BITBLT_MONO;
0738         if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
0739             fg_color =
0740                 ((u32 *)info->pseudo_palette)[image->fg_color];
0741             bg_color =
0742                 ((u32 *)info->pseudo_palette)[image->bg_color];
0743         } else {
0744             fg_color = image->fg_color;
0745             bg_color = image->bg_color;
0746         }
0747     } else
0748         op = VIA_BITBLT_COLOR;
0749 
0750     DEBUG_MSG(KERN_DEBUG "viafb 2D engine: imageblit\n");
0751     if (shared->hw_bitblt(shared->vdev->engine_mmio, op,
0752         image->width, image->height, info->var.bits_per_pixel,
0753         viapar->vram_addr, info->fix.line_length, image->dx, image->dy,
0754         (u32 *)image->data, 0, 0, 0, 0, fg_color, bg_color, 0))
0755         cfb_imageblit(info, image);
0756 }
0757 
0758 static int viafb_cursor(struct fb_info *info, struct fb_cursor *cursor)
0759 {
0760     struct viafb_par *viapar = info->par;
0761     void __iomem *engine = viapar->shared->vdev->engine_mmio;
0762     u32 temp, xx, yy, bg_color = 0, fg_color = 0,
0763         chip_name = viapar->shared->chip_info.gfx_chip_name;
0764     int i, j = 0, cur_size = 64;
0765 
0766     if (info->flags & FBINFO_HWACCEL_DISABLED || info != viafbinfo)
0767         return -ENODEV;
0768 
0769     /* LCD ouput does not support hw cursors (at least on VN896) */
0770     if ((chip_name == UNICHROME_CLE266 && viapar->iga_path == IGA2) ||
0771         viafb_LCD_ON)
0772         return -ENODEV;
0773 
0774     viafb_show_hw_cursor(info, HW_Cursor_OFF);
0775 
0776     if (cursor->set & FB_CUR_SETHOT) {
0777         temp = (cursor->hot.x << 16) + cursor->hot.y;
0778         writel(temp, engine + VIA_REG_CURSOR_ORG);
0779     }
0780 
0781     if (cursor->set & FB_CUR_SETPOS) {
0782         yy = cursor->image.dy - info->var.yoffset;
0783         xx = cursor->image.dx - info->var.xoffset;
0784         temp = yy & 0xFFFF;
0785         temp |= (xx << 16);
0786         writel(temp, engine + VIA_REG_CURSOR_POS);
0787     }
0788 
0789     if (cursor->image.width <= 32 && cursor->image.height <= 32)
0790         cur_size = 32;
0791     else if (cursor->image.width <= 64 && cursor->image.height <= 64)
0792         cur_size = 64;
0793     else {
0794         printk(KERN_WARNING "viafb_cursor: The cursor is too large "
0795             "%dx%d", cursor->image.width, cursor->image.height);
0796         return -ENXIO;
0797     }
0798 
0799     if (cursor->set & FB_CUR_SETSIZE) {
0800         temp = readl(engine + VIA_REG_CURSOR_MODE);
0801         if (cur_size == 32)
0802             temp |= 0x2;
0803         else
0804             temp &= ~0x2;
0805 
0806         writel(temp, engine + VIA_REG_CURSOR_MODE);
0807     }
0808 
0809     if (cursor->set & FB_CUR_SETCMAP) {
0810         fg_color = cursor->image.fg_color;
0811         bg_color = cursor->image.bg_color;
0812         if (chip_name == UNICHROME_CX700 ||
0813             chip_name == UNICHROME_VX800 ||
0814             chip_name == UNICHROME_VX855 ||
0815             chip_name == UNICHROME_VX900) {
0816             fg_color =
0817                 ((info->cmap.red[fg_color] & 0xFFC0) << 14) |
0818                 ((info->cmap.green[fg_color] & 0xFFC0) << 4) |
0819                 ((info->cmap.blue[fg_color] & 0xFFC0) >> 6);
0820             bg_color =
0821                 ((info->cmap.red[bg_color] & 0xFFC0) << 14) |
0822                 ((info->cmap.green[bg_color] & 0xFFC0) << 4) |
0823                 ((info->cmap.blue[bg_color] & 0xFFC0) >> 6);
0824         } else {
0825             fg_color =
0826                 ((info->cmap.red[fg_color] & 0xFF00) << 8) |
0827                 (info->cmap.green[fg_color] & 0xFF00) |
0828                 ((info->cmap.blue[fg_color] & 0xFF00) >> 8);
0829             bg_color =
0830                 ((info->cmap.red[bg_color] & 0xFF00) << 8) |
0831                 (info->cmap.green[bg_color] & 0xFF00) |
0832                 ((info->cmap.blue[bg_color] & 0xFF00) >> 8);
0833         }
0834 
0835         writel(bg_color, engine + VIA_REG_CURSOR_BG);
0836         writel(fg_color, engine + VIA_REG_CURSOR_FG);
0837     }
0838 
0839     if (cursor->set & FB_CUR_SETSHAPE) {
0840         struct {
0841             u8 data[CURSOR_SIZE];
0842             u32 bak[CURSOR_SIZE / 4];
0843         } *cr_data = kzalloc(sizeof(*cr_data), GFP_ATOMIC);
0844         int size = ((cursor->image.width + 7) >> 3) *
0845             cursor->image.height;
0846 
0847         if (!cr_data)
0848             return -ENOMEM;
0849 
0850         if (cur_size == 32) {
0851             for (i = 0; i < (CURSOR_SIZE / 4); i++) {
0852                 cr_data->bak[i] = 0x0;
0853                 cr_data->bak[i + 1] = 0xFFFFFFFF;
0854                 i += 1;
0855             }
0856         } else {
0857             for (i = 0; i < (CURSOR_SIZE / 4); i++) {
0858                 cr_data->bak[i] = 0x0;
0859                 cr_data->bak[i + 1] = 0x0;
0860                 cr_data->bak[i + 2] = 0xFFFFFFFF;
0861                 cr_data->bak[i + 3] = 0xFFFFFFFF;
0862                 i += 3;
0863             }
0864         }
0865 
0866         switch (cursor->rop) {
0867         case ROP_XOR:
0868             for (i = 0; i < size; i++)
0869                 cr_data->data[i] = cursor->mask[i];
0870             break;
0871         case ROP_COPY:
0872 
0873             for (i = 0; i < size; i++)
0874                 cr_data->data[i] = cursor->mask[i];
0875             break;
0876         default:
0877             break;
0878         }
0879 
0880         if (cur_size == 32) {
0881             for (i = 0; i < size; i++) {
0882                 cr_data->bak[j] = (u32) cr_data->data[i];
0883                 cr_data->bak[j + 1] = ~cr_data->bak[j];
0884                 j += 2;
0885             }
0886         } else {
0887             for (i = 0; i < size; i++) {
0888                 cr_data->bak[j] = (u32) cr_data->data[i];
0889                 cr_data->bak[j + 1] = 0x0;
0890                 cr_data->bak[j + 2] = ~cr_data->bak[j];
0891                 cr_data->bak[j + 3] = ~cr_data->bak[j + 1];
0892                 j += 4;
0893             }
0894         }
0895 
0896         memcpy_toio(viafbinfo->screen_base + viapar->shared->
0897             cursor_vram_addr, cr_data->bak, CURSOR_SIZE);
0898         kfree(cr_data);
0899     }
0900 
0901     if (cursor->enable)
0902         viafb_show_hw_cursor(info, HW_Cursor_ON);
0903 
0904     return 0;
0905 }
0906 
0907 static int viafb_sync(struct fb_info *info)
0908 {
0909     if (!(info->flags & FBINFO_HWACCEL_DISABLED))
0910         viafb_wait_engine_idle(info);
0911     return 0;
0912 }
0913 
0914 static int get_primary_device(void)
0915 {
0916     int primary_device = 0;
0917     /* Rule: device on iga1 path are the primary device. */
0918     if (viafb_SAMM_ON) {
0919         if (viafb_CRT_ON) {
0920             if (viaparinfo->shared->iga1_devices & VIA_CRT) {
0921                 DEBUG_MSG(KERN_INFO "CRT IGA Path:%d\n", IGA1);
0922                 primary_device = CRT_Device;
0923             }
0924         }
0925         if (viafb_DVI_ON) {
0926             if (viaparinfo->tmds_setting_info->iga_path == IGA1) {
0927                 DEBUG_MSG(KERN_INFO "DVI IGA Path:%d\n",
0928                     viaparinfo->
0929                     tmds_setting_info->iga_path);
0930                 primary_device = DVI_Device;
0931             }
0932         }
0933         if (viafb_LCD_ON) {
0934             if (viaparinfo->lvds_setting_info->iga_path == IGA1) {
0935                 DEBUG_MSG(KERN_INFO "LCD IGA Path:%d\n",
0936                     viaparinfo->
0937                     lvds_setting_info->iga_path);
0938                 primary_device = LCD_Device;
0939             }
0940         }
0941         if (viafb_LCD2_ON) {
0942             if (viaparinfo->lvds_setting_info2->iga_path == IGA1) {
0943                 DEBUG_MSG(KERN_INFO "LCD2 IGA Path:%d\n",
0944                     viaparinfo->
0945                     lvds_setting_info2->iga_path);
0946                 primary_device = LCD2_Device;
0947             }
0948         }
0949     }
0950     return primary_device;
0951 }
0952 
0953 static void retrieve_device_setting(struct viafb_ioctl_setting
0954     *setting_info)
0955 {
0956 
0957     /* get device status */
0958     if (viafb_CRT_ON == 1)
0959         setting_info->device_status = CRT_Device;
0960     if (viafb_DVI_ON == 1)
0961         setting_info->device_status |= DVI_Device;
0962     if (viafb_LCD_ON == 1)
0963         setting_info->device_status |= LCD_Device;
0964     if (viafb_LCD2_ON == 1)
0965         setting_info->device_status |= LCD2_Device;
0966 
0967     setting_info->samm_status = viafb_SAMM_ON;
0968     setting_info->primary_device = get_primary_device();
0969 
0970     setting_info->first_dev_bpp = viafb_bpp;
0971     setting_info->second_dev_bpp = viafb_bpp1;
0972 
0973     setting_info->first_dev_refresh = viafb_refresh;
0974     setting_info->second_dev_refresh = viafb_refresh1;
0975 
0976     setting_info->first_dev_hor_res = viafb_hotplug_Xres;
0977     setting_info->first_dev_ver_res = viafb_hotplug_Yres;
0978     setting_info->second_dev_hor_res = viafb_second_xres;
0979     setting_info->second_dev_ver_res = viafb_second_yres;
0980 
0981     /* Get lcd attributes */
0982     setting_info->lcd_attributes.display_center = viafb_lcd_dsp_method;
0983     setting_info->lcd_attributes.panel_id = viafb_lcd_panel_id;
0984     setting_info->lcd_attributes.lcd_mode = viafb_lcd_mode;
0985 }
0986 
0987 static int __init parse_active_dev(void)
0988 {
0989     viafb_CRT_ON = STATE_OFF;
0990     viafb_DVI_ON = STATE_OFF;
0991     viafb_LCD_ON = STATE_OFF;
0992     viafb_LCD2_ON = STATE_OFF;
0993     /* 1. Modify the active status of devices. */
0994     /* 2. Keep the order of devices, so we can set corresponding
0995        IGA path to devices in SAMM case. */
0996     /*    Note: The previous of active_dev is primary device,
0997        and the following is secondary device. */
0998     if (!viafb_active_dev) {
0999         if (machine_is_olpc()) { /* LCD only */
1000             viafb_LCD_ON = STATE_ON;
1001             viafb_SAMM_ON = STATE_OFF;
1002         } else {
1003             viafb_CRT_ON = STATE_ON;
1004             viafb_SAMM_ON = STATE_OFF;
1005         }
1006     } else if (!strcmp(viafb_active_dev, "CRT+DVI")) {
1007         /* CRT+DVI */
1008         viafb_CRT_ON = STATE_ON;
1009         viafb_DVI_ON = STATE_ON;
1010         viafb_primary_dev = CRT_Device;
1011     } else if (!strcmp(viafb_active_dev, "DVI+CRT")) {
1012         /* DVI+CRT */
1013         viafb_CRT_ON = STATE_ON;
1014         viafb_DVI_ON = STATE_ON;
1015         viafb_primary_dev = DVI_Device;
1016     } else if (!strcmp(viafb_active_dev, "CRT+LCD")) {
1017         /* CRT+LCD */
1018         viafb_CRT_ON = STATE_ON;
1019         viafb_LCD_ON = STATE_ON;
1020         viafb_primary_dev = CRT_Device;
1021     } else if (!strcmp(viafb_active_dev, "LCD+CRT")) {
1022         /* LCD+CRT */
1023         viafb_CRT_ON = STATE_ON;
1024         viafb_LCD_ON = STATE_ON;
1025         viafb_primary_dev = LCD_Device;
1026     } else if (!strcmp(viafb_active_dev, "DVI+LCD")) {
1027         /* DVI+LCD */
1028         viafb_DVI_ON = STATE_ON;
1029         viafb_LCD_ON = STATE_ON;
1030         viafb_primary_dev = DVI_Device;
1031     } else if (!strcmp(viafb_active_dev, "LCD+DVI")) {
1032         /* LCD+DVI */
1033         viafb_DVI_ON = STATE_ON;
1034         viafb_LCD_ON = STATE_ON;
1035         viafb_primary_dev = LCD_Device;
1036     } else if (!strcmp(viafb_active_dev, "LCD+LCD2")) {
1037         viafb_LCD_ON = STATE_ON;
1038         viafb_LCD2_ON = STATE_ON;
1039         viafb_primary_dev = LCD_Device;
1040     } else if (!strcmp(viafb_active_dev, "LCD2+LCD")) {
1041         viafb_LCD_ON = STATE_ON;
1042         viafb_LCD2_ON = STATE_ON;
1043         viafb_primary_dev = LCD2_Device;
1044     } else if (!strcmp(viafb_active_dev, "CRT")) {
1045         /* CRT only */
1046         viafb_CRT_ON = STATE_ON;
1047         viafb_SAMM_ON = STATE_OFF;
1048     } else if (!strcmp(viafb_active_dev, "DVI")) {
1049         /* DVI only */
1050         viafb_DVI_ON = STATE_ON;
1051         viafb_SAMM_ON = STATE_OFF;
1052     } else if (!strcmp(viafb_active_dev, "LCD")) {
1053         /* LCD only */
1054         viafb_LCD_ON = STATE_ON;
1055         viafb_SAMM_ON = STATE_OFF;
1056     } else
1057         return -EINVAL;
1058 
1059     return 0;
1060 }
1061 
1062 static int parse_port(char *opt_str, int *output_interface)
1063 {
1064     if (!strncmp(opt_str, "DVP0", 4))
1065         *output_interface = INTERFACE_DVP0;
1066     else if (!strncmp(opt_str, "DVP1", 4))
1067         *output_interface = INTERFACE_DVP1;
1068     else if (!strncmp(opt_str, "DFP_HIGHLOW", 11))
1069         *output_interface = INTERFACE_DFP;
1070     else if (!strncmp(opt_str, "DFP_HIGH", 8))
1071         *output_interface = INTERFACE_DFP_HIGH;
1072     else if (!strncmp(opt_str, "DFP_LOW", 7))
1073         *output_interface = INTERFACE_DFP_LOW;
1074     else
1075         *output_interface = INTERFACE_NONE;
1076     return 0;
1077 }
1078 
1079 static void parse_lcd_port(void)
1080 {
1081     parse_port(viafb_lcd_port, &viaparinfo->chip_info->lvds_chip_info.
1082         output_interface);
1083     /*Initialize to avoid unexpected behavior */
1084     viaparinfo->chip_info->lvds_chip_info2.output_interface =
1085     INTERFACE_NONE;
1086 
1087     DEBUG_MSG(KERN_INFO "parse_lcd_port: viafb_lcd_port:%s,interface:%d\n",
1088           viafb_lcd_port, viaparinfo->chip_info->lvds_chip_info.
1089           output_interface);
1090 }
1091 
1092 static void parse_dvi_port(void)
1093 {
1094     parse_port(viafb_dvi_port, &viaparinfo->chip_info->tmds_chip_info.
1095         output_interface);
1096 
1097     DEBUG_MSG(KERN_INFO "parse_dvi_port: viafb_dvi_port:%s,interface:%d\n",
1098           viafb_dvi_port, viaparinfo->chip_info->tmds_chip_info.
1099           output_interface);
1100 }
1101 
1102 #ifdef CONFIG_FB_VIA_DIRECT_PROCFS
1103 
1104 /*
1105  * The proc filesystem read/write function, a simple proc implement to
1106  * get/set the value of DPA  DVP0,   DVP0DataDriving,  DVP0ClockDriving, DVP1,
1107  * DVP1Driving, DFPHigh, DFPLow CR96,   SR2A[5], SR1B[1], SR2A[4], SR1E[2],
1108  * CR9B,    SR65,    CR97,    CR99
1109  */
1110 static int viafb_dvp0_proc_show(struct seq_file *m, void *v)
1111 {
1112     u8 dvp0_data_dri = 0, dvp0_clk_dri = 0, dvp0 = 0;
1113     dvp0_data_dri =
1114         (viafb_read_reg(VIASR, SR2A) & BIT5) >> 4 |
1115         (viafb_read_reg(VIASR, SR1B) & BIT1) >> 1;
1116     dvp0_clk_dri =
1117         (viafb_read_reg(VIASR, SR2A) & BIT4) >> 3 |
1118         (viafb_read_reg(VIASR, SR1E) & BIT2) >> 2;
1119     dvp0 = viafb_read_reg(VIACR, CR96) & 0x0f;
1120     seq_printf(m, "%x %x %x\n", dvp0, dvp0_data_dri, dvp0_clk_dri);
1121     return 0;
1122 }
1123 
1124 static int viafb_dvp0_proc_open(struct inode *inode, struct file *file)
1125 {
1126     return single_open(file, viafb_dvp0_proc_show, NULL);
1127 }
1128 
1129 static ssize_t viafb_dvp0_proc_write(struct file *file,
1130     const char __user *buffer, size_t count, loff_t *pos)
1131 {
1132     char buf[20], *value, *pbuf;
1133     u8 reg_val = 0;
1134     unsigned long length, i;
1135     if (count < 1)
1136         return -EINVAL;
1137     length = count > 20 ? 20 : count;
1138     if (copy_from_user(&buf[0], buffer, length))
1139         return -EFAULT;
1140     buf[length - 1] = '\0'; /*Ensure end string */
1141     pbuf = &buf[0];
1142     for (i = 0; i < 3; i++) {
1143         value = strsep(&pbuf, " ");
1144         if (value != NULL) {
1145             if (kstrtou8(value, 0, &reg_val) < 0)
1146                 return -EINVAL;
1147             DEBUG_MSG(KERN_INFO "DVP0:reg_val[%lu]=:%x\n", i,
1148                   reg_val);
1149             switch (i) {
1150             case 0:
1151                 viafb_write_reg_mask(CR96, VIACR,
1152                     reg_val, 0x0f);
1153                 break;
1154             case 1:
1155                 viafb_write_reg_mask(SR2A, VIASR,
1156                     reg_val << 4, BIT5);
1157                 viafb_write_reg_mask(SR1B, VIASR,
1158                     reg_val << 1, BIT1);
1159                 break;
1160             case 2:
1161                 viafb_write_reg_mask(SR2A, VIASR,
1162                     reg_val << 3, BIT4);
1163                 viafb_write_reg_mask(SR1E, VIASR,
1164                     reg_val << 2, BIT2);
1165                 break;
1166             default:
1167                 break;
1168             }
1169         } else {
1170             break;
1171         }
1172     }
1173     return count;
1174 }
1175 
1176 static const struct proc_ops viafb_dvp0_proc_ops = {
1177     .proc_open  = viafb_dvp0_proc_open,
1178     .proc_read  = seq_read,
1179     .proc_lseek = seq_lseek,
1180     .proc_release   = single_release,
1181     .proc_write = viafb_dvp0_proc_write,
1182 };
1183 
1184 static int viafb_dvp1_proc_show(struct seq_file *m, void *v)
1185 {
1186     u8 dvp1 = 0, dvp1_data_dri = 0, dvp1_clk_dri = 0;
1187     dvp1 = viafb_read_reg(VIACR, CR9B) & 0x0f;
1188     dvp1_data_dri = (viafb_read_reg(VIASR, SR65) & 0x0c) >> 2;
1189     dvp1_clk_dri = viafb_read_reg(VIASR, SR65) & 0x03;
1190     seq_printf(m, "%x %x %x\n", dvp1, dvp1_data_dri, dvp1_clk_dri);
1191     return 0;
1192 }
1193 
1194 static int viafb_dvp1_proc_open(struct inode *inode, struct file *file)
1195 {
1196     return single_open(file, viafb_dvp1_proc_show, NULL);
1197 }
1198 
1199 static ssize_t viafb_dvp1_proc_write(struct file *file,
1200     const char __user *buffer, size_t count, loff_t *pos)
1201 {
1202     char buf[20], *value, *pbuf;
1203     u8 reg_val = 0;
1204     unsigned long length, i;
1205     if (count < 1)
1206         return -EINVAL;
1207     length = count > 20 ? 20 : count;
1208     if (copy_from_user(&buf[0], buffer, length))
1209         return -EFAULT;
1210     buf[length - 1] = '\0'; /*Ensure end string */
1211     pbuf = &buf[0];
1212     for (i = 0; i < 3; i++) {
1213         value = strsep(&pbuf, " ");
1214         if (value != NULL) {
1215             if (kstrtou8(value, 0, &reg_val) < 0)
1216                 return -EINVAL;
1217             switch (i) {
1218             case 0:
1219                 viafb_write_reg_mask(CR9B, VIACR,
1220                     reg_val, 0x0f);
1221                 break;
1222             case 1:
1223                 viafb_write_reg_mask(SR65, VIASR,
1224                     reg_val << 2, 0x0c);
1225                 break;
1226             case 2:
1227                 viafb_write_reg_mask(SR65, VIASR,
1228                     reg_val, 0x03);
1229                 break;
1230             default:
1231                 break;
1232             }
1233         } else {
1234             break;
1235         }
1236     }
1237     return count;
1238 }
1239 
1240 static const struct proc_ops viafb_dvp1_proc_ops = {
1241     .proc_open  = viafb_dvp1_proc_open,
1242     .proc_read  = seq_read,
1243     .proc_lseek = seq_lseek,
1244     .proc_release   = single_release,
1245     .proc_write = viafb_dvp1_proc_write,
1246 };
1247 
1248 static int viafb_dfph_proc_show(struct seq_file *m, void *v)
1249 {
1250     u8 dfp_high = 0;
1251     dfp_high = viafb_read_reg(VIACR, CR97) & 0x0f;
1252     seq_printf(m, "%x\n", dfp_high);
1253     return 0;
1254 }
1255 
1256 static int viafb_dfph_proc_open(struct inode *inode, struct file *file)
1257 {
1258     return single_open(file, viafb_dfph_proc_show, NULL);
1259 }
1260 
1261 static ssize_t viafb_dfph_proc_write(struct file *file,
1262     const char __user *buffer, size_t count, loff_t *pos)
1263 {
1264     int err;
1265     u8 reg_val;
1266     err = kstrtou8_from_user(buffer, count, 0, &reg_val);
1267     if (err)
1268         return err;
1269 
1270     viafb_write_reg_mask(CR97, VIACR, reg_val, 0x0f);
1271     return count;
1272 }
1273 
1274 static const struct proc_ops viafb_dfph_proc_ops = {
1275     .proc_open  = viafb_dfph_proc_open,
1276     .proc_read  = seq_read,
1277     .proc_lseek = seq_lseek,
1278     .proc_release   = single_release,
1279     .proc_write = viafb_dfph_proc_write,
1280 };
1281 
1282 static int viafb_dfpl_proc_show(struct seq_file *m, void *v)
1283 {
1284     u8 dfp_low = 0;
1285     dfp_low = viafb_read_reg(VIACR, CR99) & 0x0f;
1286     seq_printf(m, "%x\n", dfp_low);
1287     return 0;
1288 }
1289 
1290 static int viafb_dfpl_proc_open(struct inode *inode, struct file *file)
1291 {
1292     return single_open(file, viafb_dfpl_proc_show, NULL);
1293 }
1294 
1295 static ssize_t viafb_dfpl_proc_write(struct file *file,
1296     const char __user *buffer, size_t count, loff_t *pos)
1297 {
1298     int err;
1299     u8 reg_val;
1300     err = kstrtou8_from_user(buffer, count, 0, &reg_val);
1301     if (err)
1302         return err;
1303 
1304     viafb_write_reg_mask(CR99, VIACR, reg_val, 0x0f);
1305     return count;
1306 }
1307 
1308 static const struct proc_ops viafb_dfpl_proc_ops = {
1309     .proc_open  = viafb_dfpl_proc_open,
1310     .proc_read  = seq_read,
1311     .proc_lseek = seq_lseek,
1312     .proc_release   = single_release,
1313     .proc_write = viafb_dfpl_proc_write,
1314 };
1315 
1316 static int viafb_vt1636_proc_show(struct seq_file *m, void *v)
1317 {
1318     u8 vt1636_08 = 0, vt1636_09 = 0;
1319     switch (viaparinfo->chip_info->lvds_chip_info.lvds_chip_name) {
1320     case VT1636_LVDS:
1321         vt1636_08 =
1322             viafb_gpio_i2c_read_lvds(viaparinfo->lvds_setting_info,
1323             &viaparinfo->chip_info->lvds_chip_info, 0x08) & 0x0f;
1324         vt1636_09 =
1325             viafb_gpio_i2c_read_lvds(viaparinfo->lvds_setting_info,
1326             &viaparinfo->chip_info->lvds_chip_info, 0x09) & 0x1f;
1327         seq_printf(m, "%x %x\n", vt1636_08, vt1636_09);
1328         break;
1329     default:
1330         break;
1331     }
1332     switch (viaparinfo->chip_info->lvds_chip_info2.lvds_chip_name) {
1333     case VT1636_LVDS:
1334         vt1636_08 =
1335             viafb_gpio_i2c_read_lvds(viaparinfo->lvds_setting_info2,
1336             &viaparinfo->chip_info->lvds_chip_info2, 0x08) & 0x0f;
1337         vt1636_09 =
1338             viafb_gpio_i2c_read_lvds(viaparinfo->lvds_setting_info2,
1339             &viaparinfo->chip_info->lvds_chip_info2, 0x09) & 0x1f;
1340         seq_printf(m, " %x %x\n", vt1636_08, vt1636_09);
1341         break;
1342     default:
1343         break;
1344     }
1345     return 0;
1346 }
1347 
1348 static int viafb_vt1636_proc_open(struct inode *inode, struct file *file)
1349 {
1350     return single_open(file, viafb_vt1636_proc_show, NULL);
1351 }
1352 
1353 static ssize_t viafb_vt1636_proc_write(struct file *file,
1354     const char __user *buffer, size_t count, loff_t *pos)
1355 {
1356     char buf[30], *value, *pbuf;
1357     struct IODATA reg_val;
1358     unsigned long length, i;
1359     if (count < 1)
1360         return -EINVAL;
1361     length = count > 30 ? 30 : count;
1362     if (copy_from_user(&buf[0], buffer, length))
1363         return -EFAULT;
1364     buf[length - 1] = '\0'; /*Ensure end string */
1365     pbuf = &buf[0];
1366     switch (viaparinfo->chip_info->lvds_chip_info.lvds_chip_name) {
1367     case VT1636_LVDS:
1368         for (i = 0; i < 2; i++) {
1369             value = strsep(&pbuf, " ");
1370             if (value != NULL) {
1371                 if (kstrtou8(value, 0, &reg_val.Data) < 0)
1372                     return -EINVAL;
1373                 switch (i) {
1374                 case 0:
1375                     reg_val.Index = 0x08;
1376                     reg_val.Mask = 0x0f;
1377                     viafb_gpio_i2c_write_mask_lvds
1378                         (viaparinfo->lvds_setting_info,
1379                         &viaparinfo->
1380                         chip_info->lvds_chip_info,
1381                          reg_val);
1382                     break;
1383                 case 1:
1384                     reg_val.Index = 0x09;
1385                     reg_val.Mask = 0x1f;
1386                     viafb_gpio_i2c_write_mask_lvds
1387                         (viaparinfo->lvds_setting_info,
1388                         &viaparinfo->
1389                         chip_info->lvds_chip_info,
1390                          reg_val);
1391                     break;
1392                 default:
1393                     break;
1394                 }
1395             } else {
1396                 break;
1397             }
1398         }
1399         break;
1400     default:
1401         break;
1402     }
1403     switch (viaparinfo->chip_info->lvds_chip_info2.lvds_chip_name) {
1404     case VT1636_LVDS:
1405         for (i = 0; i < 2; i++) {
1406             value = strsep(&pbuf, " ");
1407             if (value != NULL) {
1408                 if (kstrtou8(value, 0, &reg_val.Data) < 0)
1409                     return -EINVAL;
1410                 switch (i) {
1411                 case 0:
1412                     reg_val.Index = 0x08;
1413                     reg_val.Mask = 0x0f;
1414                     viafb_gpio_i2c_write_mask_lvds
1415                         (viaparinfo->lvds_setting_info2,
1416                         &viaparinfo->
1417                         chip_info->lvds_chip_info2,
1418                          reg_val);
1419                     break;
1420                 case 1:
1421                     reg_val.Index = 0x09;
1422                     reg_val.Mask = 0x1f;
1423                     viafb_gpio_i2c_write_mask_lvds
1424                         (viaparinfo->lvds_setting_info2,
1425                         &viaparinfo->
1426                         chip_info->lvds_chip_info2,
1427                          reg_val);
1428                     break;
1429                 default:
1430                     break;
1431                 }
1432             } else {
1433                 break;
1434             }
1435         }
1436         break;
1437     default:
1438         break;
1439     }
1440     return count;
1441 }
1442 
1443 static const struct proc_ops viafb_vt1636_proc_ops = {
1444     .proc_open  = viafb_vt1636_proc_open,
1445     .proc_read  = seq_read,
1446     .proc_lseek = seq_lseek,
1447     .proc_release   = single_release,
1448     .proc_write = viafb_vt1636_proc_write,
1449 };
1450 
1451 #endif /* CONFIG_FB_VIA_DIRECT_PROCFS */
1452 
1453 static int __maybe_unused viafb_sup_odev_proc_show(struct seq_file *m, void *v)
1454 {
1455     via_odev_to_seq(m, supported_odev_map[
1456         viaparinfo->shared->chip_info.gfx_chip_name]);
1457     return 0;
1458 }
1459 
1460 static ssize_t odev_update(const char __user *buffer, size_t count, u32 *odev)
1461 {
1462     char buf[64], *ptr = buf;
1463     u32 devices;
1464     bool add, sub;
1465 
1466     if (count < 1 || count > 63)
1467         return -EINVAL;
1468     if (copy_from_user(&buf[0], buffer, count))
1469         return -EFAULT;
1470     buf[count] = '\0';
1471     add = buf[0] == '+';
1472     sub = buf[0] == '-';
1473     if (add || sub)
1474         ptr++;
1475     devices = via_parse_odev(ptr, &ptr);
1476     if (*ptr == '\n')
1477         ptr++;
1478     if (*ptr != 0)
1479         return -EINVAL;
1480     if (add)
1481         *odev |= devices;
1482     else if (sub)
1483         *odev &= ~devices;
1484     else
1485         *odev = devices;
1486     return count;
1487 }
1488 
1489 static int viafb_iga1_odev_proc_show(struct seq_file *m, void *v)
1490 {
1491     via_odev_to_seq(m, viaparinfo->shared->iga1_devices);
1492     return 0;
1493 }
1494 
1495 static int viafb_iga1_odev_proc_open(struct inode *inode, struct file *file)
1496 {
1497     return single_open(file, viafb_iga1_odev_proc_show, NULL);
1498 }
1499 
1500 static ssize_t viafb_iga1_odev_proc_write(struct file *file,
1501     const char __user *buffer, size_t count, loff_t *pos)
1502 {
1503     u32 dev_on, dev_off, dev_old, dev_new;
1504     ssize_t res;
1505 
1506     dev_old = dev_new = viaparinfo->shared->iga1_devices;
1507     res = odev_update(buffer, count, &dev_new);
1508     if (res != count)
1509         return res;
1510     dev_off = dev_old & ~dev_new;
1511     dev_on = dev_new & ~dev_old;
1512     viaparinfo->shared->iga1_devices = dev_new;
1513     viaparinfo->shared->iga2_devices &= ~dev_new;
1514     via_set_state(dev_off, VIA_STATE_OFF);
1515     via_set_source(dev_new, IGA1);
1516     via_set_state(dev_on, VIA_STATE_ON);
1517     return res;
1518 }
1519 
1520 static const struct proc_ops viafb_iga1_odev_proc_ops = {
1521     .proc_open  = viafb_iga1_odev_proc_open,
1522     .proc_read  = seq_read,
1523     .proc_lseek = seq_lseek,
1524     .proc_release   = single_release,
1525     .proc_write = viafb_iga1_odev_proc_write,
1526 };
1527 
1528 static int viafb_iga2_odev_proc_show(struct seq_file *m, void *v)
1529 {
1530     via_odev_to_seq(m, viaparinfo->shared->iga2_devices);
1531     return 0;
1532 }
1533 
1534 static int viafb_iga2_odev_proc_open(struct inode *inode, struct file *file)
1535 {
1536     return single_open(file, viafb_iga2_odev_proc_show, NULL);
1537 }
1538 
1539 static ssize_t viafb_iga2_odev_proc_write(struct file *file,
1540     const char __user *buffer, size_t count, loff_t *pos)
1541 {
1542     u32 dev_on, dev_off, dev_old, dev_new;
1543     ssize_t res;
1544 
1545     dev_old = dev_new = viaparinfo->shared->iga2_devices;
1546     res = odev_update(buffer, count, &dev_new);
1547     if (res != count)
1548         return res;
1549     dev_off = dev_old & ~dev_new;
1550     dev_on = dev_new & ~dev_old;
1551     viaparinfo->shared->iga2_devices = dev_new;
1552     viaparinfo->shared->iga1_devices &= ~dev_new;
1553     via_set_state(dev_off, VIA_STATE_OFF);
1554     via_set_source(dev_new, IGA2);
1555     via_set_state(dev_on, VIA_STATE_ON);
1556     return res;
1557 }
1558 
1559 static const struct proc_ops viafb_iga2_odev_proc_ops = {
1560     .proc_open  = viafb_iga2_odev_proc_open,
1561     .proc_read  = seq_read,
1562     .proc_lseek = seq_lseek,
1563     .proc_release   = single_release,
1564     .proc_write = viafb_iga2_odev_proc_write,
1565 };
1566 
1567 #define IS_VT1636(lvds_chip)    ((lvds_chip).lvds_chip_name == VT1636_LVDS)
1568 static void viafb_init_proc(struct viafb_shared *shared)
1569 {
1570     struct proc_dir_entry *iga1_entry, *iga2_entry,
1571         *viafb_entry = proc_mkdir("viafb", NULL);
1572 
1573     shared->proc_entry = viafb_entry;
1574     if (viafb_entry) {
1575 #ifdef CONFIG_FB_VIA_DIRECT_PROCFS
1576         proc_create("dvp0", 0, viafb_entry, &viafb_dvp0_proc_ops);
1577         proc_create("dvp1", 0, viafb_entry, &viafb_dvp1_proc_ops);
1578         proc_create("dfph", 0, viafb_entry, &viafb_dfph_proc_ops);
1579         proc_create("dfpl", 0, viafb_entry, &viafb_dfpl_proc_ops);
1580         if (IS_VT1636(shared->chip_info.lvds_chip_info)
1581             || IS_VT1636(shared->chip_info.lvds_chip_info2))
1582             proc_create("vt1636", 0, viafb_entry,
1583                     &viafb_vt1636_proc_ops);
1584 #endif /* CONFIG_FB_VIA_DIRECT_PROCFS */
1585 
1586         proc_create_single("supported_output_devices", 0, viafb_entry,
1587             viafb_sup_odev_proc_show);
1588         iga1_entry = proc_mkdir("iga1", viafb_entry);
1589         shared->iga1_proc_entry = iga1_entry;
1590         proc_create("output_devices", 0, iga1_entry,
1591                 &viafb_iga1_odev_proc_ops);
1592         iga2_entry = proc_mkdir("iga2", viafb_entry);
1593         shared->iga2_proc_entry = iga2_entry;
1594         proc_create("output_devices", 0, iga2_entry,
1595                 &viafb_iga2_odev_proc_ops);
1596     }
1597 }
1598 static void viafb_remove_proc(struct viafb_shared *shared)
1599 {
1600     struct proc_dir_entry *viafb_entry = shared->proc_entry;
1601 
1602     if (!viafb_entry)
1603         return;
1604 
1605     remove_proc_entry("output_devices", shared->iga2_proc_entry);
1606     remove_proc_entry("iga2", viafb_entry);
1607     remove_proc_entry("output_devices", shared->iga1_proc_entry);
1608     remove_proc_entry("iga1", viafb_entry);
1609     remove_proc_entry("supported_output_devices", viafb_entry);
1610 
1611 #ifdef CONFIG_FB_VIA_DIRECT_PROCFS
1612     remove_proc_entry("dvp0", viafb_entry);/* parent dir */
1613     remove_proc_entry("dvp1", viafb_entry);
1614     remove_proc_entry("dfph", viafb_entry);
1615     remove_proc_entry("dfpl", viafb_entry);
1616     if (IS_VT1636(shared->chip_info.lvds_chip_info)
1617         || IS_VT1636(shared->chip_info.lvds_chip_info2))
1618         remove_proc_entry("vt1636", viafb_entry);
1619 #endif /* CONFIG_FB_VIA_DIRECT_PROCFS */
1620 
1621     remove_proc_entry("viafb", NULL);
1622 }
1623 #undef IS_VT1636
1624 
1625 static int parse_mode(const char *str, u32 devices, u32 *xres, u32 *yres)
1626 {
1627     const struct fb_videomode *mode = NULL;
1628     char *ptr;
1629 
1630     if (!str) {
1631         if (devices == VIA_CRT)
1632             mode = via_aux_get_preferred_mode(
1633                 viaparinfo->shared->i2c_26);
1634         else if (devices == VIA_DVP1)
1635             mode = via_aux_get_preferred_mode(
1636                 viaparinfo->shared->i2c_31);
1637 
1638         if (mode) {
1639             *xres = mode->xres;
1640             *yres = mode->yres;
1641         } else if (machine_is_olpc()) {
1642             *xres = 1200;
1643             *yres = 900;
1644         } else {
1645             *xres = 640;
1646             *yres = 480;
1647         }
1648         return 0;
1649     }
1650 
1651     *xres = simple_strtoul(str, &ptr, 10);
1652     if (ptr[0] != 'x')
1653         return -EINVAL;
1654 
1655     *yres = simple_strtoul(&ptr[1], &ptr, 10);
1656     if (ptr[0])
1657         return -EINVAL;
1658 
1659     return 0;
1660 }
1661 
1662 
1663 #ifdef CONFIG_PM
1664 static int viafb_suspend(void *unused)
1665 {
1666     console_lock();
1667     fb_set_suspend(viafbinfo, 1);
1668     viafb_sync(viafbinfo);
1669     console_unlock();
1670 
1671     return 0;
1672 }
1673 
1674 static int viafb_resume(void *unused)
1675 {
1676     console_lock();
1677     if (viaparinfo->shared->vdev->engine_mmio)
1678         viafb_reset_engine(viaparinfo);
1679     viafb_set_par(viafbinfo);
1680     if (viafb_dual_fb)
1681         viafb_set_par(viafbinfo1);
1682     fb_set_suspend(viafbinfo, 0);
1683 
1684     console_unlock();
1685     return 0;
1686 }
1687 
1688 static struct viafb_pm_hooks viafb_fb_pm_hooks = {
1689     .suspend = viafb_suspend,
1690     .resume = viafb_resume
1691 };
1692 
1693 #endif
1694 
1695 static void i2c_bus_probe(struct viafb_shared *shared)
1696 {
1697     /* should be always CRT */
1698     printk(KERN_INFO "viafb: Probing I2C bus 0x26\n");
1699     shared->i2c_26 = via_aux_probe(viafb_find_i2c_adapter(VIA_PORT_26));
1700 
1701     /* seems to be usually DVP1 */
1702     printk(KERN_INFO "viafb: Probing I2C bus 0x31\n");
1703     shared->i2c_31 = via_aux_probe(viafb_find_i2c_adapter(VIA_PORT_31));
1704 
1705     /* FIXME: what is this? */
1706     if (!machine_is_olpc()) {
1707         printk(KERN_INFO "viafb: Probing I2C bus 0x2C\n");
1708         shared->i2c_2C = via_aux_probe(viafb_find_i2c_adapter(VIA_PORT_2C));
1709     }
1710 
1711     printk(KERN_INFO "viafb: Finished I2C bus probing");
1712 }
1713 
1714 static void i2c_bus_free(struct viafb_shared *shared)
1715 {
1716     via_aux_free(shared->i2c_26);
1717     via_aux_free(shared->i2c_31);
1718     via_aux_free(shared->i2c_2C);
1719 }
1720 
1721 int via_fb_pci_probe(struct viafb_dev *vdev)
1722 {
1723     u32 default_xres, default_yres;
1724     struct fb_var_screeninfo default_var;
1725     int rc;
1726     u32 viafb_par_length;
1727 
1728     DEBUG_MSG(KERN_INFO "VIAFB PCI Probe!!\n");
1729     memset(&default_var, 0, sizeof(default_var));
1730     viafb_par_length = ALIGN(sizeof(struct viafb_par), BITS_PER_LONG/8);
1731 
1732     /* Allocate fb_info and ***_par here, also including some other needed
1733      * variables
1734     */
1735     viafbinfo = framebuffer_alloc(viafb_par_length +
1736         ALIGN(sizeof(struct viafb_shared), BITS_PER_LONG/8),
1737         &vdev->pdev->dev);
1738     if (!viafbinfo)
1739         return -ENOMEM;
1740 
1741     viaparinfo = (struct viafb_par *)viafbinfo->par;
1742     viaparinfo->shared = viafbinfo->par + viafb_par_length;
1743     viaparinfo->shared->vdev = vdev;
1744     viaparinfo->vram_addr = 0;
1745     viaparinfo->tmds_setting_info = &viaparinfo->shared->tmds_setting_info;
1746     viaparinfo->lvds_setting_info = &viaparinfo->shared->lvds_setting_info;
1747     viaparinfo->lvds_setting_info2 =
1748         &viaparinfo->shared->lvds_setting_info2;
1749     viaparinfo->chip_info = &viaparinfo->shared->chip_info;
1750 
1751     i2c_bus_probe(viaparinfo->shared);
1752     if (viafb_dual_fb)
1753         viafb_SAMM_ON = 1;
1754     parse_lcd_port();
1755     parse_dvi_port();
1756 
1757     viafb_init_chip_info(vdev->chip_type);
1758     /*
1759      * The framebuffer will have been successfully mapped by
1760      * the core (or we'd not be here), but we still need to
1761      * set up our own accounting.
1762      */
1763     viaparinfo->fbmem = vdev->fbmem_start;
1764     viaparinfo->memsize = vdev->fbmem_len;
1765     viaparinfo->fbmem_free = viaparinfo->memsize;
1766     viaparinfo->fbmem_used = 0;
1767     viafbinfo->screen_base = vdev->fbmem;
1768 
1769     viafbinfo->fix.mmio_start = vdev->engine_start;
1770     viafbinfo->fix.mmio_len = vdev->engine_len;
1771     viafbinfo->node = 0;
1772     viafbinfo->fbops = &viafb_ops;
1773     viafbinfo->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
1774 
1775     viafbinfo->pseudo_palette = pseudo_pal;
1776     if (viafb_accel && !viafb_setup_engine(viafbinfo)) {
1777         viafbinfo->flags |= FBINFO_HWACCEL_COPYAREA |
1778             FBINFO_HWACCEL_FILLRECT |  FBINFO_HWACCEL_IMAGEBLIT;
1779         default_var.accel_flags = FB_ACCELF_TEXT;
1780     } else {
1781         viafbinfo->flags |= FBINFO_HWACCEL_DISABLED;
1782         default_var.accel_flags = 0;
1783     }
1784 
1785     if (viafb_second_size && (viafb_second_size < 8)) {
1786         viafb_second_offset = viaparinfo->fbmem_free -
1787             viafb_second_size * 1024 * 1024;
1788     } else {
1789         viafb_second_size = 8;
1790         viafb_second_offset = viaparinfo->fbmem_free -
1791             viafb_second_size * 1024 * 1024;
1792     }
1793 
1794     parse_mode(viafb_mode, viaparinfo->shared->iga1_devices,
1795         &default_xres, &default_yres);
1796     if (viafb_SAMM_ON == 1)
1797         parse_mode(viafb_mode1, viaparinfo->shared->iga2_devices,
1798             &viafb_second_xres, &viafb_second_yres);
1799 
1800     default_var.xres = default_xres;
1801     default_var.yres = default_yres;
1802     default_var.xres_virtual = default_xres;
1803     default_var.yres_virtual = default_yres;
1804     default_var.bits_per_pixel = viafb_bpp;
1805     viafb_fill_var_timing_info(&default_var, viafb_get_best_mode(
1806         default_var.xres, default_var.yres, viafb_refresh));
1807     viafb_setup_fixinfo(&viafbinfo->fix, viaparinfo);
1808     viafbinfo->var = default_var;
1809 
1810     if (viafb_dual_fb) {
1811         viafbinfo1 = framebuffer_alloc(viafb_par_length,
1812                 &vdev->pdev->dev);
1813         if (!viafbinfo1) {
1814             rc = -ENOMEM;
1815             goto out_fb_release;
1816         }
1817         viaparinfo1 = viafbinfo1->par;
1818         memcpy(viaparinfo1, viaparinfo, viafb_par_length);
1819         viaparinfo1->vram_addr = viafb_second_offset;
1820         viaparinfo1->memsize = viaparinfo->memsize -
1821             viafb_second_offset;
1822         viaparinfo->memsize = viafb_second_offset;
1823         viaparinfo1->fbmem = viaparinfo->fbmem + viafb_second_offset;
1824 
1825         viaparinfo1->fbmem_used = viaparinfo->fbmem_used;
1826         viaparinfo1->fbmem_free = viaparinfo1->memsize -
1827             viaparinfo1->fbmem_used;
1828         viaparinfo->fbmem_free = viaparinfo->memsize;
1829         viaparinfo->fbmem_used = 0;
1830 
1831         viaparinfo->iga_path = IGA1;
1832         viaparinfo1->iga_path = IGA2;
1833         memcpy(viafbinfo1, viafbinfo, sizeof(struct fb_info));
1834         viafbinfo1->par = viaparinfo1;
1835         viafbinfo1->screen_base = viafbinfo->screen_base +
1836             viafb_second_offset;
1837 
1838         default_var.xres = viafb_second_xres;
1839         default_var.yres = viafb_second_yres;
1840         default_var.xres_virtual = viafb_second_xres;
1841         default_var.yres_virtual = viafb_second_yres;
1842         default_var.bits_per_pixel = viafb_bpp1;
1843         viafb_fill_var_timing_info(&default_var, viafb_get_best_mode(
1844             default_var.xres, default_var.yres, viafb_refresh1));
1845 
1846         viafb_setup_fixinfo(&viafbinfo1->fix, viaparinfo1);
1847         viafb_check_var(&default_var, viafbinfo1);
1848         viafbinfo1->var = default_var;
1849         viafb_update_fix(viafbinfo1);
1850         viaparinfo1->depth = fb_get_color_depth(&viafbinfo1->var,
1851             &viafbinfo1->fix);
1852     }
1853 
1854     viafb_check_var(&viafbinfo->var, viafbinfo);
1855     viafb_update_fix(viafbinfo);
1856     viaparinfo->depth = fb_get_color_depth(&viafbinfo->var,
1857         &viafbinfo->fix);
1858     default_var.activate = FB_ACTIVATE_NOW;
1859     rc = fb_alloc_cmap(&viafbinfo->cmap, 256, 0);
1860     if (rc)
1861         goto out_fb1_release;
1862 
1863     if (viafb_dual_fb && (viafb_primary_dev == LCD_Device)
1864         && (viaparinfo->chip_info->gfx_chip_name == UNICHROME_CLE266)) {
1865         rc = register_framebuffer(viafbinfo1);
1866         if (rc)
1867             goto out_dealloc_cmap;
1868     }
1869     rc = register_framebuffer(viafbinfo);
1870     if (rc)
1871         goto out_fb1_unreg_lcd_cle266;
1872 
1873     if (viafb_dual_fb && ((viafb_primary_dev != LCD_Device)
1874             || (viaparinfo->chip_info->gfx_chip_name !=
1875             UNICHROME_CLE266))) {
1876         rc = register_framebuffer(viafbinfo1);
1877         if (rc)
1878             goto out_fb_unreg;
1879     }
1880     DEBUG_MSG(KERN_INFO "fb%d: %s frame buffer device %dx%d-%dbpp\n",
1881           viafbinfo->node, viafbinfo->fix.id, default_var.xres,
1882           default_var.yres, default_var.bits_per_pixel);
1883 
1884     viafb_init_proc(viaparinfo->shared);
1885     viafb_init_dac(IGA2);
1886 
1887 #ifdef CONFIG_PM
1888     viafb_pm_register(&viafb_fb_pm_hooks);
1889 #endif
1890     return 0;
1891 
1892 out_fb_unreg:
1893     unregister_framebuffer(viafbinfo);
1894 out_fb1_unreg_lcd_cle266:
1895     if (viafb_dual_fb && (viafb_primary_dev == LCD_Device)
1896         && (viaparinfo->chip_info->gfx_chip_name == UNICHROME_CLE266))
1897         unregister_framebuffer(viafbinfo1);
1898 out_dealloc_cmap:
1899     fb_dealloc_cmap(&viafbinfo->cmap);
1900 out_fb1_release:
1901     framebuffer_release(viafbinfo1);
1902 out_fb_release:
1903     i2c_bus_free(viaparinfo->shared);
1904     framebuffer_release(viafbinfo);
1905     return rc;
1906 }
1907 
1908 void via_fb_pci_remove(struct pci_dev *pdev)
1909 {
1910     DEBUG_MSG(KERN_INFO "via_pci_remove!\n");
1911     fb_dealloc_cmap(&viafbinfo->cmap);
1912     unregister_framebuffer(viafbinfo);
1913     if (viafb_dual_fb)
1914         unregister_framebuffer(viafbinfo1);
1915     viafb_remove_proc(viaparinfo->shared);
1916     i2c_bus_free(viaparinfo->shared);
1917     framebuffer_release(viafbinfo);
1918     if (viafb_dual_fb)
1919         framebuffer_release(viafbinfo1);
1920 }
1921 
1922 #ifndef MODULE
1923 static int __init viafb_setup(void)
1924 {
1925     char *this_opt;
1926     char *options;
1927 
1928     DEBUG_MSG(KERN_INFO "viafb_setup!\n");
1929 
1930     if (fb_get_options("viafb", &options))
1931         return -ENODEV;
1932 
1933     if (!options || !*options)
1934         return 0;
1935 
1936     while ((this_opt = strsep(&options, ",")) != NULL) {
1937         if (!*this_opt)
1938             continue;
1939 
1940         if (!strncmp(this_opt, "viafb_mode1=", 12)) {
1941             viafb_mode1 = kstrdup(this_opt + 12, GFP_KERNEL);
1942             if (!viafb_mode1)
1943                 return -ENOMEM;
1944         } else if (!strncmp(this_opt, "viafb_mode=", 11)) {
1945             viafb_mode = kstrdup(this_opt + 11, GFP_KERNEL);
1946             if (!viafb_mode)
1947                 return -ENOMEM;
1948         } else if (!strncmp(this_opt, "viafb_bpp1=", 11)) {
1949             if (kstrtouint(this_opt + 11, 0, &viafb_bpp1) < 0)
1950                 return -EINVAL;
1951         } else if (!strncmp(this_opt, "viafb_bpp=", 10)) {
1952             if (kstrtouint(this_opt + 10, 0, &viafb_bpp) < 0)
1953                 return -EINVAL;
1954         } else if (!strncmp(this_opt, "viafb_refresh1=", 15)) {
1955             if (kstrtoint(this_opt + 15, 0, &viafb_refresh1) < 0)
1956                 return -EINVAL;
1957         } else if (!strncmp(this_opt, "viafb_refresh=", 14)) {
1958             if (kstrtoint(this_opt + 14, 0, &viafb_refresh) < 0)
1959                 return -EINVAL;
1960         } else if (!strncmp(this_opt, "viafb_lcd_dsp_method=", 21)) {
1961             if (kstrtoint(this_opt + 21, 0,
1962                       &viafb_lcd_dsp_method) < 0)
1963                 return -EINVAL;
1964         } else if (!strncmp(this_opt, "viafb_lcd_panel_id=", 19)) {
1965             if (kstrtoint(this_opt + 19, 0,
1966                       &viafb_lcd_panel_id) < 0)
1967                 return -EINVAL;
1968         } else if (!strncmp(this_opt, "viafb_accel=", 12)) {
1969             if (kstrtoint(this_opt + 12, 0, &viafb_accel) < 0)
1970                 return -EINVAL;
1971         } else if (!strncmp(this_opt, "viafb_SAMM_ON=", 14)) {
1972             if (kstrtoint(this_opt + 14, 0, &viafb_SAMM_ON) < 0)
1973                 return -EINVAL;
1974         } else if (!strncmp(this_opt, "viafb_active_dev=", 17)) {
1975             viafb_active_dev = kstrdup(this_opt + 17, GFP_KERNEL);
1976             if (!viafb_active_dev)
1977                 return -ENOMEM;
1978         } else if (!strncmp(this_opt,
1979             "viafb_display_hardware_layout=", 30)) {
1980             if (kstrtoint(this_opt + 30, 0,
1981                       &viafb_display_hardware_layout) < 0)
1982                 return -EINVAL;
1983         } else if (!strncmp(this_opt, "viafb_second_size=", 18)) {
1984             if (kstrtoint(this_opt + 18, 0, &viafb_second_size) < 0)
1985                 return -EINVAL;
1986         } else if (!strncmp(this_opt,
1987             "viafb_platform_epia_dvi=", 24)) {
1988             if (kstrtoint(this_opt + 24, 0,
1989                       &viafb_platform_epia_dvi) < 0)
1990                 return -EINVAL;
1991         } else if (!strncmp(this_opt,
1992             "viafb_device_lcd_dualedge=", 26)) {
1993             if (kstrtoint(this_opt + 26, 0,
1994                       &viafb_device_lcd_dualedge) < 0)
1995                 return -EINVAL;
1996         } else if (!strncmp(this_opt, "viafb_bus_width=", 16)) {
1997             if (kstrtoint(this_opt + 16, 0, &viafb_bus_width) < 0)
1998                 return -EINVAL;
1999         } else if (!strncmp(this_opt, "viafb_lcd_mode=", 15)) {
2000             if (kstrtoint(this_opt + 15, 0, &viafb_lcd_mode) < 0)
2001                 return -EINVAL;
2002         } else if (!strncmp(this_opt, "viafb_lcd_port=", 15)) {
2003             viafb_lcd_port = kstrdup(this_opt + 15, GFP_KERNEL);
2004             if (!viafb_lcd_port)
2005                 return -ENOMEM;
2006         } else if (!strncmp(this_opt, "viafb_dvi_port=", 15)) {
2007             viafb_dvi_port = kstrdup(this_opt + 15, GFP_KERNEL);
2008             if (!viafb_dvi_port)
2009                 return -ENOMEM;
2010         }
2011     }
2012     return 0;
2013 }
2014 #endif
2015 
2016 /*
2017  * These are called out of via-core for now.
2018  */
2019 int __init viafb_init(void)
2020 {
2021     u32 dummy_x, dummy_y;
2022     int r = 0;
2023 
2024     if (machine_is_olpc())
2025         /* Apply XO-1.5-specific configuration. */
2026         viafb_lcd_panel_id = 23;
2027 
2028 #ifndef MODULE
2029     r = viafb_setup();
2030     if (r < 0)
2031         return r;
2032 #endif
2033     if (parse_mode(viafb_mode, 0, &dummy_x, &dummy_y)
2034         || !viafb_get_best_mode(dummy_x, dummy_y, viafb_refresh)
2035         || parse_mode(viafb_mode1, 0, &dummy_x, &dummy_y)
2036         || !viafb_get_best_mode(dummy_x, dummy_y, viafb_refresh1)
2037         || viafb_bpp < 0 || viafb_bpp > 32
2038         || viafb_bpp1 < 0 || viafb_bpp1 > 32
2039         || parse_active_dev())
2040         return -EINVAL;
2041 
2042     printk(KERN_INFO
2043        "VIA Graphics Integration Chipset framebuffer %d.%d initializing\n",
2044            VERSION_MAJOR, VERSION_MINOR);
2045     return r;
2046 }
2047 
2048 void __exit viafb_exit(void)
2049 {
2050     DEBUG_MSG(KERN_INFO "viafb_exit!\n");
2051 }
2052 
2053 static struct fb_ops viafb_ops = {
2054     .owner = THIS_MODULE,
2055     .fb_open = viafb_open,
2056     .fb_release = viafb_release,
2057     .fb_check_var = viafb_check_var,
2058     .fb_set_par = viafb_set_par,
2059     .fb_setcolreg = viafb_setcolreg,
2060     .fb_pan_display = viafb_pan_display,
2061     .fb_blank = viafb_blank,
2062     .fb_fillrect = viafb_fillrect,
2063     .fb_copyarea = viafb_copyarea,
2064     .fb_imageblit = viafb_imageblit,
2065     .fb_cursor = viafb_cursor,
2066     .fb_ioctl = viafb_ioctl,
2067     .fb_sync = viafb_sync,
2068 };
2069 
2070 
2071 #ifdef MODULE
2072 module_param(viafb_mode, charp, S_IRUSR);
2073 MODULE_PARM_DESC(viafb_mode, "Set resolution (default=640x480)");
2074 
2075 module_param(viafb_mode1, charp, S_IRUSR);
2076 MODULE_PARM_DESC(viafb_mode1, "Set resolution (default=640x480)");
2077 
2078 module_param(viafb_bpp, int, S_IRUSR);
2079 MODULE_PARM_DESC(viafb_bpp, "Set color depth (default=32bpp)");
2080 
2081 module_param(viafb_bpp1, int, S_IRUSR);
2082 MODULE_PARM_DESC(viafb_bpp1, "Set color depth (default=32bpp)");
2083 
2084 module_param(viafb_refresh, int, S_IRUSR);
2085 MODULE_PARM_DESC(viafb_refresh,
2086     "Set CRT viafb_refresh rate (default = 60)");
2087 
2088 module_param(viafb_refresh1, int, S_IRUSR);
2089 MODULE_PARM_DESC(viafb_refresh1,
2090     "Set CRT refresh rate (default = 60)");
2091 
2092 module_param(viafb_lcd_panel_id, int, S_IRUSR);
2093 MODULE_PARM_DESC(viafb_lcd_panel_id,
2094     "Set Flat Panel type(Default=1024x768)");
2095 
2096 module_param(viafb_lcd_dsp_method, int, S_IRUSR);
2097 MODULE_PARM_DESC(viafb_lcd_dsp_method,
2098     "Set Flat Panel display scaling method.(Default=Expansion)");
2099 
2100 module_param(viafb_SAMM_ON, int, S_IRUSR);
2101 MODULE_PARM_DESC(viafb_SAMM_ON,
2102     "Turn on/off flag of SAMM(Default=OFF)");
2103 
2104 module_param(viafb_accel, int, S_IRUSR);
2105 MODULE_PARM_DESC(viafb_accel,
2106     "Set 2D Hardware Acceleration: 0 = OFF, 1 = ON (default)");
2107 
2108 module_param(viafb_active_dev, charp, S_IRUSR);
2109 MODULE_PARM_DESC(viafb_active_dev, "Specify active devices.");
2110 
2111 module_param(viafb_display_hardware_layout, int, S_IRUSR);
2112 MODULE_PARM_DESC(viafb_display_hardware_layout,
2113     "Display Hardware Layout (LCD Only, DVI Only...,etc)");
2114 
2115 module_param(viafb_second_size, int, S_IRUSR);
2116 MODULE_PARM_DESC(viafb_second_size,
2117     "Set secondary device memory size");
2118 
2119 module_param(viafb_dual_fb, int, S_IRUSR);
2120 MODULE_PARM_DESC(viafb_dual_fb,
2121     "Turn on/off flag of dual framebuffer devices.(Default = OFF)");
2122 
2123 module_param(viafb_platform_epia_dvi, int, S_IRUSR);
2124 MODULE_PARM_DESC(viafb_platform_epia_dvi,
2125     "Turn on/off flag of DVI devices on EPIA board.(Default = OFF)");
2126 
2127 module_param(viafb_device_lcd_dualedge, int, S_IRUSR);
2128 MODULE_PARM_DESC(viafb_device_lcd_dualedge,
2129     "Turn on/off flag of dual edge panel.(Default = OFF)");
2130 
2131 module_param(viafb_bus_width, int, S_IRUSR);
2132 MODULE_PARM_DESC(viafb_bus_width,
2133     "Set bus width of panel.(Default = 12)");
2134 
2135 module_param(viafb_lcd_mode, int, S_IRUSR);
2136 MODULE_PARM_DESC(viafb_lcd_mode,
2137     "Set Flat Panel mode(Default=OPENLDI)");
2138 
2139 module_param(viafb_lcd_port, charp, S_IRUSR);
2140 MODULE_PARM_DESC(viafb_lcd_port, "Specify LCD output port.");
2141 
2142 module_param(viafb_dvi_port, charp, S_IRUSR);
2143 MODULE_PARM_DESC(viafb_dvi_port, "Specify DVI output port.");
2144 
2145 MODULE_LICENSE("GPL");
2146 #endif