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 intialize 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 different set of lists than the global subsystem list are used to
16 * keep track of power info because we use different lists to hold
17 * devices based on what stage of the power management process they
18 * are in. The power domain dependencies may also differ from the
19 * ancestral dependencies that the subsystem list maintains.
22 #include <linux/device.h>
23 #include <linux/kallsyms.h>
24 #include <linux/mutex.h>
26 #include <linux/resume-trace.h>
31 LIST_HEAD(dpm_active);
32 static LIST_HEAD(dpm_off);
33 static LIST_HEAD(dpm_off_irq);
35 static DEFINE_MUTEX(dpm_mtx);
36 static DEFINE_MUTEX(dpm_list_mtx);
38 int (*platform_enable_wakeup)(struct device *dev, int is_on);
41 int device_pm_add(struct device *dev)
45 pr_debug("PM: Adding info for %s:%s\n",
46 dev->bus ? dev->bus->name : "No Bus",
47 kobject_name(&dev->kobj));
48 mutex_lock(&dpm_list_mtx);
49 list_add_tail(&dev->power.entry, &dpm_active);
50 error = dpm_sysfs_add(dev);
52 list_del(&dev->power.entry);
53 mutex_unlock(&dpm_list_mtx);
57 void device_pm_remove(struct device *dev)
59 pr_debug("PM: Removing info for %s:%s\n",
60 dev->bus ? dev->bus->name : "No Bus",
61 kobject_name(&dev->kobj));
62 mutex_lock(&dpm_list_mtx);
63 dpm_sysfs_remove(dev);
64 list_del_init(&dev->power.entry);
65 mutex_unlock(&dpm_list_mtx);
69 /*------------------------- Resume routines -------------------------*/
72 * resume_device - Restore state for one device.
77 static int resume_device(struct device * dev)
86 if (dev->bus && dev->bus->resume) {
87 dev_dbg(dev,"resuming\n");
88 error = dev->bus->resume(dev);
91 if (!error && dev->type && dev->type->resume) {
92 dev_dbg(dev,"resuming\n");
93 error = dev->type->resume(dev);
96 if (!error && dev->class && dev->class->resume) {
97 dev_dbg(dev,"class resume\n");
98 error = dev->class->resume(dev);
108 static int resume_device_early(struct device * dev)
114 if (dev->bus && dev->bus->resume_early) {
115 dev_dbg(dev,"EARLY resume\n");
116 error = dev->bus->resume_early(dev);
123 * Resume the devices that have either not gone through
124 * the late suspend, or that did go through it but also
125 * went through the early resume
127 static void dpm_resume(void)
129 mutex_lock(&dpm_list_mtx);
130 while(!list_empty(&dpm_off)) {
131 struct list_head * entry = dpm_off.next;
132 struct device * dev = to_device(entry);
135 list_move_tail(entry, &dpm_active);
137 mutex_unlock(&dpm_list_mtx);
139 mutex_lock(&dpm_list_mtx);
142 mutex_unlock(&dpm_list_mtx);
147 * device_resume - Restore state of each device in system.
149 * Walk the dpm_off list, remove each entry, resume the device,
150 * then add it to the dpm_active list.
153 void device_resume(void)
156 mutex_lock(&dpm_mtx);
158 mutex_unlock(&dpm_mtx);
161 EXPORT_SYMBOL_GPL(device_resume);
165 * dpm_power_up - Power on some devices.
167 * Walk the dpm_off_irq list and power each device up. This
168 * is used for devices that required they be powered down with
169 * interrupts disabled. As devices are powered on, they are moved
170 * to the dpm_active list.
172 * Interrupts must be disabled when calling this.
175 static void dpm_power_up(void)
177 while(!list_empty(&dpm_off_irq)) {
178 struct list_head * entry = dpm_off_irq.next;
179 struct device * dev = to_device(entry);
181 list_move_tail(entry, &dpm_off);
182 resume_device_early(dev);
188 * device_power_up - Turn on all devices that need special attention.
190 * Power on system devices then devices that required we shut them down
191 * with interrupts disabled.
192 * Called with interrupts disabled.
195 void device_power_up(void)
201 EXPORT_SYMBOL_GPL(device_power_up);
204 /*------------------------- Suspend routines -------------------------*/
207 * The entries in the dpm_active list are in a depth first order, simply
208 * because children are guaranteed to be discovered after parents, and
209 * are inserted at the back of the list on discovery.
211 * All list on the suspend path are done in reverse order, so we operate
212 * on the leaves of the device tree (or forests, depending on how you want
213 * to look at it ;) first. As nodes are removed from the back of the list,
214 * they are inserted into the front of their destintation lists.
216 * Things are the reverse on the resume path - iterations are done in
217 * forward order, and nodes are inserted at the back of their destination
218 * lists. This way, the ancestors will be accessed before their descendents.
221 static inline char *suspend_verb(u32 event)
224 case PM_EVENT_SUSPEND: return "suspend";
225 case PM_EVENT_FREEZE: return "freeze";
226 case PM_EVENT_PRETHAW: return "prethaw";
227 default: return "(unknown suspend event)";
233 suspend_device_dbg(struct device *dev, pm_message_t state, char *info)
235 dev_dbg(dev, "%s%s%s\n", info, suspend_verb(state.event),
236 ((state.event == PM_EVENT_SUSPEND) && device_may_wakeup(dev)) ?
237 ", may wakeup" : "");
241 * suspend_device - Save state of one device.
243 * @state: Power state device is entering.
246 static int suspend_device(struct device * dev, pm_message_t state)
251 if (dev->power.power_state.event) {
252 dev_dbg(dev, "PM: suspend %d-->%d\n",
253 dev->power.power_state.event, state.event);
256 if (dev->class && dev->class->suspend) {
257 suspend_device_dbg(dev, state, "class ");
258 error = dev->class->suspend(dev, state);
259 suspend_report_result(dev->class->suspend, error);
262 if (!error && dev->type && dev->type->suspend) {
263 suspend_device_dbg(dev, state, "type ");
264 error = dev->type->suspend(dev, state);
265 suspend_report_result(dev->type->suspend, error);
268 if (!error && dev->bus && dev->bus->suspend) {
269 suspend_device_dbg(dev, state, "");
270 error = dev->bus->suspend(dev, state);
271 suspend_report_result(dev->bus->suspend, error);
279 * This is called with interrupts off, only a single CPU
280 * running. We can't acquire a mutex or semaphore (and we don't
281 * need the protection)
283 static int suspend_device_late(struct device *dev, pm_message_t state)
287 if (dev->bus && dev->bus->suspend_late) {
288 suspend_device_dbg(dev, state, "LATE ");
289 error = dev->bus->suspend_late(dev, state);
290 suspend_report_result(dev->bus->suspend_late, error);
296 * device_suspend - Save state and stop all devices in system.
297 * @state: Power state to put each device in.
299 * Walk the dpm_active list, call ->suspend() for each device, and move
300 * it to the dpm_off list.
302 * (For historical reasons, if it returns -EAGAIN, that used to mean
303 * that the device would be called again with interrupts disabled.
304 * These days, we use the "suspend_late()" callback for that, so we
305 * print a warning and consider it an error).
307 * If we get a different error, try and back out.
309 * If we hit a failure with any of the devices, call device_resume()
310 * above to bring the suspended devices back to life.
314 int device_suspend(pm_message_t state)
319 mutex_lock(&dpm_mtx);
320 mutex_lock(&dpm_list_mtx);
321 while (!list_empty(&dpm_active) && error == 0) {
322 struct list_head * entry = dpm_active.prev;
323 struct device * dev = to_device(entry);
326 mutex_unlock(&dpm_list_mtx);
328 error = suspend_device(dev, state);
330 mutex_lock(&dpm_list_mtx);
332 /* Check if the device got removed */
333 if (!list_empty(&dev->power.entry)) {
334 /* Move it to the dpm_off list */
336 list_move(&dev->power.entry, &dpm_off);
339 printk(KERN_ERR "Could not suspend device %s: "
341 kobject_name(&dev->kobj), error,
342 error == -EAGAIN ? " (please convert to suspend_late)" : "");
345 mutex_unlock(&dpm_list_mtx);
349 mutex_unlock(&dpm_mtx);
353 EXPORT_SYMBOL_GPL(device_suspend);
356 * device_power_down - Shut down special devices.
357 * @state: Power state to enter.
359 * Walk the dpm_off_irq list, calling ->power_down() for each device that
360 * couldn't power down the device with interrupts enabled. When we're
361 * done, power down system devices.
364 int device_power_down(pm_message_t state)
369 while (!list_empty(&dpm_off)) {
370 struct list_head * entry = dpm_off.prev;
372 dev = to_device(entry);
373 error = suspend_device_late(dev, state);
376 list_move(&dev->power.entry, &dpm_off_irq);
379 error = sysdev_suspend(state);
383 printk(KERN_ERR "Could not power down device %s: "
384 "error %d\n", kobject_name(&dev->kobj), error);
389 EXPORT_SYMBOL_GPL(device_power_down);
391 void __suspend_report_result(const char *function, void *fn, int ret)
394 printk(KERN_ERR "%s(): ", function);
395 print_fn_descriptor_symbol("%s() returns ", (unsigned long)fn);
399 EXPORT_SYMBOL_GPL(__suspend_report_result);