From: Lucas Sobrinho <lucasmsobrinho@gmail.com>
To: jic23@kernel.org
Cc: linux-iio@vger.kernel.org, filipetressmann@usp.br,
davidbtadokoro@usp.br, paulormm@ime.usp.br,
Lucas Sobrinho <lucasmsobrinho@gmail.com>
Subject: [PATCH] iio: adc: ti-ads124s08: Add direct mode protection and refactor read channel logic
Date: Sun, 27 Apr 2025 19:22:34 -0300 [thread overview]
Message-ID: <20250427222234.126285-1-lucasmsobrinho@gmail.com> (raw)
Add a call to iio_device_claim_direct() in the ads124s_read_raw() function
to ensure exclusive access to the device during direct mode reads.
Also refactor the channel read logic into a separate function.
Signed-off-by: Lucas Sobrinho <lucasmsobrinho@gmail.com>
Co-developed-by: Filipe Tressmann Velozo <filipetressmann@usp.br>
Signed-off-by: Filipe Tressmann Velozo <filipetressmann@usp.br>
---
drivers/iio/adc/ti-ads124s08.c | 85 ++++++++++++++++------------------
1 file changed, 40 insertions(+), 45 deletions(-)
diff --git a/drivers/iio/adc/ti-ads124s08.c b/drivers/iio/adc/ti-ads124s08.c
index 8ea1269f7..e1efe8865 100644
--- a/drivers/iio/adc/ti-ads124s08.c
+++ b/drivers/iio/adc/ti-ads124s08.c
@@ -219,6 +219,40 @@ static int ads124s_read(struct iio_dev *indio_dev)
return get_unaligned_be24(&priv->data[2]);
}
+static int ads124s_read_channel(struct iio_dev *indio_dev,
+ struct iio_chan_spec const *chan,
+ int *val)
+{
+ int ret = ads124s_write_reg(indio_dev, ADS124S08_INPUT_MUX, chan);
+
+ if (ret) {
+ dev_err(&priv->spi->dev, "Set ADC CH failed\n");
+ return ret;
+ }
+
+ ret = ads124s_write_cmd(indio_dev, ADS124S08_START_CONV);
+ if (ret) {
+ dev_err(&priv->spi->dev, "Start ADC conversions failed\n");
+ return ret;
+ }
+
+ ret = ads124s_read(indio_dev);
+ if (ret < 0) {
+ dev_err(&priv->spi->dev, "Read ADC failed\n");
+ return ret;
+ }
+
+ *val = ret;
+
+ ret = ads124s_write_cmd(indio_dev, ADS124S08_STOP_CONV);
+ if (ret) {
+ dev_err(&priv->spi->dev, "Stop conversions failed\n");
+ return ret;
+ }
+
+ return IIO_VAL_INT;
+}
+
static int ads124s_read_raw(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan,
int *val, int *val2, long m)
@@ -226,44 +260,18 @@ static int ads124s_read_raw(struct iio_dev *indio_dev,
struct ads124s_private *priv = iio_priv(indio_dev);
int ret;
- mutex_lock(&priv->lock);
switch (m) {
case IIO_CHAN_INFO_RAW:
- ret = ads124s_write_reg(indio_dev, ADS124S08_INPUT_MUX,
- chan->channel);
- if (ret) {
- dev_err(&priv->spi->dev, "Set ADC CH failed\n");
- goto out;
- }
-
- ret = ads124s_write_cmd(indio_dev, ADS124S08_START_CONV);
- if (ret) {
- dev_err(&priv->spi->dev, "Start conversions failed\n");
- goto out;
- }
-
- ret = ads124s_read(indio_dev);
- if (ret < 0) {
- dev_err(&priv->spi->dev, "Read ADC failed\n");
- goto out;
- }
-
- *val = ret;
-
- ret = ads124s_write_cmd(indio_dev, ADS124S08_STOP_CONV);
- if (ret) {
- dev_err(&priv->spi->dev, "Stop conversions failed\n");
- goto out;
- }
-
- ret = IIO_VAL_INT;
+ if (!iio_device_claim_direct(indio_dev))
+ return -EBUSY;
+
+ ret = ads124s_read_channel(indio_dev, chan->channel, val);
+ iio_device_release_direct(indio_dev);
break;
default:
ret = -EINVAL;
break;
}
-out:
- mutex_unlock(&priv->lock);
return ret;
}
@@ -280,20 +288,7 @@ static irqreturn_t ads124s_trigger_handler(int irq, void *p)
int ret;
iio_for_each_active_channel(indio_dev, scan_index) {
- ret = ads124s_write_reg(indio_dev, ADS124S08_INPUT_MUX,
- scan_index);
- if (ret)
- dev_err(&priv->spi->dev, "Set ADC CH failed\n");
-
- ret = ads124s_write_cmd(indio_dev, ADS124S08_START_CONV);
- if (ret)
- dev_err(&priv->spi->dev, "Start ADC conversions failed\n");
-
- priv->buffer[j] = ads124s_read(indio_dev);
- ret = ads124s_write_cmd(indio_dev, ADS124S08_STOP_CONV);
- if (ret)
- dev_err(&priv->spi->dev, "Stop ADC conversions failed\n");
-
+ ads124s_read_channel(indio_dev, chan->channel, &(priv->buffer[j]));
j++;
}
--
2.49.0
next reply other threads:[~2025-04-27 22:22 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-27 22:22 Lucas Sobrinho [this message]
2025-05-05 15:32 ` [PATCH] iio: adc: ti-ads124s08: Add direct mode protection and refactor read channel logic 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=20250427222234.126285-1-lucasmsobrinho@gmail.com \
--to=lucasmsobrinho@gmail.com \
--cc=davidbtadokoro@usp.br \
--cc=filipetressmann@usp.br \
--cc=jic23@kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=paulormm@ime.usp.br \
/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