1ec2cd81cSRafael J. Wysocki /* 2ec2cd81cSRafael J. Wysocki * drivers/acpi/device_pm.c - ACPI device power management routines. 3ec2cd81cSRafael J. Wysocki * 4ec2cd81cSRafael J. Wysocki * Copyright (C) 2012, Intel Corp. 5ec2cd81cSRafael J. Wysocki * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com> 6ec2cd81cSRafael J. Wysocki * 7ec2cd81cSRafael J. Wysocki * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 8ec2cd81cSRafael J. Wysocki * 9ec2cd81cSRafael J. Wysocki * This program is free software; you can redistribute it and/or modify 10ec2cd81cSRafael J. Wysocki * it under the terms of the GNU General Public License version 2 as published 11ec2cd81cSRafael J. Wysocki * by the Free Software Foundation. 12ec2cd81cSRafael J. Wysocki * 13ec2cd81cSRafael J. Wysocki * This program is distributed in the hope that it will be useful, but 14ec2cd81cSRafael J. Wysocki * WITHOUT ANY WARRANTY; without even the implied warranty of 15ec2cd81cSRafael J. Wysocki * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16ec2cd81cSRafael J. Wysocki * General Public License for more details. 17ec2cd81cSRafael J. Wysocki * 18ec2cd81cSRafael J. Wysocki * You should have received a copy of the GNU General Public License along 19ec2cd81cSRafael J. Wysocki * with this program; if not, write to the Free Software Foundation, Inc., 20ec2cd81cSRafael J. Wysocki * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 21ec2cd81cSRafael J. Wysocki * 22ec2cd81cSRafael J. Wysocki * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 23ec2cd81cSRafael J. Wysocki */ 24ec2cd81cSRafael J. Wysocki 257b199811SRafael J. Wysocki #include <linux/acpi.h> 2686b3832cSRafael J. Wysocki #include <linux/export.h> 27ec2cd81cSRafael J. Wysocki #include <linux/mutex.h> 2886b3832cSRafael J. Wysocki #include <linux/pm_qos.h> 29cd7bd02dSRafael J. Wysocki #include <linux/pm_runtime.h> 30ec2cd81cSRafael J. Wysocki 319ce4e607SRafael J. Wysocki #include "internal.h" 329ce4e607SRafael J. Wysocki 339ce4e607SRafael J. Wysocki #define _COMPONENT ACPI_POWER_COMPONENT 349ce4e607SRafael J. Wysocki ACPI_MODULE_NAME("device_pm"); 35ec2cd81cSRafael J. Wysocki 3686b3832cSRafael J. Wysocki /** 379ce4e607SRafael J. Wysocki * acpi_power_state_string - String representation of ACPI device power state. 389ce4e607SRafael J. Wysocki * @state: ACPI device power state to return the string representation of. 399ce4e607SRafael J. Wysocki */ 409ce4e607SRafael J. Wysocki const char *acpi_power_state_string(int state) 419ce4e607SRafael J. Wysocki { 429ce4e607SRafael J. Wysocki switch (state) { 439ce4e607SRafael J. Wysocki case ACPI_STATE_D0: 449ce4e607SRafael J. Wysocki return "D0"; 459ce4e607SRafael J. Wysocki case ACPI_STATE_D1: 469ce4e607SRafael J. Wysocki return "D1"; 479ce4e607SRafael J. Wysocki case ACPI_STATE_D2: 489ce4e607SRafael J. Wysocki return "D2"; 499ce4e607SRafael J. Wysocki case ACPI_STATE_D3_HOT: 509ce4e607SRafael J. Wysocki return "D3hot"; 519ce4e607SRafael J. Wysocki case ACPI_STATE_D3_COLD: 52898fee4fSRafael J. Wysocki return "D3cold"; 539ce4e607SRafael J. Wysocki default: 549ce4e607SRafael J. Wysocki return "(unknown)"; 559ce4e607SRafael J. Wysocki } 569ce4e607SRafael J. Wysocki } 579ce4e607SRafael J. Wysocki 589ce4e607SRafael J. Wysocki /** 599ce4e607SRafael J. Wysocki * acpi_device_get_power - Get power state of an ACPI device. 609ce4e607SRafael J. Wysocki * @device: Device to get the power state of. 619ce4e607SRafael J. Wysocki * @state: Place to store the power state of the device. 629ce4e607SRafael J. Wysocki * 639ce4e607SRafael J. Wysocki * This function does not update the device's power.state field, but it may 649ce4e607SRafael J. Wysocki * update its parent's power.state field (when the parent's power state is 659ce4e607SRafael J. Wysocki * unknown and the device's power state turns out to be D0). 669ce4e607SRafael J. Wysocki */ 679ce4e607SRafael J. Wysocki int acpi_device_get_power(struct acpi_device *device, int *state) 689ce4e607SRafael J. Wysocki { 699ce4e607SRafael J. Wysocki int result = ACPI_STATE_UNKNOWN; 709ce4e607SRafael J. Wysocki 719ce4e607SRafael J. Wysocki if (!device || !state) 729ce4e607SRafael J. Wysocki return -EINVAL; 739ce4e607SRafael J. Wysocki 749ce4e607SRafael J. Wysocki if (!device->flags.power_manageable) { 759ce4e607SRafael J. Wysocki /* TBD: Non-recursive algorithm for walking up hierarchy. */ 769ce4e607SRafael J. Wysocki *state = device->parent ? 779ce4e607SRafael J. Wysocki device->parent->power.state : ACPI_STATE_D0; 789ce4e607SRafael J. Wysocki goto out; 799ce4e607SRafael J. Wysocki } 809ce4e607SRafael J. Wysocki 819ce4e607SRafael J. Wysocki /* 8275eb2d13SRafael J. Wysocki * Get the device's power state from power resources settings and _PSC, 8375eb2d13SRafael J. Wysocki * if available. 849ce4e607SRafael J. Wysocki */ 8575eb2d13SRafael J. Wysocki if (device->power.flags.power_resources) { 869ce4e607SRafael J. Wysocki int error = acpi_power_get_inferred_state(device, &result); 879ce4e607SRafael J. Wysocki if (error) 889ce4e607SRafael J. Wysocki return error; 8975eb2d13SRafael J. Wysocki } 9075eb2d13SRafael J. Wysocki if (device->power.flags.explicit_get) { 9175eb2d13SRafael J. Wysocki acpi_handle handle = device->handle; 9275eb2d13SRafael J. Wysocki unsigned long long psc; 9375eb2d13SRafael J. Wysocki acpi_status status; 9475eb2d13SRafael J. Wysocki 9575eb2d13SRafael J. Wysocki status = acpi_evaluate_integer(handle, "_PSC", NULL, &psc); 9675eb2d13SRafael J. Wysocki if (ACPI_FAILURE(status)) 9775eb2d13SRafael J. Wysocki return -ENODEV; 9875eb2d13SRafael J. Wysocki 9975eb2d13SRafael J. Wysocki /* 10075eb2d13SRafael J. Wysocki * The power resources settings may indicate a power state 10175eb2d13SRafael J. Wysocki * shallower than the actual power state of the device. 10275eb2d13SRafael J. Wysocki * 10375eb2d13SRafael J. Wysocki * Moreover, on systems predating ACPI 4.0, if the device 10475eb2d13SRafael J. Wysocki * doesn't depend on any power resources and _PSC returns 3, 10575eb2d13SRafael J. Wysocki * that means "power off". We need to maintain compatibility 10675eb2d13SRafael J. Wysocki * with those systems. 10775eb2d13SRafael J. Wysocki */ 10875eb2d13SRafael J. Wysocki if (psc > result && psc < ACPI_STATE_D3_COLD) 10975eb2d13SRafael J. Wysocki result = psc; 11075eb2d13SRafael J. Wysocki else if (result == ACPI_STATE_UNKNOWN) 11175eb2d13SRafael J. Wysocki result = psc > ACPI_STATE_D2 ? ACPI_STATE_D3_COLD : psc; 1129ce4e607SRafael J. Wysocki } 1139ce4e607SRafael J. Wysocki 1149ce4e607SRafael J. Wysocki /* 1159ce4e607SRafael J. Wysocki * If we were unsure about the device parent's power state up to this 1169ce4e607SRafael J. Wysocki * point, the fact that the device is in D0 implies that the parent has 117644f17adSMika Westerberg * to be in D0 too, except if ignore_parent is set. 1189ce4e607SRafael J. Wysocki */ 119644f17adSMika Westerberg if (!device->power.flags.ignore_parent && device->parent 120644f17adSMika Westerberg && device->parent->power.state == ACPI_STATE_UNKNOWN 1219ce4e607SRafael J. Wysocki && result == ACPI_STATE_D0) 1229ce4e607SRafael J. Wysocki device->parent->power.state = ACPI_STATE_D0; 1239ce4e607SRafael J. Wysocki 1249ce4e607SRafael J. Wysocki *state = result; 1259ce4e607SRafael J. Wysocki 1269ce4e607SRafael J. Wysocki out: 1279ce4e607SRafael J. Wysocki ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] power state is %s\n", 1289ce4e607SRafael J. Wysocki device->pnp.bus_id, acpi_power_state_string(*state))); 1299ce4e607SRafael J. Wysocki 1309ce4e607SRafael J. Wysocki return 0; 1319ce4e607SRafael J. Wysocki } 1329ce4e607SRafael J. Wysocki 1339c0f45e3SRafael J. Wysocki static int acpi_dev_pm_explicit_set(struct acpi_device *adev, int state) 1349c0f45e3SRafael J. Wysocki { 1359c0f45e3SRafael J. Wysocki if (adev->power.states[state].flags.explicit_set) { 1369c0f45e3SRafael J. Wysocki char method[5] = { '_', 'P', 'S', '0' + state, '\0' }; 1379c0f45e3SRafael J. Wysocki acpi_status status; 1389c0f45e3SRafael J. Wysocki 1399c0f45e3SRafael J. Wysocki status = acpi_evaluate_object(adev->handle, method, NULL, NULL); 1409c0f45e3SRafael J. Wysocki if (ACPI_FAILURE(status)) 1419c0f45e3SRafael J. Wysocki return -ENODEV; 1429c0f45e3SRafael J. Wysocki } 1439c0f45e3SRafael J. Wysocki return 0; 1449c0f45e3SRafael J. Wysocki } 1459c0f45e3SRafael J. Wysocki 1469ce4e607SRafael J. Wysocki /** 1479ce4e607SRafael J. Wysocki * acpi_device_set_power - Set power state of an ACPI device. 1489ce4e607SRafael J. Wysocki * @device: Device to set the power state of. 1499ce4e607SRafael J. Wysocki * @state: New power state to set. 1509ce4e607SRafael J. Wysocki * 1519ce4e607SRafael J. Wysocki * Callers must ensure that the device is power manageable before using this 1529ce4e607SRafael J. Wysocki * function. 1539ce4e607SRafael J. Wysocki */ 1549ce4e607SRafael J. Wysocki int acpi_device_set_power(struct acpi_device *device, int state) 1559ce4e607SRafael J. Wysocki { 1569ce4e607SRafael J. Wysocki int result = 0; 1579ce4e607SRafael J. Wysocki bool cut_power = false; 1589ce4e607SRafael J. Wysocki 1592c7d132aSRafael J. Wysocki if (!device || !device->flags.power_manageable 1602c7d132aSRafael J. Wysocki || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD)) 1619ce4e607SRafael J. Wysocki return -EINVAL; 1629ce4e607SRafael J. Wysocki 1639ce4e607SRafael J. Wysocki /* Make sure this is a valid target state */ 1649ce4e607SRafael J. Wysocki 1659ce4e607SRafael J. Wysocki if (state == device->power.state) { 166b69137a7SRafael J. Wysocki ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] already in %s\n", 167b69137a7SRafael J. Wysocki device->pnp.bus_id, 1689ce4e607SRafael J. Wysocki acpi_power_state_string(state))); 1699ce4e607SRafael J. Wysocki return 0; 1709ce4e607SRafael J. Wysocki } 1719ce4e607SRafael J. Wysocki 1729ce4e607SRafael J. Wysocki if (!device->power.states[state].flags.valid) { 173b69137a7SRafael J. Wysocki dev_warn(&device->dev, "Power state %s not supported\n", 1749ce4e607SRafael J. Wysocki acpi_power_state_string(state)); 1759ce4e607SRafael J. Wysocki return -ENODEV; 1769ce4e607SRafael J. Wysocki } 177644f17adSMika Westerberg if (!device->power.flags.ignore_parent && 178644f17adSMika Westerberg device->parent && (state < device->parent->power.state)) { 179b69137a7SRafael J. Wysocki dev_warn(&device->dev, 180593298e6SAaron Lu "Cannot transition to power state %s for parent in %s\n", 181593298e6SAaron Lu acpi_power_state_string(state), 182593298e6SAaron Lu acpi_power_state_string(device->parent->power.state)); 1839ce4e607SRafael J. Wysocki return -ENODEV; 1849ce4e607SRafael J. Wysocki } 1859ce4e607SRafael J. Wysocki 1869ce4e607SRafael J. Wysocki /* For D3cold we should first transition into D3hot. */ 1879ce4e607SRafael J. Wysocki if (state == ACPI_STATE_D3_COLD 1889ce4e607SRafael J. Wysocki && device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible) { 1899ce4e607SRafael J. Wysocki state = ACPI_STATE_D3_HOT; 1909ce4e607SRafael J. Wysocki cut_power = true; 1919ce4e607SRafael J. Wysocki } 1929ce4e607SRafael J. Wysocki 193e78adb75SRafael J. Wysocki if (state < device->power.state && state != ACPI_STATE_D0 194e78adb75SRafael J. Wysocki && device->power.state >= ACPI_STATE_D3_HOT) { 195b69137a7SRafael J. Wysocki dev_warn(&device->dev, 1969ce4e607SRafael J. Wysocki "Cannot transition to non-D0 state from D3\n"); 1979ce4e607SRafael J. Wysocki return -ENODEV; 1989ce4e607SRafael J. Wysocki } 1999c0f45e3SRafael J. Wysocki 200e78adb75SRafael J. Wysocki /* 201e78adb75SRafael J. Wysocki * Transition Power 202e78adb75SRafael J. Wysocki * ---------------- 203e78adb75SRafael J. Wysocki * In accordance with the ACPI specification first apply power (via 204e78adb75SRafael J. Wysocki * power resources) and then evalute _PSx. 205e78adb75SRafael J. Wysocki */ 2069ce4e607SRafael J. Wysocki if (device->power.flags.power_resources) { 2079ce4e607SRafael J. Wysocki result = acpi_power_transition(device, state); 2089ce4e607SRafael J. Wysocki if (result) 2099ce4e607SRafael J. Wysocki goto end; 2109ce4e607SRafael J. Wysocki } 211e78adb75SRafael J. Wysocki result = acpi_dev_pm_explicit_set(device, state); 212e78adb75SRafael J. Wysocki if (result) 213e78adb75SRafael J. Wysocki goto end; 2149ce4e607SRafael J. Wysocki 215e5656271SRafael J. Wysocki if (cut_power) { 216e5656271SRafael J. Wysocki device->power.state = state; 217e5656271SRafael J. Wysocki state = ACPI_STATE_D3_COLD; 218e5656271SRafael J. Wysocki result = acpi_power_transition(device, state); 219e5656271SRafael J. Wysocki } 2209ce4e607SRafael J. Wysocki 2219ce4e607SRafael J. Wysocki end: 222e78adb75SRafael J. Wysocki if (result) { 223b69137a7SRafael J. Wysocki dev_warn(&device->dev, "Failed to change power state to %s\n", 2249ce4e607SRafael J. Wysocki acpi_power_state_string(state)); 225e78adb75SRafael J. Wysocki } else { 2269ce4e607SRafael J. Wysocki device->power.state = state; 2279ce4e607SRafael J. Wysocki ACPI_DEBUG_PRINT((ACPI_DB_INFO, 2289ce4e607SRafael J. Wysocki "Device [%s] transitioned to %s\n", 2299ce4e607SRafael J. Wysocki device->pnp.bus_id, 2309ce4e607SRafael J. Wysocki acpi_power_state_string(state))); 2319ce4e607SRafael J. Wysocki } 2329ce4e607SRafael J. Wysocki 2339ce4e607SRafael J. Wysocki return result; 2349ce4e607SRafael J. Wysocki } 2359ce4e607SRafael J. Wysocki EXPORT_SYMBOL(acpi_device_set_power); 2369ce4e607SRafael J. Wysocki 2379ce4e607SRafael J. Wysocki int acpi_bus_set_power(acpi_handle handle, int state) 2389ce4e607SRafael J. Wysocki { 2399ce4e607SRafael J. Wysocki struct acpi_device *device; 2409ce4e607SRafael J. Wysocki int result; 2419ce4e607SRafael J. Wysocki 2429ce4e607SRafael J. Wysocki result = acpi_bus_get_device(handle, &device); 2439ce4e607SRafael J. Wysocki if (result) 2449ce4e607SRafael J. Wysocki return result; 2459ce4e607SRafael J. Wysocki 2469ce4e607SRafael J. Wysocki return acpi_device_set_power(device, state); 2479ce4e607SRafael J. Wysocki } 2489ce4e607SRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_set_power); 2499ce4e607SRafael J. Wysocki 2509ce4e607SRafael J. Wysocki int acpi_bus_init_power(struct acpi_device *device) 2519ce4e607SRafael J. Wysocki { 2529ce4e607SRafael J. Wysocki int state; 2539ce4e607SRafael J. Wysocki int result; 2549ce4e607SRafael J. Wysocki 2559ce4e607SRafael J. Wysocki if (!device) 2569ce4e607SRafael J. Wysocki return -EINVAL; 2579ce4e607SRafael J. Wysocki 2589ce4e607SRafael J. Wysocki device->power.state = ACPI_STATE_UNKNOWN; 259202317a5SRafael J. Wysocki if (!acpi_device_is_present(device)) 260202317a5SRafael J. Wysocki return 0; 2619ce4e607SRafael J. Wysocki 2629ce4e607SRafael J. Wysocki result = acpi_device_get_power(device, &state); 2639ce4e607SRafael J. Wysocki if (result) 2649ce4e607SRafael J. Wysocki return result; 2659ce4e607SRafael J. Wysocki 266a2367807SRafael J. Wysocki if (state < ACPI_STATE_D3_COLD && device->power.flags.power_resources) { 2679ce4e607SRafael J. Wysocki result = acpi_power_on_resources(device, state); 268a2367807SRafael J. Wysocki if (result) 2699ce4e607SRafael J. Wysocki return result; 270a2367807SRafael J. Wysocki 2719c0f45e3SRafael J. Wysocki result = acpi_dev_pm_explicit_set(device, state); 2729c0f45e3SRafael J. Wysocki if (result) 2739c0f45e3SRafael J. Wysocki return result; 274b3785492SRafael J. Wysocki } else if (state == ACPI_STATE_UNKNOWN) { 2757cd8407dSRafael J. Wysocki /* 2767cd8407dSRafael J. Wysocki * No power resources and missing _PSC? Cross fingers and make 2777cd8407dSRafael J. Wysocki * it D0 in hope that this is what the BIOS put the device into. 2787cd8407dSRafael J. Wysocki * [We tried to force D0 here by executing _PS0, but that broke 2797cd8407dSRafael J. Wysocki * Toshiba P870-303 in a nasty way.] 2807cd8407dSRafael J. Wysocki */ 281b3785492SRafael J. Wysocki state = ACPI_STATE_D0; 282a2367807SRafael J. Wysocki } 283a2367807SRafael J. Wysocki device->power.state = state; 284a2367807SRafael J. Wysocki return 0; 2859ce4e607SRafael J. Wysocki } 2869ce4e607SRafael J. Wysocki 287b9e95fc6SRafael J. Wysocki /** 288b9e95fc6SRafael J. Wysocki * acpi_device_fix_up_power - Force device with missing _PSC into D0. 289b9e95fc6SRafael J. Wysocki * @device: Device object whose power state is to be fixed up. 290b9e95fc6SRafael J. Wysocki * 291b9e95fc6SRafael J. Wysocki * Devices without power resources and _PSC, but having _PS0 and _PS3 defined, 292b9e95fc6SRafael J. Wysocki * are assumed to be put into D0 by the BIOS. However, in some cases that may 293b9e95fc6SRafael J. Wysocki * not be the case and this function should be used then. 294b9e95fc6SRafael J. Wysocki */ 295b9e95fc6SRafael J. Wysocki int acpi_device_fix_up_power(struct acpi_device *device) 296b9e95fc6SRafael J. Wysocki { 297b9e95fc6SRafael J. Wysocki int ret = 0; 298b9e95fc6SRafael J. Wysocki 299b9e95fc6SRafael J. Wysocki if (!device->power.flags.power_resources 300b9e95fc6SRafael J. Wysocki && !device->power.flags.explicit_get 301b9e95fc6SRafael J. Wysocki && device->power.state == ACPI_STATE_D0) 302b9e95fc6SRafael J. Wysocki ret = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0); 303b9e95fc6SRafael J. Wysocki 304b9e95fc6SRafael J. Wysocki return ret; 305b9e95fc6SRafael J. Wysocki } 306b9e95fc6SRafael J. Wysocki 307202317a5SRafael J. Wysocki int acpi_device_update_power(struct acpi_device *device, int *state_p) 3089ce4e607SRafael J. Wysocki { 3099ce4e607SRafael J. Wysocki int state; 3109ce4e607SRafael J. Wysocki int result; 3119ce4e607SRafael J. Wysocki 312202317a5SRafael J. Wysocki if (device->power.state == ACPI_STATE_UNKNOWN) { 313202317a5SRafael J. Wysocki result = acpi_bus_init_power(device); 314202317a5SRafael J. Wysocki if (!result && state_p) 315202317a5SRafael J. Wysocki *state_p = device->power.state; 316202317a5SRafael J. Wysocki 3179ce4e607SRafael J. Wysocki return result; 318202317a5SRafael J. Wysocki } 3199ce4e607SRafael J. Wysocki 3209ce4e607SRafael J. Wysocki result = acpi_device_get_power(device, &state); 3219ce4e607SRafael J. Wysocki if (result) 3229ce4e607SRafael J. Wysocki return result; 3239ce4e607SRafael J. Wysocki 32491bdad0bSRafael J. Wysocki if (state == ACPI_STATE_UNKNOWN) { 325511d5c42SRafael J. Wysocki state = ACPI_STATE_D0; 3269ce4e607SRafael J. Wysocki result = acpi_device_set_power(device, state); 32791bdad0bSRafael J. Wysocki if (result) 32891bdad0bSRafael J. Wysocki return result; 32991bdad0bSRafael J. Wysocki } else { 33091bdad0bSRafael J. Wysocki if (device->power.flags.power_resources) { 33191bdad0bSRafael J. Wysocki /* 33291bdad0bSRafael J. Wysocki * We don't need to really switch the state, bu we need 33391bdad0bSRafael J. Wysocki * to update the power resources' reference counters. 33491bdad0bSRafael J. Wysocki */ 33591bdad0bSRafael J. Wysocki result = acpi_power_transition(device, state); 33691bdad0bSRafael J. Wysocki if (result) 33791bdad0bSRafael J. Wysocki return result; 33891bdad0bSRafael J. Wysocki } 33991bdad0bSRafael J. Wysocki device->power.state = state; 34091bdad0bSRafael J. Wysocki } 34191bdad0bSRafael J. Wysocki if (state_p) 3429ce4e607SRafael J. Wysocki *state_p = state; 3439ce4e607SRafael J. Wysocki 34491bdad0bSRafael J. Wysocki return 0; 3459ce4e607SRafael J. Wysocki } 346202317a5SRafael J. Wysocki 347202317a5SRafael J. Wysocki int acpi_bus_update_power(acpi_handle handle, int *state_p) 348202317a5SRafael J. Wysocki { 349202317a5SRafael J. Wysocki struct acpi_device *device; 350202317a5SRafael J. Wysocki int result; 351202317a5SRafael J. Wysocki 352202317a5SRafael J. Wysocki result = acpi_bus_get_device(handle, &device); 353202317a5SRafael J. Wysocki return result ? result : acpi_device_update_power(device, state_p); 354202317a5SRafael J. Wysocki } 3559ce4e607SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_bus_update_power); 3569ce4e607SRafael J. Wysocki 3579ce4e607SRafael J. Wysocki bool acpi_bus_power_manageable(acpi_handle handle) 3589ce4e607SRafael J. Wysocki { 3599ce4e607SRafael J. Wysocki struct acpi_device *device; 3609ce4e607SRafael J. Wysocki int result; 3619ce4e607SRafael J. Wysocki 3629ce4e607SRafael J. Wysocki result = acpi_bus_get_device(handle, &device); 3639ce4e607SRafael J. Wysocki return result ? false : device->flags.power_manageable; 3649ce4e607SRafael J. Wysocki } 3659ce4e607SRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_power_manageable); 3669ce4e607SRafael J. Wysocki 367ec4602a9SRafael J. Wysocki #ifdef CONFIG_PM 368ec4602a9SRafael J. Wysocki static DEFINE_MUTEX(acpi_pm_notifier_lock); 369ec4602a9SRafael J. Wysocki 370ec4602a9SRafael J. Wysocki /** 371ec4602a9SRafael J. Wysocki * acpi_add_pm_notifier - Register PM notifier for given ACPI device. 372ec4602a9SRafael J. Wysocki * @adev: ACPI device to add the notifier for. 373ec4602a9SRafael J. Wysocki * @context: Context information to pass to the notifier routine. 374ec4602a9SRafael J. Wysocki * 375ec4602a9SRafael J. Wysocki * NOTE: @adev need not be a run-wake or wakeup device to be a valid source of 376ec4602a9SRafael J. Wysocki * PM wakeup events. For example, wakeup events may be generated for bridges 377ec4602a9SRafael J. Wysocki * if one of the devices below the bridge is signaling wakeup, even if the 378ec4602a9SRafael J. Wysocki * bridge itself doesn't have a wakeup GPE associated with it. 379ec4602a9SRafael J. Wysocki */ 380ec4602a9SRafael J. Wysocki acpi_status acpi_add_pm_notifier(struct acpi_device *adev, 381ec4602a9SRafael J. Wysocki acpi_notify_handler handler, void *context) 382ec4602a9SRafael J. Wysocki { 383ec4602a9SRafael J. Wysocki acpi_status status = AE_ALREADY_EXISTS; 384ec4602a9SRafael J. Wysocki 385ec4602a9SRafael J. Wysocki mutex_lock(&acpi_pm_notifier_lock); 386ec4602a9SRafael J. Wysocki 387ec4602a9SRafael J. Wysocki if (adev->wakeup.flags.notifier_present) 388ec4602a9SRafael J. Wysocki goto out; 389ec4602a9SRafael J. Wysocki 390ec4602a9SRafael J. Wysocki status = acpi_install_notify_handler(adev->handle, 391ec4602a9SRafael J. Wysocki ACPI_SYSTEM_NOTIFY, 392ec4602a9SRafael J. Wysocki handler, context); 393ec4602a9SRafael J. Wysocki if (ACPI_FAILURE(status)) 394ec4602a9SRafael J. Wysocki goto out; 395ec4602a9SRafael J. Wysocki 396ec4602a9SRafael J. Wysocki adev->wakeup.flags.notifier_present = true; 397ec4602a9SRafael J. Wysocki 398ec4602a9SRafael J. Wysocki out: 399ec4602a9SRafael J. Wysocki mutex_unlock(&acpi_pm_notifier_lock); 400ec4602a9SRafael J. Wysocki return status; 401ec4602a9SRafael J. Wysocki } 402ec4602a9SRafael J. Wysocki 403ec4602a9SRafael J. Wysocki /** 404ec4602a9SRafael J. Wysocki * acpi_remove_pm_notifier - Unregister PM notifier from given ACPI device. 405ec4602a9SRafael J. Wysocki * @adev: ACPI device to remove the notifier from. 406ec4602a9SRafael J. Wysocki */ 407ec4602a9SRafael J. Wysocki acpi_status acpi_remove_pm_notifier(struct acpi_device *adev, 408ec4602a9SRafael J. Wysocki acpi_notify_handler handler) 409ec4602a9SRafael J. Wysocki { 410ec4602a9SRafael J. Wysocki acpi_status status = AE_BAD_PARAMETER; 411ec4602a9SRafael J. Wysocki 412ec4602a9SRafael J. Wysocki mutex_lock(&acpi_pm_notifier_lock); 413ec4602a9SRafael J. Wysocki 414ec4602a9SRafael J. Wysocki if (!adev->wakeup.flags.notifier_present) 415ec4602a9SRafael J. Wysocki goto out; 416ec4602a9SRafael J. Wysocki 417ec4602a9SRafael J. Wysocki status = acpi_remove_notify_handler(adev->handle, 418ec4602a9SRafael J. Wysocki ACPI_SYSTEM_NOTIFY, 419ec4602a9SRafael J. Wysocki handler); 420ec4602a9SRafael J. Wysocki if (ACPI_FAILURE(status)) 421ec4602a9SRafael J. Wysocki goto out; 422ec4602a9SRafael J. Wysocki 423ec4602a9SRafael J. Wysocki adev->wakeup.flags.notifier_present = false; 424ec4602a9SRafael J. Wysocki 425ec4602a9SRafael J. Wysocki out: 426ec4602a9SRafael J. Wysocki mutex_unlock(&acpi_pm_notifier_lock); 427ec4602a9SRafael J. Wysocki return status; 428ec4602a9SRafael J. Wysocki } 429ec4602a9SRafael J. Wysocki 4309ce4e607SRafael J. Wysocki bool acpi_bus_can_wakeup(acpi_handle handle) 4319ce4e607SRafael J. Wysocki { 4329ce4e607SRafael J. Wysocki struct acpi_device *device; 4339ce4e607SRafael J. Wysocki int result; 4349ce4e607SRafael J. Wysocki 4359ce4e607SRafael J. Wysocki result = acpi_bus_get_device(handle, &device); 4369ce4e607SRafael J. Wysocki return result ? false : device->wakeup.flags.valid; 4379ce4e607SRafael J. Wysocki } 4389ce4e607SRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_can_wakeup); 4399ce4e607SRafael J. Wysocki 4409ce4e607SRafael J. Wysocki /** 441b25c77efSRafael J. Wysocki * acpi_dev_pm_get_state - Get preferred power state of ACPI device. 44286b3832cSRafael J. Wysocki * @dev: Device whose preferred target power state to return. 44386b3832cSRafael J. Wysocki * @adev: ACPI device node corresponding to @dev. 44486b3832cSRafael J. Wysocki * @target_state: System state to match the resultant device state. 445fa1675b5SRafael J. Wysocki * @d_min_p: Location to store the highest power state available to the device. 446fa1675b5SRafael J. Wysocki * @d_max_p: Location to store the lowest power state available to the device. 44786b3832cSRafael J. Wysocki * 448fa1675b5SRafael J. Wysocki * Find the lowest power (highest number) and highest power (lowest number) ACPI 449fa1675b5SRafael J. Wysocki * device power states that the device can be in while the system is in the 450fa1675b5SRafael J. Wysocki * state represented by @target_state. Store the integer numbers representing 451fa1675b5SRafael J. Wysocki * those stats in the memory locations pointed to by @d_max_p and @d_min_p, 452fa1675b5SRafael J. Wysocki * respectively. 45386b3832cSRafael J. Wysocki * 45486b3832cSRafael J. Wysocki * Callers must ensure that @dev and @adev are valid pointers and that @adev 45586b3832cSRafael J. Wysocki * actually corresponds to @dev before using this function. 456fa1675b5SRafael J. Wysocki * 457fa1675b5SRafael J. Wysocki * Returns 0 on success or -ENODATA when one of the ACPI methods fails or 458fa1675b5SRafael J. Wysocki * returns a value that doesn't make sense. The memory locations pointed to by 459fa1675b5SRafael J. Wysocki * @d_max_p and @d_min_p are only modified on success. 46086b3832cSRafael J. Wysocki */ 461b25c77efSRafael J. Wysocki static int acpi_dev_pm_get_state(struct device *dev, struct acpi_device *adev, 462fa1675b5SRafael J. Wysocki u32 target_state, int *d_min_p, int *d_max_p) 46386b3832cSRafael J. Wysocki { 464fa1675b5SRafael J. Wysocki char method[] = { '_', 'S', '0' + target_state, 'D', '\0' }; 465fa1675b5SRafael J. Wysocki acpi_handle handle = adev->handle; 466fa1675b5SRafael J. Wysocki unsigned long long ret; 467fa1675b5SRafael J. Wysocki int d_min, d_max; 46886b3832cSRafael J. Wysocki bool wakeup = false; 469fa1675b5SRafael J. Wysocki acpi_status status; 47086b3832cSRafael J. Wysocki 47186b3832cSRafael J. Wysocki /* 472fa1675b5SRafael J. Wysocki * If the system state is S0, the lowest power state the device can be 473fa1675b5SRafael J. Wysocki * in is D3cold, unless the device has _S0W and is supposed to signal 474fa1675b5SRafael J. Wysocki * wakeup, in which case the return value of _S0W has to be used as the 475fa1675b5SRafael J. Wysocki * lowest power state available to the device. 47686b3832cSRafael J. Wysocki */ 47786b3832cSRafael J. Wysocki d_min = ACPI_STATE_D0; 4784c164ae7SRafael J. Wysocki d_max = ACPI_STATE_D3_COLD; 47986b3832cSRafael J. Wysocki 48086b3832cSRafael J. Wysocki /* 48186b3832cSRafael J. Wysocki * If present, _SxD methods return the minimum D-state (highest power 48286b3832cSRafael J. Wysocki * state) we can use for the corresponding S-states. Otherwise, the 48386b3832cSRafael J. Wysocki * minimum D-state is D0 (ACPI 3.x). 48486b3832cSRafael J. Wysocki */ 48586b3832cSRafael J. Wysocki if (target_state > ACPI_STATE_S0) { 486fa1675b5SRafael J. Wysocki /* 487fa1675b5SRafael J. Wysocki * We rely on acpi_evaluate_integer() not clobbering the integer 488fa1675b5SRafael J. Wysocki * provided if AE_NOT_FOUND is returned. 489fa1675b5SRafael J. Wysocki */ 490fa1675b5SRafael J. Wysocki ret = d_min; 491fa1675b5SRafael J. Wysocki status = acpi_evaluate_integer(handle, method, NULL, &ret); 492fa1675b5SRafael J. Wysocki if ((ACPI_FAILURE(status) && status != AE_NOT_FOUND) 493fa1675b5SRafael J. Wysocki || ret > ACPI_STATE_D3_COLD) 494fa1675b5SRafael J. Wysocki return -ENODATA; 495fa1675b5SRafael J. Wysocki 496fa1675b5SRafael J. Wysocki /* 497fa1675b5SRafael J. Wysocki * We need to handle legacy systems where D3hot and D3cold are 498fa1675b5SRafael J. Wysocki * the same and 3 is returned in both cases, so fall back to 499fa1675b5SRafael J. Wysocki * D3cold if D3hot is not a valid state. 500fa1675b5SRafael J. Wysocki */ 501fa1675b5SRafael J. Wysocki if (!adev->power.states[ret].flags.valid) { 502fa1675b5SRafael J. Wysocki if (ret == ACPI_STATE_D3_HOT) 503fa1675b5SRafael J. Wysocki ret = ACPI_STATE_D3_COLD; 504fa1675b5SRafael J. Wysocki else 505fa1675b5SRafael J. Wysocki return -ENODATA; 506fa1675b5SRafael J. Wysocki } 507fa1675b5SRafael J. Wysocki d_min = ret; 50886b3832cSRafael J. Wysocki wakeup = device_may_wakeup(dev) && adev->wakeup.flags.valid 50986b3832cSRafael J. Wysocki && adev->wakeup.sleep_state >= target_state; 51086b3832cSRafael J. Wysocki } else if (dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) != 51186b3832cSRafael J. Wysocki PM_QOS_FLAGS_NONE) { 51286b3832cSRafael J. Wysocki wakeup = adev->wakeup.flags.valid; 51386b3832cSRafael J. Wysocki } 51486b3832cSRafael J. Wysocki 51586b3832cSRafael J. Wysocki /* 51686b3832cSRafael J. Wysocki * If _PRW says we can wake up the system from the target sleep state, 51786b3832cSRafael J. Wysocki * the D-state returned by _SxD is sufficient for that (we assume a 51886b3832cSRafael J. Wysocki * wakeup-aware driver if wake is set). Still, if _SxW exists 51986b3832cSRafael J. Wysocki * (ACPI 3.x), it should return the maximum (lowest power) D-state that 52086b3832cSRafael J. Wysocki * can wake the system. _S0W may be valid, too. 52186b3832cSRafael J. Wysocki */ 52286b3832cSRafael J. Wysocki if (wakeup) { 523fa1675b5SRafael J. Wysocki method[3] = 'W'; 524fa1675b5SRafael J. Wysocki status = acpi_evaluate_integer(handle, method, NULL, &ret); 525fa1675b5SRafael J. Wysocki if (status == AE_NOT_FOUND) { 526fa1675b5SRafael J. Wysocki if (target_state > ACPI_STATE_S0) 52786b3832cSRafael J. Wysocki d_max = d_min; 528fa1675b5SRafael J. Wysocki } else if (ACPI_SUCCESS(status) && ret <= ACPI_STATE_D3_COLD) { 529fa1675b5SRafael J. Wysocki /* Fall back to D3cold if ret is not a valid state. */ 530fa1675b5SRafael J. Wysocki if (!adev->power.states[ret].flags.valid) 531fa1675b5SRafael J. Wysocki ret = ACPI_STATE_D3_COLD; 532fa1675b5SRafael J. Wysocki 533fa1675b5SRafael J. Wysocki d_max = ret > d_min ? ret : d_min; 534fa1675b5SRafael J. Wysocki } else { 535fa1675b5SRafael J. Wysocki return -ENODATA; 53686b3832cSRafael J. Wysocki } 53786b3832cSRafael J. Wysocki } 53886b3832cSRafael J. Wysocki 53986b3832cSRafael J. Wysocki if (d_min_p) 54086b3832cSRafael J. Wysocki *d_min_p = d_min; 541fa1675b5SRafael J. Wysocki 542fa1675b5SRafael J. Wysocki if (d_max_p) 543fa1675b5SRafael J. Wysocki *d_max_p = d_max; 544fa1675b5SRafael J. Wysocki 545fa1675b5SRafael J. Wysocki return 0; 54686b3832cSRafael J. Wysocki } 547cd7bd02dSRafael J. Wysocki 548a6ae7594SRafael J. Wysocki /** 549a6ae7594SRafael J. Wysocki * acpi_pm_device_sleep_state - Get preferred power state of ACPI device. 550a6ae7594SRafael J. Wysocki * @dev: Device whose preferred target power state to return. 551a6ae7594SRafael J. Wysocki * @d_min_p: Location to store the upper limit of the allowed states range. 552a6ae7594SRafael J. Wysocki * @d_max_in: Deepest low-power state to take into consideration. 553a6ae7594SRafael J. Wysocki * Return value: Preferred power state of the device on success, -ENODEV 554fa1675b5SRafael J. Wysocki * if there's no 'struct acpi_device' for @dev, -EINVAL if @d_max_in is 555fa1675b5SRafael J. Wysocki * incorrect, or -ENODATA on ACPI method failure. 556a6ae7594SRafael J. Wysocki * 557a6ae7594SRafael J. Wysocki * The caller must ensure that @dev is valid before using this function. 558a6ae7594SRafael J. Wysocki */ 559a6ae7594SRafael J. Wysocki int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in) 560a6ae7594SRafael J. Wysocki { 5613a83f992SRafael J. Wysocki acpi_handle handle = ACPI_HANDLE(dev); 562a6ae7594SRafael J. Wysocki struct acpi_device *adev; 5639b5c7a5aSRafael J. Wysocki int ret, d_min, d_max; 564fa1675b5SRafael J. Wysocki 565fa1675b5SRafael J. Wysocki if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3_COLD) 566fa1675b5SRafael J. Wysocki return -EINVAL; 567fa1675b5SRafael J. Wysocki 568fa1675b5SRafael J. Wysocki if (d_max_in > ACPI_STATE_D3_HOT) { 569fa1675b5SRafael J. Wysocki enum pm_qos_flags_status stat; 570fa1675b5SRafael J. Wysocki 571fa1675b5SRafael J. Wysocki stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF); 572fa1675b5SRafael J. Wysocki if (stat == PM_QOS_FLAGS_ALL) 573fa1675b5SRafael J. Wysocki d_max_in = ACPI_STATE_D3_HOT; 574fa1675b5SRafael J. Wysocki } 575a6ae7594SRafael J. Wysocki 576dde3bb41SYasuaki Ishimatsu if (!handle || acpi_bus_get_device(handle, &adev)) { 577a6ae7594SRafael J. Wysocki dev_dbg(dev, "ACPI handle without context in %s!\n", __func__); 578a6ae7594SRafael J. Wysocki return -ENODEV; 579a6ae7594SRafael J. Wysocki } 580a6ae7594SRafael J. Wysocki 581fa1675b5SRafael J. Wysocki ret = acpi_dev_pm_get_state(dev, adev, acpi_target_system_state(), 5829b5c7a5aSRafael J. Wysocki &d_min, &d_max); 583fa1675b5SRafael J. Wysocki if (ret) 584fa1675b5SRafael J. Wysocki return ret; 585fa1675b5SRafael J. Wysocki 5869b5c7a5aSRafael J. Wysocki if (d_max_in < d_min) 587fa1675b5SRafael J. Wysocki return -EINVAL; 588fa1675b5SRafael J. Wysocki 589fa1675b5SRafael J. Wysocki if (d_max > d_max_in) { 5909b5c7a5aSRafael J. Wysocki for (d_max = d_max_in; d_max > d_min; d_max--) { 591fa1675b5SRafael J. Wysocki if (adev->power.states[d_max].flags.valid) 592fa1675b5SRafael J. Wysocki break; 593fa1675b5SRafael J. Wysocki } 594fa1675b5SRafael J. Wysocki } 5959b5c7a5aSRafael J. Wysocki 5969b5c7a5aSRafael J. Wysocki if (d_min_p) 5979b5c7a5aSRafael J. Wysocki *d_min_p = d_min; 5989b5c7a5aSRafael J. Wysocki 599fa1675b5SRafael J. Wysocki return d_max; 600a6ae7594SRafael J. Wysocki } 601a6ae7594SRafael J. Wysocki EXPORT_SYMBOL(acpi_pm_device_sleep_state); 602a6ae7594SRafael J. Wysocki 603cd7bd02dSRafael J. Wysocki #ifdef CONFIG_PM_RUNTIME 604cd7bd02dSRafael J. Wysocki /** 605e5cc8ef3SRafael J. Wysocki * acpi_wakeup_device - Wakeup notification handler for ACPI devices. 606e5cc8ef3SRafael J. Wysocki * @handle: ACPI handle of the device the notification is for. 607e5cc8ef3SRafael J. Wysocki * @event: Type of the signaled event. 608e5cc8ef3SRafael J. Wysocki * @context: Device corresponding to @handle. 609e5cc8ef3SRafael J. Wysocki */ 610e5cc8ef3SRafael J. Wysocki static void acpi_wakeup_device(acpi_handle handle, u32 event, void *context) 611e5cc8ef3SRafael J. Wysocki { 612e5cc8ef3SRafael J. Wysocki struct device *dev = context; 613e5cc8ef3SRafael J. Wysocki 614e5cc8ef3SRafael J. Wysocki if (event == ACPI_NOTIFY_DEVICE_WAKE && dev) { 615e5cc8ef3SRafael J. Wysocki pm_wakeup_event(dev, 0); 616e5cc8ef3SRafael J. Wysocki pm_runtime_resume(dev); 617e5cc8ef3SRafael J. Wysocki } 618e5cc8ef3SRafael J. Wysocki } 619e5cc8ef3SRafael J. Wysocki 620e5cc8ef3SRafael J. Wysocki /** 621dee8370cSRafael J. Wysocki * __acpi_device_run_wake - Enable/disable runtime remote wakeup for device. 622dee8370cSRafael J. Wysocki * @adev: ACPI device to enable/disable the remote wakeup for. 623cd7bd02dSRafael J. Wysocki * @enable: Whether to enable or disable the wakeup functionality. 624cd7bd02dSRafael J. Wysocki * 625dee8370cSRafael J. Wysocki * Enable/disable the GPE associated with @adev so that it can generate 626dee8370cSRafael J. Wysocki * wakeup signals for the device in response to external (remote) events and 627dee8370cSRafael J. Wysocki * enable/disable device wakeup power. 628dee8370cSRafael J. Wysocki * 629dee8370cSRafael J. Wysocki * Callers must ensure that @adev is a valid ACPI device node before executing 630dee8370cSRafael J. Wysocki * this function. 631dee8370cSRafael J. Wysocki */ 632dee8370cSRafael J. Wysocki int __acpi_device_run_wake(struct acpi_device *adev, bool enable) 633dee8370cSRafael J. Wysocki { 634dee8370cSRafael J. Wysocki struct acpi_device_wakeup *wakeup = &adev->wakeup; 635dee8370cSRafael J. Wysocki 636dee8370cSRafael J. Wysocki if (enable) { 637dee8370cSRafael J. Wysocki acpi_status res; 638dee8370cSRafael J. Wysocki int error; 639dee8370cSRafael J. Wysocki 640dee8370cSRafael J. Wysocki error = acpi_enable_wakeup_device_power(adev, ACPI_STATE_S0); 641dee8370cSRafael J. Wysocki if (error) 642dee8370cSRafael J. Wysocki return error; 643dee8370cSRafael J. Wysocki 644dee8370cSRafael J. Wysocki res = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number); 645dee8370cSRafael J. Wysocki if (ACPI_FAILURE(res)) { 646dee8370cSRafael J. Wysocki acpi_disable_wakeup_device_power(adev); 647dee8370cSRafael J. Wysocki return -EIO; 648dee8370cSRafael J. Wysocki } 649dee8370cSRafael J. Wysocki } else { 650dee8370cSRafael J. Wysocki acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number); 651dee8370cSRafael J. Wysocki acpi_disable_wakeup_device_power(adev); 652dee8370cSRafael J. Wysocki } 653dee8370cSRafael J. Wysocki return 0; 654dee8370cSRafael J. Wysocki } 655dee8370cSRafael J. Wysocki 656dee8370cSRafael J. Wysocki /** 657dee8370cSRafael J. Wysocki * acpi_pm_device_run_wake - Enable/disable remote wakeup for given device. 658dee8370cSRafael J. Wysocki * @dev: Device to enable/disable the platform to wake up. 659dee8370cSRafael J. Wysocki * @enable: Whether to enable or disable the wakeup functionality. 660cd7bd02dSRafael J. Wysocki */ 661cd7bd02dSRafael J. Wysocki int acpi_pm_device_run_wake(struct device *phys_dev, bool enable) 662cd7bd02dSRafael J. Wysocki { 663dee8370cSRafael J. Wysocki struct acpi_device *adev; 664cd7bd02dSRafael J. Wysocki acpi_handle handle; 665cd7bd02dSRafael J. Wysocki 666cd7bd02dSRafael J. Wysocki if (!device_run_wake(phys_dev)) 667cd7bd02dSRafael J. Wysocki return -EINVAL; 668cd7bd02dSRafael J. Wysocki 6693a83f992SRafael J. Wysocki handle = ACPI_HANDLE(phys_dev); 670dde3bb41SYasuaki Ishimatsu if (!handle || acpi_bus_get_device(handle, &adev)) { 671dee8370cSRafael J. Wysocki dev_dbg(phys_dev, "ACPI handle without context in %s!\n", 672cd7bd02dSRafael J. Wysocki __func__); 673cd7bd02dSRafael J. Wysocki return -ENODEV; 674cd7bd02dSRafael J. Wysocki } 675cd7bd02dSRafael J. Wysocki 676dee8370cSRafael J. Wysocki return __acpi_device_run_wake(adev, enable); 677cd7bd02dSRafael J. Wysocki } 678cd7bd02dSRafael J. Wysocki EXPORT_SYMBOL(acpi_pm_device_run_wake); 679e5cc8ef3SRafael J. Wysocki #else 680e5cc8ef3SRafael J. Wysocki static inline void acpi_wakeup_device(acpi_handle handle, u32 event, 681e5cc8ef3SRafael J. Wysocki void *context) {} 682cd7bd02dSRafael J. Wysocki #endif /* CONFIG_PM_RUNTIME */ 683dee8370cSRafael J. Wysocki 684dee8370cSRafael J. Wysocki #ifdef CONFIG_PM_SLEEP 685dee8370cSRafael J. Wysocki /** 686dee8370cSRafael J. Wysocki * __acpi_device_sleep_wake - Enable or disable device to wake up the system. 687dee8370cSRafael J. Wysocki * @dev: Device to enable/desible to wake up the system. 688dee8370cSRafael J. Wysocki * @target_state: System state the device is supposed to wake up from. 689dee8370cSRafael J. Wysocki * @enable: Whether to enable or disable @dev to wake up the system. 690dee8370cSRafael J. Wysocki */ 691dee8370cSRafael J. Wysocki int __acpi_device_sleep_wake(struct acpi_device *adev, u32 target_state, 692dee8370cSRafael J. Wysocki bool enable) 693dee8370cSRafael J. Wysocki { 694dee8370cSRafael J. Wysocki return enable ? 695dee8370cSRafael J. Wysocki acpi_enable_wakeup_device_power(adev, target_state) : 696dee8370cSRafael J. Wysocki acpi_disable_wakeup_device_power(adev); 697dee8370cSRafael J. Wysocki } 698a6ae7594SRafael J. Wysocki 699a6ae7594SRafael J. Wysocki /** 700a6ae7594SRafael J. Wysocki * acpi_pm_device_sleep_wake - Enable or disable device to wake up the system. 701a6ae7594SRafael J. Wysocki * @dev: Device to enable/desible to wake up the system from sleep states. 702a6ae7594SRafael J. Wysocki * @enable: Whether to enable or disable @dev to wake up the system. 703a6ae7594SRafael J. Wysocki */ 704a6ae7594SRafael J. Wysocki int acpi_pm_device_sleep_wake(struct device *dev, bool enable) 705a6ae7594SRafael J. Wysocki { 706a6ae7594SRafael J. Wysocki acpi_handle handle; 707a6ae7594SRafael J. Wysocki struct acpi_device *adev; 708a6ae7594SRafael J. Wysocki int error; 709a6ae7594SRafael J. Wysocki 710a6ae7594SRafael J. Wysocki if (!device_can_wakeup(dev)) 711a6ae7594SRafael J. Wysocki return -EINVAL; 712a6ae7594SRafael J. Wysocki 7133a83f992SRafael J. Wysocki handle = ACPI_HANDLE(dev); 714dde3bb41SYasuaki Ishimatsu if (!handle || acpi_bus_get_device(handle, &adev)) { 715a6ae7594SRafael J. Wysocki dev_dbg(dev, "ACPI handle without context in %s!\n", __func__); 716a6ae7594SRafael J. Wysocki return -ENODEV; 717a6ae7594SRafael J. Wysocki } 718a6ae7594SRafael J. Wysocki 719a6ae7594SRafael J. Wysocki error = __acpi_device_sleep_wake(adev, acpi_target_system_state(), 720a6ae7594SRafael J. Wysocki enable); 721a6ae7594SRafael J. Wysocki if (!error) 722a6ae7594SRafael J. Wysocki dev_info(dev, "System wakeup %s by ACPI\n", 723a6ae7594SRafael J. Wysocki enable ? "enabled" : "disabled"); 724a6ae7594SRafael J. Wysocki 725a6ae7594SRafael J. Wysocki return error; 726a6ae7594SRafael J. Wysocki } 727dee8370cSRafael J. Wysocki #endif /* CONFIG_PM_SLEEP */ 728e5cc8ef3SRafael J. Wysocki 729e5cc8ef3SRafael J. Wysocki /** 730e5cc8ef3SRafael J. Wysocki * acpi_dev_pm_low_power - Put ACPI device into a low-power state. 731e5cc8ef3SRafael J. Wysocki * @dev: Device to put into a low-power state. 732e5cc8ef3SRafael J. Wysocki * @adev: ACPI device node corresponding to @dev. 733e5cc8ef3SRafael J. Wysocki * @system_state: System state to choose the device state for. 734e5cc8ef3SRafael J. Wysocki */ 735e5cc8ef3SRafael J. Wysocki static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev, 736e5cc8ef3SRafael J. Wysocki u32 system_state) 737e5cc8ef3SRafael J. Wysocki { 738fa1675b5SRafael J. Wysocki int ret, state; 739e5cc8ef3SRafael J. Wysocki 740e5cc8ef3SRafael J. Wysocki if (!acpi_device_power_manageable(adev)) 741e5cc8ef3SRafael J. Wysocki return 0; 742e5cc8ef3SRafael J. Wysocki 743fa1675b5SRafael J. Wysocki ret = acpi_dev_pm_get_state(dev, adev, system_state, NULL, &state); 744fa1675b5SRafael J. Wysocki return ret ? ret : acpi_device_set_power(adev, state); 745e5cc8ef3SRafael J. Wysocki } 746e5cc8ef3SRafael J. Wysocki 747e5cc8ef3SRafael J. Wysocki /** 748e5cc8ef3SRafael J. Wysocki * acpi_dev_pm_full_power - Put ACPI device into the full-power state. 749e5cc8ef3SRafael J. Wysocki * @adev: ACPI device node to put into the full-power state. 750e5cc8ef3SRafael J. Wysocki */ 751e5cc8ef3SRafael J. Wysocki static int acpi_dev_pm_full_power(struct acpi_device *adev) 752e5cc8ef3SRafael J. Wysocki { 753e5cc8ef3SRafael J. Wysocki return acpi_device_power_manageable(adev) ? 754e5cc8ef3SRafael J. Wysocki acpi_device_set_power(adev, ACPI_STATE_D0) : 0; 755e5cc8ef3SRafael J. Wysocki } 756e5cc8ef3SRafael J. Wysocki 757e5cc8ef3SRafael J. Wysocki #ifdef CONFIG_PM_RUNTIME 758e5cc8ef3SRafael J. Wysocki /** 759e5cc8ef3SRafael J. Wysocki * acpi_dev_runtime_suspend - Put device into a low-power state using ACPI. 760e5cc8ef3SRafael J. Wysocki * @dev: Device to put into a low-power state. 761e5cc8ef3SRafael J. Wysocki * 762e5cc8ef3SRafael J. Wysocki * Put the given device into a runtime low-power state using the standard ACPI 763e5cc8ef3SRafael J. Wysocki * mechanism. Set up remote wakeup if desired, choose the state to put the 764e5cc8ef3SRafael J. Wysocki * device into (this checks if remote wakeup is expected to work too), and set 765e5cc8ef3SRafael J. Wysocki * the power state of the device. 766e5cc8ef3SRafael J. Wysocki */ 767e5cc8ef3SRafael J. Wysocki int acpi_dev_runtime_suspend(struct device *dev) 768e5cc8ef3SRafael J. Wysocki { 76979c0373fSRafael J. Wysocki struct acpi_device *adev = ACPI_COMPANION(dev); 770e5cc8ef3SRafael J. Wysocki bool remote_wakeup; 771e5cc8ef3SRafael J. Wysocki int error; 772e5cc8ef3SRafael J. Wysocki 773e5cc8ef3SRafael J. Wysocki if (!adev) 774e5cc8ef3SRafael J. Wysocki return 0; 775e5cc8ef3SRafael J. Wysocki 776e5cc8ef3SRafael J. Wysocki remote_wakeup = dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) > 777e5cc8ef3SRafael J. Wysocki PM_QOS_FLAGS_NONE; 778e5cc8ef3SRafael J. Wysocki error = __acpi_device_run_wake(adev, remote_wakeup); 779e5cc8ef3SRafael J. Wysocki if (remote_wakeup && error) 780e5cc8ef3SRafael J. Wysocki return -EAGAIN; 781e5cc8ef3SRafael J. Wysocki 782e5cc8ef3SRafael J. Wysocki error = acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0); 783e5cc8ef3SRafael J. Wysocki if (error) 784e5cc8ef3SRafael J. Wysocki __acpi_device_run_wake(adev, false); 785e5cc8ef3SRafael J. Wysocki 786e5cc8ef3SRafael J. Wysocki return error; 787e5cc8ef3SRafael J. Wysocki } 788e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_runtime_suspend); 789e5cc8ef3SRafael J. Wysocki 790e5cc8ef3SRafael J. Wysocki /** 791e5cc8ef3SRafael J. Wysocki * acpi_dev_runtime_resume - Put device into the full-power state using ACPI. 792e5cc8ef3SRafael J. Wysocki * @dev: Device to put into the full-power state. 793e5cc8ef3SRafael J. Wysocki * 794e5cc8ef3SRafael J. Wysocki * Put the given device into the full-power state using the standard ACPI 795e5cc8ef3SRafael J. Wysocki * mechanism at run time. Set the power state of the device to ACPI D0 and 796e5cc8ef3SRafael J. Wysocki * disable remote wakeup. 797e5cc8ef3SRafael J. Wysocki */ 798e5cc8ef3SRafael J. Wysocki int acpi_dev_runtime_resume(struct device *dev) 799e5cc8ef3SRafael J. Wysocki { 80079c0373fSRafael J. Wysocki struct acpi_device *adev = ACPI_COMPANION(dev); 801e5cc8ef3SRafael J. Wysocki int error; 802e5cc8ef3SRafael J. Wysocki 803e5cc8ef3SRafael J. Wysocki if (!adev) 804e5cc8ef3SRafael J. Wysocki return 0; 805e5cc8ef3SRafael J. Wysocki 806e5cc8ef3SRafael J. Wysocki error = acpi_dev_pm_full_power(adev); 807e5cc8ef3SRafael J. Wysocki __acpi_device_run_wake(adev, false); 808e5cc8ef3SRafael J. Wysocki return error; 809e5cc8ef3SRafael J. Wysocki } 810e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_runtime_resume); 811e5cc8ef3SRafael J. Wysocki 812e5cc8ef3SRafael J. Wysocki /** 813e5cc8ef3SRafael J. Wysocki * acpi_subsys_runtime_suspend - Suspend device using ACPI. 814e5cc8ef3SRafael J. Wysocki * @dev: Device to suspend. 815e5cc8ef3SRafael J. Wysocki * 816e5cc8ef3SRafael J. Wysocki * Carry out the generic runtime suspend procedure for @dev and use ACPI to put 817e5cc8ef3SRafael J. Wysocki * it into a runtime low-power state. 818e5cc8ef3SRafael J. Wysocki */ 819e5cc8ef3SRafael J. Wysocki int acpi_subsys_runtime_suspend(struct device *dev) 820e5cc8ef3SRafael J. Wysocki { 821e5cc8ef3SRafael J. Wysocki int ret = pm_generic_runtime_suspend(dev); 822e5cc8ef3SRafael J. Wysocki return ret ? ret : acpi_dev_runtime_suspend(dev); 823e5cc8ef3SRafael J. Wysocki } 824e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend); 825e5cc8ef3SRafael J. Wysocki 826e5cc8ef3SRafael J. Wysocki /** 827e5cc8ef3SRafael J. Wysocki * acpi_subsys_runtime_resume - Resume device using ACPI. 828e5cc8ef3SRafael J. Wysocki * @dev: Device to Resume. 829e5cc8ef3SRafael J. Wysocki * 830e5cc8ef3SRafael J. Wysocki * Use ACPI to put the given device into the full-power state and carry out the 831e5cc8ef3SRafael J. Wysocki * generic runtime resume procedure for it. 832e5cc8ef3SRafael J. Wysocki */ 833e5cc8ef3SRafael J. Wysocki int acpi_subsys_runtime_resume(struct device *dev) 834e5cc8ef3SRafael J. Wysocki { 835e5cc8ef3SRafael J. Wysocki int ret = acpi_dev_runtime_resume(dev); 836e5cc8ef3SRafael J. Wysocki return ret ? ret : pm_generic_runtime_resume(dev); 837e5cc8ef3SRafael J. Wysocki } 838e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume); 839e5cc8ef3SRafael J. Wysocki #endif /* CONFIG_PM_RUNTIME */ 840e5cc8ef3SRafael J. Wysocki 841e5cc8ef3SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP 842e5cc8ef3SRafael J. Wysocki /** 843e5cc8ef3SRafael J. Wysocki * acpi_dev_suspend_late - Put device into a low-power state using ACPI. 844e5cc8ef3SRafael J. Wysocki * @dev: Device to put into a low-power state. 845e5cc8ef3SRafael J. Wysocki * 846e5cc8ef3SRafael J. Wysocki * Put the given device into a low-power state during system transition to a 847e5cc8ef3SRafael J. Wysocki * sleep state using the standard ACPI mechanism. Set up system wakeup if 848e5cc8ef3SRafael J. Wysocki * desired, choose the state to put the device into (this checks if system 849e5cc8ef3SRafael J. Wysocki * wakeup is expected to work too), and set the power state of the device. 850e5cc8ef3SRafael J. Wysocki */ 851e5cc8ef3SRafael J. Wysocki int acpi_dev_suspend_late(struct device *dev) 852e5cc8ef3SRafael J. Wysocki { 85379c0373fSRafael J. Wysocki struct acpi_device *adev = ACPI_COMPANION(dev); 854e5cc8ef3SRafael J. Wysocki u32 target_state; 855e5cc8ef3SRafael J. Wysocki bool wakeup; 856e5cc8ef3SRafael J. Wysocki int error; 857e5cc8ef3SRafael J. Wysocki 858e5cc8ef3SRafael J. Wysocki if (!adev) 859e5cc8ef3SRafael J. Wysocki return 0; 860e5cc8ef3SRafael J. Wysocki 861e5cc8ef3SRafael J. Wysocki target_state = acpi_target_system_state(); 862e5cc8ef3SRafael J. Wysocki wakeup = device_may_wakeup(dev); 863e5cc8ef3SRafael J. Wysocki error = __acpi_device_sleep_wake(adev, target_state, wakeup); 864e5cc8ef3SRafael J. Wysocki if (wakeup && error) 865e5cc8ef3SRafael J. Wysocki return error; 866e5cc8ef3SRafael J. Wysocki 867e5cc8ef3SRafael J. Wysocki error = acpi_dev_pm_low_power(dev, adev, target_state); 868e5cc8ef3SRafael J. Wysocki if (error) 869e5cc8ef3SRafael J. Wysocki __acpi_device_sleep_wake(adev, ACPI_STATE_UNKNOWN, false); 870e5cc8ef3SRafael J. Wysocki 871e5cc8ef3SRafael J. Wysocki return error; 872e5cc8ef3SRafael J. Wysocki } 873e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_suspend_late); 874e5cc8ef3SRafael J. Wysocki 875e5cc8ef3SRafael J. Wysocki /** 876e5cc8ef3SRafael J. Wysocki * acpi_dev_resume_early - Put device into the full-power state using ACPI. 877e5cc8ef3SRafael J. Wysocki * @dev: Device to put into the full-power state. 878e5cc8ef3SRafael J. Wysocki * 879e5cc8ef3SRafael J. Wysocki * Put the given device into the full-power state using the standard ACPI 880e5cc8ef3SRafael J. Wysocki * mechanism during system transition to the working state. Set the power 881e5cc8ef3SRafael J. Wysocki * state of the device to ACPI D0 and disable remote wakeup. 882e5cc8ef3SRafael J. Wysocki */ 883e5cc8ef3SRafael J. Wysocki int acpi_dev_resume_early(struct device *dev) 884e5cc8ef3SRafael J. Wysocki { 88579c0373fSRafael J. Wysocki struct acpi_device *adev = ACPI_COMPANION(dev); 886e5cc8ef3SRafael J. Wysocki int error; 887e5cc8ef3SRafael J. Wysocki 888e5cc8ef3SRafael J. Wysocki if (!adev) 889e5cc8ef3SRafael J. Wysocki return 0; 890e5cc8ef3SRafael J. Wysocki 891e5cc8ef3SRafael J. Wysocki error = acpi_dev_pm_full_power(adev); 892e5cc8ef3SRafael J. Wysocki __acpi_device_sleep_wake(adev, ACPI_STATE_UNKNOWN, false); 893e5cc8ef3SRafael J. Wysocki return error; 894e5cc8ef3SRafael J. Wysocki } 895e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_resume_early); 896e5cc8ef3SRafael J. Wysocki 897e5cc8ef3SRafael J. Wysocki /** 898e5cc8ef3SRafael J. Wysocki * acpi_subsys_prepare - Prepare device for system transition to a sleep state. 899e5cc8ef3SRafael J. Wysocki * @dev: Device to prepare. 900e5cc8ef3SRafael J. Wysocki */ 901e5cc8ef3SRafael J. Wysocki int acpi_subsys_prepare(struct device *dev) 902e5cc8ef3SRafael J. Wysocki { 903*f25c0ae2SRafael J. Wysocki struct acpi_device *adev = ACPI_COMPANION(dev); 904*f25c0ae2SRafael J. Wysocki u32 sys_target; 905*f25c0ae2SRafael J. Wysocki int ret, state; 90692858c47SRafael J. Wysocki 907*f25c0ae2SRafael J. Wysocki ret = pm_generic_prepare(dev); 908*f25c0ae2SRafael J. Wysocki if (ret < 0) 909*f25c0ae2SRafael J. Wysocki return ret; 910*f25c0ae2SRafael J. Wysocki 911*f25c0ae2SRafael J. Wysocki if (!adev || !pm_runtime_suspended(dev) 912*f25c0ae2SRafael J. Wysocki || device_may_wakeup(dev) != !!adev->wakeup.prepare_count) 913*f25c0ae2SRafael J. Wysocki return 0; 914*f25c0ae2SRafael J. Wysocki 915*f25c0ae2SRafael J. Wysocki sys_target = acpi_target_system_state(); 916*f25c0ae2SRafael J. Wysocki if (sys_target == ACPI_STATE_S0) 917*f25c0ae2SRafael J. Wysocki return 1; 918*f25c0ae2SRafael J. Wysocki 919*f25c0ae2SRafael J. Wysocki if (adev->power.flags.dsw_present) 920*f25c0ae2SRafael J. Wysocki return 0; 921*f25c0ae2SRafael J. Wysocki 922*f25c0ae2SRafael J. Wysocki ret = acpi_dev_pm_get_state(dev, adev, sys_target, NULL, &state); 923*f25c0ae2SRafael J. Wysocki return !ret && state == adev->power.state; 924e5cc8ef3SRafael J. Wysocki } 925e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_prepare); 926e5cc8ef3SRafael J. Wysocki 927e5cc8ef3SRafael J. Wysocki /** 928*f25c0ae2SRafael J. Wysocki * acpi_subsys_complete - Finalize device's resume during system resume. 929*f25c0ae2SRafael J. Wysocki * @dev: Device to handle. 930*f25c0ae2SRafael J. Wysocki */ 931*f25c0ae2SRafael J. Wysocki static void acpi_subsys_complete(struct device *dev) 932*f25c0ae2SRafael J. Wysocki { 933*f25c0ae2SRafael J. Wysocki /* 934*f25c0ae2SRafael J. Wysocki * If the device had been runtime-suspended before the system went into 935*f25c0ae2SRafael J. Wysocki * the sleep state it is going out of and it has never been resumed till 936*f25c0ae2SRafael J. Wysocki * now, resume it in case the firmware powered it up. 937*f25c0ae2SRafael J. Wysocki */ 938*f25c0ae2SRafael J. Wysocki if (dev->power.direct_complete) 939*f25c0ae2SRafael J. Wysocki pm_request_resume(dev); 940*f25c0ae2SRafael J. Wysocki } 941*f25c0ae2SRafael J. Wysocki 942*f25c0ae2SRafael J. Wysocki /** 94392858c47SRafael J. Wysocki * acpi_subsys_suspend - Run the device driver's suspend callback. 94492858c47SRafael J. Wysocki * @dev: Device to handle. 94592858c47SRafael J. Wysocki * 94692858c47SRafael J. Wysocki * Follow PCI and resume devices suspended at run time before running their 94792858c47SRafael J. Wysocki * system suspend callbacks. 94892858c47SRafael J. Wysocki */ 94992858c47SRafael J. Wysocki int acpi_subsys_suspend(struct device *dev) 95092858c47SRafael J. Wysocki { 95192858c47SRafael J. Wysocki pm_runtime_resume(dev); 95292858c47SRafael J. Wysocki return pm_generic_suspend(dev); 95392858c47SRafael J. Wysocki } 95492858c47SRafael J. Wysocki 95592858c47SRafael J. Wysocki /** 956e5cc8ef3SRafael J. Wysocki * acpi_subsys_suspend_late - Suspend device using ACPI. 957e5cc8ef3SRafael J. Wysocki * @dev: Device to suspend. 958e5cc8ef3SRafael J. Wysocki * 959e5cc8ef3SRafael J. Wysocki * Carry out the generic late suspend procedure for @dev and use ACPI to put 960e5cc8ef3SRafael J. Wysocki * it into a low-power state during system transition into a sleep state. 961e5cc8ef3SRafael J. Wysocki */ 962e5cc8ef3SRafael J. Wysocki int acpi_subsys_suspend_late(struct device *dev) 963e5cc8ef3SRafael J. Wysocki { 964e5cc8ef3SRafael J. Wysocki int ret = pm_generic_suspend_late(dev); 965e5cc8ef3SRafael J. Wysocki return ret ? ret : acpi_dev_suspend_late(dev); 966e5cc8ef3SRafael J. Wysocki } 967e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late); 968e5cc8ef3SRafael J. Wysocki 969e5cc8ef3SRafael J. Wysocki /** 970e5cc8ef3SRafael J. Wysocki * acpi_subsys_resume_early - Resume device using ACPI. 971e5cc8ef3SRafael J. Wysocki * @dev: Device to Resume. 972e5cc8ef3SRafael J. Wysocki * 973e5cc8ef3SRafael J. Wysocki * Use ACPI to put the given device into the full-power state and carry out the 974e5cc8ef3SRafael J. Wysocki * generic early resume procedure for it during system transition into the 975e5cc8ef3SRafael J. Wysocki * working state. 976e5cc8ef3SRafael J. Wysocki */ 977e5cc8ef3SRafael J. Wysocki int acpi_subsys_resume_early(struct device *dev) 978e5cc8ef3SRafael J. Wysocki { 979e5cc8ef3SRafael J. Wysocki int ret = acpi_dev_resume_early(dev); 980e5cc8ef3SRafael J. Wysocki return ret ? ret : pm_generic_resume_early(dev); 981e5cc8ef3SRafael J. Wysocki } 982e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_resume_early); 98392858c47SRafael J. Wysocki 98492858c47SRafael J. Wysocki /** 98592858c47SRafael J. Wysocki * acpi_subsys_freeze - Run the device driver's freeze callback. 98692858c47SRafael J. Wysocki * @dev: Device to handle. 98792858c47SRafael J. Wysocki */ 98892858c47SRafael J. Wysocki int acpi_subsys_freeze(struct device *dev) 98992858c47SRafael J. Wysocki { 99092858c47SRafael J. Wysocki /* 99192858c47SRafael J. Wysocki * This used to be done in acpi_subsys_prepare() for all devices and 99292858c47SRafael J. Wysocki * some drivers may depend on it, so do it here. Ideally, however, 99392858c47SRafael J. Wysocki * runtime-suspended devices should not be touched during freeze/thaw 99492858c47SRafael J. Wysocki * transitions. 99592858c47SRafael J. Wysocki */ 99692858c47SRafael J. Wysocki pm_runtime_resume(dev); 99792858c47SRafael J. Wysocki return pm_generic_freeze(dev); 99892858c47SRafael J. Wysocki } 99992858c47SRafael J. Wysocki 1000e5cc8ef3SRafael J. Wysocki #endif /* CONFIG_PM_SLEEP */ 1001e5cc8ef3SRafael J. Wysocki 1002e5cc8ef3SRafael J. Wysocki static struct dev_pm_domain acpi_general_pm_domain = { 1003e5cc8ef3SRafael J. Wysocki .ops = { 1004e5cc8ef3SRafael J. Wysocki #ifdef CONFIG_PM_RUNTIME 1005e5cc8ef3SRafael J. Wysocki .runtime_suspend = acpi_subsys_runtime_suspend, 1006e5cc8ef3SRafael J. Wysocki .runtime_resume = acpi_subsys_runtime_resume, 1007e5cc8ef3SRafael J. Wysocki #endif 1008e5cc8ef3SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP 1009e5cc8ef3SRafael J. Wysocki .prepare = acpi_subsys_prepare, 1010*f25c0ae2SRafael J. Wysocki .complete = acpi_subsys_complete, 101192858c47SRafael J. Wysocki .suspend = acpi_subsys_suspend, 1012e5cc8ef3SRafael J. Wysocki .suspend_late = acpi_subsys_suspend_late, 1013e5cc8ef3SRafael J. Wysocki .resume_early = acpi_subsys_resume_early, 101492858c47SRafael J. Wysocki .freeze = acpi_subsys_freeze, 101592858c47SRafael J. Wysocki .poweroff = acpi_subsys_suspend, 1016e5cc8ef3SRafael J. Wysocki .poweroff_late = acpi_subsys_suspend_late, 1017e5cc8ef3SRafael J. Wysocki .restore_early = acpi_subsys_resume_early, 1018e5cc8ef3SRafael J. Wysocki #endif 1019e5cc8ef3SRafael J. Wysocki }, 1020e5cc8ef3SRafael J. Wysocki }; 1021e5cc8ef3SRafael J. Wysocki 1022e5cc8ef3SRafael J. Wysocki /** 1023e5cc8ef3SRafael J. Wysocki * acpi_dev_pm_attach - Prepare device for ACPI power management. 1024e5cc8ef3SRafael J. Wysocki * @dev: Device to prepare. 1025b88ce2a4SRafael J. Wysocki * @power_on: Whether or not to power on the device. 1026e5cc8ef3SRafael J. Wysocki * 1027e5cc8ef3SRafael J. Wysocki * If @dev has a valid ACPI handle that has a valid struct acpi_device object 1028e5cc8ef3SRafael J. Wysocki * attached to it, install a wakeup notification handler for the device and 1029b88ce2a4SRafael J. Wysocki * add it to the general ACPI PM domain. If @power_on is set, the device will 1030b88ce2a4SRafael J. Wysocki * be put into the ACPI D0 state before the function returns. 1031e5cc8ef3SRafael J. Wysocki * 1032e5cc8ef3SRafael J. Wysocki * This assumes that the @dev's bus type uses generic power management callbacks 1033e5cc8ef3SRafael J. Wysocki * (or doesn't use any power management callbacks at all). 1034e5cc8ef3SRafael J. Wysocki * 1035e5cc8ef3SRafael J. Wysocki * Callers must ensure proper synchronization of this function with power 1036e5cc8ef3SRafael J. Wysocki * management callbacks. 1037e5cc8ef3SRafael J. Wysocki */ 1038b88ce2a4SRafael J. Wysocki int acpi_dev_pm_attach(struct device *dev, bool power_on) 1039e5cc8ef3SRafael J. Wysocki { 104079c0373fSRafael J. Wysocki struct acpi_device *adev = ACPI_COMPANION(dev); 1041e5cc8ef3SRafael J. Wysocki 1042e5cc8ef3SRafael J. Wysocki if (!adev) 1043e5cc8ef3SRafael J. Wysocki return -ENODEV; 1044e5cc8ef3SRafael J. Wysocki 1045e5cc8ef3SRafael J. Wysocki if (dev->pm_domain) 1046e5cc8ef3SRafael J. Wysocki return -EEXIST; 1047e5cc8ef3SRafael J. Wysocki 1048e5cc8ef3SRafael J. Wysocki acpi_add_pm_notifier(adev, acpi_wakeup_device, dev); 1049e5cc8ef3SRafael J. Wysocki dev->pm_domain = &acpi_general_pm_domain; 1050b88ce2a4SRafael J. Wysocki if (power_on) { 1051b88ce2a4SRafael J. Wysocki acpi_dev_pm_full_power(adev); 1052b88ce2a4SRafael J. Wysocki __acpi_device_run_wake(adev, false); 1053b88ce2a4SRafael J. Wysocki } 1054e5cc8ef3SRafael J. Wysocki return 0; 1055e5cc8ef3SRafael J. Wysocki } 1056e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_pm_attach); 1057e5cc8ef3SRafael J. Wysocki 1058e5cc8ef3SRafael J. Wysocki /** 1059e5cc8ef3SRafael J. Wysocki * acpi_dev_pm_detach - Remove ACPI power management from the device. 1060e5cc8ef3SRafael J. Wysocki * @dev: Device to take care of. 1061b88ce2a4SRafael J. Wysocki * @power_off: Whether or not to try to remove power from the device. 1062e5cc8ef3SRafael J. Wysocki * 1063e5cc8ef3SRafael J. Wysocki * Remove the device from the general ACPI PM domain and remove its wakeup 1064b88ce2a4SRafael J. Wysocki * notifier. If @power_off is set, additionally remove power from the device if 1065b88ce2a4SRafael J. Wysocki * possible. 1066e5cc8ef3SRafael J. Wysocki * 1067e5cc8ef3SRafael J. Wysocki * Callers must ensure proper synchronization of this function with power 1068e5cc8ef3SRafael J. Wysocki * management callbacks. 1069e5cc8ef3SRafael J. Wysocki */ 1070b88ce2a4SRafael J. Wysocki void acpi_dev_pm_detach(struct device *dev, bool power_off) 1071e5cc8ef3SRafael J. Wysocki { 107279c0373fSRafael J. Wysocki struct acpi_device *adev = ACPI_COMPANION(dev); 1073e5cc8ef3SRafael J. Wysocki 1074e5cc8ef3SRafael J. Wysocki if (adev && dev->pm_domain == &acpi_general_pm_domain) { 1075e5cc8ef3SRafael J. Wysocki dev->pm_domain = NULL; 1076e5cc8ef3SRafael J. Wysocki acpi_remove_pm_notifier(adev, acpi_wakeup_device); 1077b88ce2a4SRafael J. Wysocki if (power_off) { 1078b88ce2a4SRafael J. Wysocki /* 1079b88ce2a4SRafael J. Wysocki * If the device's PM QoS resume latency limit or flags 1080b88ce2a4SRafael J. Wysocki * have been exposed to user space, they have to be 1081b88ce2a4SRafael J. Wysocki * hidden at this point, so that they don't affect the 1082b88ce2a4SRafael J. Wysocki * choice of the low-power state to put the device into. 1083b88ce2a4SRafael J. Wysocki */ 1084b88ce2a4SRafael J. Wysocki dev_pm_qos_hide_latency_limit(dev); 1085b88ce2a4SRafael J. Wysocki dev_pm_qos_hide_flags(dev); 1086b88ce2a4SRafael J. Wysocki __acpi_device_run_wake(adev, false); 1087b88ce2a4SRafael J. Wysocki acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0); 1088b88ce2a4SRafael J. Wysocki } 1089e5cc8ef3SRafael J. Wysocki } 1090e5cc8ef3SRafael J. Wysocki } 1091e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_pm_detach); 1092ec4602a9SRafael J. Wysocki #endif /* CONFIG_PM */ 1093