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(battery->device->handle, 651 use_bix ? "_BIX":"_BIF", 652 NULL, &buffer); 653 654 if (ACPI_FAILURE(status)) { 655 acpi_handle_info(battery->device->handle, 656 "%s evaluation failed: %s\n", 657 use_bix ? "_BIX":"_BIF", 658 acpi_format_exception(status)); 659 } else { 660 result = extract_battery_info(use_bix, 661 battery, 662 &buffer); 663 664 kfree(buffer.pointer); 665 break; 666 } 667 } 668 669 if (!result && !use_bix && xinfo) 670 pr_warn(FW_BUG "The _BIX method is broken, using _BIF.\n"); 671 672 return result; 673 } 674 675 static int acpi_battery_get_state(struct acpi_battery *battery) 676 { 677 int result = 0; 678 acpi_status status = 0; 679 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 680 681 lockdep_assert_held(&battery->property_lock); 682 683 if (!acpi_battery_present(battery)) 684 return 0; 685 686 if (battery->update_time && 687 time_before(jiffies, battery->update_time + 688 msecs_to_jiffies(cache_time))) 689 return 0; 690 691 status = acpi_evaluate_object(battery->device->handle, "_BST", 692 NULL, &buffer); 693 if (ACPI_FAILURE(status)) { 694 acpi_handle_info(battery->device->handle, 695 "_BST evaluation failed: %s", 696 acpi_format_exception(status)); 697 return -ENODEV; 698 } 699 700 result = extract_package(battery, buffer.pointer, 701 state_offsets, ARRAY_SIZE(state_offsets)); 702 battery->update_time = jiffies; 703 kfree(buffer.pointer); 704 705 /* For buggy DSDTs that report negative 16-bit values for either 706 * charging or discharging current and/or report 0 as 65536 707 * due to bad math. 708 */ 709 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA && 710 battery->rate_now != ACPI_BATTERY_VALUE_UNKNOWN && 711 (s16)(battery->rate_now) < 0) { 712 battery->rate_now = abs((s16)battery->rate_now); 713 pr_warn_once(FW_BUG "(dis)charge rate invalid.\n"); 714 } 715 716 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags) 717 && battery->capacity_now >= 0 && battery->capacity_now <= 100) 718 battery->capacity_now = (battery->capacity_now * 719 battery->full_charge_capacity) / 100; 720 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) && 721 battery->power_unit && battery->design_voltage) { 722 battery->capacity_now = battery->capacity_now * 723 10000 / battery->design_voltage; 724 } 725 if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags) && 726 battery->capacity_now > battery->full_charge_capacity) 727 battery->capacity_now = battery->full_charge_capacity; 728 729 return result; 730 } 731 732 static int acpi_battery_set_alarm(struct acpi_battery *battery) 733 { 734 acpi_status status = 0; 735 736 lockdep_assert_held(&battery->property_lock); 737 738 if (!acpi_battery_present(battery) || 739 !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags)) 740 return -ENODEV; 741 742 status = acpi_execute_simple_method(battery->device->handle, "_BTP", 743 battery->alarm); 744 if (ACPI_FAILURE(status)) 745 return -ENODEV; 746 747 acpi_handle_debug(battery->device->handle, "Alarm set to %d\n", 748 battery->alarm); 749 750 return 0; 751 } 752 753 static int acpi_battery_init_alarm(struct acpi_battery *battery) 754 { 755 lockdep_assert_held(&battery->property_lock); 756 757 /* See if alarms are supported, and if so, set default */ 758 if (!acpi_has_method(battery->device->handle, "_BTP")) { 759 clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags); 760 return 0; 761 } 762 set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags); 763 if (!battery->alarm) 764 battery->alarm = battery->design_capacity_warning; 765 return acpi_battery_set_alarm(battery); 766 } 767 768 static ssize_t acpi_battery_alarm_show(struct device *dev, 769 struct device_attribute *attr, 770 char *buf) 771 { 772 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev)); 773 774 guard(mutex)(&battery->property_lock); 775 776 return sysfs_emit(buf, "%d\n", battery->alarm * 1000); 777 } 778 779 static ssize_t acpi_battery_alarm_store(struct device *dev, 780 struct device_attribute *attr, 781 const char *buf, size_t count) 782 { 783 unsigned long x; 784 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev)); 785 int err; 786 787 err = kstrtoul(buf, 10, &x); 788 if (err) 789 return err; 790 791 guard(mutex)(&battery->property_lock); 792 793 battery->alarm = x / 1000; 794 if (acpi_battery_present(battery)) 795 acpi_battery_set_alarm(battery); 796 return count; 797 } 798 799 static struct device_attribute alarm_attr = { 800 .attr = {.name = "alarm", .mode = 0644}, 801 .show = acpi_battery_alarm_show, 802 .store = acpi_battery_alarm_store, 803 }; 804 805 static struct attribute *acpi_battery_attrs[] = { 806 &alarm_attr.attr, 807 NULL 808 }; 809 ATTRIBUTE_GROUPS(acpi_battery); 810 811 /* 812 * The Battery Hooking API 813 * 814 * This API is used inside other drivers that need to expose 815 * platform-specific behaviour within the generic driver in a 816 * generic way. 817 * 818 */ 819 820 static LIST_HEAD(acpi_battery_list); 821 static LIST_HEAD(battery_hook_list); 822 static DEFINE_MUTEX(hook_mutex); 823 824 static void battery_hook_unregister_unlocked(struct acpi_battery_hook *hook) 825 { 826 struct acpi_battery *battery; 827 828 /* 829 * In order to remove a hook, we first need to 830 * de-register all the batteries that are registered. 831 */ 832 list_for_each_entry(battery, &acpi_battery_list, list) { 833 if (!hook->remove_battery(battery->bat, hook)) 834 power_supply_changed(battery->bat); 835 } 836 list_del_init(&hook->list); 837 838 pr_info("hook unregistered: %s\n", hook->name); 839 } 840 841 void battery_hook_unregister(struct acpi_battery_hook *hook) 842 { 843 mutex_lock(&hook_mutex); 844 /* 845 * Ignore already unregistered battery hooks. This might happen 846 * if a battery hook was previously unloaded due to an error when 847 * adding a new battery. 848 */ 849 if (!list_empty(&hook->list)) 850 battery_hook_unregister_unlocked(hook); 851 852 mutex_unlock(&hook_mutex); 853 } 854 EXPORT_SYMBOL_GPL(battery_hook_unregister); 855 856 void battery_hook_register(struct acpi_battery_hook *hook) 857 { 858 struct acpi_battery *battery; 859 860 mutex_lock(&hook_mutex); 861 list_add(&hook->list, &battery_hook_list); 862 /* 863 * Now that the driver is registered, we need 864 * to notify the hook that a battery is available 865 * for each battery, so that the driver may add 866 * its attributes. 867 */ 868 list_for_each_entry(battery, &acpi_battery_list, list) { 869 if (hook->add_battery(battery->bat, hook)) { 870 /* 871 * If a add-battery returns non-zero, 872 * the registration of the hook has failed, 873 * and we will not add it to the list of loaded 874 * hooks. 875 */ 876 pr_err("hook failed to load: %s", hook->name); 877 battery_hook_unregister_unlocked(hook); 878 goto end; 879 } 880 881 power_supply_changed(battery->bat); 882 } 883 pr_info("new hook: %s\n", hook->name); 884 end: 885 mutex_unlock(&hook_mutex); 886 } 887 EXPORT_SYMBOL_GPL(battery_hook_register); 888 889 static void devm_battery_hook_unregister(void *data) 890 { 891 struct acpi_battery_hook *hook = data; 892 893 battery_hook_unregister(hook); 894 } 895 896 int devm_battery_hook_register(struct device *dev, struct acpi_battery_hook *hook) 897 { 898 battery_hook_register(hook); 899 900 return devm_add_action_or_reset(dev, devm_battery_hook_unregister, hook); 901 } 902 EXPORT_SYMBOL_GPL(devm_battery_hook_register); 903 904 /* 905 * This function gets called right after the battery sysfs 906 * attributes have been added, so that the drivers that 907 * define custom sysfs attributes can add their own. 908 */ 909 static void battery_hook_add_battery(struct acpi_battery *battery) 910 { 911 struct acpi_battery_hook *hook_node, *tmp; 912 913 mutex_lock(&hook_mutex); 914 INIT_LIST_HEAD(&battery->list); 915 list_add(&battery->list, &acpi_battery_list); 916 /* 917 * Since we added a new battery to the list, we need to 918 * iterate over the hooks and call add_battery for each 919 * hook that was registered. This usually happens 920 * when a battery gets hotplugged or initialized 921 * during the battery module initialization. 922 */ 923 list_for_each_entry_safe(hook_node, tmp, &battery_hook_list, list) { 924 if (hook_node->add_battery(battery->bat, hook_node)) { 925 /* 926 * The notification of the hook has failed, to 927 * prevent further errors we will unload the hook. 928 */ 929 pr_err("error in hook, unloading: %s", 930 hook_node->name); 931 battery_hook_unregister_unlocked(hook_node); 932 } 933 } 934 mutex_unlock(&hook_mutex); 935 } 936 937 static void battery_hook_remove_battery(struct acpi_battery *battery) 938 { 939 struct acpi_battery_hook *hook; 940 941 mutex_lock(&hook_mutex); 942 /* 943 * Before removing the hook, we need to remove all 944 * custom attributes from the battery. 945 */ 946 list_for_each_entry(hook, &battery_hook_list, list) { 947 hook->remove_battery(battery->bat, hook); 948 } 949 /* Then, just remove the battery from the list */ 950 list_del(&battery->list); 951 mutex_unlock(&hook_mutex); 952 } 953 954 static void __exit battery_hook_exit(void) 955 { 956 struct acpi_battery_hook *hook; 957 struct acpi_battery_hook *ptr; 958 /* 959 * At this point, the acpi_bus_unregister_driver() 960 * has called remove for all batteries. We just 961 * need to remove the hooks. 962 */ 963 list_for_each_entry_safe(hook, ptr, &battery_hook_list, list) { 964 battery_hook_unregister(hook); 965 } 966 mutex_destroy(&hook_mutex); 967 } 968 969 static int sysfs_add_battery(struct acpi_battery *battery) 970 { 971 bool extended_info_available = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags); 972 struct power_supply_config psy_cfg = { 973 .drv_data = battery, 974 .attr_grp = acpi_battery_groups, 975 .no_wakeup_source = true, 976 }; 977 bool full_cap_broken = false; 978 int power_unit; 979 980 scoped_guard(mutex, &battery->property_lock) { 981 power_unit = battery->power_unit; 982 983 if (!ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity) && 984 !ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity)) 985 full_cap_broken = true; 986 } 987 988 if (power_unit == ACPI_BATTERY_POWER_UNIT_MA) { 989 if (full_cap_broken) { 990 if (extended_info_available) { 991 battery->bat_desc.properties = 992 charge_battery_full_cap_broken_extended_props; 993 battery->bat_desc.num_properties = 994 ARRAY_SIZE(charge_battery_full_cap_broken_extended_props); 995 } else { 996 battery->bat_desc.properties = 997 charge_battery_full_cap_broken_props; 998 battery->bat_desc.num_properties = 999 ARRAY_SIZE(charge_battery_full_cap_broken_props); 1000 } 1001 } else { 1002 if (extended_info_available) { 1003 battery->bat_desc.properties = 1004 charge_battery_extended_props; 1005 battery->bat_desc.num_properties = 1006 ARRAY_SIZE(charge_battery_extended_props); 1007 } else { 1008 battery->bat_desc.properties = 1009 charge_battery_props; 1010 battery->bat_desc.num_properties = 1011 ARRAY_SIZE(charge_battery_props); 1012 } 1013 } 1014 } else { 1015 if (full_cap_broken) { 1016 if (extended_info_available) { 1017 battery->bat_desc.properties = 1018 energy_battery_full_cap_broken_extended_props; 1019 battery->bat_desc.num_properties = 1020 ARRAY_SIZE(energy_battery_full_cap_broken_extended_props); 1021 } else { 1022 battery->bat_desc.properties = 1023 energy_battery_full_cap_broken_props; 1024 battery->bat_desc.num_properties = 1025 ARRAY_SIZE(energy_battery_full_cap_broken_props); 1026 } 1027 } else { 1028 if (extended_info_available) { 1029 battery->bat_desc.properties = 1030 energy_battery_extended_props; 1031 battery->bat_desc.num_properties = 1032 ARRAY_SIZE(energy_battery_extended_props); 1033 } else { 1034 battery->bat_desc.properties = 1035 energy_battery_props; 1036 battery->bat_desc.num_properties = 1037 ARRAY_SIZE(energy_battery_props); 1038 } 1039 } 1040 } 1041 1042 battery->bat_desc.name = acpi_device_bid(battery->device); 1043 battery->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY; 1044 battery->bat_desc.get_property = acpi_battery_get_property; 1045 1046 battery->bat = power_supply_register(&battery->device->dev, 1047 &battery->bat_desc, &psy_cfg); 1048 1049 if (IS_ERR(battery->bat)) { 1050 int result = PTR_ERR(battery->bat); 1051 1052 battery->bat = NULL; 1053 return result; 1054 } 1055 battery_hook_add_battery(battery); 1056 return 0; 1057 } 1058 1059 static void sysfs_remove_battery(struct acpi_battery *battery) 1060 { 1061 if (!battery->bat) 1062 return; 1063 1064 battery_hook_remove_battery(battery); 1065 power_supply_unregister(battery->bat); 1066 battery->bat = NULL; 1067 } 1068 1069 static void find_battery(const struct dmi_header *dm, void *private) 1070 { 1071 struct acpi_battery *battery = (struct acpi_battery *)private; 1072 1073 lockdep_assert_held(&battery->property_lock); 1074 1075 /* Note: the hardcoded offsets below have been extracted from 1076 * the source code of dmidecode. 1077 */ 1078 if (dm->type == DMI_ENTRY_PORTABLE_BATTERY && dm->length >= 8) { 1079 const u8 *dmi_data = (const u8 *)(dm + 1); 1080 int dmi_capacity = get_unaligned((const u16 *)(dmi_data + 6)); 1081 1082 if (dm->length >= 18) 1083 dmi_capacity *= dmi_data[17]; 1084 if (battery->design_capacity * battery->design_voltage / 1000 1085 != dmi_capacity && 1086 battery->design_capacity * 10 == dmi_capacity) 1087 set_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, 1088 &battery->flags); 1089 } 1090 } 1091 1092 /* 1093 * According to the ACPI spec, some kinds of primary batteries can 1094 * report percentage battery remaining capacity directly to OS. 1095 * In this case, it reports the Last Full Charged Capacity == 100 1096 * and BatteryPresentRate == 0xFFFFFFFF. 1097 * 1098 * Now we found some battery reports percentage remaining capacity 1099 * even if it's rechargeable. 1100 * https://bugzilla.kernel.org/show_bug.cgi?id=15979 1101 * 1102 * Handle this correctly so that they won't break userspace. 1103 */ 1104 static void acpi_battery_quirks(struct acpi_battery *battery) 1105 { 1106 lockdep_assert_held(&battery->property_lock); 1107 1108 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)) 1109 return; 1110 1111 if (battery->full_charge_capacity == 100 && 1112 battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN && 1113 battery->capacity_now >= 0 && battery->capacity_now <= 100) { 1114 set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags); 1115 battery->full_charge_capacity = battery->design_capacity; 1116 battery->capacity_now = (battery->capacity_now * 1117 battery->full_charge_capacity) / 100; 1118 } 1119 1120 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags)) 1121 return; 1122 1123 if (battery->power_unit && dmi_name_in_vendors("LENOVO")) { 1124 const char *s; 1125 1126 s = dmi_get_system_info(DMI_PRODUCT_VERSION); 1127 if (s && !strncasecmp(s, "ThinkPad", 8)) { 1128 dmi_walk(find_battery, battery); 1129 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, 1130 &battery->flags) && 1131 battery->design_voltage) { 1132 battery->design_capacity = 1133 battery->design_capacity * 1134 10000 / battery->design_voltage; 1135 battery->full_charge_capacity = 1136 battery->full_charge_capacity * 1137 10000 / battery->design_voltage; 1138 battery->design_capacity_warning = 1139 battery->design_capacity_warning * 1140 10000 / battery->design_voltage; 1141 battery->capacity_now = battery->capacity_now * 1142 10000 / battery->design_voltage; 1143 } 1144 } 1145 } 1146 1147 if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags)) 1148 return; 1149 1150 if (acpi_battery_is_degraded(battery) && 1151 battery->capacity_now > battery->full_charge_capacity) { 1152 set_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags); 1153 battery->capacity_now = battery->full_charge_capacity; 1154 } 1155 } 1156 1157 static int acpi_battery_update(struct acpi_battery *battery, bool resume) 1158 { 1159 int result = acpi_battery_get_status(battery); 1160 bool wakeup; 1161 1162 if (result) 1163 return result; 1164 1165 if (!acpi_battery_present(battery)) { 1166 sysfs_remove_battery(battery); 1167 scoped_guard(mutex, &battery->property_lock) 1168 battery->update_time = 0; 1169 return 0; 1170 } 1171 1172 if (resume) 1173 return 0; 1174 1175 scoped_guard(mutex, &battery->property_lock) { 1176 if (!battery->update_time) { 1177 result = acpi_battery_get_info(battery); 1178 if (result) 1179 return result; 1180 acpi_battery_init_alarm(battery); 1181 } 1182 1183 result = acpi_battery_get_state(battery); 1184 if (result) 1185 return result; 1186 acpi_battery_quirks(battery); 1187 1188 wakeup = ((battery->state & ACPI_BATTERY_STATE_CRITICAL) || 1189 (test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags) && 1190 (battery->capacity_now <= battery->alarm))); 1191 } 1192 1193 if (!battery->bat) { 1194 result = sysfs_add_battery(battery); 1195 if (result) 1196 return result; 1197 } 1198 1199 /* 1200 * Wakeup the system if battery is critical low 1201 * or lower than the alarm level 1202 */ 1203 if (wakeup) 1204 acpi_pm_wakeup_event(battery->phys_dev); 1205 1206 return result; 1207 } 1208 1209 static void acpi_battery_refresh(struct acpi_battery *battery) 1210 { 1211 int power_unit; 1212 1213 if (!battery->bat) 1214 return; 1215 1216 scoped_guard(mutex, &battery->property_lock) { 1217 power_unit = battery->power_unit; 1218 1219 acpi_battery_get_info(battery); 1220 1221 if (power_unit == battery->power_unit) 1222 return; 1223 } 1224 1225 /* The battery has changed its reporting units. */ 1226 sysfs_remove_battery(battery); 1227 sysfs_add_battery(battery); 1228 } 1229 1230 /* Driver Interface */ 1231 static void acpi_battery_notification_worker(struct work_struct *work) 1232 { 1233 struct acpi_battery *battery = container_of(work, struct acpi_battery, 1234 acpi_notif_dwork.work); 1235 struct acpi_device *device = battery->device; 1236 u32 events[MAX_QUEUED_EVENTS]; 1237 struct power_supply *old; 1238 unsigned int count, i; 1239 1240 guard(mutex)(&battery->update_lock); 1241 1242 count = kfifo_out(&battery->acpi_notif_fifo, events, sizeof(events)); 1243 count /= sizeof(events[0]); 1244 if (!count) 1245 return; 1246 1247 pr_debug("merged %u battery notifications within %dms\n", count, NOTIF_MERGING_MS); 1248 1249 old = battery->bat; 1250 /* 1251 * On Acer Aspire V5-573G notifications are sometimes triggered too 1252 * early. For example, when AC is unplugged and notification is 1253 * triggered, battery state is still reported as "Full", and changes to 1254 * "Discharging" only after short delay, without any notification. 1255 */ 1256 if (battery_notification_delay_ms > 0) 1257 msleep(battery_notification_delay_ms); 1258 1259 for (i = 0; i < count; i++) { 1260 if (events[i] == ACPI_BATTERY_NOTIFY_INFO) { 1261 acpi_battery_refresh(battery); 1262 break; 1263 } 1264 } 1265 1266 acpi_battery_update(battery, false); 1267 1268 for (i = 0; i < count; i++) { 1269 acpi_bus_generate_netlink_event(ACPI_BATTERY_CLASS, 1270 dev_name(&device->dev), events[i], 1271 acpi_battery_present(battery)); 1272 acpi_notifier_call_chain(ACPI_BATTERY_CLASS, acpi_device_bid(device), 1273 events[i], acpi_battery_present(battery)); 1274 } 1275 1276 /* acpi_battery_update could remove power_supply object */ 1277 if (old && battery->bat) 1278 power_supply_changed(battery->bat); 1279 } 1280 1281 static void acpi_battery_notify(acpi_handle handle, u32 event, void *data) 1282 { 1283 struct acpi_battery *battery = data; 1284 1285 guard(mutex)(&battery->update_lock); 1286 1287 if (kfifo_avail(&battery->acpi_notif_fifo) >= sizeof(event)) { 1288 kfifo_in(&battery->acpi_notif_fifo, &event, sizeof(event)); 1289 schedule_delayed_work(&battery->acpi_notif_dwork, 1290 msecs_to_jiffies(NOTIF_MERGING_MS)); 1291 1292 return; 1293 } 1294 1295 pr_err_ratelimited("too many battery notifications within %dms\n", NOTIF_MERGING_MS); 1296 } 1297 1298 static int battery_notify(struct notifier_block *nb, 1299 unsigned long mode, void *_unused) 1300 { 1301 struct acpi_battery *battery = container_of(nb, struct acpi_battery, 1302 pm_nb); 1303 1304 if (mode == PM_POST_SUSPEND || mode == PM_POST_HIBERNATION) { 1305 guard(mutex)(&battery->update_lock); 1306 1307 if (!acpi_battery_present(battery)) 1308 return 0; 1309 1310 if (battery->bat) { 1311 acpi_battery_refresh(battery); 1312 } else { 1313 int result; 1314 1315 scoped_guard(mutex, &battery->property_lock) { 1316 result = acpi_battery_get_info(battery); 1317 if (result) 1318 return result; 1319 } 1320 1321 result = sysfs_add_battery(battery); 1322 if (result) 1323 return result; 1324 } 1325 1326 scoped_guard(mutex, &battery->property_lock) { 1327 acpi_battery_init_alarm(battery); 1328 acpi_battery_get_state(battery); 1329 } 1330 } 1331 1332 return 0; 1333 } 1334 1335 static int __init 1336 battery_bix_broken_package_quirk(const struct dmi_system_id *d) 1337 { 1338 battery_bix_broken_package = 1; 1339 return 0; 1340 } 1341 1342 static int __init 1343 battery_notification_delay_quirk(const struct dmi_system_id *d) 1344 { 1345 battery_notification_delay_ms = 1000; 1346 return 0; 1347 } 1348 1349 static int __init 1350 battery_ac_is_broken_quirk(const struct dmi_system_id *d) 1351 { 1352 battery_ac_is_broken = 1; 1353 return 0; 1354 } 1355 1356 static const struct dmi_system_id bat_dmi_table[] __initconst = { 1357 { 1358 /* NEC LZ750/LS */ 1359 .callback = battery_bix_broken_package_quirk, 1360 .matches = { 1361 DMI_MATCH(DMI_SYS_VENDOR, "NEC"), 1362 DMI_MATCH(DMI_PRODUCT_NAME, "PC-LZ750LS"), 1363 }, 1364 }, 1365 { 1366 /* Acer Aspire V5-573G */ 1367 .callback = battery_notification_delay_quirk, 1368 .matches = { 1369 DMI_MATCH(DMI_SYS_VENDOR, "Acer"), 1370 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire V5-573G"), 1371 }, 1372 }, 1373 { 1374 /* Point of View mobii wintab p800w */ 1375 .callback = battery_ac_is_broken_quirk, 1376 .matches = { 1377 DMI_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"), 1378 DMI_MATCH(DMI_BOARD_NAME, "Aptio CRB"), 1379 DMI_MATCH(DMI_BIOS_VERSION, "3BAIR1013"), 1380 /* Above matches are too generic, add bios-date match */ 1381 DMI_MATCH(DMI_BIOS_DATE, "08/22/2014"), 1382 }, 1383 }, 1384 { 1385 /* Microsoft Surface Go 3 */ 1386 .callback = battery_notification_delay_quirk, 1387 .matches = { 1388 DMI_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"), 1389 DMI_MATCH(DMI_PRODUCT_NAME, "Surface Go 3"), 1390 }, 1391 }, 1392 {}, 1393 }; 1394 1395 static void acpi_battery_wakeup_cleanup(void *data) 1396 { 1397 device_init_wakeup(data, false); 1398 } 1399 1400 static int devm_acpi_battery_init_wakeup(struct device *dev) 1401 { 1402 device_init_wakeup(dev, true); 1403 return devm_add_action_or_reset(dev, acpi_battery_wakeup_cleanup, dev); 1404 } 1405 1406 static void sysfs_battery_cleanup(void *data) 1407 { 1408 struct acpi_battery *battery = data; 1409 1410 guard(mutex)(&battery->update_lock); 1411 1412 sysfs_remove_battery(battery); 1413 } 1414 1415 /* 1416 * Some machines'(E,G Lenovo Z480) ECs are not stable 1417 * during boot up and this causes battery driver fails to be 1418 * probed due to failure of getting battery information 1419 * from EC sometimes. After several retries, the operation 1420 * may work. So add retry code here and 20ms sleep between 1421 * every retries. 1422 */ 1423 static int devm_acpi_battery_update_retry(struct device *dev, 1424 struct acpi_battery *battery) 1425 { 1426 int retry, ret; 1427 1428 ret = devm_add_action(dev, sysfs_battery_cleanup, battery); 1429 if (ret) 1430 return ret; 1431 1432 guard(mutex)(&battery->update_lock); 1433 1434 for (retry = 5; retry; retry--) { 1435 ret = acpi_battery_update(battery, false); 1436 if (!ret) 1437 break; 1438 1439 msleep(20); 1440 } 1441 return ret; 1442 } 1443 1444 static void acpi_battery_notify_dwork_cleanup(void *data) 1445 { 1446 struct acpi_battery *battery = data; 1447 1448 cancel_delayed_work_sync(&battery->acpi_notif_dwork); 1449 kfifo_free(&battery->acpi_notif_fifo); 1450 } 1451 1452 static int devm_acpi_battery_init_notify_dwork(struct device *dev, 1453 struct acpi_battery *battery) 1454 { 1455 int ret; 1456 1457 INIT_DELAYED_WORK(&battery->acpi_notif_dwork, acpi_battery_notification_worker); 1458 1459 ret = kfifo_alloc(&battery->acpi_notif_fifo, 1460 MAX_QUEUED_EVENTS * sizeof(u32), GFP_KERNEL); 1461 if (ret) 1462 return ret; 1463 1464 return devm_add_action_or_reset(dev, acpi_battery_notify_dwork_cleanup, battery); 1465 } 1466 1467 static int acpi_battery_probe(struct platform_device *pdev) 1468 { 1469 struct device *dev = &pdev->dev; 1470 struct acpi_battery *battery; 1471 struct acpi_device *device; 1472 int result; 1473 1474 device = ACPI_COMPANION(dev); 1475 if (!device) 1476 return -ENODEV; 1477 1478 if (device->dep_unmet) 1479 return -EPROBE_DEFER; 1480 1481 battery = devm_kzalloc(dev, sizeof(*battery), GFP_KERNEL); 1482 if (!battery) 1483 return -ENOMEM; 1484 1485 platform_set_drvdata(pdev, battery); 1486 1487 battery->phys_dev = &pdev->dev; 1488 battery->device = device; 1489 1490 result = devm_mutex_init(dev, &battery->update_lock); 1491 if (result) 1492 return result; 1493 1494 result = devm_mutex_init(&pdev->dev, &battery->property_lock); 1495 if (result) 1496 return result; 1497 1498 if (acpi_has_method(battery->device->handle, "_BIX")) 1499 set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags); 1500 1501 result = devm_acpi_battery_update_retry(dev, battery); 1502 if (result) 1503 return result; 1504 1505 pr_info("Slot [%s] (battery %s)\n", acpi_device_bid(device), 1506 device->status.battery_present ? "present" : "absent"); 1507 1508 result = devm_acpi_battery_init_wakeup(dev); 1509 if (result) 1510 return result; 1511 1512 result = devm_acpi_battery_init_notify_dwork(dev, battery); 1513 if (result) 1514 return result; 1515 1516 result = devm_acpi_install_notify_handler(dev, ACPI_ALL_NOTIFY, 1517 acpi_battery_notify, battery); 1518 if (result) 1519 return result; 1520 1521 battery->pm_nb.notifier_call = battery_notify; 1522 return register_pm_notifier(&battery->pm_nb); 1523 } 1524 1525 static void acpi_battery_remove(struct platform_device *pdev) 1526 { 1527 struct acpi_battery *battery = platform_get_drvdata(pdev); 1528 1529 unregister_pm_notifier(&battery->pm_nb); 1530 } 1531 1532 /* this is needed to learn about changes made in suspended state */ 1533 static int acpi_battery_resume(struct device *dev) 1534 { 1535 struct acpi_battery *battery = dev_get_drvdata(dev); 1536 1537 if (!battery) 1538 return -EINVAL; 1539 1540 battery->update_time = 0; 1541 1542 guard(mutex)(&battery->update_lock); 1543 1544 acpi_battery_update(battery, true); 1545 return 0; 1546 } 1547 1548 static DEFINE_SIMPLE_DEV_PM_OPS(acpi_battery_pm, NULL, acpi_battery_resume); 1549 1550 static struct platform_driver acpi_battery_driver = { 1551 .probe = acpi_battery_probe, 1552 .remove = acpi_battery_remove, 1553 .driver = { 1554 .name = "acpi-battery", 1555 .acpi_match_table = battery_device_ids, 1556 .pm = pm_sleep_ptr(&acpi_battery_pm), 1557 .probe_type = PROBE_PREFER_ASYNCHRONOUS, 1558 }, 1559 }; 1560 1561 static int __init acpi_battery_init(void) 1562 { 1563 if (acpi_disabled || acpi_quirk_skip_acpi_ac_and_battery()) 1564 return -ENODEV; 1565 1566 dmi_check_system(bat_dmi_table); 1567 1568 return platform_driver_register(&acpi_battery_driver); 1569 } 1570 1571 static void __exit acpi_battery_exit(void) 1572 { 1573 platform_driver_unregister(&acpi_battery_driver); 1574 battery_hook_exit(); 1575 } 1576 1577 module_init(acpi_battery_init); 1578 module_exit(acpi_battery_exit); 1579