]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/base/power/main.c
c4568b82875b9fb6379f3c650175143c2ebc6e81
[linux-2.6-omap-h63xx.git] / drivers / base / power / main.c
1 /*
2  * drivers/base/power/main.c - Where the driver meets power management.
3  *
4  * Copyright (c) 2003 Patrick Mochel
5  * Copyright (c) 2003 Open Source Development Lab
6  *
7  * This file is released under the GPLv2
8  *
9  *
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.
14  *
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.
20  */
21
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>
28
29 #include "../base.h"
30 #include "power.h"
31
32 /*
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.
36  *
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.
44  *
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.
48  */
49
50 LIST_HEAD(dpm_active);
51 static LIST_HEAD(dpm_off);
52 static LIST_HEAD(dpm_off_irq);
53
54 static DEFINE_MUTEX(dpm_list_mtx);
55
56 /* 'true' if all devices have been suspended, protected by dpm_list_mtx */
57 static bool all_sleeping;
58
59 /**
60  *      device_pm_add - add a device to the list of active devices
61  *      @dev:   Device to be added to the list
62  */
63 int device_pm_add(struct device *dev)
64 {
65         int error = 0;
66
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         if ((dev->parent && dev->parent->power.sleeping) || all_sleeping) {
72                 if (dev->parent->power.sleeping)
73                         dev_warn(dev,
74                                 "parent %s is sleeping, will not add\n",
75                                 dev->parent->bus_id);
76                 else
77                         dev_warn(dev, "devices are sleeping, will not add\n");
78                 WARN_ON(true);
79                 error = -EBUSY;
80         } else {
81                 error = dpm_sysfs_add(dev);
82                 if (!error)
83                         list_add_tail(&dev->power.entry, &dpm_active);
84         }
85         mutex_unlock(&dpm_list_mtx);
86         return error;
87 }
88
89 /**
90  *      device_pm_remove - remove a device from the list of active devices
91  *      @dev:   Device to be removed from the list
92  *
93  *      This function also removes the device's PM-related sysfs attributes.
94  */
95 void device_pm_remove(struct device *dev)
96 {
97         pr_debug("PM: Removing info for %s:%s\n",
98                  dev->bus ? dev->bus->name : "No Bus",
99                  kobject_name(&dev->kobj));
100         mutex_lock(&dpm_list_mtx);
101         dpm_sysfs_remove(dev);
102         list_del_init(&dev->power.entry);
103         mutex_unlock(&dpm_list_mtx);
104 }
105
106 /*------------------------- Resume routines -------------------------*/
107
108 /**
109  *      resume_device_early - Power on one device (early resume).
110  *      @dev:   Device.
111  *
112  *      Must be called with interrupts disabled.
113  */
114 static int resume_device_early(struct device *dev)
115 {
116         int error = 0;
117
118         TRACE_DEVICE(dev);
119         TRACE_RESUME(0);
120
121         if (dev->bus && dev->bus->resume_early) {
122                 dev_dbg(dev, "EARLY resume\n");
123                 error = dev->bus->resume_early(dev);
124         }
125
126         TRACE_RESUME(error);
127         return error;
128 }
129
130 /**
131  *      dpm_power_up - Power on all regular (non-sysdev) devices.
132  *
133  *      Walk the dpm_off_irq list and power each device up. This
134  *      is used for devices that required they be powered down with
135  *      interrupts disabled. As devices are powered on, they are moved
136  *      to the dpm_off list.
137  *
138  *      Must be called with interrupts disabled and only one CPU running.
139  */
140 static void dpm_power_up(void)
141 {
142
143         while (!list_empty(&dpm_off_irq)) {
144                 struct list_head *entry = dpm_off_irq.next;
145                 struct device *dev = to_device(entry);
146
147                 list_move_tail(entry, &dpm_off);
148                 resume_device_early(dev);
149         }
150 }
151
152 /**
153  *      device_power_up - Turn on all devices that need special attention.
154  *
155  *      Power on system devices, then devices that required we shut them down
156  *      with interrupts disabled.
157  *
158  *      Must be called with interrupts disabled.
159  */
160 void device_power_up(void)
161 {
162         sysdev_resume();
163         dpm_power_up();
164 }
165 EXPORT_SYMBOL_GPL(device_power_up);
166
167 /**
168  *      resume_device - Restore state for one device.
169  *      @dev:   Device.
170  *
171  */
172 static int resume_device(struct device *dev)
173 {
174         int error = 0;
175
176         TRACE_DEVICE(dev);
177         TRACE_RESUME(0);
178
179         down(&dev->sem);
180
181         if (dev->bus && dev->bus->resume) {
182                 dev_dbg(dev,"resuming\n");
183                 error = dev->bus->resume(dev);
184         }
185
186         if (!error && dev->type && dev->type->resume) {
187                 dev_dbg(dev,"resuming\n");
188                 error = dev->type->resume(dev);
189         }
190
191         if (!error && dev->class && dev->class->resume) {
192                 dev_dbg(dev,"class resume\n");
193                 error = dev->class->resume(dev);
194         }
195
196         up(&dev->sem);
197
198         TRACE_RESUME(error);
199         return error;
200 }
201
202 /**
203  *      dpm_resume - Resume every device.
204  *
205  *      Resume the devices that have either not gone through
206  *      the late suspend, or that did go through it but also
207  *      went through the early resume.
208  *
209  *      Take devices from the dpm_off_list, resume them,
210  *      and put them on the dpm_locked list.
211  */
212 static void dpm_resume(void)
213 {
214         mutex_lock(&dpm_list_mtx);
215         all_sleeping = false;
216         while(!list_empty(&dpm_off)) {
217                 struct list_head *entry = dpm_off.next;
218                 struct device *dev = to_device(entry);
219
220                 list_move_tail(entry, &dpm_active);
221                 dev->power.sleeping = false;
222                 mutex_unlock(&dpm_list_mtx);
223                 resume_device(dev);
224                 mutex_lock(&dpm_list_mtx);
225         }
226         mutex_unlock(&dpm_list_mtx);
227 }
228
229 /**
230  *      device_resume - Restore state of each device in system.
231  *
232  *      Resume all the devices, unlock them all, and allow new
233  *      devices to be registered once again.
234  */
235 void device_resume(void)
236 {
237         might_sleep();
238         dpm_resume();
239 }
240 EXPORT_SYMBOL_GPL(device_resume);
241
242
243 /*------------------------- Suspend routines -------------------------*/
244
245 static inline char *suspend_verb(u32 event)
246 {
247         switch (event) {
248         case PM_EVENT_SUSPEND:  return "suspend";
249         case PM_EVENT_FREEZE:   return "freeze";
250         case PM_EVENT_PRETHAW:  return "prethaw";
251         default:                return "(unknown suspend event)";
252         }
253 }
254
255 static void
256 suspend_device_dbg(struct device *dev, pm_message_t state, char *info)
257 {
258         dev_dbg(dev, "%s%s%s\n", info, suspend_verb(state.event),
259                 ((state.event == PM_EVENT_SUSPEND) && device_may_wakeup(dev)) ?
260                 ", may wakeup" : "");
261 }
262
263 /**
264  *      suspend_device_late - Shut down one device (late suspend).
265  *      @dev:   Device.
266  *      @state: Power state device is entering.
267  *
268  *      This is called with interrupts off and only a single CPU running.
269  */
270 static int suspend_device_late(struct device *dev, pm_message_t state)
271 {
272         int error = 0;
273
274         if (dev->bus && dev->bus->suspend_late) {
275                 suspend_device_dbg(dev, state, "LATE ");
276                 error = dev->bus->suspend_late(dev, state);
277                 suspend_report_result(dev->bus->suspend_late, error);
278         }
279         return error;
280 }
281
282 /**
283  *      device_power_down - Shut down special devices.
284  *      @state:         Power state to enter.
285  *
286  *      Power down devices that require interrupts to be disabled
287  *      and move them from the dpm_off list to the dpm_off_irq list.
288  *      Then power down system devices.
289  *
290  *      Must be called with interrupts disabled and only one CPU running.
291  */
292 int device_power_down(pm_message_t state)
293 {
294         int error = 0;
295
296         while (!list_empty(&dpm_off)) {
297                 struct list_head *entry = dpm_off.prev;
298                 struct device *dev = to_device(entry);
299
300                 error = suspend_device_late(dev, state);
301                 if (error) {
302                         printk(KERN_ERR "Could not power down device %s: "
303                                         "error %d\n",
304                                         kobject_name(&dev->kobj), error);
305                         break;
306                 }
307                 if (!list_empty(&dev->power.entry))
308                         list_move(&dev->power.entry, &dpm_off_irq);
309         }
310
311         if (!error)
312                 error = sysdev_suspend(state);
313         if (error)
314                 dpm_power_up();
315         return error;
316 }
317 EXPORT_SYMBOL_GPL(device_power_down);
318
319 /**
320  *      suspend_device - Save state of one device.
321  *      @dev:   Device.
322  *      @state: Power state device is entering.
323  */
324 static int suspend_device(struct device *dev, pm_message_t state)
325 {
326         int error = 0;
327
328         down(&dev->sem);
329
330         if (dev->class && dev->class->suspend) {
331                 suspend_device_dbg(dev, state, "class ");
332                 error = dev->class->suspend(dev, state);
333                 suspend_report_result(dev->class->suspend, error);
334         }
335
336         if (!error && dev->type && dev->type->suspend) {
337                 suspend_device_dbg(dev, state, "type ");
338                 error = dev->type->suspend(dev, state);
339                 suspend_report_result(dev->type->suspend, error);
340         }
341
342         if (!error && dev->bus && dev->bus->suspend) {
343                 suspend_device_dbg(dev, state, "");
344                 error = dev->bus->suspend(dev, state);
345                 suspend_report_result(dev->bus->suspend, error);
346         }
347
348         up(&dev->sem);
349
350         return error;
351 }
352
353 /**
354  *      dpm_suspend - Suspend every device.
355  *      @state: Power state to put each device in.
356  *
357  *      Walk the dpm_locked list.  Suspend each device and move it
358  *      to the dpm_off list.
359  *
360  *      (For historical reasons, if it returns -EAGAIN, that used to mean
361  *      that the device would be called again with interrupts disabled.
362  *      These days, we use the "suspend_late()" callback for that, so we
363  *      print a warning and consider it an error).
364  */
365 static int dpm_suspend(pm_message_t state)
366 {
367         int error = 0;
368
369         mutex_lock(&dpm_list_mtx);
370         while (!list_empty(&dpm_active)) {
371                 struct list_head *entry = dpm_active.prev;
372                 struct device *dev = to_device(entry);
373
374                 WARN_ON(dev->parent && dev->parent->power.sleeping);
375
376                 dev->power.sleeping = true;
377                 mutex_unlock(&dpm_list_mtx);
378                 error = suspend_device(dev, state);
379                 mutex_lock(&dpm_list_mtx);
380                 if (error) {
381                         printk(KERN_ERR "Could not suspend device %s: "
382                                         "error %d%s\n",
383                                         kobject_name(&dev->kobj),
384                                         error,
385                                         (error == -EAGAIN ?
386                                         " (please convert to suspend_late)" :
387                                         ""));
388                         dev->power.sleeping = false;
389                         break;
390                 }
391                 if (!list_empty(&dev->power.entry))
392                         list_move(&dev->power.entry, &dpm_off);
393         }
394         if (!error)
395                 all_sleeping = true;
396         mutex_unlock(&dpm_list_mtx);
397
398         return error;
399 }
400
401 /**
402  *      device_suspend - Save state and stop all devices in system.
403  *      @state: new power management state
404  *
405  *      Prevent new devices from being registered, then lock all devices
406  *      and suspend them.
407  */
408 int device_suspend(pm_message_t state)
409 {
410         int error;
411
412         might_sleep();
413         error = dpm_suspend(state);
414         if (error)
415                 device_resume();
416         return error;
417 }
418 EXPORT_SYMBOL_GPL(device_suspend);
419
420 void __suspend_report_result(const char *function, void *fn, int ret)
421 {
422         if (ret) {
423                 printk(KERN_ERR "%s(): ", function);
424                 print_fn_descriptor_symbol("%s() returns ", (unsigned long)fn);
425                 printk("%d\n", ret);
426         }
427 }
428 EXPORT_SYMBOL_GPL(__suspend_report_result);