1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * pm_runtime.h - Device run-time power management helper functions.
4 *
5 * Copyright (C) 2009 Rafael J. Wysocki <rjw@sisk.pl>
6 */
7
8 #ifndef _LINUX_PM_RUNTIME_H
9 #define _LINUX_PM_RUNTIME_H
10
11 #include <linux/device.h>
12 #include <linux/notifier.h>
13 #include <linux/pm.h>
14
15 #include <linux/jiffies.h>
16
17 /* Runtime PM flag argument bits */
18 #define RPM_ASYNC 0x01 /* Request is asynchronous */
19 #define RPM_NOWAIT 0x02 /* Don't wait for concurrent
20 state change */
21 #define RPM_GET_PUT 0x04 /* Increment/decrement the
22 usage_count */
23 #define RPM_AUTO 0x08 /* Use autosuspend_delay */
24 #define RPM_TRANSPARENT 0x10 /* Succeed if runtime PM is disabled */
25
26 /*
27 * Use this for defining a set of PM operations to be used in all situations
28 * (system suspend, hibernation or runtime PM).
29 *
30 * Note that the behaviour differs from the deprecated UNIVERSAL_DEV_PM_OPS()
31 * macro, which uses the provided callbacks for both runtime PM and system
32 * sleep, while DEFINE_RUNTIME_DEV_PM_OPS() uses pm_runtime_force_suspend()
33 * and pm_runtime_force_resume() for its system sleep callbacks.
34 *
35 * If the underlying dev_pm_ops struct symbol has to be exported, use
36 * EXPORT_RUNTIME_DEV_PM_OPS() or EXPORT_GPL_RUNTIME_DEV_PM_OPS() instead.
37 */
38 #define DEFINE_RUNTIME_DEV_PM_OPS(name, suspend_fn, resume_fn, idle_fn) \
39 _DEFINE_DEV_PM_OPS(name, pm_runtime_force_suspend, \
40 pm_runtime_force_resume, suspend_fn, \
41 resume_fn, idle_fn)
42
43 #define EXPORT_RUNTIME_DEV_PM_OPS(name, suspend_fn, resume_fn, idle_fn) \
44 EXPORT_DEV_PM_OPS(name) = { \
45 RUNTIME_PM_OPS(suspend_fn, resume_fn, idle_fn) \
46 }
47 #define EXPORT_GPL_RUNTIME_DEV_PM_OPS(name, suspend_fn, resume_fn, idle_fn) \
48 EXPORT_GPL_DEV_PM_OPS(name) = { \
49 RUNTIME_PM_OPS(suspend_fn, resume_fn, idle_fn) \
50 }
51 #define EXPORT_NS_RUNTIME_DEV_PM_OPS(name, suspend_fn, resume_fn, idle_fn, ns) \
52 EXPORT_NS_DEV_PM_OPS(name, ns) = { \
53 RUNTIME_PM_OPS(suspend_fn, resume_fn, idle_fn) \
54 }
55 #define EXPORT_NS_GPL_RUNTIME_DEV_PM_OPS(name, suspend_fn, resume_fn, idle_fn, ns) \
56 EXPORT_NS_GPL_DEV_PM_OPS(name, ns) = { \
57 RUNTIME_PM_OPS(suspend_fn, resume_fn, idle_fn) \
58 }
59
60 #ifdef CONFIG_PM
61 extern struct workqueue_struct *pm_wq;
62
queue_pm_work(struct work_struct * work)63 static inline bool queue_pm_work(struct work_struct *work)
64 {
65 return queue_work(pm_wq, work);
66 }
67
68 extern int pm_generic_runtime_suspend(struct device *dev);
69 extern int pm_generic_runtime_resume(struct device *dev);
70 extern int pm_runtime_force_suspend(struct device *dev);
71
72 extern int __pm_runtime_idle(struct device *dev, int rpmflags);
73 extern int __pm_runtime_suspend(struct device *dev, int rpmflags);
74 extern int __pm_runtime_resume(struct device *dev, int rpmflags);
75 extern int pm_runtime_get_if_active(struct device *dev);
76 extern int pm_runtime_get_if_in_use(struct device *dev);
77 extern int pm_schedule_suspend(struct device *dev, unsigned int delay);
78 extern int __pm_runtime_set_status(struct device *dev, unsigned int status);
79 extern int pm_runtime_barrier(struct device *dev);
80 extern bool pm_runtime_block_if_disabled(struct device *dev);
81 extern void pm_runtime_unblock(struct device *dev);
82 extern void pm_runtime_enable(struct device *dev);
83 extern void __pm_runtime_disable(struct device *dev, bool check_resume);
84 extern void pm_runtime_allow(struct device *dev);
85 extern void pm_runtime_forbid(struct device *dev);
86 extern void pm_runtime_no_callbacks(struct device *dev);
87 extern void pm_runtime_irq_safe(struct device *dev);
88 extern void __pm_runtime_use_autosuspend(struct device *dev, bool use);
89 extern void pm_runtime_set_autosuspend_delay(struct device *dev, int delay);
90 extern u64 pm_runtime_autosuspend_expiration(struct device *dev);
91 extern void pm_runtime_set_memalloc_noio(struct device *dev, bool enable);
92 extern void pm_runtime_get_suppliers(struct device *dev);
93 extern void pm_runtime_put_suppliers(struct device *dev);
94 extern void pm_runtime_new_link(struct device *dev);
95 extern void pm_runtime_drop_link(struct device_link *link);
96 extern void pm_runtime_release_supplier(struct device_link *link);
97
98 int devm_pm_runtime_set_active_enabled(struct device *dev);
99 extern int devm_pm_runtime_enable(struct device *dev);
100 int devm_pm_runtime_get_noresume(struct device *dev);
101
102 /**
103 * pm_suspend_ignore_children - Set runtime PM behavior regarding children.
104 * @dev: Target device.
105 * @enable: Whether or not to ignore possible dependencies on children.
106 *
107 * The dependencies of @dev on its children will not be taken into account by
108 * the runtime PM framework going forward if @enable is %true, or they will
109 * be taken into account otherwise.
110 */
pm_suspend_ignore_children(struct device * dev,bool enable)111 static inline void pm_suspend_ignore_children(struct device *dev, bool enable)
112 {
113 dev->power.ignore_children = enable;
114 }
115
116 /**
117 * pm_runtime_get_noresume - Bump up runtime PM usage counter of a device.
118 * @dev: Target device.
119 */
pm_runtime_get_noresume(struct device * dev)120 static inline void pm_runtime_get_noresume(struct device *dev)
121 {
122 atomic_inc(&dev->power.usage_count);
123 }
124
125 /**
126 * pm_runtime_put_noidle - Drop runtime PM usage counter of a device.
127 * @dev: Target device.
128 *
129 * Decrement the runtime PM usage counter of @dev unless it is 0 already.
130 */
pm_runtime_put_noidle(struct device * dev)131 static inline void pm_runtime_put_noidle(struct device *dev)
132 {
133 atomic_add_unless(&dev->power.usage_count, -1, 0);
134 }
135
136 /**
137 * pm_runtime_suspended - Check whether or not a device is runtime-suspended.
138 * @dev: Target device.
139 *
140 * Return %true if runtime PM is enabled for @dev and its runtime PM status is
141 * %RPM_SUSPENDED, or %false otherwise.
142 *
143 * Note that the return value of this function can only be trusted if it is
144 * called under the runtime PM lock of @dev or under conditions in which
145 * runtime PM cannot be either disabled or enabled for @dev and its runtime PM
146 * status cannot change.
147 */
pm_runtime_suspended(struct device * dev)148 static inline bool pm_runtime_suspended(struct device *dev)
149 {
150 return dev->power.runtime_status == RPM_SUSPENDED
151 && !dev->power.disable_depth;
152 }
153
154 /**
155 * pm_runtime_active - Check whether or not a device is runtime-active.
156 * @dev: Target device.
157 *
158 * Return %true if runtime PM is disabled for @dev or its runtime PM status is
159 * %RPM_ACTIVE, or %false otherwise.
160 *
161 * Note that the return value of this function can only be trusted if it is
162 * called under the runtime PM lock of @dev or under conditions in which
163 * runtime PM cannot be either disabled or enabled for @dev and its runtime PM
164 * status cannot change.
165 */
pm_runtime_active(struct device * dev)166 static inline bool pm_runtime_active(struct device *dev)
167 {
168 return dev->power.runtime_status == RPM_ACTIVE
169 || dev->power.disable_depth;
170 }
171
172 /**
173 * pm_runtime_status_suspended - Check if runtime PM status is "suspended".
174 * @dev: Target device.
175 *
176 * Return %true if the runtime PM status of @dev is %RPM_SUSPENDED, or %false
177 * otherwise, regardless of whether or not runtime PM has been enabled for @dev.
178 *
179 * Note that the return value of this function can only be trusted if it is
180 * called under the runtime PM lock of @dev or under conditions in which the
181 * runtime PM status of @dev cannot change.
182 */
pm_runtime_status_suspended(struct device * dev)183 static inline bool pm_runtime_status_suspended(struct device *dev)
184 {
185 return dev->power.runtime_status == RPM_SUSPENDED;
186 }
187
188 /**
189 * pm_runtime_enabled - Check if runtime PM is enabled.
190 * @dev: Target device.
191 *
192 * Return %true if runtime PM is enabled for @dev or %false otherwise.
193 *
194 * Note that the return value of this function can only be trusted if it is
195 * called under the runtime PM lock of @dev or under conditions in which
196 * runtime PM cannot be either disabled or enabled for @dev.
197 */
pm_runtime_enabled(struct device * dev)198 static inline bool pm_runtime_enabled(struct device *dev)
199 {
200 return !dev->power.disable_depth;
201 }
202
203 /**
204 * pm_runtime_blocked - Check if runtime PM enabling is blocked.
205 * @dev: Target device.
206 *
207 * Do not call this function outside system suspend/resume code paths.
208 */
pm_runtime_blocked(struct device * dev)209 static inline bool pm_runtime_blocked(struct device *dev)
210 {
211 return dev->power.last_status == RPM_BLOCKED;
212 }
213
214 /**
215 * pm_runtime_has_no_callbacks - Check if runtime PM callbacks may be present.
216 * @dev: Target device.
217 *
218 * Return %true if @dev is a special device without runtime PM callbacks or
219 * %false otherwise.
220 */
pm_runtime_has_no_callbacks(struct device * dev)221 static inline bool pm_runtime_has_no_callbacks(struct device *dev)
222 {
223 return dev->power.no_callbacks;
224 }
225
226 /**
227 * pm_runtime_mark_last_busy - Update the last access time of a device.
228 * @dev: Target device.
229 *
230 * Update the last access time of @dev used by the runtime PM autosuspend
231 * mechanism to the current time as returned by ktime_get_mono_fast_ns().
232 */
pm_runtime_mark_last_busy(struct device * dev)233 static inline void pm_runtime_mark_last_busy(struct device *dev)
234 {
235 WRITE_ONCE(dev->power.last_busy, ktime_get_mono_fast_ns());
236 }
237
238 /**
239 * pm_runtime_is_irq_safe - Check if runtime PM can work in interrupt context.
240 * @dev: Target device.
241 *
242 * Return %true if @dev has been marked as an "IRQ-safe" device (with respect
243 * to runtime PM), in which case its runtime PM callabcks can be expected to
244 * work correctly when invoked from interrupt handlers.
245 */
pm_runtime_is_irq_safe(struct device * dev)246 static inline bool pm_runtime_is_irq_safe(struct device *dev)
247 {
248 return dev->power.irq_safe;
249 }
250
251 extern u64 pm_runtime_suspended_time(struct device *dev);
252
253 #else /* !CONFIG_PM */
254
queue_pm_work(struct work_struct * work)255 static inline bool queue_pm_work(struct work_struct *work) { return false; }
256
pm_generic_runtime_suspend(struct device * dev)257 static inline int pm_generic_runtime_suspend(struct device *dev) { return 0; }
pm_generic_runtime_resume(struct device * dev)258 static inline int pm_generic_runtime_resume(struct device *dev) { return 0; }
pm_runtime_force_suspend(struct device * dev)259 static inline int pm_runtime_force_suspend(struct device *dev) { return 0; }
260
__pm_runtime_idle(struct device * dev,int rpmflags)261 static inline int __pm_runtime_idle(struct device *dev, int rpmflags)
262 {
263 return -ENOSYS;
264 }
__pm_runtime_suspend(struct device * dev,int rpmflags)265 static inline int __pm_runtime_suspend(struct device *dev, int rpmflags)
266 {
267 return -ENOSYS;
268 }
__pm_runtime_resume(struct device * dev,int rpmflags)269 static inline int __pm_runtime_resume(struct device *dev, int rpmflags)
270 {
271 return 1;
272 }
pm_schedule_suspend(struct device * dev,unsigned int delay)273 static inline int pm_schedule_suspend(struct device *dev, unsigned int delay)
274 {
275 return -ENOSYS;
276 }
pm_runtime_get_if_in_use(struct device * dev)277 static inline int pm_runtime_get_if_in_use(struct device *dev)
278 {
279 return -EINVAL;
280 }
pm_runtime_get_if_active(struct device * dev)281 static inline int pm_runtime_get_if_active(struct device *dev)
282 {
283 return -EINVAL;
284 }
__pm_runtime_set_status(struct device * dev,unsigned int status)285 static inline int __pm_runtime_set_status(struct device *dev,
286 unsigned int status) { return 0; }
pm_runtime_barrier(struct device * dev)287 static inline int pm_runtime_barrier(struct device *dev) { return 0; }
pm_runtime_block_if_disabled(struct device * dev)288 static inline bool pm_runtime_block_if_disabled(struct device *dev) { return true; }
pm_runtime_unblock(struct device * dev)289 static inline void pm_runtime_unblock(struct device *dev) {}
pm_runtime_enable(struct device * dev)290 static inline void pm_runtime_enable(struct device *dev) {}
__pm_runtime_disable(struct device * dev,bool c)291 static inline void __pm_runtime_disable(struct device *dev, bool c) {}
pm_runtime_blocked(struct device * dev)292 static inline bool pm_runtime_blocked(struct device *dev) { return true; }
pm_runtime_allow(struct device * dev)293 static inline void pm_runtime_allow(struct device *dev) {}
pm_runtime_forbid(struct device * dev)294 static inline void pm_runtime_forbid(struct device *dev) {}
295
devm_pm_runtime_set_active_enabled(struct device * dev)296 static inline int devm_pm_runtime_set_active_enabled(struct device *dev) { return 0; }
devm_pm_runtime_enable(struct device * dev)297 static inline int devm_pm_runtime_enable(struct device *dev) { return 0; }
devm_pm_runtime_get_noresume(struct device * dev)298 static inline int devm_pm_runtime_get_noresume(struct device *dev) { return 0; }
299
pm_suspend_ignore_children(struct device * dev,bool enable)300 static inline void pm_suspend_ignore_children(struct device *dev, bool enable) {}
pm_runtime_get_noresume(struct device * dev)301 static inline void pm_runtime_get_noresume(struct device *dev) {}
pm_runtime_put_noidle(struct device * dev)302 static inline void pm_runtime_put_noidle(struct device *dev) {}
pm_runtime_suspended(struct device * dev)303 static inline bool pm_runtime_suspended(struct device *dev) { return false; }
pm_runtime_active(struct device * dev)304 static inline bool pm_runtime_active(struct device *dev) { return true; }
pm_runtime_status_suspended(struct device * dev)305 static inline bool pm_runtime_status_suspended(struct device *dev) { return false; }
pm_runtime_enabled(struct device * dev)306 static inline bool pm_runtime_enabled(struct device *dev) { return false; }
307
pm_runtime_no_callbacks(struct device * dev)308 static inline void pm_runtime_no_callbacks(struct device *dev) {}
pm_runtime_irq_safe(struct device * dev)309 static inline void pm_runtime_irq_safe(struct device *dev) {}
pm_runtime_is_irq_safe(struct device * dev)310 static inline bool pm_runtime_is_irq_safe(struct device *dev) { return false; }
311
pm_runtime_has_no_callbacks(struct device * dev)312 static inline bool pm_runtime_has_no_callbacks(struct device *dev) { return false; }
pm_runtime_mark_last_busy(struct device * dev)313 static inline void pm_runtime_mark_last_busy(struct device *dev) {}
__pm_runtime_use_autosuspend(struct device * dev,bool use)314 static inline void __pm_runtime_use_autosuspend(struct device *dev,
315 bool use) {}
pm_runtime_set_autosuspend_delay(struct device * dev,int delay)316 static inline void pm_runtime_set_autosuspend_delay(struct device *dev,
317 int delay) {}
pm_runtime_autosuspend_expiration(struct device * dev)318 static inline u64 pm_runtime_autosuspend_expiration(
319 struct device *dev) { return 0; }
pm_runtime_set_memalloc_noio(struct device * dev,bool enable)320 static inline void pm_runtime_set_memalloc_noio(struct device *dev,
321 bool enable){}
pm_runtime_get_suppliers(struct device * dev)322 static inline void pm_runtime_get_suppliers(struct device *dev) {}
pm_runtime_put_suppliers(struct device * dev)323 static inline void pm_runtime_put_suppliers(struct device *dev) {}
pm_runtime_new_link(struct device * dev)324 static inline void pm_runtime_new_link(struct device *dev) {}
pm_runtime_drop_link(struct device_link * link)325 static inline void pm_runtime_drop_link(struct device_link *link) {}
pm_runtime_release_supplier(struct device_link * link)326 static inline void pm_runtime_release_supplier(struct device_link *link) {}
327
328 #endif /* !CONFIG_PM */
329
330 #ifdef CONFIG_PM_SLEEP
331
332 bool pm_runtime_need_not_resume(struct device *dev);
333 int pm_runtime_force_resume(struct device *dev);
334
335 #else /* !CONFIG_PM_SLEEP */
336
pm_runtime_need_not_resume(struct device * dev)337 static inline bool pm_runtime_need_not_resume(struct device *dev) {return true; }
pm_runtime_force_resume(struct device * dev)338 static inline int pm_runtime_force_resume(struct device *dev) { return -ENXIO; }
339
340 #endif /* CONFIG_PM_SLEEP */
341
342 /**
343 * pm_runtime_idle - Conditionally set up autosuspend of a device or suspend it.
344 * @dev: Target device.
345 *
346 * Invoke the "idle check" callback of @dev and, depending on its return value,
347 * set up autosuspend of @dev or suspend it (depending on whether or not
348 * autosuspend has been enabled for it).
349 *
350 * Return:
351 * * 0: Success.
352 * * -EINVAL: Runtime PM error.
353 * * -EACCES: Runtime PM disabled.
354 * * -EAGAIN: Runtime PM usage counter non-zero, Runtime PM status change
355 * ongoing or device not in %RPM_ACTIVE state.
356 * * -EBUSY: Runtime PM child_count non-zero.
357 * * -EPERM: Device PM QoS resume latency 0.
358 * * -EINPROGRESS: Suspend already in progress.
359 * * -ENOSYS: CONFIG_PM not enabled.
360 * Other values and conditions for the above values are possible as returned by
361 * Runtime PM idle and suspend callbacks.
362 */
pm_runtime_idle(struct device * dev)363 static inline int pm_runtime_idle(struct device *dev)
364 {
365 return __pm_runtime_idle(dev, 0);
366 }
367
368 /**
369 * pm_runtime_suspend - Suspend a device synchronously.
370 * @dev: Target device.
371 *
372 * Return:
373 * * 1: Success; device was already suspended.
374 * * 0: Success.
375 * * -EINVAL: Runtime PM error.
376 * * -EACCES: Runtime PM disabled.
377 * * -EAGAIN: Runtime PM usage counter non-zero or Runtime PM status change
378 * ongoing.
379 * * -EBUSY: Runtime PM child_count non-zero.
380 * * -EPERM: Device PM QoS resume latency 0.
381 * * -ENOSYS: CONFIG_PM not enabled.
382 * Other values and conditions for the above values are possible as returned by
383 * Runtime PM suspend callbacks.
384 */
pm_runtime_suspend(struct device * dev)385 static inline int pm_runtime_suspend(struct device *dev)
386 {
387 return __pm_runtime_suspend(dev, 0);
388 }
389
390 /**
391 * pm_runtime_autosuspend - Update the last access time and set up autosuspend
392 * of a device.
393 * @dev: Target device.
394 *
395 * First update the last access time, then set up autosuspend of @dev or suspend
396 * it (depending on whether or not autosuspend is enabled for it) without
397 * engaging its "idle check" callback.
398 *
399 * Return:
400 * * 1: Success; device was already suspended.
401 * * 0: Success.
402 * * -EINVAL: Runtime PM error.
403 * * -EACCES: Runtime PM disabled.
404 * * -EAGAIN: Runtime PM usage counter non-zero or Runtime PM status change
405 * ongoing.
406 * * -EBUSY: Runtime PM child_count non-zero.
407 * * -EPERM: Device PM QoS resume latency 0.
408 * * -ENOSYS: CONFIG_PM not enabled.
409 * Other values and conditions for the above values are possible as returned by
410 * Runtime PM suspend callbacks.
411 */
pm_runtime_autosuspend(struct device * dev)412 static inline int pm_runtime_autosuspend(struct device *dev)
413 {
414 pm_runtime_mark_last_busy(dev);
415 return __pm_runtime_suspend(dev, RPM_AUTO);
416 }
417
418 /**
419 * pm_runtime_resume - Resume a device synchronously.
420 * @dev: Target device.
421 */
pm_runtime_resume(struct device * dev)422 static inline int pm_runtime_resume(struct device *dev)
423 {
424 return __pm_runtime_resume(dev, 0);
425 }
426
427 /**
428 * pm_request_idle - Queue up "idle check" execution for a device.
429 * @dev: Target device.
430 *
431 * Queue up a work item to run an equivalent of pm_runtime_idle() for @dev
432 * asynchronously.
433 *
434 * Return:
435 * * 0: Success.
436 * * -EINVAL: Runtime PM error.
437 * * -EACCES: Runtime PM disabled.
438 * * -EAGAIN: Runtime PM usage counter non-zero, Runtime PM status change
439 * ongoing or device not in %RPM_ACTIVE state.
440 * * -EBUSY: Runtime PM child_count non-zero.
441 * * -EPERM: Device PM QoS resume latency 0.
442 * * -EINPROGRESS: Suspend already in progress.
443 * * -ENOSYS: CONFIG_PM not enabled.
444 */
pm_request_idle(struct device * dev)445 static inline int pm_request_idle(struct device *dev)
446 {
447 return __pm_runtime_idle(dev, RPM_ASYNC);
448 }
449
450 /**
451 * pm_request_resume - Queue up runtime-resume of a device.
452 * @dev: Target device.
453 */
pm_request_resume(struct device * dev)454 static inline int pm_request_resume(struct device *dev)
455 {
456 return __pm_runtime_resume(dev, RPM_ASYNC);
457 }
458
459 /**
460 * pm_request_autosuspend - Update the last access time and queue up autosuspend
461 * of a device.
462 * @dev: Target device.
463 *
464 * Update the last access time of a device and queue up a work item to run an
465 * equivalent pm_runtime_autosuspend() for @dev asynchronously.
466 *
467 * Return:
468 * * 1: Success; device was already suspended.
469 * * 0: Success.
470 * * -EINVAL: Runtime PM error.
471 * * -EACCES: Runtime PM disabled.
472 * * -EAGAIN: Runtime PM usage counter non-zero or Runtime PM status change
473 * ongoing.
474 * * -EBUSY: Runtime PM child_count non-zero.
475 * * -EPERM: Device PM QoS resume latency 0.
476 * * -EINPROGRESS: Suspend already in progress.
477 * * -ENOSYS: CONFIG_PM not enabled.
478 */
pm_request_autosuspend(struct device * dev)479 static inline int pm_request_autosuspend(struct device *dev)
480 {
481 pm_runtime_mark_last_busy(dev);
482 return __pm_runtime_suspend(dev, RPM_ASYNC | RPM_AUTO);
483 }
484
485 /**
486 * pm_runtime_get - Bump up usage counter and queue up resume of a device.
487 * @dev: Target device.
488 *
489 * Bump up the runtime PM usage counter of @dev and queue up a work item to
490 * carry out runtime-resume of it.
491 */
pm_runtime_get(struct device * dev)492 static inline int pm_runtime_get(struct device *dev)
493 {
494 return __pm_runtime_resume(dev, RPM_GET_PUT | RPM_ASYNC);
495 }
496
497 /**
498 * pm_runtime_get_sync - Bump up usage counter of a device and resume it.
499 * @dev: Target device.
500 *
501 * Bump up the runtime PM usage counter of @dev and carry out runtime-resume of
502 * it synchronously.
503 *
504 * The possible return values of this function are the same as for
505 * pm_runtime_resume() and the runtime PM usage counter of @dev remains
506 * incremented in all cases, even if it returns an error code.
507 * Consider using pm_runtime_resume_and_get() instead of it, especially
508 * if its return value is checked by the caller, as this is likely to result
509 * in cleaner code.
510 */
pm_runtime_get_sync(struct device * dev)511 static inline int pm_runtime_get_sync(struct device *dev)
512 {
513 return __pm_runtime_resume(dev, RPM_GET_PUT);
514 }
515
pm_runtime_get_active(struct device * dev,int rpmflags)516 static inline int pm_runtime_get_active(struct device *dev, int rpmflags)
517 {
518 int ret;
519
520 ret = __pm_runtime_resume(dev, RPM_GET_PUT | rpmflags);
521 if (ret < 0) {
522 pm_runtime_put_noidle(dev);
523 return ret;
524 }
525
526 return 0;
527 }
528
529 /**
530 * pm_runtime_resume_and_get - Bump up usage counter of a device and resume it.
531 * @dev: Target device.
532 *
533 * Resume @dev synchronously and if that is successful, increment its runtime
534 * PM usage counter. Return 0 if the runtime PM usage counter of @dev has been
535 * incremented or a negative error code otherwise.
536 */
pm_runtime_resume_and_get(struct device * dev)537 static inline int pm_runtime_resume_and_get(struct device *dev)
538 {
539 return pm_runtime_get_active(dev, 0);
540 }
541
542 /**
543 * pm_runtime_put - Drop device usage counter and queue up "idle check" if 0.
544 * @dev: Target device.
545 *
546 * Decrement the runtime PM usage counter of @dev and if it turns out to be
547 * equal to 0, queue up a work item for @dev like in pm_request_idle().
548 *
549 * Return:
550 * * 1: Success. Usage counter dropped to zero, but device was already suspended.
551 * * 0: Success.
552 * * -EINVAL: Runtime PM error.
553 * * -EACCES: Runtime PM disabled.
554 * * -EAGAIN: Runtime PM usage counter became non-zero or Runtime PM status
555 * change ongoing.
556 * * -EBUSY: Runtime PM child_count non-zero.
557 * * -EPERM: Device PM QoS resume latency 0.
558 * * -EINPROGRESS: Suspend already in progress.
559 * * -ENOSYS: CONFIG_PM not enabled.
560 */
pm_runtime_put(struct device * dev)561 static inline int pm_runtime_put(struct device *dev)
562 {
563 return __pm_runtime_idle(dev, RPM_GET_PUT | RPM_ASYNC);
564 }
565
566 /**
567 * __pm_runtime_put_autosuspend - Drop device usage counter and queue autosuspend if 0.
568 * @dev: Target device.
569 *
570 * Decrement the runtime PM usage counter of @dev and if it turns out to be
571 * equal to 0, queue up a work item for @dev like in pm_request_autosuspend().
572 *
573 * Return:
574 * * 1: Success. Usage counter dropped to zero, but device was already suspended.
575 * * 0: Success.
576 * * -EINVAL: Runtime PM error.
577 * * -EACCES: Runtime PM disabled.
578 * * -EAGAIN: Runtime PM usage counter became non-zero or Runtime PM status
579 * change ongoing.
580 * * -EBUSY: Runtime PM child_count non-zero.
581 * * -EPERM: Device PM QoS resume latency 0.
582 * * -EINPROGRESS: Suspend already in progress.
583 * * -ENOSYS: CONFIG_PM not enabled.
584 */
__pm_runtime_put_autosuspend(struct device * dev)585 static inline int __pm_runtime_put_autosuspend(struct device *dev)
586 {
587 return __pm_runtime_suspend(dev, RPM_GET_PUT | RPM_ASYNC | RPM_AUTO);
588 }
589
590 /**
591 * pm_runtime_put_autosuspend - Update the last access time of a device, drop
592 * its usage counter and queue autosuspend if the usage counter becomes 0.
593 * @dev: Target device.
594 *
595 * Update the last access time of @dev, decrement runtime PM usage counter of
596 * @dev and if it turns out to be equal to 0, queue up a work item for @dev like
597 * in pm_request_autosuspend().
598 *
599 * Return:
600 * * 1: Success. Usage counter dropped to zero, but device was already suspended.
601 * * 0: Success.
602 * * -EINVAL: Runtime PM error.
603 * * -EACCES: Runtime PM disabled.
604 * * -EAGAIN: Runtime PM usage counter became non-zero or Runtime PM status
605 * change ongoing.
606 * * -EBUSY: Runtime PM child_count non-zero.
607 * * -EPERM: Device PM QoS resume latency 0.
608 * * -EINPROGRESS: Suspend already in progress.
609 * * -ENOSYS: CONFIG_PM not enabled.
610 */
pm_runtime_put_autosuspend(struct device * dev)611 static inline int pm_runtime_put_autosuspend(struct device *dev)
612 {
613 pm_runtime_mark_last_busy(dev);
614 return __pm_runtime_put_autosuspend(dev);
615 }
616
617 DEFINE_GUARD(pm_runtime_noresume, struct device *,
618 pm_runtime_get_noresume(_T), pm_runtime_put_noidle(_T));
619
620 DEFINE_GUARD(pm_runtime_active, struct device *,
621 pm_runtime_get_sync(_T), pm_runtime_put(_T));
622 DEFINE_GUARD(pm_runtime_active_auto, struct device *,
623 pm_runtime_get_sync(_T), pm_runtime_put_autosuspend(_T));
624 /*
625 * Use the following guards with ACQUIRE()/ACQUIRE_ERR().
626 *
627 * The difference between the "_try" and "_try_enabled" variants is that the
628 * former do not produce an error when runtime PM is disabled for the given
629 * device.
630 */
DEFINE_GUARD_COND(pm_runtime_active,_try,pm_runtime_get_active (_T,RPM_TRANSPARENT))631 DEFINE_GUARD_COND(pm_runtime_active, _try,
632 pm_runtime_get_active(_T, RPM_TRANSPARENT))
633 DEFINE_GUARD_COND(pm_runtime_active, _try_enabled,
634 pm_runtime_resume_and_get(_T))
635 DEFINE_GUARD_COND(pm_runtime_active_auto, _try,
636 pm_runtime_get_active(_T, RPM_TRANSPARENT))
637 DEFINE_GUARD_COND(pm_runtime_active_auto, _try_enabled,
638 pm_runtime_resume_and_get(_T))
639
640 /**
641 * pm_runtime_put_sync - Drop device usage counter and run "idle check" if 0.
642 * @dev: Target device.
643 *
644 * Decrement the runtime PM usage counter of @dev and if it turns out to be
645 * equal to 0, invoke the "idle check" callback of @dev and, depending on its
646 * return value, set up autosuspend of @dev or suspend it (depending on whether
647 * or not autosuspend has been enabled for it).
648 *
649 * The runtime PM usage counter of @dev remains decremented in all cases, even
650 * if it returns an error code.
651 *
652 * Return:
653 * * 1: Success. Usage counter dropped to zero, but device was already suspended.
654 * * 0: Success.
655 * * -EINVAL: Runtime PM error.
656 * * -EACCES: Runtime PM disabled.
657 * * -EAGAIN: Runtime PM usage counter became non-zero or Runtime PM status
658 * change ongoing.
659 * * -EBUSY: Runtime PM child_count non-zero.
660 * * -EPERM: Device PM QoS resume latency 0.
661 * * -ENOSYS: CONFIG_PM not enabled.
662 * Other values and conditions for the above values are possible as returned by
663 * Runtime PM suspend callbacks.
664 */
665 static inline int pm_runtime_put_sync(struct device *dev)
666 {
667 return __pm_runtime_idle(dev, RPM_GET_PUT);
668 }
669
670 /**
671 * pm_runtime_put_sync_suspend - Drop device usage counter and suspend if 0.
672 * @dev: Target device.
673 *
674 * Decrement the runtime PM usage counter of @dev and if it turns out to be
675 * equal to 0, carry out runtime-suspend of @dev synchronously.
676 *
677 * The runtime PM usage counter of @dev remains decremented in all cases, even
678 * if it returns an error code.
679 *
680 * Return:
681 * * 1: Success. Usage counter dropped to zero, but device was already suspended.
682 * * 0: Success.
683 * * -EINVAL: Runtime PM error.
684 * * -EACCES: Runtime PM disabled.
685 * * -EAGAIN: Runtime PM usage counter became non-zero or Runtime PM status
686 * change ongoing.
687 * * -EBUSY: Runtime PM child_count non-zero.
688 * * -EPERM: Device PM QoS resume latency 0.
689 * * -ENOSYS: CONFIG_PM not enabled.
690 * Other values and conditions for the above values are possible as returned by
691 * Runtime PM suspend callbacks.
692 */
pm_runtime_put_sync_suspend(struct device * dev)693 static inline int pm_runtime_put_sync_suspend(struct device *dev)
694 {
695 return __pm_runtime_suspend(dev, RPM_GET_PUT);
696 }
697
698 /**
699 * pm_runtime_put_sync_autosuspend - Update the last access time of a device,
700 * drop device usage counter and autosuspend if 0.
701 * @dev: Target device.
702 *
703 * Update the last access time of @dev, decrement the runtime PM usage counter
704 * of @dev and if it turns out to be equal to 0, set up autosuspend of @dev or
705 * suspend it synchronously (depending on whether or not autosuspend has been
706 * enabled for it).
707 *
708 * The runtime PM usage counter of @dev remains decremented in all cases, even
709 * if it returns an error code.
710 *
711 * Return:
712 * * 1: Success. Usage counter dropped to zero, but device was already suspended.
713 * * 0: Success.
714 * * -EINVAL: Runtime PM error.
715 * * -EACCES: Runtime PM disabled.
716 * * -EAGAIN: Runtime PM usage counter became non-zero or Runtime PM status
717 * change ongoing.
718 * * -EBUSY: Runtime PM child_count non-zero.
719 * * -EPERM: Device PM QoS resume latency 0.
720 * * -EINPROGRESS: Suspend already in progress.
721 * * -ENOSYS: CONFIG_PM not enabled.
722 * Other values and conditions for the above values are possible as returned by
723 * Runtime PM suspend callbacks.
724 */
pm_runtime_put_sync_autosuspend(struct device * dev)725 static inline int pm_runtime_put_sync_autosuspend(struct device *dev)
726 {
727 pm_runtime_mark_last_busy(dev);
728 return __pm_runtime_suspend(dev, RPM_GET_PUT | RPM_AUTO);
729 }
730
731 /**
732 * pm_runtime_set_active - Set runtime PM status to "active".
733 * @dev: Target device.
734 *
735 * Set the runtime PM status of @dev to %RPM_ACTIVE and ensure that dependencies
736 * of it will be taken into account.
737 *
738 * It is not valid to call this function for devices with runtime PM enabled.
739 */
pm_runtime_set_active(struct device * dev)740 static inline int pm_runtime_set_active(struct device *dev)
741 {
742 return __pm_runtime_set_status(dev, RPM_ACTIVE);
743 }
744
745 /**
746 * pm_runtime_set_suspended - Set runtime PM status to "suspended".
747 * @dev: Target device.
748 *
749 * Set the runtime PM status of @dev to %RPM_SUSPENDED and ensure that
750 * dependencies of it will be taken into account.
751 *
752 * It is not valid to call this function for devices with runtime PM enabled.
753 */
pm_runtime_set_suspended(struct device * dev)754 static inline int pm_runtime_set_suspended(struct device *dev)
755 {
756 return __pm_runtime_set_status(dev, RPM_SUSPENDED);
757 }
758
759 /**
760 * pm_runtime_disable - Disable runtime PM for a device.
761 * @dev: Target device.
762 *
763 * Prevent the runtime PM framework from working with @dev by incrementing its
764 * "disable" counter.
765 *
766 * If the counter is zero when this function runs and there is a pending runtime
767 * resume request for @dev, it will be resumed. If the counter is still zero at
768 * that point, all of the pending runtime PM requests for @dev will be canceled
769 * and all runtime PM operations in progress involving it will be waited for to
770 * complete.
771 *
772 * For each invocation of this function for @dev, there must be a matching
773 * pm_runtime_enable() call, so that runtime PM is eventually enabled for it
774 * again.
775 */
pm_runtime_disable(struct device * dev)776 static inline void pm_runtime_disable(struct device *dev)
777 {
778 __pm_runtime_disable(dev, true);
779 }
780
781 /**
782 * pm_runtime_use_autosuspend - Allow autosuspend to be used for a device.
783 * @dev: Target device.
784 *
785 * Allow the runtime PM autosuspend mechanism to be used for @dev whenever
786 * requested (or "autosuspend" will be handled as direct runtime-suspend for
787 * it).
788 *
789 * NOTE: It's important to undo this with pm_runtime_dont_use_autosuspend()
790 * at driver exit time unless your driver initially enabled pm_runtime
791 * with devm_pm_runtime_enable() (which handles it for you).
792 */
pm_runtime_use_autosuspend(struct device * dev)793 static inline void pm_runtime_use_autosuspend(struct device *dev)
794 {
795 __pm_runtime_use_autosuspend(dev, true);
796 }
797
798 /**
799 * pm_runtime_dont_use_autosuspend - Prevent autosuspend from being used.
800 * @dev: Target device.
801 *
802 * Prevent the runtime PM autosuspend mechanism from being used for @dev which
803 * means that "autosuspend" will be handled as direct runtime-suspend for it
804 * going forward.
805 */
pm_runtime_dont_use_autosuspend(struct device * dev)806 static inline void pm_runtime_dont_use_autosuspend(struct device *dev)
807 {
808 __pm_runtime_use_autosuspend(dev, false);
809 }
810
811 #endif
812