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 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/types.h>
28 #include <linux/jiffies.h>
29 #include <linux/async.h>
30 #include <linux/dmi.h>
31 #include <linux/delay.h>
32 #include <linux/slab.h>
33 #include <linux/suspend.h>
34 #include <asm/unaligned.h>
36 #ifdef CONFIG_ACPI_PROCFS_POWER
37 #include <linux/proc_fs.h>
38 #include <linux/seq_file.h>
39 #include <linux/uaccess.h>
42 #include <linux/acpi.h>
43 #include <linux/power_supply.h>
47 #define PREFIX "ACPI: "
49 #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
51 #define ACPI_BATTERY_DEVICE_NAME "Battery"
53 /* Battery power unit: 0 means mW, 1 means mA */
54 #define ACPI_BATTERY_POWER_UNIT_MA 1
56 #define ACPI_BATTERY_STATE_DISCHARGING 0x1
57 #define ACPI_BATTERY_STATE_CHARGING 0x2
58 #define ACPI_BATTERY_STATE_CRITICAL 0x4
60 #define _COMPONENT ACPI_BATTERY_COMPONENT
62 ACPI_MODULE_NAME("battery");
64 MODULE_AUTHOR("Paul Diefenbaugh");
65 MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
66 MODULE_DESCRIPTION("ACPI Battery Driver");
67 MODULE_LICENSE("GPL");
69 static async_cookie_t async_cookie
;
70 static bool battery_driver_registered
;
71 static int battery_bix_broken_package
;
72 static int battery_notification_delay_ms
;
73 static unsigned int cache_time
= 1000;
74 module_param(cache_time
, uint
, 0644);
75 MODULE_PARM_DESC(cache_time
, "cache time in milliseconds");
77 #ifdef CONFIG_ACPI_PROCFS_POWER
78 extern struct proc_dir_entry
*acpi_lock_battery_dir(void);
79 extern void *acpi_unlock_battery_dir(struct proc_dir_entry
*acpi_battery_dir
);
81 enum acpi_battery_files
{
85 ACPI_BATTERY_NUMFILES
,
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
,
120 struct acpi_battery
{
122 struct mutex sysfs_lock
;
123 struct power_supply
*bat
;
124 struct power_supply_desc bat_desc
;
125 struct acpi_device
*device
;
126 struct notifier_block pm_nb
;
127 unsigned long update_time
;
133 int full_charge_capacity
;
136 int design_capacity_warning
;
137 int design_capacity_low
;
139 int measurement_accuracy
;
140 int max_sampling_time
;
141 int min_sampling_time
;
142 int max_averaging_interval
;
143 int min_averaging_interval
;
144 int capacity_granularity_1
;
145 int capacity_granularity_2
;
147 char model_number
[32];
148 char serial_number
[32];
156 #define to_acpi_battery(x) power_supply_get_drvdata(x)
158 static inline int acpi_battery_present(struct acpi_battery
*battery
)
160 return battery
->device
->status
.battery_present
;
163 static int acpi_battery_technology(struct acpi_battery
*battery
)
165 if (!strcasecmp("NiCd", battery
->type
))
166 return POWER_SUPPLY_TECHNOLOGY_NiCd
;
167 if (!strcasecmp("NiMH", battery
->type
))
168 return POWER_SUPPLY_TECHNOLOGY_NiMH
;
169 if (!strcasecmp("LION", battery
->type
))
170 return POWER_SUPPLY_TECHNOLOGY_LION
;
171 if (!strncasecmp("LI-ION", battery
->type
, 6))
172 return POWER_SUPPLY_TECHNOLOGY_LION
;
173 if (!strcasecmp("LiP", battery
->type
))
174 return POWER_SUPPLY_TECHNOLOGY_LIPO
;
175 return POWER_SUPPLY_TECHNOLOGY_UNKNOWN
;
178 static int acpi_battery_get_state(struct acpi_battery
*battery
);
180 static int acpi_battery_is_charged(struct acpi_battery
*battery
)
182 /* charging, discharging or critical low */
183 if (battery
->state
!= 0)
186 /* battery not reporting charge */
187 if (battery
->capacity_now
== ACPI_BATTERY_VALUE_UNKNOWN
||
188 battery
->capacity_now
== 0)
191 /* good batteries update full_charge as the batteries degrade */
192 if (battery
->full_charge_capacity
== battery
->capacity_now
)
195 /* fallback to using design values for broken batteries */
196 if (battery
->design_capacity
== battery
->capacity_now
)
199 /* we don't do any sort of metric based on percentages */
203 static int acpi_battery_handle_discharging(struct acpi_battery
*battery
)
206 * Some devices wrongly report discharging if the battery's charge level
207 * was above the device's start charging threshold atm the AC adapter
208 * was plugged in and the device thus did not start a new charge cycle.
210 if (power_supply_is_system_supplied() && battery
->rate_now
== 0)
211 return POWER_SUPPLY_STATUS_NOT_CHARGING
;
213 return POWER_SUPPLY_STATUS_DISCHARGING
;
216 static int acpi_battery_get_property(struct power_supply
*psy
,
217 enum power_supply_property psp
,
218 union power_supply_propval
*val
)
221 struct acpi_battery
*battery
= to_acpi_battery(psy
);
223 if (acpi_battery_present(battery
)) {
224 /* run battery update only if it is present */
225 acpi_battery_get_state(battery
);
226 } else if (psp
!= POWER_SUPPLY_PROP_PRESENT
)
229 case POWER_SUPPLY_PROP_STATUS
:
230 if (battery
->state
& ACPI_BATTERY_STATE_DISCHARGING
)
231 val
->intval
= acpi_battery_handle_discharging(battery
);
232 else if (battery
->state
& ACPI_BATTERY_STATE_CHARGING
)
233 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
234 else if (acpi_battery_is_charged(battery
))
235 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
237 val
->intval
= POWER_SUPPLY_STATUS_UNKNOWN
;
239 case POWER_SUPPLY_PROP_PRESENT
:
240 val
->intval
= acpi_battery_present(battery
);
242 case POWER_SUPPLY_PROP_TECHNOLOGY
:
243 val
->intval
= acpi_battery_technology(battery
);
245 case POWER_SUPPLY_PROP_CYCLE_COUNT
:
246 val
->intval
= battery
->cycle_count
;
248 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
:
249 if (battery
->design_voltage
== ACPI_BATTERY_VALUE_UNKNOWN
)
252 val
->intval
= battery
->design_voltage
* 1000;
254 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
255 if (battery
->voltage_now
== ACPI_BATTERY_VALUE_UNKNOWN
)
258 val
->intval
= battery
->voltage_now
* 1000;
260 case POWER_SUPPLY_PROP_CURRENT_NOW
:
261 case POWER_SUPPLY_PROP_POWER_NOW
:
262 if (battery
->rate_now
== ACPI_BATTERY_VALUE_UNKNOWN
)
265 val
->intval
= battery
->rate_now
* 1000;
267 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
268 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
:
269 if (battery
->design_capacity
== ACPI_BATTERY_VALUE_UNKNOWN
)
272 val
->intval
= battery
->design_capacity
* 1000;
274 case POWER_SUPPLY_PROP_CHARGE_FULL
:
275 case POWER_SUPPLY_PROP_ENERGY_FULL
:
276 if (battery
->full_charge_capacity
== ACPI_BATTERY_VALUE_UNKNOWN
)
279 val
->intval
= battery
->full_charge_capacity
* 1000;
281 case POWER_SUPPLY_PROP_CHARGE_NOW
:
282 case POWER_SUPPLY_PROP_ENERGY_NOW
:
283 if (battery
->capacity_now
== ACPI_BATTERY_VALUE_UNKNOWN
)
286 val
->intval
= battery
->capacity_now
* 1000;
288 case POWER_SUPPLY_PROP_CAPACITY
:
289 if (battery
->capacity_now
&& battery
->full_charge_capacity
)
290 val
->intval
= battery
->capacity_now
* 100/
291 battery
->full_charge_capacity
;
295 case POWER_SUPPLY_PROP_CAPACITY_LEVEL
:
296 if (battery
->state
& ACPI_BATTERY_STATE_CRITICAL
)
297 val
->intval
= POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL
;
298 else if (test_bit(ACPI_BATTERY_ALARM_PRESENT
, &battery
->flags
) &&
299 (battery
->capacity_now
<= battery
->alarm
))
300 val
->intval
= POWER_SUPPLY_CAPACITY_LEVEL_LOW
;
301 else if (acpi_battery_is_charged(battery
))
302 val
->intval
= POWER_SUPPLY_CAPACITY_LEVEL_FULL
;
304 val
->intval
= POWER_SUPPLY_CAPACITY_LEVEL_NORMAL
;
306 case POWER_SUPPLY_PROP_MODEL_NAME
:
307 val
->strval
= battery
->model_number
;
309 case POWER_SUPPLY_PROP_MANUFACTURER
:
310 val
->strval
= battery
->oem_info
;
312 case POWER_SUPPLY_PROP_SERIAL_NUMBER
:
313 val
->strval
= battery
->serial_number
;
321 static enum power_supply_property charge_battery_props
[] = {
322 POWER_SUPPLY_PROP_STATUS
,
323 POWER_SUPPLY_PROP_PRESENT
,
324 POWER_SUPPLY_PROP_TECHNOLOGY
,
325 POWER_SUPPLY_PROP_CYCLE_COUNT
,
326 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
,
327 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
328 POWER_SUPPLY_PROP_CURRENT_NOW
,
329 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
330 POWER_SUPPLY_PROP_CHARGE_FULL
,
331 POWER_SUPPLY_PROP_CHARGE_NOW
,
332 POWER_SUPPLY_PROP_CAPACITY
,
333 POWER_SUPPLY_PROP_CAPACITY_LEVEL
,
334 POWER_SUPPLY_PROP_MODEL_NAME
,
335 POWER_SUPPLY_PROP_MANUFACTURER
,
336 POWER_SUPPLY_PROP_SERIAL_NUMBER
,
339 static enum power_supply_property energy_battery_props
[] = {
340 POWER_SUPPLY_PROP_STATUS
,
341 POWER_SUPPLY_PROP_PRESENT
,
342 POWER_SUPPLY_PROP_TECHNOLOGY
,
343 POWER_SUPPLY_PROP_CYCLE_COUNT
,
344 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
,
345 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
346 POWER_SUPPLY_PROP_POWER_NOW
,
347 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
,
348 POWER_SUPPLY_PROP_ENERGY_FULL
,
349 POWER_SUPPLY_PROP_ENERGY_NOW
,
350 POWER_SUPPLY_PROP_CAPACITY
,
351 POWER_SUPPLY_PROP_CAPACITY_LEVEL
,
352 POWER_SUPPLY_PROP_MODEL_NAME
,
353 POWER_SUPPLY_PROP_MANUFACTURER
,
354 POWER_SUPPLY_PROP_SERIAL_NUMBER
,
357 /* --------------------------------------------------------------------------
359 -------------------------------------------------------------------------- */
360 struct acpi_offsets
{
361 size_t offset
; /* offset inside struct acpi_sbs_battery */
362 u8 mode
; /* int or string? */
365 static const struct acpi_offsets state_offsets
[] = {
366 {offsetof(struct acpi_battery
, state
), 0},
367 {offsetof(struct acpi_battery
, rate_now
), 0},
368 {offsetof(struct acpi_battery
, capacity_now
), 0},
369 {offsetof(struct acpi_battery
, voltage_now
), 0},
372 static const struct acpi_offsets info_offsets
[] = {
373 {offsetof(struct acpi_battery
, power_unit
), 0},
374 {offsetof(struct acpi_battery
, design_capacity
), 0},
375 {offsetof(struct acpi_battery
, full_charge_capacity
), 0},
376 {offsetof(struct acpi_battery
, technology
), 0},
377 {offsetof(struct acpi_battery
, design_voltage
), 0},
378 {offsetof(struct acpi_battery
, design_capacity_warning
), 0},
379 {offsetof(struct acpi_battery
, design_capacity_low
), 0},
380 {offsetof(struct acpi_battery
, capacity_granularity_1
), 0},
381 {offsetof(struct acpi_battery
, capacity_granularity_2
), 0},
382 {offsetof(struct acpi_battery
, model_number
), 1},
383 {offsetof(struct acpi_battery
, serial_number
), 1},
384 {offsetof(struct acpi_battery
, type
), 1},
385 {offsetof(struct acpi_battery
, oem_info
), 1},
388 static const struct acpi_offsets extended_info_offsets
[] = {
389 {offsetof(struct acpi_battery
, revision
), 0},
390 {offsetof(struct acpi_battery
, power_unit
), 0},
391 {offsetof(struct acpi_battery
, design_capacity
), 0},
392 {offsetof(struct acpi_battery
, full_charge_capacity
), 0},
393 {offsetof(struct acpi_battery
, technology
), 0},
394 {offsetof(struct acpi_battery
, design_voltage
), 0},
395 {offsetof(struct acpi_battery
, design_capacity_warning
), 0},
396 {offsetof(struct acpi_battery
, design_capacity_low
), 0},
397 {offsetof(struct acpi_battery
, cycle_count
), 0},
398 {offsetof(struct acpi_battery
, measurement_accuracy
), 0},
399 {offsetof(struct acpi_battery
, max_sampling_time
), 0},
400 {offsetof(struct acpi_battery
, min_sampling_time
), 0},
401 {offsetof(struct acpi_battery
, max_averaging_interval
), 0},
402 {offsetof(struct acpi_battery
, min_averaging_interval
), 0},
403 {offsetof(struct acpi_battery
, capacity_granularity_1
), 0},
404 {offsetof(struct acpi_battery
, capacity_granularity_2
), 0},
405 {offsetof(struct acpi_battery
, model_number
), 1},
406 {offsetof(struct acpi_battery
, serial_number
), 1},
407 {offsetof(struct acpi_battery
, type
), 1},
408 {offsetof(struct acpi_battery
, oem_info
), 1},
411 static int extract_package(struct acpi_battery
*battery
,
412 union acpi_object
*package
,
413 const struct acpi_offsets
*offsets
, int num
)
416 union acpi_object
*element
;
417 if (package
->type
!= ACPI_TYPE_PACKAGE
)
419 for (i
= 0; i
< num
; ++i
) {
420 if (package
->package
.count
<= i
)
422 element
= &package
->package
.elements
[i
];
423 if (offsets
[i
].mode
) {
424 u8
*ptr
= (u8
*)battery
+ offsets
[i
].offset
;
425 if (element
->type
== ACPI_TYPE_STRING
||
426 element
->type
== ACPI_TYPE_BUFFER
)
427 strncpy(ptr
, element
->string
.pointer
, 32);
428 else if (element
->type
== ACPI_TYPE_INTEGER
) {
429 strncpy(ptr
, (u8
*)&element
->integer
.value
,
431 ptr
[sizeof(u64
)] = 0;
433 *ptr
= 0; /* don't have value */
435 int *x
= (int *)((u8
*)battery
+ offsets
[i
].offset
);
436 *x
= (element
->type
== ACPI_TYPE_INTEGER
) ?
437 element
->integer
.value
: -1;
443 static int acpi_battery_get_status(struct acpi_battery
*battery
)
445 if (acpi_bus_get_status(battery
->device
)) {
446 ACPI_EXCEPTION((AE_INFO
, AE_ERROR
, "Evaluating _STA"));
453 static int extract_battery_info(const int use_bix
,
454 struct acpi_battery
*battery
,
455 const struct acpi_buffer
*buffer
)
457 int result
= -EFAULT
;
459 if (use_bix
&& battery_bix_broken_package
)
460 result
= extract_package(battery
, buffer
->pointer
,
461 extended_info_offsets
+ 1,
462 ARRAY_SIZE(extended_info_offsets
) - 1);
464 result
= extract_package(battery
, buffer
->pointer
,
465 extended_info_offsets
,
466 ARRAY_SIZE(extended_info_offsets
));
468 result
= extract_package(battery
, buffer
->pointer
,
469 info_offsets
, ARRAY_SIZE(info_offsets
));
470 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY
, &battery
->flags
))
471 battery
->full_charge_capacity
= battery
->design_capacity
;
472 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH
, &battery
->flags
) &&
473 battery
->power_unit
&& battery
->design_voltage
) {
474 battery
->design_capacity
= battery
->design_capacity
*
475 10000 / battery
->design_voltage
;
476 battery
->full_charge_capacity
= battery
->full_charge_capacity
*
477 10000 / battery
->design_voltage
;
478 battery
->design_capacity_warning
=
479 battery
->design_capacity_warning
*
480 10000 / battery
->design_voltage
;
481 /* Curiously, design_capacity_low, unlike the rest of them,
483 /* capacity_granularity_* equal 1 on the systems tested, so
484 it's impossible to tell if they would need an adjustment
485 or not if their values were higher. */
490 static int acpi_battery_get_info(struct acpi_battery
*battery
)
492 const int xinfo
= test_bit(ACPI_BATTERY_XINFO_PRESENT
, &battery
->flags
);
494 int result
= -ENODEV
;
496 if (!acpi_battery_present(battery
))
500 for (use_bix
= xinfo
? 1 : 0; use_bix
>= 0; use_bix
--) {
501 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
502 acpi_status status
= AE_ERROR
;
504 mutex_lock(&battery
->lock
);
505 status
= acpi_evaluate_object(battery
->device
->handle
,
506 use_bix
? "_BIX":"_BIF",
508 mutex_unlock(&battery
->lock
);
510 if (ACPI_FAILURE(status
)) {
511 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating %s",
512 use_bix
? "_BIX":"_BIF"));
514 result
= extract_battery_info(use_bix
,
518 kfree(buffer
.pointer
);
523 if (!result
&& !use_bix
&& xinfo
)
524 pr_warn(FW_BUG
"The _BIX method is broken, using _BIF.\n");
529 static int acpi_battery_get_state(struct acpi_battery
*battery
)
532 acpi_status status
= 0;
533 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
535 if (!acpi_battery_present(battery
))
538 if (battery
->update_time
&&
539 time_before(jiffies
, battery
->update_time
+
540 msecs_to_jiffies(cache_time
)))
543 mutex_lock(&battery
->lock
);
544 status
= acpi_evaluate_object(battery
->device
->handle
, "_BST",
546 mutex_unlock(&battery
->lock
);
548 if (ACPI_FAILURE(status
)) {
549 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _BST"));
553 result
= extract_package(battery
, buffer
.pointer
,
554 state_offsets
, ARRAY_SIZE(state_offsets
));
555 battery
->update_time
= jiffies
;
556 kfree(buffer
.pointer
);
558 /* For buggy DSDTs that report negative 16-bit values for either
559 * charging or discharging current and/or report 0 as 65536
562 if (battery
->power_unit
== ACPI_BATTERY_POWER_UNIT_MA
&&
563 battery
->rate_now
!= ACPI_BATTERY_VALUE_UNKNOWN
&&
564 (s16
)(battery
->rate_now
) < 0) {
565 battery
->rate_now
= abs((s16
)battery
->rate_now
);
566 printk_once(KERN_WARNING FW_BUG
567 "battery: (dis)charge rate invalid.\n");
570 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY
, &battery
->flags
)
571 && battery
->capacity_now
>= 0 && battery
->capacity_now
<= 100)
572 battery
->capacity_now
= (battery
->capacity_now
*
573 battery
->full_charge_capacity
) / 100;
574 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH
, &battery
->flags
) &&
575 battery
->power_unit
&& battery
->design_voltage
) {
576 battery
->capacity_now
= battery
->capacity_now
*
577 10000 / battery
->design_voltage
;
582 static int acpi_battery_set_alarm(struct acpi_battery
*battery
)
584 acpi_status status
= 0;
586 if (!acpi_battery_present(battery
) ||
587 !test_bit(ACPI_BATTERY_ALARM_PRESENT
, &battery
->flags
))
590 mutex_lock(&battery
->lock
);
591 status
= acpi_execute_simple_method(battery
->device
->handle
, "_BTP",
593 mutex_unlock(&battery
->lock
);
595 if (ACPI_FAILURE(status
))
598 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Alarm set to %d\n", battery
->alarm
));
602 static int acpi_battery_init_alarm(struct acpi_battery
*battery
)
604 /* See if alarms are supported, and if so, set default */
605 if (!acpi_has_method(battery
->device
->handle
, "_BTP")) {
606 clear_bit(ACPI_BATTERY_ALARM_PRESENT
, &battery
->flags
);
609 set_bit(ACPI_BATTERY_ALARM_PRESENT
, &battery
->flags
);
611 battery
->alarm
= battery
->design_capacity_warning
;
612 return acpi_battery_set_alarm(battery
);
615 static ssize_t
acpi_battery_alarm_show(struct device
*dev
,
616 struct device_attribute
*attr
,
619 struct acpi_battery
*battery
= to_acpi_battery(dev_get_drvdata(dev
));
620 return sprintf(buf
, "%d\n", battery
->alarm
* 1000);
623 static ssize_t
acpi_battery_alarm_store(struct device
*dev
,
624 struct device_attribute
*attr
,
625 const char *buf
, size_t count
)
628 struct acpi_battery
*battery
= to_acpi_battery(dev_get_drvdata(dev
));
629 if (sscanf(buf
, "%lu\n", &x
) == 1)
630 battery
->alarm
= x
/1000;
631 if (acpi_battery_present(battery
))
632 acpi_battery_set_alarm(battery
);
636 static const struct device_attribute alarm_attr
= {
637 .attr
= {.name
= "alarm", .mode
= 0644},
638 .show
= acpi_battery_alarm_show
,
639 .store
= acpi_battery_alarm_store
,
642 static int sysfs_add_battery(struct acpi_battery
*battery
)
644 struct power_supply_config psy_cfg
= { .drv_data
= battery
, };
646 if (battery
->power_unit
== ACPI_BATTERY_POWER_UNIT_MA
) {
647 battery
->bat_desc
.properties
= charge_battery_props
;
648 battery
->bat_desc
.num_properties
=
649 ARRAY_SIZE(charge_battery_props
);
651 battery
->bat_desc
.properties
= energy_battery_props
;
652 battery
->bat_desc
.num_properties
=
653 ARRAY_SIZE(energy_battery_props
);
656 battery
->bat_desc
.name
= acpi_device_bid(battery
->device
);
657 battery
->bat_desc
.type
= POWER_SUPPLY_TYPE_BATTERY
;
658 battery
->bat_desc
.get_property
= acpi_battery_get_property
;
660 battery
->bat
= power_supply_register_no_ws(&battery
->device
->dev
,
661 &battery
->bat_desc
, &psy_cfg
);
663 if (IS_ERR(battery
->bat
)) {
664 int result
= PTR_ERR(battery
->bat
);
669 return device_create_file(&battery
->bat
->dev
, &alarm_attr
);
672 static void sysfs_remove_battery(struct acpi_battery
*battery
)
674 mutex_lock(&battery
->sysfs_lock
);
676 mutex_unlock(&battery
->sysfs_lock
);
680 device_remove_file(&battery
->bat
->dev
, &alarm_attr
);
681 power_supply_unregister(battery
->bat
);
683 mutex_unlock(&battery
->sysfs_lock
);
686 static void find_battery(const struct dmi_header
*dm
, void *private)
688 struct acpi_battery
*battery
= (struct acpi_battery
*)private;
689 /* Note: the hardcoded offsets below have been extracted from
690 the source code of dmidecode. */
691 if (dm
->type
== DMI_ENTRY_PORTABLE_BATTERY
&& dm
->length
>= 8) {
692 const u8
*dmi_data
= (const u8
*)(dm
+ 1);
693 int dmi_capacity
= get_unaligned((const u16
*)(dmi_data
+ 6));
694 if (dm
->length
>= 18)
695 dmi_capacity
*= dmi_data
[17];
696 if (battery
->design_capacity
* battery
->design_voltage
/ 1000
698 battery
->design_capacity
* 10 == dmi_capacity
)
699 set_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH
,
705 * According to the ACPI spec, some kinds of primary batteries can
706 * report percentage battery remaining capacity directly to OS.
707 * In this case, it reports the Last Full Charged Capacity == 100
708 * and BatteryPresentRate == 0xFFFFFFFF.
710 * Now we found some battery reports percentage remaining capacity
711 * even if it's rechargeable.
712 * https://bugzilla.kernel.org/show_bug.cgi?id=15979
714 * Handle this correctly so that they won't break userspace.
716 static void acpi_battery_quirks(struct acpi_battery
*battery
)
718 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY
, &battery
->flags
))
721 if (battery
->full_charge_capacity
== 100 &&
722 battery
->rate_now
== ACPI_BATTERY_VALUE_UNKNOWN
&&
723 battery
->capacity_now
>= 0 && battery
->capacity_now
<= 100) {
724 set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY
, &battery
->flags
);
725 battery
->full_charge_capacity
= battery
->design_capacity
;
726 battery
->capacity_now
= (battery
->capacity_now
*
727 battery
->full_charge_capacity
) / 100;
730 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH
, &battery
->flags
))
733 if (battery
->power_unit
&& dmi_name_in_vendors("LENOVO")) {
735 s
= dmi_get_system_info(DMI_PRODUCT_VERSION
);
736 if (s
&& !strncasecmp(s
, "ThinkPad", 8)) {
737 dmi_walk(find_battery
, battery
);
738 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH
,
740 battery
->design_voltage
) {
741 battery
->design_capacity
=
742 battery
->design_capacity
*
743 10000 / battery
->design_voltage
;
744 battery
->full_charge_capacity
=
745 battery
->full_charge_capacity
*
746 10000 / battery
->design_voltage
;
747 battery
->design_capacity_warning
=
748 battery
->design_capacity_warning
*
749 10000 / battery
->design_voltage
;
750 battery
->capacity_now
= battery
->capacity_now
*
751 10000 / battery
->design_voltage
;
757 static int acpi_battery_update(struct acpi_battery
*battery
, bool resume
)
759 int result
, old_present
= acpi_battery_present(battery
);
760 result
= acpi_battery_get_status(battery
);
763 if (!acpi_battery_present(battery
)) {
764 sysfs_remove_battery(battery
);
765 battery
->update_time
= 0;
772 if (!battery
->update_time
||
773 old_present
!= acpi_battery_present(battery
)) {
774 result
= acpi_battery_get_info(battery
);
777 acpi_battery_init_alarm(battery
);
780 result
= acpi_battery_get_state(battery
);
783 acpi_battery_quirks(battery
);
786 result
= sysfs_add_battery(battery
);
792 * Wakeup the system if battery is critical low
793 * or lower than the alarm level
795 if ((battery
->state
& ACPI_BATTERY_STATE_CRITICAL
) ||
796 (test_bit(ACPI_BATTERY_ALARM_PRESENT
, &battery
->flags
) &&
797 (battery
->capacity_now
<= battery
->alarm
)))
798 acpi_pm_wakeup_event(&battery
->device
->dev
);
803 static void acpi_battery_refresh(struct acpi_battery
*battery
)
810 power_unit
= battery
->power_unit
;
812 acpi_battery_get_info(battery
);
814 if (power_unit
== battery
->power_unit
)
817 /* The battery has changed its reporting units. */
818 sysfs_remove_battery(battery
);
819 sysfs_add_battery(battery
);
822 /* --------------------------------------------------------------------------
824 -------------------------------------------------------------------------- */
826 #ifdef CONFIG_ACPI_PROCFS_POWER
827 static struct proc_dir_entry
*acpi_battery_dir
;
829 static const char *acpi_battery_units(const struct acpi_battery
*battery
)
831 return (battery
->power_unit
== ACPI_BATTERY_POWER_UNIT_MA
) ?
835 static int acpi_battery_print_info(struct seq_file
*seq
, int result
)
837 struct acpi_battery
*battery
= seq
->private;
842 seq_printf(seq
, "present: %s\n",
843 acpi_battery_present(battery
) ? "yes" : "no");
844 if (!acpi_battery_present(battery
))
846 if (battery
->design_capacity
== ACPI_BATTERY_VALUE_UNKNOWN
)
847 seq_printf(seq
, "design capacity: unknown\n");
849 seq_printf(seq
, "design capacity: %d %sh\n",
850 battery
->design_capacity
,
851 acpi_battery_units(battery
));
853 if (battery
->full_charge_capacity
== ACPI_BATTERY_VALUE_UNKNOWN
)
854 seq_printf(seq
, "last full capacity: unknown\n");
856 seq_printf(seq
, "last full capacity: %d %sh\n",
857 battery
->full_charge_capacity
,
858 acpi_battery_units(battery
));
860 seq_printf(seq
, "battery technology: %srechargeable\n",
861 (!battery
->technology
)?"non-":"");
863 if (battery
->design_voltage
== ACPI_BATTERY_VALUE_UNKNOWN
)
864 seq_printf(seq
, "design voltage: unknown\n");
866 seq_printf(seq
, "design voltage: %d mV\n",
867 battery
->design_voltage
);
868 seq_printf(seq
, "design capacity warning: %d %sh\n",
869 battery
->design_capacity_warning
,
870 acpi_battery_units(battery
));
871 seq_printf(seq
, "design capacity low: %d %sh\n",
872 battery
->design_capacity_low
,
873 acpi_battery_units(battery
));
874 seq_printf(seq
, "cycle count: %i\n", battery
->cycle_count
);
875 seq_printf(seq
, "capacity granularity 1: %d %sh\n",
876 battery
->capacity_granularity_1
,
877 acpi_battery_units(battery
));
878 seq_printf(seq
, "capacity granularity 2: %d %sh\n",
879 battery
->capacity_granularity_2
,
880 acpi_battery_units(battery
));
881 seq_printf(seq
, "model number: %s\n", battery
->model_number
);
882 seq_printf(seq
, "serial number: %s\n", battery
->serial_number
);
883 seq_printf(seq
, "battery type: %s\n", battery
->type
);
884 seq_printf(seq
, "OEM info: %s\n", battery
->oem_info
);
887 seq_printf(seq
, "ERROR: Unable to read battery info\n");
891 static int acpi_battery_print_state(struct seq_file
*seq
, int result
)
893 struct acpi_battery
*battery
= seq
->private;
898 seq_printf(seq
, "present: %s\n",
899 acpi_battery_present(battery
) ? "yes" : "no");
900 if (!acpi_battery_present(battery
))
903 seq_printf(seq
, "capacity state: %s\n",
904 (battery
->state
& 0x04) ? "critical" : "ok");
905 if ((battery
->state
& 0x01) && (battery
->state
& 0x02))
907 "charging state: charging/discharging\n");
908 else if (battery
->state
& 0x01)
909 seq_printf(seq
, "charging state: discharging\n");
910 else if (battery
->state
& 0x02)
911 seq_printf(seq
, "charging state: charging\n");
913 seq_printf(seq
, "charging state: charged\n");
915 if (battery
->rate_now
== ACPI_BATTERY_VALUE_UNKNOWN
)
916 seq_printf(seq
, "present rate: unknown\n");
918 seq_printf(seq
, "present rate: %d %s\n",
919 battery
->rate_now
, acpi_battery_units(battery
));
921 if (battery
->capacity_now
== ACPI_BATTERY_VALUE_UNKNOWN
)
922 seq_printf(seq
, "remaining capacity: unknown\n");
924 seq_printf(seq
, "remaining capacity: %d %sh\n",
925 battery
->capacity_now
, acpi_battery_units(battery
));
926 if (battery
->voltage_now
== ACPI_BATTERY_VALUE_UNKNOWN
)
927 seq_printf(seq
, "present voltage: unknown\n");
929 seq_printf(seq
, "present voltage: %d mV\n",
930 battery
->voltage_now
);
933 seq_printf(seq
, "ERROR: Unable to read battery state\n");
938 static int acpi_battery_print_alarm(struct seq_file
*seq
, int result
)
940 struct acpi_battery
*battery
= seq
->private;
945 if (!acpi_battery_present(battery
)) {
946 seq_printf(seq
, "present: no\n");
949 seq_printf(seq
, "alarm: ");
951 seq_printf(seq
, "unsupported\n");
953 seq_printf(seq
, "%u %sh\n", battery
->alarm
,
954 acpi_battery_units(battery
));
957 seq_printf(seq
, "ERROR: Unable to read battery alarm\n");
961 static ssize_t
acpi_battery_write_alarm(struct file
*file
,
962 const char __user
* buffer
,
963 size_t count
, loff_t
* ppos
)
966 char alarm_string
[12] = { '\0' };
967 struct seq_file
*m
= file
->private_data
;
968 struct acpi_battery
*battery
= m
->private;
970 if (!battery
|| (count
> sizeof(alarm_string
) - 1))
972 if (!acpi_battery_present(battery
)) {
976 if (copy_from_user(alarm_string
, buffer
, count
)) {
980 alarm_string
[count
] = '\0';
981 if (kstrtoint(alarm_string
, 0, &battery
->alarm
)) {
985 result
= acpi_battery_set_alarm(battery
);
992 typedef int(*print_func
)(struct seq_file
*seq
, int result
);
994 static print_func acpi_print_funcs
[ACPI_BATTERY_NUMFILES
] = {
995 acpi_battery_print_info
,
996 acpi_battery_print_state
,
997 acpi_battery_print_alarm
,
1000 static int acpi_battery_read(int fid
, struct seq_file
*seq
)
1002 struct acpi_battery
*battery
= seq
->private;
1003 int result
= acpi_battery_update(battery
, false);
1004 return acpi_print_funcs
[fid
](seq
, result
);
1007 #define DECLARE_FILE_FUNCTIONS(_name) \
1008 static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \
1010 return acpi_battery_read(_name##_tag, seq); \
1012 static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \
1014 return single_open(file, acpi_battery_read_##_name, PDE_DATA(inode)); \
1017 DECLARE_FILE_FUNCTIONS(info
);
1018 DECLARE_FILE_FUNCTIONS(state
);
1019 DECLARE_FILE_FUNCTIONS(alarm
);
1021 #undef DECLARE_FILE_FUNCTIONS
1023 #define FILE_DESCRIPTION_RO(_name) \
1025 .name = __stringify(_name), \
1028 .open = acpi_battery_##_name##_open_fs, \
1030 .llseek = seq_lseek, \
1031 .release = single_release, \
1032 .owner = THIS_MODULE, \
1036 #define FILE_DESCRIPTION_RW(_name) \
1038 .name = __stringify(_name), \
1039 .mode = S_IFREG | S_IRUGO | S_IWUSR, \
1041 .open = acpi_battery_##_name##_open_fs, \
1043 .llseek = seq_lseek, \
1044 .write = acpi_battery_write_##_name, \
1045 .release = single_release, \
1046 .owner = THIS_MODULE, \
1050 static const struct battery_file
{
1051 struct file_operations ops
;
1054 } acpi_battery_file
[] = {
1055 FILE_DESCRIPTION_RO(info
),
1056 FILE_DESCRIPTION_RO(state
),
1057 FILE_DESCRIPTION_RW(alarm
),
1060 #undef FILE_DESCRIPTION_RO
1061 #undef FILE_DESCRIPTION_RW
1063 static int acpi_battery_add_fs(struct acpi_device
*device
)
1065 struct proc_dir_entry
*entry
= NULL
;
1068 printk(KERN_WARNING PREFIX
"Deprecated procfs I/F for battery is loaded,"
1069 " please retry with CONFIG_ACPI_PROCFS_POWER cleared\n");
1070 if (!acpi_device_dir(device
)) {
1071 acpi_device_dir(device
) = proc_mkdir(acpi_device_bid(device
),
1073 if (!acpi_device_dir(device
))
1077 for (i
= 0; i
< ACPI_BATTERY_NUMFILES
; ++i
) {
1078 entry
= proc_create_data(acpi_battery_file
[i
].name
,
1079 acpi_battery_file
[i
].mode
,
1080 acpi_device_dir(device
),
1081 &acpi_battery_file
[i
].ops
,
1082 acpi_driver_data(device
));
1089 static void acpi_battery_remove_fs(struct acpi_device
*device
)
1092 if (!acpi_device_dir(device
))
1094 for (i
= 0; i
< ACPI_BATTERY_NUMFILES
; ++i
)
1095 remove_proc_entry(acpi_battery_file
[i
].name
,
1096 acpi_device_dir(device
));
1098 remove_proc_entry(acpi_device_bid(device
), acpi_battery_dir
);
1099 acpi_device_dir(device
) = NULL
;
1104 /* --------------------------------------------------------------------------
1106 -------------------------------------------------------------------------- */
1108 static void acpi_battery_notify(struct acpi_device
*device
, u32 event
)
1110 struct acpi_battery
*battery
= acpi_driver_data(device
);
1111 struct power_supply
*old
;
1117 * On Acer Aspire V5-573G notifications are sometimes triggered too
1118 * early. For example, when AC is unplugged and notification is
1119 * triggered, battery state is still reported as "Full", and changes to
1120 * "Discharging" only after short delay, without any notification.
1122 if (battery_notification_delay_ms
> 0)
1123 msleep(battery_notification_delay_ms
);
1124 if (event
== ACPI_BATTERY_NOTIFY_INFO
)
1125 acpi_battery_refresh(battery
);
1126 acpi_battery_update(battery
, false);
1127 acpi_bus_generate_netlink_event(device
->pnp
.device_class
,
1128 dev_name(&device
->dev
), event
,
1129 acpi_battery_present(battery
));
1130 acpi_notifier_call_chain(device
, event
, acpi_battery_present(battery
));
1131 /* acpi_battery_update could remove power_supply object */
1132 if (old
&& battery
->bat
)
1133 power_supply_changed(battery
->bat
);
1136 static int battery_notify(struct notifier_block
*nb
,
1137 unsigned long mode
, void *_unused
)
1139 struct acpi_battery
*battery
= container_of(nb
, struct acpi_battery
,
1144 case PM_POST_HIBERNATION
:
1145 case PM_POST_SUSPEND
:
1146 if (!acpi_battery_present(battery
))
1149 if (!battery
->bat
) {
1150 result
= acpi_battery_get_info(battery
);
1154 result
= sysfs_add_battery(battery
);
1158 acpi_battery_refresh(battery
);
1160 acpi_battery_init_alarm(battery
);
1161 acpi_battery_get_state(battery
);
1169 battery_bix_broken_package_quirk(const struct dmi_system_id
*d
)
1171 battery_bix_broken_package
= 1;
1176 battery_notification_delay_quirk(const struct dmi_system_id
*d
)
1178 battery_notification_delay_ms
= 1000;
1182 static const struct dmi_system_id bat_dmi_table
[] __initconst
= {
1185 .callback
= battery_bix_broken_package_quirk
,
1187 DMI_MATCH(DMI_SYS_VENDOR
, "NEC"),
1188 DMI_MATCH(DMI_PRODUCT_NAME
, "PC-LZ750LS"),
1192 /* Acer Aspire V5-573G */
1193 .callback
= battery_notification_delay_quirk
,
1195 DMI_MATCH(DMI_SYS_VENDOR
, "Acer"),
1196 DMI_MATCH(DMI_PRODUCT_NAME
, "Aspire V5-573G"),
1203 * Some machines'(E,G Lenovo Z480) ECs are not stable
1204 * during boot up and this causes battery driver fails to be
1205 * probed due to failure of getting battery information
1206 * from EC sometimes. After several retries, the operation
1207 * may work. So add retry code here and 20ms sleep between
1210 static int acpi_battery_update_retry(struct acpi_battery
*battery
)
1214 for (retry
= 5; retry
; retry
--) {
1215 ret
= acpi_battery_update(battery
, false);
1224 static int acpi_battery_add(struct acpi_device
*device
)
1227 struct acpi_battery
*battery
= NULL
;
1232 if (device
->dep_unmet
)
1233 return -EPROBE_DEFER
;
1235 battery
= kzalloc(sizeof(struct acpi_battery
), GFP_KERNEL
);
1238 battery
->device
= device
;
1239 strcpy(acpi_device_name(device
), ACPI_BATTERY_DEVICE_NAME
);
1240 strcpy(acpi_device_class(device
), ACPI_BATTERY_CLASS
);
1241 device
->driver_data
= battery
;
1242 mutex_init(&battery
->lock
);
1243 mutex_init(&battery
->sysfs_lock
);
1244 if (acpi_has_method(battery
->device
->handle
, "_BIX"))
1245 set_bit(ACPI_BATTERY_XINFO_PRESENT
, &battery
->flags
);
1247 result
= acpi_battery_update_retry(battery
);
1251 #ifdef CONFIG_ACPI_PROCFS_POWER
1252 result
= acpi_battery_add_fs(device
);
1255 #ifdef CONFIG_ACPI_PROCFS_POWER
1256 acpi_battery_remove_fs(device
);
1261 printk(KERN_INFO PREFIX
"%s Slot [%s] (battery %s)\n",
1262 ACPI_BATTERY_DEVICE_NAME
, acpi_device_bid(device
),
1263 device
->status
.battery_present
? "present" : "absent");
1265 battery
->pm_nb
.notifier_call
= battery_notify
;
1266 register_pm_notifier(&battery
->pm_nb
);
1268 device_init_wakeup(&device
->dev
, 1);
1273 sysfs_remove_battery(battery
);
1274 mutex_destroy(&battery
->lock
);
1275 mutex_destroy(&battery
->sysfs_lock
);
1280 static int acpi_battery_remove(struct acpi_device
*device
)
1282 struct acpi_battery
*battery
= NULL
;
1284 if (!device
|| !acpi_driver_data(device
))
1286 device_init_wakeup(&device
->dev
, 0);
1287 battery
= acpi_driver_data(device
);
1288 unregister_pm_notifier(&battery
->pm_nb
);
1289 #ifdef CONFIG_ACPI_PROCFS_POWER
1290 acpi_battery_remove_fs(device
);
1292 sysfs_remove_battery(battery
);
1293 mutex_destroy(&battery
->lock
);
1294 mutex_destroy(&battery
->sysfs_lock
);
1299 #ifdef CONFIG_PM_SLEEP
1300 /* this is needed to learn about changes made in suspended state */
1301 static int acpi_battery_resume(struct device
*dev
)
1303 struct acpi_battery
*battery
;
1308 battery
= acpi_driver_data(to_acpi_device(dev
));
1312 battery
->update_time
= 0;
1313 acpi_battery_update(battery
, true);
1317 #define acpi_battery_resume NULL
1320 static SIMPLE_DEV_PM_OPS(acpi_battery_pm
, NULL
, acpi_battery_resume
);
1322 static struct acpi_driver acpi_battery_driver
= {
1324 .class = ACPI_BATTERY_CLASS
,
1325 .ids
= battery_device_ids
,
1326 .flags
= ACPI_DRIVER_ALL_NOTIFY_EVENTS
,
1328 .add
= acpi_battery_add
,
1329 .remove
= acpi_battery_remove
,
1330 .notify
= acpi_battery_notify
,
1332 .drv
.pm
= &acpi_battery_pm
,
1335 static void __init
acpi_battery_init_async(void *unused
, async_cookie_t cookie
)
1340 for (i
= 0; i
< ARRAY_SIZE(acpi_battery_blacklist
); i
++)
1341 if (acpi_dev_present(acpi_battery_blacklist
[i
], "1", -1)) {
1342 pr_info(PREFIX ACPI_BATTERY_DEVICE_NAME
1343 ": found native %s PMIC, not loading\n",
1344 acpi_battery_blacklist
[i
]);
1348 dmi_check_system(bat_dmi_table
);
1350 #ifdef CONFIG_ACPI_PROCFS_POWER
1351 acpi_battery_dir
= acpi_lock_battery_dir();
1352 if (!acpi_battery_dir
)
1355 result
= acpi_bus_register_driver(&acpi_battery_driver
);
1356 #ifdef CONFIG_ACPI_PROCFS_POWER
1358 acpi_unlock_battery_dir(acpi_battery_dir
);
1360 battery_driver_registered
= (result
== 0);
1363 static int __init
acpi_battery_init(void)
1368 async_cookie
= async_schedule(acpi_battery_init_async
, NULL
);
1372 static void __exit
acpi_battery_exit(void)
1374 async_synchronize_cookie(async_cookie
+ 1);
1375 if (battery_driver_registered
)
1376 acpi_bus_unregister_driver(&acpi_battery_driver
);
1377 #ifdef CONFIG_ACPI_PROCFS_POWER
1378 if (acpi_battery_dir
)
1379 acpi_unlock_battery_dir(acpi_battery_dir
);
1383 module_init(acpi_battery_init
);
1384 module_exit(acpi_battery_exit
);