Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 42 | #include <linux/slab.h> |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 43 | #include <linux/pm_runtime.h> |
Rafael J. Wysocki | 18a3870 | 2013-01-25 21:51:32 +0100 | [diff] [blame^] | 44 | #include <linux/sysfs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <acpi/acpi_bus.h> |
| 46 | #include <acpi/acpi_drivers.h> |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 47 | #include "sleep.h" |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 48 | #include "internal.h" |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 49 | |
Len Brown | a192a95 | 2009-07-28 16:45:54 -0400 | [diff] [blame] | 50 | #define PREFIX "ACPI: " |
| 51 | |
Bjorn Helgaas | 89595b8 | 2008-11-07 16:57:45 -0700 | [diff] [blame] | 52 | #define _COMPONENT ACPI_POWER_COMPONENT |
Len Brown | f52fd66 | 2007-02-12 22:42:12 -0500 | [diff] [blame] | 53 | ACPI_MODULE_NAME("power"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | #define ACPI_POWER_CLASS "power_resource" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | #define ACPI_POWER_DEVICE_NAME "Power Resource" |
| 56 | #define ACPI_POWER_FILE_INFO "info" |
| 57 | #define ACPI_POWER_FILE_STATUS "state" |
| 58 | #define ACPI_POWER_RESOURCE_STATE_OFF 0x00 |
| 59 | #define ACPI_POWER_RESOURCE_STATE_ON 0x01 |
| 60 | #define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF |
Zhao Yakui | f5adfaa | 2008-08-11 14:57:50 +0800 | [diff] [blame] | 61 | |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 62 | struct acpi_power_dependent_device { |
| 63 | struct list_head node; |
| 64 | struct acpi_device *adev; |
| 65 | struct work_struct work; |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 66 | }; |
| 67 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 68 | struct acpi_power_resource { |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 69 | struct acpi_device device; |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 70 | struct list_head list_node; |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 71 | struct list_head dependent; |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 72 | char *name; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 73 | u32 system_level; |
| 74 | u32 order; |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 75 | unsigned int ref_count; |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 76 | struct mutex resource_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 79 | struct acpi_power_resource_entry { |
| 80 | struct list_head node; |
| 81 | struct acpi_power_resource *resource; |
| 82 | }; |
| 83 | |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 84 | static LIST_HEAD(acpi_power_resource_list); |
| 85 | static DEFINE_MUTEX(power_resource_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | /* -------------------------------------------------------------------------- |
| 88 | Power Resource Management |
| 89 | -------------------------------------------------------------------------- */ |
| 90 | |
Rafael J. Wysocki | b1c0f99 | 2013-01-24 12:50:09 +0100 | [diff] [blame] | 91 | static inline |
| 92 | struct acpi_power_resource *to_power_resource(struct acpi_device *device) |
| 93 | { |
| 94 | return container_of(device, struct acpi_power_resource, device); |
| 95 | } |
| 96 | |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 97 | static struct acpi_power_resource *acpi_power_get_context(acpi_handle handle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | { |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 99 | struct acpi_device *device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 101 | if (acpi_bus_get_device(handle, &device)) |
| 102 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
Rafael J. Wysocki | b1c0f99 | 2013-01-24 12:50:09 +0100 | [diff] [blame] | 104 | return to_power_resource(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 107 | static int acpi_power_resources_list_add(acpi_handle handle, |
| 108 | struct list_head *list) |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 109 | { |
| 110 | struct acpi_power_resource *resource = acpi_power_get_context(handle); |
| 111 | struct acpi_power_resource_entry *entry; |
| 112 | |
| 113 | if (!resource || !list) |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 114 | return -EINVAL; |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 115 | |
| 116 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
| 117 | if (!entry) |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 118 | return -ENOMEM; |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 119 | |
| 120 | entry->resource = resource; |
| 121 | if (!list_empty(list)) { |
| 122 | struct acpi_power_resource_entry *e; |
| 123 | |
| 124 | list_for_each_entry(e, list, node) |
| 125 | if (e->resource->order > resource->order) { |
| 126 | list_add_tail(&entry->node, &e->node); |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 127 | return 0; |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | list_add_tail(&entry->node, list); |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 131 | return 0; |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void acpi_power_resources_list_free(struct list_head *list) |
| 135 | { |
| 136 | struct acpi_power_resource_entry *entry, *e; |
| 137 | |
| 138 | list_for_each_entry_safe(entry, e, list, node) { |
| 139 | list_del(&entry->node); |
| 140 | kfree(entry); |
| 141 | } |
| 142 | } |
| 143 | |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 144 | int acpi_extract_power_resources(union acpi_object *package, unsigned int start, |
| 145 | struct list_head *list) |
Rafael J. Wysocki | ef85bdb | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 146 | { |
Rafael J. Wysocki | ef85bdb | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 147 | unsigned int i; |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 148 | int err = 0; |
Rafael J. Wysocki | ef85bdb | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 149 | |
| 150 | for (i = start; i < package->package.count; i++) { |
| 151 | union acpi_object *element = &package->package.elements[i]; |
| 152 | acpi_handle rhandle; |
| 153 | |
| 154 | if (element->type != ACPI_TYPE_LOCAL_REFERENCE) { |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 155 | err = -ENODATA; |
Rafael J. Wysocki | ef85bdb | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 156 | break; |
| 157 | } |
| 158 | rhandle = element->reference.handle; |
| 159 | if (!rhandle) { |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 160 | err = -ENODEV; |
Rafael J. Wysocki | ef85bdb | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 161 | break; |
| 162 | } |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 163 | err = acpi_add_power_resource(rhandle); |
| 164 | if (err) |
| 165 | break; |
| 166 | |
| 167 | err = acpi_power_resources_list_add(rhandle, list); |
| 168 | if (err) |
| 169 | break; |
Rafael J. Wysocki | ef85bdb | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 170 | } |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 171 | if (err) |
Rafael J. Wysocki | ef85bdb | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 172 | acpi_power_resources_list_free(list); |
| 173 | |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 174 | return err; |
Rafael J. Wysocki | ef85bdb | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 175 | } |
| 176 | |
Zhao Yakui | a51e145 | 2008-08-11 14:55:05 +0800 | [diff] [blame] | 177 | static int acpi_power_get_state(acpi_handle handle, int *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 179 | acpi_status status = AE_OK; |
Matthew Wilcox | 27663c5 | 2008-10-10 02:22:59 -0400 | [diff] [blame] | 180 | unsigned long long sta = 0; |
Lin Ming | 60a4ce7 | 2008-12-16 17:02:22 +0800 | [diff] [blame] | 181 | char node_name[5]; |
| 182 | struct acpi_buffer buffer = { sizeof(node_name), node_name }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
Zhao Yakui | a51e145 | 2008-08-11 14:55:05 +0800 | [diff] [blame] | 185 | if (!handle || !state) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 186 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
Zhao Yakui | a51e145 | 2008-08-11 14:55:05 +0800 | [diff] [blame] | 188 | status = acpi_evaluate_integer(handle, "_STA", NULL, &sta); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | if (ACPI_FAILURE(status)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 190 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
Alexey Starikovskiy | c35923b | 2007-10-22 14:19:09 +0400 | [diff] [blame] | 192 | *state = (sta & 0x01)?ACPI_POWER_RESOURCE_STATE_ON: |
| 193 | ACPI_POWER_RESOURCE_STATE_OFF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | |
Lin Ming | 60a4ce7 | 2008-12-16 17:02:22 +0800 | [diff] [blame] | 195 | acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer); |
| 196 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n", |
Lin Ming | 60a4ce7 | 2008-12-16 17:02:22 +0800 | [diff] [blame] | 198 | node_name, |
Zhao Yakui | b1b57fb | 2008-10-27 16:04:53 +0800 | [diff] [blame] | 199 | *state ? "on" : "off")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 201 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 204 | static int acpi_power_get_list_state(struct list_head *list, int *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | { |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 206 | struct acpi_power_resource_entry *entry; |
Rafael J. Wysocki | d0515d9 | 2011-01-06 23:38:57 +0100 | [diff] [blame] | 207 | int cur_state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
| 209 | if (!list || !state) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 210 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
| 212 | /* The state of the list is 'on' IFF all resources are 'on'. */ |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 213 | list_for_each_entry(entry, list, node) { |
| 214 | struct acpi_power_resource *resource = entry->resource; |
| 215 | acpi_handle handle = resource->device.handle; |
Rafael J. Wysocki | d0515d9 | 2011-01-06 23:38:57 +0100 | [diff] [blame] | 216 | int result; |
| 217 | |
Rafael J. Wysocki | d0515d9 | 2011-01-06 23:38:57 +0100 | [diff] [blame] | 218 | mutex_lock(&resource->resource_lock); |
Rafael J. Wysocki | d0515d9 | 2011-01-06 23:38:57 +0100 | [diff] [blame] | 219 | result = acpi_power_get_state(handle, &cur_state); |
Rafael J. Wysocki | d0515d9 | 2011-01-06 23:38:57 +0100 | [diff] [blame] | 220 | mutex_unlock(&resource->resource_lock); |
Rafael J. Wysocki | d0515d9 | 2011-01-06 23:38:57 +0100 | [diff] [blame] | 221 | if (result) |
| 222 | return result; |
| 223 | |
| 224 | if (cur_state != ACPI_POWER_RESOURCE_STATE_ON) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | break; |
| 226 | } |
| 227 | |
| 228 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n", |
Rafael J. Wysocki | d0515d9 | 2011-01-06 23:38:57 +0100 | [diff] [blame] | 229 | cur_state ? "on" : "off")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
Rafael J. Wysocki | d0515d9 | 2011-01-06 23:38:57 +0100 | [diff] [blame] | 231 | *state = cur_state; |
Rafael J. Wysocki | d0515d9 | 2011-01-06 23:38:57 +0100 | [diff] [blame] | 232 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 235 | static void acpi_power_resume_dependent(struct work_struct *work) |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 236 | { |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 237 | struct acpi_power_dependent_device *dep; |
| 238 | struct acpi_device_physical_node *pn; |
| 239 | struct acpi_device *adev; |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 240 | int state; |
| 241 | |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 242 | dep = container_of(work, struct acpi_power_dependent_device, work); |
| 243 | adev = dep->adev; |
| 244 | if (acpi_power_get_inferred_state(adev, &state)) |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 245 | return; |
| 246 | |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 247 | if (state > ACPI_STATE_D0) |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 248 | return; |
| 249 | |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 250 | mutex_lock(&adev->physical_node_lock); |
| 251 | |
| 252 | list_for_each_entry(pn, &adev->physical_node_list, node) |
| 253 | pm_request_resume(pn->dev); |
| 254 | |
| 255 | list_for_each_entry(pn, &adev->power_dependent, node) |
| 256 | pm_request_resume(pn->dev); |
| 257 | |
| 258 | mutex_unlock(&adev->physical_node_lock); |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 259 | } |
| 260 | |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 261 | static int __acpi_power_on(struct acpi_power_resource *resource) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 263 | acpi_status status = AE_OK; |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 264 | |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 265 | status = acpi_evaluate_object(resource->device.handle, "_ON", NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | if (ACPI_FAILURE(status)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 267 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 269 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned on\n", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 270 | resource->name)); |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 271 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 272 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 275 | static int acpi_power_on(struct acpi_power_resource *resource) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | { |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 277 | int result = 0;; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 279 | mutex_lock(&resource->resource_lock); |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 280 | |
| 281 | if (resource->ref_count++) { |
| 282 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
| 283 | "Power resource [%s] already on", |
| 284 | resource->name)); |
| 285 | } else { |
| 286 | result = __acpi_power_on(resource); |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 287 | if (result) { |
Rafael J. Wysocki | 12b3b5a | 2010-11-25 00:03:32 +0100 | [diff] [blame] | 288 | resource->ref_count--; |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 289 | } else { |
| 290 | struct acpi_power_dependent_device *dep; |
| 291 | |
| 292 | list_for_each_entry(dep, &resource->dependent, node) |
| 293 | schedule_work(&dep->work); |
| 294 | } |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 295 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 297 | mutex_unlock(&resource->resource_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | |
Rafael J. Wysocki | 12b3b5a | 2010-11-25 00:03:32 +0100 | [diff] [blame] | 299 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 302 | static int acpi_power_off(struct acpi_power_resource *resource) |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 303 | { |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 304 | acpi_status status = AE_OK; |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 305 | int result = 0; |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 306 | |
| 307 | mutex_lock(&resource->resource_lock); |
| 308 | |
| 309 | if (!resource->ref_count) { |
| 310 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
| 311 | "Power resource [%s] already off", |
| 312 | resource->name)); |
| 313 | goto unlock; |
| 314 | } |
| 315 | |
| 316 | if (--resource->ref_count) { |
| 317 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
| 318 | "Power resource [%s] still in use\n", |
| 319 | resource->name)); |
| 320 | goto unlock; |
| 321 | } |
| 322 | |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 323 | status = acpi_evaluate_object(resource->device.handle, "_OFF", NULL, NULL); |
Rafael J. Wysocki | 722c929 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 324 | if (ACPI_FAILURE(status)) |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 325 | result = -ENODEV; |
Rafael J. Wysocki | 722c929 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 326 | else |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 327 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
| 328 | "Power resource [%s] turned off\n", |
| 329 | resource->name)); |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 330 | |
| 331 | unlock: |
| 332 | mutex_unlock(&resource->resource_lock); |
| 333 | |
| 334 | return result; |
| 335 | } |
| 336 | |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 337 | static int acpi_power_off_list(struct list_head *list) |
Rafael J. Wysocki | d2ef555 | 2010-11-25 00:06:09 +0100 | [diff] [blame] | 338 | { |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 339 | struct acpi_power_resource_entry *entry; |
Rafael J. Wysocki | d2ef555 | 2010-11-25 00:06:09 +0100 | [diff] [blame] | 340 | int result = 0; |
Rafael J. Wysocki | d2ef555 | 2010-11-25 00:06:09 +0100 | [diff] [blame] | 341 | |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 342 | list_for_each_entry_reverse(entry, list, node) { |
| 343 | result = acpi_power_off(entry->resource); |
| 344 | if (result) |
| 345 | goto err; |
Rafael J. Wysocki | d2ef555 | 2010-11-25 00:06:09 +0100 | [diff] [blame] | 346 | } |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 347 | return 0; |
| 348 | |
| 349 | err: |
| 350 | list_for_each_entry_continue(entry, list, node) |
| 351 | acpi_power_on(entry->resource); |
Rafael J. Wysocki | d2ef555 | 2010-11-25 00:06:09 +0100 | [diff] [blame] | 352 | |
| 353 | return result; |
| 354 | } |
| 355 | |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 356 | static int acpi_power_on_list(struct list_head *list) |
| 357 | { |
| 358 | struct acpi_power_resource_entry *entry; |
| 359 | int result = 0; |
| 360 | |
| 361 | list_for_each_entry(entry, list, node) { |
| 362 | result = acpi_power_on(entry->resource); |
| 363 | if (result) |
| 364 | goto err; |
| 365 | } |
| 366 | return 0; |
| 367 | |
| 368 | err: |
| 369 | list_for_each_entry_continue_reverse(entry, list, node) |
| 370 | acpi_power_off(entry->resource); |
| 371 | |
| 372 | return result; |
| 373 | } |
| 374 | |
| 375 | static void acpi_power_add_dependent(struct acpi_power_resource *resource, |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 376 | struct acpi_device *adev) |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 377 | { |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 378 | struct acpi_power_dependent_device *dep; |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 379 | |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 380 | mutex_lock(&resource->resource_lock); |
| 381 | |
| 382 | list_for_each_entry(dep, &resource->dependent, node) |
| 383 | if (dep->adev == adev) |
| 384 | goto out; |
| 385 | |
| 386 | dep = kzalloc(sizeof(*dep), GFP_KERNEL); |
| 387 | if (!dep) |
| 388 | goto out; |
| 389 | |
| 390 | dep->adev = adev; |
| 391 | INIT_WORK(&dep->work, acpi_power_resume_dependent); |
| 392 | list_add_tail(&dep->node, &resource->dependent); |
| 393 | |
| 394 | out: |
| 395 | mutex_unlock(&resource->resource_lock); |
| 396 | } |
| 397 | |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 398 | static void acpi_power_remove_dependent(struct acpi_power_resource *resource, |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 399 | struct acpi_device *adev) |
| 400 | { |
| 401 | struct acpi_power_dependent_device *dep; |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 402 | struct work_struct *work = NULL; |
| 403 | |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 404 | mutex_lock(&resource->resource_lock); |
| 405 | |
| 406 | list_for_each_entry(dep, &resource->dependent, node) |
| 407 | if (dep->adev == adev) { |
| 408 | list_del(&dep->node); |
| 409 | work = &dep->work; |
| 410 | break; |
| 411 | } |
| 412 | |
| 413 | mutex_unlock(&resource->resource_lock); |
| 414 | |
| 415 | if (work) { |
| 416 | cancel_work_sync(work); |
| 417 | kfree(dep); |
| 418 | } |
| 419 | } |
| 420 | |
Rafael J. Wysocki | 18a3870 | 2013-01-25 21:51:32 +0100 | [diff] [blame^] | 421 | static struct attribute *attrs[] = { |
| 422 | NULL, |
| 423 | }; |
| 424 | |
| 425 | static struct attribute_group attr_groups[] = { |
| 426 | [ACPI_STATE_D0] = { |
| 427 | .name = "power_resources_D0", |
| 428 | .attrs = attrs, |
| 429 | }, |
| 430 | [ACPI_STATE_D1] = { |
| 431 | .name = "power_resources_D1", |
| 432 | .attrs = attrs, |
| 433 | }, |
| 434 | [ACPI_STATE_D2] = { |
| 435 | .name = "power_resources_D2", |
| 436 | .attrs = attrs, |
| 437 | }, |
| 438 | [ACPI_STATE_D3_HOT] = { |
| 439 | .name = "power_resources_D3hot", |
| 440 | .attrs = attrs, |
| 441 | }, |
| 442 | }; |
| 443 | |
| 444 | static void acpi_power_hide_list(struct acpi_device *adev, int state) |
| 445 | { |
| 446 | struct acpi_device_power_state *ps = &adev->power.states[state]; |
| 447 | struct acpi_power_resource_entry *entry; |
| 448 | |
| 449 | if (list_empty(&ps->resources)) |
| 450 | return; |
| 451 | |
| 452 | list_for_each_entry_reverse(entry, &ps->resources, node) { |
| 453 | struct acpi_device *res_dev = &entry->resource->device; |
| 454 | |
| 455 | sysfs_remove_link_from_group(&adev->dev.kobj, |
| 456 | attr_groups[state].name, |
| 457 | dev_name(&res_dev->dev)); |
| 458 | } |
| 459 | sysfs_remove_group(&adev->dev.kobj, &attr_groups[state]); |
| 460 | } |
| 461 | |
| 462 | static void acpi_power_expose_list(struct acpi_device *adev, int state) |
| 463 | { |
| 464 | struct acpi_device_power_state *ps = &adev->power.states[state]; |
| 465 | struct acpi_power_resource_entry *entry; |
| 466 | int ret; |
| 467 | |
| 468 | if (list_empty(&ps->resources)) |
| 469 | return; |
| 470 | |
| 471 | ret = sysfs_create_group(&adev->dev.kobj, &attr_groups[state]); |
| 472 | if (ret) |
| 473 | return; |
| 474 | |
| 475 | list_for_each_entry(entry, &ps->resources, node) { |
| 476 | struct acpi_device *res_dev = &entry->resource->device; |
| 477 | |
| 478 | ret = sysfs_add_link_to_group(&adev->dev.kobj, |
| 479 | attr_groups[state].name, |
| 480 | &res_dev->dev.kobj, |
| 481 | dev_name(&res_dev->dev)); |
| 482 | if (ret) { |
| 483 | acpi_power_hide_list(adev, state); |
| 484 | break; |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 489 | void acpi_power_add_remove_device(struct acpi_device *adev, bool add) |
| 490 | { |
Rafael J. Wysocki | 18a3870 | 2013-01-25 21:51:32 +0100 | [diff] [blame^] | 491 | struct acpi_device_power_state *ps; |
| 492 | struct acpi_power_resource_entry *entry; |
| 493 | int state; |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 494 | |
Rafael J. Wysocki | 18a3870 | 2013-01-25 21:51:32 +0100 | [diff] [blame^] | 495 | if (!adev->power.flags.power_resources) |
| 496 | return; |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 497 | |
Rafael J. Wysocki | 18a3870 | 2013-01-25 21:51:32 +0100 | [diff] [blame^] | 498 | ps = &adev->power.states[ACPI_STATE_D0]; |
| 499 | list_for_each_entry(entry, &ps->resources, node) { |
| 500 | struct acpi_power_resource *resource = entry->resource; |
| 501 | |
| 502 | if (add) |
| 503 | acpi_power_add_dependent(resource, adev); |
| 504 | else |
| 505 | acpi_power_remove_dependent(resource, adev); |
| 506 | } |
| 507 | |
| 508 | for (state = ACPI_STATE_D0; state <= ACPI_STATE_D3_HOT; state++) { |
| 509 | if (add) |
| 510 | acpi_power_expose_list(adev, state); |
| 511 | else |
| 512 | acpi_power_hide_list(adev, state); |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 513 | } |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 514 | } |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 515 | |
Rafael J. Wysocki | 0596a52 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 516 | int acpi_power_min_system_level(struct list_head *list) |
| 517 | { |
| 518 | struct acpi_power_resource_entry *entry; |
| 519 | int system_level = 5; |
| 520 | |
| 521 | list_for_each_entry(entry, list, node) { |
| 522 | struct acpi_power_resource *resource = entry->resource; |
| 523 | |
| 524 | if (system_level > resource->system_level) |
| 525 | system_level = resource->system_level; |
| 526 | } |
| 527 | return system_level; |
| 528 | } |
| 529 | |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 530 | /* -------------------------------------------------------------------------- |
| 531 | Device Power Management |
| 532 | -------------------------------------------------------------------------- */ |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 533 | |
Rafael J. Wysocki | 77e7660 | 2008-07-07 03:33:34 +0200 | [diff] [blame] | 534 | /** |
| 535 | * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in |
| 536 | * ACPI 3.0) _PSW (Power State Wake) |
| 537 | * @dev: Device to handle. |
| 538 | * @enable: 0 - disable, 1 - enable the wake capabilities of the device. |
| 539 | * @sleep_state: Target sleep state of the system. |
| 540 | * @dev_state: Target power state of the device. |
| 541 | * |
| 542 | * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power |
| 543 | * State Wake) for the device, if present. On failure reset the device's |
| 544 | * wakeup.flags.valid flag. |
| 545 | * |
| 546 | * RETURN VALUE: |
| 547 | * 0 if either _DSW or _PSW has been successfully executed |
| 548 | * 0 if neither _DSW nor _PSW has been found |
| 549 | * -ENODEV if the execution of either _DSW or _PSW has failed |
| 550 | */ |
| 551 | int acpi_device_sleep_wake(struct acpi_device *dev, |
| 552 | int enable, int sleep_state, int dev_state) |
| 553 | { |
| 554 | union acpi_object in_arg[3]; |
| 555 | struct acpi_object_list arg_list = { 3, in_arg }; |
| 556 | acpi_status status = AE_OK; |
| 557 | |
| 558 | /* |
| 559 | * Try to execute _DSW first. |
| 560 | * |
| 561 | * Three agruments are needed for the _DSW object: |
| 562 | * Argument 0: enable/disable the wake capabilities |
| 563 | * Argument 1: target system state |
| 564 | * Argument 2: target device state |
| 565 | * When _DSW object is called to disable the wake capabilities, maybe |
| 566 | * the first argument is filled. The values of the other two agruments |
| 567 | * are meaningless. |
| 568 | */ |
| 569 | in_arg[0].type = ACPI_TYPE_INTEGER; |
| 570 | in_arg[0].integer.value = enable; |
| 571 | in_arg[1].type = ACPI_TYPE_INTEGER; |
| 572 | in_arg[1].integer.value = sleep_state; |
| 573 | in_arg[2].type = ACPI_TYPE_INTEGER; |
| 574 | in_arg[2].integer.value = dev_state; |
| 575 | status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL); |
| 576 | if (ACPI_SUCCESS(status)) { |
| 577 | return 0; |
| 578 | } else if (status != AE_NOT_FOUND) { |
| 579 | printk(KERN_ERR PREFIX "_DSW execution failed\n"); |
| 580 | dev->wakeup.flags.valid = 0; |
| 581 | return -ENODEV; |
| 582 | } |
| 583 | |
| 584 | /* Execute _PSW */ |
| 585 | arg_list.count = 1; |
| 586 | in_arg[0].integer.value = enable; |
| 587 | status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL); |
| 588 | if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) { |
| 589 | printk(KERN_ERR PREFIX "_PSW execution failed\n"); |
| 590 | dev->wakeup.flags.valid = 0; |
| 591 | return -ENODEV; |
| 592 | } |
| 593 | |
| 594 | return 0; |
| 595 | } |
| 596 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | /* |
| 598 | * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229): |
| 599 | * 1. Power on the power resources required for the wakeup device |
Rafael J. Wysocki | 77e7660 | 2008-07-07 03:33:34 +0200 | [diff] [blame] | 600 | * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power |
| 601 | * State Wake) for the device, if present |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | */ |
Rafael J. Wysocki | 77e7660 | 2008-07-07 03:33:34 +0200 | [diff] [blame] | 603 | int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | { |
Rafael J. Wysocki | 993cbe5 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 605 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | if (!dev || !dev->wakeup.flags.valid) |
Rafael J. Wysocki | 77e7660 | 2008-07-07 03:33:34 +0200 | [diff] [blame] | 608 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 610 | mutex_lock(&acpi_device_lock); |
| 611 | |
| 612 | if (dev->wakeup.prepare_count++) |
| 613 | goto out; |
Rafael J. Wysocki | 0af4b8c | 2008-07-07 03:34:11 +0200 | [diff] [blame] | 614 | |
Rafael J. Wysocki | 993cbe5 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 615 | err = acpi_power_on_list(&dev->wakeup.resources); |
| 616 | if (err) { |
| 617 | dev_err(&dev->dev, "Cannot turn wakeup power resources on\n"); |
| 618 | dev->wakeup.flags.valid = 0; |
| 619 | } else { |
| 620 | /* |
| 621 | * Passing 3 as the third argument below means the device may be |
| 622 | * put into arbitrary power state afterward. |
| 623 | */ |
| 624 | err = acpi_device_sleep_wake(dev, 1, sleep_state, 3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | } |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 626 | if (err) |
| 627 | dev->wakeup.prepare_count = 0; |
| 628 | |
| 629 | out: |
| 630 | mutex_unlock(&acpi_device_lock); |
Rafael J. Wysocki | 0af4b8c | 2008-07-07 03:34:11 +0200 | [diff] [blame] | 631 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | /* |
| 635 | * Shutdown a wakeup device, counterpart of above method |
Rafael J. Wysocki | 77e7660 | 2008-07-07 03:33:34 +0200 | [diff] [blame] | 636 | * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power |
| 637 | * State Wake) for the device, if present |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | * 2. Shutdown down the power resources |
| 639 | */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 640 | int acpi_disable_wakeup_device_power(struct acpi_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | { |
Rafael J. Wysocki | 993cbe5 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 642 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | |
| 644 | if (!dev || !dev->wakeup.flags.valid) |
Rafael J. Wysocki | 77e7660 | 2008-07-07 03:33:34 +0200 | [diff] [blame] | 645 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 647 | mutex_lock(&acpi_device_lock); |
| 648 | |
| 649 | if (--dev->wakeup.prepare_count > 0) |
| 650 | goto out; |
| 651 | |
Rafael J. Wysocki | 0af4b8c | 2008-07-07 03:34:11 +0200 | [diff] [blame] | 652 | /* |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 653 | * Executing the code below even if prepare_count is already zero when |
| 654 | * the function is called may be useful, for example for initialisation. |
Rafael J. Wysocki | 0af4b8c | 2008-07-07 03:34:11 +0200 | [diff] [blame] | 655 | */ |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 656 | if (dev->wakeup.prepare_count < 0) |
| 657 | dev->wakeup.prepare_count = 0; |
Rafael J. Wysocki | 0af4b8c | 2008-07-07 03:34:11 +0200 | [diff] [blame] | 658 | |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 659 | err = acpi_device_sleep_wake(dev, 0, 0, 0); |
| 660 | if (err) |
| 661 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | |
Rafael J. Wysocki | 993cbe5 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 663 | err = acpi_power_off_list(&dev->wakeup.resources); |
| 664 | if (err) { |
| 665 | dev_err(&dev->dev, "Cannot turn wakeup power resources off\n"); |
| 666 | dev->wakeup.flags.valid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | } |
| 668 | |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 669 | out: |
| 670 | mutex_unlock(&acpi_device_lock); |
| 671 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | } |
| 673 | |
Rafael J. Wysocki | 32a00d2 | 2010-11-25 00:05:17 +0100 | [diff] [blame] | 674 | int acpi_power_get_inferred_state(struct acpi_device *device, int *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 676 | int result = 0; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 677 | int list_state = 0; |
| 678 | int i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | |
Rafael J. Wysocki | 32a00d2 | 2010-11-25 00:05:17 +0100 | [diff] [blame] | 680 | if (!device || !state) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 681 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | /* |
| 684 | * We know a device's inferred power state when all the resources |
| 685 | * required for a given D-state are 'on'. |
| 686 | */ |
Rafael J. Wysocki | 38c92ff | 2012-05-20 13:58:00 +0200 | [diff] [blame] | 687 | for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) { |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 688 | struct list_head *list = &device->power.states[i].resources; |
| 689 | |
| 690 | if (list_empty(list)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | continue; |
| 692 | |
| 693 | result = acpi_power_get_list_state(list, &list_state); |
| 694 | if (result) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 695 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | |
| 697 | if (list_state == ACPI_POWER_RESOURCE_STATE_ON) { |
Rafael J. Wysocki | 32a00d2 | 2010-11-25 00:05:17 +0100 | [diff] [blame] | 698 | *state = i; |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 699 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | } |
| 701 | } |
| 702 | |
Rafael J. Wysocki | 32a00d2 | 2010-11-25 00:05:17 +0100 | [diff] [blame] | 703 | *state = ACPI_STATE_D3; |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 704 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | } |
| 706 | |
Rafael J. Wysocki | 30d3df41 | 2010-11-25 00:06:55 +0100 | [diff] [blame] | 707 | int acpi_power_on_resources(struct acpi_device *device, int state) |
| 708 | { |
Rafael J. Wysocki | 87e753b | 2013-01-22 12:56:16 +0100 | [diff] [blame] | 709 | if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3_HOT) |
Rafael J. Wysocki | 30d3df41 | 2010-11-25 00:06:55 +0100 | [diff] [blame] | 710 | return -EINVAL; |
| 711 | |
| 712 | return acpi_power_on_list(&device->power.states[state].resources); |
| 713 | } |
| 714 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 715 | int acpi_power_transition(struct acpi_device *device, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | { |
Rafael J. Wysocki | 5c7dd71 | 2012-05-18 00:39:35 +0200 | [diff] [blame] | 717 | int result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | |
Zhang Rui | 3ebc81b | 2012-03-29 14:09:38 +0800 | [diff] [blame] | 719 | if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 720 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 722 | if (device->power.state == state || !device->flags.power_manageable) |
Rafael J. Wysocki | 212967c | 2010-11-25 00:02:36 +0100 | [diff] [blame] | 723 | return 0; |
| 724 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 725 | if ((device->power.state < ACPI_STATE_D0) |
Zhang Rui | 3ebc81b | 2012-03-29 14:09:38 +0800 | [diff] [blame] | 726 | || (device->power.state > ACPI_STATE_D3_COLD)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 727 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | /* TBD: Resources must be ordered. */ |
| 730 | |
| 731 | /* |
| 732 | * First we reference all power resources required in the target list |
Rafael J. Wysocki | d2ef555 | 2010-11-25 00:06:09 +0100 | [diff] [blame] | 733 | * (e.g. so the device doesn't lose power while transitioning). Then, |
| 734 | * we dereference all power resources used in the current list. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | */ |
Rafael J. Wysocki | 5c7dd71 | 2012-05-18 00:39:35 +0200 | [diff] [blame] | 736 | if (state < ACPI_STATE_D3_COLD) |
| 737 | result = acpi_power_on_list( |
| 738 | &device->power.states[state].resources); |
| 739 | |
| 740 | if (!result && device->power.state < ACPI_STATE_D3_COLD) |
Rafael J. Wysocki | d2ef555 | 2010-11-25 00:06:09 +0100 | [diff] [blame] | 741 | acpi_power_off_list( |
| 742 | &device->power.states[device->power.state].resources); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | |
Rafael J. Wysocki | d2ef555 | 2010-11-25 00:06:09 +0100 | [diff] [blame] | 744 | /* We shouldn't change the state unless the above operations succeed. */ |
| 745 | device->power.state = result ? ACPI_STATE_UNKNOWN : state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 747 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | } |
| 749 | |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 750 | static void acpi_release_power_resource(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | { |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 752 | struct acpi_device *device = to_acpi_device(dev); |
| 753 | struct acpi_power_resource *resource; |
| 754 | |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 755 | resource = container_of(device, struct acpi_power_resource, device); |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 756 | |
| 757 | mutex_lock(&power_resource_list_lock); |
| 758 | list_del(&resource->list_node); |
| 759 | mutex_unlock(&power_resource_list_lock); |
| 760 | |
| 761 | acpi_free_ids(device); |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 762 | kfree(resource); |
| 763 | } |
| 764 | |
Rafael J. Wysocki | b1c0f99 | 2013-01-24 12:50:09 +0100 | [diff] [blame] | 765 | static ssize_t acpi_power_in_use_show(struct device *dev, |
| 766 | struct device_attribute *attr, |
| 767 | char *buf) { |
| 768 | struct acpi_power_resource *resource; |
| 769 | |
| 770 | resource = to_power_resource(to_acpi_device(dev)); |
| 771 | return sprintf(buf, "%u\n", !!resource->ref_count); |
| 772 | } |
| 773 | static DEVICE_ATTR(resource_in_use, 0444, acpi_power_in_use_show, NULL); |
| 774 | |
| 775 | static void acpi_power_sysfs_remove(struct acpi_device *device) |
| 776 | { |
| 777 | device_remove_file(&device->dev, &dev_attr_resource_in_use); |
| 778 | } |
| 779 | |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 780 | int acpi_add_power_resource(acpi_handle handle) |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 781 | { |
| 782 | struct acpi_power_resource *resource; |
| 783 | struct acpi_device *device = NULL; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 784 | union acpi_object acpi_object; |
| 785 | struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object }; |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 786 | acpi_status status; |
| 787 | int state, result = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 789 | acpi_bus_get_device(handle, &device); |
| 790 | if (device) |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 791 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 793 | resource = kzalloc(sizeof(*resource), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | if (!resource) |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 795 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 797 | device = &resource->device; |
| 798 | acpi_init_device_object(device, handle, ACPI_BUS_TYPE_POWER, |
| 799 | ACPI_STA_DEFAULT); |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 800 | mutex_init(&resource->resource_lock); |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 801 | INIT_LIST_HEAD(&resource->dependent); |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 802 | resource->name = device->pnp.bus_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME); |
| 804 | strcpy(acpi_device_class(device), ACPI_POWER_CLASS); |
Rafael J. Wysocki | 722c929 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 805 | device->power.state = ACPI_STATE_UNKNOWN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | |
| 807 | /* Evalute the object to get the system level and resource order. */ |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 808 | status = acpi_evaluate_object(handle, NULL, NULL, &buffer); |
| 809 | if (ACPI_FAILURE(status)) |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 810 | goto err; |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 811 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | resource->system_level = acpi_object.power_resource.system_level; |
| 813 | resource->order = acpi_object.power_resource.resource_order; |
| 814 | |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 815 | result = acpi_power_get_state(handle, &state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | if (result) |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 817 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 819 | printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device), |
Alexey Starikovskiy | c35923b | 2007-10-22 14:19:09 +0400 | [diff] [blame] | 820 | acpi_device_bid(device), state ? "on" : "off"); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 821 | |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 822 | device->flags.match_driver = true; |
Rafael J. Wysocki | cf860be | 2013-01-24 12:49:49 +0100 | [diff] [blame] | 823 | result = acpi_device_add(device, acpi_release_power_resource); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | if (result) |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 825 | goto err; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 826 | |
Rafael J. Wysocki | b1c0f99 | 2013-01-24 12:50:09 +0100 | [diff] [blame] | 827 | if (!device_create_file(&device->dev, &dev_attr_resource_in_use)) |
| 828 | device->remove = acpi_power_sysfs_remove; |
| 829 | |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 830 | mutex_lock(&power_resource_list_lock); |
| 831 | list_add(&resource->list_node, &acpi_power_resource_list); |
| 832 | mutex_unlock(&power_resource_list_lock); |
Rafael J. Wysocki | cf860be | 2013-01-24 12:49:49 +0100 | [diff] [blame] | 833 | acpi_device_add_finalize(device); |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 834 | return 0; |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 835 | |
| 836 | err: |
| 837 | acpi_release_power_resource(&device->dev); |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 838 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | } |
| 840 | |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 841 | #ifdef CONFIG_ACPI_SLEEP |
| 842 | void acpi_resume_power_resources(void) |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 843 | { |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 844 | struct acpi_power_resource *resource; |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 845 | |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 846 | mutex_lock(&power_resource_list_lock); |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 847 | |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 848 | list_for_each_entry(resource, &acpi_power_resource_list, list_node) { |
| 849 | int result, state; |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 850 | |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 851 | mutex_lock(&resource->resource_lock); |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 852 | |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 853 | result = acpi_power_get_state(resource->device.handle, &state); |
| 854 | if (!result && state == ACPI_POWER_RESOURCE_STATE_OFF |
| 855 | && resource->ref_count) { |
| 856 | dev_info(&resource->device.dev, "Turning ON\n"); |
| 857 | __acpi_power_on(resource); |
| 858 | } |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 859 | |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 860 | mutex_unlock(&resource->resource_lock); |
| 861 | } |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 862 | |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 863 | mutex_unlock(&power_resource_list_lock); |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 864 | } |
Rafael J. Wysocki | 9069240 | 2012-08-09 23:00:02 +0200 | [diff] [blame] | 865 | #endif |