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