blob: 946720a4db57d7c4b745f6a5a148ba9da29d77c4 [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. Wysockib1c0f992013-01-24 12:50:09 +010090static inline
91struct acpi_power_resource *to_power_resource(struct acpi_device *device)
92{
93 return container_of(device, struct acpi_power_resource, device);
94}
95
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010096static struct acpi_power_resource *acpi_power_get_context(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010098 struct acpi_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100100 if (acpi_bus_get_device(handle, &device))
101 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Rafael J. Wysockib1c0f992013-01-24 12:50:09 +0100103 return to_power_resource(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100106static int acpi_power_resources_list_add(acpi_handle handle,
107 struct list_head *list)
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100108{
109 struct acpi_power_resource *resource = acpi_power_get_context(handle);
110 struct acpi_power_resource_entry *entry;
111
112 if (!resource || !list)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100113 return -EINVAL;
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100114
115 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
116 if (!entry)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100117 return -ENOMEM;
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100118
119 entry->resource = resource;
120 if (!list_empty(list)) {
121 struct acpi_power_resource_entry *e;
122
123 list_for_each_entry(e, list, node)
124 if (e->resource->order > resource->order) {
125 list_add_tail(&entry->node, &e->node);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100126 return 0;
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100127 }
128 }
129 list_add_tail(&entry->node, list);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100130 return 0;
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100131}
132
133void acpi_power_resources_list_free(struct list_head *list)
134{
135 struct acpi_power_resource_entry *entry, *e;
136
137 list_for_each_entry_safe(entry, e, list, node) {
138 list_del(&entry->node);
139 kfree(entry);
140 }
141}
142
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100143int acpi_extract_power_resources(union acpi_object *package, unsigned int start,
144 struct list_head *list)
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100145{
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100146 unsigned int i;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100147 int err = 0;
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100148
149 for (i = start; i < package->package.count; i++) {
150 union acpi_object *element = &package->package.elements[i];
151 acpi_handle rhandle;
152
153 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100154 err = -ENODATA;
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100155 break;
156 }
157 rhandle = element->reference.handle;
158 if (!rhandle) {
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100159 err = -ENODEV;
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100160 break;
161 }
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100162 err = acpi_add_power_resource(rhandle);
163 if (err)
164 break;
165
166 err = acpi_power_resources_list_add(rhandle, list);
167 if (err)
168 break;
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100169 }
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100170 if (err)
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100171 acpi_power_resources_list_free(list);
172
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100173 return err;
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100174}
175
Zhao Yakuia51e1452008-08-11 14:55:05 +0800176static int acpi_power_get_state(acpi_handle handle, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Len Brown4be44fc2005-08-05 00:44:28 -0400178 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400179 unsigned long long sta = 0;
Lin Ming60a4ce72008-12-16 17:02:22 +0800180 char node_name[5];
181 struct acpi_buffer buffer = { sizeof(node_name), node_name };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Zhao Yakuia51e1452008-08-11 14:55:05 +0800184 if (!handle || !state)
Patrick Mocheld550d982006-06-27 00:41:40 -0400185 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Zhao Yakuia51e1452008-08-11 14:55:05 +0800187 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400189 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Alexey Starikovskiyc35923b2007-10-22 14:19:09 +0400191 *state = (sta & 0x01)?ACPI_POWER_RESOURCE_STATE_ON:
192 ACPI_POWER_RESOURCE_STATE_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Lin Ming60a4ce72008-12-16 17:02:22 +0800194 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n",
Lin Ming60a4ce72008-12-16 17:02:22 +0800197 node_name,
Zhao Yakuib1b57fb2008-10-27 16:04:53 +0800198 *state ? "on" : "off"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Patrick Mocheld550d982006-06-27 00:41:40 -0400200 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100203static int acpi_power_get_list_state(struct list_head *list, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100205 struct acpi_power_resource_entry *entry;
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100206 int cur_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 if (!list || !state)
Patrick Mocheld550d982006-06-27 00:41:40 -0400209 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 /* The state of the list is 'on' IFF all resources are 'on'. */
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100212 list_for_each_entry(entry, list, node) {
213 struct acpi_power_resource *resource = entry->resource;
214 acpi_handle handle = resource->device.handle;
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100215 int result;
216
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100217 mutex_lock(&resource->resource_lock);
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100218 result = acpi_power_get_state(handle, &cur_state);
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100219 mutex_unlock(&resource->resource_lock);
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100220 if (result)
221 return result;
222
223 if (cur_state != ACPI_POWER_RESOURCE_STATE_ON)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 break;
225 }
226
227 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n",
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100228 cur_state ? "on" : "off"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100230 *state = cur_state;
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100231 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100234static void acpi_power_resume_dependent(struct work_struct *work)
Lin Ming0090def2012-03-29 14:09:39 +0800235{
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100236 struct acpi_power_dependent_device *dep;
237 struct acpi_device_physical_node *pn;
238 struct acpi_device *adev;
Lin Ming0090def2012-03-29 14:09:39 +0800239 int state;
240
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100241 dep = container_of(work, struct acpi_power_dependent_device, work);
242 adev = dep->adev;
243 if (acpi_power_get_inferred_state(adev, &state))
Lin Ming0090def2012-03-29 14:09:39 +0800244 return;
245
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100246 if (state > ACPI_STATE_D0)
Lin Ming0090def2012-03-29 14:09:39 +0800247 return;
248
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100249 mutex_lock(&adev->physical_node_lock);
250
251 list_for_each_entry(pn, &adev->physical_node_list, node)
252 pm_request_resume(pn->dev);
253
254 list_for_each_entry(pn, &adev->power_dependent, node)
255 pm_request_resume(pn->dev);
256
257 mutex_unlock(&adev->physical_node_lock);
Lin Ming0090def2012-03-29 14:09:39 +0800258}
259
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200260static int __acpi_power_on(struct acpi_power_resource *resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Len Brown4be44fc2005-08-05 00:44:28 -0400262 acpi_status status = AE_OK;
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500263
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100264 status = acpi_evaluate_object(resource->device.handle, "_ON", NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400266 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200268 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned on\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400269 resource->name));
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200270
Patrick Mocheld550d982006-06-27 00:41:40 -0400271 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100274static int acpi_power_on(struct acpi_power_resource *resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100276 int result = 0;;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500278 mutex_lock(&resource->resource_lock);
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200279
280 if (resource->ref_count++) {
281 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
282 "Power resource [%s] already on",
283 resource->name));
284 } else {
285 result = __acpi_power_on(resource);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100286 if (result) {
Rafael J. Wysocki12b3b5a2010-11-25 00:03:32 +0100287 resource->ref_count--;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100288 } else {
289 struct acpi_power_dependent_device *dep;
290
291 list_for_each_entry(dep, &resource->dependent, node)
292 schedule_work(&dep->work);
293 }
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500296 mutex_unlock(&resource->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Rafael J. Wysocki12b3b5a2010-11-25 00:03:32 +0100298 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100301static int acpi_power_off(struct acpi_power_resource *resource)
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200302{
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200303 acpi_status status = AE_OK;
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100304 int result = 0;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200305
306 mutex_lock(&resource->resource_lock);
307
308 if (!resource->ref_count) {
309 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
310 "Power resource [%s] already off",
311 resource->name));
312 goto unlock;
313 }
314
315 if (--resource->ref_count) {
316 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
317 "Power resource [%s] still in use\n",
318 resource->name));
319 goto unlock;
320 }
321
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100322 status = acpi_evaluate_object(resource->device.handle, "_OFF", NULL, NULL);
Rafael J. Wysocki722c9292013-01-17 14:11:06 +0100323 if (ACPI_FAILURE(status))
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200324 result = -ENODEV;
Rafael J. Wysocki722c9292013-01-17 14:11:06 +0100325 else
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200326 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
327 "Power resource [%s] turned off\n",
328 resource->name));
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200329
330 unlock:
331 mutex_unlock(&resource->resource_lock);
332
333 return result;
334}
335
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100336static int acpi_power_off_list(struct list_head *list)
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100337{
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100338 struct acpi_power_resource_entry *entry;
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100339 int result = 0;
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100340
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100341 list_for_each_entry_reverse(entry, list, node) {
342 result = acpi_power_off(entry->resource);
343 if (result)
344 goto err;
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100345 }
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100346 return 0;
347
348 err:
349 list_for_each_entry_continue(entry, list, node)
350 acpi_power_on(entry->resource);
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100351
352 return result;
353}
354
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100355static int acpi_power_on_list(struct list_head *list)
356{
357 struct acpi_power_resource_entry *entry;
358 int result = 0;
359
360 list_for_each_entry(entry, list, node) {
361 result = acpi_power_on(entry->resource);
362 if (result)
363 goto err;
364 }
365 return 0;
366
367 err:
368 list_for_each_entry_continue_reverse(entry, list, node)
369 acpi_power_off(entry->resource);
370
371 return result;
372}
373
374static void acpi_power_add_dependent(struct acpi_power_resource *resource,
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100375 struct acpi_device *adev)
Lin Ming0090def2012-03-29 14:09:39 +0800376{
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100377 struct acpi_power_dependent_device *dep;
Lin Ming0090def2012-03-29 14:09:39 +0800378
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100379 mutex_lock(&resource->resource_lock);
380
381 list_for_each_entry(dep, &resource->dependent, node)
382 if (dep->adev == adev)
383 goto out;
384
385 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
386 if (!dep)
387 goto out;
388
389 dep->adev = adev;
390 INIT_WORK(&dep->work, acpi_power_resume_dependent);
391 list_add_tail(&dep->node, &resource->dependent);
392
393 out:
394 mutex_unlock(&resource->resource_lock);
395}
396
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100397static void acpi_power_remove_dependent(struct acpi_power_resource *resource,
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100398 struct acpi_device *adev)
399{
400 struct acpi_power_dependent_device *dep;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100401 struct work_struct *work = NULL;
402
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100403 mutex_lock(&resource->resource_lock);
404
405 list_for_each_entry(dep, &resource->dependent, node)
406 if (dep->adev == adev) {
407 list_del(&dep->node);
408 work = &dep->work;
409 break;
410 }
411
412 mutex_unlock(&resource->resource_lock);
413
414 if (work) {
415 cancel_work_sync(work);
416 kfree(dep);
417 }
418}
419
420void acpi_power_add_remove_device(struct acpi_device *adev, bool add)
421{
422 if (adev->power.flags.power_resources) {
423 struct acpi_device_power_state *ps;
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100424 struct acpi_power_resource_entry *entry;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100425
426 ps = &adev->power.states[ACPI_STATE_D0];
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100427 list_for_each_entry(entry, &ps->resources, node) {
428 struct acpi_power_resource *resource = entry->resource;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100429
430 if (add)
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100431 acpi_power_add_dependent(resource, adev);
Lin Ming0090def2012-03-29 14:09:39 +0800432 else
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100433 acpi_power_remove_dependent(resource, adev);
Lin Ming0090def2012-03-29 14:09:39 +0800434 }
435 }
Lin Ming0090def2012-03-29 14:09:39 +0800436}
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100437
Rafael J. Wysocki0596a522013-01-17 14:11:07 +0100438int acpi_power_min_system_level(struct list_head *list)
439{
440 struct acpi_power_resource_entry *entry;
441 int system_level = 5;
442
443 list_for_each_entry(entry, list, node) {
444 struct acpi_power_resource *resource = entry->resource;
445
446 if (system_level > resource->system_level)
447 system_level = resource->system_level;
448 }
449 return system_level;
450}
451
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100452/* --------------------------------------------------------------------------
453 Device Power Management
454 -------------------------------------------------------------------------- */
Lin Ming0090def2012-03-29 14:09:39 +0800455
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200456/**
457 * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in
458 * ACPI 3.0) _PSW (Power State Wake)
459 * @dev: Device to handle.
460 * @enable: 0 - disable, 1 - enable the wake capabilities of the device.
461 * @sleep_state: Target sleep state of the system.
462 * @dev_state: Target power state of the device.
463 *
464 * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
465 * State Wake) for the device, if present. On failure reset the device's
466 * wakeup.flags.valid flag.
467 *
468 * RETURN VALUE:
469 * 0 if either _DSW or _PSW has been successfully executed
470 * 0 if neither _DSW nor _PSW has been found
471 * -ENODEV if the execution of either _DSW or _PSW has failed
472 */
473int acpi_device_sleep_wake(struct acpi_device *dev,
474 int enable, int sleep_state, int dev_state)
475{
476 union acpi_object in_arg[3];
477 struct acpi_object_list arg_list = { 3, in_arg };
478 acpi_status status = AE_OK;
479
480 /*
481 * Try to execute _DSW first.
482 *
483 * Three agruments are needed for the _DSW object:
484 * Argument 0: enable/disable the wake capabilities
485 * Argument 1: target system state
486 * Argument 2: target device state
487 * When _DSW object is called to disable the wake capabilities, maybe
488 * the first argument is filled. The values of the other two agruments
489 * are meaningless.
490 */
491 in_arg[0].type = ACPI_TYPE_INTEGER;
492 in_arg[0].integer.value = enable;
493 in_arg[1].type = ACPI_TYPE_INTEGER;
494 in_arg[1].integer.value = sleep_state;
495 in_arg[2].type = ACPI_TYPE_INTEGER;
496 in_arg[2].integer.value = dev_state;
497 status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL);
498 if (ACPI_SUCCESS(status)) {
499 return 0;
500 } else if (status != AE_NOT_FOUND) {
501 printk(KERN_ERR PREFIX "_DSW execution failed\n");
502 dev->wakeup.flags.valid = 0;
503 return -ENODEV;
504 }
505
506 /* Execute _PSW */
507 arg_list.count = 1;
508 in_arg[0].integer.value = enable;
509 status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL);
510 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
511 printk(KERN_ERR PREFIX "_PSW execution failed\n");
512 dev->wakeup.flags.valid = 0;
513 return -ENODEV;
514 }
515
516 return 0;
517}
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519/*
520 * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
521 * 1. Power on the power resources required for the wakeup device
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200522 * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
523 * State Wake) for the device, if present
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 */
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200525int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100527 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (!dev || !dev->wakeup.flags.valid)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200530 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200532 mutex_lock(&acpi_device_lock);
533
534 if (dev->wakeup.prepare_count++)
535 goto out;
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200536
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100537 err = acpi_power_on_list(&dev->wakeup.resources);
538 if (err) {
539 dev_err(&dev->dev, "Cannot turn wakeup power resources on\n");
540 dev->wakeup.flags.valid = 0;
541 } else {
542 /*
543 * Passing 3 as the third argument below means the device may be
544 * put into arbitrary power state afterward.
545 */
546 err = acpi_device_sleep_wake(dev, 1, sleep_state, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200548 if (err)
549 dev->wakeup.prepare_count = 0;
550
551 out:
552 mutex_unlock(&acpi_device_lock);
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200553 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
556/*
557 * Shutdown a wakeup device, counterpart of above method
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200558 * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
559 * State Wake) for the device, if present
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 * 2. Shutdown down the power resources
561 */
Len Brown4be44fc2005-08-05 00:44:28 -0400562int acpi_disable_wakeup_device_power(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100564 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 if (!dev || !dev->wakeup.flags.valid)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200567 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200569 mutex_lock(&acpi_device_lock);
570
571 if (--dev->wakeup.prepare_count > 0)
572 goto out;
573
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200574 /*
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200575 * Executing the code below even if prepare_count is already zero when
576 * the function is called may be useful, for example for initialisation.
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200577 */
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200578 if (dev->wakeup.prepare_count < 0)
579 dev->wakeup.prepare_count = 0;
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200580
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200581 err = acpi_device_sleep_wake(dev, 0, 0, 0);
582 if (err)
583 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100585 err = acpi_power_off_list(&dev->wakeup.resources);
586 if (err) {
587 dev_err(&dev->dev, "Cannot turn wakeup power resources off\n");
588 dev->wakeup.flags.valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
590
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200591 out:
592 mutex_unlock(&acpi_device_lock);
593 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100596int acpi_power_get_inferred_state(struct acpi_device *device, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Len Brown4be44fc2005-08-05 00:44:28 -0400598 int result = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400599 int list_state = 0;
600 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100602 if (!device || !state)
Patrick Mocheld550d982006-06-27 00:41:40 -0400603 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 /*
606 * We know a device's inferred power state when all the resources
607 * required for a given D-state are 'on'.
608 */
Rafael J. Wysocki38c92ff2012-05-20 13:58:00 +0200609 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100610 struct list_head *list = &device->power.states[i].resources;
611
612 if (list_empty(list))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 continue;
614
615 result = acpi_power_get_list_state(list, &list_state);
616 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400617 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100620 *state = i;
Patrick Mocheld550d982006-06-27 00:41:40 -0400621 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
623 }
624
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100625 *state = ACPI_STATE_D3;
Patrick Mocheld550d982006-06-27 00:41:40 -0400626 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
Rafael J. Wysocki30d3df42010-11-25 00:06:55 +0100629int acpi_power_on_resources(struct acpi_device *device, int state)
630{
Rafael J. Wysocki87e753b2013-01-22 12:56:16 +0100631 if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3_HOT)
Rafael J. Wysocki30d3df42010-11-25 00:06:55 +0100632 return -EINVAL;
633
634 return acpi_power_on_list(&device->power.states[state].resources);
635}
636
Len Brown4be44fc2005-08-05 00:44:28 -0400637int acpi_power_transition(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +0200639 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Zhang Rui3ebc81b2012-03-29 14:09:38 +0800641 if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
Patrick Mocheld550d982006-06-27 00:41:40 -0400642 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100644 if (device->power.state == state || !device->flags.power_manageable)
Rafael J. Wysocki212967c2010-11-25 00:02:36 +0100645 return 0;
646
Len Brown4be44fc2005-08-05 00:44:28 -0400647 if ((device->power.state < ACPI_STATE_D0)
Zhang Rui3ebc81b2012-03-29 14:09:38 +0800648 || (device->power.state > ACPI_STATE_D3_COLD))
Patrick Mocheld550d982006-06-27 00:41:40 -0400649 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 /* TBD: Resources must be ordered. */
652
653 /*
654 * First we reference all power resources required in the target list
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100655 * (e.g. so the device doesn't lose power while transitioning). Then,
656 * we dereference all power resources used in the current list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 */
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +0200658 if (state < ACPI_STATE_D3_COLD)
659 result = acpi_power_on_list(
660 &device->power.states[state].resources);
661
662 if (!result && device->power.state < ACPI_STATE_D3_COLD)
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100663 acpi_power_off_list(
664 &device->power.states[device->power.state].resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100666 /* We shouldn't change the state unless the above operations succeed. */
667 device->power.state = result ? ACPI_STATE_UNKNOWN : state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Patrick Mocheld550d982006-06-27 00:41:40 -0400669 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100672static void acpi_release_power_resource(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100674 struct acpi_device *device = to_acpi_device(dev);
675 struct acpi_power_resource *resource;
676
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100677 resource = container_of(device, struct acpi_power_resource, device);
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100678
679 mutex_lock(&power_resource_list_lock);
680 list_del(&resource->list_node);
681 mutex_unlock(&power_resource_list_lock);
682
683 acpi_free_ids(device);
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100684 kfree(resource);
685}
686
Rafael J. Wysockib1c0f992013-01-24 12:50:09 +0100687static ssize_t acpi_power_in_use_show(struct device *dev,
688 struct device_attribute *attr,
689 char *buf) {
690 struct acpi_power_resource *resource;
691
692 resource = to_power_resource(to_acpi_device(dev));
693 return sprintf(buf, "%u\n", !!resource->ref_count);
694}
695static DEVICE_ATTR(resource_in_use, 0444, acpi_power_in_use_show, NULL);
696
697static void acpi_power_sysfs_remove(struct acpi_device *device)
698{
699 device_remove_file(&device->dev, &dev_attr_resource_in_use);
700}
701
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100702int acpi_add_power_resource(acpi_handle handle)
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100703{
704 struct acpi_power_resource *resource;
705 struct acpi_device *device = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400706 union acpi_object acpi_object;
707 struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100708 acpi_status status;
709 int state, result = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100711 acpi_bus_get_device(handle, &device);
712 if (device)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100713 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100715 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (!resource)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100717 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100719 device = &resource->device;
720 acpi_init_device_object(device, handle, ACPI_BUS_TYPE_POWER,
721 ACPI_STA_DEFAULT);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500722 mutex_init(&resource->resource_lock);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100723 INIT_LIST_HEAD(&resource->dependent);
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100724 resource->name = device->pnp.bus_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
726 strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
Rafael J. Wysocki722c9292013-01-17 14:11:06 +0100727 device->power.state = ACPI_STATE_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 /* Evalute the object to get the system level and resource order. */
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100730 status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
731 if (ACPI_FAILURE(status))
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100732 goto err;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 resource->system_level = acpi_object.power_resource.system_level;
735 resource->order = acpi_object.power_resource.resource_order;
736
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100737 result = acpi_power_get_state(handle, &state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 if (result)
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100739 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Len Brown4be44fc2005-08-05 00:44:28 -0400741 printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device),
Alexey Starikovskiyc35923b2007-10-22 14:19:09 +0400742 acpi_device_bid(device), state ? "on" : "off");
Len Brown4be44fc2005-08-05 00:44:28 -0400743
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100744 device->flags.match_driver = true;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100745 result = acpi_device_add(device, acpi_release_power_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (result)
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100747 goto err;
Len Brown4be44fc2005-08-05 00:44:28 -0400748
Rafael J. Wysockib1c0f992013-01-24 12:50:09 +0100749 if (!device_create_file(&device->dev, &dev_attr_resource_in_use))
750 device->remove = acpi_power_sysfs_remove;
751
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100752 mutex_lock(&power_resource_list_lock);
753 list_add(&resource->list_node, &acpi_power_resource_list);
754 mutex_unlock(&power_resource_list_lock);
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100755 acpi_device_add_finalize(device);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100756 return 0;
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100757
758 err:
759 acpi_release_power_resource(&device->dev);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100760 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
762
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100763#ifdef CONFIG_ACPI_SLEEP
764void acpi_resume_power_resources(void)
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500765{
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200766 struct acpi_power_resource *resource;
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500767
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100768 mutex_lock(&power_resource_list_lock);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500769
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100770 list_for_each_entry(resource, &acpi_power_resource_list, list_node) {
771 int result, state;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200772
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100773 mutex_lock(&resource->resource_lock);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500774
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100775 result = acpi_power_get_state(resource->device.handle, &state);
776 if (!result && state == ACPI_POWER_RESOURCE_STATE_OFF
777 && resource->ref_count) {
778 dev_info(&resource->device.dev, "Turning ON\n");
779 __acpi_power_on(resource);
780 }
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500781
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100782 mutex_unlock(&resource->resource_lock);
783 }
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500784
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100785 mutex_unlock(&power_resource_list_lock);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500786}
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200787#endif