iio: accel: bma400: conversion to device-managed function
authorJagath Jog J <jagathjog1996@gmail.com>
Thu, 5 May 2022 13:30:14 +0000 (19:00 +0530)
committerJonathan Cameron <Jonathan.Cameron@huawei.com>
Sat, 11 Jun 2022 13:35:26 +0000 (14:35 +0100)
This is a conversion to device-managed by using devm_iio_device_register()
inside probe function. Previously the bma400 was not put into power down
mode in some error paths in probe where it now is, but that should cause
no harm.

The dev_set_drvdata() call, bma400_remove() function and hooks in the I2C
and SPI driver struct is removed as devm_iio_device_register() function is
used to automatically unregister on driver detach.

Signed-off-by: Jagath Jog J <jagathjog1996@gmail.com>
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Link: https://lore.kernel.org/r/20220505133021.22362-4-jagathjog1996@gmail.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
drivers/iio/accel/bma400.h
drivers/iio/accel/bma400_core.c
drivers/iio/accel/bma400_i2c.c
drivers/iio/accel/bma400_spi.c

index 80330c7ce17f4567e8a8864de8c3d6f16782d392..1c8c47a9a317cfd8a08ec5154049a1234f429f95 100644 (file)
@@ -113,6 +113,4 @@ extern const struct regmap_config bma400_regmap_config;
 
 int bma400_probe(struct device *dev, struct regmap *regmap, const char *name);
 
-void bma400_remove(struct device *dev);
-
 #endif
index 25ad1f7339bcb476087b6815be094589877efaff..07674d89d978225f8a7f7d54d2a0a562de62b329 100644 (file)
@@ -560,6 +560,26 @@ static void bma400_init_tables(void)
        }
 }
 
+static void bma400_regulators_disable(void *data_ptr)
+{
+       struct bma400_data *data = data_ptr;
+
+       regulator_bulk_disable(ARRAY_SIZE(data->regulators), data->regulators);
+}
+
+static void bma400_power_disable(void *data_ptr)
+{
+       struct bma400_data *data = data_ptr;
+       int ret;
+
+       mutex_lock(&data->mutex);
+       ret = bma400_set_power_mode(data, POWER_MODE_SLEEP);
+       mutex_unlock(&data->mutex);
+       if (ret)
+               dev_warn(data->dev, "Failed to put device into sleep mode (%pe)\n",
+                        ERR_PTR(ret));
+}
+
 static int bma400_init(struct bma400_data *data)
 {
        unsigned int val;
@@ -569,13 +589,12 @@ static int bma400_init(struct bma400_data *data)
        ret = regmap_read(data->regmap, BMA400_CHIP_ID_REG, &val);
        if (ret) {
                dev_err(data->dev, "Failed to read chip id register\n");
-               goto out;
+               return ret;
        }
 
        if (val != BMA400_ID_REG_VAL) {
                dev_err(data->dev, "Chip ID mismatch\n");
-               ret = -ENODEV;
-               goto out;
+               return -ENODEV;
        }
 
        data->regulators[BMA400_VDD_REGULATOR].supply = "vdd";
@@ -589,27 +608,31 @@ static int bma400_init(struct bma400_data *data)
                                "Failed to get regulators: %d\n",
                                ret);
 
-               goto out;
+               return ret;
        }
        ret = regulator_bulk_enable(ARRAY_SIZE(data->regulators),
                                    data->regulators);
        if (ret) {
                dev_err(data->dev, "Failed to enable regulators: %d\n",
                        ret);
-               goto out;
+               return ret;
        }
 
+       ret = devm_add_action_or_reset(data->dev, bma400_regulators_disable, data);
+       if (ret)
+               return ret;
+
        ret = bma400_get_power_mode(data);
        if (ret) {
                dev_err(data->dev, "Failed to get the initial power-mode\n");
-               goto err_reg_disable;
+               return ret;
        }
 
        if (data->power_mode != POWER_MODE_NORMAL) {
                ret = bma400_set_power_mode(data, POWER_MODE_NORMAL);
                if (ret) {
                        dev_err(data->dev, "Failed to wake up the device\n");
-                       goto err_reg_disable;
+                       return ret;
                }
                /*
                 * TODO: The datasheet waits 1500us here in the example, but
@@ -618,19 +641,23 @@ static int bma400_init(struct bma400_data *data)
                usleep_range(1500, 2000);
        }
 
+       ret = devm_add_action_or_reset(data->dev, bma400_power_disable, data);
+       if (ret)
+               return ret;
+
        bma400_init_tables();
 
        ret = bma400_get_accel_output_data_rate(data);
        if (ret)
-               goto err_reg_disable;
+               return ret;
 
        ret = bma400_get_accel_oversampling_ratio(data);
        if (ret)
-               goto err_reg_disable;
+               return ret;
 
        ret = bma400_get_accel_scale(data);
        if (ret)
-               goto err_reg_disable;
+               return ret;
 
        /*
         * Once the interrupt engine is supported we might use the
@@ -639,12 +666,6 @@ static int bma400_init(struct bma400_data *data)
         * channel.
         */
        return regmap_write(data->regmap, BMA400_ACC_CONFIG2_REG, 0x00);
-
-err_reg_disable:
-       regulator_bulk_disable(ARRAY_SIZE(data->regulators),
-                              data->regulators);
-out:
-       return ret;
 }
 
 static int bma400_read_raw(struct iio_dev *indio_dev,
@@ -822,32 +843,10 @@ int bma400_probe(struct device *dev, struct regmap *regmap, const char *name)
        indio_dev->num_channels = ARRAY_SIZE(bma400_channels);
        indio_dev->modes = INDIO_DIRECT_MODE;
 
-       dev_set_drvdata(dev, indio_dev);
-
-       return iio_device_register(indio_dev);
+       return devm_iio_device_register(dev, indio_dev);
 }
 EXPORT_SYMBOL_NS(bma400_probe, IIO_BMA400);
 
-void bma400_remove(struct device *dev)
-{
-       struct iio_dev *indio_dev = dev_get_drvdata(dev);
-       struct bma400_data *data = iio_priv(indio_dev);
-       int ret;
-
-       mutex_lock(&data->mutex);
-       ret = bma400_set_power_mode(data, POWER_MODE_SLEEP);
-       mutex_unlock(&data->mutex);
-
-       if (ret)
-               dev_warn(dev, "Failed to put device into sleep mode (%pe)\n", ERR_PTR(ret));
-
-       regulator_bulk_disable(ARRAY_SIZE(data->regulators),
-                              data->regulators);
-
-       iio_device_unregister(indio_dev);
-}
-EXPORT_SYMBOL_NS(bma400_remove, IIO_BMA400);
-
 MODULE_AUTHOR("Dan Robertson <dan@dlrobertson.com>");
 MODULE_DESCRIPTION("Bosch BMA400 triaxial acceleration sensor core");
 MODULE_LICENSE("GPL");
index da104ffd3fe076e081cad48a5d1bf27cc40cf491..4f6e01a3b3a1f19559efe39858a1d0667978abfb 100644 (file)
@@ -27,13 +27,6 @@ static int bma400_i2c_probe(struct i2c_client *client,
        return bma400_probe(&client->dev, regmap, id->name);
 }
 
-static int bma400_i2c_remove(struct i2c_client *client)
-{
-       bma400_remove(&client->dev);
-
-       return 0;
-}
-
 static const struct i2c_device_id bma400_i2c_ids[] = {
        { "bma400", 0 },
        { }
@@ -52,7 +45,6 @@ static struct i2c_driver bma400_i2c_driver = {
                .of_match_table = bma400_of_i2c_match,
        },
        .probe    = bma400_i2c_probe,
-       .remove   = bma400_i2c_remove,
        .id_table = bma400_i2c_ids,
 };
 
index 51f23bdc0ea5fcd7141b642266262a7644290682..28e240400a3fe1cca9d0d44be8f585f320f8c5bd 100644 (file)
@@ -87,11 +87,6 @@ static int bma400_spi_probe(struct spi_device *spi)
        return bma400_probe(&spi->dev, regmap, id->name);
 }
 
-static void bma400_spi_remove(struct spi_device *spi)
-{
-       bma400_remove(&spi->dev);
-}
-
 static const struct spi_device_id bma400_spi_ids[] = {
        { "bma400", 0 },
        { }
@@ -110,7 +105,6 @@ static struct spi_driver bma400_spi_driver = {
                .of_match_table = bma400_of_spi_match,
        },
        .probe    = bma400_spi_probe,
-       .remove   = bma400_spi_remove,
        .id_table = bma400_spi_ids,
 };