Linux IIO development
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Joshua Crofts via B4 Relay <devnull+joshua.crofts1.gmail.com@kernel.org>
Cc: joshua.crofts1@gmail.com, "David Lechner" <dlechner@baylibre.com>,
	"Nuno Sá" <nuno.sa@analog.com>,
	"Andy Shevchenko" <andy@kernel.org>,
	linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 7/7] iio: light: opt3001: switch driver to managed resources
Date: Mon, 11 May 2026 14:32:37 +0100	[thread overview]
Message-ID: <20260511143237.040382d8@jic23-huawei> (raw)
In-Reply-To: <20260511-opt3001-cleanup-v1-7-f7879dc3455c@gmail.com>

On Mon, 11 May 2026 12:04:12 +0200
Joshua Crofts via B4 Relay <devnull+joshua.crofts1.gmail.com@kernel.org> wrote:

> From: Joshua Crofts <joshua.crofts1@gmail.com>
> 
> Move the driver to use devm_* functions to automate resource
> management and simplify error handling. This also allows removal
> of the opt3001_remove() function.
> 
> Signed-off-by: Joshua Crofts <joshua.crofts1@gmail.com>
Hi Joshua

A few things inline.

Jonathan

> ---
>  drivers/iio/light/opt3001.c | 67 +++++++++++++++++++++++----------------------
>  1 file changed, 34 insertions(+), 33 deletions(-)
> 
> diff --git a/drivers/iio/light/opt3001.c b/drivers/iio/light/opt3001.c
> index 155794bb28f68a945e20b083e382561ca6ad462e..3ea18d8993da627ae226ac414e8035d8c968861b 100644
> --- a/drivers/iio/light/opt3001.c
> +++ b/drivers/iio/light/opt3001.c
> @@ -804,6 +804,29 @@ static irqreturn_t opt3001_irq(int irq, void *_iio)
>  	return IRQ_HANDLED;
>  }
>  
> +static void opt3001_power_off(void *data)
> +{
> +	struct opt3001 *opt = data;
> +	int ret;
> +	u16 reg;
> +
> +	ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION);
> +	if (ret < 0) {
> +		dev_err(opt->dev, "failed to read register %02x\n",
> +			OPT3001_CONFIGURATION);
> +		return;
> +	}
> +
> +	reg = ret;
> +	opt3001_set_mode(opt, &reg, OPT3001_CONFIGURATION_M_SHUTDOWN);
> +
> +	ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION,
> +					   reg);
> +	if (ret < 0)
> +		dev_err(opt->dev, "failed to write register %02x\n",
> +			OPT3001_CONFIGURATION);
> +}
> +
>  static int opt3001_probe(struct i2c_client *client)
>  {
>  	struct device *dev = &client->dev;
> @@ -822,7 +845,10 @@ static int opt3001_probe(struct i2c_client *client)
>  	opt->dev = dev;
>  	opt->chip_info = i2c_get_match_data(client);
>  
> -	mutex_init(&opt->lock);
> +	ret = devm_mutex_init(dev, &opt->lock);
> +	if (ret)
> +		return dev_err_probe(dev, ret, "Failed to initialize mutex\n");

Don't print on this one.  I'm fairly sure it can only return -ENOMEM as
part of devm registration failing and for those we don't print errors.
dev_err_probe() won't print anything anyway so it's just noise having
it here.


> +
>  	init_waitqueue_head(&opt->result_ready_queue);
>  	i2c_set_clientdata(client, iio);
>  
> @@ -836,6 +862,10 @@ static int opt3001_probe(struct i2c_client *client)
>  	if (ret)
>  		return ret;
>  
> +	ret = devm_add_action_or_reset(dev, opt3001_power_off, opt);
> +	if (ret)
> +		return ret;

This is undoing only part of what happens in the previous call (I think)
and if we get an error in there (opt3001_configure()) we don't turn the
power off again.  I'd move this registration into that function - probably after
the configuration write but before the thresholds etc are set.

That way those error paths are covered and it's more obvious what this
is undoing.

> +
>  	iio->name = client->name;
>  	iio->channels = *opt->chip_info->channels;
>  	iio->num_channels = opt->chip_info->num_channels;
> @@ -849,9 +879,9 @@ static int opt3001_probe(struct i2c_client *client)
>  
>  	/* Make use of INT pin only if valid IRQ no. is given */
>  	if (irq > 0) {
> -		ret = request_threaded_irq(irq, NULL, opt3001_irq,
> -				IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
> -				"opt3001", iio);
> +		ret = devm_request_threaded_irq(dev, irq, NULL, opt3001_irq,

Hmm. Without hardware too risky I think to touch it, but why is this only registering
the interrupt after the userspace interfaces? That seems to be a nasty race.

> +						IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
> +						"opt3001", iio);
>  		if (ret)
>  			return dev_err_probe(dev, ret,
>  					     "failed to request IRQ #%d\n",
> @@ -864,34 +894,6 @@ static int opt3001_probe(struct i2c_client *client)
>  	return 0;
>  }
>  
> -static void opt3001_remove(struct i2c_client *client)
> -{
> -	struct iio_dev *iio = i2c_get_clientdata(client);
> -	struct opt3001 *opt = iio_priv(iio);
> -	int ret;
> -	u16 reg;
> -
> -	if (opt->use_irq)
> -		free_irq(client->irq, iio);
> -
> -	ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION);
> -	if (ret < 0) {
> -		dev_err(opt->dev, "failed to read register %02x\n",
> -				OPT3001_CONFIGURATION);
> -		return;
> -	}
> -
> -	reg = ret;
> -	opt3001_set_mode(opt, &reg, OPT3001_CONFIGURATION_M_SHUTDOWN);
> -
> -	ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION,
> -			reg);
> -	if (ret < 0) {
> -		dev_err(opt->dev, "failed to write register %02x\n",
> -				OPT3001_CONFIGURATION);
> -	}
> -}
> -
>  static const struct opt3001_chip_info opt3001_chip_information = {
>  	.channels = &opt3001_channels,
>  	.chan_type = IIO_LIGHT,
> @@ -930,7 +932,6 @@ MODULE_DEVICE_TABLE(of, opt3001_of_match);
>  
>  static struct i2c_driver opt3001_driver = {
>  	.probe = opt3001_probe,
> -	.remove = opt3001_remove,
>  	.id_table = opt3001_id,
>  
>  	.driver = {
> 


  parent reply	other threads:[~2026-05-11 13:32 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-11 10:04 [PATCH 0/7] iio: light: opt3001: driver cleanup Joshua Crofts via B4 Relay
2026-05-11 10:04 ` [PATCH 1/7] iio: light: opt3001: make headers conform to iwyu Joshua Crofts via B4 Relay
2026-05-11 10:04 ` [PATCH 2/7] iio: light: opt3001: use macros from bits.h header Joshua Crofts via B4 Relay
2026-05-11 11:01   ` Andy Shevchenko
2026-05-11 11:07     ` Joshua Crofts
2026-05-11 13:21       ` Jonathan Cameron
2026-05-11 10:04 ` [PATCH 3/7] iio: light: opt3001: prefer dev_err_probe() Joshua Crofts via B4 Relay
2026-05-11 11:04   ` Andy Shevchenko
2026-05-11 11:11     ` Joshua Crofts
2026-05-11 10:04 ` [PATCH 4/7] iio: light: opt3001: move driver to guard(mutex)() use Joshua Crofts via B4 Relay
2026-05-11 11:07   ` Andy Shevchenko
2026-05-11 11:09     ` Joshua Crofts
2026-05-11 10:04 ` [PATCH 5/7] iio: light: opt3001: localize for loop iterator Joshua Crofts via B4 Relay
2026-05-11 11:09   ` Andy Shevchenko
2026-05-11 10:04 ` [PATCH 6/7] iio: light: opt3001: remove unnecessary comments Joshua Crofts via B4 Relay
2026-05-11 11:11   ` Andy Shevchenko
2026-05-11 11:14     ` Joshua Crofts
2026-05-11 10:04 ` [PATCH 7/7] iio: light: opt3001: switch driver to managed resources Joshua Crofts via B4 Relay
2026-05-11 11:13   ` Andy Shevchenko
2026-05-11 11:17     ` Joshua Crofts
2026-05-11 13:32   ` Jonathan Cameron [this message]
2026-05-11 13:37     ` Joshua Crofts

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=20260511143237.040382d8@jic23-huawei \
    --to=jic23@kernel.org \
    --cc=andy@kernel.org \
    --cc=devnull+joshua.crofts1.gmail.com@kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=joshua.crofts1@gmail.com \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nuno.sa@analog.com \
    /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