Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / base / power / wakeirq.c
blob6637fc319269ba9f93a4c7ae44422723656033c8
1 /*
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>
21 #include "power.h"
23 /**
24 * dev_pm_attach_wake_irq - Attach device interrupt as a wake IRQ
25 * @dev: Device entry
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)
35 unsigned long flags;
37 if (!dev || !wirq)
38 return -EINVAL;
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);
44 return -EEXIST;
47 dev->power.wakeirq = wirq;
48 device_wakeup_attach_irq(dev, wirq);
50 spin_unlock_irqrestore(&dev->power.lock, flags);
51 return 0;
54 /**
55 * dev_pm_set_wake_irq - Attach device IO interrupt as wake IRQ
56 * @dev: Device entry
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;
67 int err;
69 if (irq < 0)
70 return -EINVAL;
72 wirq = kzalloc(sizeof(*wirq), GFP_KERNEL);
73 if (!wirq)
74 return -ENOMEM;
76 wirq->dev = dev;
77 wirq->irq = irq;
79 err = dev_pm_attach_wake_irq(dev, irq, wirq);
80 if (err)
81 kfree(wirq);
83 return err;
85 EXPORT_SYMBOL_GPL(dev_pm_set_wake_irq);
87 /**
88 * dev_pm_clear_wake_irq - Detach a device IO interrupt wake IRQ
89 * @dev: Device entry
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;
101 unsigned long flags;
103 if (!wirq)
104 return;
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;
115 kfree(wirq);
117 EXPORT_SYMBOL_GPL(dev_pm_clear_wake_irq);
120 * handle_threaded_wake_irq - Handler for dedicated wake-up interrupts
121 * @irq: Device specific dedicated wake-up interrupt
122 * @_wirq: Wake IRQ data
124 * Some devices have a separate wake-up interrupt in addition to the
125 * device IO interrupt. The wake-up interrupt signals that a device
126 * should be woken up from it's idle state. This handler uses device
127 * specific pm_runtime functions to wake the device, and then it's
128 * up to the device to do whatever it needs to. Note that as the
129 * device may need to restore context and start up regulators, we
130 * use a threaded IRQ.
132 * Also note that we are not resending the lost device interrupts.
133 * We assume that the wake-up interrupt just needs to wake-up the
134 * device, and then device's pm_runtime_resume() can deal with the
135 * situation.
137 static irqreturn_t handle_threaded_wake_irq(int irq, void *_wirq)
139 struct wake_irq *wirq = _wirq;
140 int res;
142 /* Maybe abort suspend? */
143 if (irqd_is_wakeup_set(irq_get_irq_data(irq))) {
144 pm_wakeup_event(wirq->dev, 0);
146 return IRQ_HANDLED;
149 /* We don't want RPM_ASYNC or RPM_NOWAIT here */
150 res = pm_runtime_resume(wirq->dev);
151 if (res < 0)
152 dev_warn(wirq->dev,
153 "wake IRQ with no resume: %i\n", res);
155 return IRQ_HANDLED;
159 * dev_pm_set_dedicated_wake_irq - Request a dedicated wake-up interrupt
160 * @dev: Device entry
161 * @irq: Device wake-up interrupt
163 * Unless your hardware has separate wake-up interrupts in addition
164 * to the device IO interrupts, you don't need this.
166 * Sets up a threaded interrupt handler for a device that has
167 * a dedicated wake-up interrupt in addition to the device IO
168 * interrupt.
170 * The interrupt starts disabled, and needs to be managed for
171 * the device by the bus code or the device driver using
172 * dev_pm_enable_wake_irq() and dev_pm_disable_wake_irq()
173 * functions.
175 int dev_pm_set_dedicated_wake_irq(struct device *dev, int irq)
177 struct wake_irq *wirq;
178 int err;
180 if (irq < 0)
181 return -EINVAL;
183 wirq = kzalloc(sizeof(*wirq), GFP_KERNEL);
184 if (!wirq)
185 return -ENOMEM;
187 wirq->dev = dev;
188 wirq->irq = irq;
189 irq_set_status_flags(irq, IRQ_NOAUTOEN);
191 /* Prevent deferred spurious wakeirqs with disable_irq_nosync() */
192 irq_set_status_flags(irq, IRQ_DISABLE_UNLAZY);
195 * Consumer device may need to power up and restore state
196 * so we use a threaded irq.
198 err = request_threaded_irq(irq, NULL, handle_threaded_wake_irq,
199 IRQF_ONESHOT, dev_name(dev), wirq);
200 if (err)
201 goto err_free;
203 err = dev_pm_attach_wake_irq(dev, irq, wirq);
204 if (err)
205 goto err_free_irq;
207 wirq->status = WAKE_IRQ_DEDICATED_ALLOCATED;
209 return err;
211 err_free_irq:
212 free_irq(irq, wirq);
213 err_free:
214 kfree(wirq);
216 return err;
218 EXPORT_SYMBOL_GPL(dev_pm_set_dedicated_wake_irq);
221 * dev_pm_enable_wake_irq - Enable device wake-up interrupt
222 * @dev: Device
224 * Optionally called from the bus code or the device driver for
225 * runtime_resume() to override the PM runtime core managed wake-up
226 * interrupt handling to enable the wake-up interrupt.
228 * Note that for runtime_suspend()) the wake-up interrupts
229 * should be unconditionally enabled unlike for suspend()
230 * that is conditional.
232 void dev_pm_enable_wake_irq(struct device *dev)
234 struct wake_irq *wirq = dev->power.wakeirq;
236 if (wirq && (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED))
237 enable_irq(wirq->irq);
239 EXPORT_SYMBOL_GPL(dev_pm_enable_wake_irq);
242 * dev_pm_disable_wake_irq - Disable device wake-up interrupt
243 * @dev: Device
245 * Optionally called from the bus code or the device driver for
246 * runtime_suspend() to override the PM runtime core managed wake-up
247 * interrupt handling to disable the wake-up interrupt.
249 void dev_pm_disable_wake_irq(struct device *dev)
251 struct wake_irq *wirq = dev->power.wakeirq;
253 if (wirq && (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED))
254 disable_irq_nosync(wirq->irq);
256 EXPORT_SYMBOL_GPL(dev_pm_disable_wake_irq);
259 * dev_pm_enable_wake_irq_check - Checks and enables wake-up interrupt
260 * @dev: Device
261 * @can_change_status: Can change wake-up interrupt status
263 * Enables wakeirq conditionally. We need to enable wake-up interrupt
264 * lazily on the first rpm_suspend(). This is needed as the consumer device
265 * starts in RPM_SUSPENDED state, and the the first pm_runtime_get() would
266 * otherwise try to disable already disabled wakeirq. The wake-up interrupt
267 * starts disabled with IRQ_NOAUTOEN set.
269 * Should be only called from rpm_suspend() and rpm_resume() path.
270 * Caller must hold &dev->power.lock to change wirq->status
272 void dev_pm_enable_wake_irq_check(struct device *dev,
273 bool can_change_status)
275 struct wake_irq *wirq = dev->power.wakeirq;
277 if (!wirq || !((wirq->status & WAKE_IRQ_DEDICATED_MASK)))
278 return;
280 if (likely(wirq->status & WAKE_IRQ_DEDICATED_MANAGED)) {
281 goto enable;
282 } else if (can_change_status) {
283 wirq->status |= WAKE_IRQ_DEDICATED_MANAGED;
284 goto enable;
287 return;
289 enable:
290 enable_irq(wirq->irq);
294 * dev_pm_disable_wake_irq_check - Checks and disables wake-up interrupt
295 * @dev: Device
297 * Disables wake-up interrupt conditionally based on status.
298 * Should be only called from rpm_suspend() and rpm_resume() path.
300 void dev_pm_disable_wake_irq_check(struct device *dev)
302 struct wake_irq *wirq = dev->power.wakeirq;
304 if (!wirq || !((wirq->status & WAKE_IRQ_DEDICATED_MASK)))
305 return;
307 if (wirq->status & WAKE_IRQ_DEDICATED_MANAGED)
308 disable_irq_nosync(wirq->irq);
312 * dev_pm_arm_wake_irq - Arm device wake-up
313 * @wirq: Device wake-up interrupt
315 * Sets up the wake-up event conditionally based on the
316 * device_may_wake().
318 void dev_pm_arm_wake_irq(struct wake_irq *wirq)
320 if (!wirq)
321 return;
323 if (device_may_wakeup(wirq->dev)) {
324 if (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED &&
325 !pm_runtime_status_suspended(wirq->dev))
326 enable_irq(wirq->irq);
328 enable_irq_wake(wirq->irq);
333 * dev_pm_disarm_wake_irq - Disarm device wake-up
334 * @wirq: Device wake-up interrupt
336 * Clears up the wake-up event conditionally based on the
337 * device_may_wake().
339 void dev_pm_disarm_wake_irq(struct wake_irq *wirq)
341 if (!wirq)
342 return;
344 if (device_may_wakeup(wirq->dev)) {
345 disable_irq_wake(wirq->irq);
347 if (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED &&
348 !pm_runtime_status_suspended(wirq->dev))
349 disable_irq_nosync(wirq->irq);