linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4] iio: light: ltr390: Add device powerdown functionality via devm api
@ 2025-08-14  3:45 Akshay Jindal
  2025-08-16 12:57 ` Jonathan Cameron
  0 siblings, 1 reply; 2+ messages in thread
From: Akshay Jindal @ 2025-08-14  3:45 UTC (permalink / raw)
  To: anshulusr, jic23, dlechner, nuno.sa, andy
  Cc: Akshay Jindal, shuah, linux-iio, linux-kernel

Use devm_add_action_or_reset() to do cleanup when the device is removed.

Signed-off-by: Akshay Jindal <akshayaj.lkd@gmail.com>
---

Changes Since v3:
-----------------
- Removed call i2c_set_clientdata().
- Modified changelog accordingly.

Changes Since v2:
-----------------
- Replace i2c_client with ltr390_data as action parameter of devm_add_action_or_reset().
- Place i2c_set_clientdata above data population line.
- Modify changelog accordingly.

Changes Since v1:
-----------------
- Switch from remove callback to devm_* API for powerdown.
- Modify changelog and summary accordingly.

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 | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/light/ltr390.c b/drivers/iio/light/ltr390.c
index 7733830dca67..2e1cf62e8201 100644
--- a/drivers/iio/light/ltr390.c
+++ b/drivers/iio/light/ltr390.c
@@ -680,6 +680,22 @@ static irqreturn_t ltr390_interrupt_handler(int irq, void *private)
 	return IRQ_HANDLED;
 }
 
+static void ltr390_powerdown(void *priv)
+{
+	struct ltr390_data *data = priv;
+
+	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(&data->client->dev, "failed to disable interrupts\n");
+
+	if (regmap_clear_bits(data->regmap, LTR390_MAIN_CTRL,
+			LTR390_SENSOR_ENABLE) < 0)
+		dev_err(&data->client->dev, "failed to disable sensor\n");
+}
+
 static int ltr390_probe(struct i2c_client *client)
 {
 	struct ltr390_data *data;
@@ -693,7 +709,6 @@ static int ltr390_probe(struct i2c_client *client)
 		return -ENOMEM;
 
 	data = iio_priv(indio_dev);
-
 	data->regmap = devm_regmap_init_i2c(client, &ltr390_regmap_config);
 	if (IS_ERR(data->regmap))
 		return dev_err_probe(dev, PTR_ERR(data->regmap),
@@ -733,6 +748,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, data);
+	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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v4] iio: light: ltr390: Add device powerdown functionality via devm api
  2025-08-14  3:45 [PATCH v4] iio: light: ltr390: Add device powerdown functionality via devm api Akshay Jindal
@ 2025-08-16 12:57 ` Jonathan Cameron
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Cameron @ 2025-08-16 12:57 UTC (permalink / raw)
  To: Akshay Jindal
  Cc: anshulusr, dlechner, nuno.sa, andy, shuah, linux-iio,
	linux-kernel

On Thu, 14 Aug 2025 09:15:43 +0530
Akshay Jindal <akshayaj.lkd@gmail.com> wrote:

> Use devm_add_action_or_reset() to do cleanup when the device is removed.
> 
> Signed-off-by: Akshay Jindal <akshayaj.lkd@gmail.com>
LGTM and I think you've addressed Andy's remaining feedback.

Applied to the togreg branch of iio.git and pushed out for testing.

Thanks,

Jonathan

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-08-16 12:57 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-14  3:45 [PATCH v4] iio: light: ltr390: Add device powerdown functionality via devm api Akshay Jindal
2025-08-16 12:57 ` Jonathan Cameron

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).