Previously, the AD4030 driver was using the number of scan realbits for the voltage channel to derive the scale to millivolts. Though, when sample averaging is enabled (oversampling_ratio > 1), the number of scan realbits for the channel is set to 30 and doesn't match the amount of conversion precision bits. Due to that, the calculated channel scale did not correctly scale raw sample data to millivolt units in those cases. Use chip specific precision bits to derive the correct channel _scale on every and all channel configuration. Fixes: dc78e71d7c15 ("iio: adc: ad4030: remove some duplicate code") Signed-off-by: Marcelo Schmitt <marcelo.schmitt@xxxxxxxxxx> --- This was probalby buggy since commit 949abd1ca5a4 ("iio: adc: ad4030: add averaging support") but I decided to set the fixes tag with dc78e71d7c15 because this patch will not apply cleanly over 949abd1ca5a4. drivers/iio/adc/ad4030.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/drivers/iio/adc/ad4030.c b/drivers/iio/adc/ad4030.c index 1bc2f9a22470..82784593f976 100644 --- a/drivers/iio/adc/ad4030.c +++ b/drivers/iio/adc/ad4030.c @@ -394,7 +394,14 @@ static int ad4030_get_chan_scale(struct iio_dev *indio_dev, else *val = st->vref_uv / MILLI; - *val2 = scan_type->realbits; + /* + * Even though the sample data comes in a 30-bit chunk when the ADC + * is averaging samples, the conversion precision is still 16-bit or + * 24-bit depending on the device. Thus, instead of scan_type->realbits, + * use chip specific precision bits to derive the correct scale to mV. + */ + *val2 = scan_type->realbits == 30 ? st->chip->precision_bits + : scan_type->realbits; return IIO_VAL_FRACTIONAL_LOG2; } -- 2.39.2