1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * battery.c - ACPI Battery Driver (Revision: 2.0)
5 * Copyright (C) 2007 Alexey Starikovskiy <astarikovskiy@suse.de>
6 * Copyright (C) 2004-2007 Vladimir Lebedev <vladimir.p.lebedev@intel.com>
7 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
8 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/async.h>
14 #include <linux/delay.h>
15 #include <linux/dmi.h>
16 #include <linux/jiffies.h>
17 #include <linux/kernel.h>
18 #include <linux/list.h>
19 #include <linux/module.h>
20 #include <linux/mutex.h>
21 #include <linux/slab.h>
22 #include <linux/suspend.h>
23 #include <linux/types.h>
25 #include <asm/unaligned.h>
27 #ifdef CONFIG_ACPI_PROCFS_POWER
28 #include <linux/proc_fs.h>
29 #include <linux/seq_file.h>
30 #include <linux/uaccess.h>
33 #include <linux/acpi.h>
34 #include <linux/power_supply.h>
36 #include <acpi/battery.h>
38 #define PREFIX "ACPI: "
40 #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
41 #define ACPI_BATTERY_CAPACITY_VALID(capacity) \
42 ((capacity) != 0 && (capacity) != ACPI_BATTERY_VALUE_UNKNOWN)
44 #define ACPI_BATTERY_DEVICE_NAME "Battery"
46 /* Battery power unit: 0 means mW, 1 means mA */
47 #define ACPI_BATTERY_POWER_UNIT_MA 1
49 #define ACPI_BATTERY_STATE_DISCHARGING 0x1
50 #define ACPI_BATTERY_STATE_CHARGING 0x2
51 #define ACPI_BATTERY_STATE_CRITICAL 0x4
53 #define _COMPONENT ACPI_BATTERY_COMPONENT
55 ACPI_MODULE_NAME("battery");
57 MODULE_AUTHOR("Paul Diefenbaugh");
58 MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
59 MODULE_DESCRIPTION("ACPI Battery Driver");
60 MODULE_LICENSE("GPL");
62 static async_cookie_t async_cookie;
63 static bool battery_driver_registered;
64 static int battery_bix_broken_package;
65 static int battery_notification_delay_ms;
66 static int battery_ac_is_broken;
67 static int battery_check_pmic = 1;
68 static unsigned int cache_time = 1000;
69 module_param(cache_time, uint, 0644);
70 MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
72 #ifdef CONFIG_ACPI_PROCFS_POWER
73 extern struct proc_dir_entry *acpi_lock_battery_dir(void);
74 extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
77 static const struct acpi_device_id battery_device_ids[] = {
82 MODULE_DEVICE_TABLE(acpi, battery_device_ids);
84 /* Lists of PMIC ACPI HIDs with an (often better) native battery driver */
85 static const char * const acpi_battery_blacklist[] = {
86 "INT33F4", /* X-Powers AXP288 PMIC */
90 ACPI_BATTERY_ALARM_PRESENT,
91 ACPI_BATTERY_XINFO_PRESENT,
92 ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY,
93 /* On Lenovo Thinkpad models from 2010 and 2011, the power unit
94 switches between mWh and mAh depending on whether the system
95 is running on battery or not. When mAh is the unit, most
96 reported values are incorrect and need to be adjusted by
97 10000/design_voltage. Verified on x201, t410, t410s, and x220.
98 Pre-2010 and 2012 models appear to always report in mWh and
99 are thus unaffected (tested with t42, t61, t500, x200, x300,
100 and x230). Also, in mid-2012 Lenovo issued a BIOS update for
101 the 2011 models that fixes the issue (tested on x220 with a
102 post-1.29 BIOS), but as of Nov. 2012, no such update is
103 available for the 2010 models. */
104 ACPI_BATTERY_QUIRK_THINKPAD_MAH,
105 /* for batteries reporting current capacity with design capacity
106 * on a full charge, but showing degradation in full charge cap.
108 ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE,
111 struct acpi_battery {
113 struct mutex sysfs_lock;
114 struct power_supply *bat;
115 struct power_supply_desc bat_desc;
116 struct acpi_device *device;
117 struct notifier_block pm_nb;
118 struct list_head list;
119 unsigned long update_time;
125 int full_charge_capacity;
128 int design_capacity_warning;
129 int design_capacity_low;
131 int measurement_accuracy;
132 int max_sampling_time;
133 int min_sampling_time;
134 int max_averaging_interval;
135 int min_averaging_interval;
136 int capacity_granularity_1;
137 int capacity_granularity_2;
139 char model_number[32];
140 char serial_number[32];
148 #define to_acpi_battery(x) power_supply_get_drvdata(x)
150 static inline int acpi_battery_present(struct acpi_battery *battery)
152 return battery->device->status.battery_present;
155 static int acpi_battery_technology(struct acpi_battery *battery)
157 if (!strcasecmp("NiCd", battery->type))
158 return POWER_SUPPLY_TECHNOLOGY_NiCd;
159 if (!strcasecmp("NiMH", battery->type))
160 return POWER_SUPPLY_TECHNOLOGY_NiMH;
161 if (!strcasecmp("LION", battery->type))
162 return POWER_SUPPLY_TECHNOLOGY_LION;
163 if (!strncasecmp("LI-ION", battery->type, 6))
164 return POWER_SUPPLY_TECHNOLOGY_LION;
165 if (!strcasecmp("LiP", battery->type))
166 return POWER_SUPPLY_TECHNOLOGY_LIPO;
167 return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
170 static int acpi_battery_get_state(struct acpi_battery *battery);
172 static int acpi_battery_is_charged(struct acpi_battery *battery)
174 /* charging, discharging or critical low */
175 if (battery->state != 0)
178 /* battery not reporting charge */
179 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
180 battery->capacity_now == 0)
183 /* good batteries update full_charge as the batteries degrade */
184 if (battery->full_charge_capacity == battery->capacity_now)
187 /* fallback to using design values for broken batteries */
188 if (battery->design_capacity == battery->capacity_now)
191 /* we don't do any sort of metric based on percentages */
195 static bool acpi_battery_is_degraded(struct acpi_battery *battery)
197 return ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity) &&
198 ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity) &&
199 battery->full_charge_capacity < battery->design_capacity;
202 static int acpi_battery_handle_discharging(struct acpi_battery *battery)
205 * Some devices wrongly report discharging if the battery's charge level
206 * was above the device's start charging threshold atm the AC adapter
207 * was plugged in and the device thus did not start a new charge cycle.
209 if ((battery_ac_is_broken || power_supply_is_system_supplied()) &&
210 battery->rate_now == 0)
211 return POWER_SUPPLY_STATUS_NOT_CHARGING;
213 return POWER_SUPPLY_STATUS_DISCHARGING;
216 static int acpi_battery_get_property(struct power_supply *psy,
217 enum power_supply_property psp,
218 union power_supply_propval *val)
220 int full_capacity = ACPI_BATTERY_VALUE_UNKNOWN, ret = 0;
221 struct acpi_battery *battery = to_acpi_battery(psy);
223 if (acpi_battery_present(battery)) {
224 /* run battery update only if it is present */
225 acpi_battery_get_state(battery);
226 } else if (psp != POWER_SUPPLY_PROP_PRESENT)
229 case POWER_SUPPLY_PROP_STATUS:
230 if (battery->state & ACPI_BATTERY_STATE_DISCHARGING)
231 val->intval = acpi_battery_handle_discharging(battery);
232 else if (battery->state & ACPI_BATTERY_STATE_CHARGING)
233 val->intval = POWER_SUPPLY_STATUS_CHARGING;
234 else if (acpi_battery_is_charged(battery))
235 val->intval = POWER_SUPPLY_STATUS_FULL;
237 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
239 case POWER_SUPPLY_PROP_PRESENT:
240 val->intval = acpi_battery_present(battery);
242 case POWER_SUPPLY_PROP_TECHNOLOGY:
243 val->intval = acpi_battery_technology(battery);
245 case POWER_SUPPLY_PROP_CYCLE_COUNT:
246 val->intval = battery->cycle_count;
248 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
249 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
252 val->intval = battery->design_voltage * 1000;
254 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
255 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
258 val->intval = battery->voltage_now * 1000;
260 case POWER_SUPPLY_PROP_CURRENT_NOW:
261 case POWER_SUPPLY_PROP_POWER_NOW:
262 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
265 val->intval = battery->rate_now * 1000;
267 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
268 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
269 if (!ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity))
272 val->intval = battery->design_capacity * 1000;
274 case POWER_SUPPLY_PROP_CHARGE_FULL:
275 case POWER_SUPPLY_PROP_ENERGY_FULL:
276 if (!ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity))
279 val->intval = battery->full_charge_capacity * 1000;
281 case POWER_SUPPLY_PROP_CHARGE_NOW:
282 case POWER_SUPPLY_PROP_ENERGY_NOW:
283 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
286 val->intval = battery->capacity_now * 1000;
288 case POWER_SUPPLY_PROP_CAPACITY:
289 if (ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity))
290 full_capacity = battery->full_charge_capacity;
291 else if (ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity))
292 full_capacity = battery->design_capacity;
294 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
295 full_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
298 val->intval = battery->capacity_now * 100/
301 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
302 if (battery->state & ACPI_BATTERY_STATE_CRITICAL)
303 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
304 else if (test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags) &&
305 (battery->capacity_now <= battery->alarm))
306 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
307 else if (acpi_battery_is_charged(battery))
308 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
310 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
312 case POWER_SUPPLY_PROP_MODEL_NAME:
313 val->strval = battery->model_number;
315 case POWER_SUPPLY_PROP_MANUFACTURER:
316 val->strval = battery->oem_info;
318 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
319 val->strval = battery->serial_number;
327 static enum power_supply_property charge_battery_props[] = {
328 POWER_SUPPLY_PROP_STATUS,
329 POWER_SUPPLY_PROP_PRESENT,
330 POWER_SUPPLY_PROP_TECHNOLOGY,
331 POWER_SUPPLY_PROP_CYCLE_COUNT,
332 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
333 POWER_SUPPLY_PROP_VOLTAGE_NOW,
334 POWER_SUPPLY_PROP_CURRENT_NOW,
335 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
336 POWER_SUPPLY_PROP_CHARGE_FULL,
337 POWER_SUPPLY_PROP_CHARGE_NOW,
338 POWER_SUPPLY_PROP_CAPACITY,
339 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
340 POWER_SUPPLY_PROP_MODEL_NAME,
341 POWER_SUPPLY_PROP_MANUFACTURER,
342 POWER_SUPPLY_PROP_SERIAL_NUMBER,
345 static enum power_supply_property energy_battery_props[] = {
346 POWER_SUPPLY_PROP_STATUS,
347 POWER_SUPPLY_PROP_PRESENT,
348 POWER_SUPPLY_PROP_TECHNOLOGY,
349 POWER_SUPPLY_PROP_CYCLE_COUNT,
350 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
351 POWER_SUPPLY_PROP_VOLTAGE_NOW,
352 POWER_SUPPLY_PROP_POWER_NOW,
353 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
354 POWER_SUPPLY_PROP_ENERGY_FULL,
355 POWER_SUPPLY_PROP_ENERGY_NOW,
356 POWER_SUPPLY_PROP_CAPACITY,
357 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
358 POWER_SUPPLY_PROP_MODEL_NAME,
359 POWER_SUPPLY_PROP_MANUFACTURER,
360 POWER_SUPPLY_PROP_SERIAL_NUMBER,
363 static enum power_supply_property energy_battery_full_cap_broken_props[] = {
364 POWER_SUPPLY_PROP_STATUS,
365 POWER_SUPPLY_PROP_PRESENT,
366 POWER_SUPPLY_PROP_TECHNOLOGY,
367 POWER_SUPPLY_PROP_CYCLE_COUNT,
368 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
369 POWER_SUPPLY_PROP_VOLTAGE_NOW,
370 POWER_SUPPLY_PROP_POWER_NOW,
371 POWER_SUPPLY_PROP_ENERGY_NOW,
372 POWER_SUPPLY_PROP_MODEL_NAME,
373 POWER_SUPPLY_PROP_MANUFACTURER,
374 POWER_SUPPLY_PROP_SERIAL_NUMBER,
377 /* --------------------------------------------------------------------------
379 -------------------------------------------------------------------------- */
380 struct acpi_offsets {
381 size_t offset; /* offset inside struct acpi_sbs_battery */
382 u8 mode; /* int or string? */
385 static const struct acpi_offsets state_offsets[] = {
386 {offsetof(struct acpi_battery, state), 0},
387 {offsetof(struct acpi_battery, rate_now), 0},
388 {offsetof(struct acpi_battery, capacity_now), 0},
389 {offsetof(struct acpi_battery, voltage_now), 0},
392 static const struct acpi_offsets info_offsets[] = {
393 {offsetof(struct acpi_battery, power_unit), 0},
394 {offsetof(struct acpi_battery, design_capacity), 0},
395 {offsetof(struct acpi_battery, full_charge_capacity), 0},
396 {offsetof(struct acpi_battery, technology), 0},
397 {offsetof(struct acpi_battery, design_voltage), 0},
398 {offsetof(struct acpi_battery, design_capacity_warning), 0},
399 {offsetof(struct acpi_battery, design_capacity_low), 0},
400 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
401 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
402 {offsetof(struct acpi_battery, model_number), 1},
403 {offsetof(struct acpi_battery, serial_number), 1},
404 {offsetof(struct acpi_battery, type), 1},
405 {offsetof(struct acpi_battery, oem_info), 1},
408 static const struct acpi_offsets extended_info_offsets[] = {
409 {offsetof(struct acpi_battery, revision), 0},
410 {offsetof(struct acpi_battery, power_unit), 0},
411 {offsetof(struct acpi_battery, design_capacity), 0},
412 {offsetof(struct acpi_battery, full_charge_capacity), 0},
413 {offsetof(struct acpi_battery, technology), 0},
414 {offsetof(struct acpi_battery, design_voltage), 0},
415 {offsetof(struct acpi_battery, design_capacity_warning), 0},
416 {offsetof(struct acpi_battery, design_capacity_low), 0},
417 {offsetof(struct acpi_battery, cycle_count), 0},
418 {offsetof(struct acpi_battery, measurement_accuracy), 0},
419 {offsetof(struct acpi_battery, max_sampling_time), 0},
420 {offsetof(struct acpi_battery, min_sampling_time), 0},
421 {offsetof(struct acpi_battery, max_averaging_interval), 0},
422 {offsetof(struct acpi_battery, min_averaging_interval), 0},
423 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
424 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
425 {offsetof(struct acpi_battery, model_number), 1},
426 {offsetof(struct acpi_battery, serial_number), 1},
427 {offsetof(struct acpi_battery, type), 1},
428 {offsetof(struct acpi_battery, oem_info), 1},
431 static int extract_package(struct acpi_battery *battery,
432 union acpi_object *package,
433 const struct acpi_offsets *offsets, int num)
436 union acpi_object *element;
437 if (package->type != ACPI_TYPE_PACKAGE)
439 for (i = 0; i < num; ++i) {
440 if (package->package.count <= i)
442 element = &package->package.elements[i];
443 if (offsets[i].mode) {
444 u8 *ptr = (u8 *)battery + offsets[i].offset;
445 if (element->type == ACPI_TYPE_STRING ||
446 element->type == ACPI_TYPE_BUFFER)
447 strncpy(ptr, element->string.pointer, 32);
448 else if (element->type == ACPI_TYPE_INTEGER) {
449 strncpy(ptr, (u8 *)&element->integer.value,
451 ptr[sizeof(u64)] = 0;
453 *ptr = 0; /* don't have value */
455 int *x = (int *)((u8 *)battery + offsets[i].offset);
456 *x = (element->type == ACPI_TYPE_INTEGER) ?
457 element->integer.value : -1;
463 static int acpi_battery_get_status(struct acpi_battery *battery)
465 if (acpi_bus_get_status(battery->device)) {
466 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
473 static int extract_battery_info(const int use_bix,
474 struct acpi_battery *battery,
475 const struct acpi_buffer *buffer)
477 int result = -EFAULT;
479 if (use_bix && battery_bix_broken_package)
480 result = extract_package(battery, buffer->pointer,
481 extended_info_offsets + 1,
482 ARRAY_SIZE(extended_info_offsets) - 1);
484 result = extract_package(battery, buffer->pointer,
485 extended_info_offsets,
486 ARRAY_SIZE(extended_info_offsets));
488 result = extract_package(battery, buffer->pointer,
489 info_offsets, ARRAY_SIZE(info_offsets));
490 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
491 battery->full_charge_capacity = battery->design_capacity;
492 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) &&
493 battery->power_unit && battery->design_voltage) {
494 battery->design_capacity = battery->design_capacity *
495 10000 / battery->design_voltage;
496 battery->full_charge_capacity = battery->full_charge_capacity *
497 10000 / battery->design_voltage;
498 battery->design_capacity_warning =
499 battery->design_capacity_warning *
500 10000 / battery->design_voltage;
501 /* Curiously, design_capacity_low, unlike the rest of them,
503 /* capacity_granularity_* equal 1 on the systems tested, so
504 it's impossible to tell if they would need an adjustment
505 or not if their values were higher. */
507 if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags) &&
508 battery->capacity_now > battery->full_charge_capacity)
509 battery->capacity_now = battery->full_charge_capacity;
514 static int acpi_battery_get_info(struct acpi_battery *battery)
516 const int xinfo = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
518 int result = -ENODEV;
520 if (!acpi_battery_present(battery))
524 for (use_bix = xinfo ? 1 : 0; use_bix >= 0; use_bix--) {
525 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
526 acpi_status status = AE_ERROR;
528 mutex_lock(&battery->lock);
529 status = acpi_evaluate_object(battery->device->handle,
530 use_bix ? "_BIX":"_BIF",
532 mutex_unlock(&battery->lock);
534 if (ACPI_FAILURE(status)) {
535 ACPI_EXCEPTION((AE_INFO, status, "Evaluating %s",
536 use_bix ? "_BIX":"_BIF"));
538 result = extract_battery_info(use_bix,
542 kfree(buffer.pointer);
547 if (!result && !use_bix && xinfo)
548 pr_warn(FW_BUG "The _BIX method is broken, using _BIF.\n");
553 static int acpi_battery_get_state(struct acpi_battery *battery)
556 acpi_status status = 0;
557 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
559 if (!acpi_battery_present(battery))
562 if (battery->update_time &&
563 time_before(jiffies, battery->update_time +
564 msecs_to_jiffies(cache_time)))
567 mutex_lock(&battery->lock);
568 status = acpi_evaluate_object(battery->device->handle, "_BST",
570 mutex_unlock(&battery->lock);
572 if (ACPI_FAILURE(status)) {
573 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
577 result = extract_package(battery, buffer.pointer,
578 state_offsets, ARRAY_SIZE(state_offsets));
579 battery->update_time = jiffies;
580 kfree(buffer.pointer);
582 /* For buggy DSDTs that report negative 16-bit values for either
583 * charging or discharging current and/or report 0 as 65536
586 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA &&
587 battery->rate_now != ACPI_BATTERY_VALUE_UNKNOWN &&
588 (s16)(battery->rate_now) < 0) {
589 battery->rate_now = abs((s16)battery->rate_now);
590 pr_warn_once(FW_BUG "battery: (dis)charge rate invalid.\n");
593 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)
594 && battery->capacity_now >= 0 && battery->capacity_now <= 100)
595 battery->capacity_now = (battery->capacity_now *
596 battery->full_charge_capacity) / 100;
597 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) &&
598 battery->power_unit && battery->design_voltage) {
599 battery->capacity_now = battery->capacity_now *
600 10000 / battery->design_voltage;
602 if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags) &&
603 battery->capacity_now > battery->full_charge_capacity)
604 battery->capacity_now = battery->full_charge_capacity;
609 static int acpi_battery_set_alarm(struct acpi_battery *battery)
611 acpi_status status = 0;
613 if (!acpi_battery_present(battery) ||
614 !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags))
617 mutex_lock(&battery->lock);
618 status = acpi_execute_simple_method(battery->device->handle, "_BTP",
620 mutex_unlock(&battery->lock);
622 if (ACPI_FAILURE(status))
625 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm));
629 static int acpi_battery_init_alarm(struct acpi_battery *battery)
631 /* See if alarms are supported, and if so, set default */
632 if (!acpi_has_method(battery->device->handle, "_BTP")) {
633 clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
636 set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
638 battery->alarm = battery->design_capacity_warning;
639 return acpi_battery_set_alarm(battery);
642 static ssize_t acpi_battery_alarm_show(struct device *dev,
643 struct device_attribute *attr,
646 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
647 return sprintf(buf, "%d\n", battery->alarm * 1000);
650 static ssize_t acpi_battery_alarm_store(struct device *dev,
651 struct device_attribute *attr,
652 const char *buf, size_t count)
655 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
656 if (sscanf(buf, "%lu\n", &x) == 1)
657 battery->alarm = x/1000;
658 if (acpi_battery_present(battery))
659 acpi_battery_set_alarm(battery);
663 static const struct device_attribute alarm_attr = {
664 .attr = {.name = "alarm", .mode = 0644},
665 .show = acpi_battery_alarm_show,
666 .store = acpi_battery_alarm_store,
670 * The Battery Hooking API
672 * This API is used inside other drivers that need to expose
673 * platform-specific behaviour within the generic driver in a
678 static LIST_HEAD(acpi_battery_list);
679 static LIST_HEAD(battery_hook_list);
680 static DEFINE_MUTEX(hook_mutex);
682 static void __battery_hook_unregister(struct acpi_battery_hook *hook, int lock)
684 struct acpi_battery *battery;
686 * In order to remove a hook, we first need to
687 * de-register all the batteries that are registered.
690 mutex_lock(&hook_mutex);
691 list_for_each_entry(battery, &acpi_battery_list, list) {
692 hook->remove_battery(battery->bat);
694 list_del(&hook->list);
696 mutex_unlock(&hook_mutex);
697 pr_info("extension unregistered: %s\n", hook->name);
700 void battery_hook_unregister(struct acpi_battery_hook *hook)
702 __battery_hook_unregister(hook, 1);
704 EXPORT_SYMBOL_GPL(battery_hook_unregister);
706 void battery_hook_register(struct acpi_battery_hook *hook)
708 struct acpi_battery *battery;
710 mutex_lock(&hook_mutex);
711 INIT_LIST_HEAD(&hook->list);
712 list_add(&hook->list, &battery_hook_list);
714 * Now that the driver is registered, we need
715 * to notify the hook that a battery is available
716 * for each battery, so that the driver may add
719 list_for_each_entry(battery, &acpi_battery_list, list) {
720 if (hook->add_battery(battery->bat)) {
722 * If a add-battery returns non-zero,
723 * the registration of the extension has failed,
724 * and we will not add it to the list of loaded
727 pr_err("extension failed to load: %s", hook->name);
728 __battery_hook_unregister(hook, 0);
732 pr_info("new extension: %s\n", hook->name);
734 mutex_unlock(&hook_mutex);
736 EXPORT_SYMBOL_GPL(battery_hook_register);
739 * This function gets called right after the battery sysfs
740 * attributes have been added, so that the drivers that
741 * define custom sysfs attributes can add their own.
743 static void battery_hook_add_battery(struct acpi_battery *battery)
745 struct acpi_battery_hook *hook_node, *tmp;
747 mutex_lock(&hook_mutex);
748 INIT_LIST_HEAD(&battery->list);
749 list_add(&battery->list, &acpi_battery_list);
751 * Since we added a new battery to the list, we need to
752 * iterate over the hooks and call add_battery for each
753 * hook that was registered. This usually happens
754 * when a battery gets hotplugged or initialized
755 * during the battery module initialization.
757 list_for_each_entry_safe(hook_node, tmp, &battery_hook_list, list) {
758 if (hook_node->add_battery(battery->bat)) {
760 * The notification of the extensions has failed, to
761 * prevent further errors we will unload the extension.
763 pr_err("error in extension, unloading: %s",
765 __battery_hook_unregister(hook_node, 0);
768 mutex_unlock(&hook_mutex);
771 static void battery_hook_remove_battery(struct acpi_battery *battery)
773 struct acpi_battery_hook *hook;
775 mutex_lock(&hook_mutex);
777 * Before removing the hook, we need to remove all
778 * custom attributes from the battery.
780 list_for_each_entry(hook, &battery_hook_list, list) {
781 hook->remove_battery(battery->bat);
783 /* Then, just remove the battery from the list */
784 list_del(&battery->list);
785 mutex_unlock(&hook_mutex);
788 static void __exit battery_hook_exit(void)
790 struct acpi_battery_hook *hook;
791 struct acpi_battery_hook *ptr;
793 * At this point, the acpi_bus_unregister_driver()
794 * has called remove for all batteries. We just
795 * need to remove the hooks.
797 list_for_each_entry_safe(hook, ptr, &battery_hook_list, list) {
798 __battery_hook_unregister(hook, 1);
800 mutex_destroy(&hook_mutex);
803 static int sysfs_add_battery(struct acpi_battery *battery)
805 struct power_supply_config psy_cfg = { .drv_data = battery, };
807 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) {
808 battery->bat_desc.properties = charge_battery_props;
809 battery->bat_desc.num_properties =
810 ARRAY_SIZE(charge_battery_props);
811 } else if (!ACPI_BATTERY_CAPACITY_VALID(
812 battery->full_charge_capacity)) {
813 battery->bat_desc.properties =
814 energy_battery_full_cap_broken_props;
815 battery->bat_desc.num_properties =
816 ARRAY_SIZE(energy_battery_full_cap_broken_props);
818 battery->bat_desc.properties = energy_battery_props;
819 battery->bat_desc.num_properties =
820 ARRAY_SIZE(energy_battery_props);
823 battery->bat_desc.name = acpi_device_bid(battery->device);
824 battery->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY;
825 battery->bat_desc.get_property = acpi_battery_get_property;
827 battery->bat = power_supply_register_no_ws(&battery->device->dev,
828 &battery->bat_desc, &psy_cfg);
830 if (IS_ERR(battery->bat)) {
831 int result = PTR_ERR(battery->bat);
836 battery_hook_add_battery(battery);
837 return device_create_file(&battery->bat->dev, &alarm_attr);
840 static void sysfs_remove_battery(struct acpi_battery *battery)
842 mutex_lock(&battery->sysfs_lock);
844 mutex_unlock(&battery->sysfs_lock);
847 battery_hook_remove_battery(battery);
848 device_remove_file(&battery->bat->dev, &alarm_attr);
849 power_supply_unregister(battery->bat);
851 mutex_unlock(&battery->sysfs_lock);
854 static void find_battery(const struct dmi_header *dm, void *private)
856 struct acpi_battery *battery = (struct acpi_battery *)private;
857 /* Note: the hardcoded offsets below have been extracted from
858 the source code of dmidecode. */
859 if (dm->type == DMI_ENTRY_PORTABLE_BATTERY && dm->length >= 8) {
860 const u8 *dmi_data = (const u8 *)(dm + 1);
861 int dmi_capacity = get_unaligned((const u16 *)(dmi_data + 6));
862 if (dm->length >= 18)
863 dmi_capacity *= dmi_data[17];
864 if (battery->design_capacity * battery->design_voltage / 1000
866 battery->design_capacity * 10 == dmi_capacity)
867 set_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH,
873 * According to the ACPI spec, some kinds of primary batteries can
874 * report percentage battery remaining capacity directly to OS.
875 * In this case, it reports the Last Full Charged Capacity == 100
876 * and BatteryPresentRate == 0xFFFFFFFF.
878 * Now we found some battery reports percentage remaining capacity
879 * even if it's rechargeable.
880 * https://bugzilla.kernel.org/show_bug.cgi?id=15979
882 * Handle this correctly so that they won't break userspace.
884 static void acpi_battery_quirks(struct acpi_battery *battery)
886 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
889 if (battery->full_charge_capacity == 100 &&
890 battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN &&
891 battery->capacity_now >= 0 && battery->capacity_now <= 100) {
892 set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags);
893 battery->full_charge_capacity = battery->design_capacity;
894 battery->capacity_now = (battery->capacity_now *
895 battery->full_charge_capacity) / 100;
898 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags))
901 if (battery->power_unit && dmi_name_in_vendors("LENOVO")) {
903 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
904 if (s && !strncasecmp(s, "ThinkPad", 8)) {
905 dmi_walk(find_battery, battery);
906 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH,
908 battery->design_voltage) {
909 battery->design_capacity =
910 battery->design_capacity *
911 10000 / battery->design_voltage;
912 battery->full_charge_capacity =
913 battery->full_charge_capacity *
914 10000 / battery->design_voltage;
915 battery->design_capacity_warning =
916 battery->design_capacity_warning *
917 10000 / battery->design_voltage;
918 battery->capacity_now = battery->capacity_now *
919 10000 / battery->design_voltage;
924 if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags))
927 if (acpi_battery_is_degraded(battery) &&
928 battery->capacity_now > battery->full_charge_capacity) {
929 set_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags);
930 battery->capacity_now = battery->full_charge_capacity;
934 static int acpi_battery_update(struct acpi_battery *battery, bool resume)
936 int result = acpi_battery_get_status(battery);
941 if (!acpi_battery_present(battery)) {
942 sysfs_remove_battery(battery);
943 battery->update_time = 0;
950 if (!battery->update_time) {
951 result = acpi_battery_get_info(battery);
954 acpi_battery_init_alarm(battery);
957 result = acpi_battery_get_state(battery);
960 acpi_battery_quirks(battery);
963 result = sysfs_add_battery(battery);
969 * Wakeup the system if battery is critical low
970 * or lower than the alarm level
972 if ((battery->state & ACPI_BATTERY_STATE_CRITICAL) ||
973 (test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags) &&
974 (battery->capacity_now <= battery->alarm)))
975 acpi_pm_wakeup_event(&battery->device->dev);
980 static void acpi_battery_refresh(struct acpi_battery *battery)
987 power_unit = battery->power_unit;
989 acpi_battery_get_info(battery);
991 if (power_unit == battery->power_unit)
994 /* The battery has changed its reporting units. */
995 sysfs_remove_battery(battery);
996 sysfs_add_battery(battery);
999 /* --------------------------------------------------------------------------
1000 FS Interface (/proc)
1001 -------------------------------------------------------------------------- */
1003 #ifdef CONFIG_ACPI_PROCFS_POWER
1004 static struct proc_dir_entry *acpi_battery_dir;
1006 static const char *acpi_battery_units(const struct acpi_battery *battery)
1008 return (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) ?
1012 static int acpi_battery_info_proc_show(struct seq_file *seq, void *offset)
1014 struct acpi_battery *battery = seq->private;
1015 int result = acpi_battery_update(battery, false);
1020 seq_printf(seq, "present: %s\n",
1021 acpi_battery_present(battery) ? "yes" : "no");
1022 if (!acpi_battery_present(battery))
1024 if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
1025 seq_printf(seq, "design capacity: unknown\n");
1027 seq_printf(seq, "design capacity: %d %sh\n",
1028 battery->design_capacity,
1029 acpi_battery_units(battery));
1031 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
1032 seq_printf(seq, "last full capacity: unknown\n");
1034 seq_printf(seq, "last full capacity: %d %sh\n",
1035 battery->full_charge_capacity,
1036 acpi_battery_units(battery));
1038 seq_printf(seq, "battery technology: %srechargeable\n",
1039 battery->technology ? "" : "non-");
1041 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
1042 seq_printf(seq, "design voltage: unknown\n");
1044 seq_printf(seq, "design voltage: %d mV\n",
1045 battery->design_voltage);
1046 seq_printf(seq, "design capacity warning: %d %sh\n",
1047 battery->design_capacity_warning,
1048 acpi_battery_units(battery));
1049 seq_printf(seq, "design capacity low: %d %sh\n",
1050 battery->design_capacity_low,
1051 acpi_battery_units(battery));
1052 seq_printf(seq, "cycle count: %i\n", battery->cycle_count);
1053 seq_printf(seq, "capacity granularity 1: %d %sh\n",
1054 battery->capacity_granularity_1,
1055 acpi_battery_units(battery));
1056 seq_printf(seq, "capacity granularity 2: %d %sh\n",
1057 battery->capacity_granularity_2,
1058 acpi_battery_units(battery));
1059 seq_printf(seq, "model number: %s\n", battery->model_number);
1060 seq_printf(seq, "serial number: %s\n", battery->serial_number);
1061 seq_printf(seq, "battery type: %s\n", battery->type);
1062 seq_printf(seq, "OEM info: %s\n", battery->oem_info);
1065 seq_printf(seq, "ERROR: Unable to read battery info\n");
1069 static int acpi_battery_state_proc_show(struct seq_file *seq, void *offset)
1071 struct acpi_battery *battery = seq->private;
1072 int result = acpi_battery_update(battery, false);
1077 seq_printf(seq, "present: %s\n",
1078 acpi_battery_present(battery) ? "yes" : "no");
1079 if (!acpi_battery_present(battery))
1082 seq_printf(seq, "capacity state: %s\n",
1083 (battery->state & 0x04) ? "critical" : "ok");
1084 if ((battery->state & 0x01) && (battery->state & 0x02))
1086 "charging state: charging/discharging\n");
1087 else if (battery->state & 0x01)
1088 seq_printf(seq, "charging state: discharging\n");
1089 else if (battery->state & 0x02)
1090 seq_printf(seq, "charging state: charging\n");
1092 seq_printf(seq, "charging state: charged\n");
1094 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
1095 seq_printf(seq, "present rate: unknown\n");
1097 seq_printf(seq, "present rate: %d %s\n",
1098 battery->rate_now, acpi_battery_units(battery));
1100 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
1101 seq_printf(seq, "remaining capacity: unknown\n");
1103 seq_printf(seq, "remaining capacity: %d %sh\n",
1104 battery->capacity_now, acpi_battery_units(battery));
1105 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
1106 seq_printf(seq, "present voltage: unknown\n");
1108 seq_printf(seq, "present voltage: %d mV\n",
1109 battery->voltage_now);
1112 seq_printf(seq, "ERROR: Unable to read battery state\n");
1117 static int acpi_battery_alarm_proc_show(struct seq_file *seq, void *offset)
1119 struct acpi_battery *battery = seq->private;
1120 int result = acpi_battery_update(battery, false);
1125 if (!acpi_battery_present(battery)) {
1126 seq_printf(seq, "present: no\n");
1129 seq_printf(seq, "alarm: ");
1130 if (battery->alarm) {
1131 seq_printf(seq, "%u %sh\n", battery->alarm,
1132 acpi_battery_units(battery));
1134 seq_printf(seq, "unsupported\n");
1138 seq_printf(seq, "ERROR: Unable to read battery alarm\n");
1142 static ssize_t acpi_battery_write_alarm(struct file *file,
1143 const char __user * buffer,
1144 size_t count, loff_t * ppos)
1147 char alarm_string[12] = { '\0' };
1148 struct seq_file *m = file->private_data;
1149 struct acpi_battery *battery = m->private;
1151 if (!battery || (count > sizeof(alarm_string) - 1))
1153 if (!acpi_battery_present(battery)) {
1157 if (copy_from_user(alarm_string, buffer, count)) {
1161 alarm_string[count] = '\0';
1162 if (kstrtoint(alarm_string, 0, &battery->alarm)) {
1166 result = acpi_battery_set_alarm(battery);
1173 static int acpi_battery_alarm_proc_open(struct inode *inode, struct file *file)
1175 return single_open(file, acpi_battery_alarm_proc_show, PDE_DATA(inode));
1178 static const struct file_operations acpi_battery_alarm_fops = {
1179 .owner = THIS_MODULE,
1180 .open = acpi_battery_alarm_proc_open,
1182 .write = acpi_battery_write_alarm,
1183 .llseek = seq_lseek,
1184 .release = single_release,
1187 static int acpi_battery_add_fs(struct acpi_device *device)
1189 pr_warn(PREFIX "Deprecated procfs I/F for battery is loaded, please retry with CONFIG_ACPI_PROCFS_POWER cleared\n");
1190 if (!acpi_device_dir(device)) {
1191 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
1193 if (!acpi_device_dir(device))
1197 if (!proc_create_single_data("info", S_IRUGO, acpi_device_dir(device),
1198 acpi_battery_info_proc_show, acpi_driver_data(device)))
1200 if (!proc_create_single_data("state", S_IRUGO, acpi_device_dir(device),
1201 acpi_battery_state_proc_show, acpi_driver_data(device)))
1203 if (!proc_create_data("alarm", S_IFREG | S_IRUGO | S_IWUSR,
1204 acpi_device_dir(device), &acpi_battery_alarm_fops,
1205 acpi_driver_data(device)))
1210 static void acpi_battery_remove_fs(struct acpi_device *device)
1212 if (!acpi_device_dir(device))
1214 remove_proc_subtree(acpi_device_bid(device), acpi_battery_dir);
1215 acpi_device_dir(device) = NULL;
1220 /* --------------------------------------------------------------------------
1222 -------------------------------------------------------------------------- */
1224 static void acpi_battery_notify(struct acpi_device *device, u32 event)
1226 struct acpi_battery *battery = acpi_driver_data(device);
1227 struct power_supply *old;
1233 * On Acer Aspire V5-573G notifications are sometimes triggered too
1234 * early. For example, when AC is unplugged and notification is
1235 * triggered, battery state is still reported as "Full", and changes to
1236 * "Discharging" only after short delay, without any notification.
1238 if (battery_notification_delay_ms > 0)
1239 msleep(battery_notification_delay_ms);
1240 if (event == ACPI_BATTERY_NOTIFY_INFO)
1241 acpi_battery_refresh(battery);
1242 acpi_battery_update(battery, false);
1243 acpi_bus_generate_netlink_event(device->pnp.device_class,
1244 dev_name(&device->dev), event,
1245 acpi_battery_present(battery));
1246 acpi_notifier_call_chain(device, event, acpi_battery_present(battery));
1247 /* acpi_battery_update could remove power_supply object */
1248 if (old && battery->bat)
1249 power_supply_changed(battery->bat);
1252 static int battery_notify(struct notifier_block *nb,
1253 unsigned long mode, void *_unused)
1255 struct acpi_battery *battery = container_of(nb, struct acpi_battery,
1260 case PM_POST_HIBERNATION:
1261 case PM_POST_SUSPEND:
1262 if (!acpi_battery_present(battery))
1266 acpi_battery_refresh(battery);
1268 result = acpi_battery_get_info(battery);
1272 result = sysfs_add_battery(battery);
1277 acpi_battery_init_alarm(battery);
1278 acpi_battery_get_state(battery);
1286 battery_bix_broken_package_quirk(const struct dmi_system_id *d)
1288 battery_bix_broken_package = 1;
1293 battery_notification_delay_quirk(const struct dmi_system_id *d)
1295 battery_notification_delay_ms = 1000;
1300 battery_ac_is_broken_quirk(const struct dmi_system_id *d)
1302 battery_ac_is_broken = 1;
1307 battery_do_not_check_pmic_quirk(const struct dmi_system_id *d)
1309 battery_check_pmic = 0;
1313 static const struct dmi_system_id bat_dmi_table[] __initconst = {
1316 .callback = battery_bix_broken_package_quirk,
1318 DMI_MATCH(DMI_SYS_VENDOR, "NEC"),
1319 DMI_MATCH(DMI_PRODUCT_NAME, "PC-LZ750LS"),
1323 /* Acer Aspire V5-573G */
1324 .callback = battery_notification_delay_quirk,
1326 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
1327 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire V5-573G"),
1331 /* Point of View mobii wintab p800w */
1332 .callback = battery_ac_is_broken_quirk,
1334 DMI_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
1335 DMI_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
1336 DMI_MATCH(DMI_BIOS_VERSION, "3BAIR1013"),
1337 /* Above matches are too generic, add bios-date match */
1338 DMI_MATCH(DMI_BIOS_DATE, "08/22/2014"),
1343 .callback = battery_do_not_check_pmic_quirk,
1345 DMI_MATCH(DMI_PRODUCT_NAME, "EF20EA"),
1349 /* Lenovo Ideapad Miix 320 */
1350 .callback = battery_do_not_check_pmic_quirk,
1352 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1353 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "80XF"),
1354 DMI_EXACT_MATCH(DMI_PRODUCT_VERSION, "Lenovo MIIX 320-10ICR"),
1361 * Some machines'(E,G Lenovo Z480) ECs are not stable
1362 * during boot up and this causes battery driver fails to be
1363 * probed due to failure of getting battery information
1364 * from EC sometimes. After several retries, the operation
1365 * may work. So add retry code here and 20ms sleep between
1368 static int acpi_battery_update_retry(struct acpi_battery *battery)
1372 for (retry = 5; retry; retry--) {
1373 ret = acpi_battery_update(battery, false);
1382 static int acpi_battery_add(struct acpi_device *device)
1385 struct acpi_battery *battery = NULL;
1390 if (device->dep_unmet)
1391 return -EPROBE_DEFER;
1393 battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
1396 battery->device = device;
1397 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
1398 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
1399 device->driver_data = battery;
1400 mutex_init(&battery->lock);
1401 mutex_init(&battery->sysfs_lock);
1402 if (acpi_has_method(battery->device->handle, "_BIX"))
1403 set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
1405 result = acpi_battery_update_retry(battery);
1409 #ifdef CONFIG_ACPI_PROCFS_POWER
1410 result = acpi_battery_add_fs(device);
1412 acpi_battery_remove_fs(device);
1417 pr_info(PREFIX "%s Slot [%s] (battery %s)\n",
1418 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
1419 device->status.battery_present ? "present" : "absent");
1421 battery->pm_nb.notifier_call = battery_notify;
1422 register_pm_notifier(&battery->pm_nb);
1424 device_init_wakeup(&device->dev, 1);
1429 sysfs_remove_battery(battery);
1430 mutex_destroy(&battery->lock);
1431 mutex_destroy(&battery->sysfs_lock);
1436 static int acpi_battery_remove(struct acpi_device *device)
1438 struct acpi_battery *battery = NULL;
1440 if (!device || !acpi_driver_data(device))
1442 device_init_wakeup(&device->dev, 0);
1443 battery = acpi_driver_data(device);
1444 unregister_pm_notifier(&battery->pm_nb);
1445 #ifdef CONFIG_ACPI_PROCFS_POWER
1446 acpi_battery_remove_fs(device);
1448 sysfs_remove_battery(battery);
1449 mutex_destroy(&battery->lock);
1450 mutex_destroy(&battery->sysfs_lock);
1455 #ifdef CONFIG_PM_SLEEP
1456 /* this is needed to learn about changes made in suspended state */
1457 static int acpi_battery_resume(struct device *dev)
1459 struct acpi_battery *battery;
1464 battery = acpi_driver_data(to_acpi_device(dev));
1468 battery->update_time = 0;
1469 acpi_battery_update(battery, true);
1473 #define acpi_battery_resume NULL
1476 static SIMPLE_DEV_PM_OPS(acpi_battery_pm, NULL, acpi_battery_resume);
1478 static struct acpi_driver acpi_battery_driver = {
1480 .class = ACPI_BATTERY_CLASS,
1481 .ids = battery_device_ids,
1482 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1484 .add = acpi_battery_add,
1485 .remove = acpi_battery_remove,
1486 .notify = acpi_battery_notify,
1488 .drv.pm = &acpi_battery_pm,
1491 static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
1496 dmi_check_system(bat_dmi_table);
1498 if (battery_check_pmic) {
1499 for (i = 0; i < ARRAY_SIZE(acpi_battery_blacklist); i++)
1500 if (acpi_dev_present(acpi_battery_blacklist[i], "1", -1)) {
1501 pr_info(PREFIX ACPI_BATTERY_DEVICE_NAME
1502 ": found native %s PMIC, not loading\n",
1503 acpi_battery_blacklist[i]);
1508 #ifdef CONFIG_ACPI_PROCFS_POWER
1509 acpi_battery_dir = acpi_lock_battery_dir();
1510 if (!acpi_battery_dir)
1513 result = acpi_bus_register_driver(&acpi_battery_driver);
1514 #ifdef CONFIG_ACPI_PROCFS_POWER
1516 acpi_unlock_battery_dir(acpi_battery_dir);
1518 battery_driver_registered = (result == 0);
1521 static int __init acpi_battery_init(void)
1526 async_cookie = async_schedule(acpi_battery_init_async, NULL);
1530 static void __exit acpi_battery_exit(void)
1532 async_synchronize_cookie(async_cookie + 1);
1533 if (battery_driver_registered) {
1534 acpi_bus_unregister_driver(&acpi_battery_driver);
1535 battery_hook_exit();
1537 #ifdef CONFIG_ACPI_PROCFS_POWER
1538 if (acpi_battery_dir)
1539 acpi_unlock_battery_dir(acpi_battery_dir);
1543 module_init(acpi_battery_init);
1544 module_exit(acpi_battery_exit);