iio: adc: ti-ads8344: driver improvements
v2 cover iio: adc: ti-ads8344: driver improvements
Hello,
This series improves the ads8344 driver.
The first patch is a fix and can be backported.
The last one is improving power consumption by shutting down the ADC while it is not used.
Changes in v2:
- keep the SPI buffer in the driver data structure as suggested by Lars.
Alexandre Belloni (2):
iio: adc: ti-ads8344: properly byte swap value
iio: adc: ti-ads8344: optimize consumption
drivers/iio/adc/ti-ads8344.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-)
Signature · 1 line
2.25.2
PATCH v2 1/2 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.
--- drivers/iio/adc/ti-ads8344.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/iio/adc/ti-ads8344.c +3 -3
@@ -29,7 +29,7 @@ struct ads8344 {struct mutex lock;u8 tx_buf____cacheline_aligned ; -u16 rx_buf; +u8 rx_buf[3 ]; };#define ADS8344_VOLTAGE_CHANNEL (chan, si) \
@@ -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, adc->rx_buf,sizeof (adc->rx_buf));if (ret)return ret; -return adc->rx_buf; +return adc->rx_buf[0 ] <<9 | adc->rx_buf[1 ] <<1 | adc->rx_buf[2 ] >>7 ; } static int ads8344_read_raw(struct iio_dev *iio,
Signature · 1 line
2.25.2
Re: [PATCH v2 1/2] iio: adc: ti-ads8344: properly byte swap value
Quoted 7 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.
Can you show example of what is read and what is expected to be a correct value? Because it seems I have been reported with similar issue on other TI ADC chip [1]. Perhaps we have to fix all of them?
[1]: https://github.com/edison-fw/meta-intel-edison/issues/108
Quoted 36 lines
Fixes 8dd2d7c0fed7 ("iio: adc: Add driver for the TI ADS8344 A/DC chips")Signed-off-by Alexandre Belloni <[email protected]>--- drivers/iio/adc/ti-ads8344.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)drivers/iio/adc/ti-ads8344.c +3
-4@@ -29,7 +29,7 @@ struct ads8344 {struct mutex lock;u8 tx_buf____cacheline_aligned ; -u16 rx_buf; +u8 rx_buf[3 ]; };#define ADS8344_VOLTAGE_CHANNEL (chan, si) \@@ -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, adc->rx_buf,sizeof (adc->rx_buf));if (ret)return ret; -return adc->rx_buf; +return adc->rx_buf[0 ] <<9 | adc->rx_buf[1 ] <<1 | adc->rx_buf[2 ] >>7 ; } static int ads8344_read_raw(struct iio_dev *iio, --2.25.2
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 1/2] iio: adc: ti-ads8344: properly byte swap value
Quoted 11 lines · Andy Shevchenko
Quoted 7 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.
Quoted 5 lines
Can you show example of what is read and what is expected to be a correct value? Because it seems I have been reported with similar issue on other TI ADC chip [1]. Perhaps we have to fix all of them?
[1]: https://github.com/edison-fw/meta-intel-edison/issues/108
Also, forgot to mention that TI ADC are 16 bit word, so, we need to read two u16 rather then bytes.
Some configuration won't allow to do byte reads.
Quoted 2 lines
Quoted 2 lines
Fixes 8dd2d7c0fed7 ("iio: adc: Add driver for the TI ADS8344 A/DC chips")Signed-off-by Alexandre Belloni <[email protected]>
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 1/2] iio: adc: ti-ads8344: properly byte swap value
Quoted 17 lines · Andy Shevchenko
Quoted 15 lines · Andy Shevchenko
Quoted 7 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.
Can you show example of what is read and what is expected to be a correct value? Because it seems I have been reported with similar issue on other TI ADC chip [1]. Perhaps we have to fix all of them?
Quoted 7 lines
Quoted 1 line
[1]: https://github.com/edison-fw/meta-intel-edison/issues/108
Also, forgot to mention that TI ADC are 16 bit word, so, we need to read two u16 rather then bytes.
Some configuration won't allow to do byte reads.
Both ADC families are not related, I don't think this is your issue. The ADS8344 was from Burr-Brown.
Signature · 3 lines
Alexandre Belloni, Bootlin Embedded Linux and Kernel engineering https://bootlin.com
Re: [PATCH v2 1/2] iio: adc: ti-ads8344: properly byte swap value
Quoted 8 lines · Alexandre Belloni
Quoted 4 lines · Andy Shevchenko
Quoted 2 lines · Andy Shevchenko
On Thu, Apr 16, 2020 at 11:55 PM Alexandre Belloni <[email protected]> wrote:
Both ADC families are not related, I don't think this is your issue. The ADS8344 was from Burr-Brown.
Thanks for clarification.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 1/2] iio: adc: ti-ads8344: properly byte swap value
In reply to Alexandre BelloniQuoted 8 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.
+ return adc->rx_buf[0] << 9 | adc->rx_buf[1] << 1 | adc->rx_buf[2] >> 7;
I just realize, isn't it an open coded variant of ror() / rol()?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 1/2] iio: adc: ti-ads8344: properly byte swap value
Quoted 6 lines · Andy Shevchenko
Quoted 4 lines · Alexandre Belloni
The first received byte is the MSB, followed by the LSB so the value needs to be byte swapped.
Quoted 7 lines
Quoted 4 lines
Also, the ADC actually has a delay of one clock on the SPI bus. Read three bytes to get the last bit.
+ return adc->rx_buf[0] << 9 | adc->rx_buf[1] << 1 | adc->rx_buf[2] >> 7;
I just realize, isn't it an open coded variant of
ror()/rol()?
No, this byteswaps and rotates.
Signature · 3 lines
Alexandre Belloni, Bootlin Embedded Linux and Kernel engineering https://bootlin.com
Re: [PATCH v2 1/2] iio: adc: ti-ads8344: properly byte swap value
In reply to Alexandre BelloniQuoted 8 lines · On Thu, 16 Apr 2020 22:54:27 +0200 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")Signed-off-by Alexandre Belloni <[email protected]>
Applied to the fixes-togreg branch of iio.git. Marked for stable.
Random aside that I'll probably forget to clean up.
Driver includes iio/buffer.h and doesn't use it...
Thanks,
Jonathan
Quoted 31 lines
--- drivers/iio/adc/ti-ads8344.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)drivers/iio/adc/ti-ads8344.c +3
-3@@ -29,7 +29,7 @@ struct ads8344 {struct mutex lock;u8 tx_buf____cacheline_aligned ; -u16 rx_buf; +u8 rx_buf[3 ]; };#define ADS8344_VOLTAGE_CHANNEL (chan, si) \@@ -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, adc->rx_buf,sizeof (adc->rx_buf));if (ret)return ret; -return adc->rx_buf; +return adc->rx_buf[0 ] <<9 | adc->rx_buf[1 ] <<1 | adc->rx_buf[2 ] >>7 ; } static int ads8344_read_raw(struct iio_dev *iio,
PATCH v2 2/2 iio: adc: ti-ads8344: optimize consumption
In reply to Alexandre BelloniSet 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
@@ -72,7 +72,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;
@@ -81,7 +81,7 @@ static int ads8344_adc_conversion(struct ads8344 *adc, int channel,if (!differential) adc->tx_buf |=ADS8344_SINGLE_END ; adc->tx_buf |=ADS8344_CHANNEL (channel); - adc->tx_buf |=ADS8344_CLOCK_INTERNAL ; + adc->tx_buf |= clock; ret =spi_write (spi, &adc->tx_buf,1 );if (ret)
@@ -106,7 +106,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;
@@ -161,6 +161,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
Jonathan Cameron
maintainerRe: [PATCH v2 2/2] iio: adc: ti-ads8344: optimize consumption
Quoted 4 lines · On Thu, 16 Apr 2020 22:54:28 +0200 Alexandre Belloni…
Set the clock mode only once, at probe time and then keep the ADC powered down between conversions.
Signed-off-by Alexandre Belloni <[email protected]>
Looks fine to me. I'd like to be lazy and not take this until the fix is in my upstream (even though I suspect the merge would be fine).
Give me a poke if I seem to have forgotten this after that is true!
Thanks,
Jonathan
Quoted 47 lines
--- drivers/iio/adc/ti-ads8344.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)drivers/iio/adc/ti-ads8344.c +8
-3@@ -72,7 +72,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;@@ -81,7 +81,7 @@ static int ads8344_adc_conversion(struct ads8344 *adc, int channel,if (!differential) adc->tx_buf |=ADS8344_SINGLE_END ; adc->tx_buf |=ADS8344_CHANNEL (channel); - adc->tx_buf |=ADS8344_CLOCK_INTERNAL ; + adc->tx_buf |= clock; ret =spi_write (spi, &adc->tx_buf,1 );if (ret)@@ -106,7 +106,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;@@ -161,6 +161,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);
Re: [PATCH v2 2/2] iio: adc: ti-ads8344: optimize consumption
Hi,
Quoted 12 lines · Jonathan Cameron
Quoted 4 lines · On Thu, 16 Apr 2020 22:54:28 +0200 Alexandre Belloni…
Set the clock mode only once, at probe time and then keep the ADC powered down between conversions.
Signed-off-by Alexandre Belloni <[email protected]>Looks fine to me. I'd like to be lazy and not take this until the fix is in my upstream (even though I suspect the merge would be fine).
Give me a poke if I seem to have forgotten this after that is true!
It now landed in your upstream but I guess you want to take Gregory's patch as a fix first. To be honest I don't think any of the fixes are urgent as it seems we are the only users of the particular ADC.
Signature · 3 lines
Alexandre Belloni, Bootlin Embedded Linux and Kernel engineering https://bootlin.com