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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 #include <linux/async.h>
27 #include <linux/delay.h>
28 #include <linux/dmi.h>
29 #include <linux/jiffies.h>
30 #include <linux/kernel.h>
31 #include <linux/list.h>
32 #include <linux/module.h>
33 #include <linux/mutex.h>
34 #include <linux/slab.h>
35 #include <linux/suspend.h>
36 #include <linux/types.h>
38 #include <asm/unaligned.h>
40 #ifdef CONFIG_ACPI_PROCFS_POWER
41 #include <linux/proc_fs.h>
42 #include <linux/seq_file.h>
43 #include <linux/uaccess.h>
46 #include <linux/acpi.h>
47 #include <linux/power_supply.h>
49 #include <acpi/battery.h>
51 #define PREFIX "ACPI: "
53 #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
54 #define ACPI_BATTERY_CAPACITY_VALID(capacity) \
55 ((capacity) != 0 && (capacity) != ACPI_BATTERY_VALUE_UNKNOWN)
57 #define ACPI_BATTERY_DEVICE_NAME "Battery"
59 /* Battery power unit: 0 means mW, 1 means mA */
60 #define ACPI_BATTERY_POWER_UNIT_MA 1
62 #define ACPI_BATTERY_STATE_DISCHARGING 0x1
63 #define ACPI_BATTERY_STATE_CHARGING 0x2
64 #define ACPI_BATTERY_STATE_CRITICAL 0x4
66 #define _COMPONENT ACPI_BATTERY_COMPONENT
68 ACPI_MODULE_NAME("battery");
70 MODULE_AUTHOR("Paul Diefenbaugh");
71 MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
72 MODULE_DESCRIPTION("ACPI Battery Driver");
73 MODULE_LICENSE("GPL");
75 static async_cookie_t async_cookie;
76 static bool battery_driver_registered;
77 static int battery_bix_broken_package;
78 static int battery_notification_delay_ms;
79 static int battery_ac_is_broken;
80 static int battery_check_pmic = 1;
81 static int battery_quirk_notcharging;
82 static unsigned int cache_time = 1000;
83 module_param(cache_time, uint, 0644);
84 MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
86 #ifdef CONFIG_ACPI_PROCFS_POWER
87 extern struct proc_dir_entry *acpi_lock_battery_dir(void);
88 extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
91 static const struct acpi_device_id battery_device_ids[] = {
94 /* Microsoft Surface Go 3 */
100 MODULE_DEVICE_TABLE(acpi, battery_device_ids);
102 /* Lists of PMIC ACPI HIDs with an (often better) native battery driver */
103 static const char * const acpi_battery_blacklist[] = {
104 "INT33F4", /* X-Powers AXP288 PMIC */
108 ACPI_BATTERY_ALARM_PRESENT,
109 ACPI_BATTERY_XINFO_PRESENT,
110 ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY,
111 /* On Lenovo Thinkpad models from 2010 and 2011, the power unit
112 switches between mWh and mAh depending on whether the system
113 is running on battery or not. When mAh is the unit, most
114 reported values are incorrect and need to be adjusted by
115 10000/design_voltage. Verified on x201, t410, t410s, and x220.
116 Pre-2010 and 2012 models appear to always report in mWh and
117 are thus unaffected (tested with t42, t61, t500, x200, x300,
118 and x230). Also, in mid-2012 Lenovo issued a BIOS update for
119 the 2011 models that fixes the issue (tested on x220 with a
120 post-1.29 BIOS), but as of Nov. 2012, no such update is
121 available for the 2010 models. */
122 ACPI_BATTERY_QUIRK_THINKPAD_MAH,
123 /* for batteries reporting current capacity with design capacity
124 * on a full charge, but showing degradation in full charge cap.
126 ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE,
129 struct acpi_battery {
131 struct mutex sysfs_lock;
132 struct power_supply *bat;
133 struct power_supply_desc bat_desc;
134 struct acpi_device *device;
135 struct notifier_block pm_nb;
136 struct list_head list;
137 unsigned long update_time;
143 int full_charge_capacity;
146 int design_capacity_warning;
147 int design_capacity_low;
149 int measurement_accuracy;
150 int max_sampling_time;
151 int min_sampling_time;
152 int max_averaging_interval;
153 int min_averaging_interval;
154 int capacity_granularity_1;
155 int capacity_granularity_2;
157 char model_number[32];
158 char serial_number[32];
166 #define to_acpi_battery(x) power_supply_get_drvdata(x)
168 static inline int acpi_battery_present(struct acpi_battery *battery)
170 return battery->device->status.battery_present;
173 static int acpi_battery_technology(struct acpi_battery *battery)
175 if (!strcasecmp("NiCd", battery->type))
176 return POWER_SUPPLY_TECHNOLOGY_NiCd;
177 if (!strcasecmp("NiMH", battery->type))
178 return POWER_SUPPLY_TECHNOLOGY_NiMH;
179 if (!strcasecmp("LION", battery->type))
180 return POWER_SUPPLY_TECHNOLOGY_LION;
181 if (!strncasecmp("LI-ION", battery->type, 6))
182 return POWER_SUPPLY_TECHNOLOGY_LION;
183 if (!strcasecmp("LiP", battery->type))
184 return POWER_SUPPLY_TECHNOLOGY_LIPO;
185 return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
188 static int acpi_battery_get_state(struct acpi_battery *battery);
190 static int acpi_battery_is_charged(struct acpi_battery *battery)
192 /* charging, discharging or critical low */
193 if (battery->state != 0)
196 /* battery not reporting charge */
197 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
198 battery->capacity_now == 0)
201 /* good batteries update full_charge as the batteries degrade */
202 if (battery->full_charge_capacity == battery->capacity_now)
205 /* fallback to using design values for broken batteries */
206 if (battery->design_capacity <= battery->capacity_now)
209 /* we don't do any sort of metric based on percentages */
213 static bool acpi_battery_is_degraded(struct acpi_battery *battery)
215 return ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity) &&
216 ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity) &&
217 battery->full_charge_capacity < battery->design_capacity;
220 static int acpi_battery_handle_discharging(struct acpi_battery *battery)
223 * Some devices wrongly report discharging if the battery's charge level
224 * was above the device's start charging threshold atm the AC adapter
225 * was plugged in and the device thus did not start a new charge cycle.
227 if ((battery_ac_is_broken || power_supply_is_system_supplied()) &&
228 battery->rate_now == 0)
229 return POWER_SUPPLY_STATUS_NOT_CHARGING;
231 return POWER_SUPPLY_STATUS_DISCHARGING;
234 static int acpi_battery_get_property(struct power_supply *psy,
235 enum power_supply_property psp,
236 union power_supply_propval *val)
238 int full_capacity = ACPI_BATTERY_VALUE_UNKNOWN, ret = 0;
239 struct acpi_battery *battery = to_acpi_battery(psy);
241 if (acpi_battery_present(battery)) {
242 /* run battery update only if it is present */
243 acpi_battery_get_state(battery);
244 } else if (psp != POWER_SUPPLY_PROP_PRESENT)
247 case POWER_SUPPLY_PROP_STATUS:
248 if (battery->state & ACPI_BATTERY_STATE_DISCHARGING)
249 val->intval = acpi_battery_handle_discharging(battery);
250 else if (battery->state & ACPI_BATTERY_STATE_CHARGING)
251 val->intval = POWER_SUPPLY_STATUS_CHARGING;
252 else if (acpi_battery_is_charged(battery))
253 val->intval = POWER_SUPPLY_STATUS_FULL;
254 else if (battery_quirk_notcharging)
255 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
257 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
259 case POWER_SUPPLY_PROP_PRESENT:
260 val->intval = acpi_battery_present(battery);
262 case POWER_SUPPLY_PROP_TECHNOLOGY:
263 val->intval = acpi_battery_technology(battery);
265 case POWER_SUPPLY_PROP_CYCLE_COUNT:
266 val->intval = battery->cycle_count;
268 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
269 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
272 val->intval = battery->design_voltage * 1000;
274 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
275 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
278 val->intval = battery->voltage_now * 1000;
280 case POWER_SUPPLY_PROP_CURRENT_NOW:
281 case POWER_SUPPLY_PROP_POWER_NOW:
282 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
285 val->intval = battery->rate_now * 1000;
287 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
288 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
289 if (!ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity))
292 val->intval = battery->design_capacity * 1000;
294 case POWER_SUPPLY_PROP_CHARGE_FULL:
295 case POWER_SUPPLY_PROP_ENERGY_FULL:
296 if (!ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity))
299 val->intval = battery->full_charge_capacity * 1000;
301 case POWER_SUPPLY_PROP_CHARGE_NOW:
302 case POWER_SUPPLY_PROP_ENERGY_NOW:
303 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
306 val->intval = battery->capacity_now * 1000;
308 case POWER_SUPPLY_PROP_CAPACITY:
309 if (ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity))
310 full_capacity = battery->full_charge_capacity;
311 else if (ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity))
312 full_capacity = battery->design_capacity;
314 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
315 full_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
318 val->intval = battery->capacity_now * 100/
321 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
322 if (battery->state & ACPI_BATTERY_STATE_CRITICAL)
323 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
324 else if (test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags) &&
325 (battery->capacity_now <= battery->alarm))
326 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
327 else if (acpi_battery_is_charged(battery))
328 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
330 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
332 case POWER_SUPPLY_PROP_MODEL_NAME:
333 val->strval = battery->model_number;
335 case POWER_SUPPLY_PROP_MANUFACTURER:
336 val->strval = battery->oem_info;
338 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
339 val->strval = battery->serial_number;
347 static enum power_supply_property charge_battery_props[] = {
348 POWER_SUPPLY_PROP_STATUS,
349 POWER_SUPPLY_PROP_PRESENT,
350 POWER_SUPPLY_PROP_TECHNOLOGY,
351 POWER_SUPPLY_PROP_CYCLE_COUNT,
352 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
353 POWER_SUPPLY_PROP_VOLTAGE_NOW,
354 POWER_SUPPLY_PROP_CURRENT_NOW,
355 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
356 POWER_SUPPLY_PROP_CHARGE_FULL,
357 POWER_SUPPLY_PROP_CHARGE_NOW,
358 POWER_SUPPLY_PROP_CAPACITY,
359 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
360 POWER_SUPPLY_PROP_MODEL_NAME,
361 POWER_SUPPLY_PROP_MANUFACTURER,
362 POWER_SUPPLY_PROP_SERIAL_NUMBER,
365 static enum power_supply_property charge_battery_full_cap_broken_props[] = {
366 POWER_SUPPLY_PROP_STATUS,
367 POWER_SUPPLY_PROP_PRESENT,
368 POWER_SUPPLY_PROP_TECHNOLOGY,
369 POWER_SUPPLY_PROP_CYCLE_COUNT,
370 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
371 POWER_SUPPLY_PROP_VOLTAGE_NOW,
372 POWER_SUPPLY_PROP_CURRENT_NOW,
373 POWER_SUPPLY_PROP_CHARGE_NOW,
374 POWER_SUPPLY_PROP_MODEL_NAME,
375 POWER_SUPPLY_PROP_MANUFACTURER,
376 POWER_SUPPLY_PROP_SERIAL_NUMBER,
379 static enum power_supply_property energy_battery_props[] = {
380 POWER_SUPPLY_PROP_STATUS,
381 POWER_SUPPLY_PROP_PRESENT,
382 POWER_SUPPLY_PROP_TECHNOLOGY,
383 POWER_SUPPLY_PROP_CYCLE_COUNT,
384 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
385 POWER_SUPPLY_PROP_VOLTAGE_NOW,
386 POWER_SUPPLY_PROP_POWER_NOW,
387 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
388 POWER_SUPPLY_PROP_ENERGY_FULL,
389 POWER_SUPPLY_PROP_ENERGY_NOW,
390 POWER_SUPPLY_PROP_CAPACITY,
391 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
392 POWER_SUPPLY_PROP_MODEL_NAME,
393 POWER_SUPPLY_PROP_MANUFACTURER,
394 POWER_SUPPLY_PROP_SERIAL_NUMBER,
397 static enum power_supply_property energy_battery_full_cap_broken_props[] = {
398 POWER_SUPPLY_PROP_STATUS,
399 POWER_SUPPLY_PROP_PRESENT,
400 POWER_SUPPLY_PROP_TECHNOLOGY,
401 POWER_SUPPLY_PROP_CYCLE_COUNT,
402 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
403 POWER_SUPPLY_PROP_VOLTAGE_NOW,
404 POWER_SUPPLY_PROP_POWER_NOW,
405 POWER_SUPPLY_PROP_ENERGY_NOW,
406 POWER_SUPPLY_PROP_MODEL_NAME,
407 POWER_SUPPLY_PROP_MANUFACTURER,
408 POWER_SUPPLY_PROP_SERIAL_NUMBER,
411 /* --------------------------------------------------------------------------
413 -------------------------------------------------------------------------- */
414 struct acpi_offsets {
415 size_t offset; /* offset inside struct acpi_sbs_battery */
416 u8 mode; /* int or string? */
419 static const struct acpi_offsets state_offsets[] = {
420 {offsetof(struct acpi_battery, state), 0},
421 {offsetof(struct acpi_battery, rate_now), 0},
422 {offsetof(struct acpi_battery, capacity_now), 0},
423 {offsetof(struct acpi_battery, voltage_now), 0},
426 static const struct acpi_offsets info_offsets[] = {
427 {offsetof(struct acpi_battery, power_unit), 0},
428 {offsetof(struct acpi_battery, design_capacity), 0},
429 {offsetof(struct acpi_battery, full_charge_capacity), 0},
430 {offsetof(struct acpi_battery, technology), 0},
431 {offsetof(struct acpi_battery, design_voltage), 0},
432 {offsetof(struct acpi_battery, design_capacity_warning), 0},
433 {offsetof(struct acpi_battery, design_capacity_low), 0},
434 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
435 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
436 {offsetof(struct acpi_battery, model_number), 1},
437 {offsetof(struct acpi_battery, serial_number), 1},
438 {offsetof(struct acpi_battery, type), 1},
439 {offsetof(struct acpi_battery, oem_info), 1},
442 static const struct acpi_offsets extended_info_offsets[] = {
443 {offsetof(struct acpi_battery, revision), 0},
444 {offsetof(struct acpi_battery, power_unit), 0},
445 {offsetof(struct acpi_battery, design_capacity), 0},
446 {offsetof(struct acpi_battery, full_charge_capacity), 0},
447 {offsetof(struct acpi_battery, technology), 0},
448 {offsetof(struct acpi_battery, design_voltage), 0},
449 {offsetof(struct acpi_battery, design_capacity_warning), 0},
450 {offsetof(struct acpi_battery, design_capacity_low), 0},
451 {offsetof(struct acpi_battery, cycle_count), 0},
452 {offsetof(struct acpi_battery, measurement_accuracy), 0},
453 {offsetof(struct acpi_battery, max_sampling_time), 0},
454 {offsetof(struct acpi_battery, min_sampling_time), 0},
455 {offsetof(struct acpi_battery, max_averaging_interval), 0},
456 {offsetof(struct acpi_battery, min_averaging_interval), 0},
457 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
458 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
459 {offsetof(struct acpi_battery, model_number), 1},
460 {offsetof(struct acpi_battery, serial_number), 1},
461 {offsetof(struct acpi_battery, type), 1},
462 {offsetof(struct acpi_battery, oem_info), 1},
465 static int extract_package(struct acpi_battery *battery,
466 union acpi_object *package,
467 const struct acpi_offsets *offsets, int num)
470 union acpi_object *element;
471 if (package->type != ACPI_TYPE_PACKAGE)
473 for (i = 0; i < num; ++i) {
474 if (package->package.count <= i)
476 element = &package->package.elements[i];
477 if (offsets[i].mode) {
478 u8 *ptr = (u8 *)battery + offsets[i].offset;
479 if (element->type == ACPI_TYPE_STRING ||
480 element->type == ACPI_TYPE_BUFFER)
481 strscpy(ptr, element->string.pointer, 32);
482 else if (element->type == ACPI_TYPE_INTEGER) {
483 strncpy(ptr, (u8 *)&element->integer.value,
485 ptr[sizeof(u64)] = 0;
487 *ptr = 0; /* don't have value */
489 int *x = (int *)((u8 *)battery + offsets[i].offset);
490 *x = (element->type == ACPI_TYPE_INTEGER) ?
491 element->integer.value : -1;
497 static int acpi_battery_get_status(struct acpi_battery *battery)
499 if (acpi_bus_get_status(battery->device)) {
500 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
507 static int extract_battery_info(const int use_bix,
508 struct acpi_battery *battery,
509 const struct acpi_buffer *buffer)
511 int result = -EFAULT;
513 if (use_bix && battery_bix_broken_package)
514 result = extract_package(battery, buffer->pointer,
515 extended_info_offsets + 1,
516 ARRAY_SIZE(extended_info_offsets) - 1);
518 result = extract_package(battery, buffer->pointer,
519 extended_info_offsets,
520 ARRAY_SIZE(extended_info_offsets));
522 result = extract_package(battery, buffer->pointer,
523 info_offsets, ARRAY_SIZE(info_offsets));
524 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
525 battery->full_charge_capacity = battery->design_capacity;
526 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) &&
527 battery->power_unit && battery->design_voltage) {
528 battery->design_capacity = battery->design_capacity *
529 10000 / battery->design_voltage;
530 battery->full_charge_capacity = battery->full_charge_capacity *
531 10000 / battery->design_voltage;
532 battery->design_capacity_warning =
533 battery->design_capacity_warning *
534 10000 / battery->design_voltage;
535 /* Curiously, design_capacity_low, unlike the rest of them,
537 /* capacity_granularity_* equal 1 on the systems tested, so
538 it's impossible to tell if they would need an adjustment
539 or not if their values were higher. */
541 if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags) &&
542 battery->capacity_now > battery->full_charge_capacity)
543 battery->capacity_now = battery->full_charge_capacity;
548 static int acpi_battery_get_info(struct acpi_battery *battery)
550 const int xinfo = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
552 int result = -ENODEV;
554 if (!acpi_battery_present(battery))
558 for (use_bix = xinfo ? 1 : 0; use_bix >= 0; use_bix--) {
559 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
560 acpi_status status = AE_ERROR;
562 mutex_lock(&battery->lock);
563 status = acpi_evaluate_object(battery->device->handle,
564 use_bix ? "_BIX":"_BIF",
566 mutex_unlock(&battery->lock);
568 if (ACPI_FAILURE(status)) {
569 ACPI_EXCEPTION((AE_INFO, status, "Evaluating %s",
570 use_bix ? "_BIX":"_BIF"));
572 result = extract_battery_info(use_bix,
576 kfree(buffer.pointer);
581 if (!result && !use_bix && xinfo)
582 pr_warn(FW_BUG "The _BIX method is broken, using _BIF.\n");
587 static int acpi_battery_get_state(struct acpi_battery *battery)
590 acpi_status status = 0;
591 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
593 if (!acpi_battery_present(battery))
596 if (battery->update_time &&
597 time_before(jiffies, battery->update_time +
598 msecs_to_jiffies(cache_time)))
601 mutex_lock(&battery->lock);
602 status = acpi_evaluate_object(battery->device->handle, "_BST",
604 mutex_unlock(&battery->lock);
606 if (ACPI_FAILURE(status)) {
607 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
611 result = extract_package(battery, buffer.pointer,
612 state_offsets, ARRAY_SIZE(state_offsets));
613 battery->update_time = jiffies;
614 kfree(buffer.pointer);
616 /* For buggy DSDTs that report negative 16-bit values for either
617 * charging or discharging current and/or report 0 as 65536
620 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA &&
621 battery->rate_now != ACPI_BATTERY_VALUE_UNKNOWN &&
622 (s16)(battery->rate_now) < 0) {
623 battery->rate_now = abs((s16)battery->rate_now);
624 pr_warn_once(FW_BUG "battery: (dis)charge rate invalid.\n");
627 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)
628 && battery->capacity_now >= 0 && battery->capacity_now <= 100)
629 battery->capacity_now = (battery->capacity_now *
630 battery->full_charge_capacity) / 100;
631 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) &&
632 battery->power_unit && battery->design_voltage) {
633 battery->capacity_now = battery->capacity_now *
634 10000 / battery->design_voltage;
636 if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags) &&
637 battery->capacity_now > battery->full_charge_capacity)
638 battery->capacity_now = battery->full_charge_capacity;
643 static int acpi_battery_set_alarm(struct acpi_battery *battery)
645 acpi_status status = 0;
647 if (!acpi_battery_present(battery) ||
648 !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags))
651 mutex_lock(&battery->lock);
652 status = acpi_execute_simple_method(battery->device->handle, "_BTP",
654 mutex_unlock(&battery->lock);
656 if (ACPI_FAILURE(status))
659 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm));
663 static int acpi_battery_init_alarm(struct acpi_battery *battery)
665 /* See if alarms are supported, and if so, set default */
666 if (!acpi_has_method(battery->device->handle, "_BTP")) {
667 clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
670 set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
672 battery->alarm = battery->design_capacity_warning;
673 return acpi_battery_set_alarm(battery);
676 static ssize_t acpi_battery_alarm_show(struct device *dev,
677 struct device_attribute *attr,
680 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
681 return sprintf(buf, "%d\n", battery->alarm * 1000);
684 static ssize_t acpi_battery_alarm_store(struct device *dev,
685 struct device_attribute *attr,
686 const char *buf, size_t count)
689 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
690 if (sscanf(buf, "%lu\n", &x) == 1)
691 battery->alarm = x/1000;
692 if (acpi_battery_present(battery))
693 acpi_battery_set_alarm(battery);
697 static const struct device_attribute alarm_attr = {
698 .attr = {.name = "alarm", .mode = 0644},
699 .show = acpi_battery_alarm_show,
700 .store = acpi_battery_alarm_store,
704 * The Battery Hooking API
706 * This API is used inside other drivers that need to expose
707 * platform-specific behaviour within the generic driver in a
712 static LIST_HEAD(acpi_battery_list);
713 static LIST_HEAD(battery_hook_list);
714 static DEFINE_MUTEX(hook_mutex);
716 static void __battery_hook_unregister(struct acpi_battery_hook *hook, int lock)
718 struct acpi_battery *battery;
720 * In order to remove a hook, we first need to
721 * de-register all the batteries that are registered.
724 mutex_lock(&hook_mutex);
725 list_for_each_entry(battery, &acpi_battery_list, list) {
726 hook->remove_battery(battery->bat);
728 list_del(&hook->list);
730 mutex_unlock(&hook_mutex);
731 pr_info("extension unregistered: %s\n", hook->name);
734 void battery_hook_unregister(struct acpi_battery_hook *hook)
736 __battery_hook_unregister(hook, 1);
738 EXPORT_SYMBOL_GPL(battery_hook_unregister);
740 void battery_hook_register(struct acpi_battery_hook *hook)
742 struct acpi_battery *battery;
744 mutex_lock(&hook_mutex);
745 INIT_LIST_HEAD(&hook->list);
746 list_add(&hook->list, &battery_hook_list);
748 * Now that the driver is registered, we need
749 * to notify the hook that a battery is available
750 * for each battery, so that the driver may add
753 list_for_each_entry(battery, &acpi_battery_list, list) {
754 if (hook->add_battery(battery->bat)) {
756 * If a add-battery returns non-zero,
757 * the registration of the extension has failed,
758 * and we will not add it to the list of loaded
761 pr_err("extension failed to load: %s", hook->name);
762 __battery_hook_unregister(hook, 0);
766 pr_info("new extension: %s\n", hook->name);
768 mutex_unlock(&hook_mutex);
770 EXPORT_SYMBOL_GPL(battery_hook_register);
773 * This function gets called right after the battery sysfs
774 * attributes have been added, so that the drivers that
775 * define custom sysfs attributes can add their own.
777 static void battery_hook_add_battery(struct acpi_battery *battery)
779 struct acpi_battery_hook *hook_node, *tmp;
781 mutex_lock(&hook_mutex);
782 INIT_LIST_HEAD(&battery->list);
783 list_add(&battery->list, &acpi_battery_list);
785 * Since we added a new battery to the list, we need to
786 * iterate over the hooks and call add_battery for each
787 * hook that was registered. This usually happens
788 * when a battery gets hotplugged or initialized
789 * during the battery module initialization.
791 list_for_each_entry_safe(hook_node, tmp, &battery_hook_list, list) {
792 if (hook_node->add_battery(battery->bat)) {
794 * The notification of the extensions has failed, to
795 * prevent further errors we will unload the extension.
797 pr_err("error in extension, unloading: %s",
799 __battery_hook_unregister(hook_node, 0);
802 mutex_unlock(&hook_mutex);
805 static void battery_hook_remove_battery(struct acpi_battery *battery)
807 struct acpi_battery_hook *hook;
809 mutex_lock(&hook_mutex);
811 * Before removing the hook, we need to remove all
812 * custom attributes from the battery.
814 list_for_each_entry(hook, &battery_hook_list, list) {
815 hook->remove_battery(battery->bat);
817 /* Then, just remove the battery from the list */
818 list_del(&battery->list);
819 mutex_unlock(&hook_mutex);
822 static void __exit battery_hook_exit(void)
824 struct acpi_battery_hook *hook;
825 struct acpi_battery_hook *ptr;
827 * At this point, the acpi_bus_unregister_driver()
828 * has called remove for all batteries. We just
829 * need to remove the hooks.
831 list_for_each_entry_safe(hook, ptr, &battery_hook_list, list) {
832 __battery_hook_unregister(hook, 1);
834 mutex_destroy(&hook_mutex);
837 static int sysfs_add_battery(struct acpi_battery *battery)
839 struct power_supply_config psy_cfg = { .drv_data = battery, };
840 bool full_cap_broken = false;
842 if (!ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity) &&
843 !ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity))
844 full_cap_broken = true;
846 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) {
847 if (full_cap_broken) {
848 battery->bat_desc.properties =
849 charge_battery_full_cap_broken_props;
850 battery->bat_desc.num_properties =
851 ARRAY_SIZE(charge_battery_full_cap_broken_props);
853 battery->bat_desc.properties = charge_battery_props;
854 battery->bat_desc.num_properties =
855 ARRAY_SIZE(charge_battery_props);
858 if (full_cap_broken) {
859 battery->bat_desc.properties =
860 energy_battery_full_cap_broken_props;
861 battery->bat_desc.num_properties =
862 ARRAY_SIZE(energy_battery_full_cap_broken_props);
864 battery->bat_desc.properties = energy_battery_props;
865 battery->bat_desc.num_properties =
866 ARRAY_SIZE(energy_battery_props);
870 battery->bat_desc.name = acpi_device_bid(battery->device);
871 battery->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY;
872 battery->bat_desc.get_property = acpi_battery_get_property;
874 battery->bat = power_supply_register_no_ws(&battery->device->dev,
875 &battery->bat_desc, &psy_cfg);
877 if (IS_ERR(battery->bat)) {
878 int result = PTR_ERR(battery->bat);
883 battery_hook_add_battery(battery);
884 return device_create_file(&battery->bat->dev, &alarm_attr);
887 static void sysfs_remove_battery(struct acpi_battery *battery)
889 mutex_lock(&battery->sysfs_lock);
891 mutex_unlock(&battery->sysfs_lock);
894 battery_hook_remove_battery(battery);
895 device_remove_file(&battery->bat->dev, &alarm_attr);
896 power_supply_unregister(battery->bat);
898 mutex_unlock(&battery->sysfs_lock);
901 static void find_battery(const struct dmi_header *dm, void *private)
903 struct acpi_battery *battery = (struct acpi_battery *)private;
904 /* Note: the hardcoded offsets below have been extracted from
905 the source code of dmidecode. */
906 if (dm->type == DMI_ENTRY_PORTABLE_BATTERY && dm->length >= 8) {
907 const u8 *dmi_data = (const u8 *)(dm + 1);
908 int dmi_capacity = get_unaligned((const u16 *)(dmi_data + 6));
909 if (dm->length >= 18)
910 dmi_capacity *= dmi_data[17];
911 if (battery->design_capacity * battery->design_voltage / 1000
913 battery->design_capacity * 10 == dmi_capacity)
914 set_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH,
920 * According to the ACPI spec, some kinds of primary batteries can
921 * report percentage battery remaining capacity directly to OS.
922 * In this case, it reports the Last Full Charged Capacity == 100
923 * and BatteryPresentRate == 0xFFFFFFFF.
925 * Now we found some battery reports percentage remaining capacity
926 * even if it's rechargeable.
927 * https://bugzilla.kernel.org/show_bug.cgi?id=15979
929 * Handle this correctly so that they won't break userspace.
931 static void acpi_battery_quirks(struct acpi_battery *battery)
933 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
936 if (battery->full_charge_capacity == 100 &&
937 battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN &&
938 battery->capacity_now >= 0 && battery->capacity_now <= 100) {
939 set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags);
940 battery->full_charge_capacity = battery->design_capacity;
941 battery->capacity_now = (battery->capacity_now *
942 battery->full_charge_capacity) / 100;
945 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags))
948 if (battery->power_unit && dmi_name_in_vendors("LENOVO")) {
950 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
951 if (s && !strncasecmp(s, "ThinkPad", 8)) {
952 dmi_walk(find_battery, battery);
953 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH,
955 battery->design_voltage) {
956 battery->design_capacity =
957 battery->design_capacity *
958 10000 / battery->design_voltage;
959 battery->full_charge_capacity =
960 battery->full_charge_capacity *
961 10000 / battery->design_voltage;
962 battery->design_capacity_warning =
963 battery->design_capacity_warning *
964 10000 / battery->design_voltage;
965 battery->capacity_now = battery->capacity_now *
966 10000 / battery->design_voltage;
971 if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags))
974 if (acpi_battery_is_degraded(battery) &&
975 battery->capacity_now > battery->full_charge_capacity) {
976 set_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags);
977 battery->capacity_now = battery->full_charge_capacity;
981 static int acpi_battery_update(struct acpi_battery *battery, bool resume)
983 int result = acpi_battery_get_status(battery);
988 if (!acpi_battery_present(battery)) {
989 sysfs_remove_battery(battery);
990 battery->update_time = 0;
997 if (!battery->update_time) {
998 result = acpi_battery_get_info(battery);
1001 acpi_battery_init_alarm(battery);
1004 result = acpi_battery_get_state(battery);
1007 acpi_battery_quirks(battery);
1009 if (!battery->bat) {
1010 result = sysfs_add_battery(battery);
1016 * Wakeup the system if battery is critical low
1017 * or lower than the alarm level
1019 if ((battery->state & ACPI_BATTERY_STATE_CRITICAL) ||
1020 (test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags) &&
1021 (battery->capacity_now <= battery->alarm)))
1022 acpi_pm_wakeup_event(&battery->device->dev);
1027 static void acpi_battery_refresh(struct acpi_battery *battery)
1034 power_unit = battery->power_unit;
1036 acpi_battery_get_info(battery);
1038 if (power_unit == battery->power_unit)
1041 /* The battery has changed its reporting units. */
1042 sysfs_remove_battery(battery);
1043 sysfs_add_battery(battery);
1046 /* --------------------------------------------------------------------------
1047 FS Interface (/proc)
1048 -------------------------------------------------------------------------- */
1050 #ifdef CONFIG_ACPI_PROCFS_POWER
1051 static struct proc_dir_entry *acpi_battery_dir;
1053 static const char *acpi_battery_units(const struct acpi_battery *battery)
1055 return (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) ?
1059 static int acpi_battery_info_proc_show(struct seq_file *seq, void *offset)
1061 struct acpi_battery *battery = seq->private;
1062 int result = acpi_battery_update(battery, false);
1067 seq_printf(seq, "present: %s\n",
1068 acpi_battery_present(battery) ? "yes" : "no");
1069 if (!acpi_battery_present(battery))
1071 if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
1072 seq_printf(seq, "design capacity: unknown\n");
1074 seq_printf(seq, "design capacity: %d %sh\n",
1075 battery->design_capacity,
1076 acpi_battery_units(battery));
1078 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
1079 seq_printf(seq, "last full capacity: unknown\n");
1081 seq_printf(seq, "last full capacity: %d %sh\n",
1082 battery->full_charge_capacity,
1083 acpi_battery_units(battery));
1085 seq_printf(seq, "battery technology: %srechargeable\n",
1086 battery->technology ? "" : "non-");
1088 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
1089 seq_printf(seq, "design voltage: unknown\n");
1091 seq_printf(seq, "design voltage: %d mV\n",
1092 battery->design_voltage);
1093 seq_printf(seq, "design capacity warning: %d %sh\n",
1094 battery->design_capacity_warning,
1095 acpi_battery_units(battery));
1096 seq_printf(seq, "design capacity low: %d %sh\n",
1097 battery->design_capacity_low,
1098 acpi_battery_units(battery));
1099 seq_printf(seq, "cycle count: %i\n", battery->cycle_count);
1100 seq_printf(seq, "capacity granularity 1: %d %sh\n",
1101 battery->capacity_granularity_1,
1102 acpi_battery_units(battery));
1103 seq_printf(seq, "capacity granularity 2: %d %sh\n",
1104 battery->capacity_granularity_2,
1105 acpi_battery_units(battery));
1106 seq_printf(seq, "model number: %s\n", battery->model_number);
1107 seq_printf(seq, "serial number: %s\n", battery->serial_number);
1108 seq_printf(seq, "battery type: %s\n", battery->type);
1109 seq_printf(seq, "OEM info: %s\n", battery->oem_info);
1112 seq_printf(seq, "ERROR: Unable to read battery info\n");
1116 static int acpi_battery_state_proc_show(struct seq_file *seq, void *offset)
1118 struct acpi_battery *battery = seq->private;
1119 int result = acpi_battery_update(battery, false);
1124 seq_printf(seq, "present: %s\n",
1125 acpi_battery_present(battery) ? "yes" : "no");
1126 if (!acpi_battery_present(battery))
1129 seq_printf(seq, "capacity state: %s\n",
1130 (battery->state & 0x04) ? "critical" : "ok");
1131 if ((battery->state & 0x01) && (battery->state & 0x02))
1133 "charging state: charging/discharging\n");
1134 else if (battery->state & 0x01)
1135 seq_printf(seq, "charging state: discharging\n");
1136 else if (battery->state & 0x02)
1137 seq_printf(seq, "charging state: charging\n");
1139 seq_printf(seq, "charging state: charged\n");
1141 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
1142 seq_printf(seq, "present rate: unknown\n");
1144 seq_printf(seq, "present rate: %d %s\n",
1145 battery->rate_now, acpi_battery_units(battery));
1147 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
1148 seq_printf(seq, "remaining capacity: unknown\n");
1150 seq_printf(seq, "remaining capacity: %d %sh\n",
1151 battery->capacity_now, acpi_battery_units(battery));
1152 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
1153 seq_printf(seq, "present voltage: unknown\n");
1155 seq_printf(seq, "present voltage: %d mV\n",
1156 battery->voltage_now);
1159 seq_printf(seq, "ERROR: Unable to read battery state\n");
1164 static int acpi_battery_alarm_proc_show(struct seq_file *seq, void *offset)
1166 struct acpi_battery *battery = seq->private;
1167 int result = acpi_battery_update(battery, false);
1172 if (!acpi_battery_present(battery)) {
1173 seq_printf(seq, "present: no\n");
1176 seq_printf(seq, "alarm: ");
1177 if (battery->alarm) {
1178 seq_printf(seq, "%u %sh\n", battery->alarm,
1179 acpi_battery_units(battery));
1181 seq_printf(seq, "unsupported\n");
1185 seq_printf(seq, "ERROR: Unable to read battery alarm\n");
1189 static ssize_t acpi_battery_write_alarm(struct file *file,
1190 const char __user * buffer,
1191 size_t count, loff_t * ppos)
1194 char alarm_string[12] = { '\0' };
1195 struct seq_file *m = file->private_data;
1196 struct acpi_battery *battery = m->private;
1198 if (!battery || (count > sizeof(alarm_string) - 1))
1200 if (!acpi_battery_present(battery)) {
1204 if (copy_from_user(alarm_string, buffer, count)) {
1208 alarm_string[count] = '\0';
1209 if (kstrtoint(alarm_string, 0, &battery->alarm)) {
1213 result = acpi_battery_set_alarm(battery);
1220 static int acpi_battery_alarm_proc_open(struct inode *inode, struct file *file)
1222 return single_open(file, acpi_battery_alarm_proc_show, PDE_DATA(inode));
1225 static const struct file_operations acpi_battery_alarm_fops = {
1226 .owner = THIS_MODULE,
1227 .open = acpi_battery_alarm_proc_open,
1229 .write = acpi_battery_write_alarm,
1230 .llseek = seq_lseek,
1231 .release = single_release,
1234 static int acpi_battery_add_fs(struct acpi_device *device)
1236 pr_warning(PREFIX "Deprecated procfs I/F for battery is loaded, please retry with CONFIG_ACPI_PROCFS_POWER cleared\n");
1237 if (!acpi_device_dir(device)) {
1238 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
1240 if (!acpi_device_dir(device))
1244 if (!proc_create_single_data("info", S_IRUGO, acpi_device_dir(device),
1245 acpi_battery_info_proc_show, acpi_driver_data(device)))
1247 if (!proc_create_single_data("state", S_IRUGO, acpi_device_dir(device),
1248 acpi_battery_state_proc_show, acpi_driver_data(device)))
1250 if (!proc_create_data("alarm", S_IFREG | S_IRUGO | S_IWUSR,
1251 acpi_device_dir(device), &acpi_battery_alarm_fops,
1252 acpi_driver_data(device)))
1257 static void acpi_battery_remove_fs(struct acpi_device *device)
1259 if (!acpi_device_dir(device))
1261 remove_proc_subtree(acpi_device_bid(device), acpi_battery_dir);
1262 acpi_device_dir(device) = NULL;
1267 /* --------------------------------------------------------------------------
1269 -------------------------------------------------------------------------- */
1271 static void acpi_battery_notify(struct acpi_device *device, u32 event)
1273 struct acpi_battery *battery = acpi_driver_data(device);
1274 struct power_supply *old;
1280 * On Acer Aspire V5-573G notifications are sometimes triggered too
1281 * early. For example, when AC is unplugged and notification is
1282 * triggered, battery state is still reported as "Full", and changes to
1283 * "Discharging" only after short delay, without any notification.
1285 if (battery_notification_delay_ms > 0)
1286 msleep(battery_notification_delay_ms);
1287 if (event == ACPI_BATTERY_NOTIFY_INFO)
1288 acpi_battery_refresh(battery);
1289 acpi_battery_update(battery, false);
1290 acpi_bus_generate_netlink_event(device->pnp.device_class,
1291 dev_name(&device->dev), event,
1292 acpi_battery_present(battery));
1293 acpi_notifier_call_chain(device, event, acpi_battery_present(battery));
1294 /* acpi_battery_update could remove power_supply object */
1295 if (old && battery->bat)
1296 power_supply_changed(battery->bat);
1299 static int battery_notify(struct notifier_block *nb,
1300 unsigned long mode, void *_unused)
1302 struct acpi_battery *battery = container_of(nb, struct acpi_battery,
1307 case PM_POST_HIBERNATION:
1308 case PM_POST_SUSPEND:
1309 if (!acpi_battery_present(battery))
1313 acpi_battery_refresh(battery);
1315 result = acpi_battery_get_info(battery);
1319 result = sysfs_add_battery(battery);
1324 acpi_battery_init_alarm(battery);
1325 acpi_battery_get_state(battery);
1333 battery_bix_broken_package_quirk(const struct dmi_system_id *d)
1335 battery_bix_broken_package = 1;
1340 battery_notification_delay_quirk(const struct dmi_system_id *d)
1342 battery_notification_delay_ms = 1000;
1347 battery_ac_is_broken_quirk(const struct dmi_system_id *d)
1349 battery_ac_is_broken = 1;
1354 battery_do_not_check_pmic_quirk(const struct dmi_system_id *d)
1356 battery_check_pmic = 0;
1360 static int __init battery_quirk_not_charging(const struct dmi_system_id *d)
1362 battery_quirk_notcharging = 1;
1366 static const struct dmi_system_id bat_dmi_table[] __initconst = {
1369 .callback = battery_bix_broken_package_quirk,
1371 DMI_MATCH(DMI_SYS_VENDOR, "NEC"),
1372 DMI_MATCH(DMI_PRODUCT_NAME, "PC-LZ750LS"),
1376 /* Acer Aspire V5-573G */
1377 .callback = battery_notification_delay_quirk,
1379 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
1380 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire V5-573G"),
1384 /* Point of View mobii wintab p800w */
1385 .callback = battery_ac_is_broken_quirk,
1387 DMI_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
1388 DMI_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
1389 DMI_MATCH(DMI_BIOS_VERSION, "3BAIR1013"),
1390 /* Above matches are too generic, add bios-date match */
1391 DMI_MATCH(DMI_BIOS_DATE, "08/22/2014"),
1396 .callback = battery_do_not_check_pmic_quirk,
1398 DMI_MATCH(DMI_PRODUCT_NAME, "EF20EA"),
1402 /* Lenovo Ideapad Miix 320 */
1403 .callback = battery_do_not_check_pmic_quirk,
1405 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1406 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "80XF"),
1407 DMI_EXACT_MATCH(DMI_PRODUCT_VERSION, "Lenovo MIIX 320-10ICR"),
1412 * On Lenovo ThinkPads the BIOS specification defines
1413 * a state when the bits for charging and discharging
1414 * are both set to 0. That state is "Not Charging".
1416 .callback = battery_quirk_not_charging,
1417 .ident = "Lenovo ThinkPad",
1419 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1420 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad"),
1424 /* Microsoft Surface Go 3 */
1425 .callback = battery_notification_delay_quirk,
1427 DMI_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
1428 DMI_MATCH(DMI_PRODUCT_NAME, "Surface Go 3"),
1435 * Some machines'(E,G Lenovo Z480) ECs are not stable
1436 * during boot up and this causes battery driver fails to be
1437 * probed due to failure of getting battery information
1438 * from EC sometimes. After several retries, the operation
1439 * may work. So add retry code here and 20ms sleep between
1442 static int acpi_battery_update_retry(struct acpi_battery *battery)
1446 for (retry = 5; retry; retry--) {
1447 ret = acpi_battery_update(battery, false);
1456 static int acpi_battery_add(struct acpi_device *device)
1459 struct acpi_battery *battery = NULL;
1464 if (device->dep_unmet)
1465 return -EPROBE_DEFER;
1467 battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
1470 battery->device = device;
1471 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
1472 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
1473 device->driver_data = battery;
1474 mutex_init(&battery->lock);
1475 mutex_init(&battery->sysfs_lock);
1476 if (acpi_has_method(battery->device->handle, "_BIX"))
1477 set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
1479 result = acpi_battery_update_retry(battery);
1483 #ifdef CONFIG_ACPI_PROCFS_POWER
1484 result = acpi_battery_add_fs(device);
1486 acpi_battery_remove_fs(device);
1491 pr_info(PREFIX "%s Slot [%s] (battery %s)\n",
1492 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
1493 device->status.battery_present ? "present" : "absent");
1495 battery->pm_nb.notifier_call = battery_notify;
1496 register_pm_notifier(&battery->pm_nb);
1498 device_init_wakeup(&device->dev, 1);
1503 sysfs_remove_battery(battery);
1504 mutex_destroy(&battery->lock);
1505 mutex_destroy(&battery->sysfs_lock);
1510 static int acpi_battery_remove(struct acpi_device *device)
1512 struct acpi_battery *battery = NULL;
1514 if (!device || !acpi_driver_data(device))
1516 device_init_wakeup(&device->dev, 0);
1517 battery = acpi_driver_data(device);
1518 unregister_pm_notifier(&battery->pm_nb);
1519 #ifdef CONFIG_ACPI_PROCFS_POWER
1520 acpi_battery_remove_fs(device);
1522 sysfs_remove_battery(battery);
1523 mutex_destroy(&battery->lock);
1524 mutex_destroy(&battery->sysfs_lock);
1529 #ifdef CONFIG_PM_SLEEP
1530 /* this is needed to learn about changes made in suspended state */
1531 static int acpi_battery_resume(struct device *dev)
1533 struct acpi_battery *battery;
1538 battery = acpi_driver_data(to_acpi_device(dev));
1542 battery->update_time = 0;
1543 acpi_battery_update(battery, true);
1547 #define acpi_battery_resume NULL
1550 static SIMPLE_DEV_PM_OPS(acpi_battery_pm, NULL, acpi_battery_resume);
1552 static struct acpi_driver acpi_battery_driver = {
1554 .class = ACPI_BATTERY_CLASS,
1555 .ids = battery_device_ids,
1556 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1558 .add = acpi_battery_add,
1559 .remove = acpi_battery_remove,
1560 .notify = acpi_battery_notify,
1562 .drv.pm = &acpi_battery_pm,
1565 static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
1570 dmi_check_system(bat_dmi_table);
1572 if (battery_check_pmic) {
1573 for (i = 0; i < ARRAY_SIZE(acpi_battery_blacklist); i++)
1574 if (acpi_dev_present(acpi_battery_blacklist[i], "1", -1)) {
1575 pr_info(PREFIX ACPI_BATTERY_DEVICE_NAME
1576 ": found native %s PMIC, not loading\n",
1577 acpi_battery_blacklist[i]);
1582 #ifdef CONFIG_ACPI_PROCFS_POWER
1583 acpi_battery_dir = acpi_lock_battery_dir();
1584 if (!acpi_battery_dir)
1587 result = acpi_bus_register_driver(&acpi_battery_driver);
1588 #ifdef CONFIG_ACPI_PROCFS_POWER
1590 acpi_unlock_battery_dir(acpi_battery_dir);
1592 battery_driver_registered = (result == 0);
1595 static int __init acpi_battery_init(void)
1600 async_cookie = async_schedule(acpi_battery_init_async, NULL);
1604 static void __exit acpi_battery_exit(void)
1606 async_synchronize_cookie(async_cookie + 1);
1607 if (battery_driver_registered) {
1608 acpi_bus_unregister_driver(&acpi_battery_driver);
1609 battery_hook_exit();
1611 #ifdef CONFIG_ACPI_PROCFS_POWER
1612 if (acpi_battery_dir)
1613 acpi_unlock_battery_dir(acpi_battery_dir);
1617 module_init(acpi_battery_init);
1618 module_exit(acpi_battery_exit);