battery.c 37.8 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
L
Linus Torvalds 已提交
2
/*
3
 *  battery.c - ACPI Battery Driver (Revision: 2.0)
L
Linus Torvalds 已提交
4
 *
5 6
 *  Copyright (C) 2007 Alexey Starikovskiy <astarikovskiy@suse.de>
 *  Copyright (C) 2004-2007 Vladimir Lebedev <vladimir.p.lebedev@intel.com>
L
Linus Torvalds 已提交
7 8 9 10
 *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
 *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
 */

11 12
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

13 14 15 16
#include <linux/async.h>
#include <linux/delay.h>
#include <linux/dmi.h>
#include <linux/jiffies.h>
L
Linus Torvalds 已提交
17
#include <linux/kernel.h>
18
#include <linux/list.h>
L
Linus Torvalds 已提交
19
#include <linux/module.h>
20
#include <linux/mutex.h>
21
#include <linux/slab.h>
22
#include <linux/suspend.h>
23 24
#include <linux/types.h>

25
#include <asm/unaligned.h>
26

27
#include <linux/acpi.h>
28 29
#include <linux/power_supply.h>

30
#include <acpi/battery.h>
31

32 33
#define PREFIX "ACPI: "

L
Linus Torvalds 已提交
34
#define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
35 36
#define ACPI_BATTERY_CAPACITY_VALID(capacity) \
	((capacity) != 0 && (capacity) != ACPI_BATTERY_VALUE_UNKNOWN)
L
Linus Torvalds 已提交
37 38 39

#define ACPI_BATTERY_DEVICE_NAME	"Battery"

40 41 42
/* Battery power unit: 0 means mW, 1 means mA */
#define ACPI_BATTERY_POWER_UNIT_MA	1

43 44 45 46
#define ACPI_BATTERY_STATE_DISCHARGING	0x1
#define ACPI_BATTERY_STATE_CHARGING	0x2
#define ACPI_BATTERY_STATE_CRITICAL	0x4

L
Linus Torvalds 已提交
47
#define _COMPONENT		ACPI_BATTERY_COMPONENT
48

49
ACPI_MODULE_NAME("battery");
L
Linus Torvalds 已提交
50

51
MODULE_AUTHOR("Paul Diefenbaugh");
52
MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
53
MODULE_DESCRIPTION("ACPI Battery Driver");
L
Linus Torvalds 已提交
54 55
MODULE_LICENSE("GPL");

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 63 64
static unsigned int cache_time = 1000;
module_param(cache_time, uint, 0644);
MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
65

66 67 68 69 70
static const struct acpi_device_id battery_device_ids[] = {
	{"PNP0C0A", 0},
	{"", 0},
};

71
MODULE_DEVICE_TABLE(acpi, battery_device_ids);
L
Linus Torvalds 已提交
72

73 74 75 76 77
/* Lists of PMIC ACPI HIDs with an (often better) native battery driver */
static const char * const acpi_battery_blacklist[] = {
	"INT33F4", /* X-Powers AXP288 PMIC */
};

78 79
enum {
	ACPI_BATTERY_ALARM_PRESENT,
80
	ACPI_BATTERY_XINFO_PRESENT,
81
	ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY,
82 83 84 85 86 87 88 89 90 91 92 93
	/* On Lenovo Thinkpad models from 2010 and 2011, the power unit
	   switches between mWh and mAh depending on whether the system
	   is running on battery or not.  When mAh is the unit, most
	   reported values are incorrect and need to be adjusted by
	   10000/design_voltage.  Verified on x201, t410, t410s, and x220.
	   Pre-2010 and 2012 models appear to always report in mWh and
	   are thus unaffected (tested with t42, t61, t500, x200, x300,
	   and x230).  Also, in mid-2012 Lenovo issued a BIOS update for
	   the 2011 models that fixes the issue (tested on x220 with a
	   post-1.29 BIOS), but as of Nov. 2012, no such update is
	   available for the 2010 models.  */
	ACPI_BATTERY_QUIRK_THINKPAD_MAH,
94 95 96 97
	/* for batteries reporting current capacity with design capacity
	 * on a full charge, but showing degradation in full charge cap.
	 */
	ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE,
98
};
99

L
Linus Torvalds 已提交
100
struct acpi_battery {
101
	struct mutex lock;
102
	struct mutex sysfs_lock;
103 104
	struct power_supply *bat;
	struct power_supply_desc bat_desc;
105
	struct acpi_device *device;
106
	struct notifier_block pm_nb;
107
	struct list_head list;
108
	unsigned long update_time;
109
	int revision;
110
	int rate_now;
111 112
	int capacity_now;
	int voltage_now;
113
	int design_capacity;
114
	int full_charge_capacity;
115 116 117 118
	int technology;
	int design_voltage;
	int design_capacity_warning;
	int design_capacity_low;
119 120 121 122 123 124
	int cycle_count;
	int measurement_accuracy;
	int max_sampling_time;
	int min_sampling_time;
	int max_averaging_interval;
	int min_averaging_interval;
125 126
	int capacity_granularity_1;
	int capacity_granularity_2;
127
	int alarm;
128 129 130 131
	char model_number[32];
	char serial_number[32];
	char type[32];
	char oem_info[32];
132 133
	int state;
	int power_unit;
134
	unsigned long flags;
L
Linus Torvalds 已提交
135 136
};

137
#define to_acpi_battery(x) power_supply_get_drvdata(x)
138

139
static inline int acpi_battery_present(struct acpi_battery *battery)
140
{
141 142
	return battery->device->status.battery_present;
}
143

144 145 146 147 148 149 150 151
static int acpi_battery_technology(struct acpi_battery *battery)
{
	if (!strcasecmp("NiCd", battery->type))
		return POWER_SUPPLY_TECHNOLOGY_NiCd;
	if (!strcasecmp("NiMH", battery->type))
		return POWER_SUPPLY_TECHNOLOGY_NiMH;
	if (!strcasecmp("LION", battery->type))
		return POWER_SUPPLY_TECHNOLOGY_LION;
152
	if (!strncasecmp("LI-ION", battery->type, 6))
153
		return POWER_SUPPLY_TECHNOLOGY_LION;
154 155 156 157 158
	if (!strcasecmp("LiP", battery->type))
		return POWER_SUPPLY_TECHNOLOGY_LIPO;
	return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
}

159
static int acpi_battery_get_state(struct acpi_battery *battery);
160

161 162
static int acpi_battery_is_charged(struct acpi_battery *battery)
{
163
	/* charging, discharging or critical low */
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
	if (battery->state != 0)
		return 0;

	/* battery not reporting charge */
	if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
	    battery->capacity_now == 0)
		return 0;

	/* good batteries update full_charge as the batteries degrade */
	if (battery->full_charge_capacity == battery->capacity_now)
		return 1;

	/* fallback to using design values for broken batteries */
	if (battery->design_capacity == battery->capacity_now)
		return 1;

	/* we don't do any sort of metric based on percentages */
	return 0;
}

184 185
static bool acpi_battery_is_degraded(struct acpi_battery *battery)
{
186 187
	return ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity) &&
		ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity) &&
188 189 190
		battery->full_charge_capacity < battery->design_capacity;
}

191 192 193 194 195 196 197
static int acpi_battery_handle_discharging(struct acpi_battery *battery)
{
	/*
	 * Some devices wrongly report discharging if the battery's charge level
	 * was above the device's start charging threshold atm the AC adapter
	 * 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()) &&
	    battery->rate_now == 0)
200 201 202 203 204
		return POWER_SUPPLY_STATUS_NOT_CHARGING;

	return POWER_SUPPLY_STATUS_DISCHARGING;
}

205 206 207 208
static int acpi_battery_get_property(struct power_supply *psy,
				     enum power_supply_property psp,
				     union power_supply_propval *val)
{
209
	int full_capacity = ACPI_BATTERY_VALUE_UNKNOWN, ret = 0;
210 211
	struct acpi_battery *battery = to_acpi_battery(psy);

212 213 214 215
	if (acpi_battery_present(battery)) {
		/* run battery update only if it is present */
		acpi_battery_get_state(battery);
	} else if (psp != POWER_SUPPLY_PROP_PRESENT)
216 217 218
		return -ENODEV;
	switch (psp) {
	case POWER_SUPPLY_PROP_STATUS:
219
		if (battery->state & ACPI_BATTERY_STATE_DISCHARGING)
220
			val->intval = acpi_battery_handle_discharging(battery);
221
		else if (battery->state & ACPI_BATTERY_STATE_CHARGING)
222
			val->intval = POWER_SUPPLY_STATUS_CHARGING;
223
		else if (acpi_battery_is_charged(battery))
224
			val->intval = POWER_SUPPLY_STATUS_FULL;
225 226
		else
			val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
227 228 229 230 231 232 233
		break;
	case POWER_SUPPLY_PROP_PRESENT:
		val->intval = acpi_battery_present(battery);
		break;
	case POWER_SUPPLY_PROP_TECHNOLOGY:
		val->intval = acpi_battery_technology(battery);
		break;
234 235 236
	case POWER_SUPPLY_PROP_CYCLE_COUNT:
		val->intval = battery->cycle_count;
		break;
237
	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
238 239 240 241
		if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
			ret = -ENODEV;
		else
			val->intval = battery->design_voltage * 1000;
242 243
		break;
	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
244 245 246 247
		if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
			ret = -ENODEV;
		else
			val->intval = battery->voltage_now * 1000;
248 249
		break;
	case POWER_SUPPLY_PROP_CURRENT_NOW:
250
	case POWER_SUPPLY_PROP_POWER_NOW:
251 252 253 254
		if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
			ret = -ENODEV;
		else
			val->intval = battery->rate_now * 1000;
255 256 257
		break;
	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
	case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
258
		if (!ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity))
259 260 261
			ret = -ENODEV;
		else
			val->intval = battery->design_capacity * 1000;
262 263 264
		break;
	case POWER_SUPPLY_PROP_CHARGE_FULL:
	case POWER_SUPPLY_PROP_ENERGY_FULL:
265
		if (!ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity))
266 267 268
			ret = -ENODEV;
		else
			val->intval = battery->full_charge_capacity * 1000;
269 270 271
		break;
	case POWER_SUPPLY_PROP_CHARGE_NOW:
	case POWER_SUPPLY_PROP_ENERGY_NOW:
272 273 274 275
		if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
			ret = -ENODEV;
		else
			val->intval = battery->capacity_now * 1000;
276
		break;
277
	case POWER_SUPPLY_PROP_CAPACITY:
278 279 280 281 282
		if (ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity))
			full_capacity = battery->full_charge_capacity;
		else if (ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity))
			full_capacity = battery->design_capacity;

283
		if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
284
		    full_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
285 286
			ret = -ENODEV;
		else
287
			val->intval = battery->capacity_now * 100/
288
					full_capacity;
289
		break;
290 291 292 293 294 295 296 297 298 299 300
	case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
		if (battery->state & ACPI_BATTERY_STATE_CRITICAL)
			val->intval = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
		else if (test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags) &&
			(battery->capacity_now <= battery->alarm))
			val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
		else if (acpi_battery_is_charged(battery))
			val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
		else
			val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
		break;
301 302 303 304 305 306
	case POWER_SUPPLY_PROP_MODEL_NAME:
		val->strval = battery->model_number;
		break;
	case POWER_SUPPLY_PROP_MANUFACTURER:
		val->strval = battery->oem_info;
		break;
307 308 309
	case POWER_SUPPLY_PROP_SERIAL_NUMBER:
		val->strval = battery->serial_number;
		break;
310
	default:
311
		ret = -EINVAL;
312
	}
313
	return ret;
314 315 316 317 318 319
}

static enum power_supply_property charge_battery_props[] = {
	POWER_SUPPLY_PROP_STATUS,
	POWER_SUPPLY_PROP_PRESENT,
	POWER_SUPPLY_PROP_TECHNOLOGY,
320
	POWER_SUPPLY_PROP_CYCLE_COUNT,
321 322 323 324 325 326
	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
	POWER_SUPPLY_PROP_VOLTAGE_NOW,
	POWER_SUPPLY_PROP_CURRENT_NOW,
	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
	POWER_SUPPLY_PROP_CHARGE_FULL,
	POWER_SUPPLY_PROP_CHARGE_NOW,
327
	POWER_SUPPLY_PROP_CAPACITY,
328
	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
329 330
	POWER_SUPPLY_PROP_MODEL_NAME,
	POWER_SUPPLY_PROP_MANUFACTURER,
331
	POWER_SUPPLY_PROP_SERIAL_NUMBER,
332 333
};

334 335 336 337 338 339 340 341 342 343 344 345 346 347
static enum power_supply_property charge_battery_full_cap_broken_props[] = {
	POWER_SUPPLY_PROP_STATUS,
	POWER_SUPPLY_PROP_PRESENT,
	POWER_SUPPLY_PROP_TECHNOLOGY,
	POWER_SUPPLY_PROP_CYCLE_COUNT,
	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
	POWER_SUPPLY_PROP_VOLTAGE_NOW,
	POWER_SUPPLY_PROP_CURRENT_NOW,
	POWER_SUPPLY_PROP_CHARGE_NOW,
	POWER_SUPPLY_PROP_MODEL_NAME,
	POWER_SUPPLY_PROP_MANUFACTURER,
	POWER_SUPPLY_PROP_SERIAL_NUMBER,
};

348 349 350 351
static enum power_supply_property energy_battery_props[] = {
	POWER_SUPPLY_PROP_STATUS,
	POWER_SUPPLY_PROP_PRESENT,
	POWER_SUPPLY_PROP_TECHNOLOGY,
352
	POWER_SUPPLY_PROP_CYCLE_COUNT,
353 354
	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
	POWER_SUPPLY_PROP_VOLTAGE_NOW,
355
	POWER_SUPPLY_PROP_POWER_NOW,
356 357 358
	POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
	POWER_SUPPLY_PROP_ENERGY_FULL,
	POWER_SUPPLY_PROP_ENERGY_NOW,
359
	POWER_SUPPLY_PROP_CAPACITY,
360
	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
361 362
	POWER_SUPPLY_PROP_MODEL_NAME,
	POWER_SUPPLY_PROP_MANUFACTURER,
363
	POWER_SUPPLY_PROP_SERIAL_NUMBER,
364 365
};

366 367 368 369 370 371 372 373 374 375 376 377 378 379
static enum power_supply_property energy_battery_full_cap_broken_props[] = {
	POWER_SUPPLY_PROP_STATUS,
	POWER_SUPPLY_PROP_PRESENT,
	POWER_SUPPLY_PROP_TECHNOLOGY,
	POWER_SUPPLY_PROP_CYCLE_COUNT,
	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
	POWER_SUPPLY_PROP_VOLTAGE_NOW,
	POWER_SUPPLY_PROP_POWER_NOW,
	POWER_SUPPLY_PROP_ENERGY_NOW,
	POWER_SUPPLY_PROP_MODEL_NAME,
	POWER_SUPPLY_PROP_MANUFACTURER,
	POWER_SUPPLY_PROP_SERIAL_NUMBER,
};

380 381 382
/* --------------------------------------------------------------------------
                               Battery Management
   -------------------------------------------------------------------------- */
383 384 385 386
struct acpi_offsets {
	size_t offset;		/* offset inside struct acpi_sbs_battery */
	u8 mode;		/* int or string? */
};
387

388
static const struct acpi_offsets state_offsets[] = {
389
	{offsetof(struct acpi_battery, state), 0},
390
	{offsetof(struct acpi_battery, rate_now), 0},
391 392
	{offsetof(struct acpi_battery, capacity_now), 0},
	{offsetof(struct acpi_battery, voltage_now), 0},
393
};
394

395
static const struct acpi_offsets info_offsets[] = {
396 397
	{offsetof(struct acpi_battery, power_unit), 0},
	{offsetof(struct acpi_battery, design_capacity), 0},
398
	{offsetof(struct acpi_battery, full_charge_capacity), 0},
399 400 401 402 403 404 405 406 407 408 409
	{offsetof(struct acpi_battery, technology), 0},
	{offsetof(struct acpi_battery, design_voltage), 0},
	{offsetof(struct acpi_battery, design_capacity_warning), 0},
	{offsetof(struct acpi_battery, design_capacity_low), 0},
	{offsetof(struct acpi_battery, capacity_granularity_1), 0},
	{offsetof(struct acpi_battery, capacity_granularity_2), 0},
	{offsetof(struct acpi_battery, model_number), 1},
	{offsetof(struct acpi_battery, serial_number), 1},
	{offsetof(struct acpi_battery, type), 1},
	{offsetof(struct acpi_battery, oem_info), 1},
};
410

411
static const struct acpi_offsets extended_info_offsets[] = {
412
	{offsetof(struct acpi_battery, revision), 0},
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
	{offsetof(struct acpi_battery, power_unit), 0},
	{offsetof(struct acpi_battery, design_capacity), 0},
	{offsetof(struct acpi_battery, full_charge_capacity), 0},
	{offsetof(struct acpi_battery, technology), 0},
	{offsetof(struct acpi_battery, design_voltage), 0},
	{offsetof(struct acpi_battery, design_capacity_warning), 0},
	{offsetof(struct acpi_battery, design_capacity_low), 0},
	{offsetof(struct acpi_battery, cycle_count), 0},
	{offsetof(struct acpi_battery, measurement_accuracy), 0},
	{offsetof(struct acpi_battery, max_sampling_time), 0},
	{offsetof(struct acpi_battery, min_sampling_time), 0},
	{offsetof(struct acpi_battery, max_averaging_interval), 0},
	{offsetof(struct acpi_battery, min_averaging_interval), 0},
	{offsetof(struct acpi_battery, capacity_granularity_1), 0},
	{offsetof(struct acpi_battery, capacity_granularity_2), 0},
	{offsetof(struct acpi_battery, model_number), 1},
	{offsetof(struct acpi_battery, serial_number), 1},
	{offsetof(struct acpi_battery, type), 1},
	{offsetof(struct acpi_battery, oem_info), 1},
};

434 435
static int extract_package(struct acpi_battery *battery,
			   union acpi_object *package,
436
			   const struct acpi_offsets *offsets, int num)
437
{
438
	int i;
439 440 441 442 443 444 445 446
	union acpi_object *element;
	if (package->type != ACPI_TYPE_PACKAGE)
		return -EFAULT;
	for (i = 0; i < num; ++i) {
		if (package->package.count <= i)
			return -EFAULT;
		element = &package->package.elements[i];
		if (offsets[i].mode) {
447 448 449 450 451 452
			u8 *ptr = (u8 *)battery + offsets[i].offset;
			if (element->type == ACPI_TYPE_STRING ||
			    element->type == ACPI_TYPE_BUFFER)
				strncpy(ptr, element->string.pointer, 32);
			else if (element->type == ACPI_TYPE_INTEGER) {
				strncpy(ptr, (u8 *)&element->integer.value,
L
Lin Ming 已提交
453 454
					sizeof(u64));
				ptr[sizeof(u64)] = 0;
455 456
			} else
				*ptr = 0; /* don't have value */
457
		} else {
458 459 460
			int *x = (int *)((u8 *)battery + offsets[i].offset);
			*x = (element->type == ACPI_TYPE_INTEGER) ?
				element->integer.value : -1;
461
		}
462 463 464 465 466 467
	}
	return 0;
}

static int acpi_battery_get_status(struct acpi_battery *battery)
{
468
	if (acpi_bus_get_status(battery->device)) {
469 470 471
		ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
		return -ENODEV;
	}
472
	return 0;
473 474
}

475 476 477 478

static int extract_battery_info(const int use_bix,
			 struct acpi_battery *battery,
			 const struct acpi_buffer *buffer)
L
Linus Torvalds 已提交
479
{
480
	int result = -EFAULT;
L
Linus Torvalds 已提交
481

482 483
	if (use_bix && battery_bix_broken_package)
		result = extract_package(battery, buffer->pointer,
484 485
				extended_info_offsets + 1,
				ARRAY_SIZE(extended_info_offsets) - 1);
486 487
	else if (use_bix)
		result = extract_package(battery, buffer->pointer,
488 489 490
				extended_info_offsets,
				ARRAY_SIZE(extended_info_offsets));
	else
491
		result = extract_package(battery, buffer->pointer,
492
				info_offsets, ARRAY_SIZE(info_offsets));
493 494
	if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
		battery->full_charge_capacity = battery->design_capacity;
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
	if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) &&
	    battery->power_unit && battery->design_voltage) {
		battery->design_capacity = battery->design_capacity *
		    10000 / battery->design_voltage;
		battery->full_charge_capacity = battery->full_charge_capacity *
		    10000 / battery->design_voltage;
		battery->design_capacity_warning =
		    battery->design_capacity_warning *
		    10000 / battery->design_voltage;
		/* Curiously, design_capacity_low, unlike the rest of them,
		   is correct.  */
		/* capacity_granularity_* equal 1 on the systems tested, so
		   it's impossible to tell if they would need an adjustment
		   or not if their values were higher.  */
	}
510 511 512 513
	if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags) &&
	    battery->capacity_now > battery->full_charge_capacity)
		battery->capacity_now = battery->full_charge_capacity;

514
	return result;
L
Linus Torvalds 已提交
515 516
}

517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
static int acpi_battery_get_info(struct acpi_battery *battery)
{
	const int xinfo = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
	int use_bix;
	int result = -ENODEV;

	if (!acpi_battery_present(battery))
		return 0;


	for (use_bix = xinfo ? 1 : 0; use_bix >= 0; use_bix--) {
		struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
		acpi_status status = AE_ERROR;

		mutex_lock(&battery->lock);
		status = acpi_evaluate_object(battery->device->handle,
					      use_bix ? "_BIX":"_BIF",
					      NULL, &buffer);
		mutex_unlock(&battery->lock);

		if (ACPI_FAILURE(status)) {
			ACPI_EXCEPTION((AE_INFO, status, "Evaluating %s",
					use_bix ? "_BIX":"_BIF"));
		} else {
			result = extract_battery_info(use_bix,
						      battery,
						      &buffer);

			kfree(buffer.pointer);
			break;
		}
	}

	if (!result && !use_bix && xinfo)
		pr_warn(FW_BUG "The _BIX method is broken, using _BIF.\n");

	return result;
}

556
static int acpi_battery_get_state(struct acpi_battery *battery)
L
Linus Torvalds 已提交
557
{
L
Len Brown 已提交
558 559 560
	int result = 0;
	acpi_status status = 0;
	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
L
Linus Torvalds 已提交
561

562 563
	if (!acpi_battery_present(battery))
		return 0;
L
Linus Torvalds 已提交
564

565 566 567 568 569
	if (battery->update_time &&
	    time_before(jiffies, battery->update_time +
			msecs_to_jiffies(cache_time)))
		return 0;

570
	mutex_lock(&battery->lock);
571
	status = acpi_evaluate_object(battery->device->handle, "_BST",
572 573
				      NULL, &buffer);
	mutex_unlock(&battery->lock);
574

L
Linus Torvalds 已提交
575
	if (ACPI_FAILURE(status)) {
576
		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
577
		return -ENODEV;
L
Linus Torvalds 已提交
578
	}
579

580 581
	result = extract_package(battery, buffer.pointer,
				 state_offsets, ARRAY_SIZE(state_offsets));
582
	battery->update_time = jiffies;
583
	kfree(buffer.pointer);
584

585 586 587 588 589 590 591
	/* For buggy DSDTs that report negative 16-bit values for either
	 * charging or discharging current and/or report 0 as 65536
	 * due to bad math.
	 */
	if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA &&
		battery->rate_now != ACPI_BATTERY_VALUE_UNKNOWN &&
		(s16)(battery->rate_now) < 0) {
592
		battery->rate_now = abs((s16)battery->rate_now);
593
		pr_warn_once(FW_BUG "battery: (dis)charge rate invalid.\n");
594
	}
595

596 597 598 599
	if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)
	    && battery->capacity_now >= 0 && battery->capacity_now <= 100)
		battery->capacity_now = (battery->capacity_now *
				battery->full_charge_capacity) / 100;
600 601 602 603 604
	if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) &&
	    battery->power_unit && battery->design_voltage) {
		battery->capacity_now = battery->capacity_now *
		    10000 / battery->design_voltage;
	}
605 606 607 608
	if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags) &&
	    battery->capacity_now > battery->full_charge_capacity)
		battery->capacity_now = battery->full_charge_capacity;

609 610
	return result;
}
L
Linus Torvalds 已提交
611

612
static int acpi_battery_set_alarm(struct acpi_battery *battery)
L
Linus Torvalds 已提交
613
{
L
Len Brown 已提交
614
	acpi_status status = 0;
L
Linus Torvalds 已提交
615

616
	if (!acpi_battery_present(battery) ||
617
	    !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags))
618
		return -ENODEV;
L
Linus Torvalds 已提交
619

620
	mutex_lock(&battery->lock);
621 622
	status = acpi_execute_simple_method(battery->device->handle, "_BTP",
					    battery->alarm);
623
	mutex_unlock(&battery->lock);
624

L
Linus Torvalds 已提交
625
	if (ACPI_FAILURE(status))
626
		return -ENODEV;
L
Linus Torvalds 已提交
627

628
	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm));
629
	return 0;
L
Linus Torvalds 已提交
630 631
}

632
static int acpi_battery_init_alarm(struct acpi_battery *battery)
L
Linus Torvalds 已提交
633
{
634
	/* See if alarms are supported, and if so, set default */
635
	if (!acpi_has_method(battery->device->handle, "_BTP")) {
636
		clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
637
		return 0;
638
	}
639
	set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
640 641
	if (!battery->alarm)
		battery->alarm = battery->design_capacity_warning;
642
	return acpi_battery_set_alarm(battery);
643
}
L
Linus Torvalds 已提交
644

645 646 647 648 649 650 651 652 653 654 655 656 657 658
static ssize_t acpi_battery_alarm_show(struct device *dev,
					struct device_attribute *attr,
					char *buf)
{
	struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
	return sprintf(buf, "%d\n", battery->alarm * 1000);
}

static ssize_t acpi_battery_alarm_store(struct device *dev,
					struct device_attribute *attr,
					const char *buf, size_t count)
{
	unsigned long x;
	struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
659
	if (sscanf(buf, "%lu\n", &x) == 1)
660 661 662 663 664 665
		battery->alarm = x/1000;
	if (acpi_battery_present(battery))
		acpi_battery_set_alarm(battery);
	return count;
}

666
static const struct device_attribute alarm_attr = {
667
	.attr = {.name = "alarm", .mode = 0644},
668 669 670 671
	.show = acpi_battery_alarm_show,
	.store = acpi_battery_alarm_store,
};

672 673 674 675 676 677 678 679 680 681 682 683 684
/*
 * The Battery Hooking API
 *
 * This API is used inside other drivers that need to expose
 * platform-specific behaviour within the generic driver in a
 * generic way.
 *
 */

static LIST_HEAD(acpi_battery_list);
static LIST_HEAD(battery_hook_list);
static DEFINE_MUTEX(hook_mutex);

685
static void __battery_hook_unregister(struct acpi_battery_hook *hook, int lock)
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
{
	struct acpi_battery *battery;
	/*
	 * In order to remove a hook, we first need to
	 * de-register all the batteries that are registered.
	 */
	if (lock)
		mutex_lock(&hook_mutex);
	list_for_each_entry(battery, &acpi_battery_list, list) {
		hook->remove_battery(battery->bat);
	}
	list_del(&hook->list);
	if (lock)
		mutex_unlock(&hook_mutex);
	pr_info("extension unregistered: %s\n", hook->name);
}

void battery_hook_unregister(struct acpi_battery_hook *hook)
{
	__battery_hook_unregister(hook, 1);
}
EXPORT_SYMBOL_GPL(battery_hook_unregister);

void battery_hook_register(struct acpi_battery_hook *hook)
{
	struct acpi_battery *battery;

	mutex_lock(&hook_mutex);
	INIT_LIST_HEAD(&hook->list);
	list_add(&hook->list, &battery_hook_list);
	/*
	 * Now that the driver is registered, we need
	 * to notify the hook that a battery is available
	 * for each battery, so that the driver may add
	 * its attributes.
	 */
	list_for_each_entry(battery, &acpi_battery_list, list) {
		if (hook->add_battery(battery->bat)) {
			/*
			 * If a add-battery returns non-zero,
			 * the registration of the extension has failed,
			 * and we will not add it to the list of loaded
			 * hooks.
			 */
			pr_err("extension failed to load: %s", hook->name);
			__battery_hook_unregister(hook, 0);
732
			goto end;
733 734 735
		}
	}
	pr_info("new extension: %s\n", hook->name);
736
end:
737 738 739 740 741 742 743 744
	mutex_unlock(&hook_mutex);
}
EXPORT_SYMBOL_GPL(battery_hook_register);

/*
 * This function gets called right after the battery sysfs
 * attributes have been added, so that the drivers that
 * define custom sysfs attributes can add their own.
745
*/
746 747
static void battery_hook_add_battery(struct acpi_battery *battery)
{
748
	struct acpi_battery_hook *hook_node, *tmp;
749 750 751 752 753 754 755 756 757 758 759

	mutex_lock(&hook_mutex);
	INIT_LIST_HEAD(&battery->list);
	list_add(&battery->list, &acpi_battery_list);
	/*
	 * Since we added a new battery to the list, we need to
	 * iterate over the hooks and call add_battery for each
	 * hook that was registered. This usually happens
	 * when a battery gets hotplugged or initialized
	 * during the battery module initialization.
	 */
760
	list_for_each_entry_safe(hook_node, tmp, &battery_hook_list, list) {
761 762 763 764 765 766 767
		if (hook_node->add_battery(battery->bat)) {
			/*
			 * The notification of the extensions has failed, to
			 * prevent further errors we will unload the extension.
			 */
			pr_err("error in extension, unloading: %s",
					hook_node->name);
768
			__battery_hook_unregister(hook_node, 0);
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
		}
	}
	mutex_unlock(&hook_mutex);
}

static void battery_hook_remove_battery(struct acpi_battery *battery)
{
	struct acpi_battery_hook *hook;

	mutex_lock(&hook_mutex);
	/*
	 * Before removing the hook, we need to remove all
	 * custom attributes from the battery.
	 */
	list_for_each_entry(hook, &battery_hook_list, list) {
		hook->remove_battery(battery->bat);
	}
	/* Then, just remove the battery from the list */
	list_del(&battery->list);
	mutex_unlock(&hook_mutex);
}

static void __exit battery_hook_exit(void)
{
	struct acpi_battery_hook *hook;
	struct acpi_battery_hook *ptr;
	/*
	 * At this point, the acpi_bus_unregister_driver()
	 * has called remove for all batteries. We just
	 * need to remove the hooks.
	 */
	list_for_each_entry_safe(hook, ptr, &battery_hook_list, list) {
		__battery_hook_unregister(hook, 1);
	}
	mutex_destroy(&hook_mutex);
}

806 807
static int sysfs_add_battery(struct acpi_battery *battery)
{
808
	struct power_supply_config psy_cfg = { .drv_data = battery, };
809 810 811 812 813
	bool full_cap_broken = false;

	if (!ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity) &&
	    !ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity))
		full_cap_broken = true;
814

815
	if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) {
816 817 818 819 820 821 822 823 824 825
		if (full_cap_broken) {
			battery->bat_desc.properties =
			    charge_battery_full_cap_broken_props;
			battery->bat_desc.num_properties =
			    ARRAY_SIZE(charge_battery_full_cap_broken_props);
		} else {
			battery->bat_desc.properties = charge_battery_props;
			battery->bat_desc.num_properties =
			    ARRAY_SIZE(charge_battery_props);
		}
826
	} else {
827 828 829 830 831 832 833 834 835 836
		if (full_cap_broken) {
			battery->bat_desc.properties =
			    energy_battery_full_cap_broken_props;
			battery->bat_desc.num_properties =
			    ARRAY_SIZE(energy_battery_full_cap_broken_props);
		} else {
			battery->bat_desc.properties = energy_battery_props;
			battery->bat_desc.num_properties =
			    ARRAY_SIZE(energy_battery_props);
		}
837 838
	}

839 840 841
	battery->bat_desc.name = acpi_device_bid(battery->device);
	battery->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY;
	battery->bat_desc.get_property = acpi_battery_get_property;
842

843 844
	battery->bat = power_supply_register_no_ws(&battery->device->dev,
				&battery->bat_desc, &psy_cfg);
845

846 847 848 849
	if (IS_ERR(battery->bat)) {
		int result = PTR_ERR(battery->bat);

		battery->bat = NULL;
850
		return result;
851
	}
852
	battery_hook_add_battery(battery);
853
	return device_create_file(&battery->bat->dev, &alarm_attr);
854 855 856 857
}

static void sysfs_remove_battery(struct acpi_battery *battery)
{
858
	mutex_lock(&battery->sysfs_lock);
859
	if (!battery->bat) {
860
		mutex_unlock(&battery->sysfs_lock);
861
		return;
862
	}
863
	battery_hook_remove_battery(battery);
864 865 866
	device_remove_file(&battery->bat->dev, &alarm_attr);
	power_supply_unregister(battery->bat);
	battery->bat = NULL;
867
	mutex_unlock(&battery->sysfs_lock);
868 869
}

870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
static void find_battery(const struct dmi_header *dm, void *private)
{
	struct acpi_battery *battery = (struct acpi_battery *)private;
	/* Note: the hardcoded offsets below have been extracted from
	   the source code of dmidecode.  */
	if (dm->type == DMI_ENTRY_PORTABLE_BATTERY && dm->length >= 8) {
		const u8 *dmi_data = (const u8 *)(dm + 1);
		int dmi_capacity = get_unaligned((const u16 *)(dmi_data + 6));
		if (dm->length >= 18)
			dmi_capacity *= dmi_data[17];
		if (battery->design_capacity * battery->design_voltage / 1000
		    != dmi_capacity &&
		    battery->design_capacity * 10 == dmi_capacity)
			set_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH,
				&battery->flags);
	}
}

888 889 890 891 892 893 894 895 896 897 898 899
/*
 * According to the ACPI spec, some kinds of primary batteries can
 * report percentage battery remaining capacity directly to OS.
 * In this case, it reports the Last Full Charged Capacity == 100
 * and BatteryPresentRate == 0xFFFFFFFF.
 *
 * Now we found some battery reports percentage remaining capacity
 * even if it's rechargeable.
 * https://bugzilla.kernel.org/show_bug.cgi?id=15979
 *
 * Handle this correctly so that they won't break userspace.
 */
900
static void acpi_battery_quirks(struct acpi_battery *battery)
901 902
{
	if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
903
		return;
904

905 906 907
	if (battery->full_charge_capacity == 100 &&
		battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN &&
		battery->capacity_now >= 0 && battery->capacity_now <= 100) {
908 909 910 911 912
		set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags);
		battery->full_charge_capacity = battery->design_capacity;
		battery->capacity_now = (battery->capacity_now *
				battery->full_charge_capacity) / 100;
	}
913 914

	if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags))
915
		return;
916 917 918 919

	if (battery->power_unit && dmi_name_in_vendors("LENOVO")) {
		const char *s;
		s = dmi_get_system_info(DMI_PRODUCT_VERSION);
920
		if (s && !strncasecmp(s, "ThinkPad", 8)) {
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
			dmi_walk(find_battery, battery);
			if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH,
				     &battery->flags) &&
			    battery->design_voltage) {
				battery->design_capacity =
				    battery->design_capacity *
				    10000 / battery->design_voltage;
				battery->full_charge_capacity =
				    battery->full_charge_capacity *
				    10000 / battery->design_voltage;
				battery->design_capacity_warning =
				    battery->design_capacity_warning *
				    10000 / battery->design_voltage;
				battery->capacity_now = battery->capacity_now *
				    10000 / battery->design_voltage;
			}
		}
	}
939 940 941 942 943 944 945 946 947

	if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags))
		return;

	if (acpi_battery_is_degraded(battery) &&
	    battery->capacity_now > battery->full_charge_capacity) {
		set_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags);
		battery->capacity_now = battery->full_charge_capacity;
	}
948 949
}

950
static int acpi_battery_update(struct acpi_battery *battery, bool resume)
951
{
952 953
	int result = acpi_battery_get_status(battery);

954
	if (result)
955
		return result;
956

957 958
	if (!acpi_battery_present(battery)) {
		sysfs_remove_battery(battery);
959
		battery->update_time = 0;
960
		return 0;
961
	}
962 963 964 965

	if (resume)
		return 0;

966
	if (!battery->update_time) {
967 968 969 970 971
		result = acpi_battery_get_info(battery);
		if (result)
			return result;
		acpi_battery_init_alarm(battery);
	}
972 973 974 975 976 977

	result = acpi_battery_get_state(battery);
	if (result)
		return result;
	acpi_battery_quirks(battery);

978
	if (!battery->bat) {
979 980 981 982
		result = sysfs_add_battery(battery);
		if (result)
			return result;
	}
983 984 985 986 987 988 989

	/*
	 * Wakeup the system if battery is critical low
	 * or lower than the alarm level
	 */
	if ((battery->state & ACPI_BATTERY_STATE_CRITICAL) ||
	    (test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags) &&
990
	     (battery->capacity_now <= battery->alarm)))
991
		acpi_pm_wakeup_event(&battery->device->dev);
992

993
	return result;
994 995
}

996 997
static void acpi_battery_refresh(struct acpi_battery *battery)
{
998 999
	int power_unit;

1000
	if (!battery->bat)
1001 1002
		return;

1003 1004
	power_unit = battery->power_unit;

1005
	acpi_battery_get_info(battery);
1006 1007 1008 1009 1010

	if (power_unit == battery->power_unit)
		return;

	/* The battery has changed its reporting units. */
1011 1012 1013 1014
	sysfs_remove_battery(battery);
	sysfs_add_battery(battery);
}

L
Linus Torvalds 已提交
1015 1016 1017 1018
/* --------------------------------------------------------------------------
                                 Driver Interface
   -------------------------------------------------------------------------- */

1019
static void acpi_battery_notify(struct acpi_device *device, u32 event)
L
Linus Torvalds 已提交
1020
{
1021
	struct acpi_battery *battery = acpi_driver_data(device);
1022
	struct power_supply *old;
1023

L
Linus Torvalds 已提交
1024
	if (!battery)
1025
		return;
1026
	old = battery->bat;
1027 1028 1029 1030 1031 1032 1033 1034
	/*
	* On Acer Aspire V5-573G notifications are sometimes triggered too
	* early. For example, when AC is unplugged and notification is
	* triggered, battery state is still reported as "Full", and changes to
	* "Discharging" only after short delay, without any notification.
	*/
	if (battery_notification_delay_ms > 0)
		msleep(battery_notification_delay_ms);
1035 1036
	if (event == ACPI_BATTERY_NOTIFY_INFO)
		acpi_battery_refresh(battery);
1037
	acpi_battery_update(battery, false);
1038
	acpi_bus_generate_netlink_event(device->pnp.device_class,
1039
					dev_name(&device->dev), event,
V
Vladimir Lebedev 已提交
1040
					acpi_battery_present(battery));
1041
	acpi_notifier_call_chain(device, event, acpi_battery_present(battery));
J
Justin P. Mattock 已提交
1042
	/* acpi_battery_update could remove power_supply object */
1043 1044
	if (old && battery->bat)
		power_supply_changed(battery->bat);
L
Linus Torvalds 已提交
1045 1046
}

1047 1048 1049 1050 1051
static int battery_notify(struct notifier_block *nb,
			       unsigned long mode, void *_unused)
{
	struct acpi_battery *battery = container_of(nb, struct acpi_battery,
						    pm_nb);
1052 1053
	int result;

1054
	switch (mode) {
1055
	case PM_POST_HIBERNATION:
1056
	case PM_POST_SUSPEND:
1057 1058 1059
		if (!acpi_battery_present(battery))
			return 0;

1060 1061 1062
		if (battery->bat) {
			acpi_battery_refresh(battery);
		} else {
1063 1064 1065 1066 1067 1068 1069
			result = acpi_battery_get_info(battery);
			if (result)
				return result;

			result = sysfs_add_battery(battery);
			if (result)
				return result;
1070
		}
1071 1072 1073

		acpi_battery_init_alarm(battery);
		acpi_battery_get_state(battery);
1074 1075 1076 1077 1078 1079
		break;
	}

	return 0;
}

1080 1081
static int __init
battery_bix_broken_package_quirk(const struct dmi_system_id *d)
1082 1083 1084 1085 1086
{
	battery_bix_broken_package = 1;
	return 0;
}

1087 1088
static int __init
battery_notification_delay_quirk(const struct dmi_system_id *d)
1089 1090 1091 1092 1093
{
	battery_notification_delay_ms = 1000;
	return 0;
}

1094 1095 1096 1097 1098 1099 1100
static int __init
battery_ac_is_broken_quirk(const struct dmi_system_id *d)
{
	battery_ac_is_broken = 1;
	return 0;
}

1101 1102 1103 1104 1105 1106 1107
static int __init
battery_do_not_check_pmic_quirk(const struct dmi_system_id *d)
{
	battery_check_pmic = 0;
	return 0;
}

1108
static const struct dmi_system_id bat_dmi_table[] __initconst = {
1109
	{
1110
		/* NEC LZ750/LS */
1111
		.callback = battery_bix_broken_package_quirk,
1112 1113 1114 1115 1116
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "NEC"),
			DMI_MATCH(DMI_PRODUCT_NAME, "PC-LZ750LS"),
		},
	},
1117
	{
1118
		/* Acer Aspire V5-573G */
1119 1120 1121 1122 1123 1124
		.callback = battery_notification_delay_quirk,
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
			DMI_MATCH(DMI_PRODUCT_NAME, "Aspire V5-573G"),
		},
	},
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
	{
		/* Point of View mobii wintab p800w */
		.callback = battery_ac_is_broken_quirk,
		.matches = {
			DMI_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
			DMI_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
			DMI_MATCH(DMI_BIOS_VERSION, "3BAIR1013"),
			/* Above matches are too generic, add bios-date match */
			DMI_MATCH(DMI_BIOS_DATE, "08/22/2014"),
		},
	},
1136
	{
1137
		/* ECS EF20EA, AXP288 PMIC but uses separate fuel-gauge */
1138 1139 1140 1141 1142 1143
		.callback = battery_do_not_check_pmic_quirk,
		.matches = {
			DMI_MATCH(DMI_PRODUCT_NAME, "EF20EA"),
		},
	},
	{
1144
		/* Lenovo Ideapad Miix 320, AXP288 PMIC, separate fuel-gauge */
1145 1146
		.callback = battery_do_not_check_pmic_quirk,
		.matches = {
1147 1148 1149
			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
			DMI_MATCH(DMI_PRODUCT_NAME, "80XF"),
			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo MIIX 320-10ICR"),
1150 1151
		},
	},
1152 1153 1154
	{},
};

1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
/*
 * Some machines'(E,G Lenovo Z480) ECs are not stable
 * during boot up and this causes battery driver fails to be
 * probed due to failure of getting battery information
 * from EC sometimes. After several retries, the operation
 * may work. So add retry code here and 20ms sleep between
 * every retries.
 */
static int acpi_battery_update_retry(struct acpi_battery *battery)
{
	int retry, ret;

	for (retry = 5; retry; retry--) {
		ret = acpi_battery_update(battery, false);
		if (!ret)
			break;

		msleep(20);
	}
	return ret;
}

L
Len Brown 已提交
1177
static int acpi_battery_add(struct acpi_device *device)
L
Linus Torvalds 已提交
1178
{
L
Len Brown 已提交
1179 1180
	int result = 0;
	struct acpi_battery *battery = NULL;
1181

L
Linus Torvalds 已提交
1182
	if (!device)
1183
		return -EINVAL;
1184 1185 1186 1187

	if (device->dep_unmet)
		return -EPROBE_DEFER;

1188
	battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
L
Linus Torvalds 已提交
1189
	if (!battery)
1190
		return -ENOMEM;
1191
	battery->device = device;
L
Linus Torvalds 已提交
1192 1193
	strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
	strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
1194
	device->driver_data = battery;
1195
	mutex_init(&battery->lock);
1196
	mutex_init(&battery->sysfs_lock);
1197
	if (acpi_has_method(battery->device->handle, "_BIX"))
1198
		set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
1199 1200

	result = acpi_battery_update_retry(battery);
1201 1202
	if (result)
		goto fail;
1203

1204
	pr_info(PREFIX "%s Slot [%s] (battery %s)\n",
1205 1206 1207
		ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
		device->status.battery_present ? "present" : "absent");

1208 1209 1210
	battery->pm_nb.notifier_call = battery_notify;
	register_pm_notifier(&battery->pm_nb);

1211 1212
	device_init_wakeup(&device->dev, 1);

1213
	return result;
1214 1215 1216 1217

fail:
	sysfs_remove_battery(battery);
	mutex_destroy(&battery->lock);
1218
	mutex_destroy(&battery->sysfs_lock);
1219 1220
	kfree(battery);
	return result;
L
Linus Torvalds 已提交
1221 1222
}

1223
static int acpi_battery_remove(struct acpi_device *device)
L
Linus Torvalds 已提交
1224
{
L
Len Brown 已提交
1225
	struct acpi_battery *battery = NULL;
L
Linus Torvalds 已提交
1226 1227

	if (!device || !acpi_driver_data(device))
1228
		return -EINVAL;
1229
	device_init_wakeup(&device->dev, 0);
1230
	battery = acpi_driver_data(device);
1231
	unregister_pm_notifier(&battery->pm_nb);
1232
	sysfs_remove_battery(battery);
1233
	mutex_destroy(&battery->lock);
1234
	mutex_destroy(&battery->sysfs_lock);
L
Linus Torvalds 已提交
1235
	kfree(battery);
1236
	return 0;
L
Linus Torvalds 已提交
1237 1238
}

1239
#ifdef CONFIG_PM_SLEEP
1240
/* this is needed to learn about changes made in suspended state */
1241
static int acpi_battery_resume(struct device *dev)
1242 1243
{
	struct acpi_battery *battery;
1244 1245

	if (!dev)
1246
		return -EINVAL;
1247 1248 1249 1250 1251

	battery = acpi_driver_data(to_acpi_device(dev));
	if (!battery)
		return -EINVAL;

1252
	battery->update_time = 0;
1253
	acpi_battery_update(battery, true);
1254
	return 0;
1255
}
1256 1257
#else
#define acpi_battery_resume NULL
1258
#endif
1259

1260 1261
static SIMPLE_DEV_PM_OPS(acpi_battery_pm, NULL, acpi_battery_resume);

1262 1263 1264 1265
static struct acpi_driver acpi_battery_driver = {
	.name = "battery",
	.class = ACPI_BATTERY_CLASS,
	.ids = battery_device_ids,
1266
	.flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1267 1268 1269
	.ops = {
		.add = acpi_battery_add,
		.remove = acpi_battery_remove,
1270
		.notify = acpi_battery_notify,
1271
		},
1272
	.drv.pm = &acpi_battery_pm,
1273 1274
};

1275
static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
L
Linus Torvalds 已提交
1276
{
1277
	unsigned int i;
1278 1279
	int result;

1280
	dmi_check_system(bat_dmi_table);
1281

1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
	if (battery_check_pmic) {
		for (i = 0; i < ARRAY_SIZE(acpi_battery_blacklist); i++)
			if (acpi_dev_present(acpi_battery_blacklist[i], "1", -1)) {
				pr_info(PREFIX ACPI_BATTERY_DEVICE_NAME
					": found native %s PMIC, not loading\n",
					acpi_battery_blacklist[i]);
				return;
			}
	}

1292
	result = acpi_bus_register_driver(&acpi_battery_driver);
1293
	battery_driver_registered = (result == 0);
1294 1295 1296 1297
}

static int __init acpi_battery_init(void)
{
1298 1299 1300
	if (acpi_disabled)
		return -ENODEV;

1301
	async_cookie = async_schedule(acpi_battery_init_async, NULL);
1302
	return 0;
L
Linus Torvalds 已提交
1303 1304
}

L
Len Brown 已提交
1305
static void __exit acpi_battery_exit(void)
L
Linus Torvalds 已提交
1306
{
1307
	async_synchronize_cookie(async_cookie + 1);
1308
	if (battery_driver_registered) {
1309
		acpi_bus_unregister_driver(&acpi_battery_driver);
1310 1311
		battery_hook_exit();
	}
L
Linus Torvalds 已提交
1312 1313 1314 1315
}

module_init(acpi_battery_init);
module_exit(acpi_battery_exit);