2 * wakeirq.c - Device wakeirq helper functions
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.
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.
14 #include <linux/device.h>
15 #include <linux/interrupt.h>
16 #include <linux/irq.h>
17 #include <linux/slab.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/pm_wakeirq.h>
24 * dev_pm_attach_wake_irq - Attach device interrupt as a wake IRQ
26 * @irq: Device wake-up capable interrupt
27 * @wirq: Wake irq specific data
29 * Internal function to attach either a device IO interrupt or a
30 * dedicated wake-up interrupt as a wake IRQ.
32 static int dev_pm_attach_wake_irq(struct device *dev, int irq,
33 struct wake_irq *wirq)
40 spin_lock_irqsave(&dev->power.lock, flags);
41 if (dev_WARN_ONCE(dev, dev->power.wakeirq,
42 "wake irq already initialized\n")) {
43 spin_unlock_irqrestore(&dev->power.lock, flags);
47 dev->power.wakeirq = wirq;
48 device_wakeup_attach_irq(dev, wirq);
50 spin_unlock_irqrestore(&dev->power.lock, flags);
55 * dev_pm_set_wake_irq - Attach device IO interrupt as wake IRQ
57 * @irq: Device IO interrupt
59 * Attach a device IO interrupt as a wake IRQ. The wake IRQ gets
60 * automatically configured for wake-up from suspend based
61 * on the device specific sysfs wakeup entry. Typically called
62 * during driver probe after calling device_init_wakeup().
64 int dev_pm_set_wake_irq(struct device *dev, int irq)
66 struct wake_irq *wirq;
72 wirq = kzalloc(sizeof(*wirq), GFP_KERNEL);
79 err = dev_pm_attach_wake_irq(dev, irq, wirq);
85 EXPORT_SYMBOL_GPL(dev_pm_set_wake_irq);
88 * dev_pm_clear_wake_irq - Detach a device IO interrupt wake IRQ
91 * Detach a device wake IRQ and free resources.
93 * Note that it's OK for drivers to call this without calling
94 * dev_pm_set_wake_irq() as all the driver instances may not have
95 * a wake IRQ configured. This avoid adding wake IRQ specific
96 * checks into the drivers.
98 void dev_pm_clear_wake_irq(struct device *dev)
100 struct wake_irq *wirq = dev->power.wakeirq;
106 spin_lock_irqsave(&dev->power.lock, flags);
107 device_wakeup_detach_irq(dev);
108 dev->power.wakeirq = NULL;
109 spin_unlock_irqrestore(&dev->power.lock, flags);
111 if (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED) {
112 free_irq(wirq->irq, wirq);
113 wirq->status &= ~WAKE_IRQ_DEDICATED_MASK;
118 EXPORT_SYMBOL_GPL(dev_pm_clear_wake_irq);
121 * handle_threaded_wake_irq - Handler for dedicated wake-up interrupts
122 * @irq: Device specific dedicated wake-up interrupt
123 * @_wirq: Wake IRQ data
125 * Some devices have a separate wake-up interrupt in addition to the
126 * device IO interrupt. The wake-up interrupt signals that a device
127 * should be woken up from it's idle state. This handler uses device
128 * specific pm_runtime functions to wake the device, and then it's
129 * up to the device to do whatever it needs to. Note that as the
130 * device may need to restore context and start up regulators, we
131 * use a threaded IRQ.
133 * Also note that we are not resending the lost device interrupts.
134 * We assume that the wake-up interrupt just needs to wake-up the
135 * device, and then device's pm_runtime_resume() can deal with the
138 static irqreturn_t handle_threaded_wake_irq(int irq, void *_wirq)
140 struct wake_irq *wirq = _wirq;
143 /* Maybe abort suspend? */
144 if (irqd_is_wakeup_set(irq_get_irq_data(irq))) {
145 pm_wakeup_event(wirq->dev, 0);
150 /* We don't want RPM_ASYNC or RPM_NOWAIT here */
151 res = pm_runtime_resume(wirq->dev);
154 "wake IRQ with no resume: %i\n", res);
160 * dev_pm_set_dedicated_wake_irq - Request a dedicated wake-up interrupt
162 * @irq: Device wake-up interrupt
164 * Unless your hardware has separate wake-up interrupts in addition
165 * to the device IO interrupts, you don't need this.
167 * Sets up a threaded interrupt handler for a device that has
168 * a dedicated wake-up interrupt in addition to the device IO
171 * The interrupt starts disabled, and needs to be managed for
172 * the device by the bus code or the device driver using
173 * dev_pm_enable_wake_irq() and dev_pm_disable_wake_irq()
176 int dev_pm_set_dedicated_wake_irq(struct device *dev, int irq)
178 struct wake_irq *wirq;
184 wirq = kzalloc(sizeof(*wirq), GFP_KERNEL);
188 wirq->name = kasprintf(GFP_KERNEL, "%s:wakeup", dev_name(dev));
196 irq_set_status_flags(irq, IRQ_NOAUTOEN);
198 /* Prevent deferred spurious wakeirqs with disable_irq_nosync() */
199 irq_set_status_flags(irq, IRQ_DISABLE_UNLAZY);
202 * Consumer device may need to power up and restore state
203 * so we use a threaded irq.
205 err = request_threaded_irq(irq, NULL, handle_threaded_wake_irq,
206 IRQF_ONESHOT, wirq->name, wirq);
210 err = dev_pm_attach_wake_irq(dev, irq, wirq);
214 wirq->status = WAKE_IRQ_DEDICATED_ALLOCATED;
227 EXPORT_SYMBOL_GPL(dev_pm_set_dedicated_wake_irq);
230 * dev_pm_enable_wake_irq - Enable device wake-up interrupt
233 * Optionally called from the bus code or the device driver for
234 * runtime_resume() to override the PM runtime core managed wake-up
235 * interrupt handling to enable the wake-up interrupt.
237 * Note that for runtime_suspend()) the wake-up interrupts
238 * should be unconditionally enabled unlike for suspend()
239 * that is conditional.
241 void dev_pm_enable_wake_irq(struct device *dev)
243 struct wake_irq *wirq = dev->power.wakeirq;
245 if (wirq && (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED))
246 enable_irq(wirq->irq);
248 EXPORT_SYMBOL_GPL(dev_pm_enable_wake_irq);
251 * dev_pm_disable_wake_irq - Disable device wake-up interrupt
254 * Optionally called from the bus code or the device driver for
255 * runtime_suspend() to override the PM runtime core managed wake-up
256 * interrupt handling to disable the wake-up interrupt.
258 void dev_pm_disable_wake_irq(struct device *dev)
260 struct wake_irq *wirq = dev->power.wakeirq;
262 if (wirq && (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED))
263 disable_irq_nosync(wirq->irq);
265 EXPORT_SYMBOL_GPL(dev_pm_disable_wake_irq);
268 * dev_pm_enable_wake_irq_check - Checks and enables wake-up interrupt
270 * @can_change_status: Can change wake-up interrupt status
272 * Enables wakeirq conditionally. We need to enable wake-up interrupt
273 * lazily on the first rpm_suspend(). This is needed as the consumer device
274 * starts in RPM_SUSPENDED state, and the the first pm_runtime_get() would
275 * otherwise try to disable already disabled wakeirq. The wake-up interrupt
276 * starts disabled with IRQ_NOAUTOEN set.
278 * Should be only called from rpm_suspend() and rpm_resume() path.
279 * Caller must hold &dev->power.lock to change wirq->status
281 void dev_pm_enable_wake_irq_check(struct device *dev,
282 bool can_change_status)
284 struct wake_irq *wirq = dev->power.wakeirq;
286 if (!wirq || !((wirq->status & WAKE_IRQ_DEDICATED_MASK)))
289 if (likely(wirq->status & WAKE_IRQ_DEDICATED_MANAGED)) {
291 } else if (can_change_status) {
292 wirq->status |= WAKE_IRQ_DEDICATED_MANAGED;
299 enable_irq(wirq->irq);
303 * dev_pm_disable_wake_irq_check - Checks and disables wake-up interrupt
306 * Disables wake-up interrupt conditionally based on status.
307 * Should be only called from rpm_suspend() and rpm_resume() path.
309 void dev_pm_disable_wake_irq_check(struct device *dev)
311 struct wake_irq *wirq = dev->power.wakeirq;
313 if (!wirq || !((wirq->status & WAKE_IRQ_DEDICATED_MASK)))
316 if (wirq->status & WAKE_IRQ_DEDICATED_MANAGED)
317 disable_irq_nosync(wirq->irq);
321 * dev_pm_arm_wake_irq - Arm device wake-up
322 * @wirq: Device wake-up interrupt
324 * Sets up the wake-up event conditionally based on the
327 void dev_pm_arm_wake_irq(struct wake_irq *wirq)
332 if (device_may_wakeup(wirq->dev)) {
333 if (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED &&
334 !pm_runtime_status_suspended(wirq->dev))
335 enable_irq(wirq->irq);
337 enable_irq_wake(wirq->irq);
342 * dev_pm_disarm_wake_irq - Disarm device wake-up
343 * @wirq: Device wake-up interrupt
345 * Clears up the wake-up event conditionally based on the
348 void dev_pm_disarm_wake_irq(struct wake_irq *wirq)
353 if (device_may_wakeup(wirq->dev)) {
354 disable_irq_wake(wirq->irq);
356 if (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED &&
357 !pm_runtime_status_suspended(wirq->dev))
358 disable_irq_nosync(wirq->irq);