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