devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@intel.com>
To: Shrikant Raskar <raskar.shree97@gmail.com>
Cc: jic23@kernel.org, robh@kernel.org, krzk+dt@kernel.org,
	conor+dt@kernel.org, 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
Subject: Re: [PATCH 3/3] iio: proximity: rfd77402: Add interrupt handling support
Date: Wed, 26 Nov 2025 09:35:04 +0200	[thread overview]
Message-ID: <aSatqG9UEqkH0Glw@smile.fi.intel.com> (raw)
In-Reply-To: <20251126031440.30065-4-raskar.shree97@gmail.com>

On Wed, Nov 26, 2025 at 08:44:40AM +0530, Shrikant Raskar wrote:
> 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.

...

>  #include <linux/module.h>
>  #include <linux/i2c.h>
>  #include <linux/delay.h>
> -

Stray removal of blank line.

> +#include <linux/interrupt.h>
> +#include <linux/completion.h>

> +#include <linux/of.h>

Please, avoid using of.h in a new code.

>  #include <linux/iio/iio.h>

...

> +static irqreturn_t rfd77402_interrupt_handler(int irq, void *dev_id)
> +{
> +	struct rfd77402_data *data = dev_id;
> +	int ret;

> +	/* Check if the interrupt is from our device */

This comment only for the second part and I would split the condition to make
it clearer.

> +	ret = i2c_smbus_read_byte_data(data->client, RFD77402_ICSR);
> +	if (ret < 0 || !(ret & RFD77402_ICSR_RESULT))
> +		return IRQ_NONE;

	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;
> +}

...

> -	while (tries-- > 0) {
> -		ret = i2c_smbus_read_byte_data(client, RFD77402_ICSR);
> -		if (ret < 0)
> +	if (data->irq_en) {
> +		/* Wait for interrupt-driven completion */
> +		ret = wait_for_completion_timeout(&data->completion,
> +						  msecs_to_jiffies(200));
> +		if (ret == 0) {
> +			ret = -ETIMEDOUT;
>  			goto err;
> -		if (ret & RFD77402_ICSR_RESULT)
> -			break;
> -		msleep(20);
> -	}
> -
> -	if (tries < 0) {
> -		ret = -ETIMEDOUT;
> -		goto err;
> +		}
> +	} else {
> +		/* Fallback to polling mode */
> +		while (tries-- > 0) {
> +			ret = i2c_smbus_read_byte_data(client, RFD77402_ICSR);
> +			if (ret < 0)
> +				goto err;
> +			if (ret & RFD77402_ICSR_RESULT)
> +				break;
> +			msleep(20);
> +		}
> +
> +		if (tries < 0) {
> +			ret = -ETIMEDOUT;
> +			goto err;
> +		}
>  	}

Instead, move the current code into a helper (in a separate patch) and alter it
here with new conditional. So in the result it will be something like

	if (...)
		ret = call_new_helper_for_irq();
	else
		ret = call_old_helper_for_polling();

...

> +	if (data->irq_en) {
> +		/* Configure ICSR: auto-clear on read, push-pull, falling edge */
> +		ret = i2c_smbus_write_byte_data(client, RFD77402_ICSR,
> +						RFD77402_ICSR_CLR_CFG |
> +						RFD77402_ICSR_INT_MODE);
> +		if (ret < 0)
> +			return ret;
> +
> +		/* Enable 'new data available' interrupt in IER */
> +		ret = i2c_smbus_write_byte_data(client, RFD77402_IER,
> +						RFD77402_IER_RESULT);
> +		if (ret < 0)
> +			return ret;
> +	} else {
> +		/* Disable interrupts */
> +		ret = i2c_smbus_write_byte_data(client, RFD77402_ICSR, 0);
> +		if (ret < 0)
> +			return ret;
> +
> +		ret = i2c_smbus_write_byte_data(client, RFD77402_IER, 0);
> +		if (ret < 0)
> +			return ret;
> +	}

This can be also factored out to a helper(s). Something like this, perhaps

	if (irq_en)
		ret = call_a_helper(client, $CSR, $ER);
	else
		ret = call_a_helper(client, 0, 0);

...

> +	/* Check if interrupt is mentioned in device tree */
> +	data->irq_en = false;
> +	if (client->irq > 0) {
> +		ret = devm_request_threaded_irq(&client->dev, client->irq,
> +						NULL, rfd77402_interrupt_handler,
> +						IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
> +						"rfd77402", data);
> +		if (ret == 0) {
> +			data->irq_en = true;
> +			dev_info(&client->dev, "Using interrupt mode\n");
> +		} else {
> +			dev_warn(&client->dev,
> +				 "Failed to request IRQ %d, using polling mode: %d\n",
> +				 client->irq, ret);

If we asked for interrupt and didn't get it due to "linux" errors, we should
not fallback. No need to work around bugs in the DT, the DT description must
be fixed instead.

> +		}
> +	} else {
> +		dev_info(&client->dev, "No interrupt specified, using polling mode\n");
> +	}

-- 
With Best Regards,
Andy Shevchenko



  reply	other threads:[~2025-11-26  7:35 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-26  3:14 [PATCH 0/3] iio: proximity: Add DT and interrupt support for RFD77402 Shrikant Raskar
2025-11-26  3:14 ` [PATCH 1/3] dt-bindings: iio: proximity: Add YAML binding for RFD77402 ToF sensor Shrikant Raskar
2025-11-26  9:53   ` Krzysztof Kozlowski
2025-11-26 18:09     ` Shrikant
2025-11-26  3:14 ` [PATCH 2/3] iio: proximity: rfd77402: Add Device Tree support Shrikant Raskar
2025-11-26  6:53   ` Andy Shevchenko
2025-11-26  3:14 ` [PATCH 3/3] iio: proximity: rfd77402: Add interrupt handling support Shrikant Raskar
2025-11-26  7:35   ` Andy Shevchenko [this message]
2025-11-28 16:06     ` Shrikant
2025-12-07 15:47       ` 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=aSatqG9UEqkH0Glw@smile.fi.intel.com \
    --to=andriy.shevchenko@intel.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=raskar.shree97@gmail.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).