xref: /linux/drivers/base/power/sysfs.c (revision c54ea4918c2b7722d7242ea53271356501988a9b)
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/pm_runtime.h>
8 #include <asm/atomic.h>
9 #include <linux/jiffies.h>
10 #include "power.h"
11 
12 /*
13  *	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  *
28  *	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.
59  *
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.
77  *
78  *	autosuspend_delay_ms - Report/change a device's autosuspend_delay value
79  *
80  *	Some drivers don't want to carry out a runtime suspend as soon as a
81  *	device becomes idle; they want it always to remain idle for some period
82  *	of time before suspending it.  This period is the autosuspend_delay
83  *	value (expressed in milliseconds) and it can be controlled by the user.
84  *	If the value is negative then the device will never be runtime
85  *	suspended.
86  *
87  *	NOTE: The autosuspend_delay_ms attribute and the autosuspend_delay
88  *	value are used only if the driver calls pm_runtime_use_autosuspend().
89  *
90  *	wakeup_count - Report the number of wakeup events related to the device
91  */
92 
93 static const char enabled[] = "enabled";
94 static const char disabled[] = "disabled";
95 
96 const char power_group_name[] = "power";
97 EXPORT_SYMBOL_GPL(power_group_name);
98 
99 #ifdef CONFIG_PM_RUNTIME
100 static const char ctrl_auto[] = "auto";
101 static const char ctrl_on[] = "on";
102 
103 static ssize_t control_show(struct device *dev, struct device_attribute *attr,
104 			    char *buf)
105 {
106 	return sprintf(buf, "%s\n",
107 				dev->power.runtime_auto ? ctrl_auto : ctrl_on);
108 }
109 
110 static ssize_t control_store(struct device * dev, struct device_attribute *attr,
111 			     const char * buf, size_t n)
112 {
113 	char *cp;
114 	int len = n;
115 
116 	cp = memchr(buf, '\n', n);
117 	if (cp)
118 		len = cp - buf;
119 	if (len == sizeof ctrl_auto - 1 && strncmp(buf, ctrl_auto, len) == 0)
120 		pm_runtime_allow(dev);
121 	else if (len == sizeof ctrl_on - 1 && strncmp(buf, ctrl_on, len) == 0)
122 		pm_runtime_forbid(dev);
123 	else
124 		return -EINVAL;
125 	return n;
126 }
127 
128 static DEVICE_ATTR(control, 0644, control_show, control_store);
129 
130 static ssize_t rtpm_active_time_show(struct device *dev,
131 				struct device_attribute *attr, char *buf)
132 {
133 	int ret;
134 	spin_lock_irq(&dev->power.lock);
135 	update_pm_runtime_accounting(dev);
136 	ret = sprintf(buf, "%i\n", jiffies_to_msecs(dev->power.active_jiffies));
137 	spin_unlock_irq(&dev->power.lock);
138 	return ret;
139 }
140 
141 static DEVICE_ATTR(runtime_active_time, 0444, rtpm_active_time_show, NULL);
142 
143 static ssize_t rtpm_suspended_time_show(struct device *dev,
144 				struct device_attribute *attr, char *buf)
145 {
146 	int ret;
147 	spin_lock_irq(&dev->power.lock);
148 	update_pm_runtime_accounting(dev);
149 	ret = sprintf(buf, "%i\n",
150 		jiffies_to_msecs(dev->power.suspended_jiffies));
151 	spin_unlock_irq(&dev->power.lock);
152 	return ret;
153 }
154 
155 static DEVICE_ATTR(runtime_suspended_time, 0444, rtpm_suspended_time_show, NULL);
156 
157 static ssize_t rtpm_status_show(struct device *dev,
158 				struct device_attribute *attr, char *buf)
159 {
160 	const char *p;
161 
162 	if (dev->power.runtime_error) {
163 		p = "error\n";
164 	} else if (dev->power.disable_depth) {
165 		p = "unsupported\n";
166 	} else {
167 		switch (dev->power.runtime_status) {
168 		case RPM_SUSPENDED:
169 			p = "suspended\n";
170 			break;
171 		case RPM_SUSPENDING:
172 			p = "suspending\n";
173 			break;
174 		case RPM_RESUMING:
175 			p = "resuming\n";
176 			break;
177 		case RPM_ACTIVE:
178 			p = "active\n";
179 			break;
180 		default:
181 			return -EIO;
182 		}
183 	}
184 	return sprintf(buf, p);
185 }
186 
187 static DEVICE_ATTR(runtime_status, 0444, rtpm_status_show, NULL);
188 
189 static ssize_t autosuspend_delay_ms_show(struct device *dev,
190 		struct device_attribute *attr, char *buf)
191 {
192 	if (!dev->power.use_autosuspend)
193 		return -EIO;
194 	return sprintf(buf, "%d\n", dev->power.autosuspend_delay);
195 }
196 
197 static ssize_t autosuspend_delay_ms_store(struct device *dev,
198 		struct device_attribute *attr, const char *buf, size_t n)
199 {
200 	long delay;
201 
202 	if (!dev->power.use_autosuspend)
203 		return -EIO;
204 
205 	if (strict_strtol(buf, 10, &delay) != 0 || delay != (int) delay)
206 		return -EINVAL;
207 
208 	pm_runtime_set_autosuspend_delay(dev, delay);
209 	return n;
210 }
211 
212 static DEVICE_ATTR(autosuspend_delay_ms, 0644, autosuspend_delay_ms_show,
213 		autosuspend_delay_ms_store);
214 
215 #endif
216 
217 static ssize_t
218 wake_show(struct device * dev, struct device_attribute *attr, char * buf)
219 {
220 	return sprintf(buf, "%s\n", device_can_wakeup(dev)
221 		? (device_may_wakeup(dev) ? enabled : disabled)
222 		: "");
223 }
224 
225 static ssize_t
226 wake_store(struct device * dev, struct device_attribute *attr,
227 	const char * buf, size_t n)
228 {
229 	char *cp;
230 	int len = n;
231 
232 	if (!device_can_wakeup(dev))
233 		return -EINVAL;
234 
235 	cp = memchr(buf, '\n', n);
236 	if (cp)
237 		len = cp - buf;
238 	if (len == sizeof enabled - 1
239 			&& strncmp(buf, enabled, sizeof enabled - 1) == 0)
240 		device_set_wakeup_enable(dev, 1);
241 	else if (len == sizeof disabled - 1
242 			&& strncmp(buf, disabled, sizeof disabled - 1) == 0)
243 		device_set_wakeup_enable(dev, 0);
244 	else
245 		return -EINVAL;
246 	return n;
247 }
248 
249 static DEVICE_ATTR(wakeup, 0644, wake_show, wake_store);
250 
251 #ifdef CONFIG_PM_SLEEP
252 static ssize_t wakeup_count_show(struct device *dev,
253 				struct device_attribute *attr, char *buf)
254 {
255 	unsigned long count = 0;
256 	bool enabled = false;
257 
258 	spin_lock_irq(&dev->power.lock);
259 	if (dev->power.wakeup) {
260 		count = dev->power.wakeup->event_count;
261 		enabled = true;
262 	}
263 	spin_unlock_irq(&dev->power.lock);
264 	return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
265 }
266 
267 static DEVICE_ATTR(wakeup_count, 0444, wakeup_count_show, NULL);
268 
269 static ssize_t wakeup_active_count_show(struct device *dev,
270 				struct device_attribute *attr, char *buf)
271 {
272 	unsigned long count = 0;
273 	bool enabled = false;
274 
275 	spin_lock_irq(&dev->power.lock);
276 	if (dev->power.wakeup) {
277 		count = dev->power.wakeup->active_count;
278 		enabled = true;
279 	}
280 	spin_unlock_irq(&dev->power.lock);
281 	return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
282 }
283 
284 static DEVICE_ATTR(wakeup_active_count, 0444, wakeup_active_count_show, NULL);
285 
286 static ssize_t wakeup_hit_count_show(struct device *dev,
287 				struct device_attribute *attr, char *buf)
288 {
289 	unsigned long count = 0;
290 	bool enabled = false;
291 
292 	spin_lock_irq(&dev->power.lock);
293 	if (dev->power.wakeup) {
294 		count = dev->power.wakeup->hit_count;
295 		enabled = true;
296 	}
297 	spin_unlock_irq(&dev->power.lock);
298 	return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
299 }
300 
301 static DEVICE_ATTR(wakeup_hit_count, 0444, wakeup_hit_count_show, NULL);
302 
303 static ssize_t wakeup_active_show(struct device *dev,
304 				struct device_attribute *attr, char *buf)
305 {
306 	unsigned int active = 0;
307 	bool enabled = false;
308 
309 	spin_lock_irq(&dev->power.lock);
310 	if (dev->power.wakeup) {
311 		active = dev->power.wakeup->active;
312 		enabled = true;
313 	}
314 	spin_unlock_irq(&dev->power.lock);
315 	return enabled ? sprintf(buf, "%u\n", active) : sprintf(buf, "\n");
316 }
317 
318 static DEVICE_ATTR(wakeup_active, 0444, wakeup_active_show, NULL);
319 
320 static ssize_t wakeup_total_time_show(struct device *dev,
321 				struct device_attribute *attr, char *buf)
322 {
323 	s64 msec = 0;
324 	bool enabled = false;
325 
326 	spin_lock_irq(&dev->power.lock);
327 	if (dev->power.wakeup) {
328 		msec = ktime_to_ms(dev->power.wakeup->total_time);
329 		enabled = true;
330 	}
331 	spin_unlock_irq(&dev->power.lock);
332 	return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
333 }
334 
335 static DEVICE_ATTR(wakeup_total_time_ms, 0444, wakeup_total_time_show, NULL);
336 
337 static ssize_t wakeup_max_time_show(struct device *dev,
338 				struct device_attribute *attr, char *buf)
339 {
340 	s64 msec = 0;
341 	bool enabled = false;
342 
343 	spin_lock_irq(&dev->power.lock);
344 	if (dev->power.wakeup) {
345 		msec = ktime_to_ms(dev->power.wakeup->max_time);
346 		enabled = true;
347 	}
348 	spin_unlock_irq(&dev->power.lock);
349 	return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
350 }
351 
352 static DEVICE_ATTR(wakeup_max_time_ms, 0444, wakeup_max_time_show, NULL);
353 
354 static ssize_t wakeup_last_time_show(struct device *dev,
355 				struct device_attribute *attr, char *buf)
356 {
357 	s64 msec = 0;
358 	bool enabled = false;
359 
360 	spin_lock_irq(&dev->power.lock);
361 	if (dev->power.wakeup) {
362 		msec = ktime_to_ms(dev->power.wakeup->last_time);
363 		enabled = true;
364 	}
365 	spin_unlock_irq(&dev->power.lock);
366 	return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
367 }
368 
369 static DEVICE_ATTR(wakeup_last_time_ms, 0444, wakeup_last_time_show, NULL);
370 #endif /* CONFIG_PM_SLEEP */
371 
372 #ifdef CONFIG_PM_ADVANCED_DEBUG
373 #ifdef CONFIG_PM_RUNTIME
374 
375 static ssize_t rtpm_usagecount_show(struct device *dev,
376 				    struct device_attribute *attr, char *buf)
377 {
378 	return sprintf(buf, "%d\n", atomic_read(&dev->power.usage_count));
379 }
380 
381 static ssize_t rtpm_children_show(struct device *dev,
382 				  struct device_attribute *attr, char *buf)
383 {
384 	return sprintf(buf, "%d\n", dev->power.ignore_children ?
385 		0 : atomic_read(&dev->power.child_count));
386 }
387 
388 static ssize_t rtpm_enabled_show(struct device *dev,
389 				 struct device_attribute *attr, char *buf)
390 {
391 	if ((dev->power.disable_depth) && (dev->power.runtime_auto == false))
392 		return sprintf(buf, "disabled & forbidden\n");
393 	else if (dev->power.disable_depth)
394 		return sprintf(buf, "disabled\n");
395 	else if (dev->power.runtime_auto == false)
396 		return sprintf(buf, "forbidden\n");
397 	return sprintf(buf, "enabled\n");
398 }
399 
400 static DEVICE_ATTR(runtime_usage, 0444, rtpm_usagecount_show, NULL);
401 static DEVICE_ATTR(runtime_active_kids, 0444, rtpm_children_show, NULL);
402 static DEVICE_ATTR(runtime_enabled, 0444, rtpm_enabled_show, NULL);
403 
404 #endif
405 
406 static ssize_t async_show(struct device *dev, struct device_attribute *attr,
407 			  char *buf)
408 {
409 	return sprintf(buf, "%s\n",
410 			device_async_suspend_enabled(dev) ? enabled : disabled);
411 }
412 
413 static ssize_t async_store(struct device *dev, struct device_attribute *attr,
414 			   const char *buf, size_t n)
415 {
416 	char *cp;
417 	int len = n;
418 
419 	cp = memchr(buf, '\n', n);
420 	if (cp)
421 		len = cp - buf;
422 	if (len == sizeof enabled - 1 && strncmp(buf, enabled, len) == 0)
423 		device_enable_async_suspend(dev);
424 	else if (len == sizeof disabled - 1 && strncmp(buf, disabled, len) == 0)
425 		device_disable_async_suspend(dev);
426 	else
427 		return -EINVAL;
428 	return n;
429 }
430 
431 static DEVICE_ATTR(async, 0644, async_show, async_store);
432 #endif /* CONFIG_PM_ADVANCED_DEBUG */
433 
434 static struct attribute *power_attrs[] = {
435 #ifdef CONFIG_PM_ADVANCED_DEBUG
436 #ifdef CONFIG_PM_SLEEP
437 	&dev_attr_async.attr,
438 #endif
439 #ifdef CONFIG_PM_RUNTIME
440 	&dev_attr_runtime_status.attr,
441 	&dev_attr_runtime_usage.attr,
442 	&dev_attr_runtime_active_kids.attr,
443 	&dev_attr_runtime_enabled.attr,
444 #endif
445 #endif /* CONFIG_PM_ADVANCED_DEBUG */
446 	NULL,
447 };
448 static struct attribute_group pm_attr_group = {
449 	.name	= power_group_name,
450 	.attrs	= power_attrs,
451 };
452 
453 static struct attribute *wakeup_attrs[] = {
454 #ifdef CONFIG_PM_SLEEP
455 	&dev_attr_wakeup.attr,
456 	&dev_attr_wakeup_count.attr,
457 	&dev_attr_wakeup_active_count.attr,
458 	&dev_attr_wakeup_hit_count.attr,
459 	&dev_attr_wakeup_active.attr,
460 	&dev_attr_wakeup_total_time_ms.attr,
461 	&dev_attr_wakeup_max_time_ms.attr,
462 	&dev_attr_wakeup_last_time_ms.attr,
463 #endif
464 	NULL,
465 };
466 static struct attribute_group pm_wakeup_attr_group = {
467 	.name	= power_group_name,
468 	.attrs	= wakeup_attrs,
469 };
470 
471 static struct attribute *runtime_attrs[] = {
472 #ifdef CONFIG_PM_RUNTIME
473 #ifndef CONFIG_PM_ADVANCED_DEBUG
474 	&dev_attr_runtime_status.attr,
475 #endif
476 	&dev_attr_control.attr,
477 	&dev_attr_runtime_suspended_time.attr,
478 	&dev_attr_runtime_active_time.attr,
479 	&dev_attr_autosuspend_delay_ms.attr,
480 #endif /* CONFIG_PM_RUNTIME */
481 	NULL,
482 };
483 static struct attribute_group pm_runtime_attr_group = {
484 	.name	= power_group_name,
485 	.attrs	= runtime_attrs,
486 };
487 
488 int dpm_sysfs_add(struct device *dev)
489 {
490 	int rc;
491 
492 	rc = sysfs_create_group(&dev->kobj, &pm_attr_group);
493 	if (rc)
494 		return rc;
495 
496 	if (pm_runtime_callbacks_present(dev)) {
497 		rc = sysfs_merge_group(&dev->kobj, &pm_runtime_attr_group);
498 		if (rc)
499 			goto err_out;
500 	}
501 
502 	if (device_can_wakeup(dev)) {
503 		rc = sysfs_merge_group(&dev->kobj, &pm_wakeup_attr_group);
504 		if (rc) {
505 			if (pm_runtime_callbacks_present(dev))
506 				sysfs_unmerge_group(&dev->kobj,
507 						    &pm_runtime_attr_group);
508 			goto err_out;
509 		}
510 	}
511 	return 0;
512 
513  err_out:
514 	sysfs_remove_group(&dev->kobj, &pm_attr_group);
515 	return rc;
516 }
517 
518 int wakeup_sysfs_add(struct device *dev)
519 {
520 	return sysfs_merge_group(&dev->kobj, &pm_wakeup_attr_group);
521 }
522 
523 void wakeup_sysfs_remove(struct device *dev)
524 {
525 	sysfs_unmerge_group(&dev->kobj, &pm_wakeup_attr_group);
526 }
527 
528 void rpm_sysfs_remove(struct device *dev)
529 {
530 	sysfs_unmerge_group(&dev->kobj, &pm_runtime_attr_group);
531 }
532 
533 void dpm_sysfs_remove(struct device *dev)
534 {
535 	rpm_sysfs_remove(dev);
536 	sysfs_unmerge_group(&dev->kobj, &pm_wakeup_attr_group);
537 	sysfs_remove_group(&dev->kobj, &pm_attr_group);
538 }
539