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