xref: /linux/include/linux/pm_runtime.h (revision 3baf1652fde8e54754df0b8bc9a6014c1752383d)
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 
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 void 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  */
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  */
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  */
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  * Note that the return value of this function can only be trusted if it is
141  * called under the runtime PM lock of @dev or under conditions in which
142  * runtime PM cannot be either disabled or enabled for @dev and its runtime PM
143  * status cannot change.
144  *
145  * Return:
146  * * %true: @dev has runtime PM enabled and its status is %RPM_SUSPENDED.
147  * * %false: Otherwise.
148  */
149 static inline bool pm_runtime_suspended(struct device *dev)
150 {
151 	return dev->power.runtime_status == RPM_SUSPENDED
152 		&& !dev->power.disable_depth;
153 }
154 
155 /**
156  * pm_runtime_active - Check whether or not a device is runtime-active.
157  * @dev: Target device.
158  *
159  * Note that the return value of this function can only be trusted if it is
160  * called under the runtime PM lock of @dev or under conditions in which
161  * runtime PM cannot be either disabled or enabled for @dev and its runtime PM
162  * status cannot change.
163  *
164  * Return:
165  * * %true: Runtime PM is disabled for @dev or its status is %RPM_ACTIVE.
166  * * %false: Otherwise.
167  */
168 static inline bool pm_runtime_active(struct device *dev)
169 {
170 	return dev->power.runtime_status == RPM_ACTIVE
171 		|| dev->power.disable_depth;
172 }
173 
174 /**
175  * pm_runtime_status_suspended - Check if runtime PM status is "suspended".
176  * @dev: Target device.
177  *
178  * Note that the return value of this function can only be trusted if it is
179  * called under the runtime PM lock of @dev or under conditions in which the
180  * runtime PM status of @dev cannot change.
181  *
182  * Return:
183  * * %true: Runtime PM status of @dev is %RPM_SUSPENDED.
184  * * %false: Otherwise.
185  */
186 static inline bool pm_runtime_status_suspended(struct device *dev)
187 {
188 	return dev->power.runtime_status == RPM_SUSPENDED;
189 }
190 
191 /**
192  * pm_runtime_enabled - Check if runtime PM is enabled.
193  * @dev: Target device.
194  *
195  * Note that the return value of this function can only be trusted if it is
196  * called under the runtime PM lock of @dev or under conditions in which
197  * runtime PM cannot be either disabled or enabled for @dev.
198  *
199  * Return:
200  * * %true: Runtime PM is enabled for @dev.
201  * * %false: Otherwise.
202  */
203 static inline bool pm_runtime_enabled(struct device *dev)
204 {
205 	return !dev->power.disable_depth;
206 }
207 
208 /**
209  * pm_runtime_blocked - Check if runtime PM enabling is blocked.
210  * @dev: Target device.
211  *
212  * Do not call this function outside system suspend/resume code paths.
213  *
214  * Return:
215  * * %true: Runtime PM enabling is blocked for @dev.
216  * * %false: Otherwise.
217  */
218 static inline bool pm_runtime_blocked(struct device *dev)
219 {
220 	return dev->power.last_status == RPM_BLOCKED;
221 }
222 
223 /**
224  * pm_runtime_has_no_callbacks - Check if runtime PM callbacks may be present.
225  * @dev: Target device.
226  *
227  * Return:
228  * * %true: @dev is marked as having no runtime PM callbacks.
229  * * %false: Otherwise.
230  */
231 static inline bool pm_runtime_has_no_callbacks(struct device *dev)
232 {
233 	return dev->power.no_callbacks;
234 }
235 
236 /**
237  * pm_runtime_mark_last_busy - Update the last access time of a device.
238  * @dev: Target device.
239  *
240  * Update the last access time of @dev used by the runtime PM autosuspend
241  * mechanism to the current time as returned by ktime_get_mono_fast_ns().
242  */
243 static inline void pm_runtime_mark_last_busy(struct device *dev)
244 {
245 	WRITE_ONCE(dev->power.last_busy, ktime_get_mono_fast_ns());
246 }
247 
248 /**
249  * pm_runtime_is_irq_safe - Check if runtime PM can work in interrupt context.
250  * @dev: Target device.
251  *
252  * Return:
253  * * %true: @dev has been marked as an "IRQ-safe" device, in which case its
254  *   runtime PM callbacks can be expected to work correctly from interrupt
255  *   handlers.
256  * * %false: Otherwise.
257  */
258 static inline bool pm_runtime_is_irq_safe(struct device *dev)
259 {
260 	return dev->power.irq_safe;
261 }
262 
263 extern u64 pm_runtime_suspended_time(struct device *dev);
264 
265 #else /* !CONFIG_PM */
266 
267 static inline bool queue_pm_work(struct work_struct *work) { return false; }
268 
269 static inline int pm_generic_runtime_suspend(struct device *dev) { return 0; }
270 static inline int pm_generic_runtime_resume(struct device *dev) { return 0; }
271 static inline int pm_runtime_force_suspend(struct device *dev) { return 0; }
272 
273 static inline int __pm_runtime_idle(struct device *dev, int rpmflags)
274 {
275 	return -ENOSYS;
276 }
277 static inline int __pm_runtime_suspend(struct device *dev, int rpmflags)
278 {
279 	return -ENOSYS;
280 }
281 static inline int __pm_runtime_resume(struct device *dev, int rpmflags)
282 {
283 	return 1;
284 }
285 static inline int pm_schedule_suspend(struct device *dev, unsigned int delay)
286 {
287 	return -ENOSYS;
288 }
289 static inline int pm_runtime_get_if_in_use(struct device *dev)
290 {
291 	return -EINVAL;
292 }
293 static inline int pm_runtime_get_if_active(struct device *dev)
294 {
295 	return -EINVAL;
296 }
297 static inline int __pm_runtime_set_status(struct device *dev,
298 					    unsigned int status) { return 0; }
299 static inline void pm_runtime_barrier(struct device *dev) {}
300 static inline bool pm_runtime_block_if_disabled(struct device *dev) { return true; }
301 static inline void pm_runtime_unblock(struct device *dev) {}
302 static inline void pm_runtime_enable(struct device *dev) {}
303 static inline void __pm_runtime_disable(struct device *dev, bool c) {}
304 static inline bool pm_runtime_blocked(struct device *dev) { return true; }
305 static inline void pm_runtime_allow(struct device *dev) {}
306 static inline void pm_runtime_forbid(struct device *dev) {}
307 
308 static inline int devm_pm_runtime_set_active_enabled(struct device *dev) { return 0; }
309 static inline int devm_pm_runtime_enable(struct device *dev) { return 0; }
310 static inline int devm_pm_runtime_get_noresume(struct device *dev) { return 0; }
311 
312 static inline void pm_suspend_ignore_children(struct device *dev, bool enable) {}
313 static inline void pm_runtime_get_noresume(struct device *dev) {}
314 static inline void pm_runtime_put_noidle(struct device *dev) {}
315 static inline bool pm_runtime_suspended(struct device *dev) { return false; }
316 static inline bool pm_runtime_active(struct device *dev) { return true; }
317 static inline bool pm_runtime_status_suspended(struct device *dev) { return false; }
318 static inline bool pm_runtime_enabled(struct device *dev) { return false; }
319 
320 static inline void pm_runtime_no_callbacks(struct device *dev) {}
321 static inline void pm_runtime_irq_safe(struct device *dev) {}
322 static inline bool pm_runtime_is_irq_safe(struct device *dev) { return false; }
323 
324 static inline bool pm_runtime_has_no_callbacks(struct device *dev) { return false; }
325 static inline void pm_runtime_mark_last_busy(struct device *dev) {}
326 static inline void __pm_runtime_use_autosuspend(struct device *dev,
327 						bool use) {}
328 static inline void pm_runtime_set_autosuspend_delay(struct device *dev,
329 						int delay) {}
330 static inline u64 pm_runtime_autosuspend_expiration(
331 				struct device *dev) { return 0; }
332 static inline void pm_runtime_set_memalloc_noio(struct device *dev,
333 						bool enable){}
334 static inline void pm_runtime_get_suppliers(struct device *dev) {}
335 static inline void pm_runtime_put_suppliers(struct device *dev) {}
336 static inline void pm_runtime_new_link(struct device *dev) {}
337 static inline void pm_runtime_drop_link(struct device_link *link) {}
338 static inline void pm_runtime_release_supplier(struct device_link *link) {}
339 
340 #endif /* !CONFIG_PM */
341 
342 #ifdef CONFIG_PM_SLEEP
343 
344 bool pm_runtime_need_not_resume(struct device *dev);
345 int pm_runtime_force_resume(struct device *dev);
346 
347 #else /* !CONFIG_PM_SLEEP */
348 
349 static inline bool pm_runtime_need_not_resume(struct device *dev) {return true; }
350 static inline int pm_runtime_force_resume(struct device *dev) { return -ENXIO; }
351 
352 #endif /* CONFIG_PM_SLEEP */
353 
354 /**
355  * pm_runtime_idle - Conditionally initiate autosuspend of a device or suspend it.
356  * @dev: Target device.
357  *
358  * Invoke the "idle check" callback of @dev and, depending on its return value,
359  * initiate autosuspend of @dev or suspend it (depending on whether or not
360  * autosuspend has been enabled for it).
361  *
362  * Return:
363  * * %0: Success.
364  * * %-EINVAL: Runtime PM error.
365  * * %-EACCES: Runtime PM disabled.
366  * * %-EAGAIN: Runtime PM usage counter non-zero, Runtime PM status change
367  *   ongoing or device not in %RPM_ACTIVE state.
368  * * %-EBUSY: Runtime PM child_count non-zero.
369  * * %-EPERM: Device PM QoS resume latency 0.
370  * * %-EINPROGRESS: Suspend already in progress.
371  * * %-ENOSYS: %CONFIG_PM not enabled.
372  * * Other values and conditions for the above values are possible as returned
373  *   by Runtime PM idle and suspend callbacks.
374  */
375 static inline int pm_runtime_idle(struct device *dev)
376 {
377 	return __pm_runtime_idle(dev, 0);
378 }
379 
380 /**
381  * pm_runtime_suspend - Suspend a device synchronously.
382  * @dev: Target device.
383  *
384  * Return:
385  * * %1: Success; device was already suspended.
386  * * %0: Success.
387  * * %-EINVAL: Runtime PM error.
388  * * %-EACCES: Runtime PM disabled.
389  * * %-EAGAIN: Runtime PM usage counter non-zero or Runtime PM status change
390  *   ongoing.
391  * * %-EBUSY: Runtime PM child_count non-zero.
392  * * %-EPERM: Device PM QoS resume latency 0.
393  * * %-ENOSYS: %CONFIG_PM not enabled.
394  * * Other values and conditions for the above values are possible as returned
395  *   by Runtime PM suspend callbacks.
396  */
397 static inline int pm_runtime_suspend(struct device *dev)
398 {
399 	return __pm_runtime_suspend(dev, 0);
400 }
401 
402 /**
403  * pm_runtime_autosuspend - Update the last access time and initiate autosuspend
404  * of a device.
405  * @dev: Target device.
406  *
407  * First update the last access time, then initiate autosuspend of @dev or
408  * suspend it (depending on whether or not autosuspend is enabled for it)
409  * without engaging its "idle check" callback.
410  *
411  * Return:
412  * * %1: Success; device was already suspended.
413  * * %0: Success.
414  * * %-EINVAL: Runtime PM error.
415  * * %-EACCES: Runtime PM disabled.
416  * * %-EAGAIN: Runtime PM usage counter non-zero or Runtime PM status change
417  *   ongoing.
418  * * %-EBUSY: Runtime PM child_count non-zero.
419  * * %-EPERM: Device PM QoS resume latency 0.
420  * * %-ENOSYS: %CONFIG_PM not enabled.
421  * * Other values and conditions for the above values are possible as returned
422  *   by Runtime PM suspend callbacks.
423  */
424 static inline int pm_runtime_autosuspend(struct device *dev)
425 {
426 	pm_runtime_mark_last_busy(dev);
427 	return __pm_runtime_suspend(dev, RPM_AUTO);
428 }
429 
430 /**
431  * pm_runtime_resume - Resume a device synchronously.
432  * @dev: Target device.
433  *
434  * Return:
435  * * %1: Success; @dev is already %RPM_ACTIVE.
436  * * %0: Success.
437  * * Error code on failure.
438  */
439 static inline int pm_runtime_resume(struct device *dev)
440 {
441 	return __pm_runtime_resume(dev, 0);
442 }
443 
444 /**
445  * pm_request_idle - Request an asynchronous idle check for a device.
446  * @dev: Target device.
447  *
448  * Asynchronously request the PM core to evaluate whether @dev can be idled
449  * or suspended, invoking its ->runtime_idle() callback if provided.
450  *
451  * Return:
452  * * %0: Success.
453  * * %-EINVAL: Runtime PM error.
454  * * %-EACCES: Runtime PM disabled.
455  * * %-EAGAIN: Runtime PM usage counter non-zero, Runtime PM status change
456  *   ongoing or device not in %RPM_ACTIVE state.
457  * * %-EBUSY: Runtime PM child_count non-zero.
458  * * %-EPERM: Device PM QoS resume latency 0.
459  * * %-EINPROGRESS: Suspend already in progress.
460  * * %-ENOSYS: %CONFIG_PM not enabled.
461  */
462 static inline int pm_request_idle(struct device *dev)
463 {
464 	return __pm_runtime_idle(dev, RPM_ASYNC);
465 }
466 
467 /**
468  * pm_request_resume - Request an asynchronous runtime resume for a device.
469  * @dev: Target device.
470  *
471  * Asynchronously request the PM core to resume @dev to %RPM_ACTIVE state
472  * without modifying its usage counter.
473  *
474  * Return:
475  * * %1: Success; @dev is already %RPM_ACTIVE.
476  * * %0: Success.
477  * * Error code on failure.
478  */
479 static inline int pm_request_resume(struct device *dev)
480 {
481 	return __pm_runtime_resume(dev, RPM_ASYNC);
482 }
483 
484 /**
485  * pm_request_autosuspend - Update access time and request delayed suspension.
486  * @dev: Target device.
487  *
488  * Update the last access time of @dev and asynchronously request the PM core
489  * to suspend it after the autosuspend delay has elapsed.
490  *
491  * Return:
492  * * %1: Success; device was already suspended.
493  * * %0: Success.
494  * * %-EINVAL: Runtime PM error.
495  * * %-EACCES: Runtime PM disabled.
496  * * %-EAGAIN: Runtime PM usage counter non-zero or Runtime PM status change
497  *   ongoing.
498  * * %-EBUSY: Runtime PM child_count non-zero.
499  * * %-EPERM: Device PM QoS resume latency 0.
500  * * %-EINPROGRESS: Suspend already in progress.
501  * * %-ENOSYS: %CONFIG_PM not enabled.
502  */
503 static inline int pm_request_autosuspend(struct device *dev)
504 {
505 	pm_runtime_mark_last_busy(dev);
506 	return __pm_runtime_suspend(dev, RPM_ASYNC | RPM_AUTO);
507 }
508 
509 /**
510  * pm_runtime_get - Increment usage counter and request asynchronous resume.
511  * @dev: Target device.
512  *
513  * Increment the runtime PM usage counter of @dev and, if the device is
514  * currently suspended, asynchronously request the PM core to resume it.
515  *
516  * Return:
517  * * %1: Success; @dev is already %RPM_ACTIVE.
518  * * %0: Success; runtime-resume was queued.
519  * * Error code on failure.
520  */
521 static inline int pm_runtime_get(struct device *dev)
522 {
523 	return __pm_runtime_resume(dev, RPM_GET_PUT | RPM_ASYNC);
524 }
525 
526 /**
527  * pm_runtime_get_sync - Bump up usage counter of a device and resume it.
528  * @dev: Target device.
529  *
530  * Bump up the runtime PM usage counter of @dev and carry out runtime-resume of
531  * it synchronously.
532  *
533  * Note that the runtime PM usage counter of @dev remains incremented in all
534  * cases, even if it returns an error code. Consider using
535  * pm_runtime_resume_and_get() instead, especially if the return value is
536  * checked by the caller, as this is likely to result in cleaner code.
537  *
538  * Return:
539  * * %1: Success; @dev is already %RPM_ACTIVE.
540  * * %0: Success.
541  * * Error code on failure.
542  */
543 static inline int pm_runtime_get_sync(struct device *dev)
544 {
545 	return __pm_runtime_resume(dev, RPM_GET_PUT);
546 }
547 
548 static inline int pm_runtime_get_active(struct device *dev, int rpmflags)
549 {
550 	int ret;
551 
552 	ret = __pm_runtime_resume(dev, RPM_GET_PUT | rpmflags);
553 	if (ret < 0) {
554 		pm_runtime_put_noidle(dev);
555 		return ret;
556 	}
557 
558 	return 0;
559 }
560 
561 /**
562  * pm_runtime_resume_and_get - Bump up usage counter of a device and resume it.
563  * @dev: Target device.
564  *
565  * Resume @dev synchronously and if that is successful, increment its runtime
566  * PM usage counter.
567  *
568  * Return:
569  * * %0: Success; @dev is active and its usage counter has been incremented.
570  * * Negative error code on failure; usage counter is unchanged.
571  */
572 static inline int pm_runtime_resume_and_get(struct device *dev)
573 {
574 	return pm_runtime_get_active(dev, 0);
575 }
576 
577 /**
578  * pm_runtime_put - Drop device usage counter and request asynchronous idle check.
579  * @dev: Target device.
580  *
581  * Decrement the runtime PM usage counter of @dev. If the counter reaches zero
582  * and the device has no active child dependencies, asynchronously request the
583  * PM core to idle or suspend the device.
584  */
585 static inline void pm_runtime_put(struct device *dev)
586 {
587 	__pm_runtime_idle(dev, RPM_GET_PUT | RPM_ASYNC);
588 }
589 
590 /**
591  * __pm_runtime_put_autosuspend - Drop device usage counter and queue autosuspend if 0.
592  * @dev: Target device.
593  *
594  * Decrement the runtime PM usage counter of @dev and if it turns out to be
595  * equal to 0, queue up a work item for @dev like in pm_request_autosuspend().
596  *
597  * Return:
598  * * %1: Success. Usage counter dropped to zero, but device was already suspended.
599  * * %0: Success.
600  * * %-EINVAL: Runtime PM error.
601  * * %-EACCES: Runtime PM disabled.
602  * * %-EAGAIN: Runtime PM usage counter became non-zero or Runtime PM status
603  *   change ongoing.
604  * * %-EBUSY: Runtime PM child_count non-zero.
605  * * %-EPERM: Device PM QoS resume latency 0.
606  * * %-EINPROGRESS: Suspend already in progress.
607  * * %-ENOSYS: %CONFIG_PM not enabled.
608  */
609 static inline int __pm_runtime_put_autosuspend(struct device *dev)
610 {
611 	return __pm_runtime_suspend(dev, RPM_GET_PUT | RPM_ASYNC | RPM_AUTO);
612 }
613 
614 /**
615  * pm_runtime_put_autosuspend - Update the last access time, drop usage counter
616  * and request autosuspend.
617  * @dev: Target device.
618  *
619  * Update the last access time of @dev and decrement its runtime PM usage
620  * counter. If the counter drops to zero, asynchronously request the PM core to
621  * suspend the device once its autosuspend delay has elapsed.
622  *
623  * Return:
624  * * %1: Success. Usage counter dropped to zero, but device was already suspended.
625  * * %0: Success.
626  * * %-EINVAL: Runtime PM error.
627  * * %-EACCES: Runtime PM disabled.
628  * * %-EAGAIN: Runtime PM usage counter became non-zero or Runtime PM status
629  *   change ongoing.
630  * * %-EBUSY: Runtime PM child_count non-zero.
631  * * %-EPERM: Device PM QoS resume latency 0.
632  * * %-EINPROGRESS: Suspend already in progress.
633  * * %-ENOSYS: %CONFIG_PM not enabled.
634  */
635 static inline int pm_runtime_put_autosuspend(struct device *dev)
636 {
637 	pm_runtime_mark_last_busy(dev);
638 	return __pm_runtime_put_autosuspend(dev);
639 }
640 
641 DEFINE_GUARD(pm_runtime_noresume, struct device *,
642 	     pm_runtime_get_noresume(_T), pm_runtime_put_noidle(_T));
643 
644 DEFINE_GUARD(pm_runtime_active, struct device *,
645 	     pm_runtime_get_sync(_T), pm_runtime_put(_T));
646 DEFINE_GUARD(pm_runtime_active_auto, struct device *,
647 	     pm_runtime_get_sync(_T), pm_runtime_put_autosuspend(_T));
648 /*
649  * Use the following guards with ACQUIRE()/ACQUIRE_ERR().
650  *
651  * The difference between the "_try" and "_try_enabled" variants is that the
652  * former do not produce an error when runtime PM is disabled for the given
653  * device.
654  */
655 DEFINE_GUARD_COND(pm_runtime_active, _try,
656 		  pm_runtime_get_active(_T, RPM_TRANSPARENT), _RET == 0)
657 DEFINE_GUARD_COND(pm_runtime_active, _try_enabled,
658 		  pm_runtime_resume_and_get(_T), _RET == 0)
659 DEFINE_GUARD_COND(pm_runtime_active_auto, _try,
660 		  pm_runtime_get_active(_T, RPM_TRANSPARENT), _RET == 0)
661 DEFINE_GUARD_COND(pm_runtime_active_auto, _try_enabled,
662 		  pm_runtime_resume_and_get(_T), _RET == 0)
663 
664 /* ACQUIRE() wrapper macros for the guards defined above. */
665 
666 #define PM_RUNTIME_ACQUIRE(_dev, _var)			\
667 	ACQUIRE(pm_runtime_active_try, _var)(_dev)
668 
669 #define PM_RUNTIME_ACQUIRE_AUTOSUSPEND(_dev, _var)	\
670 	ACQUIRE(pm_runtime_active_auto_try, _var)(_dev)
671 
672 #define PM_RUNTIME_ACQUIRE_IF_ENABLED(_dev, _var)	\
673 	ACQUIRE(pm_runtime_active_try_enabled, _var)(_dev)
674 
675 #define PM_RUNTIME_ACQUIRE_IF_ENABLED_AUTOSUSPEND(_dev, _var)	\
676 	ACQUIRE(pm_runtime_active_auto_try_enabled, _var)(_dev)
677 
678 /*
679  * ACQUIRE_ERR() wrapper macro for guard pm_runtime_active.
680  *
681  * Always check PM_RUNTIME_ACQUIRE_ERR() after using one of the
682  * PM_RUNTIME_ACQUIRE*() macros defined above (yes, it can be used with
683  * any of them) and if it is nonzero, avoid accessing the given device.
684  */
685 #define PM_RUNTIME_ACQUIRE_ERR(_var_ptr)	\
686 	ACQUIRE_ERR(pm_runtime_active, _var_ptr)
687 
688 /**
689  * pm_runtime_put_sync - Drop device usage counter and run "idle check" if 0.
690  * @dev: Target device.
691  *
692  * Decrement the runtime PM usage counter of @dev. If the counter drops to zero,
693  * synchronously evaluate and trigger idle/suspend handling.
694  *
695  * Note that this does not update the last access time, but it does respect
696  * existing autosuspend timers. If @dev uses autosuspend, consider using
697  * pm_runtime_put_sync_autosuspend() or pm_runtime_put_sync_suspend() instead.
698  *
699  * The runtime PM usage counter of @dev remains decremented in all cases, even
700  * if it returns an error code.
701  *
702  * Return:
703  * * %1: Success. Usage counter dropped to zero, but device was already suspended.
704  * * %0: Success.
705  * * %-EINVAL: Runtime PM error.
706  * * %-EACCES: Runtime PM disabled.
707  * * %-EAGAIN: Runtime PM usage counter became non-zero or Runtime PM status
708  *   change ongoing.
709  * * %-EBUSY: Runtime PM child_count non-zero.
710  * * %-EPERM: Device PM QoS resume latency 0.
711  * * %-ENOSYS: %CONFIG_PM not enabled.
712  * * Other values and conditions for the above values are possible as returned
713  *   by Runtime PM suspend callbacks.
714  */
715 static inline int pm_runtime_put_sync(struct device *dev)
716 {
717 	return __pm_runtime_idle(dev, RPM_GET_PUT);
718 }
719 
720 /**
721  * pm_runtime_put_sync_suspend - Drop device usage counter and suspend if 0.
722  * @dev: Target device.
723  *
724  * Decrement the runtime PM usage counter of @dev. If the counter drops to zero,
725  * suspend the device synchronously.
726  *
727  * This API differs from pm_runtime_put_sync() and
728  * pm_runtime_put_sync_autosuspend() in that it ignores any outstanding
729  * autosuspend delays.
730  *
731  * The runtime PM usage counter of @dev remains decremented in all cases, even
732  * if it returns an error code.
733  *
734  * Return:
735  * * %1: Success. Usage counter dropped to zero, but device was already suspended.
736  * * %0: Success.
737  * * %-EINVAL: Runtime PM error.
738  * * %-EACCES: Runtime PM disabled.
739  * * %-EAGAIN: Runtime PM usage counter became non-zero or Runtime PM status
740  *   change ongoing.
741  * * %-EBUSY: Runtime PM child_count non-zero.
742  * * %-EPERM: Device PM QoS resume latency 0.
743  * * %-ENOSYS: %CONFIG_PM not enabled.
744  * * Other values and conditions for the above values are possible as returned
745  *   by Runtime PM suspend callbacks.
746  */
747 static inline int pm_runtime_put_sync_suspend(struct device *dev)
748 {
749 	return __pm_runtime_suspend(dev, RPM_GET_PUT);
750 }
751 
752 /**
753  * pm_runtime_put_sync_autosuspend - Update the last access time of a device,
754  * drop device usage counter and autosuspend if 0.
755  * @dev: Target device.
756  *
757  * Update the last access time of @dev and decrement its runtime PM usage
758  * counter. If the counter drops to zero, synchronously suspend the device (or
759  * schedule autosuspend if the delay has not elapsed).
760  *
761  * Prefer this API over pm_runtime_put_sync() for devices that use autosuspend.
762  *
763  * The runtime PM usage counter of @dev remains decremented in all cases, even
764  * if it returns an error code.
765  *
766  * Return:
767  * * %1: Success. Usage counter dropped to zero, but device was already suspended.
768  * * %0: Success.
769  * * %-EINVAL: Runtime PM error.
770  * * %-EACCES: Runtime PM disabled.
771  * * %-EAGAIN: Runtime PM usage counter became non-zero or Runtime PM status
772  *   change ongoing.
773  * * %-EBUSY: Runtime PM child_count non-zero.
774  * * %-EPERM: Device PM QoS resume latency 0.
775  * * %-EINPROGRESS: Suspend already in progress.
776  * * %-ENOSYS: %CONFIG_PM not enabled.
777  * * Other values and conditions for the above values are possible as returned
778  *   by Runtime PM suspend callbacks.
779  */
780 static inline int pm_runtime_put_sync_autosuspend(struct device *dev)
781 {
782 	pm_runtime_mark_last_busy(dev);
783 	return __pm_runtime_suspend(dev, RPM_GET_PUT | RPM_AUTO);
784 }
785 
786 /**
787  * pm_runtime_set_active - Set runtime PM status to "active" and clear errors.
788  * @dev: Target device.
789  *
790  * Set the runtime PM status of @dev to %RPM_ACTIVE and ensure that its
791  * dependencies will be taken into account. Also clear the device's error
792  * status (@dev->power.runtime_error).
793  *
794  * It is only valid to call this function if runtime PM is disabled or if
795  * @dev->power.runtime_error is set.
796  *
797  * This will fail if suppliers cannot be resumed, or if the parent is not in
798  * the correct state.
799  *
800  * Return:
801  * * %0: Success.
802  * * Error code on failure.
803  */
804 static inline int pm_runtime_set_active(struct device *dev)
805 {
806 	return __pm_runtime_set_status(dev, RPM_ACTIVE);
807 }
808 
809 /**
810  * pm_runtime_set_suspended - Set runtime PM status to "suspended" and clear errors.
811  * @dev: Target device.
812  *
813  * Set the runtime PM status of @dev to %RPM_SUSPENDED and ensure that its
814  * dependencies will be taken into account. Also clear the device's error
815  * status (@dev->power.runtime_error).
816  *
817  * It is only valid to call this function if runtime PM is disabled or if
818  * @dev->power.runtime_error is set.
819  *
820  * Return:
821  * * %0: Success.
822  * * Error code on failure.
823  */
824 static inline int pm_runtime_set_suspended(struct device *dev)
825 {
826 	return __pm_runtime_set_status(dev, RPM_SUSPENDED);
827 }
828 
829 /**
830  * pm_runtime_disable - Disable runtime PM for a device.
831  * @dev: Target device.
832  *
833  * Prevent the runtime PM framework from working with @dev by incrementing its
834  * "disable" counter.
835  *
836  * If the counter is zero when this function runs and there is a pending runtime
837  * resume request for @dev, it will be resumed.  If the counter is still zero at
838  * that point, this function cancels all pending runtime PM requests for @dev
839  * and waits for its runtime PM operations to complete (if any).
840  *
841  * For each invocation of this function for @dev, there must be a matching
842  * pm_runtime_enable() call, so that runtime PM is eventually enabled for it
843  * again.
844  */
845 static inline void pm_runtime_disable(struct device *dev)
846 {
847 	__pm_runtime_disable(dev, true);
848 }
849 
850 /**
851  * pm_runtime_use_autosuspend - Allow autosuspend to be used for a device.
852  * @dev: Target device.
853  *
854  * Allow the runtime PM autosuspend mechanism to be used for @dev whenever
855  * requested (or "autosuspend" will be handled as direct runtime-suspend for
856  * it).
857  *
858  * NOTE: It's important to undo this with pm_runtime_dont_use_autosuspend()
859  * at driver exit time unless your driver initially enabled pm_runtime
860  * with devm_pm_runtime_enable() (which handles it for you).
861  */
862 static inline void pm_runtime_use_autosuspend(struct device *dev)
863 {
864 	__pm_runtime_use_autosuspend(dev, true);
865 }
866 
867 /**
868  * pm_runtime_dont_use_autosuspend - Prevent autosuspend from being used.
869  * @dev: Target device.
870  *
871  * Prevent the runtime PM autosuspend mechanism from being used for @dev which
872  * means that "autosuspend" will be handled as direct runtime-suspend for it
873  * going forward.
874  */
875 static inline void pm_runtime_dont_use_autosuspend(struct device *dev)
876 {
877 	__pm_runtime_use_autosuspend(dev, false);
878 }
879 
880 #endif
881