blob: d713738ce7967cdb8003166fc17bf63116d3b629 [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
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200122static DEVICE_ATTR_RW(control);
Alan Stern0fcb4ee2010-07-08 00:05:37 +0200123
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200124static ssize_t runtime_active_time_show(struct device *dev,
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200125 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
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200135static DEVICE_ATTR_RO(runtime_active_time);
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200136
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200137static ssize_t runtime_suspended_time_show(struct device *dev,
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200138 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
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200149static DEVICE_ATTR_RO(runtime_suspended_time);
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200150
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200151static ssize_t runtime_status_show(struct device *dev,
Alan Stern0fcb4ee2010-07-08 00:05:37 +0200152 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
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200181static DEVICE_ATTR_RO(runtime_status);
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
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200208static DEVICE_ATTR_RW(autosuspend_delay_ms);
Alan Stern15bcb91d2010-09-25 23:35:21 +0200209
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200210static ssize_t pm_qos_resume_latency_us_show(struct device *dev,
211 struct device_attribute *attr,
212 char *buf)
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100213{
Rafael J. Wysocki0759e802017-11-07 11:33:49 +0100214 s32 value = dev_pm_qos_requested_resume_latency(dev);
215
216 if (value == 0)
217 return sprintf(buf, "n/a\n");
Andy Shevchenkof0e6d9f2017-11-10 20:28:08 +0200218 if (value == PM_QOS_RESUME_LATENCY_NO_CONSTRAINT)
Rafael J. Wysocki0759e802017-11-07 11:33:49 +0100219 value = 0;
220
221 return sprintf(buf, "%d\n", value);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100222}
223
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200224static ssize_t pm_qos_resume_latency_us_store(struct device *dev,
225 struct device_attribute *attr,
226 const char *buf, size_t n)
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100227{
228 s32 value;
229 int ret;
230
Rafael J. Wysocki0759e802017-11-07 11:33:49 +0100231 if (!kstrtos32(buf, 0, &value)) {
232 /*
233 * Prevent users from writing negative or "no constraint" values
234 * directly.
235 */
236 if (value < 0 || value == PM_QOS_RESUME_LATENCY_NO_CONSTRAINT)
237 return -EINVAL;
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100238
Rafael J. Wysocki0759e802017-11-07 11:33:49 +0100239 if (value == 0)
240 value = PM_QOS_RESUME_LATENCY_NO_CONSTRAINT;
Andy Shevchenko325c4b32017-11-10 20:28:07 +0200241 } else if (sysfs_streq(buf, "n/a")) {
Rafael J. Wysocki0759e802017-11-07 11:33:49 +0100242 value = 0;
243 } else {
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100244 return -EINVAL;
Rafael J. Wysocki0759e802017-11-07 11:33:49 +0100245 }
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100246
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100247 ret = dev_pm_qos_update_request(dev->power.qos->resume_latency_req,
248 value);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100249 return ret < 0 ? ret : n;
250}
251
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200252static DEVICE_ATTR_RW(pm_qos_resume_latency_us);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200253
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200254static ssize_t pm_qos_latency_tolerance_us_show(struct device *dev,
255 struct device_attribute *attr,
256 char *buf)
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100257{
258 s32 value = dev_pm_qos_get_user_latency_tolerance(dev);
259
260 if (value < 0)
261 return sprintf(buf, "auto\n");
Andy Shevchenkof0e6d9f2017-11-10 20:28:08 +0200262 if (value == PM_QOS_LATENCY_ANY)
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100263 return sprintf(buf, "any\n");
264
265 return sprintf(buf, "%d\n", value);
266}
267
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200268static ssize_t pm_qos_latency_tolerance_us_store(struct device *dev,
269 struct device_attribute *attr,
270 const char *buf, size_t n)
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100271{
272 s32 value;
273 int ret;
274
Andrew Lutomirskiee061da2016-11-29 17:11:50 -0800275 if (kstrtos32(buf, 0, &value) == 0) {
276 /* Users can't write negative values directly */
277 if (value < 0)
278 return -EINVAL;
279 } else {
Andy Shevchenko325c4b32017-11-10 20:28:07 +0200280 if (sysfs_streq(buf, "auto"))
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100281 value = PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT;
Andy Shevchenko325c4b32017-11-10 20:28:07 +0200282 else if (sysfs_streq(buf, "any"))
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100283 value = PM_QOS_LATENCY_ANY;
Dan Carpenter2ca30332017-07-10 10:21:40 +0300284 else
285 return -EINVAL;
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100286 }
287 ret = dev_pm_qos_update_user_latency_tolerance(dev, value);
288 return ret < 0 ? ret : n;
289}
290
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200291static DEVICE_ATTR_RW(pm_qos_latency_tolerance_us);
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100292
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200293static ssize_t pm_qos_no_power_off_show(struct device *dev,
294 struct device_attribute *attr,
295 char *buf)
296{
297 return sprintf(buf, "%d\n", !!(dev_pm_qos_requested_flags(dev)
298 & PM_QOS_FLAG_NO_POWER_OFF));
299}
300
301static ssize_t pm_qos_no_power_off_store(struct device *dev,
302 struct device_attribute *attr,
303 const char *buf, size_t n)
304{
305 int ret;
306
307 if (kstrtoint(buf, 0, &ret))
308 return -EINVAL;
309
310 if (ret != 0 && ret != 1)
311 return -EINVAL;
312
313 ret = dev_pm_qos_update_flags(dev, PM_QOS_FLAG_NO_POWER_OFF, ret);
314 return ret < 0 ? ret : n;
315}
316
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200317static DEVICE_ATTR_RW(pm_qos_no_power_off);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200318
Rafael J. Wysockie7623182011-05-07 18:11:52 +0200319#ifdef CONFIG_PM_SLEEP
Johannes Berg2b4f4312014-09-08 08:48:00 +0200320static const char _enabled[] = "enabled";
321static const char _disabled[] = "disabled";
322
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200323static ssize_t wakeup_show(struct device *dev, struct device_attribute *attr,
324 char *buf)
David Brownell0ac85242005-09-12 19:39:34 -0700325{
326 return sprintf(buf, "%s\n", device_can_wakeup(dev)
Johannes Berg2b4f4312014-09-08 08:48:00 +0200327 ? (device_may_wakeup(dev) ? _enabled : _disabled)
David Brownell0ac85242005-09-12 19:39:34 -0700328 : "");
329}
330
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200331static ssize_t wakeup_store(struct device *dev, struct device_attribute *attr,
332 const char *buf, size_t n)
David Brownell0ac85242005-09-12 19:39:34 -0700333{
David Brownell0ac85242005-09-12 19:39:34 -0700334 if (!device_can_wakeup(dev))
335 return -EINVAL;
336
Andy Shevchenko325c4b32017-11-10 20:28:07 +0200337 if (sysfs_streq(buf, _enabled))
David Brownell0ac85242005-09-12 19:39:34 -0700338 device_set_wakeup_enable(dev, 1);
Andy Shevchenko325c4b32017-11-10 20:28:07 +0200339 else if (sysfs_streq(buf, _disabled))
David Brownell0ac85242005-09-12 19:39:34 -0700340 device_set_wakeup_enable(dev, 0);
341 else
342 return -EINVAL;
343 return n;
344}
345
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200346static DEVICE_ATTR_RW(wakeup);
David Brownell0ac85242005-09-12 19:39:34 -0700347
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200348static ssize_t wakeup_count_show(struct device *dev,
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200349 struct device_attribute *attr, char *buf)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200350{
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200351 unsigned long count = 0;
352 bool enabled = false;
353
354 spin_lock_irq(&dev->power.lock);
355 if (dev->power.wakeup) {
Ravi Chandra Sadineni2d5ed612018-06-01 19:32:15 -0700356 count = dev->power.wakeup->wakeup_count;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200357 enabled = true;
358 }
359 spin_unlock_irq(&dev->power.lock);
360 return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200361}
362
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200363static DEVICE_ATTR_RO(wakeup_count);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200364
365static ssize_t wakeup_active_count_show(struct device *dev,
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200366 struct device_attribute *attr,
367 char *buf)
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200368{
369 unsigned long count = 0;
370 bool enabled = false;
371
372 spin_lock_irq(&dev->power.lock);
373 if (dev->power.wakeup) {
374 count = dev->power.wakeup->active_count;
375 enabled = true;
376 }
377 spin_unlock_irq(&dev->power.lock);
378 return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
379}
380
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200381static DEVICE_ATTR_RO(wakeup_active_count);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200382
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200383static ssize_t wakeup_abort_count_show(struct device *dev,
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200384 struct device_attribute *attr,
385 char *buf)
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200386{
387 unsigned long count = 0;
388 bool enabled = false;
389
390 spin_lock_irq(&dev->power.lock);
391 if (dev->power.wakeup) {
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200392 count = dev->power.wakeup->wakeup_count;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200393 enabled = true;
394 }
395 spin_unlock_irq(&dev->power.lock);
396 return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
397}
398
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200399static DEVICE_ATTR_RO(wakeup_abort_count);
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200400
401static ssize_t wakeup_expire_count_show(struct device *dev,
402 struct device_attribute *attr,
403 char *buf)
404{
405 unsigned long count = 0;
406 bool enabled = false;
407
408 spin_lock_irq(&dev->power.lock);
409 if (dev->power.wakeup) {
410 count = dev->power.wakeup->expire_count;
411 enabled = true;
412 }
413 spin_unlock_irq(&dev->power.lock);
414 return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
415}
416
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200417static DEVICE_ATTR_RO(wakeup_expire_count);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200418
419static ssize_t wakeup_active_show(struct device *dev,
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200420 struct device_attribute *attr, char *buf)
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200421{
422 unsigned int active = 0;
423 bool enabled = false;
424
425 spin_lock_irq(&dev->power.lock);
426 if (dev->power.wakeup) {
427 active = dev->power.wakeup->active;
428 enabled = true;
429 }
430 spin_unlock_irq(&dev->power.lock);
431 return enabled ? sprintf(buf, "%u\n", active) : sprintf(buf, "\n");
432}
433
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200434static DEVICE_ATTR_RO(wakeup_active);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200435
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200436static ssize_t wakeup_total_time_ms_show(struct device *dev,
437 struct device_attribute *attr,
438 char *buf)
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200439{
440 s64 msec = 0;
441 bool enabled = false;
442
443 spin_lock_irq(&dev->power.lock);
444 if (dev->power.wakeup) {
445 msec = ktime_to_ms(dev->power.wakeup->total_time);
446 enabled = true;
447 }
448 spin_unlock_irq(&dev->power.lock);
449 return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
450}
451
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200452static DEVICE_ATTR_RO(wakeup_total_time_ms);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200453
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200454static ssize_t wakeup_max_time_ms_show(struct device *dev,
455 struct device_attribute *attr, char *buf)
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200456{
457 s64 msec = 0;
458 bool enabled = false;
459
460 spin_lock_irq(&dev->power.lock);
461 if (dev->power.wakeup) {
462 msec = ktime_to_ms(dev->power.wakeup->max_time);
463 enabled = true;
464 }
465 spin_unlock_irq(&dev->power.lock);
466 return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
467}
468
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200469static DEVICE_ATTR_RO(wakeup_max_time_ms);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200470
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200471static ssize_t wakeup_last_time_ms_show(struct device *dev,
472 struct device_attribute *attr,
473 char *buf)
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200474{
475 s64 msec = 0;
476 bool enabled = false;
477
478 spin_lock_irq(&dev->power.lock);
479 if (dev->power.wakeup) {
480 msec = ktime_to_ms(dev->power.wakeup->last_time);
481 enabled = true;
482 }
483 spin_unlock_irq(&dev->power.lock);
484 return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
485}
486
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200487static DEVICE_ATTR_RO(wakeup_last_time_ms);
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200488
489#ifdef CONFIG_PM_AUTOSLEEP
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200490static ssize_t wakeup_prevent_sleep_time_ms_show(struct device *dev,
491 struct device_attribute *attr,
492 char *buf)
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200493{
494 s64 msec = 0;
495 bool enabled = false;
496
497 spin_lock_irq(&dev->power.lock);
498 if (dev->power.wakeup) {
499 msec = ktime_to_ms(dev->power.wakeup->prevent_sleep_time);
500 enabled = true;
501 }
502 spin_unlock_irq(&dev->power.lock);
503 return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
504}
505
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200506static DEVICE_ATTR_RO(wakeup_prevent_sleep_time_ms);
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200507#endif /* CONFIG_PM_AUTOSLEEP */
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200508#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200509
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200510#ifdef CONFIG_PM_ADVANCED_DEBUG
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200511static ssize_t runtime_usage_show(struct device *dev,
512 struct device_attribute *attr, char *buf)
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200513{
514 return sprintf(buf, "%d\n", atomic_read(&dev->power.usage_count));
515}
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200516static DEVICE_ATTR_RO(runtime_usage);
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200517
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200518static ssize_t runtime_active_kids_show(struct device *dev,
519 struct device_attribute *attr,
520 char *buf)
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200521{
522 return sprintf(buf, "%d\n", dev->power.ignore_children ?
523 0 : atomic_read(&dev->power.child_count));
524}
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200525static DEVICE_ATTR_RO(runtime_active_kids);
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200526
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200527static ssize_t runtime_enabled_show(struct device *dev,
528 struct device_attribute *attr, char *buf)
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200529{
Andy Shevchenkof0e6d9f2017-11-10 20:28:08 +0200530 if (dev->power.disable_depth && (dev->power.runtime_auto == false))
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200531 return sprintf(buf, "disabled & forbidden\n");
Andy Shevchenkof0e6d9f2017-11-10 20:28:08 +0200532 if (dev->power.disable_depth)
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200533 return sprintf(buf, "disabled\n");
Andy Shevchenkof0e6d9f2017-11-10 20:28:08 +0200534 if (dev->power.runtime_auto == false)
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200535 return sprintf(buf, "forbidden\n");
536 return sprintf(buf, "enabled\n");
537}
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200538static DEVICE_ATTR_RO(runtime_enabled);
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200539
Rafael J. Wysockid181b492012-07-11 22:42:58 +0200540#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki5a2eb852010-01-23 22:25:23 +0100541static ssize_t async_show(struct device *dev, struct device_attribute *attr,
542 char *buf)
543{
544 return sprintf(buf, "%s\n",
Johannes Berg2b4f4312014-09-08 08:48:00 +0200545 device_async_suspend_enabled(dev) ?
546 _enabled : _disabled);
Rafael J. Wysocki5a2eb852010-01-23 22:25:23 +0100547}
548
549static ssize_t async_store(struct device *dev, struct device_attribute *attr,
550 const char *buf, size_t n)
551{
Andy Shevchenko325c4b32017-11-10 20:28:07 +0200552 if (sysfs_streq(buf, _enabled))
Rafael J. Wysocki5a2eb852010-01-23 22:25:23 +0100553 device_enable_async_suspend(dev);
Andy Shevchenko325c4b32017-11-10 20:28:07 +0200554 else if (sysfs_streq(buf, _disabled))
Rafael J. Wysocki5a2eb852010-01-23 22:25:23 +0100555 device_disable_async_suspend(dev);
556 else
557 return -EINVAL;
558 return n;
559}
560
Andy Shevchenko47acbd72017-11-10 20:28:09 +0200561static DEVICE_ATTR_RW(async);
Rafael J. Wysockid181b492012-07-11 22:42:58 +0200562
Rafael J. Wysockid30d8192014-11-27 22:38:05 +0100563#endif /* CONFIG_PM_SLEEP */
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200564#endif /* CONFIG_PM_ADVANCED_DEBUG */
David Brownell0ac85242005-09-12 19:39:34 -0700565
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100566static struct attribute *power_attrs[] = {
567#ifdef CONFIG_PM_ADVANCED_DEBUG
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200568#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100569 &dev_attr_async.attr,
570#endif
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100571 &dev_attr_runtime_status.attr,
572 &dev_attr_runtime_usage.attr,
573 &dev_attr_runtime_active_kids.attr,
574 &dev_attr_runtime_enabled.attr,
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100575#endif /* CONFIG_PM_ADVANCED_DEBUG */
576 NULL,
577};
Arvind Yadavdbb1d8b2017-06-22 16:23:32 +0530578static const struct attribute_group pm_attr_group = {
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100579 .name = power_group_name,
580 .attrs = power_attrs,
581};
582
583static struct attribute *wakeup_attrs[] = {
584#ifdef CONFIG_PM_SLEEP
585 &dev_attr_wakeup.attr,
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200586 &dev_attr_wakeup_count.attr,
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200587 &dev_attr_wakeup_active_count.attr,
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200588 &dev_attr_wakeup_abort_count.attr,
589 &dev_attr_wakeup_expire_count.attr,
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200590 &dev_attr_wakeup_active.attr,
591 &dev_attr_wakeup_total_time_ms.attr,
592 &dev_attr_wakeup_max_time_ms.attr,
593 &dev_attr_wakeup_last_time_ms.attr,
Rafael J. Wysocki55850942012-04-29 22:53:32 +0200594#ifdef CONFIG_PM_AUTOSLEEP
595 &dev_attr_wakeup_prevent_sleep_time_ms.attr,
596#endif
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200597#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 NULL,
599};
Arvind Yadavdbb1d8b2017-06-22 16:23:32 +0530600static const struct attribute_group pm_wakeup_attr_group = {
Alan Stern7490e442010-09-25 23:35:15 +0200601 .name = power_group_name,
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100602 .attrs = wakeup_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603};
604
Alan Stern7490e442010-09-25 23:35:15 +0200605static struct attribute *runtime_attrs[] = {
606#ifndef CONFIG_PM_ADVANCED_DEBUG
607 &dev_attr_runtime_status.attr,
608#endif
609 &dev_attr_control.attr,
610 &dev_attr_runtime_suspended_time.attr,
611 &dev_attr_runtime_active_time.attr,
Alan Stern15bcb91d2010-09-25 23:35:21 +0200612 &dev_attr_autosuspend_delay_ms.attr,
Alan Stern7490e442010-09-25 23:35:15 +0200613 NULL,
614};
Arvind Yadavdbb1d8b2017-06-22 16:23:32 +0530615static const struct attribute_group pm_runtime_attr_group = {
Alan Stern7490e442010-09-25 23:35:15 +0200616 .name = power_group_name,
617 .attrs = runtime_attrs,
618};
619
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100620static struct attribute *pm_qos_resume_latency_attrs[] = {
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100621 &dev_attr_pm_qos_resume_latency_us.attr,
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100622 NULL,
623};
Arvind Yadavdbb1d8b2017-06-22 16:23:32 +0530624static const struct attribute_group pm_qos_resume_latency_attr_group = {
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100625 .name = power_group_name,
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100626 .attrs = pm_qos_resume_latency_attrs,
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200627};
628
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100629static struct attribute *pm_qos_latency_tolerance_attrs[] = {
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100630 &dev_attr_pm_qos_latency_tolerance_us.attr,
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100631 NULL,
632};
Arvind Yadavdbb1d8b2017-06-22 16:23:32 +0530633static const struct attribute_group pm_qos_latency_tolerance_attr_group = {
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100634 .name = power_group_name,
635 .attrs = pm_qos_latency_tolerance_attrs,
636};
637
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200638static struct attribute *pm_qos_flags_attrs[] = {
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200639 &dev_attr_pm_qos_no_power_off.attr,
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200640 NULL,
641};
Arvind Yadavdbb1d8b2017-06-22 16:23:32 +0530642static const struct attribute_group pm_qos_flags_attr_group = {
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200643 .name = power_group_name,
644 .attrs = pm_qos_flags_attrs,
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100645};
646
Alan Stern7490e442010-09-25 23:35:15 +0200647int dpm_sysfs_add(struct device *dev)
648{
649 int rc;
650
651 rc = sysfs_create_group(&dev->kobj, &pm_attr_group);
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100652 if (rc)
653 return rc;
654
655 if (pm_runtime_callbacks_present(dev)) {
Alan Stern7490e442010-09-25 23:35:15 +0200656 rc = sysfs_merge_group(&dev->kobj, &pm_runtime_attr_group);
657 if (rc)
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100658 goto err_out;
Alan Stern7490e442010-09-25 23:35:15 +0200659 }
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100660 if (device_can_wakeup(dev)) {
661 rc = sysfs_merge_group(&dev->kobj, &pm_wakeup_attr_group);
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100662 if (rc)
663 goto err_runtime;
664 }
665 if (dev->power.set_latency_tolerance) {
666 rc = sysfs_merge_group(&dev->kobj,
667 &pm_qos_latency_tolerance_attr_group);
668 if (rc)
669 goto err_wakeup;
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100670 }
671 return 0;
672
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100673 err_wakeup:
674 sysfs_unmerge_group(&dev->kobj, &pm_wakeup_attr_group);
675 err_runtime:
676 sysfs_unmerge_group(&dev->kobj, &pm_runtime_attr_group);
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100677 err_out:
678 sysfs_remove_group(&dev->kobj, &pm_attr_group);
Alan Stern7490e442010-09-25 23:35:15 +0200679 return rc;
680}
681
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100682int wakeup_sysfs_add(struct device *dev)
683{
684 return sysfs_merge_group(&dev->kobj, &pm_wakeup_attr_group);
685}
686
687void wakeup_sysfs_remove(struct device *dev)
688{
689 sysfs_unmerge_group(&dev->kobj, &pm_wakeup_attr_group);
690}
691
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100692int pm_qos_sysfs_add_resume_latency(struct device *dev)
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100693{
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100694 return sysfs_merge_group(&dev->kobj, &pm_qos_resume_latency_attr_group);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100695}
696
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100697void pm_qos_sysfs_remove_resume_latency(struct device *dev)
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100698{
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100699 sysfs_unmerge_group(&dev->kobj, &pm_qos_resume_latency_attr_group);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200700}
701
702int pm_qos_sysfs_add_flags(struct device *dev)
703{
704 return sysfs_merge_group(&dev->kobj, &pm_qos_flags_attr_group);
705}
706
707void pm_qos_sysfs_remove_flags(struct device *dev)
708{
709 sysfs_unmerge_group(&dev->kobj, &pm_qos_flags_attr_group);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100710}
711
Mika Westerberg13b2c4a2015-07-27 18:03:56 +0300712int pm_qos_sysfs_add_latency_tolerance(struct device *dev)
713{
714 return sysfs_merge_group(&dev->kobj,
715 &pm_qos_latency_tolerance_attr_group);
716}
717
718void pm_qos_sysfs_remove_latency_tolerance(struct device *dev)
719{
720 sysfs_unmerge_group(&dev->kobj, &pm_qos_latency_tolerance_attr_group);
721}
722
Alan Stern7490e442010-09-25 23:35:15 +0200723void rpm_sysfs_remove(struct device *dev)
724{
725 sysfs_unmerge_group(&dev->kobj, &pm_runtime_attr_group);
726}
727
728void dpm_sysfs_remove(struct device *dev)
729{
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100730 sysfs_unmerge_group(&dev->kobj, &pm_qos_latency_tolerance_attr_group);
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100731 dev_pm_qos_constraints_destroy(dev);
Alan Stern7490e442010-09-25 23:35:15 +0200732 rpm_sysfs_remove(dev);
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100733 sysfs_unmerge_group(&dev->kobj, &pm_wakeup_attr_group);
Alan Stern7490e442010-09-25 23:35:15 +0200734 sysfs_remove_group(&dev->kobj, &pm_attr_group);
735}