[PATCH 3/5] iio: dac: ad5758: Fix the offset calculation

From: Arka Mondal

Date: Fri Sep 18 2026 - 08:56:48 EST


The offset is a count of DAC codes, so the microamp or microvolt units
of min and max cancel out and the final division by 1000 is wrong. For
the +-20 mA range the driver reports -32 instead of -32768. For the
+-5 V and +-10 V ranges min * (1 << 16) also overflows int, and the
reported offset is 0.

Drop the division and do the multiplication in 64 bits.

Fixes: 28d1a7ac2a0d ("iio: dac: Add AD5758 support")
Signed-off-by: Arka Mondal <arka@xxxxxxxxxxxxxx>
---

Notes:
Compile tested only; no relevant hardware available.

drivers/iio/dac/ad5758.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/dac/ad5758.c b/drivers/iio/dac/ad5758.c
index 2a9e907b5eb8..1e21ad2a050d 100644
--- a/drivers/iio/dac/ad5758.c
+++ b/drivers/iio/dac/ad5758.c
@@ -9,6 +9,7 @@
#include <linux/bsearch.h>
#include <linux/delay.h>
#include <linux/kernel.h>
+#include <linux/math64.h>
#include <linux/module.h>
#include <linux/property.h>
#include <linux/spi/spi.h>
@@ -540,7 +541,7 @@ static int ad5758_read_raw(struct iio_dev *indio_dev,
case IIO_CHAN_INFO_OFFSET:
min = st->out_range.min;
max = st->out_range.max;
- *val = ((min * (1 << 16)) / (max - min)) / 1000;
+ *val = div_s64((s64)min * (1 << 16), max - min);
return IIO_VAL_INT;
default:
return -EINVAL;
--
2.55.0