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 static DEFINE_MUTEX(dpm_list_mtx
);
50 static pm_message_t pm_transition
;
52 static int async_error
;
55 * device_pm_init - Initialize the PM-related part of a device object.
56 * @dev: Device object being initialized.
58 void device_pm_init(struct device
*dev
)
60 dev
->power
.is_prepared
= false;
61 dev
->power
.is_suspended
= false;
62 init_completion(&dev
->power
.completion
);
63 complete_all(&dev
->power
.completion
);
64 dev
->power
.wakeup
= NULL
;
65 spin_lock_init(&dev
->power
.lock
);
67 INIT_LIST_HEAD(&dev
->power
.entry
);
71 * device_pm_lock - Lock the list of active devices used by the PM core.
73 void device_pm_lock(void)
75 mutex_lock(&dpm_list_mtx
);
79 * device_pm_unlock - Unlock the list of active devices used by the PM core.
81 void device_pm_unlock(void)
83 mutex_unlock(&dpm_list_mtx
);
87 * device_pm_add - Add a device to the PM core's list of active devices.
88 * @dev: Device to add to the list.
90 void device_pm_add(struct device
*dev
)
92 pr_debug("PM: Adding info for %s:%s\n",
93 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
94 mutex_lock(&dpm_list_mtx
);
95 if (dev
->parent
&& dev
->parent
->power
.is_prepared
)
96 dev_warn(dev
, "parent %s should not be sleeping\n",
97 dev_name(dev
->parent
));
98 list_add_tail(&dev
->power
.entry
, &dpm_list
);
99 mutex_unlock(&dpm_list_mtx
);
103 * device_pm_remove - Remove a device from the PM core's list of active devices.
104 * @dev: Device to be removed from the list.
106 void device_pm_remove(struct device
*dev
)
108 pr_debug("PM: Removing info for %s:%s\n",
109 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
110 complete_all(&dev
->power
.completion
);
111 mutex_lock(&dpm_list_mtx
);
112 list_del_init(&dev
->power
.entry
);
113 mutex_unlock(&dpm_list_mtx
);
114 device_wakeup_disable(dev
);
115 pm_runtime_remove(dev
);
119 * device_pm_move_before - Move device in the PM core's list of active devices.
120 * @deva: Device to move in dpm_list.
121 * @devb: Device @deva should come before.
123 void device_pm_move_before(struct device
*deva
, struct device
*devb
)
125 pr_debug("PM: Moving %s:%s before %s:%s\n",
126 deva
->bus
? deva
->bus
->name
: "No Bus", dev_name(deva
),
127 devb
->bus
? devb
->bus
->name
: "No Bus", dev_name(devb
));
128 /* Delete deva from dpm_list and reinsert before devb. */
129 list_move_tail(&deva
->power
.entry
, &devb
->power
.entry
);
133 * device_pm_move_after - Move device in the PM core's list of active devices.
134 * @deva: Device to move in dpm_list.
135 * @devb: Device @deva should come after.
137 void device_pm_move_after(struct device
*deva
, struct device
*devb
)
139 pr_debug("PM: Moving %s:%s after %s:%s\n",
140 deva
->bus
? deva
->bus
->name
: "No Bus", dev_name(deva
),
141 devb
->bus
? devb
->bus
->name
: "No Bus", dev_name(devb
));
142 /* Delete deva from dpm_list and reinsert after devb. */
143 list_move(&deva
->power
.entry
, &devb
->power
.entry
);
147 * device_pm_move_last - Move device to end of the PM core's list of devices.
148 * @dev: Device to move in dpm_list.
150 void device_pm_move_last(struct device
*dev
)
152 pr_debug("PM: Moving %s:%s to end of list\n",
153 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
154 list_move_tail(&dev
->power
.entry
, &dpm_list
);
157 static ktime_t
initcall_debug_start(struct device
*dev
)
159 ktime_t calltime
= ktime_set(0, 0);
161 if (initcall_debug
) {
162 pr_info("calling %s+ @ %i\n",
163 dev_name(dev
), task_pid_nr(current
));
164 calltime
= ktime_get();
170 static void initcall_debug_report(struct device
*dev
, ktime_t calltime
,
173 ktime_t delta
, rettime
;
175 if (initcall_debug
) {
176 rettime
= ktime_get();
177 delta
= ktime_sub(rettime
, calltime
);
178 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev
),
179 error
, (unsigned long long)ktime_to_ns(delta
) >> 10);
184 * dpm_wait - Wait for a PM operation to complete.
185 * @dev: Device to wait for.
186 * @async: If unset, wait only if the device's power.async_suspend flag is set.
188 static void dpm_wait(struct device
*dev
, bool async
)
193 if (async
|| (pm_async_enabled
&& dev
->power
.async_suspend
))
194 wait_for_completion(&dev
->power
.completion
);
197 static int dpm_wait_fn(struct device
*dev
, void *async_ptr
)
199 dpm_wait(dev
, *((bool *)async_ptr
));
203 static void dpm_wait_for_children(struct device
*dev
, bool async
)
205 device_for_each_child(dev
, &async
, dpm_wait_fn
);
209 * pm_op - Execute the PM operation appropriate for given PM event.
210 * @dev: Device to handle.
211 * @ops: PM operations to choose from.
212 * @state: PM transition of the system being carried out.
214 static int pm_op(struct device
*dev
,
215 const struct dev_pm_ops
*ops
,
221 calltime
= initcall_debug_start(dev
);
223 switch (state
.event
) {
224 #ifdef CONFIG_SUSPEND
225 case PM_EVENT_SUSPEND
:
227 error
= ops
->suspend(dev
);
228 suspend_report_result(ops
->suspend
, error
);
231 case PM_EVENT_RESUME
:
233 error
= ops
->resume(dev
);
234 suspend_report_result(ops
->resume
, error
);
237 #endif /* CONFIG_SUSPEND */
238 #ifdef CONFIG_HIBERNATE_CALLBACKS
239 case PM_EVENT_FREEZE
:
240 case PM_EVENT_QUIESCE
:
242 error
= ops
->freeze(dev
);
243 suspend_report_result(ops
->freeze
, error
);
246 case PM_EVENT_HIBERNATE
:
248 error
= ops
->poweroff(dev
);
249 suspend_report_result(ops
->poweroff
, error
);
253 case PM_EVENT_RECOVER
:
255 error
= ops
->thaw(dev
);
256 suspend_report_result(ops
->thaw
, error
);
259 case PM_EVENT_RESTORE
:
261 error
= ops
->restore(dev
);
262 suspend_report_result(ops
->restore
, error
);
265 #endif /* CONFIG_HIBERNATE_CALLBACKS */
270 initcall_debug_report(dev
, calltime
, error
);
276 * pm_noirq_op - Execute the PM operation appropriate for given PM event.
277 * @dev: Device to handle.
278 * @ops: PM operations to choose from.
279 * @state: PM transition of the system being carried out.
281 * The driver of @dev will not receive interrupts while this function is being
284 static int pm_noirq_op(struct device
*dev
,
285 const struct dev_pm_ops
*ops
,
289 ktime_t calltime
= ktime_set(0, 0), delta
, rettime
;
291 if (initcall_debug
) {
292 pr_info("calling %s+ @ %i, parent: %s\n",
293 dev_name(dev
), task_pid_nr(current
),
294 dev
->parent
? dev_name(dev
->parent
) : "none");
295 calltime
= ktime_get();
298 switch (state
.event
) {
299 #ifdef CONFIG_SUSPEND
300 case PM_EVENT_SUSPEND
:
301 if (ops
->suspend_noirq
) {
302 error
= ops
->suspend_noirq(dev
);
303 suspend_report_result(ops
->suspend_noirq
, error
);
306 case PM_EVENT_RESUME
:
307 if (ops
->resume_noirq
) {
308 error
= ops
->resume_noirq(dev
);
309 suspend_report_result(ops
->resume_noirq
, error
);
312 #endif /* CONFIG_SUSPEND */
313 #ifdef CONFIG_HIBERNATE_CALLBACKS
314 case PM_EVENT_FREEZE
:
315 case PM_EVENT_QUIESCE
:
316 if (ops
->freeze_noirq
) {
317 error
= ops
->freeze_noirq(dev
);
318 suspend_report_result(ops
->freeze_noirq
, error
);
321 case PM_EVENT_HIBERNATE
:
322 if (ops
->poweroff_noirq
) {
323 error
= ops
->poweroff_noirq(dev
);
324 suspend_report_result(ops
->poweroff_noirq
, error
);
328 case PM_EVENT_RECOVER
:
329 if (ops
->thaw_noirq
) {
330 error
= ops
->thaw_noirq(dev
);
331 suspend_report_result(ops
->thaw_noirq
, error
);
334 case PM_EVENT_RESTORE
:
335 if (ops
->restore_noirq
) {
336 error
= ops
->restore_noirq(dev
);
337 suspend_report_result(ops
->restore_noirq
, error
);
340 #endif /* CONFIG_HIBERNATE_CALLBACKS */
345 if (initcall_debug
) {
346 rettime
= ktime_get();
347 delta
= ktime_sub(rettime
, calltime
);
348 printk("initcall %s_i+ returned %d after %Ld usecs\n",
349 dev_name(dev
), error
,
350 (unsigned long long)ktime_to_ns(delta
) >> 10);
356 static char *pm_verb(int event
)
359 case PM_EVENT_SUSPEND
:
361 case PM_EVENT_RESUME
:
363 case PM_EVENT_FREEZE
:
365 case PM_EVENT_QUIESCE
:
367 case PM_EVENT_HIBERNATE
:
371 case PM_EVENT_RESTORE
:
373 case PM_EVENT_RECOVER
:
376 return "(unknown PM event)";
380 static void pm_dev_dbg(struct device
*dev
, pm_message_t state
, char *info
)
382 dev_dbg(dev
, "%s%s%s\n", info
, pm_verb(state
.event
),
383 ((state
.event
& PM_EVENT_SLEEP
) && device_may_wakeup(dev
)) ?
384 ", may wakeup" : "");
387 static void pm_dev_err(struct device
*dev
, pm_message_t state
, char *info
,
390 printk(KERN_ERR
"PM: Device %s failed to %s%s: error %d\n",
391 dev_name(dev
), pm_verb(state
.event
), info
, error
);
394 static void dpm_show_time(ktime_t starttime
, pm_message_t state
, char *info
)
400 calltime
= ktime_get();
401 usecs64
= ktime_to_ns(ktime_sub(calltime
, starttime
));
402 do_div(usecs64
, NSEC_PER_USEC
);
406 pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
407 info
?: "", info
? " " : "", pm_verb(state
.event
),
408 usecs
/ USEC_PER_MSEC
, usecs
% USEC_PER_MSEC
);
411 /*------------------------- Resume routines -------------------------*/
414 * device_resume_noirq - Execute an "early resume" callback for given device.
415 * @dev: Device to handle.
416 * @state: PM transition of the system being carried out.
418 * The driver of @dev will not receive interrupts while this function is being
421 static int device_resume_noirq(struct device
*dev
, pm_message_t state
)
428 if (dev
->pm_domain
) {
429 pm_dev_dbg(dev
, state
, "EARLY power domain ");
430 error
= pm_noirq_op(dev
, &dev
->pm_domain
->ops
, state
);
431 } else if (dev
->type
&& dev
->type
->pm
) {
432 pm_dev_dbg(dev
, state
, "EARLY type ");
433 error
= pm_noirq_op(dev
, dev
->type
->pm
, state
);
434 } else if (dev
->class && dev
->class->pm
) {
435 pm_dev_dbg(dev
, state
, "EARLY class ");
436 error
= pm_noirq_op(dev
, dev
->class->pm
, state
);
437 } else if (dev
->bus
&& dev
->bus
->pm
) {
438 pm_dev_dbg(dev
, state
, "EARLY ");
439 error
= pm_noirq_op(dev
, dev
->bus
->pm
, state
);
447 * dpm_resume_noirq - Execute "early resume" callbacks for non-sysdev devices.
448 * @state: PM transition of the system being carried out.
450 * Call the "noirq" resume handlers for all devices marked as DPM_OFF_IRQ and
451 * enable device drivers to receive interrupts.
453 void dpm_resume_noirq(pm_message_t state
)
455 ktime_t starttime
= ktime_get();
457 mutex_lock(&dpm_list_mtx
);
458 while (!list_empty(&dpm_noirq_list
)) {
459 struct device
*dev
= to_device(dpm_noirq_list
.next
);
463 list_move_tail(&dev
->power
.entry
, &dpm_suspended_list
);
464 mutex_unlock(&dpm_list_mtx
);
466 error
= device_resume_noirq(dev
, state
);
468 pm_dev_err(dev
, state
, " early", error
);
470 mutex_lock(&dpm_list_mtx
);
473 mutex_unlock(&dpm_list_mtx
);
474 dpm_show_time(starttime
, state
, "early");
475 resume_device_irqs();
477 EXPORT_SYMBOL_GPL(dpm_resume_noirq
);
480 * legacy_resume - Execute a legacy (bus or class) resume callback for device.
481 * @dev: Device to resume.
482 * @cb: Resume callback to execute.
484 static int legacy_resume(struct device
*dev
, int (*cb
)(struct device
*dev
))
489 calltime
= initcall_debug_start(dev
);
492 suspend_report_result(cb
, error
);
494 initcall_debug_report(dev
, calltime
, error
);
500 * device_resume - Execute "resume" callbacks for given device.
501 * @dev: Device to handle.
502 * @state: PM transition of the system being carried out.
503 * @async: If true, the device is being resumed asynchronously.
505 static int device_resume(struct device
*dev
, pm_message_t state
, bool async
)
513 dpm_wait(dev
->parent
, async
);
517 * This is a fib. But we'll allow new children to be added below
518 * a resumed device, even if the device hasn't been completed yet.
520 dev
->power
.is_prepared
= false;
522 if (!dev
->power
.is_suspended
)
525 pm_runtime_enable(dev
);
528 if (dev
->pm_domain
) {
529 pm_dev_dbg(dev
, state
, "power domain ");
530 error
= pm_op(dev
, &dev
->pm_domain
->ops
, state
);
534 if (dev
->type
&& dev
->type
->pm
) {
535 pm_dev_dbg(dev
, state
, "type ");
536 error
= pm_op(dev
, dev
->type
->pm
, state
);
541 if (dev
->class->pm
) {
542 pm_dev_dbg(dev
, state
, "class ");
543 error
= pm_op(dev
, dev
->class->pm
, state
);
545 } else if (dev
->class->resume
) {
546 pm_dev_dbg(dev
, state
, "legacy class ");
547 error
= legacy_resume(dev
, dev
->class->resume
);
554 pm_dev_dbg(dev
, state
, "");
555 error
= pm_op(dev
, dev
->bus
->pm
, state
);
556 } else if (dev
->bus
->resume
) {
557 pm_dev_dbg(dev
, state
, "legacy ");
558 error
= legacy_resume(dev
, dev
->bus
->resume
);
563 dev
->power
.is_suspended
= false;
567 complete_all(&dev
->power
.completion
);
572 pm_runtime_put_sync(dev
);
577 static void async_resume(void *data
, async_cookie_t cookie
)
579 struct device
*dev
= (struct device
*)data
;
582 error
= device_resume(dev
, pm_transition
, true);
584 pm_dev_err(dev
, pm_transition
, " async", error
);
588 static bool is_async(struct device
*dev
)
590 return dev
->power
.async_suspend
&& pm_async_enabled
591 && !pm_trace_is_enabled();
595 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
596 * @state: PM transition of the system being carried out.
598 * Execute the appropriate "resume" callback for all devices whose status
599 * indicates that they are suspended.
601 void dpm_resume(pm_message_t state
)
604 ktime_t starttime
= ktime_get();
608 mutex_lock(&dpm_list_mtx
);
609 pm_transition
= state
;
612 list_for_each_entry(dev
, &dpm_suspended_list
, power
.entry
) {
613 INIT_COMPLETION(dev
->power
.completion
);
616 async_schedule(async_resume
, dev
);
620 while (!list_empty(&dpm_suspended_list
)) {
621 dev
= to_device(dpm_suspended_list
.next
);
623 if (!is_async(dev
)) {
626 mutex_unlock(&dpm_list_mtx
);
628 error
= device_resume(dev
, state
, false);
630 pm_dev_err(dev
, state
, "", error
);
632 mutex_lock(&dpm_list_mtx
);
634 if (!list_empty(&dev
->power
.entry
))
635 list_move_tail(&dev
->power
.entry
, &dpm_prepared_list
);
638 mutex_unlock(&dpm_list_mtx
);
639 async_synchronize_full();
640 dpm_show_time(starttime
, state
, NULL
);
644 * device_complete - Complete a PM transition for given device.
645 * @dev: Device to handle.
646 * @state: PM transition of the system being carried out.
648 static void device_complete(struct device
*dev
, pm_message_t state
)
652 if (dev
->pm_domain
) {
653 pm_dev_dbg(dev
, state
, "completing power domain ");
654 if (dev
->pm_domain
->ops
.complete
)
655 dev
->pm_domain
->ops
.complete(dev
);
656 } else if (dev
->type
&& dev
->type
->pm
) {
657 pm_dev_dbg(dev
, state
, "completing type ");
658 if (dev
->type
->pm
->complete
)
659 dev
->type
->pm
->complete(dev
);
660 } else if (dev
->class && dev
->class->pm
) {
661 pm_dev_dbg(dev
, state
, "completing class ");
662 if (dev
->class->pm
->complete
)
663 dev
->class->pm
->complete(dev
);
664 } else if (dev
->bus
&& dev
->bus
->pm
) {
665 pm_dev_dbg(dev
, state
, "completing ");
666 if (dev
->bus
->pm
->complete
)
667 dev
->bus
->pm
->complete(dev
);
674 * dpm_complete - Complete a PM transition for all non-sysdev devices.
675 * @state: PM transition of the system being carried out.
677 * Execute the ->complete() callbacks for all devices whose PM status is not
678 * DPM_ON (this allows new devices to be registered).
680 void dpm_complete(pm_message_t state
)
682 struct list_head list
;
686 INIT_LIST_HEAD(&list
);
687 mutex_lock(&dpm_list_mtx
);
688 while (!list_empty(&dpm_prepared_list
)) {
689 struct device
*dev
= to_device(dpm_prepared_list
.prev
);
692 dev
->power
.is_prepared
= false;
693 list_move(&dev
->power
.entry
, &list
);
694 mutex_unlock(&dpm_list_mtx
);
696 device_complete(dev
, state
);
698 mutex_lock(&dpm_list_mtx
);
701 list_splice(&list
, &dpm_list
);
702 mutex_unlock(&dpm_list_mtx
);
706 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
707 * @state: PM transition of the system being carried out.
709 * Execute "resume" callbacks for all devices and complete the PM transition of
712 void dpm_resume_end(pm_message_t state
)
717 EXPORT_SYMBOL_GPL(dpm_resume_end
);
720 /*------------------------- Suspend routines -------------------------*/
723 * resume_event - Return a "resume" message for given "suspend" sleep state.
724 * @sleep_state: PM message representing a sleep state.
726 * Return a PM message representing the resume event corresponding to given
729 static pm_message_t
resume_event(pm_message_t sleep_state
)
731 switch (sleep_state
.event
) {
732 case PM_EVENT_SUSPEND
:
734 case PM_EVENT_FREEZE
:
735 case PM_EVENT_QUIESCE
:
737 case PM_EVENT_HIBERNATE
:
744 * device_suspend_noirq - Execute a "late suspend" callback for given device.
745 * @dev: Device to handle.
746 * @state: PM transition of the system being carried out.
748 * The driver of @dev will not receive interrupts while this function is being
751 static int device_suspend_noirq(struct device
*dev
, pm_message_t state
)
755 if (dev
->pm_domain
) {
756 pm_dev_dbg(dev
, state
, "LATE power domain ");
757 error
= pm_noirq_op(dev
, &dev
->pm_domain
->ops
, state
);
760 } else if (dev
->type
&& dev
->type
->pm
) {
761 pm_dev_dbg(dev
, state
, "LATE type ");
762 error
= pm_noirq_op(dev
, dev
->type
->pm
, state
);
765 } else if (dev
->class && dev
->class->pm
) {
766 pm_dev_dbg(dev
, state
, "LATE class ");
767 error
= pm_noirq_op(dev
, dev
->class->pm
, state
);
770 } else if (dev
->bus
&& dev
->bus
->pm
) {
771 pm_dev_dbg(dev
, state
, "LATE ");
772 error
= pm_noirq_op(dev
, dev
->bus
->pm
, state
);
781 * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices.
782 * @state: PM transition of the system being carried out.
784 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
785 * handlers for all non-sysdev devices.
787 int dpm_suspend_noirq(pm_message_t state
)
789 ktime_t starttime
= ktime_get();
792 suspend_device_irqs();
793 mutex_lock(&dpm_list_mtx
);
794 while (!list_empty(&dpm_suspended_list
)) {
795 struct device
*dev
= to_device(dpm_suspended_list
.prev
);
798 mutex_unlock(&dpm_list_mtx
);
800 error
= device_suspend_noirq(dev
, state
);
802 mutex_lock(&dpm_list_mtx
);
804 pm_dev_err(dev
, state
, " late", error
);
808 if (!list_empty(&dev
->power
.entry
))
809 list_move(&dev
->power
.entry
, &dpm_noirq_list
);
812 mutex_unlock(&dpm_list_mtx
);
814 dpm_resume_noirq(resume_event(state
));
816 dpm_show_time(starttime
, state
, "late");
819 EXPORT_SYMBOL_GPL(dpm_suspend_noirq
);
822 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
823 * @dev: Device to suspend.
824 * @state: PM transition of the system being carried out.
825 * @cb: Suspend callback to execute.
827 static int legacy_suspend(struct device
*dev
, pm_message_t state
,
828 int (*cb
)(struct device
*dev
, pm_message_t state
))
833 calltime
= initcall_debug_start(dev
);
835 error
= cb(dev
, state
);
836 suspend_report_result(cb
, error
);
838 initcall_debug_report(dev
, calltime
, error
);
844 * device_suspend - Execute "suspend" callbacks for given device.
845 * @dev: Device to handle.
846 * @state: PM transition of the system being carried out.
847 * @async: If true, the device is being suspended asynchronously.
849 static int __device_suspend(struct device
*dev
, pm_message_t state
, bool async
)
853 dpm_wait_for_children(dev
, async
);
858 pm_runtime_get_noresume(dev
);
859 if (pm_runtime_barrier(dev
) && device_may_wakeup(dev
))
860 pm_wakeup_event(dev
, 0);
862 if (pm_wakeup_pending()) {
863 pm_runtime_put_sync(dev
);
864 async_error
= -EBUSY
;
870 if (dev
->pm_domain
) {
871 pm_dev_dbg(dev
, state
, "power domain ");
872 error
= pm_op(dev
, &dev
->pm_domain
->ops
, state
);
876 if (dev
->type
&& dev
->type
->pm
) {
877 pm_dev_dbg(dev
, state
, "type ");
878 error
= pm_op(dev
, dev
->type
->pm
, state
);
883 if (dev
->class->pm
) {
884 pm_dev_dbg(dev
, state
, "class ");
885 error
= pm_op(dev
, dev
->class->pm
, state
);
887 } else if (dev
->class->suspend
) {
888 pm_dev_dbg(dev
, state
, "legacy class ");
889 error
= legacy_suspend(dev
, state
, dev
->class->suspend
);
896 pm_dev_dbg(dev
, state
, "");
897 error
= pm_op(dev
, dev
->bus
->pm
, state
);
898 } else if (dev
->bus
->suspend
) {
899 pm_dev_dbg(dev
, state
, "legacy ");
900 error
= legacy_suspend(dev
, state
, dev
->bus
->suspend
);
905 dev
->power
.is_suspended
= !error
;
908 complete_all(&dev
->power
.completion
);
911 pm_runtime_put_sync(dev
);
913 } else if (dev
->power
.is_suspended
) {
914 __pm_runtime_disable(dev
, false);
920 static void async_suspend(void *data
, async_cookie_t cookie
)
922 struct device
*dev
= (struct device
*)data
;
925 error
= __device_suspend(dev
, pm_transition
, true);
927 pm_dev_err(dev
, pm_transition
, " async", error
);
932 static int device_suspend(struct device
*dev
)
934 INIT_COMPLETION(dev
->power
.completion
);
936 if (pm_async_enabled
&& dev
->power
.async_suspend
) {
938 async_schedule(async_suspend
, dev
);
942 return __device_suspend(dev
, pm_transition
, false);
946 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
947 * @state: PM transition of the system being carried out.
949 int dpm_suspend(pm_message_t state
)
951 ktime_t starttime
= ktime_get();
956 mutex_lock(&dpm_list_mtx
);
957 pm_transition
= state
;
959 while (!list_empty(&dpm_prepared_list
)) {
960 struct device
*dev
= to_device(dpm_prepared_list
.prev
);
963 mutex_unlock(&dpm_list_mtx
);
965 error
= device_suspend(dev
);
967 mutex_lock(&dpm_list_mtx
);
969 pm_dev_err(dev
, state
, "", error
);
973 if (!list_empty(&dev
->power
.entry
))
974 list_move(&dev
->power
.entry
, &dpm_suspended_list
);
979 mutex_unlock(&dpm_list_mtx
);
980 async_synchronize_full();
984 dpm_show_time(starttime
, state
, NULL
);
989 * device_prepare - Prepare a device for system power transition.
990 * @dev: Device to handle.
991 * @state: PM transition of the system being carried out.
993 * Execute the ->prepare() callback(s) for given device. No new children of the
994 * device may be registered after this function has returned.
996 static int device_prepare(struct device
*dev
, pm_message_t state
)
1002 if (dev
->pm_domain
) {
1003 pm_dev_dbg(dev
, state
, "preparing power domain ");
1004 if (dev
->pm_domain
->ops
.prepare
)
1005 error
= dev
->pm_domain
->ops
.prepare(dev
);
1006 suspend_report_result(dev
->pm_domain
->ops
.prepare
, error
);
1009 } else if (dev
->type
&& dev
->type
->pm
) {
1010 pm_dev_dbg(dev
, state
, "preparing type ");
1011 if (dev
->type
->pm
->prepare
)
1012 error
= dev
->type
->pm
->prepare(dev
);
1013 suspend_report_result(dev
->type
->pm
->prepare
, error
);
1016 } else if (dev
->class && dev
->class->pm
) {
1017 pm_dev_dbg(dev
, state
, "preparing class ");
1018 if (dev
->class->pm
->prepare
)
1019 error
= dev
->class->pm
->prepare(dev
);
1020 suspend_report_result(dev
->class->pm
->prepare
, error
);
1023 } else if (dev
->bus
&& dev
->bus
->pm
) {
1024 pm_dev_dbg(dev
, state
, "preparing ");
1025 if (dev
->bus
->pm
->prepare
)
1026 error
= dev
->bus
->pm
->prepare(dev
);
1027 suspend_report_result(dev
->bus
->pm
->prepare
, error
);
1037 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1038 * @state: PM transition of the system being carried out.
1040 * Execute the ->prepare() callback(s) for all devices.
1042 int dpm_prepare(pm_message_t state
)
1048 mutex_lock(&dpm_list_mtx
);
1049 while (!list_empty(&dpm_list
)) {
1050 struct device
*dev
= to_device(dpm_list
.next
);
1053 mutex_unlock(&dpm_list_mtx
);
1055 error
= device_prepare(dev
, state
);
1057 mutex_lock(&dpm_list_mtx
);
1059 if (error
== -EAGAIN
) {
1064 printk(KERN_INFO
"PM: Device %s not prepared "
1065 "for power transition: code %d\n",
1066 dev_name(dev
), error
);
1070 dev
->power
.is_prepared
= true;
1071 if (!list_empty(&dev
->power
.entry
))
1072 list_move_tail(&dev
->power
.entry
, &dpm_prepared_list
);
1075 mutex_unlock(&dpm_list_mtx
);
1080 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1081 * @state: PM transition of the system being carried out.
1083 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1084 * callbacks for them.
1086 int dpm_suspend_start(pm_message_t state
)
1090 error
= dpm_prepare(state
);
1092 error
= dpm_suspend(state
);
1095 EXPORT_SYMBOL_GPL(dpm_suspend_start
);
1097 void __suspend_report_result(const char *function
, void *fn
, int ret
)
1100 printk(KERN_ERR
"%s(): %pF returns %d\n", function
, fn
, ret
);
1102 EXPORT_SYMBOL_GPL(__suspend_report_result
);
1105 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1106 * @dev: Device to wait for.
1107 * @subordinate: Device that needs to wait for @dev.
1109 int device_pm_wait_for_dev(struct device
*subordinate
, struct device
*dev
)
1111 dpm_wait(dev
, subordinate
->power
.async_suspend
);
1114 EXPORT_SYMBOL_GPL(device_pm_wait_for_dev
);