wakeirq.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * wakeirq.c - Device wakeirq helper functions
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  9. * kind, whether express or implied; without even the implied warranty
  10. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/device.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/irq.h>
  16. #include <linux/slab.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/pm_wakeirq.h>
  19. #include "power.h"
  20. /**
  21. * dev_pm_attach_wake_irq - Attach device interrupt as a wake IRQ
  22. * @dev: Device entry
  23. * @irq: Device wake-up capable interrupt
  24. * @wirq: Wake irq specific data
  25. *
  26. * Internal function to attach either a device IO interrupt or a
  27. * dedicated wake-up interrupt as a wake IRQ.
  28. */
  29. static int dev_pm_attach_wake_irq(struct device *dev, int irq,
  30. struct wake_irq *wirq)
  31. {
  32. unsigned long flags;
  33. int err;
  34. if (!dev || !wirq)
  35. return -EINVAL;
  36. spin_lock_irqsave(&dev->power.lock, flags);
  37. if (dev_WARN_ONCE(dev, dev->power.wakeirq,
  38. "wake irq already initialized\n")) {
  39. spin_unlock_irqrestore(&dev->power.lock, flags);
  40. return -EEXIST;
  41. }
  42. err = device_wakeup_attach_irq(dev, wirq);
  43. if (!err)
  44. dev->power.wakeirq = wirq;
  45. spin_unlock_irqrestore(&dev->power.lock, flags);
  46. return err;
  47. }
  48. /**
  49. * dev_pm_set_wake_irq - Attach device IO interrupt as wake IRQ
  50. * @dev: Device entry
  51. * @irq: Device IO interrupt
  52. *
  53. * Attach a device IO interrupt as a wake IRQ. The wake IRQ gets
  54. * automatically configured for wake-up from suspend based
  55. * on the device specific sysfs wakeup entry. Typically called
  56. * during driver probe after calling device_init_wakeup().
  57. */
  58. int dev_pm_set_wake_irq(struct device *dev, int irq)
  59. {
  60. struct wake_irq *wirq;
  61. int err;
  62. wirq = kzalloc(sizeof(*wirq), GFP_KERNEL);
  63. if (!wirq)
  64. return -ENOMEM;
  65. wirq->dev = dev;
  66. wirq->irq = irq;
  67. err = dev_pm_attach_wake_irq(dev, irq, wirq);
  68. if (err)
  69. kfree(wirq);
  70. return err;
  71. }
  72. EXPORT_SYMBOL_GPL(dev_pm_set_wake_irq);
  73. /**
  74. * dev_pm_clear_wake_irq - Detach a device IO interrupt wake IRQ
  75. * @dev: Device entry
  76. *
  77. * Detach a device wake IRQ and free resources.
  78. *
  79. * Note that it's OK for drivers to call this without calling
  80. * dev_pm_set_wake_irq() as all the driver instances may not have
  81. * a wake IRQ configured. This avoid adding wake IRQ specific
  82. * checks into the drivers.
  83. */
  84. void dev_pm_clear_wake_irq(struct device *dev)
  85. {
  86. struct wake_irq *wirq = dev->power.wakeirq;
  87. unsigned long flags;
  88. if (!wirq)
  89. return;
  90. spin_lock_irqsave(&dev->power.lock, flags);
  91. device_wakeup_detach_irq(dev);
  92. dev->power.wakeirq = NULL;
  93. spin_unlock_irqrestore(&dev->power.lock, flags);
  94. if (wirq->dedicated_irq)
  95. free_irq(wirq->irq, wirq);
  96. kfree(wirq);
  97. }
  98. EXPORT_SYMBOL_GPL(dev_pm_clear_wake_irq);
  99. /**
  100. * handle_threaded_wake_irq - Handler for dedicated wake-up interrupts
  101. * @irq: Device specific dedicated wake-up interrupt
  102. * @_wirq: Wake IRQ data
  103. *
  104. * Some devices have a separate wake-up interrupt in addition to the
  105. * device IO interrupt. The wake-up interrupt signals that a device
  106. * should be woken up from it's idle state. This handler uses device
  107. * specific pm_runtime functions to wake the device, and then it's
  108. * up to the device to do whatever it needs to. Note that as the
  109. * device may need to restore context and start up regulators, we
  110. * use a threaded IRQ.
  111. *
  112. * Also note that we are not resending the lost device interrupts.
  113. * We assume that the wake-up interrupt just needs to wake-up the
  114. * device, and then device's pm_runtime_resume() can deal with the
  115. * situation.
  116. */
  117. static irqreturn_t handle_threaded_wake_irq(int irq, void *_wirq)
  118. {
  119. struct wake_irq *wirq = _wirq;
  120. int res;
  121. /* We don't want RPM_ASYNC or RPM_NOWAIT here */
  122. res = pm_runtime_resume(wirq->dev);
  123. if (res < 0)
  124. dev_warn(wirq->dev,
  125. "wake IRQ with no resume: %i\n", res);
  126. return IRQ_HANDLED;
  127. }
  128. /**
  129. * dev_pm_set_dedicated_wake_irq - Request a dedicated wake-up interrupt
  130. * @dev: Device entry
  131. * @irq: Device wake-up interrupt
  132. *
  133. * Unless your hardware has separate wake-up interrupts in addition
  134. * to the device IO interrupts, you don't need this.
  135. *
  136. * Sets up a threaded interrupt handler for a device that has
  137. * a dedicated wake-up interrupt in addition to the device IO
  138. * interrupt.
  139. *
  140. * The interrupt starts disabled, and needs to be managed for
  141. * the device by the bus code or the device driver using
  142. * dev_pm_enable_wake_irq() and dev_pm_disable_wake_irq()
  143. * functions.
  144. */
  145. int dev_pm_set_dedicated_wake_irq(struct device *dev, int irq)
  146. {
  147. struct wake_irq *wirq;
  148. int err;
  149. wirq = kzalloc(sizeof(*wirq), GFP_KERNEL);
  150. if (!wirq)
  151. return -ENOMEM;
  152. wirq->dev = dev;
  153. wirq->irq = irq;
  154. wirq->dedicated_irq = true;
  155. irq_set_status_flags(irq, IRQ_NOAUTOEN);
  156. /*
  157. * Consumer device may need to power up and restore state
  158. * so we use a threaded irq.
  159. */
  160. err = request_threaded_irq(irq, NULL, handle_threaded_wake_irq,
  161. IRQF_ONESHOT, dev_name(dev), wirq);
  162. if (err)
  163. goto err_free;
  164. err = dev_pm_attach_wake_irq(dev, irq, wirq);
  165. if (err)
  166. goto err_free_irq;
  167. return err;
  168. err_free_irq:
  169. free_irq(irq, wirq);
  170. err_free:
  171. kfree(wirq);
  172. return err;
  173. }
  174. EXPORT_SYMBOL_GPL(dev_pm_set_dedicated_wake_irq);
  175. /**
  176. * dev_pm_enable_wake_irq - Enable device wake-up interrupt
  177. * @dev: Device
  178. *
  179. * Called from the bus code or the device driver for
  180. * runtime_suspend() to enable the wake-up interrupt while
  181. * the device is running.
  182. *
  183. * Note that for runtime_suspend()) the wake-up interrupts
  184. * should be unconditionally enabled unlike for suspend()
  185. * that is conditional.
  186. */
  187. void dev_pm_enable_wake_irq(struct device *dev)
  188. {
  189. struct wake_irq *wirq = dev->power.wakeirq;
  190. if (wirq && wirq->dedicated_irq)
  191. enable_irq(wirq->irq);
  192. }
  193. EXPORT_SYMBOL_GPL(dev_pm_enable_wake_irq);
  194. /**
  195. * dev_pm_disable_wake_irq - Disable device wake-up interrupt
  196. * @dev: Device
  197. *
  198. * Called from the bus code or the device driver for
  199. * runtime_resume() to disable the wake-up interrupt while
  200. * the device is running.
  201. */
  202. void dev_pm_disable_wake_irq(struct device *dev)
  203. {
  204. struct wake_irq *wirq = dev->power.wakeirq;
  205. if (wirq && wirq->dedicated_irq)
  206. disable_irq_nosync(wirq->irq);
  207. }
  208. EXPORT_SYMBOL_GPL(dev_pm_disable_wake_irq);
  209. /**
  210. * dev_pm_arm_wake_irq - Arm device wake-up
  211. * @wirq: Device wake-up interrupt
  212. *
  213. * Sets up the wake-up event conditionally based on the
  214. * device_may_wake().
  215. */
  216. void dev_pm_arm_wake_irq(struct wake_irq *wirq)
  217. {
  218. if (!wirq)
  219. return;
  220. if (device_may_wakeup(wirq->dev))
  221. enable_irq_wake(wirq->irq);
  222. }
  223. /**
  224. * dev_pm_disarm_wake_irq - Disarm device wake-up
  225. * @wirq: Device wake-up interrupt
  226. *
  227. * Clears up the wake-up event conditionally based on the
  228. * device_may_wake().
  229. */
  230. void dev_pm_disarm_wake_irq(struct wake_irq *wirq)
  231. {
  232. if (!wirq)
  233. return;
  234. if (device_may_wakeup(wirq->dev))
  235. disable_irq_wake(wirq->irq);
  236. }