1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * drivers/acpi/device_pm.c - ACPI device power management routines.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2012, Intel Corp.
6*4882a593Smuzhiyun * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11*4882a593Smuzhiyun */
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #include <linux/acpi.h>
14*4882a593Smuzhiyun #include <linux/export.h>
15*4882a593Smuzhiyun #include <linux/mutex.h>
16*4882a593Smuzhiyun #include <linux/pm_qos.h>
17*4882a593Smuzhiyun #include <linux/pm_domain.h>
18*4882a593Smuzhiyun #include <linux/pm_runtime.h>
19*4882a593Smuzhiyun #include <linux/suspend.h>
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun #include "fan.h"
22*4882a593Smuzhiyun #include "internal.h"
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun #define _COMPONENT ACPI_POWER_COMPONENT
25*4882a593Smuzhiyun ACPI_MODULE_NAME("device_pm");
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun /**
28*4882a593Smuzhiyun * acpi_power_state_string - String representation of ACPI device power state.
29*4882a593Smuzhiyun * @state: ACPI device power state to return the string representation of.
30*4882a593Smuzhiyun */
acpi_power_state_string(int state)31*4882a593Smuzhiyun const char *acpi_power_state_string(int state)
32*4882a593Smuzhiyun {
33*4882a593Smuzhiyun switch (state) {
34*4882a593Smuzhiyun case ACPI_STATE_D0:
35*4882a593Smuzhiyun return "D0";
36*4882a593Smuzhiyun case ACPI_STATE_D1:
37*4882a593Smuzhiyun return "D1";
38*4882a593Smuzhiyun case ACPI_STATE_D2:
39*4882a593Smuzhiyun return "D2";
40*4882a593Smuzhiyun case ACPI_STATE_D3_HOT:
41*4882a593Smuzhiyun return "D3hot";
42*4882a593Smuzhiyun case ACPI_STATE_D3_COLD:
43*4882a593Smuzhiyun return "D3cold";
44*4882a593Smuzhiyun default:
45*4882a593Smuzhiyun return "(unknown)";
46*4882a593Smuzhiyun }
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun
acpi_dev_pm_explicit_get(struct acpi_device * device,int * state)49*4882a593Smuzhiyun static int acpi_dev_pm_explicit_get(struct acpi_device *device, int *state)
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun unsigned long long psc;
52*4882a593Smuzhiyun acpi_status status;
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun status = acpi_evaluate_integer(device->handle, "_PSC", NULL, &psc);
55*4882a593Smuzhiyun if (ACPI_FAILURE(status))
56*4882a593Smuzhiyun return -ENODEV;
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun *state = psc;
59*4882a593Smuzhiyun return 0;
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun /**
63*4882a593Smuzhiyun * acpi_device_get_power - Get power state of an ACPI device.
64*4882a593Smuzhiyun * @device: Device to get the power state of.
65*4882a593Smuzhiyun * @state: Place to store the power state of the device.
66*4882a593Smuzhiyun *
67*4882a593Smuzhiyun * This function does not update the device's power.state field, but it may
68*4882a593Smuzhiyun * update its parent's power.state field (when the parent's power state is
69*4882a593Smuzhiyun * unknown and the device's power state turns out to be D0).
70*4882a593Smuzhiyun *
71*4882a593Smuzhiyun * Also, it does not update power resource reference counters to ensure that
72*4882a593Smuzhiyun * the power state returned by it will be persistent and it may return a power
73*4882a593Smuzhiyun * state shallower than previously set by acpi_device_set_power() for @device
74*4882a593Smuzhiyun * (if that power state depends on any power resources).
75*4882a593Smuzhiyun */
acpi_device_get_power(struct acpi_device * device,int * state)76*4882a593Smuzhiyun int acpi_device_get_power(struct acpi_device *device, int *state)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun int result = ACPI_STATE_UNKNOWN;
79*4882a593Smuzhiyun int error;
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun if (!device || !state)
82*4882a593Smuzhiyun return -EINVAL;
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun if (!device->flags.power_manageable) {
85*4882a593Smuzhiyun /* TBD: Non-recursive algorithm for walking up hierarchy. */
86*4882a593Smuzhiyun *state = device->parent ?
87*4882a593Smuzhiyun device->parent->power.state : ACPI_STATE_D0;
88*4882a593Smuzhiyun goto out;
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun /*
92*4882a593Smuzhiyun * Get the device's power state from power resources settings and _PSC,
93*4882a593Smuzhiyun * if available.
94*4882a593Smuzhiyun */
95*4882a593Smuzhiyun if (device->power.flags.power_resources) {
96*4882a593Smuzhiyun error = acpi_power_get_inferred_state(device, &result);
97*4882a593Smuzhiyun if (error)
98*4882a593Smuzhiyun return error;
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun if (device->power.flags.explicit_get) {
101*4882a593Smuzhiyun int psc;
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun error = acpi_dev_pm_explicit_get(device, &psc);
104*4882a593Smuzhiyun if (error)
105*4882a593Smuzhiyun return error;
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun /*
108*4882a593Smuzhiyun * The power resources settings may indicate a power state
109*4882a593Smuzhiyun * shallower than the actual power state of the device, because
110*4882a593Smuzhiyun * the same power resources may be referenced by other devices.
111*4882a593Smuzhiyun *
112*4882a593Smuzhiyun * For systems predating ACPI 4.0 we assume that D3hot is the
113*4882a593Smuzhiyun * deepest state that can be supported.
114*4882a593Smuzhiyun */
115*4882a593Smuzhiyun if (psc > result && psc < ACPI_STATE_D3_COLD)
116*4882a593Smuzhiyun result = psc;
117*4882a593Smuzhiyun else if (result == ACPI_STATE_UNKNOWN)
118*4882a593Smuzhiyun result = psc > ACPI_STATE_D2 ? ACPI_STATE_D3_HOT : psc;
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun /*
122*4882a593Smuzhiyun * If we were unsure about the device parent's power state up to this
123*4882a593Smuzhiyun * point, the fact that the device is in D0 implies that the parent has
124*4882a593Smuzhiyun * to be in D0 too, except if ignore_parent is set.
125*4882a593Smuzhiyun */
126*4882a593Smuzhiyun if (!device->power.flags.ignore_parent && device->parent
127*4882a593Smuzhiyun && device->parent->power.state == ACPI_STATE_UNKNOWN
128*4882a593Smuzhiyun && result == ACPI_STATE_D0)
129*4882a593Smuzhiyun device->parent->power.state = ACPI_STATE_D0;
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun *state = result;
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun out:
134*4882a593Smuzhiyun ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] power state is %s\n",
135*4882a593Smuzhiyun device->pnp.bus_id, acpi_power_state_string(*state)));
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun return 0;
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun
acpi_dev_pm_explicit_set(struct acpi_device * adev,int state)140*4882a593Smuzhiyun static int acpi_dev_pm_explicit_set(struct acpi_device *adev, int state)
141*4882a593Smuzhiyun {
142*4882a593Smuzhiyun if (adev->power.states[state].flags.explicit_set) {
143*4882a593Smuzhiyun char method[5] = { '_', 'P', 'S', '0' + state, '\0' };
144*4882a593Smuzhiyun acpi_status status;
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun status = acpi_evaluate_object(adev->handle, method, NULL, NULL);
147*4882a593Smuzhiyun if (ACPI_FAILURE(status))
148*4882a593Smuzhiyun return -ENODEV;
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun return 0;
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun /**
154*4882a593Smuzhiyun * acpi_device_set_power - Set power state of an ACPI device.
155*4882a593Smuzhiyun * @device: Device to set the power state of.
156*4882a593Smuzhiyun * @state: New power state to set.
157*4882a593Smuzhiyun *
158*4882a593Smuzhiyun * Callers must ensure that the device is power manageable before using this
159*4882a593Smuzhiyun * function.
160*4882a593Smuzhiyun */
acpi_device_set_power(struct acpi_device * device,int state)161*4882a593Smuzhiyun int acpi_device_set_power(struct acpi_device *device, int state)
162*4882a593Smuzhiyun {
163*4882a593Smuzhiyun int target_state = state;
164*4882a593Smuzhiyun int result = 0;
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun if (!device || !device->flags.power_manageable
167*4882a593Smuzhiyun || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
168*4882a593Smuzhiyun return -EINVAL;
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun acpi_handle_debug(device->handle, "Power state change: %s -> %s\n",
171*4882a593Smuzhiyun acpi_power_state_string(device->power.state),
172*4882a593Smuzhiyun acpi_power_state_string(state));
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun /* Make sure this is a valid target state */
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun /* There is a special case for D0 addressed below. */
177*4882a593Smuzhiyun if (state > ACPI_STATE_D0 && state == device->power.state) {
178*4882a593Smuzhiyun ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] already in %s\n",
179*4882a593Smuzhiyun device->pnp.bus_id,
180*4882a593Smuzhiyun acpi_power_state_string(state)));
181*4882a593Smuzhiyun return 0;
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun if (state == ACPI_STATE_D3_COLD) {
185*4882a593Smuzhiyun /*
186*4882a593Smuzhiyun * For transitions to D3cold we need to execute _PS3 and then
187*4882a593Smuzhiyun * possibly drop references to the power resources in use.
188*4882a593Smuzhiyun */
189*4882a593Smuzhiyun state = ACPI_STATE_D3_HOT;
190*4882a593Smuzhiyun /* If D3cold is not supported, use D3hot as the target state. */
191*4882a593Smuzhiyun if (!device->power.states[ACPI_STATE_D3_COLD].flags.valid)
192*4882a593Smuzhiyun target_state = state;
193*4882a593Smuzhiyun } else if (!device->power.states[state].flags.valid) {
194*4882a593Smuzhiyun dev_warn(&device->dev, "Power state %s not supported\n",
195*4882a593Smuzhiyun acpi_power_state_string(state));
196*4882a593Smuzhiyun return -ENODEV;
197*4882a593Smuzhiyun }
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun if (!device->power.flags.ignore_parent &&
200*4882a593Smuzhiyun device->parent && (state < device->parent->power.state)) {
201*4882a593Smuzhiyun dev_warn(&device->dev,
202*4882a593Smuzhiyun "Cannot transition to power state %s for parent in %s\n",
203*4882a593Smuzhiyun acpi_power_state_string(state),
204*4882a593Smuzhiyun acpi_power_state_string(device->parent->power.state));
205*4882a593Smuzhiyun return -ENODEV;
206*4882a593Smuzhiyun }
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun /*
209*4882a593Smuzhiyun * Transition Power
210*4882a593Smuzhiyun * ----------------
211*4882a593Smuzhiyun * In accordance with ACPI 6, _PSx is executed before manipulating power
212*4882a593Smuzhiyun * resources, unless the target state is D0, in which case _PS0 is
213*4882a593Smuzhiyun * supposed to be executed after turning the power resources on.
214*4882a593Smuzhiyun */
215*4882a593Smuzhiyun if (state > ACPI_STATE_D0) {
216*4882a593Smuzhiyun /*
217*4882a593Smuzhiyun * According to ACPI 6, devices cannot go from lower-power
218*4882a593Smuzhiyun * (deeper) states to higher-power (shallower) states.
219*4882a593Smuzhiyun */
220*4882a593Smuzhiyun if (state < device->power.state) {
221*4882a593Smuzhiyun dev_warn(&device->dev, "Cannot transition from %s to %s\n",
222*4882a593Smuzhiyun acpi_power_state_string(device->power.state),
223*4882a593Smuzhiyun acpi_power_state_string(state));
224*4882a593Smuzhiyun return -ENODEV;
225*4882a593Smuzhiyun }
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun /*
228*4882a593Smuzhiyun * If the device goes from D3hot to D3cold, _PS3 has been
229*4882a593Smuzhiyun * evaluated for it already, so skip it in that case.
230*4882a593Smuzhiyun */
231*4882a593Smuzhiyun if (device->power.state < ACPI_STATE_D3_HOT) {
232*4882a593Smuzhiyun result = acpi_dev_pm_explicit_set(device, state);
233*4882a593Smuzhiyun if (result)
234*4882a593Smuzhiyun goto end;
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun if (device->power.flags.power_resources)
238*4882a593Smuzhiyun result = acpi_power_transition(device, target_state);
239*4882a593Smuzhiyun } else {
240*4882a593Smuzhiyun int cur_state = device->power.state;
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun if (device->power.flags.power_resources) {
243*4882a593Smuzhiyun result = acpi_power_transition(device, ACPI_STATE_D0);
244*4882a593Smuzhiyun if (result)
245*4882a593Smuzhiyun goto end;
246*4882a593Smuzhiyun }
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun if (cur_state == ACPI_STATE_D0) {
249*4882a593Smuzhiyun int psc;
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun /* Nothing to do here if _PSC is not present. */
252*4882a593Smuzhiyun if (!device->power.flags.explicit_get)
253*4882a593Smuzhiyun return 0;
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun /*
256*4882a593Smuzhiyun * The power state of the device was set to D0 last
257*4882a593Smuzhiyun * time, but that might have happened before a
258*4882a593Smuzhiyun * system-wide transition involving the platform
259*4882a593Smuzhiyun * firmware, so it may be necessary to evaluate _PS0
260*4882a593Smuzhiyun * for the device here. However, use extra care here
261*4882a593Smuzhiyun * and evaluate _PSC to check the device's current power
262*4882a593Smuzhiyun * state, and only invoke _PS0 if the evaluation of _PSC
263*4882a593Smuzhiyun * is successful and it returns a power state different
264*4882a593Smuzhiyun * from D0.
265*4882a593Smuzhiyun */
266*4882a593Smuzhiyun result = acpi_dev_pm_explicit_get(device, &psc);
267*4882a593Smuzhiyun if (result || psc == ACPI_STATE_D0)
268*4882a593Smuzhiyun return 0;
269*4882a593Smuzhiyun }
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun result = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0);
272*4882a593Smuzhiyun }
273*4882a593Smuzhiyun
274*4882a593Smuzhiyun end:
275*4882a593Smuzhiyun if (result) {
276*4882a593Smuzhiyun dev_warn(&device->dev, "Failed to change power state to %s\n",
277*4882a593Smuzhiyun acpi_power_state_string(target_state));
278*4882a593Smuzhiyun } else {
279*4882a593Smuzhiyun device->power.state = target_state;
280*4882a593Smuzhiyun ACPI_DEBUG_PRINT((ACPI_DB_INFO,
281*4882a593Smuzhiyun "Device [%s] transitioned to %s\n",
282*4882a593Smuzhiyun device->pnp.bus_id,
283*4882a593Smuzhiyun acpi_power_state_string(target_state)));
284*4882a593Smuzhiyun }
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun return result;
287*4882a593Smuzhiyun }
288*4882a593Smuzhiyun EXPORT_SYMBOL(acpi_device_set_power);
289*4882a593Smuzhiyun
acpi_bus_set_power(acpi_handle handle,int state)290*4882a593Smuzhiyun int acpi_bus_set_power(acpi_handle handle, int state)
291*4882a593Smuzhiyun {
292*4882a593Smuzhiyun struct acpi_device *device;
293*4882a593Smuzhiyun int result;
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun result = acpi_bus_get_device(handle, &device);
296*4882a593Smuzhiyun if (result)
297*4882a593Smuzhiyun return result;
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun return acpi_device_set_power(device, state);
300*4882a593Smuzhiyun }
301*4882a593Smuzhiyun EXPORT_SYMBOL(acpi_bus_set_power);
302*4882a593Smuzhiyun
acpi_bus_init_power(struct acpi_device * device)303*4882a593Smuzhiyun int acpi_bus_init_power(struct acpi_device *device)
304*4882a593Smuzhiyun {
305*4882a593Smuzhiyun int state;
306*4882a593Smuzhiyun int result;
307*4882a593Smuzhiyun
308*4882a593Smuzhiyun if (!device)
309*4882a593Smuzhiyun return -EINVAL;
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun device->power.state = ACPI_STATE_UNKNOWN;
312*4882a593Smuzhiyun if (!acpi_device_is_present(device)) {
313*4882a593Smuzhiyun device->flags.initialized = false;
314*4882a593Smuzhiyun return -ENXIO;
315*4882a593Smuzhiyun }
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun result = acpi_device_get_power(device, &state);
318*4882a593Smuzhiyun if (result)
319*4882a593Smuzhiyun return result;
320*4882a593Smuzhiyun
321*4882a593Smuzhiyun if (state < ACPI_STATE_D3_COLD && device->power.flags.power_resources) {
322*4882a593Smuzhiyun /* Reference count the power resources. */
323*4882a593Smuzhiyun result = acpi_power_on_resources(device, state);
324*4882a593Smuzhiyun if (result)
325*4882a593Smuzhiyun return result;
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun if (state == ACPI_STATE_D0) {
328*4882a593Smuzhiyun /*
329*4882a593Smuzhiyun * If _PSC is not present and the state inferred from
330*4882a593Smuzhiyun * power resources appears to be D0, it still may be
331*4882a593Smuzhiyun * necessary to execute _PS0 at this point, because
332*4882a593Smuzhiyun * another device using the same power resources may
333*4882a593Smuzhiyun * have been put into D0 previously and that's why we
334*4882a593Smuzhiyun * see D0 here.
335*4882a593Smuzhiyun */
336*4882a593Smuzhiyun result = acpi_dev_pm_explicit_set(device, state);
337*4882a593Smuzhiyun if (result)
338*4882a593Smuzhiyun return result;
339*4882a593Smuzhiyun }
340*4882a593Smuzhiyun } else if (state == ACPI_STATE_UNKNOWN) {
341*4882a593Smuzhiyun /*
342*4882a593Smuzhiyun * No power resources and missing _PSC? Cross fingers and make
343*4882a593Smuzhiyun * it D0 in hope that this is what the BIOS put the device into.
344*4882a593Smuzhiyun * [We tried to force D0 here by executing _PS0, but that broke
345*4882a593Smuzhiyun * Toshiba P870-303 in a nasty way.]
346*4882a593Smuzhiyun */
347*4882a593Smuzhiyun state = ACPI_STATE_D0;
348*4882a593Smuzhiyun }
349*4882a593Smuzhiyun device->power.state = state;
350*4882a593Smuzhiyun return 0;
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun /**
354*4882a593Smuzhiyun * acpi_device_fix_up_power - Force device with missing _PSC into D0.
355*4882a593Smuzhiyun * @device: Device object whose power state is to be fixed up.
356*4882a593Smuzhiyun *
357*4882a593Smuzhiyun * Devices without power resources and _PSC, but having _PS0 and _PS3 defined,
358*4882a593Smuzhiyun * are assumed to be put into D0 by the BIOS. However, in some cases that may
359*4882a593Smuzhiyun * not be the case and this function should be used then.
360*4882a593Smuzhiyun */
acpi_device_fix_up_power(struct acpi_device * device)361*4882a593Smuzhiyun int acpi_device_fix_up_power(struct acpi_device *device)
362*4882a593Smuzhiyun {
363*4882a593Smuzhiyun int ret = 0;
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun if (!device->power.flags.power_resources
366*4882a593Smuzhiyun && !device->power.flags.explicit_get
367*4882a593Smuzhiyun && device->power.state == ACPI_STATE_D0)
368*4882a593Smuzhiyun ret = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0);
369*4882a593Smuzhiyun
370*4882a593Smuzhiyun return ret;
371*4882a593Smuzhiyun }
372*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(acpi_device_fix_up_power);
373*4882a593Smuzhiyun
acpi_device_update_power(struct acpi_device * device,int * state_p)374*4882a593Smuzhiyun int acpi_device_update_power(struct acpi_device *device, int *state_p)
375*4882a593Smuzhiyun {
376*4882a593Smuzhiyun int state;
377*4882a593Smuzhiyun int result;
378*4882a593Smuzhiyun
379*4882a593Smuzhiyun if (device->power.state == ACPI_STATE_UNKNOWN) {
380*4882a593Smuzhiyun result = acpi_bus_init_power(device);
381*4882a593Smuzhiyun if (!result && state_p)
382*4882a593Smuzhiyun *state_p = device->power.state;
383*4882a593Smuzhiyun
384*4882a593Smuzhiyun return result;
385*4882a593Smuzhiyun }
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun result = acpi_device_get_power(device, &state);
388*4882a593Smuzhiyun if (result)
389*4882a593Smuzhiyun return result;
390*4882a593Smuzhiyun
391*4882a593Smuzhiyun if (state == ACPI_STATE_UNKNOWN) {
392*4882a593Smuzhiyun state = ACPI_STATE_D0;
393*4882a593Smuzhiyun result = acpi_device_set_power(device, state);
394*4882a593Smuzhiyun if (result)
395*4882a593Smuzhiyun return result;
396*4882a593Smuzhiyun } else {
397*4882a593Smuzhiyun if (device->power.flags.power_resources) {
398*4882a593Smuzhiyun /*
399*4882a593Smuzhiyun * We don't need to really switch the state, bu we need
400*4882a593Smuzhiyun * to update the power resources' reference counters.
401*4882a593Smuzhiyun */
402*4882a593Smuzhiyun result = acpi_power_transition(device, state);
403*4882a593Smuzhiyun if (result)
404*4882a593Smuzhiyun return result;
405*4882a593Smuzhiyun }
406*4882a593Smuzhiyun device->power.state = state;
407*4882a593Smuzhiyun }
408*4882a593Smuzhiyun if (state_p)
409*4882a593Smuzhiyun *state_p = state;
410*4882a593Smuzhiyun
411*4882a593Smuzhiyun return 0;
412*4882a593Smuzhiyun }
413*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(acpi_device_update_power);
414*4882a593Smuzhiyun
acpi_bus_update_power(acpi_handle handle,int * state_p)415*4882a593Smuzhiyun int acpi_bus_update_power(acpi_handle handle, int *state_p)
416*4882a593Smuzhiyun {
417*4882a593Smuzhiyun struct acpi_device *device;
418*4882a593Smuzhiyun int result;
419*4882a593Smuzhiyun
420*4882a593Smuzhiyun result = acpi_bus_get_device(handle, &device);
421*4882a593Smuzhiyun return result ? result : acpi_device_update_power(device, state_p);
422*4882a593Smuzhiyun }
423*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(acpi_bus_update_power);
424*4882a593Smuzhiyun
acpi_bus_power_manageable(acpi_handle handle)425*4882a593Smuzhiyun bool acpi_bus_power_manageable(acpi_handle handle)
426*4882a593Smuzhiyun {
427*4882a593Smuzhiyun struct acpi_device *device;
428*4882a593Smuzhiyun int result;
429*4882a593Smuzhiyun
430*4882a593Smuzhiyun result = acpi_bus_get_device(handle, &device);
431*4882a593Smuzhiyun return result ? false : device->flags.power_manageable;
432*4882a593Smuzhiyun }
433*4882a593Smuzhiyun EXPORT_SYMBOL(acpi_bus_power_manageable);
434*4882a593Smuzhiyun
435*4882a593Smuzhiyun #ifdef CONFIG_PM
436*4882a593Smuzhiyun static DEFINE_MUTEX(acpi_pm_notifier_lock);
437*4882a593Smuzhiyun static DEFINE_MUTEX(acpi_pm_notifier_install_lock);
438*4882a593Smuzhiyun
acpi_pm_wakeup_event(struct device * dev)439*4882a593Smuzhiyun void acpi_pm_wakeup_event(struct device *dev)
440*4882a593Smuzhiyun {
441*4882a593Smuzhiyun pm_wakeup_dev_event(dev, 0, acpi_s2idle_wakeup());
442*4882a593Smuzhiyun }
443*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(acpi_pm_wakeup_event);
444*4882a593Smuzhiyun
acpi_pm_notify_handler(acpi_handle handle,u32 val,void * not_used)445*4882a593Smuzhiyun static void acpi_pm_notify_handler(acpi_handle handle, u32 val, void *not_used)
446*4882a593Smuzhiyun {
447*4882a593Smuzhiyun struct acpi_device *adev;
448*4882a593Smuzhiyun
449*4882a593Smuzhiyun if (val != ACPI_NOTIFY_DEVICE_WAKE)
450*4882a593Smuzhiyun return;
451*4882a593Smuzhiyun
452*4882a593Smuzhiyun acpi_handle_debug(handle, "Wake notify\n");
453*4882a593Smuzhiyun
454*4882a593Smuzhiyun adev = acpi_bus_get_acpi_device(handle);
455*4882a593Smuzhiyun if (!adev)
456*4882a593Smuzhiyun return;
457*4882a593Smuzhiyun
458*4882a593Smuzhiyun mutex_lock(&acpi_pm_notifier_lock);
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun if (adev->wakeup.flags.notifier_present) {
461*4882a593Smuzhiyun pm_wakeup_ws_event(adev->wakeup.ws, 0, acpi_s2idle_wakeup());
462*4882a593Smuzhiyun if (adev->wakeup.context.func) {
463*4882a593Smuzhiyun acpi_handle_debug(handle, "Running %pS for %s\n",
464*4882a593Smuzhiyun adev->wakeup.context.func,
465*4882a593Smuzhiyun dev_name(adev->wakeup.context.dev));
466*4882a593Smuzhiyun adev->wakeup.context.func(&adev->wakeup.context);
467*4882a593Smuzhiyun }
468*4882a593Smuzhiyun }
469*4882a593Smuzhiyun
470*4882a593Smuzhiyun mutex_unlock(&acpi_pm_notifier_lock);
471*4882a593Smuzhiyun
472*4882a593Smuzhiyun acpi_bus_put_acpi_device(adev);
473*4882a593Smuzhiyun }
474*4882a593Smuzhiyun
475*4882a593Smuzhiyun /**
476*4882a593Smuzhiyun * acpi_add_pm_notifier - Register PM notify handler for given ACPI device.
477*4882a593Smuzhiyun * @adev: ACPI device to add the notify handler for.
478*4882a593Smuzhiyun * @dev: Device to generate a wakeup event for while handling the notification.
479*4882a593Smuzhiyun * @func: Work function to execute when handling the notification.
480*4882a593Smuzhiyun *
481*4882a593Smuzhiyun * NOTE: @adev need not be a run-wake or wakeup device to be a valid source of
482*4882a593Smuzhiyun * PM wakeup events. For example, wakeup events may be generated for bridges
483*4882a593Smuzhiyun * if one of the devices below the bridge is signaling wakeup, even if the
484*4882a593Smuzhiyun * bridge itself doesn't have a wakeup GPE associated with it.
485*4882a593Smuzhiyun */
acpi_add_pm_notifier(struct acpi_device * adev,struct device * dev,void (* func)(struct acpi_device_wakeup_context * context))486*4882a593Smuzhiyun acpi_status acpi_add_pm_notifier(struct acpi_device *adev, struct device *dev,
487*4882a593Smuzhiyun void (*func)(struct acpi_device_wakeup_context *context))
488*4882a593Smuzhiyun {
489*4882a593Smuzhiyun acpi_status status = AE_ALREADY_EXISTS;
490*4882a593Smuzhiyun
491*4882a593Smuzhiyun if (!dev && !func)
492*4882a593Smuzhiyun return AE_BAD_PARAMETER;
493*4882a593Smuzhiyun
494*4882a593Smuzhiyun mutex_lock(&acpi_pm_notifier_install_lock);
495*4882a593Smuzhiyun
496*4882a593Smuzhiyun if (adev->wakeup.flags.notifier_present)
497*4882a593Smuzhiyun goto out;
498*4882a593Smuzhiyun
499*4882a593Smuzhiyun status = acpi_install_notify_handler(adev->handle, ACPI_SYSTEM_NOTIFY,
500*4882a593Smuzhiyun acpi_pm_notify_handler, NULL);
501*4882a593Smuzhiyun if (ACPI_FAILURE(status))
502*4882a593Smuzhiyun goto out;
503*4882a593Smuzhiyun
504*4882a593Smuzhiyun mutex_lock(&acpi_pm_notifier_lock);
505*4882a593Smuzhiyun adev->wakeup.ws = wakeup_source_register(&adev->dev,
506*4882a593Smuzhiyun dev_name(&adev->dev));
507*4882a593Smuzhiyun adev->wakeup.context.dev = dev;
508*4882a593Smuzhiyun adev->wakeup.context.func = func;
509*4882a593Smuzhiyun adev->wakeup.flags.notifier_present = true;
510*4882a593Smuzhiyun mutex_unlock(&acpi_pm_notifier_lock);
511*4882a593Smuzhiyun
512*4882a593Smuzhiyun out:
513*4882a593Smuzhiyun mutex_unlock(&acpi_pm_notifier_install_lock);
514*4882a593Smuzhiyun return status;
515*4882a593Smuzhiyun }
516*4882a593Smuzhiyun
517*4882a593Smuzhiyun /**
518*4882a593Smuzhiyun * acpi_remove_pm_notifier - Unregister PM notifier from given ACPI device.
519*4882a593Smuzhiyun * @adev: ACPI device to remove the notifier from.
520*4882a593Smuzhiyun */
acpi_remove_pm_notifier(struct acpi_device * adev)521*4882a593Smuzhiyun acpi_status acpi_remove_pm_notifier(struct acpi_device *adev)
522*4882a593Smuzhiyun {
523*4882a593Smuzhiyun acpi_status status = AE_BAD_PARAMETER;
524*4882a593Smuzhiyun
525*4882a593Smuzhiyun mutex_lock(&acpi_pm_notifier_install_lock);
526*4882a593Smuzhiyun
527*4882a593Smuzhiyun if (!adev->wakeup.flags.notifier_present)
528*4882a593Smuzhiyun goto out;
529*4882a593Smuzhiyun
530*4882a593Smuzhiyun status = acpi_remove_notify_handler(adev->handle,
531*4882a593Smuzhiyun ACPI_SYSTEM_NOTIFY,
532*4882a593Smuzhiyun acpi_pm_notify_handler);
533*4882a593Smuzhiyun if (ACPI_FAILURE(status))
534*4882a593Smuzhiyun goto out;
535*4882a593Smuzhiyun
536*4882a593Smuzhiyun mutex_lock(&acpi_pm_notifier_lock);
537*4882a593Smuzhiyun adev->wakeup.context.func = NULL;
538*4882a593Smuzhiyun adev->wakeup.context.dev = NULL;
539*4882a593Smuzhiyun wakeup_source_unregister(adev->wakeup.ws);
540*4882a593Smuzhiyun adev->wakeup.flags.notifier_present = false;
541*4882a593Smuzhiyun mutex_unlock(&acpi_pm_notifier_lock);
542*4882a593Smuzhiyun
543*4882a593Smuzhiyun out:
544*4882a593Smuzhiyun mutex_unlock(&acpi_pm_notifier_install_lock);
545*4882a593Smuzhiyun return status;
546*4882a593Smuzhiyun }
547*4882a593Smuzhiyun
acpi_bus_can_wakeup(acpi_handle handle)548*4882a593Smuzhiyun bool acpi_bus_can_wakeup(acpi_handle handle)
549*4882a593Smuzhiyun {
550*4882a593Smuzhiyun struct acpi_device *device;
551*4882a593Smuzhiyun int result;
552*4882a593Smuzhiyun
553*4882a593Smuzhiyun result = acpi_bus_get_device(handle, &device);
554*4882a593Smuzhiyun return result ? false : device->wakeup.flags.valid;
555*4882a593Smuzhiyun }
556*4882a593Smuzhiyun EXPORT_SYMBOL(acpi_bus_can_wakeup);
557*4882a593Smuzhiyun
acpi_pm_device_can_wakeup(struct device * dev)558*4882a593Smuzhiyun bool acpi_pm_device_can_wakeup(struct device *dev)
559*4882a593Smuzhiyun {
560*4882a593Smuzhiyun struct acpi_device *adev = ACPI_COMPANION(dev);
561*4882a593Smuzhiyun
562*4882a593Smuzhiyun return adev ? acpi_device_can_wakeup(adev) : false;
563*4882a593Smuzhiyun }
564*4882a593Smuzhiyun
565*4882a593Smuzhiyun /**
566*4882a593Smuzhiyun * acpi_dev_pm_get_state - Get preferred power state of ACPI device.
567*4882a593Smuzhiyun * @dev: Device whose preferred target power state to return.
568*4882a593Smuzhiyun * @adev: ACPI device node corresponding to @dev.
569*4882a593Smuzhiyun * @target_state: System state to match the resultant device state.
570*4882a593Smuzhiyun * @d_min_p: Location to store the highest power state available to the device.
571*4882a593Smuzhiyun * @d_max_p: Location to store the lowest power state available to the device.
572*4882a593Smuzhiyun *
573*4882a593Smuzhiyun * Find the lowest power (highest number) and highest power (lowest number) ACPI
574*4882a593Smuzhiyun * device power states that the device can be in while the system is in the
575*4882a593Smuzhiyun * state represented by @target_state. Store the integer numbers representing
576*4882a593Smuzhiyun * those stats in the memory locations pointed to by @d_max_p and @d_min_p,
577*4882a593Smuzhiyun * respectively.
578*4882a593Smuzhiyun *
579*4882a593Smuzhiyun * Callers must ensure that @dev and @adev are valid pointers and that @adev
580*4882a593Smuzhiyun * actually corresponds to @dev before using this function.
581*4882a593Smuzhiyun *
582*4882a593Smuzhiyun * Returns 0 on success or -ENODATA when one of the ACPI methods fails or
583*4882a593Smuzhiyun * returns a value that doesn't make sense. The memory locations pointed to by
584*4882a593Smuzhiyun * @d_max_p and @d_min_p are only modified on success.
585*4882a593Smuzhiyun */
acpi_dev_pm_get_state(struct device * dev,struct acpi_device * adev,u32 target_state,int * d_min_p,int * d_max_p)586*4882a593Smuzhiyun static int acpi_dev_pm_get_state(struct device *dev, struct acpi_device *adev,
587*4882a593Smuzhiyun u32 target_state, int *d_min_p, int *d_max_p)
588*4882a593Smuzhiyun {
589*4882a593Smuzhiyun char method[] = { '_', 'S', '0' + target_state, 'D', '\0' };
590*4882a593Smuzhiyun acpi_handle handle = adev->handle;
591*4882a593Smuzhiyun unsigned long long ret;
592*4882a593Smuzhiyun int d_min, d_max;
593*4882a593Smuzhiyun bool wakeup = false;
594*4882a593Smuzhiyun bool has_sxd = false;
595*4882a593Smuzhiyun acpi_status status;
596*4882a593Smuzhiyun
597*4882a593Smuzhiyun /*
598*4882a593Smuzhiyun * If the system state is S0, the lowest power state the device can be
599*4882a593Smuzhiyun * in is D3cold, unless the device has _S0W and is supposed to signal
600*4882a593Smuzhiyun * wakeup, in which case the return value of _S0W has to be used as the
601*4882a593Smuzhiyun * lowest power state available to the device.
602*4882a593Smuzhiyun */
603*4882a593Smuzhiyun d_min = ACPI_STATE_D0;
604*4882a593Smuzhiyun d_max = ACPI_STATE_D3_COLD;
605*4882a593Smuzhiyun
606*4882a593Smuzhiyun /*
607*4882a593Smuzhiyun * If present, _SxD methods return the minimum D-state (highest power
608*4882a593Smuzhiyun * state) we can use for the corresponding S-states. Otherwise, the
609*4882a593Smuzhiyun * minimum D-state is D0 (ACPI 3.x).
610*4882a593Smuzhiyun */
611*4882a593Smuzhiyun if (target_state > ACPI_STATE_S0) {
612*4882a593Smuzhiyun /*
613*4882a593Smuzhiyun * We rely on acpi_evaluate_integer() not clobbering the integer
614*4882a593Smuzhiyun * provided if AE_NOT_FOUND is returned.
615*4882a593Smuzhiyun */
616*4882a593Smuzhiyun ret = d_min;
617*4882a593Smuzhiyun status = acpi_evaluate_integer(handle, method, NULL, &ret);
618*4882a593Smuzhiyun if ((ACPI_FAILURE(status) && status != AE_NOT_FOUND)
619*4882a593Smuzhiyun || ret > ACPI_STATE_D3_COLD)
620*4882a593Smuzhiyun return -ENODATA;
621*4882a593Smuzhiyun
622*4882a593Smuzhiyun /*
623*4882a593Smuzhiyun * We need to handle legacy systems where D3hot and D3cold are
624*4882a593Smuzhiyun * the same and 3 is returned in both cases, so fall back to
625*4882a593Smuzhiyun * D3cold if D3hot is not a valid state.
626*4882a593Smuzhiyun */
627*4882a593Smuzhiyun if (!adev->power.states[ret].flags.valid) {
628*4882a593Smuzhiyun if (ret == ACPI_STATE_D3_HOT)
629*4882a593Smuzhiyun ret = ACPI_STATE_D3_COLD;
630*4882a593Smuzhiyun else
631*4882a593Smuzhiyun return -ENODATA;
632*4882a593Smuzhiyun }
633*4882a593Smuzhiyun
634*4882a593Smuzhiyun if (status == AE_OK)
635*4882a593Smuzhiyun has_sxd = true;
636*4882a593Smuzhiyun
637*4882a593Smuzhiyun d_min = ret;
638*4882a593Smuzhiyun wakeup = device_may_wakeup(dev) && adev->wakeup.flags.valid
639*4882a593Smuzhiyun && adev->wakeup.sleep_state >= target_state;
640*4882a593Smuzhiyun } else {
641*4882a593Smuzhiyun wakeup = adev->wakeup.flags.valid;
642*4882a593Smuzhiyun }
643*4882a593Smuzhiyun
644*4882a593Smuzhiyun /*
645*4882a593Smuzhiyun * If _PRW says we can wake up the system from the target sleep state,
646*4882a593Smuzhiyun * the D-state returned by _SxD is sufficient for that (we assume a
647*4882a593Smuzhiyun * wakeup-aware driver if wake is set). Still, if _SxW exists
648*4882a593Smuzhiyun * (ACPI 3.x), it should return the maximum (lowest power) D-state that
649*4882a593Smuzhiyun * can wake the system. _S0W may be valid, too.
650*4882a593Smuzhiyun */
651*4882a593Smuzhiyun if (wakeup) {
652*4882a593Smuzhiyun method[3] = 'W';
653*4882a593Smuzhiyun status = acpi_evaluate_integer(handle, method, NULL, &ret);
654*4882a593Smuzhiyun if (status == AE_NOT_FOUND) {
655*4882a593Smuzhiyun /* No _SxW. In this case, the ACPI spec says that we
656*4882a593Smuzhiyun * must not go into any power state deeper than the
657*4882a593Smuzhiyun * value returned from _SxD.
658*4882a593Smuzhiyun */
659*4882a593Smuzhiyun if (has_sxd && target_state > ACPI_STATE_S0)
660*4882a593Smuzhiyun d_max = d_min;
661*4882a593Smuzhiyun } else if (ACPI_SUCCESS(status) && ret <= ACPI_STATE_D3_COLD) {
662*4882a593Smuzhiyun /* Fall back to D3cold if ret is not a valid state. */
663*4882a593Smuzhiyun if (!adev->power.states[ret].flags.valid)
664*4882a593Smuzhiyun ret = ACPI_STATE_D3_COLD;
665*4882a593Smuzhiyun
666*4882a593Smuzhiyun d_max = ret > d_min ? ret : d_min;
667*4882a593Smuzhiyun } else {
668*4882a593Smuzhiyun return -ENODATA;
669*4882a593Smuzhiyun }
670*4882a593Smuzhiyun }
671*4882a593Smuzhiyun
672*4882a593Smuzhiyun if (d_min_p)
673*4882a593Smuzhiyun *d_min_p = d_min;
674*4882a593Smuzhiyun
675*4882a593Smuzhiyun if (d_max_p)
676*4882a593Smuzhiyun *d_max_p = d_max;
677*4882a593Smuzhiyun
678*4882a593Smuzhiyun return 0;
679*4882a593Smuzhiyun }
680*4882a593Smuzhiyun
681*4882a593Smuzhiyun /**
682*4882a593Smuzhiyun * acpi_pm_device_sleep_state - Get preferred power state of ACPI device.
683*4882a593Smuzhiyun * @dev: Device whose preferred target power state to return.
684*4882a593Smuzhiyun * @d_min_p: Location to store the upper limit of the allowed states range.
685*4882a593Smuzhiyun * @d_max_in: Deepest low-power state to take into consideration.
686*4882a593Smuzhiyun * Return value: Preferred power state of the device on success, -ENODEV
687*4882a593Smuzhiyun * if there's no 'struct acpi_device' for @dev, -EINVAL if @d_max_in is
688*4882a593Smuzhiyun * incorrect, or -ENODATA on ACPI method failure.
689*4882a593Smuzhiyun *
690*4882a593Smuzhiyun * The caller must ensure that @dev is valid before using this function.
691*4882a593Smuzhiyun */
acpi_pm_device_sleep_state(struct device * dev,int * d_min_p,int d_max_in)692*4882a593Smuzhiyun int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in)
693*4882a593Smuzhiyun {
694*4882a593Smuzhiyun struct acpi_device *adev;
695*4882a593Smuzhiyun int ret, d_min, d_max;
696*4882a593Smuzhiyun
697*4882a593Smuzhiyun if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3_COLD)
698*4882a593Smuzhiyun return -EINVAL;
699*4882a593Smuzhiyun
700*4882a593Smuzhiyun if (d_max_in > ACPI_STATE_D2) {
701*4882a593Smuzhiyun enum pm_qos_flags_status stat;
702*4882a593Smuzhiyun
703*4882a593Smuzhiyun stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF);
704*4882a593Smuzhiyun if (stat == PM_QOS_FLAGS_ALL)
705*4882a593Smuzhiyun d_max_in = ACPI_STATE_D2;
706*4882a593Smuzhiyun }
707*4882a593Smuzhiyun
708*4882a593Smuzhiyun adev = ACPI_COMPANION(dev);
709*4882a593Smuzhiyun if (!adev) {
710*4882a593Smuzhiyun dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);
711*4882a593Smuzhiyun return -ENODEV;
712*4882a593Smuzhiyun }
713*4882a593Smuzhiyun
714*4882a593Smuzhiyun ret = acpi_dev_pm_get_state(dev, adev, acpi_target_system_state(),
715*4882a593Smuzhiyun &d_min, &d_max);
716*4882a593Smuzhiyun if (ret)
717*4882a593Smuzhiyun return ret;
718*4882a593Smuzhiyun
719*4882a593Smuzhiyun if (d_max_in < d_min)
720*4882a593Smuzhiyun return -EINVAL;
721*4882a593Smuzhiyun
722*4882a593Smuzhiyun if (d_max > d_max_in) {
723*4882a593Smuzhiyun for (d_max = d_max_in; d_max > d_min; d_max--) {
724*4882a593Smuzhiyun if (adev->power.states[d_max].flags.valid)
725*4882a593Smuzhiyun break;
726*4882a593Smuzhiyun }
727*4882a593Smuzhiyun }
728*4882a593Smuzhiyun
729*4882a593Smuzhiyun if (d_min_p)
730*4882a593Smuzhiyun *d_min_p = d_min;
731*4882a593Smuzhiyun
732*4882a593Smuzhiyun return d_max;
733*4882a593Smuzhiyun }
734*4882a593Smuzhiyun EXPORT_SYMBOL(acpi_pm_device_sleep_state);
735*4882a593Smuzhiyun
736*4882a593Smuzhiyun /**
737*4882a593Smuzhiyun * acpi_pm_notify_work_func - ACPI devices wakeup notification work function.
738*4882a593Smuzhiyun * @context: Device wakeup context.
739*4882a593Smuzhiyun */
acpi_pm_notify_work_func(struct acpi_device_wakeup_context * context)740*4882a593Smuzhiyun static void acpi_pm_notify_work_func(struct acpi_device_wakeup_context *context)
741*4882a593Smuzhiyun {
742*4882a593Smuzhiyun struct device *dev = context->dev;
743*4882a593Smuzhiyun
744*4882a593Smuzhiyun if (dev) {
745*4882a593Smuzhiyun pm_wakeup_event(dev, 0);
746*4882a593Smuzhiyun pm_request_resume(dev);
747*4882a593Smuzhiyun }
748*4882a593Smuzhiyun }
749*4882a593Smuzhiyun
750*4882a593Smuzhiyun static DEFINE_MUTEX(acpi_wakeup_lock);
751*4882a593Smuzhiyun
__acpi_device_wakeup_enable(struct acpi_device * adev,u32 target_state)752*4882a593Smuzhiyun static int __acpi_device_wakeup_enable(struct acpi_device *adev,
753*4882a593Smuzhiyun u32 target_state)
754*4882a593Smuzhiyun {
755*4882a593Smuzhiyun struct acpi_device_wakeup *wakeup = &adev->wakeup;
756*4882a593Smuzhiyun acpi_status status;
757*4882a593Smuzhiyun int error = 0;
758*4882a593Smuzhiyun
759*4882a593Smuzhiyun mutex_lock(&acpi_wakeup_lock);
760*4882a593Smuzhiyun
761*4882a593Smuzhiyun if (wakeup->enable_count >= INT_MAX) {
762*4882a593Smuzhiyun acpi_handle_info(adev->handle, "Wakeup enable count out of bounds!\n");
763*4882a593Smuzhiyun goto out;
764*4882a593Smuzhiyun }
765*4882a593Smuzhiyun if (wakeup->enable_count > 0)
766*4882a593Smuzhiyun goto inc;
767*4882a593Smuzhiyun
768*4882a593Smuzhiyun error = acpi_enable_wakeup_device_power(adev, target_state);
769*4882a593Smuzhiyun if (error)
770*4882a593Smuzhiyun goto out;
771*4882a593Smuzhiyun
772*4882a593Smuzhiyun status = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number);
773*4882a593Smuzhiyun if (ACPI_FAILURE(status)) {
774*4882a593Smuzhiyun acpi_disable_wakeup_device_power(adev);
775*4882a593Smuzhiyun error = -EIO;
776*4882a593Smuzhiyun goto out;
777*4882a593Smuzhiyun }
778*4882a593Smuzhiyun
779*4882a593Smuzhiyun acpi_handle_debug(adev->handle, "GPE%2X enabled for wakeup\n",
780*4882a593Smuzhiyun (unsigned int)wakeup->gpe_number);
781*4882a593Smuzhiyun
782*4882a593Smuzhiyun inc:
783*4882a593Smuzhiyun wakeup->enable_count++;
784*4882a593Smuzhiyun
785*4882a593Smuzhiyun out:
786*4882a593Smuzhiyun mutex_unlock(&acpi_wakeup_lock);
787*4882a593Smuzhiyun return error;
788*4882a593Smuzhiyun }
789*4882a593Smuzhiyun
790*4882a593Smuzhiyun /**
791*4882a593Smuzhiyun * acpi_device_wakeup_enable - Enable wakeup functionality for device.
792*4882a593Smuzhiyun * @adev: ACPI device to enable wakeup functionality for.
793*4882a593Smuzhiyun * @target_state: State the system is transitioning into.
794*4882a593Smuzhiyun *
795*4882a593Smuzhiyun * Enable the GPE associated with @adev so that it can generate wakeup signals
796*4882a593Smuzhiyun * for the device in response to external (remote) events and enable wakeup
797*4882a593Smuzhiyun * power for it.
798*4882a593Smuzhiyun *
799*4882a593Smuzhiyun * Callers must ensure that @adev is a valid ACPI device node before executing
800*4882a593Smuzhiyun * this function.
801*4882a593Smuzhiyun */
acpi_device_wakeup_enable(struct acpi_device * adev,u32 target_state)802*4882a593Smuzhiyun static int acpi_device_wakeup_enable(struct acpi_device *adev, u32 target_state)
803*4882a593Smuzhiyun {
804*4882a593Smuzhiyun return __acpi_device_wakeup_enable(adev, target_state);
805*4882a593Smuzhiyun }
806*4882a593Smuzhiyun
807*4882a593Smuzhiyun /**
808*4882a593Smuzhiyun * acpi_device_wakeup_disable - Disable wakeup functionality for device.
809*4882a593Smuzhiyun * @adev: ACPI device to disable wakeup functionality for.
810*4882a593Smuzhiyun *
811*4882a593Smuzhiyun * Disable the GPE associated with @adev and disable wakeup power for it.
812*4882a593Smuzhiyun *
813*4882a593Smuzhiyun * Callers must ensure that @adev is a valid ACPI device node before executing
814*4882a593Smuzhiyun * this function.
815*4882a593Smuzhiyun */
acpi_device_wakeup_disable(struct acpi_device * adev)816*4882a593Smuzhiyun static void acpi_device_wakeup_disable(struct acpi_device *adev)
817*4882a593Smuzhiyun {
818*4882a593Smuzhiyun struct acpi_device_wakeup *wakeup = &adev->wakeup;
819*4882a593Smuzhiyun
820*4882a593Smuzhiyun mutex_lock(&acpi_wakeup_lock);
821*4882a593Smuzhiyun
822*4882a593Smuzhiyun if (!wakeup->enable_count)
823*4882a593Smuzhiyun goto out;
824*4882a593Smuzhiyun
825*4882a593Smuzhiyun acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number);
826*4882a593Smuzhiyun acpi_disable_wakeup_device_power(adev);
827*4882a593Smuzhiyun
828*4882a593Smuzhiyun wakeup->enable_count--;
829*4882a593Smuzhiyun
830*4882a593Smuzhiyun out:
831*4882a593Smuzhiyun mutex_unlock(&acpi_wakeup_lock);
832*4882a593Smuzhiyun }
833*4882a593Smuzhiyun
834*4882a593Smuzhiyun /**
835*4882a593Smuzhiyun * acpi_pm_set_device_wakeup - Enable/disable remote wakeup for given device.
836*4882a593Smuzhiyun * @dev: Device to enable/disable to generate wakeup events.
837*4882a593Smuzhiyun * @enable: Whether to enable or disable the wakeup functionality.
838*4882a593Smuzhiyun */
acpi_pm_set_device_wakeup(struct device * dev,bool enable)839*4882a593Smuzhiyun int acpi_pm_set_device_wakeup(struct device *dev, bool enable)
840*4882a593Smuzhiyun {
841*4882a593Smuzhiyun struct acpi_device *adev;
842*4882a593Smuzhiyun int error;
843*4882a593Smuzhiyun
844*4882a593Smuzhiyun adev = ACPI_COMPANION(dev);
845*4882a593Smuzhiyun if (!adev) {
846*4882a593Smuzhiyun dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);
847*4882a593Smuzhiyun return -ENODEV;
848*4882a593Smuzhiyun }
849*4882a593Smuzhiyun
850*4882a593Smuzhiyun if (!acpi_device_can_wakeup(adev))
851*4882a593Smuzhiyun return -EINVAL;
852*4882a593Smuzhiyun
853*4882a593Smuzhiyun if (!enable) {
854*4882a593Smuzhiyun acpi_device_wakeup_disable(adev);
855*4882a593Smuzhiyun dev_dbg(dev, "Wakeup disabled by ACPI\n");
856*4882a593Smuzhiyun return 0;
857*4882a593Smuzhiyun }
858*4882a593Smuzhiyun
859*4882a593Smuzhiyun error = __acpi_device_wakeup_enable(adev, acpi_target_system_state());
860*4882a593Smuzhiyun if (!error)
861*4882a593Smuzhiyun dev_dbg(dev, "Wakeup enabled by ACPI\n");
862*4882a593Smuzhiyun
863*4882a593Smuzhiyun return error;
864*4882a593Smuzhiyun }
865*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(acpi_pm_set_device_wakeup);
866*4882a593Smuzhiyun
867*4882a593Smuzhiyun /**
868*4882a593Smuzhiyun * acpi_dev_pm_low_power - Put ACPI device into a low-power state.
869*4882a593Smuzhiyun * @dev: Device to put into a low-power state.
870*4882a593Smuzhiyun * @adev: ACPI device node corresponding to @dev.
871*4882a593Smuzhiyun * @system_state: System state to choose the device state for.
872*4882a593Smuzhiyun */
acpi_dev_pm_low_power(struct device * dev,struct acpi_device * adev,u32 system_state)873*4882a593Smuzhiyun static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev,
874*4882a593Smuzhiyun u32 system_state)
875*4882a593Smuzhiyun {
876*4882a593Smuzhiyun int ret, state;
877*4882a593Smuzhiyun
878*4882a593Smuzhiyun if (!acpi_device_power_manageable(adev))
879*4882a593Smuzhiyun return 0;
880*4882a593Smuzhiyun
881*4882a593Smuzhiyun ret = acpi_dev_pm_get_state(dev, adev, system_state, NULL, &state);
882*4882a593Smuzhiyun return ret ? ret : acpi_device_set_power(adev, state);
883*4882a593Smuzhiyun }
884*4882a593Smuzhiyun
885*4882a593Smuzhiyun /**
886*4882a593Smuzhiyun * acpi_dev_pm_full_power - Put ACPI device into the full-power state.
887*4882a593Smuzhiyun * @adev: ACPI device node to put into the full-power state.
888*4882a593Smuzhiyun */
acpi_dev_pm_full_power(struct acpi_device * adev)889*4882a593Smuzhiyun static int acpi_dev_pm_full_power(struct acpi_device *adev)
890*4882a593Smuzhiyun {
891*4882a593Smuzhiyun return acpi_device_power_manageable(adev) ?
892*4882a593Smuzhiyun acpi_device_set_power(adev, ACPI_STATE_D0) : 0;
893*4882a593Smuzhiyun }
894*4882a593Smuzhiyun
895*4882a593Smuzhiyun /**
896*4882a593Smuzhiyun * acpi_dev_suspend - Put device into a low-power state using ACPI.
897*4882a593Smuzhiyun * @dev: Device to put into a low-power state.
898*4882a593Smuzhiyun * @wakeup: Whether or not to enable wakeup for the device.
899*4882a593Smuzhiyun *
900*4882a593Smuzhiyun * Put the given device into a low-power state using the standard ACPI
901*4882a593Smuzhiyun * mechanism. Set up remote wakeup if desired, choose the state to put the
902*4882a593Smuzhiyun * device into (this checks if remote wakeup is expected to work too), and set
903*4882a593Smuzhiyun * the power state of the device.
904*4882a593Smuzhiyun */
acpi_dev_suspend(struct device * dev,bool wakeup)905*4882a593Smuzhiyun int acpi_dev_suspend(struct device *dev, bool wakeup)
906*4882a593Smuzhiyun {
907*4882a593Smuzhiyun struct acpi_device *adev = ACPI_COMPANION(dev);
908*4882a593Smuzhiyun u32 target_state = acpi_target_system_state();
909*4882a593Smuzhiyun int error;
910*4882a593Smuzhiyun
911*4882a593Smuzhiyun if (!adev)
912*4882a593Smuzhiyun return 0;
913*4882a593Smuzhiyun
914*4882a593Smuzhiyun if (wakeup && acpi_device_can_wakeup(adev)) {
915*4882a593Smuzhiyun error = acpi_device_wakeup_enable(adev, target_state);
916*4882a593Smuzhiyun if (error)
917*4882a593Smuzhiyun return -EAGAIN;
918*4882a593Smuzhiyun } else {
919*4882a593Smuzhiyun wakeup = false;
920*4882a593Smuzhiyun }
921*4882a593Smuzhiyun
922*4882a593Smuzhiyun error = acpi_dev_pm_low_power(dev, adev, target_state);
923*4882a593Smuzhiyun if (error && wakeup)
924*4882a593Smuzhiyun acpi_device_wakeup_disable(adev);
925*4882a593Smuzhiyun
926*4882a593Smuzhiyun return error;
927*4882a593Smuzhiyun }
928*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(acpi_dev_suspend);
929*4882a593Smuzhiyun
930*4882a593Smuzhiyun /**
931*4882a593Smuzhiyun * acpi_dev_resume - Put device into the full-power state using ACPI.
932*4882a593Smuzhiyun * @dev: Device to put into the full-power state.
933*4882a593Smuzhiyun *
934*4882a593Smuzhiyun * Put the given device into the full-power state using the standard ACPI
935*4882a593Smuzhiyun * mechanism. Set the power state of the device to ACPI D0 and disable wakeup.
936*4882a593Smuzhiyun */
acpi_dev_resume(struct device * dev)937*4882a593Smuzhiyun int acpi_dev_resume(struct device *dev)
938*4882a593Smuzhiyun {
939*4882a593Smuzhiyun struct acpi_device *adev = ACPI_COMPANION(dev);
940*4882a593Smuzhiyun int error;
941*4882a593Smuzhiyun
942*4882a593Smuzhiyun if (!adev)
943*4882a593Smuzhiyun return 0;
944*4882a593Smuzhiyun
945*4882a593Smuzhiyun error = acpi_dev_pm_full_power(adev);
946*4882a593Smuzhiyun acpi_device_wakeup_disable(adev);
947*4882a593Smuzhiyun return error;
948*4882a593Smuzhiyun }
949*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(acpi_dev_resume);
950*4882a593Smuzhiyun
951*4882a593Smuzhiyun /**
952*4882a593Smuzhiyun * acpi_subsys_runtime_suspend - Suspend device using ACPI.
953*4882a593Smuzhiyun * @dev: Device to suspend.
954*4882a593Smuzhiyun *
955*4882a593Smuzhiyun * Carry out the generic runtime suspend procedure for @dev and use ACPI to put
956*4882a593Smuzhiyun * it into a runtime low-power state.
957*4882a593Smuzhiyun */
acpi_subsys_runtime_suspend(struct device * dev)958*4882a593Smuzhiyun int acpi_subsys_runtime_suspend(struct device *dev)
959*4882a593Smuzhiyun {
960*4882a593Smuzhiyun int ret = pm_generic_runtime_suspend(dev);
961*4882a593Smuzhiyun return ret ? ret : acpi_dev_suspend(dev, true);
962*4882a593Smuzhiyun }
963*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend);
964*4882a593Smuzhiyun
965*4882a593Smuzhiyun /**
966*4882a593Smuzhiyun * acpi_subsys_runtime_resume - Resume device using ACPI.
967*4882a593Smuzhiyun * @dev: Device to Resume.
968*4882a593Smuzhiyun *
969*4882a593Smuzhiyun * Use ACPI to put the given device into the full-power state and carry out the
970*4882a593Smuzhiyun * generic runtime resume procedure for it.
971*4882a593Smuzhiyun */
acpi_subsys_runtime_resume(struct device * dev)972*4882a593Smuzhiyun int acpi_subsys_runtime_resume(struct device *dev)
973*4882a593Smuzhiyun {
974*4882a593Smuzhiyun int ret = acpi_dev_resume(dev);
975*4882a593Smuzhiyun return ret ? ret : pm_generic_runtime_resume(dev);
976*4882a593Smuzhiyun }
977*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume);
978*4882a593Smuzhiyun
979*4882a593Smuzhiyun #ifdef CONFIG_PM_SLEEP
acpi_dev_needs_resume(struct device * dev,struct acpi_device * adev)980*4882a593Smuzhiyun static bool acpi_dev_needs_resume(struct device *dev, struct acpi_device *adev)
981*4882a593Smuzhiyun {
982*4882a593Smuzhiyun u32 sys_target = acpi_target_system_state();
983*4882a593Smuzhiyun int ret, state;
984*4882a593Smuzhiyun
985*4882a593Smuzhiyun if (!pm_runtime_suspended(dev) || !adev || (adev->wakeup.flags.valid &&
986*4882a593Smuzhiyun device_may_wakeup(dev) != !!adev->wakeup.prepare_count))
987*4882a593Smuzhiyun return true;
988*4882a593Smuzhiyun
989*4882a593Smuzhiyun if (sys_target == ACPI_STATE_S0)
990*4882a593Smuzhiyun return false;
991*4882a593Smuzhiyun
992*4882a593Smuzhiyun if (adev->power.flags.dsw_present)
993*4882a593Smuzhiyun return true;
994*4882a593Smuzhiyun
995*4882a593Smuzhiyun ret = acpi_dev_pm_get_state(dev, adev, sys_target, NULL, &state);
996*4882a593Smuzhiyun if (ret)
997*4882a593Smuzhiyun return true;
998*4882a593Smuzhiyun
999*4882a593Smuzhiyun return state != adev->power.state;
1000*4882a593Smuzhiyun }
1001*4882a593Smuzhiyun
1002*4882a593Smuzhiyun /**
1003*4882a593Smuzhiyun * acpi_subsys_prepare - Prepare device for system transition to a sleep state.
1004*4882a593Smuzhiyun * @dev: Device to prepare.
1005*4882a593Smuzhiyun */
acpi_subsys_prepare(struct device * dev)1006*4882a593Smuzhiyun int acpi_subsys_prepare(struct device *dev)
1007*4882a593Smuzhiyun {
1008*4882a593Smuzhiyun struct acpi_device *adev = ACPI_COMPANION(dev);
1009*4882a593Smuzhiyun
1010*4882a593Smuzhiyun if (dev->driver && dev->driver->pm && dev->driver->pm->prepare) {
1011*4882a593Smuzhiyun int ret = dev->driver->pm->prepare(dev);
1012*4882a593Smuzhiyun
1013*4882a593Smuzhiyun if (ret < 0)
1014*4882a593Smuzhiyun return ret;
1015*4882a593Smuzhiyun
1016*4882a593Smuzhiyun if (!ret && dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_PREPARE))
1017*4882a593Smuzhiyun return 0;
1018*4882a593Smuzhiyun }
1019*4882a593Smuzhiyun
1020*4882a593Smuzhiyun return !acpi_dev_needs_resume(dev, adev);
1021*4882a593Smuzhiyun }
1022*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(acpi_subsys_prepare);
1023*4882a593Smuzhiyun
1024*4882a593Smuzhiyun /**
1025*4882a593Smuzhiyun * acpi_subsys_complete - Finalize device's resume during system resume.
1026*4882a593Smuzhiyun * @dev: Device to handle.
1027*4882a593Smuzhiyun */
acpi_subsys_complete(struct device * dev)1028*4882a593Smuzhiyun void acpi_subsys_complete(struct device *dev)
1029*4882a593Smuzhiyun {
1030*4882a593Smuzhiyun pm_generic_complete(dev);
1031*4882a593Smuzhiyun /*
1032*4882a593Smuzhiyun * If the device had been runtime-suspended before the system went into
1033*4882a593Smuzhiyun * the sleep state it is going out of and it has never been resumed till
1034*4882a593Smuzhiyun * now, resume it in case the firmware powered it up.
1035*4882a593Smuzhiyun */
1036*4882a593Smuzhiyun if (pm_runtime_suspended(dev) && pm_resume_via_firmware())
1037*4882a593Smuzhiyun pm_request_resume(dev);
1038*4882a593Smuzhiyun }
1039*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(acpi_subsys_complete);
1040*4882a593Smuzhiyun
1041*4882a593Smuzhiyun /**
1042*4882a593Smuzhiyun * acpi_subsys_suspend - Run the device driver's suspend callback.
1043*4882a593Smuzhiyun * @dev: Device to handle.
1044*4882a593Smuzhiyun *
1045*4882a593Smuzhiyun * Follow PCI and resume devices from runtime suspend before running their
1046*4882a593Smuzhiyun * system suspend callbacks, unless the driver can cope with runtime-suspended
1047*4882a593Smuzhiyun * devices during system suspend and there are no ACPI-specific reasons for
1048*4882a593Smuzhiyun * resuming them.
1049*4882a593Smuzhiyun */
acpi_subsys_suspend(struct device * dev)1050*4882a593Smuzhiyun int acpi_subsys_suspend(struct device *dev)
1051*4882a593Smuzhiyun {
1052*4882a593Smuzhiyun if (!dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND) ||
1053*4882a593Smuzhiyun acpi_dev_needs_resume(dev, ACPI_COMPANION(dev)))
1054*4882a593Smuzhiyun pm_runtime_resume(dev);
1055*4882a593Smuzhiyun
1056*4882a593Smuzhiyun return pm_generic_suspend(dev);
1057*4882a593Smuzhiyun }
1058*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(acpi_subsys_suspend);
1059*4882a593Smuzhiyun
1060*4882a593Smuzhiyun /**
1061*4882a593Smuzhiyun * acpi_subsys_suspend_late - Suspend device using ACPI.
1062*4882a593Smuzhiyun * @dev: Device to suspend.
1063*4882a593Smuzhiyun *
1064*4882a593Smuzhiyun * Carry out the generic late suspend procedure for @dev and use ACPI to put
1065*4882a593Smuzhiyun * it into a low-power state during system transition into a sleep state.
1066*4882a593Smuzhiyun */
acpi_subsys_suspend_late(struct device * dev)1067*4882a593Smuzhiyun int acpi_subsys_suspend_late(struct device *dev)
1068*4882a593Smuzhiyun {
1069*4882a593Smuzhiyun int ret;
1070*4882a593Smuzhiyun
1071*4882a593Smuzhiyun if (dev_pm_skip_suspend(dev))
1072*4882a593Smuzhiyun return 0;
1073*4882a593Smuzhiyun
1074*4882a593Smuzhiyun ret = pm_generic_suspend_late(dev);
1075*4882a593Smuzhiyun return ret ? ret : acpi_dev_suspend(dev, device_may_wakeup(dev));
1076*4882a593Smuzhiyun }
1077*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late);
1078*4882a593Smuzhiyun
1079*4882a593Smuzhiyun /**
1080*4882a593Smuzhiyun * acpi_subsys_suspend_noirq - Run the device driver's "noirq" suspend callback.
1081*4882a593Smuzhiyun * @dev: Device to suspend.
1082*4882a593Smuzhiyun */
acpi_subsys_suspend_noirq(struct device * dev)1083*4882a593Smuzhiyun int acpi_subsys_suspend_noirq(struct device *dev)
1084*4882a593Smuzhiyun {
1085*4882a593Smuzhiyun int ret;
1086*4882a593Smuzhiyun
1087*4882a593Smuzhiyun if (dev_pm_skip_suspend(dev))
1088*4882a593Smuzhiyun return 0;
1089*4882a593Smuzhiyun
1090*4882a593Smuzhiyun ret = pm_generic_suspend_noirq(dev);
1091*4882a593Smuzhiyun if (ret)
1092*4882a593Smuzhiyun return ret;
1093*4882a593Smuzhiyun
1094*4882a593Smuzhiyun /*
1095*4882a593Smuzhiyun * If the target system sleep state is suspend-to-idle, it is sufficient
1096*4882a593Smuzhiyun * to check whether or not the device's wakeup settings are good for
1097*4882a593Smuzhiyun * runtime PM. Otherwise, the pm_resume_via_firmware() check will cause
1098*4882a593Smuzhiyun * acpi_subsys_complete() to take care of fixing up the device's state
1099*4882a593Smuzhiyun * anyway, if need be.
1100*4882a593Smuzhiyun */
1101*4882a593Smuzhiyun if (device_can_wakeup(dev) && !device_may_wakeup(dev))
1102*4882a593Smuzhiyun dev->power.may_skip_resume = false;
1103*4882a593Smuzhiyun
1104*4882a593Smuzhiyun return 0;
1105*4882a593Smuzhiyun }
1106*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(acpi_subsys_suspend_noirq);
1107*4882a593Smuzhiyun
1108*4882a593Smuzhiyun /**
1109*4882a593Smuzhiyun * acpi_subsys_resume_noirq - Run the device driver's "noirq" resume callback.
1110*4882a593Smuzhiyun * @dev: Device to handle.
1111*4882a593Smuzhiyun */
acpi_subsys_resume_noirq(struct device * dev)1112*4882a593Smuzhiyun static int acpi_subsys_resume_noirq(struct device *dev)
1113*4882a593Smuzhiyun {
1114*4882a593Smuzhiyun if (dev_pm_skip_resume(dev))
1115*4882a593Smuzhiyun return 0;
1116*4882a593Smuzhiyun
1117*4882a593Smuzhiyun return pm_generic_resume_noirq(dev);
1118*4882a593Smuzhiyun }
1119*4882a593Smuzhiyun
1120*4882a593Smuzhiyun /**
1121*4882a593Smuzhiyun * acpi_subsys_resume_early - Resume device using ACPI.
1122*4882a593Smuzhiyun * @dev: Device to Resume.
1123*4882a593Smuzhiyun *
1124*4882a593Smuzhiyun * Use ACPI to put the given device into the full-power state and carry out the
1125*4882a593Smuzhiyun * generic early resume procedure for it during system transition into the
1126*4882a593Smuzhiyun * working state.
1127*4882a593Smuzhiyun */
acpi_subsys_resume_early(struct device * dev)1128*4882a593Smuzhiyun static int acpi_subsys_resume_early(struct device *dev)
1129*4882a593Smuzhiyun {
1130*4882a593Smuzhiyun int ret;
1131*4882a593Smuzhiyun
1132*4882a593Smuzhiyun if (dev_pm_skip_resume(dev))
1133*4882a593Smuzhiyun return 0;
1134*4882a593Smuzhiyun
1135*4882a593Smuzhiyun ret = acpi_dev_resume(dev);
1136*4882a593Smuzhiyun return ret ? ret : pm_generic_resume_early(dev);
1137*4882a593Smuzhiyun }
1138*4882a593Smuzhiyun
1139*4882a593Smuzhiyun /**
1140*4882a593Smuzhiyun * acpi_subsys_freeze - Run the device driver's freeze callback.
1141*4882a593Smuzhiyun * @dev: Device to handle.
1142*4882a593Smuzhiyun */
acpi_subsys_freeze(struct device * dev)1143*4882a593Smuzhiyun int acpi_subsys_freeze(struct device *dev)
1144*4882a593Smuzhiyun {
1145*4882a593Smuzhiyun /*
1146*4882a593Smuzhiyun * Resume all runtime-suspended devices before creating a snapshot
1147*4882a593Smuzhiyun * image of system memory, because the restore kernel generally cannot
1148*4882a593Smuzhiyun * be expected to always handle them consistently and they need to be
1149*4882a593Smuzhiyun * put into the runtime-active metastate during system resume anyway,
1150*4882a593Smuzhiyun * so it is better to ensure that the state saved in the image will be
1151*4882a593Smuzhiyun * always consistent with that.
1152*4882a593Smuzhiyun */
1153*4882a593Smuzhiyun pm_runtime_resume(dev);
1154*4882a593Smuzhiyun
1155*4882a593Smuzhiyun return pm_generic_freeze(dev);
1156*4882a593Smuzhiyun }
1157*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(acpi_subsys_freeze);
1158*4882a593Smuzhiyun
1159*4882a593Smuzhiyun /**
1160*4882a593Smuzhiyun * acpi_subsys_restore_early - Restore device using ACPI.
1161*4882a593Smuzhiyun * @dev: Device to restore.
1162*4882a593Smuzhiyun */
acpi_subsys_restore_early(struct device * dev)1163*4882a593Smuzhiyun int acpi_subsys_restore_early(struct device *dev)
1164*4882a593Smuzhiyun {
1165*4882a593Smuzhiyun int ret = acpi_dev_resume(dev);
1166*4882a593Smuzhiyun return ret ? ret : pm_generic_restore_early(dev);
1167*4882a593Smuzhiyun }
1168*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(acpi_subsys_restore_early);
1169*4882a593Smuzhiyun
1170*4882a593Smuzhiyun /**
1171*4882a593Smuzhiyun * acpi_subsys_poweroff - Run the device driver's poweroff callback.
1172*4882a593Smuzhiyun * @dev: Device to handle.
1173*4882a593Smuzhiyun *
1174*4882a593Smuzhiyun * Follow PCI and resume devices from runtime suspend before running their
1175*4882a593Smuzhiyun * system poweroff callbacks, unless the driver can cope with runtime-suspended
1176*4882a593Smuzhiyun * devices during system suspend and there are no ACPI-specific reasons for
1177*4882a593Smuzhiyun * resuming them.
1178*4882a593Smuzhiyun */
acpi_subsys_poweroff(struct device * dev)1179*4882a593Smuzhiyun int acpi_subsys_poweroff(struct device *dev)
1180*4882a593Smuzhiyun {
1181*4882a593Smuzhiyun if (!dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND) ||
1182*4882a593Smuzhiyun acpi_dev_needs_resume(dev, ACPI_COMPANION(dev)))
1183*4882a593Smuzhiyun pm_runtime_resume(dev);
1184*4882a593Smuzhiyun
1185*4882a593Smuzhiyun return pm_generic_poweroff(dev);
1186*4882a593Smuzhiyun }
1187*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(acpi_subsys_poweroff);
1188*4882a593Smuzhiyun
1189*4882a593Smuzhiyun /**
1190*4882a593Smuzhiyun * acpi_subsys_poweroff_late - Run the device driver's poweroff callback.
1191*4882a593Smuzhiyun * @dev: Device to handle.
1192*4882a593Smuzhiyun *
1193*4882a593Smuzhiyun * Carry out the generic late poweroff procedure for @dev and use ACPI to put
1194*4882a593Smuzhiyun * it into a low-power state during system transition into a sleep state.
1195*4882a593Smuzhiyun */
acpi_subsys_poweroff_late(struct device * dev)1196*4882a593Smuzhiyun static int acpi_subsys_poweroff_late(struct device *dev)
1197*4882a593Smuzhiyun {
1198*4882a593Smuzhiyun int ret;
1199*4882a593Smuzhiyun
1200*4882a593Smuzhiyun if (dev_pm_skip_suspend(dev))
1201*4882a593Smuzhiyun return 0;
1202*4882a593Smuzhiyun
1203*4882a593Smuzhiyun ret = pm_generic_poweroff_late(dev);
1204*4882a593Smuzhiyun if (ret)
1205*4882a593Smuzhiyun return ret;
1206*4882a593Smuzhiyun
1207*4882a593Smuzhiyun return acpi_dev_suspend(dev, device_may_wakeup(dev));
1208*4882a593Smuzhiyun }
1209*4882a593Smuzhiyun
1210*4882a593Smuzhiyun /**
1211*4882a593Smuzhiyun * acpi_subsys_poweroff_noirq - Run the driver's "noirq" poweroff callback.
1212*4882a593Smuzhiyun * @dev: Device to suspend.
1213*4882a593Smuzhiyun */
acpi_subsys_poweroff_noirq(struct device * dev)1214*4882a593Smuzhiyun static int acpi_subsys_poweroff_noirq(struct device *dev)
1215*4882a593Smuzhiyun {
1216*4882a593Smuzhiyun if (dev_pm_skip_suspend(dev))
1217*4882a593Smuzhiyun return 0;
1218*4882a593Smuzhiyun
1219*4882a593Smuzhiyun return pm_generic_poweroff_noirq(dev);
1220*4882a593Smuzhiyun }
1221*4882a593Smuzhiyun #endif /* CONFIG_PM_SLEEP */
1222*4882a593Smuzhiyun
1223*4882a593Smuzhiyun static struct dev_pm_domain acpi_general_pm_domain = {
1224*4882a593Smuzhiyun .ops = {
1225*4882a593Smuzhiyun .runtime_suspend = acpi_subsys_runtime_suspend,
1226*4882a593Smuzhiyun .runtime_resume = acpi_subsys_runtime_resume,
1227*4882a593Smuzhiyun #ifdef CONFIG_PM_SLEEP
1228*4882a593Smuzhiyun .prepare = acpi_subsys_prepare,
1229*4882a593Smuzhiyun .complete = acpi_subsys_complete,
1230*4882a593Smuzhiyun .suspend = acpi_subsys_suspend,
1231*4882a593Smuzhiyun .suspend_late = acpi_subsys_suspend_late,
1232*4882a593Smuzhiyun .suspend_noirq = acpi_subsys_suspend_noirq,
1233*4882a593Smuzhiyun .resume_noirq = acpi_subsys_resume_noirq,
1234*4882a593Smuzhiyun .resume_early = acpi_subsys_resume_early,
1235*4882a593Smuzhiyun .freeze = acpi_subsys_freeze,
1236*4882a593Smuzhiyun .poweroff = acpi_subsys_poweroff,
1237*4882a593Smuzhiyun .poweroff_late = acpi_subsys_poweroff_late,
1238*4882a593Smuzhiyun .poweroff_noirq = acpi_subsys_poweroff_noirq,
1239*4882a593Smuzhiyun .restore_early = acpi_subsys_restore_early,
1240*4882a593Smuzhiyun #endif
1241*4882a593Smuzhiyun },
1242*4882a593Smuzhiyun };
1243*4882a593Smuzhiyun
1244*4882a593Smuzhiyun /**
1245*4882a593Smuzhiyun * acpi_dev_pm_detach - Remove ACPI power management from the device.
1246*4882a593Smuzhiyun * @dev: Device to take care of.
1247*4882a593Smuzhiyun * @power_off: Whether or not to try to remove power from the device.
1248*4882a593Smuzhiyun *
1249*4882a593Smuzhiyun * Remove the device from the general ACPI PM domain and remove its wakeup
1250*4882a593Smuzhiyun * notifier. If @power_off is set, additionally remove power from the device if
1251*4882a593Smuzhiyun * possible.
1252*4882a593Smuzhiyun *
1253*4882a593Smuzhiyun * Callers must ensure proper synchronization of this function with power
1254*4882a593Smuzhiyun * management callbacks.
1255*4882a593Smuzhiyun */
acpi_dev_pm_detach(struct device * dev,bool power_off)1256*4882a593Smuzhiyun static void acpi_dev_pm_detach(struct device *dev, bool power_off)
1257*4882a593Smuzhiyun {
1258*4882a593Smuzhiyun struct acpi_device *adev = ACPI_COMPANION(dev);
1259*4882a593Smuzhiyun
1260*4882a593Smuzhiyun if (adev && dev->pm_domain == &acpi_general_pm_domain) {
1261*4882a593Smuzhiyun dev_pm_domain_set(dev, NULL);
1262*4882a593Smuzhiyun acpi_remove_pm_notifier(adev);
1263*4882a593Smuzhiyun if (power_off) {
1264*4882a593Smuzhiyun /*
1265*4882a593Smuzhiyun * If the device's PM QoS resume latency limit or flags
1266*4882a593Smuzhiyun * have been exposed to user space, they have to be
1267*4882a593Smuzhiyun * hidden at this point, so that they don't affect the
1268*4882a593Smuzhiyun * choice of the low-power state to put the device into.
1269*4882a593Smuzhiyun */
1270*4882a593Smuzhiyun dev_pm_qos_hide_latency_limit(dev);
1271*4882a593Smuzhiyun dev_pm_qos_hide_flags(dev);
1272*4882a593Smuzhiyun acpi_device_wakeup_disable(adev);
1273*4882a593Smuzhiyun acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
1274*4882a593Smuzhiyun }
1275*4882a593Smuzhiyun }
1276*4882a593Smuzhiyun }
1277*4882a593Smuzhiyun
1278*4882a593Smuzhiyun /**
1279*4882a593Smuzhiyun * acpi_dev_pm_attach - Prepare device for ACPI power management.
1280*4882a593Smuzhiyun * @dev: Device to prepare.
1281*4882a593Smuzhiyun * @power_on: Whether or not to power on the device.
1282*4882a593Smuzhiyun *
1283*4882a593Smuzhiyun * If @dev has a valid ACPI handle that has a valid struct acpi_device object
1284*4882a593Smuzhiyun * attached to it, install a wakeup notification handler for the device and
1285*4882a593Smuzhiyun * add it to the general ACPI PM domain. If @power_on is set, the device will
1286*4882a593Smuzhiyun * be put into the ACPI D0 state before the function returns.
1287*4882a593Smuzhiyun *
1288*4882a593Smuzhiyun * This assumes that the @dev's bus type uses generic power management callbacks
1289*4882a593Smuzhiyun * (or doesn't use any power management callbacks at all).
1290*4882a593Smuzhiyun *
1291*4882a593Smuzhiyun * Callers must ensure proper synchronization of this function with power
1292*4882a593Smuzhiyun * management callbacks.
1293*4882a593Smuzhiyun */
acpi_dev_pm_attach(struct device * dev,bool power_on)1294*4882a593Smuzhiyun int acpi_dev_pm_attach(struct device *dev, bool power_on)
1295*4882a593Smuzhiyun {
1296*4882a593Smuzhiyun /*
1297*4882a593Smuzhiyun * Skip devices whose ACPI companions match the device IDs below,
1298*4882a593Smuzhiyun * because they require special power management handling incompatible
1299*4882a593Smuzhiyun * with the generic ACPI PM domain.
1300*4882a593Smuzhiyun */
1301*4882a593Smuzhiyun static const struct acpi_device_id special_pm_ids[] = {
1302*4882a593Smuzhiyun ACPI_FAN_DEVICE_IDS,
1303*4882a593Smuzhiyun {}
1304*4882a593Smuzhiyun };
1305*4882a593Smuzhiyun struct acpi_device *adev = ACPI_COMPANION(dev);
1306*4882a593Smuzhiyun
1307*4882a593Smuzhiyun if (!adev || !acpi_match_device_ids(adev, special_pm_ids))
1308*4882a593Smuzhiyun return 0;
1309*4882a593Smuzhiyun
1310*4882a593Smuzhiyun /*
1311*4882a593Smuzhiyun * Only attach the power domain to the first device if the
1312*4882a593Smuzhiyun * companion is shared by multiple. This is to prevent doing power
1313*4882a593Smuzhiyun * management twice.
1314*4882a593Smuzhiyun */
1315*4882a593Smuzhiyun if (!acpi_device_is_first_physical_node(adev, dev))
1316*4882a593Smuzhiyun return 0;
1317*4882a593Smuzhiyun
1318*4882a593Smuzhiyun acpi_add_pm_notifier(adev, dev, acpi_pm_notify_work_func);
1319*4882a593Smuzhiyun dev_pm_domain_set(dev, &acpi_general_pm_domain);
1320*4882a593Smuzhiyun if (power_on) {
1321*4882a593Smuzhiyun acpi_dev_pm_full_power(adev);
1322*4882a593Smuzhiyun acpi_device_wakeup_disable(adev);
1323*4882a593Smuzhiyun }
1324*4882a593Smuzhiyun
1325*4882a593Smuzhiyun dev->pm_domain->detach = acpi_dev_pm_detach;
1326*4882a593Smuzhiyun return 1;
1327*4882a593Smuzhiyun }
1328*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(acpi_dev_pm_attach);
1329*4882a593Smuzhiyun #endif /* CONFIG_PM */
1330