Re: [PATCH v2 2/2] iio: adc: ad7606: finish pwm_get_state_hw() TODO

From: Uwe Kleine-König
Date: Wed Oct 30 2024 - 04:28:20 EST


On Tue, Oct 29, 2024 at 04:18:50PM -0500, David Lechner wrote:
> Replace the call to pwm_get_state() with a call to pwm_get_state_hw() in
> the ad7606 driver. This allows reading the sampling_frequency attribute
> to return the rate the hardware is actually running at rather than the
> rate that was requested. These may differ when the hardware isn't
> capable of running at exactly the requested frequency.
>
> Signed-off-by: David Lechner <dlechner@xxxxxxxxxxxx>
> ---
>
> I went ahead and made this patch since it is trivial, but it would be
> nice to get a Tested-by from Guillaume to make sure it actually works
> as expected.
> ---
> drivers/iio/adc/ad7606.c | 8 +++-----
> 1 file changed, 3 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/iio/adc/ad7606.c b/drivers/iio/adc/ad7606.c
> index 8b2046baaa3e..1581eb31b8f9 100644
> --- a/drivers/iio/adc/ad7606.c
> +++ b/drivers/iio/adc/ad7606.c
> @@ -762,11 +762,9 @@ static int ad7606_read_raw(struct iio_dev *indio_dev,
> *val = st->oversampling;
> return IIO_VAL_INT;
> case IIO_CHAN_INFO_SAMP_FREQ:
> - /*
> - * TODO: return the real frequency intead of the requested one once
> - * pwm_get_state_hw comes upstream.
> - */
> - pwm_get_state(st->cnvst_pwm, &cnvst_pwm_state);
> + ret = pwm_get_state_hw(st->cnvst_pwm, &cnvst_pwm_state);
> + if (ret < 0)
> + return ret;
> *val = DIV_ROUND_CLOSEST_ULL(NSEC_PER_SEC, cnvst_pwm_state.period);
> return IIO_VAL_INT;
> }

Acked-by: Uwe Kleine-König <u.kleine-koenig@xxxxxxxxxxxx>

There is a slight inconsistency compared to ad7606_set_sampling_freq():

ad7606_set_sampling_freq uses

cnvst_pwm_state.period = DIV_ROUND_UP_ULL(NSEC_PER_SEC, freq);

. So if cnvst_pwm_state.period happens to be 3 ns then reading
the freq value yields 333333333, but if you feed freq=333333333 into
ad7606_set_sampling_freq() it sets period = 4.

To fix that you'd better use a plain / here in ad7606_read_raw().
(Note that with using round-closest for both there are still corner
cases, e.g. period = 31796 ns yields freq = 31450.496917851302 but
setting freq = 31450 yields 31796.50238473768 and so 31797.)

Best regards
Uwe

Attachment: signature.asc
Description: PGP signature