uio_pdrv_genirq.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * drivers/uio/uio_pdrv_genirq.c
  3. *
  4. * Userspace I/O platform driver with generic IRQ handling code.
  5. *
  6. * Copyright (C) 2008 Magnus Damm
  7. *
  8. * Based on uio_pdrv.c by Uwe Kleine-Koenig,
  9. * Copyright (C) 2008 by Digi International Inc.
  10. * All rights reserved.
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License version 2 as published by
  14. * the Free Software Foundation.
  15. */
  16. #include <linux/platform_device.h>
  17. #include <linux/uio_driver.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/bitops.h>
  20. #include <linux/module.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/stringify.h>
  23. #include <linux/pm_runtime.h>
  24. #include <linux/slab.h>
  25. #include <linux/of.h>
  26. #include <linux/of_platform.h>
  27. #include <linux/of_address.h>
  28. #define DRIVER_NAME "uio_pdrv_genirq"
  29. struct uio_pdrv_genirq_platdata {
  30. struct uio_info *uioinfo;
  31. spinlock_t lock;
  32. unsigned long flags;
  33. struct platform_device *pdev;
  34. };
  35. static int uio_pdrv_genirq_open(struct uio_info *info, struct inode *inode)
  36. {
  37. struct uio_pdrv_genirq_platdata *priv = info->priv;
  38. /* Wait until the Runtime PM code has woken up the device */
  39. pm_runtime_get_sync(&priv->pdev->dev);
  40. return 0;
  41. }
  42. static int uio_pdrv_genirq_release(struct uio_info *info, struct inode *inode)
  43. {
  44. struct uio_pdrv_genirq_platdata *priv = info->priv;
  45. /* Tell the Runtime PM code that the device has become idle */
  46. pm_runtime_put_sync(&priv->pdev->dev);
  47. return 0;
  48. }
  49. static irqreturn_t uio_pdrv_genirq_handler(int irq, struct uio_info *dev_info)
  50. {
  51. struct uio_pdrv_genirq_platdata *priv = dev_info->priv;
  52. /* Just disable the interrupt in the interrupt controller, and
  53. * remember the state so we can allow user space to enable it later.
  54. */
  55. if (!test_and_set_bit(0, &priv->flags))
  56. disable_irq_nosync(irq);
  57. return IRQ_HANDLED;
  58. }
  59. static int uio_pdrv_genirq_irqcontrol(struct uio_info *dev_info, s32 irq_on)
  60. {
  61. struct uio_pdrv_genirq_platdata *priv = dev_info->priv;
  62. unsigned long flags;
  63. /* Allow user space to enable and disable the interrupt
  64. * in the interrupt controller, but keep track of the
  65. * state to prevent per-irq depth damage.
  66. *
  67. * Serialize this operation to support multiple tasks.
  68. */
  69. spin_lock_irqsave(&priv->lock, flags);
  70. if (irq_on) {
  71. if (test_and_clear_bit(0, &priv->flags))
  72. enable_irq(dev_info->irq);
  73. } else {
  74. if (!test_and_set_bit(0, &priv->flags))
  75. disable_irq(dev_info->irq);
  76. }
  77. spin_unlock_irqrestore(&priv->lock, flags);
  78. return 0;
  79. }
  80. static int uio_pdrv_genirq_probe(struct platform_device *pdev)
  81. {
  82. struct uio_info *uioinfo = pdev->dev.platform_data;
  83. struct uio_pdrv_genirq_platdata *priv;
  84. struct uio_mem *uiomem;
  85. int ret = -EINVAL;
  86. int i;
  87. if (!uioinfo) {
  88. int irq;
  89. /* alloc uioinfo for one device */
  90. uioinfo = kzalloc(sizeof(*uioinfo), GFP_KERNEL);
  91. if (!uioinfo) {
  92. ret = -ENOMEM;
  93. dev_err(&pdev->dev, "unable to kmalloc\n");
  94. goto bad2;
  95. }
  96. uioinfo->name = pdev->dev.of_node->name;
  97. uioinfo->version = "devicetree";
  98. /* Multiple IRQs are not supported */
  99. irq = platform_get_irq(pdev, 0);
  100. if (irq == -ENXIO)
  101. uioinfo->irq = UIO_IRQ_NONE;
  102. else
  103. uioinfo->irq = irq;
  104. }
  105. if (!uioinfo || !uioinfo->name || !uioinfo->version) {
  106. dev_err(&pdev->dev, "missing platform_data\n");
  107. goto bad0;
  108. }
  109. if (uioinfo->handler || uioinfo->irqcontrol ||
  110. uioinfo->irq_flags & IRQF_SHARED) {
  111. dev_err(&pdev->dev, "interrupt configuration error\n");
  112. goto bad0;
  113. }
  114. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  115. if (!priv) {
  116. ret = -ENOMEM;
  117. dev_err(&pdev->dev, "unable to kmalloc\n");
  118. goto bad0;
  119. }
  120. priv->uioinfo = uioinfo;
  121. spin_lock_init(&priv->lock);
  122. priv->flags = 0; /* interrupt is enabled to begin with */
  123. priv->pdev = pdev;
  124. if (!uioinfo->irq) {
  125. ret = platform_get_irq(pdev, 0);
  126. if (ret < 0) {
  127. dev_err(&pdev->dev, "failed to get IRQ\n");
  128. goto bad0;
  129. }
  130. uioinfo->irq = ret;
  131. }
  132. uiomem = &uioinfo->mem[0];
  133. for (i = 0; i < pdev->num_resources; ++i) {
  134. struct resource *r = &pdev->resource[i];
  135. if (r->flags != IORESOURCE_MEM)
  136. continue;
  137. if (uiomem >= &uioinfo->mem[MAX_UIO_MAPS]) {
  138. dev_warn(&pdev->dev, "device has more than "
  139. __stringify(MAX_UIO_MAPS)
  140. " I/O memory resources.\n");
  141. break;
  142. }
  143. uiomem->memtype = UIO_MEM_PHYS;
  144. uiomem->addr = r->start;
  145. uiomem->size = resource_size(r);
  146. ++uiomem;
  147. }
  148. while (uiomem < &uioinfo->mem[MAX_UIO_MAPS]) {
  149. uiomem->size = 0;
  150. ++uiomem;
  151. }
  152. /* This driver requires no hardware specific kernel code to handle
  153. * interrupts. Instead, the interrupt handler simply disables the
  154. * interrupt in the interrupt controller. User space is responsible
  155. * for performing hardware specific acknowledge and re-enabling of
  156. * the interrupt in the interrupt controller.
  157. *
  158. * Interrupt sharing is not supported.
  159. */
  160. uioinfo->handler = uio_pdrv_genirq_handler;
  161. uioinfo->irqcontrol = uio_pdrv_genirq_irqcontrol;
  162. uioinfo->open = uio_pdrv_genirq_open;
  163. uioinfo->release = uio_pdrv_genirq_release;
  164. uioinfo->priv = priv;
  165. /* Enable Runtime PM for this device:
  166. * The device starts in suspended state to allow the hardware to be
  167. * turned off by default. The Runtime PM bus code should power on the
  168. * hardware and enable clocks at open().
  169. */
  170. pm_runtime_enable(&pdev->dev);
  171. ret = uio_register_device(&pdev->dev, priv->uioinfo);
  172. if (ret) {
  173. dev_err(&pdev->dev, "unable to register uio device\n");
  174. goto bad1;
  175. }
  176. platform_set_drvdata(pdev, priv);
  177. return 0;
  178. bad1:
  179. kfree(priv);
  180. pm_runtime_disable(&pdev->dev);
  181. bad0:
  182. /* kfree uioinfo for OF */
  183. if (pdev->dev.of_node)
  184. kfree(uioinfo);
  185. bad2:
  186. return ret;
  187. }
  188. static int uio_pdrv_genirq_remove(struct platform_device *pdev)
  189. {
  190. struct uio_pdrv_genirq_platdata *priv = platform_get_drvdata(pdev);
  191. uio_unregister_device(priv->uioinfo);
  192. pm_runtime_disable(&pdev->dev);
  193. priv->uioinfo->handler = NULL;
  194. priv->uioinfo->irqcontrol = NULL;
  195. /* kfree uioinfo for OF */
  196. if (pdev->dev.of_node)
  197. kfree(priv->uioinfo);
  198. kfree(priv);
  199. return 0;
  200. }
  201. static int uio_pdrv_genirq_runtime_nop(struct device *dev)
  202. {
  203. /* Runtime PM callback shared between ->runtime_suspend()
  204. * and ->runtime_resume(). Simply returns success.
  205. *
  206. * In this driver pm_runtime_get_sync() and pm_runtime_put_sync()
  207. * are used at open() and release() time. This allows the
  208. * Runtime PM code to turn off power to the device while the
  209. * device is unused, ie before open() and after release().
  210. *
  211. * This Runtime PM callback does not need to save or restore
  212. * any registers since user space is responsbile for hardware
  213. * register reinitialization after open().
  214. */
  215. return 0;
  216. }
  217. static const struct dev_pm_ops uio_pdrv_genirq_dev_pm_ops = {
  218. .runtime_suspend = uio_pdrv_genirq_runtime_nop,
  219. .runtime_resume = uio_pdrv_genirq_runtime_nop,
  220. };
  221. #ifdef CONFIG_OF
  222. static const struct of_device_id uio_of_genirq_match[] = {
  223. { /* empty for now */ },
  224. };
  225. MODULE_DEVICE_TABLE(of, uio_of_genirq_match);
  226. #else
  227. # define uio_of_genirq_match NULL
  228. #endif
  229. static struct platform_driver uio_pdrv_genirq = {
  230. .probe = uio_pdrv_genirq_probe,
  231. .remove = uio_pdrv_genirq_remove,
  232. .driver = {
  233. .name = DRIVER_NAME,
  234. .owner = THIS_MODULE,
  235. .pm = &uio_pdrv_genirq_dev_pm_ops,
  236. .of_match_table = uio_of_genirq_match,
  237. },
  238. };
  239. module_platform_driver(uio_pdrv_genirq);
  240. MODULE_AUTHOR("Magnus Damm");
  241. MODULE_DESCRIPTION("Userspace I/O platform driver with generic IRQ handling");
  242. MODULE_LICENSE("GPL v2");
  243. MODULE_ALIAS("platform:" DRIVER_NAME);