File Index
Files and directories
Sign in
FILE

battery.c

File preview with syntax highlighting.

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