wakeirq.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. if (irq < 0)
  63. return -EINVAL;
  64. wirq = kzalloc(sizeof(*wirq), GFP_KERNEL);
  65. if (!wirq)
  66. return -ENOMEM;
  67. wirq->dev = dev;
  68. wirq->irq = irq;
  69. err = dev_pm_attach_wake_irq(dev, irq, wirq);
  70. if (err)
  71. kfree(wirq);
  72. return err;
  73. }
  74. EXPORT_SYMBOL_GPL(dev_pm_set_wake_irq);
  75. /**
  76. * dev_pm_clear_wake_irq - Detach a device IO interrupt wake IRQ
  77. * @dev: Device entry
  78. *
  79. * Detach a device wake IRQ and free resources.
  80. *
  81. * Note that it's OK for drivers to call this without calling
  82. * dev_pm_set_wake_irq() as all the driver instances may not have
  83. * a wake IRQ configured. This avoid adding wake IRQ specific
  84. * checks into the drivers.
  85. */
  86. void dev_pm_clear_wake_irq(struct device *dev)
  87. {
  88. struct wake_irq *wirq = dev->power.wakeirq;
  89. unsigned long flags;
  90. if (!wirq)
  91. return;
  92. spin_lock_irqsave(&dev->power.lock, flags);
  93. device_wakeup_detach_irq(dev);
  94. dev->power.wakeirq = NULL;
  95. spin_unlock_irqrestore(&dev->power.lock, flags);
  96. if (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED) {
  97. free_irq(wirq->irq, wirq);
  98. wirq->status &= ~WAKE_IRQ_DEDICATED_MASK;
  99. }
  100. kfree(wirq);
  101. }
  102. EXPORT_SYMBOL_GPL(dev_pm_clear_wake_irq);
  103. /**
  104. * handle_threaded_wake_irq - Handler for dedicated wake-up interrupts
  105. * @irq: Device specific dedicated wake-up interrupt
  106. * @_wirq: Wake IRQ data
  107. *
  108. * Some devices have a separate wake-up interrupt in addition to the
  109. * device IO interrupt. The wake-up interrupt signals that a device
  110. * should be woken up from it's idle state. This handler uses device
  111. * specific pm_runtime functions to wake the device, and then it's
  112. * up to the device to do whatever it needs to. Note that as the
  113. * device may need to restore context and start up regulators, we
  114. * use a threaded IRQ.
  115. *
  116. * Also note that we are not resending the lost device interrupts.
  117. * We assume that the wake-up interrupt just needs to wake-up the
  118. * device, and then device's pm_runtime_resume() can deal with the
  119. * situation.
  120. */
  121. static irqreturn_t handle_threaded_wake_irq(int irq, void *_wirq)
  122. {
  123. struct wake_irq *wirq = _wirq;
  124. int res;
  125. /* We don't want RPM_ASYNC or RPM_NOWAIT here */
  126. res = pm_runtime_resume(wirq->dev);
  127. if (res < 0)
  128. dev_warn(wirq->dev,
  129. "wake IRQ with no resume: %i\n", res);
  130. return IRQ_HANDLED;
  131. }
  132. /**
  133. * dev_pm_set_dedicated_wake_irq - Request a dedicated wake-up interrupt
  134. * @dev: Device entry
  135. * @irq: Device wake-up interrupt
  136. *
  137. * Unless your hardware has separate wake-up interrupts in addition
  138. * to the device IO interrupts, you don't need this.
  139. *
  140. * Sets up a threaded interrupt handler for a device that has
  141. * a dedicated wake-up interrupt in addition to the device IO
  142. * interrupt.
  143. *
  144. * The interrupt starts disabled, and needs to be managed for
  145. * the device by the bus code or the device driver using
  146. * dev_pm_enable_wake_irq() and dev_pm_disable_wake_irq()
  147. * functions.
  148. */
  149. int dev_pm_set_dedicated_wake_irq(struct device *dev, int irq)
  150. {
  151. struct wake_irq *wirq;
  152. int err;
  153. if (irq < 0)
  154. return -EINVAL;
  155. wirq = kzalloc(sizeof(*wirq), GFP_KERNEL);
  156. if (!wirq)
  157. return -ENOMEM;
  158. wirq->dev = dev;
  159. wirq->irq = irq;
  160. irq_set_status_flags(irq, IRQ_NOAUTOEN);
  161. /*
  162. * Consumer device may need to power up and restore state
  163. * so we use a threaded irq.
  164. */
  165. err = request_threaded_irq(irq, NULL, handle_threaded_wake_irq,
  166. IRQF_ONESHOT, dev_name(dev), wirq);
  167. if (err)
  168. goto err_free;
  169. err = dev_pm_attach_wake_irq(dev, irq, wirq);
  170. if (err)
  171. goto err_free_irq;
  172. wirq->status = WAKE_IRQ_DEDICATED_ALLOCATED;
  173. return err;
  174. err_free_irq:
  175. free_irq(irq, wirq);
  176. err_free:
  177. kfree(wirq);
  178. return err;
  179. }
  180. EXPORT_SYMBOL_GPL(dev_pm_set_dedicated_wake_irq);
  181. /**
  182. * dev_pm_enable_wake_irq - Enable device wake-up interrupt
  183. * @dev: Device
  184. *
  185. * Optionally called from the bus code or the device driver for
  186. * runtime_resume() to override the PM runtime core managed wake-up
  187. * interrupt handling to enable the wake-up interrupt.
  188. *
  189. * Note that for runtime_suspend()) the wake-up interrupts
  190. * should be unconditionally enabled unlike for suspend()
  191. * that is conditional.
  192. */
  193. void dev_pm_enable_wake_irq(struct device *dev)
  194. {
  195. struct wake_irq *wirq = dev->power.wakeirq;
  196. if (wirq && (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED))
  197. enable_irq(wirq->irq);
  198. }
  199. EXPORT_SYMBOL_GPL(dev_pm_enable_wake_irq);
  200. /**
  201. * dev_pm_disable_wake_irq - Disable device wake-up interrupt
  202. * @dev: Device
  203. *
  204. * Optionally called from the bus code or the device driver for
  205. * runtime_suspend() to override the PM runtime core managed wake-up
  206. * interrupt handling to disable the wake-up interrupt.
  207. */
  208. void dev_pm_disable_wake_irq(struct device *dev)
  209. {
  210. struct wake_irq *wirq = dev->power.wakeirq;
  211. if (wirq && (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED))
  212. disable_irq_nosync(wirq->irq);
  213. }
  214. EXPORT_SYMBOL_GPL(dev_pm_disable_wake_irq);
  215. /**
  216. * dev_pm_enable_wake_irq_check - Checks and enables wake-up interrupt
  217. * @dev: Device
  218. * @can_change_status: Can change wake-up interrupt status
  219. *
  220. * Enables wakeirq conditionally. We need to enable wake-up interrupt
  221. * lazily on the first rpm_suspend(). This is needed as the consumer device
  222. * starts in RPM_SUSPENDED state, and the the first pm_runtime_get() would
  223. * otherwise try to disable already disabled wakeirq. The wake-up interrupt
  224. * starts disabled with IRQ_NOAUTOEN set.
  225. *
  226. * Should be only called from rpm_suspend() and rpm_resume() path.
  227. * Caller must hold &dev->power.lock to change wirq->status
  228. */
  229. void dev_pm_enable_wake_irq_check(struct device *dev,
  230. bool can_change_status)
  231. {
  232. struct wake_irq *wirq = dev->power.wakeirq;
  233. if (!wirq || !((wirq->status & WAKE_IRQ_DEDICATED_MASK)))
  234. return;
  235. if (likely(wirq->status & WAKE_IRQ_DEDICATED_MANAGED)) {
  236. goto enable;
  237. } else if (can_change_status) {
  238. wirq->status |= WAKE_IRQ_DEDICATED_MANAGED;
  239. goto enable;
  240. }
  241. return;
  242. enable:
  243. enable_irq(wirq->irq);
  244. }
  245. /**
  246. * dev_pm_disable_wake_irq_check - Checks and disables wake-up interrupt
  247. * @dev: Device
  248. *
  249. * Disables wake-up interrupt conditionally based on status.
  250. * Should be only called from rpm_suspend() and rpm_resume() path.
  251. */
  252. void dev_pm_disable_wake_irq_check(struct device *dev)
  253. {
  254. struct wake_irq *wirq = dev->power.wakeirq;
  255. if (!wirq || !((wirq->status & WAKE_IRQ_DEDICATED_MASK)))
  256. return;
  257. if (wirq->status & WAKE_IRQ_DEDICATED_MANAGED)
  258. disable_irq_nosync(wirq->irq);
  259. }
  260. /**
  261. * dev_pm_arm_wake_irq - Arm device wake-up
  262. * @wirq: Device wake-up interrupt
  263. *
  264. * Sets up the wake-up event conditionally based on the
  265. * device_may_wake().
  266. */
  267. void dev_pm_arm_wake_irq(struct wake_irq *wirq)
  268. {
  269. if (!wirq)
  270. return;
  271. if (device_may_wakeup(wirq->dev))
  272. enable_irq_wake(wirq->irq);
  273. }
  274. /**
  275. * dev_pm_disarm_wake_irq - Disarm device wake-up
  276. * @wirq: Device wake-up interrupt
  277. *
  278. * Clears up the wake-up event conditionally based on the
  279. * device_may_wake().
  280. */
  281. void dev_pm_disarm_wake_irq(struct wake_irq *wirq)
  282. {
  283. if (!wirq)
  284. return;
  285. if (device_may_wakeup(wirq->dev))
  286. disable_irq_wake(wirq->irq);
  287. }