[PATCH v3 3/7] iio: adc: ad7625: fix sampling frequency handling
From: Nicolae-Daniel Deaconescu
Date: Fri Nov 06 2026 - 11:10:00 EST
Fix sampling frequency handling issues:
- Fix issue where writing 0 to sampling_frequency caused a division by
zero. Return -EINVAL.
- Fix issue where a waveform rounded up by the PWM went unreported.
pwm_round_waveform_might_sleep() returns 1 in that case and
ad7625_set_sampling_freq() passed it on, so the sampling_frequency write
returned 1 and the new waveforms were dropped. Return -EINVAL.
- Fix issue where probe() continued after failing to set the initial
sampling frequency. The dev_err_probe() result was not returned, leaving
the PWMs 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 | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)
diff --git a/drivers/iio/adc/ad7625.c b/drivers/iio/adc/ad7625.c
index e73a5c9e7f0b..ade2f2368bd8 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);
@@ -188,8 +191,10 @@ static int ad7625_set_sampling_freq(struct ad7625_state *st, u32 freq)
cnv_wf.duty_length_ns = st->info->timing_spec->conv_high_ns;
ret = pwm_round_waveform_might_sleep(st->cnv_pwm, &cnv_wf);
- if (ret)
+ if (ret < 0)
return ret;
+ if (ret)
+ return -EINVAL;
/*
* Set up the burst signal for transferring data. period and
@@ -205,8 +210,10 @@ static int ad7625_set_sampling_freq(struct ad7625_state *st, u32 freq)
clk_gate_wf.duty_offset_ns = st->info->timing_spec->conv_msb_ns;
ret = pwm_round_waveform_might_sleep(st->clk_gate_pwm, &clk_gate_wf);
- if (ret)
+ if (ret < 0)
return ret;
+ if (ret)
+ return -EINVAL;
st->cnv_wf = cnv_wf;
st->clk_gate_wf = clk_gate_wf;
@@ -646,8 +653,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.56.0