From: Shrikant Raskar <raskar.shree97@gmail.com>
To: jic23@kernel.org, robh@kernel.org, krzk+dt@kernel.org,
conor+dt@kernel.org
Cc: dlechner@baylibre.com, nuno.sa@analog.com, andy@kernel.org,
heiko@sntech.de, neil.armstrong@linaro.org,
skhan@linuxfoundation.org, david.hunter.linux@gmail.com,
linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
linux-kernel@vger.kernel.org,
Shrikant Raskar <raskar.shree97@gmail.com>
Subject: [PATCH v2 4/4] iio: proximity: rfd77402: Add interrupt handling support
Date: Sun, 30 Nov 2025 21:07:12 +0530 [thread overview]
Message-ID: <20251130153712.6792-5-raskar.shree97@gmail.com> (raw)
In-Reply-To: <20251130153712.6792-1-raskar.shree97@gmail.com>
Add interrupt handling support to enable event-driven data acquisition
instead of continuous polling. This improves responsiveness, reduces
CPU overhead, and supports low-power operation by allowing the system
to remain idle until an interrupt occurs.
Signed-off-by: Shrikant Raskar <raskar.shree97@gmail.com>
---
Changelog:
Changes since v1:
- Remove 'of.h' header
- Split the condition in 2 parts in interrupt handler
- Add helper rfd77402_wait_for_irq()
- Add helper rfd77402_config_irq()
- Code refactoring
- Return failure if request IRQ failed
Link to v1:https://lore.kernel.org/all/20251126031440.30065-4-raskar.shree97@gmail.com/
Test Logs:
1. Interrupt Mode :
pi@raspberrypi:~$ sudo i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- UU -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
pi@raspberrypi:~$ cat /sys/bus/iio/devices/iio:device0/in_distance_raw
35
pi@raspberrypi:~$ cat /sys/bus/iio/devices/iio:device0/in_distance_raw
39
pi@raspberrypi:~$ cat /proc/interrupts | grep rfd
127: 2 0 0 0 pinctrl-bcm2835 4 Edge rfd77402
pi@raspberrypi:~$ dmesg | grep rfd
[ 7.464789] rfd77402 1-004c: Using interrupt mode
2. Polling Mode :
pi@raspberrypi:~$ sudo i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- UU -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
pi@raspberrypi:~$ dmesg | grep rfd
[ 26.908031] rfd77402 1-004c: No interrupt specified, using polling mode
pi@raspberrypi:~$ cat /sys/bus/iio/devices/iio:device0/in_distance_raw
36
pi@raspberrypi:~$ cat /sys/bus/iio/devices/iio:device0/in_distance_raw
45
---
drivers/iio/proximity/rfd77402.c | 126 +++++++++++++++++++++++++++++--
1 file changed, 118 insertions(+), 8 deletions(-)
diff --git a/drivers/iio/proximity/rfd77402.c b/drivers/iio/proximity/rfd77402.c
index 2152509816ca..8441a7bcabec 100644
--- a/drivers/iio/proximity/rfd77402.c
+++ b/drivers/iio/proximity/rfd77402.c
@@ -6,19 +6,21 @@
*
* 7-bit I2C slave address 0x4c
*
- * TODO: interrupt
* https://media.digikey.com/pdf/Data%20Sheets/RF%20Digital%20PDFs/RFD77402.pdf
*/
#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/delay.h>
-
+#include <linux/interrupt.h>
+#include <linux/completion.h>
#include <linux/iio/iio.h>
#define RFD77402_DRV_NAME "rfd77402"
#define RFD77402_ICSR 0x00 /* Interrupt Control Status Register */
+#define RFD77402_ICSR_CLR_CFG BIT(0)
+#define RFD77402_ICSR_CLR_TYPE BIT(1)
#define RFD77402_ICSR_INT_MODE BIT(2)
#define RFD77402_ICSR_INT_POL BIT(3)
#define RFD77402_ICSR_RESULT BIT(4)
@@ -26,6 +28,12 @@
#define RFD77402_ICSR_H2M_MSG BIT(6)
#define RFD77402_ICSR_RESET BIT(7)
+#define RFD77402_IER 0x02
+#define RFD77402_IER_RESULT BIT(0)
+#define RFD77402_IER_M2H_MSG BIT(1)
+#define RFD77402_IER_H2M_MSG BIT(2)
+#define RFD77402_IER_RESET BIT(3)
+
#define RFD77402_CMD_R 0x04
#define RFD77402_CMD_SINGLE 0x01
#define RFD77402_CMD_STANDBY 0x10
@@ -80,6 +88,10 @@ struct rfd77402_data {
struct i2c_client *client;
/* Serialize reads from the sensor */
struct mutex lock;
+ /* Completion for interrupt-driven measurements */
+ struct completion completion;
+ /* Flag to indicate if interrupt is available */
+ bool irq_en;
};
static const struct iio_chan_spec rfd77402_channels[] = {
@@ -110,6 +122,36 @@ static int rfd77402_set_state(struct i2c_client *client, u8 state, u16 check)
return 0;
}
+static irqreturn_t rfd77402_interrupt_handler(int irq, void *dev_id)
+{
+ struct rfd77402_data *data = dev_id;
+ int ret;
+
+ ret = i2c_smbus_read_byte_data(data->client, RFD77402_ICSR);
+ if (ret < 0)
+ return IRQ_NONE;
+
+ /* Check if the interrupt is from our device */
+ if (!(ret & RFD77402_ICSR_RESULT))
+ return IRQ_NONE;
+
+ /* Signal completion of measurement */
+ complete(&data->completion);
+ return IRQ_HANDLED;
+}
+
+static int rfd77402_wait_for_irq(struct rfd77402_data *data)
+{
+ int ret;
+
+ ret = wait_for_completion_timeout(&data->completion,
+ msecs_to_jiffies(200));
+ if (ret == 0)
+ return -ETIMEDOUT;
+
+ return 0;
+}
+
static int rfd77402_result_polling(struct i2c_client *client)
{
int ret;
@@ -129,21 +171,31 @@ static int rfd77402_result_polling(struct i2c_client *client)
return -ETIMEDOUT;
}
-static int rfd77402_measure(struct i2c_client *client)
+static int rfd77402_measure(struct rfd77402_data *data)
{
+ struct i2c_client *client = data->client;
int ret;
+
ret = rfd77402_set_state(client, RFD77402_CMD_MCPU_ON,
RFD77402_STATUS_MCPU_ON);
if (ret < 0)
return ret;
+ /* Initialize completion for interrupt mode */
+ if (data->irq_en)
+ reinit_completion(&data->completion);
+
ret = i2c_smbus_write_byte_data(client, RFD77402_CMD_R,
RFD77402_CMD_SINGLE |
RFD77402_CMD_VALID);
if (ret < 0)
goto err;
- ret = rfd77402_result_polling(client);
+ if (data->irq_en)
+ ret = rfd77402_wait_for_irq(data);
+ else
+ ret = rfd77402_result_polling(data->client);
+
if (ret < 0)
goto err;
@@ -175,7 +227,7 @@ static int rfd77402_read_raw(struct iio_dev *indio_dev,
switch (mask) {
case IIO_CHAN_INFO_RAW:
mutex_lock(&data->lock);
- ret = rfd77402_measure(data->client);
+ ret = rfd77402_measure(data);
mutex_unlock(&data->lock);
if (ret < 0)
return ret;
@@ -195,8 +247,25 @@ static const struct iio_info rfd77402_info = {
.read_raw = rfd77402_read_raw,
};
+static int rfd77402_config_irq(struct i2c_client *client, u8 csr, u8 ier)
+{
+ int ret;
+
+ ret = i2c_smbus_write_byte_data(client, RFD77402_ICSR, csr);
+ if (ret < 0)
+ return ret;
+
+ ret = i2c_smbus_write_byte_data(client, RFD77402_IER, ier);
+ if (ret < 0)
+ return ret;
+
+ return 0;
+}
+
static int rfd77402_init(struct i2c_client *client)
{
+ struct iio_dev *indio_dev = i2c_get_clientdata(client);
+ struct rfd77402_data *data = iio_priv(indio_dev);
int ret, i;
ret = rfd77402_set_state(client, RFD77402_CMD_STANDBY,
@@ -204,9 +273,25 @@ static int rfd77402_init(struct i2c_client *client)
if (ret < 0)
return ret;
- /* configure INT pad as push-pull, active low */
- ret = i2c_smbus_write_byte_data(client, RFD77402_ICSR,
- RFD77402_ICSR_INT_MODE);
+ if (data->irq_en) {
+ /*
+ * Enable interrupt mode:
+ * - Configure ICSR for auto-clear on read, push-pull output and falling edge
+ * - Enable "result ready" interrupt in IER
+ */
+ ret = rfd77402_config_irq(client,
+ RFD77402_ICSR_CLR_CFG |
+ RFD77402_ICSR_INT_MODE,
+ RFD77402_IER_RESULT);
+ } else {
+ /*
+ * Disable all interrupts:
+ * - Clear ICSR configuration
+ * - Disable all interrupt in IER
+ */
+ ret = rfd77402_config_irq(client, 0, 0);
+ }
+
if (ret < 0)
return ret;
@@ -283,6 +368,31 @@ static int rfd77402_probe(struct i2c_client *client)
data = iio_priv(indio_dev);
data->client = client;
mutex_init(&data->lock);
+ init_completion(&data->completion);
+
+ i2c_set_clientdata(client, indio_dev);
+
+ data->irq_en = false;
+ if (client->irq > 0) {
+ /* interrupt mode */
+ ret = devm_request_threaded_irq(&client->dev, client->irq,
+ NULL, rfd77402_interrupt_handler,
+ IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
+ "rfd77402", data);
+ if (ret < 0) {
+ dev_err(&client->dev,
+ "Failed to request IRQ %d: %d\n",
+ client->irq, ret);
+ return ret;
+ }
+
+ data->irq_en = true;
+ dev_info(&client->dev, "Using interrupt mode\n");
+
+ } else {
+ /* polling mode */
+ dev_info(&client->dev, "No interrupt specified, using polling mode\n");
+ }
indio_dev->info = &rfd77402_info;
indio_dev->channels = rfd77402_channels;
--
2.43.0
next prev parent reply other threads:[~2025-11-30 15:38 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-30 15:37 [PATCH v2 0/4] iio: proximity: Add DT and interrupt support for RFD77402 Shrikant Raskar
2025-11-30 15:37 ` [PATCH v2 1/4] dt-bindings: iio: proximity: Add RF Digital RFD77402 ToF sensor Shrikant Raskar
2025-11-30 16:16 ` Rob Herring (Arm)
2025-12-01 8:06 ` Krzysztof Kozlowski
2025-12-02 3:15 ` Shrikant
2025-11-30 15:37 ` [PATCH v2 2/4] iio: proximity: rfd77402: Add Device Tree support Shrikant Raskar
2025-11-30 16:48 ` Andy Shevchenko
2025-11-30 15:37 ` [PATCH v2 3/4] iio: proximity: rfd77402: Move polling logic into helper Shrikant Raskar
2025-11-30 16:52 ` Andy Shevchenko
2025-12-07 15:53 ` Jonathan Cameron
2025-11-30 15:37 ` Shrikant Raskar [this message]
2025-11-30 17:01 ` [PATCH v2 4/4] iio: proximity: rfd77402: Add interrupt handling support Andy Shevchenko
2025-12-07 15:58 ` 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=20251130153712.6792-5-raskar.shree97@gmail.com \
--to=raskar.shree97@gmail.com \
--cc=andy@kernel.org \
--cc=conor+dt@kernel.org \
--cc=david.hunter.linux@gmail.com \
--cc=devicetree@vger.kernel.org \
--cc=dlechner@baylibre.com \
--cc=heiko@sntech.de \
--cc=jic23@kernel.org \
--cc=krzk+dt@kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=neil.armstrong@linaro.org \
--cc=nuno.sa@analog.com \
--cc=robh@kernel.org \
--cc=skhan@linuxfoundation.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;
as well as URLs for NNTP newsgroup(s).