public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Mitja Spes <mitja@lxnav.com>
To: Jonathan Cameron <jic23@kernel.org>,
	Lars-Peter Clausen <lars@metafoo.de>,
	Mitja Spes <mitja@lxnav.com>,
	Angelo Compagnucci <angelo.compagnucci@gmail.com>,
	linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH 4/4] iio: adc: mcp3422: reduce sleep for fast sampling rates
Date: Fri, 11 Nov 2022 12:26:56 +0100	[thread overview]
Message-ID: <20221111112657.1521307-5-mitja@lxnav.com> (raw)
In-Reply-To: <20221111112657.1521307-1-mitja@lxnav.com>

- reduced sleep time for 240 & 60 sps rates
- minor roundup fix for sleep times

Signed-off-by: Mitja Spes <mitja@lxnav.com>
---
 drivers/iio/adc/mcp3422.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/drivers/iio/adc/mcp3422.c b/drivers/iio/adc/mcp3422.c
index eef35fb2fc22..dbcc8fe91aaa 100644
--- a/drivers/iio/adc/mcp3422.c
+++ b/drivers/iio/adc/mcp3422.c
@@ -70,10 +70,11 @@ static const int mcp3422_scales[MCP3422_SRATE_COUNT][MCP3422_PGA_COUNT] = {
 
 /* Constant msleep times for data acquisitions */
 static const int mcp3422_read_times[MCP3422_SRATE_COUNT] = {
-	[MCP3422_SRATE_240] = 1000 / 240,
-	[MCP3422_SRATE_60] = 1000 / 60,
-	[MCP3422_SRATE_15] = 1000 / 15,
-	[MCP3422_SRATE_3] = 1000 / 3 };
+	[MCP3422_SRATE_240] = DIV_ROUND_UP(1000, 240),
+	[MCP3422_SRATE_60] = DIV_ROUND_UP(1000, 60),
+	[MCP3422_SRATE_15] = DIV_ROUND_UP(1000, 15),
+	[MCP3422_SRATE_3] = (100000 + 375 - 100) / 375 /* real rate is 3.75 sps */
+};
 
 /* sample rates to integer conversion table */
 static const int mcp3422_sample_rates[MCP3422_SRATE_COUNT] = {
@@ -137,6 +138,7 @@ static int mcp3422_read_channel(struct mcp3422 *adc,
 				struct iio_chan_spec const *channel, int *value)
 {
 	int ret;
+	int sleep_duration;
 	u8 config;
 	u8 req_channel = channel->channel;
 
@@ -148,7 +150,11 @@ static int mcp3422_read_channel(struct mcp3422 *adc,
 			mutex_unlock(&adc->lock);
 			return ret;
 		}
-		msleep(mcp3422_read_times[MCP3422_SAMPLE_RATE(adc->active_config)]);
+		sleep_duration = mcp3422_read_times[MCP3422_SAMPLE_RATE(adc->active_config)];
+		if (sleep_duration < 20)
+			usleep_range(sleep_duration * 1000, sleep_duration * 1300);
+		else
+			msleep(sleep_duration);
 	}
 
 	ret = mcp3422_read(adc, value, &config);
-- 
2.34.1


  parent reply	other threads:[~2022-11-11 11:28 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-11 11:26 [PATCH 0/4] iio: adc: mcp3422 improvements Mitja Spes
2022-11-11 11:26 ` [PATCH 1/4] iio: adc: mcp3422: fix scale read bug Mitja Spes
2022-11-12 17:10   ` Jonathan Cameron
2022-11-12 20:06     ` Mitja Špes
2022-11-11 11:26 ` [PATCH 2/4] iio: adc: mcp3422: allow setting gain and sampling per channel Mitja Spes
2022-11-12 17:28   ` Jonathan Cameron
2022-11-12 20:51     ` Mitja Špes
2022-11-13 12:06       ` Jonathan Cameron
2022-11-13 13:39         ` Mitja Špes
2022-11-14 20:18           ` Jonathan Cameron
2022-11-11 11:26 ` [PATCH 3/4] iio: adc: mcp3422: add hardware gain attribute Mitja Spes
2022-11-12 17:32   ` Jonathan Cameron
2022-11-12 21:19     ` Mitja Špes
2022-11-13 12:33       ` Jonathan Cameron
2022-11-13 13:51         ` Mitja Špes
2022-11-11 11:26 ` Mitja Spes [this message]
2022-11-12 17:33   ` [PATCH 4/4] iio: adc: mcp3422: reduce sleep for fast sampling rates Jonathan Cameron

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221111112657.1521307-5-mitja@lxnav.com \
    --to=mitja@lxnav.com \
    --cc=angelo.compagnucci@gmail.com \
    --cc=jic23@kernel.org \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox