ACPI / battery: ensure acpi_battery_init() has finish
[deliverable/linux.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>
f43691c6 35#include <linux/delay.h>
5a0e3ad6 36#include <linux/slab.h>
25be5821 37#include <linux/suspend.h>
4000e626 38#include <asm/unaligned.h>
d7380965 39
3a670cc7
LT
40#ifdef CONFIG_ACPI_PROCFS_POWER
41#include <linux/proc_fs.h>
42#include <linux/seq_file.h>
43#include <asm/uaccess.h>
44#endif
45
8b48463f 46#include <linux/acpi.h>
d7380965
AS
47#include <linux/power_supply.h>
48
f03be352
AM
49#include "battery.h"
50
a192a958
LB
51#define PREFIX "ACPI: "
52
1da177e4
LT
53#define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
54
1da177e4 55#define ACPI_BATTERY_DEVICE_NAME "Battery"
1da177e4 56
ae6f6187
LT
57/* Battery power unit: 0 means mW, 1 means mA */
58#define ACPI_BATTERY_POWER_UNIT_MA 1
59
1ac5aaa6
ZR
60#define ACPI_BATTERY_STATE_DISCHARGING 0x1
61#define ACPI_BATTERY_STATE_CHARGING 0x2
62#define ACPI_BATTERY_STATE_CRITICAL 0x4
63
1da177e4 64#define _COMPONENT ACPI_BATTERY_COMPONENT
b6ce4083 65
f52fd66d 66ACPI_MODULE_NAME("battery");
1da177e4 67
f52fd66d 68MODULE_AUTHOR("Paul Diefenbaugh");
aa650bbd 69MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
7cda93e0 70MODULE_DESCRIPTION("ACPI Battery Driver");
1da177e4
LT
71MODULE_LICENSE("GPL");
72
eca21d91 73static async_cookie_t async_cookie;
a90b4038 74static int battery_bix_broken_package;
f43691c6 75static int battery_notification_delay_ms;
f1d4661a
AS
76static unsigned int cache_time = 1000;
77module_param(cache_time, uint, 0644);
78MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
b6ce4083 79
3a670cc7
LT
80#ifdef CONFIG_ACPI_PROCFS_POWER
81extern struct proc_dir_entry *acpi_lock_battery_dir(void);
82extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
83
84enum acpi_battery_files {
85 info_tag = 0,
86 state_tag,
87 alarm_tag,
88 ACPI_BATTERY_NUMFILES,
89};
90
91#endif
92
1ba90e3a
TR
93static const struct acpi_device_id battery_device_ids[] = {
94 {"PNP0C0A", 0},
95 {"", 0},
96};
1ba90e3a 97
aa650bbd 98MODULE_DEVICE_TABLE(acpi, battery_device_ids);
1da177e4 99
7b3bcc4a
AS
100enum {
101 ACPI_BATTERY_ALARM_PRESENT,
c67fcd67 102 ACPI_BATTERY_XINFO_PRESENT,
557d5868 103 ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY,
4000e626
KI
104 /* On Lenovo Thinkpad models from 2010 and 2011, the power unit
105 switches between mWh and mAh depending on whether the system
106 is running on battery or not. When mAh is the unit, most
107 reported values are incorrect and need to be adjusted by
108 10000/design_voltage. Verified on x201, t410, t410s, and x220.
109 Pre-2010 and 2012 models appear to always report in mWh and
110 are thus unaffected (tested with t42, t61, t500, x200, x300,
111 and x230). Also, in mid-2012 Lenovo issued a BIOS update for
112 the 2011 models that fixes the issue (tested on x220 with a
113 post-1.29 BIOS), but as of Nov. 2012, no such update is
114 available for the 2010 models. */
115 ACPI_BATTERY_QUIRK_THINKPAD_MAH,
7b3bcc4a 116};
78490d82 117
1da177e4 118struct acpi_battery {
038fdea2 119 struct mutex lock;
69d94ec6 120 struct mutex sysfs_lock;
297d716f
KK
121 struct power_supply *bat;
122 struct power_supply_desc bat_desc;
f1d4661a 123 struct acpi_device *device;
25be5821 124 struct notifier_block pm_nb;
f1d4661a 125 unsigned long update_time;
016d5baa 126 int revision;
7faa144a 127 int rate_now;
d7380965
AS
128 int capacity_now;
129 int voltage_now;
038fdea2 130 int design_capacity;
d7380965 131 int full_charge_capacity;
038fdea2
AS
132 int technology;
133 int design_voltage;
134 int design_capacity_warning;
135 int design_capacity_low;
c67fcd67
AS
136 int cycle_count;
137 int measurement_accuracy;
138 int max_sampling_time;
139 int min_sampling_time;
140 int max_averaging_interval;
141 int min_averaging_interval;
038fdea2
AS
142 int capacity_granularity_1;
143 int capacity_granularity_2;
f1d4661a 144 int alarm;
038fdea2
AS
145 char model_number[32];
146 char serial_number[32];
147 char type[32];
148 char oem_info[32];
f1d4661a
AS
149 int state;
150 int power_unit;
7b3bcc4a 151 unsigned long flags;
1da177e4
LT
152};
153
297d716f 154#define to_acpi_battery(x) power_supply_get_drvdata(x)
d7380965 155
efd941f1 156static inline int acpi_battery_present(struct acpi_battery *battery)
b6ce4083 157{
78490d82
AS
158 return battery->device->status.battery_present;
159}
038fdea2 160
d7380965
AS
161static int acpi_battery_technology(struct acpi_battery *battery)
162{
163 if (!strcasecmp("NiCd", battery->type))
164 return POWER_SUPPLY_TECHNOLOGY_NiCd;
165 if (!strcasecmp("NiMH", battery->type))
166 return POWER_SUPPLY_TECHNOLOGY_NiMH;
167 if (!strcasecmp("LION", battery->type))
168 return POWER_SUPPLY_TECHNOLOGY_LION;
ad40e68b 169 if (!strncasecmp("LI-ION", battery->type, 6))
0bde7eee 170 return POWER_SUPPLY_TECHNOLOGY_LION;
d7380965
AS
171 if (!strcasecmp("LiP", battery->type))
172 return POWER_SUPPLY_TECHNOLOGY_LIPO;
173 return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
174}
175
9104476e 176static int acpi_battery_get_state(struct acpi_battery *battery);
b19073a0 177
56f382a0
RH
178static int acpi_battery_is_charged(struct acpi_battery *battery)
179{
1ac5aaa6 180 /* charging, discharging or critical low */
56f382a0
RH
181 if (battery->state != 0)
182 return 0;
183
184 /* battery not reporting charge */
185 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
186 battery->capacity_now == 0)
187 return 0;
188
189 /* good batteries update full_charge as the batteries degrade */
190 if (battery->full_charge_capacity == battery->capacity_now)
191 return 1;
192
193 /* fallback to using design values for broken batteries */
194 if (battery->design_capacity == battery->capacity_now)
195 return 1;
196
197 /* we don't do any sort of metric based on percentages */
198 return 0;
199}
200
d7380965
AS
201static int acpi_battery_get_property(struct power_supply *psy,
202 enum power_supply_property psp,
203 union power_supply_propval *val)
204{
a1b4bd69 205 int ret = 0;
d7380965
AS
206 struct acpi_battery *battery = to_acpi_battery(psy);
207
9104476e
AS
208 if (acpi_battery_present(battery)) {
209 /* run battery update only if it is present */
210 acpi_battery_get_state(battery);
211 } else if (psp != POWER_SUPPLY_PROP_PRESENT)
d7380965
AS
212 return -ENODEV;
213 switch (psp) {
214 case POWER_SUPPLY_PROP_STATUS:
1ac5aaa6 215 if (battery->state & ACPI_BATTERY_STATE_DISCHARGING)
d7380965 216 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1ac5aaa6 217 else if (battery->state & ACPI_BATTERY_STATE_CHARGING)
d7380965 218 val->intval = POWER_SUPPLY_STATUS_CHARGING;
56f382a0 219 else if (acpi_battery_is_charged(battery))
d7380965 220 val->intval = POWER_SUPPLY_STATUS_FULL;
4c41d3ad
RD
221 else
222 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
d7380965
AS
223 break;
224 case POWER_SUPPLY_PROP_PRESENT:
225 val->intval = acpi_battery_present(battery);
226 break;
227 case POWER_SUPPLY_PROP_TECHNOLOGY:
228 val->intval = acpi_battery_technology(battery);
229 break;
c67fcd67
AS
230 case POWER_SUPPLY_PROP_CYCLE_COUNT:
231 val->intval = battery->cycle_count;
232 break;
d7380965 233 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
a1b4bd69
RW
234 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
235 ret = -ENODEV;
236 else
237 val->intval = battery->design_voltage * 1000;
d7380965
AS
238 break;
239 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
a1b4bd69
RW
240 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
241 ret = -ENODEV;
242 else
243 val->intval = battery->voltage_now * 1000;
d7380965
AS
244 break;
245 case POWER_SUPPLY_PROP_CURRENT_NOW:
7faa144a 246 case POWER_SUPPLY_PROP_POWER_NOW:
a1b4bd69
RW
247 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
248 ret = -ENODEV;
249 else
250 val->intval = battery->rate_now * 1000;
d7380965
AS
251 break;
252 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
253 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
a1b4bd69
RW
254 if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
255 ret = -ENODEV;
256 else
257 val->intval = battery->design_capacity * 1000;
d7380965
AS
258 break;
259 case POWER_SUPPLY_PROP_CHARGE_FULL:
260 case POWER_SUPPLY_PROP_ENERGY_FULL:
a1b4bd69
RW
261 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
262 ret = -ENODEV;
263 else
264 val->intval = battery->full_charge_capacity * 1000;
d7380965
AS
265 break;
266 case POWER_SUPPLY_PROP_CHARGE_NOW:
267 case POWER_SUPPLY_PROP_ENERGY_NOW:
a1b4bd69
RW
268 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
269 ret = -ENODEV;
270 else
271 val->intval = battery->capacity_now * 1000;
d7380965 272 break;
a58e1150 273 case POWER_SUPPLY_PROP_CAPACITY:
274 if (battery->capacity_now && battery->full_charge_capacity)
275 val->intval = battery->capacity_now * 100/
276 battery->full_charge_capacity;
277 else
278 val->intval = 0;
279 break;
1ac5aaa6
ZR
280 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
281 if (battery->state & ACPI_BATTERY_STATE_CRITICAL)
282 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
283 else if (test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags) &&
284 (battery->capacity_now <= battery->alarm))
285 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
286 else if (acpi_battery_is_charged(battery))
287 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
288 else
289 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
290 break;
d7380965
AS
291 case POWER_SUPPLY_PROP_MODEL_NAME:
292 val->strval = battery->model_number;
293 break;
294 case POWER_SUPPLY_PROP_MANUFACTURER:
295 val->strval = battery->oem_info;
296 break;
7c2670bb 297 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
298 val->strval = battery->serial_number;
299 break;
d7380965 300 default:
a1b4bd69 301 ret = -EINVAL;
d7380965 302 }
a1b4bd69 303 return ret;
d7380965
AS
304}
305
306static enum power_supply_property charge_battery_props[] = {
307 POWER_SUPPLY_PROP_STATUS,
308 POWER_SUPPLY_PROP_PRESENT,
309 POWER_SUPPLY_PROP_TECHNOLOGY,
c67fcd67 310 POWER_SUPPLY_PROP_CYCLE_COUNT,
d7380965
AS
311 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
312 POWER_SUPPLY_PROP_VOLTAGE_NOW,
313 POWER_SUPPLY_PROP_CURRENT_NOW,
314 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
315 POWER_SUPPLY_PROP_CHARGE_FULL,
316 POWER_SUPPLY_PROP_CHARGE_NOW,
a58e1150 317 POWER_SUPPLY_PROP_CAPACITY,
1ac5aaa6 318 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
d7380965
AS
319 POWER_SUPPLY_PROP_MODEL_NAME,
320 POWER_SUPPLY_PROP_MANUFACTURER,
7c2670bb 321 POWER_SUPPLY_PROP_SERIAL_NUMBER,
d7380965
AS
322};
323
324static enum power_supply_property energy_battery_props[] = {
325 POWER_SUPPLY_PROP_STATUS,
326 POWER_SUPPLY_PROP_PRESENT,
327 POWER_SUPPLY_PROP_TECHNOLOGY,
c67fcd67 328 POWER_SUPPLY_PROP_CYCLE_COUNT,
d7380965
AS
329 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
330 POWER_SUPPLY_PROP_VOLTAGE_NOW,
7faa144a 331 POWER_SUPPLY_PROP_POWER_NOW,
d7380965
AS
332 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
333 POWER_SUPPLY_PROP_ENERGY_FULL,
334 POWER_SUPPLY_PROP_ENERGY_NOW,
a58e1150 335 POWER_SUPPLY_PROP_CAPACITY,
1ac5aaa6 336 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
d7380965
AS
337 POWER_SUPPLY_PROP_MODEL_NAME,
338 POWER_SUPPLY_PROP_MANUFACTURER,
7c2670bb 339 POWER_SUPPLY_PROP_SERIAL_NUMBER,
d7380965
AS
340};
341
3a670cc7
LT
342#ifdef CONFIG_ACPI_PROCFS_POWER
343inline char *acpi_battery_units(struct acpi_battery *battery)
344{
345 return (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) ?
346 "mA" : "mW";
347}
348#endif
349
78490d82
AS
350/* --------------------------------------------------------------------------
351 Battery Management
352 -------------------------------------------------------------------------- */
038fdea2
AS
353struct acpi_offsets {
354 size_t offset; /* offset inside struct acpi_sbs_battery */
355 u8 mode; /* int or string? */
356};
b6ce4083 357
038fdea2
AS
358static struct acpi_offsets state_offsets[] = {
359 {offsetof(struct acpi_battery, state), 0},
7faa144a 360 {offsetof(struct acpi_battery, rate_now), 0},
d7380965
AS
361 {offsetof(struct acpi_battery, capacity_now), 0},
362 {offsetof(struct acpi_battery, voltage_now), 0},
038fdea2 363};
b6ce4083 364
038fdea2
AS
365static struct acpi_offsets info_offsets[] = {
366 {offsetof(struct acpi_battery, power_unit), 0},
367 {offsetof(struct acpi_battery, design_capacity), 0},
d7380965 368 {offsetof(struct acpi_battery, full_charge_capacity), 0},
038fdea2
AS
369 {offsetof(struct acpi_battery, technology), 0},
370 {offsetof(struct acpi_battery, design_voltage), 0},
371 {offsetof(struct acpi_battery, design_capacity_warning), 0},
372 {offsetof(struct acpi_battery, design_capacity_low), 0},
373 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
374 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
375 {offsetof(struct acpi_battery, model_number), 1},
376 {offsetof(struct acpi_battery, serial_number), 1},
377 {offsetof(struct acpi_battery, type), 1},
378 {offsetof(struct acpi_battery, oem_info), 1},
379};
b6ce4083 380
c67fcd67 381static struct acpi_offsets extended_info_offsets[] = {
016d5baa 382 {offsetof(struct acpi_battery, revision), 0},
c67fcd67
AS
383 {offsetof(struct acpi_battery, power_unit), 0},
384 {offsetof(struct acpi_battery, design_capacity), 0},
385 {offsetof(struct acpi_battery, full_charge_capacity), 0},
386 {offsetof(struct acpi_battery, technology), 0},
387 {offsetof(struct acpi_battery, design_voltage), 0},
388 {offsetof(struct acpi_battery, design_capacity_warning), 0},
389 {offsetof(struct acpi_battery, design_capacity_low), 0},
390 {offsetof(struct acpi_battery, cycle_count), 0},
391 {offsetof(struct acpi_battery, measurement_accuracy), 0},
392 {offsetof(struct acpi_battery, max_sampling_time), 0},
393 {offsetof(struct acpi_battery, min_sampling_time), 0},
394 {offsetof(struct acpi_battery, max_averaging_interval), 0},
395 {offsetof(struct acpi_battery, min_averaging_interval), 0},
396 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
397 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
398 {offsetof(struct acpi_battery, model_number), 1},
399 {offsetof(struct acpi_battery, serial_number), 1},
400 {offsetof(struct acpi_battery, type), 1},
401 {offsetof(struct acpi_battery, oem_info), 1},
402};
403
038fdea2
AS
404static int extract_package(struct acpi_battery *battery,
405 union acpi_object *package,
406 struct acpi_offsets *offsets, int num)
407{
106449e8 408 int i;
038fdea2
AS
409 union acpi_object *element;
410 if (package->type != ACPI_TYPE_PACKAGE)
411 return -EFAULT;
412 for (i = 0; i < num; ++i) {
413 if (package->package.count <= i)
414 return -EFAULT;
415 element = &package->package.elements[i];
416 if (offsets[i].mode) {
106449e8
AS
417 u8 *ptr = (u8 *)battery + offsets[i].offset;
418 if (element->type == ACPI_TYPE_STRING ||
419 element->type == ACPI_TYPE_BUFFER)
420 strncpy(ptr, element->string.pointer, 32);
421 else if (element->type == ACPI_TYPE_INTEGER) {
422 strncpy(ptr, (u8 *)&element->integer.value,
439913ff
LM
423 sizeof(u64));
424 ptr[sizeof(u64)] = 0;
b8a1bdb1
AS
425 } else
426 *ptr = 0; /* don't have value */
038fdea2 427 } else {
b8a1bdb1
AS
428 int *x = (int *)((u8 *)battery + offsets[i].offset);
429 *x = (element->type == ACPI_TYPE_INTEGER) ?
430 element->integer.value : -1;
038fdea2 431 }
b6ce4083 432 }
b6ce4083
VL
433 return 0;
434}
435
436static int acpi_battery_get_status(struct acpi_battery *battery)
437{
aa650bbd 438 if (acpi_bus_get_status(battery->device)) {
b6ce4083
VL
439 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
440 return -ENODEV;
441 }
aa650bbd 442 return 0;
b6ce4083
VL
443}
444
445static int acpi_battery_get_info(struct acpi_battery *battery)
1da177e4 446{
aa650bbd 447 int result = -EFAULT;
4be44fcd 448 acpi_status status = 0;
0f4c6547 449 char *name = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags) ?
c67fcd67
AS
450 "_BIX" : "_BIF";
451
4be44fcd 452 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1da177e4 453
b6ce4083
VL
454 if (!acpi_battery_present(battery))
455 return 0;
038fdea2 456 mutex_lock(&battery->lock);
c67fcd67
AS
457 status = acpi_evaluate_object(battery->device->handle, name,
458 NULL, &buffer);
038fdea2 459 mutex_unlock(&battery->lock);
aa650bbd 460
1da177e4 461 if (ACPI_FAILURE(status)) {
c67fcd67 462 ACPI_EXCEPTION((AE_INFO, status, "Evaluating %s", name));
d550d98d 463 return -ENODEV;
1da177e4 464 }
a90b4038
LT
465
466 if (battery_bix_broken_package)
467 result = extract_package(battery, buffer.pointer,
468 extended_info_offsets + 1,
469 ARRAY_SIZE(extended_info_offsets) - 1);
470 else if (test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags))
c67fcd67
AS
471 result = extract_package(battery, buffer.pointer,
472 extended_info_offsets,
473 ARRAY_SIZE(extended_info_offsets));
474 else
475 result = extract_package(battery, buffer.pointer,
476 info_offsets, ARRAY_SIZE(info_offsets));
78490d82 477 kfree(buffer.pointer);
557d5868
ZR
478 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
479 battery->full_charge_capacity = battery->design_capacity;
4000e626
KI
480 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) &&
481 battery->power_unit && battery->design_voltage) {
482 battery->design_capacity = battery->design_capacity *
483 10000 / battery->design_voltage;
484 battery->full_charge_capacity = battery->full_charge_capacity *
485 10000 / battery->design_voltage;
486 battery->design_capacity_warning =
487 battery->design_capacity_warning *
488 10000 / battery->design_voltage;
489 /* Curiously, design_capacity_low, unlike the rest of them,
490 is correct. */
491 /* capacity_granularity_* equal 1 on the systems tested, so
492 it's impossible to tell if they would need an adjustment
493 or not if their values were higher. */
494 }
d550d98d 495 return result;
1da177e4
LT
496}
497
b6ce4083 498static int acpi_battery_get_state(struct acpi_battery *battery)
1da177e4 499{
4be44fcd
LB
500 int result = 0;
501 acpi_status status = 0;
502 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1da177e4 503
b6ce4083
VL
504 if (!acpi_battery_present(battery))
505 return 0;
1da177e4 506
f1d4661a
AS
507 if (battery->update_time &&
508 time_before(jiffies, battery->update_time +
509 msecs_to_jiffies(cache_time)))
510 return 0;
511
038fdea2 512 mutex_lock(&battery->lock);
f1d4661a 513 status = acpi_evaluate_object(battery->device->handle, "_BST",
038fdea2
AS
514 NULL, &buffer);
515 mutex_unlock(&battery->lock);
5b31d895 516
1da177e4 517 if (ACPI_FAILURE(status)) {
a6fc6720 518 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
d550d98d 519 return -ENODEV;
1da177e4 520 }
aa650bbd 521
038fdea2
AS
522 result = extract_package(battery, buffer.pointer,
523 state_offsets, ARRAY_SIZE(state_offsets));
f1d4661a 524 battery->update_time = jiffies;
78490d82 525 kfree(buffer.pointer);
bc76f90b 526
55003b21
LT
527 /* For buggy DSDTs that report negative 16-bit values for either
528 * charging or discharging current and/or report 0 as 65536
529 * due to bad math.
530 */
531 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA &&
532 battery->rate_now != ACPI_BATTERY_VALUE_UNKNOWN &&
533 (s16)(battery->rate_now) < 0) {
bc76f90b 534 battery->rate_now = abs((s16)battery->rate_now);
9237516c
MK
535 printk_once(KERN_WARNING FW_BUG
536 "battery: (dis)charge rate invalid.\n");
55003b21 537 }
bc76f90b 538
557d5868
ZR
539 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)
540 && battery->capacity_now >= 0 && battery->capacity_now <= 100)
541 battery->capacity_now = (battery->capacity_now *
542 battery->full_charge_capacity) / 100;
4000e626
KI
543 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) &&
544 battery->power_unit && battery->design_voltage) {
545 battery->capacity_now = battery->capacity_now *
546 10000 / battery->design_voltage;
547 }
b6ce4083
VL
548 return result;
549}
1da177e4 550
aa650bbd 551static int acpi_battery_set_alarm(struct acpi_battery *battery)
1da177e4 552{
4be44fcd 553 acpi_status status = 0;
1da177e4 554
c67fcd67 555 if (!acpi_battery_present(battery) ||
7b3bcc4a 556 !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags))
d550d98d 557 return -ENODEV;
1da177e4 558
038fdea2 559 mutex_lock(&battery->lock);
0db98202
JL
560 status = acpi_execute_simple_method(battery->device->handle, "_BTP",
561 battery->alarm);
038fdea2 562 mutex_unlock(&battery->lock);
aa650bbd 563
1da177e4 564 if (ACPI_FAILURE(status))
d550d98d 565 return -ENODEV;
1da177e4 566
aa650bbd 567 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm));
d550d98d 568 return 0;
1da177e4
LT
569}
570
b6ce4083 571static int acpi_battery_init_alarm(struct acpi_battery *battery)
1da177e4 572{
b6ce4083 573 /* See if alarms are supported, and if so, set default */
952c63e9 574 if (!acpi_has_method(battery->device->handle, "_BTP")) {
7b3bcc4a 575 clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
f1d4661a 576 return 0;
b6ce4083 577 }
7b3bcc4a 578 set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
f1d4661a
AS
579 if (!battery->alarm)
580 battery->alarm = battery->design_capacity_warning;
aa650bbd 581 return acpi_battery_set_alarm(battery);
b6ce4083 582}
1da177e4 583
508df92d
AB
584static ssize_t acpi_battery_alarm_show(struct device *dev,
585 struct device_attribute *attr,
586 char *buf)
587{
588 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
589 return sprintf(buf, "%d\n", battery->alarm * 1000);
590}
591
592static ssize_t acpi_battery_alarm_store(struct device *dev,
593 struct device_attribute *attr,
594 const char *buf, size_t count)
595{
596 unsigned long x;
597 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
47a08c85 598 if (sscanf(buf, "%lu\n", &x) == 1)
508df92d
AB
599 battery->alarm = x/1000;
600 if (acpi_battery_present(battery))
601 acpi_battery_set_alarm(battery);
602 return count;
603}
604
605static struct device_attribute alarm_attr = {
01e8ef11 606 .attr = {.name = "alarm", .mode = 0644},
508df92d
AB
607 .show = acpi_battery_alarm_show,
608 .store = acpi_battery_alarm_store,
609};
610
611static int sysfs_add_battery(struct acpi_battery *battery)
612{
297d716f 613 struct power_supply_config psy_cfg = { .drv_data = battery, };
508df92d 614
ae6f6187 615 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) {
297d716f
KK
616 battery->bat_desc.properties = charge_battery_props;
617 battery->bat_desc.num_properties =
508df92d
AB
618 ARRAY_SIZE(charge_battery_props);
619 } else {
297d716f
KK
620 battery->bat_desc.properties = energy_battery_props;
621 battery->bat_desc.num_properties =
508df92d
AB
622 ARRAY_SIZE(energy_battery_props);
623 }
624
297d716f
KK
625 battery->bat_desc.name = acpi_device_bid(battery->device);
626 battery->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY;
627 battery->bat_desc.get_property = acpi_battery_get_property;
508df92d 628
297d716f
KK
629 battery->bat = power_supply_register_no_ws(&battery->device->dev,
630 &battery->bat_desc, &psy_cfg);
e0d1f09e 631
297d716f
KK
632 if (IS_ERR(battery->bat)) {
633 int result = PTR_ERR(battery->bat);
634
635 battery->bat = NULL;
508df92d 636 return result;
297d716f
KK
637 }
638 return device_create_file(&battery->bat->dev, &alarm_attr);
508df92d
AB
639}
640
641static void sysfs_remove_battery(struct acpi_battery *battery)
642{
69d94ec6 643 mutex_lock(&battery->sysfs_lock);
297d716f 644 if (!battery->bat) {
69d94ec6 645 mutex_unlock(&battery->sysfs_lock);
508df92d 646 return;
9c921c22
LT
647 }
648
297d716f
KK
649 device_remove_file(&battery->bat->dev, &alarm_attr);
650 power_supply_unregister(battery->bat);
651 battery->bat = NULL;
69d94ec6 652 mutex_unlock(&battery->sysfs_lock);
bc76f90b
HM
653}
654
4000e626
KI
655static void find_battery(const struct dmi_header *dm, void *private)
656{
657 struct acpi_battery *battery = (struct acpi_battery *)private;
658 /* Note: the hardcoded offsets below have been extracted from
659 the source code of dmidecode. */
660 if (dm->type == DMI_ENTRY_PORTABLE_BATTERY && dm->length >= 8) {
661 const u8 *dmi_data = (const u8 *)(dm + 1);
662 int dmi_capacity = get_unaligned((const u16 *)(dmi_data + 6));
663 if (dm->length >= 18)
664 dmi_capacity *= dmi_data[17];
665 if (battery->design_capacity * battery->design_voltage / 1000
666 != dmi_capacity &&
667 battery->design_capacity * 10 == dmi_capacity)
668 set_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH,
669 &battery->flags);
670 }
671}
672
557d5868
ZR
673/*
674 * According to the ACPI spec, some kinds of primary batteries can
675 * report percentage battery remaining capacity directly to OS.
676 * In this case, it reports the Last Full Charged Capacity == 100
677 * and BatteryPresentRate == 0xFFFFFFFF.
678 *
679 * Now we found some battery reports percentage remaining capacity
680 * even if it's rechargeable.
681 * https://bugzilla.kernel.org/show_bug.cgi?id=15979
682 *
683 * Handle this correctly so that they won't break userspace.
684 */
7b78622d 685static void acpi_battery_quirks(struct acpi_battery *battery)
557d5868
ZR
686{
687 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
0f4c6547 688 return;
557d5868 689
0f4c6547
NM
690 if (battery->full_charge_capacity == 100 &&
691 battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN &&
692 battery->capacity_now >= 0 && battery->capacity_now <= 100) {
557d5868
ZR
693 set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags);
694 battery->full_charge_capacity = battery->design_capacity;
695 battery->capacity_now = (battery->capacity_now *
696 battery->full_charge_capacity) / 100;
697 }
4000e626
KI
698
699 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags))
0f4c6547 700 return;
4000e626
KI
701
702 if (battery->power_unit && dmi_name_in_vendors("LENOVO")) {
703 const char *s;
704 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
ffd8a731 705 if (s && !strncasecmp(s, "ThinkPad", 8)) {
4000e626
KI
706 dmi_walk(find_battery, battery);
707 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH,
708 &battery->flags) &&
709 battery->design_voltage) {
710 battery->design_capacity =
711 battery->design_capacity *
712 10000 / battery->design_voltage;
713 battery->full_charge_capacity =
714 battery->full_charge_capacity *
715 10000 / battery->design_voltage;
716 battery->design_capacity_warning =
717 battery->design_capacity_warning *
718 10000 / battery->design_voltage;
719 battery->capacity_now = battery->capacity_now *
720 10000 / battery->design_voltage;
721 }
722 }
723 }
557d5868
ZR
724}
725
9e50bc14 726static int acpi_battery_update(struct acpi_battery *battery, bool resume)
b6ce4083 727{
50b17851 728 int result, old_present = acpi_battery_present(battery);
97749cd9 729 result = acpi_battery_get_status(battery);
508df92d 730 if (result)
b6ce4083 731 return result;
508df92d
AB
732 if (!acpi_battery_present(battery)) {
733 sysfs_remove_battery(battery);
97749cd9 734 battery->update_time = 0;
508df92d 735 return 0;
b6ce4083 736 }
9e50bc14
LT
737
738 if (resume)
739 return 0;
740
50b17851
AS
741 if (!battery->update_time ||
742 old_present != acpi_battery_present(battery)) {
97749cd9
AS
743 result = acpi_battery_get_info(battery);
744 if (result)
745 return result;
746 acpi_battery_init_alarm(battery);
747 }
297d716f 748 if (!battery->bat) {
eb03cb02
SH
749 result = sysfs_add_battery(battery);
750 if (result)
751 return result;
752 }
557d5868 753 result = acpi_battery_get_state(battery);
e0d1f09e
ZR
754 if (result)
755 return result;
7b78622d 756 acpi_battery_quirks(battery);
e0d1f09e
ZR
757
758 /*
759 * Wakeup the system if battery is critical low
760 * or lower than the alarm level
761 */
762 if ((battery->state & ACPI_BATTERY_STATE_CRITICAL) ||
763 (test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags) &&
764 (battery->capacity_now <= battery->alarm)))
765 pm_wakeup_event(&battery->device->dev, 0);
766
557d5868 767 return result;
4bd35cdb
VL
768}
769
da8aeb92
RW
770static void acpi_battery_refresh(struct acpi_battery *battery)
771{
c5971456
AW
772 int power_unit;
773
297d716f 774 if (!battery->bat)
da8aeb92
RW
775 return;
776
c5971456
AW
777 power_unit = battery->power_unit;
778
da8aeb92 779 acpi_battery_get_info(battery);
c5971456
AW
780
781 if (power_unit == battery->power_unit)
782 return;
783
784 /* The battery has changed its reporting units. */
da8aeb92
RW
785 sysfs_remove_battery(battery);
786 sysfs_add_battery(battery);
787}
788
3a670cc7
LT
789/* --------------------------------------------------------------------------
790 FS Interface (/proc)
791 -------------------------------------------------------------------------- */
792
793#ifdef CONFIG_ACPI_PROCFS_POWER
794static struct proc_dir_entry *acpi_battery_dir;
795
796static int acpi_battery_print_info(struct seq_file *seq, int result)
797{
798 struct acpi_battery *battery = seq->private;
799
800 if (result)
801 goto end;
802
803 seq_printf(seq, "present: %s\n",
804 acpi_battery_present(battery) ? "yes" : "no");
805 if (!acpi_battery_present(battery))
806 goto end;
807 if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
808 seq_printf(seq, "design capacity: unknown\n");
809 else
810 seq_printf(seq, "design capacity: %d %sh\n",
811 battery->design_capacity,
812 acpi_battery_units(battery));
813
814 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
815 seq_printf(seq, "last full capacity: unknown\n");
816 else
817 seq_printf(seq, "last full capacity: %d %sh\n",
818 battery->full_charge_capacity,
819 acpi_battery_units(battery));
820
821 seq_printf(seq, "battery technology: %srechargeable\n",
822 (!battery->technology)?"non-":"");
823
824 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
825 seq_printf(seq, "design voltage: unknown\n");
826 else
827 seq_printf(seq, "design voltage: %d mV\n",
828 battery->design_voltage);
829 seq_printf(seq, "design capacity warning: %d %sh\n",
830 battery->design_capacity_warning,
831 acpi_battery_units(battery));
832 seq_printf(seq, "design capacity low: %d %sh\n",
833 battery->design_capacity_low,
834 acpi_battery_units(battery));
835 seq_printf(seq, "cycle count: %i\n", battery->cycle_count);
836 seq_printf(seq, "capacity granularity 1: %d %sh\n",
837 battery->capacity_granularity_1,
838 acpi_battery_units(battery));
839 seq_printf(seq, "capacity granularity 2: %d %sh\n",
840 battery->capacity_granularity_2,
841 acpi_battery_units(battery));
842 seq_printf(seq, "model number: %s\n", battery->model_number);
843 seq_printf(seq, "serial number: %s\n", battery->serial_number);
844 seq_printf(seq, "battery type: %s\n", battery->type);
845 seq_printf(seq, "OEM info: %s\n", battery->oem_info);
846 end:
847 if (result)
848 seq_printf(seq, "ERROR: Unable to read battery info\n");
849 return result;
850}
851
852static int acpi_battery_print_state(struct seq_file *seq, int result)
853{
854 struct acpi_battery *battery = seq->private;
855
856 if (result)
857 goto end;
858
859 seq_printf(seq, "present: %s\n",
860 acpi_battery_present(battery) ? "yes" : "no");
861 if (!acpi_battery_present(battery))
862 goto end;
863
864 seq_printf(seq, "capacity state: %s\n",
865 (battery->state & 0x04) ? "critical" : "ok");
866 if ((battery->state & 0x01) && (battery->state & 0x02))
867 seq_printf(seq,
868 "charging state: charging/discharging\n");
869 else if (battery->state & 0x01)
870 seq_printf(seq, "charging state: discharging\n");
871 else if (battery->state & 0x02)
872 seq_printf(seq, "charging state: charging\n");
873 else
874 seq_printf(seq, "charging state: charged\n");
875
876 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
877 seq_printf(seq, "present rate: unknown\n");
878 else
879 seq_printf(seq, "present rate: %d %s\n",
880 battery->rate_now, acpi_battery_units(battery));
881
882 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
883 seq_printf(seq, "remaining capacity: unknown\n");
884 else
885 seq_printf(seq, "remaining capacity: %d %sh\n",
886 battery->capacity_now, acpi_battery_units(battery));
887 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
888 seq_printf(seq, "present voltage: unknown\n");
889 else
890 seq_printf(seq, "present voltage: %d mV\n",
891 battery->voltage_now);
892 end:
893 if (result)
894 seq_printf(seq, "ERROR: Unable to read battery state\n");
895
896 return result;
897}
898
899static int acpi_battery_print_alarm(struct seq_file *seq, int result)
900{
901 struct acpi_battery *battery = seq->private;
902
903 if (result)
904 goto end;
905
906 if (!acpi_battery_present(battery)) {
907 seq_printf(seq, "present: no\n");
908 goto end;
909 }
910 seq_printf(seq, "alarm: ");
911 if (!battery->alarm)
912 seq_printf(seq, "unsupported\n");
913 else
914 seq_printf(seq, "%u %sh\n", battery->alarm,
915 acpi_battery_units(battery));
916 end:
917 if (result)
918 seq_printf(seq, "ERROR: Unable to read battery alarm\n");
919 return result;
920}
921
922static ssize_t acpi_battery_write_alarm(struct file *file,
923 const char __user * buffer,
924 size_t count, loff_t * ppos)
925{
926 int result = 0;
927 char alarm_string[12] = { '\0' };
928 struct seq_file *m = file->private_data;
929 struct acpi_battery *battery = m->private;
930
931 if (!battery || (count > sizeof(alarm_string) - 1))
932 return -EINVAL;
933 if (!acpi_battery_present(battery)) {
934 result = -ENODEV;
935 goto end;
936 }
937 if (copy_from_user(alarm_string, buffer, count)) {
938 result = -EFAULT;
939 goto end;
940 }
941 alarm_string[count] = '\0';
3d915894
CJ
942 if (kstrtoint(alarm_string, 0, &battery->alarm)) {
943 result = -EINVAL;
944 goto end;
945 }
3a670cc7
LT
946 result = acpi_battery_set_alarm(battery);
947 end:
948 if (!result)
949 return count;
950 return result;
951}
952
953typedef int(*print_func)(struct seq_file *seq, int result);
954
955static print_func acpi_print_funcs[ACPI_BATTERY_NUMFILES] = {
956 acpi_battery_print_info,
957 acpi_battery_print_state,
958 acpi_battery_print_alarm,
959};
960
961static int acpi_battery_read(int fid, struct seq_file *seq)
962{
963 struct acpi_battery *battery = seq->private;
9e50bc14 964 int result = acpi_battery_update(battery, false);
3a670cc7
LT
965 return acpi_print_funcs[fid](seq, result);
966}
967
968#define DECLARE_FILE_FUNCTIONS(_name) \
969static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \
970{ \
971 return acpi_battery_read(_name##_tag, seq); \
972} \
973static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \
974{ \
975 return single_open(file, acpi_battery_read_##_name, PDE_DATA(inode)); \
976}
977
978DECLARE_FILE_FUNCTIONS(info);
979DECLARE_FILE_FUNCTIONS(state);
980DECLARE_FILE_FUNCTIONS(alarm);
981
982#undef DECLARE_FILE_FUNCTIONS
983
984#define FILE_DESCRIPTION_RO(_name) \
985 { \
986 .name = __stringify(_name), \
987 .mode = S_IRUGO, \
988 .ops = { \
989 .open = acpi_battery_##_name##_open_fs, \
990 .read = seq_read, \
991 .llseek = seq_lseek, \
992 .release = single_release, \
993 .owner = THIS_MODULE, \
994 }, \
995 }
996
997#define FILE_DESCRIPTION_RW(_name) \
998 { \
999 .name = __stringify(_name), \
1000 .mode = S_IFREG | S_IRUGO | S_IWUSR, \
1001 .ops = { \
1002 .open = acpi_battery_##_name##_open_fs, \
1003 .read = seq_read, \
1004 .llseek = seq_lseek, \
1005 .write = acpi_battery_write_##_name, \
1006 .release = single_release, \
1007 .owner = THIS_MODULE, \
1008 }, \
1009 }
1010
1011static const struct battery_file {
1012 struct file_operations ops;
1013 umode_t mode;
1014 const char *name;
1015} acpi_battery_file[] = {
1016 FILE_DESCRIPTION_RO(info),
1017 FILE_DESCRIPTION_RO(state),
1018 FILE_DESCRIPTION_RW(alarm),
1019};
1020
1021#undef FILE_DESCRIPTION_RO
1022#undef FILE_DESCRIPTION_RW
1023
1024static int acpi_battery_add_fs(struct acpi_device *device)
1025{
1026 struct proc_dir_entry *entry = NULL;
1027 int i;
1028
1029 printk(KERN_WARNING PREFIX "Deprecated procfs I/F for battery is loaded,"
1030 " please retry with CONFIG_ACPI_PROCFS_POWER cleared\n");
1031 if (!acpi_device_dir(device)) {
1032 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
1033 acpi_battery_dir);
1034 if (!acpi_device_dir(device))
1035 return -ENODEV;
1036 }
1037
1038 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
1039 entry = proc_create_data(acpi_battery_file[i].name,
1040 acpi_battery_file[i].mode,
1041 acpi_device_dir(device),
1042 &acpi_battery_file[i].ops,
1043 acpi_driver_data(device));
1044 if (!entry)
1045 return -ENODEV;
1046 }
1047 return 0;
1048}
1049
1050static void acpi_battery_remove_fs(struct acpi_device *device)
1051{
1052 int i;
1053 if (!acpi_device_dir(device))
1054 return;
1055 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i)
1056 remove_proc_entry(acpi_battery_file[i].name,
1057 acpi_device_dir(device));
1058
1059 remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
1060 acpi_device_dir(device) = NULL;
1061}
1062
1063#endif
1064
1da177e4
LT
1065/* --------------------------------------------------------------------------
1066 Driver Interface
1067 -------------------------------------------------------------------------- */
1068
d9406691 1069static void acpi_battery_notify(struct acpi_device *device, u32 event)
1da177e4 1070{
d9406691 1071 struct acpi_battery *battery = acpi_driver_data(device);
297d716f 1072 struct power_supply *old;
d9406691 1073
1da177e4 1074 if (!battery)
d550d98d 1075 return;
297d716f 1076 old = battery->bat;
f43691c6
AM
1077 /*
1078 * On Acer Aspire V5-573G notifications are sometimes triggered too
1079 * early. For example, when AC is unplugged and notification is
1080 * triggered, battery state is still reported as "Full", and changes to
1081 * "Discharging" only after short delay, without any notification.
1082 */
1083 if (battery_notification_delay_ms > 0)
1084 msleep(battery_notification_delay_ms);
da8aeb92
RW
1085 if (event == ACPI_BATTERY_NOTIFY_INFO)
1086 acpi_battery_refresh(battery);
9e50bc14 1087 acpi_battery_update(battery, false);
f1d4661a 1088 acpi_bus_generate_netlink_event(device->pnp.device_class,
0794469d 1089 dev_name(&device->dev), event,
9ea7d575 1090 acpi_battery_present(battery));
411e0f77 1091 acpi_notifier_call_chain(device, event, acpi_battery_present(battery));
2345baf4 1092 /* acpi_battery_update could remove power_supply object */
297d716f
KK
1093 if (old && battery->bat)
1094 power_supply_changed(battery->bat);
1da177e4
LT
1095}
1096
25be5821
KM
1097static int battery_notify(struct notifier_block *nb,
1098 unsigned long mode, void *_unused)
1099{
1100 struct acpi_battery *battery = container_of(nb, struct acpi_battery,
1101 pm_nb);
9e50bc14
LT
1102 int result;
1103
25be5821 1104 switch (mode) {
d5a5911b 1105 case PM_POST_HIBERNATION:
25be5821 1106 case PM_POST_SUSPEND:
9e50bc14
LT
1107 if (!acpi_battery_present(battery))
1108 return 0;
1109
31f7dc79 1110 if (!battery->bat) {
9e50bc14
LT
1111 result = acpi_battery_get_info(battery);
1112 if (result)
1113 return result;
1114
1115 result = sysfs_add_battery(battery);
1116 if (result)
1117 return result;
1118 } else
1119 acpi_battery_refresh(battery);
1120
1121 acpi_battery_init_alarm(battery);
1122 acpi_battery_get_state(battery);
25be5821
KM
1123 break;
1124 }
1125
1126 return 0;
1127}
1128
3f5dc08f
AM
1129static int battery_bix_broken_package_quirk(const struct dmi_system_id *d)
1130{
1131 battery_bix_broken_package = 1;
1132 return 0;
1133}
1134
f43691c6
AM
1135static int battery_notification_delay_quirk(const struct dmi_system_id *d)
1136{
1137 battery_notification_delay_ms = 1000;
1138 return 0;
1139}
1140
a90b4038
LT
1141static struct dmi_system_id bat_dmi_table[] = {
1142 {
3f5dc08f 1143 .callback = battery_bix_broken_package_quirk,
a90b4038
LT
1144 .ident = "NEC LZ750/LS",
1145 .matches = {
1146 DMI_MATCH(DMI_SYS_VENDOR, "NEC"),
1147 DMI_MATCH(DMI_PRODUCT_NAME, "PC-LZ750LS"),
1148 },
1149 },
f43691c6
AM
1150 {
1151 .callback = battery_notification_delay_quirk,
1152 .ident = "Acer Aspire V5-573G",
1153 .matches = {
1154 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
1155 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire V5-573G"),
1156 },
1157 },
a90b4038
LT
1158 {},
1159};
1160
75646e75
LT
1161/*
1162 * Some machines'(E,G Lenovo Z480) ECs are not stable
1163 * during boot up and this causes battery driver fails to be
1164 * probed due to failure of getting battery information
1165 * from EC sometimes. After several retries, the operation
1166 * may work. So add retry code here and 20ms sleep between
1167 * every retries.
1168 */
1169static int acpi_battery_update_retry(struct acpi_battery *battery)
1170{
1171 int retry, ret;
1172
1173 for (retry = 5; retry; retry--) {
1174 ret = acpi_battery_update(battery, false);
1175 if (!ret)
1176 break;
1177
1178 msleep(20);
1179 }
1180 return ret;
1181}
1182
4be44fcd 1183static int acpi_battery_add(struct acpi_device *device)
1da177e4 1184{
4be44fcd 1185 int result = 0;
4be44fcd 1186 struct acpi_battery *battery = NULL;
952c63e9 1187
1da177e4 1188 if (!device)
d550d98d 1189 return -EINVAL;
40e7fcb1
LT
1190
1191 if (device->dep_unmet)
1192 return -EPROBE_DEFER;
1193
36bcbec7 1194 battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
1da177e4 1195 if (!battery)
d550d98d 1196 return -ENOMEM;
145def84 1197 battery->device = device;
1da177e4
LT
1198 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
1199 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
db89b4f0 1200 device->driver_data = battery;
038fdea2 1201 mutex_init(&battery->lock);
69d94ec6 1202 mutex_init(&battery->sysfs_lock);
952c63e9 1203 if (acpi_has_method(battery->device->handle, "_BIX"))
c67fcd67 1204 set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
75646e75
LT
1205
1206 result = acpi_battery_update_retry(battery);
eb03cb02
SH
1207 if (result)
1208 goto fail;
75646e75 1209
3a670cc7
LT
1210#ifdef CONFIG_ACPI_PROCFS_POWER
1211 result = acpi_battery_add_fs(device);
1212#endif
1213 if (result) {
1214#ifdef CONFIG_ACPI_PROCFS_POWER
1215 acpi_battery_remove_fs(device);
1216#endif
1217 goto fail;
1218 }
25be5821 1219
e80bba4b
SH
1220 printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
1221 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
1222 device->status.battery_present ? "present" : "absent");
1223
25be5821
KM
1224 battery->pm_nb.notifier_call = battery_notify;
1225 register_pm_notifier(&battery->pm_nb);
1226
e0d1f09e
ZR
1227 device_init_wakeup(&device->dev, 1);
1228
d550d98d 1229 return result;
e80bba4b
SH
1230
1231fail:
1232 sysfs_remove_battery(battery);
1233 mutex_destroy(&battery->lock);
69d94ec6 1234 mutex_destroy(&battery->sysfs_lock);
e80bba4b
SH
1235 kfree(battery);
1236 return result;
1da177e4
LT
1237}
1238
51fac838 1239static int acpi_battery_remove(struct acpi_device *device)
1da177e4 1240{
4be44fcd 1241 struct acpi_battery *battery = NULL;
1da177e4 1242
1da177e4 1243 if (!device || !acpi_driver_data(device))
d550d98d 1244 return -EINVAL;
e0d1f09e 1245 device_init_wakeup(&device->dev, 0);
50dd0969 1246 battery = acpi_driver_data(device);
25be5821 1247 unregister_pm_notifier(&battery->pm_nb);
3a670cc7
LT
1248#ifdef CONFIG_ACPI_PROCFS_POWER
1249 acpi_battery_remove_fs(device);
1250#endif
508df92d 1251 sysfs_remove_battery(battery);
038fdea2 1252 mutex_destroy(&battery->lock);
69d94ec6 1253 mutex_destroy(&battery->sysfs_lock);
1da177e4 1254 kfree(battery);
d550d98d 1255 return 0;
1da177e4
LT
1256}
1257
90692404 1258#ifdef CONFIG_PM_SLEEP
34c4415a 1259/* this is needed to learn about changes made in suspended state */
a6f50dc8 1260static int acpi_battery_resume(struct device *dev)
34c4415a
JK
1261{
1262 struct acpi_battery *battery;
a6f50dc8
RW
1263
1264 if (!dev)
34c4415a 1265 return -EINVAL;
a6f50dc8
RW
1266
1267 battery = acpi_driver_data(to_acpi_device(dev));
1268 if (!battery)
1269 return -EINVAL;
1270
f1d4661a 1271 battery->update_time = 0;
9e50bc14 1272 acpi_battery_update(battery, true);
b6ce4083 1273 return 0;
34c4415a 1274}
7f6895c6
SK
1275#else
1276#define acpi_battery_resume NULL
90692404 1277#endif
34c4415a 1278
a6f50dc8
RW
1279static SIMPLE_DEV_PM_OPS(acpi_battery_pm, NULL, acpi_battery_resume);
1280
aa650bbd
AS
1281static struct acpi_driver acpi_battery_driver = {
1282 .name = "battery",
1283 .class = ACPI_BATTERY_CLASS,
1284 .ids = battery_device_ids,
d9406691 1285 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
aa650bbd
AS
1286 .ops = {
1287 .add = acpi_battery_add,
aa650bbd 1288 .remove = acpi_battery_remove,
d9406691 1289 .notify = acpi_battery_notify,
aa650bbd 1290 },
a6f50dc8 1291 .drv.pm = &acpi_battery_pm,
aa650bbd
AS
1292};
1293
b0cbc861 1294static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
1da177e4 1295{
479faaf0
LH
1296 int result;
1297
3f5dc08f 1298 dmi_check_system(bat_dmi_table);
479faaf0 1299
3a670cc7
LT
1300#ifdef CONFIG_ACPI_PROCFS_POWER
1301 acpi_battery_dir = acpi_lock_battery_dir();
1302 if (!acpi_battery_dir)
1303 return;
1304#endif
479faaf0 1305 result = acpi_bus_register_driver(&acpi_battery_driver);
3a670cc7 1306#ifdef CONFIG_ACPI_PROCFS_POWER
479faaf0 1307 if (result < 0)
3a670cc7
LT
1308 acpi_unlock_battery_dir(acpi_battery_dir);
1309#endif
0f66af53
AV
1310}
1311
1312static int __init acpi_battery_init(void)
1313{
e234b074
LH
1314 if (acpi_disabled)
1315 return -ENODEV;
1316
eca21d91 1317 async_cookie = async_schedule(acpi_battery_init_async, NULL);
d550d98d 1318 return 0;
1da177e4
LT
1319}
1320
4be44fcd 1321static void __exit acpi_battery_exit(void)
1da177e4 1322{
eca21d91 1323 async_synchronize_cookie(async_cookie);
1da177e4 1324 acpi_bus_unregister_driver(&acpi_battery_driver);
3a670cc7
LT
1325#ifdef CONFIG_ACPI_PROCFS_POWER
1326 acpi_unlock_battery_dir(acpi_battery_dir);
1327#endif
1da177e4
LT
1328}
1329
1da177e4
LT
1330module_init(acpi_battery_init);
1331module_exit(acpi_battery_exit);
This page took 0.746564 seconds and 5 git commands to generate.