1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * drivers/acpi/power.c - ACPI Power Resources management.
5 * Copyright (C) 2001 - 2015 Intel Corp.
6 * Author: Andy Grover <andrew.grover@intel.com>
7 * Author: Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
8 * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
12 * ACPI power-managed devices may be controlled in two ways:
13 * 1. via "Device Specific (D-State) Control"
14 * 2. via "Power Resource Control".
15 * The code below deals with ACPI Power Resources control.
17 * An ACPI "power resource object" represents a software controllable power
18 * plane, clock plane, or other resource depended on by a device.
20 * A device may rely on multiple power resources, and a power resource
21 * may be shared by multiple devices.
24 #define pr_fmt(fmt) "ACPI: PM: " fmt
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/types.h>
30 #include <linux/slab.h>
31 #include <linux/pm_runtime.h>
32 #include <linux/sysfs.h>
33 #include <linux/acpi.h>
37 #define ACPI_POWER_CLASS "power_resource"
38 #define ACPI_POWER_DEVICE_NAME "Power Resource"
39 #define ACPI_POWER_RESOURCE_STATE_OFF 0x00
40 #define ACPI_POWER_RESOURCE_STATE_ON 0x01
41 #define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF
43 struct acpi_power_dependent_device {
45 struct list_head node;
48 struct acpi_power_resource {
49 struct acpi_device device;
50 struct list_head list_node;
54 unsigned int ref_count;
57 struct mutex resource_lock;
58 struct list_head dependents;
61 struct acpi_power_resource_entry {
62 struct list_head node;
63 struct acpi_power_resource *resource;
66 static LIST_HEAD(acpi_power_resource_list);
67 static DEFINE_MUTEX(power_resource_list_lock);
69 /* --------------------------------------------------------------------------
70 Power Resource Management
71 -------------------------------------------------------------------------- */
74 struct acpi_power_resource *to_power_resource(struct acpi_device *device)
76 return container_of(device, struct acpi_power_resource, device);
79 static struct acpi_power_resource *acpi_power_get_context(acpi_handle handle)
81 struct acpi_device *device;
83 if (acpi_bus_get_device(handle, &device))
86 return to_power_resource(device);
89 static int acpi_power_resources_list_add(acpi_handle handle,
90 struct list_head *list)
92 struct acpi_power_resource *resource = acpi_power_get_context(handle);
93 struct acpi_power_resource_entry *entry;
95 if (!resource || !list)
98 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
102 entry->resource = resource;
103 if (!list_empty(list)) {
104 struct acpi_power_resource_entry *e;
106 list_for_each_entry(e, list, node)
107 if (e->resource->order > resource->order) {
108 list_add_tail(&entry->node, &e->node);
112 list_add_tail(&entry->node, list);
116 void acpi_power_resources_list_free(struct list_head *list)
118 struct acpi_power_resource_entry *entry, *e;
120 list_for_each_entry_safe(entry, e, list, node) {
121 list_del(&entry->node);
126 static bool acpi_power_resource_is_dup(union acpi_object *package,
127 unsigned int start, unsigned int i)
129 acpi_handle rhandle, dup;
132 /* The caller is expected to check the package element types */
133 rhandle = package->package.elements[i].reference.handle;
134 for (j = start; j < i; j++) {
135 dup = package->package.elements[j].reference.handle;
143 int acpi_extract_power_resources(union acpi_object *package, unsigned int start,
144 struct list_head *list)
149 for (i = start; i < package->package.count; i++) {
150 union acpi_object *element = &package->package.elements[i];
151 struct acpi_device *rdev;
154 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
158 rhandle = element->reference.handle;
164 /* Some ACPI tables contain duplicate power resource references */
165 if (acpi_power_resource_is_dup(package, start, i))
168 rdev = acpi_add_power_resource(rhandle);
173 err = acpi_power_resources_list_add(rhandle, list);
177 to_power_resource(rdev)->users++;
180 acpi_power_resources_list_free(list);
185 static int acpi_power_get_state(acpi_handle handle, int *state)
187 acpi_status status = AE_OK;
188 unsigned long long sta = 0;
190 if (!handle || !state)
193 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
194 if (ACPI_FAILURE(status))
197 *state = (sta & 0x01)?ACPI_POWER_RESOURCE_STATE_ON:
198 ACPI_POWER_RESOURCE_STATE_OFF;
200 acpi_handle_debug(handle, "Power resource is %s\n",
201 *state ? "on" : "off");
206 static int acpi_power_get_list_state(struct list_head *list, int *state)
208 struct acpi_power_resource_entry *entry;
214 /* The state of the list is 'on' IFF all resources are 'on'. */
216 list_for_each_entry(entry, list, node) {
217 struct acpi_power_resource *resource = entry->resource;
218 acpi_handle handle = resource->device.handle;
221 mutex_lock(&resource->resource_lock);
222 result = acpi_power_get_state(handle, &cur_state);
223 mutex_unlock(&resource->resource_lock);
227 if (cur_state != ACPI_POWER_RESOURCE_STATE_ON)
231 pr_debug("Power resource list is %s\n", cur_state ? "on" : "off");
238 acpi_power_resource_add_dependent(struct acpi_power_resource *resource,
241 struct acpi_power_dependent_device *dep;
244 mutex_lock(&resource->resource_lock);
245 list_for_each_entry(dep, &resource->dependents, node) {
246 /* Only add it once */
251 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
258 list_add_tail(&dep->node, &resource->dependents);
259 dev_dbg(dev, "added power dependency to [%s]\n", resource->name);
262 mutex_unlock(&resource->resource_lock);
267 acpi_power_resource_remove_dependent(struct acpi_power_resource *resource,
270 struct acpi_power_dependent_device *dep;
272 mutex_lock(&resource->resource_lock);
273 list_for_each_entry(dep, &resource->dependents, node) {
274 if (dep->dev == dev) {
275 list_del(&dep->node);
277 dev_dbg(dev, "removed power dependency to [%s]\n",
282 mutex_unlock(&resource->resource_lock);
286 * acpi_device_power_add_dependent - Add dependent device of this ACPI device
287 * @adev: ACPI device pointer
288 * @dev: Dependent device
290 * If @adev has non-empty _PR0 the @dev is added as dependent device to all
291 * power resources returned by it. This means that whenever these power
292 * resources are turned _ON the dependent devices get runtime resumed. This
293 * is needed for devices such as PCI to allow its driver to re-initialize
294 * it after it went to D0uninitialized.
296 * If @adev does not have _PR0 this does nothing.
298 * Returns %0 in case of success and negative errno otherwise.
300 int acpi_device_power_add_dependent(struct acpi_device *adev,
303 struct acpi_power_resource_entry *entry;
304 struct list_head *resources;
307 if (!adev->flags.power_manageable)
310 resources = &adev->power.states[ACPI_STATE_D0].resources;
311 list_for_each_entry(entry, resources, node) {
312 ret = acpi_power_resource_add_dependent(entry->resource, dev);
320 list_for_each_entry(entry, resources, node)
321 acpi_power_resource_remove_dependent(entry->resource, dev);
327 * acpi_device_power_remove_dependent - Remove dependent device
328 * @adev: ACPI device pointer
329 * @dev: Dependent device
331 * Does the opposite of acpi_device_power_add_dependent() and removes the
332 * dependent device if it is found. Can be called to @adev that does not
335 void acpi_device_power_remove_dependent(struct acpi_device *adev,
338 struct acpi_power_resource_entry *entry;
339 struct list_head *resources;
341 if (!adev->flags.power_manageable)
344 resources = &adev->power.states[ACPI_STATE_D0].resources;
345 list_for_each_entry_reverse(entry, resources, node)
346 acpi_power_resource_remove_dependent(entry->resource, dev);
349 static int __acpi_power_on(struct acpi_power_resource *resource)
351 struct acpi_power_dependent_device *dep;
352 acpi_status status = AE_OK;
354 status = acpi_evaluate_object(resource->device.handle, "_ON", NULL, NULL);
355 if (ACPI_FAILURE(status))
358 pr_debug("Power resource [%s] turned on\n", resource->name);
361 * If there are other dependents on this power resource we need to
362 * resume them now so that their drivers can re-initialize the
363 * hardware properly after it went back to D0.
365 if (list_empty(&resource->dependents) ||
366 list_is_singular(&resource->dependents))
369 list_for_each_entry(dep, &resource->dependents, node) {
370 dev_dbg(dep->dev, "runtime resuming because [%s] turned on\n",
372 pm_request_resume(dep->dev);
378 static int acpi_power_on_unlocked(struct acpi_power_resource *resource)
382 if (resource->ref_count++) {
383 pr_debug("Power resource [%s] already on\n", resource->name);
385 result = __acpi_power_on(resource);
387 resource->ref_count--;
392 static int acpi_power_on(struct acpi_power_resource *resource)
396 mutex_lock(&resource->resource_lock);
397 result = acpi_power_on_unlocked(resource);
398 mutex_unlock(&resource->resource_lock);
402 static int __acpi_power_off(struct acpi_power_resource *resource)
406 status = acpi_evaluate_object(resource->device.handle, "_OFF",
408 if (ACPI_FAILURE(status))
411 pr_debug("Power resource [%s] turned off\n", resource->name);
416 static int acpi_power_off_unlocked(struct acpi_power_resource *resource)
420 if (!resource->ref_count) {
421 pr_debug("Power resource [%s] already off\n", resource->name);
425 if (--resource->ref_count) {
426 pr_debug("Power resource [%s] still in use\n", resource->name);
428 result = __acpi_power_off(resource);
430 resource->ref_count++;
435 static int acpi_power_off(struct acpi_power_resource *resource)
439 mutex_lock(&resource->resource_lock);
440 result = acpi_power_off_unlocked(resource);
441 mutex_unlock(&resource->resource_lock);
445 static int acpi_power_off_list(struct list_head *list)
447 struct acpi_power_resource_entry *entry;
450 list_for_each_entry_reverse(entry, list, node) {
451 result = acpi_power_off(entry->resource);
458 list_for_each_entry_continue(entry, list, node)
459 acpi_power_on(entry->resource);
464 static int acpi_power_on_list(struct list_head *list)
466 struct acpi_power_resource_entry *entry;
469 list_for_each_entry(entry, list, node) {
470 result = acpi_power_on(entry->resource);
477 list_for_each_entry_continue_reverse(entry, list, node)
478 acpi_power_off(entry->resource);
483 static struct attribute *attrs[] = {
487 static const struct attribute_group attr_groups[] = {
489 .name = "power_resources_D0",
493 .name = "power_resources_D1",
497 .name = "power_resources_D2",
500 [ACPI_STATE_D3_HOT] = {
501 .name = "power_resources_D3hot",
506 static const struct attribute_group wakeup_attr_group = {
507 .name = "power_resources_wakeup",
511 static void acpi_power_hide_list(struct acpi_device *adev,
512 struct list_head *resources,
513 const struct attribute_group *attr_group)
515 struct acpi_power_resource_entry *entry;
517 if (list_empty(resources))
520 list_for_each_entry_reverse(entry, resources, node) {
521 struct acpi_device *res_dev = &entry->resource->device;
523 sysfs_remove_link_from_group(&adev->dev.kobj,
525 dev_name(&res_dev->dev));
527 sysfs_remove_group(&adev->dev.kobj, attr_group);
530 static void acpi_power_expose_list(struct acpi_device *adev,
531 struct list_head *resources,
532 const struct attribute_group *attr_group)
534 struct acpi_power_resource_entry *entry;
537 if (list_empty(resources))
540 ret = sysfs_create_group(&adev->dev.kobj, attr_group);
544 list_for_each_entry(entry, resources, node) {
545 struct acpi_device *res_dev = &entry->resource->device;
547 ret = sysfs_add_link_to_group(&adev->dev.kobj,
550 dev_name(&res_dev->dev));
552 acpi_power_hide_list(adev, resources, attr_group);
558 static void acpi_power_expose_hide(struct acpi_device *adev,
559 struct list_head *resources,
560 const struct attribute_group *attr_group,
564 acpi_power_expose_list(adev, resources, attr_group);
566 acpi_power_hide_list(adev, resources, attr_group);
569 void acpi_power_add_remove_device(struct acpi_device *adev, bool add)
573 if (adev->wakeup.flags.valid)
574 acpi_power_expose_hide(adev, &adev->wakeup.resources,
575 &wakeup_attr_group, add);
577 if (!adev->power.flags.power_resources)
580 for (state = ACPI_STATE_D0; state <= ACPI_STATE_D3_HOT; state++)
581 acpi_power_expose_hide(adev,
582 &adev->power.states[state].resources,
583 &attr_groups[state], add);
586 int acpi_power_wakeup_list_init(struct list_head *list, int *system_level_p)
588 struct acpi_power_resource_entry *entry;
589 int system_level = 5;
591 list_for_each_entry(entry, list, node) {
592 struct acpi_power_resource *resource = entry->resource;
593 acpi_handle handle = resource->device.handle;
597 mutex_lock(&resource->resource_lock);
599 result = acpi_power_get_state(handle, &state);
601 mutex_unlock(&resource->resource_lock);
604 if (state == ACPI_POWER_RESOURCE_STATE_ON) {
605 resource->ref_count++;
606 resource->wakeup_enabled = true;
608 if (system_level > resource->system_level)
609 system_level = resource->system_level;
611 mutex_unlock(&resource->resource_lock);
613 *system_level_p = system_level;
617 /* --------------------------------------------------------------------------
618 Device Power Management
619 -------------------------------------------------------------------------- */
622 * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in
623 * ACPI 3.0) _PSW (Power State Wake)
624 * @dev: Device to handle.
625 * @enable: 0 - disable, 1 - enable the wake capabilities of the device.
626 * @sleep_state: Target sleep state of the system.
627 * @dev_state: Target power state of the device.
629 * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
630 * State Wake) for the device, if present. On failure reset the device's
631 * wakeup.flags.valid flag.
634 * 0 if either _DSW or _PSW has been successfully executed
635 * 0 if neither _DSW nor _PSW has been found
636 * -ENODEV if the execution of either _DSW or _PSW has failed
638 int acpi_device_sleep_wake(struct acpi_device *dev,
639 int enable, int sleep_state, int dev_state)
641 union acpi_object in_arg[3];
642 struct acpi_object_list arg_list = { 3, in_arg };
643 acpi_status status = AE_OK;
646 * Try to execute _DSW first.
648 * Three arguments are needed for the _DSW object:
649 * Argument 0: enable/disable the wake capabilities
650 * Argument 1: target system state
651 * Argument 2: target device state
652 * When _DSW object is called to disable the wake capabilities, maybe
653 * the first argument is filled. The values of the other two arguments
656 in_arg[0].type = ACPI_TYPE_INTEGER;
657 in_arg[0].integer.value = enable;
658 in_arg[1].type = ACPI_TYPE_INTEGER;
659 in_arg[1].integer.value = sleep_state;
660 in_arg[2].type = ACPI_TYPE_INTEGER;
661 in_arg[2].integer.value = dev_state;
662 status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL);
663 if (ACPI_SUCCESS(status)) {
665 } else if (status != AE_NOT_FOUND) {
666 acpi_handle_info(dev->handle, "_DSW execution failed\n");
667 dev->wakeup.flags.valid = 0;
672 status = acpi_execute_simple_method(dev->handle, "_PSW", enable);
673 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
674 acpi_handle_info(dev->handle, "_PSW execution failed\n");
675 dev->wakeup.flags.valid = 0;
683 * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
684 * 1. Power on the power resources required for the wakeup device
685 * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
686 * State Wake) for the device, if present
688 int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
690 struct acpi_power_resource_entry *entry;
693 if (!dev || !dev->wakeup.flags.valid)
696 mutex_lock(&acpi_device_lock);
698 if (dev->wakeup.prepare_count++)
701 list_for_each_entry(entry, &dev->wakeup.resources, node) {
702 struct acpi_power_resource *resource = entry->resource;
704 mutex_lock(&resource->resource_lock);
706 if (!resource->wakeup_enabled) {
707 err = acpi_power_on_unlocked(resource);
709 resource->wakeup_enabled = true;
712 mutex_unlock(&resource->resource_lock);
716 "Cannot turn wakeup power resources on\n");
717 dev->wakeup.flags.valid = 0;
722 * Passing 3 as the third argument below means the device may be
723 * put into arbitrary power state afterward.
725 err = acpi_device_sleep_wake(dev, 1, sleep_state, 3);
727 dev->wakeup.prepare_count = 0;
730 mutex_unlock(&acpi_device_lock);
735 * Shutdown a wakeup device, counterpart of above method
736 * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
737 * State Wake) for the device, if present
738 * 2. Shutdown down the power resources
740 int acpi_disable_wakeup_device_power(struct acpi_device *dev)
742 struct acpi_power_resource_entry *entry;
745 if (!dev || !dev->wakeup.flags.valid)
748 mutex_lock(&acpi_device_lock);
750 if (--dev->wakeup.prepare_count > 0)
754 * Executing the code below even if prepare_count is already zero when
755 * the function is called may be useful, for example for initialisation.
757 if (dev->wakeup.prepare_count < 0)
758 dev->wakeup.prepare_count = 0;
760 err = acpi_device_sleep_wake(dev, 0, 0, 0);
764 list_for_each_entry(entry, &dev->wakeup.resources, node) {
765 struct acpi_power_resource *resource = entry->resource;
767 mutex_lock(&resource->resource_lock);
769 if (resource->wakeup_enabled) {
770 err = acpi_power_off_unlocked(resource);
772 resource->wakeup_enabled = false;
775 mutex_unlock(&resource->resource_lock);
779 "Cannot turn wakeup power resources off\n");
780 dev->wakeup.flags.valid = 0;
786 mutex_unlock(&acpi_device_lock);
790 int acpi_power_get_inferred_state(struct acpi_device *device, int *state)
796 if (!device || !state)
800 * We know a device's inferred power state when all the resources
801 * required for a given D-state are 'on'.
803 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
804 struct list_head *list = &device->power.states[i].resources;
806 if (list_empty(list))
809 result = acpi_power_get_list_state(list, &list_state);
813 if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
819 *state = device->power.states[ACPI_STATE_D3_COLD].flags.valid ?
820 ACPI_STATE_D3_COLD : ACPI_STATE_D3_HOT;
824 int acpi_power_on_resources(struct acpi_device *device, int state)
826 if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3_HOT)
829 return acpi_power_on_list(&device->power.states[state].resources);
832 int acpi_power_transition(struct acpi_device *device, int state)
836 if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
839 if (device->power.state == state || !device->flags.power_manageable)
842 if ((device->power.state < ACPI_STATE_D0)
843 || (device->power.state > ACPI_STATE_D3_COLD))
847 * First we reference all power resources required in the target list
848 * (e.g. so the device doesn't lose power while transitioning). Then,
849 * we dereference all power resources used in the current list.
851 if (state < ACPI_STATE_D3_COLD)
852 result = acpi_power_on_list(
853 &device->power.states[state].resources);
855 if (!result && device->power.state < ACPI_STATE_D3_COLD)
857 &device->power.states[device->power.state].resources);
859 /* We shouldn't change the state unless the above operations succeed. */
860 device->power.state = result ? ACPI_STATE_UNKNOWN : state;
865 static void acpi_release_power_resource(struct device *dev)
867 struct acpi_device *device = to_acpi_device(dev);
868 struct acpi_power_resource *resource;
870 resource = container_of(device, struct acpi_power_resource, device);
872 mutex_lock(&power_resource_list_lock);
873 list_del(&resource->list_node);
874 mutex_unlock(&power_resource_list_lock);
876 acpi_free_pnp_ids(&device->pnp);
880 static ssize_t resource_in_use_show(struct device *dev,
881 struct device_attribute *attr,
884 struct acpi_power_resource *resource;
886 resource = to_power_resource(to_acpi_device(dev));
887 return sprintf(buf, "%u\n", !!resource->ref_count);
889 static DEVICE_ATTR_RO(resource_in_use);
891 static void acpi_power_sysfs_remove(struct acpi_device *device)
893 device_remove_file(&device->dev, &dev_attr_resource_in_use);
896 static void acpi_power_add_resource_to_list(struct acpi_power_resource *resource)
898 mutex_lock(&power_resource_list_lock);
900 if (!list_empty(&acpi_power_resource_list)) {
901 struct acpi_power_resource *r;
903 list_for_each_entry(r, &acpi_power_resource_list, list_node)
904 if (r->order > resource->order) {
905 list_add_tail(&resource->list_node, &r->list_node);
909 list_add_tail(&resource->list_node, &acpi_power_resource_list);
912 mutex_unlock(&power_resource_list_lock);
915 struct acpi_device *acpi_add_power_resource(acpi_handle handle)
917 struct acpi_power_resource *resource;
918 struct acpi_device *device = NULL;
919 union acpi_object acpi_object;
920 struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
922 int state, result = -ENODEV;
924 acpi_bus_get_device(handle, &device);
928 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
932 device = &resource->device;
933 acpi_init_device_object(device, handle, ACPI_BUS_TYPE_POWER);
934 mutex_init(&resource->resource_lock);
935 INIT_LIST_HEAD(&resource->list_node);
936 INIT_LIST_HEAD(&resource->dependents);
937 resource->name = device->pnp.bus_id;
938 strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
939 strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
940 device->power.state = ACPI_STATE_UNKNOWN;
942 /* Evaluate the object to get the system level and resource order. */
943 status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
944 if (ACPI_FAILURE(status))
947 resource->system_level = acpi_object.power_resource.system_level;
948 resource->order = acpi_object.power_resource.resource_order;
950 result = acpi_power_get_state(handle, &state);
954 pr_info("%s [%s] (%s)\n", acpi_device_name(device),
955 acpi_device_bid(device), state ? "on" : "off");
957 device->flags.match_driver = true;
958 result = acpi_device_add(device, acpi_release_power_resource);
962 if (!device_create_file(&device->dev, &dev_attr_resource_in_use))
963 device->remove = acpi_power_sysfs_remove;
965 acpi_power_add_resource_to_list(resource);
966 acpi_device_add_finalize(device);
970 acpi_release_power_resource(&device->dev);
974 #ifdef CONFIG_ACPI_SLEEP
975 void acpi_resume_power_resources(void)
977 struct acpi_power_resource *resource;
979 mutex_lock(&power_resource_list_lock);
981 list_for_each_entry(resource, &acpi_power_resource_list, list_node) {
984 mutex_lock(&resource->resource_lock);
986 result = acpi_power_get_state(resource->device.handle, &state);
988 mutex_unlock(&resource->resource_lock);
992 if (state == ACPI_POWER_RESOURCE_STATE_OFF
993 && resource->ref_count) {
994 dev_info(&resource->device.dev, "Turning ON\n");
995 __acpi_power_on(resource);
998 mutex_unlock(&resource->resource_lock);
1001 mutex_unlock(&power_resource_list_lock);
1005 static void acpi_power_turn_off_if_unused(struct acpi_power_resource *resource,
1008 if (resource->ref_count > 0)
1012 if (resource->users > 0)
1017 result = acpi_power_get_state(resource->device.handle, &state);
1018 if (result || state == ACPI_POWER_RESOURCE_STATE_OFF)
1022 dev_info(&resource->device.dev, "Turning OFF\n");
1023 __acpi_power_off(resource);
1027 * acpi_turn_off_unused_power_resources - Turn off power resources not in use.
1028 * @init: Control switch.
1030 * If @ainit is set, unconditionally turn off all of the ACPI power resources
1031 * without any users.
1033 * Otherwise, turn off all ACPI power resources without active references (that
1034 * is, the ones that should be "off" at the moment) that are "on".
1036 void acpi_turn_off_unused_power_resources(bool init)
1038 struct acpi_power_resource *resource;
1040 mutex_lock(&power_resource_list_lock);
1042 list_for_each_entry_reverse(resource, &acpi_power_resource_list, list_node) {
1043 mutex_lock(&resource->resource_lock);
1045 acpi_power_turn_off_if_unused(resource, init);
1047 mutex_unlock(&resource->resource_lock);
1050 mutex_unlock(&power_resource_list_lock);