PM / Runtime: Add no_callbacks flag
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / base / power / sysfs.c
blobb5708c47ce2dfd5c261dd990c2e2bf3efe5cf8e0
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/pm_runtime.h>
8 #include <asm/atomic.h>
9 #include <linux/jiffies.h>
10 #include "power.h"
13 * control - Report/change current runtime PM setting of the device
15 * Runtime power management of a device can be blocked with the help of
16 * this attribute. All devices have one of the following two values for
17 * the power/control file:
19 * + "auto\n" to allow the device to be power managed at run time;
20 * + "on\n" to prevent the device from being power managed at run time;
22 * The default for all devices is "auto", which means that devices may be
23 * subject to automatic power management, depending on their drivers.
24 * Changing this attribute to "on" prevents the driver from power managing
25 * the device at run time. Doing that while the device is suspended causes
26 * it to be woken up.
28 * wakeup - Report/change current wakeup option for device
30 * Some devices support "wakeup" events, which are hardware signals
31 * used to activate devices from suspended or low power states. Such
32 * devices have one of three values for the sysfs power/wakeup file:
34 * + "enabled\n" to issue the events;
35 * + "disabled\n" not to do so; or
36 * + "\n" for temporary or permanent inability to issue wakeup.
38 * (For example, unconfigured USB devices can't issue wakeups.)
40 * Familiar examples of devices that can issue wakeup events include
41 * keyboards and mice (both PS2 and USB styles), power buttons, modems,
42 * "Wake-On-LAN" Ethernet links, GPIO lines, and more. Some events
43 * will wake the entire system from a suspend state; others may just
44 * wake up the device (if the system as a whole is already active).
45 * Some wakeup events use normal IRQ lines; other use special out
46 * of band signaling.
48 * It is the responsibility of device drivers to enable (or disable)
49 * wakeup signaling as part of changing device power states, respecting
50 * the policy choices provided through the driver model.
52 * Devices may not be able to generate wakeup events from all power
53 * states. Also, the events may be ignored in some configurations;
54 * for example, they might need help from other devices that aren't
55 * active, or which may have wakeup disabled. Some drivers rely on
56 * wakeup events internally (unless they are disabled), keeping
57 * their hardware in low power modes whenever they're unused. This
58 * saves runtime power, without requiring system-wide sleep states.
60 * async - Report/change current async suspend setting for the device
62 * Asynchronous suspend and resume of the device during system-wide power
63 * state transitions can be enabled by writing "enabled" to this file.
64 * Analogously, if "disabled" is written to this file, the device will be
65 * suspended and resumed synchronously.
67 * All devices have one of the following two values for power/async:
69 * + "enabled\n" to permit the asynchronous suspend/resume of the device;
70 * + "disabled\n" to forbid it;
72 * NOTE: It generally is unsafe to permit the asynchronous suspend/resume
73 * of a device unless it is certain that all of the PM dependencies of the
74 * device are known to the PM core. However, for some devices this
75 * attribute is set to "enabled" by bus type code or device drivers and in
76 * that cases it should be safe to leave the default value.
78 * wakeup_count - Report the number of wakeup events related to the device
81 static const char enabled[] = "enabled";
82 static const char disabled[] = "disabled";
84 const char power_group_name[] = "power";
85 EXPORT_SYMBOL_GPL(power_group_name);
87 #ifdef CONFIG_PM_RUNTIME
88 static const char ctrl_auto[] = "auto";
89 static const char ctrl_on[] = "on";
91 static ssize_t control_show(struct device *dev, struct device_attribute *attr,
92 char *buf)
94 return sprintf(buf, "%s\n",
95 dev->power.runtime_auto ? ctrl_auto : ctrl_on);
98 static ssize_t control_store(struct device * dev, struct device_attribute *attr,
99 const char * buf, size_t n)
101 char *cp;
102 int len = n;
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;
116 static DEVICE_ATTR(control, 0644, control_show, control_store);
118 static ssize_t rtpm_active_time_show(struct device *dev,
119 struct device_attribute *attr, char *buf)
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;
129 static DEVICE_ATTR(runtime_active_time, 0444, rtpm_active_time_show, NULL);
131 static ssize_t rtpm_suspended_time_show(struct device *dev,
132 struct device_attribute *attr, char *buf)
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;
143 static DEVICE_ATTR(runtime_suspended_time, 0444, rtpm_suspended_time_show, NULL);
145 static ssize_t rtpm_status_show(struct device *dev,
146 struct device_attribute *attr, char *buf)
148 const char *p;
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;
172 return sprintf(buf, p);
175 static DEVICE_ATTR(runtime_status, 0444, rtpm_status_show, NULL);
176 #endif
178 static ssize_t
179 wake_show(struct device * dev, struct device_attribute *attr, char * buf)
181 return sprintf(buf, "%s\n", device_can_wakeup(dev)
182 ? (device_may_wakeup(dev) ? enabled : disabled)
183 : "");
186 static ssize_t
187 wake_store(struct device * dev, struct device_attribute *attr,
188 const char * buf, size_t n)
190 char *cp;
191 int len = n;
193 if (!device_can_wakeup(dev))
194 return -EINVAL;
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;
210 static DEVICE_ATTR(wakeup, 0644, wake_show, wake_store);
212 #ifdef CONFIG_PM_SLEEP
213 static ssize_t wakeup_count_show(struct device *dev,
214 struct device_attribute *attr, char *buf)
216 unsigned long count = 0;
217 bool enabled = false;
219 spin_lock_irq(&dev->power.lock);
220 if (dev->power.wakeup) {
221 count = dev->power.wakeup->event_count;
222 enabled = true;
224 spin_unlock_irq(&dev->power.lock);
225 return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
228 static DEVICE_ATTR(wakeup_count, 0444, wakeup_count_show, NULL);
230 static ssize_t wakeup_active_count_show(struct device *dev,
231 struct device_attribute *attr, char *buf)
233 unsigned long count = 0;
234 bool enabled = false;
236 spin_lock_irq(&dev->power.lock);
237 if (dev->power.wakeup) {
238 count = dev->power.wakeup->active_count;
239 enabled = true;
241 spin_unlock_irq(&dev->power.lock);
242 return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
245 static DEVICE_ATTR(wakeup_active_count, 0444, wakeup_active_count_show, NULL);
247 static ssize_t wakeup_hit_count_show(struct device *dev,
248 struct device_attribute *attr, char *buf)
250 unsigned long count = 0;
251 bool enabled = false;
253 spin_lock_irq(&dev->power.lock);
254 if (dev->power.wakeup) {
255 count = dev->power.wakeup->hit_count;
256 enabled = true;
258 spin_unlock_irq(&dev->power.lock);
259 return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
262 static DEVICE_ATTR(wakeup_hit_count, 0444, wakeup_hit_count_show, NULL);
264 static ssize_t wakeup_active_show(struct device *dev,
265 struct device_attribute *attr, char *buf)
267 unsigned int active = 0;
268 bool enabled = false;
270 spin_lock_irq(&dev->power.lock);
271 if (dev->power.wakeup) {
272 active = dev->power.wakeup->active;
273 enabled = true;
275 spin_unlock_irq(&dev->power.lock);
276 return enabled ? sprintf(buf, "%u\n", active) : sprintf(buf, "\n");
279 static DEVICE_ATTR(wakeup_active, 0444, wakeup_active_show, NULL);
281 static ssize_t wakeup_total_time_show(struct device *dev,
282 struct device_attribute *attr, char *buf)
284 s64 msec = 0;
285 bool enabled = false;
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;
292 spin_unlock_irq(&dev->power.lock);
293 return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
296 static DEVICE_ATTR(wakeup_total_time_ms, 0444, wakeup_total_time_show, NULL);
298 static ssize_t wakeup_max_time_show(struct device *dev,
299 struct device_attribute *attr, char *buf)
301 s64 msec = 0;
302 bool enabled = false;
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;
309 spin_unlock_irq(&dev->power.lock);
310 return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
313 static DEVICE_ATTR(wakeup_max_time_ms, 0444, wakeup_max_time_show, NULL);
315 static ssize_t wakeup_last_time_show(struct device *dev,
316 struct device_attribute *attr, char *buf)
318 s64 msec = 0;
319 bool enabled = false;
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;
326 spin_unlock_irq(&dev->power.lock);
327 return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
330 static DEVICE_ATTR(wakeup_last_time_ms, 0444, wakeup_last_time_show, NULL);
331 #endif /* CONFIG_PM_SLEEP */
333 #ifdef CONFIG_PM_ADVANCED_DEBUG
334 #ifdef CONFIG_PM_RUNTIME
336 static ssize_t rtpm_usagecount_show(struct device *dev,
337 struct device_attribute *attr, char *buf)
339 return sprintf(buf, "%d\n", atomic_read(&dev->power.usage_count));
342 static ssize_t rtpm_children_show(struct device *dev,
343 struct device_attribute *attr, char *buf)
345 return sprintf(buf, "%d\n", dev->power.ignore_children ?
346 0 : atomic_read(&dev->power.child_count));
349 static ssize_t rtpm_enabled_show(struct device *dev,
350 struct device_attribute *attr, char *buf)
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");
361 static DEVICE_ATTR(runtime_usage, 0444, rtpm_usagecount_show, NULL);
362 static DEVICE_ATTR(runtime_active_kids, 0444, rtpm_children_show, NULL);
363 static DEVICE_ATTR(runtime_enabled, 0444, rtpm_enabled_show, NULL);
365 #endif
367 static ssize_t async_show(struct device *dev, struct device_attribute *attr,
368 char *buf)
370 return sprintf(buf, "%s\n",
371 device_async_suspend_enabled(dev) ? enabled : disabled);
374 static ssize_t async_store(struct device *dev, struct device_attribute *attr,
375 const char *buf, size_t n)
377 char *cp;
378 int len = n;
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;
392 static DEVICE_ATTR(async, 0644, async_show, async_store);
393 #endif /* CONFIG_PM_ADVANCED_DEBUG */
395 static struct attribute * power_attrs[] = {
396 &dev_attr_wakeup.attr,
397 #ifdef CONFIG_PM_SLEEP
398 &dev_attr_wakeup_count.attr,
399 &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,
405 #endif
406 #ifdef CONFIG_PM_ADVANCED_DEBUG
407 &dev_attr_async.attr,
408 #ifdef CONFIG_PM_RUNTIME
409 &dev_attr_runtime_status.attr,
410 &dev_attr_runtime_usage.attr,
411 &dev_attr_runtime_active_kids.attr,
412 &dev_attr_runtime_enabled.attr,
413 #endif
414 #endif
415 NULL,
417 static struct attribute_group pm_attr_group = {
418 .name = power_group_name,
419 .attrs = power_attrs,
422 #ifdef CONFIG_PM_RUNTIME
424 static 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,
433 static struct attribute_group pm_runtime_attr_group = {
434 .name = power_group_name,
435 .attrs = runtime_attrs,
438 int dpm_sysfs_add(struct device *dev)
440 int rc;
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);
448 return rc;
451 void rpm_sysfs_remove(struct device *dev)
453 sysfs_unmerge_group(&dev->kobj, &pm_runtime_attr_group);
456 void dpm_sysfs_remove(struct device *dev)
458 rpm_sysfs_remove(dev);
459 sysfs_remove_group(&dev->kobj, &pm_attr_group);
462 #else /* CONFIG_PM_RUNTIME */
464 int dpm_sysfs_add(struct device * dev)
466 return sysfs_create_group(&dev->kobj, &pm_attr_group);
469 void dpm_sysfs_remove(struct device * dev)
471 sysfs_remove_group(&dev->kobj, &pm_attr_group);
474 #endif