Merge remote-tracking branch 'moduleh/module.h-split'
[linux-2.6/next.git] / drivers / base / power / sysfs.c
blobadf41be0ea664b7debc52b0ec49c5282a4fb46d5
1 /*
2 * drivers/base/power/sysfs.c - sysfs entries for device PM
3 */
5 #include <linux/device.h>
6 #include <linux/string.h>
7 #include <linux/export.h>
8 #include <linux/pm_runtime.h>
9 #include <linux/atomic.h>
10 #include <linux/jiffies.h>
11 #include "power.h"
14 * control - Report/change current runtime PM setting of the device
16 * Runtime power management of a device can be blocked with the help of
17 * this attribute. All devices have one of the following two values for
18 * the power/control file:
20 * + "auto\n" to allow the device to be power managed at run time;
21 * + "on\n" to prevent the device from being power managed at run time;
23 * The default for all devices is "auto", which means that devices may be
24 * subject to automatic power management, depending on their drivers.
25 * Changing this attribute to "on" prevents the driver from power managing
26 * the device at run time. Doing that while the device is suspended causes
27 * it to be woken up.
29 * wakeup - Report/change current wakeup option for device
31 * Some devices support "wakeup" events, which are hardware signals
32 * used to activate devices from suspended or low power states. Such
33 * devices have one of three values for the sysfs power/wakeup file:
35 * + "enabled\n" to issue the events;
36 * + "disabled\n" not to do so; or
37 * + "\n" for temporary or permanent inability to issue wakeup.
39 * (For example, unconfigured USB devices can't issue wakeups.)
41 * Familiar examples of devices that can issue wakeup events include
42 * keyboards and mice (both PS2 and USB styles), power buttons, modems,
43 * "Wake-On-LAN" Ethernet links, GPIO lines, and more. Some events
44 * will wake the entire system from a suspend state; others may just
45 * wake up the device (if the system as a whole is already active).
46 * Some wakeup events use normal IRQ lines; other use special out
47 * of band signaling.
49 * It is the responsibility of device drivers to enable (or disable)
50 * wakeup signaling as part of changing device power states, respecting
51 * the policy choices provided through the driver model.
53 * Devices may not be able to generate wakeup events from all power
54 * states. Also, the events may be ignored in some configurations;
55 * for example, they might need help from other devices that aren't
56 * active, or which may have wakeup disabled. Some drivers rely on
57 * wakeup events internally (unless they are disabled), keeping
58 * their hardware in low power modes whenever they're unused. This
59 * saves runtime power, without requiring system-wide sleep states.
61 * async - Report/change current async suspend setting for the device
63 * Asynchronous suspend and resume of the device during system-wide power
64 * state transitions can be enabled by writing "enabled" to this file.
65 * Analogously, if "disabled" is written to this file, the device will be
66 * suspended and resumed synchronously.
68 * All devices have one of the following two values for power/async:
70 * + "enabled\n" to permit the asynchronous suspend/resume of the device;
71 * + "disabled\n" to forbid it;
73 * NOTE: It generally is unsafe to permit the asynchronous suspend/resume
74 * of a device unless it is certain that all of the PM dependencies of the
75 * device are known to the PM core. However, for some devices this
76 * attribute is set to "enabled" by bus type code or device drivers and in
77 * that cases it should be safe to leave the default value.
79 * autosuspend_delay_ms - Report/change a device's autosuspend_delay value
81 * Some drivers don't want to carry out a runtime suspend as soon as a
82 * device becomes idle; they want it always to remain idle for some period
83 * of time before suspending it. This period is the autosuspend_delay
84 * value (expressed in milliseconds) and it can be controlled by the user.
85 * If the value is negative then the device will never be runtime
86 * suspended.
88 * NOTE: The autosuspend_delay_ms attribute and the autosuspend_delay
89 * value are used only if the driver calls pm_runtime_use_autosuspend().
91 * wakeup_count - Report the number of wakeup events related to the device
94 static const char enabled[] = "enabled";
95 static const char disabled[] = "disabled";
97 const char power_group_name[] = "power";
98 EXPORT_SYMBOL_GPL(power_group_name);
100 #ifdef CONFIG_PM_RUNTIME
101 static const char ctrl_auto[] = "auto";
102 static const char ctrl_on[] = "on";
104 static ssize_t control_show(struct device *dev, struct device_attribute *attr,
105 char *buf)
107 return sprintf(buf, "%s\n",
108 dev->power.runtime_auto ? ctrl_auto : ctrl_on);
111 static ssize_t control_store(struct device * dev, struct device_attribute *attr,
112 const char * buf, size_t n)
114 char *cp;
115 int len = n;
117 cp = memchr(buf, '\n', n);
118 if (cp)
119 len = cp - buf;
120 device_lock(dev);
121 if (len == sizeof ctrl_auto - 1 && strncmp(buf, ctrl_auto, len) == 0)
122 pm_runtime_allow(dev);
123 else if (len == sizeof ctrl_on - 1 && strncmp(buf, ctrl_on, len) == 0)
124 pm_runtime_forbid(dev);
125 else
126 n = -EINVAL;
127 device_unlock(dev);
128 return n;
131 static DEVICE_ATTR(control, 0644, control_show, control_store);
133 static ssize_t rtpm_active_time_show(struct device *dev,
134 struct device_attribute *attr, char *buf)
136 int ret;
137 spin_lock_irq(&dev->power.lock);
138 update_pm_runtime_accounting(dev);
139 ret = sprintf(buf, "%i\n", jiffies_to_msecs(dev->power.active_jiffies));
140 spin_unlock_irq(&dev->power.lock);
141 return ret;
144 static DEVICE_ATTR(runtime_active_time, 0444, rtpm_active_time_show, NULL);
146 static ssize_t rtpm_suspended_time_show(struct device *dev,
147 struct device_attribute *attr, char *buf)
149 int ret;
150 spin_lock_irq(&dev->power.lock);
151 update_pm_runtime_accounting(dev);
152 ret = sprintf(buf, "%i\n",
153 jiffies_to_msecs(dev->power.suspended_jiffies));
154 spin_unlock_irq(&dev->power.lock);
155 return ret;
158 static DEVICE_ATTR(runtime_suspended_time, 0444, rtpm_suspended_time_show, NULL);
160 static ssize_t rtpm_status_show(struct device *dev,
161 struct device_attribute *attr, char *buf)
163 const char *p;
165 if (dev->power.runtime_error) {
166 p = "error\n";
167 } else if (dev->power.disable_depth) {
168 p = "unsupported\n";
169 } else {
170 switch (dev->power.runtime_status) {
171 case RPM_SUSPENDED:
172 p = "suspended\n";
173 break;
174 case RPM_SUSPENDING:
175 p = "suspending\n";
176 break;
177 case RPM_RESUMING:
178 p = "resuming\n";
179 break;
180 case RPM_ACTIVE:
181 p = "active\n";
182 break;
183 default:
184 return -EIO;
187 return sprintf(buf, p);
190 static DEVICE_ATTR(runtime_status, 0444, rtpm_status_show, NULL);
192 static ssize_t autosuspend_delay_ms_show(struct device *dev,
193 struct device_attribute *attr, char *buf)
195 if (!dev->power.use_autosuspend)
196 return -EIO;
197 return sprintf(buf, "%d\n", dev->power.autosuspend_delay);
200 static ssize_t autosuspend_delay_ms_store(struct device *dev,
201 struct device_attribute *attr, const char *buf, size_t n)
203 long delay;
205 if (!dev->power.use_autosuspend)
206 return -EIO;
208 if (strict_strtol(buf, 10, &delay) != 0 || delay != (int) delay)
209 return -EINVAL;
211 device_lock(dev);
212 pm_runtime_set_autosuspend_delay(dev, delay);
213 device_unlock(dev);
214 return n;
217 static DEVICE_ATTR(autosuspend_delay_ms, 0644, autosuspend_delay_ms_show,
218 autosuspend_delay_ms_store);
220 #endif /* CONFIG_PM_RUNTIME */
222 #ifdef CONFIG_PM_SLEEP
223 static ssize_t
224 wake_show(struct device * dev, struct device_attribute *attr, char * buf)
226 return sprintf(buf, "%s\n", device_can_wakeup(dev)
227 ? (device_may_wakeup(dev) ? enabled : disabled)
228 : "");
231 static ssize_t
232 wake_store(struct device * dev, struct device_attribute *attr,
233 const char * buf, size_t n)
235 char *cp;
236 int len = n;
238 if (!device_can_wakeup(dev))
239 return -EINVAL;
241 cp = memchr(buf, '\n', n);
242 if (cp)
243 len = cp - buf;
244 if (len == sizeof enabled - 1
245 && strncmp(buf, enabled, sizeof enabled - 1) == 0)
246 device_set_wakeup_enable(dev, 1);
247 else if (len == sizeof disabled - 1
248 && strncmp(buf, disabled, sizeof disabled - 1) == 0)
249 device_set_wakeup_enable(dev, 0);
250 else
251 return -EINVAL;
252 return n;
255 static DEVICE_ATTR(wakeup, 0644, wake_show, wake_store);
257 static ssize_t wakeup_count_show(struct device *dev,
258 struct device_attribute *attr, char *buf)
260 unsigned long count = 0;
261 bool enabled = false;
263 spin_lock_irq(&dev->power.lock);
264 if (dev->power.wakeup) {
265 count = dev->power.wakeup->event_count;
266 enabled = true;
268 spin_unlock_irq(&dev->power.lock);
269 return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
272 static DEVICE_ATTR(wakeup_count, 0444, wakeup_count_show, NULL);
274 static ssize_t wakeup_active_count_show(struct device *dev,
275 struct device_attribute *attr, char *buf)
277 unsigned long count = 0;
278 bool enabled = false;
280 spin_lock_irq(&dev->power.lock);
281 if (dev->power.wakeup) {
282 count = dev->power.wakeup->active_count;
283 enabled = true;
285 spin_unlock_irq(&dev->power.lock);
286 return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
289 static DEVICE_ATTR(wakeup_active_count, 0444, wakeup_active_count_show, NULL);
291 static ssize_t wakeup_hit_count_show(struct device *dev,
292 struct device_attribute *attr, char *buf)
294 unsigned long count = 0;
295 bool enabled = false;
297 spin_lock_irq(&dev->power.lock);
298 if (dev->power.wakeup) {
299 count = dev->power.wakeup->hit_count;
300 enabled = true;
302 spin_unlock_irq(&dev->power.lock);
303 return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
306 static DEVICE_ATTR(wakeup_hit_count, 0444, wakeup_hit_count_show, NULL);
308 static ssize_t wakeup_active_show(struct device *dev,
309 struct device_attribute *attr, char *buf)
311 unsigned int active = 0;
312 bool enabled = false;
314 spin_lock_irq(&dev->power.lock);
315 if (dev->power.wakeup) {
316 active = dev->power.wakeup->active;
317 enabled = true;
319 spin_unlock_irq(&dev->power.lock);
320 return enabled ? sprintf(buf, "%u\n", active) : sprintf(buf, "\n");
323 static DEVICE_ATTR(wakeup_active, 0444, wakeup_active_show, NULL);
325 static ssize_t wakeup_total_time_show(struct device *dev,
326 struct device_attribute *attr, char *buf)
328 s64 msec = 0;
329 bool enabled = false;
331 spin_lock_irq(&dev->power.lock);
332 if (dev->power.wakeup) {
333 msec = ktime_to_ms(dev->power.wakeup->total_time);
334 enabled = true;
336 spin_unlock_irq(&dev->power.lock);
337 return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
340 static DEVICE_ATTR(wakeup_total_time_ms, 0444, wakeup_total_time_show, NULL);
342 static ssize_t wakeup_max_time_show(struct device *dev,
343 struct device_attribute *attr, char *buf)
345 s64 msec = 0;
346 bool enabled = false;
348 spin_lock_irq(&dev->power.lock);
349 if (dev->power.wakeup) {
350 msec = ktime_to_ms(dev->power.wakeup->max_time);
351 enabled = true;
353 spin_unlock_irq(&dev->power.lock);
354 return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
357 static DEVICE_ATTR(wakeup_max_time_ms, 0444, wakeup_max_time_show, NULL);
359 static ssize_t wakeup_last_time_show(struct device *dev,
360 struct device_attribute *attr, char *buf)
362 s64 msec = 0;
363 bool enabled = false;
365 spin_lock_irq(&dev->power.lock);
366 if (dev->power.wakeup) {
367 msec = ktime_to_ms(dev->power.wakeup->last_time);
368 enabled = true;
370 spin_unlock_irq(&dev->power.lock);
371 return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
374 static DEVICE_ATTR(wakeup_last_time_ms, 0444, wakeup_last_time_show, NULL);
375 #endif /* CONFIG_PM_SLEEP */
377 #ifdef CONFIG_PM_ADVANCED_DEBUG
378 #ifdef CONFIG_PM_RUNTIME
380 static ssize_t rtpm_usagecount_show(struct device *dev,
381 struct device_attribute *attr, char *buf)
383 return sprintf(buf, "%d\n", atomic_read(&dev->power.usage_count));
386 static ssize_t rtpm_children_show(struct device *dev,
387 struct device_attribute *attr, char *buf)
389 return sprintf(buf, "%d\n", dev->power.ignore_children ?
390 0 : atomic_read(&dev->power.child_count));
393 static ssize_t rtpm_enabled_show(struct device *dev,
394 struct device_attribute *attr, char *buf)
396 if ((dev->power.disable_depth) && (dev->power.runtime_auto == false))
397 return sprintf(buf, "disabled & forbidden\n");
398 else if (dev->power.disable_depth)
399 return sprintf(buf, "disabled\n");
400 else if (dev->power.runtime_auto == false)
401 return sprintf(buf, "forbidden\n");
402 return sprintf(buf, "enabled\n");
405 static DEVICE_ATTR(runtime_usage, 0444, rtpm_usagecount_show, NULL);
406 static DEVICE_ATTR(runtime_active_kids, 0444, rtpm_children_show, NULL);
407 static DEVICE_ATTR(runtime_enabled, 0444, rtpm_enabled_show, NULL);
409 #endif
411 static ssize_t async_show(struct device *dev, struct device_attribute *attr,
412 char *buf)
414 return sprintf(buf, "%s\n",
415 device_async_suspend_enabled(dev) ? enabled : disabled);
418 static ssize_t async_store(struct device *dev, struct device_attribute *attr,
419 const char *buf, size_t n)
421 char *cp;
422 int len = n;
424 cp = memchr(buf, '\n', n);
425 if (cp)
426 len = cp - buf;
427 if (len == sizeof enabled - 1 && strncmp(buf, enabled, len) == 0)
428 device_enable_async_suspend(dev);
429 else if (len == sizeof disabled - 1 && strncmp(buf, disabled, len) == 0)
430 device_disable_async_suspend(dev);
431 else
432 return -EINVAL;
433 return n;
436 static DEVICE_ATTR(async, 0644, async_show, async_store);
437 #endif /* CONFIG_PM_ADVANCED_DEBUG */
439 static struct attribute *power_attrs[] = {
440 #ifdef CONFIG_PM_ADVANCED_DEBUG
441 #ifdef CONFIG_PM_SLEEP
442 &dev_attr_async.attr,
443 #endif
444 #ifdef CONFIG_PM_RUNTIME
445 &dev_attr_runtime_status.attr,
446 &dev_attr_runtime_usage.attr,
447 &dev_attr_runtime_active_kids.attr,
448 &dev_attr_runtime_enabled.attr,
449 #endif
450 #endif /* CONFIG_PM_ADVANCED_DEBUG */
451 NULL,
453 static struct attribute_group pm_attr_group = {
454 .name = power_group_name,
455 .attrs = power_attrs,
458 static struct attribute *wakeup_attrs[] = {
459 #ifdef CONFIG_PM_SLEEP
460 &dev_attr_wakeup.attr,
461 &dev_attr_wakeup_count.attr,
462 &dev_attr_wakeup_active_count.attr,
463 &dev_attr_wakeup_hit_count.attr,
464 &dev_attr_wakeup_active.attr,
465 &dev_attr_wakeup_total_time_ms.attr,
466 &dev_attr_wakeup_max_time_ms.attr,
467 &dev_attr_wakeup_last_time_ms.attr,
468 #endif
469 NULL,
471 static struct attribute_group pm_wakeup_attr_group = {
472 .name = power_group_name,
473 .attrs = wakeup_attrs,
476 static struct attribute *runtime_attrs[] = {
477 #ifdef CONFIG_PM_RUNTIME
478 #ifndef CONFIG_PM_ADVANCED_DEBUG
479 &dev_attr_runtime_status.attr,
480 #endif
481 &dev_attr_control.attr,
482 &dev_attr_runtime_suspended_time.attr,
483 &dev_attr_runtime_active_time.attr,
484 &dev_attr_autosuspend_delay_ms.attr,
485 #endif /* CONFIG_PM_RUNTIME */
486 NULL,
488 static struct attribute_group pm_runtime_attr_group = {
489 .name = power_group_name,
490 .attrs = runtime_attrs,
493 int dpm_sysfs_add(struct device *dev)
495 int rc;
497 rc = sysfs_create_group(&dev->kobj, &pm_attr_group);
498 if (rc)
499 return rc;
501 if (pm_runtime_callbacks_present(dev)) {
502 rc = sysfs_merge_group(&dev->kobj, &pm_runtime_attr_group);
503 if (rc)
504 goto err_out;
507 if (device_can_wakeup(dev)) {
508 rc = sysfs_merge_group(&dev->kobj, &pm_wakeup_attr_group);
509 if (rc) {
510 if (pm_runtime_callbacks_present(dev))
511 sysfs_unmerge_group(&dev->kobj,
512 &pm_runtime_attr_group);
513 goto err_out;
516 return 0;
518 err_out:
519 sysfs_remove_group(&dev->kobj, &pm_attr_group);
520 return rc;
523 int wakeup_sysfs_add(struct device *dev)
525 return sysfs_merge_group(&dev->kobj, &pm_wakeup_attr_group);
528 void wakeup_sysfs_remove(struct device *dev)
530 sysfs_unmerge_group(&dev->kobj, &pm_wakeup_attr_group);
533 void rpm_sysfs_remove(struct device *dev)
535 sysfs_unmerge_group(&dev->kobj, &pm_runtime_attr_group);
538 void dpm_sysfs_remove(struct device *dev)
540 rpm_sysfs_remove(dev);
541 sysfs_unmerge_group(&dev->kobj, &pm_wakeup_attr_group);
542 sysfs_remove_group(&dev->kobj, &pm_attr_group);