blob: b5708c47ce2dfd5c261dd990c2e2bf3efe5cf8e0 [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>
Rafael J. Wysocki53823632010-01-23 22:02:51 +01007#include <linux/pm_runtime.h>
Dominik Brodowskic92445f2010-04-23 20:32:23 +02008#include <asm/atomic.h>
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +02009#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include "power.h"
11
David Brownell0ac85242005-09-12 19:39:34 -070012/*
Rafael J. Wysocki53823632010-01-23 22:02:51 +010013 * control - Report/change current runtime PM setting of the device
14 *
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:
18 *
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;
21 *
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
26 * it to be woken up.
27 *
David Brownell0ac85242005-09-12 19:39:34 -070028 * wakeup - Report/change current wakeup option for device
29 *
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:
33 *
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.
37 *
38 * (For example, unconfigured USB devices can't issue wakeups.)
39 *
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
46 * of band signaling.
47 *
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.
51 *
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.
Rafael J. Wysocki5a2eb852010-01-23 22:25:23 +010059 *
60 * async - Report/change current async suspend setting for the device
61 *
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.
66 *
67 * All devices have one of the following two values for power/async:
68 *
69 * + "enabled\n" to permit the asynchronous suspend/resume of the device;
70 * + "disabled\n" to forbid it;
71 *
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.
Rafael J. Wysockic125e962010-07-05 22:43:53 +020077 *
78 * wakeup_count - Report the number of wakeup events related to the device
David Brownell0ac85242005-09-12 19:39:34 -070079 */
80
81static const char enabled[] = "enabled";
82static const char disabled[] = "disabled";
83
Alan Stern7490e442010-09-25 23:35:15 +020084const char power_group_name[] = "power";
85EXPORT_SYMBOL_GPL(power_group_name);
86
Rafael J. Wysocki53823632010-01-23 22:02:51 +010087#ifdef CONFIG_PM_RUNTIME
88static const char ctrl_auto[] = "auto";
89static const char ctrl_on[] = "on";
90
91static ssize_t control_show(struct device *dev, struct device_attribute *attr,
92 char *buf)
93{
94 return sprintf(buf, "%s\n",
95 dev->power.runtime_auto ? ctrl_auto : ctrl_on);
96}
97
98static ssize_t control_store(struct device * dev, struct device_attribute *attr,
99 const char * buf, size_t n)
100{
101 char *cp;
102 int len = n;
103
104 cp = memchr(buf, '\n', n);
105 if (cp)
106 len = cp - buf;
107 if (len == sizeof ctrl_auto - 1 && strncmp(buf, ctrl_auto, len) == 0)
108 pm_runtime_allow(dev);
109 else if (len == sizeof ctrl_on - 1 && strncmp(buf, ctrl_on, len) == 0)
110 pm_runtime_forbid(dev);
111 else
112 return -EINVAL;
113 return n;
114}
115
116static DEVICE_ATTR(control, 0644, control_show, control_store);
Alan Stern0fcb4ee2010-07-08 00:05:37 +0200117
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200118static ssize_t rtpm_active_time_show(struct device *dev,
119 struct device_attribute *attr, char *buf)
120{
121 int ret;
122 spin_lock_irq(&dev->power.lock);
123 update_pm_runtime_accounting(dev);
124 ret = sprintf(buf, "%i\n", jiffies_to_msecs(dev->power.active_jiffies));
125 spin_unlock_irq(&dev->power.lock);
126 return ret;
127}
128
129static DEVICE_ATTR(runtime_active_time, 0444, rtpm_active_time_show, NULL);
130
131static ssize_t rtpm_suspended_time_show(struct device *dev,
132 struct device_attribute *attr, char *buf)
133{
134 int ret;
135 spin_lock_irq(&dev->power.lock);
136 update_pm_runtime_accounting(dev);
137 ret = sprintf(buf, "%i\n",
138 jiffies_to_msecs(dev->power.suspended_jiffies));
139 spin_unlock_irq(&dev->power.lock);
140 return ret;
141}
142
143static DEVICE_ATTR(runtime_suspended_time, 0444, rtpm_suspended_time_show, NULL);
144
Alan Stern0fcb4ee2010-07-08 00:05:37 +0200145static ssize_t rtpm_status_show(struct device *dev,
146 struct device_attribute *attr, char *buf)
147{
148 const char *p;
149
150 if (dev->power.runtime_error) {
151 p = "error\n";
152 } else if (dev->power.disable_depth) {
153 p = "unsupported\n";
154 } else {
155 switch (dev->power.runtime_status) {
156 case RPM_SUSPENDED:
157 p = "suspended\n";
158 break;
159 case RPM_SUSPENDING:
160 p = "suspending\n";
161 break;
162 case RPM_RESUMING:
163 p = "resuming\n";
164 break;
165 case RPM_ACTIVE:
166 p = "active\n";
167 break;
168 default:
169 return -EIO;
170 }
171 }
172 return sprintf(buf, p);
173}
174
175static DEVICE_ATTR(runtime_status, 0444, rtpm_status_show, NULL);
Rafael J. Wysocki53823632010-01-23 22:02:51 +0100176#endif
177
David Brownell0ac85242005-09-12 19:39:34 -0700178static ssize_t
179wake_show(struct device * dev, struct device_attribute *attr, char * buf)
180{
181 return sprintf(buf, "%s\n", device_can_wakeup(dev)
182 ? (device_may_wakeup(dev) ? enabled : disabled)
183 : "");
184}
185
186static ssize_t
187wake_store(struct device * dev, struct device_attribute *attr,
188 const char * buf, size_t n)
189{
190 char *cp;
191 int len = n;
192
193 if (!device_can_wakeup(dev))
194 return -EINVAL;
195
196 cp = memchr(buf, '\n', n);
197 if (cp)
198 len = cp - buf;
199 if (len == sizeof enabled - 1
200 && strncmp(buf, enabled, sizeof enabled - 1) == 0)
201 device_set_wakeup_enable(dev, 1);
202 else if (len == sizeof disabled - 1
203 && strncmp(buf, disabled, sizeof disabled - 1) == 0)
204 device_set_wakeup_enable(dev, 0);
205 else
206 return -EINVAL;
207 return n;
208}
209
210static DEVICE_ATTR(wakeup, 0644, wake_show, wake_store);
211
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200212#ifdef CONFIG_PM_SLEEP
213static ssize_t wakeup_count_show(struct device *dev,
214 struct device_attribute *attr, char *buf)
215{
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200216 unsigned long count = 0;
217 bool enabled = false;
218
219 spin_lock_irq(&dev->power.lock);
220 if (dev->power.wakeup) {
221 count = dev->power.wakeup->event_count;
222 enabled = true;
223 }
224 spin_unlock_irq(&dev->power.lock);
225 return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200226}
227
228static DEVICE_ATTR(wakeup_count, 0444, wakeup_count_show, NULL);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200229
230static ssize_t wakeup_active_count_show(struct device *dev,
231 struct device_attribute *attr, char *buf)
232{
233 unsigned long count = 0;
234 bool enabled = false;
235
236 spin_lock_irq(&dev->power.lock);
237 if (dev->power.wakeup) {
238 count = dev->power.wakeup->active_count;
239 enabled = true;
240 }
241 spin_unlock_irq(&dev->power.lock);
242 return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
243}
244
245static DEVICE_ATTR(wakeup_active_count, 0444, wakeup_active_count_show, NULL);
246
247static ssize_t wakeup_hit_count_show(struct device *dev,
248 struct device_attribute *attr, char *buf)
249{
250 unsigned long count = 0;
251 bool enabled = false;
252
253 spin_lock_irq(&dev->power.lock);
254 if (dev->power.wakeup) {
255 count = dev->power.wakeup->hit_count;
256 enabled = true;
257 }
258 spin_unlock_irq(&dev->power.lock);
259 return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
260}
261
262static DEVICE_ATTR(wakeup_hit_count, 0444, wakeup_hit_count_show, NULL);
263
264static ssize_t wakeup_active_show(struct device *dev,
265 struct device_attribute *attr, char *buf)
266{
267 unsigned int active = 0;
268 bool enabled = false;
269
270 spin_lock_irq(&dev->power.lock);
271 if (dev->power.wakeup) {
272 active = dev->power.wakeup->active;
273 enabled = true;
274 }
275 spin_unlock_irq(&dev->power.lock);
276 return enabled ? sprintf(buf, "%u\n", active) : sprintf(buf, "\n");
277}
278
279static DEVICE_ATTR(wakeup_active, 0444, wakeup_active_show, NULL);
280
281static ssize_t wakeup_total_time_show(struct device *dev,
282 struct device_attribute *attr, char *buf)
283{
284 s64 msec = 0;
285 bool enabled = false;
286
287 spin_lock_irq(&dev->power.lock);
288 if (dev->power.wakeup) {
289 msec = ktime_to_ms(dev->power.wakeup->total_time);
290 enabled = true;
291 }
292 spin_unlock_irq(&dev->power.lock);
293 return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
294}
295
296static DEVICE_ATTR(wakeup_total_time_ms, 0444, wakeup_total_time_show, NULL);
297
298static ssize_t wakeup_max_time_show(struct device *dev,
299 struct device_attribute *attr, char *buf)
300{
301 s64 msec = 0;
302 bool enabled = false;
303
304 spin_lock_irq(&dev->power.lock);
305 if (dev->power.wakeup) {
306 msec = ktime_to_ms(dev->power.wakeup->max_time);
307 enabled = true;
308 }
309 spin_unlock_irq(&dev->power.lock);
310 return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
311}
312
313static DEVICE_ATTR(wakeup_max_time_ms, 0444, wakeup_max_time_show, NULL);
314
315static ssize_t wakeup_last_time_show(struct device *dev,
316 struct device_attribute *attr, char *buf)
317{
318 s64 msec = 0;
319 bool enabled = false;
320
321 spin_lock_irq(&dev->power.lock);
322 if (dev->power.wakeup) {
323 msec = ktime_to_ms(dev->power.wakeup->last_time);
324 enabled = true;
325 }
326 spin_unlock_irq(&dev->power.lock);
327 return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
328}
329
330static DEVICE_ATTR(wakeup_last_time_ms, 0444, wakeup_last_time_show, NULL);
331#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200332
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200333#ifdef CONFIG_PM_ADVANCED_DEBUG
334#ifdef CONFIG_PM_RUNTIME
335
336static ssize_t rtpm_usagecount_show(struct device *dev,
337 struct device_attribute *attr, char *buf)
338{
339 return sprintf(buf, "%d\n", atomic_read(&dev->power.usage_count));
340}
341
342static ssize_t rtpm_children_show(struct device *dev,
343 struct device_attribute *attr, char *buf)
344{
345 return sprintf(buf, "%d\n", dev->power.ignore_children ?
346 0 : atomic_read(&dev->power.child_count));
347}
348
349static ssize_t rtpm_enabled_show(struct device *dev,
350 struct device_attribute *attr, char *buf)
351{
352 if ((dev->power.disable_depth) && (dev->power.runtime_auto == false))
353 return sprintf(buf, "disabled & forbidden\n");
354 else if (dev->power.disable_depth)
355 return sprintf(buf, "disabled\n");
356 else if (dev->power.runtime_auto == false)
357 return sprintf(buf, "forbidden\n");
358 return sprintf(buf, "enabled\n");
359}
360
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200361static DEVICE_ATTR(runtime_usage, 0444, rtpm_usagecount_show, NULL);
362static DEVICE_ATTR(runtime_active_kids, 0444, rtpm_children_show, NULL);
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200363static DEVICE_ATTR(runtime_enabled, 0444, rtpm_enabled_show, NULL);
364
365#endif
366
Rafael J. Wysocki5a2eb852010-01-23 22:25:23 +0100367static ssize_t async_show(struct device *dev, struct device_attribute *attr,
368 char *buf)
369{
370 return sprintf(buf, "%s\n",
371 device_async_suspend_enabled(dev) ? enabled : disabled);
372}
373
374static ssize_t async_store(struct device *dev, struct device_attribute *attr,
375 const char *buf, size_t n)
376{
377 char *cp;
378 int len = n;
379
380 cp = memchr(buf, '\n', n);
381 if (cp)
382 len = cp - buf;
383 if (len == sizeof enabled - 1 && strncmp(buf, enabled, len) == 0)
384 device_enable_async_suspend(dev);
385 else if (len == sizeof disabled - 1 && strncmp(buf, disabled, len) == 0)
386 device_disable_async_suspend(dev);
387 else
388 return -EINVAL;
389 return n;
390}
391
392static DEVICE_ATTR(async, 0644, async_show, async_store);
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200393#endif /* CONFIG_PM_ADVANCED_DEBUG */
David Brownell0ac85242005-09-12 19:39:34 -0700394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395static struct attribute * power_attrs[] = {
David Brownell0ac85242005-09-12 19:39:34 -0700396 &dev_attr_wakeup.attr,
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200397#ifdef CONFIG_PM_SLEEP
398 &dev_attr_wakeup_count.attr,
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200399 &dev_attr_wakeup_active_count.attr,
400 &dev_attr_wakeup_hit_count.attr,
401 &dev_attr_wakeup_active.attr,
402 &dev_attr_wakeup_total_time_ms.attr,
403 &dev_attr_wakeup_max_time_ms.attr,
404 &dev_attr_wakeup_last_time_ms.attr,
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200405#endif
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200406#ifdef CONFIG_PM_ADVANCED_DEBUG
Rafael J. Wysocki5a2eb852010-01-23 22:25:23 +0100407 &dev_attr_async.attr,
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200408#ifdef CONFIG_PM_RUNTIME
Alan Stern7490e442010-09-25 23:35:15 +0200409 &dev_attr_runtime_status.attr,
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200410 &dev_attr_runtime_usage.attr,
411 &dev_attr_runtime_active_kids.attr,
Dominik Brodowskic92445f2010-04-23 20:32:23 +0200412 &dev_attr_runtime_enabled.attr,
413#endif
Rafael J. Wysocki5a2eb852010-01-23 22:25:23 +0100414#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 NULL,
416};
417static struct attribute_group pm_attr_group = {
Alan Stern7490e442010-09-25 23:35:15 +0200418 .name = power_group_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 .attrs = power_attrs,
420};
421
Alan Stern7490e442010-09-25 23:35:15 +0200422#ifdef CONFIG_PM_RUNTIME
423
424static struct attribute *runtime_attrs[] = {
425#ifndef CONFIG_PM_ADVANCED_DEBUG
426 &dev_attr_runtime_status.attr,
427#endif
428 &dev_attr_control.attr,
429 &dev_attr_runtime_suspended_time.attr,
430 &dev_attr_runtime_active_time.attr,
431 NULL,
432};
433static struct attribute_group pm_runtime_attr_group = {
434 .name = power_group_name,
435 .attrs = runtime_attrs,
436};
437
438int dpm_sysfs_add(struct device *dev)
439{
440 int rc;
441
442 rc = sysfs_create_group(&dev->kobj, &pm_attr_group);
443 if (rc == 0 && !dev->power.no_callbacks) {
444 rc = sysfs_merge_group(&dev->kobj, &pm_runtime_attr_group);
445 if (rc)
446 sysfs_remove_group(&dev->kobj, &pm_attr_group);
447 }
448 return rc;
449}
450
451void rpm_sysfs_remove(struct device *dev)
452{
453 sysfs_unmerge_group(&dev->kobj, &pm_runtime_attr_group);
454}
455
456void dpm_sysfs_remove(struct device *dev)
457{
458 rpm_sysfs_remove(dev);
459 sysfs_remove_group(&dev->kobj, &pm_attr_group);
460}
461
462#else /* CONFIG_PM_RUNTIME */
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464int dpm_sysfs_add(struct device * dev)
465{
466 return sysfs_create_group(&dev->kobj, &pm_attr_group);
467}
468
469void dpm_sysfs_remove(struct device * dev)
470{
471 sysfs_remove_group(&dev->kobj, &pm_attr_group);
472}
Alan Stern7490e442010-09-25 23:35:15 +0200473
474#endif