blob: b820528a5fa3fa99a2c6628dd7aa0740776a402e [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>
Rafael J. Wysocki18a38702013-01-25 21:51:32 +010044#include <linux/sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <acpi/acpi_bus.h>
46#include <acpi/acpi_drivers.h>
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +020047#include "sleep.h"
Lin Ming0090def2012-03-29 14:09:39 +080048#include "internal.h"
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +020049
Len Browna192a952009-07-28 16:45:54 -040050#define PREFIX "ACPI: "
51
Bjorn Helgaas89595b82008-11-07 16:57:45 -070052#define _COMPONENT ACPI_POWER_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050053ACPI_MODULE_NAME("power");
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define ACPI_POWER_CLASS "power_resource"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#define ACPI_POWER_DEVICE_NAME "Power Resource"
56#define ACPI_POWER_FILE_INFO "info"
57#define ACPI_POWER_FILE_STATUS "state"
58#define ACPI_POWER_RESOURCE_STATE_OFF 0x00
59#define ACPI_POWER_RESOURCE_STATE_ON 0x01
60#define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF
Zhao Yakuif5adfaa2008-08-11 14:57:50 +080061
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +010062struct acpi_power_dependent_device {
63 struct list_head node;
64 struct acpi_device *adev;
65 struct work_struct work;
Lin Ming0090def2012-03-29 14:09:39 +080066};
67
Len Brown4be44fc2005-08-05 00:44:28 -040068struct acpi_power_resource {
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010069 struct acpi_device device;
Rafael J. Wysocki781d7372013-01-17 14:11:06 +010070 struct list_head list_node;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +010071 struct list_head dependent;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010072 char *name;
Len Brown4be44fc2005-08-05 00:44:28 -040073 u32 system_level;
74 u32 order;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +020075 unsigned int ref_count;
Konstantin Karasyov0a613902007-02-16 01:47:06 -050076 struct mutex resource_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077};
78
Rafael J. Wysocki0b224522013-01-17 14:11:06 +010079struct acpi_power_resource_entry {
80 struct list_head node;
81 struct acpi_power_resource *resource;
82};
83
Rafael J. Wysocki781d7372013-01-17 14:11:06 +010084static LIST_HEAD(acpi_power_resource_list);
85static DEFINE_MUTEX(power_resource_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/* --------------------------------------------------------------------------
88 Power Resource Management
89 -------------------------------------------------------------------------- */
90
Rafael J. Wysockib1c0f992013-01-24 12:50:09 +010091static inline
92struct acpi_power_resource *to_power_resource(struct acpi_device *device)
93{
94 return container_of(device, struct acpi_power_resource, device);
95}
96
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010097static struct acpi_power_resource *acpi_power_get_context(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010099 struct acpi_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100101 if (acpi_bus_get_device(handle, &device))
102 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Rafael J. Wysockib1c0f992013-01-24 12:50:09 +0100104 return to_power_resource(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100107static int acpi_power_resources_list_add(acpi_handle handle,
108 struct list_head *list)
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100109{
110 struct acpi_power_resource *resource = acpi_power_get_context(handle);
111 struct acpi_power_resource_entry *entry;
112
113 if (!resource || !list)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100114 return -EINVAL;
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100115
116 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
117 if (!entry)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100118 return -ENOMEM;
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100119
120 entry->resource = resource;
121 if (!list_empty(list)) {
122 struct acpi_power_resource_entry *e;
123
124 list_for_each_entry(e, list, node)
125 if (e->resource->order > resource->order) {
126 list_add_tail(&entry->node, &e->node);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100127 return 0;
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100128 }
129 }
130 list_add_tail(&entry->node, list);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100131 return 0;
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100132}
133
134void acpi_power_resources_list_free(struct list_head *list)
135{
136 struct acpi_power_resource_entry *entry, *e;
137
138 list_for_each_entry_safe(entry, e, list, node) {
139 list_del(&entry->node);
140 kfree(entry);
141 }
142}
143
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100144int acpi_extract_power_resources(union acpi_object *package, unsigned int start,
145 struct list_head *list)
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100146{
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100147 unsigned int i;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100148 int err = 0;
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100149
150 for (i = start; i < package->package.count; i++) {
151 union acpi_object *element = &package->package.elements[i];
152 acpi_handle rhandle;
153
154 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100155 err = -ENODATA;
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100156 break;
157 }
158 rhandle = element->reference.handle;
159 if (!rhandle) {
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100160 err = -ENODEV;
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100161 break;
162 }
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100163 err = acpi_add_power_resource(rhandle);
164 if (err)
165 break;
166
167 err = acpi_power_resources_list_add(rhandle, list);
168 if (err)
169 break;
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100170 }
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100171 if (err)
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100172 acpi_power_resources_list_free(list);
173
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100174 return err;
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100175}
176
Zhao Yakuia51e1452008-08-11 14:55:05 +0800177static int acpi_power_get_state(acpi_handle handle, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Len Brown4be44fc2005-08-05 00:44:28 -0400179 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400180 unsigned long long sta = 0;
Lin Ming60a4ce72008-12-16 17:02:22 +0800181 char node_name[5];
182 struct acpi_buffer buffer = { sizeof(node_name), node_name };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Zhao Yakuia51e1452008-08-11 14:55:05 +0800185 if (!handle || !state)
Patrick Mocheld550d982006-06-27 00:41:40 -0400186 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Zhao Yakuia51e1452008-08-11 14:55:05 +0800188 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400190 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Alexey Starikovskiyc35923b2007-10-22 14:19:09 +0400192 *state = (sta & 0x01)?ACPI_POWER_RESOURCE_STATE_ON:
193 ACPI_POWER_RESOURCE_STATE_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Lin Ming60a4ce72008-12-16 17:02:22 +0800195 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n",
Lin Ming60a4ce72008-12-16 17:02:22 +0800198 node_name,
Zhao Yakuib1b57fb2008-10-27 16:04:53 +0800199 *state ? "on" : "off"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Patrick Mocheld550d982006-06-27 00:41:40 -0400201 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100204static int acpi_power_get_list_state(struct list_head *list, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100206 struct acpi_power_resource_entry *entry;
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100207 int cur_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 if (!list || !state)
Patrick Mocheld550d982006-06-27 00:41:40 -0400210 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 /* The state of the list is 'on' IFF all resources are 'on'. */
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100213 list_for_each_entry(entry, list, node) {
214 struct acpi_power_resource *resource = entry->resource;
215 acpi_handle handle = resource->device.handle;
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100216 int result;
217
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100218 mutex_lock(&resource->resource_lock);
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100219 result = acpi_power_get_state(handle, &cur_state);
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100220 mutex_unlock(&resource->resource_lock);
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100221 if (result)
222 return result;
223
224 if (cur_state != ACPI_POWER_RESOURCE_STATE_ON)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 break;
226 }
227
228 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n",
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100229 cur_state ? "on" : "off"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100231 *state = cur_state;
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100232 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100235static void acpi_power_resume_dependent(struct work_struct *work)
Lin Ming0090def2012-03-29 14:09:39 +0800236{
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100237 struct acpi_power_dependent_device *dep;
238 struct acpi_device_physical_node *pn;
239 struct acpi_device *adev;
Lin Ming0090def2012-03-29 14:09:39 +0800240 int state;
241
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100242 dep = container_of(work, struct acpi_power_dependent_device, work);
243 adev = dep->adev;
244 if (acpi_power_get_inferred_state(adev, &state))
Lin Ming0090def2012-03-29 14:09:39 +0800245 return;
246
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100247 if (state > ACPI_STATE_D0)
Lin Ming0090def2012-03-29 14:09:39 +0800248 return;
249
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100250 mutex_lock(&adev->physical_node_lock);
251
252 list_for_each_entry(pn, &adev->physical_node_list, node)
253 pm_request_resume(pn->dev);
254
255 list_for_each_entry(pn, &adev->power_dependent, node)
256 pm_request_resume(pn->dev);
257
258 mutex_unlock(&adev->physical_node_lock);
Lin Ming0090def2012-03-29 14:09:39 +0800259}
260
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200261static int __acpi_power_on(struct acpi_power_resource *resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Len Brown4be44fc2005-08-05 00:44:28 -0400263 acpi_status status = AE_OK;
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500264
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100265 status = acpi_evaluate_object(resource->device.handle, "_ON", NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400267 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200269 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned on\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400270 resource->name));
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200271
Patrick Mocheld550d982006-06-27 00:41:40 -0400272 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100275static int acpi_power_on(struct acpi_power_resource *resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100277 int result = 0;;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500279 mutex_lock(&resource->resource_lock);
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200280
281 if (resource->ref_count++) {
282 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
283 "Power resource [%s] already on",
284 resource->name));
285 } else {
286 result = __acpi_power_on(resource);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100287 if (result) {
Rafael J. Wysocki12b3b5a2010-11-25 00:03:32 +0100288 resource->ref_count--;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100289 } else {
290 struct acpi_power_dependent_device *dep;
291
292 list_for_each_entry(dep, &resource->dependent, node)
293 schedule_work(&dep->work);
294 }
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500297 mutex_unlock(&resource->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Rafael J. Wysocki12b3b5a2010-11-25 00:03:32 +0100299 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
Rafael J. Wysocki660b1112013-01-25 21:51:57 +0100302static int __acpi_power_off(struct acpi_power_resource *resource)
303{
304 acpi_status status;
305
306 status = acpi_evaluate_object(resource->device.handle, "_OFF",
307 NULL, NULL);
308 if (ACPI_FAILURE(status))
309 return -ENODEV;
310
311 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned off\n",
312 resource->name));
313 return 0;
314}
315
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100316static int acpi_power_off(struct acpi_power_resource *resource)
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200317{
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100318 int result = 0;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200319
320 mutex_lock(&resource->resource_lock);
321
322 if (!resource->ref_count) {
323 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
324 "Power resource [%s] already off",
325 resource->name));
326 goto unlock;
327 }
328
329 if (--resource->ref_count) {
330 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
331 "Power resource [%s] still in use\n",
332 resource->name));
Rafael J. Wysocki660b1112013-01-25 21:51:57 +0100333 } else {
334 result = __acpi_power_off(resource);
335 if (result)
336 resource->ref_count++;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200337 }
338
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200339 unlock:
340 mutex_unlock(&resource->resource_lock);
341
342 return result;
343}
344
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100345static int acpi_power_off_list(struct list_head *list)
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100346{
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100347 struct acpi_power_resource_entry *entry;
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100348 int result = 0;
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100349
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100350 list_for_each_entry_reverse(entry, list, node) {
351 result = acpi_power_off(entry->resource);
352 if (result)
353 goto err;
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100354 }
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100355 return 0;
356
357 err:
358 list_for_each_entry_continue(entry, list, node)
359 acpi_power_on(entry->resource);
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100360
361 return result;
362}
363
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100364static int acpi_power_on_list(struct list_head *list)
365{
366 struct acpi_power_resource_entry *entry;
367 int result = 0;
368
369 list_for_each_entry(entry, list, node) {
370 result = acpi_power_on(entry->resource);
371 if (result)
372 goto err;
373 }
374 return 0;
375
376 err:
377 list_for_each_entry_continue_reverse(entry, list, node)
378 acpi_power_off(entry->resource);
379
380 return result;
381}
382
383static void acpi_power_add_dependent(struct acpi_power_resource *resource,
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100384 struct acpi_device *adev)
Lin Ming0090def2012-03-29 14:09:39 +0800385{
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100386 struct acpi_power_dependent_device *dep;
Lin Ming0090def2012-03-29 14:09:39 +0800387
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100388 mutex_lock(&resource->resource_lock);
389
390 list_for_each_entry(dep, &resource->dependent, node)
391 if (dep->adev == adev)
392 goto out;
393
394 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
395 if (!dep)
396 goto out;
397
398 dep->adev = adev;
399 INIT_WORK(&dep->work, acpi_power_resume_dependent);
400 list_add_tail(&dep->node, &resource->dependent);
401
402 out:
403 mutex_unlock(&resource->resource_lock);
404}
405
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100406static void acpi_power_remove_dependent(struct acpi_power_resource *resource,
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100407 struct acpi_device *adev)
408{
409 struct acpi_power_dependent_device *dep;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100410 struct work_struct *work = NULL;
411
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100412 mutex_lock(&resource->resource_lock);
413
414 list_for_each_entry(dep, &resource->dependent, node)
415 if (dep->adev == adev) {
416 list_del(&dep->node);
417 work = &dep->work;
418 break;
419 }
420
421 mutex_unlock(&resource->resource_lock);
422
423 if (work) {
424 cancel_work_sync(work);
425 kfree(dep);
426 }
427}
428
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100429static struct attribute *attrs[] = {
430 NULL,
431};
432
433static struct attribute_group attr_groups[] = {
434 [ACPI_STATE_D0] = {
435 .name = "power_resources_D0",
436 .attrs = attrs,
437 },
438 [ACPI_STATE_D1] = {
439 .name = "power_resources_D1",
440 .attrs = attrs,
441 },
442 [ACPI_STATE_D2] = {
443 .name = "power_resources_D2",
444 .attrs = attrs,
445 },
446 [ACPI_STATE_D3_HOT] = {
447 .name = "power_resources_D3hot",
448 .attrs = attrs,
449 },
450};
451
452static void acpi_power_hide_list(struct acpi_device *adev, int state)
453{
454 struct acpi_device_power_state *ps = &adev->power.states[state];
455 struct acpi_power_resource_entry *entry;
456
457 if (list_empty(&ps->resources))
458 return;
459
460 list_for_each_entry_reverse(entry, &ps->resources, node) {
461 struct acpi_device *res_dev = &entry->resource->device;
462
463 sysfs_remove_link_from_group(&adev->dev.kobj,
464 attr_groups[state].name,
465 dev_name(&res_dev->dev));
466 }
467 sysfs_remove_group(&adev->dev.kobj, &attr_groups[state]);
468}
469
470static void acpi_power_expose_list(struct acpi_device *adev, int state)
471{
472 struct acpi_device_power_state *ps = &adev->power.states[state];
473 struct acpi_power_resource_entry *entry;
474 int ret;
475
476 if (list_empty(&ps->resources))
477 return;
478
479 ret = sysfs_create_group(&adev->dev.kobj, &attr_groups[state]);
480 if (ret)
481 return;
482
483 list_for_each_entry(entry, &ps->resources, node) {
484 struct acpi_device *res_dev = &entry->resource->device;
485
486 ret = sysfs_add_link_to_group(&adev->dev.kobj,
487 attr_groups[state].name,
488 &res_dev->dev.kobj,
489 dev_name(&res_dev->dev));
490 if (ret) {
491 acpi_power_hide_list(adev, state);
492 break;
493 }
494 }
495}
496
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100497void acpi_power_add_remove_device(struct acpi_device *adev, bool add)
498{
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100499 struct acpi_device_power_state *ps;
500 struct acpi_power_resource_entry *entry;
501 int state;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100502
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100503 if (!adev->power.flags.power_resources)
504 return;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100505
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100506 ps = &adev->power.states[ACPI_STATE_D0];
507 list_for_each_entry(entry, &ps->resources, node) {
508 struct acpi_power_resource *resource = entry->resource;
509
510 if (add)
511 acpi_power_add_dependent(resource, adev);
512 else
513 acpi_power_remove_dependent(resource, adev);
514 }
515
516 for (state = ACPI_STATE_D0; state <= ACPI_STATE_D3_HOT; state++) {
517 if (add)
518 acpi_power_expose_list(adev, state);
519 else
520 acpi_power_hide_list(adev, state);
Lin Ming0090def2012-03-29 14:09:39 +0800521 }
Lin Ming0090def2012-03-29 14:09:39 +0800522}
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100523
Rafael J. Wysocki0596a522013-01-17 14:11:07 +0100524int acpi_power_min_system_level(struct list_head *list)
525{
526 struct acpi_power_resource_entry *entry;
527 int system_level = 5;
528
529 list_for_each_entry(entry, list, node) {
530 struct acpi_power_resource *resource = entry->resource;
531
532 if (system_level > resource->system_level)
533 system_level = resource->system_level;
534 }
535 return system_level;
536}
537
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100538/* --------------------------------------------------------------------------
539 Device Power Management
540 -------------------------------------------------------------------------- */
Lin Ming0090def2012-03-29 14:09:39 +0800541
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200542/**
543 * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in
544 * ACPI 3.0) _PSW (Power State Wake)
545 * @dev: Device to handle.
546 * @enable: 0 - disable, 1 - enable the wake capabilities of the device.
547 * @sleep_state: Target sleep state of the system.
548 * @dev_state: Target power state of the device.
549 *
550 * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
551 * State Wake) for the device, if present. On failure reset the device's
552 * wakeup.flags.valid flag.
553 *
554 * RETURN VALUE:
555 * 0 if either _DSW or _PSW has been successfully executed
556 * 0 if neither _DSW nor _PSW has been found
557 * -ENODEV if the execution of either _DSW or _PSW has failed
558 */
559int acpi_device_sleep_wake(struct acpi_device *dev,
560 int enable, int sleep_state, int dev_state)
561{
562 union acpi_object in_arg[3];
563 struct acpi_object_list arg_list = { 3, in_arg };
564 acpi_status status = AE_OK;
565
566 /*
567 * Try to execute _DSW first.
568 *
569 * Three agruments are needed for the _DSW object:
570 * Argument 0: enable/disable the wake capabilities
571 * Argument 1: target system state
572 * Argument 2: target device state
573 * When _DSW object is called to disable the wake capabilities, maybe
574 * the first argument is filled. The values of the other two agruments
575 * are meaningless.
576 */
577 in_arg[0].type = ACPI_TYPE_INTEGER;
578 in_arg[0].integer.value = enable;
579 in_arg[1].type = ACPI_TYPE_INTEGER;
580 in_arg[1].integer.value = sleep_state;
581 in_arg[2].type = ACPI_TYPE_INTEGER;
582 in_arg[2].integer.value = dev_state;
583 status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL);
584 if (ACPI_SUCCESS(status)) {
585 return 0;
586 } else if (status != AE_NOT_FOUND) {
587 printk(KERN_ERR PREFIX "_DSW execution failed\n");
588 dev->wakeup.flags.valid = 0;
589 return -ENODEV;
590 }
591
592 /* Execute _PSW */
593 arg_list.count = 1;
594 in_arg[0].integer.value = enable;
595 status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL);
596 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
597 printk(KERN_ERR PREFIX "_PSW execution failed\n");
598 dev->wakeup.flags.valid = 0;
599 return -ENODEV;
600 }
601
602 return 0;
603}
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605/*
606 * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
607 * 1. Power on the power resources required for the wakeup device
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200608 * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
609 * State Wake) for the device, if present
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 */
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200611int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100613 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (!dev || !dev->wakeup.flags.valid)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200616 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200618 mutex_lock(&acpi_device_lock);
619
620 if (dev->wakeup.prepare_count++)
621 goto out;
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200622
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100623 err = acpi_power_on_list(&dev->wakeup.resources);
624 if (err) {
625 dev_err(&dev->dev, "Cannot turn wakeup power resources on\n");
626 dev->wakeup.flags.valid = 0;
627 } else {
628 /*
629 * Passing 3 as the third argument below means the device may be
630 * put into arbitrary power state afterward.
631 */
632 err = acpi_device_sleep_wake(dev, 1, sleep_state, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200634 if (err)
635 dev->wakeup.prepare_count = 0;
636
637 out:
638 mutex_unlock(&acpi_device_lock);
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200639 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
642/*
643 * Shutdown a wakeup device, counterpart of above method
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200644 * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
645 * State Wake) for the device, if present
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 * 2. Shutdown down the power resources
647 */
Len Brown4be44fc2005-08-05 00:44:28 -0400648int acpi_disable_wakeup_device_power(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100650 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 if (!dev || !dev->wakeup.flags.valid)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200653 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200655 mutex_lock(&acpi_device_lock);
656
657 if (--dev->wakeup.prepare_count > 0)
658 goto out;
659
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200660 /*
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200661 * Executing the code below even if prepare_count is already zero when
662 * the function is called may be useful, for example for initialisation.
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200663 */
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200664 if (dev->wakeup.prepare_count < 0)
665 dev->wakeup.prepare_count = 0;
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200666
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200667 err = acpi_device_sleep_wake(dev, 0, 0, 0);
668 if (err)
669 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100671 err = acpi_power_off_list(&dev->wakeup.resources);
672 if (err) {
673 dev_err(&dev->dev, "Cannot turn wakeup power resources off\n");
674 dev->wakeup.flags.valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
676
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200677 out:
678 mutex_unlock(&acpi_device_lock);
679 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100682int acpi_power_get_inferred_state(struct acpi_device *device, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Len Brown4be44fc2005-08-05 00:44:28 -0400684 int result = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400685 int list_state = 0;
686 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100688 if (!device || !state)
Patrick Mocheld550d982006-06-27 00:41:40 -0400689 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 /*
692 * We know a device's inferred power state when all the resources
693 * required for a given D-state are 'on'.
694 */
Rafael J. Wysocki38c92ff2012-05-20 13:58:00 +0200695 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100696 struct list_head *list = &device->power.states[i].resources;
697
698 if (list_empty(list))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 continue;
700
701 result = acpi_power_get_list_state(list, &list_state);
702 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400703 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100706 *state = i;
Patrick Mocheld550d982006-06-27 00:41:40 -0400707 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709 }
710
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100711 *state = ACPI_STATE_D3;
Patrick Mocheld550d982006-06-27 00:41:40 -0400712 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
Rafael J. Wysocki30d3df42010-11-25 00:06:55 +0100715int acpi_power_on_resources(struct acpi_device *device, int state)
716{
Rafael J. Wysocki87e753b2013-01-22 12:56:16 +0100717 if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3_HOT)
Rafael J. Wysocki30d3df42010-11-25 00:06:55 +0100718 return -EINVAL;
719
720 return acpi_power_on_list(&device->power.states[state].resources);
721}
722
Len Brown4be44fc2005-08-05 00:44:28 -0400723int acpi_power_transition(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +0200725 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Zhang Rui3ebc81b2012-03-29 14:09:38 +0800727 if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
Patrick Mocheld550d982006-06-27 00:41:40 -0400728 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100730 if (device->power.state == state || !device->flags.power_manageable)
Rafael J. Wysocki212967c2010-11-25 00:02:36 +0100731 return 0;
732
Len Brown4be44fc2005-08-05 00:44:28 -0400733 if ((device->power.state < ACPI_STATE_D0)
Zhang Rui3ebc81b2012-03-29 14:09:38 +0800734 || (device->power.state > ACPI_STATE_D3_COLD))
Patrick Mocheld550d982006-06-27 00:41:40 -0400735 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 /* TBD: Resources must be ordered. */
738
739 /*
740 * First we reference all power resources required in the target list
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100741 * (e.g. so the device doesn't lose power while transitioning). Then,
742 * we dereference all power resources used in the current list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 */
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +0200744 if (state < ACPI_STATE_D3_COLD)
745 result = acpi_power_on_list(
746 &device->power.states[state].resources);
747
748 if (!result && device->power.state < ACPI_STATE_D3_COLD)
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100749 acpi_power_off_list(
750 &device->power.states[device->power.state].resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100752 /* We shouldn't change the state unless the above operations succeed. */
753 device->power.state = result ? ACPI_STATE_UNKNOWN : state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Patrick Mocheld550d982006-06-27 00:41:40 -0400755 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100758static void acpi_release_power_resource(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100760 struct acpi_device *device = to_acpi_device(dev);
761 struct acpi_power_resource *resource;
762
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100763 resource = container_of(device, struct acpi_power_resource, device);
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100764
765 mutex_lock(&power_resource_list_lock);
766 list_del(&resource->list_node);
767 mutex_unlock(&power_resource_list_lock);
768
769 acpi_free_ids(device);
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100770 kfree(resource);
771}
772
Rafael J. Wysockib1c0f992013-01-24 12:50:09 +0100773static ssize_t acpi_power_in_use_show(struct device *dev,
774 struct device_attribute *attr,
775 char *buf) {
776 struct acpi_power_resource *resource;
777
778 resource = to_power_resource(to_acpi_device(dev));
779 return sprintf(buf, "%u\n", !!resource->ref_count);
780}
781static DEVICE_ATTR(resource_in_use, 0444, acpi_power_in_use_show, NULL);
782
783static void acpi_power_sysfs_remove(struct acpi_device *device)
784{
785 device_remove_file(&device->dev, &dev_attr_resource_in_use);
786}
787
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100788int acpi_add_power_resource(acpi_handle handle)
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100789{
790 struct acpi_power_resource *resource;
791 struct acpi_device *device = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400792 union acpi_object acpi_object;
793 struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100794 acpi_status status;
795 int state, result = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100797 acpi_bus_get_device(handle, &device);
798 if (device)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100799 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100801 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (!resource)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100803 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100805 device = &resource->device;
806 acpi_init_device_object(device, handle, ACPI_BUS_TYPE_POWER,
807 ACPI_STA_DEFAULT);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500808 mutex_init(&resource->resource_lock);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100809 INIT_LIST_HEAD(&resource->dependent);
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100810 resource->name = device->pnp.bus_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
812 strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
Rafael J. Wysocki722c9292013-01-17 14:11:06 +0100813 device->power.state = ACPI_STATE_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 /* Evalute the object to get the system level and resource order. */
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100816 status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
817 if (ACPI_FAILURE(status))
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100818 goto err;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 resource->system_level = acpi_object.power_resource.system_level;
821 resource->order = acpi_object.power_resource.resource_order;
822
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100823 result = acpi_power_get_state(handle, &state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 if (result)
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100825 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Len Brown4be44fc2005-08-05 00:44:28 -0400827 printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device),
Alexey Starikovskiyc35923b2007-10-22 14:19:09 +0400828 acpi_device_bid(device), state ? "on" : "off");
Len Brown4be44fc2005-08-05 00:44:28 -0400829
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100830 device->flags.match_driver = true;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100831 result = acpi_device_add(device, acpi_release_power_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (result)
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100833 goto err;
Len Brown4be44fc2005-08-05 00:44:28 -0400834
Rafael J. Wysockib1c0f992013-01-24 12:50:09 +0100835 if (!device_create_file(&device->dev, &dev_attr_resource_in_use))
836 device->remove = acpi_power_sysfs_remove;
837
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100838 mutex_lock(&power_resource_list_lock);
839 list_add(&resource->list_node, &acpi_power_resource_list);
840 mutex_unlock(&power_resource_list_lock);
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100841 acpi_device_add_finalize(device);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100842 return 0;
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100843
844 err:
845 acpi_release_power_resource(&device->dev);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100846 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
848
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100849#ifdef CONFIG_ACPI_SLEEP
850void acpi_resume_power_resources(void)
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500851{
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200852 struct acpi_power_resource *resource;
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500853
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100854 mutex_lock(&power_resource_list_lock);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500855
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100856 list_for_each_entry(resource, &acpi_power_resource_list, list_node) {
857 int result, state;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200858
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100859 mutex_lock(&resource->resource_lock);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500860
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100861 result = acpi_power_get_state(resource->device.handle, &state);
Rafael J. Wysocki660b1112013-01-25 21:51:57 +0100862 if (result)
863 continue;
864
865 if (state == ACPI_POWER_RESOURCE_STATE_OFF
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100866 && resource->ref_count) {
867 dev_info(&resource->device.dev, "Turning ON\n");
868 __acpi_power_on(resource);
Rafael J. Wysocki660b1112013-01-25 21:51:57 +0100869 } else if (state == ACPI_POWER_RESOURCE_STATE_ON
870 && !resource->ref_count) {
871 dev_info(&resource->device.dev, "Turning OFF\n");
872 __acpi_power_off(resource);
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100873 }
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500874
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100875 mutex_unlock(&resource->resource_lock);
876 }
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500877
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100878 mutex_unlock(&power_resource_list_lock);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500879}
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200880#endif