i2c-eg20t: change timeout value 50msec to 1000msec
[zen-stable.git] / drivers / base / power / main.c
blobe2cc3d2e0ecccc1d6e6cb9b8f8f95ac4f81812f5
1 /*
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>
24 #include <linux/pm.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>
32 #include "../base.h"
33 #include "power.h"
35 typedef int (*pm_callback_t)(struct device *);
38 * The entries in the dpm_list list are in a depth first order, simply
39 * because children are guaranteed to be discovered after parents, and
40 * are inserted at the back of the list on discovery.
42 * Since device_pm_add() may be called with a device lock held,
43 * we must never try to acquire a device lock while holding
44 * dpm_list_mutex.
47 LIST_HEAD(dpm_list);
48 LIST_HEAD(dpm_prepared_list);
49 LIST_HEAD(dpm_suspended_list);
50 LIST_HEAD(dpm_noirq_list);
52 struct suspend_stats suspend_stats;
53 static DEFINE_MUTEX(dpm_list_mtx);
54 static pm_message_t pm_transition;
56 static int async_error;
58 /**
59 * device_pm_init - Initialize the PM-related part of a device object.
60 * @dev: Device object being initialized.
62 void device_pm_init(struct device *dev)
64 dev->power.is_prepared = false;
65 dev->power.is_suspended = false;
66 init_completion(&dev->power.completion);
67 complete_all(&dev->power.completion);
68 dev->power.wakeup = NULL;
69 spin_lock_init(&dev->power.lock);
70 pm_runtime_init(dev);
71 INIT_LIST_HEAD(&dev->power.entry);
72 dev->power.power_state = PMSG_INVALID;
75 /**
76 * device_pm_lock - Lock the list of active devices used by the PM core.
78 void device_pm_lock(void)
80 mutex_lock(&dpm_list_mtx);
83 /**
84 * device_pm_unlock - Unlock the list of active devices used by the PM core.
86 void device_pm_unlock(void)
88 mutex_unlock(&dpm_list_mtx);
91 /**
92 * device_pm_add - Add a device to the PM core's list of active devices.
93 * @dev: Device to add to the list.
95 void device_pm_add(struct device *dev)
97 pr_debug("PM: Adding info for %s:%s\n",
98 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
99 mutex_lock(&dpm_list_mtx);
100 if (dev->parent && dev->parent->power.is_prepared)
101 dev_warn(dev, "parent %s should not be sleeping\n",
102 dev_name(dev->parent));
103 list_add_tail(&dev->power.entry, &dpm_list);
104 dev_pm_qos_constraints_init(dev);
105 mutex_unlock(&dpm_list_mtx);
109 * device_pm_remove - Remove a device from the PM core's list of active devices.
110 * @dev: Device to be removed from the list.
112 void device_pm_remove(struct device *dev)
114 pr_debug("PM: Removing info for %s:%s\n",
115 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
116 complete_all(&dev->power.completion);
117 mutex_lock(&dpm_list_mtx);
118 dev_pm_qos_constraints_destroy(dev);
119 list_del_init(&dev->power.entry);
120 mutex_unlock(&dpm_list_mtx);
121 device_wakeup_disable(dev);
122 pm_runtime_remove(dev);
126 * device_pm_move_before - Move device in the PM core's list of active devices.
127 * @deva: Device to move in dpm_list.
128 * @devb: Device @deva should come before.
130 void device_pm_move_before(struct device *deva, struct device *devb)
132 pr_debug("PM: Moving %s:%s before %s:%s\n",
133 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
134 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
135 /* Delete deva from dpm_list and reinsert before devb. */
136 list_move_tail(&deva->power.entry, &devb->power.entry);
140 * device_pm_move_after - Move device in the PM core's list of active devices.
141 * @deva: Device to move in dpm_list.
142 * @devb: Device @deva should come after.
144 void device_pm_move_after(struct device *deva, struct device *devb)
146 pr_debug("PM: Moving %s:%s after %s:%s\n",
147 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
148 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
149 /* Delete deva from dpm_list and reinsert after devb. */
150 list_move(&deva->power.entry, &devb->power.entry);
154 * device_pm_move_last - Move device to end of the PM core's list of devices.
155 * @dev: Device to move in dpm_list.
157 void device_pm_move_last(struct device *dev)
159 pr_debug("PM: Moving %s:%s to end of list\n",
160 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
161 list_move_tail(&dev->power.entry, &dpm_list);
164 static ktime_t initcall_debug_start(struct device *dev)
166 ktime_t calltime = ktime_set(0, 0);
168 if (initcall_debug) {
169 pr_info("calling %s+ @ %i, parent: %s\n",
170 dev_name(dev), task_pid_nr(current),
171 dev->parent ? dev_name(dev->parent) : "none");
172 calltime = ktime_get();
175 return calltime;
178 static void initcall_debug_report(struct device *dev, ktime_t calltime,
179 int error)
181 ktime_t delta, rettime;
183 if (initcall_debug) {
184 rettime = ktime_get();
185 delta = ktime_sub(rettime, calltime);
186 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev),
187 error, (unsigned long long)ktime_to_ns(delta) >> 10);
192 * dpm_wait - Wait for a PM operation to complete.
193 * @dev: Device to wait for.
194 * @async: If unset, wait only if the device's power.async_suspend flag is set.
196 static void dpm_wait(struct device *dev, bool async)
198 if (!dev)
199 return;
201 if (async || (pm_async_enabled && dev->power.async_suspend))
202 wait_for_completion(&dev->power.completion);
205 static int dpm_wait_fn(struct device *dev, void *async_ptr)
207 dpm_wait(dev, *((bool *)async_ptr));
208 return 0;
211 static void dpm_wait_for_children(struct device *dev, bool async)
213 device_for_each_child(dev, &async, dpm_wait_fn);
217 * pm_op - Return the PM operation appropriate for given PM event.
218 * @ops: PM operations to choose from.
219 * @state: PM transition of the system being carried out.
221 static pm_callback_t pm_op(const struct dev_pm_ops *ops, pm_message_t state)
223 switch (state.event) {
224 #ifdef CONFIG_SUSPEND
225 case PM_EVENT_SUSPEND:
226 return ops->suspend;
227 case PM_EVENT_RESUME:
228 return ops->resume;
229 #endif /* CONFIG_SUSPEND */
230 #ifdef CONFIG_HIBERNATE_CALLBACKS
231 case PM_EVENT_FREEZE:
232 case PM_EVENT_QUIESCE:
233 return ops->freeze;
234 case PM_EVENT_HIBERNATE:
235 return ops->poweroff;
236 case PM_EVENT_THAW:
237 case PM_EVENT_RECOVER:
238 return ops->thaw;
239 break;
240 case PM_EVENT_RESTORE:
241 return ops->restore;
242 #endif /* CONFIG_HIBERNATE_CALLBACKS */
245 return NULL;
249 * pm_noirq_op - Return the PM operation appropriate for given PM event.
250 * @ops: PM operations to choose from.
251 * @state: PM transition of the system being carried out.
253 * The driver of @dev will not receive interrupts while this function is being
254 * executed.
256 static pm_callback_t pm_noirq_op(const struct dev_pm_ops *ops, pm_message_t state)
258 switch (state.event) {
259 #ifdef CONFIG_SUSPEND
260 case PM_EVENT_SUSPEND:
261 return ops->suspend_noirq;
262 case PM_EVENT_RESUME:
263 return ops->resume_noirq;
264 #endif /* CONFIG_SUSPEND */
265 #ifdef CONFIG_HIBERNATE_CALLBACKS
266 case PM_EVENT_FREEZE:
267 case PM_EVENT_QUIESCE:
268 return ops->freeze_noirq;
269 case PM_EVENT_HIBERNATE:
270 return ops->poweroff_noirq;
271 case PM_EVENT_THAW:
272 case PM_EVENT_RECOVER:
273 return ops->thaw_noirq;
274 case PM_EVENT_RESTORE:
275 return ops->restore_noirq;
276 #endif /* CONFIG_HIBERNATE_CALLBACKS */
279 return NULL;
282 static char *pm_verb(int event)
284 switch (event) {
285 case PM_EVENT_SUSPEND:
286 return "suspend";
287 case PM_EVENT_RESUME:
288 return "resume";
289 case PM_EVENT_FREEZE:
290 return "freeze";
291 case PM_EVENT_QUIESCE:
292 return "quiesce";
293 case PM_EVENT_HIBERNATE:
294 return "hibernate";
295 case PM_EVENT_THAW:
296 return "thaw";
297 case PM_EVENT_RESTORE:
298 return "restore";
299 case PM_EVENT_RECOVER:
300 return "recover";
301 default:
302 return "(unknown PM event)";
306 static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
308 dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
309 ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
310 ", may wakeup" : "");
313 static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
314 int error)
316 printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
317 dev_name(dev), pm_verb(state.event), info, error);
320 static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info)
322 ktime_t calltime;
323 u64 usecs64;
324 int usecs;
326 calltime = ktime_get();
327 usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
328 do_div(usecs64, NSEC_PER_USEC);
329 usecs = usecs64;
330 if (usecs == 0)
331 usecs = 1;
332 pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
333 info ?: "", info ? " " : "", pm_verb(state.event),
334 usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
337 static int dpm_run_callback(pm_callback_t cb, struct device *dev,
338 pm_message_t state, char *info)
340 ktime_t calltime;
341 int error;
343 if (!cb)
344 return 0;
346 calltime = initcall_debug_start(dev);
348 pm_dev_dbg(dev, state, info);
349 error = cb(dev);
350 suspend_report_result(cb, error);
352 initcall_debug_report(dev, calltime, error);
354 return error;
357 /*------------------------- Resume routines -------------------------*/
360 * device_resume_noirq - Execute an "early resume" callback for given device.
361 * @dev: Device to handle.
362 * @state: PM transition of the system being carried out.
364 * The driver of @dev will not receive interrupts while this function is being
365 * executed.
367 static int device_resume_noirq(struct device *dev, pm_message_t state)
369 pm_callback_t callback = NULL;
370 char *info = NULL;
371 int error = 0;
373 TRACE_DEVICE(dev);
374 TRACE_RESUME(0);
376 if (dev->pm_domain) {
377 info = "EARLY power domain ";
378 callback = pm_noirq_op(&dev->pm_domain->ops, state);
379 } else if (dev->type && dev->type->pm) {
380 info = "EARLY type ";
381 callback = pm_noirq_op(dev->type->pm, state);
382 } else if (dev->class && dev->class->pm) {
383 info = "EARLY class ";
384 callback = pm_noirq_op(dev->class->pm, state);
385 } else if (dev->bus && dev->bus->pm) {
386 info = "EARLY bus ";
387 callback = pm_noirq_op(dev->bus->pm, state);
390 if (!callback && dev->driver && dev->driver->pm) {
391 info = "EARLY driver ";
392 callback = pm_noirq_op(dev->driver->pm, state);
395 error = dpm_run_callback(callback, dev, state, info);
397 TRACE_RESUME(error);
398 return error;
402 * dpm_resume_noirq - Execute "early resume" callbacks for non-sysdev devices.
403 * @state: PM transition of the system being carried out.
405 * Call the "noirq" resume handlers for all devices marked as DPM_OFF_IRQ and
406 * enable device drivers to receive interrupts.
408 void dpm_resume_noirq(pm_message_t state)
410 ktime_t starttime = ktime_get();
412 mutex_lock(&dpm_list_mtx);
413 while (!list_empty(&dpm_noirq_list)) {
414 struct device *dev = to_device(dpm_noirq_list.next);
415 int error;
417 get_device(dev);
418 list_move_tail(&dev->power.entry, &dpm_suspended_list);
419 mutex_unlock(&dpm_list_mtx);
421 error = device_resume_noirq(dev, state);
422 if (error) {
423 suspend_stats.failed_resume_noirq++;
424 dpm_save_failed_step(SUSPEND_RESUME_NOIRQ);
425 dpm_save_failed_dev(dev_name(dev));
426 pm_dev_err(dev, state, " early", error);
429 mutex_lock(&dpm_list_mtx);
430 put_device(dev);
432 mutex_unlock(&dpm_list_mtx);
433 dpm_show_time(starttime, state, "early");
434 resume_device_irqs();
436 EXPORT_SYMBOL_GPL(dpm_resume_noirq);
439 * device_resume - Execute "resume" callbacks for given device.
440 * @dev: Device to handle.
441 * @state: PM transition of the system being carried out.
442 * @async: If true, the device is being resumed asynchronously.
444 static int device_resume(struct device *dev, pm_message_t state, bool async)
446 pm_callback_t callback = NULL;
447 char *info = NULL;
448 int error = 0;
449 bool put = false;
451 TRACE_DEVICE(dev);
452 TRACE_RESUME(0);
454 dpm_wait(dev->parent, async);
455 device_lock(dev);
458 * This is a fib. But we'll allow new children to be added below
459 * a resumed device, even if the device hasn't been completed yet.
461 dev->power.is_prepared = false;
463 if (!dev->power.is_suspended)
464 goto Unlock;
466 pm_runtime_enable(dev);
467 put = true;
469 if (dev->pm_domain) {
470 info = "power domain ";
471 callback = pm_op(&dev->pm_domain->ops, state);
472 goto Driver;
475 if (dev->type && dev->type->pm) {
476 info = "type ";
477 callback = pm_op(dev->type->pm, state);
478 goto Driver;
481 if (dev->class) {
482 if (dev->class->pm) {
483 info = "class ";
484 callback = pm_op(dev->class->pm, state);
485 goto Driver;
486 } else if (dev->class->resume) {
487 info = "legacy class ";
488 callback = dev->class->resume;
489 goto End;
493 if (dev->bus) {
494 if (dev->bus->pm) {
495 info = "bus ";
496 callback = pm_op(dev->bus->pm, state);
497 } else if (dev->bus->resume) {
498 info = "legacy bus ";
499 callback = dev->bus->resume;
500 goto End;
504 Driver:
505 if (!callback && dev->driver && dev->driver->pm) {
506 info = "driver ";
507 callback = pm_op(dev->driver->pm, state);
510 End:
511 error = dpm_run_callback(callback, dev, state, info);
512 dev->power.is_suspended = false;
514 Unlock:
515 device_unlock(dev);
516 complete_all(&dev->power.completion);
518 TRACE_RESUME(error);
520 if (put)
521 pm_runtime_put_sync(dev);
523 return error;
526 static void async_resume(void *data, async_cookie_t cookie)
528 struct device *dev = (struct device *)data;
529 int error;
531 error = device_resume(dev, pm_transition, true);
532 if (error)
533 pm_dev_err(dev, pm_transition, " async", error);
534 put_device(dev);
537 static bool is_async(struct device *dev)
539 return dev->power.async_suspend && pm_async_enabled
540 && !pm_trace_is_enabled();
544 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
545 * @state: PM transition of the system being carried out.
547 * Execute the appropriate "resume" callback for all devices whose status
548 * indicates that they are suspended.
550 void dpm_resume(pm_message_t state)
552 struct device *dev;
553 ktime_t starttime = ktime_get();
555 might_sleep();
557 mutex_lock(&dpm_list_mtx);
558 pm_transition = state;
559 async_error = 0;
561 list_for_each_entry(dev, &dpm_suspended_list, power.entry) {
562 INIT_COMPLETION(dev->power.completion);
563 if (is_async(dev)) {
564 get_device(dev);
565 async_schedule(async_resume, dev);
569 while (!list_empty(&dpm_suspended_list)) {
570 dev = to_device(dpm_suspended_list.next);
571 get_device(dev);
572 if (!is_async(dev)) {
573 int error;
575 mutex_unlock(&dpm_list_mtx);
577 error = device_resume(dev, state, false);
578 if (error) {
579 suspend_stats.failed_resume++;
580 dpm_save_failed_step(SUSPEND_RESUME);
581 dpm_save_failed_dev(dev_name(dev));
582 pm_dev_err(dev, state, "", error);
585 mutex_lock(&dpm_list_mtx);
587 if (!list_empty(&dev->power.entry))
588 list_move_tail(&dev->power.entry, &dpm_prepared_list);
589 put_device(dev);
591 mutex_unlock(&dpm_list_mtx);
592 async_synchronize_full();
593 dpm_show_time(starttime, state, NULL);
597 * device_complete - Complete a PM transition for given device.
598 * @dev: Device to handle.
599 * @state: PM transition of the system being carried out.
601 static void device_complete(struct device *dev, pm_message_t state)
603 void (*callback)(struct device *) = NULL;
604 char *info = NULL;
606 device_lock(dev);
608 if (dev->pm_domain) {
609 info = "completing power domain ";
610 callback = dev->pm_domain->ops.complete;
611 } else if (dev->type && dev->type->pm) {
612 info = "completing type ";
613 callback = dev->type->pm->complete;
614 } else if (dev->class && dev->class->pm) {
615 info = "completing class ";
616 callback = dev->class->pm->complete;
617 } else if (dev->bus && dev->bus->pm) {
618 info = "completing bus ";
619 callback = dev->bus->pm->complete;
622 if (!callback && dev->driver && dev->driver->pm) {
623 info = "completing driver ";
624 callback = dev->driver->pm->complete;
627 if (callback) {
628 pm_dev_dbg(dev, state, info);
629 callback(dev);
632 device_unlock(dev);
636 * dpm_complete - Complete a PM transition for all non-sysdev devices.
637 * @state: PM transition of the system being carried out.
639 * Execute the ->complete() callbacks for all devices whose PM status is not
640 * DPM_ON (this allows new devices to be registered).
642 void dpm_complete(pm_message_t state)
644 struct list_head list;
646 might_sleep();
648 INIT_LIST_HEAD(&list);
649 mutex_lock(&dpm_list_mtx);
650 while (!list_empty(&dpm_prepared_list)) {
651 struct device *dev = to_device(dpm_prepared_list.prev);
653 get_device(dev);
654 dev->power.is_prepared = false;
655 list_move(&dev->power.entry, &list);
656 mutex_unlock(&dpm_list_mtx);
658 device_complete(dev, state);
660 mutex_lock(&dpm_list_mtx);
661 put_device(dev);
663 list_splice(&list, &dpm_list);
664 mutex_unlock(&dpm_list_mtx);
668 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
669 * @state: PM transition of the system being carried out.
671 * Execute "resume" callbacks for all devices and complete the PM transition of
672 * the system.
674 void dpm_resume_end(pm_message_t state)
676 dpm_resume(state);
677 dpm_complete(state);
679 EXPORT_SYMBOL_GPL(dpm_resume_end);
682 /*------------------------- Suspend routines -------------------------*/
685 * resume_event - Return a "resume" message for given "suspend" sleep state.
686 * @sleep_state: PM message representing a sleep state.
688 * Return a PM message representing the resume event corresponding to given
689 * sleep state.
691 static pm_message_t resume_event(pm_message_t sleep_state)
693 switch (sleep_state.event) {
694 case PM_EVENT_SUSPEND:
695 return PMSG_RESUME;
696 case PM_EVENT_FREEZE:
697 case PM_EVENT_QUIESCE:
698 return PMSG_RECOVER;
699 case PM_EVENT_HIBERNATE:
700 return PMSG_RESTORE;
702 return PMSG_ON;
706 * device_suspend_noirq - Execute a "late suspend" callback for given device.
707 * @dev: Device to handle.
708 * @state: PM transition of the system being carried out.
710 * The driver of @dev will not receive interrupts while this function is being
711 * executed.
713 static int device_suspend_noirq(struct device *dev, pm_message_t state)
715 pm_callback_t callback = NULL;
716 char *info = NULL;
718 if (dev->pm_domain) {
719 info = "LATE power domain ";
720 callback = pm_noirq_op(&dev->pm_domain->ops, state);
721 } else if (dev->type && dev->type->pm) {
722 info = "LATE type ";
723 callback = pm_noirq_op(dev->type->pm, state);
724 } else if (dev->class && dev->class->pm) {
725 info = "LATE class ";
726 callback = pm_noirq_op(dev->class->pm, state);
727 } else if (dev->bus && dev->bus->pm) {
728 info = "LATE bus ";
729 callback = pm_noirq_op(dev->bus->pm, state);
732 if (!callback && dev->driver && dev->driver->pm) {
733 info = "LATE driver ";
734 callback = pm_noirq_op(dev->driver->pm, state);
737 return dpm_run_callback(callback, dev, state, info);
741 * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices.
742 * @state: PM transition of the system being carried out.
744 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
745 * handlers for all non-sysdev devices.
747 int dpm_suspend_noirq(pm_message_t state)
749 ktime_t starttime = ktime_get();
750 int error = 0;
752 suspend_device_irqs();
753 mutex_lock(&dpm_list_mtx);
754 while (!list_empty(&dpm_suspended_list)) {
755 struct device *dev = to_device(dpm_suspended_list.prev);
757 get_device(dev);
758 mutex_unlock(&dpm_list_mtx);
760 error = device_suspend_noirq(dev, state);
762 mutex_lock(&dpm_list_mtx);
763 if (error) {
764 pm_dev_err(dev, state, " late", error);
765 suspend_stats.failed_suspend_noirq++;
766 dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ);
767 dpm_save_failed_dev(dev_name(dev));
768 put_device(dev);
769 break;
771 if (!list_empty(&dev->power.entry))
772 list_move(&dev->power.entry, &dpm_noirq_list);
773 put_device(dev);
775 mutex_unlock(&dpm_list_mtx);
776 if (error)
777 dpm_resume_noirq(resume_event(state));
778 else
779 dpm_show_time(starttime, state, "late");
780 return error;
782 EXPORT_SYMBOL_GPL(dpm_suspend_noirq);
785 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
786 * @dev: Device to suspend.
787 * @state: PM transition of the system being carried out.
788 * @cb: Suspend callback to execute.
790 static int legacy_suspend(struct device *dev, pm_message_t state,
791 int (*cb)(struct device *dev, pm_message_t state))
793 int error;
794 ktime_t calltime;
796 calltime = initcall_debug_start(dev);
798 error = cb(dev, state);
799 suspend_report_result(cb, error);
801 initcall_debug_report(dev, calltime, error);
803 return error;
807 * device_suspend - Execute "suspend" callbacks for given device.
808 * @dev: Device to handle.
809 * @state: PM transition of the system being carried out.
810 * @async: If true, the device is being suspended asynchronously.
812 static int __device_suspend(struct device *dev, pm_message_t state, bool async)
814 pm_callback_t callback = NULL;
815 char *info = NULL;
816 int error = 0;
818 dpm_wait_for_children(dev, async);
820 if (async_error)
821 return 0;
823 pm_runtime_get_noresume(dev);
824 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
825 pm_wakeup_event(dev, 0);
827 if (pm_wakeup_pending()) {
828 pm_runtime_put_sync(dev);
829 async_error = -EBUSY;
830 return 0;
833 device_lock(dev);
835 if (dev->pm_domain) {
836 info = "power domain ";
837 callback = pm_op(&dev->pm_domain->ops, state);
838 goto Run;
841 if (dev->type && dev->type->pm) {
842 info = "type ";
843 callback = pm_op(dev->type->pm, state);
844 goto Run;
847 if (dev->class) {
848 if (dev->class->pm) {
849 info = "class ";
850 callback = pm_op(dev->class->pm, state);
851 goto Run;
852 } else if (dev->class->suspend) {
853 pm_dev_dbg(dev, state, "legacy class ");
854 error = legacy_suspend(dev, state, dev->class->suspend);
855 goto End;
859 if (dev->bus) {
860 if (dev->bus->pm) {
861 info = "bus ";
862 callback = pm_op(dev->bus->pm, state);
863 } else if (dev->bus->suspend) {
864 pm_dev_dbg(dev, state, "legacy bus ");
865 error = legacy_suspend(dev, state, dev->bus->suspend);
866 goto End;
870 Run:
871 if (!callback && dev->driver && dev->driver->pm) {
872 info = "driver ";
873 callback = pm_op(dev->driver->pm, state);
876 error = dpm_run_callback(callback, dev, state, info);
878 End:
879 if (!error) {
880 dev->power.is_suspended = true;
881 if (dev->power.wakeup_path
882 && dev->parent && !dev->parent->power.ignore_children)
883 dev->parent->power.wakeup_path = true;
886 device_unlock(dev);
887 complete_all(&dev->power.completion);
889 if (error) {
890 pm_runtime_put_sync(dev);
891 async_error = error;
892 } else if (dev->power.is_suspended) {
893 __pm_runtime_disable(dev, false);
896 return error;
899 static void async_suspend(void *data, async_cookie_t cookie)
901 struct device *dev = (struct device *)data;
902 int error;
904 error = __device_suspend(dev, pm_transition, true);
905 if (error) {
906 dpm_save_failed_dev(dev_name(dev));
907 pm_dev_err(dev, pm_transition, " async", error);
910 put_device(dev);
913 static int device_suspend(struct device *dev)
915 INIT_COMPLETION(dev->power.completion);
917 if (pm_async_enabled && dev->power.async_suspend) {
918 get_device(dev);
919 async_schedule(async_suspend, dev);
920 return 0;
923 return __device_suspend(dev, pm_transition, false);
927 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
928 * @state: PM transition of the system being carried out.
930 int dpm_suspend(pm_message_t state)
932 ktime_t starttime = ktime_get();
933 int error = 0;
935 might_sleep();
937 mutex_lock(&dpm_list_mtx);
938 pm_transition = state;
939 async_error = 0;
940 while (!list_empty(&dpm_prepared_list)) {
941 struct device *dev = to_device(dpm_prepared_list.prev);
943 get_device(dev);
944 mutex_unlock(&dpm_list_mtx);
946 error = device_suspend(dev);
948 mutex_lock(&dpm_list_mtx);
949 if (error) {
950 pm_dev_err(dev, state, "", error);
951 dpm_save_failed_dev(dev_name(dev));
952 put_device(dev);
953 break;
955 if (!list_empty(&dev->power.entry))
956 list_move(&dev->power.entry, &dpm_suspended_list);
957 put_device(dev);
958 if (async_error)
959 break;
961 mutex_unlock(&dpm_list_mtx);
962 async_synchronize_full();
963 if (!error)
964 error = async_error;
965 if (error) {
966 suspend_stats.failed_suspend++;
967 dpm_save_failed_step(SUSPEND_SUSPEND);
968 } else
969 dpm_show_time(starttime, state, NULL);
970 return error;
974 * device_prepare - Prepare a device for system power transition.
975 * @dev: Device to handle.
976 * @state: PM transition of the system being carried out.
978 * Execute the ->prepare() callback(s) for given device. No new children of the
979 * device may be registered after this function has returned.
981 static int device_prepare(struct device *dev, pm_message_t state)
983 int (*callback)(struct device *) = NULL;
984 char *info = NULL;
985 int error = 0;
987 device_lock(dev);
989 dev->power.wakeup_path = device_may_wakeup(dev);
991 if (dev->pm_domain) {
992 info = "preparing power domain ";
993 callback = dev->pm_domain->ops.prepare;
994 } else if (dev->type && dev->type->pm) {
995 info = "preparing type ";
996 callback = dev->type->pm->prepare;
997 } else if (dev->class && dev->class->pm) {
998 info = "preparing class ";
999 callback = dev->class->pm->prepare;
1000 } else if (dev->bus && dev->bus->pm) {
1001 info = "preparing bus ";
1002 callback = dev->bus->pm->prepare;
1005 if (!callback && dev->driver && dev->driver->pm) {
1006 info = "preparing driver ";
1007 callback = dev->driver->pm->prepare;
1010 if (callback) {
1011 error = callback(dev);
1012 suspend_report_result(callback, error);
1015 device_unlock(dev);
1017 return error;
1021 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1022 * @state: PM transition of the system being carried out.
1024 * Execute the ->prepare() callback(s) for all devices.
1026 int dpm_prepare(pm_message_t state)
1028 int error = 0;
1030 might_sleep();
1032 mutex_lock(&dpm_list_mtx);
1033 while (!list_empty(&dpm_list)) {
1034 struct device *dev = to_device(dpm_list.next);
1036 get_device(dev);
1037 mutex_unlock(&dpm_list_mtx);
1039 error = device_prepare(dev, state);
1041 mutex_lock(&dpm_list_mtx);
1042 if (error) {
1043 if (error == -EAGAIN) {
1044 put_device(dev);
1045 error = 0;
1046 continue;
1048 printk(KERN_INFO "PM: Device %s not prepared "
1049 "for power transition: code %d\n",
1050 dev_name(dev), error);
1051 put_device(dev);
1052 break;
1054 dev->power.is_prepared = true;
1055 if (!list_empty(&dev->power.entry))
1056 list_move_tail(&dev->power.entry, &dpm_prepared_list);
1057 put_device(dev);
1059 mutex_unlock(&dpm_list_mtx);
1060 return error;
1064 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1065 * @state: PM transition of the system being carried out.
1067 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1068 * callbacks for them.
1070 int dpm_suspend_start(pm_message_t state)
1072 int error;
1074 error = dpm_prepare(state);
1075 if (error) {
1076 suspend_stats.failed_prepare++;
1077 dpm_save_failed_step(SUSPEND_PREPARE);
1078 } else
1079 error = dpm_suspend(state);
1080 return error;
1082 EXPORT_SYMBOL_GPL(dpm_suspend_start);
1084 void __suspend_report_result(const char *function, void *fn, int ret)
1086 if (ret)
1087 printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
1089 EXPORT_SYMBOL_GPL(__suspend_report_result);
1092 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1093 * @dev: Device to wait for.
1094 * @subordinate: Device that needs to wait for @dev.
1096 int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
1098 dpm_wait(dev, subordinate->power.async_suspend);
1099 return async_error;
1101 EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);