wakeirq.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. /* Maybe abort suspend? */
  126. if (irqd_is_wakeup_set(irq_get_irq_data(irq))) {
  127. pm_wakeup_event(wirq->dev, 0);
  128. return IRQ_HANDLED;
  129. }
  130. /* We don't want RPM_ASYNC or RPM_NOWAIT here */
  131. res = pm_runtime_resume(wirq->dev);
  132. if (res < 0)
  133. dev_warn(wirq->dev,
  134. "wake IRQ with no resume: %i\n", res);
  135. return IRQ_HANDLED;
  136. }
  137. /**
  138. * dev_pm_set_dedicated_wake_irq - Request a dedicated wake-up interrupt
  139. * @dev: Device entry
  140. * @irq: Device wake-up interrupt
  141. *
  142. * Unless your hardware has separate wake-up interrupts in addition
  143. * to the device IO interrupts, you don't need this.
  144. *
  145. * Sets up a threaded interrupt handler for a device that has
  146. * a dedicated wake-up interrupt in addition to the device IO
  147. * interrupt.
  148. *
  149. * The interrupt starts disabled, and needs to be managed for
  150. * the device by the bus code or the device driver using
  151. * dev_pm_enable_wake_irq() and dev_pm_disable_wake_irq()
  152. * functions.
  153. */
  154. int dev_pm_set_dedicated_wake_irq(struct device *dev, int irq)
  155. {
  156. struct wake_irq *wirq;
  157. int err;
  158. if (irq < 0)
  159. return -EINVAL;
  160. wirq = kzalloc(sizeof(*wirq), GFP_KERNEL);
  161. if (!wirq)
  162. return -ENOMEM;
  163. wirq->dev = dev;
  164. wirq->irq = irq;
  165. irq_set_status_flags(irq, IRQ_NOAUTOEN);
  166. /*
  167. * Consumer device may need to power up and restore state
  168. * so we use a threaded irq.
  169. */
  170. err = request_threaded_irq(irq, NULL, handle_threaded_wake_irq,
  171. IRQF_ONESHOT, dev_name(dev), wirq);
  172. if (err)
  173. goto err_free;
  174. err = dev_pm_attach_wake_irq(dev, irq, wirq);
  175. if (err)
  176. goto err_free_irq;
  177. wirq->status = WAKE_IRQ_DEDICATED_ALLOCATED;
  178. return err;
  179. err_free_irq:
  180. free_irq(irq, wirq);
  181. err_free:
  182. kfree(wirq);
  183. return err;
  184. }
  185. EXPORT_SYMBOL_GPL(dev_pm_set_dedicated_wake_irq);
  186. /**
  187. * dev_pm_enable_wake_irq - Enable device wake-up interrupt
  188. * @dev: Device
  189. *
  190. * Optionally called from the bus code or the device driver for
  191. * runtime_resume() to override the PM runtime core managed wake-up
  192. * interrupt handling to enable the wake-up interrupt.
  193. *
  194. * Note that for runtime_suspend()) the wake-up interrupts
  195. * should be unconditionally enabled unlike for suspend()
  196. * that is conditional.
  197. */
  198. void dev_pm_enable_wake_irq(struct device *dev)
  199. {
  200. struct wake_irq *wirq = dev->power.wakeirq;
  201. if (wirq && (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED))
  202. enable_irq(wirq->irq);
  203. }
  204. EXPORT_SYMBOL_GPL(dev_pm_enable_wake_irq);
  205. /**
  206. * dev_pm_disable_wake_irq - Disable device wake-up interrupt
  207. * @dev: Device
  208. *
  209. * Optionally called from the bus code or the device driver for
  210. * runtime_suspend() to override the PM runtime core managed wake-up
  211. * interrupt handling to disable the wake-up interrupt.
  212. */
  213. void dev_pm_disable_wake_irq(struct device *dev)
  214. {
  215. struct wake_irq *wirq = dev->power.wakeirq;
  216. if (wirq && (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED))
  217. disable_irq_nosync(wirq->irq);
  218. }
  219. EXPORT_SYMBOL_GPL(dev_pm_disable_wake_irq);
  220. /**
  221. * dev_pm_enable_wake_irq_check - Checks and enables wake-up interrupt
  222. * @dev: Device
  223. * @can_change_status: Can change wake-up interrupt status
  224. *
  225. * Enables wakeirq conditionally. We need to enable wake-up interrupt
  226. * lazily on the first rpm_suspend(). This is needed as the consumer device
  227. * starts in RPM_SUSPENDED state, and the the first pm_runtime_get() would
  228. * otherwise try to disable already disabled wakeirq. The wake-up interrupt
  229. * starts disabled with IRQ_NOAUTOEN set.
  230. *
  231. * Should be only called from rpm_suspend() and rpm_resume() path.
  232. * Caller must hold &dev->power.lock to change wirq->status
  233. */
  234. void dev_pm_enable_wake_irq_check(struct device *dev,
  235. bool can_change_status)
  236. {
  237. struct wake_irq *wirq = dev->power.wakeirq;
  238. if (!wirq || !((wirq->status & WAKE_IRQ_DEDICATED_MASK)))
  239. return;
  240. if (likely(wirq->status & WAKE_IRQ_DEDICATED_MANAGED)) {
  241. goto enable;
  242. } else if (can_change_status) {
  243. wirq->status |= WAKE_IRQ_DEDICATED_MANAGED;
  244. goto enable;
  245. }
  246. return;
  247. enable:
  248. enable_irq(wirq->irq);
  249. }
  250. /**
  251. * dev_pm_disable_wake_irq_check - Checks and disables wake-up interrupt
  252. * @dev: Device
  253. *
  254. * Disables wake-up interrupt conditionally based on status.
  255. * Should be only called from rpm_suspend() and rpm_resume() path.
  256. */
  257. void dev_pm_disable_wake_irq_check(struct device *dev)
  258. {
  259. struct wake_irq *wirq = dev->power.wakeirq;
  260. if (!wirq || !((wirq->status & WAKE_IRQ_DEDICATED_MASK)))
  261. return;
  262. if (wirq->status & WAKE_IRQ_DEDICATED_MANAGED)
  263. disable_irq_nosync(wirq->irq);
  264. }
  265. /**
  266. * dev_pm_arm_wake_irq - Arm device wake-up
  267. * @wirq: Device wake-up interrupt
  268. *
  269. * Sets up the wake-up event conditionally based on the
  270. * device_may_wake().
  271. */
  272. void dev_pm_arm_wake_irq(struct wake_irq *wirq)
  273. {
  274. if (!wirq)
  275. return;
  276. if (device_may_wakeup(wirq->dev))
  277. enable_irq_wake(wirq->irq);
  278. }
  279. /**
  280. * dev_pm_disarm_wake_irq - Disarm device wake-up
  281. * @wirq: Device wake-up interrupt
  282. *
  283. * Clears up the wake-up event conditionally based on the
  284. * device_may_wake().
  285. */
  286. void dev_pm_disarm_wake_irq(struct wake_irq *wirq)
  287. {
  288. if (!wirq)
  289. return;
  290. if (device_may_wakeup(wirq->dev))
  291. disable_irq_wake(wirq->irq);
  292. }