blob: b12933fd2e565c2f53112e0bd69e26f59f83a936 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * acpi_power.c - ACPI Bus Power Management ($Revision: 39 $)
3 *
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@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 as published by
11 * the Free Software Foundation; either version 2 of the License, or (at
12 * your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 *
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 */
25
26/*
27 * ACPI power-managed devices may be controlled in two ways:
28 * 1. via "Device Specific (D-State) Control"
29 * 2. via "Power Resource Control".
30 * This module is used to manage devices relying on Power Resource Control.
31 *
32 * An ACPI "power resource object" describes a software controllable power
33 * plane, clock plane, or other resource used by a power managed device.
34 * A device may rely on multiple power resources, and a power resource
35 * may be shared by multiple devices.
36 */
37
38#include <linux/kernel.h>
39#include <linux/module.h>
40#include <linux/init.h>
41#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Lin Ming0090def2012-03-29 14:09:39 +080043#include <linux/pm_runtime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <acpi/acpi_bus.h>
45#include <acpi/acpi_drivers.h>
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +020046#include "sleep.h"
Lin Ming0090def2012-03-29 14:09:39 +080047#include "internal.h"
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +020048
Len Browna192a952009-07-28 16:45:54 -040049#define PREFIX "ACPI: "
50
Bjorn Helgaas89595b82008-11-07 16:57:45 -070051#define _COMPONENT ACPI_POWER_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050052ACPI_MODULE_NAME("power");
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define ACPI_POWER_CLASS "power_resource"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define ACPI_POWER_DEVICE_NAME "Power Resource"
55#define ACPI_POWER_FILE_INFO "info"
56#define ACPI_POWER_FILE_STATUS "state"
57#define ACPI_POWER_RESOURCE_STATE_OFF 0x00
58#define ACPI_POWER_RESOURCE_STATE_ON 0x01
59#define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF
Zhao Yakuif5adfaa2008-08-11 14:57:50 +080060
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010061static inline int acpi_power_add(struct acpi_device *device) { return 0; }
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Márton Némethc97adf92010-01-10 17:15:36 +010063static const struct acpi_device_id power_device_ids[] = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +020064 {ACPI_POWER_HID, 0},
65 {"", 0},
66};
67MODULE_DEVICE_TABLE(acpi, power_device_ids);
68
Rafael J. Wysocki90692402012-08-09 23:00:02 +020069#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockie579e2d2012-06-27 23:26:59 +020070static int acpi_power_resume(struct device *dev);
Rafael J. Wysocki90692402012-08-09 23:00:02 +020071#endif
Rafael J. Wysockie579e2d2012-06-27 23:26:59 +020072static SIMPLE_DEV_PM_OPS(acpi_power_pm, NULL, acpi_power_resume);
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static struct acpi_driver acpi_power_driver = {
Len Brownc2b67052007-02-12 23:33:40 -050075 .name = "power",
Len Brown4be44fc2005-08-05 00:44:28 -040076 .class = ACPI_POWER_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020077 .ids = power_device_ids,
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010078 .ops.add = acpi_power_add,
Rafael J. Wysockie579e2d2012-06-27 23:26:59 +020079 .drv.pm = &acpi_power_pm,
Linus Torvalds1da177e2005-04-16 15:20:36 -070080};
81
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +010082struct acpi_power_dependent_device {
83 struct list_head node;
84 struct acpi_device *adev;
85 struct work_struct work;
Lin Ming0090def2012-03-29 14:09:39 +080086};
87
Len Brown4be44fc2005-08-05 00:44:28 -040088struct acpi_power_resource {
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010089 struct acpi_device device;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +010090 struct list_head dependent;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010091 char *name;
Len Brown4be44fc2005-08-05 00:44:28 -040092 u32 system_level;
93 u32 order;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +020094 unsigned int ref_count;
Konstantin Karasyov0a613902007-02-16 01:47:06 -050095 struct mutex resource_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096};
97
Len Brown4be44fc2005-08-05 00:44:28 -040098static struct list_head acpi_power_resource_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100/* --------------------------------------------------------------------------
101 Power Resource Management
102 -------------------------------------------------------------------------- */
103
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100104static struct acpi_power_resource *acpi_power_get_context(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100106 struct acpi_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100108 if (acpi_bus_get_device(handle, &device))
109 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100111 return container_of(device, struct acpi_power_resource, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Zhao Yakuia51e1452008-08-11 14:55:05 +0800114static int acpi_power_get_state(acpi_handle handle, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Len Brown4be44fc2005-08-05 00:44:28 -0400116 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400117 unsigned long long sta = 0;
Lin Ming60a4ce72008-12-16 17:02:22 +0800118 char node_name[5];
119 struct acpi_buffer buffer = { sizeof(node_name), node_name };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Zhao Yakuia51e1452008-08-11 14:55:05 +0800122 if (!handle || !state)
Patrick Mocheld550d982006-06-27 00:41:40 -0400123 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Zhao Yakuia51e1452008-08-11 14:55:05 +0800125 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400127 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Alexey Starikovskiyc35923b2007-10-22 14:19:09 +0400129 *state = (sta & 0x01)?ACPI_POWER_RESOURCE_STATE_ON:
130 ACPI_POWER_RESOURCE_STATE_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Lin Ming60a4ce72008-12-16 17:02:22 +0800132 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n",
Lin Ming60a4ce72008-12-16 17:02:22 +0800135 node_name,
Zhao Yakuib1b57fb2008-10-27 16:04:53 +0800136 *state ? "on" : "off"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Patrick Mocheld550d982006-06-27 00:41:40 -0400138 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
Len Brown4be44fc2005-08-05 00:44:28 -0400141static int acpi_power_get_list_state(struct acpi_handle_list *list, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100143 int cur_state;
144 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 if (!list || !state)
Patrick Mocheld550d982006-06-27 00:41:40 -0400147 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 /* The state of the list is 'on' IFF all resources are 'on'. */
150
Len Brown4be44fc2005-08-05 00:44:28 -0400151 for (i = 0; i < list->count; i++) {
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100152 struct acpi_power_resource *resource;
153 acpi_handle handle = list->handles[i];
154 int result;
155
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100156 resource = acpi_power_get_context(handle);
157 if (!resource)
158 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100160 mutex_lock(&resource->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100162 result = acpi_power_get_state(handle, &cur_state);
163
164 mutex_unlock(&resource->resource_lock);
165
166 if (result)
167 return result;
168
169 if (cur_state != ACPI_POWER_RESOURCE_STATE_ON)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 break;
171 }
172
173 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n",
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100174 cur_state ? "on" : "off"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100176 *state = cur_state;
177
178 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100181static void acpi_power_resume_dependent(struct work_struct *work)
Lin Ming0090def2012-03-29 14:09:39 +0800182{
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100183 struct acpi_power_dependent_device *dep;
184 struct acpi_device_physical_node *pn;
185 struct acpi_device *adev;
Lin Ming0090def2012-03-29 14:09:39 +0800186 int state;
187
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100188 dep = container_of(work, struct acpi_power_dependent_device, work);
189 adev = dep->adev;
190 if (acpi_power_get_inferred_state(adev, &state))
Lin Ming0090def2012-03-29 14:09:39 +0800191 return;
192
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100193 if (state > ACPI_STATE_D0)
Lin Ming0090def2012-03-29 14:09:39 +0800194 return;
195
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100196 mutex_lock(&adev->physical_node_lock);
197
198 list_for_each_entry(pn, &adev->physical_node_list, node)
199 pm_request_resume(pn->dev);
200
201 list_for_each_entry(pn, &adev->power_dependent, node)
202 pm_request_resume(pn->dev);
203
204 mutex_unlock(&adev->physical_node_lock);
Lin Ming0090def2012-03-29 14:09:39 +0800205}
206
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200207static int __acpi_power_on(struct acpi_power_resource *resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Len Brown4be44fc2005-08-05 00:44:28 -0400209 acpi_status status = AE_OK;
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500210
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100211 status = acpi_evaluate_object(resource->device.handle, "_ON", NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400213 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 /* Update the power resource's _device_ power state */
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100216 resource->device.power.state = ACPI_STATE_D0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200218 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned on\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400219 resource->name));
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200220
Patrick Mocheld550d982006-06-27 00:41:40 -0400221 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200224static int acpi_power_on(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Bjorn Helgaasbdf43bb2009-05-21 17:28:53 -0600226 int result = 0;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100227 struct acpi_power_resource *resource;
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500228
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100229 resource = acpi_power_get_context(handle);
230 if (!resource)
231 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500233 mutex_lock(&resource->resource_lock);
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200234
235 if (resource->ref_count++) {
236 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
237 "Power resource [%s] already on",
238 resource->name));
239 } else {
240 result = __acpi_power_on(resource);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100241 if (result) {
Rafael J. Wysocki12b3b5a2010-11-25 00:03:32 +0100242 resource->ref_count--;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100243 } else {
244 struct acpi_power_dependent_device *dep;
245
246 list_for_each_entry(dep, &resource->dependent, node)
247 schedule_work(&dep->work);
248 }
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500251 mutex_unlock(&resource->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Rafael J. Wysocki12b3b5a2010-11-25 00:03:32 +0100253 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
Rafael J. Wysocki36237fa2011-01-06 23:38:04 +0100256static int acpi_power_off(acpi_handle handle)
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200257{
258 int result = 0;
259 acpi_status status = AE_OK;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100260 struct acpi_power_resource *resource;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200261
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100262 resource = acpi_power_get_context(handle);
263 if (!resource)
264 return -ENODEV;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200265
266 mutex_lock(&resource->resource_lock);
267
268 if (!resource->ref_count) {
269 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
270 "Power resource [%s] already off",
271 resource->name));
272 goto unlock;
273 }
274
275 if (--resource->ref_count) {
276 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
277 "Power resource [%s] still in use\n",
278 resource->name));
279 goto unlock;
280 }
281
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100282 status = acpi_evaluate_object(resource->device.handle, "_OFF", NULL, NULL);
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200283 if (ACPI_FAILURE(status)) {
284 result = -ENODEV;
285 } else {
286 /* Update the power resource's _device_ power state */
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100287 resource->device.power.state = ACPI_STATE_D3;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200288
289 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
290 "Power resource [%s] turned off\n",
291 resource->name));
292 }
293
294 unlock:
295 mutex_unlock(&resource->resource_lock);
296
297 return result;
298}
299
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100300static void __acpi_power_off_list(struct acpi_handle_list *list, int num_res)
301{
302 int i;
303
304 for (i = num_res - 1; i >= 0 ; i--)
Rafael J. Wysocki36237fa2011-01-06 23:38:04 +0100305 acpi_power_off(list->handles[i]);
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100306}
307
308static void acpi_power_off_list(struct acpi_handle_list *list)
309{
310 __acpi_power_off_list(list, list->count);
311}
312
313static int acpi_power_on_list(struct acpi_handle_list *list)
314{
315 int result = 0;
316 int i;
317
318 for (i = 0; i < list->count; i++) {
319 result = acpi_power_on(list->handles[i]);
320 if (result) {
321 __acpi_power_off_list(list, i);
322 break;
323 }
324 }
325
326 return result;
327}
328
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100329static void acpi_power_add_dependent(acpi_handle rhandle,
330 struct acpi_device *adev)
Lin Ming0090def2012-03-29 14:09:39 +0800331{
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100332 struct acpi_power_dependent_device *dep;
333 struct acpi_power_resource *resource;
Lin Ming0090def2012-03-29 14:09:39 +0800334
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100335 if (!rhandle || !adev)
336 return;
337
338 resource = acpi_power_get_context(rhandle);
339 if (!resource)
Lin Ming0090def2012-03-29 14:09:39 +0800340 return;
341
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100342 mutex_lock(&resource->resource_lock);
343
344 list_for_each_entry(dep, &resource->dependent, node)
345 if (dep->adev == adev)
346 goto out;
347
348 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
349 if (!dep)
350 goto out;
351
352 dep->adev = adev;
353 INIT_WORK(&dep->work, acpi_power_resume_dependent);
354 list_add_tail(&dep->node, &resource->dependent);
355
356 out:
357 mutex_unlock(&resource->resource_lock);
358}
359
360static void acpi_power_remove_dependent(acpi_handle rhandle,
361 struct acpi_device *adev)
362{
363 struct acpi_power_dependent_device *dep;
364 struct acpi_power_resource *resource;
365 struct work_struct *work = NULL;
366
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100367 if (!rhandle || !adev)
368 return;
369
370 resource = acpi_power_get_context(rhandle);
371 if (!resource)
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100372 return;
373
374 mutex_lock(&resource->resource_lock);
375
376 list_for_each_entry(dep, &resource->dependent, node)
377 if (dep->adev == adev) {
378 list_del(&dep->node);
379 work = &dep->work;
380 break;
381 }
382
383 mutex_unlock(&resource->resource_lock);
384
385 if (work) {
386 cancel_work_sync(work);
387 kfree(dep);
388 }
389}
390
391void acpi_power_add_remove_device(struct acpi_device *adev, bool add)
392{
393 if (adev->power.flags.power_resources) {
394 struct acpi_device_power_state *ps;
395 int j;
396
397 ps = &adev->power.states[ACPI_STATE_D0];
398 for (j = 0; j < ps->resources.count; j++) {
399 acpi_handle rhandle = ps->resources.handles[j];
400
401 if (add)
402 acpi_power_add_dependent(rhandle, adev);
Lin Ming0090def2012-03-29 14:09:39 +0800403 else
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100404 acpi_power_remove_dependent(rhandle, adev);
Lin Ming0090def2012-03-29 14:09:39 +0800405 }
406 }
Lin Ming0090def2012-03-29 14:09:39 +0800407}
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100408
409/* --------------------------------------------------------------------------
410 Device Power Management
411 -------------------------------------------------------------------------- */
Lin Ming0090def2012-03-29 14:09:39 +0800412
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200413/**
414 * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in
415 * ACPI 3.0) _PSW (Power State Wake)
416 * @dev: Device to handle.
417 * @enable: 0 - disable, 1 - enable the wake capabilities of the device.
418 * @sleep_state: Target sleep state of the system.
419 * @dev_state: Target power state of the device.
420 *
421 * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
422 * State Wake) for the device, if present. On failure reset the device's
423 * wakeup.flags.valid flag.
424 *
425 * RETURN VALUE:
426 * 0 if either _DSW or _PSW has been successfully executed
427 * 0 if neither _DSW nor _PSW has been found
428 * -ENODEV if the execution of either _DSW or _PSW has failed
429 */
430int acpi_device_sleep_wake(struct acpi_device *dev,
431 int enable, int sleep_state, int dev_state)
432{
433 union acpi_object in_arg[3];
434 struct acpi_object_list arg_list = { 3, in_arg };
435 acpi_status status = AE_OK;
436
437 /*
438 * Try to execute _DSW first.
439 *
440 * Three agruments are needed for the _DSW object:
441 * Argument 0: enable/disable the wake capabilities
442 * Argument 1: target system state
443 * Argument 2: target device state
444 * When _DSW object is called to disable the wake capabilities, maybe
445 * the first argument is filled. The values of the other two agruments
446 * are meaningless.
447 */
448 in_arg[0].type = ACPI_TYPE_INTEGER;
449 in_arg[0].integer.value = enable;
450 in_arg[1].type = ACPI_TYPE_INTEGER;
451 in_arg[1].integer.value = sleep_state;
452 in_arg[2].type = ACPI_TYPE_INTEGER;
453 in_arg[2].integer.value = dev_state;
454 status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL);
455 if (ACPI_SUCCESS(status)) {
456 return 0;
457 } else if (status != AE_NOT_FOUND) {
458 printk(KERN_ERR PREFIX "_DSW execution failed\n");
459 dev->wakeup.flags.valid = 0;
460 return -ENODEV;
461 }
462
463 /* Execute _PSW */
464 arg_list.count = 1;
465 in_arg[0].integer.value = enable;
466 status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL);
467 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
468 printk(KERN_ERR PREFIX "_PSW execution failed\n");
469 dev->wakeup.flags.valid = 0;
470 return -ENODEV;
471 }
472
473 return 0;
474}
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476/*
477 * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
478 * 1. Power on the power resources required for the wakeup device
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200479 * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
480 * State Wake) for the device, if present
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 */
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200482int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200484 int i, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (!dev || !dev->wakeup.flags.valid)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200487 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200489 mutex_lock(&acpi_device_lock);
490
491 if (dev->wakeup.prepare_count++)
492 goto out;
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 /* Open power resource */
495 for (i = 0; i < dev->wakeup.resources.count; i++) {
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200496 int ret = acpi_power_on(dev->wakeup.resources.handles[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (ret) {
Len Brown64684632006-06-26 23:41:38 -0400498 printk(KERN_ERR PREFIX "Transition power state\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 dev->wakeup.flags.valid = 0;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200500 err = -ENODEV;
501 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
503 }
504
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200505 /*
506 * Passing 3 as the third argument below means the device may be placed
507 * in arbitrary power state afterwards.
508 */
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200509 err = acpi_device_sleep_wake(dev, 1, sleep_state, 3);
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200510
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200511 err_out:
512 if (err)
513 dev->wakeup.prepare_count = 0;
514
515 out:
516 mutex_unlock(&acpi_device_lock);
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200517 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
520/*
521 * Shutdown a wakeup device, counterpart of above method
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200522 * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
523 * State Wake) for the device, if present
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 * 2. Shutdown down the power resources
525 */
Len Brown4be44fc2005-08-05 00:44:28 -0400526int acpi_disable_wakeup_device_power(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200528 int i, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 if (!dev || !dev->wakeup.flags.valid)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200531 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200533 mutex_lock(&acpi_device_lock);
534
535 if (--dev->wakeup.prepare_count > 0)
536 goto out;
537
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200538 /*
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200539 * Executing the code below even if prepare_count is already zero when
540 * the function is called may be useful, for example for initialisation.
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200541 */
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200542 if (dev->wakeup.prepare_count < 0)
543 dev->wakeup.prepare_count = 0;
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200544
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200545 err = acpi_device_sleep_wake(dev, 0, 0, 0);
546 if (err)
547 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 /* Close power resource */
550 for (i = 0; i < dev->wakeup.resources.count; i++) {
Rafael J. Wysocki36237fa2011-01-06 23:38:04 +0100551 int ret = acpi_power_off(dev->wakeup.resources.handles[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (ret) {
Len Brown64684632006-06-26 23:41:38 -0400553 printk(KERN_ERR PREFIX "Transition power state\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 dev->wakeup.flags.valid = 0;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200555 err = -ENODEV;
556 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558 }
559
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200560 out:
561 mutex_unlock(&acpi_device_lock);
562 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100565int acpi_power_get_inferred_state(struct acpi_device *device, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Len Brown4be44fc2005-08-05 00:44:28 -0400567 int result = 0;
568 struct acpi_handle_list *list = NULL;
569 int list_state = 0;
570 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100572 if (!device || !state)
Patrick Mocheld550d982006-06-27 00:41:40 -0400573 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 /*
576 * We know a device's inferred power state when all the resources
577 * required for a given D-state are 'on'.
578 */
Rafael J. Wysocki38c92ff2012-05-20 13:58:00 +0200579 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 list = &device->power.states[i].resources;
581 if (list->count < 1)
582 continue;
583
584 result = acpi_power_get_list_state(list, &list_state);
585 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400586 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100589 *state = i;
Patrick Mocheld550d982006-06-27 00:41:40 -0400590 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
592 }
593
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100594 *state = ACPI_STATE_D3;
Patrick Mocheld550d982006-06-27 00:41:40 -0400595 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
Rafael J. Wysocki30d3df412010-11-25 00:06:55 +0100598int acpi_power_on_resources(struct acpi_device *device, int state)
599{
600 if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3)
601 return -EINVAL;
602
603 return acpi_power_on_list(&device->power.states[state].resources);
604}
605
Len Brown4be44fc2005-08-05 00:44:28 -0400606int acpi_power_transition(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +0200608 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Zhang Rui3ebc81b2012-03-29 14:09:38 +0800610 if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
Patrick Mocheld550d982006-06-27 00:41:40 -0400611 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Rafael J. Wysocki212967c2010-11-25 00:02:36 +0100613 if (device->power.state == state)
614 return 0;
615
Len Brown4be44fc2005-08-05 00:44:28 -0400616 if ((device->power.state < ACPI_STATE_D0)
Zhang Rui3ebc81b2012-03-29 14:09:38 +0800617 || (device->power.state > ACPI_STATE_D3_COLD))
Patrick Mocheld550d982006-06-27 00:41:40 -0400618 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 /* TBD: Resources must be ordered. */
621
622 /*
623 * First we reference all power resources required in the target list
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100624 * (e.g. so the device doesn't lose power while transitioning). Then,
625 * we dereference all power resources used in the current list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 */
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +0200627 if (state < ACPI_STATE_D3_COLD)
628 result = acpi_power_on_list(
629 &device->power.states[state].resources);
630
631 if (!result && device->power.state < ACPI_STATE_D3_COLD)
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100632 acpi_power_off_list(
633 &device->power.states[device->power.state].resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100635 /* We shouldn't change the state unless the above operations succeed. */
636 device->power.state = result ? ACPI_STATE_UNKNOWN : state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Patrick Mocheld550d982006-06-27 00:41:40 -0400638 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100641static void acpi_release_power_resource(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100643 struct acpi_device *device = to_acpi_device(dev);
644 struct acpi_power_resource *resource;
645
646 acpi_free_ids(device);
647 resource = container_of(device, struct acpi_power_resource, device);
648 kfree(resource);
649}
650
651void acpi_add_power_resource(acpi_handle handle)
652{
653 struct acpi_power_resource *resource;
654 struct acpi_device *device = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400655 union acpi_object acpi_object;
656 struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100657 acpi_status status;
658 int state, result = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100660 acpi_bus_get_device(handle, &device);
661 if (device)
662 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100664 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if (!resource)
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100666 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100668 device = &resource->device;
669 acpi_init_device_object(device, handle, ACPI_BUS_TYPE_POWER,
670 ACPI_STA_DEFAULT);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500671 mutex_init(&resource->resource_lock);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100672 INIT_LIST_HEAD(&resource->dependent);
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100673 resource->name = device->pnp.bus_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
675 strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 /* Evalute the object to get the system level and resource order. */
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100678 status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
679 if (ACPI_FAILURE(status))
680 goto out;
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 resource->system_level = acpi_object.power_resource.system_level;
683 resource->order = acpi_object.power_resource.resource_order;
684
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100685 result = acpi_power_get_state(handle, &state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (result)
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100687 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Alexey Starikovskiyc35923b2007-10-22 14:19:09 +0400689 switch (state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 case ACPI_POWER_RESOURCE_STATE_ON:
691 device->power.state = ACPI_STATE_D0;
692 break;
693 case ACPI_POWER_RESOURCE_STATE_OFF:
694 device->power.state = ACPI_STATE_D3;
695 break;
696 default:
697 device->power.state = ACPI_STATE_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
699
Len Brown4be44fc2005-08-05 00:44:28 -0400700 printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device),
Alexey Starikovskiyc35923b2007-10-22 14:19:09 +0400701 acpi_device_bid(device), state ? "on" : "off");
Len Brown4be44fc2005-08-05 00:44:28 -0400702
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100703 device->flags.match_driver = true;
704 result = acpi_device_register(device, acpi_release_power_resource);
705
706 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (result)
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100708 acpi_release_power_resource(&device->dev);
Len Brown4be44fc2005-08-05 00:44:28 -0400709
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100710 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100713/* --------------------------------------------------------------------------
714 Driver Interface
715 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200717#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockie579e2d2012-06-27 23:26:59 +0200718static int acpi_power_resume(struct device *dev)
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500719{
Alexey Starikovskiyc35923b2007-10-22 14:19:09 +0400720 int result = 0, state;
Rafael J. Wysockie579e2d2012-06-27 23:26:59 +0200721 struct acpi_device *device;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200722 struct acpi_power_resource *resource;
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500723
Rafael J. Wysockie579e2d2012-06-27 23:26:59 +0200724 if (!dev)
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500725 return -EINVAL;
726
Rafael J. Wysockie579e2d2012-06-27 23:26:59 +0200727 device = to_acpi_device(dev);
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700728 resource = acpi_driver_data(device);
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200729 if (!resource)
730 return -EINVAL;
731
732 mutex_lock(&resource->resource_lock);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500733
Zhao Yakuia51e1452008-08-11 14:55:05 +0800734 result = acpi_power_get_state(device->handle, &state);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500735 if (result)
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200736 goto unlock;
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500737
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200738 if (state == ACPI_POWER_RESOURCE_STATE_OFF && resource->ref_count)
739 result = __acpi_power_on(resource);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500740
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200741 unlock:
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500742 mutex_unlock(&resource->resource_lock);
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200743
744 return result;
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500745}
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200746#endif
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500747
Bjorn Helgaas44515372009-03-24 16:49:53 -0600748int __init acpi_power_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 INIT_LIST_HEAD(&acpi_power_resource_list);
Zhang Rui06af7eb2010-07-15 10:46:38 +0800751 return acpi_bus_register_driver(&acpi_power_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}