Lines Matching refs:battery

134 static inline int acpi_battery_present(struct acpi_battery *battery)  in acpi_battery_present()  argument
136 return battery->device->status.battery_present; in acpi_battery_present()
139 static int acpi_battery_technology(struct acpi_battery *battery) in acpi_battery_technology() argument
141 if (!strcasecmp("NiCd", battery->type)) in acpi_battery_technology()
143 if (!strcasecmp("NiMH", battery->type)) in acpi_battery_technology()
145 if (!strcasecmp("LION", battery->type)) in acpi_battery_technology()
147 if (!strncasecmp("LI-ION", battery->type, 6)) in acpi_battery_technology()
149 if (!strcasecmp("LiP", battery->type)) in acpi_battery_technology()
154 static int acpi_battery_get_state(struct acpi_battery *battery);
156 static int acpi_battery_is_charged(struct acpi_battery *battery) in acpi_battery_is_charged() argument
159 if (battery->state != 0) in acpi_battery_is_charged()
163 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN || in acpi_battery_is_charged()
164 battery->capacity_now == 0) in acpi_battery_is_charged()
168 if (battery->full_charge_capacity == battery->capacity_now) in acpi_battery_is_charged()
172 if (battery->design_capacity <= battery->capacity_now) in acpi_battery_is_charged()
179 static bool acpi_battery_is_degraded(struct acpi_battery *battery) in acpi_battery_is_degraded() argument
181 return ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity) && in acpi_battery_is_degraded()
182 ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity) && in acpi_battery_is_degraded()
183 battery->full_charge_capacity < battery->design_capacity; in acpi_battery_is_degraded()
186 static int acpi_battery_handle_discharging(struct acpi_battery *battery) in acpi_battery_handle_discharging() argument
194 battery->rate_now == 0) in acpi_battery_handle_discharging()
205 struct acpi_battery *battery = to_acpi_battery(psy); in acpi_battery_get_property() local
207 if (acpi_battery_present(battery)) { in acpi_battery_get_property()
209 acpi_battery_get_state(battery); in acpi_battery_get_property()
214 if (battery->state & ACPI_BATTERY_STATE_DISCHARGING) in acpi_battery_get_property()
215 val->intval = acpi_battery_handle_discharging(battery); in acpi_battery_get_property()
216 else if (battery->state & ACPI_BATTERY_STATE_CHARGING) in acpi_battery_get_property()
218 else if (acpi_battery_is_charged(battery)) in acpi_battery_get_property()
224 val->intval = acpi_battery_present(battery); in acpi_battery_get_property()
227 val->intval = acpi_battery_technology(battery); in acpi_battery_get_property()
230 val->intval = battery->cycle_count; in acpi_battery_get_property()
233 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN) in acpi_battery_get_property()
236 val->intval = battery->design_voltage * 1000; in acpi_battery_get_property()
239 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN) in acpi_battery_get_property()
242 val->intval = battery->voltage_now * 1000; in acpi_battery_get_property()
246 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN) in acpi_battery_get_property()
249 val->intval = battery->rate_now * 1000; in acpi_battery_get_property()
253 if (!ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity)) in acpi_battery_get_property()
256 val->intval = battery->design_capacity * 1000; in acpi_battery_get_property()
260 if (!ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity)) in acpi_battery_get_property()
263 val->intval = battery->full_charge_capacity * 1000; in acpi_battery_get_property()
267 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN) in acpi_battery_get_property()
270 val->intval = battery->capacity_now * 1000; in acpi_battery_get_property()
273 if (ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity)) in acpi_battery_get_property()
274 full_capacity = battery->full_charge_capacity; in acpi_battery_get_property()
275 else if (ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity)) in acpi_battery_get_property()
276 full_capacity = battery->design_capacity; in acpi_battery_get_property()
278 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN || in acpi_battery_get_property()
282 val->intval = battery->capacity_now * 100/ in acpi_battery_get_property()
286 if (battery->state & ACPI_BATTERY_STATE_CRITICAL) in acpi_battery_get_property()
288 else if (test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags) && in acpi_battery_get_property()
289 (battery->capacity_now <= battery->alarm)) in acpi_battery_get_property()
291 else if (acpi_battery_is_charged(battery)) in acpi_battery_get_property()
297 val->strval = battery->model_number; in acpi_battery_get_property()
300 val->strval = battery->oem_info; in acpi_battery_get_property()
303 val->strval = battery->serial_number; in acpi_battery_get_property()
427 static int extract_package(struct acpi_battery *battery, in extract_package() argument
441 u8 *ptr = (u8 *)battery + offsets[i].offset; in extract_package()
453 int *x = (int *)((u8 *)battery + offsets[i].offset); in extract_package()
461 static int acpi_battery_get_status(struct acpi_battery *battery) in acpi_battery_get_status() argument
463 if (acpi_bus_get_status(battery->device)) { in acpi_battery_get_status()
464 acpi_handle_info(battery->device->handle, in acpi_battery_get_status()
473 struct acpi_battery *battery, in extract_battery_info() argument
479 result = extract_package(battery, buffer->pointer, in extract_battery_info()
483 result = extract_package(battery, buffer->pointer, in extract_battery_info()
487 result = extract_package(battery, buffer->pointer, in extract_battery_info()
489 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)) in extract_battery_info()
490 battery->full_charge_capacity = battery->design_capacity; in extract_battery_info()
491 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) && in extract_battery_info()
492 battery->power_unit && battery->design_voltage) { in extract_battery_info()
493 battery->design_capacity = battery->design_capacity * in extract_battery_info()
494 10000 / battery->design_voltage; in extract_battery_info()
495 battery->full_charge_capacity = battery->full_charge_capacity * in extract_battery_info()
496 10000 / battery->design_voltage; in extract_battery_info()
497 battery->design_capacity_warning = in extract_battery_info()
498 battery->design_capacity_warning * in extract_battery_info()
499 10000 / battery->design_voltage; in extract_battery_info()
508 if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags) && in extract_battery_info()
509 battery->capacity_now > battery->full_charge_capacity) in extract_battery_info()
510 battery->capacity_now = battery->full_charge_capacity; in extract_battery_info()
515 static int acpi_battery_get_info(struct acpi_battery *battery) in acpi_battery_get_info() argument
517 const int xinfo = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags); in acpi_battery_get_info()
521 if (!acpi_battery_present(battery)) in acpi_battery_get_info()
529 mutex_lock(&battery->lock); in acpi_battery_get_info()
530 status = acpi_evaluate_object(battery->device->handle, in acpi_battery_get_info()
533 mutex_unlock(&battery->lock); in acpi_battery_get_info()
536 acpi_handle_info(battery->device->handle, in acpi_battery_get_info()
542 battery, in acpi_battery_get_info()
556 static int acpi_battery_get_state(struct acpi_battery *battery) in acpi_battery_get_state() argument
562 if (!acpi_battery_present(battery)) in acpi_battery_get_state()
565 if (battery->update_time && in acpi_battery_get_state()
566 time_before(jiffies, battery->update_time + in acpi_battery_get_state()
570 mutex_lock(&battery->lock); in acpi_battery_get_state()
571 status = acpi_evaluate_object(battery->device->handle, "_BST", in acpi_battery_get_state()
573 mutex_unlock(&battery->lock); in acpi_battery_get_state()
576 acpi_handle_info(battery->device->handle, in acpi_battery_get_state()
582 result = extract_package(battery, buffer.pointer, in acpi_battery_get_state()
584 battery->update_time = jiffies; in acpi_battery_get_state()
591 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA && in acpi_battery_get_state()
592 battery->rate_now != ACPI_BATTERY_VALUE_UNKNOWN && in acpi_battery_get_state()
593 (s16)(battery->rate_now) < 0) { in acpi_battery_get_state()
594 battery->rate_now = abs((s16)battery->rate_now); in acpi_battery_get_state()
598 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags) in acpi_battery_get_state()
599 && battery->capacity_now >= 0 && battery->capacity_now <= 100) in acpi_battery_get_state()
600 battery->capacity_now = (battery->capacity_now * in acpi_battery_get_state()
601 battery->full_charge_capacity) / 100; in acpi_battery_get_state()
602 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) && in acpi_battery_get_state()
603 battery->power_unit && battery->design_voltage) { in acpi_battery_get_state()
604 battery->capacity_now = battery->capacity_now * in acpi_battery_get_state()
605 10000 / battery->design_voltage; in acpi_battery_get_state()
607 if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags) && in acpi_battery_get_state()
608 battery->capacity_now > battery->full_charge_capacity) in acpi_battery_get_state()
609 battery->capacity_now = battery->full_charge_capacity; in acpi_battery_get_state()
614 static int acpi_battery_set_alarm(struct acpi_battery *battery) in acpi_battery_set_alarm() argument
618 if (!acpi_battery_present(battery) || in acpi_battery_set_alarm()
619 !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags)) in acpi_battery_set_alarm()
622 mutex_lock(&battery->lock); in acpi_battery_set_alarm()
623 status = acpi_execute_simple_method(battery->device->handle, "_BTP", in acpi_battery_set_alarm()
624 battery->alarm); in acpi_battery_set_alarm()
625 mutex_unlock(&battery->lock); in acpi_battery_set_alarm()
630 acpi_handle_debug(battery->device->handle, "Alarm set to %d\n", in acpi_battery_set_alarm()
631 battery->alarm); in acpi_battery_set_alarm()
636 static int acpi_battery_init_alarm(struct acpi_battery *battery) in acpi_battery_init_alarm() argument
639 if (!acpi_has_method(battery->device->handle, "_BTP")) { in acpi_battery_init_alarm()
640 clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags); in acpi_battery_init_alarm()
643 set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags); in acpi_battery_init_alarm()
644 if (!battery->alarm) in acpi_battery_init_alarm()
645 battery->alarm = battery->design_capacity_warning; in acpi_battery_init_alarm()
646 return acpi_battery_set_alarm(battery); in acpi_battery_init_alarm()
653 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev)); in acpi_battery_alarm_show() local
655 return sprintf(buf, "%d\n", battery->alarm * 1000); in acpi_battery_alarm_show()
663 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev)); in acpi_battery_alarm_store() local
666 battery->alarm = x/1000; in acpi_battery_alarm_store()
667 if (acpi_battery_present(battery)) in acpi_battery_alarm_store()
668 acpi_battery_set_alarm(battery); in acpi_battery_alarm_store()
693 struct acpi_battery *battery; in __battery_hook_unregister() local
700 list_for_each_entry(battery, &acpi_battery_list, list) { in __battery_hook_unregister()
701 hook->remove_battery(battery->bat); in __battery_hook_unregister()
717 struct acpi_battery *battery; in battery_hook_register() local
728 list_for_each_entry(battery, &acpi_battery_list, list) { in battery_hook_register()
729 if (hook->add_battery(battery->bat)) { in battery_hook_register()
752 static void battery_hook_add_battery(struct acpi_battery *battery) in battery_hook_add_battery() argument
757 INIT_LIST_HEAD(&battery->list); in battery_hook_add_battery()
758 list_add(&battery->list, &acpi_battery_list); in battery_hook_add_battery()
767 if (hook_node->add_battery(battery->bat)) { in battery_hook_add_battery()
780 static void battery_hook_remove_battery(struct acpi_battery *battery) in battery_hook_remove_battery() argument
790 hook->remove_battery(battery->bat); in battery_hook_remove_battery()
793 list_del(&battery->list); in battery_hook_remove_battery()
812 static int sysfs_add_battery(struct acpi_battery *battery) in sysfs_add_battery() argument
814 struct power_supply_config psy_cfg = { .drv_data = battery, }; in sysfs_add_battery()
817 if (!ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity) && in sysfs_add_battery()
818 !ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity)) in sysfs_add_battery()
821 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) { in sysfs_add_battery()
823 battery->bat_desc.properties = in sysfs_add_battery()
825 battery->bat_desc.num_properties = in sysfs_add_battery()
828 battery->bat_desc.properties = charge_battery_props; in sysfs_add_battery()
829 battery->bat_desc.num_properties = in sysfs_add_battery()
834 battery->bat_desc.properties = in sysfs_add_battery()
836 battery->bat_desc.num_properties = in sysfs_add_battery()
839 battery->bat_desc.properties = energy_battery_props; in sysfs_add_battery()
840 battery->bat_desc.num_properties = in sysfs_add_battery()
845 battery->bat_desc.name = acpi_device_bid(battery->device); in sysfs_add_battery()
846 battery->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY; in sysfs_add_battery()
847 battery->bat_desc.get_property = acpi_battery_get_property; in sysfs_add_battery()
849 battery->bat = power_supply_register_no_ws(&battery->device->dev, in sysfs_add_battery()
850 &battery->bat_desc, &psy_cfg); in sysfs_add_battery()
852 if (IS_ERR(battery->bat)) { in sysfs_add_battery()
853 int result = PTR_ERR(battery->bat); in sysfs_add_battery()
855 battery->bat = NULL; in sysfs_add_battery()
858 battery_hook_add_battery(battery); in sysfs_add_battery()
859 return device_create_file(&battery->bat->dev, &alarm_attr); in sysfs_add_battery()
862 static void sysfs_remove_battery(struct acpi_battery *battery) in sysfs_remove_battery() argument
864 mutex_lock(&battery->sysfs_lock); in sysfs_remove_battery()
865 if (!battery->bat) { in sysfs_remove_battery()
866 mutex_unlock(&battery->sysfs_lock); in sysfs_remove_battery()
869 battery_hook_remove_battery(battery); in sysfs_remove_battery()
870 device_remove_file(&battery->bat->dev, &alarm_attr); in sysfs_remove_battery()
871 power_supply_unregister(battery->bat); in sysfs_remove_battery()
872 battery->bat = NULL; in sysfs_remove_battery()
873 mutex_unlock(&battery->sysfs_lock); in sysfs_remove_battery()
878 struct acpi_battery *battery = (struct acpi_battery *)private; in find_battery() local
888 if (battery->design_capacity * battery->design_voltage / 1000 in find_battery()
890 battery->design_capacity * 10 == dmi_capacity) in find_battery()
892 &battery->flags); in find_battery()
908 static void acpi_battery_quirks(struct acpi_battery *battery) in acpi_battery_quirks() argument
910 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)) in acpi_battery_quirks()
913 if (battery->full_charge_capacity == 100 && in acpi_battery_quirks()
914 battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN && in acpi_battery_quirks()
915 battery->capacity_now >= 0 && battery->capacity_now <= 100) { in acpi_battery_quirks()
916 set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags); in acpi_battery_quirks()
917 battery->full_charge_capacity = battery->design_capacity; in acpi_battery_quirks()
918 battery->capacity_now = (battery->capacity_now * in acpi_battery_quirks()
919 battery->full_charge_capacity) / 100; in acpi_battery_quirks()
922 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags)) in acpi_battery_quirks()
925 if (battery->power_unit && dmi_name_in_vendors("LENOVO")) { in acpi_battery_quirks()
930 dmi_walk(find_battery, battery); in acpi_battery_quirks()
932 &battery->flags) && in acpi_battery_quirks()
933 battery->design_voltage) { in acpi_battery_quirks()
934 battery->design_capacity = in acpi_battery_quirks()
935 battery->design_capacity * in acpi_battery_quirks()
936 10000 / battery->design_voltage; in acpi_battery_quirks()
937 battery->full_charge_capacity = in acpi_battery_quirks()
938 battery->full_charge_capacity * in acpi_battery_quirks()
939 10000 / battery->design_voltage; in acpi_battery_quirks()
940 battery->design_capacity_warning = in acpi_battery_quirks()
941 battery->design_capacity_warning * in acpi_battery_quirks()
942 10000 / battery->design_voltage; in acpi_battery_quirks()
943 battery->capacity_now = battery->capacity_now * in acpi_battery_quirks()
944 10000 / battery->design_voltage; in acpi_battery_quirks()
949 if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags)) in acpi_battery_quirks()
952 if (acpi_battery_is_degraded(battery) && in acpi_battery_quirks()
953 battery->capacity_now > battery->full_charge_capacity) { in acpi_battery_quirks()
954 set_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags); in acpi_battery_quirks()
955 battery->capacity_now = battery->full_charge_capacity; in acpi_battery_quirks()
959 static int acpi_battery_update(struct acpi_battery *battery, bool resume) in acpi_battery_update() argument
961 int result = acpi_battery_get_status(battery); in acpi_battery_update()
966 if (!acpi_battery_present(battery)) { in acpi_battery_update()
967 sysfs_remove_battery(battery); in acpi_battery_update()
968 battery->update_time = 0; in acpi_battery_update()
975 if (!battery->update_time) { in acpi_battery_update()
976 result = acpi_battery_get_info(battery); in acpi_battery_update()
979 acpi_battery_init_alarm(battery); in acpi_battery_update()
982 result = acpi_battery_get_state(battery); in acpi_battery_update()
985 acpi_battery_quirks(battery); in acpi_battery_update()
987 if (!battery->bat) { in acpi_battery_update()
988 result = sysfs_add_battery(battery); in acpi_battery_update()
997 if ((battery->state & ACPI_BATTERY_STATE_CRITICAL) || in acpi_battery_update()
998 (test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags) && in acpi_battery_update()
999 (battery->capacity_now <= battery->alarm))) in acpi_battery_update()
1000 acpi_pm_wakeup_event(&battery->device->dev); in acpi_battery_update()
1005 static void acpi_battery_refresh(struct acpi_battery *battery) in acpi_battery_refresh() argument
1009 if (!battery->bat) in acpi_battery_refresh()
1012 power_unit = battery->power_unit; in acpi_battery_refresh()
1014 acpi_battery_get_info(battery); in acpi_battery_refresh()
1016 if (power_unit == battery->power_unit) in acpi_battery_refresh()
1020 sysfs_remove_battery(battery); in acpi_battery_refresh()
1021 sysfs_add_battery(battery); in acpi_battery_refresh()
1027 struct acpi_battery *battery = acpi_driver_data(device); in acpi_battery_notify() local
1030 if (!battery) in acpi_battery_notify()
1032 old = battery->bat; in acpi_battery_notify()
1042 acpi_battery_refresh(battery); in acpi_battery_notify()
1043 acpi_battery_update(battery, false); in acpi_battery_notify()
1046 acpi_battery_present(battery)); in acpi_battery_notify()
1047 acpi_notifier_call_chain(device, event, acpi_battery_present(battery)); in acpi_battery_notify()
1049 if (old && battery->bat) in acpi_battery_notify()
1050 power_supply_changed(battery->bat); in acpi_battery_notify()
1056 struct acpi_battery *battery = container_of(nb, struct acpi_battery, in battery_notify() local
1063 if (!acpi_battery_present(battery)) in battery_notify()
1066 if (battery->bat) { in battery_notify()
1067 acpi_battery_refresh(battery); in battery_notify()
1069 result = acpi_battery_get_info(battery); in battery_notify()
1073 result = sysfs_add_battery(battery); in battery_notify()
1078 acpi_battery_init_alarm(battery); in battery_notify()
1079 acpi_battery_get_state(battery); in battery_notify()
1169 static int acpi_battery_update_retry(struct acpi_battery *battery) in acpi_battery_update_retry() argument
1174 ret = acpi_battery_update(battery, false); in acpi_battery_update_retry()
1186 struct acpi_battery *battery = NULL; in acpi_battery_add() local
1194 battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL); in acpi_battery_add()
1195 if (!battery) in acpi_battery_add()
1197 battery->device = device; in acpi_battery_add()
1200 device->driver_data = battery; in acpi_battery_add()
1201 mutex_init(&battery->lock); in acpi_battery_add()
1202 mutex_init(&battery->sysfs_lock); in acpi_battery_add()
1203 if (acpi_has_method(battery->device->handle, "_BIX")) in acpi_battery_add()
1204 set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags); in acpi_battery_add()
1206 result = acpi_battery_update_retry(battery); in acpi_battery_add()
1213 battery->pm_nb.notifier_call = battery_notify; in acpi_battery_add()
1214 register_pm_notifier(&battery->pm_nb); in acpi_battery_add()
1221 sysfs_remove_battery(battery); in acpi_battery_add()
1222 mutex_destroy(&battery->lock); in acpi_battery_add()
1223 mutex_destroy(&battery->sysfs_lock); in acpi_battery_add()
1224 kfree(battery); in acpi_battery_add()
1230 struct acpi_battery *battery = NULL; in acpi_battery_remove() local
1235 battery = acpi_driver_data(device); in acpi_battery_remove()
1236 unregister_pm_notifier(&battery->pm_nb); in acpi_battery_remove()
1237 sysfs_remove_battery(battery); in acpi_battery_remove()
1238 mutex_destroy(&battery->lock); in acpi_battery_remove()
1239 mutex_destroy(&battery->sysfs_lock); in acpi_battery_remove()
1240 kfree(battery); in acpi_battery_remove()
1248 struct acpi_battery *battery; in acpi_battery_resume() local
1253 battery = acpi_driver_data(to_acpi_device(dev)); in acpi_battery_resume()
1254 if (!battery) in acpi_battery_resume()
1257 battery->update_time = 0; in acpi_battery_resume()
1258 acpi_battery_update(battery, true); in acpi_battery_resume()