blob: adf41be0ea664b7debc52b0ec49c5282a4fb46d5 [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>
Paul Gortmaker1b6bc322011-05-27 07:12:15 -04007#include <linux/export.h>
Rafael J. Wysocki53823632010-01-23 22:02:51 +01008#include <linux/pm_runtime.h>
Arun Sharma600634972011-07-26 16:09:06 -07009#include <linux/atomic.h>
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +020010#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include "power.h"
12
David Brownell0ac85242005-09-12 19:39:34 -070013/*
Rafael J. Wysocki53823632010-01-23 22:02:51 +010014 * control - Report/change current runtime PM setting of the device
15 *
16 * Runtime power management of a device can be blocked with the help of
17 * this attribute. All devices have one of the following two values for
18 * the power/control file:
19 *
20 * + "auto\n" to allow the device to be power managed at run time;
21 * + "on\n" to prevent the device from being power managed at run time;
22 *
23 * The default for all devices is "auto", which means that devices may be
24 * subject to automatic power management, depending on their drivers.
25 * Changing this attribute to "on" prevents the driver from power managing
26 * the device at run time. Doing that while the device is suspended causes
27 * it to be woken up.
28 *
David Brownell0ac85242005-09-12 19:39:34 -070029 * wakeup - Report/change current wakeup option for device
30 *
31 * Some devices support "wakeup" events, which are hardware signals
32 * used to activate devices from suspended or low power states. Such
33 * devices have one of three values for the sysfs power/wakeup file:
34 *
35 * + "enabled\n" to issue the events;
36 * + "disabled\n" not to do so; or
37 * + "\n" for temporary or permanent inability to issue wakeup.
38 *
39 * (For example, unconfigured USB devices can't issue wakeups.)
40 *
41 * Familiar examples of devices that can issue wakeup events include
42 * keyboards and mice (both PS2 and USB styles), power buttons, modems,
43 * "Wake-On-LAN" Ethernet links, GPIO lines, and more. Some events
44 * will wake the entire system from a suspend state; others may just
45 * wake up the device (if the system as a whole is already active).
46 * Some wakeup events use normal IRQ lines; other use special out
47 * of band signaling.
48 *
49 * It is the responsibility of device drivers to enable (or disable)
50 * wakeup signaling as part of changing device power states, respecting
51 * the policy choices provided through the driver model.
52 *
53 * Devices may not be able to generate wakeup events from all power
54 * states. Also, the events may be ignored in some configurations;
55 * for example, they might need help from other devices that aren't
56 * active, or which may have wakeup disabled. Some drivers rely on
57 * wakeup events internally (unless they are disabled), keeping
58 * their hardware in low power modes whenever they're unused. This
59 * saves runtime power, without requiring system-wide sleep states.
Rafael J. Wysocki5a2eb852010-01-23 22:25:23 +010060 *
61 * async - Report/change current async suspend setting for the device
62 *
63 * Asynchronous suspend and resume of the device during system-wide power
64 * state transitions can be enabled by writing "enabled" to this file.
65 * Analogously, if "disabled" is written to this file, the device will be
66 * suspended and resumed synchronously.
67 *
68 * All devices have one of the following two values for power/async:
69 *
70 * + "enabled\n" to permit the asynchronous suspend/resume of the device;
71 * + "disabled\n" to forbid it;
72 *
73 * NOTE: It generally is unsafe to permit the asynchronous suspend/resume
74 * of a device unless it is certain that all of the PM dependencies of the
75 * device are known to the PM core. However, for some devices this
76 * attribute is set to "enabled" by bus type code or device drivers and in
77 * that cases it should be safe to leave the default value.
Rafael J. Wysockic125e962010-07-05 22:43:53 +020078 *
Alan Stern15bcb91d2010-09-25 23:35:21 +020079 * autosuspend_delay_ms - Report/change a device's autosuspend_delay value
80 *
81 * Some drivers don't want to carry out a runtime suspend as soon as a
82 * device becomes idle; they want it always to remain idle for some period
83 * of time before suspending it. This period is the autosuspend_delay
84 * value (expressed in milliseconds) and it can be controlled by the user.
85 * If the value is negative then the device will never be runtime
86 * suspended.
87 *
88 * NOTE: The autosuspend_delay_ms attribute and the autosuspend_delay
89 * value are used only if the driver calls pm_runtime_use_autosuspend().
90 *
Rafael J. Wysockic125e962010-07-05 22:43:53 +020091 * wakeup_count - Report the number of wakeup events related to the device
David Brownell0ac85242005-09-12 19:39:34 -070092 */
93
94static const char enabled[] = "enabled";
95static const char disabled[] = "disabled";
96
Alan Stern7490e442010-09-25 23:35:15 +020097const char power_group_name[] = "power";
98EXPORT_SYMBOL_GPL(power_group_name);
99
Rafael J. Wysocki53823632010-01-23 22:02:51 +0100100#ifdef CONFIG_PM_RUNTIME
101static const char ctrl_auto[] = "auto";
102static const char ctrl_on[] = "on";
103
104static ssize_t control_show(struct device *dev, struct device_attribute *attr,
105 char *buf)
106{
107 return sprintf(buf, "%s\n",
108 dev->power.runtime_auto ? ctrl_auto : ctrl_on);
109}
110
111static ssize_t control_store(struct device * dev, struct device_attribute *attr,
112 const char * buf, size_t n)
113{
114 char *cp;
115 int len = n;
116
117 cp = memchr(buf, '\n', n);
118 if (cp)
119 len = cp - buf;
Alan Stern69c843b2011-07-06 10:52:23 +0200120 device_lock(dev);
Rafael J. Wysocki53823632010-01-23 22:02:51 +0100121 if (len == sizeof ctrl_auto - 1 && strncmp(buf, ctrl_auto, len) == 0)
122 pm_runtime_allow(dev);
123 else if (len == sizeof ctrl_on - 1 && strncmp(buf, ctrl_on, len) == 0)
124 pm_runtime_forbid(dev);
125 else
Alan Stern69c843b2011-07-06 10:52:23 +0200126 n = -EINVAL;
127 device_unlock(dev);
Rafael J. Wysocki53823632010-01-23 22:02:51 +0100128 return n;
129}
130
131static DEVICE_ATTR(control, 0644, control_show, control_store);
Alan Stern0fcb4ee2010-07-08 00:05:37 +0200132
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200133static ssize_t rtpm_active_time_show(struct device *dev,
134 struct device_attribute *attr, char *buf)
135{
136 int ret;
137 spin_lock_irq(&dev->power.lock);
138 update_pm_runtime_accounting(dev);
139 ret = sprintf(buf, "%i\n", jiffies_to_msecs(dev->power.active_jiffies));
140 spin_unlock_irq(&dev->power.lock);
141 return ret;
142}
143
144static DEVICE_ATTR(runtime_active_time, 0444, rtpm_active_time_show, NULL);
145
146static ssize_t rtpm_suspended_time_show(struct device *dev,
147 struct device_attribute *attr, char *buf)
148{
149 int ret;
150 spin_lock_irq(&dev->power.lock);
151 update_pm_runtime_accounting(dev);
152 ret = sprintf(buf, "%i\n",
153 jiffies_to_msecs(dev->power.suspended_jiffies));
154 spin_unlock_irq(&dev->power.lock);
155 return ret;
156}
157
158static DEVICE_ATTR(runtime_suspended_time, 0444, rtpm_suspended_time_show, NULL);
159
Alan Stern0fcb4ee2010-07-08 00:05:37 +0200160static ssize_t rtpm_status_show(struct device *dev,
161 struct device_attribute *attr, char *buf)
162{
163 const char *p;
164
165 if (dev->power.runtime_error) {
166 p = "error\n";
167 } else if (dev->power.disable_depth) {
168 p = "unsupported\n";
169 } else {
170 switch (dev->power.runtime_status) {
171 case RPM_SUSPENDED:
172 p = "suspended\n";
173 break;
174 case RPM_SUSPENDING:
175 p = "suspending\n";
176 break;
177 case RPM_RESUMING:
178 p = "resuming\n";
179 break;
180 case RPM_ACTIVE:
181 p = "active\n";
182 break;
183 default:
184 return -EIO;
185 }
186 }
187 return sprintf(buf, p);
188}
189
190static DEVICE_ATTR(runtime_status, 0444, rtpm_status_show, NULL);
Alan Stern15bcb91d2010-09-25 23:35:21 +0200191
192static ssize_t autosuspend_delay_ms_show(struct device *dev,
193 struct device_attribute *attr, char *buf)
194{
195 if (!dev->power.use_autosuspend)
196 return -EIO;
197 return sprintf(buf, "%d\n", dev->power.autosuspend_delay);
198}
199
200static ssize_t autosuspend_delay_ms_store(struct device *dev,
201 struct device_attribute *attr, const char *buf, size_t n)
202{
203 long delay;
204
205 if (!dev->power.use_autosuspend)
206 return -EIO;
207
208 if (strict_strtol(buf, 10, &delay) != 0 || delay != (int) delay)
209 return -EINVAL;
210
Alan Stern69c843b2011-07-06 10:52:23 +0200211 device_lock(dev);
Alan Stern15bcb91d2010-09-25 23:35:21 +0200212 pm_runtime_set_autosuspend_delay(dev, delay);
Alan Stern69c843b2011-07-06 10:52:23 +0200213 device_unlock(dev);
Alan Stern15bcb91d2010-09-25 23:35:21 +0200214 return n;
215}
216
217static DEVICE_ATTR(autosuspend_delay_ms, 0644, autosuspend_delay_ms_show,
218 autosuspend_delay_ms_store);
219
Rafael J. Wysockie7623182011-05-07 18:11:52 +0200220#endif /* CONFIG_PM_RUNTIME */
Rafael J. Wysocki53823632010-01-23 22:02:51 +0100221
Rafael J. Wysockie7623182011-05-07 18:11:52 +0200222#ifdef CONFIG_PM_SLEEP
David Brownell0ac85242005-09-12 19:39:34 -0700223static ssize_t
224wake_show(struct device * dev, struct device_attribute *attr, char * buf)
225{
226 return sprintf(buf, "%s\n", device_can_wakeup(dev)
227 ? (device_may_wakeup(dev) ? enabled : disabled)
228 : "");
229}
230
231static ssize_t
232wake_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 if (!device_can_wakeup(dev))
239 return -EINVAL;
240
241 cp = memchr(buf, '\n', n);
242 if (cp)
243 len = cp - buf;
244 if (len == sizeof enabled - 1
245 && strncmp(buf, enabled, sizeof enabled - 1) == 0)
246 device_set_wakeup_enable(dev, 1);
247 else if (len == sizeof disabled - 1
248 && strncmp(buf, disabled, sizeof disabled - 1) == 0)
249 device_set_wakeup_enable(dev, 0);
250 else
251 return -EINVAL;
252 return n;
253}
254
255static DEVICE_ATTR(wakeup, 0644, wake_show, wake_store);
256
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200257static ssize_t wakeup_count_show(struct device *dev,
258 struct device_attribute *attr, char *buf)
259{
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200260 unsigned long count = 0;
261 bool enabled = false;
262
263 spin_lock_irq(&dev->power.lock);
264 if (dev->power.wakeup) {
265 count = dev->power.wakeup->event_count;
266 enabled = true;
267 }
268 spin_unlock_irq(&dev->power.lock);
269 return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200270}
271
272static DEVICE_ATTR(wakeup_count, 0444, wakeup_count_show, NULL);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200273
274static ssize_t wakeup_active_count_show(struct device *dev,
275 struct device_attribute *attr, char *buf)
276{
277 unsigned long count = 0;
278 bool enabled = false;
279
280 spin_lock_irq(&dev->power.lock);
281 if (dev->power.wakeup) {
282 count = dev->power.wakeup->active_count;
283 enabled = true;
284 }
285 spin_unlock_irq(&dev->power.lock);
286 return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
287}
288
289static DEVICE_ATTR(wakeup_active_count, 0444, wakeup_active_count_show, NULL);
290
291static ssize_t wakeup_hit_count_show(struct device *dev,
292 struct device_attribute *attr, char *buf)
293{
294 unsigned long count = 0;
295 bool enabled = false;
296
297 spin_lock_irq(&dev->power.lock);
298 if (dev->power.wakeup) {
299 count = dev->power.wakeup->hit_count;
300 enabled = true;
301 }
302 spin_unlock_irq(&dev->power.lock);
303 return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
304}
305
306static DEVICE_ATTR(wakeup_hit_count, 0444, wakeup_hit_count_show, NULL);
307
308static ssize_t wakeup_active_show(struct device *dev,
309 struct device_attribute *attr, char *buf)
310{
311 unsigned int active = 0;
312 bool enabled = false;
313
314 spin_lock_irq(&dev->power.lock);
315 if (dev->power.wakeup) {
316 active = dev->power.wakeup->active;
317 enabled = true;
318 }
319 spin_unlock_irq(&dev->power.lock);
320 return enabled ? sprintf(buf, "%u\n", active) : sprintf(buf, "\n");
321}
322
323static DEVICE_ATTR(wakeup_active, 0444, wakeup_active_show, NULL);
324
325static ssize_t wakeup_total_time_show(struct device *dev,
326 struct device_attribute *attr, char *buf)
327{
328 s64 msec = 0;
329 bool enabled = false;
330
331 spin_lock_irq(&dev->power.lock);
332 if (dev->power.wakeup) {
333 msec = ktime_to_ms(dev->power.wakeup->total_time);
334 enabled = true;
335 }
336 spin_unlock_irq(&dev->power.lock);
337 return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
338}
339
340static DEVICE_ATTR(wakeup_total_time_ms, 0444, wakeup_total_time_show, NULL);
341
342static ssize_t wakeup_max_time_show(struct device *dev,
343 struct device_attribute *attr, char *buf)
344{
345 s64 msec = 0;
346 bool enabled = false;
347
348 spin_lock_irq(&dev->power.lock);
349 if (dev->power.wakeup) {
350 msec = ktime_to_ms(dev->power.wakeup->max_time);
351 enabled = true;
352 }
353 spin_unlock_irq(&dev->power.lock);
354 return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
355}
356
357static DEVICE_ATTR(wakeup_max_time_ms, 0444, wakeup_max_time_show, NULL);
358
359static ssize_t wakeup_last_time_show(struct device *dev,
360 struct device_attribute *attr, char *buf)
361{
362 s64 msec = 0;
363 bool enabled = false;
364
365 spin_lock_irq(&dev->power.lock);
366 if (dev->power.wakeup) {
367 msec = ktime_to_ms(dev->power.wakeup->last_time);
368 enabled = true;
369 }
370 spin_unlock_irq(&dev->power.lock);
371 return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
372}
373
374static DEVICE_ATTR(wakeup_last_time_ms, 0444, wakeup_last_time_show, NULL);
375#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200376
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200377#ifdef CONFIG_PM_ADVANCED_DEBUG
378#ifdef CONFIG_PM_RUNTIME
379
380static ssize_t rtpm_usagecount_show(struct device *dev,
381 struct device_attribute *attr, char *buf)
382{
383 return sprintf(buf, "%d\n", atomic_read(&dev->power.usage_count));
384}
385
386static ssize_t rtpm_children_show(struct device *dev,
387 struct device_attribute *attr, char *buf)
388{
389 return sprintf(buf, "%d\n", dev->power.ignore_children ?
390 0 : atomic_read(&dev->power.child_count));
391}
392
393static ssize_t rtpm_enabled_show(struct device *dev,
394 struct device_attribute *attr, char *buf)
395{
396 if ((dev->power.disable_depth) && (dev->power.runtime_auto == false))
397 return sprintf(buf, "disabled & forbidden\n");
398 else if (dev->power.disable_depth)
399 return sprintf(buf, "disabled\n");
400 else if (dev->power.runtime_auto == false)
401 return sprintf(buf, "forbidden\n");
402 return sprintf(buf, "enabled\n");
403}
404
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200405static DEVICE_ATTR(runtime_usage, 0444, rtpm_usagecount_show, NULL);
406static DEVICE_ATTR(runtime_active_kids, 0444, rtpm_children_show, NULL);
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200407static DEVICE_ATTR(runtime_enabled, 0444, rtpm_enabled_show, NULL);
408
409#endif
410
Rafael J. Wysocki5a2eb852010-01-23 22:25:23 +0100411static ssize_t async_show(struct device *dev, struct device_attribute *attr,
412 char *buf)
413{
414 return sprintf(buf, "%s\n",
415 device_async_suspend_enabled(dev) ? enabled : disabled);
416}
417
418static ssize_t async_store(struct device *dev, struct device_attribute *attr,
419 const char *buf, size_t n)
420{
421 char *cp;
422 int len = n;
423
424 cp = memchr(buf, '\n', n);
425 if (cp)
426 len = cp - buf;
427 if (len == sizeof enabled - 1 && strncmp(buf, enabled, len) == 0)
428 device_enable_async_suspend(dev);
429 else if (len == sizeof disabled - 1 && strncmp(buf, disabled, len) == 0)
430 device_disable_async_suspend(dev);
431 else
432 return -EINVAL;
433 return n;
434}
435
436static DEVICE_ATTR(async, 0644, async_show, async_store);
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200437#endif /* CONFIG_PM_ADVANCED_DEBUG */
David Brownell0ac85242005-09-12 19:39:34 -0700438
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100439static struct attribute *power_attrs[] = {
440#ifdef CONFIG_PM_ADVANCED_DEBUG
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200441#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100442 &dev_attr_async.attr,
443#endif
444#ifdef CONFIG_PM_RUNTIME
445 &dev_attr_runtime_status.attr,
446 &dev_attr_runtime_usage.attr,
447 &dev_attr_runtime_active_kids.attr,
448 &dev_attr_runtime_enabled.attr,
449#endif
450#endif /* CONFIG_PM_ADVANCED_DEBUG */
451 NULL,
452};
453static struct attribute_group pm_attr_group = {
454 .name = power_group_name,
455 .attrs = power_attrs,
456};
457
458static struct attribute *wakeup_attrs[] = {
459#ifdef CONFIG_PM_SLEEP
460 &dev_attr_wakeup.attr,
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200461 &dev_attr_wakeup_count.attr,
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200462 &dev_attr_wakeup_active_count.attr,
463 &dev_attr_wakeup_hit_count.attr,
464 &dev_attr_wakeup_active.attr,
465 &dev_attr_wakeup_total_time_ms.attr,
466 &dev_attr_wakeup_max_time_ms.attr,
467 &dev_attr_wakeup_last_time_ms.attr,
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200468#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 NULL,
470};
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100471static struct attribute_group pm_wakeup_attr_group = {
Alan Stern7490e442010-09-25 23:35:15 +0200472 .name = power_group_name,
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100473 .attrs = wakeup_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474};
475
Alan Stern7490e442010-09-25 23:35:15 +0200476static struct attribute *runtime_attrs[] = {
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100477#ifdef CONFIG_PM_RUNTIME
Alan Stern7490e442010-09-25 23:35:15 +0200478#ifndef CONFIG_PM_ADVANCED_DEBUG
479 &dev_attr_runtime_status.attr,
480#endif
481 &dev_attr_control.attr,
482 &dev_attr_runtime_suspended_time.attr,
483 &dev_attr_runtime_active_time.attr,
Alan Stern15bcb91d2010-09-25 23:35:21 +0200484 &dev_attr_autosuspend_delay_ms.attr,
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100485#endif /* CONFIG_PM_RUNTIME */
Alan Stern7490e442010-09-25 23:35:15 +0200486 NULL,
487};
488static struct attribute_group pm_runtime_attr_group = {
489 .name = power_group_name,
490 .attrs = runtime_attrs,
491};
492
493int dpm_sysfs_add(struct device *dev)
494{
495 int rc;
496
497 rc = sysfs_create_group(&dev->kobj, &pm_attr_group);
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100498 if (rc)
499 return rc;
500
501 if (pm_runtime_callbacks_present(dev)) {
Alan Stern7490e442010-09-25 23:35:15 +0200502 rc = sysfs_merge_group(&dev->kobj, &pm_runtime_attr_group);
503 if (rc)
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100504 goto err_out;
Alan Stern7490e442010-09-25 23:35:15 +0200505 }
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100506
507 if (device_can_wakeup(dev)) {
508 rc = sysfs_merge_group(&dev->kobj, &pm_wakeup_attr_group);
509 if (rc) {
510 if (pm_runtime_callbacks_present(dev))
511 sysfs_unmerge_group(&dev->kobj,
512 &pm_runtime_attr_group);
513 goto err_out;
514 }
515 }
516 return 0;
517
518 err_out:
519 sysfs_remove_group(&dev->kobj, &pm_attr_group);
Alan Stern7490e442010-09-25 23:35:15 +0200520 return rc;
521}
522
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100523int wakeup_sysfs_add(struct device *dev)
524{
525 return sysfs_merge_group(&dev->kobj, &pm_wakeup_attr_group);
526}
527
528void wakeup_sysfs_remove(struct device *dev)
529{
530 sysfs_unmerge_group(&dev->kobj, &pm_wakeup_attr_group);
531}
532
Alan Stern7490e442010-09-25 23:35:15 +0200533void rpm_sysfs_remove(struct device *dev)
534{
535 sysfs_unmerge_group(&dev->kobj, &pm_runtime_attr_group);
536}
537
538void dpm_sysfs_remove(struct device *dev)
539{
540 rpm_sysfs_remove(dev);
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100541 sysfs_unmerge_group(&dev->kobj, &pm_wakeup_attr_group);
Alan Stern7490e442010-09-25 23:35:15 +0200542 sysfs_remove_group(&dev->kobj, &pm_attr_group);
543}