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/export.h>
23 #include <linux/mutex.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/resume-trace.h>
27 #include <linux/interrupt.h>
28 #include <linux/sched.h>
29 #include <linux/async.h>
30 #include <linux/suspend.h>
36 * The entries in the dpm_list list are in a depth first order, simply
37 * because children are guaranteed to be discovered after parents, and
38 * are inserted at the back of the list on discovery.
40 * Since device_pm_add() may be called with a device lock held,
41 * we must never try to acquire a device lock while holding
46 LIST_HEAD(dpm_prepared_list
);
47 LIST_HEAD(dpm_suspended_list
);
48 LIST_HEAD(dpm_noirq_list
);
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
);
72 * device_pm_lock - Lock the list of active devices used by the PM core.
74 void device_pm_lock(void)
76 mutex_lock(&dpm_list_mtx
);
80 * device_pm_unlock - Unlock the list of active devices used by the PM core.
82 void device_pm_unlock(void)
84 mutex_unlock(&dpm_list_mtx
);
88 * device_pm_add - Add a device to the PM core's list of active devices.
89 * @dev: Device to add to the list.
91 void device_pm_add(struct device
*dev
)
93 pr_debug("PM: Adding info for %s:%s\n",
94 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
95 mutex_lock(&dpm_list_mtx
);
96 if (dev
->parent
&& dev
->parent
->power
.is_prepared
)
97 dev_warn(dev
, "parent %s should not be sleeping\n",
98 dev_name(dev
->parent
));
99 list_add_tail(&dev
->power
.entry
, &dpm_list
);
100 mutex_unlock(&dpm_list_mtx
);
104 * device_pm_remove - Remove a device from the PM core's list of active devices.
105 * @dev: Device to be removed from the list.
107 void device_pm_remove(struct device
*dev
)
109 pr_debug("PM: Removing info for %s:%s\n",
110 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
111 complete_all(&dev
->power
.completion
);
112 mutex_lock(&dpm_list_mtx
);
113 list_del_init(&dev
->power
.entry
);
114 mutex_unlock(&dpm_list_mtx
);
115 device_wakeup_disable(dev
);
116 pm_runtime_remove(dev
);
120 * device_pm_move_before - Move device in the PM core's list of active devices.
121 * @deva: Device to move in dpm_list.
122 * @devb: Device @deva should come before.
124 void device_pm_move_before(struct device
*deva
, struct device
*devb
)
126 pr_debug("PM: Moving %s:%s before %s:%s\n",
127 deva
->bus
? deva
->bus
->name
: "No Bus", dev_name(deva
),
128 devb
->bus
? devb
->bus
->name
: "No Bus", dev_name(devb
));
129 /* Delete deva from dpm_list and reinsert before devb. */
130 list_move_tail(&deva
->power
.entry
, &devb
->power
.entry
);
134 * device_pm_move_after - Move device in the PM core's list of active devices.
135 * @deva: Device to move in dpm_list.
136 * @devb: Device @deva should come after.
138 void device_pm_move_after(struct device
*deva
, struct device
*devb
)
140 pr_debug("PM: Moving %s:%s after %s:%s\n",
141 deva
->bus
? deva
->bus
->name
: "No Bus", dev_name(deva
),
142 devb
->bus
? devb
->bus
->name
: "No Bus", dev_name(devb
));
143 /* Delete deva from dpm_list and reinsert after devb. */
144 list_move(&deva
->power
.entry
, &devb
->power
.entry
);
148 * device_pm_move_last - Move device to end of the PM core's list of devices.
149 * @dev: Device to move in dpm_list.
151 void device_pm_move_last(struct device
*dev
)
153 pr_debug("PM: Moving %s:%s to end of list\n",
154 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
155 list_move_tail(&dev
->power
.entry
, &dpm_list
);
158 static ktime_t
initcall_debug_start(struct device
*dev
)
160 ktime_t calltime
= ktime_set(0, 0);
162 if (initcall_debug
) {
163 pr_info("calling %s+ @ %i\n",
164 dev_name(dev
), task_pid_nr(current
));
165 calltime
= ktime_get();
171 static void initcall_debug_report(struct device
*dev
, ktime_t calltime
,
174 ktime_t delta
, rettime
;
176 if (initcall_debug
) {
177 rettime
= ktime_get();
178 delta
= ktime_sub(rettime
, calltime
);
179 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev
),
180 error
, (unsigned long long)ktime_to_ns(delta
) >> 10);
185 * dpm_wait - Wait for a PM operation to complete.
186 * @dev: Device to wait for.
187 * @async: If unset, wait only if the device's power.async_suspend flag is set.
189 static void dpm_wait(struct device
*dev
, bool async
)
194 if (async
|| (pm_async_enabled
&& dev
->power
.async_suspend
))
195 wait_for_completion(&dev
->power
.completion
);
198 static int dpm_wait_fn(struct device
*dev
, void *async_ptr
)
200 dpm_wait(dev
, *((bool *)async_ptr
));
204 static void dpm_wait_for_children(struct device
*dev
, bool async
)
206 device_for_each_child(dev
, &async
, dpm_wait_fn
);
210 * pm_op - Execute the PM operation appropriate for given PM event.
211 * @dev: Device to handle.
212 * @ops: PM operations to choose from.
213 * @state: PM transition of the system being carried out.
215 static int pm_op(struct device
*dev
,
216 const struct dev_pm_ops
*ops
,
222 calltime
= initcall_debug_start(dev
);
224 switch (state
.event
) {
225 #ifdef CONFIG_SUSPEND
226 case PM_EVENT_SUSPEND
:
228 error
= ops
->suspend(dev
);
229 suspend_report_result(ops
->suspend
, error
);
232 case PM_EVENT_RESUME
:
234 error
= ops
->resume(dev
);
235 suspend_report_result(ops
->resume
, error
);
238 #endif /* CONFIG_SUSPEND */
239 #ifdef CONFIG_HIBERNATE_CALLBACKS
240 case PM_EVENT_FREEZE
:
241 case PM_EVENT_QUIESCE
:
243 error
= ops
->freeze(dev
);
244 suspend_report_result(ops
->freeze
, error
);
247 case PM_EVENT_HIBERNATE
:
249 error
= ops
->poweroff(dev
);
250 suspend_report_result(ops
->poweroff
, error
);
254 case PM_EVENT_RECOVER
:
256 error
= ops
->thaw(dev
);
257 suspend_report_result(ops
->thaw
, error
);
260 case PM_EVENT_RESTORE
:
262 error
= ops
->restore(dev
);
263 suspend_report_result(ops
->restore
, error
);
266 #endif /* CONFIG_HIBERNATE_CALLBACKS */
271 initcall_debug_report(dev
, calltime
, error
);
277 * pm_noirq_op - Execute the PM operation appropriate for given PM event.
278 * @dev: Device to handle.
279 * @ops: PM operations to choose from.
280 * @state: PM transition of the system being carried out.
282 * The driver of @dev will not receive interrupts while this function is being
285 static int pm_noirq_op(struct device
*dev
,
286 const struct dev_pm_ops
*ops
,
290 ktime_t calltime
= ktime_set(0, 0), delta
, rettime
;
292 if (initcall_debug
) {
293 pr_info("calling %s+ @ %i, parent: %s\n",
294 dev_name(dev
), task_pid_nr(current
),
295 dev
->parent
? dev_name(dev
->parent
) : "none");
296 calltime
= ktime_get();
299 switch (state
.event
) {
300 #ifdef CONFIG_SUSPEND
301 case PM_EVENT_SUSPEND
:
302 if (ops
->suspend_noirq
) {
303 error
= ops
->suspend_noirq(dev
);
304 suspend_report_result(ops
->suspend_noirq
, error
);
307 case PM_EVENT_RESUME
:
308 if (ops
->resume_noirq
) {
309 error
= ops
->resume_noirq(dev
);
310 suspend_report_result(ops
->resume_noirq
, error
);
313 #endif /* CONFIG_SUSPEND */
314 #ifdef CONFIG_HIBERNATE_CALLBACKS
315 case PM_EVENT_FREEZE
:
316 case PM_EVENT_QUIESCE
:
317 if (ops
->freeze_noirq
) {
318 error
= ops
->freeze_noirq(dev
);
319 suspend_report_result(ops
->freeze_noirq
, error
);
322 case PM_EVENT_HIBERNATE
:
323 if (ops
->poweroff_noirq
) {
324 error
= ops
->poweroff_noirq(dev
);
325 suspend_report_result(ops
->poweroff_noirq
, error
);
329 case PM_EVENT_RECOVER
:
330 if (ops
->thaw_noirq
) {
331 error
= ops
->thaw_noirq(dev
);
332 suspend_report_result(ops
->thaw_noirq
, error
);
335 case PM_EVENT_RESTORE
:
336 if (ops
->restore_noirq
) {
337 error
= ops
->restore_noirq(dev
);
338 suspend_report_result(ops
->restore_noirq
, error
);
341 #endif /* CONFIG_HIBERNATE_CALLBACKS */
346 if (initcall_debug
) {
347 rettime
= ktime_get();
348 delta
= ktime_sub(rettime
, calltime
);
349 printk("initcall %s_i+ returned %d after %Ld usecs\n",
350 dev_name(dev
), error
,
351 (unsigned long long)ktime_to_ns(delta
) >> 10);
357 static char *pm_verb(int event
)
360 case PM_EVENT_SUSPEND
:
362 case PM_EVENT_RESUME
:
364 case PM_EVENT_FREEZE
:
366 case PM_EVENT_QUIESCE
:
368 case PM_EVENT_HIBERNATE
:
372 case PM_EVENT_RESTORE
:
374 case PM_EVENT_RECOVER
:
377 return "(unknown PM event)";
381 static void pm_dev_dbg(struct device
*dev
, pm_message_t state
, char *info
)
383 dev_dbg(dev
, "%s%s%s\n", info
, pm_verb(state
.event
),
384 ((state
.event
& PM_EVENT_SLEEP
) && device_may_wakeup(dev
)) ?
385 ", may wakeup" : "");
388 static void pm_dev_err(struct device
*dev
, pm_message_t state
, char *info
,
391 printk(KERN_ERR
"PM: Device %s failed to %s%s: error %d\n",
392 dev_name(dev
), pm_verb(state
.event
), info
, error
);
395 static void dpm_show_time(ktime_t starttime
, pm_message_t state
, char *info
)
401 calltime
= ktime_get();
402 usecs64
= ktime_to_ns(ktime_sub(calltime
, starttime
));
403 do_div(usecs64
, NSEC_PER_USEC
);
407 pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
408 info
?: "", info
? " " : "", pm_verb(state
.event
),
409 usecs
/ USEC_PER_MSEC
, usecs
% USEC_PER_MSEC
);
412 /*------------------------- Resume routines -------------------------*/
415 * device_resume_noirq - Execute an "early resume" callback for given device.
416 * @dev: Device to handle.
417 * @state: PM transition of the system being carried out.
419 * The driver of @dev will not receive interrupts while this function is being
422 static int device_resume_noirq(struct device
*dev
, pm_message_t state
)
429 if (dev
->pm_domain
) {
430 pm_dev_dbg(dev
, state
, "EARLY power domain ");
431 error
= pm_noirq_op(dev
, &dev
->pm_domain
->ops
, state
);
432 } else if (dev
->type
&& dev
->type
->pm
) {
433 pm_dev_dbg(dev
, state
, "EARLY type ");
434 error
= pm_noirq_op(dev
, dev
->type
->pm
, state
);
435 } else if (dev
->class && dev
->class->pm
) {
436 pm_dev_dbg(dev
, state
, "EARLY class ");
437 error
= pm_noirq_op(dev
, dev
->class->pm
, state
);
438 } else if (dev
->bus
&& dev
->bus
->pm
) {
439 pm_dev_dbg(dev
, state
, "EARLY ");
440 error
= pm_noirq_op(dev
, dev
->bus
->pm
, state
);
448 * dpm_resume_noirq - Execute "early resume" callbacks for non-sysdev devices.
449 * @state: PM transition of the system being carried out.
451 * Call the "noirq" resume handlers for all devices marked as DPM_OFF_IRQ and
452 * enable device drivers to receive interrupts.
454 void dpm_resume_noirq(pm_message_t state
)
456 ktime_t starttime
= ktime_get();
458 mutex_lock(&dpm_list_mtx
);
459 while (!list_empty(&dpm_noirq_list
)) {
460 struct device
*dev
= to_device(dpm_noirq_list
.next
);
464 list_move_tail(&dev
->power
.entry
, &dpm_suspended_list
);
465 mutex_unlock(&dpm_list_mtx
);
467 error
= device_resume_noirq(dev
, state
);
469 pm_dev_err(dev
, state
, " early", error
);
471 mutex_lock(&dpm_list_mtx
);
474 mutex_unlock(&dpm_list_mtx
);
475 dpm_show_time(starttime
, state
, "early");
476 resume_device_irqs();
478 EXPORT_SYMBOL_GPL(dpm_resume_noirq
);
481 * legacy_resume - Execute a legacy (bus or class) resume callback for device.
482 * @dev: Device to resume.
483 * @cb: Resume callback to execute.
485 static int legacy_resume(struct device
*dev
, int (*cb
)(struct device
*dev
))
490 calltime
= initcall_debug_start(dev
);
493 suspend_report_result(cb
, error
);
495 initcall_debug_report(dev
, calltime
, error
);
501 * device_resume - Execute "resume" callbacks for given device.
502 * @dev: Device to handle.
503 * @state: PM transition of the system being carried out.
504 * @async: If true, the device is being resumed asynchronously.
506 static int device_resume(struct device
*dev
, pm_message_t state
, bool async
)
514 dpm_wait(dev
->parent
, async
);
518 * This is a fib. But we'll allow new children to be added below
519 * a resumed device, even if the device hasn't been completed yet.
521 dev
->power
.is_prepared
= false;
523 if (!dev
->power
.is_suspended
)
526 pm_runtime_enable(dev
);
529 if (dev
->pm_domain
) {
530 pm_dev_dbg(dev
, state
, "power domain ");
531 error
= pm_op(dev
, &dev
->pm_domain
->ops
, state
);
535 if (dev
->type
&& dev
->type
->pm
) {
536 pm_dev_dbg(dev
, state
, "type ");
537 error
= pm_op(dev
, dev
->type
->pm
, state
);
542 if (dev
->class->pm
) {
543 pm_dev_dbg(dev
, state
, "class ");
544 error
= pm_op(dev
, dev
->class->pm
, state
);
546 } else if (dev
->class->resume
) {
547 pm_dev_dbg(dev
, state
, "legacy class ");
548 error
= legacy_resume(dev
, dev
->class->resume
);
555 pm_dev_dbg(dev
, state
, "");
556 error
= pm_op(dev
, dev
->bus
->pm
, state
);
557 } else if (dev
->bus
->resume
) {
558 pm_dev_dbg(dev
, state
, "legacy ");
559 error
= legacy_resume(dev
, dev
->bus
->resume
);
564 dev
->power
.is_suspended
= false;
568 complete_all(&dev
->power
.completion
);
573 pm_runtime_put_sync(dev
);
578 static void async_resume(void *data
, async_cookie_t cookie
)
580 struct device
*dev
= (struct device
*)data
;
583 error
= device_resume(dev
, pm_transition
, true);
585 pm_dev_err(dev
, pm_transition
, " async", error
);
589 static bool is_async(struct device
*dev
)
591 return dev
->power
.async_suspend
&& pm_async_enabled
592 && !pm_trace_is_enabled();
596 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
597 * @state: PM transition of the system being carried out.
599 * Execute the appropriate "resume" callback for all devices whose status
600 * indicates that they are suspended.
602 void dpm_resume(pm_message_t state
)
605 ktime_t starttime
= ktime_get();
609 mutex_lock(&dpm_list_mtx
);
610 pm_transition
= state
;
613 list_for_each_entry(dev
, &dpm_suspended_list
, power
.entry
) {
614 INIT_COMPLETION(dev
->power
.completion
);
617 async_schedule(async_resume
, dev
);
621 while (!list_empty(&dpm_suspended_list
)) {
622 dev
= to_device(dpm_suspended_list
.next
);
624 if (!is_async(dev
)) {
627 mutex_unlock(&dpm_list_mtx
);
629 error
= device_resume(dev
, state
, false);
631 pm_dev_err(dev
, state
, "", error
);
633 mutex_lock(&dpm_list_mtx
);
635 if (!list_empty(&dev
->power
.entry
))
636 list_move_tail(&dev
->power
.entry
, &dpm_prepared_list
);
639 mutex_unlock(&dpm_list_mtx
);
640 async_synchronize_full();
641 dpm_show_time(starttime
, state
, NULL
);
645 * device_complete - Complete a PM transition for given device.
646 * @dev: Device to handle.
647 * @state: PM transition of the system being carried out.
649 static void device_complete(struct device
*dev
, pm_message_t state
)
653 if (dev
->pm_domain
) {
654 pm_dev_dbg(dev
, state
, "completing power domain ");
655 if (dev
->pm_domain
->ops
.complete
)
656 dev
->pm_domain
->ops
.complete(dev
);
657 } else if (dev
->type
&& dev
->type
->pm
) {
658 pm_dev_dbg(dev
, state
, "completing type ");
659 if (dev
->type
->pm
->complete
)
660 dev
->type
->pm
->complete(dev
);
661 } else if (dev
->class && dev
->class->pm
) {
662 pm_dev_dbg(dev
, state
, "completing class ");
663 if (dev
->class->pm
->complete
)
664 dev
->class->pm
->complete(dev
);
665 } else if (dev
->bus
&& dev
->bus
->pm
) {
666 pm_dev_dbg(dev
, state
, "completing ");
667 if (dev
->bus
->pm
->complete
)
668 dev
->bus
->pm
->complete(dev
);
675 * dpm_complete - Complete a PM transition for all non-sysdev devices.
676 * @state: PM transition of the system being carried out.
678 * Execute the ->complete() callbacks for all devices whose PM status is not
679 * DPM_ON (this allows new devices to be registered).
681 void dpm_complete(pm_message_t state
)
683 struct list_head list
;
687 INIT_LIST_HEAD(&list
);
688 mutex_lock(&dpm_list_mtx
);
689 while (!list_empty(&dpm_prepared_list
)) {
690 struct device
*dev
= to_device(dpm_prepared_list
.prev
);
693 dev
->power
.is_prepared
= false;
694 list_move(&dev
->power
.entry
, &list
);
695 mutex_unlock(&dpm_list_mtx
);
697 device_complete(dev
, state
);
699 mutex_lock(&dpm_list_mtx
);
702 list_splice(&list
, &dpm_list
);
703 mutex_unlock(&dpm_list_mtx
);
707 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
708 * @state: PM transition of the system being carried out.
710 * Execute "resume" callbacks for all devices and complete the PM transition of
713 void dpm_resume_end(pm_message_t state
)
718 EXPORT_SYMBOL_GPL(dpm_resume_end
);
721 /*------------------------- Suspend routines -------------------------*/
724 * resume_event - Return a "resume" message for given "suspend" sleep state.
725 * @sleep_state: PM message representing a sleep state.
727 * Return a PM message representing the resume event corresponding to given
730 static pm_message_t
resume_event(pm_message_t sleep_state
)
732 switch (sleep_state
.event
) {
733 case PM_EVENT_SUSPEND
:
735 case PM_EVENT_FREEZE
:
736 case PM_EVENT_QUIESCE
:
738 case PM_EVENT_HIBERNATE
:
745 * device_suspend_noirq - Execute a "late suspend" callback for given device.
746 * @dev: Device to handle.
747 * @state: PM transition of the system being carried out.
749 * The driver of @dev will not receive interrupts while this function is being
752 static int device_suspend_noirq(struct device
*dev
, pm_message_t state
)
756 if (dev
->pm_domain
) {
757 pm_dev_dbg(dev
, state
, "LATE power domain ");
758 error
= pm_noirq_op(dev
, &dev
->pm_domain
->ops
, state
);
761 } else if (dev
->type
&& dev
->type
->pm
) {
762 pm_dev_dbg(dev
, state
, "LATE type ");
763 error
= pm_noirq_op(dev
, dev
->type
->pm
, state
);
766 } else if (dev
->class && dev
->class->pm
) {
767 pm_dev_dbg(dev
, state
, "LATE class ");
768 error
= pm_noirq_op(dev
, dev
->class->pm
, state
);
771 } else if (dev
->bus
&& dev
->bus
->pm
) {
772 pm_dev_dbg(dev
, state
, "LATE ");
773 error
= pm_noirq_op(dev
, dev
->bus
->pm
, state
);
782 * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices.
783 * @state: PM transition of the system being carried out.
785 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
786 * handlers for all non-sysdev devices.
788 int dpm_suspend_noirq(pm_message_t state
)
790 ktime_t starttime
= ktime_get();
793 suspend_device_irqs();
794 mutex_lock(&dpm_list_mtx
);
795 while (!list_empty(&dpm_suspended_list
)) {
796 struct device
*dev
= to_device(dpm_suspended_list
.prev
);
799 mutex_unlock(&dpm_list_mtx
);
801 error
= device_suspend_noirq(dev
, state
);
803 mutex_lock(&dpm_list_mtx
);
805 pm_dev_err(dev
, state
, " late", error
);
809 if (!list_empty(&dev
->power
.entry
))
810 list_move(&dev
->power
.entry
, &dpm_noirq_list
);
813 mutex_unlock(&dpm_list_mtx
);
815 dpm_resume_noirq(resume_event(state
));
817 dpm_show_time(starttime
, state
, "late");
820 EXPORT_SYMBOL_GPL(dpm_suspend_noirq
);
823 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
824 * @dev: Device to suspend.
825 * @state: PM transition of the system being carried out.
826 * @cb: Suspend callback to execute.
828 static int legacy_suspend(struct device
*dev
, pm_message_t state
,
829 int (*cb
)(struct device
*dev
, pm_message_t state
))
834 calltime
= initcall_debug_start(dev
);
836 error
= cb(dev
, state
);
837 suspend_report_result(cb
, error
);
839 initcall_debug_report(dev
, calltime
, error
);
845 * device_suspend - Execute "suspend" callbacks for given device.
846 * @dev: Device to handle.
847 * @state: PM transition of the system being carried out.
848 * @async: If true, the device is being suspended asynchronously.
850 static int __device_suspend(struct device
*dev
, pm_message_t state
, bool async
)
854 dpm_wait_for_children(dev
, async
);
859 pm_runtime_get_noresume(dev
);
860 if (pm_runtime_barrier(dev
) && device_may_wakeup(dev
))
861 pm_wakeup_event(dev
, 0);
863 if (pm_wakeup_pending()) {
864 pm_runtime_put_sync(dev
);
865 async_error
= -EBUSY
;
871 if (dev
->pm_domain
) {
872 pm_dev_dbg(dev
, state
, "power domain ");
873 error
= pm_op(dev
, &dev
->pm_domain
->ops
, state
);
877 if (dev
->type
&& dev
->type
->pm
) {
878 pm_dev_dbg(dev
, state
, "type ");
879 error
= pm_op(dev
, dev
->type
->pm
, state
);
884 if (dev
->class->pm
) {
885 pm_dev_dbg(dev
, state
, "class ");
886 error
= pm_op(dev
, dev
->class->pm
, state
);
888 } else if (dev
->class->suspend
) {
889 pm_dev_dbg(dev
, state
, "legacy class ");
890 error
= legacy_suspend(dev
, state
, dev
->class->suspend
);
897 pm_dev_dbg(dev
, state
, "");
898 error
= pm_op(dev
, dev
->bus
->pm
, state
);
899 } else if (dev
->bus
->suspend
) {
900 pm_dev_dbg(dev
, state
, "legacy ");
901 error
= legacy_suspend(dev
, state
, dev
->bus
->suspend
);
906 dev
->power
.is_suspended
= !error
;
909 complete_all(&dev
->power
.completion
);
912 pm_runtime_put_sync(dev
);
914 } else if (dev
->power
.is_suspended
) {
915 __pm_runtime_disable(dev
, false);
921 static void async_suspend(void *data
, async_cookie_t cookie
)
923 struct device
*dev
= (struct device
*)data
;
926 error
= __device_suspend(dev
, pm_transition
, true);
928 pm_dev_err(dev
, pm_transition
, " async", error
);
933 static int device_suspend(struct device
*dev
)
935 INIT_COMPLETION(dev
->power
.completion
);
937 if (pm_async_enabled
&& dev
->power
.async_suspend
) {
939 async_schedule(async_suspend
, dev
);
943 return __device_suspend(dev
, pm_transition
, false);
947 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
948 * @state: PM transition of the system being carried out.
950 int dpm_suspend(pm_message_t state
)
952 ktime_t starttime
= ktime_get();
957 mutex_lock(&dpm_list_mtx
);
958 pm_transition
= state
;
960 while (!list_empty(&dpm_prepared_list
)) {
961 struct device
*dev
= to_device(dpm_prepared_list
.prev
);
964 mutex_unlock(&dpm_list_mtx
);
966 error
= device_suspend(dev
);
968 mutex_lock(&dpm_list_mtx
);
970 pm_dev_err(dev
, state
, "", error
);
974 if (!list_empty(&dev
->power
.entry
))
975 list_move(&dev
->power
.entry
, &dpm_suspended_list
);
980 mutex_unlock(&dpm_list_mtx
);
981 async_synchronize_full();
985 dpm_show_time(starttime
, state
, NULL
);
990 * device_prepare - Prepare a device for system power transition.
991 * @dev: Device to handle.
992 * @state: PM transition of the system being carried out.
994 * Execute the ->prepare() callback(s) for given device. No new children of the
995 * device may be registered after this function has returned.
997 static int device_prepare(struct device
*dev
, pm_message_t state
)
1003 if (dev
->pm_domain
) {
1004 pm_dev_dbg(dev
, state
, "preparing power domain ");
1005 if (dev
->pm_domain
->ops
.prepare
)
1006 error
= dev
->pm_domain
->ops
.prepare(dev
);
1007 suspend_report_result(dev
->pm_domain
->ops
.prepare
, error
);
1010 } else if (dev
->type
&& dev
->type
->pm
) {
1011 pm_dev_dbg(dev
, state
, "preparing type ");
1012 if (dev
->type
->pm
->prepare
)
1013 error
= dev
->type
->pm
->prepare(dev
);
1014 suspend_report_result(dev
->type
->pm
->prepare
, error
);
1017 } else if (dev
->class && dev
->class->pm
) {
1018 pm_dev_dbg(dev
, state
, "preparing class ");
1019 if (dev
->class->pm
->prepare
)
1020 error
= dev
->class->pm
->prepare(dev
);
1021 suspend_report_result(dev
->class->pm
->prepare
, error
);
1024 } else if (dev
->bus
&& dev
->bus
->pm
) {
1025 pm_dev_dbg(dev
, state
, "preparing ");
1026 if (dev
->bus
->pm
->prepare
)
1027 error
= dev
->bus
->pm
->prepare(dev
);
1028 suspend_report_result(dev
->bus
->pm
->prepare
, error
);
1038 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1039 * @state: PM transition of the system being carried out.
1041 * Execute the ->prepare() callback(s) for all devices.
1043 int dpm_prepare(pm_message_t state
)
1049 mutex_lock(&dpm_list_mtx
);
1050 while (!list_empty(&dpm_list
)) {
1051 struct device
*dev
= to_device(dpm_list
.next
);
1054 mutex_unlock(&dpm_list_mtx
);
1056 error
= device_prepare(dev
, state
);
1058 mutex_lock(&dpm_list_mtx
);
1060 if (error
== -EAGAIN
) {
1065 printk(KERN_INFO
"PM: Device %s not prepared "
1066 "for power transition: code %d\n",
1067 dev_name(dev
), error
);
1071 dev
->power
.is_prepared
= true;
1072 if (!list_empty(&dev
->power
.entry
))
1073 list_move_tail(&dev
->power
.entry
, &dpm_prepared_list
);
1076 mutex_unlock(&dpm_list_mtx
);
1081 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1082 * @state: PM transition of the system being carried out.
1084 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1085 * callbacks for them.
1087 int dpm_suspend_start(pm_message_t state
)
1091 error
= dpm_prepare(state
);
1093 error
= dpm_suspend(state
);
1096 EXPORT_SYMBOL_GPL(dpm_suspend_start
);
1098 void __suspend_report_result(const char *function
, void *fn
, int ret
)
1101 printk(KERN_ERR
"%s(): %pF returns %d\n", function
, fn
, ret
);
1103 EXPORT_SYMBOL_GPL(__suspend_report_result
);
1106 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1107 * @dev: Device to wait for.
1108 * @subordinate: Device that needs to wait for @dev.
1110 int device_pm_wait_for_dev(struct device
*subordinate
, struct device
*dev
)
1112 dpm_wait(dev
, subordinate
->power
.async_suspend
);
1115 EXPORT_SYMBOL_GPL(device_pm_wait_for_dev
);