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