Lines Matching +full:device +full:- +full:handle

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.
12 * ACPI power-managed devices may be controlled in two ways:
13 * 1. via "Device Specific (D-State) Control"
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
45 struct device *dev;
50 struct acpi_device device; member
68 /* --------------------------------------------------------------------------
70 -------------------------------------------------------------------------- */
74 return dev_name(&pr->device.dev); in resource_dev_name()
78 struct acpi_power_resource *to_power_resource(struct acpi_device *device) in to_power_resource() argument
80 return container_of(device, struct acpi_power_resource, device); in to_power_resource()
83 static struct acpi_power_resource *acpi_power_get_context(acpi_handle handle) in acpi_power_get_context() argument
85 struct acpi_device *device = acpi_fetch_acpi_dev(handle); in acpi_power_get_context() local
87 if (!device) in acpi_power_get_context()
90 return to_power_resource(device); in acpi_power_get_context()
93 static int acpi_power_resources_list_add(acpi_handle handle, in acpi_power_resources_list_add() argument
96 struct acpi_power_resource *resource = acpi_power_get_context(handle); in acpi_power_resources_list_add()
100 return -EINVAL; in acpi_power_resources_list_add()
104 return -ENOMEM; in acpi_power_resources_list_add()
106 entry->resource = resource; in acpi_power_resources_list_add()
111 if (e->resource->order > resource->order) { in acpi_power_resources_list_add()
112 list_add_tail(&entry->node, &e->node); in acpi_power_resources_list_add()
116 list_add_tail(&entry->node, list); in acpi_power_resources_list_add()
125 list_del(&entry->node); in acpi_power_resources_list_free()
137 rhandle = package->package.elements[i].reference.handle; in acpi_power_resource_is_dup()
139 dup = package->package.elements[j].reference.handle; in acpi_power_resource_is_dup()
153 for (i = start; i < package->package.count; i++) { in acpi_extract_power_resources()
154 union acpi_object *element = &package->package.elements[i]; in acpi_extract_power_resources()
158 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) { in acpi_extract_power_resources()
159 err = -ENODATA; in acpi_extract_power_resources()
162 rhandle = element->reference.handle; in acpi_extract_power_resources()
164 err = -ENODEV; in acpi_extract_power_resources()
174 err = -ENODEV; in acpi_extract_power_resources()
187 static int __get_state(acpi_handle handle, u8 *state) in __get_state() argument
193 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta); in __get_state()
195 return -ENODEV; in __get_state()
199 acpi_handle_debug(handle, "Power resource is %s\n", in __get_state()
208 if (resource->state == ACPI_POWER_RESOURCE_STATE_UNKNOWN) { in acpi_power_get_state()
211 ret = __get_state(resource->device.handle, &resource->state); in acpi_power_get_state()
216 *state = resource->state; in acpi_power_get_state()
226 return -EINVAL; in acpi_power_get_list_state()
230 struct acpi_power_resource *resource = entry->resource; in acpi_power_get_list_state()
233 mutex_lock(&resource->resource_lock); in acpi_power_get_list_state()
235 mutex_unlock(&resource->resource_lock); in acpi_power_get_list_state()
251 struct device *dev) in acpi_power_resource_add_dependent()
256 mutex_lock(&resource->resource_lock); in acpi_power_resource_add_dependent()
257 list_for_each_entry(dep, &resource->dependents, node) { in acpi_power_resource_add_dependent()
259 if (dep->dev == dev) in acpi_power_resource_add_dependent()
265 ret = -ENOMEM; in acpi_power_resource_add_dependent()
269 dep->dev = dev; in acpi_power_resource_add_dependent()
270 list_add_tail(&dep->node, &resource->dependents); in acpi_power_resource_add_dependent()
275 mutex_unlock(&resource->resource_lock); in acpi_power_resource_add_dependent()
281 struct device *dev) in acpi_power_resource_remove_dependent()
285 mutex_lock(&resource->resource_lock); in acpi_power_resource_remove_dependent()
286 list_for_each_entry(dep, &resource->dependents, node) { in acpi_power_resource_remove_dependent()
287 if (dep->dev == dev) { in acpi_power_resource_remove_dependent()
288 list_del(&dep->node); in acpi_power_resource_remove_dependent()
295 mutex_unlock(&resource->resource_lock); in acpi_power_resource_remove_dependent()
299 * acpi_device_power_add_dependent - Add dependent device of this ACPI device
300 * @adev: ACPI device pointer
301 * @dev: Dependent device
303 * If @adev has non-empty _PR0 the @dev is added as dependent device to all
306 * is needed for devices such as PCI to allow its driver to re-initialize
314 struct device *dev) in acpi_device_power_add_dependent()
320 if (!adev->flags.power_manageable) in acpi_device_power_add_dependent()
323 resources = &adev->power.states[ACPI_STATE_D0].resources; in acpi_device_power_add_dependent()
325 ret = acpi_power_resource_add_dependent(entry->resource, dev); in acpi_device_power_add_dependent()
334 acpi_power_resource_remove_dependent(entry->resource, dev); in acpi_device_power_add_dependent()
340 * acpi_device_power_remove_dependent - Remove dependent device
341 * @adev: ACPI device pointer
342 * @dev: Dependent device
345 * dependent device if it is found. Can be called to @adev that does not
349 struct device *dev) in acpi_device_power_remove_dependent()
354 if (!adev->flags.power_manageable) in acpi_device_power_remove_dependent()
357 resources = &adev->power.states[ACPI_STATE_D0].resources; in acpi_device_power_remove_dependent()
359 acpi_power_resource_remove_dependent(entry->resource, dev); in acpi_device_power_remove_dependent()
364 acpi_handle handle = resource->device.handle; in __acpi_power_on() local
368 status = acpi_evaluate_object(handle, "_ON", NULL, NULL); in __acpi_power_on()
370 resource->state = ACPI_POWER_RESOURCE_STATE_UNKNOWN; in __acpi_power_on()
371 return -ENODEV; in __acpi_power_on()
374 resource->state = ACPI_POWER_RESOURCE_STATE_ON; in __acpi_power_on()
376 acpi_handle_debug(handle, "Power resource turned on\n"); in __acpi_power_on()
380 * resume them now so that their drivers can re-initialize the in __acpi_power_on()
383 if (list_empty(&resource->dependents) || in __acpi_power_on()
384 list_is_singular(&resource->dependents)) in __acpi_power_on()
387 list_for_each_entry(dep, &resource->dependents, node) { in __acpi_power_on()
388 dev_dbg(dep->dev, "runtime resuming because [%s] turned on\n", in __acpi_power_on()
390 pm_request_resume(dep->dev); in __acpi_power_on()
400 if (resource->ref_count++) { in acpi_power_on_unlocked()
401 acpi_handle_debug(resource->device.handle, in acpi_power_on_unlocked()
406 resource->ref_count--; in acpi_power_on_unlocked()
415 mutex_lock(&resource->resource_lock); in acpi_power_on()
417 mutex_unlock(&resource->resource_lock); in acpi_power_on()
423 acpi_handle handle = resource->device.handle; in __acpi_power_off() local
426 status = acpi_evaluate_object(handle, "_OFF", NULL, NULL); in __acpi_power_off()
428 resource->state = ACPI_POWER_RESOURCE_STATE_UNKNOWN; in __acpi_power_off()
429 return -ENODEV; in __acpi_power_off()
432 resource->state = ACPI_POWER_RESOURCE_STATE_OFF; in __acpi_power_off()
434 acpi_handle_debug(handle, "Power resource turned off\n"); in __acpi_power_off()
443 if (!resource->ref_count) { in acpi_power_off_unlocked()
444 acpi_handle_debug(resource->device.handle, in acpi_power_off_unlocked()
449 if (--resource->ref_count) { in acpi_power_off_unlocked()
450 acpi_handle_debug(resource->device.handle, in acpi_power_off_unlocked()
455 resource->ref_count++; in acpi_power_off_unlocked()
464 mutex_lock(&resource->resource_lock); in acpi_power_off()
466 mutex_unlock(&resource->resource_lock); in acpi_power_off()
476 result = acpi_power_off(entry->resource); in acpi_power_off_list()
484 acpi_power_on(entry->resource); in acpi_power_off_list()
495 result = acpi_power_on(entry->resource); in acpi_power_on_list()
503 acpi_power_off(entry->resource); in acpi_power_on_list()
546 struct acpi_device *res_dev = &entry->resource->device; in acpi_power_hide_list()
548 sysfs_remove_link_from_group(&adev->dev.kobj, in acpi_power_hide_list()
549 attr_group->name, in acpi_power_hide_list()
550 dev_name(&res_dev->dev)); in acpi_power_hide_list()
552 sysfs_remove_group(&adev->dev.kobj, attr_group); in acpi_power_hide_list()
565 ret = sysfs_create_group(&adev->dev.kobj, attr_group); in acpi_power_expose_list()
570 struct acpi_device *res_dev = &entry->resource->device; in acpi_power_expose_list()
572 ret = sysfs_add_link_to_group(&adev->dev.kobj, in acpi_power_expose_list()
573 attr_group->name, in acpi_power_expose_list()
574 &res_dev->dev.kobj, in acpi_power_expose_list()
575 dev_name(&res_dev->dev)); in acpi_power_expose_list()
598 if (adev->wakeup.flags.valid) in acpi_power_add_remove_device()
599 acpi_power_expose_hide(adev, &adev->wakeup.resources, in acpi_power_add_remove_device()
602 if (!adev->power.flags.power_resources) in acpi_power_add_remove_device()
607 &adev->power.states[state].resources, in acpi_power_add_remove_device()
617 struct acpi_power_resource *resource = entry->resource; in acpi_power_wakeup_list_init()
620 mutex_lock(&resource->resource_lock); in acpi_power_wakeup_list_init()
626 if (!resource->ref_count && in acpi_power_wakeup_list_init()
631 if (system_level > resource->system_level) in acpi_power_wakeup_list_init()
632 system_level = resource->system_level; in acpi_power_wakeup_list_init()
634 mutex_unlock(&resource->resource_lock); in acpi_power_wakeup_list_init()
640 /* --------------------------------------------------------------------------
641 Device Power Management
642 -------------------------------------------------------------------------- */
645 * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in
647 * @dev: Device to handle.
648 * @enable: 0 - disable, 1 - enable the wake capabilities of the device.
650 * @dev_state: Target power state of the device.
652 * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
653 * State Wake) for the device, if present. On failure reset the device's
659 * -ENODEV if the execution of either _DSW or _PSW has failed
674 * Argument 2: target device state in acpi_device_sleep_wake()
685 status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL); in acpi_device_sleep_wake()
689 acpi_handle_info(dev->handle, "_DSW execution failed\n"); in acpi_device_sleep_wake()
690 dev->wakeup.flags.valid = 0; in acpi_device_sleep_wake()
691 return -ENODEV; in acpi_device_sleep_wake()
695 status = acpi_execute_simple_method(dev->handle, "_PSW", enable); in acpi_device_sleep_wake()
697 acpi_handle_info(dev->handle, "_PSW execution failed\n"); in acpi_device_sleep_wake()
698 dev->wakeup.flags.valid = 0; in acpi_device_sleep_wake()
699 return -ENODEV; in acpi_device_sleep_wake()
706 * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
707 * 1. Power on the power resources required for the wakeup device
708 * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
709 * State Wake) for the device, if present
715 if (!dev || !dev->wakeup.flags.valid) in acpi_enable_wakeup_device_power()
716 return -EINVAL; in acpi_enable_wakeup_device_power()
720 dev_dbg(&dev->dev, "Enabling wakeup power (count %d)\n", in acpi_enable_wakeup_device_power()
721 dev->wakeup.prepare_count); in acpi_enable_wakeup_device_power()
723 if (dev->wakeup.prepare_count++) in acpi_enable_wakeup_device_power()
726 err = acpi_power_on_list(&dev->wakeup.resources); in acpi_enable_wakeup_device_power()
728 dev_err(&dev->dev, "Cannot turn on wakeup power resources\n"); in acpi_enable_wakeup_device_power()
729 dev->wakeup.flags.valid = 0; in acpi_enable_wakeup_device_power()
734 * Passing 3 as the third argument below means the device may be in acpi_enable_wakeup_device_power()
739 acpi_power_off_list(&dev->wakeup.resources); in acpi_enable_wakeup_device_power()
740 dev->wakeup.prepare_count = 0; in acpi_enable_wakeup_device_power()
744 dev_dbg(&dev->dev, "Wakeup power enabled\n"); in acpi_enable_wakeup_device_power()
752 * Shutdown a wakeup device, counterpart of above method
753 * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
754 * State Wake) for the device, if present
762 if (!dev || !dev->wakeup.flags.valid) in acpi_disable_wakeup_device_power()
763 return -EINVAL; in acpi_disable_wakeup_device_power()
767 dev_dbg(&dev->dev, "Disabling wakeup power (count %d)\n", in acpi_disable_wakeup_device_power()
768 dev->wakeup.prepare_count); in acpi_disable_wakeup_device_power()
770 /* Do nothing if wakeup power has not been enabled for this device. */ in acpi_disable_wakeup_device_power()
771 if (dev->wakeup.prepare_count <= 0) in acpi_disable_wakeup_device_power()
774 if (--dev->wakeup.prepare_count > 0) in acpi_disable_wakeup_device_power()
785 list_for_each_entry(entry, &dev->wakeup.resources, node) { in acpi_disable_wakeup_device_power()
788 ret = acpi_power_off(entry->resource); in acpi_disable_wakeup_device_power()
793 dev_err(&dev->dev, "Cannot turn off wakeup power resources\n"); in acpi_disable_wakeup_device_power()
794 dev->wakeup.flags.valid = 0; in acpi_disable_wakeup_device_power()
798 dev_dbg(&dev->dev, "Wakeup power disabled\n"); in acpi_disable_wakeup_device_power()
805 int acpi_power_get_inferred_state(struct acpi_device *device, int *state) in acpi_power_get_inferred_state() argument
811 if (!device || !state) in acpi_power_get_inferred_state()
812 return -EINVAL; in acpi_power_get_inferred_state()
815 * We know a device's inferred power state when all the resources in acpi_power_get_inferred_state()
816 * required for a given D-state are 'on'. in acpi_power_get_inferred_state()
819 struct list_head *list = &device->power.states[i].resources; in acpi_power_get_inferred_state()
834 *state = device->power.states[ACPI_STATE_D3_COLD].flags.valid ? in acpi_power_get_inferred_state()
839 int acpi_power_on_resources(struct acpi_device *device, int state) in acpi_power_on_resources() argument
841 if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3_HOT) in acpi_power_on_resources()
842 return -EINVAL; in acpi_power_on_resources()
844 return acpi_power_on_list(&device->power.states[state].resources); in acpi_power_on_resources()
847 int acpi_power_transition(struct acpi_device *device, int state) in acpi_power_transition() argument
851 if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD)) in acpi_power_transition()
852 return -EINVAL; in acpi_power_transition()
854 if (device->power.state == state || !device->flags.power_manageable) in acpi_power_transition()
857 if ((device->power.state < ACPI_STATE_D0) in acpi_power_transition()
858 || (device->power.state > ACPI_STATE_D3_COLD)) in acpi_power_transition()
859 return -ENODEV; in acpi_power_transition()
863 * (e.g. so the device doesn't lose power while transitioning). Then, in acpi_power_transition()
868 &device->power.states[state].resources); in acpi_power_transition()
870 if (!result && device->power.state < ACPI_STATE_D3_COLD) in acpi_power_transition()
872 &device->power.states[device->power.state].resources); in acpi_power_transition()
875 device->power.state = result ? ACPI_STATE_UNKNOWN : state; in acpi_power_transition()
880 static void acpi_release_power_resource(struct device *dev) in acpi_release_power_resource()
882 struct acpi_device *device = to_acpi_device(dev); in acpi_release_power_resource() local
885 resource = container_of(device, struct acpi_power_resource, device); in acpi_release_power_resource()
888 list_del(&resource->list_node); in acpi_release_power_resource()
891 acpi_free_pnp_ids(&device->pnp); in acpi_release_power_resource()
895 static ssize_t resource_in_use_show(struct device *dev, in resource_in_use_show()
902 return sprintf(buf, "%u\n", !!resource->ref_count); in resource_in_use_show()
906 static void acpi_power_sysfs_remove(struct acpi_device *device) in acpi_power_sysfs_remove() argument
908 device_remove_file(&device->dev, &dev_attr_resource_in_use); in acpi_power_sysfs_remove()
919 if (r->order > resource->order) { in acpi_power_add_resource_to_list()
920 list_add_tail(&resource->list_node, &r->list_node); in acpi_power_add_resource_to_list()
924 list_add_tail(&resource->list_node, &acpi_power_resource_list); in acpi_power_add_resource_to_list()
930 struct acpi_device *acpi_add_power_resource(acpi_handle handle) in acpi_add_power_resource() argument
932 struct acpi_device *device = acpi_fetch_acpi_dev(handle); in acpi_add_power_resource() local
940 if (device) in acpi_add_power_resource()
941 return device; in acpi_add_power_resource()
947 device = &resource->device; in acpi_add_power_resource()
948 acpi_init_device_object(device, handle, ACPI_BUS_TYPE_POWER, in acpi_add_power_resource()
950 mutex_init(&resource->resource_lock); in acpi_add_power_resource()
951 INIT_LIST_HEAD(&resource->list_node); in acpi_add_power_resource()
952 INIT_LIST_HEAD(&resource->dependents); in acpi_add_power_resource()
953 strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME); in acpi_add_power_resource()
954 strcpy(acpi_device_class(device), ACPI_POWER_CLASS); in acpi_add_power_resource()
955 device->power.state = ACPI_STATE_UNKNOWN; in acpi_add_power_resource()
956 device->flags.match_driver = true; in acpi_add_power_resource()
959 status = acpi_evaluate_object(handle, NULL, NULL, &buffer); in acpi_add_power_resource()
963 resource->system_level = acpi_object.power_resource.system_level; in acpi_add_power_resource()
964 resource->order = acpi_object.power_resource.resource_order; in acpi_add_power_resource()
965 resource->state = ACPI_POWER_RESOURCE_STATE_UNKNOWN; in acpi_add_power_resource()
971 acpi_handle_info(handle, "New power resource\n"); in acpi_add_power_resource()
973 result = acpi_tie_acpi_dev(device); in acpi_add_power_resource()
977 result = acpi_device_add(device); in acpi_add_power_resource()
981 if (!device_create_file(&device->dev, &dev_attr_resource_in_use)) in acpi_add_power_resource()
982 device->remove = acpi_power_sysfs_remove; in acpi_add_power_resource()
985 acpi_device_add_finalize(device); in acpi_add_power_resource()
986 return device; in acpi_add_power_resource()
989 acpi_release_power_resource(&device->dev); in acpi_add_power_resource()
1004 mutex_lock(&resource->resource_lock); in acpi_resume_power_resources()
1006 resource->state = ACPI_POWER_RESOURCE_STATE_UNKNOWN; in acpi_resume_power_resources()
1009 mutex_unlock(&resource->resource_lock); in acpi_resume_power_resources()
1014 && resource->ref_count) { in acpi_resume_power_resources()
1015 acpi_handle_debug(resource->device.handle, "Turning ON\n"); in acpi_resume_power_resources()
1019 mutex_unlock(&resource->resource_lock); in acpi_resume_power_resources()
1029 * The Toshiba Click Mini has a CPR3 power-resource which must
1031 * _PR? lists. The other 2 affected power-resources are no-ops.
1035 DMI_MATCH(DMI_PRODUCT_NAME, "SATELLITE Click Mini L9W-B"),
1042 * acpi_turn_off_unused_power_resources - Turn off power resources not in use.
1054 mutex_lock(&resource->resource_lock); in acpi_turn_off_unused_power_resources()
1056 if (!resource->ref_count && in acpi_turn_off_unused_power_resources()
1057 resource->state == ACPI_POWER_RESOURCE_STATE_ON) { in acpi_turn_off_unused_power_resources()
1058 acpi_handle_debug(resource->device.handle, "Turning OFF\n"); in acpi_turn_off_unused_power_resources()
1062 mutex_unlock(&resource->resource_lock); in acpi_turn_off_unused_power_resources()