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) "ACPI: battery: " 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 #include <linux/acpi.h>
28 #include <linux/power_supply.h>
30 #include <acpi/battery.h>
32 #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
33 #define ACPI_BATTERY_CAPACITY_VALID(capacity) \
34 ((capacity) != 0 && (capacity) != ACPI_BATTERY_VALUE_UNKNOWN)
36 #define ACPI_BATTERY_DEVICE_NAME "Battery"
38 /* Battery power unit: 0 means mW, 1 means mA */
39 #define ACPI_BATTERY_POWER_UNIT_MA 1
41 #define ACPI_BATTERY_STATE_DISCHARGING 0x1
42 #define ACPI_BATTERY_STATE_CHARGING 0x2
43 #define ACPI_BATTERY_STATE_CRITICAL 0x4
45 MODULE_AUTHOR("Paul Diefenbaugh");
46 MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
47 MODULE_DESCRIPTION("ACPI Battery Driver");
48 MODULE_LICENSE("GPL");
50 static async_cookie_t async_cookie;
51 static bool battery_driver_registered;
52 static int battery_bix_broken_package;
53 static int battery_notification_delay_ms;
54 static int battery_ac_is_broken;
55 static int battery_check_pmic = 1;
56 static int battery_quirk_notcharging;
57 static unsigned int cache_time = 1000;
58 module_param(cache_time, uint, 0644);
59 MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
61 static const struct acpi_device_id battery_device_ids[] = {
64 /* Microsoft Surface Go 3 */
70 MODULE_DEVICE_TABLE(acpi, battery_device_ids);
72 /* Lists of PMIC ACPI HIDs with an (often better) native battery driver */
73 static const char * const acpi_battery_blacklist[] = {
74 "INT33F4", /* X-Powers AXP288 PMIC */
78 ACPI_BATTERY_ALARM_PRESENT,
79 ACPI_BATTERY_XINFO_PRESENT,
80 ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY,
81 /* On Lenovo Thinkpad models from 2010 and 2011, the power unit
82 * switches between mWh and mAh depending on whether the system
83 * is running on battery or not. When mAh is the unit, most
84 * reported values are incorrect and need to be adjusted by
85 * 10000/design_voltage. Verified on x201, t410, t410s, and x220.
86 * Pre-2010 and 2012 models appear to always report in mWh and
87 * are thus unaffected (tested with t42, t61, t500, x200, x300,
88 * and x230). Also, in mid-2012 Lenovo issued a BIOS update for
89 * the 2011 models that fixes the issue (tested on x220 with a
90 * post-1.29 BIOS), but as of Nov. 2012, no such update is
91 * available for the 2010 models.
93 ACPI_BATTERY_QUIRK_THINKPAD_MAH,
94 /* for batteries reporting current capacity with design capacity
95 * on a full charge, but showing degradation in full charge cap.
97 ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE,
100 struct acpi_battery {
102 struct mutex sysfs_lock;
103 struct power_supply *bat;
104 struct power_supply_desc bat_desc;
105 struct acpi_device *device;
106 struct notifier_block pm_nb;
107 struct list_head list;
108 unsigned long update_time;
114 int full_charge_capacity;
117 int design_capacity_warning;
118 int design_capacity_low;
120 int measurement_accuracy;
121 int max_sampling_time;
122 int min_sampling_time;
123 int max_averaging_interval;
124 int min_averaging_interval;
125 int capacity_granularity_1;
126 int capacity_granularity_2;
128 char model_number[32];
129 char serial_number[32];
137 #define to_acpi_battery(x) power_supply_get_drvdata(x)
139 static inline int acpi_battery_present(struct acpi_battery *battery)
141 return battery->device->status.battery_present;
144 static int acpi_battery_technology(struct acpi_battery *battery)
146 if (!strcasecmp("NiCd", battery->type))
147 return POWER_SUPPLY_TECHNOLOGY_NiCd;
148 if (!strcasecmp("NiMH", battery->type))
149 return POWER_SUPPLY_TECHNOLOGY_NiMH;
150 if (!strcasecmp("LION", battery->type))
151 return POWER_SUPPLY_TECHNOLOGY_LION;
152 if (!strncasecmp("LI-ION", battery->type, 6))
153 return POWER_SUPPLY_TECHNOLOGY_LION;
154 if (!strcasecmp("LiP", battery->type))
155 return POWER_SUPPLY_TECHNOLOGY_LIPO;
156 return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
159 static int acpi_battery_get_state(struct acpi_battery *battery);
161 static int acpi_battery_is_charged(struct acpi_battery *battery)
163 /* charging, discharging or critical low */
164 if (battery->state != 0)
167 /* battery not reporting charge */
168 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
169 battery->capacity_now == 0)
172 /* good batteries update full_charge as the batteries degrade */
173 if (battery->full_charge_capacity == battery->capacity_now)
176 /* fallback to using design values for broken batteries */
177 if (battery->design_capacity <= battery->capacity_now)
180 /* we don't do any sort of metric based on percentages */
184 static bool acpi_battery_is_degraded(struct acpi_battery *battery)
186 return ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity) &&
187 ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity) &&
188 battery->full_charge_capacity < battery->design_capacity;
191 static int acpi_battery_handle_discharging(struct acpi_battery *battery)
194 * Some devices wrongly report discharging if the battery's charge level
195 * was above the device's start charging threshold atm the AC adapter
196 * was plugged in and the device thus did not start a new charge cycle.
198 if ((battery_ac_is_broken || power_supply_is_system_supplied()) &&
199 battery->rate_now == 0)
200 return POWER_SUPPLY_STATUS_NOT_CHARGING;
202 return POWER_SUPPLY_STATUS_DISCHARGING;
205 static int acpi_battery_get_property(struct power_supply *psy,
206 enum power_supply_property psp,
207 union power_supply_propval *val)
209 int full_capacity = ACPI_BATTERY_VALUE_UNKNOWN, ret = 0;
210 struct acpi_battery *battery = to_acpi_battery(psy);
212 if (acpi_battery_present(battery)) {
213 /* run battery update only if it is present */
214 acpi_battery_get_state(battery);
215 } else if (psp != POWER_SUPPLY_PROP_PRESENT)
218 case POWER_SUPPLY_PROP_STATUS:
219 if (battery->state & ACPI_BATTERY_STATE_DISCHARGING)
220 val->intval = acpi_battery_handle_discharging(battery);
221 else if (battery->state & ACPI_BATTERY_STATE_CHARGING)
222 val->intval = POWER_SUPPLY_STATUS_CHARGING;
223 else if (acpi_battery_is_charged(battery))
224 val->intval = POWER_SUPPLY_STATUS_FULL;
225 else if (battery_quirk_notcharging)
226 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
228 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
230 case POWER_SUPPLY_PROP_PRESENT:
231 val->intval = acpi_battery_present(battery);
233 case POWER_SUPPLY_PROP_TECHNOLOGY:
234 val->intval = acpi_battery_technology(battery);
236 case POWER_SUPPLY_PROP_CYCLE_COUNT:
237 val->intval = battery->cycle_count;
239 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
240 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
243 val->intval = battery->design_voltage * 1000;
245 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
246 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
249 val->intval = battery->voltage_now * 1000;
251 case POWER_SUPPLY_PROP_CURRENT_NOW:
252 case POWER_SUPPLY_PROP_POWER_NOW:
253 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
256 val->intval = battery->rate_now * 1000;
258 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
259 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
260 if (!ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity))
263 val->intval = battery->design_capacity * 1000;
265 case POWER_SUPPLY_PROP_CHARGE_FULL:
266 case POWER_SUPPLY_PROP_ENERGY_FULL:
267 if (!ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity))
270 val->intval = battery->full_charge_capacity * 1000;
272 case POWER_SUPPLY_PROP_CHARGE_NOW:
273 case POWER_SUPPLY_PROP_ENERGY_NOW:
274 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
277 val->intval = battery->capacity_now * 1000;
279 case POWER_SUPPLY_PROP_CAPACITY:
280 if (ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity))
281 full_capacity = battery->full_charge_capacity;
282 else if (ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity))
283 full_capacity = battery->design_capacity;
285 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
286 full_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
289 val->intval = battery->capacity_now * 100/
292 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
293 if (battery->state & ACPI_BATTERY_STATE_CRITICAL)
294 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
295 else if (test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags) &&
296 (battery->capacity_now <= battery->alarm))
297 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
298 else if (acpi_battery_is_charged(battery))
299 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
301 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
303 case POWER_SUPPLY_PROP_MODEL_NAME:
304 val->strval = battery->model_number;
306 case POWER_SUPPLY_PROP_MANUFACTURER:
307 val->strval = battery->oem_info;
309 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
310 val->strval = battery->serial_number;
318 static enum power_supply_property charge_battery_props[] = {
319 POWER_SUPPLY_PROP_STATUS,
320 POWER_SUPPLY_PROP_PRESENT,
321 POWER_SUPPLY_PROP_TECHNOLOGY,
322 POWER_SUPPLY_PROP_CYCLE_COUNT,
323 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
324 POWER_SUPPLY_PROP_VOLTAGE_NOW,
325 POWER_SUPPLY_PROP_CURRENT_NOW,
326 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
327 POWER_SUPPLY_PROP_CHARGE_FULL,
328 POWER_SUPPLY_PROP_CHARGE_NOW,
329 POWER_SUPPLY_PROP_CAPACITY,
330 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
331 POWER_SUPPLY_PROP_MODEL_NAME,
332 POWER_SUPPLY_PROP_MANUFACTURER,
333 POWER_SUPPLY_PROP_SERIAL_NUMBER,
336 static enum power_supply_property charge_battery_full_cap_broken_props[] = {
337 POWER_SUPPLY_PROP_STATUS,
338 POWER_SUPPLY_PROP_PRESENT,
339 POWER_SUPPLY_PROP_TECHNOLOGY,
340 POWER_SUPPLY_PROP_CYCLE_COUNT,
341 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
342 POWER_SUPPLY_PROP_VOLTAGE_NOW,
343 POWER_SUPPLY_PROP_CURRENT_NOW,
344 POWER_SUPPLY_PROP_CHARGE_NOW,
345 POWER_SUPPLY_PROP_MODEL_NAME,
346 POWER_SUPPLY_PROP_MANUFACTURER,
347 POWER_SUPPLY_PROP_SERIAL_NUMBER,
350 static enum power_supply_property energy_battery_props[] = {
351 POWER_SUPPLY_PROP_STATUS,
352 POWER_SUPPLY_PROP_PRESENT,
353 POWER_SUPPLY_PROP_TECHNOLOGY,
354 POWER_SUPPLY_PROP_CYCLE_COUNT,
355 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
356 POWER_SUPPLY_PROP_VOLTAGE_NOW,
357 POWER_SUPPLY_PROP_POWER_NOW,
358 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
359 POWER_SUPPLY_PROP_ENERGY_FULL,
360 POWER_SUPPLY_PROP_ENERGY_NOW,
361 POWER_SUPPLY_PROP_CAPACITY,
362 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
363 POWER_SUPPLY_PROP_MODEL_NAME,
364 POWER_SUPPLY_PROP_MANUFACTURER,
365 POWER_SUPPLY_PROP_SERIAL_NUMBER,
368 static enum power_supply_property energy_battery_full_cap_broken_props[] = {
369 POWER_SUPPLY_PROP_STATUS,
370 POWER_SUPPLY_PROP_PRESENT,
371 POWER_SUPPLY_PROP_TECHNOLOGY,
372 POWER_SUPPLY_PROP_CYCLE_COUNT,
373 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
374 POWER_SUPPLY_PROP_VOLTAGE_NOW,
375 POWER_SUPPLY_PROP_POWER_NOW,
376 POWER_SUPPLY_PROP_ENERGY_NOW,
377 POWER_SUPPLY_PROP_MODEL_NAME,
378 POWER_SUPPLY_PROP_MANUFACTURER,
379 POWER_SUPPLY_PROP_SERIAL_NUMBER,
382 /* Battery Management */
383 struct acpi_offsets {
384 size_t offset; /* offset inside struct acpi_sbs_battery */
385 u8 mode; /* int or string? */
388 static const struct acpi_offsets state_offsets[] = {
389 {offsetof(struct acpi_battery, state), 0},
390 {offsetof(struct acpi_battery, rate_now), 0},
391 {offsetof(struct acpi_battery, capacity_now), 0},
392 {offsetof(struct acpi_battery, voltage_now), 0},
395 static const struct acpi_offsets info_offsets[] = {
396 {offsetof(struct acpi_battery, power_unit), 0},
397 {offsetof(struct acpi_battery, design_capacity), 0},
398 {offsetof(struct acpi_battery, full_charge_capacity), 0},
399 {offsetof(struct acpi_battery, technology), 0},
400 {offsetof(struct acpi_battery, design_voltage), 0},
401 {offsetof(struct acpi_battery, design_capacity_warning), 0},
402 {offsetof(struct acpi_battery, design_capacity_low), 0},
403 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
404 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
405 {offsetof(struct acpi_battery, model_number), 1},
406 {offsetof(struct acpi_battery, serial_number), 1},
407 {offsetof(struct acpi_battery, type), 1},
408 {offsetof(struct acpi_battery, oem_info), 1},
411 static const struct acpi_offsets extended_info_offsets[] = {
412 {offsetof(struct acpi_battery, revision), 0},
413 {offsetof(struct acpi_battery, power_unit), 0},
414 {offsetof(struct acpi_battery, design_capacity), 0},
415 {offsetof(struct acpi_battery, full_charge_capacity), 0},
416 {offsetof(struct acpi_battery, technology), 0},
417 {offsetof(struct acpi_battery, design_voltage), 0},
418 {offsetof(struct acpi_battery, design_capacity_warning), 0},
419 {offsetof(struct acpi_battery, design_capacity_low), 0},
420 {offsetof(struct acpi_battery, cycle_count), 0},
421 {offsetof(struct acpi_battery, measurement_accuracy), 0},
422 {offsetof(struct acpi_battery, max_sampling_time), 0},
423 {offsetof(struct acpi_battery, min_sampling_time), 0},
424 {offsetof(struct acpi_battery, max_averaging_interval), 0},
425 {offsetof(struct acpi_battery, min_averaging_interval), 0},
426 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
427 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
428 {offsetof(struct acpi_battery, model_number), 1},
429 {offsetof(struct acpi_battery, serial_number), 1},
430 {offsetof(struct acpi_battery, type), 1},
431 {offsetof(struct acpi_battery, oem_info), 1},
434 static int extract_package(struct acpi_battery *battery,
435 union acpi_object *package,
436 const struct acpi_offsets *offsets, int num)
439 union acpi_object *element;
441 if (package->type != ACPI_TYPE_PACKAGE)
443 for (i = 0; i < num; ++i) {
444 if (package->package.count <= i)
446 element = &package->package.elements[i];
447 if (offsets[i].mode) {
448 u8 *ptr = (u8 *)battery + offsets[i].offset;
450 if (element->type == ACPI_TYPE_STRING ||
451 element->type == ACPI_TYPE_BUFFER)
452 strncpy(ptr, element->string.pointer, 32);
453 else if (element->type == ACPI_TYPE_INTEGER) {
454 strncpy(ptr, (u8 *)&element->integer.value,
456 ptr[sizeof(u64)] = 0;
458 *ptr = 0; /* don't have value */
460 int *x = (int *)((u8 *)battery + offsets[i].offset);
461 *x = (element->type == ACPI_TYPE_INTEGER) ?
462 element->integer.value : -1;
468 static int acpi_battery_get_status(struct acpi_battery *battery)
470 if (acpi_bus_get_status(battery->device)) {
471 acpi_handle_info(battery->device->handle,
472 "_STA evaluation failed\n");
479 static int extract_battery_info(const int use_bix,
480 struct acpi_battery *battery,
481 const struct acpi_buffer *buffer)
483 int result = -EFAULT;
485 if (use_bix && battery_bix_broken_package)
486 result = extract_package(battery, buffer->pointer,
487 extended_info_offsets + 1,
488 ARRAY_SIZE(extended_info_offsets) - 1);
490 result = extract_package(battery, buffer->pointer,
491 extended_info_offsets,
492 ARRAY_SIZE(extended_info_offsets));
494 result = extract_package(battery, buffer->pointer,
495 info_offsets, ARRAY_SIZE(info_offsets));
496 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
497 battery->full_charge_capacity = battery->design_capacity;
498 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) &&
499 battery->power_unit && battery->design_voltage) {
500 battery->design_capacity = battery->design_capacity *
501 10000 / battery->design_voltage;
502 battery->full_charge_capacity = battery->full_charge_capacity *
503 10000 / battery->design_voltage;
504 battery->design_capacity_warning =
505 battery->design_capacity_warning *
506 10000 / battery->design_voltage;
507 /* Curiously, design_capacity_low, unlike the rest of them,
510 /* capacity_granularity_* equal 1 on the systems tested, so
511 * it's impossible to tell if they would need an adjustment
512 * or not if their values were higher.
515 if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags) &&
516 battery->capacity_now > battery->full_charge_capacity)
517 battery->capacity_now = battery->full_charge_capacity;
522 static int acpi_battery_get_info(struct acpi_battery *battery)
524 const int xinfo = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
526 int result = -ENODEV;
528 if (!acpi_battery_present(battery))
532 for (use_bix = xinfo ? 1 : 0; use_bix >= 0; use_bix--) {
533 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
534 acpi_status status = AE_ERROR;
536 mutex_lock(&battery->lock);
537 status = acpi_evaluate_object(battery->device->handle,
538 use_bix ? "_BIX":"_BIF",
540 mutex_unlock(&battery->lock);
542 if (ACPI_FAILURE(status)) {
543 acpi_handle_info(battery->device->handle,
544 "%s evaluation failed: %s\n",
545 use_bix ? "_BIX":"_BIF",
546 acpi_format_exception(status));
548 result = extract_battery_info(use_bix,
552 kfree(buffer.pointer);
557 if (!result && !use_bix && xinfo)
558 pr_warn(FW_BUG "The _BIX method is broken, using _BIF.\n");
563 static int acpi_battery_get_state(struct acpi_battery *battery)
566 acpi_status status = 0;
567 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
569 if (!acpi_battery_present(battery))
572 if (battery->update_time &&
573 time_before(jiffies, battery->update_time +
574 msecs_to_jiffies(cache_time)))
577 mutex_lock(&battery->lock);
578 status = acpi_evaluate_object(battery->device->handle, "_BST",
580 mutex_unlock(&battery->lock);
582 if (ACPI_FAILURE(status)) {
583 acpi_handle_info(battery->device->handle,
584 "_BST evaluation failed: %s",
585 acpi_format_exception(status));
589 result = extract_package(battery, buffer.pointer,
590 state_offsets, ARRAY_SIZE(state_offsets));
591 battery->update_time = jiffies;
592 kfree(buffer.pointer);
594 /* For buggy DSDTs that report negative 16-bit values for either
595 * charging or discharging current and/or report 0 as 65536
598 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA &&
599 battery->rate_now != ACPI_BATTERY_VALUE_UNKNOWN &&
600 (s16)(battery->rate_now) < 0) {
601 battery->rate_now = abs((s16)battery->rate_now);
602 pr_warn_once(FW_BUG "(dis)charge rate invalid.\n");
605 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)
606 && battery->capacity_now >= 0 && battery->capacity_now <= 100)
607 battery->capacity_now = (battery->capacity_now *
608 battery->full_charge_capacity) / 100;
609 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) &&
610 battery->power_unit && battery->design_voltage) {
611 battery->capacity_now = battery->capacity_now *
612 10000 / battery->design_voltage;
614 if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags) &&
615 battery->capacity_now > battery->full_charge_capacity)
616 battery->capacity_now = battery->full_charge_capacity;
621 static int acpi_battery_set_alarm(struct acpi_battery *battery)
623 acpi_status status = 0;
625 if (!acpi_battery_present(battery) ||
626 !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags))
629 mutex_lock(&battery->lock);
630 status = acpi_execute_simple_method(battery->device->handle, "_BTP",
632 mutex_unlock(&battery->lock);
634 if (ACPI_FAILURE(status))
637 acpi_handle_debug(battery->device->handle, "Alarm set to %d\n",
643 static int acpi_battery_init_alarm(struct acpi_battery *battery)
645 /* See if alarms are supported, and if so, set default */
646 if (!acpi_has_method(battery->device->handle, "_BTP")) {
647 clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
650 set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
652 battery->alarm = battery->design_capacity_warning;
653 return acpi_battery_set_alarm(battery);
656 static ssize_t acpi_battery_alarm_show(struct device *dev,
657 struct device_attribute *attr,
660 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
662 return sprintf(buf, "%d\n", battery->alarm * 1000);
665 static ssize_t acpi_battery_alarm_store(struct device *dev,
666 struct device_attribute *attr,
667 const char *buf, size_t count)
670 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
672 if (sscanf(buf, "%lu\n", &x) == 1)
673 battery->alarm = x/1000;
674 if (acpi_battery_present(battery))
675 acpi_battery_set_alarm(battery);
679 static const struct device_attribute alarm_attr = {
680 .attr = {.name = "alarm", .mode = 0644},
681 .show = acpi_battery_alarm_show,
682 .store = acpi_battery_alarm_store,
686 * The Battery Hooking API
688 * This API is used inside other drivers that need to expose
689 * platform-specific behaviour within the generic driver in a
694 static LIST_HEAD(acpi_battery_list);
695 static LIST_HEAD(battery_hook_list);
696 static DEFINE_MUTEX(hook_mutex);
698 static void __battery_hook_unregister(struct acpi_battery_hook *hook, int lock)
700 struct acpi_battery *battery;
702 * In order to remove a hook, we first need to
703 * de-register all the batteries that are registered.
706 mutex_lock(&hook_mutex);
707 list_for_each_entry(battery, &acpi_battery_list, list) {
708 hook->remove_battery(battery->bat);
710 list_del(&hook->list);
712 mutex_unlock(&hook_mutex);
713 pr_info("extension unregistered: %s\n", hook->name);
716 void battery_hook_unregister(struct acpi_battery_hook *hook)
718 __battery_hook_unregister(hook, 1);
720 EXPORT_SYMBOL_GPL(battery_hook_unregister);
722 void battery_hook_register(struct acpi_battery_hook *hook)
724 struct acpi_battery *battery;
726 mutex_lock(&hook_mutex);
727 INIT_LIST_HEAD(&hook->list);
728 list_add(&hook->list, &battery_hook_list);
730 * Now that the driver is registered, we need
731 * to notify the hook that a battery is available
732 * for each battery, so that the driver may add
735 list_for_each_entry(battery, &acpi_battery_list, list) {
736 if (hook->add_battery(battery->bat)) {
738 * If a add-battery returns non-zero,
739 * the registration of the extension has failed,
740 * and we will not add it to the list of loaded
743 pr_err("extension failed to load: %s", hook->name);
744 __battery_hook_unregister(hook, 0);
748 pr_info("new extension: %s\n", hook->name);
750 mutex_unlock(&hook_mutex);
752 EXPORT_SYMBOL_GPL(battery_hook_register);
755 * This function gets called right after the battery sysfs
756 * attributes have been added, so that the drivers that
757 * define custom sysfs attributes can add their own.
759 static void battery_hook_add_battery(struct acpi_battery *battery)
761 struct acpi_battery_hook *hook_node, *tmp;
763 mutex_lock(&hook_mutex);
764 INIT_LIST_HEAD(&battery->list);
765 list_add(&battery->list, &acpi_battery_list);
767 * Since we added a new battery to the list, we need to
768 * iterate over the hooks and call add_battery for each
769 * hook that was registered. This usually happens
770 * when a battery gets hotplugged or initialized
771 * during the battery module initialization.
773 list_for_each_entry_safe(hook_node, tmp, &battery_hook_list, list) {
774 if (hook_node->add_battery(battery->bat)) {
776 * The notification of the extensions has failed, to
777 * prevent further errors we will unload the extension.
779 pr_err("error in extension, unloading: %s",
781 __battery_hook_unregister(hook_node, 0);
784 mutex_unlock(&hook_mutex);
787 static void battery_hook_remove_battery(struct acpi_battery *battery)
789 struct acpi_battery_hook *hook;
791 mutex_lock(&hook_mutex);
793 * Before removing the hook, we need to remove all
794 * custom attributes from the battery.
796 list_for_each_entry(hook, &battery_hook_list, list) {
797 hook->remove_battery(battery->bat);
799 /* Then, just remove the battery from the list */
800 list_del(&battery->list);
801 mutex_unlock(&hook_mutex);
804 static void __exit battery_hook_exit(void)
806 struct acpi_battery_hook *hook;
807 struct acpi_battery_hook *ptr;
809 * At this point, the acpi_bus_unregister_driver()
810 * has called remove for all batteries. We just
811 * need to remove the hooks.
813 list_for_each_entry_safe(hook, ptr, &battery_hook_list, list) {
814 __battery_hook_unregister(hook, 1);
816 mutex_destroy(&hook_mutex);
819 static int sysfs_add_battery(struct acpi_battery *battery)
821 struct power_supply_config psy_cfg = { .drv_data = battery, };
822 bool full_cap_broken = false;
824 if (!ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity) &&
825 !ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity))
826 full_cap_broken = true;
828 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) {
829 if (full_cap_broken) {
830 battery->bat_desc.properties =
831 charge_battery_full_cap_broken_props;
832 battery->bat_desc.num_properties =
833 ARRAY_SIZE(charge_battery_full_cap_broken_props);
835 battery->bat_desc.properties = charge_battery_props;
836 battery->bat_desc.num_properties =
837 ARRAY_SIZE(charge_battery_props);
840 if (full_cap_broken) {
841 battery->bat_desc.properties =
842 energy_battery_full_cap_broken_props;
843 battery->bat_desc.num_properties =
844 ARRAY_SIZE(energy_battery_full_cap_broken_props);
846 battery->bat_desc.properties = energy_battery_props;
847 battery->bat_desc.num_properties =
848 ARRAY_SIZE(energy_battery_props);
852 battery->bat_desc.name = acpi_device_bid(battery->device);
853 battery->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY;
854 battery->bat_desc.get_property = acpi_battery_get_property;
856 battery->bat = power_supply_register_no_ws(&battery->device->dev,
857 &battery->bat_desc, &psy_cfg);
859 if (IS_ERR(battery->bat)) {
860 int result = PTR_ERR(battery->bat);
865 battery_hook_add_battery(battery);
866 return device_create_file(&battery->bat->dev, &alarm_attr);
869 static void sysfs_remove_battery(struct acpi_battery *battery)
871 mutex_lock(&battery->sysfs_lock);
873 mutex_unlock(&battery->sysfs_lock);
876 battery_hook_remove_battery(battery);
877 device_remove_file(&battery->bat->dev, &alarm_attr);
878 power_supply_unregister(battery->bat);
880 mutex_unlock(&battery->sysfs_lock);
883 static void find_battery(const struct dmi_header *dm, void *private)
885 struct acpi_battery *battery = (struct acpi_battery *)private;
886 /* Note: the hardcoded offsets below have been extracted from
887 * the source code of dmidecode.
889 if (dm->type == DMI_ENTRY_PORTABLE_BATTERY && dm->length >= 8) {
890 const u8 *dmi_data = (const u8 *)(dm + 1);
891 int dmi_capacity = get_unaligned((const u16 *)(dmi_data + 6));
893 if (dm->length >= 18)
894 dmi_capacity *= dmi_data[17];
895 if (battery->design_capacity * battery->design_voltage / 1000
897 battery->design_capacity * 10 == dmi_capacity)
898 set_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH,
904 * According to the ACPI spec, some kinds of primary batteries can
905 * report percentage battery remaining capacity directly to OS.
906 * In this case, it reports the Last Full Charged Capacity == 100
907 * and BatteryPresentRate == 0xFFFFFFFF.
909 * Now we found some battery reports percentage remaining capacity
910 * even if it's rechargeable.
911 * https://bugzilla.kernel.org/show_bug.cgi?id=15979
913 * Handle this correctly so that they won't break userspace.
915 static void acpi_battery_quirks(struct acpi_battery *battery)
917 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
920 if (battery->full_charge_capacity == 100 &&
921 battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN &&
922 battery->capacity_now >= 0 && battery->capacity_now <= 100) {
923 set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags);
924 battery->full_charge_capacity = battery->design_capacity;
925 battery->capacity_now = (battery->capacity_now *
926 battery->full_charge_capacity) / 100;
929 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags))
932 if (battery->power_unit && dmi_name_in_vendors("LENOVO")) {
935 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
936 if (s && !strncasecmp(s, "ThinkPad", 8)) {
937 dmi_walk(find_battery, battery);
938 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH,
940 battery->design_voltage) {
941 battery->design_capacity =
942 battery->design_capacity *
943 10000 / battery->design_voltage;
944 battery->full_charge_capacity =
945 battery->full_charge_capacity *
946 10000 / battery->design_voltage;
947 battery->design_capacity_warning =
948 battery->design_capacity_warning *
949 10000 / battery->design_voltage;
950 battery->capacity_now = battery->capacity_now *
951 10000 / battery->design_voltage;
956 if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags))
959 if (acpi_battery_is_degraded(battery) &&
960 battery->capacity_now > battery->full_charge_capacity) {
961 set_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags);
962 battery->capacity_now = battery->full_charge_capacity;
966 static int acpi_battery_update(struct acpi_battery *battery, bool resume)
968 int result = acpi_battery_get_status(battery);
973 if (!acpi_battery_present(battery)) {
974 sysfs_remove_battery(battery);
975 battery->update_time = 0;
982 if (!battery->update_time) {
983 result = acpi_battery_get_info(battery);
986 acpi_battery_init_alarm(battery);
989 result = acpi_battery_get_state(battery);
992 acpi_battery_quirks(battery);
995 result = sysfs_add_battery(battery);
1001 * Wakeup the system if battery is critical low
1002 * or lower than the alarm level
1004 if ((battery->state & ACPI_BATTERY_STATE_CRITICAL) ||
1005 (test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags) &&
1006 (battery->capacity_now <= battery->alarm)))
1007 acpi_pm_wakeup_event(&battery->device->dev);
1012 static void acpi_battery_refresh(struct acpi_battery *battery)
1019 power_unit = battery->power_unit;
1021 acpi_battery_get_info(battery);
1023 if (power_unit == battery->power_unit)
1026 /* The battery has changed its reporting units. */
1027 sysfs_remove_battery(battery);
1028 sysfs_add_battery(battery);
1031 /* Driver Interface */
1032 static void acpi_battery_notify(struct acpi_device *device, u32 event)
1034 struct acpi_battery *battery = acpi_driver_data(device);
1035 struct power_supply *old;
1041 * On Acer Aspire V5-573G notifications are sometimes triggered too
1042 * early. For example, when AC is unplugged and notification is
1043 * triggered, battery state is still reported as "Full", and changes to
1044 * "Discharging" only after short delay, without any notification.
1046 if (battery_notification_delay_ms > 0)
1047 msleep(battery_notification_delay_ms);
1048 if (event == ACPI_BATTERY_NOTIFY_INFO)
1049 acpi_battery_refresh(battery);
1050 acpi_battery_update(battery, false);
1051 acpi_bus_generate_netlink_event(device->pnp.device_class,
1052 dev_name(&device->dev), event,
1053 acpi_battery_present(battery));
1054 acpi_notifier_call_chain(device, event, acpi_battery_present(battery));
1055 /* acpi_battery_update could remove power_supply object */
1056 if (old && battery->bat)
1057 power_supply_changed(battery->bat);
1060 static int battery_notify(struct notifier_block *nb,
1061 unsigned long mode, void *_unused)
1063 struct acpi_battery *battery = container_of(nb, struct acpi_battery,
1068 case PM_POST_HIBERNATION:
1069 case PM_POST_SUSPEND:
1070 if (!acpi_battery_present(battery))
1074 acpi_battery_refresh(battery);
1076 result = acpi_battery_get_info(battery);
1080 result = sysfs_add_battery(battery);
1085 acpi_battery_init_alarm(battery);
1086 acpi_battery_get_state(battery);
1094 battery_bix_broken_package_quirk(const struct dmi_system_id *d)
1096 battery_bix_broken_package = 1;
1101 battery_notification_delay_quirk(const struct dmi_system_id *d)
1103 battery_notification_delay_ms = 1000;
1108 battery_ac_is_broken_quirk(const struct dmi_system_id *d)
1110 battery_ac_is_broken = 1;
1115 battery_do_not_check_pmic_quirk(const struct dmi_system_id *d)
1117 battery_check_pmic = 0;
1121 static int __init battery_quirk_not_charging(const struct dmi_system_id *d)
1123 battery_quirk_notcharging = 1;
1127 static const struct dmi_system_id bat_dmi_table[] __initconst = {
1130 .callback = battery_bix_broken_package_quirk,
1132 DMI_MATCH(DMI_SYS_VENDOR, "NEC"),
1133 DMI_MATCH(DMI_PRODUCT_NAME, "PC-LZ750LS"),
1137 /* Acer Aspire V5-573G */
1138 .callback = battery_notification_delay_quirk,
1140 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
1141 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire V5-573G"),
1145 /* Point of View mobii wintab p800w */
1146 .callback = battery_ac_is_broken_quirk,
1148 DMI_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
1149 DMI_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
1150 DMI_MATCH(DMI_BIOS_VERSION, "3BAIR1013"),
1151 /* Above matches are too generic, add bios-date match */
1152 DMI_MATCH(DMI_BIOS_DATE, "08/22/2014"),
1156 /* ECS EF20EA, AXP288 PMIC but uses separate fuel-gauge */
1157 .callback = battery_do_not_check_pmic_quirk,
1159 DMI_MATCH(DMI_PRODUCT_NAME, "EF20EA"),
1163 /* Lenovo Ideapad Miix 320, AXP288 PMIC, separate fuel-gauge */
1164 .callback = battery_do_not_check_pmic_quirk,
1166 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1167 DMI_MATCH(DMI_PRODUCT_NAME, "80XF"),
1168 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo MIIX 320-10ICR"),
1173 * On Lenovo ThinkPads the BIOS specification defines
1174 * a state when the bits for charging and discharging
1175 * are both set to 0. That state is "Not Charging".
1177 .callback = battery_quirk_not_charging,
1178 .ident = "Lenovo ThinkPad",
1180 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1181 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad"),
1185 /* Microsoft Surface Go 3 */
1186 .callback = battery_notification_delay_quirk,
1188 DMI_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
1189 DMI_MATCH(DMI_PRODUCT_NAME, "Surface Go 3"),
1196 * Some machines'(E,G Lenovo Z480) ECs are not stable
1197 * during boot up and this causes battery driver fails to be
1198 * probed due to failure of getting battery information
1199 * from EC sometimes. After several retries, the operation
1200 * may work. So add retry code here and 20ms sleep between
1203 static int acpi_battery_update_retry(struct acpi_battery *battery)
1207 for (retry = 5; retry; retry--) {
1208 ret = acpi_battery_update(battery, false);
1217 static int acpi_battery_add(struct acpi_device *device)
1220 struct acpi_battery *battery = NULL;
1225 if (device->dep_unmet)
1226 return -EPROBE_DEFER;
1228 battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
1231 battery->device = device;
1232 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
1233 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
1234 device->driver_data = battery;
1235 mutex_init(&battery->lock);
1236 mutex_init(&battery->sysfs_lock);
1237 if (acpi_has_method(battery->device->handle, "_BIX"))
1238 set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
1240 result = acpi_battery_update_retry(battery);
1244 pr_info("Slot [%s] (battery %s)\n", acpi_device_bid(device),
1245 device->status.battery_present ? "present" : "absent");
1247 battery->pm_nb.notifier_call = battery_notify;
1248 register_pm_notifier(&battery->pm_nb);
1250 device_init_wakeup(&device->dev, 1);
1255 sysfs_remove_battery(battery);
1256 mutex_destroy(&battery->lock);
1257 mutex_destroy(&battery->sysfs_lock);
1262 static int acpi_battery_remove(struct acpi_device *device)
1264 struct acpi_battery *battery = NULL;
1266 if (!device || !acpi_driver_data(device))
1268 device_init_wakeup(&device->dev, 0);
1269 battery = acpi_driver_data(device);
1270 unregister_pm_notifier(&battery->pm_nb);
1271 sysfs_remove_battery(battery);
1272 mutex_destroy(&battery->lock);
1273 mutex_destroy(&battery->sysfs_lock);
1278 #ifdef CONFIG_PM_SLEEP
1279 /* this is needed to learn about changes made in suspended state */
1280 static int acpi_battery_resume(struct device *dev)
1282 struct acpi_battery *battery;
1287 battery = acpi_driver_data(to_acpi_device(dev));
1291 battery->update_time = 0;
1292 acpi_battery_update(battery, true);
1296 #define acpi_battery_resume NULL
1299 static SIMPLE_DEV_PM_OPS(acpi_battery_pm, NULL, acpi_battery_resume);
1301 static struct acpi_driver acpi_battery_driver = {
1303 .class = ACPI_BATTERY_CLASS,
1304 .ids = battery_device_ids,
1305 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1307 .add = acpi_battery_add,
1308 .remove = acpi_battery_remove,
1309 .notify = acpi_battery_notify,
1311 .drv.pm = &acpi_battery_pm,
1314 static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
1319 dmi_check_system(bat_dmi_table);
1321 if (battery_check_pmic) {
1322 for (i = 0; i < ARRAY_SIZE(acpi_battery_blacklist); i++)
1323 if (acpi_dev_present(acpi_battery_blacklist[i], "1", -1)) {
1324 pr_info("found native %s PMIC, not loading\n",
1325 acpi_battery_blacklist[i]);
1330 result = acpi_bus_register_driver(&acpi_battery_driver);
1331 battery_driver_registered = (result == 0);
1334 static int __init acpi_battery_init(void)
1339 async_cookie = async_schedule(acpi_battery_init_async, NULL);
1343 static void __exit acpi_battery_exit(void)
1345 async_synchronize_cookie(async_cookie + 1);
1346 if (battery_driver_registered) {
1347 acpi_bus_unregister_driver(&acpi_battery_driver);
1348 battery_hook_exit();
1352 module_init(acpi_battery_init);
1353 module_exit(acpi_battery_exit);