xref: /linux/drivers/acpi/device_pm.c (revision 0ea5c948cb64bab5bc7a5516774eb8536f05aa0d)
11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2ec2cd81cSRafael J. Wysocki /*
3ec2cd81cSRafael J. Wysocki  * drivers/acpi/device_pm.c - ACPI device power management routines.
4ec2cd81cSRafael J. Wysocki  *
5ec2cd81cSRafael J. Wysocki  * Copyright (C) 2012, Intel Corp.
6ec2cd81cSRafael J. Wysocki  * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
7ec2cd81cSRafael J. Wysocki  *
8ec2cd81cSRafael J. Wysocki  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9ec2cd81cSRafael J. Wysocki  *
10ec2cd81cSRafael J. Wysocki  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11ec2cd81cSRafael J. Wysocki  */
12ec2cd81cSRafael J. Wysocki 
13255a04ccSRafael J. Wysocki #define pr_fmt(fmt) "PM: " fmt
14c56fd5eaSRafael J. Wysocki 
157b199811SRafael J. Wysocki #include <linux/acpi.h>
1686b3832cSRafael J. Wysocki #include <linux/export.h>
17ec2cd81cSRafael J. Wysocki #include <linux/mutex.h>
1886b3832cSRafael J. Wysocki #include <linux/pm_qos.h>
19989561deSTomeu Vizoso #include <linux/pm_domain.h>
20cd7bd02dSRafael J. Wysocki #include <linux/pm_runtime.h>
2133e4f80eSRafael J. Wysocki #include <linux/suspend.h>
22ec2cd81cSRafael J. Wysocki 
23b9370dceSRafael J. Wysocki #include "fan.h"
249ce4e607SRafael J. Wysocki #include "internal.h"
259ce4e607SRafael J. Wysocki 
2686b3832cSRafael J. Wysocki /**
279ce4e607SRafael J. Wysocki  * acpi_power_state_string - String representation of ACPI device power state.
289ce4e607SRafael J. Wysocki  * @state: ACPI device power state to return the string representation of.
299ce4e607SRafael J. Wysocki  */
acpi_power_state_string(int state)309ce4e607SRafael J. Wysocki const char *acpi_power_state_string(int state)
319ce4e607SRafael J. Wysocki {
329ce4e607SRafael J. Wysocki 	switch (state) {
339ce4e607SRafael J. Wysocki 	case ACPI_STATE_D0:
349ce4e607SRafael J. Wysocki 		return "D0";
359ce4e607SRafael J. Wysocki 	case ACPI_STATE_D1:
369ce4e607SRafael J. Wysocki 		return "D1";
379ce4e607SRafael J. Wysocki 	case ACPI_STATE_D2:
389ce4e607SRafael J. Wysocki 		return "D2";
399ce4e607SRafael J. Wysocki 	case ACPI_STATE_D3_HOT:
409ce4e607SRafael J. Wysocki 		return "D3hot";
419ce4e607SRafael J. Wysocki 	case ACPI_STATE_D3_COLD:
42898fee4fSRafael J. Wysocki 		return "D3cold";
439ce4e607SRafael J. Wysocki 	default:
449ce4e607SRafael J. Wysocki 		return "(unknown)";
459ce4e607SRafael J. Wysocki 	}
469ce4e607SRafael J. Wysocki }
479ce4e607SRafael J. Wysocki 
acpi_dev_pm_explicit_get(struct acpi_device * device,int * state)48f850a48aSRafael J. Wysocki static int acpi_dev_pm_explicit_get(struct acpi_device *device, int *state)
49f850a48aSRafael J. Wysocki {
50f850a48aSRafael J. Wysocki 	unsigned long long psc;
51f850a48aSRafael J. Wysocki 	acpi_status status;
52f850a48aSRafael J. Wysocki 
53f850a48aSRafael J. Wysocki 	status = acpi_evaluate_integer(device->handle, "_PSC", NULL, &psc);
54f850a48aSRafael J. Wysocki 	if (ACPI_FAILURE(status))
55f850a48aSRafael J. Wysocki 		return -ENODEV;
56f850a48aSRafael J. Wysocki 
57f850a48aSRafael J. Wysocki 	*state = psc;
58f850a48aSRafael J. Wysocki 	return 0;
59f850a48aSRafael J. Wysocki }
60f850a48aSRafael J. Wysocki 
619ce4e607SRafael J. Wysocki /**
629ce4e607SRafael J. Wysocki  * acpi_device_get_power - Get power state of an ACPI device.
639ce4e607SRafael J. Wysocki  * @device: Device to get the power state of.
649ce4e607SRafael J. Wysocki  * @state: Place to store the power state of the device.
659ce4e607SRafael J. Wysocki  *
669ce4e607SRafael J. Wysocki  * This function does not update the device's power.state field, but it may
679ce4e607SRafael J. Wysocki  * update its parent's power.state field (when the parent's power state is
689ce4e607SRafael J. Wysocki  * unknown and the device's power state turns out to be D0).
699ed411c0SRafael J. Wysocki  *
709ed411c0SRafael J. Wysocki  * Also, it does not update power resource reference counters to ensure that
719ed411c0SRafael J. Wysocki  * the power state returned by it will be persistent and it may return a power
729ed411c0SRafael J. Wysocki  * state shallower than previously set by acpi_device_set_power() for @device
739ed411c0SRafael J. Wysocki  * (if that power state depends on any power resources).
749ce4e607SRafael J. Wysocki  */
acpi_device_get_power(struct acpi_device * device,int * state)759ce4e607SRafael J. Wysocki int acpi_device_get_power(struct acpi_device *device, int *state)
769ce4e607SRafael J. Wysocki {
779ce4e607SRafael J. Wysocki 	int result = ACPI_STATE_UNKNOWN;
78d5008ef5SRafael J. Wysocki 	struct acpi_device *parent;
79f850a48aSRafael J. Wysocki 	int error;
809ce4e607SRafael J. Wysocki 
819ce4e607SRafael J. Wysocki 	if (!device || !state)
829ce4e607SRafael J. Wysocki 		return -EINVAL;
839ce4e607SRafael J. Wysocki 
84d5008ef5SRafael J. Wysocki 	parent = acpi_dev_parent(device);
85d5008ef5SRafael J. Wysocki 
869ce4e607SRafael J. Wysocki 	if (!device->flags.power_manageable) {
879ce4e607SRafael J. Wysocki 		/* TBD: Non-recursive algorithm for walking up hierarchy. */
8862fcb99bSRafael J. Wysocki 		*state = parent ? parent->power.state : ACPI_STATE_D0;
899ce4e607SRafael J. Wysocki 		goto out;
909ce4e607SRafael J. Wysocki 	}
919ce4e607SRafael J. Wysocki 
929ce4e607SRafael J. Wysocki 	/*
9375eb2d13SRafael J. Wysocki 	 * Get the device's power state from power resources settings and _PSC,
9475eb2d13SRafael J. Wysocki 	 * if available.
959ce4e607SRafael J. Wysocki 	 */
9675eb2d13SRafael J. Wysocki 	if (device->power.flags.power_resources) {
97f850a48aSRafael J. Wysocki 		error = acpi_power_get_inferred_state(device, &result);
989ce4e607SRafael J. Wysocki 		if (error)
999ce4e607SRafael J. Wysocki 			return error;
10075eb2d13SRafael J. Wysocki 	}
10175eb2d13SRafael J. Wysocki 	if (device->power.flags.explicit_get) {
102f850a48aSRafael J. Wysocki 		int psc;
10375eb2d13SRafael J. Wysocki 
104f850a48aSRafael J. Wysocki 		error = acpi_dev_pm_explicit_get(device, &psc);
105f850a48aSRafael J. Wysocki 		if (error)
106f850a48aSRafael J. Wysocki 			return error;
10775eb2d13SRafael J. Wysocki 
10875eb2d13SRafael J. Wysocki 		/*
10975eb2d13SRafael J. Wysocki 		 * The power resources settings may indicate a power state
11020dacb71SRafael J. Wysocki 		 * shallower than the actual power state of the device, because
11120dacb71SRafael J. Wysocki 		 * the same power resources may be referenced by other devices.
11275eb2d13SRafael J. Wysocki 		 *
11320dacb71SRafael J. Wysocki 		 * For systems predating ACPI 4.0 we assume that D3hot is the
11420dacb71SRafael J. Wysocki 		 * deepest state that can be supported.
11575eb2d13SRafael J. Wysocki 		 */
11675eb2d13SRafael J. Wysocki 		if (psc > result && psc < ACPI_STATE_D3_COLD)
11775eb2d13SRafael J. Wysocki 			result = psc;
11875eb2d13SRafael J. Wysocki 		else if (result == ACPI_STATE_UNKNOWN)
11920dacb71SRafael J. Wysocki 			result = psc > ACPI_STATE_D2 ? ACPI_STATE_D3_HOT : psc;
1209ce4e607SRafael J. Wysocki 	}
1219ce4e607SRafael J. Wysocki 
1229ce4e607SRafael J. Wysocki 	/*
1239ce4e607SRafael J. Wysocki 	 * If we were unsure about the device parent's power state up to this
1249ce4e607SRafael J. Wysocki 	 * point, the fact that the device is in D0 implies that the parent has
125644f17adSMika Westerberg 	 * to be in D0 too, except if ignore_parent is set.
1269ce4e607SRafael J. Wysocki 	 */
12762fcb99bSRafael J. Wysocki 	if (!device->power.flags.ignore_parent && parent &&
12862fcb99bSRafael J. Wysocki 	    parent->power.state == ACPI_STATE_UNKNOWN &&
12962fcb99bSRafael J. Wysocki 	    result == ACPI_STATE_D0)
13062fcb99bSRafael J. Wysocki 		parent->power.state = ACPI_STATE_D0;
1319ce4e607SRafael J. Wysocki 
1329ce4e607SRafael J. Wysocki 	*state = result;
1339ce4e607SRafael J. Wysocki 
1349ce4e607SRafael J. Wysocki  out:
135198ee437SRafael J. Wysocki 	acpi_handle_debug(device->handle, "Power state: %s\n",
136c56fd5eaSRafael J. Wysocki 			  acpi_power_state_string(*state));
1379ce4e607SRafael J. Wysocki 
1389ce4e607SRafael J. Wysocki 	return 0;
1399ce4e607SRafael J. Wysocki }
1409ce4e607SRafael J. Wysocki 
acpi_dev_pm_explicit_set(struct acpi_device * adev,int state)1419c0f45e3SRafael J. Wysocki static int acpi_dev_pm_explicit_set(struct acpi_device *adev, int state)
1429c0f45e3SRafael J. Wysocki {
1439c0f45e3SRafael J. Wysocki 	if (adev->power.states[state].flags.explicit_set) {
1449c0f45e3SRafael J. Wysocki 		char method[5] = { '_', 'P', 'S', '0' + state, '\0' };
1459c0f45e3SRafael J. Wysocki 		acpi_status status;
1469c0f45e3SRafael J. Wysocki 
1479c0f45e3SRafael J. Wysocki 		status = acpi_evaluate_object(adev->handle, method, NULL, NULL);
1489c0f45e3SRafael J. Wysocki 		if (ACPI_FAILURE(status))
1499c0f45e3SRafael J. Wysocki 			return -ENODEV;
1509c0f45e3SRafael J. Wysocki 	}
1519c0f45e3SRafael J. Wysocki 	return 0;
1529c0f45e3SRafael J. Wysocki }
1539c0f45e3SRafael J. Wysocki 
1549ce4e607SRafael J. Wysocki /**
1559ce4e607SRafael J. Wysocki  * acpi_device_set_power - Set power state of an ACPI device.
1569ce4e607SRafael J. Wysocki  * @device: Device to set the power state of.
1579ce4e607SRafael J. Wysocki  * @state: New power state to set.
1589ce4e607SRafael J. Wysocki  *
1599ce4e607SRafael J. Wysocki  * Callers must ensure that the device is power manageable before using this
1609ce4e607SRafael J. Wysocki  * function.
1619ce4e607SRafael J. Wysocki  */
acpi_device_set_power(struct acpi_device * device,int state)1629ce4e607SRafael J. Wysocki int acpi_device_set_power(struct acpi_device *device, int state)
1639ce4e607SRafael J. Wysocki {
16420dacb71SRafael J. Wysocki 	int target_state = state;
1659ce4e607SRafael J. Wysocki 	int result = 0;
1669ce4e607SRafael J. Wysocki 
1672c7d132aSRafael J. Wysocki 	if (!device || !device->flags.power_manageable
1682c7d132aSRafael J. Wysocki 	    || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
1699ce4e607SRafael J. Wysocki 		return -EINVAL;
1709ce4e607SRafael J. Wysocki 
171ee8193eeSRafael J. Wysocki 	acpi_handle_debug(device->handle, "Power state change: %s -> %s\n",
172ee8193eeSRafael J. Wysocki 			  acpi_power_state_string(device->power.state),
173ee8193eeSRafael J. Wysocki 			  acpi_power_state_string(state));
174ee8193eeSRafael J. Wysocki 
1759ce4e607SRafael J. Wysocki 	/* Make sure this is a valid target state */
1769ce4e607SRafael J. Wysocki 
177f850a48aSRafael J. Wysocki 	/* There is a special case for D0 addressed below. */
1786dd4a29dSRafael J. Wysocki 	if (state > ACPI_STATE_D0 && state == device->power.state)
1796dd4a29dSRafael J. Wysocki 		goto no_change;
1809ce4e607SRafael J. Wysocki 
18120dacb71SRafael J. Wysocki 	if (state == ACPI_STATE_D3_COLD) {
18220dacb71SRafael J. Wysocki 		/*
18320dacb71SRafael J. Wysocki 		 * For transitions to D3cold we need to execute _PS3 and then
18420dacb71SRafael J. Wysocki 		 * possibly drop references to the power resources in use.
18520dacb71SRafael J. Wysocki 		 */
18620dacb71SRafael J. Wysocki 		state = ACPI_STATE_D3_HOT;
187956ad9d9SRafael J. Wysocki 		/* If D3cold is not supported, use D3hot as the target state. */
18820dacb71SRafael J. Wysocki 		if (!device->power.states[ACPI_STATE_D3_COLD].flags.valid)
18920dacb71SRafael J. Wysocki 			target_state = state;
19020dacb71SRafael J. Wysocki 	} else if (!device->power.states[state].flags.valid) {
191f4f3548dSRafael J. Wysocki 		acpi_handle_debug(device->handle, "Power state %s not supported\n",
1929ce4e607SRafael J. Wysocki 				  acpi_power_state_string(state));
1939ce4e607SRafael J. Wysocki 		return -ENODEV;
1949ce4e607SRafael J. Wysocki 	}
19520dacb71SRafael J. Wysocki 
196d5008ef5SRafael J. Wysocki 	if (!device->power.flags.ignore_parent) {
197d5008ef5SRafael J. Wysocki 		struct acpi_device *parent;
198d5008ef5SRafael J. Wysocki 
199d5008ef5SRafael J. Wysocki 		parent = acpi_dev_parent(device);
200d5008ef5SRafael J. Wysocki 		if (parent && state < parent->power.state) {
201f4f3548dSRafael J. Wysocki 			acpi_handle_debug(device->handle,
202f4f3548dSRafael J. Wysocki 					  "Cannot transition to %s for parent in %s\n",
203593298e6SAaron Lu 					  acpi_power_state_string(state),
20462fcb99bSRafael J. Wysocki 					  acpi_power_state_string(parent->power.state));
2059ce4e607SRafael J. Wysocki 			return -ENODEV;
2069ce4e607SRafael J. Wysocki 		}
207d5008ef5SRafael J. Wysocki 	}
2089ce4e607SRafael J. Wysocki 
209e78adb75SRafael J. Wysocki 	/*
210e78adb75SRafael J. Wysocki 	 * Transition Power
211e78adb75SRafael J. Wysocki 	 * ----------------
21220dacb71SRafael J. Wysocki 	 * In accordance with ACPI 6, _PSx is executed before manipulating power
21320dacb71SRafael J. Wysocki 	 * resources, unless the target state is D0, in which case _PS0 is
21420dacb71SRafael J. Wysocki 	 * supposed to be executed after turning the power resources on.
215e78adb75SRafael J. Wysocki 	 */
21620dacb71SRafael J. Wysocki 	if (state > ACPI_STATE_D0) {
21720dacb71SRafael J. Wysocki 		/*
21820dacb71SRafael J. Wysocki 		 * According to ACPI 6, devices cannot go from lower-power
21920dacb71SRafael J. Wysocki 		 * (deeper) states to higher-power (shallower) states.
22020dacb71SRafael J. Wysocki 		 */
22120dacb71SRafael J. Wysocki 		if (state < device->power.state) {
222f4f3548dSRafael J. Wysocki 			acpi_handle_debug(device->handle,
223f4f3548dSRafael J. Wysocki 					  "Cannot transition from %s to %s\n",
22420dacb71SRafael J. Wysocki 					  acpi_power_state_string(device->power.state),
22520dacb71SRafael J. Wysocki 					  acpi_power_state_string(state));
22620dacb71SRafael J. Wysocki 			return -ENODEV;
2279ce4e607SRafael J. Wysocki 		}
22820dacb71SRafael J. Wysocki 
22921ba2379SRafael J. Wysocki 		/*
23021ba2379SRafael J. Wysocki 		 * If the device goes from D3hot to D3cold, _PS3 has been
23121ba2379SRafael J. Wysocki 		 * evaluated for it already, so skip it in that case.
23221ba2379SRafael J. Wysocki 		 */
23321ba2379SRafael J. Wysocki 		if (device->power.state < ACPI_STATE_D3_HOT) {
234e78adb75SRafael J. Wysocki 			result = acpi_dev_pm_explicit_set(device, state);
235e78adb75SRafael J. Wysocki 			if (result)
236e78adb75SRafael J. Wysocki 				goto end;
23721ba2379SRafael J. Wysocki 		}
2389ce4e607SRafael J. Wysocki 
23920dacb71SRafael J. Wysocki 		if (device->power.flags.power_resources)
24020dacb71SRafael J. Wysocki 			result = acpi_power_transition(device, target_state);
24120dacb71SRafael J. Wysocki 	} else {
24242787ed7SRafael J. Wysocki 		int cur_state = device->power.state;
24342787ed7SRafael J. Wysocki 
24420dacb71SRafael J. Wysocki 		if (device->power.flags.power_resources) {
24520dacb71SRafael J. Wysocki 			result = acpi_power_transition(device, ACPI_STATE_D0);
24620dacb71SRafael J. Wysocki 			if (result)
24720dacb71SRafael J. Wysocki 				goto end;
24820dacb71SRafael J. Wysocki 		}
249f850a48aSRafael J. Wysocki 
25042787ed7SRafael J. Wysocki 		if (cur_state == ACPI_STATE_D0) {
251f850a48aSRafael J. Wysocki 			int psc;
252f850a48aSRafael J. Wysocki 
253f850a48aSRafael J. Wysocki 			/* Nothing to do here if _PSC is not present. */
254f850a48aSRafael J. Wysocki 			if (!device->power.flags.explicit_get)
2556dd4a29dSRafael J. Wysocki 				goto no_change;
256f850a48aSRafael J. Wysocki 
257f850a48aSRafael J. Wysocki 			/*
258f850a48aSRafael J. Wysocki 			 * The power state of the device was set to D0 last
259f850a48aSRafael J. Wysocki 			 * time, but that might have happened before a
260f850a48aSRafael J. Wysocki 			 * system-wide transition involving the platform
261f850a48aSRafael J. Wysocki 			 * firmware, so it may be necessary to evaluate _PS0
262f850a48aSRafael J. Wysocki 			 * for the device here.  However, use extra care here
263f850a48aSRafael J. Wysocki 			 * and evaluate _PSC to check the device's current power
264f850a48aSRafael J. Wysocki 			 * state, and only invoke _PS0 if the evaluation of _PSC
265f850a48aSRafael J. Wysocki 			 * is successful and it returns a power state different
266f850a48aSRafael J. Wysocki 			 * from D0.
267f850a48aSRafael J. Wysocki 			 */
268f850a48aSRafael J. Wysocki 			result = acpi_dev_pm_explicit_get(device, &psc);
269f850a48aSRafael J. Wysocki 			if (result || psc == ACPI_STATE_D0)
2706dd4a29dSRafael J. Wysocki 				goto no_change;
271f850a48aSRafael J. Wysocki 		}
272f850a48aSRafael J. Wysocki 
27320dacb71SRafael J. Wysocki 		result = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0);
274e5656271SRafael J. Wysocki 	}
2759ce4e607SRafael J. Wysocki 
2769ce4e607SRafael J. Wysocki end:
277e78adb75SRafael J. Wysocki 	if (result) {
278f4f3548dSRafael J. Wysocki 		acpi_handle_debug(device->handle,
279f4f3548dSRafael J. Wysocki 				  "Failed to change power state to %s\n",
280a9b760b0SKai-Heng Feng 				  acpi_power_state_string(target_state));
281e78adb75SRafael J. Wysocki 	} else {
28271b65445SMika Westerberg 		device->power.state = target_state;
283f4f3548dSRafael J. Wysocki 		acpi_handle_debug(device->handle, "Power state changed to %s\n",
284c56fd5eaSRafael J. Wysocki 				  acpi_power_state_string(target_state));
2859ce4e607SRafael J. Wysocki 	}
2869ce4e607SRafael J. Wysocki 
2879ce4e607SRafael J. Wysocki 	return result;
2886dd4a29dSRafael J. Wysocki 
2896dd4a29dSRafael J. Wysocki no_change:
2906dd4a29dSRafael J. Wysocki 	acpi_handle_debug(device->handle, "Already in %s\n",
2916dd4a29dSRafael J. Wysocki 			  acpi_power_state_string(state));
2926dd4a29dSRafael J. Wysocki 	return 0;
2939ce4e607SRafael J. Wysocki }
2949ce4e607SRafael J. Wysocki EXPORT_SYMBOL(acpi_device_set_power);
2959ce4e607SRafael J. Wysocki 
acpi_bus_set_power(acpi_handle handle,int state)2969ce4e607SRafael J. Wysocki int acpi_bus_set_power(acpi_handle handle, int state)
2979ce4e607SRafael J. Wysocki {
29899ece713SRafael J. Wysocki 	struct acpi_device *device = acpi_fetch_acpi_dev(handle);
2999ce4e607SRafael J. Wysocki 
30099ece713SRafael J. Wysocki 	if (device)
3019ce4e607SRafael J. Wysocki 		return acpi_device_set_power(device, state);
30299ece713SRafael J. Wysocki 
30399ece713SRafael J. Wysocki 	return -ENODEV;
3049ce4e607SRafael J. Wysocki }
3059ce4e607SRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_set_power);
3069ce4e607SRafael J. Wysocki 
acpi_bus_init_power(struct acpi_device * device)3079ce4e607SRafael J. Wysocki int acpi_bus_init_power(struct acpi_device *device)
3089ce4e607SRafael J. Wysocki {
3099ce4e607SRafael J. Wysocki 	int state;
3109ce4e607SRafael J. Wysocki 	int result;
3119ce4e607SRafael J. Wysocki 
3129ce4e607SRafael J. Wysocki 	if (!device)
3139ce4e607SRafael J. Wysocki 		return -EINVAL;
3149ce4e607SRafael J. Wysocki 
3159ce4e607SRafael J. Wysocki 	device->power.state = ACPI_STATE_UNKNOWN;
316cde1f95fSSakari Ailus 	if (!acpi_device_is_present(device)) {
317cde1f95fSSakari Ailus 		device->flags.initialized = false;
3181b1f3e16SRafael J. Wysocki 		return -ENXIO;
319cde1f95fSSakari Ailus 	}
3209ce4e607SRafael J. Wysocki 
3219ce4e607SRafael J. Wysocki 	result = acpi_device_get_power(device, &state);
3229ce4e607SRafael J. Wysocki 	if (result)
3239ce4e607SRafael J. Wysocki 		return result;
3249ce4e607SRafael J. Wysocki 
325a2367807SRafael J. Wysocki 	if (state < ACPI_STATE_D3_COLD && device->power.flags.power_resources) {
32620dacb71SRafael J. Wysocki 		/* Reference count the power resources. */
3279ce4e607SRafael J. Wysocki 		result = acpi_power_on_resources(device, state);
328a2367807SRafael J. Wysocki 		if (result)
3299ce4e607SRafael J. Wysocki 			return result;
330a2367807SRafael J. Wysocki 
33120dacb71SRafael J. Wysocki 		if (state == ACPI_STATE_D0) {
33220dacb71SRafael J. Wysocki 			/*
33320dacb71SRafael J. Wysocki 			 * If _PSC is not present and the state inferred from
33420dacb71SRafael J. Wysocki 			 * power resources appears to be D0, it still may be
33520dacb71SRafael J. Wysocki 			 * necessary to execute _PS0 at this point, because
33620dacb71SRafael J. Wysocki 			 * another device using the same power resources may
33720dacb71SRafael J. Wysocki 			 * have been put into D0 previously and that's why we
33820dacb71SRafael J. Wysocki 			 * see D0 here.
33920dacb71SRafael J. Wysocki 			 */
3409c0f45e3SRafael J. Wysocki 			result = acpi_dev_pm_explicit_set(device, state);
3419c0f45e3SRafael J. Wysocki 			if (result)
3429c0f45e3SRafael J. Wysocki 				return result;
34320dacb71SRafael J. Wysocki 		}
344b3785492SRafael J. Wysocki 	} else if (state == ACPI_STATE_UNKNOWN) {
3457cd8407dSRafael J. Wysocki 		/*
3467cd8407dSRafael J. Wysocki 		 * No power resources and missing _PSC?  Cross fingers and make
3477cd8407dSRafael J. Wysocki 		 * it D0 in hope that this is what the BIOS put the device into.
3487cd8407dSRafael J. Wysocki 		 * [We tried to force D0 here by executing _PS0, but that broke
3497cd8407dSRafael J. Wysocki 		 * Toshiba P870-303 in a nasty way.]
3507cd8407dSRafael J. Wysocki 		 */
351b3785492SRafael J. Wysocki 		state = ACPI_STATE_D0;
352a2367807SRafael J. Wysocki 	}
353a2367807SRafael J. Wysocki 	device->power.state = state;
354a2367807SRafael J. Wysocki 	return 0;
3559ce4e607SRafael J. Wysocki }
3569ce4e607SRafael J. Wysocki 
357b9e95fc6SRafael J. Wysocki /**
358b9e95fc6SRafael J. Wysocki  * acpi_device_fix_up_power - Force device with missing _PSC into D0.
359b9e95fc6SRafael J. Wysocki  * @device: Device object whose power state is to be fixed up.
360b9e95fc6SRafael J. Wysocki  *
361b9e95fc6SRafael J. Wysocki  * Devices without power resources and _PSC, but having _PS0 and _PS3 defined,
362b9e95fc6SRafael J. Wysocki  * are assumed to be put into D0 by the BIOS.  However, in some cases that may
363b9e95fc6SRafael J. Wysocki  * not be the case and this function should be used then.
364b9e95fc6SRafael J. Wysocki  */
acpi_device_fix_up_power(struct acpi_device * device)365b9e95fc6SRafael J. Wysocki int acpi_device_fix_up_power(struct acpi_device *device)
366b9e95fc6SRafael J. Wysocki {
367b9e95fc6SRafael J. Wysocki 	int ret = 0;
368b9e95fc6SRafael J. Wysocki 
369b9e95fc6SRafael J. Wysocki 	if (!device->power.flags.power_resources
370b9e95fc6SRafael J. Wysocki 	    && !device->power.flags.explicit_get
371b9e95fc6SRafael J. Wysocki 	    && device->power.state == ACPI_STATE_D0)
372b9e95fc6SRafael J. Wysocki 		ret = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0);
373b9e95fc6SRafael J. Wysocki 
374b9e95fc6SRafael J. Wysocki 	return ret;
375b9e95fc6SRafael J. Wysocki }
37678a898d0SUlf Hansson EXPORT_SYMBOL_GPL(acpi_device_fix_up_power);
377b9e95fc6SRafael J. Wysocki 
fix_up_power_if_applicable(struct acpi_device * adev,void * not_used)378a22f18bdSRafael J. Wysocki static int fix_up_power_if_applicable(struct acpi_device *adev, void *not_used)
379a22f18bdSRafael J. Wysocki {
380a22f18bdSRafael J. Wysocki 	if (adev->status.present && adev->status.enabled)
381a22f18bdSRafael J. Wysocki 		acpi_device_fix_up_power(adev);
382a22f18bdSRafael J. Wysocki 
383a22f18bdSRafael J. Wysocki 	return 0;
384a22f18bdSRafael J. Wysocki }
385a22f18bdSRafael J. Wysocki 
386a22f18bdSRafael J. Wysocki /**
387a22f18bdSRafael J. Wysocki  * acpi_device_fix_up_power_extended - Force device and its children into D0.
388a22f18bdSRafael J. Wysocki  * @adev: Parent device object whose power state is to be fixed up.
389a22f18bdSRafael J. Wysocki  *
390a22f18bdSRafael J. Wysocki  * Call acpi_device_fix_up_power() for @adev and its children so long as they
391a22f18bdSRafael J. Wysocki  * are reported as present and enabled.
392a22f18bdSRafael J. Wysocki  */
acpi_device_fix_up_power_extended(struct acpi_device * adev)393a22f18bdSRafael J. Wysocki void acpi_device_fix_up_power_extended(struct acpi_device *adev)
394a22f18bdSRafael J. Wysocki {
395a22f18bdSRafael J. Wysocki 	acpi_device_fix_up_power(adev);
396a22f18bdSRafael J. Wysocki 	acpi_dev_for_each_child(adev, fix_up_power_if_applicable, NULL);
397a22f18bdSRafael J. Wysocki }
398a22f18bdSRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_device_fix_up_power_extended);
399a22f18bdSRafael J. Wysocki 
400*37ba91a8SHans de Goede /**
401*37ba91a8SHans de Goede  * acpi_device_fix_up_power_children - Force a device's children into D0.
402*37ba91a8SHans de Goede  * @adev: Parent device object whose children's power state is to be fixed up.
403*37ba91a8SHans de Goede  *
404*37ba91a8SHans de Goede  * Call acpi_device_fix_up_power() for @adev's children so long as they
405*37ba91a8SHans de Goede  * are reported as present and enabled.
406*37ba91a8SHans de Goede  */
acpi_device_fix_up_power_children(struct acpi_device * adev)407*37ba91a8SHans de Goede void acpi_device_fix_up_power_children(struct acpi_device *adev)
408*37ba91a8SHans de Goede {
409*37ba91a8SHans de Goede 	acpi_dev_for_each_child(adev, fix_up_power_if_applicable, NULL);
410*37ba91a8SHans de Goede }
411*37ba91a8SHans de Goede EXPORT_SYMBOL_GPL(acpi_device_fix_up_power_children);
412*37ba91a8SHans de Goede 
acpi_device_update_power(struct acpi_device * device,int * state_p)413202317a5SRafael J. Wysocki int acpi_device_update_power(struct acpi_device *device, int *state_p)
4149ce4e607SRafael J. Wysocki {
4159ce4e607SRafael J. Wysocki 	int state;
4169ce4e607SRafael J. Wysocki 	int result;
4179ce4e607SRafael J. Wysocki 
418202317a5SRafael J. Wysocki 	if (device->power.state == ACPI_STATE_UNKNOWN) {
419202317a5SRafael J. Wysocki 		result = acpi_bus_init_power(device);
420202317a5SRafael J. Wysocki 		if (!result && state_p)
421202317a5SRafael J. Wysocki 			*state_p = device->power.state;
422202317a5SRafael J. Wysocki 
4239ce4e607SRafael J. Wysocki 		return result;
424202317a5SRafael J. Wysocki 	}
4259ce4e607SRafael J. Wysocki 
4269ce4e607SRafael J. Wysocki 	result = acpi_device_get_power(device, &state);
4279ce4e607SRafael J. Wysocki 	if (result)
4289ce4e607SRafael J. Wysocki 		return result;
4299ce4e607SRafael J. Wysocki 
43091bdad0bSRafael J. Wysocki 	if (state == ACPI_STATE_UNKNOWN) {
431511d5c42SRafael J. Wysocki 		state = ACPI_STATE_D0;
4329ce4e607SRafael J. Wysocki 		result = acpi_device_set_power(device, state);
43391bdad0bSRafael J. Wysocki 		if (result)
43491bdad0bSRafael J. Wysocki 			return result;
43591bdad0bSRafael J. Wysocki 	} else {
43691bdad0bSRafael J. Wysocki 		if (device->power.flags.power_resources) {
43791bdad0bSRafael J. Wysocki 			/*
43891bdad0bSRafael J. Wysocki 			 * We don't need to really switch the state, bu we need
43991bdad0bSRafael J. Wysocki 			 * to update the power resources' reference counters.
44091bdad0bSRafael J. Wysocki 			 */
44191bdad0bSRafael J. Wysocki 			result = acpi_power_transition(device, state);
44291bdad0bSRafael J. Wysocki 			if (result)
44391bdad0bSRafael J. Wysocki 				return result;
44491bdad0bSRafael J. Wysocki 		}
44591bdad0bSRafael J. Wysocki 		device->power.state = state;
44691bdad0bSRafael J. Wysocki 	}
44791bdad0bSRafael J. Wysocki 	if (state_p)
4489ce4e607SRafael J. Wysocki 		*state_p = state;
4499ce4e607SRafael J. Wysocki 
45091bdad0bSRafael J. Wysocki 	return 0;
4519ce4e607SRafael J. Wysocki }
4522bb3a2bfSAaron Lu EXPORT_SYMBOL_GPL(acpi_device_update_power);
453202317a5SRafael J. Wysocki 
acpi_bus_update_power(acpi_handle handle,int * state_p)454202317a5SRafael J. Wysocki int acpi_bus_update_power(acpi_handle handle, int *state_p)
455202317a5SRafael J. Wysocki {
45699ece713SRafael J. Wysocki 	struct acpi_device *device = acpi_fetch_acpi_dev(handle);
457202317a5SRafael J. Wysocki 
45899ece713SRafael J. Wysocki 	if (device)
45999ece713SRafael J. Wysocki 		return acpi_device_update_power(device, state_p);
46099ece713SRafael J. Wysocki 
46199ece713SRafael J. Wysocki 	return -ENODEV;
462202317a5SRafael J. Wysocki }
4639ce4e607SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_bus_update_power);
4649ce4e607SRafael J. Wysocki 
acpi_bus_power_manageable(acpi_handle handle)4659ce4e607SRafael J. Wysocki bool acpi_bus_power_manageable(acpi_handle handle)
4669ce4e607SRafael J. Wysocki {
46799ece713SRafael J. Wysocki 	struct acpi_device *device = acpi_fetch_acpi_dev(handle);
4689ce4e607SRafael J. Wysocki 
46999ece713SRafael J. Wysocki 	return device && device->flags.power_manageable;
4709ce4e607SRafael J. Wysocki }
4719ce4e607SRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_power_manageable);
4729ce4e607SRafael J. Wysocki 
acpi_power_up_if_adr_present(struct acpi_device * adev,void * not_used)47310fa1b2cSRafael J. Wysocki static int acpi_power_up_if_adr_present(struct acpi_device *adev, void *not_used)
474b7dd6298SRafael J. Wysocki {
475b7dd6298SRafael J. Wysocki 	if (!(adev->flags.power_manageable && adev->pnp.type.bus_address))
476b7dd6298SRafael J. Wysocki 		return 0;
477b7dd6298SRafael J. Wysocki 
478b7dd6298SRafael J. Wysocki 	acpi_handle_debug(adev->handle, "Power state: %s\n",
479b7dd6298SRafael J. Wysocki 			  acpi_power_state_string(adev->power.state));
480b7dd6298SRafael J. Wysocki 
481b7dd6298SRafael J. Wysocki 	if (adev->power.state == ACPI_STATE_D3_COLD)
482b7dd6298SRafael J. Wysocki 		return acpi_device_set_power(adev, ACPI_STATE_D0);
483b7dd6298SRafael J. Wysocki 
484b7dd6298SRafael J. Wysocki 	return 0;
485b7dd6298SRafael J. Wysocki }
486b7dd6298SRafael J. Wysocki 
487b7dd6298SRafael J. Wysocki /**
488b7dd6298SRafael J. Wysocki  * acpi_dev_power_up_children_with_adr - Power up childres with valid _ADR
489b7dd6298SRafael J. Wysocki  * @adev: Parent ACPI device object.
490b7dd6298SRafael J. Wysocki  *
491b7dd6298SRafael J. Wysocki  * Change the power states of the direct children of @adev that are in D3cold
492b7dd6298SRafael J. Wysocki  * and hold valid _ADR objects to D0 in order to allow bus (e.g. PCI)
493b7dd6298SRafael J. Wysocki  * enumeration code to access them.
494b7dd6298SRafael J. Wysocki  */
acpi_dev_power_up_children_with_adr(struct acpi_device * adev)495b7dd6298SRafael J. Wysocki void acpi_dev_power_up_children_with_adr(struct acpi_device *adev)
496b7dd6298SRafael J. Wysocki {
497b7dd6298SRafael J. Wysocki 	acpi_dev_for_each_child(adev, acpi_power_up_if_adr_present, NULL);
498b7dd6298SRafael J. Wysocki }
499b7dd6298SRafael J. Wysocki 
5008133844aSRafael J. Wysocki /**
5018133844aSRafael J. Wysocki  * acpi_dev_power_state_for_wake - Deepest power state for wakeup signaling
5028133844aSRafael J. Wysocki  * @adev: ACPI companion of the target device.
5038133844aSRafael J. Wysocki  *
5048133844aSRafael J. Wysocki  * Evaluate _S0W for @adev and return the value produced by it or return
5058133844aSRafael J. Wysocki  * ACPI_STATE_UNKNOWN on errors (including _S0W not present).
5068133844aSRafael J. Wysocki  */
acpi_dev_power_state_for_wake(struct acpi_device * adev)5078133844aSRafael J. Wysocki u8 acpi_dev_power_state_for_wake(struct acpi_device *adev)
5088133844aSRafael J. Wysocki {
5098133844aSRafael J. Wysocki 	unsigned long long state;
5108133844aSRafael J. Wysocki 	acpi_status status;
5118133844aSRafael J. Wysocki 
5128133844aSRafael J. Wysocki 	status = acpi_evaluate_integer(adev->handle, "_S0W", NULL, &state);
5138133844aSRafael J. Wysocki 	if (ACPI_FAILURE(status))
5148133844aSRafael J. Wysocki 		return ACPI_STATE_UNKNOWN;
5158133844aSRafael J. Wysocki 
5168133844aSRafael J. Wysocki 	return state;
5178133844aSRafael J. Wysocki }
5188133844aSRafael J. Wysocki 
519ec4602a9SRafael J. Wysocki #ifdef CONFIG_PM
520ec4602a9SRafael J. Wysocki static DEFINE_MUTEX(acpi_pm_notifier_lock);
521ff165679SVille Syrjälä static DEFINE_MUTEX(acpi_pm_notifier_install_lock);
522ec4602a9SRafael J. Wysocki 
acpi_pm_wakeup_event(struct device * dev)52333e4f80eSRafael J. Wysocki void acpi_pm_wakeup_event(struct device *dev)
52433e4f80eSRafael J. Wysocki {
52533e4f80eSRafael J. Wysocki 	pm_wakeup_dev_event(dev, 0, acpi_s2idle_wakeup());
52633e4f80eSRafael J. Wysocki }
52733e4f80eSRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_pm_wakeup_event);
52833e4f80eSRafael J. Wysocki 
acpi_pm_notify_handler(acpi_handle handle,u32 val,void * not_used)529c072530fSRafael J. Wysocki static void acpi_pm_notify_handler(acpi_handle handle, u32 val, void *not_used)
530c072530fSRafael J. Wysocki {
531c072530fSRafael J. Wysocki 	struct acpi_device *adev;
532c072530fSRafael J. Wysocki 
533c072530fSRafael J. Wysocki 	if (val != ACPI_NOTIFY_DEVICE_WAKE)
534c072530fSRafael J. Wysocki 		return;
535c072530fSRafael J. Wysocki 
536020a6375SRafael J. Wysocki 	acpi_handle_debug(handle, "Wake notify\n");
537020a6375SRafael J. Wysocki 
53845e9aa1fSRafael J. Wysocki 	adev = acpi_get_acpi_dev(handle);
539c072530fSRafael J. Wysocki 	if (!adev)
540c072530fSRafael J. Wysocki 		return;
541c072530fSRafael J. Wysocki 
542c072530fSRafael J. Wysocki 	mutex_lock(&acpi_pm_notifier_lock);
543c072530fSRafael J. Wysocki 
544c072530fSRafael J. Wysocki 	if (adev->wakeup.flags.notifier_present) {
54533e4f80eSRafael J. Wysocki 		pm_wakeup_ws_event(adev->wakeup.ws, 0, acpi_s2idle_wakeup());
546020a6375SRafael J. Wysocki 		if (adev->wakeup.context.func) {
547d75f773cSSakari Ailus 			acpi_handle_debug(handle, "Running %pS for %s\n",
548020a6375SRafael J. Wysocki 					  adev->wakeup.context.func,
549020a6375SRafael J. Wysocki 					  dev_name(adev->wakeup.context.dev));
55064fd1c70SRafael J. Wysocki 			adev->wakeup.context.func(&adev->wakeup.context);
551c072530fSRafael J. Wysocki 		}
552020a6375SRafael J. Wysocki 	}
553c072530fSRafael J. Wysocki 
554c072530fSRafael J. Wysocki 	mutex_unlock(&acpi_pm_notifier_lock);
555c072530fSRafael J. Wysocki 
55645e9aa1fSRafael J. Wysocki 	acpi_put_acpi_dev(adev);
557c072530fSRafael J. Wysocki }
558c072530fSRafael J. Wysocki 
559ec4602a9SRafael J. Wysocki /**
560c072530fSRafael J. Wysocki  * acpi_add_pm_notifier - Register PM notify handler for given ACPI device.
561c072530fSRafael J. Wysocki  * @adev: ACPI device to add the notify handler for.
562c072530fSRafael J. Wysocki  * @dev: Device to generate a wakeup event for while handling the notification.
56364fd1c70SRafael J. Wysocki  * @func: Work function to execute when handling the notification.
564ec4602a9SRafael J. Wysocki  *
565ec4602a9SRafael J. Wysocki  * NOTE: @adev need not be a run-wake or wakeup device to be a valid source of
566ec4602a9SRafael J. Wysocki  * PM wakeup events.  For example, wakeup events may be generated for bridges
567ec4602a9SRafael J. Wysocki  * if one of the devices below the bridge is signaling wakeup, even if the
568ec4602a9SRafael J. Wysocki  * bridge itself doesn't have a wakeup GPE associated with it.
569ec4602a9SRafael J. Wysocki  */
acpi_add_pm_notifier(struct acpi_device * adev,struct device * dev,void (* func)(struct acpi_device_wakeup_context * context))570c072530fSRafael J. Wysocki acpi_status acpi_add_pm_notifier(struct acpi_device *adev, struct device *dev,
57164fd1c70SRafael J. Wysocki 			void (*func)(struct acpi_device_wakeup_context *context))
572ec4602a9SRafael J. Wysocki {
573ec4602a9SRafael J. Wysocki 	acpi_status status = AE_ALREADY_EXISTS;
574ec4602a9SRafael J. Wysocki 
57564fd1c70SRafael J. Wysocki 	if (!dev && !func)
576c072530fSRafael J. Wysocki 		return AE_BAD_PARAMETER;
577c072530fSRafael J. Wysocki 
578ff165679SVille Syrjälä 	mutex_lock(&acpi_pm_notifier_install_lock);
579ec4602a9SRafael J. Wysocki 
580ec4602a9SRafael J. Wysocki 	if (adev->wakeup.flags.notifier_present)
581ec4602a9SRafael J. Wysocki 		goto out;
582ec4602a9SRafael J. Wysocki 
583c072530fSRafael J. Wysocki 	status = acpi_install_notify_handler(adev->handle, ACPI_SYSTEM_NOTIFY,
584c072530fSRafael J. Wysocki 					     acpi_pm_notify_handler, NULL);
585ec4602a9SRafael J. Wysocki 	if (ACPI_FAILURE(status))
586ec4602a9SRafael J. Wysocki 		goto out;
587ec4602a9SRafael J. Wysocki 
588ff165679SVille Syrjälä 	mutex_lock(&acpi_pm_notifier_lock);
589c8377adfSTri Vo 	adev->wakeup.ws = wakeup_source_register(&adev->dev,
590c8377adfSTri Vo 						 dev_name(&adev->dev));
591ff165679SVille Syrjälä 	adev->wakeup.context.dev = dev;
592ff165679SVille Syrjälä 	adev->wakeup.context.func = func;
593ec4602a9SRafael J. Wysocki 	adev->wakeup.flags.notifier_present = true;
594ff165679SVille Syrjälä 	mutex_unlock(&acpi_pm_notifier_lock);
595ec4602a9SRafael J. Wysocki 
596ec4602a9SRafael J. Wysocki  out:
597ff165679SVille Syrjälä 	mutex_unlock(&acpi_pm_notifier_install_lock);
598ec4602a9SRafael J. Wysocki 	return status;
599ec4602a9SRafael J. Wysocki }
600ec4602a9SRafael J. Wysocki 
601ec4602a9SRafael J. Wysocki /**
602ec4602a9SRafael J. Wysocki  * acpi_remove_pm_notifier - Unregister PM notifier from given ACPI device.
603ec4602a9SRafael J. Wysocki  * @adev: ACPI device to remove the notifier from.
604ec4602a9SRafael J. Wysocki  */
acpi_remove_pm_notifier(struct acpi_device * adev)605c072530fSRafael J. Wysocki acpi_status acpi_remove_pm_notifier(struct acpi_device *adev)
606ec4602a9SRafael J. Wysocki {
607ec4602a9SRafael J. Wysocki 	acpi_status status = AE_BAD_PARAMETER;
608ec4602a9SRafael J. Wysocki 
609ff165679SVille Syrjälä 	mutex_lock(&acpi_pm_notifier_install_lock);
610ec4602a9SRafael J. Wysocki 
611ec4602a9SRafael J. Wysocki 	if (!adev->wakeup.flags.notifier_present)
612ec4602a9SRafael J. Wysocki 		goto out;
613ec4602a9SRafael J. Wysocki 
614ec4602a9SRafael J. Wysocki 	status = acpi_remove_notify_handler(adev->handle,
615ec4602a9SRafael J. Wysocki 					    ACPI_SYSTEM_NOTIFY,
616c072530fSRafael J. Wysocki 					    acpi_pm_notify_handler);
617ec4602a9SRafael J. Wysocki 	if (ACPI_FAILURE(status))
618ec4602a9SRafael J. Wysocki 		goto out;
619ec4602a9SRafael J. Wysocki 
620ff165679SVille Syrjälä 	mutex_lock(&acpi_pm_notifier_lock);
62164fd1c70SRafael J. Wysocki 	adev->wakeup.context.func = NULL;
622c072530fSRafael J. Wysocki 	adev->wakeup.context.dev = NULL;
623c072530fSRafael J. Wysocki 	wakeup_source_unregister(adev->wakeup.ws);
624ec4602a9SRafael J. Wysocki 	adev->wakeup.flags.notifier_present = false;
625ff165679SVille Syrjälä 	mutex_unlock(&acpi_pm_notifier_lock);
626ec4602a9SRafael J. Wysocki 
627ec4602a9SRafael J. Wysocki  out:
628ff165679SVille Syrjälä 	mutex_unlock(&acpi_pm_notifier_install_lock);
629ec4602a9SRafael J. Wysocki 	return status;
630ec4602a9SRafael J. Wysocki }
631ec4602a9SRafael J. Wysocki 
acpi_bus_can_wakeup(acpi_handle handle)6329ce4e607SRafael J. Wysocki bool acpi_bus_can_wakeup(acpi_handle handle)
6339ce4e607SRafael J. Wysocki {
63499ece713SRafael J. Wysocki 	struct acpi_device *device = acpi_fetch_acpi_dev(handle);
6359ce4e607SRafael J. Wysocki 
63699ece713SRafael J. Wysocki 	return device && device->wakeup.flags.valid;
6379ce4e607SRafael J. Wysocki }
6389ce4e607SRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_can_wakeup);
6399ce4e607SRafael J. Wysocki 
acpi_pm_device_can_wakeup(struct device * dev)6408370c2dcSRafael J. Wysocki bool acpi_pm_device_can_wakeup(struct device *dev)
6418370c2dcSRafael J. Wysocki {
6428370c2dcSRafael J. Wysocki 	struct acpi_device *adev = ACPI_COMPANION(dev);
6438370c2dcSRafael J. Wysocki 
6448370c2dcSRafael J. Wysocki 	return adev ? acpi_device_can_wakeup(adev) : false;
6458370c2dcSRafael J. Wysocki }
6468370c2dcSRafael J. Wysocki 
6479ce4e607SRafael J. Wysocki /**
648b25c77efSRafael J. Wysocki  * acpi_dev_pm_get_state - Get preferred power state of ACPI device.
64986b3832cSRafael J. Wysocki  * @dev: Device whose preferred target power state to return.
65086b3832cSRafael J. Wysocki  * @adev: ACPI device node corresponding to @dev.
65186b3832cSRafael J. Wysocki  * @target_state: System state to match the resultant device state.
652fa1675b5SRafael J. Wysocki  * @d_min_p: Location to store the highest power state available to the device.
653fa1675b5SRafael J. Wysocki  * @d_max_p: Location to store the lowest power state available to the device.
65486b3832cSRafael J. Wysocki  *
655fa1675b5SRafael J. Wysocki  * Find the lowest power (highest number) and highest power (lowest number) ACPI
656fa1675b5SRafael J. Wysocki  * device power states that the device can be in while the system is in the
657fa1675b5SRafael J. Wysocki  * state represented by @target_state.  Store the integer numbers representing
658fa1675b5SRafael J. Wysocki  * those stats in the memory locations pointed to by @d_max_p and @d_min_p,
659fa1675b5SRafael J. Wysocki  * respectively.
66086b3832cSRafael J. Wysocki  *
66186b3832cSRafael J. Wysocki  * Callers must ensure that @dev and @adev are valid pointers and that @adev
66286b3832cSRafael J. Wysocki  * actually corresponds to @dev before using this function.
663fa1675b5SRafael J. Wysocki  *
664fa1675b5SRafael J. Wysocki  * Returns 0 on success or -ENODATA when one of the ACPI methods fails or
665fa1675b5SRafael J. Wysocki  * returns a value that doesn't make sense.  The memory locations pointed to by
666fa1675b5SRafael J. Wysocki  * @d_max_p and @d_min_p are only modified on success.
66786b3832cSRafael J. Wysocki  */
acpi_dev_pm_get_state(struct device * dev,struct acpi_device * adev,u32 target_state,int * d_min_p,int * d_max_p)668b25c77efSRafael J. Wysocki static int acpi_dev_pm_get_state(struct device *dev, struct acpi_device *adev,
669fa1675b5SRafael J. Wysocki 				 u32 target_state, int *d_min_p, int *d_max_p)
67086b3832cSRafael J. Wysocki {
671fa1675b5SRafael J. Wysocki 	char method[] = { '_', 'S', '0' + target_state, 'D', '\0' };
672fa1675b5SRafael J. Wysocki 	acpi_handle handle = adev->handle;
673fa1675b5SRafael J. Wysocki 	unsigned long long ret;
674fa1675b5SRafael J. Wysocki 	int d_min, d_max;
67586b3832cSRafael J. Wysocki 	bool wakeup = false;
676bf8c6184SDaniel Drake 	bool has_sxd = false;
677fa1675b5SRafael J. Wysocki 	acpi_status status;
67886b3832cSRafael J. Wysocki 
67986b3832cSRafael J. Wysocki 	/*
680fa1675b5SRafael J. Wysocki 	 * If the system state is S0, the lowest power state the device can be
681fa1675b5SRafael J. Wysocki 	 * in is D3cold, unless the device has _S0W and is supposed to signal
682fa1675b5SRafael J. Wysocki 	 * wakeup, in which case the return value of _S0W has to be used as the
683fa1675b5SRafael J. Wysocki 	 * lowest power state available to the device.
68486b3832cSRafael J. Wysocki 	 */
68586b3832cSRafael J. Wysocki 	d_min = ACPI_STATE_D0;
6864c164ae7SRafael J. Wysocki 	d_max = ACPI_STATE_D3_COLD;
68786b3832cSRafael J. Wysocki 
68886b3832cSRafael J. Wysocki 	/*
68986b3832cSRafael J. Wysocki 	 * If present, _SxD methods return the minimum D-state (highest power
69086b3832cSRafael J. Wysocki 	 * state) we can use for the corresponding S-states.  Otherwise, the
69186b3832cSRafael J. Wysocki 	 * minimum D-state is D0 (ACPI 3.x).
69286b3832cSRafael J. Wysocki 	 */
69386b3832cSRafael J. Wysocki 	if (target_state > ACPI_STATE_S0) {
694fa1675b5SRafael J. Wysocki 		/*
695fa1675b5SRafael J. Wysocki 		 * We rely on acpi_evaluate_integer() not clobbering the integer
696fa1675b5SRafael J. Wysocki 		 * provided if AE_NOT_FOUND is returned.
697fa1675b5SRafael J. Wysocki 		 */
698fa1675b5SRafael J. Wysocki 		ret = d_min;
699fa1675b5SRafael J. Wysocki 		status = acpi_evaluate_integer(handle, method, NULL, &ret);
700fa1675b5SRafael J. Wysocki 		if ((ACPI_FAILURE(status) && status != AE_NOT_FOUND)
701fa1675b5SRafael J. Wysocki 		    || ret > ACPI_STATE_D3_COLD)
702fa1675b5SRafael J. Wysocki 			return -ENODATA;
703fa1675b5SRafael J. Wysocki 
704fa1675b5SRafael J. Wysocki 		/*
705fa1675b5SRafael J. Wysocki 		 * We need to handle legacy systems where D3hot and D3cold are
706fa1675b5SRafael J. Wysocki 		 * the same and 3 is returned in both cases, so fall back to
707fa1675b5SRafael J. Wysocki 		 * D3cold if D3hot is not a valid state.
708fa1675b5SRafael J. Wysocki 		 */
709fa1675b5SRafael J. Wysocki 		if (!adev->power.states[ret].flags.valid) {
710fa1675b5SRafael J. Wysocki 			if (ret == ACPI_STATE_D3_HOT)
711fa1675b5SRafael J. Wysocki 				ret = ACPI_STATE_D3_COLD;
712fa1675b5SRafael J. Wysocki 			else
713fa1675b5SRafael J. Wysocki 				return -ENODATA;
714fa1675b5SRafael J. Wysocki 		}
715bf8c6184SDaniel Drake 
716bf8c6184SDaniel Drake 		if (status == AE_OK)
717bf8c6184SDaniel Drake 			has_sxd = true;
718bf8c6184SDaniel Drake 
719fa1675b5SRafael J. Wysocki 		d_min = ret;
72086b3832cSRafael J. Wysocki 		wakeup = device_may_wakeup(dev) && adev->wakeup.flags.valid
72186b3832cSRafael J. Wysocki 			&& adev->wakeup.sleep_state >= target_state;
722a6c05e12SRaul E Rangel 	} else if (device_may_wakeup(dev) && dev->power.wakeirq) {
723a6c05e12SRaul E Rangel 		/*
724a6c05e12SRaul E Rangel 		 * The ACPI subsystem doesn't manage the wake bit for IRQs
725a6c05e12SRaul E Rangel 		 * defined with ExclusiveAndWake and SharedAndWake. Instead we
726a6c05e12SRaul E Rangel 		 * expect them to be managed via the PM subsystem. Drivers
727a6c05e12SRaul E Rangel 		 * should call dev_pm_set_wake_irq to register an IRQ as a wake
728a6c05e12SRaul E Rangel 		 * source.
729a6c05e12SRaul E Rangel 		 *
730a6c05e12SRaul E Rangel 		 * If a device has a wake IRQ attached we need to check the
731a6c05e12SRaul E Rangel 		 * _S0W method to get the correct wake D-state. Otherwise we
732a6c05e12SRaul E Rangel 		 * end up putting the device into D3Cold which will more than
733a6c05e12SRaul E Rangel 		 * likely disable wake functionality.
734a6c05e12SRaul E Rangel 		 */
735a6c05e12SRaul E Rangel 		wakeup = true;
73620f97cafSRafael J. Wysocki 	} else {
737a6c05e12SRaul E Rangel 		/* ACPI GPE is specified in _PRW. */
73886b3832cSRafael J. Wysocki 		wakeup = adev->wakeup.flags.valid;
73986b3832cSRafael J. Wysocki 	}
74086b3832cSRafael J. Wysocki 
74186b3832cSRafael J. Wysocki 	/*
74286b3832cSRafael J. Wysocki 	 * If _PRW says we can wake up the system from the target sleep state,
74386b3832cSRafael J. Wysocki 	 * the D-state returned by _SxD is sufficient for that (we assume a
74486b3832cSRafael J. Wysocki 	 * wakeup-aware driver if wake is set).  Still, if _SxW exists
74586b3832cSRafael J. Wysocki 	 * (ACPI 3.x), it should return the maximum (lowest power) D-state that
74686b3832cSRafael J. Wysocki 	 * can wake the system.  _S0W may be valid, too.
74786b3832cSRafael J. Wysocki 	 */
74886b3832cSRafael J. Wysocki 	if (wakeup) {
749fa1675b5SRafael J. Wysocki 		method[3] = 'W';
750fa1675b5SRafael J. Wysocki 		status = acpi_evaluate_integer(handle, method, NULL, &ret);
751fa1675b5SRafael J. Wysocki 		if (status == AE_NOT_FOUND) {
752bf8c6184SDaniel Drake 			/* No _SxW. In this case, the ACPI spec says that we
753bf8c6184SDaniel Drake 			 * must not go into any power state deeper than the
754bf8c6184SDaniel Drake 			 * value returned from _SxD.
755bf8c6184SDaniel Drake 			 */
756bf8c6184SDaniel Drake 			if (has_sxd && target_state > ACPI_STATE_S0)
75786b3832cSRafael J. Wysocki 				d_max = d_min;
758fa1675b5SRafael J. Wysocki 		} else if (ACPI_SUCCESS(status) && ret <= ACPI_STATE_D3_COLD) {
759fa1675b5SRafael J. Wysocki 			/* Fall back to D3cold if ret is not a valid state. */
760fa1675b5SRafael J. Wysocki 			if (!adev->power.states[ret].flags.valid)
761fa1675b5SRafael J. Wysocki 				ret = ACPI_STATE_D3_COLD;
762fa1675b5SRafael J. Wysocki 
763fa1675b5SRafael J. Wysocki 			d_max = ret > d_min ? ret : d_min;
764fa1675b5SRafael J. Wysocki 		} else {
765fa1675b5SRafael J. Wysocki 			return -ENODATA;
76686b3832cSRafael J. Wysocki 		}
76786b3832cSRafael J. Wysocki 	}
76886b3832cSRafael J. Wysocki 
76986b3832cSRafael J. Wysocki 	if (d_min_p)
77086b3832cSRafael J. Wysocki 		*d_min_p = d_min;
771fa1675b5SRafael J. Wysocki 
772fa1675b5SRafael J. Wysocki 	if (d_max_p)
773fa1675b5SRafael J. Wysocki 		*d_max_p = d_max;
774fa1675b5SRafael J. Wysocki 
775fa1675b5SRafael J. Wysocki 	return 0;
77686b3832cSRafael J. Wysocki }
777cd7bd02dSRafael J. Wysocki 
778a6ae7594SRafael J. Wysocki /**
779a6ae7594SRafael J. Wysocki  * acpi_pm_device_sleep_state - Get preferred power state of ACPI device.
780a6ae7594SRafael J. Wysocki  * @dev: Device whose preferred target power state to return.
781a6ae7594SRafael J. Wysocki  * @d_min_p: Location to store the upper limit of the allowed states range.
782a6ae7594SRafael J. Wysocki  * @d_max_in: Deepest low-power state to take into consideration.
783a6ae7594SRafael J. Wysocki  * Return value: Preferred power state of the device on success, -ENODEV
784fa1675b5SRafael J. Wysocki  * if there's no 'struct acpi_device' for @dev, -EINVAL if @d_max_in is
785fa1675b5SRafael J. Wysocki  * incorrect, or -ENODATA on ACPI method failure.
786a6ae7594SRafael J. Wysocki  *
787a6ae7594SRafael J. Wysocki  * The caller must ensure that @dev is valid before using this function.
788a6ae7594SRafael J. Wysocki  */
acpi_pm_device_sleep_state(struct device * dev,int * d_min_p,int d_max_in)789a6ae7594SRafael J. Wysocki int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in)
790a6ae7594SRafael J. Wysocki {
791a6ae7594SRafael J. Wysocki 	struct acpi_device *adev;
7929b5c7a5aSRafael J. Wysocki 	int ret, d_min, d_max;
793fa1675b5SRafael J. Wysocki 
794fa1675b5SRafael J. Wysocki 	if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3_COLD)
795fa1675b5SRafael J. Wysocki 		return -EINVAL;
796fa1675b5SRafael J. Wysocki 
79720dacb71SRafael J. Wysocki 	if (d_max_in > ACPI_STATE_D2) {
798fa1675b5SRafael J. Wysocki 		enum pm_qos_flags_status stat;
799fa1675b5SRafael J. Wysocki 
800fa1675b5SRafael J. Wysocki 		stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF);
801fa1675b5SRafael J. Wysocki 		if (stat == PM_QOS_FLAGS_ALL)
80220dacb71SRafael J. Wysocki 			d_max_in = ACPI_STATE_D2;
803fa1675b5SRafael J. Wysocki 	}
804a6ae7594SRafael J. Wysocki 
80517653a3eSRafael J. Wysocki 	adev = ACPI_COMPANION(dev);
80617653a3eSRafael J. Wysocki 	if (!adev) {
80717653a3eSRafael J. Wysocki 		dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);
808a6ae7594SRafael J. Wysocki 		return -ENODEV;
809a6ae7594SRafael J. Wysocki 	}
810a6ae7594SRafael J. Wysocki 
811fa1675b5SRafael J. Wysocki 	ret = acpi_dev_pm_get_state(dev, adev, acpi_target_system_state(),
8129b5c7a5aSRafael J. Wysocki 				    &d_min, &d_max);
813fa1675b5SRafael J. Wysocki 	if (ret)
814fa1675b5SRafael J. Wysocki 		return ret;
815fa1675b5SRafael J. Wysocki 
8169b5c7a5aSRafael J. Wysocki 	if (d_max_in < d_min)
817fa1675b5SRafael J. Wysocki 		return -EINVAL;
818fa1675b5SRafael J. Wysocki 
819fa1675b5SRafael J. Wysocki 	if (d_max > d_max_in) {
8209b5c7a5aSRafael J. Wysocki 		for (d_max = d_max_in; d_max > d_min; d_max--) {
821fa1675b5SRafael J. Wysocki 			if (adev->power.states[d_max].flags.valid)
822fa1675b5SRafael J. Wysocki 				break;
823fa1675b5SRafael J. Wysocki 		}
824fa1675b5SRafael J. Wysocki 	}
8259b5c7a5aSRafael J. Wysocki 
8269b5c7a5aSRafael J. Wysocki 	if (d_min_p)
8279b5c7a5aSRafael J. Wysocki 		*d_min_p = d_min;
8289b5c7a5aSRafael J. Wysocki 
829fa1675b5SRafael J. Wysocki 	return d_max;
830a6ae7594SRafael J. Wysocki }
831a6ae7594SRafael J. Wysocki EXPORT_SYMBOL(acpi_pm_device_sleep_state);
832a6ae7594SRafael J. Wysocki 
833cd7bd02dSRafael J. Wysocki /**
834c072530fSRafael J. Wysocki  * acpi_pm_notify_work_func - ACPI devices wakeup notification work function.
83564fd1c70SRafael J. Wysocki  * @context: Device wakeup context.
836e5cc8ef3SRafael J. Wysocki  */
acpi_pm_notify_work_func(struct acpi_device_wakeup_context * context)83764fd1c70SRafael J. Wysocki static void acpi_pm_notify_work_func(struct acpi_device_wakeup_context *context)
838e5cc8ef3SRafael J. Wysocki {
83964fd1c70SRafael J. Wysocki 	struct device *dev = context->dev;
840e5cc8ef3SRafael J. Wysocki 
841c072530fSRafael J. Wysocki 	if (dev) {
842e5cc8ef3SRafael J. Wysocki 		pm_wakeup_event(dev, 0);
84364fd1c70SRafael J. Wysocki 		pm_request_resume(dev);
844e5cc8ef3SRafael J. Wysocki 	}
845e5cc8ef3SRafael J. Wysocki }
846e5cc8ef3SRafael J. Wysocki 
84799d8845eSRafael J. Wysocki static DEFINE_MUTEX(acpi_wakeup_lock);
84899d8845eSRafael J. Wysocki 
__acpi_device_wakeup_enable(struct acpi_device * adev,u32 target_state)8491ba51a7cSRafael J. Wysocki static int __acpi_device_wakeup_enable(struct acpi_device *adev,
8507482c5cbSRafael J. Wysocki 				       u32 target_state)
8511ba51a7cSRafael J. Wysocki {
8521ba51a7cSRafael J. Wysocki 	struct acpi_device_wakeup *wakeup = &adev->wakeup;
8531ba51a7cSRafael J. Wysocki 	acpi_status status;
8541ba51a7cSRafael J. Wysocki 	int error = 0;
8551ba51a7cSRafael J. Wysocki 
8561ba51a7cSRafael J. Wysocki 	mutex_lock(&acpi_wakeup_lock);
8571ba51a7cSRafael J. Wysocki 
858b93b7ef6SRafael J. Wysocki 	/*
859b93b7ef6SRafael J. Wysocki 	 * If the device wakeup power is already enabled, disable it and enable
860b93b7ef6SRafael J. Wysocki 	 * it again in case it depends on the configuration of subordinate
861b93b7ef6SRafael J. Wysocki 	 * devices and the conditions have changed since it was enabled last
862b93b7ef6SRafael J. Wysocki 	 * time.
863b93b7ef6SRafael J. Wysocki 	 */
8641ba51a7cSRafael J. Wysocki 	if (wakeup->enable_count > 0)
865b93b7ef6SRafael J. Wysocki 		acpi_disable_wakeup_device_power(adev);
8661ba51a7cSRafael J. Wysocki 
8671ba51a7cSRafael J. Wysocki 	error = acpi_enable_wakeup_device_power(adev, target_state);
868b93b7ef6SRafael J. Wysocki 	if (error) {
869b93b7ef6SRafael J. Wysocki 		if (wakeup->enable_count > 0) {
870b93b7ef6SRafael J. Wysocki 			acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number);
871b93b7ef6SRafael J. Wysocki 			wakeup->enable_count = 0;
872b93b7ef6SRafael J. Wysocki 		}
8731ba51a7cSRafael J. Wysocki 		goto out;
874b93b7ef6SRafael J. Wysocki 	}
875b93b7ef6SRafael J. Wysocki 
876b93b7ef6SRafael J. Wysocki 	if (wakeup->enable_count > 0)
877b93b7ef6SRafael J. Wysocki 		goto inc;
8781ba51a7cSRafael J. Wysocki 
8791ba51a7cSRafael J. Wysocki 	status = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number);
8801ba51a7cSRafael J. Wysocki 	if (ACPI_FAILURE(status)) {
8811ba51a7cSRafael J. Wysocki 		acpi_disable_wakeup_device_power(adev);
8821ba51a7cSRafael J. Wysocki 		error = -EIO;
8831ba51a7cSRafael J. Wysocki 		goto out;
8841ba51a7cSRafael J. Wysocki 	}
8851ba51a7cSRafael J. Wysocki 
886fbc9418fSRafael J. Wysocki 	acpi_handle_debug(adev->handle, "GPE%2X enabled for wakeup\n",
887fbc9418fSRafael J. Wysocki 			  (unsigned int)wakeup->gpe_number);
888fbc9418fSRafael J. Wysocki 
8891ba51a7cSRafael J. Wysocki inc:
890b93b7ef6SRafael J. Wysocki 	if (wakeup->enable_count < INT_MAX)
8911ba51a7cSRafael J. Wysocki 		wakeup->enable_count++;
892b93b7ef6SRafael J. Wysocki 	else
893b93b7ef6SRafael J. Wysocki 		acpi_handle_info(adev->handle, "Wakeup enable count out of bounds!\n");
8941ba51a7cSRafael J. Wysocki 
8951ba51a7cSRafael J. Wysocki out:
8961ba51a7cSRafael J. Wysocki 	mutex_unlock(&acpi_wakeup_lock);
8971ba51a7cSRafael J. Wysocki 	return error;
8981ba51a7cSRafael J. Wysocki }
8991ba51a7cSRafael J. Wysocki 
900e5cc8ef3SRafael J. Wysocki /**
90199d8845eSRafael J. Wysocki  * acpi_device_wakeup_enable - Enable wakeup functionality for device.
90299d8845eSRafael J. Wysocki  * @adev: ACPI device to enable wakeup functionality for.
903f35cec25SRafael J. Wysocki  * @target_state: State the system is transitioning into.
904cd7bd02dSRafael J. Wysocki  *
90599d8845eSRafael J. Wysocki  * Enable the GPE associated with @adev so that it can generate wakeup signals
90699d8845eSRafael J. Wysocki  * for the device in response to external (remote) events and enable wakeup
90799d8845eSRafael J. Wysocki  * power for it.
908dee8370cSRafael J. Wysocki  *
909dee8370cSRafael J. Wysocki  * Callers must ensure that @adev is a valid ACPI device node before executing
910dee8370cSRafael J. Wysocki  * this function.
911dee8370cSRafael J. Wysocki  */
acpi_device_wakeup_enable(struct acpi_device * adev,u32 target_state)91299d8845eSRafael J. Wysocki static int acpi_device_wakeup_enable(struct acpi_device *adev, u32 target_state)
913dee8370cSRafael J. Wysocki {
9147482c5cbSRafael J. Wysocki 	return __acpi_device_wakeup_enable(adev, target_state);
91599d8845eSRafael J. Wysocki }
91699d8845eSRafael J. Wysocki 
91799d8845eSRafael J. Wysocki /**
91899d8845eSRafael J. Wysocki  * acpi_device_wakeup_disable - Disable wakeup functionality for device.
91999d8845eSRafael J. Wysocki  * @adev: ACPI device to disable wakeup functionality for.
92099d8845eSRafael J. Wysocki  *
92199d8845eSRafael J. Wysocki  * Disable the GPE associated with @adev and disable wakeup power for it.
92299d8845eSRafael J. Wysocki  *
92399d8845eSRafael J. Wysocki  * Callers must ensure that @adev is a valid ACPI device node before executing
92499d8845eSRafael J. Wysocki  * this function.
92599d8845eSRafael J. Wysocki  */
acpi_device_wakeup_disable(struct acpi_device * adev)92699d8845eSRafael J. Wysocki static void acpi_device_wakeup_disable(struct acpi_device *adev)
92799d8845eSRafael J. Wysocki {
92899d8845eSRafael J. Wysocki 	struct acpi_device_wakeup *wakeup = &adev->wakeup;
92999d8845eSRafael J. Wysocki 
93099d8845eSRafael J. Wysocki 	mutex_lock(&acpi_wakeup_lock);
93199d8845eSRafael J. Wysocki 
93299d8845eSRafael J. Wysocki 	if (!wakeup->enable_count)
93399d8845eSRafael J. Wysocki 		goto out;
93499d8845eSRafael J. Wysocki 
935dee8370cSRafael J. Wysocki 	acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number);
936dee8370cSRafael J. Wysocki 	acpi_disable_wakeup_device_power(adev);
93799d8845eSRafael J. Wysocki 
93899d8845eSRafael J. Wysocki 	wakeup->enable_count--;
93999d8845eSRafael J. Wysocki 
94099d8845eSRafael J. Wysocki out:
94199d8845eSRafael J. Wysocki 	mutex_unlock(&acpi_wakeup_lock);
942dee8370cSRafael J. Wysocki }
943dee8370cSRafael J. Wysocki 
9447482c5cbSRafael J. Wysocki /**
9457482c5cbSRafael J. Wysocki  * acpi_pm_set_device_wakeup - Enable/disable remote wakeup for given device.
9467482c5cbSRafael J. Wysocki  * @dev: Device to enable/disable to generate wakeup events.
9477482c5cbSRafael J. Wysocki  * @enable: Whether to enable or disable the wakeup functionality.
9487482c5cbSRafael J. Wysocki  */
acpi_pm_set_device_wakeup(struct device * dev,bool enable)9497482c5cbSRafael J. Wysocki int acpi_pm_set_device_wakeup(struct device *dev, bool enable)
950a6ae7594SRafael J. Wysocki {
951a6ae7594SRafael J. Wysocki 	struct acpi_device *adev;
952a6ae7594SRafael J. Wysocki 	int error;
953a6ae7594SRafael J. Wysocki 
95417653a3eSRafael J. Wysocki 	adev = ACPI_COMPANION(dev);
95517653a3eSRafael J. Wysocki 	if (!adev) {
95617653a3eSRafael J. Wysocki 		dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);
957a6ae7594SRafael J. Wysocki 		return -ENODEV;
958a6ae7594SRafael J. Wysocki 	}
959a6ae7594SRafael J. Wysocki 
9604d183d04SRafael J. Wysocki 	if (!acpi_device_can_wakeup(adev))
9614d183d04SRafael J. Wysocki 		return -EINVAL;
9624d183d04SRafael J. Wysocki 
96399d8845eSRafael J. Wysocki 	if (!enable) {
96499d8845eSRafael J. Wysocki 		acpi_device_wakeup_disable(adev);
96599d8845eSRafael J. Wysocki 		dev_dbg(dev, "Wakeup disabled by ACPI\n");
96699d8845eSRafael J. Wysocki 		return 0;
96799d8845eSRafael J. Wysocki 	}
96899d8845eSRafael J. Wysocki 
9697482c5cbSRafael J. Wysocki 	error = __acpi_device_wakeup_enable(adev, acpi_target_system_state());
970a6ae7594SRafael J. Wysocki 	if (!error)
97199d8845eSRafael J. Wysocki 		dev_dbg(dev, "Wakeup enabled by ACPI\n");
972a6ae7594SRafael J. Wysocki 
973a6ae7594SRafael J. Wysocki 	return error;
974a6ae7594SRafael J. Wysocki }
9751ba51a7cSRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_pm_set_device_wakeup);
9761ba51a7cSRafael J. Wysocki 
9771ba51a7cSRafael J. Wysocki /**
978e5cc8ef3SRafael J. Wysocki  * acpi_dev_pm_low_power - Put ACPI device into a low-power state.
979e5cc8ef3SRafael J. Wysocki  * @dev: Device to put into a low-power state.
980e5cc8ef3SRafael J. Wysocki  * @adev: ACPI device node corresponding to @dev.
981e5cc8ef3SRafael J. Wysocki  * @system_state: System state to choose the device state for.
982e5cc8ef3SRafael J. Wysocki  */
acpi_dev_pm_low_power(struct device * dev,struct acpi_device * adev,u32 system_state)983e5cc8ef3SRafael J. Wysocki static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev,
984e5cc8ef3SRafael J. Wysocki 				 u32 system_state)
985e5cc8ef3SRafael J. Wysocki {
986fa1675b5SRafael J. Wysocki 	int ret, state;
987e5cc8ef3SRafael J. Wysocki 
988e5cc8ef3SRafael J. Wysocki 	if (!acpi_device_power_manageable(adev))
989e5cc8ef3SRafael J. Wysocki 		return 0;
990e5cc8ef3SRafael J. Wysocki 
991fa1675b5SRafael J. Wysocki 	ret = acpi_dev_pm_get_state(dev, adev, system_state, NULL, &state);
992fa1675b5SRafael J. Wysocki 	return ret ? ret : acpi_device_set_power(adev, state);
993e5cc8ef3SRafael J. Wysocki }
994e5cc8ef3SRafael J. Wysocki 
995e5cc8ef3SRafael J. Wysocki /**
996e5cc8ef3SRafael J. Wysocki  * acpi_dev_pm_full_power - Put ACPI device into the full-power state.
997e5cc8ef3SRafael J. Wysocki  * @adev: ACPI device node to put into the full-power state.
998e5cc8ef3SRafael J. Wysocki  */
acpi_dev_pm_full_power(struct acpi_device * adev)999e5cc8ef3SRafael J. Wysocki static int acpi_dev_pm_full_power(struct acpi_device *adev)
1000e5cc8ef3SRafael J. Wysocki {
1001e5cc8ef3SRafael J. Wysocki 	return acpi_device_power_manageable(adev) ?
1002e5cc8ef3SRafael J. Wysocki 		acpi_device_set_power(adev, ACPI_STATE_D0) : 0;
1003e5cc8ef3SRafael J. Wysocki }
1004e5cc8ef3SRafael J. Wysocki 
1005e5cc8ef3SRafael J. Wysocki /**
1006cbe25ce3SRafael J. Wysocki  * acpi_dev_suspend - Put device into a low-power state using ACPI.
1007e5cc8ef3SRafael J. Wysocki  * @dev: Device to put into a low-power state.
1008cbe25ce3SRafael J. Wysocki  * @wakeup: Whether or not to enable wakeup for the device.
1009e5cc8ef3SRafael J. Wysocki  *
1010cbe25ce3SRafael J. Wysocki  * Put the given device into a low-power state using the standard ACPI
1011e5cc8ef3SRafael J. Wysocki  * mechanism.  Set up remote wakeup if desired, choose the state to put the
1012e5cc8ef3SRafael J. Wysocki  * device into (this checks if remote wakeup is expected to work too), and set
1013e5cc8ef3SRafael J. Wysocki  * the power state of the device.
1014e5cc8ef3SRafael J. Wysocki  */
acpi_dev_suspend(struct device * dev,bool wakeup)1015cbe25ce3SRafael J. Wysocki int acpi_dev_suspend(struct device *dev, bool wakeup)
1016e5cc8ef3SRafael J. Wysocki {
101779c0373fSRafael J. Wysocki 	struct acpi_device *adev = ACPI_COMPANION(dev);
1018cbe25ce3SRafael J. Wysocki 	u32 target_state = acpi_target_system_state();
1019e5cc8ef3SRafael J. Wysocki 	int error;
1020e5cc8ef3SRafael J. Wysocki 
1021e5cc8ef3SRafael J. Wysocki 	if (!adev)
1022e5cc8ef3SRafael J. Wysocki 		return 0;
1023e5cc8ef3SRafael J. Wysocki 
1024cbe25ce3SRafael J. Wysocki 	if (wakeup && acpi_device_can_wakeup(adev)) {
1025cbe25ce3SRafael J. Wysocki 		error = acpi_device_wakeup_enable(adev, target_state);
102699d8845eSRafael J. Wysocki 		if (error)
1027e5cc8ef3SRafael J. Wysocki 			return -EAGAIN;
1028cbe25ce3SRafael J. Wysocki 	} else {
1029cbe25ce3SRafael J. Wysocki 		wakeup = false;
103099d8845eSRafael J. Wysocki 	}
1031e5cc8ef3SRafael J. Wysocki 
1032cbe25ce3SRafael J. Wysocki 	error = acpi_dev_pm_low_power(dev, adev, target_state);
1033cbe25ce3SRafael J. Wysocki 	if (error && wakeup)
103499d8845eSRafael J. Wysocki 		acpi_device_wakeup_disable(adev);
1035e5cc8ef3SRafael J. Wysocki 
1036e5cc8ef3SRafael J. Wysocki 	return error;
1037e5cc8ef3SRafael J. Wysocki }
1038cbe25ce3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_suspend);
1039e5cc8ef3SRafael J. Wysocki 
1040e5cc8ef3SRafael J. Wysocki /**
104163705c40SRafael J. Wysocki  * acpi_dev_resume - Put device into the full-power state using ACPI.
1042e5cc8ef3SRafael J. Wysocki  * @dev: Device to put into the full-power state.
1043e5cc8ef3SRafael J. Wysocki  *
1044e5cc8ef3SRafael J. Wysocki  * Put the given device into the full-power state using the standard ACPI
104563705c40SRafael J. Wysocki  * mechanism.  Set the power state of the device to ACPI D0 and disable wakeup.
1046e5cc8ef3SRafael J. Wysocki  */
acpi_dev_resume(struct device * dev)104763705c40SRafael J. Wysocki int acpi_dev_resume(struct device *dev)
1048e5cc8ef3SRafael J. Wysocki {
104979c0373fSRafael J. Wysocki 	struct acpi_device *adev = ACPI_COMPANION(dev);
1050e5cc8ef3SRafael J. Wysocki 	int error;
1051e5cc8ef3SRafael J. Wysocki 
1052e5cc8ef3SRafael J. Wysocki 	if (!adev)
1053e5cc8ef3SRafael J. Wysocki 		return 0;
1054e5cc8ef3SRafael J. Wysocki 
1055e5cc8ef3SRafael J. Wysocki 	error = acpi_dev_pm_full_power(adev);
105699d8845eSRafael J. Wysocki 	acpi_device_wakeup_disable(adev);
1057e5cc8ef3SRafael J. Wysocki 	return error;
1058e5cc8ef3SRafael J. Wysocki }
105963705c40SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_resume);
1060e5cc8ef3SRafael J. Wysocki 
1061e5cc8ef3SRafael J. Wysocki /**
1062e5cc8ef3SRafael J. Wysocki  * acpi_subsys_runtime_suspend - Suspend device using ACPI.
1063e5cc8ef3SRafael J. Wysocki  * @dev: Device to suspend.
1064e5cc8ef3SRafael J. Wysocki  *
1065e5cc8ef3SRafael J. Wysocki  * Carry out the generic runtime suspend procedure for @dev and use ACPI to put
1066e5cc8ef3SRafael J. Wysocki  * it into a runtime low-power state.
1067e5cc8ef3SRafael J. Wysocki  */
acpi_subsys_runtime_suspend(struct device * dev)1068e5cc8ef3SRafael J. Wysocki int acpi_subsys_runtime_suspend(struct device *dev)
1069e5cc8ef3SRafael J. Wysocki {
1070e5cc8ef3SRafael J. Wysocki 	int ret = pm_generic_runtime_suspend(dev);
10713da8236bSXiaofei Tan 
1072cbe25ce3SRafael J. Wysocki 	return ret ? ret : acpi_dev_suspend(dev, true);
1073e5cc8ef3SRafael J. Wysocki }
1074e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend);
1075e5cc8ef3SRafael J. Wysocki 
1076e5cc8ef3SRafael J. Wysocki /**
1077e5cc8ef3SRafael J. Wysocki  * acpi_subsys_runtime_resume - Resume device using ACPI.
1078e5cc8ef3SRafael J. Wysocki  * @dev: Device to Resume.
1079e5cc8ef3SRafael J. Wysocki  *
1080e5cc8ef3SRafael J. Wysocki  * Use ACPI to put the given device into the full-power state and carry out the
1081e5cc8ef3SRafael J. Wysocki  * generic runtime resume procedure for it.
1082e5cc8ef3SRafael J. Wysocki  */
acpi_subsys_runtime_resume(struct device * dev)1083e5cc8ef3SRafael J. Wysocki int acpi_subsys_runtime_resume(struct device *dev)
1084e5cc8ef3SRafael J. Wysocki {
108563705c40SRafael J. Wysocki 	int ret = acpi_dev_resume(dev);
10863da8236bSXiaofei Tan 
1087e5cc8ef3SRafael J. Wysocki 	return ret ? ret : pm_generic_runtime_resume(dev);
1088e5cc8ef3SRafael J. Wysocki }
1089e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume);
1090e5cc8ef3SRafael J. Wysocki 
1091e5cc8ef3SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP
acpi_dev_needs_resume(struct device * dev,struct acpi_device * adev)1092c2ebf788SUlf Hansson static bool acpi_dev_needs_resume(struct device *dev, struct acpi_device *adev)
1093c2ebf788SUlf Hansson {
1094c2ebf788SUlf Hansson 	u32 sys_target = acpi_target_system_state();
1095c2ebf788SUlf Hansson 	int ret, state;
1096c2ebf788SUlf Hansson 
10979a51c6b1SRafael J. Wysocki 	if (!pm_runtime_suspended(dev) || !adev || (adev->wakeup.flags.valid &&
10989a51c6b1SRafael J. Wysocki 	    device_may_wakeup(dev) != !!adev->wakeup.prepare_count))
1099c2ebf788SUlf Hansson 		return true;
1100c2ebf788SUlf Hansson 
1101c2ebf788SUlf Hansson 	if (sys_target == ACPI_STATE_S0)
1102c2ebf788SUlf Hansson 		return false;
1103c2ebf788SUlf Hansson 
1104c2ebf788SUlf Hansson 	if (adev->power.flags.dsw_present)
1105c2ebf788SUlf Hansson 		return true;
1106c2ebf788SUlf Hansson 
1107c2ebf788SUlf Hansson 	ret = acpi_dev_pm_get_state(dev, adev, sys_target, NULL, &state);
1108c2ebf788SUlf Hansson 	if (ret)
1109c2ebf788SUlf Hansson 		return true;
1110c2ebf788SUlf Hansson 
1111c2ebf788SUlf Hansson 	return state != adev->power.state;
1112c2ebf788SUlf Hansson }
1113c2ebf788SUlf Hansson 
1114e5cc8ef3SRafael J. Wysocki /**
1115e5cc8ef3SRafael J. Wysocki  * acpi_subsys_prepare - Prepare device for system transition to a sleep state.
1116e5cc8ef3SRafael J. Wysocki  * @dev: Device to prepare.
1117e5cc8ef3SRafael J. Wysocki  */
acpi_subsys_prepare(struct device * dev)1118e5cc8ef3SRafael J. Wysocki int acpi_subsys_prepare(struct device *dev)
1119e5cc8ef3SRafael J. Wysocki {
1120f25c0ae2SRafael J. Wysocki 	struct acpi_device *adev = ACPI_COMPANION(dev);
112192858c47SRafael J. Wysocki 
112208810a41SRafael J. Wysocki 	if (dev->driver && dev->driver->pm && dev->driver->pm->prepare) {
112308810a41SRafael J. Wysocki 		int ret = dev->driver->pm->prepare(dev);
112408810a41SRafael J. Wysocki 
1125f25c0ae2SRafael J. Wysocki 		if (ret < 0)
1126f25c0ae2SRafael J. Wysocki 			return ret;
1127f25c0ae2SRafael J. Wysocki 
112808810a41SRafael J. Wysocki 		if (!ret && dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_PREPARE))
1129f25c0ae2SRafael J. Wysocki 			return 0;
113008810a41SRafael J. Wysocki 	}
1131f25c0ae2SRafael J. Wysocki 
1132c2ebf788SUlf Hansson 	return !acpi_dev_needs_resume(dev, adev);
1133e5cc8ef3SRafael J. Wysocki }
1134e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_prepare);
1135e5cc8ef3SRafael J. Wysocki 
1136e5cc8ef3SRafael J. Wysocki /**
1137e4da817dSUlf Hansson  * acpi_subsys_complete - Finalize device's resume during system resume.
1138e4da817dSUlf Hansson  * @dev: Device to handle.
1139e4da817dSUlf Hansson  */
acpi_subsys_complete(struct device * dev)1140e4da817dSUlf Hansson void acpi_subsys_complete(struct device *dev)
1141e4da817dSUlf Hansson {
1142e4da817dSUlf Hansson 	pm_generic_complete(dev);
1143e4da817dSUlf Hansson 	/*
1144e4da817dSUlf Hansson 	 * If the device had been runtime-suspended before the system went into
1145e4da817dSUlf Hansson 	 * the sleep state it is going out of and it has never been resumed till
1146e4da817dSUlf Hansson 	 * now, resume it in case the firmware powered it up.
1147e4da817dSUlf Hansson 	 */
1148db68daffSRafael J. Wysocki 	if (pm_runtime_suspended(dev) && pm_resume_via_firmware())
1149e4da817dSUlf Hansson 		pm_request_resume(dev);
1150e4da817dSUlf Hansson }
1151e4da817dSUlf Hansson EXPORT_SYMBOL_GPL(acpi_subsys_complete);
1152e4da817dSUlf Hansson 
1153e4da817dSUlf Hansson /**
115492858c47SRafael J. Wysocki  * acpi_subsys_suspend - Run the device driver's suspend callback.
115592858c47SRafael J. Wysocki  * @dev: Device to handle.
115692858c47SRafael J. Wysocki  *
115705087360SRafael J. Wysocki  * Follow PCI and resume devices from runtime suspend before running their
115805087360SRafael J. Wysocki  * system suspend callbacks, unless the driver can cope with runtime-suspended
115905087360SRafael J. Wysocki  * devices during system suspend and there are no ACPI-specific reasons for
116005087360SRafael J. Wysocki  * resuming them.
116192858c47SRafael J. Wysocki  */
acpi_subsys_suspend(struct device * dev)116292858c47SRafael J. Wysocki int acpi_subsys_suspend(struct device *dev)
116392858c47SRafael J. Wysocki {
116405087360SRafael J. Wysocki 	if (!dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND) ||
116505087360SRafael J. Wysocki 	    acpi_dev_needs_resume(dev, ACPI_COMPANION(dev)))
116692858c47SRafael J. Wysocki 		pm_runtime_resume(dev);
116705087360SRafael J. Wysocki 
116892858c47SRafael J. Wysocki 	return pm_generic_suspend(dev);
116992858c47SRafael J. Wysocki }
11704cf563c5SHeikki Krogerus EXPORT_SYMBOL_GPL(acpi_subsys_suspend);
117192858c47SRafael J. Wysocki 
117292858c47SRafael J. Wysocki /**
1173e5cc8ef3SRafael J. Wysocki  * acpi_subsys_suspend_late - Suspend device using ACPI.
1174e5cc8ef3SRafael J. Wysocki  * @dev: Device to suspend.
1175e5cc8ef3SRafael J. Wysocki  *
1176e5cc8ef3SRafael J. Wysocki  * Carry out the generic late suspend procedure for @dev and use ACPI to put
1177e5cc8ef3SRafael J. Wysocki  * it into a low-power state during system transition into a sleep state.
1178e5cc8ef3SRafael J. Wysocki  */
acpi_subsys_suspend_late(struct device * dev)1179e5cc8ef3SRafael J. Wysocki int acpi_subsys_suspend_late(struct device *dev)
1180e5cc8ef3SRafael J. Wysocki {
118105087360SRafael J. Wysocki 	int ret;
118205087360SRafael J. Wysocki 
1183fa2bfeadSRafael J. Wysocki 	if (dev_pm_skip_suspend(dev))
118405087360SRafael J. Wysocki 		return 0;
118505087360SRafael J. Wysocki 
118605087360SRafael J. Wysocki 	ret = pm_generic_suspend_late(dev);
1187cbe25ce3SRafael J. Wysocki 	return ret ? ret : acpi_dev_suspend(dev, device_may_wakeup(dev));
1188e5cc8ef3SRafael J. Wysocki }
1189e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late);
1190e5cc8ef3SRafael J. Wysocki 
1191e5cc8ef3SRafael J. Wysocki /**
119205087360SRafael J. Wysocki  * acpi_subsys_suspend_noirq - Run the device driver's "noirq" suspend callback.
119305087360SRafael J. Wysocki  * @dev: Device to suspend.
119405087360SRafael J. Wysocki  */
acpi_subsys_suspend_noirq(struct device * dev)119505087360SRafael J. Wysocki int acpi_subsys_suspend_noirq(struct device *dev)
119605087360SRafael J. Wysocki {
1197db68daffSRafael J. Wysocki 	int ret;
119805087360SRafael J. Wysocki 
1199fa2bfeadSRafael J. Wysocki 	if (dev_pm_skip_suspend(dev))
1200db68daffSRafael J. Wysocki 		return 0;
1201db68daffSRafael J. Wysocki 
1202db68daffSRafael J. Wysocki 	ret = pm_generic_suspend_noirq(dev);
1203db68daffSRafael J. Wysocki 	if (ret)
1204db68daffSRafael J. Wysocki 		return ret;
1205db68daffSRafael J. Wysocki 
1206db68daffSRafael J. Wysocki 	/*
1207db68daffSRafael J. Wysocki 	 * If the target system sleep state is suspend-to-idle, it is sufficient
1208db68daffSRafael J. Wysocki 	 * to check whether or not the device's wakeup settings are good for
1209db68daffSRafael J. Wysocki 	 * runtime PM.  Otherwise, the pm_resume_via_firmware() check will cause
1210db68daffSRafael J. Wysocki 	 * acpi_subsys_complete() to take care of fixing up the device's state
1211db68daffSRafael J. Wysocki 	 * anyway, if need be.
1212db68daffSRafael J. Wysocki 	 */
12130fe8a1beSRafael J. Wysocki 	if (device_can_wakeup(dev) && !device_may_wakeup(dev))
12140fe8a1beSRafael J. Wysocki 		dev->power.may_skip_resume = false;
1215db68daffSRafael J. Wysocki 
1216db68daffSRafael J. Wysocki 	return 0;
121705087360SRafael J. Wysocki }
121805087360SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_suspend_noirq);
121905087360SRafael J. Wysocki 
122005087360SRafael J. Wysocki /**
122105087360SRafael J. Wysocki  * acpi_subsys_resume_noirq - Run the device driver's "noirq" resume callback.
122205087360SRafael J. Wysocki  * @dev: Device to handle.
122305087360SRafael J. Wysocki  */
acpi_subsys_resume_noirq(struct device * dev)12243cd7957eSRafael J. Wysocki static int acpi_subsys_resume_noirq(struct device *dev)
122505087360SRafael J. Wysocki {
122676c70cb5SRafael J. Wysocki 	if (dev_pm_skip_resume(dev))
1227db68daffSRafael J. Wysocki 		return 0;
1228db68daffSRafael J. Wysocki 
122905087360SRafael J. Wysocki 	return pm_generic_resume_noirq(dev);
123005087360SRafael J. Wysocki }
123105087360SRafael J. Wysocki 
123205087360SRafael J. Wysocki /**
1233e5cc8ef3SRafael J. Wysocki  * acpi_subsys_resume_early - Resume device using ACPI.
1234e5cc8ef3SRafael J. Wysocki  * @dev: Device to Resume.
1235e5cc8ef3SRafael J. Wysocki  *
1236e5cc8ef3SRafael J. Wysocki  * Use ACPI to put the given device into the full-power state and carry out the
1237e5cc8ef3SRafael J. Wysocki  * generic early resume procedure for it during system transition into the
1238f7599be2SDmitry Torokhov  * working state, but only do that if device either defines early resume
1239f7599be2SDmitry Torokhov  * handler, or does not define power operations at all. Otherwise powering up
1240f7599be2SDmitry Torokhov  * of the device is postponed to the normal resume phase.
1241e5cc8ef3SRafael J. Wysocki  */
acpi_subsys_resume_early(struct device * dev)12423cd7957eSRafael J. Wysocki static int acpi_subsys_resume_early(struct device *dev)
1243e5cc8ef3SRafael J. Wysocki {
1244f7599be2SDmitry Torokhov 	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
12456e176bf8SRafael J. Wysocki 	int ret;
12466e176bf8SRafael J. Wysocki 
124776c70cb5SRafael J. Wysocki 	if (dev_pm_skip_resume(dev))
12486e176bf8SRafael J. Wysocki 		return 0;
12496e176bf8SRafael J. Wysocki 
1250f7599be2SDmitry Torokhov 	if (pm && !pm->resume_early) {
1251f7599be2SDmitry Torokhov 		dev_dbg(dev, "postponing D0 transition to normal resume stage\n");
1252f7599be2SDmitry Torokhov 		return 0;
1253f7599be2SDmitry Torokhov 	}
1254f7599be2SDmitry Torokhov 
12556e176bf8SRafael J. Wysocki 	ret = acpi_dev_resume(dev);
1256e5cc8ef3SRafael J. Wysocki 	return ret ? ret : pm_generic_resume_early(dev);
1257e5cc8ef3SRafael J. Wysocki }
125892858c47SRafael J. Wysocki 
125992858c47SRafael J. Wysocki /**
1260f7599be2SDmitry Torokhov  * acpi_subsys_resume - Resume device using ACPI.
1261f7599be2SDmitry Torokhov  * @dev: Device to Resume.
1262f7599be2SDmitry Torokhov  *
1263f7599be2SDmitry Torokhov  * Use ACPI to put the given device into the full-power state if it has not been
1264f7599be2SDmitry Torokhov  * powered up during early resume phase, and carry out the generic resume
1265f7599be2SDmitry Torokhov  * procedure for it during system transition into the working state.
1266f7599be2SDmitry Torokhov  */
acpi_subsys_resume(struct device * dev)1267f7599be2SDmitry Torokhov static int acpi_subsys_resume(struct device *dev)
1268f7599be2SDmitry Torokhov {
1269f7599be2SDmitry Torokhov 	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1270f7599be2SDmitry Torokhov 	int ret = 0;
1271f7599be2SDmitry Torokhov 
1272f7599be2SDmitry Torokhov 	if (!dev_pm_skip_resume(dev) && pm && !pm->resume_early) {
1273f7599be2SDmitry Torokhov 		dev_dbg(dev, "executing postponed D0 transition\n");
1274f7599be2SDmitry Torokhov 		ret = acpi_dev_resume(dev);
1275f7599be2SDmitry Torokhov 	}
1276f7599be2SDmitry Torokhov 
1277f7599be2SDmitry Torokhov 	return ret ? ret : pm_generic_resume(dev);
1278f7599be2SDmitry Torokhov }
1279f7599be2SDmitry Torokhov 
1280f7599be2SDmitry Torokhov /**
128192858c47SRafael J. Wysocki  * acpi_subsys_freeze - Run the device driver's freeze callback.
128292858c47SRafael J. Wysocki  * @dev: Device to handle.
128392858c47SRafael J. Wysocki  */
acpi_subsys_freeze(struct device * dev)128492858c47SRafael J. Wysocki int acpi_subsys_freeze(struct device *dev)
128592858c47SRafael J. Wysocki {
128692858c47SRafael J. Wysocki 	/*
1287501debd4SRafael J. Wysocki 	 * Resume all runtime-suspended devices before creating a snapshot
1288501debd4SRafael J. Wysocki 	 * image of system memory, because the restore kernel generally cannot
1289501debd4SRafael J. Wysocki 	 * be expected to always handle them consistently and they need to be
1290501debd4SRafael J. Wysocki 	 * put into the runtime-active metastate during system resume anyway,
1291501debd4SRafael J. Wysocki 	 * so it is better to ensure that the state saved in the image will be
1292501debd4SRafael J. Wysocki 	 * always consistent with that.
129392858c47SRafael J. Wysocki 	 */
129492858c47SRafael J. Wysocki 	pm_runtime_resume(dev);
129505087360SRafael J. Wysocki 
129692858c47SRafael J. Wysocki 	return pm_generic_freeze(dev);
129792858c47SRafael J. Wysocki }
12984cf563c5SHeikki Krogerus EXPORT_SYMBOL_GPL(acpi_subsys_freeze);
129992858c47SRafael J. Wysocki 
130005087360SRafael J. Wysocki /**
13013cd7957eSRafael J. Wysocki  * acpi_subsys_restore_early - Restore device using ACPI.
13023cd7957eSRafael J. Wysocki  * @dev: Device to restore.
130305087360SRafael J. Wysocki  */
acpi_subsys_restore_early(struct device * dev)13043cd7957eSRafael J. Wysocki int acpi_subsys_restore_early(struct device *dev)
130505087360SRafael J. Wysocki {
13063cd7957eSRafael J. Wysocki 	int ret = acpi_dev_resume(dev);
13073da8236bSXiaofei Tan 
13083cd7957eSRafael J. Wysocki 	return ret ? ret : pm_generic_restore_early(dev);
130905087360SRafael J. Wysocki }
13103cd7957eSRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_restore_early);
1311c95b7595SRafael J. Wysocki 
1312c95b7595SRafael J. Wysocki /**
1313c95b7595SRafael J. Wysocki  * acpi_subsys_poweroff - Run the device driver's poweroff callback.
1314c95b7595SRafael J. Wysocki  * @dev: Device to handle.
1315c95b7595SRafael J. Wysocki  *
1316c95b7595SRafael J. Wysocki  * Follow PCI and resume devices from runtime suspend before running their
1317c95b7595SRafael J. Wysocki  * system poweroff callbacks, unless the driver can cope with runtime-suspended
1318c95b7595SRafael J. Wysocki  * devices during system suspend and there are no ACPI-specific reasons for
1319c95b7595SRafael J. Wysocki  * resuming them.
1320c95b7595SRafael J. Wysocki  */
acpi_subsys_poweroff(struct device * dev)1321c95b7595SRafael J. Wysocki int acpi_subsys_poweroff(struct device *dev)
1322c95b7595SRafael J. Wysocki {
1323c95b7595SRafael J. Wysocki 	if (!dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND) ||
1324c95b7595SRafael J. Wysocki 	    acpi_dev_needs_resume(dev, ACPI_COMPANION(dev)))
1325c95b7595SRafael J. Wysocki 		pm_runtime_resume(dev);
1326c95b7595SRafael J. Wysocki 
1327c95b7595SRafael J. Wysocki 	return pm_generic_poweroff(dev);
1328c95b7595SRafael J. Wysocki }
1329c95b7595SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_subsys_poweroff);
1330c95b7595SRafael J. Wysocki 
1331c95b7595SRafael J. Wysocki /**
1332c95b7595SRafael J. Wysocki  * acpi_subsys_poweroff_late - Run the device driver's poweroff callback.
1333c95b7595SRafael J. Wysocki  * @dev: Device to handle.
1334c95b7595SRafael J. Wysocki  *
1335c95b7595SRafael J. Wysocki  * Carry out the generic late poweroff procedure for @dev and use ACPI to put
1336c95b7595SRafael J. Wysocki  * it into a low-power state during system transition into a sleep state.
1337c95b7595SRafael J. Wysocki  */
acpi_subsys_poweroff_late(struct device * dev)1338c95b7595SRafael J. Wysocki static int acpi_subsys_poweroff_late(struct device *dev)
1339c95b7595SRafael J. Wysocki {
1340c95b7595SRafael J. Wysocki 	int ret;
134105087360SRafael J. Wysocki 
1342fa2bfeadSRafael J. Wysocki 	if (dev_pm_skip_suspend(dev))
134305087360SRafael J. Wysocki 		return 0;
134405087360SRafael J. Wysocki 
1345c95b7595SRafael J. Wysocki 	ret = pm_generic_poweroff_late(dev);
1346c95b7595SRafael J. Wysocki 	if (ret)
1347c95b7595SRafael J. Wysocki 		return ret;
1348c95b7595SRafael J. Wysocki 
1349c95b7595SRafael J. Wysocki 	return acpi_dev_suspend(dev, device_may_wakeup(dev));
135005087360SRafael J. Wysocki }
135105087360SRafael J. Wysocki 
135205087360SRafael J. Wysocki /**
1353c95b7595SRafael J. Wysocki  * acpi_subsys_poweroff_noirq - Run the driver's "noirq" poweroff callback.
1354c95b7595SRafael J. Wysocki  * @dev: Device to suspend.
135505087360SRafael J. Wysocki  */
acpi_subsys_poweroff_noirq(struct device * dev)1356c95b7595SRafael J. Wysocki static int acpi_subsys_poweroff_noirq(struct device *dev)
135705087360SRafael J. Wysocki {
1358fa2bfeadSRafael J. Wysocki 	if (dev_pm_skip_suspend(dev))
135905087360SRafael J. Wysocki 		return 0;
136005087360SRafael J. Wysocki 
1361c95b7595SRafael J. Wysocki 	return pm_generic_poweroff_noirq(dev);
136205087360SRafael J. Wysocki }
1363e5cc8ef3SRafael J. Wysocki #endif /* CONFIG_PM_SLEEP */
1364e5cc8ef3SRafael J. Wysocki 
1365e5cc8ef3SRafael J. Wysocki static struct dev_pm_domain acpi_general_pm_domain = {
1366e5cc8ef3SRafael J. Wysocki 	.ops = {
1367e5cc8ef3SRafael J. Wysocki 		.runtime_suspend = acpi_subsys_runtime_suspend,
1368e5cc8ef3SRafael J. Wysocki 		.runtime_resume = acpi_subsys_runtime_resume,
1369e5cc8ef3SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP
1370e5cc8ef3SRafael J. Wysocki 		.prepare = acpi_subsys_prepare,
1371e4da817dSUlf Hansson 		.complete = acpi_subsys_complete,
137292858c47SRafael J. Wysocki 		.suspend = acpi_subsys_suspend,
1373f7599be2SDmitry Torokhov 		.resume = acpi_subsys_resume,
1374e5cc8ef3SRafael J. Wysocki 		.suspend_late = acpi_subsys_suspend_late,
137505087360SRafael J. Wysocki 		.suspend_noirq = acpi_subsys_suspend_noirq,
137605087360SRafael J. Wysocki 		.resume_noirq = acpi_subsys_resume_noirq,
1377e5cc8ef3SRafael J. Wysocki 		.resume_early = acpi_subsys_resume_early,
137892858c47SRafael J. Wysocki 		.freeze = acpi_subsys_freeze,
1379c95b7595SRafael J. Wysocki 		.poweroff = acpi_subsys_poweroff,
1380c95b7595SRafael J. Wysocki 		.poweroff_late = acpi_subsys_poweroff_late,
1381c95b7595SRafael J. Wysocki 		.poweroff_noirq = acpi_subsys_poweroff_noirq,
13823cd7957eSRafael J. Wysocki 		.restore_early = acpi_subsys_restore_early,
1383e5cc8ef3SRafael J. Wysocki #endif
1384e5cc8ef3SRafael J. Wysocki 	},
1385e5cc8ef3SRafael J. Wysocki };
1386e5cc8ef3SRafael J. Wysocki 
1387e5cc8ef3SRafael J. Wysocki /**
138891d66cd2SUlf Hansson  * acpi_dev_pm_detach - Remove ACPI power management from the device.
138991d66cd2SUlf Hansson  * @dev: Device to take care of.
139091d66cd2SUlf Hansson  * @power_off: Whether or not to try to remove power from the device.
139191d66cd2SUlf Hansson  *
139291d66cd2SUlf Hansson  * Remove the device from the general ACPI PM domain and remove its wakeup
139391d66cd2SUlf Hansson  * notifier.  If @power_off is set, additionally remove power from the device if
139491d66cd2SUlf Hansson  * possible.
139591d66cd2SUlf Hansson  *
139691d66cd2SUlf Hansson  * Callers must ensure proper synchronization of this function with power
139791d66cd2SUlf Hansson  * management callbacks.
139891d66cd2SUlf Hansson  */
acpi_dev_pm_detach(struct device * dev,bool power_off)139991d66cd2SUlf Hansson static void acpi_dev_pm_detach(struct device *dev, bool power_off)
140091d66cd2SUlf Hansson {
140191d66cd2SUlf Hansson 	struct acpi_device *adev = ACPI_COMPANION(dev);
140291d66cd2SUlf Hansson 
140391d66cd2SUlf Hansson 	if (adev && dev->pm_domain == &acpi_general_pm_domain) {
1404989561deSTomeu Vizoso 		dev_pm_domain_set(dev, NULL);
140591d66cd2SUlf Hansson 		acpi_remove_pm_notifier(adev);
140691d66cd2SUlf Hansson 		if (power_off) {
140791d66cd2SUlf Hansson 			/*
140891d66cd2SUlf Hansson 			 * If the device's PM QoS resume latency limit or flags
140991d66cd2SUlf Hansson 			 * have been exposed to user space, they have to be
141091d66cd2SUlf Hansson 			 * hidden at this point, so that they don't affect the
141191d66cd2SUlf Hansson 			 * choice of the low-power state to put the device into.
141291d66cd2SUlf Hansson 			 */
141391d66cd2SUlf Hansson 			dev_pm_qos_hide_latency_limit(dev);
141491d66cd2SUlf Hansson 			dev_pm_qos_hide_flags(dev);
141599d8845eSRafael J. Wysocki 			acpi_device_wakeup_disable(adev);
141691d66cd2SUlf Hansson 			acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
141791d66cd2SUlf Hansson 		}
141891d66cd2SUlf Hansson 	}
141991d66cd2SUlf Hansson }
142091d66cd2SUlf Hansson 
142191d66cd2SUlf Hansson /**
1422e5cc8ef3SRafael J. Wysocki  * acpi_dev_pm_attach - Prepare device for ACPI power management.
1423e5cc8ef3SRafael J. Wysocki  * @dev: Device to prepare.
1424b88ce2a4SRafael J. Wysocki  * @power_on: Whether or not to power on the device.
1425e5cc8ef3SRafael J. Wysocki  *
1426e5cc8ef3SRafael J. Wysocki  * If @dev has a valid ACPI handle that has a valid struct acpi_device object
1427e5cc8ef3SRafael J. Wysocki  * attached to it, install a wakeup notification handler for the device and
1428b88ce2a4SRafael J. Wysocki  * add it to the general ACPI PM domain.  If @power_on is set, the device will
1429b88ce2a4SRafael J. Wysocki  * be put into the ACPI D0 state before the function returns.
1430e5cc8ef3SRafael J. Wysocki  *
1431e5cc8ef3SRafael J. Wysocki  * This assumes that the @dev's bus type uses generic power management callbacks
1432e5cc8ef3SRafael J. Wysocki  * (or doesn't use any power management callbacks at all).
1433e5cc8ef3SRafael J. Wysocki  *
1434e5cc8ef3SRafael J. Wysocki  * Callers must ensure proper synchronization of this function with power
1435e5cc8ef3SRafael J. Wysocki  * management callbacks.
1436e5cc8ef3SRafael J. Wysocki  */
acpi_dev_pm_attach(struct device * dev,bool power_on)1437b88ce2a4SRafael J. Wysocki int acpi_dev_pm_attach(struct device *dev, bool power_on)
1438e5cc8ef3SRafael J. Wysocki {
1439b9ea0baeSRafael J. Wysocki 	/*
1440b9ea0baeSRafael J. Wysocki 	 * Skip devices whose ACPI companions match the device IDs below,
1441b9ea0baeSRafael J. Wysocki 	 * because they require special power management handling incompatible
1442b9ea0baeSRafael J. Wysocki 	 * with the generic ACPI PM domain.
1443b9ea0baeSRafael J. Wysocki 	 */
1444b9ea0baeSRafael J. Wysocki 	static const struct acpi_device_id special_pm_ids[] = {
1445b9370dceSRafael J. Wysocki 		ACPI_FAN_DEVICE_IDS,
1446b9ea0baeSRafael J. Wysocki 		{}
1447b9ea0baeSRafael J. Wysocki 	};
144879c0373fSRafael J. Wysocki 	struct acpi_device *adev = ACPI_COMPANION(dev);
1449e5cc8ef3SRafael J. Wysocki 
1450b9ea0baeSRafael J. Wysocki 	if (!adev || !acpi_match_device_ids(adev, special_pm_ids))
1451919b7308SUlf Hansson 		return 0;
1452e5cc8ef3SRafael J. Wysocki 
1453712e960fSMika Westerberg 	/*
1454712e960fSMika Westerberg 	 * Only attach the power domain to the first device if the
1455712e960fSMika Westerberg 	 * companion is shared by multiple. This is to prevent doing power
1456712e960fSMika Westerberg 	 * management twice.
1457712e960fSMika Westerberg 	 */
1458712e960fSMika Westerberg 	if (!acpi_device_is_first_physical_node(adev, dev))
1459919b7308SUlf Hansson 		return 0;
1460712e960fSMika Westerberg 
1461c072530fSRafael J. Wysocki 	acpi_add_pm_notifier(adev, dev, acpi_pm_notify_work_func);
1462989561deSTomeu Vizoso 	dev_pm_domain_set(dev, &acpi_general_pm_domain);
1463b88ce2a4SRafael J. Wysocki 	if (power_on) {
1464b88ce2a4SRafael J. Wysocki 		acpi_dev_pm_full_power(adev);
146599d8845eSRafael J. Wysocki 		acpi_device_wakeup_disable(adev);
1466b88ce2a4SRafael J. Wysocki 	}
146786f1e15fSUlf Hansson 
146886f1e15fSUlf Hansson 	dev->pm_domain->detach = acpi_dev_pm_detach;
1469919b7308SUlf Hansson 	return 1;
1470e5cc8ef3SRafael J. Wysocki }
1471e5cc8ef3SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_dev_pm_attach);
14722744d7a0SMario Limonciello 
14732744d7a0SMario Limonciello /**
14742744d7a0SMario Limonciello  * acpi_storage_d3 - Check if D3 should be used in the suspend path
14752744d7a0SMario Limonciello  * @dev: Device to check
14762744d7a0SMario Limonciello  *
14772744d7a0SMario Limonciello  * Return %true if the platform firmware wants @dev to be programmed
14782744d7a0SMario Limonciello  * into D3hot or D3cold (if supported) in the suspend path, or %false
14792744d7a0SMario Limonciello  * when there is no specific preference. On some platforms, if this
14802744d7a0SMario Limonciello  * hint is ignored, @dev may remain unresponsive after suspending the
14812744d7a0SMario Limonciello  * platform as a whole.
14822744d7a0SMario Limonciello  *
14832744d7a0SMario Limonciello  * Although the property has storage in the name it actually is
14842744d7a0SMario Limonciello  * applied to the PCIe slot and plugging in a non-storage device the
14852744d7a0SMario Limonciello  * same platform restrictions will likely apply.
14862744d7a0SMario Limonciello  */
acpi_storage_d3(struct device * dev)14872744d7a0SMario Limonciello bool acpi_storage_d3(struct device *dev)
14882744d7a0SMario Limonciello {
14892744d7a0SMario Limonciello 	struct acpi_device *adev = ACPI_COMPANION(dev);
14902744d7a0SMario Limonciello 	u8 val;
14912744d7a0SMario Limonciello 
14926485fc18SMario Limonciello 	if (force_storage_d3())
14936485fc18SMario Limonciello 		return true;
14946485fc18SMario Limonciello 
14952744d7a0SMario Limonciello 	if (!adev)
14962744d7a0SMario Limonciello 		return false;
14972744d7a0SMario Limonciello 	if (fwnode_property_read_u8(acpi_fwnode_handle(adev), "StorageD3Enable",
14982744d7a0SMario Limonciello 			&val))
14992744d7a0SMario Limonciello 		return false;
15002744d7a0SMario Limonciello 	return val == 1;
15012744d7a0SMario Limonciello }
15022744d7a0SMario Limonciello EXPORT_SYMBOL_GPL(acpi_storage_d3);
15032744d7a0SMario Limonciello 
1504b82a7df4SSakari Ailus /**
1505b82a7df4SSakari Ailus  * acpi_dev_state_d0 - Tell if the device is in D0 power state
1506b82a7df4SSakari Ailus  * @dev: Physical device the ACPI power state of which to check
1507b82a7df4SSakari Ailus  *
1508b82a7df4SSakari Ailus  * On a system without ACPI, return true. On a system with ACPI, return true if
1509b82a7df4SSakari Ailus  * the current ACPI power state of the device is D0, or false otherwise.
1510b82a7df4SSakari Ailus  *
1511b82a7df4SSakari Ailus  * Note that the power state of a device is not well-defined after it has been
1512b82a7df4SSakari Ailus  * passed to acpi_device_set_power() and before that function returns, so it is
1513b82a7df4SSakari Ailus  * not valid to ask for the ACPI power state of the device in that time frame.
1514b82a7df4SSakari Ailus  *
1515b82a7df4SSakari Ailus  * This function is intended to be used in a driver's probe or remove
15160efe92b4SSakari Ailus  * function. See Documentation/firmware-guide/acpi/non-d0-probe.rst for
1517b82a7df4SSakari Ailus  * more information.
1518b82a7df4SSakari Ailus  */
acpi_dev_state_d0(struct device * dev)1519b82a7df4SSakari Ailus bool acpi_dev_state_d0(struct device *dev)
1520b82a7df4SSakari Ailus {
1521b82a7df4SSakari Ailus 	struct acpi_device *adev = ACPI_COMPANION(dev);
1522b82a7df4SSakari Ailus 
1523b82a7df4SSakari Ailus 	if (!adev)
1524b82a7df4SSakari Ailus 		return true;
1525b82a7df4SSakari Ailus 
1526b82a7df4SSakari Ailus 	return adev->power.state == ACPI_STATE_D0;
1527b82a7df4SSakari Ailus }
1528b82a7df4SSakari Ailus EXPORT_SYMBOL_GPL(acpi_dev_state_d0);
1529b82a7df4SSakari Ailus 
1530ec4602a9SRafael J. Wysocki #endif /* CONFIG_PM */
1531