blob: a916417b9e70f69b4d022196eb25ba372ad781e6 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Rafael J. Wysockiaa57aca2015-07-16 02:01:28 +02003 * drivers/acpi/power.c - ACPI Power Resources management.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Rafael J. Wysockiaa57aca2015-07-16 02:01:28 +02005 * Copyright (C) 2001 - 2015 Intel Corp.
6 * Author: Andy Grover <andrew.grover@intel.com>
7 * Author: Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
8 * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11/*
12 * ACPI power-managed devices may be controlled in two ways:
13 * 1. via "Device Specific (D-State) Control"
14 * 2. via "Power Resource Control".
Rafael J. Wysockiaa57aca2015-07-16 02:01:28 +020015 * The code below deals with ACPI Power Resources control.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 *
Rafael J. Wysockiaa57aca2015-07-16 02:01:28 +020017 * An ACPI "power resource object" represents a software controllable power
18 * plane, clock plane, or other resource depended on by a device.
19 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * A device may rely on multiple power resources, and a power resource
21 * may be shared by multiple devices.
22 */
23
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Lin Ming0090def2012-03-29 14:09:39 +080029#include <linux/pm_runtime.h>
Rafael J. Wysocki18a38702013-01-25 21:51:32 +010030#include <linux/sysfs.h>
Lv Zheng8b484632013-12-03 08:49:16 +080031#include <linux/acpi.h>
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +020032#include "sleep.h"
Lin Ming0090def2012-03-29 14:09:39 +080033#include "internal.h"
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +020034
Bjorn Helgaas89595b82008-11-07 16:57:45 -070035#define _COMPONENT ACPI_POWER_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050036ACPI_MODULE_NAME("power");
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define ACPI_POWER_CLASS "power_resource"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#define ACPI_POWER_DEVICE_NAME "Power Resource"
39#define ACPI_POWER_FILE_INFO "info"
40#define ACPI_POWER_FILE_STATUS "state"
41#define ACPI_POWER_RESOURCE_STATE_OFF 0x00
42#define ACPI_POWER_RESOURCE_STATE_ON 0x01
43#define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF
Zhao Yakuif5adfaa2008-08-11 14:57:50 +080044
Len Brown4be44fc2005-08-05 00:44:28 -040045struct acpi_power_resource {
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010046 struct acpi_device device;
Rafael J. Wysocki781d7372013-01-17 14:11:06 +010047 struct list_head list_node;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010048 char *name;
Len Brown4be44fc2005-08-05 00:44:28 -040049 u32 system_level;
50 u32 order;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +020051 unsigned int ref_count;
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +010052 bool wakeup_enabled;
Konstantin Karasyov0a613902007-02-16 01:47:06 -050053 struct mutex resource_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054};
55
Rafael J. Wysocki0b224522013-01-17 14:11:06 +010056struct acpi_power_resource_entry {
57 struct list_head node;
58 struct acpi_power_resource *resource;
59};
60
Rafael J. Wysocki781d7372013-01-17 14:11:06 +010061static LIST_HEAD(acpi_power_resource_list);
62static DEFINE_MUTEX(power_resource_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/* --------------------------------------------------------------------------
65 Power Resource Management
66 -------------------------------------------------------------------------- */
67
Rafael J. Wysockib1c0f992013-01-24 12:50:09 +010068static inline
69struct acpi_power_resource *to_power_resource(struct acpi_device *device)
70{
71 return container_of(device, struct acpi_power_resource, device);
72}
73
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010074static struct acpi_power_resource *acpi_power_get_context(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010076 struct acpi_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010078 if (acpi_bus_get_device(handle, &device))
79 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Rafael J. Wysockib1c0f992013-01-24 12:50:09 +010081 return to_power_resource(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +010084static int acpi_power_resources_list_add(acpi_handle handle,
85 struct list_head *list)
Rafael J. Wysocki0b224522013-01-17 14:11:06 +010086{
87 struct acpi_power_resource *resource = acpi_power_get_context(handle);
88 struct acpi_power_resource_entry *entry;
89
90 if (!resource || !list)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +010091 return -EINVAL;
Rafael J. Wysocki0b224522013-01-17 14:11:06 +010092
93 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
94 if (!entry)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +010095 return -ENOMEM;
Rafael J. Wysocki0b224522013-01-17 14:11:06 +010096
97 entry->resource = resource;
98 if (!list_empty(list)) {
99 struct acpi_power_resource_entry *e;
100
101 list_for_each_entry(e, list, node)
102 if (e->resource->order > resource->order) {
103 list_add_tail(&entry->node, &e->node);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100104 return 0;
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100105 }
106 }
107 list_add_tail(&entry->node, list);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100108 return 0;
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100109}
110
111void acpi_power_resources_list_free(struct list_head *list)
112{
113 struct acpi_power_resource_entry *entry, *e;
114
115 list_for_each_entry_safe(entry, e, list, node) {
116 list_del(&entry->node);
117 kfree(entry);
118 }
119}
120
Hans de Goede7d7b4672018-12-30 18:25:00 +0100121static bool acpi_power_resource_is_dup(union acpi_object *package,
122 unsigned int start, unsigned int i)
123{
124 acpi_handle rhandle, dup;
125 unsigned int j;
126
127 /* The caller is expected to check the package element types */
128 rhandle = package->package.elements[i].reference.handle;
129 for (j = start; j < i; j++) {
130 dup = package->package.elements[j].reference.handle;
131 if (dup == rhandle)
132 return true;
133 }
134
135 return false;
136}
137
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100138int acpi_extract_power_resources(union acpi_object *package, unsigned int start,
139 struct list_head *list)
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100140{
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100141 unsigned int i;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100142 int err = 0;
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100143
144 for (i = start; i < package->package.count; i++) {
145 union acpi_object *element = &package->package.elements[i];
146 acpi_handle rhandle;
147
148 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100149 err = -ENODATA;
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100150 break;
151 }
152 rhandle = element->reference.handle;
153 if (!rhandle) {
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100154 err = -ENODEV;
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100155 break;
156 }
Hans de Goede7d7b4672018-12-30 18:25:00 +0100157
158 /* Some ACPI tables contain duplicate power resource references */
159 if (acpi_power_resource_is_dup(package, start, i))
160 continue;
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'. */
Arnd Bergmannfe8c4702017-04-19 19:47:04 +0200212 cur_state = 0;
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. Wysocki3e384ee2010-10-22 02:35:54 +0200235static int __acpi_power_on(struct acpi_power_resource *resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Len Brown4be44fc2005-08-05 00:44:28 -0400237 acpi_status status = AE_OK;
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500238
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100239 status = acpi_evaluate_object(resource->device.handle, "_ON", NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400241 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200243 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned on\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400244 resource->name));
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200245
Patrick Mocheld550d982006-06-27 00:41:40 -0400246 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100249static int acpi_power_on_unlocked(struct acpi_power_resource *resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100251 int result = 0;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200252
253 if (resource->ref_count++) {
254 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Mika Westerberg10a0b612013-07-05 12:15:56 +0300255 "Power resource [%s] already on\n",
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200256 resource->name));
257 } else {
258 result = __acpi_power_on(resource);
Rafael J. Wysocki41863fc2013-10-16 23:05:42 +0200259 if (result)
Rafael J. Wysocki12b3b5a2010-11-25 00:03:32 +0100260 resource->ref_count--;
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500261 }
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100262 return result;
263}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100265static int acpi_power_on(struct acpi_power_resource *resource)
266{
267 int result;
268
269 mutex_lock(&resource->resource_lock);
270 result = acpi_power_on_unlocked(resource);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500271 mutex_unlock(&resource->resource_lock);
Rafael J. Wysocki12b3b5a2010-11-25 00:03:32 +0100272 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
Rafael J. Wysocki660b1112013-01-25 21:51:57 +0100275static int __acpi_power_off(struct acpi_power_resource *resource)
276{
277 acpi_status status;
278
279 status = acpi_evaluate_object(resource->device.handle, "_OFF",
280 NULL, NULL);
281 if (ACPI_FAILURE(status))
282 return -ENODEV;
283
284 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned off\n",
285 resource->name));
286 return 0;
287}
288
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100289static int acpi_power_off_unlocked(struct acpi_power_resource *resource)
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200290{
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100291 int result = 0;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200292
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200293 if (!resource->ref_count) {
294 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Mika Westerberg10a0b612013-07-05 12:15:56 +0300295 "Power resource [%s] already off\n",
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200296 resource->name));
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100297 return 0;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200298 }
299
300 if (--resource->ref_count) {
301 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
302 "Power resource [%s] still in use\n",
303 resource->name));
Rafael J. Wysocki660b1112013-01-25 21:51:57 +0100304 } else {
305 result = __acpi_power_off(resource);
306 if (result)
307 resource->ref_count++;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200308 }
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100309 return result;
310}
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200311
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100312static int acpi_power_off(struct acpi_power_resource *resource)
313{
314 int result;
315
316 mutex_lock(&resource->resource_lock);
317 result = acpi_power_off_unlocked(resource);
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200318 mutex_unlock(&resource->resource_lock);
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200319 return result;
320}
321
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100322static int acpi_power_off_list(struct list_head *list)
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100323{
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100324 struct acpi_power_resource_entry *entry;
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100325 int result = 0;
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100326
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100327 list_for_each_entry_reverse(entry, list, node) {
328 result = acpi_power_off(entry->resource);
329 if (result)
330 goto err;
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100331 }
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100332 return 0;
333
334 err:
335 list_for_each_entry_continue(entry, list, node)
336 acpi_power_on(entry->resource);
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100337
338 return result;
339}
340
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100341static int acpi_power_on_list(struct list_head *list)
342{
343 struct acpi_power_resource_entry *entry;
344 int result = 0;
345
346 list_for_each_entry(entry, list, node) {
347 result = acpi_power_on(entry->resource);
348 if (result)
349 goto err;
350 }
351 return 0;
352
353 err:
354 list_for_each_entry_continue_reverse(entry, list, node)
355 acpi_power_off(entry->resource);
356
357 return result;
358}
359
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100360static struct attribute *attrs[] = {
361 NULL,
362};
363
Arvind Yadav26408b22017-06-30 17:39:05 +0530364static const struct attribute_group attr_groups[] = {
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100365 [ACPI_STATE_D0] = {
366 .name = "power_resources_D0",
367 .attrs = attrs,
368 },
369 [ACPI_STATE_D1] = {
370 .name = "power_resources_D1",
371 .attrs = attrs,
372 },
373 [ACPI_STATE_D2] = {
374 .name = "power_resources_D2",
375 .attrs = attrs,
376 },
377 [ACPI_STATE_D3_HOT] = {
378 .name = "power_resources_D3hot",
379 .attrs = attrs,
380 },
381};
382
Arvind Yadav26408b22017-06-30 17:39:05 +0530383static const struct attribute_group wakeup_attr_group = {
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200384 .name = "power_resources_wakeup",
385 .attrs = attrs,
386};
387
388static void acpi_power_hide_list(struct acpi_device *adev,
389 struct list_head *resources,
Arvind Yadav26408b22017-06-30 17:39:05 +0530390 const struct attribute_group *attr_group)
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100391{
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100392 struct acpi_power_resource_entry *entry;
393
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200394 if (list_empty(resources))
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100395 return;
396
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200397 list_for_each_entry_reverse(entry, resources, node) {
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100398 struct acpi_device *res_dev = &entry->resource->device;
399
400 sysfs_remove_link_from_group(&adev->dev.kobj,
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200401 attr_group->name,
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100402 dev_name(&res_dev->dev));
403 }
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200404 sysfs_remove_group(&adev->dev.kobj, attr_group);
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100405}
406
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200407static void acpi_power_expose_list(struct acpi_device *adev,
408 struct list_head *resources,
Arvind Yadav26408b22017-06-30 17:39:05 +0530409 const struct attribute_group *attr_group)
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100410{
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100411 struct acpi_power_resource_entry *entry;
412 int ret;
413
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200414 if (list_empty(resources))
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100415 return;
416
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200417 ret = sysfs_create_group(&adev->dev.kobj, attr_group);
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100418 if (ret)
419 return;
420
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200421 list_for_each_entry(entry, resources, node) {
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100422 struct acpi_device *res_dev = &entry->resource->device;
423
424 ret = sysfs_add_link_to_group(&adev->dev.kobj,
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200425 attr_group->name,
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100426 &res_dev->dev.kobj,
427 dev_name(&res_dev->dev));
428 if (ret) {
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200429 acpi_power_hide_list(adev, resources, attr_group);
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100430 break;
431 }
432 }
433}
434
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200435static void acpi_power_expose_hide(struct acpi_device *adev,
436 struct list_head *resources,
Arvind Yadav26408b22017-06-30 17:39:05 +0530437 const struct attribute_group *attr_group,
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200438 bool expose)
439{
440 if (expose)
441 acpi_power_expose_list(adev, resources, attr_group);
442 else
443 acpi_power_hide_list(adev, resources, attr_group);
444}
445
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100446void acpi_power_add_remove_device(struct acpi_device *adev, bool add)
447{
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100448 int state;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100449
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200450 if (adev->wakeup.flags.valid)
451 acpi_power_expose_hide(adev, &adev->wakeup.resources,
452 &wakeup_attr_group, add);
453
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100454 if (!adev->power.flags.power_resources)
455 return;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100456
Rafael J. Wysocki41a2a462013-04-11 22:41:48 +0200457 for (state = ACPI_STATE_D0; state <= ACPI_STATE_D3_HOT; state++)
458 acpi_power_expose_hide(adev,
459 &adev->power.states[state].resources,
460 &attr_groups[state], add);
Lin Ming0090def2012-03-29 14:09:39 +0800461}
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100462
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100463int acpi_power_wakeup_list_init(struct list_head *list, int *system_level_p)
Rafael J. Wysocki0596a522013-01-17 14:11:07 +0100464{
465 struct acpi_power_resource_entry *entry;
466 int system_level = 5;
467
468 list_for_each_entry(entry, list, node) {
469 struct acpi_power_resource *resource = entry->resource;
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100470 acpi_handle handle = resource->device.handle;
471 int result;
472 int state;
Rafael J. Wysocki0596a522013-01-17 14:11:07 +0100473
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100474 mutex_lock(&resource->resource_lock);
475
476 result = acpi_power_get_state(handle, &state);
477 if (result) {
478 mutex_unlock(&resource->resource_lock);
479 return result;
480 }
481 if (state == ACPI_POWER_RESOURCE_STATE_ON) {
482 resource->ref_count++;
483 resource->wakeup_enabled = true;
484 }
Rafael J. Wysocki0596a522013-01-17 14:11:07 +0100485 if (system_level > resource->system_level)
486 system_level = resource->system_level;
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100487
488 mutex_unlock(&resource->resource_lock);
Rafael J. Wysocki0596a522013-01-17 14:11:07 +0100489 }
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100490 *system_level_p = system_level;
491 return 0;
Rafael J. Wysocki0596a522013-01-17 14:11:07 +0100492}
493
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100494/* --------------------------------------------------------------------------
495 Device Power Management
496 -------------------------------------------------------------------------- */
Lin Ming0090def2012-03-29 14:09:39 +0800497
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200498/**
499 * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in
500 * ACPI 3.0) _PSW (Power State Wake)
501 * @dev: Device to handle.
502 * @enable: 0 - disable, 1 - enable the wake capabilities of the device.
503 * @sleep_state: Target sleep state of the system.
504 * @dev_state: Target power state of the device.
505 *
506 * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
507 * State Wake) for the device, if present. On failure reset the device's
508 * wakeup.flags.valid flag.
509 *
510 * RETURN VALUE:
511 * 0 if either _DSW or _PSW has been successfully executed
512 * 0 if neither _DSW nor _PSW has been found
513 * -ENODEV if the execution of either _DSW or _PSW has failed
514 */
515int acpi_device_sleep_wake(struct acpi_device *dev,
516 int enable, int sleep_state, int dev_state)
517{
518 union acpi_object in_arg[3];
519 struct acpi_object_list arg_list = { 3, in_arg };
520 acpi_status status = AE_OK;
521
522 /*
523 * Try to execute _DSW first.
524 *
Bjorn Helgaas603fadf2019-03-25 13:34:00 -0500525 * Three arguments are needed for the _DSW object:
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200526 * Argument 0: enable/disable the wake capabilities
527 * Argument 1: target system state
528 * Argument 2: target device state
529 * When _DSW object is called to disable the wake capabilities, maybe
Bjorn Helgaas603fadf2019-03-25 13:34:00 -0500530 * the first argument is filled. The values of the other two arguments
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200531 * are meaningless.
532 */
533 in_arg[0].type = ACPI_TYPE_INTEGER;
534 in_arg[0].integer.value = enable;
535 in_arg[1].type = ACPI_TYPE_INTEGER;
536 in_arg[1].integer.value = sleep_state;
537 in_arg[2].type = ACPI_TYPE_INTEGER;
538 in_arg[2].integer.value = dev_state;
539 status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL);
540 if (ACPI_SUCCESS(status)) {
541 return 0;
542 } else if (status != AE_NOT_FOUND) {
543 printk(KERN_ERR PREFIX "_DSW execution failed\n");
544 dev->wakeup.flags.valid = 0;
545 return -ENODEV;
546 }
547
548 /* Execute _PSW */
Jiang Liu0db98202013-06-29 00:24:39 +0800549 status = acpi_execute_simple_method(dev->handle, "_PSW", enable);
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200550 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
551 printk(KERN_ERR PREFIX "_PSW execution failed\n");
552 dev->wakeup.flags.valid = 0;
553 return -ENODEV;
554 }
555
556 return 0;
557}
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559/*
560 * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
561 * 1. Power on the power resources required for the wakeup device
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200562 * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
563 * State Wake) for the device, if present
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 */
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200565int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100567 struct acpi_power_resource_entry *entry;
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100568 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (!dev || !dev->wakeup.flags.valid)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200571 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200573 mutex_lock(&acpi_device_lock);
574
575 if (dev->wakeup.prepare_count++)
576 goto out;
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200577
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100578 list_for_each_entry(entry, &dev->wakeup.resources, node) {
579 struct acpi_power_resource *resource = entry->resource;
580
581 mutex_lock(&resource->resource_lock);
582
583 if (!resource->wakeup_enabled) {
584 err = acpi_power_on_unlocked(resource);
585 if (!err)
586 resource->wakeup_enabled = true;
587 }
588
589 mutex_unlock(&resource->resource_lock);
590
591 if (err) {
592 dev_err(&dev->dev,
593 "Cannot turn wakeup power resources on\n");
594 dev->wakeup.flags.valid = 0;
595 goto out;
596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100598 /*
599 * Passing 3 as the third argument below means the device may be
600 * put into arbitrary power state afterward.
601 */
602 err = acpi_device_sleep_wake(dev, 1, sleep_state, 3);
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200603 if (err)
604 dev->wakeup.prepare_count = 0;
605
606 out:
607 mutex_unlock(&acpi_device_lock);
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200608 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
611/*
612 * Shutdown a wakeup device, counterpart of above method
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200613 * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
614 * State Wake) for the device, if present
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 * 2. Shutdown down the power resources
616 */
Len Brown4be44fc2005-08-05 00:44:28 -0400617int acpi_disable_wakeup_device_power(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100619 struct acpi_power_resource_entry *entry;
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100620 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 if (!dev || !dev->wakeup.flags.valid)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200623 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200625 mutex_lock(&acpi_device_lock);
626
627 if (--dev->wakeup.prepare_count > 0)
628 goto out;
629
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200630 /*
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200631 * Executing the code below even if prepare_count is already zero when
632 * the function is called may be useful, for example for initialisation.
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200633 */
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200634 if (dev->wakeup.prepare_count < 0)
635 dev->wakeup.prepare_count = 0;
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200636
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200637 err = acpi_device_sleep_wake(dev, 0, 0, 0);
638 if (err)
639 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100641 list_for_each_entry(entry, &dev->wakeup.resources, node) {
642 struct acpi_power_resource *resource = entry->resource;
643
644 mutex_lock(&resource->resource_lock);
645
646 if (resource->wakeup_enabled) {
647 err = acpi_power_off_unlocked(resource);
648 if (!err)
649 resource->wakeup_enabled = false;
650 }
651
652 mutex_unlock(&resource->resource_lock);
653
654 if (err) {
655 dev_err(&dev->dev,
656 "Cannot turn wakeup power resources off\n");
657 dev->wakeup.flags.valid = 0;
658 break;
659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
661
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200662 out:
663 mutex_unlock(&acpi_device_lock);
664 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100667int acpi_power_get_inferred_state(struct acpi_device *device, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Len Brown4be44fc2005-08-05 00:44:28 -0400669 int result = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400670 int list_state = 0;
671 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100673 if (!device || !state)
Patrick Mocheld550d982006-06-27 00:41:40 -0400674 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 /*
677 * We know a device's inferred power state when all the resources
678 * required for a given D-state are 'on'.
679 */
Rafael J. Wysocki38c92ff2012-05-20 13:58:00 +0200680 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100681 struct list_head *list = &device->power.states[i].resources;
682
683 if (list_empty(list))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 continue;
685
686 result = acpi_power_get_list_state(list, &list_state);
687 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400688 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100691 *state = i;
Patrick Mocheld550d982006-06-27 00:41:40 -0400692 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
694 }
695
Rafael J. Wysocki20dacb72015-05-16 01:55:35 +0200696 *state = device->power.states[ACPI_STATE_D3_COLD].flags.valid ?
697 ACPI_STATE_D3_COLD : ACPI_STATE_D3_HOT;
Patrick Mocheld550d982006-06-27 00:41:40 -0400698 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
Rafael J. Wysocki30d3df412010-11-25 00:06:55 +0100701int acpi_power_on_resources(struct acpi_device *device, int state)
702{
Rafael J. Wysocki87e753b2013-01-22 12:56:16 +0100703 if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3_HOT)
Rafael J. Wysocki30d3df412010-11-25 00:06:55 +0100704 return -EINVAL;
705
706 return acpi_power_on_list(&device->power.states[state].resources);
707}
708
Len Brown4be44fc2005-08-05 00:44:28 -0400709int acpi_power_transition(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +0200711 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Zhang Rui3ebc81b2012-03-29 14:09:38 +0800713 if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
Patrick Mocheld550d982006-06-27 00:41:40 -0400714 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100716 if (device->power.state == state || !device->flags.power_manageable)
Rafael J. Wysocki212967c2010-11-25 00:02:36 +0100717 return 0;
718
Len Brown4be44fc2005-08-05 00:44:28 -0400719 if ((device->power.state < ACPI_STATE_D0)
Zhang Rui3ebc81b2012-03-29 14:09:38 +0800720 || (device->power.state > ACPI_STATE_D3_COLD))
Patrick Mocheld550d982006-06-27 00:41:40 -0400721 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 /*
724 * First we reference all power resources required in the target list
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100725 * (e.g. so the device doesn't lose power while transitioning). Then,
726 * we dereference all power resources used in the current list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 */
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +0200728 if (state < ACPI_STATE_D3_COLD)
729 result = acpi_power_on_list(
730 &device->power.states[state].resources);
731
732 if (!result && device->power.state < ACPI_STATE_D3_COLD)
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100733 acpi_power_off_list(
734 &device->power.states[device->power.state].resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100736 /* We shouldn't change the state unless the above operations succeed. */
737 device->power.state = result ? ACPI_STATE_UNKNOWN : state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Patrick Mocheld550d982006-06-27 00:41:40 -0400739 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
741
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100742static void acpi_release_power_resource(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100744 struct acpi_device *device = to_acpi_device(dev);
745 struct acpi_power_resource *resource;
746
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100747 resource = container_of(device, struct acpi_power_resource, device);
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100748
749 mutex_lock(&power_resource_list_lock);
750 list_del(&resource->list_node);
751 mutex_unlock(&power_resource_list_lock);
752
Toshi Kanic0af4172013-03-04 21:30:42 +0000753 acpi_free_pnp_ids(&device->pnp);
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100754 kfree(resource);
755}
756
Rafael J. Wysockib1c0f992013-01-24 12:50:09 +0100757static ssize_t acpi_power_in_use_show(struct device *dev,
758 struct device_attribute *attr,
759 char *buf) {
760 struct acpi_power_resource *resource;
761
762 resource = to_power_resource(to_acpi_device(dev));
763 return sprintf(buf, "%u\n", !!resource->ref_count);
764}
765static DEVICE_ATTR(resource_in_use, 0444, acpi_power_in_use_show, NULL);
766
767static void acpi_power_sysfs_remove(struct acpi_device *device)
768{
769 device_remove_file(&device->dev, &dev_attr_resource_in_use);
770}
771
Rafael J. Wysockid5eefa82015-05-21 04:19:49 +0200772static void acpi_power_add_resource_to_list(struct acpi_power_resource *resource)
773{
774 mutex_lock(&power_resource_list_lock);
775
776 if (!list_empty(&acpi_power_resource_list)) {
777 struct acpi_power_resource *r;
778
779 list_for_each_entry(r, &acpi_power_resource_list, list_node)
780 if (r->order > resource->order) {
781 list_add_tail(&resource->list_node, &r->list_node);
782 goto out;
783 }
784 }
785 list_add_tail(&resource->list_node, &acpi_power_resource_list);
786
787 out:
788 mutex_unlock(&power_resource_list_lock);
789}
790
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100791int acpi_add_power_resource(acpi_handle handle)
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100792{
793 struct acpi_power_resource *resource;
794 struct acpi_device *device = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400795 union acpi_object acpi_object;
796 struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100797 acpi_status status;
798 int state, result = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100800 acpi_bus_get_device(handle, &device);
801 if (device)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100802 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100804 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (!resource)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100806 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100808 device = &resource->device;
809 acpi_init_device_object(device, handle, ACPI_BUS_TYPE_POWER,
810 ACPI_STA_DEFAULT);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500811 mutex_init(&resource->resource_lock);
Rafael J. Wysocki6ee22e9d2013-06-19 00:44:45 +0200812 INIT_LIST_HEAD(&resource->list_node);
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100813 resource->name = device->pnp.bus_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
815 strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
Rafael J. Wysocki722c9292013-01-17 14:11:06 +0100816 device->power.state = ACPI_STATE_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 /* Evalute the object to get the system level and resource order. */
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100819 status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
820 if (ACPI_FAILURE(status))
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100821 goto err;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 resource->system_level = acpi_object.power_resource.system_level;
824 resource->order = acpi_object.power_resource.resource_order;
825
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100826 result = acpi_power_get_state(handle, &state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 if (result)
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100828 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Len Brown4be44fc2005-08-05 00:44:28 -0400830 printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device),
Alexey Starikovskiyc35923b2007-10-22 14:19:09 +0400831 acpi_device_bid(device), state ? "on" : "off");
Len Brown4be44fc2005-08-05 00:44:28 -0400832
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100833 device->flags.match_driver = true;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100834 result = acpi_device_add(device, acpi_release_power_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (result)
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100836 goto err;
Len Brown4be44fc2005-08-05 00:44:28 -0400837
Rafael J. Wysockib1c0f992013-01-24 12:50:09 +0100838 if (!device_create_file(&device->dev, &dev_attr_resource_in_use))
839 device->remove = acpi_power_sysfs_remove;
840
Rafael J. Wysockid5eefa82015-05-21 04:19:49 +0200841 acpi_power_add_resource_to_list(resource);
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100842 acpi_device_add_finalize(device);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100843 return 0;
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100844
845 err:
846 acpi_release_power_resource(&device->dev);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100847 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100850#ifdef CONFIG_ACPI_SLEEP
851void acpi_resume_power_resources(void)
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500852{
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200853 struct acpi_power_resource *resource;
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500854
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100855 mutex_lock(&power_resource_list_lock);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500856
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100857 list_for_each_entry(resource, &acpi_power_resource_list, list_node) {
858 int result, state;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200859
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100860 mutex_lock(&resource->resource_lock);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500861
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100862 result = acpi_power_get_state(resource->device.handle, &state);
Lan Tianyud7d49012013-10-15 19:48:11 +0800863 if (result) {
864 mutex_unlock(&resource->resource_lock);
Rafael J. Wysocki660b1112013-01-25 21:51:57 +0100865 continue;
Lan Tianyud7d49012013-10-15 19:48:11 +0800866 }
Rafael J. Wysocki660b1112013-01-25 21:51:57 +0100867
868 if (state == ACPI_POWER_RESOURCE_STATE_OFF
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100869 && resource->ref_count) {
870 dev_info(&resource->device.dev, "Turning ON\n");
871 __acpi_power_on(resource);
Rafael J. Wysockid5eefa82015-05-21 04:19:49 +0200872 }
873
874 mutex_unlock(&resource->resource_lock);
875 }
Hans de Goede8ece1d82017-04-30 22:54:16 +0200876
877 mutex_unlock(&power_resource_list_lock);
878}
879
880void acpi_turn_off_unused_power_resources(void)
881{
882 struct acpi_power_resource *resource;
883
884 mutex_lock(&power_resource_list_lock);
885
Rafael J. Wysockid5eefa82015-05-21 04:19:49 +0200886 list_for_each_entry_reverse(resource, &acpi_power_resource_list, list_node) {
887 int result, state;
888
889 mutex_lock(&resource->resource_lock);
890
891 result = acpi_power_get_state(resource->device.handle, &state);
892 if (result) {
893 mutex_unlock(&resource->resource_lock);
894 continue;
895 }
896
897 if (state == ACPI_POWER_RESOURCE_STATE_ON
Rafael J. Wysocki660b1112013-01-25 21:51:57 +0100898 && !resource->ref_count) {
899 dev_info(&resource->device.dev, "Turning OFF\n");
900 __acpi_power_off(resource);
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100901 }
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500902
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100903 mutex_unlock(&resource->resource_lock);
904 }
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500905
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100906 mutex_unlock(&power_resource_list_lock);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500907}
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200908#endif