2 * battery.c - ACPI Battery Driver (Revision: 2.0)
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 <acpi/acpi_bus.h>
40 #include <acpi/acpi_drivers.h>
41 #include <linux/power_supply.h>
43 #define PREFIX "ACPI: "
45 #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
47 #define ACPI_BATTERY_CLASS "battery"
48 #define ACPI_BATTERY_DEVICE_NAME "Battery"
49 #define ACPI_BATTERY_NOTIFY_STATUS 0x80
50 #define ACPI_BATTERY_NOTIFY_INFO 0x81
51 #define ACPI_BATTERY_NOTIFY_THRESHOLD 0x82
53 /* Battery power unit: 0 means mW, 1 means mA */
54 #define ACPI_BATTERY_POWER_UNIT_MA 1
56 #define _COMPONENT ACPI_BATTERY_COMPONENT
58 ACPI_MODULE_NAME("battery");
60 MODULE_AUTHOR("Paul Diefenbaugh");
62 MODULE_DESCRIPTION("ACPI Battery Driver");
63 MODULE_LICENSE("GPL");
65 static int battery_bix_broken_package;
66 static unsigned int cache_time = 1000;
67 module_param(cache_time, uint, 0644);
68 MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
70 static const struct acpi_device_id battery_device_ids[] = {
75 MODULE_DEVICE_TABLE(acpi, battery_device_ids);
78 ACPI_BATTERY_ALARM_PRESENT,
79 ACPI_BATTERY_XINFO_PRESENT,
80 ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY,
81 /* On Lenovo Thinkpad models from 2010 and 2011, the power unit
82 switches between mWh and mAh depending on whether the system
83 is running on battery or not. When mAh is the unit, most
84 reported values are incorrect and need to be adjusted by
85 10000/design_voltage. Verified on x201, t410, t410s, and x220.
86 Pre-2010 and 2012 models appear to always report in mWh and
87 are thus unaffected (tested with t42, t61, t500, x200, x300,
88 and x230). Also, in mid-2012 Lenovo issued a BIOS update for
89 the 2011 models that fixes the issue (tested on x220 with a
90 post-1.29 BIOS), but as of Nov. 2012, no such update is
91 available for the 2010 models. */
92 ACPI_BATTERY_QUIRK_THINKPAD_MAH,
97 struct mutex sysfs_lock;
98 struct power_supply bat;
99 struct acpi_device *device;
100 struct notifier_block pm_nb;
101 unsigned long update_time;
107 int full_charge_capacity;
110 int design_capacity_warning;
111 int design_capacity_low;
113 int measurement_accuracy;
114 int max_sampling_time;
115 int min_sampling_time;
116 int max_averaging_interval;
117 int min_averaging_interval;
118 int capacity_granularity_1;
119 int capacity_granularity_2;
121 char model_number[32];
122 char serial_number[32];
130 #define to_acpi_battery(x) container_of(x, struct acpi_battery, bat)
132 static inline int acpi_battery_present(struct acpi_battery *battery)
134 return battery->device->status.battery_present;
137 static int acpi_battery_technology(struct acpi_battery *battery)
139 if (!strcasecmp("NiCd", battery->type))
140 return POWER_SUPPLY_TECHNOLOGY_NiCd;
141 if (!strcasecmp("NiMH", battery->type))
142 return POWER_SUPPLY_TECHNOLOGY_NiMH;
143 if (!strcasecmp("LION", battery->type))
144 return POWER_SUPPLY_TECHNOLOGY_LION;
145 if (!strncasecmp("LI-ION", battery->type, 6))
146 return POWER_SUPPLY_TECHNOLOGY_LION;
147 if (!strcasecmp("LiP", battery->type))
148 return POWER_SUPPLY_TECHNOLOGY_LIPO;
149 return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
152 static int acpi_battery_get_state(struct acpi_battery *battery);
154 static int acpi_battery_is_charged(struct acpi_battery *battery)
156 /* either charging or discharging */
157 if (battery->state != 0)
160 /* battery not reporting charge */
161 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
162 battery->capacity_now == 0)
165 /* good batteries update full_charge as the batteries degrade */
166 if (battery->full_charge_capacity == battery->capacity_now)
169 /* fallback to using design values for broken batteries */
170 if (battery->design_capacity == battery->capacity_now)
173 /* we don't do any sort of metric based on percentages */
177 static int acpi_battery_get_property(struct power_supply *psy,
178 enum power_supply_property psp,
179 union power_supply_propval *val)
182 struct acpi_battery *battery = to_acpi_battery(psy);
184 if (acpi_battery_present(battery)) {
185 /* run battery update only if it is present */
186 acpi_battery_get_state(battery);
187 } else if (psp != POWER_SUPPLY_PROP_PRESENT)
190 case POWER_SUPPLY_PROP_STATUS:
191 if (battery->state & 0x01)
192 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
193 else if (battery->state & 0x02)
194 val->intval = POWER_SUPPLY_STATUS_CHARGING;
195 else if (acpi_battery_is_charged(battery))
196 val->intval = POWER_SUPPLY_STATUS_FULL;
198 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
200 case POWER_SUPPLY_PROP_PRESENT:
201 val->intval = acpi_battery_present(battery);
203 case POWER_SUPPLY_PROP_TECHNOLOGY:
204 val->intval = acpi_battery_technology(battery);
206 case POWER_SUPPLY_PROP_CYCLE_COUNT:
207 val->intval = battery->cycle_count;
209 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
210 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
213 val->intval = battery->design_voltage * 1000;
215 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
216 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
219 val->intval = battery->voltage_now * 1000;
221 case POWER_SUPPLY_PROP_CURRENT_NOW:
222 case POWER_SUPPLY_PROP_POWER_NOW:
223 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
226 val->intval = battery->rate_now * 1000;
228 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
229 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
230 if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
233 val->intval = battery->design_capacity * 1000;
235 case POWER_SUPPLY_PROP_CHARGE_FULL:
236 case POWER_SUPPLY_PROP_ENERGY_FULL:
237 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
240 val->intval = battery->full_charge_capacity * 1000;
242 case POWER_SUPPLY_PROP_CHARGE_NOW:
243 case POWER_SUPPLY_PROP_ENERGY_NOW:
244 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
247 val->intval = battery->capacity_now * 1000;
249 case POWER_SUPPLY_PROP_CAPACITY:
250 if (battery->capacity_now && battery->full_charge_capacity)
251 val->intval = battery->capacity_now * 100/
252 battery->full_charge_capacity;
256 case POWER_SUPPLY_PROP_MODEL_NAME:
257 val->strval = battery->model_number;
259 case POWER_SUPPLY_PROP_MANUFACTURER:
260 val->strval = battery->oem_info;
262 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
263 val->strval = battery->serial_number;
271 static enum power_supply_property charge_battery_props[] = {
272 POWER_SUPPLY_PROP_STATUS,
273 POWER_SUPPLY_PROP_PRESENT,
274 POWER_SUPPLY_PROP_TECHNOLOGY,
275 POWER_SUPPLY_PROP_CYCLE_COUNT,
276 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
277 POWER_SUPPLY_PROP_VOLTAGE_NOW,
278 POWER_SUPPLY_PROP_CURRENT_NOW,
279 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
280 POWER_SUPPLY_PROP_CHARGE_FULL,
281 POWER_SUPPLY_PROP_CHARGE_NOW,
282 POWER_SUPPLY_PROP_CAPACITY,
283 POWER_SUPPLY_PROP_MODEL_NAME,
284 POWER_SUPPLY_PROP_MANUFACTURER,
285 POWER_SUPPLY_PROP_SERIAL_NUMBER,
288 static enum power_supply_property energy_battery_props[] = {
289 POWER_SUPPLY_PROP_STATUS,
290 POWER_SUPPLY_PROP_PRESENT,
291 POWER_SUPPLY_PROP_TECHNOLOGY,
292 POWER_SUPPLY_PROP_CYCLE_COUNT,
293 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
294 POWER_SUPPLY_PROP_VOLTAGE_NOW,
295 POWER_SUPPLY_PROP_POWER_NOW,
296 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
297 POWER_SUPPLY_PROP_ENERGY_FULL,
298 POWER_SUPPLY_PROP_ENERGY_NOW,
299 POWER_SUPPLY_PROP_CAPACITY,
300 POWER_SUPPLY_PROP_MODEL_NAME,
301 POWER_SUPPLY_PROP_MANUFACTURER,
302 POWER_SUPPLY_PROP_SERIAL_NUMBER,
305 /* --------------------------------------------------------------------------
307 -------------------------------------------------------------------------- */
308 struct acpi_offsets {
309 size_t offset; /* offset inside struct acpi_sbs_battery */
310 u8 mode; /* int or string? */
313 static struct acpi_offsets state_offsets[] = {
314 {offsetof(struct acpi_battery, state), 0},
315 {offsetof(struct acpi_battery, rate_now), 0},
316 {offsetof(struct acpi_battery, capacity_now), 0},
317 {offsetof(struct acpi_battery, voltage_now), 0},
320 static struct acpi_offsets info_offsets[] = {
321 {offsetof(struct acpi_battery, power_unit), 0},
322 {offsetof(struct acpi_battery, design_capacity), 0},
323 {offsetof(struct acpi_battery, full_charge_capacity), 0},
324 {offsetof(struct acpi_battery, technology), 0},
325 {offsetof(struct acpi_battery, design_voltage), 0},
326 {offsetof(struct acpi_battery, design_capacity_warning), 0},
327 {offsetof(struct acpi_battery, design_capacity_low), 0},
328 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
329 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
330 {offsetof(struct acpi_battery, model_number), 1},
331 {offsetof(struct acpi_battery, serial_number), 1},
332 {offsetof(struct acpi_battery, type), 1},
333 {offsetof(struct acpi_battery, oem_info), 1},
336 static struct acpi_offsets extended_info_offsets[] = {
337 {offsetof(struct acpi_battery, revision), 0},
338 {offsetof(struct acpi_battery, power_unit), 0},
339 {offsetof(struct acpi_battery, design_capacity), 0},
340 {offsetof(struct acpi_battery, full_charge_capacity), 0},
341 {offsetof(struct acpi_battery, technology), 0},
342 {offsetof(struct acpi_battery, design_voltage), 0},
343 {offsetof(struct acpi_battery, design_capacity_warning), 0},
344 {offsetof(struct acpi_battery, design_capacity_low), 0},
345 {offsetof(struct acpi_battery, cycle_count), 0},
346 {offsetof(struct acpi_battery, measurement_accuracy), 0},
347 {offsetof(struct acpi_battery, max_sampling_time), 0},
348 {offsetof(struct acpi_battery, min_sampling_time), 0},
349 {offsetof(struct acpi_battery, max_averaging_interval), 0},
350 {offsetof(struct acpi_battery, min_averaging_interval), 0},
351 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
352 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
353 {offsetof(struct acpi_battery, model_number), 1},
354 {offsetof(struct acpi_battery, serial_number), 1},
355 {offsetof(struct acpi_battery, type), 1},
356 {offsetof(struct acpi_battery, oem_info), 1},
359 static int extract_package(struct acpi_battery *battery,
360 union acpi_object *package,
361 struct acpi_offsets *offsets, int num)
364 union acpi_object *element;
365 if (package->type != ACPI_TYPE_PACKAGE)
367 for (i = 0; i < num; ++i) {
368 if (package->package.count <= i)
370 element = &package->package.elements[i];
371 if (offsets[i].mode) {
372 u8 *ptr = (u8 *)battery + offsets[i].offset;
373 if (element->type == ACPI_TYPE_STRING ||
374 element->type == ACPI_TYPE_BUFFER)
375 strncpy(ptr, element->string.pointer, 32);
376 else if (element->type == ACPI_TYPE_INTEGER) {
377 strncpy(ptr, (u8 *)&element->integer.value,
379 ptr[sizeof(u64)] = 0;
381 *ptr = 0; /* don't have value */
383 int *x = (int *)((u8 *)battery + offsets[i].offset);
384 *x = (element->type == ACPI_TYPE_INTEGER) ?
385 element->integer.value : -1;
391 static int acpi_battery_get_status(struct acpi_battery *battery)
393 if (acpi_bus_get_status(battery->device)) {
394 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
400 static int acpi_battery_get_info(struct acpi_battery *battery)
402 int result = -EFAULT;
403 acpi_status status = 0;
404 char *name = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags) ?
407 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
409 if (!acpi_battery_present(battery))
411 mutex_lock(&battery->lock);
412 status = acpi_evaluate_object(battery->device->handle, name,
414 mutex_unlock(&battery->lock);
416 if (ACPI_FAILURE(status)) {
417 ACPI_EXCEPTION((AE_INFO, status, "Evaluating %s", name));
421 if (battery_bix_broken_package)
422 result = extract_package(battery, buffer.pointer,
423 extended_info_offsets + 1,
424 ARRAY_SIZE(extended_info_offsets) - 1);
425 else if (test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags))
426 result = extract_package(battery, buffer.pointer,
427 extended_info_offsets,
428 ARRAY_SIZE(extended_info_offsets));
430 result = extract_package(battery, buffer.pointer,
431 info_offsets, ARRAY_SIZE(info_offsets));
432 kfree(buffer.pointer);
433 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
434 battery->full_charge_capacity = battery->design_capacity;
435 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) &&
436 battery->power_unit && battery->design_voltage) {
437 battery->design_capacity = battery->design_capacity *
438 10000 / battery->design_voltage;
439 battery->full_charge_capacity = battery->full_charge_capacity *
440 10000 / battery->design_voltage;
441 battery->design_capacity_warning =
442 battery->design_capacity_warning *
443 10000 / battery->design_voltage;
444 /* Curiously, design_capacity_low, unlike the rest of them,
446 /* capacity_granularity_* equal 1 on the systems tested, so
447 it's impossible to tell if they would need an adjustment
448 or not if their values were higher. */
453 static int acpi_battery_get_state(struct acpi_battery *battery)
456 acpi_status status = 0;
457 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
459 if (!acpi_battery_present(battery))
462 if (battery->update_time &&
463 time_before(jiffies, battery->update_time +
464 msecs_to_jiffies(cache_time)))
467 mutex_lock(&battery->lock);
468 status = acpi_evaluate_object(battery->device->handle, "_BST",
470 mutex_unlock(&battery->lock);
472 if (ACPI_FAILURE(status)) {
473 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
477 result = extract_package(battery, buffer.pointer,
478 state_offsets, ARRAY_SIZE(state_offsets));
479 battery->update_time = jiffies;
480 kfree(buffer.pointer);
482 /* For buggy DSDTs that report negative 16-bit values for either
483 * charging or discharging current and/or report 0 as 65536
486 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA &&
487 battery->rate_now != ACPI_BATTERY_VALUE_UNKNOWN &&
488 (s16)(battery->rate_now) < 0) {
489 battery->rate_now = abs((s16)battery->rate_now);
490 printk_once(KERN_WARNING FW_BUG "battery: (dis)charge rate"
494 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)
495 && battery->capacity_now >= 0 && battery->capacity_now <= 100)
496 battery->capacity_now = (battery->capacity_now *
497 battery->full_charge_capacity) / 100;
498 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) &&
499 battery->power_unit && battery->design_voltage) {
500 battery->capacity_now = battery->capacity_now *
501 10000 / battery->design_voltage;
506 static int acpi_battery_set_alarm(struct acpi_battery *battery)
508 acpi_status status = 0;
510 if (!acpi_battery_present(battery) ||
511 !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags))
514 mutex_lock(&battery->lock);
515 status = acpi_execute_simple_method(battery->device->handle, "_BTP",
517 mutex_unlock(&battery->lock);
519 if (ACPI_FAILURE(status))
522 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm));
526 static int acpi_battery_init_alarm(struct acpi_battery *battery)
528 /* See if alarms are supported, and if so, set default */
529 if (!acpi_has_method(battery->device->handle, "_BTP")) {
530 clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
533 set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
535 battery->alarm = battery->design_capacity_warning;
536 return acpi_battery_set_alarm(battery);
539 static ssize_t acpi_battery_alarm_show(struct device *dev,
540 struct device_attribute *attr,
543 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
544 return sprintf(buf, "%d\n", battery->alarm * 1000);
547 static ssize_t acpi_battery_alarm_store(struct device *dev,
548 struct device_attribute *attr,
549 const char *buf, size_t count)
552 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
553 if (sscanf(buf, "%ld\n", &x) == 1)
554 battery->alarm = x/1000;
555 if (acpi_battery_present(battery))
556 acpi_battery_set_alarm(battery);
560 static struct device_attribute alarm_attr = {
561 .attr = {.name = "alarm", .mode = 0644},
562 .show = acpi_battery_alarm_show,
563 .store = acpi_battery_alarm_store,
566 static int sysfs_add_battery(struct acpi_battery *battery)
570 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) {
571 battery->bat.properties = charge_battery_props;
572 battery->bat.num_properties =
573 ARRAY_SIZE(charge_battery_props);
575 battery->bat.properties = energy_battery_props;
576 battery->bat.num_properties =
577 ARRAY_SIZE(energy_battery_props);
580 battery->bat.name = acpi_device_bid(battery->device);
581 battery->bat.type = POWER_SUPPLY_TYPE_BATTERY;
582 battery->bat.get_property = acpi_battery_get_property;
584 result = power_supply_register(&battery->device->dev, &battery->bat);
587 return device_create_file(battery->bat.dev, &alarm_attr);
590 static void sysfs_remove_battery(struct acpi_battery *battery)
592 mutex_lock(&battery->sysfs_lock);
593 if (!battery->bat.dev) {
594 mutex_unlock(&battery->sysfs_lock);
598 device_remove_file(battery->bat.dev, &alarm_attr);
599 power_supply_unregister(&battery->bat);
600 battery->bat.dev = NULL;
601 mutex_unlock(&battery->sysfs_lock);
604 static void find_battery(const struct dmi_header *dm, void *private)
606 struct acpi_battery *battery = (struct acpi_battery *)private;
607 /* Note: the hardcoded offsets below have been extracted from
608 the source code of dmidecode. */
609 if (dm->type == DMI_ENTRY_PORTABLE_BATTERY && dm->length >= 8) {
610 const u8 *dmi_data = (const u8 *)(dm + 1);
611 int dmi_capacity = get_unaligned((const u16 *)(dmi_data + 6));
612 if (dm->length >= 18)
613 dmi_capacity *= dmi_data[17];
614 if (battery->design_capacity * battery->design_voltage / 1000
616 battery->design_capacity * 10 == dmi_capacity)
617 set_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH,
623 * According to the ACPI spec, some kinds of primary batteries can
624 * report percentage battery remaining capacity directly to OS.
625 * In this case, it reports the Last Full Charged Capacity == 100
626 * and BatteryPresentRate == 0xFFFFFFFF.
628 * Now we found some battery reports percentage remaining capacity
629 * even if it's rechargeable.
630 * https://bugzilla.kernel.org/show_bug.cgi?id=15979
632 * Handle this correctly so that they won't break userspace.
634 static void acpi_battery_quirks(struct acpi_battery *battery)
636 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
639 if (battery->full_charge_capacity == 100 &&
640 battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN &&
641 battery->capacity_now >= 0 && battery->capacity_now <= 100) {
642 set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags);
643 battery->full_charge_capacity = battery->design_capacity;
644 battery->capacity_now = (battery->capacity_now *
645 battery->full_charge_capacity) / 100;
648 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags))
651 if (battery->power_unit && dmi_name_in_vendors("LENOVO")) {
653 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
654 if (s && !strnicmp(s, "ThinkPad", 8)) {
655 dmi_walk(find_battery, battery);
656 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH,
658 battery->design_voltage) {
659 battery->design_capacity =
660 battery->design_capacity *
661 10000 / battery->design_voltage;
662 battery->full_charge_capacity =
663 battery->full_charge_capacity *
664 10000 / battery->design_voltage;
665 battery->design_capacity_warning =
666 battery->design_capacity_warning *
667 10000 / battery->design_voltage;
668 battery->capacity_now = battery->capacity_now *
669 10000 / battery->design_voltage;
675 static int acpi_battery_update(struct acpi_battery *battery)
677 int result, old_present = acpi_battery_present(battery);
678 result = acpi_battery_get_status(battery);
681 if (!acpi_battery_present(battery)) {
682 sysfs_remove_battery(battery);
683 battery->update_time = 0;
686 if (!battery->update_time ||
687 old_present != acpi_battery_present(battery)) {
688 result = acpi_battery_get_info(battery);
691 acpi_battery_init_alarm(battery);
693 if (!battery->bat.dev) {
694 result = sysfs_add_battery(battery);
698 result = acpi_battery_get_state(battery);
699 acpi_battery_quirks(battery);
703 static void acpi_battery_refresh(struct acpi_battery *battery)
707 if (!battery->bat.dev)
710 power_unit = battery->power_unit;
712 acpi_battery_get_info(battery);
714 if (power_unit == battery->power_unit)
717 /* The battery has changed its reporting units. */
718 sysfs_remove_battery(battery);
719 sysfs_add_battery(battery);
722 /* --------------------------------------------------------------------------
724 -------------------------------------------------------------------------- */
726 static void acpi_battery_notify(struct acpi_device *device, u32 event)
728 struct acpi_battery *battery = acpi_driver_data(device);
733 old = battery->bat.dev;
734 if (event == ACPI_BATTERY_NOTIFY_INFO)
735 acpi_battery_refresh(battery);
736 acpi_battery_update(battery);
737 acpi_bus_generate_netlink_event(device->pnp.device_class,
738 dev_name(&device->dev), event,
739 acpi_battery_present(battery));
740 /* acpi_battery_update could remove power_supply object */
741 if (old && battery->bat.dev)
742 power_supply_changed(&battery->bat);
745 static int battery_notify(struct notifier_block *nb,
746 unsigned long mode, void *_unused)
748 struct acpi_battery *battery = container_of(nb, struct acpi_battery,
751 case PM_POST_HIBERNATION:
752 case PM_POST_SUSPEND:
753 if (battery->bat.dev) {
754 sysfs_remove_battery(battery);
755 sysfs_add_battery(battery);
763 static struct dmi_system_id bat_dmi_table[] = {
765 .ident = "NEC LZ750/LS",
767 DMI_MATCH(DMI_SYS_VENDOR, "NEC"),
768 DMI_MATCH(DMI_PRODUCT_NAME, "PC-LZ750LS"),
774 static int acpi_battery_add(struct acpi_device *device)
777 struct acpi_battery *battery = NULL;
781 battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
784 battery->device = device;
785 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
786 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
787 device->driver_data = battery;
788 mutex_init(&battery->lock);
789 mutex_init(&battery->sysfs_lock);
790 if (acpi_has_method(battery->device->handle, "_BIX"))
791 set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
792 result = acpi_battery_update(battery);
796 printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
797 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
798 device->status.battery_present ? "present" : "absent");
800 battery->pm_nb.notifier_call = battery_notify;
801 register_pm_notifier(&battery->pm_nb);
806 sysfs_remove_battery(battery);
807 mutex_destroy(&battery->lock);
808 mutex_destroy(&battery->sysfs_lock);
813 static int acpi_battery_remove(struct acpi_device *device)
815 struct acpi_battery *battery = NULL;
817 if (!device || !acpi_driver_data(device))
819 battery = acpi_driver_data(device);
820 unregister_pm_notifier(&battery->pm_nb);
821 sysfs_remove_battery(battery);
822 mutex_destroy(&battery->lock);
823 mutex_destroy(&battery->sysfs_lock);
828 #ifdef CONFIG_PM_SLEEP
829 /* this is needed to learn about changes made in suspended state */
830 static int acpi_battery_resume(struct device *dev)
832 struct acpi_battery *battery;
837 battery = acpi_driver_data(to_acpi_device(dev));
841 battery->update_time = 0;
842 acpi_battery_update(battery);
847 static SIMPLE_DEV_PM_OPS(acpi_battery_pm, NULL, acpi_battery_resume);
849 static struct acpi_driver acpi_battery_driver = {
851 .class = ACPI_BATTERY_CLASS,
852 .ids = battery_device_ids,
853 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
855 .add = acpi_battery_add,
856 .remove = acpi_battery_remove,
857 .notify = acpi_battery_notify,
859 .drv.pm = &acpi_battery_pm,
862 static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
867 if (dmi_check_system(bat_dmi_table))
868 battery_bix_broken_package = 1;
869 acpi_bus_register_driver(&acpi_battery_driver);
872 static int __init acpi_battery_init(void)
874 async_schedule(acpi_battery_init_async, NULL);
878 static void __exit acpi_battery_exit(void)
880 acpi_bus_unregister_driver(&acpi_battery_driver);
883 module_init(acpi_battery_init);
884 module_exit(acpi_battery_exit);