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