1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * battery.c - ACPI Battery Driver (Revision: 2.0)
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
44 #define ACPI_BATTERY_STATE_CHARGE_LIMITING 0x8
46 #define MAX_STRING_LENGTH 64
48 MODULE_AUTHOR("Paul Diefenbaugh");
50 MODULE_DESCRIPTION("ACPI Battery Driver");
51 MODULE_LICENSE("GPL");
53 static async_cookie_t async_cookie;
54 static bool battery_driver_registered;
55 static int battery_bix_broken_package;
56 static int battery_notification_delay_ms;
57 static int battery_ac_is_broken;
58 static unsigned int cache_time = 1000;
59 module_param(cache_time, uint, 0644);
60 MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
62 static const struct acpi_device_id battery_device_ids[] = {
65 /* Microsoft Surface Go 3 */
71 MODULE_DEVICE_TABLE(acpi, battery_device_ids);
74 ACPI_BATTERY_ALARM_PRESENT,
75 ACPI_BATTERY_XINFO_PRESENT,
76 ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY,
77 /* On Lenovo Thinkpad models from 2010 and 2011, the power unit
78 * switches between mWh and mAh depending on whether the system
79 * is running on battery or not. When mAh is the unit, most
80 * reported values are incorrect and need to be adjusted by
81 * 10000/design_voltage. Verified on x201, t410, t410s, and x220.
82 * Pre-2010 and 2012 models appear to always report in mWh and
83 * are thus unaffected (tested with t42, t61, t500, x200, x300,
84 * and x230). Also, in mid-2012 Lenovo issued a BIOS update for
85 * the 2011 models that fixes the issue (tested on x220 with a
86 * post-1.29 BIOS), but as of Nov. 2012, no such update is
87 * available for the 2010 models.
89 ACPI_BATTERY_QUIRK_THINKPAD_MAH,
90 /* for batteries reporting current capacity with design capacity
91 * on a full charge, but showing degradation in full charge cap.
93 ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE,
98 struct mutex sysfs_lock;
99 struct power_supply *bat;
100 struct power_supply_desc bat_desc;
101 struct acpi_device *device;
102 struct notifier_block pm_nb;
103 struct list_head list;
104 unsigned long update_time;
110 int full_charge_capacity;
113 int design_capacity_warning;
114 int design_capacity_low;
116 int measurement_accuracy;
117 int max_sampling_time;
118 int min_sampling_time;
119 int max_averaging_interval;
120 int min_averaging_interval;
121 int capacity_granularity_1;
122 int capacity_granularity_2;
124 char model_number[MAX_STRING_LENGTH];
125 char serial_number[MAX_STRING_LENGTH];
126 char type[MAX_STRING_LENGTH];
127 char oem_info[MAX_STRING_LENGTH];
133 #define to_acpi_battery(x) power_supply_get_drvdata(x)
135 static inline int acpi_battery_present(struct acpi_battery *battery)
137 return battery->device->status.battery_present;
140 static int acpi_battery_technology(struct acpi_battery *battery)
142 if (!strcasecmp("NiCd", battery->type))
143 return POWER_SUPPLY_TECHNOLOGY_NiCd;
144 if (!strcasecmp("NiMH", battery->type))
145 return POWER_SUPPLY_TECHNOLOGY_NiMH;
146 if (!strcasecmp("LION", battery->type))
147 return POWER_SUPPLY_TECHNOLOGY_LION;
148 if (!strncasecmp("LI-ION", battery->type, 6))
149 return POWER_SUPPLY_TECHNOLOGY_LION;
150 if (!strcasecmp("LiP", battery->type))
151 return POWER_SUPPLY_TECHNOLOGY_LIPO;
152 return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
155 static int acpi_battery_get_state(struct acpi_battery *battery);
157 static int acpi_battery_is_charged(struct acpi_battery *battery)
159 /* charging, discharging, critical low or charge limited */
160 if (battery->state != 0)
163 /* battery not reporting charge */
164 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
165 battery->capacity_now == 0)
168 /* good batteries update full_charge as the batteries degrade */
169 if (battery->full_charge_capacity == battery->capacity_now)
172 /* fallback to using design values for broken batteries */
173 if (battery->design_capacity <= battery->capacity_now)
176 /* we don't do any sort of metric based on percentages */
180 static bool acpi_battery_is_degraded(struct acpi_battery *battery)
182 return ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity) &&
183 ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity) &&
184 battery->full_charge_capacity < battery->design_capacity;
187 static int acpi_battery_handle_discharging(struct acpi_battery *battery)
190 * Some devices wrongly report discharging if the battery's charge level
191 * was above the device's start charging threshold atm the AC adapter
192 * was plugged in and the device thus did not start a new charge cycle.
194 if ((battery_ac_is_broken || power_supply_is_system_supplied()) &&
195 battery->rate_now == 0)
196 return POWER_SUPPLY_STATUS_NOT_CHARGING;
198 return POWER_SUPPLY_STATUS_DISCHARGING;
201 static int acpi_battery_get_property(struct power_supply *psy,
202 enum power_supply_property psp,
203 union power_supply_propval *val)
205 int full_capacity = ACPI_BATTERY_VALUE_UNKNOWN, ret = 0;
206 struct acpi_battery *battery = to_acpi_battery(psy);
208 if (acpi_battery_present(battery)) {
209 /* run battery update only if it is present */
210 acpi_battery_get_state(battery);
211 } else if (psp != POWER_SUPPLY_PROP_PRESENT)
214 case POWER_SUPPLY_PROP_STATUS:
215 if (battery->state & ACPI_BATTERY_STATE_DISCHARGING)
216 val->intval = acpi_battery_handle_discharging(battery);
217 else if (battery->state & ACPI_BATTERY_STATE_CHARGING)
218 val->intval = POWER_SUPPLY_STATUS_CHARGING;
219 else if (battery->state & ACPI_BATTERY_STATE_CHARGE_LIMITING)
220 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
221 else if (acpi_battery_is_charged(battery))
222 val->intval = POWER_SUPPLY_STATUS_FULL;
224 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
226 case POWER_SUPPLY_PROP_PRESENT:
227 val->intval = acpi_battery_present(battery);
229 case POWER_SUPPLY_PROP_TECHNOLOGY:
230 val->intval = acpi_battery_technology(battery);
232 case POWER_SUPPLY_PROP_CYCLE_COUNT:
233 val->intval = battery->cycle_count;
235 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
236 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
239 val->intval = battery->design_voltage * 1000;
241 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
242 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
245 val->intval = battery->voltage_now * 1000;
247 case POWER_SUPPLY_PROP_CURRENT_NOW:
248 case POWER_SUPPLY_PROP_POWER_NOW:
249 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
252 val->intval = battery->rate_now * 1000;
254 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
255 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
256 if (!ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity))
259 val->intval = battery->design_capacity * 1000;
261 case POWER_SUPPLY_PROP_CHARGE_FULL:
262 case POWER_SUPPLY_PROP_ENERGY_FULL:
263 if (!ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity))
266 val->intval = battery->full_charge_capacity * 1000;
268 case POWER_SUPPLY_PROP_CHARGE_NOW:
269 case POWER_SUPPLY_PROP_ENERGY_NOW:
270 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
273 val->intval = battery->capacity_now * 1000;
275 case POWER_SUPPLY_PROP_CAPACITY:
276 if (ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity))
277 full_capacity = battery->full_charge_capacity;
278 else if (ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity))
279 full_capacity = battery->design_capacity;
281 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
282 full_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
285 val->intval = battery->capacity_now * 100/
288 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
289 if (battery->state & ACPI_BATTERY_STATE_CRITICAL)
290 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
291 else if (test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags) &&
292 (battery->capacity_now <= battery->alarm))
293 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
294 else if (acpi_battery_is_charged(battery))
295 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
297 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
299 case POWER_SUPPLY_PROP_MODEL_NAME:
300 val->strval = battery->model_number;
302 case POWER_SUPPLY_PROP_MANUFACTURER:
303 val->strval = battery->oem_info;
305 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
306 val->strval = battery->serial_number;
314 static const enum power_supply_property charge_battery_props[] = {
315 POWER_SUPPLY_PROP_STATUS,
316 POWER_SUPPLY_PROP_PRESENT,
317 POWER_SUPPLY_PROP_TECHNOLOGY,
318 POWER_SUPPLY_PROP_CYCLE_COUNT,
319 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
320 POWER_SUPPLY_PROP_VOLTAGE_NOW,
321 POWER_SUPPLY_PROP_CURRENT_NOW,
322 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
323 POWER_SUPPLY_PROP_CHARGE_FULL,
324 POWER_SUPPLY_PROP_CHARGE_NOW,
325 POWER_SUPPLY_PROP_CAPACITY,
326 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
327 POWER_SUPPLY_PROP_MODEL_NAME,
328 POWER_SUPPLY_PROP_MANUFACTURER,
329 POWER_SUPPLY_PROP_SERIAL_NUMBER,
332 static const enum power_supply_property charge_battery_full_cap_broken_props[] = {
333 POWER_SUPPLY_PROP_STATUS,
334 POWER_SUPPLY_PROP_PRESENT,
335 POWER_SUPPLY_PROP_TECHNOLOGY,
336 POWER_SUPPLY_PROP_CYCLE_COUNT,
337 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
338 POWER_SUPPLY_PROP_VOLTAGE_NOW,
339 POWER_SUPPLY_PROP_CURRENT_NOW,
340 POWER_SUPPLY_PROP_CHARGE_NOW,
341 POWER_SUPPLY_PROP_MODEL_NAME,
342 POWER_SUPPLY_PROP_MANUFACTURER,
343 POWER_SUPPLY_PROP_SERIAL_NUMBER,
346 static const enum power_supply_property energy_battery_props[] = {
347 POWER_SUPPLY_PROP_STATUS,
348 POWER_SUPPLY_PROP_PRESENT,
349 POWER_SUPPLY_PROP_TECHNOLOGY,
350 POWER_SUPPLY_PROP_CYCLE_COUNT,
351 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
352 POWER_SUPPLY_PROP_VOLTAGE_NOW,
353 POWER_SUPPLY_PROP_POWER_NOW,
354 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
355 POWER_SUPPLY_PROP_ENERGY_FULL,
356 POWER_SUPPLY_PROP_ENERGY_NOW,
357 POWER_SUPPLY_PROP_CAPACITY,
358 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
359 POWER_SUPPLY_PROP_MODEL_NAME,
360 POWER_SUPPLY_PROP_MANUFACTURER,
361 POWER_SUPPLY_PROP_SERIAL_NUMBER,
364 static const enum power_supply_property energy_battery_full_cap_broken_props[] = {
365 POWER_SUPPLY_PROP_STATUS,
366 POWER_SUPPLY_PROP_PRESENT,
367 POWER_SUPPLY_PROP_TECHNOLOGY,
368 POWER_SUPPLY_PROP_CYCLE_COUNT,
369 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
370 POWER_SUPPLY_PROP_VOLTAGE_NOW,
371 POWER_SUPPLY_PROP_POWER_NOW,
372 POWER_SUPPLY_PROP_ENERGY_NOW,
373 POWER_SUPPLY_PROP_MODEL_NAME,
374 POWER_SUPPLY_PROP_MANUFACTURER,
375 POWER_SUPPLY_PROP_SERIAL_NUMBER,
378 /* Battery Management */
379 struct acpi_offsets {
380 size_t offset; /* offset inside struct acpi_sbs_battery */
381 u8 mode; /* int or string? */
384 static const struct acpi_offsets state_offsets[] = {
385 {offsetof(struct acpi_battery, state), 0},
386 {offsetof(struct acpi_battery, rate_now), 0},
387 {offsetof(struct acpi_battery, capacity_now), 0},
388 {offsetof(struct acpi_battery, voltage_now), 0},
391 static const struct acpi_offsets info_offsets[] = {
392 {offsetof(struct acpi_battery, power_unit), 0},
393 {offsetof(struct acpi_battery, design_capacity), 0},
394 {offsetof(struct acpi_battery, full_charge_capacity), 0},
395 {offsetof(struct acpi_battery, technology), 0},
396 {offsetof(struct acpi_battery, design_voltage), 0},
397 {offsetof(struct acpi_battery, design_capacity_warning), 0},
398 {offsetof(struct acpi_battery, design_capacity_low), 0},
399 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
400 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
401 {offsetof(struct acpi_battery, model_number), 1},
402 {offsetof(struct acpi_battery, serial_number), 1},
403 {offsetof(struct acpi_battery, type), 1},
404 {offsetof(struct acpi_battery, oem_info), 1},
407 static const struct acpi_offsets extended_info_offsets[] = {
408 {offsetof(struct acpi_battery, revision), 0},
409 {offsetof(struct acpi_battery, power_unit), 0},
410 {offsetof(struct acpi_battery, design_capacity), 0},
411 {offsetof(struct acpi_battery, full_charge_capacity), 0},
412 {offsetof(struct acpi_battery, technology), 0},
413 {offsetof(struct acpi_battery, design_voltage), 0},
414 {offsetof(struct acpi_battery, design_capacity_warning), 0},
415 {offsetof(struct acpi_battery, design_capacity_low), 0},
416 {offsetof(struct acpi_battery, cycle_count), 0},
417 {offsetof(struct acpi_battery, measurement_accuracy), 0},
418 {offsetof(struct acpi_battery, max_sampling_time), 0},
419 {offsetof(struct acpi_battery, min_sampling_time), 0},
420 {offsetof(struct acpi_battery, max_averaging_interval), 0},
421 {offsetof(struct acpi_battery, min_averaging_interval), 0},
422 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
423 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
424 {offsetof(struct acpi_battery, model_number), 1},
425 {offsetof(struct acpi_battery, serial_number), 1},
426 {offsetof(struct acpi_battery, type), 1},
427 {offsetof(struct acpi_battery, oem_info), 1},
430 static int extract_package(struct acpi_battery *battery,
431 union acpi_object *package,
432 const struct acpi_offsets *offsets, int num)
435 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 u32 len = MAX_STRING_LENGTH;
447 switch (element->type) {
448 case ACPI_TYPE_BUFFER:
449 if (len > element->buffer.length + 1)
450 len = element->buffer.length + 1;
453 case ACPI_TYPE_STRING:
454 strscpy(ptr, element->string.pointer, len);
457 case ACPI_TYPE_INTEGER:
458 strscpy(ptr, (u8 *)&element->integer.value, sizeof(u64) + 1);
462 *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_handle_info(battery->device->handle,
477 "_STA evaluation failed\n");
484 static int extract_battery_info(const int use_bix,
485 struct acpi_battery *battery,
486 const struct acpi_buffer *buffer)
488 int result = -EFAULT;
490 if (use_bix && battery_bix_broken_package)
491 result = extract_package(battery, buffer->pointer,
492 extended_info_offsets + 1,
493 ARRAY_SIZE(extended_info_offsets) - 1);
495 result = extract_package(battery, buffer->pointer,
496 extended_info_offsets,
497 ARRAY_SIZE(extended_info_offsets));
499 result = extract_package(battery, buffer->pointer,
500 info_offsets, ARRAY_SIZE(info_offsets));
501 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
502 battery->full_charge_capacity = battery->design_capacity;
503 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) &&
504 battery->power_unit && battery->design_voltage) {
505 battery->design_capacity = battery->design_capacity *
506 10000 / battery->design_voltage;
507 battery->full_charge_capacity = battery->full_charge_capacity *
508 10000 / battery->design_voltage;
509 battery->design_capacity_warning =
510 battery->design_capacity_warning *
511 10000 / battery->design_voltage;
512 /* Curiously, design_capacity_low, unlike the rest of them,
515 /* capacity_granularity_* equal 1 on the systems tested, so
516 * it's impossible to tell if they would need an adjustment
517 * or not if their values were higher.
520 if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags) &&
521 battery->capacity_now > battery->full_charge_capacity)
522 battery->capacity_now = battery->full_charge_capacity;
527 static int acpi_battery_get_info(struct acpi_battery *battery)
529 const int xinfo = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
531 int result = -ENODEV;
533 if (!acpi_battery_present(battery))
537 for (use_bix = xinfo ? 1 : 0; use_bix >= 0; use_bix--) {
538 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
539 acpi_status status = AE_ERROR;
541 mutex_lock(&battery->lock);
542 status = acpi_evaluate_object(battery->device->handle,
543 use_bix ? "_BIX":"_BIF",
545 mutex_unlock(&battery->lock);
547 if (ACPI_FAILURE(status)) {
548 acpi_handle_info(battery->device->handle,
549 "%s evaluation failed: %s\n",
550 use_bix ? "_BIX":"_BIF",
551 acpi_format_exception(status));
553 result = extract_battery_info(use_bix,
557 kfree(buffer.pointer);
562 if (!result && !use_bix && xinfo)
563 pr_warn(FW_BUG "The _BIX method is broken, using _BIF.\n");
568 static int acpi_battery_get_state(struct acpi_battery *battery)
571 acpi_status status = 0;
572 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
574 if (!acpi_battery_present(battery))
577 if (battery->update_time &&
578 time_before(jiffies, battery->update_time +
579 msecs_to_jiffies(cache_time)))
582 mutex_lock(&battery->lock);
583 status = acpi_evaluate_object(battery->device->handle, "_BST",
585 mutex_unlock(&battery->lock);
587 if (ACPI_FAILURE(status)) {
588 acpi_handle_info(battery->device->handle,
589 "_BST evaluation failed: %s",
590 acpi_format_exception(status));
594 result = extract_package(battery, buffer.pointer,
595 state_offsets, ARRAY_SIZE(state_offsets));
596 battery->update_time = jiffies;
597 kfree(buffer.pointer);
599 /* For buggy DSDTs that report negative 16-bit values for either
600 * charging or discharging current and/or report 0 as 65536
603 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA &&
604 battery->rate_now != ACPI_BATTERY_VALUE_UNKNOWN &&
605 (s16)(battery->rate_now) < 0) {
606 battery->rate_now = abs((s16)battery->rate_now);
607 pr_warn_once(FW_BUG "(dis)charge rate invalid.\n");
610 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)
611 && battery->capacity_now >= 0 && battery->capacity_now <= 100)
612 battery->capacity_now = (battery->capacity_now *
613 battery->full_charge_capacity) / 100;
614 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) &&
615 battery->power_unit && battery->design_voltage) {
616 battery->capacity_now = battery->capacity_now *
617 10000 / battery->design_voltage;
619 if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags) &&
620 battery->capacity_now > battery->full_charge_capacity)
621 battery->capacity_now = battery->full_charge_capacity;
626 static int acpi_battery_set_alarm(struct acpi_battery *battery)
628 acpi_status status = 0;
630 if (!acpi_battery_present(battery) ||
631 !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags))
634 mutex_lock(&battery->lock);
635 status = acpi_execute_simple_method(battery->device->handle, "_BTP",
637 mutex_unlock(&battery->lock);
639 if (ACPI_FAILURE(status))
642 acpi_handle_debug(battery->device->handle, "Alarm set to %d\n",
648 static int acpi_battery_init_alarm(struct acpi_battery *battery)
650 /* See if alarms are supported, and if so, set default */
651 if (!acpi_has_method(battery->device->handle, "_BTP")) {
652 clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
655 set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
657 battery->alarm = battery->design_capacity_warning;
658 return acpi_battery_set_alarm(battery);
661 static ssize_t acpi_battery_alarm_show(struct device *dev,
662 struct device_attribute *attr,
665 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
667 return sysfs_emit(buf, "%d\n", battery->alarm * 1000);
670 static ssize_t acpi_battery_alarm_store(struct device *dev,
671 struct device_attribute *attr,
672 const char *buf, size_t count)
675 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
677 if (sscanf(buf, "%lu\n", &x) == 1)
678 battery->alarm = x/1000;
679 if (acpi_battery_present(battery))
680 acpi_battery_set_alarm(battery);
684 static struct device_attribute alarm_attr = {
685 .attr = {.name = "alarm", .mode = 0644},
686 .show = acpi_battery_alarm_show,
687 .store = acpi_battery_alarm_store,
690 static struct attribute *acpi_battery_attrs[] = {
694 ATTRIBUTE_GROUPS(acpi_battery);
697 * The Battery Hooking API
699 * This API is used inside other drivers that need to expose
700 * platform-specific behaviour within the generic driver in a
705 static LIST_HEAD(acpi_battery_list);
706 static LIST_HEAD(battery_hook_list);
707 static DEFINE_MUTEX(hook_mutex);
709 static void __battery_hook_unregister(struct acpi_battery_hook *hook, int lock)
711 struct acpi_battery *battery;
713 * In order to remove a hook, we first need to
714 * de-register all the batteries that are registered.
717 mutex_lock(&hook_mutex);
718 list_for_each_entry(battery, &acpi_battery_list, list) {
719 if (!hook->remove_battery(battery->bat, hook))
720 power_supply_changed(battery->bat);
722 list_del(&hook->list);
724 mutex_unlock(&hook_mutex);
725 pr_info("extension unregistered: %s\n", hook->name);
728 void battery_hook_unregister(struct acpi_battery_hook *hook)
730 __battery_hook_unregister(hook, 1);
732 EXPORT_SYMBOL_GPL(battery_hook_unregister);
734 void battery_hook_register(struct acpi_battery_hook *hook)
736 struct acpi_battery *battery;
738 mutex_lock(&hook_mutex);
739 INIT_LIST_HEAD(&hook->list);
740 list_add(&hook->list, &battery_hook_list);
742 * Now that the driver is registered, we need
743 * to notify the hook that a battery is available
744 * for each battery, so that the driver may add
747 list_for_each_entry(battery, &acpi_battery_list, list) {
748 if (hook->add_battery(battery->bat, hook)) {
750 * If a add-battery returns non-zero,
751 * the registration of the extension has failed,
752 * and we will not add it to the list of loaded
755 pr_err("extension failed to load: %s", hook->name);
756 __battery_hook_unregister(hook, 0);
760 power_supply_changed(battery->bat);
762 pr_info("new extension: %s\n", hook->name);
764 mutex_unlock(&hook_mutex);
766 EXPORT_SYMBOL_GPL(battery_hook_register);
768 static void devm_battery_hook_unregister(void *data)
770 struct acpi_battery_hook *hook = data;
772 battery_hook_unregister(hook);
775 int devm_battery_hook_register(struct device *dev, struct acpi_battery_hook *hook)
777 battery_hook_register(hook);
779 return devm_add_action_or_reset(dev, devm_battery_hook_unregister, hook);
781 EXPORT_SYMBOL_GPL(devm_battery_hook_register);
784 * This function gets called right after the battery sysfs
785 * attributes have been added, so that the drivers that
786 * define custom sysfs attributes can add their own.
788 static void battery_hook_add_battery(struct acpi_battery *battery)
790 struct acpi_battery_hook *hook_node, *tmp;
792 mutex_lock(&hook_mutex);
793 INIT_LIST_HEAD(&battery->list);
794 list_add(&battery->list, &acpi_battery_list);
796 * Since we added a new battery to the list, we need to
797 * iterate over the hooks and call add_battery for each
798 * hook that was registered. This usually happens
799 * when a battery gets hotplugged or initialized
800 * during the battery module initialization.
802 list_for_each_entry_safe(hook_node, tmp, &battery_hook_list, list) {
803 if (hook_node->add_battery(battery->bat, hook_node)) {
805 * The notification of the extensions has failed, to
806 * prevent further errors we will unload the extension.
808 pr_err("error in extension, unloading: %s",
810 __battery_hook_unregister(hook_node, 0);
813 mutex_unlock(&hook_mutex);
816 static void battery_hook_remove_battery(struct acpi_battery *battery)
818 struct acpi_battery_hook *hook;
820 mutex_lock(&hook_mutex);
822 * Before removing the hook, we need to remove all
823 * custom attributes from the battery.
825 list_for_each_entry(hook, &battery_hook_list, list) {
826 hook->remove_battery(battery->bat, hook);
828 /* Then, just remove the battery from the list */
829 list_del(&battery->list);
830 mutex_unlock(&hook_mutex);
833 static void __exit battery_hook_exit(void)
835 struct acpi_battery_hook *hook;
836 struct acpi_battery_hook *ptr;
838 * At this point, the acpi_bus_unregister_driver()
839 * has called remove for all batteries. We just
840 * need to remove the hooks.
842 list_for_each_entry_safe(hook, ptr, &battery_hook_list, list) {
843 __battery_hook_unregister(hook, 1);
845 mutex_destroy(&hook_mutex);
848 static int sysfs_add_battery(struct acpi_battery *battery)
850 struct power_supply_config psy_cfg = {
852 .attr_grp = acpi_battery_groups,
854 bool full_cap_broken = false;
856 if (!ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity) &&
857 !ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity))
858 full_cap_broken = true;
860 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) {
861 if (full_cap_broken) {
862 battery->bat_desc.properties =
863 charge_battery_full_cap_broken_props;
864 battery->bat_desc.num_properties =
865 ARRAY_SIZE(charge_battery_full_cap_broken_props);
867 battery->bat_desc.properties = charge_battery_props;
868 battery->bat_desc.num_properties =
869 ARRAY_SIZE(charge_battery_props);
872 if (full_cap_broken) {
873 battery->bat_desc.properties =
874 energy_battery_full_cap_broken_props;
875 battery->bat_desc.num_properties =
876 ARRAY_SIZE(energy_battery_full_cap_broken_props);
878 battery->bat_desc.properties = energy_battery_props;
879 battery->bat_desc.num_properties =
880 ARRAY_SIZE(energy_battery_props);
884 battery->bat_desc.name = acpi_device_bid(battery->device);
885 battery->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY;
886 battery->bat_desc.get_property = acpi_battery_get_property;
888 battery->bat = power_supply_register_no_ws(&battery->device->dev,
889 &battery->bat_desc, &psy_cfg);
891 if (IS_ERR(battery->bat)) {
892 int result = PTR_ERR(battery->bat);
897 battery_hook_add_battery(battery);
901 static void sysfs_remove_battery(struct acpi_battery *battery)
903 mutex_lock(&battery->sysfs_lock);
905 mutex_unlock(&battery->sysfs_lock);
908 battery_hook_remove_battery(battery);
909 power_supply_unregister(battery->bat);
911 mutex_unlock(&battery->sysfs_lock);
914 static void find_battery(const struct dmi_header *dm, void *private)
916 struct acpi_battery *battery = (struct acpi_battery *)private;
917 /* Note: the hardcoded offsets below have been extracted from
918 * the source code of dmidecode.
920 if (dm->type == DMI_ENTRY_PORTABLE_BATTERY && dm->length >= 8) {
921 const u8 *dmi_data = (const u8 *)(dm + 1);
922 int dmi_capacity = get_unaligned((const u16 *)(dmi_data + 6));
924 if (dm->length >= 18)
925 dmi_capacity *= dmi_data[17];
926 if (battery->design_capacity * battery->design_voltage / 1000
928 battery->design_capacity * 10 == dmi_capacity)
929 set_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH,
935 * According to the ACPI spec, some kinds of primary batteries can
936 * report percentage battery remaining capacity directly to OS.
937 * In this case, it reports the Last Full Charged Capacity == 100
938 * and BatteryPresentRate == 0xFFFFFFFF.
940 * Now we found some battery reports percentage remaining capacity
941 * even if it's rechargeable.
942 * https://bugzilla.kernel.org/show_bug.cgi?id=15979
944 * Handle this correctly so that they won't break userspace.
946 static void acpi_battery_quirks(struct acpi_battery *battery)
948 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
951 if (battery->full_charge_capacity == 100 &&
952 battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN &&
953 battery->capacity_now >= 0 && battery->capacity_now <= 100) {
954 set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags);
955 battery->full_charge_capacity = battery->design_capacity;
956 battery->capacity_now = (battery->capacity_now *
957 battery->full_charge_capacity) / 100;
960 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags))
963 if (battery->power_unit && dmi_name_in_vendors("LENOVO")) {
966 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
967 if (s && !strncasecmp(s, "ThinkPad", 8)) {
968 dmi_walk(find_battery, battery);
969 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH,
971 battery->design_voltage) {
972 battery->design_capacity =
973 battery->design_capacity *
974 10000 / battery->design_voltage;
975 battery->full_charge_capacity =
976 battery->full_charge_capacity *
977 10000 / battery->design_voltage;
978 battery->design_capacity_warning =
979 battery->design_capacity_warning *
980 10000 / battery->design_voltage;
981 battery->capacity_now = battery->capacity_now *
982 10000 / battery->design_voltage;
987 if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags))
990 if (acpi_battery_is_degraded(battery) &&
991 battery->capacity_now > battery->full_charge_capacity) {
992 set_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags);
993 battery->capacity_now = battery->full_charge_capacity;
997 static int acpi_battery_update(struct acpi_battery *battery, bool resume)
999 int result = acpi_battery_get_status(battery);
1004 if (!acpi_battery_present(battery)) {
1005 sysfs_remove_battery(battery);
1006 battery->update_time = 0;
1013 if (!battery->update_time) {
1014 result = acpi_battery_get_info(battery);
1017 acpi_battery_init_alarm(battery);
1020 result = acpi_battery_get_state(battery);
1023 acpi_battery_quirks(battery);
1025 if (!battery->bat) {
1026 result = sysfs_add_battery(battery);
1032 * Wakeup the system if battery is critical low
1033 * or lower than the alarm level
1035 if ((battery->state & ACPI_BATTERY_STATE_CRITICAL) ||
1036 (test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags) &&
1037 (battery->capacity_now <= battery->alarm)))
1038 acpi_pm_wakeup_event(&battery->device->dev);
1043 static void acpi_battery_refresh(struct acpi_battery *battery)
1050 power_unit = battery->power_unit;
1052 acpi_battery_get_info(battery);
1054 if (power_unit == battery->power_unit)
1057 /* The battery has changed its reporting units. */
1058 sysfs_remove_battery(battery);
1059 sysfs_add_battery(battery);
1062 /* Driver Interface */
1063 static void acpi_battery_notify(acpi_handle handle, u32 event, void *data)
1065 struct acpi_device *device = data;
1066 struct acpi_battery *battery = acpi_driver_data(device);
1067 struct power_supply *old;
1073 * On Acer Aspire V5-573G notifications are sometimes triggered too
1074 * early. For example, when AC is unplugged and notification is
1075 * triggered, battery state is still reported as "Full", and changes to
1076 * "Discharging" only after short delay, without any notification.
1078 if (battery_notification_delay_ms > 0)
1079 msleep(battery_notification_delay_ms);
1080 if (event == ACPI_BATTERY_NOTIFY_INFO)
1081 acpi_battery_refresh(battery);
1082 acpi_battery_update(battery, false);
1083 acpi_bus_generate_netlink_event(device->pnp.device_class,
1084 dev_name(&device->dev), event,
1085 acpi_battery_present(battery));
1086 acpi_notifier_call_chain(device, event, acpi_battery_present(battery));
1087 /* acpi_battery_update could remove power_supply object */
1088 if (old && battery->bat)
1089 power_supply_changed(battery->bat);
1092 static int battery_notify(struct notifier_block *nb,
1093 unsigned long mode, void *_unused)
1095 struct acpi_battery *battery = container_of(nb, struct acpi_battery,
1100 case PM_POST_HIBERNATION:
1101 case PM_POST_SUSPEND:
1102 if (!acpi_battery_present(battery))
1106 acpi_battery_refresh(battery);
1108 result = acpi_battery_get_info(battery);
1112 result = sysfs_add_battery(battery);
1117 acpi_battery_init_alarm(battery);
1118 acpi_battery_get_state(battery);
1126 battery_bix_broken_package_quirk(const struct dmi_system_id *d)
1128 battery_bix_broken_package = 1;
1133 battery_notification_delay_quirk(const struct dmi_system_id *d)
1135 battery_notification_delay_ms = 1000;
1140 battery_ac_is_broken_quirk(const struct dmi_system_id *d)
1142 battery_ac_is_broken = 1;
1146 static const struct dmi_system_id bat_dmi_table[] __initconst = {
1149 .callback = battery_bix_broken_package_quirk,
1151 DMI_MATCH(DMI_SYS_VENDOR, "NEC"),
1152 DMI_MATCH(DMI_PRODUCT_NAME, "PC-LZ750LS"),
1156 /* Acer Aspire V5-573G */
1157 .callback = battery_notification_delay_quirk,
1159 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
1160 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire V5-573G"),
1164 /* Point of View mobii wintab p800w */
1165 .callback = battery_ac_is_broken_quirk,
1167 DMI_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
1168 DMI_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
1169 DMI_MATCH(DMI_BIOS_VERSION, "3BAIR1013"),
1170 /* Above matches are too generic, add bios-date match */
1171 DMI_MATCH(DMI_BIOS_DATE, "08/22/2014"),
1175 /* Microsoft Surface Go 3 */
1176 .callback = battery_notification_delay_quirk,
1178 DMI_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
1179 DMI_MATCH(DMI_PRODUCT_NAME, "Surface Go 3"),
1186 * Some machines'(E,G Lenovo Z480) ECs are not stable
1187 * during boot up and this causes battery driver fails to be
1188 * probed due to failure of getting battery information
1189 * from EC sometimes. After several retries, the operation
1190 * may work. So add retry code here and 20ms sleep between
1193 static int acpi_battery_update_retry(struct acpi_battery *battery)
1197 for (retry = 5; retry; retry--) {
1198 ret = acpi_battery_update(battery, false);
1207 static int acpi_battery_add(struct acpi_device *device)
1210 struct acpi_battery *battery = NULL;
1215 if (device->dep_unmet)
1216 return -EPROBE_DEFER;
1218 battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
1221 battery->device = device;
1222 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
1223 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
1224 device->driver_data = battery;
1225 mutex_init(&battery->lock);
1226 mutex_init(&battery->sysfs_lock);
1227 if (acpi_has_method(battery->device->handle, "_BIX"))
1228 set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
1230 result = acpi_battery_update_retry(battery);
1234 pr_info("Slot [%s] (battery %s)\n", acpi_device_bid(device),
1235 device->status.battery_present ? "present" : "absent");
1237 battery->pm_nb.notifier_call = battery_notify;
1238 register_pm_notifier(&battery->pm_nb);
1240 device_init_wakeup(&device->dev, 1);
1242 result = acpi_dev_install_notify_handler(device, ACPI_ALL_NOTIFY,
1243 acpi_battery_notify, device);
1250 device_init_wakeup(&device->dev, 0);
1251 unregister_pm_notifier(&battery->pm_nb);
1253 sysfs_remove_battery(battery);
1254 mutex_destroy(&battery->lock);
1255 mutex_destroy(&battery->sysfs_lock);
1261 static void acpi_battery_remove(struct acpi_device *device)
1263 struct acpi_battery *battery = NULL;
1265 if (!device || !acpi_driver_data(device))
1268 battery = acpi_driver_data(device);
1270 acpi_dev_remove_notify_handler(device, ACPI_ALL_NOTIFY,
1271 acpi_battery_notify);
1273 device_init_wakeup(&device->dev, 0);
1274 unregister_pm_notifier(&battery->pm_nb);
1275 sysfs_remove_battery(battery);
1277 mutex_destroy(&battery->lock);
1278 mutex_destroy(&battery->sysfs_lock);
1282 #ifdef CONFIG_PM_SLEEP
1283 /* this is needed to learn about changes made in suspended state */
1284 static int acpi_battery_resume(struct device *dev)
1286 struct acpi_battery *battery;
1291 battery = acpi_driver_data(to_acpi_device(dev));
1295 battery->update_time = 0;
1296 acpi_battery_update(battery, true);
1300 #define acpi_battery_resume NULL
1303 static SIMPLE_DEV_PM_OPS(acpi_battery_pm, NULL, acpi_battery_resume);
1305 static struct acpi_driver acpi_battery_driver = {
1307 .class = ACPI_BATTERY_CLASS,
1308 .ids = battery_device_ids,
1310 .add = acpi_battery_add,
1311 .remove = acpi_battery_remove,
1313 .drv.pm = &acpi_battery_pm,
1316 static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
1320 if (acpi_quirk_skip_acpi_ac_and_battery())
1323 dmi_check_system(bat_dmi_table);
1325 result = acpi_bus_register_driver(&acpi_battery_driver);
1326 battery_driver_registered = (result == 0);
1329 static int __init acpi_battery_init(void)
1334 async_cookie = async_schedule(acpi_battery_init_async, NULL);
1338 static void __exit acpi_battery_exit(void)
1340 async_synchronize_cookie(async_cookie + 1);
1341 if (battery_driver_registered) {
1342 acpi_bus_unregister_driver(&acpi_battery_driver);
1343 battery_hook_exit();
1347 module_init(acpi_battery_init);
1348 module_exit(acpi_battery_exit);