wakeirq.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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_obj(*wirq);
  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;
  74. unsigned long flags;
  75. spin_lock_irqsave(&dev->power.lock, flags);
  76. wirq = dev->power.wakeirq;
  77. if (!wirq) {
  78. spin_unlock_irqrestore(&dev->power.lock, flags);
  79. return;
  80. }
  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. static void devm_pm_clear_wake_irq(void *dev)
  93. {
  94. dev_pm_clear_wake_irq(dev);
  95. }
  96. /**
  97. * devm_pm_set_wake_irq - device-managed variant of dev_pm_set_wake_irq
  98. * @dev: Device entry
  99. * @irq: Device IO interrupt
  100. *
  101. *
  102. * Attach a device IO interrupt as a wake IRQ, same with dev_pm_set_wake_irq,
  103. * but the device will be auto clear wake capability on driver detach.
  104. */
  105. int devm_pm_set_wake_irq(struct device *dev, int irq)
  106. {
  107. int ret;
  108. ret = dev_pm_set_wake_irq(dev, irq);
  109. if (ret)
  110. return ret;
  111. return devm_add_action_or_reset(dev, devm_pm_clear_wake_irq, dev);
  112. }
  113. EXPORT_SYMBOL_GPL(devm_pm_set_wake_irq);
  114. /**
  115. * handle_threaded_wake_irq - Handler for dedicated wake-up interrupts
  116. * @irq: Device specific dedicated wake-up interrupt
  117. * @_wirq: Wake IRQ data
  118. *
  119. * Some devices have a separate wake-up interrupt in addition to the
  120. * device IO interrupt. The wake-up interrupt signals that a device
  121. * should be woken up from it's idle state. This handler uses device
  122. * specific pm_runtime functions to wake the device, and then it's
  123. * up to the device to do whatever it needs to. Note that as the
  124. * device may need to restore context and start up regulators, we
  125. * use a threaded IRQ.
  126. *
  127. * Also note that we are not resending the lost device interrupts.
  128. * We assume that the wake-up interrupt just needs to wake-up the
  129. * device, and then device's pm_runtime_resume() can deal with the
  130. * situation.
  131. */
  132. static irqreturn_t handle_threaded_wake_irq(int irq, void *_wirq)
  133. {
  134. struct wake_irq *wirq = _wirq;
  135. int res;
  136. /* Maybe abort suspend? */
  137. if (irqd_is_wakeup_set(irq_get_irq_data(irq))) {
  138. pm_wakeup_event(wirq->dev, 0);
  139. return IRQ_HANDLED;
  140. }
  141. /* We don't want RPM_ASYNC or RPM_NOWAIT here */
  142. res = pm_runtime_resume(wirq->dev);
  143. if (res < 0)
  144. dev_warn(wirq->dev,
  145. "wake IRQ with no resume: %i\n", res);
  146. return IRQ_HANDLED;
  147. }
  148. static int __dev_pm_set_dedicated_wake_irq(struct device *dev, int irq, unsigned int flag)
  149. {
  150. struct wake_irq *wirq;
  151. int err;
  152. if (irq < 0)
  153. return -EINVAL;
  154. wirq = kzalloc_obj(*wirq);
  155. if (!wirq)
  156. return -ENOMEM;
  157. wirq->name = kasprintf(GFP_KERNEL, "%s:wakeup", dev_name(dev));
  158. if (!wirq->name) {
  159. err = -ENOMEM;
  160. goto err_free;
  161. }
  162. wirq->dev = dev;
  163. wirq->irq = irq;
  164. /* Prevent deferred spurious wakeirqs with disable_irq_nosync() */
  165. irq_set_status_flags(irq, IRQ_DISABLE_UNLAZY);
  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 | IRQF_NO_AUTOEN,
  172. wirq->name, wirq);
  173. if (err)
  174. goto err_free_name;
  175. err = dev_pm_attach_wake_irq(dev, wirq);
  176. if (err)
  177. goto err_free_irq;
  178. wirq->status = WAKE_IRQ_DEDICATED_ALLOCATED | flag;
  179. return err;
  180. err_free_irq:
  181. free_irq(irq, wirq);
  182. err_free_name:
  183. kfree(wirq->name);
  184. err_free:
  185. kfree(wirq);
  186. return err;
  187. }
  188. /**
  189. * dev_pm_set_dedicated_wake_irq - Request a dedicated wake-up interrupt
  190. * @dev: Device entry
  191. * @irq: Device wake-up interrupt
  192. *
  193. * Unless your hardware has separate wake-up interrupts in addition
  194. * to the device IO interrupts, you don't need this.
  195. *
  196. * Sets up a threaded interrupt handler for a device that has
  197. * a dedicated wake-up interrupt in addition to the device IO
  198. * interrupt.
  199. */
  200. int dev_pm_set_dedicated_wake_irq(struct device *dev, int irq)
  201. {
  202. return __dev_pm_set_dedicated_wake_irq(dev, irq, 0);
  203. }
  204. EXPORT_SYMBOL_GPL(dev_pm_set_dedicated_wake_irq);
  205. /**
  206. * dev_pm_set_dedicated_wake_irq_reverse - Request a dedicated wake-up interrupt
  207. * with reverse enable ordering
  208. * @dev: Device entry
  209. * @irq: Device wake-up interrupt
  210. *
  211. * Unless your hardware has separate wake-up interrupts in addition
  212. * to the device IO interrupts, you don't need this.
  213. *
  214. * Sets up a threaded interrupt handler for a device that has a dedicated
  215. * wake-up interrupt in addition to the device IO interrupt. It sets
  216. * the status of WAKE_IRQ_DEDICATED_REVERSE to tell rpm_suspend()
  217. * to enable dedicated wake-up interrupt after running the runtime suspend
  218. * callback for @dev.
  219. */
  220. int dev_pm_set_dedicated_wake_irq_reverse(struct device *dev, int irq)
  221. {
  222. return __dev_pm_set_dedicated_wake_irq(dev, irq, WAKE_IRQ_DEDICATED_REVERSE);
  223. }
  224. EXPORT_SYMBOL_GPL(dev_pm_set_dedicated_wake_irq_reverse);
  225. /**
  226. * dev_pm_enable_wake_irq_check - Checks and enables wake-up interrupt
  227. * @dev: Device
  228. * @can_change_status: Can change wake-up interrupt status
  229. *
  230. * Enables wakeirq conditionally. We need to enable wake-up interrupt
  231. * lazily on the first rpm_suspend(). This is needed as the consumer device
  232. * starts in RPM_SUSPENDED state, and the first pm_runtime_get() would
  233. * otherwise try to disable already disabled wakeirq. The wake-up interrupt
  234. * starts disabled with IRQ_NOAUTOEN set.
  235. *
  236. * Should be called from rpm_suspend(), rpm_resume(),
  237. * pm_runtime_force_suspend() or pm_runtime_force_resume().
  238. * Caller must hold &dev->power.lock or disable runtime PM to change
  239. * wirq->status.
  240. */
  241. void dev_pm_enable_wake_irq_check(struct device *dev,
  242. bool can_change_status)
  243. {
  244. struct wake_irq *wirq = dev->power.wakeirq;
  245. if (!wirq || !(wirq->status & WAKE_IRQ_DEDICATED_MASK))
  246. return;
  247. if (likely(wirq->status & WAKE_IRQ_DEDICATED_MANAGED)) {
  248. goto enable;
  249. } else if (can_change_status) {
  250. wirq->status |= WAKE_IRQ_DEDICATED_MANAGED;
  251. goto enable;
  252. }
  253. return;
  254. enable:
  255. if (!can_change_status || !(wirq->status & WAKE_IRQ_DEDICATED_REVERSE)) {
  256. enable_irq(wirq->irq);
  257. wirq->status |= WAKE_IRQ_DEDICATED_ENABLED;
  258. }
  259. }
  260. /**
  261. * dev_pm_disable_wake_irq_check - Checks and disables wake-up interrupt
  262. * @dev: Device
  263. * @cond_disable: if set, also check WAKE_IRQ_DEDICATED_REVERSE
  264. *
  265. * Disables wake-up interrupt conditionally based on status.
  266. * Should be called from rpm_suspend(), rpm_resume(),
  267. * pm_runtime_force_suspend() or pm_runtime_force_resume().
  268. */
  269. void dev_pm_disable_wake_irq_check(struct device *dev, bool cond_disable)
  270. {
  271. struct wake_irq *wirq = dev->power.wakeirq;
  272. if (!wirq || !(wirq->status & WAKE_IRQ_DEDICATED_MASK))
  273. return;
  274. if (cond_disable && (wirq->status & WAKE_IRQ_DEDICATED_REVERSE))
  275. return;
  276. if (wirq->status & WAKE_IRQ_DEDICATED_MANAGED) {
  277. wirq->status &= ~WAKE_IRQ_DEDICATED_ENABLED;
  278. disable_irq_nosync(wirq->irq);
  279. }
  280. }
  281. /**
  282. * dev_pm_enable_wake_irq_complete - enable wake IRQ not enabled before
  283. * @dev: Device using the wake IRQ
  284. *
  285. * Enable wake IRQ conditionally based on status, mainly used if want to
  286. * enable wake IRQ after running ->runtime_suspend() which depends on
  287. * WAKE_IRQ_DEDICATED_REVERSE.
  288. *
  289. * Should be called from rpm_suspend() or pm_runtime_force_suspend().
  290. */
  291. void dev_pm_enable_wake_irq_complete(struct device *dev)
  292. {
  293. struct wake_irq *wirq = dev->power.wakeirq;
  294. if (!wirq || !(wirq->status & WAKE_IRQ_DEDICATED_MASK))
  295. return;
  296. if (wirq->status & WAKE_IRQ_DEDICATED_MANAGED &&
  297. wirq->status & WAKE_IRQ_DEDICATED_REVERSE) {
  298. enable_irq(wirq->irq);
  299. wirq->status |= WAKE_IRQ_DEDICATED_ENABLED;
  300. }
  301. }
  302. /**
  303. * dev_pm_arm_wake_irq - Arm device wake-up
  304. * @wirq: Device wake-up interrupt
  305. *
  306. * Sets up the wake-up event conditionally based on the
  307. * device_may_wake().
  308. */
  309. void dev_pm_arm_wake_irq(struct wake_irq *wirq)
  310. {
  311. if (!wirq)
  312. return;
  313. if (device_may_wakeup(wirq->dev)) {
  314. if (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED &&
  315. !(wirq->status & WAKE_IRQ_DEDICATED_ENABLED))
  316. enable_irq(wirq->irq);
  317. enable_irq_wake(wirq->irq);
  318. }
  319. }
  320. /**
  321. * dev_pm_disarm_wake_irq - Disarm device wake-up
  322. * @wirq: Device wake-up interrupt
  323. *
  324. * Clears up the wake-up event conditionally based on the
  325. * device_may_wake().
  326. */
  327. void dev_pm_disarm_wake_irq(struct wake_irq *wirq)
  328. {
  329. if (!wirq)
  330. return;
  331. if (device_may_wakeup(wirq->dev)) {
  332. disable_irq_wake(wirq->irq);
  333. if (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED &&
  334. !(wirq->status & WAKE_IRQ_DEDICATED_ENABLED))
  335. disable_irq_nosync(wirq->irq);
  336. }
  337. }