blob: ad7da686e6e6f8d9745a4edf866e174b99b08538 [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>
Lv Zheng8b484632013-12-03 08:49:16 +080045#include <linux/acpi.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
Len Brown4be44fc2005-08-05 00:44:28 -040061struct acpi_power_resource {
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010062 struct acpi_device device;
Rafael J. Wysocki781d7372013-01-17 14:11:06 +010063 struct list_head list_node;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010064 char *name;
Len Brown4be44fc2005-08-05 00:44:28 -040065 u32 system_level;
66 u32 order;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +020067 unsigned int ref_count;
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +010068 bool wakeup_enabled;
Konstantin Karasyov0a613902007-02-16 01:47:06 -050069 struct mutex resource_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070};
71
Rafael J. Wysocki0b224522013-01-17 14:11:06 +010072struct acpi_power_resource_entry {
73 struct list_head node;
74 struct acpi_power_resource *resource;
75};
76
Rafael J. Wysocki781d7372013-01-17 14:11:06 +010077static LIST_HEAD(acpi_power_resource_list);
78static DEFINE_MUTEX(power_resource_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/* --------------------------------------------------------------------------
81 Power Resource Management
82 -------------------------------------------------------------------------- */
83
Rafael J. Wysockib1c0f992013-01-24 12:50:09 +010084static inline
85struct acpi_power_resource *to_power_resource(struct acpi_device *device)
86{
87 return container_of(device, struct acpi_power_resource, device);
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. Wysockib1c0f992013-01-24 12:50:09 +010097 return to_power_resource(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100100static int 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)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100107 return -EINVAL;
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100108
109 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
110 if (!entry)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100111 return -ENOMEM;
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100112
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);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100120 return 0;
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100121 }
122 }
123 list_add_tail(&entry->node, list);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100124 return 0;
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100125}
126
127void acpi_power_resources_list_free(struct list_head *list)
128{
129 struct acpi_power_resource_entry *entry, *e;
130
131 list_for_each_entry_safe(entry, e, list, node) {
132 list_del(&entry->node);
133 kfree(entry);
134 }
135}
136
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100137int acpi_extract_power_resources(union acpi_object *package, unsigned int start,
138 struct list_head *list)
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100139{
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100140 unsigned int i;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100141 int err = 0;
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100142
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) {
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100148 err = -ENODATA;
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100149 break;
150 }
151 rhandle = element->reference.handle;
152 if (!rhandle) {
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100153 err = -ENODEV;
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100154 break;
155 }
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100156 err = acpi_add_power_resource(rhandle);
157 if (err)
158 break;
159
160 err = acpi_power_resources_list_add(rhandle, list);
161 if (err)
162 break;
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100163 }
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100164 if (err)
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100165 acpi_power_resources_list_free(list);
166
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100167 return err;
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100168}
169
Zhao Yakuia51e1452008-08-11 14:55:05 +0800170static int acpi_power_get_state(acpi_handle handle, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Len Brown4be44fc2005-08-05 00:44:28 -0400172 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400173 unsigned long long sta = 0;
Lin Ming60a4ce72008-12-16 17:02:22 +0800174 char node_name[5];
175 struct acpi_buffer buffer = { sizeof(node_name), node_name };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Zhao Yakuia51e1452008-08-11 14:55:05 +0800178 if (!handle || !state)
Patrick Mocheld550d982006-06-27 00:41:40 -0400179 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Zhao Yakuia51e1452008-08-11 14:55:05 +0800181 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400183 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Alexey Starikovskiyc35923b2007-10-22 14:19:09 +0400185 *state = (sta & 0x01)?ACPI_POWER_RESOURCE_STATE_ON:
186 ACPI_POWER_RESOURCE_STATE_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Lin Ming60a4ce72008-12-16 17:02:22 +0800188 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n",
Lin Ming60a4ce72008-12-16 17:02:22 +0800191 node_name,
Zhao Yakuib1b57fb2008-10-27 16:04:53 +0800192 *state ? "on" : "off"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Patrick Mocheld550d982006-06-27 00:41:40 -0400194 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100197static int acpi_power_get_list_state(struct list_head *list, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100199 struct acpi_power_resource_entry *entry;
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100200 int cur_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 if (!list || !state)
Patrick Mocheld550d982006-06-27 00:41:40 -0400203 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 /* The state of the list is 'on' IFF all resources are 'on'. */
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100206 list_for_each_entry(entry, list, node) {
207 struct acpi_power_resource *resource = entry->resource;
208 acpi_handle handle = resource->device.handle;
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100209 int result;
210
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100211 mutex_lock(&resource->resource_lock);
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100212 result = acpi_power_get_state(handle, &cur_state);
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100213 mutex_unlock(&resource->resource_lock);
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100214 if (result)
215 return result;
216
217 if (cur_state != ACPI_POWER_RESOURCE_STATE_ON)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 break;
219 }
220
221 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n",
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100222 cur_state ? "on" : "off"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100224 *state = cur_state;
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100225 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200228static int __acpi_power_on(struct acpi_power_resource *resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Len Brown4be44fc2005-08-05 00:44:28 -0400230 acpi_status status = AE_OK;
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500231
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100232 status = acpi_evaluate_object(resource->device.handle, "_ON", NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400234 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200236 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned on\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400237 resource->name));
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200238
Patrick Mocheld550d982006-06-27 00:41:40 -0400239 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100242static int acpi_power_on_unlocked(struct acpi_power_resource *resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100244 int result = 0;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200245
246 if (resource->ref_count++) {
247 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Mika Westerberg10a0b612013-07-05 12:15:56 +0300248 "Power resource [%s] already on\n",
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200249 resource->name));
250 } else {
251 result = __acpi_power_on(resource);
Rafael J. Wysocki41863fc2013-10-16 23:05:42 +0200252 if (result)
Rafael J. Wysocki12b3b5a2010-11-25 00:03:32 +0100253 resource->ref_count--;
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500254 }
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100255 return result;
256}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100258static int acpi_power_on(struct acpi_power_resource *resource)
259{
260 int result;
261
262 mutex_lock(&resource->resource_lock);
263 result = acpi_power_on_unlocked(resource);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500264 mutex_unlock(&resource->resource_lock);
Rafael J. Wysocki12b3b5a2010-11-25 00:03:32 +0100265 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
Rafael J. Wysocki660b1112013-01-25 21:51:57 +0100268static int __acpi_power_off(struct acpi_power_resource *resource)
269{
270 acpi_status status;
271
272 status = acpi_evaluate_object(resource->device.handle, "_OFF",
273 NULL, NULL);
274 if (ACPI_FAILURE(status))
275 return -ENODEV;
276
277 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned off\n",
278 resource->name));
279 return 0;
280}
281
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100282static int acpi_power_off_unlocked(struct acpi_power_resource *resource)
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200283{
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100284 int result = 0;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200285
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200286 if (!resource->ref_count) {
287 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Mika Westerberg10a0b612013-07-05 12:15:56 +0300288 "Power resource [%s] already off\n",
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200289 resource->name));
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100290 return 0;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200291 }
292
293 if (--resource->ref_count) {
294 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
295 "Power resource [%s] still in use\n",
296 resource->name));
Rafael J. Wysocki660b1112013-01-25 21:51:57 +0100297 } else {
298 result = __acpi_power_off(resource);
299 if (result)
300 resource->ref_count++;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200301 }
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100302 return result;
303}
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200304
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100305static int acpi_power_off(struct acpi_power_resource *resource)
306{
307 int result;
308
309 mutex_lock(&resource->resource_lock);
310 result = acpi_power_off_unlocked(resource);
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200311 mutex_unlock(&resource->resource_lock);
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200312 return result;
313}
314
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100315static int acpi_power_off_list(struct list_head *list)
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100316{
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100317 struct acpi_power_resource_entry *entry;
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100318 int result = 0;
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100319
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100320 list_for_each_entry_reverse(entry, list, node) {
321 result = acpi_power_off(entry->resource);
322 if (result)
323 goto err;
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100324 }
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100325 return 0;
326
327 err:
328 list_for_each_entry_continue(entry, list, node)
329 acpi_power_on(entry->resource);
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100330
331 return result;
332}
333
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100334static int acpi_power_on_list(struct list_head *list)
335{
336 struct acpi_power_resource_entry *entry;
337 int result = 0;
338
339 list_for_each_entry(entry, list, node) {
340 result = acpi_power_on(entry->resource);
341 if (result)
342 goto err;
343 }
344 return 0;
345
346 err:
347 list_for_each_entry_continue_reverse(entry, list, node)
348 acpi_power_off(entry->resource);
349
350 return result;
351}
352
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100353static struct attribute *attrs[] = {
354 NULL,
355};
356
357static struct attribute_group attr_groups[] = {
358 [ACPI_STATE_D0] = {
359 .name = "power_resources_D0",
360 .attrs = attrs,
361 },
362 [ACPI_STATE_D1] = {
363 .name = "power_resources_D1",
364 .attrs = attrs,
365 },
366 [ACPI_STATE_D2] = {
367 .name = "power_resources_D2",
368 .attrs = attrs,
369 },
370 [ACPI_STATE_D3_HOT] = {
371 .name = "power_resources_D3hot",
372 .attrs = attrs,
373 },
374};
375
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200376static struct attribute_group wakeup_attr_group = {
377 .name = "power_resources_wakeup",
378 .attrs = attrs,
379};
380
381static void acpi_power_hide_list(struct acpi_device *adev,
382 struct list_head *resources,
383 struct attribute_group *attr_group)
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100384{
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100385 struct acpi_power_resource_entry *entry;
386
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200387 if (list_empty(resources))
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100388 return;
389
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200390 list_for_each_entry_reverse(entry, resources, node) {
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100391 struct acpi_device *res_dev = &entry->resource->device;
392
393 sysfs_remove_link_from_group(&adev->dev.kobj,
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200394 attr_group->name,
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100395 dev_name(&res_dev->dev));
396 }
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200397 sysfs_remove_group(&adev->dev.kobj, attr_group);
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100398}
399
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200400static void acpi_power_expose_list(struct acpi_device *adev,
401 struct list_head *resources,
402 struct attribute_group *attr_group)
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100403{
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100404 struct acpi_power_resource_entry *entry;
405 int ret;
406
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200407 if (list_empty(resources))
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100408 return;
409
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200410 ret = sysfs_create_group(&adev->dev.kobj, attr_group);
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100411 if (ret)
412 return;
413
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200414 list_for_each_entry(entry, resources, node) {
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100415 struct acpi_device *res_dev = &entry->resource->device;
416
417 ret = sysfs_add_link_to_group(&adev->dev.kobj,
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200418 attr_group->name,
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100419 &res_dev->dev.kobj,
420 dev_name(&res_dev->dev));
421 if (ret) {
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200422 acpi_power_hide_list(adev, resources, attr_group);
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100423 break;
424 }
425 }
426}
427
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200428static void acpi_power_expose_hide(struct acpi_device *adev,
429 struct list_head *resources,
430 struct attribute_group *attr_group,
431 bool expose)
432{
433 if (expose)
434 acpi_power_expose_list(adev, resources, attr_group);
435 else
436 acpi_power_hide_list(adev, resources, attr_group);
437}
438
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100439void acpi_power_add_remove_device(struct acpi_device *adev, bool add)
440{
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100441 int state;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100442
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200443 if (adev->wakeup.flags.valid)
444 acpi_power_expose_hide(adev, &adev->wakeup.resources,
445 &wakeup_attr_group, add);
446
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100447 if (!adev->power.flags.power_resources)
448 return;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100449
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200450 for (state = ACPI_STATE_D0; state <= ACPI_STATE_D3_HOT; state++)
451 acpi_power_expose_hide(adev,
452 &adev->power.states[state].resources,
453 &attr_groups[state], add);
Lin Ming0090def2012-03-29 14:09:39 +0800454}
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100455
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100456int acpi_power_wakeup_list_init(struct list_head *list, int *system_level_p)
Rafael J. Wysocki0596a522013-01-17 14:11:07 +0100457{
458 struct acpi_power_resource_entry *entry;
459 int system_level = 5;
460
461 list_for_each_entry(entry, list, node) {
462 struct acpi_power_resource *resource = entry->resource;
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100463 acpi_handle handle = resource->device.handle;
464 int result;
465 int state;
Rafael J. Wysocki0596a522013-01-17 14:11:07 +0100466
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100467 mutex_lock(&resource->resource_lock);
468
469 result = acpi_power_get_state(handle, &state);
470 if (result) {
471 mutex_unlock(&resource->resource_lock);
472 return result;
473 }
474 if (state == ACPI_POWER_RESOURCE_STATE_ON) {
475 resource->ref_count++;
476 resource->wakeup_enabled = true;
477 }
Rafael J. Wysocki0596a522013-01-17 14:11:07 +0100478 if (system_level > resource->system_level)
479 system_level = resource->system_level;
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100480
481 mutex_unlock(&resource->resource_lock);
Rafael J. Wysocki0596a522013-01-17 14:11:07 +0100482 }
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100483 *system_level_p = system_level;
484 return 0;
Rafael J. Wysocki0596a522013-01-17 14:11:07 +0100485}
486
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100487/* --------------------------------------------------------------------------
488 Device Power Management
489 -------------------------------------------------------------------------- */
Lin Ming0090def2012-03-29 14:09:39 +0800490
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200491/**
492 * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in
493 * ACPI 3.0) _PSW (Power State Wake)
494 * @dev: Device to handle.
495 * @enable: 0 - disable, 1 - enable the wake capabilities of the device.
496 * @sleep_state: Target sleep state of the system.
497 * @dev_state: Target power state of the device.
498 *
499 * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
500 * State Wake) for the device, if present. On failure reset the device's
501 * wakeup.flags.valid flag.
502 *
503 * RETURN VALUE:
504 * 0 if either _DSW or _PSW has been successfully executed
505 * 0 if neither _DSW nor _PSW has been found
506 * -ENODEV if the execution of either _DSW or _PSW has failed
507 */
508int acpi_device_sleep_wake(struct acpi_device *dev,
509 int enable, int sleep_state, int dev_state)
510{
511 union acpi_object in_arg[3];
512 struct acpi_object_list arg_list = { 3, in_arg };
513 acpi_status status = AE_OK;
514
515 /*
516 * Try to execute _DSW first.
517 *
518 * Three agruments are needed for the _DSW object:
519 * Argument 0: enable/disable the wake capabilities
520 * Argument 1: target system state
521 * Argument 2: target device state
522 * When _DSW object is called to disable the wake capabilities, maybe
523 * the first argument is filled. The values of the other two agruments
524 * are meaningless.
525 */
526 in_arg[0].type = ACPI_TYPE_INTEGER;
527 in_arg[0].integer.value = enable;
528 in_arg[1].type = ACPI_TYPE_INTEGER;
529 in_arg[1].integer.value = sleep_state;
530 in_arg[2].type = ACPI_TYPE_INTEGER;
531 in_arg[2].integer.value = dev_state;
532 status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL);
533 if (ACPI_SUCCESS(status)) {
534 return 0;
535 } else if (status != AE_NOT_FOUND) {
536 printk(KERN_ERR PREFIX "_DSW execution failed\n");
537 dev->wakeup.flags.valid = 0;
538 return -ENODEV;
539 }
540
541 /* Execute _PSW */
Jiang Liu0db98202013-06-29 00:24:39 +0800542 status = acpi_execute_simple_method(dev->handle, "_PSW", enable);
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200543 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
544 printk(KERN_ERR PREFIX "_PSW execution failed\n");
545 dev->wakeup.flags.valid = 0;
546 return -ENODEV;
547 }
548
549 return 0;
550}
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552/*
553 * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
554 * 1. Power on the power resources required for the wakeup device
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200555 * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
556 * State Wake) for the device, if present
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 */
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200558int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100560 struct acpi_power_resource_entry *entry;
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100561 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (!dev || !dev->wakeup.flags.valid)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200564 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200566 mutex_lock(&acpi_device_lock);
567
568 if (dev->wakeup.prepare_count++)
569 goto out;
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200570
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100571 list_for_each_entry(entry, &dev->wakeup.resources, node) {
572 struct acpi_power_resource *resource = entry->resource;
573
574 mutex_lock(&resource->resource_lock);
575
576 if (!resource->wakeup_enabled) {
577 err = acpi_power_on_unlocked(resource);
578 if (!err)
579 resource->wakeup_enabled = true;
580 }
581
582 mutex_unlock(&resource->resource_lock);
583
584 if (err) {
585 dev_err(&dev->dev,
586 "Cannot turn wakeup power resources on\n");
587 dev->wakeup.flags.valid = 0;
588 goto out;
589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100591 /*
592 * Passing 3 as the third argument below means the device may be
593 * put into arbitrary power state afterward.
594 */
595 err = acpi_device_sleep_wake(dev, 1, sleep_state, 3);
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200596 if (err)
597 dev->wakeup.prepare_count = 0;
598
599 out:
600 mutex_unlock(&acpi_device_lock);
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200601 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603
604/*
605 * Shutdown a wakeup device, counterpart of above method
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200606 * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
607 * State Wake) for the device, if present
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 * 2. Shutdown down the power resources
609 */
Len Brown4be44fc2005-08-05 00:44:28 -0400610int acpi_disable_wakeup_device_power(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100612 struct acpi_power_resource_entry *entry;
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100613 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 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 > 0)
621 goto out;
622
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200623 /*
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200624 * Executing the code below even if prepare_count is already zero when
625 * the function is called may be useful, for example for initialisation.
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200626 */
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200627 if (dev->wakeup.prepare_count < 0)
628 dev->wakeup.prepare_count = 0;
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200629
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200630 err = acpi_device_sleep_wake(dev, 0, 0, 0);
631 if (err)
632 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100634 list_for_each_entry(entry, &dev->wakeup.resources, node) {
635 struct acpi_power_resource *resource = entry->resource;
636
637 mutex_lock(&resource->resource_lock);
638
639 if (resource->wakeup_enabled) {
640 err = acpi_power_off_unlocked(resource);
641 if (!err)
642 resource->wakeup_enabled = false;
643 }
644
645 mutex_unlock(&resource->resource_lock);
646
647 if (err) {
648 dev_err(&dev->dev,
649 "Cannot turn wakeup power resources off\n");
650 dev->wakeup.flags.valid = 0;
651 break;
652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
654
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200655 out:
656 mutex_unlock(&acpi_device_lock);
657 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100660int acpi_power_get_inferred_state(struct acpi_device *device, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Len Brown4be44fc2005-08-05 00:44:28 -0400662 int result = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400663 int list_state = 0;
664 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100666 if (!device || !state)
Patrick Mocheld550d982006-06-27 00:41:40 -0400667 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 /*
670 * We know a device's inferred power state when all the resources
671 * required for a given D-state are 'on'.
672 */
Rafael J. Wysocki38c92ff2012-05-20 13:58:00 +0200673 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100674 struct list_head *list = &device->power.states[i].resources;
675
676 if (list_empty(list))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 continue;
678
679 result = acpi_power_get_list_state(list, &list_state);
680 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400681 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100684 *state = i;
Patrick Mocheld550d982006-06-27 00:41:40 -0400685 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
687 }
688
Rafael J. Wysocki8ad928d2013-07-30 14:36:20 +0200689 *state = ACPI_STATE_D3_COLD;
Patrick Mocheld550d982006-06-27 00:41:40 -0400690 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691}
692
Rafael J. Wysocki30d3df412010-11-25 00:06:55 +0100693int acpi_power_on_resources(struct acpi_device *device, int state)
694{
Rafael J. Wysocki87e753b2013-01-22 12:56:16 +0100695 if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3_HOT)
Rafael J. Wysocki30d3df412010-11-25 00:06:55 +0100696 return -EINVAL;
697
698 return acpi_power_on_list(&device->power.states[state].resources);
699}
700
Len Brown4be44fc2005-08-05 00:44:28 -0400701int acpi_power_transition(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +0200703 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Zhang Rui3ebc81b2012-03-29 14:09:38 +0800705 if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
Patrick Mocheld550d982006-06-27 00:41:40 -0400706 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100708 if (device->power.state == state || !device->flags.power_manageable)
Rafael J. Wysocki212967c2010-11-25 00:02:36 +0100709 return 0;
710
Len Brown4be44fc2005-08-05 00:44:28 -0400711 if ((device->power.state < ACPI_STATE_D0)
Zhang Rui3ebc81b2012-03-29 14:09:38 +0800712 || (device->power.state > ACPI_STATE_D3_COLD))
Patrick Mocheld550d982006-06-27 00:41:40 -0400713 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 /* TBD: Resources must be ordered. */
716
717 /*
718 * First we reference all power resources required in the target list
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100719 * (e.g. so the device doesn't lose power while transitioning). Then,
720 * we dereference all power resources used in the current list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 */
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +0200722 if (state < ACPI_STATE_D3_COLD)
723 result = acpi_power_on_list(
724 &device->power.states[state].resources);
725
726 if (!result && device->power.state < ACPI_STATE_D3_COLD)
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100727 acpi_power_off_list(
728 &device->power.states[device->power.state].resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100730 /* We shouldn't change the state unless the above operations succeed. */
731 device->power.state = result ? ACPI_STATE_UNKNOWN : state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Patrick Mocheld550d982006-06-27 00:41:40 -0400733 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
735
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100736static void acpi_release_power_resource(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100738 struct acpi_device *device = to_acpi_device(dev);
739 struct acpi_power_resource *resource;
740
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100741 resource = container_of(device, struct acpi_power_resource, device);
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100742
743 mutex_lock(&power_resource_list_lock);
744 list_del(&resource->list_node);
745 mutex_unlock(&power_resource_list_lock);
746
Toshi Kanic0af4172013-03-04 21:30:42 +0000747 acpi_free_pnp_ids(&device->pnp);
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100748 kfree(resource);
749}
750
Rafael J. Wysockib1c0f992013-01-24 12:50:09 +0100751static ssize_t acpi_power_in_use_show(struct device *dev,
752 struct device_attribute *attr,
753 char *buf) {
754 struct acpi_power_resource *resource;
755
756 resource = to_power_resource(to_acpi_device(dev));
757 return sprintf(buf, "%u\n", !!resource->ref_count);
758}
759static DEVICE_ATTR(resource_in_use, 0444, acpi_power_in_use_show, NULL);
760
761static void acpi_power_sysfs_remove(struct acpi_device *device)
762{
763 device_remove_file(&device->dev, &dev_attr_resource_in_use);
764}
765
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100766int acpi_add_power_resource(acpi_handle handle)
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100767{
768 struct acpi_power_resource *resource;
769 struct acpi_device *device = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400770 union acpi_object acpi_object;
771 struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100772 acpi_status status;
773 int state, result = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100775 acpi_bus_get_device(handle, &device);
776 if (device)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100777 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100779 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (!resource)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100781 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100783 device = &resource->device;
784 acpi_init_device_object(device, handle, ACPI_BUS_TYPE_POWER,
785 ACPI_STA_DEFAULT);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500786 mutex_init(&resource->resource_lock);
Rafael J. Wysocki6ee22e9d2013-06-19 00:44:45 +0200787 INIT_LIST_HEAD(&resource->list_node);
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100788 resource->name = device->pnp.bus_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
790 strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
Rafael J. Wysocki722c9292013-01-17 14:11:06 +0100791 device->power.state = ACPI_STATE_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 /* Evalute the object to get the system level and resource order. */
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100794 status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
795 if (ACPI_FAILURE(status))
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100796 goto err;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 resource->system_level = acpi_object.power_resource.system_level;
799 resource->order = acpi_object.power_resource.resource_order;
800
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100801 result = acpi_power_get_state(handle, &state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (result)
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100803 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Len Brown4be44fc2005-08-05 00:44:28 -0400805 printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device),
Alexey Starikovskiyc35923b2007-10-22 14:19:09 +0400806 acpi_device_bid(device), state ? "on" : "off");
Len Brown4be44fc2005-08-05 00:44:28 -0400807
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100808 device->flags.match_driver = true;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100809 result = acpi_device_add(device, acpi_release_power_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (result)
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100811 goto err;
Len Brown4be44fc2005-08-05 00:44:28 -0400812
Rafael J. Wysockib1c0f992013-01-24 12:50:09 +0100813 if (!device_create_file(&device->dev, &dev_attr_resource_in_use))
814 device->remove = acpi_power_sysfs_remove;
815
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100816 mutex_lock(&power_resource_list_lock);
817 list_add(&resource->list_node, &acpi_power_resource_list);
818 mutex_unlock(&power_resource_list_lock);
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100819 acpi_device_add_finalize(device);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100820 return 0;
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100821
822 err:
823 acpi_release_power_resource(&device->dev);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100824 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
826
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100827#ifdef CONFIG_ACPI_SLEEP
828void acpi_resume_power_resources(void)
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500829{
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200830 struct acpi_power_resource *resource;
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500831
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100832 mutex_lock(&power_resource_list_lock);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500833
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100834 list_for_each_entry(resource, &acpi_power_resource_list, list_node) {
835 int result, state;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200836
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100837 mutex_lock(&resource->resource_lock);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500838
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100839 result = acpi_power_get_state(resource->device.handle, &state);
Lan Tianyud7d49012013-10-15 19:48:11 +0800840 if (result) {
841 mutex_unlock(&resource->resource_lock);
Rafael J. Wysocki660b1112013-01-25 21:51:57 +0100842 continue;
Lan Tianyud7d49012013-10-15 19:48:11 +0800843 }
Rafael J. Wysocki660b1112013-01-25 21:51:57 +0100844
845 if (state == ACPI_POWER_RESOURCE_STATE_OFF
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100846 && resource->ref_count) {
847 dev_info(&resource->device.dev, "Turning ON\n");
848 __acpi_power_on(resource);
Rafael J. Wysocki660b1112013-01-25 21:51:57 +0100849 } else if (state == ACPI_POWER_RESOURCE_STATE_ON
850 && !resource->ref_count) {
851 dev_info(&resource->device.dev, "Turning OFF\n");
852 __acpi_power_off(resource);
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100853 }
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500854
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100855 mutex_unlock(&resource->resource_lock);
856 }
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500857
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100858 mutex_unlock(&power_resource_list_lock);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500859}
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200860#endif