81d344e0e95dbb7d368452f1278d3ec62c348fb7
[pandora-kernel.git] / drivers / base / power / sysfs.c
1 /*
2  * drivers/base/power/sysfs.c - sysfs entries for device PM
3  */
4
5 #include <linux/device.h>
6 #include <linux/string.h>
7 #include <linux/pm_runtime.h>
8 #include <asm/atomic.h>
9 #include "power.h"
10
11 /*
12  *      control - Report/change current runtime PM setting of the device
13  *
14  *      Runtime power management of a device can be blocked with the help of
15  *      this attribute.  All devices have one of the following two values for
16  *      the power/control file:
17  *
18  *       + "auto\n" to allow the device to be power managed at run time;
19  *       + "on\n" to prevent the device from being power managed at run time;
20  *
21  *      The default for all devices is "auto", which means that devices may be
22  *      subject to automatic power management, depending on their drivers.
23  *      Changing this attribute to "on" prevents the driver from power managing
24  *      the device at run time.  Doing that while the device is suspended causes
25  *      it to be woken up.
26  *
27  *      wakeup - Report/change current wakeup option for device
28  *
29  *      Some devices support "wakeup" events, which are hardware signals
30  *      used to activate devices from suspended or low power states.  Such
31  *      devices have one of three values for the sysfs power/wakeup file:
32  *
33  *       + "enabled\n" to issue the events;
34  *       + "disabled\n" not to do so; or
35  *       + "\n" for temporary or permanent inability to issue wakeup.
36  *
37  *      (For example, unconfigured USB devices can't issue wakeups.)
38  *
39  *      Familiar examples of devices that can issue wakeup events include
40  *      keyboards and mice (both PS2 and USB styles), power buttons, modems,
41  *      "Wake-On-LAN" Ethernet links, GPIO lines, and more.  Some events
42  *      will wake the entire system from a suspend state; others may just
43  *      wake up the device (if the system as a whole is already active).
44  *      Some wakeup events use normal IRQ lines; other use special out
45  *      of band signaling.
46  *
47  *      It is the responsibility of device drivers to enable (or disable)
48  *      wakeup signaling as part of changing device power states, respecting
49  *      the policy choices provided through the driver model.
50  *
51  *      Devices may not be able to generate wakeup events from all power
52  *      states.  Also, the events may be ignored in some configurations;
53  *      for example, they might need help from other devices that aren't
54  *      active, or which may have wakeup disabled.  Some drivers rely on
55  *      wakeup events internally (unless they are disabled), keeping
56  *      their hardware in low power modes whenever they're unused.  This
57  *      saves runtime power, without requiring system-wide sleep states.
58  *
59  *      async - Report/change current async suspend setting for the device
60  *
61  *      Asynchronous suspend and resume of the device during system-wide power
62  *      state transitions can be enabled by writing "enabled" to this file.
63  *      Analogously, if "disabled" is written to this file, the device will be
64  *      suspended and resumed synchronously.
65  *
66  *      All devices have one of the following two values for power/async:
67  *
68  *       + "enabled\n" to permit the asynchronous suspend/resume of the device;
69  *       + "disabled\n" to forbid it;
70  *
71  *      NOTE: It generally is unsafe to permit the asynchronous suspend/resume
72  *      of a device unless it is certain that all of the PM dependencies of the
73  *      device are known to the PM core.  However, for some devices this
74  *      attribute is set to "enabled" by bus type code or device drivers and in
75  *      that cases it should be safe to leave the default value.
76  *
77  *      wakeup_count - Report the number of wakeup events related to the device
78  */
79
80 static const char enabled[] = "enabled";
81 static const char disabled[] = "disabled";
82
83 #ifdef CONFIG_PM_RUNTIME
84 static const char ctrl_auto[] = "auto";
85 static const char ctrl_on[] = "on";
86
87 static ssize_t control_show(struct device *dev, struct device_attribute *attr,
88                             char *buf)
89 {
90         return sprintf(buf, "%s\n",
91                                 dev->power.runtime_auto ? ctrl_auto : ctrl_on);
92 }
93
94 static ssize_t control_store(struct device * dev, struct device_attribute *attr,
95                              const char * buf, size_t n)
96 {
97         char *cp;
98         int len = n;
99
100         cp = memchr(buf, '\n', n);
101         if (cp)
102                 len = cp - buf;
103         if (len == sizeof ctrl_auto - 1 && strncmp(buf, ctrl_auto, len) == 0)
104                 pm_runtime_allow(dev);
105         else if (len == sizeof ctrl_on - 1 && strncmp(buf, ctrl_on, len) == 0)
106                 pm_runtime_forbid(dev);
107         else
108                 return -EINVAL;
109         return n;
110 }
111
112 static DEVICE_ATTR(control, 0644, control_show, control_store);
113 #endif
114
115 static ssize_t
116 wake_show(struct device * dev, struct device_attribute *attr, char * buf)
117 {
118         return sprintf(buf, "%s\n", device_can_wakeup(dev)
119                 ? (device_may_wakeup(dev) ? enabled : disabled)
120                 : "");
121 }
122
123 static ssize_t
124 wake_store(struct device * dev, struct device_attribute *attr,
125         const char * buf, size_t n)
126 {
127         char *cp;
128         int len = n;
129
130         if (!device_can_wakeup(dev))
131                 return -EINVAL;
132
133         cp = memchr(buf, '\n', n);
134         if (cp)
135                 len = cp - buf;
136         if (len == sizeof enabled - 1
137                         && strncmp(buf, enabled, sizeof enabled - 1) == 0)
138                 device_set_wakeup_enable(dev, 1);
139         else if (len == sizeof disabled - 1
140                         && strncmp(buf, disabled, sizeof disabled - 1) == 0)
141                 device_set_wakeup_enable(dev, 0);
142         else
143                 return -EINVAL;
144         return n;
145 }
146
147 static DEVICE_ATTR(wakeup, 0644, wake_show, wake_store);
148
149 #ifdef CONFIG_PM_SLEEP
150 static ssize_t wakeup_count_show(struct device *dev,
151                                 struct device_attribute *attr, char *buf)
152 {
153         return sprintf(buf, "%lu\n", dev->power.wakeup_count);
154 }
155
156 static DEVICE_ATTR(wakeup_count, 0444, wakeup_count_show, NULL);
157 #endif
158
159 #ifdef CONFIG_PM_ADVANCED_DEBUG
160 #ifdef CONFIG_PM_RUNTIME
161
162 static ssize_t rtpm_usagecount_show(struct device *dev,
163                                     struct device_attribute *attr, char *buf)
164 {
165         return sprintf(buf, "%d\n", atomic_read(&dev->power.usage_count));
166 }
167
168 static ssize_t rtpm_children_show(struct device *dev,
169                                   struct device_attribute *attr, char *buf)
170 {
171         return sprintf(buf, "%d\n", dev->power.ignore_children ?
172                 0 : atomic_read(&dev->power.child_count));
173 }
174
175 static ssize_t rtpm_enabled_show(struct device *dev,
176                                  struct device_attribute *attr, char *buf)
177 {
178         if ((dev->power.disable_depth) && (dev->power.runtime_auto == false))
179                 return sprintf(buf, "disabled & forbidden\n");
180         else if (dev->power.disable_depth)
181                 return sprintf(buf, "disabled\n");
182         else if (dev->power.runtime_auto == false)
183                 return sprintf(buf, "forbidden\n");
184         return sprintf(buf, "enabled\n");
185 }
186
187 static ssize_t rtpm_status_show(struct device *dev,
188                                 struct device_attribute *attr, char *buf)
189 {
190         if (dev->power.runtime_error)
191                 return sprintf(buf, "error\n");
192         switch (dev->power.runtime_status) {
193         case RPM_SUSPENDED:
194                 return sprintf(buf, "suspended\n");
195         case RPM_SUSPENDING:
196                 return sprintf(buf, "suspending\n");
197         case RPM_RESUMING:
198                 return sprintf(buf, "resuming\n");
199         case RPM_ACTIVE:
200                 return sprintf(buf, "active\n");
201         }
202         return -EIO;
203 }
204
205 static DEVICE_ATTR(runtime_usage, 0444, rtpm_usagecount_show, NULL);
206 static DEVICE_ATTR(runtime_active_kids, 0444, rtpm_children_show, NULL);
207 static DEVICE_ATTR(runtime_status, 0444, rtpm_status_show, NULL);
208 static DEVICE_ATTR(runtime_enabled, 0444, rtpm_enabled_show, NULL);
209
210 #endif
211
212 static ssize_t async_show(struct device *dev, struct device_attribute *attr,
213                           char *buf)
214 {
215         return sprintf(buf, "%s\n",
216                         device_async_suspend_enabled(dev) ? enabled : disabled);
217 }
218
219 static ssize_t async_store(struct device *dev, struct device_attribute *attr,
220                            const char *buf, size_t n)
221 {
222         char *cp;
223         int len = n;
224
225         cp = memchr(buf, '\n', n);
226         if (cp)
227                 len = cp - buf;
228         if (len == sizeof enabled - 1 && strncmp(buf, enabled, len) == 0)
229                 device_enable_async_suspend(dev);
230         else if (len == sizeof disabled - 1 && strncmp(buf, disabled, len) == 0)
231                 device_disable_async_suspend(dev);
232         else
233                 return -EINVAL;
234         return n;
235 }
236
237 static DEVICE_ATTR(async, 0644, async_show, async_store);
238 #endif /* CONFIG_PM_ADVANCED_DEBUG */
239
240 static struct attribute * power_attrs[] = {
241 #ifdef CONFIG_PM_RUNTIME
242         &dev_attr_control.attr,
243 #endif
244         &dev_attr_wakeup.attr,
245 #ifdef CONFIG_PM_SLEEP
246         &dev_attr_wakeup_count.attr,
247 #endif
248 #ifdef CONFIG_PM_ADVANCED_DEBUG
249         &dev_attr_async.attr,
250 #ifdef CONFIG_PM_RUNTIME
251         &dev_attr_runtime_usage.attr,
252         &dev_attr_runtime_active_kids.attr,
253         &dev_attr_runtime_status.attr,
254         &dev_attr_runtime_enabled.attr,
255 #endif
256 #endif
257         NULL,
258 };
259 static struct attribute_group pm_attr_group = {
260         .name   = "power",
261         .attrs  = power_attrs,
262 };
263
264 int dpm_sysfs_add(struct device * dev)
265 {
266         return sysfs_create_group(&dev->kobj, &pm_attr_group);
267 }
268
269 void dpm_sysfs_remove(struct device * dev)
270 {
271         sysfs_remove_group(&dev->kobj, &pm_attr_group);
272 }