linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3] iio: ad7949: fix wrong ADC result due to incorrect bit mask
@ 2021-02-08 14:27 Wilfried Wessner
  2021-02-08 16:06 ` Andy Shevchenko
  2021-02-09  8:44 ` Couret Charles-Antoine
  0 siblings, 2 replies; 7+ messages in thread
From: Wilfried Wessner @ 2021-02-08 14:27 UTC (permalink / raw)
  To: Lars-Peter Clausen, Michael Hennerich, Jonathan Cameron,
	Peter Meerwald-Stadler
  Cc: linux-iio, Greg KH, Andy Shevchenko, Charles-Antoine Couret

Fixes a wrong bit mask used for the ADC's result, which was caused by an
improper usage of the GENMASK() macro. The bits higher than ADC's 
resolution are undefined and if not masked out correctly, a wrong result 
can be given. The GENMASK() macro indexing is zero based, so the mask has 
to go from [resolution - 1 , 0].

Fixes: 7f40e0614317f ("iio:adc:ad7949: Add AD7949 ADC driver family")

Signed-off-by: Wilfried Wessner <wilfried.wessner@gmail.com>

---

The issue was found in combination of an AD7682 ADC with an ARM based 
iMX7-CPU. The SPI line was analyzed with a logic analyzer and a 
discrepancy between applied voltage level and the ADC reported value 
in user space was observed. Digging into the driver code revealed an 
improper mask used for the ADC-result.


 drivers/iio/adc/ad7949.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/iio/adc/ad7949.c b/drivers/iio/adc/ad7949.c
index 5d597e5050f6..1b4b3203e428 100644
--- a/drivers/iio/adc/ad7949.c
+++ b/drivers/iio/adc/ad7949.c
@@ -91,7 +91,7 @@ static int ad7949_spi_read_channel(struct ad7949_adc_chip *ad7949_adc, int *val,
 	int ret;
 	int i;
 	int bits_per_word = ad7949_adc->resolution;
-	int mask = GENMASK(ad7949_adc->resolution, 0);
+	int mask = GENMASK(ad7949_adc->resolution - 1, 0);
 	struct spi_message msg;
 	struct spi_transfer tx[] = {
 		{
-- 
2.25.1


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

* Re: [PATCH v3] iio: ad7949: fix wrong ADC result due to incorrect bit mask
  2021-02-08 14:27 [PATCH v3] iio: ad7949: fix wrong ADC result due to incorrect bit mask Wilfried Wessner
@ 2021-02-08 16:06 ` Andy Shevchenko
  2021-02-11 18:42   ` Wilfried Wessner
  2021-02-09  8:44 ` Couret Charles-Antoine
  1 sibling, 1 reply; 7+ messages in thread
From: Andy Shevchenko @ 2021-02-08 16:06 UTC (permalink / raw)
  To: Wilfried Wessner
  Cc: Lars-Peter Clausen, Michael Hennerich, Jonathan Cameron,
	Peter Meerwald-Stadler, linux-iio, Greg KH,
	Charles-Antoine Couret

On Mon, Feb 8, 2021 at 4:27 PM Wilfried Wessner
<wilfried.wessner@gmail.com> wrote:
>
> Fixes a wrong bit mask used for the ADC's result, which was caused by an
> improper usage of the GENMASK() macro. The bits higher than ADC's
> resolution are undefined and if not masked out correctly, a wrong result
> can be given. The GENMASK() macro indexing is zero based, so the mask has
> to go from [resolution - 1 , 0].


> Fixes: 7f40e0614317f ("iio:adc:ad7949: Add AD7949 ADC driver family")

>

Shouldn't be blank like here, but I think Jonathan can fix when applying.
Jonathan, can you also amend the subject (I totally forgot about
subsubsystem prefix)?
Should be like:
"iio: adc: ad7949: fix wrong results due to incorrect bit mask"

And FWIW,
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>

> Signed-off-by: Wilfried Wessner <wilfried.wessner@gmail.com>
>
> ---
>
> The issue was found in combination of an AD7682 ADC with an ARM based
> iMX7-CPU. The SPI line was analyzed with a logic analyzer and a
> discrepancy between applied voltage level and the ADC reported value
> in user space was observed. Digging into the driver code revealed an
> improper mask used for the ADC-result.
>
>
>  drivers/iio/adc/ad7949.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/iio/adc/ad7949.c b/drivers/iio/adc/ad7949.c
> index 5d597e5050f6..1b4b3203e428 100644
> --- a/drivers/iio/adc/ad7949.c
> +++ b/drivers/iio/adc/ad7949.c
> @@ -91,7 +91,7 @@ static int ad7949_spi_read_channel(struct ad7949_adc_chip *ad7949_adc, int *val,
>         int ret;
>         int i;
>         int bits_per_word = ad7949_adc->resolution;
> -       int mask = GENMASK(ad7949_adc->resolution, 0);
> +       int mask = GENMASK(ad7949_adc->resolution - 1, 0);
>         struct spi_message msg;
>         struct spi_transfer tx[] = {
>                 {
> --
> 2.25.1
>


-- 
With Best Regards,
Andy Shevchenko

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

* Re: [PATCH v3] iio: ad7949: fix wrong ADC result due to incorrect bit mask
  2021-02-08 14:27 [PATCH v3] iio: ad7949: fix wrong ADC result due to incorrect bit mask Wilfried Wessner
  2021-02-08 16:06 ` Andy Shevchenko
@ 2021-02-09  8:44 ` Couret Charles-Antoine
  1 sibling, 0 replies; 7+ messages in thread
From: Couret Charles-Antoine @ 2021-02-09  8:44 UTC (permalink / raw)
  To: Wilfried Wessner, Lars-Peter Clausen, Michael Hennerich,
	Jonathan Cameron, Peter Meerwald-Stadler
  Cc: linux-iio, Greg KH, Andy Shevchenko

Le 08/02/2021 à 15:27, Wilfried Wessner a écrit :
> Fixes a wrong bit mask used for the ADC's result, which was caused by an
> improper usage of the GENMASK() macro. The bits higher than ADC's 
> resolution are undefined and if not masked out correctly, a wrong result 
> can be given. The GENMASK() macro indexing is zero based, so the mask has 
> to go from [resolution - 1 , 0].
>
> Fixes: 7f40e0614317f ("iio:adc:ad7949: Add AD7949 ADC driver family")
>
> Signed-off-by: Wilfried Wessner <wilfried.wessner@gmail.com>
>
> ---
>
> The issue was found in combination of an AD7682 ADC with an ARM based 
> iMX7-CPU. The SPI line was analyzed with a logic analyzer and a 
> discrepancy between applied voltage level and the ADC reported value 
> in user space was observed. Digging into the driver code revealed an 
> improper mask used for the ADC-result.
>
>
>  drivers/iio/adc/ad7949.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/iio/adc/ad7949.c b/drivers/iio/adc/ad7949.c
> index 5d597e5050f6..1b4b3203e428 100644
> --- a/drivers/iio/adc/ad7949.c
> +++ b/drivers/iio/adc/ad7949.c
> @@ -91,7 +91,7 @@ static int ad7949_spi_read_channel(struct ad7949_adc_chip *ad7949_adc, int *val,
>  	int ret;
>  	int i;
>  	int bits_per_word = ad7949_adc->resolution;
> -	int mask = GENMASK(ad7949_adc->resolution, 0);
> +	int mask = GENMASK(ad7949_adc->resolution - 1, 0);
>  	struct spi_message msg;
>  	struct spi_transfer tx[] = {
>  		{

Hi,

Thank you for the fix. :)

Reviewed-by: Charles-Antoine Couret <charles-antoine.couret@essensium.com>

Regards,

Charles-Antoine Couret


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

* Re: [PATCH v3] iio: ad7949: fix wrong ADC result due to incorrect bit mask
  2021-02-08 16:06 ` Andy Shevchenko
@ 2021-02-11 18:42   ` Wilfried Wessner
  2021-02-11 19:24     ` Andy Shevchenko
  0 siblings, 1 reply; 7+ messages in thread
From: Wilfried Wessner @ 2021-02-11 18:42 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Lars-Peter Clausen, Michael Hennerich, Peter Meerwald-Stadler,
	linux-iio, Greg KH, Charles-Antoine Couret, Andy Shevchenko

On Mon, Feb 8, 2021 at 5:06 PM Andy Shevchenko
<andy.shevchenko@gmail.com> wrote:
>
> On Mon, Feb 8, 2021 at 4:27 PM Wilfried Wessner
> <wilfried.wessner@gmail.com> wrote:
> >
> > Fixes a wrong bit mask used for the ADC's result, which was caused by an
> > improper usage of the GENMASK() macro. The bits higher than ADC's
> > resolution are undefined and if not masked out correctly, a wrong result
> > can be given. The GENMASK() macro indexing is zero based, so the mask has
> > to go from [resolution - 1 , 0].
>
>
> > Fixes: 7f40e0614317f ("iio:adc:ad7949: Add AD7949 ADC driver family")
>
> >
>
> Shouldn't be blank like here, but I think Jonathan can fix when applying.
> Jonathan, can you also amend the subject (I totally forgot about
> subsubsystem prefix)?
> Should be like:
> "iio: adc: ad7949: fix wrong results due to incorrect bit mask"

Should I send a v4 with the changes proposed by Andy?
It would change the subject.

And if so, should I add the tags:
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Reviewed-by: Charles-Antoine Couret <charles-antoine.couret@essensium.com>

Please advise, thank you!
Best Regards,
Willi



>
> And FWIW,
> Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
>
> > Signed-off-by: Wilfried Wessner <wilfried.wessner@gmail.com>
> >
> > ---
> >
> > The issue was found in combination of an AD7682 ADC with an ARM based
> > iMX7-CPU. The SPI line was analyzed with a logic analyzer and a
> > discrepancy between applied voltage level and the ADC reported value
> > in user space was observed. Digging into the driver code revealed an
> > improper mask used for the ADC-result.
> >
> >
> >  drivers/iio/adc/ad7949.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/drivers/iio/adc/ad7949.c b/drivers/iio/adc/ad7949.c
> > index 5d597e5050f6..1b4b3203e428 100644
> > --- a/drivers/iio/adc/ad7949.c
> > +++ b/drivers/iio/adc/ad7949.c
> > @@ -91,7 +91,7 @@ static int ad7949_spi_read_channel(struct ad7949_adc_chip *ad7949_adc, int *val,
> >         int ret;
> >         int i;
> >         int bits_per_word = ad7949_adc->resolution;
> > -       int mask = GENMASK(ad7949_adc->resolution, 0);
> > +       int mask = GENMASK(ad7949_adc->resolution - 1, 0);
> >         struct spi_message msg;
> >         struct spi_transfer tx[] = {
> >                 {
> > --
> > 2.25.1
> >
>
>
> --
> With Best Regards,
> Andy Shevchenko

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

* Re: [PATCH v3] iio: ad7949: fix wrong ADC result due to incorrect bit mask
  2021-02-11 18:42   ` Wilfried Wessner
@ 2021-02-11 19:24     ` Andy Shevchenko
  2021-02-12 19:04       ` Jonathan Cameron
  0 siblings, 1 reply; 7+ messages in thread
From: Andy Shevchenko @ 2021-02-11 19:24 UTC (permalink / raw)
  To: Wilfried Wessner
  Cc: Jonathan Cameron, Lars-Peter Clausen, Michael Hennerich,
	Peter Meerwald-Stadler, linux-iio, Greg KH,
	Charles-Antoine Couret

On Thu, Feb 11, 2021 at 8:42 PM Wilfried Wessner
<wilfried.wessner@gmail.com> wrote:
> On Mon, Feb 8, 2021 at 5:06 PM Andy Shevchenko
> <andy.shevchenko@gmail.com> wrote:
> > On Mon, Feb 8, 2021 at 4:27 PM Wilfried Wessner
> > <wilfried.wessner@gmail.com> wrote:

...

> > Shouldn't be blank like here, but I think Jonathan can fix when applying.
> > Jonathan, can you also amend the subject (I totally forgot about
> > subsubsystem prefix)?
> > Should be like:
> > "iio: adc: ad7949: fix wrong results due to incorrect bit mask"
>
> Should I send a v4 with the changes proposed by Andy?
> It would change the subject.

Depends on you. Jonothan usually processes the queue during weekends,
so no hurry.

> And if so, should I add the tags:
> Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
> Reviewed-by: Charles-Antoine Couret <charles-antoine.couret@essensium.com>

If resend, yes, you need to add them.

-- 
With Best Regards,
Andy Shevchenko

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

* Re: [PATCH v3] iio: ad7949: fix wrong ADC result due to incorrect bit mask
  2021-02-11 19:24     ` Andy Shevchenko
@ 2021-02-12 19:04       ` Jonathan Cameron
  2021-02-12 19:19         ` Wilfried Wessner
  0 siblings, 1 reply; 7+ messages in thread
From: Jonathan Cameron @ 2021-02-12 19:04 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Wilfried Wessner, Lars-Peter Clausen, Michael Hennerich,
	Peter Meerwald-Stadler, linux-iio, Greg KH,
	Charles-Antoine Couret

On Thu, 11 Feb 2021 21:24:04 +0200
Andy Shevchenko <andy.shevchenko@gmail.com> wrote:

> On Thu, Feb 11, 2021 at 8:42 PM Wilfried Wessner
> <wilfried.wessner@gmail.com> wrote:
> > On Mon, Feb 8, 2021 at 5:06 PM Andy Shevchenko
> > <andy.shevchenko@gmail.com> wrote:  
> > > On Mon, Feb 8, 2021 at 4:27 PM Wilfried Wessner
> > > <wilfried.wessner@gmail.com> wrote:  
> 
> ...
> 
> > > Shouldn't be blank like here, but I think Jonathan can fix when applying.
> > > Jonathan, can you also amend the subject (I totally forgot about
> > > subsubsystem prefix)?
> > > Should be like:
> > > "iio: adc: ad7949: fix wrong results due to incorrect bit mask"  
> >
> > Should I send a v4 with the changes proposed by Andy?
> > It would change the subject.  
> 
> Depends on you. Jonothan usually processes the queue during weekends,
> so no hurry.
> 
> > And if so, should I add the tags:
> > Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
> > Reviewed-by: Charles-Antoine Couret <charles-antoine.couret@essensium.com>  
> 
> If resend, yes, you need to add them.
> 
Applied with the minor spacing and title tweak Andy suggested

Applied to the fixes-togreg branch of iio.git. Given this has been
there a while, I'm going to wait until after the merge window to
send this upstream.  So it will be a few weeks.

Thanks,

Jonathan



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

* Re: [PATCH v3] iio: ad7949: fix wrong ADC result due to incorrect bit mask
  2021-02-12 19:04       ` Jonathan Cameron
@ 2021-02-12 19:19         ` Wilfried Wessner
  0 siblings, 0 replies; 7+ messages in thread
From: Wilfried Wessner @ 2021-02-12 19:19 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Andy Shevchenko, Lars-Peter Clausen, Michael Hennerich,
	Peter Meerwald-Stadler, linux-iio, Greg KH,
	Charles-Antoine Couret

On Fri, Feb 12, 2021 at 8:05 PM Jonathan Cameron <jic23@kernel.org> wrote:
>
> On Thu, 11 Feb 2021 21:24:04 +0200
> Andy Shevchenko <andy.shevchenko@gmail.com> wrote:
>
> > On Thu, Feb 11, 2021 at 8:42 PM Wilfried Wessner
> > <wilfried.wessner@gmail.com> wrote:
> > > On Mon, Feb 8, 2021 at 5:06 PM Andy Shevchenko
> > > <andy.shevchenko@gmail.com> wrote:
> > > > On Mon, Feb 8, 2021 at 4:27 PM Wilfried Wessner
> > > > <wilfried.wessner@gmail.com> wrote:
> >
> > ...
> >
> > > > Shouldn't be blank like here, but I think Jonathan can fix when applying.
> > > > Jonathan, can you also amend the subject (I totally forgot about
> > > > subsubsystem prefix)?
> > > > Should be like:
> > > > "iio: adc: ad7949: fix wrong results due to incorrect bit mask"
> > >
> > > Should I send a v4 with the changes proposed by Andy?
> > > It would change the subject.
> >
> > Depends on you. Jonothan usually processes the queue during weekends,
> > so no hurry.
> >
> > > And if so, should I add the tags:
> > > Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
> > > Reviewed-by: Charles-Antoine Couret <charles-antoine.couret@essensium.com>
> >
> > If resend, yes, you need to add them.
> >
> Applied with the minor spacing and title tweak Andy suggested
>
> Applied to the fixes-togreg branch of iio.git. Given this has been
> there a while, I'm going to wait until after the merge window to
> send this upstream.  So it will be a few weeks.
>
> Thanks,
>
> Jonathan
>
>
Great, thank you!

Best regards,
Willi

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

end of thread, other threads:[~2021-02-12 19:21 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-08 14:27 [PATCH v3] iio: ad7949: fix wrong ADC result due to incorrect bit mask Wilfried Wessner
2021-02-08 16:06 ` Andy Shevchenko
2021-02-11 18:42   ` Wilfried Wessner
2021-02-11 19:24     ` Andy Shevchenko
2021-02-12 19:04       ` Jonathan Cameron
2021-02-12 19:19         ` Wilfried Wessner
2021-02-09  8:44 ` Couret Charles-Antoine

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).