Skip to contentSkip to conversation

Some fixes and improvments for cpcap battery and charger

PATCH In review Tony Lindgren 3 participants 15 messages Apr 7, 2019 – May 18, 2019 Power supply

lore
Share & tools
lore
#0 lore raw

cover Some fixes and improvments for cpcap battery and charger

Hi all,

I've been trying to figure out how come the coulomb counter on droid 4 is off especially for the low power consumption values. Turns out the "coulomb counter" values correlate better with average power consumption if we divide the value with number of samples. Otherwise we have a curve instead of flat correlation between the register values and power consumed. I have some patches coming up eventually to fix that, but meanwhile I've noticed some minor issues in general that would be good to have out of the way.

I've only tagged the first one with fixes tag, the other ones can certainly wait for the merge window considering further changes are needed at least for low power consumption values.

Regards,

Tony

Tony Lindgren (7):
power: supply: cpcap-battery: Fix division by zero
power: supply: cpcap-battery: Fix low battery check
power: supply: cpcap-battery: Fix signed counter sample register
power: supply: cpcap-battery: Fix coulomb counter calibration register
use
power: supply: cpcap-battery: Use accumulator for current and power
average
power: supply: cpcap-battery: Fix pointless EPROBE_DEFER warnings
power: supply: cpcap-charger: Fix pointless EPROBE_DEFER warnings

 drivers/power/supply/cpcap-battery.c | 45 +++++++++++++++-------------
 drivers/power/supply/cpcap-charger.c |  5 ++--
 2 files changed, 27 insertions(+), 23 deletions(-)
Signature · 1 line
2.21.0
#1 lore raw

PATCH 1/7 power: supply: cpcap-battery: Fix division by zero

If called fast enough so samples do not increment, we can get division by zero in kernel:

__div0
cpcap_battery_cc_raw_div
cpcap_battery_get_property
power_supply_get_property.part.1
power_supply_get_property
power_supply_show_property
power_supply_uevent

Fixes 874b2adbed12 ("power: supply: cpcap-battery: Add a battery driver")
Cc Pavel Machek <[email protected]>
---
 drivers/power/supply/cpcap-battery.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/power/supply/cpcap-battery.c +3 -0
@@ -221,6 +221,9 @@ static int cpcap_battery_cc_raw_div(struct cpcap_battery_ddata *ddata,
 	int avg_current;
 	u32 cc_lsb;
 
+	if (!divider)
+		return 0;
+
 	sample &= 0xffffff;		/* 24-bits, unsigned */
 	offset &= 0x7ff;		/* 10-bits, signed */
 
Signature · 1 line
2.21.0
Acked-by on v1 1/7
#2 lore raw

Re: [PATCH 1/7] power: supply: cpcap-battery: Fix division by zero

Quoted 11 lines · Tony Lindgren

If called fast enough so samples do not increment, we can get division by zero in kernel:

__div0
cpcap_battery_cc_raw_div
cpcap_battery_get_property
power_supply_get_property.part.1
power_supply_get_property
power_supply_show_property
power_supply_uevent

Quoted 3 lines
Fixes 874b2adbed12 ("power: supply: cpcap-battery: Add a battery driver")
Cc Pavel Machek <[email protected]>
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#3 lore raw

PATCH 2/7 power: supply: cpcap-battery: Fix low battery check

In reply to Tony Lindgren

We need to check current instead of the charge counter to see if a charger is connected. The charge counter shows the cumulated value instead of the current charge current and can be negative or positive.

---
 drivers/power/supply/cpcap-battery.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/power/supply/cpcap-battery.c +2 -2
@@ -562,11 +562,11 @@ static irqreturn_t cpcap_battery_irq_thread(int irq, void *data)
 
 	switch (d->action) {
 	case CPCAP_BATTERY_IRQ_ACTION_BATTERY_LOW:
-		if (latest->counter_uah >= 0)
+		if (latest->current_ua >= 0)
 			dev_warn(ddata->dev, "Battery low at 3.3V!\n");
 		break;
 	case CPCAP_BATTERY_IRQ_ACTION_POWEROFF:
-		if (latest->counter_uah >= 0) {
+		if (latest->current_ua >= 0) {
 			dev_emerg(ddata->dev,
 				  "Battery empty at 3.1V, powering off\n");
 			orderly_poweroff(true);
Signature · 1 line
2.21.0
Acked-by on v1 2/7
#4 lore raw

Re: [PATCH 2/7] power: supply: cpcap-battery: Fix low battery check

Quoted 4 lines · Tony Lindgren

We need to check current instead of the charge counter to see if a charger is connected. The charge counter shows the cumulated value instead of the current charge current and can be negative or positive.

Quoted 24 lines
---
 drivers/power/supply/cpcap-battery.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/power/supply/cpcap-battery.c +2 -2
@@ -562,11 +562,11 @@ static irqreturn_t cpcap_battery_irq_thread(int irq, void *data)
 
 	switch (d->action) {
 	case CPCAP_BATTERY_IRQ_ACTION_BATTERY_LOW:
-		if (latest->counter_uah >= 0)
+		if (latest->current_ua >= 0)
 			dev_warn(ddata->dev, "Battery low at 3.3V!\n");
 		break;
 	case CPCAP_BATTERY_IRQ_ACTION_POWEROFF:
-		if (latest->counter_uah >= 0) {
+		if (latest->current_ua >= 0) {
 			dev_emerg(ddata->dev,
 				  "Battery empty at 3.1V, powering off\n");
 			orderly_poweroff(true);
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#5 lore raw

PATCH 3/7 power: supply: cpcap-battery: Fix signed counter sample register

In reply to Tony Lindgren

The accumulator sample register is signed 32-bits wide register on droid 4. And only the earlier version of cpcap has a signed 24-bits wide register. We're currently passing it around as unsigned, so let's fix that and use sign_extend32() for the earlier revision.

---
 drivers/power/supply/cpcap-battery.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
drivers/power/supply/cpcap-battery.c +6 -5
@@ -82,7 +82,7 @@ struct cpcap_battery_config {
 };
 
 struct cpcap_coulomb_counter_data {
-	s32 sample;		/* 24-bits */
+	s32 sample;		/* 24 or 32 bits */
 	s32 accumulator;
 	s16 offset;		/* 10-bits */
 };
@@ -213,7 +213,7 @@ static int cpcap_battery_get_current(struct cpcap_battery_ddata *ddata)
  * TI or ST coulomb counter in the PMIC.
  */
 static int cpcap_battery_cc_raw_div(struct cpcap_battery_ddata *ddata,
-				    u32 sample, s32 accumulator,
+				    s32 sample, s32 accumulator,
 				    s16 offset, u32 divider)
 {
 	s64 acc;
@@ -224,7 +224,6 @@ static int cpcap_battery_cc_raw_div(struct cpcap_battery_ddata *ddata,
 	if (!divider)
 		return 0;
 
-	sample &= 0xffffff;		/* 24-bits, unsigned */
 	offset &= 0x7ff;		/* 10-bits, signed */
 
 	switch (ddata->vendor) {
@@ -259,7 +258,7 @@ static int cpcap_battery_cc_raw_div(struct cpcap_battery_ddata *ddata,
 
 /* 3600000μAms = 1μAh */
 static int cpcap_battery_cc_to_uah(struct cpcap_battery_ddata *ddata,
-				   u32 sample, s32 accumulator,
+				   s32 sample, s32 accumulator,
 				   s16 offset)
 {
 	return cpcap_battery_cc_raw_div(ddata, sample,
@@ -268,7 +267,7 @@ static int cpcap_battery_cc_to_uah(struct cpcap_battery_ddata *ddata,
 }
 
 static int cpcap_battery_cc_to_ua(struct cpcap_battery_ddata *ddata,
-				  u32 sample, s32 accumulator,
+				  s32 sample, s32 accumulator,
 				  s16 offset)
 {
 	return cpcap_battery_cc_raw_div(ddata, sample,
@@ -312,6 +311,8 @@ cpcap_battery_read_accumulated(struct cpcap_battery_ddata *ddata,
 	/* Sample value CPCAP_REG_CCS1 & 2 */
 	ccd->sample = (buf[1] & 0x0fff) << 16;
 	ccd->sample |= buf[0];
+	if (ddata->vendor == CPCAP_VENDOR_TI)
+		ccd->sample = sign_extend32(24, ccd->sample);
 
 	/* Accumulator value CPCAP_REG_CCA1 & 2 */
 	ccd->accumulator = ((s16)buf[3]) << 16;
Signature · 1 line
2.21.0
Acked-by on v1 3/7
#6 lore raw

Re: [PATCH 3/7] power: supply: cpcap-battery: Fix signed counter sample register

Quoted 7 lines · Tony Lindgren

The accumulator sample register is signed 32-bits wide register on droid 4. And only the earlier version of cpcap has a signed 24-bits wide register. We're currently passing it around as unsigned, so let's fix that and use sign_extend32() for the earlier revision.

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#7 lore raw

PATCH 4/7 power: supply: cpcap-battery: Fix coulomb counter calibration register use

In reply to Tony Lindgren

The coulomb counter calibration is not CCO, it's CCM. And the CCM is nine bits wide signed register, so let's use sign_extend32() for it.

---
 drivers/power/supply/cpcap-battery.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
drivers/power/supply/cpcap-battery.c +7 -9
@@ -84,7 +84,7 @@ struct cpcap_battery_config {
 struct cpcap_coulomb_counter_data {
 	s32 sample;		/* 24 or 32 bits */
 	s32 accumulator;
-	s16 offset;		/* 10-bits */
+	s16 offset;		/* 9 bits */
 };
 
 enum cpcap_battery_state {
@@ -224,8 +224,6 @@ static int cpcap_battery_cc_raw_div(struct cpcap_battery_ddata *ddata,
 	if (!divider)
 		return 0;
 
-	offset &= 0x7ff;		/* 10-bits, signed */
-
 	switch (ddata->vendor) {
 	case CPCAP_VENDOR_ST:
 		cc_lsb = 95374;		/* μAms per LSB */
@@ -318,12 +316,12 @@ cpcap_battery_read_accumulated(struct cpcap_battery_ddata *ddata,
 	ccd->accumulator = ((s16)buf[3]) << 16;
 	ccd->accumulator |= buf[2];
 
-	/* Offset value CPCAP_REG_CCO */
-	ccd->offset = buf[5];
-
-	/* Adjust offset based on mode value CPCAP_REG_CCM? */
-	if (buf[4] >= 0x200)
-		ccd->offset |= 0xfc00;
+	/*
+	 * Coulomb counter calibration offset is CPCAP_REG_CCM,
+	 * REG_CCO seems unused
+	 */
+	ccd->offset = buf[4];
+	ccd->offset = sign_extend32(ccd->offset, 9);
 
 	return cpcap_battery_cc_to_uah(ddata,
 				       ccd->sample,
Signature · 1 line
2.21.0
Acked-by on v1 4/7
#8 lore raw

Re: [PATCH 4/7] power: supply: cpcap-battery: Fix coulomb counter calibration register use

Quoted 5 lines · Tony Lindgren

The coulomb counter calibration is not CCO, it's CCM. And the CCM is nine bits wide signed register, so let's use sign_extend32() for it.

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#9 lore raw

PATCH 5/7 power: supply: cpcap-battery: Use accumulator for current and power average

In reply to Tony Lindgren

We should not use measured current value for average since we have proper coulomb counter values available. Using measured current value should be only used when the value is queried at a higher rate than the 250 ms rate the coulomb counter is configured to run at.

---
 drivers/power/supply/cpcap-battery.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/power/supply/cpcap-battery.c +4 -4
@@ -476,11 +476,11 @@ static int cpcap_battery_get_property(struct power_supply *psy,
 		val->intval = ddata->config.info.voltage_min_design;
 		break;
 	case POWER_SUPPLY_PROP_CURRENT_AVG:
-		if (cached) {
+		sample = latest->cc.sample - previous->cc.sample;
+		if (!sample) {
 			val->intval = cpcap_battery_cc_get_avg_current(ddata);
 			break;
 		}
-		sample = latest->cc.sample - previous->cc.sample;
 		accumulator = latest->cc.accumulator - previous->cc.accumulator;
 		val->intval = cpcap_battery_cc_to_ua(ddata, sample,
 						     accumulator,
@@ -497,13 +497,13 @@ static int cpcap_battery_get_property(struct power_supply *psy,
 		val->intval = div64_s64(tmp, 100);
 		break;
 	case POWER_SUPPLY_PROP_POWER_AVG:
-		if (cached) {
+		sample = latest->cc.sample - previous->cc.sample;
+		if (!sample) {
 			tmp = cpcap_battery_cc_get_avg_current(ddata);
 			tmp *= (latest->voltage / 10000);
 			val->intval = div64_s64(tmp, 100);
 			break;
 		}
-		sample = latest->cc.sample - previous->cc.sample;
 		accumulator = latest->cc.accumulator - previous->cc.accumulator;
 		tmp = cpcap_battery_cc_to_ua(ddata, sample, accumulator,
 					     latest->cc.offset);
Signature · 1 line
2.21.0
#10 lore raw

PATCH 6/7 power: supply: cpcap-battery: Fix pointless EPROBE_DEFER warnings

In reply to Tony Lindgren

With loadable modules we may get the following during init:

could not initialize VBUS or ID IIO: -517

Let's not print any pointless error messages for deferred probe.

---
 drivers/power/supply/cpcap-battery.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/power/supply/cpcap-battery.c +3 -2
@@ -669,8 +669,9 @@ static int cpcap_battery_init_iio(struct cpcap_battery_ddata *ddata)
 	return 0;
 
 out_err:
-	dev_err(ddata->dev, "could not initialize VBUS or ID IIO: %i\n",
-		error);
+	if (error != -EPROBE_DEFER)
+		dev_err(ddata->dev, "could not initialize VBUS or ID IIO: %i\n",
+			error);
 
 	return error;
 }
Signature · 1 line
2.21.0
#11 lore raw

Re: [PATCH 6/7] power: supply: cpcap-battery: Fix pointless EPROBE_DEFER warnings

Quoted 7 lines · Tony Lindgren

With loadable modules we may get the following during init:

could not initialize VBUS or ID IIO: -517

Let's not print any pointless error messages for deferred probe.

6,7: Acked-by: Pavel Machek <[email protected]>

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#12 lore raw

PATCH 7/7 power: supply: cpcap-charger: Fix pointless EPROBE_DEFER warnings

In reply to Tony Lindgren

With loadable modules we may get the following during init:

could not initialize VBUS or ID IIO: -517

Let's not print any pointless error messages for deferred probe.

---
 drivers/power/supply/cpcap-charger.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/power/supply/cpcap-charger.c +3 -2
@@ -574,8 +574,9 @@ static int cpcap_charger_init_iio(struct cpcap_charger_ddata *ddata)
 	return 0;
 
 out_err:
-	dev_err(ddata->dev, "could not initialize VBUS or ID IIO: %i\n",
-		error);
+	if (error != -EPROBE_DEFER)
+		dev_err(ddata->dev, "could not initialize VBUS or ID IIO: %i\n",
+			error);
 
 	return error;
 }
Signature · 1 line
2.21.0
#13 lore raw
In reply to Tony Lindgren

Hi,

Quoted 11 lines · Tony Lindgren

I've been trying to figure out how come the coulomb counter on droid 4 is off especially for the low power consumption values. Turns out the "coulomb counter" values correlate better with average power consumption if we divide the value with number of samples. Otherwise we have a curve instead of flat correlation between the register values and power consumed. I have some patches coming up eventually to fix that, but meanwhile I've noticed some minor issues in general that would be good to have out of the way.

I've only tagged the first one with fixes tag, the other ones can certainly wait for the merge window considering further changes are needed at least for low power consumption values.

Thanks, all queued.

-- Sebastian

#14 lore raw
In reply to Tony Lindgren

Hi all,

Just to follow-up on the information below.

Quoted 7 lines · Tony Lindgren

I've been trying to figure out how come the coulomb counter on droid 4 is off especially for the low power consumption values. Turns out the "coulomb counter" values correlate better with average power consumption if we divide the value with number of samples. Otherwise we have a curve instead of flat correlation between the register values and power consumed. I have some patches coming up eventually to fix that, but meanwhile I've noticed some minor issues in general that would be good to have out of the way.

So I've compared measurements from my power supply to measurements from Baylibre ACME ina226, custom ina226 setup and ARM Energy Probe, and turns out it's my power supply that has started wrong values and propably needs calibration :)

So apologies for bashing the cpcap coulomb counter, it seems it's already quite accurate showing average few mW less over 3 minute sample period compared to my other measurements.

Regards,

Tony