power: pm8921-bms: Add api to read Vsense

The Rsense resistor connects between the -ve end of the battery
and GND. The voltage across the Rsense gives us a good indication
of the current flowing through the battery.

Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>
diff --git a/drivers/power/pm8921-bms.c b/drivers/power/pm8921-bms.c
index e6e59de..eb98eed 100644
--- a/drivers/power/pm8921-bms.c
+++ b/drivers/power/pm8921-bms.c
@@ -285,6 +285,21 @@
 	return 0;
 }
 
+static int read_vsense_avg(struct pm8921_bms_chip *chip, int *result)
+{
+	int rc;
+	int16_t reading;
+
+	rc = pm_bms_read_output_data(chip, VSENSE_AVG, &reading);
+	if (rc) {
+		pr_err("fail to read VSENSE_AVG rc = %d\n", rc);
+		return rc;
+	}
+	*result = CONV_READING(reading);
+	pr_debug("read = %04x vsense = %d\n", reading, *result);
+	return 0;
+}
+
 static int linear_interpolate(int y0, int x0, int y1, int x1, int x)
 {
 	if (y0 == y1 || x == x0)
@@ -709,6 +724,16 @@
 	return soc;
 }
 
+int pm8921_bms_get_vsense_avg(int *result)
+{
+	if (the_chip)
+		return read_vsense_avg(the_chip, result);
+
+	pr_err("called before initialization\n");
+	return -EINVAL;
+}
+EXPORT_SYMBOL(pm8921_bms_get_vsense_avg);
+
 int pm8921_bms_get_percent_charge(void)
 {
 	int batt_temp, rc;
@@ -1004,6 +1029,9 @@
 	case OCV_FOR_RBATT:
 		read_ocv_for_rbatt(the_chip, (uint *)val);
 		break;
+	case VSENSE_AVG:
+		read_vsense_avg(the_chip, (uint *)val);
+		break;
 	default:
 		ret = -EINVAL;
 	}
@@ -1107,6 +1135,8 @@
 				(void *)VSENSE_FOR_RBATT, &reading_fops);
 	debugfs_create_file("read_ocv_for_rbatt", 0644, chip->dent,
 				(void *)OCV_FOR_RBATT, &reading_fops);
+	debugfs_create_file("read_vsense_avg", 0644, chip->dent,
+				(void *)VSENSE_AVG, &reading_fops);
 
 	debugfs_create_file("show_rbatt", 0644, chip->dent,
 				(void *)CALC_RBATT, &calc_fops);
diff --git a/include/linux/mfd/pm8xxx/pm8921-bms.h b/include/linux/mfd/pm8xxx/pm8921-bms.h
index 394553b..279e6f0 100644
--- a/include/linux/mfd/pm8xxx/pm8921-bms.h
+++ b/include/linux/mfd/pm8xxx/pm8921-bms.h
@@ -111,10 +111,23 @@
 };
 
 #if defined(CONFIG_PM8921_BMS) || defined(CONFIG_PM8921_BMS_MODULE)
+/**
+ * pm8921_bms_get_vsense_avg - return the voltage across the sense
+ *				resitor in microvolts
+ * @result:	The pointer where the voltage will be updated
+ *
+ * RETURNS:	Error code if there was a problem reading vsense, Zero otherwise
+ *		The result won't be updated in case of an error.
+ */
+int pm8921_bms_get_vsense_avg(int *result);
 int pm8921_bms_get_percent_charge(void);
 void pm8921_bms_charging_began(void);
 void pm8921_bms_charging_end(void);
 #else
+static inline int pm8921_bms_get_vsense_avg(int *result)
+{
+	return -ENXIO;
+}
 static inline int pm8921_bms_get_percent_charge(void)
 {
 	return -ENXIO;