iio: dac: ad5686: use devm_regulator_get_enable_read_voltage()
authorDavid Lechner <dlechner@baylibre.com>
Wed, 20 Nov 2024 21:33:26 +0000 (15:33 -0600)
committerJonathan Cameron <Jonathan.Cameron@huawei.com>
Sat, 7 Dec 2024 17:47:35 +0000 (17:47 +0000)
Simplify the code by using devm_regulator_get_enable_read_voltage().

There is a small change in behavior. Before, all errors from
devm_regulator_get_optional() were ignored and assumed to mean that
the external reference supply was absent. Now, only -ENODEV is checked
and other errors will cause a failure to probe. So now, this will
catch errors, like using the wrong data type for the devicetree
property.

Signed-off-by: David Lechner <dlechner@baylibre.com>
Link: https://patch.msgid.link/20241120-iio-regulator-cleanup-round-6-v1-3-d5a5360f7ec3@baylibre.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
drivers/iio/dac/ad5686-spi.c
drivers/iio/dac/ad5686.c
drivers/iio/dac/ad5686.h
drivers/iio/dac/ad5696-i2c.c

index 39b5dad0d6a5ee9abae9dcefcd90a2c512192f3a..9c727aa6ea189a7ed6353392bcb73e113e5d5bdb 100644 (file)
@@ -95,11 +95,6 @@ static int ad5686_spi_probe(struct spi_device *spi)
                            ad5686_spi_write, ad5686_spi_read);
 }
 
-static void ad5686_spi_remove(struct spi_device *spi)
-{
-       ad5686_remove(&spi->dev);
-}
-
 static const struct spi_device_id ad5686_spi_id[] = {
        {"ad5310r", ID_AD5310R},
        {"ad5672r", ID_AD5672R},
@@ -126,7 +121,6 @@ static struct spi_driver ad5686_spi_driver = {
                .name = "ad5686",
        },
        .probe = ad5686_spi_probe,
-       .remove = ad5686_spi_remove,
        .id_table = ad5686_spi_id,
 };
 
index 8dc578b087845a8473cab8bf38ab2349b6590efb..763af690c444399a620fd0e55c1e916b1beb8b39 100644 (file)
@@ -455,39 +455,28 @@ int ad5686_probe(struct device *dev,
        struct ad5686_state *st;
        struct iio_dev *indio_dev;
        unsigned int val, ref_bit_msk;
+       bool has_external_vref;
        u8 cmd;
-       int ret, i, voltage_uv = 0;
+       int ret, i;
 
        indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
        if (indio_dev == NULL)
                return  -ENOMEM;
 
        st = iio_priv(indio_dev);
-       dev_set_drvdata(dev, indio_dev);
 
        st->dev = dev;
        st->write = write;
        st->read = read;
 
-       st->reg = devm_regulator_get_optional(dev, "vcc");
-       if (!IS_ERR(st->reg)) {
-               ret = regulator_enable(st->reg);
-               if (ret)
-                       return ret;
-
-               ret = regulator_get_voltage(st->reg);
-               if (ret < 0)
-                       goto error_disable_reg;
-
-               voltage_uv = ret;
-       }
-
        st->chip_info = &ad5686_chip_info_tbl[chip_type];
 
-       if (voltage_uv)
-               st->vref_mv = voltage_uv / 1000;
-       else
-               st->vref_mv = st->chip_info->int_vref_mv;
+       ret = devm_regulator_get_enable_read_voltage(dev, "vcc");
+       if (ret < 0 && ret != -ENODEV)
+               return ret;
+
+       has_external_vref = ret != -ENODEV;
+       st->vref_mv = has_external_vref ? ret / 1000 : st->chip_info->int_vref_mv;
 
        /* Set all the power down mode for all channels to 1K pulldown */
        for (i = 0; i < st->chip_info->num_channels; i++)
@@ -505,12 +494,12 @@ int ad5686_probe(struct device *dev,
        case AD5310_REGMAP:
                cmd = AD5686_CMD_CONTROL_REG;
                ref_bit_msk = AD5310_REF_BIT_MSK;
-               st->use_internal_vref = !voltage_uv;
+               st->use_internal_vref = !has_external_vref;
                break;
        case AD5683_REGMAP:
                cmd = AD5686_CMD_CONTROL_REG;
                ref_bit_msk = AD5683_REF_BIT_MSK;
-               st->use_internal_vref = !voltage_uv;
+               st->use_internal_vref = !has_external_vref;
                break;
        case AD5686_REGMAP:
                cmd = AD5686_CMD_INTERNAL_REFER_SETUP;
@@ -519,43 +508,22 @@ int ad5686_probe(struct device *dev,
        case AD5693_REGMAP:
                cmd = AD5686_CMD_CONTROL_REG;
                ref_bit_msk = AD5693_REF_BIT_MSK;
-               st->use_internal_vref = !voltage_uv;
+               st->use_internal_vref = !has_external_vref;
                break;
        default:
-               ret = -EINVAL;
-               goto error_disable_reg;
+               return -EINVAL;
        }
 
-       val = (voltage_uv | ref_bit_msk);
+       val = (has_external_vref | ref_bit_msk);
 
        ret = st->write(st, cmd, 0, !!val);
        if (ret)
-               goto error_disable_reg;
-
-       ret = iio_device_register(indio_dev);
-       if (ret)
-               goto error_disable_reg;
-
-       return 0;
+               return ret;
 
-error_disable_reg:
-       if (!IS_ERR(st->reg))
-               regulator_disable(st->reg);
-       return ret;
+       return devm_iio_device_register(dev, indio_dev);
 }
 EXPORT_SYMBOL_NS_GPL(ad5686_probe, "IIO_AD5686");
 
-void ad5686_remove(struct device *dev)
-{
-       struct iio_dev *indio_dev = dev_get_drvdata(dev);
-       struct ad5686_state *st = iio_priv(indio_dev);
-
-       iio_device_unregister(indio_dev);
-       if (!IS_ERR(st->reg))
-               regulator_disable(st->reg);
-}
-EXPORT_SYMBOL_NS_GPL(ad5686_remove, "IIO_AD5686");
-
 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
 MODULE_DESCRIPTION("Analog Devices AD5686/85/84 DAC");
 MODULE_LICENSE("GPL v2");
index 5b150f344fdacbb2c569f888a5c6af4b48a4bd4d..e7d36bae3e59336816a68bcf81cb1726f79df13a 100644 (file)
@@ -118,7 +118,6 @@ struct ad5686_chip_info {
  * struct ad5686_state - driver instance specific data
  * @spi:               spi_device
  * @chip_info:         chip model specific constants, available modes etc
- * @reg:               supply regulator
  * @vref_mv:           actual reference voltage used
  * @pwr_down_mask:     power down mask
  * @pwr_down_mode:     current power down mode
@@ -130,7 +129,6 @@ struct ad5686_chip_info {
 struct ad5686_state {
        struct device                   *dev;
        const struct ad5686_chip_info   *chip_info;
-       struct regulator                *reg;
        unsigned short                  vref_mv;
        unsigned int                    pwr_down_mask;
        unsigned int                    pwr_down_mode;
@@ -157,7 +155,5 @@ int ad5686_probe(struct device *dev,
                 const char *name, ad5686_write_func write,
                 ad5686_read_func read);
 
-void ad5686_remove(struct device *dev);
-
 
 #endif /* __DRIVERS_IIO_DAC_AD5686_H__ */
index bbcda246c54705678b63b3bd8e0a0f5002665293..0156f32c12c88edb46303cf8a3b79c7ed7624036 100644 (file)
@@ -65,11 +65,6 @@ static int ad5686_i2c_probe(struct i2c_client *i2c)
                            ad5686_i2c_write, ad5686_i2c_read);
 }
 
-static void ad5686_i2c_remove(struct i2c_client *i2c)
-{
-       ad5686_remove(&i2c->dev);
-}
-
 static const struct i2c_device_id ad5686_i2c_id[] = {
        {"ad5311r", ID_AD5311R},
        {"ad5337r", ID_AD5337R},
@@ -116,7 +111,6 @@ static struct i2c_driver ad5686_i2c_driver = {
                .of_match_table = ad5686_of_match,
        },
        .probe = ad5686_i2c_probe,
-       .remove = ad5686_i2c_remove,
        .id_table = ad5686_i2c_id,
 };