From: Akshay Jindal <akshayaj.lkd@gmail.com>
To: anshulusr@gmail.com, jic23@kernel.org, dlechner@baylibre.com,
nuno.sa@analog.com, andy@kernel.org
Cc: Akshay Jindal <akshayaj.lkd@gmail.com>,
shuah@kernel.org, linux-iio@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: [PATCH v2] iio: light: ltr390: Add device powerdown functionality via devm api
Date: Tue, 12 Aug 2025 22:37:18 +0530 [thread overview]
Message-ID: <20250812170723.51639-1-akshayaj.lkd@gmail.com> (raw)
Use devm_add_action_or_reset to do cleanup when the device is removed.
Set client data with i2c_set_clientdata() to ensure indio_dev is accessible
in powerdown function.
Signed-off-by: Akshay Jindal <akshayaj.lkd@gmail.com>
---
Testing details:
================
-> Tested on Raspberrypi 4B. Following tests were performed.
1. Sensor and interrupts should be disabled after module unload.
-> Before unload
akshayaj@raspberrypi:~ $ echo 1 | sudo tee /sys/bus/iio/devices/iio\:device0/events/in_illuminance_thresh_either_en
1
akshayaj@raspberrypi:~ $ cat /sys/bus/iio/devices/iio\:device0/events/in_illuminance_thresh_either_en
1
akshayaj@raspberrypi:~ $ i2cget -f -y 1 0x53 0x19
0x14
akshayaj@raspberrypi:~ $ i2cget -f -y 1 0x53 0x0
0x02
-> After unload
akshayaj@raspberrypi:~ $ sudo rmmod ltr390
akshayaj@raspberrypi:~ $ i2cget -f -y 1 0x53 0x0
0x00
akshayaj@raspberrypi:~ $ i2cget -f -y 1 0x53 0x19
0x10
drivers/iio/light/ltr390.c | 24 +++++++++++++++++++++++-
1 file changed, 23 insertions(+), 1 deletion(-)
diff --git a/drivers/iio/light/ltr390.c b/drivers/iio/light/ltr390.c
index 7733830dca67..f9c8e7fc57fd 100644
--- a/drivers/iio/light/ltr390.c
+++ b/drivers/iio/light/ltr390.c
@@ -680,6 +680,24 @@ static irqreturn_t ltr390_interrupt_handler(int irq, void *private)
return IRQ_HANDLED;
}
+static void ltr390_powerdown(void *client)
+{
+ struct i2c_client *i2c_clnt = client;
+ struct iio_dev *indio_dev = i2c_get_clientdata(i2c_clnt);
+ struct ltr390_data *data = iio_priv(indio_dev);
+
+ guard(mutex)(&data->lock);
+
+ /* Ensure that power off and interrupts are disabled */
+ if (regmap_clear_bits(data->regmap, LTR390_INT_CFG,
+ LTR390_LS_INT_EN) < 0)
+ dev_err(&i2c_clnt->dev, "failed to disable interrupts\n");
+
+ if (regmap_clear_bits(data->regmap, LTR390_MAIN_CTRL,
+ LTR390_SENSOR_ENABLE) < 0)
+ dev_err(&i2c_clnt->dev, "failed to disable sensor\n");
+}
+
static int ltr390_probe(struct i2c_client *client)
{
struct ltr390_data *data;
@@ -693,7 +711,7 @@ static int ltr390_probe(struct i2c_client *client)
return -ENOMEM;
data = iio_priv(indio_dev);
-
+ i2c_set_clientdata(client, indio_dev);
data->regmap = devm_regmap_init_i2c(client, <r390_regmap_config);
if (IS_ERR(data->regmap))
return dev_err_probe(dev, PTR_ERR(data->regmap),
@@ -733,6 +751,10 @@ static int ltr390_probe(struct i2c_client *client)
if (ret)
return dev_err_probe(dev, ret, "failed to enable the sensor\n");
+ ret = devm_add_action_or_reset(dev, ltr390_powerdown, client);
+ if (ret)
+ return dev_err_probe(dev, ret, "failed to add action or reset\n");
+
if (client->irq) {
ret = devm_request_threaded_irq(dev, client->irq,
NULL, ltr390_interrupt_handler,
--
2.43.0
next reply other threads:[~2025-08-12 17:07 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-12 17:07 Akshay Jindal [this message]
2025-08-12 17:13 ` Akshay Jindal
2025-08-13 10:13 ` Andy Shevchenko
2025-08-13 13:35 ` Akshay Jindal
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=20250812170723.51639-1-akshayaj.lkd@gmail.com \
--to=akshayaj.lkd@gmail.com \
--cc=andy@kernel.org \
--cc=anshulusr@gmail.com \
--cc=dlechner@baylibre.com \
--cc=jic23@kernel.org \
--cc=linux-iio@vger.kernel.org \
--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
all inboxes | Powered by JetHome®