Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / base / power / main.c
blobd887d5cb5bef74496f323a84d94b691ab24a2550
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 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>
25 #include <linux/pm.h>
26 #include <linux/resume-trace.h>
27 #include <linux/rwsem.h>
29 #include "../base.h"
30 #include "power.h"
33 * The entries in the dpm_active list are in a depth first order, simply
34 * because children are guaranteed to be discovered after parents, and
35 * are inserted at the back of the list on discovery.
37 * All the other lists are kept in the same order, for consistency.
38 * However the lists aren't always traversed in the same order.
39 * Semaphores must be acquired from the top (i.e., front) down
40 * and released in the opposite order. Devices must be suspended
41 * from the bottom (i.e., end) up and resumed in the opposite order.
42 * That way no parent will be suspended while it still has an active
43 * child.
45 * Since device_pm_add() may be called with a device semaphore held,
46 * we must never try to acquire a device semaphore while holding
47 * dpm_list_mutex.
50 LIST_HEAD(dpm_active);
51 static LIST_HEAD(dpm_off);
52 static LIST_HEAD(dpm_off_irq);
53 static LIST_HEAD(dpm_destroy);
55 static DEFINE_MUTEX(dpm_list_mtx);
57 static DECLARE_RWSEM(pm_sleep_rwsem);
59 int (*platform_enable_wakeup)(struct device *dev, int is_on);
61 /**
62 * device_pm_add - add a device to the list of active devices
63 * @dev: Device to be added to the list
65 void device_pm_add(struct device *dev)
67 pr_debug("PM: Adding info for %s:%s\n",
68 dev->bus ? dev->bus->name : "No Bus",
69 kobject_name(&dev->kobj));
70 mutex_lock(&dpm_list_mtx);
71 list_add_tail(&dev->power.entry, &dpm_active);
72 mutex_unlock(&dpm_list_mtx);
75 /**
76 * device_pm_remove - remove a device from the list of active devices
77 * @dev: Device to be removed from the list
79 * This function also removes the device's PM-related sysfs attributes.
81 void device_pm_remove(struct device *dev)
83 pr_debug("PM: Removing info for %s:%s\n",
84 dev->bus ? dev->bus->name : "No Bus",
85 kobject_name(&dev->kobj));
86 mutex_lock(&dpm_list_mtx);
87 dpm_sysfs_remove(dev);
88 list_del_init(&dev->power.entry);
89 mutex_unlock(&dpm_list_mtx);
92 /**
93 * device_pm_schedule_removal - schedule the removal of a suspended device
94 * @dev: Device to destroy
96 * Moves the device to the dpm_destroy list for further processing by
97 * unregister_dropped_devices().
99 void device_pm_schedule_removal(struct device *dev)
101 pr_debug("PM: Preparing for removal: %s:%s\n",
102 dev->bus ? dev->bus->name : "No Bus",
103 kobject_name(&dev->kobj));
104 mutex_lock(&dpm_list_mtx);
105 list_move_tail(&dev->power.entry, &dpm_destroy);
106 mutex_unlock(&dpm_list_mtx);
108 EXPORT_SYMBOL_GPL(device_pm_schedule_removal);
111 * pm_sleep_lock - mutual exclusion for registration and suspend
113 * Returns 0 if no suspend is underway and device registration
114 * may proceed, otherwise -EBUSY.
116 int pm_sleep_lock(void)
118 if (down_read_trylock(&pm_sleep_rwsem))
119 return 0;
121 return -EBUSY;
125 * pm_sleep_unlock - mutual exclusion for registration and suspend
127 * This routine undoes the effect of device_pm_add_lock
128 * when a device's registration is complete.
130 void pm_sleep_unlock(void)
132 up_read(&pm_sleep_rwsem);
136 /*------------------------- Resume routines -------------------------*/
139 * resume_device_early - Power on one device (early resume).
140 * @dev: Device.
142 * Must be called with interrupts disabled.
144 static int resume_device_early(struct device *dev)
146 int error = 0;
148 TRACE_DEVICE(dev);
149 TRACE_RESUME(0);
151 if (dev->bus && dev->bus->resume_early) {
152 dev_dbg(dev, "EARLY resume\n");
153 error = dev->bus->resume_early(dev);
156 TRACE_RESUME(error);
157 return error;
161 * dpm_power_up - Power on all regular (non-sysdev) devices.
163 * Walk the dpm_off_irq list and power each device up. This
164 * is used for devices that required they be powered down with
165 * interrupts disabled. As devices are powered on, they are moved
166 * to the dpm_off list.
168 * Must be called with interrupts disabled and only one CPU running.
170 static void dpm_power_up(void)
173 while (!list_empty(&dpm_off_irq)) {
174 struct list_head *entry = dpm_off_irq.next;
175 struct device *dev = to_device(entry);
177 list_move_tail(entry, &dpm_off);
178 resume_device_early(dev);
183 * device_power_up - Turn on all devices that need special attention.
185 * Power on system devices, then devices that required we shut them down
186 * with interrupts disabled.
188 * Must be called with interrupts disabled.
190 void device_power_up(void)
192 sysdev_resume();
193 dpm_power_up();
195 EXPORT_SYMBOL_GPL(device_power_up);
198 * resume_device - Restore state for one device.
199 * @dev: Device.
202 static int resume_device(struct device *dev)
204 int error = 0;
206 TRACE_DEVICE(dev);
207 TRACE_RESUME(0);
209 down(&dev->sem);
211 if (dev->bus && dev->bus->resume) {
212 dev_dbg(dev,"resuming\n");
213 error = dev->bus->resume(dev);
216 if (!error && dev->type && dev->type->resume) {
217 dev_dbg(dev,"resuming\n");
218 error = dev->type->resume(dev);
221 if (!error && dev->class && dev->class->resume) {
222 dev_dbg(dev,"class resume\n");
223 error = dev->class->resume(dev);
226 up(&dev->sem);
228 TRACE_RESUME(error);
229 return error;
233 * dpm_resume - Resume every device.
235 * Resume the devices that have either not gone through
236 * the late suspend, or that did go through it but also
237 * went through the early resume.
239 * Take devices from the dpm_off_list, resume them,
240 * and put them on the dpm_locked list.
242 static void dpm_resume(void)
244 mutex_lock(&dpm_list_mtx);
245 while(!list_empty(&dpm_off)) {
246 struct list_head *entry = dpm_off.next;
247 struct device *dev = to_device(entry);
249 list_move_tail(entry, &dpm_active);
250 mutex_unlock(&dpm_list_mtx);
251 resume_device(dev);
252 mutex_lock(&dpm_list_mtx);
254 mutex_unlock(&dpm_list_mtx);
258 * unregister_dropped_devices - Unregister devices scheduled for removal
260 * Unregister all devices on the dpm_destroy list.
262 static void unregister_dropped_devices(void)
264 mutex_lock(&dpm_list_mtx);
265 while (!list_empty(&dpm_destroy)) {
266 struct list_head *entry = dpm_destroy.next;
267 struct device *dev = to_device(entry);
269 mutex_unlock(&dpm_list_mtx);
270 /* This also removes the device from the list */
271 device_unregister(dev);
272 mutex_lock(&dpm_list_mtx);
274 mutex_unlock(&dpm_list_mtx);
278 * device_resume - Restore state of each device in system.
280 * Resume all the devices, unlock them all, and allow new
281 * devices to be registered once again.
283 void device_resume(void)
285 might_sleep();
286 dpm_resume();
287 unregister_dropped_devices();
288 up_write(&pm_sleep_rwsem);
290 EXPORT_SYMBOL_GPL(device_resume);
293 /*------------------------- Suspend routines -------------------------*/
295 static inline char *suspend_verb(u32 event)
297 switch (event) {
298 case PM_EVENT_SUSPEND: return "suspend";
299 case PM_EVENT_FREEZE: return "freeze";
300 case PM_EVENT_PRETHAW: return "prethaw";
301 default: return "(unknown suspend event)";
305 static void
306 suspend_device_dbg(struct device *dev, pm_message_t state, char *info)
308 dev_dbg(dev, "%s%s%s\n", info, suspend_verb(state.event),
309 ((state.event == PM_EVENT_SUSPEND) && device_may_wakeup(dev)) ?
310 ", may wakeup" : "");
314 * suspend_device_late - Shut down one device (late suspend).
315 * @dev: Device.
316 * @state: Power state device is entering.
318 * This is called with interrupts off and only a single CPU running.
320 static int suspend_device_late(struct device *dev, pm_message_t state)
322 int error = 0;
324 if (dev->bus && dev->bus->suspend_late) {
325 suspend_device_dbg(dev, state, "LATE ");
326 error = dev->bus->suspend_late(dev, state);
327 suspend_report_result(dev->bus->suspend_late, error);
329 return error;
333 * device_power_down - Shut down special devices.
334 * @state: Power state to enter.
336 * Power down devices that require interrupts to be disabled
337 * and move them from the dpm_off list to the dpm_off_irq list.
338 * Then power down system devices.
340 * Must be called with interrupts disabled and only one CPU running.
342 int device_power_down(pm_message_t state)
344 int error = 0;
346 while (!list_empty(&dpm_off)) {
347 struct list_head *entry = dpm_off.prev;
348 struct device *dev = to_device(entry);
350 error = suspend_device_late(dev, state);
351 if (error) {
352 printk(KERN_ERR "Could not power down device %s: "
353 "error %d\n",
354 kobject_name(&dev->kobj), error);
355 break;
357 if (!list_empty(&dev->power.entry))
358 list_move(&dev->power.entry, &dpm_off_irq);
361 if (!error)
362 error = sysdev_suspend(state);
363 if (error)
364 dpm_power_up();
365 return error;
367 EXPORT_SYMBOL_GPL(device_power_down);
370 * suspend_device - Save state of one device.
371 * @dev: Device.
372 * @state: Power state device is entering.
374 static int suspend_device(struct device *dev, pm_message_t state)
376 int error = 0;
378 down(&dev->sem);
380 if (dev->power.power_state.event) {
381 dev_dbg(dev, "PM: suspend %d-->%d\n",
382 dev->power.power_state.event, state.event);
385 if (dev->class && dev->class->suspend) {
386 suspend_device_dbg(dev, state, "class ");
387 error = dev->class->suspend(dev, state);
388 suspend_report_result(dev->class->suspend, error);
391 if (!error && dev->type && dev->type->suspend) {
392 suspend_device_dbg(dev, state, "type ");
393 error = dev->type->suspend(dev, state);
394 suspend_report_result(dev->type->suspend, error);
397 if (!error && dev->bus && dev->bus->suspend) {
398 suspend_device_dbg(dev, state, "");
399 error = dev->bus->suspend(dev, state);
400 suspend_report_result(dev->bus->suspend, error);
403 up(&dev->sem);
405 return error;
409 * dpm_suspend - Suspend every device.
410 * @state: Power state to put each device in.
412 * Walk the dpm_locked list. Suspend each device and move it
413 * to the dpm_off list.
415 * (For historical reasons, if it returns -EAGAIN, that used to mean
416 * that the device would be called again with interrupts disabled.
417 * These days, we use the "suspend_late()" callback for that, so we
418 * print a warning and consider it an error).
420 static int dpm_suspend(pm_message_t state)
422 int error = 0;
424 mutex_lock(&dpm_list_mtx);
425 while (!list_empty(&dpm_active)) {
426 struct list_head *entry = dpm_active.prev;
427 struct device *dev = to_device(entry);
429 mutex_unlock(&dpm_list_mtx);
430 error = suspend_device(dev, state);
431 mutex_lock(&dpm_list_mtx);
432 if (error) {
433 printk(KERN_ERR "Could not suspend device %s: "
434 "error %d%s\n",
435 kobject_name(&dev->kobj),
436 error,
437 (error == -EAGAIN ?
438 " (please convert to suspend_late)" :
439 ""));
440 break;
442 if (!list_empty(&dev->power.entry))
443 list_move(&dev->power.entry, &dpm_off);
445 mutex_unlock(&dpm_list_mtx);
447 return error;
451 * device_suspend - Save state and stop all devices in system.
452 * @state: new power management state
454 * Prevent new devices from being registered, then lock all devices
455 * and suspend them.
457 int device_suspend(pm_message_t state)
459 int error;
461 might_sleep();
462 down_write(&pm_sleep_rwsem);
463 error = dpm_suspend(state);
464 if (error)
465 device_resume();
466 return error;
468 EXPORT_SYMBOL_GPL(device_suspend);
470 void __suspend_report_result(const char *function, void *fn, int ret)
472 if (ret) {
473 printk(KERN_ERR "%s(): ", function);
474 print_fn_descriptor_symbol("%s() returns ", (unsigned long)fn);
475 printk("%d\n", ret);
478 EXPORT_SYMBOL_GPL(__suspend_report_result);