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.
78 static const char enabled[] = "enabled";
79 static const char disabled[] = "disabled";
81 #ifdef CONFIG_PM_RUNTIME
82 static const char ctrl_auto[] = "auto";
83 static const char ctrl_on[] = "on";
85 static ssize_t control_show(struct device *dev, struct device_attribute *attr,
88 return sprintf(buf, "%s\n",
89 dev->power.runtime_auto ? ctrl_auto : ctrl_on);
92 static ssize_t control_store(struct device * dev, struct device_attribute *attr,
93 const char * buf, size_t n)
98 cp = memchr(buf, '\n', n);
101 if (len == sizeof ctrl_auto - 1 && strncmp(buf, ctrl_auto, len) == 0)
102 pm_runtime_allow(dev);
103 else if (len == sizeof ctrl_on - 1 && strncmp(buf, ctrl_on, len) == 0)
104 pm_runtime_forbid(dev);
110 static DEVICE_ATTR(control, 0644, control_show, control_store);
114 wake_show(struct device * dev, struct device_attribute *attr, char * buf)
116 return sprintf(buf, "%s\n", device_can_wakeup(dev)
117 ? (device_may_wakeup(dev) ? enabled : disabled)
122 wake_store(struct device * dev, struct device_attribute *attr,
123 const char * buf, size_t n)
128 if (!device_can_wakeup(dev))
131 cp = memchr(buf, '\n', n);
134 if (len == sizeof enabled - 1
135 && strncmp(buf, enabled, sizeof enabled - 1) == 0)
136 device_set_wakeup_enable(dev, 1);
137 else if (len == sizeof disabled - 1
138 && strncmp(buf, disabled, sizeof disabled - 1) == 0)
139 device_set_wakeup_enable(dev, 0);
145 static DEVICE_ATTR(wakeup, 0644, wake_show, wake_store);
147 #ifdef CONFIG_PM_ADVANCED_DEBUG
148 #ifdef CONFIG_PM_RUNTIME
150 static ssize_t rtpm_usagecount_show(struct device *dev,
151 struct device_attribute *attr, char *buf)
153 return sprintf(buf, "%d\n", atomic_read(&dev->power.usage_count));
156 static ssize_t rtpm_children_show(struct device *dev,
157 struct device_attribute *attr, char *buf)
159 return sprintf(buf, "%d\n", dev->power.ignore_children ?
160 0 : atomic_read(&dev->power.child_count));
163 static ssize_t rtpm_enabled_show(struct device *dev,
164 struct device_attribute *attr, char *buf)
166 if ((dev->power.disable_depth) && (dev->power.runtime_auto == false))
167 return sprintf(buf, "disabled & forbidden\n");
168 else if (dev->power.disable_depth)
169 return sprintf(buf, "disabled\n");
170 else if (dev->power.runtime_auto == false)
171 return sprintf(buf, "forbidden\n");
172 return sprintf(buf, "enabled\n");
175 static ssize_t rtpm_status_show(struct device *dev,
176 struct device_attribute *attr, char *buf)
178 if (dev->power.runtime_error)
179 return sprintf(buf, "error\n");
180 switch (dev->power.runtime_status) {
182 return sprintf(buf, "suspended\n");
184 return sprintf(buf, "suspending\n");
186 return sprintf(buf, "resuming\n");
188 return sprintf(buf, "active\n");
193 static DEVICE_ATTR(runtime_usage, 0444, rtpm_usagecount_show, NULL);
194 static DEVICE_ATTR(runtime_active_kids, 0444, rtpm_children_show, NULL);
195 static DEVICE_ATTR(runtime_status, 0444, rtpm_status_show, NULL);
196 static DEVICE_ATTR(runtime_enabled, 0444, rtpm_enabled_show, NULL);
200 static ssize_t async_show(struct device *dev, struct device_attribute *attr,
203 return sprintf(buf, "%s\n",
204 device_async_suspend_enabled(dev) ? enabled : disabled);
207 static ssize_t async_store(struct device *dev, struct device_attribute *attr,
208 const char *buf, size_t n)
213 cp = memchr(buf, '\n', n);
216 if (len == sizeof enabled - 1 && strncmp(buf, enabled, len) == 0)
217 device_enable_async_suspend(dev);
218 else if (len == sizeof disabled - 1 && strncmp(buf, disabled, len) == 0)
219 device_disable_async_suspend(dev);
225 static DEVICE_ATTR(async, 0644, async_show, async_store);
226 #endif /* CONFIG_PM_ADVANCED_DEBUG */
228 static struct attribute * power_attrs[] = {
229 #ifdef CONFIG_PM_RUNTIME
230 &dev_attr_control.attr,
232 &dev_attr_wakeup.attr,
233 #ifdef CONFIG_PM_ADVANCED_DEBUG
234 &dev_attr_async.attr,
235 #ifdef CONFIG_PM_RUNTIME
236 &dev_attr_runtime_usage.attr,
237 &dev_attr_runtime_active_kids.attr,
238 &dev_attr_runtime_status.attr,
239 &dev_attr_runtime_enabled.attr,
244 static struct attribute_group pm_attr_group = {
246 .attrs = power_attrs,
249 int dpm_sysfs_add(struct device * dev)
251 return sysfs_create_group(&dev->kobj, &pm_attr_group);
254 void dpm_sysfs_remove(struct device * dev)
256 sysfs_remove_group(&dev->kobj, &pm_attr_group);