42be3d955887856215103d0114596ca52cc4d86e
[pandora-kernel.git] / drivers / video / via / via-core.c
1 /*
2  * Copyright 1998-2009 VIA Technologies, Inc. All Rights Reserved.
3  * Copyright 2001-2008 S3 Graphics, Inc. All Rights Reserved.
4  * Copyright 2009 Jonathan Corbet <corbet@lwn.net>
5  */
6
7 /*
8  * Core code for the Via multifunction framebuffer device.
9  */
10 #include <linux/via-core.h>
11 #include <linux/via_i2c.h>
12 #include <linux/via-gpio.h>
13 #include "global.h"
14
15 #include <linux/module.h>
16 #include <linux/interrupt.h>
17 #include <linux/platform_device.h>
18 #include <linux/list.h>
19 #include <linux/pm.h>
20
21 /*
22  * The default port config.
23  */
24 static struct via_port_cfg adap_configs[] = {
25         [VIA_PORT_26]   = { VIA_PORT_I2C,  VIA_MODE_I2C, VIASR, 0x26 },
26         [VIA_PORT_31]   = { VIA_PORT_I2C,  VIA_MODE_I2C, VIASR, 0x31 },
27         [VIA_PORT_25]   = { VIA_PORT_GPIO, VIA_MODE_GPIO, VIASR, 0x25 },
28         [VIA_PORT_2C]   = { VIA_PORT_GPIO, VIA_MODE_I2C, VIASR, 0x2c },
29         [VIA_PORT_3D]   = { VIA_PORT_GPIO, VIA_MODE_GPIO, VIASR, 0x3d },
30         { 0, 0, 0, 0 }
31 };
32
33 /*
34  * We currently only support one viafb device (will there ever be
35  * more than one?), so just declare it globally here.
36  */
37 static struct viafb_dev global_dev;
38
39
40 /*
41  * Basic register access; spinlock required.
42  */
43 static inline void viafb_mmio_write(int reg, u32 v)
44 {
45         iowrite32(v, global_dev.engine_mmio + reg);
46 }
47
48 static inline int viafb_mmio_read(int reg)
49 {
50         return ioread32(global_dev.engine_mmio + reg);
51 }
52
53 /* ---------------------------------------------------------------------- */
54 /*
55  * Interrupt management.  We have a single IRQ line for a lot of
56  * different functions, so we need to share it.  The design here
57  * is that we don't want to reimplement the shared IRQ code here;
58  * we also want to avoid having contention for a single handler thread.
59  * So each subdev driver which needs interrupts just requests
60  * them directly from the kernel.  We just have what's needed for
61  * overall access to the interrupt control register.
62  */
63
64 /*
65  * Which interrupts are enabled now?
66  */
67 static u32 viafb_enabled_ints;
68
69 static void __devinit viafb_int_init(void)
70 {
71         viafb_enabled_ints = 0;
72
73         viafb_mmio_write(VDE_INTERRUPT, 0);
74 }
75
76 /*
77  * Allow subdevs to ask for specific interrupts to be enabled.  These
78  * functions must be called with reg_lock held
79  */
80 void viafb_irq_enable(u32 mask)
81 {
82         viafb_enabled_ints |= mask;
83         viafb_mmio_write(VDE_INTERRUPT, viafb_enabled_ints | VDE_I_ENABLE);
84 }
85 EXPORT_SYMBOL_GPL(viafb_irq_enable);
86
87 void viafb_irq_disable(u32 mask)
88 {
89         viafb_enabled_ints &= ~mask;
90         if (viafb_enabled_ints == 0)
91                 viafb_mmio_write(VDE_INTERRUPT, 0);  /* Disable entirely */
92         else
93                 viafb_mmio_write(VDE_INTERRUPT,
94                                 viafb_enabled_ints | VDE_I_ENABLE);
95 }
96 EXPORT_SYMBOL_GPL(viafb_irq_disable);
97
98 /* ---------------------------------------------------------------------- */
99 /*
100  * Access to the DMA engine.  This currently provides what the camera
101  * driver needs (i.e. outgoing only) but is easily expandable if need
102  * be.
103  */
104
105 /*
106  * There are four DMA channels in the vx855.  For now, we only
107  * use one of them, though.  Most of the time, the DMA channel
108  * will be idle, so we keep the IRQ handler unregistered except
109  * when some subsystem has indicated an interest.
110  */
111 static int viafb_dma_users;
112 static DECLARE_COMPLETION(viafb_dma_completion);
113 /*
114  * This mutex protects viafb_dma_users and our global interrupt
115  * registration state; it also serializes access to the DMA
116  * engine.
117  */
118 static DEFINE_MUTEX(viafb_dma_lock);
119
120 /*
121  * The VX855 DMA descriptor (used for s/g transfers) looks
122  * like this.
123  */
124 struct viafb_vx855_dma_descr {
125         u32     addr_low;       /* Low part of phys addr */
126         u32     addr_high;      /* High 12 bits of addr */
127         u32     fb_offset;      /* Offset into FB memory */
128         u32     seg_size;       /* Size, 16-byte units */
129         u32     tile_mode;      /* "tile mode" setting */
130         u32     next_desc_low;  /* Next descriptor addr */
131         u32     next_desc_high;
132         u32     pad;            /* Fill out to 64 bytes */
133 };
134
135 /*
136  * Flags added to the "next descriptor low" pointers
137  */
138 #define VIAFB_DMA_MAGIC         0x01  /* ??? Just has to be there */
139 #define VIAFB_DMA_FINAL_SEGMENT 0x02  /* Final segment */
140
141 /*
142  * The completion IRQ handler.
143  */
144 static irqreturn_t viafb_dma_irq(int irq, void *data)
145 {
146         int csr;
147         irqreturn_t ret = IRQ_NONE;
148
149         spin_lock(&global_dev.reg_lock);
150         csr = viafb_mmio_read(VDMA_CSR0);
151         if (csr & VDMA_C_DONE) {
152                 viafb_mmio_write(VDMA_CSR0, VDMA_C_DONE);
153                 complete(&viafb_dma_completion);
154                 ret = IRQ_HANDLED;
155         }
156         spin_unlock(&global_dev.reg_lock);
157         return ret;
158 }
159
160 /*
161  * Indicate a need for DMA functionality.
162  */
163 int viafb_request_dma(void)
164 {
165         int ret = 0;
166
167         /*
168          * Only VX855 is supported currently.
169          */
170         if (global_dev.chip_type != UNICHROME_VX855)
171                 return -ENODEV;
172         /*
173          * Note the new user and set up our interrupt handler
174          * if need be.
175          */
176         mutex_lock(&viafb_dma_lock);
177         viafb_dma_users++;
178         if (viafb_dma_users == 1) {
179                 ret = request_irq(global_dev.pdev->irq, viafb_dma_irq,
180                                 IRQF_SHARED, "via-dma", &viafb_dma_users);
181                 if (ret)
182                         viafb_dma_users--;
183                 else
184                         viafb_irq_enable(VDE_I_DMA0TDEN);
185         }
186         mutex_unlock(&viafb_dma_lock);
187         return ret;
188 }
189 EXPORT_SYMBOL_GPL(viafb_request_dma);
190
191 void viafb_release_dma(void)
192 {
193         mutex_lock(&viafb_dma_lock);
194         viafb_dma_users--;
195         if (viafb_dma_users == 0) {
196                 viafb_irq_disable(VDE_I_DMA0TDEN);
197                 free_irq(global_dev.pdev->irq, &viafb_dma_users);
198         }
199         mutex_unlock(&viafb_dma_lock);
200 }
201 EXPORT_SYMBOL_GPL(viafb_release_dma);
202
203
204 #if 0
205 /*
206  * Copy a single buffer from FB memory, synchronously.  This code works
207  * but is not currently used.
208  */
209 void viafb_dma_copy_out(unsigned int offset, dma_addr_t paddr, int len)
210 {
211         unsigned long flags;
212         int csr;
213
214         mutex_lock(&viafb_dma_lock);
215         init_completion(&viafb_dma_completion);
216         /*
217          * Program the controller.
218          */
219         spin_lock_irqsave(&global_dev.reg_lock, flags);
220         viafb_mmio_write(VDMA_CSR0, VDMA_C_ENABLE|VDMA_C_DONE);
221         /* Enable ints; must happen after CSR0 write! */
222         viafb_mmio_write(VDMA_MR0, VDMA_MR_TDIE);
223         viafb_mmio_write(VDMA_MARL0, (int) (paddr & 0xfffffff0));
224         viafb_mmio_write(VDMA_MARH0, (int) ((paddr >> 28) & 0xfff));
225         /* Data sheet suggests DAR0 should be <<4, but it lies */
226         viafb_mmio_write(VDMA_DAR0, offset);
227         viafb_mmio_write(VDMA_DQWCR0, len >> 4);
228         viafb_mmio_write(VDMA_TMR0, 0);
229         viafb_mmio_write(VDMA_DPRL0, 0);
230         viafb_mmio_write(VDMA_DPRH0, 0);
231         viafb_mmio_write(VDMA_PMR0, 0);
232         csr = viafb_mmio_read(VDMA_CSR0);
233         viafb_mmio_write(VDMA_CSR0, VDMA_C_ENABLE|VDMA_C_START);
234         spin_unlock_irqrestore(&global_dev.reg_lock, flags);
235         /*
236          * Now we just wait until the interrupt handler says
237          * we're done.
238          */
239         wait_for_completion_interruptible(&viafb_dma_completion);
240         viafb_mmio_write(VDMA_MR0, 0); /* Reset int enable */
241         mutex_unlock(&viafb_dma_lock);
242 }
243 EXPORT_SYMBOL_GPL(viafb_dma_copy_out);
244 #endif
245
246 /*
247  * Do a scatter/gather DMA copy from FB memory.  You must have done
248  * a successful call to viafb_request_dma() first.
249  */
250 int viafb_dma_copy_out_sg(unsigned int offset, struct scatterlist *sg, int nsg)
251 {
252         struct viafb_vx855_dma_descr *descr;
253         void *descrpages;
254         dma_addr_t descr_handle;
255         unsigned long flags;
256         int i;
257         struct scatterlist *sgentry;
258         dma_addr_t nextdesc;
259
260         /*
261          * Get a place to put the descriptors.
262          */
263         descrpages = dma_alloc_coherent(&global_dev.pdev->dev,
264                         nsg*sizeof(struct viafb_vx855_dma_descr),
265                         &descr_handle, GFP_KERNEL);
266         if (descrpages == NULL) {
267                 dev_err(&global_dev.pdev->dev, "Unable to get descr page.\n");
268                 return -ENOMEM;
269         }
270         mutex_lock(&viafb_dma_lock);
271         /*
272          * Fill them in.
273          */
274         descr = descrpages;
275         nextdesc = descr_handle + sizeof(struct viafb_vx855_dma_descr);
276         for_each_sg(sg, sgentry, nsg, i) {
277                 dma_addr_t paddr = sg_dma_address(sgentry);
278                 descr->addr_low = paddr & 0xfffffff0;
279                 descr->addr_high = ((u64) paddr >> 32) & 0x0fff;
280                 descr->fb_offset = offset;
281                 descr->seg_size = sg_dma_len(sgentry) >> 4;
282                 descr->tile_mode = 0;
283                 descr->next_desc_low = (nextdesc&0xfffffff0) | VIAFB_DMA_MAGIC;
284                 descr->next_desc_high = ((u64) nextdesc >> 32) & 0x0fff;
285                 descr->pad = 0xffffffff;  /* VIA driver does this */
286                 offset += sg_dma_len(sgentry);
287                 nextdesc += sizeof(struct viafb_vx855_dma_descr);
288                 descr++;
289         }
290         descr[-1].next_desc_low = VIAFB_DMA_FINAL_SEGMENT|VIAFB_DMA_MAGIC;
291         /*
292          * Program the engine.
293          */
294         spin_lock_irqsave(&global_dev.reg_lock, flags);
295         init_completion(&viafb_dma_completion);
296         viafb_mmio_write(VDMA_DQWCR0, 0);
297         viafb_mmio_write(VDMA_CSR0, VDMA_C_ENABLE|VDMA_C_DONE);
298         viafb_mmio_write(VDMA_MR0, VDMA_MR_TDIE | VDMA_MR_CHAIN);
299         viafb_mmio_write(VDMA_DPRL0, descr_handle | VIAFB_DMA_MAGIC);
300         viafb_mmio_write(VDMA_DPRH0,
301                         (((u64)descr_handle >> 32) & 0x0fff) | 0xf0000);
302         (void) viafb_mmio_read(VDMA_CSR0);
303         viafb_mmio_write(VDMA_CSR0, VDMA_C_ENABLE|VDMA_C_START);
304         spin_unlock_irqrestore(&global_dev.reg_lock, flags);
305         /*
306          * Now we just wait until the interrupt handler says
307          * we're done.  Except that, actually, we need to wait a little
308          * longer: the interrupts seem to jump the gun a little and we
309          * get corrupted frames sometimes.
310          */
311         wait_for_completion_timeout(&viafb_dma_completion, 1);
312         msleep(1);
313         if ((viafb_mmio_read(VDMA_CSR0)&VDMA_C_DONE) == 0)
314                 printk(KERN_ERR "VIA DMA timeout!\n");
315         /*
316          * Clean up and we're done.
317          */
318         viafb_mmio_write(VDMA_CSR0, VDMA_C_DONE);
319         viafb_mmio_write(VDMA_MR0, 0); /* Reset int enable */
320         mutex_unlock(&viafb_dma_lock);
321         dma_free_coherent(&global_dev.pdev->dev,
322                         nsg*sizeof(struct viafb_vx855_dma_descr), descrpages,
323                         descr_handle);
324         return 0;
325 }
326 EXPORT_SYMBOL_GPL(viafb_dma_copy_out_sg);
327
328
329 /* ---------------------------------------------------------------------- */
330 /*
331  * Figure out how big our framebuffer memory is.  Kind of ugly,
332  * but evidently we can't trust the information found in the
333  * fbdev configuration area.
334  */
335 static u16 via_function3[] = {
336         CLE266_FUNCTION3, KM400_FUNCTION3, CN400_FUNCTION3, CN700_FUNCTION3,
337         CX700_FUNCTION3, KM800_FUNCTION3, KM890_FUNCTION3, P4M890_FUNCTION3,
338         P4M900_FUNCTION3, VX800_FUNCTION3, VX855_FUNCTION3, VX900_FUNCTION3,
339 };
340
341 /* Get the BIOS-configured framebuffer size from PCI configuration space
342  * of function 3 in the respective chipset */
343 static int viafb_get_fb_size_from_pci(int chip_type)
344 {
345         int i;
346         u8 offset = 0;
347         u32 FBSize;
348         u32 VideoMemSize;
349
350         /* search for the "FUNCTION3" device in this chipset */
351         for (i = 0; i < ARRAY_SIZE(via_function3); i++) {
352                 struct pci_dev *pdev;
353
354                 pdev = pci_get_device(PCI_VENDOR_ID_VIA, via_function3[i],
355                                       NULL);
356                 if (!pdev)
357                         continue;
358
359                 DEBUG_MSG(KERN_INFO "Device ID = %x\n", pdev->device);
360
361                 switch (pdev->device) {
362                 case CLE266_FUNCTION3:
363                 case KM400_FUNCTION3:
364                         offset = 0xE0;
365                         break;
366                 case CN400_FUNCTION3:
367                 case CN700_FUNCTION3:
368                 case CX700_FUNCTION3:
369                 case KM800_FUNCTION3:
370                 case KM890_FUNCTION3:
371                 case P4M890_FUNCTION3:
372                 case P4M900_FUNCTION3:
373                 case VX800_FUNCTION3:
374                 case VX855_FUNCTION3:
375                 case VX900_FUNCTION3:
376                 /*case CN750_FUNCTION3: */
377                         offset = 0xA0;
378                         break;
379                 }
380
381                 if (!offset)
382                         break;
383
384                 pci_read_config_dword(pdev, offset, &FBSize);
385                 pci_dev_put(pdev);
386         }
387
388         if (!offset) {
389                 printk(KERN_ERR "cannot determine framebuffer size\n");
390                 return -EIO;
391         }
392
393         FBSize = FBSize & 0x00007000;
394         DEBUG_MSG(KERN_INFO "FB Size = %x\n", FBSize);
395
396         if (chip_type < UNICHROME_CX700) {
397                 switch (FBSize) {
398                 case 0x00004000:
399                         VideoMemSize = (16 << 20);      /*16M */
400                         break;
401
402                 case 0x00005000:
403                         VideoMemSize = (32 << 20);      /*32M */
404                         break;
405
406                 case 0x00006000:
407                         VideoMemSize = (64 << 20);      /*64M */
408                         break;
409
410                 default:
411                         VideoMemSize = (32 << 20);      /*32M */
412                         break;
413                 }
414         } else {
415                 switch (FBSize) {
416                 case 0x00001000:
417                         VideoMemSize = (8 << 20);       /*8M */
418                         break;
419
420                 case 0x00002000:
421                         VideoMemSize = (16 << 20);      /*16M */
422                         break;
423
424                 case 0x00003000:
425                         VideoMemSize = (32 << 20);      /*32M */
426                         break;
427
428                 case 0x00004000:
429                         VideoMemSize = (64 << 20);      /*64M */
430                         break;
431
432                 case 0x00005000:
433                         VideoMemSize = (128 << 20);     /*128M */
434                         break;
435
436                 case 0x00006000:
437                         VideoMemSize = (256 << 20);     /*256M */
438                         break;
439
440                 case 0x00007000:        /* Only on VX855/875 */
441                         VideoMemSize = (512 << 20);     /*512M */
442                         break;
443
444                 default:
445                         VideoMemSize = (32 << 20);      /*32M */
446                         break;
447                 }
448         }
449
450         return VideoMemSize;
451 }
452
453
454 /*
455  * Figure out and map our MMIO regions.
456  */
457 static int __devinit via_pci_setup_mmio(struct viafb_dev *vdev)
458 {
459         int ret;
460         /*
461          * Hook up to the device registers.  Note that we soldier
462          * on if it fails; the framebuffer can operate (without
463          * acceleration) without this region.
464          */
465         vdev->engine_start = pci_resource_start(vdev->pdev, 1);
466         vdev->engine_len = pci_resource_len(vdev->pdev, 1);
467         vdev->engine_mmio = ioremap_nocache(vdev->engine_start,
468                         vdev->engine_len);
469         if (vdev->engine_mmio == NULL)
470                 dev_err(&vdev->pdev->dev,
471                                 "Unable to map engine MMIO; operation will be "
472                                 "slow and crippled.\n");
473         /*
474          * Map in framebuffer memory.  For now, failure here is
475          * fatal.  Unfortunately, in the absence of significant
476          * vmalloc space, failure here is also entirely plausible.
477          * Eventually we want to move away from mapping this
478          * entire region.
479          */
480         if (vdev->chip_type == UNICHROME_VX900)
481                 vdev->fbmem_start = pci_resource_start(vdev->pdev, 2);
482         else
483                 vdev->fbmem_start = pci_resource_start(vdev->pdev, 0);
484         ret = vdev->fbmem_len = viafb_get_fb_size_from_pci(vdev->chip_type);
485         if (ret < 0)
486                 goto out_unmap;
487         vdev->fbmem = ioremap_nocache(vdev->fbmem_start, vdev->fbmem_len);
488         if (vdev->fbmem == NULL) {
489                 ret = -ENOMEM;
490                 goto out_unmap;
491         }
492         return 0;
493 out_unmap:
494         iounmap(vdev->engine_mmio);
495         return ret;
496 }
497
498 static void via_pci_teardown_mmio(struct viafb_dev *vdev)
499 {
500         iounmap(vdev->fbmem);
501         iounmap(vdev->engine_mmio);
502 }
503
504 /*
505  * Create our subsidiary devices.
506  */
507 static struct viafb_subdev_info {
508         char *name;
509         struct platform_device *platdev;
510 } viafb_subdevs[] = {
511         {
512                 .name = "viafb-gpio",
513         },
514         {
515                 .name = "viafb-i2c",
516         }
517 };
518 #define N_SUBDEVS ARRAY_SIZE(viafb_subdevs)
519
520 static int __devinit via_create_subdev(struct viafb_dev *vdev,
521                 struct viafb_subdev_info *info)
522 {
523         int ret;
524
525         info->platdev = platform_device_alloc(info->name, -1);
526         if (!info->platdev) {
527                 dev_err(&vdev->pdev->dev, "Unable to allocate pdev %s\n",
528                         info->name);
529                 return -ENOMEM;
530         }
531         info->platdev->dev.parent = &vdev->pdev->dev;
532         info->platdev->dev.platform_data = vdev;
533         ret = platform_device_add(info->platdev);
534         if (ret) {
535                 dev_err(&vdev->pdev->dev, "Unable to add pdev %s\n",
536                                 info->name);
537                 platform_device_put(info->platdev);
538                 info->platdev = NULL;
539         }
540         return ret;
541 }
542
543 static int __devinit via_setup_subdevs(struct viafb_dev *vdev)
544 {
545         int i;
546
547         /*
548          * Ignore return values.  Even if some of the devices
549          * fail to be created, we'll still be able to use some
550          * of the rest.
551          */
552         for (i = 0; i < N_SUBDEVS; i++)
553                 via_create_subdev(vdev, viafb_subdevs + i);
554         return 0;
555 }
556
557 static void via_teardown_subdevs(void)
558 {
559         int i;
560
561         for (i = 0; i < N_SUBDEVS; i++)
562                 if (viafb_subdevs[i].platdev) {
563                         viafb_subdevs[i].platdev->dev.platform_data = NULL;
564                         platform_device_unregister(viafb_subdevs[i].platdev);
565                 }
566 }
567
568 /*
569  * Power management functions
570  */
571 #ifdef CONFIG_PM
572 static LIST_HEAD(viafb_pm_hooks);
573 static DEFINE_MUTEX(viafb_pm_hooks_lock);
574
575 void viafb_pm_register(struct viafb_pm_hooks *hooks)
576 {
577         INIT_LIST_HEAD(&hooks->list);
578
579         mutex_lock(&viafb_pm_hooks_lock);
580         list_add_tail(&hooks->list, &viafb_pm_hooks);
581         mutex_unlock(&viafb_pm_hooks_lock);
582 }
583 EXPORT_SYMBOL_GPL(viafb_pm_register);
584
585 void viafb_pm_unregister(struct viafb_pm_hooks *hooks)
586 {
587         mutex_lock(&viafb_pm_hooks_lock);
588         list_del(&hooks->list);
589         mutex_unlock(&viafb_pm_hooks_lock);
590 }
591 EXPORT_SYMBOL_GPL(viafb_pm_unregister);
592
593 static int via_suspend(struct pci_dev *pdev, pm_message_t state)
594 {
595         struct viafb_pm_hooks *hooks;
596
597         if (state.event != PM_EVENT_SUSPEND)
598                 return 0;
599         /*
600          * "I've occasionally hit a few drivers that caused suspend
601          * failures, and each and every time it was a driver bug, and
602          * the right thing to do was to just ignore the error and suspend
603          * anyway - returning an error code and trying to undo the suspend
604          * is not what anybody ever really wants, even if our model
605          *_allows_ for it."
606          * -- Linus Torvalds, Dec. 7, 2009
607          */
608         mutex_lock(&viafb_pm_hooks_lock);
609         list_for_each_entry_reverse(hooks, &viafb_pm_hooks, list)
610                 hooks->suspend(hooks->private);
611         mutex_unlock(&viafb_pm_hooks_lock);
612
613         pci_save_state(pdev);
614         pci_disable_device(pdev);
615         pci_set_power_state(pdev, pci_choose_state(pdev, state));
616         return 0;
617 }
618
619 static int via_resume(struct pci_dev *pdev)
620 {
621         struct viafb_pm_hooks *hooks;
622
623         /* Get the bus side powered up */
624         pci_set_power_state(pdev, PCI_D0);
625         pci_restore_state(pdev);
626         if (pci_enable_device(pdev))
627                 return 0;
628
629         pci_set_master(pdev);
630
631         /* Now bring back any subdevs */
632         mutex_lock(&viafb_pm_hooks_lock);
633         list_for_each_entry(hooks, &viafb_pm_hooks, list)
634                 hooks->resume(hooks->private);
635         mutex_unlock(&viafb_pm_hooks_lock);
636
637         return 0;
638 }
639 #endif /* CONFIG_PM */
640
641 static int __devinit via_pci_probe(struct pci_dev *pdev,
642                 const struct pci_device_id *ent)
643 {
644         int ret;
645
646         ret = pci_enable_device(pdev);
647         if (ret)
648                 return ret;
649
650         /*
651          * Global device initialization.
652          */
653         memset(&global_dev, 0, sizeof(global_dev));
654         global_dev.pdev = pdev;
655         global_dev.chip_type = ent->driver_data;
656         global_dev.port_cfg = adap_configs;
657         spin_lock_init(&global_dev.reg_lock);
658         ret = via_pci_setup_mmio(&global_dev);
659         if (ret)
660                 goto out_disable;
661         /*
662          * Set up interrupts and create our subdevices.  Continue even if
663          * some things fail.
664          */
665         viafb_int_init();
666         via_setup_subdevs(&global_dev);
667         /*
668          * Set up the framebuffer device
669          */
670         ret = via_fb_pci_probe(&global_dev);
671         if (ret)
672                 goto out_subdevs;
673         return 0;
674
675 out_subdevs:
676         via_teardown_subdevs();
677         via_pci_teardown_mmio(&global_dev);
678 out_disable:
679         pci_disable_device(pdev);
680         return ret;
681 }
682
683 static void __devexit via_pci_remove(struct pci_dev *pdev)
684 {
685         via_teardown_subdevs();
686         via_fb_pci_remove(pdev);
687         via_pci_teardown_mmio(&global_dev);
688         pci_disable_device(pdev);
689 }
690
691
692 static struct pci_device_id via_pci_table[] __devinitdata = {
693         { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_CLE266_DID),
694           .driver_data = UNICHROME_CLE266 },
695         { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_K400_DID),
696           .driver_data = UNICHROME_K400 },
697         { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_K800_DID),
698           .driver_data = UNICHROME_K800 },
699         { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_PM800_DID),
700           .driver_data = UNICHROME_PM800 },
701         { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_CN700_DID),
702           .driver_data = UNICHROME_CN700 },
703         { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_CX700_DID),
704           .driver_data = UNICHROME_CX700 },
705         { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_CN750_DID),
706           .driver_data = UNICHROME_CN750 },
707         { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_K8M890_DID),
708           .driver_data = UNICHROME_K8M890 },
709         { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_P4M890_DID),
710           .driver_data = UNICHROME_P4M890 },
711         { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_P4M900_DID),
712           .driver_data = UNICHROME_P4M900 },
713         { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_VX800_DID),
714           .driver_data = UNICHROME_VX800 },
715         { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_VX855_DID),
716           .driver_data = UNICHROME_VX855 },
717         { PCI_DEVICE(PCI_VENDOR_ID_VIA, UNICHROME_VX900_DID),
718           .driver_data = UNICHROME_VX900 },
719         { }
720 };
721 MODULE_DEVICE_TABLE(pci, via_pci_table);
722
723 static struct pci_driver via_driver = {
724         .name           = "viafb",
725         .id_table       = via_pci_table,
726         .probe          = via_pci_probe,
727         .remove         = __devexit_p(via_pci_remove),
728 #ifdef CONFIG_PM
729         .suspend        = via_suspend,
730         .resume         = via_resume,
731 #endif
732 };
733
734 static int __init via_core_init(void)
735 {
736         int ret;
737
738         ret = viafb_init();
739         if (ret)
740                 return ret;
741         viafb_i2c_init();
742         viafb_gpio_init();
743         return pci_register_driver(&via_driver);
744 }
745
746 static void __exit via_core_exit(void)
747 {
748         pci_unregister_driver(&via_driver);
749         viafb_gpio_exit();
750         viafb_i2c_exit();
751         viafb_exit();
752 }
753
754 module_init(via_core_init);
755 module_exit(via_core_exit);