drivers: power: report battery voltage in AOSP compatible format
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / acpi / glue.c
CommitLineData
4e10d12a
DSL
1/*
2 * Link physical devices with ACPI devices support
3 *
4 * Copyright (c) 2005 David Shaohua Li <shaohua.li@intel.com>
5 * Copyright (c) 2005 Intel Corp.
6 *
7 * This file is released under the GPLv2.
8 */
214f2c90 9#include <linux/export.h>
4e10d12a
DSL
10#include <linux/init.h>
11#include <linux/list.h>
12#include <linux/device.h>
5a0e3ad6 13#include <linux/slab.h>
4e10d12a
DSL
14#include <linux/rwsem.h>
15#include <linux/acpi.h>
16
a192a958
LB
17#include "internal.h"
18
4e10d12a
DSL
19#define ACPI_GLUE_DEBUG 0
20#if ACPI_GLUE_DEBUG
23415eb5
JP
21#define DBG(fmt, ...) \
22 printk(KERN_DEBUG PREFIX fmt, ##__VA_ARGS__)
4e10d12a 23#else
23415eb5
JP
24#define DBG(fmt, ...) \
25do { \
26 if (0) \
27 printk(KERN_DEBUG PREFIX fmt, ##__VA_ARGS__); \
28} while (0)
4e10d12a
DSL
29#endif
30static LIST_HEAD(bus_type_list);
31static DECLARE_RWSEM(bus_type_sem);
32
1033f904
LT
33#define PHYSICAL_NODE_STRING "physical_node"
34
4e10d12a
DSL
35int register_acpi_bus_type(struct acpi_bus_type *type)
36{
37 if (acpi_disabled)
38 return -ENODEV;
53540098 39 if (type && type->match && type->find_device) {
4e10d12a
DSL
40 down_write(&bus_type_sem);
41 list_add_tail(&type->list, &bus_type_list);
42 up_write(&bus_type_sem);
53540098 43 printk(KERN_INFO PREFIX "bus type %s registered\n", type->name);
4e10d12a
DSL
44 return 0;
45 }
46 return -ENODEV;
47}
91e4d5a1 48EXPORT_SYMBOL_GPL(register_acpi_bus_type);
4e10d12a 49
4e10d12a
DSL
50int unregister_acpi_bus_type(struct acpi_bus_type *type)
51{
52 if (acpi_disabled)
53 return 0;
54 if (type) {
55 down_write(&bus_type_sem);
56 list_del_init(&type->list);
57 up_write(&bus_type_sem);
53540098
RW
58 printk(KERN_INFO PREFIX "bus type %s unregistered\n",
59 type->name);
4e10d12a
DSL
60 return 0;
61 }
62 return -ENODEV;
63}
91e4d5a1 64EXPORT_SYMBOL_GPL(unregister_acpi_bus_type);
4e10d12a 65
53540098 66static struct acpi_bus_type *acpi_get_bus_type(struct device *dev)
4e10d12a
DSL
67{
68 struct acpi_bus_type *tmp, *ret = NULL;
69
70 down_read(&bus_type_sem);
71 list_for_each_entry(tmp, &bus_type_list, list) {
53540098 72 if (tmp->match(dev)) {
4e10d12a
DSL
73 ret = tmp;
74 break;
75 }
76 }
77 up_read(&bus_type_sem);
78 return ret;
79}
80
6e4fdb80
RW
81static acpi_status acpi_dev_present(acpi_handle handle, u32 lvl_not_used,
82 void *not_used, void **ret_p)
4e10d12a 83{
6e4fdb80
RW
84 struct acpi_device *adev = NULL;
85
86 acpi_bus_get_device(handle, &adev);
87 if (adev) {
88 *ret_p = handle;
89 return AE_CTRL_TERMINATE;
90 }
91 return AE_OK;
92}
93
94static bool acpi_extra_checks_passed(acpi_handle handle, bool is_bridge)
95{
96 unsigned long long sta;
97 acpi_status status;
98
99 status = acpi_bus_get_status_handle(handle, &sta);
100 if (ACPI_FAILURE(status) || !(sta & ACPI_STA_DEVICE_ENABLED))
101 return false;
102
103 if (is_bridge) {
104 void *test = NULL;
105
106 /* Check if this object has at least one child device. */
107 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
108 acpi_dev_present, NULL, NULL, &test);
109 return !!test;
110 }
111 return true;
112}
113
114struct find_child_context {
115 u64 addr;
116 bool is_bridge;
117 acpi_handle ret;
118 bool ret_checked;
119};
120
121static acpi_status do_find_child(acpi_handle handle, u32 lvl_not_used,
122 void *data, void **not_used)
123{
124 struct find_child_context *context = data;
125 unsigned long long addr;
4e10d12a 126 acpi_status status;
33f767d7
RW
127
128 status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &addr);
6e4fdb80
RW
129 if (ACPI_FAILURE(status) || addr != context->addr)
130 return AE_OK;
131
132 if (!context->ret) {
133 /* This is the first matching object. Save its handle. */
134 context->ret = handle;
135 return AE_OK;
136 }
137 /*
138 * There is more than one matching object with the same _ADR value.
139 * That really is unexpected, so we are kind of beyond the scope of the
140 * spec here. We have to choose which one to return, though.
141 *
142 * First, check if the previously found object is good enough and return
143 * its handle if so. Second, check the same for the object that we've
144 * just found.
145 */
146 if (!context->ret_checked) {
147 if (acpi_extra_checks_passed(context->ret, context->is_bridge))
2ac3b5e4 148 return AE_CTRL_TERMINATE;
6e4fdb80
RW
149 else
150 context->ret_checked = true;
151 }
152 if (acpi_extra_checks_passed(handle, context->is_bridge)) {
153 context->ret = handle;
154 return AE_CTRL_TERMINATE;
4e10d12a
DSL
155 }
156 return AE_OK;
157}
158
6e4fdb80 159acpi_handle acpi_find_child(acpi_handle parent, u64 addr, bool is_bridge)
4e10d12a 160{
6e4fdb80
RW
161 if (parent) {
162 struct find_child_context context = {
163 .addr = addr,
164 .is_bridge = is_bridge,
165 };
4e10d12a 166
6e4fdb80
RW
167 acpi_walk_namespace(ACPI_TYPE_DEVICE, parent, 1, do_find_child,
168 NULL, &context, NULL);
169 return context.ret;
170 }
171 return NULL;
33f767d7 172}
6e4fdb80 173EXPORT_SYMBOL_GPL(acpi_find_child);
4e10d12a 174
4e10d12a
DSL
175static int acpi_bind_one(struct device *dev, acpi_handle handle)
176{
1071695f 177 struct acpi_device *acpi_dev;
4e10d12a 178 acpi_status status;
f3fd0c8a 179 struct acpi_device_physical_node *physical_node, *pn;
1033f904
LT
180 char physical_node_name[sizeof(PHYSICAL_NODE_STRING) + 2];
181 int retval = -EINVAL;
4e10d12a 182
95f8a082 183 if (ACPI_HANDLE(dev)) {
f3fd0c8a
RW
184 if (handle) {
185 dev_warn(dev, "ACPI handle is already set\n");
186 return -EINVAL;
187 } else {
95f8a082 188 handle = ACPI_HANDLE(dev);
f3fd0c8a 189 }
4e10d12a 190 }
f3fd0c8a
RW
191 if (!handle)
192 return -EINVAL;
1033f904 193
4e10d12a 194 get_device(dev);
1033f904
LT
195 status = acpi_bus_get_device(handle, &acpi_dev);
196 if (ACPI_FAILURE(status))
197 goto err;
198
f3fd0c8a 199 physical_node = kzalloc(sizeof(*physical_node), GFP_KERNEL);
1033f904
LT
200 if (!physical_node) {
201 retval = -ENOMEM;
202 goto err;
4e10d12a 203 }
4e10d12a 204
1033f904 205 mutex_lock(&acpi_dev->physical_node_lock);
f3fd0c8a
RW
206
207 /* Sanity check. */
208 list_for_each_entry(pn, &acpi_dev->physical_node_list, node)
209 if (pn->dev == dev) {
210 dev_warn(dev, "Already associated with ACPI node\n");
211 goto err_free;
212 }
213
1033f904
LT
214 /* allocate physical node id according to physical_node_id_bitmap */
215 physical_node->node_id =
216 find_first_zero_bit(acpi_dev->physical_node_id_bitmap,
217 ACPI_MAX_PHYSICAL_NODE);
218 if (physical_node->node_id >= ACPI_MAX_PHYSICAL_NODE) {
219 retval = -ENOSPC;
f3fd0c8a 220 goto err_free;
1071695f
DB
221 }
222
1033f904
LT
223 set_bit(physical_node->node_id, acpi_dev->physical_node_id_bitmap);
224 physical_node->dev = dev;
225 list_add_tail(&physical_node->node, &acpi_dev->physical_node_list);
226 acpi_dev->physical_node_count++;
f3fd0c8a 227
1033f904
LT
228 mutex_unlock(&acpi_dev->physical_node_lock);
229
95f8a082
RW
230 if (!ACPI_HANDLE(dev))
231 ACPI_HANDLE_SET(dev, acpi_dev->handle);
1033f904
LT
232
233 if (!physical_node->node_id)
234 strcpy(physical_node_name, PHYSICAL_NODE_STRING);
235 else
236 sprintf(physical_node_name,
237 "physical_node%d", physical_node->node_id);
238 retval = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
239 physical_node_name);
240 retval = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
241 "firmware_node");
242
243 if (acpi_dev->wakeup.flags.valid)
244 device_set_wakeup_capable(dev, true);
245
4e10d12a 246 return 0;
1033f904
LT
247
248 err:
95f8a082 249 ACPI_HANDLE_SET(dev, NULL);
1033f904
LT
250 put_device(dev);
251 return retval;
f3fd0c8a
RW
252
253 err_free:
254 mutex_unlock(&acpi_dev->physical_node_lock);
255 kfree(physical_node);
256 goto err;
4e10d12a
DSL
257}
258
259static int acpi_unbind_one(struct device *dev)
260{
1033f904
LT
261 struct acpi_device_physical_node *entry;
262 struct acpi_device *acpi_dev;
263 acpi_status status;
264 struct list_head *node, *next;
265
95f8a082 266 if (!ACPI_HANDLE(dev))
4e10d12a 267 return 0;
1071695f 268
95f8a082 269 status = acpi_bus_get_device(ACPI_HANDLE(dev), &acpi_dev);
1033f904
LT
270 if (ACPI_FAILURE(status))
271 goto err;
1071695f 272
1033f904
LT
273 mutex_lock(&acpi_dev->physical_node_lock);
274 list_for_each_safe(node, next, &acpi_dev->physical_node_list) {
275 char physical_node_name[sizeof(PHYSICAL_NODE_STRING) + 2];
276
277 entry = list_entry(node, struct acpi_device_physical_node,
278 node);
279 if (entry->dev != dev)
280 continue;
281
282 list_del(node);
283 clear_bit(entry->node_id, acpi_dev->physical_node_id_bitmap);
1071695f 284
1033f904
LT
285 acpi_dev->physical_node_count--;
286
287 if (!entry->node_id)
288 strcpy(physical_node_name, PHYSICAL_NODE_STRING);
289 else
290 sprintf(physical_node_name,
291 "physical_node%d", entry->node_id);
292
293 sysfs_remove_link(&acpi_dev->dev.kobj, physical_node_name);
294 sysfs_remove_link(&dev->kobj, "firmware_node");
95f8a082 295 ACPI_HANDLE_SET(dev, NULL);
4e10d12a
DSL
296 /* acpi_bind_one increase refcnt by one */
297 put_device(dev);
1033f904 298 kfree(entry);
4e10d12a 299 }
1033f904
LT
300 mutex_unlock(&acpi_dev->physical_node_lock);
301
4e10d12a 302 return 0;
1033f904
LT
303
304err:
305 dev_err(dev, "Oops, 'acpi_handle' corrupt\n");
306 return -EINVAL;
4e10d12a
DSL
307}
308
309static int acpi_platform_notify(struct device *dev)
310{
53540098 311 struct acpi_bus_type *type = acpi_get_bus_type(dev);
4e10d12a 312 acpi_handle handle;
11909ca1 313 int ret;
4e10d12a 314
f3fd0c8a 315 ret = acpi_bind_one(dev, NULL);
92414481 316 if (ret && type) {
11909ca1
RW
317 ret = type->find_device(dev, &handle);
318 if (ret) {
319 DBG("Unable to get handle for %s\n", dev_name(dev));
320 goto out;
321 }
322 ret = acpi_bind_one(dev, handle);
323 if (ret)
324 goto out;
4e10d12a 325 }
11909ca1
RW
326
327 if (type && type->setup)
328 type->setup(dev);
4e10d12a 329
f3fd0c8a 330 out:
4e10d12a
DSL
331#if ACPI_GLUE_DEBUG
332 if (!ret) {
333 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
334
a412a11d 335 acpi_get_name(ACPI_HANDLE(dev), ACPI_FULL_PATHNAME, &buffer);
db1461ad 336 DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer);
02438d87 337 kfree(buffer.pointer);
4e10d12a 338 } else
db1461ad 339 DBG("Device %s -> No ACPI support\n", dev_name(dev));
4e10d12a
DSL
340#endif
341
342 return ret;
343}
344
345static int acpi_platform_notify_remove(struct device *dev)
346{
11909ca1
RW
347 struct acpi_bus_type *type;
348
53540098 349 type = acpi_get_bus_type(dev);
11909ca1
RW
350 if (type && type->cleanup)
351 type->cleanup(dev);
352
4e10d12a
DSL
353 acpi_unbind_one(dev);
354 return 0;
355}
356
0e46517d 357int __init init_acpi_device_notify(void)
4e10d12a 358{
4e10d12a
DSL
359 if (platform_notify || platform_notify_remove) {
360 printk(KERN_ERR PREFIX "Can't use platform_notify\n");
361 return 0;
362 }
363 platform_notify = acpi_platform_notify;
364 platform_notify_remove = acpi_platform_notify_remove;
365 return 0;
366}