max1720x_battery: Cycle count needs to be positive
Conversion for signed 16 bit can yield negative cycle count. Battery
cycle count should never be negative. Switching to signed 32 bit for
calculation.
Bug: 188245977
Signed-off-by: George Lee <[email protected]>
Change-Id: If854ecadf873ff698c6925a4ee7f92b0670f25ae
diff --git a/max1720x_battery.c b/max1720x_battery.c
index 1be55a9..8de65e6 100644
--- a/max1720x_battery.c
+++ b/max1720x_battery.c
@@ -489,14 +489,14 @@
return div_s64((s64) val * 1000 * rsense, 4096);
}
-static inline int reg_to_cycles(s16 val, int gauge_type)
+static inline int reg_to_cycles(u32 val, int gauge_type)
{
if (gauge_type == MAX_M5_GAUGE_TYPE) {
/* LSB: 1% of one cycle */
- return DIV_ROUND_CLOSEST((int) val, 100);
+ return DIV_ROUND_CLOSEST(val, 100);
} else {
/* LSB: 16% of one cycle */
- return DIV_ROUND_CLOSEST((int) val * 16, 100);
+ return DIV_ROUND_CLOSEST(val * 16, 100);
}
}
@@ -1425,7 +1425,7 @@
if (err < 0)
return err;
- cycle_count = reg_to_cycles(reg_cycle, chip->gauge_type);
+ cycle_count = reg_to_cycles((u32)reg_cycle, chip->gauge_type);
if ((chip->cycle_count == -1) ||
((cycle_count + chip->cycle_count_offset) < chip->cycle_count))
chip->cycle_count_offset =