xref: /linux/drivers/acpi/device_pm.c (revision 3a83f992490f8235661b768e53bd5f14915420ac)
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;
2599ce4e607SRafael J. Wysocki 
2609ce4e607SRafael J. Wysocki 	result = acpi_device_get_power(device, &state);
2619ce4e607SRafael J. Wysocki 	if (result)
2629ce4e607SRafael J. Wysocki 		return result;
2639ce4e607SRafael J. Wysocki 
264a2367807SRafael J. Wysocki 	if (state < ACPI_STATE_D3_COLD && device->power.flags.power_resources) {
2659ce4e607SRafael J. Wysocki 		result = acpi_power_on_resources(device, state);
266a2367807SRafael J. Wysocki 		if (result)
2679ce4e607SRafael J. Wysocki 			return result;
268a2367807SRafael J. Wysocki 
2699c0f45e3SRafael J. Wysocki 		result = acpi_dev_pm_explicit_set(device, state);
2709c0f45e3SRafael J. Wysocki 		if (result)
2719c0f45e3SRafael J. Wysocki 			return result;
272b3785492SRafael J. Wysocki 	} else if (state == ACPI_STATE_UNKNOWN) {
2737cd8407dSRafael J. Wysocki 		/*
2747cd8407dSRafael J. Wysocki 		 * No power resources and missing _PSC?  Cross fingers and make
2757cd8407dSRafael J. Wysocki 		 * it D0 in hope that this is what the BIOS put the device into.
2767cd8407dSRafael J. Wysocki 		 * [We tried to force D0 here by executing _PS0, but that broke
2777cd8407dSRafael J. Wysocki 		 * Toshiba P870-303 in a nasty way.]
2787cd8407dSRafael J. Wysocki 		 */
279b3785492SRafael J. Wysocki 		state = ACPI_STATE_D0;
280a2367807SRafael J. Wysocki 	}
281a2367807SRafael J. Wysocki 	device->power.state = state;
282a2367807SRafael J. Wysocki 	return 0;
2839ce4e607SRafael J. Wysocki }
2849ce4e607SRafael J. Wysocki 
285b9e95fc6SRafael J. Wysocki /**
286b9e95fc6SRafael J. Wysocki  * acpi_device_fix_up_power - Force device with missing _PSC into D0.
287b9e95fc6SRafael J. Wysocki  * @device: Device object whose power state is to be fixed up.
288b9e95fc6SRafael J. Wysocki  *
289b9e95fc6SRafael J. Wysocki  * Devices without power resources and _PSC, but having _PS0 and _PS3 defined,
290b9e95fc6SRafael J. Wysocki  * are assumed to be put into D0 by the BIOS.  However, in some cases that may
291b9e95fc6SRafael J. Wysocki  * not be the case and this function should be used then.
292b9e95fc6SRafael J. Wysocki  */
293b9e95fc6SRafael J. Wysocki int acpi_device_fix_up_power(struct acpi_device *device)
294b9e95fc6SRafael J. Wysocki {
295b9e95fc6SRafael J. Wysocki 	int ret = 0;
296b9e95fc6SRafael J. Wysocki 
297b9e95fc6SRafael J. Wysocki 	if (!device->power.flags.power_resources
298b9e95fc6SRafael J. Wysocki 	    && !device->power.flags.explicit_get
299b9e95fc6SRafael J. Wysocki 	    && device->power.state == ACPI_STATE_D0)
300b9e95fc6SRafael J. Wysocki 		ret = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0);
301b9e95fc6SRafael J. Wysocki 
302b9e95fc6SRafael J. Wysocki 	return ret;
303b9e95fc6SRafael J. Wysocki }
304b9e95fc6SRafael J. Wysocki 
3059ce4e607SRafael J. Wysocki int acpi_bus_update_power(acpi_handle handle, int *state_p)
3069ce4e607SRafael J. Wysocki {
3079ce4e607SRafael J. Wysocki 	struct acpi_device *device;
3089ce4e607SRafael J. Wysocki 	int state;
3099ce4e607SRafael J. Wysocki 	int result;
3109ce4e607SRafael J. Wysocki 
3119ce4e607SRafael J. Wysocki 	result = acpi_bus_get_device(handle, &device);
3129ce4e607SRafael J. Wysocki 	if (result)
3139ce4e607SRafael J. Wysocki 		return result;
3149ce4e607SRafael J. Wysocki 
3159ce4e607SRafael J. Wysocki 	result = acpi_device_get_power(device, &state);
3169ce4e607SRafael J. Wysocki 	if (result)
3179ce4e607SRafael J. Wysocki 		return result;
3189ce4e607SRafael J. Wysocki 
31991bdad0bSRafael J. Wysocki 	if (state == ACPI_STATE_UNKNOWN) {
320511d5c42SRafael J. Wysocki 		state = ACPI_STATE_D0;
3219ce4e607SRafael J. Wysocki 		result = acpi_device_set_power(device, state);
32291bdad0bSRafael J. Wysocki 		if (result)
32391bdad0bSRafael J. Wysocki 			return result;
32491bdad0bSRafael J. Wysocki 	} else {
32591bdad0bSRafael J. Wysocki 		if (device->power.flags.power_resources) {
32691bdad0bSRafael J. Wysocki 			/*
32791bdad0bSRafael J. Wysocki 			 * We don't need to really switch the state, bu we need
32891bdad0bSRafael J. Wysocki 			 * to update the power resources' reference counters.
32991bdad0bSRafael J. Wysocki 			 */
33091bdad0bSRafael J. Wysocki 			result = acpi_power_transition(device, state);
33191bdad0bSRafael J. Wysocki 			if (result)
33291bdad0bSRafael J. Wysocki 				return result;
33391bdad0bSRafael J. Wysocki 		}
33491bdad0bSRafael J. Wysocki 		device->power.state = state;
33591bdad0bSRafael J. Wysocki 	}
33691bdad0bSRafael J. Wysocki 	if (state_p)
3379ce4e607SRafael J. Wysocki 		*state_p = state;
3389ce4e607SRafael J. Wysocki 
33991bdad0bSRafael J. Wysocki 	return 0;
3409ce4e607SRafael J. Wysocki }
3419ce4e607SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_bus_update_power);
3429ce4e607SRafael J. Wysocki 
3439ce4e607SRafael J. Wysocki bool acpi_bus_power_manageable(acpi_handle handle)
3449ce4e607SRafael J. Wysocki {
3459ce4e607SRafael J. Wysocki 	struct acpi_device *device;
3469ce4e607SRafael J. Wysocki 	int result;
3479ce4e607SRafael J. Wysocki 
3489ce4e607SRafael J. Wysocki 	result = acpi_bus_get_device(handle, &device);
3499ce4e607SRafael J. Wysocki 	return result ? false : device->flags.power_manageable;
3509ce4e607SRafael J. Wysocki }
3519ce4e607SRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_power_manageable);
3529ce4e607SRafael J. Wysocki 
353ec4602a9SRafael J. Wysocki #ifdef CONFIG_PM
354ec4602a9SRafael J. Wysocki static DEFINE_MUTEX(acpi_pm_notifier_lock);
355ec4602a9SRafael J. Wysocki 
356ec4602a9SRafael J. Wysocki /**
357ec4602a9SRafael J. Wysocki  * acpi_add_pm_notifier - Register PM notifier for given ACPI device.
358ec4602a9SRafael J. Wysocki  * @adev: ACPI device to add the notifier for.
359ec4602a9SRafael J. Wysocki  * @context: Context information to pass to the notifier routine.
360ec4602a9SRafael J. Wysocki  *
361ec4602a9SRafael J. Wysocki  * NOTE: @adev need not be a run-wake or wakeup device to be a valid source of
362ec4602a9SRafael J. Wysocki  * PM wakeup events.  For example, wakeup events may be generated for bridges
363ec4602a9SRafael J. Wysocki  * if one of the devices below the bridge is signaling wakeup, even if the
364ec4602a9SRafael J. Wysocki  * bridge itself doesn't have a wakeup GPE associated with it.
365ec4602a9SRafael J. Wysocki  */
366ec4602a9SRafael J. Wysocki acpi_status acpi_add_pm_notifier(struct acpi_device *adev,
367ec4602a9SRafael J. Wysocki 				 acpi_notify_handler handler, void *context)
368ec4602a9SRafael J. Wysocki {
369ec4602a9SRafael J. Wysocki 	acpi_status status = AE_ALREADY_EXISTS;
370ec4602a9SRafael J. Wysocki 
371ec4602a9SRafael J. Wysocki 	mutex_lock(&acpi_pm_notifier_lock);
372ec4602a9SRafael J. Wysocki 
373ec4602a9SRafael J. Wysocki 	if (adev->wakeup.flags.notifier_present)
374ec4602a9SRafael J. Wysocki 		goto out;
375ec4602a9SRafael J. Wysocki 
376ec4602a9SRafael J. Wysocki 	status = acpi_install_notify_handler(adev->handle,
377ec4602a9SRafael J. Wysocki 					     ACPI_SYSTEM_NOTIFY,
378ec4602a9SRafael J. Wysocki 					     handler, context);
379ec4602a9SRafael J. Wysocki 	if (ACPI_FAILURE(status))
380ec4602a9SRafael J. Wysocki 		goto out;
381ec4602a9SRafael J. Wysocki 
382ec4602a9SRafael J. Wysocki 	adev->wakeup.flags.notifier_present = true;
383ec4602a9SRafael J. Wysocki 
384ec4602a9SRafael J. Wysocki  out:
385ec4602a9SRafael J. Wysocki 	mutex_unlock(&acpi_pm_notifier_lock);
386ec4602a9SRafael J. Wysocki 	return status;
387ec4602a9SRafael J. Wysocki }
388ec4602a9SRafael J. Wysocki 
389ec4602a9SRafael J. Wysocki /**
390ec4602a9SRafael J. Wysocki  * acpi_remove_pm_notifier - Unregister PM notifier from given ACPI device.
391ec4602a9SRafael J. Wysocki  * @adev: ACPI device to remove the notifier from.
392ec4602a9SRafael J. Wysocki  */
393ec4602a9SRafael J. Wysocki acpi_status acpi_remove_pm_notifier(struct acpi_device *adev,
394ec4602a9SRafael J. Wysocki 				    acpi_notify_handler handler)
395ec4602a9SRafael J. Wysocki {
396ec4602a9SRafael J. Wysocki 	acpi_status status = AE_BAD_PARAMETER;
397ec4602a9SRafael J. Wysocki 
398ec4602a9SRafael J. Wysocki 	mutex_lock(&acpi_pm_notifier_lock);
399ec4602a9SRafael J. Wysocki 
400ec4602a9SRafael J. Wysocki 	if (!adev->wakeup.flags.notifier_present)
401ec4602a9SRafael J. Wysocki 		goto out;
402ec4602a9SRafael J. Wysocki 
403ec4602a9SRafael J. Wysocki 	status = acpi_remove_notify_handler(adev->handle,
404ec4602a9SRafael J. Wysocki 					    ACPI_SYSTEM_NOTIFY,
405ec4602a9SRafael J. Wysocki 					    handler);
406ec4602a9SRafael J. Wysocki 	if (ACPI_FAILURE(status))
407ec4602a9SRafael J. Wysocki 		goto out;
408ec4602a9SRafael J. Wysocki 
409ec4602a9SRafael J. Wysocki 	adev->wakeup.flags.notifier_present = false;
410ec4602a9SRafael J. Wysocki 
411ec4602a9SRafael J. Wysocki  out:
412ec4602a9SRafael J. Wysocki 	mutex_unlock(&acpi_pm_notifier_lock);
413ec4602a9SRafael J. Wysocki 	return status;
414ec4602a9SRafael J. Wysocki }
415ec4602a9SRafael J. Wysocki 
4169ce4e607SRafael J. Wysocki bool acpi_bus_can_wakeup(acpi_handle handle)
4179ce4e607SRafael J. Wysocki {
4189ce4e607SRafael J. Wysocki 	struct acpi_device *device;
4199ce4e607SRafael J. Wysocki 	int result;
4209ce4e607SRafael J. Wysocki 
4219ce4e607SRafael J. Wysocki 	result = acpi_bus_get_device(handle, &device);
4229ce4e607SRafael J. Wysocki 	return result ? false : device->wakeup.flags.valid;
4239ce4e607SRafael J. Wysocki }
4249ce4e607SRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_can_wakeup);
4259ce4e607SRafael J. Wysocki 
4269ce4e607SRafael J. Wysocki /**
427b25c77efSRafael J. Wysocki  * acpi_dev_pm_get_state - Get preferred power state of ACPI device.
42886b3832cSRafael J. Wysocki  * @dev: Device whose preferred target power state to return.
42986b3832cSRafael J. Wysocki  * @adev: ACPI device node corresponding to @dev.
43086b3832cSRafael J. Wysocki  * @target_state: System state to match the resultant device state.
431fa1675b5SRafael J. Wysocki  * @d_min_p: Location to store the highest power state available to the device.
432fa1675b5SRafael J. Wysocki  * @d_max_p: Location to store the lowest power state available to the device.
43386b3832cSRafael J. Wysocki  *
434fa1675b5SRafael J. Wysocki  * Find the lowest power (highest number) and highest power (lowest number) ACPI
435fa1675b5SRafael J. Wysocki  * device power states that the device can be in while the system is in the
436fa1675b5SRafael J. Wysocki  * state represented by @target_state.  Store the integer numbers representing
437fa1675b5SRafael J. Wysocki  * those stats in the memory locations pointed to by @d_max_p and @d_min_p,
438fa1675b5SRafael J. Wysocki  * respectively.
43986b3832cSRafael J. Wysocki  *
44086b3832cSRafael J. Wysocki  * Callers must ensure that @dev and @adev are valid pointers and that @adev
44186b3832cSRafael J. Wysocki  * actually corresponds to @dev before using this function.
442fa1675b5SRafael J. Wysocki  *
443fa1675b5SRafael J. Wysocki  * Returns 0 on success or -ENODATA when one of the ACPI methods fails or
444fa1675b5SRafael J. Wysocki  * returns a value that doesn't make sense.  The memory locations pointed to by
445fa1675b5SRafael J. Wysocki  * @d_max_p and @d_min_p are only modified on success.
44686b3832cSRafael J. Wysocki  */
447b25c77efSRafael J. Wysocki static int acpi_dev_pm_get_state(struct device *dev, struct acpi_device *adev,
448fa1675b5SRafael J. Wysocki 				 u32 target_state, int *d_min_p, int *d_max_p)
44986b3832cSRafael J. Wysocki {
450fa1675b5SRafael J. Wysocki 	char method[] = { '_', 'S', '0' + target_state, 'D', '\0' };
451fa1675b5SRafael J. Wysocki 	acpi_handle handle = adev->handle;
452fa1675b5SRafael J. Wysocki 	unsigned long long ret;
453fa1675b5SRafael J. Wysocki 	int d_min, d_max;
45486b3832cSRafael J. Wysocki 	bool wakeup = false;
455fa1675b5SRafael J. Wysocki 	acpi_status status;
45686b3832cSRafael J. Wysocki 
45786b3832cSRafael J. Wysocki 	/*
458fa1675b5SRafael J. Wysocki 	 * If the system state is S0, the lowest power state the device can be
459fa1675b5SRafael J. Wysocki 	 * in is D3cold, unless the device has _S0W and is supposed to signal
460fa1675b5SRafael J. Wysocki 	 * wakeup, in which case the return value of _S0W has to be used as the
461fa1675b5SRafael J. Wysocki 	 * lowest power state available to the device.
46286b3832cSRafael J. Wysocki 	 */
46386b3832cSRafael J. Wysocki 	d_min = ACPI_STATE_D0;
4644c164ae7SRafael J. Wysocki 	d_max = ACPI_STATE_D3_COLD;
46586b3832cSRafael J. Wysocki 
46686b3832cSRafael J. Wysocki 	/*
46786b3832cSRafael J. Wysocki 	 * If present, _SxD methods return the minimum D-state (highest power
46886b3832cSRafael J. Wysocki 	 * state) we can use for the corresponding S-states.  Otherwise, the
46986b3832cSRafael J. Wysocki 	 * minimum D-state is D0 (ACPI 3.x).
47086b3832cSRafael J. Wysocki 	 */
47186b3832cSRafael J. Wysocki 	if (target_state > ACPI_STATE_S0) {
472fa1675b5SRafael J. Wysocki 		/*
473fa1675b5SRafael J. Wysocki 		 * We rely on acpi_evaluate_integer() not clobbering the integer
474fa1675b5SRafael J. Wysocki 		 * provided if AE_NOT_FOUND is returned.
475fa1675b5SRafael J. Wysocki 		 */
476fa1675b5SRafael J. Wysocki 		ret = d_min;
477fa1675b5SRafael J. Wysocki 		status = acpi_evaluate_integer(handle, method, NULL, &ret);
478fa1675b5SRafael J. Wysocki 		if ((ACPI_FAILURE(status) && status != AE_NOT_FOUND)
479fa1675b5SRafael J. Wysocki 		    || ret > ACPI_STATE_D3_COLD)
480fa1675b5SRafael J. Wysocki 			return -ENODATA;
481fa1675b5SRafael J. Wysocki 
482fa1675b5SRafael J. Wysocki 		/*
483fa1675b5SRafael J. Wysocki 		 * We need to handle legacy systems where D3hot and D3cold are
484fa1675b5SRafael J. Wysocki 		 * the same and 3 is returned in both cases, so fall back to
485fa1675b5SRafael J. Wysocki 		 * D3cold if D3hot is not a valid state.
486fa1675b5SRafael J. Wysocki 		 */
487fa1675b5SRafael J. Wysocki 		if (!adev->power.states[ret].flags.valid) {
488fa1675b5SRafael J. Wysocki 			if (ret == ACPI_STATE_D3_HOT)
489fa1675b5SRafael J. Wysocki 				ret = ACPI_STATE_D3_COLD;
490fa1675b5SRafael J. Wysocki 			else
491fa1675b5SRafael J. Wysocki 				return -ENODATA;
492fa1675b5SRafael J. Wysocki 		}
493fa1675b5SRafael J. Wysocki 		d_min = ret;
49486b3832cSRafael J. Wysocki 		wakeup = device_may_wakeup(dev) && adev->wakeup.flags.valid
49586b3832cSRafael J. Wysocki 			&& adev->wakeup.sleep_state >= target_state;
49686b3832cSRafael J. Wysocki 	} else if (dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) !=
49786b3832cSRafael J. Wysocki 			PM_QOS_FLAGS_NONE) {
49886b3832cSRafael J. Wysocki 		wakeup = adev->wakeup.flags.valid;
49986b3832cSRafael J. Wysocki 	}
50086b3832cSRafael J. Wysocki 
50186b3832cSRafael J. Wysocki 	/*
50286b3832cSRafael J. Wysocki 	 * If _PRW says we can wake up the system from the target sleep state,
50386b3832cSRafael J. Wysocki 	 * the D-state returned by _SxD is sufficient for that (we assume a
50486b3832cSRafael J. Wysocki 	 * wakeup-aware driver if wake is set).  Still, if _SxW exists
50586b3832cSRafael J. Wysocki 	 * (ACPI 3.x), it should return the maximum (lowest power) D-state that
50686b3832cSRafael J. Wysocki 	 * can wake the system.  _S0W may be valid, too.
50786b3832cSRafael J. Wysocki 	 */
50886b3832cSRafael J. Wysocki 	if (wakeup) {
509fa1675b5SRafael J. Wysocki 		method[3] = 'W';
510fa1675b5SRafael J. Wysocki 		status = acpi_evaluate_integer(handle, method, NULL, &ret);
511fa1675b5SRafael J. Wysocki 		if (status == AE_NOT_FOUND) {
512fa1675b5SRafael J. Wysocki 			if (target_state > ACPI_STATE_S0)
51386b3832cSRafael J. Wysocki 				d_max = d_min;
514fa1675b5SRafael J. Wysocki 		} else if (ACPI_SUCCESS(status) && ret <= ACPI_STATE_D3_COLD) {
515fa1675b5SRafael J. Wysocki 			/* Fall back to D3cold if ret is not a valid state. */
516fa1675b5SRafael J. Wysocki 			if (!adev->power.states[ret].flags.valid)
517fa1675b5SRafael J. Wysocki 				ret = ACPI_STATE_D3_COLD;
518fa1675b5SRafael J. Wysocki 
519fa1675b5SRafael J. Wysocki 			d_max = ret > d_min ? ret : d_min;
520fa1675b5SRafael J. Wysocki 		} else {
521fa1675b5SRafael J. Wysocki 			return -ENODATA;
52286b3832cSRafael J. Wysocki 		}
52386b3832cSRafael J. Wysocki 	}
52486b3832cSRafael J. Wysocki 
52586b3832cSRafael J. Wysocki 	if (d_min_p)
52686b3832cSRafael J. Wysocki 		*d_min_p = d_min;
527fa1675b5SRafael J. Wysocki 
528fa1675b5SRafael J. Wysocki 	if (d_max_p)
529fa1675b5SRafael J. Wysocki 		*d_max_p = d_max;
530fa1675b5SRafael J. Wysocki 
531fa1675b5SRafael J. Wysocki 	return 0;
53286b3832cSRafael J. Wysocki }
533cd7bd02dSRafael J. Wysocki 
534a6ae7594SRafael J. Wysocki /**
535a6ae7594SRafael J. Wysocki  * acpi_pm_device_sleep_state - Get preferred power state of ACPI device.
536a6ae7594SRafael J. Wysocki  * @dev: Device whose preferred target power state to return.
537a6ae7594SRafael J. Wysocki  * @d_min_p: Location to store the upper limit of the allowed states range.
538a6ae7594SRafael J. Wysocki  * @d_max_in: Deepest low-power state to take into consideration.
539a6ae7594SRafael J. Wysocki  * Return value: Preferred power state of the device on success, -ENODEV
540fa1675b5SRafael J. Wysocki  * if there's no 'struct acpi_device' for @dev, -EINVAL if @d_max_in is
541fa1675b5SRafael J. Wysocki  * incorrect, or -ENODATA on ACPI method failure.
542a6ae7594SRafael J. Wysocki  *
543a6ae7594SRafael J. Wysocki  * The caller must ensure that @dev is valid before using this function.
544a6ae7594SRafael J. Wysocki  */
545a6ae7594SRafael J. Wysocki int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in)
546a6ae7594SRafael J. Wysocki {
547*3a83f992SRafael J. Wysocki 	acpi_handle handle = ACPI_HANDLE(dev);
548a6ae7594SRafael J. Wysocki 	struct acpi_device *adev;
5499b5c7a5aSRafael J. Wysocki 	int ret, d_min, d_max;
550fa1675b5SRafael J. Wysocki 
551fa1675b5SRafael J. Wysocki 	if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3_COLD)
552fa1675b5SRafael J. Wysocki 		return -EINVAL;
553fa1675b5SRafael J. Wysocki 
554fa1675b5SRafael J. Wysocki 	if (d_max_in > ACPI_STATE_D3_HOT) {
555fa1675b5SRafael J. Wysocki 		enum pm_qos_flags_status stat;
556fa1675b5SRafael J. Wysocki 
557fa1675b5SRafael J. Wysocki 		stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF);
558fa1675b5SRafael J. Wysocki 		if (stat == PM_QOS_FLAGS_ALL)
559fa1675b5SRafael J. Wysocki 			d_max_in = ACPI_STATE_D3_HOT;
560fa1675b5SRafael J. Wysocki 	}
561a6ae7594SRafael J. Wysocki 
562dde3bb41SYasuaki Ishimatsu 	if (!handle || acpi_bus_get_device(handle, &adev)) {
563a6ae7594SRafael J. Wysocki 		dev_dbg(dev, "ACPI handle without context in %s!\n", __func__);
564a6ae7594SRafael J. Wysocki 		return -ENODEV;
565a6ae7594SRafael J. Wysocki 	}
566a6ae7594SRafael J. Wysocki 
567fa1675b5SRafael J. Wysocki 	ret = acpi_dev_pm_get_state(dev, adev, acpi_target_system_state(),
5689b5c7a5aSRafael J. Wysocki 				    &d_min, &d_max);
569fa1675b5SRafael J. Wysocki 	if (ret)
570fa1675b5SRafael J. Wysocki 		return ret;
571fa1675b5SRafael J. Wysocki 
5729b5c7a5aSRafael J. Wysocki 	if (d_max_in < d_min)
573fa1675b5SRafael J. Wysocki 		return -EINVAL;
574fa1675b5SRafael J. Wysocki 
575fa1675b5SRafael J. Wysocki 	if (d_max > d_max_in) {
5769b5c7a5aSRafael J. Wysocki 		for (d_max = d_max_in; d_max > d_min; d_max--) {
577fa1675b5SRafael J. Wysocki 			if (adev->power.states[d_max].flags.valid)
578fa1675b5SRafael J. Wysocki 				break;
579fa1675b5SRafael J. Wysocki 		}
580fa1675b5SRafael J. Wysocki 	}
5819b5c7a5aSRafael J. Wysocki 
5829b5c7a5aSRafael J. Wysocki 	if (d_min_p)
5839b5c7a5aSRafael J. Wysocki 		*d_min_p = d_min;
5849b5c7a5aSRafael J. Wysocki 
585fa1675b5SRafael J. Wysocki 	return d_max;
586a6ae7594SRafael J. Wysocki }
587a6ae7594SRafael J. Wysocki EXPORT_SYMBOL(acpi_pm_device_sleep_state);
588a6ae7594SRafael J. Wysocki 
589cd7bd02dSRafael J. Wysocki #ifdef CONFIG_PM_RUNTIME
590cd7bd02dSRafael J. Wysocki /**
591e5cc8ef3SRafael J. Wysocki  * acpi_wakeup_device - Wakeup notification handler for ACPI devices.
592e5cc8ef3SRafael J. Wysocki  * @handle: ACPI handle of the device the notification is for.
593e5cc8ef3SRafael J. Wysocki  * @event: Type of the signaled event.
594e5cc8ef3SRafael J. Wysocki  * @context: Device corresponding to @handle.
595e5cc8ef3SRafael J. Wysocki  */
596e5cc8ef3SRafael J. Wysocki static void acpi_wakeup_device(acpi_handle handle, u32 event, void *context)
597e5cc8ef3SRafael J. Wysocki {
598e5cc8ef3SRafael J. Wysocki 	struct device *dev = context;
599e5cc8ef3SRafael J. Wysocki 
600e5cc8ef3SRafael J. Wysocki 	if (event == ACPI_NOTIFY_DEVICE_WAKE && dev) {
601e5cc8ef3SRafael J. Wysocki 		pm_wakeup_event(dev, 0);
602e5cc8ef3SRafael J. Wysocki 		pm_runtime_resume(dev);
603e5cc8ef3SRafael J. Wysocki 	}
604e5cc8ef3SRafael J. Wysocki }
605e5cc8ef3SRafael J. Wysocki 
606e5cc8ef3SRafael J. Wysocki /**
607dee8370cSRafael J. Wysocki  * __acpi_device_run_wake - Enable/disable runtime remote wakeup for device.
608dee8370cSRafael J. Wysocki  * @adev: ACPI device to enable/disable the remote wakeup for.
609cd7bd02dSRafael J. Wysocki  * @enable: Whether to enable or disable the wakeup functionality.
610cd7bd02dSRafael J. Wysocki  *
611dee8370cSRafael J. Wysocki  * Enable/disable the GPE associated with @adev so that it can generate
612dee8370cSRafael J. Wysocki  * wakeup signals for the device in response to external (remote) events and
613dee8370cSRafael J. Wysocki  * enable/disable device wakeup power.
614dee8370cSRafael J. Wysocki  *
615dee8370cSRafael J. Wysocki  * Callers must ensure that @adev is a valid ACPI device node before executing
616dee8370cSRafael J. Wysocki  * this function.
617dee8370cSRafael J. Wysocki  */
618dee8370cSRafael J. Wysocki int __acpi_device_run_wake(struct acpi_device *adev, bool enable)
619dee8370cSRafael J. Wysocki {
620dee8370cSRafael J. Wysocki 	struct acpi_device_wakeup *wakeup = &adev->wakeup;
621dee8370cSRafael J. Wysocki 
622dee8370cSRafael J. Wysocki 	if (enable) {
623dee8370cSRafael J. Wysocki 		acpi_status res;
624dee8370cSRafael J. Wysocki 		int error;
625dee8370cSRafael J. Wysocki 
626dee8370cSRafael J. Wysocki 		error = acpi_enable_wakeup_device_power(adev, ACPI_STATE_S0);
627dee8370cSRafael J. Wysocki 		if (error)
628dee8370cSRafael J. Wysocki 			return error;
629dee8370cSRafael J. Wysocki 
630dee8370cSRafael J. Wysocki 		res = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number);
631dee8370cSRafael J. Wysocki 		if (ACPI_FAILURE(res)) {
632dee8370cSRafael J. Wysocki 			acpi_disable_wakeup_device_power(adev);
633dee8370cSRafael J. Wysocki 			return -EIO;
634dee8370cSRafael J. Wysocki 		}
635dee8370cSRafael J. Wysocki 	} else {
636dee8370cSRafael J. Wysocki 		acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number);
637dee8370cSRafael J. Wysocki 		acpi_disable_wakeup_device_power(adev);
638dee8370cSRafael J. Wysocki 	}
639dee8370cSRafael J. Wysocki 	return 0;
640dee8370cSRafael J. Wysocki }
641dee8370cSRafael J. Wysocki 
642dee8370cSRafael J. Wysocki /**
643dee8370cSRafael J. Wysocki  * acpi_pm_device_run_wake - Enable/disable remote wakeup for given device.
644dee8370cSRafael J. Wysocki  * @dev: Device to enable/disable the platform to wake up.
645dee8370cSRafael J. Wysocki  * @enable: Whether to enable or disable the wakeup functionality.
646cd7bd02dSRafael J. Wysocki  */
647cd7bd02dSRafael J. Wysocki int acpi_pm_device_run_wake(struct device *phys_dev, bool enable)
648cd7bd02dSRafael J. Wysocki {
649dee8370cSRafael J. Wysocki 	struct acpi_device *adev;
650cd7bd02dSRafael J. Wysocki 	acpi_handle handle;
651cd7bd02dSRafael J. Wysocki 
652cd7bd02dSRafael J. Wysocki 	if (!device_run_wake(phys_dev))
653cd7bd02dSRafael J. Wysocki 		return -EINVAL;
654cd7bd02dSRafael J. Wysocki 
655*3a83f992SRafael J. Wysocki 	handle = ACPI_HANDLE(phys_dev);
656dde3bb41SYasuaki Ishimatsu 	if (!handle || acpi_bus_get_device(handle, &adev)) {
657dee8370cSRafael J. Wysocki 		dev_dbg(phys_dev, "ACPI handle without context in %s!\n",
658cd7bd02dSRafael J. Wysocki 			__func__);
659cd7bd02dSRafael J. Wysocki 		return -ENODEV;
660cd7bd02dSRafael J. Wysocki 	}
661cd7bd02dSRafael J. Wysocki 
662dee8370cSRafael J. Wysocki 	return __acpi_device_run_wake(adev, enable);
663cd7bd02dSRafael J. Wysocki }
664cd7bd02dSRafael J. Wysocki EXPORT_SYMBOL(acpi_pm_device_run_wake);
665e5cc8ef3SRafael J. Wysocki #else
666e5cc8ef3SRafael J. Wysocki static inline void acpi_wakeup_device(acpi_handle handle, u32 event,
667e5cc8ef3SRafael J. Wysocki 				      void *context) {}
668cd7bd02dSRafael J. Wysocki #endif /* CONFIG_PM_RUNTIME */
669dee8370cSRafael J. Wysocki 
670dee8370cSRafael J. Wysocki #ifdef CONFIG_PM_SLEEP
671dee8370cSRafael J. Wysocki /**
672dee8370cSRafael J. Wysocki  * __acpi_device_sleep_wake - Enable or disable device to wake up the system.
673dee8370cSRafael J. Wysocki  * @dev: Device to enable/desible to wake up the system.
674dee8370cSRafael J. Wysocki  * @target_state: System state the device is supposed to wake up from.
675dee8370cSRafael J. Wysocki  * @enable: Whether to enable or disable @dev to wake up the system.
676dee8370cSRafael J. Wysocki  */
677dee8370cSRafael J. Wysocki int __acpi_device_sleep_wake(struct acpi_device *adev, u32 target_state,
678dee8370cSRafael J. Wysocki 			     bool enable)
679dee8370cSRafael J. Wysocki {
680dee8370cSRafael J. Wysocki 	return enable ?
681dee8370cSRafael J. Wysocki 		acpi_enable_wakeup_device_power(adev, target_state) :
682dee8370cSRafael J. Wysocki 		acpi_disable_wakeup_device_power(adev);
683dee8370cSRafael J. Wysocki }
684a6ae7594SRafael J. Wysocki 
685a6ae7594SRafael J. Wysocki /**
686a6ae7594SRafael J. Wysocki  * acpi_pm_device_sleep_wake - Enable or disable device to wake up the system.
687a6ae7594SRafael J. Wysocki  * @dev: Device to enable/desible to wake up the system from sleep states.
688a6ae7594SRafael J. Wysocki  * @enable: Whether to enable or disable @dev to wake up the system.
689a6ae7594SRafael J. Wysocki  */
690a6ae7594SRafael J. Wysocki int acpi_pm_device_sleep_wake(struct device *dev, bool enable)
691a6ae7594SRafael J. Wysocki {
692a6ae7594SRafael J. Wysocki 	acpi_handle handle;
693a6ae7594SRafael J. Wysocki 	struct acpi_device *adev;
694a6ae7594SRafael J. Wysocki 	int error;
695a6ae7594SRafael J. Wysocki 
696a6ae7594SRafael J. Wysocki 	if (!device_can_wakeup(dev))
697a6ae7594SRafael J. Wysocki 		return -EINVAL;
698a6ae7594SRafael J. Wysocki 
699*3a83f992SRafael J. Wysocki 	handle = ACPI_HANDLE(dev);
700dde3bb41SYasuaki Ishimatsu 	if (!handle || acpi_bus_get_device(handle, &adev)) {
701a6ae7594SRafael J. Wysocki 		dev_dbg(dev, "ACPI handle without context in %s!\n", __func__);
702a6ae7594SRafael J. Wysocki 		return -ENODEV;
703a6ae7594SRafael J. Wysocki 	}
704a6ae7594SRafael J. Wysocki 
705a6ae7594SRafael J. Wysocki 	error = __acpi_device_sleep_wake(adev, acpi_target_system_state(),
706a6ae7594SRafael J. Wysocki 					 enable);
707a6ae7594SRafael J. Wysocki 	if (!error)
708a6ae7594SRafael J. Wysocki 		dev_info(dev, "System wakeup %s by ACPI\n",
709a6ae7594SRafael J. Wysocki 				enable ? "enabled" : "disabled");
710a6ae7594SRafael J. Wysocki 
711a6ae7594SRafael J. Wysocki 	return error;
712a6ae7594SRafael J. Wysocki }
713dee8370cSRafael J. Wysocki #endif /* CONFIG_PM_SLEEP */
714e5cc8ef3SRafael J. Wysocki 
715e5cc8ef3SRafael J. Wysocki /**
716e5cc8ef3SRafael J. Wysocki  * acpi_dev_pm_get_node - Get ACPI device node for the given physical device.
717e5cc8ef3SRafael J. Wysocki  * @dev: Device to get the ACPI node for.
718e5cc8ef3SRafael J. Wysocki  */
719d2e5f0c1SRafael J. Wysocki struct acpi_device *acpi_dev_pm_get_node(struct device *dev)
720e5cc8ef3SRafael J. Wysocki {
721*3a83f992SRafael J. Wysocki 	acpi_handle handle = ACPI_HANDLE(dev);
722e5cc8ef3SRafael J. Wysocki 	struct acpi_device *adev;
723e5cc8ef3SRafael J. Wysocki 
7245cc36c72SRafael J. Wysocki 	return handle && !acpi_bus_get_device(handle, &adev) ? adev : NULL;
725e5cc8ef3SRafael J. Wysocki }
726e5cc8ef3SRafael J. Wysocki 
727e5cc8ef3SRafael J. Wysocki /**
728e5cc8ef3SRafael J. Wysocki  * acpi_dev_pm_low_power - Put ACPI device into a low-power state.
729e5cc8ef3SRafael J. Wysocki  * @dev: Device to put into a low-power state.
730e5cc8ef3SRafael J. Wysocki  * @adev: ACPI device node corresponding to @dev.
731e5cc8ef3SRafael J. Wysocki  * @system_state: System state to choose the device state for.
732e5cc8ef3SRafael J. Wysocki  */
733e5cc8ef3SRafael J. Wysocki static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev,
734e5cc8ef3SRafael J. Wysocki 				 u32 system_state)
735e5cc8ef3SRafael J. Wysocki {
736fa1675b5SRafael J. Wysocki 	int ret, state;
737e5cc8ef3SRafael J. Wysocki 
738e5cc8ef3SRafael J. Wysocki 	if (!acpi_device_power_manageable(adev))
739e5cc8ef3SRafael J. Wysocki 		return 0;
740e5cc8ef3SRafael J. Wysocki 
741fa1675b5SRafael J. Wysocki 	ret = acpi_dev_pm_get_state(dev, adev, system_state, NULL, &state);
742fa1675b5SRafael J. Wysocki 	return ret ? ret : acpi_device_set_power(adev, state);
743e5cc8ef3SRafael J. Wysocki }
744e5cc8ef3SRafael J. Wysocki 
745e5cc8ef3SRafael J. Wysocki /**
746e5cc8ef3SRafael J. Wysocki  * acpi_dev_pm_full_power - Put ACPI device into the full-power state.
747e5cc8ef3SRafael J. Wysocki  * @adev: ACPI device node to put into the full-power state.
748e5cc8ef3SRafael J. Wysocki  */
749e5cc8ef3SRafael J. Wysocki static int acpi_dev_pm_full_power(struct acpi_device *adev)
750e5cc8ef3SRafael J. Wysocki {
751e5cc8ef3SRafael J. Wysocki 	return acpi_device_power_manageable(adev) ?
752e5cc8ef3SRafael J. Wysocki 		acpi_device_set_power(adev, ACPI_STATE_D0) : 0;
753e5cc8ef3SRafael J. Wysocki }
754e5cc8ef3SRafael J. Wysocki 
755e5cc8ef3SRafael J. Wysocki #ifdef CONFIG_PM_RUNTIME
756e5cc8ef3SRafael J. Wysocki /**
757e5cc8ef3SRafael J. Wysocki  * acpi_dev_runtime_suspend - Put device into a low-power state using ACPI.
758e5cc8ef3SRafael J. Wysocki  * @dev: Device to put into a low-power state.
759e5cc8ef3SRafael J. Wysocki  *
760e5cc8ef3SRafael J. Wysocki  * Put the given device into a runtime low-power state using the standard ACPI
761e5cc8ef3SRafael J. Wysocki  * mechanism.  Set up remote wakeup if desired, choose the state to put the
762e5cc8ef3SRafael J. Wysocki  * device into (this checks if remote wakeup is expected to work too), and set
763e5cc8ef3SRafael J. Wysocki  * the power state of the device.
764e5cc8ef3SRafael J. Wysocki  */
765e5cc8ef3SRafael J. Wysocki int acpi_dev_runtime_suspend(struct device *dev)
766e5cc8ef3SRafael J. Wysocki {
767e5cc8ef3SRafael J. Wysocki 	struct acpi_device *adev = acpi_dev_pm_get_node(dev);
768e5cc8ef3SRafael J. Wysocki 	bool remote_wakeup;
769e5cc8ef3SRafael J. Wysocki 	int error;
770e5cc8ef3SRafael J. Wysocki 
771e5cc8ef3SRafael J. Wysocki 	if (!adev)
772e5cc8ef3SRafael J. Wysocki 		return 0;
773e5cc8ef3SRafael J. Wysocki 
774e5cc8ef3SRafael J. Wysocki 	remote_wakeup = dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) >
775e5cc8ef3SRafael J. Wysocki 				PM_QOS_FLAGS_NONE;
776e5cc8ef3SRafael J. Wysocki 	error = __acpi_device_run_wake(adev, remote_wakeup);
777e5cc8ef3SRafael J. Wysocki 	if (remote_wakeup && error)
778e5cc8ef3SRafael J. Wysocki 		return -EAGAIN;
779e5cc8ef3SRafael J. Wysocki 
780e5cc8ef3SRafael J. Wysocki 	error = acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
781e5cc8ef3SRafael J. Wysocki 	if (error)
782e5cc8ef3SRafael J. Wysocki 		__acpi_device_run_wake(adev, false);
783e5cc8ef3SRafael J. Wysocki 
784e5cc8ef3SRafael J. Wysocki 	return error;
785e5cc8ef3SRafael J. Wysocki }
786e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_runtime_suspend);
787e5cc8ef3SRafael J. Wysocki 
788e5cc8ef3SRafael J. Wysocki /**
789e5cc8ef3SRafael J. Wysocki  * acpi_dev_runtime_resume - Put device into the full-power state using ACPI.
790e5cc8ef3SRafael J. Wysocki  * @dev: Device to put into the full-power state.
791e5cc8ef3SRafael J. Wysocki  *
792e5cc8ef3SRafael J. Wysocki  * Put the given device into the full-power state using the standard ACPI
793e5cc8ef3SRafael J. Wysocki  * mechanism at run time.  Set the power state of the device to ACPI D0 and
794e5cc8ef3SRafael J. Wysocki  * disable remote wakeup.
795e5cc8ef3SRafael J. Wysocki  */
796e5cc8ef3SRafael J. Wysocki int acpi_dev_runtime_resume(struct device *dev)
797e5cc8ef3SRafael J. Wysocki {
798e5cc8ef3SRafael J. Wysocki 	struct acpi_device *adev = acpi_dev_pm_get_node(dev);
799e5cc8ef3SRafael J. Wysocki 	int error;
800e5cc8ef3SRafael J. Wysocki 
801e5cc8ef3SRafael J. Wysocki 	if (!adev)
802e5cc8ef3SRafael J. Wysocki 		return 0;
803e5cc8ef3SRafael J. Wysocki 
804e5cc8ef3SRafael J. Wysocki 	error = acpi_dev_pm_full_power(adev);
805e5cc8ef3SRafael J. Wysocki 	__acpi_device_run_wake(adev, false);
806e5cc8ef3SRafael J. Wysocki 	return error;
807e5cc8ef3SRafael J. Wysocki }
808e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_runtime_resume);
809e5cc8ef3SRafael J. Wysocki 
810e5cc8ef3SRafael J. Wysocki /**
811e5cc8ef3SRafael J. Wysocki  * acpi_subsys_runtime_suspend - Suspend device using ACPI.
812e5cc8ef3SRafael J. Wysocki  * @dev: Device to suspend.
813e5cc8ef3SRafael J. Wysocki  *
814e5cc8ef3SRafael J. Wysocki  * Carry out the generic runtime suspend procedure for @dev and use ACPI to put
815e5cc8ef3SRafael J. Wysocki  * it into a runtime low-power state.
816e5cc8ef3SRafael J. Wysocki  */
817e5cc8ef3SRafael J. Wysocki int acpi_subsys_runtime_suspend(struct device *dev)
818e5cc8ef3SRafael J. Wysocki {
819e5cc8ef3SRafael J. Wysocki 	int ret = pm_generic_runtime_suspend(dev);
820e5cc8ef3SRafael J. Wysocki 	return ret ? ret : acpi_dev_runtime_suspend(dev);
821e5cc8ef3SRafael J. Wysocki }
822e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend);
823e5cc8ef3SRafael J. Wysocki 
824e5cc8ef3SRafael J. Wysocki /**
825e5cc8ef3SRafael J. Wysocki  * acpi_subsys_runtime_resume - Resume device using ACPI.
826e5cc8ef3SRafael J. Wysocki  * @dev: Device to Resume.
827e5cc8ef3SRafael J. Wysocki  *
828e5cc8ef3SRafael J. Wysocki  * Use ACPI to put the given device into the full-power state and carry out the
829e5cc8ef3SRafael J. Wysocki  * generic runtime resume procedure for it.
830e5cc8ef3SRafael J. Wysocki  */
831e5cc8ef3SRafael J. Wysocki int acpi_subsys_runtime_resume(struct device *dev)
832e5cc8ef3SRafael J. Wysocki {
833e5cc8ef3SRafael J. Wysocki 	int ret = acpi_dev_runtime_resume(dev);
834e5cc8ef3SRafael J. Wysocki 	return ret ? ret : pm_generic_runtime_resume(dev);
835e5cc8ef3SRafael J. Wysocki }
836e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume);
837e5cc8ef3SRafael J. Wysocki #endif /* CONFIG_PM_RUNTIME */
838e5cc8ef3SRafael J. Wysocki 
839e5cc8ef3SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP
840e5cc8ef3SRafael J. Wysocki /**
841e5cc8ef3SRafael J. Wysocki  * acpi_dev_suspend_late - Put device into a low-power state using ACPI.
842e5cc8ef3SRafael J. Wysocki  * @dev: Device to put into a low-power state.
843e5cc8ef3SRafael J. Wysocki  *
844e5cc8ef3SRafael J. Wysocki  * Put the given device into a low-power state during system transition to a
845e5cc8ef3SRafael J. Wysocki  * sleep state using the standard ACPI mechanism.  Set up system wakeup if
846e5cc8ef3SRafael J. Wysocki  * desired, choose the state to put the device into (this checks if system
847e5cc8ef3SRafael J. Wysocki  * wakeup is expected to work too), and set the power state of the device.
848e5cc8ef3SRafael J. Wysocki  */
849e5cc8ef3SRafael J. Wysocki int acpi_dev_suspend_late(struct device *dev)
850e5cc8ef3SRafael J. Wysocki {
851e5cc8ef3SRafael J. Wysocki 	struct acpi_device *adev = acpi_dev_pm_get_node(dev);
852e5cc8ef3SRafael J. Wysocki 	u32 target_state;
853e5cc8ef3SRafael J. Wysocki 	bool wakeup;
854e5cc8ef3SRafael J. Wysocki 	int error;
855e5cc8ef3SRafael J. Wysocki 
856e5cc8ef3SRafael J. Wysocki 	if (!adev)
857e5cc8ef3SRafael J. Wysocki 		return 0;
858e5cc8ef3SRafael J. Wysocki 
859e5cc8ef3SRafael J. Wysocki 	target_state = acpi_target_system_state();
860e5cc8ef3SRafael J. Wysocki 	wakeup = device_may_wakeup(dev);
861e5cc8ef3SRafael J. Wysocki 	error = __acpi_device_sleep_wake(adev, target_state, wakeup);
862e5cc8ef3SRafael J. Wysocki 	if (wakeup && error)
863e5cc8ef3SRafael J. Wysocki 		return error;
864e5cc8ef3SRafael J. Wysocki 
865e5cc8ef3SRafael J. Wysocki 	error = acpi_dev_pm_low_power(dev, adev, target_state);
866e5cc8ef3SRafael J. Wysocki 	if (error)
867e5cc8ef3SRafael J. Wysocki 		__acpi_device_sleep_wake(adev, ACPI_STATE_UNKNOWN, false);
868e5cc8ef3SRafael J. Wysocki 
869e5cc8ef3SRafael J. Wysocki 	return error;
870e5cc8ef3SRafael J. Wysocki }
871e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_suspend_late);
872e5cc8ef3SRafael J. Wysocki 
873e5cc8ef3SRafael J. Wysocki /**
874e5cc8ef3SRafael J. Wysocki  * acpi_dev_resume_early - Put device into the full-power state using ACPI.
875e5cc8ef3SRafael J. Wysocki  * @dev: Device to put into the full-power state.
876e5cc8ef3SRafael J. Wysocki  *
877e5cc8ef3SRafael J. Wysocki  * Put the given device into the full-power state using the standard ACPI
878e5cc8ef3SRafael J. Wysocki  * mechanism during system transition to the working state.  Set the power
879e5cc8ef3SRafael J. Wysocki  * state of the device to ACPI D0 and disable remote wakeup.
880e5cc8ef3SRafael J. Wysocki  */
881e5cc8ef3SRafael J. Wysocki int acpi_dev_resume_early(struct device *dev)
882e5cc8ef3SRafael J. Wysocki {
883e5cc8ef3SRafael J. Wysocki 	struct acpi_device *adev = acpi_dev_pm_get_node(dev);
884e5cc8ef3SRafael J. Wysocki 	int error;
885e5cc8ef3SRafael J. Wysocki 
886e5cc8ef3SRafael J. Wysocki 	if (!adev)
887e5cc8ef3SRafael J. Wysocki 		return 0;
888e5cc8ef3SRafael J. Wysocki 
889e5cc8ef3SRafael J. Wysocki 	error = acpi_dev_pm_full_power(adev);
890e5cc8ef3SRafael J. Wysocki 	__acpi_device_sleep_wake(adev, ACPI_STATE_UNKNOWN, false);
891e5cc8ef3SRafael J. Wysocki 	return error;
892e5cc8ef3SRafael J. Wysocki }
893e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_resume_early);
894e5cc8ef3SRafael J. Wysocki 
895e5cc8ef3SRafael J. Wysocki /**
896e5cc8ef3SRafael J. Wysocki  * acpi_subsys_prepare - Prepare device for system transition to a sleep state.
897e5cc8ef3SRafael J. Wysocki  * @dev: Device to prepare.
898e5cc8ef3SRafael J. Wysocki  */
899e5cc8ef3SRafael J. Wysocki int acpi_subsys_prepare(struct device *dev)
900e5cc8ef3SRafael J. Wysocki {
901e5cc8ef3SRafael J. Wysocki 	/*
902e5cc8ef3SRafael J. Wysocki 	 * Follow PCI and resume devices suspended at run time before running
903e5cc8ef3SRafael J. Wysocki 	 * their system suspend callbacks.
904e5cc8ef3SRafael J. Wysocki 	 */
905e5cc8ef3SRafael J. Wysocki 	pm_runtime_resume(dev);
906e5cc8ef3SRafael J. Wysocki 	return pm_generic_prepare(dev);
907e5cc8ef3SRafael J. Wysocki }
908e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_prepare);
909e5cc8ef3SRafael J. Wysocki 
910e5cc8ef3SRafael J. Wysocki /**
911e5cc8ef3SRafael J. Wysocki  * acpi_subsys_suspend_late - Suspend device using ACPI.
912e5cc8ef3SRafael J. Wysocki  * @dev: Device to suspend.
913e5cc8ef3SRafael J. Wysocki  *
914e5cc8ef3SRafael J. Wysocki  * Carry out the generic late suspend procedure for @dev and use ACPI to put
915e5cc8ef3SRafael J. Wysocki  * it into a low-power state during system transition into a sleep state.
916e5cc8ef3SRafael J. Wysocki  */
917e5cc8ef3SRafael J. Wysocki int acpi_subsys_suspend_late(struct device *dev)
918e5cc8ef3SRafael J. Wysocki {
919e5cc8ef3SRafael J. Wysocki 	int ret = pm_generic_suspend_late(dev);
920e5cc8ef3SRafael J. Wysocki 	return ret ? ret : acpi_dev_suspend_late(dev);
921e5cc8ef3SRafael J. Wysocki }
922e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late);
923e5cc8ef3SRafael J. Wysocki 
924e5cc8ef3SRafael J. Wysocki /**
925e5cc8ef3SRafael J. Wysocki  * acpi_subsys_resume_early - Resume device using ACPI.
926e5cc8ef3SRafael J. Wysocki  * @dev: Device to Resume.
927e5cc8ef3SRafael J. Wysocki  *
928e5cc8ef3SRafael J. Wysocki  * Use ACPI to put the given device into the full-power state and carry out the
929e5cc8ef3SRafael J. Wysocki  * generic early resume procedure for it during system transition into the
930e5cc8ef3SRafael J. Wysocki  * working state.
931e5cc8ef3SRafael J. Wysocki  */
932e5cc8ef3SRafael J. Wysocki int acpi_subsys_resume_early(struct device *dev)
933e5cc8ef3SRafael J. Wysocki {
934e5cc8ef3SRafael J. Wysocki 	int ret = acpi_dev_resume_early(dev);
935e5cc8ef3SRafael J. Wysocki 	return ret ? ret : pm_generic_resume_early(dev);
936e5cc8ef3SRafael J. Wysocki }
937e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_resume_early);
938e5cc8ef3SRafael J. Wysocki #endif /* CONFIG_PM_SLEEP */
939e5cc8ef3SRafael J. Wysocki 
940e5cc8ef3SRafael J. Wysocki static struct dev_pm_domain acpi_general_pm_domain = {
941e5cc8ef3SRafael J. Wysocki 	.ops = {
942e5cc8ef3SRafael J. Wysocki #ifdef CONFIG_PM_RUNTIME
943e5cc8ef3SRafael J. Wysocki 		.runtime_suspend = acpi_subsys_runtime_suspend,
944e5cc8ef3SRafael J. Wysocki 		.runtime_resume = acpi_subsys_runtime_resume,
945e5cc8ef3SRafael J. Wysocki #endif
946e5cc8ef3SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP
947e5cc8ef3SRafael J. Wysocki 		.prepare = acpi_subsys_prepare,
948e5cc8ef3SRafael J. Wysocki 		.suspend_late = acpi_subsys_suspend_late,
949e5cc8ef3SRafael J. Wysocki 		.resume_early = acpi_subsys_resume_early,
950e5cc8ef3SRafael J. Wysocki 		.poweroff_late = acpi_subsys_suspend_late,
951e5cc8ef3SRafael J. Wysocki 		.restore_early = acpi_subsys_resume_early,
952e5cc8ef3SRafael J. Wysocki #endif
953e5cc8ef3SRafael J. Wysocki 	},
954e5cc8ef3SRafael J. Wysocki };
955e5cc8ef3SRafael J. Wysocki 
956e5cc8ef3SRafael J. Wysocki /**
957e5cc8ef3SRafael J. Wysocki  * acpi_dev_pm_attach - Prepare device for ACPI power management.
958e5cc8ef3SRafael J. Wysocki  * @dev: Device to prepare.
959b88ce2a4SRafael J. Wysocki  * @power_on: Whether or not to power on the device.
960e5cc8ef3SRafael J. Wysocki  *
961e5cc8ef3SRafael J. Wysocki  * If @dev has a valid ACPI handle that has a valid struct acpi_device object
962e5cc8ef3SRafael J. Wysocki  * attached to it, install a wakeup notification handler for the device and
963b88ce2a4SRafael J. Wysocki  * add it to the general ACPI PM domain.  If @power_on is set, the device will
964b88ce2a4SRafael J. Wysocki  * be put into the ACPI D0 state before the function returns.
965e5cc8ef3SRafael J. Wysocki  *
966e5cc8ef3SRafael J. Wysocki  * This assumes that the @dev's bus type uses generic power management callbacks
967e5cc8ef3SRafael J. Wysocki  * (or doesn't use any power management callbacks at all).
968e5cc8ef3SRafael J. Wysocki  *
969e5cc8ef3SRafael J. Wysocki  * Callers must ensure proper synchronization of this function with power
970e5cc8ef3SRafael J. Wysocki  * management callbacks.
971e5cc8ef3SRafael J. Wysocki  */
972b88ce2a4SRafael J. Wysocki int acpi_dev_pm_attach(struct device *dev, bool power_on)
973e5cc8ef3SRafael J. Wysocki {
974e5cc8ef3SRafael J. Wysocki 	struct acpi_device *adev = acpi_dev_pm_get_node(dev);
975e5cc8ef3SRafael J. Wysocki 
976e5cc8ef3SRafael J. Wysocki 	if (!adev)
977e5cc8ef3SRafael J. Wysocki 		return -ENODEV;
978e5cc8ef3SRafael J. Wysocki 
979e5cc8ef3SRafael J. Wysocki 	if (dev->pm_domain)
980e5cc8ef3SRafael J. Wysocki 		return -EEXIST;
981e5cc8ef3SRafael J. Wysocki 
982e5cc8ef3SRafael J. Wysocki 	acpi_add_pm_notifier(adev, acpi_wakeup_device, dev);
983e5cc8ef3SRafael J. Wysocki 	dev->pm_domain = &acpi_general_pm_domain;
984b88ce2a4SRafael J. Wysocki 	if (power_on) {
985b88ce2a4SRafael J. Wysocki 		acpi_dev_pm_full_power(adev);
986b88ce2a4SRafael J. Wysocki 		__acpi_device_run_wake(adev, false);
987b88ce2a4SRafael J. Wysocki 	}
988e5cc8ef3SRafael J. Wysocki 	return 0;
989e5cc8ef3SRafael J. Wysocki }
990e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_pm_attach);
991e5cc8ef3SRafael J. Wysocki 
992e5cc8ef3SRafael J. Wysocki /**
993e5cc8ef3SRafael J. Wysocki  * acpi_dev_pm_detach - Remove ACPI power management from the device.
994e5cc8ef3SRafael J. Wysocki  * @dev: Device to take care of.
995b88ce2a4SRafael J. Wysocki  * @power_off: Whether or not to try to remove power from the device.
996e5cc8ef3SRafael J. Wysocki  *
997e5cc8ef3SRafael J. Wysocki  * Remove the device from the general ACPI PM domain and remove its wakeup
998b88ce2a4SRafael J. Wysocki  * notifier.  If @power_off is set, additionally remove power from the device if
999b88ce2a4SRafael J. Wysocki  * possible.
1000e5cc8ef3SRafael J. Wysocki  *
1001e5cc8ef3SRafael J. Wysocki  * Callers must ensure proper synchronization of this function with power
1002e5cc8ef3SRafael J. Wysocki  * management callbacks.
1003e5cc8ef3SRafael J. Wysocki  */
1004b88ce2a4SRafael J. Wysocki void acpi_dev_pm_detach(struct device *dev, bool power_off)
1005e5cc8ef3SRafael J. Wysocki {
1006e5cc8ef3SRafael J. Wysocki 	struct acpi_device *adev = acpi_dev_pm_get_node(dev);
1007e5cc8ef3SRafael J. Wysocki 
1008e5cc8ef3SRafael J. Wysocki 	if (adev && dev->pm_domain == &acpi_general_pm_domain) {
1009e5cc8ef3SRafael J. Wysocki 		dev->pm_domain = NULL;
1010e5cc8ef3SRafael J. Wysocki 		acpi_remove_pm_notifier(adev, acpi_wakeup_device);
1011b88ce2a4SRafael J. Wysocki 		if (power_off) {
1012b88ce2a4SRafael J. Wysocki 			/*
1013b88ce2a4SRafael J. Wysocki 			 * If the device's PM QoS resume latency limit or flags
1014b88ce2a4SRafael J. Wysocki 			 * have been exposed to user space, they have to be
1015b88ce2a4SRafael J. Wysocki 			 * hidden at this point, so that they don't affect the
1016b88ce2a4SRafael J. Wysocki 			 * choice of the low-power state to put the device into.
1017b88ce2a4SRafael J. Wysocki 			 */
1018b88ce2a4SRafael J. Wysocki 			dev_pm_qos_hide_latency_limit(dev);
1019b88ce2a4SRafael J. Wysocki 			dev_pm_qos_hide_flags(dev);
1020b88ce2a4SRafael J. Wysocki 			__acpi_device_run_wake(adev, false);
1021b88ce2a4SRafael J. Wysocki 			acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
1022b88ce2a4SRafael J. Wysocki 		}
1023e5cc8ef3SRafael J. Wysocki 	}
1024e5cc8ef3SRafael J. Wysocki }
1025e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_pm_detach);
1026ec4602a9SRafael J. Wysocki #endif /* CONFIG_PM */
1027