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_open_fs(struct inode *inode, struct file *file);
62 static struct acpi_driver acpi_power_driver = {
63 .name = ACPI_POWER_DRIVER_NAME,
64 .class = ACPI_POWER_CLASS,
65 .ids = ACPI_POWER_HID,
67 .add = acpi_power_add,
68 .remove = acpi_power_remove,
72 struct acpi_power_resource {
81 static struct list_head acpi_power_resource_list;
83 static struct file_operations acpi_power_fops = {
84 .open = acpi_power_open_fs,
87 .release = single_release,
90 /* --------------------------------------------------------------------------
91 Power Resource Management
92 -------------------------------------------------------------------------- */
95 acpi_power_get_context(acpi_handle handle,
96 struct acpi_power_resource **resource)
99 struct acpi_device *device = NULL;
101 ACPI_FUNCTION_TRACE("acpi_power_get_context");
104 return_VALUE(-ENODEV);
106 result = acpi_bus_get_device(handle, &device);
108 printk(KERN_WARNING PREFIX "Getting context [%p]\n", handle);
109 return_VALUE(result);
112 *resource = (struct acpi_power_resource *)acpi_driver_data(device);
114 return_VALUE(-ENODEV);
119 static int acpi_power_get_state(struct acpi_power_resource *resource)
121 acpi_status status = AE_OK;
122 unsigned long sta = 0;
124 ACPI_FUNCTION_TRACE("acpi_power_get_state");
127 return_VALUE(-EINVAL);
129 status = acpi_evaluate_integer(resource->handle, "_STA", NULL, &sta);
130 if (ACPI_FAILURE(status))
131 return_VALUE(-ENODEV);
134 resource->state = ACPI_POWER_RESOURCE_STATE_ON;
136 resource->state = ACPI_POWER_RESOURCE_STATE_OFF;
138 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n",
139 resource->name, resource->state ? "on" : "off"));
144 static int acpi_power_get_list_state(struct acpi_handle_list *list, int *state)
147 struct acpi_power_resource *resource = NULL;
150 ACPI_FUNCTION_TRACE("acpi_power_get_list_state");
153 return_VALUE(-EINVAL);
155 /* The state of the list is 'on' IFF all resources are 'on'. */
157 for (i = 0; i < list->count; i++) {
158 result = acpi_power_get_context(list->handles[i], &resource);
160 return_VALUE(result);
161 result = acpi_power_get_state(resource);
163 return_VALUE(result);
165 *state = resource->state;
167 if (*state != ACPI_POWER_RESOURCE_STATE_ON)
171 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n",
172 *state ? "on" : "off"));
174 return_VALUE(result);
177 static int acpi_power_on(acpi_handle handle)
180 acpi_status status = AE_OK;
181 struct acpi_device *device = NULL;
182 struct acpi_power_resource *resource = NULL;
184 ACPI_FUNCTION_TRACE("acpi_power_on");
186 result = acpi_power_get_context(handle, &resource);
188 return_VALUE(result);
190 resource->references++;
192 if ((resource->references > 1)
193 || (resource->state == ACPI_POWER_RESOURCE_STATE_ON)) {
194 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] already on\n",
199 status = acpi_evaluate_object(resource->handle, "_ON", NULL, NULL);
200 if (ACPI_FAILURE(status))
201 return_VALUE(-ENODEV);
203 result = acpi_power_get_state(resource);
205 return_VALUE(result);
206 if (resource->state != ACPI_POWER_RESOURCE_STATE_ON)
207 return_VALUE(-ENOEXEC);
209 /* Update the power resource's _device_ power state */
210 result = acpi_bus_get_device(resource->handle, &device);
212 return_VALUE(result);
213 device->power.state = ACPI_STATE_D0;
215 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] turned on\n",
221 static int acpi_power_off_device(acpi_handle handle)
224 acpi_status status = AE_OK;
225 struct acpi_device *device = NULL;
226 struct acpi_power_resource *resource = NULL;
228 ACPI_FUNCTION_TRACE("acpi_power_off_device");
230 result = acpi_power_get_context(handle, &resource);
232 return_VALUE(result);
234 if (resource->references)
235 resource->references--;
237 if (resource->references) {
238 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
239 "Resource [%s] is still in use, dereferencing\n",
240 device->pnp.bus_id));
244 if (resource->state == ACPI_POWER_RESOURCE_STATE_OFF) {
245 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] already off\n",
246 device->pnp.bus_id));
250 status = acpi_evaluate_object(resource->handle, "_OFF", NULL, NULL);
251 if (ACPI_FAILURE(status))
252 return_VALUE(-ENODEV);
254 result = acpi_power_get_state(resource);
256 return_VALUE(result);
257 if (resource->state != ACPI_POWER_RESOURCE_STATE_OFF)
258 return_VALUE(-ENOEXEC);
260 /* Update the power resource's _device_ power state */
261 result = acpi_bus_get_device(resource->handle, &device);
263 return_VALUE(result);
264 device->power.state = ACPI_STATE_D3;
266 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] turned off\n",
273 * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
274 * 1. Power on the power resources required for the wakeup device
275 * 2. Enable _PSW (power state wake) for the device if present
277 int acpi_enable_wakeup_device_power(struct acpi_device *dev)
279 union acpi_object arg = { ACPI_TYPE_INTEGER };
280 struct acpi_object_list arg_list = { 1, &arg };
281 acpi_status status = AE_OK;
285 ACPI_FUNCTION_TRACE("acpi_enable_wakeup_device_power");
286 if (!dev || !dev->wakeup.flags.valid)
289 arg.integer.value = 1;
290 /* Open power resource */
291 for (i = 0; i < dev->wakeup.resources.count; i++) {
292 ret = acpi_power_on(dev->wakeup.resources.handles[i]);
294 printk(KERN_ERR PREFIX "Transition power state\n");
295 dev->wakeup.flags.valid = 0;
301 status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL);
302 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
303 printk(KERN_ERR PREFIX "Evaluate _PSW\n");
304 dev->wakeup.flags.valid = 0;
312 * Shutdown a wakeup device, counterpart of above method
313 * 1. Disable _PSW (power state wake)
314 * 2. Shutdown down the power resources
316 int acpi_disable_wakeup_device_power(struct acpi_device *dev)
318 union acpi_object arg = { ACPI_TYPE_INTEGER };
319 struct acpi_object_list arg_list = { 1, &arg };
320 acpi_status status = AE_OK;
324 ACPI_FUNCTION_TRACE("acpi_disable_wakeup_device_power");
326 if (!dev || !dev->wakeup.flags.valid)
329 arg.integer.value = 0;
331 status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL);
332 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
333 printk(KERN_ERR PREFIX "Evaluate _PSW\n");
334 dev->wakeup.flags.valid = 0;
338 /* Close power resource */
339 for (i = 0; i < dev->wakeup.resources.count; i++) {
340 ret = acpi_power_off_device(dev->wakeup.resources.handles[i]);
342 printk(KERN_ERR PREFIX "Transition power state\n");
343 dev->wakeup.flags.valid = 0;
351 /* --------------------------------------------------------------------------
352 Device Power Management
353 -------------------------------------------------------------------------- */
355 int acpi_power_get_inferred_state(struct acpi_device *device)
358 struct acpi_handle_list *list = NULL;
362 ACPI_FUNCTION_TRACE("acpi_power_get_inferred_state");
365 return_VALUE(-EINVAL);
367 device->power.state = ACPI_STATE_UNKNOWN;
370 * We know a device's inferred power state when all the resources
371 * required for a given D-state are 'on'.
373 for (i = ACPI_STATE_D0; i < ACPI_STATE_D3; i++) {
374 list = &device->power.states[i].resources;
378 result = acpi_power_get_list_state(list, &list_state);
380 return_VALUE(result);
382 if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
383 device->power.state = i;
388 device->power.state = ACPI_STATE_D3;
393 int acpi_power_transition(struct acpi_device *device, int state)
396 struct acpi_handle_list *cl = NULL; /* Current Resources */
397 struct acpi_handle_list *tl = NULL; /* Target Resources */
400 ACPI_FUNCTION_TRACE("acpi_power_transition");
402 if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3))
403 return_VALUE(-EINVAL);
405 if ((device->power.state < ACPI_STATE_D0)
406 || (device->power.state > ACPI_STATE_D3))
407 return_VALUE(-ENODEV);
409 cl = &device->power.states[device->power.state].resources;
410 tl = &device->power.states[state].resources;
412 device->power.state = ACPI_STATE_UNKNOWN;
414 if (!cl->count && !tl->count) {
419 /* TBD: Resources must be ordered. */
422 * First we reference all power resources required in the target list
423 * (e.g. so the device doesn't lose power while transitioning).
425 for (i = 0; i < tl->count; i++) {
426 result = acpi_power_on(tl->handles[i]);
432 * Then we dereference all power resources used in the current list.
434 for (i = 0; i < cl->count; i++) {
435 result = acpi_power_off_device(cl->handles[i]);
440 /* We shouldn't change the state till all above operations succeed */
441 device->power.state = state;
444 printk(KERN_WARNING PREFIX "Transitioning device [%s] to D%d\n",
445 device->pnp.bus_id, state);
447 return_VALUE(result);
450 /* --------------------------------------------------------------------------
452 -------------------------------------------------------------------------- */
454 static struct proc_dir_entry *acpi_power_dir;
456 static int acpi_power_seq_show(struct seq_file *seq, void *offset)
458 struct acpi_power_resource *resource = NULL;
460 ACPI_FUNCTION_TRACE("acpi_power_seq_show");
462 resource = (struct acpi_power_resource *)seq->private;
467 seq_puts(seq, "state: ");
468 switch (resource->state) {
469 case ACPI_POWER_RESOURCE_STATE_ON:
470 seq_puts(seq, "on\n");
472 case ACPI_POWER_RESOURCE_STATE_OFF:
473 seq_puts(seq, "off\n");
476 seq_puts(seq, "unknown\n");
480 seq_printf(seq, "system level: S%d\n"
482 "reference count: %d\n",
483 resource->system_level,
484 resource->order, resource->references);
490 static int acpi_power_open_fs(struct inode *inode, struct file *file)
492 return single_open(file, acpi_power_seq_show, PDE(inode)->data);
495 static int acpi_power_add_fs(struct acpi_device *device)
497 struct proc_dir_entry *entry = NULL;
499 ACPI_FUNCTION_TRACE("acpi_power_add_fs");
502 return_VALUE(-EINVAL);
504 if (!acpi_device_dir(device)) {
505 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
507 if (!acpi_device_dir(device))
508 return_VALUE(-ENODEV);
512 entry = create_proc_entry(ACPI_POWER_FILE_STATUS,
513 S_IRUGO, acpi_device_dir(device));
517 entry->proc_fops = &acpi_power_fops;
518 entry->data = acpi_driver_data(device);
524 static int acpi_power_remove_fs(struct acpi_device *device)
526 ACPI_FUNCTION_TRACE("acpi_power_remove_fs");
528 if (acpi_device_dir(device)) {
529 remove_proc_entry(ACPI_POWER_FILE_STATUS,
530 acpi_device_dir(device));
531 remove_proc_entry(acpi_device_bid(device), acpi_power_dir);
532 acpi_device_dir(device) = NULL;
538 /* --------------------------------------------------------------------------
540 -------------------------------------------------------------------------- */
542 static int acpi_power_add(struct acpi_device *device)
545 acpi_status status = AE_OK;
546 struct acpi_power_resource *resource = NULL;
547 union acpi_object acpi_object;
548 struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
550 ACPI_FUNCTION_TRACE("acpi_power_add");
553 return_VALUE(-EINVAL);
555 resource = kmalloc(sizeof(struct acpi_power_resource), GFP_KERNEL);
557 return_VALUE(-ENOMEM);
558 memset(resource, 0, sizeof(struct acpi_power_resource));
560 resource->handle = device->handle;
561 strcpy(resource->name, device->pnp.bus_id);
562 strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
563 strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
564 acpi_driver_data(device) = resource;
566 /* Evalute the object to get the system level and resource order. */
567 status = acpi_evaluate_object(resource->handle, NULL, NULL, &buffer);
568 if (ACPI_FAILURE(status)) {
572 resource->system_level = acpi_object.power_resource.system_level;
573 resource->order = acpi_object.power_resource.resource_order;
575 result = acpi_power_get_state(resource);
579 switch (resource->state) {
580 case ACPI_POWER_RESOURCE_STATE_ON:
581 device->power.state = ACPI_STATE_D0;
583 case ACPI_POWER_RESOURCE_STATE_OFF:
584 device->power.state = ACPI_STATE_D3;
587 device->power.state = ACPI_STATE_UNKNOWN;
591 result = acpi_power_add_fs(device);
595 printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device),
596 acpi_device_bid(device), resource->state ? "on" : "off");
602 return_VALUE(result);
605 static int acpi_power_remove(struct acpi_device *device, int type)
607 struct acpi_power_resource *resource = NULL;
609 ACPI_FUNCTION_TRACE("acpi_power_remove");
611 if (!device || !acpi_driver_data(device))
612 return_VALUE(-EINVAL);
614 resource = (struct acpi_power_resource *)acpi_driver_data(device);
616 acpi_power_remove_fs(device);
623 static int __init acpi_power_init(void)
627 ACPI_FUNCTION_TRACE("acpi_power_init");
632 INIT_LIST_HEAD(&acpi_power_resource_list);
634 acpi_power_dir = proc_mkdir(ACPI_POWER_CLASS, acpi_root_dir);
636 return_VALUE(-ENODEV);
638 result = acpi_bus_register_driver(&acpi_power_driver);
640 remove_proc_entry(ACPI_POWER_CLASS, acpi_root_dir);
641 return_VALUE(-ENODEV);
647 subsys_initcall(acpi_power_init);