Fix a possible invalid update of the battery value. If the hdq goes in timeout
the driver must manteins the old value.

Signed-off-by: Michael Trimarchi <[email protected]>
---
diff --git a/drivers/power/bq27000_battery.c b/drivers/power/bq27000_battery.c
index 593cbe6..f3a52da 100644
--- a/drivers/power/bq27000_battery.c
+++ b/drivers/power/bq27000_battery.c
@@ -169,6 +169,32 @@ static int hdq_read16(struct bq27000_device_info *di, int address)
 	return -ETIME;
 }
 
+static void update_hdq_read16(struct bq27000_device_info *di,
+			      int address, int *value)
+{
+	int ret = hdq_read16(di, address);
+
+	if (ret != -ETIME) {
+		*value = ret;
+		return;
+	}
+
+	dev_dbg(di->dev, "Timeout reading value %s\n", __func__);
+}
+
+static void update_hdq_data(struct bq27000_device_info *di,
+			    int address, int *value)
+{
+	int ret = (di->pdata->hdq_read)(address);
+
+	if (ret != -ETIME) {
+		*value = ret;
+		return;
+	}
+
+	dev_dbg(di->dev, "Timeout reading value %s\n", __func__);
+}
+
 static void bq27000_battery_external_power_changed(struct power_supply *psy)
 {
 	struct bq27000_device_info *di =
@@ -184,7 +210,8 @@ static int bq27000_battery_get_property(struct power_supply *psy,
 				       union power_supply_propval *val)
 {
 	int n;
-	struct bq27000_device_info *di = container_of(psy, struct bq27000_device_info, bat);
+	struct bq27000_device_info *di =
+		container_of(psy, struct bq27000_device_info, bat);
 
 	switch (psp) {
 	case POWER_SUPPLY_PROP_STATUS:
@@ -326,15 +353,16 @@ static void bq27000_battery_work(struct work_struct *work)
 	if ((di->pdata->hdq_initialized)()) {
 		struct bq27000_bat_regs regs;
 
-		regs.ai    = hdq_read16(di, BQ27000_AI_L);
-		regs.flags = (di->pdata->hdq_read)(BQ27000_FLAGS);
-		regs.lmd   = hdq_read16(di, BQ27000_LMD_L);
-		regs.nac   = hdq_read16(di, BQ27000_NAC_L);
-		regs.rsoc  = (di->pdata->hdq_read)(BQ27000_RSOC);
-		regs.temp  = hdq_read16(di, BQ27000_TEMP_L);
-		regs.tte   = hdq_read16(di, BQ27000_TTE_L);
-		regs.ttf   = hdq_read16(di, BQ27000_TTF_L);
-		regs.volt  = hdq_read16(di, BQ27000_VOLT_L);
+		regs = di->regs;
+		update_hdq_read16(di, BQ27000_AI_L, &regs.ai);
+		update_hdq_read16(di, BQ27000_LMD_L, &regs.lmd);
+		update_hdq_read16(di, BQ27000_NAC_L, &regs.nac);
+		update_hdq_read16(di, BQ27000_TEMP_L, &regs.temp);
+		update_hdq_read16(di, BQ27000_TTE_L, &regs.tte);
+		update_hdq_read16(di, BQ27000_TTF_L, &regs.ttf);
+		update_hdq_read16(di, BQ27000_VOLT_L, &regs.volt);
+		update_hdq_data(di, BQ27000_FLAGS, &regs.flags);
+		update_hdq_data(di, BQ27000_RSOC, &regs.rsoc);
 
 		if (memcmp (&regs, &di->regs, sizeof(regs)) != 0) {
 			di->regs = regs;

Reply via email to