Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/drzeus/mmc
[pandora-kernel.git] / drivers / video / sunxvr2500.c
1 /* s3d.c: Sun 3DLABS XVR-2500 et al. driver for sparc64 systems
2  *
3  * Copyright (C) 2007 David S. Miller (davem@davemloft.net)
4  */
5
6 #include <linux/module.h>
7 #include <linux/kernel.h>
8 #include <linux/slab.h>
9 #include <linux/fb.h>
10 #include <linux/pci.h>
11 #include <linux/init.h>
12 #include <linux/of_device.h>
13
14 #include <asm/io.h>
15
16 struct s3d_info {
17         struct fb_info          *info;
18         struct pci_dev          *pdev;
19
20         char __iomem            *fb_base;
21         unsigned long           fb_base_phys;
22
23         struct device_node      *of_node;
24
25         unsigned int            width;
26         unsigned int            height;
27         unsigned int            depth;
28         unsigned int            fb_size;
29
30         u32                     pseudo_palette[16];
31 };
32
33 static int __devinit s3d_get_props(struct s3d_info *sp)
34 {
35         sp->width = of_getintprop_default(sp->of_node, "width", 0);
36         sp->height = of_getintprop_default(sp->of_node, "height", 0);
37         sp->depth = of_getintprop_default(sp->of_node, "depth", 8);
38
39         if (!sp->width || !sp->height) {
40                 printk(KERN_ERR "s3d: Critical properties missing for %s\n",
41                        pci_name(sp->pdev));
42                 return -EINVAL;
43         }
44
45         return 0;
46 }
47
48 static int s3d_setcolreg(unsigned regno,
49                          unsigned red, unsigned green, unsigned blue,
50                          unsigned transp, struct fb_info *info)
51 {
52         u32 value;
53
54         if (regno < 16) {
55                 red >>= 8;
56                 green >>= 8;
57                 blue >>= 8;
58
59                 value = (blue << 24) | (green << 16) | (red << 8);
60                 ((u32 *)info->pseudo_palette)[regno] = value;
61         }
62
63         return 0;
64 }
65
66 static struct fb_ops s3d_ops = {
67         .owner                  = THIS_MODULE,
68         .fb_setcolreg           = s3d_setcolreg,
69         .fb_fillrect            = cfb_fillrect,
70         .fb_copyarea            = cfb_copyarea,
71         .fb_imageblit           = cfb_imageblit,
72 };
73
74 static int __devinit s3d_set_fbinfo(struct s3d_info *sp)
75 {
76         struct fb_info *info = sp->info;
77         struct fb_var_screeninfo *var = &info->var;
78
79         info->flags = FBINFO_DEFAULT;
80         info->fbops = &s3d_ops;
81         info->screen_base = sp->fb_base;
82         info->screen_size = sp->fb_size;
83
84         info->pseudo_palette = sp->pseudo_palette;
85
86         /* Fill fix common fields */
87         strlcpy(info->fix.id, "s3d", sizeof(info->fix.id));
88         info->fix.smem_start = sp->fb_base_phys;
89         info->fix.smem_len = sp->fb_size;
90         info->fix.type = FB_TYPE_PACKED_PIXELS;
91         if (sp->depth == 32 || sp->depth == 24)
92                 info->fix.visual = FB_VISUAL_TRUECOLOR;
93         else
94                 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
95
96         var->xres = sp->width;
97         var->yres = sp->height;
98         var->xres_virtual = var->xres;
99         var->yres_virtual = var->yres;
100         var->bits_per_pixel = sp->depth;
101
102         var->red.offset = 8;
103         var->red.length = 8;
104         var->green.offset = 16;
105         var->green.length = 8;
106         var->blue.offset = 24;
107         var->blue.length = 8;
108         var->transp.offset = 0;
109         var->transp.length = 0;
110
111         if (fb_alloc_cmap(&info->cmap, 256, 0)) {
112                 printk(KERN_ERR "s3d: Cannot allocate color map.\n");
113                 return -ENOMEM;
114         }
115
116         return 0;
117 }
118
119 static int __devinit s3d_pci_register(struct pci_dev *pdev,
120                                       const struct pci_device_id *ent)
121 {
122         struct fb_info *info;
123         struct s3d_info *sp;
124         int err;
125
126         err = pci_enable_device(pdev);
127         if (err < 0) {
128                 printk(KERN_ERR "s3d: Cannot enable PCI device %s\n",
129                        pci_name(pdev));
130                 goto err_out;
131         }
132
133         info = framebuffer_alloc(sizeof(struct s3d_info), &pdev->dev);
134         if (!info) {
135                 printk(KERN_ERR "s3d: Cannot allocate fb_info\n");
136                 err = -ENOMEM;
137                 goto err_disable;
138         }
139
140         sp = info->par;
141         sp->info = info;
142         sp->pdev = pdev;
143         sp->of_node = pci_device_to_OF_node(pdev);
144         if (!sp->of_node) {
145                 printk(KERN_ERR "s3d: Cannot find OF node of %s\n",
146                        pci_name(pdev));
147                 err = -ENODEV;
148                 goto err_release_fb;
149         }
150
151         sp->fb_base_phys = pci_resource_start (pdev, 1);
152
153         err = pci_request_region(pdev, 1, "s3d framebuffer");
154         if (err < 0) {
155                 printk("s3d: Cannot request region 1 for %s\n",
156                        pci_name(pdev));
157                 goto err_release_fb;
158         }
159
160         err = s3d_get_props(sp);
161         if (err)
162                 goto err_release_pci;
163
164         /* XXX 'linebytes' is often wrong, it is equal to the width
165          * XXX with depth of 32 on my XVR-2500 which is clearly not
166          * XXX right.  So we don't try to use it.
167          */
168         switch (sp->depth) {
169         case 8:
170                 info->fix.line_length = sp->width;
171                 break;
172         case 16:
173                 info->fix.line_length = sp->width * 2;
174                 break;
175         case 24:
176                 info->fix.line_length = sp->width * 3;
177                 break;
178         case 32:
179                 info->fix.line_length = sp->width * 4;
180                 break;
181         }
182         sp->fb_size = info->fix.line_length * sp->height;
183
184         sp->fb_base = ioremap(sp->fb_base_phys, sp->fb_size);
185         if (!sp->fb_base)
186                 goto err_release_pci;
187
188         err = s3d_set_fbinfo(sp);
189         if (err)
190                 goto err_unmap_fb;
191
192         pci_set_drvdata(pdev, info);
193
194         printk("s3d: Found device at %s\n", pci_name(pdev));
195
196         err = register_framebuffer(info);
197         if (err < 0) {
198                 printk(KERN_ERR "s3d: Could not register framebuffer %s\n",
199                        pci_name(pdev));
200                 goto err_unmap_fb;
201         }
202
203         return 0;
204
205 err_unmap_fb:
206         iounmap(sp->fb_base);
207
208 err_release_pci:
209         pci_release_region(pdev, 1);
210
211 err_release_fb:
212         framebuffer_release(info);
213
214 err_disable:
215         pci_disable_device(pdev);
216
217 err_out:
218         return err;
219 }
220
221 static void __devexit s3d_pci_unregister(struct pci_dev *pdev)
222 {
223         struct fb_info *info = pci_get_drvdata(pdev);
224         struct s3d_info *sp = info->par;
225
226         unregister_framebuffer(info);
227
228         iounmap(sp->fb_base);
229
230         pci_release_region(pdev, 1);
231
232         framebuffer_release(info);
233
234         pci_disable_device(pdev);
235 }
236
237 static struct pci_device_id s3d_pci_table[] = {
238         {       PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x002c),       },
239         {       PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x002d),       },
240         {       PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x002e),       },
241         {       PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x002f),       },
242         {       PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x0030),       },
243         {       PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x0031),       },
244         {       PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x0032),       },
245         {       PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x0033),       },
246         { 0, }
247 };
248
249 static struct pci_driver s3d_driver = {
250         .name           = "s3d",
251         .id_table       = s3d_pci_table,
252         .probe          = s3d_pci_register,
253         .remove         = __devexit_p(s3d_pci_unregister),
254 };
255
256 static int __init s3d_init(void)
257 {
258         if (fb_get_options("s3d", NULL))
259                 return -ENODEV;
260
261         return pci_register_driver(&s3d_driver);
262 }
263
264 static void __exit s3d_exit(void)
265 {
266         pci_unregister_driver(&s3d_driver);
267 }
268
269 module_init(s3d_init);
270 module_exit(s3d_exit);
271
272 MODULE_DESCRIPTION("framebuffer driver for Sun XVR-2500 graphics");
273 MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
274 MODULE_VERSION("1.0");
275 MODULE_LICENSE("GPL");