2 * battery.c - ACPI Battery Driver (Revision: 2.0)
4 * Copyright (C) 2007 Alexey Starikovskiy <astarikovskiy@suse.de>
5 * Copyright (C) 2004-2007 Vladimir Lebedev <vladimir.p.lebedev@intel.com>
6 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
7 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or (at
14 * your option) any later version.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 #include <linux/async.h>
27 #include <linux/delay.h>
28 #include <linux/dmi.h>
29 #include <linux/jiffies.h>
30 #include <linux/kernel.h>
31 #include <linux/list.h>
32 #include <linux/module.h>
33 #include <linux/mutex.h>
34 #include <linux/slab.h>
35 #include <linux/suspend.h>
36 #include <linux/types.h>
38 #include <asm/unaligned.h>
40 #ifdef CONFIG_ACPI_PROCFS_POWER
41 #include <linux/proc_fs.h>
42 #include <linux/seq_file.h>
43 #include <linux/uaccess.h>
46 #include <linux/acpi.h>
47 #include <linux/power_supply.h>
49 #include <acpi/battery.h>
51 #define PREFIX "ACPI: "
53 #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
54 #define ACPI_BATTERY_CAPACITY_VALID(capacity) \
55 ((capacity) != 0 && (capacity) != ACPI_BATTERY_VALUE_UNKNOWN)
57 #define ACPI_BATTERY_DEVICE_NAME "Battery"
59 /* Battery power unit: 0 means mW, 1 means mA */
60 #define ACPI_BATTERY_POWER_UNIT_MA 1
62 #define ACPI_BATTERY_STATE_DISCHARGING 0x1
63 #define ACPI_BATTERY_STATE_CHARGING 0x2
64 #define ACPI_BATTERY_STATE_CRITICAL 0x4
66 #define _COMPONENT ACPI_BATTERY_COMPONENT
68 ACPI_MODULE_NAME("battery");
70 MODULE_AUTHOR("Paul Diefenbaugh");
71 MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
72 MODULE_DESCRIPTION("ACPI Battery Driver");
73 MODULE_LICENSE("GPL");
75 static async_cookie_t async_cookie
;
76 static bool battery_driver_registered
;
77 static int battery_bix_broken_package
;
78 static int battery_notification_delay_ms
;
79 static int battery_ac_is_broken
;
80 static int battery_check_pmic
= 1;
81 static unsigned int cache_time
= 1000;
82 module_param(cache_time
, uint
, 0644);
83 MODULE_PARM_DESC(cache_time
, "cache time in milliseconds");
85 #ifdef CONFIG_ACPI_PROCFS_POWER
86 extern struct proc_dir_entry
*acpi_lock_battery_dir(void);
87 extern void *acpi_unlock_battery_dir(struct proc_dir_entry
*acpi_battery_dir
);
90 static const struct acpi_device_id battery_device_ids
[] = {
95 MODULE_DEVICE_TABLE(acpi
, battery_device_ids
);
97 /* Lists of PMIC ACPI HIDs with an (often better) native battery driver */
98 static const char * const acpi_battery_blacklist
[] = {
99 "INT33F4", /* X-Powers AXP288 PMIC */
103 ACPI_BATTERY_ALARM_PRESENT
,
104 ACPI_BATTERY_XINFO_PRESENT
,
105 ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY
,
106 /* On Lenovo Thinkpad models from 2010 and 2011, the power unit
107 switches between mWh and mAh depending on whether the system
108 is running on battery or not. When mAh is the unit, most
109 reported values are incorrect and need to be adjusted by
110 10000/design_voltage. Verified on x201, t410, t410s, and x220.
111 Pre-2010 and 2012 models appear to always report in mWh and
112 are thus unaffected (tested with t42, t61, t500, x200, x300,
113 and x230). Also, in mid-2012 Lenovo issued a BIOS update for
114 the 2011 models that fixes the issue (tested on x220 with a
115 post-1.29 BIOS), but as of Nov. 2012, no such update is
116 available for the 2010 models. */
117 ACPI_BATTERY_QUIRK_THINKPAD_MAH
,
118 /* for batteries reporting current capacity with design capacity
119 * on a full charge, but showing degradation in full charge cap.
121 ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE
,
124 struct acpi_battery
{
126 struct mutex sysfs_lock
;
127 struct power_supply
*bat
;
128 struct power_supply_desc bat_desc
;
129 struct acpi_device
*device
;
130 struct notifier_block pm_nb
;
131 struct list_head list
;
132 unsigned long update_time
;
138 int full_charge_capacity
;
141 int design_capacity_warning
;
142 int design_capacity_low
;
144 int measurement_accuracy
;
145 int max_sampling_time
;
146 int min_sampling_time
;
147 int max_averaging_interval
;
148 int min_averaging_interval
;
149 int capacity_granularity_1
;
150 int capacity_granularity_2
;
152 char model_number
[32];
153 char serial_number
[32];
161 #define to_acpi_battery(x) power_supply_get_drvdata(x)
163 static inline int acpi_battery_present(struct acpi_battery
*battery
)
165 return battery
->device
->status
.battery_present
;
168 static int acpi_battery_technology(struct acpi_battery
*battery
)
170 if (!strcasecmp("NiCd", battery
->type
))
171 return POWER_SUPPLY_TECHNOLOGY_NiCd
;
172 if (!strcasecmp("NiMH", battery
->type
))
173 return POWER_SUPPLY_TECHNOLOGY_NiMH
;
174 if (!strcasecmp("LION", battery
->type
))
175 return POWER_SUPPLY_TECHNOLOGY_LION
;
176 if (!strncasecmp("LI-ION", battery
->type
, 6))
177 return POWER_SUPPLY_TECHNOLOGY_LION
;
178 if (!strcasecmp("LiP", battery
->type
))
179 return POWER_SUPPLY_TECHNOLOGY_LIPO
;
180 return POWER_SUPPLY_TECHNOLOGY_UNKNOWN
;
183 static int acpi_battery_get_state(struct acpi_battery
*battery
);
185 static int acpi_battery_is_charged(struct acpi_battery
*battery
)
187 /* charging, discharging or critical low */
188 if (battery
->state
!= 0)
191 /* battery not reporting charge */
192 if (battery
->capacity_now
== ACPI_BATTERY_VALUE_UNKNOWN
||
193 battery
->capacity_now
== 0)
196 /* good batteries update full_charge as the batteries degrade */
197 if (battery
->full_charge_capacity
== battery
->capacity_now
)
200 /* fallback to using design values for broken batteries */
201 if (battery
->design_capacity
== battery
->capacity_now
)
204 /* we don't do any sort of metric based on percentages */
208 static bool acpi_battery_is_degraded(struct acpi_battery
*battery
)
210 return ACPI_BATTERY_CAPACITY_VALID(battery
->full_charge_capacity
) &&
211 ACPI_BATTERY_CAPACITY_VALID(battery
->design_capacity
) &&
212 battery
->full_charge_capacity
< battery
->design_capacity
;
215 static int acpi_battery_handle_discharging(struct acpi_battery
*battery
)
218 * Some devices wrongly report discharging if the battery's charge level
219 * was above the device's start charging threshold atm the AC adapter
220 * was plugged in and the device thus did not start a new charge cycle.
222 if ((battery_ac_is_broken
|| power_supply_is_system_supplied()) &&
223 battery
->rate_now
== 0)
224 return POWER_SUPPLY_STATUS_NOT_CHARGING
;
226 return POWER_SUPPLY_STATUS_DISCHARGING
;
229 static int acpi_battery_get_property(struct power_supply
*psy
,
230 enum power_supply_property psp
,
231 union power_supply_propval
*val
)
233 int full_capacity
= ACPI_BATTERY_VALUE_UNKNOWN
, ret
= 0;
234 struct acpi_battery
*battery
= to_acpi_battery(psy
);
236 if (acpi_battery_present(battery
)) {
237 /* run battery update only if it is present */
238 acpi_battery_get_state(battery
);
239 } else if (psp
!= POWER_SUPPLY_PROP_PRESENT
)
242 case POWER_SUPPLY_PROP_STATUS
:
243 if (battery
->state
& ACPI_BATTERY_STATE_DISCHARGING
)
244 val
->intval
= acpi_battery_handle_discharging(battery
);
245 else if (battery
->state
& ACPI_BATTERY_STATE_CHARGING
)
246 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
247 else if (acpi_battery_is_charged(battery
))
248 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
250 val
->intval
= POWER_SUPPLY_STATUS_UNKNOWN
;
252 case POWER_SUPPLY_PROP_PRESENT
:
253 val
->intval
= acpi_battery_present(battery
);
255 case POWER_SUPPLY_PROP_TECHNOLOGY
:
256 val
->intval
= acpi_battery_technology(battery
);
258 case POWER_SUPPLY_PROP_CYCLE_COUNT
:
259 val
->intval
= battery
->cycle_count
;
261 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
:
262 if (battery
->design_voltage
== ACPI_BATTERY_VALUE_UNKNOWN
)
265 val
->intval
= battery
->design_voltage
* 1000;
267 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
268 if (battery
->voltage_now
== ACPI_BATTERY_VALUE_UNKNOWN
)
271 val
->intval
= battery
->voltage_now
* 1000;
273 case POWER_SUPPLY_PROP_CURRENT_NOW
:
274 case POWER_SUPPLY_PROP_POWER_NOW
:
275 if (battery
->rate_now
== ACPI_BATTERY_VALUE_UNKNOWN
)
278 val
->intval
= battery
->rate_now
* 1000;
280 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
281 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
:
282 if (!ACPI_BATTERY_CAPACITY_VALID(battery
->design_capacity
))
285 val
->intval
= battery
->design_capacity
* 1000;
287 case POWER_SUPPLY_PROP_CHARGE_FULL
:
288 case POWER_SUPPLY_PROP_ENERGY_FULL
:
289 if (!ACPI_BATTERY_CAPACITY_VALID(battery
->full_charge_capacity
))
292 val
->intval
= battery
->full_charge_capacity
* 1000;
294 case POWER_SUPPLY_PROP_CHARGE_NOW
:
295 case POWER_SUPPLY_PROP_ENERGY_NOW
:
296 if (battery
->capacity_now
== ACPI_BATTERY_VALUE_UNKNOWN
)
299 val
->intval
= battery
->capacity_now
* 1000;
301 case POWER_SUPPLY_PROP_CAPACITY
:
302 if (ACPI_BATTERY_CAPACITY_VALID(battery
->full_charge_capacity
))
303 full_capacity
= battery
->full_charge_capacity
;
304 else if (ACPI_BATTERY_CAPACITY_VALID(battery
->design_capacity
))
305 full_capacity
= battery
->design_capacity
;
307 if (battery
->capacity_now
== ACPI_BATTERY_VALUE_UNKNOWN
||
308 full_capacity
== ACPI_BATTERY_VALUE_UNKNOWN
)
311 val
->intval
= battery
->capacity_now
* 100/
314 case POWER_SUPPLY_PROP_CAPACITY_LEVEL
:
315 if (battery
->state
& ACPI_BATTERY_STATE_CRITICAL
)
316 val
->intval
= POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL
;
317 else if (test_bit(ACPI_BATTERY_ALARM_PRESENT
, &battery
->flags
) &&
318 (battery
->capacity_now
<= battery
->alarm
))
319 val
->intval
= POWER_SUPPLY_CAPACITY_LEVEL_LOW
;
320 else if (acpi_battery_is_charged(battery
))
321 val
->intval
= POWER_SUPPLY_CAPACITY_LEVEL_FULL
;
323 val
->intval
= POWER_SUPPLY_CAPACITY_LEVEL_NORMAL
;
325 case POWER_SUPPLY_PROP_MODEL_NAME
:
326 val
->strval
= battery
->model_number
;
328 case POWER_SUPPLY_PROP_MANUFACTURER
:
329 val
->strval
= battery
->oem_info
;
331 case POWER_SUPPLY_PROP_SERIAL_NUMBER
:
332 val
->strval
= battery
->serial_number
;
340 static enum power_supply_property charge_battery_props
[] = {
341 POWER_SUPPLY_PROP_STATUS
,
342 POWER_SUPPLY_PROP_PRESENT
,
343 POWER_SUPPLY_PROP_TECHNOLOGY
,
344 POWER_SUPPLY_PROP_CYCLE_COUNT
,
345 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
,
346 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
347 POWER_SUPPLY_PROP_CURRENT_NOW
,
348 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
349 POWER_SUPPLY_PROP_CHARGE_FULL
,
350 POWER_SUPPLY_PROP_CHARGE_NOW
,
351 POWER_SUPPLY_PROP_CAPACITY
,
352 POWER_SUPPLY_PROP_CAPACITY_LEVEL
,
353 POWER_SUPPLY_PROP_MODEL_NAME
,
354 POWER_SUPPLY_PROP_MANUFACTURER
,
355 POWER_SUPPLY_PROP_SERIAL_NUMBER
,
358 static enum power_supply_property charge_battery_full_cap_broken_props
[] = {
359 POWER_SUPPLY_PROP_STATUS
,
360 POWER_SUPPLY_PROP_PRESENT
,
361 POWER_SUPPLY_PROP_TECHNOLOGY
,
362 POWER_SUPPLY_PROP_CYCLE_COUNT
,
363 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
,
364 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
365 POWER_SUPPLY_PROP_CURRENT_NOW
,
366 POWER_SUPPLY_PROP_CHARGE_NOW
,
367 POWER_SUPPLY_PROP_MODEL_NAME
,
368 POWER_SUPPLY_PROP_MANUFACTURER
,
369 POWER_SUPPLY_PROP_SERIAL_NUMBER
,
372 static enum power_supply_property energy_battery_props
[] = {
373 POWER_SUPPLY_PROP_STATUS
,
374 POWER_SUPPLY_PROP_PRESENT
,
375 POWER_SUPPLY_PROP_TECHNOLOGY
,
376 POWER_SUPPLY_PROP_CYCLE_COUNT
,
377 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
,
378 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
379 POWER_SUPPLY_PROP_POWER_NOW
,
380 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
,
381 POWER_SUPPLY_PROP_ENERGY_FULL
,
382 POWER_SUPPLY_PROP_ENERGY_NOW
,
383 POWER_SUPPLY_PROP_CAPACITY
,
384 POWER_SUPPLY_PROP_CAPACITY_LEVEL
,
385 POWER_SUPPLY_PROP_MODEL_NAME
,
386 POWER_SUPPLY_PROP_MANUFACTURER
,
387 POWER_SUPPLY_PROP_SERIAL_NUMBER
,
390 static enum power_supply_property energy_battery_full_cap_broken_props
[] = {
391 POWER_SUPPLY_PROP_STATUS
,
392 POWER_SUPPLY_PROP_PRESENT
,
393 POWER_SUPPLY_PROP_TECHNOLOGY
,
394 POWER_SUPPLY_PROP_CYCLE_COUNT
,
395 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
,
396 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
397 POWER_SUPPLY_PROP_POWER_NOW
,
398 POWER_SUPPLY_PROP_ENERGY_NOW
,
399 POWER_SUPPLY_PROP_MODEL_NAME
,
400 POWER_SUPPLY_PROP_MANUFACTURER
,
401 POWER_SUPPLY_PROP_SERIAL_NUMBER
,
404 /* --------------------------------------------------------------------------
406 -------------------------------------------------------------------------- */
407 struct acpi_offsets
{
408 size_t offset
; /* offset inside struct acpi_sbs_battery */
409 u8 mode
; /* int or string? */
412 static const struct acpi_offsets state_offsets
[] = {
413 {offsetof(struct acpi_battery
, state
), 0},
414 {offsetof(struct acpi_battery
, rate_now
), 0},
415 {offsetof(struct acpi_battery
, capacity_now
), 0},
416 {offsetof(struct acpi_battery
, voltage_now
), 0},
419 static const struct acpi_offsets info_offsets
[] = {
420 {offsetof(struct acpi_battery
, power_unit
), 0},
421 {offsetof(struct acpi_battery
, design_capacity
), 0},
422 {offsetof(struct acpi_battery
, full_charge_capacity
), 0},
423 {offsetof(struct acpi_battery
, technology
), 0},
424 {offsetof(struct acpi_battery
, design_voltage
), 0},
425 {offsetof(struct acpi_battery
, design_capacity_warning
), 0},
426 {offsetof(struct acpi_battery
, design_capacity_low
), 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},
435 static const struct acpi_offsets extended_info_offsets
[] = {
436 {offsetof(struct acpi_battery
, revision
), 0},
437 {offsetof(struct acpi_battery
, power_unit
), 0},
438 {offsetof(struct acpi_battery
, design_capacity
), 0},
439 {offsetof(struct acpi_battery
, full_charge_capacity
), 0},
440 {offsetof(struct acpi_battery
, technology
), 0},
441 {offsetof(struct acpi_battery
, design_voltage
), 0},
442 {offsetof(struct acpi_battery
, design_capacity_warning
), 0},
443 {offsetof(struct acpi_battery
, design_capacity_low
), 0},
444 {offsetof(struct acpi_battery
, cycle_count
), 0},
445 {offsetof(struct acpi_battery
, measurement_accuracy
), 0},
446 {offsetof(struct acpi_battery
, max_sampling_time
), 0},
447 {offsetof(struct acpi_battery
, min_sampling_time
), 0},
448 {offsetof(struct acpi_battery
, max_averaging_interval
), 0},
449 {offsetof(struct acpi_battery
, min_averaging_interval
), 0},
450 {offsetof(struct acpi_battery
, capacity_granularity_1
), 0},
451 {offsetof(struct acpi_battery
, capacity_granularity_2
), 0},
452 {offsetof(struct acpi_battery
, model_number
), 1},
453 {offsetof(struct acpi_battery
, serial_number
), 1},
454 {offsetof(struct acpi_battery
, type
), 1},
455 {offsetof(struct acpi_battery
, oem_info
), 1},
458 static int extract_package(struct acpi_battery
*battery
,
459 union acpi_object
*package
,
460 const struct acpi_offsets
*offsets
, int num
)
463 union acpi_object
*element
;
464 if (package
->type
!= ACPI_TYPE_PACKAGE
)
466 for (i
= 0; i
< num
; ++i
) {
467 if (package
->package
.count
<= i
)
469 element
= &package
->package
.elements
[i
];
470 if (offsets
[i
].mode
) {
471 u8
*ptr
= (u8
*)battery
+ offsets
[i
].offset
;
472 if (element
->type
== ACPI_TYPE_STRING
||
473 element
->type
== ACPI_TYPE_BUFFER
)
474 strncpy(ptr
, element
->string
.pointer
, 32);
475 else if (element
->type
== ACPI_TYPE_INTEGER
) {
476 strncpy(ptr
, (u8
*)&element
->integer
.value
,
478 ptr
[sizeof(u64
)] = 0;
480 *ptr
= 0; /* don't have value */
482 int *x
= (int *)((u8
*)battery
+ offsets
[i
].offset
);
483 *x
= (element
->type
== ACPI_TYPE_INTEGER
) ?
484 element
->integer
.value
: -1;
490 static int acpi_battery_get_status(struct acpi_battery
*battery
)
492 if (acpi_bus_get_status(battery
->device
)) {
493 ACPI_EXCEPTION((AE_INFO
, AE_ERROR
, "Evaluating _STA"));
500 static int extract_battery_info(const int use_bix
,
501 struct acpi_battery
*battery
,
502 const struct acpi_buffer
*buffer
)
504 int result
= -EFAULT
;
506 if (use_bix
&& battery_bix_broken_package
)
507 result
= extract_package(battery
, buffer
->pointer
,
508 extended_info_offsets
+ 1,
509 ARRAY_SIZE(extended_info_offsets
) - 1);
511 result
= extract_package(battery
, buffer
->pointer
,
512 extended_info_offsets
,
513 ARRAY_SIZE(extended_info_offsets
));
515 result
= extract_package(battery
, buffer
->pointer
,
516 info_offsets
, ARRAY_SIZE(info_offsets
));
517 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY
, &battery
->flags
))
518 battery
->full_charge_capacity
= battery
->design_capacity
;
519 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH
, &battery
->flags
) &&
520 battery
->power_unit
&& battery
->design_voltage
) {
521 battery
->design_capacity
= battery
->design_capacity
*
522 10000 / battery
->design_voltage
;
523 battery
->full_charge_capacity
= battery
->full_charge_capacity
*
524 10000 / battery
->design_voltage
;
525 battery
->design_capacity_warning
=
526 battery
->design_capacity_warning
*
527 10000 / battery
->design_voltage
;
528 /* Curiously, design_capacity_low, unlike the rest of them,
530 /* capacity_granularity_* equal 1 on the systems tested, so
531 it's impossible to tell if they would need an adjustment
532 or not if their values were higher. */
534 if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE
, &battery
->flags
) &&
535 battery
->capacity_now
> battery
->full_charge_capacity
)
536 battery
->capacity_now
= battery
->full_charge_capacity
;
541 static int acpi_battery_get_info(struct acpi_battery
*battery
)
543 const int xinfo
= test_bit(ACPI_BATTERY_XINFO_PRESENT
, &battery
->flags
);
545 int result
= -ENODEV
;
547 if (!acpi_battery_present(battery
))
551 for (use_bix
= xinfo
? 1 : 0; use_bix
>= 0; use_bix
--) {
552 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
553 acpi_status status
= AE_ERROR
;
555 mutex_lock(&battery
->lock
);
556 status
= acpi_evaluate_object(battery
->device
->handle
,
557 use_bix
? "_BIX":"_BIF",
559 mutex_unlock(&battery
->lock
);
561 if (ACPI_FAILURE(status
)) {
562 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating %s",
563 use_bix
? "_BIX":"_BIF"));
565 result
= extract_battery_info(use_bix
,
569 kfree(buffer
.pointer
);
574 if (!result
&& !use_bix
&& xinfo
)
575 pr_warn(FW_BUG
"The _BIX method is broken, using _BIF.\n");
580 static int acpi_battery_get_state(struct acpi_battery
*battery
)
583 acpi_status status
= 0;
584 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
586 if (!acpi_battery_present(battery
))
589 if (battery
->update_time
&&
590 time_before(jiffies
, battery
->update_time
+
591 msecs_to_jiffies(cache_time
)))
594 mutex_lock(&battery
->lock
);
595 status
= acpi_evaluate_object(battery
->device
->handle
, "_BST",
597 mutex_unlock(&battery
->lock
);
599 if (ACPI_FAILURE(status
)) {
600 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _BST"));
604 result
= extract_package(battery
, buffer
.pointer
,
605 state_offsets
, ARRAY_SIZE(state_offsets
));
606 battery
->update_time
= jiffies
;
607 kfree(buffer
.pointer
);
609 /* For buggy DSDTs that report negative 16-bit values for either
610 * charging or discharging current and/or report 0 as 65536
613 if (battery
->power_unit
== ACPI_BATTERY_POWER_UNIT_MA
&&
614 battery
->rate_now
!= ACPI_BATTERY_VALUE_UNKNOWN
&&
615 (s16
)(battery
->rate_now
) < 0) {
616 battery
->rate_now
= abs((s16
)battery
->rate_now
);
617 pr_warn_once(FW_BUG
"battery: (dis)charge rate invalid.\n");
620 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY
, &battery
->flags
)
621 && battery
->capacity_now
>= 0 && battery
->capacity_now
<= 100)
622 battery
->capacity_now
= (battery
->capacity_now
*
623 battery
->full_charge_capacity
) / 100;
624 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH
, &battery
->flags
) &&
625 battery
->power_unit
&& battery
->design_voltage
) {
626 battery
->capacity_now
= battery
->capacity_now
*
627 10000 / battery
->design_voltage
;
629 if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE
, &battery
->flags
) &&
630 battery
->capacity_now
> battery
->full_charge_capacity
)
631 battery
->capacity_now
= battery
->full_charge_capacity
;
636 static int acpi_battery_set_alarm(struct acpi_battery
*battery
)
638 acpi_status status
= 0;
640 if (!acpi_battery_present(battery
) ||
641 !test_bit(ACPI_BATTERY_ALARM_PRESENT
, &battery
->flags
))
644 mutex_lock(&battery
->lock
);
645 status
= acpi_execute_simple_method(battery
->device
->handle
, "_BTP",
647 mutex_unlock(&battery
->lock
);
649 if (ACPI_FAILURE(status
))
652 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Alarm set to %d\n", battery
->alarm
));
656 static int acpi_battery_init_alarm(struct acpi_battery
*battery
)
658 /* See if alarms are supported, and if so, set default */
659 if (!acpi_has_method(battery
->device
->handle
, "_BTP")) {
660 clear_bit(ACPI_BATTERY_ALARM_PRESENT
, &battery
->flags
);
663 set_bit(ACPI_BATTERY_ALARM_PRESENT
, &battery
->flags
);
665 battery
->alarm
= battery
->design_capacity_warning
;
666 return acpi_battery_set_alarm(battery
);
669 static ssize_t
acpi_battery_alarm_show(struct device
*dev
,
670 struct device_attribute
*attr
,
673 struct acpi_battery
*battery
= to_acpi_battery(dev_get_drvdata(dev
));
674 return sprintf(buf
, "%d\n", battery
->alarm
* 1000);
677 static ssize_t
acpi_battery_alarm_store(struct device
*dev
,
678 struct device_attribute
*attr
,
679 const char *buf
, size_t count
)
682 struct acpi_battery
*battery
= to_acpi_battery(dev_get_drvdata(dev
));
683 if (sscanf(buf
, "%lu\n", &x
) == 1)
684 battery
->alarm
= x
/1000;
685 if (acpi_battery_present(battery
))
686 acpi_battery_set_alarm(battery
);
690 static const struct device_attribute alarm_attr
= {
691 .attr
= {.name
= "alarm", .mode
= 0644},
692 .show
= acpi_battery_alarm_show
,
693 .store
= acpi_battery_alarm_store
,
697 * The Battery Hooking API
699 * This API is used inside other drivers that need to expose
700 * platform-specific behaviour within the generic driver in a
705 static LIST_HEAD(acpi_battery_list
);
706 static LIST_HEAD(battery_hook_list
);
707 static DEFINE_MUTEX(hook_mutex
);
709 static void __battery_hook_unregister(struct acpi_battery_hook
*hook
, int lock
)
711 struct acpi_battery
*battery
;
713 * In order to remove a hook, we first need to
714 * de-register all the batteries that are registered.
717 mutex_lock(&hook_mutex
);
718 list_for_each_entry(battery
, &acpi_battery_list
, list
) {
719 hook
->remove_battery(battery
->bat
);
721 list_del(&hook
->list
);
723 mutex_unlock(&hook_mutex
);
724 pr_info("extension unregistered: %s\n", hook
->name
);
727 void battery_hook_unregister(struct acpi_battery_hook
*hook
)
729 __battery_hook_unregister(hook
, 1);
731 EXPORT_SYMBOL_GPL(battery_hook_unregister
);
733 void battery_hook_register(struct acpi_battery_hook
*hook
)
735 struct acpi_battery
*battery
;
737 mutex_lock(&hook_mutex
);
738 INIT_LIST_HEAD(&hook
->list
);
739 list_add(&hook
->list
, &battery_hook_list
);
741 * Now that the driver is registered, we need
742 * to notify the hook that a battery is available
743 * for each battery, so that the driver may add
746 list_for_each_entry(battery
, &acpi_battery_list
, list
) {
747 if (hook
->add_battery(battery
->bat
)) {
749 * If a add-battery returns non-zero,
750 * the registration of the extension has failed,
751 * and we will not add it to the list of loaded
754 pr_err("extension failed to load: %s", hook
->name
);
755 __battery_hook_unregister(hook
, 0);
759 pr_info("new extension: %s\n", hook
->name
);
761 mutex_unlock(&hook_mutex
);
763 EXPORT_SYMBOL_GPL(battery_hook_register
);
766 * This function gets called right after the battery sysfs
767 * attributes have been added, so that the drivers that
768 * define custom sysfs attributes can add their own.
770 static void battery_hook_add_battery(struct acpi_battery
*battery
)
772 struct acpi_battery_hook
*hook_node
, *tmp
;
774 mutex_lock(&hook_mutex
);
775 INIT_LIST_HEAD(&battery
->list
);
776 list_add(&battery
->list
, &acpi_battery_list
);
778 * Since we added a new battery to the list, we need to
779 * iterate over the hooks and call add_battery for each
780 * hook that was registered. This usually happens
781 * when a battery gets hotplugged or initialized
782 * during the battery module initialization.
784 list_for_each_entry_safe(hook_node
, tmp
, &battery_hook_list
, list
) {
785 if (hook_node
->add_battery(battery
->bat
)) {
787 * The notification of the extensions has failed, to
788 * prevent further errors we will unload the extension.
790 pr_err("error in extension, unloading: %s",
792 __battery_hook_unregister(hook_node
, 0);
795 mutex_unlock(&hook_mutex
);
798 static void battery_hook_remove_battery(struct acpi_battery
*battery
)
800 struct acpi_battery_hook
*hook
;
802 mutex_lock(&hook_mutex
);
804 * Before removing the hook, we need to remove all
805 * custom attributes from the battery.
807 list_for_each_entry(hook
, &battery_hook_list
, list
) {
808 hook
->remove_battery(battery
->bat
);
810 /* Then, just remove the battery from the list */
811 list_del(&battery
->list
);
812 mutex_unlock(&hook_mutex
);
815 static void __exit
battery_hook_exit(void)
817 struct acpi_battery_hook
*hook
;
818 struct acpi_battery_hook
*ptr
;
820 * At this point, the acpi_bus_unregister_driver()
821 * has called remove for all batteries. We just
822 * need to remove the hooks.
824 list_for_each_entry_safe(hook
, ptr
, &battery_hook_list
, list
) {
825 __battery_hook_unregister(hook
, 1);
827 mutex_destroy(&hook_mutex
);
830 static int sysfs_add_battery(struct acpi_battery
*battery
)
832 struct power_supply_config psy_cfg
= { .drv_data
= battery
, };
833 bool full_cap_broken
= false;
835 if (!ACPI_BATTERY_CAPACITY_VALID(battery
->full_charge_capacity
) &&
836 !ACPI_BATTERY_CAPACITY_VALID(battery
->design_capacity
))
837 full_cap_broken
= true;
839 if (battery
->power_unit
== ACPI_BATTERY_POWER_UNIT_MA
) {
840 if (full_cap_broken
) {
841 battery
->bat_desc
.properties
=
842 charge_battery_full_cap_broken_props
;
843 battery
->bat_desc
.num_properties
=
844 ARRAY_SIZE(charge_battery_full_cap_broken_props
);
846 battery
->bat_desc
.properties
= charge_battery_props
;
847 battery
->bat_desc
.num_properties
=
848 ARRAY_SIZE(charge_battery_props
);
851 if (full_cap_broken
) {
852 battery
->bat_desc
.properties
=
853 energy_battery_full_cap_broken_props
;
854 battery
->bat_desc
.num_properties
=
855 ARRAY_SIZE(energy_battery_full_cap_broken_props
);
857 battery
->bat_desc
.properties
= energy_battery_props
;
858 battery
->bat_desc
.num_properties
=
859 ARRAY_SIZE(energy_battery_props
);
863 battery
->bat_desc
.name
= acpi_device_bid(battery
->device
);
864 battery
->bat_desc
.type
= POWER_SUPPLY_TYPE_BATTERY
;
865 battery
->bat_desc
.get_property
= acpi_battery_get_property
;
867 battery
->bat
= power_supply_register_no_ws(&battery
->device
->dev
,
868 &battery
->bat_desc
, &psy_cfg
);
870 if (IS_ERR(battery
->bat
)) {
871 int result
= PTR_ERR(battery
->bat
);
876 battery_hook_add_battery(battery
);
877 return device_create_file(&battery
->bat
->dev
, &alarm_attr
);
880 static void sysfs_remove_battery(struct acpi_battery
*battery
)
882 mutex_lock(&battery
->sysfs_lock
);
884 mutex_unlock(&battery
->sysfs_lock
);
887 battery_hook_remove_battery(battery
);
888 device_remove_file(&battery
->bat
->dev
, &alarm_attr
);
889 power_supply_unregister(battery
->bat
);
891 mutex_unlock(&battery
->sysfs_lock
);
894 static void find_battery(const struct dmi_header
*dm
, void *private)
896 struct acpi_battery
*battery
= (struct acpi_battery
*)private;
897 /* Note: the hardcoded offsets below have been extracted from
898 the source code of dmidecode. */
899 if (dm
->type
== DMI_ENTRY_PORTABLE_BATTERY
&& dm
->length
>= 8) {
900 const u8
*dmi_data
= (const u8
*)(dm
+ 1);
901 int dmi_capacity
= get_unaligned((const u16
*)(dmi_data
+ 6));
902 if (dm
->length
>= 18)
903 dmi_capacity
*= dmi_data
[17];
904 if (battery
->design_capacity
* battery
->design_voltage
/ 1000
906 battery
->design_capacity
* 10 == dmi_capacity
)
907 set_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH
,
913 * According to the ACPI spec, some kinds of primary batteries can
914 * report percentage battery remaining capacity directly to OS.
915 * In this case, it reports the Last Full Charged Capacity == 100
916 * and BatteryPresentRate == 0xFFFFFFFF.
918 * Now we found some battery reports percentage remaining capacity
919 * even if it's rechargeable.
920 * https://bugzilla.kernel.org/show_bug.cgi?id=15979
922 * Handle this correctly so that they won't break userspace.
924 static void acpi_battery_quirks(struct acpi_battery
*battery
)
926 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY
, &battery
->flags
))
929 if (battery
->full_charge_capacity
== 100 &&
930 battery
->rate_now
== ACPI_BATTERY_VALUE_UNKNOWN
&&
931 battery
->capacity_now
>= 0 && battery
->capacity_now
<= 100) {
932 set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY
, &battery
->flags
);
933 battery
->full_charge_capacity
= battery
->design_capacity
;
934 battery
->capacity_now
= (battery
->capacity_now
*
935 battery
->full_charge_capacity
) / 100;
938 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH
, &battery
->flags
))
941 if (battery
->power_unit
&& dmi_name_in_vendors("LENOVO")) {
943 s
= dmi_get_system_info(DMI_PRODUCT_VERSION
);
944 if (s
&& !strncasecmp(s
, "ThinkPad", 8)) {
945 dmi_walk(find_battery
, battery
);
946 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH
,
948 battery
->design_voltage
) {
949 battery
->design_capacity
=
950 battery
->design_capacity
*
951 10000 / battery
->design_voltage
;
952 battery
->full_charge_capacity
=
953 battery
->full_charge_capacity
*
954 10000 / battery
->design_voltage
;
955 battery
->design_capacity_warning
=
956 battery
->design_capacity_warning
*
957 10000 / battery
->design_voltage
;
958 battery
->capacity_now
= battery
->capacity_now
*
959 10000 / battery
->design_voltage
;
964 if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE
, &battery
->flags
))
967 if (acpi_battery_is_degraded(battery
) &&
968 battery
->capacity_now
> battery
->full_charge_capacity
) {
969 set_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE
, &battery
->flags
);
970 battery
->capacity_now
= battery
->full_charge_capacity
;
974 static int acpi_battery_update(struct acpi_battery
*battery
, bool resume
)
976 int result
= acpi_battery_get_status(battery
);
981 if (!acpi_battery_present(battery
)) {
982 sysfs_remove_battery(battery
);
983 battery
->update_time
= 0;
990 if (!battery
->update_time
) {
991 result
= acpi_battery_get_info(battery
);
994 acpi_battery_init_alarm(battery
);
997 result
= acpi_battery_get_state(battery
);
1000 acpi_battery_quirks(battery
);
1002 if (!battery
->bat
) {
1003 result
= sysfs_add_battery(battery
);
1009 * Wakeup the system if battery is critical low
1010 * or lower than the alarm level
1012 if ((battery
->state
& ACPI_BATTERY_STATE_CRITICAL
) ||
1013 (test_bit(ACPI_BATTERY_ALARM_PRESENT
, &battery
->flags
) &&
1014 (battery
->capacity_now
<= battery
->alarm
)))
1015 acpi_pm_wakeup_event(&battery
->device
->dev
);
1020 static void acpi_battery_refresh(struct acpi_battery
*battery
)
1027 power_unit
= battery
->power_unit
;
1029 acpi_battery_get_info(battery
);
1031 if (power_unit
== battery
->power_unit
)
1034 /* The battery has changed its reporting units. */
1035 sysfs_remove_battery(battery
);
1036 sysfs_add_battery(battery
);
1039 /* --------------------------------------------------------------------------
1040 FS Interface (/proc)
1041 -------------------------------------------------------------------------- */
1043 #ifdef CONFIG_ACPI_PROCFS_POWER
1044 static struct proc_dir_entry
*acpi_battery_dir
;
1046 static const char *acpi_battery_units(const struct acpi_battery
*battery
)
1048 return (battery
->power_unit
== ACPI_BATTERY_POWER_UNIT_MA
) ?
1052 static int acpi_battery_info_proc_show(struct seq_file
*seq
, void *offset
)
1054 struct acpi_battery
*battery
= seq
->private;
1055 int result
= acpi_battery_update(battery
, false);
1060 seq_printf(seq
, "present: %s\n",
1061 acpi_battery_present(battery
) ? "yes" : "no");
1062 if (!acpi_battery_present(battery
))
1064 if (battery
->design_capacity
== ACPI_BATTERY_VALUE_UNKNOWN
)
1065 seq_printf(seq
, "design capacity: unknown\n");
1067 seq_printf(seq
, "design capacity: %d %sh\n",
1068 battery
->design_capacity
,
1069 acpi_battery_units(battery
));
1071 if (battery
->full_charge_capacity
== ACPI_BATTERY_VALUE_UNKNOWN
)
1072 seq_printf(seq
, "last full capacity: unknown\n");
1074 seq_printf(seq
, "last full capacity: %d %sh\n",
1075 battery
->full_charge_capacity
,
1076 acpi_battery_units(battery
));
1078 seq_printf(seq
, "battery technology: %srechargeable\n",
1079 battery
->technology
? "" : "non-");
1081 if (battery
->design_voltage
== ACPI_BATTERY_VALUE_UNKNOWN
)
1082 seq_printf(seq
, "design voltage: unknown\n");
1084 seq_printf(seq
, "design voltage: %d mV\n",
1085 battery
->design_voltage
);
1086 seq_printf(seq
, "design capacity warning: %d %sh\n",
1087 battery
->design_capacity_warning
,
1088 acpi_battery_units(battery
));
1089 seq_printf(seq
, "design capacity low: %d %sh\n",
1090 battery
->design_capacity_low
,
1091 acpi_battery_units(battery
));
1092 seq_printf(seq
, "cycle count: %i\n", battery
->cycle_count
);
1093 seq_printf(seq
, "capacity granularity 1: %d %sh\n",
1094 battery
->capacity_granularity_1
,
1095 acpi_battery_units(battery
));
1096 seq_printf(seq
, "capacity granularity 2: %d %sh\n",
1097 battery
->capacity_granularity_2
,
1098 acpi_battery_units(battery
));
1099 seq_printf(seq
, "model number: %s\n", battery
->model_number
);
1100 seq_printf(seq
, "serial number: %s\n", battery
->serial_number
);
1101 seq_printf(seq
, "battery type: %s\n", battery
->type
);
1102 seq_printf(seq
, "OEM info: %s\n", battery
->oem_info
);
1105 seq_printf(seq
, "ERROR: Unable to read battery info\n");
1109 static int acpi_battery_state_proc_show(struct seq_file
*seq
, void *offset
)
1111 struct acpi_battery
*battery
= seq
->private;
1112 int result
= acpi_battery_update(battery
, false);
1117 seq_printf(seq
, "present: %s\n",
1118 acpi_battery_present(battery
) ? "yes" : "no");
1119 if (!acpi_battery_present(battery
))
1122 seq_printf(seq
, "capacity state: %s\n",
1123 (battery
->state
& 0x04) ? "critical" : "ok");
1124 if ((battery
->state
& 0x01) && (battery
->state
& 0x02))
1126 "charging state: charging/discharging\n");
1127 else if (battery
->state
& 0x01)
1128 seq_printf(seq
, "charging state: discharging\n");
1129 else if (battery
->state
& 0x02)
1130 seq_printf(seq
, "charging state: charging\n");
1132 seq_printf(seq
, "charging state: charged\n");
1134 if (battery
->rate_now
== ACPI_BATTERY_VALUE_UNKNOWN
)
1135 seq_printf(seq
, "present rate: unknown\n");
1137 seq_printf(seq
, "present rate: %d %s\n",
1138 battery
->rate_now
, acpi_battery_units(battery
));
1140 if (battery
->capacity_now
== ACPI_BATTERY_VALUE_UNKNOWN
)
1141 seq_printf(seq
, "remaining capacity: unknown\n");
1143 seq_printf(seq
, "remaining capacity: %d %sh\n",
1144 battery
->capacity_now
, acpi_battery_units(battery
));
1145 if (battery
->voltage_now
== ACPI_BATTERY_VALUE_UNKNOWN
)
1146 seq_printf(seq
, "present voltage: unknown\n");
1148 seq_printf(seq
, "present voltage: %d mV\n",
1149 battery
->voltage_now
);
1152 seq_printf(seq
, "ERROR: Unable to read battery state\n");
1157 static int acpi_battery_alarm_proc_show(struct seq_file
*seq
, void *offset
)
1159 struct acpi_battery
*battery
= seq
->private;
1160 int result
= acpi_battery_update(battery
, false);
1165 if (!acpi_battery_present(battery
)) {
1166 seq_printf(seq
, "present: no\n");
1169 seq_printf(seq
, "alarm: ");
1170 if (battery
->alarm
) {
1171 seq_printf(seq
, "%u %sh\n", battery
->alarm
,
1172 acpi_battery_units(battery
));
1174 seq_printf(seq
, "unsupported\n");
1178 seq_printf(seq
, "ERROR: Unable to read battery alarm\n");
1182 static ssize_t
acpi_battery_write_alarm(struct file
*file
,
1183 const char __user
* buffer
,
1184 size_t count
, loff_t
* ppos
)
1187 char alarm_string
[12] = { '\0' };
1188 struct seq_file
*m
= file
->private_data
;
1189 struct acpi_battery
*battery
= m
->private;
1191 if (!battery
|| (count
> sizeof(alarm_string
) - 1))
1193 if (!acpi_battery_present(battery
)) {
1197 if (copy_from_user(alarm_string
, buffer
, count
)) {
1201 alarm_string
[count
] = '\0';
1202 if (kstrtoint(alarm_string
, 0, &battery
->alarm
)) {
1206 result
= acpi_battery_set_alarm(battery
);
1213 static int acpi_battery_alarm_proc_open(struct inode
*inode
, struct file
*file
)
1215 return single_open(file
, acpi_battery_alarm_proc_show
, PDE_DATA(inode
));
1218 static const struct file_operations acpi_battery_alarm_fops
= {
1219 .owner
= THIS_MODULE
,
1220 .open
= acpi_battery_alarm_proc_open
,
1222 .write
= acpi_battery_write_alarm
,
1223 .llseek
= seq_lseek
,
1224 .release
= single_release
,
1227 static int acpi_battery_add_fs(struct acpi_device
*device
)
1229 pr_warning(PREFIX
"Deprecated procfs I/F for battery is loaded, please retry with CONFIG_ACPI_PROCFS_POWER cleared\n");
1230 if (!acpi_device_dir(device
)) {
1231 acpi_device_dir(device
) = proc_mkdir(acpi_device_bid(device
),
1233 if (!acpi_device_dir(device
))
1237 if (!proc_create_single_data("info", S_IRUGO
, acpi_device_dir(device
),
1238 acpi_battery_info_proc_show
, acpi_driver_data(device
)))
1240 if (!proc_create_single_data("state", S_IRUGO
, acpi_device_dir(device
),
1241 acpi_battery_state_proc_show
, acpi_driver_data(device
)))
1243 if (!proc_create_data("alarm", S_IFREG
| S_IRUGO
| S_IWUSR
,
1244 acpi_device_dir(device
), &acpi_battery_alarm_fops
,
1245 acpi_driver_data(device
)))
1250 static void acpi_battery_remove_fs(struct acpi_device
*device
)
1252 if (!acpi_device_dir(device
))
1254 remove_proc_subtree(acpi_device_bid(device
), acpi_battery_dir
);
1255 acpi_device_dir(device
) = NULL
;
1260 /* --------------------------------------------------------------------------
1262 -------------------------------------------------------------------------- */
1264 static void acpi_battery_notify(struct acpi_device
*device
, u32 event
)
1266 struct acpi_battery
*battery
= acpi_driver_data(device
);
1267 struct power_supply
*old
;
1273 * On Acer Aspire V5-573G notifications are sometimes triggered too
1274 * early. For example, when AC is unplugged and notification is
1275 * triggered, battery state is still reported as "Full", and changes to
1276 * "Discharging" only after short delay, without any notification.
1278 if (battery_notification_delay_ms
> 0)
1279 msleep(battery_notification_delay_ms
);
1280 if (event
== ACPI_BATTERY_NOTIFY_INFO
)
1281 acpi_battery_refresh(battery
);
1282 acpi_battery_update(battery
, false);
1283 acpi_bus_generate_netlink_event(device
->pnp
.device_class
,
1284 dev_name(&device
->dev
), event
,
1285 acpi_battery_present(battery
));
1286 acpi_notifier_call_chain(device
, event
, acpi_battery_present(battery
));
1287 /* acpi_battery_update could remove power_supply object */
1288 if (old
&& battery
->bat
)
1289 power_supply_changed(battery
->bat
);
1292 static int battery_notify(struct notifier_block
*nb
,
1293 unsigned long mode
, void *_unused
)
1295 struct acpi_battery
*battery
= container_of(nb
, struct acpi_battery
,
1300 case PM_POST_HIBERNATION
:
1301 case PM_POST_SUSPEND
:
1302 if (!acpi_battery_present(battery
))
1306 acpi_battery_refresh(battery
);
1308 result
= acpi_battery_get_info(battery
);
1312 result
= sysfs_add_battery(battery
);
1317 acpi_battery_init_alarm(battery
);
1318 acpi_battery_get_state(battery
);
1326 battery_bix_broken_package_quirk(const struct dmi_system_id
*d
)
1328 battery_bix_broken_package
= 1;
1333 battery_notification_delay_quirk(const struct dmi_system_id
*d
)
1335 battery_notification_delay_ms
= 1000;
1340 battery_ac_is_broken_quirk(const struct dmi_system_id
*d
)
1342 battery_ac_is_broken
= 1;
1347 battery_do_not_check_pmic_quirk(const struct dmi_system_id
*d
)
1349 battery_check_pmic
= 0;
1353 static const struct dmi_system_id bat_dmi_table
[] __initconst
= {
1356 .callback
= battery_bix_broken_package_quirk
,
1358 DMI_MATCH(DMI_SYS_VENDOR
, "NEC"),
1359 DMI_MATCH(DMI_PRODUCT_NAME
, "PC-LZ750LS"),
1363 /* Acer Aspire V5-573G */
1364 .callback
= battery_notification_delay_quirk
,
1366 DMI_MATCH(DMI_SYS_VENDOR
, "Acer"),
1367 DMI_MATCH(DMI_PRODUCT_NAME
, "Aspire V5-573G"),
1371 /* Point of View mobii wintab p800w */
1372 .callback
= battery_ac_is_broken_quirk
,
1374 DMI_MATCH(DMI_BOARD_VENDOR
, "AMI Corporation"),
1375 DMI_MATCH(DMI_BOARD_NAME
, "Aptio CRB"),
1376 DMI_MATCH(DMI_BIOS_VERSION
, "3BAIR1013"),
1377 /* Above matches are too generic, add bios-date match */
1378 DMI_MATCH(DMI_BIOS_DATE
, "08/22/2014"),
1383 .callback
= battery_do_not_check_pmic_quirk
,
1385 DMI_MATCH(DMI_PRODUCT_NAME
, "EF20EA"),
1389 /* Lenovo Ideapad Miix 320 */
1390 .callback
= battery_do_not_check_pmic_quirk
,
1392 DMI_EXACT_MATCH(DMI_SYS_VENDOR
, "LENOVO"),
1393 DMI_EXACT_MATCH(DMI_PRODUCT_NAME
, "80XF"),
1394 DMI_EXACT_MATCH(DMI_PRODUCT_VERSION
, "Lenovo MIIX 320-10ICR"),
1401 * Some machines'(E,G Lenovo Z480) ECs are not stable
1402 * during boot up and this causes battery driver fails to be
1403 * probed due to failure of getting battery information
1404 * from EC sometimes. After several retries, the operation
1405 * may work. So add retry code here and 20ms sleep between
1408 static int acpi_battery_update_retry(struct acpi_battery
*battery
)
1412 for (retry
= 5; retry
; retry
--) {
1413 ret
= acpi_battery_update(battery
, false);
1422 static int acpi_battery_add(struct acpi_device
*device
)
1425 struct acpi_battery
*battery
= NULL
;
1430 if (device
->dep_unmet
)
1431 return -EPROBE_DEFER
;
1433 battery
= kzalloc(sizeof(struct acpi_battery
), GFP_KERNEL
);
1436 battery
->device
= device
;
1437 strcpy(acpi_device_name(device
), ACPI_BATTERY_DEVICE_NAME
);
1438 strcpy(acpi_device_class(device
), ACPI_BATTERY_CLASS
);
1439 device
->driver_data
= battery
;
1440 mutex_init(&battery
->lock
);
1441 mutex_init(&battery
->sysfs_lock
);
1442 if (acpi_has_method(battery
->device
->handle
, "_BIX"))
1443 set_bit(ACPI_BATTERY_XINFO_PRESENT
, &battery
->flags
);
1445 result
= acpi_battery_update_retry(battery
);
1449 #ifdef CONFIG_ACPI_PROCFS_POWER
1450 result
= acpi_battery_add_fs(device
);
1452 acpi_battery_remove_fs(device
);
1457 pr_info(PREFIX
"%s Slot [%s] (battery %s)\n",
1458 ACPI_BATTERY_DEVICE_NAME
, acpi_device_bid(device
),
1459 device
->status
.battery_present
? "present" : "absent");
1461 battery
->pm_nb
.notifier_call
= battery_notify
;
1462 register_pm_notifier(&battery
->pm_nb
);
1464 device_init_wakeup(&device
->dev
, 1);
1469 sysfs_remove_battery(battery
);
1470 mutex_destroy(&battery
->lock
);
1471 mutex_destroy(&battery
->sysfs_lock
);
1476 static int acpi_battery_remove(struct acpi_device
*device
)
1478 struct acpi_battery
*battery
= NULL
;
1480 if (!device
|| !acpi_driver_data(device
))
1482 device_init_wakeup(&device
->dev
, 0);
1483 battery
= acpi_driver_data(device
);
1484 unregister_pm_notifier(&battery
->pm_nb
);
1485 #ifdef CONFIG_ACPI_PROCFS_POWER
1486 acpi_battery_remove_fs(device
);
1488 sysfs_remove_battery(battery
);
1489 mutex_destroy(&battery
->lock
);
1490 mutex_destroy(&battery
->sysfs_lock
);
1495 #ifdef CONFIG_PM_SLEEP
1496 /* this is needed to learn about changes made in suspended state */
1497 static int acpi_battery_resume(struct device
*dev
)
1499 struct acpi_battery
*battery
;
1504 battery
= acpi_driver_data(to_acpi_device(dev
));
1508 battery
->update_time
= 0;
1509 acpi_battery_update(battery
, true);
1513 #define acpi_battery_resume NULL
1516 static SIMPLE_DEV_PM_OPS(acpi_battery_pm
, NULL
, acpi_battery_resume
);
1518 static struct acpi_driver acpi_battery_driver
= {
1520 .class = ACPI_BATTERY_CLASS
,
1521 .ids
= battery_device_ids
,
1522 .flags
= ACPI_DRIVER_ALL_NOTIFY_EVENTS
,
1524 .add
= acpi_battery_add
,
1525 .remove
= acpi_battery_remove
,
1526 .notify
= acpi_battery_notify
,
1528 .drv
.pm
= &acpi_battery_pm
,
1531 static void __init
acpi_battery_init_async(void *unused
, async_cookie_t cookie
)
1536 dmi_check_system(bat_dmi_table
);
1538 if (battery_check_pmic
) {
1539 for (i
= 0; i
< ARRAY_SIZE(acpi_battery_blacklist
); i
++)
1540 if (acpi_dev_present(acpi_battery_blacklist
[i
], "1", -1)) {
1541 pr_info(PREFIX ACPI_BATTERY_DEVICE_NAME
1542 ": found native %s PMIC, not loading\n",
1543 acpi_battery_blacklist
[i
]);
1548 #ifdef CONFIG_ACPI_PROCFS_POWER
1549 acpi_battery_dir
= acpi_lock_battery_dir();
1550 if (!acpi_battery_dir
)
1553 result
= acpi_bus_register_driver(&acpi_battery_driver
);
1554 #ifdef CONFIG_ACPI_PROCFS_POWER
1556 acpi_unlock_battery_dir(acpi_battery_dir
);
1558 battery_driver_registered
= (result
== 0);
1561 static int __init
acpi_battery_init(void)
1566 async_cookie
= async_schedule(acpi_battery_init_async
, NULL
);
1570 static void __exit
acpi_battery_exit(void)
1572 async_synchronize_cookie(async_cookie
+ 1);
1573 if (battery_driver_registered
) {
1574 acpi_bus_unregister_driver(&acpi_battery_driver
);
1575 battery_hook_exit();
1577 #ifdef CONFIG_ACPI_PROCFS_POWER
1578 if (acpi_battery_dir
)
1579 acpi_unlock_battery_dir(acpi_battery_dir
);
1583 module_init(acpi_battery_init
);
1584 module_exit(acpi_battery_exit
);