wakeirq.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. /* Prevent deferred spurious wakeirqs with disable_irq_nosync() */
  167. irq_set_status_flags(irq, IRQ_DISABLE_UNLAZY);
  168. /*
  169. * Consumer device may need to power up and restore state
  170. * so we use a threaded irq.
  171. */
  172. err = request_threaded_irq(irq, NULL, handle_threaded_wake_irq,
  173. IRQF_ONESHOT, dev_name(dev), wirq);
  174. if (err)
  175. goto err_free;
  176. err = dev_pm_attach_wake_irq(dev, irq, wirq);
  177. if (err)
  178. goto err_free_irq;
  179. wirq->status = WAKE_IRQ_DEDICATED_ALLOCATED;
  180. return err;
  181. err_free_irq:
  182. free_irq(irq, wirq);
  183. err_free:
  184. kfree(wirq);
  185. return err;
  186. }
  187. EXPORT_SYMBOL_GPL(dev_pm_set_dedicated_wake_irq);
  188. /**
  189. * dev_pm_enable_wake_irq - Enable device wake-up interrupt
  190. * @dev: Device
  191. *
  192. * Optionally called from the bus code or the device driver for
  193. * runtime_resume() to override the PM runtime core managed wake-up
  194. * interrupt handling to enable the wake-up interrupt.
  195. *
  196. * Note that for runtime_suspend()) the wake-up interrupts
  197. * should be unconditionally enabled unlike for suspend()
  198. * that is conditional.
  199. */
  200. void dev_pm_enable_wake_irq(struct device *dev)
  201. {
  202. struct wake_irq *wirq = dev->power.wakeirq;
  203. if (wirq && (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED))
  204. enable_irq(wirq->irq);
  205. }
  206. EXPORT_SYMBOL_GPL(dev_pm_enable_wake_irq);
  207. /**
  208. * dev_pm_disable_wake_irq - Disable device wake-up interrupt
  209. * @dev: Device
  210. *
  211. * Optionally called from the bus code or the device driver for
  212. * runtime_suspend() to override the PM runtime core managed wake-up
  213. * interrupt handling to disable the wake-up interrupt.
  214. */
  215. void dev_pm_disable_wake_irq(struct device *dev)
  216. {
  217. struct wake_irq *wirq = dev->power.wakeirq;
  218. if (wirq && (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED))
  219. disable_irq_nosync(wirq->irq);
  220. }
  221. EXPORT_SYMBOL_GPL(dev_pm_disable_wake_irq);
  222. /**
  223. * dev_pm_enable_wake_irq_check - Checks and enables wake-up interrupt
  224. * @dev: Device
  225. * @can_change_status: Can change wake-up interrupt status
  226. *
  227. * Enables wakeirq conditionally. We need to enable wake-up interrupt
  228. * lazily on the first rpm_suspend(). This is needed as the consumer device
  229. * starts in RPM_SUSPENDED state, and the the first pm_runtime_get() would
  230. * otherwise try to disable already disabled wakeirq. The wake-up interrupt
  231. * starts disabled with IRQ_NOAUTOEN set.
  232. *
  233. * Should be only called from rpm_suspend() and rpm_resume() path.
  234. * Caller must hold &dev->power.lock to change wirq->status
  235. */
  236. void dev_pm_enable_wake_irq_check(struct device *dev,
  237. bool can_change_status)
  238. {
  239. struct wake_irq *wirq = dev->power.wakeirq;
  240. if (!wirq || !((wirq->status & WAKE_IRQ_DEDICATED_MASK)))
  241. return;
  242. if (likely(wirq->status & WAKE_IRQ_DEDICATED_MANAGED)) {
  243. goto enable;
  244. } else if (can_change_status) {
  245. wirq->status |= WAKE_IRQ_DEDICATED_MANAGED;
  246. goto enable;
  247. }
  248. return;
  249. enable:
  250. enable_irq(wirq->irq);
  251. }
  252. /**
  253. * dev_pm_disable_wake_irq_check - Checks and disables wake-up interrupt
  254. * @dev: Device
  255. *
  256. * Disables wake-up interrupt conditionally based on status.
  257. * Should be only called from rpm_suspend() and rpm_resume() path.
  258. */
  259. void dev_pm_disable_wake_irq_check(struct device *dev)
  260. {
  261. struct wake_irq *wirq = dev->power.wakeirq;
  262. if (!wirq || !((wirq->status & WAKE_IRQ_DEDICATED_MASK)))
  263. return;
  264. if (wirq->status & WAKE_IRQ_DEDICATED_MANAGED)
  265. disable_irq_nosync(wirq->irq);
  266. }
  267. /**
  268. * dev_pm_arm_wake_irq - Arm device wake-up
  269. * @wirq: Device wake-up interrupt
  270. *
  271. * Sets up the wake-up event conditionally based on the
  272. * device_may_wake().
  273. */
  274. void dev_pm_arm_wake_irq(struct wake_irq *wirq)
  275. {
  276. if (!wirq)
  277. return;
  278. if (device_may_wakeup(wirq->dev)) {
  279. if (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED)
  280. enable_irq(wirq->irq);
  281. enable_irq_wake(wirq->irq);
  282. }
  283. }
  284. /**
  285. * dev_pm_disarm_wake_irq - Disarm device wake-up
  286. * @wirq: Device wake-up interrupt
  287. *
  288. * Clears up the wake-up event conditionally based on the
  289. * device_may_wake().
  290. */
  291. void dev_pm_disarm_wake_irq(struct wake_irq *wirq)
  292. {
  293. if (!wirq)
  294. return;
  295. if (device_may_wakeup(wirq->dev)) {
  296. disable_irq_wake(wirq->irq);
  297. if (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED)
  298. disable_irq_nosync(wirq->irq);
  299. }
  300. }