blob: 4b93c97aff9f8126d7fd1952dd5fd8f2b090621e [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. Wysockibc9b6402013-01-17 14:11:05 +010061struct acpi_power_dependent_device {
62 struct list_head node;
63 struct acpi_device *adev;
64 struct work_struct work;
Lin Ming0090def2012-03-29 14:09:39 +080065};
66
Len Brown4be44fc2005-08-05 00:44:28 -040067struct acpi_power_resource {
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010068 struct acpi_device device;
Rafael J. Wysocki781d7372013-01-17 14:11:06 +010069 struct list_head list_node;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +010070 struct list_head dependent;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010071 char *name;
Len Brown4be44fc2005-08-05 00:44:28 -040072 u32 system_level;
73 u32 order;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +020074 unsigned int ref_count;
Konstantin Karasyov0a613902007-02-16 01:47:06 -050075 struct mutex resource_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076};
77
Rafael J. Wysocki0b224522013-01-17 14:11:06 +010078struct acpi_power_resource_entry {
79 struct list_head node;
80 struct acpi_power_resource *resource;
81};
82
Rafael J. Wysocki781d7372013-01-17 14:11:06 +010083static LIST_HEAD(acpi_power_resource_list);
84static DEFINE_MUTEX(power_resource_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086/* --------------------------------------------------------------------------
87 Power Resource Management
88 -------------------------------------------------------------------------- */
89
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010090static struct acpi_power_resource *acpi_power_get_context(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010092 struct acpi_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010094 if (acpi_bus_get_device(handle, &device))
95 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010097 return container_of(device, struct acpi_power_resource, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100100static void acpi_power_resources_list_add(acpi_handle handle,
101 struct list_head *list)
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100102{
103 struct acpi_power_resource *resource = acpi_power_get_context(handle);
104 struct acpi_power_resource_entry *entry;
105
106 if (!resource || !list)
107 return;
108
109 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
110 if (!entry)
111 return;
112
113 entry->resource = resource;
114 if (!list_empty(list)) {
115 struct acpi_power_resource_entry *e;
116
117 list_for_each_entry(e, list, node)
118 if (e->resource->order > resource->order) {
119 list_add_tail(&entry->node, &e->node);
120 return;
121 }
122 }
123 list_add_tail(&entry->node, list);
124}
125
126void acpi_power_resources_list_free(struct list_head *list)
127{
128 struct acpi_power_resource_entry *entry, *e;
129
130 list_for_each_entry_safe(entry, e, list, node) {
131 list_del(&entry->node);
132 kfree(entry);
133 }
134}
135
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100136acpi_status acpi_extract_power_resources(union acpi_object *package,
137 unsigned int start,
138 struct list_head *list)
139{
140 acpi_status status = AE_OK;
141 unsigned int i;
142
143 for (i = start; i < package->package.count; i++) {
144 union acpi_object *element = &package->package.elements[i];
145 acpi_handle rhandle;
146
147 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
148 status = AE_BAD_DATA;
149 break;
150 }
151 rhandle = element->reference.handle;
152 if (!rhandle) {
153 status = AE_NULL_ENTRY;
154 break;
155 }
156 acpi_add_power_resource(rhandle);
157 acpi_power_resources_list_add(rhandle, list);
158 }
159 if (ACPI_FAILURE(status))
160 acpi_power_resources_list_free(list);
161
162 return status;
163}
164
Zhao Yakuia51e1452008-08-11 14:55:05 +0800165static int acpi_power_get_state(acpi_handle handle, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Len Brown4be44fc2005-08-05 00:44:28 -0400167 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400168 unsigned long long sta = 0;
Lin Ming60a4ce72008-12-16 17:02:22 +0800169 char node_name[5];
170 struct acpi_buffer buffer = { sizeof(node_name), node_name };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Zhao Yakuia51e1452008-08-11 14:55:05 +0800173 if (!handle || !state)
Patrick Mocheld550d982006-06-27 00:41:40 -0400174 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Zhao Yakuia51e1452008-08-11 14:55:05 +0800176 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400178 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Alexey Starikovskiyc35923b2007-10-22 14:19:09 +0400180 *state = (sta & 0x01)?ACPI_POWER_RESOURCE_STATE_ON:
181 ACPI_POWER_RESOURCE_STATE_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Lin Ming60a4ce72008-12-16 17:02:22 +0800183 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n",
Lin Ming60a4ce72008-12-16 17:02:22 +0800186 node_name,
Zhao Yakuib1b57fb2008-10-27 16:04:53 +0800187 *state ? "on" : "off"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Patrick Mocheld550d982006-06-27 00:41:40 -0400189 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100192static int acpi_power_get_list_state(struct list_head *list, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100194 struct acpi_power_resource_entry *entry;
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100195 int cur_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 if (!list || !state)
Patrick Mocheld550d982006-06-27 00:41:40 -0400198 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 /* The state of the list is 'on' IFF all resources are 'on'. */
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100201 list_for_each_entry(entry, list, node) {
202 struct acpi_power_resource *resource = entry->resource;
203 acpi_handle handle = resource->device.handle;
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100204 int result;
205
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100206 mutex_lock(&resource->resource_lock);
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100207 result = acpi_power_get_state(handle, &cur_state);
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100208 mutex_unlock(&resource->resource_lock);
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100209 if (result)
210 return result;
211
212 if (cur_state != ACPI_POWER_RESOURCE_STATE_ON)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 break;
214 }
215
216 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n",
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100217 cur_state ? "on" : "off"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100219 *state = cur_state;
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100220 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100223static void acpi_power_resume_dependent(struct work_struct *work)
Lin Ming0090def2012-03-29 14:09:39 +0800224{
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100225 struct acpi_power_dependent_device *dep;
226 struct acpi_device_physical_node *pn;
227 struct acpi_device *adev;
Lin Ming0090def2012-03-29 14:09:39 +0800228 int state;
229
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100230 dep = container_of(work, struct acpi_power_dependent_device, work);
231 adev = dep->adev;
232 if (acpi_power_get_inferred_state(adev, &state))
Lin Ming0090def2012-03-29 14:09:39 +0800233 return;
234
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100235 if (state > ACPI_STATE_D0)
Lin Ming0090def2012-03-29 14:09:39 +0800236 return;
237
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100238 mutex_lock(&adev->physical_node_lock);
239
240 list_for_each_entry(pn, &adev->physical_node_list, node)
241 pm_request_resume(pn->dev);
242
243 list_for_each_entry(pn, &adev->power_dependent, node)
244 pm_request_resume(pn->dev);
245
246 mutex_unlock(&adev->physical_node_lock);
Lin Ming0090def2012-03-29 14:09:39 +0800247}
248
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200249static int __acpi_power_on(struct acpi_power_resource *resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Len Brown4be44fc2005-08-05 00:44:28 -0400251 acpi_status status = AE_OK;
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500252
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100253 status = acpi_evaluate_object(resource->device.handle, "_ON", NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400255 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200257 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned on\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400258 resource->name));
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200259
Patrick Mocheld550d982006-06-27 00:41:40 -0400260 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100263static int acpi_power_on(struct acpi_power_resource *resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100265 int result = 0;;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500267 mutex_lock(&resource->resource_lock);
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200268
269 if (resource->ref_count++) {
270 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
271 "Power resource [%s] already on",
272 resource->name));
273 } else {
274 result = __acpi_power_on(resource);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100275 if (result) {
Rafael J. Wysocki12b3b5a2010-11-25 00:03:32 +0100276 resource->ref_count--;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100277 } else {
278 struct acpi_power_dependent_device *dep;
279
280 list_for_each_entry(dep, &resource->dependent, node)
281 schedule_work(&dep->work);
282 }
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500285 mutex_unlock(&resource->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Rafael J. Wysocki12b3b5a2010-11-25 00:03:32 +0100287 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100290static int acpi_power_off(struct acpi_power_resource *resource)
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200291{
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200292 acpi_status status = AE_OK;
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100293 int result = 0;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200294
295 mutex_lock(&resource->resource_lock);
296
297 if (!resource->ref_count) {
298 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
299 "Power resource [%s] already off",
300 resource->name));
301 goto unlock;
302 }
303
304 if (--resource->ref_count) {
305 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
306 "Power resource [%s] still in use\n",
307 resource->name));
308 goto unlock;
309 }
310
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100311 status = acpi_evaluate_object(resource->device.handle, "_OFF", NULL, NULL);
Rafael J. Wysocki722c9292013-01-17 14:11:06 +0100312 if (ACPI_FAILURE(status))
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200313 result = -ENODEV;
Rafael J. Wysocki722c9292013-01-17 14:11:06 +0100314 else
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200315 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
316 "Power resource [%s] turned off\n",
317 resource->name));
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200318
319 unlock:
320 mutex_unlock(&resource->resource_lock);
321
322 return result;
323}
324
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100325static int acpi_power_off_list(struct list_head *list)
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100326{
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100327 struct acpi_power_resource_entry *entry;
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100328 int result = 0;
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100329
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100330 list_for_each_entry_reverse(entry, list, node) {
331 result = acpi_power_off(entry->resource);
332 if (result)
333 goto err;
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100334 }
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100335 return 0;
336
337 err:
338 list_for_each_entry_continue(entry, list, node)
339 acpi_power_on(entry->resource);
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100340
341 return result;
342}
343
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100344static int acpi_power_on_list(struct list_head *list)
345{
346 struct acpi_power_resource_entry *entry;
347 int result = 0;
348
349 list_for_each_entry(entry, list, node) {
350 result = acpi_power_on(entry->resource);
351 if (result)
352 goto err;
353 }
354 return 0;
355
356 err:
357 list_for_each_entry_continue_reverse(entry, list, node)
358 acpi_power_off(entry->resource);
359
360 return result;
361}
362
363static void acpi_power_add_dependent(struct acpi_power_resource *resource,
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100364 struct acpi_device *adev)
Lin Ming0090def2012-03-29 14:09:39 +0800365{
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100366 struct acpi_power_dependent_device *dep;
Lin Ming0090def2012-03-29 14:09:39 +0800367
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100368 mutex_lock(&resource->resource_lock);
369
370 list_for_each_entry(dep, &resource->dependent, node)
371 if (dep->adev == adev)
372 goto out;
373
374 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
375 if (!dep)
376 goto out;
377
378 dep->adev = adev;
379 INIT_WORK(&dep->work, acpi_power_resume_dependent);
380 list_add_tail(&dep->node, &resource->dependent);
381
382 out:
383 mutex_unlock(&resource->resource_lock);
384}
385
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100386static void acpi_power_remove_dependent(struct acpi_power_resource *resource,
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100387 struct acpi_device *adev)
388{
389 struct acpi_power_dependent_device *dep;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100390 struct work_struct *work = NULL;
391
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100392 mutex_lock(&resource->resource_lock);
393
394 list_for_each_entry(dep, &resource->dependent, node)
395 if (dep->adev == adev) {
396 list_del(&dep->node);
397 work = &dep->work;
398 break;
399 }
400
401 mutex_unlock(&resource->resource_lock);
402
403 if (work) {
404 cancel_work_sync(work);
405 kfree(dep);
406 }
407}
408
409void acpi_power_add_remove_device(struct acpi_device *adev, bool add)
410{
411 if (adev->power.flags.power_resources) {
412 struct acpi_device_power_state *ps;
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100413 struct acpi_power_resource_entry *entry;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100414
415 ps = &adev->power.states[ACPI_STATE_D0];
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100416 list_for_each_entry(entry, &ps->resources, node) {
417 struct acpi_power_resource *resource = entry->resource;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100418
419 if (add)
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100420 acpi_power_add_dependent(resource, adev);
Lin Ming0090def2012-03-29 14:09:39 +0800421 else
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100422 acpi_power_remove_dependent(resource, adev);
Lin Ming0090def2012-03-29 14:09:39 +0800423 }
424 }
Lin Ming0090def2012-03-29 14:09:39 +0800425}
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100426
427/* --------------------------------------------------------------------------
428 Device Power Management
429 -------------------------------------------------------------------------- */
Lin Ming0090def2012-03-29 14:09:39 +0800430
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200431/**
432 * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in
433 * ACPI 3.0) _PSW (Power State Wake)
434 * @dev: Device to handle.
435 * @enable: 0 - disable, 1 - enable the wake capabilities of the device.
436 * @sleep_state: Target sleep state of the system.
437 * @dev_state: Target power state of the device.
438 *
439 * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
440 * State Wake) for the device, if present. On failure reset the device's
441 * wakeup.flags.valid flag.
442 *
443 * RETURN VALUE:
444 * 0 if either _DSW or _PSW has been successfully executed
445 * 0 if neither _DSW nor _PSW has been found
446 * -ENODEV if the execution of either _DSW or _PSW has failed
447 */
448int acpi_device_sleep_wake(struct acpi_device *dev,
449 int enable, int sleep_state, int dev_state)
450{
451 union acpi_object in_arg[3];
452 struct acpi_object_list arg_list = { 3, in_arg };
453 acpi_status status = AE_OK;
454
455 /*
456 * Try to execute _DSW first.
457 *
458 * Three agruments are needed for the _DSW object:
459 * Argument 0: enable/disable the wake capabilities
460 * Argument 1: target system state
461 * Argument 2: target device state
462 * When _DSW object is called to disable the wake capabilities, maybe
463 * the first argument is filled. The values of the other two agruments
464 * are meaningless.
465 */
466 in_arg[0].type = ACPI_TYPE_INTEGER;
467 in_arg[0].integer.value = enable;
468 in_arg[1].type = ACPI_TYPE_INTEGER;
469 in_arg[1].integer.value = sleep_state;
470 in_arg[2].type = ACPI_TYPE_INTEGER;
471 in_arg[2].integer.value = dev_state;
472 status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL);
473 if (ACPI_SUCCESS(status)) {
474 return 0;
475 } else if (status != AE_NOT_FOUND) {
476 printk(KERN_ERR PREFIX "_DSW execution failed\n");
477 dev->wakeup.flags.valid = 0;
478 return -ENODEV;
479 }
480
481 /* Execute _PSW */
482 arg_list.count = 1;
483 in_arg[0].integer.value = enable;
484 status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL);
485 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
486 printk(KERN_ERR PREFIX "_PSW execution failed\n");
487 dev->wakeup.flags.valid = 0;
488 return -ENODEV;
489 }
490
491 return 0;
492}
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494/*
495 * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
496 * 1. Power on the power resources required for the wakeup device
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200497 * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
498 * State Wake) for the device, if present
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 */
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200500int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100502 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (!dev || !dev->wakeup.flags.valid)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200505 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200507 mutex_lock(&acpi_device_lock);
508
509 if (dev->wakeup.prepare_count++)
510 goto out;
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200511
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100512 err = acpi_power_on_list(&dev->wakeup.resources);
513 if (err) {
514 dev_err(&dev->dev, "Cannot turn wakeup power resources on\n");
515 dev->wakeup.flags.valid = 0;
516 } else {
517 /*
518 * Passing 3 as the third argument below means the device may be
519 * put into arbitrary power state afterward.
520 */
521 err = acpi_device_sleep_wake(dev, 1, sleep_state, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200523 if (err)
524 dev->wakeup.prepare_count = 0;
525
526 out:
527 mutex_unlock(&acpi_device_lock);
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200528 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
531/*
532 * Shutdown a wakeup device, counterpart of above method
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200533 * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
534 * State Wake) for the device, if present
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 * 2. Shutdown down the power resources
536 */
Len Brown4be44fc2005-08-05 00:44:28 -0400537int acpi_disable_wakeup_device_power(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100539 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 if (!dev || !dev->wakeup.flags.valid)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200542 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200544 mutex_lock(&acpi_device_lock);
545
546 if (--dev->wakeup.prepare_count > 0)
547 goto out;
548
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200549 /*
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200550 * Executing the code below even if prepare_count is already zero when
551 * the function is called may be useful, for example for initialisation.
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200552 */
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200553 if (dev->wakeup.prepare_count < 0)
554 dev->wakeup.prepare_count = 0;
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200555
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200556 err = acpi_device_sleep_wake(dev, 0, 0, 0);
557 if (err)
558 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100560 err = acpi_power_off_list(&dev->wakeup.resources);
561 if (err) {
562 dev_err(&dev->dev, "Cannot turn wakeup power resources off\n");
563 dev->wakeup.flags.valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200566 out:
567 mutex_unlock(&acpi_device_lock);
568 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100571int acpi_power_get_inferred_state(struct acpi_device *device, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Len Brown4be44fc2005-08-05 00:44:28 -0400573 int result = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400574 int list_state = 0;
575 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100577 if (!device || !state)
Patrick Mocheld550d982006-06-27 00:41:40 -0400578 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 /*
581 * We know a device's inferred power state when all the resources
582 * required for a given D-state are 'on'.
583 */
Rafael J. Wysocki38c92ff2012-05-20 13:58:00 +0200584 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100585 struct list_head *list = &device->power.states[i].resources;
586
587 if (list_empty(list))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 continue;
589
590 result = acpi_power_get_list_state(list, &list_state);
591 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400592 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100595 *state = i;
Patrick Mocheld550d982006-06-27 00:41:40 -0400596 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
598 }
599
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100600 *state = ACPI_STATE_D3;
Patrick Mocheld550d982006-06-27 00:41:40 -0400601 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603
Rafael J. Wysocki30d3df412010-11-25 00:06:55 +0100604int acpi_power_on_resources(struct acpi_device *device, int state)
605{
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100606 if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3_COLD)
Rafael J. Wysocki30d3df412010-11-25 00:06:55 +0100607 return -EINVAL;
608
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100609 if (state == ACPI_STATE_D3_COLD)
610 return 0;
611
Rafael J. Wysocki30d3df412010-11-25 00:06:55 +0100612 return acpi_power_on_list(&device->power.states[state].resources);
613}
614
Len Brown4be44fc2005-08-05 00:44:28 -0400615int acpi_power_transition(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +0200617 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Zhang Rui3ebc81b2012-03-29 14:09:38 +0800619 if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
Patrick Mocheld550d982006-06-27 00:41:40 -0400620 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100622 if (device->power.state == state || !device->flags.power_manageable)
Rafael J. Wysocki212967c2010-11-25 00:02:36 +0100623 return 0;
624
Len Brown4be44fc2005-08-05 00:44:28 -0400625 if ((device->power.state < ACPI_STATE_D0)
Zhang Rui3ebc81b2012-03-29 14:09:38 +0800626 || (device->power.state > ACPI_STATE_D3_COLD))
Patrick Mocheld550d982006-06-27 00:41:40 -0400627 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 /* TBD: Resources must be ordered. */
630
631 /*
632 * First we reference all power resources required in the target list
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100633 * (e.g. so the device doesn't lose power while transitioning). Then,
634 * we dereference all power resources used in the current list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 */
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +0200636 if (state < ACPI_STATE_D3_COLD)
637 result = acpi_power_on_list(
638 &device->power.states[state].resources);
639
640 if (!result && device->power.state < ACPI_STATE_D3_COLD)
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100641 acpi_power_off_list(
642 &device->power.states[device->power.state].resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100644 /* We shouldn't change the state unless the above operations succeed. */
645 device->power.state = result ? ACPI_STATE_UNKNOWN : state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Patrick Mocheld550d982006-06-27 00:41:40 -0400647 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100650static void acpi_release_power_resource(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100652 struct acpi_device *device = to_acpi_device(dev);
653 struct acpi_power_resource *resource;
654
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100655 resource = container_of(device, struct acpi_power_resource, device);
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100656
657 mutex_lock(&power_resource_list_lock);
658 list_del(&resource->list_node);
659 mutex_unlock(&power_resource_list_lock);
660
661 acpi_free_ids(device);
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100662 kfree(resource);
663}
664
665void acpi_add_power_resource(acpi_handle handle)
666{
667 struct acpi_power_resource *resource;
668 struct acpi_device *device = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400669 union acpi_object acpi_object;
670 struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100671 acpi_status status;
672 int state, result = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100674 acpi_bus_get_device(handle, &device);
675 if (device)
676 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100678 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (!resource)
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100680 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100682 device = &resource->device;
683 acpi_init_device_object(device, handle, ACPI_BUS_TYPE_POWER,
684 ACPI_STA_DEFAULT);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500685 mutex_init(&resource->resource_lock);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100686 INIT_LIST_HEAD(&resource->dependent);
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100687 resource->name = device->pnp.bus_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
689 strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
Rafael J. Wysocki722c9292013-01-17 14:11:06 +0100690 device->power.state = ACPI_STATE_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 /* Evalute the object to get the system level and resource order. */
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100693 status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
694 if (ACPI_FAILURE(status))
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100695 goto err;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 resource->system_level = acpi_object.power_resource.system_level;
698 resource->order = acpi_object.power_resource.resource_order;
699
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100700 result = acpi_power_get_state(handle, &state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (result)
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100702 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Len Brown4be44fc2005-08-05 00:44:28 -0400704 printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device),
Alexey Starikovskiyc35923b2007-10-22 14:19:09 +0400705 acpi_device_bid(device), state ? "on" : "off");
Len Brown4be44fc2005-08-05 00:44:28 -0400706
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100707 device->flags.match_driver = true;
708 result = acpi_device_register(device, acpi_release_power_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (result)
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100710 goto err;
Len Brown4be44fc2005-08-05 00:44:28 -0400711
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100712 mutex_lock(&power_resource_list_lock);
713 list_add(&resource->list_node, &acpi_power_resource_list);
714 mutex_unlock(&power_resource_list_lock);
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100715 return;
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100716
717 err:
718 acpi_release_power_resource(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100721#ifdef CONFIG_ACPI_SLEEP
722void acpi_resume_power_resources(void)
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500723{
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200724 struct acpi_power_resource *resource;
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500725
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100726 mutex_lock(&power_resource_list_lock);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500727
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100728 list_for_each_entry(resource, &acpi_power_resource_list, list_node) {
729 int result, state;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200730
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100731 mutex_lock(&resource->resource_lock);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500732
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100733 result = acpi_power_get_state(resource->device.handle, &state);
734 if (!result && state == ACPI_POWER_RESOURCE_STATE_OFF
735 && resource->ref_count) {
736 dev_info(&resource->device.dev, "Turning ON\n");
737 __acpi_power_on(resource);
738 }
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500739
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100740 mutex_unlock(&resource->resource_lock);
741 }
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500742
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100743 mutex_unlock(&power_resource_list_lock);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500744}
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200745#endif