From: "Nuno Sá" <noname.nuno@gmail.com>
To: "Angelo Dureghello" <adureghello@baylibre.com>,
"Jonathan Cameron" <jic23@kernel.org>,
"David Lechner" <dlechner@baylibre.com>,
"Nuno Sá" <nuno.sa@analog.com>,
"Andy Shevchenko" <andy@kernel.org>,
"Lars-Peter Clausen" <lars@metafoo.de>,
"Michael Hennerich" <Michael.Hennerich@analog.com>,
"Rob Herring" <robh@kernel.org>,
"Krzysztof Kozlowski" <krzk+dt@kernel.org>,
"Conor Dooley" <conor+dt@kernel.org>
Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org,
devicetree@vger.kernel.org
Subject: Re: [PATCH 3/5] iio: adc: ad7606: add offset and phase calibration support
Date: Wed, 30 Apr 2025 16:36:13 +0100 [thread overview]
Message-ID: <d273fa78cb3986da5249bd800dd25c4c0bcfde7e.camel@gmail.com> (raw)
In-Reply-To: <20250429-wip-bl-ad7606-calibration-v1-3-eb4d4821b172@baylibre.com>
On Tue, 2025-04-29 at 15:06 +0200, Angelo Dureghello wrote:
> From: Angelo Dureghello <adureghello@baylibre.com>
>
> Add support for offset and phase calibration, only for
> devices that support software mode, that are:
>
> ad7606b
> ad7606c-16
> ad7606c-18
>
> Signed-off-by: Angelo Dureghello <adureghello@baylibre.com>
> ---
> drivers/iio/adc/ad7606.c | 160
> +++++++++++++++++++++++++++++++++++++++++++++++
> drivers/iio/adc/ad7606.h | 9 +++
> 2 files changed, 169 insertions(+)
>
> diff --git a/drivers/iio/adc/ad7606.c b/drivers/iio/adc/ad7606.c
> index
> ad5e6b5e1d5d2edc7f8ac7ed9a8a4e6e43827b85..ec063dd4a67eb94610c41c473e2d8040c919
> 74cf 100644
> --- a/drivers/iio/adc/ad7606.c
> +++ b/drivers/iio/adc/ad7606.c
> @@ -95,6 +95,22 @@ static const unsigned int ad7616_oversampling_avail[8] = {
> 1, 2, 4, 8, 16, 32, 64, 128,
> };
>
> +static const int ad7606_calib_offset_avail[3] = {
> + -128, 1, 127,
> +};
> +
> +static const int ad7606c_18bit_calib_offset_avail[3] = {
> + -512, 4, 511,
> +};
From the DS, it seems this is 508?
> +
> +static const int ad7606b_calib_phase_avail[][2] = {
> + { 0, 0 }, { 0, 1250 }, { 0, 318750 },
> +};
> +
> +static const int ad7606c_calib_phase_avail[][2] = {
> + { 0, 0 }, { 0, 1000 }, { 0, 255000 },
> +};
> +
> static int ad7606c_18bit_chan_scale_setup(struct iio_dev *indio_dev,
> struct iio_chan_spec *chan);
> static int ad7606c_16bit_chan_scale_setup(struct iio_dev *indio_dev,
> @@ -164,6 +180,8 @@ const struct ad7606_chip_info ad7606b_info = {
> .scale_setup_cb = ad7606_16bit_chan_scale_setup,
> .sw_setup_cb = ad7606b_sw_mode_setup,
> .offload_storagebits = 32,
> + .calib_offset_avail = ad7606_calib_offset_avail,
> + .calib_phase_avail = ad7606b_calib_phase_avail,
> };
> EXPORT_SYMBOL_NS_GPL(ad7606b_info, "IIO_AD7606");
>
> @@ -177,6 +195,8 @@ const struct ad7606_chip_info ad7606c_16_info = {
> .scale_setup_cb = ad7606c_16bit_chan_scale_setup,
> .sw_setup_cb = ad7606b_sw_mode_setup,
> .offload_storagebits = 32,
> + .calib_offset_avail = ad7606_calib_offset_avail,
> + .calib_phase_avail = ad7606c_calib_phase_avail,
> };
> EXPORT_SYMBOL_NS_GPL(ad7606c_16_info, "IIO_AD7606");
>
> @@ -226,6 +246,8 @@ const struct ad7606_chip_info ad7606c_18_info = {
> .scale_setup_cb = ad7606c_18bit_chan_scale_setup,
> .sw_setup_cb = ad7606b_sw_mode_setup,
> .offload_storagebits = 32,
> + .calib_offset_avail = ad7606c_18bit_calib_offset_avail,
> + .calib_phase_avail = ad7606c_calib_phase_avail,
> };
> EXPORT_SYMBOL_NS_GPL(ad7606c_18_info, "IIO_AD7606");
>
> @@ -683,6 +705,40 @@ static int ad7606_scan_direct(struct iio_dev *indio_dev,
> unsigned int ch,
> return ret;
> }
>
> +static int ad7606_get_calib_offset(struct ad7606_state *st, int ch, int *val)
> +{
> + int ret;
> +
> + ret = st->bops->reg_read(st, AD7606_CALIB_OFFSET(ch));
> + if (ret < 0)
> + return ret;
> +
> + *val = st->chip_info->calib_offset_avail[0] +
> + ret * st->chip_info->calib_offset_avail[1];
> +
> + return 0;
> +}
> +
> +static int ad7606_get_calib_phase(struct ad7606_state *st, int ch, int *val,
> + int *val2)
> +{
> + int ret;
> +
> + ret = st->bops->reg_read(st, AD7606_CALIB_PHASE(ch));
> + if (ret < 0)
> + return ret;
> +
> + *val = 0;
> +
> + /*
> + * ad7606b: phase delay from 0 to 318.75 μs in steps of 1.25 μs.
> + * ad7606c-16/18: phase delay from 0 µs to 255 µs in steps of 1 µs.
> + */
> + *val2 = ret * st->chip_info->calib_phase_avail[1][1];
> +
> + return 0;
> +}
> +
> static int ad7606_read_raw(struct iio_dev *indio_dev,
> struct iio_chan_spec const *chan,
> int *val,
> @@ -717,6 +773,22 @@ static int ad7606_read_raw(struct iio_dev *indio_dev,
> pwm_get_state(st->cnvst_pwm, &cnvst_pwm_state);
> *val = DIV_ROUND_CLOSEST_ULL(NSEC_PER_SEC,
> cnvst_pwm_state.period);
> return IIO_VAL_INT;
> + case IIO_CHAN_INFO_CALIBBIAS:
> + if (!iio_device_claim_direct(indio_dev))
> + return -EBUSY;
> + ret = ad7606_get_calib_offset(st, chan->scan_index, val);
> + iio_device_release_direct(indio_dev);
> + if (ret)
> + return ret;
> + return IIO_VAL_INT;
> + case IIO_CHAN_INFO_CALIBPHASE_DELAY:
> + if (!iio_device_claim_direct(indio_dev))
> + return -EBUSY;
> + ret = ad7606_get_calib_phase(st, chan->scan_index, val,
> val2);
> + iio_device_release_direct(indio_dev);
> + if (ret)
> + return ret;
> + return IIO_VAL_INT_PLUS_NANO;
> }
> return -EINVAL;
> }
> @@ -767,6 +839,64 @@ static int ad7606_write_os_hw(struct iio_dev *indio_dev,
> int val)
> return 0;
> }
>
> +static int ad7606_set_calib_offset(struct ad7606_state *st, int ch, int val)
> +{
> + int start_val, step_val, stop_val;
> +
> + start_val = st->chip_info->calib_offset_avail[0];
> + step_val = st->chip_info->calib_offset_avail[1];
> + stop_val = st->chip_info->calib_offset_avail[2];
> +
> + if (val < start_val || val > stop_val)
> + return -EINVAL;
> +
> + val += start_val;
Shouldn't this be val -= start_val?
I also don't think we have any strict rules in the ABI for units for these kind
of interfaces so using "raw" values is easier. But FWIW, I think we could have
this in mv (would naturally depend on scale)
- Nuno Sá
next prev parent reply other threads:[~2025-04-30 15:36 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-29 13:06 [PATCH 0/5] iio: adc: add ad7606 calibration support Angelo Dureghello
2025-04-29 13:06 ` [PATCH 1/5] Documentation: ABI: IIO: add calibphase_delay documentation Angelo Dureghello
2025-04-30 5:40 ` Nuno Sá
2025-04-30 14:21 ` David Lechner
2025-04-30 14:45 ` Andy Shevchenko
2025-04-30 14:56 ` David Lechner
2025-04-30 15:04 ` David Lechner
2025-05-01 12:33 ` Angelo Dureghello
2025-05-01 14:44 ` David Lechner
2025-05-04 15:04 ` Jonathan Cameron
2025-04-29 13:06 ` [PATCH 2/5] iio: core: add ADC phase calibration definition Angelo Dureghello
2025-04-29 13:06 ` [PATCH 3/5] iio: adc: ad7606: add offset and phase calibration support Angelo Dureghello
2025-04-30 12:50 ` Andy Shevchenko
2025-05-01 12:37 ` Angelo Dureghello
2025-04-30 15:36 ` Nuno Sá [this message]
2025-04-30 16:14 ` David Lechner
2025-04-30 18:33 ` David Lechner
2025-05-02 7:43 ` Nuno Sá
2025-04-29 13:06 ` [PATCH 4/5] dt-bindings: iio: adc: adi,ad7606: add gain " Angelo Dureghello
2025-04-29 14:56 ` Conor Dooley
2025-04-29 15:26 ` David Lechner
2025-04-29 20:45 ` Angelo Dureghello
2025-04-29 13:06 ` [PATCH 5/5] iio: adc: ad7606: " Angelo Dureghello
2025-04-29 22:34 ` Andy Shevchenko
2025-05-01 13:49 ` Angelo Dureghello
2025-04-29 22:46 ` David Lechner
2025-05-01 13:35 ` Angelo Dureghello
2025-05-01 14:26 ` David Lechner
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=d273fa78cb3986da5249bd800dd25c4c0bcfde7e.camel@gmail.com \
--to=noname.nuno@gmail.com \
--cc=Michael.Hennerich@analog.com \
--cc=adureghello@baylibre.com \
--cc=andy@kernel.org \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=dlechner@baylibre.com \
--cc=jic23@kernel.org \
--cc=krzk+dt@kernel.org \
--cc=lars@metafoo.de \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.