blob: 1eca50c8e7ca36dab412e1c455c245022d1ee1da [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/base/power/sysfs.c - sysfs entries for device PM
3 */
4
5#include <linux/device.h>
Tim Schmielau8c65b4a2005-11-07 00:59:43 -08006#include <linux/string.h>
Rafael J. Wysocki53823632010-01-23 22:02:51 +01007#include <linux/pm_runtime.h>
Dominik Brodowskic92445f2010-04-23 20:32:23 +02008#include <asm/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include "power.h"
10
David Brownell0ac85242005-09-12 19:39:34 -070011/*
Rafael J. Wysocki53823632010-01-23 22:02:51 +010012 * 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 *
David Brownell0ac85242005-09-12 19:39:34 -070027 * 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.
Rafael J. Wysocki5a2eb852010-01-23 22:25:23 +010058 *
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.
Rafael J. Wysockic125e962010-07-05 22:43:53 +020076 *
77 * wakeup_count - Report the number of wakeup events related to the device
David Brownell0ac85242005-09-12 19:39:34 -070078 */
79
80static const char enabled[] = "enabled";
81static const char disabled[] = "disabled";
82
Rafael J. Wysocki53823632010-01-23 22:02:51 +010083#ifdef CONFIG_PM_RUNTIME
84static const char ctrl_auto[] = "auto";
85static const char ctrl_on[] = "on";
86
87static 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
94static 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
112static DEVICE_ATTR(control, 0644, control_show, control_store);
Alan Stern0fcb4ee2010-07-08 00:05:37 +0200113
114static ssize_t rtpm_status_show(struct device *dev,
115 struct device_attribute *attr, char *buf)
116{
117 const char *p;
118
119 if (dev->power.runtime_error) {
120 p = "error\n";
121 } else if (dev->power.disable_depth) {
122 p = "unsupported\n";
123 } else {
124 switch (dev->power.runtime_status) {
125 case RPM_SUSPENDED:
126 p = "suspended\n";
127 break;
128 case RPM_SUSPENDING:
129 p = "suspending\n";
130 break;
131 case RPM_RESUMING:
132 p = "resuming\n";
133 break;
134 case RPM_ACTIVE:
135 p = "active\n";
136 break;
137 default:
138 return -EIO;
139 }
140 }
141 return sprintf(buf, p);
142}
143
144static DEVICE_ATTR(runtime_status, 0444, rtpm_status_show, NULL);
Rafael J. Wysocki53823632010-01-23 22:02:51 +0100145#endif
146
David Brownell0ac85242005-09-12 19:39:34 -0700147static ssize_t
148wake_show(struct device * dev, struct device_attribute *attr, char * buf)
149{
150 return sprintf(buf, "%s\n", device_can_wakeup(dev)
151 ? (device_may_wakeup(dev) ? enabled : disabled)
152 : "");
153}
154
155static ssize_t
156wake_store(struct device * dev, struct device_attribute *attr,
157 const char * buf, size_t n)
158{
159 char *cp;
160 int len = n;
161
162 if (!device_can_wakeup(dev))
163 return -EINVAL;
164
165 cp = memchr(buf, '\n', n);
166 if (cp)
167 len = cp - buf;
168 if (len == sizeof enabled - 1
169 && strncmp(buf, enabled, sizeof enabled - 1) == 0)
170 device_set_wakeup_enable(dev, 1);
171 else if (len == sizeof disabled - 1
172 && strncmp(buf, disabled, sizeof disabled - 1) == 0)
173 device_set_wakeup_enable(dev, 0);
174 else
175 return -EINVAL;
176 return n;
177}
178
179static DEVICE_ATTR(wakeup, 0644, wake_show, wake_store);
180
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200181#ifdef CONFIG_PM_SLEEP
182static ssize_t wakeup_count_show(struct device *dev,
183 struct device_attribute *attr, char *buf)
184{
185 return sprintf(buf, "%lu\n", dev->power.wakeup_count);
186}
187
188static DEVICE_ATTR(wakeup_count, 0444, wakeup_count_show, NULL);
189#endif
190
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200191#ifdef CONFIG_PM_ADVANCED_DEBUG
192#ifdef CONFIG_PM_RUNTIME
193
194static ssize_t rtpm_usagecount_show(struct device *dev,
195 struct device_attribute *attr, char *buf)
196{
197 return sprintf(buf, "%d\n", atomic_read(&dev->power.usage_count));
198}
199
200static ssize_t rtpm_children_show(struct device *dev,
201 struct device_attribute *attr, char *buf)
202{
203 return sprintf(buf, "%d\n", dev->power.ignore_children ?
204 0 : atomic_read(&dev->power.child_count));
205}
206
207static ssize_t rtpm_enabled_show(struct device *dev,
208 struct device_attribute *attr, char *buf)
209{
210 if ((dev->power.disable_depth) && (dev->power.runtime_auto == false))
211 return sprintf(buf, "disabled & forbidden\n");
212 else if (dev->power.disable_depth)
213 return sprintf(buf, "disabled\n");
214 else if (dev->power.runtime_auto == false)
215 return sprintf(buf, "forbidden\n");
216 return sprintf(buf, "enabled\n");
217}
218
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200219static DEVICE_ATTR(runtime_usage, 0444, rtpm_usagecount_show, NULL);
220static DEVICE_ATTR(runtime_active_kids, 0444, rtpm_children_show, NULL);
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200221static DEVICE_ATTR(runtime_enabled, 0444, rtpm_enabled_show, NULL);
222
223#endif
224
Rafael J. Wysocki5a2eb852010-01-23 22:25:23 +0100225static ssize_t async_show(struct device *dev, struct device_attribute *attr,
226 char *buf)
227{
228 return sprintf(buf, "%s\n",
229 device_async_suspend_enabled(dev) ? enabled : disabled);
230}
231
232static ssize_t async_store(struct device *dev, struct device_attribute *attr,
233 const char *buf, size_t n)
234{
235 char *cp;
236 int len = n;
237
238 cp = memchr(buf, '\n', n);
239 if (cp)
240 len = cp - buf;
241 if (len == sizeof enabled - 1 && strncmp(buf, enabled, len) == 0)
242 device_enable_async_suspend(dev);
243 else if (len == sizeof disabled - 1 && strncmp(buf, disabled, len) == 0)
244 device_disable_async_suspend(dev);
245 else
246 return -EINVAL;
247 return n;
248}
249
250static DEVICE_ATTR(async, 0644, async_show, async_store);
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200251#endif /* CONFIG_PM_ADVANCED_DEBUG */
David Brownell0ac85242005-09-12 19:39:34 -0700252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253static struct attribute * power_attrs[] = {
Rafael J. Wysocki53823632010-01-23 22:02:51 +0100254#ifdef CONFIG_PM_RUNTIME
255 &dev_attr_control.attr,
Alan Stern0fcb4ee2010-07-08 00:05:37 +0200256 &dev_attr_runtime_status.attr,
Rafael J. Wysocki53823632010-01-23 22:02:51 +0100257#endif
David Brownell0ac85242005-09-12 19:39:34 -0700258 &dev_attr_wakeup.attr,
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200259#ifdef CONFIG_PM_SLEEP
260 &dev_attr_wakeup_count.attr,
261#endif
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200262#ifdef CONFIG_PM_ADVANCED_DEBUG
Rafael J. Wysocki5a2eb852010-01-23 22:25:23 +0100263 &dev_attr_async.attr,
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200264#ifdef CONFIG_PM_RUNTIME
265 &dev_attr_runtime_usage.attr,
266 &dev_attr_runtime_active_kids.attr,
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200267 &dev_attr_runtime_enabled.attr,
268#endif
Rafael J. Wysocki5a2eb852010-01-23 22:25:23 +0100269#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 NULL,
271};
272static struct attribute_group pm_attr_group = {
273 .name = "power",
274 .attrs = power_attrs,
275};
276
277int dpm_sysfs_add(struct device * dev)
278{
279 return sysfs_create_group(&dev->kobj, &pm_attr_group);
280}
281
282void dpm_sysfs_remove(struct device * dev)
283{
284 sysfs_remove_group(&dev->kobj, &pm_attr_group);
285}