power: pm8921-bms: Fix coulomb counter overflow

The coulomb counter in hardware is 32bits and is signed. The software
also adds a 32bit value (the cc reading when end of charge happened).
This addition might cause the value of the coulomb counter to overflow.

Fix it by switching to 64bit values before adding to it.

CRs-Fixed: 328352
Change-Id: Ic8268014fccca00acd03b0693896ab494fe94d87
Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>
diff --git a/drivers/power/pm8921-bms.c b/drivers/power/pm8921-bms.c
index 5bb76e7..293c25d 100644
--- a/drivers/power/pm8921-bms.c
+++ b/drivers/power/pm8921-bms.c
@@ -475,8 +475,6 @@
 	}
 	*result = msw << 16 | lsw;
 	pr_debug("msw = %04x lsw = %04x cc = %d\n", msw, lsw, *result);
-	pr_debug("cc = %d after subtracting %d\n",
-					*result, chip->cc_reading_at_100);
 	return 0;
 }
 
@@ -923,14 +921,15 @@
  * RETURNS: in val pointer coulumb counter based charger in uAh
  *          (micro Amp hour)
  */
-static void calculate_cc_uah(struct pm8921_bms_chip *chip, int cc, int *val,
-			int *coulumb_counter)
+static void calculate_cc_uah(struct pm8921_bms_chip *chip, int cc, int *val)
 {
 	int64_t cc_voltage_uv, cc_nvh, cc_uah;
 
-	*coulumb_counter = cc;
-	*coulumb_counter -= chip->cc_reading_at_100;
-	cc_voltage_uv = (int64_t)*coulumb_counter;
+	cc_voltage_uv = cc;
+	cc_voltage_uv -= chip->cc_reading_at_100;
+	pr_debug("cc = %d. after subtracting %d cc = %lld\n",
+					cc, chip->cc_reading_at_100,
+					cc_voltage_uv);
 	cc_voltage_uv = cc_to_microvolt(chip, cc_voltage_uv);
 	cc_voltage_uv = pm8xxx_cc_adjust_for_gain(cc_voltage_uv);
 	pr_debug("cc_voltage_uv = %lld microvolts\n", cc_voltage_uv);
@@ -1002,7 +1001,6 @@
 						int *remaining_charge_uah,
 						int *cc_uah)
 {
-	int coulumb_counter;
 	unsigned long flags;
 
 	*fcc_uah = calculate_fcc_uah(chip, batt_temp, chargecycles);
@@ -1021,9 +1019,11 @@
 	pr_debug("RC = %uuAh\n", *remaining_charge_uah);
 
 	/* calculate cc micro_volt_hour */
-	calculate_cc_uah(chip, raw->cc, cc_uah, &coulumb_counter);
-	pr_debug("cc_uah = %duAh raw->cc = %x cc = %x\n",
-					*cc_uah, raw->cc, coulumb_counter);
+	calculate_cc_uah(chip, raw->cc, cc_uah);
+	pr_debug("cc_uah = %duAh raw->cc = %x cc = %lld after subtracting %d\n",
+				*cc_uah, raw->cc,
+				(int64_t)raw->cc - chip->cc_reading_at_100,
+				chip->cc_reading_at_100);
 	spin_unlock_irqrestore(&chip->bms_100_lock, flags);
 }
 
@@ -1270,7 +1270,7 @@
 
 void pm8921_bms_charging_began(void)
 {
-	int batt_temp, coulumb_counter, rc;
+	int batt_temp, rc;
 	struct pm8xxx_adc_chan_result result;
 	struct pm8921_soc_params raw;
 
@@ -1290,7 +1290,7 @@
 					batt_temp, last_chargecycles);
 	bms_start_percent = the_chip->start_percent;
 	bms_start_ocv_uv = raw.last_good_ocv_uv;
-	calculate_cc_uah(the_chip, raw.cc, &bms_start_cc_uah, &coulumb_counter);
+	calculate_cc_uah(the_chip, raw.cc, &bms_start_cc_uah);
 
 	pr_debug("start_percent = %u%%\n", the_chip->start_percent);
 }
@@ -1299,7 +1299,7 @@
 #define DELTA_FCC_PERCENT	3
 void pm8921_bms_charging_end(int is_battery_full)
 {
-	int batt_temp, coulumb_counter, rc;
+	int batt_temp, rc;
 	struct pm8xxx_adc_chan_result result;
 	struct pm8921_soc_params raw;
 
@@ -1355,7 +1355,7 @@
 
 	bms_end_percent = the_chip->end_percent;
 	bms_end_ocv_uv = raw.last_good_ocv_uv;
-	calculate_cc_uah(the_chip, raw.cc, &bms_end_cc_uah, &coulumb_counter);
+	calculate_cc_uah(the_chip, raw.cc, &bms_end_cc_uah);
 
 	if (the_chip->end_percent > the_chip->start_percent) {
 		last_charge_increase =