diff mbox series

[6/8] power: supply: adc-battery-helper: Fix reporting capacity > 100%

Message ID 20241215172133.178460-7-hdegoede@redhat.com (mailing list archive)
State Handled Elsewhere, archived
Headers show
Series power: supply: Add adc-battery-helper lib and Intel Dollar Cove TI CC battery driver | expand

Commit Message

Hans de Goede Dec. 15, 2024, 5:21 p.m. UTC
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(-)
diff mbox series

Patch

diff --git a/drivers/power/supply/adc-battery-helper.c b/drivers/power/supply/adc-battery-helper.c
index 1917e92ab1eb..104d9a888486 100644
--- a/drivers/power/supply/adc-battery-helper.c
+++ b/drivers/power/supply/adc-battery-helper.c
@@ -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;