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