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