Skip to contentSkip to conversation

drivers: iio: adc: ltc2497: fix LSB shift

PATCH Applied Ian Ray 3 participants 4 messages Jan 27, 2023 – Jan 30, 2023

Subsystems (2)
lore
Share & tools
lore

Ian Ray

author
#0 lore raw

PATCH 1/1 drivers: iio: adc: ltc2497: fix LSB shift

Correct the "sub_lsb" shift for both ltc2497 and ltc2499.

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")

---
 drivers/iio/adc/ltc2497.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/iio/adc/ltc2497.c +2 -4
index 17370c5..ec198c6 100644
@@ -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)
 				- 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)
 				- 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);
Signature · 1 line
2.10.1
#1 lore raw
Quoted 1 line · On Fri, 27 Jan 2023 14:57:14 +0200 Ian Ray <[email protected]> wrote:

Correct the "sub_lsb" shift for both ltc2497 and ltc2499.

Hi Iain, Thanks for the patch.

Quoted 7 lines

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.

Quoted 38 lines
---
 drivers/iio/adc/ltc2497.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/iio/adc/ltc2497.c +2 -4
index 17370c5..ec198c6 100644
@@ -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)
 				- 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)
 				- 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);
maintainer Applied
#2 lore raw
Quoted 4 lines · On Fri, 27 Jan 2023 17:47:56 +0000 Jonathan Cameron…
Quoted 1 line · On Fri, 27 Jan 2023 14:57:14 +0200 Ian Ray <[email protected]> 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)

Quoted 9 lines

Hi Iain, Thanks for the patch.

Quoted 6 lines

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.

Quoted 6 lines
Quoted 1 line

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.

Quoted 1 line

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

Quoted 14 lines
Quoted 14 lines
---
 drivers/iio/adc/ltc2497.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/iio/adc/ltc2497.c +0 -1
index 17370c5..ec198c6 100644
@@ -28,7 +28,6 @@ struct ltc2497_driverdata {
 	struct ltc2497core_driverdata common_ddata;
 	struct i2c_client *client;
 	u32 recv_size;
-	u32 sub_lsb;
 	/*
Quoted 8 lines

drivers/iio/adc/ltc2497.c static int ltc2497_result_and_measure(struct ltc2497core_driverdata *ddata, · line 67

Quoted 8 lines
drivers/iio/adc/ltc2497.c +1 -1
 	 * 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.

Quoted 4 lines

drivers/iio/adc/ltc2497.c static int ltc2497_result_and_measure(struct ltc2497core_driverdata *ddata, · line 70

Quoted 4 lines
drivers/iio/adc/ltc2497.c +1 -1
 				- 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.

Quoted 12 lines
Quoted 11 lines
drivers/iio/adc/ltc2497.c +0 -1
 				- 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);  
author
#3 lore raw

Re: EXT: Re: [PATCH] drivers: iio: adc: ltc2497: fix LSB shift

Quoted 11 lines · Jonathan Cameron
Quoted 4 lines · On Fri, 27 Jan 2023 17:47:56 +0000 Jonathan Cameron…
Quoted 1 line · On Fri, 27 Jan 2023 14:57:14 +0200 Ian Ray <[email protected]> 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)

Makes sense.

Quoted 20 lines
Quoted 10 lines

Hi Iain, Thanks for the patch.

Quoted 6 lines

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.

Quoted 6 lines
Quoted 1 line

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.

Quoted 1 line

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

Quoted 6 lines

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.

lgtm

Many thanks!

Quoted 68 lines

Thanks,

Jonathan

Quoted 14 lines
Quoted 14 lines
---
 drivers/iio/adc/ltc2497.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/iio/adc/ltc2497.c +0 -1
index 17370c5..ec198c6 100644
@@ -28,7 +28,6 @@ struct ltc2497_driverdata {
 	struct ltc2497core_driverdata common_ddata;
 	struct i2c_client *client;
 	u32 recv_size;
-	u32 sub_lsb;
 	/*
Quoted 8 lines

drivers/iio/adc/ltc2497.c static int ltc2497_result_and_measure(struct ltc2497core_driverdata *ddata, · line 67

Quoted 8 lines
drivers/iio/adc/ltc2497.c +1 -1
 	 * 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.

Quoted 4 lines

drivers/iio/adc/ltc2497.c static int ltc2497_result_and_measure(struct ltc2497core_driverdata *ddata, · line 70

Quoted 4 lines
drivers/iio/adc/ltc2497.c +1 -1
 				- 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.

Quoted 12 lines
Quoted 11 lines
drivers/iio/adc/ltc2497.c +0 -1
 				- 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);