2 * acpi_power.c - ACPI Bus Power Management ($Revision: 39 $)
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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.
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
38 #include <linux/kernel.h>
39 #include <linux/module.h>
40 #include <linux/init.h>
41 #include <linux/types.h>
42 #include <linux/proc_fs.h>
43 #include <linux/seq_file.h>
44 #include <acpi/acpi_bus.h>
45 #include <acpi/acpi_drivers.h>
47 #define _COMPONENT ACPI_POWER_COMPONENT
48 ACPI_MODULE_NAME("acpi_power")
49 #define ACPI_POWER_COMPONENT 0x00800000
50 #define ACPI_POWER_CLASS "power_resource"
51 #define ACPI_POWER_DRIVER_NAME "ACPI Power Resource Driver"
52 #define ACPI_POWER_DEVICE_NAME "Power Resource"
53 #define ACPI_POWER_FILE_INFO "info"
54 #define ACPI_POWER_FILE_STATUS "state"
55 #define ACPI_POWER_RESOURCE_STATE_OFF 0x00
56 #define ACPI_POWER_RESOURCE_STATE_ON 0x01
57 #define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF
58 static int acpi_power_add(struct acpi_device *device);
59 static int acpi_power_remove(struct acpi_device *device, int type);
60 static int acpi_power_resume(struct acpi_device *device, int state);
61 static int acpi_power_open_fs(struct inode *inode, struct file *file);
63 static struct acpi_driver acpi_power_driver = {
64 .name = ACPI_POWER_DRIVER_NAME,
65 .class = ACPI_POWER_CLASS,
66 .ids = ACPI_POWER_HID,
68 .add = acpi_power_add,
69 .remove = acpi_power_remove,
70 .resume = acpi_power_resume,
74 struct acpi_power_reference {
75 struct list_head node;
76 struct acpi_device *device;
79 struct acpi_power_resource {
80 struct acpi_device * device;
85 struct mutex resource_lock;
86 struct list_head reference;
89 static struct list_head acpi_power_resource_list;
91 static const struct file_operations acpi_power_fops = {
92 .open = acpi_power_open_fs,
95 .release = single_release,
98 /* --------------------------------------------------------------------------
99 Power Resource Management
100 -------------------------------------------------------------------------- */
103 acpi_power_get_context(acpi_handle handle,
104 struct acpi_power_resource **resource)
107 struct acpi_device *device = NULL;
113 result = acpi_bus_get_device(handle, &device);
115 printk(KERN_WARNING PREFIX "Getting context [%p]\n", handle);
119 *resource = acpi_driver_data(device);
126 static int acpi_power_get_state(struct acpi_power_resource *resource)
128 acpi_status status = AE_OK;
129 unsigned long sta = 0;
135 status = acpi_evaluate_integer(resource->device->handle, "_STA", NULL, &sta);
136 if (ACPI_FAILURE(status))
140 resource->state = ACPI_POWER_RESOURCE_STATE_ON;
142 resource->state = ACPI_POWER_RESOURCE_STATE_OFF;
144 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n",
145 resource->name, resource->state ? "on" : "off"));
150 static int acpi_power_get_list_state(struct acpi_handle_list *list, int *state)
153 struct acpi_power_resource *resource = NULL;
160 /* The state of the list is 'on' IFF all resources are 'on'. */
162 for (i = 0; i < list->count; i++) {
163 result = acpi_power_get_context(list->handles[i], &resource);
166 result = acpi_power_get_state(resource);
170 *state = resource->state;
172 if (*state != ACPI_POWER_RESOURCE_STATE_ON)
176 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n",
177 *state ? "on" : "off"));
182 static int acpi_power_on(acpi_handle handle, struct acpi_device *dev)
186 acpi_status status = AE_OK;
187 struct acpi_power_resource *resource = NULL;
188 struct list_head *node, *next;
189 struct acpi_power_reference *ref;
192 result = acpi_power_get_context(handle, &resource);
196 mutex_lock(&resource->resource_lock);
197 list_for_each_safe(node, next, &resource->reference) {
198 ref = container_of(node, struct acpi_power_reference, node);
199 if (dev->handle == ref->device->handle) {
200 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] already referenced by resource [%s]\n",
201 dev->pnp.bus_id, resource->name));
208 ref = kmalloc(sizeof (struct acpi_power_reference),
209 irqs_disabled() ? GFP_ATOMIC : GFP_KERNEL);
211 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "kmalloc() failed\n"));
212 mutex_unlock(&resource->resource_lock);
215 list_add_tail(&ref->node, &resource->reference);
217 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] added to resource [%s] references\n",
218 dev->pnp.bus_id, resource->name));
220 mutex_unlock(&resource->resource_lock);
222 if (resource->state == ACPI_POWER_RESOURCE_STATE_ON) {
223 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] already on\n",
228 status = acpi_evaluate_object(resource->device->handle, "_ON", NULL, NULL);
229 if (ACPI_FAILURE(status))
232 result = acpi_power_get_state(resource);
235 if (resource->state != ACPI_POWER_RESOURCE_STATE_ON)
238 /* Update the power resource's _device_ power state */
239 resource->device->power.state = ACPI_STATE_D0;
241 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] turned on\n",
246 static int acpi_power_off_device(acpi_handle handle, struct acpi_device *dev)
249 acpi_status status = AE_OK;
250 struct acpi_power_resource *resource = NULL;
251 struct list_head *node, *next;
252 struct acpi_power_reference *ref;
255 result = acpi_power_get_context(handle, &resource);
259 mutex_lock(&resource->resource_lock);
260 list_for_each_safe(node, next, &resource->reference) {
261 ref = container_of(node, struct acpi_power_reference, node);
262 if (dev->handle == ref->device->handle) {
263 list_del(&ref->node);
265 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] removed from resource [%s] references\n",
266 dev->pnp.bus_id, resource->name));
271 if (!list_empty(&resource->reference)) {
272 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Cannot turn resource [%s] off - resource is in use\n",
274 mutex_unlock(&resource->resource_lock);
277 mutex_unlock(&resource->resource_lock);
279 if (resource->state == ACPI_POWER_RESOURCE_STATE_OFF) {
280 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] already off\n",
285 status = acpi_evaluate_object(resource->device->handle, "_OFF", NULL, NULL);
286 if (ACPI_FAILURE(status))
289 result = acpi_power_get_state(resource);
292 if (resource->state != ACPI_POWER_RESOURCE_STATE_OFF)
295 /* Update the power resource's _device_ power state */
296 resource->device->power.state = ACPI_STATE_D3;
298 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] turned off\n",
305 * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
306 * 1. Power on the power resources required for the wakeup device
307 * 2. Enable _PSW (power state wake) for the device if present
309 int acpi_enable_wakeup_device_power(struct acpi_device *dev)
311 union acpi_object arg = { ACPI_TYPE_INTEGER };
312 struct acpi_object_list arg_list = { 1, &arg };
313 acpi_status status = AE_OK;
317 if (!dev || !dev->wakeup.flags.valid)
320 arg.integer.value = 1;
321 /* Open power resource */
322 for (i = 0; i < dev->wakeup.resources.count; i++) {
323 ret = acpi_power_on(dev->wakeup.resources.handles[i], dev);
325 printk(KERN_ERR PREFIX "Transition power state\n");
326 dev->wakeup.flags.valid = 0;
332 status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL);
333 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
334 printk(KERN_ERR PREFIX "Evaluate _PSW\n");
335 dev->wakeup.flags.valid = 0;
343 * Shutdown a wakeup device, counterpart of above method
344 * 1. Disable _PSW (power state wake)
345 * 2. Shutdown down the power resources
347 int acpi_disable_wakeup_device_power(struct acpi_device *dev)
349 union acpi_object arg = { ACPI_TYPE_INTEGER };
350 struct acpi_object_list arg_list = { 1, &arg };
351 acpi_status status = AE_OK;
356 if (!dev || !dev->wakeup.flags.valid)
359 arg.integer.value = 0;
361 status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL);
362 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
363 printk(KERN_ERR PREFIX "Evaluate _PSW\n");
364 dev->wakeup.flags.valid = 0;
368 /* Close power resource */
369 for (i = 0; i < dev->wakeup.resources.count; i++) {
370 ret = acpi_power_off_device(dev->wakeup.resources.handles[i], dev);
372 printk(KERN_ERR PREFIX "Transition power state\n");
373 dev->wakeup.flags.valid = 0;
381 /* --------------------------------------------------------------------------
382 Device Power Management
383 -------------------------------------------------------------------------- */
385 int acpi_power_get_inferred_state(struct acpi_device *device)
388 struct acpi_handle_list *list = NULL;
396 device->power.state = ACPI_STATE_UNKNOWN;
399 * We know a device's inferred power state when all the resources
400 * required for a given D-state are 'on'.
402 for (i = ACPI_STATE_D0; i < ACPI_STATE_D3; i++) {
403 list = &device->power.states[i].resources;
407 result = acpi_power_get_list_state(list, &list_state);
411 if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
412 device->power.state = i;
417 device->power.state = ACPI_STATE_D3;
422 int acpi_power_transition(struct acpi_device *device, int state)
425 struct acpi_handle_list *cl = NULL; /* Current Resources */
426 struct acpi_handle_list *tl = NULL; /* Target Resources */
430 if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3))
433 if ((device->power.state < ACPI_STATE_D0)
434 || (device->power.state > ACPI_STATE_D3))
437 cl = &device->power.states[device->power.state].resources;
438 tl = &device->power.states[state].resources;
440 device->power.state = ACPI_STATE_UNKNOWN;
442 if (!cl->count && !tl->count) {
447 /* TBD: Resources must be ordered. */
450 * First we reference all power resources required in the target list
451 * (e.g. so the device doesn't lose power while transitioning).
453 for (i = 0; i < tl->count; i++) {
454 result = acpi_power_on(tl->handles[i], device);
460 * Then we dereference all power resources used in the current list.
462 for (i = 0; i < cl->count; i++) {
463 result = acpi_power_off_device(cl->handles[i], device);
468 /* We shouldn't change the state till all above operations succeed */
469 device->power.state = state;
472 printk(KERN_WARNING PREFIX "Transitioning device [%s] to D%d\n",
473 device->pnp.bus_id, state);
478 /* --------------------------------------------------------------------------
480 -------------------------------------------------------------------------- */
482 static struct proc_dir_entry *acpi_power_dir;
484 static int acpi_power_seq_show(struct seq_file *seq, void *offset)
488 struct acpi_power_resource *resource = NULL;
489 struct list_head *node, *next;
490 struct acpi_power_reference *ref;
493 resource = seq->private;
498 result = acpi_power_get_state(resource);
502 seq_puts(seq, "state: ");
503 switch (resource->state) {
504 case ACPI_POWER_RESOURCE_STATE_ON:
505 seq_puts(seq, "on\n");
507 case ACPI_POWER_RESOURCE_STATE_OFF:
508 seq_puts(seq, "off\n");
511 seq_puts(seq, "unknown\n");
515 mutex_lock(&resource->resource_lock);
516 list_for_each_safe(node, next, &resource->reference) {
517 ref = container_of(node, struct acpi_power_reference, node);
520 mutex_unlock(&resource->resource_lock);
522 seq_printf(seq, "system level: S%d\n"
524 "reference count: %d\n",
525 resource->system_level,
526 resource->order, count);
532 static int acpi_power_open_fs(struct inode *inode, struct file *file)
534 return single_open(file, acpi_power_seq_show, PDE(inode)->data);
537 static int acpi_power_add_fs(struct acpi_device *device)
539 struct proc_dir_entry *entry = NULL;
545 if (!acpi_device_dir(device)) {
546 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
548 if (!acpi_device_dir(device))
553 entry = create_proc_entry(ACPI_POWER_FILE_STATUS,
554 S_IRUGO, acpi_device_dir(device));
558 entry->proc_fops = &acpi_power_fops;
559 entry->data = acpi_driver_data(device);
565 static int acpi_power_remove_fs(struct acpi_device *device)
568 if (acpi_device_dir(device)) {
569 remove_proc_entry(ACPI_POWER_FILE_STATUS,
570 acpi_device_dir(device));
571 remove_proc_entry(acpi_device_bid(device), acpi_power_dir);
572 acpi_device_dir(device) = NULL;
578 /* --------------------------------------------------------------------------
580 -------------------------------------------------------------------------- */
582 static int acpi_power_add(struct acpi_device *device)
585 acpi_status status = AE_OK;
586 struct acpi_power_resource *resource = NULL;
587 union acpi_object acpi_object;
588 struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
594 resource = kzalloc(sizeof(struct acpi_power_resource), GFP_KERNEL);
598 resource->device = device;
599 mutex_init(&resource->resource_lock);
600 INIT_LIST_HEAD(&resource->reference);
601 strcpy(resource->name, device->pnp.bus_id);
602 strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
603 strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
604 acpi_driver_data(device) = resource;
606 /* Evalute the object to get the system level and resource order. */
607 status = acpi_evaluate_object(device->handle, NULL, NULL, &buffer);
608 if (ACPI_FAILURE(status)) {
612 resource->system_level = acpi_object.power_resource.system_level;
613 resource->order = acpi_object.power_resource.resource_order;
615 result = acpi_power_get_state(resource);
619 switch (resource->state) {
620 case ACPI_POWER_RESOURCE_STATE_ON:
621 device->power.state = ACPI_STATE_D0;
623 case ACPI_POWER_RESOURCE_STATE_OFF:
624 device->power.state = ACPI_STATE_D3;
627 device->power.state = ACPI_STATE_UNKNOWN;
631 result = acpi_power_add_fs(device);
635 printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device),
636 acpi_device_bid(device), resource->state ? "on" : "off");
645 static int acpi_power_remove(struct acpi_device *device, int type)
647 struct acpi_power_resource *resource = NULL;
648 struct list_head *node, *next;
651 if (!device || !acpi_driver_data(device))
654 resource = acpi_driver_data(device);
656 acpi_power_remove_fs(device);
658 mutex_lock(&resource->resource_lock);
659 list_for_each_safe(node, next, &resource->reference) {
660 struct acpi_power_reference *ref = container_of(node, struct acpi_power_reference, node);
661 list_del(&ref->node);
664 mutex_unlock(&resource->resource_lock);
671 static int acpi_power_resume(struct acpi_device *device, int state)
674 struct acpi_power_resource *resource = NULL;
675 struct acpi_power_reference *ref;
677 if (!device || !acpi_driver_data(device))
680 resource = (struct acpi_power_resource *)acpi_driver_data(device);
682 result = acpi_power_get_state(resource);
686 mutex_lock(&resource->resource_lock);
687 if ((resource->state == ACPI_POWER_RESOURCE_STATE_ON) &&
688 list_empty(&resource->reference)) {
689 mutex_unlock(&resource->resource_lock);
690 result = acpi_power_off_device(device->handle, NULL);
694 if ((resource->state == ACPI_POWER_RESOURCE_STATE_OFF) &&
695 !list_empty(&resource->reference)) {
696 ref = container_of(resource->reference.next, struct acpi_power_reference, node);
697 mutex_unlock(&resource->resource_lock);
698 result = acpi_power_on(device->handle, ref->device);
702 mutex_unlock(&resource->resource_lock);
706 static int __init acpi_power_init(void)
714 INIT_LIST_HEAD(&acpi_power_resource_list);
716 acpi_power_dir = proc_mkdir(ACPI_POWER_CLASS, acpi_root_dir);
720 result = acpi_bus_register_driver(&acpi_power_driver);
722 remove_proc_entry(ACPI_POWER_CLASS, acpi_root_dir);
729 subsys_initcall(acpi_power_init);