via-core.c 19 KB

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