Skip to contentSkip to conversation

power: supply: axp20x_battery: properly report current when discharging

PATCH Superseded Evgeny Boger 3 participants 3 messages Nov 19, 2021 – Dec 2, 2021

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 power: supply: axp20x_battery: properly report current when discharging

As stated in [1], negative current values are used for discharging batteries.

AXP PMICs internally have two different ADC channels for shunt current
measurement: one used during charging and one during discharging. The values reported by these ADCs are unsigned.
While the driver properly selects ADC channel to get the data from, it doesn't apply negative sign when reporting discharging current.

[1] Documentation/ABI/testing/sysfs-class-power

---
 drivers/power/supply/axp20x_battery.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/power/supply/axp20x_battery.c +5 -6
index 18a9db0df4b1..4ed01777035b 100644
@@ -186,7 +186,6 @@ static int axp20x_battery_get_prop(struct power_supply *psy,
 				   union power_supply_propval *val)
 {
 	struct axp20x_batt_ps *axp20x_batt = power_supply_get_drvdata(psy);
-	struct iio_channel *chan;
 	int ret = 0, reg, val1;
 
 	switch (psp) {
@@ -267,11 +266,11 @@ static int axp20x_battery_get_prop(struct power_supply *psy,
 			return ret;
 
 		if (reg & AXP20X_PWR_STATUS_BAT_CHARGING)
-			chan = axp20x_batt->batt_chrg_i;
-		else
-			chan = axp20x_batt->batt_dischrg_i;
-
-		ret = iio_read_channel_processed(chan, &val->intval);
+			ret = iio_read_channel_processed(axp20x_batt->batt_chrg_i, &val->intval);
+		else {
+			ret = iio_read_channel_processed(axp20x_batt->batt_dischrg_i, &val1);
+			val->intval = -val1;
+		}
 		if (ret)
 			return ret;
 
Signature · 1 line
2.25.1
#1 lore raw

Hi Evgeny,

Quoted 29 lines · Evgeny Boger

As stated in [1], negative current values are used for discharging batteries.

AXP PMICs internally have two different ADC channels for shunt current
measurement: one used during charging and one during discharging. The values reported by these ADCs are unsigned.
While the driver properly selects ADC channel to get the data from, it doesn't apply negative sign when reporting discharging current.

[1] Documentation/ABI/testing/sysfs-class-power

---
 drivers/power/supply/axp20x_battery.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/power/supply/axp20x_battery.c +0 -1
index 18a9db0df4b1..4ed01777035b 100644
@@ -186,7 +186,6 @@ static int axp20x_battery_get_prop(struct power_supply *psy,
 				   union power_supply_propval *val)
 {
 	struct axp20x_batt_ps *axp20x_batt = power_supply_get_drvdata(psy);
-	struct iio_channel *chan;
 	int ret = 0, reg, val1;
 
 	switch (psp) {

drivers/power/supply/axp20x_battery.c static int axp20x_battery_get_prop(struct power_supply *psy, · line 270

Quoted 10 lines
drivers/power/supply/axp20x_battery.c +2 -5
@@ -267,11 +266,11 @@ static int axp20x_battery_get_prop(struct power_supply *psy,
 			return ret;
 
 		if (reg & AXP20X_PWR_STATUS_BAT_CHARGING)
-			chan = axp20x_batt->batt_chrg_i;
-		else
-			chan = axp20x_batt->batt_dischrg_i;
-
-		ret = iio_read_channel_processed(chan, &val->intval);
+			ret = iio_read_channel_processed(axp20x_batt->batt_chrg_i, &val->intval);
+		else {

The if should also have curly brackets now, even if it's only one line.

drivers/power/supply/axp20x_battery.c static int axp20x_battery_get_prop(struct power_supply *psy, · line 273

Quoted 3 lines
drivers/power/supply/axp20x_battery.c +3 -0
+			ret = iio_read_channel_processed(axp20x_batt->batt_dischrg_i, &val1);
+			val->intval = -val1;
+		}

Otherwise, since the ABI is not deemed stable (testing in the path of the name), I guess there;s no contract with userspace and we're fine fixing this broken behavior (which userspace might have worked-around themselves and thus we break userspace).

@Sebastian, is that ok?

Cheers,
Quentin

#2 lore raw

Hi,

Quoted 54 lines · Quentin Schulz

Hi Evgeny,

Quoted 29 lines · Evgeny Boger

As stated in [1], negative current values are used for discharging batteries.

AXP PMICs internally have two different ADC channels for shunt current
measurement: one used during charging and one during discharging. The values reported by these ADCs are unsigned.
While the driver properly selects ADC channel to get the data from, it doesn't apply negative sign when reporting discharging current.

[1] Documentation/ABI/testing/sysfs-class-power

---
 drivers/power/supply/axp20x_battery.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/power/supply/axp20x_battery.c +0 -1
index 18a9db0df4b1..4ed01777035b 100644
@@ -186,7 +186,6 @@ static int axp20x_battery_get_prop(struct power_supply *psy,
 				   union power_supply_propval *val)
 {
 	struct axp20x_batt_ps *axp20x_batt = power_supply_get_drvdata(psy);
-	struct iio_channel *chan;
 	int ret = 0, reg, val1;
 
 	switch (psp) {

drivers/power/supply/axp20x_battery.c static int axp20x_battery_get_prop(struct power_supply *psy, · line 270

Quoted 10 lines
drivers/power/supply/axp20x_battery.c +2 -5
@@ -267,11 +266,11 @@ static int axp20x_battery_get_prop(struct power_supply *psy,
 			return ret;
 
 		if (reg & AXP20X_PWR_STATUS_BAT_CHARGING)
-			chan = axp20x_batt->batt_chrg_i;
-		else
-			chan = axp20x_batt->batt_dischrg_i;
-
-		ret = iio_read_channel_processed(chan, &val->intval);
+			ret = iio_read_channel_processed(axp20x_batt->batt_chrg_i, &val->intval);
+		else {

The if should also have curly brackets now, even if it's only one line.

drivers/power/supply/axp20x_battery.c static int axp20x_battery_get_prop(struct power_supply *psy, · line 273

Quoted 3 lines
drivers/power/supply/axp20x_battery.c +3 -0
+			ret = iio_read_channel_processed(axp20x_batt->batt_dischrg_i, &val1);
+			val->intval = -val1;
+		}

Otherwise, since the ABI is not deemed stable (testing in the path of the name), I guess there;s no contract with userspace and we're fine fixing this broken behavior (which userspace might have worked-around themselves and thus we break userspace).

Quoted 1 line

@Sebastian, is that ok?

Sure.

-- Sebastian