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 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/types.h>
32 #include <linux/jiffies.h>
33 #include <linux/async.h>
34 #include <linux/dmi.h>
35 #include <linux/slab.h>
36 #include <linux/suspend.h>
37 #include <asm/unaligned.h>
39 #include <linux/acpi.h>
40 #include <linux/power_supply.h>
42 #define PREFIX "ACPI: "
44 #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
46 #define ACPI_BATTERY_CLASS "battery"
47 #define ACPI_BATTERY_DEVICE_NAME "Battery"
48 #define ACPI_BATTERY_NOTIFY_STATUS 0x80
49 #define ACPI_BATTERY_NOTIFY_INFO 0x81
50 #define ACPI_BATTERY_NOTIFY_THRESHOLD 0x82
52 /* Battery power unit: 0 means mW, 1 means mA */
53 #define ACPI_BATTERY_POWER_UNIT_MA 1
55 #define _COMPONENT ACPI_BATTERY_COMPONENT
57 ACPI_MODULE_NAME("battery");
59 MODULE_AUTHOR("Paul Diefenbaugh");
60 MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
61 MODULE_DESCRIPTION("ACPI Battery Driver");
62 MODULE_LICENSE("GPL");
64 static unsigned int cache_time
= 1000;
65 module_param(cache_time
, uint
, 0644);
66 MODULE_PARM_DESC(cache_time
, "cache time in milliseconds");
68 static const struct acpi_device_id battery_device_ids
[] = {
73 MODULE_DEVICE_TABLE(acpi
, battery_device_ids
);
76 ACPI_BATTERY_ALARM_PRESENT
,
77 ACPI_BATTERY_XINFO_PRESENT
,
78 ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY
,
79 /* On Lenovo Thinkpad models from 2010 and 2011, the power unit
80 switches between mWh and mAh depending on whether the system
81 is running on battery or not. When mAh is the unit, most
82 reported values are incorrect and need to be adjusted by
83 10000/design_voltage. Verified on x201, t410, t410s, and x220.
84 Pre-2010 and 2012 models appear to always report in mWh and
85 are thus unaffected (tested with t42, t61, t500, x200, x300,
86 and x230). Also, in mid-2012 Lenovo issued a BIOS update for
87 the 2011 models that fixes the issue (tested on x220 with a
88 post-1.29 BIOS), but as of Nov. 2012, no such update is
89 available for the 2010 models. */
90 ACPI_BATTERY_QUIRK_THINKPAD_MAH
,
95 struct mutex sysfs_lock
;
96 struct power_supply bat
;
97 struct acpi_device
*device
;
98 struct notifier_block pm_nb
;
99 unsigned long update_time
;
105 int full_charge_capacity
;
108 int design_capacity_warning
;
109 int design_capacity_low
;
111 int measurement_accuracy
;
112 int max_sampling_time
;
113 int min_sampling_time
;
114 int max_averaging_interval
;
115 int min_averaging_interval
;
116 int capacity_granularity_1
;
117 int capacity_granularity_2
;
119 char model_number
[32];
120 char serial_number
[32];
128 #define to_acpi_battery(x) container_of(x, struct acpi_battery, bat)
130 static inline int acpi_battery_present(struct acpi_battery
*battery
)
132 return battery
->device
->status
.battery_present
;
135 static int acpi_battery_technology(struct acpi_battery
*battery
)
137 if (!strcasecmp("NiCd", battery
->type
))
138 return POWER_SUPPLY_TECHNOLOGY_NiCd
;
139 if (!strcasecmp("NiMH", battery
->type
))
140 return POWER_SUPPLY_TECHNOLOGY_NiMH
;
141 if (!strcasecmp("LION", battery
->type
))
142 return POWER_SUPPLY_TECHNOLOGY_LION
;
143 if (!strncasecmp("LI-ION", battery
->type
, 6))
144 return POWER_SUPPLY_TECHNOLOGY_LION
;
145 if (!strcasecmp("LiP", battery
->type
))
146 return POWER_SUPPLY_TECHNOLOGY_LIPO
;
147 return POWER_SUPPLY_TECHNOLOGY_UNKNOWN
;
150 static int acpi_battery_get_state(struct acpi_battery
*battery
);
152 static int acpi_battery_is_charged(struct acpi_battery
*battery
)
154 /* either charging or discharging */
155 if (battery
->state
!= 0)
158 /* battery not reporting charge */
159 if (battery
->capacity_now
== ACPI_BATTERY_VALUE_UNKNOWN
||
160 battery
->capacity_now
== 0)
163 /* good batteries update full_charge as the batteries degrade */
164 if (battery
->full_charge_capacity
== battery
->capacity_now
)
167 /* fallback to using design values for broken batteries */
168 if (battery
->design_capacity
== battery
->capacity_now
)
171 /* we don't do any sort of metric based on percentages */
175 static int acpi_battery_get_property(struct power_supply
*psy
,
176 enum power_supply_property psp
,
177 union power_supply_propval
*val
)
180 struct acpi_battery
*battery
= to_acpi_battery(psy
);
182 if (acpi_battery_present(battery
)) {
183 /* run battery update only if it is present */
184 acpi_battery_get_state(battery
);
185 } else if (psp
!= POWER_SUPPLY_PROP_PRESENT
)
188 case POWER_SUPPLY_PROP_STATUS
:
189 if (battery
->state
& 0x01)
190 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
191 else if (battery
->state
& 0x02)
192 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
193 else if (acpi_battery_is_charged(battery
))
194 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
196 val
->intval
= POWER_SUPPLY_STATUS_UNKNOWN
;
198 case POWER_SUPPLY_PROP_PRESENT
:
199 val
->intval
= acpi_battery_present(battery
);
201 case POWER_SUPPLY_PROP_TECHNOLOGY
:
202 val
->intval
= acpi_battery_technology(battery
);
204 case POWER_SUPPLY_PROP_CYCLE_COUNT
:
205 val
->intval
= battery
->cycle_count
;
207 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
:
208 if (battery
->design_voltage
== ACPI_BATTERY_VALUE_UNKNOWN
)
211 val
->intval
= battery
->design_voltage
* 1000;
213 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
214 if (battery
->voltage_now
== ACPI_BATTERY_VALUE_UNKNOWN
)
217 val
->intval
= battery
->voltage_now
* 1000;
219 case POWER_SUPPLY_PROP_CURRENT_NOW
:
220 case POWER_SUPPLY_PROP_POWER_NOW
:
221 if (battery
->rate_now
== ACPI_BATTERY_VALUE_UNKNOWN
)
224 val
->intval
= battery
->rate_now
* 1000;
226 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
227 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
:
228 if (battery
->design_capacity
== ACPI_BATTERY_VALUE_UNKNOWN
)
231 val
->intval
= battery
->design_capacity
* 1000;
233 case POWER_SUPPLY_PROP_CHARGE_FULL
:
234 case POWER_SUPPLY_PROP_ENERGY_FULL
:
235 if (battery
->full_charge_capacity
== ACPI_BATTERY_VALUE_UNKNOWN
)
238 val
->intval
= battery
->full_charge_capacity
* 1000;
240 case POWER_SUPPLY_PROP_CHARGE_NOW
:
241 case POWER_SUPPLY_PROP_ENERGY_NOW
:
242 if (battery
->capacity_now
== ACPI_BATTERY_VALUE_UNKNOWN
)
245 val
->intval
= battery
->capacity_now
* 1000;
247 case POWER_SUPPLY_PROP_CAPACITY
:
248 if (battery
->capacity_now
&& battery
->full_charge_capacity
)
249 val
->intval
= battery
->capacity_now
* 100/
250 battery
->full_charge_capacity
;
254 case POWER_SUPPLY_PROP_MODEL_NAME
:
255 val
->strval
= battery
->model_number
;
257 case POWER_SUPPLY_PROP_MANUFACTURER
:
258 val
->strval
= battery
->oem_info
;
260 case POWER_SUPPLY_PROP_SERIAL_NUMBER
:
261 val
->strval
= battery
->serial_number
;
269 static enum power_supply_property charge_battery_props
[] = {
270 POWER_SUPPLY_PROP_STATUS
,
271 POWER_SUPPLY_PROP_PRESENT
,
272 POWER_SUPPLY_PROP_TECHNOLOGY
,
273 POWER_SUPPLY_PROP_CYCLE_COUNT
,
274 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
,
275 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
276 POWER_SUPPLY_PROP_CURRENT_NOW
,
277 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
278 POWER_SUPPLY_PROP_CHARGE_FULL
,
279 POWER_SUPPLY_PROP_CHARGE_NOW
,
280 POWER_SUPPLY_PROP_CAPACITY
,
281 POWER_SUPPLY_PROP_MODEL_NAME
,
282 POWER_SUPPLY_PROP_MANUFACTURER
,
283 POWER_SUPPLY_PROP_SERIAL_NUMBER
,
286 static enum power_supply_property energy_battery_props
[] = {
287 POWER_SUPPLY_PROP_STATUS
,
288 POWER_SUPPLY_PROP_PRESENT
,
289 POWER_SUPPLY_PROP_TECHNOLOGY
,
290 POWER_SUPPLY_PROP_CYCLE_COUNT
,
291 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
,
292 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
293 POWER_SUPPLY_PROP_POWER_NOW
,
294 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
,
295 POWER_SUPPLY_PROP_ENERGY_FULL
,
296 POWER_SUPPLY_PROP_ENERGY_NOW
,
297 POWER_SUPPLY_PROP_CAPACITY
,
298 POWER_SUPPLY_PROP_MODEL_NAME
,
299 POWER_SUPPLY_PROP_MANUFACTURER
,
300 POWER_SUPPLY_PROP_SERIAL_NUMBER
,
303 /* --------------------------------------------------------------------------
305 -------------------------------------------------------------------------- */
306 struct acpi_offsets
{
307 size_t offset
; /* offset inside struct acpi_sbs_battery */
308 u8 mode
; /* int or string? */
311 static struct acpi_offsets state_offsets
[] = {
312 {offsetof(struct acpi_battery
, state
), 0},
313 {offsetof(struct acpi_battery
, rate_now
), 0},
314 {offsetof(struct acpi_battery
, capacity_now
), 0},
315 {offsetof(struct acpi_battery
, voltage_now
), 0},
318 static struct acpi_offsets info_offsets
[] = {
319 {offsetof(struct acpi_battery
, power_unit
), 0},
320 {offsetof(struct acpi_battery
, design_capacity
), 0},
321 {offsetof(struct acpi_battery
, full_charge_capacity
), 0},
322 {offsetof(struct acpi_battery
, technology
), 0},
323 {offsetof(struct acpi_battery
, design_voltage
), 0},
324 {offsetof(struct acpi_battery
, design_capacity_warning
), 0},
325 {offsetof(struct acpi_battery
, design_capacity_low
), 0},
326 {offsetof(struct acpi_battery
, capacity_granularity_1
), 0},
327 {offsetof(struct acpi_battery
, capacity_granularity_2
), 0},
328 {offsetof(struct acpi_battery
, model_number
), 1},
329 {offsetof(struct acpi_battery
, serial_number
), 1},
330 {offsetof(struct acpi_battery
, type
), 1},
331 {offsetof(struct acpi_battery
, oem_info
), 1},
334 static struct acpi_offsets extended_info_offsets
[] = {
335 {offsetof(struct acpi_battery
, revision
), 0},
336 {offsetof(struct acpi_battery
, power_unit
), 0},
337 {offsetof(struct acpi_battery
, design_capacity
), 0},
338 {offsetof(struct acpi_battery
, full_charge_capacity
), 0},
339 {offsetof(struct acpi_battery
, technology
), 0},
340 {offsetof(struct acpi_battery
, design_voltage
), 0},
341 {offsetof(struct acpi_battery
, design_capacity_warning
), 0},
342 {offsetof(struct acpi_battery
, design_capacity_low
), 0},
343 {offsetof(struct acpi_battery
, cycle_count
), 0},
344 {offsetof(struct acpi_battery
, measurement_accuracy
), 0},
345 {offsetof(struct acpi_battery
, max_sampling_time
), 0},
346 {offsetof(struct acpi_battery
, min_sampling_time
), 0},
347 {offsetof(struct acpi_battery
, max_averaging_interval
), 0},
348 {offsetof(struct acpi_battery
, min_averaging_interval
), 0},
349 {offsetof(struct acpi_battery
, capacity_granularity_1
), 0},
350 {offsetof(struct acpi_battery
, capacity_granularity_2
), 0},
351 {offsetof(struct acpi_battery
, model_number
), 1},
352 {offsetof(struct acpi_battery
, serial_number
), 1},
353 {offsetof(struct acpi_battery
, type
), 1},
354 {offsetof(struct acpi_battery
, oem_info
), 1},
357 static int extract_package(struct acpi_battery
*battery
,
358 union acpi_object
*package
,
359 struct acpi_offsets
*offsets
, int num
)
362 union acpi_object
*element
;
363 if (package
->type
!= ACPI_TYPE_PACKAGE
)
365 for (i
= 0; i
< num
; ++i
) {
366 if (package
->package
.count
<= i
)
368 element
= &package
->package
.elements
[i
];
369 if (offsets
[i
].mode
) {
370 u8
*ptr
= (u8
*)battery
+ offsets
[i
].offset
;
371 if (element
->type
== ACPI_TYPE_STRING
||
372 element
->type
== ACPI_TYPE_BUFFER
)
373 strncpy(ptr
, element
->string
.pointer
, 32);
374 else if (element
->type
== ACPI_TYPE_INTEGER
) {
375 strncpy(ptr
, (u8
*)&element
->integer
.value
,
377 ptr
[sizeof(u64
)] = 0;
379 *ptr
= 0; /* don't have value */
381 int *x
= (int *)((u8
*)battery
+ offsets
[i
].offset
);
382 *x
= (element
->type
== ACPI_TYPE_INTEGER
) ?
383 element
->integer
.value
: -1;
389 static int acpi_battery_get_status(struct acpi_battery
*battery
)
391 if (acpi_bus_get_status(battery
->device
)) {
392 ACPI_EXCEPTION((AE_INFO
, AE_ERROR
, "Evaluating _STA"));
398 static int acpi_battery_get_info(struct acpi_battery
*battery
)
400 int result
= -EFAULT
;
401 acpi_status status
= 0;
402 char *name
= test_bit(ACPI_BATTERY_XINFO_PRESENT
, &battery
->flags
) ?
405 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
407 if (!acpi_battery_present(battery
))
409 mutex_lock(&battery
->lock
);
410 status
= acpi_evaluate_object(battery
->device
->handle
, name
,
412 mutex_unlock(&battery
->lock
);
414 if (ACPI_FAILURE(status
)) {
415 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating %s", name
));
418 if (test_bit(ACPI_BATTERY_XINFO_PRESENT
, &battery
->flags
))
419 result
= extract_package(battery
, buffer
.pointer
,
420 extended_info_offsets
,
421 ARRAY_SIZE(extended_info_offsets
));
423 result
= extract_package(battery
, buffer
.pointer
,
424 info_offsets
, ARRAY_SIZE(info_offsets
));
425 kfree(buffer
.pointer
);
426 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY
, &battery
->flags
))
427 battery
->full_charge_capacity
= battery
->design_capacity
;
428 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH
, &battery
->flags
) &&
429 battery
->power_unit
&& battery
->design_voltage
) {
430 battery
->design_capacity
= battery
->design_capacity
*
431 10000 / battery
->design_voltage
;
432 battery
->full_charge_capacity
= battery
->full_charge_capacity
*
433 10000 / battery
->design_voltage
;
434 battery
->design_capacity_warning
=
435 battery
->design_capacity_warning
*
436 10000 / battery
->design_voltage
;
437 /* Curiously, design_capacity_low, unlike the rest of them,
439 /* capacity_granularity_* equal 1 on the systems tested, so
440 it's impossible to tell if they would need an adjustment
441 or not if their values were higher. */
446 static int acpi_battery_get_state(struct acpi_battery
*battery
)
449 acpi_status status
= 0;
450 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
452 if (!acpi_battery_present(battery
))
455 if (battery
->update_time
&&
456 time_before(jiffies
, battery
->update_time
+
457 msecs_to_jiffies(cache_time
)))
460 mutex_lock(&battery
->lock
);
461 status
= acpi_evaluate_object(battery
->device
->handle
, "_BST",
463 mutex_unlock(&battery
->lock
);
465 if (ACPI_FAILURE(status
)) {
466 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _BST"));
470 result
= extract_package(battery
, buffer
.pointer
,
471 state_offsets
, ARRAY_SIZE(state_offsets
));
472 battery
->update_time
= jiffies
;
473 kfree(buffer
.pointer
);
475 /* For buggy DSDTs that report negative 16-bit values for either
476 * charging or discharging current and/or report 0 as 65536
479 if (battery
->power_unit
== ACPI_BATTERY_POWER_UNIT_MA
&&
480 battery
->rate_now
!= ACPI_BATTERY_VALUE_UNKNOWN
&&
481 (s16
)(battery
->rate_now
) < 0) {
482 battery
->rate_now
= abs((s16
)battery
->rate_now
);
483 printk_once(KERN_WARNING FW_BUG
"battery: (dis)charge rate"
487 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY
, &battery
->flags
)
488 && battery
->capacity_now
>= 0 && battery
->capacity_now
<= 100)
489 battery
->capacity_now
= (battery
->capacity_now
*
490 battery
->full_charge_capacity
) / 100;
491 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH
, &battery
->flags
) &&
492 battery
->power_unit
&& battery
->design_voltage
) {
493 battery
->capacity_now
= battery
->capacity_now
*
494 10000 / battery
->design_voltage
;
499 static int acpi_battery_set_alarm(struct acpi_battery
*battery
)
501 acpi_status status
= 0;
503 if (!acpi_battery_present(battery
) ||
504 !test_bit(ACPI_BATTERY_ALARM_PRESENT
, &battery
->flags
))
507 mutex_lock(&battery
->lock
);
508 status
= acpi_execute_simple_method(battery
->device
->handle
, "_BTP",
510 mutex_unlock(&battery
->lock
);
512 if (ACPI_FAILURE(status
))
515 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Alarm set to %d\n", battery
->alarm
));
519 static int acpi_battery_init_alarm(struct acpi_battery
*battery
)
521 /* See if alarms are supported, and if so, set default */
522 if (!acpi_has_method(battery
->device
->handle
, "_BTP")) {
523 clear_bit(ACPI_BATTERY_ALARM_PRESENT
, &battery
->flags
);
526 set_bit(ACPI_BATTERY_ALARM_PRESENT
, &battery
->flags
);
528 battery
->alarm
= battery
->design_capacity_warning
;
529 return acpi_battery_set_alarm(battery
);
532 static ssize_t
acpi_battery_alarm_show(struct device
*dev
,
533 struct device_attribute
*attr
,
536 struct acpi_battery
*battery
= to_acpi_battery(dev_get_drvdata(dev
));
537 return sprintf(buf
, "%d\n", battery
->alarm
* 1000);
540 static ssize_t
acpi_battery_alarm_store(struct device
*dev
,
541 struct device_attribute
*attr
,
542 const char *buf
, size_t count
)
545 struct acpi_battery
*battery
= to_acpi_battery(dev_get_drvdata(dev
));
546 if (sscanf(buf
, "%ld\n", &x
) == 1)
547 battery
->alarm
= x
/1000;
548 if (acpi_battery_present(battery
))
549 acpi_battery_set_alarm(battery
);
553 static struct device_attribute alarm_attr
= {
554 .attr
= {.name
= "alarm", .mode
= 0644},
555 .show
= acpi_battery_alarm_show
,
556 .store
= acpi_battery_alarm_store
,
559 static int sysfs_add_battery(struct acpi_battery
*battery
)
563 if (battery
->power_unit
== ACPI_BATTERY_POWER_UNIT_MA
) {
564 battery
->bat
.properties
= charge_battery_props
;
565 battery
->bat
.num_properties
=
566 ARRAY_SIZE(charge_battery_props
);
568 battery
->bat
.properties
= energy_battery_props
;
569 battery
->bat
.num_properties
=
570 ARRAY_SIZE(energy_battery_props
);
573 battery
->bat
.name
= acpi_device_bid(battery
->device
);
574 battery
->bat
.type
= POWER_SUPPLY_TYPE_BATTERY
;
575 battery
->bat
.get_property
= acpi_battery_get_property
;
577 result
= power_supply_register(&battery
->device
->dev
, &battery
->bat
);
580 return device_create_file(battery
->bat
.dev
, &alarm_attr
);
583 static void sysfs_remove_battery(struct acpi_battery
*battery
)
585 mutex_lock(&battery
->sysfs_lock
);
586 if (!battery
->bat
.dev
) {
587 mutex_unlock(&battery
->sysfs_lock
);
591 device_remove_file(battery
->bat
.dev
, &alarm_attr
);
592 power_supply_unregister(&battery
->bat
);
593 battery
->bat
.dev
= NULL
;
594 mutex_unlock(&battery
->sysfs_lock
);
597 static void find_battery(const struct dmi_header
*dm
, void *private)
599 struct acpi_battery
*battery
= (struct acpi_battery
*)private;
600 /* Note: the hardcoded offsets below have been extracted from
601 the source code of dmidecode. */
602 if (dm
->type
== DMI_ENTRY_PORTABLE_BATTERY
&& dm
->length
>= 8) {
603 const u8
*dmi_data
= (const u8
*)(dm
+ 1);
604 int dmi_capacity
= get_unaligned((const u16
*)(dmi_data
+ 6));
605 if (dm
->length
>= 18)
606 dmi_capacity
*= dmi_data
[17];
607 if (battery
->design_capacity
* battery
->design_voltage
/ 1000
609 battery
->design_capacity
* 10 == dmi_capacity
)
610 set_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH
,
616 * According to the ACPI spec, some kinds of primary batteries can
617 * report percentage battery remaining capacity directly to OS.
618 * In this case, it reports the Last Full Charged Capacity == 100
619 * and BatteryPresentRate == 0xFFFFFFFF.
621 * Now we found some battery reports percentage remaining capacity
622 * even if it's rechargeable.
623 * https://bugzilla.kernel.org/show_bug.cgi?id=15979
625 * Handle this correctly so that they won't break userspace.
627 static void acpi_battery_quirks(struct acpi_battery
*battery
)
629 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY
, &battery
->flags
))
632 if (battery
->full_charge_capacity
== 100 &&
633 battery
->rate_now
== ACPI_BATTERY_VALUE_UNKNOWN
&&
634 battery
->capacity_now
>= 0 && battery
->capacity_now
<= 100) {
635 set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY
, &battery
->flags
);
636 battery
->full_charge_capacity
= battery
->design_capacity
;
637 battery
->capacity_now
= (battery
->capacity_now
*
638 battery
->full_charge_capacity
) / 100;
641 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH
, &battery
->flags
))
644 if (battery
->power_unit
&& dmi_name_in_vendors("LENOVO")) {
646 s
= dmi_get_system_info(DMI_PRODUCT_VERSION
);
647 if (s
&& !strnicmp(s
, "ThinkPad", 8)) {
648 dmi_walk(find_battery
, battery
);
649 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH
,
651 battery
->design_voltage
) {
652 battery
->design_capacity
=
653 battery
->design_capacity
*
654 10000 / battery
->design_voltage
;
655 battery
->full_charge_capacity
=
656 battery
->full_charge_capacity
*
657 10000 / battery
->design_voltage
;
658 battery
->design_capacity_warning
=
659 battery
->design_capacity_warning
*
660 10000 / battery
->design_voltage
;
661 battery
->capacity_now
= battery
->capacity_now
*
662 10000 / battery
->design_voltage
;
668 static int acpi_battery_update(struct acpi_battery
*battery
)
670 int result
, old_present
= acpi_battery_present(battery
);
671 result
= acpi_battery_get_status(battery
);
674 if (!acpi_battery_present(battery
)) {
675 sysfs_remove_battery(battery
);
676 battery
->update_time
= 0;
679 if (!battery
->update_time
||
680 old_present
!= acpi_battery_present(battery
)) {
681 result
= acpi_battery_get_info(battery
);
684 acpi_battery_init_alarm(battery
);
686 if (!battery
->bat
.dev
) {
687 result
= sysfs_add_battery(battery
);
691 result
= acpi_battery_get_state(battery
);
692 acpi_battery_quirks(battery
);
696 static void acpi_battery_refresh(struct acpi_battery
*battery
)
700 if (!battery
->bat
.dev
)
703 power_unit
= battery
->power_unit
;
705 acpi_battery_get_info(battery
);
707 if (power_unit
== battery
->power_unit
)
710 /* The battery has changed its reporting units. */
711 sysfs_remove_battery(battery
);
712 sysfs_add_battery(battery
);
715 /* --------------------------------------------------------------------------
717 -------------------------------------------------------------------------- */
719 static void acpi_battery_notify(struct acpi_device
*device
, u32 event
)
721 struct acpi_battery
*battery
= acpi_driver_data(device
);
726 old
= battery
->bat
.dev
;
727 if (event
== ACPI_BATTERY_NOTIFY_INFO
)
728 acpi_battery_refresh(battery
);
729 acpi_battery_update(battery
);
730 acpi_bus_generate_netlink_event(device
->pnp
.device_class
,
731 dev_name(&device
->dev
), event
,
732 acpi_battery_present(battery
));
733 /* acpi_battery_update could remove power_supply object */
734 if (old
&& battery
->bat
.dev
)
735 power_supply_changed(&battery
->bat
);
738 static int battery_notify(struct notifier_block
*nb
,
739 unsigned long mode
, void *_unused
)
741 struct acpi_battery
*battery
= container_of(nb
, struct acpi_battery
,
744 case PM_POST_HIBERNATION
:
745 case PM_POST_SUSPEND
:
746 if (battery
->bat
.dev
) {
747 sysfs_remove_battery(battery
);
748 sysfs_add_battery(battery
);
756 static int acpi_battery_add(struct acpi_device
*device
)
759 struct acpi_battery
*battery
= NULL
;
763 battery
= kzalloc(sizeof(struct acpi_battery
), GFP_KERNEL
);
766 battery
->device
= device
;
767 strcpy(acpi_device_name(device
), ACPI_BATTERY_DEVICE_NAME
);
768 strcpy(acpi_device_class(device
), ACPI_BATTERY_CLASS
);
769 device
->driver_data
= battery
;
770 mutex_init(&battery
->lock
);
771 mutex_init(&battery
->sysfs_lock
);
772 if (acpi_has_method(battery
->device
->handle
, "_BIX"))
773 set_bit(ACPI_BATTERY_XINFO_PRESENT
, &battery
->flags
);
774 result
= acpi_battery_update(battery
);
778 printk(KERN_INFO PREFIX
"%s Slot [%s] (battery %s)\n",
779 ACPI_BATTERY_DEVICE_NAME
, acpi_device_bid(device
),
780 device
->status
.battery_present
? "present" : "absent");
782 battery
->pm_nb
.notifier_call
= battery_notify
;
783 register_pm_notifier(&battery
->pm_nb
);
788 sysfs_remove_battery(battery
);
789 mutex_destroy(&battery
->lock
);
790 mutex_destroy(&battery
->sysfs_lock
);
795 static int acpi_battery_remove(struct acpi_device
*device
)
797 struct acpi_battery
*battery
= NULL
;
799 if (!device
|| !acpi_driver_data(device
))
801 battery
= acpi_driver_data(device
);
802 unregister_pm_notifier(&battery
->pm_nb
);
803 sysfs_remove_battery(battery
);
804 mutex_destroy(&battery
->lock
);
805 mutex_destroy(&battery
->sysfs_lock
);
810 #ifdef CONFIG_PM_SLEEP
811 /* this is needed to learn about changes made in suspended state */
812 static int acpi_battery_resume(struct device
*dev
)
814 struct acpi_battery
*battery
;
819 battery
= acpi_driver_data(to_acpi_device(dev
));
823 battery
->update_time
= 0;
824 acpi_battery_update(battery
);
829 static SIMPLE_DEV_PM_OPS(acpi_battery_pm
, NULL
, acpi_battery_resume
);
831 static struct acpi_driver acpi_battery_driver
= {
833 .class = ACPI_BATTERY_CLASS
,
834 .ids
= battery_device_ids
,
835 .flags
= ACPI_DRIVER_ALL_NOTIFY_EVENTS
,
837 .add
= acpi_battery_add
,
838 .remove
= acpi_battery_remove
,
839 .notify
= acpi_battery_notify
,
841 .drv
.pm
= &acpi_battery_pm
,
844 static void __init
acpi_battery_init_async(void *unused
, async_cookie_t cookie
)
848 acpi_bus_register_driver(&acpi_battery_driver
);
851 static int __init
acpi_battery_init(void)
853 async_schedule(acpi_battery_init_async
, NULL
);
857 static void __exit
acpi_battery_exit(void)
859 acpi_bus_unregister_driver(&acpi_battery_driver
);
862 module_init(acpi_battery_init
);
863 module_exit(acpi_battery_exit
);