blob: 8859780817e11b5e3e5484815dfca88856d539a1 [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>
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +02009#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include "power.h"
11
David Brownell0ac85242005-09-12 19:39:34 -070012/*
Rafael J. Wysocki53823632010-01-23 22:02:51 +010013 * control - Report/change current runtime PM setting of the device
14 *
15 * Runtime power management of a device can be blocked with the help of
16 * this attribute. All devices have one of the following two values for
17 * the power/control file:
18 *
19 * + "auto\n" to allow the device to be power managed at run time;
20 * + "on\n" to prevent the device from being power managed at run time;
21 *
22 * The default for all devices is "auto", which means that devices may be
23 * subject to automatic power management, depending on their drivers.
24 * Changing this attribute to "on" prevents the driver from power managing
25 * the device at run time. Doing that while the device is suspended causes
26 * it to be woken up.
27 *
David Brownell0ac85242005-09-12 19:39:34 -070028 * wakeup - Report/change current wakeup option for device
29 *
30 * Some devices support "wakeup" events, which are hardware signals
31 * used to activate devices from suspended or low power states. Such
32 * devices have one of three values for the sysfs power/wakeup file:
33 *
34 * + "enabled\n" to issue the events;
35 * + "disabled\n" not to do so; or
36 * + "\n" for temporary or permanent inability to issue wakeup.
37 *
38 * (For example, unconfigured USB devices can't issue wakeups.)
39 *
40 * Familiar examples of devices that can issue wakeup events include
41 * keyboards and mice (both PS2 and USB styles), power buttons, modems,
42 * "Wake-On-LAN" Ethernet links, GPIO lines, and more. Some events
43 * will wake the entire system from a suspend state; others may just
44 * wake up the device (if the system as a whole is already active).
45 * Some wakeup events use normal IRQ lines; other use special out
46 * of band signaling.
47 *
48 * It is the responsibility of device drivers to enable (or disable)
49 * wakeup signaling as part of changing device power states, respecting
50 * the policy choices provided through the driver model.
51 *
52 * Devices may not be able to generate wakeup events from all power
53 * states. Also, the events may be ignored in some configurations;
54 * for example, they might need help from other devices that aren't
55 * active, or which may have wakeup disabled. Some drivers rely on
56 * wakeup events internally (unless they are disabled), keeping
57 * their hardware in low power modes whenever they're unused. This
58 * saves runtime power, without requiring system-wide sleep states.
Rafael J. Wysocki5a2eb852010-01-23 22:25:23 +010059 *
60 * async - Report/change current async suspend setting for the device
61 *
62 * Asynchronous suspend and resume of the device during system-wide power
63 * state transitions can be enabled by writing "enabled" to this file.
64 * Analogously, if "disabled" is written to this file, the device will be
65 * suspended and resumed synchronously.
66 *
67 * All devices have one of the following two values for power/async:
68 *
69 * + "enabled\n" to permit the asynchronous suspend/resume of the device;
70 * + "disabled\n" to forbid it;
71 *
72 * NOTE: It generally is unsafe to permit the asynchronous suspend/resume
73 * of a device unless it is certain that all of the PM dependencies of the
74 * device are known to the PM core. However, for some devices this
75 * attribute is set to "enabled" by bus type code or device drivers and in
76 * that cases it should be safe to leave the default value.
Rafael J. Wysockic125e962010-07-05 22:43:53 +020077 *
78 * wakeup_count - Report the number of wakeup events related to the device
David Brownell0ac85242005-09-12 19:39:34 -070079 */
80
81static const char enabled[] = "enabled";
82static const char disabled[] = "disabled";
83
Rafael J. Wysocki53823632010-01-23 22:02:51 +010084#ifdef CONFIG_PM_RUNTIME
85static const char ctrl_auto[] = "auto";
86static const char ctrl_on[] = "on";
87
88static ssize_t control_show(struct device *dev, struct device_attribute *attr,
89 char *buf)
90{
91 return sprintf(buf, "%s\n",
92 dev->power.runtime_auto ? ctrl_auto : ctrl_on);
93}
94
95static ssize_t control_store(struct device * dev, struct device_attribute *attr,
96 const char * buf, size_t n)
97{
98 char *cp;
99 int len = n;
100
101 cp = memchr(buf, '\n', n);
102 if (cp)
103 len = cp - buf;
104 if (len == sizeof ctrl_auto - 1 && strncmp(buf, ctrl_auto, len) == 0)
105 pm_runtime_allow(dev);
106 else if (len == sizeof ctrl_on - 1 && strncmp(buf, ctrl_on, len) == 0)
107 pm_runtime_forbid(dev);
108 else
109 return -EINVAL;
110 return n;
111}
112
113static DEVICE_ATTR(control, 0644, control_show, control_store);
Alan Stern0fcb4ee2010-07-08 00:05:37 +0200114
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200115static ssize_t rtpm_active_time_show(struct device *dev,
116 struct device_attribute *attr, char *buf)
117{
118 int ret;
119 spin_lock_irq(&dev->power.lock);
120 update_pm_runtime_accounting(dev);
121 ret = sprintf(buf, "%i\n", jiffies_to_msecs(dev->power.active_jiffies));
122 spin_unlock_irq(&dev->power.lock);
123 return ret;
124}
125
126static DEVICE_ATTR(runtime_active_time, 0444, rtpm_active_time_show, NULL);
127
128static ssize_t rtpm_suspended_time_show(struct device *dev,
129 struct device_attribute *attr, char *buf)
130{
131 int ret;
132 spin_lock_irq(&dev->power.lock);
133 update_pm_runtime_accounting(dev);
134 ret = sprintf(buf, "%i\n",
135 jiffies_to_msecs(dev->power.suspended_jiffies));
136 spin_unlock_irq(&dev->power.lock);
137 return ret;
138}
139
140static DEVICE_ATTR(runtime_suspended_time, 0444, rtpm_suspended_time_show, NULL);
141
Alan Stern0fcb4ee2010-07-08 00:05:37 +0200142static ssize_t rtpm_status_show(struct device *dev,
143 struct device_attribute *attr, char *buf)
144{
145 const char *p;
146
147 if (dev->power.runtime_error) {
148 p = "error\n";
149 } else if (dev->power.disable_depth) {
150 p = "unsupported\n";
151 } else {
152 switch (dev->power.runtime_status) {
153 case RPM_SUSPENDED:
154 p = "suspended\n";
155 break;
156 case RPM_SUSPENDING:
157 p = "suspending\n";
158 break;
159 case RPM_RESUMING:
160 p = "resuming\n";
161 break;
162 case RPM_ACTIVE:
163 p = "active\n";
164 break;
165 default:
166 return -EIO;
167 }
168 }
169 return sprintf(buf, p);
170}
171
172static DEVICE_ATTR(runtime_status, 0444, rtpm_status_show, NULL);
Rafael J. Wysocki53823632010-01-23 22:02:51 +0100173#endif
174
David Brownell0ac85242005-09-12 19:39:34 -0700175static ssize_t
176wake_show(struct device * dev, struct device_attribute *attr, char * buf)
177{
178 return sprintf(buf, "%s\n", device_can_wakeup(dev)
179 ? (device_may_wakeup(dev) ? enabled : disabled)
180 : "");
181}
182
183static ssize_t
184wake_store(struct device * dev, struct device_attribute *attr,
185 const char * buf, size_t n)
186{
187 char *cp;
188 int len = n;
189
190 if (!device_can_wakeup(dev))
191 return -EINVAL;
192
193 cp = memchr(buf, '\n', n);
194 if (cp)
195 len = cp - buf;
196 if (len == sizeof enabled - 1
197 && strncmp(buf, enabled, sizeof enabled - 1) == 0)
198 device_set_wakeup_enable(dev, 1);
199 else if (len == sizeof disabled - 1
200 && strncmp(buf, disabled, sizeof disabled - 1) == 0)
201 device_set_wakeup_enable(dev, 0);
202 else
203 return -EINVAL;
204 return n;
205}
206
207static DEVICE_ATTR(wakeup, 0644, wake_show, wake_store);
208
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200209#ifdef CONFIG_PM_SLEEP
210static ssize_t wakeup_count_show(struct device *dev,
211 struct device_attribute *attr, char *buf)
212{
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200213 unsigned long count = 0;
214 bool enabled = false;
215
216 spin_lock_irq(&dev->power.lock);
217 if (dev->power.wakeup) {
218 count = dev->power.wakeup->event_count;
219 enabled = true;
220 }
221 spin_unlock_irq(&dev->power.lock);
222 return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200223}
224
225static DEVICE_ATTR(wakeup_count, 0444, wakeup_count_show, NULL);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200226
227static ssize_t wakeup_active_count_show(struct device *dev,
228 struct device_attribute *attr, char *buf)
229{
230 unsigned long count = 0;
231 bool enabled = false;
232
233 spin_lock_irq(&dev->power.lock);
234 if (dev->power.wakeup) {
235 count = dev->power.wakeup->active_count;
236 enabled = true;
237 }
238 spin_unlock_irq(&dev->power.lock);
239 return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
240}
241
242static DEVICE_ATTR(wakeup_active_count, 0444, wakeup_active_count_show, NULL);
243
244static ssize_t wakeup_hit_count_show(struct device *dev,
245 struct device_attribute *attr, char *buf)
246{
247 unsigned long count = 0;
248 bool enabled = false;
249
250 spin_lock_irq(&dev->power.lock);
251 if (dev->power.wakeup) {
252 count = dev->power.wakeup->hit_count;
253 enabled = true;
254 }
255 spin_unlock_irq(&dev->power.lock);
256 return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
257}
258
259static DEVICE_ATTR(wakeup_hit_count, 0444, wakeup_hit_count_show, NULL);
260
261static ssize_t wakeup_active_show(struct device *dev,
262 struct device_attribute *attr, char *buf)
263{
264 unsigned int active = 0;
265 bool enabled = false;
266
267 spin_lock_irq(&dev->power.lock);
268 if (dev->power.wakeup) {
269 active = dev->power.wakeup->active;
270 enabled = true;
271 }
272 spin_unlock_irq(&dev->power.lock);
273 return enabled ? sprintf(buf, "%u\n", active) : sprintf(buf, "\n");
274}
275
276static DEVICE_ATTR(wakeup_active, 0444, wakeup_active_show, NULL);
277
278static ssize_t wakeup_total_time_show(struct device *dev,
279 struct device_attribute *attr, char *buf)
280{
281 s64 msec = 0;
282 bool enabled = false;
283
284 spin_lock_irq(&dev->power.lock);
285 if (dev->power.wakeup) {
286 msec = ktime_to_ms(dev->power.wakeup->total_time);
287 enabled = true;
288 }
289 spin_unlock_irq(&dev->power.lock);
290 return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
291}
292
293static DEVICE_ATTR(wakeup_total_time_ms, 0444, wakeup_total_time_show, NULL);
294
295static ssize_t wakeup_max_time_show(struct device *dev,
296 struct device_attribute *attr, char *buf)
297{
298 s64 msec = 0;
299 bool enabled = false;
300
301 spin_lock_irq(&dev->power.lock);
302 if (dev->power.wakeup) {
303 msec = ktime_to_ms(dev->power.wakeup->max_time);
304 enabled = true;
305 }
306 spin_unlock_irq(&dev->power.lock);
307 return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
308}
309
310static DEVICE_ATTR(wakeup_max_time_ms, 0444, wakeup_max_time_show, NULL);
311
312static ssize_t wakeup_last_time_show(struct device *dev,
313 struct device_attribute *attr, char *buf)
314{
315 s64 msec = 0;
316 bool enabled = false;
317
318 spin_lock_irq(&dev->power.lock);
319 if (dev->power.wakeup) {
320 msec = ktime_to_ms(dev->power.wakeup->last_time);
321 enabled = true;
322 }
323 spin_unlock_irq(&dev->power.lock);
324 return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
325}
326
327static DEVICE_ATTR(wakeup_last_time_ms, 0444, wakeup_last_time_show, NULL);
328#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200329
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200330#ifdef CONFIG_PM_ADVANCED_DEBUG
331#ifdef CONFIG_PM_RUNTIME
332
333static ssize_t rtpm_usagecount_show(struct device *dev,
334 struct device_attribute *attr, char *buf)
335{
336 return sprintf(buf, "%d\n", atomic_read(&dev->power.usage_count));
337}
338
339static ssize_t rtpm_children_show(struct device *dev,
340 struct device_attribute *attr, char *buf)
341{
342 return sprintf(buf, "%d\n", dev->power.ignore_children ?
343 0 : atomic_read(&dev->power.child_count));
344}
345
346static ssize_t rtpm_enabled_show(struct device *dev,
347 struct device_attribute *attr, char *buf)
348{
349 if ((dev->power.disable_depth) && (dev->power.runtime_auto == false))
350 return sprintf(buf, "disabled & forbidden\n");
351 else if (dev->power.disable_depth)
352 return sprintf(buf, "disabled\n");
353 else if (dev->power.runtime_auto == false)
354 return sprintf(buf, "forbidden\n");
355 return sprintf(buf, "enabled\n");
356}
357
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200358static DEVICE_ATTR(runtime_usage, 0444, rtpm_usagecount_show, NULL);
359static DEVICE_ATTR(runtime_active_kids, 0444, rtpm_children_show, NULL);
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200360static DEVICE_ATTR(runtime_enabled, 0444, rtpm_enabled_show, NULL);
361
362#endif
363
Rafael J. Wysocki5a2eb852010-01-23 22:25:23 +0100364static ssize_t async_show(struct device *dev, struct device_attribute *attr,
365 char *buf)
366{
367 return sprintf(buf, "%s\n",
368 device_async_suspend_enabled(dev) ? enabled : disabled);
369}
370
371static ssize_t async_store(struct device *dev, struct device_attribute *attr,
372 const char *buf, size_t n)
373{
374 char *cp;
375 int len = n;
376
377 cp = memchr(buf, '\n', n);
378 if (cp)
379 len = cp - buf;
380 if (len == sizeof enabled - 1 && strncmp(buf, enabled, len) == 0)
381 device_enable_async_suspend(dev);
382 else if (len == sizeof disabled - 1 && strncmp(buf, disabled, len) == 0)
383 device_disable_async_suspend(dev);
384 else
385 return -EINVAL;
386 return n;
387}
388
389static DEVICE_ATTR(async, 0644, async_show, async_store);
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200390#endif /* CONFIG_PM_ADVANCED_DEBUG */
David Brownell0ac85242005-09-12 19:39:34 -0700391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392static struct attribute * power_attrs[] = {
Rafael J. Wysocki53823632010-01-23 22:02:51 +0100393#ifdef CONFIG_PM_RUNTIME
394 &dev_attr_control.attr,
Alan Stern0fcb4ee2010-07-08 00:05:37 +0200395 &dev_attr_runtime_status.attr,
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200396 &dev_attr_runtime_suspended_time.attr,
397 &dev_attr_runtime_active_time.attr,
Rafael J. Wysocki53823632010-01-23 22:02:51 +0100398#endif
David Brownell0ac85242005-09-12 19:39:34 -0700399 &dev_attr_wakeup.attr,
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200400#ifdef CONFIG_PM_SLEEP
401 &dev_attr_wakeup_count.attr,
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200402 &dev_attr_wakeup_active_count.attr,
403 &dev_attr_wakeup_hit_count.attr,
404 &dev_attr_wakeup_active.attr,
405 &dev_attr_wakeup_total_time_ms.attr,
406 &dev_attr_wakeup_max_time_ms.attr,
407 &dev_attr_wakeup_last_time_ms.attr,
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200408#endif
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200409#ifdef CONFIG_PM_ADVANCED_DEBUG
Rafael J. Wysocki5a2eb852010-01-23 22:25:23 +0100410 &dev_attr_async.attr,
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200411#ifdef CONFIG_PM_RUNTIME
412 &dev_attr_runtime_usage.attr,
413 &dev_attr_runtime_active_kids.attr,
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200414 &dev_attr_runtime_enabled.attr,
415#endif
Rafael J. Wysocki5a2eb852010-01-23 22:25:23 +0100416#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 NULL,
418};
419static struct attribute_group pm_attr_group = {
420 .name = "power",
421 .attrs = power_attrs,
422};
423
424int dpm_sysfs_add(struct device * dev)
425{
426 return sysfs_create_group(&dev->kobj, &pm_attr_group);
427}
428
429void dpm_sysfs_remove(struct device * dev)
430{
431 sysfs_remove_group(&dev->kobj, &pm_attr_group);
432}