wakeirq.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Device wakeirq helper functions */
  3. #include <linux/device.h>
  4. #include <linux/interrupt.h>
  5. #include <linux/irq.h>
  6. #include <linux/slab.h>
  7. #include <linux/pm_runtime.h>
  8. #include <linux/pm_wakeirq.h>
  9. #include "power.h"
  10. /**
  11. * dev_pm_attach_wake_irq - Attach device interrupt as a wake IRQ
  12. * @dev: Device entry
  13. * @irq: Device wake-up capable interrupt
  14. * @wirq: Wake irq specific data
  15. *
  16. * Internal function to attach either a device IO interrupt or a
  17. * dedicated wake-up interrupt as a wake IRQ.
  18. */
  19. static int dev_pm_attach_wake_irq(struct device *dev, int irq,
  20. struct wake_irq *wirq)
  21. {
  22. unsigned long flags;
  23. if (!dev || !wirq)
  24. return -EINVAL;
  25. spin_lock_irqsave(&dev->power.lock, flags);
  26. if (dev_WARN_ONCE(dev, dev->power.wakeirq,
  27. "wake irq already initialized\n")) {
  28. spin_unlock_irqrestore(&dev->power.lock, flags);
  29. return -EEXIST;
  30. }
  31. dev->power.wakeirq = wirq;
  32. device_wakeup_attach_irq(dev, wirq);
  33. spin_unlock_irqrestore(&dev->power.lock, flags);
  34. return 0;
  35. }
  36. /**
  37. * dev_pm_set_wake_irq - Attach device IO interrupt as wake IRQ
  38. * @dev: Device entry
  39. * @irq: Device IO interrupt
  40. *
  41. * Attach a device IO interrupt as a wake IRQ. The wake IRQ gets
  42. * automatically configured for wake-up from suspend based
  43. * on the device specific sysfs wakeup entry. Typically called
  44. * during driver probe after calling device_init_wakeup().
  45. */
  46. int dev_pm_set_wake_irq(struct device *dev, int irq)
  47. {
  48. struct wake_irq *wirq;
  49. int err;
  50. if (irq < 0)
  51. return -EINVAL;
  52. wirq = kzalloc(sizeof(*wirq), GFP_KERNEL);
  53. if (!wirq)
  54. return -ENOMEM;
  55. wirq->dev = dev;
  56. wirq->irq = irq;
  57. err = dev_pm_attach_wake_irq(dev, irq, wirq);
  58. if (err)
  59. kfree(wirq);
  60. return err;
  61. }
  62. EXPORT_SYMBOL_GPL(dev_pm_set_wake_irq);
  63. /**
  64. * dev_pm_clear_wake_irq - Detach a device IO interrupt wake IRQ
  65. * @dev: Device entry
  66. *
  67. * Detach a device wake IRQ and free resources.
  68. *
  69. * Note that it's OK for drivers to call this without calling
  70. * dev_pm_set_wake_irq() as all the driver instances may not have
  71. * a wake IRQ configured. This avoid adding wake IRQ specific
  72. * checks into the drivers.
  73. */
  74. void dev_pm_clear_wake_irq(struct device *dev)
  75. {
  76. struct wake_irq *wirq = dev->power.wakeirq;
  77. unsigned long flags;
  78. if (!wirq)
  79. return;
  80. spin_lock_irqsave(&dev->power.lock, flags);
  81. device_wakeup_detach_irq(dev);
  82. dev->power.wakeirq = NULL;
  83. spin_unlock_irqrestore(&dev->power.lock, flags);
  84. if (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED) {
  85. free_irq(wirq->irq, wirq);
  86. wirq->status &= ~WAKE_IRQ_DEDICATED_MASK;
  87. }
  88. kfree(wirq->name);
  89. kfree(wirq);
  90. }
  91. EXPORT_SYMBOL_GPL(dev_pm_clear_wake_irq);
  92. /**
  93. * handle_threaded_wake_irq - Handler for dedicated wake-up interrupts
  94. * @irq: Device specific dedicated wake-up interrupt
  95. * @_wirq: Wake IRQ data
  96. *
  97. * Some devices have a separate wake-up interrupt in addition to the
  98. * device IO interrupt. The wake-up interrupt signals that a device
  99. * should be woken up from it's idle state. This handler uses device
  100. * specific pm_runtime functions to wake the device, and then it's
  101. * up to the device to do whatever it needs to. Note that as the
  102. * device may need to restore context and start up regulators, we
  103. * use a threaded IRQ.
  104. *
  105. * Also note that we are not resending the lost device interrupts.
  106. * We assume that the wake-up interrupt just needs to wake-up the
  107. * device, and then device's pm_runtime_resume() can deal with the
  108. * situation.
  109. */
  110. static irqreturn_t handle_threaded_wake_irq(int irq, void *_wirq)
  111. {
  112. struct wake_irq *wirq = _wirq;
  113. int res;
  114. /* Maybe abort suspend? */
  115. if (irqd_is_wakeup_set(irq_get_irq_data(irq))) {
  116. pm_wakeup_event(wirq->dev, 0);
  117. return IRQ_HANDLED;
  118. }
  119. /* We don't want RPM_ASYNC or RPM_NOWAIT here */
  120. res = pm_runtime_resume(wirq->dev);
  121. if (res < 0)
  122. dev_warn(wirq->dev,
  123. "wake IRQ with no resume: %i\n", res);
  124. return IRQ_HANDLED;
  125. }
  126. /**
  127. * dev_pm_set_dedicated_wake_irq - Request a dedicated wake-up interrupt
  128. * @dev: Device entry
  129. * @irq: Device wake-up interrupt
  130. *
  131. * Unless your hardware has separate wake-up interrupts in addition
  132. * to the device IO interrupts, you don't need this.
  133. *
  134. * Sets up a threaded interrupt handler for a device that has
  135. * a dedicated wake-up interrupt in addition to the device IO
  136. * interrupt.
  137. *
  138. * The interrupt starts disabled, and needs to be managed for
  139. * the device by the bus code or the device driver using
  140. * dev_pm_enable_wake_irq() and dev_pm_disable_wake_irq()
  141. * functions.
  142. */
  143. int dev_pm_set_dedicated_wake_irq(struct device *dev, int irq)
  144. {
  145. struct wake_irq *wirq;
  146. int err;
  147. if (irq < 0)
  148. return -EINVAL;
  149. wirq = kzalloc(sizeof(*wirq), GFP_KERNEL);
  150. if (!wirq)
  151. return -ENOMEM;
  152. wirq->name = kasprintf(GFP_KERNEL, "%s:wakeup", dev_name(dev));
  153. if (!wirq->name) {
  154. err = -ENOMEM;
  155. goto err_free;
  156. }
  157. wirq->dev = dev;
  158. wirq->irq = irq;
  159. irq_set_status_flags(irq, IRQ_NOAUTOEN);
  160. /* Prevent deferred spurious wakeirqs with disable_irq_nosync() */
  161. irq_set_status_flags(irq, IRQ_DISABLE_UNLAZY);
  162. /*
  163. * Consumer device may need to power up and restore state
  164. * so we use a threaded irq.
  165. */
  166. err = request_threaded_irq(irq, NULL, handle_threaded_wake_irq,
  167. IRQF_ONESHOT, wirq->name, wirq);
  168. if (err)
  169. goto err_free_name;
  170. err = dev_pm_attach_wake_irq(dev, irq, wirq);
  171. if (err)
  172. goto err_free_irq;
  173. wirq->status = WAKE_IRQ_DEDICATED_ALLOCATED;
  174. return err;
  175. err_free_irq:
  176. free_irq(irq, wirq);
  177. err_free_name:
  178. kfree(wirq->name);
  179. err_free:
  180. kfree(wirq);
  181. return err;
  182. }
  183. EXPORT_SYMBOL_GPL(dev_pm_set_dedicated_wake_irq);
  184. /**
  185. * dev_pm_enable_wake_irq - Enable device wake-up interrupt
  186. * @dev: Device
  187. *
  188. * Optionally called from the bus code or the device driver for
  189. * runtime_resume() to override the PM runtime core managed wake-up
  190. * interrupt handling to enable the wake-up interrupt.
  191. *
  192. * Note that for runtime_suspend()) the wake-up interrupts
  193. * should be unconditionally enabled unlike for suspend()
  194. * that is conditional.
  195. */
  196. void dev_pm_enable_wake_irq(struct device *dev)
  197. {
  198. struct wake_irq *wirq = dev->power.wakeirq;
  199. if (wirq && (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED))
  200. enable_irq(wirq->irq);
  201. }
  202. EXPORT_SYMBOL_GPL(dev_pm_enable_wake_irq);
  203. /**
  204. * dev_pm_disable_wake_irq - Disable device wake-up interrupt
  205. * @dev: Device
  206. *
  207. * Optionally called from the bus code or the device driver for
  208. * runtime_suspend() to override the PM runtime core managed wake-up
  209. * interrupt handling to disable the wake-up interrupt.
  210. */
  211. void dev_pm_disable_wake_irq(struct device *dev)
  212. {
  213. struct wake_irq *wirq = dev->power.wakeirq;
  214. if (wirq && (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED))
  215. disable_irq_nosync(wirq->irq);
  216. }
  217. EXPORT_SYMBOL_GPL(dev_pm_disable_wake_irq);
  218. /**
  219. * dev_pm_enable_wake_irq_check - Checks and enables wake-up interrupt
  220. * @dev: Device
  221. * @can_change_status: Can change wake-up interrupt status
  222. *
  223. * Enables wakeirq conditionally. We need to enable wake-up interrupt
  224. * lazily on the first rpm_suspend(). This is needed as the consumer device
  225. * starts in RPM_SUSPENDED state, and the the first pm_runtime_get() would
  226. * otherwise try to disable already disabled wakeirq. The wake-up interrupt
  227. * starts disabled with IRQ_NOAUTOEN set.
  228. *
  229. * Should be only called from rpm_suspend() and rpm_resume() path.
  230. * Caller must hold &dev->power.lock to change wirq->status
  231. */
  232. void dev_pm_enable_wake_irq_check(struct device *dev,
  233. bool can_change_status)
  234. {
  235. struct wake_irq *wirq = dev->power.wakeirq;
  236. if (!wirq || !(wirq->status & WAKE_IRQ_DEDICATED_MASK))
  237. return;
  238. if (likely(wirq->status & WAKE_IRQ_DEDICATED_MANAGED)) {
  239. goto enable;
  240. } else if (can_change_status) {
  241. wirq->status |= WAKE_IRQ_DEDICATED_MANAGED;
  242. goto enable;
  243. }
  244. return;
  245. enable:
  246. enable_irq(wirq->irq);
  247. }
  248. /**
  249. * dev_pm_disable_wake_irq_check - Checks and disables wake-up interrupt
  250. * @dev: Device
  251. *
  252. * Disables wake-up interrupt conditionally based on status.
  253. * Should be only called from rpm_suspend() and rpm_resume() path.
  254. */
  255. void dev_pm_disable_wake_irq_check(struct device *dev)
  256. {
  257. struct wake_irq *wirq = dev->power.wakeirq;
  258. if (!wirq || !(wirq->status & WAKE_IRQ_DEDICATED_MASK))
  259. return;
  260. if (wirq->status & WAKE_IRQ_DEDICATED_MANAGED)
  261. disable_irq_nosync(wirq->irq);
  262. }
  263. /**
  264. * dev_pm_arm_wake_irq - Arm device wake-up
  265. * @wirq: Device wake-up interrupt
  266. *
  267. * Sets up the wake-up event conditionally based on the
  268. * device_may_wake().
  269. */
  270. void dev_pm_arm_wake_irq(struct wake_irq *wirq)
  271. {
  272. if (!wirq)
  273. return;
  274. if (device_may_wakeup(wirq->dev)) {
  275. if (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED &&
  276. !pm_runtime_status_suspended(wirq->dev))
  277. enable_irq(wirq->irq);
  278. enable_irq_wake(wirq->irq);
  279. }
  280. }
  281. /**
  282. * dev_pm_disarm_wake_irq - Disarm device wake-up
  283. * @wirq: Device wake-up interrupt
  284. *
  285. * Clears up the wake-up event conditionally based on the
  286. * device_may_wake().
  287. */
  288. void dev_pm_disarm_wake_irq(struct wake_irq *wirq)
  289. {
  290. if (!wirq)
  291. return;
  292. if (device_may_wakeup(wirq->dev)) {
  293. disable_irq_wake(wirq->irq);
  294. if (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED &&
  295. !pm_runtime_status_suspended(wirq->dev))
  296. disable_irq_nosync(wirq->irq);
  297. }
  298. }