iio: adc: ad7768-1: add support for ADAQ776x-1 ADC Family
Subsystems (3)
PATCH v2 4/4 iio: adc: ad7768-1: add support for ADAQ776x-1 ADC Family
Add support for ADAQ7767/68/69-1 series, which includes PGIA and Anti-aliasing filter (AAF) gains. Unlike the AD7768-1, they do not provide a VCM regulator interface.
The PGA gain is configured in run-time through the scale attribute, if supported by the device. PGA is enabled and controlled by the GPIO controller (GPIOs 0, 1 and 2) provided by the device with the SPI interface.
The AAF gain is defined by hardware connections and should be specified in device tree.
---
v2 Changes:
* Added more details to the commit message.
* Some devices does not provide VCM regulator, so a new field in the chip info struct was added to indicate this.
* Added 'select RATIONAL' to Kconfig. Kernel test robot pointed out compilation error due to undefined reference to rational_best_approximation().
* Added lock to protect PGA value access.
* precision in the PGA calculation is now dependent of the channel sign (signed or unsigned).
* went back to the original scale computation: (st->vref_uv * 2) / 2^n instead of st->vref_uv / 2^(n-1).
* rewrote AAF gain check and replaced error returns with warnings. I the AAF gain is not provided, a default value is used.
* Addressed other minor suggestions.
--- drivers/iio/adc/Kconfig | 1 + drivers/iio/adc/ad7768-1.c | 288 ++++++++++++++++++++++++++++++++++++- 2 files changed, 286 insertions(+), 3 deletions(-)
drivers/iio/adc/Kconfig +1 -0
@@ -374,6 +374,7 @@ config AD7768_1 depends on SPI select REGULATOR select REGMAP_SPI + select RATIONAL select IIO_BUFFER select IIO_TRIGGER select IIO_TRIGGERED_BUFFER
drivers/iio/adc/ad7768-1.c +285 -3
438 lines not on this page · raw message
Signature · 1 line
2.34.1
Quoted 5 lines · Jonathan Santos
Add support for ADAQ7767/68/69-1 series, which includes PGIA and Anti-aliasing filter (AAF) gains. Unlike the AD7768-1, they do not provide a VCM regulator interface.
Quoted 7 lines
The PGA gain is configured in run-time through the scale attribute, if supported by the device. PGA is enabled and controlled by the GPIO controller (GPIOs 0, 1 and 2) provided by the device with the SPI interface.
The AAF gain is defined by hardware connections and should be specified in device tree.
the device
...
drivers/iio/adc/ad7768-1.c enum ad7768_scan_type { · line 175
Quoted 11 lines
drivers/iio/adc/ad7768-1.c +10
-0+enum { +AD7768_PGA_GAIN_0 , +AD7768_PGA_GAIN_1 , +AD7768_PGA_GAIN_2 , +AD7768_PGA_GAIN_3 , +AD7768_PGA_GAIN_4 , +AD7768_PGA_GAIN_5 , +AD7768_PGA_GAIN_6 , +AD7768_PGA_GAIN_7 , +AD7768_MAX_PGA_GAIN ,
No trailing comma for the terminator line and I haven't noticed if it's even being used in the code...
Quoted 1 line
+};
...
drivers/iio/adc/ad7768-1.c enum ad7768_scan_type { · line 185
Quoted 2 lines
drivers/iio/adc/ad7768-1.c +2
-0+/* PGA and AAF gains in V/V */ +static const int adaq7768_gains[7 ] = {
The 7 is redundant. Leave it for the compiler.
drivers/iio/adc/ad7768-1.c enum ad7768_scan_type { · line 192
Quoted 7 lines
drivers/iio/adc/ad7768-1.c +7
-0+ [AD7768_PGA_GAIN_0] = 325, /* 0.325 */ + [AD7768_PGA_GAIN_1] = 650, /* 0.650 */ + [AD7768_PGA_GAIN_2] = 1300, /* 1.300 */ + [AD7768_PGA_GAIN_3] = 2600, /* 2.600 */ + [AD7768_PGA_GAIN_4] = 5200, /* 5.200 */ + [AD7768_PGA_GAIN_5] = 10400, /* 10.400 */ + [AD7768_PGA_GAIN_6 ] =20800 /* 20.800 */
Please, leave a trailing comma.
drivers/iio/adc/ad7768-1.c enum ad7768_scan_type { · line 195
Quoted 3 lines
drivers/iio/adc/ad7768-1.c +3
-0+}; + +static const int adaq7769_gains[8 ] = {
8 is redundant.
drivers/iio/adc/ad7768-1.c enum ad7768_scan_type { · line 203
Quoted 8 lines
drivers/iio/adc/ad7768-1.c +8
-0+ [AD7768_PGA_GAIN_0] = 1000, /* 1.000 */ + [AD7768_PGA_GAIN_1] = 2000, /* 2.000 */ + [AD7768_PGA_GAIN_2] = 4000, /* 4.000 */ + [AD7768_PGA_GAIN_3] = 8000, /* 8.000 */ + [AD7768_PGA_GAIN_4] = 16000, /* 16.000 */ + [AD7768_PGA_GAIN_5] = 32000, /* 32.000 */ + [AD7768_PGA_GAIN_6] = 64000, /* 64.000 */ + [AD7768_PGA_GAIN_7 ] =128000 /* 128.000 */
Please, leave a trailing comma.
drivers/iio/adc/ad7768-1.c enum ad7768_scan_type { · line 206
Quoted 3 lines
drivers/iio/adc/ad7768-1.c +2
-0+}; +static const int ad7768_aaf_gains[3 ] = {
3 is redundant,
drivers/iio/adc/ad7768-1.c enum ad7768_scan_type { · line 209
Quoted 3 lines
drivers/iio/adc/ad7768-1.c +3
-0+ [AD7768_AAF_IN1] = 1000, /* 1.000 */ + [AD7768_AAF_IN2] = 364, /* 0.364 */ + [AD7768_AAF_IN3 ] =143 /* 0.143 */
Please, leave a trailing comma.
Quoted 1 line
+};
...
Quoted 6 lines
static const int ad7768_filter_3db_odr_multiplier[] = {
[AD7768_FILTER_SINC5] = 204, /* 0.204 */
const unsigned long *available_masks;
const struct iio_chan_spec *channel_spec;
int num_channels;drivers/iio/adc/ad7768-1.c struct ad7768_chip_info { · line 284
Quoted 8 lines
drivers/iio/adc/ad7768-1.c +7
-0+const int *pga_gains; +int num_pga_modes; +int default_pga_mode; +int pgia_mode2pin_offset; +bool has_pga; +bool has_variable_aaf; +bool has_vcm_regulator; };
Same comments as per below.
...
drivers/iio/adc/ad7768-1.c struct ad7768_state { · line 303
Quoted 8 lines
struct
ad7768_state{unsigned int samp_freq; unsigned int samp_freq_avail[ARRAY_SIZE(ad7768_mclk_div_rates)]; unsigned int samp_freq_avail_len;drivers/iio/adc/ad7768-1.c +3
-0+int pga_gain_mode; +int aaf_gain; +int scale_tbl[ADAQ776X_MAX_GAIN_MODES ][2 ];
Why all signed?
Quoted 10 lines
struct completion completion; struct iio_trigger *trig; struct gpio_desc *gpio_sync_in; struct gpio_chip gpiochip; const struct ad7768_chip_info *chip; bool en_spi_sync;+ struct mutex
pga_lock; /* protect PGA value access */}
Have you run pahole? Does it suggest a better layout?
...
drivers/iio/adc/ad7768-1.c static int ad7768_reg_access(struct iio_dev *indio_dev, · line 540
Quoted 7 lines
drivers/iio/adc/ad7768-1.c +6
-0+static void ad7768_fill_scale_tbl (struct iio_dev *dev) +{ +struct ad7768_state *st =iio_priv (dev); +const struct iio_scan_type *scan_type; +int val, val2, tmp0, tmp1, i; +unsigned long denominator, numerator;
struct u32_fract fract;
Quoted 15 lines
drivers/iio/adc/ad7768-1.c +15
-0+u64 tmp2; + + scan_type =iio_get_current_scan_type (dev, &dev->channels[0 ]); +if (scan_type->sign =='s' ) + val2 = scan_type->realbits -1 ; +else + val2 = scan_type->realbits; + +for (i =0 ; i < st->chip->num_pga_modes; i++) { +/* Convert gain to a fraction format */ + numerator = st->chip->pga_gains[i]; + denominator =MILLI ; +if (st->chip->has_variable_aaf) { + numerator *= ad7768_aaf_gains[st->aaf_gain]; + denominator *=MILLI ;
drivers/iio/adc/ad7768-1.c static int ad7768_reg_access(struct iio_dev *indio_dev, · line 564
Quoted 10 lines
drivers/iio/adc/ad7768-1.c +9
-0+ } + +rational_best_approximation (numerator, denominator,INT_MAX ,INT_MAX , + &numerator, &denominator); + + val =mult_frac (st->vref_uv, denominator, numerator); +/* Would multiply by NANO here, but value is already in milli */ + tmp2 =shift_right ((u64 )val *MICRO , val2); + tmp0 = (int )div_u64_rem (tmp2,NANO , &tmp1);
Why casting here?
Quoted 4 lines
drivers/iio/adc/ad7768-1.c +4
-0+ st->scale_tbl[i][0 ] = tmp0;/* Integer part */ + st->scale_tbl[i][1 ] =abs (tmp1);/* Fractional part */ + } +}
...
drivers/iio/adc/ad7768-1.c static int ad7768_configure_dig_fil(struct iio_dev *dev, · line 708
Quoted 8 lines
drivers/iio/adc/ad7768-1.c +8
-0+static int ad7768_set_pga_gain (struct ad7768_state *st, +int gain_mode) +{ +int pgia_pins_value =abs (gain_mode - st->chip->pgia_mode2pin_offset); +int check_val; +int ret; + +guard (mutex)(&st->pga_lock);
+ Blank line.
drivers/iio/adc/ad7768-1.c static int ad7768_configure_dig_fil(struct iio_dev *dev, · line 711
Quoted 3 lines
drivers/iio/adc/ad7768-1.c +3
-0+/* Check GPIO control register */ + ret =regmap_read (st->regmap,AD7768_REG_GPIO_CONTROL , &check_val); +if (ret <0 )
Here and elsewhere when it makes sense, drop redundant ' < 0' pieces. It's even inconsistent in the same patch with how you check other regmap returned calls.
Quoted 5 lines
drivers/iio/adc/ad7768-1.c +3
-0+return ret; +return 0 ; +}
...
drivers/iio/adc/ad7768-1.c static int ad7768_read_raw(struct iio_dev *indio_dev, · line 982
Quoted 12 lines
case
IIO_CHAN_INFO_SCALE:drivers/iio/adc/ad7768-1.c +10
-0+if (st->chip->has_pga) { +guard (mutex)(&st->pga_lock); + + *val = st->scale_tbl[st->pga_gain_mode][0 ]; + *val2 = st->scale_tbl[st->pga_gain_mode][1 ]; +return IIO_VAL_INT_PLUS_NANO ; + } + *val = (st->vref_uv *2 ) /1000 ; +if (st->chip->has_variable_aaf) + *val = (*val *MILLI ) / ad7768_aaf_gains[st->aaf_gain];
This is unreadable. Just use a temporary variable for all calculations and when it's done, assign *val to it.
Quoted 3 lines
*val2 = scan_type->realbits;
return IIO_VAL_FRACTIONAL_LOG2;...
drivers/iio/adc/ad7768-1.c static int ad7768_read_avail(struct iio_dev *indio_dev,
Quoted 12 lines
drivers/iio/adc/ad7768-1.c +11
-0+static int ad7768_write_raw_get_fmt (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan,long mask) +{ +switch (mask) { +case IIO_CHAN_INFO_SCALE : +return IIO_VAL_INT_PLUS_NANO ; +default : +return IIO_VAL_INT_PLUS_MICRO ; + } + +return -EINVAL ;
What's the point in this return, please?
Quoted 1 line
+}
...
Quoted 6 lines
static const struct
ad7768_chip_infoad7768_chip_info= {.channel_spec = ad7768_channels, .num_channels = ARRAY_SIZE(ad7768_channels), .available_masks = ad7768_channel_masks,+ .has_vcm_regulator = true
Missing trailing comma. Also same in the below. I believe I have pointed that out in v1.
Quoted 22 lines
(quoted diff) +20
-0+}; + +static const struct ad7768_chip_info adaq7767_chip_info = { + .name = "adaq7767-1", + .channel_spec = ad7768_channels, + .num_channels = ARRAY_SIZE(ad7768_channels), + .available_masks = ad7768_channel_masks, + .has_variable_aaf = true +}; +static const struct ad7768_chip_info adaq7769_chip_info = { + .name = "adaq7769-1", + .channel_spec = adaq776x_channels, + .num_channels = ARRAY_SIZE(adaq776x_channels), + .available_masks = ad7768_channel_masks, + .pga_gains = adaq7769_gains, + .default_pga_mode = AD7768_PGA_GAIN_0, + .num_pga_modes = ARRAY_SIZE(adaq7769_gains), + .pgia_mode2pin_offset = 0, + .has_pga = true, + .has_variable_aaf = true };
...
Quoted 2 lines
init_completion(&st->completion);+
mutex_init(&st->pga_lock);
Perhaps devm_mutex_init()?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Jonathan Cameron
maintainerQuoted 9 lines · On Sun, 24 Aug 2025 01:10:13 -0300 Jonathan Santos…
Add support for ADAQ7767/68/69-1 series, which includes PGIA and Anti-aliasing filter (AAF) gains. Unlike the AD7768-1, they do not provide a VCM regulator interface.
The PGA gain is configured in run-time through the scale attribute, if supported by the device. PGA is enabled and controlled by the GPIO controller (GPIOs 0, 1 and 2) provided by the device with the SPI interface.
Quoted 4 lines
The AAF gain is defined by hardware connections and should be specified in device tree.
Signed-off-by Jonathan Santos <[email protected]>
Hi Jonathan
Various minor comments inline.
Thanks,
Jonathan
Quoted 18 lines
drivers/iio/adc/ad7768-1.c +3
-0struct ad7768_state {@@ -235,6 +298,9 @@ struct ad7768_state {unsigned int samp_freq;unsigned int samp_freq_avail[ARRAY_SIZE (ad7768_mclk_div_rates)];unsigned int samp_freq_avail_len; +int pga_gain_mode; +int aaf_gain; +int scale_tbl[ADAQ776X_MAX_GAIN_MODES ][2 ];struct completion completion;struct iio_trigger *trig;struct gpio_desc *gpio_sync_in;
drivers/iio/adc/ad7768-1.c struct ad7768_state { · line 312
Quoted 4 lines
drivers/iio/adc/ad7768-1.c +1
-0@@ -243,6 +309,7 @@ struct ad7768_state {struct gpio_chip gpiochip;const struct ad7768_chip_info *chip;bool en_spi_sync; +struct mutex pga_lock;/* protect PGA value access */
why? What is special about that value? Is it only state in device? If so I'd go with
/* protect device internal state (PGA) */
Quoted 7 lines
drivers/iio/adc/ad7768-1.c +7
-0+static int ad7768_calc_pga_gain (struct ad7768_state *st,int gain_int, +int gain_fract,int precision) +{ +u64 gain_nano; +int gain_idx; +u32 tmp; +
drivers/iio/adc/ad7768-1.c static int ad7768_configure_dig_fil(struct iio_dev *dev, · line 696
Quoted 10 lines
drivers/iio/adc/ad7768-1.c +10
-0+ gain_nano = gain_int *NANO + gain_fract; + gain_nano =clamp (gain_nano,0 ,ADAQ776X_GAIN_MAX_NANO ); + tmp =DIV_ROUND_CLOSEST_ULL (gain_nano << precision,NANO ); + gain_nano =DIV_ROUND_CLOSEST (st->vref_uv, tmp); +if (st->chip->has_variable_aaf) +/* remove the AAF gain from the overall gain */ + gain_nano =DIV_ROUND_CLOSEST_ULL (gain_nano *MILLI , + ad7768_aaf_gains[st->aaf_gain]); + tmp = st->chip->num_pga_modes; + gain_idx =find_closest (gain_nano, st->chip->pga_gains, tmp);
put tmp inline. It's not adding anything by having it in the local variable where it's not so obvious it is the number of the previous parameter.
drivers/iio/adc/ad7768-1.c static int ad7768_configure_dig_fil(struct iio_dev *dev, · line 709
Quoted 13 lines
drivers/iio/adc/ad7768-1.c +13
-0+ +return gain_idx; +} + +static int ad7768_set_pga_gain (struct ad7768_state *st, +int gain_mode) +{ +int pgia_pins_value =abs (gain_mode - st->chip->pgia_mode2pin_offset); +int check_val; +int ret; + +guard (mutex)(&st->pga_lock); +/* Check GPIO control register */
The code is pretty much saying what that comment does. Drop the comment.
drivers/iio/adc/ad7768-1.c static int ad7768_configure_dig_fil(struct iio_dev *dev, · line 716
Quoted 7 lines
drivers/iio/adc/ad7768-1.c +7
-0+ ret =regmap_read (st->regmap,AD7768_REG_GPIO_CONTROL , &check_val); +if (ret <0 ) +return ret; + +if ((check_val &AD7768_GPIO_PGIA_EN ) !=AD7768_GPIO_PGIA_EN ) { +/* Enable PGIA GPIOs and set them as output */ + ret =regmap_write (st->regmap,AD7768_REG_GPIO_CONTROL ,AD7768_GPIO_PGIA_EN );
Why check it? Just write it and rely on regcache making that only happen once if at all. If you need to be careful about double writing - add a comment.
Quoted 13 lines
drivers/iio/adc/ad7768-1.c +13
-0+if (ret) +return ret; + } + +/* Write the respective gain values to GPIOs 0, 1, 2 */ + ret =regmap_write (st->regmap,AD7768_REG_GPIO_WRITE , +AD7768_GPIO_WRITE (pgia_pins_value)); +if (ret) +return ret; + + st->pga_gain_mode = gain_mode; + +return 0 ;
drivers/iio/adc/ad7768-1.c static int ad7768_configure_dig_fil(struct iio_dev *dev,
Quoted 10 lines
drivers/iio/adc/ad7768-1.c +3
-0+}static int __ad7768_write_raw (struct iio_dev *indio_dev,struct iio_chan_spec const *chan,int val,int val2,long info) {struct ad7768_state *st =iio_priv (indio_dev); +const struct iio_scan_type *scan_type; +int gain_mode;int ret;
Might as well combine ret and gain_mode on one line.
or pus gain_mode down into a more local scope so it's not defined here at all.
Quoted 43 lines
drivers/iio/adc/ad7768-1.c +16
-0+ scan_type =iio_get_current_scan_type (indio_dev, chan); +if (IS_ERR (scan_type)) +return PTR_ERR (scan_type); +switch (info) {case IIO_CHAN_INFO_SAMP_FREQ :return ad7768_set_freq (st, val);@@ -887,6 +1083,18 @@ static int __ad7768_write_raw(struct iio_dev *indio_dev,/* Update sampling frequency */ return ad7768_set_freq (st, st->samp_freq); +case IIO_CHAN_INFO_SCALE : +if (!st->chip->has_pga) +return -EOPNOTSUPP ; + +if (scan_type->sign =='s' ) + gain_mode =ad7768_calc_pga_gain (st, val, val2, + scan_type->realbits -1 ); +else + gain_mode =ad7768_calc_pga_gain (st, val, val2, + scan_type->realbits); + +return ad7768_set_pga_gain (st, gain_mode); static int ad7768_probe(struct spi_device *spi) {drivers/iio/adc/ad7768-1.c +4
-3struct ad7768_state *st;struct iio_dev *indio_dev; +u32 val;int ret; indio_dev =devm_iio_device_alloc (&spi->dev,sizeof (*st));@@ -1409,9 +1653,37 @@ static int ad7768_probe(struct spi_device *spi) indio_dev->modes =INDIO_DIRECT_MODE ;/* Register VCM output regulator */ - ret =ad7768_register_regulators (&spi->dev, st, indio_dev); -if (ret) -return ret; +if (st->chip->has_vcm_regulator) { + ret =ad7768_register_regulators (&spi->dev, st, indio_dev); +if (ret)
drivers/iio/adc/ad7768-1.c static int ad7768_probe(struct spi_device *spi) · line 1668
Quoted 10 lines
drivers/iio/adc/ad7768-1.c +10
-0+return ret; + } + + st->aaf_gain =AD7768_AAF_IN1 ; + ret =device_property_read_u32 (&spi->dev,"adi,gain-milli" , &val); +if (ret && st->chip->has_variable_aaf) +dev_warn (&spi->dev,"AAF gain not specified, using default\n" ); + +if (!ret && !st->chip->has_variable_aaf) +dev_warn (&spi->dev,"AAF gain not supported for %s\n" , st->chip->name);
I'm confused. If you hit this warn, you then go ahead and set it anyway. How does that work?
drivers/iio/adc/ad7768-1.c static int ad7768_probe(struct spi_device *spi) · line 1670
Quoted 2 lines
drivers/iio/adc/ad7768-1.c +2
-0+ +if (!ret) {
use a local bool for ret here with a suitable name. Carrying on using ret is fragile against later code changes where someone doesn't realise it is still in use.
Quoted 16 lines
drivers/iio/adc/ad7768-1.c +16
-0+/* If provided, validate and set the gain */ +switch (val) { +case 1000 : + st->aaf_gain =AD7768_AAF_IN1 ; +break ; +case 364 : + st->aaf_gain =AD7768_AAF_IN2 ; +break ; +case 143 : + st->aaf_gain =AD7768_AAF_IN3 ; +break ; +default : +return dev_err_probe (&spi->dev, -EINVAL , +"Invalid firmware provided AAF gain\n" ); + } + }
Quoted 24 lines · Jonathan Cameron
Quoted 9 lines · On Sun, 24 Aug 2025 01:10:13 -0300 Jonathan Santos…
Add support for ADAQ7767/68/69-1 series, which includes PGIA and Anti-aliasing filter (AAF) gains. Unlike the AD7768-1, they do not provide a VCM regulator interface.
The PGA gain is configured in run-time through the scale attribute, if supported by the device. PGA is enabled and controlled by the GPIO controller (GPIOs 0, 1 and 2) provided by the device with the SPI interface.
Quoted 4 lines
The AAF gain is defined by hardware connections and should be specified in device tree.
Signed-off-by Jonathan Santos <[email protected]>Hi Jonathan
Various minor comments inline.
Thanks,
Jonathan
...
Quoted 12 lines
Quoted 12 lines
drivers/iio/adc/ad7768-1.c +8
-3/* Register VCM output regulator */ - ret =ad7768_register_regulators (&spi->dev, st, indio_dev); -if (ret) -return ret; +if (st->chip->has_vcm_regulator) { + ret =ad7768_register_regulators (&spi->dev, st, indio_dev); +if (ret) +return ret; + } + + st->aaf_gain =AD7768_AAF_IN1 ; + ret =device_property_read_u32 (&spi->dev,"adi,gain-milli" , &val);
Quoted 9 lines
drivers/iio/adc/ad7768-1.c static int ad7768_probe(struct spi_device *spi) · line 1668
Quoted 5 lines
drivers/iio/adc/ad7768-1.c +5
-0+if (ret && st->chip->has_variable_aaf) +dev_warn (&spi->dev,"AAF gain not specified, using default\n" ); + +if (!ret && !st->chip->has_variable_aaf) +dev_warn (&spi->dev,"AAF gain not supported for %s\n" , st->chip->name);I'm confused. If you hit this warn, you then go ahead and set it anyway. How does that work?
I would be setting a variable that will not be used, so no functional impact. I could add another condition to avoid assigning it, or do you believe it is better to return error like before?
Quoted 5 lines
drivers/iio/adc/ad7768-1.c static int ad7768_probe(struct spi_device *spi) · line 1670
Quoted 2 lines
drivers/iio/adc/ad7768-1.c +2
-0+ +if (!ret) {use a local bool for ret here with a suitable name. Carrying on using ret is fragile against later code changes where someone doesn't realise it is still in use.
Maybe it is better to handle this in a helper function and apply the suggestion.
Quoted 8 lines
Quoted 8 lines
drivers/iio/adc/ad7768-1.c +8
-0+/* If provided, validate and set the gain */ +switch (val) { +case 1000 : + st->aaf_gain =AD7768_AAF_IN1 ; +break ; +case 364 : + st->aaf_gain =AD7768_AAF_IN2 ; +break ;
Quoted 8 lines
drivers/iio/adc/ad7768-1.c static int ad7768_probe(struct spi_device *spi) · line 1686
Quoted 8 lines
drivers/iio/adc/ad7768-1.c +8
-0+case 143 : + st->aaf_gain =AD7768_AAF_IN3 ; +break ; +default : +return dev_err_probe (&spi->dev, -EINVAL , +"Invalid firmware provided AAF gain\n" ); + } + }
Regards,
Jonathan S.
Quoted 19 lines · Andy Shevchenko
Quoted 5 lines · Jonathan Santos
Add support for ADAQ7767/68/69-1 series, which includes PGIA and Anti-aliasing filter (AAF) gains. Unlike the AD7768-1, they do not provide a VCM regulator interface.
Quoted 7 lines
The PGA gain is configured in run-time through the scale attribute, if supported by the device. PGA is enabled and controlled by the GPIO controller (GPIOs 0, 1 and 2) provided by the device with the SPI interface.
The AAF gain is defined by hardware connections and should be specified in device tree.
the device
...
...
Quoted 9 lines
Quoted 8 lines
struct completion completion; struct iio_trigger *trig; struct gpio_desc *gpio_sync_in; struct gpio_chip gpiochip; const struct ad7768_chip_info *chip; bool en_spi_sync;+ struct mutex
pga_lock; /* protect PGA value access */
Quoted 6 lines
Quoted 1 line
}
Have you run
pahole? Does it suggest a better layout?...
yes, I have run with the reorganaize option, tried a few combinations, but couldn't found a better arrengement.
Quoted 10 lines
drivers/iio/adc/ad7768-1.c static int ad7768_reg_access(struct iio_dev *indio_dev, · line 540
Quoted 7 lines
drivers/iio/adc/ad7768-1.c +6
-0+static void ad7768_fill_scale_tbl (struct iio_dev *dev) +{ +struct ad7768_state *st =iio_priv (dev); +const struct iio_scan_type *scan_type; +int val, val2, tmp0, tmp1, i; +unsigned long denominator, numerator;struct
u32_fractfract;
Problem is that rational_best_approximation requires a unsigned long. I could do:
rational_best_approximation(fract.numerator, fract.denominator,
INT_MAX, INT_MAX, (unsigned long *)&fract.numerator, (unsigned long *)&fract.denominator);
But I don't know if this is the best practice.
One way around this would be to represent the gains in fractions instead of 1000x, example:
static const int ad7768_gains_frac[][2] = {
...
[AD7768_PGA_GAIN_3] = { 13, 5 }, /* 2.600 */...
};
then we could drop the rational approximation.
Quoted 23 lines
Quoted 23 lines
drivers/iio/adc/ad7768-1.c +23
-0+u64 tmp2; + + scan_type =iio_get_current_scan_type (dev, &dev->channels[0 ]); +if (scan_type->sign =='s' ) + val2 = scan_type->realbits -1 ; +else + val2 = scan_type->realbits; + +for (i =0 ; i < st->chip->num_pga_modes; i++) { +/* Convert gain to a fraction format */ + numerator = st->chip->pga_gains[i]; + denominator =MILLI ; +if (st->chip->has_variable_aaf) { + numerator *= ad7768_aaf_gains[st->aaf_gain]; + denominator *=MILLI ; + } + +rational_best_approximation (numerator, denominator,INT_MAX ,INT_MAX , + &numerator, &denominator); + + val =mult_frac (st->vref_uv, denominator, numerator); +/* Would multiply by NANO here, but value is already in milli */ + tmp2 =shift_right ((u64 )val *MICRO , val2);
...
Quoted 5 lines
Quoted 2 lines
init_completion(&st->completion);+
mutex_init(&st->pga_lock);Perhaps
devm_mutex_init()?
sure!
Quoted 3 lines
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 2 lines · Jonathan Santos
Quoted 1 line · Andy Shevchenko
On Sun, Aug 24, 2025 at 7:10 AM Jonathan Santos
...
Quoted 11 lines
Quoted 3 lines
Quoted 1 line
+ unsigned long denominator, numerator;
struct
u32_fractfract;Problem is that
rational_best_approximationrequires a unsigned long. I could do:rational_best_approximation(fract.numerator, fract.denominator,
INT_MAX,INT_MAX, (unsigned long *)&fract.numerator, (unsigned long *)&fract.denominator);
No, this is not good. And it will overflow. See below for a better alternative.
Quoted 1 line
But I don't know if this is the best practice.
The best practice is to add a patch to extend that to cover unsigned long and long
https://elixir.bootlin.com/linux/v6.17-rc3/source/include/linux/math.h#L122
It's just a couple of lines which I review and acknowledge immediately. (Just make them a precursor patch)
BUT, looking at the code, I don't see that people use the same variables for input and output (otherwise it's also considered not the best practice, for example, in the cases when input can be made a const).
That said, the patching math.h is optional, either way, make sure your input and output are different variables.
Quoted 2 lines
One way around this would be to represent the gains in fractions instead of 1000x, example:
then we could drop the rational approximation.
I like the use of rational best approximation. I think it gives a really nice algo for simplifying fractions.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Jonathan Cameron
maintainerQuoted 48 lines · On Tue, 26 Aug 2025 23:57:44 -0300 Jonathan Santos <[email protected]> wrote:
Quoted 24 lines · Jonathan Cameron
Quoted 9 lines · On Sun, 24 Aug 2025 01:10:13 -0300 Jonathan Santos…
Add support for ADAQ7767/68/69-1 series, which includes PGIA and Anti-aliasing filter (AAF) gains. Unlike the AD7768-1, they do not provide a VCM regulator interface.
The PGA gain is configured in run-time through the scale attribute, if supported by the device. PGA is enabled and controlled by the GPIO controller (GPIOs 0, 1 and 2) provided by the device with the SPI interface.
Quoted 4 lines
The AAF gain is defined by hardware connections and should be specified in device tree.
Signed-off-by Jonathan Santos <[email protected]>Hi Jonathan
Various minor comments inline.
Thanks,
Jonathan
...
Quoted 12 lines
Quoted 12 lines
drivers/iio/adc/ad7768-1.c +8
-3/* Register VCM output regulator */ - ret =ad7768_register_regulators (&spi->dev, st, indio_dev); -if (ret) -return ret; +if (st->chip->has_vcm_regulator) { + ret =ad7768_register_regulators (&spi->dev, st, indio_dev); +if (ret) +return ret; + } + + st->aaf_gain =AD7768_AAF_IN1 ; + ret =device_property_read_u32 (&spi->dev,"adi,gain-milli" , &val);Quoted 9 lines
drivers/iio/adc/ad7768-1.c static int ad7768_probe(struct spi_device *spi) · line 1668
Quoted 5 lines
drivers/iio/adc/ad7768-1.c +5
-0+if (ret && st->chip->has_variable_aaf) +dev_warn (&spi->dev,"AAF gain not specified, using default\n" ); + +if (!ret && !st->chip->has_variable_aaf) +dev_warn (&spi->dev,"AAF gain not supported for %s\n" , st->chip->name);I'm confused. If you hit this warn, you then go ahead and set it anyway. How does that work?
Quoted 3 lines
I would be setting a variable that will not be used, so no functional impact. I could add another condition to avoid assigning it, or do you believe it is better to return error like before?
Just don't set it to avoid confusion. Alternative would be a comment that says that it won't be used, which is probably more complex than the code.
Quoted 9 lines
Quoted 6 lines
drivers/iio/adc/ad7768-1.c static int ad7768_probe(struct spi_device *spi) · line 1670
Quoted 3 lines
drivers/iio/adc/ad7768-1.c +2
-0+ +if (!ret) {use a local bool for ret here with a suitable name. Carrying on using ret is fragile against later code changes where someone doesn't realise it is still in use.
Maybe it is better to handle this in a helper function and apply the suggestion.
If that works, sounds good!
J
Quoted 21 lines
Quoted 17 lines
Quoted 7 lines
drivers/iio/adc/ad7768-1.c +6
-0+/* If provided, validate and set the gain */ +switch (val) { +case 1000 : + st->aaf_gain =AD7768_AAF_IN1 ; +break ; +case 364 :drivers/iio/adc/ad7768-1.c static int ad7768_probe(struct spi_device *spi) · line 1686
Quoted 10 lines
drivers/iio/adc/ad7768-1.c +10
-0+ st->aaf_gain =AD7768_AAF_IN2 ; +break ; +case 143 : + st->aaf_gain =AD7768_AAF_IN3 ; +break ; +default : +return dev_err_probe (&spi->dev, -EINVAL , +"Invalid firmware provided AAF gain\n" ); + } + }Regards,
Jonathan S.