linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] iio: light: ltr390: Add remove callback with needed support in device registration
@ 2025-08-04 19:25 Akshay Jindal
  2025-08-04 21:05 ` Andy Shevchenko
  0 siblings, 1 reply; 15+ messages in thread
From: Akshay Jindal @ 2025-08-04 19:25 UTC (permalink / raw)
  To: anshulusr, jic23, dlechner, nuno.sa, andy
  Cc: Akshay Jindal, shuah, linux-iio, linux-kernel

Implement the .remove() callback in the ltr390 driver to ensure proper
cleanup when the device is removed.

Set client data with i2c_set_clientdata() to ensure indio_dev is accessible
in .remove(). Replace devm_iio_device_register() with iio_device_register()
and explicitly unregister the device to align with the updated removal path
and follow common patterns used in other IIO drivers.

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, 22 insertions(+), 2 deletions(-)

diff --git a/drivers/iio/light/ltr390.c b/drivers/iio/light/ltr390.c
index 7733830dca67..af1b04c8524e 100644
--- a/drivers/iio/light/ltr390.c
+++ b/drivers/iio/light/ltr390.c
@@ -693,7 +693,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, &ltr390_regmap_config);
 	if (IS_ERR(data->regmap))
 		return dev_err_probe(dev, PTR_ERR(data->regmap),
@@ -744,7 +744,26 @@ static int ltr390_probe(struct i2c_client *client)
 					     "request irq (%d) failed\n", client->irq);
 	}
 
-	return devm_iio_device_register(dev, indio_dev);
+	return iio_device_register(indio_dev);
+}
+
+static void ltr390_remove(struct i2c_client *client)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(client);
+	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(&client->dev, "failed to disable interrupts\n");
+
+	if (regmap_clear_bits(data->regmap, LTR390_MAIN_CTRL,
+			LTR390_SENSOR_ENABLE) < 0)
+		dev_err(&client->dev, "failed to disable sensor\n");
+
+	iio_device_unregister(indio_dev);
 }
 
 static int ltr390_suspend(struct device *dev)
@@ -786,6 +805,7 @@ static struct i2c_driver ltr390_driver = {
 		.pm = pm_sleep_ptr(&ltr390_pm_ops),
 	},
 	.probe = ltr390_probe,
+	.remove = ltr390_remove,
 	.id_table = ltr390_id,
 };
 module_i2c_driver(ltr390_driver);
-- 
2.43.0


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

end of thread, other threads:[~2025-08-11 20:25 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-04 19:25 [PATCH] iio: light: ltr390: Add remove callback with needed support in device registration Akshay Jindal
2025-08-04 21:05 ` Andy Shevchenko
2025-08-05  4:05   ` Akshay Jindal
2025-08-05 12:47     ` Andy Shevchenko
2025-08-06 15:18       ` Jonathan Cameron
2025-08-06 20:02         ` Andy Shevchenko
2025-08-07 13:04           ` Jonathan Cameron
2025-08-09  9:53             ` Andy Shevchenko
2025-08-09 19:57               ` Jonathan Cameron
2025-08-09 20:34                 ` Andy Shevchenko
2025-08-10 20:48                   ` Akshay Jindal
2025-08-11 20:25                     ` Jonathan Cameron
2025-08-08 14:23       ` Akshay Jindal
2025-08-08 14:33         ` Andy Shevchenko
2025-08-08 14:55           ` Akshay Jindal

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