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 | * |
Rafael J. Wysocki | ec2cd81 | 2012-11-02 01:40:09 +0100 | [diff] [blame] | 18 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 19 | */ |
| 20 | |
Rafael J. Wysocki | 7b19981 | 2013-11-11 22:41:56 +0100 | [diff] [blame] | 21 | #include <linux/acpi.h> |
Rafael J. Wysocki | 86b3832 | 2012-11-02 01:40:18 +0100 | [diff] [blame] | 22 | #include <linux/export.h> |
Rafael J. Wysocki | ec2cd81 | 2012-11-02 01:40:09 +0100 | [diff] [blame] | 23 | #include <linux/mutex.h> |
Rafael J. Wysocki | 86b3832 | 2012-11-02 01:40:18 +0100 | [diff] [blame] | 24 | #include <linux/pm_qos.h> |
Rafael J. Wysocki | cd7bd02 | 2012-11-02 01:40:28 +0100 | [diff] [blame] | 25 | #include <linux/pm_runtime.h> |
Rafael J. Wysocki | ec2cd81 | 2012-11-02 01:40:09 +0100 | [diff] [blame] | 26 | |
Rafael J. Wysocki | 9ce4e60 | 2013-01-17 14:11:08 +0100 | [diff] [blame] | 27 | #include "internal.h" |
| 28 | |
| 29 | #define _COMPONENT ACPI_POWER_COMPONENT |
| 30 | ACPI_MODULE_NAME("device_pm"); |
Rafael J. Wysocki | ec2cd81 | 2012-11-02 01:40:09 +0100 | [diff] [blame] | 31 | |
Rafael J. Wysocki | 86b3832 | 2012-11-02 01:40:18 +0100 | [diff] [blame] | 32 | /** |
Rafael J. Wysocki | 9ce4e60 | 2013-01-17 14:11:08 +0100 | [diff] [blame] | 33 | * acpi_power_state_string - String representation of ACPI device power state. |
| 34 | * @state: ACPI device power state to return the string representation of. |
| 35 | */ |
| 36 | const char *acpi_power_state_string(int state) |
| 37 | { |
| 38 | switch (state) { |
| 39 | case ACPI_STATE_D0: |
| 40 | return "D0"; |
| 41 | case ACPI_STATE_D1: |
| 42 | return "D1"; |
| 43 | case ACPI_STATE_D2: |
| 44 | return "D2"; |
| 45 | case ACPI_STATE_D3_HOT: |
| 46 | return "D3hot"; |
| 47 | case ACPI_STATE_D3_COLD: |
Rafael J. Wysocki | 898fee4 | 2013-01-22 12:56:26 +0100 | [diff] [blame] | 48 | return "D3cold"; |
Rafael J. Wysocki | 9ce4e60 | 2013-01-17 14:11:08 +0100 | [diff] [blame] | 49 | default: |
| 50 | return "(unknown)"; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * acpi_device_get_power - Get power state of an ACPI device. |
| 56 | * @device: Device to get the power state of. |
| 57 | * @state: Place to store the power state of the device. |
| 58 | * |
| 59 | * This function does not update the device's power.state field, but it may |
| 60 | * update its parent's power.state field (when the parent's power state is |
| 61 | * unknown and the device's power state turns out to be D0). |
| 62 | */ |
| 63 | int acpi_device_get_power(struct acpi_device *device, int *state) |
| 64 | { |
| 65 | int result = ACPI_STATE_UNKNOWN; |
| 66 | |
| 67 | if (!device || !state) |
| 68 | return -EINVAL; |
| 69 | |
| 70 | if (!device->flags.power_manageable) { |
| 71 | /* TBD: Non-recursive algorithm for walking up hierarchy. */ |
| 72 | *state = device->parent ? |
| 73 | device->parent->power.state : ACPI_STATE_D0; |
| 74 | goto out; |
| 75 | } |
| 76 | |
| 77 | /* |
Rafael J. Wysocki | 75eb2d1 | 2013-03-24 22:54:40 +0100 | [diff] [blame] | 78 | * Get the device's power state from power resources settings and _PSC, |
| 79 | * if available. |
Rafael J. Wysocki | 9ce4e60 | 2013-01-17 14:11:08 +0100 | [diff] [blame] | 80 | */ |
Rafael J. Wysocki | 75eb2d1 | 2013-03-24 22:54:40 +0100 | [diff] [blame] | 81 | if (device->power.flags.power_resources) { |
Rafael J. Wysocki | 9ce4e60 | 2013-01-17 14:11:08 +0100 | [diff] [blame] | 82 | int error = acpi_power_get_inferred_state(device, &result); |
| 83 | if (error) |
| 84 | return error; |
Rafael J. Wysocki | 75eb2d1 | 2013-03-24 22:54:40 +0100 | [diff] [blame] | 85 | } |
| 86 | if (device->power.flags.explicit_get) { |
| 87 | acpi_handle handle = device->handle; |
| 88 | unsigned long long psc; |
| 89 | acpi_status status; |
| 90 | |
| 91 | status = acpi_evaluate_integer(handle, "_PSC", NULL, &psc); |
| 92 | if (ACPI_FAILURE(status)) |
| 93 | return -ENODEV; |
| 94 | |
| 95 | /* |
| 96 | * The power resources settings may indicate a power state |
Rafael J. Wysocki | 20dacb7 | 2015-05-16 01:55:35 +0200 | [diff] [blame] | 97 | * shallower than the actual power state of the device, because |
| 98 | * the same power resources may be referenced by other devices. |
Rafael J. Wysocki | 75eb2d1 | 2013-03-24 22:54:40 +0100 | [diff] [blame] | 99 | * |
Rafael J. Wysocki | 20dacb7 | 2015-05-16 01:55:35 +0200 | [diff] [blame] | 100 | * For systems predating ACPI 4.0 we assume that D3hot is the |
| 101 | * deepest state that can be supported. |
Rafael J. Wysocki | 75eb2d1 | 2013-03-24 22:54:40 +0100 | [diff] [blame] | 102 | */ |
| 103 | if (psc > result && psc < ACPI_STATE_D3_COLD) |
| 104 | result = psc; |
| 105 | else if (result == ACPI_STATE_UNKNOWN) |
Rafael J. Wysocki | 20dacb7 | 2015-05-16 01:55:35 +0200 | [diff] [blame] | 106 | result = psc > ACPI_STATE_D2 ? ACPI_STATE_D3_HOT : psc; |
Rafael J. Wysocki | 9ce4e60 | 2013-01-17 14:11:08 +0100 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /* |
| 110 | * If we were unsure about the device parent's power state up to this |
| 111 | * point, the fact that the device is in D0 implies that the parent has |
Mika Westerberg | 644f17a | 2013-10-10 13:28:46 +0300 | [diff] [blame] | 112 | * to be in D0 too, except if ignore_parent is set. |
Rafael J. Wysocki | 9ce4e60 | 2013-01-17 14:11:08 +0100 | [diff] [blame] | 113 | */ |
Mika Westerberg | 644f17a | 2013-10-10 13:28:46 +0300 | [diff] [blame] | 114 | if (!device->power.flags.ignore_parent && device->parent |
| 115 | && device->parent->power.state == ACPI_STATE_UNKNOWN |
Rafael J. Wysocki | 9ce4e60 | 2013-01-17 14:11:08 +0100 | [diff] [blame] | 116 | && result == ACPI_STATE_D0) |
| 117 | device->parent->power.state = ACPI_STATE_D0; |
| 118 | |
| 119 | *state = result; |
| 120 | |
| 121 | out: |
| 122 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] power state is %s\n", |
| 123 | device->pnp.bus_id, acpi_power_state_string(*state))); |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
Rafael J. Wysocki | 9c0f45e | 2013-01-22 12:55:52 +0100 | [diff] [blame] | 128 | static int acpi_dev_pm_explicit_set(struct acpi_device *adev, int state) |
| 129 | { |
| 130 | if (adev->power.states[state].flags.explicit_set) { |
| 131 | char method[5] = { '_', 'P', 'S', '0' + state, '\0' }; |
| 132 | acpi_status status; |
| 133 | |
| 134 | status = acpi_evaluate_object(adev->handle, method, NULL, NULL); |
| 135 | if (ACPI_FAILURE(status)) |
| 136 | return -ENODEV; |
| 137 | } |
| 138 | return 0; |
| 139 | } |
| 140 | |
Rafael J. Wysocki | 9ce4e60 | 2013-01-17 14:11:08 +0100 | [diff] [blame] | 141 | /** |
| 142 | * acpi_device_set_power - Set power state of an ACPI device. |
| 143 | * @device: Device to set the power state of. |
| 144 | * @state: New power state to set. |
| 145 | * |
| 146 | * Callers must ensure that the device is power manageable before using this |
| 147 | * function. |
| 148 | */ |
| 149 | int acpi_device_set_power(struct acpi_device *device, int state) |
| 150 | { |
Rafael J. Wysocki | 20dacb7 | 2015-05-16 01:55:35 +0200 | [diff] [blame] | 151 | int target_state = state; |
Rafael J. Wysocki | 9ce4e60 | 2013-01-17 14:11:08 +0100 | [diff] [blame] | 152 | int result = 0; |
Rafael J. Wysocki | 9ce4e60 | 2013-01-17 14:11:08 +0100 | [diff] [blame] | 153 | |
Rafael J. Wysocki | 2c7d132 | 2013-07-30 14:34:00 +0200 | [diff] [blame] | 154 | if (!device || !device->flags.power_manageable |
| 155 | || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD)) |
Rafael J. Wysocki | 9ce4e60 | 2013-01-17 14:11:08 +0100 | [diff] [blame] | 156 | return -EINVAL; |
| 157 | |
| 158 | /* Make sure this is a valid target state */ |
| 159 | |
| 160 | if (state == device->power.state) { |
Rafael J. Wysocki | b69137a | 2013-07-30 14:34:55 +0200 | [diff] [blame] | 161 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] already in %s\n", |
| 162 | device->pnp.bus_id, |
Rafael J. Wysocki | 9ce4e60 | 2013-01-17 14:11:08 +0100 | [diff] [blame] | 163 | acpi_power_state_string(state))); |
| 164 | return 0; |
| 165 | } |
| 166 | |
Rafael J. Wysocki | 20dacb7 | 2015-05-16 01:55:35 +0200 | [diff] [blame] | 167 | if (state == ACPI_STATE_D3_COLD) { |
| 168 | /* |
| 169 | * For transitions to D3cold we need to execute _PS3 and then |
| 170 | * possibly drop references to the power resources in use. |
| 171 | */ |
| 172 | state = ACPI_STATE_D3_HOT; |
| 173 | /* If _PR3 is not available, use D3hot as the target state. */ |
| 174 | if (!device->power.states[ACPI_STATE_D3_COLD].flags.valid) |
| 175 | target_state = state; |
| 176 | } else if (!device->power.states[state].flags.valid) { |
Rafael J. Wysocki | b69137a | 2013-07-30 14:34:55 +0200 | [diff] [blame] | 177 | dev_warn(&device->dev, "Power state %s not supported\n", |
| 178 | acpi_power_state_string(state)); |
Rafael J. Wysocki | 9ce4e60 | 2013-01-17 14:11:08 +0100 | [diff] [blame] | 179 | return -ENODEV; |
| 180 | } |
Rafael J. Wysocki | 20dacb7 | 2015-05-16 01:55:35 +0200 | [diff] [blame] | 181 | |
Mika Westerberg | 644f17a | 2013-10-10 13:28:46 +0300 | [diff] [blame] | 182 | if (!device->power.flags.ignore_parent && |
| 183 | device->parent && (state < device->parent->power.state)) { |
Rafael J. Wysocki | b69137a | 2013-07-30 14:34:55 +0200 | [diff] [blame] | 184 | dev_warn(&device->dev, |
Aaron Lu | 593298e | 2013-08-03 21:13:22 +0200 | [diff] [blame] | 185 | "Cannot transition to power state %s for parent in %s\n", |
| 186 | acpi_power_state_string(state), |
| 187 | acpi_power_state_string(device->parent->power.state)); |
Rafael J. Wysocki | 9ce4e60 | 2013-01-17 14:11:08 +0100 | [diff] [blame] | 188 | return -ENODEV; |
| 189 | } |
| 190 | |
Rafael J. Wysocki | 9ce4e60 | 2013-01-17 14:11:08 +0100 | [diff] [blame] | 191 | /* |
| 192 | * Transition Power |
| 193 | * ---------------- |
Rafael J. Wysocki | 20dacb7 | 2015-05-16 01:55:35 +0200 | [diff] [blame] | 194 | * In accordance with ACPI 6, _PSx is executed before manipulating power |
| 195 | * resources, unless the target state is D0, in which case _PS0 is |
| 196 | * supposed to be executed after turning the power resources on. |
Rafael J. Wysocki | 9ce4e60 | 2013-01-17 14:11:08 +0100 | [diff] [blame] | 197 | */ |
Rafael J. Wysocki | 20dacb7 | 2015-05-16 01:55:35 +0200 | [diff] [blame] | 198 | if (state > ACPI_STATE_D0) { |
| 199 | /* |
| 200 | * According to ACPI 6, devices cannot go from lower-power |
| 201 | * (deeper) states to higher-power (shallower) states. |
| 202 | */ |
| 203 | if (state < device->power.state) { |
| 204 | dev_warn(&device->dev, "Cannot transition from %s to %s\n", |
| 205 | acpi_power_state_string(device->power.state), |
| 206 | acpi_power_state_string(state)); |
| 207 | return -ENODEV; |
| 208 | } |
| 209 | |
| 210 | result = acpi_dev_pm_explicit_set(device, state); |
Rafael J. Wysocki | 9c0f45e | 2013-01-22 12:55:52 +0100 | [diff] [blame] | 211 | if (result) |
| 212 | goto end; |
Rafael J. Wysocki | 9ce4e60 | 2013-01-17 14:11:08 +0100 | [diff] [blame] | 213 | |
Rafael J. Wysocki | 20dacb7 | 2015-05-16 01:55:35 +0200 | [diff] [blame] | 214 | if (device->power.flags.power_resources) |
| 215 | result = acpi_power_transition(device, target_state); |
| 216 | } else { |
| 217 | if (device->power.flags.power_resources) { |
| 218 | result = acpi_power_transition(device, ACPI_STATE_D0); |
| 219 | if (result) |
| 220 | goto end; |
| 221 | } |
| 222 | result = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0); |
Rafael J. Wysocki | e565627 | 2013-01-22 12:56:35 +0100 | [diff] [blame] | 223 | } |
Rafael J. Wysocki | 9ce4e60 | 2013-01-17 14:11:08 +0100 | [diff] [blame] | 224 | |
Rafael J. Wysocki | e78adb7 | 2013-01-22 12:56:04 +0100 | [diff] [blame] | 225 | end: |
| 226 | if (result) { |
Rafael J. Wysocki | b69137a | 2013-07-30 14:34:55 +0200 | [diff] [blame] | 227 | dev_warn(&device->dev, "Failed to change power state to %s\n", |
| 228 | acpi_power_state_string(state)); |
Rafael J. Wysocki | e78adb7 | 2013-01-22 12:56:04 +0100 | [diff] [blame] | 229 | } else { |
Mika Westerberg | 71b6544 | 2015-07-28 13:51:21 +0300 | [diff] [blame] | 230 | device->power.state = target_state; |
Rafael J. Wysocki | 9ce4e60 | 2013-01-17 14:11:08 +0100 | [diff] [blame] | 231 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
| 232 | "Device [%s] transitioned to %s\n", |
| 233 | device->pnp.bus_id, |
| 234 | acpi_power_state_string(state))); |
| 235 | } |
| 236 | |
| 237 | return result; |
| 238 | } |
| 239 | EXPORT_SYMBOL(acpi_device_set_power); |
| 240 | |
| 241 | int acpi_bus_set_power(acpi_handle handle, int state) |
| 242 | { |
| 243 | struct acpi_device *device; |
| 244 | int result; |
| 245 | |
| 246 | result = acpi_bus_get_device(handle, &device); |
| 247 | if (result) |
| 248 | return result; |
| 249 | |
Rafael J. Wysocki | 9ce4e60 | 2013-01-17 14:11:08 +0100 | [diff] [blame] | 250 | return acpi_device_set_power(device, state); |
| 251 | } |
| 252 | EXPORT_SYMBOL(acpi_bus_set_power); |
| 253 | |
| 254 | int acpi_bus_init_power(struct acpi_device *device) |
| 255 | { |
| 256 | int state; |
| 257 | int result; |
| 258 | |
| 259 | if (!device) |
| 260 | return -EINVAL; |
| 261 | |
| 262 | device->power.state = ACPI_STATE_UNKNOWN; |
Rafael J. Wysocki | 202317a | 2013-11-22 21:54:37 +0100 | [diff] [blame] | 263 | if (!acpi_device_is_present(device)) |
Rafael J. Wysocki | 1b1f3e1 | 2015-01-01 23:38:28 +0100 | [diff] [blame] | 264 | return -ENXIO; |
Rafael J. Wysocki | 9ce4e60 | 2013-01-17 14:11:08 +0100 | [diff] [blame] | 265 | |
| 266 | result = acpi_device_get_power(device, &state); |
| 267 | if (result) |
| 268 | return result; |
| 269 | |
Rafael J. Wysocki | a236780 | 2013-01-22 12:54:38 +0100 | [diff] [blame] | 270 | if (state < ACPI_STATE_D3_COLD && device->power.flags.power_resources) { |
Rafael J. Wysocki | 20dacb7 | 2015-05-16 01:55:35 +0200 | [diff] [blame] | 271 | /* Reference count the power resources. */ |
Rafael J. Wysocki | 9ce4e60 | 2013-01-17 14:11:08 +0100 | [diff] [blame] | 272 | result = acpi_power_on_resources(device, state); |
Rafael J. Wysocki | a236780 | 2013-01-22 12:54:38 +0100 | [diff] [blame] | 273 | if (result) |
| 274 | return result; |
Rafael J. Wysocki | 9ce4e60 | 2013-01-17 14:11:08 +0100 | [diff] [blame] | 275 | |
Rafael J. Wysocki | 20dacb7 | 2015-05-16 01:55:35 +0200 | [diff] [blame] | 276 | if (state == ACPI_STATE_D0) { |
| 277 | /* |
| 278 | * If _PSC is not present and the state inferred from |
| 279 | * power resources appears to be D0, it still may be |
| 280 | * necessary to execute _PS0 at this point, because |
| 281 | * another device using the same power resources may |
| 282 | * have been put into D0 previously and that's why we |
| 283 | * see D0 here. |
| 284 | */ |
| 285 | result = acpi_dev_pm_explicit_set(device, state); |
| 286 | if (result) |
| 287 | return result; |
| 288 | } |
Rafael J. Wysocki | b378549 | 2013-02-01 23:43:02 +0100 | [diff] [blame] | 289 | } else if (state == ACPI_STATE_UNKNOWN) { |
Rafael J. Wysocki | 7cd8407 | 2013-06-05 14:01:19 +0200 | [diff] [blame] | 290 | /* |
| 291 | * No power resources and missing _PSC? Cross fingers and make |
| 292 | * it D0 in hope that this is what the BIOS put the device into. |
| 293 | * [We tried to force D0 here by executing _PS0, but that broke |
| 294 | * Toshiba P870-303 in a nasty way.] |
| 295 | */ |
Rafael J. Wysocki | b378549 | 2013-02-01 23:43:02 +0100 | [diff] [blame] | 296 | state = ACPI_STATE_D0; |
Rafael J. Wysocki | a236780 | 2013-01-22 12:54:38 +0100 | [diff] [blame] | 297 | } |
| 298 | device->power.state = state; |
| 299 | return 0; |
Rafael J. Wysocki | 9ce4e60 | 2013-01-17 14:11:08 +0100 | [diff] [blame] | 300 | } |
| 301 | |
Rafael J. Wysocki | b9e95fc | 2013-06-19 00:45:34 +0200 | [diff] [blame] | 302 | /** |
| 303 | * acpi_device_fix_up_power - Force device with missing _PSC into D0. |
| 304 | * @device: Device object whose power state is to be fixed up. |
| 305 | * |
| 306 | * Devices without power resources and _PSC, but having _PS0 and _PS3 defined, |
| 307 | * are assumed to be put into D0 by the BIOS. However, in some cases that may |
| 308 | * not be the case and this function should be used then. |
| 309 | */ |
| 310 | int acpi_device_fix_up_power(struct acpi_device *device) |
| 311 | { |
| 312 | int ret = 0; |
| 313 | |
| 314 | if (!device->power.flags.power_resources |
| 315 | && !device->power.flags.explicit_get |
| 316 | && device->power.state == ACPI_STATE_D0) |
| 317 | ret = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0); |
| 318 | |
| 319 | return ret; |
| 320 | } |
| 321 | |
Rafael J. Wysocki | 202317a | 2013-11-22 21:54:37 +0100 | [diff] [blame] | 322 | int acpi_device_update_power(struct acpi_device *device, int *state_p) |
Rafael J. Wysocki | 9ce4e60 | 2013-01-17 14:11:08 +0100 | [diff] [blame] | 323 | { |
Rafael J. Wysocki | 9ce4e60 | 2013-01-17 14:11:08 +0100 | [diff] [blame] | 324 | int state; |
| 325 | int result; |
| 326 | |
Rafael J. Wysocki | 202317a | 2013-11-22 21:54:37 +0100 | [diff] [blame] | 327 | if (device->power.state == ACPI_STATE_UNKNOWN) { |
| 328 | result = acpi_bus_init_power(device); |
| 329 | if (!result && state_p) |
| 330 | *state_p = device->power.state; |
| 331 | |
Rafael J. Wysocki | 9ce4e60 | 2013-01-17 14:11:08 +0100 | [diff] [blame] | 332 | return result; |
Rafael J. Wysocki | 202317a | 2013-11-22 21:54:37 +0100 | [diff] [blame] | 333 | } |
Rafael J. Wysocki | 9ce4e60 | 2013-01-17 14:11:08 +0100 | [diff] [blame] | 334 | |
| 335 | result = acpi_device_get_power(device, &state); |
| 336 | if (result) |
| 337 | return result; |
| 338 | |
Rafael J. Wysocki | 91bdad0 | 2013-07-04 13:22:11 +0200 | [diff] [blame] | 339 | if (state == ACPI_STATE_UNKNOWN) { |
Rafael J. Wysocki | 511d5c4 | 2013-02-03 14:57:32 +0100 | [diff] [blame] | 340 | state = ACPI_STATE_D0; |
Rafael J. Wysocki | 91bdad0 | 2013-07-04 13:22:11 +0200 | [diff] [blame] | 341 | result = acpi_device_set_power(device, state); |
| 342 | if (result) |
| 343 | return result; |
| 344 | } else { |
| 345 | if (device->power.flags.power_resources) { |
| 346 | /* |
| 347 | * We don't need to really switch the state, bu we need |
| 348 | * to update the power resources' reference counters. |
| 349 | */ |
| 350 | result = acpi_power_transition(device, state); |
| 351 | if (result) |
| 352 | return result; |
| 353 | } |
| 354 | device->power.state = state; |
| 355 | } |
| 356 | if (state_p) |
Rafael J. Wysocki | 9ce4e60 | 2013-01-17 14:11:08 +0100 | [diff] [blame] | 357 | *state_p = state; |
| 358 | |
Rafael J. Wysocki | 91bdad0 | 2013-07-04 13:22:11 +0200 | [diff] [blame] | 359 | return 0; |
Rafael J. Wysocki | 9ce4e60 | 2013-01-17 14:11:08 +0100 | [diff] [blame] | 360 | } |
Aaron Lu | 2bb3a2b | 2013-11-19 15:43:52 +0800 | [diff] [blame] | 361 | EXPORT_SYMBOL_GPL(acpi_device_update_power); |
Rafael J. Wysocki | 202317a | 2013-11-22 21:54:37 +0100 | [diff] [blame] | 362 | |
| 363 | int acpi_bus_update_power(acpi_handle handle, int *state_p) |
| 364 | { |
| 365 | struct acpi_device *device; |
| 366 | int result; |
| 367 | |
| 368 | result = acpi_bus_get_device(handle, &device); |
| 369 | return result ? result : acpi_device_update_power(device, state_p); |
| 370 | } |
Rafael J. Wysocki | 9ce4e60 | 2013-01-17 14:11:08 +0100 | [diff] [blame] | 371 | EXPORT_SYMBOL_GPL(acpi_bus_update_power); |
| 372 | |
| 373 | bool acpi_bus_power_manageable(acpi_handle handle) |
| 374 | { |
| 375 | struct acpi_device *device; |
| 376 | int result; |
| 377 | |
| 378 | result = acpi_bus_get_device(handle, &device); |
| 379 | return result ? false : device->flags.power_manageable; |
| 380 | } |
| 381 | EXPORT_SYMBOL(acpi_bus_power_manageable); |
| 382 | |
Rafael J. Wysocki | ec4602a | 2013-05-16 22:29:28 +0200 | [diff] [blame] | 383 | #ifdef CONFIG_PM |
| 384 | static DEFINE_MUTEX(acpi_pm_notifier_lock); |
| 385 | |
Rafael J. Wysocki | c072530 | 2014-07-23 01:00:45 +0200 | [diff] [blame] | 386 | static void acpi_pm_notify_handler(acpi_handle handle, u32 val, void *not_used) |
| 387 | { |
| 388 | struct acpi_device *adev; |
| 389 | |
| 390 | if (val != ACPI_NOTIFY_DEVICE_WAKE) |
| 391 | return; |
| 392 | |
| 393 | adev = acpi_bus_get_acpi_device(handle); |
| 394 | if (!adev) |
| 395 | return; |
| 396 | |
| 397 | mutex_lock(&acpi_pm_notifier_lock); |
| 398 | |
| 399 | if (adev->wakeup.flags.notifier_present) { |
| 400 | __pm_wakeup_event(adev->wakeup.ws, 0); |
| 401 | if (adev->wakeup.context.work.func) |
| 402 | queue_pm_work(&adev->wakeup.context.work); |
| 403 | } |
| 404 | |
| 405 | mutex_unlock(&acpi_pm_notifier_lock); |
| 406 | |
| 407 | acpi_bus_put_acpi_device(adev); |
| 408 | } |
| 409 | |
Rafael J. Wysocki | ec4602a | 2013-05-16 22:29:28 +0200 | [diff] [blame] | 410 | /** |
Rafael J. Wysocki | c072530 | 2014-07-23 01:00:45 +0200 | [diff] [blame] | 411 | * acpi_add_pm_notifier - Register PM notify handler for given ACPI device. |
| 412 | * @adev: ACPI device to add the notify handler for. |
| 413 | * @dev: Device to generate a wakeup event for while handling the notification. |
| 414 | * @work_func: Work function to execute when handling the notification. |
Rafael J. Wysocki | ec4602a | 2013-05-16 22:29:28 +0200 | [diff] [blame] | 415 | * |
| 416 | * NOTE: @adev need not be a run-wake or wakeup device to be a valid source of |
| 417 | * PM wakeup events. For example, wakeup events may be generated for bridges |
| 418 | * if one of the devices below the bridge is signaling wakeup, even if the |
| 419 | * bridge itself doesn't have a wakeup GPE associated with it. |
| 420 | */ |
Rafael J. Wysocki | c072530 | 2014-07-23 01:00:45 +0200 | [diff] [blame] | 421 | acpi_status acpi_add_pm_notifier(struct acpi_device *adev, struct device *dev, |
| 422 | void (*work_func)(struct work_struct *work)) |
Rafael J. Wysocki | ec4602a | 2013-05-16 22:29:28 +0200 | [diff] [blame] | 423 | { |
| 424 | acpi_status status = AE_ALREADY_EXISTS; |
| 425 | |
Rafael J. Wysocki | c072530 | 2014-07-23 01:00:45 +0200 | [diff] [blame] | 426 | if (!dev && !work_func) |
| 427 | return AE_BAD_PARAMETER; |
| 428 | |
Rafael J. Wysocki | ec4602a | 2013-05-16 22:29:28 +0200 | [diff] [blame] | 429 | mutex_lock(&acpi_pm_notifier_lock); |
| 430 | |
| 431 | if (adev->wakeup.flags.notifier_present) |
| 432 | goto out; |
| 433 | |
Rafael J. Wysocki | c072530 | 2014-07-23 01:00:45 +0200 | [diff] [blame] | 434 | adev->wakeup.ws = wakeup_source_register(dev_name(&adev->dev)); |
| 435 | adev->wakeup.context.dev = dev; |
| 436 | if (work_func) |
| 437 | INIT_WORK(&adev->wakeup.context.work, work_func); |
| 438 | |
| 439 | status = acpi_install_notify_handler(adev->handle, ACPI_SYSTEM_NOTIFY, |
| 440 | acpi_pm_notify_handler, NULL); |
Rafael J. Wysocki | ec4602a | 2013-05-16 22:29:28 +0200 | [diff] [blame] | 441 | if (ACPI_FAILURE(status)) |
| 442 | goto out; |
| 443 | |
| 444 | adev->wakeup.flags.notifier_present = true; |
| 445 | |
| 446 | out: |
| 447 | mutex_unlock(&acpi_pm_notifier_lock); |
| 448 | return status; |
| 449 | } |
| 450 | |
| 451 | /** |
| 452 | * acpi_remove_pm_notifier - Unregister PM notifier from given ACPI device. |
| 453 | * @adev: ACPI device to remove the notifier from. |
| 454 | */ |
Rafael J. Wysocki | c072530 | 2014-07-23 01:00:45 +0200 | [diff] [blame] | 455 | acpi_status acpi_remove_pm_notifier(struct acpi_device *adev) |
Rafael J. Wysocki | ec4602a | 2013-05-16 22:29:28 +0200 | [diff] [blame] | 456 | { |
| 457 | acpi_status status = AE_BAD_PARAMETER; |
| 458 | |
| 459 | mutex_lock(&acpi_pm_notifier_lock); |
| 460 | |
| 461 | if (!adev->wakeup.flags.notifier_present) |
| 462 | goto out; |
| 463 | |
| 464 | status = acpi_remove_notify_handler(adev->handle, |
| 465 | ACPI_SYSTEM_NOTIFY, |
Rafael J. Wysocki | c072530 | 2014-07-23 01:00:45 +0200 | [diff] [blame] | 466 | acpi_pm_notify_handler); |
Rafael J. Wysocki | ec4602a | 2013-05-16 22:29:28 +0200 | [diff] [blame] | 467 | if (ACPI_FAILURE(status)) |
| 468 | goto out; |
| 469 | |
Rafael J. Wysocki | c072530 | 2014-07-23 01:00:45 +0200 | [diff] [blame] | 470 | if (adev->wakeup.context.work.func) { |
| 471 | cancel_work_sync(&adev->wakeup.context.work); |
| 472 | adev->wakeup.context.work.func = NULL; |
| 473 | } |
| 474 | adev->wakeup.context.dev = NULL; |
| 475 | wakeup_source_unregister(adev->wakeup.ws); |
| 476 | |
Rafael J. Wysocki | ec4602a | 2013-05-16 22:29:28 +0200 | [diff] [blame] | 477 | adev->wakeup.flags.notifier_present = false; |
| 478 | |
| 479 | out: |
| 480 | mutex_unlock(&acpi_pm_notifier_lock); |
| 481 | return status; |
| 482 | } |
| 483 | |
Rafael J. Wysocki | 9ce4e60 | 2013-01-17 14:11:08 +0100 | [diff] [blame] | 484 | bool acpi_bus_can_wakeup(acpi_handle handle) |
| 485 | { |
| 486 | struct acpi_device *device; |
| 487 | int result; |
| 488 | |
| 489 | result = acpi_bus_get_device(handle, &device); |
| 490 | return result ? false : device->wakeup.flags.valid; |
| 491 | } |
| 492 | EXPORT_SYMBOL(acpi_bus_can_wakeup); |
| 493 | |
| 494 | /** |
Rafael J. Wysocki | b25c77e | 2013-06-16 00:37:42 +0200 | [diff] [blame] | 495 | * acpi_dev_pm_get_state - Get preferred power state of ACPI device. |
Rafael J. Wysocki | 86b3832 | 2012-11-02 01:40:18 +0100 | [diff] [blame] | 496 | * @dev: Device whose preferred target power state to return. |
| 497 | * @adev: ACPI device node corresponding to @dev. |
| 498 | * @target_state: System state to match the resultant device state. |
Rafael J. Wysocki | fa1675b | 2013-06-16 00:37:59 +0200 | [diff] [blame] | 499 | * @d_min_p: Location to store the highest power state available to the device. |
| 500 | * @d_max_p: Location to store the lowest power state available to the device. |
Rafael J. Wysocki | 86b3832 | 2012-11-02 01:40:18 +0100 | [diff] [blame] | 501 | * |
Rafael J. Wysocki | fa1675b | 2013-06-16 00:37:59 +0200 | [diff] [blame] | 502 | * Find the lowest power (highest number) and highest power (lowest number) ACPI |
| 503 | * device power states that the device can be in while the system is in the |
| 504 | * state represented by @target_state. Store the integer numbers representing |
| 505 | * those stats in the memory locations pointed to by @d_max_p and @d_min_p, |
| 506 | * respectively. |
Rafael J. Wysocki | 86b3832 | 2012-11-02 01:40:18 +0100 | [diff] [blame] | 507 | * |
| 508 | * Callers must ensure that @dev and @adev are valid pointers and that @adev |
| 509 | * actually corresponds to @dev before using this function. |
Rafael J. Wysocki | fa1675b | 2013-06-16 00:37:59 +0200 | [diff] [blame] | 510 | * |
| 511 | * Returns 0 on success or -ENODATA when one of the ACPI methods fails or |
| 512 | * returns a value that doesn't make sense. The memory locations pointed to by |
| 513 | * @d_max_p and @d_min_p are only modified on success. |
Rafael J. Wysocki | 86b3832 | 2012-11-02 01:40:18 +0100 | [diff] [blame] | 514 | */ |
Rafael J. Wysocki | b25c77e | 2013-06-16 00:37:42 +0200 | [diff] [blame] | 515 | static int acpi_dev_pm_get_state(struct device *dev, struct acpi_device *adev, |
Rafael J. Wysocki | fa1675b | 2013-06-16 00:37:59 +0200 | [diff] [blame] | 516 | u32 target_state, int *d_min_p, int *d_max_p) |
Rafael J. Wysocki | 86b3832 | 2012-11-02 01:40:18 +0100 | [diff] [blame] | 517 | { |
Rafael J. Wysocki | fa1675b | 2013-06-16 00:37:59 +0200 | [diff] [blame] | 518 | char method[] = { '_', 'S', '0' + target_state, 'D', '\0' }; |
| 519 | acpi_handle handle = adev->handle; |
| 520 | unsigned long long ret; |
| 521 | int d_min, d_max; |
Rafael J. Wysocki | 86b3832 | 2012-11-02 01:40:18 +0100 | [diff] [blame] | 522 | bool wakeup = false; |
Rafael J. Wysocki | fa1675b | 2013-06-16 00:37:59 +0200 | [diff] [blame] | 523 | acpi_status status; |
Rafael J. Wysocki | 86b3832 | 2012-11-02 01:40:18 +0100 | [diff] [blame] | 524 | |
Rafael J. Wysocki | 86b3832 | 2012-11-02 01:40:18 +0100 | [diff] [blame] | 525 | /* |
Rafael J. Wysocki | fa1675b | 2013-06-16 00:37:59 +0200 | [diff] [blame] | 526 | * If the system state is S0, the lowest power state the device can be |
| 527 | * in is D3cold, unless the device has _S0W and is supposed to signal |
| 528 | * wakeup, in which case the return value of _S0W has to be used as the |
| 529 | * lowest power state available to the device. |
Rafael J. Wysocki | 86b3832 | 2012-11-02 01:40:18 +0100 | [diff] [blame] | 530 | */ |
| 531 | d_min = ACPI_STATE_D0; |
Rafael J. Wysocki | 4c164ae | 2013-06-16 00:37:50 +0200 | [diff] [blame] | 532 | d_max = ACPI_STATE_D3_COLD; |
Rafael J. Wysocki | 86b3832 | 2012-11-02 01:40:18 +0100 | [diff] [blame] | 533 | |
| 534 | /* |
| 535 | * If present, _SxD methods return the minimum D-state (highest power |
| 536 | * state) we can use for the corresponding S-states. Otherwise, the |
| 537 | * minimum D-state is D0 (ACPI 3.x). |
Rafael J. Wysocki | 86b3832 | 2012-11-02 01:40:18 +0100 | [diff] [blame] | 538 | */ |
| 539 | if (target_state > ACPI_STATE_S0) { |
Rafael J. Wysocki | fa1675b | 2013-06-16 00:37:59 +0200 | [diff] [blame] | 540 | /* |
| 541 | * We rely on acpi_evaluate_integer() not clobbering the integer |
| 542 | * provided if AE_NOT_FOUND is returned. |
| 543 | */ |
| 544 | ret = d_min; |
| 545 | status = acpi_evaluate_integer(handle, method, NULL, &ret); |
| 546 | if ((ACPI_FAILURE(status) && status != AE_NOT_FOUND) |
| 547 | || ret > ACPI_STATE_D3_COLD) |
| 548 | return -ENODATA; |
| 549 | |
| 550 | /* |
| 551 | * We need to handle legacy systems where D3hot and D3cold are |
| 552 | * the same and 3 is returned in both cases, so fall back to |
| 553 | * D3cold if D3hot is not a valid state. |
| 554 | */ |
| 555 | if (!adev->power.states[ret].flags.valid) { |
| 556 | if (ret == ACPI_STATE_D3_HOT) |
| 557 | ret = ACPI_STATE_D3_COLD; |
| 558 | else |
| 559 | return -ENODATA; |
| 560 | } |
| 561 | d_min = ret; |
Rafael J. Wysocki | 86b3832 | 2012-11-02 01:40:18 +0100 | [diff] [blame] | 562 | wakeup = device_may_wakeup(dev) && adev->wakeup.flags.valid |
| 563 | && adev->wakeup.sleep_state >= target_state; |
| 564 | } else if (dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) != |
| 565 | PM_QOS_FLAGS_NONE) { |
| 566 | wakeup = adev->wakeup.flags.valid; |
| 567 | } |
| 568 | |
| 569 | /* |
| 570 | * If _PRW says we can wake up the system from the target sleep state, |
| 571 | * the D-state returned by _SxD is sufficient for that (we assume a |
| 572 | * wakeup-aware driver if wake is set). Still, if _SxW exists |
| 573 | * (ACPI 3.x), it should return the maximum (lowest power) D-state that |
| 574 | * can wake the system. _S0W may be valid, too. |
| 575 | */ |
| 576 | if (wakeup) { |
Rafael J. Wysocki | fa1675b | 2013-06-16 00:37:59 +0200 | [diff] [blame] | 577 | method[3] = 'W'; |
| 578 | status = acpi_evaluate_integer(handle, method, NULL, &ret); |
| 579 | if (status == AE_NOT_FOUND) { |
| 580 | if (target_state > ACPI_STATE_S0) |
Rafael J. Wysocki | 86b3832 | 2012-11-02 01:40:18 +0100 | [diff] [blame] | 581 | d_max = d_min; |
Rafael J. Wysocki | fa1675b | 2013-06-16 00:37:59 +0200 | [diff] [blame] | 582 | } else if (ACPI_SUCCESS(status) && ret <= ACPI_STATE_D3_COLD) { |
| 583 | /* Fall back to D3cold if ret is not a valid state. */ |
| 584 | if (!adev->power.states[ret].flags.valid) |
| 585 | ret = ACPI_STATE_D3_COLD; |
| 586 | |
| 587 | d_max = ret > d_min ? ret : d_min; |
| 588 | } else { |
| 589 | return -ENODATA; |
Rafael J. Wysocki | 86b3832 | 2012-11-02 01:40:18 +0100 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | |
Rafael J. Wysocki | 86b3832 | 2012-11-02 01:40:18 +0100 | [diff] [blame] | 593 | if (d_min_p) |
| 594 | *d_min_p = d_min; |
Rafael J. Wysocki | fa1675b | 2013-06-16 00:37:59 +0200 | [diff] [blame] | 595 | |
| 596 | if (d_max_p) |
| 597 | *d_max_p = d_max; |
| 598 | |
| 599 | return 0; |
Rafael J. Wysocki | 86b3832 | 2012-11-02 01:40:18 +0100 | [diff] [blame] | 600 | } |
Rafael J. Wysocki | cd7bd02 | 2012-11-02 01:40:28 +0100 | [diff] [blame] | 601 | |
Rafael J. Wysocki | a6ae759 | 2012-11-02 01:40:53 +0100 | [diff] [blame] | 602 | /** |
| 603 | * acpi_pm_device_sleep_state - Get preferred power state of ACPI device. |
| 604 | * @dev: Device whose preferred target power state to return. |
| 605 | * @d_min_p: Location to store the upper limit of the allowed states range. |
| 606 | * @d_max_in: Deepest low-power state to take into consideration. |
| 607 | * Return value: Preferred power state of the device on success, -ENODEV |
Rafael J. Wysocki | fa1675b | 2013-06-16 00:37:59 +0200 | [diff] [blame] | 608 | * if there's no 'struct acpi_device' for @dev, -EINVAL if @d_max_in is |
| 609 | * incorrect, or -ENODATA on ACPI method failure. |
Rafael J. Wysocki | a6ae759 | 2012-11-02 01:40:53 +0100 | [diff] [blame] | 610 | * |
| 611 | * The caller must ensure that @dev is valid before using this function. |
| 612 | */ |
| 613 | int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in) |
| 614 | { |
Rafael J. Wysocki | a6ae759 | 2012-11-02 01:40:53 +0100 | [diff] [blame] | 615 | struct acpi_device *adev; |
Rafael J. Wysocki | 9b5c7a5 | 2013-06-27 14:01:02 +0200 | [diff] [blame] | 616 | int ret, d_min, d_max; |
Rafael J. Wysocki | fa1675b | 2013-06-16 00:37:59 +0200 | [diff] [blame] | 617 | |
| 618 | if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3_COLD) |
| 619 | return -EINVAL; |
| 620 | |
Rafael J. Wysocki | 20dacb7 | 2015-05-16 01:55:35 +0200 | [diff] [blame] | 621 | if (d_max_in > ACPI_STATE_D2) { |
Rafael J. Wysocki | fa1675b | 2013-06-16 00:37:59 +0200 | [diff] [blame] | 622 | enum pm_qos_flags_status stat; |
| 623 | |
| 624 | stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF); |
| 625 | if (stat == PM_QOS_FLAGS_ALL) |
Rafael J. Wysocki | 20dacb7 | 2015-05-16 01:55:35 +0200 | [diff] [blame] | 626 | d_max_in = ACPI_STATE_D2; |
Rafael J. Wysocki | fa1675b | 2013-06-16 00:37:59 +0200 | [diff] [blame] | 627 | } |
Rafael J. Wysocki | a6ae759 | 2012-11-02 01:40:53 +0100 | [diff] [blame] | 628 | |
Rafael J. Wysocki | 17653a3 | 2014-07-23 01:01:41 +0200 | [diff] [blame] | 629 | adev = ACPI_COMPANION(dev); |
| 630 | if (!adev) { |
| 631 | dev_dbg(dev, "ACPI companion missing in %s!\n", __func__); |
Rafael J. Wysocki | a6ae759 | 2012-11-02 01:40:53 +0100 | [diff] [blame] | 632 | return -ENODEV; |
| 633 | } |
| 634 | |
Rafael J. Wysocki | fa1675b | 2013-06-16 00:37:59 +0200 | [diff] [blame] | 635 | ret = acpi_dev_pm_get_state(dev, adev, acpi_target_system_state(), |
Rafael J. Wysocki | 9b5c7a5 | 2013-06-27 14:01:02 +0200 | [diff] [blame] | 636 | &d_min, &d_max); |
Rafael J. Wysocki | fa1675b | 2013-06-16 00:37:59 +0200 | [diff] [blame] | 637 | if (ret) |
| 638 | return ret; |
| 639 | |
Rafael J. Wysocki | 9b5c7a5 | 2013-06-27 14:01:02 +0200 | [diff] [blame] | 640 | if (d_max_in < d_min) |
Rafael J. Wysocki | fa1675b | 2013-06-16 00:37:59 +0200 | [diff] [blame] | 641 | return -EINVAL; |
| 642 | |
| 643 | if (d_max > d_max_in) { |
Rafael J. Wysocki | 9b5c7a5 | 2013-06-27 14:01:02 +0200 | [diff] [blame] | 644 | for (d_max = d_max_in; d_max > d_min; d_max--) { |
Rafael J. Wysocki | fa1675b | 2013-06-16 00:37:59 +0200 | [diff] [blame] | 645 | if (adev->power.states[d_max].flags.valid) |
| 646 | break; |
| 647 | } |
| 648 | } |
Rafael J. Wysocki | 9b5c7a5 | 2013-06-27 14:01:02 +0200 | [diff] [blame] | 649 | |
| 650 | if (d_min_p) |
| 651 | *d_min_p = d_min; |
| 652 | |
Rafael J. Wysocki | fa1675b | 2013-06-16 00:37:59 +0200 | [diff] [blame] | 653 | return d_max; |
Rafael J. Wysocki | a6ae759 | 2012-11-02 01:40:53 +0100 | [diff] [blame] | 654 | } |
| 655 | EXPORT_SYMBOL(acpi_pm_device_sleep_state); |
| 656 | |
Rafael J. Wysocki | cd7bd02 | 2012-11-02 01:40:28 +0100 | [diff] [blame] | 657 | /** |
Rafael J. Wysocki | c072530 | 2014-07-23 01:00:45 +0200 | [diff] [blame] | 658 | * acpi_pm_notify_work_func - ACPI devices wakeup notification work function. |
| 659 | * @work: Work item to handle. |
Rafael J. Wysocki | e5cc8ef | 2012-11-02 01:41:01 +0100 | [diff] [blame] | 660 | */ |
Rafael J. Wysocki | c072530 | 2014-07-23 01:00:45 +0200 | [diff] [blame] | 661 | static void acpi_pm_notify_work_func(struct work_struct *work) |
Rafael J. Wysocki | e5cc8ef | 2012-11-02 01:41:01 +0100 | [diff] [blame] | 662 | { |
Rafael J. Wysocki | c072530 | 2014-07-23 01:00:45 +0200 | [diff] [blame] | 663 | struct device *dev; |
Rafael J. Wysocki | e5cc8ef | 2012-11-02 01:41:01 +0100 | [diff] [blame] | 664 | |
Rafael J. Wysocki | c072530 | 2014-07-23 01:00:45 +0200 | [diff] [blame] | 665 | dev = container_of(work, struct acpi_device_wakeup_context, work)->dev; |
| 666 | if (dev) { |
Rafael J. Wysocki | e5cc8ef | 2012-11-02 01:41:01 +0100 | [diff] [blame] | 667 | pm_wakeup_event(dev, 0); |
| 668 | pm_runtime_resume(dev); |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | /** |
Rafael J. Wysocki | f35cec2 | 2014-07-23 01:00:53 +0200 | [diff] [blame] | 673 | * acpi_device_wakeup - Enable/disable wakeup functionality for device. |
| 674 | * @adev: ACPI device to enable/disable wakeup functionality for. |
| 675 | * @target_state: State the system is transitioning into. |
Rafael J. Wysocki | cd7bd02 | 2012-11-02 01:40:28 +0100 | [diff] [blame] | 676 | * @enable: Whether to enable or disable the wakeup functionality. |
| 677 | * |
Rafael J. Wysocki | dee8370 | 2012-11-02 01:40:36 +0100 | [diff] [blame] | 678 | * Enable/disable the GPE associated with @adev so that it can generate |
| 679 | * wakeup signals for the device in response to external (remote) events and |
| 680 | * enable/disable device wakeup power. |
| 681 | * |
| 682 | * Callers must ensure that @adev is a valid ACPI device node before executing |
| 683 | * this function. |
| 684 | */ |
Rafael J. Wysocki | f35cec2 | 2014-07-23 01:00:53 +0200 | [diff] [blame] | 685 | static int acpi_device_wakeup(struct acpi_device *adev, u32 target_state, |
| 686 | bool enable) |
Rafael J. Wysocki | dee8370 | 2012-11-02 01:40:36 +0100 | [diff] [blame] | 687 | { |
| 688 | struct acpi_device_wakeup *wakeup = &adev->wakeup; |
| 689 | |
| 690 | if (enable) { |
| 691 | acpi_status res; |
| 692 | int error; |
| 693 | |
Rafael J. Wysocki | f35cec2 | 2014-07-23 01:00:53 +0200 | [diff] [blame] | 694 | error = acpi_enable_wakeup_device_power(adev, target_state); |
Rafael J. Wysocki | dee8370 | 2012-11-02 01:40:36 +0100 | [diff] [blame] | 695 | if (error) |
| 696 | return error; |
| 697 | |
Rafael J. Wysocki | 175f8e2 | 2014-12-12 22:51:58 +0100 | [diff] [blame] | 698 | if (adev->wakeup.flags.enabled) |
| 699 | return 0; |
| 700 | |
Rafael J. Wysocki | dee8370 | 2012-11-02 01:40:36 +0100 | [diff] [blame] | 701 | res = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number); |
Rafael J. Wysocki | 175f8e2 | 2014-12-12 22:51:58 +0100 | [diff] [blame] | 702 | if (ACPI_SUCCESS(res)) { |
| 703 | adev->wakeup.flags.enabled = 1; |
| 704 | } else { |
Rafael J. Wysocki | dee8370 | 2012-11-02 01:40:36 +0100 | [diff] [blame] | 705 | acpi_disable_wakeup_device_power(adev); |
| 706 | return -EIO; |
| 707 | } |
| 708 | } else { |
Rafael J. Wysocki | 175f8e2 | 2014-12-12 22:51:58 +0100 | [diff] [blame] | 709 | if (adev->wakeup.flags.enabled) { |
| 710 | acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number); |
| 711 | adev->wakeup.flags.enabled = 0; |
| 712 | } |
Rafael J. Wysocki | dee8370 | 2012-11-02 01:40:36 +0100 | [diff] [blame] | 713 | acpi_disable_wakeup_device_power(adev); |
| 714 | } |
| 715 | return 0; |
| 716 | } |
| 717 | |
| 718 | /** |
| 719 | * acpi_pm_device_run_wake - Enable/disable remote wakeup for given device. |
| 720 | * @dev: Device to enable/disable the platform to wake up. |
| 721 | * @enable: Whether to enable or disable the wakeup functionality. |
Rafael J. Wysocki | cd7bd02 | 2012-11-02 01:40:28 +0100 | [diff] [blame] | 722 | */ |
| 723 | int acpi_pm_device_run_wake(struct device *phys_dev, bool enable) |
| 724 | { |
Rafael J. Wysocki | dee8370 | 2012-11-02 01:40:36 +0100 | [diff] [blame] | 725 | struct acpi_device *adev; |
Rafael J. Wysocki | cd7bd02 | 2012-11-02 01:40:28 +0100 | [diff] [blame] | 726 | |
| 727 | if (!device_run_wake(phys_dev)) |
| 728 | return -EINVAL; |
| 729 | |
Rafael J. Wysocki | 17653a3 | 2014-07-23 01:01:41 +0200 | [diff] [blame] | 730 | adev = ACPI_COMPANION(phys_dev); |
| 731 | if (!adev) { |
| 732 | dev_dbg(phys_dev, "ACPI companion missing in %s!\n", __func__); |
Rafael J. Wysocki | cd7bd02 | 2012-11-02 01:40:28 +0100 | [diff] [blame] | 733 | return -ENODEV; |
| 734 | } |
| 735 | |
Zhang Rui | 67598a1 | 2014-10-23 20:20:00 +0800 | [diff] [blame] | 736 | return acpi_device_wakeup(adev, ACPI_STATE_S0, enable); |
Rafael J. Wysocki | cd7bd02 | 2012-11-02 01:40:28 +0100 | [diff] [blame] | 737 | } |
| 738 | EXPORT_SYMBOL(acpi_pm_device_run_wake); |
Rafael J. Wysocki | dee8370 | 2012-11-02 01:40:36 +0100 | [diff] [blame] | 739 | |
Mika Westerberg | 4d56410 | 2013-01-14 20:13:37 +0000 | [diff] [blame] | 740 | #ifdef CONFIG_PM_SLEEP |
Rafael J. Wysocki | dee8370 | 2012-11-02 01:40:36 +0100 | [diff] [blame] | 741 | /** |
Rafael J. Wysocki | a6ae759 | 2012-11-02 01:40:53 +0100 | [diff] [blame] | 742 | * acpi_pm_device_sleep_wake - Enable or disable device to wake up the system. |
| 743 | * @dev: Device to enable/desible to wake up the system from sleep states. |
| 744 | * @enable: Whether to enable or disable @dev to wake up the system. |
| 745 | */ |
| 746 | int acpi_pm_device_sleep_wake(struct device *dev, bool enable) |
| 747 | { |
Rafael J. Wysocki | a6ae759 | 2012-11-02 01:40:53 +0100 | [diff] [blame] | 748 | struct acpi_device *adev; |
| 749 | int error; |
| 750 | |
| 751 | if (!device_can_wakeup(dev)) |
| 752 | return -EINVAL; |
| 753 | |
Rafael J. Wysocki | 17653a3 | 2014-07-23 01:01:41 +0200 | [diff] [blame] | 754 | adev = ACPI_COMPANION(dev); |
| 755 | if (!adev) { |
| 756 | dev_dbg(dev, "ACPI companion missing in %s!\n", __func__); |
Rafael J. Wysocki | a6ae759 | 2012-11-02 01:40:53 +0100 | [diff] [blame] | 757 | return -ENODEV; |
| 758 | } |
| 759 | |
Rafael J. Wysocki | f35cec2 | 2014-07-23 01:00:53 +0200 | [diff] [blame] | 760 | error = acpi_device_wakeup(adev, acpi_target_system_state(), enable); |
Rafael J. Wysocki | a6ae759 | 2012-11-02 01:40:53 +0100 | [diff] [blame] | 761 | if (!error) |
| 762 | dev_info(dev, "System wakeup %s by ACPI\n", |
| 763 | enable ? "enabled" : "disabled"); |
| 764 | |
| 765 | return error; |
| 766 | } |
Rafael J. Wysocki | dee8370 | 2012-11-02 01:40:36 +0100 | [diff] [blame] | 767 | #endif /* CONFIG_PM_SLEEP */ |
Rafael J. Wysocki | e5cc8ef | 2012-11-02 01:41:01 +0100 | [diff] [blame] | 768 | |
| 769 | /** |
Rafael J. Wysocki | e5cc8ef | 2012-11-02 01:41:01 +0100 | [diff] [blame] | 770 | * acpi_dev_pm_low_power - Put ACPI device into a low-power state. |
| 771 | * @dev: Device to put into a low-power state. |
| 772 | * @adev: ACPI device node corresponding to @dev. |
| 773 | * @system_state: System state to choose the device state for. |
| 774 | */ |
| 775 | static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev, |
| 776 | u32 system_state) |
| 777 | { |
Rafael J. Wysocki | fa1675b | 2013-06-16 00:37:59 +0200 | [diff] [blame] | 778 | int ret, state; |
Rafael J. Wysocki | e5cc8ef | 2012-11-02 01:41:01 +0100 | [diff] [blame] | 779 | |
| 780 | if (!acpi_device_power_manageable(adev)) |
| 781 | return 0; |
| 782 | |
Rafael J. Wysocki | fa1675b | 2013-06-16 00:37:59 +0200 | [diff] [blame] | 783 | ret = acpi_dev_pm_get_state(dev, adev, system_state, NULL, &state); |
| 784 | return ret ? ret : acpi_device_set_power(adev, state); |
Rafael J. Wysocki | e5cc8ef | 2012-11-02 01:41:01 +0100 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | /** |
| 788 | * acpi_dev_pm_full_power - Put ACPI device into the full-power state. |
| 789 | * @adev: ACPI device node to put into the full-power state. |
| 790 | */ |
| 791 | static int acpi_dev_pm_full_power(struct acpi_device *adev) |
| 792 | { |
| 793 | return acpi_device_power_manageable(adev) ? |
| 794 | acpi_device_set_power(adev, ACPI_STATE_D0) : 0; |
| 795 | } |
| 796 | |
Rafael J. Wysocki | e5cc8ef | 2012-11-02 01:41:01 +0100 | [diff] [blame] | 797 | /** |
| 798 | * acpi_dev_runtime_suspend - Put device into a low-power state using ACPI. |
| 799 | * @dev: Device to put into a low-power state. |
| 800 | * |
| 801 | * Put the given device into a runtime low-power state using the standard ACPI |
| 802 | * mechanism. Set up remote wakeup if desired, choose the state to put the |
| 803 | * device into (this checks if remote wakeup is expected to work too), and set |
| 804 | * the power state of the device. |
| 805 | */ |
| 806 | int acpi_dev_runtime_suspend(struct device *dev) |
| 807 | { |
Rafael J. Wysocki | 79c0373 | 2014-01-27 23:10:24 +0100 | [diff] [blame] | 808 | struct acpi_device *adev = ACPI_COMPANION(dev); |
Rafael J. Wysocki | e5cc8ef | 2012-11-02 01:41:01 +0100 | [diff] [blame] | 809 | bool remote_wakeup; |
| 810 | int error; |
| 811 | |
| 812 | if (!adev) |
| 813 | return 0; |
| 814 | |
| 815 | remote_wakeup = dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) > |
| 816 | PM_QOS_FLAGS_NONE; |
Rafael J. Wysocki | f35cec2 | 2014-07-23 01:00:53 +0200 | [diff] [blame] | 817 | error = acpi_device_wakeup(adev, ACPI_STATE_S0, remote_wakeup); |
Rafael J. Wysocki | e5cc8ef | 2012-11-02 01:41:01 +0100 | [diff] [blame] | 818 | if (remote_wakeup && error) |
| 819 | return -EAGAIN; |
| 820 | |
| 821 | error = acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0); |
| 822 | if (error) |
Rafael J. Wysocki | f35cec2 | 2014-07-23 01:00:53 +0200 | [diff] [blame] | 823 | acpi_device_wakeup(adev, ACPI_STATE_S0, false); |
Rafael J. Wysocki | e5cc8ef | 2012-11-02 01:41:01 +0100 | [diff] [blame] | 824 | |
| 825 | return error; |
| 826 | } |
| 827 | EXPORT_SYMBOL_GPL(acpi_dev_runtime_suspend); |
| 828 | |
| 829 | /** |
| 830 | * acpi_dev_runtime_resume - Put device into the full-power state using ACPI. |
| 831 | * @dev: Device to put into the full-power state. |
| 832 | * |
| 833 | * Put the given device into the full-power state using the standard ACPI |
| 834 | * mechanism at run time. Set the power state of the device to ACPI D0 and |
| 835 | * disable remote wakeup. |
| 836 | */ |
| 837 | int acpi_dev_runtime_resume(struct device *dev) |
| 838 | { |
Rafael J. Wysocki | 79c0373 | 2014-01-27 23:10:24 +0100 | [diff] [blame] | 839 | struct acpi_device *adev = ACPI_COMPANION(dev); |
Rafael J. Wysocki | e5cc8ef | 2012-11-02 01:41:01 +0100 | [diff] [blame] | 840 | int error; |
| 841 | |
| 842 | if (!adev) |
| 843 | return 0; |
| 844 | |
| 845 | error = acpi_dev_pm_full_power(adev); |
Rafael J. Wysocki | f35cec2 | 2014-07-23 01:00:53 +0200 | [diff] [blame] | 846 | acpi_device_wakeup(adev, ACPI_STATE_S0, false); |
Rafael J. Wysocki | e5cc8ef | 2012-11-02 01:41:01 +0100 | [diff] [blame] | 847 | return error; |
| 848 | } |
| 849 | EXPORT_SYMBOL_GPL(acpi_dev_runtime_resume); |
| 850 | |
| 851 | /** |
| 852 | * acpi_subsys_runtime_suspend - Suspend device using ACPI. |
| 853 | * @dev: Device to suspend. |
| 854 | * |
| 855 | * Carry out the generic runtime suspend procedure for @dev and use ACPI to put |
| 856 | * it into a runtime low-power state. |
| 857 | */ |
| 858 | int acpi_subsys_runtime_suspend(struct device *dev) |
| 859 | { |
| 860 | int ret = pm_generic_runtime_suspend(dev); |
| 861 | return ret ? ret : acpi_dev_runtime_suspend(dev); |
| 862 | } |
| 863 | EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend); |
| 864 | |
| 865 | /** |
| 866 | * acpi_subsys_runtime_resume - Resume device using ACPI. |
| 867 | * @dev: Device to Resume. |
| 868 | * |
| 869 | * Use ACPI to put the given device into the full-power state and carry out the |
| 870 | * generic runtime resume procedure for it. |
| 871 | */ |
| 872 | int acpi_subsys_runtime_resume(struct device *dev) |
| 873 | { |
| 874 | int ret = acpi_dev_runtime_resume(dev); |
| 875 | return ret ? ret : pm_generic_runtime_resume(dev); |
| 876 | } |
| 877 | EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume); |
Rafael J. Wysocki | e5cc8ef | 2012-11-02 01:41:01 +0100 | [diff] [blame] | 878 | |
| 879 | #ifdef CONFIG_PM_SLEEP |
| 880 | /** |
| 881 | * acpi_dev_suspend_late - Put device into a low-power state using ACPI. |
| 882 | * @dev: Device to put into a low-power state. |
| 883 | * |
| 884 | * Put the given device into a low-power state during system transition to a |
| 885 | * sleep state using the standard ACPI mechanism. Set up system wakeup if |
| 886 | * desired, choose the state to put the device into (this checks if system |
| 887 | * wakeup is expected to work too), and set the power state of the device. |
| 888 | */ |
| 889 | int acpi_dev_suspend_late(struct device *dev) |
| 890 | { |
Rafael J. Wysocki | 79c0373 | 2014-01-27 23:10:24 +0100 | [diff] [blame] | 891 | struct acpi_device *adev = ACPI_COMPANION(dev); |
Rafael J. Wysocki | e5cc8ef | 2012-11-02 01:41:01 +0100 | [diff] [blame] | 892 | u32 target_state; |
| 893 | bool wakeup; |
| 894 | int error; |
| 895 | |
| 896 | if (!adev) |
| 897 | return 0; |
| 898 | |
| 899 | target_state = acpi_target_system_state(); |
Rafael J. Wysocki | 78579b7 | 2014-11-19 01:44:11 +0100 | [diff] [blame] | 900 | wakeup = device_may_wakeup(dev) && acpi_device_can_wakeup(adev); |
Rafael J. Wysocki | f35cec2 | 2014-07-23 01:00:53 +0200 | [diff] [blame] | 901 | error = acpi_device_wakeup(adev, target_state, wakeup); |
Rafael J. Wysocki | e5cc8ef | 2012-11-02 01:41:01 +0100 | [diff] [blame] | 902 | if (wakeup && error) |
| 903 | return error; |
| 904 | |
| 905 | error = acpi_dev_pm_low_power(dev, adev, target_state); |
| 906 | if (error) |
Rafael J. Wysocki | f35cec2 | 2014-07-23 01:00:53 +0200 | [diff] [blame] | 907 | acpi_device_wakeup(adev, ACPI_STATE_UNKNOWN, false); |
Rafael J. Wysocki | e5cc8ef | 2012-11-02 01:41:01 +0100 | [diff] [blame] | 908 | |
| 909 | return error; |
| 910 | } |
| 911 | EXPORT_SYMBOL_GPL(acpi_dev_suspend_late); |
| 912 | |
| 913 | /** |
| 914 | * acpi_dev_resume_early - Put device into the full-power state using ACPI. |
| 915 | * @dev: Device to put into the full-power state. |
| 916 | * |
| 917 | * Put the given device into the full-power state using the standard ACPI |
| 918 | * mechanism during system transition to the working state. Set the power |
| 919 | * state of the device to ACPI D0 and disable remote wakeup. |
| 920 | */ |
| 921 | int acpi_dev_resume_early(struct device *dev) |
| 922 | { |
Rafael J. Wysocki | 79c0373 | 2014-01-27 23:10:24 +0100 | [diff] [blame] | 923 | struct acpi_device *adev = ACPI_COMPANION(dev); |
Rafael J. Wysocki | e5cc8ef | 2012-11-02 01:41:01 +0100 | [diff] [blame] | 924 | int error; |
| 925 | |
| 926 | if (!adev) |
| 927 | return 0; |
| 928 | |
| 929 | error = acpi_dev_pm_full_power(adev); |
Rafael J. Wysocki | f35cec2 | 2014-07-23 01:00:53 +0200 | [diff] [blame] | 930 | acpi_device_wakeup(adev, ACPI_STATE_UNKNOWN, false); |
Rafael J. Wysocki | e5cc8ef | 2012-11-02 01:41:01 +0100 | [diff] [blame] | 931 | return error; |
| 932 | } |
| 933 | EXPORT_SYMBOL_GPL(acpi_dev_resume_early); |
| 934 | |
| 935 | /** |
| 936 | * acpi_subsys_prepare - Prepare device for system transition to a sleep state. |
| 937 | * @dev: Device to prepare. |
| 938 | */ |
| 939 | int acpi_subsys_prepare(struct device *dev) |
| 940 | { |
Rafael J. Wysocki | f25c0ae | 2014-05-17 00:18:13 +0200 | [diff] [blame] | 941 | struct acpi_device *adev = ACPI_COMPANION(dev); |
| 942 | u32 sys_target; |
| 943 | int ret, state; |
Rafael J. Wysocki | 92858c4 | 2014-02-26 01:00:19 +0100 | [diff] [blame] | 944 | |
Rafael J. Wysocki | f25c0ae | 2014-05-17 00:18:13 +0200 | [diff] [blame] | 945 | ret = pm_generic_prepare(dev); |
| 946 | if (ret < 0) |
| 947 | return ret; |
| 948 | |
| 949 | if (!adev || !pm_runtime_suspended(dev) |
| 950 | || device_may_wakeup(dev) != !!adev->wakeup.prepare_count) |
| 951 | return 0; |
| 952 | |
| 953 | sys_target = acpi_target_system_state(); |
| 954 | if (sys_target == ACPI_STATE_S0) |
| 955 | return 1; |
| 956 | |
| 957 | if (adev->power.flags.dsw_present) |
| 958 | return 0; |
| 959 | |
| 960 | ret = acpi_dev_pm_get_state(dev, adev, sys_target, NULL, &state); |
| 961 | return !ret && state == adev->power.state; |
Rafael J. Wysocki | e5cc8ef | 2012-11-02 01:41:01 +0100 | [diff] [blame] | 962 | } |
| 963 | EXPORT_SYMBOL_GPL(acpi_subsys_prepare); |
| 964 | |
| 965 | /** |
Rafael J. Wysocki | 92858c4 | 2014-02-26 01:00:19 +0100 | [diff] [blame] | 966 | * acpi_subsys_suspend - Run the device driver's suspend callback. |
| 967 | * @dev: Device to handle. |
| 968 | * |
| 969 | * Follow PCI and resume devices suspended at run time before running their |
| 970 | * system suspend callbacks. |
| 971 | */ |
| 972 | int acpi_subsys_suspend(struct device *dev) |
| 973 | { |
| 974 | pm_runtime_resume(dev); |
| 975 | return pm_generic_suspend(dev); |
| 976 | } |
Heikki Krogerus | 4cf563c | 2014-05-15 16:40:23 +0300 | [diff] [blame] | 977 | EXPORT_SYMBOL_GPL(acpi_subsys_suspend); |
Rafael J. Wysocki | 92858c4 | 2014-02-26 01:00:19 +0100 | [diff] [blame] | 978 | |
| 979 | /** |
Rafael J. Wysocki | e5cc8ef | 2012-11-02 01:41:01 +0100 | [diff] [blame] | 980 | * acpi_subsys_suspend_late - Suspend device using ACPI. |
| 981 | * @dev: Device to suspend. |
| 982 | * |
| 983 | * Carry out the generic late suspend procedure for @dev and use ACPI to put |
| 984 | * it into a low-power state during system transition into a sleep state. |
| 985 | */ |
| 986 | int acpi_subsys_suspend_late(struct device *dev) |
| 987 | { |
| 988 | int ret = pm_generic_suspend_late(dev); |
| 989 | return ret ? ret : acpi_dev_suspend_late(dev); |
| 990 | } |
| 991 | EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late); |
| 992 | |
| 993 | /** |
| 994 | * acpi_subsys_resume_early - Resume device using ACPI. |
| 995 | * @dev: Device to Resume. |
| 996 | * |
| 997 | * Use ACPI to put the given device into the full-power state and carry out the |
| 998 | * generic early resume procedure for it during system transition into the |
| 999 | * working state. |
| 1000 | */ |
| 1001 | int acpi_subsys_resume_early(struct device *dev) |
| 1002 | { |
| 1003 | int ret = acpi_dev_resume_early(dev); |
| 1004 | return ret ? ret : pm_generic_resume_early(dev); |
| 1005 | } |
| 1006 | EXPORT_SYMBOL_GPL(acpi_subsys_resume_early); |
Rafael J. Wysocki | 92858c4 | 2014-02-26 01:00:19 +0100 | [diff] [blame] | 1007 | |
| 1008 | /** |
| 1009 | * acpi_subsys_freeze - Run the device driver's freeze callback. |
| 1010 | * @dev: Device to handle. |
| 1011 | */ |
| 1012 | int acpi_subsys_freeze(struct device *dev) |
| 1013 | { |
| 1014 | /* |
| 1015 | * This used to be done in acpi_subsys_prepare() for all devices and |
| 1016 | * some drivers may depend on it, so do it here. Ideally, however, |
| 1017 | * runtime-suspended devices should not be touched during freeze/thaw |
| 1018 | * transitions. |
| 1019 | */ |
| 1020 | pm_runtime_resume(dev); |
| 1021 | return pm_generic_freeze(dev); |
| 1022 | } |
Heikki Krogerus | 4cf563c | 2014-05-15 16:40:23 +0300 | [diff] [blame] | 1023 | EXPORT_SYMBOL_GPL(acpi_subsys_freeze); |
Rafael J. Wysocki | 92858c4 | 2014-02-26 01:00:19 +0100 | [diff] [blame] | 1024 | |
Rafael J. Wysocki | e5cc8ef | 2012-11-02 01:41:01 +0100 | [diff] [blame] | 1025 | #endif /* CONFIG_PM_SLEEP */ |
| 1026 | |
| 1027 | static struct dev_pm_domain acpi_general_pm_domain = { |
| 1028 | .ops = { |
Rafael J. Wysocki | e5cc8ef | 2012-11-02 01:41:01 +0100 | [diff] [blame] | 1029 | .runtime_suspend = acpi_subsys_runtime_suspend, |
| 1030 | .runtime_resume = acpi_subsys_runtime_resume, |
Rafael J. Wysocki | e5cc8ef | 2012-11-02 01:41:01 +0100 | [diff] [blame] | 1031 | #ifdef CONFIG_PM_SLEEP |
| 1032 | .prepare = acpi_subsys_prepare, |
Rafael J. Wysocki | 58a1fbb | 2015-10-07 00:50:24 +0200 | [diff] [blame] | 1033 | .complete = pm_complete_with_resume_check, |
Rafael J. Wysocki | 92858c4 | 2014-02-26 01:00:19 +0100 | [diff] [blame] | 1034 | .suspend = acpi_subsys_suspend, |
Rafael J. Wysocki | e5cc8ef | 2012-11-02 01:41:01 +0100 | [diff] [blame] | 1035 | .suspend_late = acpi_subsys_suspend_late, |
| 1036 | .resume_early = acpi_subsys_resume_early, |
Rafael J. Wysocki | 92858c4 | 2014-02-26 01:00:19 +0100 | [diff] [blame] | 1037 | .freeze = acpi_subsys_freeze, |
| 1038 | .poweroff = acpi_subsys_suspend, |
Rafael J. Wysocki | e5cc8ef | 2012-11-02 01:41:01 +0100 | [diff] [blame] | 1039 | .poweroff_late = acpi_subsys_suspend_late, |
| 1040 | .restore_early = acpi_subsys_resume_early, |
| 1041 | #endif |
| 1042 | }, |
| 1043 | }; |
| 1044 | |
| 1045 | /** |
Ulf Hansson | 91d66cd | 2014-09-19 20:27:44 +0200 | [diff] [blame] | 1046 | * acpi_dev_pm_detach - Remove ACPI power management from the device. |
| 1047 | * @dev: Device to take care of. |
| 1048 | * @power_off: Whether or not to try to remove power from the device. |
| 1049 | * |
| 1050 | * Remove the device from the general ACPI PM domain and remove its wakeup |
| 1051 | * notifier. If @power_off is set, additionally remove power from the device if |
| 1052 | * possible. |
| 1053 | * |
| 1054 | * Callers must ensure proper synchronization of this function with power |
| 1055 | * management callbacks. |
| 1056 | */ |
| 1057 | static void acpi_dev_pm_detach(struct device *dev, bool power_off) |
| 1058 | { |
| 1059 | struct acpi_device *adev = ACPI_COMPANION(dev); |
| 1060 | |
| 1061 | if (adev && dev->pm_domain == &acpi_general_pm_domain) { |
| 1062 | dev->pm_domain = NULL; |
| 1063 | acpi_remove_pm_notifier(adev); |
| 1064 | if (power_off) { |
| 1065 | /* |
| 1066 | * If the device's PM QoS resume latency limit or flags |
| 1067 | * have been exposed to user space, they have to be |
| 1068 | * hidden at this point, so that they don't affect the |
| 1069 | * choice of the low-power state to put the device into. |
| 1070 | */ |
| 1071 | dev_pm_qos_hide_latency_limit(dev); |
| 1072 | dev_pm_qos_hide_flags(dev); |
| 1073 | acpi_device_wakeup(adev, ACPI_STATE_S0, false); |
| 1074 | acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0); |
| 1075 | } |
| 1076 | } |
| 1077 | } |
| 1078 | |
| 1079 | /** |
Rafael J. Wysocki | e5cc8ef | 2012-11-02 01:41:01 +0100 | [diff] [blame] | 1080 | * acpi_dev_pm_attach - Prepare device for ACPI power management. |
| 1081 | * @dev: Device to prepare. |
Rafael J. Wysocki | b88ce2a | 2012-11-26 10:03:06 +0100 | [diff] [blame] | 1082 | * @power_on: Whether or not to power on the device. |
Rafael J. Wysocki | e5cc8ef | 2012-11-02 01:41:01 +0100 | [diff] [blame] | 1083 | * |
| 1084 | * If @dev has a valid ACPI handle that has a valid struct acpi_device object |
| 1085 | * attached to it, install a wakeup notification handler for the device and |
Rafael J. Wysocki | b88ce2a | 2012-11-26 10:03:06 +0100 | [diff] [blame] | 1086 | * add it to the general ACPI PM domain. If @power_on is set, the device will |
| 1087 | * be put into the ACPI D0 state before the function returns. |
Rafael J. Wysocki | e5cc8ef | 2012-11-02 01:41:01 +0100 | [diff] [blame] | 1088 | * |
| 1089 | * This assumes that the @dev's bus type uses generic power management callbacks |
| 1090 | * (or doesn't use any power management callbacks at all). |
| 1091 | * |
| 1092 | * Callers must ensure proper synchronization of this function with power |
| 1093 | * management callbacks. |
| 1094 | */ |
Rafael J. Wysocki | b88ce2a | 2012-11-26 10:03:06 +0100 | [diff] [blame] | 1095 | int acpi_dev_pm_attach(struct device *dev, bool power_on) |
Rafael J. Wysocki | e5cc8ef | 2012-11-02 01:41:01 +0100 | [diff] [blame] | 1096 | { |
Rafael J. Wysocki | 79c0373 | 2014-01-27 23:10:24 +0100 | [diff] [blame] | 1097 | struct acpi_device *adev = ACPI_COMPANION(dev); |
Rafael J. Wysocki | e5cc8ef | 2012-11-02 01:41:01 +0100 | [diff] [blame] | 1098 | |
| 1099 | if (!adev) |
| 1100 | return -ENODEV; |
| 1101 | |
| 1102 | if (dev->pm_domain) |
| 1103 | return -EEXIST; |
| 1104 | |
Mika Westerberg | 712e960 | 2015-07-27 18:03:57 +0300 | [diff] [blame] | 1105 | /* |
| 1106 | * Only attach the power domain to the first device if the |
| 1107 | * companion is shared by multiple. This is to prevent doing power |
| 1108 | * management twice. |
| 1109 | */ |
| 1110 | if (!acpi_device_is_first_physical_node(adev, dev)) |
| 1111 | return -EBUSY; |
| 1112 | |
Rafael J. Wysocki | c072530 | 2014-07-23 01:00:45 +0200 | [diff] [blame] | 1113 | acpi_add_pm_notifier(adev, dev, acpi_pm_notify_work_func); |
Rafael J. Wysocki | e5cc8ef | 2012-11-02 01:41:01 +0100 | [diff] [blame] | 1114 | dev->pm_domain = &acpi_general_pm_domain; |
Rafael J. Wysocki | b88ce2a | 2012-11-26 10:03:06 +0100 | [diff] [blame] | 1115 | if (power_on) { |
| 1116 | acpi_dev_pm_full_power(adev); |
Rafael J. Wysocki | f35cec2 | 2014-07-23 01:00:53 +0200 | [diff] [blame] | 1117 | acpi_device_wakeup(adev, ACPI_STATE_S0, false); |
Rafael J. Wysocki | b88ce2a | 2012-11-26 10:03:06 +0100 | [diff] [blame] | 1118 | } |
Ulf Hansson | 86f1e15 | 2014-09-19 20:27:35 +0200 | [diff] [blame] | 1119 | |
| 1120 | dev->pm_domain->detach = acpi_dev_pm_detach; |
Rafael J. Wysocki | e5cc8ef | 2012-11-02 01:41:01 +0100 | [diff] [blame] | 1121 | return 0; |
| 1122 | } |
| 1123 | EXPORT_SYMBOL_GPL(acpi_dev_pm_attach); |
Rafael J. Wysocki | ec4602a | 2013-05-16 22:29:28 +0200 | [diff] [blame] | 1124 | #endif /* CONFIG_PM */ |