xref: /linux/drivers/acpi/device_pm.c (revision c072530f391e33bd22ed0638c08f07528f154493)
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 
370*c072530fSRafael J. Wysocki static void acpi_pm_notify_handler(acpi_handle handle, u32 val, void *not_used)
371*c072530fSRafael J. Wysocki {
372*c072530fSRafael J. Wysocki 	struct acpi_device *adev;
373*c072530fSRafael J. Wysocki 
374*c072530fSRafael J. Wysocki 	if (val != ACPI_NOTIFY_DEVICE_WAKE)
375*c072530fSRafael J. Wysocki 		return;
376*c072530fSRafael J. Wysocki 
377*c072530fSRafael J. Wysocki 	adev = acpi_bus_get_acpi_device(handle);
378*c072530fSRafael J. Wysocki 	if (!adev)
379*c072530fSRafael J. Wysocki 		return;
380*c072530fSRafael J. Wysocki 
381*c072530fSRafael J. Wysocki 	mutex_lock(&acpi_pm_notifier_lock);
382*c072530fSRafael J. Wysocki 
383*c072530fSRafael J. Wysocki 	if (adev->wakeup.flags.notifier_present) {
384*c072530fSRafael J. Wysocki 		__pm_wakeup_event(adev->wakeup.ws, 0);
385*c072530fSRafael J. Wysocki 		if (adev->wakeup.context.work.func)
386*c072530fSRafael J. Wysocki 			queue_pm_work(&adev->wakeup.context.work);
387*c072530fSRafael J. Wysocki 	}
388*c072530fSRafael J. Wysocki 
389*c072530fSRafael J. Wysocki 	mutex_unlock(&acpi_pm_notifier_lock);
390*c072530fSRafael J. Wysocki 
391*c072530fSRafael J. Wysocki 	acpi_bus_put_acpi_device(adev);
392*c072530fSRafael J. Wysocki }
393*c072530fSRafael J. Wysocki 
394ec4602a9SRafael J. Wysocki /**
395*c072530fSRafael J. Wysocki  * acpi_add_pm_notifier - Register PM notify handler for given ACPI device.
396*c072530fSRafael J. Wysocki  * @adev: ACPI device to add the notify handler for.
397*c072530fSRafael J. Wysocki  * @dev: Device to generate a wakeup event for while handling the notification.
398*c072530fSRafael 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  */
405*c072530fSRafael J. Wysocki acpi_status acpi_add_pm_notifier(struct acpi_device *adev, struct device *dev,
406*c072530fSRafael J. Wysocki 				 void (*work_func)(struct work_struct *work))
407ec4602a9SRafael J. Wysocki {
408ec4602a9SRafael J. Wysocki 	acpi_status status = AE_ALREADY_EXISTS;
409ec4602a9SRafael J. Wysocki 
410*c072530fSRafael J. Wysocki 	if (!dev && !work_func)
411*c072530fSRafael J. Wysocki 		return AE_BAD_PARAMETER;
412*c072530fSRafael 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 
418*c072530fSRafael J. Wysocki 	adev->wakeup.ws = wakeup_source_register(dev_name(&adev->dev));
419*c072530fSRafael J. Wysocki 	adev->wakeup.context.dev = dev;
420*c072530fSRafael J. Wysocki 	if (work_func)
421*c072530fSRafael J. Wysocki 		INIT_WORK(&adev->wakeup.context.work, work_func);
422*c072530fSRafael J. Wysocki 
423*c072530fSRafael J. Wysocki 	status = acpi_install_notify_handler(adev->handle, ACPI_SYSTEM_NOTIFY,
424*c072530fSRafael 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  */
439*c072530fSRafael 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,
450*c072530fSRafael J. Wysocki 					    acpi_pm_notify_handler);
451ec4602a9SRafael J. Wysocki 	if (ACPI_FAILURE(status))
452ec4602a9SRafael J. Wysocki 		goto out;
453ec4602a9SRafael J. Wysocki 
454*c072530fSRafael J. Wysocki 	if (adev->wakeup.context.work.func) {
455*c072530fSRafael J. Wysocki 		cancel_work_sync(&adev->wakeup.context.work);
456*c072530fSRafael J. Wysocki 		adev->wakeup.context.work.func = NULL;
457*c072530fSRafael J. Wysocki 	}
458*c072530fSRafael J. Wysocki 	adev->wakeup.context.dev = NULL;
459*c072530fSRafael J. Wysocki 	wakeup_source_unregister(adev->wakeup.ws);
460*c072530fSRafael 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 #ifdef CONFIG_PM_RUNTIME
642cd7bd02dSRafael J. Wysocki /**
643*c072530fSRafael J. Wysocki  * acpi_pm_notify_work_func - ACPI devices wakeup notification work function.
644*c072530fSRafael J. Wysocki  * @work: Work item to handle.
645e5cc8ef3SRafael J. Wysocki  */
646*c072530fSRafael J. Wysocki static void acpi_pm_notify_work_func(struct work_struct *work)
647e5cc8ef3SRafael J. Wysocki {
648*c072530fSRafael J. Wysocki 	struct device *dev;
649e5cc8ef3SRafael J. Wysocki 
650*c072530fSRafael J. Wysocki 	dev = container_of(work, struct acpi_device_wakeup_context, work)->dev;
651*c072530fSRafael J. Wysocki 	if (dev) {
652e5cc8ef3SRafael J. Wysocki 		pm_wakeup_event(dev, 0);
653e5cc8ef3SRafael J. Wysocki 		pm_runtime_resume(dev);
654e5cc8ef3SRafael J. Wysocki 	}
655e5cc8ef3SRafael J. Wysocki }
656e5cc8ef3SRafael J. Wysocki 
657e5cc8ef3SRafael J. Wysocki /**
658dee8370cSRafael J. Wysocki  * __acpi_device_run_wake - Enable/disable runtime remote wakeup for device.
659dee8370cSRafael J. Wysocki  * @adev: ACPI device to enable/disable the remote wakeup for.
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  */
669dee8370cSRafael J. Wysocki int __acpi_device_run_wake(struct acpi_device *adev, bool enable)
670dee8370cSRafael J. Wysocki {
671dee8370cSRafael J. Wysocki 	struct acpi_device_wakeup *wakeup = &adev->wakeup;
672dee8370cSRafael J. Wysocki 
673dee8370cSRafael J. Wysocki 	if (enable) {
674dee8370cSRafael J. Wysocki 		acpi_status res;
675dee8370cSRafael J. Wysocki 		int error;
676dee8370cSRafael J. Wysocki 
677dee8370cSRafael J. Wysocki 		error = acpi_enable_wakeup_device_power(adev, ACPI_STATE_S0);
678dee8370cSRafael J. Wysocki 		if (error)
679dee8370cSRafael J. Wysocki 			return error;
680dee8370cSRafael J. Wysocki 
681dee8370cSRafael J. Wysocki 		res = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number);
682dee8370cSRafael J. Wysocki 		if (ACPI_FAILURE(res)) {
683dee8370cSRafael J. Wysocki 			acpi_disable_wakeup_device_power(adev);
684dee8370cSRafael J. Wysocki 			return -EIO;
685dee8370cSRafael J. Wysocki 		}
686dee8370cSRafael J. Wysocki 	} else {
687dee8370cSRafael J. Wysocki 		acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number);
688dee8370cSRafael J. Wysocki 		acpi_disable_wakeup_device_power(adev);
689dee8370cSRafael J. Wysocki 	}
690dee8370cSRafael J. Wysocki 	return 0;
691dee8370cSRafael J. Wysocki }
692dee8370cSRafael J. Wysocki 
693dee8370cSRafael J. Wysocki /**
694dee8370cSRafael J. Wysocki  * acpi_pm_device_run_wake - Enable/disable remote wakeup for given device.
695dee8370cSRafael J. Wysocki  * @dev: Device to enable/disable the platform to wake up.
696dee8370cSRafael J. Wysocki  * @enable: Whether to enable or disable the wakeup functionality.
697cd7bd02dSRafael J. Wysocki  */
698cd7bd02dSRafael J. Wysocki int acpi_pm_device_run_wake(struct device *phys_dev, bool enable)
699cd7bd02dSRafael J. Wysocki {
700dee8370cSRafael J. Wysocki 	struct acpi_device *adev;
701cd7bd02dSRafael J. Wysocki 	acpi_handle handle;
702cd7bd02dSRafael J. Wysocki 
703cd7bd02dSRafael J. Wysocki 	if (!device_run_wake(phys_dev))
704cd7bd02dSRafael J. Wysocki 		return -EINVAL;
705cd7bd02dSRafael J. Wysocki 
7063a83f992SRafael J. Wysocki 	handle = ACPI_HANDLE(phys_dev);
707dde3bb41SYasuaki Ishimatsu 	if (!handle || acpi_bus_get_device(handle, &adev)) {
708dee8370cSRafael J. Wysocki 		dev_dbg(phys_dev, "ACPI handle without context in %s!\n",
709cd7bd02dSRafael J. Wysocki 			__func__);
710cd7bd02dSRafael J. Wysocki 		return -ENODEV;
711cd7bd02dSRafael J. Wysocki 	}
712cd7bd02dSRafael J. Wysocki 
713dee8370cSRafael J. Wysocki 	return __acpi_device_run_wake(adev, enable);
714cd7bd02dSRafael J. Wysocki }
715cd7bd02dSRafael J. Wysocki EXPORT_SYMBOL(acpi_pm_device_run_wake);
716e5cc8ef3SRafael J. Wysocki #else
717*c072530fSRafael J. Wysocki static inline void acpi_pm_notify_work_func(struct work_struct *work) {}
718cd7bd02dSRafael J. Wysocki #endif /* CONFIG_PM_RUNTIME */
719dee8370cSRafael J. Wysocki 
720dee8370cSRafael J. Wysocki #ifdef CONFIG_PM_SLEEP
721dee8370cSRafael J. Wysocki /**
722dee8370cSRafael J. Wysocki  * __acpi_device_sleep_wake - Enable or disable device to wake up the system.
723dee8370cSRafael J. Wysocki  * @dev: Device to enable/desible to wake up the system.
724dee8370cSRafael J. Wysocki  * @target_state: System state the device is supposed to wake up from.
725dee8370cSRafael J. Wysocki  * @enable: Whether to enable or disable @dev to wake up the system.
726dee8370cSRafael J. Wysocki  */
727dee8370cSRafael J. Wysocki int __acpi_device_sleep_wake(struct acpi_device *adev, u32 target_state,
728dee8370cSRafael J. Wysocki 			     bool enable)
729dee8370cSRafael J. Wysocki {
730dee8370cSRafael J. Wysocki 	return enable ?
731dee8370cSRafael J. Wysocki 		acpi_enable_wakeup_device_power(adev, target_state) :
732dee8370cSRafael J. Wysocki 		acpi_disable_wakeup_device_power(adev);
733dee8370cSRafael J. Wysocki }
734a6ae7594SRafael J. Wysocki 
735a6ae7594SRafael J. Wysocki /**
736a6ae7594SRafael J. Wysocki  * acpi_pm_device_sleep_wake - Enable or disable device to wake up the system.
737a6ae7594SRafael J. Wysocki  * @dev: Device to enable/desible to wake up the system from sleep states.
738a6ae7594SRafael J. Wysocki  * @enable: Whether to enable or disable @dev to wake up the system.
739a6ae7594SRafael J. Wysocki  */
740a6ae7594SRafael J. Wysocki int acpi_pm_device_sleep_wake(struct device *dev, bool enable)
741a6ae7594SRafael J. Wysocki {
742a6ae7594SRafael J. Wysocki 	acpi_handle handle;
743a6ae7594SRafael J. Wysocki 	struct acpi_device *adev;
744a6ae7594SRafael J. Wysocki 	int error;
745a6ae7594SRafael J. Wysocki 
746a6ae7594SRafael J. Wysocki 	if (!device_can_wakeup(dev))
747a6ae7594SRafael J. Wysocki 		return -EINVAL;
748a6ae7594SRafael J. Wysocki 
7493a83f992SRafael J. Wysocki 	handle = ACPI_HANDLE(dev);
750dde3bb41SYasuaki Ishimatsu 	if (!handle || acpi_bus_get_device(handle, &adev)) {
751a6ae7594SRafael J. Wysocki 		dev_dbg(dev, "ACPI handle without context in %s!\n", __func__);
752a6ae7594SRafael J. Wysocki 		return -ENODEV;
753a6ae7594SRafael J. Wysocki 	}
754a6ae7594SRafael J. Wysocki 
755a6ae7594SRafael J. Wysocki 	error = __acpi_device_sleep_wake(adev, acpi_target_system_state(),
756a6ae7594SRafael J. Wysocki 					 enable);
757a6ae7594SRafael J. Wysocki 	if (!error)
758a6ae7594SRafael J. Wysocki 		dev_info(dev, "System wakeup %s by ACPI\n",
759a6ae7594SRafael J. Wysocki 				enable ? "enabled" : "disabled");
760a6ae7594SRafael J. Wysocki 
761a6ae7594SRafael J. Wysocki 	return error;
762a6ae7594SRafael J. Wysocki }
763dee8370cSRafael J. Wysocki #endif /* CONFIG_PM_SLEEP */
764e5cc8ef3SRafael J. Wysocki 
765e5cc8ef3SRafael J. Wysocki /**
766e5cc8ef3SRafael J. Wysocki  * acpi_dev_pm_low_power - Put ACPI device into a low-power state.
767e5cc8ef3SRafael J. Wysocki  * @dev: Device to put into a low-power state.
768e5cc8ef3SRafael J. Wysocki  * @adev: ACPI device node corresponding to @dev.
769e5cc8ef3SRafael J. Wysocki  * @system_state: System state to choose the device state for.
770e5cc8ef3SRafael J. Wysocki  */
771e5cc8ef3SRafael J. Wysocki static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev,
772e5cc8ef3SRafael J. Wysocki 				 u32 system_state)
773e5cc8ef3SRafael J. Wysocki {
774fa1675b5SRafael J. Wysocki 	int ret, state;
775e5cc8ef3SRafael J. Wysocki 
776e5cc8ef3SRafael J. Wysocki 	if (!acpi_device_power_manageable(adev))
777e5cc8ef3SRafael J. Wysocki 		return 0;
778e5cc8ef3SRafael J. Wysocki 
779fa1675b5SRafael J. Wysocki 	ret = acpi_dev_pm_get_state(dev, adev, system_state, NULL, &state);
780fa1675b5SRafael J. Wysocki 	return ret ? ret : acpi_device_set_power(adev, state);
781e5cc8ef3SRafael J. Wysocki }
782e5cc8ef3SRafael J. Wysocki 
783e5cc8ef3SRafael J. Wysocki /**
784e5cc8ef3SRafael J. Wysocki  * acpi_dev_pm_full_power - Put ACPI device into the full-power state.
785e5cc8ef3SRafael J. Wysocki  * @adev: ACPI device node to put into the full-power state.
786e5cc8ef3SRafael J. Wysocki  */
787e5cc8ef3SRafael J. Wysocki static int acpi_dev_pm_full_power(struct acpi_device *adev)
788e5cc8ef3SRafael J. Wysocki {
789e5cc8ef3SRafael J. Wysocki 	return acpi_device_power_manageable(adev) ?
790e5cc8ef3SRafael J. Wysocki 		acpi_device_set_power(adev, ACPI_STATE_D0) : 0;
791e5cc8ef3SRafael J. Wysocki }
792e5cc8ef3SRafael J. Wysocki 
793e5cc8ef3SRafael J. Wysocki #ifdef CONFIG_PM_RUNTIME
794e5cc8ef3SRafael J. Wysocki /**
795e5cc8ef3SRafael J. Wysocki  * acpi_dev_runtime_suspend - Put device into a low-power state using ACPI.
796e5cc8ef3SRafael J. Wysocki  * @dev: Device to put into a low-power state.
797e5cc8ef3SRafael J. Wysocki  *
798e5cc8ef3SRafael J. Wysocki  * Put the given device into a runtime low-power state using the standard ACPI
799e5cc8ef3SRafael J. Wysocki  * mechanism.  Set up remote wakeup if desired, choose the state to put the
800e5cc8ef3SRafael J. Wysocki  * device into (this checks if remote wakeup is expected to work too), and set
801e5cc8ef3SRafael J. Wysocki  * the power state of the device.
802e5cc8ef3SRafael J. Wysocki  */
803e5cc8ef3SRafael J. Wysocki int acpi_dev_runtime_suspend(struct device *dev)
804e5cc8ef3SRafael J. Wysocki {
80579c0373fSRafael J. Wysocki 	struct acpi_device *adev = ACPI_COMPANION(dev);
806e5cc8ef3SRafael J. Wysocki 	bool remote_wakeup;
807e5cc8ef3SRafael J. Wysocki 	int error;
808e5cc8ef3SRafael J. Wysocki 
809e5cc8ef3SRafael J. Wysocki 	if (!adev)
810e5cc8ef3SRafael J. Wysocki 		return 0;
811e5cc8ef3SRafael J. Wysocki 
812e5cc8ef3SRafael J. Wysocki 	remote_wakeup = dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) >
813e5cc8ef3SRafael J. Wysocki 				PM_QOS_FLAGS_NONE;
814e5cc8ef3SRafael J. Wysocki 	error = __acpi_device_run_wake(adev, remote_wakeup);
815e5cc8ef3SRafael J. Wysocki 	if (remote_wakeup && error)
816e5cc8ef3SRafael J. Wysocki 		return -EAGAIN;
817e5cc8ef3SRafael J. Wysocki 
818e5cc8ef3SRafael J. Wysocki 	error = acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
819e5cc8ef3SRafael J. Wysocki 	if (error)
820e5cc8ef3SRafael J. Wysocki 		__acpi_device_run_wake(adev, false);
821e5cc8ef3SRafael J. Wysocki 
822e5cc8ef3SRafael J. Wysocki 	return error;
823e5cc8ef3SRafael J. Wysocki }
824e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_runtime_suspend);
825e5cc8ef3SRafael J. Wysocki 
826e5cc8ef3SRafael J. Wysocki /**
827e5cc8ef3SRafael J. Wysocki  * acpi_dev_runtime_resume - Put device into the full-power state using ACPI.
828e5cc8ef3SRafael J. Wysocki  * @dev: Device to put into the full-power state.
829e5cc8ef3SRafael J. Wysocki  *
830e5cc8ef3SRafael J. Wysocki  * Put the given device into the full-power state using the standard ACPI
831e5cc8ef3SRafael J. Wysocki  * mechanism at run time.  Set the power state of the device to ACPI D0 and
832e5cc8ef3SRafael J. Wysocki  * disable remote wakeup.
833e5cc8ef3SRafael J. Wysocki  */
834e5cc8ef3SRafael J. Wysocki int acpi_dev_runtime_resume(struct device *dev)
835e5cc8ef3SRafael J. Wysocki {
83679c0373fSRafael J. Wysocki 	struct acpi_device *adev = ACPI_COMPANION(dev);
837e5cc8ef3SRafael J. Wysocki 	int error;
838e5cc8ef3SRafael J. Wysocki 
839e5cc8ef3SRafael J. Wysocki 	if (!adev)
840e5cc8ef3SRafael J. Wysocki 		return 0;
841e5cc8ef3SRafael J. Wysocki 
842e5cc8ef3SRafael J. Wysocki 	error = acpi_dev_pm_full_power(adev);
843e5cc8ef3SRafael J. Wysocki 	__acpi_device_run_wake(adev, false);
844e5cc8ef3SRafael J. Wysocki 	return error;
845e5cc8ef3SRafael J. Wysocki }
846e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_runtime_resume);
847e5cc8ef3SRafael J. Wysocki 
848e5cc8ef3SRafael J. Wysocki /**
849e5cc8ef3SRafael J. Wysocki  * acpi_subsys_runtime_suspend - Suspend device using ACPI.
850e5cc8ef3SRafael J. Wysocki  * @dev: Device to suspend.
851e5cc8ef3SRafael J. Wysocki  *
852e5cc8ef3SRafael J. Wysocki  * Carry out the generic runtime suspend procedure for @dev and use ACPI to put
853e5cc8ef3SRafael J. Wysocki  * it into a runtime low-power state.
854e5cc8ef3SRafael J. Wysocki  */
855e5cc8ef3SRafael J. Wysocki int acpi_subsys_runtime_suspend(struct device *dev)
856e5cc8ef3SRafael J. Wysocki {
857e5cc8ef3SRafael J. Wysocki 	int ret = pm_generic_runtime_suspend(dev);
858e5cc8ef3SRafael J. Wysocki 	return ret ? ret : acpi_dev_runtime_suspend(dev);
859e5cc8ef3SRafael J. Wysocki }
860e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend);
861e5cc8ef3SRafael J. Wysocki 
862e5cc8ef3SRafael J. Wysocki /**
863e5cc8ef3SRafael J. Wysocki  * acpi_subsys_runtime_resume - Resume device using ACPI.
864e5cc8ef3SRafael J. Wysocki  * @dev: Device to Resume.
865e5cc8ef3SRafael J. Wysocki  *
866e5cc8ef3SRafael J. Wysocki  * Use ACPI to put the given device into the full-power state and carry out the
867e5cc8ef3SRafael J. Wysocki  * generic runtime resume procedure for it.
868e5cc8ef3SRafael J. Wysocki  */
869e5cc8ef3SRafael J. Wysocki int acpi_subsys_runtime_resume(struct device *dev)
870e5cc8ef3SRafael J. Wysocki {
871e5cc8ef3SRafael J. Wysocki 	int ret = acpi_dev_runtime_resume(dev);
872e5cc8ef3SRafael J. Wysocki 	return ret ? ret : pm_generic_runtime_resume(dev);
873e5cc8ef3SRafael J. Wysocki }
874e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume);
875e5cc8ef3SRafael J. Wysocki #endif /* CONFIG_PM_RUNTIME */
876e5cc8ef3SRafael J. Wysocki 
877e5cc8ef3SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP
878e5cc8ef3SRafael J. Wysocki /**
879e5cc8ef3SRafael J. Wysocki  * acpi_dev_suspend_late - Put device into a low-power state using ACPI.
880e5cc8ef3SRafael J. Wysocki  * @dev: Device to put into a low-power state.
881e5cc8ef3SRafael J. Wysocki  *
882e5cc8ef3SRafael J. Wysocki  * Put the given device into a low-power state during system transition to a
883e5cc8ef3SRafael J. Wysocki  * sleep state using the standard ACPI mechanism.  Set up system wakeup if
884e5cc8ef3SRafael J. Wysocki  * desired, choose the state to put the device into (this checks if system
885e5cc8ef3SRafael J. Wysocki  * wakeup is expected to work too), and set the power state of the device.
886e5cc8ef3SRafael J. Wysocki  */
887e5cc8ef3SRafael J. Wysocki int acpi_dev_suspend_late(struct device *dev)
888e5cc8ef3SRafael J. Wysocki {
88979c0373fSRafael J. Wysocki 	struct acpi_device *adev = ACPI_COMPANION(dev);
890e5cc8ef3SRafael J. Wysocki 	u32 target_state;
891e5cc8ef3SRafael J. Wysocki 	bool wakeup;
892e5cc8ef3SRafael J. Wysocki 	int error;
893e5cc8ef3SRafael J. Wysocki 
894e5cc8ef3SRafael J. Wysocki 	if (!adev)
895e5cc8ef3SRafael J. Wysocki 		return 0;
896e5cc8ef3SRafael J. Wysocki 
897e5cc8ef3SRafael J. Wysocki 	target_state = acpi_target_system_state();
898e5cc8ef3SRafael J. Wysocki 	wakeup = device_may_wakeup(dev);
899e5cc8ef3SRafael J. Wysocki 	error = __acpi_device_sleep_wake(adev, target_state, wakeup);
900e5cc8ef3SRafael J. Wysocki 	if (wakeup && error)
901e5cc8ef3SRafael J. Wysocki 		return error;
902e5cc8ef3SRafael J. Wysocki 
903e5cc8ef3SRafael J. Wysocki 	error = acpi_dev_pm_low_power(dev, adev, target_state);
904e5cc8ef3SRafael J. Wysocki 	if (error)
905e5cc8ef3SRafael J. Wysocki 		__acpi_device_sleep_wake(adev, ACPI_STATE_UNKNOWN, false);
906e5cc8ef3SRafael J. Wysocki 
907e5cc8ef3SRafael J. Wysocki 	return error;
908e5cc8ef3SRafael J. Wysocki }
909e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_suspend_late);
910e5cc8ef3SRafael J. Wysocki 
911e5cc8ef3SRafael J. Wysocki /**
912e5cc8ef3SRafael J. Wysocki  * acpi_dev_resume_early - Put device into the full-power state using ACPI.
913e5cc8ef3SRafael J. Wysocki  * @dev: Device to put into the full-power state.
914e5cc8ef3SRafael J. Wysocki  *
915e5cc8ef3SRafael J. Wysocki  * Put the given device into the full-power state using the standard ACPI
916e5cc8ef3SRafael J. Wysocki  * mechanism during system transition to the working state.  Set the power
917e5cc8ef3SRafael J. Wysocki  * state of the device to ACPI D0 and disable remote wakeup.
918e5cc8ef3SRafael J. Wysocki  */
919e5cc8ef3SRafael J. Wysocki int acpi_dev_resume_early(struct device *dev)
920e5cc8ef3SRafael J. Wysocki {
92179c0373fSRafael J. Wysocki 	struct acpi_device *adev = ACPI_COMPANION(dev);
922e5cc8ef3SRafael J. Wysocki 	int error;
923e5cc8ef3SRafael J. Wysocki 
924e5cc8ef3SRafael J. Wysocki 	if (!adev)
925e5cc8ef3SRafael J. Wysocki 		return 0;
926e5cc8ef3SRafael J. Wysocki 
927e5cc8ef3SRafael J. Wysocki 	error = acpi_dev_pm_full_power(adev);
928e5cc8ef3SRafael J. Wysocki 	__acpi_device_sleep_wake(adev, ACPI_STATE_UNKNOWN, false);
929e5cc8ef3SRafael J. Wysocki 	return error;
930e5cc8ef3SRafael J. Wysocki }
931e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_resume_early);
932e5cc8ef3SRafael J. Wysocki 
933e5cc8ef3SRafael J. Wysocki /**
934e5cc8ef3SRafael J. Wysocki  * acpi_subsys_prepare - Prepare device for system transition to a sleep state.
935e5cc8ef3SRafael J. Wysocki  * @dev: Device to prepare.
936e5cc8ef3SRafael J. Wysocki  */
937e5cc8ef3SRafael J. Wysocki int acpi_subsys_prepare(struct device *dev)
938e5cc8ef3SRafael J. Wysocki {
939f25c0ae2SRafael J. Wysocki 	struct acpi_device *adev = ACPI_COMPANION(dev);
940f25c0ae2SRafael J. Wysocki 	u32 sys_target;
941f25c0ae2SRafael J. Wysocki 	int ret, state;
94292858c47SRafael J. Wysocki 
943f25c0ae2SRafael J. Wysocki 	ret = pm_generic_prepare(dev);
944f25c0ae2SRafael J. Wysocki 	if (ret < 0)
945f25c0ae2SRafael J. Wysocki 		return ret;
946f25c0ae2SRafael J. Wysocki 
947f25c0ae2SRafael J. Wysocki 	if (!adev || !pm_runtime_suspended(dev)
948f25c0ae2SRafael J. Wysocki 	    || device_may_wakeup(dev) != !!adev->wakeup.prepare_count)
949f25c0ae2SRafael J. Wysocki 		return 0;
950f25c0ae2SRafael J. Wysocki 
951f25c0ae2SRafael J. Wysocki 	sys_target = acpi_target_system_state();
952f25c0ae2SRafael J. Wysocki 	if (sys_target == ACPI_STATE_S0)
953f25c0ae2SRafael J. Wysocki 		return 1;
954f25c0ae2SRafael J. Wysocki 
955f25c0ae2SRafael J. Wysocki 	if (adev->power.flags.dsw_present)
956f25c0ae2SRafael J. Wysocki 		return 0;
957f25c0ae2SRafael J. Wysocki 
958f25c0ae2SRafael J. Wysocki 	ret = acpi_dev_pm_get_state(dev, adev, sys_target, NULL, &state);
959f25c0ae2SRafael J. Wysocki 	return !ret && state == adev->power.state;
960e5cc8ef3SRafael J. Wysocki }
961e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_prepare);
962e5cc8ef3SRafael J. Wysocki 
963e5cc8ef3SRafael J. Wysocki /**
964f25c0ae2SRafael J. Wysocki  * acpi_subsys_complete - Finalize device's resume during system resume.
965f25c0ae2SRafael J. Wysocki  * @dev: Device to handle.
966f25c0ae2SRafael J. Wysocki  */
9674cf563c5SHeikki Krogerus void acpi_subsys_complete(struct device *dev)
968f25c0ae2SRafael J. Wysocki {
969f25c0ae2SRafael J. Wysocki 	/*
970f25c0ae2SRafael J. Wysocki 	 * If the device had been runtime-suspended before the system went into
971f25c0ae2SRafael J. Wysocki 	 * the sleep state it is going out of and it has never been resumed till
972f25c0ae2SRafael J. Wysocki 	 * now, resume it in case the firmware powered it up.
973f25c0ae2SRafael J. Wysocki 	 */
974f25c0ae2SRafael J. Wysocki 	if (dev->power.direct_complete)
975f25c0ae2SRafael J. Wysocki 		pm_request_resume(dev);
976f25c0ae2SRafael J. Wysocki }
9774cf563c5SHeikki Krogerus EXPORT_SYMBOL_GPL(acpi_subsys_complete);
978f25c0ae2SRafael J. Wysocki 
979f25c0ae2SRafael J. Wysocki /**
98092858c47SRafael J. Wysocki  * acpi_subsys_suspend - Run the device driver's suspend callback.
98192858c47SRafael J. Wysocki  * @dev: Device to handle.
98292858c47SRafael J. Wysocki  *
98392858c47SRafael J. Wysocki  * Follow PCI and resume devices suspended at run time before running their
98492858c47SRafael J. Wysocki  * system suspend callbacks.
98592858c47SRafael J. Wysocki  */
98692858c47SRafael J. Wysocki int acpi_subsys_suspend(struct device *dev)
98792858c47SRafael J. Wysocki {
98892858c47SRafael J. Wysocki 	pm_runtime_resume(dev);
98992858c47SRafael J. Wysocki 	return pm_generic_suspend(dev);
99092858c47SRafael J. Wysocki }
9914cf563c5SHeikki Krogerus EXPORT_SYMBOL_GPL(acpi_subsys_suspend);
99292858c47SRafael J. Wysocki 
99392858c47SRafael J. Wysocki /**
994e5cc8ef3SRafael J. Wysocki  * acpi_subsys_suspend_late - Suspend device using ACPI.
995e5cc8ef3SRafael J. Wysocki  * @dev: Device to suspend.
996e5cc8ef3SRafael J. Wysocki  *
997e5cc8ef3SRafael J. Wysocki  * Carry out the generic late suspend procedure for @dev and use ACPI to put
998e5cc8ef3SRafael J. Wysocki  * it into a low-power state during system transition into a sleep state.
999e5cc8ef3SRafael J. Wysocki  */
1000e5cc8ef3SRafael J. Wysocki int acpi_subsys_suspend_late(struct device *dev)
1001e5cc8ef3SRafael J. Wysocki {
1002e5cc8ef3SRafael J. Wysocki 	int ret = pm_generic_suspend_late(dev);
1003e5cc8ef3SRafael J. Wysocki 	return ret ? ret : acpi_dev_suspend_late(dev);
1004e5cc8ef3SRafael J. Wysocki }
1005e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late);
1006e5cc8ef3SRafael J. Wysocki 
1007e5cc8ef3SRafael J. Wysocki /**
1008e5cc8ef3SRafael J. Wysocki  * acpi_subsys_resume_early - Resume device using ACPI.
1009e5cc8ef3SRafael J. Wysocki  * @dev: Device to Resume.
1010e5cc8ef3SRafael J. Wysocki  *
1011e5cc8ef3SRafael J. Wysocki  * Use ACPI to put the given device into the full-power state and carry out the
1012e5cc8ef3SRafael J. Wysocki  * generic early resume procedure for it during system transition into the
1013e5cc8ef3SRafael J. Wysocki  * working state.
1014e5cc8ef3SRafael J. Wysocki  */
1015e5cc8ef3SRafael J. Wysocki int acpi_subsys_resume_early(struct device *dev)
1016e5cc8ef3SRafael J. Wysocki {
1017e5cc8ef3SRafael J. Wysocki 	int ret = acpi_dev_resume_early(dev);
1018e5cc8ef3SRafael J. Wysocki 	return ret ? ret : pm_generic_resume_early(dev);
1019e5cc8ef3SRafael J. Wysocki }
1020e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_resume_early);
102192858c47SRafael J. Wysocki 
102292858c47SRafael J. Wysocki /**
102392858c47SRafael J. Wysocki  * acpi_subsys_freeze - Run the device driver's freeze callback.
102492858c47SRafael J. Wysocki  * @dev: Device to handle.
102592858c47SRafael J. Wysocki  */
102692858c47SRafael J. Wysocki int acpi_subsys_freeze(struct device *dev)
102792858c47SRafael J. Wysocki {
102892858c47SRafael J. Wysocki 	/*
102992858c47SRafael J. Wysocki 	 * This used to be done in acpi_subsys_prepare() for all devices and
103092858c47SRafael J. Wysocki 	 * some drivers may depend on it, so do it here.  Ideally, however,
103192858c47SRafael J. Wysocki 	 * runtime-suspended devices should not be touched during freeze/thaw
103292858c47SRafael J. Wysocki 	 * transitions.
103392858c47SRafael J. Wysocki 	 */
103492858c47SRafael J. Wysocki 	pm_runtime_resume(dev);
103592858c47SRafael J. Wysocki 	return pm_generic_freeze(dev);
103692858c47SRafael J. Wysocki }
10374cf563c5SHeikki Krogerus EXPORT_SYMBOL_GPL(acpi_subsys_freeze);
103892858c47SRafael J. Wysocki 
1039e5cc8ef3SRafael J. Wysocki #endif /* CONFIG_PM_SLEEP */
1040e5cc8ef3SRafael J. Wysocki 
1041e5cc8ef3SRafael J. Wysocki static struct dev_pm_domain acpi_general_pm_domain = {
1042e5cc8ef3SRafael J. Wysocki 	.ops = {
1043e5cc8ef3SRafael J. Wysocki #ifdef CONFIG_PM_RUNTIME
1044e5cc8ef3SRafael J. Wysocki 		.runtime_suspend = acpi_subsys_runtime_suspend,
1045e5cc8ef3SRafael J. Wysocki 		.runtime_resume = acpi_subsys_runtime_resume,
1046e5cc8ef3SRafael J. Wysocki #endif
1047e5cc8ef3SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP
1048e5cc8ef3SRafael J. Wysocki 		.prepare = acpi_subsys_prepare,
1049f25c0ae2SRafael J. Wysocki 		.complete = acpi_subsys_complete,
105092858c47SRafael J. Wysocki 		.suspend = acpi_subsys_suspend,
1051e5cc8ef3SRafael J. Wysocki 		.suspend_late = acpi_subsys_suspend_late,
1052e5cc8ef3SRafael J. Wysocki 		.resume_early = acpi_subsys_resume_early,
105392858c47SRafael J. Wysocki 		.freeze = acpi_subsys_freeze,
105492858c47SRafael J. Wysocki 		.poweroff = acpi_subsys_suspend,
1055e5cc8ef3SRafael J. Wysocki 		.poweroff_late = acpi_subsys_suspend_late,
1056e5cc8ef3SRafael J. Wysocki 		.restore_early = acpi_subsys_resume_early,
1057e5cc8ef3SRafael J. Wysocki #endif
1058e5cc8ef3SRafael J. Wysocki 	},
1059e5cc8ef3SRafael J. Wysocki };
1060e5cc8ef3SRafael J. Wysocki 
1061e5cc8ef3SRafael J. Wysocki /**
1062e5cc8ef3SRafael J. Wysocki  * acpi_dev_pm_attach - Prepare device for ACPI power management.
1063e5cc8ef3SRafael J. Wysocki  * @dev: Device to prepare.
1064b88ce2a4SRafael J. Wysocki  * @power_on: Whether or not to power on the device.
1065e5cc8ef3SRafael J. Wysocki  *
1066e5cc8ef3SRafael J. Wysocki  * If @dev has a valid ACPI handle that has a valid struct acpi_device object
1067e5cc8ef3SRafael J. Wysocki  * attached to it, install a wakeup notification handler for the device and
1068b88ce2a4SRafael J. Wysocki  * add it to the general ACPI PM domain.  If @power_on is set, the device will
1069b88ce2a4SRafael J. Wysocki  * be put into the ACPI D0 state before the function returns.
1070e5cc8ef3SRafael J. Wysocki  *
1071e5cc8ef3SRafael J. Wysocki  * This assumes that the @dev's bus type uses generic power management callbacks
1072e5cc8ef3SRafael J. Wysocki  * (or doesn't use any power management callbacks at all).
1073e5cc8ef3SRafael J. Wysocki  *
1074e5cc8ef3SRafael J. Wysocki  * Callers must ensure proper synchronization of this function with power
1075e5cc8ef3SRafael J. Wysocki  * management callbacks.
1076e5cc8ef3SRafael J. Wysocki  */
1077b88ce2a4SRafael J. Wysocki int acpi_dev_pm_attach(struct device *dev, bool power_on)
1078e5cc8ef3SRafael J. Wysocki {
107979c0373fSRafael J. Wysocki 	struct acpi_device *adev = ACPI_COMPANION(dev);
1080e5cc8ef3SRafael J. Wysocki 
1081e5cc8ef3SRafael J. Wysocki 	if (!adev)
1082e5cc8ef3SRafael J. Wysocki 		return -ENODEV;
1083e5cc8ef3SRafael J. Wysocki 
1084e5cc8ef3SRafael J. Wysocki 	if (dev->pm_domain)
1085e5cc8ef3SRafael J. Wysocki 		return -EEXIST;
1086e5cc8ef3SRafael J. Wysocki 
1087*c072530fSRafael J. Wysocki 	acpi_add_pm_notifier(adev, dev, acpi_pm_notify_work_func);
1088e5cc8ef3SRafael J. Wysocki 	dev->pm_domain = &acpi_general_pm_domain;
1089b88ce2a4SRafael J. Wysocki 	if (power_on) {
1090b88ce2a4SRafael J. Wysocki 		acpi_dev_pm_full_power(adev);
1091b88ce2a4SRafael J. Wysocki 		__acpi_device_run_wake(adev, false);
1092b88ce2a4SRafael J. Wysocki 	}
1093e5cc8ef3SRafael J. Wysocki 	return 0;
1094e5cc8ef3SRafael J. Wysocki }
1095e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_pm_attach);
1096e5cc8ef3SRafael J. Wysocki 
1097e5cc8ef3SRafael J. Wysocki /**
1098e5cc8ef3SRafael J. Wysocki  * acpi_dev_pm_detach - Remove ACPI power management from the device.
1099e5cc8ef3SRafael J. Wysocki  * @dev: Device to take care of.
1100b88ce2a4SRafael J. Wysocki  * @power_off: Whether or not to try to remove power from the device.
1101e5cc8ef3SRafael J. Wysocki  *
1102e5cc8ef3SRafael J. Wysocki  * Remove the device from the general ACPI PM domain and remove its wakeup
1103b88ce2a4SRafael J. Wysocki  * notifier.  If @power_off is set, additionally remove power from the device if
1104b88ce2a4SRafael J. Wysocki  * possible.
1105e5cc8ef3SRafael J. Wysocki  *
1106e5cc8ef3SRafael J. Wysocki  * Callers must ensure proper synchronization of this function with power
1107e5cc8ef3SRafael J. Wysocki  * management callbacks.
1108e5cc8ef3SRafael J. Wysocki  */
1109b88ce2a4SRafael J. Wysocki void acpi_dev_pm_detach(struct device *dev, bool power_off)
1110e5cc8ef3SRafael J. Wysocki {
111179c0373fSRafael J. Wysocki 	struct acpi_device *adev = ACPI_COMPANION(dev);
1112e5cc8ef3SRafael J. Wysocki 
1113e5cc8ef3SRafael J. Wysocki 	if (adev && dev->pm_domain == &acpi_general_pm_domain) {
1114e5cc8ef3SRafael J. Wysocki 		dev->pm_domain = NULL;
1115*c072530fSRafael J. Wysocki 		acpi_remove_pm_notifier(adev);
1116b88ce2a4SRafael J. Wysocki 		if (power_off) {
1117b88ce2a4SRafael J. Wysocki 			/*
1118b88ce2a4SRafael J. Wysocki 			 * If the device's PM QoS resume latency limit or flags
1119b88ce2a4SRafael J. Wysocki 			 * have been exposed to user space, they have to be
1120b88ce2a4SRafael J. Wysocki 			 * hidden at this point, so that they don't affect the
1121b88ce2a4SRafael J. Wysocki 			 * choice of the low-power state to put the device into.
1122b88ce2a4SRafael J. Wysocki 			 */
1123b88ce2a4SRafael J. Wysocki 			dev_pm_qos_hide_latency_limit(dev);
1124b88ce2a4SRafael J. Wysocki 			dev_pm_qos_hide_flags(dev);
1125b88ce2a4SRafael J. Wysocki 			__acpi_device_run_wake(adev, false);
1126b88ce2a4SRafael J. Wysocki 			acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
1127b88ce2a4SRafael J. Wysocki 		}
1128e5cc8ef3SRafael J. Wysocki 	}
1129e5cc8ef3SRafael J. Wysocki }
1130e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_pm_detach);
1131ec4602a9SRafael J. Wysocki #endif /* CONFIG_PM */
1132