IPoIB: Avoid reading an uninitialized member variable
[linux/fpc-iii.git] / drivers / base / power / wakeirq.c
blob0d77cd6fd8d11653c789fa8834e8eb53b801a6d9
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;
36 int err;
38 if (!dev || !wirq)
39 return -EINVAL;
41 spin_lock_irqsave(&dev->power.lock, flags);
42 if (dev_WARN_ONCE(dev, dev->power.wakeirq,
43 "wake irq already initialized\n")) {
44 spin_unlock_irqrestore(&dev->power.lock, flags);
45 return -EEXIST;
48 err = device_wakeup_attach_irq(dev, wirq);
49 if (!err)
50 dev->power.wakeirq = wirq;
52 spin_unlock_irqrestore(&dev->power.lock, flags);
53 return err;
56 /**
57 * dev_pm_set_wake_irq - Attach device IO interrupt as wake IRQ
58 * @dev: Device entry
59 * @irq: Device IO interrupt
61 * Attach a device IO interrupt as a wake IRQ. The wake IRQ gets
62 * automatically configured for wake-up from suspend based
63 * on the device specific sysfs wakeup entry. Typically called
64 * during driver probe after calling device_init_wakeup().
66 int dev_pm_set_wake_irq(struct device *dev, int irq)
68 struct wake_irq *wirq;
69 int err;
71 if (irq < 0)
72 return -EINVAL;
74 wirq = kzalloc(sizeof(*wirq), GFP_KERNEL);
75 if (!wirq)
76 return -ENOMEM;
78 wirq->dev = dev;
79 wirq->irq = irq;
81 err = dev_pm_attach_wake_irq(dev, irq, wirq);
82 if (err)
83 kfree(wirq);
85 return err;
87 EXPORT_SYMBOL_GPL(dev_pm_set_wake_irq);
89 /**
90 * dev_pm_clear_wake_irq - Detach a device IO interrupt wake IRQ
91 * @dev: Device entry
93 * Detach a device wake IRQ and free resources.
95 * Note that it's OK for drivers to call this without calling
96 * dev_pm_set_wake_irq() as all the driver instances may not have
97 * a wake IRQ configured. This avoid adding wake IRQ specific
98 * checks into the drivers.
100 void dev_pm_clear_wake_irq(struct device *dev)
102 struct wake_irq *wirq = dev->power.wakeirq;
103 unsigned long flags;
105 if (!wirq)
106 return;
108 spin_lock_irqsave(&dev->power.lock, flags);
109 device_wakeup_detach_irq(dev);
110 dev->power.wakeirq = NULL;
111 spin_unlock_irqrestore(&dev->power.lock, flags);
113 if (wirq->dedicated_irq)
114 free_irq(wirq->irq, wirq);
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 /* We don't want RPM_ASYNC or RPM_NOWAIT here */
143 res = pm_runtime_resume(wirq->dev);
144 if (res < 0)
145 dev_warn(wirq->dev,
146 "wake IRQ with no resume: %i\n", res);
148 return IRQ_HANDLED;
152 * dev_pm_set_dedicated_wake_irq - Request a dedicated wake-up interrupt
153 * @dev: Device entry
154 * @irq: Device wake-up interrupt
156 * Unless your hardware has separate wake-up interrupts in addition
157 * to the device IO interrupts, you don't need this.
159 * Sets up a threaded interrupt handler for a device that has
160 * a dedicated wake-up interrupt in addition to the device IO
161 * interrupt.
163 * The interrupt starts disabled, and needs to be managed for
164 * the device by the bus code or the device driver using
165 * dev_pm_enable_wake_irq() and dev_pm_disable_wake_irq()
166 * functions.
168 int dev_pm_set_dedicated_wake_irq(struct device *dev, int irq)
170 struct wake_irq *wirq;
171 int err;
173 if (irq < 0)
174 return -EINVAL;
176 wirq = kzalloc(sizeof(*wirq), GFP_KERNEL);
177 if (!wirq)
178 return -ENOMEM;
180 wirq->dev = dev;
181 wirq->irq = irq;
182 wirq->dedicated_irq = true;
183 irq_set_status_flags(irq, IRQ_NOAUTOEN);
186 * Consumer device may need to power up and restore state
187 * so we use a threaded irq.
189 err = request_threaded_irq(irq, NULL, handle_threaded_wake_irq,
190 IRQF_ONESHOT, dev_name(dev), wirq);
191 if (err)
192 goto err_free;
194 err = dev_pm_attach_wake_irq(dev, irq, wirq);
195 if (err)
196 goto err_free_irq;
198 return err;
200 err_free_irq:
201 free_irq(irq, wirq);
202 err_free:
203 kfree(wirq);
205 return err;
207 EXPORT_SYMBOL_GPL(dev_pm_set_dedicated_wake_irq);
210 * dev_pm_enable_wake_irq - Enable device wake-up interrupt
211 * @dev: Device
213 * Called from the bus code or the device driver for
214 * runtime_suspend() to enable the wake-up interrupt while
215 * the device is running.
217 * Note that for runtime_suspend()) the wake-up interrupts
218 * should be unconditionally enabled unlike for suspend()
219 * that is conditional.
221 void dev_pm_enable_wake_irq(struct device *dev)
223 struct wake_irq *wirq = dev->power.wakeirq;
225 if (wirq && wirq->dedicated_irq)
226 enable_irq(wirq->irq);
228 EXPORT_SYMBOL_GPL(dev_pm_enable_wake_irq);
231 * dev_pm_disable_wake_irq - Disable device wake-up interrupt
232 * @dev: Device
234 * Called from the bus code or the device driver for
235 * runtime_resume() to disable the wake-up interrupt while
236 * the device is running.
238 void dev_pm_disable_wake_irq(struct device *dev)
240 struct wake_irq *wirq = dev->power.wakeirq;
242 if (wirq && wirq->dedicated_irq)
243 disable_irq_nosync(wirq->irq);
245 EXPORT_SYMBOL_GPL(dev_pm_disable_wake_irq);
248 * dev_pm_arm_wake_irq - Arm device wake-up
249 * @wirq: Device wake-up interrupt
251 * Sets up the wake-up event conditionally based on the
252 * device_may_wake().
254 void dev_pm_arm_wake_irq(struct wake_irq *wirq)
256 if (!wirq)
257 return;
259 if (device_may_wakeup(wirq->dev))
260 enable_irq_wake(wirq->irq);
264 * dev_pm_disarm_wake_irq - Disarm device wake-up
265 * @wirq: Device wake-up interrupt
267 * Clears up the wake-up event conditionally based on the
268 * device_may_wake().
270 void dev_pm_disarm_wake_irq(struct wake_irq *wirq)
272 if (!wirq)
273 return;
275 if (device_may_wakeup(wirq->dev))
276 disable_irq_wake(wirq->irq);