blob: 8234e1f8c79d49fe32d7fc07c9ef2bed8567117a [file] [log] [blame]
Rafael J. Wysockiec2cd812012-11-02 01:40:09 +01001/*
2 * drivers/acpi/device_pm.c - ACPI device power management routines.
3 *
4 * Copyright (C) 2012, Intel Corp.
5 * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
6 *
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as published
11 * by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 *
22 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23 */
24
25#include <linux/device.h>
Rafael J. Wysocki86b38322012-11-02 01:40:18 +010026#include <linux/export.h>
Rafael J. Wysockiec2cd812012-11-02 01:40:09 +010027#include <linux/mutex.h>
Rafael J. Wysocki86b38322012-11-02 01:40:18 +010028#include <linux/pm_qos.h>
Rafael J. Wysockicd7bd022012-11-02 01:40:28 +010029#include <linux/pm_runtime.h>
Rafael J. Wysockiec2cd812012-11-02 01:40:09 +010030
31#include <acpi/acpi.h>
32#include <acpi/acpi_bus.h>
Rafael J. Wysocki9ce4e602013-01-17 14:11:08 +010033#include <acpi/acpi_drivers.h>
34
35#include "internal.h"
36
37#define _COMPONENT ACPI_POWER_COMPONENT
38ACPI_MODULE_NAME("device_pm");
Rafael J. Wysockiec2cd812012-11-02 01:40:09 +010039
Rafael J. Wysocki86b38322012-11-02 01:40:18 +010040/**
Rafael J. Wysocki9ce4e602013-01-17 14:11:08 +010041 * acpi_power_state_string - String representation of ACPI device power state.
42 * @state: ACPI device power state to return the string representation of.
43 */
44const char *acpi_power_state_string(int state)
45{
46 switch (state) {
47 case ACPI_STATE_D0:
48 return "D0";
49 case ACPI_STATE_D1:
50 return "D1";
51 case ACPI_STATE_D2:
52 return "D2";
53 case ACPI_STATE_D3_HOT:
54 return "D3hot";
55 case ACPI_STATE_D3_COLD:
Rafael J. Wysocki898fee42013-01-22 12:56:26 +010056 return "D3cold";
Rafael J. Wysocki9ce4e602013-01-17 14:11:08 +010057 default:
58 return "(unknown)";
59 }
60}
61
62/**
63 * acpi_device_get_power - Get power state of an ACPI device.
64 * @device: Device to get the power state of.
65 * @state: Place to store the power state of the device.
66 *
67 * This function does not update the device's power.state field, but it may
68 * update its parent's power.state field (when the parent's power state is
69 * unknown and the device's power state turns out to be D0).
70 */
71int acpi_device_get_power(struct acpi_device *device, int *state)
72{
73 int result = ACPI_STATE_UNKNOWN;
74
75 if (!device || !state)
76 return -EINVAL;
77
78 if (!device->flags.power_manageable) {
79 /* TBD: Non-recursive algorithm for walking up hierarchy. */
80 *state = device->parent ?
81 device->parent->power.state : ACPI_STATE_D0;
82 goto out;
83 }
84
85 /*
Rafael J. Wysocki75eb2d12013-03-24 22:54:40 +010086 * Get the device's power state from power resources settings and _PSC,
87 * if available.
Rafael J. Wysocki9ce4e602013-01-17 14:11:08 +010088 */
Rafael J. Wysocki75eb2d12013-03-24 22:54:40 +010089 if (device->power.flags.power_resources) {
Rafael J. Wysocki9ce4e602013-01-17 14:11:08 +010090 int error = acpi_power_get_inferred_state(device, &result);
91 if (error)
92 return error;
Rafael J. Wysocki75eb2d12013-03-24 22:54:40 +010093 }
94 if (device->power.flags.explicit_get) {
95 acpi_handle handle = device->handle;
96 unsigned long long psc;
97 acpi_status status;
98
99 status = acpi_evaluate_integer(handle, "_PSC", NULL, &psc);
100 if (ACPI_FAILURE(status))
101 return -ENODEV;
102
103 /*
104 * The power resources settings may indicate a power state
105 * shallower than the actual power state of the device.
106 *
107 * Moreover, on systems predating ACPI 4.0, if the device
108 * doesn't depend on any power resources and _PSC returns 3,
109 * that means "power off". We need to maintain compatibility
110 * with those systems.
111 */
112 if (psc > result && psc < ACPI_STATE_D3_COLD)
113 result = psc;
114 else if (result == ACPI_STATE_UNKNOWN)
115 result = psc > ACPI_STATE_D2 ? ACPI_STATE_D3_COLD : psc;
Rafael J. Wysocki9ce4e602013-01-17 14:11:08 +0100116 }
117
118 /*
119 * If we were unsure about the device parent's power state up to this
120 * point, the fact that the device is in D0 implies that the parent has
121 * to be in D0 too.
122 */
123 if (device->parent && device->parent->power.state == ACPI_STATE_UNKNOWN
124 && result == ACPI_STATE_D0)
125 device->parent->power.state = ACPI_STATE_D0;
126
127 *state = result;
128
129 out:
130 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] power state is %s\n",
131 device->pnp.bus_id, acpi_power_state_string(*state)));
132
133 return 0;
134}
135
Rafael J. Wysocki9c0f45e2013-01-22 12:55:52 +0100136static int acpi_dev_pm_explicit_set(struct acpi_device *adev, int state)
137{
138 if (adev->power.states[state].flags.explicit_set) {
139 char method[5] = { '_', 'P', 'S', '0' + state, '\0' };
140 acpi_status status;
141
142 status = acpi_evaluate_object(adev->handle, method, NULL, NULL);
143 if (ACPI_FAILURE(status))
144 return -ENODEV;
145 }
146 return 0;
147}
148
Rafael J. Wysocki9ce4e602013-01-17 14:11:08 +0100149/**
150 * acpi_device_set_power - Set power state of an ACPI device.
151 * @device: Device to set the power state of.
152 * @state: New power state to set.
153 *
154 * Callers must ensure that the device is power manageable before using this
155 * function.
156 */
157int acpi_device_set_power(struct acpi_device *device, int state)
158{
159 int result = 0;
Rafael J. Wysocki9ce4e602013-01-17 14:11:08 +0100160 bool cut_power = false;
161
Rafael J. Wysocki2c7d1322013-07-30 14:34:00 +0200162 if (!device || !device->flags.power_manageable
163 || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
Rafael J. Wysocki9ce4e602013-01-17 14:11:08 +0100164 return -EINVAL;
165
166 /* Make sure this is a valid target state */
167
168 if (state == device->power.state) {
Rafael J. Wysockib69137a2013-07-30 14:34:55 +0200169 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] already in %s\n",
170 device->pnp.bus_id,
Rafael J. Wysocki9ce4e602013-01-17 14:11:08 +0100171 acpi_power_state_string(state)));
172 return 0;
173 }
174
175 if (!device->power.states[state].flags.valid) {
Rafael J. Wysockib69137a2013-07-30 14:34:55 +0200176 dev_warn(&device->dev, "Power state %s not supported\n",
177 acpi_power_state_string(state));
Rafael J. Wysocki9ce4e602013-01-17 14:11:08 +0100178 return -ENODEV;
179 }
180 if (device->parent && (state < device->parent->power.state)) {
Rafael J. Wysockib69137a2013-07-30 14:34:55 +0200181 dev_warn(&device->dev,
182 "Cannot transition to a higher-powered state than parent\n");
Rafael J. Wysocki9ce4e602013-01-17 14:11:08 +0100183 return -ENODEV;
184 }
185
186 /* For D3cold we should first transition into D3hot. */
187 if (state == ACPI_STATE_D3_COLD
188 && device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible) {
189 state = ACPI_STATE_D3_HOT;
Rafael J. Wysocki9ce4e602013-01-17 14:11:08 +0100190 cut_power = true;
191 }
192
Rafael J. Wysockie78adb72013-01-22 12:56:04 +0100193 if (state < device->power.state && state != ACPI_STATE_D0
194 && device->power.state >= ACPI_STATE_D3_HOT) {
Rafael J. Wysockib69137a2013-07-30 14:34:55 +0200195 dev_warn(&device->dev,
196 "Cannot transition to non-D0 state from D3\n");
Rafael J. Wysockie78adb72013-01-22 12:56:04 +0100197 return -ENODEV;
198 }
199
Rafael J. Wysocki9ce4e602013-01-17 14:11:08 +0100200 /*
201 * Transition Power
202 * ----------------
Rafael J. Wysockie78adb72013-01-22 12:56:04 +0100203 * In accordance with the ACPI specification first apply power (via
204 * power resources) and then evalute _PSx.
Rafael J. Wysocki9ce4e602013-01-17 14:11:08 +0100205 */
Rafael J. Wysockie78adb72013-01-22 12:56:04 +0100206 if (device->power.flags.power_resources) {
207 result = acpi_power_transition(device, state);
Rafael J. Wysocki9c0f45e2013-01-22 12:55:52 +0100208 if (result)
209 goto end;
Rafael J. Wysocki9ce4e602013-01-17 14:11:08 +0100210 }
Rafael J. Wysockie78adb72013-01-22 12:56:04 +0100211 result = acpi_dev_pm_explicit_set(device, state);
212 if (result)
213 goto end;
Rafael J. Wysocki9ce4e602013-01-17 14:11:08 +0100214
Rafael J. Wysockie5656272013-01-22 12:56:35 +0100215 if (cut_power) {
216 device->power.state = state;
217 state = ACPI_STATE_D3_COLD;
218 result = acpi_power_transition(device, state);
219 }
Rafael J. Wysocki9ce4e602013-01-17 14:11:08 +0100220
Rafael J. Wysockie78adb72013-01-22 12:56:04 +0100221 end:
222 if (result) {
Rafael J. Wysockib69137a2013-07-30 14:34:55 +0200223 dev_warn(&device->dev, "Failed to change power state to %s\n",
224 acpi_power_state_string(state));
Rafael J. Wysockie78adb72013-01-22 12:56:04 +0100225 } else {
Rafael J. Wysocki9ce4e602013-01-17 14:11:08 +0100226 device->power.state = state;
227 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
228 "Device [%s] transitioned to %s\n",
229 device->pnp.bus_id,
230 acpi_power_state_string(state)));
231 }
232
233 return result;
234}
235EXPORT_SYMBOL(acpi_device_set_power);
236
237int acpi_bus_set_power(acpi_handle handle, int state)
238{
239 struct acpi_device *device;
240 int result;
241
242 result = acpi_bus_get_device(handle, &device);
243 if (result)
244 return result;
245
246 if (!device->flags.power_manageable) {
247 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
248 "Device [%s] is not power manageable\n",
249 dev_name(&device->dev)));
250 return -ENODEV;
251 }
252
253 return acpi_device_set_power(device, state);
254}
255EXPORT_SYMBOL(acpi_bus_set_power);
256
257int acpi_bus_init_power(struct acpi_device *device)
258{
259 int state;
260 int result;
261
262 if (!device)
263 return -EINVAL;
264
265 device->power.state = ACPI_STATE_UNKNOWN;
266
267 result = acpi_device_get_power(device, &state);
268 if (result)
269 return result;
270
Rafael J. Wysockia2367802013-01-22 12:54:38 +0100271 if (state < ACPI_STATE_D3_COLD && device->power.flags.power_resources) {
Rafael J. Wysocki9ce4e602013-01-17 14:11:08 +0100272 result = acpi_power_on_resources(device, state);
Rafael J. Wysockia2367802013-01-22 12:54:38 +0100273 if (result)
274 return result;
Rafael J. Wysocki9ce4e602013-01-17 14:11:08 +0100275
Rafael J. Wysocki9c0f45e2013-01-22 12:55:52 +0100276 result = acpi_dev_pm_explicit_set(device, state);
277 if (result)
278 return result;
Rafael J. Wysockib3785492013-02-01 23:43:02 +0100279 } else if (state == ACPI_STATE_UNKNOWN) {
Rafael J. Wysocki7cd84072013-06-05 14:01:19 +0200280 /*
281 * No power resources and missing _PSC? Cross fingers and make
282 * it D0 in hope that this is what the BIOS put the device into.
283 * [We tried to force D0 here by executing _PS0, but that broke
284 * Toshiba P870-303 in a nasty way.]
285 */
Rafael J. Wysockib3785492013-02-01 23:43:02 +0100286 state = ACPI_STATE_D0;
Rafael J. Wysockia2367802013-01-22 12:54:38 +0100287 }
288 device->power.state = state;
289 return 0;
Rafael J. Wysocki9ce4e602013-01-17 14:11:08 +0100290}
291
Rafael J. Wysockib9e95fc2013-06-19 00:45:34 +0200292/**
293 * acpi_device_fix_up_power - Force device with missing _PSC into D0.
294 * @device: Device object whose power state is to be fixed up.
295 *
296 * Devices without power resources and _PSC, but having _PS0 and _PS3 defined,
297 * are assumed to be put into D0 by the BIOS. However, in some cases that may
298 * not be the case and this function should be used then.
299 */
300int acpi_device_fix_up_power(struct acpi_device *device)
301{
302 int ret = 0;
303
304 if (!device->power.flags.power_resources
305 && !device->power.flags.explicit_get
306 && device->power.state == ACPI_STATE_D0)
307 ret = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0);
308
309 return ret;
310}
311
Rafael J. Wysocki9ce4e602013-01-17 14:11:08 +0100312int acpi_bus_update_power(acpi_handle handle, int *state_p)
313{
314 struct acpi_device *device;
315 int state;
316 int result;
317
318 result = acpi_bus_get_device(handle, &device);
319 if (result)
320 return result;
321
322 result = acpi_device_get_power(device, &state);
323 if (result)
324 return result;
325
Rafael J. Wysocki91bdad02013-07-04 13:22:11 +0200326 if (state == ACPI_STATE_UNKNOWN) {
Rafael J. Wysocki511d5c42013-02-03 14:57:32 +0100327 state = ACPI_STATE_D0;
Rafael J. Wysocki91bdad02013-07-04 13:22:11 +0200328 result = acpi_device_set_power(device, state);
329 if (result)
330 return result;
331 } else {
332 if (device->power.flags.power_resources) {
333 /*
334 * We don't need to really switch the state, bu we need
335 * to update the power resources' reference counters.
336 */
337 result = acpi_power_transition(device, state);
338 if (result)
339 return result;
340 }
341 device->power.state = state;
342 }
343 if (state_p)
Rafael J. Wysocki9ce4e602013-01-17 14:11:08 +0100344 *state_p = state;
345
Rafael J. Wysocki91bdad02013-07-04 13:22:11 +0200346 return 0;
Rafael J. Wysocki9ce4e602013-01-17 14:11:08 +0100347}
348EXPORT_SYMBOL_GPL(acpi_bus_update_power);
349
350bool acpi_bus_power_manageable(acpi_handle handle)
351{
352 struct acpi_device *device;
353 int result;
354
355 result = acpi_bus_get_device(handle, &device);
356 return result ? false : device->flags.power_manageable;
357}
358EXPORT_SYMBOL(acpi_bus_power_manageable);
359
Rafael J. Wysockiec4602a2013-05-16 22:29:28 +0200360#ifdef CONFIG_PM
361static DEFINE_MUTEX(acpi_pm_notifier_lock);
362
363/**
364 * acpi_add_pm_notifier - Register PM notifier for given ACPI device.
365 * @adev: ACPI device to add the notifier for.
366 * @context: Context information to pass to the notifier routine.
367 *
368 * NOTE: @adev need not be a run-wake or wakeup device to be a valid source of
369 * PM wakeup events. For example, wakeup events may be generated for bridges
370 * if one of the devices below the bridge is signaling wakeup, even if the
371 * bridge itself doesn't have a wakeup GPE associated with it.
372 */
373acpi_status acpi_add_pm_notifier(struct acpi_device *adev,
374 acpi_notify_handler handler, void *context)
375{
376 acpi_status status = AE_ALREADY_EXISTS;
377
378 mutex_lock(&acpi_pm_notifier_lock);
379
380 if (adev->wakeup.flags.notifier_present)
381 goto out;
382
383 status = acpi_install_notify_handler(adev->handle,
384 ACPI_SYSTEM_NOTIFY,
385 handler, context);
386 if (ACPI_FAILURE(status))
387 goto out;
388
389 adev->wakeup.flags.notifier_present = true;
390
391 out:
392 mutex_unlock(&acpi_pm_notifier_lock);
393 return status;
394}
395
396/**
397 * acpi_remove_pm_notifier - Unregister PM notifier from given ACPI device.
398 * @adev: ACPI device to remove the notifier from.
399 */
400acpi_status acpi_remove_pm_notifier(struct acpi_device *adev,
401 acpi_notify_handler handler)
402{
403 acpi_status status = AE_BAD_PARAMETER;
404
405 mutex_lock(&acpi_pm_notifier_lock);
406
407 if (!adev->wakeup.flags.notifier_present)
408 goto out;
409
410 status = acpi_remove_notify_handler(adev->handle,
411 ACPI_SYSTEM_NOTIFY,
412 handler);
413 if (ACPI_FAILURE(status))
414 goto out;
415
416 adev->wakeup.flags.notifier_present = false;
417
418 out:
419 mutex_unlock(&acpi_pm_notifier_lock);
420 return status;
421}
422
Rafael J. Wysocki9ce4e602013-01-17 14:11:08 +0100423bool acpi_bus_can_wakeup(acpi_handle handle)
424{
425 struct acpi_device *device;
426 int result;
427
428 result = acpi_bus_get_device(handle, &device);
429 return result ? false : device->wakeup.flags.valid;
430}
431EXPORT_SYMBOL(acpi_bus_can_wakeup);
432
433/**
Rafael J. Wysockib25c77e2013-06-16 00:37:42 +0200434 * acpi_dev_pm_get_state - Get preferred power state of ACPI device.
Rafael J. Wysocki86b38322012-11-02 01:40:18 +0100435 * @dev: Device whose preferred target power state to return.
436 * @adev: ACPI device node corresponding to @dev.
437 * @target_state: System state to match the resultant device state.
Rafael J. Wysockifa1675b2013-06-16 00:37:59 +0200438 * @d_min_p: Location to store the highest power state available to the device.
439 * @d_max_p: Location to store the lowest power state available to the device.
Rafael J. Wysocki86b38322012-11-02 01:40:18 +0100440 *
Rafael J. Wysockifa1675b2013-06-16 00:37:59 +0200441 * Find the lowest power (highest number) and highest power (lowest number) ACPI
442 * device power states that the device can be in while the system is in the
443 * state represented by @target_state. Store the integer numbers representing
444 * those stats in the memory locations pointed to by @d_max_p and @d_min_p,
445 * respectively.
Rafael J. Wysocki86b38322012-11-02 01:40:18 +0100446 *
447 * Callers must ensure that @dev and @adev are valid pointers and that @adev
448 * actually corresponds to @dev before using this function.
Rafael J. Wysockifa1675b2013-06-16 00:37:59 +0200449 *
450 * Returns 0 on success or -ENODATA when one of the ACPI methods fails or
451 * returns a value that doesn't make sense. The memory locations pointed to by
452 * @d_max_p and @d_min_p are only modified on success.
Rafael J. Wysocki86b38322012-11-02 01:40:18 +0100453 */
Rafael J. Wysockib25c77e2013-06-16 00:37:42 +0200454static int acpi_dev_pm_get_state(struct device *dev, struct acpi_device *adev,
Rafael J. Wysockifa1675b2013-06-16 00:37:59 +0200455 u32 target_state, int *d_min_p, int *d_max_p)
Rafael J. Wysocki86b38322012-11-02 01:40:18 +0100456{
Rafael J. Wysockifa1675b2013-06-16 00:37:59 +0200457 char method[] = { '_', 'S', '0' + target_state, 'D', '\0' };
458 acpi_handle handle = adev->handle;
459 unsigned long long ret;
460 int d_min, d_max;
Rafael J. Wysocki86b38322012-11-02 01:40:18 +0100461 bool wakeup = false;
Rafael J. Wysockifa1675b2013-06-16 00:37:59 +0200462 acpi_status status;
Rafael J. Wysocki86b38322012-11-02 01:40:18 +0100463
Rafael J. Wysocki86b38322012-11-02 01:40:18 +0100464 /*
Rafael J. Wysockifa1675b2013-06-16 00:37:59 +0200465 * If the system state is S0, the lowest power state the device can be
466 * in is D3cold, unless the device has _S0W and is supposed to signal
467 * wakeup, in which case the return value of _S0W has to be used as the
468 * lowest power state available to the device.
Rafael J. Wysocki86b38322012-11-02 01:40:18 +0100469 */
470 d_min = ACPI_STATE_D0;
Rafael J. Wysocki4c164ae2013-06-16 00:37:50 +0200471 d_max = ACPI_STATE_D3_COLD;
Rafael J. Wysocki86b38322012-11-02 01:40:18 +0100472
473 /*
474 * If present, _SxD methods return the minimum D-state (highest power
475 * state) we can use for the corresponding S-states. Otherwise, the
476 * minimum D-state is D0 (ACPI 3.x).
Rafael J. Wysocki86b38322012-11-02 01:40:18 +0100477 */
478 if (target_state > ACPI_STATE_S0) {
Rafael J. Wysockifa1675b2013-06-16 00:37:59 +0200479 /*
480 * We rely on acpi_evaluate_integer() not clobbering the integer
481 * provided if AE_NOT_FOUND is returned.
482 */
483 ret = d_min;
484 status = acpi_evaluate_integer(handle, method, NULL, &ret);
485 if ((ACPI_FAILURE(status) && status != AE_NOT_FOUND)
486 || ret > ACPI_STATE_D3_COLD)
487 return -ENODATA;
488
489 /*
490 * We need to handle legacy systems where D3hot and D3cold are
491 * the same and 3 is returned in both cases, so fall back to
492 * D3cold if D3hot is not a valid state.
493 */
494 if (!adev->power.states[ret].flags.valid) {
495 if (ret == ACPI_STATE_D3_HOT)
496 ret = ACPI_STATE_D3_COLD;
497 else
498 return -ENODATA;
499 }
500 d_min = ret;
Rafael J. Wysocki86b38322012-11-02 01:40:18 +0100501 wakeup = device_may_wakeup(dev) && adev->wakeup.flags.valid
502 && adev->wakeup.sleep_state >= target_state;
503 } else if (dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) !=
504 PM_QOS_FLAGS_NONE) {
505 wakeup = adev->wakeup.flags.valid;
506 }
507
508 /*
509 * If _PRW says we can wake up the system from the target sleep state,
510 * the D-state returned by _SxD is sufficient for that (we assume a
511 * wakeup-aware driver if wake is set). Still, if _SxW exists
512 * (ACPI 3.x), it should return the maximum (lowest power) D-state that
513 * can wake the system. _S0W may be valid, too.
514 */
515 if (wakeup) {
Rafael J. Wysockifa1675b2013-06-16 00:37:59 +0200516 method[3] = 'W';
517 status = acpi_evaluate_integer(handle, method, NULL, &ret);
518 if (status == AE_NOT_FOUND) {
519 if (target_state > ACPI_STATE_S0)
Rafael J. Wysocki86b38322012-11-02 01:40:18 +0100520 d_max = d_min;
Rafael J. Wysockifa1675b2013-06-16 00:37:59 +0200521 } else if (ACPI_SUCCESS(status) && ret <= ACPI_STATE_D3_COLD) {
522 /* Fall back to D3cold if ret is not a valid state. */
523 if (!adev->power.states[ret].flags.valid)
524 ret = ACPI_STATE_D3_COLD;
525
526 d_max = ret > d_min ? ret : d_min;
527 } else {
528 return -ENODATA;
Rafael J. Wysocki86b38322012-11-02 01:40:18 +0100529 }
530 }
531
Rafael J. Wysocki86b38322012-11-02 01:40:18 +0100532 if (d_min_p)
533 *d_min_p = d_min;
Rafael J. Wysockifa1675b2013-06-16 00:37:59 +0200534
535 if (d_max_p)
536 *d_max_p = d_max;
537
538 return 0;
Rafael J. Wysocki86b38322012-11-02 01:40:18 +0100539}
Rafael J. Wysockicd7bd022012-11-02 01:40:28 +0100540
Rafael J. Wysockia6ae7592012-11-02 01:40:53 +0100541/**
542 * acpi_pm_device_sleep_state - Get preferred power state of ACPI device.
543 * @dev: Device whose preferred target power state to return.
544 * @d_min_p: Location to store the upper limit of the allowed states range.
545 * @d_max_in: Deepest low-power state to take into consideration.
546 * Return value: Preferred power state of the device on success, -ENODEV
Rafael J. Wysockifa1675b2013-06-16 00:37:59 +0200547 * if there's no 'struct acpi_device' for @dev, -EINVAL if @d_max_in is
548 * incorrect, or -ENODATA on ACPI method failure.
Rafael J. Wysockia6ae7592012-11-02 01:40:53 +0100549 *
550 * The caller must ensure that @dev is valid before using this function.
551 */
552int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in)
553{
554 acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
555 struct acpi_device *adev;
Rafael J. Wysocki9b5c7a52013-06-27 14:01:02 +0200556 int ret, d_min, d_max;
Rafael J. Wysockifa1675b2013-06-16 00:37:59 +0200557
558 if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3_COLD)
559 return -EINVAL;
560
561 if (d_max_in > ACPI_STATE_D3_HOT) {
562 enum pm_qos_flags_status stat;
563
564 stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF);
565 if (stat == PM_QOS_FLAGS_ALL)
566 d_max_in = ACPI_STATE_D3_HOT;
567 }
Rafael J. Wysockia6ae7592012-11-02 01:40:53 +0100568
Yasuaki Ishimatsudde3bb42013-01-31 03:22:14 +0000569 if (!handle || acpi_bus_get_device(handle, &adev)) {
Rafael J. Wysockia6ae7592012-11-02 01:40:53 +0100570 dev_dbg(dev, "ACPI handle without context in %s!\n", __func__);
571 return -ENODEV;
572 }
573
Rafael J. Wysockifa1675b2013-06-16 00:37:59 +0200574 ret = acpi_dev_pm_get_state(dev, adev, acpi_target_system_state(),
Rafael J. Wysocki9b5c7a52013-06-27 14:01:02 +0200575 &d_min, &d_max);
Rafael J. Wysockifa1675b2013-06-16 00:37:59 +0200576 if (ret)
577 return ret;
578
Rafael J. Wysocki9b5c7a52013-06-27 14:01:02 +0200579 if (d_max_in < d_min)
Rafael J. Wysockifa1675b2013-06-16 00:37:59 +0200580 return -EINVAL;
581
582 if (d_max > d_max_in) {
Rafael J. Wysocki9b5c7a52013-06-27 14:01:02 +0200583 for (d_max = d_max_in; d_max > d_min; d_max--) {
Rafael J. Wysockifa1675b2013-06-16 00:37:59 +0200584 if (adev->power.states[d_max].flags.valid)
585 break;
586 }
587 }
Rafael J. Wysocki9b5c7a52013-06-27 14:01:02 +0200588
589 if (d_min_p)
590 *d_min_p = d_min;
591
Rafael J. Wysockifa1675b2013-06-16 00:37:59 +0200592 return d_max;
Rafael J. Wysockia6ae7592012-11-02 01:40:53 +0100593}
594EXPORT_SYMBOL(acpi_pm_device_sleep_state);
595
Rafael J. Wysockicd7bd022012-11-02 01:40:28 +0100596#ifdef CONFIG_PM_RUNTIME
597/**
Rafael J. Wysockie5cc8ef2012-11-02 01:41:01 +0100598 * acpi_wakeup_device - Wakeup notification handler for ACPI devices.
599 * @handle: ACPI handle of the device the notification is for.
600 * @event: Type of the signaled event.
601 * @context: Device corresponding to @handle.
602 */
603static void acpi_wakeup_device(acpi_handle handle, u32 event, void *context)
604{
605 struct device *dev = context;
606
607 if (event == ACPI_NOTIFY_DEVICE_WAKE && dev) {
608 pm_wakeup_event(dev, 0);
609 pm_runtime_resume(dev);
610 }
611}
612
613/**
Rafael J. Wysockidee83702012-11-02 01:40:36 +0100614 * __acpi_device_run_wake - Enable/disable runtime remote wakeup for device.
615 * @adev: ACPI device to enable/disable the remote wakeup for.
Rafael J. Wysockicd7bd022012-11-02 01:40:28 +0100616 * @enable: Whether to enable or disable the wakeup functionality.
617 *
Rafael J. Wysockidee83702012-11-02 01:40:36 +0100618 * Enable/disable the GPE associated with @adev so that it can generate
619 * wakeup signals for the device in response to external (remote) events and
620 * enable/disable device wakeup power.
621 *
622 * Callers must ensure that @adev is a valid ACPI device node before executing
623 * this function.
624 */
625int __acpi_device_run_wake(struct acpi_device *adev, bool enable)
626{
627 struct acpi_device_wakeup *wakeup = &adev->wakeup;
628
629 if (enable) {
630 acpi_status res;
631 int error;
632
633 error = acpi_enable_wakeup_device_power(adev, ACPI_STATE_S0);
634 if (error)
635 return error;
636
637 res = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number);
638 if (ACPI_FAILURE(res)) {
639 acpi_disable_wakeup_device_power(adev);
640 return -EIO;
641 }
642 } else {
643 acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number);
644 acpi_disable_wakeup_device_power(adev);
645 }
646 return 0;
647}
648
649/**
650 * acpi_pm_device_run_wake - Enable/disable remote wakeup for given device.
651 * @dev: Device to enable/disable the platform to wake up.
652 * @enable: Whether to enable or disable the wakeup functionality.
Rafael J. Wysockicd7bd022012-11-02 01:40:28 +0100653 */
654int acpi_pm_device_run_wake(struct device *phys_dev, bool enable)
655{
Rafael J. Wysockidee83702012-11-02 01:40:36 +0100656 struct acpi_device *adev;
Rafael J. Wysockicd7bd022012-11-02 01:40:28 +0100657 acpi_handle handle;
658
659 if (!device_run_wake(phys_dev))
660 return -EINVAL;
661
662 handle = DEVICE_ACPI_HANDLE(phys_dev);
Yasuaki Ishimatsudde3bb42013-01-31 03:22:14 +0000663 if (!handle || acpi_bus_get_device(handle, &adev)) {
Rafael J. Wysockidee83702012-11-02 01:40:36 +0100664 dev_dbg(phys_dev, "ACPI handle without context in %s!\n",
Rafael J. Wysockicd7bd022012-11-02 01:40:28 +0100665 __func__);
666 return -ENODEV;
667 }
668
Rafael J. Wysockidee83702012-11-02 01:40:36 +0100669 return __acpi_device_run_wake(adev, enable);
Rafael J. Wysockicd7bd022012-11-02 01:40:28 +0100670}
671EXPORT_SYMBOL(acpi_pm_device_run_wake);
Rafael J. Wysockie5cc8ef2012-11-02 01:41:01 +0100672#else
673static inline void acpi_wakeup_device(acpi_handle handle, u32 event,
674 void *context) {}
Rafael J. Wysockicd7bd022012-11-02 01:40:28 +0100675#endif /* CONFIG_PM_RUNTIME */
Rafael J. Wysockidee83702012-11-02 01:40:36 +0100676
Mika Westerberg4d564102013-01-14 20:13:37 +0000677#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockidee83702012-11-02 01:40:36 +0100678/**
679 * __acpi_device_sleep_wake - Enable or disable device to wake up the system.
680 * @dev: Device to enable/desible to wake up the system.
681 * @target_state: System state the device is supposed to wake up from.
682 * @enable: Whether to enable or disable @dev to wake up the system.
683 */
684int __acpi_device_sleep_wake(struct acpi_device *adev, u32 target_state,
685 bool enable)
686{
687 return enable ?
688 acpi_enable_wakeup_device_power(adev, target_state) :
689 acpi_disable_wakeup_device_power(adev);
690}
Rafael J. Wysockia6ae7592012-11-02 01:40:53 +0100691
692/**
693 * acpi_pm_device_sleep_wake - Enable or disable device to wake up the system.
694 * @dev: Device to enable/desible to wake up the system from sleep states.
695 * @enable: Whether to enable or disable @dev to wake up the system.
696 */
697int acpi_pm_device_sleep_wake(struct device *dev, bool enable)
698{
699 acpi_handle handle;
700 struct acpi_device *adev;
701 int error;
702
703 if (!device_can_wakeup(dev))
704 return -EINVAL;
705
706 handle = DEVICE_ACPI_HANDLE(dev);
Yasuaki Ishimatsudde3bb42013-01-31 03:22:14 +0000707 if (!handle || acpi_bus_get_device(handle, &adev)) {
Rafael J. Wysockia6ae7592012-11-02 01:40:53 +0100708 dev_dbg(dev, "ACPI handle without context in %s!\n", __func__);
709 return -ENODEV;
710 }
711
712 error = __acpi_device_sleep_wake(adev, acpi_target_system_state(),
713 enable);
714 if (!error)
715 dev_info(dev, "System wakeup %s by ACPI\n",
716 enable ? "enabled" : "disabled");
717
718 return error;
719}
Rafael J. Wysockidee83702012-11-02 01:40:36 +0100720#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockie5cc8ef2012-11-02 01:41:01 +0100721
722/**
723 * acpi_dev_pm_get_node - Get ACPI device node for the given physical device.
724 * @dev: Device to get the ACPI node for.
725 */
Rafael J. Wysockid2e5f0c2012-12-23 00:02:44 +0100726struct acpi_device *acpi_dev_pm_get_node(struct device *dev)
Rafael J. Wysockie5cc8ef2012-11-02 01:41:01 +0100727{
728 acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
729 struct acpi_device *adev;
730
Rafael J. Wysocki5cc36c72012-12-16 23:28:53 +0100731 return handle && !acpi_bus_get_device(handle, &adev) ? adev : NULL;
Rafael J. Wysockie5cc8ef2012-11-02 01:41:01 +0100732}
733
734/**
735 * acpi_dev_pm_low_power - Put ACPI device into a low-power state.
736 * @dev: Device to put into a low-power state.
737 * @adev: ACPI device node corresponding to @dev.
738 * @system_state: System state to choose the device state for.
739 */
740static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev,
741 u32 system_state)
742{
Rafael J. Wysockifa1675b2013-06-16 00:37:59 +0200743 int ret, state;
Rafael J. Wysockie5cc8ef2012-11-02 01:41:01 +0100744
745 if (!acpi_device_power_manageable(adev))
746 return 0;
747
Rafael J. Wysockifa1675b2013-06-16 00:37:59 +0200748 ret = acpi_dev_pm_get_state(dev, adev, system_state, NULL, &state);
749 return ret ? ret : acpi_device_set_power(adev, state);
Rafael J. Wysockie5cc8ef2012-11-02 01:41:01 +0100750}
751
752/**
753 * acpi_dev_pm_full_power - Put ACPI device into the full-power state.
754 * @adev: ACPI device node to put into the full-power state.
755 */
756static int acpi_dev_pm_full_power(struct acpi_device *adev)
757{
758 return acpi_device_power_manageable(adev) ?
759 acpi_device_set_power(adev, ACPI_STATE_D0) : 0;
760}
761
762#ifdef CONFIG_PM_RUNTIME
763/**
764 * acpi_dev_runtime_suspend - Put device into a low-power state using ACPI.
765 * @dev: Device to put into a low-power state.
766 *
767 * Put the given device into a runtime low-power state using the standard ACPI
768 * mechanism. Set up remote wakeup if desired, choose the state to put the
769 * device into (this checks if remote wakeup is expected to work too), and set
770 * the power state of the device.
771 */
772int acpi_dev_runtime_suspend(struct device *dev)
773{
774 struct acpi_device *adev = acpi_dev_pm_get_node(dev);
775 bool remote_wakeup;
776 int error;
777
778 if (!adev)
779 return 0;
780
781 remote_wakeup = dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) >
782 PM_QOS_FLAGS_NONE;
783 error = __acpi_device_run_wake(adev, remote_wakeup);
784 if (remote_wakeup && error)
785 return -EAGAIN;
786
787 error = acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
788 if (error)
789 __acpi_device_run_wake(adev, false);
790
791 return error;
792}
793EXPORT_SYMBOL_GPL(acpi_dev_runtime_suspend);
794
795/**
796 * acpi_dev_runtime_resume - Put device into the full-power state using ACPI.
797 * @dev: Device to put into the full-power state.
798 *
799 * Put the given device into the full-power state using the standard ACPI
800 * mechanism at run time. Set the power state of the device to ACPI D0 and
801 * disable remote wakeup.
802 */
803int acpi_dev_runtime_resume(struct device *dev)
804{
805 struct acpi_device *adev = acpi_dev_pm_get_node(dev);
806 int error;
807
808 if (!adev)
809 return 0;
810
811 error = acpi_dev_pm_full_power(adev);
812 __acpi_device_run_wake(adev, false);
813 return error;
814}
815EXPORT_SYMBOL_GPL(acpi_dev_runtime_resume);
816
817/**
818 * acpi_subsys_runtime_suspend - Suspend device using ACPI.
819 * @dev: Device to suspend.
820 *
821 * Carry out the generic runtime suspend procedure for @dev and use ACPI to put
822 * it into a runtime low-power state.
823 */
824int acpi_subsys_runtime_suspend(struct device *dev)
825{
826 int ret = pm_generic_runtime_suspend(dev);
827 return ret ? ret : acpi_dev_runtime_suspend(dev);
828}
829EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend);
830
831/**
832 * acpi_subsys_runtime_resume - Resume device using ACPI.
833 * @dev: Device to Resume.
834 *
835 * Use ACPI to put the given device into the full-power state and carry out the
836 * generic runtime resume procedure for it.
837 */
838int acpi_subsys_runtime_resume(struct device *dev)
839{
840 int ret = acpi_dev_runtime_resume(dev);
841 return ret ? ret : pm_generic_runtime_resume(dev);
842}
843EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume);
844#endif /* CONFIG_PM_RUNTIME */
845
846#ifdef CONFIG_PM_SLEEP
847/**
848 * acpi_dev_suspend_late - Put device into a low-power state using ACPI.
849 * @dev: Device to put into a low-power state.
850 *
851 * Put the given device into a low-power state during system transition to a
852 * sleep state using the standard ACPI mechanism. Set up system wakeup if
853 * desired, choose the state to put the device into (this checks if system
854 * wakeup is expected to work too), and set the power state of the device.
855 */
856int acpi_dev_suspend_late(struct device *dev)
857{
858 struct acpi_device *adev = acpi_dev_pm_get_node(dev);
859 u32 target_state;
860 bool wakeup;
861 int error;
862
863 if (!adev)
864 return 0;
865
866 target_state = acpi_target_system_state();
867 wakeup = device_may_wakeup(dev);
868 error = __acpi_device_sleep_wake(adev, target_state, wakeup);
869 if (wakeup && error)
870 return error;
871
872 error = acpi_dev_pm_low_power(dev, adev, target_state);
873 if (error)
874 __acpi_device_sleep_wake(adev, ACPI_STATE_UNKNOWN, false);
875
876 return error;
877}
878EXPORT_SYMBOL_GPL(acpi_dev_suspend_late);
879
880/**
881 * acpi_dev_resume_early - Put device into the full-power state using ACPI.
882 * @dev: Device to put into the full-power state.
883 *
884 * Put the given device into the full-power state using the standard ACPI
885 * mechanism during system transition to the working state. Set the power
886 * state of the device to ACPI D0 and disable remote wakeup.
887 */
888int acpi_dev_resume_early(struct device *dev)
889{
890 struct acpi_device *adev = acpi_dev_pm_get_node(dev);
891 int error;
892
893 if (!adev)
894 return 0;
895
896 error = acpi_dev_pm_full_power(adev);
897 __acpi_device_sleep_wake(adev, ACPI_STATE_UNKNOWN, false);
898 return error;
899}
900EXPORT_SYMBOL_GPL(acpi_dev_resume_early);
901
902/**
903 * acpi_subsys_prepare - Prepare device for system transition to a sleep state.
904 * @dev: Device to prepare.
905 */
906int acpi_subsys_prepare(struct device *dev)
907{
908 /*
909 * Follow PCI and resume devices suspended at run time before running
910 * their system suspend callbacks.
911 */
912 pm_runtime_resume(dev);
913 return pm_generic_prepare(dev);
914}
915EXPORT_SYMBOL_GPL(acpi_subsys_prepare);
916
917/**
918 * acpi_subsys_suspend_late - Suspend device using ACPI.
919 * @dev: Device to suspend.
920 *
921 * Carry out the generic late suspend procedure for @dev and use ACPI to put
922 * it into a low-power state during system transition into a sleep state.
923 */
924int acpi_subsys_suspend_late(struct device *dev)
925{
926 int ret = pm_generic_suspend_late(dev);
927 return ret ? ret : acpi_dev_suspend_late(dev);
928}
929EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late);
930
931/**
932 * acpi_subsys_resume_early - Resume device using ACPI.
933 * @dev: Device to Resume.
934 *
935 * Use ACPI to put the given device into the full-power state and carry out the
936 * generic early resume procedure for it during system transition into the
937 * working state.
938 */
939int acpi_subsys_resume_early(struct device *dev)
940{
941 int ret = acpi_dev_resume_early(dev);
942 return ret ? ret : pm_generic_resume_early(dev);
943}
944EXPORT_SYMBOL_GPL(acpi_subsys_resume_early);
945#endif /* CONFIG_PM_SLEEP */
946
947static struct dev_pm_domain acpi_general_pm_domain = {
948 .ops = {
949#ifdef CONFIG_PM_RUNTIME
950 .runtime_suspend = acpi_subsys_runtime_suspend,
951 .runtime_resume = acpi_subsys_runtime_resume,
Rafael J. Wysockie5cc8ef2012-11-02 01:41:01 +0100952#endif
953#ifdef CONFIG_PM_SLEEP
954 .prepare = acpi_subsys_prepare,
955 .suspend_late = acpi_subsys_suspend_late,
956 .resume_early = acpi_subsys_resume_early,
957 .poweroff_late = acpi_subsys_suspend_late,
958 .restore_early = acpi_subsys_resume_early,
959#endif
960 },
961};
962
963/**
964 * acpi_dev_pm_attach - Prepare device for ACPI power management.
965 * @dev: Device to prepare.
Rafael J. Wysockib88ce2a2012-11-26 10:03:06 +0100966 * @power_on: Whether or not to power on the device.
Rafael J. Wysockie5cc8ef2012-11-02 01:41:01 +0100967 *
968 * If @dev has a valid ACPI handle that has a valid struct acpi_device object
969 * attached to it, install a wakeup notification handler for the device and
Rafael J. Wysockib88ce2a2012-11-26 10:03:06 +0100970 * add it to the general ACPI PM domain. If @power_on is set, the device will
971 * be put into the ACPI D0 state before the function returns.
Rafael J. Wysockie5cc8ef2012-11-02 01:41:01 +0100972 *
973 * This assumes that the @dev's bus type uses generic power management callbacks
974 * (or doesn't use any power management callbacks at all).
975 *
976 * Callers must ensure proper synchronization of this function with power
977 * management callbacks.
978 */
Rafael J. Wysockib88ce2a2012-11-26 10:03:06 +0100979int acpi_dev_pm_attach(struct device *dev, bool power_on)
Rafael J. Wysockie5cc8ef2012-11-02 01:41:01 +0100980{
981 struct acpi_device *adev = acpi_dev_pm_get_node(dev);
982
983 if (!adev)
984 return -ENODEV;
985
986 if (dev->pm_domain)
987 return -EEXIST;
988
989 acpi_add_pm_notifier(adev, acpi_wakeup_device, dev);
990 dev->pm_domain = &acpi_general_pm_domain;
Rafael J. Wysockib88ce2a2012-11-26 10:03:06 +0100991 if (power_on) {
992 acpi_dev_pm_full_power(adev);
993 __acpi_device_run_wake(adev, false);
994 }
Rafael J. Wysockie5cc8ef2012-11-02 01:41:01 +0100995 return 0;
996}
997EXPORT_SYMBOL_GPL(acpi_dev_pm_attach);
998
999/**
1000 * acpi_dev_pm_detach - Remove ACPI power management from the device.
1001 * @dev: Device to take care of.
Rafael J. Wysockib88ce2a2012-11-26 10:03:06 +01001002 * @power_off: Whether or not to try to remove power from the device.
Rafael J. Wysockie5cc8ef2012-11-02 01:41:01 +01001003 *
1004 * Remove the device from the general ACPI PM domain and remove its wakeup
Rafael J. Wysockib88ce2a2012-11-26 10:03:06 +01001005 * notifier. If @power_off is set, additionally remove power from the device if
1006 * possible.
Rafael J. Wysockie5cc8ef2012-11-02 01:41:01 +01001007 *
1008 * Callers must ensure proper synchronization of this function with power
1009 * management callbacks.
1010 */
Rafael J. Wysockib88ce2a2012-11-26 10:03:06 +01001011void acpi_dev_pm_detach(struct device *dev, bool power_off)
Rafael J. Wysockie5cc8ef2012-11-02 01:41:01 +01001012{
1013 struct acpi_device *adev = acpi_dev_pm_get_node(dev);
1014
1015 if (adev && dev->pm_domain == &acpi_general_pm_domain) {
1016 dev->pm_domain = NULL;
1017 acpi_remove_pm_notifier(adev, acpi_wakeup_device);
Rafael J. Wysockib88ce2a2012-11-26 10:03:06 +01001018 if (power_off) {
1019 /*
1020 * If the device's PM QoS resume latency limit or flags
1021 * have been exposed to user space, they have to be
1022 * hidden at this point, so that they don't affect the
1023 * choice of the low-power state to put the device into.
1024 */
1025 dev_pm_qos_hide_latency_limit(dev);
1026 dev_pm_qos_hide_flags(dev);
1027 __acpi_device_run_wake(adev, false);
1028 acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
1029 }
Rafael J. Wysockie5cc8ef2012-11-02 01:41:01 +01001030 }
1031}
1032EXPORT_SYMBOL_GPL(acpi_dev_pm_detach);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +01001033
1034/**
1035 * acpi_dev_pm_add_dependent - Add physical device depending for PM.
1036 * @handle: Handle of ACPI device node.
1037 * @depdev: Device depending on that node for PM.
1038 */
1039void acpi_dev_pm_add_dependent(acpi_handle handle, struct device *depdev)
1040{
1041 struct acpi_device_physical_node *dep;
1042 struct acpi_device *adev;
1043
1044 if (!depdev || acpi_bus_get_device(handle, &adev))
1045 return;
1046
1047 mutex_lock(&adev->physical_node_lock);
1048
1049 list_for_each_entry(dep, &adev->power_dependent, node)
1050 if (dep->dev == depdev)
1051 goto out;
1052
1053 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1054 if (dep) {
1055 dep->dev = depdev;
1056 list_add_tail(&dep->node, &adev->power_dependent);
1057 }
1058
1059 out:
1060 mutex_unlock(&adev->physical_node_lock);
1061}
1062EXPORT_SYMBOL_GPL(acpi_dev_pm_add_dependent);
1063
1064/**
1065 * acpi_dev_pm_remove_dependent - Remove physical device depending for PM.
1066 * @handle: Handle of ACPI device node.
1067 * @depdev: Device depending on that node for PM.
1068 */
1069void acpi_dev_pm_remove_dependent(acpi_handle handle, struct device *depdev)
1070{
1071 struct acpi_device_physical_node *dep;
1072 struct acpi_device *adev;
1073
1074 if (!depdev || acpi_bus_get_device(handle, &adev))
1075 return;
1076
1077 mutex_lock(&adev->physical_node_lock);
1078
1079 list_for_each_entry(dep, &adev->power_dependent, node)
1080 if (dep->dev == depdev) {
1081 list_del(&dep->node);
1082 kfree(dep);
1083 break;
1084 }
1085
1086 mutex_unlock(&adev->physical_node_lock);
1087}
1088EXPORT_SYMBOL_GPL(acpi_dev_pm_remove_dependent);
Rafael J. Wysockiec4602a2013-05-16 22:29:28 +02001089#endif /* CONFIG_PM */