diff mbox series

[v6,3/5] iio: adc: ad7192: Add aincom supply

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

Commit Message

Alisa-Dariana Roman April 17, 2024, 5 p.m. UTC
AINCOM should actually be a supply. AINx inputs are referenced to AINCOM
in pseduo-differential operation mode. AINCOM voltage represets the
offset of corresponding channels.

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

Comments

Andy Shevchenko April 17, 2024, 5:08 p.m. UTC | #1
On Wed, Apr 17, 2024 at 8:01 PM Alisa-Dariana Roman
<alisadariana@gmail.com> wrote:
>
> AINCOM should actually be a supply. AINx inputs are referenced to AINCOM
> in pseduo-differential operation mode. AINCOM voltage represets the

pseudo

> offset of corresponding channels.

...

> +               case IIO_VOLTAGE:
> +                       if (st->aincom_mv && !chan->differential)
> +                               *val += DIV_ROUND_CLOSEST_ULL((u64)st->aincom_mv * 1000000000,

It's quite easy to make a mistake in this long constant. Can you use
an appropriate one from units.h?

> +                                                             st->scale_avail[gain][1]);
> +                       return IIO_VAL_INT;

...

> +       aincom = devm_regulator_get_optional(&spi->dev, "aincom");
> +       if (!IS_ERR(aincom)) {

Why not a positive condition?

> +               ret = regulator_enable(aincom);
> +               if (ret) {

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

return dev_err_probe();

> +                       return ret;
> +               }
> +
> +               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 / 1000;
> +       } else {
> +               st->aincom_mv = 0;
> +       }

...

> @@ -1113,6 +1145,7 @@ static int ad7192_probe(struct spi_device *spi)
>         st->int_vref_mv = ret / 1000;
>
>         st->chip_info = spi_get_device_match_data(spi);
> +
>         indio_dev->name = st->chip_info->name;
>         indio_dev->modes = INDIO_DIRECT_MODE;
>         indio_dev->channels = st->chip_info->channels;

Stray change.
Jonathan Cameron April 20, 2024, 10:42 a.m. UTC | #2
On Wed, 17 Apr 2024 20:00:52 +0300
Alisa-Dariana Roman <alisadariana@gmail.com> wrote:

> AINCOM should actually be a supply. AINx inputs are referenced to AINCOM
> in pseduo-differential operation mode. AINCOM voltage represets the
> offset of corresponding channels.
> 
> Signed-off-by: Alisa-Dariana Roman <alisa.roman@analog.com>
One request inline to make my life easier :)

Otherwise, I noticed a subset of what Andy picked up on so won't
repeat his comments!

Thanks,

Jonathan

> ---
>  drivers/iio/adc/ad7192.c | 37 +++++++++++++++++++++++++++++++++++--
>  1 file changed, 35 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/iio/adc/ad7192.c b/drivers/iio/adc/ad7192.c
> index fe8dbb68a8ba..8d56cf889973 100644
> --- a/drivers/iio/adc/ad7192.c
> +++ b/drivers/iio/adc/ad7192.c
> @@ -186,6 +186,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 +743,19 @@ 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:
> +			if (st->aincom_mv && !chan->differential)
> +				*val += DIV_ROUND_CLOSEST_ULL((u64)st->aincom_mv * 1000000000,
> +							      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 +1062,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 +1078,27 @@ static int ad7192_probe(struct spi_device *spi)
>  
>  	mutex_init(&st->lock);
>  
As we are going around again, could you do me a favour and add a comment
here along the lines of.

	/*
	 * aincom is optional to maintain compatibility with older DT.
	 * Newer firmware should provide a zero volt fixed supply if
	 * this is wired to ground.	 
	 */

Aim being to discourage this getting cut and paste into other drivers
that support the common voltage from the start!


> +	aincom = devm_regulator_get_optional(&spi->dev, "aincom");
> +	if (!IS_ERR(aincom)) {
> +		ret = regulator_enable(aincom);
> +		if (ret) {
> +			dev_err(&spi->dev, "Failed to enable specified AINCOM supply\n");
> +			return ret;
> +		}
> +
> +		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 / 1000;
> +	} else {
> +		st->aincom_mv = 0;
> +	}
> +
>  	st->avdd = devm_regulator_get(&spi->dev, "avdd");
>  	if (IS_ERR(st->avdd))
>  		return PTR_ERR(st->avdd);
> @@ -1113,6 +1145,7 @@ static int ad7192_probe(struct spi_device *spi)
>  	st->int_vref_mv = ret / 1000;
>  
>  	st->chip_info = spi_get_device_match_data(spi);
> +
>  	indio_dev->name = st->chip_info->name;
>  	indio_dev->modes = INDIO_DIRECT_MODE;
>  	indio_dev->channels = st->chip_info->channels;
diff mbox series

Patch

diff --git a/drivers/iio/adc/ad7192.c b/drivers/iio/adc/ad7192.c
index fe8dbb68a8ba..8d56cf889973 100644
--- a/drivers/iio/adc/ad7192.c
+++ b/drivers/iio/adc/ad7192.c
@@ -186,6 +186,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 +743,19 @@  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:
+			if (st->aincom_mv && !chan->differential)
+				*val += DIV_ROUND_CLOSEST_ULL((u64)st->aincom_mv * 1000000000,
+							      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 +1062,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 +1078,27 @@  static int ad7192_probe(struct spi_device *spi)
 
 	mutex_init(&st->lock);
 
+	aincom = devm_regulator_get_optional(&spi->dev, "aincom");
+	if (!IS_ERR(aincom)) {
+		ret = regulator_enable(aincom);
+		if (ret) {
+			dev_err(&spi->dev, "Failed to enable specified AINCOM supply\n");
+			return ret;
+		}
+
+		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 / 1000;
+	} else {
+		st->aincom_mv = 0;
+	}
+
 	st->avdd = devm_regulator_get(&spi->dev, "avdd");
 	if (IS_ERR(st->avdd))
 		return PTR_ERR(st->avdd);
@@ -1113,6 +1145,7 @@  static int ad7192_probe(struct spi_device *spi)
 	st->int_vref_mv = ret / 1000;
 
 	st->chip_info = spi_get_device_match_data(spi);
+
 	indio_dev->name = st->chip_info->name;
 	indio_dev->modes = INDIO_DIRECT_MODE;
 	indio_dev->channels = st->chip_info->channels;