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> 26 #include <linux/export.h> 27 #include <linux/mutex.h> 28 #include <linux/pm_qos.h> 29 #include <linux/pm_runtime.h> 30 31 #include <acpi/acpi.h> 32 #include <acpi/acpi_bus.h> 33 #include <acpi/acpi_drivers.h> 34 35 #include "internal.h" 36 37 #define _COMPONENT ACPI_POWER_COMPONENT 38 ACPI_MODULE_NAME("device_pm"); 39 40 static DEFINE_MUTEX(acpi_pm_notifier_lock); 41 42 /** 43 * acpi_add_pm_notifier - Register PM notifier for given ACPI device. 44 * @adev: ACPI device to add the notifier for. 45 * @context: Context information to pass to the notifier routine. 46 * 47 * NOTE: @adev need not be a run-wake or wakeup device to be a valid source of 48 * PM wakeup events. For example, wakeup events may be generated for bridges 49 * if one of the devices below the bridge is signaling wakeup, even if the 50 * bridge itself doesn't have a wakeup GPE associated with it. 51 */ 52 acpi_status acpi_add_pm_notifier(struct acpi_device *adev, 53 acpi_notify_handler handler, void *context) 54 { 55 acpi_status status = AE_ALREADY_EXISTS; 56 57 mutex_lock(&acpi_pm_notifier_lock); 58 59 if (adev->wakeup.flags.notifier_present) 60 goto out; 61 62 status = acpi_install_notify_handler(adev->handle, 63 ACPI_SYSTEM_NOTIFY, 64 handler, context); 65 if (ACPI_FAILURE(status)) 66 goto out; 67 68 adev->wakeup.flags.notifier_present = true; 69 70 out: 71 mutex_unlock(&acpi_pm_notifier_lock); 72 return status; 73 } 74 75 /** 76 * acpi_remove_pm_notifier - Unregister PM notifier from given ACPI device. 77 * @adev: ACPI device to remove the notifier from. 78 */ 79 acpi_status acpi_remove_pm_notifier(struct acpi_device *adev, 80 acpi_notify_handler handler) 81 { 82 acpi_status status = AE_BAD_PARAMETER; 83 84 mutex_lock(&acpi_pm_notifier_lock); 85 86 if (!adev->wakeup.flags.notifier_present) 87 goto out; 88 89 status = acpi_remove_notify_handler(adev->handle, 90 ACPI_SYSTEM_NOTIFY, 91 handler); 92 if (ACPI_FAILURE(status)) 93 goto out; 94 95 adev->wakeup.flags.notifier_present = false; 96 97 out: 98 mutex_unlock(&acpi_pm_notifier_lock); 99 return status; 100 } 101 102 /** 103 * acpi_power_state_string - String representation of ACPI device power state. 104 * @state: ACPI device power state to return the string representation of. 105 */ 106 const char *acpi_power_state_string(int state) 107 { 108 switch (state) { 109 case ACPI_STATE_D0: 110 return "D0"; 111 case ACPI_STATE_D1: 112 return "D1"; 113 case ACPI_STATE_D2: 114 return "D2"; 115 case ACPI_STATE_D3_HOT: 116 return "D3hot"; 117 case ACPI_STATE_D3_COLD: 118 return "D3cold"; 119 default: 120 return "(unknown)"; 121 } 122 } 123 124 /** 125 * acpi_device_get_power - Get power state of an ACPI device. 126 * @device: Device to get the power state of. 127 * @state: Place to store the power state of the device. 128 * 129 * This function does not update the device's power.state field, but it may 130 * update its parent's power.state field (when the parent's power state is 131 * unknown and the device's power state turns out to be D0). 132 */ 133 int acpi_device_get_power(struct acpi_device *device, int *state) 134 { 135 int result = ACPI_STATE_UNKNOWN; 136 137 if (!device || !state) 138 return -EINVAL; 139 140 if (!device->flags.power_manageable) { 141 /* TBD: Non-recursive algorithm for walking up hierarchy. */ 142 *state = device->parent ? 143 device->parent->power.state : ACPI_STATE_D0; 144 goto out; 145 } 146 147 /* 148 * Get the device's power state from power resources settings and _PSC, 149 * if available. 150 */ 151 if (device->power.flags.power_resources) { 152 int error = acpi_power_get_inferred_state(device, &result); 153 if (error) 154 return error; 155 } 156 if (device->power.flags.explicit_get) { 157 acpi_handle handle = device->handle; 158 unsigned long long psc; 159 acpi_status status; 160 161 status = acpi_evaluate_integer(handle, "_PSC", NULL, &psc); 162 if (ACPI_FAILURE(status)) 163 return -ENODEV; 164 165 /* 166 * The power resources settings may indicate a power state 167 * shallower than the actual power state of the device. 168 * 169 * Moreover, on systems predating ACPI 4.0, if the device 170 * doesn't depend on any power resources and _PSC returns 3, 171 * that means "power off". We need to maintain compatibility 172 * with those systems. 173 */ 174 if (psc > result && psc < ACPI_STATE_D3_COLD) 175 result = psc; 176 else if (result == ACPI_STATE_UNKNOWN) 177 result = psc > ACPI_STATE_D2 ? ACPI_STATE_D3_COLD : psc; 178 } 179 180 /* 181 * If we were unsure about the device parent's power state up to this 182 * point, the fact that the device is in D0 implies that the parent has 183 * to be in D0 too. 184 */ 185 if (device->parent && device->parent->power.state == ACPI_STATE_UNKNOWN 186 && result == ACPI_STATE_D0) 187 device->parent->power.state = ACPI_STATE_D0; 188 189 *state = result; 190 191 out: 192 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] power state is %s\n", 193 device->pnp.bus_id, acpi_power_state_string(*state))); 194 195 return 0; 196 } 197 198 static int acpi_dev_pm_explicit_set(struct acpi_device *adev, int state) 199 { 200 if (adev->power.states[state].flags.explicit_set) { 201 char method[5] = { '_', 'P', 'S', '0' + state, '\0' }; 202 acpi_status status; 203 204 status = acpi_evaluate_object(adev->handle, method, NULL, NULL); 205 if (ACPI_FAILURE(status)) 206 return -ENODEV; 207 } 208 return 0; 209 } 210 211 /** 212 * acpi_device_set_power - Set power state of an ACPI device. 213 * @device: Device to set the power state of. 214 * @state: New power state to set. 215 * 216 * Callers must ensure that the device is power manageable before using this 217 * function. 218 */ 219 int acpi_device_set_power(struct acpi_device *device, int state) 220 { 221 int result = 0; 222 bool cut_power = false; 223 224 if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD)) 225 return -EINVAL; 226 227 /* Make sure this is a valid target state */ 228 229 if (state == device->power.state) { 230 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device is already at %s\n", 231 acpi_power_state_string(state))); 232 return 0; 233 } 234 235 if (!device->power.states[state].flags.valid) { 236 printk(KERN_WARNING PREFIX "Device does not support %s\n", 237 acpi_power_state_string(state)); 238 return -ENODEV; 239 } 240 if (device->parent && (state < device->parent->power.state)) { 241 printk(KERN_WARNING PREFIX 242 "Cannot set device to a higher-powered" 243 " state than parent\n"); 244 return -ENODEV; 245 } 246 247 /* For D3cold we should first transition into D3hot. */ 248 if (state == ACPI_STATE_D3_COLD 249 && device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible) { 250 state = ACPI_STATE_D3_HOT; 251 cut_power = true; 252 } 253 254 if (state < device->power.state && state != ACPI_STATE_D0 255 && device->power.state >= ACPI_STATE_D3_HOT) { 256 printk(KERN_WARNING PREFIX 257 "Cannot transition to non-D0 state from D3\n"); 258 return -ENODEV; 259 } 260 261 /* 262 * Transition Power 263 * ---------------- 264 * In accordance with the ACPI specification first apply power (via 265 * power resources) and then evalute _PSx. 266 */ 267 if (device->power.flags.power_resources) { 268 result = acpi_power_transition(device, state); 269 if (result) 270 goto end; 271 } 272 result = acpi_dev_pm_explicit_set(device, state); 273 if (result) 274 goto end; 275 276 if (cut_power) { 277 device->power.state = state; 278 state = ACPI_STATE_D3_COLD; 279 result = acpi_power_transition(device, state); 280 } 281 282 end: 283 if (result) { 284 printk(KERN_WARNING PREFIX 285 "Device [%s] failed to transition to %s\n", 286 device->pnp.bus_id, 287 acpi_power_state_string(state)); 288 } else { 289 device->power.state = state; 290 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 291 "Device [%s] transitioned to %s\n", 292 device->pnp.bus_id, 293 acpi_power_state_string(state))); 294 } 295 296 return result; 297 } 298 EXPORT_SYMBOL(acpi_device_set_power); 299 300 int acpi_bus_set_power(acpi_handle handle, int state) 301 { 302 struct acpi_device *device; 303 int result; 304 305 result = acpi_bus_get_device(handle, &device); 306 if (result) 307 return result; 308 309 if (!device->flags.power_manageable) { 310 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 311 "Device [%s] is not power manageable\n", 312 dev_name(&device->dev))); 313 return -ENODEV; 314 } 315 316 return acpi_device_set_power(device, state); 317 } 318 EXPORT_SYMBOL(acpi_bus_set_power); 319 320 int acpi_bus_init_power(struct acpi_device *device) 321 { 322 int state; 323 int result; 324 325 if (!device) 326 return -EINVAL; 327 328 device->power.state = ACPI_STATE_UNKNOWN; 329 330 result = acpi_device_get_power(device, &state); 331 if (result) 332 return result; 333 334 if (state < ACPI_STATE_D3_COLD && device->power.flags.power_resources) { 335 result = acpi_power_on_resources(device, state); 336 if (result) 337 return result; 338 339 result = acpi_dev_pm_explicit_set(device, state); 340 if (result) 341 return result; 342 } else if (state == ACPI_STATE_UNKNOWN) { 343 /* No power resources and missing _PSC? Try to force D0. */ 344 state = ACPI_STATE_D0; 345 result = acpi_dev_pm_explicit_set(device, state); 346 if (result) 347 return result; 348 } 349 device->power.state = state; 350 return 0; 351 } 352 353 int acpi_bus_update_power(acpi_handle handle, int *state_p) 354 { 355 struct acpi_device *device; 356 int state; 357 int result; 358 359 result = acpi_bus_get_device(handle, &device); 360 if (result) 361 return result; 362 363 result = acpi_device_get_power(device, &state); 364 if (result) 365 return result; 366 367 if (state == ACPI_STATE_UNKNOWN) 368 state = ACPI_STATE_D0; 369 370 result = acpi_device_set_power(device, state); 371 if (!result && state_p) 372 *state_p = state; 373 374 return result; 375 } 376 EXPORT_SYMBOL_GPL(acpi_bus_update_power); 377 378 bool acpi_bus_power_manageable(acpi_handle handle) 379 { 380 struct acpi_device *device; 381 int result; 382 383 result = acpi_bus_get_device(handle, &device); 384 return result ? false : device->flags.power_manageable; 385 } 386 EXPORT_SYMBOL(acpi_bus_power_manageable); 387 388 bool acpi_bus_can_wakeup(acpi_handle handle) 389 { 390 struct acpi_device *device; 391 int result; 392 393 result = acpi_bus_get_device(handle, &device); 394 return result ? false : device->wakeup.flags.valid; 395 } 396 EXPORT_SYMBOL(acpi_bus_can_wakeup); 397 398 /** 399 * acpi_device_power_state - Get preferred power state of ACPI device. 400 * @dev: Device whose preferred target power state to return. 401 * @adev: ACPI device node corresponding to @dev. 402 * @target_state: System state to match the resultant device state. 403 * @d_max_in: Deepest low-power state to take into consideration. 404 * @d_min_p: Location to store the upper limit of the allowed states range. 405 * Return value: Preferred power state of the device on success, -ENODEV 406 * (if there's no 'struct acpi_device' for @dev) or -EINVAL on failure 407 * 408 * Find the lowest power (highest number) ACPI device power state that the 409 * device can be in while the system is in the state represented by 410 * @target_state. If @d_min_p is set, the highest power (lowest number) device 411 * power state that @dev can be in for the given system sleep state is stored 412 * at the location pointed to by it. 413 * 414 * Callers must ensure that @dev and @adev are valid pointers and that @adev 415 * actually corresponds to @dev before using this function. 416 */ 417 int acpi_device_power_state(struct device *dev, struct acpi_device *adev, 418 u32 target_state, int d_max_in, int *d_min_p) 419 { 420 char acpi_method[] = "_SxD"; 421 unsigned long long d_min, d_max; 422 bool wakeup = false; 423 424 if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3) 425 return -EINVAL; 426 427 if (d_max_in > ACPI_STATE_D3_HOT) { 428 enum pm_qos_flags_status stat; 429 430 stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF); 431 if (stat == PM_QOS_FLAGS_ALL) 432 d_max_in = ACPI_STATE_D3_HOT; 433 } 434 435 acpi_method[2] = '0' + target_state; 436 /* 437 * If the sleep state is S0, the lowest limit from ACPI is D3, 438 * but if the device has _S0W, we will use the value from _S0W 439 * as the lowest limit from ACPI. Finally, we will constrain 440 * the lowest limit with the specified one. 441 */ 442 d_min = ACPI_STATE_D0; 443 d_max = ACPI_STATE_D3; 444 445 /* 446 * If present, _SxD methods return the minimum D-state (highest power 447 * state) we can use for the corresponding S-states. Otherwise, the 448 * minimum D-state is D0 (ACPI 3.x). 449 * 450 * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer 451 * provided -- that's our fault recovery, we ignore retval. 452 */ 453 if (target_state > ACPI_STATE_S0) { 454 acpi_evaluate_integer(adev->handle, acpi_method, NULL, &d_min); 455 wakeup = device_may_wakeup(dev) && adev->wakeup.flags.valid 456 && adev->wakeup.sleep_state >= target_state; 457 } else if (dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) != 458 PM_QOS_FLAGS_NONE) { 459 wakeup = adev->wakeup.flags.valid; 460 } 461 462 /* 463 * If _PRW says we can wake up the system from the target sleep state, 464 * the D-state returned by _SxD is sufficient for that (we assume a 465 * wakeup-aware driver if wake is set). Still, if _SxW exists 466 * (ACPI 3.x), it should return the maximum (lowest power) D-state that 467 * can wake the system. _S0W may be valid, too. 468 */ 469 if (wakeup) { 470 acpi_status status; 471 472 acpi_method[3] = 'W'; 473 status = acpi_evaluate_integer(adev->handle, acpi_method, NULL, 474 &d_max); 475 if (ACPI_FAILURE(status)) { 476 if (target_state != ACPI_STATE_S0 || 477 status != AE_NOT_FOUND) 478 d_max = d_min; 479 } else if (d_max < d_min) { 480 /* Warn the user of the broken DSDT */ 481 printk(KERN_WARNING "ACPI: Wrong value from %s\n", 482 acpi_method); 483 /* Sanitize it */ 484 d_min = d_max; 485 } 486 } 487 488 if (d_max_in < d_min) 489 return -EINVAL; 490 if (d_min_p) 491 *d_min_p = d_min; 492 /* constrain d_max with specified lowest limit (max number) */ 493 if (d_max > d_max_in) { 494 for (d_max = d_max_in; d_max > d_min; d_max--) { 495 if (adev->power.states[d_max].flags.valid) 496 break; 497 } 498 } 499 return d_max; 500 } 501 EXPORT_SYMBOL_GPL(acpi_device_power_state); 502 503 /** 504 * acpi_pm_device_sleep_state - Get preferred power state of ACPI device. 505 * @dev: Device whose preferred target power state to return. 506 * @d_min_p: Location to store the upper limit of the allowed states range. 507 * @d_max_in: Deepest low-power state to take into consideration. 508 * Return value: Preferred power state of the device on success, -ENODEV 509 * (if there's no 'struct acpi_device' for @dev) or -EINVAL on failure 510 * 511 * The caller must ensure that @dev is valid before using this function. 512 */ 513 int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in) 514 { 515 acpi_handle handle = DEVICE_ACPI_HANDLE(dev); 516 struct acpi_device *adev; 517 518 if (!handle || acpi_bus_get_device(handle, &adev)) { 519 dev_dbg(dev, "ACPI handle without context in %s!\n", __func__); 520 return -ENODEV; 521 } 522 523 return acpi_device_power_state(dev, adev, acpi_target_system_state(), 524 d_max_in, d_min_p); 525 } 526 EXPORT_SYMBOL(acpi_pm_device_sleep_state); 527 528 #ifdef CONFIG_PM_RUNTIME 529 /** 530 * acpi_wakeup_device - Wakeup notification handler for ACPI devices. 531 * @handle: ACPI handle of the device the notification is for. 532 * @event: Type of the signaled event. 533 * @context: Device corresponding to @handle. 534 */ 535 static void acpi_wakeup_device(acpi_handle handle, u32 event, void *context) 536 { 537 struct device *dev = context; 538 539 if (event == ACPI_NOTIFY_DEVICE_WAKE && dev) { 540 pm_wakeup_event(dev, 0); 541 pm_runtime_resume(dev); 542 } 543 } 544 545 /** 546 * __acpi_device_run_wake - Enable/disable runtime remote wakeup for device. 547 * @adev: ACPI device to enable/disable the remote wakeup for. 548 * @enable: Whether to enable or disable the wakeup functionality. 549 * 550 * Enable/disable the GPE associated with @adev so that it can generate 551 * wakeup signals for the device in response to external (remote) events and 552 * enable/disable device wakeup power. 553 * 554 * Callers must ensure that @adev is a valid ACPI device node before executing 555 * this function. 556 */ 557 int __acpi_device_run_wake(struct acpi_device *adev, bool enable) 558 { 559 struct acpi_device_wakeup *wakeup = &adev->wakeup; 560 561 if (enable) { 562 acpi_status res; 563 int error; 564 565 error = acpi_enable_wakeup_device_power(adev, ACPI_STATE_S0); 566 if (error) 567 return error; 568 569 res = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number); 570 if (ACPI_FAILURE(res)) { 571 acpi_disable_wakeup_device_power(adev); 572 return -EIO; 573 } 574 } else { 575 acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number); 576 acpi_disable_wakeup_device_power(adev); 577 } 578 return 0; 579 } 580 581 /** 582 * acpi_pm_device_run_wake - Enable/disable remote wakeup for given device. 583 * @dev: Device to enable/disable the platform to wake up. 584 * @enable: Whether to enable or disable the wakeup functionality. 585 */ 586 int acpi_pm_device_run_wake(struct device *phys_dev, bool enable) 587 { 588 struct acpi_device *adev; 589 acpi_handle handle; 590 591 if (!device_run_wake(phys_dev)) 592 return -EINVAL; 593 594 handle = DEVICE_ACPI_HANDLE(phys_dev); 595 if (!handle || acpi_bus_get_device(handle, &adev)) { 596 dev_dbg(phys_dev, "ACPI handle without context in %s!\n", 597 __func__); 598 return -ENODEV; 599 } 600 601 return __acpi_device_run_wake(adev, enable); 602 } 603 EXPORT_SYMBOL(acpi_pm_device_run_wake); 604 #else 605 static inline void acpi_wakeup_device(acpi_handle handle, u32 event, 606 void *context) {} 607 #endif /* CONFIG_PM_RUNTIME */ 608 609 #ifdef CONFIG_PM_SLEEP 610 /** 611 * __acpi_device_sleep_wake - Enable or disable device to wake up the system. 612 * @dev: Device to enable/desible to wake up the system. 613 * @target_state: System state the device is supposed to wake up from. 614 * @enable: Whether to enable or disable @dev to wake up the system. 615 */ 616 int __acpi_device_sleep_wake(struct acpi_device *adev, u32 target_state, 617 bool enable) 618 { 619 return enable ? 620 acpi_enable_wakeup_device_power(adev, target_state) : 621 acpi_disable_wakeup_device_power(adev); 622 } 623 624 /** 625 * acpi_pm_device_sleep_wake - Enable or disable device to wake up the system. 626 * @dev: Device to enable/desible to wake up the system from sleep states. 627 * @enable: Whether to enable or disable @dev to wake up the system. 628 */ 629 int acpi_pm_device_sleep_wake(struct device *dev, bool enable) 630 { 631 acpi_handle handle; 632 struct acpi_device *adev; 633 int error; 634 635 if (!device_can_wakeup(dev)) 636 return -EINVAL; 637 638 handle = DEVICE_ACPI_HANDLE(dev); 639 if (!handle || acpi_bus_get_device(handle, &adev)) { 640 dev_dbg(dev, "ACPI handle without context in %s!\n", __func__); 641 return -ENODEV; 642 } 643 644 error = __acpi_device_sleep_wake(adev, acpi_target_system_state(), 645 enable); 646 if (!error) 647 dev_info(dev, "System wakeup %s by ACPI\n", 648 enable ? "enabled" : "disabled"); 649 650 return error; 651 } 652 #endif /* CONFIG_PM_SLEEP */ 653 654 /** 655 * acpi_dev_pm_get_node - Get ACPI device node for the given physical device. 656 * @dev: Device to get the ACPI node for. 657 */ 658 struct acpi_device *acpi_dev_pm_get_node(struct device *dev) 659 { 660 acpi_handle handle = DEVICE_ACPI_HANDLE(dev); 661 struct acpi_device *adev; 662 663 return handle && !acpi_bus_get_device(handle, &adev) ? adev : NULL; 664 } 665 666 /** 667 * acpi_dev_pm_low_power - Put ACPI device into a low-power state. 668 * @dev: Device to put into a low-power state. 669 * @adev: ACPI device node corresponding to @dev. 670 * @system_state: System state to choose the device state for. 671 */ 672 static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev, 673 u32 system_state) 674 { 675 int power_state; 676 677 if (!acpi_device_power_manageable(adev)) 678 return 0; 679 680 power_state = acpi_device_power_state(dev, adev, system_state, 681 ACPI_STATE_D3, NULL); 682 if (power_state < ACPI_STATE_D0 || power_state > ACPI_STATE_D3) 683 return -EIO; 684 685 return acpi_device_set_power(adev, power_state); 686 } 687 688 /** 689 * acpi_dev_pm_full_power - Put ACPI device into the full-power state. 690 * @adev: ACPI device node to put into the full-power state. 691 */ 692 static int acpi_dev_pm_full_power(struct acpi_device *adev) 693 { 694 return acpi_device_power_manageable(adev) ? 695 acpi_device_set_power(adev, ACPI_STATE_D0) : 0; 696 } 697 698 #ifdef CONFIG_PM_RUNTIME 699 /** 700 * acpi_dev_runtime_suspend - Put device into a low-power state using ACPI. 701 * @dev: Device to put into a low-power state. 702 * 703 * Put the given device into a runtime low-power state using the standard ACPI 704 * mechanism. Set up remote wakeup if desired, choose the state to put the 705 * device into (this checks if remote wakeup is expected to work too), and set 706 * the power state of the device. 707 */ 708 int acpi_dev_runtime_suspend(struct device *dev) 709 { 710 struct acpi_device *adev = acpi_dev_pm_get_node(dev); 711 bool remote_wakeup; 712 int error; 713 714 if (!adev) 715 return 0; 716 717 remote_wakeup = dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) > 718 PM_QOS_FLAGS_NONE; 719 error = __acpi_device_run_wake(adev, remote_wakeup); 720 if (remote_wakeup && error) 721 return -EAGAIN; 722 723 error = acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0); 724 if (error) 725 __acpi_device_run_wake(adev, false); 726 727 return error; 728 } 729 EXPORT_SYMBOL_GPL(acpi_dev_runtime_suspend); 730 731 /** 732 * acpi_dev_runtime_resume - Put device into the full-power state using ACPI. 733 * @dev: Device to put into the full-power state. 734 * 735 * Put the given device into the full-power state using the standard ACPI 736 * mechanism at run time. Set the power state of the device to ACPI D0 and 737 * disable remote wakeup. 738 */ 739 int acpi_dev_runtime_resume(struct device *dev) 740 { 741 struct acpi_device *adev = acpi_dev_pm_get_node(dev); 742 int error; 743 744 if (!adev) 745 return 0; 746 747 error = acpi_dev_pm_full_power(adev); 748 __acpi_device_run_wake(adev, false); 749 return error; 750 } 751 EXPORT_SYMBOL_GPL(acpi_dev_runtime_resume); 752 753 /** 754 * acpi_subsys_runtime_suspend - Suspend device using ACPI. 755 * @dev: Device to suspend. 756 * 757 * Carry out the generic runtime suspend procedure for @dev and use ACPI to put 758 * it into a runtime low-power state. 759 */ 760 int acpi_subsys_runtime_suspend(struct device *dev) 761 { 762 int ret = pm_generic_runtime_suspend(dev); 763 return ret ? ret : acpi_dev_runtime_suspend(dev); 764 } 765 EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend); 766 767 /** 768 * acpi_subsys_runtime_resume - Resume device using ACPI. 769 * @dev: Device to Resume. 770 * 771 * Use ACPI to put the given device into the full-power state and carry out the 772 * generic runtime resume procedure for it. 773 */ 774 int acpi_subsys_runtime_resume(struct device *dev) 775 { 776 int ret = acpi_dev_runtime_resume(dev); 777 return ret ? ret : pm_generic_runtime_resume(dev); 778 } 779 EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume); 780 #endif /* CONFIG_PM_RUNTIME */ 781 782 #ifdef CONFIG_PM_SLEEP 783 /** 784 * acpi_dev_suspend_late - Put device into a low-power state using ACPI. 785 * @dev: Device to put into a low-power state. 786 * 787 * Put the given device into a low-power state during system transition to a 788 * sleep state using the standard ACPI mechanism. Set up system wakeup if 789 * desired, choose the state to put the device into (this checks if system 790 * wakeup is expected to work too), and set the power state of the device. 791 */ 792 int acpi_dev_suspend_late(struct device *dev) 793 { 794 struct acpi_device *adev = acpi_dev_pm_get_node(dev); 795 u32 target_state; 796 bool wakeup; 797 int error; 798 799 if (!adev) 800 return 0; 801 802 target_state = acpi_target_system_state(); 803 wakeup = device_may_wakeup(dev); 804 error = __acpi_device_sleep_wake(adev, target_state, wakeup); 805 if (wakeup && error) 806 return error; 807 808 error = acpi_dev_pm_low_power(dev, adev, target_state); 809 if (error) 810 __acpi_device_sleep_wake(adev, ACPI_STATE_UNKNOWN, false); 811 812 return error; 813 } 814 EXPORT_SYMBOL_GPL(acpi_dev_suspend_late); 815 816 /** 817 * acpi_dev_resume_early - Put device into the full-power state using ACPI. 818 * @dev: Device to put into the full-power state. 819 * 820 * Put the given device into the full-power state using the standard ACPI 821 * mechanism during system transition to the working state. Set the power 822 * state of the device to ACPI D0 and disable remote wakeup. 823 */ 824 int acpi_dev_resume_early(struct device *dev) 825 { 826 struct acpi_device *adev = acpi_dev_pm_get_node(dev); 827 int error; 828 829 if (!adev) 830 return 0; 831 832 error = acpi_dev_pm_full_power(adev); 833 __acpi_device_sleep_wake(adev, ACPI_STATE_UNKNOWN, false); 834 return error; 835 } 836 EXPORT_SYMBOL_GPL(acpi_dev_resume_early); 837 838 /** 839 * acpi_subsys_prepare - Prepare device for system transition to a sleep state. 840 * @dev: Device to prepare. 841 */ 842 int acpi_subsys_prepare(struct device *dev) 843 { 844 /* 845 * Follow PCI and resume devices suspended at run time before running 846 * their system suspend callbacks. 847 */ 848 pm_runtime_resume(dev); 849 return pm_generic_prepare(dev); 850 } 851 EXPORT_SYMBOL_GPL(acpi_subsys_prepare); 852 853 /** 854 * acpi_subsys_suspend_late - Suspend device using ACPI. 855 * @dev: Device to suspend. 856 * 857 * Carry out the generic late suspend procedure for @dev and use ACPI to put 858 * it into a low-power state during system transition into a sleep state. 859 */ 860 int acpi_subsys_suspend_late(struct device *dev) 861 { 862 int ret = pm_generic_suspend_late(dev); 863 return ret ? ret : acpi_dev_suspend_late(dev); 864 } 865 EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late); 866 867 /** 868 * acpi_subsys_resume_early - Resume device using ACPI. 869 * @dev: Device to Resume. 870 * 871 * Use ACPI to put the given device into the full-power state and carry out the 872 * generic early resume procedure for it during system transition into the 873 * working state. 874 */ 875 int acpi_subsys_resume_early(struct device *dev) 876 { 877 int ret = acpi_dev_resume_early(dev); 878 return ret ? ret : pm_generic_resume_early(dev); 879 } 880 EXPORT_SYMBOL_GPL(acpi_subsys_resume_early); 881 #endif /* CONFIG_PM_SLEEP */ 882 883 static struct dev_pm_domain acpi_general_pm_domain = { 884 .ops = { 885 #ifdef CONFIG_PM_RUNTIME 886 .runtime_suspend = acpi_subsys_runtime_suspend, 887 .runtime_resume = acpi_subsys_runtime_resume, 888 .runtime_idle = pm_generic_runtime_idle, 889 #endif 890 #ifdef CONFIG_PM_SLEEP 891 .prepare = acpi_subsys_prepare, 892 .suspend_late = acpi_subsys_suspend_late, 893 .resume_early = acpi_subsys_resume_early, 894 .poweroff_late = acpi_subsys_suspend_late, 895 .restore_early = acpi_subsys_resume_early, 896 #endif 897 }, 898 }; 899 900 /** 901 * acpi_dev_pm_attach - Prepare device for ACPI power management. 902 * @dev: Device to prepare. 903 * @power_on: Whether or not to power on the device. 904 * 905 * If @dev has a valid ACPI handle that has a valid struct acpi_device object 906 * attached to it, install a wakeup notification handler for the device and 907 * add it to the general ACPI PM domain. If @power_on is set, the device will 908 * be put into the ACPI D0 state before the function returns. 909 * 910 * This assumes that the @dev's bus type uses generic power management callbacks 911 * (or doesn't use any power management callbacks at all). 912 * 913 * Callers must ensure proper synchronization of this function with power 914 * management callbacks. 915 */ 916 int acpi_dev_pm_attach(struct device *dev, bool power_on) 917 { 918 struct acpi_device *adev = acpi_dev_pm_get_node(dev); 919 920 if (!adev) 921 return -ENODEV; 922 923 if (dev->pm_domain) 924 return -EEXIST; 925 926 acpi_add_pm_notifier(adev, acpi_wakeup_device, dev); 927 dev->pm_domain = &acpi_general_pm_domain; 928 if (power_on) { 929 acpi_dev_pm_full_power(adev); 930 __acpi_device_run_wake(adev, false); 931 } 932 return 0; 933 } 934 EXPORT_SYMBOL_GPL(acpi_dev_pm_attach); 935 936 /** 937 * acpi_dev_pm_detach - Remove ACPI power management from the device. 938 * @dev: Device to take care of. 939 * @power_off: Whether or not to try to remove power from the device. 940 * 941 * Remove the device from the general ACPI PM domain and remove its wakeup 942 * notifier. If @power_off is set, additionally remove power from the device if 943 * possible. 944 * 945 * Callers must ensure proper synchronization of this function with power 946 * management callbacks. 947 */ 948 void acpi_dev_pm_detach(struct device *dev, bool power_off) 949 { 950 struct acpi_device *adev = acpi_dev_pm_get_node(dev); 951 952 if (adev && dev->pm_domain == &acpi_general_pm_domain) { 953 dev->pm_domain = NULL; 954 acpi_remove_pm_notifier(adev, acpi_wakeup_device); 955 if (power_off) { 956 /* 957 * If the device's PM QoS resume latency limit or flags 958 * have been exposed to user space, they have to be 959 * hidden at this point, so that they don't affect the 960 * choice of the low-power state to put the device into. 961 */ 962 dev_pm_qos_hide_latency_limit(dev); 963 dev_pm_qos_hide_flags(dev); 964 __acpi_device_run_wake(adev, false); 965 acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0); 966 } 967 } 968 } 969 EXPORT_SYMBOL_GPL(acpi_dev_pm_detach); 970 971 /** 972 * acpi_dev_pm_add_dependent - Add physical device depending for PM. 973 * @handle: Handle of ACPI device node. 974 * @depdev: Device depending on that node for PM. 975 */ 976 void acpi_dev_pm_add_dependent(acpi_handle handle, struct device *depdev) 977 { 978 struct acpi_device_physical_node *dep; 979 struct acpi_device *adev; 980 981 if (!depdev || acpi_bus_get_device(handle, &adev)) 982 return; 983 984 mutex_lock(&adev->physical_node_lock); 985 986 list_for_each_entry(dep, &adev->power_dependent, node) 987 if (dep->dev == depdev) 988 goto out; 989 990 dep = kzalloc(sizeof(*dep), GFP_KERNEL); 991 if (dep) { 992 dep->dev = depdev; 993 list_add_tail(&dep->node, &adev->power_dependent); 994 } 995 996 out: 997 mutex_unlock(&adev->physical_node_lock); 998 } 999 EXPORT_SYMBOL_GPL(acpi_dev_pm_add_dependent); 1000 1001 /** 1002 * acpi_dev_pm_remove_dependent - Remove physical device depending for PM. 1003 * @handle: Handle of ACPI device node. 1004 * @depdev: Device depending on that node for PM. 1005 */ 1006 void acpi_dev_pm_remove_dependent(acpi_handle handle, struct device *depdev) 1007 { 1008 struct acpi_device_physical_node *dep; 1009 struct acpi_device *adev; 1010 1011 if (!depdev || acpi_bus_get_device(handle, &adev)) 1012 return; 1013 1014 mutex_lock(&adev->physical_node_lock); 1015 1016 list_for_each_entry(dep, &adev->power_dependent, node) 1017 if (dep->dev == depdev) { 1018 list_del(&dep->node); 1019 kfree(dep); 1020 break; 1021 } 1022 1023 mutex_unlock(&adev->physical_node_lock); 1024 } 1025 EXPORT_SYMBOL_GPL(acpi_dev_pm_remove_dependent); 1026