diff mbox series

[v8,3/6] iio: adc: ad7192: Add aincom supply

Message ID 20240514120222.56488-4-alisa.roman@analog.com (mailing list archive)
State Accepted
Headers show
Series iio: adc: ad7192: Add AD7194 support | expand

Commit Message

Alisa-Dariana Roman May 14, 2024, 12:02 p.m. UTC
AINCOM should actually be a supply. AINx inputs are referenced to AINCOM
in pseudo-differential operation mode. AINCOM voltage represents the
offset of corresponding channels.

Signed-off-by: Alisa-Dariana Roman <alisa.roman@analog.com>
---
 drivers/iio/adc/ad7192.c | 50 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 48 insertions(+), 2 deletions(-)

Comments

Andy Shevchenko May 14, 2024, 1:13 p.m. UTC | #1
On Tue, May 14, 2024 at 03:02:19PM +0300, Alisa-Dariana Roman wrote:
> AINCOM should actually be a supply. AINx inputs are referenced to AINCOM
> in pseudo-differential operation mode. AINCOM voltage represents the
> offset of corresponding channels.

...

Possible cleanup with the help of

	struct device *dev = &spi->dev;


>  	struct ad7192_state *st;
>  	struct iio_dev *indio_dev;
> +	struct regulator *aincom;
>  	int ret;

...

> +	aincom = devm_regulator_get_optional(&spi->dev, "aincom");

	aincom = devm_regulator_get_optional(dev, "aincom");

...

> +			return dev_err_probe(&spi->dev, PTR_ERR(aincom),
> +					     "Failed to get AINCOM supply\n");

			return dev_err_probe(dev, PTR_ERR(aincom),
					     "Failed to get AINCOM supply\n");

...

> +			return dev_err_probe(&spi->dev, ret,
> +					     "Failed to enable specified AINCOM supply\n");

			return dev_err_probe(dev, ret,
					     "Failed to enable specified AINCOM supply\n");

...

> +		ret = devm_add_action_or_reset(&spi->dev, ad7192_reg_disable, aincom);

		ret = devm_add_action_or_reset(dev, ad7192_reg_disable, aincom);

...

> +			return dev_err_probe(&spi->dev, ret,
> +					     "Device tree error, AINCOM voltage undefined\n");

			return dev_err_probe(dev, ret,
					     "Device tree error, AINCOM voltage undefined\n");
Jonathan Cameron May 19, 2024, 5:53 p.m. UTC | #2
On Tue, 14 May 2024 16:13:05 +0300
Andy Shevchenko <andy@kernel.org> wrote:

> On Tue, May 14, 2024 at 03:02:19PM +0300, Alisa-Dariana Roman wrote:
> > AINCOM should actually be a supply. AINx inputs are referenced to AINCOM
> > in pseudo-differential operation mode. AINCOM voltage represents the
> > offset of corresponding channels.  
> 
> ...
> 
> Possible cleanup with the help of
> 
> 	struct device *dev = &spi->dev;
This is a good thing to have as a follow up as it applies much more widely
than what is visible in this patch. In ideal world it would have been
a precursor to this series, but I don't want to delay this for a v9 just
to add that.

Hence I'm not going to tweak this whilst applying.

Patch applied as is.

Thanks,

Jonathan

> 
> 
> >  	struct ad7192_state *st;
> >  	struct iio_dev *indio_dev;
> > +	struct regulator *aincom;
> >  	int ret;  
> 
> ...
> 
> > +	aincom = devm_regulator_get_optional(&spi->dev, "aincom");  
> 
> 	aincom = devm_regulator_get_optional(dev, "aincom");
> 
> ...
> 
> > +			return dev_err_probe(&spi->dev, PTR_ERR(aincom),
> > +					     "Failed to get AINCOM supply\n");  
> 
> 			return dev_err_probe(dev, PTR_ERR(aincom),
> 					     "Failed to get AINCOM supply\n");
> 
> ...
> 
> > +			return dev_err_probe(&spi->dev, ret,
> > +					     "Failed to enable specified AINCOM supply\n");  
> 
> 			return dev_err_probe(dev, ret,
> 					     "Failed to enable specified AINCOM supply\n");
> 
> ...
> 
> > +		ret = devm_add_action_or_reset(&spi->dev, ad7192_reg_disable, aincom);  
> 
> 		ret = devm_add_action_or_reset(dev, ad7192_reg_disable, aincom);
> 
> ...
> 
> > +			return dev_err_probe(&spi->dev, ret,
> > +					     "Device tree error, AINCOM voltage undefined\n");  
> 
> 			return dev_err_probe(dev, ret,
> 					     "Device tree error, AINCOM voltage undefined\n");
>
diff mbox series

Patch

diff --git a/drivers/iio/adc/ad7192.c b/drivers/iio/adc/ad7192.c
index ace81e3817a1..7160929d32c9 100644
--- a/drivers/iio/adc/ad7192.c
+++ b/drivers/iio/adc/ad7192.c
@@ -20,6 +20,7 @@ 
 #include <linux/module.h>
 #include <linux/mod_devicetable.h>
 #include <linux/property.h>
+#include <linux/units.h>
 
 #include <linux/iio/iio.h>
 #include <linux/iio/sysfs.h>
@@ -186,6 +187,7 @@  struct ad7192_state {
 	struct regulator		*vref;
 	struct clk			*mclk;
 	u16				int_vref_mv;
+	u32				aincom_mv;
 	u32				fclk;
 	u32				mode;
 	u32				conf;
@@ -742,10 +744,24 @@  static int ad7192_read_raw(struct iio_dev *indio_dev,
 			*val = -(1 << (chan->scan_type.realbits - 1));
 		else
 			*val = 0;
+
+		switch (chan->type) {
+		case IIO_VOLTAGE:
+			/*
+			 * Only applies to pseudo-differential inputs.
+			 * AINCOM voltage has to be converted to "raw" units.
+			 */
+			if (st->aincom_mv && !chan->differential)
+				*val += DIV_ROUND_CLOSEST_ULL((u64)st->aincom_mv * NANO,
+							      st->scale_avail[gain][1]);
+			return IIO_VAL_INT;
 		/* Kelvin to Celsius */
-		if (chan->type == IIO_TEMP)
+		case IIO_TEMP:
 			*val -= 273 * ad7192_get_temp_scale(unipolar);
-		return IIO_VAL_INT;
+			return IIO_VAL_INT;
+		default:
+			return -EINVAL;
+		}
 	case IIO_CHAN_INFO_SAMP_FREQ:
 		*val = DIV_ROUND_CLOSEST(ad7192_get_f_adc(st), 1024);
 		return IIO_VAL_INT;
@@ -1052,6 +1068,7 @@  static int ad7192_probe(struct spi_device *spi)
 {
 	struct ad7192_state *st;
 	struct iio_dev *indio_dev;
+	struct regulator *aincom;
 	int ret;
 
 	if (!spi->irq) {
@@ -1067,6 +1084,35 @@  static int ad7192_probe(struct spi_device *spi)
 
 	mutex_init(&st->lock);
 
+	/*
+	 * Regulator aincom is optional to maintain compatibility with older DT.
+	 * Newer firmware should provide a zero volt fixed supply if wired to
+	 * ground.
+	 */
+	aincom = devm_regulator_get_optional(&spi->dev, "aincom");
+	if (IS_ERR(aincom)) {
+		if (PTR_ERR(aincom) != -ENODEV)
+			return dev_err_probe(&spi->dev, PTR_ERR(aincom),
+					     "Failed to get AINCOM supply\n");
+
+		st->aincom_mv = 0;
+	} else {
+		ret = regulator_enable(aincom);
+		if (ret)
+			return dev_err_probe(&spi->dev, ret,
+					     "Failed to enable specified AINCOM supply\n");
+
+		ret = devm_add_action_or_reset(&spi->dev, ad7192_reg_disable, aincom);
+		if (ret)
+			return ret;
+
+		ret = regulator_get_voltage(aincom);
+		if (ret < 0)
+			return dev_err_probe(&spi->dev, ret,
+					     "Device tree error, AINCOM voltage undefined\n");
+		st->aincom_mv = ret / MILLI;
+	}
+
 	st->avdd = devm_regulator_get(&spi->dev, "avdd");
 	if (IS_ERR(st->avdd))
 		return PTR_ERR(st->avdd);