Linux IIO development
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Jonathan Cameron <Jonathan.Cameron@Huawei.com>
Cc: Ian Ray <ian.ray@ge.com>, <lars@metafoo.de>,
	<Michael.Hennerich@analog.com>, <linux-kernel@vger.kernel.org>,
	<linux-iio@vger.kernel.org>
Subject: Re: [PATCH] drivers: iio: adc: ltc2497: fix LSB shift
Date: Sat, 28 Jan 2023 18:00:32 +0000	[thread overview]
Message-ID: <20230128180032.2905cee7@jic23-huawei> (raw)
In-Reply-To: <20230127174756.00007299@Huawei.com>

On Fri, 27 Jan 2023 17:47:56 +0000
Jonathan Cameron <Jonathan.Cameron@Huawei.com> wrote:

> On Fri, 27 Jan 2023 14:57:14 +0200
> Ian Ray <ian.ray@ge.com> wrote:
> 
> > Correct the "sub_lsb" shift for both ltc2497 and ltc2499.  

I dropped the ltc2499 mention here as that one seems to be unaffected
by this patch (beyond as a simplification)

> 
> Hi Iain, Thanks for the patch.
> 
> > 
> > An earlier version of the code shifted by 14 but this was a consequence
> > of reading three bytes into a __be32 buffer and using be32_to_cpu(), so
> > eight extra bits needed to be skipped.  Now we use get_unaligned_be24()
> > and thus the additional skip is wrong.
> > 
> > Fixes 2187cfe ("drivers: iio: adc: ltc2497: LTC2499 support")  
> 
> This isn't in standard Fixes tag format.  If nothing else comes
> up I'll fix up whilst applying.
> 
> > Signed-off-by: Ian Ray <ian.ray@ge.com>

Fix looks good to me.  (I noted my reasoning below on basis someone might
find it useful) 

Applied to the fixes-togreg branch of iio.git with fixes tag fixed and
marked for stable.

Please take a look at:
https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git/commit/?h=fixes-togreg&id=6327a930ab7bfa1ab33bcdffd5f5f4b1e7131504
to check I didn't mess anything up.

Thanks,

Jonathan


> > ---
> >  drivers/iio/adc/ltc2497.c | 6 ++----
> >  1 file changed, 2 insertions(+), 4 deletions(-)
> > 
> > diff --git a/drivers/iio/adc/ltc2497.c b/drivers/iio/adc/ltc2497.c
> > index 17370c5..ec198c6 100644
> > --- a/drivers/iio/adc/ltc2497.c
> > +++ b/drivers/iio/adc/ltc2497.c
> > @@ -28,7 +28,6 @@ struct ltc2497_driverdata {
> >  	struct ltc2497core_driverdata common_ddata;
> >  	struct i2c_client *client;
> >  	u32 recv_size;
> > -	u32 sub_lsb;
> >  	/*
> >  	 * DMA (thus cache coherency maintenance) may require the
> >  	 * transfer buffers to live in their own cache lines.
> > @@ -65,10 +64,10 @@ static int ltc2497_result_and_measure(struct ltc2497core_driverdata *ddata,
> >  		 * equivalent to a sign extension.
> >  		 */
> >  		if (st->recv_size == 3) {
> > -			*val = (get_unaligned_be24(st->data.d8) >> st->sub_lsb)
> > +			*val = (get_unaligned_be24(st->data.d8) >> 6)
Prior to the buggy patch.

1) Read into top 3 bytes of BE32 which corresponds to 8 bits high.
2) Shift by 14.

Before this patch
1) Read only 3 bytes
2) shift by  (31 - (resolution + 1) where resolution is 16 so shift by 14.
Indeed bug.

After this patch.
1) Read 3 bytes
2) shift by 6 

Matches original behaviour - fix is correct.


> >  				- BIT(ddata->chip_info->resolution + 1);
> >  		} else {
> > -			*val = (be32_to_cpu(st->data.d32) >> st->sub_lsb)
> > +			*val = (be32_to_cpu(st->data.d32) >> 6)

Prior to this patch
sub_lsb = 31 - (resolution + 1) where in this case resolution is 24
31 - 25 = 6 so this change is a noop change, so this is good.

> >  				- BIT(ddata->chip_info->resolution + 1);
> >  		}
> >  
> > @@ -122,7 +121,6 @@ static int ltc2497_probe(struct i2c_client *client)
> >  	st->common_ddata.chip_info = chip_info;
> >  
> >  	resolution = chip_info->resolution;
> > -	st->sub_lsb = 31 - (resolution + 1);
> >  	st->recv_size = BITS_TO_BYTES(resolution) + 1;
> >  
> >  	return ltc2497core_probe(dev, indio_dev);  
> 


  reply	other threads:[~2023-01-28 17:46 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-27 12:57 [PATCH] drivers: iio: adc: ltc2497: fix LSB shift Ian Ray
2023-01-27 17:47 ` Jonathan Cameron
2023-01-28 18:00   ` Jonathan Cameron [this message]
2023-01-30  6:53     ` EXT: " Ian Ray

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230128180032.2905cee7@jic23-huawei \
    --to=jic23@kernel.org \
    --cc=Jonathan.Cameron@Huawei.com \
    --cc=Michael.Hennerich@analog.com \
    --cc=ian.ray@ge.com \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox