2 * battery.c - ACPI Battery Driver (Revision: 2.0)
4 * Copyright (C) 2007 Alexey Starikovskiy <astarikovskiy@suse.de>
5 * Copyright (C) 2004-2007 Vladimir Lebedev <vladimir.p.lebedev@intel.com>
6 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
7 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or (at
14 * your option) any later version.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/types.h>
28 #include <linux/jiffies.h>
29 #include <linux/async.h>
30 #include <linux/dmi.h>
31 #include <linux/delay.h>
32 #include <linux/slab.h>
33 #include <linux/suspend.h>
34 #include <asm/unaligned.h>
36 #ifdef CONFIG_ACPI_PROCFS_POWER
37 #include <linux/proc_fs.h>
38 #include <linux/seq_file.h>
39 #include <linux/uaccess.h>
42 #include <linux/acpi.h>
43 #include <linux/power_supply.h>
47 #define PREFIX "ACPI: "
49 #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
51 #define ACPI_BATTERY_DEVICE_NAME "Battery"
53 /* Battery power unit: 0 means mW, 1 means mA */
54 #define ACPI_BATTERY_POWER_UNIT_MA 1
56 #define ACPI_BATTERY_STATE_DISCHARGING 0x1
57 #define ACPI_BATTERY_STATE_CHARGING 0x2
58 #define ACPI_BATTERY_STATE_CRITICAL 0x4
60 #define _COMPONENT ACPI_BATTERY_COMPONENT
62 ACPI_MODULE_NAME("battery");
64 MODULE_AUTHOR("Paul Diefenbaugh");
65 MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
66 MODULE_DESCRIPTION("ACPI Battery Driver");
67 MODULE_LICENSE("GPL");
69 static async_cookie_t async_cookie;
70 static bool battery_driver_registered;
71 static int battery_bix_broken_package;
72 static int battery_notification_delay_ms;
73 static unsigned int cache_time = 1000;
74 module_param(cache_time, uint, 0644);
75 MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
77 #ifdef CONFIG_ACPI_PROCFS_POWER
78 extern struct proc_dir_entry *acpi_lock_battery_dir(void);
79 extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
81 enum acpi_battery_files {
85 ACPI_BATTERY_NUMFILES,
90 static const struct acpi_device_id battery_device_ids[] = {
93 /* Microsoft Surface Go 3 */
99 MODULE_DEVICE_TABLE(acpi, battery_device_ids);
101 /* Lists of PMIC ACPI HIDs with an (often better) native battery driver */
102 static const char * const acpi_battery_blacklist[] = {
103 "INT33F4", /* X-Powers AXP288 PMIC */
107 ACPI_BATTERY_ALARM_PRESENT,
108 ACPI_BATTERY_XINFO_PRESENT,
109 ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY,
110 /* On Lenovo Thinkpad models from 2010 and 2011, the power unit
111 switches between mWh and mAh depending on whether the system
112 is running on battery or not. When mAh is the unit, most
113 reported values are incorrect and need to be adjusted by
114 10000/design_voltage. Verified on x201, t410, t410s, and x220.
115 Pre-2010 and 2012 models appear to always report in mWh and
116 are thus unaffected (tested with t42, t61, t500, x200, x300,
117 and x230). Also, in mid-2012 Lenovo issued a BIOS update for
118 the 2011 models that fixes the issue (tested on x220 with a
119 post-1.29 BIOS), but as of Nov. 2012, no such update is
120 available for the 2010 models. */
121 ACPI_BATTERY_QUIRK_THINKPAD_MAH,
124 struct acpi_battery {
126 struct mutex sysfs_lock;
127 struct power_supply *bat;
128 struct power_supply_desc bat_desc;
129 struct acpi_device *device;
130 struct notifier_block pm_nb;
131 unsigned long update_time;
137 int full_charge_capacity;
140 int design_capacity_warning;
141 int design_capacity_low;
143 int measurement_accuracy;
144 int max_sampling_time;
145 int min_sampling_time;
146 int max_averaging_interval;
147 int min_averaging_interval;
148 int capacity_granularity_1;
149 int capacity_granularity_2;
151 char model_number[32];
152 char serial_number[32];
160 #define to_acpi_battery(x) power_supply_get_drvdata(x)
162 static inline int acpi_battery_present(struct acpi_battery *battery)
164 return battery->device->status.battery_present;
167 static int acpi_battery_technology(struct acpi_battery *battery)
169 if (!strcasecmp("NiCd", battery->type))
170 return POWER_SUPPLY_TECHNOLOGY_NiCd;
171 if (!strcasecmp("NiMH", battery->type))
172 return POWER_SUPPLY_TECHNOLOGY_NiMH;
173 if (!strcasecmp("LION", battery->type))
174 return POWER_SUPPLY_TECHNOLOGY_LION;
175 if (!strncasecmp("LI-ION", battery->type, 6))
176 return POWER_SUPPLY_TECHNOLOGY_LION;
177 if (!strcasecmp("LiP", battery->type))
178 return POWER_SUPPLY_TECHNOLOGY_LIPO;
179 return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
182 static int acpi_battery_get_state(struct acpi_battery *battery);
184 static int acpi_battery_is_charged(struct acpi_battery *battery)
186 /* charging, discharging or critical low */
187 if (battery->state != 0)
190 /* battery not reporting charge */
191 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
192 battery->capacity_now == 0)
195 /* good batteries update full_charge as the batteries degrade */
196 if (battery->full_charge_capacity == battery->capacity_now)
199 /* fallback to using design values for broken batteries */
200 if (battery->design_capacity <= battery->capacity_now)
203 /* we don't do any sort of metric based on percentages */
207 static int acpi_battery_get_property(struct power_supply *psy,
208 enum power_supply_property psp,
209 union power_supply_propval *val)
212 struct acpi_battery *battery = to_acpi_battery(psy);
214 if (acpi_battery_present(battery)) {
215 /* run battery update only if it is present */
216 acpi_battery_get_state(battery);
217 } else if (psp != POWER_SUPPLY_PROP_PRESENT)
220 case POWER_SUPPLY_PROP_STATUS:
221 if (battery->state & ACPI_BATTERY_STATE_DISCHARGING)
222 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
223 else if (battery->state & ACPI_BATTERY_STATE_CHARGING)
224 val->intval = POWER_SUPPLY_STATUS_CHARGING;
225 else if (acpi_battery_is_charged(battery))
226 val->intval = POWER_SUPPLY_STATUS_FULL;
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 (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
263 val->intval = battery->design_capacity * 1000;
265 case POWER_SUPPLY_PROP_CHARGE_FULL:
266 case POWER_SUPPLY_PROP_ENERGY_FULL:
267 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
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 (battery->capacity_now && battery->full_charge_capacity)
281 val->intval = battery->capacity_now * 100/
282 battery->full_charge_capacity;
286 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
287 if (battery->state & ACPI_BATTERY_STATE_CRITICAL)
288 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
289 else if (test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags) &&
290 (battery->capacity_now <= battery->alarm))
291 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
292 else if (acpi_battery_is_charged(battery))
293 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
295 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
297 case POWER_SUPPLY_PROP_MODEL_NAME:
298 val->strval = battery->model_number;
300 case POWER_SUPPLY_PROP_MANUFACTURER:
301 val->strval = battery->oem_info;
303 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
304 val->strval = battery->serial_number;
312 static enum power_supply_property charge_battery_props[] = {
313 POWER_SUPPLY_PROP_STATUS,
314 POWER_SUPPLY_PROP_PRESENT,
315 POWER_SUPPLY_PROP_TECHNOLOGY,
316 POWER_SUPPLY_PROP_CYCLE_COUNT,
317 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
318 POWER_SUPPLY_PROP_VOLTAGE_NOW,
319 POWER_SUPPLY_PROP_CURRENT_NOW,
320 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
321 POWER_SUPPLY_PROP_CHARGE_FULL,
322 POWER_SUPPLY_PROP_CHARGE_NOW,
323 POWER_SUPPLY_PROP_CAPACITY,
324 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
325 POWER_SUPPLY_PROP_MODEL_NAME,
326 POWER_SUPPLY_PROP_MANUFACTURER,
327 POWER_SUPPLY_PROP_SERIAL_NUMBER,
330 static enum power_supply_property energy_battery_props[] = {
331 POWER_SUPPLY_PROP_STATUS,
332 POWER_SUPPLY_PROP_PRESENT,
333 POWER_SUPPLY_PROP_TECHNOLOGY,
334 POWER_SUPPLY_PROP_CYCLE_COUNT,
335 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
336 POWER_SUPPLY_PROP_VOLTAGE_NOW,
337 POWER_SUPPLY_PROP_POWER_NOW,
338 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
339 POWER_SUPPLY_PROP_ENERGY_FULL,
340 POWER_SUPPLY_PROP_ENERGY_NOW,
341 POWER_SUPPLY_PROP_CAPACITY,
342 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
343 POWER_SUPPLY_PROP_MODEL_NAME,
344 POWER_SUPPLY_PROP_MANUFACTURER,
345 POWER_SUPPLY_PROP_SERIAL_NUMBER,
348 /* --------------------------------------------------------------------------
350 -------------------------------------------------------------------------- */
351 struct acpi_offsets {
352 size_t offset; /* offset inside struct acpi_sbs_battery */
353 u8 mode; /* int or string? */
356 static const struct acpi_offsets state_offsets[] = {
357 {offsetof(struct acpi_battery, state), 0},
358 {offsetof(struct acpi_battery, rate_now), 0},
359 {offsetof(struct acpi_battery, capacity_now), 0},
360 {offsetof(struct acpi_battery, voltage_now), 0},
363 static const struct acpi_offsets info_offsets[] = {
364 {offsetof(struct acpi_battery, power_unit), 0},
365 {offsetof(struct acpi_battery, design_capacity), 0},
366 {offsetof(struct acpi_battery, full_charge_capacity), 0},
367 {offsetof(struct acpi_battery, technology), 0},
368 {offsetof(struct acpi_battery, design_voltage), 0},
369 {offsetof(struct acpi_battery, design_capacity_warning), 0},
370 {offsetof(struct acpi_battery, design_capacity_low), 0},
371 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
372 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
373 {offsetof(struct acpi_battery, model_number), 1},
374 {offsetof(struct acpi_battery, serial_number), 1},
375 {offsetof(struct acpi_battery, type), 1},
376 {offsetof(struct acpi_battery, oem_info), 1},
379 static const struct acpi_offsets extended_info_offsets[] = {
380 {offsetof(struct acpi_battery, revision), 0},
381 {offsetof(struct acpi_battery, power_unit), 0},
382 {offsetof(struct acpi_battery, design_capacity), 0},
383 {offsetof(struct acpi_battery, full_charge_capacity), 0},
384 {offsetof(struct acpi_battery, technology), 0},
385 {offsetof(struct acpi_battery, design_voltage), 0},
386 {offsetof(struct acpi_battery, design_capacity_warning), 0},
387 {offsetof(struct acpi_battery, design_capacity_low), 0},
388 {offsetof(struct acpi_battery, cycle_count), 0},
389 {offsetof(struct acpi_battery, measurement_accuracy), 0},
390 {offsetof(struct acpi_battery, max_sampling_time), 0},
391 {offsetof(struct acpi_battery, min_sampling_time), 0},
392 {offsetof(struct acpi_battery, max_averaging_interval), 0},
393 {offsetof(struct acpi_battery, min_averaging_interval), 0},
394 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
395 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
396 {offsetof(struct acpi_battery, model_number), 1},
397 {offsetof(struct acpi_battery, serial_number), 1},
398 {offsetof(struct acpi_battery, type), 1},
399 {offsetof(struct acpi_battery, oem_info), 1},
402 static int extract_package(struct acpi_battery *battery,
403 union acpi_object *package,
404 const struct acpi_offsets *offsets, int num)
407 union acpi_object *element;
408 if (package->type != ACPI_TYPE_PACKAGE)
410 for (i = 0; i < num; ++i) {
411 if (package->package.count <= i)
413 element = &package->package.elements[i];
414 if (offsets[i].mode) {
415 u8 *ptr = (u8 *)battery + offsets[i].offset;
416 if (element->type == ACPI_TYPE_STRING ||
417 element->type == ACPI_TYPE_BUFFER)
418 strncpy(ptr, element->string.pointer, 32);
419 else if (element->type == ACPI_TYPE_INTEGER) {
420 strncpy(ptr, (u8 *)&element->integer.value,
422 ptr[sizeof(u64)] = 0;
424 *ptr = 0; /* don't have value */
426 int *x = (int *)((u8 *)battery + offsets[i].offset);
427 *x = (element->type == ACPI_TYPE_INTEGER) ?
428 element->integer.value : -1;
434 static int acpi_battery_get_status(struct acpi_battery *battery)
436 if (acpi_bus_get_status(battery->device)) {
437 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
444 static int extract_battery_info(const int use_bix,
445 struct acpi_battery *battery,
446 const struct acpi_buffer *buffer)
448 int result = -EFAULT;
450 if (use_bix && battery_bix_broken_package)
451 result = extract_package(battery, buffer->pointer,
452 extended_info_offsets + 1,
453 ARRAY_SIZE(extended_info_offsets) - 1);
455 result = extract_package(battery, buffer->pointer,
456 extended_info_offsets,
457 ARRAY_SIZE(extended_info_offsets));
459 result = extract_package(battery, buffer->pointer,
460 info_offsets, ARRAY_SIZE(info_offsets));
461 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
462 battery->full_charge_capacity = battery->design_capacity;
463 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) &&
464 battery->power_unit && battery->design_voltage) {
465 battery->design_capacity = battery->design_capacity *
466 10000 / battery->design_voltage;
467 battery->full_charge_capacity = battery->full_charge_capacity *
468 10000 / battery->design_voltage;
469 battery->design_capacity_warning =
470 battery->design_capacity_warning *
471 10000 / battery->design_voltage;
472 /* Curiously, design_capacity_low, unlike the rest of them,
474 /* capacity_granularity_* equal 1 on the systems tested, so
475 it's impossible to tell if they would need an adjustment
476 or not if their values were higher. */
481 static int acpi_battery_get_info(struct acpi_battery *battery)
483 const int xinfo = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
485 int result = -ENODEV;
487 if (!acpi_battery_present(battery))
491 for (use_bix = xinfo ? 1 : 0; use_bix >= 0; use_bix--) {
492 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
493 acpi_status status = AE_ERROR;
495 mutex_lock(&battery->lock);
496 status = acpi_evaluate_object(battery->device->handle,
497 use_bix ? "_BIX":"_BIF",
499 mutex_unlock(&battery->lock);
501 if (ACPI_FAILURE(status)) {
502 ACPI_EXCEPTION((AE_INFO, status, "Evaluating %s",
503 use_bix ? "_BIX":"_BIF"));
505 result = extract_battery_info(use_bix,
509 kfree(buffer.pointer);
514 if (!result && !use_bix && xinfo)
515 pr_warn(FW_BUG "The _BIX method is broken, using _BIF.\n");
520 static int acpi_battery_get_state(struct acpi_battery *battery)
523 acpi_status status = 0;
524 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
526 if (!acpi_battery_present(battery))
529 if (battery->update_time &&
530 time_before(jiffies, battery->update_time +
531 msecs_to_jiffies(cache_time)))
534 mutex_lock(&battery->lock);
535 status = acpi_evaluate_object(battery->device->handle, "_BST",
537 mutex_unlock(&battery->lock);
539 if (ACPI_FAILURE(status)) {
540 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
544 result = extract_package(battery, buffer.pointer,
545 state_offsets, ARRAY_SIZE(state_offsets));
546 battery->update_time = jiffies;
547 kfree(buffer.pointer);
549 /* For buggy DSDTs that report negative 16-bit values for either
550 * charging or discharging current and/or report 0 as 65536
553 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA &&
554 battery->rate_now != ACPI_BATTERY_VALUE_UNKNOWN &&
555 (s16)(battery->rate_now) < 0) {
556 battery->rate_now = abs((s16)battery->rate_now);
557 printk_once(KERN_WARNING FW_BUG
558 "battery: (dis)charge rate invalid.\n");
561 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)
562 && battery->capacity_now >= 0 && battery->capacity_now <= 100)
563 battery->capacity_now = (battery->capacity_now *
564 battery->full_charge_capacity) / 100;
565 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) &&
566 battery->power_unit && battery->design_voltage) {
567 battery->capacity_now = battery->capacity_now *
568 10000 / battery->design_voltage;
573 static int acpi_battery_set_alarm(struct acpi_battery *battery)
575 acpi_status status = 0;
577 if (!acpi_battery_present(battery) ||
578 !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags))
581 mutex_lock(&battery->lock);
582 status = acpi_execute_simple_method(battery->device->handle, "_BTP",
584 mutex_unlock(&battery->lock);
586 if (ACPI_FAILURE(status))
589 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm));
593 static int acpi_battery_init_alarm(struct acpi_battery *battery)
595 /* See if alarms are supported, and if so, set default */
596 if (!acpi_has_method(battery->device->handle, "_BTP")) {
597 clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
600 set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
602 battery->alarm = battery->design_capacity_warning;
603 return acpi_battery_set_alarm(battery);
606 static ssize_t acpi_battery_alarm_show(struct device *dev,
607 struct device_attribute *attr,
610 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
611 return sprintf(buf, "%d\n", battery->alarm * 1000);
614 static ssize_t acpi_battery_alarm_store(struct device *dev,
615 struct device_attribute *attr,
616 const char *buf, size_t count)
619 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
620 if (sscanf(buf, "%lu\n", &x) == 1)
621 battery->alarm = x/1000;
622 if (acpi_battery_present(battery))
623 acpi_battery_set_alarm(battery);
627 static const struct device_attribute alarm_attr = {
628 .attr = {.name = "alarm", .mode = 0644},
629 .show = acpi_battery_alarm_show,
630 .store = acpi_battery_alarm_store,
633 static int sysfs_add_battery(struct acpi_battery *battery)
635 struct power_supply_config psy_cfg = { .drv_data = battery, };
637 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) {
638 battery->bat_desc.properties = charge_battery_props;
639 battery->bat_desc.num_properties =
640 ARRAY_SIZE(charge_battery_props);
642 battery->bat_desc.properties = energy_battery_props;
643 battery->bat_desc.num_properties =
644 ARRAY_SIZE(energy_battery_props);
647 battery->bat_desc.name = acpi_device_bid(battery->device);
648 battery->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY;
649 battery->bat_desc.get_property = acpi_battery_get_property;
651 battery->bat = power_supply_register_no_ws(&battery->device->dev,
652 &battery->bat_desc, &psy_cfg);
654 if (IS_ERR(battery->bat)) {
655 int result = PTR_ERR(battery->bat);
660 return device_create_file(&battery->bat->dev, &alarm_attr);
663 static void sysfs_remove_battery(struct acpi_battery *battery)
665 mutex_lock(&battery->sysfs_lock);
667 mutex_unlock(&battery->sysfs_lock);
671 device_remove_file(&battery->bat->dev, &alarm_attr);
672 power_supply_unregister(battery->bat);
674 mutex_unlock(&battery->sysfs_lock);
677 static void find_battery(const struct dmi_header *dm, void *private)
679 struct acpi_battery *battery = (struct acpi_battery *)private;
680 /* Note: the hardcoded offsets below have been extracted from
681 the source code of dmidecode. */
682 if (dm->type == DMI_ENTRY_PORTABLE_BATTERY && dm->length >= 8) {
683 const u8 *dmi_data = (const u8 *)(dm + 1);
684 int dmi_capacity = get_unaligned((const u16 *)(dmi_data + 6));
685 if (dm->length >= 18)
686 dmi_capacity *= dmi_data[17];
687 if (battery->design_capacity * battery->design_voltage / 1000
689 battery->design_capacity * 10 == dmi_capacity)
690 set_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH,
696 * According to the ACPI spec, some kinds of primary batteries can
697 * report percentage battery remaining capacity directly to OS.
698 * In this case, it reports the Last Full Charged Capacity == 100
699 * and BatteryPresentRate == 0xFFFFFFFF.
701 * Now we found some battery reports percentage remaining capacity
702 * even if it's rechargeable.
703 * https://bugzilla.kernel.org/show_bug.cgi?id=15979
705 * Handle this correctly so that they won't break userspace.
707 static void acpi_battery_quirks(struct acpi_battery *battery)
709 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
712 if (battery->full_charge_capacity == 100 &&
713 battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN &&
714 battery->capacity_now >= 0 && battery->capacity_now <= 100) {
715 set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags);
716 battery->full_charge_capacity = battery->design_capacity;
717 battery->capacity_now = (battery->capacity_now *
718 battery->full_charge_capacity) / 100;
721 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags))
724 if (battery->power_unit && dmi_name_in_vendors("LENOVO")) {
726 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
727 if (s && !strncasecmp(s, "ThinkPad", 8)) {
728 dmi_walk(find_battery, battery);
729 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH,
731 battery->design_voltage) {
732 battery->design_capacity =
733 battery->design_capacity *
734 10000 / battery->design_voltage;
735 battery->full_charge_capacity =
736 battery->full_charge_capacity *
737 10000 / battery->design_voltage;
738 battery->design_capacity_warning =
739 battery->design_capacity_warning *
740 10000 / battery->design_voltage;
741 battery->capacity_now = battery->capacity_now *
742 10000 / battery->design_voltage;
748 static int acpi_battery_update(struct acpi_battery *battery, bool resume)
750 int result, old_present = acpi_battery_present(battery);
751 result = acpi_battery_get_status(battery);
754 if (!acpi_battery_present(battery)) {
755 sysfs_remove_battery(battery);
756 battery->update_time = 0;
763 if (!battery->update_time ||
764 old_present != acpi_battery_present(battery)) {
765 result = acpi_battery_get_info(battery);
768 acpi_battery_init_alarm(battery);
771 result = acpi_battery_get_state(battery);
774 acpi_battery_quirks(battery);
777 result = sysfs_add_battery(battery);
783 * Wakeup the system if battery is critical low
784 * or lower than the alarm level
786 if ((battery->state & ACPI_BATTERY_STATE_CRITICAL) ||
787 (test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags) &&
788 (battery->capacity_now <= battery->alarm)))
789 acpi_pm_wakeup_event(&battery->device->dev);
794 static void acpi_battery_refresh(struct acpi_battery *battery)
801 power_unit = battery->power_unit;
803 acpi_battery_get_info(battery);
805 if (power_unit == battery->power_unit)
808 /* The battery has changed its reporting units. */
809 sysfs_remove_battery(battery);
810 sysfs_add_battery(battery);
813 /* --------------------------------------------------------------------------
815 -------------------------------------------------------------------------- */
817 #ifdef CONFIG_ACPI_PROCFS_POWER
818 static struct proc_dir_entry *acpi_battery_dir;
820 static const char *acpi_battery_units(const struct acpi_battery *battery)
822 return (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) ?
826 static int acpi_battery_print_info(struct seq_file *seq, int result)
828 struct acpi_battery *battery = seq->private;
833 seq_printf(seq, "present: %s\n",
834 acpi_battery_present(battery) ? "yes" : "no");
835 if (!acpi_battery_present(battery))
837 if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
838 seq_printf(seq, "design capacity: unknown\n");
840 seq_printf(seq, "design capacity: %d %sh\n",
841 battery->design_capacity,
842 acpi_battery_units(battery));
844 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
845 seq_printf(seq, "last full capacity: unknown\n");
847 seq_printf(seq, "last full capacity: %d %sh\n",
848 battery->full_charge_capacity,
849 acpi_battery_units(battery));
851 seq_printf(seq, "battery technology: %srechargeable\n",
852 (!battery->technology)?"non-":"");
854 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
855 seq_printf(seq, "design voltage: unknown\n");
857 seq_printf(seq, "design voltage: %d mV\n",
858 battery->design_voltage);
859 seq_printf(seq, "design capacity warning: %d %sh\n",
860 battery->design_capacity_warning,
861 acpi_battery_units(battery));
862 seq_printf(seq, "design capacity low: %d %sh\n",
863 battery->design_capacity_low,
864 acpi_battery_units(battery));
865 seq_printf(seq, "cycle count: %i\n", battery->cycle_count);
866 seq_printf(seq, "capacity granularity 1: %d %sh\n",
867 battery->capacity_granularity_1,
868 acpi_battery_units(battery));
869 seq_printf(seq, "capacity granularity 2: %d %sh\n",
870 battery->capacity_granularity_2,
871 acpi_battery_units(battery));
872 seq_printf(seq, "model number: %s\n", battery->model_number);
873 seq_printf(seq, "serial number: %s\n", battery->serial_number);
874 seq_printf(seq, "battery type: %s\n", battery->type);
875 seq_printf(seq, "OEM info: %s\n", battery->oem_info);
878 seq_printf(seq, "ERROR: Unable to read battery info\n");
882 static int acpi_battery_print_state(struct seq_file *seq, int result)
884 struct acpi_battery *battery = seq->private;
889 seq_printf(seq, "present: %s\n",
890 acpi_battery_present(battery) ? "yes" : "no");
891 if (!acpi_battery_present(battery))
894 seq_printf(seq, "capacity state: %s\n",
895 (battery->state & 0x04) ? "critical" : "ok");
896 if ((battery->state & 0x01) && (battery->state & 0x02))
898 "charging state: charging/discharging\n");
899 else if (battery->state & 0x01)
900 seq_printf(seq, "charging state: discharging\n");
901 else if (battery->state & 0x02)
902 seq_printf(seq, "charging state: charging\n");
904 seq_printf(seq, "charging state: charged\n");
906 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
907 seq_printf(seq, "present rate: unknown\n");
909 seq_printf(seq, "present rate: %d %s\n",
910 battery->rate_now, acpi_battery_units(battery));
912 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
913 seq_printf(seq, "remaining capacity: unknown\n");
915 seq_printf(seq, "remaining capacity: %d %sh\n",
916 battery->capacity_now, acpi_battery_units(battery));
917 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
918 seq_printf(seq, "present voltage: unknown\n");
920 seq_printf(seq, "present voltage: %d mV\n",
921 battery->voltage_now);
924 seq_printf(seq, "ERROR: Unable to read battery state\n");
929 static int acpi_battery_print_alarm(struct seq_file *seq, int result)
931 struct acpi_battery *battery = seq->private;
936 if (!acpi_battery_present(battery)) {
937 seq_printf(seq, "present: no\n");
940 seq_printf(seq, "alarm: ");
942 seq_printf(seq, "unsupported\n");
944 seq_printf(seq, "%u %sh\n", battery->alarm,
945 acpi_battery_units(battery));
948 seq_printf(seq, "ERROR: Unable to read battery alarm\n");
952 static ssize_t acpi_battery_write_alarm(struct file *file,
953 const char __user * buffer,
954 size_t count, loff_t * ppos)
957 char alarm_string[12] = { '\0' };
958 struct seq_file *m = file->private_data;
959 struct acpi_battery *battery = m->private;
961 if (!battery || (count > sizeof(alarm_string) - 1))
963 if (!acpi_battery_present(battery)) {
967 if (copy_from_user(alarm_string, buffer, count)) {
971 alarm_string[count] = '\0';
972 if (kstrtoint(alarm_string, 0, &battery->alarm)) {
976 result = acpi_battery_set_alarm(battery);
983 typedef int(*print_func)(struct seq_file *seq, int result);
985 static print_func acpi_print_funcs[ACPI_BATTERY_NUMFILES] = {
986 acpi_battery_print_info,
987 acpi_battery_print_state,
988 acpi_battery_print_alarm,
991 static int acpi_battery_read(int fid, struct seq_file *seq)
993 struct acpi_battery *battery = seq->private;
994 int result = acpi_battery_update(battery, false);
995 return acpi_print_funcs[fid](seq, result);
998 #define DECLARE_FILE_FUNCTIONS(_name) \
999 static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \
1001 return acpi_battery_read(_name##_tag, seq); \
1003 static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \
1005 return single_open(file, acpi_battery_read_##_name, PDE_DATA(inode)); \
1008 DECLARE_FILE_FUNCTIONS(info);
1009 DECLARE_FILE_FUNCTIONS(state);
1010 DECLARE_FILE_FUNCTIONS(alarm);
1012 #undef DECLARE_FILE_FUNCTIONS
1014 #define FILE_DESCRIPTION_RO(_name) \
1016 .name = __stringify(_name), \
1019 .open = acpi_battery_##_name##_open_fs, \
1021 .llseek = seq_lseek, \
1022 .release = single_release, \
1023 .owner = THIS_MODULE, \
1027 #define FILE_DESCRIPTION_RW(_name) \
1029 .name = __stringify(_name), \
1030 .mode = S_IFREG | S_IRUGO | S_IWUSR, \
1032 .open = acpi_battery_##_name##_open_fs, \
1034 .llseek = seq_lseek, \
1035 .write = acpi_battery_write_##_name, \
1036 .release = single_release, \
1037 .owner = THIS_MODULE, \
1041 static const struct battery_file {
1042 struct file_operations ops;
1045 } acpi_battery_file[] = {
1046 FILE_DESCRIPTION_RO(info),
1047 FILE_DESCRIPTION_RO(state),
1048 FILE_DESCRIPTION_RW(alarm),
1051 #undef FILE_DESCRIPTION_RO
1052 #undef FILE_DESCRIPTION_RW
1054 static int acpi_battery_add_fs(struct acpi_device *device)
1056 struct proc_dir_entry *entry = NULL;
1059 printk(KERN_WARNING PREFIX "Deprecated procfs I/F for battery is loaded,"
1060 " please retry with CONFIG_ACPI_PROCFS_POWER cleared\n");
1061 if (!acpi_device_dir(device)) {
1062 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
1064 if (!acpi_device_dir(device))
1068 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
1069 entry = proc_create_data(acpi_battery_file[i].name,
1070 acpi_battery_file[i].mode,
1071 acpi_device_dir(device),
1072 &acpi_battery_file[i].ops,
1073 acpi_driver_data(device));
1080 static void acpi_battery_remove_fs(struct acpi_device *device)
1083 if (!acpi_device_dir(device))
1085 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i)
1086 remove_proc_entry(acpi_battery_file[i].name,
1087 acpi_device_dir(device));
1089 remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
1090 acpi_device_dir(device) = NULL;
1095 /* --------------------------------------------------------------------------
1097 -------------------------------------------------------------------------- */
1099 static void acpi_battery_notify(struct acpi_device *device, u32 event)
1101 struct acpi_battery *battery = acpi_driver_data(device);
1102 struct power_supply *old;
1108 * On Acer Aspire V5-573G notifications are sometimes triggered too
1109 * early. For example, when AC is unplugged and notification is
1110 * triggered, battery state is still reported as "Full", and changes to
1111 * "Discharging" only after short delay, without any notification.
1113 if (battery_notification_delay_ms > 0)
1114 msleep(battery_notification_delay_ms);
1115 if (event == ACPI_BATTERY_NOTIFY_INFO)
1116 acpi_battery_refresh(battery);
1117 acpi_battery_update(battery, false);
1118 acpi_bus_generate_netlink_event(device->pnp.device_class,
1119 dev_name(&device->dev), event,
1120 acpi_battery_present(battery));
1121 acpi_notifier_call_chain(device, event, acpi_battery_present(battery));
1122 /* acpi_battery_update could remove power_supply object */
1123 if (old && battery->bat)
1124 power_supply_changed(battery->bat);
1127 static int battery_notify(struct notifier_block *nb,
1128 unsigned long mode, void *_unused)
1130 struct acpi_battery *battery = container_of(nb, struct acpi_battery,
1135 case PM_POST_HIBERNATION:
1136 case PM_POST_SUSPEND:
1137 if (!acpi_battery_present(battery))
1140 if (!battery->bat) {
1141 result = acpi_battery_get_info(battery);
1145 result = sysfs_add_battery(battery);
1149 acpi_battery_refresh(battery);
1151 acpi_battery_init_alarm(battery);
1152 acpi_battery_get_state(battery);
1160 battery_bix_broken_package_quirk(const struct dmi_system_id *d)
1162 battery_bix_broken_package = 1;
1167 battery_notification_delay_quirk(const struct dmi_system_id *d)
1169 battery_notification_delay_ms = 1000;
1173 static const struct dmi_system_id bat_dmi_table[] __initconst = {
1175 .callback = battery_bix_broken_package_quirk,
1176 .ident = "NEC LZ750/LS",
1178 DMI_MATCH(DMI_SYS_VENDOR, "NEC"),
1179 DMI_MATCH(DMI_PRODUCT_NAME, "PC-LZ750LS"),
1183 .callback = battery_notification_delay_quirk,
1184 .ident = "Acer Aspire V5-573G",
1186 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
1187 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire V5-573G"),
1191 /* Microsoft Surface Go 3 */
1192 .callback = battery_notification_delay_quirk,
1194 DMI_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
1195 DMI_MATCH(DMI_PRODUCT_NAME, "Surface Go 3"),
1202 * Some machines'(E,G Lenovo Z480) ECs are not stable
1203 * during boot up and this causes battery driver fails to be
1204 * probed due to failure of getting battery information
1205 * from EC sometimes. After several retries, the operation
1206 * may work. So add retry code here and 20ms sleep between
1209 static int acpi_battery_update_retry(struct acpi_battery *battery)
1213 for (retry = 5; retry; retry--) {
1214 ret = acpi_battery_update(battery, false);
1223 static int acpi_battery_add(struct acpi_device *device)
1226 struct acpi_battery *battery = NULL;
1231 if (device->dep_unmet)
1232 return -EPROBE_DEFER;
1234 battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
1237 battery->device = device;
1238 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
1239 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
1240 device->driver_data = battery;
1241 mutex_init(&battery->lock);
1242 mutex_init(&battery->sysfs_lock);
1243 if (acpi_has_method(battery->device->handle, "_BIX"))
1244 set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
1246 result = acpi_battery_update_retry(battery);
1250 #ifdef CONFIG_ACPI_PROCFS_POWER
1251 result = acpi_battery_add_fs(device);
1254 #ifdef CONFIG_ACPI_PROCFS_POWER
1255 acpi_battery_remove_fs(device);
1260 printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
1261 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
1262 device->status.battery_present ? "present" : "absent");
1264 battery->pm_nb.notifier_call = battery_notify;
1265 register_pm_notifier(&battery->pm_nb);
1267 device_init_wakeup(&device->dev, 1);
1272 sysfs_remove_battery(battery);
1273 mutex_destroy(&battery->lock);
1274 mutex_destroy(&battery->sysfs_lock);
1279 static int acpi_battery_remove(struct acpi_device *device)
1281 struct acpi_battery *battery = NULL;
1283 if (!device || !acpi_driver_data(device))
1285 device_init_wakeup(&device->dev, 0);
1286 battery = acpi_driver_data(device);
1287 unregister_pm_notifier(&battery->pm_nb);
1288 #ifdef CONFIG_ACPI_PROCFS_POWER
1289 acpi_battery_remove_fs(device);
1291 sysfs_remove_battery(battery);
1292 mutex_destroy(&battery->lock);
1293 mutex_destroy(&battery->sysfs_lock);
1298 #ifdef CONFIG_PM_SLEEP
1299 /* this is needed to learn about changes made in suspended state */
1300 static int acpi_battery_resume(struct device *dev)
1302 struct acpi_battery *battery;
1307 battery = acpi_driver_data(to_acpi_device(dev));
1311 battery->update_time = 0;
1312 acpi_battery_update(battery, true);
1316 #define acpi_battery_resume NULL
1319 static SIMPLE_DEV_PM_OPS(acpi_battery_pm, NULL, acpi_battery_resume);
1321 static struct acpi_driver acpi_battery_driver = {
1323 .class = ACPI_BATTERY_CLASS,
1324 .ids = battery_device_ids,
1325 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1327 .add = acpi_battery_add,
1328 .remove = acpi_battery_remove,
1329 .notify = acpi_battery_notify,
1331 .drv.pm = &acpi_battery_pm,
1334 static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
1339 for (i = 0; i < ARRAY_SIZE(acpi_battery_blacklist); i++)
1340 if (acpi_dev_present(acpi_battery_blacklist[i], "1", -1)) {
1341 pr_info(PREFIX ACPI_BATTERY_DEVICE_NAME
1342 ": found native %s PMIC, not loading\n",
1343 acpi_battery_blacklist[i]);
1347 dmi_check_system(bat_dmi_table);
1349 #ifdef CONFIG_ACPI_PROCFS_POWER
1350 acpi_battery_dir = acpi_lock_battery_dir();
1351 if (!acpi_battery_dir)
1354 result = acpi_bus_register_driver(&acpi_battery_driver);
1355 #ifdef CONFIG_ACPI_PROCFS_POWER
1357 acpi_unlock_battery_dir(acpi_battery_dir);
1359 battery_driver_registered = (result == 0);
1362 static int __init acpi_battery_init(void)
1367 async_cookie = async_schedule(acpi_battery_init_async, NULL);
1371 static void __exit acpi_battery_exit(void)
1373 async_synchronize_cookie(async_cookie + 1);
1374 if (battery_driver_registered)
1375 acpi_bus_unregister_driver(&acpi_battery_driver);
1376 #ifdef CONFIG_ACPI_PROCFS_POWER
1377 if (acpi_battery_dir)
1378 acpi_unlock_battery_dir(acpi_battery_dir);
1382 module_init(acpi_battery_init);
1383 module_exit(acpi_battery_exit);