xref: /openbmc/linux/drivers/base/power/wakeirq.c (revision 0ed66cb7)
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 
10 #include "power.h"
11 
12 /**
13  * dev_pm_attach_wake_irq - Attach device interrupt as a wake IRQ
14  * @dev: Device entry
15  * @wirq: Wake irq specific data
16  *
17  * Internal function to attach a dedicated wake-up interrupt as a wake IRQ.
18  */
19 static int dev_pm_attach_wake_irq(struct device *dev, struct wake_irq *wirq)
20 {
21 	unsigned long flags;
22 
23 	if (!dev || !wirq)
24 		return -EINVAL;
25 
26 	spin_lock_irqsave(&dev->power.lock, flags);
27 	if (dev_WARN_ONCE(dev, dev->power.wakeirq,
28 			  "wake irq already initialized\n")) {
29 		spin_unlock_irqrestore(&dev->power.lock, flags);
30 		return -EEXIST;
31 	}
32 
33 	dev->power.wakeirq = wirq;
34 	device_wakeup_attach_irq(dev, wirq);
35 
36 	spin_unlock_irqrestore(&dev->power.lock, flags);
37 	return 0;
38 }
39 
40 /**
41  * dev_pm_set_wake_irq - Attach device IO interrupt as wake IRQ
42  * @dev: Device entry
43  * @irq: Device IO interrupt
44  *
45  * Attach a device IO interrupt as a wake IRQ. The wake IRQ gets
46  * automatically configured for wake-up from suspend  based
47  * on the device specific sysfs wakeup entry. Typically called
48  * during driver probe after calling device_init_wakeup().
49  */
50 int dev_pm_set_wake_irq(struct device *dev, int irq)
51 {
52 	struct wake_irq *wirq;
53 	int err;
54 
55 	if (irq < 0)
56 		return -EINVAL;
57 
58 	wirq = kzalloc(sizeof(*wirq), GFP_KERNEL);
59 	if (!wirq)
60 		return -ENOMEM;
61 
62 	wirq->dev = dev;
63 	wirq->irq = irq;
64 
65 	err = dev_pm_attach_wake_irq(dev, wirq);
66 	if (err)
67 		kfree(wirq);
68 
69 	return err;
70 }
71 EXPORT_SYMBOL_GPL(dev_pm_set_wake_irq);
72 
73 /**
74  * dev_pm_clear_wake_irq - Detach a device IO interrupt wake IRQ
75  * @dev: Device entry
76  *
77  * Detach a device wake IRQ and free resources.
78  *
79  * Note that it's OK for drivers to call this without calling
80  * dev_pm_set_wake_irq() as all the driver instances may not have
81  * a wake IRQ configured. This avoid adding wake IRQ specific
82  * checks into the drivers.
83  */
84 void dev_pm_clear_wake_irq(struct device *dev)
85 {
86 	struct wake_irq *wirq = dev->power.wakeirq;
87 	unsigned long flags;
88 
89 	if (!wirq)
90 		return;
91 
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 
97 	if (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED) {
98 		free_irq(wirq->irq, wirq);
99 		wirq->status &= ~WAKE_IRQ_DEDICATED_MASK;
100 	}
101 	kfree(wirq->name);
102 	kfree(wirq);
103 }
104 EXPORT_SYMBOL_GPL(dev_pm_clear_wake_irq);
105 
106 /**
107  * handle_threaded_wake_irq - Handler for dedicated wake-up interrupts
108  * @irq: Device specific dedicated wake-up interrupt
109  * @_wirq: Wake IRQ data
110  *
111  * Some devices have a separate wake-up interrupt in addition to the
112  * device IO interrupt. The wake-up interrupt signals that a device
113  * should be woken up from it's idle state. This handler uses device
114  * specific pm_runtime functions to wake the device, and then it's
115  * up to the device to do whatever it needs to. Note that as the
116  * device may need to restore context and start up regulators, we
117  * use a threaded IRQ.
118  *
119  * Also note that we are not resending the lost device interrupts.
120  * We assume that the wake-up interrupt just needs to wake-up the
121  * device, and then device's pm_runtime_resume() can deal with the
122  * situation.
123  */
124 static irqreturn_t handle_threaded_wake_irq(int irq, void *_wirq)
125 {
126 	struct wake_irq *wirq = _wirq;
127 	int res;
128 
129 	/* Maybe abort suspend? */
130 	if (irqd_is_wakeup_set(irq_get_irq_data(irq))) {
131 		pm_wakeup_event(wirq->dev, 0);
132 
133 		return IRQ_HANDLED;
134 	}
135 
136 	/* We don't want RPM_ASYNC or RPM_NOWAIT here */
137 	res = pm_runtime_resume(wirq->dev);
138 	if (res < 0)
139 		dev_warn(wirq->dev,
140 			 "wake IRQ with no resume: %i\n", res);
141 
142 	return IRQ_HANDLED;
143 }
144 
145 /**
146  * dev_pm_set_dedicated_wake_irq - Request a dedicated wake-up interrupt
147  * @dev: Device entry
148  * @irq: Device wake-up interrupt
149  *
150  * Unless your hardware has separate wake-up interrupts in addition
151  * to the device IO interrupts, you don't need this.
152  *
153  * Sets up a threaded interrupt handler for a device that has
154  * a dedicated wake-up interrupt in addition to the device IO
155  * interrupt.
156  *
157  * The interrupt starts disabled, and needs to be managed for
158  * the device by the bus code or the device driver using
159  * dev_pm_enable_wake_irq() and dev_pm_disable_wake_irq()
160  * functions.
161  */
162 int dev_pm_set_dedicated_wake_irq(struct device *dev, int irq)
163 {
164 	struct wake_irq *wirq;
165 	int err;
166 
167 	if (irq < 0)
168 		return -EINVAL;
169 
170 	wirq = kzalloc(sizeof(*wirq), GFP_KERNEL);
171 	if (!wirq)
172 		return -ENOMEM;
173 
174 	wirq->name = kasprintf(GFP_KERNEL, "%s:wakeup", dev_name(dev));
175 	if (!wirq->name) {
176 		err = -ENOMEM;
177 		goto err_free;
178 	}
179 
180 	wirq->dev = dev;
181 	wirq->irq = irq;
182 
183 	/* Prevent deferred spurious wakeirqs with disable_irq_nosync() */
184 	irq_set_status_flags(irq, IRQ_DISABLE_UNLAZY);
185 
186 	/*
187 	 * Consumer device may need to power up and restore state
188 	 * so we use a threaded irq.
189 	 */
190 	err = request_threaded_irq(irq, NULL, handle_threaded_wake_irq,
191 				   IRQF_ONESHOT | IRQF_NO_AUTOEN,
192 				   wirq->name, wirq);
193 	if (err)
194 		goto err_free_name;
195 
196 	err = dev_pm_attach_wake_irq(dev, wirq);
197 	if (err)
198 		goto err_free_irq;
199 
200 	wirq->status = WAKE_IRQ_DEDICATED_ALLOCATED;
201 
202 	return err;
203 
204 err_free_irq:
205 	free_irq(irq, wirq);
206 err_free_name:
207 	kfree(wirq->name);
208 err_free:
209 	kfree(wirq);
210 
211 	return err;
212 }
213 EXPORT_SYMBOL_GPL(dev_pm_set_dedicated_wake_irq);
214 
215 /**
216  * dev_pm_enable_wake_irq - Enable device wake-up interrupt
217  * @dev: Device
218  *
219  * Optionally called from the bus code or the device driver for
220  * runtime_resume() to override the PM runtime core managed wake-up
221  * interrupt handling to enable the wake-up interrupt.
222  *
223  * Note that for runtime_suspend()) the wake-up interrupts
224  * should be unconditionally enabled unlike for suspend()
225  * that is conditional.
226  */
227 void dev_pm_enable_wake_irq(struct device *dev)
228 {
229 	struct wake_irq *wirq = dev->power.wakeirq;
230 
231 	if (wirq && (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED))
232 		enable_irq(wirq->irq);
233 }
234 EXPORT_SYMBOL_GPL(dev_pm_enable_wake_irq);
235 
236 /**
237  * dev_pm_disable_wake_irq - Disable device wake-up interrupt
238  * @dev: Device
239  *
240  * Optionally called from the bus code or the device driver for
241  * runtime_suspend() to override the PM runtime core managed wake-up
242  * interrupt handling to disable the wake-up interrupt.
243  */
244 void dev_pm_disable_wake_irq(struct device *dev)
245 {
246 	struct wake_irq *wirq = dev->power.wakeirq;
247 
248 	if (wirq && (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED))
249 		disable_irq_nosync(wirq->irq);
250 }
251 EXPORT_SYMBOL_GPL(dev_pm_disable_wake_irq);
252 
253 /**
254  * dev_pm_enable_wake_irq_check - Checks and enables wake-up interrupt
255  * @dev: Device
256  * @can_change_status: Can change wake-up interrupt status
257  *
258  * Enables wakeirq conditionally. We need to enable wake-up interrupt
259  * lazily on the first rpm_suspend(). This is needed as the consumer device
260  * starts in RPM_SUSPENDED state, and the the first pm_runtime_get() would
261  * otherwise try to disable already disabled wakeirq. The wake-up interrupt
262  * starts disabled with IRQ_NOAUTOEN set.
263  *
264  * Should be only called from rpm_suspend() and rpm_resume() path.
265  * Caller must hold &dev->power.lock to change wirq->status
266  */
267 void dev_pm_enable_wake_irq_check(struct device *dev,
268 				  bool can_change_status)
269 {
270 	struct wake_irq *wirq = dev->power.wakeirq;
271 
272 	if (!wirq || !(wirq->status & WAKE_IRQ_DEDICATED_MASK))
273 		return;
274 
275 	if (likely(wirq->status & WAKE_IRQ_DEDICATED_MANAGED)) {
276 		goto enable;
277 	} else if (can_change_status) {
278 		wirq->status |= WAKE_IRQ_DEDICATED_MANAGED;
279 		goto enable;
280 	}
281 
282 	return;
283 
284 enable:
285 	enable_irq(wirq->irq);
286 }
287 
288 /**
289  * dev_pm_disable_wake_irq_check - Checks and disables wake-up interrupt
290  * @dev: Device
291  *
292  * Disables wake-up interrupt conditionally based on status.
293  * Should be only called from rpm_suspend() and rpm_resume() path.
294  */
295 void dev_pm_disable_wake_irq_check(struct device *dev)
296 {
297 	struct wake_irq *wirq = dev->power.wakeirq;
298 
299 	if (!wirq || !(wirq->status & WAKE_IRQ_DEDICATED_MASK))
300 		return;
301 
302 	if (wirq->status & WAKE_IRQ_DEDICATED_MANAGED)
303 		disable_irq_nosync(wirq->irq);
304 }
305 
306 /**
307  * dev_pm_arm_wake_irq - Arm device wake-up
308  * @wirq: Device wake-up interrupt
309  *
310  * Sets up the wake-up event conditionally based on the
311  * device_may_wake().
312  */
313 void dev_pm_arm_wake_irq(struct wake_irq *wirq)
314 {
315 	if (!wirq)
316 		return;
317 
318 	if (device_may_wakeup(wirq->dev)) {
319 		if (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED &&
320 		    !pm_runtime_status_suspended(wirq->dev))
321 			enable_irq(wirq->irq);
322 
323 		enable_irq_wake(wirq->irq);
324 	}
325 }
326 
327 /**
328  * dev_pm_disarm_wake_irq - Disarm device wake-up
329  * @wirq: Device wake-up interrupt
330  *
331  * Clears up the wake-up event conditionally based on the
332  * device_may_wake().
333  */
334 void dev_pm_disarm_wake_irq(struct wake_irq *wirq)
335 {
336 	if (!wirq)
337 		return;
338 
339 	if (device_may_wakeup(wirq->dev)) {
340 		disable_irq_wake(wirq->irq);
341 
342 		if (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED &&
343 		    !pm_runtime_status_suspended(wirq->dev))
344 			disable_irq_nosync(wirq->irq);
345 	}
346 }
347