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 void device_pm_add(struct device *dev)
43 pr_debug("PM: Adding info for %s:%s\n",
44 dev->bus ? dev->bus->name : "No Bus",
45 kobject_name(&dev->kobj));
46 mutex_lock(&dpm_list_mtx);
47 list_add_tail(&dev->power.entry, &dpm_active);
48 mutex_unlock(&dpm_list_mtx);
51 void device_pm_remove(struct device *dev)
53 pr_debug("PM: Removing info for %s:%s\n",
54 dev->bus ? dev->bus->name : "No Bus",
55 kobject_name(&dev->kobj));
56 mutex_lock(&dpm_list_mtx);
57 dpm_sysfs_remove(dev);
58 list_del_init(&dev->power.entry);
59 mutex_unlock(&dpm_list_mtx);
63 /*------------------------- Resume routines -------------------------*/
66 * resume_device - Restore state for one device.
71 static int resume_device(struct device * dev)
80 if (dev->bus && dev->bus->resume) {
81 dev_dbg(dev,"resuming\n");
82 error = dev->bus->resume(dev);
85 if (!error && dev->type && dev->type->resume) {
86 dev_dbg(dev,"resuming\n");
87 error = dev->type->resume(dev);
90 if (!error && dev->class && dev->class->resume) {
91 dev_dbg(dev,"class resume\n");
92 error = dev->class->resume(dev);
102 static int resume_device_early(struct device * dev)
108 if (dev->bus && dev->bus->resume_early) {
109 dev_dbg(dev,"EARLY resume\n");
110 error = dev->bus->resume_early(dev);
117 * Resume the devices that have either not gone through
118 * the late suspend, or that did go through it but also
119 * went through the early resume
121 static void dpm_resume(void)
123 mutex_lock(&dpm_list_mtx);
124 while(!list_empty(&dpm_off)) {
125 struct list_head * entry = dpm_off.next;
126 struct device * dev = to_device(entry);
129 list_move_tail(entry, &dpm_active);
131 mutex_unlock(&dpm_list_mtx);
133 mutex_lock(&dpm_list_mtx);
136 mutex_unlock(&dpm_list_mtx);
141 * device_resume - Restore state of each device in system.
143 * Walk the dpm_off list, remove each entry, resume the device,
144 * then add it to the dpm_active list.
147 void device_resume(void)
150 mutex_lock(&dpm_mtx);
152 mutex_unlock(&dpm_mtx);
155 EXPORT_SYMBOL_GPL(device_resume);
159 * dpm_power_up - Power on some devices.
161 * Walk the dpm_off_irq list and power each device up. This
162 * is used for devices that required they be powered down with
163 * interrupts disabled. As devices are powered on, they are moved
164 * to the dpm_active list.
166 * Interrupts must be disabled when calling this.
169 static void dpm_power_up(void)
171 while(!list_empty(&dpm_off_irq)) {
172 struct list_head * entry = dpm_off_irq.next;
173 struct device * dev = to_device(entry);
175 list_move_tail(entry, &dpm_off);
176 resume_device_early(dev);
182 * device_power_up - Turn on all devices that need special attention.
184 * Power on system devices then devices that required we shut them down
185 * with interrupts disabled.
186 * Called with interrupts disabled.
189 void device_power_up(void)
195 EXPORT_SYMBOL_GPL(device_power_up);
198 /*------------------------- Suspend routines -------------------------*/
201 * The entries in the dpm_active list are in a depth first order, simply
202 * because children are guaranteed to be discovered after parents, and
203 * are inserted at the back of the list on discovery.
205 * All list on the suspend path are done in reverse order, so we operate
206 * on the leaves of the device tree (or forests, depending on how you want
207 * to look at it ;) first. As nodes are removed from the back of the list,
208 * they are inserted into the front of their destintation lists.
210 * Things are the reverse on the resume path - iterations are done in
211 * forward order, and nodes are inserted at the back of their destination
212 * lists. This way, the ancestors will be accessed before their descendents.
215 static inline char *suspend_verb(u32 event)
218 case PM_EVENT_SUSPEND: return "suspend";
219 case PM_EVENT_FREEZE: return "freeze";
220 case PM_EVENT_PRETHAW: return "prethaw";
221 default: return "(unknown suspend event)";
227 suspend_device_dbg(struct device *dev, pm_message_t state, char *info)
229 dev_dbg(dev, "%s%s%s\n", info, suspend_verb(state.event),
230 ((state.event == PM_EVENT_SUSPEND) && device_may_wakeup(dev)) ?
231 ", may wakeup" : "");
235 * suspend_device - Save state of one device.
237 * @state: Power state device is entering.
240 static int suspend_device(struct device * dev, pm_message_t state)
245 if (dev->power.power_state.event) {
246 dev_dbg(dev, "PM: suspend %d-->%d\n",
247 dev->power.power_state.event, state.event);
250 if (dev->class && dev->class->suspend) {
251 suspend_device_dbg(dev, state, "class ");
252 error = dev->class->suspend(dev, state);
253 suspend_report_result(dev->class->suspend, error);
256 if (!error && dev->type && dev->type->suspend) {
257 suspend_device_dbg(dev, state, "type ");
258 error = dev->type->suspend(dev, state);
259 suspend_report_result(dev->type->suspend, error);
262 if (!error && dev->bus && dev->bus->suspend) {
263 suspend_device_dbg(dev, state, "");
264 error = dev->bus->suspend(dev, state);
265 suspend_report_result(dev->bus->suspend, error);
273 * This is called with interrupts off, only a single CPU
274 * running. We can't acquire a mutex or semaphore (and we don't
275 * need the protection)
277 static int suspend_device_late(struct device *dev, pm_message_t state)
281 if (dev->bus && dev->bus->suspend_late) {
282 suspend_device_dbg(dev, state, "LATE ");
283 error = dev->bus->suspend_late(dev, state);
284 suspend_report_result(dev->bus->suspend_late, error);
290 * device_suspend - Save state and stop all devices in system.
291 * @state: Power state to put each device in.
293 * Walk the dpm_active list, call ->suspend() for each device, and move
294 * it to the dpm_off list.
296 * (For historical reasons, if it returns -EAGAIN, that used to mean
297 * that the device would be called again with interrupts disabled.
298 * These days, we use the "suspend_late()" callback for that, so we
299 * print a warning and consider it an error).
301 * If we get a different error, try and back out.
303 * If we hit a failure with any of the devices, call device_resume()
304 * above to bring the suspended devices back to life.
308 int device_suspend(pm_message_t state)
313 mutex_lock(&dpm_mtx);
314 mutex_lock(&dpm_list_mtx);
315 while (!list_empty(&dpm_active) && error == 0) {
316 struct list_head * entry = dpm_active.prev;
317 struct device * dev = to_device(entry);
320 mutex_unlock(&dpm_list_mtx);
322 error = suspend_device(dev, state);
324 mutex_lock(&dpm_list_mtx);
326 /* Check if the device got removed */
327 if (!list_empty(&dev->power.entry)) {
328 /* Move it to the dpm_off list */
330 list_move(&dev->power.entry, &dpm_off);
333 printk(KERN_ERR "Could not suspend device %s: "
335 kobject_name(&dev->kobj), error,
336 error == -EAGAIN ? " (please convert to suspend_late)" : "");
339 mutex_unlock(&dpm_list_mtx);
343 mutex_unlock(&dpm_mtx);
347 EXPORT_SYMBOL_GPL(device_suspend);
350 * device_power_down - Shut down special devices.
351 * @state: Power state to enter.
353 * Walk the dpm_off_irq list, calling ->power_down() for each device that
354 * couldn't power down the device with interrupts enabled. When we're
355 * done, power down system devices.
358 int device_power_down(pm_message_t state)
363 while (!list_empty(&dpm_off)) {
364 struct list_head * entry = dpm_off.prev;
366 dev = to_device(entry);
367 error = suspend_device_late(dev, state);
370 list_move(&dev->power.entry, &dpm_off_irq);
373 error = sysdev_suspend(state);
377 printk(KERN_ERR "Could not power down device %s: "
378 "error %d\n", kobject_name(&dev->kobj), error);
383 EXPORT_SYMBOL_GPL(device_power_down);
385 void __suspend_report_result(const char *function, void *fn, int ret)
388 printk(KERN_ERR "%s(): ", function);
389 print_fn_descriptor_symbol("%s() returns ", (unsigned long)fn);
393 EXPORT_SYMBOL_GPL(__suspend_report_result);