@@ -115,7 +115,7 @@ static int adc_battery_helper_get_capacity(struct adc_battery_helper *help)
4250000,
4300000,
};
- int i, ocv_diff, ocv_step;
+ int i, array_len, ocv_diff, ocv_step;
if (help->ocv_avg_uv < ocv_capacity_tbl[0])
return 0;
@@ -123,7 +123,16 @@ static int adc_battery_helper_get_capacity(struct adc_battery_helper *help)
if (help->status == POWER_SUPPLY_STATUS_FULL)
return 100;
- for (i = 1; i < ARRAY_SIZE(ocv_capacity_tbl); i++) {
+ /*
+ * Ignore the array-entries with voltages > 4200000 for non High-Voltage
+ * batteries to avoid reporting values > 100% in the non HV case.
+ */
+ if (help->psy->battery_info->constant_charge_voltage_max_uv >= 4300000)
+ array_len = ARRAY_SIZE(ocv_capacity_tbl);
+ else
+ array_len = ARRAY_SIZE(ocv_capacity_tbl) - 2;
+
+ for (i = 1; i < array_len; i++) {
if (help->ocv_avg_uv > ocv_capacity_tbl[i])
continue;
The ocv_capacity_tbl[] to map the ocv voltage to capacity goes up to 4.3V for LiPo High Voltage (LiHV) cells. For non HV cells the code assumes that the estimated ocv value never comes above 4.2V, but there might be cases where it does go above 4.2V leading to adc_battery_helper_get_capacity() reporting a capacity above 100%. Do not use the table entries with a voltage above 4.2V for non HV cells to avoid this use. Signed-off-by: Hans de Goede <hdegoede@redhat.com> --- drivers/power/supply/adc-battery-helper.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)