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