From: Abhash Jha <abhashkumarjha123@gmail.com>
To: linux-iio@vger.kernel.org
Cc: jic23@kernel.org, lars@metafoo.de, linux-kernel@vger.kernel.org,
Abhash Jha <abhashkumarjha123@gmail.com>
Subject: [PATCH v3 2/3] iio: light: vl6180: Added Interrupt support for single shot access
Date: Mon, 7 Oct 2024 20:49:46 +0530 [thread overview]
Message-ID: <20241007151947.58828-3-abhashkumarjha123@gmail.com> (raw)
In-Reply-To: <20241007151947.58828-1-abhashkumarjha123@gmail.com>
The interrupts are serviced in the `vl6180_measure` function when the
irq_handler signals that the reading is complete. We now can read
asynchronously if `client->irq` is set.
Signed-off-by: Abhash Jha <abhashkumarjha123@gmail.com>
---
drivers/iio/light/vl6180.c | 54 +++++++++++++++++++++++++++++---------
1 file changed, 42 insertions(+), 12 deletions(-)
diff --git a/drivers/iio/light/vl6180.c b/drivers/iio/light/vl6180.c
index 67aa2f101..a747501b0 100644
--- a/drivers/iio/light/vl6180.c
+++ b/drivers/iio/light/vl6180.c
@@ -86,6 +86,7 @@
struct vl6180_data {
struct i2c_client *client;
struct mutex lock;
+ struct completion completion;
unsigned int als_gain_milli;
unsigned int als_it_ms;
unsigned int als_meas_rate;
@@ -211,29 +212,38 @@ static int vl6180_write_word(struct i2c_client *client, u16 cmd, u16 val)
static int vl6180_measure(struct vl6180_data *data, int addr)
{
struct i2c_client *client = data->client;
+ unsigned long time_left;
int tries = 20, ret;
u16 value;
mutex_lock(&data->lock);
+ reinit_completion(&data->completion);
+
/* Start single shot measurement */
ret = vl6180_write_byte(client,
vl6180_chan_regs_table[addr].start_reg, VL6180_STARTSTOP);
if (ret < 0)
goto fail;
- while (tries--) {
- ret = vl6180_read_byte(client, VL6180_INTR_STATUS);
- if (ret < 0)
- goto fail;
-
- if (ret & vl6180_chan_regs_table[addr].drdy_mask)
- break;
- msleep(20);
- }
+ if (client->irq) {
+ time_left = wait_for_completion_timeout(&data->completion, HZ / 10);
+ if (time_left == 0)
+ return -ETIMEDOUT;
+ } else {
+ while (tries--) {
+ ret = vl6180_read_byte(client, VL6180_INTR_STATUS);
+ if (ret < 0)
+ goto fail;
+
+ if (ret & vl6180_chan_regs_table[addr].drdy_mask)
+ break;
+ msleep(20);
+ }
- if (tries < 0) {
- ret = -EIO;
- goto fail;
+ if (tries < 0) {
+ ret = -EIO;
+ goto fail;
+ }
}
/* Read result value from appropriate registers */
@@ -484,6 +494,15 @@ static int vl6180_write_raw(struct iio_dev *indio_dev,
}
}
+static irqreturn_t vl6180_threaded_irq(int irq, void *priv)
+{
+ struct iio_dev *indio_dev = priv;
+ struct vl6180_data *data = iio_priv(indio_dev);
+
+ complete(&data->completion);
+ return IRQ_HANDLED;
+}
+
static const struct iio_info vl6180_info = {
.read_raw = vl6180_read_raw,
.write_raw = vl6180_write_raw,
@@ -583,6 +602,17 @@ static int vl6180_probe(struct i2c_client *client)
if (ret < 0)
return ret;
+ if (client->irq) {
+ ret = devm_request_threaded_irq(&client->dev, client->irq,
+ NULL, vl6180_threaded_irq,
+ IRQF_ONESHOT,
+ indio_dev->name, indio_dev);
+ if (ret)
+ return dev_err_probe(&client->dev, ret, "devm_request_irq error \n");
+
+ init_completion(&data->completion);
+ }
+
return devm_iio_device_register(&client->dev, indio_dev);
}
--
2.43.0
next prev parent reply other threads:[~2024-10-07 15:20 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-07 15:19 [PATCH v3 0/3] Abhash Jha
2024-10-07 15:19 ` [PATCH v3 1/3] iio: light: vl6180: Add configurable inter-measurement period support Abhash Jha
2024-10-07 15:19 ` Abhash Jha [this message]
2024-10-07 15:19 ` [PATCH v3 3/3] iio: light: vl6180: Add support for Continuous Mode Abhash Jha
-- strict thread matches above, loose matches on Subject: below --
2024-10-07 15:22 [PATCH v3 0/3] Interrupt and Continuous mode support for VL6180 Abhash Jha
2024-10-07 15:22 ` [PATCH v3 2/3] iio: light: vl6180: Added Interrupt support for single shot access Abhash Jha
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=20241007151947.58828-3-abhashkumarjha123@gmail.com \
--to=abhashkumarjha123@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