xref: /linux/drivers/base/power/sysfs.c (revision 9abdb50cda0ffe33bbb2e40cbad97b32fb7ff892)
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_qos.h>
9 #include <linux/pm_runtime.h>
10 #include <linux/atomic.h>
11 #include <linux/jiffies.h>
12 #include "power.h"
13 
14 /*
15  *	control - Report/change current runtime PM setting of the device
16  *
17  *	Runtime power management of a device can be blocked with the help of
18  *	this attribute.  All devices have one of the following two values for
19  *	the power/control file:
20  *
21  *	 + "auto\n" to allow the device to be power managed at run time;
22  *	 + "on\n" to prevent the device from being power managed at run time;
23  *
24  *	The default for all devices is "auto", which means that devices may be
25  *	subject to automatic power management, depending on their drivers.
26  *	Changing this attribute to "on" prevents the driver from power managing
27  *	the device at run time.  Doing that while the device is suspended causes
28  *	it to be woken up.
29  *
30  *	wakeup - Report/change current wakeup option for device
31  *
32  *	Some devices support "wakeup" events, which are hardware signals
33  *	used to activate devices from suspended or low power states.  Such
34  *	devices have one of three values for the sysfs power/wakeup file:
35  *
36  *	 + "enabled\n" to issue the events;
37  *	 + "disabled\n" not to do so; or
38  *	 + "\n" for temporary or permanent inability to issue wakeup.
39  *
40  *	(For example, unconfigured USB devices can't issue wakeups.)
41  *
42  *	Familiar examples of devices that can issue wakeup events include
43  *	keyboards and mice (both PS2 and USB styles), power buttons, modems,
44  *	"Wake-On-LAN" Ethernet links, GPIO lines, and more.  Some events
45  *	will wake the entire system from a suspend state; others may just
46  *	wake up the device (if the system as a whole is already active).
47  *	Some wakeup events use normal IRQ lines; other use special out
48  *	of band signaling.
49  *
50  *	It is the responsibility of device drivers to enable (or disable)
51  *	wakeup signaling as part of changing device power states, respecting
52  *	the policy choices provided through the driver model.
53  *
54  *	Devices may not be able to generate wakeup events from all power
55  *	states.  Also, the events may be ignored in some configurations;
56  *	for example, they might need help from other devices that aren't
57  *	active, or which may have wakeup disabled.  Some drivers rely on
58  *	wakeup events internally (unless they are disabled), keeping
59  *	their hardware in low power modes whenever they're unused.  This
60  *	saves runtime power, without requiring system-wide sleep states.
61  *
62  *	async - Report/change current async suspend setting for the device
63  *
64  *	Asynchronous suspend and resume of the device during system-wide power
65  *	state transitions can be enabled by writing "enabled" to this file.
66  *	Analogously, if "disabled" is written to this file, the device will be
67  *	suspended and resumed synchronously.
68  *
69  *	All devices have one of the following two values for power/async:
70  *
71  *	 + "enabled\n" to permit the asynchronous suspend/resume of the device;
72  *	 + "disabled\n" to forbid it;
73  *
74  *	NOTE: It generally is unsafe to permit the asynchronous suspend/resume
75  *	of a device unless it is certain that all of the PM dependencies of the
76  *	device are known to the PM core.  However, for some devices this
77  *	attribute is set to "enabled" by bus type code or device drivers and in
78  *	that cases it should be safe to leave the default value.
79  *
80  *	autosuspend_delay_ms - Report/change a device's autosuspend_delay value
81  *
82  *	Some drivers don't want to carry out a runtime suspend as soon as a
83  *	device becomes idle; they want it always to remain idle for some period
84  *	of time before suspending it.  This period is the autosuspend_delay
85  *	value (expressed in milliseconds) and it can be controlled by the user.
86  *	If the value is negative then the device will never be runtime
87  *	suspended.
88  *
89  *	NOTE: The autosuspend_delay_ms attribute and the autosuspend_delay
90  *	value are used only if the driver calls pm_runtime_use_autosuspend().
91  *
92  *	wakeup_count - Report the number of wakeup events related to the device
93  */
94 
95 const char power_group_name[] = "power";
96 EXPORT_SYMBOL_GPL(power_group_name);
97 
98 static const char ctrl_auto[] = "auto";
99 static const char ctrl_on[] = "on";
100 
101 static ssize_t control_show(struct device *dev, struct device_attribute *attr,
102 			    char *buf)
103 {
104 	return sprintf(buf, "%s\n",
105 				dev->power.runtime_auto ? ctrl_auto : ctrl_on);
106 }
107 
108 static ssize_t control_store(struct device * dev, struct device_attribute *attr,
109 			     const char * buf, size_t n)
110 {
111 	device_lock(dev);
112 	if (sysfs_streq(buf, ctrl_auto))
113 		pm_runtime_allow(dev);
114 	else if (sysfs_streq(buf, ctrl_on))
115 		pm_runtime_forbid(dev);
116 	else
117 		n = -EINVAL;
118 	device_unlock(dev);
119 	return n;
120 }
121 
122 static DEVICE_ATTR_RW(control);
123 
124 static ssize_t runtime_active_time_show(struct device *dev,
125 				struct device_attribute *attr, char *buf)
126 {
127 	int ret;
128 	u64 tmp;
129 	spin_lock_irq(&dev->power.lock);
130 	update_pm_runtime_accounting(dev);
131 	tmp = dev->power.active_time;
132 	do_div(tmp, NSEC_PER_MSEC);
133 	ret = sprintf(buf, "%llu\n", tmp);
134 	spin_unlock_irq(&dev->power.lock);
135 	return ret;
136 }
137 
138 static DEVICE_ATTR_RO(runtime_active_time);
139 
140 static ssize_t runtime_suspended_time_show(struct device *dev,
141 				struct device_attribute *attr, char *buf)
142 {
143 	int ret;
144 	u64 tmp;
145 	spin_lock_irq(&dev->power.lock);
146 	update_pm_runtime_accounting(dev);
147 	tmp = dev->power.suspended_time;
148 	do_div(tmp, NSEC_PER_MSEC);
149 	ret = sprintf(buf, "%llu\n", tmp);
150 	spin_unlock_irq(&dev->power.lock);
151 	return ret;
152 }
153 
154 static DEVICE_ATTR_RO(runtime_suspended_time);
155 
156 static ssize_t runtime_status_show(struct device *dev,
157 				struct device_attribute *attr, char *buf)
158 {
159 	const char *p;
160 
161 	if (dev->power.runtime_error) {
162 		p = "error\n";
163 	} else if (dev->power.disable_depth) {
164 		p = "unsupported\n";
165 	} else {
166 		switch (dev->power.runtime_status) {
167 		case RPM_SUSPENDED:
168 			p = "suspended\n";
169 			break;
170 		case RPM_SUSPENDING:
171 			p = "suspending\n";
172 			break;
173 		case RPM_RESUMING:
174 			p = "resuming\n";
175 			break;
176 		case RPM_ACTIVE:
177 			p = "active\n";
178 			break;
179 		default:
180 			return -EIO;
181 		}
182 	}
183 	return sprintf(buf, p);
184 }
185 
186 static DEVICE_ATTR_RO(runtime_status);
187 
188 static ssize_t autosuspend_delay_ms_show(struct device *dev,
189 		struct device_attribute *attr, char *buf)
190 {
191 	if (!dev->power.use_autosuspend)
192 		return -EIO;
193 	return sprintf(buf, "%d\n", dev->power.autosuspend_delay);
194 }
195 
196 static ssize_t autosuspend_delay_ms_store(struct device *dev,
197 		struct device_attribute *attr, const char *buf, size_t n)
198 {
199 	long delay;
200 
201 	if (!dev->power.use_autosuspend)
202 		return -EIO;
203 
204 	if (kstrtol(buf, 10, &delay) != 0 || delay != (int) delay)
205 		return -EINVAL;
206 
207 	device_lock(dev);
208 	pm_runtime_set_autosuspend_delay(dev, delay);
209 	device_unlock(dev);
210 	return n;
211 }
212 
213 static DEVICE_ATTR_RW(autosuspend_delay_ms);
214 
215 static ssize_t pm_qos_resume_latency_us_show(struct device *dev,
216 					     struct device_attribute *attr,
217 					     char *buf)
218 {
219 	s32 value = dev_pm_qos_requested_resume_latency(dev);
220 
221 	if (value == 0)
222 		return sprintf(buf, "n/a\n");
223 	if (value == PM_QOS_RESUME_LATENCY_NO_CONSTRAINT)
224 		value = 0;
225 
226 	return sprintf(buf, "%d\n", value);
227 }
228 
229 static ssize_t pm_qos_resume_latency_us_store(struct device *dev,
230 					      struct device_attribute *attr,
231 					      const char *buf, size_t n)
232 {
233 	s32 value;
234 	int ret;
235 
236 	if (!kstrtos32(buf, 0, &value)) {
237 		/*
238 		 * Prevent users from writing negative or "no constraint" values
239 		 * directly.
240 		 */
241 		if (value < 0 || value == PM_QOS_RESUME_LATENCY_NO_CONSTRAINT)
242 			return -EINVAL;
243 
244 		if (value == 0)
245 			value = PM_QOS_RESUME_LATENCY_NO_CONSTRAINT;
246 	} else if (sysfs_streq(buf, "n/a")) {
247 		value = 0;
248 	} else {
249 		return -EINVAL;
250 	}
251 
252 	ret = dev_pm_qos_update_request(dev->power.qos->resume_latency_req,
253 					value);
254 	return ret < 0 ? ret : n;
255 }
256 
257 static DEVICE_ATTR_RW(pm_qos_resume_latency_us);
258 
259 static ssize_t pm_qos_latency_tolerance_us_show(struct device *dev,
260 						struct device_attribute *attr,
261 						char *buf)
262 {
263 	s32 value = dev_pm_qos_get_user_latency_tolerance(dev);
264 
265 	if (value < 0)
266 		return sprintf(buf, "auto\n");
267 	if (value == PM_QOS_LATENCY_ANY)
268 		return sprintf(buf, "any\n");
269 
270 	return sprintf(buf, "%d\n", value);
271 }
272 
273 static ssize_t pm_qos_latency_tolerance_us_store(struct device *dev,
274 						 struct device_attribute *attr,
275 						 const char *buf, size_t n)
276 {
277 	s32 value;
278 	int ret;
279 
280 	if (kstrtos32(buf, 0, &value) == 0) {
281 		/* Users can't write negative values directly */
282 		if (value < 0)
283 			return -EINVAL;
284 	} else {
285 		if (sysfs_streq(buf, "auto"))
286 			value = PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT;
287 		else if (sysfs_streq(buf, "any"))
288 			value = PM_QOS_LATENCY_ANY;
289 		else
290 			return -EINVAL;
291 	}
292 	ret = dev_pm_qos_update_user_latency_tolerance(dev, value);
293 	return ret < 0 ? ret : n;
294 }
295 
296 static DEVICE_ATTR_RW(pm_qos_latency_tolerance_us);
297 
298 static ssize_t pm_qos_no_power_off_show(struct device *dev,
299 					struct device_attribute *attr,
300 					char *buf)
301 {
302 	return sprintf(buf, "%d\n", !!(dev_pm_qos_requested_flags(dev)
303 					& PM_QOS_FLAG_NO_POWER_OFF));
304 }
305 
306 static ssize_t pm_qos_no_power_off_store(struct device *dev,
307 					 struct device_attribute *attr,
308 					 const char *buf, size_t n)
309 {
310 	int ret;
311 
312 	if (kstrtoint(buf, 0, &ret))
313 		return -EINVAL;
314 
315 	if (ret != 0 && ret != 1)
316 		return -EINVAL;
317 
318 	ret = dev_pm_qos_update_flags(dev, PM_QOS_FLAG_NO_POWER_OFF, ret);
319 	return ret < 0 ? ret : n;
320 }
321 
322 static DEVICE_ATTR_RW(pm_qos_no_power_off);
323 
324 #ifdef CONFIG_PM_SLEEP
325 static const char _enabled[] = "enabled";
326 static const char _disabled[] = "disabled";
327 
328 static ssize_t wakeup_show(struct device *dev, struct device_attribute *attr,
329 			   char *buf)
330 {
331 	return sprintf(buf, "%s\n", device_can_wakeup(dev)
332 		? (device_may_wakeup(dev) ? _enabled : _disabled)
333 		: "");
334 }
335 
336 static ssize_t wakeup_store(struct device *dev, struct device_attribute *attr,
337 			    const char *buf, size_t n)
338 {
339 	if (!device_can_wakeup(dev))
340 		return -EINVAL;
341 
342 	if (sysfs_streq(buf, _enabled))
343 		device_set_wakeup_enable(dev, 1);
344 	else if (sysfs_streq(buf, _disabled))
345 		device_set_wakeup_enable(dev, 0);
346 	else
347 		return -EINVAL;
348 	return n;
349 }
350 
351 static DEVICE_ATTR_RW(wakeup);
352 
353 static ssize_t wakeup_count_show(struct device *dev,
354 				 struct device_attribute *attr, char *buf)
355 {
356 	unsigned long count = 0;
357 	bool enabled = false;
358 
359 	spin_lock_irq(&dev->power.lock);
360 	if (dev->power.wakeup) {
361 		count = dev->power.wakeup->wakeup_count;
362 		enabled = true;
363 	}
364 	spin_unlock_irq(&dev->power.lock);
365 	return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
366 }
367 
368 static DEVICE_ATTR_RO(wakeup_count);
369 
370 static ssize_t wakeup_active_count_show(struct device *dev,
371 					struct device_attribute *attr,
372 					char *buf)
373 {
374 	unsigned long count = 0;
375 	bool enabled = false;
376 
377 	spin_lock_irq(&dev->power.lock);
378 	if (dev->power.wakeup) {
379 		count = dev->power.wakeup->active_count;
380 		enabled = true;
381 	}
382 	spin_unlock_irq(&dev->power.lock);
383 	return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
384 }
385 
386 static DEVICE_ATTR_RO(wakeup_active_count);
387 
388 static ssize_t wakeup_abort_count_show(struct device *dev,
389 				       struct device_attribute *attr,
390 				       char *buf)
391 {
392 	unsigned long count = 0;
393 	bool enabled = false;
394 
395 	spin_lock_irq(&dev->power.lock);
396 	if (dev->power.wakeup) {
397 		count = dev->power.wakeup->wakeup_count;
398 		enabled = true;
399 	}
400 	spin_unlock_irq(&dev->power.lock);
401 	return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
402 }
403 
404 static DEVICE_ATTR_RO(wakeup_abort_count);
405 
406 static ssize_t wakeup_expire_count_show(struct device *dev,
407 					struct device_attribute *attr,
408 					char *buf)
409 {
410 	unsigned long count = 0;
411 	bool enabled = false;
412 
413 	spin_lock_irq(&dev->power.lock);
414 	if (dev->power.wakeup) {
415 		count = dev->power.wakeup->expire_count;
416 		enabled = true;
417 	}
418 	spin_unlock_irq(&dev->power.lock);
419 	return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
420 }
421 
422 static DEVICE_ATTR_RO(wakeup_expire_count);
423 
424 static ssize_t wakeup_active_show(struct device *dev,
425 				  struct device_attribute *attr, char *buf)
426 {
427 	unsigned int active = 0;
428 	bool enabled = false;
429 
430 	spin_lock_irq(&dev->power.lock);
431 	if (dev->power.wakeup) {
432 		active = dev->power.wakeup->active;
433 		enabled = true;
434 	}
435 	spin_unlock_irq(&dev->power.lock);
436 	return enabled ? sprintf(buf, "%u\n", active) : sprintf(buf, "\n");
437 }
438 
439 static DEVICE_ATTR_RO(wakeup_active);
440 
441 static ssize_t wakeup_total_time_ms_show(struct device *dev,
442 					 struct device_attribute *attr,
443 					 char *buf)
444 {
445 	s64 msec = 0;
446 	bool enabled = false;
447 
448 	spin_lock_irq(&dev->power.lock);
449 	if (dev->power.wakeup) {
450 		msec = ktime_to_ms(dev->power.wakeup->total_time);
451 		enabled = true;
452 	}
453 	spin_unlock_irq(&dev->power.lock);
454 	return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
455 }
456 
457 static DEVICE_ATTR_RO(wakeup_total_time_ms);
458 
459 static ssize_t wakeup_max_time_ms_show(struct device *dev,
460 				       struct device_attribute *attr, char *buf)
461 {
462 	s64 msec = 0;
463 	bool enabled = false;
464 
465 	spin_lock_irq(&dev->power.lock);
466 	if (dev->power.wakeup) {
467 		msec = ktime_to_ms(dev->power.wakeup->max_time);
468 		enabled = true;
469 	}
470 	spin_unlock_irq(&dev->power.lock);
471 	return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
472 }
473 
474 static DEVICE_ATTR_RO(wakeup_max_time_ms);
475 
476 static ssize_t wakeup_last_time_ms_show(struct device *dev,
477 					struct device_attribute *attr,
478 					char *buf)
479 {
480 	s64 msec = 0;
481 	bool enabled = false;
482 
483 	spin_lock_irq(&dev->power.lock);
484 	if (dev->power.wakeup) {
485 		msec = ktime_to_ms(dev->power.wakeup->last_time);
486 		enabled = true;
487 	}
488 	spin_unlock_irq(&dev->power.lock);
489 	return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
490 }
491 
492 static DEVICE_ATTR_RO(wakeup_last_time_ms);
493 
494 #ifdef CONFIG_PM_AUTOSLEEP
495 static ssize_t wakeup_prevent_sleep_time_ms_show(struct device *dev,
496 						 struct device_attribute *attr,
497 						 char *buf)
498 {
499 	s64 msec = 0;
500 	bool enabled = false;
501 
502 	spin_lock_irq(&dev->power.lock);
503 	if (dev->power.wakeup) {
504 		msec = ktime_to_ms(dev->power.wakeup->prevent_sleep_time);
505 		enabled = true;
506 	}
507 	spin_unlock_irq(&dev->power.lock);
508 	return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
509 }
510 
511 static DEVICE_ATTR_RO(wakeup_prevent_sleep_time_ms);
512 #endif /* CONFIG_PM_AUTOSLEEP */
513 #endif /* CONFIG_PM_SLEEP */
514 
515 #ifdef CONFIG_PM_ADVANCED_DEBUG
516 static ssize_t runtime_usage_show(struct device *dev,
517 				  struct device_attribute *attr, char *buf)
518 {
519 	return sprintf(buf, "%d\n", atomic_read(&dev->power.usage_count));
520 }
521 static DEVICE_ATTR_RO(runtime_usage);
522 
523 static ssize_t runtime_active_kids_show(struct device *dev,
524 					struct device_attribute *attr,
525 					char *buf)
526 {
527 	return sprintf(buf, "%d\n", dev->power.ignore_children ?
528 		0 : atomic_read(&dev->power.child_count));
529 }
530 static DEVICE_ATTR_RO(runtime_active_kids);
531 
532 static ssize_t runtime_enabled_show(struct device *dev,
533 				    struct device_attribute *attr, char *buf)
534 {
535 	if (dev->power.disable_depth && (dev->power.runtime_auto == false))
536 		return sprintf(buf, "disabled & forbidden\n");
537 	if (dev->power.disable_depth)
538 		return sprintf(buf, "disabled\n");
539 	if (dev->power.runtime_auto == false)
540 		return sprintf(buf, "forbidden\n");
541 	return sprintf(buf, "enabled\n");
542 }
543 static DEVICE_ATTR_RO(runtime_enabled);
544 
545 #ifdef CONFIG_PM_SLEEP
546 static ssize_t async_show(struct device *dev, struct device_attribute *attr,
547 			  char *buf)
548 {
549 	return sprintf(buf, "%s\n",
550 			device_async_suspend_enabled(dev) ?
551 				_enabled : _disabled);
552 }
553 
554 static ssize_t async_store(struct device *dev, struct device_attribute *attr,
555 			   const char *buf, size_t n)
556 {
557 	if (sysfs_streq(buf, _enabled))
558 		device_enable_async_suspend(dev);
559 	else if (sysfs_streq(buf, _disabled))
560 		device_disable_async_suspend(dev);
561 	else
562 		return -EINVAL;
563 	return n;
564 }
565 
566 static DEVICE_ATTR_RW(async);
567 
568 #endif /* CONFIG_PM_SLEEP */
569 #endif /* CONFIG_PM_ADVANCED_DEBUG */
570 
571 static struct attribute *power_attrs[] = {
572 #ifdef CONFIG_PM_ADVANCED_DEBUG
573 #ifdef CONFIG_PM_SLEEP
574 	&dev_attr_async.attr,
575 #endif
576 	&dev_attr_runtime_status.attr,
577 	&dev_attr_runtime_usage.attr,
578 	&dev_attr_runtime_active_kids.attr,
579 	&dev_attr_runtime_enabled.attr,
580 #endif /* CONFIG_PM_ADVANCED_DEBUG */
581 	NULL,
582 };
583 static const struct attribute_group pm_attr_group = {
584 	.name	= power_group_name,
585 	.attrs	= power_attrs,
586 };
587 
588 static struct attribute *wakeup_attrs[] = {
589 #ifdef CONFIG_PM_SLEEP
590 	&dev_attr_wakeup.attr,
591 	&dev_attr_wakeup_count.attr,
592 	&dev_attr_wakeup_active_count.attr,
593 	&dev_attr_wakeup_abort_count.attr,
594 	&dev_attr_wakeup_expire_count.attr,
595 	&dev_attr_wakeup_active.attr,
596 	&dev_attr_wakeup_total_time_ms.attr,
597 	&dev_attr_wakeup_max_time_ms.attr,
598 	&dev_attr_wakeup_last_time_ms.attr,
599 #ifdef CONFIG_PM_AUTOSLEEP
600 	&dev_attr_wakeup_prevent_sleep_time_ms.attr,
601 #endif
602 #endif
603 	NULL,
604 };
605 static const struct attribute_group pm_wakeup_attr_group = {
606 	.name	= power_group_name,
607 	.attrs	= wakeup_attrs,
608 };
609 
610 static struct attribute *runtime_attrs[] = {
611 #ifndef CONFIG_PM_ADVANCED_DEBUG
612 	&dev_attr_runtime_status.attr,
613 #endif
614 	&dev_attr_control.attr,
615 	&dev_attr_runtime_suspended_time.attr,
616 	&dev_attr_runtime_active_time.attr,
617 	&dev_attr_autosuspend_delay_ms.attr,
618 	NULL,
619 };
620 static const struct attribute_group pm_runtime_attr_group = {
621 	.name	= power_group_name,
622 	.attrs	= runtime_attrs,
623 };
624 
625 static struct attribute *pm_qos_resume_latency_attrs[] = {
626 	&dev_attr_pm_qos_resume_latency_us.attr,
627 	NULL,
628 };
629 static const struct attribute_group pm_qos_resume_latency_attr_group = {
630 	.name	= power_group_name,
631 	.attrs	= pm_qos_resume_latency_attrs,
632 };
633 
634 static struct attribute *pm_qos_latency_tolerance_attrs[] = {
635 	&dev_attr_pm_qos_latency_tolerance_us.attr,
636 	NULL,
637 };
638 static const struct attribute_group pm_qos_latency_tolerance_attr_group = {
639 	.name	= power_group_name,
640 	.attrs	= pm_qos_latency_tolerance_attrs,
641 };
642 
643 static struct attribute *pm_qos_flags_attrs[] = {
644 	&dev_attr_pm_qos_no_power_off.attr,
645 	NULL,
646 };
647 static const struct attribute_group pm_qos_flags_attr_group = {
648 	.name	= power_group_name,
649 	.attrs	= pm_qos_flags_attrs,
650 };
651 
652 int dpm_sysfs_add(struct device *dev)
653 {
654 	int rc;
655 
656 	/* No need to create PM sysfs if explicitly disabled. */
657 	if (device_pm_not_required(dev))
658 		return 0;
659 
660 	rc = sysfs_create_group(&dev->kobj, &pm_attr_group);
661 	if (rc)
662 		return rc;
663 
664 	if (pm_runtime_callbacks_present(dev)) {
665 		rc = sysfs_merge_group(&dev->kobj, &pm_runtime_attr_group);
666 		if (rc)
667 			goto err_out;
668 	}
669 	if (device_can_wakeup(dev)) {
670 		rc = sysfs_merge_group(&dev->kobj, &pm_wakeup_attr_group);
671 		if (rc)
672 			goto err_runtime;
673 	}
674 	if (dev->power.set_latency_tolerance) {
675 		rc = sysfs_merge_group(&dev->kobj,
676 				       &pm_qos_latency_tolerance_attr_group);
677 		if (rc)
678 			goto err_wakeup;
679 	}
680 	return 0;
681 
682  err_wakeup:
683 	sysfs_unmerge_group(&dev->kobj, &pm_wakeup_attr_group);
684  err_runtime:
685 	sysfs_unmerge_group(&dev->kobj, &pm_runtime_attr_group);
686  err_out:
687 	sysfs_remove_group(&dev->kobj, &pm_attr_group);
688 	return rc;
689 }
690 
691 int wakeup_sysfs_add(struct device *dev)
692 {
693 	return sysfs_merge_group(&dev->kobj, &pm_wakeup_attr_group);
694 }
695 
696 void wakeup_sysfs_remove(struct device *dev)
697 {
698 	sysfs_unmerge_group(&dev->kobj, &pm_wakeup_attr_group);
699 }
700 
701 int pm_qos_sysfs_add_resume_latency(struct device *dev)
702 {
703 	return sysfs_merge_group(&dev->kobj, &pm_qos_resume_latency_attr_group);
704 }
705 
706 void pm_qos_sysfs_remove_resume_latency(struct device *dev)
707 {
708 	sysfs_unmerge_group(&dev->kobj, &pm_qos_resume_latency_attr_group);
709 }
710 
711 int pm_qos_sysfs_add_flags(struct device *dev)
712 {
713 	return sysfs_merge_group(&dev->kobj, &pm_qos_flags_attr_group);
714 }
715 
716 void pm_qos_sysfs_remove_flags(struct device *dev)
717 {
718 	sysfs_unmerge_group(&dev->kobj, &pm_qos_flags_attr_group);
719 }
720 
721 int pm_qos_sysfs_add_latency_tolerance(struct device *dev)
722 {
723 	return sysfs_merge_group(&dev->kobj,
724 				 &pm_qos_latency_tolerance_attr_group);
725 }
726 
727 void pm_qos_sysfs_remove_latency_tolerance(struct device *dev)
728 {
729 	sysfs_unmerge_group(&dev->kobj, &pm_qos_latency_tolerance_attr_group);
730 }
731 
732 void rpm_sysfs_remove(struct device *dev)
733 {
734 	sysfs_unmerge_group(&dev->kobj, &pm_runtime_attr_group);
735 }
736 
737 void dpm_sysfs_remove(struct device *dev)
738 {
739 	if (device_pm_not_required(dev))
740 		return;
741 	sysfs_unmerge_group(&dev->kobj, &pm_qos_latency_tolerance_attr_group);
742 	dev_pm_qos_constraints_destroy(dev);
743 	rpm_sysfs_remove(dev);
744 	sysfs_unmerge_group(&dev->kobj, &pm_wakeup_attr_group);
745 	sysfs_remove_group(&dev->kobj, &pm_attr_group);
746 }
747