Devicetree
 help / color / mirror / Atom feed
From: Arka Mondal <arka@arkamondal.net>
To: "Jonathan Cameron" <jic23@kernel.org>,
	"Nuno Sá" <nuno.sa@analog.com>,
	"Michael Hennerich" <Michael.Hennerich@analog.com>
Cc: David Lechner <dlechner@baylibre.com>,
	Andy Shevchenko <andy@kernel.org>, Rob Herring <robh@kernel.org>,
	Krzysztof Kozlowski <krzk+dt@kernel.org>,
	Conor Dooley <conor+dt@kernel.org>,
	linux@analog.com, linux-iio@vger.kernel.org,
	devicetree@vger.kernel.org, linux-kernel@vger.kernel.org,
	arkamondalofficial@gmail.com, Arka Mondal <arka@arkamondal.net>
Subject: [PATCH 3/5] iio: dac: ad5758: Fix the offset calculation
Date: Fri, 18 Sep 2026 21:20:30 +0900	[thread overview]
Message-ID: <20260918122032.134947-4-arka@arkamondal.net> (raw)
In-Reply-To: <20260918122032.134947-1-arka@arkamondal.net>

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@arkamondal.net>
---

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


  parent reply	other threads:[~2026-09-18 12:21 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-18 12:20 [PATCH 0/5] iio: dac: ad5758: fix DMA alignment, raw range, offset and 4-20 mA Arka Mondal
2026-09-18 12:20 ` [PATCH 1/5] iio: dac: ad5758: Fix alignment for DMA safety Arka Mondal
2026-09-18 12:20 ` [PATCH 2/5] iio: dac: ad5758: Reject out-of-range raw values Arka Mondal
2026-09-18 12:20 ` Arka Mondal [this message]
2026-09-18 12:20 ` [PATCH 4/5] dt-bindings: iio: dac: adi,ad5758: Fix the 4 mA to 20 mA current range Arka Mondal
2026-09-20 18:39   ` Jonathan Cameron
2026-09-28 19:34     ` Rob Herring
2026-10-04 16:27       ` Jonathan Cameron
2026-09-18 12:20 ` [PATCH 5/5] iio: dac: ad5758: " Arka Mondal
2026-09-18 15:22 ` [PATCH 0/5] iio: dac: ad5758: fix DMA alignment, raw range, offset and 4-20 mA Nuno Sá
2026-09-20 18:41   ` Jonathan Cameron
2026-09-27 20:52     ` Jonathan Cameron

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260918122032.134947-4-arka@arkamondal.net \
    --to=arka@arkamondal.net \
    --cc=Michael.Hennerich@analog.com \
    --cc=andy@kernel.org \
    --cc=arkamondalofficial@gmail.com \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=jic23@kernel.org \
    --cc=krzk+dt@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@analog.com \
    --cc=nuno.sa@analog.com \
    --cc=robh@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox