blob: be743d177bcbf8ddc5089d6079c651550fb40179 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * battery.c - ACPI Battery Driver (Revision: 2.0)
4 *
5 * Copyright (C) 2007 Alexey Starikovskiy <astarikovskiy@suse.de>
6 * Copyright (C) 2004-2007 Vladimir Lebedev <vladimir.p.lebedev@intel.com>
7 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
8 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009 */
10
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
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>
24
25#include <asm/unaligned.h>
26
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000027#include <linux/acpi.h>
28#include <linux/power_supply.h>
29
30#include <acpi/battery.h>
31
32#define PREFIX "ACPI: "
33
34#define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
Olivier Deprez0e641232021-09-23 10:07:05 +020035#define ACPI_BATTERY_CAPACITY_VALID(capacity) \
36 ((capacity) != 0 && (capacity) != ACPI_BATTERY_VALUE_UNKNOWN)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000037
38#define ACPI_BATTERY_DEVICE_NAME "Battery"
39
40/* Battery power unit: 0 means mW, 1 means mA */
41#define ACPI_BATTERY_POWER_UNIT_MA 1
42
43#define ACPI_BATTERY_STATE_DISCHARGING 0x1
44#define ACPI_BATTERY_STATE_CHARGING 0x2
45#define ACPI_BATTERY_STATE_CRITICAL 0x4
46
47#define _COMPONENT ACPI_BATTERY_COMPONENT
48
49ACPI_MODULE_NAME("battery");
50
51MODULE_AUTHOR("Paul Diefenbaugh");
52MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
53MODULE_DESCRIPTION("ACPI Battery Driver");
54MODULE_LICENSE("GPL");
55
56static async_cookie_t async_cookie;
57static bool battery_driver_registered;
58static int battery_bix_broken_package;
59static int battery_notification_delay_ms;
60static int battery_ac_is_broken;
61static int battery_check_pmic = 1;
Olivier Deprez157378f2022-04-04 15:47:50 +020062static int battery_quirk_notcharging;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000063static unsigned int cache_time = 1000;
64module_param(cache_time, uint, 0644);
65MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
66
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000067static const struct acpi_device_id battery_device_ids[] = {
68 {"PNP0C0A", 0},
Olivier Deprez157378f2022-04-04 15:47:50 +020069
70 /* Microsoft Surface Go 3 */
71 {"MSHW0146", 0},
72
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000073 {"", 0},
74};
75
76MODULE_DEVICE_TABLE(acpi, battery_device_ids);
77
78/* Lists of PMIC ACPI HIDs with an (often better) native battery driver */
79static const char * const acpi_battery_blacklist[] = {
80 "INT33F4", /* X-Powers AXP288 PMIC */
81};
82
83enum {
84 ACPI_BATTERY_ALARM_PRESENT,
85 ACPI_BATTERY_XINFO_PRESENT,
86 ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY,
87 /* On Lenovo Thinkpad models from 2010 and 2011, the power unit
88 switches between mWh and mAh depending on whether the system
89 is running on battery or not. When mAh is the unit, most
90 reported values are incorrect and need to be adjusted by
91 10000/design_voltage. Verified on x201, t410, t410s, and x220.
92 Pre-2010 and 2012 models appear to always report in mWh and
93 are thus unaffected (tested with t42, t61, t500, x200, x300,
94 and x230). Also, in mid-2012 Lenovo issued a BIOS update for
95 the 2011 models that fixes the issue (tested on x220 with a
96 post-1.29 BIOS), but as of Nov. 2012, no such update is
97 available for the 2010 models. */
98 ACPI_BATTERY_QUIRK_THINKPAD_MAH,
99 /* for batteries reporting current capacity with design capacity
100 * on a full charge, but showing degradation in full charge cap.
101 */
102 ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE,
103};
104
105struct acpi_battery {
106 struct mutex lock;
107 struct mutex sysfs_lock;
108 struct power_supply *bat;
109 struct power_supply_desc bat_desc;
110 struct acpi_device *device;
111 struct notifier_block pm_nb;
112 struct list_head list;
113 unsigned long update_time;
114 int revision;
115 int rate_now;
116 int capacity_now;
117 int voltage_now;
118 int design_capacity;
119 int full_charge_capacity;
120 int technology;
121 int design_voltage;
122 int design_capacity_warning;
123 int design_capacity_low;
124 int cycle_count;
125 int measurement_accuracy;
126 int max_sampling_time;
127 int min_sampling_time;
128 int max_averaging_interval;
129 int min_averaging_interval;
130 int capacity_granularity_1;
131 int capacity_granularity_2;
132 int alarm;
133 char model_number[32];
134 char serial_number[32];
135 char type[32];
136 char oem_info[32];
137 int state;
138 int power_unit;
139 unsigned long flags;
140};
141
142#define to_acpi_battery(x) power_supply_get_drvdata(x)
143
144static inline int acpi_battery_present(struct acpi_battery *battery)
145{
146 return battery->device->status.battery_present;
147}
148
149static int acpi_battery_technology(struct acpi_battery *battery)
150{
151 if (!strcasecmp("NiCd", battery->type))
152 return POWER_SUPPLY_TECHNOLOGY_NiCd;
153 if (!strcasecmp("NiMH", battery->type))
154 return POWER_SUPPLY_TECHNOLOGY_NiMH;
155 if (!strcasecmp("LION", battery->type))
156 return POWER_SUPPLY_TECHNOLOGY_LION;
157 if (!strncasecmp("LI-ION", battery->type, 6))
158 return POWER_SUPPLY_TECHNOLOGY_LION;
159 if (!strcasecmp("LiP", battery->type))
160 return POWER_SUPPLY_TECHNOLOGY_LIPO;
161 return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
162}
163
164static int acpi_battery_get_state(struct acpi_battery *battery);
165
166static int acpi_battery_is_charged(struct acpi_battery *battery)
167{
168 /* charging, discharging or critical low */
169 if (battery->state != 0)
170 return 0;
171
172 /* battery not reporting charge */
173 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
174 battery->capacity_now == 0)
175 return 0;
176
177 /* good batteries update full_charge as the batteries degrade */
178 if (battery->full_charge_capacity == battery->capacity_now)
179 return 1;
180
181 /* fallback to using design values for broken batteries */
Olivier Deprez157378f2022-04-04 15:47:50 +0200182 if (battery->design_capacity <= battery->capacity_now)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000183 return 1;
184
185 /* we don't do any sort of metric based on percentages */
186 return 0;
187}
188
189static bool acpi_battery_is_degraded(struct acpi_battery *battery)
190{
Olivier Deprez0e641232021-09-23 10:07:05 +0200191 return ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity) &&
192 ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity) &&
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000193 battery->full_charge_capacity < battery->design_capacity;
194}
195
196static int acpi_battery_handle_discharging(struct acpi_battery *battery)
197{
198 /*
199 * Some devices wrongly report discharging if the battery's charge level
200 * was above the device's start charging threshold atm the AC adapter
201 * was plugged in and the device thus did not start a new charge cycle.
202 */
203 if ((battery_ac_is_broken || power_supply_is_system_supplied()) &&
204 battery->rate_now == 0)
205 return POWER_SUPPLY_STATUS_NOT_CHARGING;
206
207 return POWER_SUPPLY_STATUS_DISCHARGING;
208}
209
210static int acpi_battery_get_property(struct power_supply *psy,
211 enum power_supply_property psp,
212 union power_supply_propval *val)
213{
Olivier Deprez0e641232021-09-23 10:07:05 +0200214 int full_capacity = ACPI_BATTERY_VALUE_UNKNOWN, ret = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000215 struct acpi_battery *battery = to_acpi_battery(psy);
216
217 if (acpi_battery_present(battery)) {
218 /* run battery update only if it is present */
219 acpi_battery_get_state(battery);
220 } else if (psp != POWER_SUPPLY_PROP_PRESENT)
221 return -ENODEV;
222 switch (psp) {
223 case POWER_SUPPLY_PROP_STATUS:
224 if (battery->state & ACPI_BATTERY_STATE_DISCHARGING)
225 val->intval = acpi_battery_handle_discharging(battery);
226 else if (battery->state & ACPI_BATTERY_STATE_CHARGING)
227 val->intval = POWER_SUPPLY_STATUS_CHARGING;
228 else if (acpi_battery_is_charged(battery))
229 val->intval = POWER_SUPPLY_STATUS_FULL;
Olivier Deprez157378f2022-04-04 15:47:50 +0200230 else if (battery_quirk_notcharging)
231 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000232 else
233 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
234 break;
235 case POWER_SUPPLY_PROP_PRESENT:
236 val->intval = acpi_battery_present(battery);
237 break;
238 case POWER_SUPPLY_PROP_TECHNOLOGY:
239 val->intval = acpi_battery_technology(battery);
240 break;
241 case POWER_SUPPLY_PROP_CYCLE_COUNT:
242 val->intval = battery->cycle_count;
243 break;
244 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
245 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
246 ret = -ENODEV;
247 else
248 val->intval = battery->design_voltage * 1000;
249 break;
250 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
251 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
252 ret = -ENODEV;
253 else
254 val->intval = battery->voltage_now * 1000;
255 break;
256 case POWER_SUPPLY_PROP_CURRENT_NOW:
257 case POWER_SUPPLY_PROP_POWER_NOW:
258 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
259 ret = -ENODEV;
260 else
261 val->intval = battery->rate_now * 1000;
262 break;
263 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
264 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
Olivier Deprez0e641232021-09-23 10:07:05 +0200265 if (!ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000266 ret = -ENODEV;
267 else
268 val->intval = battery->design_capacity * 1000;
269 break;
270 case POWER_SUPPLY_PROP_CHARGE_FULL:
271 case POWER_SUPPLY_PROP_ENERGY_FULL:
Olivier Deprez0e641232021-09-23 10:07:05 +0200272 if (!ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000273 ret = -ENODEV;
274 else
275 val->intval = battery->full_charge_capacity * 1000;
276 break;
277 case POWER_SUPPLY_PROP_CHARGE_NOW:
278 case POWER_SUPPLY_PROP_ENERGY_NOW:
279 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
280 ret = -ENODEV;
281 else
282 val->intval = battery->capacity_now * 1000;
283 break;
284 case POWER_SUPPLY_PROP_CAPACITY:
Olivier Deprez0e641232021-09-23 10:07:05 +0200285 if (ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity))
286 full_capacity = battery->full_charge_capacity;
287 else if (ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity))
288 full_capacity = battery->design_capacity;
289
290 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
291 full_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
292 ret = -ENODEV;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000293 else
Olivier Deprez0e641232021-09-23 10:07:05 +0200294 val->intval = battery->capacity_now * 100/
295 full_capacity;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000296 break;
297 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
298 if (battery->state & ACPI_BATTERY_STATE_CRITICAL)
299 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
300 else if (test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags) &&
301 (battery->capacity_now <= battery->alarm))
302 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
303 else if (acpi_battery_is_charged(battery))
304 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
305 else
306 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
307 break;
308 case POWER_SUPPLY_PROP_MODEL_NAME:
309 val->strval = battery->model_number;
310 break;
311 case POWER_SUPPLY_PROP_MANUFACTURER:
312 val->strval = battery->oem_info;
313 break;
314 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
315 val->strval = battery->serial_number;
316 break;
317 default:
318 ret = -EINVAL;
319 }
320 return ret;
321}
322
323static enum power_supply_property charge_battery_props[] = {
324 POWER_SUPPLY_PROP_STATUS,
325 POWER_SUPPLY_PROP_PRESENT,
326 POWER_SUPPLY_PROP_TECHNOLOGY,
327 POWER_SUPPLY_PROP_CYCLE_COUNT,
328 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
329 POWER_SUPPLY_PROP_VOLTAGE_NOW,
330 POWER_SUPPLY_PROP_CURRENT_NOW,
331 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
332 POWER_SUPPLY_PROP_CHARGE_FULL,
333 POWER_SUPPLY_PROP_CHARGE_NOW,
334 POWER_SUPPLY_PROP_CAPACITY,
335 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
336 POWER_SUPPLY_PROP_MODEL_NAME,
337 POWER_SUPPLY_PROP_MANUFACTURER,
338 POWER_SUPPLY_PROP_SERIAL_NUMBER,
339};
340
Olivier Deprez0e641232021-09-23 10:07:05 +0200341static enum power_supply_property charge_battery_full_cap_broken_props[] = {
342 POWER_SUPPLY_PROP_STATUS,
343 POWER_SUPPLY_PROP_PRESENT,
344 POWER_SUPPLY_PROP_TECHNOLOGY,
345 POWER_SUPPLY_PROP_CYCLE_COUNT,
346 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
347 POWER_SUPPLY_PROP_VOLTAGE_NOW,
348 POWER_SUPPLY_PROP_CURRENT_NOW,
349 POWER_SUPPLY_PROP_CHARGE_NOW,
350 POWER_SUPPLY_PROP_MODEL_NAME,
351 POWER_SUPPLY_PROP_MANUFACTURER,
352 POWER_SUPPLY_PROP_SERIAL_NUMBER,
353};
354
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000355static enum power_supply_property energy_battery_props[] = {
356 POWER_SUPPLY_PROP_STATUS,
357 POWER_SUPPLY_PROP_PRESENT,
358 POWER_SUPPLY_PROP_TECHNOLOGY,
359 POWER_SUPPLY_PROP_CYCLE_COUNT,
360 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
361 POWER_SUPPLY_PROP_VOLTAGE_NOW,
362 POWER_SUPPLY_PROP_POWER_NOW,
363 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
364 POWER_SUPPLY_PROP_ENERGY_FULL,
365 POWER_SUPPLY_PROP_ENERGY_NOW,
366 POWER_SUPPLY_PROP_CAPACITY,
367 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
368 POWER_SUPPLY_PROP_MODEL_NAME,
369 POWER_SUPPLY_PROP_MANUFACTURER,
370 POWER_SUPPLY_PROP_SERIAL_NUMBER,
371};
372
373static enum power_supply_property energy_battery_full_cap_broken_props[] = {
374 POWER_SUPPLY_PROP_STATUS,
375 POWER_SUPPLY_PROP_PRESENT,
376 POWER_SUPPLY_PROP_TECHNOLOGY,
377 POWER_SUPPLY_PROP_CYCLE_COUNT,
378 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
379 POWER_SUPPLY_PROP_VOLTAGE_NOW,
380 POWER_SUPPLY_PROP_POWER_NOW,
381 POWER_SUPPLY_PROP_ENERGY_NOW,
382 POWER_SUPPLY_PROP_MODEL_NAME,
383 POWER_SUPPLY_PROP_MANUFACTURER,
384 POWER_SUPPLY_PROP_SERIAL_NUMBER,
385};
386
387/* --------------------------------------------------------------------------
388 Battery Management
389 -------------------------------------------------------------------------- */
390struct acpi_offsets {
391 size_t offset; /* offset inside struct acpi_sbs_battery */
392 u8 mode; /* int or string? */
393};
394
395static const struct acpi_offsets state_offsets[] = {
396 {offsetof(struct acpi_battery, state), 0},
397 {offsetof(struct acpi_battery, rate_now), 0},
398 {offsetof(struct acpi_battery, capacity_now), 0},
399 {offsetof(struct acpi_battery, voltage_now), 0},
400};
401
402static const struct acpi_offsets info_offsets[] = {
403 {offsetof(struct acpi_battery, power_unit), 0},
404 {offsetof(struct acpi_battery, design_capacity), 0},
405 {offsetof(struct acpi_battery, full_charge_capacity), 0},
406 {offsetof(struct acpi_battery, technology), 0},
407 {offsetof(struct acpi_battery, design_voltage), 0},
408 {offsetof(struct acpi_battery, design_capacity_warning), 0},
409 {offsetof(struct acpi_battery, design_capacity_low), 0},
410 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
411 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
412 {offsetof(struct acpi_battery, model_number), 1},
413 {offsetof(struct acpi_battery, serial_number), 1},
414 {offsetof(struct acpi_battery, type), 1},
415 {offsetof(struct acpi_battery, oem_info), 1},
416};
417
418static const struct acpi_offsets extended_info_offsets[] = {
419 {offsetof(struct acpi_battery, revision), 0},
420 {offsetof(struct acpi_battery, power_unit), 0},
421 {offsetof(struct acpi_battery, design_capacity), 0},
422 {offsetof(struct acpi_battery, full_charge_capacity), 0},
423 {offsetof(struct acpi_battery, technology), 0},
424 {offsetof(struct acpi_battery, design_voltage), 0},
425 {offsetof(struct acpi_battery, design_capacity_warning), 0},
426 {offsetof(struct acpi_battery, design_capacity_low), 0},
427 {offsetof(struct acpi_battery, cycle_count), 0},
428 {offsetof(struct acpi_battery, measurement_accuracy), 0},
429 {offsetof(struct acpi_battery, max_sampling_time), 0},
430 {offsetof(struct acpi_battery, min_sampling_time), 0},
431 {offsetof(struct acpi_battery, max_averaging_interval), 0},
432 {offsetof(struct acpi_battery, min_averaging_interval), 0},
433 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
434 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
435 {offsetof(struct acpi_battery, model_number), 1},
436 {offsetof(struct acpi_battery, serial_number), 1},
437 {offsetof(struct acpi_battery, type), 1},
438 {offsetof(struct acpi_battery, oem_info), 1},
439};
440
441static int extract_package(struct acpi_battery *battery,
442 union acpi_object *package,
443 const struct acpi_offsets *offsets, int num)
444{
445 int i;
446 union acpi_object *element;
447 if (package->type != ACPI_TYPE_PACKAGE)
448 return -EFAULT;
449 for (i = 0; i < num; ++i) {
450 if (package->package.count <= i)
451 return -EFAULT;
452 element = &package->package.elements[i];
453 if (offsets[i].mode) {
454 u8 *ptr = (u8 *)battery + offsets[i].offset;
455 if (element->type == ACPI_TYPE_STRING ||
456 element->type == ACPI_TYPE_BUFFER)
457 strncpy(ptr, element->string.pointer, 32);
458 else if (element->type == ACPI_TYPE_INTEGER) {
459 strncpy(ptr, (u8 *)&element->integer.value,
460 sizeof(u64));
461 ptr[sizeof(u64)] = 0;
462 } else
463 *ptr = 0; /* don't have value */
464 } else {
465 int *x = (int *)((u8 *)battery + offsets[i].offset);
466 *x = (element->type == ACPI_TYPE_INTEGER) ?
467 element->integer.value : -1;
468 }
469 }
470 return 0;
471}
472
473static int acpi_battery_get_status(struct acpi_battery *battery)
474{
475 if (acpi_bus_get_status(battery->device)) {
476 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
477 return -ENODEV;
478 }
479 return 0;
480}
481
482
483static int extract_battery_info(const int use_bix,
484 struct acpi_battery *battery,
485 const struct acpi_buffer *buffer)
486{
487 int result = -EFAULT;
488
489 if (use_bix && battery_bix_broken_package)
490 result = extract_package(battery, buffer->pointer,
491 extended_info_offsets + 1,
492 ARRAY_SIZE(extended_info_offsets) - 1);
493 else if (use_bix)
494 result = extract_package(battery, buffer->pointer,
495 extended_info_offsets,
496 ARRAY_SIZE(extended_info_offsets));
497 else
498 result = extract_package(battery, buffer->pointer,
499 info_offsets, ARRAY_SIZE(info_offsets));
500 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
501 battery->full_charge_capacity = battery->design_capacity;
502 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) &&
503 battery->power_unit && battery->design_voltage) {
504 battery->design_capacity = battery->design_capacity *
505 10000 / battery->design_voltage;
506 battery->full_charge_capacity = battery->full_charge_capacity *
507 10000 / battery->design_voltage;
508 battery->design_capacity_warning =
509 battery->design_capacity_warning *
510 10000 / battery->design_voltage;
511 /* Curiously, design_capacity_low, unlike the rest of them,
512 is correct. */
513 /* capacity_granularity_* equal 1 on the systems tested, so
514 it's impossible to tell if they would need an adjustment
515 or not if their values were higher. */
516 }
517 if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags) &&
518 battery->capacity_now > battery->full_charge_capacity)
519 battery->capacity_now = battery->full_charge_capacity;
520
521 return result;
522}
523
524static int acpi_battery_get_info(struct acpi_battery *battery)
525{
526 const int xinfo = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
527 int use_bix;
528 int result = -ENODEV;
529
530 if (!acpi_battery_present(battery))
531 return 0;
532
533
534 for (use_bix = xinfo ? 1 : 0; use_bix >= 0; use_bix--) {
535 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
536 acpi_status status = AE_ERROR;
537
538 mutex_lock(&battery->lock);
539 status = acpi_evaluate_object(battery->device->handle,
540 use_bix ? "_BIX":"_BIF",
541 NULL, &buffer);
542 mutex_unlock(&battery->lock);
543
544 if (ACPI_FAILURE(status)) {
545 ACPI_EXCEPTION((AE_INFO, status, "Evaluating %s",
546 use_bix ? "_BIX":"_BIF"));
547 } else {
548 result = extract_battery_info(use_bix,
549 battery,
550 &buffer);
551
552 kfree(buffer.pointer);
553 break;
554 }
555 }
556
557 if (!result && !use_bix && xinfo)
558 pr_warn(FW_BUG "The _BIX method is broken, using _BIF.\n");
559
560 return result;
561}
562
563static int acpi_battery_get_state(struct acpi_battery *battery)
564{
565 int result = 0;
566 acpi_status status = 0;
567 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
568
569 if (!acpi_battery_present(battery))
570 return 0;
571
572 if (battery->update_time &&
573 time_before(jiffies, battery->update_time +
574 msecs_to_jiffies(cache_time)))
575 return 0;
576
577 mutex_lock(&battery->lock);
578 status = acpi_evaluate_object(battery->device->handle, "_BST",
579 NULL, &buffer);
580 mutex_unlock(&battery->lock);
581
582 if (ACPI_FAILURE(status)) {
583 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
584 return -ENODEV;
585 }
586
587 result = extract_package(battery, buffer.pointer,
588 state_offsets, ARRAY_SIZE(state_offsets));
589 battery->update_time = jiffies;
590 kfree(buffer.pointer);
591
592 /* For buggy DSDTs that report negative 16-bit values for either
593 * charging or discharging current and/or report 0 as 65536
594 * due to bad math.
595 */
596 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA &&
597 battery->rate_now != ACPI_BATTERY_VALUE_UNKNOWN &&
598 (s16)(battery->rate_now) < 0) {
599 battery->rate_now = abs((s16)battery->rate_now);
600 pr_warn_once(FW_BUG "battery: (dis)charge rate invalid.\n");
601 }
602
603 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)
604 && battery->capacity_now >= 0 && battery->capacity_now <= 100)
605 battery->capacity_now = (battery->capacity_now *
606 battery->full_charge_capacity) / 100;
607 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) &&
608 battery->power_unit && battery->design_voltage) {
609 battery->capacity_now = battery->capacity_now *
610 10000 / battery->design_voltage;
611 }
612 if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags) &&
613 battery->capacity_now > battery->full_charge_capacity)
614 battery->capacity_now = battery->full_charge_capacity;
615
616 return result;
617}
618
619static int acpi_battery_set_alarm(struct acpi_battery *battery)
620{
621 acpi_status status = 0;
622
623 if (!acpi_battery_present(battery) ||
624 !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags))
625 return -ENODEV;
626
627 mutex_lock(&battery->lock);
628 status = acpi_execute_simple_method(battery->device->handle, "_BTP",
629 battery->alarm);
630 mutex_unlock(&battery->lock);
631
632 if (ACPI_FAILURE(status))
633 return -ENODEV;
634
635 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm));
636 return 0;
637}
638
639static int acpi_battery_init_alarm(struct acpi_battery *battery)
640{
641 /* See if alarms are supported, and if so, set default */
642 if (!acpi_has_method(battery->device->handle, "_BTP")) {
643 clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
644 return 0;
645 }
646 set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
647 if (!battery->alarm)
648 battery->alarm = battery->design_capacity_warning;
649 return acpi_battery_set_alarm(battery);
650}
651
652static ssize_t acpi_battery_alarm_show(struct device *dev,
653 struct device_attribute *attr,
654 char *buf)
655{
656 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
657 return sprintf(buf, "%d\n", battery->alarm * 1000);
658}
659
660static ssize_t acpi_battery_alarm_store(struct device *dev,
661 struct device_attribute *attr,
662 const char *buf, size_t count)
663{
664 unsigned long x;
665 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
666 if (sscanf(buf, "%lu\n", &x) == 1)
667 battery->alarm = x/1000;
668 if (acpi_battery_present(battery))
669 acpi_battery_set_alarm(battery);
670 return count;
671}
672
673static const struct device_attribute alarm_attr = {
674 .attr = {.name = "alarm", .mode = 0644},
675 .show = acpi_battery_alarm_show,
676 .store = acpi_battery_alarm_store,
677};
678
679/*
680 * The Battery Hooking API
681 *
682 * This API is used inside other drivers that need to expose
683 * platform-specific behaviour within the generic driver in a
684 * generic way.
685 *
686 */
687
688static LIST_HEAD(acpi_battery_list);
689static LIST_HEAD(battery_hook_list);
690static DEFINE_MUTEX(hook_mutex);
691
692static void __battery_hook_unregister(struct acpi_battery_hook *hook, int lock)
693{
694 struct acpi_battery *battery;
695 /*
696 * In order to remove a hook, we first need to
697 * de-register all the batteries that are registered.
698 */
699 if (lock)
700 mutex_lock(&hook_mutex);
701 list_for_each_entry(battery, &acpi_battery_list, list) {
702 hook->remove_battery(battery->bat);
703 }
704 list_del(&hook->list);
705 if (lock)
706 mutex_unlock(&hook_mutex);
707 pr_info("extension unregistered: %s\n", hook->name);
708}
709
710void battery_hook_unregister(struct acpi_battery_hook *hook)
711{
712 __battery_hook_unregister(hook, 1);
713}
714EXPORT_SYMBOL_GPL(battery_hook_unregister);
715
716void battery_hook_register(struct acpi_battery_hook *hook)
717{
718 struct acpi_battery *battery;
719
720 mutex_lock(&hook_mutex);
721 INIT_LIST_HEAD(&hook->list);
722 list_add(&hook->list, &battery_hook_list);
723 /*
724 * Now that the driver is registered, we need
725 * to notify the hook that a battery is available
726 * for each battery, so that the driver may add
727 * its attributes.
728 */
729 list_for_each_entry(battery, &acpi_battery_list, list) {
730 if (hook->add_battery(battery->bat)) {
731 /*
732 * If a add-battery returns non-zero,
733 * the registration of the extension has failed,
734 * and we will not add it to the list of loaded
735 * hooks.
736 */
737 pr_err("extension failed to load: %s", hook->name);
738 __battery_hook_unregister(hook, 0);
739 goto end;
740 }
741 }
742 pr_info("new extension: %s\n", hook->name);
743end:
744 mutex_unlock(&hook_mutex);
745}
746EXPORT_SYMBOL_GPL(battery_hook_register);
747
748/*
749 * This function gets called right after the battery sysfs
750 * attributes have been added, so that the drivers that
751 * define custom sysfs attributes can add their own.
752*/
753static void battery_hook_add_battery(struct acpi_battery *battery)
754{
755 struct acpi_battery_hook *hook_node, *tmp;
756
757 mutex_lock(&hook_mutex);
758 INIT_LIST_HEAD(&battery->list);
759 list_add(&battery->list, &acpi_battery_list);
760 /*
761 * Since we added a new battery to the list, we need to
762 * iterate over the hooks and call add_battery for each
763 * hook that was registered. This usually happens
764 * when a battery gets hotplugged or initialized
765 * during the battery module initialization.
766 */
767 list_for_each_entry_safe(hook_node, tmp, &battery_hook_list, list) {
768 if (hook_node->add_battery(battery->bat)) {
769 /*
770 * The notification of the extensions has failed, to
771 * prevent further errors we will unload the extension.
772 */
773 pr_err("error in extension, unloading: %s",
774 hook_node->name);
775 __battery_hook_unregister(hook_node, 0);
776 }
777 }
778 mutex_unlock(&hook_mutex);
779}
780
781static void battery_hook_remove_battery(struct acpi_battery *battery)
782{
783 struct acpi_battery_hook *hook;
784
785 mutex_lock(&hook_mutex);
786 /*
787 * Before removing the hook, we need to remove all
788 * custom attributes from the battery.
789 */
790 list_for_each_entry(hook, &battery_hook_list, list) {
791 hook->remove_battery(battery->bat);
792 }
793 /* Then, just remove the battery from the list */
794 list_del(&battery->list);
795 mutex_unlock(&hook_mutex);
796}
797
798static void __exit battery_hook_exit(void)
799{
800 struct acpi_battery_hook *hook;
801 struct acpi_battery_hook *ptr;
802 /*
803 * At this point, the acpi_bus_unregister_driver()
804 * has called remove for all batteries. We just
805 * need to remove the hooks.
806 */
807 list_for_each_entry_safe(hook, ptr, &battery_hook_list, list) {
808 __battery_hook_unregister(hook, 1);
809 }
810 mutex_destroy(&hook_mutex);
811}
812
813static int sysfs_add_battery(struct acpi_battery *battery)
814{
815 struct power_supply_config psy_cfg = { .drv_data = battery, };
Olivier Deprez0e641232021-09-23 10:07:05 +0200816 bool full_cap_broken = false;
817
818 if (!ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity) &&
819 !ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity))
820 full_cap_broken = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000821
822 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) {
Olivier Deprez0e641232021-09-23 10:07:05 +0200823 if (full_cap_broken) {
824 battery->bat_desc.properties =
825 charge_battery_full_cap_broken_props;
826 battery->bat_desc.num_properties =
827 ARRAY_SIZE(charge_battery_full_cap_broken_props);
828 } else {
829 battery->bat_desc.properties = charge_battery_props;
830 battery->bat_desc.num_properties =
831 ARRAY_SIZE(charge_battery_props);
832 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000833 } else {
Olivier Deprez0e641232021-09-23 10:07:05 +0200834 if (full_cap_broken) {
835 battery->bat_desc.properties =
836 energy_battery_full_cap_broken_props;
837 battery->bat_desc.num_properties =
838 ARRAY_SIZE(energy_battery_full_cap_broken_props);
839 } else {
840 battery->bat_desc.properties = energy_battery_props;
841 battery->bat_desc.num_properties =
842 ARRAY_SIZE(energy_battery_props);
843 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000844 }
845
846 battery->bat_desc.name = acpi_device_bid(battery->device);
847 battery->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY;
848 battery->bat_desc.get_property = acpi_battery_get_property;
849
850 battery->bat = power_supply_register_no_ws(&battery->device->dev,
851 &battery->bat_desc, &psy_cfg);
852
853 if (IS_ERR(battery->bat)) {
854 int result = PTR_ERR(battery->bat);
855
856 battery->bat = NULL;
857 return result;
858 }
859 battery_hook_add_battery(battery);
860 return device_create_file(&battery->bat->dev, &alarm_attr);
861}
862
863static void sysfs_remove_battery(struct acpi_battery *battery)
864{
865 mutex_lock(&battery->sysfs_lock);
866 if (!battery->bat) {
867 mutex_unlock(&battery->sysfs_lock);
868 return;
869 }
870 battery_hook_remove_battery(battery);
871 device_remove_file(&battery->bat->dev, &alarm_attr);
872 power_supply_unregister(battery->bat);
873 battery->bat = NULL;
874 mutex_unlock(&battery->sysfs_lock);
875}
876
877static void find_battery(const struct dmi_header *dm, void *private)
878{
879 struct acpi_battery *battery = (struct acpi_battery *)private;
880 /* Note: the hardcoded offsets below have been extracted from
881 the source code of dmidecode. */
882 if (dm->type == DMI_ENTRY_PORTABLE_BATTERY && dm->length >= 8) {
883 const u8 *dmi_data = (const u8 *)(dm + 1);
884 int dmi_capacity = get_unaligned((const u16 *)(dmi_data + 6));
885 if (dm->length >= 18)
886 dmi_capacity *= dmi_data[17];
887 if (battery->design_capacity * battery->design_voltage / 1000
888 != dmi_capacity &&
889 battery->design_capacity * 10 == dmi_capacity)
890 set_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH,
891 &battery->flags);
892 }
893}
894
895/*
896 * According to the ACPI spec, some kinds of primary batteries can
897 * report percentage battery remaining capacity directly to OS.
898 * In this case, it reports the Last Full Charged Capacity == 100
899 * and BatteryPresentRate == 0xFFFFFFFF.
900 *
901 * Now we found some battery reports percentage remaining capacity
902 * even if it's rechargeable.
903 * https://bugzilla.kernel.org/show_bug.cgi?id=15979
904 *
905 * Handle this correctly so that they won't break userspace.
906 */
907static void acpi_battery_quirks(struct acpi_battery *battery)
908{
909 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
910 return;
911
912 if (battery->full_charge_capacity == 100 &&
913 battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN &&
914 battery->capacity_now >= 0 && battery->capacity_now <= 100) {
915 set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags);
916 battery->full_charge_capacity = battery->design_capacity;
917 battery->capacity_now = (battery->capacity_now *
918 battery->full_charge_capacity) / 100;
919 }
920
921 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags))
922 return;
923
924 if (battery->power_unit && dmi_name_in_vendors("LENOVO")) {
925 const char *s;
926 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
927 if (s && !strncasecmp(s, "ThinkPad", 8)) {
928 dmi_walk(find_battery, battery);
929 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH,
930 &battery->flags) &&
931 battery->design_voltage) {
932 battery->design_capacity =
933 battery->design_capacity *
934 10000 / battery->design_voltage;
935 battery->full_charge_capacity =
936 battery->full_charge_capacity *
937 10000 / battery->design_voltage;
938 battery->design_capacity_warning =
939 battery->design_capacity_warning *
940 10000 / battery->design_voltage;
941 battery->capacity_now = battery->capacity_now *
942 10000 / battery->design_voltage;
943 }
944 }
945 }
946
947 if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags))
948 return;
949
950 if (acpi_battery_is_degraded(battery) &&
951 battery->capacity_now > battery->full_charge_capacity) {
952 set_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags);
953 battery->capacity_now = battery->full_charge_capacity;
954 }
955}
956
957static int acpi_battery_update(struct acpi_battery *battery, bool resume)
958{
959 int result = acpi_battery_get_status(battery);
960
961 if (result)
962 return result;
963
964 if (!acpi_battery_present(battery)) {
965 sysfs_remove_battery(battery);
966 battery->update_time = 0;
967 return 0;
968 }
969
970 if (resume)
971 return 0;
972
973 if (!battery->update_time) {
974 result = acpi_battery_get_info(battery);
975 if (result)
976 return result;
977 acpi_battery_init_alarm(battery);
978 }
979
980 result = acpi_battery_get_state(battery);
981 if (result)
982 return result;
983 acpi_battery_quirks(battery);
984
985 if (!battery->bat) {
986 result = sysfs_add_battery(battery);
987 if (result)
988 return result;
989 }
990
991 /*
992 * Wakeup the system if battery is critical low
993 * or lower than the alarm level
994 */
995 if ((battery->state & ACPI_BATTERY_STATE_CRITICAL) ||
996 (test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags) &&
Olivier Deprez157378f2022-04-04 15:47:50 +0200997 (battery->capacity_now <= battery->alarm)))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000998 acpi_pm_wakeup_event(&battery->device->dev);
999
1000 return result;
1001}
1002
1003static void acpi_battery_refresh(struct acpi_battery *battery)
1004{
1005 int power_unit;
1006
1007 if (!battery->bat)
1008 return;
1009
1010 power_unit = battery->power_unit;
1011
1012 acpi_battery_get_info(battery);
1013
1014 if (power_unit == battery->power_unit)
1015 return;
1016
1017 /* The battery has changed its reporting units. */
1018 sysfs_remove_battery(battery);
1019 sysfs_add_battery(battery);
1020}
1021
1022/* --------------------------------------------------------------------------
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001023 Driver Interface
1024 -------------------------------------------------------------------------- */
1025
1026static void acpi_battery_notify(struct acpi_device *device, u32 event)
1027{
1028 struct acpi_battery *battery = acpi_driver_data(device);
1029 struct power_supply *old;
1030
1031 if (!battery)
1032 return;
1033 old = battery->bat;
1034 /*
1035 * On Acer Aspire V5-573G notifications are sometimes triggered too
1036 * early. For example, when AC is unplugged and notification is
1037 * triggered, battery state is still reported as "Full", and changes to
1038 * "Discharging" only after short delay, without any notification.
1039 */
1040 if (battery_notification_delay_ms > 0)
1041 msleep(battery_notification_delay_ms);
1042 if (event == ACPI_BATTERY_NOTIFY_INFO)
1043 acpi_battery_refresh(battery);
1044 acpi_battery_update(battery, false);
1045 acpi_bus_generate_netlink_event(device->pnp.device_class,
1046 dev_name(&device->dev), event,
1047 acpi_battery_present(battery));
1048 acpi_notifier_call_chain(device, event, acpi_battery_present(battery));
1049 /* acpi_battery_update could remove power_supply object */
1050 if (old && battery->bat)
1051 power_supply_changed(battery->bat);
1052}
1053
1054static int battery_notify(struct notifier_block *nb,
1055 unsigned long mode, void *_unused)
1056{
1057 struct acpi_battery *battery = container_of(nb, struct acpi_battery,
1058 pm_nb);
1059 int result;
1060
1061 switch (mode) {
1062 case PM_POST_HIBERNATION:
1063 case PM_POST_SUSPEND:
1064 if (!acpi_battery_present(battery))
1065 return 0;
1066
1067 if (battery->bat) {
1068 acpi_battery_refresh(battery);
1069 } else {
1070 result = acpi_battery_get_info(battery);
1071 if (result)
1072 return result;
1073
1074 result = sysfs_add_battery(battery);
1075 if (result)
1076 return result;
1077 }
1078
1079 acpi_battery_init_alarm(battery);
1080 acpi_battery_get_state(battery);
1081 break;
1082 }
1083
1084 return 0;
1085}
1086
1087static int __init
1088battery_bix_broken_package_quirk(const struct dmi_system_id *d)
1089{
1090 battery_bix_broken_package = 1;
1091 return 0;
1092}
1093
1094static int __init
1095battery_notification_delay_quirk(const struct dmi_system_id *d)
1096{
1097 battery_notification_delay_ms = 1000;
1098 return 0;
1099}
1100
1101static int __init
1102battery_ac_is_broken_quirk(const struct dmi_system_id *d)
1103{
1104 battery_ac_is_broken = 1;
1105 return 0;
1106}
1107
1108static int __init
1109battery_do_not_check_pmic_quirk(const struct dmi_system_id *d)
1110{
1111 battery_check_pmic = 0;
1112 return 0;
1113}
1114
Olivier Deprez157378f2022-04-04 15:47:50 +02001115static int __init battery_quirk_not_charging(const struct dmi_system_id *d)
1116{
1117 battery_quirk_notcharging = 1;
1118 return 0;
1119}
1120
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001121static const struct dmi_system_id bat_dmi_table[] __initconst = {
1122 {
1123 /* NEC LZ750/LS */
1124 .callback = battery_bix_broken_package_quirk,
1125 .matches = {
1126 DMI_MATCH(DMI_SYS_VENDOR, "NEC"),
1127 DMI_MATCH(DMI_PRODUCT_NAME, "PC-LZ750LS"),
1128 },
1129 },
1130 {
1131 /* Acer Aspire V5-573G */
1132 .callback = battery_notification_delay_quirk,
1133 .matches = {
1134 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
1135 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire V5-573G"),
1136 },
1137 },
1138 {
1139 /* Point of View mobii wintab p800w */
1140 .callback = battery_ac_is_broken_quirk,
1141 .matches = {
1142 DMI_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
1143 DMI_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
1144 DMI_MATCH(DMI_BIOS_VERSION, "3BAIR1013"),
1145 /* Above matches are too generic, add bios-date match */
1146 DMI_MATCH(DMI_BIOS_DATE, "08/22/2014"),
1147 },
1148 },
1149 {
Olivier Deprez157378f2022-04-04 15:47:50 +02001150 /* ECS EF20EA, AXP288 PMIC but uses separate fuel-gauge */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001151 .callback = battery_do_not_check_pmic_quirk,
1152 .matches = {
1153 DMI_MATCH(DMI_PRODUCT_NAME, "EF20EA"),
1154 },
1155 },
1156 {
Olivier Deprez157378f2022-04-04 15:47:50 +02001157 /* Lenovo Ideapad Miix 320, AXP288 PMIC, separate fuel-gauge */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001158 .callback = battery_do_not_check_pmic_quirk,
1159 .matches = {
Olivier Deprez157378f2022-04-04 15:47:50 +02001160 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1161 DMI_MATCH(DMI_PRODUCT_NAME, "80XF"),
1162 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo MIIX 320-10ICR"),
1163 },
1164 },
1165 {
1166 /*
1167 * On Lenovo ThinkPads the BIOS specification defines
1168 * a state when the bits for charging and discharging
1169 * are both set to 0. That state is "Not Charging".
1170 */
1171 .callback = battery_quirk_not_charging,
1172 .ident = "Lenovo ThinkPad",
1173 .matches = {
1174 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1175 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad"),
1176 },
1177 },
1178 {
1179 /* Microsoft Surface Go 3 */
1180 .callback = battery_notification_delay_quirk,
1181 .matches = {
1182 DMI_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
1183 DMI_MATCH(DMI_PRODUCT_NAME, "Surface Go 3"),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001184 },
1185 },
1186 {},
1187};
1188
1189/*
1190 * Some machines'(E,G Lenovo Z480) ECs are not stable
1191 * during boot up and this causes battery driver fails to be
1192 * probed due to failure of getting battery information
1193 * from EC sometimes. After several retries, the operation
1194 * may work. So add retry code here and 20ms sleep between
1195 * every retries.
1196 */
1197static int acpi_battery_update_retry(struct acpi_battery *battery)
1198{
1199 int retry, ret;
1200
1201 for (retry = 5; retry; retry--) {
1202 ret = acpi_battery_update(battery, false);
1203 if (!ret)
1204 break;
1205
1206 msleep(20);
1207 }
1208 return ret;
1209}
1210
1211static int acpi_battery_add(struct acpi_device *device)
1212{
1213 int result = 0;
1214 struct acpi_battery *battery = NULL;
1215
1216 if (!device)
1217 return -EINVAL;
1218
1219 if (device->dep_unmet)
1220 return -EPROBE_DEFER;
1221
1222 battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
1223 if (!battery)
1224 return -ENOMEM;
1225 battery->device = device;
1226 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
1227 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
1228 device->driver_data = battery;
1229 mutex_init(&battery->lock);
1230 mutex_init(&battery->sysfs_lock);
1231 if (acpi_has_method(battery->device->handle, "_BIX"))
1232 set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
1233
1234 result = acpi_battery_update_retry(battery);
1235 if (result)
1236 goto fail;
1237
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001238 pr_info(PREFIX "%s Slot [%s] (battery %s)\n",
1239 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
1240 device->status.battery_present ? "present" : "absent");
1241
1242 battery->pm_nb.notifier_call = battery_notify;
1243 register_pm_notifier(&battery->pm_nb);
1244
1245 device_init_wakeup(&device->dev, 1);
1246
1247 return result;
1248
1249fail:
1250 sysfs_remove_battery(battery);
1251 mutex_destroy(&battery->lock);
1252 mutex_destroy(&battery->sysfs_lock);
1253 kfree(battery);
1254 return result;
1255}
1256
1257static int acpi_battery_remove(struct acpi_device *device)
1258{
1259 struct acpi_battery *battery = NULL;
1260
1261 if (!device || !acpi_driver_data(device))
1262 return -EINVAL;
1263 device_init_wakeup(&device->dev, 0);
1264 battery = acpi_driver_data(device);
1265 unregister_pm_notifier(&battery->pm_nb);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001266 sysfs_remove_battery(battery);
1267 mutex_destroy(&battery->lock);
1268 mutex_destroy(&battery->sysfs_lock);
1269 kfree(battery);
1270 return 0;
1271}
1272
1273#ifdef CONFIG_PM_SLEEP
1274/* this is needed to learn about changes made in suspended state */
1275static int acpi_battery_resume(struct device *dev)
1276{
1277 struct acpi_battery *battery;
1278
1279 if (!dev)
1280 return -EINVAL;
1281
1282 battery = acpi_driver_data(to_acpi_device(dev));
1283 if (!battery)
1284 return -EINVAL;
1285
1286 battery->update_time = 0;
1287 acpi_battery_update(battery, true);
1288 return 0;
1289}
1290#else
1291#define acpi_battery_resume NULL
1292#endif
1293
1294static SIMPLE_DEV_PM_OPS(acpi_battery_pm, NULL, acpi_battery_resume);
1295
1296static struct acpi_driver acpi_battery_driver = {
1297 .name = "battery",
1298 .class = ACPI_BATTERY_CLASS,
1299 .ids = battery_device_ids,
1300 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1301 .ops = {
1302 .add = acpi_battery_add,
1303 .remove = acpi_battery_remove,
1304 .notify = acpi_battery_notify,
1305 },
1306 .drv.pm = &acpi_battery_pm,
1307};
1308
1309static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
1310{
1311 unsigned int i;
1312 int result;
1313
1314 dmi_check_system(bat_dmi_table);
1315
1316 if (battery_check_pmic) {
1317 for (i = 0; i < ARRAY_SIZE(acpi_battery_blacklist); i++)
1318 if (acpi_dev_present(acpi_battery_blacklist[i], "1", -1)) {
1319 pr_info(PREFIX ACPI_BATTERY_DEVICE_NAME
1320 ": found native %s PMIC, not loading\n",
1321 acpi_battery_blacklist[i]);
1322 return;
1323 }
1324 }
1325
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001326 result = acpi_bus_register_driver(&acpi_battery_driver);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001327 battery_driver_registered = (result == 0);
1328}
1329
1330static int __init acpi_battery_init(void)
1331{
1332 if (acpi_disabled)
1333 return -ENODEV;
1334
1335 async_cookie = async_schedule(acpi_battery_init_async, NULL);
1336 return 0;
1337}
1338
1339static void __exit acpi_battery_exit(void)
1340{
1341 async_synchronize_cookie(async_cookie + 1);
1342 if (battery_driver_registered) {
1343 acpi_bus_unregister_driver(&acpi_battery_driver);
1344 battery_hook_exit();
1345 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001346}
1347
1348module_init(acpi_battery_init);
1349module_exit(acpi_battery_exit);