Skip to contentSkip to conversation

iio: adc: ti-ads8344: improve the driver

PATCH Superseded Alexandre Belloni 6 participants 12 messages Apr 15, 2020 – Apr 21, 2020 IIO

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover iio: adc: ti-ads8344: improve the driver

Hello,

This series improves the ads8344 driver.

The first patch is fix and can be backported.

The second patch removes the dubious ____cacheline_aligned

The last one is improving power consumption by shutting down the ADC while it is not used.

Alexandre Belloni (3):
iio: adc: ti-ads8344: properly byte swap value
iio: adc: ti-ads8344: remove tx_buf from driver data
iio: adc: ti-ads8344: optimize consumption

 drivers/iio/adc/ti-ads8344.c | 33 ++++++++++++++++-----------------
 1 file changed, 16 insertions(+), 17 deletions(-)
Signature · 1 line
2.25.2
#1 lore raw

PATCH 1/3 iio: adc: ti-ads8344: properly byte swap value

The first received byte is the MSB, followed by the LSB so the value needs to be byte swapped.

Also, the ADC actually has a delay of one clock on the SPI bus. Read three bytes to get the last bit.

Fixes 8dd2d7c0fed7 ("iio: adc: Add driver for the TI ADS8344 A/DC chips")
---
 drivers/iio/adc/ti-ads8344.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/iio/adc/ti-ads8344.c +3 -3
index 9a460807d46d..6da50ea35217 100644
@@ -29,7 +29,6 @@ struct ads8344 {
 	struct mutex lock;
 
 	u8 tx_buf ____cacheline_aligned;
-	u16 rx_buf;
 };
 
 #define ADS8344_VOLTAGE_CHANNEL(chan, si)				\
@@ -76,6 +75,7 @@ static int ads8344_adc_conversion(struct ads8344 *adc, int channel,
 {
 	struct spi_device *spi = adc->spi;
 	int ret;
+	u8 buf[3];
 
 	adc->tx_buf = ADS8344_START;
 	if (!differential)
@@ -89,11 +89,11 @@ static int ads8344_adc_conversion(struct ads8344 *adc, int channel,
 
 	udelay(9);
 
-	ret = spi_read(spi, &adc->rx_buf, 2);
+	ret = spi_read(spi, buf, sizeof(buf));
 	if (ret)
 		return ret;
 
-	return adc->rx_buf;
+	return buf[0] << 9 | buf[1] << 1 | buf[2] >> 7;
 }
 
 static int ads8344_read_raw(struct iio_dev *iio,
Signature · 1 line
2.25.2
#2 lore raw

Re: [PATCH 1/3] iio: adc: ti-ads8344: properly byte swap value

I love your patch! Yet something to improve:

Hi Alexandre,

I love your patch! Yet something to improve:

[auto build test ERROR on iio/togreg]
[also build test ERROR on v5.7-rc1 next-20200415]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/Alexandre-Belloni/iio-adc-ti-ads8344-improve-the-driver/20200416-073357
base:   https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git togreg
config: c6x-allyesconfig (attached as .config)
compiler: c6x-elf-gcc (GCC) 9.3.0

reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day GCC_VERSION=9.3.0 make.cross ARCH=c6x

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

vim +/302 +96 drivers/iio/adc/ti-ads8344.c

    72	
    73	static int ads8344_adc_conversion(struct ads8344 *adc, int channel,
    74					  bool differential)
    75	{
    76		struct spi_device *spi = adc->spi;
    77		int ret;
    78		u8 buf[3];
    79	
    80		adc->tx_buf = ADS8344_START;
    81		if (!differential)
    82			adc->tx_buf |= ADS8344_SINGLE_END;
    83		adc->tx_buf |= ADS8344_CHANNEL(channel);
    84		adc->tx_buf |= ADS8344_CLOCK_INTERNAL;
    85	
    86		ret = spi_write(spi, &adc->tx_buf, 1);
    87		if (ret)
    88			return ret;
    89	
    90		udelay(9);
    91	
    92		ret = spi_read(spi, buf, sizeof(buf));
    93		if (ret)
    94			return ret;
    95	
  > 96		return buf[0] << 9 | buf[1] << 1 | buf[2] >> 7;
    97	}
    98	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#3 lore raw

Re: [PATCH 1/3] iio: adc: ti-ads8344: properly byte swap value

Hi,

Quoted 14 lines · kbuild test robot

Hi Alexandre,

I love your patch! Yet something to improve:

[auto build test ERROR on iio/togreg]
[also build test ERROR on v5.7-rc1 next-20200415]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/Alexandre-Belloni/iio-adc-ti-ads8344-improve-the-driver/20200416-073357
base:   https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git togreg
config: c6x-allyesconfig (attached as .config)
compiler: c6x-elf-gcc (GCC) 9.3.0
Quoted 6 lines

reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day GCC_VERSION=9.3.0 make.cross ARCH=c6x

I spent some time to reproduce and this is actually not that trivial because your toolchains are linked with libisl22 and most distributions still ship an older version. Maybe you can do something about that?

Quoted 32 lines

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

vim +/302 +96 drivers/iio/adc/ti-ads8344.c

    72	
    73	static int ads8344_adc_conversion(struct ads8344 *adc, int channel,
    74					  bool differential)
    75	{
    76		struct spi_device *spi = adc->spi;
    77		int ret;
    78		u8 buf[3];
    79	
    80		adc->tx_buf = ADS8344_START;
    81		if (!differential)
    82			adc->tx_buf |= ADS8344_SINGLE_END;
    83		adc->tx_buf |= ADS8344_CHANNEL(channel);
    84		adc->tx_buf |= ADS8344_CLOCK_INTERNAL;
    85	
    86		ret = spi_write(spi, &adc->tx_buf, 1);
    87		if (ret)
    88			return ret;
    89	
    90		udelay(9);
    91	
    92		ret = spi_read(spi, buf, sizeof(buf));
    93		if (ret)
    94			return ret;
    95	
Quoted 4 lines
Quoted 1 line

96 return buf[0] << 9 | buf[1] << 1 | buf[2] >> 7;

    97	}
    98	

I take it this is a false positive as I don't get any errors when building this driver with the provided toolchain. However, I see a few "internal compiler error: in priority, at haifa-sched.c:1599"

Signature · 3 lines
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#4 lore raw

Re: [PATCH 1/3] iio: adc: ti-ads8344: properly byte swap value

Quoted 24 lines · Alexandre Belloni

Hi,

Quoted 14 lines · kbuild test robot

Hi Alexandre,

I love your patch! Yet something to improve:

[auto build test ERROR on iio/togreg]
[also build test ERROR on v5.7-rc1 next-20200415]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/Alexandre-Belloni/iio-adc-ti-ads8344-improve-the-driver/20200416-073357
base:   https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git togreg
config: c6x-allyesconfig (attached as .config)
compiler: c6x-elf-gcc (GCC) 9.3.0
Quoted 6 lines

reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day GCC_VERSION=9.3.0 make.cross ARCH=c6x

Quoted 3 lines

I spent some time to reproduce and this is actually not that trivial because your toolchains are linked with libisl22 and most distributions still ship an older version. Maybe you can do something about that?

Thanks for the feedback, we will resolve this to use old version in earliest time.

Quoted 47 lines
Quoted 33 lines

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

vim +/302 +96 drivers/iio/adc/ti-ads8344.c

    72	
    73	static int ads8344_adc_conversion(struct ads8344 *adc, int channel,
    74					  bool differential)
    75	{
    76		struct spi_device *spi = adc->spi;
    77		int ret;
    78		u8 buf[3];
    79	
    80		adc->tx_buf = ADS8344_START;
    81		if (!differential)
    82			adc->tx_buf |= ADS8344_SINGLE_END;
    83		adc->tx_buf |= ADS8344_CHANNEL(channel);
    84		adc->tx_buf |= ADS8344_CLOCK_INTERNAL;
    85	
    86		ret = spi_write(spi, &adc->tx_buf, 1);
    87		if (ret)
    88			return ret;
    89	
    90		udelay(9);
    91	
    92		ret = spi_read(spi, buf, sizeof(buf));
    93		if (ret)
    94			return ret;
    95	
Quoted 4 lines
Quoted 1 line

96 return buf[0] << 9 | buf[1] << 1 | buf[2] >> 7;

    97	}
    98	

I take it this is a false positive as I don't get any errors when building this driver with the provided toolchain. However, I see a few "internal compiler error: in priority, at haifa-sched.c:1599"

Signature · 3 lines
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#5 lore raw

RE: [PATCH 1/3] iio: adc: ti-ads8344: properly byte swap value

Quoted 41 lines

-----Original Message-----
From: Li, Philip <[email protected]>
Sent: 2020年4月19日 10:50
To: Alexandre Belloni <[email protected]>
Cc: lkp <[email protected]>; Jonathan Cameron <[email protected]>; kbuild- all(a)lists.01.org; Hartmut Knaack <[email protected]>; Lars-Peter Clausen <[email protected]>; Peter Meerwald-Stadler <[email protected]>; Gregory CLEMENT <[email protected]>; linux-iio(a)vger.kernel.org; linux- kernel(a)vger.kernel.org
Subject: Re: [PATCH 1/3] iio: adc: ti-ads8344: properly byte swap value

Quoted 14 lines · Alexandre Belloni

Hi,

Quoted 11 lines · kbuild test robot

Hi Alexandre,

I love your patch! Yet something to improve:

[auto build test ERROR on iio/togreg] [also build test ERROR on v5.7-rc1 next-20200415] [if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url: https://github.com/0day-ci/linux/commits/Alexandre-Belloni/iio-adc-ti-

ads8344-improve-the-driver/20200416-073357

Quoted 5 lines
Quoted 5 lines

base: https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git togreg
config: c6x-allyesconfig (attached as .config)
compiler: c6x-elf-gcc (GCC) 9.3.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-

tests/master/sbin/make.cross -O ~/bin/make.cross

Quoted 6 lines
Quoted 5 lines

chmod +x ~/bin/make.cross
# save the attached .config to linux build tree COMPILER_INSTALL_PATH=$HOME/0day GCC_VERSION=9.3.0
make.cross ARCH=c6x

Quoted 4 lines
Quoted 3 lines

I spent some time to reproduce and this is actually not that trivial because your toolchains are linked with libisl22 and most distributions still ship an older version. Maybe you can do something about that?

Thanks for the feedback, we will resolve this to use old version in earliest time.

The cross toolchains has been updated to link with libisl19 the stable version. Please remove the old toolchain and make.cross again. Feel free to let us know if still have problem. Thanks.

Quoted 49 lines
Quoted 5 lines
Quoted 5 lines

If you fix the issue, kindly add following tag as appropriate

Quoted 7 lines
Quoted 7 lines

All errors (new ones prefixed by >>):

vim +/302 +96 drivers/iio/adc/ti-ads8344.c

    72
    73	static int ads8344_adc_conversion(struct ads8344 *adc, int

channel,

Quoted 36 lines
Quoted 22 lines
    74					  bool differential)
    75	{
    76		struct spi_device *spi = adc->spi;
    77		int ret;
    78		u8 buf[3];
    79
    80		adc->tx_buf = ADS8344_START;
    81		if (!differential)
    82			adc->tx_buf |= ADS8344_SINGLE_END;
    83		adc->tx_buf |= ADS8344_CHANNEL(channel);
    84		adc->tx_buf |= ADS8344_CLOCK_INTERNAL;
    85
    86		ret = spi_write(spi, &adc->tx_buf, 1);
    87		if (ret)
    88			return ret;
    89
    90		udelay(9);
    91
    92		ret = spi_read(spi, buf, sizeof(buf));
    93		if (ret)
    94			return ret;
    95
Quoted 4 lines
Quoted 1 line

96 return buf[0] << 9 | buf[1] << 1 | buf[2] >> 7;

    97	}
    98

I take it this is a false positive as I don't get any errors when building this driver with the provided toolchain. However, I see a few "internal compiler error: in priority, at haifa-sched.c:1599"

--
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com

#6 lore raw

Re: [PATCH 1/3] iio: adc: ti-ads8344: properly byte swap value

Hi,

Quoted 39 lines · Hui
Quoted 39 lines

-----Original Message-----
From: Li, Philip <[email protected]>
Sent: 2020年4月19日 10:50
To: Alexandre Belloni <[email protected]>
Cc: lkp <[email protected]>; Jonathan Cameron <[email protected]>; kbuild- [email protected]; Hartmut Knaack <[email protected]>; Lars-Peter Clausen <[email protected]>; Peter Meerwald-Stadler <[email protected]>; Gregory CLEMENT <[email protected]>; [email protected]; linux- [email protected]
Subject: Re: [PATCH 1/3] iio: adc: ti-ads8344: properly byte swap value

Quoted 14 lines · Alexandre Belloni

Hi,

Quoted 11 lines · kbuild test robot

Hi Alexandre,

I love your patch! Yet something to improve:

[auto build test ERROR on iio/togreg] [also build test ERROR on v5.7-rc1 next-20200415] [if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url: https://github.com/0day-ci/linux/commits/Alexandre-Belloni/iio-adc-ti-

ads8344-improve-the-driver/20200416-073357

Quoted 5 lines
Quoted 5 lines

base: https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git togreg
config: c6x-allyesconfig (attached as .config)
compiler: c6x-elf-gcc (GCC) 9.3.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-

tests/master/sbin/make.cross -O ~/bin/make.cross

Quoted 6 lines
Quoted 5 lines

chmod +x ~/bin/make.cross
# save the attached .config to linux build tree COMPILER_INSTALL_PATH=$HOME/0day GCC_VERSION=9.3.0
make.cross ARCH=c6x

Quoted 7 lines
Quoted 4 lines
Quoted 3 lines

I spent some time to reproduce and this is actually not that trivial because your toolchains are linked with libisl22 and most distributions still ship an older version. Maybe you can do something about that?

Thanks for the feedback, we will resolve this to use old version in earliest time.

The cross toolchains has been updated to link with libisl19 the stable version. Please remove the old toolchain and make.cross again. Feel free to let us know if still have problem. Thanks.

Thank you, this works properly on more machines now.

Signature · 3 lines
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#7 lore raw

Re: [PATCH 1/3] iio: adc: ti-ads8344: properly byte swap value

In reply to Alexandre Belloni
Quoted 25 lines · Alexandre Belloni

The first received byte is the MSB, followed by the LSB so the value needs to be byte swapped.

Also, the ADC actually has a delay of one clock on the SPI bus. Read three bytes to get the last bit.

Fixes 8dd2d7c0fed7 ("iio: adc: Add driver for the TI ADS8344 A/DC chips")
---
  drivers/iio/adc/ti-ads8344.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
drivers/iio/adc/ti-ads8344.c +0 -1
index 9a460807d46d..6da50ea35217 100644
@@ -29,7 +29,6 @@ struct ads8344 {
  	struct mutex lock;
 
  	u8 tx_buf ____cacheline_aligned;
-	u16 rx_buf;
  };
 
  #define ADS8344_VOLTAGE_CHANNEL(chan, si)				\

drivers/iio/adc/ti-ads8344.c static int ads8344_adc_conversion(struct ads8344 *adc, int channel, · line 78

Quoted 4 lines
drivers/iio/adc/ti-ads8344.c +1 -0
@@ -76,6 +75,7 @@ static int ads8344_adc_conversion(struct ads8344 *adc, int channel,
  {
  	struct spi_device *spi = adc->spi;
  	int ret;
+	u8 buf[3];

Same as with spi_write(), spi_read() transfer buffers should not be on the stack in case the driver tries to use it for DMA.

Quoted 17 lines
drivers/iio/adc/ti-ads8344.c +2 -2
  	adc->tx_buf = ADS8344_START;
  	if (!differential)
@@ -89,11 +89,11 @@ static int ads8344_adc_conversion(struct ads8344 *adc, int channel,
 
  	udelay(9);
 
-	ret = spi_read(spi, &adc->rx_buf, 2);
+	ret = spi_read(spi, buf, sizeof(buf));
  	if (ret)
  		return ret;
 
-	return adc->rx_buf;
+	return buf[0] << 9 | buf[1] << 1 | buf[2] >> 7;
  }
 
  static int ads8344_read_raw(struct iio_dev *iio,
#8 lore raw

PATCH 2/3 iio: adc: ti-ads8344: remove tx_buf from driver data

In reply to Alexandre Belloni

There is no need to keep tx_buf around, it is only used for the conversion.

---
 drivers/iio/adc/ti-ads8344.c | 18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)
drivers/iio/adc/ti-ads8344.c +6 -12
index 6da50ea35217..9b2d3a8ea6bd 100644
@@ -22,13 +22,7 @@
 struct ads8344 {
 	struct spi_device *spi;
 	struct regulator *reg;
-	/*
-	 * Lock protecting access to adc->tx_buff and rx_buff,
-	 * especially from concurrent read on sysfs file.
-	 */
-	struct mutex lock;
-
-	u8 tx_buf ____cacheline_aligned;
+	struct mutex lock; /* protect from concurrent conversions */
 };
 
 #define ADS8344_VOLTAGE_CHANNEL(chan, si)				\
@@ -77,13 +71,13 @@ static int ads8344_adc_conversion(struct ads8344 *adc, int channel,
 	int ret;
 	u8 buf[3];
 
-	adc->tx_buf = ADS8344_START;
+	buf[0] = ADS8344_START;
 	if (!differential)
-		adc->tx_buf |= ADS8344_SINGLE_END;
-	adc->tx_buf |= ADS8344_CHANNEL(channel);
-	adc->tx_buf |= ADS8344_CLOCK_INTERNAL;
+		buf[0] |= ADS8344_SINGLE_END;
+	buf[0] |= ADS8344_CHANNEL(channel);
+	buf[0] |= ADS8344_CLOCK_INTERNAL;
 
-	ret = spi_write(spi, &adc->tx_buf, 1);
+	ret = spi_write(spi, buf, 1);
 	if (ret)
 		return ret;
 
Signature · 1 line
2.25.2
#9 lore raw

Re: [PATCH 2/3] iio: adc: ti-ads8344: remove tx_buf from driver data

Quoted 27 lines · Alexandre Belloni

There is no need to keep tx_buf around, it is only used for the conversion.

---
  drivers/iio/adc/ti-ads8344.c | 18 ++++++------------
  1 file changed, 6 insertions(+), 12 deletions(-)
drivers/iio/adc/ti-ads8344.c +1 -7
index 6da50ea35217..9b2d3a8ea6bd 100644
@@ -22,13 +22,7 @@
  struct ads8344 {
  	struct spi_device *spi;
  	struct regulator *reg;
-	/*
-	 * Lock protecting access to adc->tx_buff and rx_buff,
-	 * especially from concurrent read on sysfs file.
-	 */
-	struct mutex lock;
-
-	u8 tx_buf ____cacheline_aligned;
+	struct mutex lock; /* protect from concurrent conversions */
  };
 
  #define ADS8344_VOLTAGE_CHANNEL(chan, si)				\

drivers/iio/adc/ti-ads8344.c static int ads8344_adc_conversion(struct ads8344 *adc, int channel, · line 72

Quoted 2 lines
drivers/iio/adc/ti-ads8344.c +0 -0
@@ -77,13 +71,13 @@ static int ads8344_adc_conversion(struct ads8344 *adc, int channel,
  	int ret;
  	u8 buf[3];

spi_write() might use the buffer in a DMA transfer, so it can't be on the stack and needs to be in its own cacheline.

Quoted 16 lines
drivers/iio/adc/ti-ads8344.c +5 -5
-	adc->tx_buf = ADS8344_START;
+	buf[0] = ADS8344_START;
  	if (!differential)
-		adc->tx_buf |= ADS8344_SINGLE_END;
-	adc->tx_buf |= ADS8344_CHANNEL(channel);
-	adc->tx_buf |= ADS8344_CLOCK_INTERNAL;
+		buf[0] |= ADS8344_SINGLE_END;
+	buf[0] |= ADS8344_CHANNEL(channel);
+	buf[0] |= ADS8344_CLOCK_INTERNAL;
 
-	ret = spi_write(spi, &adc->tx_buf, 1);
+	ret = spi_write(spi, buf, 1);
  	if (ret)
  		return ret;
 
#10 lore raw

Re: [PATCH 2/3] iio: adc: ti-ads8344: remove tx_buf from driver data

In reply to Alexandre Belloni
Quoted 3 lines · Alexandre Belloni

There is no need to keep tx_buf around, it is only used for the conversion.

As Lars said. And some SPI controllers may want to DMA even one byte, so, NAK.

Quoted 46 lines
---
 drivers/iio/adc/ti-ads8344.c | 18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)
drivers/iio/adc/ti-ads8344.c +6 -13
index 6da50ea35217..9b2d3a8ea6bd 100644
@@ -22,13 +22,7 @@
 struct ads8344 {
        struct spi_device *spi;
        struct regulator *reg;
-       /*
-        * Lock protecting access to adc->tx_buff and rx_buff,
-        * especially from concurrent read on sysfs file.
-        */
-       struct mutex lock;
-
-       u8 tx_buf ____cacheline_aligned;
+       struct mutex lock; /* protect from concurrent conversions */
 };
 
 #define ADS8344_VOLTAGE_CHANNEL(chan, si)                              \
@@ -77,13 +71,13 @@ static int ads8344_adc_conversion(struct ads8344 *adc, int channel,
        int ret;
        u8 buf[3];
 
-       adc->tx_buf = ADS8344_START;
+       buf[0] = ADS8344_START;
        if (!differential)
-               adc->tx_buf |= ADS8344_SINGLE_END;
-       adc->tx_buf |= ADS8344_CHANNEL(channel);
-       adc->tx_buf |= ADS8344_CLOCK_INTERNAL;
+               buf[0] |= ADS8344_SINGLE_END;
+       buf[0] |= ADS8344_CHANNEL(channel);
+       buf[0] |= ADS8344_CLOCK_INTERNAL;
 
-       ret = spi_write(spi, &adc->tx_buf, 1);
+       ret = spi_write(spi, buf, 1);
        if (ret)
                return ret;
 
--

2.25.2

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#11 lore raw

PATCH 3/3 iio: adc: ti-ads8344: optimize consumption

In reply to Alexandre Belloni

Set the clock mode only once, at probe time and then keep the ADC powered down between conversions.

---
 drivers/iio/adc/ti-ads8344.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/iio/adc/ti-ads8344.c +8 -3
index 9b2d3a8ea6bd..0804650d8d8a 100644
@@ -65,7 +65,7 @@ static const struct iio_chan_spec ads8344_channels[] = {
 };
 
 static int ads8344_adc_conversion(struct ads8344 *adc, int channel,
-				  bool differential)
+				  bool differential, u8 clock)
 {
 	struct spi_device *spi = adc->spi;
 	int ret;
@@ -75,7 +75,7 @@ static int ads8344_adc_conversion(struct ads8344 *adc, int channel,
 	if (!differential)
 		buf[0] |= ADS8344_SINGLE_END;
 	buf[0] |= ADS8344_CHANNEL(channel);
-	buf[0] |= ADS8344_CLOCK_INTERNAL;
+	buf[0] |= clock;
 
 	ret = spi_write(spi, buf, 1);
 	if (ret)
@@ -100,7 +100,7 @@ static int ads8344_read_raw(struct iio_dev *iio,
 	case IIO_CHAN_INFO_RAW:
 		mutex_lock(&adc->lock);
 		*value = ads8344_adc_conversion(adc, channel->scan_index,
-						channel->differential);
+						channel->differential, 0);
 		mutex_unlock(&adc->lock);
 		if (*value < 0)
 			return *value;
@@ -155,6 +155,11 @@ static int ads8344_probe(struct spi_device *spi)
 	if (ret)
 		return ret;
 
+	/* Do a dummy read and set external clock mode */
+	ret = ads8344_adc_conversion(adc, 0, 0, ADS8344_CLOCK_INTERNAL);
+	if (ret < 0)
+		return ret;
+
 	spi_set_drvdata(spi, indio_dev);
 
 	ret = iio_device_register(indio_dev);
Signature · 1 line
2.25.2