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