iio: light: ltr390: Add device powerdown functionality via devm api
Subsystems (2)
PATCH v2 1/1 iio: light: ltr390: Add device powerdown functionality via devm api
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.
---
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 0x190x14
akshayaj@raspberrypi:~ $ i2cget -f -y 1 0x53 0x00x02
-> After unload
akshayaj@raspberrypi:~ $ sudo rmmod ltr390
akshayaj@raspberrypi:~ $ i2cget -f -y 1 0x53 0x00x00
akshayaj@raspberrypi:~ $ i2cget -f -y 1 0x53 0x190x10
drivers/iio/light/ltr390.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-)
drivers/iio/light/ltr390.c +23 -1
@@ -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,
Signature · 1 line
2.43.0
Quoted 7 lines · Akshay Jindal
Use
devm_add_action_or_resetto do cleanup when the device is removed. Set client data withi2c_set_clientdata()to ensureindio_devis accessible in powerdown function.Signed-off-by Akshay Jindal <[email protected]>---
Just realized I did not include a 'Changes since v1' section.
Changes since v1:
-------------------------
- Switched from remove callback to devm_* API for powerdown.
- Modified the changelog accordingly and summary accordingly.
Inorder to avoid redundancy, I am replying on the patch mail. I am happy to RESEND the patch with updated Changes since v1 section.
Thanks,
Akshay
Quoted 2 lines · Akshay Jindal
Use
devm_add_action_or_resetto do cleanup when the device is removed.
devm_add_action_or_reset()
Quoted 2 lines
Set client data with
i2c_set_clientdata()to ensureindio_devis accessible in powerdown function.
...
drivers/iio/light/ltr390.c static irqreturn_t ltr390_interrupt_handler(int irq, void *private) · line 686
Quoted 4 lines
drivers/iio/light/ltr390.c +4
-0+static void ltr390_powerdown (void *client) +{ +struct i2c_client *i2c_clnt = client; +struct iio_dev *indio_dev =i2c_get_clientdata (i2c_clnt);
Why? I mean why can't you simply provide a data pointer directly as a parameter to this function?
Quoted 13 lines
drivers/iio/light/ltr390.c +13
-0+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" ); +}
...
drivers/iio/light/ltr390.c static int ltr390_probe(struct i2c_client *client) · line 697
Quoted 6 lines
drivers/iio/light/ltr390.c +0
-1@@ -693,7 +711,7 @@ static int ltr390_probe(struct i2c_client *client)return -ENOMEM ; data =iio_priv (indio_dev); -
Stray change.
drivers/iio/light/ltr390.c static int ltr390_probe(struct i2c_client *client) · line 714
Quoted 1 line
drivers/iio/light/ltr390.c +1
-0+i2c_set_clientdata (client, indio_dev);
I would suggest moving this to be before data = ... line above. And add a blank line after it. BUT, read above first.
Quoted 3 lines
drivers/iio/light/ltr390.c +0
-0data->regmap =devm_regmap_init_i2c (client, <r390_regmap_config);if (IS_ERR (data->regmap))return dev_err_probe(dev, PTR_ERR(data->regmap),
Signature · 2 lines
With Best Regards, Andy Shevchenko
Sent a v3 for review. Acknowledged your comments inline.
Quoted 7 lines · Andy Shevchenko
Quoted 2 lines · Akshay Jindal
Use
devm_add_action_or_resetto do cleanup when the device is removed.
devm_add_action_or_reset()
done
Quoted 8 lines
drivers/iio/light/ltr390.c static irqreturn_t ltr390_interrupt_handler(int irq, void *private) · line 686
Quoted 4 lines
drivers/iio/light/ltr390.c +4
-0+static void ltr390_powerdown (void *client) +{ +struct i2c_client *i2c_clnt = client; +struct iio_dev *indio_dev =i2c_get_clientdata (i2c_clnt);Why? I mean why can't you simply provide a data pointer directly as a parameter to this function?
yeah that is much better. Done.
Quoted 4 lines
Quoted 4 lines
drivers/iio/light/ltr390.c +0
-0@@ -693,7 +711,7 @@ static int ltr390_probe(struct i2c_client *client)return -ENOMEM ;
Quoted 8 lines
drivers/iio/light/ltr390.c static int ltr390_probe(struct i2c_client *client) · line 713
Quoted 1 line
drivers/iio/light/ltr390.c +0
-0data =iio_priv (indio_dev);Stray change.
drivers/iio/light/ltr390.c static int ltr390_probe(struct i2c_client *client) · line 714
Quoted 1 line
drivers/iio/light/ltr390.c +1
-0+i2c_set_clientdata (client, indio_dev);I would suggest moving this to be before data = ... line above. And add a blank line after it. BUT, read above first.
Done.