2 * drivers/base/power/sysfs.c - sysfs entries for device PM
5 #include <linux/device.h>
6 #include <linux/string.h>
7 #include <linux/pm_runtime.h>
8 #include <asm/atomic.h>
12 * control - Report/change current runtime PM setting of the device
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:
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;
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
27 * wakeup - Report/change current wakeup option for device
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:
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.
37 * (For example, unconfigured USB devices can't issue wakeups.)
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
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.
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.
59 * async - Report/change current async suspend setting for the device
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.
66 * All devices have one of the following two values for power/async:
68 * + "enabled\n" to permit the asynchronous suspend/resume of the device;
69 * + "disabled\n" to forbid it;
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.
77 * wakeup_count - Report the number of wakeup events related to the device
80 static const char enabled[] = "enabled";
81 static const char disabled[] = "disabled";
83 #ifdef CONFIG_PM_RUNTIME
84 static const char ctrl_auto[] = "auto";
85 static const char ctrl_on[] = "on";
87 static ssize_t control_show(struct device *dev, struct device_attribute *attr,
90 return sprintf(buf, "%s\n",
91 dev->power.runtime_auto ? ctrl_auto : ctrl_on);
94 static ssize_t control_store(struct device * dev, struct device_attribute *attr,
95 const char * buf, size_t n)
100 cp = memchr(buf, '\n', n);
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);
112 static DEVICE_ATTR(control, 0644, control_show, control_store);
116 wake_show(struct device * dev, struct device_attribute *attr, char * buf)
118 return sprintf(buf, "%s\n", device_can_wakeup(dev)
119 ? (device_may_wakeup(dev) ? enabled : disabled)
124 wake_store(struct device * dev, struct device_attribute *attr,
125 const char * buf, size_t n)
130 if (!device_can_wakeup(dev))
133 cp = memchr(buf, '\n', n);
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);
147 static DEVICE_ATTR(wakeup, 0644, wake_show, wake_store);
149 #ifdef CONFIG_PM_SLEEP
150 static ssize_t wakeup_count_show(struct device *dev,
151 struct device_attribute *attr, char *buf)
153 return sprintf(buf, "%lu\n", dev->power.wakeup_count);
156 static DEVICE_ATTR(wakeup_count, 0444, wakeup_count_show, NULL);
159 #ifdef CONFIG_PM_ADVANCED_DEBUG
160 #ifdef CONFIG_PM_RUNTIME
162 static ssize_t rtpm_usagecount_show(struct device *dev,
163 struct device_attribute *attr, char *buf)
165 return sprintf(buf, "%d\n", atomic_read(&dev->power.usage_count));
168 static ssize_t rtpm_children_show(struct device *dev,
169 struct device_attribute *attr, char *buf)
171 return sprintf(buf, "%d\n", dev->power.ignore_children ?
172 0 : atomic_read(&dev->power.child_count));
175 static ssize_t rtpm_enabled_show(struct device *dev,
176 struct device_attribute *attr, char *buf)
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");
187 static ssize_t rtpm_status_show(struct device *dev,
188 struct device_attribute *attr, char *buf)
190 if (dev->power.runtime_error)
191 return sprintf(buf, "error\n");
192 switch (dev->power.runtime_status) {
194 return sprintf(buf, "suspended\n");
196 return sprintf(buf, "suspending\n");
198 return sprintf(buf, "resuming\n");
200 return sprintf(buf, "active\n");
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);
212 static ssize_t async_show(struct device *dev, struct device_attribute *attr,
215 return sprintf(buf, "%s\n",
216 device_async_suspend_enabled(dev) ? enabled : disabled);
219 static ssize_t async_store(struct device *dev, struct device_attribute *attr,
220 const char *buf, size_t n)
225 cp = memchr(buf, '\n', n);
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);
237 static DEVICE_ATTR(async, 0644, async_show, async_store);
238 #endif /* CONFIG_PM_ADVANCED_DEBUG */
240 static struct attribute * power_attrs[] = {
241 #ifdef CONFIG_PM_RUNTIME
242 &dev_attr_control.attr,
244 &dev_attr_wakeup.attr,
245 #ifdef CONFIG_PM_SLEEP
246 &dev_attr_wakeup_count.attr,
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,
259 static struct attribute_group pm_attr_group = {
261 .attrs = power_attrs,
264 int dpm_sysfs_add(struct device * dev)
266 return sysfs_create_group(&dev->kobj, &pm_attr_group);
269 void dpm_sysfs_remove(struct device * dev)
271 sysfs_remove_group(&dev->kobj, &pm_attr_group);