Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
aa650bbd | 2 | * battery.c - ACPI Battery Driver (Revision: 2.0) |
1da177e4 | 3 | * |
aa650bbd AS |
4 | * Copyright (C) 2007 Alexey Starikovskiy <astarikovskiy@suse.de> |
5 | * Copyright (C) 2004-2007 Vladimir Lebedev <vladimir.p.lebedev@intel.com> | |
1da177e4 LT |
6 | * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com> |
7 | * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com> | |
8 | * | |
9 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
10 | * | |
11 | * This program is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License as published by | |
13 | * the Free Software Foundation; either version 2 of the License, or (at | |
14 | * your option) any later version. | |
15 | * | |
16 | * This program is distributed in the hope that it will be useful, but | |
17 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
19 | * General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU General Public License along | |
22 | * with this program; if not, write to the Free Software Foundation, Inc., | |
23 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
24 | * | |
25 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
26 | */ | |
27 | ||
28 | #include <linux/kernel.h> | |
29 | #include <linux/module.h> | |
30 | #include <linux/init.h> | |
31 | #include <linux/types.h> | |
f1d4661a | 32 | #include <linux/jiffies.h> |
0f66af53 | 33 | #include <linux/async.h> |
bc76f90b | 34 | #include <linux/dmi.h> |
5a0e3ad6 | 35 | #include <linux/slab.h> |
25be5821 | 36 | #include <linux/suspend.h> |
4000e626 | 37 | #include <asm/unaligned.h> |
d7380965 | 38 | |
1da177e4 LT |
39 | #include <acpi/acpi_bus.h> |
40 | #include <acpi/acpi_drivers.h> | |
d7380965 AS |
41 | #include <linux/power_supply.h> |
42 | ||
a192a958 LB |
43 | #define PREFIX "ACPI: " |
44 | ||
1da177e4 LT |
45 | #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF |
46 | ||
1da177e4 | 47 | #define ACPI_BATTERY_CLASS "battery" |
1da177e4 | 48 | #define ACPI_BATTERY_DEVICE_NAME "Battery" |
1da177e4 LT |
49 | #define ACPI_BATTERY_NOTIFY_STATUS 0x80 |
50 | #define ACPI_BATTERY_NOTIFY_INFO 0x81 | |
c67fcd67 | 51 | #define ACPI_BATTERY_NOTIFY_THRESHOLD 0x82 |
1da177e4 | 52 | |
ae6f6187 LT |
53 | /* Battery power unit: 0 means mW, 1 means mA */ |
54 | #define ACPI_BATTERY_POWER_UNIT_MA 1 | |
55 | ||
1da177e4 | 56 | #define _COMPONENT ACPI_BATTERY_COMPONENT |
b6ce4083 | 57 | |
f52fd66d | 58 | ACPI_MODULE_NAME("battery"); |
1da177e4 | 59 | |
f52fd66d | 60 | MODULE_AUTHOR("Paul Diefenbaugh"); |
aa650bbd | 61 | MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>"); |
7cda93e0 | 62 | MODULE_DESCRIPTION("ACPI Battery Driver"); |
1da177e4 LT |
63 | MODULE_LICENSE("GPL"); |
64 | ||
f1d4661a AS |
65 | static unsigned int cache_time = 1000; |
66 | module_param(cache_time, uint, 0644); | |
67 | MODULE_PARM_DESC(cache_time, "cache time in milliseconds"); | |
b6ce4083 | 68 | |
1ba90e3a TR |
69 | static const struct acpi_device_id battery_device_ids[] = { |
70 | {"PNP0C0A", 0}, | |
71 | {"", 0}, | |
72 | }; | |
1ba90e3a | 73 | |
aa650bbd | 74 | MODULE_DEVICE_TABLE(acpi, battery_device_ids); |
1da177e4 | 75 | |
7b3bcc4a AS |
76 | enum { |
77 | ACPI_BATTERY_ALARM_PRESENT, | |
c67fcd67 | 78 | ACPI_BATTERY_XINFO_PRESENT, |
557d5868 | 79 | ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, |
4000e626 KI |
80 | /* On Lenovo Thinkpad models from 2010 and 2011, the power unit |
81 | switches between mWh and mAh depending on whether the system | |
82 | is running on battery or not. When mAh is the unit, most | |
83 | reported values are incorrect and need to be adjusted by | |
84 | 10000/design_voltage. Verified on x201, t410, t410s, and x220. | |
85 | Pre-2010 and 2012 models appear to always report in mWh and | |
86 | are thus unaffected (tested with t42, t61, t500, x200, x300, | |
87 | and x230). Also, in mid-2012 Lenovo issued a BIOS update for | |
88 | the 2011 models that fixes the issue (tested on x220 with a | |
89 | post-1.29 BIOS), but as of Nov. 2012, no such update is | |
90 | available for the 2010 models. */ | |
91 | ACPI_BATTERY_QUIRK_THINKPAD_MAH, | |
7b3bcc4a | 92 | }; |
78490d82 | 93 | |
1da177e4 | 94 | struct acpi_battery { |
038fdea2 | 95 | struct mutex lock; |
69d94ec6 | 96 | struct mutex sysfs_lock; |
d7380965 | 97 | struct power_supply bat; |
f1d4661a | 98 | struct acpi_device *device; |
25be5821 | 99 | struct notifier_block pm_nb; |
f1d4661a | 100 | unsigned long update_time; |
016d5baa | 101 | int revision; |
7faa144a | 102 | int rate_now; |
d7380965 AS |
103 | int capacity_now; |
104 | int voltage_now; | |
038fdea2 | 105 | int design_capacity; |
d7380965 | 106 | int full_charge_capacity; |
038fdea2 AS |
107 | int technology; |
108 | int design_voltage; | |
109 | int design_capacity_warning; | |
110 | int design_capacity_low; | |
c67fcd67 AS |
111 | int cycle_count; |
112 | int measurement_accuracy; | |
113 | int max_sampling_time; | |
114 | int min_sampling_time; | |
115 | int max_averaging_interval; | |
116 | int min_averaging_interval; | |
038fdea2 AS |
117 | int capacity_granularity_1; |
118 | int capacity_granularity_2; | |
f1d4661a | 119 | int alarm; |
038fdea2 AS |
120 | char model_number[32]; |
121 | char serial_number[32]; | |
122 | char type[32]; | |
123 | char oem_info[32]; | |
f1d4661a AS |
124 | int state; |
125 | int power_unit; | |
7b3bcc4a | 126 | unsigned long flags; |
1da177e4 LT |
127 | }; |
128 | ||
497888cf | 129 | #define to_acpi_battery(x) container_of(x, struct acpi_battery, bat) |
d7380965 | 130 | |
efd941f1 | 131 | static inline int acpi_battery_present(struct acpi_battery *battery) |
b6ce4083 | 132 | { |
78490d82 AS |
133 | return battery->device->status.battery_present; |
134 | } | |
038fdea2 | 135 | |
d7380965 AS |
136 | static int acpi_battery_technology(struct acpi_battery *battery) |
137 | { | |
138 | if (!strcasecmp("NiCd", battery->type)) | |
139 | return POWER_SUPPLY_TECHNOLOGY_NiCd; | |
140 | if (!strcasecmp("NiMH", battery->type)) | |
141 | return POWER_SUPPLY_TECHNOLOGY_NiMH; | |
142 | if (!strcasecmp("LION", battery->type)) | |
143 | return POWER_SUPPLY_TECHNOLOGY_LION; | |
ad40e68b | 144 | if (!strncasecmp("LI-ION", battery->type, 6)) |
0bde7eee | 145 | return POWER_SUPPLY_TECHNOLOGY_LION; |
d7380965 AS |
146 | if (!strcasecmp("LiP", battery->type)) |
147 | return POWER_SUPPLY_TECHNOLOGY_LIPO; | |
148 | return POWER_SUPPLY_TECHNOLOGY_UNKNOWN; | |
149 | } | |
150 | ||
9104476e | 151 | static int acpi_battery_get_state(struct acpi_battery *battery); |
b19073a0 | 152 | |
56f382a0 RH |
153 | static int acpi_battery_is_charged(struct acpi_battery *battery) |
154 | { | |
155 | /* either charging or discharging */ | |
156 | if (battery->state != 0) | |
157 | return 0; | |
158 | ||
159 | /* battery not reporting charge */ | |
160 | if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN || | |
161 | battery->capacity_now == 0) | |
162 | return 0; | |
163 | ||
164 | /* good batteries update full_charge as the batteries degrade */ | |
165 | if (battery->full_charge_capacity == battery->capacity_now) | |
166 | return 1; | |
167 | ||
168 | /* fallback to using design values for broken batteries */ | |
169 | if (battery->design_capacity == battery->capacity_now) | |
170 | return 1; | |
171 | ||
172 | /* we don't do any sort of metric based on percentages */ | |
173 | return 0; | |
174 | } | |
175 | ||
d7380965 AS |
176 | static int acpi_battery_get_property(struct power_supply *psy, |
177 | enum power_supply_property psp, | |
178 | union power_supply_propval *val) | |
179 | { | |
a1b4bd69 | 180 | int ret = 0; |
d7380965 AS |
181 | struct acpi_battery *battery = to_acpi_battery(psy); |
182 | ||
9104476e AS |
183 | if (acpi_battery_present(battery)) { |
184 | /* run battery update only if it is present */ | |
185 | acpi_battery_get_state(battery); | |
186 | } else if (psp != POWER_SUPPLY_PROP_PRESENT) | |
d7380965 AS |
187 | return -ENODEV; |
188 | switch (psp) { | |
189 | case POWER_SUPPLY_PROP_STATUS: | |
190 | if (battery->state & 0x01) | |
191 | val->intval = POWER_SUPPLY_STATUS_DISCHARGING; | |
192 | else if (battery->state & 0x02) | |
193 | val->intval = POWER_SUPPLY_STATUS_CHARGING; | |
56f382a0 | 194 | else if (acpi_battery_is_charged(battery)) |
d7380965 | 195 | val->intval = POWER_SUPPLY_STATUS_FULL; |
4c41d3ad RD |
196 | else |
197 | val->intval = POWER_SUPPLY_STATUS_UNKNOWN; | |
d7380965 AS |
198 | break; |
199 | case POWER_SUPPLY_PROP_PRESENT: | |
200 | val->intval = acpi_battery_present(battery); | |
201 | break; | |
202 | case POWER_SUPPLY_PROP_TECHNOLOGY: | |
203 | val->intval = acpi_battery_technology(battery); | |
204 | break; | |
c67fcd67 AS |
205 | case POWER_SUPPLY_PROP_CYCLE_COUNT: |
206 | val->intval = battery->cycle_count; | |
207 | break; | |
d7380965 | 208 | case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: |
a1b4bd69 RW |
209 | if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN) |
210 | ret = -ENODEV; | |
211 | else | |
212 | val->intval = battery->design_voltage * 1000; | |
d7380965 AS |
213 | break; |
214 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: | |
a1b4bd69 RW |
215 | if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN) |
216 | ret = -ENODEV; | |
217 | else | |
218 | val->intval = battery->voltage_now * 1000; | |
d7380965 AS |
219 | break; |
220 | case POWER_SUPPLY_PROP_CURRENT_NOW: | |
7faa144a | 221 | case POWER_SUPPLY_PROP_POWER_NOW: |
a1b4bd69 RW |
222 | if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN) |
223 | ret = -ENODEV; | |
224 | else | |
225 | val->intval = battery->rate_now * 1000; | |
d7380965 AS |
226 | break; |
227 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: | |
228 | case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: | |
a1b4bd69 RW |
229 | if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN) |
230 | ret = -ENODEV; | |
231 | else | |
232 | val->intval = battery->design_capacity * 1000; | |
d7380965 AS |
233 | break; |
234 | case POWER_SUPPLY_PROP_CHARGE_FULL: | |
235 | case POWER_SUPPLY_PROP_ENERGY_FULL: | |
a1b4bd69 RW |
236 | if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN) |
237 | ret = -ENODEV; | |
238 | else | |
239 | val->intval = battery->full_charge_capacity * 1000; | |
d7380965 AS |
240 | break; |
241 | case POWER_SUPPLY_PROP_CHARGE_NOW: | |
242 | case POWER_SUPPLY_PROP_ENERGY_NOW: | |
a1b4bd69 RW |
243 | if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN) |
244 | ret = -ENODEV; | |
245 | else | |
246 | val->intval = battery->capacity_now * 1000; | |
d7380965 | 247 | break; |
a58e1150 | 248 | case POWER_SUPPLY_PROP_CAPACITY: |
249 | if (battery->capacity_now && battery->full_charge_capacity) | |
250 | val->intval = battery->capacity_now * 100/ | |
251 | battery->full_charge_capacity; | |
252 | else | |
253 | val->intval = 0; | |
254 | break; | |
d7380965 AS |
255 | case POWER_SUPPLY_PROP_MODEL_NAME: |
256 | val->strval = battery->model_number; | |
257 | break; | |
258 | case POWER_SUPPLY_PROP_MANUFACTURER: | |
259 | val->strval = battery->oem_info; | |
260 | break; | |
7c2670bb | 261 | case POWER_SUPPLY_PROP_SERIAL_NUMBER: |
262 | val->strval = battery->serial_number; | |
263 | break; | |
d7380965 | 264 | default: |
a1b4bd69 | 265 | ret = -EINVAL; |
d7380965 | 266 | } |
a1b4bd69 | 267 | return ret; |
d7380965 AS |
268 | } |
269 | ||
270 | static enum power_supply_property charge_battery_props[] = { | |
271 | POWER_SUPPLY_PROP_STATUS, | |
272 | POWER_SUPPLY_PROP_PRESENT, | |
273 | POWER_SUPPLY_PROP_TECHNOLOGY, | |
c67fcd67 | 274 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
d7380965 AS |
275 | POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, |
276 | POWER_SUPPLY_PROP_VOLTAGE_NOW, | |
277 | POWER_SUPPLY_PROP_CURRENT_NOW, | |
278 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, | |
279 | POWER_SUPPLY_PROP_CHARGE_FULL, | |
280 | POWER_SUPPLY_PROP_CHARGE_NOW, | |
a58e1150 | 281 | POWER_SUPPLY_PROP_CAPACITY, |
d7380965 AS |
282 | POWER_SUPPLY_PROP_MODEL_NAME, |
283 | POWER_SUPPLY_PROP_MANUFACTURER, | |
7c2670bb | 284 | POWER_SUPPLY_PROP_SERIAL_NUMBER, |
d7380965 AS |
285 | }; |
286 | ||
287 | static enum power_supply_property energy_battery_props[] = { | |
288 | POWER_SUPPLY_PROP_STATUS, | |
289 | POWER_SUPPLY_PROP_PRESENT, | |
290 | POWER_SUPPLY_PROP_TECHNOLOGY, | |
c67fcd67 | 291 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
d7380965 AS |
292 | POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, |
293 | POWER_SUPPLY_PROP_VOLTAGE_NOW, | |
7faa144a | 294 | POWER_SUPPLY_PROP_POWER_NOW, |
d7380965 AS |
295 | POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, |
296 | POWER_SUPPLY_PROP_ENERGY_FULL, | |
297 | POWER_SUPPLY_PROP_ENERGY_NOW, | |
a58e1150 | 298 | POWER_SUPPLY_PROP_CAPACITY, |
d7380965 AS |
299 | POWER_SUPPLY_PROP_MODEL_NAME, |
300 | POWER_SUPPLY_PROP_MANUFACTURER, | |
7c2670bb | 301 | POWER_SUPPLY_PROP_SERIAL_NUMBER, |
d7380965 AS |
302 | }; |
303 | ||
78490d82 AS |
304 | /* -------------------------------------------------------------------------- |
305 | Battery Management | |
306 | -------------------------------------------------------------------------- */ | |
038fdea2 AS |
307 | struct acpi_offsets { |
308 | size_t offset; /* offset inside struct acpi_sbs_battery */ | |
309 | u8 mode; /* int or string? */ | |
310 | }; | |
b6ce4083 | 311 | |
038fdea2 AS |
312 | static struct acpi_offsets state_offsets[] = { |
313 | {offsetof(struct acpi_battery, state), 0}, | |
7faa144a | 314 | {offsetof(struct acpi_battery, rate_now), 0}, |
d7380965 AS |
315 | {offsetof(struct acpi_battery, capacity_now), 0}, |
316 | {offsetof(struct acpi_battery, voltage_now), 0}, | |
038fdea2 | 317 | }; |
b6ce4083 | 318 | |
038fdea2 AS |
319 | static struct acpi_offsets info_offsets[] = { |
320 | {offsetof(struct acpi_battery, power_unit), 0}, | |
321 | {offsetof(struct acpi_battery, design_capacity), 0}, | |
d7380965 | 322 | {offsetof(struct acpi_battery, full_charge_capacity), 0}, |
038fdea2 AS |
323 | {offsetof(struct acpi_battery, technology), 0}, |
324 | {offsetof(struct acpi_battery, design_voltage), 0}, | |
325 | {offsetof(struct acpi_battery, design_capacity_warning), 0}, | |
326 | {offsetof(struct acpi_battery, design_capacity_low), 0}, | |
327 | {offsetof(struct acpi_battery, capacity_granularity_1), 0}, | |
328 | {offsetof(struct acpi_battery, capacity_granularity_2), 0}, | |
329 | {offsetof(struct acpi_battery, model_number), 1}, | |
330 | {offsetof(struct acpi_battery, serial_number), 1}, | |
331 | {offsetof(struct acpi_battery, type), 1}, | |
332 | {offsetof(struct acpi_battery, oem_info), 1}, | |
333 | }; | |
b6ce4083 | 334 | |
c67fcd67 | 335 | static struct acpi_offsets extended_info_offsets[] = { |
016d5baa | 336 | {offsetof(struct acpi_battery, revision), 0}, |
c67fcd67 AS |
337 | {offsetof(struct acpi_battery, power_unit), 0}, |
338 | {offsetof(struct acpi_battery, design_capacity), 0}, | |
339 | {offsetof(struct acpi_battery, full_charge_capacity), 0}, | |
340 | {offsetof(struct acpi_battery, technology), 0}, | |
341 | {offsetof(struct acpi_battery, design_voltage), 0}, | |
342 | {offsetof(struct acpi_battery, design_capacity_warning), 0}, | |
343 | {offsetof(struct acpi_battery, design_capacity_low), 0}, | |
344 | {offsetof(struct acpi_battery, cycle_count), 0}, | |
345 | {offsetof(struct acpi_battery, measurement_accuracy), 0}, | |
346 | {offsetof(struct acpi_battery, max_sampling_time), 0}, | |
347 | {offsetof(struct acpi_battery, min_sampling_time), 0}, | |
348 | {offsetof(struct acpi_battery, max_averaging_interval), 0}, | |
349 | {offsetof(struct acpi_battery, min_averaging_interval), 0}, | |
350 | {offsetof(struct acpi_battery, capacity_granularity_1), 0}, | |
351 | {offsetof(struct acpi_battery, capacity_granularity_2), 0}, | |
352 | {offsetof(struct acpi_battery, model_number), 1}, | |
353 | {offsetof(struct acpi_battery, serial_number), 1}, | |
354 | {offsetof(struct acpi_battery, type), 1}, | |
355 | {offsetof(struct acpi_battery, oem_info), 1}, | |
356 | }; | |
357 | ||
038fdea2 AS |
358 | static int extract_package(struct acpi_battery *battery, |
359 | union acpi_object *package, | |
360 | struct acpi_offsets *offsets, int num) | |
361 | { | |
106449e8 | 362 | int i; |
038fdea2 AS |
363 | union acpi_object *element; |
364 | if (package->type != ACPI_TYPE_PACKAGE) | |
365 | return -EFAULT; | |
366 | for (i = 0; i < num; ++i) { | |
367 | if (package->package.count <= i) | |
368 | return -EFAULT; | |
369 | element = &package->package.elements[i]; | |
370 | if (offsets[i].mode) { | |
106449e8 AS |
371 | u8 *ptr = (u8 *)battery + offsets[i].offset; |
372 | if (element->type == ACPI_TYPE_STRING || | |
373 | element->type == ACPI_TYPE_BUFFER) | |
374 | strncpy(ptr, element->string.pointer, 32); | |
375 | else if (element->type == ACPI_TYPE_INTEGER) { | |
376 | strncpy(ptr, (u8 *)&element->integer.value, | |
439913ff LM |
377 | sizeof(u64)); |
378 | ptr[sizeof(u64)] = 0; | |
b8a1bdb1 AS |
379 | } else |
380 | *ptr = 0; /* don't have value */ | |
038fdea2 | 381 | } else { |
b8a1bdb1 AS |
382 | int *x = (int *)((u8 *)battery + offsets[i].offset); |
383 | *x = (element->type == ACPI_TYPE_INTEGER) ? | |
384 | element->integer.value : -1; | |
038fdea2 | 385 | } |
b6ce4083 | 386 | } |
b6ce4083 VL |
387 | return 0; |
388 | } | |
389 | ||
390 | static int acpi_battery_get_status(struct acpi_battery *battery) | |
391 | { | |
aa650bbd | 392 | if (acpi_bus_get_status(battery->device)) { |
b6ce4083 VL |
393 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA")); |
394 | return -ENODEV; | |
395 | } | |
aa650bbd | 396 | return 0; |
b6ce4083 VL |
397 | } |
398 | ||
399 | static int acpi_battery_get_info(struct acpi_battery *battery) | |
1da177e4 | 400 | { |
aa650bbd | 401 | int result = -EFAULT; |
4be44fcd | 402 | acpi_status status = 0; |
0f4c6547 | 403 | char *name = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags) ? |
c67fcd67 AS |
404 | "_BIX" : "_BIF"; |
405 | ||
4be44fcd | 406 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
1da177e4 | 407 | |
b6ce4083 VL |
408 | if (!acpi_battery_present(battery)) |
409 | return 0; | |
038fdea2 | 410 | mutex_lock(&battery->lock); |
c67fcd67 AS |
411 | status = acpi_evaluate_object(battery->device->handle, name, |
412 | NULL, &buffer); | |
038fdea2 | 413 | mutex_unlock(&battery->lock); |
aa650bbd | 414 | |
1da177e4 | 415 | if (ACPI_FAILURE(status)) { |
c67fcd67 | 416 | ACPI_EXCEPTION((AE_INFO, status, "Evaluating %s", name)); |
d550d98d | 417 | return -ENODEV; |
1da177e4 | 418 | } |
c67fcd67 AS |
419 | if (test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags)) |
420 | result = extract_package(battery, buffer.pointer, | |
421 | extended_info_offsets, | |
422 | ARRAY_SIZE(extended_info_offsets)); | |
423 | else | |
424 | result = extract_package(battery, buffer.pointer, | |
425 | info_offsets, ARRAY_SIZE(info_offsets)); | |
78490d82 | 426 | kfree(buffer.pointer); |
557d5868 ZR |
427 | if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)) |
428 | battery->full_charge_capacity = battery->design_capacity; | |
4000e626 KI |
429 | if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) && |
430 | battery->power_unit && battery->design_voltage) { | |
431 | battery->design_capacity = battery->design_capacity * | |
432 | 10000 / battery->design_voltage; | |
433 | battery->full_charge_capacity = battery->full_charge_capacity * | |
434 | 10000 / battery->design_voltage; | |
435 | battery->design_capacity_warning = | |
436 | battery->design_capacity_warning * | |
437 | 10000 / battery->design_voltage; | |
438 | /* Curiously, design_capacity_low, unlike the rest of them, | |
439 | is correct. */ | |
440 | /* capacity_granularity_* equal 1 on the systems tested, so | |
441 | it's impossible to tell if they would need an adjustment | |
442 | or not if their values were higher. */ | |
443 | } | |
d550d98d | 444 | return result; |
1da177e4 LT |
445 | } |
446 | ||
b6ce4083 | 447 | static int acpi_battery_get_state(struct acpi_battery *battery) |
1da177e4 | 448 | { |
4be44fcd LB |
449 | int result = 0; |
450 | acpi_status status = 0; | |
451 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; | |
1da177e4 | 452 | |
b6ce4083 VL |
453 | if (!acpi_battery_present(battery)) |
454 | return 0; | |
1da177e4 | 455 | |
f1d4661a AS |
456 | if (battery->update_time && |
457 | time_before(jiffies, battery->update_time + | |
458 | msecs_to_jiffies(cache_time))) | |
459 | return 0; | |
460 | ||
038fdea2 | 461 | mutex_lock(&battery->lock); |
f1d4661a | 462 | status = acpi_evaluate_object(battery->device->handle, "_BST", |
038fdea2 AS |
463 | NULL, &buffer); |
464 | mutex_unlock(&battery->lock); | |
5b31d895 | 465 | |
1da177e4 | 466 | if (ACPI_FAILURE(status)) { |
a6fc6720 | 467 | ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST")); |
d550d98d | 468 | return -ENODEV; |
1da177e4 | 469 | } |
aa650bbd | 470 | |
038fdea2 AS |
471 | result = extract_package(battery, buffer.pointer, |
472 | state_offsets, ARRAY_SIZE(state_offsets)); | |
f1d4661a | 473 | battery->update_time = jiffies; |
78490d82 | 474 | kfree(buffer.pointer); |
bc76f90b | 475 | |
55003b21 LT |
476 | /* For buggy DSDTs that report negative 16-bit values for either |
477 | * charging or discharging current and/or report 0 as 65536 | |
478 | * due to bad math. | |
479 | */ | |
480 | if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA && | |
481 | battery->rate_now != ACPI_BATTERY_VALUE_UNKNOWN && | |
482 | (s16)(battery->rate_now) < 0) { | |
bc76f90b | 483 | battery->rate_now = abs((s16)battery->rate_now); |
55003b21 LT |
484 | printk_once(KERN_WARNING FW_BUG "battery: (dis)charge rate" |
485 | " invalid.\n"); | |
486 | } | |
bc76f90b | 487 | |
557d5868 ZR |
488 | if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags) |
489 | && battery->capacity_now >= 0 && battery->capacity_now <= 100) | |
490 | battery->capacity_now = (battery->capacity_now * | |
491 | battery->full_charge_capacity) / 100; | |
4000e626 KI |
492 | if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) && |
493 | battery->power_unit && battery->design_voltage) { | |
494 | battery->capacity_now = battery->capacity_now * | |
495 | 10000 / battery->design_voltage; | |
496 | } | |
b6ce4083 VL |
497 | return result; |
498 | } | |
1da177e4 | 499 | |
aa650bbd | 500 | static int acpi_battery_set_alarm(struct acpi_battery *battery) |
1da177e4 | 501 | { |
4be44fcd | 502 | acpi_status status = 0; |
1da177e4 | 503 | |
c67fcd67 | 504 | if (!acpi_battery_present(battery) || |
7b3bcc4a | 505 | !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags)) |
d550d98d | 506 | return -ENODEV; |
1da177e4 | 507 | |
038fdea2 | 508 | mutex_lock(&battery->lock); |
0db98202 JL |
509 | status = acpi_execute_simple_method(battery->device->handle, "_BTP", |
510 | battery->alarm); | |
038fdea2 | 511 | mutex_unlock(&battery->lock); |
aa650bbd | 512 | |
1da177e4 | 513 | if (ACPI_FAILURE(status)) |
d550d98d | 514 | return -ENODEV; |
1da177e4 | 515 | |
aa650bbd | 516 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm)); |
d550d98d | 517 | return 0; |
1da177e4 LT |
518 | } |
519 | ||
b6ce4083 | 520 | static int acpi_battery_init_alarm(struct acpi_battery *battery) |
1da177e4 | 521 | { |
b6ce4083 | 522 | /* See if alarms are supported, and if so, set default */ |
952c63e9 | 523 | if (!acpi_has_method(battery->device->handle, "_BTP")) { |
7b3bcc4a | 524 | clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags); |
f1d4661a | 525 | return 0; |
b6ce4083 | 526 | } |
7b3bcc4a | 527 | set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags); |
f1d4661a AS |
528 | if (!battery->alarm) |
529 | battery->alarm = battery->design_capacity_warning; | |
aa650bbd | 530 | return acpi_battery_set_alarm(battery); |
b6ce4083 | 531 | } |
1da177e4 | 532 | |
508df92d AB |
533 | static ssize_t acpi_battery_alarm_show(struct device *dev, |
534 | struct device_attribute *attr, | |
535 | char *buf) | |
536 | { | |
537 | struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev)); | |
538 | return sprintf(buf, "%d\n", battery->alarm * 1000); | |
539 | } | |
540 | ||
541 | static ssize_t acpi_battery_alarm_store(struct device *dev, | |
542 | struct device_attribute *attr, | |
543 | const char *buf, size_t count) | |
544 | { | |
545 | unsigned long x; | |
546 | struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev)); | |
547 | if (sscanf(buf, "%ld\n", &x) == 1) | |
548 | battery->alarm = x/1000; | |
549 | if (acpi_battery_present(battery)) | |
550 | acpi_battery_set_alarm(battery); | |
551 | return count; | |
552 | } | |
553 | ||
554 | static struct device_attribute alarm_attr = { | |
01e8ef11 | 555 | .attr = {.name = "alarm", .mode = 0644}, |
508df92d AB |
556 | .show = acpi_battery_alarm_show, |
557 | .store = acpi_battery_alarm_store, | |
558 | }; | |
559 | ||
560 | static int sysfs_add_battery(struct acpi_battery *battery) | |
561 | { | |
562 | int result; | |
563 | ||
ae6f6187 | 564 | if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) { |
508df92d AB |
565 | battery->bat.properties = charge_battery_props; |
566 | battery->bat.num_properties = | |
567 | ARRAY_SIZE(charge_battery_props); | |
568 | } else { | |
569 | battery->bat.properties = energy_battery_props; | |
570 | battery->bat.num_properties = | |
571 | ARRAY_SIZE(energy_battery_props); | |
572 | } | |
573 | ||
574 | battery->bat.name = acpi_device_bid(battery->device); | |
575 | battery->bat.type = POWER_SUPPLY_TYPE_BATTERY; | |
576 | battery->bat.get_property = acpi_battery_get_property; | |
577 | ||
578 | result = power_supply_register(&battery->device->dev, &battery->bat); | |
579 | if (result) | |
580 | return result; | |
581 | return device_create_file(battery->bat.dev, &alarm_attr); | |
582 | } | |
583 | ||
584 | static void sysfs_remove_battery(struct acpi_battery *battery) | |
585 | { | |
69d94ec6 | 586 | mutex_lock(&battery->sysfs_lock); |
9c921c22 | 587 | if (!battery->bat.dev) { |
69d94ec6 | 588 | mutex_unlock(&battery->sysfs_lock); |
508df92d | 589 | return; |
9c921c22 LT |
590 | } |
591 | ||
508df92d AB |
592 | device_remove_file(battery->bat.dev, &alarm_attr); |
593 | power_supply_unregister(&battery->bat); | |
9104476e | 594 | battery->bat.dev = NULL; |
69d94ec6 | 595 | mutex_unlock(&battery->sysfs_lock); |
bc76f90b HM |
596 | } |
597 | ||
4000e626 KI |
598 | static void find_battery(const struct dmi_header *dm, void *private) |
599 | { | |
600 | struct acpi_battery *battery = (struct acpi_battery *)private; | |
601 | /* Note: the hardcoded offsets below have been extracted from | |
602 | the source code of dmidecode. */ | |
603 | if (dm->type == DMI_ENTRY_PORTABLE_BATTERY && dm->length >= 8) { | |
604 | const u8 *dmi_data = (const u8 *)(dm + 1); | |
605 | int dmi_capacity = get_unaligned((const u16 *)(dmi_data + 6)); | |
606 | if (dm->length >= 18) | |
607 | dmi_capacity *= dmi_data[17]; | |
608 | if (battery->design_capacity * battery->design_voltage / 1000 | |
609 | != dmi_capacity && | |
610 | battery->design_capacity * 10 == dmi_capacity) | |
611 | set_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, | |
612 | &battery->flags); | |
613 | } | |
614 | } | |
615 | ||
557d5868 ZR |
616 | /* |
617 | * According to the ACPI spec, some kinds of primary batteries can | |
618 | * report percentage battery remaining capacity directly to OS. | |
619 | * In this case, it reports the Last Full Charged Capacity == 100 | |
620 | * and BatteryPresentRate == 0xFFFFFFFF. | |
621 | * | |
622 | * Now we found some battery reports percentage remaining capacity | |
623 | * even if it's rechargeable. | |
624 | * https://bugzilla.kernel.org/show_bug.cgi?id=15979 | |
625 | * | |
626 | * Handle this correctly so that they won't break userspace. | |
627 | */ | |
7b78622d | 628 | static void acpi_battery_quirks(struct acpi_battery *battery) |
557d5868 ZR |
629 | { |
630 | if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)) | |
0f4c6547 | 631 | return; |
557d5868 | 632 | |
0f4c6547 NM |
633 | if (battery->full_charge_capacity == 100 && |
634 | battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN && | |
635 | battery->capacity_now >= 0 && battery->capacity_now <= 100) { | |
557d5868 ZR |
636 | set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags); |
637 | battery->full_charge_capacity = battery->design_capacity; | |
638 | battery->capacity_now = (battery->capacity_now * | |
639 | battery->full_charge_capacity) / 100; | |
640 | } | |
4000e626 KI |
641 | |
642 | if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags)) | |
0f4c6547 | 643 | return; |
4000e626 KI |
644 | |
645 | if (battery->power_unit && dmi_name_in_vendors("LENOVO")) { | |
646 | const char *s; | |
647 | s = dmi_get_system_info(DMI_PRODUCT_VERSION); | |
648 | if (s && !strnicmp(s, "ThinkPad", 8)) { | |
649 | dmi_walk(find_battery, battery); | |
650 | if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, | |
651 | &battery->flags) && | |
652 | battery->design_voltage) { | |
653 | battery->design_capacity = | |
654 | battery->design_capacity * | |
655 | 10000 / battery->design_voltage; | |
656 | battery->full_charge_capacity = | |
657 | battery->full_charge_capacity * | |
658 | 10000 / battery->design_voltage; | |
659 | battery->design_capacity_warning = | |
660 | battery->design_capacity_warning * | |
661 | 10000 / battery->design_voltage; | |
662 | battery->capacity_now = battery->capacity_now * | |
663 | 10000 / battery->design_voltage; | |
664 | } | |
665 | } | |
666 | } | |
557d5868 ZR |
667 | } |
668 | ||
f1d4661a | 669 | static int acpi_battery_update(struct acpi_battery *battery) |
b6ce4083 | 670 | { |
50b17851 | 671 | int result, old_present = acpi_battery_present(battery); |
97749cd9 | 672 | result = acpi_battery_get_status(battery); |
508df92d | 673 | if (result) |
b6ce4083 | 674 | return result; |
508df92d AB |
675 | if (!acpi_battery_present(battery)) { |
676 | sysfs_remove_battery(battery); | |
97749cd9 | 677 | battery->update_time = 0; |
508df92d | 678 | return 0; |
b6ce4083 | 679 | } |
50b17851 AS |
680 | if (!battery->update_time || |
681 | old_present != acpi_battery_present(battery)) { | |
97749cd9 AS |
682 | result = acpi_battery_get_info(battery); |
683 | if (result) | |
684 | return result; | |
685 | acpi_battery_init_alarm(battery); | |
686 | } | |
eb03cb02 SH |
687 | if (!battery->bat.dev) { |
688 | result = sysfs_add_battery(battery); | |
689 | if (result) | |
690 | return result; | |
691 | } | |
557d5868 | 692 | result = acpi_battery_get_state(battery); |
7b78622d | 693 | acpi_battery_quirks(battery); |
557d5868 | 694 | return result; |
4bd35cdb VL |
695 | } |
696 | ||
da8aeb92 RW |
697 | static void acpi_battery_refresh(struct acpi_battery *battery) |
698 | { | |
c5971456 AW |
699 | int power_unit; |
700 | ||
da8aeb92 RW |
701 | if (!battery->bat.dev) |
702 | return; | |
703 | ||
c5971456 AW |
704 | power_unit = battery->power_unit; |
705 | ||
da8aeb92 | 706 | acpi_battery_get_info(battery); |
c5971456 AW |
707 | |
708 | if (power_unit == battery->power_unit) | |
709 | return; | |
710 | ||
711 | /* The battery has changed its reporting units. */ | |
da8aeb92 RW |
712 | sysfs_remove_battery(battery); |
713 | sysfs_add_battery(battery); | |
714 | } | |
715 | ||
1da177e4 LT |
716 | /* -------------------------------------------------------------------------- |
717 | Driver Interface | |
718 | -------------------------------------------------------------------------- */ | |
719 | ||
d9406691 | 720 | static void acpi_battery_notify(struct acpi_device *device, u32 event) |
1da177e4 | 721 | { |
d9406691 | 722 | struct acpi_battery *battery = acpi_driver_data(device); |
153e500f | 723 | struct device *old; |
d9406691 | 724 | |
1da177e4 | 725 | if (!battery) |
d550d98d | 726 | return; |
153e500f | 727 | old = battery->bat.dev; |
da8aeb92 RW |
728 | if (event == ACPI_BATTERY_NOTIFY_INFO) |
729 | acpi_battery_refresh(battery); | |
f1d4661a | 730 | acpi_battery_update(battery); |
f1d4661a | 731 | acpi_bus_generate_netlink_event(device->pnp.device_class, |
0794469d | 732 | dev_name(&device->dev), event, |
9ea7d575 | 733 | acpi_battery_present(battery)); |
2345baf4 | 734 | /* acpi_battery_update could remove power_supply object */ |
153e500f | 735 | if (old && battery->bat.dev) |
f79e1cec | 736 | power_supply_changed(&battery->bat); |
1da177e4 LT |
737 | } |
738 | ||
25be5821 KM |
739 | static int battery_notify(struct notifier_block *nb, |
740 | unsigned long mode, void *_unused) | |
741 | { | |
742 | struct acpi_battery *battery = container_of(nb, struct acpi_battery, | |
743 | pm_nb); | |
744 | switch (mode) { | |
d5a5911b | 745 | case PM_POST_HIBERNATION: |
25be5821 | 746 | case PM_POST_SUSPEND: |
6e17fb6a LT |
747 | if (battery->bat.dev) { |
748 | sysfs_remove_battery(battery); | |
749 | sysfs_add_battery(battery); | |
750 | } | |
25be5821 KM |
751 | break; |
752 | } | |
753 | ||
754 | return 0; | |
755 | } | |
756 | ||
4be44fcd | 757 | static int acpi_battery_add(struct acpi_device *device) |
1da177e4 | 758 | { |
4be44fcd | 759 | int result = 0; |
4be44fcd | 760 | struct acpi_battery *battery = NULL; |
952c63e9 | 761 | |
1da177e4 | 762 | if (!device) |
d550d98d | 763 | return -EINVAL; |
36bcbec7 | 764 | battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL); |
1da177e4 | 765 | if (!battery) |
d550d98d | 766 | return -ENOMEM; |
145def84 | 767 | battery->device = device; |
1da177e4 LT |
768 | strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME); |
769 | strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS); | |
db89b4f0 | 770 | device->driver_data = battery; |
038fdea2 | 771 | mutex_init(&battery->lock); |
69d94ec6 | 772 | mutex_init(&battery->sysfs_lock); |
952c63e9 | 773 | if (acpi_has_method(battery->device->handle, "_BIX")) |
c67fcd67 | 774 | set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags); |
eb03cb02 SH |
775 | result = acpi_battery_update(battery); |
776 | if (result) | |
777 | goto fail; | |
25be5821 | 778 | |
e80bba4b SH |
779 | printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n", |
780 | ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device), | |
781 | device->status.battery_present ? "present" : "absent"); | |
782 | ||
25be5821 KM |
783 | battery->pm_nb.notifier_call = battery_notify; |
784 | register_pm_notifier(&battery->pm_nb); | |
785 | ||
d550d98d | 786 | return result; |
e80bba4b SH |
787 | |
788 | fail: | |
789 | sysfs_remove_battery(battery); | |
790 | mutex_destroy(&battery->lock); | |
69d94ec6 | 791 | mutex_destroy(&battery->sysfs_lock); |
e80bba4b SH |
792 | kfree(battery); |
793 | return result; | |
1da177e4 LT |
794 | } |
795 | ||
51fac838 | 796 | static int acpi_battery_remove(struct acpi_device *device) |
1da177e4 | 797 | { |
4be44fcd | 798 | struct acpi_battery *battery = NULL; |
1da177e4 | 799 | |
1da177e4 | 800 | if (!device || !acpi_driver_data(device)) |
d550d98d | 801 | return -EINVAL; |
50dd0969 | 802 | battery = acpi_driver_data(device); |
25be5821 | 803 | unregister_pm_notifier(&battery->pm_nb); |
508df92d | 804 | sysfs_remove_battery(battery); |
038fdea2 | 805 | mutex_destroy(&battery->lock); |
69d94ec6 | 806 | mutex_destroy(&battery->sysfs_lock); |
1da177e4 | 807 | kfree(battery); |
d550d98d | 808 | return 0; |
1da177e4 LT |
809 | } |
810 | ||
90692404 | 811 | #ifdef CONFIG_PM_SLEEP |
34c4415a | 812 | /* this is needed to learn about changes made in suspended state */ |
a6f50dc8 | 813 | static int acpi_battery_resume(struct device *dev) |
34c4415a JK |
814 | { |
815 | struct acpi_battery *battery; | |
a6f50dc8 RW |
816 | |
817 | if (!dev) | |
34c4415a | 818 | return -EINVAL; |
a6f50dc8 RW |
819 | |
820 | battery = acpi_driver_data(to_acpi_device(dev)); | |
821 | if (!battery) | |
822 | return -EINVAL; | |
823 | ||
f1d4661a | 824 | battery->update_time = 0; |
508df92d | 825 | acpi_battery_update(battery); |
b6ce4083 | 826 | return 0; |
34c4415a | 827 | } |
90692404 | 828 | #endif |
34c4415a | 829 | |
a6f50dc8 RW |
830 | static SIMPLE_DEV_PM_OPS(acpi_battery_pm, NULL, acpi_battery_resume); |
831 | ||
aa650bbd AS |
832 | static struct acpi_driver acpi_battery_driver = { |
833 | .name = "battery", | |
834 | .class = ACPI_BATTERY_CLASS, | |
835 | .ids = battery_device_ids, | |
d9406691 | 836 | .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS, |
aa650bbd AS |
837 | .ops = { |
838 | .add = acpi_battery_add, | |
aa650bbd | 839 | .remove = acpi_battery_remove, |
d9406691 | 840 | .notify = acpi_battery_notify, |
aa650bbd | 841 | }, |
a6f50dc8 | 842 | .drv.pm = &acpi_battery_pm, |
aa650bbd AS |
843 | }; |
844 | ||
b0cbc861 | 845 | static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie) |
1da177e4 | 846 | { |
4d8316d5 | 847 | if (acpi_disabled) |
0f66af53 | 848 | return; |
1e2d9cdf | 849 | acpi_bus_register_driver(&acpi_battery_driver); |
0f66af53 AV |
850 | } |
851 | ||
852 | static int __init acpi_battery_init(void) | |
853 | { | |
854 | async_schedule(acpi_battery_init_async, NULL); | |
d550d98d | 855 | return 0; |
1da177e4 LT |
856 | } |
857 | ||
4be44fcd | 858 | static void __exit acpi_battery_exit(void) |
1da177e4 | 859 | { |
1da177e4 | 860 | acpi_bus_unregister_driver(&acpi_battery_driver); |
1da177e4 LT |
861 | } |
862 | ||
1da177e4 LT |
863 | module_init(acpi_battery_init); |
864 | module_exit(acpi_battery_exit); |