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