regmap: Support register patch sets
[linux-2.6.git] / drivers / base / power / sysfs.c
1 /*
2  * drivers/base/power/sysfs.c - sysfs entries for device PM
3  */
4
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"
12
13 /*
14  *      control - Report/change current runtime PM setting of the device
15  *
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:
19  *
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;
22  *
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.
28  *
29  *      wakeup - Report/change current wakeup option for device
30  *
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:
34  *
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.
38  *
39  *      (For example, unconfigured USB devices can't issue wakeups.)
40  *
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.
48  *
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.
52  *
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.
60  *
61  *      async - Report/change current async suspend setting for the device
62  *
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.
67  *
68  *      All devices have one of the following two values for power/async:
69  *
70  *       + "enabled\n" to permit the asynchronous suspend/resume of the device;
71  *       + "disabled\n" to forbid it;
72  *
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.
78  *
79  *      autosuspend_delay_ms - Report/change a device's autosuspend_delay value
80  *
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.
87  *
88  *      NOTE: The autosuspend_delay_ms attribute and the autosuspend_delay
89  *      value are used only if the driver calls pm_runtime_use_autosuspend().
90  *
91  *      wakeup_count - Report the number of wakeup events related to the device
92  */
93
94 static const char enabled[] = "enabled";
95 static const char disabled[] = "disabled";
96
97 const char power_group_name[] = "power";
98 EXPORT_SYMBOL_GPL(power_group_name);
99
100 #ifdef CONFIG_PM_RUNTIME
101 static const char ctrl_auto[] = "auto";
102 static const char ctrl_on[] = "on";
103
104 static ssize_t control_show(struct device *dev, struct device_attribute *attr,
105                             char *buf)
106 {
107         return sprintf(buf, "%s\n",
108                                 dev->power.runtime_auto ? ctrl_auto : ctrl_on);
109 }
110
111 static ssize_t control_store(struct device * dev, struct device_attribute *attr,
112                              const char * buf, size_t n)
113 {
114         char *cp;
115         int len = n;
116
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;
129 }
130
131 static DEVICE_ATTR(control, 0644, control_show, control_store);
132
133 static ssize_t rtpm_active_time_show(struct device *dev,
134                                 struct device_attribute *attr, char *buf)
135 {
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;
142 }
143
144 static DEVICE_ATTR(runtime_active_time, 0444, rtpm_active_time_show, NULL);
145
146 static ssize_t rtpm_suspended_time_show(struct device *dev,
147                                 struct device_attribute *attr, char *buf)
148 {
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;
156 }
157
158 static DEVICE_ATTR(runtime_suspended_time, 0444, rtpm_suspended_time_show, NULL);
159
160 static ssize_t rtpm_status_show(struct device *dev,
161                                 struct device_attribute *attr, char *buf)
162 {
163         const char *p;
164
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;
185                 }
186         }
187         return sprintf(buf, p);
188 }
189
190 static DEVICE_ATTR(runtime_status, 0444, rtpm_status_show, NULL);
191
192 static ssize_t autosuspend_delay_ms_show(struct device *dev,
193                 struct device_attribute *attr, char *buf)
194 {
195         if (!dev->power.use_autosuspend)
196                 return -EIO;
197         return sprintf(buf, "%d\n", dev->power.autosuspend_delay);
198 }
199
200 static ssize_t autosuspend_delay_ms_store(struct device *dev,
201                 struct device_attribute *attr, const char *buf, size_t n)
202 {
203         long delay;
204
205         if (!dev->power.use_autosuspend)
206                 return -EIO;
207
208         if (strict_strtol(buf, 10, &delay) != 0 || delay != (int) delay)
209                 return -EINVAL;
210
211         device_lock(dev);
212         pm_runtime_set_autosuspend_delay(dev, delay);
213         device_unlock(dev);
214         return n;
215 }
216
217 static DEVICE_ATTR(autosuspend_delay_ms, 0644, autosuspend_delay_ms_show,
218                 autosuspend_delay_ms_store);
219
220 #endif /* CONFIG_PM_RUNTIME */
221
222 #ifdef CONFIG_PM_SLEEP
223 static ssize_t
224 wake_show(struct device * dev, struct device_attribute *attr, char * buf)
225 {
226         return sprintf(buf, "%s\n", device_can_wakeup(dev)
227                 ? (device_may_wakeup(dev) ? enabled : disabled)
228                 : "");
229 }
230
231 static ssize_t
232 wake_store(struct device * dev, struct device_attribute *attr,
233         const char * buf, size_t n)
234 {
235         char *cp;
236         int len = n;
237
238         if (!device_can_wakeup(dev))
239                 return -EINVAL;
240
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;
253 }
254
255 static DEVICE_ATTR(wakeup, 0644, wake_show, wake_store);
256
257 static ssize_t wakeup_count_show(struct device *dev,
258                                 struct device_attribute *attr, char *buf)
259 {
260         unsigned long count = 0;
261         bool enabled = false;
262
263         spin_lock_irq(&dev->power.lock);
264         if (dev->power.wakeup) {
265                 count = dev->power.wakeup->event_count;
266                 enabled = true;
267         }
268         spin_unlock_irq(&dev->power.lock);
269         return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
270 }
271
272 static DEVICE_ATTR(wakeup_count, 0444, wakeup_count_show, NULL);
273
274 static ssize_t wakeup_active_count_show(struct device *dev,
275                                 struct device_attribute *attr, char *buf)
276 {
277         unsigned long count = 0;
278         bool enabled = false;
279
280         spin_lock_irq(&dev->power.lock);
281         if (dev->power.wakeup) {
282                 count = dev->power.wakeup->active_count;
283                 enabled = true;
284         }
285         spin_unlock_irq(&dev->power.lock);
286         return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
287 }
288
289 static DEVICE_ATTR(wakeup_active_count, 0444, wakeup_active_count_show, NULL);
290
291 static ssize_t wakeup_hit_count_show(struct device *dev,
292                                 struct device_attribute *attr, char *buf)
293 {
294         unsigned long count = 0;
295         bool enabled = false;
296
297         spin_lock_irq(&dev->power.lock);
298         if (dev->power.wakeup) {
299                 count = dev->power.wakeup->hit_count;
300                 enabled = true;
301         }
302         spin_unlock_irq(&dev->power.lock);
303         return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
304 }
305
306 static DEVICE_ATTR(wakeup_hit_count, 0444, wakeup_hit_count_show, NULL);
307
308 static ssize_t wakeup_active_show(struct device *dev,
309                                 struct device_attribute *attr, char *buf)
310 {
311         unsigned int active = 0;
312         bool enabled = false;
313
314         spin_lock_irq(&dev->power.lock);
315         if (dev->power.wakeup) {
316                 active = dev->power.wakeup->active;
317                 enabled = true;
318         }
319         spin_unlock_irq(&dev->power.lock);
320         return enabled ? sprintf(buf, "%u\n", active) : sprintf(buf, "\n");
321 }
322
323 static DEVICE_ATTR(wakeup_active, 0444, wakeup_active_show, NULL);
324
325 static ssize_t wakeup_total_time_show(struct device *dev,
326                                 struct device_attribute *attr, char *buf)
327 {
328         s64 msec = 0;
329         bool enabled = false;
330
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;
335         }
336         spin_unlock_irq(&dev->power.lock);
337         return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
338 }
339
340 static DEVICE_ATTR(wakeup_total_time_ms, 0444, wakeup_total_time_show, NULL);
341
342 static ssize_t wakeup_max_time_show(struct device *dev,
343                                 struct device_attribute *attr, char *buf)
344 {
345         s64 msec = 0;
346         bool enabled = false;
347
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;
352         }
353         spin_unlock_irq(&dev->power.lock);
354         return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
355 }
356
357 static DEVICE_ATTR(wakeup_max_time_ms, 0444, wakeup_max_time_show, NULL);
358
359 static ssize_t wakeup_last_time_show(struct device *dev,
360                                 struct device_attribute *attr, char *buf)
361 {
362         s64 msec = 0;
363         bool enabled = false;
364
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;
369         }
370         spin_unlock_irq(&dev->power.lock);
371         return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
372 }
373
374 static DEVICE_ATTR(wakeup_last_time_ms, 0444, wakeup_last_time_show, NULL);
375 #endif /* CONFIG_PM_SLEEP */
376
377 #ifdef CONFIG_PM_ADVANCED_DEBUG
378 #ifdef CONFIG_PM_RUNTIME
379
380 static ssize_t rtpm_usagecount_show(struct device *dev,
381                                     struct device_attribute *attr, char *buf)
382 {
383         return sprintf(buf, "%d\n", atomic_read(&dev->power.usage_count));
384 }
385
386 static ssize_t rtpm_children_show(struct device *dev,
387                                   struct device_attribute *attr, char *buf)
388 {
389         return sprintf(buf, "%d\n", dev->power.ignore_children ?
390                 0 : atomic_read(&dev->power.child_count));
391 }
392
393 static ssize_t rtpm_enabled_show(struct device *dev,
394                                  struct device_attribute *attr, char *buf)
395 {
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");
403 }
404
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);
408
409 #endif
410
411 static ssize_t async_show(struct device *dev, struct device_attribute *attr,
412                           char *buf)
413 {
414         return sprintf(buf, "%s\n",
415                         device_async_suspend_enabled(dev) ? enabled : disabled);
416 }
417
418 static ssize_t async_store(struct device *dev, struct device_attribute *attr,
419                            const char *buf, size_t n)
420 {
421         char *cp;
422         int len = n;
423
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;
434 }
435
436 static DEVICE_ATTR(async, 0644, async_show, async_store);
437 #endif /* CONFIG_PM_ADVANCED_DEBUG */
438
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,
452 };
453 static struct attribute_group pm_attr_group = {
454         .name   = power_group_name,
455         .attrs  = power_attrs,
456 };
457
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,
470 };
471 static struct attribute_group pm_wakeup_attr_group = {
472         .name   = power_group_name,
473         .attrs  = wakeup_attrs,
474 };
475
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,
487 };
488 static struct attribute_group pm_runtime_attr_group = {
489         .name   = power_group_name,
490         .attrs  = runtime_attrs,
491 };
492
493 int dpm_sysfs_add(struct device *dev)
494 {
495         int rc;
496
497         rc = sysfs_create_group(&dev->kobj, &pm_attr_group);
498         if (rc)
499                 return rc;
500
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;
505         }
506
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;
514                 }
515         }
516         return 0;
517
518  err_out:
519         sysfs_remove_group(&dev->kobj, &pm_attr_group);
520         return rc;
521 }
522
523 int wakeup_sysfs_add(struct device *dev)
524 {
525         return sysfs_merge_group(&dev->kobj, &pm_wakeup_attr_group);
526 }
527
528 void wakeup_sysfs_remove(struct device *dev)
529 {
530         sysfs_unmerge_group(&dev->kobj, &pm_wakeup_attr_group);
531 }
532
533 void rpm_sysfs_remove(struct device *dev)
534 {
535         sysfs_unmerge_group(&dev->kobj, &pm_runtime_attr_group);
536 }
537
538 void dpm_sysfs_remove(struct device *dev)
539 {
540         rpm_sysfs_remove(dev);
541         sysfs_unmerge_group(&dev->kobj, &pm_wakeup_attr_group);
542         sysfs_remove_group(&dev->kobj, &pm_attr_group);
543 }