^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * drivers/acpi/device_pm.c - ACPI device power management routines.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2012, Intel Corp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/pm_qos.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/pm_domain.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/pm_runtime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/suspend.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "fan.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define _COMPONENT ACPI_POWER_COMPONENT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) ACPI_MODULE_NAME("device_pm");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * acpi_power_state_string - String representation of ACPI device power state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * @state: ACPI device power state to return the string representation of.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) const char *acpi_power_state_string(int state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) switch (state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) case ACPI_STATE_D0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) return "D0";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) case ACPI_STATE_D1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return "D1";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) case ACPI_STATE_D2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return "D2";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) case ACPI_STATE_D3_HOT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) return "D3hot";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) case ACPI_STATE_D3_COLD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return "D3cold";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return "(unknown)";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static int acpi_dev_pm_explicit_get(struct acpi_device *device, int *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) unsigned long long psc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) acpi_status status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) status = acpi_evaluate_integer(device->handle, "_PSC", NULL, &psc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (ACPI_FAILURE(status))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) *state = psc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * acpi_device_get_power - Get power state of an ACPI device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * @device: Device to get the power state of.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * @state: Place to store the power state of the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * This function does not update the device's power.state field, but it may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * update its parent's power.state field (when the parent's power state is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * unknown and the device's power state turns out to be D0).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * Also, it does not update power resource reference counters to ensure that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * the power state returned by it will be persistent and it may return a power
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * state shallower than previously set by acpi_device_set_power() for @device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * (if that power state depends on any power resources).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) int acpi_device_get_power(struct acpi_device *device, int *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) int result = ACPI_STATE_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (!device || !state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (!device->flags.power_manageable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) /* TBD: Non-recursive algorithm for walking up hierarchy. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) *state = device->parent ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) device->parent->power.state : ACPI_STATE_D0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) * Get the device's power state from power resources settings and _PSC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * if available.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (device->power.flags.power_resources) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) error = acpi_power_get_inferred_state(device, &result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (device->power.flags.explicit_get) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) int psc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) error = acpi_dev_pm_explicit_get(device, &psc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * The power resources settings may indicate a power state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * shallower than the actual power state of the device, because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * the same power resources may be referenced by other devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * For systems predating ACPI 4.0 we assume that D3hot is the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * deepest state that can be supported.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (psc > result && psc < ACPI_STATE_D3_COLD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) result = psc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) else if (result == ACPI_STATE_UNKNOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) result = psc > ACPI_STATE_D2 ? ACPI_STATE_D3_HOT : psc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * If we were unsure about the device parent's power state up to this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * point, the fact that the device is in D0 implies that the parent has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * to be in D0 too, except if ignore_parent is set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (!device->power.flags.ignore_parent && device->parent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) && device->parent->power.state == ACPI_STATE_UNKNOWN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) && result == ACPI_STATE_D0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) device->parent->power.state = ACPI_STATE_D0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) *state = result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] power state is %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) device->pnp.bus_id, acpi_power_state_string(*state)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static int acpi_dev_pm_explicit_set(struct acpi_device *adev, int state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (adev->power.states[state].flags.explicit_set) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) char method[5] = { '_', 'P', 'S', '0' + state, '\0' };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) acpi_status status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) status = acpi_evaluate_object(adev->handle, method, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (ACPI_FAILURE(status))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * acpi_device_set_power - Set power state of an ACPI device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * @device: Device to set the power state of.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * @state: New power state to set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * Callers must ensure that the device is power manageable before using this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) * function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) int acpi_device_set_power(struct acpi_device *device, int state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) int target_state = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) int result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (!device || !device->flags.power_manageable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) acpi_handle_debug(device->handle, "Power state change: %s -> %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) acpi_power_state_string(device->power.state),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) acpi_power_state_string(state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) /* Make sure this is a valid target state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) /* There is a special case for D0 addressed below. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (state > ACPI_STATE_D0 && state == device->power.state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] already in %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) device->pnp.bus_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) acpi_power_state_string(state)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (state == ACPI_STATE_D3_COLD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) * For transitions to D3cold we need to execute _PS3 and then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) * possibly drop references to the power resources in use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) state = ACPI_STATE_D3_HOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) /* If D3cold is not supported, use D3hot as the target state. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if (!device->power.states[ACPI_STATE_D3_COLD].flags.valid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) target_state = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) } else if (!device->power.states[state].flags.valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) dev_warn(&device->dev, "Power state %s not supported\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) acpi_power_state_string(state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) if (!device->power.flags.ignore_parent &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) device->parent && (state < device->parent->power.state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) dev_warn(&device->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) "Cannot transition to power state %s for parent in %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) acpi_power_state_string(state),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) acpi_power_state_string(device->parent->power.state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * Transition Power
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * ----------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * In accordance with ACPI 6, _PSx is executed before manipulating power
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * resources, unless the target state is D0, in which case _PS0 is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) * supposed to be executed after turning the power resources on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if (state > ACPI_STATE_D0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) * According to ACPI 6, devices cannot go from lower-power
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) * (deeper) states to higher-power (shallower) states.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) if (state < device->power.state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) dev_warn(&device->dev, "Cannot transition from %s to %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) acpi_power_state_string(device->power.state),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) acpi_power_state_string(state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) * If the device goes from D3hot to D3cold, _PS3 has been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) * evaluated for it already, so skip it in that case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (device->power.state < ACPI_STATE_D3_HOT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) result = acpi_dev_pm_explicit_set(device, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) if (device->power.flags.power_resources)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) result = acpi_power_transition(device, target_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) int cur_state = device->power.state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (device->power.flags.power_resources) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) result = acpi_power_transition(device, ACPI_STATE_D0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) if (cur_state == ACPI_STATE_D0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) int psc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) /* Nothing to do here if _PSC is not present. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) if (!device->power.flags.explicit_get)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) * The power state of the device was set to D0 last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) * time, but that might have happened before a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) * system-wide transition involving the platform
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) * firmware, so it may be necessary to evaluate _PS0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) * for the device here. However, use extra care here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) * and evaluate _PSC to check the device's current power
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) * state, and only invoke _PS0 if the evaluation of _PSC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) * is successful and it returns a power state different
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) * from D0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) result = acpi_dev_pm_explicit_get(device, &psc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) if (result || psc == ACPI_STATE_D0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) result = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) if (result) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) dev_warn(&device->dev, "Failed to change power state to %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) acpi_power_state_string(target_state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) device->power.state = target_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) ACPI_DEBUG_PRINT((ACPI_DB_INFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) "Device [%s] transitioned to %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) device->pnp.bus_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) acpi_power_state_string(target_state)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) EXPORT_SYMBOL(acpi_device_set_power);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) int acpi_bus_set_power(acpi_handle handle, int state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) struct acpi_device *device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) result = acpi_bus_get_device(handle, &device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) return acpi_device_set_power(device, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) EXPORT_SYMBOL(acpi_bus_set_power);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) int acpi_bus_init_power(struct acpi_device *device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) int state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) if (!device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) device->power.state = ACPI_STATE_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) if (!acpi_device_is_present(device)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) device->flags.initialized = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) result = acpi_device_get_power(device, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) if (state < ACPI_STATE_D3_COLD && device->power.flags.power_resources) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) /* Reference count the power resources. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) result = acpi_power_on_resources(device, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) if (state == ACPI_STATE_D0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) * If _PSC is not present and the state inferred from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) * power resources appears to be D0, it still may be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) * necessary to execute _PS0 at this point, because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) * another device using the same power resources may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) * have been put into D0 previously and that's why we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) * see D0 here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) result = acpi_dev_pm_explicit_set(device, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) } else if (state == ACPI_STATE_UNKNOWN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) * No power resources and missing _PSC? Cross fingers and make
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) * it D0 in hope that this is what the BIOS put the device into.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) * [We tried to force D0 here by executing _PS0, but that broke
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) * Toshiba P870-303 in a nasty way.]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) state = ACPI_STATE_D0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) device->power.state = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) * acpi_device_fix_up_power - Force device with missing _PSC into D0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) * @device: Device object whose power state is to be fixed up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) * Devices without power resources and _PSC, but having _PS0 and _PS3 defined,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) * are assumed to be put into D0 by the BIOS. However, in some cases that may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) * not be the case and this function should be used then.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) int acpi_device_fix_up_power(struct acpi_device *device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) if (!device->power.flags.power_resources
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) && !device->power.flags.explicit_get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) && device->power.state == ACPI_STATE_D0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) ret = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) EXPORT_SYMBOL_GPL(acpi_device_fix_up_power);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) int acpi_device_update_power(struct acpi_device *device, int *state_p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) int state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) if (device->power.state == ACPI_STATE_UNKNOWN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) result = acpi_bus_init_power(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) if (!result && state_p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) *state_p = device->power.state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) result = acpi_device_get_power(device, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) if (state == ACPI_STATE_UNKNOWN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) state = ACPI_STATE_D0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) result = acpi_device_set_power(device, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) if (device->power.flags.power_resources) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) * We don't need to really switch the state, bu we need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) * to update the power resources' reference counters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) result = acpi_power_transition(device, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) device->power.state = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) if (state_p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) *state_p = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) EXPORT_SYMBOL_GPL(acpi_device_update_power);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) int acpi_bus_update_power(acpi_handle handle, int *state_p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) struct acpi_device *device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) result = acpi_bus_get_device(handle, &device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) return result ? result : acpi_device_update_power(device, state_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) EXPORT_SYMBOL_GPL(acpi_bus_update_power);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) bool acpi_bus_power_manageable(acpi_handle handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) struct acpi_device *device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) result = acpi_bus_get_device(handle, &device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) return result ? false : device->flags.power_manageable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) EXPORT_SYMBOL(acpi_bus_power_manageable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) #ifdef CONFIG_PM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) static DEFINE_MUTEX(acpi_pm_notifier_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) static DEFINE_MUTEX(acpi_pm_notifier_install_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) void acpi_pm_wakeup_event(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) pm_wakeup_dev_event(dev, 0, acpi_s2idle_wakeup());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) EXPORT_SYMBOL_GPL(acpi_pm_wakeup_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) static void acpi_pm_notify_handler(acpi_handle handle, u32 val, void *not_used)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) struct acpi_device *adev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) if (val != ACPI_NOTIFY_DEVICE_WAKE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) acpi_handle_debug(handle, "Wake notify\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) adev = acpi_bus_get_acpi_device(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) if (!adev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) mutex_lock(&acpi_pm_notifier_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) if (adev->wakeup.flags.notifier_present) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) pm_wakeup_ws_event(adev->wakeup.ws, 0, acpi_s2idle_wakeup());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) if (adev->wakeup.context.func) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) acpi_handle_debug(handle, "Running %pS for %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) adev->wakeup.context.func,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) dev_name(adev->wakeup.context.dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) adev->wakeup.context.func(&adev->wakeup.context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) mutex_unlock(&acpi_pm_notifier_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) acpi_bus_put_acpi_device(adev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) * acpi_add_pm_notifier - Register PM notify handler for given ACPI device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) * @adev: ACPI device to add the notify handler for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) * @dev: Device to generate a wakeup event for while handling the notification.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) * @func: Work function to execute when handling the notification.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) * NOTE: @adev need not be a run-wake or wakeup device to be a valid source of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) * PM wakeup events. For example, wakeup events may be generated for bridges
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) * if one of the devices below the bridge is signaling wakeup, even if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) * bridge itself doesn't have a wakeup GPE associated with it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) acpi_status acpi_add_pm_notifier(struct acpi_device *adev, struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) void (*func)(struct acpi_device_wakeup_context *context))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) acpi_status status = AE_ALREADY_EXISTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) if (!dev && !func)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) return AE_BAD_PARAMETER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) mutex_lock(&acpi_pm_notifier_install_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) if (adev->wakeup.flags.notifier_present)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) status = acpi_install_notify_handler(adev->handle, ACPI_SYSTEM_NOTIFY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) acpi_pm_notify_handler, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) if (ACPI_FAILURE(status))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) mutex_lock(&acpi_pm_notifier_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) adev->wakeup.ws = wakeup_source_register(&adev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) dev_name(&adev->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) adev->wakeup.context.dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) adev->wakeup.context.func = func;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) adev->wakeup.flags.notifier_present = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) mutex_unlock(&acpi_pm_notifier_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) mutex_unlock(&acpi_pm_notifier_install_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) * acpi_remove_pm_notifier - Unregister PM notifier from given ACPI device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) * @adev: ACPI device to remove the notifier from.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) acpi_status acpi_remove_pm_notifier(struct acpi_device *adev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) acpi_status status = AE_BAD_PARAMETER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) mutex_lock(&acpi_pm_notifier_install_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) if (!adev->wakeup.flags.notifier_present)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) status = acpi_remove_notify_handler(adev->handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) ACPI_SYSTEM_NOTIFY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) acpi_pm_notify_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) if (ACPI_FAILURE(status))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) mutex_lock(&acpi_pm_notifier_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) adev->wakeup.context.func = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) adev->wakeup.context.dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) wakeup_source_unregister(adev->wakeup.ws);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) adev->wakeup.flags.notifier_present = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) mutex_unlock(&acpi_pm_notifier_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) mutex_unlock(&acpi_pm_notifier_install_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) bool acpi_bus_can_wakeup(acpi_handle handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) struct acpi_device *device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) result = acpi_bus_get_device(handle, &device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) return result ? false : device->wakeup.flags.valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) EXPORT_SYMBOL(acpi_bus_can_wakeup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) bool acpi_pm_device_can_wakeup(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) struct acpi_device *adev = ACPI_COMPANION(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) return adev ? acpi_device_can_wakeup(adev) : false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) * acpi_dev_pm_get_state - Get preferred power state of ACPI device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) * @dev: Device whose preferred target power state to return.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) * @adev: ACPI device node corresponding to @dev.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) * @target_state: System state to match the resultant device state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) * @d_min_p: Location to store the highest power state available to the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) * @d_max_p: Location to store the lowest power state available to the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) * Find the lowest power (highest number) and highest power (lowest number) ACPI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) * device power states that the device can be in while the system is in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) * state represented by @target_state. Store the integer numbers representing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) * those stats in the memory locations pointed to by @d_max_p and @d_min_p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) * respectively.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) * Callers must ensure that @dev and @adev are valid pointers and that @adev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) * actually corresponds to @dev before using this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) * Returns 0 on success or -ENODATA when one of the ACPI methods fails or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) * returns a value that doesn't make sense. The memory locations pointed to by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) * @d_max_p and @d_min_p are only modified on success.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) static int acpi_dev_pm_get_state(struct device *dev, struct acpi_device *adev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) u32 target_state, int *d_min_p, int *d_max_p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) char method[] = { '_', 'S', '0' + target_state, 'D', '\0' };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) acpi_handle handle = adev->handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) unsigned long long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) int d_min, d_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) bool wakeup = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) bool has_sxd = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) acpi_status status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) * If the system state is S0, the lowest power state the device can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) * in is D3cold, unless the device has _S0W and is supposed to signal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) * wakeup, in which case the return value of _S0W has to be used as the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) * lowest power state available to the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) d_min = ACPI_STATE_D0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) d_max = ACPI_STATE_D3_COLD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) * If present, _SxD methods return the minimum D-state (highest power
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) * state) we can use for the corresponding S-states. Otherwise, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) * minimum D-state is D0 (ACPI 3.x).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) if (target_state > ACPI_STATE_S0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) * We rely on acpi_evaluate_integer() not clobbering the integer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) * provided if AE_NOT_FOUND is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) ret = d_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) status = acpi_evaluate_integer(handle, method, NULL, &ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) if ((ACPI_FAILURE(status) && status != AE_NOT_FOUND)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) || ret > ACPI_STATE_D3_COLD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) * We need to handle legacy systems where D3hot and D3cold are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) * the same and 3 is returned in both cases, so fall back to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) * D3cold if D3hot is not a valid state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) if (!adev->power.states[ret].flags.valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) if (ret == ACPI_STATE_D3_HOT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) ret = ACPI_STATE_D3_COLD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) if (status == AE_OK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) has_sxd = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) d_min = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) wakeup = device_may_wakeup(dev) && adev->wakeup.flags.valid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) && adev->wakeup.sleep_state >= target_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) wakeup = adev->wakeup.flags.valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) * If _PRW says we can wake up the system from the target sleep state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) * the D-state returned by _SxD is sufficient for that (we assume a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) * wakeup-aware driver if wake is set). Still, if _SxW exists
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) * (ACPI 3.x), it should return the maximum (lowest power) D-state that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) * can wake the system. _S0W may be valid, too.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) if (wakeup) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) method[3] = 'W';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) status = acpi_evaluate_integer(handle, method, NULL, &ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) if (status == AE_NOT_FOUND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) /* No _SxW. In this case, the ACPI spec says that we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) * must not go into any power state deeper than the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) * value returned from _SxD.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) if (has_sxd && target_state > ACPI_STATE_S0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) d_max = d_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) } else if (ACPI_SUCCESS(status) && ret <= ACPI_STATE_D3_COLD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) /* Fall back to D3cold if ret is not a valid state. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) if (!adev->power.states[ret].flags.valid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) ret = ACPI_STATE_D3_COLD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) d_max = ret > d_min ? ret : d_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) if (d_min_p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) *d_min_p = d_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) if (d_max_p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) *d_max_p = d_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) * acpi_pm_device_sleep_state - Get preferred power state of ACPI device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) * @dev: Device whose preferred target power state to return.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) * @d_min_p: Location to store the upper limit of the allowed states range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) * @d_max_in: Deepest low-power state to take into consideration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) * Return value: Preferred power state of the device on success, -ENODEV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) * if there's no 'struct acpi_device' for @dev, -EINVAL if @d_max_in is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) * incorrect, or -ENODATA on ACPI method failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) * The caller must ensure that @dev is valid before using this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) struct acpi_device *adev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) int ret, d_min, d_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3_COLD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) if (d_max_in > ACPI_STATE_D2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) enum pm_qos_flags_status stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) if (stat == PM_QOS_FLAGS_ALL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) d_max_in = ACPI_STATE_D2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) adev = ACPI_COMPANION(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) if (!adev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) ret = acpi_dev_pm_get_state(dev, adev, acpi_target_system_state(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) &d_min, &d_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) if (d_max_in < d_min)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) if (d_max > d_max_in) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) for (d_max = d_max_in; d_max > d_min; d_max--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) if (adev->power.states[d_max].flags.valid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) if (d_min_p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) *d_min_p = d_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) return d_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) EXPORT_SYMBOL(acpi_pm_device_sleep_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) * acpi_pm_notify_work_func - ACPI devices wakeup notification work function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) * @context: Device wakeup context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) static void acpi_pm_notify_work_func(struct acpi_device_wakeup_context *context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) struct device *dev = context->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) if (dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) pm_wakeup_event(dev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) pm_request_resume(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) static DEFINE_MUTEX(acpi_wakeup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) static int __acpi_device_wakeup_enable(struct acpi_device *adev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) u32 target_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) struct acpi_device_wakeup *wakeup = &adev->wakeup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) acpi_status status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) mutex_lock(&acpi_wakeup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) if (wakeup->enable_count >= INT_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) acpi_handle_info(adev->handle, "Wakeup enable count out of bounds!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) if (wakeup->enable_count > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) goto inc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) error = acpi_enable_wakeup_device_power(adev, target_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) status = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) if (ACPI_FAILURE(status)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) acpi_disable_wakeup_device_power(adev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) error = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) acpi_handle_debug(adev->handle, "GPE%2X enabled for wakeup\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) (unsigned int)wakeup->gpe_number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) inc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) wakeup->enable_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) mutex_unlock(&acpi_wakeup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) * acpi_device_wakeup_enable - Enable wakeup functionality for device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) * @adev: ACPI device to enable wakeup functionality for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) * @target_state: State the system is transitioning into.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) * Enable the GPE associated with @adev so that it can generate wakeup signals
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) * for the device in response to external (remote) events and enable wakeup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) * power for it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) * Callers must ensure that @adev is a valid ACPI device node before executing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) * this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) static int acpi_device_wakeup_enable(struct acpi_device *adev, u32 target_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) return __acpi_device_wakeup_enable(adev, target_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) * acpi_device_wakeup_disable - Disable wakeup functionality for device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) * @adev: ACPI device to disable wakeup functionality for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) * Disable the GPE associated with @adev and disable wakeup power for it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) * Callers must ensure that @adev is a valid ACPI device node before executing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) * this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) static void acpi_device_wakeup_disable(struct acpi_device *adev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) struct acpi_device_wakeup *wakeup = &adev->wakeup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) mutex_lock(&acpi_wakeup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) if (!wakeup->enable_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) acpi_disable_wakeup_device_power(adev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) wakeup->enable_count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) mutex_unlock(&acpi_wakeup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) * acpi_pm_set_device_wakeup - Enable/disable remote wakeup for given device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) * @dev: Device to enable/disable to generate wakeup events.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) * @enable: Whether to enable or disable the wakeup functionality.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) int acpi_pm_set_device_wakeup(struct device *dev, bool enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) struct acpi_device *adev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) adev = ACPI_COMPANION(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) if (!adev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) if (!acpi_device_can_wakeup(adev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) if (!enable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) acpi_device_wakeup_disable(adev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) dev_dbg(dev, "Wakeup disabled by ACPI\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) error = __acpi_device_wakeup_enable(adev, acpi_target_system_state());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) if (!error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) dev_dbg(dev, "Wakeup enabled by ACPI\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) EXPORT_SYMBOL_GPL(acpi_pm_set_device_wakeup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) * acpi_dev_pm_low_power - Put ACPI device into a low-power state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) * @dev: Device to put into a low-power state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) * @adev: ACPI device node corresponding to @dev.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) * @system_state: System state to choose the device state for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) u32 system_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) int ret, state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) if (!acpi_device_power_manageable(adev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) ret = acpi_dev_pm_get_state(dev, adev, system_state, NULL, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) return ret ? ret : acpi_device_set_power(adev, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) * acpi_dev_pm_full_power - Put ACPI device into the full-power state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) * @adev: ACPI device node to put into the full-power state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) static int acpi_dev_pm_full_power(struct acpi_device *adev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) return acpi_device_power_manageable(adev) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) acpi_device_set_power(adev, ACPI_STATE_D0) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) * acpi_dev_suspend - Put device into a low-power state using ACPI.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) * @dev: Device to put into a low-power state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) * @wakeup: Whether or not to enable wakeup for the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) * Put the given device into a low-power state using the standard ACPI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) * mechanism. Set up remote wakeup if desired, choose the state to put the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) * device into (this checks if remote wakeup is expected to work too), and set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) * the power state of the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) int acpi_dev_suspend(struct device *dev, bool wakeup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) struct acpi_device *adev = ACPI_COMPANION(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) u32 target_state = acpi_target_system_state();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) if (!adev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) if (wakeup && acpi_device_can_wakeup(adev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) error = acpi_device_wakeup_enable(adev, target_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) wakeup = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) error = acpi_dev_pm_low_power(dev, adev, target_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) if (error && wakeup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) acpi_device_wakeup_disable(adev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) EXPORT_SYMBOL_GPL(acpi_dev_suspend);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) * acpi_dev_resume - Put device into the full-power state using ACPI.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) * @dev: Device to put into the full-power state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) * Put the given device into the full-power state using the standard ACPI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) * mechanism. Set the power state of the device to ACPI D0 and disable wakeup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) int acpi_dev_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) struct acpi_device *adev = ACPI_COMPANION(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) if (!adev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) error = acpi_dev_pm_full_power(adev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) acpi_device_wakeup_disable(adev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) EXPORT_SYMBOL_GPL(acpi_dev_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) * acpi_subsys_runtime_suspend - Suspend device using ACPI.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) * @dev: Device to suspend.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) * Carry out the generic runtime suspend procedure for @dev and use ACPI to put
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) * it into a runtime low-power state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) int acpi_subsys_runtime_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) int ret = pm_generic_runtime_suspend(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) return ret ? ret : acpi_dev_suspend(dev, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) * acpi_subsys_runtime_resume - Resume device using ACPI.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) * @dev: Device to Resume.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) * Use ACPI to put the given device into the full-power state and carry out the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) * generic runtime resume procedure for it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) int acpi_subsys_runtime_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) int ret = acpi_dev_resume(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) return ret ? ret : pm_generic_runtime_resume(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) #ifdef CONFIG_PM_SLEEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) static bool acpi_dev_needs_resume(struct device *dev, struct acpi_device *adev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) u32 sys_target = acpi_target_system_state();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) int ret, state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) if (!pm_runtime_suspended(dev) || !adev || (adev->wakeup.flags.valid &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) device_may_wakeup(dev) != !!adev->wakeup.prepare_count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) if (sys_target == ACPI_STATE_S0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) if (adev->power.flags.dsw_present)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) ret = acpi_dev_pm_get_state(dev, adev, sys_target, NULL, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) return state != adev->power.state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) * acpi_subsys_prepare - Prepare device for system transition to a sleep state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) * @dev: Device to prepare.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) int acpi_subsys_prepare(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) struct acpi_device *adev = ACPI_COMPANION(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) if (dev->driver && dev->driver->pm && dev->driver->pm->prepare) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) int ret = dev->driver->pm->prepare(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) if (!ret && dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_PREPARE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) return !acpi_dev_needs_resume(dev, adev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) EXPORT_SYMBOL_GPL(acpi_subsys_prepare);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) * acpi_subsys_complete - Finalize device's resume during system resume.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) * @dev: Device to handle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) void acpi_subsys_complete(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) pm_generic_complete(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) * If the device had been runtime-suspended before the system went into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) * the sleep state it is going out of and it has never been resumed till
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) * now, resume it in case the firmware powered it up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) if (pm_runtime_suspended(dev) && pm_resume_via_firmware())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) pm_request_resume(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) EXPORT_SYMBOL_GPL(acpi_subsys_complete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) * acpi_subsys_suspend - Run the device driver's suspend callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) * @dev: Device to handle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) * Follow PCI and resume devices from runtime suspend before running their
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) * system suspend callbacks, unless the driver can cope with runtime-suspended
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) * devices during system suspend and there are no ACPI-specific reasons for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) * resuming them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) int acpi_subsys_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) if (!dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) acpi_dev_needs_resume(dev, ACPI_COMPANION(dev)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) pm_runtime_resume(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) return pm_generic_suspend(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) EXPORT_SYMBOL_GPL(acpi_subsys_suspend);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) * acpi_subsys_suspend_late - Suspend device using ACPI.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) * @dev: Device to suspend.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) * Carry out the generic late suspend procedure for @dev and use ACPI to put
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) * it into a low-power state during system transition into a sleep state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) int acpi_subsys_suspend_late(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) if (dev_pm_skip_suspend(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) ret = pm_generic_suspend_late(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) return ret ? ret : acpi_dev_suspend(dev, device_may_wakeup(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) * acpi_subsys_suspend_noirq - Run the device driver's "noirq" suspend callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) * @dev: Device to suspend.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) int acpi_subsys_suspend_noirq(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) if (dev_pm_skip_suspend(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) ret = pm_generic_suspend_noirq(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) * If the target system sleep state is suspend-to-idle, it is sufficient
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) * to check whether or not the device's wakeup settings are good for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) * runtime PM. Otherwise, the pm_resume_via_firmware() check will cause
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) * acpi_subsys_complete() to take care of fixing up the device's state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) * anyway, if need be.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) if (device_can_wakeup(dev) && !device_may_wakeup(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) dev->power.may_skip_resume = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) EXPORT_SYMBOL_GPL(acpi_subsys_suspend_noirq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) * acpi_subsys_resume_noirq - Run the device driver's "noirq" resume callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) * @dev: Device to handle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) static int acpi_subsys_resume_noirq(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) if (dev_pm_skip_resume(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) return pm_generic_resume_noirq(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) * acpi_subsys_resume_early - Resume device using ACPI.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) * @dev: Device to Resume.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) * Use ACPI to put the given device into the full-power state and carry out the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) * generic early resume procedure for it during system transition into the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) * working state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) static int acpi_subsys_resume_early(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) if (dev_pm_skip_resume(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) ret = acpi_dev_resume(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) return ret ? ret : pm_generic_resume_early(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) * acpi_subsys_freeze - Run the device driver's freeze callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) * @dev: Device to handle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) int acpi_subsys_freeze(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) * Resume all runtime-suspended devices before creating a snapshot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) * image of system memory, because the restore kernel generally cannot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) * be expected to always handle them consistently and they need to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) * put into the runtime-active metastate during system resume anyway,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) * so it is better to ensure that the state saved in the image will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) * always consistent with that.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) pm_runtime_resume(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) return pm_generic_freeze(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) EXPORT_SYMBOL_GPL(acpi_subsys_freeze);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) * acpi_subsys_restore_early - Restore device using ACPI.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) * @dev: Device to restore.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) int acpi_subsys_restore_early(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) int ret = acpi_dev_resume(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) return ret ? ret : pm_generic_restore_early(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) EXPORT_SYMBOL_GPL(acpi_subsys_restore_early);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) * acpi_subsys_poweroff - Run the device driver's poweroff callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) * @dev: Device to handle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) * Follow PCI and resume devices from runtime suspend before running their
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) * system poweroff callbacks, unless the driver can cope with runtime-suspended
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) * devices during system suspend and there are no ACPI-specific reasons for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) * resuming them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) int acpi_subsys_poweroff(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) if (!dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) acpi_dev_needs_resume(dev, ACPI_COMPANION(dev)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) pm_runtime_resume(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) return pm_generic_poweroff(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) EXPORT_SYMBOL_GPL(acpi_subsys_poweroff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) * acpi_subsys_poweroff_late - Run the device driver's poweroff callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) * @dev: Device to handle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) * Carry out the generic late poweroff procedure for @dev and use ACPI to put
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) * it into a low-power state during system transition into a sleep state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) static int acpi_subsys_poweroff_late(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) if (dev_pm_skip_suspend(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) ret = pm_generic_poweroff_late(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) return acpi_dev_suspend(dev, device_may_wakeup(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) * acpi_subsys_poweroff_noirq - Run the driver's "noirq" poweroff callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) * @dev: Device to suspend.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) static int acpi_subsys_poweroff_noirq(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) if (dev_pm_skip_suspend(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) return pm_generic_poweroff_noirq(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) #endif /* CONFIG_PM_SLEEP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) static struct dev_pm_domain acpi_general_pm_domain = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) .ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) .runtime_suspend = acpi_subsys_runtime_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) .runtime_resume = acpi_subsys_runtime_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) #ifdef CONFIG_PM_SLEEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) .prepare = acpi_subsys_prepare,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) .complete = acpi_subsys_complete,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) .suspend = acpi_subsys_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) .suspend_late = acpi_subsys_suspend_late,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) .suspend_noirq = acpi_subsys_suspend_noirq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) .resume_noirq = acpi_subsys_resume_noirq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) .resume_early = acpi_subsys_resume_early,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) .freeze = acpi_subsys_freeze,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) .poweroff = acpi_subsys_poweroff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) .poweroff_late = acpi_subsys_poweroff_late,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) .poweroff_noirq = acpi_subsys_poweroff_noirq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) .restore_early = acpi_subsys_restore_early,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) * acpi_dev_pm_detach - Remove ACPI power management from the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) * @dev: Device to take care of.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) * @power_off: Whether or not to try to remove power from the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) * Remove the device from the general ACPI PM domain and remove its wakeup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) * notifier. If @power_off is set, additionally remove power from the device if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) * possible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) * Callers must ensure proper synchronization of this function with power
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) * management callbacks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) static void acpi_dev_pm_detach(struct device *dev, bool power_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) struct acpi_device *adev = ACPI_COMPANION(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) if (adev && dev->pm_domain == &acpi_general_pm_domain) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) dev_pm_domain_set(dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) acpi_remove_pm_notifier(adev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) if (power_off) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) * If the device's PM QoS resume latency limit or flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) * have been exposed to user space, they have to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) * hidden at this point, so that they don't affect the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) * choice of the low-power state to put the device into.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) dev_pm_qos_hide_latency_limit(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) dev_pm_qos_hide_flags(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) acpi_device_wakeup_disable(adev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) * acpi_dev_pm_attach - Prepare device for ACPI power management.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) * @dev: Device to prepare.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) * @power_on: Whether or not to power on the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) * If @dev has a valid ACPI handle that has a valid struct acpi_device object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) * attached to it, install a wakeup notification handler for the device and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) * add it to the general ACPI PM domain. If @power_on is set, the device will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) * be put into the ACPI D0 state before the function returns.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) * This assumes that the @dev's bus type uses generic power management callbacks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) * (or doesn't use any power management callbacks at all).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) * Callers must ensure proper synchronization of this function with power
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) * management callbacks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) int acpi_dev_pm_attach(struct device *dev, bool power_on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) * Skip devices whose ACPI companions match the device IDs below,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) * because they require special power management handling incompatible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) * with the generic ACPI PM domain.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) static const struct acpi_device_id special_pm_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) ACPI_FAN_DEVICE_IDS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) struct acpi_device *adev = ACPI_COMPANION(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) if (!adev || !acpi_match_device_ids(adev, special_pm_ids))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) * Only attach the power domain to the first device if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) * companion is shared by multiple. This is to prevent doing power
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) * management twice.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) if (!acpi_device_is_first_physical_node(adev, dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) acpi_add_pm_notifier(adev, dev, acpi_pm_notify_work_func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) dev_pm_domain_set(dev, &acpi_general_pm_domain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) if (power_on) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) acpi_dev_pm_full_power(adev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) acpi_device_wakeup_disable(adev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) dev->pm_domain->detach = acpi_dev_pm_detach;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) EXPORT_SYMBOL_GPL(acpi_dev_pm_attach);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) #endif /* CONFIG_PM */