From patchwork Wed Apr 15 21:22:55 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alexandre Belloni X-Patchwork-Id: 11492053 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 92DBC6CA for ; Wed, 15 Apr 2020 21:23:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7A99020784 for ; Wed, 15 Apr 2020 21:23:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2438919AbgDOVXO (ORCPT ); Wed, 15 Apr 2020 17:23:14 -0400 Received: from relay12.mail.gandi.net ([217.70.178.232]:37605 "EHLO relay12.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2438899AbgDOVXL (ORCPT ); Wed, 15 Apr 2020 17:23:11 -0400 Received: from localhost (lfbn-lyo-1-9-35.w86-202.abo.wanadoo.fr [86.202.105.35]) (Authenticated sender: alexandre.belloni@bootlin.com) by relay12.mail.gandi.net (Postfix) with ESMTPSA id 05AFA200003; Wed, 15 Apr 2020 21:23:05 +0000 (UTC) From: Alexandre Belloni To: Jonathan Cameron Cc: Hartmut Knaack , Lars-Peter Clausen , Peter Meerwald-Stadler , Gregory CLEMENT , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Alexandre Belloni Subject: [PATCH 1/3] iio: adc: ti-ads8344: properly byte swap value Date: Wed, 15 Apr 2020 23:22:55 +0200 Message-Id: <20200415212257.161238-2-alexandre.belloni@bootlin.com> X-Mailer: git-send-email 2.25.2 In-Reply-To: <20200415212257.161238-1-alexandre.belloni@bootlin.com> References: <20200415212257.161238-1-alexandre.belloni@bootlin.com> MIME-Version: 1.0 Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org 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 Reported-by: kbuild test robot --- drivers/iio/adc/ti-ads8344.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/iio/adc/ti-ads8344.c b/drivers/iio/adc/ti-ads8344.c index 9a460807d46d..6da50ea35217 100644 --- a/drivers/iio/adc/ti-ads8344.c +++ b/drivers/iio/adc/ti-ads8344.c @@ -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, From patchwork Wed Apr 15 21:22:56 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Belloni X-Patchwork-Id: 11492059 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0AEA914DD for ; Wed, 15 Apr 2020 21:23:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EF8B520732 for ; Wed, 15 Apr 2020 21:23:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2442365AbgDOVXR (ORCPT ); Wed, 15 Apr 2020 17:23:17 -0400 Received: from relay1-d.mail.gandi.net ([217.70.183.193]:27701 "EHLO relay1-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2438905AbgDOVXL (ORCPT ); Wed, 15 Apr 2020 17:23:11 -0400 X-Originating-IP: 86.202.105.35 Received: from localhost (lfbn-lyo-1-9-35.w86-202.abo.wanadoo.fr [86.202.105.35]) (Authenticated sender: alexandre.belloni@bootlin.com) by relay1-d.mail.gandi.net (Postfix) with ESMTPSA id 1768124000C; Wed, 15 Apr 2020 21:23:07 +0000 (UTC) From: Alexandre Belloni To: Jonathan Cameron Cc: Hartmut Knaack , Lars-Peter Clausen , Peter Meerwald-Stadler , Gregory CLEMENT , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Alexandre Belloni Subject: [PATCH 2/3] iio: adc: ti-ads8344: remove tx_buf from driver data Date: Wed, 15 Apr 2020 23:22:56 +0200 Message-Id: <20200415212257.161238-3-alexandre.belloni@bootlin.com> X-Mailer: git-send-email 2.25.2 In-Reply-To: <20200415212257.161238-1-alexandre.belloni@bootlin.com> References: <20200415212257.161238-1-alexandre.belloni@bootlin.com> MIME-Version: 1.0 Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org There is no need to keep tx_buf around, it is only used for the conversion. Signed-off-by: Alexandre Belloni --- drivers/iio/adc/ti-ads8344.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/drivers/iio/adc/ti-ads8344.c b/drivers/iio/adc/ti-ads8344.c index 6da50ea35217..9b2d3a8ea6bd 100644 --- a/drivers/iio/adc/ti-ads8344.c +++ b/drivers/iio/adc/ti-ads8344.c @@ -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; From patchwork Wed Apr 15 21:22:57 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Belloni X-Patchwork-Id: 11492055 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 63DDF6CA for ; Wed, 15 Apr 2020 21:23:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4BF7620784 for ; Wed, 15 Apr 2020 21:23:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2438958AbgDOVXQ (ORCPT ); Wed, 15 Apr 2020 17:23:16 -0400 Received: from relay12.mail.gandi.net ([217.70.178.232]:59015 "EHLO relay12.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2438917AbgDOVXL (ORCPT ); Wed, 15 Apr 2020 17:23:11 -0400 Received: from localhost (lfbn-lyo-1-9-35.w86-202.abo.wanadoo.fr [86.202.105.35]) (Authenticated sender: alexandre.belloni@bootlin.com) by relay12.mail.gandi.net (Postfix) with ESMTPSA id C7B2C200004; Wed, 15 Apr 2020 21:23:07 +0000 (UTC) From: Alexandre Belloni To: Jonathan Cameron Cc: Hartmut Knaack , Lars-Peter Clausen , Peter Meerwald-Stadler , Gregory CLEMENT , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Alexandre Belloni Subject: [PATCH 3/3] iio: adc: ti-ads8344: optimize consumption Date: Wed, 15 Apr 2020 23:22:57 +0200 Message-Id: <20200415212257.161238-4-alexandre.belloni@bootlin.com> X-Mailer: git-send-email 2.25.2 In-Reply-To: <20200415212257.161238-1-alexandre.belloni@bootlin.com> References: <20200415212257.161238-1-alexandre.belloni@bootlin.com> MIME-Version: 1.0 Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Set the clock mode only once, at probe time and then keep the ADC powered down between conversions. Signed-off-by: Alexandre Belloni --- drivers/iio/adc/ti-ads8344.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/drivers/iio/adc/ti-ads8344.c b/drivers/iio/adc/ti-ads8344.c index 9b2d3a8ea6bd..0804650d8d8a 100644 --- a/drivers/iio/adc/ti-ads8344.c +++ b/drivers/iio/adc/ti-ads8344.c @@ -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);