wakeirq.c 9.5 KB

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