include: convert various register fcns to macros to avoid include chaining
[linux-2.6/next.git] / drivers / base / power / main.c
blobcc4a222e73ab8a45e0b811d6ce21456e930d87f5
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"
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
42 * dpm_list_mutex.
45 LIST_HEAD(dpm_list);
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;
55 /**
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);
67 pm_runtime_init(dev);
68 INIT_LIST_HEAD(&dev->power.entry);
71 /**
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);
79 /**
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);
87 /**
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();
168 return calltime;
171 static void initcall_debug_report(struct device *dev, ktime_t calltime,
172 int error)
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)
191 if (!dev)
192 return;
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));
201 return 0;
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,
217 pm_message_t state)
219 int error = 0;
220 ktime_t calltime;
222 calltime = initcall_debug_start(dev);
224 switch (state.event) {
225 #ifdef CONFIG_SUSPEND
226 case PM_EVENT_SUSPEND:
227 if (ops->suspend) {
228 error = ops->suspend(dev);
229 suspend_report_result(ops->suspend, error);
231 break;
232 case PM_EVENT_RESUME:
233 if (ops->resume) {
234 error = ops->resume(dev);
235 suspend_report_result(ops->resume, error);
237 break;
238 #endif /* CONFIG_SUSPEND */
239 #ifdef CONFIG_HIBERNATE_CALLBACKS
240 case PM_EVENT_FREEZE:
241 case PM_EVENT_QUIESCE:
242 if (ops->freeze) {
243 error = ops->freeze(dev);
244 suspend_report_result(ops->freeze, error);
246 break;
247 case PM_EVENT_HIBERNATE:
248 if (ops->poweroff) {
249 error = ops->poweroff(dev);
250 suspend_report_result(ops->poweroff, error);
252 break;
253 case PM_EVENT_THAW:
254 case PM_EVENT_RECOVER:
255 if (ops->thaw) {
256 error = ops->thaw(dev);
257 suspend_report_result(ops->thaw, error);
259 break;
260 case PM_EVENT_RESTORE:
261 if (ops->restore) {
262 error = ops->restore(dev);
263 suspend_report_result(ops->restore, error);
265 break;
266 #endif /* CONFIG_HIBERNATE_CALLBACKS */
267 default:
268 error = -EINVAL;
271 initcall_debug_report(dev, calltime, error);
273 return 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
283 * executed.
285 static int pm_noirq_op(struct device *dev,
286 const struct dev_pm_ops *ops,
287 pm_message_t state)
289 int error = 0;
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);
306 break;
307 case PM_EVENT_RESUME:
308 if (ops->resume_noirq) {
309 error = ops->resume_noirq(dev);
310 suspend_report_result(ops->resume_noirq, error);
312 break;
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);
321 break;
322 case PM_EVENT_HIBERNATE:
323 if (ops->poweroff_noirq) {
324 error = ops->poweroff_noirq(dev);
325 suspend_report_result(ops->poweroff_noirq, error);
327 break;
328 case PM_EVENT_THAW:
329 case PM_EVENT_RECOVER:
330 if (ops->thaw_noirq) {
331 error = ops->thaw_noirq(dev);
332 suspend_report_result(ops->thaw_noirq, error);
334 break;
335 case PM_EVENT_RESTORE:
336 if (ops->restore_noirq) {
337 error = ops->restore_noirq(dev);
338 suspend_report_result(ops->restore_noirq, error);
340 break;
341 #endif /* CONFIG_HIBERNATE_CALLBACKS */
342 default:
343 error = -EINVAL;
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);
354 return error;
357 static char *pm_verb(int event)
359 switch (event) {
360 case PM_EVENT_SUSPEND:
361 return "suspend";
362 case PM_EVENT_RESUME:
363 return "resume";
364 case PM_EVENT_FREEZE:
365 return "freeze";
366 case PM_EVENT_QUIESCE:
367 return "quiesce";
368 case PM_EVENT_HIBERNATE:
369 return "hibernate";
370 case PM_EVENT_THAW:
371 return "thaw";
372 case PM_EVENT_RESTORE:
373 return "restore";
374 case PM_EVENT_RECOVER:
375 return "recover";
376 default:
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,
389 int error)
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)
397 ktime_t calltime;
398 u64 usecs64;
399 int usecs;
401 calltime = ktime_get();
402 usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
403 do_div(usecs64, NSEC_PER_USEC);
404 usecs = usecs64;
405 if (usecs == 0)
406 usecs = 1;
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
420 * executed.
422 static int device_resume_noirq(struct device *dev, pm_message_t state)
424 int error = 0;
426 TRACE_DEVICE(dev);
427 TRACE_RESUME(0);
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);
443 TRACE_RESUME(error);
444 return error;
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);
461 int error;
463 get_device(dev);
464 list_move_tail(&dev->power.entry, &dpm_suspended_list);
465 mutex_unlock(&dpm_list_mtx);
467 error = device_resume_noirq(dev, state);
468 if (error)
469 pm_dev_err(dev, state, " early", error);
471 mutex_lock(&dpm_list_mtx);
472 put_device(dev);
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))
487 int error;
488 ktime_t calltime;
490 calltime = initcall_debug_start(dev);
492 error = cb(dev);
493 suspend_report_result(cb, error);
495 initcall_debug_report(dev, calltime, error);
497 return 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)
508 int error = 0;
509 bool put = false;
511 TRACE_DEVICE(dev);
512 TRACE_RESUME(0);
514 dpm_wait(dev->parent, async);
515 device_lock(dev);
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)
524 goto Unlock;
526 pm_runtime_enable(dev);
527 put = true;
529 if (dev->pm_domain) {
530 pm_dev_dbg(dev, state, "power domain ");
531 error = pm_op(dev, &dev->pm_domain->ops, state);
532 goto End;
535 if (dev->type && dev->type->pm) {
536 pm_dev_dbg(dev, state, "type ");
537 error = pm_op(dev, dev->type->pm, state);
538 goto End;
541 if (dev->class) {
542 if (dev->class->pm) {
543 pm_dev_dbg(dev, state, "class ");
544 error = pm_op(dev, dev->class->pm, state);
545 goto End;
546 } else if (dev->class->resume) {
547 pm_dev_dbg(dev, state, "legacy class ");
548 error = legacy_resume(dev, dev->class->resume);
549 goto End;
553 if (dev->bus) {
554 if (dev->bus->pm) {
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);
563 End:
564 dev->power.is_suspended = false;
566 Unlock:
567 device_unlock(dev);
568 complete_all(&dev->power.completion);
570 TRACE_RESUME(error);
572 if (put)
573 pm_runtime_put_sync(dev);
575 return error;
578 static void async_resume(void *data, async_cookie_t cookie)
580 struct device *dev = (struct device *)data;
581 int error;
583 error = device_resume(dev, pm_transition, true);
584 if (error)
585 pm_dev_err(dev, pm_transition, " async", error);
586 put_device(dev);
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)
604 struct device *dev;
605 ktime_t starttime = ktime_get();
607 might_sleep();
609 mutex_lock(&dpm_list_mtx);
610 pm_transition = state;
611 async_error = 0;
613 list_for_each_entry(dev, &dpm_suspended_list, power.entry) {
614 INIT_COMPLETION(dev->power.completion);
615 if (is_async(dev)) {
616 get_device(dev);
617 async_schedule(async_resume, dev);
621 while (!list_empty(&dpm_suspended_list)) {
622 dev = to_device(dpm_suspended_list.next);
623 get_device(dev);
624 if (!is_async(dev)) {
625 int error;
627 mutex_unlock(&dpm_list_mtx);
629 error = device_resume(dev, state, false);
630 if (error)
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);
637 put_device(dev);
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)
651 device_lock(dev);
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);
671 device_unlock(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;
685 might_sleep();
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);
692 get_device(dev);
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);
700 put_device(dev);
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
711 * the system.
713 void dpm_resume_end(pm_message_t state)
715 dpm_resume(state);
716 dpm_complete(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
728 * sleep state.
730 static pm_message_t resume_event(pm_message_t sleep_state)
732 switch (sleep_state.event) {
733 case PM_EVENT_SUSPEND:
734 return PMSG_RESUME;
735 case PM_EVENT_FREEZE:
736 case PM_EVENT_QUIESCE:
737 return PMSG_RECOVER;
738 case PM_EVENT_HIBERNATE:
739 return PMSG_RESTORE;
741 return PMSG_ON;
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
750 * executed.
752 static int device_suspend_noirq(struct device *dev, pm_message_t state)
754 int error;
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);
759 if (error)
760 return error;
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);
764 if (error)
765 return error;
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);
769 if (error)
770 return error;
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);
774 if (error)
775 return error;
778 return 0;
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();
791 int error = 0;
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);
798 get_device(dev);
799 mutex_unlock(&dpm_list_mtx);
801 error = device_suspend_noirq(dev, state);
803 mutex_lock(&dpm_list_mtx);
804 if (error) {
805 pm_dev_err(dev, state, " late", error);
806 put_device(dev);
807 break;
809 if (!list_empty(&dev->power.entry))
810 list_move(&dev->power.entry, &dpm_noirq_list);
811 put_device(dev);
813 mutex_unlock(&dpm_list_mtx);
814 if (error)
815 dpm_resume_noirq(resume_event(state));
816 else
817 dpm_show_time(starttime, state, "late");
818 return error;
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))
831 int error;
832 ktime_t calltime;
834 calltime = initcall_debug_start(dev);
836 error = cb(dev, state);
837 suspend_report_result(cb, error);
839 initcall_debug_report(dev, calltime, error);
841 return 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)
852 int error = 0;
854 dpm_wait_for_children(dev, async);
856 if (async_error)
857 return 0;
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;
866 return 0;
869 device_lock(dev);
871 if (dev->pm_domain) {
872 pm_dev_dbg(dev, state, "power domain ");
873 error = pm_op(dev, &dev->pm_domain->ops, state);
874 goto End;
877 if (dev->type && dev->type->pm) {
878 pm_dev_dbg(dev, state, "type ");
879 error = pm_op(dev, dev->type->pm, state);
880 goto End;
883 if (dev->class) {
884 if (dev->class->pm) {
885 pm_dev_dbg(dev, state, "class ");
886 error = pm_op(dev, dev->class->pm, state);
887 goto End;
888 } else if (dev->class->suspend) {
889 pm_dev_dbg(dev, state, "legacy class ");
890 error = legacy_suspend(dev, state, dev->class->suspend);
891 goto End;
895 if (dev->bus) {
896 if (dev->bus->pm) {
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);
905 End:
906 dev->power.is_suspended = !error;
908 device_unlock(dev);
909 complete_all(&dev->power.completion);
911 if (error) {
912 pm_runtime_put_sync(dev);
913 async_error = error;
914 } else if (dev->power.is_suspended) {
915 __pm_runtime_disable(dev, false);
918 return error;
921 static void async_suspend(void *data, async_cookie_t cookie)
923 struct device *dev = (struct device *)data;
924 int error;
926 error = __device_suspend(dev, pm_transition, true);
927 if (error)
928 pm_dev_err(dev, pm_transition, " async", error);
930 put_device(dev);
933 static int device_suspend(struct device *dev)
935 INIT_COMPLETION(dev->power.completion);
937 if (pm_async_enabled && dev->power.async_suspend) {
938 get_device(dev);
939 async_schedule(async_suspend, dev);
940 return 0;
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();
953 int error = 0;
955 might_sleep();
957 mutex_lock(&dpm_list_mtx);
958 pm_transition = state;
959 async_error = 0;
960 while (!list_empty(&dpm_prepared_list)) {
961 struct device *dev = to_device(dpm_prepared_list.prev);
963 get_device(dev);
964 mutex_unlock(&dpm_list_mtx);
966 error = device_suspend(dev);
968 mutex_lock(&dpm_list_mtx);
969 if (error) {
970 pm_dev_err(dev, state, "", error);
971 put_device(dev);
972 break;
974 if (!list_empty(&dev->power.entry))
975 list_move(&dev->power.entry, &dpm_suspended_list);
976 put_device(dev);
977 if (async_error)
978 break;
980 mutex_unlock(&dpm_list_mtx);
981 async_synchronize_full();
982 if (!error)
983 error = async_error;
984 if (!error)
985 dpm_show_time(starttime, state, NULL);
986 return error;
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)
999 int error = 0;
1001 device_lock(dev);
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);
1008 if (error)
1009 goto End;
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);
1015 if (error)
1016 goto End;
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);
1022 if (error)
1023 goto End;
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);
1031 End:
1032 device_unlock(dev);
1034 return 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)
1045 int error = 0;
1047 might_sleep();
1049 mutex_lock(&dpm_list_mtx);
1050 while (!list_empty(&dpm_list)) {
1051 struct device *dev = to_device(dpm_list.next);
1053 get_device(dev);
1054 mutex_unlock(&dpm_list_mtx);
1056 error = device_prepare(dev, state);
1058 mutex_lock(&dpm_list_mtx);
1059 if (error) {
1060 if (error == -EAGAIN) {
1061 put_device(dev);
1062 error = 0;
1063 continue;
1065 printk(KERN_INFO "PM: Device %s not prepared "
1066 "for power transition: code %d\n",
1067 dev_name(dev), error);
1068 put_device(dev);
1069 break;
1071 dev->power.is_prepared = true;
1072 if (!list_empty(&dev->power.entry))
1073 list_move_tail(&dev->power.entry, &dpm_prepared_list);
1074 put_device(dev);
1076 mutex_unlock(&dpm_list_mtx);
1077 return error;
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)
1089 int error;
1091 error = dpm_prepare(state);
1092 if (!error)
1093 error = dpm_suspend(state);
1094 return error;
1096 EXPORT_SYMBOL_GPL(dpm_suspend_start);
1098 void __suspend_report_result(const char *function, void *fn, int ret)
1100 if (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);
1113 return async_error;
1115 EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);