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 13255a04ccSRafael 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. */ 1766dd4a29dSRafael J. Wysocki if (state > ACPI_STATE_D0 && state == device->power.state) 1776dd4a29dSRafael J. Wysocki goto no_change; 1789ce4e607SRafael J. Wysocki 17920dacb71SRafael J. Wysocki if (state == ACPI_STATE_D3_COLD) { 18020dacb71SRafael J. Wysocki /* 18120dacb71SRafael J. Wysocki * For transitions to D3cold we need to execute _PS3 and then 18220dacb71SRafael J. Wysocki * possibly drop references to the power resources in use. 18320dacb71SRafael J. Wysocki */ 18420dacb71SRafael J. Wysocki state = ACPI_STATE_D3_HOT; 185956ad9d9SRafael J. Wysocki /* If D3cold is not supported, use D3hot as the target state. */ 18620dacb71SRafael J. Wysocki if (!device->power.states[ACPI_STATE_D3_COLD].flags.valid) 18720dacb71SRafael J. Wysocki target_state = state; 18820dacb71SRafael J. Wysocki } else if (!device->power.states[state].flags.valid) { 189f4f3548dSRafael J. Wysocki acpi_handle_debug(device->handle, "Power state %s not supported\n", 1909ce4e607SRafael J. Wysocki acpi_power_state_string(state)); 1919ce4e607SRafael J. Wysocki return -ENODEV; 1929ce4e607SRafael J. Wysocki } 19320dacb71SRafael J. Wysocki 194f4f3548dSRafael J. Wysocki if (!device->power.flags.ignore_parent && device->parent && 195f4f3548dSRafael J. Wysocki state < device->parent->power.state) { 196f4f3548dSRafael J. Wysocki acpi_handle_debug(device->handle, 197f4f3548dSRafael J. Wysocki "Cannot transition to %s for parent in %s\n", 198593298e6SAaron Lu acpi_power_state_string(state), 199593298e6SAaron Lu acpi_power_state_string(device->parent->power.state)); 2009ce4e607SRafael J. Wysocki return -ENODEV; 2019ce4e607SRafael J. Wysocki } 2029ce4e607SRafael J. Wysocki 203e78adb75SRafael J. Wysocki /* 204e78adb75SRafael J. Wysocki * Transition Power 205e78adb75SRafael J. Wysocki * ---------------- 20620dacb71SRafael J. Wysocki * In accordance with ACPI 6, _PSx is executed before manipulating power 20720dacb71SRafael J. Wysocki * resources, unless the target state is D0, in which case _PS0 is 20820dacb71SRafael J. Wysocki * supposed to be executed after turning the power resources on. 209e78adb75SRafael J. Wysocki */ 21020dacb71SRafael J. Wysocki if (state > ACPI_STATE_D0) { 21120dacb71SRafael J. Wysocki /* 21220dacb71SRafael J. Wysocki * According to ACPI 6, devices cannot go from lower-power 21320dacb71SRafael J. Wysocki * (deeper) states to higher-power (shallower) states. 21420dacb71SRafael J. Wysocki */ 21520dacb71SRafael J. Wysocki if (state < device->power.state) { 216f4f3548dSRafael J. Wysocki acpi_handle_debug(device->handle, 217f4f3548dSRafael J. Wysocki "Cannot transition from %s to %s\n", 21820dacb71SRafael J. Wysocki acpi_power_state_string(device->power.state), 21920dacb71SRafael J. Wysocki acpi_power_state_string(state)); 22020dacb71SRafael J. Wysocki return -ENODEV; 2219ce4e607SRafael J. Wysocki } 22220dacb71SRafael J. Wysocki 22321ba2379SRafael J. Wysocki /* 22421ba2379SRafael J. Wysocki * If the device goes from D3hot to D3cold, _PS3 has been 22521ba2379SRafael J. Wysocki * evaluated for it already, so skip it in that case. 22621ba2379SRafael J. Wysocki */ 22721ba2379SRafael J. Wysocki if (device->power.state < ACPI_STATE_D3_HOT) { 228e78adb75SRafael J. Wysocki result = acpi_dev_pm_explicit_set(device, state); 229e78adb75SRafael J. Wysocki if (result) 230e78adb75SRafael J. Wysocki goto end; 23121ba2379SRafael J. Wysocki } 2329ce4e607SRafael J. Wysocki 23320dacb71SRafael J. Wysocki if (device->power.flags.power_resources) 23420dacb71SRafael J. Wysocki result = acpi_power_transition(device, target_state); 23520dacb71SRafael J. Wysocki } else { 23642787ed7SRafael J. Wysocki int cur_state = device->power.state; 23742787ed7SRafael J. Wysocki 23820dacb71SRafael J. Wysocki if (device->power.flags.power_resources) { 23920dacb71SRafael J. Wysocki result = acpi_power_transition(device, ACPI_STATE_D0); 24020dacb71SRafael J. Wysocki if (result) 24120dacb71SRafael J. Wysocki goto end; 24220dacb71SRafael J. Wysocki } 243f850a48aSRafael J. Wysocki 24442787ed7SRafael J. Wysocki if (cur_state == ACPI_STATE_D0) { 245f850a48aSRafael J. Wysocki int psc; 246f850a48aSRafael J. Wysocki 247f850a48aSRafael J. Wysocki /* Nothing to do here if _PSC is not present. */ 248f850a48aSRafael J. Wysocki if (!device->power.flags.explicit_get) 2496dd4a29dSRafael J. Wysocki goto no_change; 250f850a48aSRafael J. Wysocki 251f850a48aSRafael J. Wysocki /* 252f850a48aSRafael J. Wysocki * The power state of the device was set to D0 last 253f850a48aSRafael J. Wysocki * time, but that might have happened before a 254f850a48aSRafael J. Wysocki * system-wide transition involving the platform 255f850a48aSRafael J. Wysocki * firmware, so it may be necessary to evaluate _PS0 256f850a48aSRafael J. Wysocki * for the device here. However, use extra care here 257f850a48aSRafael J. Wysocki * and evaluate _PSC to check the device's current power 258f850a48aSRafael J. Wysocki * state, and only invoke _PS0 if the evaluation of _PSC 259f850a48aSRafael J. Wysocki * is successful and it returns a power state different 260f850a48aSRafael J. Wysocki * from D0. 261f850a48aSRafael J. Wysocki */ 262f850a48aSRafael J. Wysocki result = acpi_dev_pm_explicit_get(device, &psc); 263f850a48aSRafael J. Wysocki if (result || psc == ACPI_STATE_D0) 2646dd4a29dSRafael J. Wysocki goto no_change; 265f850a48aSRafael J. Wysocki } 266f850a48aSRafael J. Wysocki 26720dacb71SRafael J. Wysocki result = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0); 268e5656271SRafael J. Wysocki } 2699ce4e607SRafael J. Wysocki 2709ce4e607SRafael J. Wysocki end: 271e78adb75SRafael J. Wysocki if (result) { 272f4f3548dSRafael J. Wysocki acpi_handle_debug(device->handle, 273f4f3548dSRafael J. Wysocki "Failed to change power state to %s\n", 274a9b760b0SKai-Heng Feng acpi_power_state_string(target_state)); 275e78adb75SRafael J. Wysocki } else { 27671b65445SMika Westerberg device->power.state = target_state; 277f4f3548dSRafael J. Wysocki acpi_handle_debug(device->handle, "Power state changed to %s\n", 278c56fd5eaSRafael J. Wysocki acpi_power_state_string(target_state)); 2799ce4e607SRafael J. Wysocki } 2809ce4e607SRafael J. Wysocki 2819ce4e607SRafael J. Wysocki return result; 2826dd4a29dSRafael J. Wysocki 2836dd4a29dSRafael J. Wysocki no_change: 2846dd4a29dSRafael J. Wysocki acpi_handle_debug(device->handle, "Already in %s\n", 2856dd4a29dSRafael J. Wysocki acpi_power_state_string(state)); 2866dd4a29dSRafael J. Wysocki return 0; 2879ce4e607SRafael J. Wysocki } 2889ce4e607SRafael J. Wysocki EXPORT_SYMBOL(acpi_device_set_power); 2899ce4e607SRafael J. Wysocki 2909ce4e607SRafael J. Wysocki int acpi_bus_set_power(acpi_handle handle, int state) 2919ce4e607SRafael J. Wysocki { 29299ece713SRafael J. Wysocki struct acpi_device *device = acpi_fetch_acpi_dev(handle); 2939ce4e607SRafael J. Wysocki 29499ece713SRafael J. Wysocki if (device) 2959ce4e607SRafael J. Wysocki return acpi_device_set_power(device, state); 29699ece713SRafael J. Wysocki 29799ece713SRafael J. Wysocki return -ENODEV; 2989ce4e607SRafael J. Wysocki } 2999ce4e607SRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_set_power); 3009ce4e607SRafael J. Wysocki 3019ce4e607SRafael J. Wysocki int acpi_bus_init_power(struct acpi_device *device) 3029ce4e607SRafael J. Wysocki { 3039ce4e607SRafael J. Wysocki int state; 3049ce4e607SRafael J. Wysocki int result; 3059ce4e607SRafael J. Wysocki 3069ce4e607SRafael J. Wysocki if (!device) 3079ce4e607SRafael J. Wysocki return -EINVAL; 3089ce4e607SRafael J. Wysocki 3099ce4e607SRafael J. Wysocki device->power.state = ACPI_STATE_UNKNOWN; 310cde1f95fSSakari Ailus if (!acpi_device_is_present(device)) { 311cde1f95fSSakari Ailus device->flags.initialized = false; 3121b1f3e16SRafael J. Wysocki return -ENXIO; 313cde1f95fSSakari Ailus } 3149ce4e607SRafael J. Wysocki 3159ce4e607SRafael J. Wysocki result = acpi_device_get_power(device, &state); 3169ce4e607SRafael J. Wysocki if (result) 3179ce4e607SRafael J. Wysocki return result; 3189ce4e607SRafael J. Wysocki 319a2367807SRafael J. Wysocki if (state < ACPI_STATE_D3_COLD && device->power.flags.power_resources) { 32020dacb71SRafael J. Wysocki /* Reference count the power resources. */ 3219ce4e607SRafael J. Wysocki result = acpi_power_on_resources(device, state); 322a2367807SRafael J. Wysocki if (result) 3239ce4e607SRafael J. Wysocki return result; 324a2367807SRafael J. Wysocki 32520dacb71SRafael J. Wysocki if (state == ACPI_STATE_D0) { 32620dacb71SRafael J. Wysocki /* 32720dacb71SRafael J. Wysocki * If _PSC is not present and the state inferred from 32820dacb71SRafael J. Wysocki * power resources appears to be D0, it still may be 32920dacb71SRafael J. Wysocki * necessary to execute _PS0 at this point, because 33020dacb71SRafael J. Wysocki * another device using the same power resources may 33120dacb71SRafael J. Wysocki * have been put into D0 previously and that's why we 33220dacb71SRafael J. Wysocki * see D0 here. 33320dacb71SRafael J. Wysocki */ 3349c0f45e3SRafael J. Wysocki result = acpi_dev_pm_explicit_set(device, state); 3359c0f45e3SRafael J. Wysocki if (result) 3369c0f45e3SRafael J. Wysocki return result; 33720dacb71SRafael J. Wysocki } 338b3785492SRafael J. Wysocki } else if (state == ACPI_STATE_UNKNOWN) { 3397cd8407dSRafael J. Wysocki /* 3407cd8407dSRafael J. Wysocki * No power resources and missing _PSC? Cross fingers and make 3417cd8407dSRafael J. Wysocki * it D0 in hope that this is what the BIOS put the device into. 3427cd8407dSRafael J. Wysocki * [We tried to force D0 here by executing _PS0, but that broke 3437cd8407dSRafael J. Wysocki * Toshiba P870-303 in a nasty way.] 3447cd8407dSRafael J. Wysocki */ 345b3785492SRafael J. Wysocki state = ACPI_STATE_D0; 346a2367807SRafael J. Wysocki } 347a2367807SRafael J. Wysocki device->power.state = state; 348a2367807SRafael J. Wysocki return 0; 3499ce4e607SRafael J. Wysocki } 3509ce4e607SRafael J. Wysocki 351b9e95fc6SRafael J. Wysocki /** 352b9e95fc6SRafael J. Wysocki * acpi_device_fix_up_power - Force device with missing _PSC into D0. 353b9e95fc6SRafael J. Wysocki * @device: Device object whose power state is to be fixed up. 354b9e95fc6SRafael J. Wysocki * 355b9e95fc6SRafael J. Wysocki * Devices without power resources and _PSC, but having _PS0 and _PS3 defined, 356b9e95fc6SRafael J. Wysocki * are assumed to be put into D0 by the BIOS. However, in some cases that may 357b9e95fc6SRafael J. Wysocki * not be the case and this function should be used then. 358b9e95fc6SRafael J. Wysocki */ 359b9e95fc6SRafael J. Wysocki int acpi_device_fix_up_power(struct acpi_device *device) 360b9e95fc6SRafael J. Wysocki { 361b9e95fc6SRafael J. Wysocki int ret = 0; 362b9e95fc6SRafael J. Wysocki 363b9e95fc6SRafael J. Wysocki if (!device->power.flags.power_resources 364b9e95fc6SRafael J. Wysocki && !device->power.flags.explicit_get 365b9e95fc6SRafael J. Wysocki && device->power.state == ACPI_STATE_D0) 366b9e95fc6SRafael J. Wysocki ret = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0); 367b9e95fc6SRafael J. Wysocki 368b9e95fc6SRafael J. Wysocki return ret; 369b9e95fc6SRafael J. Wysocki } 37078a898d0SUlf Hansson EXPORT_SYMBOL_GPL(acpi_device_fix_up_power); 371b9e95fc6SRafael J. Wysocki 372a22f18bdSRafael J. Wysocki static int fix_up_power_if_applicable(struct acpi_device *adev, void *not_used) 373a22f18bdSRafael J. Wysocki { 374a22f18bdSRafael J. Wysocki if (adev->status.present && adev->status.enabled) 375a22f18bdSRafael J. Wysocki acpi_device_fix_up_power(adev); 376a22f18bdSRafael J. Wysocki 377a22f18bdSRafael J. Wysocki return 0; 378a22f18bdSRafael J. Wysocki } 379a22f18bdSRafael J. Wysocki 380a22f18bdSRafael J. Wysocki /** 381a22f18bdSRafael J. Wysocki * acpi_device_fix_up_power_extended - Force device and its children into D0. 382a22f18bdSRafael J. Wysocki * @adev: Parent device object whose power state is to be fixed up. 383a22f18bdSRafael J. Wysocki * 384a22f18bdSRafael J. Wysocki * Call acpi_device_fix_up_power() for @adev and its children so long as they 385a22f18bdSRafael J. Wysocki * are reported as present and enabled. 386a22f18bdSRafael J. Wysocki */ 387a22f18bdSRafael J. Wysocki void acpi_device_fix_up_power_extended(struct acpi_device *adev) 388a22f18bdSRafael J. Wysocki { 389a22f18bdSRafael J. Wysocki acpi_device_fix_up_power(adev); 390a22f18bdSRafael J. Wysocki acpi_dev_for_each_child(adev, fix_up_power_if_applicable, NULL); 391a22f18bdSRafael J. Wysocki } 392a22f18bdSRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_device_fix_up_power_extended); 393a22f18bdSRafael J. Wysocki 394202317a5SRafael J. Wysocki int acpi_device_update_power(struct acpi_device *device, int *state_p) 3959ce4e607SRafael J. Wysocki { 3969ce4e607SRafael J. Wysocki int state; 3979ce4e607SRafael J. Wysocki int result; 3989ce4e607SRafael J. Wysocki 399202317a5SRafael J. Wysocki if (device->power.state == ACPI_STATE_UNKNOWN) { 400202317a5SRafael J. Wysocki result = acpi_bus_init_power(device); 401202317a5SRafael J. Wysocki if (!result && state_p) 402202317a5SRafael J. Wysocki *state_p = device->power.state; 403202317a5SRafael J. Wysocki 4049ce4e607SRafael J. Wysocki return result; 405202317a5SRafael J. Wysocki } 4069ce4e607SRafael J. Wysocki 4079ce4e607SRafael J. Wysocki result = acpi_device_get_power(device, &state); 4089ce4e607SRafael J. Wysocki if (result) 4099ce4e607SRafael J. Wysocki return result; 4109ce4e607SRafael J. Wysocki 41191bdad0bSRafael J. Wysocki if (state == ACPI_STATE_UNKNOWN) { 412511d5c42SRafael J. Wysocki state = ACPI_STATE_D0; 4139ce4e607SRafael J. Wysocki result = acpi_device_set_power(device, state); 41491bdad0bSRafael J. Wysocki if (result) 41591bdad0bSRafael J. Wysocki return result; 41691bdad0bSRafael J. Wysocki } else { 41791bdad0bSRafael J. Wysocki if (device->power.flags.power_resources) { 41891bdad0bSRafael J. Wysocki /* 41991bdad0bSRafael J. Wysocki * We don't need to really switch the state, bu we need 42091bdad0bSRafael J. Wysocki * to update the power resources' reference counters. 42191bdad0bSRafael J. Wysocki */ 42291bdad0bSRafael J. Wysocki result = acpi_power_transition(device, state); 42391bdad0bSRafael J. Wysocki if (result) 42491bdad0bSRafael J. Wysocki return result; 42591bdad0bSRafael J. Wysocki } 42691bdad0bSRafael J. Wysocki device->power.state = state; 42791bdad0bSRafael J. Wysocki } 42891bdad0bSRafael J. Wysocki if (state_p) 4299ce4e607SRafael J. Wysocki *state_p = state; 4309ce4e607SRafael J. Wysocki 43191bdad0bSRafael J. Wysocki return 0; 4329ce4e607SRafael J. Wysocki } 4332bb3a2bfSAaron Lu EXPORT_SYMBOL_GPL(acpi_device_update_power); 434202317a5SRafael J. Wysocki 435202317a5SRafael J. Wysocki int acpi_bus_update_power(acpi_handle handle, int *state_p) 436202317a5SRafael J. Wysocki { 43799ece713SRafael J. Wysocki struct acpi_device *device = acpi_fetch_acpi_dev(handle); 438202317a5SRafael J. Wysocki 43999ece713SRafael J. Wysocki if (device) 44099ece713SRafael J. Wysocki return acpi_device_update_power(device, state_p); 44199ece713SRafael J. Wysocki 44299ece713SRafael J. Wysocki return -ENODEV; 443202317a5SRafael J. Wysocki } 4449ce4e607SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_bus_update_power); 4459ce4e607SRafael J. Wysocki 4469ce4e607SRafael J. Wysocki bool acpi_bus_power_manageable(acpi_handle handle) 4479ce4e607SRafael J. Wysocki { 44899ece713SRafael J. Wysocki struct acpi_device *device = acpi_fetch_acpi_dev(handle); 4499ce4e607SRafael J. Wysocki 45099ece713SRafael J. Wysocki return device && device->flags.power_manageable; 4519ce4e607SRafael J. Wysocki } 4529ce4e607SRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_power_manageable); 4539ce4e607SRafael J. Wysocki 45410fa1b2cSRafael J. Wysocki static int acpi_power_up_if_adr_present(struct acpi_device *adev, void *not_used) 455b7dd6298SRafael J. Wysocki { 456b7dd6298SRafael J. Wysocki if (!(adev->flags.power_manageable && adev->pnp.type.bus_address)) 457b7dd6298SRafael J. Wysocki return 0; 458b7dd6298SRafael J. Wysocki 459b7dd6298SRafael J. Wysocki acpi_handle_debug(adev->handle, "Power state: %s\n", 460b7dd6298SRafael J. Wysocki acpi_power_state_string(adev->power.state)); 461b7dd6298SRafael J. Wysocki 462b7dd6298SRafael J. Wysocki if (adev->power.state == ACPI_STATE_D3_COLD) 463b7dd6298SRafael J. Wysocki return acpi_device_set_power(adev, ACPI_STATE_D0); 464b7dd6298SRafael J. Wysocki 465b7dd6298SRafael J. Wysocki return 0; 466b7dd6298SRafael J. Wysocki } 467b7dd6298SRafael J. Wysocki 468b7dd6298SRafael J. Wysocki /** 469b7dd6298SRafael J. Wysocki * acpi_dev_power_up_children_with_adr - Power up childres with valid _ADR 470b7dd6298SRafael J. Wysocki * @adev: Parent ACPI device object. 471b7dd6298SRafael J. Wysocki * 472b7dd6298SRafael J. Wysocki * Change the power states of the direct children of @adev that are in D3cold 473b7dd6298SRafael J. Wysocki * and hold valid _ADR objects to D0 in order to allow bus (e.g. PCI) 474b7dd6298SRafael J. Wysocki * enumeration code to access them. 475b7dd6298SRafael J. Wysocki */ 476b7dd6298SRafael J. Wysocki void acpi_dev_power_up_children_with_adr(struct acpi_device *adev) 477b7dd6298SRafael J. Wysocki { 478b7dd6298SRafael J. Wysocki acpi_dev_for_each_child(adev, acpi_power_up_if_adr_present, NULL); 479b7dd6298SRafael J. Wysocki } 480b7dd6298SRafael J. Wysocki 481ec4602a9SRafael J. Wysocki #ifdef CONFIG_PM 482ec4602a9SRafael J. Wysocki static DEFINE_MUTEX(acpi_pm_notifier_lock); 483ff165679SVille Syrjälä static DEFINE_MUTEX(acpi_pm_notifier_install_lock); 484ec4602a9SRafael J. Wysocki 48533e4f80eSRafael J. Wysocki void acpi_pm_wakeup_event(struct device *dev) 48633e4f80eSRafael J. Wysocki { 48733e4f80eSRafael J. Wysocki pm_wakeup_dev_event(dev, 0, acpi_s2idle_wakeup()); 48833e4f80eSRafael J. Wysocki } 48933e4f80eSRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_pm_wakeup_event); 49033e4f80eSRafael J. Wysocki 491c072530fSRafael J. Wysocki static void acpi_pm_notify_handler(acpi_handle handle, u32 val, void *not_used) 492c072530fSRafael J. Wysocki { 493c072530fSRafael J. Wysocki struct acpi_device *adev; 494c072530fSRafael J. Wysocki 495c072530fSRafael J. Wysocki if (val != ACPI_NOTIFY_DEVICE_WAKE) 496c072530fSRafael J. Wysocki return; 497c072530fSRafael J. Wysocki 498020a6375SRafael J. Wysocki acpi_handle_debug(handle, "Wake notify\n"); 499020a6375SRafael J. Wysocki 500*45e9aa1fSRafael J. Wysocki adev = acpi_get_acpi_dev(handle); 501c072530fSRafael J. Wysocki if (!adev) 502c072530fSRafael J. Wysocki return; 503c072530fSRafael J. Wysocki 504c072530fSRafael J. Wysocki mutex_lock(&acpi_pm_notifier_lock); 505c072530fSRafael J. Wysocki 506c072530fSRafael J. Wysocki if (adev->wakeup.flags.notifier_present) { 50733e4f80eSRafael J. Wysocki pm_wakeup_ws_event(adev->wakeup.ws, 0, acpi_s2idle_wakeup()); 508020a6375SRafael J. Wysocki if (adev->wakeup.context.func) { 509d75f773cSSakari Ailus acpi_handle_debug(handle, "Running %pS for %s\n", 510020a6375SRafael J. Wysocki adev->wakeup.context.func, 511020a6375SRafael J. Wysocki dev_name(adev->wakeup.context.dev)); 51264fd1c70SRafael J. Wysocki adev->wakeup.context.func(&adev->wakeup.context); 513c072530fSRafael J. Wysocki } 514020a6375SRafael J. Wysocki } 515c072530fSRafael J. Wysocki 516c072530fSRafael J. Wysocki mutex_unlock(&acpi_pm_notifier_lock); 517c072530fSRafael J. Wysocki 518*45e9aa1fSRafael J. Wysocki acpi_put_acpi_dev(adev); 519c072530fSRafael J. Wysocki } 520c072530fSRafael J. Wysocki 521ec4602a9SRafael J. Wysocki /** 522c072530fSRafael J. Wysocki * acpi_add_pm_notifier - Register PM notify handler for given ACPI device. 523c072530fSRafael J. Wysocki * @adev: ACPI device to add the notify handler for. 524c072530fSRafael J. Wysocki * @dev: Device to generate a wakeup event for while handling the notification. 52564fd1c70SRafael J. Wysocki * @func: Work function to execute when handling the notification. 526ec4602a9SRafael J. Wysocki * 527ec4602a9SRafael J. Wysocki * NOTE: @adev need not be a run-wake or wakeup device to be a valid source of 528ec4602a9SRafael J. Wysocki * PM wakeup events. For example, wakeup events may be generated for bridges 529ec4602a9SRafael J. Wysocki * if one of the devices below the bridge is signaling wakeup, even if the 530ec4602a9SRafael J. Wysocki * bridge itself doesn't have a wakeup GPE associated with it. 531ec4602a9SRafael J. Wysocki */ 532c072530fSRafael J. Wysocki acpi_status acpi_add_pm_notifier(struct acpi_device *adev, struct device *dev, 53364fd1c70SRafael J. Wysocki void (*func)(struct acpi_device_wakeup_context *context)) 534ec4602a9SRafael J. Wysocki { 535ec4602a9SRafael J. Wysocki acpi_status status = AE_ALREADY_EXISTS; 536ec4602a9SRafael J. Wysocki 53764fd1c70SRafael J. Wysocki if (!dev && !func) 538c072530fSRafael J. Wysocki return AE_BAD_PARAMETER; 539c072530fSRafael J. Wysocki 540ff165679SVille Syrjälä mutex_lock(&acpi_pm_notifier_install_lock); 541ec4602a9SRafael J. Wysocki 542ec4602a9SRafael J. Wysocki if (adev->wakeup.flags.notifier_present) 543ec4602a9SRafael J. Wysocki goto out; 544ec4602a9SRafael J. Wysocki 545c072530fSRafael J. Wysocki status = acpi_install_notify_handler(adev->handle, ACPI_SYSTEM_NOTIFY, 546c072530fSRafael J. Wysocki acpi_pm_notify_handler, NULL); 547ec4602a9SRafael J. Wysocki if (ACPI_FAILURE(status)) 548ec4602a9SRafael J. Wysocki goto out; 549ec4602a9SRafael J. Wysocki 550ff165679SVille Syrjälä mutex_lock(&acpi_pm_notifier_lock); 551c8377adfSTri Vo adev->wakeup.ws = wakeup_source_register(&adev->dev, 552c8377adfSTri Vo dev_name(&adev->dev)); 553ff165679SVille Syrjälä adev->wakeup.context.dev = dev; 554ff165679SVille Syrjälä adev->wakeup.context.func = func; 555ec4602a9SRafael J. Wysocki adev->wakeup.flags.notifier_present = true; 556ff165679SVille Syrjälä mutex_unlock(&acpi_pm_notifier_lock); 557ec4602a9SRafael J. Wysocki 558ec4602a9SRafael J. Wysocki out: 559ff165679SVille Syrjälä mutex_unlock(&acpi_pm_notifier_install_lock); 560ec4602a9SRafael J. Wysocki return status; 561ec4602a9SRafael J. Wysocki } 562ec4602a9SRafael J. Wysocki 563ec4602a9SRafael J. Wysocki /** 564ec4602a9SRafael J. Wysocki * acpi_remove_pm_notifier - Unregister PM notifier from given ACPI device. 565ec4602a9SRafael J. Wysocki * @adev: ACPI device to remove the notifier from. 566ec4602a9SRafael J. Wysocki */ 567c072530fSRafael J. Wysocki acpi_status acpi_remove_pm_notifier(struct acpi_device *adev) 568ec4602a9SRafael J. Wysocki { 569ec4602a9SRafael J. Wysocki acpi_status status = AE_BAD_PARAMETER; 570ec4602a9SRafael J. Wysocki 571ff165679SVille Syrjälä mutex_lock(&acpi_pm_notifier_install_lock); 572ec4602a9SRafael J. Wysocki 573ec4602a9SRafael J. Wysocki if (!adev->wakeup.flags.notifier_present) 574ec4602a9SRafael J. Wysocki goto out; 575ec4602a9SRafael J. Wysocki 576ec4602a9SRafael J. Wysocki status = acpi_remove_notify_handler(adev->handle, 577ec4602a9SRafael J. Wysocki ACPI_SYSTEM_NOTIFY, 578c072530fSRafael J. Wysocki acpi_pm_notify_handler); 579ec4602a9SRafael J. Wysocki if (ACPI_FAILURE(status)) 580ec4602a9SRafael J. Wysocki goto out; 581ec4602a9SRafael J. Wysocki 582ff165679SVille Syrjälä mutex_lock(&acpi_pm_notifier_lock); 58364fd1c70SRafael J. Wysocki adev->wakeup.context.func = NULL; 584c072530fSRafael J. Wysocki adev->wakeup.context.dev = NULL; 585c072530fSRafael J. Wysocki wakeup_source_unregister(adev->wakeup.ws); 586ec4602a9SRafael J. Wysocki adev->wakeup.flags.notifier_present = false; 587ff165679SVille Syrjälä mutex_unlock(&acpi_pm_notifier_lock); 588ec4602a9SRafael J. Wysocki 589ec4602a9SRafael J. Wysocki out: 590ff165679SVille Syrjälä mutex_unlock(&acpi_pm_notifier_install_lock); 591ec4602a9SRafael J. Wysocki return status; 592ec4602a9SRafael J. Wysocki } 593ec4602a9SRafael J. Wysocki 5949ce4e607SRafael J. Wysocki bool acpi_bus_can_wakeup(acpi_handle handle) 5959ce4e607SRafael J. Wysocki { 59699ece713SRafael J. Wysocki struct acpi_device *device = acpi_fetch_acpi_dev(handle); 5979ce4e607SRafael J. Wysocki 59899ece713SRafael J. Wysocki return device && device->wakeup.flags.valid; 5999ce4e607SRafael J. Wysocki } 6009ce4e607SRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_can_wakeup); 6019ce4e607SRafael J. Wysocki 6028370c2dcSRafael J. Wysocki bool acpi_pm_device_can_wakeup(struct device *dev) 6038370c2dcSRafael J. Wysocki { 6048370c2dcSRafael J. Wysocki struct acpi_device *adev = ACPI_COMPANION(dev); 6058370c2dcSRafael J. Wysocki 6068370c2dcSRafael J. Wysocki return adev ? acpi_device_can_wakeup(adev) : false; 6078370c2dcSRafael J. Wysocki } 6088370c2dcSRafael J. Wysocki 6099ce4e607SRafael J. Wysocki /** 610b25c77efSRafael J. Wysocki * acpi_dev_pm_get_state - Get preferred power state of ACPI device. 61186b3832cSRafael J. Wysocki * @dev: Device whose preferred target power state to return. 61286b3832cSRafael J. Wysocki * @adev: ACPI device node corresponding to @dev. 61386b3832cSRafael J. Wysocki * @target_state: System state to match the resultant device state. 614fa1675b5SRafael J. Wysocki * @d_min_p: Location to store the highest power state available to the device. 615fa1675b5SRafael J. Wysocki * @d_max_p: Location to store the lowest power state available to the device. 61686b3832cSRafael J. Wysocki * 617fa1675b5SRafael J. Wysocki * Find the lowest power (highest number) and highest power (lowest number) ACPI 618fa1675b5SRafael J. Wysocki * device power states that the device can be in while the system is in the 619fa1675b5SRafael J. Wysocki * state represented by @target_state. Store the integer numbers representing 620fa1675b5SRafael J. Wysocki * those stats in the memory locations pointed to by @d_max_p and @d_min_p, 621fa1675b5SRafael J. Wysocki * respectively. 62286b3832cSRafael J. Wysocki * 62386b3832cSRafael J. Wysocki * Callers must ensure that @dev and @adev are valid pointers and that @adev 62486b3832cSRafael J. Wysocki * actually corresponds to @dev before using this function. 625fa1675b5SRafael J. Wysocki * 626fa1675b5SRafael J. Wysocki * Returns 0 on success or -ENODATA when one of the ACPI methods fails or 627fa1675b5SRafael J. Wysocki * returns a value that doesn't make sense. The memory locations pointed to by 628fa1675b5SRafael J. Wysocki * @d_max_p and @d_min_p are only modified on success. 62986b3832cSRafael J. Wysocki */ 630b25c77efSRafael J. Wysocki static int acpi_dev_pm_get_state(struct device *dev, struct acpi_device *adev, 631fa1675b5SRafael J. Wysocki u32 target_state, int *d_min_p, int *d_max_p) 63286b3832cSRafael J. Wysocki { 633fa1675b5SRafael J. Wysocki char method[] = { '_', 'S', '0' + target_state, 'D', '\0' }; 634fa1675b5SRafael J. Wysocki acpi_handle handle = adev->handle; 635fa1675b5SRafael J. Wysocki unsigned long long ret; 636fa1675b5SRafael J. Wysocki int d_min, d_max; 63786b3832cSRafael J. Wysocki bool wakeup = false; 638bf8c6184SDaniel Drake bool has_sxd = false; 639fa1675b5SRafael J. Wysocki acpi_status status; 64086b3832cSRafael J. Wysocki 64186b3832cSRafael J. Wysocki /* 642fa1675b5SRafael J. Wysocki * If the system state is S0, the lowest power state the device can be 643fa1675b5SRafael J. Wysocki * in is D3cold, unless the device has _S0W and is supposed to signal 644fa1675b5SRafael J. Wysocki * wakeup, in which case the return value of _S0W has to be used as the 645fa1675b5SRafael J. Wysocki * lowest power state available to the device. 64686b3832cSRafael J. Wysocki */ 64786b3832cSRafael J. Wysocki d_min = ACPI_STATE_D0; 6484c164ae7SRafael J. Wysocki d_max = ACPI_STATE_D3_COLD; 64986b3832cSRafael J. Wysocki 65086b3832cSRafael J. Wysocki /* 65186b3832cSRafael J. Wysocki * If present, _SxD methods return the minimum D-state (highest power 65286b3832cSRafael J. Wysocki * state) we can use for the corresponding S-states. Otherwise, the 65386b3832cSRafael J. Wysocki * minimum D-state is D0 (ACPI 3.x). 65486b3832cSRafael J. Wysocki */ 65586b3832cSRafael J. Wysocki if (target_state > ACPI_STATE_S0) { 656fa1675b5SRafael J. Wysocki /* 657fa1675b5SRafael J. Wysocki * We rely on acpi_evaluate_integer() not clobbering the integer 658fa1675b5SRafael J. Wysocki * provided if AE_NOT_FOUND is returned. 659fa1675b5SRafael J. Wysocki */ 660fa1675b5SRafael J. Wysocki ret = d_min; 661fa1675b5SRafael J. Wysocki status = acpi_evaluate_integer(handle, method, NULL, &ret); 662fa1675b5SRafael J. Wysocki if ((ACPI_FAILURE(status) && status != AE_NOT_FOUND) 663fa1675b5SRafael J. Wysocki || ret > ACPI_STATE_D3_COLD) 664fa1675b5SRafael J. Wysocki return -ENODATA; 665fa1675b5SRafael J. Wysocki 666fa1675b5SRafael J. Wysocki /* 667fa1675b5SRafael J. Wysocki * We need to handle legacy systems where D3hot and D3cold are 668fa1675b5SRafael J. Wysocki * the same and 3 is returned in both cases, so fall back to 669fa1675b5SRafael J. Wysocki * D3cold if D3hot is not a valid state. 670fa1675b5SRafael J. Wysocki */ 671fa1675b5SRafael J. Wysocki if (!adev->power.states[ret].flags.valid) { 672fa1675b5SRafael J. Wysocki if (ret == ACPI_STATE_D3_HOT) 673fa1675b5SRafael J. Wysocki ret = ACPI_STATE_D3_COLD; 674fa1675b5SRafael J. Wysocki else 675fa1675b5SRafael J. Wysocki return -ENODATA; 676fa1675b5SRafael J. Wysocki } 677bf8c6184SDaniel Drake 678bf8c6184SDaniel Drake if (status == AE_OK) 679bf8c6184SDaniel Drake has_sxd = true; 680bf8c6184SDaniel Drake 681fa1675b5SRafael J. Wysocki d_min = ret; 68286b3832cSRafael J. Wysocki wakeup = device_may_wakeup(dev) && adev->wakeup.flags.valid 68386b3832cSRafael J. Wysocki && adev->wakeup.sleep_state >= target_state; 68420f97cafSRafael J. Wysocki } else { 68586b3832cSRafael J. Wysocki wakeup = adev->wakeup.flags.valid; 68686b3832cSRafael J. Wysocki } 68786b3832cSRafael J. Wysocki 68886b3832cSRafael J. Wysocki /* 68986b3832cSRafael J. Wysocki * If _PRW says we can wake up the system from the target sleep state, 69086b3832cSRafael J. Wysocki * the D-state returned by _SxD is sufficient for that (we assume a 69186b3832cSRafael J. Wysocki * wakeup-aware driver if wake is set). Still, if _SxW exists 69286b3832cSRafael J. Wysocki * (ACPI 3.x), it should return the maximum (lowest power) D-state that 69386b3832cSRafael J. Wysocki * can wake the system. _S0W may be valid, too. 69486b3832cSRafael J. Wysocki */ 69586b3832cSRafael J. Wysocki if (wakeup) { 696fa1675b5SRafael J. Wysocki method[3] = 'W'; 697fa1675b5SRafael J. Wysocki status = acpi_evaluate_integer(handle, method, NULL, &ret); 698fa1675b5SRafael J. Wysocki if (status == AE_NOT_FOUND) { 699bf8c6184SDaniel Drake /* No _SxW. In this case, the ACPI spec says that we 700bf8c6184SDaniel Drake * must not go into any power state deeper than the 701bf8c6184SDaniel Drake * value returned from _SxD. 702bf8c6184SDaniel Drake */ 703bf8c6184SDaniel Drake if (has_sxd && target_state > ACPI_STATE_S0) 70486b3832cSRafael J. Wysocki d_max = d_min; 705fa1675b5SRafael J. Wysocki } else if (ACPI_SUCCESS(status) && ret <= ACPI_STATE_D3_COLD) { 706fa1675b5SRafael J. Wysocki /* Fall back to D3cold if ret is not a valid state. */ 707fa1675b5SRafael J. Wysocki if (!adev->power.states[ret].flags.valid) 708fa1675b5SRafael J. Wysocki ret = ACPI_STATE_D3_COLD; 709fa1675b5SRafael J. Wysocki 710fa1675b5SRafael J. Wysocki d_max = ret > d_min ? ret : d_min; 711fa1675b5SRafael J. Wysocki } else { 712fa1675b5SRafael J. Wysocki return -ENODATA; 71386b3832cSRafael J. Wysocki } 71486b3832cSRafael J. Wysocki } 71586b3832cSRafael J. Wysocki 71686b3832cSRafael J. Wysocki if (d_min_p) 71786b3832cSRafael J. Wysocki *d_min_p = d_min; 718fa1675b5SRafael J. Wysocki 719fa1675b5SRafael J. Wysocki if (d_max_p) 720fa1675b5SRafael J. Wysocki *d_max_p = d_max; 721fa1675b5SRafael J. Wysocki 722fa1675b5SRafael J. Wysocki return 0; 72386b3832cSRafael J. Wysocki } 724cd7bd02dSRafael J. Wysocki 725a6ae7594SRafael J. Wysocki /** 726a6ae7594SRafael J. Wysocki * acpi_pm_device_sleep_state - Get preferred power state of ACPI device. 727a6ae7594SRafael J. Wysocki * @dev: Device whose preferred target power state to return. 728a6ae7594SRafael J. Wysocki * @d_min_p: Location to store the upper limit of the allowed states range. 729a6ae7594SRafael J. Wysocki * @d_max_in: Deepest low-power state to take into consideration. 730a6ae7594SRafael J. Wysocki * Return value: Preferred power state of the device on success, -ENODEV 731fa1675b5SRafael J. Wysocki * if there's no 'struct acpi_device' for @dev, -EINVAL if @d_max_in is 732fa1675b5SRafael J. Wysocki * incorrect, or -ENODATA on ACPI method failure. 733a6ae7594SRafael J. Wysocki * 734a6ae7594SRafael J. Wysocki * The caller must ensure that @dev is valid before using this function. 735a6ae7594SRafael J. Wysocki */ 736a6ae7594SRafael J. Wysocki int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in) 737a6ae7594SRafael J. Wysocki { 738a6ae7594SRafael J. Wysocki struct acpi_device *adev; 7399b5c7a5aSRafael J. Wysocki int ret, d_min, d_max; 740fa1675b5SRafael J. Wysocki 741fa1675b5SRafael J. Wysocki if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3_COLD) 742fa1675b5SRafael J. Wysocki return -EINVAL; 743fa1675b5SRafael J. Wysocki 74420dacb71SRafael J. Wysocki if (d_max_in > ACPI_STATE_D2) { 745fa1675b5SRafael J. Wysocki enum pm_qos_flags_status stat; 746fa1675b5SRafael J. Wysocki 747fa1675b5SRafael J. Wysocki stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF); 748fa1675b5SRafael J. Wysocki if (stat == PM_QOS_FLAGS_ALL) 74920dacb71SRafael J. Wysocki d_max_in = ACPI_STATE_D2; 750fa1675b5SRafael J. Wysocki } 751a6ae7594SRafael J. Wysocki 75217653a3eSRafael J. Wysocki adev = ACPI_COMPANION(dev); 75317653a3eSRafael J. Wysocki if (!adev) { 75417653a3eSRafael J. Wysocki dev_dbg(dev, "ACPI companion missing in %s!\n", __func__); 755a6ae7594SRafael J. Wysocki return -ENODEV; 756a6ae7594SRafael J. Wysocki } 757a6ae7594SRafael J. Wysocki 758fa1675b5SRafael J. Wysocki ret = acpi_dev_pm_get_state(dev, adev, acpi_target_system_state(), 7599b5c7a5aSRafael J. Wysocki &d_min, &d_max); 760fa1675b5SRafael J. Wysocki if (ret) 761fa1675b5SRafael J. Wysocki return ret; 762fa1675b5SRafael J. Wysocki 7639b5c7a5aSRafael J. Wysocki if (d_max_in < d_min) 764fa1675b5SRafael J. Wysocki return -EINVAL; 765fa1675b5SRafael J. Wysocki 766fa1675b5SRafael J. Wysocki if (d_max > d_max_in) { 7679b5c7a5aSRafael J. Wysocki for (d_max = d_max_in; d_max > d_min; d_max--) { 768fa1675b5SRafael J. Wysocki if (adev->power.states[d_max].flags.valid) 769fa1675b5SRafael J. Wysocki break; 770fa1675b5SRafael J. Wysocki } 771fa1675b5SRafael J. Wysocki } 7729b5c7a5aSRafael J. Wysocki 7739b5c7a5aSRafael J. Wysocki if (d_min_p) 7749b5c7a5aSRafael J. Wysocki *d_min_p = d_min; 7759b5c7a5aSRafael J. Wysocki 776fa1675b5SRafael J. Wysocki return d_max; 777a6ae7594SRafael J. Wysocki } 778a6ae7594SRafael J. Wysocki EXPORT_SYMBOL(acpi_pm_device_sleep_state); 779a6ae7594SRafael J. Wysocki 780cd7bd02dSRafael J. Wysocki /** 781c072530fSRafael J. Wysocki * acpi_pm_notify_work_func - ACPI devices wakeup notification work function. 78264fd1c70SRafael J. Wysocki * @context: Device wakeup context. 783e5cc8ef3SRafael J. Wysocki */ 78464fd1c70SRafael J. Wysocki static void acpi_pm_notify_work_func(struct acpi_device_wakeup_context *context) 785e5cc8ef3SRafael J. Wysocki { 78664fd1c70SRafael J. Wysocki struct device *dev = context->dev; 787e5cc8ef3SRafael J. Wysocki 788c072530fSRafael J. Wysocki if (dev) { 789e5cc8ef3SRafael J. Wysocki pm_wakeup_event(dev, 0); 79064fd1c70SRafael J. Wysocki pm_request_resume(dev); 791e5cc8ef3SRafael J. Wysocki } 792e5cc8ef3SRafael J. Wysocki } 793e5cc8ef3SRafael J. Wysocki 79499d8845eSRafael J. Wysocki static DEFINE_MUTEX(acpi_wakeup_lock); 79599d8845eSRafael J. Wysocki 7961ba51a7cSRafael J. Wysocki static int __acpi_device_wakeup_enable(struct acpi_device *adev, 7977482c5cbSRafael J. Wysocki u32 target_state) 7981ba51a7cSRafael J. Wysocki { 7991ba51a7cSRafael J. Wysocki struct acpi_device_wakeup *wakeup = &adev->wakeup; 8001ba51a7cSRafael J. Wysocki acpi_status status; 8011ba51a7cSRafael J. Wysocki int error = 0; 8021ba51a7cSRafael J. Wysocki 8031ba51a7cSRafael J. Wysocki mutex_lock(&acpi_wakeup_lock); 8041ba51a7cSRafael J. Wysocki 805b93b7ef6SRafael J. Wysocki /* 806b93b7ef6SRafael J. Wysocki * If the device wakeup power is already enabled, disable it and enable 807b93b7ef6SRafael J. Wysocki * it again in case it depends on the configuration of subordinate 808b93b7ef6SRafael J. Wysocki * devices and the conditions have changed since it was enabled last 809b93b7ef6SRafael J. Wysocki * time. 810b93b7ef6SRafael J. Wysocki */ 8111ba51a7cSRafael J. Wysocki if (wakeup->enable_count > 0) 812b93b7ef6SRafael J. Wysocki acpi_disable_wakeup_device_power(adev); 8131ba51a7cSRafael J. Wysocki 8141ba51a7cSRafael J. Wysocki error = acpi_enable_wakeup_device_power(adev, target_state); 815b93b7ef6SRafael J. Wysocki if (error) { 816b93b7ef6SRafael J. Wysocki if (wakeup->enable_count > 0) { 817b93b7ef6SRafael J. Wysocki acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number); 818b93b7ef6SRafael J. Wysocki wakeup->enable_count = 0; 819b93b7ef6SRafael J. Wysocki } 8201ba51a7cSRafael J. Wysocki goto out; 821b93b7ef6SRafael J. Wysocki } 822b93b7ef6SRafael J. Wysocki 823b93b7ef6SRafael J. Wysocki if (wakeup->enable_count > 0) 824b93b7ef6SRafael J. Wysocki goto inc; 8251ba51a7cSRafael J. Wysocki 8261ba51a7cSRafael J. Wysocki status = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number); 8271ba51a7cSRafael J. Wysocki if (ACPI_FAILURE(status)) { 8281ba51a7cSRafael J. Wysocki acpi_disable_wakeup_device_power(adev); 8291ba51a7cSRafael J. Wysocki error = -EIO; 8301ba51a7cSRafael J. Wysocki goto out; 8311ba51a7cSRafael J. Wysocki } 8321ba51a7cSRafael J. Wysocki 833fbc9418fSRafael J. Wysocki acpi_handle_debug(adev->handle, "GPE%2X enabled for wakeup\n", 834fbc9418fSRafael J. Wysocki (unsigned int)wakeup->gpe_number); 835fbc9418fSRafael J. Wysocki 8361ba51a7cSRafael J. Wysocki inc: 837b93b7ef6SRafael J. Wysocki if (wakeup->enable_count < INT_MAX) 8381ba51a7cSRafael J. Wysocki wakeup->enable_count++; 839b93b7ef6SRafael J. Wysocki else 840b93b7ef6SRafael J. Wysocki acpi_handle_info(adev->handle, "Wakeup enable count out of bounds!\n"); 8411ba51a7cSRafael J. Wysocki 8421ba51a7cSRafael J. Wysocki out: 8431ba51a7cSRafael J. Wysocki mutex_unlock(&acpi_wakeup_lock); 8441ba51a7cSRafael J. Wysocki return error; 8451ba51a7cSRafael J. Wysocki } 8461ba51a7cSRafael J. Wysocki 847e5cc8ef3SRafael J. Wysocki /** 84899d8845eSRafael J. Wysocki * acpi_device_wakeup_enable - Enable wakeup functionality for device. 84999d8845eSRafael J. Wysocki * @adev: ACPI device to enable wakeup functionality for. 850f35cec25SRafael J. Wysocki * @target_state: State the system is transitioning into. 851cd7bd02dSRafael J. Wysocki * 85299d8845eSRafael J. Wysocki * Enable the GPE associated with @adev so that it can generate wakeup signals 85399d8845eSRafael J. Wysocki * for the device in response to external (remote) events and enable wakeup 85499d8845eSRafael J. Wysocki * power for it. 855dee8370cSRafael J. Wysocki * 856dee8370cSRafael J. Wysocki * Callers must ensure that @adev is a valid ACPI device node before executing 857dee8370cSRafael J. Wysocki * this function. 858dee8370cSRafael J. Wysocki */ 85999d8845eSRafael J. Wysocki static int acpi_device_wakeup_enable(struct acpi_device *adev, u32 target_state) 860dee8370cSRafael J. Wysocki { 8617482c5cbSRafael J. Wysocki return __acpi_device_wakeup_enable(adev, target_state); 86299d8845eSRafael J. Wysocki } 86399d8845eSRafael J. Wysocki 86499d8845eSRafael J. Wysocki /** 86599d8845eSRafael J. Wysocki * acpi_device_wakeup_disable - Disable wakeup functionality for device. 86699d8845eSRafael J. Wysocki * @adev: ACPI device to disable wakeup functionality for. 86799d8845eSRafael J. Wysocki * 86899d8845eSRafael J. Wysocki * Disable the GPE associated with @adev and disable wakeup power for it. 86999d8845eSRafael J. Wysocki * 87099d8845eSRafael J. Wysocki * Callers must ensure that @adev is a valid ACPI device node before executing 87199d8845eSRafael J. Wysocki * this function. 87299d8845eSRafael J. Wysocki */ 87399d8845eSRafael J. Wysocki static void acpi_device_wakeup_disable(struct acpi_device *adev) 87499d8845eSRafael J. Wysocki { 87599d8845eSRafael J. Wysocki struct acpi_device_wakeup *wakeup = &adev->wakeup; 87699d8845eSRafael J. Wysocki 87799d8845eSRafael J. Wysocki mutex_lock(&acpi_wakeup_lock); 87899d8845eSRafael J. Wysocki 87999d8845eSRafael J. Wysocki if (!wakeup->enable_count) 88099d8845eSRafael J. Wysocki goto out; 88199d8845eSRafael J. Wysocki 882dee8370cSRafael J. Wysocki acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number); 883dee8370cSRafael J. Wysocki acpi_disable_wakeup_device_power(adev); 88499d8845eSRafael J. Wysocki 88599d8845eSRafael J. Wysocki wakeup->enable_count--; 88699d8845eSRafael J. Wysocki 88799d8845eSRafael J. Wysocki out: 88899d8845eSRafael J. Wysocki mutex_unlock(&acpi_wakeup_lock); 889dee8370cSRafael J. Wysocki } 890dee8370cSRafael J. Wysocki 8917482c5cbSRafael J. Wysocki /** 8927482c5cbSRafael J. Wysocki * acpi_pm_set_device_wakeup - Enable/disable remote wakeup for given device. 8937482c5cbSRafael J. Wysocki * @dev: Device to enable/disable to generate wakeup events. 8947482c5cbSRafael J. Wysocki * @enable: Whether to enable or disable the wakeup functionality. 8957482c5cbSRafael J. Wysocki */ 8967482c5cbSRafael J. Wysocki int acpi_pm_set_device_wakeup(struct device *dev, bool enable) 897a6ae7594SRafael J. Wysocki { 898a6ae7594SRafael J. Wysocki struct acpi_device *adev; 899a6ae7594SRafael J. Wysocki int error; 900a6ae7594SRafael J. Wysocki 90117653a3eSRafael J. Wysocki adev = ACPI_COMPANION(dev); 90217653a3eSRafael J. Wysocki if (!adev) { 90317653a3eSRafael J. Wysocki dev_dbg(dev, "ACPI companion missing in %s!\n", __func__); 904a6ae7594SRafael J. Wysocki return -ENODEV; 905a6ae7594SRafael J. Wysocki } 906a6ae7594SRafael J. Wysocki 9074d183d04SRafael J. Wysocki if (!acpi_device_can_wakeup(adev)) 9084d183d04SRafael J. Wysocki return -EINVAL; 9094d183d04SRafael J. Wysocki 91099d8845eSRafael J. Wysocki if (!enable) { 91199d8845eSRafael J. Wysocki acpi_device_wakeup_disable(adev); 91299d8845eSRafael J. Wysocki dev_dbg(dev, "Wakeup disabled by ACPI\n"); 91399d8845eSRafael J. Wysocki return 0; 91499d8845eSRafael J. Wysocki } 91599d8845eSRafael J. Wysocki 9167482c5cbSRafael J. Wysocki error = __acpi_device_wakeup_enable(adev, acpi_target_system_state()); 917a6ae7594SRafael J. Wysocki if (!error) 91899d8845eSRafael J. Wysocki dev_dbg(dev, "Wakeup enabled by ACPI\n"); 919a6ae7594SRafael J. Wysocki 920a6ae7594SRafael J. Wysocki return error; 921a6ae7594SRafael J. Wysocki } 9221ba51a7cSRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_pm_set_device_wakeup); 9231ba51a7cSRafael J. Wysocki 9241ba51a7cSRafael J. Wysocki /** 925e5cc8ef3SRafael J. Wysocki * acpi_dev_pm_low_power - Put ACPI device into a low-power state. 926e5cc8ef3SRafael J. Wysocki * @dev: Device to put into a low-power state. 927e5cc8ef3SRafael J. Wysocki * @adev: ACPI device node corresponding to @dev. 928e5cc8ef3SRafael J. Wysocki * @system_state: System state to choose the device state for. 929e5cc8ef3SRafael J. Wysocki */ 930e5cc8ef3SRafael J. Wysocki static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev, 931e5cc8ef3SRafael J. Wysocki u32 system_state) 932e5cc8ef3SRafael J. Wysocki { 933fa1675b5SRafael J. Wysocki int ret, state; 934e5cc8ef3SRafael J. Wysocki 935e5cc8ef3SRafael J. Wysocki if (!acpi_device_power_manageable(adev)) 936e5cc8ef3SRafael J. Wysocki return 0; 937e5cc8ef3SRafael J. Wysocki 938fa1675b5SRafael J. Wysocki ret = acpi_dev_pm_get_state(dev, adev, system_state, NULL, &state); 939fa1675b5SRafael J. Wysocki return ret ? ret : acpi_device_set_power(adev, state); 940e5cc8ef3SRafael J. Wysocki } 941e5cc8ef3SRafael J. Wysocki 942e5cc8ef3SRafael J. Wysocki /** 943e5cc8ef3SRafael J. Wysocki * acpi_dev_pm_full_power - Put ACPI device into the full-power state. 944e5cc8ef3SRafael J. Wysocki * @adev: ACPI device node to put into the full-power state. 945e5cc8ef3SRafael J. Wysocki */ 946e5cc8ef3SRafael J. Wysocki static int acpi_dev_pm_full_power(struct acpi_device *adev) 947e5cc8ef3SRafael J. Wysocki { 948e5cc8ef3SRafael J. Wysocki return acpi_device_power_manageable(adev) ? 949e5cc8ef3SRafael J. Wysocki acpi_device_set_power(adev, ACPI_STATE_D0) : 0; 950e5cc8ef3SRafael J. Wysocki } 951e5cc8ef3SRafael J. Wysocki 952e5cc8ef3SRafael J. Wysocki /** 953cbe25ce3SRafael J. Wysocki * acpi_dev_suspend - Put device into a low-power state using ACPI. 954e5cc8ef3SRafael J. Wysocki * @dev: Device to put into a low-power state. 955cbe25ce3SRafael J. Wysocki * @wakeup: Whether or not to enable wakeup for the device. 956e5cc8ef3SRafael J. Wysocki * 957cbe25ce3SRafael J. Wysocki * Put the given device into a low-power state using the standard ACPI 958e5cc8ef3SRafael J. Wysocki * mechanism. Set up remote wakeup if desired, choose the state to put the 959e5cc8ef3SRafael J. Wysocki * device into (this checks if remote wakeup is expected to work too), and set 960e5cc8ef3SRafael J. Wysocki * the power state of the device. 961e5cc8ef3SRafael J. Wysocki */ 962cbe25ce3SRafael J. Wysocki int acpi_dev_suspend(struct device *dev, bool wakeup) 963e5cc8ef3SRafael J. Wysocki { 96479c0373fSRafael J. Wysocki struct acpi_device *adev = ACPI_COMPANION(dev); 965cbe25ce3SRafael J. Wysocki u32 target_state = acpi_target_system_state(); 966e5cc8ef3SRafael J. Wysocki int error; 967e5cc8ef3SRafael J. Wysocki 968e5cc8ef3SRafael J. Wysocki if (!adev) 969e5cc8ef3SRafael J. Wysocki return 0; 970e5cc8ef3SRafael J. Wysocki 971cbe25ce3SRafael J. Wysocki if (wakeup && acpi_device_can_wakeup(adev)) { 972cbe25ce3SRafael J. Wysocki error = acpi_device_wakeup_enable(adev, target_state); 97399d8845eSRafael J. Wysocki if (error) 974e5cc8ef3SRafael J. Wysocki return -EAGAIN; 975cbe25ce3SRafael J. Wysocki } else { 976cbe25ce3SRafael J. Wysocki wakeup = false; 97799d8845eSRafael J. Wysocki } 978e5cc8ef3SRafael J. Wysocki 979cbe25ce3SRafael J. Wysocki error = acpi_dev_pm_low_power(dev, adev, target_state); 980cbe25ce3SRafael J. Wysocki if (error && wakeup) 98199d8845eSRafael J. Wysocki acpi_device_wakeup_disable(adev); 982e5cc8ef3SRafael J. Wysocki 983e5cc8ef3SRafael J. Wysocki return error; 984e5cc8ef3SRafael J. Wysocki } 985cbe25ce3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_suspend); 986e5cc8ef3SRafael J. Wysocki 987e5cc8ef3SRafael J. Wysocki /** 98863705c40SRafael J. Wysocki * acpi_dev_resume - Put device into the full-power state using ACPI. 989e5cc8ef3SRafael J. Wysocki * @dev: Device to put into the full-power state. 990e5cc8ef3SRafael J. Wysocki * 991e5cc8ef3SRafael J. Wysocki * Put the given device into the full-power state using the standard ACPI 99263705c40SRafael J. Wysocki * mechanism. Set the power state of the device to ACPI D0 and disable wakeup. 993e5cc8ef3SRafael J. Wysocki */ 99463705c40SRafael J. Wysocki int acpi_dev_resume(struct device *dev) 995e5cc8ef3SRafael J. Wysocki { 99679c0373fSRafael J. Wysocki struct acpi_device *adev = ACPI_COMPANION(dev); 997e5cc8ef3SRafael J. Wysocki int error; 998e5cc8ef3SRafael J. Wysocki 999e5cc8ef3SRafael J. Wysocki if (!adev) 1000e5cc8ef3SRafael J. Wysocki return 0; 1001e5cc8ef3SRafael J. Wysocki 1002e5cc8ef3SRafael J. Wysocki error = acpi_dev_pm_full_power(adev); 100399d8845eSRafael J. Wysocki acpi_device_wakeup_disable(adev); 1004e5cc8ef3SRafael J. Wysocki return error; 1005e5cc8ef3SRafael J. Wysocki } 100663705c40SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_resume); 1007e5cc8ef3SRafael J. Wysocki 1008e5cc8ef3SRafael J. Wysocki /** 1009e5cc8ef3SRafael J. Wysocki * acpi_subsys_runtime_suspend - Suspend device using ACPI. 1010e5cc8ef3SRafael J. Wysocki * @dev: Device to suspend. 1011e5cc8ef3SRafael J. Wysocki * 1012e5cc8ef3SRafael J. Wysocki * Carry out the generic runtime suspend procedure for @dev and use ACPI to put 1013e5cc8ef3SRafael J. Wysocki * it into a runtime low-power state. 1014e5cc8ef3SRafael J. Wysocki */ 1015e5cc8ef3SRafael J. Wysocki int acpi_subsys_runtime_suspend(struct device *dev) 1016e5cc8ef3SRafael J. Wysocki { 1017e5cc8ef3SRafael J. Wysocki int ret = pm_generic_runtime_suspend(dev); 10183da8236bSXiaofei Tan 1019cbe25ce3SRafael J. Wysocki return ret ? ret : acpi_dev_suspend(dev, true); 1020e5cc8ef3SRafael J. Wysocki } 1021e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend); 1022e5cc8ef3SRafael J. Wysocki 1023e5cc8ef3SRafael J. Wysocki /** 1024e5cc8ef3SRafael J. Wysocki * acpi_subsys_runtime_resume - Resume device using ACPI. 1025e5cc8ef3SRafael J. Wysocki * @dev: Device to Resume. 1026e5cc8ef3SRafael J. Wysocki * 1027e5cc8ef3SRafael J. Wysocki * Use ACPI to put the given device into the full-power state and carry out the 1028e5cc8ef3SRafael J. Wysocki * generic runtime resume procedure for it. 1029e5cc8ef3SRafael J. Wysocki */ 1030e5cc8ef3SRafael J. Wysocki int acpi_subsys_runtime_resume(struct device *dev) 1031e5cc8ef3SRafael J. Wysocki { 103263705c40SRafael J. Wysocki int ret = acpi_dev_resume(dev); 10333da8236bSXiaofei Tan 1034e5cc8ef3SRafael J. Wysocki return ret ? ret : pm_generic_runtime_resume(dev); 1035e5cc8ef3SRafael J. Wysocki } 1036e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume); 1037e5cc8ef3SRafael J. Wysocki 1038e5cc8ef3SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP 1039c2ebf788SUlf Hansson static bool acpi_dev_needs_resume(struct device *dev, struct acpi_device *adev) 1040c2ebf788SUlf Hansson { 1041c2ebf788SUlf Hansson u32 sys_target = acpi_target_system_state(); 1042c2ebf788SUlf Hansson int ret, state; 1043c2ebf788SUlf Hansson 10449a51c6b1SRafael J. Wysocki if (!pm_runtime_suspended(dev) || !adev || (adev->wakeup.flags.valid && 10459a51c6b1SRafael J. Wysocki device_may_wakeup(dev) != !!adev->wakeup.prepare_count)) 1046c2ebf788SUlf Hansson return true; 1047c2ebf788SUlf Hansson 1048c2ebf788SUlf Hansson if (sys_target == ACPI_STATE_S0) 1049c2ebf788SUlf Hansson return false; 1050c2ebf788SUlf Hansson 1051c2ebf788SUlf Hansson if (adev->power.flags.dsw_present) 1052c2ebf788SUlf Hansson return true; 1053c2ebf788SUlf Hansson 1054c2ebf788SUlf Hansson ret = acpi_dev_pm_get_state(dev, adev, sys_target, NULL, &state); 1055c2ebf788SUlf Hansson if (ret) 1056c2ebf788SUlf Hansson return true; 1057c2ebf788SUlf Hansson 1058c2ebf788SUlf Hansson return state != adev->power.state; 1059c2ebf788SUlf Hansson } 1060c2ebf788SUlf Hansson 1061e5cc8ef3SRafael J. Wysocki /** 1062e5cc8ef3SRafael J. Wysocki * acpi_subsys_prepare - Prepare device for system transition to a sleep state. 1063e5cc8ef3SRafael J. Wysocki * @dev: Device to prepare. 1064e5cc8ef3SRafael J. Wysocki */ 1065e5cc8ef3SRafael J. Wysocki int acpi_subsys_prepare(struct device *dev) 1066e5cc8ef3SRafael J. Wysocki { 1067f25c0ae2SRafael J. Wysocki struct acpi_device *adev = ACPI_COMPANION(dev); 106892858c47SRafael J. Wysocki 106908810a41SRafael J. Wysocki if (dev->driver && dev->driver->pm && dev->driver->pm->prepare) { 107008810a41SRafael J. Wysocki int ret = dev->driver->pm->prepare(dev); 107108810a41SRafael J. Wysocki 1072f25c0ae2SRafael J. Wysocki if (ret < 0) 1073f25c0ae2SRafael J. Wysocki return ret; 1074f25c0ae2SRafael J. Wysocki 107508810a41SRafael J. Wysocki if (!ret && dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_PREPARE)) 1076f25c0ae2SRafael J. Wysocki return 0; 107708810a41SRafael J. Wysocki } 1078f25c0ae2SRafael J. Wysocki 1079c2ebf788SUlf Hansson return !acpi_dev_needs_resume(dev, adev); 1080e5cc8ef3SRafael J. Wysocki } 1081e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_prepare); 1082e5cc8ef3SRafael J. Wysocki 1083e5cc8ef3SRafael J. Wysocki /** 1084e4da817dSUlf Hansson * acpi_subsys_complete - Finalize device's resume during system resume. 1085e4da817dSUlf Hansson * @dev: Device to handle. 1086e4da817dSUlf Hansson */ 1087e4da817dSUlf Hansson void acpi_subsys_complete(struct device *dev) 1088e4da817dSUlf Hansson { 1089e4da817dSUlf Hansson pm_generic_complete(dev); 1090e4da817dSUlf Hansson /* 1091e4da817dSUlf Hansson * If the device had been runtime-suspended before the system went into 1092e4da817dSUlf Hansson * the sleep state it is going out of and it has never been resumed till 1093e4da817dSUlf Hansson * now, resume it in case the firmware powered it up. 1094e4da817dSUlf Hansson */ 1095db68daffSRafael J. Wysocki if (pm_runtime_suspended(dev) && pm_resume_via_firmware()) 1096e4da817dSUlf Hansson pm_request_resume(dev); 1097e4da817dSUlf Hansson } 1098e4da817dSUlf Hansson EXPORT_SYMBOL_GPL(acpi_subsys_complete); 1099e4da817dSUlf Hansson 1100e4da817dSUlf Hansson /** 110192858c47SRafael J. Wysocki * acpi_subsys_suspend - Run the device driver's suspend callback. 110292858c47SRafael J. Wysocki * @dev: Device to handle. 110392858c47SRafael J. Wysocki * 110405087360SRafael J. Wysocki * Follow PCI and resume devices from runtime suspend before running their 110505087360SRafael J. Wysocki * system suspend callbacks, unless the driver can cope with runtime-suspended 110605087360SRafael J. Wysocki * devices during system suspend and there are no ACPI-specific reasons for 110705087360SRafael J. Wysocki * resuming them. 110892858c47SRafael J. Wysocki */ 110992858c47SRafael J. Wysocki int acpi_subsys_suspend(struct device *dev) 111092858c47SRafael J. Wysocki { 111105087360SRafael J. Wysocki if (!dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND) || 111205087360SRafael J. Wysocki acpi_dev_needs_resume(dev, ACPI_COMPANION(dev))) 111392858c47SRafael J. Wysocki pm_runtime_resume(dev); 111405087360SRafael J. Wysocki 111592858c47SRafael J. Wysocki return pm_generic_suspend(dev); 111692858c47SRafael J. Wysocki } 11174cf563c5SHeikki Krogerus EXPORT_SYMBOL_GPL(acpi_subsys_suspend); 111892858c47SRafael J. Wysocki 111992858c47SRafael J. Wysocki /** 1120e5cc8ef3SRafael J. Wysocki * acpi_subsys_suspend_late - Suspend device using ACPI. 1121e5cc8ef3SRafael J. Wysocki * @dev: Device to suspend. 1122e5cc8ef3SRafael J. Wysocki * 1123e5cc8ef3SRafael J. Wysocki * Carry out the generic late suspend procedure for @dev and use ACPI to put 1124e5cc8ef3SRafael J. Wysocki * it into a low-power state during system transition into a sleep state. 1125e5cc8ef3SRafael J. Wysocki */ 1126e5cc8ef3SRafael J. Wysocki int acpi_subsys_suspend_late(struct device *dev) 1127e5cc8ef3SRafael J. Wysocki { 112805087360SRafael J. Wysocki int ret; 112905087360SRafael J. Wysocki 1130fa2bfeadSRafael J. Wysocki if (dev_pm_skip_suspend(dev)) 113105087360SRafael J. Wysocki return 0; 113205087360SRafael J. Wysocki 113305087360SRafael J. Wysocki ret = pm_generic_suspend_late(dev); 1134cbe25ce3SRafael J. Wysocki return ret ? ret : acpi_dev_suspend(dev, device_may_wakeup(dev)); 1135e5cc8ef3SRafael J. Wysocki } 1136e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late); 1137e5cc8ef3SRafael J. Wysocki 1138e5cc8ef3SRafael J. Wysocki /** 113905087360SRafael J. Wysocki * acpi_subsys_suspend_noirq - Run the device driver's "noirq" suspend callback. 114005087360SRafael J. Wysocki * @dev: Device to suspend. 114105087360SRafael J. Wysocki */ 114205087360SRafael J. Wysocki int acpi_subsys_suspend_noirq(struct device *dev) 114305087360SRafael J. Wysocki { 1144db68daffSRafael J. Wysocki int ret; 114505087360SRafael J. Wysocki 1146fa2bfeadSRafael J. Wysocki if (dev_pm_skip_suspend(dev)) 1147db68daffSRafael J. Wysocki return 0; 1148db68daffSRafael J. Wysocki 1149db68daffSRafael J. Wysocki ret = pm_generic_suspend_noirq(dev); 1150db68daffSRafael J. Wysocki if (ret) 1151db68daffSRafael J. Wysocki return ret; 1152db68daffSRafael J. Wysocki 1153db68daffSRafael J. Wysocki /* 1154db68daffSRafael J. Wysocki * If the target system sleep state is suspend-to-idle, it is sufficient 1155db68daffSRafael J. Wysocki * to check whether or not the device's wakeup settings are good for 1156db68daffSRafael J. Wysocki * runtime PM. Otherwise, the pm_resume_via_firmware() check will cause 1157db68daffSRafael J. Wysocki * acpi_subsys_complete() to take care of fixing up the device's state 1158db68daffSRafael J. Wysocki * anyway, if need be. 1159db68daffSRafael J. Wysocki */ 11600fe8a1beSRafael J. Wysocki if (device_can_wakeup(dev) && !device_may_wakeup(dev)) 11610fe8a1beSRafael J. Wysocki dev->power.may_skip_resume = false; 1162db68daffSRafael J. Wysocki 1163db68daffSRafael J. Wysocki return 0; 116405087360SRafael J. Wysocki } 116505087360SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_suspend_noirq); 116605087360SRafael J. Wysocki 116705087360SRafael J. Wysocki /** 116805087360SRafael J. Wysocki * acpi_subsys_resume_noirq - Run the device driver's "noirq" resume callback. 116905087360SRafael J. Wysocki * @dev: Device to handle. 117005087360SRafael J. Wysocki */ 11713cd7957eSRafael J. Wysocki static int acpi_subsys_resume_noirq(struct device *dev) 117205087360SRafael J. Wysocki { 117376c70cb5SRafael J. Wysocki if (dev_pm_skip_resume(dev)) 1174db68daffSRafael J. Wysocki return 0; 1175db68daffSRafael J. Wysocki 117605087360SRafael J. Wysocki return pm_generic_resume_noirq(dev); 117705087360SRafael J. Wysocki } 117805087360SRafael J. Wysocki 117905087360SRafael J. Wysocki /** 1180e5cc8ef3SRafael J. Wysocki * acpi_subsys_resume_early - Resume device using ACPI. 1181e5cc8ef3SRafael J. Wysocki * @dev: Device to Resume. 1182e5cc8ef3SRafael J. Wysocki * 1183e5cc8ef3SRafael J. Wysocki * Use ACPI to put the given device into the full-power state and carry out the 1184e5cc8ef3SRafael J. Wysocki * generic early resume procedure for it during system transition into the 1185f7599be2SDmitry Torokhov * working state, but only do that if device either defines early resume 1186f7599be2SDmitry Torokhov * handler, or does not define power operations at all. Otherwise powering up 1187f7599be2SDmitry Torokhov * of the device is postponed to the normal resume phase. 1188e5cc8ef3SRafael J. Wysocki */ 11893cd7957eSRafael J. Wysocki static int acpi_subsys_resume_early(struct device *dev) 1190e5cc8ef3SRafael J. Wysocki { 1191f7599be2SDmitry Torokhov const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; 11926e176bf8SRafael J. Wysocki int ret; 11936e176bf8SRafael J. Wysocki 119476c70cb5SRafael J. Wysocki if (dev_pm_skip_resume(dev)) 11956e176bf8SRafael J. Wysocki return 0; 11966e176bf8SRafael J. Wysocki 1197f7599be2SDmitry Torokhov if (pm && !pm->resume_early) { 1198f7599be2SDmitry Torokhov dev_dbg(dev, "postponing D0 transition to normal resume stage\n"); 1199f7599be2SDmitry Torokhov return 0; 1200f7599be2SDmitry Torokhov } 1201f7599be2SDmitry Torokhov 12026e176bf8SRafael J. Wysocki ret = acpi_dev_resume(dev); 1203e5cc8ef3SRafael J. Wysocki return ret ? ret : pm_generic_resume_early(dev); 1204e5cc8ef3SRafael J. Wysocki } 120592858c47SRafael J. Wysocki 120692858c47SRafael J. Wysocki /** 1207f7599be2SDmitry Torokhov * acpi_subsys_resume - Resume device using ACPI. 1208f7599be2SDmitry Torokhov * @dev: Device to Resume. 1209f7599be2SDmitry Torokhov * 1210f7599be2SDmitry Torokhov * Use ACPI to put the given device into the full-power state if it has not been 1211f7599be2SDmitry Torokhov * powered up during early resume phase, and carry out the generic resume 1212f7599be2SDmitry Torokhov * procedure for it during system transition into the working state. 1213f7599be2SDmitry Torokhov */ 1214f7599be2SDmitry Torokhov static int acpi_subsys_resume(struct device *dev) 1215f7599be2SDmitry Torokhov { 1216f7599be2SDmitry Torokhov const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; 1217f7599be2SDmitry Torokhov int ret = 0; 1218f7599be2SDmitry Torokhov 1219f7599be2SDmitry Torokhov if (!dev_pm_skip_resume(dev) && pm && !pm->resume_early) { 1220f7599be2SDmitry Torokhov dev_dbg(dev, "executing postponed D0 transition\n"); 1221f7599be2SDmitry Torokhov ret = acpi_dev_resume(dev); 1222f7599be2SDmitry Torokhov } 1223f7599be2SDmitry Torokhov 1224f7599be2SDmitry Torokhov return ret ? ret : pm_generic_resume(dev); 1225f7599be2SDmitry Torokhov } 1226f7599be2SDmitry Torokhov 1227f7599be2SDmitry Torokhov /** 122892858c47SRafael J. Wysocki * acpi_subsys_freeze - Run the device driver's freeze callback. 122992858c47SRafael J. Wysocki * @dev: Device to handle. 123092858c47SRafael J. Wysocki */ 123192858c47SRafael J. Wysocki int acpi_subsys_freeze(struct device *dev) 123292858c47SRafael J. Wysocki { 123392858c47SRafael J. Wysocki /* 1234501debd4SRafael J. Wysocki * Resume all runtime-suspended devices before creating a snapshot 1235501debd4SRafael J. Wysocki * image of system memory, because the restore kernel generally cannot 1236501debd4SRafael J. Wysocki * be expected to always handle them consistently and they need to be 1237501debd4SRafael J. Wysocki * put into the runtime-active metastate during system resume anyway, 1238501debd4SRafael J. Wysocki * so it is better to ensure that the state saved in the image will be 1239501debd4SRafael J. Wysocki * always consistent with that. 124092858c47SRafael J. Wysocki */ 124192858c47SRafael J. Wysocki pm_runtime_resume(dev); 124205087360SRafael J. Wysocki 124392858c47SRafael J. Wysocki return pm_generic_freeze(dev); 124492858c47SRafael J. Wysocki } 12454cf563c5SHeikki Krogerus EXPORT_SYMBOL_GPL(acpi_subsys_freeze); 124692858c47SRafael J. Wysocki 124705087360SRafael J. Wysocki /** 12483cd7957eSRafael J. Wysocki * acpi_subsys_restore_early - Restore device using ACPI. 12493cd7957eSRafael J. Wysocki * @dev: Device to restore. 125005087360SRafael J. Wysocki */ 12513cd7957eSRafael J. Wysocki int acpi_subsys_restore_early(struct device *dev) 125205087360SRafael J. Wysocki { 12533cd7957eSRafael J. Wysocki int ret = acpi_dev_resume(dev); 12543da8236bSXiaofei Tan 12553cd7957eSRafael J. Wysocki return ret ? ret : pm_generic_restore_early(dev); 125605087360SRafael J. Wysocki } 12573cd7957eSRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_restore_early); 1258c95b7595SRafael J. Wysocki 1259c95b7595SRafael J. Wysocki /** 1260c95b7595SRafael J. Wysocki * acpi_subsys_poweroff - Run the device driver's poweroff callback. 1261c95b7595SRafael J. Wysocki * @dev: Device to handle. 1262c95b7595SRafael J. Wysocki * 1263c95b7595SRafael J. Wysocki * Follow PCI and resume devices from runtime suspend before running their 1264c95b7595SRafael J. Wysocki * system poweroff callbacks, unless the driver can cope with runtime-suspended 1265c95b7595SRafael J. Wysocki * devices during system suspend and there are no ACPI-specific reasons for 1266c95b7595SRafael J. Wysocki * resuming them. 1267c95b7595SRafael J. Wysocki */ 1268c95b7595SRafael J. Wysocki int acpi_subsys_poweroff(struct device *dev) 1269c95b7595SRafael J. Wysocki { 1270c95b7595SRafael J. Wysocki if (!dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND) || 1271c95b7595SRafael J. Wysocki acpi_dev_needs_resume(dev, ACPI_COMPANION(dev))) 1272c95b7595SRafael J. Wysocki pm_runtime_resume(dev); 1273c95b7595SRafael J. Wysocki 1274c95b7595SRafael J. Wysocki return pm_generic_poweroff(dev); 1275c95b7595SRafael J. Wysocki } 1276c95b7595SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_poweroff); 1277c95b7595SRafael J. Wysocki 1278c95b7595SRafael J. Wysocki /** 1279c95b7595SRafael J. Wysocki * acpi_subsys_poweroff_late - Run the device driver's poweroff callback. 1280c95b7595SRafael J. Wysocki * @dev: Device to handle. 1281c95b7595SRafael J. Wysocki * 1282c95b7595SRafael J. Wysocki * Carry out the generic late poweroff procedure for @dev and use ACPI to put 1283c95b7595SRafael J. Wysocki * it into a low-power state during system transition into a sleep state. 1284c95b7595SRafael J. Wysocki */ 1285c95b7595SRafael J. Wysocki static int acpi_subsys_poweroff_late(struct device *dev) 1286c95b7595SRafael J. Wysocki { 1287c95b7595SRafael J. Wysocki int ret; 128805087360SRafael J. Wysocki 1289fa2bfeadSRafael J. Wysocki if (dev_pm_skip_suspend(dev)) 129005087360SRafael J. Wysocki return 0; 129105087360SRafael J. Wysocki 1292c95b7595SRafael J. Wysocki ret = pm_generic_poweroff_late(dev); 1293c95b7595SRafael J. Wysocki if (ret) 1294c95b7595SRafael J. Wysocki return ret; 1295c95b7595SRafael J. Wysocki 1296c95b7595SRafael J. Wysocki return acpi_dev_suspend(dev, device_may_wakeup(dev)); 129705087360SRafael J. Wysocki } 129805087360SRafael J. Wysocki 129905087360SRafael J. Wysocki /** 1300c95b7595SRafael J. Wysocki * acpi_subsys_poweroff_noirq - Run the driver's "noirq" poweroff callback. 1301c95b7595SRafael J. Wysocki * @dev: Device to suspend. 130205087360SRafael J. Wysocki */ 1303c95b7595SRafael J. Wysocki static int acpi_subsys_poweroff_noirq(struct device *dev) 130405087360SRafael J. Wysocki { 1305fa2bfeadSRafael J. Wysocki if (dev_pm_skip_suspend(dev)) 130605087360SRafael J. Wysocki return 0; 130705087360SRafael J. Wysocki 1308c95b7595SRafael J. Wysocki return pm_generic_poweroff_noirq(dev); 130905087360SRafael J. Wysocki } 1310e5cc8ef3SRafael J. Wysocki #endif /* CONFIG_PM_SLEEP */ 1311e5cc8ef3SRafael J. Wysocki 1312e5cc8ef3SRafael J. Wysocki static struct dev_pm_domain acpi_general_pm_domain = { 1313e5cc8ef3SRafael J. Wysocki .ops = { 1314e5cc8ef3SRafael J. Wysocki .runtime_suspend = acpi_subsys_runtime_suspend, 1315e5cc8ef3SRafael J. Wysocki .runtime_resume = acpi_subsys_runtime_resume, 1316e5cc8ef3SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP 1317e5cc8ef3SRafael J. Wysocki .prepare = acpi_subsys_prepare, 1318e4da817dSUlf Hansson .complete = acpi_subsys_complete, 131992858c47SRafael J. Wysocki .suspend = acpi_subsys_suspend, 1320f7599be2SDmitry Torokhov .resume = acpi_subsys_resume, 1321e5cc8ef3SRafael J. Wysocki .suspend_late = acpi_subsys_suspend_late, 132205087360SRafael J. Wysocki .suspend_noirq = acpi_subsys_suspend_noirq, 132305087360SRafael J. Wysocki .resume_noirq = acpi_subsys_resume_noirq, 1324e5cc8ef3SRafael J. Wysocki .resume_early = acpi_subsys_resume_early, 132592858c47SRafael J. Wysocki .freeze = acpi_subsys_freeze, 1326c95b7595SRafael J. Wysocki .poweroff = acpi_subsys_poweroff, 1327c95b7595SRafael J. Wysocki .poweroff_late = acpi_subsys_poweroff_late, 1328c95b7595SRafael J. Wysocki .poweroff_noirq = acpi_subsys_poweroff_noirq, 13293cd7957eSRafael J. Wysocki .restore_early = acpi_subsys_restore_early, 1330e5cc8ef3SRafael J. Wysocki #endif 1331e5cc8ef3SRafael J. Wysocki }, 1332e5cc8ef3SRafael J. Wysocki }; 1333e5cc8ef3SRafael J. Wysocki 1334e5cc8ef3SRafael J. Wysocki /** 133591d66cd2SUlf Hansson * acpi_dev_pm_detach - Remove ACPI power management from the device. 133691d66cd2SUlf Hansson * @dev: Device to take care of. 133791d66cd2SUlf Hansson * @power_off: Whether or not to try to remove power from the device. 133891d66cd2SUlf Hansson * 133991d66cd2SUlf Hansson * Remove the device from the general ACPI PM domain and remove its wakeup 134091d66cd2SUlf Hansson * notifier. If @power_off is set, additionally remove power from the device if 134191d66cd2SUlf Hansson * possible. 134291d66cd2SUlf Hansson * 134391d66cd2SUlf Hansson * Callers must ensure proper synchronization of this function with power 134491d66cd2SUlf Hansson * management callbacks. 134591d66cd2SUlf Hansson */ 134691d66cd2SUlf Hansson static void acpi_dev_pm_detach(struct device *dev, bool power_off) 134791d66cd2SUlf Hansson { 134891d66cd2SUlf Hansson struct acpi_device *adev = ACPI_COMPANION(dev); 134991d66cd2SUlf Hansson 135091d66cd2SUlf Hansson if (adev && dev->pm_domain == &acpi_general_pm_domain) { 1351989561deSTomeu Vizoso dev_pm_domain_set(dev, NULL); 135291d66cd2SUlf Hansson acpi_remove_pm_notifier(adev); 135391d66cd2SUlf Hansson if (power_off) { 135491d66cd2SUlf Hansson /* 135591d66cd2SUlf Hansson * If the device's PM QoS resume latency limit or flags 135691d66cd2SUlf Hansson * have been exposed to user space, they have to be 135791d66cd2SUlf Hansson * hidden at this point, so that they don't affect the 135891d66cd2SUlf Hansson * choice of the low-power state to put the device into. 135991d66cd2SUlf Hansson */ 136091d66cd2SUlf Hansson dev_pm_qos_hide_latency_limit(dev); 136191d66cd2SUlf Hansson dev_pm_qos_hide_flags(dev); 136299d8845eSRafael J. Wysocki acpi_device_wakeup_disable(adev); 136391d66cd2SUlf Hansson acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0); 136491d66cd2SUlf Hansson } 136591d66cd2SUlf Hansson } 136691d66cd2SUlf Hansson } 136791d66cd2SUlf Hansson 136891d66cd2SUlf Hansson /** 1369e5cc8ef3SRafael J. Wysocki * acpi_dev_pm_attach - Prepare device for ACPI power management. 1370e5cc8ef3SRafael J. Wysocki * @dev: Device to prepare. 1371b88ce2a4SRafael J. Wysocki * @power_on: Whether or not to power on the device. 1372e5cc8ef3SRafael J. Wysocki * 1373e5cc8ef3SRafael J. Wysocki * If @dev has a valid ACPI handle that has a valid struct acpi_device object 1374e5cc8ef3SRafael J. Wysocki * attached to it, install a wakeup notification handler for the device and 1375b88ce2a4SRafael J. Wysocki * add it to the general ACPI PM domain. If @power_on is set, the device will 1376b88ce2a4SRafael J. Wysocki * be put into the ACPI D0 state before the function returns. 1377e5cc8ef3SRafael J. Wysocki * 1378e5cc8ef3SRafael J. Wysocki * This assumes that the @dev's bus type uses generic power management callbacks 1379e5cc8ef3SRafael J. Wysocki * (or doesn't use any power management callbacks at all). 1380e5cc8ef3SRafael J. Wysocki * 1381e5cc8ef3SRafael J. Wysocki * Callers must ensure proper synchronization of this function with power 1382e5cc8ef3SRafael J. Wysocki * management callbacks. 1383e5cc8ef3SRafael J. Wysocki */ 1384b88ce2a4SRafael J. Wysocki int acpi_dev_pm_attach(struct device *dev, bool power_on) 1385e5cc8ef3SRafael J. Wysocki { 1386b9ea0baeSRafael J. Wysocki /* 1387b9ea0baeSRafael J. Wysocki * Skip devices whose ACPI companions match the device IDs below, 1388b9ea0baeSRafael J. Wysocki * because they require special power management handling incompatible 1389b9ea0baeSRafael J. Wysocki * with the generic ACPI PM domain. 1390b9ea0baeSRafael J. Wysocki */ 1391b9ea0baeSRafael J. Wysocki static const struct acpi_device_id special_pm_ids[] = { 1392b9370dceSRafael J. Wysocki ACPI_FAN_DEVICE_IDS, 1393b9ea0baeSRafael J. Wysocki {} 1394b9ea0baeSRafael J. Wysocki }; 139579c0373fSRafael J. Wysocki struct acpi_device *adev = ACPI_COMPANION(dev); 1396e5cc8ef3SRafael J. Wysocki 1397b9ea0baeSRafael J. Wysocki if (!adev || !acpi_match_device_ids(adev, special_pm_ids)) 1398919b7308SUlf Hansson return 0; 1399e5cc8ef3SRafael J. Wysocki 1400712e960fSMika Westerberg /* 1401712e960fSMika Westerberg * Only attach the power domain to the first device if the 1402712e960fSMika Westerberg * companion is shared by multiple. This is to prevent doing power 1403712e960fSMika Westerberg * management twice. 1404712e960fSMika Westerberg */ 1405712e960fSMika Westerberg if (!acpi_device_is_first_physical_node(adev, dev)) 1406919b7308SUlf Hansson return 0; 1407712e960fSMika Westerberg 1408c072530fSRafael J. Wysocki acpi_add_pm_notifier(adev, dev, acpi_pm_notify_work_func); 1409989561deSTomeu Vizoso dev_pm_domain_set(dev, &acpi_general_pm_domain); 1410b88ce2a4SRafael J. Wysocki if (power_on) { 1411b88ce2a4SRafael J. Wysocki acpi_dev_pm_full_power(adev); 141299d8845eSRafael J. Wysocki acpi_device_wakeup_disable(adev); 1413b88ce2a4SRafael J. Wysocki } 141486f1e15fSUlf Hansson 141586f1e15fSUlf Hansson dev->pm_domain->detach = acpi_dev_pm_detach; 1416919b7308SUlf Hansson return 1; 1417e5cc8ef3SRafael J. Wysocki } 1418e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_pm_attach); 14192744d7a0SMario Limonciello 14202744d7a0SMario Limonciello /** 14212744d7a0SMario Limonciello * acpi_storage_d3 - Check if D3 should be used in the suspend path 14222744d7a0SMario Limonciello * @dev: Device to check 14232744d7a0SMario Limonciello * 14242744d7a0SMario Limonciello * Return %true if the platform firmware wants @dev to be programmed 14252744d7a0SMario Limonciello * into D3hot or D3cold (if supported) in the suspend path, or %false 14262744d7a0SMario Limonciello * when there is no specific preference. On some platforms, if this 14272744d7a0SMario Limonciello * hint is ignored, @dev may remain unresponsive after suspending the 14282744d7a0SMario Limonciello * platform as a whole. 14292744d7a0SMario Limonciello * 14302744d7a0SMario Limonciello * Although the property has storage in the name it actually is 14312744d7a0SMario Limonciello * applied to the PCIe slot and plugging in a non-storage device the 14322744d7a0SMario Limonciello * same platform restrictions will likely apply. 14332744d7a0SMario Limonciello */ 14342744d7a0SMario Limonciello bool acpi_storage_d3(struct device *dev) 14352744d7a0SMario Limonciello { 14362744d7a0SMario Limonciello struct acpi_device *adev = ACPI_COMPANION(dev); 14372744d7a0SMario Limonciello u8 val; 14382744d7a0SMario Limonciello 14396485fc18SMario Limonciello if (force_storage_d3()) 14406485fc18SMario Limonciello return true; 14416485fc18SMario Limonciello 14422744d7a0SMario Limonciello if (!adev) 14432744d7a0SMario Limonciello return false; 14442744d7a0SMario Limonciello if (fwnode_property_read_u8(acpi_fwnode_handle(adev), "StorageD3Enable", 14452744d7a0SMario Limonciello &val)) 14462744d7a0SMario Limonciello return false; 14472744d7a0SMario Limonciello return val == 1; 14482744d7a0SMario Limonciello } 14492744d7a0SMario Limonciello EXPORT_SYMBOL_GPL(acpi_storage_d3); 14502744d7a0SMario Limonciello 1451b82a7df4SSakari Ailus /** 1452b82a7df4SSakari Ailus * acpi_dev_state_d0 - Tell if the device is in D0 power state 1453b82a7df4SSakari Ailus * @dev: Physical device the ACPI power state of which to check 1454b82a7df4SSakari Ailus * 1455b82a7df4SSakari Ailus * On a system without ACPI, return true. On a system with ACPI, return true if 1456b82a7df4SSakari Ailus * the current ACPI power state of the device is D0, or false otherwise. 1457b82a7df4SSakari Ailus * 1458b82a7df4SSakari Ailus * Note that the power state of a device is not well-defined after it has been 1459b82a7df4SSakari Ailus * passed to acpi_device_set_power() and before that function returns, so it is 1460b82a7df4SSakari Ailus * not valid to ask for the ACPI power state of the device in that time frame. 1461b82a7df4SSakari Ailus * 1462b82a7df4SSakari Ailus * This function is intended to be used in a driver's probe or remove 1463b82a7df4SSakari Ailus * function. See Documentation/firmware-guide/acpi/low-power-probe.rst for 1464b82a7df4SSakari Ailus * more information. 1465b82a7df4SSakari Ailus */ 1466b82a7df4SSakari Ailus bool acpi_dev_state_d0(struct device *dev) 1467b82a7df4SSakari Ailus { 1468b82a7df4SSakari Ailus struct acpi_device *adev = ACPI_COMPANION(dev); 1469b82a7df4SSakari Ailus 1470b82a7df4SSakari Ailus if (!adev) 1471b82a7df4SSakari Ailus return true; 1472b82a7df4SSakari Ailus 1473b82a7df4SSakari Ailus return adev->power.state == ACPI_STATE_D0; 1474b82a7df4SSakari Ailus } 1475b82a7df4SSakari Ailus EXPORT_SYMBOL_GPL(acpi_dev_state_d0); 1476b82a7df4SSakari Ailus 1477ec4602a9SRafael J. Wysocki #endif /* CONFIG_PM */ 1478