via-core.c 20 KB

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