2 * drivers/base/power/main.c - Where the driver meets power management.
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
7 * This file is released under the GPLv2
10 * The driver model core calls device_pm_add() when a device is registered.
11 * This will initialize the embedded device_pm_info object in the device
12 * and add it to the list of power-controlled devices. sysfs entries for
13 * controlling device power management will also be added.
15 * A separate list is used for keeping track of power info, because the power
16 * domain dependencies may differ from the ancestral dependencies that the
17 * subsystem list maintains.
20 #include <linux/device.h>
21 #include <linux/kallsyms.h>
22 #include <linux/mutex.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/resume-trace.h>
26 #include <linux/interrupt.h>
27 #include <linux/sched.h>
28 #include <linux/async.h>
29 #include <linux/suspend.h>
35 * The entries in the dpm_list list are in a depth first order, simply
36 * because children are guaranteed to be discovered after parents, and
37 * are inserted at the back of the list on discovery.
39 * Since device_pm_add() may be called with a device lock held,
40 * we must never try to acquire a device lock while holding
45 LIST_HEAD(dpm_prepared_list
);
46 LIST_HEAD(dpm_suspended_list
);
47 LIST_HEAD(dpm_noirq_list
);
49 struct suspend_stats suspend_stats
;
50 static DEFINE_MUTEX(dpm_list_mtx
);
51 static pm_message_t pm_transition
;
53 static int async_error
;
56 * device_pm_init - Initialize the PM-related part of a device object.
57 * @dev: Device object being initialized.
59 void device_pm_init(struct device
*dev
)
61 dev
->power
.is_prepared
= false;
62 dev
->power
.is_suspended
= false;
63 init_completion(&dev
->power
.completion
);
64 complete_all(&dev
->power
.completion
);
65 dev
->power
.wakeup
= NULL
;
66 spin_lock_init(&dev
->power
.lock
);
68 INIT_LIST_HEAD(&dev
->power
.entry
);
69 dev
->power
.power_state
= PMSG_INVALID
;
73 * device_pm_lock - Lock the list of active devices used by the PM core.
75 void device_pm_lock(void)
77 mutex_lock(&dpm_list_mtx
);
81 * device_pm_unlock - Unlock the list of active devices used by the PM core.
83 void device_pm_unlock(void)
85 mutex_unlock(&dpm_list_mtx
);
89 * device_pm_add - Add a device to the PM core's list of active devices.
90 * @dev: Device to add to the list.
92 void device_pm_add(struct device
*dev
)
94 pr_debug("PM: Adding info for %s:%s\n",
95 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
96 mutex_lock(&dpm_list_mtx
);
97 if (dev
->parent
&& dev
->parent
->power
.is_prepared
)
98 dev_warn(dev
, "parent %s should not be sleeping\n",
99 dev_name(dev
->parent
));
100 list_add_tail(&dev
->power
.entry
, &dpm_list
);
101 dev_pm_qos_constraints_init(dev
);
102 mutex_unlock(&dpm_list_mtx
);
106 * device_pm_remove - Remove a device from the PM core's list of active devices.
107 * @dev: Device to be removed from the list.
109 void device_pm_remove(struct device
*dev
)
111 pr_debug("PM: Removing info for %s:%s\n",
112 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
113 complete_all(&dev
->power
.completion
);
114 mutex_lock(&dpm_list_mtx
);
115 dev_pm_qos_constraints_destroy(dev
);
116 list_del_init(&dev
->power
.entry
);
117 mutex_unlock(&dpm_list_mtx
);
118 device_wakeup_disable(dev
);
119 pm_runtime_remove(dev
);
123 * device_pm_move_before - Move device in the PM core's list of active devices.
124 * @deva: Device to move in dpm_list.
125 * @devb: Device @deva should come before.
127 void device_pm_move_before(struct device
*deva
, struct device
*devb
)
129 pr_debug("PM: Moving %s:%s before %s:%s\n",
130 deva
->bus
? deva
->bus
->name
: "No Bus", dev_name(deva
),
131 devb
->bus
? devb
->bus
->name
: "No Bus", dev_name(devb
));
132 /* Delete deva from dpm_list and reinsert before devb. */
133 list_move_tail(&deva
->power
.entry
, &devb
->power
.entry
);
137 * device_pm_move_after - Move device in the PM core's list of active devices.
138 * @deva: Device to move in dpm_list.
139 * @devb: Device @deva should come after.
141 void device_pm_move_after(struct device
*deva
, struct device
*devb
)
143 pr_debug("PM: Moving %s:%s after %s:%s\n",
144 deva
->bus
? deva
->bus
->name
: "No Bus", dev_name(deva
),
145 devb
->bus
? devb
->bus
->name
: "No Bus", dev_name(devb
));
146 /* Delete deva from dpm_list and reinsert after devb. */
147 list_move(&deva
->power
.entry
, &devb
->power
.entry
);
151 * device_pm_move_last - Move device to end of the PM core's list of devices.
152 * @dev: Device to move in dpm_list.
154 void device_pm_move_last(struct device
*dev
)
156 pr_debug("PM: Moving %s:%s to end of list\n",
157 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
158 list_move_tail(&dev
->power
.entry
, &dpm_list
);
161 static ktime_t
initcall_debug_start(struct device
*dev
)
163 ktime_t calltime
= ktime_set(0, 0);
165 if (initcall_debug
) {
166 pr_info("calling %s+ @ %i\n",
167 dev_name(dev
), task_pid_nr(current
));
168 calltime
= ktime_get();
174 static void initcall_debug_report(struct device
*dev
, ktime_t calltime
,
177 ktime_t delta
, rettime
;
179 if (initcall_debug
) {
180 rettime
= ktime_get();
181 delta
= ktime_sub(rettime
, calltime
);
182 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev
),
183 error
, (unsigned long long)ktime_to_ns(delta
) >> 10);
188 * dpm_wait - Wait for a PM operation to complete.
189 * @dev: Device to wait for.
190 * @async: If unset, wait only if the device's power.async_suspend flag is set.
192 static void dpm_wait(struct device
*dev
, bool async
)
197 if (async
|| (pm_async_enabled
&& dev
->power
.async_suspend
))
198 wait_for_completion(&dev
->power
.completion
);
201 static int dpm_wait_fn(struct device
*dev
, void *async_ptr
)
203 dpm_wait(dev
, *((bool *)async_ptr
));
207 static void dpm_wait_for_children(struct device
*dev
, bool async
)
209 device_for_each_child(dev
, &async
, dpm_wait_fn
);
213 * pm_op - Execute the PM operation appropriate for given PM event.
214 * @dev: Device to handle.
215 * @ops: PM operations to choose from.
216 * @state: PM transition of the system being carried out.
218 static int pm_op(struct device
*dev
,
219 const struct dev_pm_ops
*ops
,
225 calltime
= initcall_debug_start(dev
);
227 switch (state
.event
) {
228 #ifdef CONFIG_SUSPEND
229 case PM_EVENT_SUSPEND
:
231 error
= ops
->suspend(dev
);
232 suspend_report_result(ops
->suspend
, error
);
235 case PM_EVENT_RESUME
:
237 error
= ops
->resume(dev
);
238 suspend_report_result(ops
->resume
, error
);
241 #endif /* CONFIG_SUSPEND */
242 #ifdef CONFIG_HIBERNATE_CALLBACKS
243 case PM_EVENT_FREEZE
:
244 case PM_EVENT_QUIESCE
:
246 error
= ops
->freeze(dev
);
247 suspend_report_result(ops
->freeze
, error
);
250 case PM_EVENT_HIBERNATE
:
252 error
= ops
->poweroff(dev
);
253 suspend_report_result(ops
->poweroff
, error
);
257 case PM_EVENT_RECOVER
:
259 error
= ops
->thaw(dev
);
260 suspend_report_result(ops
->thaw
, error
);
263 case PM_EVENT_RESTORE
:
265 error
= ops
->restore(dev
);
266 suspend_report_result(ops
->restore
, error
);
269 #endif /* CONFIG_HIBERNATE_CALLBACKS */
274 initcall_debug_report(dev
, calltime
, error
);
280 * pm_noirq_op - Execute the PM operation appropriate for given PM event.
281 * @dev: Device to handle.
282 * @ops: PM operations to choose from.
283 * @state: PM transition of the system being carried out.
285 * The driver of @dev will not receive interrupts while this function is being
288 static int pm_noirq_op(struct device
*dev
,
289 const struct dev_pm_ops
*ops
,
293 ktime_t calltime
= ktime_set(0, 0), delta
, rettime
;
295 if (initcall_debug
) {
296 pr_info("calling %s+ @ %i, parent: %s\n",
297 dev_name(dev
), task_pid_nr(current
),
298 dev
->parent
? dev_name(dev
->parent
) : "none");
299 calltime
= ktime_get();
302 switch (state
.event
) {
303 #ifdef CONFIG_SUSPEND
304 case PM_EVENT_SUSPEND
:
305 if (ops
->suspend_noirq
) {
306 error
= ops
->suspend_noirq(dev
);
307 suspend_report_result(ops
->suspend_noirq
, error
);
310 case PM_EVENT_RESUME
:
311 if (ops
->resume_noirq
) {
312 error
= ops
->resume_noirq(dev
);
313 suspend_report_result(ops
->resume_noirq
, error
);
316 #endif /* CONFIG_SUSPEND */
317 #ifdef CONFIG_HIBERNATE_CALLBACKS
318 case PM_EVENT_FREEZE
:
319 case PM_EVENT_QUIESCE
:
320 if (ops
->freeze_noirq
) {
321 error
= ops
->freeze_noirq(dev
);
322 suspend_report_result(ops
->freeze_noirq
, error
);
325 case PM_EVENT_HIBERNATE
:
326 if (ops
->poweroff_noirq
) {
327 error
= ops
->poweroff_noirq(dev
);
328 suspend_report_result(ops
->poweroff_noirq
, error
);
332 case PM_EVENT_RECOVER
:
333 if (ops
->thaw_noirq
) {
334 error
= ops
->thaw_noirq(dev
);
335 suspend_report_result(ops
->thaw_noirq
, error
);
338 case PM_EVENT_RESTORE
:
339 if (ops
->restore_noirq
) {
340 error
= ops
->restore_noirq(dev
);
341 suspend_report_result(ops
->restore_noirq
, error
);
344 #endif /* CONFIG_HIBERNATE_CALLBACKS */
349 if (initcall_debug
) {
350 rettime
= ktime_get();
351 delta
= ktime_sub(rettime
, calltime
);
352 printk("initcall %s_i+ returned %d after %Ld usecs\n",
353 dev_name(dev
), error
,
354 (unsigned long long)ktime_to_ns(delta
) >> 10);
360 static char *pm_verb(int event
)
363 case PM_EVENT_SUSPEND
:
365 case PM_EVENT_RESUME
:
367 case PM_EVENT_FREEZE
:
369 case PM_EVENT_QUIESCE
:
371 case PM_EVENT_HIBERNATE
:
375 case PM_EVENT_RESTORE
:
377 case PM_EVENT_RECOVER
:
380 return "(unknown PM event)";
384 static void pm_dev_dbg(struct device
*dev
, pm_message_t state
, char *info
)
386 dev_dbg(dev
, "%s%s%s\n", info
, pm_verb(state
.event
),
387 ((state
.event
& PM_EVENT_SLEEP
) && device_may_wakeup(dev
)) ?
388 ", may wakeup" : "");
391 static void pm_dev_err(struct device
*dev
, pm_message_t state
, char *info
,
394 printk(KERN_ERR
"PM: Device %s failed to %s%s: error %d\n",
395 dev_name(dev
), pm_verb(state
.event
), info
, error
);
398 static void dpm_show_time(ktime_t starttime
, pm_message_t state
, char *info
)
404 calltime
= ktime_get();
405 usecs64
= ktime_to_ns(ktime_sub(calltime
, starttime
));
406 do_div(usecs64
, NSEC_PER_USEC
);
410 pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
411 info
?: "", info
? " " : "", pm_verb(state
.event
),
412 usecs
/ USEC_PER_MSEC
, usecs
% USEC_PER_MSEC
);
415 /*------------------------- Resume routines -------------------------*/
418 * device_resume_noirq - Execute an "early resume" callback for given device.
419 * @dev: Device to handle.
420 * @state: PM transition of the system being carried out.
422 * The driver of @dev will not receive interrupts while this function is being
425 static int device_resume_noirq(struct device
*dev
, pm_message_t state
)
432 if (dev
->pm_domain
) {
433 pm_dev_dbg(dev
, state
, "EARLY power domain ");
434 error
= pm_noirq_op(dev
, &dev
->pm_domain
->ops
, state
);
435 } else if (dev
->type
&& dev
->type
->pm
) {
436 pm_dev_dbg(dev
, state
, "EARLY type ");
437 error
= pm_noirq_op(dev
, dev
->type
->pm
, state
);
438 } else if (dev
->class && dev
->class->pm
) {
439 pm_dev_dbg(dev
, state
, "EARLY class ");
440 error
= pm_noirq_op(dev
, dev
->class->pm
, state
);
441 } else if (dev
->bus
&& dev
->bus
->pm
) {
442 pm_dev_dbg(dev
, state
, "EARLY ");
443 error
= pm_noirq_op(dev
, dev
->bus
->pm
, state
);
451 * dpm_resume_noirq - Execute "early resume" callbacks for non-sysdev devices.
452 * @state: PM transition of the system being carried out.
454 * Call the "noirq" resume handlers for all devices marked as DPM_OFF_IRQ and
455 * enable device drivers to receive interrupts.
457 void dpm_resume_noirq(pm_message_t state
)
459 ktime_t starttime
= ktime_get();
461 mutex_lock(&dpm_list_mtx
);
462 while (!list_empty(&dpm_noirq_list
)) {
463 struct device
*dev
= to_device(dpm_noirq_list
.next
);
467 list_move_tail(&dev
->power
.entry
, &dpm_suspended_list
);
468 mutex_unlock(&dpm_list_mtx
);
470 error
= device_resume_noirq(dev
, state
);
472 suspend_stats
.failed_resume_noirq
++;
473 dpm_save_failed_step(SUSPEND_RESUME_NOIRQ
);
474 dpm_save_failed_dev(dev_name(dev
));
475 pm_dev_err(dev
, state
, " early", error
);
478 mutex_lock(&dpm_list_mtx
);
481 mutex_unlock(&dpm_list_mtx
);
482 dpm_show_time(starttime
, state
, "early");
483 resume_device_irqs();
485 EXPORT_SYMBOL_GPL(dpm_resume_noirq
);
488 * legacy_resume - Execute a legacy (bus or class) resume callback for device.
489 * @dev: Device to resume.
490 * @cb: Resume callback to execute.
492 static int legacy_resume(struct device
*dev
, int (*cb
)(struct device
*dev
))
497 calltime
= initcall_debug_start(dev
);
500 suspend_report_result(cb
, error
);
502 initcall_debug_report(dev
, calltime
, error
);
508 * device_resume - Execute "resume" callbacks for given device.
509 * @dev: Device to handle.
510 * @state: PM transition of the system being carried out.
511 * @async: If true, the device is being resumed asynchronously.
513 static int device_resume(struct device
*dev
, pm_message_t state
, bool async
)
521 dpm_wait(dev
->parent
, async
);
525 * This is a fib. But we'll allow new children to be added below
526 * a resumed device, even if the device hasn't been completed yet.
528 dev
->power
.is_prepared
= false;
530 if (!dev
->power
.is_suspended
)
533 pm_runtime_enable(dev
);
536 if (dev
->pm_domain
) {
537 pm_dev_dbg(dev
, state
, "power domain ");
538 error
= pm_op(dev
, &dev
->pm_domain
->ops
, state
);
542 if (dev
->type
&& dev
->type
->pm
) {
543 pm_dev_dbg(dev
, state
, "type ");
544 error
= pm_op(dev
, dev
->type
->pm
, state
);
549 if (dev
->class->pm
) {
550 pm_dev_dbg(dev
, state
, "class ");
551 error
= pm_op(dev
, dev
->class->pm
, state
);
553 } else if (dev
->class->resume
) {
554 pm_dev_dbg(dev
, state
, "legacy class ");
555 error
= legacy_resume(dev
, dev
->class->resume
);
562 pm_dev_dbg(dev
, state
, "");
563 error
= pm_op(dev
, dev
->bus
->pm
, state
);
564 } else if (dev
->bus
->resume
) {
565 pm_dev_dbg(dev
, state
, "legacy ");
566 error
= legacy_resume(dev
, dev
->bus
->resume
);
571 dev
->power
.is_suspended
= false;
575 complete_all(&dev
->power
.completion
);
580 pm_runtime_put_sync(dev
);
585 static void async_resume(void *data
, async_cookie_t cookie
)
587 struct device
*dev
= (struct device
*)data
;
590 error
= device_resume(dev
, pm_transition
, true);
592 pm_dev_err(dev
, pm_transition
, " async", error
);
596 static bool is_async(struct device
*dev
)
598 return dev
->power
.async_suspend
&& pm_async_enabled
599 && !pm_trace_is_enabled();
603 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
604 * @state: PM transition of the system being carried out.
606 * Execute the appropriate "resume" callback for all devices whose status
607 * indicates that they are suspended.
609 void dpm_resume(pm_message_t state
)
612 ktime_t starttime
= ktime_get();
616 mutex_lock(&dpm_list_mtx
);
617 pm_transition
= state
;
620 list_for_each_entry(dev
, &dpm_suspended_list
, power
.entry
) {
621 INIT_COMPLETION(dev
->power
.completion
);
624 async_schedule(async_resume
, dev
);
628 while (!list_empty(&dpm_suspended_list
)) {
629 dev
= to_device(dpm_suspended_list
.next
);
631 if (!is_async(dev
)) {
634 mutex_unlock(&dpm_list_mtx
);
636 error
= device_resume(dev
, state
, false);
638 suspend_stats
.failed_resume
++;
639 dpm_save_failed_step(SUSPEND_RESUME
);
640 dpm_save_failed_dev(dev_name(dev
));
641 pm_dev_err(dev
, state
, "", error
);
644 mutex_lock(&dpm_list_mtx
);
646 if (!list_empty(&dev
->power
.entry
))
647 list_move_tail(&dev
->power
.entry
, &dpm_prepared_list
);
650 mutex_unlock(&dpm_list_mtx
);
651 async_synchronize_full();
652 dpm_show_time(starttime
, state
, NULL
);
656 * device_complete - Complete a PM transition for given device.
657 * @dev: Device to handle.
658 * @state: PM transition of the system being carried out.
660 static void device_complete(struct device
*dev
, pm_message_t state
)
664 if (dev
->pm_domain
) {
665 pm_dev_dbg(dev
, state
, "completing power domain ");
666 if (dev
->pm_domain
->ops
.complete
)
667 dev
->pm_domain
->ops
.complete(dev
);
668 } else if (dev
->type
&& dev
->type
->pm
) {
669 pm_dev_dbg(dev
, state
, "completing type ");
670 if (dev
->type
->pm
->complete
)
671 dev
->type
->pm
->complete(dev
);
672 } else if (dev
->class && dev
->class->pm
) {
673 pm_dev_dbg(dev
, state
, "completing class ");
674 if (dev
->class->pm
->complete
)
675 dev
->class->pm
->complete(dev
);
676 } else if (dev
->bus
&& dev
->bus
->pm
) {
677 pm_dev_dbg(dev
, state
, "completing ");
678 if (dev
->bus
->pm
->complete
)
679 dev
->bus
->pm
->complete(dev
);
686 * dpm_complete - Complete a PM transition for all non-sysdev devices.
687 * @state: PM transition of the system being carried out.
689 * Execute the ->complete() callbacks for all devices whose PM status is not
690 * DPM_ON (this allows new devices to be registered).
692 void dpm_complete(pm_message_t state
)
694 struct list_head list
;
698 INIT_LIST_HEAD(&list
);
699 mutex_lock(&dpm_list_mtx
);
700 while (!list_empty(&dpm_prepared_list
)) {
701 struct device
*dev
= to_device(dpm_prepared_list
.prev
);
704 dev
->power
.is_prepared
= false;
705 list_move(&dev
->power
.entry
, &list
);
706 mutex_unlock(&dpm_list_mtx
);
708 device_complete(dev
, state
);
710 mutex_lock(&dpm_list_mtx
);
713 list_splice(&list
, &dpm_list
);
714 mutex_unlock(&dpm_list_mtx
);
718 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
719 * @state: PM transition of the system being carried out.
721 * Execute "resume" callbacks for all devices and complete the PM transition of
724 void dpm_resume_end(pm_message_t state
)
729 EXPORT_SYMBOL_GPL(dpm_resume_end
);
732 /*------------------------- Suspend routines -------------------------*/
735 * resume_event - Return a "resume" message for given "suspend" sleep state.
736 * @sleep_state: PM message representing a sleep state.
738 * Return a PM message representing the resume event corresponding to given
741 static pm_message_t
resume_event(pm_message_t sleep_state
)
743 switch (sleep_state
.event
) {
744 case PM_EVENT_SUSPEND
:
746 case PM_EVENT_FREEZE
:
747 case PM_EVENT_QUIESCE
:
749 case PM_EVENT_HIBERNATE
:
756 * device_suspend_noirq - Execute a "late suspend" callback for given device.
757 * @dev: Device to handle.
758 * @state: PM transition of the system being carried out.
760 * The driver of @dev will not receive interrupts while this function is being
763 static int device_suspend_noirq(struct device
*dev
, pm_message_t state
)
767 if (dev
->pm_domain
) {
768 pm_dev_dbg(dev
, state
, "LATE power domain ");
769 error
= pm_noirq_op(dev
, &dev
->pm_domain
->ops
, state
);
772 } else if (dev
->type
&& dev
->type
->pm
) {
773 pm_dev_dbg(dev
, state
, "LATE type ");
774 error
= pm_noirq_op(dev
, dev
->type
->pm
, state
);
777 } else if (dev
->class && dev
->class->pm
) {
778 pm_dev_dbg(dev
, state
, "LATE class ");
779 error
= pm_noirq_op(dev
, dev
->class->pm
, state
);
782 } else if (dev
->bus
&& dev
->bus
->pm
) {
783 pm_dev_dbg(dev
, state
, "LATE ");
784 error
= pm_noirq_op(dev
, dev
->bus
->pm
, state
);
793 * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices.
794 * @state: PM transition of the system being carried out.
796 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
797 * handlers for all non-sysdev devices.
799 int dpm_suspend_noirq(pm_message_t state
)
801 ktime_t starttime
= ktime_get();
804 suspend_device_irqs();
805 mutex_lock(&dpm_list_mtx
);
806 while (!list_empty(&dpm_suspended_list
)) {
807 struct device
*dev
= to_device(dpm_suspended_list
.prev
);
810 mutex_unlock(&dpm_list_mtx
);
812 error
= device_suspend_noirq(dev
, state
);
814 mutex_lock(&dpm_list_mtx
);
816 pm_dev_err(dev
, state
, " late", error
);
817 suspend_stats
.failed_suspend_noirq
++;
818 dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ
);
819 dpm_save_failed_dev(dev_name(dev
));
823 if (!list_empty(&dev
->power
.entry
))
824 list_move(&dev
->power
.entry
, &dpm_noirq_list
);
827 mutex_unlock(&dpm_list_mtx
);
829 dpm_resume_noirq(resume_event(state
));
831 dpm_show_time(starttime
, state
, "late");
834 EXPORT_SYMBOL_GPL(dpm_suspend_noirq
);
837 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
838 * @dev: Device to suspend.
839 * @state: PM transition of the system being carried out.
840 * @cb: Suspend callback to execute.
842 static int legacy_suspend(struct device
*dev
, pm_message_t state
,
843 int (*cb
)(struct device
*dev
, pm_message_t state
))
848 calltime
= initcall_debug_start(dev
);
850 error
= cb(dev
, state
);
851 suspend_report_result(cb
, error
);
853 initcall_debug_report(dev
, calltime
, error
);
859 * device_suspend - Execute "suspend" callbacks for given device.
860 * @dev: Device to handle.
861 * @state: PM transition of the system being carried out.
862 * @async: If true, the device is being suspended asynchronously.
864 static int __device_suspend(struct device
*dev
, pm_message_t state
, bool async
)
868 dpm_wait_for_children(dev
, async
);
873 pm_runtime_get_noresume(dev
);
874 if (pm_runtime_barrier(dev
) && device_may_wakeup(dev
))
875 pm_wakeup_event(dev
, 0);
877 if (pm_wakeup_pending()) {
878 pm_runtime_put_sync(dev
);
879 async_error
= -EBUSY
;
885 if (dev
->pm_domain
) {
886 pm_dev_dbg(dev
, state
, "power domain ");
887 error
= pm_op(dev
, &dev
->pm_domain
->ops
, state
);
891 if (dev
->type
&& dev
->type
->pm
) {
892 pm_dev_dbg(dev
, state
, "type ");
893 error
= pm_op(dev
, dev
->type
->pm
, state
);
898 if (dev
->class->pm
) {
899 pm_dev_dbg(dev
, state
, "class ");
900 error
= pm_op(dev
, dev
->class->pm
, state
);
902 } else if (dev
->class->suspend
) {
903 pm_dev_dbg(dev
, state
, "legacy class ");
904 error
= legacy_suspend(dev
, state
, dev
->class->suspend
);
911 pm_dev_dbg(dev
, state
, "");
912 error
= pm_op(dev
, dev
->bus
->pm
, state
);
913 } else if (dev
->bus
->suspend
) {
914 pm_dev_dbg(dev
, state
, "legacy ");
915 error
= legacy_suspend(dev
, state
, dev
->bus
->suspend
);
921 dev
->power
.is_suspended
= true;
922 if (dev
->power
.wakeup_path
&& dev
->parent
)
923 dev
->parent
->power
.wakeup_path
= true;
927 complete_all(&dev
->power
.completion
);
930 pm_runtime_put_sync(dev
);
932 } else if (dev
->power
.is_suspended
) {
933 __pm_runtime_disable(dev
, false);
939 static void async_suspend(void *data
, async_cookie_t cookie
)
941 struct device
*dev
= (struct device
*)data
;
944 error
= __device_suspend(dev
, pm_transition
, true);
946 dpm_save_failed_dev(dev_name(dev
));
947 pm_dev_err(dev
, pm_transition
, " async", error
);
953 static int device_suspend(struct device
*dev
)
955 INIT_COMPLETION(dev
->power
.completion
);
957 if (pm_async_enabled
&& dev
->power
.async_suspend
) {
959 async_schedule(async_suspend
, dev
);
963 return __device_suspend(dev
, pm_transition
, false);
967 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
968 * @state: PM transition of the system being carried out.
970 int dpm_suspend(pm_message_t state
)
972 ktime_t starttime
= ktime_get();
977 mutex_lock(&dpm_list_mtx
);
978 pm_transition
= state
;
980 while (!list_empty(&dpm_prepared_list
)) {
981 struct device
*dev
= to_device(dpm_prepared_list
.prev
);
984 mutex_unlock(&dpm_list_mtx
);
986 error
= device_suspend(dev
);
988 mutex_lock(&dpm_list_mtx
);
990 pm_dev_err(dev
, state
, "", error
);
991 dpm_save_failed_dev(dev_name(dev
));
995 if (!list_empty(&dev
->power
.entry
))
996 list_move(&dev
->power
.entry
, &dpm_suspended_list
);
1001 mutex_unlock(&dpm_list_mtx
);
1002 async_synchronize_full();
1004 error
= async_error
;
1006 suspend_stats
.failed_suspend
++;
1007 dpm_save_failed_step(SUSPEND_SUSPEND
);
1009 dpm_show_time(starttime
, state
, NULL
);
1014 * device_prepare - Prepare a device for system power transition.
1015 * @dev: Device to handle.
1016 * @state: PM transition of the system being carried out.
1018 * Execute the ->prepare() callback(s) for given device. No new children of the
1019 * device may be registered after this function has returned.
1021 static int device_prepare(struct device
*dev
, pm_message_t state
)
1027 dev
->power
.wakeup_path
= device_may_wakeup(dev
);
1029 if (dev
->pm_domain
) {
1030 pm_dev_dbg(dev
, state
, "preparing power domain ");
1031 if (dev
->pm_domain
->ops
.prepare
)
1032 error
= dev
->pm_domain
->ops
.prepare(dev
);
1033 suspend_report_result(dev
->pm_domain
->ops
.prepare
, error
);
1036 } else if (dev
->type
&& dev
->type
->pm
) {
1037 pm_dev_dbg(dev
, state
, "preparing type ");
1038 if (dev
->type
->pm
->prepare
)
1039 error
= dev
->type
->pm
->prepare(dev
);
1040 suspend_report_result(dev
->type
->pm
->prepare
, error
);
1043 } else if (dev
->class && dev
->class->pm
) {
1044 pm_dev_dbg(dev
, state
, "preparing class ");
1045 if (dev
->class->pm
->prepare
)
1046 error
= dev
->class->pm
->prepare(dev
);
1047 suspend_report_result(dev
->class->pm
->prepare
, error
);
1050 } else if (dev
->bus
&& dev
->bus
->pm
) {
1051 pm_dev_dbg(dev
, state
, "preparing ");
1052 if (dev
->bus
->pm
->prepare
)
1053 error
= dev
->bus
->pm
->prepare(dev
);
1054 suspend_report_result(dev
->bus
->pm
->prepare
, error
);
1064 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1065 * @state: PM transition of the system being carried out.
1067 * Execute the ->prepare() callback(s) for all devices.
1069 int dpm_prepare(pm_message_t state
)
1075 mutex_lock(&dpm_list_mtx
);
1076 while (!list_empty(&dpm_list
)) {
1077 struct device
*dev
= to_device(dpm_list
.next
);
1080 mutex_unlock(&dpm_list_mtx
);
1082 error
= device_prepare(dev
, state
);
1084 mutex_lock(&dpm_list_mtx
);
1086 if (error
== -EAGAIN
) {
1091 printk(KERN_INFO
"PM: Device %s not prepared "
1092 "for power transition: code %d\n",
1093 dev_name(dev
), error
);
1097 dev
->power
.is_prepared
= true;
1098 if (!list_empty(&dev
->power
.entry
))
1099 list_move_tail(&dev
->power
.entry
, &dpm_prepared_list
);
1102 mutex_unlock(&dpm_list_mtx
);
1107 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1108 * @state: PM transition of the system being carried out.
1110 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1111 * callbacks for them.
1113 int dpm_suspend_start(pm_message_t state
)
1117 error
= dpm_prepare(state
);
1119 suspend_stats
.failed_prepare
++;
1120 dpm_save_failed_step(SUSPEND_PREPARE
);
1122 error
= dpm_suspend(state
);
1125 EXPORT_SYMBOL_GPL(dpm_suspend_start
);
1127 void __suspend_report_result(const char *function
, void *fn
, int ret
)
1130 printk(KERN_ERR
"%s(): %pF returns %d\n", function
, fn
, ret
);
1132 EXPORT_SYMBOL_GPL(__suspend_report_result
);
1135 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1136 * @dev: Device to wait for.
1137 * @subordinate: Device that needs to wait for @dev.
1139 int device_pm_wait_for_dev(struct device
*subordinate
, struct device
*dev
)
1141 dpm_wait(dev
, subordinate
->power
.async_suspend
);
1144 EXPORT_SYMBOL_GPL(device_pm_wait_for_dev
);