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