[PATCH v2] iio: adc: ad7124: fix temperature channel

David Lechner posted 1 patch 2 months, 1 week ago
drivers/iio/adc/ad7124.c | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)
[PATCH v2] iio: adc: ad7124: fix temperature channel
Posted by David Lechner 2 months, 1 week ago
Fix temperature channel not working due to gain and offset not being
initialized.  For channels other than the voltage ones calibration is
skipped (which is OK).  However that results in the calibration register
values tracked in st->channels[i].cfg all being zero.  These zeros are
later written to hardware before a measurement is made which caused the
raw temperature readings to be always 8388608 (0x800000).

To fix it, we just make sure the gain and offset values are set to the
default values and still return early without doing an internal
calibration.

While here, add a comment explaining why we don't bother calibrating
the temperature channel.

Fixes: 47036a03a303 ("iio: adc: ad7124: Implement internal calibration at probe time")
Reviewed-by: Marcelo Schmitt <marcelo.schmitt@analog.com>
Signed-off-by: David Lechner <dlechner@baylibre.com>
---
Changes in v2:
- Improved commit message.
- Expanded code comment.
- Link to v1: https://lore.kernel.org/r/20250923-iio-adc-ad7124-fix-temperature-channel-v1-1-e421c98c0d72@baylibre.com
---
 drivers/iio/adc/ad7124.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/drivers/iio/adc/ad7124.c b/drivers/iio/adc/ad7124.c
index 9d58ced7371d0af7004a81153888714e9795d4f4..2a449baf5b5645c8dbcc6af671aaafb86db96011 100644
--- a/drivers/iio/adc/ad7124.c
+++ b/drivers/iio/adc/ad7124.c
@@ -1482,10 +1482,6 @@ static int __ad7124_calibrate_all(struct ad7124_state *st, struct iio_dev *indio
 	int ret, i;
 
 	for (i = 0; i < st->num_channels; i++) {
-
-		if (indio_dev->channels[i].type != IIO_VOLTAGE)
-			continue;
-
 		/*
 		 * For calibration the OFFSET register should hold its reset default
 		 * value. For the GAIN register there is no such requirement but
@@ -1495,6 +1491,14 @@ static int __ad7124_calibrate_all(struct ad7124_state *st, struct iio_dev *indio
 		st->channels[i].cfg.calibration_offset = 0x800000;
 		st->channels[i].cfg.calibration_gain = st->gain_default;
 
+		/*
+		 * Only the main voltage input channels are important enough
+		 * to be automatically calibrated here. For everything else,
+		 * just use the default values set above.
+		 */
+		if (indio_dev->channels[i].type != IIO_VOLTAGE)
+			continue;
+
 		/*
 		 * Full-scale calibration isn't supported at gain 1, so skip in
 		 * that case. Note that untypically full-scale calibration has

---
base-commit: a9682f53c2d1678b93a123cdaa260e955430bc5c
change-id: 20250923-iio-adc-ad7124-fix-temperature-channel-5900f7302886

Best regards,
-- 
David Lechner <dlechner@baylibre.com>
Re: [PATCH v2] iio: adc: ad7124: fix temperature channel
Posted by Uwe Kleine-König 2 months ago
Hello David,

On Fri, Oct 10, 2025 at 03:24:31PM -0500, David Lechner wrote:
> Fix temperature channel not working due to gain and offset not being
> initialized.  For channels other than the voltage ones calibration is
> skipped (which is OK).  However that results in the calibration register
> values tracked in st->channels[i].cfg all being zero.  These zeros are
> later written to hardware before a measurement is made which caused the
> raw temperature readings to be always 8388608 (0x800000).
> 
> To fix it, we just make sure the gain and offset values are set to the
> default values and still return early without doing an internal
> calibration.
> 
> While here, add a comment explaining why we don't bother calibrating
> the temperature channel.
> 
> Fixes: 47036a03a303 ("iio: adc: ad7124: Implement internal calibration at probe time")
> Reviewed-by: Marcelo Schmitt <marcelo.schmitt@analog.com>
> Signed-off-by: David Lechner <dlechner@baylibre.com>
> ---
> Changes in v2:
> - Improved commit message.
> - Expanded code comment.

I like both improvements, thanks!

Reviewed-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
Re: [PATCH v2] iio: adc: ad7124: fix temperature channel
Posted by Jonathan Cameron 2 months ago
On Sun, 12 Oct 2025 14:47:13 +0200
Uwe Kleine-König <u.kleine-koenig@baylibre.com> wrote:

> Hello David,
> 
> On Fri, Oct 10, 2025 at 03:24:31PM -0500, David Lechner wrote:
> > Fix temperature channel not working due to gain and offset not being
> > initialized.  For channels other than the voltage ones calibration is
> > skipped (which is OK).  However that results in the calibration register
> > values tracked in st->channels[i].cfg all being zero.  These zeros are
> > later written to hardware before a measurement is made which caused the
> > raw temperature readings to be always 8388608 (0x800000).
> > 
> > To fix it, we just make sure the gain and offset values are set to the
> > default values and still return early without doing an internal
> > calibration.
> > 
> > While here, add a comment explaining why we don't bother calibrating
> > the temperature channel.
> > 
> > Fixes: 47036a03a303 ("iio: adc: ad7124: Implement internal calibration at probe time")
> > Reviewed-by: Marcelo Schmitt <marcelo.schmitt@analog.com>
> > Signed-off-by: David Lechner <dlechner@baylibre.com>
> > ---
> > Changes in v2:
> > - Improved commit message.
> > - Expanded code comment.  
> 
> I like both improvements, thanks!
> 
> Reviewed-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>

Applied to the temporary branch I'm queuing fixes on until I can rebase on rc1.
+ marked for stable.