iio: adc: ad7887: invert/rework external ref logic
authorAlexandru Ardelean <alexandru.ardelean@analog.com>
Fri, 2 Oct 2020 08:27:23 +0000 (11:27 +0300)
committerJonathan Cameron <Jonathan.Cameron@huawei.com>
Sat, 10 Oct 2020 16:38:36 +0000 (17:38 +0100)
This change inverts/reworks the logic to use an external reference via a
provided regulator.

Now the driver tries to obtain a regulator. If one is found, then it is
used. The rest of the driver logic already checks if there is a non-NULL
reference to a regulator, so it should be fine.

Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
Link: https://lore.kernel.org/r/20201002082723.184810-1-alexandru.ardelean@analog.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
drivers/iio/adc/ad7887.c
include/linux/platform_data/ad7887.h

index 037bcb4..99a480a 100644 (file)
@@ -246,11 +246,15 @@ static int ad7887_probe(struct spi_device *spi)
 
        st = iio_priv(indio_dev);
 
-       if (!pdata || !pdata->use_onchip_ref) {
-               st->reg = devm_regulator_get(&spi->dev, "vref");
-               if (IS_ERR(st->reg))
+       st->reg = devm_regulator_get_optional(&spi->dev, "vref");
+       if (IS_ERR(st->reg)) {
+               if (PTR_ERR(st->reg) != -ENODEV)
                        return PTR_ERR(st->reg);
 
+               st->reg = NULL;
+       }
+
+       if (st->reg) {
                ret = regulator_enable(st->reg);
                if (ret)
                        return ret;
@@ -269,7 +273,7 @@ static int ad7887_probe(struct spi_device *spi)
        /* Setup default message */
 
        mode = AD7887_PM_MODE4;
-       if (!pdata || !pdata->use_onchip_ref)
+       if (!st->reg)
                mode |= AD7887_REF_DIS;
        if (pdata && pdata->en_dual)
                mode |= AD7887_DUAL;
index 732af46..9b4dca6 100644 (file)
  *     second input channel, and Vref is internally connected to Vdd. If set to
  *     false the device is used in single channel mode and AIN1/Vref is used as
  *     VREF input.
- * @use_onchip_ref: Whether to use the onchip reference. If set to true the
- *     internal 2.5V reference is used. If set to false a external reference is
- *     used.
  */
 struct ad7887_platform_data {
        bool en_dual;
-       bool use_onchip_ref;
 };
 
 #endif /* IIO_ADC_AD7887_H_ */