The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH] iio: adc: ad7124: fix channel lookup in syscalib functions
@ 2025-07-26 16:28 David Lechner
  2025-07-27 12:31 ` Jonathan Cameron
  0 siblings, 1 reply; 3+ messages in thread
From: David Lechner @ 2025-07-26 16:28 UTC (permalink / raw)
  To: Michael Hennerich, Jonathan Cameron, Nuno Sá,
	Andy Shevchenko, Uwe Kleine-König
  Cc: Jonathan Cameron, linux-iio, linux-kernel, David Lechner

Fix possible incorrect channel lookup in the syscalib functions by using
the correct channel address instead of the channel number.

In the ad7124 driver, the channel field of struct iio_chan_spec is the
input pin number of the positive input of the channel. This can be, but
is not always the same as the index in the channels array. The correct
index in the channels array is stored in the address field (and also
scan_index). We use the address field to perform the correct lookup.

Fixes: 47036a03a303 ("iio: adc: ad7124: Implement internal calibration at probe time")
Signed-off-by: David Lechner <dlechner@baylibre.com>
---
 drivers/iio/adc/ad7124.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/drivers/iio/adc/ad7124.c b/drivers/iio/adc/ad7124.c
index 9808df2e92424283a86e9c105492c7447d071e44..4d8c6bafd1c3171054c72a0d2b13d6b1afc4e51a 100644
--- a/drivers/iio/adc/ad7124.c
+++ b/drivers/iio/adc/ad7124.c
@@ -849,7 +849,7 @@ enum {
 static int ad7124_syscalib_locked(struct ad7124_state *st, const struct iio_chan_spec *chan)
 {
 	struct device *dev = &st->sd.spi->dev;
-	struct ad7124_channel *ch = &st->channels[chan->channel];
+	struct ad7124_channel *ch = &st->channels[chan->address];
 	int ret;
 
 	if (ch->syscalib_mode == AD7124_SYSCALIB_ZERO_SCALE) {
@@ -865,8 +865,8 @@ static int ad7124_syscalib_locked(struct ad7124_state *st, const struct iio_chan
 		if (ret < 0)
 			return ret;
 
-		dev_dbg(dev, "offset for channel %d after zero-scale calibration: 0x%x\n",
-			chan->channel, ch->cfg.calibration_offset);
+		dev_dbg(dev, "offset for channel %lu after zero-scale calibration: 0x%x\n",
+			chan->address, ch->cfg.calibration_offset);
 	} else {
 		ch->cfg.calibration_gain = st->gain_default;
 
@@ -880,8 +880,8 @@ static int ad7124_syscalib_locked(struct ad7124_state *st, const struct iio_chan
 		if (ret < 0)
 			return ret;
 
-		dev_dbg(dev, "gain for channel %d after full-scale calibration: 0x%x\n",
-			chan->channel, ch->cfg.calibration_gain);
+		dev_dbg(dev, "gain for channel %lu after full-scale calibration: 0x%x\n",
+			chan->address, ch->cfg.calibration_gain);
 	}
 
 	return 0;
@@ -924,7 +924,7 @@ static int ad7124_set_syscalib_mode(struct iio_dev *indio_dev,
 {
 	struct ad7124_state *st = iio_priv(indio_dev);
 
-	st->channels[chan->channel].syscalib_mode = mode;
+	st->channels[chan->address].syscalib_mode = mode;
 
 	return 0;
 }
@@ -934,7 +934,7 @@ static int ad7124_get_syscalib_mode(struct iio_dev *indio_dev,
 {
 	struct ad7124_state *st = iio_priv(indio_dev);
 
-	return st->channels[chan->channel].syscalib_mode;
+	return st->channels[chan->address].syscalib_mode;
 }
 
 static const struct iio_enum ad7124_syscalib_mode_enum = {

---
base-commit: e4d9886ad25adae72f38f2b12f41649b101581ae
change-id: 20250726-iio-adc-ad7124-fix-channel-lookup-in-syscalib-e28c933ead2a

Best regards,
-- 
David Lechner <dlechner@baylibre.com>


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] iio: adc: ad7124: fix channel lookup in syscalib functions
  2025-07-26 16:28 [PATCH] iio: adc: ad7124: fix channel lookup in syscalib functions David Lechner
@ 2025-07-27 12:31 ` Jonathan Cameron
  2025-07-28  9:12   ` Nuno Sá
  0 siblings, 1 reply; 3+ messages in thread
From: Jonathan Cameron @ 2025-07-27 12:31 UTC (permalink / raw)
  To: David Lechner
  Cc: Michael Hennerich, Nuno Sá, Andy Shevchenko,
	Uwe Kleine-König, Jonathan Cameron, linux-iio, linux-kernel

On Sat, 26 Jul 2025 11:28:48 -0500
David Lechner <dlechner@baylibre.com> wrote:

> Fix possible incorrect channel lookup in the syscalib functions by using
> the correct channel address instead of the channel number.
> 
> In the ad7124 driver, the channel field of struct iio_chan_spec is the
> input pin number of the positive input of the channel. This can be, but
> is not always the same as the index in the channels array. The correct
> index in the channels array is stored in the address field (and also
> scan_index). We use the address field to perform the correct lookup.
> 
> Fixes: 47036a03a303 ("iio: adc: ad7124: Implement internal calibration at probe time")
> Signed-off-by: David Lechner <dlechner@baylibre.com>
Seems fine to me and i'll queue it up, but I would welcome another set of
eyes on this one.

Thanks,

Jonathan

> ---
>  drivers/iio/adc/ad7124.c | 14 +++++++-------
>  1 file changed, 7 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/iio/adc/ad7124.c b/drivers/iio/adc/ad7124.c
> index 9808df2e92424283a86e9c105492c7447d071e44..4d8c6bafd1c3171054c72a0d2b13d6b1afc4e51a 100644
> --- a/drivers/iio/adc/ad7124.c
> +++ b/drivers/iio/adc/ad7124.c
> @@ -849,7 +849,7 @@ enum {
>  static int ad7124_syscalib_locked(struct ad7124_state *st, const struct iio_chan_spec *chan)
>  {
>  	struct device *dev = &st->sd.spi->dev;
> -	struct ad7124_channel *ch = &st->channels[chan->channel];
> +	struct ad7124_channel *ch = &st->channels[chan->address];
>  	int ret;
>  
>  	if (ch->syscalib_mode == AD7124_SYSCALIB_ZERO_SCALE) {
> @@ -865,8 +865,8 @@ static int ad7124_syscalib_locked(struct ad7124_state *st, const struct iio_chan
>  		if (ret < 0)
>  			return ret;
>  
> -		dev_dbg(dev, "offset for channel %d after zero-scale calibration: 0x%x\n",
> -			chan->channel, ch->cfg.calibration_offset);
> +		dev_dbg(dev, "offset for channel %lu after zero-scale calibration: 0x%x\n",
> +			chan->address, ch->cfg.calibration_offset);
>  	} else {
>  		ch->cfg.calibration_gain = st->gain_default;
>  
> @@ -880,8 +880,8 @@ static int ad7124_syscalib_locked(struct ad7124_state *st, const struct iio_chan
>  		if (ret < 0)
>  			return ret;
>  
> -		dev_dbg(dev, "gain for channel %d after full-scale calibration: 0x%x\n",
> -			chan->channel, ch->cfg.calibration_gain);
> +		dev_dbg(dev, "gain for channel %lu after full-scale calibration: 0x%x\n",
> +			chan->address, ch->cfg.calibration_gain);
>  	}
>  
>  	return 0;
> @@ -924,7 +924,7 @@ static int ad7124_set_syscalib_mode(struct iio_dev *indio_dev,
>  {
>  	struct ad7124_state *st = iio_priv(indio_dev);
>  
> -	st->channels[chan->channel].syscalib_mode = mode;
> +	st->channels[chan->address].syscalib_mode = mode;
>  
>  	return 0;
>  }
> @@ -934,7 +934,7 @@ static int ad7124_get_syscalib_mode(struct iio_dev *indio_dev,
>  {
>  	struct ad7124_state *st = iio_priv(indio_dev);
>  
> -	return st->channels[chan->channel].syscalib_mode;
> +	return st->channels[chan->address].syscalib_mode;
>  }
>  
>  static const struct iio_enum ad7124_syscalib_mode_enum = {
> 
> ---
> base-commit: e4d9886ad25adae72f38f2b12f41649b101581ae
> change-id: 20250726-iio-adc-ad7124-fix-channel-lookup-in-syscalib-e28c933ead2a
> 
> Best regards,


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] iio: adc: ad7124: fix channel lookup in syscalib functions
  2025-07-27 12:31 ` Jonathan Cameron
@ 2025-07-28  9:12   ` Nuno Sá
  0 siblings, 0 replies; 3+ messages in thread
From: Nuno Sá @ 2025-07-28  9:12 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: David Lechner, Michael Hennerich, Nuno Sá, Andy Shevchenko,
	Uwe Kleine-König, Jonathan Cameron, linux-iio, linux-kernel

On Sun, Jul 27, 2025 at 01:31:35PM +0100, Jonathan Cameron wrote:
> On Sat, 26 Jul 2025 11:28:48 -0500
> David Lechner <dlechner@baylibre.com> wrote:
> 
> > Fix possible incorrect channel lookup in the syscalib functions by using
> > the correct channel address instead of the channel number.
> > 
> > In the ad7124 driver, the channel field of struct iio_chan_spec is the
> > input pin number of the positive input of the channel. This can be, but
> > is not always the same as the index in the channels array. The correct
> > index in the channels array is stored in the address field (and also
> > scan_index). We use the address field to perform the correct lookup.
> > 
> > Fixes: 47036a03a303 ("iio: adc: ad7124: Implement internal calibration at probe time")
> > Signed-off-by: David Lechner <dlechner@baylibre.com>
> Seems fine to me and i'll queue it up, but I would welcome another set of
> eyes on this one.
> 
> Thanks,
> 
> Jonathan
> 

The fix seems valid to me:

Reviewed-by: Nuno Sá <nuno.sa@analog.com>

> > ---
> >  drivers/iio/adc/ad7124.c | 14 +++++++-------
> >  1 file changed, 7 insertions(+), 7 deletions(-)
> > 
> > diff --git a/drivers/iio/adc/ad7124.c b/drivers/iio/adc/ad7124.c
> > index 9808df2e92424283a86e9c105492c7447d071e44..4d8c6bafd1c3171054c72a0d2b13d6b1afc4e51a 100644
> > --- a/drivers/iio/adc/ad7124.c
> > +++ b/drivers/iio/adc/ad7124.c
> > @@ -849,7 +849,7 @@ enum {
> >  static int ad7124_syscalib_locked(struct ad7124_state *st, const struct iio_chan_spec *chan)
> >  {
> >  	struct device *dev = &st->sd.spi->dev;
> > -	struct ad7124_channel *ch = &st->channels[chan->channel];
> > +	struct ad7124_channel *ch = &st->channels[chan->address];
> >  	int ret;
> >  
> >  	if (ch->syscalib_mode == AD7124_SYSCALIB_ZERO_SCALE) {
> > @@ -865,8 +865,8 @@ static int ad7124_syscalib_locked(struct ad7124_state *st, const struct iio_chan
> >  		if (ret < 0)
> >  			return ret;
> >  
> > -		dev_dbg(dev, "offset for channel %d after zero-scale calibration: 0x%x\n",
> > -			chan->channel, ch->cfg.calibration_offset);
> > +		dev_dbg(dev, "offset for channel %lu after zero-scale calibration: 0x%x\n",
> > +			chan->address, ch->cfg.calibration_offset);
> >  	} else {
> >  		ch->cfg.calibration_gain = st->gain_default;
> >  
> > @@ -880,8 +880,8 @@ static int ad7124_syscalib_locked(struct ad7124_state *st, const struct iio_chan
> >  		if (ret < 0)
> >  			return ret;
> >  
> > -		dev_dbg(dev, "gain for channel %d after full-scale calibration: 0x%x\n",
> > -			chan->channel, ch->cfg.calibration_gain);
> > +		dev_dbg(dev, "gain for channel %lu after full-scale calibration: 0x%x\n",
> > +			chan->address, ch->cfg.calibration_gain);
> >  	}
> >  
> >  	return 0;
> > @@ -924,7 +924,7 @@ static int ad7124_set_syscalib_mode(struct iio_dev *indio_dev,
> >  {
> >  	struct ad7124_state *st = iio_priv(indio_dev);
> >  
> > -	st->channels[chan->channel].syscalib_mode = mode;
> > +	st->channels[chan->address].syscalib_mode = mode;
> >  
> >  	return 0;
> >  }
> > @@ -934,7 +934,7 @@ static int ad7124_get_syscalib_mode(struct iio_dev *indio_dev,
> >  {
> >  	struct ad7124_state *st = iio_priv(indio_dev);
> >  
> > -	return st->channels[chan->channel].syscalib_mode;
> > +	return st->channels[chan->address].syscalib_mode;
> >  }
> >  
> >  static const struct iio_enum ad7124_syscalib_mode_enum = {
> > 
> > ---
> > base-commit: e4d9886ad25adae72f38f2b12f41649b101581ae
> > change-id: 20250726-iio-adc-ad7124-fix-channel-lookup-in-syscalib-e28c933ead2a
> > 
> > Best regards,
> 

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-07-28  9:12 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-26 16:28 [PATCH] iio: adc: ad7124: fix channel lookup in syscalib functions David Lechner
2025-07-27 12:31 ` Jonathan Cameron
2025-07-28  9:12   ` Nuno Sá

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox