[PATCH v2 3/5] iio: adc: ad7625: fix sampling frequency handling
From: Nicolae-Daniel Deaconescu
Date: Wed Nov 04 2026 - 08:27:43 EST
Fix the following issues in the handling of the sampling frequency:
- Fix the issue where writing 0 to sampling_frequency caused a division
by zero. Return -EINVAL instead.
- Fix the issue where a waveform that the PWM had to round up was not
reported as an error. pwm_round_waveform_might_sleep() returns 1 in that
case and ad7625_set_sampling_freq() passed it on, so the write to
sampling_frequency returned 1 and the new waveforms were dropped. Return
-EINVAL instead.
- Fix the issue where probe() went on after failing to set the initial
sampling frequency. The result of dev_err_probe() was not returned, so
the PWMs stayed unconfigured. Return the error.
Fixes: b7ffd0fa65e9 ("iio: adc: ad7625: add driver")
Signed-off-by: Nicolae-Daniel Deaconescu <Nicolae-daniel.Deaconescu@xxxxxxxxxx>
---
drivers/iio/adc/ad7625.c | 11 +++++++----
1 file changed, 7 insertions(+), 4 deletions(-)
diff --git a/drivers/iio/adc/ad7625.c b/drivers/iio/adc/ad7625.c
index e73a5c9e7f0b..0de7f29797ef 100644
--- a/drivers/iio/adc/ad7625.c
+++ b/drivers/iio/adc/ad7625.c
@@ -178,6 +178,9 @@ static int ad7625_set_sampling_freq(struct ad7625_state *st, u32 freq)
unsigned long target;
int ret;
+ if (!freq)
+ return -EINVAL;
+
target = DIV_ROUND_UP(NSEC_PER_SEC, freq);
cnv_wf.period_length_ns = clamp(target, 100UL, 10UL * NSEC_PER_USEC);
@@ -189,7 +192,7 @@ static int ad7625_set_sampling_freq(struct ad7625_state *st, u32 freq)
ret = pwm_round_waveform_might_sleep(st->cnv_pwm, &cnv_wf);
if (ret)
- return ret;
+ return ret < 0 ? ret : -EINVAL;
/*
* Set up the burst signal for transferring data. period and
@@ -206,7 +209,7 @@ static int ad7625_set_sampling_freq(struct ad7625_state *st, u32 freq)
ret = pwm_round_waveform_might_sleep(st->clk_gate_pwm, &clk_gate_wf);
if (ret)
- return ret;
+ return ret < 0 ? ret : -EINVAL;
st->cnv_wf = cnv_wf;
st->clk_gate_wf = clk_gate_wf;
@@ -646,8 +649,8 @@ static int ad7625_probe(struct platform_device *pdev)
ret = ad7625_set_sampling_freq(st, default_sample_freq);
if (ret)
- dev_err_probe(dev, ret,
- "failed to set valid sampling frequency\n");
+ return dev_err_probe(dev, ret,
+ "failed to set valid sampling frequency\n");
return devm_iio_device_register(dev, indio_dev);
}
--
2.34.1