uio_pdrv_genirq.c 7.3 KB

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