via-core.c 19 KB

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