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