Back to home page

OSCL-LXR

 
 

    


0001 /* sunxvr2500.c: Sun 3DLABS XVR-2500 et al. fb driver for sparc64 systems
0002  *
0003  * License: GPL
0004  *
0005  * Copyright (C) 2007 David S. Miller (davem@davemloft.net)
0006  */
0007 
0008 #include <linux/kernel.h>
0009 #include <linux/fb.h>
0010 #include <linux/pci.h>
0011 #include <linux/init.h>
0012 #include <linux/of_device.h>
0013 
0014 #include <asm/io.h>
0015 
0016 struct s3d_info {
0017     struct fb_info      *info;
0018     struct pci_dev      *pdev;
0019 
0020     char __iomem        *fb_base;
0021     unsigned long       fb_base_phys;
0022 
0023     struct device_node  *of_node;
0024 
0025     unsigned int        width;
0026     unsigned int        height;
0027     unsigned int        depth;
0028     unsigned int        fb_size;
0029 
0030     u32         pseudo_palette[16];
0031 };
0032 
0033 static int s3d_get_props(struct s3d_info *sp)
0034 {
0035     sp->width = of_getintprop_default(sp->of_node, "width", 0);
0036     sp->height = of_getintprop_default(sp->of_node, "height", 0);
0037     sp->depth = of_getintprop_default(sp->of_node, "depth", 8);
0038 
0039     if (!sp->width || !sp->height) {
0040         printk(KERN_ERR "s3d: Critical properties missing for %s\n",
0041                pci_name(sp->pdev));
0042         return -EINVAL;
0043     }
0044 
0045     return 0;
0046 }
0047 
0048 static int s3d_setcolreg(unsigned regno,
0049              unsigned red, unsigned green, unsigned blue,
0050              unsigned transp, struct fb_info *info)
0051 {
0052     u32 value;
0053 
0054     if (regno < 16) {
0055         red >>= 8;
0056         green >>= 8;
0057         blue >>= 8;
0058 
0059         value = (blue << 24) | (green << 16) | (red << 8);
0060         ((u32 *)info->pseudo_palette)[regno] = value;
0061     }
0062 
0063     return 0;
0064 }
0065 
0066 static const struct fb_ops s3d_ops = {
0067     .owner          = THIS_MODULE,
0068     .fb_setcolreg       = s3d_setcolreg,
0069     .fb_fillrect        = cfb_fillrect,
0070     .fb_copyarea        = cfb_copyarea,
0071     .fb_imageblit       = cfb_imageblit,
0072 };
0073 
0074 static int s3d_set_fbinfo(struct s3d_info *sp)
0075 {
0076     struct fb_info *info = sp->info;
0077     struct fb_var_screeninfo *var = &info->var;
0078 
0079     info->flags = FBINFO_DEFAULT;
0080     info->fbops = &s3d_ops;
0081     info->screen_base = sp->fb_base;
0082     info->screen_size = sp->fb_size;
0083 
0084     info->pseudo_palette = sp->pseudo_palette;
0085 
0086     /* Fill fix common fields */
0087     strscpy(info->fix.id, "s3d", sizeof(info->fix.id));
0088         info->fix.smem_start = sp->fb_base_phys;
0089         info->fix.smem_len = sp->fb_size;
0090         info->fix.type = FB_TYPE_PACKED_PIXELS;
0091     if (sp->depth == 32 || sp->depth == 24)
0092         info->fix.visual = FB_VISUAL_TRUECOLOR;
0093     else
0094         info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
0095 
0096     var->xres = sp->width;
0097     var->yres = sp->height;
0098     var->xres_virtual = var->xres;
0099     var->yres_virtual = var->yres;
0100     var->bits_per_pixel = sp->depth;
0101 
0102     var->red.offset = 8;
0103     var->red.length = 8;
0104     var->green.offset = 16;
0105     var->green.length = 8;
0106     var->blue.offset = 24;
0107     var->blue.length = 8;
0108     var->transp.offset = 0;
0109     var->transp.length = 0;
0110 
0111     if (fb_alloc_cmap(&info->cmap, 256, 0)) {
0112         printk(KERN_ERR "s3d: Cannot allocate color map.\n");
0113         return -ENOMEM;
0114     }
0115 
0116         return 0;
0117 }
0118 
0119 static int s3d_pci_register(struct pci_dev *pdev,
0120                 const struct pci_device_id *ent)
0121 {
0122     struct fb_info *info;
0123     struct s3d_info *sp;
0124     int err;
0125 
0126     err = pci_enable_device(pdev);
0127     if (err < 0) {
0128         printk(KERN_ERR "s3d: Cannot enable PCI device %s\n",
0129                pci_name(pdev));
0130         goto err_out;
0131     }
0132 
0133     info = framebuffer_alloc(sizeof(struct s3d_info), &pdev->dev);
0134     if (!info) {
0135         err = -ENOMEM;
0136         goto err_disable;
0137     }
0138 
0139     sp = info->par;
0140     sp->info = info;
0141     sp->pdev = pdev;
0142     sp->of_node = pci_device_to_OF_node(pdev);
0143     if (!sp->of_node) {
0144         printk(KERN_ERR "s3d: Cannot find OF node of %s\n",
0145                pci_name(pdev));
0146         err = -ENODEV;
0147         goto err_release_fb;
0148     }
0149 
0150     sp->fb_base_phys = pci_resource_start (pdev, 1);
0151 
0152     err = pci_request_region(pdev, 1, "s3d framebuffer");
0153     if (err < 0) {
0154         printk("s3d: Cannot request region 1 for %s\n",
0155                pci_name(pdev));
0156         goto err_release_fb;
0157     }
0158 
0159     err = s3d_get_props(sp);
0160     if (err)
0161         goto err_release_pci;
0162 
0163     /* XXX 'linebytes' is often wrong, it is equal to the width
0164      * XXX with depth of 32 on my XVR-2500 which is clearly not
0165      * XXX right.  So we don't try to use it.
0166      */
0167     switch (sp->depth) {
0168     case 8:
0169         info->fix.line_length = sp->width;
0170         break;
0171     case 16:
0172         info->fix.line_length = sp->width * 2;
0173         break;
0174     case 24:
0175         info->fix.line_length = sp->width * 3;
0176         break;
0177     case 32:
0178         info->fix.line_length = sp->width * 4;
0179         break;
0180     }
0181     sp->fb_size = info->fix.line_length * sp->height;
0182 
0183     sp->fb_base = ioremap(sp->fb_base_phys, sp->fb_size);
0184     if (!sp->fb_base) {
0185         err = -ENOMEM;
0186         goto err_release_pci;
0187     }
0188 
0189     err = s3d_set_fbinfo(sp);
0190     if (err)
0191         goto err_unmap_fb;
0192 
0193     pci_set_drvdata(pdev, info);
0194 
0195     printk("s3d: Found device at %s\n", pci_name(pdev));
0196 
0197     err = register_framebuffer(info);
0198     if (err < 0) {
0199         printk(KERN_ERR "s3d: Could not register framebuffer %s\n",
0200                pci_name(pdev));
0201         goto err_unmap_fb;
0202     }
0203 
0204     return 0;
0205 
0206 err_unmap_fb:
0207     iounmap(sp->fb_base);
0208 
0209 err_release_pci:
0210     pci_release_region(pdev, 1);
0211 
0212 err_release_fb:
0213         framebuffer_release(info);
0214 
0215 err_disable:
0216     pci_disable_device(pdev);
0217 
0218 err_out:
0219     return err;
0220 }
0221 
0222 static const struct pci_device_id s3d_pci_table[] = {
0223     {   PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x002c),   },
0224     {   PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x002d),   },
0225     {   PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x002e),   },
0226     {   PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x002f),   },
0227     {   PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x0030),   },
0228     {   PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x0031),   },
0229     {   PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x0032),   },
0230     {   PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x0033),   },
0231     { 0, }
0232 };
0233 
0234 static struct pci_driver s3d_driver = {
0235     .driver = {
0236         .suppress_bind_attrs = true,
0237     },
0238     .name       = "s3d",
0239     .id_table   = s3d_pci_table,
0240     .probe      = s3d_pci_register,
0241 };
0242 
0243 static int __init s3d_init(void)
0244 {
0245     if (fb_get_options("s3d", NULL))
0246         return -ENODEV;
0247 
0248     return pci_register_driver(&s3d_driver);
0249 }
0250 device_initcall(s3d_init);