From: David Lechner <dlechner@baylibre.com>
To: Marcelo Schmitt <marcelo.schmitt@analog.com>,
linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: Ana-Maria Cusco <ana-maria.cusco@analog.com>,
jic23@kernel.org, lars@metafoo.de, Michael.Hennerich@analog.com,
nuno.sa@analog.com, andy@kernel.org, robh@kernel.org,
krzk+dt@kernel.org, conor+dt@kernel.org,
linus.walleij@linaro.org, brgl@bgdev.pl,
marcelo.schmitt1@gmail.com
Subject: Re: [PATCH v5 02/11] iio: adc: Add basic support for AD4170
Date: Mon, 16 Jun 2025 15:41:20 -0500 [thread overview]
Message-ID: <c29feb5b-2699-411a-87dc-249f5b9ff6c0@baylibre.com> (raw)
In-Reply-To: <48598c0753cccf515addbe85acba3f883ff8f036.1749582679.git.marcelo.schmitt@analog.com>
On 6/10/25 3:31 PM, Marcelo Schmitt wrote:
> From: Ana-Maria Cusco <ana-maria.cusco@analog.com>
>
> The AD4170 is a multichannel, low noise, 24-bit precision sigma-delta
> analog to digital converter. The AD4170 design offers a flexible data
> acquisition solution with crosspoint multiplexed analog inputs,
> configurable ADC voltage reference inputs, ultra-low noise integrated PGA,
> digital filtering, wide range of configurable output data rates, internal
> oscillator and temperature sensor, four GPIOs, and integrated features for
> interfacing with load cell weigh scales, RTD, and thermocouple sensors.
>
> Add basic support for the AD4170 ADC with the following features:
> - Single-shot read.
> - Analog front end PGA configuration.
> - Differential and pseudo-differential input configuration.
>
...
> +static int ad4170_fill_scale_tbl(struct iio_dev *indio_dev,
> + struct iio_chan_spec const *chan)
> +{
> + struct ad4170_state *st = iio_priv(indio_dev);
> + struct ad4170_chan_info *chan_info = &st->chan_infos[chan->address];
> + struct device *dev = &st->spi->dev;
> + int bipolar = chan->scan_type.sign == 's' ? 1 : 0;
> + int precision_bits = chan->scan_type.realbits;
> + int pga, ainm_voltage, ret;
> + unsigned long long offset;
> +
> + ainm_voltage = 0;
> + ret = ad4170_get_ain_voltage_uv(st, chan->channel2, &ainm_voltage);
> + if (ret < 0)
> + return dev_err_probe(dev, ret, "Failed to fill scale table\n");
> +
> + for (pga = 0; pga < AD4170_NUM_PGA_OPTIONS; pga++) {
From what I read in the datasheet, it sounds like if adi,reference-buffer is
precharge, then the PGA is bypassed, so there would only be 1 option in that
case (gain = 1).
> + u64 nv;
> + unsigned int lshift, rshift;
> +
> + /*
> + * The PGA options are numbered from 0 to 9, with option 0 being
> + * a gain of 2^0 (no actual gain), and 7 meaning a gain of 2^7.
> + * Option 8, though, sets a gain of 0.5, so the input signal can
> + * be attenuated by 2 rather than amplified. Option 9, allows
> + * the signal to bypass the PGA circuitry (no gain).
> + *
> + * The scale factor to get ADC output codes to values in mV
> + * units is given by:
> + * _scale = (input_range / gain) / 2^precision
> + * AD4170 gain is a power of 2 so the above can be written as
> + * _scale = input_range / 2^(precision + gain)
> + * Keep the input range in µV to avoid truncating the less
> + * significan bits when right shifting it so to preserve scale
> + * precision.
> + */
> + nv = (u64)chan_info->input_range_uv * NANO;
> + lshift = !!(pga & BIT(3)); /* handle options 8 and 9 */
> + rshift = precision_bits - bipolar + (pga & GENMASK(2, 0)) - lshift;
> + chan_info->scale_tbl[pga][0] = 0;
> + chan_info->scale_tbl[pga][1] = div_u64(nv >> rshift, MILLI);
> +
> + /*
> + * If the negative input is not at GND, the conversion result
> + * (which is relative to IN-) will be offset by the level at IN-.
> + * Use the scale factor the other way around to go from a known
> + * voltage to the corresponding ADC output code.
> + * With that, we are able to get to what would be the output
> + * code for the voltage at the negative input.
> + * If the negative input is not fixed, there is no offset.
> + */
> + offset = ((unsigned long long)abs(ainm_voltage)) * MICRO;
> + offset = DIV_ROUND_CLOSEST_ULL(offset, chan_info->scale_tbl[pga][1]);
> +
> + /*
> + * After divided by the scale, offset will always fit into 31
> + * bits. For _raw + _offset to be relative to GND, the value
> + * provided as _offset is of opposite sign than the real offset.
> + */
> + if (ainm_voltage > 0)
> + chan_info->offset_tbl[pga] = -(int)(offset);
> + else
> + chan_info->offset_tbl[pga] = (int)(offset);
> + }
> + return 0;
> +}
> +
> +static int ad4170_read_avail(struct iio_dev *indio_dev,
> + struct iio_chan_spec const *chan,
> + const int **vals, int *type, int *length,
> + long info)
> +{
> + struct ad4170_state *st = iio_priv(indio_dev);
> + struct ad4170_chan_info *chan_info = &st->chan_infos[chan->address];
> +
> + switch (info) {
> + case IIO_CHAN_INFO_SCALE:
> + *vals = (int *)chan_info->scale_tbl;
> + *length = ARRAY_SIZE(chan_info->scale_tbl) * 2;
> + *type = IIO_VAL_INT_PLUS_NANO;
> + return IIO_AVAIL_LIST;
> + default:
> + return -EINVAL;
> + }
> +}
> +
> +static int ad4170_set_pga(struct ad4170_state *st,
> + struct iio_chan_spec const *chan, int val, int val2)
> +{
> + struct ad4170_chan_info *chan_info = &st->chan_infos[chan->address];
> + struct ad4170_setup *setup = &chan_info->setup;
> + unsigned int pga;
Likewise, here would also need to check for precharge and only allow
gain = 1 in in that case.
> +
> + for (pga = 0; pga < AD4170_NUM_PGA_OPTIONS; pga++) {
> + if (val == chan_info->scale_tbl[pga][0] &&
> + val2 == chan_info->scale_tbl[pga][1])
> + break;
> + }
> +
> + if (pga == AD4170_NUM_PGA_OPTIONS)
> + return -EINVAL;
> +
> + guard(mutex)(&st->lock);
> + setup->afe &= ~AD4170_AFE_PGA_GAIN_MSK;
> + setup->afe |= FIELD_PREP(AD4170_AFE_PGA_GAIN_MSK, pga);
> +
> + return ad4170_write_channel_setup(st, chan->address, false);
> +}
> +
next prev parent reply other threads:[~2025-06-16 20:41 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-10 20:30 [PATCH v5 00/11] iio: adc: Add support for AD4170 series of ADCs Marcelo Schmitt
2025-06-10 20:31 ` [PATCH v5 01/11] dt-bindings: iio: adc: Add AD4170 Marcelo Schmitt
2025-06-16 15:41 ` Conor Dooley
2025-06-16 17:58 ` Marcelo Schmitt
2025-06-16 20:41 ` David Lechner
2025-06-21 16:28 ` Jonathan Cameron
2025-06-23 16:03 ` Conor Dooley
2025-06-21 16:37 ` Jonathan Cameron
2025-06-10 20:31 ` [PATCH v5 02/11] iio: adc: Add basic support for AD4170 Marcelo Schmitt
2025-06-10 21:10 ` Andy Shevchenko
2025-06-11 21:04 ` Marcelo Schmitt
2025-06-12 12:48 ` Andy Shevchenko
2025-06-12 14:03 ` Marcelo Schmitt
2025-06-12 18:48 ` Andy Shevchenko
2025-06-14 10:51 ` Jonathan Cameron
2025-06-14 10:52 ` Jonathan Cameron
2025-06-16 20:41 ` David Lechner [this message]
2025-06-17 18:54 ` Marcelo Schmitt
2025-06-18 17:37 ` Dan Carpenter
2025-06-18 17:59 ` Andy Shevchenko
2025-06-10 20:31 ` [PATCH v5 03/11] iio: adc: ad4170: Add support for calibration gain Marcelo Schmitt
2025-06-10 20:32 ` [PATCH v5 04/11] iio: adc: ad4170: Add support for calibration bias Marcelo Schmitt
2025-06-10 20:32 ` [PATCH v5 05/11] iio: adc: ad4170: Add digital filter and sample frequency config support Marcelo Schmitt
2025-06-16 20:53 ` David Lechner
2025-06-10 20:32 ` [PATCH v5 06/11] iio: adc: ad4170: Add support for buffered data capture Marcelo Schmitt
2025-06-10 21:17 ` Andy Shevchenko
2025-06-10 20:33 ` [PATCH v5 07/11] iio: adc: ad4170: Add clock provider support Marcelo Schmitt
2025-06-16 21:11 ` David Lechner
2025-06-17 6:24 ` Andy Shevchenko
2025-06-10 20:33 ` [PATCH v5 08/11] iio: adc: ad4170: Add GPIO controller support Marcelo Schmitt
2025-06-18 10:15 ` Linus Walleij
2025-06-10 20:33 ` [PATCH v5 09/11] iio: adc: ad4170: Add support for internal temperature sensor Marcelo Schmitt
2025-06-10 20:33 ` [PATCH v5 10/11] iio: adc: ad4170: Add support for weigh scale and RTD sensors Marcelo Schmitt
2025-06-10 20:34 ` [PATCH v5 11/11] iio: adc: ad4170: Add timestamp channel Marcelo Schmitt
2025-06-14 11:04 ` [PATCH v5 00/11] iio: adc: Add support for AD4170 series of ADCs Jonathan Cameron
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=c29feb5b-2699-411a-87dc-249f5b9ff6c0@baylibre.com \
--to=dlechner@baylibre.com \
--cc=Michael.Hennerich@analog.com \
--cc=ana-maria.cusco@analog.com \
--cc=andy@kernel.org \
--cc=brgl@bgdev.pl \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=jic23@kernel.org \
--cc=krzk+dt@kernel.org \
--cc=lars@metafoo.de \
--cc=linus.walleij@linaro.org \
--cc=linux-gpio@vger.kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=marcelo.schmitt1@gmail.com \
--cc=marcelo.schmitt@analog.com \
--cc=nuno.sa@analog.com \
--cc=robh@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).