2 * drivers/base/power/sysfs.c - sysfs entries for device PM
5 #include <linux/device.h>
6 #include <linux/string.h>
7 #include <linux/export.h>
8 #include <linux/pm_runtime.h>
9 #include <linux/atomic.h>
10 #include <linux/jiffies.h>
14 * control - Report/change current runtime PM setting of the device
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:
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;
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
29 * wakeup - Report/change current wakeup option for device
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:
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.
39 * (For example, unconfigured USB devices can't issue wakeups.)
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
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.
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.
61 * async - Report/change current async suspend setting for the device
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.
68 * All devices have one of the following two values for power/async:
70 * + "enabled\n" to permit the asynchronous suspend/resume of the device;
71 * + "disabled\n" to forbid it;
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.
79 * autosuspend_delay_ms - Report/change a device's autosuspend_delay value
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
88 * NOTE: The autosuspend_delay_ms attribute and the autosuspend_delay
89 * value are used only if the driver calls pm_runtime_use_autosuspend().
91 * wakeup_count - Report the number of wakeup events related to the device
94 static const char enabled
[] = "enabled";
95 static const char disabled
[] = "disabled";
97 const char power_group_name
[] = "power";
98 EXPORT_SYMBOL_GPL(power_group_name
);
100 #ifdef CONFIG_PM_RUNTIME
101 static const char ctrl_auto
[] = "auto";
102 static const char ctrl_on
[] = "on";
104 static ssize_t
control_show(struct device
*dev
, struct device_attribute
*attr
,
107 return sprintf(buf
, "%s\n",
108 dev
->power
.runtime_auto
? ctrl_auto
: ctrl_on
);
111 static ssize_t
control_store(struct device
* dev
, struct device_attribute
*attr
,
112 const char * buf
, size_t n
)
117 cp
= memchr(buf
, '\n', n
);
121 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
);
131 static DEVICE_ATTR(control
, 0644, control_show
, control_store
);
133 static ssize_t
rtpm_active_time_show(struct device
*dev
,
134 struct device_attribute
*attr
, char *buf
)
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
);
144 static DEVICE_ATTR(runtime_active_time
, 0444, rtpm_active_time_show
, NULL
);
146 static ssize_t
rtpm_suspended_time_show(struct device
*dev
,
147 struct device_attribute
*attr
, char *buf
)
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
);
158 static DEVICE_ATTR(runtime_suspended_time
, 0444, rtpm_suspended_time_show
, NULL
);
160 static ssize_t
rtpm_status_show(struct device
*dev
,
161 struct device_attribute
*attr
, char *buf
)
165 if (dev
->power
.runtime_error
) {
167 } else if (dev
->power
.disable_depth
) {
170 switch (dev
->power
.runtime_status
) {
187 return sprintf(buf
, p
);
190 static DEVICE_ATTR(runtime_status
, 0444, rtpm_status_show
, NULL
);
192 static ssize_t
autosuspend_delay_ms_show(struct device
*dev
,
193 struct device_attribute
*attr
, char *buf
)
195 if (!dev
->power
.use_autosuspend
)
197 return sprintf(buf
, "%d\n", dev
->power
.autosuspend_delay
);
200 static ssize_t
autosuspend_delay_ms_store(struct device
*dev
,
201 struct device_attribute
*attr
, const char *buf
, size_t n
)
205 if (!dev
->power
.use_autosuspend
)
208 if (strict_strtol(buf
, 10, &delay
) != 0 || delay
!= (int) delay
)
212 pm_runtime_set_autosuspend_delay(dev
, delay
);
217 static DEVICE_ATTR(autosuspend_delay_ms
, 0644, autosuspend_delay_ms_show
,
218 autosuspend_delay_ms_store
);
220 #endif /* CONFIG_PM_RUNTIME */
222 #ifdef CONFIG_PM_SLEEP
224 wake_show(struct device
* dev
, struct device_attribute
*attr
, char * buf
)
226 return sprintf(buf
, "%s\n", device_can_wakeup(dev
)
227 ? (device_may_wakeup(dev
) ? enabled
: disabled
)
232 wake_store(struct device
* dev
, struct device_attribute
*attr
,
233 const char * buf
, size_t n
)
238 if (!device_can_wakeup(dev
))
241 cp
= memchr(buf
, '\n', n
);
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);
255 static DEVICE_ATTR(wakeup
, 0644, wake_show
, wake_store
);
257 static ssize_t
wakeup_count_show(struct device
*dev
,
258 struct device_attribute
*attr
, char *buf
)
260 unsigned long count
= 0;
261 bool enabled
= false;
263 spin_lock_irq(&dev
->power
.lock
);
264 if (dev
->power
.wakeup
) {
265 count
= dev
->power
.wakeup
->event_count
;
268 spin_unlock_irq(&dev
->power
.lock
);
269 return enabled
? sprintf(buf
, "%lu\n", count
) : sprintf(buf
, "\n");
272 static DEVICE_ATTR(wakeup_count
, 0444, wakeup_count_show
, NULL
);
274 static ssize_t
wakeup_active_count_show(struct device
*dev
,
275 struct device_attribute
*attr
, char *buf
)
277 unsigned long count
= 0;
278 bool enabled
= false;
280 spin_lock_irq(&dev
->power
.lock
);
281 if (dev
->power
.wakeup
) {
282 count
= dev
->power
.wakeup
->active_count
;
285 spin_unlock_irq(&dev
->power
.lock
);
286 return enabled
? sprintf(buf
, "%lu\n", count
) : sprintf(buf
, "\n");
289 static DEVICE_ATTR(wakeup_active_count
, 0444, wakeup_active_count_show
, NULL
);
291 static ssize_t
wakeup_hit_count_show(struct device
*dev
,
292 struct device_attribute
*attr
, char *buf
)
294 unsigned long count
= 0;
295 bool enabled
= false;
297 spin_lock_irq(&dev
->power
.lock
);
298 if (dev
->power
.wakeup
) {
299 count
= dev
->power
.wakeup
->hit_count
;
302 spin_unlock_irq(&dev
->power
.lock
);
303 return enabled
? sprintf(buf
, "%lu\n", count
) : sprintf(buf
, "\n");
306 static DEVICE_ATTR(wakeup_hit_count
, 0444, wakeup_hit_count_show
, NULL
);
308 static ssize_t
wakeup_active_show(struct device
*dev
,
309 struct device_attribute
*attr
, char *buf
)
311 unsigned int active
= 0;
312 bool enabled
= false;
314 spin_lock_irq(&dev
->power
.lock
);
315 if (dev
->power
.wakeup
) {
316 active
= dev
->power
.wakeup
->active
;
319 spin_unlock_irq(&dev
->power
.lock
);
320 return enabled
? sprintf(buf
, "%u\n", active
) : sprintf(buf
, "\n");
323 static DEVICE_ATTR(wakeup_active
, 0444, wakeup_active_show
, NULL
);
325 static ssize_t
wakeup_total_time_show(struct device
*dev
,
326 struct device_attribute
*attr
, char *buf
)
329 bool enabled
= false;
331 spin_lock_irq(&dev
->power
.lock
);
332 if (dev
->power
.wakeup
) {
333 msec
= ktime_to_ms(dev
->power
.wakeup
->total_time
);
336 spin_unlock_irq(&dev
->power
.lock
);
337 return enabled
? sprintf(buf
, "%lld\n", msec
) : sprintf(buf
, "\n");
340 static DEVICE_ATTR(wakeup_total_time_ms
, 0444, wakeup_total_time_show
, NULL
);
342 static ssize_t
wakeup_max_time_show(struct device
*dev
,
343 struct device_attribute
*attr
, char *buf
)
346 bool enabled
= false;
348 spin_lock_irq(&dev
->power
.lock
);
349 if (dev
->power
.wakeup
) {
350 msec
= ktime_to_ms(dev
->power
.wakeup
->max_time
);
353 spin_unlock_irq(&dev
->power
.lock
);
354 return enabled
? sprintf(buf
, "%lld\n", msec
) : sprintf(buf
, "\n");
357 static DEVICE_ATTR(wakeup_max_time_ms
, 0444, wakeup_max_time_show
, NULL
);
359 static ssize_t
wakeup_last_time_show(struct device
*dev
,
360 struct device_attribute
*attr
, char *buf
)
363 bool enabled
= false;
365 spin_lock_irq(&dev
->power
.lock
);
366 if (dev
->power
.wakeup
) {
367 msec
= ktime_to_ms(dev
->power
.wakeup
->last_time
);
370 spin_unlock_irq(&dev
->power
.lock
);
371 return enabled
? sprintf(buf
, "%lld\n", msec
) : sprintf(buf
, "\n");
374 static DEVICE_ATTR(wakeup_last_time_ms
, 0444, wakeup_last_time_show
, NULL
);
375 #endif /* CONFIG_PM_SLEEP */
377 #ifdef CONFIG_PM_ADVANCED_DEBUG
378 #ifdef CONFIG_PM_RUNTIME
380 static ssize_t
rtpm_usagecount_show(struct device
*dev
,
381 struct device_attribute
*attr
, char *buf
)
383 return sprintf(buf
, "%d\n", atomic_read(&dev
->power
.usage_count
));
386 static ssize_t
rtpm_children_show(struct device
*dev
,
387 struct device_attribute
*attr
, char *buf
)
389 return sprintf(buf
, "%d\n", dev
->power
.ignore_children
?
390 0 : atomic_read(&dev
->power
.child_count
));
393 static ssize_t
rtpm_enabled_show(struct device
*dev
,
394 struct device_attribute
*attr
, char *buf
)
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");
405 static DEVICE_ATTR(runtime_usage
, 0444, rtpm_usagecount_show
, NULL
);
406 static DEVICE_ATTR(runtime_active_kids
, 0444, rtpm_children_show
, NULL
);
407 static DEVICE_ATTR(runtime_enabled
, 0444, rtpm_enabled_show
, NULL
);
411 static ssize_t
async_show(struct device
*dev
, struct device_attribute
*attr
,
414 return sprintf(buf
, "%s\n",
415 device_async_suspend_enabled(dev
) ? enabled
: disabled
);
418 static ssize_t
async_store(struct device
*dev
, struct device_attribute
*attr
,
419 const char *buf
, size_t n
)
424 cp
= memchr(buf
, '\n', n
);
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
);
436 static DEVICE_ATTR(async
, 0644, async_show
, async_store
);
437 #endif /* CONFIG_PM_ADVANCED_DEBUG */
439 static struct attribute
*power_attrs
[] = {
440 #ifdef CONFIG_PM_ADVANCED_DEBUG
441 #ifdef CONFIG_PM_SLEEP
442 &dev_attr_async
.attr
,
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
,
450 #endif /* CONFIG_PM_ADVANCED_DEBUG */
453 static struct attribute_group pm_attr_group
= {
454 .name
= power_group_name
,
455 .attrs
= power_attrs
,
458 static struct attribute
*wakeup_attrs
[] = {
459 #ifdef CONFIG_PM_SLEEP
460 &dev_attr_wakeup
.attr
,
461 &dev_attr_wakeup_count
.attr
,
462 &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
,
471 static struct attribute_group pm_wakeup_attr_group
= {
472 .name
= power_group_name
,
473 .attrs
= wakeup_attrs
,
476 static struct attribute
*runtime_attrs
[] = {
477 #ifdef CONFIG_PM_RUNTIME
478 #ifndef CONFIG_PM_ADVANCED_DEBUG
479 &dev_attr_runtime_status
.attr
,
481 &dev_attr_control
.attr
,
482 &dev_attr_runtime_suspended_time
.attr
,
483 &dev_attr_runtime_active_time
.attr
,
484 &dev_attr_autosuspend_delay_ms
.attr
,
485 #endif /* CONFIG_PM_RUNTIME */
488 static struct attribute_group pm_runtime_attr_group
= {
489 .name
= power_group_name
,
490 .attrs
= runtime_attrs
,
493 int dpm_sysfs_add(struct device
*dev
)
497 rc
= sysfs_create_group(&dev
->kobj
, &pm_attr_group
);
501 if (pm_runtime_callbacks_present(dev
)) {
502 rc
= sysfs_merge_group(&dev
->kobj
, &pm_runtime_attr_group
);
507 if (device_can_wakeup(dev
)) {
508 rc
= sysfs_merge_group(&dev
->kobj
, &pm_wakeup_attr_group
);
510 if (pm_runtime_callbacks_present(dev
))
511 sysfs_unmerge_group(&dev
->kobj
,
512 &pm_runtime_attr_group
);
519 sysfs_remove_group(&dev
->kobj
, &pm_attr_group
);
523 int wakeup_sysfs_add(struct device
*dev
)
525 return sysfs_merge_group(&dev
->kobj
, &pm_wakeup_attr_group
);
528 void wakeup_sysfs_remove(struct device
*dev
)
530 sysfs_unmerge_group(&dev
->kobj
, &pm_wakeup_attr_group
);
533 void rpm_sysfs_remove(struct device
*dev
)
535 sysfs_unmerge_group(&dev
->kobj
, &pm_runtime_attr_group
);
538 void dpm_sysfs_remove(struct device
*dev
)
540 rpm_sysfs_remove(dev
);
541 sysfs_unmerge_group(&dev
->kobj
, &pm_wakeup_attr_group
);
542 sysfs_remove_group(&dev
->kobj
, &pm_attr_group
);