1 // SPDX-License-Identifier: GPL-2.0
2 /* sysfs entries for device PM */
3 #include <linux/device.h>
4 #include <linux/string.h>
5 #include <linux/export.h>
6 #include <linux/pm_qos.h>
7 #include <linux/pm_runtime.h>
8 #include <linux/atomic.h>
9 #include <linux/jiffies.h>
13 * control - Report/change current runtime PM setting of the device
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:
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;
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
28 * wakeup - Report/change current wakeup option for device
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:
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.
38 * (For example, unconfigured USB devices can't issue wakeups.)
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
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.
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.
60 * async - Report/change current async suspend setting for the device
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.
67 * All devices have one of the following two values for power/async:
69 * + "enabled\n" to permit the asynchronous suspend/resume of the device;
70 * + "disabled\n" to forbid it;
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.
78 * autosuspend_delay_ms - Report/change a device's autosuspend_delay value
80 * Some drivers don't want to carry out a runtime suspend as soon as a
81 * device becomes idle; they want it always to remain idle for some period
82 * of time before suspending it. This period is the autosuspend_delay
83 * value (expressed in milliseconds) and it can be controlled by the user.
84 * If the value is negative then the device will never be runtime
87 * NOTE: The autosuspend_delay_ms attribute and the autosuspend_delay
88 * value are used only if the driver calls pm_runtime_use_autosuspend().
90 * wakeup_count - Report the number of wakeup events related to the device
93 const char power_group_name
[] = "power";
94 EXPORT_SYMBOL_GPL(power_group_name
);
96 static const char ctrl_auto
[] = "auto";
97 static const char ctrl_on
[] = "on";
99 static ssize_t
control_show(struct device
*dev
, struct device_attribute
*attr
,
102 return sprintf(buf
, "%s\n",
103 dev
->power
.runtime_auto
? ctrl_auto
: ctrl_on
);
106 static ssize_t
control_store(struct device
* dev
, struct device_attribute
*attr
,
107 const char * buf
, size_t n
)
110 if (sysfs_streq(buf
, ctrl_auto
))
111 pm_runtime_allow(dev
);
112 else if (sysfs_streq(buf
, ctrl_on
))
113 pm_runtime_forbid(dev
);
120 static DEVICE_ATTR_RW(control
);
122 static ssize_t
runtime_active_time_show(struct device
*dev
,
123 struct device_attribute
*attr
, char *buf
)
126 u64 tmp
= pm_runtime_active_time(dev
);
127 do_div(tmp
, NSEC_PER_MSEC
);
128 ret
= sprintf(buf
, "%llu\n", tmp
);
132 static DEVICE_ATTR_RO(runtime_active_time
);
134 static ssize_t
runtime_suspended_time_show(struct device
*dev
,
135 struct device_attribute
*attr
, char *buf
)
138 u64 tmp
= pm_runtime_suspended_time(dev
);
139 do_div(tmp
, NSEC_PER_MSEC
);
140 ret
= sprintf(buf
, "%llu\n", tmp
);
144 static DEVICE_ATTR_RO(runtime_suspended_time
);
146 static ssize_t
runtime_status_show(struct device
*dev
,
147 struct device_attribute
*attr
, char *buf
)
151 if (dev
->power
.runtime_error
) {
153 } else if (dev
->power
.disable_depth
) {
156 switch (dev
->power
.runtime_status
) {
173 return sprintf(buf
, p
);
176 static DEVICE_ATTR_RO(runtime_status
);
178 static ssize_t
autosuspend_delay_ms_show(struct device
*dev
,
179 struct device_attribute
*attr
, char *buf
)
181 if (!dev
->power
.use_autosuspend
)
183 return sprintf(buf
, "%d\n", dev
->power
.autosuspend_delay
);
186 static ssize_t
autosuspend_delay_ms_store(struct device
*dev
,
187 struct device_attribute
*attr
, const char *buf
, size_t n
)
191 if (!dev
->power
.use_autosuspend
)
194 if (kstrtol(buf
, 10, &delay
) != 0 || delay
!= (int) delay
)
198 pm_runtime_set_autosuspend_delay(dev
, delay
);
203 static DEVICE_ATTR_RW(autosuspend_delay_ms
);
205 static ssize_t
pm_qos_resume_latency_us_show(struct device
*dev
,
206 struct device_attribute
*attr
,
209 s32 value
= dev_pm_qos_requested_resume_latency(dev
);
212 return sprintf(buf
, "n/a\n");
213 if (value
== PM_QOS_RESUME_LATENCY_NO_CONSTRAINT
)
216 return sprintf(buf
, "%d\n", value
);
219 static ssize_t
pm_qos_resume_latency_us_store(struct device
*dev
,
220 struct device_attribute
*attr
,
221 const char *buf
, size_t n
)
226 if (!kstrtos32(buf
, 0, &value
)) {
228 * Prevent users from writing negative or "no constraint" values
231 if (value
< 0 || value
== PM_QOS_RESUME_LATENCY_NO_CONSTRAINT
)
235 value
= PM_QOS_RESUME_LATENCY_NO_CONSTRAINT
;
236 } else if (sysfs_streq(buf
, "n/a")) {
242 ret
= dev_pm_qos_update_request(dev
->power
.qos
->resume_latency_req
,
244 return ret
< 0 ? ret
: n
;
247 static DEVICE_ATTR_RW(pm_qos_resume_latency_us
);
249 static ssize_t
pm_qos_latency_tolerance_us_show(struct device
*dev
,
250 struct device_attribute
*attr
,
253 s32 value
= dev_pm_qos_get_user_latency_tolerance(dev
);
256 return sprintf(buf
, "auto\n");
257 if (value
== PM_QOS_LATENCY_ANY
)
258 return sprintf(buf
, "any\n");
260 return sprintf(buf
, "%d\n", value
);
263 static ssize_t
pm_qos_latency_tolerance_us_store(struct device
*dev
,
264 struct device_attribute
*attr
,
265 const char *buf
, size_t n
)
270 if (kstrtos32(buf
, 0, &value
) == 0) {
271 /* Users can't write negative values directly */
275 if (sysfs_streq(buf
, "auto"))
276 value
= PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT
;
277 else if (sysfs_streq(buf
, "any"))
278 value
= PM_QOS_LATENCY_ANY
;
282 ret
= dev_pm_qos_update_user_latency_tolerance(dev
, value
);
283 return ret
< 0 ? ret
: n
;
286 static DEVICE_ATTR_RW(pm_qos_latency_tolerance_us
);
288 static ssize_t
pm_qos_no_power_off_show(struct device
*dev
,
289 struct device_attribute
*attr
,
292 return sprintf(buf
, "%d\n", !!(dev_pm_qos_requested_flags(dev
)
293 & PM_QOS_FLAG_NO_POWER_OFF
));
296 static ssize_t
pm_qos_no_power_off_store(struct device
*dev
,
297 struct device_attribute
*attr
,
298 const char *buf
, size_t n
)
302 if (kstrtoint(buf
, 0, &ret
))
305 if (ret
!= 0 && ret
!= 1)
308 ret
= dev_pm_qos_update_flags(dev
, PM_QOS_FLAG_NO_POWER_OFF
, ret
);
309 return ret
< 0 ? ret
: n
;
312 static DEVICE_ATTR_RW(pm_qos_no_power_off
);
314 #ifdef CONFIG_PM_SLEEP
315 static const char _enabled
[] = "enabled";
316 static const char _disabled
[] = "disabled";
318 static ssize_t
wakeup_show(struct device
*dev
, struct device_attribute
*attr
,
321 return sprintf(buf
, "%s\n", device_can_wakeup(dev
)
322 ? (device_may_wakeup(dev
) ? _enabled
: _disabled
)
326 static ssize_t
wakeup_store(struct device
*dev
, struct device_attribute
*attr
,
327 const char *buf
, size_t n
)
329 if (!device_can_wakeup(dev
))
332 if (sysfs_streq(buf
, _enabled
))
333 device_set_wakeup_enable(dev
, 1);
334 else if (sysfs_streq(buf
, _disabled
))
335 device_set_wakeup_enable(dev
, 0);
341 static DEVICE_ATTR_RW(wakeup
);
343 static ssize_t
wakeup_count_show(struct device
*dev
,
344 struct device_attribute
*attr
, char *buf
)
346 unsigned long count
= 0;
347 bool enabled
= false;
349 spin_lock_irq(&dev
->power
.lock
);
350 if (dev
->power
.wakeup
) {
351 count
= dev
->power
.wakeup
->wakeup_count
;
354 spin_unlock_irq(&dev
->power
.lock
);
355 return enabled
? sprintf(buf
, "%lu\n", count
) : sprintf(buf
, "\n");
358 static DEVICE_ATTR_RO(wakeup_count
);
360 static ssize_t
wakeup_active_count_show(struct device
*dev
,
361 struct device_attribute
*attr
,
364 unsigned long count
= 0;
365 bool enabled
= false;
367 spin_lock_irq(&dev
->power
.lock
);
368 if (dev
->power
.wakeup
) {
369 count
= dev
->power
.wakeup
->active_count
;
372 spin_unlock_irq(&dev
->power
.lock
);
373 return enabled
? sprintf(buf
, "%lu\n", count
) : sprintf(buf
, "\n");
376 static DEVICE_ATTR_RO(wakeup_active_count
);
378 static ssize_t
wakeup_abort_count_show(struct device
*dev
,
379 struct device_attribute
*attr
,
382 unsigned long count
= 0;
383 bool enabled
= false;
385 spin_lock_irq(&dev
->power
.lock
);
386 if (dev
->power
.wakeup
) {
387 count
= dev
->power
.wakeup
->wakeup_count
;
390 spin_unlock_irq(&dev
->power
.lock
);
391 return enabled
? sprintf(buf
, "%lu\n", count
) : sprintf(buf
, "\n");
394 static DEVICE_ATTR_RO(wakeup_abort_count
);
396 static ssize_t
wakeup_expire_count_show(struct device
*dev
,
397 struct device_attribute
*attr
,
400 unsigned long count
= 0;
401 bool enabled
= false;
403 spin_lock_irq(&dev
->power
.lock
);
404 if (dev
->power
.wakeup
) {
405 count
= dev
->power
.wakeup
->expire_count
;
408 spin_unlock_irq(&dev
->power
.lock
);
409 return enabled
? sprintf(buf
, "%lu\n", count
) : sprintf(buf
, "\n");
412 static DEVICE_ATTR_RO(wakeup_expire_count
);
414 static ssize_t
wakeup_active_show(struct device
*dev
,
415 struct device_attribute
*attr
, char *buf
)
417 unsigned int active
= 0;
418 bool enabled
= false;
420 spin_lock_irq(&dev
->power
.lock
);
421 if (dev
->power
.wakeup
) {
422 active
= dev
->power
.wakeup
->active
;
425 spin_unlock_irq(&dev
->power
.lock
);
426 return enabled
? sprintf(buf
, "%u\n", active
) : sprintf(buf
, "\n");
429 static DEVICE_ATTR_RO(wakeup_active
);
431 static ssize_t
wakeup_total_time_ms_show(struct device
*dev
,
432 struct device_attribute
*attr
,
436 bool enabled
= false;
438 spin_lock_irq(&dev
->power
.lock
);
439 if (dev
->power
.wakeup
) {
440 msec
= ktime_to_ms(dev
->power
.wakeup
->total_time
);
443 spin_unlock_irq(&dev
->power
.lock
);
444 return enabled
? sprintf(buf
, "%lld\n", msec
) : sprintf(buf
, "\n");
447 static DEVICE_ATTR_RO(wakeup_total_time_ms
);
449 static ssize_t
wakeup_max_time_ms_show(struct device
*dev
,
450 struct device_attribute
*attr
, char *buf
)
453 bool enabled
= false;
455 spin_lock_irq(&dev
->power
.lock
);
456 if (dev
->power
.wakeup
) {
457 msec
= ktime_to_ms(dev
->power
.wakeup
->max_time
);
460 spin_unlock_irq(&dev
->power
.lock
);
461 return enabled
? sprintf(buf
, "%lld\n", msec
) : sprintf(buf
, "\n");
464 static DEVICE_ATTR_RO(wakeup_max_time_ms
);
466 static ssize_t
wakeup_last_time_ms_show(struct device
*dev
,
467 struct device_attribute
*attr
,
471 bool enabled
= false;
473 spin_lock_irq(&dev
->power
.lock
);
474 if (dev
->power
.wakeup
) {
475 msec
= ktime_to_ms(dev
->power
.wakeup
->last_time
);
478 spin_unlock_irq(&dev
->power
.lock
);
479 return enabled
? sprintf(buf
, "%lld\n", msec
) : sprintf(buf
, "\n");
482 static DEVICE_ATTR_RO(wakeup_last_time_ms
);
484 #ifdef CONFIG_PM_AUTOSLEEP
485 static ssize_t
wakeup_prevent_sleep_time_ms_show(struct device
*dev
,
486 struct device_attribute
*attr
,
490 bool enabled
= false;
492 spin_lock_irq(&dev
->power
.lock
);
493 if (dev
->power
.wakeup
) {
494 msec
= ktime_to_ms(dev
->power
.wakeup
->prevent_sleep_time
);
497 spin_unlock_irq(&dev
->power
.lock
);
498 return enabled
? sprintf(buf
, "%lld\n", msec
) : sprintf(buf
, "\n");
501 static DEVICE_ATTR_RO(wakeup_prevent_sleep_time_ms
);
502 #endif /* CONFIG_PM_AUTOSLEEP */
503 #endif /* CONFIG_PM_SLEEP */
505 #ifdef CONFIG_PM_ADVANCED_DEBUG
506 static ssize_t
runtime_usage_show(struct device
*dev
,
507 struct device_attribute
*attr
, char *buf
)
509 return sprintf(buf
, "%d\n", atomic_read(&dev
->power
.usage_count
));
511 static DEVICE_ATTR_RO(runtime_usage
);
513 static ssize_t
runtime_active_kids_show(struct device
*dev
,
514 struct device_attribute
*attr
,
517 return sprintf(buf
, "%d\n", dev
->power
.ignore_children
?
518 0 : atomic_read(&dev
->power
.child_count
));
520 static DEVICE_ATTR_RO(runtime_active_kids
);
522 static ssize_t
runtime_enabled_show(struct device
*dev
,
523 struct device_attribute
*attr
, char *buf
)
525 if (dev
->power
.disable_depth
&& (dev
->power
.runtime_auto
== false))
526 return sprintf(buf
, "disabled & forbidden\n");
527 if (dev
->power
.disable_depth
)
528 return sprintf(buf
, "disabled\n");
529 if (dev
->power
.runtime_auto
== false)
530 return sprintf(buf
, "forbidden\n");
531 return sprintf(buf
, "enabled\n");
533 static DEVICE_ATTR_RO(runtime_enabled
);
535 #ifdef CONFIG_PM_SLEEP
536 static ssize_t
async_show(struct device
*dev
, struct device_attribute
*attr
,
539 return sprintf(buf
, "%s\n",
540 device_async_suspend_enabled(dev
) ?
541 _enabled
: _disabled
);
544 static ssize_t
async_store(struct device
*dev
, struct device_attribute
*attr
,
545 const char *buf
, size_t n
)
547 if (sysfs_streq(buf
, _enabled
))
548 device_enable_async_suspend(dev
);
549 else if (sysfs_streq(buf
, _disabled
))
550 device_disable_async_suspend(dev
);
556 static DEVICE_ATTR_RW(async
);
558 #endif /* CONFIG_PM_SLEEP */
559 #endif /* CONFIG_PM_ADVANCED_DEBUG */
561 static struct attribute
*power_attrs
[] = {
562 #ifdef CONFIG_PM_ADVANCED_DEBUG
563 #ifdef CONFIG_PM_SLEEP
564 &dev_attr_async
.attr
,
566 &dev_attr_runtime_status
.attr
,
567 &dev_attr_runtime_usage
.attr
,
568 &dev_attr_runtime_active_kids
.attr
,
569 &dev_attr_runtime_enabled
.attr
,
570 #endif /* CONFIG_PM_ADVANCED_DEBUG */
573 static const struct attribute_group pm_attr_group
= {
574 .name
= power_group_name
,
575 .attrs
= power_attrs
,
578 static struct attribute
*wakeup_attrs
[] = {
579 #ifdef CONFIG_PM_SLEEP
580 &dev_attr_wakeup
.attr
,
581 &dev_attr_wakeup_count
.attr
,
582 &dev_attr_wakeup_active_count
.attr
,
583 &dev_attr_wakeup_abort_count
.attr
,
584 &dev_attr_wakeup_expire_count
.attr
,
585 &dev_attr_wakeup_active
.attr
,
586 &dev_attr_wakeup_total_time_ms
.attr
,
587 &dev_attr_wakeup_max_time_ms
.attr
,
588 &dev_attr_wakeup_last_time_ms
.attr
,
589 #ifdef CONFIG_PM_AUTOSLEEP
590 &dev_attr_wakeup_prevent_sleep_time_ms
.attr
,
595 static const struct attribute_group pm_wakeup_attr_group
= {
596 .name
= power_group_name
,
597 .attrs
= wakeup_attrs
,
600 static struct attribute
*runtime_attrs
[] = {
601 #ifndef CONFIG_PM_ADVANCED_DEBUG
602 &dev_attr_runtime_status
.attr
,
604 &dev_attr_control
.attr
,
605 &dev_attr_runtime_suspended_time
.attr
,
606 &dev_attr_runtime_active_time
.attr
,
607 &dev_attr_autosuspend_delay_ms
.attr
,
610 static const struct attribute_group pm_runtime_attr_group
= {
611 .name
= power_group_name
,
612 .attrs
= runtime_attrs
,
615 static struct attribute
*pm_qos_resume_latency_attrs
[] = {
616 &dev_attr_pm_qos_resume_latency_us
.attr
,
619 static const struct attribute_group pm_qos_resume_latency_attr_group
= {
620 .name
= power_group_name
,
621 .attrs
= pm_qos_resume_latency_attrs
,
624 static struct attribute
*pm_qos_latency_tolerance_attrs
[] = {
625 &dev_attr_pm_qos_latency_tolerance_us
.attr
,
628 static const struct attribute_group pm_qos_latency_tolerance_attr_group
= {
629 .name
= power_group_name
,
630 .attrs
= pm_qos_latency_tolerance_attrs
,
633 static struct attribute
*pm_qos_flags_attrs
[] = {
634 &dev_attr_pm_qos_no_power_off
.attr
,
637 static const struct attribute_group pm_qos_flags_attr_group
= {
638 .name
= power_group_name
,
639 .attrs
= pm_qos_flags_attrs
,
642 int dpm_sysfs_add(struct device
*dev
)
646 /* No need to create PM sysfs if explicitly disabled. */
647 if (device_pm_not_required(dev
))
650 rc
= sysfs_create_group(&dev
->kobj
, &pm_attr_group
);
654 if (pm_runtime_callbacks_present(dev
)) {
655 rc
= sysfs_merge_group(&dev
->kobj
, &pm_runtime_attr_group
);
659 if (device_can_wakeup(dev
)) {
660 rc
= sysfs_merge_group(&dev
->kobj
, &pm_wakeup_attr_group
);
664 if (dev
->power
.set_latency_tolerance
) {
665 rc
= sysfs_merge_group(&dev
->kobj
,
666 &pm_qos_latency_tolerance_attr_group
);
673 sysfs_unmerge_group(&dev
->kobj
, &pm_wakeup_attr_group
);
675 sysfs_unmerge_group(&dev
->kobj
, &pm_runtime_attr_group
);
677 sysfs_remove_group(&dev
->kobj
, &pm_attr_group
);
681 int wakeup_sysfs_add(struct device
*dev
)
683 return sysfs_merge_group(&dev
->kobj
, &pm_wakeup_attr_group
);
686 void wakeup_sysfs_remove(struct device
*dev
)
688 sysfs_unmerge_group(&dev
->kobj
, &pm_wakeup_attr_group
);
691 int pm_qos_sysfs_add_resume_latency(struct device
*dev
)
693 return sysfs_merge_group(&dev
->kobj
, &pm_qos_resume_latency_attr_group
);
696 void pm_qos_sysfs_remove_resume_latency(struct device
*dev
)
698 sysfs_unmerge_group(&dev
->kobj
, &pm_qos_resume_latency_attr_group
);
701 int pm_qos_sysfs_add_flags(struct device
*dev
)
703 return sysfs_merge_group(&dev
->kobj
, &pm_qos_flags_attr_group
);
706 void pm_qos_sysfs_remove_flags(struct device
*dev
)
708 sysfs_unmerge_group(&dev
->kobj
, &pm_qos_flags_attr_group
);
711 int pm_qos_sysfs_add_latency_tolerance(struct device
*dev
)
713 return sysfs_merge_group(&dev
->kobj
,
714 &pm_qos_latency_tolerance_attr_group
);
717 void pm_qos_sysfs_remove_latency_tolerance(struct device
*dev
)
719 sysfs_unmerge_group(&dev
->kobj
, &pm_qos_latency_tolerance_attr_group
);
722 void rpm_sysfs_remove(struct device
*dev
)
724 sysfs_unmerge_group(&dev
->kobj
, &pm_runtime_attr_group
);
727 void dpm_sysfs_remove(struct device
*dev
)
729 if (device_pm_not_required(dev
))
731 sysfs_unmerge_group(&dev
->kobj
, &pm_qos_latency_tolerance_attr_group
);
732 dev_pm_qos_constraints_destroy(dev
);
733 rpm_sysfs_remove(dev
);
734 sysfs_unmerge_group(&dev
->kobj
, &pm_wakeup_attr_group
);
735 sysfs_remove_group(&dev
->kobj
, &pm_attr_group
);