Skip to contentSkip to conversation

iio: adc: ti-adc128s052: Use get_unaligned_beXX()

PATCH In review Andy Shevchenko 2 participants 4 messages Dec 14, 2022 – Dec 28, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 iio: adc: ti-adc128s052: Use get_unaligned_beXX()

This makes the driver code slightly easier to read. While at it, use GENMASK() as well.

---
 drivers/iio/adc/ti-adc128s052.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/iio/adc/ti-adc128s052.c +6 -1
index fc09ee6bb174..7c4e8025861c 100644
@@ -9,6 +9,7 @@
  * https://www.ti.com/lit/ds/symlink/adc124s021.pdf
  */
 
+#include <linux/bits.h>
 #include <linux/err.h>
 #include <linux/spi/spi.h>
 #include <linux/module.h>
@@ -17,6 +18,8 @@
 #include <linux/property.h>
 #include <linux/regulator/consumer.h>
 
+#include <asm/unaligned.h>
+
 struct adc128_configuration {
 	const struct iio_chan_spec	*channels;
 	u8				num_channels;
@@ -33,6 +36,7 @@ struct adc128 {
 
 static int adc128_adc_conversion(struct adc128 *adc, u8 channel)
 {
+	u16 value;
 	int ret;
 
 	mutex_lock(&adc->lock);
@@ -53,7 +57,8 @@ static int adc128_adc_conversion(struct adc128 *adc, u8 channel)
 	if (ret < 0)
 		return ret;
 
-	return ((adc->buffer[0] << 8 | adc->buffer[1]) & 0xFFF);
+	value = get_unaligned_be16(&adc->buffer);
+	return value & GENMASK(11, 0);
 }
 
 static int adc128_read_raw(struct iio_dev *indio_dev,
Signature · 1 line
2.35.1
#1 lore raw

PATCH 2/2 iio: adc: ti-adc128s052: Sort headers

Sort the headers in alphabetic order in order to ease the maintenance for this part.

---
 drivers/iio/adc/ti-adc128s052.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/iio/adc/ti-adc128s052.c +3 -3
index 7c4e8025861c..a003264ca835 100644
@@ -11,12 +11,12 @@
 
 #include <linux/bits.h>
 #include <linux/err.h>
-#include <linux/spi/spi.h>
-#include <linux/module.h>
-#include <linux/mod_devicetable.h>
 #include <linux/iio/iio.h>
+#include <linux/mod_devicetable.h>
+#include <linux/module.h>
 #include <linux/property.h>
 #include <linux/regulator/consumer.h>
+#include <linux/spi/spi.h>
 
 #include <asm/unaligned.h>
 
Signature · 1 line
2.35.1
maintainer
#2 lore raw
In reply to Andy Shevchenko
Quoted 4 lines · On Wed, 14 Dec 2022 14:02:01 +0200 Andy Shevchenko…

This makes the driver code slightly easier to read. While at it, use GENMASK() as well.

Hi Andy,

Patch looks good, but it got me thinking. Why not just flip this driver over to using spi_write_then_read(). Would let us drop the DMA safety protections and simplify the code + use a __be16 as the read buffer and this would then be the slightly nicer aligned form.

Worth doing whilst you are here?

Jonathan

Quoted 43 lines
---
 drivers/iio/adc/ti-adc128s052.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/iio/adc/ti-adc128s052.c +6 -1
index fc09ee6bb174..7c4e8025861c 100644
@@ -9,6 +9,7 @@
  * https://www.ti.com/lit/ds/symlink/adc124s021.pdf
  */
 
+#include <linux/bits.h>
 #include <linux/err.h>
 #include <linux/spi/spi.h>
 #include <linux/module.h>
@@ -17,6 +18,8 @@
 #include <linux/property.h>
 #include <linux/regulator/consumer.h>
 
+#include <asm/unaligned.h>
+
 struct adc128_configuration {
 	const struct iio_chan_spec	*channels;
 	u8				num_channels;
@@ -33,6 +36,7 @@ struct adc128 {
 
 static int adc128_adc_conversion(struct adc128 *adc, u8 channel)
 {
+	u16 value;
 	int ret;
 
 	mutex_lock(&adc->lock);
@@ -53,7 +57,8 @@ static int adc128_adc_conversion(struct adc128 *adc, u8 channel)
 	if (ret < 0)
 		return ret;
 
-	return ((adc->buffer[0] << 8 | adc->buffer[1]) & 0xFFF);
+	value = get_unaligned_be16(&adc->buffer);
+	return value & GENMASK(11, 0);
 }
 
 static int adc128_read_raw(struct iio_dev *indio_dev,
#3 lore raw
Quoted 7 lines · Jonathan Cameron
Quoted 4 lines · On Wed, 14 Dec 2022 14:02:01 +0200 Andy Shevchenko…

This makes the driver code slightly easier to read. While at it, use GENMASK() as well.

Quoted 8 lines

Hi Andy,

Patch looks good, but it got me thinking. Why not just flip this driver over to using spi_write_then_read(). Would let us drop the DMA safety protections and simplify the code + use a __be16 as the read buffer and this would then be the slightly nicer aligned form.

Worth doing whilst you are here?

OK, I will consider when I have time for this.

Signature · 2 lines
With Best Regards,
Andy Shevchenko