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