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