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 #include <linux/acpi.h>
28 #include <linux/power_supply.h>
30 #include <acpi/battery.h>
32 #define PREFIX "ACPI: "
34 #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
35 #define ACPI_BATTERY_CAPACITY_VALID(capacity) \
36 ((capacity) != 0 && (capacity) != ACPI_BATTERY_VALUE_UNKNOWN)
38 #define ACPI_BATTERY_DEVICE_NAME "Battery"
40 /* Battery power unit: 0 means mW, 1 means mA */
41 #define ACPI_BATTERY_POWER_UNIT_MA 1
43 #define ACPI_BATTERY_STATE_DISCHARGING 0x1
44 #define ACPI_BATTERY_STATE_CHARGING 0x2
45 #define ACPI_BATTERY_STATE_CRITICAL 0x4
47 #define _COMPONENT ACPI_BATTERY_COMPONENT
49 ACPI_MODULE_NAME("battery");
51 MODULE_AUTHOR("Paul Diefenbaugh");
52 MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
53 MODULE_DESCRIPTION("ACPI Battery Driver");
54 MODULE_LICENSE("GPL");
56 static async_cookie_t async_cookie;
57 static bool battery_driver_registered;
58 static int battery_bix_broken_package;
59 static int battery_notification_delay_ms;
60 static int battery_ac_is_broken;
61 static int battery_check_pmic = 1;
62 static int battery_quirk_notcharging;
63 static unsigned int cache_time = 1000;
64 module_param(cache_time, uint, 0644);
65 MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
67 static const struct acpi_device_id battery_device_ids[] = {
70 /* Microsoft Surface Go 3 */
76 MODULE_DEVICE_TABLE(acpi, battery_device_ids);
78 /* Lists of PMIC ACPI HIDs with an (often better) native battery driver */
79 static const char * const acpi_battery_blacklist[] = {
80 "INT33F4", /* X-Powers AXP288 PMIC */
84 ACPI_BATTERY_ALARM_PRESENT,
85 ACPI_BATTERY_XINFO_PRESENT,
86 ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY,
87 /* On Lenovo Thinkpad models from 2010 and 2011, the power unit
88 switches between mWh and mAh depending on whether the system
89 is running on battery or not. When mAh is the unit, most
90 reported values are incorrect and need to be adjusted by
91 10000/design_voltage. Verified on x201, t410, t410s, and x220.
92 Pre-2010 and 2012 models appear to always report in mWh and
93 are thus unaffected (tested with t42, t61, t500, x200, x300,
94 and x230). Also, in mid-2012 Lenovo issued a BIOS update for
95 the 2011 models that fixes the issue (tested on x220 with a
96 post-1.29 BIOS), but as of Nov. 2012, no such update is
97 available for the 2010 models. */
98 ACPI_BATTERY_QUIRK_THINKPAD_MAH,
99 /* for batteries reporting current capacity with design capacity
100 * on a full charge, but showing degradation in full charge cap.
102 ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE,
105 struct acpi_battery {
107 struct mutex sysfs_lock;
108 struct power_supply *bat;
109 struct power_supply_desc bat_desc;
110 struct acpi_device *device;
111 struct notifier_block pm_nb;
112 struct list_head list;
113 unsigned long update_time;
119 int full_charge_capacity;
122 int design_capacity_warning;
123 int design_capacity_low;
125 int measurement_accuracy;
126 int max_sampling_time;
127 int min_sampling_time;
128 int max_averaging_interval;
129 int min_averaging_interval;
130 int capacity_granularity_1;
131 int capacity_granularity_2;
133 char model_number[32];
134 char serial_number[32];
142 #define to_acpi_battery(x) power_supply_get_drvdata(x)
144 static inline int acpi_battery_present(struct acpi_battery *battery)
146 return battery->device->status.battery_present;
149 static int acpi_battery_technology(struct acpi_battery *battery)
151 if (!strcasecmp("NiCd", battery->type))
152 return POWER_SUPPLY_TECHNOLOGY_NiCd;
153 if (!strcasecmp("NiMH", battery->type))
154 return POWER_SUPPLY_TECHNOLOGY_NiMH;
155 if (!strcasecmp("LION", battery->type))
156 return POWER_SUPPLY_TECHNOLOGY_LION;
157 if (!strncasecmp("LI-ION", battery->type, 6))
158 return POWER_SUPPLY_TECHNOLOGY_LION;
159 if (!strcasecmp("LiP", battery->type))
160 return POWER_SUPPLY_TECHNOLOGY_LIPO;
161 return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
164 static int acpi_battery_get_state(struct acpi_battery *battery);
166 static int acpi_battery_is_charged(struct acpi_battery *battery)
168 /* charging, discharging or critical low */
169 if (battery->state != 0)
172 /* battery not reporting charge */
173 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
174 battery->capacity_now == 0)
177 /* good batteries update full_charge as the batteries degrade */
178 if (battery->full_charge_capacity == battery->capacity_now)
181 /* fallback to using design values for broken batteries */
182 if (battery->design_capacity <= battery->capacity_now)
185 /* we don't do any sort of metric based on percentages */
189 static bool acpi_battery_is_degraded(struct acpi_battery *battery)
191 return ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity) &&
192 ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity) &&
193 battery->full_charge_capacity < battery->design_capacity;
196 static int acpi_battery_handle_discharging(struct acpi_battery *battery)
199 * Some devices wrongly report discharging if the battery's charge level
200 * was above the device's start charging threshold atm the AC adapter
201 * was plugged in and the device thus did not start a new charge cycle.
203 if ((battery_ac_is_broken || power_supply_is_system_supplied()) &&
204 battery->rate_now == 0)
205 return POWER_SUPPLY_STATUS_NOT_CHARGING;
207 return POWER_SUPPLY_STATUS_DISCHARGING;
210 static int acpi_battery_get_property(struct power_supply *psy,
211 enum power_supply_property psp,
212 union power_supply_propval *val)
214 int full_capacity = ACPI_BATTERY_VALUE_UNKNOWN, ret = 0;
215 struct acpi_battery *battery = to_acpi_battery(psy);
217 if (acpi_battery_present(battery)) {
218 /* run battery update only if it is present */
219 acpi_battery_get_state(battery);
220 } else if (psp != POWER_SUPPLY_PROP_PRESENT)
223 case POWER_SUPPLY_PROP_STATUS:
224 if (battery->state & ACPI_BATTERY_STATE_DISCHARGING)
225 val->intval = acpi_battery_handle_discharging(battery);
226 else if (battery->state & ACPI_BATTERY_STATE_CHARGING)
227 val->intval = POWER_SUPPLY_STATUS_CHARGING;
228 else if (acpi_battery_is_charged(battery))
229 val->intval = POWER_SUPPLY_STATUS_FULL;
230 else if (battery_quirk_notcharging)
231 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
233 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
235 case POWER_SUPPLY_PROP_PRESENT:
236 val->intval = acpi_battery_present(battery);
238 case POWER_SUPPLY_PROP_TECHNOLOGY:
239 val->intval = acpi_battery_technology(battery);
241 case POWER_SUPPLY_PROP_CYCLE_COUNT:
242 val->intval = battery->cycle_count;
244 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
245 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
248 val->intval = battery->design_voltage * 1000;
250 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
251 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
254 val->intval = battery->voltage_now * 1000;
256 case POWER_SUPPLY_PROP_CURRENT_NOW:
257 case POWER_SUPPLY_PROP_POWER_NOW:
258 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
261 val->intval = battery->rate_now * 1000;
263 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
264 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
265 if (!ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity))
268 val->intval = battery->design_capacity * 1000;
270 case POWER_SUPPLY_PROP_CHARGE_FULL:
271 case POWER_SUPPLY_PROP_ENERGY_FULL:
272 if (!ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity))
275 val->intval = battery->full_charge_capacity * 1000;
277 case POWER_SUPPLY_PROP_CHARGE_NOW:
278 case POWER_SUPPLY_PROP_ENERGY_NOW:
279 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
282 val->intval = battery->capacity_now * 1000;
284 case POWER_SUPPLY_PROP_CAPACITY:
285 if (ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity))
286 full_capacity = battery->full_charge_capacity;
287 else if (ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity))
288 full_capacity = battery->design_capacity;
290 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
291 full_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
294 val->intval = battery->capacity_now * 100/
297 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
298 if (battery->state & ACPI_BATTERY_STATE_CRITICAL)
299 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
300 else if (test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags) &&
301 (battery->capacity_now <= battery->alarm))
302 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
303 else if (acpi_battery_is_charged(battery))
304 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
306 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
308 case POWER_SUPPLY_PROP_MODEL_NAME:
309 val->strval = battery->model_number;
311 case POWER_SUPPLY_PROP_MANUFACTURER:
312 val->strval = battery->oem_info;
314 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
315 val->strval = battery->serial_number;
323 static enum power_supply_property charge_battery_props[] = {
324 POWER_SUPPLY_PROP_STATUS,
325 POWER_SUPPLY_PROP_PRESENT,
326 POWER_SUPPLY_PROP_TECHNOLOGY,
327 POWER_SUPPLY_PROP_CYCLE_COUNT,
328 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
329 POWER_SUPPLY_PROP_VOLTAGE_NOW,
330 POWER_SUPPLY_PROP_CURRENT_NOW,
331 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
332 POWER_SUPPLY_PROP_CHARGE_FULL,
333 POWER_SUPPLY_PROP_CHARGE_NOW,
334 POWER_SUPPLY_PROP_CAPACITY,
335 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
336 POWER_SUPPLY_PROP_MODEL_NAME,
337 POWER_SUPPLY_PROP_MANUFACTURER,
338 POWER_SUPPLY_PROP_SERIAL_NUMBER,
341 static enum power_supply_property charge_battery_full_cap_broken_props[] = {
342 POWER_SUPPLY_PROP_STATUS,
343 POWER_SUPPLY_PROP_PRESENT,
344 POWER_SUPPLY_PROP_TECHNOLOGY,
345 POWER_SUPPLY_PROP_CYCLE_COUNT,
346 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
347 POWER_SUPPLY_PROP_VOLTAGE_NOW,
348 POWER_SUPPLY_PROP_CURRENT_NOW,
349 POWER_SUPPLY_PROP_CHARGE_NOW,
350 POWER_SUPPLY_PROP_MODEL_NAME,
351 POWER_SUPPLY_PROP_MANUFACTURER,
352 POWER_SUPPLY_PROP_SERIAL_NUMBER,
355 static enum power_supply_property energy_battery_props[] = {
356 POWER_SUPPLY_PROP_STATUS,
357 POWER_SUPPLY_PROP_PRESENT,
358 POWER_SUPPLY_PROP_TECHNOLOGY,
359 POWER_SUPPLY_PROP_CYCLE_COUNT,
360 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
361 POWER_SUPPLY_PROP_VOLTAGE_NOW,
362 POWER_SUPPLY_PROP_POWER_NOW,
363 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
364 POWER_SUPPLY_PROP_ENERGY_FULL,
365 POWER_SUPPLY_PROP_ENERGY_NOW,
366 POWER_SUPPLY_PROP_CAPACITY,
367 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
368 POWER_SUPPLY_PROP_MODEL_NAME,
369 POWER_SUPPLY_PROP_MANUFACTURER,
370 POWER_SUPPLY_PROP_SERIAL_NUMBER,
373 static enum power_supply_property energy_battery_full_cap_broken_props[] = {
374 POWER_SUPPLY_PROP_STATUS,
375 POWER_SUPPLY_PROP_PRESENT,
376 POWER_SUPPLY_PROP_TECHNOLOGY,
377 POWER_SUPPLY_PROP_CYCLE_COUNT,
378 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
379 POWER_SUPPLY_PROP_VOLTAGE_NOW,
380 POWER_SUPPLY_PROP_POWER_NOW,
381 POWER_SUPPLY_PROP_ENERGY_NOW,
382 POWER_SUPPLY_PROP_MODEL_NAME,
383 POWER_SUPPLY_PROP_MANUFACTURER,
384 POWER_SUPPLY_PROP_SERIAL_NUMBER,
387 /* --------------------------------------------------------------------------
389 -------------------------------------------------------------------------- */
390 struct acpi_offsets {
391 size_t offset; /* offset inside struct acpi_sbs_battery */
392 u8 mode; /* int or string? */
395 static const struct acpi_offsets state_offsets[] = {
396 {offsetof(struct acpi_battery, state), 0},
397 {offsetof(struct acpi_battery, rate_now), 0},
398 {offsetof(struct acpi_battery, capacity_now), 0},
399 {offsetof(struct acpi_battery, voltage_now), 0},
402 static const struct acpi_offsets info_offsets[] = {
403 {offsetof(struct acpi_battery, power_unit), 0},
404 {offsetof(struct acpi_battery, design_capacity), 0},
405 {offsetof(struct acpi_battery, full_charge_capacity), 0},
406 {offsetof(struct acpi_battery, technology), 0},
407 {offsetof(struct acpi_battery, design_voltage), 0},
408 {offsetof(struct acpi_battery, design_capacity_warning), 0},
409 {offsetof(struct acpi_battery, design_capacity_low), 0},
410 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
411 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
412 {offsetof(struct acpi_battery, model_number), 1},
413 {offsetof(struct acpi_battery, serial_number), 1},
414 {offsetof(struct acpi_battery, type), 1},
415 {offsetof(struct acpi_battery, oem_info), 1},
418 static const struct acpi_offsets extended_info_offsets[] = {
419 {offsetof(struct acpi_battery, revision), 0},
420 {offsetof(struct acpi_battery, power_unit), 0},
421 {offsetof(struct acpi_battery, design_capacity), 0},
422 {offsetof(struct acpi_battery, full_charge_capacity), 0},
423 {offsetof(struct acpi_battery, technology), 0},
424 {offsetof(struct acpi_battery, design_voltage), 0},
425 {offsetof(struct acpi_battery, design_capacity_warning), 0},
426 {offsetof(struct acpi_battery, design_capacity_low), 0},
427 {offsetof(struct acpi_battery, cycle_count), 0},
428 {offsetof(struct acpi_battery, measurement_accuracy), 0},
429 {offsetof(struct acpi_battery, max_sampling_time), 0},
430 {offsetof(struct acpi_battery, min_sampling_time), 0},
431 {offsetof(struct acpi_battery, max_averaging_interval), 0},
432 {offsetof(struct acpi_battery, min_averaging_interval), 0},
433 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
434 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
435 {offsetof(struct acpi_battery, model_number), 1},
436 {offsetof(struct acpi_battery, serial_number), 1},
437 {offsetof(struct acpi_battery, type), 1},
438 {offsetof(struct acpi_battery, oem_info), 1},
441 static int extract_package(struct acpi_battery *battery,
442 union acpi_object *package,
443 const struct acpi_offsets *offsets, int num)
446 union acpi_object *element;
447 if (package->type != ACPI_TYPE_PACKAGE)
449 for (i = 0; i < num; ++i) {
450 if (package->package.count <= i)
452 element = &package->package.elements[i];
453 if (offsets[i].mode) {
454 u8 *ptr = (u8 *)battery + offsets[i].offset;
455 if (element->type == ACPI_TYPE_STRING ||
456 element->type == ACPI_TYPE_BUFFER)
457 strncpy(ptr, element->string.pointer, 32);
458 else if (element->type == ACPI_TYPE_INTEGER) {
459 strncpy(ptr, (u8 *)&element->integer.value,
461 ptr[sizeof(u64)] = 0;
463 *ptr = 0; /* don't have value */
465 int *x = (int *)((u8 *)battery + offsets[i].offset);
466 *x = (element->type == ACPI_TYPE_INTEGER) ?
467 element->integer.value : -1;
473 static int acpi_battery_get_status(struct acpi_battery *battery)
475 if (acpi_bus_get_status(battery->device)) {
476 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
483 static int extract_battery_info(const int use_bix,
484 struct acpi_battery *battery,
485 const struct acpi_buffer *buffer)
487 int result = -EFAULT;
489 if (use_bix && battery_bix_broken_package)
490 result = extract_package(battery, buffer->pointer,
491 extended_info_offsets + 1,
492 ARRAY_SIZE(extended_info_offsets) - 1);
494 result = extract_package(battery, buffer->pointer,
495 extended_info_offsets,
496 ARRAY_SIZE(extended_info_offsets));
498 result = extract_package(battery, buffer->pointer,
499 info_offsets, ARRAY_SIZE(info_offsets));
500 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
501 battery->full_charge_capacity = battery->design_capacity;
502 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) &&
503 battery->power_unit && battery->design_voltage) {
504 battery->design_capacity = battery->design_capacity *
505 10000 / battery->design_voltage;
506 battery->full_charge_capacity = battery->full_charge_capacity *
507 10000 / battery->design_voltage;
508 battery->design_capacity_warning =
509 battery->design_capacity_warning *
510 10000 / battery->design_voltage;
511 /* Curiously, design_capacity_low, unlike the rest of them,
513 /* capacity_granularity_* equal 1 on the systems tested, so
514 it's impossible to tell if they would need an adjustment
515 or not if their values were higher. */
517 if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags) &&
518 battery->capacity_now > battery->full_charge_capacity)
519 battery->capacity_now = battery->full_charge_capacity;
524 static int acpi_battery_get_info(struct acpi_battery *battery)
526 const int xinfo = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
528 int result = -ENODEV;
530 if (!acpi_battery_present(battery))
534 for (use_bix = xinfo ? 1 : 0; use_bix >= 0; use_bix--) {
535 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
536 acpi_status status = AE_ERROR;
538 mutex_lock(&battery->lock);
539 status = acpi_evaluate_object(battery->device->handle,
540 use_bix ? "_BIX":"_BIF",
542 mutex_unlock(&battery->lock);
544 if (ACPI_FAILURE(status)) {
545 ACPI_EXCEPTION((AE_INFO, status, "Evaluating %s",
546 use_bix ? "_BIX":"_BIF"));
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_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
587 result = extract_package(battery, buffer.pointer,
588 state_offsets, ARRAY_SIZE(state_offsets));
589 battery->update_time = jiffies;
590 kfree(buffer.pointer);
592 /* For buggy DSDTs that report negative 16-bit values for either
593 * charging or discharging current and/or report 0 as 65536
596 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA &&
597 battery->rate_now != ACPI_BATTERY_VALUE_UNKNOWN &&
598 (s16)(battery->rate_now) < 0) {
599 battery->rate_now = abs((s16)battery->rate_now);
600 pr_warn_once(FW_BUG "battery: (dis)charge rate invalid.\n");
603 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)
604 && battery->capacity_now >= 0 && battery->capacity_now <= 100)
605 battery->capacity_now = (battery->capacity_now *
606 battery->full_charge_capacity) / 100;
607 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) &&
608 battery->power_unit && battery->design_voltage) {
609 battery->capacity_now = battery->capacity_now *
610 10000 / battery->design_voltage;
612 if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags) &&
613 battery->capacity_now > battery->full_charge_capacity)
614 battery->capacity_now = battery->full_charge_capacity;
619 static int acpi_battery_set_alarm(struct acpi_battery *battery)
621 acpi_status status = 0;
623 if (!acpi_battery_present(battery) ||
624 !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags))
627 mutex_lock(&battery->lock);
628 status = acpi_execute_simple_method(battery->device->handle, "_BTP",
630 mutex_unlock(&battery->lock);
632 if (ACPI_FAILURE(status))
635 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm));
639 static int acpi_battery_init_alarm(struct acpi_battery *battery)
641 /* See if alarms are supported, and if so, set default */
642 if (!acpi_has_method(battery->device->handle, "_BTP")) {
643 clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
646 set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
648 battery->alarm = battery->design_capacity_warning;
649 return acpi_battery_set_alarm(battery);
652 static ssize_t acpi_battery_alarm_show(struct device *dev,
653 struct device_attribute *attr,
656 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
657 return sprintf(buf, "%d\n", battery->alarm * 1000);
660 static ssize_t acpi_battery_alarm_store(struct device *dev,
661 struct device_attribute *attr,
662 const char *buf, size_t count)
665 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
666 if (sscanf(buf, "%lu\n", &x) == 1)
667 battery->alarm = x/1000;
668 if (acpi_battery_present(battery))
669 acpi_battery_set_alarm(battery);
673 static const struct device_attribute alarm_attr = {
674 .attr = {.name = "alarm", .mode = 0644},
675 .show = acpi_battery_alarm_show,
676 .store = acpi_battery_alarm_store,
680 * The Battery Hooking API
682 * This API is used inside other drivers that need to expose
683 * platform-specific behaviour within the generic driver in a
688 static LIST_HEAD(acpi_battery_list);
689 static LIST_HEAD(battery_hook_list);
690 static DEFINE_MUTEX(hook_mutex);
692 static void __battery_hook_unregister(struct acpi_battery_hook *hook, int lock)
694 struct acpi_battery *battery;
696 * In order to remove a hook, we first need to
697 * de-register all the batteries that are registered.
700 mutex_lock(&hook_mutex);
701 list_for_each_entry(battery, &acpi_battery_list, list) {
702 hook->remove_battery(battery->bat);
704 list_del(&hook->list);
706 mutex_unlock(&hook_mutex);
707 pr_info("extension unregistered: %s\n", hook->name);
710 void battery_hook_unregister(struct acpi_battery_hook *hook)
712 __battery_hook_unregister(hook, 1);
714 EXPORT_SYMBOL_GPL(battery_hook_unregister);
716 void battery_hook_register(struct acpi_battery_hook *hook)
718 struct acpi_battery *battery;
720 mutex_lock(&hook_mutex);
721 INIT_LIST_HEAD(&hook->list);
722 list_add(&hook->list, &battery_hook_list);
724 * Now that the driver is registered, we need
725 * to notify the hook that a battery is available
726 * for each battery, so that the driver may add
729 list_for_each_entry(battery, &acpi_battery_list, list) {
730 if (hook->add_battery(battery->bat)) {
732 * If a add-battery returns non-zero,
733 * the registration of the extension has failed,
734 * and we will not add it to the list of loaded
737 pr_err("extension failed to load: %s", hook->name);
738 __battery_hook_unregister(hook, 0);
742 pr_info("new extension: %s\n", hook->name);
744 mutex_unlock(&hook_mutex);
746 EXPORT_SYMBOL_GPL(battery_hook_register);
749 * This function gets called right after the battery sysfs
750 * attributes have been added, so that the drivers that
751 * define custom sysfs attributes can add their own.
753 static void battery_hook_add_battery(struct acpi_battery *battery)
755 struct acpi_battery_hook *hook_node, *tmp;
757 mutex_lock(&hook_mutex);
758 INIT_LIST_HEAD(&battery->list);
759 list_add(&battery->list, &acpi_battery_list);
761 * Since we added a new battery to the list, we need to
762 * iterate over the hooks and call add_battery for each
763 * hook that was registered. This usually happens
764 * when a battery gets hotplugged or initialized
765 * during the battery module initialization.
767 list_for_each_entry_safe(hook_node, tmp, &battery_hook_list, list) {
768 if (hook_node->add_battery(battery->bat)) {
770 * The notification of the extensions has failed, to
771 * prevent further errors we will unload the extension.
773 pr_err("error in extension, unloading: %s",
775 __battery_hook_unregister(hook_node, 0);
778 mutex_unlock(&hook_mutex);
781 static void battery_hook_remove_battery(struct acpi_battery *battery)
783 struct acpi_battery_hook *hook;
785 mutex_lock(&hook_mutex);
787 * Before removing the hook, we need to remove all
788 * custom attributes from the battery.
790 list_for_each_entry(hook, &battery_hook_list, list) {
791 hook->remove_battery(battery->bat);
793 /* Then, just remove the battery from the list */
794 list_del(&battery->list);
795 mutex_unlock(&hook_mutex);
798 static void __exit battery_hook_exit(void)
800 struct acpi_battery_hook *hook;
801 struct acpi_battery_hook *ptr;
803 * At this point, the acpi_bus_unregister_driver()
804 * has called remove for all batteries. We just
805 * need to remove the hooks.
807 list_for_each_entry_safe(hook, ptr, &battery_hook_list, list) {
808 __battery_hook_unregister(hook, 1);
810 mutex_destroy(&hook_mutex);
813 static int sysfs_add_battery(struct acpi_battery *battery)
815 struct power_supply_config psy_cfg = { .drv_data = battery, };
816 bool full_cap_broken = false;
818 if (!ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity) &&
819 !ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity))
820 full_cap_broken = true;
822 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) {
823 if (full_cap_broken) {
824 battery->bat_desc.properties =
825 charge_battery_full_cap_broken_props;
826 battery->bat_desc.num_properties =
827 ARRAY_SIZE(charge_battery_full_cap_broken_props);
829 battery->bat_desc.properties = charge_battery_props;
830 battery->bat_desc.num_properties =
831 ARRAY_SIZE(charge_battery_props);
834 if (full_cap_broken) {
835 battery->bat_desc.properties =
836 energy_battery_full_cap_broken_props;
837 battery->bat_desc.num_properties =
838 ARRAY_SIZE(energy_battery_full_cap_broken_props);
840 battery->bat_desc.properties = energy_battery_props;
841 battery->bat_desc.num_properties =
842 ARRAY_SIZE(energy_battery_props);
846 battery->bat_desc.name = acpi_device_bid(battery->device);
847 battery->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY;
848 battery->bat_desc.get_property = acpi_battery_get_property;
850 battery->bat = power_supply_register_no_ws(&battery->device->dev,
851 &battery->bat_desc, &psy_cfg);
853 if (IS_ERR(battery->bat)) {
854 int result = PTR_ERR(battery->bat);
859 battery_hook_add_battery(battery);
860 return device_create_file(&battery->bat->dev, &alarm_attr);
863 static void sysfs_remove_battery(struct acpi_battery *battery)
865 mutex_lock(&battery->sysfs_lock);
867 mutex_unlock(&battery->sysfs_lock);
870 battery_hook_remove_battery(battery);
871 device_remove_file(&battery->bat->dev, &alarm_attr);
872 power_supply_unregister(battery->bat);
874 mutex_unlock(&battery->sysfs_lock);
877 static void find_battery(const struct dmi_header *dm, void *private)
879 struct acpi_battery *battery = (struct acpi_battery *)private;
880 /* Note: the hardcoded offsets below have been extracted from
881 the source code of dmidecode. */
882 if (dm->type == DMI_ENTRY_PORTABLE_BATTERY && dm->length >= 8) {
883 const u8 *dmi_data = (const u8 *)(dm + 1);
884 int dmi_capacity = get_unaligned((const u16 *)(dmi_data + 6));
885 if (dm->length >= 18)
886 dmi_capacity *= dmi_data[17];
887 if (battery->design_capacity * battery->design_voltage / 1000
889 battery->design_capacity * 10 == dmi_capacity)
890 set_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH,
896 * According to the ACPI spec, some kinds of primary batteries can
897 * report percentage battery remaining capacity directly to OS.
898 * In this case, it reports the Last Full Charged Capacity == 100
899 * and BatteryPresentRate == 0xFFFFFFFF.
901 * Now we found some battery reports percentage remaining capacity
902 * even if it's rechargeable.
903 * https://bugzilla.kernel.org/show_bug.cgi?id=15979
905 * Handle this correctly so that they won't break userspace.
907 static void acpi_battery_quirks(struct acpi_battery *battery)
909 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
912 if (battery->full_charge_capacity == 100 &&
913 battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN &&
914 battery->capacity_now >= 0 && battery->capacity_now <= 100) {
915 set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags);
916 battery->full_charge_capacity = battery->design_capacity;
917 battery->capacity_now = (battery->capacity_now *
918 battery->full_charge_capacity) / 100;
921 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags))
924 if (battery->power_unit && dmi_name_in_vendors("LENOVO")) {
926 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
927 if (s && !strncasecmp(s, "ThinkPad", 8)) {
928 dmi_walk(find_battery, battery);
929 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH,
931 battery->design_voltage) {
932 battery->design_capacity =
933 battery->design_capacity *
934 10000 / battery->design_voltage;
935 battery->full_charge_capacity =
936 battery->full_charge_capacity *
937 10000 / battery->design_voltage;
938 battery->design_capacity_warning =
939 battery->design_capacity_warning *
940 10000 / battery->design_voltage;
941 battery->capacity_now = battery->capacity_now *
942 10000 / battery->design_voltage;
947 if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags))
950 if (acpi_battery_is_degraded(battery) &&
951 battery->capacity_now > battery->full_charge_capacity) {
952 set_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags);
953 battery->capacity_now = battery->full_charge_capacity;
957 static int acpi_battery_update(struct acpi_battery *battery, bool resume)
959 int result = acpi_battery_get_status(battery);
964 if (!acpi_battery_present(battery)) {
965 sysfs_remove_battery(battery);
966 battery->update_time = 0;
973 if (!battery->update_time) {
974 result = acpi_battery_get_info(battery);
977 acpi_battery_init_alarm(battery);
980 result = acpi_battery_get_state(battery);
983 acpi_battery_quirks(battery);
986 result = sysfs_add_battery(battery);
992 * Wakeup the system if battery is critical low
993 * or lower than the alarm level
995 if ((battery->state & ACPI_BATTERY_STATE_CRITICAL) ||
996 (test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags) &&
997 (battery->capacity_now <= battery->alarm)))
998 acpi_pm_wakeup_event(&battery->device->dev);
1003 static void acpi_battery_refresh(struct acpi_battery *battery)
1010 power_unit = battery->power_unit;
1012 acpi_battery_get_info(battery);
1014 if (power_unit == battery->power_unit)
1017 /* The battery has changed its reporting units. */
1018 sysfs_remove_battery(battery);
1019 sysfs_add_battery(battery);
1022 /* --------------------------------------------------------------------------
1024 -------------------------------------------------------------------------- */
1026 static void acpi_battery_notify(struct acpi_device *device, u32 event)
1028 struct acpi_battery *battery = acpi_driver_data(device);
1029 struct power_supply *old;
1035 * On Acer Aspire V5-573G notifications are sometimes triggered too
1036 * early. For example, when AC is unplugged and notification is
1037 * triggered, battery state is still reported as "Full", and changes to
1038 * "Discharging" only after short delay, without any notification.
1040 if (battery_notification_delay_ms > 0)
1041 msleep(battery_notification_delay_ms);
1042 if (event == ACPI_BATTERY_NOTIFY_INFO)
1043 acpi_battery_refresh(battery);
1044 acpi_battery_update(battery, false);
1045 acpi_bus_generate_netlink_event(device->pnp.device_class,
1046 dev_name(&device->dev), event,
1047 acpi_battery_present(battery));
1048 acpi_notifier_call_chain(device, event, acpi_battery_present(battery));
1049 /* acpi_battery_update could remove power_supply object */
1050 if (old && battery->bat)
1051 power_supply_changed(battery->bat);
1054 static int battery_notify(struct notifier_block *nb,
1055 unsigned long mode, void *_unused)
1057 struct acpi_battery *battery = container_of(nb, struct acpi_battery,
1062 case PM_POST_HIBERNATION:
1063 case PM_POST_SUSPEND:
1064 if (!acpi_battery_present(battery))
1068 acpi_battery_refresh(battery);
1070 result = acpi_battery_get_info(battery);
1074 result = sysfs_add_battery(battery);
1079 acpi_battery_init_alarm(battery);
1080 acpi_battery_get_state(battery);
1088 battery_bix_broken_package_quirk(const struct dmi_system_id *d)
1090 battery_bix_broken_package = 1;
1095 battery_notification_delay_quirk(const struct dmi_system_id *d)
1097 battery_notification_delay_ms = 1000;
1102 battery_ac_is_broken_quirk(const struct dmi_system_id *d)
1104 battery_ac_is_broken = 1;
1109 battery_do_not_check_pmic_quirk(const struct dmi_system_id *d)
1111 battery_check_pmic = 0;
1115 static int __init battery_quirk_not_charging(const struct dmi_system_id *d)
1117 battery_quirk_notcharging = 1;
1121 static const struct dmi_system_id bat_dmi_table[] __initconst = {
1124 .callback = battery_bix_broken_package_quirk,
1126 DMI_MATCH(DMI_SYS_VENDOR, "NEC"),
1127 DMI_MATCH(DMI_PRODUCT_NAME, "PC-LZ750LS"),
1131 /* Acer Aspire V5-573G */
1132 .callback = battery_notification_delay_quirk,
1134 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
1135 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire V5-573G"),
1139 /* Point of View mobii wintab p800w */
1140 .callback = battery_ac_is_broken_quirk,
1142 DMI_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
1143 DMI_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
1144 DMI_MATCH(DMI_BIOS_VERSION, "3BAIR1013"),
1145 /* Above matches are too generic, add bios-date match */
1146 DMI_MATCH(DMI_BIOS_DATE, "08/22/2014"),
1150 /* ECS EF20EA, AXP288 PMIC but uses separate fuel-gauge */
1151 .callback = battery_do_not_check_pmic_quirk,
1153 DMI_MATCH(DMI_PRODUCT_NAME, "EF20EA"),
1157 /* Lenovo Ideapad Miix 320, AXP288 PMIC, separate fuel-gauge */
1158 .callback = battery_do_not_check_pmic_quirk,
1160 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1161 DMI_MATCH(DMI_PRODUCT_NAME, "80XF"),
1162 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo MIIX 320-10ICR"),
1167 * On Lenovo ThinkPads the BIOS specification defines
1168 * a state when the bits for charging and discharging
1169 * are both set to 0. That state is "Not Charging".
1171 .callback = battery_quirk_not_charging,
1172 .ident = "Lenovo ThinkPad",
1174 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1175 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad"),
1179 /* Microsoft Surface Go 3 */
1180 .callback = battery_notification_delay_quirk,
1182 DMI_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
1183 DMI_MATCH(DMI_PRODUCT_NAME, "Surface Go 3"),
1190 * Some machines'(E,G Lenovo Z480) ECs are not stable
1191 * during boot up and this causes battery driver fails to be
1192 * probed due to failure of getting battery information
1193 * from EC sometimes. After several retries, the operation
1194 * may work. So add retry code here and 20ms sleep between
1197 static int acpi_battery_update_retry(struct acpi_battery *battery)
1201 for (retry = 5; retry; retry--) {
1202 ret = acpi_battery_update(battery, false);
1211 static int acpi_battery_add(struct acpi_device *device)
1214 struct acpi_battery *battery = NULL;
1219 if (device->dep_unmet)
1220 return -EPROBE_DEFER;
1222 battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
1225 battery->device = device;
1226 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
1227 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
1228 device->driver_data = battery;
1229 mutex_init(&battery->lock);
1230 mutex_init(&battery->sysfs_lock);
1231 if (acpi_has_method(battery->device->handle, "_BIX"))
1232 set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
1234 result = acpi_battery_update_retry(battery);
1238 pr_info(PREFIX "%s Slot [%s] (battery %s)\n",
1239 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
1240 device->status.battery_present ? "present" : "absent");
1242 battery->pm_nb.notifier_call = battery_notify;
1243 register_pm_notifier(&battery->pm_nb);
1245 device_init_wakeup(&device->dev, 1);
1250 sysfs_remove_battery(battery);
1251 mutex_destroy(&battery->lock);
1252 mutex_destroy(&battery->sysfs_lock);
1257 static int acpi_battery_remove(struct acpi_device *device)
1259 struct acpi_battery *battery = NULL;
1261 if (!device || !acpi_driver_data(device))
1263 device_init_wakeup(&device->dev, 0);
1264 battery = acpi_driver_data(device);
1265 unregister_pm_notifier(&battery->pm_nb);
1266 sysfs_remove_battery(battery);
1267 mutex_destroy(&battery->lock);
1268 mutex_destroy(&battery->sysfs_lock);
1273 #ifdef CONFIG_PM_SLEEP
1274 /* this is needed to learn about changes made in suspended state */
1275 static int acpi_battery_resume(struct device *dev)
1277 struct acpi_battery *battery;
1282 battery = acpi_driver_data(to_acpi_device(dev));
1286 battery->update_time = 0;
1287 acpi_battery_update(battery, true);
1291 #define acpi_battery_resume NULL
1294 static SIMPLE_DEV_PM_OPS(acpi_battery_pm, NULL, acpi_battery_resume);
1296 static struct acpi_driver acpi_battery_driver = {
1298 .class = ACPI_BATTERY_CLASS,
1299 .ids = battery_device_ids,
1300 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1302 .add = acpi_battery_add,
1303 .remove = acpi_battery_remove,
1304 .notify = acpi_battery_notify,
1306 .drv.pm = &acpi_battery_pm,
1309 static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
1314 dmi_check_system(bat_dmi_table);
1316 if (battery_check_pmic) {
1317 for (i = 0; i < ARRAY_SIZE(acpi_battery_blacklist); i++)
1318 if (acpi_dev_present(acpi_battery_blacklist[i], "1", -1)) {
1319 pr_info(PREFIX ACPI_BATTERY_DEVICE_NAME
1320 ": found native %s PMIC, not loading\n",
1321 acpi_battery_blacklist[i]);
1326 result = acpi_bus_register_driver(&acpi_battery_driver);
1327 battery_driver_registered = (result == 0);
1330 static int __init acpi_battery_init(void)
1335 async_cookie = async_schedule(acpi_battery_init_async, NULL);
1339 static void __exit acpi_battery_exit(void)
1341 async_synchronize_cookie(async_cookie + 1);
1342 if (battery_driver_registered) {
1343 acpi_bus_unregister_driver(&acpi_battery_driver);
1344 battery_hook_exit();
1348 module_init(acpi_battery_init);
1349 module_exit(acpi_battery_exit);