iio: light: tsl4531: Use device-managed cleanup in probe
PATCH 1/1 iio: light: tsl4531: Use device-managed cleanup in probe
The sensor is switched to normal mode during probe, but if a later step fails (writing the config register or registering the IIO device), probe returns an error and the sensor is left powered on, because remove() is not called for a failed probe.
Register a devm action that powers down the sensor right after it is enabled, and use devm_iio_device_register(). This also lets the driver drop its remove() callback and the now unused i2c_set_clientdata() call, while keeping the same teardown order: unregister first, then power down.
Compile-tested only; no hardware available.
--- drivers/iio/light/tsl4531.c | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-)
drivers/iio/light/tsl4531.c +17 -15
@@ -160,6 +160,17 @@ static int tsl4531_check_id(struct i2c_client *client) } } +static int tsl4531_powerdown (struct i2c_client *client) +{ +return i2c_smbus_write_byte_data (client,TSL4531_CONTROL , +TSL4531_MODE_POWERDOWN ); +} + +static void tsl4531_powerdown_action (void *client) +{ +tsl4531_powerdown (client); +} +static int tsl4531_probe (struct i2c_client *client) {struct tsl4531_data *data;
@@ -171,7 +182,6 @@ static int tsl4531_probe(struct i2c_client *client)return -ENOMEM ; data =iio_priv (indio_dev); -i2c_set_clientdata (client, indio_dev); data->client = client;mutex_init (&data->lock);
@@ -186,6 +196,11 @@ static int tsl4531_probe(struct i2c_client *client)if (ret <0 )return ret; + ret =devm_add_action_or_reset (&client->dev, tsl4531_powerdown_action, + client); +if (ret) +return ret; + ret =i2c_smbus_write_byte_data (data->client,TSL4531_CONFIG ,TSL4531_TCNTRL_400MS );if (ret <0 )
@@ -197,19 +212,7 @@ static int tsl4531_probe(struct i2c_client *client) indio_dev->name =TSL4531_DRV_NAME ; indio_dev->modes =INDIO_DIRECT_MODE ; -return iio_device_register (indio_dev); -} - -static int tsl4531_powerdown (struct i2c_client *client) -{ -return i2c_smbus_write_byte_data (client,TSL4531_CONTROL , -TSL4531_MODE_POWERDOWN ); -} - -static void tsl4531_remove (struct i2c_client *client) -{ -iio_device_unregister (i2c_get_clientdata (client)); -tsl4531_powerdown (client); +return devm_iio_device_register (&client->dev, indio_dev); }static int tsl4531_suspend (struct device *dev)
@@ -238,7 +241,6 @@ static struct i2c_driver tsl4531_driver = { .pm = pm_sleep_ptr(&tsl4531_pm_ops), }, .probe = tsl4531_probe, - .remove = tsl4531_remove, .id_table = tsl4531_id, };
base-commit: e5aad2bdbbee783611d32599f89cea3f12f9593d
Signature · 1 line
2.43.0
Quoted 4 lines · Vineet Thakran
The sensor is switched to normal mode during probe, but if a later step fails (writing the config register or registering the IIO device), probe returns an error and the sensor is left powered on, because
remove()is not called for a failed probe.
Quoted 11 lines · Vineet Thakran
The sensor is switched to normal mode during probe, but if a later step fails (writing the config register or registering the IIO device), probe returns an error and the sensor is left powered on, because
remove()is not called for a failed probe.Register a devm action that powers down the sensor right after it is enabled, and use
devm_iio_device_register(). This also lets the driver drop itsremove()callback and the now unusedi2c_set_clientdata()call, while keeping the same teardown order: unregister first, then power down.Compile-tested only; no hardware available.
...
drivers/iio/light/tsl4531.c static int tsl4531_probe(struct i2c_client *client) · line 200
Quoted 2 lines
drivers/iio/light/tsl4531.c +2
-0+ ret =devm_add_action_or_reset (&client->dev, tsl4531_powerdown_action, + client);
With
struct device *dev = &client->dev;
at the top of the function this becomes
ret = devm_add_action_or_reset(dev, tsl4531_powerdown_action, client);
Quoted 2 lines
drivers/iio/light/tsl4531.c +2
-0+if (ret) +return ret;
...
drivers/iio/light/tsl4531.c static int tsl4531_probe(struct i2c_client *client) · line 215
Quoted 1 line
drivers/iio/light/tsl4531.c +1
-0+return devm_iio_device_register (&client->dev, indio_dev);
And this respectively
return devm_iio_device_register(dev, indio_dev);
Signature · 2 lines
With Best Regards, Andy Shevchenko