blob: 662632ac5e0e9750fd370a128f93fd8b75d70889 [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. Wysocki85dc0b82012-03-13 01:01:39 +01008#include <linux/pm_qos.h>
Rafael J. Wysocki53823632010-01-23 22:02:51 +01009#include <linux/pm_runtime.h>
Arun Sharma600634972011-07-26 16:09:06 -070010#include <linux/atomic.h>
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +020011#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "power.h"
13
David Brownell0ac85242005-09-12 19:39:34 -070014/*
Rafael J. Wysocki53823632010-01-23 22:02:51 +010015 * control - Report/change current runtime PM setting of the device
16 *
17 * Runtime power management of a device can be blocked with the help of
18 * this attribute. All devices have one of the following two values for
19 * the power/control file:
20 *
21 * + "auto\n" to allow the device to be power managed at run time;
22 * + "on\n" to prevent the device from being power managed at run time;
23 *
24 * The default for all devices is "auto", which means that devices may be
25 * subject to automatic power management, depending on their drivers.
26 * Changing this attribute to "on" prevents the driver from power managing
27 * the device at run time. Doing that while the device is suspended causes
28 * it to be woken up.
29 *
David Brownell0ac85242005-09-12 19:39:34 -070030 * wakeup - Report/change current wakeup option for device
31 *
32 * Some devices support "wakeup" events, which are hardware signals
33 * used to activate devices from suspended or low power states. Such
34 * devices have one of three values for the sysfs power/wakeup file:
35 *
36 * + "enabled\n" to issue the events;
37 * + "disabled\n" not to do so; or
38 * + "\n" for temporary or permanent inability to issue wakeup.
39 *
40 * (For example, unconfigured USB devices can't issue wakeups.)
41 *
42 * Familiar examples of devices that can issue wakeup events include
43 * keyboards and mice (both PS2 and USB styles), power buttons, modems,
44 * "Wake-On-LAN" Ethernet links, GPIO lines, and more. Some events
45 * will wake the entire system from a suspend state; others may just
46 * wake up the device (if the system as a whole is already active).
47 * Some wakeup events use normal IRQ lines; other use special out
48 * of band signaling.
49 *
50 * It is the responsibility of device drivers to enable (or disable)
51 * wakeup signaling as part of changing device power states, respecting
52 * the policy choices provided through the driver model.
53 *
54 * Devices may not be able to generate wakeup events from all power
55 * states. Also, the events may be ignored in some configurations;
56 * for example, they might need help from other devices that aren't
57 * active, or which may have wakeup disabled. Some drivers rely on
58 * wakeup events internally (unless they are disabled), keeping
59 * their hardware in low power modes whenever they're unused. This
60 * saves runtime power, without requiring system-wide sleep states.
Rafael J. Wysocki5a2eb852010-01-23 22:25:23 +010061 *
62 * async - Report/change current async suspend setting for the device
63 *
64 * Asynchronous suspend and resume of the device during system-wide power
65 * state transitions can be enabled by writing "enabled" to this file.
66 * Analogously, if "disabled" is written to this file, the device will be
67 * suspended and resumed synchronously.
68 *
69 * All devices have one of the following two values for power/async:
70 *
71 * + "enabled\n" to permit the asynchronous suspend/resume of the device;
72 * + "disabled\n" to forbid it;
73 *
74 * NOTE: It generally is unsafe to permit the asynchronous suspend/resume
75 * of a device unless it is certain that all of the PM dependencies of the
76 * device are known to the PM core. However, for some devices this
77 * attribute is set to "enabled" by bus type code or device drivers and in
78 * that cases it should be safe to leave the default value.
Rafael J. Wysockic125e962010-07-05 22:43:53 +020079 *
Alan Stern15bcb91d2010-09-25 23:35:21 +020080 * autosuspend_delay_ms - Report/change a device's autosuspend_delay value
81 *
82 * Some drivers don't want to carry out a runtime suspend as soon as a
83 * device becomes idle; they want it always to remain idle for some period
84 * of time before suspending it. This period is the autosuspend_delay
85 * value (expressed in milliseconds) and it can be controlled by the user.
86 * If the value is negative then the device will never be runtime
87 * suspended.
88 *
89 * NOTE: The autosuspend_delay_ms attribute and the autosuspend_delay
90 * value are used only if the driver calls pm_runtime_use_autosuspend().
91 *
Rafael J. Wysockic125e962010-07-05 22:43:53 +020092 * wakeup_count - Report the number of wakeup events related to the device
David Brownell0ac85242005-09-12 19:39:34 -070093 */
94
Alan Stern7490e442010-09-25 23:35:15 +020095const char power_group_name[] = "power";
96EXPORT_SYMBOL_GPL(power_group_name);
97
Rafael J. Wysocki53823632010-01-23 22:02:51 +010098static const char ctrl_auto[] = "auto";
99static const char ctrl_on[] = "on";
100
101static ssize_t control_show(struct device *dev, struct device_attribute *attr,
102 char *buf)
103{
104 return sprintf(buf, "%s\n",
105 dev->power.runtime_auto ? ctrl_auto : ctrl_on);
106}
107
108static ssize_t control_store(struct device * dev, struct device_attribute *attr,
109 const char * buf, size_t n)
110{
Alan Stern69c843b2011-07-06 10:52:23 +0200111 device_lock(dev);
Andy Shevchenko325c4b32017-11-10 20:28:07 +0200112 if (sysfs_streq(buf, ctrl_auto))
Rafael J. Wysocki53823632010-01-23 22:02:51 +0100113 pm_runtime_allow(dev);
Andy Shevchenko325c4b32017-11-10 20:28:07 +0200114 else if (sysfs_streq(buf, ctrl_on))
Rafael J. Wysocki53823632010-01-23 22:02:51 +0100115 pm_runtime_forbid(dev);
116 else
Alan Stern69c843b2011-07-06 10:52:23 +0200117 n = -EINVAL;
118 device_unlock(dev);
Rafael J. Wysocki53823632010-01-23 22:02:51 +0100119 return n;
120}
121
122static DEVICE_ATTR(control, 0644, control_show, control_store);
Alan Stern0fcb4ee2010-07-08 00:05:37 +0200123
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200124static ssize_t rtpm_active_time_show(struct device *dev,
125 struct device_attribute *attr, char *buf)
126{
127 int ret;
128 spin_lock_irq(&dev->power.lock);
129 update_pm_runtime_accounting(dev);
130 ret = sprintf(buf, "%i\n", jiffies_to_msecs(dev->power.active_jiffies));
131 spin_unlock_irq(&dev->power.lock);
132 return ret;
133}
134
135static DEVICE_ATTR(runtime_active_time, 0444, rtpm_active_time_show, NULL);
136
137static ssize_t rtpm_suspended_time_show(struct device *dev,
138 struct device_attribute *attr, char *buf)
139{
140 int ret;
141 spin_lock_irq(&dev->power.lock);
142 update_pm_runtime_accounting(dev);
143 ret = sprintf(buf, "%i\n",
144 jiffies_to_msecs(dev->power.suspended_jiffies));
145 spin_unlock_irq(&dev->power.lock);
146 return ret;
147}
148
149static DEVICE_ATTR(runtime_suspended_time, 0444, rtpm_suspended_time_show, NULL);
150
Alan Stern0fcb4ee2010-07-08 00:05:37 +0200151static ssize_t rtpm_status_show(struct device *dev,
152 struct device_attribute *attr, char *buf)
153{
154 const char *p;
155
156 if (dev->power.runtime_error) {
157 p = "error\n";
158 } else if (dev->power.disable_depth) {
159 p = "unsupported\n";
160 } else {
161 switch (dev->power.runtime_status) {
162 case RPM_SUSPENDED:
163 p = "suspended\n";
164 break;
165 case RPM_SUSPENDING:
166 p = "suspending\n";
167 break;
168 case RPM_RESUMING:
169 p = "resuming\n";
170 break;
171 case RPM_ACTIVE:
172 p = "active\n";
173 break;
174 default:
175 return -EIO;
176 }
177 }
178 return sprintf(buf, p);
179}
180
181static DEVICE_ATTR(runtime_status, 0444, rtpm_status_show, NULL);
Alan Stern15bcb91d2010-09-25 23:35:21 +0200182
183static ssize_t autosuspend_delay_ms_show(struct device *dev,
184 struct device_attribute *attr, char *buf)
185{
186 if (!dev->power.use_autosuspend)
187 return -EIO;
188 return sprintf(buf, "%d\n", dev->power.autosuspend_delay);
189}
190
191static ssize_t autosuspend_delay_ms_store(struct device *dev,
192 struct device_attribute *attr, const char *buf, size_t n)
193{
194 long delay;
195
196 if (!dev->power.use_autosuspend)
197 return -EIO;
198
Jingoo Han34da5e62013-07-26 13:10:22 +0900199 if (kstrtol(buf, 10, &delay) != 0 || delay != (int) delay)
Alan Stern15bcb91d2010-09-25 23:35:21 +0200200 return -EINVAL;
201
Alan Stern69c843b2011-07-06 10:52:23 +0200202 device_lock(dev);
Alan Stern15bcb91d2010-09-25 23:35:21 +0200203 pm_runtime_set_autosuspend_delay(dev, delay);
Alan Stern69c843b2011-07-06 10:52:23 +0200204 device_unlock(dev);
Alan Stern15bcb91d2010-09-25 23:35:21 +0200205 return n;
206}
207
208static DEVICE_ATTR(autosuspend_delay_ms, 0644, autosuspend_delay_ms_show,
209 autosuspend_delay_ms_store);
210
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100211static ssize_t pm_qos_resume_latency_show(struct device *dev,
212 struct device_attribute *attr,
213 char *buf)
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100214{
Rafael J. Wysocki0759e802017-11-07 11:33:49 +0100215 s32 value = dev_pm_qos_requested_resume_latency(dev);
216
217 if (value == 0)
218 return sprintf(buf, "n/a\n");
219 else if (value == PM_QOS_RESUME_LATENCY_NO_CONSTRAINT)
220 value = 0;
221
222 return sprintf(buf, "%d\n", value);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100223}
224
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100225static ssize_t pm_qos_resume_latency_store(struct device *dev,
226 struct device_attribute *attr,
227 const char *buf, size_t n)
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100228{
229 s32 value;
230 int ret;
231
Rafael J. Wysocki0759e802017-11-07 11:33:49 +0100232 if (!kstrtos32(buf, 0, &value)) {
233 /*
234 * Prevent users from writing negative or "no constraint" values
235 * directly.
236 */
237 if (value < 0 || value == PM_QOS_RESUME_LATENCY_NO_CONSTRAINT)
238 return -EINVAL;
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100239
Rafael J. Wysocki0759e802017-11-07 11:33:49 +0100240 if (value == 0)
241 value = PM_QOS_RESUME_LATENCY_NO_CONSTRAINT;
Andy Shevchenko325c4b32017-11-10 20:28:07 +0200242 } else if (sysfs_streq(buf, "n/a")) {
Rafael J. Wysocki0759e802017-11-07 11:33:49 +0100243 value = 0;
244 } else {
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100245 return -EINVAL;
Rafael J. Wysocki0759e802017-11-07 11:33:49 +0100246 }
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100247
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100248 ret = dev_pm_qos_update_request(dev->power.qos->resume_latency_req,
249 value);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100250 return ret < 0 ? ret : n;
251}
252
253static DEVICE_ATTR(pm_qos_resume_latency_us, 0644,
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100254 pm_qos_resume_latency_show, pm_qos_resume_latency_store);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200255
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100256static ssize_t pm_qos_latency_tolerance_show(struct device *dev,
257 struct device_attribute *attr,
258 char *buf)
259{
260 s32 value = dev_pm_qos_get_user_latency_tolerance(dev);
261
262 if (value < 0)
263 return sprintf(buf, "auto\n");
264 else if (value == PM_QOS_LATENCY_ANY)
265 return sprintf(buf, "any\n");
266
267 return sprintf(buf, "%d\n", value);
268}
269
270static ssize_t pm_qos_latency_tolerance_store(struct device *dev,
271 struct device_attribute *attr,
272 const char *buf, size_t n)
273{
274 s32 value;
275 int ret;
276
Andrew Lutomirskiee061da2016-11-29 17:11:50 -0800277 if (kstrtos32(buf, 0, &value) == 0) {
278 /* Users can't write negative values directly */
279 if (value < 0)
280 return -EINVAL;
281 } else {
Andy Shevchenko325c4b32017-11-10 20:28:07 +0200282 if (sysfs_streq(buf, "auto"))
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100283 value = PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT;
Andy Shevchenko325c4b32017-11-10 20:28:07 +0200284 else if (sysfs_streq(buf, "any"))
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100285 value = PM_QOS_LATENCY_ANY;
Dan Carpenter2ca30332017-07-10 10:21:40 +0300286 else
287 return -EINVAL;
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100288 }
289 ret = dev_pm_qos_update_user_latency_tolerance(dev, value);
290 return ret < 0 ? ret : n;
291}
292
293static DEVICE_ATTR(pm_qos_latency_tolerance_us, 0644,
294 pm_qos_latency_tolerance_show, pm_qos_latency_tolerance_store);
295
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200296static ssize_t pm_qos_no_power_off_show(struct device *dev,
297 struct device_attribute *attr,
298 char *buf)
299{
300 return sprintf(buf, "%d\n", !!(dev_pm_qos_requested_flags(dev)
301 & PM_QOS_FLAG_NO_POWER_OFF));
302}
303
304static ssize_t pm_qos_no_power_off_store(struct device *dev,
305 struct device_attribute *attr,
306 const char *buf, size_t n)
307{
308 int ret;
309
310 if (kstrtoint(buf, 0, &ret))
311 return -EINVAL;
312
313 if (ret != 0 && ret != 1)
314 return -EINVAL;
315
316 ret = dev_pm_qos_update_flags(dev, PM_QOS_FLAG_NO_POWER_OFF, ret);
317 return ret < 0 ? ret : n;
318}
319
320static DEVICE_ATTR(pm_qos_no_power_off, 0644,
321 pm_qos_no_power_off_show, pm_qos_no_power_off_store);
322
Rafael J. Wysockie7623182011-05-07 18:11:52 +0200323#ifdef CONFIG_PM_SLEEP
Johannes Berg2b4f4312014-09-08 08:48:00 +0200324static const char _enabled[] = "enabled";
325static const char _disabled[] = "disabled";
326
David Brownell0ac85242005-09-12 19:39:34 -0700327static ssize_t
328wake_show(struct device * dev, struct device_attribute *attr, char * buf)
329{
330 return sprintf(buf, "%s\n", device_can_wakeup(dev)
Johannes Berg2b4f4312014-09-08 08:48:00 +0200331 ? (device_may_wakeup(dev) ? _enabled : _disabled)
David Brownell0ac85242005-09-12 19:39:34 -0700332 : "");
333}
334
335static ssize_t
336wake_store(struct device * dev, struct device_attribute *attr,
337 const char * buf, size_t n)
338{
David Brownell0ac85242005-09-12 19:39:34 -0700339 if (!device_can_wakeup(dev))
340 return -EINVAL;
341
Andy Shevchenko325c4b32017-11-10 20:28:07 +0200342 if (sysfs_streq(buf, _enabled))
David Brownell0ac85242005-09-12 19:39:34 -0700343 device_set_wakeup_enable(dev, 1);
Andy Shevchenko325c4b32017-11-10 20:28:07 +0200344 else if (sysfs_streq(buf, _disabled))
David Brownell0ac85242005-09-12 19:39:34 -0700345 device_set_wakeup_enable(dev, 0);
346 else
347 return -EINVAL;
348 return n;
349}
350
351static DEVICE_ATTR(wakeup, 0644, wake_show, wake_store);
352
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200353static ssize_t wakeup_count_show(struct device *dev,
354 struct device_attribute *attr, char *buf)
355{
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200356 unsigned long count = 0;
357 bool enabled = false;
358
359 spin_lock_irq(&dev->power.lock);
360 if (dev->power.wakeup) {
361 count = dev->power.wakeup->event_count;
362 enabled = true;
363 }
364 spin_unlock_irq(&dev->power.lock);
365 return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200366}
367
368static DEVICE_ATTR(wakeup_count, 0444, wakeup_count_show, NULL);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200369
370static ssize_t wakeup_active_count_show(struct device *dev,
371 struct device_attribute *attr, char *buf)
372{
373 unsigned long count = 0;
374 bool enabled = false;
375
376 spin_lock_irq(&dev->power.lock);
377 if (dev->power.wakeup) {
378 count = dev->power.wakeup->active_count;
379 enabled = true;
380 }
381 spin_unlock_irq(&dev->power.lock);
382 return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
383}
384
385static DEVICE_ATTR(wakeup_active_count, 0444, wakeup_active_count_show, NULL);
386
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200387static ssize_t wakeup_abort_count_show(struct device *dev,
388 struct device_attribute *attr,
389 char *buf)
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200390{
391 unsigned long count = 0;
392 bool enabled = false;
393
394 spin_lock_irq(&dev->power.lock);
395 if (dev->power.wakeup) {
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200396 count = dev->power.wakeup->wakeup_count;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200397 enabled = true;
398 }
399 spin_unlock_irq(&dev->power.lock);
400 return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
401}
402
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200403static DEVICE_ATTR(wakeup_abort_count, 0444, wakeup_abort_count_show, NULL);
404
405static ssize_t wakeup_expire_count_show(struct device *dev,
406 struct device_attribute *attr,
407 char *buf)
408{
409 unsigned long count = 0;
410 bool enabled = false;
411
412 spin_lock_irq(&dev->power.lock);
413 if (dev->power.wakeup) {
414 count = dev->power.wakeup->expire_count;
415 enabled = true;
416 }
417 spin_unlock_irq(&dev->power.lock);
418 return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
419}
420
421static DEVICE_ATTR(wakeup_expire_count, 0444, wakeup_expire_count_show, NULL);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200422
423static ssize_t wakeup_active_show(struct device *dev,
424 struct device_attribute *attr, char *buf)
425{
426 unsigned int active = 0;
427 bool enabled = false;
428
429 spin_lock_irq(&dev->power.lock);
430 if (dev->power.wakeup) {
431 active = dev->power.wakeup->active;
432 enabled = true;
433 }
434 spin_unlock_irq(&dev->power.lock);
435 return enabled ? sprintf(buf, "%u\n", active) : sprintf(buf, "\n");
436}
437
438static DEVICE_ATTR(wakeup_active, 0444, wakeup_active_show, NULL);
439
440static ssize_t wakeup_total_time_show(struct device *dev,
441 struct device_attribute *attr, char *buf)
442{
443 s64 msec = 0;
444 bool enabled = false;
445
446 spin_lock_irq(&dev->power.lock);
447 if (dev->power.wakeup) {
448 msec = ktime_to_ms(dev->power.wakeup->total_time);
449 enabled = true;
450 }
451 spin_unlock_irq(&dev->power.lock);
452 return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
453}
454
455static DEVICE_ATTR(wakeup_total_time_ms, 0444, wakeup_total_time_show, NULL);
456
457static ssize_t wakeup_max_time_show(struct device *dev,
458 struct device_attribute *attr, char *buf)
459{
460 s64 msec = 0;
461 bool enabled = false;
462
463 spin_lock_irq(&dev->power.lock);
464 if (dev->power.wakeup) {
465 msec = ktime_to_ms(dev->power.wakeup->max_time);
466 enabled = true;
467 }
468 spin_unlock_irq(&dev->power.lock);
469 return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
470}
471
472static DEVICE_ATTR(wakeup_max_time_ms, 0444, wakeup_max_time_show, NULL);
473
474static ssize_t wakeup_last_time_show(struct device *dev,
475 struct device_attribute *attr, char *buf)
476{
477 s64 msec = 0;
478 bool enabled = false;
479
480 spin_lock_irq(&dev->power.lock);
481 if (dev->power.wakeup) {
482 msec = ktime_to_ms(dev->power.wakeup->last_time);
483 enabled = true;
484 }
485 spin_unlock_irq(&dev->power.lock);
486 return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
487}
488
489static DEVICE_ATTR(wakeup_last_time_ms, 0444, wakeup_last_time_show, NULL);
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200490
491#ifdef CONFIG_PM_AUTOSLEEP
492static ssize_t wakeup_prevent_sleep_time_show(struct device *dev,
493 struct device_attribute *attr,
494 char *buf)
495{
496 s64 msec = 0;
497 bool enabled = false;
498
499 spin_lock_irq(&dev->power.lock);
500 if (dev->power.wakeup) {
501 msec = ktime_to_ms(dev->power.wakeup->prevent_sleep_time);
502 enabled = true;
503 }
504 spin_unlock_irq(&dev->power.lock);
505 return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
506}
507
508static DEVICE_ATTR(wakeup_prevent_sleep_time_ms, 0444,
509 wakeup_prevent_sleep_time_show, NULL);
510#endif /* CONFIG_PM_AUTOSLEEP */
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200511#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200512
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200513#ifdef CONFIG_PM_ADVANCED_DEBUG
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200514static ssize_t rtpm_usagecount_show(struct device *dev,
515 struct device_attribute *attr, char *buf)
516{
517 return sprintf(buf, "%d\n", atomic_read(&dev->power.usage_count));
518}
519
520static ssize_t rtpm_children_show(struct device *dev,
521 struct device_attribute *attr, char *buf)
522{
523 return sprintf(buf, "%d\n", dev->power.ignore_children ?
524 0 : atomic_read(&dev->power.child_count));
525}
526
527static ssize_t rtpm_enabled_show(struct device *dev,
528 struct device_attribute *attr, char *buf)
529{
530 if ((dev->power.disable_depth) && (dev->power.runtime_auto == false))
531 return sprintf(buf, "disabled & forbidden\n");
532 else if (dev->power.disable_depth)
533 return sprintf(buf, "disabled\n");
534 else if (dev->power.runtime_auto == false)
535 return sprintf(buf, "forbidden\n");
536 return sprintf(buf, "enabled\n");
537}
538
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200539static DEVICE_ATTR(runtime_usage, 0444, rtpm_usagecount_show, NULL);
540static DEVICE_ATTR(runtime_active_kids, 0444, rtpm_children_show, NULL);
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200541static DEVICE_ATTR(runtime_enabled, 0444, rtpm_enabled_show, NULL);
542
Rafael J. Wysockid181b492012-07-11 22:42:58 +0200543#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki5a2eb852010-01-23 22:25:23 +0100544static ssize_t async_show(struct device *dev, struct device_attribute *attr,
545 char *buf)
546{
547 return sprintf(buf, "%s\n",
Johannes Berg2b4f4312014-09-08 08:48:00 +0200548 device_async_suspend_enabled(dev) ?
549 _enabled : _disabled);
Rafael J. Wysocki5a2eb852010-01-23 22:25:23 +0100550}
551
552static ssize_t async_store(struct device *dev, struct device_attribute *attr,
553 const char *buf, size_t n)
554{
Andy Shevchenko325c4b32017-11-10 20:28:07 +0200555 if (sysfs_streq(buf, _enabled))
Rafael J. Wysocki5a2eb852010-01-23 22:25:23 +0100556 device_enable_async_suspend(dev);
Andy Shevchenko325c4b32017-11-10 20:28:07 +0200557 else if (sysfs_streq(buf, _disabled))
Rafael J. Wysocki5a2eb852010-01-23 22:25:23 +0100558 device_disable_async_suspend(dev);
559 else
560 return -EINVAL;
561 return n;
562}
563
564static DEVICE_ATTR(async, 0644, async_show, async_store);
Rafael J. Wysockid181b492012-07-11 22:42:58 +0200565
Rafael J. Wysockid30d8192014-11-27 22:38:05 +0100566#endif /* CONFIG_PM_SLEEP */
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200567#endif /* CONFIG_PM_ADVANCED_DEBUG */
David Brownell0ac85242005-09-12 19:39:34 -0700568
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100569static struct attribute *power_attrs[] = {
570#ifdef CONFIG_PM_ADVANCED_DEBUG
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200571#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100572 &dev_attr_async.attr,
573#endif
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100574 &dev_attr_runtime_status.attr,
575 &dev_attr_runtime_usage.attr,
576 &dev_attr_runtime_active_kids.attr,
577 &dev_attr_runtime_enabled.attr,
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100578#endif /* CONFIG_PM_ADVANCED_DEBUG */
579 NULL,
580};
Arvind Yadavdbb1d8b2017-06-22 16:23:32 +0530581static const struct attribute_group pm_attr_group = {
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100582 .name = power_group_name,
583 .attrs = power_attrs,
584};
585
586static struct attribute *wakeup_attrs[] = {
587#ifdef CONFIG_PM_SLEEP
588 &dev_attr_wakeup.attr,
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200589 &dev_attr_wakeup_count.attr,
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200590 &dev_attr_wakeup_active_count.attr,
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200591 &dev_attr_wakeup_abort_count.attr,
592 &dev_attr_wakeup_expire_count.attr,
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200593 &dev_attr_wakeup_active.attr,
594 &dev_attr_wakeup_total_time_ms.attr,
595 &dev_attr_wakeup_max_time_ms.attr,
596 &dev_attr_wakeup_last_time_ms.attr,
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200597#ifdef CONFIG_PM_AUTOSLEEP
598 &dev_attr_wakeup_prevent_sleep_time_ms.attr,
599#endif
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200600#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 NULL,
602};
Arvind Yadavdbb1d8b2017-06-22 16:23:32 +0530603static const struct attribute_group pm_wakeup_attr_group = {
Alan Stern7490e442010-09-25 23:35:15 +0200604 .name = power_group_name,
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100605 .attrs = wakeup_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606};
607
Alan Stern7490e442010-09-25 23:35:15 +0200608static struct attribute *runtime_attrs[] = {
609#ifndef CONFIG_PM_ADVANCED_DEBUG
610 &dev_attr_runtime_status.attr,
611#endif
612 &dev_attr_control.attr,
613 &dev_attr_runtime_suspended_time.attr,
614 &dev_attr_runtime_active_time.attr,
Alan Stern15bcb91d2010-09-25 23:35:21 +0200615 &dev_attr_autosuspend_delay_ms.attr,
Alan Stern7490e442010-09-25 23:35:15 +0200616 NULL,
617};
Arvind Yadavdbb1d8b2017-06-22 16:23:32 +0530618static const struct attribute_group pm_runtime_attr_group = {
Alan Stern7490e442010-09-25 23:35:15 +0200619 .name = power_group_name,
620 .attrs = runtime_attrs,
621};
622
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100623static struct attribute *pm_qos_resume_latency_attrs[] = {
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100624 &dev_attr_pm_qos_resume_latency_us.attr,
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100625 NULL,
626};
Arvind Yadavdbb1d8b2017-06-22 16:23:32 +0530627static const struct attribute_group pm_qos_resume_latency_attr_group = {
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100628 .name = power_group_name,
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100629 .attrs = pm_qos_resume_latency_attrs,
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200630};
631
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100632static struct attribute *pm_qos_latency_tolerance_attrs[] = {
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100633 &dev_attr_pm_qos_latency_tolerance_us.attr,
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100634 NULL,
635};
Arvind Yadavdbb1d8b2017-06-22 16:23:32 +0530636static const struct attribute_group pm_qos_latency_tolerance_attr_group = {
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100637 .name = power_group_name,
638 .attrs = pm_qos_latency_tolerance_attrs,
639};
640
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200641static struct attribute *pm_qos_flags_attrs[] = {
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200642 &dev_attr_pm_qos_no_power_off.attr,
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200643 NULL,
644};
Arvind Yadavdbb1d8b2017-06-22 16:23:32 +0530645static const struct attribute_group pm_qos_flags_attr_group = {
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200646 .name = power_group_name,
647 .attrs = pm_qos_flags_attrs,
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100648};
649
Alan Stern7490e442010-09-25 23:35:15 +0200650int dpm_sysfs_add(struct device *dev)
651{
652 int rc;
653
654 rc = sysfs_create_group(&dev->kobj, &pm_attr_group);
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100655 if (rc)
656 return rc;
657
658 if (pm_runtime_callbacks_present(dev)) {
Alan Stern7490e442010-09-25 23:35:15 +0200659 rc = sysfs_merge_group(&dev->kobj, &pm_runtime_attr_group);
660 if (rc)
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100661 goto err_out;
Alan Stern7490e442010-09-25 23:35:15 +0200662 }
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100663 if (device_can_wakeup(dev)) {
664 rc = sysfs_merge_group(&dev->kobj, &pm_wakeup_attr_group);
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100665 if (rc)
666 goto err_runtime;
667 }
668 if (dev->power.set_latency_tolerance) {
669 rc = sysfs_merge_group(&dev->kobj,
670 &pm_qos_latency_tolerance_attr_group);
671 if (rc)
672 goto err_wakeup;
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100673 }
674 return 0;
675
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100676 err_wakeup:
677 sysfs_unmerge_group(&dev->kobj, &pm_wakeup_attr_group);
678 err_runtime:
679 sysfs_unmerge_group(&dev->kobj, &pm_runtime_attr_group);
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100680 err_out:
681 sysfs_remove_group(&dev->kobj, &pm_attr_group);
Alan Stern7490e442010-09-25 23:35:15 +0200682 return rc;
683}
684
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100685int wakeup_sysfs_add(struct device *dev)
686{
687 return sysfs_merge_group(&dev->kobj, &pm_wakeup_attr_group);
688}
689
690void wakeup_sysfs_remove(struct device *dev)
691{
692 sysfs_unmerge_group(&dev->kobj, &pm_wakeup_attr_group);
693}
694
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100695int pm_qos_sysfs_add_resume_latency(struct device *dev)
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100696{
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100697 return sysfs_merge_group(&dev->kobj, &pm_qos_resume_latency_attr_group);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100698}
699
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100700void pm_qos_sysfs_remove_resume_latency(struct device *dev)
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100701{
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100702 sysfs_unmerge_group(&dev->kobj, &pm_qos_resume_latency_attr_group);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200703}
704
705int pm_qos_sysfs_add_flags(struct device *dev)
706{
707 return sysfs_merge_group(&dev->kobj, &pm_qos_flags_attr_group);
708}
709
710void pm_qos_sysfs_remove_flags(struct device *dev)
711{
712 sysfs_unmerge_group(&dev->kobj, &pm_qos_flags_attr_group);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100713}
714
Mika Westerberg13b2c4a2015-07-27 18:03:56 +0300715int pm_qos_sysfs_add_latency_tolerance(struct device *dev)
716{
717 return sysfs_merge_group(&dev->kobj,
718 &pm_qos_latency_tolerance_attr_group);
719}
720
721void pm_qos_sysfs_remove_latency_tolerance(struct device *dev)
722{
723 sysfs_unmerge_group(&dev->kobj, &pm_qos_latency_tolerance_attr_group);
724}
725
Alan Stern7490e442010-09-25 23:35:15 +0200726void rpm_sysfs_remove(struct device *dev)
727{
728 sysfs_unmerge_group(&dev->kobj, &pm_runtime_attr_group);
729}
730
731void dpm_sysfs_remove(struct device *dev)
732{
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100733 sysfs_unmerge_group(&dev->kobj, &pm_qos_latency_tolerance_attr_group);
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100734 dev_pm_qos_constraints_destroy(dev);
Alan Stern7490e442010-09-25 23:35:15 +0200735 rpm_sysfs_remove(dev);
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100736 sysfs_unmerge_group(&dev->kobj, &pm_wakeup_attr_group);
Alan Stern7490e442010-09-25 23:35:15 +0200737 sysfs_remove_group(&dev->kobj, &pm_attr_group);
738}