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 370c072530fSRafael J. Wysocki static void acpi_pm_notify_handler(acpi_handle handle, u32 val, void *not_used) 371c072530fSRafael J. Wysocki { 372c072530fSRafael J. Wysocki struct acpi_device *adev; 373c072530fSRafael J. Wysocki 374c072530fSRafael J. Wysocki if (val != ACPI_NOTIFY_DEVICE_WAKE) 375c072530fSRafael J. Wysocki return; 376c072530fSRafael J. Wysocki 377c072530fSRafael J. Wysocki adev = acpi_bus_get_acpi_device(handle); 378c072530fSRafael J. Wysocki if (!adev) 379c072530fSRafael J. Wysocki return; 380c072530fSRafael J. Wysocki 381c072530fSRafael J. Wysocki mutex_lock(&acpi_pm_notifier_lock); 382c072530fSRafael J. Wysocki 383c072530fSRafael J. Wysocki if (adev->wakeup.flags.notifier_present) { 384c072530fSRafael J. Wysocki __pm_wakeup_event(adev->wakeup.ws, 0); 385c072530fSRafael J. Wysocki if (adev->wakeup.context.work.func) 386c072530fSRafael J. Wysocki queue_pm_work(&adev->wakeup.context.work); 387c072530fSRafael J. Wysocki } 388c072530fSRafael J. Wysocki 389c072530fSRafael J. Wysocki mutex_unlock(&acpi_pm_notifier_lock); 390c072530fSRafael J. Wysocki 391c072530fSRafael J. Wysocki acpi_bus_put_acpi_device(adev); 392c072530fSRafael J. Wysocki } 393c072530fSRafael J. Wysocki 394ec4602a9SRafael J. Wysocki /** 395c072530fSRafael J. Wysocki * acpi_add_pm_notifier - Register PM notify handler for given ACPI device. 396c072530fSRafael J. Wysocki * @adev: ACPI device to add the notify handler for. 397c072530fSRafael J. Wysocki * @dev: Device to generate a wakeup event for while handling the notification. 398c072530fSRafael J. Wysocki * @work_func: Work function to execute when handling the notification. 399ec4602a9SRafael J. Wysocki * 400ec4602a9SRafael J. Wysocki * NOTE: @adev need not be a run-wake or wakeup device to be a valid source of 401ec4602a9SRafael J. Wysocki * PM wakeup events. For example, wakeup events may be generated for bridges 402ec4602a9SRafael J. Wysocki * if one of the devices below the bridge is signaling wakeup, even if the 403ec4602a9SRafael J. Wysocki * bridge itself doesn't have a wakeup GPE associated with it. 404ec4602a9SRafael J. Wysocki */ 405c072530fSRafael J. Wysocki acpi_status acpi_add_pm_notifier(struct acpi_device *adev, struct device *dev, 406c072530fSRafael J. Wysocki void (*work_func)(struct work_struct *work)) 407ec4602a9SRafael J. Wysocki { 408ec4602a9SRafael J. Wysocki acpi_status status = AE_ALREADY_EXISTS; 409ec4602a9SRafael J. Wysocki 410c072530fSRafael J. Wysocki if (!dev && !work_func) 411c072530fSRafael J. Wysocki return AE_BAD_PARAMETER; 412c072530fSRafael J. Wysocki 413ec4602a9SRafael J. Wysocki mutex_lock(&acpi_pm_notifier_lock); 414ec4602a9SRafael J. Wysocki 415ec4602a9SRafael J. Wysocki if (adev->wakeup.flags.notifier_present) 416ec4602a9SRafael J. Wysocki goto out; 417ec4602a9SRafael J. Wysocki 418c072530fSRafael J. Wysocki adev->wakeup.ws = wakeup_source_register(dev_name(&adev->dev)); 419c072530fSRafael J. Wysocki adev->wakeup.context.dev = dev; 420c072530fSRafael J. Wysocki if (work_func) 421c072530fSRafael J. Wysocki INIT_WORK(&adev->wakeup.context.work, work_func); 422c072530fSRafael J. Wysocki 423c072530fSRafael J. Wysocki status = acpi_install_notify_handler(adev->handle, ACPI_SYSTEM_NOTIFY, 424c072530fSRafael J. Wysocki acpi_pm_notify_handler, NULL); 425ec4602a9SRafael J. Wysocki if (ACPI_FAILURE(status)) 426ec4602a9SRafael J. Wysocki goto out; 427ec4602a9SRafael J. Wysocki 428ec4602a9SRafael J. Wysocki adev->wakeup.flags.notifier_present = true; 429ec4602a9SRafael J. Wysocki 430ec4602a9SRafael J. Wysocki out: 431ec4602a9SRafael J. Wysocki mutex_unlock(&acpi_pm_notifier_lock); 432ec4602a9SRafael J. Wysocki return status; 433ec4602a9SRafael J. Wysocki } 434ec4602a9SRafael J. Wysocki 435ec4602a9SRafael J. Wysocki /** 436ec4602a9SRafael J. Wysocki * acpi_remove_pm_notifier - Unregister PM notifier from given ACPI device. 437ec4602a9SRafael J. Wysocki * @adev: ACPI device to remove the notifier from. 438ec4602a9SRafael J. Wysocki */ 439c072530fSRafael J. Wysocki acpi_status acpi_remove_pm_notifier(struct acpi_device *adev) 440ec4602a9SRafael J. Wysocki { 441ec4602a9SRafael J. Wysocki acpi_status status = AE_BAD_PARAMETER; 442ec4602a9SRafael J. Wysocki 443ec4602a9SRafael J. Wysocki mutex_lock(&acpi_pm_notifier_lock); 444ec4602a9SRafael J. Wysocki 445ec4602a9SRafael J. Wysocki if (!adev->wakeup.flags.notifier_present) 446ec4602a9SRafael J. Wysocki goto out; 447ec4602a9SRafael J. Wysocki 448ec4602a9SRafael J. Wysocki status = acpi_remove_notify_handler(adev->handle, 449ec4602a9SRafael J. Wysocki ACPI_SYSTEM_NOTIFY, 450c072530fSRafael J. Wysocki acpi_pm_notify_handler); 451ec4602a9SRafael J. Wysocki if (ACPI_FAILURE(status)) 452ec4602a9SRafael J. Wysocki goto out; 453ec4602a9SRafael J. Wysocki 454c072530fSRafael J. Wysocki if (adev->wakeup.context.work.func) { 455c072530fSRafael J. Wysocki cancel_work_sync(&adev->wakeup.context.work); 456c072530fSRafael J. Wysocki adev->wakeup.context.work.func = NULL; 457c072530fSRafael J. Wysocki } 458c072530fSRafael J. Wysocki adev->wakeup.context.dev = NULL; 459c072530fSRafael J. Wysocki wakeup_source_unregister(adev->wakeup.ws); 460c072530fSRafael J. Wysocki 461ec4602a9SRafael J. Wysocki adev->wakeup.flags.notifier_present = false; 462ec4602a9SRafael J. Wysocki 463ec4602a9SRafael J. Wysocki out: 464ec4602a9SRafael J. Wysocki mutex_unlock(&acpi_pm_notifier_lock); 465ec4602a9SRafael J. Wysocki return status; 466ec4602a9SRafael J. Wysocki } 467ec4602a9SRafael J. Wysocki 4689ce4e607SRafael J. Wysocki bool acpi_bus_can_wakeup(acpi_handle handle) 4699ce4e607SRafael J. Wysocki { 4709ce4e607SRafael J. Wysocki struct acpi_device *device; 4719ce4e607SRafael J. Wysocki int result; 4729ce4e607SRafael J. Wysocki 4739ce4e607SRafael J. Wysocki result = acpi_bus_get_device(handle, &device); 4749ce4e607SRafael J. Wysocki return result ? false : device->wakeup.flags.valid; 4759ce4e607SRafael J. Wysocki } 4769ce4e607SRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_can_wakeup); 4779ce4e607SRafael J. Wysocki 4789ce4e607SRafael J. Wysocki /** 479b25c77efSRafael J. Wysocki * acpi_dev_pm_get_state - Get preferred power state of ACPI device. 48086b3832cSRafael J. Wysocki * @dev: Device whose preferred target power state to return. 48186b3832cSRafael J. Wysocki * @adev: ACPI device node corresponding to @dev. 48286b3832cSRafael J. Wysocki * @target_state: System state to match the resultant device state. 483fa1675b5SRafael J. Wysocki * @d_min_p: Location to store the highest power state available to the device. 484fa1675b5SRafael J. Wysocki * @d_max_p: Location to store the lowest power state available to the device. 48586b3832cSRafael J. Wysocki * 486fa1675b5SRafael J. Wysocki * Find the lowest power (highest number) and highest power (lowest number) ACPI 487fa1675b5SRafael J. Wysocki * device power states that the device can be in while the system is in the 488fa1675b5SRafael J. Wysocki * state represented by @target_state. Store the integer numbers representing 489fa1675b5SRafael J. Wysocki * those stats in the memory locations pointed to by @d_max_p and @d_min_p, 490fa1675b5SRafael J. Wysocki * respectively. 49186b3832cSRafael J. Wysocki * 49286b3832cSRafael J. Wysocki * Callers must ensure that @dev and @adev are valid pointers and that @adev 49386b3832cSRafael J. Wysocki * actually corresponds to @dev before using this function. 494fa1675b5SRafael J. Wysocki * 495fa1675b5SRafael J. Wysocki * Returns 0 on success or -ENODATA when one of the ACPI methods fails or 496fa1675b5SRafael J. Wysocki * returns a value that doesn't make sense. The memory locations pointed to by 497fa1675b5SRafael J. Wysocki * @d_max_p and @d_min_p are only modified on success. 49886b3832cSRafael J. Wysocki */ 499b25c77efSRafael J. Wysocki static int acpi_dev_pm_get_state(struct device *dev, struct acpi_device *adev, 500fa1675b5SRafael J. Wysocki u32 target_state, int *d_min_p, int *d_max_p) 50186b3832cSRafael J. Wysocki { 502fa1675b5SRafael J. Wysocki char method[] = { '_', 'S', '0' + target_state, 'D', '\0' }; 503fa1675b5SRafael J. Wysocki acpi_handle handle = adev->handle; 504fa1675b5SRafael J. Wysocki unsigned long long ret; 505fa1675b5SRafael J. Wysocki int d_min, d_max; 50686b3832cSRafael J. Wysocki bool wakeup = false; 507fa1675b5SRafael J. Wysocki acpi_status status; 50886b3832cSRafael J. Wysocki 50986b3832cSRafael J. Wysocki /* 510fa1675b5SRafael J. Wysocki * If the system state is S0, the lowest power state the device can be 511fa1675b5SRafael J. Wysocki * in is D3cold, unless the device has _S0W and is supposed to signal 512fa1675b5SRafael J. Wysocki * wakeup, in which case the return value of _S0W has to be used as the 513fa1675b5SRafael J. Wysocki * lowest power state available to the device. 51486b3832cSRafael J. Wysocki */ 51586b3832cSRafael J. Wysocki d_min = ACPI_STATE_D0; 5164c164ae7SRafael J. Wysocki d_max = ACPI_STATE_D3_COLD; 51786b3832cSRafael J. Wysocki 51886b3832cSRafael J. Wysocki /* 51986b3832cSRafael J. Wysocki * If present, _SxD methods return the minimum D-state (highest power 52086b3832cSRafael J. Wysocki * state) we can use for the corresponding S-states. Otherwise, the 52186b3832cSRafael J. Wysocki * minimum D-state is D0 (ACPI 3.x). 52286b3832cSRafael J. Wysocki */ 52386b3832cSRafael J. Wysocki if (target_state > ACPI_STATE_S0) { 524fa1675b5SRafael J. Wysocki /* 525fa1675b5SRafael J. Wysocki * We rely on acpi_evaluate_integer() not clobbering the integer 526fa1675b5SRafael J. Wysocki * provided if AE_NOT_FOUND is returned. 527fa1675b5SRafael J. Wysocki */ 528fa1675b5SRafael J. Wysocki ret = d_min; 529fa1675b5SRafael J. Wysocki status = acpi_evaluate_integer(handle, method, NULL, &ret); 530fa1675b5SRafael J. Wysocki if ((ACPI_FAILURE(status) && status != AE_NOT_FOUND) 531fa1675b5SRafael J. Wysocki || ret > ACPI_STATE_D3_COLD) 532fa1675b5SRafael J. Wysocki return -ENODATA; 533fa1675b5SRafael J. Wysocki 534fa1675b5SRafael J. Wysocki /* 535fa1675b5SRafael J. Wysocki * We need to handle legacy systems where D3hot and D3cold are 536fa1675b5SRafael J. Wysocki * the same and 3 is returned in both cases, so fall back to 537fa1675b5SRafael J. Wysocki * D3cold if D3hot is not a valid state. 538fa1675b5SRafael J. Wysocki */ 539fa1675b5SRafael J. Wysocki if (!adev->power.states[ret].flags.valid) { 540fa1675b5SRafael J. Wysocki if (ret == ACPI_STATE_D3_HOT) 541fa1675b5SRafael J. Wysocki ret = ACPI_STATE_D3_COLD; 542fa1675b5SRafael J. Wysocki else 543fa1675b5SRafael J. Wysocki return -ENODATA; 544fa1675b5SRafael J. Wysocki } 545fa1675b5SRafael J. Wysocki d_min = ret; 54686b3832cSRafael J. Wysocki wakeup = device_may_wakeup(dev) && adev->wakeup.flags.valid 54786b3832cSRafael J. Wysocki && adev->wakeup.sleep_state >= target_state; 54886b3832cSRafael J. Wysocki } else if (dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) != 54986b3832cSRafael J. Wysocki PM_QOS_FLAGS_NONE) { 55086b3832cSRafael J. Wysocki wakeup = adev->wakeup.flags.valid; 55186b3832cSRafael J. Wysocki } 55286b3832cSRafael J. Wysocki 55386b3832cSRafael J. Wysocki /* 55486b3832cSRafael J. Wysocki * If _PRW says we can wake up the system from the target sleep state, 55586b3832cSRafael J. Wysocki * the D-state returned by _SxD is sufficient for that (we assume a 55686b3832cSRafael J. Wysocki * wakeup-aware driver if wake is set). Still, if _SxW exists 55786b3832cSRafael J. Wysocki * (ACPI 3.x), it should return the maximum (lowest power) D-state that 55886b3832cSRafael J. Wysocki * can wake the system. _S0W may be valid, too. 55986b3832cSRafael J. Wysocki */ 56086b3832cSRafael J. Wysocki if (wakeup) { 561fa1675b5SRafael J. Wysocki method[3] = 'W'; 562fa1675b5SRafael J. Wysocki status = acpi_evaluate_integer(handle, method, NULL, &ret); 563fa1675b5SRafael J. Wysocki if (status == AE_NOT_FOUND) { 564fa1675b5SRafael J. Wysocki if (target_state > ACPI_STATE_S0) 56586b3832cSRafael J. Wysocki d_max = d_min; 566fa1675b5SRafael J. Wysocki } else if (ACPI_SUCCESS(status) && ret <= ACPI_STATE_D3_COLD) { 567fa1675b5SRafael J. Wysocki /* Fall back to D3cold if ret is not a valid state. */ 568fa1675b5SRafael J. Wysocki if (!adev->power.states[ret].flags.valid) 569fa1675b5SRafael J. Wysocki ret = ACPI_STATE_D3_COLD; 570fa1675b5SRafael J. Wysocki 571fa1675b5SRafael J. Wysocki d_max = ret > d_min ? ret : d_min; 572fa1675b5SRafael J. Wysocki } else { 573fa1675b5SRafael J. Wysocki return -ENODATA; 57486b3832cSRafael J. Wysocki } 57586b3832cSRafael J. Wysocki } 57686b3832cSRafael J. Wysocki 57786b3832cSRafael J. Wysocki if (d_min_p) 57886b3832cSRafael J. Wysocki *d_min_p = d_min; 579fa1675b5SRafael J. Wysocki 580fa1675b5SRafael J. Wysocki if (d_max_p) 581fa1675b5SRafael J. Wysocki *d_max_p = d_max; 582fa1675b5SRafael J. Wysocki 583fa1675b5SRafael J. Wysocki return 0; 58486b3832cSRafael J. Wysocki } 585cd7bd02dSRafael J. Wysocki 586a6ae7594SRafael J. Wysocki /** 587a6ae7594SRafael J. Wysocki * acpi_pm_device_sleep_state - Get preferred power state of ACPI device. 588a6ae7594SRafael J. Wysocki * @dev: Device whose preferred target power state to return. 589a6ae7594SRafael J. Wysocki * @d_min_p: Location to store the upper limit of the allowed states range. 590a6ae7594SRafael J. Wysocki * @d_max_in: Deepest low-power state to take into consideration. 591a6ae7594SRafael J. Wysocki * Return value: Preferred power state of the device on success, -ENODEV 592fa1675b5SRafael J. Wysocki * if there's no 'struct acpi_device' for @dev, -EINVAL if @d_max_in is 593fa1675b5SRafael J. Wysocki * incorrect, or -ENODATA on ACPI method failure. 594a6ae7594SRafael J. Wysocki * 595a6ae7594SRafael J. Wysocki * The caller must ensure that @dev is valid before using this function. 596a6ae7594SRafael J. Wysocki */ 597a6ae7594SRafael J. Wysocki int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in) 598a6ae7594SRafael J. Wysocki { 5993a83f992SRafael J. Wysocki acpi_handle handle = ACPI_HANDLE(dev); 600a6ae7594SRafael J. Wysocki struct acpi_device *adev; 6019b5c7a5aSRafael J. Wysocki int ret, d_min, d_max; 602fa1675b5SRafael J. Wysocki 603fa1675b5SRafael J. Wysocki if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3_COLD) 604fa1675b5SRafael J. Wysocki return -EINVAL; 605fa1675b5SRafael J. Wysocki 606fa1675b5SRafael J. Wysocki if (d_max_in > ACPI_STATE_D3_HOT) { 607fa1675b5SRafael J. Wysocki enum pm_qos_flags_status stat; 608fa1675b5SRafael J. Wysocki 609fa1675b5SRafael J. Wysocki stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF); 610fa1675b5SRafael J. Wysocki if (stat == PM_QOS_FLAGS_ALL) 611fa1675b5SRafael J. Wysocki d_max_in = ACPI_STATE_D3_HOT; 612fa1675b5SRafael J. Wysocki } 613a6ae7594SRafael J. Wysocki 614dde3bb41SYasuaki Ishimatsu if (!handle || acpi_bus_get_device(handle, &adev)) { 615a6ae7594SRafael J. Wysocki dev_dbg(dev, "ACPI handle without context in %s!\n", __func__); 616a6ae7594SRafael J. Wysocki return -ENODEV; 617a6ae7594SRafael J. Wysocki } 618a6ae7594SRafael J. Wysocki 619fa1675b5SRafael J. Wysocki ret = acpi_dev_pm_get_state(dev, adev, acpi_target_system_state(), 6209b5c7a5aSRafael J. Wysocki &d_min, &d_max); 621fa1675b5SRafael J. Wysocki if (ret) 622fa1675b5SRafael J. Wysocki return ret; 623fa1675b5SRafael J. Wysocki 6249b5c7a5aSRafael J. Wysocki if (d_max_in < d_min) 625fa1675b5SRafael J. Wysocki return -EINVAL; 626fa1675b5SRafael J. Wysocki 627fa1675b5SRafael J. Wysocki if (d_max > d_max_in) { 6289b5c7a5aSRafael J. Wysocki for (d_max = d_max_in; d_max > d_min; d_max--) { 629fa1675b5SRafael J. Wysocki if (adev->power.states[d_max].flags.valid) 630fa1675b5SRafael J. Wysocki break; 631fa1675b5SRafael J. Wysocki } 632fa1675b5SRafael J. Wysocki } 6339b5c7a5aSRafael J. Wysocki 6349b5c7a5aSRafael J. Wysocki if (d_min_p) 6359b5c7a5aSRafael J. Wysocki *d_min_p = d_min; 6369b5c7a5aSRafael J. Wysocki 637fa1675b5SRafael J. Wysocki return d_max; 638a6ae7594SRafael J. Wysocki } 639a6ae7594SRafael J. Wysocki EXPORT_SYMBOL(acpi_pm_device_sleep_state); 640a6ae7594SRafael J. Wysocki 641cd7bd02dSRafael J. Wysocki /** 642c072530fSRafael J. Wysocki * acpi_pm_notify_work_func - ACPI devices wakeup notification work function. 643c072530fSRafael J. Wysocki * @work: Work item to handle. 644e5cc8ef3SRafael J. Wysocki */ 645c072530fSRafael J. Wysocki static void acpi_pm_notify_work_func(struct work_struct *work) 646e5cc8ef3SRafael J. Wysocki { 647c072530fSRafael J. Wysocki struct device *dev; 648e5cc8ef3SRafael J. Wysocki 649c072530fSRafael J. Wysocki dev = container_of(work, struct acpi_device_wakeup_context, work)->dev; 650c072530fSRafael J. Wysocki if (dev) { 651e5cc8ef3SRafael J. Wysocki pm_wakeup_event(dev, 0); 652e5cc8ef3SRafael J. Wysocki pm_runtime_resume(dev); 653e5cc8ef3SRafael J. Wysocki } 654e5cc8ef3SRafael J. Wysocki } 655e5cc8ef3SRafael J. Wysocki 656e5cc8ef3SRafael J. Wysocki /** 657*f35cec25SRafael J. Wysocki * acpi_device_wakeup - Enable/disable wakeup functionality for device. 658*f35cec25SRafael J. Wysocki * @adev: ACPI device to enable/disable wakeup functionality for. 659*f35cec25SRafael J. Wysocki * @target_state: State the system is transitioning into. 660cd7bd02dSRafael J. Wysocki * @enable: Whether to enable or disable the wakeup functionality. 661cd7bd02dSRafael J. Wysocki * 662dee8370cSRafael J. Wysocki * Enable/disable the GPE associated with @adev so that it can generate 663dee8370cSRafael J. Wysocki * wakeup signals for the device in response to external (remote) events and 664dee8370cSRafael J. Wysocki * enable/disable device wakeup power. 665dee8370cSRafael J. Wysocki * 666dee8370cSRafael J. Wysocki * Callers must ensure that @adev is a valid ACPI device node before executing 667dee8370cSRafael J. Wysocki * this function. 668dee8370cSRafael J. Wysocki */ 669*f35cec25SRafael J. Wysocki static int acpi_device_wakeup(struct acpi_device *adev, u32 target_state, 670*f35cec25SRafael J. Wysocki bool enable) 671dee8370cSRafael J. Wysocki { 672dee8370cSRafael J. Wysocki struct acpi_device_wakeup *wakeup = &adev->wakeup; 673dee8370cSRafael J. Wysocki 674dee8370cSRafael J. Wysocki if (enable) { 675dee8370cSRafael J. Wysocki acpi_status res; 676dee8370cSRafael J. Wysocki int error; 677dee8370cSRafael J. Wysocki 678*f35cec25SRafael J. Wysocki error = acpi_enable_wakeup_device_power(adev, target_state); 679dee8370cSRafael J. Wysocki if (error) 680dee8370cSRafael J. Wysocki return error; 681dee8370cSRafael J. Wysocki 682dee8370cSRafael J. Wysocki res = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number); 683dee8370cSRafael J. Wysocki if (ACPI_FAILURE(res)) { 684dee8370cSRafael J. Wysocki acpi_disable_wakeup_device_power(adev); 685dee8370cSRafael J. Wysocki return -EIO; 686dee8370cSRafael J. Wysocki } 687dee8370cSRafael J. Wysocki } else { 688dee8370cSRafael J. Wysocki acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number); 689dee8370cSRafael J. Wysocki acpi_disable_wakeup_device_power(adev); 690dee8370cSRafael J. Wysocki } 691dee8370cSRafael J. Wysocki return 0; 692dee8370cSRafael J. Wysocki } 693dee8370cSRafael J. Wysocki 694*f35cec25SRafael J. Wysocki #ifdef CONFIG_PM_RUNTIME 695dee8370cSRafael J. Wysocki /** 696dee8370cSRafael J. Wysocki * acpi_pm_device_run_wake - Enable/disable remote wakeup for given device. 697dee8370cSRafael J. Wysocki * @dev: Device to enable/disable the platform to wake up. 698dee8370cSRafael J. Wysocki * @enable: Whether to enable or disable the wakeup functionality. 699cd7bd02dSRafael J. Wysocki */ 700cd7bd02dSRafael J. Wysocki int acpi_pm_device_run_wake(struct device *phys_dev, bool enable) 701cd7bd02dSRafael J. Wysocki { 702dee8370cSRafael J. Wysocki struct acpi_device *adev; 703cd7bd02dSRafael J. Wysocki acpi_handle handle; 704cd7bd02dSRafael J. Wysocki 705cd7bd02dSRafael J. Wysocki if (!device_run_wake(phys_dev)) 706cd7bd02dSRafael J. Wysocki return -EINVAL; 707cd7bd02dSRafael J. Wysocki 7083a83f992SRafael J. Wysocki handle = ACPI_HANDLE(phys_dev); 709dde3bb41SYasuaki Ishimatsu if (!handle || acpi_bus_get_device(handle, &adev)) { 710dee8370cSRafael J. Wysocki dev_dbg(phys_dev, "ACPI handle without context in %s!\n", 711cd7bd02dSRafael J. Wysocki __func__); 712cd7bd02dSRafael J. Wysocki return -ENODEV; 713cd7bd02dSRafael J. Wysocki } 714cd7bd02dSRafael J. Wysocki 715*f35cec25SRafael J. Wysocki return acpi_device_wakeup(adev, enable, ACPI_STATE_S0); 716cd7bd02dSRafael J. Wysocki } 717cd7bd02dSRafael J. Wysocki EXPORT_SYMBOL(acpi_pm_device_run_wake); 718cd7bd02dSRafael J. Wysocki #endif /* CONFIG_PM_RUNTIME */ 719dee8370cSRafael J. Wysocki 720dee8370cSRafael J. Wysocki #ifdef CONFIG_PM_SLEEP 721dee8370cSRafael J. Wysocki /** 722a6ae7594SRafael J. Wysocki * acpi_pm_device_sleep_wake - Enable or disable device to wake up the system. 723a6ae7594SRafael J. Wysocki * @dev: Device to enable/desible to wake up the system from sleep states. 724a6ae7594SRafael J. Wysocki * @enable: Whether to enable or disable @dev to wake up the system. 725a6ae7594SRafael J. Wysocki */ 726a6ae7594SRafael J. Wysocki int acpi_pm_device_sleep_wake(struct device *dev, bool enable) 727a6ae7594SRafael J. Wysocki { 728a6ae7594SRafael J. Wysocki acpi_handle handle; 729a6ae7594SRafael J. Wysocki struct acpi_device *adev; 730a6ae7594SRafael J. Wysocki int error; 731a6ae7594SRafael J. Wysocki 732a6ae7594SRafael J. Wysocki if (!device_can_wakeup(dev)) 733a6ae7594SRafael J. Wysocki return -EINVAL; 734a6ae7594SRafael J. Wysocki 7353a83f992SRafael J. Wysocki handle = ACPI_HANDLE(dev); 736dde3bb41SYasuaki Ishimatsu if (!handle || acpi_bus_get_device(handle, &adev)) { 737a6ae7594SRafael J. Wysocki dev_dbg(dev, "ACPI handle without context in %s!\n", __func__); 738a6ae7594SRafael J. Wysocki return -ENODEV; 739a6ae7594SRafael J. Wysocki } 740a6ae7594SRafael J. Wysocki 741*f35cec25SRafael J. Wysocki error = acpi_device_wakeup(adev, acpi_target_system_state(), enable); 742a6ae7594SRafael J. Wysocki if (!error) 743a6ae7594SRafael J. Wysocki dev_info(dev, "System wakeup %s by ACPI\n", 744a6ae7594SRafael J. Wysocki enable ? "enabled" : "disabled"); 745a6ae7594SRafael J. Wysocki 746a6ae7594SRafael J. Wysocki return error; 747a6ae7594SRafael J. Wysocki } 748dee8370cSRafael J. Wysocki #endif /* CONFIG_PM_SLEEP */ 749e5cc8ef3SRafael J. Wysocki 750e5cc8ef3SRafael J. Wysocki /** 751e5cc8ef3SRafael J. Wysocki * acpi_dev_pm_low_power - Put ACPI device into a low-power state. 752e5cc8ef3SRafael J. Wysocki * @dev: Device to put into a low-power state. 753e5cc8ef3SRafael J. Wysocki * @adev: ACPI device node corresponding to @dev. 754e5cc8ef3SRafael J. Wysocki * @system_state: System state to choose the device state for. 755e5cc8ef3SRafael J. Wysocki */ 756e5cc8ef3SRafael J. Wysocki static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev, 757e5cc8ef3SRafael J. Wysocki u32 system_state) 758e5cc8ef3SRafael J. Wysocki { 759fa1675b5SRafael J. Wysocki int ret, state; 760e5cc8ef3SRafael J. Wysocki 761e5cc8ef3SRafael J. Wysocki if (!acpi_device_power_manageable(adev)) 762e5cc8ef3SRafael J. Wysocki return 0; 763e5cc8ef3SRafael J. Wysocki 764fa1675b5SRafael J. Wysocki ret = acpi_dev_pm_get_state(dev, adev, system_state, NULL, &state); 765fa1675b5SRafael J. Wysocki return ret ? ret : acpi_device_set_power(adev, state); 766e5cc8ef3SRafael J. Wysocki } 767e5cc8ef3SRafael J. Wysocki 768e5cc8ef3SRafael J. Wysocki /** 769e5cc8ef3SRafael J. Wysocki * acpi_dev_pm_full_power - Put ACPI device into the full-power state. 770e5cc8ef3SRafael J. Wysocki * @adev: ACPI device node to put into the full-power state. 771e5cc8ef3SRafael J. Wysocki */ 772e5cc8ef3SRafael J. Wysocki static int acpi_dev_pm_full_power(struct acpi_device *adev) 773e5cc8ef3SRafael J. Wysocki { 774e5cc8ef3SRafael J. Wysocki return acpi_device_power_manageable(adev) ? 775e5cc8ef3SRafael J. Wysocki acpi_device_set_power(adev, ACPI_STATE_D0) : 0; 776e5cc8ef3SRafael J. Wysocki } 777e5cc8ef3SRafael J. Wysocki 778e5cc8ef3SRafael J. Wysocki #ifdef CONFIG_PM_RUNTIME 779e5cc8ef3SRafael J. Wysocki /** 780e5cc8ef3SRafael J. Wysocki * acpi_dev_runtime_suspend - Put device into a low-power state using ACPI. 781e5cc8ef3SRafael J. Wysocki * @dev: Device to put into a low-power state. 782e5cc8ef3SRafael J. Wysocki * 783e5cc8ef3SRafael J. Wysocki * Put the given device into a runtime low-power state using the standard ACPI 784e5cc8ef3SRafael J. Wysocki * mechanism. Set up remote wakeup if desired, choose the state to put the 785e5cc8ef3SRafael J. Wysocki * device into (this checks if remote wakeup is expected to work too), and set 786e5cc8ef3SRafael J. Wysocki * the power state of the device. 787e5cc8ef3SRafael J. Wysocki */ 788e5cc8ef3SRafael J. Wysocki int acpi_dev_runtime_suspend(struct device *dev) 789e5cc8ef3SRafael J. Wysocki { 79079c0373fSRafael J. Wysocki struct acpi_device *adev = ACPI_COMPANION(dev); 791e5cc8ef3SRafael J. Wysocki bool remote_wakeup; 792e5cc8ef3SRafael J. Wysocki int error; 793e5cc8ef3SRafael J. Wysocki 794e5cc8ef3SRafael J. Wysocki if (!adev) 795e5cc8ef3SRafael J. Wysocki return 0; 796e5cc8ef3SRafael J. Wysocki 797e5cc8ef3SRafael J. Wysocki remote_wakeup = dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) > 798e5cc8ef3SRafael J. Wysocki PM_QOS_FLAGS_NONE; 799*f35cec25SRafael J. Wysocki error = acpi_device_wakeup(adev, ACPI_STATE_S0, remote_wakeup); 800e5cc8ef3SRafael J. Wysocki if (remote_wakeup && error) 801e5cc8ef3SRafael J. Wysocki return -EAGAIN; 802e5cc8ef3SRafael J. Wysocki 803e5cc8ef3SRafael J. Wysocki error = acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0); 804e5cc8ef3SRafael J. Wysocki if (error) 805*f35cec25SRafael J. Wysocki acpi_device_wakeup(adev, ACPI_STATE_S0, false); 806e5cc8ef3SRafael J. Wysocki 807e5cc8ef3SRafael J. Wysocki return error; 808e5cc8ef3SRafael J. Wysocki } 809e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_runtime_suspend); 810e5cc8ef3SRafael J. Wysocki 811e5cc8ef3SRafael J. Wysocki /** 812e5cc8ef3SRafael J. Wysocki * acpi_dev_runtime_resume - Put device into the full-power state using ACPI. 813e5cc8ef3SRafael J. Wysocki * @dev: Device to put into the full-power state. 814e5cc8ef3SRafael J. Wysocki * 815e5cc8ef3SRafael J. Wysocki * Put the given device into the full-power state using the standard ACPI 816e5cc8ef3SRafael J. Wysocki * mechanism at run time. Set the power state of the device to ACPI D0 and 817e5cc8ef3SRafael J. Wysocki * disable remote wakeup. 818e5cc8ef3SRafael J. Wysocki */ 819e5cc8ef3SRafael J. Wysocki int acpi_dev_runtime_resume(struct device *dev) 820e5cc8ef3SRafael J. Wysocki { 82179c0373fSRafael J. Wysocki struct acpi_device *adev = ACPI_COMPANION(dev); 822e5cc8ef3SRafael J. Wysocki int error; 823e5cc8ef3SRafael J. Wysocki 824e5cc8ef3SRafael J. Wysocki if (!adev) 825e5cc8ef3SRafael J. Wysocki return 0; 826e5cc8ef3SRafael J. Wysocki 827e5cc8ef3SRafael J. Wysocki error = acpi_dev_pm_full_power(adev); 828*f35cec25SRafael J. Wysocki acpi_device_wakeup(adev, ACPI_STATE_S0, false); 829e5cc8ef3SRafael J. Wysocki return error; 830e5cc8ef3SRafael J. Wysocki } 831e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_runtime_resume); 832e5cc8ef3SRafael J. Wysocki 833e5cc8ef3SRafael J. Wysocki /** 834e5cc8ef3SRafael J. Wysocki * acpi_subsys_runtime_suspend - Suspend device using ACPI. 835e5cc8ef3SRafael J. Wysocki * @dev: Device to suspend. 836e5cc8ef3SRafael J. Wysocki * 837e5cc8ef3SRafael J. Wysocki * Carry out the generic runtime suspend procedure for @dev and use ACPI to put 838e5cc8ef3SRafael J. Wysocki * it into a runtime low-power state. 839e5cc8ef3SRafael J. Wysocki */ 840e5cc8ef3SRafael J. Wysocki int acpi_subsys_runtime_suspend(struct device *dev) 841e5cc8ef3SRafael J. Wysocki { 842e5cc8ef3SRafael J. Wysocki int ret = pm_generic_runtime_suspend(dev); 843e5cc8ef3SRafael J. Wysocki return ret ? ret : acpi_dev_runtime_suspend(dev); 844e5cc8ef3SRafael J. Wysocki } 845e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend); 846e5cc8ef3SRafael J. Wysocki 847e5cc8ef3SRafael J. Wysocki /** 848e5cc8ef3SRafael J. Wysocki * acpi_subsys_runtime_resume - Resume device using ACPI. 849e5cc8ef3SRafael J. Wysocki * @dev: Device to Resume. 850e5cc8ef3SRafael J. Wysocki * 851e5cc8ef3SRafael J. Wysocki * Use ACPI to put the given device into the full-power state and carry out the 852e5cc8ef3SRafael J. Wysocki * generic runtime resume procedure for it. 853e5cc8ef3SRafael J. Wysocki */ 854e5cc8ef3SRafael J. Wysocki int acpi_subsys_runtime_resume(struct device *dev) 855e5cc8ef3SRafael J. Wysocki { 856e5cc8ef3SRafael J. Wysocki int ret = acpi_dev_runtime_resume(dev); 857e5cc8ef3SRafael J. Wysocki return ret ? ret : pm_generic_runtime_resume(dev); 858e5cc8ef3SRafael J. Wysocki } 859e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume); 860e5cc8ef3SRafael J. Wysocki #endif /* CONFIG_PM_RUNTIME */ 861e5cc8ef3SRafael J. Wysocki 862e5cc8ef3SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP 863e5cc8ef3SRafael J. Wysocki /** 864e5cc8ef3SRafael J. Wysocki * acpi_dev_suspend_late - Put device into a low-power state using ACPI. 865e5cc8ef3SRafael J. Wysocki * @dev: Device to put into a low-power state. 866e5cc8ef3SRafael J. Wysocki * 867e5cc8ef3SRafael J. Wysocki * Put the given device into a low-power state during system transition to a 868e5cc8ef3SRafael J. Wysocki * sleep state using the standard ACPI mechanism. Set up system wakeup if 869e5cc8ef3SRafael J. Wysocki * desired, choose the state to put the device into (this checks if system 870e5cc8ef3SRafael J. Wysocki * wakeup is expected to work too), and set the power state of the device. 871e5cc8ef3SRafael J. Wysocki */ 872e5cc8ef3SRafael J. Wysocki int acpi_dev_suspend_late(struct device *dev) 873e5cc8ef3SRafael J. Wysocki { 87479c0373fSRafael J. Wysocki struct acpi_device *adev = ACPI_COMPANION(dev); 875e5cc8ef3SRafael J. Wysocki u32 target_state; 876e5cc8ef3SRafael J. Wysocki bool wakeup; 877e5cc8ef3SRafael J. Wysocki int error; 878e5cc8ef3SRafael J. Wysocki 879e5cc8ef3SRafael J. Wysocki if (!adev) 880e5cc8ef3SRafael J. Wysocki return 0; 881e5cc8ef3SRafael J. Wysocki 882e5cc8ef3SRafael J. Wysocki target_state = acpi_target_system_state(); 883e5cc8ef3SRafael J. Wysocki wakeup = device_may_wakeup(dev); 884*f35cec25SRafael J. Wysocki error = acpi_device_wakeup(adev, target_state, wakeup); 885e5cc8ef3SRafael J. Wysocki if (wakeup && error) 886e5cc8ef3SRafael J. Wysocki return error; 887e5cc8ef3SRafael J. Wysocki 888e5cc8ef3SRafael J. Wysocki error = acpi_dev_pm_low_power(dev, adev, target_state); 889e5cc8ef3SRafael J. Wysocki if (error) 890*f35cec25SRafael J. Wysocki acpi_device_wakeup(adev, ACPI_STATE_UNKNOWN, false); 891e5cc8ef3SRafael J. Wysocki 892e5cc8ef3SRafael J. Wysocki return error; 893e5cc8ef3SRafael J. Wysocki } 894e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_suspend_late); 895e5cc8ef3SRafael J. Wysocki 896e5cc8ef3SRafael J. Wysocki /** 897e5cc8ef3SRafael J. Wysocki * acpi_dev_resume_early - Put device into the full-power state using ACPI. 898e5cc8ef3SRafael J. Wysocki * @dev: Device to put into the full-power state. 899e5cc8ef3SRafael J. Wysocki * 900e5cc8ef3SRafael J. Wysocki * Put the given device into the full-power state using the standard ACPI 901e5cc8ef3SRafael J. Wysocki * mechanism during system transition to the working state. Set the power 902e5cc8ef3SRafael J. Wysocki * state of the device to ACPI D0 and disable remote wakeup. 903e5cc8ef3SRafael J. Wysocki */ 904e5cc8ef3SRafael J. Wysocki int acpi_dev_resume_early(struct device *dev) 905e5cc8ef3SRafael J. Wysocki { 90679c0373fSRafael J. Wysocki struct acpi_device *adev = ACPI_COMPANION(dev); 907e5cc8ef3SRafael J. Wysocki int error; 908e5cc8ef3SRafael J. Wysocki 909e5cc8ef3SRafael J. Wysocki if (!adev) 910e5cc8ef3SRafael J. Wysocki return 0; 911e5cc8ef3SRafael J. Wysocki 912e5cc8ef3SRafael J. Wysocki error = acpi_dev_pm_full_power(adev); 913*f35cec25SRafael J. Wysocki acpi_device_wakeup(adev, ACPI_STATE_UNKNOWN, false); 914e5cc8ef3SRafael J. Wysocki return error; 915e5cc8ef3SRafael J. Wysocki } 916e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_resume_early); 917e5cc8ef3SRafael J. Wysocki 918e5cc8ef3SRafael J. Wysocki /** 919e5cc8ef3SRafael J. Wysocki * acpi_subsys_prepare - Prepare device for system transition to a sleep state. 920e5cc8ef3SRafael J. Wysocki * @dev: Device to prepare. 921e5cc8ef3SRafael J. Wysocki */ 922e5cc8ef3SRafael J. Wysocki int acpi_subsys_prepare(struct device *dev) 923e5cc8ef3SRafael J. Wysocki { 924f25c0ae2SRafael J. Wysocki struct acpi_device *adev = ACPI_COMPANION(dev); 925f25c0ae2SRafael J. Wysocki u32 sys_target; 926f25c0ae2SRafael J. Wysocki int ret, state; 92792858c47SRafael J. Wysocki 928f25c0ae2SRafael J. Wysocki ret = pm_generic_prepare(dev); 929f25c0ae2SRafael J. Wysocki if (ret < 0) 930f25c0ae2SRafael J. Wysocki return ret; 931f25c0ae2SRafael J. Wysocki 932f25c0ae2SRafael J. Wysocki if (!adev || !pm_runtime_suspended(dev) 933f25c0ae2SRafael J. Wysocki || device_may_wakeup(dev) != !!adev->wakeup.prepare_count) 934f25c0ae2SRafael J. Wysocki return 0; 935f25c0ae2SRafael J. Wysocki 936f25c0ae2SRafael J. Wysocki sys_target = acpi_target_system_state(); 937f25c0ae2SRafael J. Wysocki if (sys_target == ACPI_STATE_S0) 938f25c0ae2SRafael J. Wysocki return 1; 939f25c0ae2SRafael J. Wysocki 940f25c0ae2SRafael J. Wysocki if (adev->power.flags.dsw_present) 941f25c0ae2SRafael J. Wysocki return 0; 942f25c0ae2SRafael J. Wysocki 943f25c0ae2SRafael J. Wysocki ret = acpi_dev_pm_get_state(dev, adev, sys_target, NULL, &state); 944f25c0ae2SRafael J. Wysocki return !ret && state == adev->power.state; 945e5cc8ef3SRafael J. Wysocki } 946e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_prepare); 947e5cc8ef3SRafael J. Wysocki 948e5cc8ef3SRafael J. Wysocki /** 949f25c0ae2SRafael J. Wysocki * acpi_subsys_complete - Finalize device's resume during system resume. 950f25c0ae2SRafael J. Wysocki * @dev: Device to handle. 951f25c0ae2SRafael J. Wysocki */ 9524cf563c5SHeikki Krogerus void acpi_subsys_complete(struct device *dev) 953f25c0ae2SRafael J. Wysocki { 954f25c0ae2SRafael J. Wysocki /* 955f25c0ae2SRafael J. Wysocki * If the device had been runtime-suspended before the system went into 956f25c0ae2SRafael J. Wysocki * the sleep state it is going out of and it has never been resumed till 957f25c0ae2SRafael J. Wysocki * now, resume it in case the firmware powered it up. 958f25c0ae2SRafael J. Wysocki */ 959f25c0ae2SRafael J. Wysocki if (dev->power.direct_complete) 960f25c0ae2SRafael J. Wysocki pm_request_resume(dev); 961f25c0ae2SRafael J. Wysocki } 9624cf563c5SHeikki Krogerus EXPORT_SYMBOL_GPL(acpi_subsys_complete); 963f25c0ae2SRafael J. Wysocki 964f25c0ae2SRafael J. Wysocki /** 96592858c47SRafael J. Wysocki * acpi_subsys_suspend - Run the device driver's suspend callback. 96692858c47SRafael J. Wysocki * @dev: Device to handle. 96792858c47SRafael J. Wysocki * 96892858c47SRafael J. Wysocki * Follow PCI and resume devices suspended at run time before running their 96992858c47SRafael J. Wysocki * system suspend callbacks. 97092858c47SRafael J. Wysocki */ 97192858c47SRafael J. Wysocki int acpi_subsys_suspend(struct device *dev) 97292858c47SRafael J. Wysocki { 97392858c47SRafael J. Wysocki pm_runtime_resume(dev); 97492858c47SRafael J. Wysocki return pm_generic_suspend(dev); 97592858c47SRafael J. Wysocki } 9764cf563c5SHeikki Krogerus EXPORT_SYMBOL_GPL(acpi_subsys_suspend); 97792858c47SRafael J. Wysocki 97892858c47SRafael J. Wysocki /** 979e5cc8ef3SRafael J. Wysocki * acpi_subsys_suspend_late - Suspend device using ACPI. 980e5cc8ef3SRafael J. Wysocki * @dev: Device to suspend. 981e5cc8ef3SRafael J. Wysocki * 982e5cc8ef3SRafael J. Wysocki * Carry out the generic late suspend procedure for @dev and use ACPI to put 983e5cc8ef3SRafael J. Wysocki * it into a low-power state during system transition into a sleep state. 984e5cc8ef3SRafael J. Wysocki */ 985e5cc8ef3SRafael J. Wysocki int acpi_subsys_suspend_late(struct device *dev) 986e5cc8ef3SRafael J. Wysocki { 987e5cc8ef3SRafael J. Wysocki int ret = pm_generic_suspend_late(dev); 988e5cc8ef3SRafael J. Wysocki return ret ? ret : acpi_dev_suspend_late(dev); 989e5cc8ef3SRafael J. Wysocki } 990e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late); 991e5cc8ef3SRafael J. Wysocki 992e5cc8ef3SRafael J. Wysocki /** 993e5cc8ef3SRafael J. Wysocki * acpi_subsys_resume_early - Resume device using ACPI. 994e5cc8ef3SRafael J. Wysocki * @dev: Device to Resume. 995e5cc8ef3SRafael J. Wysocki * 996e5cc8ef3SRafael J. Wysocki * Use ACPI to put the given device into the full-power state and carry out the 997e5cc8ef3SRafael J. Wysocki * generic early resume procedure for it during system transition into the 998e5cc8ef3SRafael J. Wysocki * working state. 999e5cc8ef3SRafael J. Wysocki */ 1000e5cc8ef3SRafael J. Wysocki int acpi_subsys_resume_early(struct device *dev) 1001e5cc8ef3SRafael J. Wysocki { 1002e5cc8ef3SRafael J. Wysocki int ret = acpi_dev_resume_early(dev); 1003e5cc8ef3SRafael J. Wysocki return ret ? ret : pm_generic_resume_early(dev); 1004e5cc8ef3SRafael J. Wysocki } 1005e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_resume_early); 100692858c47SRafael J. Wysocki 100792858c47SRafael J. Wysocki /** 100892858c47SRafael J. Wysocki * acpi_subsys_freeze - Run the device driver's freeze callback. 100992858c47SRafael J. Wysocki * @dev: Device to handle. 101092858c47SRafael J. Wysocki */ 101192858c47SRafael J. Wysocki int acpi_subsys_freeze(struct device *dev) 101292858c47SRafael J. Wysocki { 101392858c47SRafael J. Wysocki /* 101492858c47SRafael J. Wysocki * This used to be done in acpi_subsys_prepare() for all devices and 101592858c47SRafael J. Wysocki * some drivers may depend on it, so do it here. Ideally, however, 101692858c47SRafael J. Wysocki * runtime-suspended devices should not be touched during freeze/thaw 101792858c47SRafael J. Wysocki * transitions. 101892858c47SRafael J. Wysocki */ 101992858c47SRafael J. Wysocki pm_runtime_resume(dev); 102092858c47SRafael J. Wysocki return pm_generic_freeze(dev); 102192858c47SRafael J. Wysocki } 10224cf563c5SHeikki Krogerus EXPORT_SYMBOL_GPL(acpi_subsys_freeze); 102392858c47SRafael J. Wysocki 1024e5cc8ef3SRafael J. Wysocki #endif /* CONFIG_PM_SLEEP */ 1025e5cc8ef3SRafael J. Wysocki 1026e5cc8ef3SRafael J. Wysocki static struct dev_pm_domain acpi_general_pm_domain = { 1027e5cc8ef3SRafael J. Wysocki .ops = { 1028e5cc8ef3SRafael J. Wysocki #ifdef CONFIG_PM_RUNTIME 1029e5cc8ef3SRafael J. Wysocki .runtime_suspend = acpi_subsys_runtime_suspend, 1030e5cc8ef3SRafael J. Wysocki .runtime_resume = acpi_subsys_runtime_resume, 1031e5cc8ef3SRafael J. Wysocki #endif 1032e5cc8ef3SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP 1033e5cc8ef3SRafael J. Wysocki .prepare = acpi_subsys_prepare, 1034f25c0ae2SRafael J. Wysocki .complete = acpi_subsys_complete, 103592858c47SRafael J. Wysocki .suspend = acpi_subsys_suspend, 1036e5cc8ef3SRafael J. Wysocki .suspend_late = acpi_subsys_suspend_late, 1037e5cc8ef3SRafael J. Wysocki .resume_early = acpi_subsys_resume_early, 103892858c47SRafael J. Wysocki .freeze = acpi_subsys_freeze, 103992858c47SRafael J. Wysocki .poweroff = acpi_subsys_suspend, 1040e5cc8ef3SRafael J. Wysocki .poweroff_late = acpi_subsys_suspend_late, 1041e5cc8ef3SRafael J. Wysocki .restore_early = acpi_subsys_resume_early, 1042e5cc8ef3SRafael J. Wysocki #endif 1043e5cc8ef3SRafael J. Wysocki }, 1044e5cc8ef3SRafael J. Wysocki }; 1045e5cc8ef3SRafael J. Wysocki 1046e5cc8ef3SRafael J. Wysocki /** 1047e5cc8ef3SRafael J. Wysocki * acpi_dev_pm_attach - Prepare device for ACPI power management. 1048e5cc8ef3SRafael J. Wysocki * @dev: Device to prepare. 1049b88ce2a4SRafael J. Wysocki * @power_on: Whether or not to power on the device. 1050e5cc8ef3SRafael J. Wysocki * 1051e5cc8ef3SRafael J. Wysocki * If @dev has a valid ACPI handle that has a valid struct acpi_device object 1052e5cc8ef3SRafael J. Wysocki * attached to it, install a wakeup notification handler for the device and 1053b88ce2a4SRafael J. Wysocki * add it to the general ACPI PM domain. If @power_on is set, the device will 1054b88ce2a4SRafael J. Wysocki * be put into the ACPI D0 state before the function returns. 1055e5cc8ef3SRafael J. Wysocki * 1056e5cc8ef3SRafael J. Wysocki * This assumes that the @dev's bus type uses generic power management callbacks 1057e5cc8ef3SRafael J. Wysocki * (or doesn't use any power management callbacks at all). 1058e5cc8ef3SRafael J. Wysocki * 1059e5cc8ef3SRafael J. Wysocki * Callers must ensure proper synchronization of this function with power 1060e5cc8ef3SRafael J. Wysocki * management callbacks. 1061e5cc8ef3SRafael J. Wysocki */ 1062b88ce2a4SRafael J. Wysocki int acpi_dev_pm_attach(struct device *dev, bool power_on) 1063e5cc8ef3SRafael J. Wysocki { 106479c0373fSRafael J. Wysocki struct acpi_device *adev = ACPI_COMPANION(dev); 1065e5cc8ef3SRafael J. Wysocki 1066e5cc8ef3SRafael J. Wysocki if (!adev) 1067e5cc8ef3SRafael J. Wysocki return -ENODEV; 1068e5cc8ef3SRafael J. Wysocki 1069e5cc8ef3SRafael J. Wysocki if (dev->pm_domain) 1070e5cc8ef3SRafael J. Wysocki return -EEXIST; 1071e5cc8ef3SRafael J. Wysocki 1072c072530fSRafael J. Wysocki acpi_add_pm_notifier(adev, dev, acpi_pm_notify_work_func); 1073e5cc8ef3SRafael J. Wysocki dev->pm_domain = &acpi_general_pm_domain; 1074b88ce2a4SRafael J. Wysocki if (power_on) { 1075b88ce2a4SRafael J. Wysocki acpi_dev_pm_full_power(adev); 1076*f35cec25SRafael J. Wysocki acpi_device_wakeup(adev, ACPI_STATE_S0, false); 1077b88ce2a4SRafael J. Wysocki } 1078e5cc8ef3SRafael J. Wysocki return 0; 1079e5cc8ef3SRafael J. Wysocki } 1080e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_pm_attach); 1081e5cc8ef3SRafael J. Wysocki 1082e5cc8ef3SRafael J. Wysocki /** 1083e5cc8ef3SRafael J. Wysocki * acpi_dev_pm_detach - Remove ACPI power management from the device. 1084e5cc8ef3SRafael J. Wysocki * @dev: Device to take care of. 1085b88ce2a4SRafael J. Wysocki * @power_off: Whether or not to try to remove power from the device. 1086e5cc8ef3SRafael J. Wysocki * 1087e5cc8ef3SRafael J. Wysocki * Remove the device from the general ACPI PM domain and remove its wakeup 1088b88ce2a4SRafael J. Wysocki * notifier. If @power_off is set, additionally remove power from the device if 1089b88ce2a4SRafael J. Wysocki * possible. 1090e5cc8ef3SRafael J. Wysocki * 1091e5cc8ef3SRafael J. Wysocki * Callers must ensure proper synchronization of this function with power 1092e5cc8ef3SRafael J. Wysocki * management callbacks. 1093e5cc8ef3SRafael J. Wysocki */ 1094b88ce2a4SRafael J. Wysocki void acpi_dev_pm_detach(struct device *dev, bool power_off) 1095e5cc8ef3SRafael J. Wysocki { 109679c0373fSRafael J. Wysocki struct acpi_device *adev = ACPI_COMPANION(dev); 1097e5cc8ef3SRafael J. Wysocki 1098e5cc8ef3SRafael J. Wysocki if (adev && dev->pm_domain == &acpi_general_pm_domain) { 1099e5cc8ef3SRafael J. Wysocki dev->pm_domain = NULL; 1100c072530fSRafael J. Wysocki acpi_remove_pm_notifier(adev); 1101b88ce2a4SRafael J. Wysocki if (power_off) { 1102b88ce2a4SRafael J. Wysocki /* 1103b88ce2a4SRafael J. Wysocki * If the device's PM QoS resume latency limit or flags 1104b88ce2a4SRafael J. Wysocki * have been exposed to user space, they have to be 1105b88ce2a4SRafael J. Wysocki * hidden at this point, so that they don't affect the 1106b88ce2a4SRafael J. Wysocki * choice of the low-power state to put the device into. 1107b88ce2a4SRafael J. Wysocki */ 1108b88ce2a4SRafael J. Wysocki dev_pm_qos_hide_latency_limit(dev); 1109b88ce2a4SRafael J. Wysocki dev_pm_qos_hide_flags(dev); 1110*f35cec25SRafael J. Wysocki acpi_device_wakeup(adev, ACPI_STATE_S0, false); 1111b88ce2a4SRafael J. Wysocki acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0); 1112b88ce2a4SRafael J. Wysocki } 1113e5cc8ef3SRafael J. Wysocki } 1114e5cc8ef3SRafael J. Wysocki } 1115e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_pm_detach); 1116ec4602a9SRafael J. Wysocki #endif /* CONFIG_PM */ 1117