uio_pdrv_genirq.c 7.7 KB

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