power: pm8921-bms: calibrate ocv only when it changes

The current driver adjusts the ocv (open circuit voltage) every time
it is asked to do a state of charge calculation. This results in small
errors in state of charge as the calibration values changes.

Update the driver such that it converts a raw ocv value to voltage only
when it changes. For other times it uses the previously calculated ocv
voltage (stored in last_ocv_uv). This implies that calibration data used
for raw to voltage conversion is right around when ocv was taken.

Change-Id: I27832682f77391c4fce65127e25ec456ee1a0fd1
Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>
diff --git a/drivers/power/pm8921-bms.c b/drivers/power/pm8921-bms.c
index ebf6ae3..c275a06 100644
--- a/drivers/power/pm8921-bms.c
+++ b/drivers/power/pm8921-bms.c
@@ -121,6 +121,7 @@
 	int			batt_temp_suspend;
 	int			soc_rbatt_suspend;
 	int			default_rbatt_mohm;
+	uint16_t		prev_last_good_ocv_raw;
 };
 
 static struct pm8921_bms_chip *the_chip;
@@ -917,8 +918,16 @@
 	mutex_unlock(&chip->bms_output_lock);
 
 	usb_chg =  usb_chg_plugged_in();
-	convert_vbatt_raw_to_uv(chip, usb_chg,
+
+	if (chip->prev_last_good_ocv_raw == 0 ||
+		chip->prev_last_good_ocv_raw != raw->last_good_ocv_raw) {
+		chip->prev_last_good_ocv_raw = raw->last_good_ocv_raw;
+		convert_vbatt_raw_to_uv(chip, usb_chg,
 			raw->last_good_ocv_raw, &raw->last_good_ocv_uv);
+		last_ocv_uv = raw->last_good_ocv_uv;
+	} else {
+		raw->last_good_ocv_uv = last_ocv_uv;
+	}
 
 	if (raw->last_good_ocv_uv)
 		last_ocv_uv = raw->last_good_ocv_uv;