[PATCH 08/18] iio: adc: ad4170-4: Fix output data rate on AD4190-4 and AD4195-4

From: Janani Sunil

Date: Thu Dec 03 2026 - 10:21:43 EST


The main clock divider (CLOCKDIV in CLOCK_CTRL) defaults to divide-by-1
on the AD4170-4, but to divide-by-2 on the AD4190-4 and AD4195-4. The
driver uses the undivided input clock for the sample rate table and the
conversion timeout. On AD4190-4 and AD4195-4 this doubles the reported
sampling frequencies and halves the calculated settling time.

Add a per-device clock_div to chip_info and divide the main clock by it.
Initialize each channel's FILTER_FS to its reset value of 4. A fixed
default sampling frequency assumes a 16 MHz clock and can make probe fail
with -EINVAL for a valid external clock. For example, with a 12 MHz clock
the default filter on AD4190-4 runs at 46875 samples per second.

The external-clock case was identified during review with Codex.

Fixes: 5731f2a06c0f ("iio: adc: Add basic support for AD4170-4")
Signed-off-by: Janani Sunil <janani.sunil@xxxxxxxxxx>
---
drivers/iio/adc/ad4170-4.c | 19 +++++++++++--------
1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/drivers/iio/adc/ad4170-4.c b/drivers/iio/adc/ad4170-4.c
index 575ac8d61301..0a5d29200ff2 100644
--- a/drivers/iio/adc/ad4170-4.c
+++ b/drivers/iio/adc/ad4170-4.c
@@ -194,7 +194,6 @@
#define AD4170_SPI_INST_PHASE_LEN 2
#define AD4170_SPI_MAX_XFER_LEN 6
#define AD4170_NUM_CURRENT_SRC 4
-#define AD4170_DEFAULT_SAMP_RATE (125 * HZ_PER_KHZ)

#define AD4170_INT_REF_2_5V 2500000

@@ -211,6 +210,7 @@
#define AD4170_SINC5_MIN_FS 1
#define AD4170_SINC5_MAX_FS 256

+#define AD4170_FILTER_FS_REG_DEFAULT 4
#define AD4170_GAIN_REG_DEFAULT 0x555555

#define AD4170_ADC_CTRL_CONT_READ_EXIT 0xA5
@@ -374,18 +374,22 @@ static const char * const ad4170_sensor_type[] = {

struct ad4170_chip_info {
const char *name;
+ unsigned int clock_div;
};

static const struct ad4170_chip_info ad4170_chip_info = {
.name = "ad4170-4",
+ .clock_div = 1,
};

static const struct ad4170_chip_info ad4190_chip_info = {
.name = "ad4190-4",
+ .clock_div = 2,
};

static const struct ad4170_chip_info ad4195_chip_info = {
.name = "ad4195-4",
+ .clock_div = 2,
};

/*
@@ -437,6 +441,7 @@ static const char * const ad4170_filt_names[] = {

struct ad4170_state {
struct mutex lock; /* Protect read-modify-write and multi write sequences */
+ const struct ad4170_chip_info *chip;
int vrefs_uv[AD4170_MAX_SUP];
u32 mclk_hz;
struct ad4170_setup_info setup_infos[AD4170_MAX_SETUPS];
@@ -2479,7 +2484,7 @@ static int ad4170_clock_select(struct iio_dev *indio_dev)

if (!ext_clk) {
/* Use internal clock reference */
- st->mclk_hz = AD4170_INT_CLOCK_16MHZ;
+ st->mclk_hz = AD4170_INT_CLOCK_16MHZ / st->chip->clock_div;
st->clock_ctrl |= FIELD_PREP(AD4170_CLOCK_CTRL_CLOCKSEL_MSK,
AD4170_CLOCK_CTRL_CLOCKSEL_INT_OUT);

@@ -2506,6 +2511,8 @@ static int ad4170_clock_select(struct iio_dev *indio_dev)
st->mclk_hz);
}

+ st->mclk_hz /= st->chip->clock_div;
+
return 0;
}

@@ -2622,6 +2629,7 @@ static int ad4170_initial_config(struct iio_dev *indio_dev)
chan_info = &st->chan_infos[chan->address];

setup = &chan_info->setup;
+ setup->filter_fs = AD4170_FILTER_FS_REG_DEFAULT;
setup->gain = AD4170_GAIN_REG_DEFAULT;
ret = ad4170_write_channel_setup(st, chan->address, false);
if (ret)
@@ -2636,12 +2644,6 @@ static int ad4170_initial_config(struct iio_dev *indio_dev)
return dev_err_probe(dev, ret,
"Failed to write CHAN_MAP_REG\n");

- ret = ad4170_set_channel_freq(st, chan,
- AD4170_DEFAULT_SAMP_RATE, 0);
- if (ret)
- return dev_err_probe(dev, ret,
- "Failed to set channel freq\n");
-
ret = ad4170_fill_scale_tbl(indio_dev, chan);
if (ret)
return dev_err_probe(dev, ret,
@@ -2934,6 +2936,7 @@ static int ad4170_probe(struct spi_device *spi)
if (!chip)
return -EINVAL;

+ st->chip = chip;
indio_dev->name = chip->name;
indio_dev->info = &ad4170_info;


--
2.43.0