iio: light: tsl4531: Use device-managed cleanup in probe
PATCH v2 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.
---
Notes:
v2:
- Use a local struct device *dev in probe() (Andy)
- Link to v1: https://lore.kernel.org/linux-iio/[email protected]/
drivers/iio/light/tsl4531.c | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-)
drivers/iio/light/tsl4531.c +19 -17
@@ -160,24 +160,35 @@ 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 device *dev = &client->dev;struct tsl4531_data *data;struct iio_dev *indio_dev;int ret; - indio_dev =devm_iio_device_alloc (&client->dev,sizeof (*data)); + indio_dev =devm_iio_device_alloc (dev,sizeof (*data));if (!indio_dev)return -ENOMEM ; data =iio_priv (indio_dev); -i2c_set_clientdata (client, indio_dev); data->client = client;mutex_init (&data->lock); ret =tsl4531_check_id (client);if (ret) { -dev_err (&client->dev,"no TSL4531 sensor\n" ); +dev_err (dev,"no TSL4531 sensor\n" );return ret; }
@@ -186,6 +197,10 @@ static int tsl4531_probe(struct i2c_client *client)if (ret <0 )return ret; + ret =devm_add_action_or_reset (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 (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, };
Signature · 1 line
2.43.0