Rafael J. Wysocki | ec2cd81 | 2012-11-02 01:40:09 +0100 | [diff] [blame] | 1 | /* |
| 2 | * drivers/acpi/device_pm.c - ACPI device power management routines. |
| 3 | * |
| 4 | * Copyright (C) 2012, Intel Corp. |
| 5 | * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com> |
| 6 | * |
| 7 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as published |
| 11 | * by the Free Software Foundation. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, but |
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 21 | * |
| 22 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 23 | */ |
| 24 | |
| 25 | #include <linux/device.h> |
Rafael J. Wysocki | 86b3832 | 2012-11-02 01:40:18 +0100 | [diff] [blame^] | 26 | #include <linux/export.h> |
Rafael J. Wysocki | ec2cd81 | 2012-11-02 01:40:09 +0100 | [diff] [blame] | 27 | #include <linux/mutex.h> |
Rafael J. Wysocki | 86b3832 | 2012-11-02 01:40:18 +0100 | [diff] [blame^] | 28 | #include <linux/pm_qos.h> |
Rafael J. Wysocki | ec2cd81 | 2012-11-02 01:40:09 +0100 | [diff] [blame] | 29 | |
| 30 | #include <acpi/acpi.h> |
| 31 | #include <acpi/acpi_bus.h> |
| 32 | |
| 33 | static DEFINE_MUTEX(acpi_pm_notifier_lock); |
| 34 | |
| 35 | /** |
| 36 | * acpi_add_pm_notifier - Register PM notifier for given ACPI device. |
| 37 | * @adev: ACPI device to add the notifier for. |
| 38 | * @context: Context information to pass to the notifier routine. |
| 39 | * |
| 40 | * NOTE: @adev need not be a run-wake or wakeup device to be a valid source of |
| 41 | * PM wakeup events. For example, wakeup events may be generated for bridges |
| 42 | * if one of the devices below the bridge is signaling wakeup, even if the |
| 43 | * bridge itself doesn't have a wakeup GPE associated with it. |
| 44 | */ |
| 45 | acpi_status acpi_add_pm_notifier(struct acpi_device *adev, |
| 46 | acpi_notify_handler handler, void *context) |
| 47 | { |
| 48 | acpi_status status = AE_ALREADY_EXISTS; |
| 49 | |
| 50 | mutex_lock(&acpi_pm_notifier_lock); |
| 51 | |
| 52 | if (adev->wakeup.flags.notifier_present) |
| 53 | goto out; |
| 54 | |
| 55 | status = acpi_install_notify_handler(adev->handle, |
| 56 | ACPI_SYSTEM_NOTIFY, |
| 57 | handler, context); |
| 58 | if (ACPI_FAILURE(status)) |
| 59 | goto out; |
| 60 | |
| 61 | adev->wakeup.flags.notifier_present = true; |
| 62 | |
| 63 | out: |
| 64 | mutex_unlock(&acpi_pm_notifier_lock); |
| 65 | return status; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * acpi_remove_pm_notifier - Unregister PM notifier from given ACPI device. |
| 70 | * @adev: ACPI device to remove the notifier from. |
| 71 | */ |
| 72 | acpi_status acpi_remove_pm_notifier(struct acpi_device *adev, |
| 73 | acpi_notify_handler handler) |
| 74 | { |
| 75 | acpi_status status = AE_BAD_PARAMETER; |
| 76 | |
| 77 | mutex_lock(&acpi_pm_notifier_lock); |
| 78 | |
| 79 | if (!adev->wakeup.flags.notifier_present) |
| 80 | goto out; |
| 81 | |
| 82 | status = acpi_remove_notify_handler(adev->handle, |
| 83 | ACPI_SYSTEM_NOTIFY, |
| 84 | handler); |
| 85 | if (ACPI_FAILURE(status)) |
| 86 | goto out; |
| 87 | |
| 88 | adev->wakeup.flags.notifier_present = false; |
| 89 | |
| 90 | out: |
| 91 | mutex_unlock(&acpi_pm_notifier_lock); |
| 92 | return status; |
| 93 | } |
Rafael J. Wysocki | 86b3832 | 2012-11-02 01:40:18 +0100 | [diff] [blame^] | 94 | |
| 95 | /** |
| 96 | * acpi_device_power_state - Get preferred power state of ACPI device. |
| 97 | * @dev: Device whose preferred target power state to return. |
| 98 | * @adev: ACPI device node corresponding to @dev. |
| 99 | * @target_state: System state to match the resultant device state. |
| 100 | * @d_max_in: Deepest low-power state to take into consideration. |
| 101 | * @d_min_p: Location to store the upper limit of the allowed states range. |
| 102 | * Return value: Preferred power state of the device on success, -ENODEV |
| 103 | * (if there's no 'struct acpi_device' for @dev) or -EINVAL on failure |
| 104 | * |
| 105 | * Find the lowest power (highest number) ACPI device power state that the |
| 106 | * device can be in while the system is in the state represented by |
| 107 | * @target_state. If @d_min_p is set, the highest power (lowest number) device |
| 108 | * power state that @dev can be in for the given system sleep state is stored |
| 109 | * at the location pointed to by it. |
| 110 | * |
| 111 | * Callers must ensure that @dev and @adev are valid pointers and that @adev |
| 112 | * actually corresponds to @dev before using this function. |
| 113 | */ |
| 114 | int acpi_device_power_state(struct device *dev, struct acpi_device *adev, |
| 115 | u32 target_state, int d_max_in, int *d_min_p) |
| 116 | { |
| 117 | char acpi_method[] = "_SxD"; |
| 118 | unsigned long long d_min, d_max; |
| 119 | bool wakeup = false; |
| 120 | |
| 121 | if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3) |
| 122 | return -EINVAL; |
| 123 | |
| 124 | if (d_max_in > ACPI_STATE_D3_HOT) { |
| 125 | enum pm_qos_flags_status stat; |
| 126 | |
| 127 | stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF); |
| 128 | if (stat == PM_QOS_FLAGS_ALL) |
| 129 | d_max_in = ACPI_STATE_D3_HOT; |
| 130 | } |
| 131 | |
| 132 | acpi_method[2] = '0' + target_state; |
| 133 | /* |
| 134 | * If the sleep state is S0, the lowest limit from ACPI is D3, |
| 135 | * but if the device has _S0W, we will use the value from _S0W |
| 136 | * as the lowest limit from ACPI. Finally, we will constrain |
| 137 | * the lowest limit with the specified one. |
| 138 | */ |
| 139 | d_min = ACPI_STATE_D0; |
| 140 | d_max = ACPI_STATE_D3; |
| 141 | |
| 142 | /* |
| 143 | * If present, _SxD methods return the minimum D-state (highest power |
| 144 | * state) we can use for the corresponding S-states. Otherwise, the |
| 145 | * minimum D-state is D0 (ACPI 3.x). |
| 146 | * |
| 147 | * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer |
| 148 | * provided -- that's our fault recovery, we ignore retval. |
| 149 | */ |
| 150 | if (target_state > ACPI_STATE_S0) { |
| 151 | acpi_evaluate_integer(adev->handle, acpi_method, NULL, &d_min); |
| 152 | wakeup = device_may_wakeup(dev) && adev->wakeup.flags.valid |
| 153 | && adev->wakeup.sleep_state >= target_state; |
| 154 | } else if (dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) != |
| 155 | PM_QOS_FLAGS_NONE) { |
| 156 | wakeup = adev->wakeup.flags.valid; |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * If _PRW says we can wake up the system from the target sleep state, |
| 161 | * the D-state returned by _SxD is sufficient for that (we assume a |
| 162 | * wakeup-aware driver if wake is set). Still, if _SxW exists |
| 163 | * (ACPI 3.x), it should return the maximum (lowest power) D-state that |
| 164 | * can wake the system. _S0W may be valid, too. |
| 165 | */ |
| 166 | if (wakeup) { |
| 167 | acpi_status status; |
| 168 | |
| 169 | acpi_method[3] = 'W'; |
| 170 | status = acpi_evaluate_integer(adev->handle, acpi_method, NULL, |
| 171 | &d_max); |
| 172 | if (ACPI_FAILURE(status)) { |
| 173 | if (target_state != ACPI_STATE_S0 || |
| 174 | status != AE_NOT_FOUND) |
| 175 | d_max = d_min; |
| 176 | } else if (d_max < d_min) { |
| 177 | /* Warn the user of the broken DSDT */ |
| 178 | printk(KERN_WARNING "ACPI: Wrong value from %s\n", |
| 179 | acpi_method); |
| 180 | /* Sanitize it */ |
| 181 | d_min = d_max; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | if (d_max_in < d_min) |
| 186 | return -EINVAL; |
| 187 | if (d_min_p) |
| 188 | *d_min_p = d_min; |
| 189 | /* constrain d_max with specified lowest limit (max number) */ |
| 190 | if (d_max > d_max_in) { |
| 191 | for (d_max = d_max_in; d_max > d_min; d_max--) { |
| 192 | if (adev->power.states[d_max].flags.valid) |
| 193 | break; |
| 194 | } |
| 195 | } |
| 196 | return d_max; |
| 197 | } |
| 198 | EXPORT_SYMBOL_GPL(acpi_device_power_state); |