max1720x_battery: sync get_cycle_count behavior from android-msm-pixel-4.14
Bug: 172018661
Signed-off-by: Jenny Ho <[email protected]>
Change-Id: If31f3afe443b6377146b66c81d57fbb0014b1c7e
diff --git a/max1720x_battery.c b/max1720x_battery.c
index ae2ef9f..fa6e248 100644
--- a/max1720x_battery.c
+++ b/max1720x_battery.c
@@ -168,6 +168,7 @@
int batt_id;
int batt_id_defer_cnt;
int cycle_count;
+ int cycle_count_offset;
bool init_complete;
bool resume_complete;
@@ -1225,15 +1226,36 @@
* count if the fuel gauge history has an entry with 0 cycles and
* non 0 time-in-field.
*/
-static int max1720x_get_cycle_count_offset(const struct max1720x_chip *chip)
+static int max1720x_get_cycle_count_offset(struct max1720x_chip *chip)
{
- int offset = 0;
+ int offset = 0, i, history_count;
+ struct max1720x_history hi;
- /*
- * uses history on devices that have it (max1720x), use EEPROM
- * in others. it might be written in terms of storage.
- */
+ if (!chip->history_page_size)
+ return 0;
+ mutex_lock(&chip->history_lock);
+ history_count = max1720x_history_read(chip, &hi);
+ if (history_count < 0) {
+ mutex_unlock(&chip->history_lock);
+ return 0;
+ }
+
+ for (i = 0; i < history_count; i++) {
+ u16 *entry = &hi.history[i * chip->history_page_size];
+
+ if (entry[MAX17201_HIST_CYCLE_COUNT_OFFSET] == 0 &&
+ entry[MAX17201_HIST_TIME_OFFSET] != 0) {
+ offset += MAXIM_CYCLE_COUNT_RESET;
+ break;
+ }
+ }
+ mutex_unlock(&chip->history_lock);
+
+ dev_dbg(chip->dev, "history_count=%d page_size=%d i=%d offset=%d\n",
+ history_count, chip->history_page_size, i, offset);
+
+ max1720x_history_free(&hi);
return offset;
}
@@ -1247,11 +1269,14 @@
return err;
cycle_count = reg_to_cycles(temp);
- if (chip->cycle_count == -1 || cycle_count < chip->cycle_count)
- cycle_count += max1720x_get_cycle_count_offset(chip);
+ if ((chip->cycle_count == -1) ||
+ ((cycle_count + chip->cycle_count_offset) < chip->cycle_count))
+ chip->cycle_count_offset =
+ max1720x_get_cycle_count_offset(chip);
- chip->cycle_count = cycle_count;
- return cycle_count;
+ chip->cycle_count = cycle_count + chip->cycle_count_offset;
+
+ return chip->cycle_count;
}
static void max1720x_handle_update_empty_voltage(struct max1720x_chip *chip,