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
.in_suspend
= false;
61 init_completion(&dev
->power
.completion
);
62 complete_all(&dev
->power
.completion
);
63 dev
->power
.wakeup
= NULL
;
64 spin_lock_init(&dev
->power
.lock
);
69 * device_pm_lock - Lock the list of active devices used by the PM core.
71 void device_pm_lock(void)
73 mutex_lock(&dpm_list_mtx
);
77 * device_pm_unlock - Unlock the list of active devices used by the PM core.
79 void device_pm_unlock(void)
81 mutex_unlock(&dpm_list_mtx
);
85 * device_pm_add - Add a device to the PM core's list of active devices.
86 * @dev: Device to add to the list.
88 void device_pm_add(struct device
*dev
)
90 pr_debug("PM: Adding info for %s:%s\n",
91 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
92 mutex_lock(&dpm_list_mtx
);
93 if (dev
->parent
&& dev
->parent
->power
.in_suspend
)
94 dev_warn(dev
, "parent %s should not be sleeping\n",
95 dev_name(dev
->parent
));
96 list_add_tail(&dev
->power
.entry
, &dpm_list
);
97 mutex_unlock(&dpm_list_mtx
);
101 * device_pm_remove - Remove a device from the PM core's list of active devices.
102 * @dev: Device to be removed from the list.
104 void device_pm_remove(struct device
*dev
)
106 pr_debug("PM: Removing info for %s:%s\n",
107 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
108 complete_all(&dev
->power
.completion
);
109 mutex_lock(&dpm_list_mtx
);
110 list_del_init(&dev
->power
.entry
);
111 mutex_unlock(&dpm_list_mtx
);
112 device_wakeup_disable(dev
);
113 pm_runtime_remove(dev
);
117 * device_pm_move_before - Move device in the PM core's list of active devices.
118 * @deva: Device to move in dpm_list.
119 * @devb: Device @deva should come before.
121 void device_pm_move_before(struct device
*deva
, struct device
*devb
)
123 pr_debug("PM: Moving %s:%s before %s:%s\n",
124 deva
->bus
? deva
->bus
->name
: "No Bus", dev_name(deva
),
125 devb
->bus
? devb
->bus
->name
: "No Bus", dev_name(devb
));
126 /* Delete deva from dpm_list and reinsert before devb. */
127 list_move_tail(&deva
->power
.entry
, &devb
->power
.entry
);
131 * device_pm_move_after - Move device in the PM core's list of active devices.
132 * @deva: Device to move in dpm_list.
133 * @devb: Device @deva should come after.
135 void device_pm_move_after(struct device
*deva
, struct device
*devb
)
137 pr_debug("PM: Moving %s:%s after %s:%s\n",
138 deva
->bus
? deva
->bus
->name
: "No Bus", dev_name(deva
),
139 devb
->bus
? devb
->bus
->name
: "No Bus", dev_name(devb
));
140 /* Delete deva from dpm_list and reinsert after devb. */
141 list_move(&deva
->power
.entry
, &devb
->power
.entry
);
145 * device_pm_move_last - Move device to end of the PM core's list of devices.
146 * @dev: Device to move in dpm_list.
148 void device_pm_move_last(struct device
*dev
)
150 pr_debug("PM: Moving %s:%s to end of list\n",
151 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
152 list_move_tail(&dev
->power
.entry
, &dpm_list
);
155 static ktime_t
initcall_debug_start(struct device
*dev
)
157 ktime_t calltime
= ktime_set(0, 0);
159 if (initcall_debug
) {
160 pr_info("calling %s+ @ %i\n",
161 dev_name(dev
), task_pid_nr(current
));
162 calltime
= ktime_get();
168 static void initcall_debug_report(struct device
*dev
, ktime_t calltime
,
171 ktime_t delta
, rettime
;
173 if (initcall_debug
) {
174 rettime
= ktime_get();
175 delta
= ktime_sub(rettime
, calltime
);
176 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev
),
177 error
, (unsigned long long)ktime_to_ns(delta
) >> 10);
182 * dpm_wait - Wait for a PM operation to complete.
183 * @dev: Device to wait for.
184 * @async: If unset, wait only if the device's power.async_suspend flag is set.
186 static void dpm_wait(struct device
*dev
, bool async
)
191 if (async
|| (pm_async_enabled
&& dev
->power
.async_suspend
))
192 wait_for_completion(&dev
->power
.completion
);
195 static int dpm_wait_fn(struct device
*dev
, void *async_ptr
)
197 dpm_wait(dev
, *((bool *)async_ptr
));
201 static void dpm_wait_for_children(struct device
*dev
, bool async
)
203 device_for_each_child(dev
, &async
, dpm_wait_fn
);
207 * pm_op - Execute the PM operation appropriate for given PM event.
208 * @dev: Device to handle.
209 * @ops: PM operations to choose from.
210 * @state: PM transition of the system being carried out.
212 static int pm_op(struct device
*dev
,
213 const struct dev_pm_ops
*ops
,
219 calltime
= initcall_debug_start(dev
);
221 switch (state
.event
) {
222 #ifdef CONFIG_SUSPEND
223 case PM_EVENT_SUSPEND
:
225 error
= ops
->suspend(dev
);
226 suspend_report_result(ops
->suspend
, error
);
229 case PM_EVENT_RESUME
:
231 error
= ops
->resume(dev
);
232 suspend_report_result(ops
->resume
, error
);
235 #endif /* CONFIG_SUSPEND */
236 #ifdef CONFIG_HIBERNATION
237 case PM_EVENT_FREEZE
:
238 case PM_EVENT_QUIESCE
:
240 error
= ops
->freeze(dev
);
241 suspend_report_result(ops
->freeze
, error
);
244 case PM_EVENT_HIBERNATE
:
246 error
= ops
->poweroff(dev
);
247 suspend_report_result(ops
->poweroff
, error
);
251 case PM_EVENT_RECOVER
:
253 error
= ops
->thaw(dev
);
254 suspend_report_result(ops
->thaw
, error
);
257 case PM_EVENT_RESTORE
:
259 error
= ops
->restore(dev
);
260 suspend_report_result(ops
->restore
, error
);
263 #endif /* CONFIG_HIBERNATION */
268 initcall_debug_report(dev
, calltime
, error
);
274 * pm_noirq_op - Execute the PM operation appropriate for given PM event.
275 * @dev: Device to handle.
276 * @ops: PM operations to choose from.
277 * @state: PM transition of the system being carried out.
279 * The driver of @dev will not receive interrupts while this function is being
282 static int pm_noirq_op(struct device
*dev
,
283 const struct dev_pm_ops
*ops
,
287 ktime_t calltime
= ktime_set(0, 0), delta
, rettime
;
289 if (initcall_debug
) {
290 pr_info("calling %s+ @ %i, parent: %s\n",
291 dev_name(dev
), task_pid_nr(current
),
292 dev
->parent
? dev_name(dev
->parent
) : "none");
293 calltime
= ktime_get();
296 switch (state
.event
) {
297 #ifdef CONFIG_SUSPEND
298 case PM_EVENT_SUSPEND
:
299 if (ops
->suspend_noirq
) {
300 error
= ops
->suspend_noirq(dev
);
301 suspend_report_result(ops
->suspend_noirq
, error
);
304 case PM_EVENT_RESUME
:
305 if (ops
->resume_noirq
) {
306 error
= ops
->resume_noirq(dev
);
307 suspend_report_result(ops
->resume_noirq
, error
);
310 #endif /* CONFIG_SUSPEND */
311 #ifdef CONFIG_HIBERNATION
312 case PM_EVENT_FREEZE
:
313 case PM_EVENT_QUIESCE
:
314 if (ops
->freeze_noirq
) {
315 error
= ops
->freeze_noirq(dev
);
316 suspend_report_result(ops
->freeze_noirq
, error
);
319 case PM_EVENT_HIBERNATE
:
320 if (ops
->poweroff_noirq
) {
321 error
= ops
->poweroff_noirq(dev
);
322 suspend_report_result(ops
->poweroff_noirq
, error
);
326 case PM_EVENT_RECOVER
:
327 if (ops
->thaw_noirq
) {
328 error
= ops
->thaw_noirq(dev
);
329 suspend_report_result(ops
->thaw_noirq
, error
);
332 case PM_EVENT_RESTORE
:
333 if (ops
->restore_noirq
) {
334 error
= ops
->restore_noirq(dev
);
335 suspend_report_result(ops
->restore_noirq
, error
);
338 #endif /* CONFIG_HIBERNATION */
343 if (initcall_debug
) {
344 rettime
= ktime_get();
345 delta
= ktime_sub(rettime
, calltime
);
346 printk("initcall %s_i+ returned %d after %Ld usecs\n",
347 dev_name(dev
), error
,
348 (unsigned long long)ktime_to_ns(delta
) >> 10);
354 static char *pm_verb(int event
)
357 case PM_EVENT_SUSPEND
:
359 case PM_EVENT_RESUME
:
361 case PM_EVENT_FREEZE
:
363 case PM_EVENT_QUIESCE
:
365 case PM_EVENT_HIBERNATE
:
369 case PM_EVENT_RESTORE
:
371 case PM_EVENT_RECOVER
:
374 return "(unknown PM event)";
378 static void pm_dev_dbg(struct device
*dev
, pm_message_t state
, char *info
)
380 dev_dbg(dev
, "%s%s%s\n", info
, pm_verb(state
.event
),
381 ((state
.event
& PM_EVENT_SLEEP
) && device_may_wakeup(dev
)) ?
382 ", may wakeup" : "");
385 static void pm_dev_err(struct device
*dev
, pm_message_t state
, char *info
,
388 printk(KERN_ERR
"PM: Device %s failed to %s%s: error %d\n",
389 dev_name(dev
), pm_verb(state
.event
), info
, error
);
392 static void dpm_show_time(ktime_t starttime
, pm_message_t state
, char *info
)
398 calltime
= ktime_get();
399 usecs64
= ktime_to_ns(ktime_sub(calltime
, starttime
));
400 do_div(usecs64
, NSEC_PER_USEC
);
404 pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
405 info
?: "", info
? " " : "", pm_verb(state
.event
),
406 usecs
/ USEC_PER_MSEC
, usecs
% USEC_PER_MSEC
);
409 /*------------------------- Resume routines -------------------------*/
412 * device_resume_noirq - Execute an "early resume" callback for given device.
413 * @dev: Device to handle.
414 * @state: PM transition of the system being carried out.
416 * The driver of @dev will not receive interrupts while this function is being
419 static int device_resume_noirq(struct device
*dev
, pm_message_t state
)
426 if (dev
->pwr_domain
) {
427 pm_dev_dbg(dev
, state
, "EARLY power domain ");
428 pm_noirq_op(dev
, &dev
->pwr_domain
->ops
, state
);
431 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
)
512 dpm_wait(dev
->parent
, async
);
515 dev
->power
.in_suspend
= false;
517 if (dev
->pwr_domain
) {
518 pm_dev_dbg(dev
, state
, "power domain ");
519 pm_op(dev
, &dev
->pwr_domain
->ops
, state
);
522 if (dev
->type
&& dev
->type
->pm
) {
523 pm_dev_dbg(dev
, state
, "type ");
524 error
= pm_op(dev
, dev
->type
->pm
, state
);
529 if (dev
->class->pm
) {
530 pm_dev_dbg(dev
, state
, "class ");
531 error
= pm_op(dev
, dev
->class->pm
, state
);
533 } else if (dev
->class->resume
) {
534 pm_dev_dbg(dev
, state
, "legacy class ");
535 error
= legacy_resume(dev
, dev
->class->resume
);
542 pm_dev_dbg(dev
, state
, "");
543 error
= pm_op(dev
, dev
->bus
->pm
, state
);
544 } else if (dev
->bus
->resume
) {
545 pm_dev_dbg(dev
, state
, "legacy ");
546 error
= legacy_resume(dev
, dev
->bus
->resume
);
552 complete_all(&dev
->power
.completion
);
558 static void async_resume(void *data
, async_cookie_t cookie
)
560 struct device
*dev
= (struct device
*)data
;
563 error
= device_resume(dev
, pm_transition
, true);
565 pm_dev_err(dev
, pm_transition
, " async", error
);
569 static bool is_async(struct device
*dev
)
571 return dev
->power
.async_suspend
&& pm_async_enabled
572 && !pm_trace_is_enabled();
576 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
577 * @state: PM transition of the system being carried out.
579 * Execute the appropriate "resume" callback for all devices whose status
580 * indicates that they are suspended.
582 static void dpm_resume(pm_message_t state
)
585 ktime_t starttime
= ktime_get();
587 mutex_lock(&dpm_list_mtx
);
588 pm_transition
= state
;
591 list_for_each_entry(dev
, &dpm_suspended_list
, power
.entry
) {
592 INIT_COMPLETION(dev
->power
.completion
);
595 async_schedule(async_resume
, dev
);
599 while (!list_empty(&dpm_suspended_list
)) {
600 dev
= to_device(dpm_suspended_list
.next
);
602 if (!is_async(dev
)) {
605 mutex_unlock(&dpm_list_mtx
);
607 error
= device_resume(dev
, state
, false);
609 pm_dev_err(dev
, state
, "", error
);
611 mutex_lock(&dpm_list_mtx
);
613 if (!list_empty(&dev
->power
.entry
))
614 list_move_tail(&dev
->power
.entry
, &dpm_prepared_list
);
617 mutex_unlock(&dpm_list_mtx
);
618 async_synchronize_full();
619 dpm_show_time(starttime
, state
, NULL
);
623 * device_complete - Complete a PM transition for given device.
624 * @dev: Device to handle.
625 * @state: PM transition of the system being carried out.
627 static void device_complete(struct device
*dev
, pm_message_t state
)
631 if (dev
->pwr_domain
&& dev
->pwr_domain
->ops
.complete
) {
632 pm_dev_dbg(dev
, state
, "completing power domain ");
633 dev
->pwr_domain
->ops
.complete(dev
);
636 if (dev
->type
&& dev
->type
->pm
) {
637 pm_dev_dbg(dev
, state
, "completing type ");
638 if (dev
->type
->pm
->complete
)
639 dev
->type
->pm
->complete(dev
);
640 } else if (dev
->class && dev
->class->pm
) {
641 pm_dev_dbg(dev
, state
, "completing class ");
642 if (dev
->class->pm
->complete
)
643 dev
->class->pm
->complete(dev
);
644 } else if (dev
->bus
&& dev
->bus
->pm
) {
645 pm_dev_dbg(dev
, state
, "completing ");
646 if (dev
->bus
->pm
->complete
)
647 dev
->bus
->pm
->complete(dev
);
654 * dpm_complete - Complete a PM transition for all non-sysdev devices.
655 * @state: PM transition of the system being carried out.
657 * Execute the ->complete() callbacks for all devices whose PM status is not
658 * DPM_ON (this allows new devices to be registered).
660 static void dpm_complete(pm_message_t state
)
662 struct list_head list
;
664 INIT_LIST_HEAD(&list
);
665 mutex_lock(&dpm_list_mtx
);
666 while (!list_empty(&dpm_prepared_list
)) {
667 struct device
*dev
= to_device(dpm_prepared_list
.prev
);
670 dev
->power
.in_suspend
= false;
671 list_move(&dev
->power
.entry
, &list
);
672 mutex_unlock(&dpm_list_mtx
);
674 device_complete(dev
, state
);
676 mutex_lock(&dpm_list_mtx
);
679 list_splice(&list
, &dpm_list
);
680 mutex_unlock(&dpm_list_mtx
);
684 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
685 * @state: PM transition of the system being carried out.
687 * Execute "resume" callbacks for all devices and complete the PM transition of
690 void dpm_resume_end(pm_message_t state
)
696 EXPORT_SYMBOL_GPL(dpm_resume_end
);
699 /*------------------------- Suspend routines -------------------------*/
702 * resume_event - Return a "resume" message for given "suspend" sleep state.
703 * @sleep_state: PM message representing a sleep state.
705 * Return a PM message representing the resume event corresponding to given
708 static pm_message_t
resume_event(pm_message_t sleep_state
)
710 switch (sleep_state
.event
) {
711 case PM_EVENT_SUSPEND
:
713 case PM_EVENT_FREEZE
:
714 case PM_EVENT_QUIESCE
:
716 case PM_EVENT_HIBERNATE
:
723 * device_suspend_noirq - Execute a "late suspend" callback for given device.
724 * @dev: Device to handle.
725 * @state: PM transition of the system being carried out.
727 * The driver of @dev will not receive interrupts while this function is being
730 static int device_suspend_noirq(struct device
*dev
, pm_message_t state
)
734 if (dev
->type
&& dev
->type
->pm
) {
735 pm_dev_dbg(dev
, state
, "LATE type ");
736 error
= pm_noirq_op(dev
, dev
->type
->pm
, state
);
739 } else if (dev
->class && dev
->class->pm
) {
740 pm_dev_dbg(dev
, state
, "LATE class ");
741 error
= pm_noirq_op(dev
, dev
->class->pm
, state
);
744 } else if (dev
->bus
&& dev
->bus
->pm
) {
745 pm_dev_dbg(dev
, state
, "LATE ");
746 error
= pm_noirq_op(dev
, dev
->bus
->pm
, state
);
751 if (dev
->pwr_domain
) {
752 pm_dev_dbg(dev
, state
, "LATE power domain ");
753 pm_noirq_op(dev
, &dev
->pwr_domain
->ops
, state
);
760 * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices.
761 * @state: PM transition of the system being carried out.
763 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
764 * handlers for all non-sysdev devices.
766 int dpm_suspend_noirq(pm_message_t state
)
768 ktime_t starttime
= ktime_get();
771 suspend_device_irqs();
772 mutex_lock(&dpm_list_mtx
);
773 while (!list_empty(&dpm_suspended_list
)) {
774 struct device
*dev
= to_device(dpm_suspended_list
.prev
);
777 mutex_unlock(&dpm_list_mtx
);
779 error
= device_suspend_noirq(dev
, state
);
781 mutex_lock(&dpm_list_mtx
);
783 pm_dev_err(dev
, state
, " late", error
);
787 if (!list_empty(&dev
->power
.entry
))
788 list_move(&dev
->power
.entry
, &dpm_noirq_list
);
791 mutex_unlock(&dpm_list_mtx
);
793 dpm_resume_noirq(resume_event(state
));
795 dpm_show_time(starttime
, state
, "late");
798 EXPORT_SYMBOL_GPL(dpm_suspend_noirq
);
801 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
802 * @dev: Device to suspend.
803 * @state: PM transition of the system being carried out.
804 * @cb: Suspend callback to execute.
806 static int legacy_suspend(struct device
*dev
, pm_message_t state
,
807 int (*cb
)(struct device
*dev
, pm_message_t state
))
812 calltime
= initcall_debug_start(dev
);
814 error
= cb(dev
, state
);
815 suspend_report_result(cb
, error
);
817 initcall_debug_report(dev
, calltime
, error
);
823 * device_suspend - Execute "suspend" callbacks for given device.
824 * @dev: Device to handle.
825 * @state: PM transition of the system being carried out.
826 * @async: If true, the device is being suspended asynchronously.
828 static int __device_suspend(struct device
*dev
, pm_message_t state
, bool async
)
832 dpm_wait_for_children(dev
, async
);
838 if (pm_wakeup_pending()) {
839 async_error
= -EBUSY
;
843 if (dev
->type
&& dev
->type
->pm
) {
844 pm_dev_dbg(dev
, state
, "type ");
845 error
= pm_op(dev
, dev
->type
->pm
, state
);
850 if (dev
->class->pm
) {
851 pm_dev_dbg(dev
, state
, "class ");
852 error
= pm_op(dev
, dev
->class->pm
, state
);
854 } else if (dev
->class->suspend
) {
855 pm_dev_dbg(dev
, state
, "legacy class ");
856 error
= legacy_suspend(dev
, state
, dev
->class->suspend
);
863 pm_dev_dbg(dev
, state
, "");
864 error
= pm_op(dev
, dev
->bus
->pm
, state
);
865 } else if (dev
->bus
->suspend
) {
866 pm_dev_dbg(dev
, state
, "legacy ");
867 error
= legacy_suspend(dev
, state
, dev
->bus
->suspend
);
872 if (!error
&& dev
->pwr_domain
) {
873 pm_dev_dbg(dev
, state
, "power domain ");
874 pm_op(dev
, &dev
->pwr_domain
->ops
, state
);
879 complete_all(&dev
->power
.completion
);
887 static void async_suspend(void *data
, async_cookie_t cookie
)
889 struct device
*dev
= (struct device
*)data
;
892 error
= __device_suspend(dev
, pm_transition
, true);
894 pm_dev_err(dev
, pm_transition
, " async", error
);
899 static int device_suspend(struct device
*dev
)
901 INIT_COMPLETION(dev
->power
.completion
);
903 if (pm_async_enabled
&& dev
->power
.async_suspend
) {
905 async_schedule(async_suspend
, dev
);
909 return __device_suspend(dev
, pm_transition
, false);
913 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
914 * @state: PM transition of the system being carried out.
916 static int dpm_suspend(pm_message_t state
)
918 ktime_t starttime
= ktime_get();
921 mutex_lock(&dpm_list_mtx
);
922 pm_transition
= state
;
924 while (!list_empty(&dpm_prepared_list
)) {
925 struct device
*dev
= to_device(dpm_prepared_list
.prev
);
928 mutex_unlock(&dpm_list_mtx
);
930 error
= device_suspend(dev
);
932 mutex_lock(&dpm_list_mtx
);
934 pm_dev_err(dev
, state
, "", error
);
938 if (!list_empty(&dev
->power
.entry
))
939 list_move(&dev
->power
.entry
, &dpm_suspended_list
);
944 mutex_unlock(&dpm_list_mtx
);
945 async_synchronize_full();
949 dpm_show_time(starttime
, state
, NULL
);
954 * device_prepare - Prepare a device for system power transition.
955 * @dev: Device to handle.
956 * @state: PM transition of the system being carried out.
958 * Execute the ->prepare() callback(s) for given device. No new children of the
959 * device may be registered after this function has returned.
961 static int device_prepare(struct device
*dev
, pm_message_t state
)
967 if (dev
->type
&& dev
->type
->pm
) {
968 pm_dev_dbg(dev
, state
, "preparing type ");
969 if (dev
->type
->pm
->prepare
)
970 error
= dev
->type
->pm
->prepare(dev
);
971 suspend_report_result(dev
->type
->pm
->prepare
, error
);
974 } else if (dev
->class && dev
->class->pm
) {
975 pm_dev_dbg(dev
, state
, "preparing class ");
976 if (dev
->class->pm
->prepare
)
977 error
= dev
->class->pm
->prepare(dev
);
978 suspend_report_result(dev
->class->pm
->prepare
, error
);
981 } else if (dev
->bus
&& dev
->bus
->pm
) {
982 pm_dev_dbg(dev
, state
, "preparing ");
983 if (dev
->bus
->pm
->prepare
)
984 error
= dev
->bus
->pm
->prepare(dev
);
985 suspend_report_result(dev
->bus
->pm
->prepare
, error
);
990 if (dev
->pwr_domain
&& dev
->pwr_domain
->ops
.prepare
) {
991 pm_dev_dbg(dev
, state
, "preparing power domain ");
992 dev
->pwr_domain
->ops
.prepare(dev
);
1002 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1003 * @state: PM transition of the system being carried out.
1005 * Execute the ->prepare() callback(s) for all devices.
1007 static int dpm_prepare(pm_message_t state
)
1011 mutex_lock(&dpm_list_mtx
);
1012 while (!list_empty(&dpm_list
)) {
1013 struct device
*dev
= to_device(dpm_list
.next
);
1016 mutex_unlock(&dpm_list_mtx
);
1018 pm_runtime_get_noresume(dev
);
1019 if (pm_runtime_barrier(dev
) && device_may_wakeup(dev
))
1020 pm_wakeup_event(dev
, 0);
1022 pm_runtime_put_sync(dev
);
1023 error
= pm_wakeup_pending() ?
1024 -EBUSY
: device_prepare(dev
, state
);
1026 mutex_lock(&dpm_list_mtx
);
1028 if (error
== -EAGAIN
) {
1033 printk(KERN_INFO
"PM: Device %s not prepared "
1034 "for power transition: code %d\n",
1035 dev_name(dev
), error
);
1039 dev
->power
.in_suspend
= true;
1040 if (!list_empty(&dev
->power
.entry
))
1041 list_move_tail(&dev
->power
.entry
, &dpm_prepared_list
);
1044 mutex_unlock(&dpm_list_mtx
);
1049 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1050 * @state: PM transition of the system being carried out.
1052 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1053 * callbacks for them.
1055 int dpm_suspend_start(pm_message_t state
)
1060 error
= dpm_prepare(state
);
1062 error
= dpm_suspend(state
);
1065 EXPORT_SYMBOL_GPL(dpm_suspend_start
);
1067 void __suspend_report_result(const char *function
, void *fn
, int ret
)
1070 printk(KERN_ERR
"%s(): %pF returns %d\n", function
, fn
, ret
);
1072 EXPORT_SYMBOL_GPL(__suspend_report_result
);
1075 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1076 * @dev: Device to wait for.
1077 * @subordinate: Device that needs to wait for @dev.
1079 int device_pm_wait_for_dev(struct device
*subordinate
, struct device
*dev
)
1081 dpm_wait(dev
, subordinate
->power
.async_suspend
);
1084 EXPORT_SYMBOL_GPL(device_pm_wait_for_dev
);