11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2ec2cd81cSRafael J. Wysocki /* 3ec2cd81cSRafael J. Wysocki * drivers/acpi/device_pm.c - ACPI device power management routines. 4ec2cd81cSRafael J. Wysocki * 5ec2cd81cSRafael J. Wysocki * Copyright (C) 2012, Intel Corp. 6ec2cd81cSRafael J. Wysocki * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com> 7ec2cd81cSRafael J. Wysocki * 8ec2cd81cSRafael J. Wysocki * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 9ec2cd81cSRafael J. Wysocki * 10ec2cd81cSRafael J. Wysocki * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 11ec2cd81cSRafael J. Wysocki */ 12ec2cd81cSRafael J. Wysocki 13*255a04ccSRafael J. Wysocki #define pr_fmt(fmt) "PM: " fmt 14c56fd5eaSRafael J. Wysocki 157b199811SRafael J. Wysocki #include <linux/acpi.h> 1686b3832cSRafael J. Wysocki #include <linux/export.h> 17ec2cd81cSRafael J. Wysocki #include <linux/mutex.h> 1886b3832cSRafael J. Wysocki #include <linux/pm_qos.h> 19989561deSTomeu Vizoso #include <linux/pm_domain.h> 20cd7bd02dSRafael J. Wysocki #include <linux/pm_runtime.h> 2133e4f80eSRafael J. Wysocki #include <linux/suspend.h> 22ec2cd81cSRafael J. Wysocki 23b9370dceSRafael J. Wysocki #include "fan.h" 249ce4e607SRafael J. Wysocki #include "internal.h" 259ce4e607SRafael J. Wysocki 2686b3832cSRafael J. Wysocki /** 279ce4e607SRafael J. Wysocki * acpi_power_state_string - String representation of ACPI device power state. 289ce4e607SRafael J. Wysocki * @state: ACPI device power state to return the string representation of. 299ce4e607SRafael J. Wysocki */ 309ce4e607SRafael J. Wysocki const char *acpi_power_state_string(int state) 319ce4e607SRafael J. Wysocki { 329ce4e607SRafael J. Wysocki switch (state) { 339ce4e607SRafael J. Wysocki case ACPI_STATE_D0: 349ce4e607SRafael J. Wysocki return "D0"; 359ce4e607SRafael J. Wysocki case ACPI_STATE_D1: 369ce4e607SRafael J. Wysocki return "D1"; 379ce4e607SRafael J. Wysocki case ACPI_STATE_D2: 389ce4e607SRafael J. Wysocki return "D2"; 399ce4e607SRafael J. Wysocki case ACPI_STATE_D3_HOT: 409ce4e607SRafael J. Wysocki return "D3hot"; 419ce4e607SRafael J. Wysocki case ACPI_STATE_D3_COLD: 42898fee4fSRafael J. Wysocki return "D3cold"; 439ce4e607SRafael J. Wysocki default: 449ce4e607SRafael J. Wysocki return "(unknown)"; 459ce4e607SRafael J. Wysocki } 469ce4e607SRafael J. Wysocki } 479ce4e607SRafael J. Wysocki 48f850a48aSRafael J. Wysocki static int acpi_dev_pm_explicit_get(struct acpi_device *device, int *state) 49f850a48aSRafael J. Wysocki { 50f850a48aSRafael J. Wysocki unsigned long long psc; 51f850a48aSRafael J. Wysocki acpi_status status; 52f850a48aSRafael J. Wysocki 53f850a48aSRafael J. Wysocki status = acpi_evaluate_integer(device->handle, "_PSC", NULL, &psc); 54f850a48aSRafael J. Wysocki if (ACPI_FAILURE(status)) 55f850a48aSRafael J. Wysocki return -ENODEV; 56f850a48aSRafael J. Wysocki 57f850a48aSRafael J. Wysocki *state = psc; 58f850a48aSRafael J. Wysocki return 0; 59f850a48aSRafael J. Wysocki } 60f850a48aSRafael J. Wysocki 619ce4e607SRafael J. Wysocki /** 629ce4e607SRafael J. Wysocki * acpi_device_get_power - Get power state of an ACPI device. 639ce4e607SRafael J. Wysocki * @device: Device to get the power state of. 649ce4e607SRafael J. Wysocki * @state: Place to store the power state of the device. 659ce4e607SRafael J. Wysocki * 669ce4e607SRafael J. Wysocki * This function does not update the device's power.state field, but it may 679ce4e607SRafael J. Wysocki * update its parent's power.state field (when the parent's power state is 689ce4e607SRafael J. Wysocki * unknown and the device's power state turns out to be D0). 699ed411c0SRafael J. Wysocki * 709ed411c0SRafael J. Wysocki * Also, it does not update power resource reference counters to ensure that 719ed411c0SRafael J. Wysocki * the power state returned by it will be persistent and it may return a power 729ed411c0SRafael J. Wysocki * state shallower than previously set by acpi_device_set_power() for @device 739ed411c0SRafael J. Wysocki * (if that power state depends on any power resources). 749ce4e607SRafael J. Wysocki */ 759ce4e607SRafael J. Wysocki int acpi_device_get_power(struct acpi_device *device, int *state) 769ce4e607SRafael J. Wysocki { 779ce4e607SRafael J. Wysocki int result = ACPI_STATE_UNKNOWN; 78f850a48aSRafael J. Wysocki int error; 799ce4e607SRafael J. Wysocki 809ce4e607SRafael J. Wysocki if (!device || !state) 819ce4e607SRafael J. Wysocki return -EINVAL; 829ce4e607SRafael J. Wysocki 839ce4e607SRafael J. Wysocki if (!device->flags.power_manageable) { 849ce4e607SRafael J. Wysocki /* TBD: Non-recursive algorithm for walking up hierarchy. */ 859ce4e607SRafael J. Wysocki *state = device->parent ? 869ce4e607SRafael J. Wysocki device->parent->power.state : ACPI_STATE_D0; 879ce4e607SRafael J. Wysocki goto out; 889ce4e607SRafael J. Wysocki } 899ce4e607SRafael J. Wysocki 909ce4e607SRafael J. Wysocki /* 9175eb2d13SRafael J. Wysocki * Get the device's power state from power resources settings and _PSC, 9275eb2d13SRafael J. Wysocki * if available. 939ce4e607SRafael J. Wysocki */ 9475eb2d13SRafael J. Wysocki if (device->power.flags.power_resources) { 95f850a48aSRafael J. Wysocki error = acpi_power_get_inferred_state(device, &result); 969ce4e607SRafael J. Wysocki if (error) 979ce4e607SRafael J. Wysocki return error; 9875eb2d13SRafael J. Wysocki } 9975eb2d13SRafael J. Wysocki if (device->power.flags.explicit_get) { 100f850a48aSRafael J. Wysocki int psc; 10175eb2d13SRafael J. Wysocki 102f850a48aSRafael J. Wysocki error = acpi_dev_pm_explicit_get(device, &psc); 103f850a48aSRafael J. Wysocki if (error) 104f850a48aSRafael J. Wysocki return error; 10575eb2d13SRafael J. Wysocki 10675eb2d13SRafael J. Wysocki /* 10775eb2d13SRafael J. Wysocki * The power resources settings may indicate a power state 10820dacb71SRafael J. Wysocki * shallower than the actual power state of the device, because 10920dacb71SRafael J. Wysocki * the same power resources may be referenced by other devices. 11075eb2d13SRafael J. Wysocki * 11120dacb71SRafael J. Wysocki * For systems predating ACPI 4.0 we assume that D3hot is the 11220dacb71SRafael J. Wysocki * deepest state that can be supported. 11375eb2d13SRafael J. Wysocki */ 11475eb2d13SRafael J. Wysocki if (psc > result && psc < ACPI_STATE_D3_COLD) 11575eb2d13SRafael J. Wysocki result = psc; 11675eb2d13SRafael J. Wysocki else if (result == ACPI_STATE_UNKNOWN) 11720dacb71SRafael J. Wysocki result = psc > ACPI_STATE_D2 ? ACPI_STATE_D3_HOT : psc; 1189ce4e607SRafael J. Wysocki } 1199ce4e607SRafael J. Wysocki 1209ce4e607SRafael J. Wysocki /* 1219ce4e607SRafael J. Wysocki * If we were unsure about the device parent's power state up to this 1229ce4e607SRafael J. Wysocki * point, the fact that the device is in D0 implies that the parent has 123644f17adSMika Westerberg * to be in D0 too, except if ignore_parent is set. 1249ce4e607SRafael J. Wysocki */ 125644f17adSMika Westerberg if (!device->power.flags.ignore_parent && device->parent 126644f17adSMika Westerberg && device->parent->power.state == ACPI_STATE_UNKNOWN 1279ce4e607SRafael J. Wysocki && result == ACPI_STATE_D0) 1289ce4e607SRafael J. Wysocki device->parent->power.state = ACPI_STATE_D0; 1299ce4e607SRafael J. Wysocki 1309ce4e607SRafael J. Wysocki *state = result; 1319ce4e607SRafael J. Wysocki 1329ce4e607SRafael J. Wysocki out: 133198ee437SRafael J. Wysocki acpi_handle_debug(device->handle, "Power state: %s\n", 134c56fd5eaSRafael J. Wysocki acpi_power_state_string(*state)); 1359ce4e607SRafael J. Wysocki 1369ce4e607SRafael J. Wysocki return 0; 1379ce4e607SRafael J. Wysocki } 1389ce4e607SRafael J. Wysocki 1399c0f45e3SRafael J. Wysocki static int acpi_dev_pm_explicit_set(struct acpi_device *adev, int state) 1409c0f45e3SRafael J. Wysocki { 1419c0f45e3SRafael J. Wysocki if (adev->power.states[state].flags.explicit_set) { 1429c0f45e3SRafael J. Wysocki char method[5] = { '_', 'P', 'S', '0' + state, '\0' }; 1439c0f45e3SRafael J. Wysocki acpi_status status; 1449c0f45e3SRafael J. Wysocki 1459c0f45e3SRafael J. Wysocki status = acpi_evaluate_object(adev->handle, method, NULL, NULL); 1469c0f45e3SRafael J. Wysocki if (ACPI_FAILURE(status)) 1479c0f45e3SRafael J. Wysocki return -ENODEV; 1489c0f45e3SRafael J. Wysocki } 1499c0f45e3SRafael J. Wysocki return 0; 1509c0f45e3SRafael J. Wysocki } 1519c0f45e3SRafael J. Wysocki 1529ce4e607SRafael J. Wysocki /** 1539ce4e607SRafael J. Wysocki * acpi_device_set_power - Set power state of an ACPI device. 1549ce4e607SRafael J. Wysocki * @device: Device to set the power state of. 1559ce4e607SRafael J. Wysocki * @state: New power state to set. 1569ce4e607SRafael J. Wysocki * 1579ce4e607SRafael J. Wysocki * Callers must ensure that the device is power manageable before using this 1589ce4e607SRafael J. Wysocki * function. 1599ce4e607SRafael J. Wysocki */ 1609ce4e607SRafael J. Wysocki int acpi_device_set_power(struct acpi_device *device, int state) 1619ce4e607SRafael J. Wysocki { 16220dacb71SRafael J. Wysocki int target_state = state; 1639ce4e607SRafael J. Wysocki int result = 0; 1649ce4e607SRafael J. Wysocki 1652c7d132aSRafael J. Wysocki if (!device || !device->flags.power_manageable 1662c7d132aSRafael J. Wysocki || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD)) 1679ce4e607SRafael J. Wysocki return -EINVAL; 1689ce4e607SRafael J. Wysocki 169ee8193eeSRafael J. Wysocki acpi_handle_debug(device->handle, "Power state change: %s -> %s\n", 170ee8193eeSRafael J. Wysocki acpi_power_state_string(device->power.state), 171ee8193eeSRafael J. Wysocki acpi_power_state_string(state)); 172ee8193eeSRafael J. Wysocki 1739ce4e607SRafael J. Wysocki /* Make sure this is a valid target state */ 1749ce4e607SRafael J. Wysocki 175f850a48aSRafael J. Wysocki /* There is a special case for D0 addressed below. */ 176f850a48aSRafael J. Wysocki if (state > ACPI_STATE_D0 && state == device->power.state) { 177c56fd5eaSRafael J. Wysocki dev_dbg(&device->dev, "Device already in %s\n", 178c56fd5eaSRafael J. Wysocki acpi_power_state_string(state)); 1799ce4e607SRafael J. Wysocki return 0; 1809ce4e607SRafael J. Wysocki } 1819ce4e607SRafael J. Wysocki 18220dacb71SRafael J. Wysocki if (state == ACPI_STATE_D3_COLD) { 18320dacb71SRafael J. Wysocki /* 18420dacb71SRafael J. Wysocki * For transitions to D3cold we need to execute _PS3 and then 18520dacb71SRafael J. Wysocki * possibly drop references to the power resources in use. 18620dacb71SRafael J. Wysocki */ 18720dacb71SRafael J. Wysocki state = ACPI_STATE_D3_HOT; 188956ad9d9SRafael J. Wysocki /* If D3cold is not supported, use D3hot as the target state. */ 18920dacb71SRafael J. Wysocki if (!device->power.states[ACPI_STATE_D3_COLD].flags.valid) 19020dacb71SRafael J. Wysocki target_state = state; 19120dacb71SRafael J. Wysocki } else if (!device->power.states[state].flags.valid) { 192b69137a7SRafael J. Wysocki dev_warn(&device->dev, "Power state %s not supported\n", 1939ce4e607SRafael J. Wysocki acpi_power_state_string(state)); 1949ce4e607SRafael J. Wysocki return -ENODEV; 1959ce4e607SRafael J. Wysocki } 19620dacb71SRafael J. Wysocki 197644f17adSMika Westerberg if (!device->power.flags.ignore_parent && 198644f17adSMika Westerberg device->parent && (state < device->parent->power.state)) { 199b69137a7SRafael J. Wysocki dev_warn(&device->dev, 200593298e6SAaron Lu "Cannot transition to power state %s for parent in %s\n", 201593298e6SAaron Lu acpi_power_state_string(state), 202593298e6SAaron Lu acpi_power_state_string(device->parent->power.state)); 2039ce4e607SRafael J. Wysocki return -ENODEV; 2049ce4e607SRafael J. Wysocki } 2059ce4e607SRafael J. Wysocki 206e78adb75SRafael J. Wysocki /* 207e78adb75SRafael J. Wysocki * Transition Power 208e78adb75SRafael J. Wysocki * ---------------- 20920dacb71SRafael J. Wysocki * In accordance with ACPI 6, _PSx is executed before manipulating power 21020dacb71SRafael J. Wysocki * resources, unless the target state is D0, in which case _PS0 is 21120dacb71SRafael J. Wysocki * supposed to be executed after turning the power resources on. 212e78adb75SRafael J. Wysocki */ 21320dacb71SRafael J. Wysocki if (state > ACPI_STATE_D0) { 21420dacb71SRafael J. Wysocki /* 21520dacb71SRafael J. Wysocki * According to ACPI 6, devices cannot go from lower-power 21620dacb71SRafael J. Wysocki * (deeper) states to higher-power (shallower) states. 21720dacb71SRafael J. Wysocki */ 21820dacb71SRafael J. Wysocki if (state < device->power.state) { 21920dacb71SRafael J. Wysocki dev_warn(&device->dev, "Cannot transition from %s to %s\n", 22020dacb71SRafael J. Wysocki acpi_power_state_string(device->power.state), 22120dacb71SRafael J. Wysocki acpi_power_state_string(state)); 22220dacb71SRafael J. Wysocki return -ENODEV; 2239ce4e607SRafael J. Wysocki } 22420dacb71SRafael J. Wysocki 22521ba2379SRafael J. Wysocki /* 22621ba2379SRafael J. Wysocki * If the device goes from D3hot to D3cold, _PS3 has been 22721ba2379SRafael J. Wysocki * evaluated for it already, so skip it in that case. 22821ba2379SRafael J. Wysocki */ 22921ba2379SRafael J. Wysocki if (device->power.state < ACPI_STATE_D3_HOT) { 230e78adb75SRafael J. Wysocki result = acpi_dev_pm_explicit_set(device, state); 231e78adb75SRafael J. Wysocki if (result) 232e78adb75SRafael J. Wysocki goto end; 23321ba2379SRafael J. Wysocki } 2349ce4e607SRafael J. Wysocki 23520dacb71SRafael J. Wysocki if (device->power.flags.power_resources) 23620dacb71SRafael J. Wysocki result = acpi_power_transition(device, target_state); 23720dacb71SRafael J. Wysocki } else { 23842787ed7SRafael J. Wysocki int cur_state = device->power.state; 23942787ed7SRafael J. Wysocki 24020dacb71SRafael J. Wysocki if (device->power.flags.power_resources) { 24120dacb71SRafael J. Wysocki result = acpi_power_transition(device, ACPI_STATE_D0); 24220dacb71SRafael J. Wysocki if (result) 24320dacb71SRafael J. Wysocki goto end; 24420dacb71SRafael J. Wysocki } 245f850a48aSRafael J. Wysocki 24642787ed7SRafael J. Wysocki if (cur_state == ACPI_STATE_D0) { 247f850a48aSRafael J. Wysocki int psc; 248f850a48aSRafael J. Wysocki 249f850a48aSRafael J. Wysocki /* Nothing to do here if _PSC is not present. */ 250f850a48aSRafael J. Wysocki if (!device->power.flags.explicit_get) 251f850a48aSRafael J. Wysocki return 0; 252f850a48aSRafael J. Wysocki 253f850a48aSRafael J. Wysocki /* 254f850a48aSRafael J. Wysocki * The power state of the device was set to D0 last 255f850a48aSRafael J. Wysocki * time, but that might have happened before a 256f850a48aSRafael J. Wysocki * system-wide transition involving the platform 257f850a48aSRafael J. Wysocki * firmware, so it may be necessary to evaluate _PS0 258f850a48aSRafael J. Wysocki * for the device here. However, use extra care here 259f850a48aSRafael J. Wysocki * and evaluate _PSC to check the device's current power 260f850a48aSRafael J. Wysocki * state, and only invoke _PS0 if the evaluation of _PSC 261f850a48aSRafael J. Wysocki * is successful and it returns a power state different 262f850a48aSRafael J. Wysocki * from D0. 263f850a48aSRafael J. Wysocki */ 264f850a48aSRafael J. Wysocki result = acpi_dev_pm_explicit_get(device, &psc); 265f850a48aSRafael J. Wysocki if (result || psc == ACPI_STATE_D0) 266f850a48aSRafael J. Wysocki return 0; 267f850a48aSRafael J. Wysocki } 268f850a48aSRafael J. Wysocki 26920dacb71SRafael J. Wysocki result = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0); 270e5656271SRafael J. Wysocki } 2719ce4e607SRafael J. Wysocki 2729ce4e607SRafael J. Wysocki end: 273e78adb75SRafael J. Wysocki if (result) { 274b69137a7SRafael J. Wysocki dev_warn(&device->dev, "Failed to change power state to %s\n", 275a9b760b0SKai-Heng Feng acpi_power_state_string(target_state)); 276e78adb75SRafael J. Wysocki } else { 27771b65445SMika Westerberg device->power.state = target_state; 278c56fd5eaSRafael J. Wysocki dev_dbg(&device->dev, "Power state changed to %s\n", 279c56fd5eaSRafael J. Wysocki acpi_power_state_string(target_state)); 2809ce4e607SRafael J. Wysocki } 2819ce4e607SRafael J. Wysocki 2829ce4e607SRafael J. Wysocki return result; 2839ce4e607SRafael J. Wysocki } 2849ce4e607SRafael J. Wysocki EXPORT_SYMBOL(acpi_device_set_power); 2859ce4e607SRafael J. Wysocki 2869ce4e607SRafael J. Wysocki int acpi_bus_set_power(acpi_handle handle, int state) 2879ce4e607SRafael J. Wysocki { 28899ece713SRafael J. Wysocki struct acpi_device *device = acpi_fetch_acpi_dev(handle); 2899ce4e607SRafael J. Wysocki 29099ece713SRafael J. Wysocki if (device) 2919ce4e607SRafael J. Wysocki return acpi_device_set_power(device, state); 29299ece713SRafael J. Wysocki 29399ece713SRafael J. Wysocki return -ENODEV; 2949ce4e607SRafael J. Wysocki } 2959ce4e607SRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_set_power); 2969ce4e607SRafael J. Wysocki 2979ce4e607SRafael J. Wysocki int acpi_bus_init_power(struct acpi_device *device) 2989ce4e607SRafael J. Wysocki { 2999ce4e607SRafael J. Wysocki int state; 3009ce4e607SRafael J. Wysocki int result; 3019ce4e607SRafael J. Wysocki 3029ce4e607SRafael J. Wysocki if (!device) 3039ce4e607SRafael J. Wysocki return -EINVAL; 3049ce4e607SRafael J. Wysocki 3059ce4e607SRafael J. Wysocki device->power.state = ACPI_STATE_UNKNOWN; 306cde1f95fSSakari Ailus if (!acpi_device_is_present(device)) { 307cde1f95fSSakari Ailus device->flags.initialized = false; 3081b1f3e16SRafael J. Wysocki return -ENXIO; 309cde1f95fSSakari Ailus } 3109ce4e607SRafael J. Wysocki 3119ce4e607SRafael J. Wysocki result = acpi_device_get_power(device, &state); 3129ce4e607SRafael J. Wysocki if (result) 3139ce4e607SRafael J. Wysocki return result; 3149ce4e607SRafael J. Wysocki 315a2367807SRafael J. Wysocki if (state < ACPI_STATE_D3_COLD && device->power.flags.power_resources) { 31620dacb71SRafael J. Wysocki /* Reference count the power resources. */ 3179ce4e607SRafael J. Wysocki result = acpi_power_on_resources(device, state); 318a2367807SRafael J. Wysocki if (result) 3199ce4e607SRafael J. Wysocki return result; 320a2367807SRafael J. Wysocki 32120dacb71SRafael J. Wysocki if (state == ACPI_STATE_D0) { 32220dacb71SRafael J. Wysocki /* 32320dacb71SRafael J. Wysocki * If _PSC is not present and the state inferred from 32420dacb71SRafael J. Wysocki * power resources appears to be D0, it still may be 32520dacb71SRafael J. Wysocki * necessary to execute _PS0 at this point, because 32620dacb71SRafael J. Wysocki * another device using the same power resources may 32720dacb71SRafael J. Wysocki * have been put into D0 previously and that's why we 32820dacb71SRafael J. Wysocki * see D0 here. 32920dacb71SRafael J. Wysocki */ 3309c0f45e3SRafael J. Wysocki result = acpi_dev_pm_explicit_set(device, state); 3319c0f45e3SRafael J. Wysocki if (result) 3329c0f45e3SRafael J. Wysocki return result; 33320dacb71SRafael J. Wysocki } 334b3785492SRafael J. Wysocki } else if (state == ACPI_STATE_UNKNOWN) { 3357cd8407dSRafael J. Wysocki /* 3367cd8407dSRafael J. Wysocki * No power resources and missing _PSC? Cross fingers and make 3377cd8407dSRafael J. Wysocki * it D0 in hope that this is what the BIOS put the device into. 3387cd8407dSRafael J. Wysocki * [We tried to force D0 here by executing _PS0, but that broke 3397cd8407dSRafael J. Wysocki * Toshiba P870-303 in a nasty way.] 3407cd8407dSRafael J. Wysocki */ 341b3785492SRafael J. Wysocki state = ACPI_STATE_D0; 342a2367807SRafael J. Wysocki } 343a2367807SRafael J. Wysocki device->power.state = state; 344a2367807SRafael J. Wysocki return 0; 3459ce4e607SRafael J. Wysocki } 3469ce4e607SRafael J. Wysocki 347b9e95fc6SRafael J. Wysocki /** 348b9e95fc6SRafael J. Wysocki * acpi_device_fix_up_power - Force device with missing _PSC into D0. 349b9e95fc6SRafael J. Wysocki * @device: Device object whose power state is to be fixed up. 350b9e95fc6SRafael J. Wysocki * 351b9e95fc6SRafael J. Wysocki * Devices without power resources and _PSC, but having _PS0 and _PS3 defined, 352b9e95fc6SRafael J. Wysocki * are assumed to be put into D0 by the BIOS. However, in some cases that may 353b9e95fc6SRafael J. Wysocki * not be the case and this function should be used then. 354b9e95fc6SRafael J. Wysocki */ 355b9e95fc6SRafael J. Wysocki int acpi_device_fix_up_power(struct acpi_device *device) 356b9e95fc6SRafael J. Wysocki { 357b9e95fc6SRafael J. Wysocki int ret = 0; 358b9e95fc6SRafael J. Wysocki 359b9e95fc6SRafael J. Wysocki if (!device->power.flags.power_resources 360b9e95fc6SRafael J. Wysocki && !device->power.flags.explicit_get 361b9e95fc6SRafael J. Wysocki && device->power.state == ACPI_STATE_D0) 362b9e95fc6SRafael J. Wysocki ret = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0); 363b9e95fc6SRafael J. Wysocki 364b9e95fc6SRafael J. Wysocki return ret; 365b9e95fc6SRafael J. Wysocki } 36678a898d0SUlf Hansson EXPORT_SYMBOL_GPL(acpi_device_fix_up_power); 367b9e95fc6SRafael J. Wysocki 368202317a5SRafael J. Wysocki int acpi_device_update_power(struct acpi_device *device, int *state_p) 3699ce4e607SRafael J. Wysocki { 3709ce4e607SRafael J. Wysocki int state; 3719ce4e607SRafael J. Wysocki int result; 3729ce4e607SRafael J. Wysocki 373202317a5SRafael J. Wysocki if (device->power.state == ACPI_STATE_UNKNOWN) { 374202317a5SRafael J. Wysocki result = acpi_bus_init_power(device); 375202317a5SRafael J. Wysocki if (!result && state_p) 376202317a5SRafael J. Wysocki *state_p = device->power.state; 377202317a5SRafael J. Wysocki 3789ce4e607SRafael J. Wysocki return result; 379202317a5SRafael J. Wysocki } 3809ce4e607SRafael J. Wysocki 3819ce4e607SRafael J. Wysocki result = acpi_device_get_power(device, &state); 3829ce4e607SRafael J. Wysocki if (result) 3839ce4e607SRafael J. Wysocki return result; 3849ce4e607SRafael J. Wysocki 38591bdad0bSRafael J. Wysocki if (state == ACPI_STATE_UNKNOWN) { 386511d5c42SRafael J. Wysocki state = ACPI_STATE_D0; 3879ce4e607SRafael J. Wysocki result = acpi_device_set_power(device, state); 38891bdad0bSRafael J. Wysocki if (result) 38991bdad0bSRafael J. Wysocki return result; 39091bdad0bSRafael J. Wysocki } else { 39191bdad0bSRafael J. Wysocki if (device->power.flags.power_resources) { 39291bdad0bSRafael J. Wysocki /* 39391bdad0bSRafael J. Wysocki * We don't need to really switch the state, bu we need 39491bdad0bSRafael J. Wysocki * to update the power resources' reference counters. 39591bdad0bSRafael J. Wysocki */ 39691bdad0bSRafael J. Wysocki result = acpi_power_transition(device, state); 39791bdad0bSRafael J. Wysocki if (result) 39891bdad0bSRafael J. Wysocki return result; 39991bdad0bSRafael J. Wysocki } 40091bdad0bSRafael J. Wysocki device->power.state = state; 40191bdad0bSRafael J. Wysocki } 40291bdad0bSRafael J. Wysocki if (state_p) 4039ce4e607SRafael J. Wysocki *state_p = state; 4049ce4e607SRafael J. Wysocki 40591bdad0bSRafael J. Wysocki return 0; 4069ce4e607SRafael J. Wysocki } 4072bb3a2bfSAaron Lu EXPORT_SYMBOL_GPL(acpi_device_update_power); 408202317a5SRafael J. Wysocki 409202317a5SRafael J. Wysocki int acpi_bus_update_power(acpi_handle handle, int *state_p) 410202317a5SRafael J. Wysocki { 41199ece713SRafael J. Wysocki struct acpi_device *device = acpi_fetch_acpi_dev(handle); 412202317a5SRafael J. Wysocki 41399ece713SRafael J. Wysocki if (device) 41499ece713SRafael J. Wysocki return acpi_device_update_power(device, state_p); 41599ece713SRafael J. Wysocki 41699ece713SRafael J. Wysocki return -ENODEV; 417202317a5SRafael J. Wysocki } 4189ce4e607SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_bus_update_power); 4199ce4e607SRafael J. Wysocki 4209ce4e607SRafael J. Wysocki bool acpi_bus_power_manageable(acpi_handle handle) 4219ce4e607SRafael J. Wysocki { 42299ece713SRafael J. Wysocki struct acpi_device *device = acpi_fetch_acpi_dev(handle); 4239ce4e607SRafael J. Wysocki 42499ece713SRafael J. Wysocki return device && device->flags.power_manageable; 4259ce4e607SRafael J. Wysocki } 4269ce4e607SRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_power_manageable); 4279ce4e607SRafael J. Wysocki 428ec4602a9SRafael J. Wysocki #ifdef CONFIG_PM 429ec4602a9SRafael J. Wysocki static DEFINE_MUTEX(acpi_pm_notifier_lock); 430ff165679SVille Syrjälä static DEFINE_MUTEX(acpi_pm_notifier_install_lock); 431ec4602a9SRafael J. Wysocki 43233e4f80eSRafael J. Wysocki void acpi_pm_wakeup_event(struct device *dev) 43333e4f80eSRafael J. Wysocki { 43433e4f80eSRafael J. Wysocki pm_wakeup_dev_event(dev, 0, acpi_s2idle_wakeup()); 43533e4f80eSRafael J. Wysocki } 43633e4f80eSRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_pm_wakeup_event); 43733e4f80eSRafael J. Wysocki 438c072530fSRafael J. Wysocki static void acpi_pm_notify_handler(acpi_handle handle, u32 val, void *not_used) 439c072530fSRafael J. Wysocki { 440c072530fSRafael J. Wysocki struct acpi_device *adev; 441c072530fSRafael J. Wysocki 442c072530fSRafael J. Wysocki if (val != ACPI_NOTIFY_DEVICE_WAKE) 443c072530fSRafael J. Wysocki return; 444c072530fSRafael J. Wysocki 445020a6375SRafael J. Wysocki acpi_handle_debug(handle, "Wake notify\n"); 446020a6375SRafael J. Wysocki 447c072530fSRafael J. Wysocki adev = acpi_bus_get_acpi_device(handle); 448c072530fSRafael J. Wysocki if (!adev) 449c072530fSRafael J. Wysocki return; 450c072530fSRafael J. Wysocki 451c072530fSRafael J. Wysocki mutex_lock(&acpi_pm_notifier_lock); 452c072530fSRafael J. Wysocki 453c072530fSRafael J. Wysocki if (adev->wakeup.flags.notifier_present) { 45433e4f80eSRafael J. Wysocki pm_wakeup_ws_event(adev->wakeup.ws, 0, acpi_s2idle_wakeup()); 455020a6375SRafael J. Wysocki if (adev->wakeup.context.func) { 456d75f773cSSakari Ailus acpi_handle_debug(handle, "Running %pS for %s\n", 457020a6375SRafael J. Wysocki adev->wakeup.context.func, 458020a6375SRafael J. Wysocki dev_name(adev->wakeup.context.dev)); 45964fd1c70SRafael J. Wysocki adev->wakeup.context.func(&adev->wakeup.context); 460c072530fSRafael J. Wysocki } 461020a6375SRafael J. Wysocki } 462c072530fSRafael J. Wysocki 463c072530fSRafael J. Wysocki mutex_unlock(&acpi_pm_notifier_lock); 464c072530fSRafael J. Wysocki 465c072530fSRafael J. Wysocki acpi_bus_put_acpi_device(adev); 466c072530fSRafael J. Wysocki } 467c072530fSRafael J. Wysocki 468ec4602a9SRafael J. Wysocki /** 469c072530fSRafael J. Wysocki * acpi_add_pm_notifier - Register PM notify handler for given ACPI device. 470c072530fSRafael J. Wysocki * @adev: ACPI device to add the notify handler for. 471c072530fSRafael J. Wysocki * @dev: Device to generate a wakeup event for while handling the notification. 47264fd1c70SRafael J. Wysocki * @func: Work function to execute when handling the notification. 473ec4602a9SRafael J. Wysocki * 474ec4602a9SRafael J. Wysocki * NOTE: @adev need not be a run-wake or wakeup device to be a valid source of 475ec4602a9SRafael J. Wysocki * PM wakeup events. For example, wakeup events may be generated for bridges 476ec4602a9SRafael J. Wysocki * if one of the devices below the bridge is signaling wakeup, even if the 477ec4602a9SRafael J. Wysocki * bridge itself doesn't have a wakeup GPE associated with it. 478ec4602a9SRafael J. Wysocki */ 479c072530fSRafael J. Wysocki acpi_status acpi_add_pm_notifier(struct acpi_device *adev, struct device *dev, 48064fd1c70SRafael J. Wysocki void (*func)(struct acpi_device_wakeup_context *context)) 481ec4602a9SRafael J. Wysocki { 482ec4602a9SRafael J. Wysocki acpi_status status = AE_ALREADY_EXISTS; 483ec4602a9SRafael J. Wysocki 48464fd1c70SRafael J. Wysocki if (!dev && !func) 485c072530fSRafael J. Wysocki return AE_BAD_PARAMETER; 486c072530fSRafael J. Wysocki 487ff165679SVille Syrjälä mutex_lock(&acpi_pm_notifier_install_lock); 488ec4602a9SRafael J. Wysocki 489ec4602a9SRafael J. Wysocki if (adev->wakeup.flags.notifier_present) 490ec4602a9SRafael J. Wysocki goto out; 491ec4602a9SRafael J. Wysocki 492c072530fSRafael J. Wysocki status = acpi_install_notify_handler(adev->handle, ACPI_SYSTEM_NOTIFY, 493c072530fSRafael J. Wysocki acpi_pm_notify_handler, NULL); 494ec4602a9SRafael J. Wysocki if (ACPI_FAILURE(status)) 495ec4602a9SRafael J. Wysocki goto out; 496ec4602a9SRafael J. Wysocki 497ff165679SVille Syrjälä mutex_lock(&acpi_pm_notifier_lock); 498c8377adfSTri Vo adev->wakeup.ws = wakeup_source_register(&adev->dev, 499c8377adfSTri Vo dev_name(&adev->dev)); 500ff165679SVille Syrjälä adev->wakeup.context.dev = dev; 501ff165679SVille Syrjälä adev->wakeup.context.func = func; 502ec4602a9SRafael J. Wysocki adev->wakeup.flags.notifier_present = true; 503ff165679SVille Syrjälä mutex_unlock(&acpi_pm_notifier_lock); 504ec4602a9SRafael J. Wysocki 505ec4602a9SRafael J. Wysocki out: 506ff165679SVille Syrjälä mutex_unlock(&acpi_pm_notifier_install_lock); 507ec4602a9SRafael J. Wysocki return status; 508ec4602a9SRafael J. Wysocki } 509ec4602a9SRafael J. Wysocki 510ec4602a9SRafael J. Wysocki /** 511ec4602a9SRafael J. Wysocki * acpi_remove_pm_notifier - Unregister PM notifier from given ACPI device. 512ec4602a9SRafael J. Wysocki * @adev: ACPI device to remove the notifier from. 513ec4602a9SRafael J. Wysocki */ 514c072530fSRafael J. Wysocki acpi_status acpi_remove_pm_notifier(struct acpi_device *adev) 515ec4602a9SRafael J. Wysocki { 516ec4602a9SRafael J. Wysocki acpi_status status = AE_BAD_PARAMETER; 517ec4602a9SRafael J. Wysocki 518ff165679SVille Syrjälä mutex_lock(&acpi_pm_notifier_install_lock); 519ec4602a9SRafael J. Wysocki 520ec4602a9SRafael J. Wysocki if (!adev->wakeup.flags.notifier_present) 521ec4602a9SRafael J. Wysocki goto out; 522ec4602a9SRafael J. Wysocki 523ec4602a9SRafael J. Wysocki status = acpi_remove_notify_handler(adev->handle, 524ec4602a9SRafael J. Wysocki ACPI_SYSTEM_NOTIFY, 525c072530fSRafael J. Wysocki acpi_pm_notify_handler); 526ec4602a9SRafael J. Wysocki if (ACPI_FAILURE(status)) 527ec4602a9SRafael J. Wysocki goto out; 528ec4602a9SRafael J. Wysocki 529ff165679SVille Syrjälä mutex_lock(&acpi_pm_notifier_lock); 53064fd1c70SRafael J. Wysocki adev->wakeup.context.func = NULL; 531c072530fSRafael J. Wysocki adev->wakeup.context.dev = NULL; 532c072530fSRafael J. Wysocki wakeup_source_unregister(adev->wakeup.ws); 533ec4602a9SRafael J. Wysocki adev->wakeup.flags.notifier_present = false; 534ff165679SVille Syrjälä mutex_unlock(&acpi_pm_notifier_lock); 535ec4602a9SRafael J. Wysocki 536ec4602a9SRafael J. Wysocki out: 537ff165679SVille Syrjälä mutex_unlock(&acpi_pm_notifier_install_lock); 538ec4602a9SRafael J. Wysocki return status; 539ec4602a9SRafael J. Wysocki } 540ec4602a9SRafael J. Wysocki 5419ce4e607SRafael J. Wysocki bool acpi_bus_can_wakeup(acpi_handle handle) 5429ce4e607SRafael J. Wysocki { 54399ece713SRafael J. Wysocki struct acpi_device *device = acpi_fetch_acpi_dev(handle); 5449ce4e607SRafael J. Wysocki 54599ece713SRafael J. Wysocki return device && device->wakeup.flags.valid; 5469ce4e607SRafael J. Wysocki } 5479ce4e607SRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_can_wakeup); 5489ce4e607SRafael J. Wysocki 5498370c2dcSRafael J. Wysocki bool acpi_pm_device_can_wakeup(struct device *dev) 5508370c2dcSRafael J. Wysocki { 5518370c2dcSRafael J. Wysocki struct acpi_device *adev = ACPI_COMPANION(dev); 5528370c2dcSRafael J. Wysocki 5538370c2dcSRafael J. Wysocki return adev ? acpi_device_can_wakeup(adev) : false; 5548370c2dcSRafael J. Wysocki } 5558370c2dcSRafael J. Wysocki 5569ce4e607SRafael J. Wysocki /** 557b25c77efSRafael J. Wysocki * acpi_dev_pm_get_state - Get preferred power state of ACPI device. 55886b3832cSRafael J. Wysocki * @dev: Device whose preferred target power state to return. 55986b3832cSRafael J. Wysocki * @adev: ACPI device node corresponding to @dev. 56086b3832cSRafael J. Wysocki * @target_state: System state to match the resultant device state. 561fa1675b5SRafael J. Wysocki * @d_min_p: Location to store the highest power state available to the device. 562fa1675b5SRafael J. Wysocki * @d_max_p: Location to store the lowest power state available to the device. 56386b3832cSRafael J. Wysocki * 564fa1675b5SRafael J. Wysocki * Find the lowest power (highest number) and highest power (lowest number) ACPI 565fa1675b5SRafael J. Wysocki * device power states that the device can be in while the system is in the 566fa1675b5SRafael J. Wysocki * state represented by @target_state. Store the integer numbers representing 567fa1675b5SRafael J. Wysocki * those stats in the memory locations pointed to by @d_max_p and @d_min_p, 568fa1675b5SRafael J. Wysocki * respectively. 56986b3832cSRafael J. Wysocki * 57086b3832cSRafael J. Wysocki * Callers must ensure that @dev and @adev are valid pointers and that @adev 57186b3832cSRafael J. Wysocki * actually corresponds to @dev before using this function. 572fa1675b5SRafael J. Wysocki * 573fa1675b5SRafael J. Wysocki * Returns 0 on success or -ENODATA when one of the ACPI methods fails or 574fa1675b5SRafael J. Wysocki * returns a value that doesn't make sense. The memory locations pointed to by 575fa1675b5SRafael J. Wysocki * @d_max_p and @d_min_p are only modified on success. 57686b3832cSRafael J. Wysocki */ 577b25c77efSRafael J. Wysocki static int acpi_dev_pm_get_state(struct device *dev, struct acpi_device *adev, 578fa1675b5SRafael J. Wysocki u32 target_state, int *d_min_p, int *d_max_p) 57986b3832cSRafael J. Wysocki { 580fa1675b5SRafael J. Wysocki char method[] = { '_', 'S', '0' + target_state, 'D', '\0' }; 581fa1675b5SRafael J. Wysocki acpi_handle handle = adev->handle; 582fa1675b5SRafael J. Wysocki unsigned long long ret; 583fa1675b5SRafael J. Wysocki int d_min, d_max; 58486b3832cSRafael J. Wysocki bool wakeup = false; 585bf8c6184SDaniel Drake bool has_sxd = false; 586fa1675b5SRafael J. Wysocki acpi_status status; 58786b3832cSRafael J. Wysocki 58886b3832cSRafael J. Wysocki /* 589fa1675b5SRafael J. Wysocki * If the system state is S0, the lowest power state the device can be 590fa1675b5SRafael J. Wysocki * in is D3cold, unless the device has _S0W and is supposed to signal 591fa1675b5SRafael J. Wysocki * wakeup, in which case the return value of _S0W has to be used as the 592fa1675b5SRafael J. Wysocki * lowest power state available to the device. 59386b3832cSRafael J. Wysocki */ 59486b3832cSRafael J. Wysocki d_min = ACPI_STATE_D0; 5954c164ae7SRafael J. Wysocki d_max = ACPI_STATE_D3_COLD; 59686b3832cSRafael J. Wysocki 59786b3832cSRafael J. Wysocki /* 59886b3832cSRafael J. Wysocki * If present, _SxD methods return the minimum D-state (highest power 59986b3832cSRafael J. Wysocki * state) we can use for the corresponding S-states. Otherwise, the 60086b3832cSRafael J. Wysocki * minimum D-state is D0 (ACPI 3.x). 60186b3832cSRafael J. Wysocki */ 60286b3832cSRafael J. Wysocki if (target_state > ACPI_STATE_S0) { 603fa1675b5SRafael J. Wysocki /* 604fa1675b5SRafael J. Wysocki * We rely on acpi_evaluate_integer() not clobbering the integer 605fa1675b5SRafael J. Wysocki * provided if AE_NOT_FOUND is returned. 606fa1675b5SRafael J. Wysocki */ 607fa1675b5SRafael J. Wysocki ret = d_min; 608fa1675b5SRafael J. Wysocki status = acpi_evaluate_integer(handle, method, NULL, &ret); 609fa1675b5SRafael J. Wysocki if ((ACPI_FAILURE(status) && status != AE_NOT_FOUND) 610fa1675b5SRafael J. Wysocki || ret > ACPI_STATE_D3_COLD) 611fa1675b5SRafael J. Wysocki return -ENODATA; 612fa1675b5SRafael J. Wysocki 613fa1675b5SRafael J. Wysocki /* 614fa1675b5SRafael J. Wysocki * We need to handle legacy systems where D3hot and D3cold are 615fa1675b5SRafael J. Wysocki * the same and 3 is returned in both cases, so fall back to 616fa1675b5SRafael J. Wysocki * D3cold if D3hot is not a valid state. 617fa1675b5SRafael J. Wysocki */ 618fa1675b5SRafael J. Wysocki if (!adev->power.states[ret].flags.valid) { 619fa1675b5SRafael J. Wysocki if (ret == ACPI_STATE_D3_HOT) 620fa1675b5SRafael J. Wysocki ret = ACPI_STATE_D3_COLD; 621fa1675b5SRafael J. Wysocki else 622fa1675b5SRafael J. Wysocki return -ENODATA; 623fa1675b5SRafael J. Wysocki } 624bf8c6184SDaniel Drake 625bf8c6184SDaniel Drake if (status == AE_OK) 626bf8c6184SDaniel Drake has_sxd = true; 627bf8c6184SDaniel Drake 628fa1675b5SRafael J. Wysocki d_min = ret; 62986b3832cSRafael J. Wysocki wakeup = device_may_wakeup(dev) && adev->wakeup.flags.valid 63086b3832cSRafael J. Wysocki && adev->wakeup.sleep_state >= target_state; 63120f97cafSRafael J. Wysocki } else { 63286b3832cSRafael J. Wysocki wakeup = adev->wakeup.flags.valid; 63386b3832cSRafael J. Wysocki } 63486b3832cSRafael J. Wysocki 63586b3832cSRafael J. Wysocki /* 63686b3832cSRafael J. Wysocki * If _PRW says we can wake up the system from the target sleep state, 63786b3832cSRafael J. Wysocki * the D-state returned by _SxD is sufficient for that (we assume a 63886b3832cSRafael J. Wysocki * wakeup-aware driver if wake is set). Still, if _SxW exists 63986b3832cSRafael J. Wysocki * (ACPI 3.x), it should return the maximum (lowest power) D-state that 64086b3832cSRafael J. Wysocki * can wake the system. _S0W may be valid, too. 64186b3832cSRafael J. Wysocki */ 64286b3832cSRafael J. Wysocki if (wakeup) { 643fa1675b5SRafael J. Wysocki method[3] = 'W'; 644fa1675b5SRafael J. Wysocki status = acpi_evaluate_integer(handle, method, NULL, &ret); 645fa1675b5SRafael J. Wysocki if (status == AE_NOT_FOUND) { 646bf8c6184SDaniel Drake /* No _SxW. In this case, the ACPI spec says that we 647bf8c6184SDaniel Drake * must not go into any power state deeper than the 648bf8c6184SDaniel Drake * value returned from _SxD. 649bf8c6184SDaniel Drake */ 650bf8c6184SDaniel Drake if (has_sxd && target_state > ACPI_STATE_S0) 65186b3832cSRafael J. Wysocki d_max = d_min; 652fa1675b5SRafael J. Wysocki } else if (ACPI_SUCCESS(status) && ret <= ACPI_STATE_D3_COLD) { 653fa1675b5SRafael J. Wysocki /* Fall back to D3cold if ret is not a valid state. */ 654fa1675b5SRafael J. Wysocki if (!adev->power.states[ret].flags.valid) 655fa1675b5SRafael J. Wysocki ret = ACPI_STATE_D3_COLD; 656fa1675b5SRafael J. Wysocki 657fa1675b5SRafael J. Wysocki d_max = ret > d_min ? ret : d_min; 658fa1675b5SRafael J. Wysocki } else { 659fa1675b5SRafael J. Wysocki return -ENODATA; 66086b3832cSRafael J. Wysocki } 66186b3832cSRafael J. Wysocki } 66286b3832cSRafael J. Wysocki 66386b3832cSRafael J. Wysocki if (d_min_p) 66486b3832cSRafael J. Wysocki *d_min_p = d_min; 665fa1675b5SRafael J. Wysocki 666fa1675b5SRafael J. Wysocki if (d_max_p) 667fa1675b5SRafael J. Wysocki *d_max_p = d_max; 668fa1675b5SRafael J. Wysocki 669fa1675b5SRafael J. Wysocki return 0; 67086b3832cSRafael J. Wysocki } 671cd7bd02dSRafael J. Wysocki 672a6ae7594SRafael J. Wysocki /** 673a6ae7594SRafael J. Wysocki * acpi_pm_device_sleep_state - Get preferred power state of ACPI device. 674a6ae7594SRafael J. Wysocki * @dev: Device whose preferred target power state to return. 675a6ae7594SRafael J. Wysocki * @d_min_p: Location to store the upper limit of the allowed states range. 676a6ae7594SRafael J. Wysocki * @d_max_in: Deepest low-power state to take into consideration. 677a6ae7594SRafael J. Wysocki * Return value: Preferred power state of the device on success, -ENODEV 678fa1675b5SRafael J. Wysocki * if there's no 'struct acpi_device' for @dev, -EINVAL if @d_max_in is 679fa1675b5SRafael J. Wysocki * incorrect, or -ENODATA on ACPI method failure. 680a6ae7594SRafael J. Wysocki * 681a6ae7594SRafael J. Wysocki * The caller must ensure that @dev is valid before using this function. 682a6ae7594SRafael J. Wysocki */ 683a6ae7594SRafael J. Wysocki int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in) 684a6ae7594SRafael J. Wysocki { 685a6ae7594SRafael J. Wysocki struct acpi_device *adev; 6869b5c7a5aSRafael J. Wysocki int ret, d_min, d_max; 687fa1675b5SRafael J. Wysocki 688fa1675b5SRafael J. Wysocki if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3_COLD) 689fa1675b5SRafael J. Wysocki return -EINVAL; 690fa1675b5SRafael J. Wysocki 69120dacb71SRafael J. Wysocki if (d_max_in > ACPI_STATE_D2) { 692fa1675b5SRafael J. Wysocki enum pm_qos_flags_status stat; 693fa1675b5SRafael J. Wysocki 694fa1675b5SRafael J. Wysocki stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF); 695fa1675b5SRafael J. Wysocki if (stat == PM_QOS_FLAGS_ALL) 69620dacb71SRafael J. Wysocki d_max_in = ACPI_STATE_D2; 697fa1675b5SRafael J. Wysocki } 698a6ae7594SRafael J. Wysocki 69917653a3eSRafael J. Wysocki adev = ACPI_COMPANION(dev); 70017653a3eSRafael J. Wysocki if (!adev) { 70117653a3eSRafael J. Wysocki dev_dbg(dev, "ACPI companion missing in %s!\n", __func__); 702a6ae7594SRafael J. Wysocki return -ENODEV; 703a6ae7594SRafael J. Wysocki } 704a6ae7594SRafael J. Wysocki 705fa1675b5SRafael J. Wysocki ret = acpi_dev_pm_get_state(dev, adev, acpi_target_system_state(), 7069b5c7a5aSRafael J. Wysocki &d_min, &d_max); 707fa1675b5SRafael J. Wysocki if (ret) 708fa1675b5SRafael J. Wysocki return ret; 709fa1675b5SRafael J. Wysocki 7109b5c7a5aSRafael J. Wysocki if (d_max_in < d_min) 711fa1675b5SRafael J. Wysocki return -EINVAL; 712fa1675b5SRafael J. Wysocki 713fa1675b5SRafael J. Wysocki if (d_max > d_max_in) { 7149b5c7a5aSRafael J. Wysocki for (d_max = d_max_in; d_max > d_min; d_max--) { 715fa1675b5SRafael J. Wysocki if (adev->power.states[d_max].flags.valid) 716fa1675b5SRafael J. Wysocki break; 717fa1675b5SRafael J. Wysocki } 718fa1675b5SRafael J. Wysocki } 7199b5c7a5aSRafael J. Wysocki 7209b5c7a5aSRafael J. Wysocki if (d_min_p) 7219b5c7a5aSRafael J. Wysocki *d_min_p = d_min; 7229b5c7a5aSRafael J. Wysocki 723fa1675b5SRafael J. Wysocki return d_max; 724a6ae7594SRafael J. Wysocki } 725a6ae7594SRafael J. Wysocki EXPORT_SYMBOL(acpi_pm_device_sleep_state); 726a6ae7594SRafael J. Wysocki 727cd7bd02dSRafael J. Wysocki /** 728c072530fSRafael J. Wysocki * acpi_pm_notify_work_func - ACPI devices wakeup notification work function. 72964fd1c70SRafael J. Wysocki * @context: Device wakeup context. 730e5cc8ef3SRafael J. Wysocki */ 73164fd1c70SRafael J. Wysocki static void acpi_pm_notify_work_func(struct acpi_device_wakeup_context *context) 732e5cc8ef3SRafael J. Wysocki { 73364fd1c70SRafael J. Wysocki struct device *dev = context->dev; 734e5cc8ef3SRafael J. Wysocki 735c072530fSRafael J. Wysocki if (dev) { 736e5cc8ef3SRafael J. Wysocki pm_wakeup_event(dev, 0); 73764fd1c70SRafael J. Wysocki pm_request_resume(dev); 738e5cc8ef3SRafael J. Wysocki } 739e5cc8ef3SRafael J. Wysocki } 740e5cc8ef3SRafael J. Wysocki 74199d8845eSRafael J. Wysocki static DEFINE_MUTEX(acpi_wakeup_lock); 74299d8845eSRafael J. Wysocki 7431ba51a7cSRafael J. Wysocki static int __acpi_device_wakeup_enable(struct acpi_device *adev, 7447482c5cbSRafael J. Wysocki u32 target_state) 7451ba51a7cSRafael J. Wysocki { 7461ba51a7cSRafael J. Wysocki struct acpi_device_wakeup *wakeup = &adev->wakeup; 7471ba51a7cSRafael J. Wysocki acpi_status status; 7481ba51a7cSRafael J. Wysocki int error = 0; 7491ba51a7cSRafael J. Wysocki 7501ba51a7cSRafael J. Wysocki mutex_lock(&acpi_wakeup_lock); 7511ba51a7cSRafael J. Wysocki 752b93b7ef6SRafael J. Wysocki /* 753b93b7ef6SRafael J. Wysocki * If the device wakeup power is already enabled, disable it and enable 754b93b7ef6SRafael J. Wysocki * it again in case it depends on the configuration of subordinate 755b93b7ef6SRafael J. Wysocki * devices and the conditions have changed since it was enabled last 756b93b7ef6SRafael J. Wysocki * time. 757b93b7ef6SRafael J. Wysocki */ 7581ba51a7cSRafael J. Wysocki if (wakeup->enable_count > 0) 759b93b7ef6SRafael J. Wysocki acpi_disable_wakeup_device_power(adev); 7601ba51a7cSRafael J. Wysocki 7611ba51a7cSRafael J. Wysocki error = acpi_enable_wakeup_device_power(adev, target_state); 762b93b7ef6SRafael J. Wysocki if (error) { 763b93b7ef6SRafael J. Wysocki if (wakeup->enable_count > 0) { 764b93b7ef6SRafael J. Wysocki acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number); 765b93b7ef6SRafael J. Wysocki wakeup->enable_count = 0; 766b93b7ef6SRafael J. Wysocki } 7671ba51a7cSRafael J. Wysocki goto out; 768b93b7ef6SRafael J. Wysocki } 769b93b7ef6SRafael J. Wysocki 770b93b7ef6SRafael J. Wysocki if (wakeup->enable_count > 0) 771b93b7ef6SRafael J. Wysocki goto inc; 7721ba51a7cSRafael J. Wysocki 7731ba51a7cSRafael J. Wysocki status = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number); 7741ba51a7cSRafael J. Wysocki if (ACPI_FAILURE(status)) { 7751ba51a7cSRafael J. Wysocki acpi_disable_wakeup_device_power(adev); 7761ba51a7cSRafael J. Wysocki error = -EIO; 7771ba51a7cSRafael J. Wysocki goto out; 7781ba51a7cSRafael J. Wysocki } 7791ba51a7cSRafael J. Wysocki 780fbc9418fSRafael J. Wysocki acpi_handle_debug(adev->handle, "GPE%2X enabled for wakeup\n", 781fbc9418fSRafael J. Wysocki (unsigned int)wakeup->gpe_number); 782fbc9418fSRafael J. Wysocki 7831ba51a7cSRafael J. Wysocki inc: 784b93b7ef6SRafael J. Wysocki if (wakeup->enable_count < INT_MAX) 7851ba51a7cSRafael J. Wysocki wakeup->enable_count++; 786b93b7ef6SRafael J. Wysocki else 787b93b7ef6SRafael J. Wysocki acpi_handle_info(adev->handle, "Wakeup enable count out of bounds!\n"); 7881ba51a7cSRafael J. Wysocki 7891ba51a7cSRafael J. Wysocki out: 7901ba51a7cSRafael J. Wysocki mutex_unlock(&acpi_wakeup_lock); 7911ba51a7cSRafael J. Wysocki return error; 7921ba51a7cSRafael J. Wysocki } 7931ba51a7cSRafael J. Wysocki 794e5cc8ef3SRafael J. Wysocki /** 79599d8845eSRafael J. Wysocki * acpi_device_wakeup_enable - Enable wakeup functionality for device. 79699d8845eSRafael J. Wysocki * @adev: ACPI device to enable wakeup functionality for. 797f35cec25SRafael J. Wysocki * @target_state: State the system is transitioning into. 798cd7bd02dSRafael J. Wysocki * 79999d8845eSRafael J. Wysocki * Enable the GPE associated with @adev so that it can generate wakeup signals 80099d8845eSRafael J. Wysocki * for the device in response to external (remote) events and enable wakeup 80199d8845eSRafael J. Wysocki * power for it. 802dee8370cSRafael J. Wysocki * 803dee8370cSRafael J. Wysocki * Callers must ensure that @adev is a valid ACPI device node before executing 804dee8370cSRafael J. Wysocki * this function. 805dee8370cSRafael J. Wysocki */ 80699d8845eSRafael J. Wysocki static int acpi_device_wakeup_enable(struct acpi_device *adev, u32 target_state) 807dee8370cSRafael J. Wysocki { 8087482c5cbSRafael J. Wysocki return __acpi_device_wakeup_enable(adev, target_state); 80999d8845eSRafael J. Wysocki } 81099d8845eSRafael J. Wysocki 81199d8845eSRafael J. Wysocki /** 81299d8845eSRafael J. Wysocki * acpi_device_wakeup_disable - Disable wakeup functionality for device. 81399d8845eSRafael J. Wysocki * @adev: ACPI device to disable wakeup functionality for. 81499d8845eSRafael J. Wysocki * 81599d8845eSRafael J. Wysocki * Disable the GPE associated with @adev and disable wakeup power for it. 81699d8845eSRafael J. Wysocki * 81799d8845eSRafael J. Wysocki * Callers must ensure that @adev is a valid ACPI device node before executing 81899d8845eSRafael J. Wysocki * this function. 81999d8845eSRafael J. Wysocki */ 82099d8845eSRafael J. Wysocki static void acpi_device_wakeup_disable(struct acpi_device *adev) 82199d8845eSRafael J. Wysocki { 82299d8845eSRafael J. Wysocki struct acpi_device_wakeup *wakeup = &adev->wakeup; 82399d8845eSRafael J. Wysocki 82499d8845eSRafael J. Wysocki mutex_lock(&acpi_wakeup_lock); 82599d8845eSRafael J. Wysocki 82699d8845eSRafael J. Wysocki if (!wakeup->enable_count) 82799d8845eSRafael J. Wysocki goto out; 82899d8845eSRafael J. Wysocki 829dee8370cSRafael J. Wysocki acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number); 830dee8370cSRafael J. Wysocki acpi_disable_wakeup_device_power(adev); 83199d8845eSRafael J. Wysocki 83299d8845eSRafael J. Wysocki wakeup->enable_count--; 83399d8845eSRafael J. Wysocki 83499d8845eSRafael J. Wysocki out: 83599d8845eSRafael J. Wysocki mutex_unlock(&acpi_wakeup_lock); 836dee8370cSRafael J. Wysocki } 837dee8370cSRafael J. Wysocki 8387482c5cbSRafael J. Wysocki /** 8397482c5cbSRafael J. Wysocki * acpi_pm_set_device_wakeup - Enable/disable remote wakeup for given device. 8407482c5cbSRafael J. Wysocki * @dev: Device to enable/disable to generate wakeup events. 8417482c5cbSRafael J. Wysocki * @enable: Whether to enable or disable the wakeup functionality. 8427482c5cbSRafael J. Wysocki */ 8437482c5cbSRafael J. Wysocki int acpi_pm_set_device_wakeup(struct device *dev, bool enable) 844a6ae7594SRafael J. Wysocki { 845a6ae7594SRafael J. Wysocki struct acpi_device *adev; 846a6ae7594SRafael J. Wysocki int error; 847a6ae7594SRafael J. Wysocki 84817653a3eSRafael J. Wysocki adev = ACPI_COMPANION(dev); 84917653a3eSRafael J. Wysocki if (!adev) { 85017653a3eSRafael J. Wysocki dev_dbg(dev, "ACPI companion missing in %s!\n", __func__); 851a6ae7594SRafael J. Wysocki return -ENODEV; 852a6ae7594SRafael J. Wysocki } 853a6ae7594SRafael J. Wysocki 8544d183d04SRafael J. Wysocki if (!acpi_device_can_wakeup(adev)) 8554d183d04SRafael J. Wysocki return -EINVAL; 8564d183d04SRafael J. Wysocki 85799d8845eSRafael J. Wysocki if (!enable) { 85899d8845eSRafael J. Wysocki acpi_device_wakeup_disable(adev); 85999d8845eSRafael J. Wysocki dev_dbg(dev, "Wakeup disabled by ACPI\n"); 86099d8845eSRafael J. Wysocki return 0; 86199d8845eSRafael J. Wysocki } 86299d8845eSRafael J. Wysocki 8637482c5cbSRafael J. Wysocki error = __acpi_device_wakeup_enable(adev, acpi_target_system_state()); 864a6ae7594SRafael J. Wysocki if (!error) 86599d8845eSRafael J. Wysocki dev_dbg(dev, "Wakeup enabled by ACPI\n"); 866a6ae7594SRafael J. Wysocki 867a6ae7594SRafael J. Wysocki return error; 868a6ae7594SRafael J. Wysocki } 8691ba51a7cSRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_pm_set_device_wakeup); 8701ba51a7cSRafael J. Wysocki 8711ba51a7cSRafael J. Wysocki /** 872e5cc8ef3SRafael J. Wysocki * acpi_dev_pm_low_power - Put ACPI device into a low-power state. 873e5cc8ef3SRafael J. Wysocki * @dev: Device to put into a low-power state. 874e5cc8ef3SRafael J. Wysocki * @adev: ACPI device node corresponding to @dev. 875e5cc8ef3SRafael J. Wysocki * @system_state: System state to choose the device state for. 876e5cc8ef3SRafael J. Wysocki */ 877e5cc8ef3SRafael J. Wysocki static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev, 878e5cc8ef3SRafael J. Wysocki u32 system_state) 879e5cc8ef3SRafael J. Wysocki { 880fa1675b5SRafael J. Wysocki int ret, state; 881e5cc8ef3SRafael J. Wysocki 882e5cc8ef3SRafael J. Wysocki if (!acpi_device_power_manageable(adev)) 883e5cc8ef3SRafael J. Wysocki return 0; 884e5cc8ef3SRafael J. Wysocki 885fa1675b5SRafael J. Wysocki ret = acpi_dev_pm_get_state(dev, adev, system_state, NULL, &state); 886fa1675b5SRafael J. Wysocki return ret ? ret : acpi_device_set_power(adev, state); 887e5cc8ef3SRafael J. Wysocki } 888e5cc8ef3SRafael J. Wysocki 889e5cc8ef3SRafael J. Wysocki /** 890e5cc8ef3SRafael J. Wysocki * acpi_dev_pm_full_power - Put ACPI device into the full-power state. 891e5cc8ef3SRafael J. Wysocki * @adev: ACPI device node to put into the full-power state. 892e5cc8ef3SRafael J. Wysocki */ 893e5cc8ef3SRafael J. Wysocki static int acpi_dev_pm_full_power(struct acpi_device *adev) 894e5cc8ef3SRafael J. Wysocki { 895e5cc8ef3SRafael J. Wysocki return acpi_device_power_manageable(adev) ? 896e5cc8ef3SRafael J. Wysocki acpi_device_set_power(adev, ACPI_STATE_D0) : 0; 897e5cc8ef3SRafael J. Wysocki } 898e5cc8ef3SRafael J. Wysocki 899e5cc8ef3SRafael J. Wysocki /** 900cbe25ce3SRafael J. Wysocki * acpi_dev_suspend - Put device into a low-power state using ACPI. 901e5cc8ef3SRafael J. Wysocki * @dev: Device to put into a low-power state. 902cbe25ce3SRafael J. Wysocki * @wakeup: Whether or not to enable wakeup for the device. 903e5cc8ef3SRafael J. Wysocki * 904cbe25ce3SRafael J. Wysocki * Put the given device into a low-power state using the standard ACPI 905e5cc8ef3SRafael J. Wysocki * mechanism. Set up remote wakeup if desired, choose the state to put the 906e5cc8ef3SRafael J. Wysocki * device into (this checks if remote wakeup is expected to work too), and set 907e5cc8ef3SRafael J. Wysocki * the power state of the device. 908e5cc8ef3SRafael J. Wysocki */ 909cbe25ce3SRafael J. Wysocki int acpi_dev_suspend(struct device *dev, bool wakeup) 910e5cc8ef3SRafael J. Wysocki { 91179c0373fSRafael J. Wysocki struct acpi_device *adev = ACPI_COMPANION(dev); 912cbe25ce3SRafael J. Wysocki u32 target_state = acpi_target_system_state(); 913e5cc8ef3SRafael J. Wysocki int error; 914e5cc8ef3SRafael J. Wysocki 915e5cc8ef3SRafael J. Wysocki if (!adev) 916e5cc8ef3SRafael J. Wysocki return 0; 917e5cc8ef3SRafael J. Wysocki 918cbe25ce3SRafael J. Wysocki if (wakeup && acpi_device_can_wakeup(adev)) { 919cbe25ce3SRafael J. Wysocki error = acpi_device_wakeup_enable(adev, target_state); 92099d8845eSRafael J. Wysocki if (error) 921e5cc8ef3SRafael J. Wysocki return -EAGAIN; 922cbe25ce3SRafael J. Wysocki } else { 923cbe25ce3SRafael J. Wysocki wakeup = false; 92499d8845eSRafael J. Wysocki } 925e5cc8ef3SRafael J. Wysocki 926cbe25ce3SRafael J. Wysocki error = acpi_dev_pm_low_power(dev, adev, target_state); 927cbe25ce3SRafael J. Wysocki if (error && wakeup) 92899d8845eSRafael J. Wysocki acpi_device_wakeup_disable(adev); 929e5cc8ef3SRafael J. Wysocki 930e5cc8ef3SRafael J. Wysocki return error; 931e5cc8ef3SRafael J. Wysocki } 932cbe25ce3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_suspend); 933e5cc8ef3SRafael J. Wysocki 934e5cc8ef3SRafael J. Wysocki /** 93563705c40SRafael J. Wysocki * acpi_dev_resume - Put device into the full-power state using ACPI. 936e5cc8ef3SRafael J. Wysocki * @dev: Device to put into the full-power state. 937e5cc8ef3SRafael J. Wysocki * 938e5cc8ef3SRafael J. Wysocki * Put the given device into the full-power state using the standard ACPI 93963705c40SRafael J. Wysocki * mechanism. Set the power state of the device to ACPI D0 and disable wakeup. 940e5cc8ef3SRafael J. Wysocki */ 94163705c40SRafael J. Wysocki int acpi_dev_resume(struct device *dev) 942e5cc8ef3SRafael J. Wysocki { 94379c0373fSRafael J. Wysocki struct acpi_device *adev = ACPI_COMPANION(dev); 944e5cc8ef3SRafael J. Wysocki int error; 945e5cc8ef3SRafael J. Wysocki 946e5cc8ef3SRafael J. Wysocki if (!adev) 947e5cc8ef3SRafael J. Wysocki return 0; 948e5cc8ef3SRafael J. Wysocki 949e5cc8ef3SRafael J. Wysocki error = acpi_dev_pm_full_power(adev); 95099d8845eSRafael J. Wysocki acpi_device_wakeup_disable(adev); 951e5cc8ef3SRafael J. Wysocki return error; 952e5cc8ef3SRafael J. Wysocki } 95363705c40SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_resume); 954e5cc8ef3SRafael J. Wysocki 955e5cc8ef3SRafael J. Wysocki /** 956e5cc8ef3SRafael J. Wysocki * acpi_subsys_runtime_suspend - Suspend device using ACPI. 957e5cc8ef3SRafael J. Wysocki * @dev: Device to suspend. 958e5cc8ef3SRafael J. Wysocki * 959e5cc8ef3SRafael J. Wysocki * Carry out the generic runtime suspend procedure for @dev and use ACPI to put 960e5cc8ef3SRafael J. Wysocki * it into a runtime low-power state. 961e5cc8ef3SRafael J. Wysocki */ 962e5cc8ef3SRafael J. Wysocki int acpi_subsys_runtime_suspend(struct device *dev) 963e5cc8ef3SRafael J. Wysocki { 964e5cc8ef3SRafael J. Wysocki int ret = pm_generic_runtime_suspend(dev); 9653da8236bSXiaofei Tan 966cbe25ce3SRafael J. Wysocki return ret ? ret : acpi_dev_suspend(dev, true); 967e5cc8ef3SRafael J. Wysocki } 968e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend); 969e5cc8ef3SRafael J. Wysocki 970e5cc8ef3SRafael J. Wysocki /** 971e5cc8ef3SRafael J. Wysocki * acpi_subsys_runtime_resume - Resume device using ACPI. 972e5cc8ef3SRafael J. Wysocki * @dev: Device to Resume. 973e5cc8ef3SRafael J. Wysocki * 974e5cc8ef3SRafael J. Wysocki * Use ACPI to put the given device into the full-power state and carry out the 975e5cc8ef3SRafael J. Wysocki * generic runtime resume procedure for it. 976e5cc8ef3SRafael J. Wysocki */ 977e5cc8ef3SRafael J. Wysocki int acpi_subsys_runtime_resume(struct device *dev) 978e5cc8ef3SRafael J. Wysocki { 97963705c40SRafael J. Wysocki int ret = acpi_dev_resume(dev); 9803da8236bSXiaofei Tan 981e5cc8ef3SRafael J. Wysocki return ret ? ret : pm_generic_runtime_resume(dev); 982e5cc8ef3SRafael J. Wysocki } 983e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume); 984e5cc8ef3SRafael J. Wysocki 985e5cc8ef3SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP 986c2ebf788SUlf Hansson static bool acpi_dev_needs_resume(struct device *dev, struct acpi_device *adev) 987c2ebf788SUlf Hansson { 988c2ebf788SUlf Hansson u32 sys_target = acpi_target_system_state(); 989c2ebf788SUlf Hansson int ret, state; 990c2ebf788SUlf Hansson 9919a51c6b1SRafael J. Wysocki if (!pm_runtime_suspended(dev) || !adev || (adev->wakeup.flags.valid && 9929a51c6b1SRafael J. Wysocki device_may_wakeup(dev) != !!adev->wakeup.prepare_count)) 993c2ebf788SUlf Hansson return true; 994c2ebf788SUlf Hansson 995c2ebf788SUlf Hansson if (sys_target == ACPI_STATE_S0) 996c2ebf788SUlf Hansson return false; 997c2ebf788SUlf Hansson 998c2ebf788SUlf Hansson if (adev->power.flags.dsw_present) 999c2ebf788SUlf Hansson return true; 1000c2ebf788SUlf Hansson 1001c2ebf788SUlf Hansson ret = acpi_dev_pm_get_state(dev, adev, sys_target, NULL, &state); 1002c2ebf788SUlf Hansson if (ret) 1003c2ebf788SUlf Hansson return true; 1004c2ebf788SUlf Hansson 1005c2ebf788SUlf Hansson return state != adev->power.state; 1006c2ebf788SUlf Hansson } 1007c2ebf788SUlf Hansson 1008e5cc8ef3SRafael J. Wysocki /** 1009e5cc8ef3SRafael J. Wysocki * acpi_subsys_prepare - Prepare device for system transition to a sleep state. 1010e5cc8ef3SRafael J. Wysocki * @dev: Device to prepare. 1011e5cc8ef3SRafael J. Wysocki */ 1012e5cc8ef3SRafael J. Wysocki int acpi_subsys_prepare(struct device *dev) 1013e5cc8ef3SRafael J. Wysocki { 1014f25c0ae2SRafael J. Wysocki struct acpi_device *adev = ACPI_COMPANION(dev); 101592858c47SRafael J. Wysocki 101608810a41SRafael J. Wysocki if (dev->driver && dev->driver->pm && dev->driver->pm->prepare) { 101708810a41SRafael J. Wysocki int ret = dev->driver->pm->prepare(dev); 101808810a41SRafael J. Wysocki 1019f25c0ae2SRafael J. Wysocki if (ret < 0) 1020f25c0ae2SRafael J. Wysocki return ret; 1021f25c0ae2SRafael J. Wysocki 102208810a41SRafael J. Wysocki if (!ret && dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_PREPARE)) 1023f25c0ae2SRafael J. Wysocki return 0; 102408810a41SRafael J. Wysocki } 1025f25c0ae2SRafael J. Wysocki 1026c2ebf788SUlf Hansson return !acpi_dev_needs_resume(dev, adev); 1027e5cc8ef3SRafael J. Wysocki } 1028e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_prepare); 1029e5cc8ef3SRafael J. Wysocki 1030e5cc8ef3SRafael J. Wysocki /** 1031e4da817dSUlf Hansson * acpi_subsys_complete - Finalize device's resume during system resume. 1032e4da817dSUlf Hansson * @dev: Device to handle. 1033e4da817dSUlf Hansson */ 1034e4da817dSUlf Hansson void acpi_subsys_complete(struct device *dev) 1035e4da817dSUlf Hansson { 1036e4da817dSUlf Hansson pm_generic_complete(dev); 1037e4da817dSUlf Hansson /* 1038e4da817dSUlf Hansson * If the device had been runtime-suspended before the system went into 1039e4da817dSUlf Hansson * the sleep state it is going out of and it has never been resumed till 1040e4da817dSUlf Hansson * now, resume it in case the firmware powered it up. 1041e4da817dSUlf Hansson */ 1042db68daffSRafael J. Wysocki if (pm_runtime_suspended(dev) && pm_resume_via_firmware()) 1043e4da817dSUlf Hansson pm_request_resume(dev); 1044e4da817dSUlf Hansson } 1045e4da817dSUlf Hansson EXPORT_SYMBOL_GPL(acpi_subsys_complete); 1046e4da817dSUlf Hansson 1047e4da817dSUlf Hansson /** 104892858c47SRafael J. Wysocki * acpi_subsys_suspend - Run the device driver's suspend callback. 104992858c47SRafael J. Wysocki * @dev: Device to handle. 105092858c47SRafael J. Wysocki * 105105087360SRafael J. Wysocki * Follow PCI and resume devices from runtime suspend before running their 105205087360SRafael J. Wysocki * system suspend callbacks, unless the driver can cope with runtime-suspended 105305087360SRafael J. Wysocki * devices during system suspend and there are no ACPI-specific reasons for 105405087360SRafael J. Wysocki * resuming them. 105592858c47SRafael J. Wysocki */ 105692858c47SRafael J. Wysocki int acpi_subsys_suspend(struct device *dev) 105792858c47SRafael J. Wysocki { 105805087360SRafael J. Wysocki if (!dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND) || 105905087360SRafael J. Wysocki acpi_dev_needs_resume(dev, ACPI_COMPANION(dev))) 106092858c47SRafael J. Wysocki pm_runtime_resume(dev); 106105087360SRafael J. Wysocki 106292858c47SRafael J. Wysocki return pm_generic_suspend(dev); 106392858c47SRafael J. Wysocki } 10644cf563c5SHeikki Krogerus EXPORT_SYMBOL_GPL(acpi_subsys_suspend); 106592858c47SRafael J. Wysocki 106692858c47SRafael J. Wysocki /** 1067e5cc8ef3SRafael J. Wysocki * acpi_subsys_suspend_late - Suspend device using ACPI. 1068e5cc8ef3SRafael J. Wysocki * @dev: Device to suspend. 1069e5cc8ef3SRafael J. Wysocki * 1070e5cc8ef3SRafael J. Wysocki * Carry out the generic late suspend procedure for @dev and use ACPI to put 1071e5cc8ef3SRafael J. Wysocki * it into a low-power state during system transition into a sleep state. 1072e5cc8ef3SRafael J. Wysocki */ 1073e5cc8ef3SRafael J. Wysocki int acpi_subsys_suspend_late(struct device *dev) 1074e5cc8ef3SRafael J. Wysocki { 107505087360SRafael J. Wysocki int ret; 107605087360SRafael J. Wysocki 1077fa2bfeadSRafael J. Wysocki if (dev_pm_skip_suspend(dev)) 107805087360SRafael J. Wysocki return 0; 107905087360SRafael J. Wysocki 108005087360SRafael J. Wysocki ret = pm_generic_suspend_late(dev); 1081cbe25ce3SRafael J. Wysocki return ret ? ret : acpi_dev_suspend(dev, device_may_wakeup(dev)); 1082e5cc8ef3SRafael J. Wysocki } 1083e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late); 1084e5cc8ef3SRafael J. Wysocki 1085e5cc8ef3SRafael J. Wysocki /** 108605087360SRafael J. Wysocki * acpi_subsys_suspend_noirq - Run the device driver's "noirq" suspend callback. 108705087360SRafael J. Wysocki * @dev: Device to suspend. 108805087360SRafael J. Wysocki */ 108905087360SRafael J. Wysocki int acpi_subsys_suspend_noirq(struct device *dev) 109005087360SRafael J. Wysocki { 1091db68daffSRafael J. Wysocki int ret; 109205087360SRafael J. Wysocki 1093fa2bfeadSRafael J. Wysocki if (dev_pm_skip_suspend(dev)) 1094db68daffSRafael J. Wysocki return 0; 1095db68daffSRafael J. Wysocki 1096db68daffSRafael J. Wysocki ret = pm_generic_suspend_noirq(dev); 1097db68daffSRafael J. Wysocki if (ret) 1098db68daffSRafael J. Wysocki return ret; 1099db68daffSRafael J. Wysocki 1100db68daffSRafael J. Wysocki /* 1101db68daffSRafael J. Wysocki * If the target system sleep state is suspend-to-idle, it is sufficient 1102db68daffSRafael J. Wysocki * to check whether or not the device's wakeup settings are good for 1103db68daffSRafael J. Wysocki * runtime PM. Otherwise, the pm_resume_via_firmware() check will cause 1104db68daffSRafael J. Wysocki * acpi_subsys_complete() to take care of fixing up the device's state 1105db68daffSRafael J. Wysocki * anyway, if need be. 1106db68daffSRafael J. Wysocki */ 11070fe8a1beSRafael J. Wysocki if (device_can_wakeup(dev) && !device_may_wakeup(dev)) 11080fe8a1beSRafael J. Wysocki dev->power.may_skip_resume = false; 1109db68daffSRafael J. Wysocki 1110db68daffSRafael J. Wysocki return 0; 111105087360SRafael J. Wysocki } 111205087360SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_suspend_noirq); 111305087360SRafael J. Wysocki 111405087360SRafael J. Wysocki /** 111505087360SRafael J. Wysocki * acpi_subsys_resume_noirq - Run the device driver's "noirq" resume callback. 111605087360SRafael J. Wysocki * @dev: Device to handle. 111705087360SRafael J. Wysocki */ 11183cd7957eSRafael J. Wysocki static int acpi_subsys_resume_noirq(struct device *dev) 111905087360SRafael J. Wysocki { 112076c70cb5SRafael J. Wysocki if (dev_pm_skip_resume(dev)) 1121db68daffSRafael J. Wysocki return 0; 1122db68daffSRafael J. Wysocki 112305087360SRafael J. Wysocki return pm_generic_resume_noirq(dev); 112405087360SRafael J. Wysocki } 112505087360SRafael J. Wysocki 112605087360SRafael J. Wysocki /** 1127e5cc8ef3SRafael J. Wysocki * acpi_subsys_resume_early - Resume device using ACPI. 1128e5cc8ef3SRafael J. Wysocki * @dev: Device to Resume. 1129e5cc8ef3SRafael J. Wysocki * 1130e5cc8ef3SRafael J. Wysocki * Use ACPI to put the given device into the full-power state and carry out the 1131e5cc8ef3SRafael J. Wysocki * generic early resume procedure for it during system transition into the 1132f7599be2SDmitry Torokhov * working state, but only do that if device either defines early resume 1133f7599be2SDmitry Torokhov * handler, or does not define power operations at all. Otherwise powering up 1134f7599be2SDmitry Torokhov * of the device is postponed to the normal resume phase. 1135e5cc8ef3SRafael J. Wysocki */ 11363cd7957eSRafael J. Wysocki static int acpi_subsys_resume_early(struct device *dev) 1137e5cc8ef3SRafael J. Wysocki { 1138f7599be2SDmitry Torokhov const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; 11396e176bf8SRafael J. Wysocki int ret; 11406e176bf8SRafael J. Wysocki 114176c70cb5SRafael J. Wysocki if (dev_pm_skip_resume(dev)) 11426e176bf8SRafael J. Wysocki return 0; 11436e176bf8SRafael J. Wysocki 1144f7599be2SDmitry Torokhov if (pm && !pm->resume_early) { 1145f7599be2SDmitry Torokhov dev_dbg(dev, "postponing D0 transition to normal resume stage\n"); 1146f7599be2SDmitry Torokhov return 0; 1147f7599be2SDmitry Torokhov } 1148f7599be2SDmitry Torokhov 11496e176bf8SRafael J. Wysocki ret = acpi_dev_resume(dev); 1150e5cc8ef3SRafael J. Wysocki return ret ? ret : pm_generic_resume_early(dev); 1151e5cc8ef3SRafael J. Wysocki } 115292858c47SRafael J. Wysocki 115392858c47SRafael J. Wysocki /** 1154f7599be2SDmitry Torokhov * acpi_subsys_resume - Resume device using ACPI. 1155f7599be2SDmitry Torokhov * @dev: Device to Resume. 1156f7599be2SDmitry Torokhov * 1157f7599be2SDmitry Torokhov * Use ACPI to put the given device into the full-power state if it has not been 1158f7599be2SDmitry Torokhov * powered up during early resume phase, and carry out the generic resume 1159f7599be2SDmitry Torokhov * procedure for it during system transition into the working state. 1160f7599be2SDmitry Torokhov */ 1161f7599be2SDmitry Torokhov static int acpi_subsys_resume(struct device *dev) 1162f7599be2SDmitry Torokhov { 1163f7599be2SDmitry Torokhov const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; 1164f7599be2SDmitry Torokhov int ret = 0; 1165f7599be2SDmitry Torokhov 1166f7599be2SDmitry Torokhov if (!dev_pm_skip_resume(dev) && pm && !pm->resume_early) { 1167f7599be2SDmitry Torokhov dev_dbg(dev, "executing postponed D0 transition\n"); 1168f7599be2SDmitry Torokhov ret = acpi_dev_resume(dev); 1169f7599be2SDmitry Torokhov } 1170f7599be2SDmitry Torokhov 1171f7599be2SDmitry Torokhov return ret ? ret : pm_generic_resume(dev); 1172f7599be2SDmitry Torokhov } 1173f7599be2SDmitry Torokhov 1174f7599be2SDmitry Torokhov /** 117592858c47SRafael J. Wysocki * acpi_subsys_freeze - Run the device driver's freeze callback. 117692858c47SRafael J. Wysocki * @dev: Device to handle. 117792858c47SRafael J. Wysocki */ 117892858c47SRafael J. Wysocki int acpi_subsys_freeze(struct device *dev) 117992858c47SRafael J. Wysocki { 118092858c47SRafael J. Wysocki /* 1181501debd4SRafael J. Wysocki * Resume all runtime-suspended devices before creating a snapshot 1182501debd4SRafael J. Wysocki * image of system memory, because the restore kernel generally cannot 1183501debd4SRafael J. Wysocki * be expected to always handle them consistently and they need to be 1184501debd4SRafael J. Wysocki * put into the runtime-active metastate during system resume anyway, 1185501debd4SRafael J. Wysocki * so it is better to ensure that the state saved in the image will be 1186501debd4SRafael J. Wysocki * always consistent with that. 118792858c47SRafael J. Wysocki */ 118892858c47SRafael J. Wysocki pm_runtime_resume(dev); 118905087360SRafael J. Wysocki 119092858c47SRafael J. Wysocki return pm_generic_freeze(dev); 119192858c47SRafael J. Wysocki } 11924cf563c5SHeikki Krogerus EXPORT_SYMBOL_GPL(acpi_subsys_freeze); 119392858c47SRafael J. Wysocki 119405087360SRafael J. Wysocki /** 11953cd7957eSRafael J. Wysocki * acpi_subsys_restore_early - Restore device using ACPI. 11963cd7957eSRafael J. Wysocki * @dev: Device to restore. 119705087360SRafael J. Wysocki */ 11983cd7957eSRafael J. Wysocki int acpi_subsys_restore_early(struct device *dev) 119905087360SRafael J. Wysocki { 12003cd7957eSRafael J. Wysocki int ret = acpi_dev_resume(dev); 12013da8236bSXiaofei Tan 12023cd7957eSRafael J. Wysocki return ret ? ret : pm_generic_restore_early(dev); 120305087360SRafael J. Wysocki } 12043cd7957eSRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_restore_early); 1205c95b7595SRafael J. Wysocki 1206c95b7595SRafael J. Wysocki /** 1207c95b7595SRafael J. Wysocki * acpi_subsys_poweroff - Run the device driver's poweroff callback. 1208c95b7595SRafael J. Wysocki * @dev: Device to handle. 1209c95b7595SRafael J. Wysocki * 1210c95b7595SRafael J. Wysocki * Follow PCI and resume devices from runtime suspend before running their 1211c95b7595SRafael J. Wysocki * system poweroff callbacks, unless the driver can cope with runtime-suspended 1212c95b7595SRafael J. Wysocki * devices during system suspend and there are no ACPI-specific reasons for 1213c95b7595SRafael J. Wysocki * resuming them. 1214c95b7595SRafael J. Wysocki */ 1215c95b7595SRafael J. Wysocki int acpi_subsys_poweroff(struct device *dev) 1216c95b7595SRafael J. Wysocki { 1217c95b7595SRafael J. Wysocki if (!dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND) || 1218c95b7595SRafael J. Wysocki acpi_dev_needs_resume(dev, ACPI_COMPANION(dev))) 1219c95b7595SRafael J. Wysocki pm_runtime_resume(dev); 1220c95b7595SRafael J. Wysocki 1221c95b7595SRafael J. Wysocki return pm_generic_poweroff(dev); 1222c95b7595SRafael J. Wysocki } 1223c95b7595SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_poweroff); 1224c95b7595SRafael J. Wysocki 1225c95b7595SRafael J. Wysocki /** 1226c95b7595SRafael J. Wysocki * acpi_subsys_poweroff_late - Run the device driver's poweroff callback. 1227c95b7595SRafael J. Wysocki * @dev: Device to handle. 1228c95b7595SRafael J. Wysocki * 1229c95b7595SRafael J. Wysocki * Carry out the generic late poweroff procedure for @dev and use ACPI to put 1230c95b7595SRafael J. Wysocki * it into a low-power state during system transition into a sleep state. 1231c95b7595SRafael J. Wysocki */ 1232c95b7595SRafael J. Wysocki static int acpi_subsys_poweroff_late(struct device *dev) 1233c95b7595SRafael J. Wysocki { 1234c95b7595SRafael J. Wysocki int ret; 123505087360SRafael J. Wysocki 1236fa2bfeadSRafael J. Wysocki if (dev_pm_skip_suspend(dev)) 123705087360SRafael J. Wysocki return 0; 123805087360SRafael J. Wysocki 1239c95b7595SRafael J. Wysocki ret = pm_generic_poweroff_late(dev); 1240c95b7595SRafael J. Wysocki if (ret) 1241c95b7595SRafael J. Wysocki return ret; 1242c95b7595SRafael J. Wysocki 1243c95b7595SRafael J. Wysocki return acpi_dev_suspend(dev, device_may_wakeup(dev)); 124405087360SRafael J. Wysocki } 124505087360SRafael J. Wysocki 124605087360SRafael J. Wysocki /** 1247c95b7595SRafael J. Wysocki * acpi_subsys_poweroff_noirq - Run the driver's "noirq" poweroff callback. 1248c95b7595SRafael J. Wysocki * @dev: Device to suspend. 124905087360SRafael J. Wysocki */ 1250c95b7595SRafael J. Wysocki static int acpi_subsys_poweroff_noirq(struct device *dev) 125105087360SRafael J. Wysocki { 1252fa2bfeadSRafael J. Wysocki if (dev_pm_skip_suspend(dev)) 125305087360SRafael J. Wysocki return 0; 125405087360SRafael J. Wysocki 1255c95b7595SRafael J. Wysocki return pm_generic_poweroff_noirq(dev); 125605087360SRafael J. Wysocki } 1257e5cc8ef3SRafael J. Wysocki #endif /* CONFIG_PM_SLEEP */ 1258e5cc8ef3SRafael J. Wysocki 1259e5cc8ef3SRafael J. Wysocki static struct dev_pm_domain acpi_general_pm_domain = { 1260e5cc8ef3SRafael J. Wysocki .ops = { 1261e5cc8ef3SRafael J. Wysocki .runtime_suspend = acpi_subsys_runtime_suspend, 1262e5cc8ef3SRafael J. Wysocki .runtime_resume = acpi_subsys_runtime_resume, 1263e5cc8ef3SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP 1264e5cc8ef3SRafael J. Wysocki .prepare = acpi_subsys_prepare, 1265e4da817dSUlf Hansson .complete = acpi_subsys_complete, 126692858c47SRafael J. Wysocki .suspend = acpi_subsys_suspend, 1267f7599be2SDmitry Torokhov .resume = acpi_subsys_resume, 1268e5cc8ef3SRafael J. Wysocki .suspend_late = acpi_subsys_suspend_late, 126905087360SRafael J. Wysocki .suspend_noirq = acpi_subsys_suspend_noirq, 127005087360SRafael J. Wysocki .resume_noirq = acpi_subsys_resume_noirq, 1271e5cc8ef3SRafael J. Wysocki .resume_early = acpi_subsys_resume_early, 127292858c47SRafael J. Wysocki .freeze = acpi_subsys_freeze, 1273c95b7595SRafael J. Wysocki .poweroff = acpi_subsys_poweroff, 1274c95b7595SRafael J. Wysocki .poweroff_late = acpi_subsys_poweroff_late, 1275c95b7595SRafael J. Wysocki .poweroff_noirq = acpi_subsys_poweroff_noirq, 12763cd7957eSRafael J. Wysocki .restore_early = acpi_subsys_restore_early, 1277e5cc8ef3SRafael J. Wysocki #endif 1278e5cc8ef3SRafael J. Wysocki }, 1279e5cc8ef3SRafael J. Wysocki }; 1280e5cc8ef3SRafael J. Wysocki 1281e5cc8ef3SRafael J. Wysocki /** 128291d66cd2SUlf Hansson * acpi_dev_pm_detach - Remove ACPI power management from the device. 128391d66cd2SUlf Hansson * @dev: Device to take care of. 128491d66cd2SUlf Hansson * @power_off: Whether or not to try to remove power from the device. 128591d66cd2SUlf Hansson * 128691d66cd2SUlf Hansson * Remove the device from the general ACPI PM domain and remove its wakeup 128791d66cd2SUlf Hansson * notifier. If @power_off is set, additionally remove power from the device if 128891d66cd2SUlf Hansson * possible. 128991d66cd2SUlf Hansson * 129091d66cd2SUlf Hansson * Callers must ensure proper synchronization of this function with power 129191d66cd2SUlf Hansson * management callbacks. 129291d66cd2SUlf Hansson */ 129391d66cd2SUlf Hansson static void acpi_dev_pm_detach(struct device *dev, bool power_off) 129491d66cd2SUlf Hansson { 129591d66cd2SUlf Hansson struct acpi_device *adev = ACPI_COMPANION(dev); 129691d66cd2SUlf Hansson 129791d66cd2SUlf Hansson if (adev && dev->pm_domain == &acpi_general_pm_domain) { 1298989561deSTomeu Vizoso dev_pm_domain_set(dev, NULL); 129991d66cd2SUlf Hansson acpi_remove_pm_notifier(adev); 130091d66cd2SUlf Hansson if (power_off) { 130191d66cd2SUlf Hansson /* 130291d66cd2SUlf Hansson * If the device's PM QoS resume latency limit or flags 130391d66cd2SUlf Hansson * have been exposed to user space, they have to be 130491d66cd2SUlf Hansson * hidden at this point, so that they don't affect the 130591d66cd2SUlf Hansson * choice of the low-power state to put the device into. 130691d66cd2SUlf Hansson */ 130791d66cd2SUlf Hansson dev_pm_qos_hide_latency_limit(dev); 130891d66cd2SUlf Hansson dev_pm_qos_hide_flags(dev); 130999d8845eSRafael J. Wysocki acpi_device_wakeup_disable(adev); 131091d66cd2SUlf Hansson acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0); 131191d66cd2SUlf Hansson } 131291d66cd2SUlf Hansson } 131391d66cd2SUlf Hansson } 131491d66cd2SUlf Hansson 131591d66cd2SUlf Hansson /** 1316e5cc8ef3SRafael J. Wysocki * acpi_dev_pm_attach - Prepare device for ACPI power management. 1317e5cc8ef3SRafael J. Wysocki * @dev: Device to prepare. 1318b88ce2a4SRafael J. Wysocki * @power_on: Whether or not to power on the device. 1319e5cc8ef3SRafael J. Wysocki * 1320e5cc8ef3SRafael J. Wysocki * If @dev has a valid ACPI handle that has a valid struct acpi_device object 1321e5cc8ef3SRafael J. Wysocki * attached to it, install a wakeup notification handler for the device and 1322b88ce2a4SRafael J. Wysocki * add it to the general ACPI PM domain. If @power_on is set, the device will 1323b88ce2a4SRafael J. Wysocki * be put into the ACPI D0 state before the function returns. 1324e5cc8ef3SRafael J. Wysocki * 1325e5cc8ef3SRafael J. Wysocki * This assumes that the @dev's bus type uses generic power management callbacks 1326e5cc8ef3SRafael J. Wysocki * (or doesn't use any power management callbacks at all). 1327e5cc8ef3SRafael J. Wysocki * 1328e5cc8ef3SRafael J. Wysocki * Callers must ensure proper synchronization of this function with power 1329e5cc8ef3SRafael J. Wysocki * management callbacks. 1330e5cc8ef3SRafael J. Wysocki */ 1331b88ce2a4SRafael J. Wysocki int acpi_dev_pm_attach(struct device *dev, bool power_on) 1332e5cc8ef3SRafael J. Wysocki { 1333b9ea0baeSRafael J. Wysocki /* 1334b9ea0baeSRafael J. Wysocki * Skip devices whose ACPI companions match the device IDs below, 1335b9ea0baeSRafael J. Wysocki * because they require special power management handling incompatible 1336b9ea0baeSRafael J. Wysocki * with the generic ACPI PM domain. 1337b9ea0baeSRafael J. Wysocki */ 1338b9ea0baeSRafael J. Wysocki static const struct acpi_device_id special_pm_ids[] = { 1339b9370dceSRafael J. Wysocki ACPI_FAN_DEVICE_IDS, 1340b9ea0baeSRafael J. Wysocki {} 1341b9ea0baeSRafael J. Wysocki }; 134279c0373fSRafael J. Wysocki struct acpi_device *adev = ACPI_COMPANION(dev); 1343e5cc8ef3SRafael J. Wysocki 1344b9ea0baeSRafael J. Wysocki if (!adev || !acpi_match_device_ids(adev, special_pm_ids)) 1345919b7308SUlf Hansson return 0; 1346e5cc8ef3SRafael J. Wysocki 1347712e960fSMika Westerberg /* 1348712e960fSMika Westerberg * Only attach the power domain to the first device if the 1349712e960fSMika Westerberg * companion is shared by multiple. This is to prevent doing power 1350712e960fSMika Westerberg * management twice. 1351712e960fSMika Westerberg */ 1352712e960fSMika Westerberg if (!acpi_device_is_first_physical_node(adev, dev)) 1353919b7308SUlf Hansson return 0; 1354712e960fSMika Westerberg 1355c072530fSRafael J. Wysocki acpi_add_pm_notifier(adev, dev, acpi_pm_notify_work_func); 1356989561deSTomeu Vizoso dev_pm_domain_set(dev, &acpi_general_pm_domain); 1357b88ce2a4SRafael J. Wysocki if (power_on) { 1358b88ce2a4SRafael J. Wysocki acpi_dev_pm_full_power(adev); 135999d8845eSRafael J. Wysocki acpi_device_wakeup_disable(adev); 1360b88ce2a4SRafael J. Wysocki } 136186f1e15fSUlf Hansson 136286f1e15fSUlf Hansson dev->pm_domain->detach = acpi_dev_pm_detach; 1363919b7308SUlf Hansson return 1; 1364e5cc8ef3SRafael J. Wysocki } 1365e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_pm_attach); 13662744d7a0SMario Limonciello 13672744d7a0SMario Limonciello /** 13682744d7a0SMario Limonciello * acpi_storage_d3 - Check if D3 should be used in the suspend path 13692744d7a0SMario Limonciello * @dev: Device to check 13702744d7a0SMario Limonciello * 13712744d7a0SMario Limonciello * Return %true if the platform firmware wants @dev to be programmed 13722744d7a0SMario Limonciello * into D3hot or D3cold (if supported) in the suspend path, or %false 13732744d7a0SMario Limonciello * when there is no specific preference. On some platforms, if this 13742744d7a0SMario Limonciello * hint is ignored, @dev may remain unresponsive after suspending the 13752744d7a0SMario Limonciello * platform as a whole. 13762744d7a0SMario Limonciello * 13772744d7a0SMario Limonciello * Although the property has storage in the name it actually is 13782744d7a0SMario Limonciello * applied to the PCIe slot and plugging in a non-storage device the 13792744d7a0SMario Limonciello * same platform restrictions will likely apply. 13802744d7a0SMario Limonciello */ 13812744d7a0SMario Limonciello bool acpi_storage_d3(struct device *dev) 13822744d7a0SMario Limonciello { 13832744d7a0SMario Limonciello struct acpi_device *adev = ACPI_COMPANION(dev); 13842744d7a0SMario Limonciello u8 val; 13852744d7a0SMario Limonciello 13866485fc18SMario Limonciello if (force_storage_d3()) 13876485fc18SMario Limonciello return true; 13886485fc18SMario Limonciello 13892744d7a0SMario Limonciello if (!adev) 13902744d7a0SMario Limonciello return false; 13912744d7a0SMario Limonciello if (fwnode_property_read_u8(acpi_fwnode_handle(adev), "StorageD3Enable", 13922744d7a0SMario Limonciello &val)) 13932744d7a0SMario Limonciello return false; 13942744d7a0SMario Limonciello return val == 1; 13952744d7a0SMario Limonciello } 13962744d7a0SMario Limonciello EXPORT_SYMBOL_GPL(acpi_storage_d3); 13972744d7a0SMario Limonciello 1398b82a7df4SSakari Ailus /** 1399b82a7df4SSakari Ailus * acpi_dev_state_d0 - Tell if the device is in D0 power state 1400b82a7df4SSakari Ailus * @dev: Physical device the ACPI power state of which to check 1401b82a7df4SSakari Ailus * 1402b82a7df4SSakari Ailus * On a system without ACPI, return true. On a system with ACPI, return true if 1403b82a7df4SSakari Ailus * the current ACPI power state of the device is D0, or false otherwise. 1404b82a7df4SSakari Ailus * 1405b82a7df4SSakari Ailus * Note that the power state of a device is not well-defined after it has been 1406b82a7df4SSakari Ailus * passed to acpi_device_set_power() and before that function returns, so it is 1407b82a7df4SSakari Ailus * not valid to ask for the ACPI power state of the device in that time frame. 1408b82a7df4SSakari Ailus * 1409b82a7df4SSakari Ailus * This function is intended to be used in a driver's probe or remove 1410b82a7df4SSakari Ailus * function. See Documentation/firmware-guide/acpi/low-power-probe.rst for 1411b82a7df4SSakari Ailus * more information. 1412b82a7df4SSakari Ailus */ 1413b82a7df4SSakari Ailus bool acpi_dev_state_d0(struct device *dev) 1414b82a7df4SSakari Ailus { 1415b82a7df4SSakari Ailus struct acpi_device *adev = ACPI_COMPANION(dev); 1416b82a7df4SSakari Ailus 1417b82a7df4SSakari Ailus if (!adev) 1418b82a7df4SSakari Ailus return true; 1419b82a7df4SSakari Ailus 1420b82a7df4SSakari Ailus return adev->power.state == ACPI_STATE_D0; 1421b82a7df4SSakari Ailus } 1422b82a7df4SSakari Ailus EXPORT_SYMBOL_GPL(acpi_dev_state_d0); 1423b82a7df4SSakari Ailus 1424ec4602a9SRafael J. Wysocki #endif /* CONFIG_PM */ 1425