Lines Matching refs:battery

132 static inline int acpi_battery_present(struct acpi_battery *battery)  in acpi_battery_present()  argument
134 return battery->device->status.battery_present; in acpi_battery_present()
137 static int acpi_battery_technology(struct acpi_battery *battery) in acpi_battery_technology() argument
139 if (!strcasecmp("NiCd", battery->type)) in acpi_battery_technology()
141 if (!strcasecmp("NiMH", battery->type)) in acpi_battery_technology()
143 if (!strcasecmp("LION", battery->type)) in acpi_battery_technology()
145 if (!strncasecmp("LI-ION", battery->type, 6)) in acpi_battery_technology()
147 if (!strcasecmp("LiP", battery->type)) in acpi_battery_technology()
152 static int acpi_battery_get_state(struct acpi_battery *battery);
154 static int acpi_battery_is_charged(struct acpi_battery *battery) in acpi_battery_is_charged() argument
157 if (battery->state != 0) in acpi_battery_is_charged()
161 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN || in acpi_battery_is_charged()
162 battery->capacity_now == 0) in acpi_battery_is_charged()
166 if (battery->full_charge_capacity == battery->capacity_now) in acpi_battery_is_charged()
170 if (battery->design_capacity <= battery->capacity_now) in acpi_battery_is_charged()
177 static bool acpi_battery_is_degraded(struct acpi_battery *battery) in acpi_battery_is_degraded() argument
179 return ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity) && in acpi_battery_is_degraded()
180 ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity) && in acpi_battery_is_degraded()
181 battery->full_charge_capacity < battery->design_capacity; in acpi_battery_is_degraded()
184 static int acpi_battery_handle_discharging(struct acpi_battery *battery) in acpi_battery_handle_discharging() argument
192 battery->rate_now == 0) in acpi_battery_handle_discharging()
203 struct acpi_battery *battery = to_acpi_battery(psy); in acpi_battery_get_property() local
205 if (acpi_battery_present(battery)) { in acpi_battery_get_property()
207 acpi_battery_get_state(battery); in acpi_battery_get_property()
212 if (battery->state & ACPI_BATTERY_STATE_DISCHARGING) in acpi_battery_get_property()
213 val->intval = acpi_battery_handle_discharging(battery); in acpi_battery_get_property()
214 else if (battery->state & ACPI_BATTERY_STATE_CHARGING) in acpi_battery_get_property()
216 else if (acpi_battery_is_charged(battery)) in acpi_battery_get_property()
222 val->intval = acpi_battery_present(battery); in acpi_battery_get_property()
225 val->intval = acpi_battery_technology(battery); in acpi_battery_get_property()
228 val->intval = battery->cycle_count; in acpi_battery_get_property()
231 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN) in acpi_battery_get_property()
234 val->intval = battery->design_voltage * 1000; in acpi_battery_get_property()
237 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN) in acpi_battery_get_property()
240 val->intval = battery->voltage_now * 1000; in acpi_battery_get_property()
244 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN) in acpi_battery_get_property()
247 val->intval = battery->rate_now * 1000; in acpi_battery_get_property()
251 if (!ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity)) in acpi_battery_get_property()
254 val->intval = battery->design_capacity * 1000; in acpi_battery_get_property()
258 if (!ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity)) in acpi_battery_get_property()
261 val->intval = battery->full_charge_capacity * 1000; in acpi_battery_get_property()
265 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN) in acpi_battery_get_property()
268 val->intval = battery->capacity_now * 1000; in acpi_battery_get_property()
271 if (ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity)) in acpi_battery_get_property()
272 full_capacity = battery->full_charge_capacity; in acpi_battery_get_property()
273 else if (ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity)) in acpi_battery_get_property()
274 full_capacity = battery->design_capacity; in acpi_battery_get_property()
276 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN || in acpi_battery_get_property()
280 val->intval = battery->capacity_now * 100/ in acpi_battery_get_property()
284 if (battery->state & ACPI_BATTERY_STATE_CRITICAL) in acpi_battery_get_property()
286 else if (test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags) && in acpi_battery_get_property()
287 (battery->capacity_now <= battery->alarm)) in acpi_battery_get_property()
289 else if (acpi_battery_is_charged(battery)) in acpi_battery_get_property()
295 val->strval = battery->model_number; in acpi_battery_get_property()
298 val->strval = battery->oem_info; in acpi_battery_get_property()
301 val->strval = battery->serial_number; in acpi_battery_get_property()
425 static int extract_package(struct acpi_battery *battery, in extract_package() argument
439 u8 *ptr = (u8 *)battery + offsets[i].offset; in extract_package()
451 int *x = (int *)((u8 *)battery + offsets[i].offset); in extract_package()
459 static int acpi_battery_get_status(struct acpi_battery *battery) in acpi_battery_get_status() argument
461 if (acpi_bus_get_status(battery->device)) { in acpi_battery_get_status()
462 acpi_handle_info(battery->device->handle, in acpi_battery_get_status()
471 struct acpi_battery *battery, in extract_battery_info() argument
477 result = extract_package(battery, buffer->pointer, in extract_battery_info()
481 result = extract_package(battery, buffer->pointer, in extract_battery_info()
485 result = extract_package(battery, buffer->pointer, in extract_battery_info()
487 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)) in extract_battery_info()
488 battery->full_charge_capacity = battery->design_capacity; in extract_battery_info()
489 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) && in extract_battery_info()
490 battery->power_unit && battery->design_voltage) { in extract_battery_info()
491 battery->design_capacity = battery->design_capacity * in extract_battery_info()
492 10000 / battery->design_voltage; in extract_battery_info()
493 battery->full_charge_capacity = battery->full_charge_capacity * in extract_battery_info()
494 10000 / battery->design_voltage; in extract_battery_info()
495 battery->design_capacity_warning = in extract_battery_info()
496 battery->design_capacity_warning * in extract_battery_info()
497 10000 / battery->design_voltage; in extract_battery_info()
506 if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags) && in extract_battery_info()
507 battery->capacity_now > battery->full_charge_capacity) in extract_battery_info()
508 battery->capacity_now = battery->full_charge_capacity; in extract_battery_info()
513 static int acpi_battery_get_info(struct acpi_battery *battery) in acpi_battery_get_info() argument
515 const int xinfo = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags); in acpi_battery_get_info()
519 if (!acpi_battery_present(battery)) in acpi_battery_get_info()
527 mutex_lock(&battery->lock); in acpi_battery_get_info()
528 status = acpi_evaluate_object(battery->device->handle, in acpi_battery_get_info()
531 mutex_unlock(&battery->lock); in acpi_battery_get_info()
534 acpi_handle_info(battery->device->handle, in acpi_battery_get_info()
540 battery, in acpi_battery_get_info()
554 static int acpi_battery_get_state(struct acpi_battery *battery) in acpi_battery_get_state() argument
560 if (!acpi_battery_present(battery)) in acpi_battery_get_state()
563 if (battery->update_time && in acpi_battery_get_state()
564 time_before(jiffies, battery->update_time + in acpi_battery_get_state()
568 mutex_lock(&battery->lock); in acpi_battery_get_state()
569 status = acpi_evaluate_object(battery->device->handle, "_BST", in acpi_battery_get_state()
571 mutex_unlock(&battery->lock); in acpi_battery_get_state()
574 acpi_handle_info(battery->device->handle, in acpi_battery_get_state()
580 result = extract_package(battery, buffer.pointer, in acpi_battery_get_state()
582 battery->update_time = jiffies; in acpi_battery_get_state()
589 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA && in acpi_battery_get_state()
590 battery->rate_now != ACPI_BATTERY_VALUE_UNKNOWN && in acpi_battery_get_state()
591 (s16)(battery->rate_now) < 0) { in acpi_battery_get_state()
592 battery->rate_now = abs((s16)battery->rate_now); in acpi_battery_get_state()
596 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags) in acpi_battery_get_state()
597 && battery->capacity_now >= 0 && battery->capacity_now <= 100) in acpi_battery_get_state()
598 battery->capacity_now = (battery->capacity_now * in acpi_battery_get_state()
599 battery->full_charge_capacity) / 100; in acpi_battery_get_state()
600 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) && in acpi_battery_get_state()
601 battery->power_unit && battery->design_voltage) { in acpi_battery_get_state()
602 battery->capacity_now = battery->capacity_now * in acpi_battery_get_state()
603 10000 / battery->design_voltage; in acpi_battery_get_state()
605 if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags) && in acpi_battery_get_state()
606 battery->capacity_now > battery->full_charge_capacity) in acpi_battery_get_state()
607 battery->capacity_now = battery->full_charge_capacity; in acpi_battery_get_state()
612 static int acpi_battery_set_alarm(struct acpi_battery *battery) in acpi_battery_set_alarm() argument
616 if (!acpi_battery_present(battery) || in acpi_battery_set_alarm()
617 !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags)) in acpi_battery_set_alarm()
620 mutex_lock(&battery->lock); in acpi_battery_set_alarm()
621 status = acpi_execute_simple_method(battery->device->handle, "_BTP", in acpi_battery_set_alarm()
622 battery->alarm); in acpi_battery_set_alarm()
623 mutex_unlock(&battery->lock); in acpi_battery_set_alarm()
628 acpi_handle_debug(battery->device->handle, "Alarm set to %d\n", in acpi_battery_set_alarm()
629 battery->alarm); in acpi_battery_set_alarm()
634 static int acpi_battery_init_alarm(struct acpi_battery *battery) in acpi_battery_init_alarm() argument
637 if (!acpi_has_method(battery->device->handle, "_BTP")) { in acpi_battery_init_alarm()
638 clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags); in acpi_battery_init_alarm()
641 set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags); in acpi_battery_init_alarm()
642 if (!battery->alarm) in acpi_battery_init_alarm()
643 battery->alarm = battery->design_capacity_warning; in acpi_battery_init_alarm()
644 return acpi_battery_set_alarm(battery); in acpi_battery_init_alarm()
651 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev)); in acpi_battery_alarm_show() local
653 return sprintf(buf, "%d\n", battery->alarm * 1000); in acpi_battery_alarm_show()
661 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev)); in acpi_battery_alarm_store() local
664 battery->alarm = x/1000; in acpi_battery_alarm_store()
665 if (acpi_battery_present(battery)) in acpi_battery_alarm_store()
666 acpi_battery_set_alarm(battery); in acpi_battery_alarm_store()
691 struct acpi_battery *battery; in __battery_hook_unregister() local
698 list_for_each_entry(battery, &acpi_battery_list, list) { in __battery_hook_unregister()
699 hook->remove_battery(battery->bat); in __battery_hook_unregister()
715 struct acpi_battery *battery; in battery_hook_register() local
726 list_for_each_entry(battery, &acpi_battery_list, list) { in battery_hook_register()
727 if (hook->add_battery(battery->bat)) { in battery_hook_register()
750 static void battery_hook_add_battery(struct acpi_battery *battery) in battery_hook_add_battery() argument
755 INIT_LIST_HEAD(&battery->list); in battery_hook_add_battery()
756 list_add(&battery->list, &acpi_battery_list); in battery_hook_add_battery()
765 if (hook_node->add_battery(battery->bat)) { in battery_hook_add_battery()
778 static void battery_hook_remove_battery(struct acpi_battery *battery) in battery_hook_remove_battery() argument
788 hook->remove_battery(battery->bat); in battery_hook_remove_battery()
791 list_del(&battery->list); in battery_hook_remove_battery()
810 static int sysfs_add_battery(struct acpi_battery *battery) in sysfs_add_battery() argument
812 struct power_supply_config psy_cfg = { .drv_data = battery, }; in sysfs_add_battery()
815 if (!ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity) && in sysfs_add_battery()
816 !ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity)) in sysfs_add_battery()
819 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) { in sysfs_add_battery()
821 battery->bat_desc.properties = in sysfs_add_battery()
823 battery->bat_desc.num_properties = in sysfs_add_battery()
826 battery->bat_desc.properties = charge_battery_props; in sysfs_add_battery()
827 battery->bat_desc.num_properties = in sysfs_add_battery()
832 battery->bat_desc.properties = in sysfs_add_battery()
834 battery->bat_desc.num_properties = in sysfs_add_battery()
837 battery->bat_desc.properties = energy_battery_props; in sysfs_add_battery()
838 battery->bat_desc.num_properties = in sysfs_add_battery()
843 battery->bat_desc.name = acpi_device_bid(battery->device); in sysfs_add_battery()
844 battery->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY; in sysfs_add_battery()
845 battery->bat_desc.get_property = acpi_battery_get_property; in sysfs_add_battery()
847 battery->bat = power_supply_register_no_ws(&battery->device->dev, in sysfs_add_battery()
848 &battery->bat_desc, &psy_cfg); in sysfs_add_battery()
850 if (IS_ERR(battery->bat)) { in sysfs_add_battery()
851 int result = PTR_ERR(battery->bat); in sysfs_add_battery()
853 battery->bat = NULL; in sysfs_add_battery()
856 battery_hook_add_battery(battery); in sysfs_add_battery()
857 return device_create_file(&battery->bat->dev, &alarm_attr); in sysfs_add_battery()
860 static void sysfs_remove_battery(struct acpi_battery *battery) in sysfs_remove_battery() argument
862 mutex_lock(&battery->sysfs_lock); in sysfs_remove_battery()
863 if (!battery->bat) { in sysfs_remove_battery()
864 mutex_unlock(&battery->sysfs_lock); in sysfs_remove_battery()
867 battery_hook_remove_battery(battery); in sysfs_remove_battery()
868 device_remove_file(&battery->bat->dev, &alarm_attr); in sysfs_remove_battery()
869 power_supply_unregister(battery->bat); in sysfs_remove_battery()
870 battery->bat = NULL; in sysfs_remove_battery()
871 mutex_unlock(&battery->sysfs_lock); in sysfs_remove_battery()
876 struct acpi_battery *battery = (struct acpi_battery *)private; in find_battery() local
886 if (battery->design_capacity * battery->design_voltage / 1000 in find_battery()
888 battery->design_capacity * 10 == dmi_capacity) in find_battery()
890 &battery->flags); in find_battery()
906 static void acpi_battery_quirks(struct acpi_battery *battery) in acpi_battery_quirks() argument
908 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)) in acpi_battery_quirks()
911 if (battery->full_charge_capacity == 100 && in acpi_battery_quirks()
912 battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN && in acpi_battery_quirks()
913 battery->capacity_now >= 0 && battery->capacity_now <= 100) { in acpi_battery_quirks()
914 set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags); in acpi_battery_quirks()
915 battery->full_charge_capacity = battery->design_capacity; in acpi_battery_quirks()
916 battery->capacity_now = (battery->capacity_now * in acpi_battery_quirks()
917 battery->full_charge_capacity) / 100; in acpi_battery_quirks()
920 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags)) in acpi_battery_quirks()
923 if (battery->power_unit && dmi_name_in_vendors("LENOVO")) { in acpi_battery_quirks()
928 dmi_walk(find_battery, battery); in acpi_battery_quirks()
930 &battery->flags) && in acpi_battery_quirks()
931 battery->design_voltage) { in acpi_battery_quirks()
932 battery->design_capacity = in acpi_battery_quirks()
933 battery->design_capacity * in acpi_battery_quirks()
934 10000 / battery->design_voltage; in acpi_battery_quirks()
935 battery->full_charge_capacity = in acpi_battery_quirks()
936 battery->full_charge_capacity * in acpi_battery_quirks()
937 10000 / battery->design_voltage; in acpi_battery_quirks()
938 battery->design_capacity_warning = in acpi_battery_quirks()
939 battery->design_capacity_warning * in acpi_battery_quirks()
940 10000 / battery->design_voltage; in acpi_battery_quirks()
941 battery->capacity_now = battery->capacity_now * in acpi_battery_quirks()
942 10000 / battery->design_voltage; in acpi_battery_quirks()
947 if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags)) in acpi_battery_quirks()
950 if (acpi_battery_is_degraded(battery) && in acpi_battery_quirks()
951 battery->capacity_now > battery->full_charge_capacity) { in acpi_battery_quirks()
952 set_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags); in acpi_battery_quirks()
953 battery->capacity_now = battery->full_charge_capacity; in acpi_battery_quirks()
957 static int acpi_battery_update(struct acpi_battery *battery, bool resume) in acpi_battery_update() argument
959 int result = acpi_battery_get_status(battery); in acpi_battery_update()
964 if (!acpi_battery_present(battery)) { in acpi_battery_update()
965 sysfs_remove_battery(battery); in acpi_battery_update()
966 battery->update_time = 0; in acpi_battery_update()
973 if (!battery->update_time) { in acpi_battery_update()
974 result = acpi_battery_get_info(battery); in acpi_battery_update()
977 acpi_battery_init_alarm(battery); in acpi_battery_update()
980 result = acpi_battery_get_state(battery); in acpi_battery_update()
983 acpi_battery_quirks(battery); in acpi_battery_update()
985 if (!battery->bat) { in acpi_battery_update()
986 result = sysfs_add_battery(battery); in acpi_battery_update()
995 if ((battery->state & ACPI_BATTERY_STATE_CRITICAL) || in acpi_battery_update()
996 (test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags) && in acpi_battery_update()
997 (battery->capacity_now <= battery->alarm))) in acpi_battery_update()
998 acpi_pm_wakeup_event(&battery->device->dev); in acpi_battery_update()
1003 static void acpi_battery_refresh(struct acpi_battery *battery) in acpi_battery_refresh() argument
1007 if (!battery->bat) in acpi_battery_refresh()
1010 power_unit = battery->power_unit; in acpi_battery_refresh()
1012 acpi_battery_get_info(battery); in acpi_battery_refresh()
1014 if (power_unit == battery->power_unit) in acpi_battery_refresh()
1018 sysfs_remove_battery(battery); in acpi_battery_refresh()
1019 sysfs_add_battery(battery); in acpi_battery_refresh()
1025 struct acpi_battery *battery = acpi_driver_data(device); in acpi_battery_notify() local
1028 if (!battery) in acpi_battery_notify()
1030 old = battery->bat; in acpi_battery_notify()
1040 acpi_battery_refresh(battery); in acpi_battery_notify()
1041 acpi_battery_update(battery, false); in acpi_battery_notify()
1044 acpi_battery_present(battery)); in acpi_battery_notify()
1045 acpi_notifier_call_chain(device, event, acpi_battery_present(battery)); in acpi_battery_notify()
1047 if (old && battery->bat) in acpi_battery_notify()
1048 power_supply_changed(battery->bat); in acpi_battery_notify()
1054 struct acpi_battery *battery = container_of(nb, struct acpi_battery, in battery_notify() local
1061 if (!acpi_battery_present(battery)) in battery_notify()
1064 if (battery->bat) { in battery_notify()
1065 acpi_battery_refresh(battery); in battery_notify()
1067 result = acpi_battery_get_info(battery); in battery_notify()
1071 result = sysfs_add_battery(battery); in battery_notify()
1076 acpi_battery_init_alarm(battery); in battery_notify()
1077 acpi_battery_get_state(battery); in battery_notify()
1152 static int acpi_battery_update_retry(struct acpi_battery *battery) in acpi_battery_update_retry() argument
1157 ret = acpi_battery_update(battery, false); in acpi_battery_update_retry()
1169 struct acpi_battery *battery = NULL; in acpi_battery_add() local
1177 battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL); in acpi_battery_add()
1178 if (!battery) in acpi_battery_add()
1180 battery->device = device; in acpi_battery_add()
1183 device->driver_data = battery; in acpi_battery_add()
1184 mutex_init(&battery->lock); in acpi_battery_add()
1185 mutex_init(&battery->sysfs_lock); in acpi_battery_add()
1186 if (acpi_has_method(battery->device->handle, "_BIX")) in acpi_battery_add()
1187 set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags); in acpi_battery_add()
1189 result = acpi_battery_update_retry(battery); in acpi_battery_add()
1196 battery->pm_nb.notifier_call = battery_notify; in acpi_battery_add()
1197 register_pm_notifier(&battery->pm_nb); in acpi_battery_add()
1204 sysfs_remove_battery(battery); in acpi_battery_add()
1205 mutex_destroy(&battery->lock); in acpi_battery_add()
1206 mutex_destroy(&battery->sysfs_lock); in acpi_battery_add()
1207 kfree(battery); in acpi_battery_add()
1213 struct acpi_battery *battery = NULL; in acpi_battery_remove() local
1218 battery = acpi_driver_data(device); in acpi_battery_remove()
1219 unregister_pm_notifier(&battery->pm_nb); in acpi_battery_remove()
1220 sysfs_remove_battery(battery); in acpi_battery_remove()
1221 mutex_destroy(&battery->lock); in acpi_battery_remove()
1222 mutex_destroy(&battery->sysfs_lock); in acpi_battery_remove()
1223 kfree(battery); in acpi_battery_remove()
1231 struct acpi_battery *battery; in acpi_battery_resume() local
1236 battery = acpi_driver_data(to_acpi_device(dev)); in acpi_battery_resume()
1237 if (!battery) in acpi_battery_resume()
1240 battery->update_time = 0; in acpi_battery_resume()
1241 acpi_battery_update(battery, true); in acpi_battery_resume()