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 25ec2cd81cSRafael J. Wysocki #include <linux/device.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 31ec2cd81cSRafael J. Wysocki #include <acpi/acpi.h> 32ec2cd81cSRafael J. Wysocki #include <acpi/acpi_bus.h> 339ce4e607SRafael J. Wysocki #include <acpi/acpi_drivers.h> 349ce4e607SRafael J. Wysocki 359ce4e607SRafael J. Wysocki #include "internal.h" 369ce4e607SRafael J. Wysocki 379ce4e607SRafael J. Wysocki #define _COMPONENT ACPI_POWER_COMPONENT 389ce4e607SRafael J. Wysocki ACPI_MODULE_NAME("device_pm"); 39ec2cd81cSRafael J. Wysocki 4086b3832cSRafael J. Wysocki /** 419ce4e607SRafael J. Wysocki * acpi_power_state_string - String representation of ACPI device power state. 429ce4e607SRafael J. Wysocki * @state: ACPI device power state to return the string representation of. 439ce4e607SRafael J. Wysocki */ 449ce4e607SRafael J. Wysocki const char *acpi_power_state_string(int state) 459ce4e607SRafael J. Wysocki { 469ce4e607SRafael J. Wysocki switch (state) { 479ce4e607SRafael J. Wysocki case ACPI_STATE_D0: 489ce4e607SRafael J. Wysocki return "D0"; 499ce4e607SRafael J. Wysocki case ACPI_STATE_D1: 509ce4e607SRafael J. Wysocki return "D1"; 519ce4e607SRafael J. Wysocki case ACPI_STATE_D2: 529ce4e607SRafael J. Wysocki return "D2"; 539ce4e607SRafael J. Wysocki case ACPI_STATE_D3_HOT: 549ce4e607SRafael J. Wysocki return "D3hot"; 559ce4e607SRafael J. Wysocki case ACPI_STATE_D3_COLD: 56898fee4fSRafael J. Wysocki return "D3cold"; 579ce4e607SRafael J. Wysocki default: 589ce4e607SRafael J. Wysocki return "(unknown)"; 599ce4e607SRafael J. Wysocki } 609ce4e607SRafael J. Wysocki } 619ce4e607SRafael J. Wysocki 629ce4e607SRafael J. Wysocki /** 639ce4e607SRafael J. Wysocki * acpi_device_get_power - Get power state of an ACPI device. 649ce4e607SRafael J. Wysocki * @device: Device to get the power state of. 659ce4e607SRafael J. Wysocki * @state: Place to store the power state of the device. 669ce4e607SRafael J. Wysocki * 679ce4e607SRafael J. Wysocki * This function does not update the device's power.state field, but it may 689ce4e607SRafael J. Wysocki * update its parent's power.state field (when the parent's power state is 699ce4e607SRafael J. Wysocki * unknown and the device's power state turns out to be D0). 709ce4e607SRafael J. Wysocki */ 719ce4e607SRafael J. Wysocki int acpi_device_get_power(struct acpi_device *device, int *state) 729ce4e607SRafael J. Wysocki { 739ce4e607SRafael J. Wysocki int result = ACPI_STATE_UNKNOWN; 749ce4e607SRafael J. Wysocki 759ce4e607SRafael J. Wysocki if (!device || !state) 769ce4e607SRafael J. Wysocki return -EINVAL; 779ce4e607SRafael J. Wysocki 789ce4e607SRafael J. Wysocki if (!device->flags.power_manageable) { 799ce4e607SRafael J. Wysocki /* TBD: Non-recursive algorithm for walking up hierarchy. */ 809ce4e607SRafael J. Wysocki *state = device->parent ? 819ce4e607SRafael J. Wysocki device->parent->power.state : ACPI_STATE_D0; 829ce4e607SRafael J. Wysocki goto out; 839ce4e607SRafael J. Wysocki } 849ce4e607SRafael J. Wysocki 859ce4e607SRafael J. Wysocki /* 8675eb2d13SRafael J. Wysocki * Get the device's power state from power resources settings and _PSC, 8775eb2d13SRafael J. Wysocki * if available. 889ce4e607SRafael J. Wysocki */ 8975eb2d13SRafael J. Wysocki if (device->power.flags.power_resources) { 909ce4e607SRafael J. Wysocki int error = acpi_power_get_inferred_state(device, &result); 919ce4e607SRafael J. Wysocki if (error) 929ce4e607SRafael J. Wysocki return error; 9375eb2d13SRafael J. Wysocki } 9475eb2d13SRafael J. Wysocki if (device->power.flags.explicit_get) { 9575eb2d13SRafael J. Wysocki acpi_handle handle = device->handle; 9675eb2d13SRafael J. Wysocki unsigned long long psc; 9775eb2d13SRafael J. Wysocki acpi_status status; 9875eb2d13SRafael J. Wysocki 9975eb2d13SRafael J. Wysocki status = acpi_evaluate_integer(handle, "_PSC", NULL, &psc); 10075eb2d13SRafael J. Wysocki if (ACPI_FAILURE(status)) 10175eb2d13SRafael J. Wysocki return -ENODEV; 10275eb2d13SRafael J. Wysocki 10375eb2d13SRafael J. Wysocki /* 10475eb2d13SRafael J. Wysocki * The power resources settings may indicate a power state 10575eb2d13SRafael J. Wysocki * shallower than the actual power state of the device. 10675eb2d13SRafael J. Wysocki * 10775eb2d13SRafael J. Wysocki * Moreover, on systems predating ACPI 4.0, if the device 10875eb2d13SRafael J. Wysocki * doesn't depend on any power resources and _PSC returns 3, 10975eb2d13SRafael J. Wysocki * that means "power off". We need to maintain compatibility 11075eb2d13SRafael J. Wysocki * with those systems. 11175eb2d13SRafael J. Wysocki */ 11275eb2d13SRafael J. Wysocki if (psc > result && psc < ACPI_STATE_D3_COLD) 11375eb2d13SRafael J. Wysocki result = psc; 11475eb2d13SRafael J. Wysocki else if (result == ACPI_STATE_UNKNOWN) 11575eb2d13SRafael J. Wysocki result = psc > ACPI_STATE_D2 ? ACPI_STATE_D3_COLD : psc; 1169ce4e607SRafael J. Wysocki } 1179ce4e607SRafael J. Wysocki 1189ce4e607SRafael J. Wysocki /* 1199ce4e607SRafael J. Wysocki * If we were unsure about the device parent's power state up to this 1209ce4e607SRafael J. Wysocki * point, the fact that the device is in D0 implies that the parent has 121*644f17adSMika Westerberg * to be in D0 too, except if ignore_parent is set. 1229ce4e607SRafael J. Wysocki */ 123*644f17adSMika Westerberg if (!device->power.flags.ignore_parent && device->parent 124*644f17adSMika Westerberg && device->parent->power.state == ACPI_STATE_UNKNOWN 1259ce4e607SRafael J. Wysocki && result == ACPI_STATE_D0) 1269ce4e607SRafael J. Wysocki device->parent->power.state = ACPI_STATE_D0; 1279ce4e607SRafael J. Wysocki 1289ce4e607SRafael J. Wysocki *state = result; 1299ce4e607SRafael J. Wysocki 1309ce4e607SRafael J. Wysocki out: 1319ce4e607SRafael J. Wysocki ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] power state is %s\n", 1329ce4e607SRafael J. Wysocki device->pnp.bus_id, acpi_power_state_string(*state))); 1339ce4e607SRafael J. Wysocki 1349ce4e607SRafael J. Wysocki return 0; 1359ce4e607SRafael J. Wysocki } 1369ce4e607SRafael J. Wysocki 1379c0f45e3SRafael J. Wysocki static int acpi_dev_pm_explicit_set(struct acpi_device *adev, int state) 1389c0f45e3SRafael J. Wysocki { 1399c0f45e3SRafael J. Wysocki if (adev->power.states[state].flags.explicit_set) { 1409c0f45e3SRafael J. Wysocki char method[5] = { '_', 'P', 'S', '0' + state, '\0' }; 1419c0f45e3SRafael J. Wysocki acpi_status status; 1429c0f45e3SRafael J. Wysocki 1439c0f45e3SRafael J. Wysocki status = acpi_evaluate_object(adev->handle, method, NULL, NULL); 1449c0f45e3SRafael J. Wysocki if (ACPI_FAILURE(status)) 1459c0f45e3SRafael J. Wysocki return -ENODEV; 1469c0f45e3SRafael J. Wysocki } 1479c0f45e3SRafael J. Wysocki return 0; 1489c0f45e3SRafael J. Wysocki } 1499c0f45e3SRafael J. Wysocki 1509ce4e607SRafael J. Wysocki /** 1519ce4e607SRafael J. Wysocki * acpi_device_set_power - Set power state of an ACPI device. 1529ce4e607SRafael J. Wysocki * @device: Device to set the power state of. 1539ce4e607SRafael J. Wysocki * @state: New power state to set. 1549ce4e607SRafael J. Wysocki * 1559ce4e607SRafael J. Wysocki * Callers must ensure that the device is power manageable before using this 1569ce4e607SRafael J. Wysocki * function. 1579ce4e607SRafael J. Wysocki */ 1589ce4e607SRafael J. Wysocki int acpi_device_set_power(struct acpi_device *device, int state) 1599ce4e607SRafael J. Wysocki { 1609ce4e607SRafael J. Wysocki int result = 0; 1619ce4e607SRafael J. Wysocki bool cut_power = false; 1629ce4e607SRafael J. Wysocki 1632c7d132aSRafael J. Wysocki if (!device || !device->flags.power_manageable 1642c7d132aSRafael J. Wysocki || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD)) 1659ce4e607SRafael J. Wysocki return -EINVAL; 1669ce4e607SRafael J. Wysocki 1679ce4e607SRafael J. Wysocki /* Make sure this is a valid target state */ 1689ce4e607SRafael J. Wysocki 1699ce4e607SRafael J. Wysocki if (state == device->power.state) { 170b69137a7SRafael J. Wysocki ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] already in %s\n", 171b69137a7SRafael J. Wysocki device->pnp.bus_id, 1729ce4e607SRafael J. Wysocki acpi_power_state_string(state))); 1739ce4e607SRafael J. Wysocki return 0; 1749ce4e607SRafael J. Wysocki } 1759ce4e607SRafael J. Wysocki 1769ce4e607SRafael J. Wysocki if (!device->power.states[state].flags.valid) { 177b69137a7SRafael J. Wysocki dev_warn(&device->dev, "Power state %s not supported\n", 1789ce4e607SRafael J. Wysocki acpi_power_state_string(state)); 1799ce4e607SRafael J. Wysocki return -ENODEV; 1809ce4e607SRafael J. Wysocki } 181*644f17adSMika Westerberg if (!device->power.flags.ignore_parent && 182*644f17adSMika Westerberg device->parent && (state < device->parent->power.state)) { 183b69137a7SRafael J. Wysocki dev_warn(&device->dev, 184593298e6SAaron Lu "Cannot transition to power state %s for parent in %s\n", 185593298e6SAaron Lu acpi_power_state_string(state), 186593298e6SAaron Lu acpi_power_state_string(device->parent->power.state)); 1879ce4e607SRafael J. Wysocki return -ENODEV; 1889ce4e607SRafael J. Wysocki } 1899ce4e607SRafael J. Wysocki 1909ce4e607SRafael J. Wysocki /* For D3cold we should first transition into D3hot. */ 1919ce4e607SRafael J. Wysocki if (state == ACPI_STATE_D3_COLD 1929ce4e607SRafael J. Wysocki && device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible) { 1939ce4e607SRafael J. Wysocki state = ACPI_STATE_D3_HOT; 1949ce4e607SRafael J. Wysocki cut_power = true; 1959ce4e607SRafael J. Wysocki } 1969ce4e607SRafael J. Wysocki 197e78adb75SRafael J. Wysocki if (state < device->power.state && state != ACPI_STATE_D0 198e78adb75SRafael J. Wysocki && device->power.state >= ACPI_STATE_D3_HOT) { 199b69137a7SRafael J. Wysocki dev_warn(&device->dev, 2009ce4e607SRafael J. Wysocki "Cannot transition to non-D0 state from D3\n"); 2019ce4e607SRafael J. Wysocki return -ENODEV; 2029ce4e607SRafael J. Wysocki } 2039c0f45e3SRafael J. Wysocki 204e78adb75SRafael J. Wysocki /* 205e78adb75SRafael J. Wysocki * Transition Power 206e78adb75SRafael J. Wysocki * ---------------- 207e78adb75SRafael J. Wysocki * In accordance with the ACPI specification first apply power (via 208e78adb75SRafael J. Wysocki * power resources) and then evalute _PSx. 209e78adb75SRafael J. Wysocki */ 2109ce4e607SRafael J. Wysocki if (device->power.flags.power_resources) { 2119ce4e607SRafael J. Wysocki result = acpi_power_transition(device, state); 2129ce4e607SRafael J. Wysocki if (result) 2139ce4e607SRafael J. Wysocki goto end; 2149ce4e607SRafael J. Wysocki } 215e78adb75SRafael J. Wysocki result = acpi_dev_pm_explicit_set(device, state); 216e78adb75SRafael J. Wysocki if (result) 217e78adb75SRafael J. Wysocki goto end; 2189ce4e607SRafael J. Wysocki 219e5656271SRafael J. Wysocki if (cut_power) { 220e5656271SRafael J. Wysocki device->power.state = state; 221e5656271SRafael J. Wysocki state = ACPI_STATE_D3_COLD; 222e5656271SRafael J. Wysocki result = acpi_power_transition(device, state); 223e5656271SRafael J. Wysocki } 2249ce4e607SRafael J. Wysocki 2259ce4e607SRafael J. Wysocki end: 226e78adb75SRafael J. Wysocki if (result) { 227b69137a7SRafael J. Wysocki dev_warn(&device->dev, "Failed to change power state to %s\n", 2289ce4e607SRafael J. Wysocki acpi_power_state_string(state)); 229e78adb75SRafael J. Wysocki } else { 2309ce4e607SRafael J. Wysocki device->power.state = state; 2319ce4e607SRafael J. Wysocki ACPI_DEBUG_PRINT((ACPI_DB_INFO, 2329ce4e607SRafael J. Wysocki "Device [%s] transitioned to %s\n", 2339ce4e607SRafael J. Wysocki device->pnp.bus_id, 2349ce4e607SRafael J. Wysocki acpi_power_state_string(state))); 2359ce4e607SRafael J. Wysocki } 2369ce4e607SRafael J. Wysocki 2379ce4e607SRafael J. Wysocki return result; 2389ce4e607SRafael J. Wysocki } 2399ce4e607SRafael J. Wysocki EXPORT_SYMBOL(acpi_device_set_power); 2409ce4e607SRafael J. Wysocki 2419ce4e607SRafael J. Wysocki int acpi_bus_set_power(acpi_handle handle, int state) 2429ce4e607SRafael J. Wysocki { 2439ce4e607SRafael J. Wysocki struct acpi_device *device; 2449ce4e607SRafael J. Wysocki int result; 2459ce4e607SRafael J. Wysocki 2469ce4e607SRafael J. Wysocki result = acpi_bus_get_device(handle, &device); 2479ce4e607SRafael J. Wysocki if (result) 2489ce4e607SRafael J. Wysocki return result; 2499ce4e607SRafael J. Wysocki 2509ce4e607SRafael J. Wysocki return acpi_device_set_power(device, state); 2519ce4e607SRafael J. Wysocki } 2529ce4e607SRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_set_power); 2539ce4e607SRafael J. Wysocki 2549ce4e607SRafael J. Wysocki int acpi_bus_init_power(struct acpi_device *device) 2559ce4e607SRafael J. Wysocki { 2569ce4e607SRafael J. Wysocki int state; 2579ce4e607SRafael J. Wysocki int result; 2589ce4e607SRafael J. Wysocki 2599ce4e607SRafael J. Wysocki if (!device) 2609ce4e607SRafael J. Wysocki return -EINVAL; 2619ce4e607SRafael J. Wysocki 2629ce4e607SRafael J. Wysocki device->power.state = ACPI_STATE_UNKNOWN; 2639ce4e607SRafael J. Wysocki 2649ce4e607SRafael J. Wysocki result = acpi_device_get_power(device, &state); 2659ce4e607SRafael J. Wysocki if (result) 2669ce4e607SRafael J. Wysocki return result; 2679ce4e607SRafael J. Wysocki 268a2367807SRafael J. Wysocki if (state < ACPI_STATE_D3_COLD && device->power.flags.power_resources) { 2699ce4e607SRafael J. Wysocki result = acpi_power_on_resources(device, state); 270a2367807SRafael J. Wysocki if (result) 2719ce4e607SRafael J. Wysocki return result; 272a2367807SRafael J. Wysocki 2739c0f45e3SRafael J. Wysocki result = acpi_dev_pm_explicit_set(device, state); 2749c0f45e3SRafael J. Wysocki if (result) 2759c0f45e3SRafael J. Wysocki return result; 276b3785492SRafael J. Wysocki } else if (state == ACPI_STATE_UNKNOWN) { 2777cd8407dSRafael J. Wysocki /* 2787cd8407dSRafael J. Wysocki * No power resources and missing _PSC? Cross fingers and make 2797cd8407dSRafael J. Wysocki * it D0 in hope that this is what the BIOS put the device into. 2807cd8407dSRafael J. Wysocki * [We tried to force D0 here by executing _PS0, but that broke 2817cd8407dSRafael J. Wysocki * Toshiba P870-303 in a nasty way.] 2827cd8407dSRafael J. Wysocki */ 283b3785492SRafael J. Wysocki state = ACPI_STATE_D0; 284a2367807SRafael J. Wysocki } 285a2367807SRafael J. Wysocki device->power.state = state; 286a2367807SRafael J. Wysocki return 0; 2879ce4e607SRafael J. Wysocki } 2889ce4e607SRafael J. Wysocki 289b9e95fc6SRafael J. Wysocki /** 290b9e95fc6SRafael J. Wysocki * acpi_device_fix_up_power - Force device with missing _PSC into D0. 291b9e95fc6SRafael J. Wysocki * @device: Device object whose power state is to be fixed up. 292b9e95fc6SRafael J. Wysocki * 293b9e95fc6SRafael J. Wysocki * Devices without power resources and _PSC, but having _PS0 and _PS3 defined, 294b9e95fc6SRafael J. Wysocki * are assumed to be put into D0 by the BIOS. However, in some cases that may 295b9e95fc6SRafael J. Wysocki * not be the case and this function should be used then. 296b9e95fc6SRafael J. Wysocki */ 297b9e95fc6SRafael J. Wysocki int acpi_device_fix_up_power(struct acpi_device *device) 298b9e95fc6SRafael J. Wysocki { 299b9e95fc6SRafael J. Wysocki int ret = 0; 300b9e95fc6SRafael J. Wysocki 301b9e95fc6SRafael J. Wysocki if (!device->power.flags.power_resources 302b9e95fc6SRafael J. Wysocki && !device->power.flags.explicit_get 303b9e95fc6SRafael J. Wysocki && device->power.state == ACPI_STATE_D0) 304b9e95fc6SRafael J. Wysocki ret = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0); 305b9e95fc6SRafael J. Wysocki 306b9e95fc6SRafael J. Wysocki return ret; 307b9e95fc6SRafael J. Wysocki } 308b9e95fc6SRafael J. Wysocki 3099ce4e607SRafael J. Wysocki int acpi_bus_update_power(acpi_handle handle, int *state_p) 3109ce4e607SRafael J. Wysocki { 3119ce4e607SRafael J. Wysocki struct acpi_device *device; 3129ce4e607SRafael J. Wysocki int state; 3139ce4e607SRafael J. Wysocki int result; 3149ce4e607SRafael J. Wysocki 3159ce4e607SRafael J. Wysocki result = acpi_bus_get_device(handle, &device); 3169ce4e607SRafael J. Wysocki if (result) 3179ce4e607SRafael J. Wysocki return result; 3189ce4e607SRafael J. Wysocki 3199ce4e607SRafael J. Wysocki result = acpi_device_get_power(device, &state); 3209ce4e607SRafael J. Wysocki if (result) 3219ce4e607SRafael J. Wysocki return result; 3229ce4e607SRafael J. Wysocki 32391bdad0bSRafael J. Wysocki if (state == ACPI_STATE_UNKNOWN) { 324511d5c42SRafael J. Wysocki state = ACPI_STATE_D0; 3259ce4e607SRafael J. Wysocki result = acpi_device_set_power(device, state); 32691bdad0bSRafael J. Wysocki if (result) 32791bdad0bSRafael J. Wysocki return result; 32891bdad0bSRafael J. Wysocki } else { 32991bdad0bSRafael J. Wysocki if (device->power.flags.power_resources) { 33091bdad0bSRafael J. Wysocki /* 33191bdad0bSRafael J. Wysocki * We don't need to really switch the state, bu we need 33291bdad0bSRafael J. Wysocki * to update the power resources' reference counters. 33391bdad0bSRafael J. Wysocki */ 33491bdad0bSRafael J. Wysocki result = acpi_power_transition(device, state); 33591bdad0bSRafael J. Wysocki if (result) 33691bdad0bSRafael J. Wysocki return result; 33791bdad0bSRafael J. Wysocki } 33891bdad0bSRafael J. Wysocki device->power.state = state; 33991bdad0bSRafael J. Wysocki } 34091bdad0bSRafael J. Wysocki if (state_p) 3419ce4e607SRafael J. Wysocki *state_p = state; 3429ce4e607SRafael J. Wysocki 34391bdad0bSRafael J. Wysocki return 0; 3449ce4e607SRafael J. Wysocki } 3459ce4e607SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_bus_update_power); 3469ce4e607SRafael J. Wysocki 3479ce4e607SRafael J. Wysocki bool acpi_bus_power_manageable(acpi_handle handle) 3489ce4e607SRafael J. Wysocki { 3499ce4e607SRafael J. Wysocki struct acpi_device *device; 3509ce4e607SRafael J. Wysocki int result; 3519ce4e607SRafael J. Wysocki 3529ce4e607SRafael J. Wysocki result = acpi_bus_get_device(handle, &device); 3539ce4e607SRafael J. Wysocki return result ? false : device->flags.power_manageable; 3549ce4e607SRafael J. Wysocki } 3559ce4e607SRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_power_manageable); 3569ce4e607SRafael J. Wysocki 357ec4602a9SRafael J. Wysocki #ifdef CONFIG_PM 358ec4602a9SRafael J. Wysocki static DEFINE_MUTEX(acpi_pm_notifier_lock); 359ec4602a9SRafael J. Wysocki 360ec4602a9SRafael J. Wysocki /** 361ec4602a9SRafael J. Wysocki * acpi_add_pm_notifier - Register PM notifier for given ACPI device. 362ec4602a9SRafael J. Wysocki * @adev: ACPI device to add the notifier for. 363ec4602a9SRafael J. Wysocki * @context: Context information to pass to the notifier routine. 364ec4602a9SRafael J. Wysocki * 365ec4602a9SRafael J. Wysocki * NOTE: @adev need not be a run-wake or wakeup device to be a valid source of 366ec4602a9SRafael J. Wysocki * PM wakeup events. For example, wakeup events may be generated for bridges 367ec4602a9SRafael J. Wysocki * if one of the devices below the bridge is signaling wakeup, even if the 368ec4602a9SRafael J. Wysocki * bridge itself doesn't have a wakeup GPE associated with it. 369ec4602a9SRafael J. Wysocki */ 370ec4602a9SRafael J. Wysocki acpi_status acpi_add_pm_notifier(struct acpi_device *adev, 371ec4602a9SRafael J. Wysocki acpi_notify_handler handler, void *context) 372ec4602a9SRafael J. Wysocki { 373ec4602a9SRafael J. Wysocki acpi_status status = AE_ALREADY_EXISTS; 374ec4602a9SRafael J. Wysocki 375ec4602a9SRafael J. Wysocki mutex_lock(&acpi_pm_notifier_lock); 376ec4602a9SRafael J. Wysocki 377ec4602a9SRafael J. Wysocki if (adev->wakeup.flags.notifier_present) 378ec4602a9SRafael J. Wysocki goto out; 379ec4602a9SRafael J. Wysocki 380ec4602a9SRafael J. Wysocki status = acpi_install_notify_handler(adev->handle, 381ec4602a9SRafael J. Wysocki ACPI_SYSTEM_NOTIFY, 382ec4602a9SRafael J. Wysocki handler, context); 383ec4602a9SRafael J. Wysocki if (ACPI_FAILURE(status)) 384ec4602a9SRafael J. Wysocki goto out; 385ec4602a9SRafael J. Wysocki 386ec4602a9SRafael J. Wysocki adev->wakeup.flags.notifier_present = true; 387ec4602a9SRafael J. Wysocki 388ec4602a9SRafael J. Wysocki out: 389ec4602a9SRafael J. Wysocki mutex_unlock(&acpi_pm_notifier_lock); 390ec4602a9SRafael J. Wysocki return status; 391ec4602a9SRafael J. Wysocki } 392ec4602a9SRafael J. Wysocki 393ec4602a9SRafael J. Wysocki /** 394ec4602a9SRafael J. Wysocki * acpi_remove_pm_notifier - Unregister PM notifier from given ACPI device. 395ec4602a9SRafael J. Wysocki * @adev: ACPI device to remove the notifier from. 396ec4602a9SRafael J. Wysocki */ 397ec4602a9SRafael J. Wysocki acpi_status acpi_remove_pm_notifier(struct acpi_device *adev, 398ec4602a9SRafael J. Wysocki acpi_notify_handler handler) 399ec4602a9SRafael J. Wysocki { 400ec4602a9SRafael J. Wysocki acpi_status status = AE_BAD_PARAMETER; 401ec4602a9SRafael J. Wysocki 402ec4602a9SRafael J. Wysocki mutex_lock(&acpi_pm_notifier_lock); 403ec4602a9SRafael J. Wysocki 404ec4602a9SRafael J. Wysocki if (!adev->wakeup.flags.notifier_present) 405ec4602a9SRafael J. Wysocki goto out; 406ec4602a9SRafael J. Wysocki 407ec4602a9SRafael J. Wysocki status = acpi_remove_notify_handler(adev->handle, 408ec4602a9SRafael J. Wysocki ACPI_SYSTEM_NOTIFY, 409ec4602a9SRafael J. Wysocki handler); 410ec4602a9SRafael J. Wysocki if (ACPI_FAILURE(status)) 411ec4602a9SRafael J. Wysocki goto out; 412ec4602a9SRafael J. Wysocki 413ec4602a9SRafael J. Wysocki adev->wakeup.flags.notifier_present = false; 414ec4602a9SRafael J. Wysocki 415ec4602a9SRafael J. Wysocki out: 416ec4602a9SRafael J. Wysocki mutex_unlock(&acpi_pm_notifier_lock); 417ec4602a9SRafael J. Wysocki return status; 418ec4602a9SRafael J. Wysocki } 419ec4602a9SRafael J. Wysocki 4209ce4e607SRafael J. Wysocki bool acpi_bus_can_wakeup(acpi_handle handle) 4219ce4e607SRafael J. Wysocki { 4229ce4e607SRafael J. Wysocki struct acpi_device *device; 4239ce4e607SRafael J. Wysocki int result; 4249ce4e607SRafael J. Wysocki 4259ce4e607SRafael J. Wysocki result = acpi_bus_get_device(handle, &device); 4269ce4e607SRafael J. Wysocki return result ? false : device->wakeup.flags.valid; 4279ce4e607SRafael J. Wysocki } 4289ce4e607SRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_can_wakeup); 4299ce4e607SRafael J. Wysocki 4309ce4e607SRafael J. Wysocki /** 431b25c77efSRafael J. Wysocki * acpi_dev_pm_get_state - Get preferred power state of ACPI device. 43286b3832cSRafael J. Wysocki * @dev: Device whose preferred target power state to return. 43386b3832cSRafael J. Wysocki * @adev: ACPI device node corresponding to @dev. 43486b3832cSRafael J. Wysocki * @target_state: System state to match the resultant device state. 435fa1675b5SRafael J. Wysocki * @d_min_p: Location to store the highest power state available to the device. 436fa1675b5SRafael J. Wysocki * @d_max_p: Location to store the lowest power state available to the device. 43786b3832cSRafael J. Wysocki * 438fa1675b5SRafael J. Wysocki * Find the lowest power (highest number) and highest power (lowest number) ACPI 439fa1675b5SRafael J. Wysocki * device power states that the device can be in while the system is in the 440fa1675b5SRafael J. Wysocki * state represented by @target_state. Store the integer numbers representing 441fa1675b5SRafael J. Wysocki * those stats in the memory locations pointed to by @d_max_p and @d_min_p, 442fa1675b5SRafael J. Wysocki * respectively. 44386b3832cSRafael J. Wysocki * 44486b3832cSRafael J. Wysocki * Callers must ensure that @dev and @adev are valid pointers and that @adev 44586b3832cSRafael J. Wysocki * actually corresponds to @dev before using this function. 446fa1675b5SRafael J. Wysocki * 447fa1675b5SRafael J. Wysocki * Returns 0 on success or -ENODATA when one of the ACPI methods fails or 448fa1675b5SRafael J. Wysocki * returns a value that doesn't make sense. The memory locations pointed to by 449fa1675b5SRafael J. Wysocki * @d_max_p and @d_min_p are only modified on success. 45086b3832cSRafael J. Wysocki */ 451b25c77efSRafael J. Wysocki static int acpi_dev_pm_get_state(struct device *dev, struct acpi_device *adev, 452fa1675b5SRafael J. Wysocki u32 target_state, int *d_min_p, int *d_max_p) 45386b3832cSRafael J. Wysocki { 454fa1675b5SRafael J. Wysocki char method[] = { '_', 'S', '0' + target_state, 'D', '\0' }; 455fa1675b5SRafael J. Wysocki acpi_handle handle = adev->handle; 456fa1675b5SRafael J. Wysocki unsigned long long ret; 457fa1675b5SRafael J. Wysocki int d_min, d_max; 45886b3832cSRafael J. Wysocki bool wakeup = false; 459fa1675b5SRafael J. Wysocki acpi_status status; 46086b3832cSRafael J. Wysocki 46186b3832cSRafael J. Wysocki /* 462fa1675b5SRafael J. Wysocki * If the system state is S0, the lowest power state the device can be 463fa1675b5SRafael J. Wysocki * in is D3cold, unless the device has _S0W and is supposed to signal 464fa1675b5SRafael J. Wysocki * wakeup, in which case the return value of _S0W has to be used as the 465fa1675b5SRafael J. Wysocki * lowest power state available to the device. 46686b3832cSRafael J. Wysocki */ 46786b3832cSRafael J. Wysocki d_min = ACPI_STATE_D0; 4684c164ae7SRafael J. Wysocki d_max = ACPI_STATE_D3_COLD; 46986b3832cSRafael J. Wysocki 47086b3832cSRafael J. Wysocki /* 47186b3832cSRafael J. Wysocki * If present, _SxD methods return the minimum D-state (highest power 47286b3832cSRafael J. Wysocki * state) we can use for the corresponding S-states. Otherwise, the 47386b3832cSRafael J. Wysocki * minimum D-state is D0 (ACPI 3.x). 47486b3832cSRafael J. Wysocki */ 47586b3832cSRafael J. Wysocki if (target_state > ACPI_STATE_S0) { 476fa1675b5SRafael J. Wysocki /* 477fa1675b5SRafael J. Wysocki * We rely on acpi_evaluate_integer() not clobbering the integer 478fa1675b5SRafael J. Wysocki * provided if AE_NOT_FOUND is returned. 479fa1675b5SRafael J. Wysocki */ 480fa1675b5SRafael J. Wysocki ret = d_min; 481fa1675b5SRafael J. Wysocki status = acpi_evaluate_integer(handle, method, NULL, &ret); 482fa1675b5SRafael J. Wysocki if ((ACPI_FAILURE(status) && status != AE_NOT_FOUND) 483fa1675b5SRafael J. Wysocki || ret > ACPI_STATE_D3_COLD) 484fa1675b5SRafael J. Wysocki return -ENODATA; 485fa1675b5SRafael J. Wysocki 486fa1675b5SRafael J. Wysocki /* 487fa1675b5SRafael J. Wysocki * We need to handle legacy systems where D3hot and D3cold are 488fa1675b5SRafael J. Wysocki * the same and 3 is returned in both cases, so fall back to 489fa1675b5SRafael J. Wysocki * D3cold if D3hot is not a valid state. 490fa1675b5SRafael J. Wysocki */ 491fa1675b5SRafael J. Wysocki if (!adev->power.states[ret].flags.valid) { 492fa1675b5SRafael J. Wysocki if (ret == ACPI_STATE_D3_HOT) 493fa1675b5SRafael J. Wysocki ret = ACPI_STATE_D3_COLD; 494fa1675b5SRafael J. Wysocki else 495fa1675b5SRafael J. Wysocki return -ENODATA; 496fa1675b5SRafael J. Wysocki } 497fa1675b5SRafael J. Wysocki d_min = ret; 49886b3832cSRafael J. Wysocki wakeup = device_may_wakeup(dev) && adev->wakeup.flags.valid 49986b3832cSRafael J. Wysocki && adev->wakeup.sleep_state >= target_state; 50086b3832cSRafael J. Wysocki } else if (dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) != 50186b3832cSRafael J. Wysocki PM_QOS_FLAGS_NONE) { 50286b3832cSRafael J. Wysocki wakeup = adev->wakeup.flags.valid; 50386b3832cSRafael J. Wysocki } 50486b3832cSRafael J. Wysocki 50586b3832cSRafael J. Wysocki /* 50686b3832cSRafael J. Wysocki * If _PRW says we can wake up the system from the target sleep state, 50786b3832cSRafael J. Wysocki * the D-state returned by _SxD is sufficient for that (we assume a 50886b3832cSRafael J. Wysocki * wakeup-aware driver if wake is set). Still, if _SxW exists 50986b3832cSRafael J. Wysocki * (ACPI 3.x), it should return the maximum (lowest power) D-state that 51086b3832cSRafael J. Wysocki * can wake the system. _S0W may be valid, too. 51186b3832cSRafael J. Wysocki */ 51286b3832cSRafael J. Wysocki if (wakeup) { 513fa1675b5SRafael J. Wysocki method[3] = 'W'; 514fa1675b5SRafael J. Wysocki status = acpi_evaluate_integer(handle, method, NULL, &ret); 515fa1675b5SRafael J. Wysocki if (status == AE_NOT_FOUND) { 516fa1675b5SRafael J. Wysocki if (target_state > ACPI_STATE_S0) 51786b3832cSRafael J. Wysocki d_max = d_min; 518fa1675b5SRafael J. Wysocki } else if (ACPI_SUCCESS(status) && ret <= ACPI_STATE_D3_COLD) { 519fa1675b5SRafael J. Wysocki /* Fall back to D3cold if ret is not a valid state. */ 520fa1675b5SRafael J. Wysocki if (!adev->power.states[ret].flags.valid) 521fa1675b5SRafael J. Wysocki ret = ACPI_STATE_D3_COLD; 522fa1675b5SRafael J. Wysocki 523fa1675b5SRafael J. Wysocki d_max = ret > d_min ? ret : d_min; 524fa1675b5SRafael J. Wysocki } else { 525fa1675b5SRafael J. Wysocki return -ENODATA; 52686b3832cSRafael J. Wysocki } 52786b3832cSRafael J. Wysocki } 52886b3832cSRafael J. Wysocki 52986b3832cSRafael J. Wysocki if (d_min_p) 53086b3832cSRafael J. Wysocki *d_min_p = d_min; 531fa1675b5SRafael J. Wysocki 532fa1675b5SRafael J. Wysocki if (d_max_p) 533fa1675b5SRafael J. Wysocki *d_max_p = d_max; 534fa1675b5SRafael J. Wysocki 535fa1675b5SRafael J. Wysocki return 0; 53686b3832cSRafael J. Wysocki } 537cd7bd02dSRafael J. Wysocki 538a6ae7594SRafael J. Wysocki /** 539a6ae7594SRafael J. Wysocki * acpi_pm_device_sleep_state - Get preferred power state of ACPI device. 540a6ae7594SRafael J. Wysocki * @dev: Device whose preferred target power state to return. 541a6ae7594SRafael J. Wysocki * @d_min_p: Location to store the upper limit of the allowed states range. 542a6ae7594SRafael J. Wysocki * @d_max_in: Deepest low-power state to take into consideration. 543a6ae7594SRafael J. Wysocki * Return value: Preferred power state of the device on success, -ENODEV 544fa1675b5SRafael J. Wysocki * if there's no 'struct acpi_device' for @dev, -EINVAL if @d_max_in is 545fa1675b5SRafael J. Wysocki * incorrect, or -ENODATA on ACPI method failure. 546a6ae7594SRafael J. Wysocki * 547a6ae7594SRafael J. Wysocki * The caller must ensure that @dev is valid before using this function. 548a6ae7594SRafael J. Wysocki */ 549a6ae7594SRafael J. Wysocki int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in) 550a6ae7594SRafael J. Wysocki { 551a6ae7594SRafael J. Wysocki acpi_handle handle = DEVICE_ACPI_HANDLE(dev); 552a6ae7594SRafael J. Wysocki struct acpi_device *adev; 5539b5c7a5aSRafael J. Wysocki int ret, d_min, d_max; 554fa1675b5SRafael J. Wysocki 555fa1675b5SRafael J. Wysocki if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3_COLD) 556fa1675b5SRafael J. Wysocki return -EINVAL; 557fa1675b5SRafael J. Wysocki 558fa1675b5SRafael J. Wysocki if (d_max_in > ACPI_STATE_D3_HOT) { 559fa1675b5SRafael J. Wysocki enum pm_qos_flags_status stat; 560fa1675b5SRafael J. Wysocki 561fa1675b5SRafael J. Wysocki stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF); 562fa1675b5SRafael J. Wysocki if (stat == PM_QOS_FLAGS_ALL) 563fa1675b5SRafael J. Wysocki d_max_in = ACPI_STATE_D3_HOT; 564fa1675b5SRafael J. Wysocki } 565a6ae7594SRafael J. Wysocki 566dde3bb41SYasuaki Ishimatsu if (!handle || acpi_bus_get_device(handle, &adev)) { 567a6ae7594SRafael J. Wysocki dev_dbg(dev, "ACPI handle without context in %s!\n", __func__); 568a6ae7594SRafael J. Wysocki return -ENODEV; 569a6ae7594SRafael J. Wysocki } 570a6ae7594SRafael J. Wysocki 571fa1675b5SRafael J. Wysocki ret = acpi_dev_pm_get_state(dev, adev, acpi_target_system_state(), 5729b5c7a5aSRafael J. Wysocki &d_min, &d_max); 573fa1675b5SRafael J. Wysocki if (ret) 574fa1675b5SRafael J. Wysocki return ret; 575fa1675b5SRafael J. Wysocki 5769b5c7a5aSRafael J. Wysocki if (d_max_in < d_min) 577fa1675b5SRafael J. Wysocki return -EINVAL; 578fa1675b5SRafael J. Wysocki 579fa1675b5SRafael J. Wysocki if (d_max > d_max_in) { 5809b5c7a5aSRafael J. Wysocki for (d_max = d_max_in; d_max > d_min; d_max--) { 581fa1675b5SRafael J. Wysocki if (adev->power.states[d_max].flags.valid) 582fa1675b5SRafael J. Wysocki break; 583fa1675b5SRafael J. Wysocki } 584fa1675b5SRafael J. Wysocki } 5859b5c7a5aSRafael J. Wysocki 5869b5c7a5aSRafael J. Wysocki if (d_min_p) 5879b5c7a5aSRafael J. Wysocki *d_min_p = d_min; 5889b5c7a5aSRafael J. Wysocki 589fa1675b5SRafael J. Wysocki return d_max; 590a6ae7594SRafael J. Wysocki } 591a6ae7594SRafael J. Wysocki EXPORT_SYMBOL(acpi_pm_device_sleep_state); 592a6ae7594SRafael J. Wysocki 593cd7bd02dSRafael J. Wysocki #ifdef CONFIG_PM_RUNTIME 594cd7bd02dSRafael J. Wysocki /** 595e5cc8ef3SRafael J. Wysocki * acpi_wakeup_device - Wakeup notification handler for ACPI devices. 596e5cc8ef3SRafael J. Wysocki * @handle: ACPI handle of the device the notification is for. 597e5cc8ef3SRafael J. Wysocki * @event: Type of the signaled event. 598e5cc8ef3SRafael J. Wysocki * @context: Device corresponding to @handle. 599e5cc8ef3SRafael J. Wysocki */ 600e5cc8ef3SRafael J. Wysocki static void acpi_wakeup_device(acpi_handle handle, u32 event, void *context) 601e5cc8ef3SRafael J. Wysocki { 602e5cc8ef3SRafael J. Wysocki struct device *dev = context; 603e5cc8ef3SRafael J. Wysocki 604e5cc8ef3SRafael J. Wysocki if (event == ACPI_NOTIFY_DEVICE_WAKE && dev) { 605e5cc8ef3SRafael J. Wysocki pm_wakeup_event(dev, 0); 606e5cc8ef3SRafael J. Wysocki pm_runtime_resume(dev); 607e5cc8ef3SRafael J. Wysocki } 608e5cc8ef3SRafael J. Wysocki } 609e5cc8ef3SRafael J. Wysocki 610e5cc8ef3SRafael J. Wysocki /** 611dee8370cSRafael J. Wysocki * __acpi_device_run_wake - Enable/disable runtime remote wakeup for device. 612dee8370cSRafael J. Wysocki * @adev: ACPI device to enable/disable the remote wakeup for. 613cd7bd02dSRafael J. Wysocki * @enable: Whether to enable or disable the wakeup functionality. 614cd7bd02dSRafael J. Wysocki * 615dee8370cSRafael J. Wysocki * Enable/disable the GPE associated with @adev so that it can generate 616dee8370cSRafael J. Wysocki * wakeup signals for the device in response to external (remote) events and 617dee8370cSRafael J. Wysocki * enable/disable device wakeup power. 618dee8370cSRafael J. Wysocki * 619dee8370cSRafael J. Wysocki * Callers must ensure that @adev is a valid ACPI device node before executing 620dee8370cSRafael J. Wysocki * this function. 621dee8370cSRafael J. Wysocki */ 622dee8370cSRafael J. Wysocki int __acpi_device_run_wake(struct acpi_device *adev, bool enable) 623dee8370cSRafael J. Wysocki { 624dee8370cSRafael J. Wysocki struct acpi_device_wakeup *wakeup = &adev->wakeup; 625dee8370cSRafael J. Wysocki 626dee8370cSRafael J. Wysocki if (enable) { 627dee8370cSRafael J. Wysocki acpi_status res; 628dee8370cSRafael J. Wysocki int error; 629dee8370cSRafael J. Wysocki 630dee8370cSRafael J. Wysocki error = acpi_enable_wakeup_device_power(adev, ACPI_STATE_S0); 631dee8370cSRafael J. Wysocki if (error) 632dee8370cSRafael J. Wysocki return error; 633dee8370cSRafael J. Wysocki 634dee8370cSRafael J. Wysocki res = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number); 635dee8370cSRafael J. Wysocki if (ACPI_FAILURE(res)) { 636dee8370cSRafael J. Wysocki acpi_disable_wakeup_device_power(adev); 637dee8370cSRafael J. Wysocki return -EIO; 638dee8370cSRafael J. Wysocki } 639dee8370cSRafael J. Wysocki } else { 640dee8370cSRafael J. Wysocki acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number); 641dee8370cSRafael J. Wysocki acpi_disable_wakeup_device_power(adev); 642dee8370cSRafael J. Wysocki } 643dee8370cSRafael J. Wysocki return 0; 644dee8370cSRafael J. Wysocki } 645dee8370cSRafael J. Wysocki 646dee8370cSRafael J. Wysocki /** 647dee8370cSRafael J. Wysocki * acpi_pm_device_run_wake - Enable/disable remote wakeup for given device. 648dee8370cSRafael J. Wysocki * @dev: Device to enable/disable the platform to wake up. 649dee8370cSRafael J. Wysocki * @enable: Whether to enable or disable the wakeup functionality. 650cd7bd02dSRafael J. Wysocki */ 651cd7bd02dSRafael J. Wysocki int acpi_pm_device_run_wake(struct device *phys_dev, bool enable) 652cd7bd02dSRafael J. Wysocki { 653dee8370cSRafael J. Wysocki struct acpi_device *adev; 654cd7bd02dSRafael J. Wysocki acpi_handle handle; 655cd7bd02dSRafael J. Wysocki 656cd7bd02dSRafael J. Wysocki if (!device_run_wake(phys_dev)) 657cd7bd02dSRafael J. Wysocki return -EINVAL; 658cd7bd02dSRafael J. Wysocki 659cd7bd02dSRafael J. Wysocki handle = DEVICE_ACPI_HANDLE(phys_dev); 660dde3bb41SYasuaki Ishimatsu if (!handle || acpi_bus_get_device(handle, &adev)) { 661dee8370cSRafael J. Wysocki dev_dbg(phys_dev, "ACPI handle without context in %s!\n", 662cd7bd02dSRafael J. Wysocki __func__); 663cd7bd02dSRafael J. Wysocki return -ENODEV; 664cd7bd02dSRafael J. Wysocki } 665cd7bd02dSRafael J. Wysocki 666dee8370cSRafael J. Wysocki return __acpi_device_run_wake(adev, enable); 667cd7bd02dSRafael J. Wysocki } 668cd7bd02dSRafael J. Wysocki EXPORT_SYMBOL(acpi_pm_device_run_wake); 669e5cc8ef3SRafael J. Wysocki #else 670e5cc8ef3SRafael J. Wysocki static inline void acpi_wakeup_device(acpi_handle handle, u32 event, 671e5cc8ef3SRafael J. Wysocki void *context) {} 672cd7bd02dSRafael J. Wysocki #endif /* CONFIG_PM_RUNTIME */ 673dee8370cSRafael J. Wysocki 674dee8370cSRafael J. Wysocki #ifdef CONFIG_PM_SLEEP 675dee8370cSRafael J. Wysocki /** 676dee8370cSRafael J. Wysocki * __acpi_device_sleep_wake - Enable or disable device to wake up the system. 677dee8370cSRafael J. Wysocki * @dev: Device to enable/desible to wake up the system. 678dee8370cSRafael J. Wysocki * @target_state: System state the device is supposed to wake up from. 679dee8370cSRafael J. Wysocki * @enable: Whether to enable or disable @dev to wake up the system. 680dee8370cSRafael J. Wysocki */ 681dee8370cSRafael J. Wysocki int __acpi_device_sleep_wake(struct acpi_device *adev, u32 target_state, 682dee8370cSRafael J. Wysocki bool enable) 683dee8370cSRafael J. Wysocki { 684dee8370cSRafael J. Wysocki return enable ? 685dee8370cSRafael J. Wysocki acpi_enable_wakeup_device_power(adev, target_state) : 686dee8370cSRafael J. Wysocki acpi_disable_wakeup_device_power(adev); 687dee8370cSRafael J. Wysocki } 688a6ae7594SRafael J. Wysocki 689a6ae7594SRafael J. Wysocki /** 690a6ae7594SRafael J. Wysocki * acpi_pm_device_sleep_wake - Enable or disable device to wake up the system. 691a6ae7594SRafael J. Wysocki * @dev: Device to enable/desible to wake up the system from sleep states. 692a6ae7594SRafael J. Wysocki * @enable: Whether to enable or disable @dev to wake up the system. 693a6ae7594SRafael J. Wysocki */ 694a6ae7594SRafael J. Wysocki int acpi_pm_device_sleep_wake(struct device *dev, bool enable) 695a6ae7594SRafael J. Wysocki { 696a6ae7594SRafael J. Wysocki acpi_handle handle; 697a6ae7594SRafael J. Wysocki struct acpi_device *adev; 698a6ae7594SRafael J. Wysocki int error; 699a6ae7594SRafael J. Wysocki 700a6ae7594SRafael J. Wysocki if (!device_can_wakeup(dev)) 701a6ae7594SRafael J. Wysocki return -EINVAL; 702a6ae7594SRafael J. Wysocki 703a6ae7594SRafael J. Wysocki handle = DEVICE_ACPI_HANDLE(dev); 704dde3bb41SYasuaki Ishimatsu if (!handle || acpi_bus_get_device(handle, &adev)) { 705a6ae7594SRafael J. Wysocki dev_dbg(dev, "ACPI handle without context in %s!\n", __func__); 706a6ae7594SRafael J. Wysocki return -ENODEV; 707a6ae7594SRafael J. Wysocki } 708a6ae7594SRafael J. Wysocki 709a6ae7594SRafael J. Wysocki error = __acpi_device_sleep_wake(adev, acpi_target_system_state(), 710a6ae7594SRafael J. Wysocki enable); 711a6ae7594SRafael J. Wysocki if (!error) 712a6ae7594SRafael J. Wysocki dev_info(dev, "System wakeup %s by ACPI\n", 713a6ae7594SRafael J. Wysocki enable ? "enabled" : "disabled"); 714a6ae7594SRafael J. Wysocki 715a6ae7594SRafael J. Wysocki return error; 716a6ae7594SRafael J. Wysocki } 717dee8370cSRafael J. Wysocki #endif /* CONFIG_PM_SLEEP */ 718e5cc8ef3SRafael J. Wysocki 719e5cc8ef3SRafael J. Wysocki /** 720e5cc8ef3SRafael J. Wysocki * acpi_dev_pm_get_node - Get ACPI device node for the given physical device. 721e5cc8ef3SRafael J. Wysocki * @dev: Device to get the ACPI node for. 722e5cc8ef3SRafael J. Wysocki */ 723d2e5f0c1SRafael J. Wysocki struct acpi_device *acpi_dev_pm_get_node(struct device *dev) 724e5cc8ef3SRafael J. Wysocki { 725e5cc8ef3SRafael J. Wysocki acpi_handle handle = DEVICE_ACPI_HANDLE(dev); 726e5cc8ef3SRafael J. Wysocki struct acpi_device *adev; 727e5cc8ef3SRafael J. Wysocki 7285cc36c72SRafael J. Wysocki return handle && !acpi_bus_get_device(handle, &adev) ? adev : NULL; 729e5cc8ef3SRafael J. Wysocki } 730e5cc8ef3SRafael J. Wysocki 731e5cc8ef3SRafael J. Wysocki /** 732e5cc8ef3SRafael J. Wysocki * acpi_dev_pm_low_power - Put ACPI device into a low-power state. 733e5cc8ef3SRafael J. Wysocki * @dev: Device to put into a low-power state. 734e5cc8ef3SRafael J. Wysocki * @adev: ACPI device node corresponding to @dev. 735e5cc8ef3SRafael J. Wysocki * @system_state: System state to choose the device state for. 736e5cc8ef3SRafael J. Wysocki */ 737e5cc8ef3SRafael J. Wysocki static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev, 738e5cc8ef3SRafael J. Wysocki u32 system_state) 739e5cc8ef3SRafael J. Wysocki { 740fa1675b5SRafael J. Wysocki int ret, state; 741e5cc8ef3SRafael J. Wysocki 742e5cc8ef3SRafael J. Wysocki if (!acpi_device_power_manageable(adev)) 743e5cc8ef3SRafael J. Wysocki return 0; 744e5cc8ef3SRafael J. Wysocki 745fa1675b5SRafael J. Wysocki ret = acpi_dev_pm_get_state(dev, adev, system_state, NULL, &state); 746fa1675b5SRafael J. Wysocki return ret ? ret : acpi_device_set_power(adev, state); 747e5cc8ef3SRafael J. Wysocki } 748e5cc8ef3SRafael J. Wysocki 749e5cc8ef3SRafael J. Wysocki /** 750e5cc8ef3SRafael J. Wysocki * acpi_dev_pm_full_power - Put ACPI device into the full-power state. 751e5cc8ef3SRafael J. Wysocki * @adev: ACPI device node to put into the full-power state. 752e5cc8ef3SRafael J. Wysocki */ 753e5cc8ef3SRafael J. Wysocki static int acpi_dev_pm_full_power(struct acpi_device *adev) 754e5cc8ef3SRafael J. Wysocki { 755e5cc8ef3SRafael J. Wysocki return acpi_device_power_manageable(adev) ? 756e5cc8ef3SRafael J. Wysocki acpi_device_set_power(adev, ACPI_STATE_D0) : 0; 757e5cc8ef3SRafael J. Wysocki } 758e5cc8ef3SRafael J. Wysocki 759e5cc8ef3SRafael J. Wysocki #ifdef CONFIG_PM_RUNTIME 760e5cc8ef3SRafael J. Wysocki /** 761e5cc8ef3SRafael J. Wysocki * acpi_dev_runtime_suspend - Put device into a low-power state using ACPI. 762e5cc8ef3SRafael J. Wysocki * @dev: Device to put into a low-power state. 763e5cc8ef3SRafael J. Wysocki * 764e5cc8ef3SRafael J. Wysocki * Put the given device into a runtime low-power state using the standard ACPI 765e5cc8ef3SRafael J. Wysocki * mechanism. Set up remote wakeup if desired, choose the state to put the 766e5cc8ef3SRafael J. Wysocki * device into (this checks if remote wakeup is expected to work too), and set 767e5cc8ef3SRafael J. Wysocki * the power state of the device. 768e5cc8ef3SRafael J. Wysocki */ 769e5cc8ef3SRafael J. Wysocki int acpi_dev_runtime_suspend(struct device *dev) 770e5cc8ef3SRafael J. Wysocki { 771e5cc8ef3SRafael J. Wysocki struct acpi_device *adev = acpi_dev_pm_get_node(dev); 772e5cc8ef3SRafael J. Wysocki bool remote_wakeup; 773e5cc8ef3SRafael J. Wysocki int error; 774e5cc8ef3SRafael J. Wysocki 775e5cc8ef3SRafael J. Wysocki if (!adev) 776e5cc8ef3SRafael J. Wysocki return 0; 777e5cc8ef3SRafael J. Wysocki 778e5cc8ef3SRafael J. Wysocki remote_wakeup = dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) > 779e5cc8ef3SRafael J. Wysocki PM_QOS_FLAGS_NONE; 780e5cc8ef3SRafael J. Wysocki error = __acpi_device_run_wake(adev, remote_wakeup); 781e5cc8ef3SRafael J. Wysocki if (remote_wakeup && error) 782e5cc8ef3SRafael J. Wysocki return -EAGAIN; 783e5cc8ef3SRafael J. Wysocki 784e5cc8ef3SRafael J. Wysocki error = acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0); 785e5cc8ef3SRafael J. Wysocki if (error) 786e5cc8ef3SRafael J. Wysocki __acpi_device_run_wake(adev, false); 787e5cc8ef3SRafael J. Wysocki 788e5cc8ef3SRafael J. Wysocki return error; 789e5cc8ef3SRafael J. Wysocki } 790e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_runtime_suspend); 791e5cc8ef3SRafael J. Wysocki 792e5cc8ef3SRafael J. Wysocki /** 793e5cc8ef3SRafael J. Wysocki * acpi_dev_runtime_resume - Put device into the full-power state using ACPI. 794e5cc8ef3SRafael J. Wysocki * @dev: Device to put into the full-power state. 795e5cc8ef3SRafael J. Wysocki * 796e5cc8ef3SRafael J. Wysocki * Put the given device into the full-power state using the standard ACPI 797e5cc8ef3SRafael J. Wysocki * mechanism at run time. Set the power state of the device to ACPI D0 and 798e5cc8ef3SRafael J. Wysocki * disable remote wakeup. 799e5cc8ef3SRafael J. Wysocki */ 800e5cc8ef3SRafael J. Wysocki int acpi_dev_runtime_resume(struct device *dev) 801e5cc8ef3SRafael J. Wysocki { 802e5cc8ef3SRafael J. Wysocki struct acpi_device *adev = acpi_dev_pm_get_node(dev); 803e5cc8ef3SRafael J. Wysocki int error; 804e5cc8ef3SRafael J. Wysocki 805e5cc8ef3SRafael J. Wysocki if (!adev) 806e5cc8ef3SRafael J. Wysocki return 0; 807e5cc8ef3SRafael J. Wysocki 808e5cc8ef3SRafael J. Wysocki error = acpi_dev_pm_full_power(adev); 809e5cc8ef3SRafael J. Wysocki __acpi_device_run_wake(adev, false); 810e5cc8ef3SRafael J. Wysocki return error; 811e5cc8ef3SRafael J. Wysocki } 812e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_runtime_resume); 813e5cc8ef3SRafael J. Wysocki 814e5cc8ef3SRafael J. Wysocki /** 815e5cc8ef3SRafael J. Wysocki * acpi_subsys_runtime_suspend - Suspend device using ACPI. 816e5cc8ef3SRafael J. Wysocki * @dev: Device to suspend. 817e5cc8ef3SRafael J. Wysocki * 818e5cc8ef3SRafael J. Wysocki * Carry out the generic runtime suspend procedure for @dev and use ACPI to put 819e5cc8ef3SRafael J. Wysocki * it into a runtime low-power state. 820e5cc8ef3SRafael J. Wysocki */ 821e5cc8ef3SRafael J. Wysocki int acpi_subsys_runtime_suspend(struct device *dev) 822e5cc8ef3SRafael J. Wysocki { 823e5cc8ef3SRafael J. Wysocki int ret = pm_generic_runtime_suspend(dev); 824e5cc8ef3SRafael J. Wysocki return ret ? ret : acpi_dev_runtime_suspend(dev); 825e5cc8ef3SRafael J. Wysocki } 826e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend); 827e5cc8ef3SRafael J. Wysocki 828e5cc8ef3SRafael J. Wysocki /** 829e5cc8ef3SRafael J. Wysocki * acpi_subsys_runtime_resume - Resume device using ACPI. 830e5cc8ef3SRafael J. Wysocki * @dev: Device to Resume. 831e5cc8ef3SRafael J. Wysocki * 832e5cc8ef3SRafael J. Wysocki * Use ACPI to put the given device into the full-power state and carry out the 833e5cc8ef3SRafael J. Wysocki * generic runtime resume procedure for it. 834e5cc8ef3SRafael J. Wysocki */ 835e5cc8ef3SRafael J. Wysocki int acpi_subsys_runtime_resume(struct device *dev) 836e5cc8ef3SRafael J. Wysocki { 837e5cc8ef3SRafael J. Wysocki int ret = acpi_dev_runtime_resume(dev); 838e5cc8ef3SRafael J. Wysocki return ret ? ret : pm_generic_runtime_resume(dev); 839e5cc8ef3SRafael J. Wysocki } 840e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume); 841e5cc8ef3SRafael J. Wysocki #endif /* CONFIG_PM_RUNTIME */ 842e5cc8ef3SRafael J. Wysocki 843e5cc8ef3SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP 844e5cc8ef3SRafael J. Wysocki /** 845e5cc8ef3SRafael J. Wysocki * acpi_dev_suspend_late - Put device into a low-power state using ACPI. 846e5cc8ef3SRafael J. Wysocki * @dev: Device to put into a low-power state. 847e5cc8ef3SRafael J. Wysocki * 848e5cc8ef3SRafael J. Wysocki * Put the given device into a low-power state during system transition to a 849e5cc8ef3SRafael J. Wysocki * sleep state using the standard ACPI mechanism. Set up system wakeup if 850e5cc8ef3SRafael J. Wysocki * desired, choose the state to put the device into (this checks if system 851e5cc8ef3SRafael J. Wysocki * wakeup is expected to work too), and set the power state of the device. 852e5cc8ef3SRafael J. Wysocki */ 853e5cc8ef3SRafael J. Wysocki int acpi_dev_suspend_late(struct device *dev) 854e5cc8ef3SRafael J. Wysocki { 855e5cc8ef3SRafael J. Wysocki struct acpi_device *adev = acpi_dev_pm_get_node(dev); 856e5cc8ef3SRafael J. Wysocki u32 target_state; 857e5cc8ef3SRafael J. Wysocki bool wakeup; 858e5cc8ef3SRafael J. Wysocki int error; 859e5cc8ef3SRafael J. Wysocki 860e5cc8ef3SRafael J. Wysocki if (!adev) 861e5cc8ef3SRafael J. Wysocki return 0; 862e5cc8ef3SRafael J. Wysocki 863e5cc8ef3SRafael J. Wysocki target_state = acpi_target_system_state(); 864e5cc8ef3SRafael J. Wysocki wakeup = device_may_wakeup(dev); 865e5cc8ef3SRafael J. Wysocki error = __acpi_device_sleep_wake(adev, target_state, wakeup); 866e5cc8ef3SRafael J. Wysocki if (wakeup && error) 867e5cc8ef3SRafael J. Wysocki return error; 868e5cc8ef3SRafael J. Wysocki 869e5cc8ef3SRafael J. Wysocki error = acpi_dev_pm_low_power(dev, adev, target_state); 870e5cc8ef3SRafael J. Wysocki if (error) 871e5cc8ef3SRafael J. Wysocki __acpi_device_sleep_wake(adev, ACPI_STATE_UNKNOWN, false); 872e5cc8ef3SRafael J. Wysocki 873e5cc8ef3SRafael J. Wysocki return error; 874e5cc8ef3SRafael J. Wysocki } 875e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_suspend_late); 876e5cc8ef3SRafael J. Wysocki 877e5cc8ef3SRafael J. Wysocki /** 878e5cc8ef3SRafael J. Wysocki * acpi_dev_resume_early - Put device into the full-power state using ACPI. 879e5cc8ef3SRafael J. Wysocki * @dev: Device to put into the full-power state. 880e5cc8ef3SRafael J. Wysocki * 881e5cc8ef3SRafael J. Wysocki * Put the given device into the full-power state using the standard ACPI 882e5cc8ef3SRafael J. Wysocki * mechanism during system transition to the working state. Set the power 883e5cc8ef3SRafael J. Wysocki * state of the device to ACPI D0 and disable remote wakeup. 884e5cc8ef3SRafael J. Wysocki */ 885e5cc8ef3SRafael J. Wysocki int acpi_dev_resume_early(struct device *dev) 886e5cc8ef3SRafael J. Wysocki { 887e5cc8ef3SRafael J. Wysocki struct acpi_device *adev = acpi_dev_pm_get_node(dev); 888e5cc8ef3SRafael J. Wysocki int error; 889e5cc8ef3SRafael J. Wysocki 890e5cc8ef3SRafael J. Wysocki if (!adev) 891e5cc8ef3SRafael J. Wysocki return 0; 892e5cc8ef3SRafael J. Wysocki 893e5cc8ef3SRafael J. Wysocki error = acpi_dev_pm_full_power(adev); 894e5cc8ef3SRafael J. Wysocki __acpi_device_sleep_wake(adev, ACPI_STATE_UNKNOWN, false); 895e5cc8ef3SRafael J. Wysocki return error; 896e5cc8ef3SRafael J. Wysocki } 897e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_resume_early); 898e5cc8ef3SRafael J. Wysocki 899e5cc8ef3SRafael J. Wysocki /** 900e5cc8ef3SRafael J. Wysocki * acpi_subsys_prepare - Prepare device for system transition to a sleep state. 901e5cc8ef3SRafael J. Wysocki * @dev: Device to prepare. 902e5cc8ef3SRafael J. Wysocki */ 903e5cc8ef3SRafael J. Wysocki int acpi_subsys_prepare(struct device *dev) 904e5cc8ef3SRafael J. Wysocki { 905e5cc8ef3SRafael J. Wysocki /* 906e5cc8ef3SRafael J. Wysocki * Follow PCI and resume devices suspended at run time before running 907e5cc8ef3SRafael J. Wysocki * their system suspend callbacks. 908e5cc8ef3SRafael J. Wysocki */ 909e5cc8ef3SRafael J. Wysocki pm_runtime_resume(dev); 910e5cc8ef3SRafael J. Wysocki return pm_generic_prepare(dev); 911e5cc8ef3SRafael J. Wysocki } 912e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_prepare); 913e5cc8ef3SRafael J. Wysocki 914e5cc8ef3SRafael J. Wysocki /** 915e5cc8ef3SRafael J. Wysocki * acpi_subsys_suspend_late - Suspend device using ACPI. 916e5cc8ef3SRafael J. Wysocki * @dev: Device to suspend. 917e5cc8ef3SRafael J. Wysocki * 918e5cc8ef3SRafael J. Wysocki * Carry out the generic late suspend procedure for @dev and use ACPI to put 919e5cc8ef3SRafael J. Wysocki * it into a low-power state during system transition into a sleep state. 920e5cc8ef3SRafael J. Wysocki */ 921e5cc8ef3SRafael J. Wysocki int acpi_subsys_suspend_late(struct device *dev) 922e5cc8ef3SRafael J. Wysocki { 923e5cc8ef3SRafael J. Wysocki int ret = pm_generic_suspend_late(dev); 924e5cc8ef3SRafael J. Wysocki return ret ? ret : acpi_dev_suspend_late(dev); 925e5cc8ef3SRafael J. Wysocki } 926e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late); 927e5cc8ef3SRafael J. Wysocki 928e5cc8ef3SRafael J. Wysocki /** 929e5cc8ef3SRafael J. Wysocki * acpi_subsys_resume_early - Resume device using ACPI. 930e5cc8ef3SRafael J. Wysocki * @dev: Device to Resume. 931e5cc8ef3SRafael J. Wysocki * 932e5cc8ef3SRafael J. Wysocki * Use ACPI to put the given device into the full-power state and carry out the 933e5cc8ef3SRafael J. Wysocki * generic early resume procedure for it during system transition into the 934e5cc8ef3SRafael J. Wysocki * working state. 935e5cc8ef3SRafael J. Wysocki */ 936e5cc8ef3SRafael J. Wysocki int acpi_subsys_resume_early(struct device *dev) 937e5cc8ef3SRafael J. Wysocki { 938e5cc8ef3SRafael J. Wysocki int ret = acpi_dev_resume_early(dev); 939e5cc8ef3SRafael J. Wysocki return ret ? ret : pm_generic_resume_early(dev); 940e5cc8ef3SRafael J. Wysocki } 941e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_resume_early); 942e5cc8ef3SRafael J. Wysocki #endif /* CONFIG_PM_SLEEP */ 943e5cc8ef3SRafael J. Wysocki 944e5cc8ef3SRafael J. Wysocki static struct dev_pm_domain acpi_general_pm_domain = { 945e5cc8ef3SRafael J. Wysocki .ops = { 946e5cc8ef3SRafael J. Wysocki #ifdef CONFIG_PM_RUNTIME 947e5cc8ef3SRafael J. Wysocki .runtime_suspend = acpi_subsys_runtime_suspend, 948e5cc8ef3SRafael J. Wysocki .runtime_resume = acpi_subsys_runtime_resume, 949e5cc8ef3SRafael J. Wysocki #endif 950e5cc8ef3SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP 951e5cc8ef3SRafael J. Wysocki .prepare = acpi_subsys_prepare, 952e5cc8ef3SRafael J. Wysocki .suspend_late = acpi_subsys_suspend_late, 953e5cc8ef3SRafael J. Wysocki .resume_early = acpi_subsys_resume_early, 954e5cc8ef3SRafael J. Wysocki .poweroff_late = acpi_subsys_suspend_late, 955e5cc8ef3SRafael J. Wysocki .restore_early = acpi_subsys_resume_early, 956e5cc8ef3SRafael J. Wysocki #endif 957e5cc8ef3SRafael J. Wysocki }, 958e5cc8ef3SRafael J. Wysocki }; 959e5cc8ef3SRafael J. Wysocki 960e5cc8ef3SRafael J. Wysocki /** 961e5cc8ef3SRafael J. Wysocki * acpi_dev_pm_attach - Prepare device for ACPI power management. 962e5cc8ef3SRafael J. Wysocki * @dev: Device to prepare. 963b88ce2a4SRafael J. Wysocki * @power_on: Whether or not to power on the device. 964e5cc8ef3SRafael J. Wysocki * 965e5cc8ef3SRafael J. Wysocki * If @dev has a valid ACPI handle that has a valid struct acpi_device object 966e5cc8ef3SRafael J. Wysocki * attached to it, install a wakeup notification handler for the device and 967b88ce2a4SRafael J. Wysocki * add it to the general ACPI PM domain. If @power_on is set, the device will 968b88ce2a4SRafael J. Wysocki * be put into the ACPI D0 state before the function returns. 969e5cc8ef3SRafael J. Wysocki * 970e5cc8ef3SRafael J. Wysocki * This assumes that the @dev's bus type uses generic power management callbacks 971e5cc8ef3SRafael J. Wysocki * (or doesn't use any power management callbacks at all). 972e5cc8ef3SRafael J. Wysocki * 973e5cc8ef3SRafael J. Wysocki * Callers must ensure proper synchronization of this function with power 974e5cc8ef3SRafael J. Wysocki * management callbacks. 975e5cc8ef3SRafael J. Wysocki */ 976b88ce2a4SRafael J. Wysocki int acpi_dev_pm_attach(struct device *dev, bool power_on) 977e5cc8ef3SRafael J. Wysocki { 978e5cc8ef3SRafael J. Wysocki struct acpi_device *adev = acpi_dev_pm_get_node(dev); 979e5cc8ef3SRafael J. Wysocki 980e5cc8ef3SRafael J. Wysocki if (!adev) 981e5cc8ef3SRafael J. Wysocki return -ENODEV; 982e5cc8ef3SRafael J. Wysocki 983e5cc8ef3SRafael J. Wysocki if (dev->pm_domain) 984e5cc8ef3SRafael J. Wysocki return -EEXIST; 985e5cc8ef3SRafael J. Wysocki 986e5cc8ef3SRafael J. Wysocki acpi_add_pm_notifier(adev, acpi_wakeup_device, dev); 987e5cc8ef3SRafael J. Wysocki dev->pm_domain = &acpi_general_pm_domain; 988b88ce2a4SRafael J. Wysocki if (power_on) { 989b88ce2a4SRafael J. Wysocki acpi_dev_pm_full_power(adev); 990b88ce2a4SRafael J. Wysocki __acpi_device_run_wake(adev, false); 991b88ce2a4SRafael J. Wysocki } 992e5cc8ef3SRafael J. Wysocki return 0; 993e5cc8ef3SRafael J. Wysocki } 994e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_pm_attach); 995e5cc8ef3SRafael J. Wysocki 996e5cc8ef3SRafael J. Wysocki /** 997e5cc8ef3SRafael J. Wysocki * acpi_dev_pm_detach - Remove ACPI power management from the device. 998e5cc8ef3SRafael J. Wysocki * @dev: Device to take care of. 999b88ce2a4SRafael J. Wysocki * @power_off: Whether or not to try to remove power from the device. 1000e5cc8ef3SRafael J. Wysocki * 1001e5cc8ef3SRafael J. Wysocki * Remove the device from the general ACPI PM domain and remove its wakeup 1002b88ce2a4SRafael J. Wysocki * notifier. If @power_off is set, additionally remove power from the device if 1003b88ce2a4SRafael J. Wysocki * possible. 1004e5cc8ef3SRafael J. Wysocki * 1005e5cc8ef3SRafael J. Wysocki * Callers must ensure proper synchronization of this function with power 1006e5cc8ef3SRafael J. Wysocki * management callbacks. 1007e5cc8ef3SRafael J. Wysocki */ 1008b88ce2a4SRafael J. Wysocki void acpi_dev_pm_detach(struct device *dev, bool power_off) 1009e5cc8ef3SRafael J. Wysocki { 1010e5cc8ef3SRafael J. Wysocki struct acpi_device *adev = acpi_dev_pm_get_node(dev); 1011e5cc8ef3SRafael J. Wysocki 1012e5cc8ef3SRafael J. Wysocki if (adev && dev->pm_domain == &acpi_general_pm_domain) { 1013e5cc8ef3SRafael J. Wysocki dev->pm_domain = NULL; 1014e5cc8ef3SRafael J. Wysocki acpi_remove_pm_notifier(adev, acpi_wakeup_device); 1015b88ce2a4SRafael J. Wysocki if (power_off) { 1016b88ce2a4SRafael J. Wysocki /* 1017b88ce2a4SRafael J. Wysocki * If the device's PM QoS resume latency limit or flags 1018b88ce2a4SRafael J. Wysocki * have been exposed to user space, they have to be 1019b88ce2a4SRafael J. Wysocki * hidden at this point, so that they don't affect the 1020b88ce2a4SRafael J. Wysocki * choice of the low-power state to put the device into. 1021b88ce2a4SRafael J. Wysocki */ 1022b88ce2a4SRafael J. Wysocki dev_pm_qos_hide_latency_limit(dev); 1023b88ce2a4SRafael J. Wysocki dev_pm_qos_hide_flags(dev); 1024b88ce2a4SRafael J. Wysocki __acpi_device_run_wake(adev, false); 1025b88ce2a4SRafael J. Wysocki acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0); 1026b88ce2a4SRafael J. Wysocki } 1027e5cc8ef3SRafael J. Wysocki } 1028e5cc8ef3SRafael J. Wysocki } 1029e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_pm_detach); 1030bc9b6407SRafael J. Wysocki 1031bc9b6407SRafael J. Wysocki /** 1032bc9b6407SRafael J. Wysocki * acpi_dev_pm_add_dependent - Add physical device depending for PM. 1033bc9b6407SRafael J. Wysocki * @handle: Handle of ACPI device node. 1034bc9b6407SRafael J. Wysocki * @depdev: Device depending on that node for PM. 1035bc9b6407SRafael J. Wysocki */ 1036bc9b6407SRafael J. Wysocki void acpi_dev_pm_add_dependent(acpi_handle handle, struct device *depdev) 1037bc9b6407SRafael J. Wysocki { 1038bc9b6407SRafael J. Wysocki struct acpi_device_physical_node *dep; 1039bc9b6407SRafael J. Wysocki struct acpi_device *adev; 1040bc9b6407SRafael J. Wysocki 1041bc9b6407SRafael J. Wysocki if (!depdev || acpi_bus_get_device(handle, &adev)) 1042bc9b6407SRafael J. Wysocki return; 1043bc9b6407SRafael J. Wysocki 1044bc9b6407SRafael J. Wysocki mutex_lock(&adev->physical_node_lock); 1045bc9b6407SRafael J. Wysocki 1046bc9b6407SRafael J. Wysocki list_for_each_entry(dep, &adev->power_dependent, node) 1047bc9b6407SRafael J. Wysocki if (dep->dev == depdev) 1048bc9b6407SRafael J. Wysocki goto out; 1049bc9b6407SRafael J. Wysocki 1050bc9b6407SRafael J. Wysocki dep = kzalloc(sizeof(*dep), GFP_KERNEL); 1051bc9b6407SRafael J. Wysocki if (dep) { 1052bc9b6407SRafael J. Wysocki dep->dev = depdev; 1053bc9b6407SRafael J. Wysocki list_add_tail(&dep->node, &adev->power_dependent); 1054bc9b6407SRafael J. Wysocki } 1055bc9b6407SRafael J. Wysocki 1056bc9b6407SRafael J. Wysocki out: 1057bc9b6407SRafael J. Wysocki mutex_unlock(&adev->physical_node_lock); 1058bc9b6407SRafael J. Wysocki } 1059bc9b6407SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_pm_add_dependent); 1060bc9b6407SRafael J. Wysocki 1061bc9b6407SRafael J. Wysocki /** 1062bc9b6407SRafael J. Wysocki * acpi_dev_pm_remove_dependent - Remove physical device depending for PM. 1063bc9b6407SRafael J. Wysocki * @handle: Handle of ACPI device node. 1064bc9b6407SRafael J. Wysocki * @depdev: Device depending on that node for PM. 1065bc9b6407SRafael J. Wysocki */ 1066bc9b6407SRafael J. Wysocki void acpi_dev_pm_remove_dependent(acpi_handle handle, struct device *depdev) 1067bc9b6407SRafael J. Wysocki { 1068bc9b6407SRafael J. Wysocki struct acpi_device_physical_node *dep; 1069bc9b6407SRafael J. Wysocki struct acpi_device *adev; 1070bc9b6407SRafael J. Wysocki 1071bc9b6407SRafael J. Wysocki if (!depdev || acpi_bus_get_device(handle, &adev)) 1072bc9b6407SRafael J. Wysocki return; 1073bc9b6407SRafael J. Wysocki 1074bc9b6407SRafael J. Wysocki mutex_lock(&adev->physical_node_lock); 1075bc9b6407SRafael J. Wysocki 1076bc9b6407SRafael J. Wysocki list_for_each_entry(dep, &adev->power_dependent, node) 1077bc9b6407SRafael J. Wysocki if (dep->dev == depdev) { 1078bc9b6407SRafael J. Wysocki list_del(&dep->node); 1079bc9b6407SRafael J. Wysocki kfree(dep); 1080bc9b6407SRafael J. Wysocki break; 1081bc9b6407SRafael J. Wysocki } 1082bc9b6407SRafael J. Wysocki 1083bc9b6407SRafael J. Wysocki mutex_unlock(&adev->physical_node_lock); 1084bc9b6407SRafael J. Wysocki } 1085bc9b6407SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_pm_remove_dependent); 1086ec4602a9SRafael J. Wysocki #endif /* CONFIG_PM */ 1087