ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.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/config.h>
23 #include <linux/device.h>
24 #include "power.h"
25
26 LIST_HEAD(dpm_active);
27 LIST_HEAD(dpm_off);
28 LIST_HEAD(dpm_off_irq);
29
30 DECLARE_MUTEX(dpm_sem);
31
32 /*
33  * PM Reference Counting.
34  */
35
36 static inline void device_pm_hold(struct device * dev)
37 {
38         if (dev)
39                 atomic_inc(&dev->power.pm_users);
40 }
41
42 static inline void device_pm_release(struct device * dev)
43 {
44         if (dev)
45                 atomic_dec(&dev->power.pm_users);
46 }
47
48
49 /**
50  *      device_pm_set_parent - Specify power dependency.
51  *      @dev:           Device who needs power.
52  *      @parent:        Device that supplies power.
53  *
54  *      This function is used to manually describe a power-dependency
55  *      relationship. It may be used to specify a transversal relationship
56  *      (where the power supplier is not the physical (or electrical)
57  *      ancestor of a specific device.
58  *      The effect of this is that the supplier will not be powered down
59  *      before the power dependent.
60  */
61
62 void device_pm_set_parent(struct device * dev, struct device * parent)
63 {
64         struct device * old_parent = dev->power.pm_parent;
65         device_pm_release(old_parent);
66         dev->power.pm_parent = parent;
67         device_pm_hold(parent);
68 }
69 EXPORT_SYMBOL(device_pm_set_parent);
70
71 int device_pm_add(struct device * dev)
72 {
73         int error;
74
75         pr_debug("PM: Adding info for %s:%s\n",
76                  dev->bus ? dev->bus->name : "No Bus", dev->kobj.name);
77         atomic_set(&dev->power.pm_users,0);
78         down(&dpm_sem);
79         list_add_tail(&dev->power.entry,&dpm_active);
80         device_pm_set_parent(dev,dev->parent);
81         if ((error = dpm_sysfs_add(dev)))
82                 list_del(&dev->power.entry);
83         up(&dpm_sem);
84         return error;
85 }
86
87 void device_pm_remove(struct device * dev)
88 {
89         pr_debug("PM: Removing info for %s:%s\n",
90                  dev->bus ? dev->bus->name : "No Bus", dev->kobj.name);
91         down(&dpm_sem);
92         dpm_sysfs_remove(dev);
93         device_pm_release(dev->power.pm_parent);
94         list_del(&dev->power.entry);
95         up(&dpm_sem);
96 }
97
98