From c58a5a2a153855956d54dd9b6dd388d4ef63926a Mon Sep 17 00:00:00 2001
From: Ramakrishna Pallala <ramakrishna.pallala@intel.com>
Date: Thu, 16 Dec 2010 07:08:55 +0530
Subject: [PATCH] Modified Discharge curve loopup function

Signed-off-by: Ramakrishna Pallala <ramakrishna.pallala@intel.com>
---
 drivers/power/intel_mdf_battery.c |   59 +++++++++++++++++++-----------------
 1 files changed, 31 insertions(+), 28 deletions(-)

diff --git a/drivers/power/intel_mdf_battery.c b/drivers/power/intel_mdf_battery.c
index c89ac97..ed6eff5 100644
--- a/drivers/power/intel_mdf_battery.c
+++ b/drivers/power/intel_mdf_battery.c
@@ -234,6 +234,8 @@
 
 /* IPC defines */
 #define IPCMSG_BATTERY		0xEF
+#define IPCCMD_CC_WRITE		0x00
+#define IPCCMD_CC_READ		0x01
 
 #define TEMP_CHARGE_DELAY_JIFFIES	(HZ * 30)	/*30 sec */
 #define CHARGE_STATUS_DELAY_JIFFIES	(HZ * 10)	/*10 sec */
@@ -894,12 +896,14 @@ static unsigned int msic_read_coloumb_ctr(void)
 	uint32_t cvalue;
 
 	/* determine other parameters */
-	err = intel_scu_ipc_command(IPCMSG_BATTERY, 0x01, NULL, 0, &cvalue, 1);
+	err = intel_scu_ipc_command(IPCMSG_BATTERY, IPCCMD_CC_READ, NULL, 0,
+						&cvalue, sizeof(cvalue));
 	if (err)
 		dev_warn(msic_dev, "IPC Command Failed %s\n", __func__);
 
 	return cvalue;
 }
+
 static unsigned int cc_to_coloumbs(unsigned int cc_val)
 {
 	unsigned int coloumbs = 0;
@@ -985,14 +989,20 @@ static unsigned int mdf_cal_avg(unsigned int avg)
 	return avg / 2;
 }
 
-static unsigned int chrg_to_vol_lookup(unsigned int chrg)
+static unsigned int dischrg_curve_lookup(unsigned int val, int j)
 {
-	int ch_diff, ch_total_diff, volt_total_diff, i;
-	unsigned int volt;
+	int val_diff, val_total_diff, total_diff, i, k;
+	unsigned int lookup_val;
+
+	/* if j = 0 input is voltage, we need to lookup
+	 * for charge else if if j =1 input is charge,
+	 * we need to lookup for volatge
+	 */
+	k = !j;
 
-	/* Find the index of most nearest charge value */
+	/* Find the index of most nearest voltage value */
 	for (i = 0; i < DISCHRG_CURVE_MAX_SAMPLES; i++) {
-		if (chrg < dischargeCurve[i][1])
+		if (val < dischargeCurve[i][j])
 			continue;
 		else
 			break;
@@ -1003,34 +1013,29 @@ static unsigned int chrg_to_vol_lookup(unsigned int chrg)
 		return 0;
 	}
 
-	if ((i == 0) || (chrg == dischargeCurve[i][1]))
-		return dischargeCurve[i][0];
+	if ((i == 0) || (val == dischargeCurve[i][j]))
+		return dischargeCurve[i][k];
 
-	/* Linear approximation of the charge to voltage */
-	ch_diff = chrg - dischargeCurve[i][1];
-	ch_total_diff = dischargeCurve[i-1][1] - dischargeCurve[i][1];
-	volt_total_diff = dischargeCurve[i-1][0] - dischargeCurve[i][0];
+	/* Linear approximation of the discharge curve */
+	val_diff = val - dischargeCurve[i][j];
+	val_total_diff = dischargeCurve[i-1][j] - dischargeCurve[i][j];
+	total_diff = dischargeCurve[i-1][k] - dischargeCurve[i][k];
 
-	volt  =  dischargeCurve[i][0] +
-				(ch_diff * volt_total_diff) / ch_total_diff;
+	lookup_val  =  dischargeCurve[i][k] +
+				(val_diff * val_total_diff) / total_diff;
 
-	return volt;
+	return lookup_val;
 }
 
 static unsigned int msic_read_energy_now(struct msic_power_module_info *mbi)
 {
 	unsigned int vltg, chrg;
 
-	/* Read CC register value */
-	chrg = msic_read_coloumb_ctr();
-	/* Covert to milli coloumbs */
-	chrg = cc_to_coloumbs(chrg);
+	/* Read battery charge in mAh */
+	chrg = msic_get_charge_now();
 
 	/* get voltage form lookup table */
-	vltg = chrg_to_vol_lookup(chrg);
-
-	/* Convert from mColoumbs to mAh */
-	chrg = chrg / COLMB_TO_MAHRS_CONV_FCTR;
+	vltg = dischrg_curve_lookup(chrg, 1);
 
 	return  (vltg * chrg) / 1000;
 }
@@ -1629,7 +1634,7 @@ static int check_charge_full(struct msic_power_module_info *mbi, int vref)
 	 * temporary fix we are hardcoding the Rbatt as 180 mOhms
 	 */
 	vocv = vbat - (ibatt_avg * MSIC_CHRG_RBATT_VAL)/1000;
-	dev_dbg(msic_dev, "vocv:%d  vbat:%d\n", vocv, vbat);
+	dev_dbg(msic_dev, "vref:%d vocv:%d  vbat:%d\n", vref, vocv, vbat);
 
 	/*
 	 * Check if Vbat is equal to max charge voltage and
@@ -1714,7 +1719,7 @@ static void calculate_charge_cycles(struct msic_power_module_info *mbi,
 */
 static void msic_batt_temp_charging(struct work_struct *work)
 {
-	int ret, i, charge_now, is_maint_chrg = false, is_lowchrg_enbl;
+	int ret, i, is_maint_chrg = false, is_lowchrg_enbl;
 	static int iprev = -1, is_chrg_enbl;
 	short int cv = 0, cc = 0, vinlimit = 0, cvref;
 	int adc_temp, adc_vol;
@@ -1800,9 +1805,7 @@ static void msic_batt_temp_charging(struct work_struct *work)
 			spin_unlock(&mbi->event_lock);
 
 			mutex_lock(&mbi->batt_lock);
-			charge_now = msic_read_coloumb_ctr();
-			mbi->batt_props.charge_full =
-						cc_to_coloumbs(charge_now);
+			mbi->batt_props.charge_full = msic_get_charge_now();
 			mbi->batt_props.status = POWER_SUPPLY_STATUS_FULL;
 			mutex_unlock(&mbi->batt_lock);
 
-- 
1.7.2.3

