xref: /linux/drivers/acpi/device_pm.c (revision e4da817d2acbd05217adc0dc821bc8361e86ee30)
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  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19ec2cd81cSRafael J. Wysocki  */
20ec2cd81cSRafael J. Wysocki 
217b199811SRafael J. Wysocki #include <linux/acpi.h>
2286b3832cSRafael J. Wysocki #include <linux/export.h>
23ec2cd81cSRafael J. Wysocki #include <linux/mutex.h>
2486b3832cSRafael J. Wysocki #include <linux/pm_qos.h>
25989561deSTomeu Vizoso #include <linux/pm_domain.h>
26cd7bd02dSRafael J. Wysocki #include <linux/pm_runtime.h>
2733e4f80eSRafael J. Wysocki #include <linux/suspend.h>
28ec2cd81cSRafael J. Wysocki 
299ce4e607SRafael J. Wysocki #include "internal.h"
309ce4e607SRafael J. Wysocki 
319ce4e607SRafael J. Wysocki #define _COMPONENT	ACPI_POWER_COMPONENT
329ce4e607SRafael J. Wysocki ACPI_MODULE_NAME("device_pm");
33ec2cd81cSRafael J. Wysocki 
3486b3832cSRafael J. Wysocki /**
359ce4e607SRafael J. Wysocki  * acpi_power_state_string - String representation of ACPI device power state.
369ce4e607SRafael J. Wysocki  * @state: ACPI device power state to return the string representation of.
379ce4e607SRafael J. Wysocki  */
389ce4e607SRafael J. Wysocki const char *acpi_power_state_string(int state)
399ce4e607SRafael J. Wysocki {
409ce4e607SRafael J. Wysocki 	switch (state) {
419ce4e607SRafael J. Wysocki 	case ACPI_STATE_D0:
429ce4e607SRafael J. Wysocki 		return "D0";
439ce4e607SRafael J. Wysocki 	case ACPI_STATE_D1:
449ce4e607SRafael J. Wysocki 		return "D1";
459ce4e607SRafael J. Wysocki 	case ACPI_STATE_D2:
469ce4e607SRafael J. Wysocki 		return "D2";
479ce4e607SRafael J. Wysocki 	case ACPI_STATE_D3_HOT:
489ce4e607SRafael J. Wysocki 		return "D3hot";
499ce4e607SRafael J. Wysocki 	case ACPI_STATE_D3_COLD:
50898fee4fSRafael J. Wysocki 		return "D3cold";
519ce4e607SRafael J. Wysocki 	default:
529ce4e607SRafael J. Wysocki 		return "(unknown)";
539ce4e607SRafael J. Wysocki 	}
549ce4e607SRafael J. Wysocki }
559ce4e607SRafael J. Wysocki 
569ce4e607SRafael J. Wysocki /**
579ce4e607SRafael J. Wysocki  * acpi_device_get_power - Get power state of an ACPI device.
589ce4e607SRafael J. Wysocki  * @device: Device to get the power state of.
599ce4e607SRafael J. Wysocki  * @state: Place to store the power state of the device.
609ce4e607SRafael J. Wysocki  *
619ce4e607SRafael J. Wysocki  * This function does not update the device's power.state field, but it may
629ce4e607SRafael J. Wysocki  * update its parent's power.state field (when the parent's power state is
639ce4e607SRafael J. Wysocki  * unknown and the device's power state turns out to be D0).
649ce4e607SRafael J. Wysocki  */
659ce4e607SRafael J. Wysocki int acpi_device_get_power(struct acpi_device *device, int *state)
669ce4e607SRafael J. Wysocki {
679ce4e607SRafael J. Wysocki 	int result = ACPI_STATE_UNKNOWN;
689ce4e607SRafael J. Wysocki 
699ce4e607SRafael J. Wysocki 	if (!device || !state)
709ce4e607SRafael J. Wysocki 		return -EINVAL;
719ce4e607SRafael J. Wysocki 
729ce4e607SRafael J. Wysocki 	if (!device->flags.power_manageable) {
739ce4e607SRafael J. Wysocki 		/* TBD: Non-recursive algorithm for walking up hierarchy. */
749ce4e607SRafael J. Wysocki 		*state = device->parent ?
759ce4e607SRafael J. Wysocki 			device->parent->power.state : ACPI_STATE_D0;
769ce4e607SRafael J. Wysocki 		goto out;
779ce4e607SRafael J. Wysocki 	}
789ce4e607SRafael J. Wysocki 
799ce4e607SRafael J. Wysocki 	/*
8075eb2d13SRafael J. Wysocki 	 * Get the device's power state from power resources settings and _PSC,
8175eb2d13SRafael J. Wysocki 	 * if available.
829ce4e607SRafael J. Wysocki 	 */
8375eb2d13SRafael J. Wysocki 	if (device->power.flags.power_resources) {
849ce4e607SRafael J. Wysocki 		int error = acpi_power_get_inferred_state(device, &result);
859ce4e607SRafael J. Wysocki 		if (error)
869ce4e607SRafael J. Wysocki 			return error;
8775eb2d13SRafael J. Wysocki 	}
8875eb2d13SRafael J. Wysocki 	if (device->power.flags.explicit_get) {
8975eb2d13SRafael J. Wysocki 		acpi_handle handle = device->handle;
9075eb2d13SRafael J. Wysocki 		unsigned long long psc;
9175eb2d13SRafael J. Wysocki 		acpi_status status;
9275eb2d13SRafael J. Wysocki 
9375eb2d13SRafael J. Wysocki 		status = acpi_evaluate_integer(handle, "_PSC", NULL, &psc);
9475eb2d13SRafael J. Wysocki 		if (ACPI_FAILURE(status))
9575eb2d13SRafael J. Wysocki 			return -ENODEV;
9675eb2d13SRafael J. Wysocki 
9775eb2d13SRafael J. Wysocki 		/*
9875eb2d13SRafael J. Wysocki 		 * The power resources settings may indicate a power state
9920dacb71SRafael J. Wysocki 		 * shallower than the actual power state of the device, because
10020dacb71SRafael J. Wysocki 		 * the same power resources may be referenced by other devices.
10175eb2d13SRafael J. Wysocki 		 *
10220dacb71SRafael J. Wysocki 		 * For systems predating ACPI 4.0 we assume that D3hot is the
10320dacb71SRafael J. Wysocki 		 * deepest state that can be supported.
10475eb2d13SRafael J. Wysocki 		 */
10575eb2d13SRafael J. Wysocki 		if (psc > result && psc < ACPI_STATE_D3_COLD)
10675eb2d13SRafael J. Wysocki 			result = psc;
10775eb2d13SRafael J. Wysocki 		else if (result == ACPI_STATE_UNKNOWN)
10820dacb71SRafael J. Wysocki 			result = psc > ACPI_STATE_D2 ? ACPI_STATE_D3_HOT : psc;
1099ce4e607SRafael J. Wysocki 	}
1109ce4e607SRafael J. Wysocki 
1119ce4e607SRafael J. Wysocki 	/*
1129ce4e607SRafael J. Wysocki 	 * If we were unsure about the device parent's power state up to this
1139ce4e607SRafael J. Wysocki 	 * point, the fact that the device is in D0 implies that the parent has
114644f17adSMika Westerberg 	 * to be in D0 too, except if ignore_parent is set.
1159ce4e607SRafael J. Wysocki 	 */
116644f17adSMika Westerberg 	if (!device->power.flags.ignore_parent && device->parent
117644f17adSMika Westerberg 	    && device->parent->power.state == ACPI_STATE_UNKNOWN
1189ce4e607SRafael J. Wysocki 	    && result == ACPI_STATE_D0)
1199ce4e607SRafael J. Wysocki 		device->parent->power.state = ACPI_STATE_D0;
1209ce4e607SRafael J. Wysocki 
1219ce4e607SRafael J. Wysocki 	*state = result;
1229ce4e607SRafael J. Wysocki 
1239ce4e607SRafael J. Wysocki  out:
1249ce4e607SRafael J. Wysocki 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] power state is %s\n",
1259ce4e607SRafael J. Wysocki 			  device->pnp.bus_id, acpi_power_state_string(*state)));
1269ce4e607SRafael J. Wysocki 
1279ce4e607SRafael J. Wysocki 	return 0;
1289ce4e607SRafael J. Wysocki }
1299ce4e607SRafael J. Wysocki 
1309c0f45e3SRafael J. Wysocki static int acpi_dev_pm_explicit_set(struct acpi_device *adev, int state)
1319c0f45e3SRafael J. Wysocki {
1329c0f45e3SRafael J. Wysocki 	if (adev->power.states[state].flags.explicit_set) {
1339c0f45e3SRafael J. Wysocki 		char method[5] = { '_', 'P', 'S', '0' + state, '\0' };
1349c0f45e3SRafael J. Wysocki 		acpi_status status;
1359c0f45e3SRafael J. Wysocki 
1369c0f45e3SRafael J. Wysocki 		status = acpi_evaluate_object(adev->handle, method, NULL, NULL);
1379c0f45e3SRafael J. Wysocki 		if (ACPI_FAILURE(status))
1389c0f45e3SRafael J. Wysocki 			return -ENODEV;
1399c0f45e3SRafael J. Wysocki 	}
1409c0f45e3SRafael J. Wysocki 	return 0;
1419c0f45e3SRafael J. Wysocki }
1429c0f45e3SRafael J. Wysocki 
1439ce4e607SRafael J. Wysocki /**
1449ce4e607SRafael J. Wysocki  * acpi_device_set_power - Set power state of an ACPI device.
1459ce4e607SRafael J. Wysocki  * @device: Device to set the power state of.
1469ce4e607SRafael J. Wysocki  * @state: New power state to set.
1479ce4e607SRafael J. Wysocki  *
1489ce4e607SRafael J. Wysocki  * Callers must ensure that the device is power manageable before using this
1499ce4e607SRafael J. Wysocki  * function.
1509ce4e607SRafael J. Wysocki  */
1519ce4e607SRafael J. Wysocki int acpi_device_set_power(struct acpi_device *device, int state)
1529ce4e607SRafael J. Wysocki {
15320dacb71SRafael J. Wysocki 	int target_state = state;
1549ce4e607SRafael J. Wysocki 	int result = 0;
1559ce4e607SRafael J. Wysocki 
1562c7d132aSRafael J. Wysocki 	if (!device || !device->flags.power_manageable
1572c7d132aSRafael J. Wysocki 	    || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
1589ce4e607SRafael J. Wysocki 		return -EINVAL;
1599ce4e607SRafael J. Wysocki 
1609ce4e607SRafael J. Wysocki 	/* Make sure this is a valid target state */
1619ce4e607SRafael J. Wysocki 
1629ce4e607SRafael J. Wysocki 	if (state == device->power.state) {
163b69137a7SRafael J. Wysocki 		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] already in %s\n",
164b69137a7SRafael J. Wysocki 				  device->pnp.bus_id,
1659ce4e607SRafael J. Wysocki 				  acpi_power_state_string(state)));
1669ce4e607SRafael J. Wysocki 		return 0;
1679ce4e607SRafael J. Wysocki 	}
1689ce4e607SRafael J. Wysocki 
16920dacb71SRafael J. Wysocki 	if (state == ACPI_STATE_D3_COLD) {
17020dacb71SRafael J. Wysocki 		/*
17120dacb71SRafael J. Wysocki 		 * For transitions to D3cold we need to execute _PS3 and then
17220dacb71SRafael J. Wysocki 		 * possibly drop references to the power resources in use.
17320dacb71SRafael J. Wysocki 		 */
17420dacb71SRafael J. Wysocki 		state = ACPI_STATE_D3_HOT;
17520dacb71SRafael J. Wysocki 		/* If _PR3 is not available, use D3hot as the target state. */
17620dacb71SRafael J. Wysocki 		if (!device->power.states[ACPI_STATE_D3_COLD].flags.valid)
17720dacb71SRafael J. Wysocki 			target_state = state;
17820dacb71SRafael J. Wysocki 	} else if (!device->power.states[state].flags.valid) {
179b69137a7SRafael J. Wysocki 		dev_warn(&device->dev, "Power state %s not supported\n",
1809ce4e607SRafael J. Wysocki 			 acpi_power_state_string(state));
1819ce4e607SRafael J. Wysocki 		return -ENODEV;
1829ce4e607SRafael J. Wysocki 	}
18320dacb71SRafael J. Wysocki 
184644f17adSMika Westerberg 	if (!device->power.flags.ignore_parent &&
185644f17adSMika Westerberg 	    device->parent && (state < device->parent->power.state)) {
186b69137a7SRafael J. Wysocki 		dev_warn(&device->dev,
187593298e6SAaron Lu 			 "Cannot transition to power state %s for parent in %s\n",
188593298e6SAaron Lu 			 acpi_power_state_string(state),
189593298e6SAaron Lu 			 acpi_power_state_string(device->parent->power.state));
1909ce4e607SRafael J. Wysocki 		return -ENODEV;
1919ce4e607SRafael J. Wysocki 	}
1929ce4e607SRafael J. Wysocki 
193e78adb75SRafael J. Wysocki 	/*
194e78adb75SRafael J. Wysocki 	 * Transition Power
195e78adb75SRafael J. Wysocki 	 * ----------------
19620dacb71SRafael J. Wysocki 	 * In accordance with ACPI 6, _PSx is executed before manipulating power
19720dacb71SRafael J. Wysocki 	 * resources, unless the target state is D0, in which case _PS0 is
19820dacb71SRafael J. Wysocki 	 * supposed to be executed after turning the power resources on.
199e78adb75SRafael J. Wysocki 	 */
20020dacb71SRafael J. Wysocki 	if (state > ACPI_STATE_D0) {
20120dacb71SRafael J. Wysocki 		/*
20220dacb71SRafael J. Wysocki 		 * According to ACPI 6, devices cannot go from lower-power
20320dacb71SRafael J. Wysocki 		 * (deeper) states to higher-power (shallower) states.
20420dacb71SRafael J. Wysocki 		 */
20520dacb71SRafael J. Wysocki 		if (state < device->power.state) {
20620dacb71SRafael J. Wysocki 			dev_warn(&device->dev, "Cannot transition from %s to %s\n",
20720dacb71SRafael J. Wysocki 				 acpi_power_state_string(device->power.state),
20820dacb71SRafael J. Wysocki 				 acpi_power_state_string(state));
20920dacb71SRafael J. Wysocki 			return -ENODEV;
2109ce4e607SRafael J. Wysocki 		}
21120dacb71SRafael J. Wysocki 
212e78adb75SRafael J. Wysocki 		result = acpi_dev_pm_explicit_set(device, state);
213e78adb75SRafael J. Wysocki 		if (result)
214e78adb75SRafael J. Wysocki 			goto end;
2159ce4e607SRafael J. Wysocki 
21620dacb71SRafael J. Wysocki 		if (device->power.flags.power_resources)
21720dacb71SRafael J. Wysocki 			result = acpi_power_transition(device, target_state);
21820dacb71SRafael J. Wysocki 	} else {
21920dacb71SRafael J. Wysocki 		if (device->power.flags.power_resources) {
22020dacb71SRafael J. Wysocki 			result = acpi_power_transition(device, ACPI_STATE_D0);
22120dacb71SRafael J. Wysocki 			if (result)
22220dacb71SRafael J. Wysocki 				goto end;
22320dacb71SRafael J. Wysocki 		}
22420dacb71SRafael J. Wysocki 		result = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0);
225e5656271SRafael J. Wysocki 	}
2269ce4e607SRafael J. Wysocki 
2279ce4e607SRafael J. Wysocki  end:
228e78adb75SRafael J. Wysocki 	if (result) {
229b69137a7SRafael J. Wysocki 		dev_warn(&device->dev, "Failed to change power state to %s\n",
2309ce4e607SRafael J. Wysocki 			 acpi_power_state_string(state));
231e78adb75SRafael J. Wysocki 	} else {
23271b65445SMika Westerberg 		device->power.state = target_state;
2339ce4e607SRafael J. Wysocki 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
2349ce4e607SRafael J. Wysocki 				  "Device [%s] transitioned to %s\n",
2359ce4e607SRafael J. Wysocki 				  device->pnp.bus_id,
2369ce4e607SRafael J. Wysocki 				  acpi_power_state_string(state)));
2379ce4e607SRafael J. Wysocki 	}
2389ce4e607SRafael J. Wysocki 
2399ce4e607SRafael J. Wysocki 	return result;
2409ce4e607SRafael J. Wysocki }
2419ce4e607SRafael J. Wysocki EXPORT_SYMBOL(acpi_device_set_power);
2429ce4e607SRafael J. Wysocki 
2439ce4e607SRafael J. Wysocki int acpi_bus_set_power(acpi_handle handle, int state)
2449ce4e607SRafael J. Wysocki {
2459ce4e607SRafael J. Wysocki 	struct acpi_device *device;
2469ce4e607SRafael J. Wysocki 	int result;
2479ce4e607SRafael J. Wysocki 
2489ce4e607SRafael J. Wysocki 	result = acpi_bus_get_device(handle, &device);
2499ce4e607SRafael J. Wysocki 	if (result)
2509ce4e607SRafael J. Wysocki 		return result;
2519ce4e607SRafael J. Wysocki 
2529ce4e607SRafael J. Wysocki 	return acpi_device_set_power(device, state);
2539ce4e607SRafael J. Wysocki }
2549ce4e607SRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_set_power);
2559ce4e607SRafael J. Wysocki 
2569ce4e607SRafael J. Wysocki int acpi_bus_init_power(struct acpi_device *device)
2579ce4e607SRafael J. Wysocki {
2589ce4e607SRafael J. Wysocki 	int state;
2599ce4e607SRafael J. Wysocki 	int result;
2609ce4e607SRafael J. Wysocki 
2619ce4e607SRafael J. Wysocki 	if (!device)
2629ce4e607SRafael J. Wysocki 		return -EINVAL;
2639ce4e607SRafael J. Wysocki 
2649ce4e607SRafael J. Wysocki 	device->power.state = ACPI_STATE_UNKNOWN;
265cde1f95fSSakari Ailus 	if (!acpi_device_is_present(device)) {
266cde1f95fSSakari Ailus 		device->flags.initialized = false;
2671b1f3e16SRafael J. Wysocki 		return -ENXIO;
268cde1f95fSSakari Ailus 	}
2699ce4e607SRafael J. Wysocki 
2709ce4e607SRafael J. Wysocki 	result = acpi_device_get_power(device, &state);
2719ce4e607SRafael J. Wysocki 	if (result)
2729ce4e607SRafael J. Wysocki 		return result;
2739ce4e607SRafael J. Wysocki 
274a2367807SRafael J. Wysocki 	if (state < ACPI_STATE_D3_COLD && device->power.flags.power_resources) {
27520dacb71SRafael J. Wysocki 		/* Reference count the power resources. */
2769ce4e607SRafael J. Wysocki 		result = acpi_power_on_resources(device, state);
277a2367807SRafael J. Wysocki 		if (result)
2789ce4e607SRafael J. Wysocki 			return result;
279a2367807SRafael J. Wysocki 
28020dacb71SRafael J. Wysocki 		if (state == ACPI_STATE_D0) {
28120dacb71SRafael J. Wysocki 			/*
28220dacb71SRafael J. Wysocki 			 * If _PSC is not present and the state inferred from
28320dacb71SRafael J. Wysocki 			 * power resources appears to be D0, it still may be
28420dacb71SRafael J. Wysocki 			 * necessary to execute _PS0 at this point, because
28520dacb71SRafael J. Wysocki 			 * another device using the same power resources may
28620dacb71SRafael J. Wysocki 			 * have been put into D0 previously and that's why we
28720dacb71SRafael J. Wysocki 			 * see D0 here.
28820dacb71SRafael J. Wysocki 			 */
2899c0f45e3SRafael J. Wysocki 			result = acpi_dev_pm_explicit_set(device, state);
2909c0f45e3SRafael J. Wysocki 			if (result)
2919c0f45e3SRafael J. Wysocki 				return result;
29220dacb71SRafael J. Wysocki 		}
293b3785492SRafael J. Wysocki 	} else if (state == ACPI_STATE_UNKNOWN) {
2947cd8407dSRafael J. Wysocki 		/*
2957cd8407dSRafael J. Wysocki 		 * No power resources and missing _PSC?  Cross fingers and make
2967cd8407dSRafael J. Wysocki 		 * it D0 in hope that this is what the BIOS put the device into.
2977cd8407dSRafael J. Wysocki 		 * [We tried to force D0 here by executing _PS0, but that broke
2987cd8407dSRafael J. Wysocki 		 * Toshiba P870-303 in a nasty way.]
2997cd8407dSRafael J. Wysocki 		 */
300b3785492SRafael J. Wysocki 		state = ACPI_STATE_D0;
301a2367807SRafael J. Wysocki 	}
302a2367807SRafael J. Wysocki 	device->power.state = state;
303a2367807SRafael J. Wysocki 	return 0;
3049ce4e607SRafael J. Wysocki }
3059ce4e607SRafael J. Wysocki 
306b9e95fc6SRafael J. Wysocki /**
307b9e95fc6SRafael J. Wysocki  * acpi_device_fix_up_power - Force device with missing _PSC into D0.
308b9e95fc6SRafael J. Wysocki  * @device: Device object whose power state is to be fixed up.
309b9e95fc6SRafael J. Wysocki  *
310b9e95fc6SRafael J. Wysocki  * Devices without power resources and _PSC, but having _PS0 and _PS3 defined,
311b9e95fc6SRafael J. Wysocki  * are assumed to be put into D0 by the BIOS.  However, in some cases that may
312b9e95fc6SRafael J. Wysocki  * not be the case and this function should be used then.
313b9e95fc6SRafael J. Wysocki  */
314b9e95fc6SRafael J. Wysocki int acpi_device_fix_up_power(struct acpi_device *device)
315b9e95fc6SRafael J. Wysocki {
316b9e95fc6SRafael J. Wysocki 	int ret = 0;
317b9e95fc6SRafael J. Wysocki 
318b9e95fc6SRafael J. Wysocki 	if (!device->power.flags.power_resources
319b9e95fc6SRafael J. Wysocki 	    && !device->power.flags.explicit_get
320b9e95fc6SRafael J. Wysocki 	    && device->power.state == ACPI_STATE_D0)
321b9e95fc6SRafael J. Wysocki 		ret = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0);
322b9e95fc6SRafael J. Wysocki 
323b9e95fc6SRafael J. Wysocki 	return ret;
324b9e95fc6SRafael J. Wysocki }
32578a898d0SUlf Hansson EXPORT_SYMBOL_GPL(acpi_device_fix_up_power);
326b9e95fc6SRafael J. Wysocki 
327202317a5SRafael J. Wysocki int acpi_device_update_power(struct acpi_device *device, int *state_p)
3289ce4e607SRafael J. Wysocki {
3299ce4e607SRafael J. Wysocki 	int state;
3309ce4e607SRafael J. Wysocki 	int result;
3319ce4e607SRafael J. Wysocki 
332202317a5SRafael J. Wysocki 	if (device->power.state == ACPI_STATE_UNKNOWN) {
333202317a5SRafael J. Wysocki 		result = acpi_bus_init_power(device);
334202317a5SRafael J. Wysocki 		if (!result && state_p)
335202317a5SRafael J. Wysocki 			*state_p = device->power.state;
336202317a5SRafael J. Wysocki 
3379ce4e607SRafael J. Wysocki 		return result;
338202317a5SRafael J. Wysocki 	}
3399ce4e607SRafael J. Wysocki 
3409ce4e607SRafael J. Wysocki 	result = acpi_device_get_power(device, &state);
3419ce4e607SRafael J. Wysocki 	if (result)
3429ce4e607SRafael J. Wysocki 		return result;
3439ce4e607SRafael J. Wysocki 
34491bdad0bSRafael J. Wysocki 	if (state == ACPI_STATE_UNKNOWN) {
345511d5c42SRafael J. Wysocki 		state = ACPI_STATE_D0;
3469ce4e607SRafael J. Wysocki 		result = acpi_device_set_power(device, state);
34791bdad0bSRafael J. Wysocki 		if (result)
34891bdad0bSRafael J. Wysocki 			return result;
34991bdad0bSRafael J. Wysocki 	} else {
35091bdad0bSRafael J. Wysocki 		if (device->power.flags.power_resources) {
35191bdad0bSRafael J. Wysocki 			/*
35291bdad0bSRafael J. Wysocki 			 * We don't need to really switch the state, bu we need
35391bdad0bSRafael J. Wysocki 			 * to update the power resources' reference counters.
35491bdad0bSRafael J. Wysocki 			 */
35591bdad0bSRafael J. Wysocki 			result = acpi_power_transition(device, state);
35691bdad0bSRafael J. Wysocki 			if (result)
35791bdad0bSRafael J. Wysocki 				return result;
35891bdad0bSRafael J. Wysocki 		}
35991bdad0bSRafael J. Wysocki 		device->power.state = state;
36091bdad0bSRafael J. Wysocki 	}
36191bdad0bSRafael J. Wysocki 	if (state_p)
3629ce4e607SRafael J. Wysocki 		*state_p = state;
3639ce4e607SRafael J. Wysocki 
36491bdad0bSRafael J. Wysocki 	return 0;
3659ce4e607SRafael J. Wysocki }
3662bb3a2bfSAaron Lu EXPORT_SYMBOL_GPL(acpi_device_update_power);
367202317a5SRafael J. Wysocki 
368202317a5SRafael J. Wysocki int acpi_bus_update_power(acpi_handle handle, int *state_p)
369202317a5SRafael J. Wysocki {
370202317a5SRafael J. Wysocki 	struct acpi_device *device;
371202317a5SRafael J. Wysocki 	int result;
372202317a5SRafael J. Wysocki 
373202317a5SRafael J. Wysocki 	result = acpi_bus_get_device(handle, &device);
374202317a5SRafael J. Wysocki 	return result ? result : acpi_device_update_power(device, state_p);
375202317a5SRafael J. Wysocki }
3769ce4e607SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_bus_update_power);
3779ce4e607SRafael J. Wysocki 
3789ce4e607SRafael J. Wysocki bool acpi_bus_power_manageable(acpi_handle handle)
3799ce4e607SRafael J. Wysocki {
3809ce4e607SRafael J. Wysocki 	struct acpi_device *device;
3819ce4e607SRafael J. Wysocki 	int result;
3829ce4e607SRafael J. Wysocki 
3839ce4e607SRafael J. Wysocki 	result = acpi_bus_get_device(handle, &device);
3849ce4e607SRafael J. Wysocki 	return result ? false : device->flags.power_manageable;
3859ce4e607SRafael J. Wysocki }
3869ce4e607SRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_power_manageable);
3879ce4e607SRafael J. Wysocki 
388ec4602a9SRafael J. Wysocki #ifdef CONFIG_PM
389ec4602a9SRafael J. Wysocki static DEFINE_MUTEX(acpi_pm_notifier_lock);
390ec4602a9SRafael J. Wysocki 
39133e4f80eSRafael J. Wysocki void acpi_pm_wakeup_event(struct device *dev)
39233e4f80eSRafael J. Wysocki {
39333e4f80eSRafael J. Wysocki 	pm_wakeup_dev_event(dev, 0, acpi_s2idle_wakeup());
39433e4f80eSRafael J. Wysocki }
39533e4f80eSRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_pm_wakeup_event);
39633e4f80eSRafael J. Wysocki 
397c072530fSRafael J. Wysocki static void acpi_pm_notify_handler(acpi_handle handle, u32 val, void *not_used)
398c072530fSRafael J. Wysocki {
399c072530fSRafael J. Wysocki 	struct acpi_device *adev;
400c072530fSRafael J. Wysocki 
401c072530fSRafael J. Wysocki 	if (val != ACPI_NOTIFY_DEVICE_WAKE)
402c072530fSRafael J. Wysocki 		return;
403c072530fSRafael J. Wysocki 
404020a6375SRafael J. Wysocki 	acpi_handle_debug(handle, "Wake notify\n");
405020a6375SRafael J. Wysocki 
406c072530fSRafael J. Wysocki 	adev = acpi_bus_get_acpi_device(handle);
407c072530fSRafael J. Wysocki 	if (!adev)
408c072530fSRafael J. Wysocki 		return;
409c072530fSRafael J. Wysocki 
410c072530fSRafael J. Wysocki 	mutex_lock(&acpi_pm_notifier_lock);
411c072530fSRafael J. Wysocki 
412c072530fSRafael J. Wysocki 	if (adev->wakeup.flags.notifier_present) {
41333e4f80eSRafael J. Wysocki 		pm_wakeup_ws_event(adev->wakeup.ws, 0, acpi_s2idle_wakeup());
414020a6375SRafael J. Wysocki 		if (adev->wakeup.context.func) {
415020a6375SRafael J. Wysocki 			acpi_handle_debug(handle, "Running %pF for %s\n",
416020a6375SRafael J. Wysocki 					  adev->wakeup.context.func,
417020a6375SRafael J. Wysocki 					  dev_name(adev->wakeup.context.dev));
41864fd1c70SRafael J. Wysocki 			adev->wakeup.context.func(&adev->wakeup.context);
419c072530fSRafael J. Wysocki 		}
420020a6375SRafael J. Wysocki 	}
421c072530fSRafael J. Wysocki 
422c072530fSRafael J. Wysocki 	mutex_unlock(&acpi_pm_notifier_lock);
423c072530fSRafael J. Wysocki 
424c072530fSRafael J. Wysocki 	acpi_bus_put_acpi_device(adev);
425c072530fSRafael J. Wysocki }
426c072530fSRafael J. Wysocki 
427ec4602a9SRafael J. Wysocki /**
428c072530fSRafael J. Wysocki  * acpi_add_pm_notifier - Register PM notify handler for given ACPI device.
429c072530fSRafael J. Wysocki  * @adev: ACPI device to add the notify handler for.
430c072530fSRafael J. Wysocki  * @dev: Device to generate a wakeup event for while handling the notification.
43164fd1c70SRafael J. Wysocki  * @func: Work function to execute when handling the notification.
432ec4602a9SRafael J. Wysocki  *
433ec4602a9SRafael J. Wysocki  * NOTE: @adev need not be a run-wake or wakeup device to be a valid source of
434ec4602a9SRafael J. Wysocki  * PM wakeup events.  For example, wakeup events may be generated for bridges
435ec4602a9SRafael J. Wysocki  * if one of the devices below the bridge is signaling wakeup, even if the
436ec4602a9SRafael J. Wysocki  * bridge itself doesn't have a wakeup GPE associated with it.
437ec4602a9SRafael J. Wysocki  */
438c072530fSRafael J. Wysocki acpi_status acpi_add_pm_notifier(struct acpi_device *adev, struct device *dev,
43964fd1c70SRafael J. Wysocki 			void (*func)(struct acpi_device_wakeup_context *context))
440ec4602a9SRafael J. Wysocki {
441ec4602a9SRafael J. Wysocki 	acpi_status status = AE_ALREADY_EXISTS;
442ec4602a9SRafael J. Wysocki 
44364fd1c70SRafael J. Wysocki 	if (!dev && !func)
444c072530fSRafael J. Wysocki 		return AE_BAD_PARAMETER;
445c072530fSRafael J. Wysocki 
446ec4602a9SRafael J. Wysocki 	mutex_lock(&acpi_pm_notifier_lock);
447ec4602a9SRafael J. Wysocki 
448ec4602a9SRafael J. Wysocki 	if (adev->wakeup.flags.notifier_present)
449ec4602a9SRafael J. Wysocki 		goto out;
450ec4602a9SRafael J. Wysocki 
451c072530fSRafael J. Wysocki 	adev->wakeup.ws = wakeup_source_register(dev_name(&adev->dev));
452c072530fSRafael J. Wysocki 	adev->wakeup.context.dev = dev;
45364fd1c70SRafael J. Wysocki 	adev->wakeup.context.func = func;
454c072530fSRafael J. Wysocki 
455c072530fSRafael J. Wysocki 	status = acpi_install_notify_handler(adev->handle, ACPI_SYSTEM_NOTIFY,
456c072530fSRafael J. Wysocki 					     acpi_pm_notify_handler, NULL);
457ec4602a9SRafael J. Wysocki 	if (ACPI_FAILURE(status))
458ec4602a9SRafael J. Wysocki 		goto out;
459ec4602a9SRafael J. Wysocki 
460ec4602a9SRafael J. Wysocki 	adev->wakeup.flags.notifier_present = true;
461ec4602a9SRafael J. Wysocki 
462ec4602a9SRafael J. Wysocki  out:
463ec4602a9SRafael J. Wysocki 	mutex_unlock(&acpi_pm_notifier_lock);
464ec4602a9SRafael J. Wysocki 	return status;
465ec4602a9SRafael J. Wysocki }
466ec4602a9SRafael J. Wysocki 
467ec4602a9SRafael J. Wysocki /**
468ec4602a9SRafael J. Wysocki  * acpi_remove_pm_notifier - Unregister PM notifier from given ACPI device.
469ec4602a9SRafael J. Wysocki  * @adev: ACPI device to remove the notifier from.
470ec4602a9SRafael J. Wysocki  */
471c072530fSRafael J. Wysocki acpi_status acpi_remove_pm_notifier(struct acpi_device *adev)
472ec4602a9SRafael J. Wysocki {
473ec4602a9SRafael J. Wysocki 	acpi_status status = AE_BAD_PARAMETER;
474ec4602a9SRafael J. Wysocki 
475ec4602a9SRafael J. Wysocki 	mutex_lock(&acpi_pm_notifier_lock);
476ec4602a9SRafael J. Wysocki 
477ec4602a9SRafael J. Wysocki 	if (!adev->wakeup.flags.notifier_present)
478ec4602a9SRafael J. Wysocki 		goto out;
479ec4602a9SRafael J. Wysocki 
480ec4602a9SRafael J. Wysocki 	status = acpi_remove_notify_handler(adev->handle,
481ec4602a9SRafael J. Wysocki 					    ACPI_SYSTEM_NOTIFY,
482c072530fSRafael J. Wysocki 					    acpi_pm_notify_handler);
483ec4602a9SRafael J. Wysocki 	if (ACPI_FAILURE(status))
484ec4602a9SRafael J. Wysocki 		goto out;
485ec4602a9SRafael J. Wysocki 
48664fd1c70SRafael J. Wysocki 	adev->wakeup.context.func = NULL;
487c072530fSRafael J. Wysocki 	adev->wakeup.context.dev = NULL;
488c072530fSRafael J. Wysocki 	wakeup_source_unregister(adev->wakeup.ws);
489c072530fSRafael J. Wysocki 
490ec4602a9SRafael J. Wysocki 	adev->wakeup.flags.notifier_present = false;
491ec4602a9SRafael J. Wysocki 
492ec4602a9SRafael J. Wysocki  out:
493ec4602a9SRafael J. Wysocki 	mutex_unlock(&acpi_pm_notifier_lock);
494ec4602a9SRafael J. Wysocki 	return status;
495ec4602a9SRafael J. Wysocki }
496ec4602a9SRafael J. Wysocki 
4979ce4e607SRafael J. Wysocki bool acpi_bus_can_wakeup(acpi_handle handle)
4989ce4e607SRafael J. Wysocki {
4999ce4e607SRafael J. Wysocki 	struct acpi_device *device;
5009ce4e607SRafael J. Wysocki 	int result;
5019ce4e607SRafael J. Wysocki 
5029ce4e607SRafael J. Wysocki 	result = acpi_bus_get_device(handle, &device);
5039ce4e607SRafael J. Wysocki 	return result ? false : device->wakeup.flags.valid;
5049ce4e607SRafael J. Wysocki }
5059ce4e607SRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_can_wakeup);
5069ce4e607SRafael J. Wysocki 
5078370c2dcSRafael J. Wysocki bool acpi_pm_device_can_wakeup(struct device *dev)
5088370c2dcSRafael J. Wysocki {
5098370c2dcSRafael J. Wysocki 	struct acpi_device *adev = ACPI_COMPANION(dev);
5108370c2dcSRafael J. Wysocki 
5118370c2dcSRafael J. Wysocki 	return adev ? acpi_device_can_wakeup(adev) : false;
5128370c2dcSRafael J. Wysocki }
5138370c2dcSRafael J. Wysocki 
5149ce4e607SRafael J. Wysocki /**
515b25c77efSRafael J. Wysocki  * acpi_dev_pm_get_state - Get preferred power state of ACPI device.
51686b3832cSRafael J. Wysocki  * @dev: Device whose preferred target power state to return.
51786b3832cSRafael J. Wysocki  * @adev: ACPI device node corresponding to @dev.
51886b3832cSRafael J. Wysocki  * @target_state: System state to match the resultant device state.
519fa1675b5SRafael J. Wysocki  * @d_min_p: Location to store the highest power state available to the device.
520fa1675b5SRafael J. Wysocki  * @d_max_p: Location to store the lowest power state available to the device.
52186b3832cSRafael J. Wysocki  *
522fa1675b5SRafael J. Wysocki  * Find the lowest power (highest number) and highest power (lowest number) ACPI
523fa1675b5SRafael J. Wysocki  * device power states that the device can be in while the system is in the
524fa1675b5SRafael J. Wysocki  * state represented by @target_state.  Store the integer numbers representing
525fa1675b5SRafael J. Wysocki  * those stats in the memory locations pointed to by @d_max_p and @d_min_p,
526fa1675b5SRafael J. Wysocki  * respectively.
52786b3832cSRafael J. Wysocki  *
52886b3832cSRafael J. Wysocki  * Callers must ensure that @dev and @adev are valid pointers and that @adev
52986b3832cSRafael J. Wysocki  * actually corresponds to @dev before using this function.
530fa1675b5SRafael J. Wysocki  *
531fa1675b5SRafael J. Wysocki  * Returns 0 on success or -ENODATA when one of the ACPI methods fails or
532fa1675b5SRafael J. Wysocki  * returns a value that doesn't make sense.  The memory locations pointed to by
533fa1675b5SRafael J. Wysocki  * @d_max_p and @d_min_p are only modified on success.
53486b3832cSRafael J. Wysocki  */
535b25c77efSRafael J. Wysocki static int acpi_dev_pm_get_state(struct device *dev, struct acpi_device *adev,
536fa1675b5SRafael J. Wysocki 				 u32 target_state, int *d_min_p, int *d_max_p)
53786b3832cSRafael J. Wysocki {
538fa1675b5SRafael J. Wysocki 	char method[] = { '_', 'S', '0' + target_state, 'D', '\0' };
539fa1675b5SRafael J. Wysocki 	acpi_handle handle = adev->handle;
540fa1675b5SRafael J. Wysocki 	unsigned long long ret;
541fa1675b5SRafael J. Wysocki 	int d_min, d_max;
54286b3832cSRafael J. Wysocki 	bool wakeup = false;
543fa1675b5SRafael J. Wysocki 	acpi_status status;
54486b3832cSRafael J. Wysocki 
54586b3832cSRafael J. Wysocki 	/*
546fa1675b5SRafael J. Wysocki 	 * If the system state is S0, the lowest power state the device can be
547fa1675b5SRafael J. Wysocki 	 * in is D3cold, unless the device has _S0W and is supposed to signal
548fa1675b5SRafael J. Wysocki 	 * wakeup, in which case the return value of _S0W has to be used as the
549fa1675b5SRafael J. Wysocki 	 * lowest power state available to the device.
55086b3832cSRafael J. Wysocki 	 */
55186b3832cSRafael J. Wysocki 	d_min = ACPI_STATE_D0;
5524c164ae7SRafael J. Wysocki 	d_max = ACPI_STATE_D3_COLD;
55386b3832cSRafael J. Wysocki 
55486b3832cSRafael J. Wysocki 	/*
55586b3832cSRafael J. Wysocki 	 * If present, _SxD methods return the minimum D-state (highest power
55686b3832cSRafael J. Wysocki 	 * state) we can use for the corresponding S-states.  Otherwise, the
55786b3832cSRafael J. Wysocki 	 * minimum D-state is D0 (ACPI 3.x).
55886b3832cSRafael J. Wysocki 	 */
55986b3832cSRafael J. Wysocki 	if (target_state > ACPI_STATE_S0) {
560fa1675b5SRafael J. Wysocki 		/*
561fa1675b5SRafael J. Wysocki 		 * We rely on acpi_evaluate_integer() not clobbering the integer
562fa1675b5SRafael J. Wysocki 		 * provided if AE_NOT_FOUND is returned.
563fa1675b5SRafael J. Wysocki 		 */
564fa1675b5SRafael J. Wysocki 		ret = d_min;
565fa1675b5SRafael J. Wysocki 		status = acpi_evaluate_integer(handle, method, NULL, &ret);
566fa1675b5SRafael J. Wysocki 		if ((ACPI_FAILURE(status) && status != AE_NOT_FOUND)
567fa1675b5SRafael J. Wysocki 		    || ret > ACPI_STATE_D3_COLD)
568fa1675b5SRafael J. Wysocki 			return -ENODATA;
569fa1675b5SRafael J. Wysocki 
570fa1675b5SRafael J. Wysocki 		/*
571fa1675b5SRafael J. Wysocki 		 * We need to handle legacy systems where D3hot and D3cold are
572fa1675b5SRafael J. Wysocki 		 * the same and 3 is returned in both cases, so fall back to
573fa1675b5SRafael J. Wysocki 		 * D3cold if D3hot is not a valid state.
574fa1675b5SRafael J. Wysocki 		 */
575fa1675b5SRafael J. Wysocki 		if (!adev->power.states[ret].flags.valid) {
576fa1675b5SRafael J. Wysocki 			if (ret == ACPI_STATE_D3_HOT)
577fa1675b5SRafael J. Wysocki 				ret = ACPI_STATE_D3_COLD;
578fa1675b5SRafael J. Wysocki 			else
579fa1675b5SRafael J. Wysocki 				return -ENODATA;
580fa1675b5SRafael J. Wysocki 		}
581fa1675b5SRafael J. Wysocki 		d_min = ret;
58286b3832cSRafael J. Wysocki 		wakeup = device_may_wakeup(dev) && adev->wakeup.flags.valid
58386b3832cSRafael J. Wysocki 			&& adev->wakeup.sleep_state >= target_state;
58486b3832cSRafael J. Wysocki 	} else if (dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) !=
58586b3832cSRafael J. Wysocki 			PM_QOS_FLAGS_NONE) {
58686b3832cSRafael J. Wysocki 		wakeup = adev->wakeup.flags.valid;
58786b3832cSRafael J. Wysocki 	}
58886b3832cSRafael J. Wysocki 
58986b3832cSRafael J. Wysocki 	/*
59086b3832cSRafael J. Wysocki 	 * If _PRW says we can wake up the system from the target sleep state,
59186b3832cSRafael J. Wysocki 	 * the D-state returned by _SxD is sufficient for that (we assume a
59286b3832cSRafael J. Wysocki 	 * wakeup-aware driver if wake is set).  Still, if _SxW exists
59386b3832cSRafael J. Wysocki 	 * (ACPI 3.x), it should return the maximum (lowest power) D-state that
59486b3832cSRafael J. Wysocki 	 * can wake the system.  _S0W may be valid, too.
59586b3832cSRafael J. Wysocki 	 */
59686b3832cSRafael J. Wysocki 	if (wakeup) {
597fa1675b5SRafael J. Wysocki 		method[3] = 'W';
598fa1675b5SRafael J. Wysocki 		status = acpi_evaluate_integer(handle, method, NULL, &ret);
599fa1675b5SRafael J. Wysocki 		if (status == AE_NOT_FOUND) {
600fa1675b5SRafael J. Wysocki 			if (target_state > ACPI_STATE_S0)
60186b3832cSRafael J. Wysocki 				d_max = d_min;
602fa1675b5SRafael J. Wysocki 		} else if (ACPI_SUCCESS(status) && ret <= ACPI_STATE_D3_COLD) {
603fa1675b5SRafael J. Wysocki 			/* Fall back to D3cold if ret is not a valid state. */
604fa1675b5SRafael J. Wysocki 			if (!adev->power.states[ret].flags.valid)
605fa1675b5SRafael J. Wysocki 				ret = ACPI_STATE_D3_COLD;
606fa1675b5SRafael J. Wysocki 
607fa1675b5SRafael J. Wysocki 			d_max = ret > d_min ? ret : d_min;
608fa1675b5SRafael J. Wysocki 		} else {
609fa1675b5SRafael J. Wysocki 			return -ENODATA;
61086b3832cSRafael J. Wysocki 		}
61186b3832cSRafael J. Wysocki 	}
61286b3832cSRafael J. Wysocki 
61386b3832cSRafael J. Wysocki 	if (d_min_p)
61486b3832cSRafael J. Wysocki 		*d_min_p = d_min;
615fa1675b5SRafael J. Wysocki 
616fa1675b5SRafael J. Wysocki 	if (d_max_p)
617fa1675b5SRafael J. Wysocki 		*d_max_p = d_max;
618fa1675b5SRafael J. Wysocki 
619fa1675b5SRafael J. Wysocki 	return 0;
62086b3832cSRafael J. Wysocki }
621cd7bd02dSRafael J. Wysocki 
622a6ae7594SRafael J. Wysocki /**
623a6ae7594SRafael J. Wysocki  * acpi_pm_device_sleep_state - Get preferred power state of ACPI device.
624a6ae7594SRafael J. Wysocki  * @dev: Device whose preferred target power state to return.
625a6ae7594SRafael J. Wysocki  * @d_min_p: Location to store the upper limit of the allowed states range.
626a6ae7594SRafael J. Wysocki  * @d_max_in: Deepest low-power state to take into consideration.
627a6ae7594SRafael J. Wysocki  * Return value: Preferred power state of the device on success, -ENODEV
628fa1675b5SRafael J. Wysocki  * if there's no 'struct acpi_device' for @dev, -EINVAL if @d_max_in is
629fa1675b5SRafael J. Wysocki  * incorrect, or -ENODATA on ACPI method failure.
630a6ae7594SRafael J. Wysocki  *
631a6ae7594SRafael J. Wysocki  * The caller must ensure that @dev is valid before using this function.
632a6ae7594SRafael J. Wysocki  */
633a6ae7594SRafael J. Wysocki int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in)
634a6ae7594SRafael J. Wysocki {
635a6ae7594SRafael J. Wysocki 	struct acpi_device *adev;
6369b5c7a5aSRafael J. Wysocki 	int ret, d_min, d_max;
637fa1675b5SRafael J. Wysocki 
638fa1675b5SRafael J. Wysocki 	if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3_COLD)
639fa1675b5SRafael J. Wysocki 		return -EINVAL;
640fa1675b5SRafael J. Wysocki 
64120dacb71SRafael J. Wysocki 	if (d_max_in > ACPI_STATE_D2) {
642fa1675b5SRafael J. Wysocki 		enum pm_qos_flags_status stat;
643fa1675b5SRafael J. Wysocki 
644fa1675b5SRafael J. Wysocki 		stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF);
645fa1675b5SRafael J. Wysocki 		if (stat == PM_QOS_FLAGS_ALL)
64620dacb71SRafael J. Wysocki 			d_max_in = ACPI_STATE_D2;
647fa1675b5SRafael J. Wysocki 	}
648a6ae7594SRafael J. Wysocki 
64917653a3eSRafael J. Wysocki 	adev = ACPI_COMPANION(dev);
65017653a3eSRafael J. Wysocki 	if (!adev) {
65117653a3eSRafael J. Wysocki 		dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);
652a6ae7594SRafael J. Wysocki 		return -ENODEV;
653a6ae7594SRafael J. Wysocki 	}
654a6ae7594SRafael J. Wysocki 
655fa1675b5SRafael J. Wysocki 	ret = acpi_dev_pm_get_state(dev, adev, acpi_target_system_state(),
6569b5c7a5aSRafael J. Wysocki 				    &d_min, &d_max);
657fa1675b5SRafael J. Wysocki 	if (ret)
658fa1675b5SRafael J. Wysocki 		return ret;
659fa1675b5SRafael J. Wysocki 
6609b5c7a5aSRafael J. Wysocki 	if (d_max_in < d_min)
661fa1675b5SRafael J. Wysocki 		return -EINVAL;
662fa1675b5SRafael J. Wysocki 
663fa1675b5SRafael J. Wysocki 	if (d_max > d_max_in) {
6649b5c7a5aSRafael J. Wysocki 		for (d_max = d_max_in; d_max > d_min; d_max--) {
665fa1675b5SRafael J. Wysocki 			if (adev->power.states[d_max].flags.valid)
666fa1675b5SRafael J. Wysocki 				break;
667fa1675b5SRafael J. Wysocki 		}
668fa1675b5SRafael J. Wysocki 	}
6699b5c7a5aSRafael J. Wysocki 
6709b5c7a5aSRafael J. Wysocki 	if (d_min_p)
6719b5c7a5aSRafael J. Wysocki 		*d_min_p = d_min;
6729b5c7a5aSRafael J. Wysocki 
673fa1675b5SRafael J. Wysocki 	return d_max;
674a6ae7594SRafael J. Wysocki }
675a6ae7594SRafael J. Wysocki EXPORT_SYMBOL(acpi_pm_device_sleep_state);
676a6ae7594SRafael J. Wysocki 
677cd7bd02dSRafael J. Wysocki /**
678c072530fSRafael J. Wysocki  * acpi_pm_notify_work_func - ACPI devices wakeup notification work function.
67964fd1c70SRafael J. Wysocki  * @context: Device wakeup context.
680e5cc8ef3SRafael J. Wysocki  */
68164fd1c70SRafael J. Wysocki static void acpi_pm_notify_work_func(struct acpi_device_wakeup_context *context)
682e5cc8ef3SRafael J. Wysocki {
68364fd1c70SRafael J. Wysocki 	struct device *dev = context->dev;
684e5cc8ef3SRafael J. Wysocki 
685c072530fSRafael J. Wysocki 	if (dev) {
686e5cc8ef3SRafael J. Wysocki 		pm_wakeup_event(dev, 0);
68764fd1c70SRafael J. Wysocki 		pm_request_resume(dev);
688e5cc8ef3SRafael J. Wysocki 	}
689e5cc8ef3SRafael J. Wysocki }
690e5cc8ef3SRafael J. Wysocki 
69199d8845eSRafael J. Wysocki static DEFINE_MUTEX(acpi_wakeup_lock);
69299d8845eSRafael J. Wysocki 
6931ba51a7cSRafael J. Wysocki static int __acpi_device_wakeup_enable(struct acpi_device *adev,
6941ba51a7cSRafael J. Wysocki 				       u32 target_state, int max_count)
6951ba51a7cSRafael J. Wysocki {
6961ba51a7cSRafael J. Wysocki 	struct acpi_device_wakeup *wakeup = &adev->wakeup;
6971ba51a7cSRafael J. Wysocki 	acpi_status status;
6981ba51a7cSRafael J. Wysocki 	int error = 0;
6991ba51a7cSRafael J. Wysocki 
7001ba51a7cSRafael J. Wysocki 	mutex_lock(&acpi_wakeup_lock);
7011ba51a7cSRafael J. Wysocki 
7021ba51a7cSRafael J. Wysocki 	if (wakeup->enable_count >= max_count)
7031ba51a7cSRafael J. Wysocki 		goto out;
7041ba51a7cSRafael J. Wysocki 
7051ba51a7cSRafael J. Wysocki 	if (wakeup->enable_count > 0)
7061ba51a7cSRafael J. Wysocki 		goto inc;
7071ba51a7cSRafael J. Wysocki 
7081ba51a7cSRafael J. Wysocki 	error = acpi_enable_wakeup_device_power(adev, target_state);
7091ba51a7cSRafael J. Wysocki 	if (error)
7101ba51a7cSRafael J. Wysocki 		goto out;
7111ba51a7cSRafael J. Wysocki 
7121ba51a7cSRafael J. Wysocki 	status = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number);
7131ba51a7cSRafael J. Wysocki 	if (ACPI_FAILURE(status)) {
7141ba51a7cSRafael J. Wysocki 		acpi_disable_wakeup_device_power(adev);
7151ba51a7cSRafael J. Wysocki 		error = -EIO;
7161ba51a7cSRafael J. Wysocki 		goto out;
7171ba51a7cSRafael J. Wysocki 	}
7181ba51a7cSRafael J. Wysocki 
7191ba51a7cSRafael J. Wysocki inc:
7201ba51a7cSRafael J. Wysocki 	wakeup->enable_count++;
7211ba51a7cSRafael J. Wysocki 
7221ba51a7cSRafael J. Wysocki out:
7231ba51a7cSRafael J. Wysocki 	mutex_unlock(&acpi_wakeup_lock);
7241ba51a7cSRafael J. Wysocki 	return error;
7251ba51a7cSRafael J. Wysocki }
7261ba51a7cSRafael J. Wysocki 
727e5cc8ef3SRafael J. Wysocki /**
72899d8845eSRafael J. Wysocki  * acpi_device_wakeup_enable - Enable wakeup functionality for device.
72999d8845eSRafael J. Wysocki  * @adev: ACPI device to enable wakeup functionality for.
730f35cec25SRafael J. Wysocki  * @target_state: State the system is transitioning into.
731cd7bd02dSRafael J. Wysocki  *
73299d8845eSRafael J. Wysocki  * Enable the GPE associated with @adev so that it can generate wakeup signals
73399d8845eSRafael J. Wysocki  * for the device in response to external (remote) events and enable wakeup
73499d8845eSRafael J. Wysocki  * power for it.
735dee8370cSRafael J. Wysocki  *
736dee8370cSRafael J. Wysocki  * Callers must ensure that @adev is a valid ACPI device node before executing
737dee8370cSRafael J. Wysocki  * this function.
738dee8370cSRafael J. Wysocki  */
73999d8845eSRafael J. Wysocki static int acpi_device_wakeup_enable(struct acpi_device *adev, u32 target_state)
740dee8370cSRafael J. Wysocki {
7411ba51a7cSRafael J. Wysocki 	return __acpi_device_wakeup_enable(adev, target_state, 1);
74299d8845eSRafael J. Wysocki }
74399d8845eSRafael J. Wysocki 
74499d8845eSRafael J. Wysocki /**
74599d8845eSRafael J. Wysocki  * acpi_device_wakeup_disable - Disable wakeup functionality for device.
74699d8845eSRafael J. Wysocki  * @adev: ACPI device to disable wakeup functionality for.
74799d8845eSRafael J. Wysocki  *
74899d8845eSRafael J. Wysocki  * Disable the GPE associated with @adev and disable wakeup power for it.
74999d8845eSRafael J. Wysocki  *
75099d8845eSRafael J. Wysocki  * Callers must ensure that @adev is a valid ACPI device node before executing
75199d8845eSRafael J. Wysocki  * this function.
75299d8845eSRafael J. Wysocki  */
75399d8845eSRafael J. Wysocki static void acpi_device_wakeup_disable(struct acpi_device *adev)
75499d8845eSRafael J. Wysocki {
75599d8845eSRafael J. Wysocki 	struct acpi_device_wakeup *wakeup = &adev->wakeup;
75699d8845eSRafael J. Wysocki 
75799d8845eSRafael J. Wysocki 	mutex_lock(&acpi_wakeup_lock);
75899d8845eSRafael J. Wysocki 
75999d8845eSRafael J. Wysocki 	if (!wakeup->enable_count)
76099d8845eSRafael J. Wysocki 		goto out;
76199d8845eSRafael J. Wysocki 
762dee8370cSRafael J. Wysocki 	acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number);
763dee8370cSRafael J. Wysocki 	acpi_disable_wakeup_device_power(adev);
76499d8845eSRafael J. Wysocki 
76599d8845eSRafael J. Wysocki 	wakeup->enable_count--;
76699d8845eSRafael J. Wysocki 
76799d8845eSRafael J. Wysocki out:
76899d8845eSRafael J. Wysocki 	mutex_unlock(&acpi_wakeup_lock);
769dee8370cSRafael J. Wysocki }
770dee8370cSRafael J. Wysocki 
7711ba51a7cSRafael J. Wysocki static int __acpi_pm_set_device_wakeup(struct device *dev, bool enable,
7721ba51a7cSRafael J. Wysocki 				       int max_count)
773a6ae7594SRafael J. Wysocki {
774a6ae7594SRafael J. Wysocki 	struct acpi_device *adev;
775a6ae7594SRafael J. Wysocki 	int error;
776a6ae7594SRafael J. Wysocki 
77717653a3eSRafael J. Wysocki 	adev = ACPI_COMPANION(dev);
77817653a3eSRafael J. Wysocki 	if (!adev) {
77917653a3eSRafael J. Wysocki 		dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);
780a6ae7594SRafael J. Wysocki 		return -ENODEV;
781a6ae7594SRafael J. Wysocki 	}
782a6ae7594SRafael J. Wysocki 
7834d183d04SRafael J. Wysocki 	if (!acpi_device_can_wakeup(adev))
7844d183d04SRafael J. Wysocki 		return -EINVAL;
7854d183d04SRafael J. Wysocki 
78699d8845eSRafael J. Wysocki 	if (!enable) {
78799d8845eSRafael J. Wysocki 		acpi_device_wakeup_disable(adev);
78899d8845eSRafael J. Wysocki 		dev_dbg(dev, "Wakeup disabled by ACPI\n");
78999d8845eSRafael J. Wysocki 		return 0;
79099d8845eSRafael J. Wysocki 	}
79199d8845eSRafael J. Wysocki 
7921ba51a7cSRafael J. Wysocki 	error = __acpi_device_wakeup_enable(adev, acpi_target_system_state(),
7931ba51a7cSRafael J. Wysocki 					    max_count);
794a6ae7594SRafael J. Wysocki 	if (!error)
79599d8845eSRafael J. Wysocki 		dev_dbg(dev, "Wakeup enabled by ACPI\n");
796a6ae7594SRafael J. Wysocki 
797a6ae7594SRafael J. Wysocki 	return error;
798a6ae7594SRafael J. Wysocki }
7991ba51a7cSRafael J. Wysocki 
8001ba51a7cSRafael J. Wysocki /**
8011ba51a7cSRafael J. Wysocki  * acpi_pm_set_device_wakeup - Enable/disable remote wakeup for given device.
8021ba51a7cSRafael J. Wysocki  * @dev: Device to enable/disable to generate wakeup events.
8031ba51a7cSRafael J. Wysocki  * @enable: Whether to enable or disable the wakeup functionality.
8041ba51a7cSRafael J. Wysocki  */
8051ba51a7cSRafael J. Wysocki int acpi_pm_set_device_wakeup(struct device *dev, bool enable)
8061ba51a7cSRafael J. Wysocki {
8071ba51a7cSRafael J. Wysocki 	return __acpi_pm_set_device_wakeup(dev, enable, 1);
8081ba51a7cSRafael J. Wysocki }
8091ba51a7cSRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_pm_set_device_wakeup);
8101ba51a7cSRafael J. Wysocki 
8111ba51a7cSRafael J. Wysocki /**
8121ba51a7cSRafael J. Wysocki  * acpi_pm_set_bridge_wakeup - Enable/disable remote wakeup for given bridge.
8131ba51a7cSRafael J. Wysocki  * @dev: Bridge device to enable/disable to generate wakeup events.
8141ba51a7cSRafael J. Wysocki  * @enable: Whether to enable or disable the wakeup functionality.
8151ba51a7cSRafael J. Wysocki  */
8161ba51a7cSRafael J. Wysocki int acpi_pm_set_bridge_wakeup(struct device *dev, bool enable)
8171ba51a7cSRafael J. Wysocki {
8181ba51a7cSRafael J. Wysocki 	return __acpi_pm_set_device_wakeup(dev, enable, INT_MAX);
8191ba51a7cSRafael J. Wysocki }
8201ba51a7cSRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_pm_set_bridge_wakeup);
821e5cc8ef3SRafael J. Wysocki 
822e5cc8ef3SRafael J. Wysocki /**
823e5cc8ef3SRafael J. Wysocki  * acpi_dev_pm_low_power - Put ACPI device into a low-power state.
824e5cc8ef3SRafael J. Wysocki  * @dev: Device to put into a low-power state.
825e5cc8ef3SRafael J. Wysocki  * @adev: ACPI device node corresponding to @dev.
826e5cc8ef3SRafael J. Wysocki  * @system_state: System state to choose the device state for.
827e5cc8ef3SRafael J. Wysocki  */
828e5cc8ef3SRafael J. Wysocki static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev,
829e5cc8ef3SRafael J. Wysocki 				 u32 system_state)
830e5cc8ef3SRafael J. Wysocki {
831fa1675b5SRafael J. Wysocki 	int ret, state;
832e5cc8ef3SRafael J. Wysocki 
833e5cc8ef3SRafael J. Wysocki 	if (!acpi_device_power_manageable(adev))
834e5cc8ef3SRafael J. Wysocki 		return 0;
835e5cc8ef3SRafael J. Wysocki 
836fa1675b5SRafael J. Wysocki 	ret = acpi_dev_pm_get_state(dev, adev, system_state, NULL, &state);
837fa1675b5SRafael J. Wysocki 	return ret ? ret : acpi_device_set_power(adev, state);
838e5cc8ef3SRafael J. Wysocki }
839e5cc8ef3SRafael J. Wysocki 
840e5cc8ef3SRafael J. Wysocki /**
841e5cc8ef3SRafael J. Wysocki  * acpi_dev_pm_full_power - Put ACPI device into the full-power state.
842e5cc8ef3SRafael J. Wysocki  * @adev: ACPI device node to put into the full-power state.
843e5cc8ef3SRafael J. Wysocki  */
844e5cc8ef3SRafael J. Wysocki static int acpi_dev_pm_full_power(struct acpi_device *adev)
845e5cc8ef3SRafael J. Wysocki {
846e5cc8ef3SRafael J. Wysocki 	return acpi_device_power_manageable(adev) ?
847e5cc8ef3SRafael J. Wysocki 		acpi_device_set_power(adev, ACPI_STATE_D0) : 0;
848e5cc8ef3SRafael J. Wysocki }
849e5cc8ef3SRafael J. Wysocki 
850e5cc8ef3SRafael J. Wysocki /**
851e5cc8ef3SRafael J. Wysocki  * acpi_dev_runtime_suspend - Put device into a low-power state using ACPI.
852e5cc8ef3SRafael J. Wysocki  * @dev: Device to put into a low-power state.
853e5cc8ef3SRafael J. Wysocki  *
854e5cc8ef3SRafael J. Wysocki  * Put the given device into a runtime low-power state using the standard ACPI
855e5cc8ef3SRafael J. Wysocki  * mechanism.  Set up remote wakeup if desired, choose the state to put the
856e5cc8ef3SRafael J. Wysocki  * device into (this checks if remote wakeup is expected to work too), and set
857e5cc8ef3SRafael J. Wysocki  * the power state of the device.
858e5cc8ef3SRafael J. Wysocki  */
859e5cc8ef3SRafael J. Wysocki int acpi_dev_runtime_suspend(struct device *dev)
860e5cc8ef3SRafael J. Wysocki {
86179c0373fSRafael J. Wysocki 	struct acpi_device *adev = ACPI_COMPANION(dev);
862e5cc8ef3SRafael J. Wysocki 	bool remote_wakeup;
863e5cc8ef3SRafael J. Wysocki 	int error;
864e5cc8ef3SRafael J. Wysocki 
865e5cc8ef3SRafael J. Wysocki 	if (!adev)
866e5cc8ef3SRafael J. Wysocki 		return 0;
867e5cc8ef3SRafael J. Wysocki 
868e5cc8ef3SRafael J. Wysocki 	remote_wakeup = dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) >
869e5cc8ef3SRafael J. Wysocki 				PM_QOS_FLAGS_NONE;
87099d8845eSRafael J. Wysocki 	if (remote_wakeup) {
87199d8845eSRafael J. Wysocki 		error = acpi_device_wakeup_enable(adev, ACPI_STATE_S0);
87299d8845eSRafael J. Wysocki 		if (error)
873e5cc8ef3SRafael J. Wysocki 			return -EAGAIN;
87499d8845eSRafael J. Wysocki 	}
875e5cc8ef3SRafael J. Wysocki 
876e5cc8ef3SRafael J. Wysocki 	error = acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
87799d8845eSRafael J. Wysocki 	if (error && remote_wakeup)
87899d8845eSRafael J. Wysocki 		acpi_device_wakeup_disable(adev);
879e5cc8ef3SRafael J. Wysocki 
880e5cc8ef3SRafael J. Wysocki 	return error;
881e5cc8ef3SRafael J. Wysocki }
882e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_runtime_suspend);
883e5cc8ef3SRafael J. Wysocki 
884e5cc8ef3SRafael J. Wysocki /**
88563705c40SRafael J. Wysocki  * acpi_dev_resume - Put device into the full-power state using ACPI.
886e5cc8ef3SRafael J. Wysocki  * @dev: Device to put into the full-power state.
887e5cc8ef3SRafael J. Wysocki  *
888e5cc8ef3SRafael J. Wysocki  * Put the given device into the full-power state using the standard ACPI
88963705c40SRafael J. Wysocki  * mechanism.  Set the power state of the device to ACPI D0 and disable wakeup.
890e5cc8ef3SRafael J. Wysocki  */
89163705c40SRafael J. Wysocki int acpi_dev_resume(struct device *dev)
892e5cc8ef3SRafael J. Wysocki {
89379c0373fSRafael J. Wysocki 	struct acpi_device *adev = ACPI_COMPANION(dev);
894e5cc8ef3SRafael J. Wysocki 	int error;
895e5cc8ef3SRafael J. Wysocki 
896e5cc8ef3SRafael J. Wysocki 	if (!adev)
897e5cc8ef3SRafael J. Wysocki 		return 0;
898e5cc8ef3SRafael J. Wysocki 
899e5cc8ef3SRafael J. Wysocki 	error = acpi_dev_pm_full_power(adev);
90099d8845eSRafael J. Wysocki 	acpi_device_wakeup_disable(adev);
901e5cc8ef3SRafael J. Wysocki 	return error;
902e5cc8ef3SRafael J. Wysocki }
90363705c40SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_resume);
904e5cc8ef3SRafael J. Wysocki 
905e5cc8ef3SRafael J. Wysocki /**
906e5cc8ef3SRafael J. Wysocki  * acpi_subsys_runtime_suspend - Suspend device using ACPI.
907e5cc8ef3SRafael J. Wysocki  * @dev: Device to suspend.
908e5cc8ef3SRafael J. Wysocki  *
909e5cc8ef3SRafael J. Wysocki  * Carry out the generic runtime suspend procedure for @dev and use ACPI to put
910e5cc8ef3SRafael J. Wysocki  * it into a runtime low-power state.
911e5cc8ef3SRafael J. Wysocki  */
912e5cc8ef3SRafael J. Wysocki int acpi_subsys_runtime_suspend(struct device *dev)
913e5cc8ef3SRafael J. Wysocki {
914e5cc8ef3SRafael J. Wysocki 	int ret = pm_generic_runtime_suspend(dev);
915e5cc8ef3SRafael J. Wysocki 	return ret ? ret : acpi_dev_runtime_suspend(dev);
916e5cc8ef3SRafael J. Wysocki }
917e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend);
918e5cc8ef3SRafael J. Wysocki 
919e5cc8ef3SRafael J. Wysocki /**
920e5cc8ef3SRafael J. Wysocki  * acpi_subsys_runtime_resume - Resume device using ACPI.
921e5cc8ef3SRafael J. Wysocki  * @dev: Device to Resume.
922e5cc8ef3SRafael J. Wysocki  *
923e5cc8ef3SRafael J. Wysocki  * Use ACPI to put the given device into the full-power state and carry out the
924e5cc8ef3SRafael J. Wysocki  * generic runtime resume procedure for it.
925e5cc8ef3SRafael J. Wysocki  */
926e5cc8ef3SRafael J. Wysocki int acpi_subsys_runtime_resume(struct device *dev)
927e5cc8ef3SRafael J. Wysocki {
92863705c40SRafael J. Wysocki 	int ret = acpi_dev_resume(dev);
929e5cc8ef3SRafael J. Wysocki 	return ret ? ret : pm_generic_runtime_resume(dev);
930e5cc8ef3SRafael J. Wysocki }
931e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume);
932e5cc8ef3SRafael J. Wysocki 
933e5cc8ef3SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP
934e5cc8ef3SRafael J. Wysocki /**
935e5cc8ef3SRafael J. Wysocki  * acpi_dev_suspend_late - Put device into a low-power state using ACPI.
936e5cc8ef3SRafael J. Wysocki  * @dev: Device to put into a low-power state.
937e5cc8ef3SRafael J. Wysocki  *
938e5cc8ef3SRafael J. Wysocki  * Put the given device into a low-power state during system transition to a
939e5cc8ef3SRafael J. Wysocki  * sleep state using the standard ACPI mechanism.  Set up system wakeup if
940e5cc8ef3SRafael J. Wysocki  * desired, choose the state to put the device into (this checks if system
941e5cc8ef3SRafael J. Wysocki  * wakeup is expected to work too), and set the power state of the device.
942e5cc8ef3SRafael J. Wysocki  */
943e5cc8ef3SRafael J. Wysocki int acpi_dev_suspend_late(struct device *dev)
944e5cc8ef3SRafael J. Wysocki {
94579c0373fSRafael J. Wysocki 	struct acpi_device *adev = ACPI_COMPANION(dev);
946e5cc8ef3SRafael J. Wysocki 	u32 target_state;
947e5cc8ef3SRafael J. Wysocki 	bool wakeup;
948e5cc8ef3SRafael J. Wysocki 	int error;
949e5cc8ef3SRafael J. Wysocki 
950e5cc8ef3SRafael J. Wysocki 	if (!adev)
951e5cc8ef3SRafael J. Wysocki 		return 0;
952e5cc8ef3SRafael J. Wysocki 
953e5cc8ef3SRafael J. Wysocki 	target_state = acpi_target_system_state();
95478579b7cSRafael J. Wysocki 	wakeup = device_may_wakeup(dev) && acpi_device_can_wakeup(adev);
95599d8845eSRafael J. Wysocki 	if (wakeup) {
95699d8845eSRafael J. Wysocki 		error = acpi_device_wakeup_enable(adev, target_state);
95799d8845eSRafael J. Wysocki 		if (error)
958e5cc8ef3SRafael J. Wysocki 			return error;
95999d8845eSRafael J. Wysocki 	}
960e5cc8ef3SRafael J. Wysocki 
961e5cc8ef3SRafael J. Wysocki 	error = acpi_dev_pm_low_power(dev, adev, target_state);
96299d8845eSRafael J. Wysocki 	if (error && wakeup)
96399d8845eSRafael J. Wysocki 		acpi_device_wakeup_disable(adev);
964e5cc8ef3SRafael J. Wysocki 
965e5cc8ef3SRafael J. Wysocki 	return error;
966e5cc8ef3SRafael J. Wysocki }
967e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_suspend_late);
968e5cc8ef3SRafael J. Wysocki 
969e5cc8ef3SRafael J. Wysocki /**
970e5cc8ef3SRafael J. Wysocki  * acpi_subsys_prepare - Prepare device for system transition to a sleep state.
971e5cc8ef3SRafael J. Wysocki  * @dev: Device to prepare.
972e5cc8ef3SRafael J. Wysocki  */
973e5cc8ef3SRafael J. Wysocki int acpi_subsys_prepare(struct device *dev)
974e5cc8ef3SRafael J. Wysocki {
975f25c0ae2SRafael J. Wysocki 	struct acpi_device *adev = ACPI_COMPANION(dev);
976f25c0ae2SRafael J. Wysocki 	u32 sys_target;
977f25c0ae2SRafael J. Wysocki 	int ret, state;
97892858c47SRafael J. Wysocki 
979f25c0ae2SRafael J. Wysocki 	ret = pm_generic_prepare(dev);
980f25c0ae2SRafael J. Wysocki 	if (ret < 0)
981f25c0ae2SRafael J. Wysocki 		return ret;
982f25c0ae2SRafael J. Wysocki 
983f25c0ae2SRafael J. Wysocki 	if (!adev || !pm_runtime_suspended(dev)
984f25c0ae2SRafael J. Wysocki 	    || device_may_wakeup(dev) != !!adev->wakeup.prepare_count)
985f25c0ae2SRafael J. Wysocki 		return 0;
986f25c0ae2SRafael J. Wysocki 
987f25c0ae2SRafael J. Wysocki 	sys_target = acpi_target_system_state();
988f25c0ae2SRafael J. Wysocki 	if (sys_target == ACPI_STATE_S0)
989f25c0ae2SRafael J. Wysocki 		return 1;
990f25c0ae2SRafael J. Wysocki 
991f25c0ae2SRafael J. Wysocki 	if (adev->power.flags.dsw_present)
992f25c0ae2SRafael J. Wysocki 		return 0;
993f25c0ae2SRafael J. Wysocki 
994f25c0ae2SRafael J. Wysocki 	ret = acpi_dev_pm_get_state(dev, adev, sys_target, NULL, &state);
995f25c0ae2SRafael J. Wysocki 	return !ret && state == adev->power.state;
996e5cc8ef3SRafael J. Wysocki }
997e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_prepare);
998e5cc8ef3SRafael J. Wysocki 
999e5cc8ef3SRafael J. Wysocki /**
1000*e4da817dSUlf Hansson  * acpi_subsys_complete - Finalize device's resume during system resume.
1001*e4da817dSUlf Hansson  * @dev: Device to handle.
1002*e4da817dSUlf Hansson  */
1003*e4da817dSUlf Hansson void acpi_subsys_complete(struct device *dev)
1004*e4da817dSUlf Hansson {
1005*e4da817dSUlf Hansson 	pm_generic_complete(dev);
1006*e4da817dSUlf Hansson 	/*
1007*e4da817dSUlf Hansson 	 * If the device had been runtime-suspended before the system went into
1008*e4da817dSUlf Hansson 	 * the sleep state it is going out of and it has never been resumed till
1009*e4da817dSUlf Hansson 	 * now, resume it in case the firmware powered it up.
1010*e4da817dSUlf Hansson 	 */
1011*e4da817dSUlf Hansson 	if (dev->power.direct_complete && pm_resume_via_firmware())
1012*e4da817dSUlf Hansson 		pm_request_resume(dev);
1013*e4da817dSUlf Hansson }
1014*e4da817dSUlf Hansson EXPORT_SYMBOL_GPL(acpi_subsys_complete);
1015*e4da817dSUlf Hansson 
1016*e4da817dSUlf Hansson /**
101792858c47SRafael J. Wysocki  * acpi_subsys_suspend - Run the device driver's suspend callback.
101892858c47SRafael J. Wysocki  * @dev: Device to handle.
101992858c47SRafael J. Wysocki  *
102092858c47SRafael J. Wysocki  * Follow PCI and resume devices suspended at run time before running their
102192858c47SRafael J. Wysocki  * system suspend callbacks.
102292858c47SRafael J. Wysocki  */
102392858c47SRafael J. Wysocki int acpi_subsys_suspend(struct device *dev)
102492858c47SRafael J. Wysocki {
102592858c47SRafael J. Wysocki 	pm_runtime_resume(dev);
102692858c47SRafael J. Wysocki 	return pm_generic_suspend(dev);
102792858c47SRafael J. Wysocki }
10284cf563c5SHeikki Krogerus EXPORT_SYMBOL_GPL(acpi_subsys_suspend);
102992858c47SRafael J. Wysocki 
103092858c47SRafael J. Wysocki /**
1031e5cc8ef3SRafael J. Wysocki  * acpi_subsys_suspend_late - Suspend device using ACPI.
1032e5cc8ef3SRafael J. Wysocki  * @dev: Device to suspend.
1033e5cc8ef3SRafael J. Wysocki  *
1034e5cc8ef3SRafael J. Wysocki  * Carry out the generic late suspend procedure for @dev and use ACPI to put
1035e5cc8ef3SRafael J. Wysocki  * it into a low-power state during system transition into a sleep state.
1036e5cc8ef3SRafael J. Wysocki  */
1037e5cc8ef3SRafael J. Wysocki int acpi_subsys_suspend_late(struct device *dev)
1038e5cc8ef3SRafael J. Wysocki {
1039e5cc8ef3SRafael J. Wysocki 	int ret = pm_generic_suspend_late(dev);
1040e5cc8ef3SRafael J. Wysocki 	return ret ? ret : acpi_dev_suspend_late(dev);
1041e5cc8ef3SRafael J. Wysocki }
1042e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late);
1043e5cc8ef3SRafael J. Wysocki 
1044e5cc8ef3SRafael J. Wysocki /**
1045e5cc8ef3SRafael J. Wysocki  * acpi_subsys_resume_early - Resume device using ACPI.
1046e5cc8ef3SRafael J. Wysocki  * @dev: Device to Resume.
1047e5cc8ef3SRafael J. Wysocki  *
1048e5cc8ef3SRafael J. Wysocki  * Use ACPI to put the given device into the full-power state and carry out the
1049e5cc8ef3SRafael J. Wysocki  * generic early resume procedure for it during system transition into the
1050e5cc8ef3SRafael J. Wysocki  * working state.
1051e5cc8ef3SRafael J. Wysocki  */
1052e5cc8ef3SRafael J. Wysocki int acpi_subsys_resume_early(struct device *dev)
1053e5cc8ef3SRafael J. Wysocki {
105463705c40SRafael J. Wysocki 	int ret = acpi_dev_resume(dev);
1055e5cc8ef3SRafael J. Wysocki 	return ret ? ret : pm_generic_resume_early(dev);
1056e5cc8ef3SRafael J. Wysocki }
1057e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_resume_early);
105892858c47SRafael J. Wysocki 
105992858c47SRafael J. Wysocki /**
106092858c47SRafael J. Wysocki  * acpi_subsys_freeze - Run the device driver's freeze callback.
106192858c47SRafael J. Wysocki  * @dev: Device to handle.
106292858c47SRafael J. Wysocki  */
106392858c47SRafael J. Wysocki int acpi_subsys_freeze(struct device *dev)
106492858c47SRafael J. Wysocki {
106592858c47SRafael J. Wysocki 	/*
106692858c47SRafael J. Wysocki 	 * This used to be done in acpi_subsys_prepare() for all devices and
106792858c47SRafael J. Wysocki 	 * some drivers may depend on it, so do it here.  Ideally, however,
106892858c47SRafael J. Wysocki 	 * runtime-suspended devices should not be touched during freeze/thaw
106992858c47SRafael J. Wysocki 	 * transitions.
107092858c47SRafael J. Wysocki 	 */
107192858c47SRafael J. Wysocki 	pm_runtime_resume(dev);
107292858c47SRafael J. Wysocki 	return pm_generic_freeze(dev);
107392858c47SRafael J. Wysocki }
10744cf563c5SHeikki Krogerus EXPORT_SYMBOL_GPL(acpi_subsys_freeze);
107592858c47SRafael J. Wysocki 
1076e5cc8ef3SRafael J. Wysocki #endif /* CONFIG_PM_SLEEP */
1077e5cc8ef3SRafael J. Wysocki 
1078e5cc8ef3SRafael J. Wysocki static struct dev_pm_domain acpi_general_pm_domain = {
1079e5cc8ef3SRafael J. Wysocki 	.ops = {
1080e5cc8ef3SRafael J. Wysocki 		.runtime_suspend = acpi_subsys_runtime_suspend,
1081e5cc8ef3SRafael J. Wysocki 		.runtime_resume = acpi_subsys_runtime_resume,
1082e5cc8ef3SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP
1083e5cc8ef3SRafael J. Wysocki 		.prepare = acpi_subsys_prepare,
1084*e4da817dSUlf Hansson 		.complete = acpi_subsys_complete,
108592858c47SRafael J. Wysocki 		.suspend = acpi_subsys_suspend,
1086e5cc8ef3SRafael J. Wysocki 		.suspend_late = acpi_subsys_suspend_late,
1087e5cc8ef3SRafael J. Wysocki 		.resume_early = acpi_subsys_resume_early,
108892858c47SRafael J. Wysocki 		.freeze = acpi_subsys_freeze,
108992858c47SRafael J. Wysocki 		.poweroff = acpi_subsys_suspend,
1090e5cc8ef3SRafael J. Wysocki 		.poweroff_late = acpi_subsys_suspend_late,
1091e5cc8ef3SRafael J. Wysocki 		.restore_early = acpi_subsys_resume_early,
1092e5cc8ef3SRafael J. Wysocki #endif
1093e5cc8ef3SRafael J. Wysocki 	},
1094e5cc8ef3SRafael J. Wysocki };
1095e5cc8ef3SRafael J. Wysocki 
1096e5cc8ef3SRafael J. Wysocki /**
109791d66cd2SUlf Hansson  * acpi_dev_pm_detach - Remove ACPI power management from the device.
109891d66cd2SUlf Hansson  * @dev: Device to take care of.
109991d66cd2SUlf Hansson  * @power_off: Whether or not to try to remove power from the device.
110091d66cd2SUlf Hansson  *
110191d66cd2SUlf Hansson  * Remove the device from the general ACPI PM domain and remove its wakeup
110291d66cd2SUlf Hansson  * notifier.  If @power_off is set, additionally remove power from the device if
110391d66cd2SUlf Hansson  * possible.
110491d66cd2SUlf Hansson  *
110591d66cd2SUlf Hansson  * Callers must ensure proper synchronization of this function with power
110691d66cd2SUlf Hansson  * management callbacks.
110791d66cd2SUlf Hansson  */
110891d66cd2SUlf Hansson static void acpi_dev_pm_detach(struct device *dev, bool power_off)
110991d66cd2SUlf Hansson {
111091d66cd2SUlf Hansson 	struct acpi_device *adev = ACPI_COMPANION(dev);
111191d66cd2SUlf Hansson 
111291d66cd2SUlf Hansson 	if (adev && dev->pm_domain == &acpi_general_pm_domain) {
1113989561deSTomeu Vizoso 		dev_pm_domain_set(dev, NULL);
111491d66cd2SUlf Hansson 		acpi_remove_pm_notifier(adev);
111591d66cd2SUlf Hansson 		if (power_off) {
111691d66cd2SUlf Hansson 			/*
111791d66cd2SUlf Hansson 			 * If the device's PM QoS resume latency limit or flags
111891d66cd2SUlf Hansson 			 * have been exposed to user space, they have to be
111991d66cd2SUlf Hansson 			 * hidden at this point, so that they don't affect the
112091d66cd2SUlf Hansson 			 * choice of the low-power state to put the device into.
112191d66cd2SUlf Hansson 			 */
112291d66cd2SUlf Hansson 			dev_pm_qos_hide_latency_limit(dev);
112391d66cd2SUlf Hansson 			dev_pm_qos_hide_flags(dev);
112499d8845eSRafael J. Wysocki 			acpi_device_wakeup_disable(adev);
112591d66cd2SUlf Hansson 			acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
112691d66cd2SUlf Hansson 		}
112791d66cd2SUlf Hansson 	}
112891d66cd2SUlf Hansson }
112991d66cd2SUlf Hansson 
113091d66cd2SUlf Hansson /**
1131e5cc8ef3SRafael J. Wysocki  * acpi_dev_pm_attach - Prepare device for ACPI power management.
1132e5cc8ef3SRafael J. Wysocki  * @dev: Device to prepare.
1133b88ce2a4SRafael J. Wysocki  * @power_on: Whether or not to power on the device.
1134e5cc8ef3SRafael J. Wysocki  *
1135e5cc8ef3SRafael J. Wysocki  * If @dev has a valid ACPI handle that has a valid struct acpi_device object
1136e5cc8ef3SRafael J. Wysocki  * attached to it, install a wakeup notification handler for the device and
1137b88ce2a4SRafael J. Wysocki  * add it to the general ACPI PM domain.  If @power_on is set, the device will
1138b88ce2a4SRafael J. Wysocki  * be put into the ACPI D0 state before the function returns.
1139e5cc8ef3SRafael J. Wysocki  *
1140e5cc8ef3SRafael J. Wysocki  * This assumes that the @dev's bus type uses generic power management callbacks
1141e5cc8ef3SRafael J. Wysocki  * (or doesn't use any power management callbacks at all).
1142e5cc8ef3SRafael J. Wysocki  *
1143e5cc8ef3SRafael J. Wysocki  * Callers must ensure proper synchronization of this function with power
1144e5cc8ef3SRafael J. Wysocki  * management callbacks.
1145e5cc8ef3SRafael J. Wysocki  */
1146b88ce2a4SRafael J. Wysocki int acpi_dev_pm_attach(struct device *dev, bool power_on)
1147e5cc8ef3SRafael J. Wysocki {
114879c0373fSRafael J. Wysocki 	struct acpi_device *adev = ACPI_COMPANION(dev);
1149e5cc8ef3SRafael J. Wysocki 
1150e5cc8ef3SRafael J. Wysocki 	if (!adev)
1151e5cc8ef3SRafael J. Wysocki 		return -ENODEV;
1152e5cc8ef3SRafael J. Wysocki 
1153e5cc8ef3SRafael J. Wysocki 	if (dev->pm_domain)
1154e5cc8ef3SRafael J. Wysocki 		return -EEXIST;
1155e5cc8ef3SRafael J. Wysocki 
1156712e960fSMika Westerberg 	/*
1157712e960fSMika Westerberg 	 * Only attach the power domain to the first device if the
1158712e960fSMika Westerberg 	 * companion is shared by multiple. This is to prevent doing power
1159712e960fSMika Westerberg 	 * management twice.
1160712e960fSMika Westerberg 	 */
1161712e960fSMika Westerberg 	if (!acpi_device_is_first_physical_node(adev, dev))
1162712e960fSMika Westerberg 		return -EBUSY;
1163712e960fSMika Westerberg 
1164c072530fSRafael J. Wysocki 	acpi_add_pm_notifier(adev, dev, acpi_pm_notify_work_func);
1165989561deSTomeu Vizoso 	dev_pm_domain_set(dev, &acpi_general_pm_domain);
1166b88ce2a4SRafael J. Wysocki 	if (power_on) {
1167b88ce2a4SRafael J. Wysocki 		acpi_dev_pm_full_power(adev);
116899d8845eSRafael J. Wysocki 		acpi_device_wakeup_disable(adev);
1169b88ce2a4SRafael J. Wysocki 	}
117086f1e15fSUlf Hansson 
117186f1e15fSUlf Hansson 	dev->pm_domain->detach = acpi_dev_pm_detach;
1172e5cc8ef3SRafael J. Wysocki 	return 0;
1173e5cc8ef3SRafael J. Wysocki }
1174e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_pm_attach);
1175ec4602a9SRafael J. Wysocki #endif /* CONFIG_PM */
1176