From: Aldo Conte <aldocontelk@gmail.com>
To: jic23@kernel.org
Cc: dlechner@baylibre.com, nuno.sa@analog.com, andy@kernel.org,
shuah@kernel.org, linux-iio@vger.kernel.org,
linux-kernel@vger.kernel.org,
linux-kernel-mentees@lists.linux.dev
Subject: [PATCH v2 3/5] iio: light: tcs3472: use devm for resource management
Date: Wed, 13 May 2026 00:32:13 +0200 [thread overview]
Message-ID: <20260512223215.25596-4-aldocontelk@gmail.com> (raw)
In-Reply-To: <20260512223215.25596-1-aldocontelk@gmail.com>
Convert the driver to use device-managed resource allocation:
- Add tcs3472_powerdown_action() and register it with
devm_add_action_or_reset() to ensure the device is powered down on
cleanup. Before this patch, the chip remained powered if probe
failed after enabling it.
- Replace iio_triggered_buffer_setup() with
devm_iio_triggered_buffer_setup().
- Replace request_threaded_irq() with devm_request_threaded_irq().
- Replace iio_device_register() with devm_iio_device_register().
- Remove tcs3472_remove() as all cleanup is now handled by devm.
Rewrite the read-modify-write pattern in tcs3472_powerdown() and
tcs3472_resume() so the new register value is computed first, written
to the chip, and committed to data->enable only on success.
Use a local 'dev = &client->dev' in tcs3472_probe() to keep the devm
calls compact.
Suggested-by: Andy Shevchenko <andy@kernel.org>
Signed-off-by: Aldo Conte <aldocontelk@gmail.com>
---
v2:
(Suggested by Andy)
- Rewrote read-modify-write in tcs3472_powerdown() and tcs3472_resume()
- Use local 'struct device *dev = &client->dev' in probe.
- Dropped "Compiled with W=1" and test details from the commit message.
drivers/iio/light/tcs3472.c | 107 +++++++++++++++++-------------------
1 file changed, 51 insertions(+), 56 deletions(-)
diff --git a/drivers/iio/light/tcs3472.c b/drivers/iio/light/tcs3472.c
index 4fd4fd74d0d6..7a6dc8360326 100644
--- a/drivers/iio/light/tcs3472.c
+++ b/drivers/iio/light/tcs3472.c
@@ -427,13 +427,38 @@ static const struct iio_info tcs3472_info = {
.attrs = &tcs3472_attribute_group,
};
+static int tcs3472_powerdown(struct tcs3472_data *data)
+{
+ u8 enable_mask = TCS3472_ENABLE_AEN | TCS3472_ENABLE_PON;
+ u8 value;
+ int ret;
+
+ guard(mutex)(&data->lock);
+
+ value = data->enable & ~enable_mask;
+
+ ret = i2c_smbus_write_byte_data(data->client, TCS3472_ENABLE, value);
+ if (ret)
+ return ret;
+
+ data->enable = value;
+
+ return 0;
+}
+
+static void tcs3472_powerdown_action(void *data)
+{
+ tcs3472_powerdown(data);
+}
+
static int tcs3472_probe(struct i2c_client *client)
{
+ struct device *dev = &client->dev;
struct tcs3472_data *data;
struct iio_dev *indio_dev;
int ret;
- indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
+ indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
if (indio_dev == NULL)
return -ENOMEM;
@@ -453,9 +478,9 @@ static int tcs3472_probe(struct i2c_client *client)
return ret;
if (ret == 0x44)
- dev_info(&client->dev, "TCS34721/34725 found\n");
+ dev_info(dev, "TCS34721/34725 found\n");
else if (ret == 0x4d)
- dev_info(&client->dev, "TCS34723/34727 found\n");
+ dev_info(dev, "TCS34723/34727 found\n");
else
return -ENODEV;
@@ -497,59 +522,26 @@ static int tcs3472_probe(struct i2c_client *client)
if (ret < 0)
return ret;
- ret = iio_triggered_buffer_setup(indio_dev, NULL,
- tcs3472_trigger_handler, NULL);
+ ret = devm_add_action_or_reset(dev, tcs3472_powerdown_action, data);
+ if (ret)
+ return ret;
+
+ ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
+ tcs3472_trigger_handler, NULL);
if (ret < 0)
return ret;
if (client->irq) {
- ret = request_threaded_irq(client->irq, NULL,
- tcs3472_event_handler,
- IRQF_TRIGGER_FALLING | IRQF_SHARED |
- IRQF_ONESHOT,
- client->name, indio_dev);
+ ret = devm_request_threaded_irq(dev, client->irq, NULL,
+ tcs3472_event_handler,
+ IRQF_TRIGGER_FALLING | IRQF_SHARED |
+ IRQF_ONESHOT,
+ client->name, indio_dev);
if (ret)
- goto buffer_cleanup;
+ return ret;
}
- ret = iio_device_register(indio_dev);
- if (ret < 0)
- goto free_irq;
-
- return 0;
-
-free_irq:
- if (client->irq)
- free_irq(client->irq, indio_dev);
-buffer_cleanup:
- iio_triggered_buffer_cleanup(indio_dev);
- return ret;
-}
-
-static int tcs3472_powerdown(struct tcs3472_data *data)
-{
- int ret;
- u8 enable_mask = TCS3472_ENABLE_AEN | TCS3472_ENABLE_PON;
-
- guard(mutex)(&data->lock);
-
- ret = i2c_smbus_write_byte_data(data->client, TCS3472_ENABLE,
- data->enable & ~enable_mask);
- if (!ret)
- data->enable &= ~enable_mask;
-
- return ret;
-}
-
-static void tcs3472_remove(struct i2c_client *client)
-{
- struct iio_dev *indio_dev = i2c_get_clientdata(client);
-
- iio_device_unregister(indio_dev);
- if (client->irq)
- free_irq(client->irq, indio_dev);
- iio_triggered_buffer_cleanup(indio_dev);
- tcs3472_powerdown(iio_priv(indio_dev));
+ return devm_iio_device_register(dev, indio_dev);
}
static int tcs3472_suspend(struct device *dev)
@@ -563,17 +555,21 @@ static int tcs3472_resume(struct device *dev)
{
struct tcs3472_data *data = iio_priv(i2c_get_clientdata(
to_i2c_client(dev)));
- int ret;
u8 enable_mask = TCS3472_ENABLE_AEN | TCS3472_ENABLE_PON;
+ u8 value;
+ int ret;
guard(mutex)(&data->lock);
- ret = i2c_smbus_write_byte_data(data->client, TCS3472_ENABLE,
- data->enable | enable_mask);
- if (!ret)
- data->enable |= enable_mask;
+ value = data->enable | enable_mask;
- return ret;
+ ret = i2c_smbus_write_byte_data(data->client, TCS3472_ENABLE, value);
+ if (ret)
+ return ret;
+
+ data->enable = value;
+
+ return 0;
}
static DEFINE_SIMPLE_DEV_PM_OPS(tcs3472_pm_ops, tcs3472_suspend,
@@ -591,7 +587,6 @@ static struct i2c_driver tcs3472_driver = {
.pm = pm_sleep_ptr(&tcs3472_pm_ops),
},
.probe = tcs3472_probe,
- .remove = tcs3472_remove,
.id_table = tcs3472_id,
};
module_i2c_driver(tcs3472_driver);
--
2.54.0
next prev parent reply other threads:[~2026-05-12 22:32 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-12 22:32 [PATCH v2 0/5] devm conversion, wait time, locking cleanup Aldo Conte
2026-05-12 22:32 ` [PATCH v2 1/5] iio: light: tcs3472: sort headers alphabetically Aldo Conte
2026-05-13 8:15 ` Joshua Crofts
2026-05-12 22:32 ` [PATCH v2 2/5] iio: light: tcs3472: convert remaining locking to guard(mutex) Aldo Conte
2026-05-13 7:47 ` Joshua Crofts
2026-05-13 11:00 ` Andy Shevchenko
2026-05-13 10:58 ` Andy Shevchenko
2026-05-12 22:32 ` Aldo Conte [this message]
2026-05-13 8:07 ` [PATCH v2 3/5] iio: light: tcs3472: use devm for resource management Joshua Crofts
2026-05-13 11:02 ` Andy Shevchenko
2026-05-12 22:32 ` [PATCH v2 4/5] iio: light: tcs3472: implement wait time and sampling frequency Aldo Conte
2026-05-13 11:17 ` Andy Shevchenko
2026-05-12 22:32 ` [PATCH v2 5/5] iio: light: tcs3472: move standalone return to default case Aldo Conte
2026-05-13 8:16 ` Joshua Crofts
2026-05-13 11:23 ` Andy Shevchenko
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=20260512223215.25596-4-aldocontelk@gmail.com \
--to=aldocontelk@gmail.com \
--cc=andy@kernel.org \
--cc=dlechner@baylibre.com \
--cc=jic23@kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel-mentees@lists.linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=nuno.sa@analog.com \
--cc=shuah@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