mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Andy Shevchenko <andriy.shevchenko@intel.com>
Cc: "Oleksij Rempel" <o.rempel@pengutronix.de>,
	"Rob Herring" <robh@kernel.org>,
	"Krzysztof Kozlowski" <krzk+dt@kernel.org>,
	"Conor Dooley" <conor+dt@kernel.org>,
	stable@vger.kernel.org, kernel@pengutronix.de,
	linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org,
	devicetree@vger.kernel.org, "Andy Shevchenko" <andy@kernel.org>,
	"David Lechner" <dlechner@baylibre.com>,
	"Nuno Sá" <nuno.sa@analog.com>,
	"David Jander" <david@protonic.nl>
Subject: Re: [PATCH v1 4/8] iio: dac: ds4424: reject -128 RAW value
Date: Fri, 23 Jan 2026 09:33:57 +0000	[thread overview]
Message-ID: <20260123093357.6996154f@jic23-huawei> (raw)
In-Reply-To: <aW6AEszfRQzuHf6j@smile.fi.intel.com>

On Mon, 19 Jan 2026 21:03:46 +0200
Andy Shevchenko <andriy.shevchenko@intel.com> wrote:

> On Mon, Jan 19, 2026 at 07:24:20PM +0100, Oleksij Rempel wrote:
> > The DS442x DAC uses sign-magnitude encoding, so -128 cannot be
> > represented in hardware.
> > 
> > With the previous check, userspace could pass -128, which gets converted
> > to a magnitude of 128 and then truncated by the 7-bit DAC field. This
> > ends up programming a zero magnitude with the sign bit set, i.e. an
> > unintended output (effectively 0 mA instead of -128 steps).
> > 
> > Reject -128 to avoid silently producing the wrong current.  
> 
> ...
> 
> > -		if (val < S8_MIN || val > S8_MAX)
> > +		if (val <= S8_MIN || val > S8_MAX)
> >  			return -EINVAL;  
> 
> Hmm... So the range is [ -127 .. 0 .. 127 ] ?
> 
> I think in such case the plain numbers would be more specific than
> the type related limits.
> 

Check the abs(val) <= 127 given that's what we care about I think?
Or make it explicit and do
FIELD_FIT() against a mask that you then use to fill the register
value (another mask for the sign bit).

Btw use abs(val) to set raw.dx and drop it out of the conditional.
Even better get rid of the bitfield stuff and just add
two defines + fill val directly in this function using FIELD_PREP().
Then both the checking and the field filling use the same defines
and it should be easy to see what is going on.

Jonathan




  reply	other threads:[~2026-01-23  9:34 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-19 18:24 [PATCH v1 0/8] iio: dac: ds4424: add DS4402/DS4404 support and scale Oleksij Rempel
2026-01-19 18:24 ` [PATCH v1 1/8] dt-bindings: iio: dac: maxim,ds4424: add ds4402/ds4404 Oleksij Rempel
2026-01-19 18:28   ` Conor Dooley
2026-01-19 18:24 ` [PATCH v1 2/8] dt-bindings: iio: dac: maxim,ds4424: add maxim,rfs-ohms property Oleksij Rempel
2026-01-19 18:27   ` Conor Dooley
2026-01-23  9:28     ` Jonathan Cameron
2026-01-23 10:03       ` Oleksij Rempel
2026-01-25 10:22         ` Jonathan Cameron
2026-01-26  9:54           ` Oleksij Rempel
2026-01-20 10:39   ` Krzysztof Kozlowski
2026-01-19 18:24 ` [PATCH v1 3/8] iio: dac: ds4424: add DS4402/DS4404 device IDs Oleksij Rempel
2026-01-19 18:24 ` [PATCH v1 4/8] iio: dac: ds4424: reject -128 RAW value Oleksij Rempel
2026-01-19 19:03   ` Andy Shevchenko
2026-01-23  9:33     ` Jonathan Cameron [this message]
2026-01-26  9:52       ` Oleksij Rempel
2026-01-19 18:24 ` [PATCH v1 5/8] iio: dac: ds4424: add Rfs-based scale and per-variant limits Oleksij Rempel
2026-01-19 19:11   ` Andy Shevchenko
2026-01-19 18:24 ` [PATCH v1 6/8] iio: dac: ds4424: clear outputs on probe Oleksij Rempel
2026-01-19 19:12   ` Andy Shevchenko
2026-01-23  9:38   ` Jonathan Cameron
2026-01-23  9:59     ` Oleksij Rempel
2026-01-19 18:24 ` [PATCH v1 7/8] iio: dac: ds4424: ratelimit read errors and use device context Oleksij Rempel
2026-01-19 19:14   ` Andy Shevchenko
2026-01-19 18:24 ` [PATCH v1 8/8] iio: dac: ds4424: document output sign and probe verification Oleksij Rempel

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=20260123093357.6996154f@jic23-huawei \
    --to=jic23@kernel.org \
    --cc=andriy.shevchenko@intel.com \
    --cc=andy@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=david@protonic.nl \
    --cc=devicetree@vger.kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=kernel@pengutronix.de \
    --cc=krzk+dt@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nuno.sa@analog.com \
    --cc=o.rempel@pengutronix.de \
    --cc=robh@kernel.org \
    --cc=stable@vger.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

all inboxes | Powered by JetHome®