Using devm callbacks helps to make the ordering of probe / remove operations easier to reason about and removes some duplicate code between the probe error path and driver remove. ---
Notes: Changes in v4: - also replace mutex_init with devm_mutex_init Added in v3 drivers/iio/light/stk3310.c | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/drivers/iio/light/stk3310.c b/drivers/iio/light/stk3310.c index ed20b6714546..181b7acb3f96 100644 --- a/drivers/iio/light/stk3310.c +++ b/drivers/iio/light/stk3310.c @@ -484,6 +484,17 @@ static int stk3310_set_state(struct stk3310_data *data, u8 state) return ret; } +static void stk3310_set_state_disable(void *private) +{ + int ret; + struct stk3310_data *data = private; + struct device *dev = &data->client->dev; + + ret = stk3310_set_state(data, STK3310_STATE_STANDBY); + if (ret) + dev_err(dev, "failed to set state to standby: %d\n", ret); +} + static int stk3310_init(struct iio_dev *indio_dev) { int ret; @@ -491,6 +502,7 @@ static int stk3310_init(struct iio_dev *indio_dev) u8 state; struct stk3310_data *data = iio_priv(indio_dev); struct i2c_client *client = data->client; + struct device *dev = &client->dev; ret = regmap_read(data->regmap, STK3310_REG_ID, &chipid); if (ret < 0) @@ -507,6 +519,10 @@ static int stk3310_init(struct iio_dev *indio_dev) return ret; } + ret = devm_add_action_or_reset(&client->dev, stk3310_set_state_disable, data); + if (ret) + return dev_err_probe(dev, ret, "failed to register cleanup function\n"); + /* Enable PS interrupts */ ret = regmap_field_write(data->reg_int_ps, STK3310_PSINT_EN); if (ret < 0) @@ -624,7 +640,7 @@ static int stk3310_probe(struct i2c_client *client) device_property_read_u32(&client->dev, "proximity-near-level", &data->ps_near_level); - mutex_init(&data->lock); + devm_mutex_init(&client->dev, &data->lock); ret = stk3310_regmap_init(data); if (ret < 0) @@ -650,29 +666,17 @@ static int stk3310_probe(struct i2c_client *client) if (ret < 0) { dev_err(&client->dev, "request irq %d failed\n", client->irq); - goto err_standby; + return ret; } } - ret = iio_device_register(indio_dev); + ret = devm_iio_device_register(&client->dev, indio_dev); if (ret < 0) { dev_err(&client->dev, "device_register failed\n"); - goto err_standby; + return ret; } return 0; - -err_standby: - stk3310_set_state(data, STK3310_STATE_STANDBY); - return ret; -} - -static void stk3310_remove(struct i2c_client *client) -{ - struct iio_dev *indio_dev = i2c_get_clientdata(client); - - iio_device_unregister(indio_dev); - stk3310_set_state(iio_priv(indio_dev), STK3310_STATE_STANDBY); } static int stk3310_suspend(struct device *dev) @@ -736,7 +740,6 @@ static struct i2c_driver stk3310_driver = { .acpi_match_table = stk3310_acpi_id, }, .probe = stk3310_probe, - .remove = stk3310_remove, .id_table = stk3310_i2c_id, }; -- 2.47.0