diff mbox series

[2/3] power: supply: ab8500: Use iio_read_channel_processed_scale()

Message ID 5668d73b92eb6318c7f094a9a8fa914c909485ca.1719037737.git.christophe.jaillet@wanadoo.fr
State Accepted
Commit dc6ce568afd3452ac682261ea0db570d28f7d82d
Headers show
Series power: supply: ab8500: Improve code related to iio_read_channel_processed() and fix a bug | expand

Commit Message

Christophe JAILLET June 22, 2024, 7:04 a.m. UTC
Instead of rescaling current or voltage channels after the fact, use the
dedicated scaling API. This should reduce any inaccuracies resulting from
the scaling.

This is also slightly more efficient as it saves a function call and a
multiplication.

Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
It is inspired by:
  https://lore.kernel.org/all/20240620212005.821805-1-sean.anderson@linux.dev/
---
 drivers/power/supply/ab8500_charger.c | 28 +++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)

Comments

Jonathan Cameron June 23, 2024, 5:18 p.m. UTC | #1
On Sat, 22 Jun 2024 09:04:25 +0200
Christophe JAILLET <christophe.jaillet@wanadoo.fr> wrote:

> Instead of rescaling current or voltage channels after the fact, use the
> dedicated scaling API. This should reduce any inaccuracies resulting from
> the scaling.
> 
> This is also slightly more efficient as it saves a function call and a
> multiplication.
> 
> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
LGTM
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Linus Walleij June 26, 2024, 10:39 a.m. UTC | #2
On Sat, Jun 22, 2024 at 9:05 AM Christophe JAILLET
<christophe.jaillet@wanadoo.fr> wrote:

> Instead of rescaling current or voltage channels after the fact, use the
> dedicated scaling API. This should reduce any inaccuracies resulting from
> the scaling.
>
> This is also slightly more efficient as it saves a function call and a
> multiplication.
>
> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>

Very nice!
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>

Yours,
Linus Walleij
diff mbox series

Patch

diff --git a/drivers/power/supply/ab8500_charger.c b/drivers/power/supply/ab8500_charger.c
index 4b0ad1b4b4c9..2f06b93682ac 100644
--- a/drivers/power/supply/ab8500_charger.c
+++ b/drivers/power/supply/ab8500_charger.c
@@ -487,7 +487,9 @@  static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di)
 
 	/* Only measure voltage if the charger is connected */
 	if (di->ac.charger_connected) {
-		ret = iio_read_channel_processed(di->adc_main_charger_v, &vch);
+		/* Convert to microvolt, IIO returns millivolt */
+		ret = iio_read_channel_processed_scale(di->adc_main_charger_v,
+						       &vch, 1000);
 		if (ret < 0) {
 			dev_err(di->dev, "%s ADC conv failed,\n", __func__);
 			return ret;
@@ -495,8 +497,7 @@  static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di)
 	} else {
 		vch = 0;
 	}
-	/* Convert to microvolt, IIO returns millivolt */
-	return vch * 1000;
+	return vch;
 }
 
 /**
@@ -541,7 +542,9 @@  static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di)
 
 	/* Only measure voltage if the charger is connected */
 	if (di->usb.charger_connected) {
-		ret = iio_read_channel_processed(di->adc_vbus_v, &vch);
+		/* Convert to microvolt, IIO returns millivolt */
+		ret = iio_read_channel_processed_scale(di->adc_vbus_v,
+						       &vch, 1000);
 		if (ret < 0) {
 			dev_err(di->dev, "%s ADC conv failed,\n", __func__);
 			return ret;
@@ -549,8 +552,7 @@  static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di)
 	} else {
 		vch = 0;
 	}
-	/* Convert to microvolt, IIO returns millivolt */
-	return vch * 1000;
+	return vch;
 }
 
 /**
@@ -566,7 +568,9 @@  static int ab8500_charger_get_usb_current(struct ab8500_charger *di)
 
 	/* Only measure current if the charger is online */
 	if (di->usb.charger_online) {
-		ret = iio_read_channel_processed(di->adc_usb_charger_c, &ich);
+		/* Return microamperes */
+		ret = iio_read_channel_processed_scale(di->adc_usb_charger_c,
+						       &ich, 1000);
 		if (ret < 0) {
 			dev_err(di->dev, "%s ADC conv failed,\n", __func__);
 			return ret;
@@ -574,8 +578,7 @@  static int ab8500_charger_get_usb_current(struct ab8500_charger *di)
 	} else {
 		ich = 0;
 	}
-	/* Return microamperes */
-	return ich * 1000;
+	return ich;
 }
 
 /**
@@ -591,7 +594,9 @@  static int ab8500_charger_get_ac_current(struct ab8500_charger *di)
 
 	/* Only measure current if the charger is online */
 	if (di->ac.charger_online) {
-		ret = iio_read_channel_processed(di->adc_main_charger_c, &ich);
+		/* Return microamperes */
+		ret = iio_read_channel_processed_scale(di->adc_main_charger_c,
+						       &ich, 1000);
 		if (ret < 0) {
 			dev_err(di->dev, "%s ADC conv failed,\n", __func__);
 			return ret;
@@ -599,8 +604,7 @@  static int ab8500_charger_get_ac_current(struct ab8500_charger *di)
 	} else {
 		ich = 0;
 	}
-	/* Return microamperes */
-	return ich * 1000;
+	return ich;
 }
 
 /**