From: "Nuno Sá" <noname.nuno@gmail.com>
To: Andy Shevchenko <andriy.shevchenko@intel.com>
Cc: "David Lechner (TI)" <dlechner@baylibre.com>,
"Jonathan Cameron" <jic23@kernel.org>,
"Nuno Sá" <nuno.sa@analog.com>,
"Andy Shevchenko" <andy@kernel.org>,
"Rob Herring" <robh@kernel.org>,
"Krzysztof Kozlowski" <krzk+dt@kernel.org>,
"Conor Dooley" <conor+dt@kernel.org>,
"Kurt Borja" <kuurtb@gmail.com>,
"Nguyen Minh Tien" <zizuzacker@gmail.com>,
linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH 3/4] iio: adc: ti-ads112c14: implement gain on internal short SYS_MON channel
Date: Tue, 16 Jun 2026 11:03:20 +0100 [thread overview]
Message-ID: <ajEeZWj8JWoJi1oP@nsa> (raw)
In-Reply-To: <ajECNvjntVg_GjwR@ashevche-desk.local>
On Tue, Jun 16, 2026 at 10:58:46AM +0300, Andy Shevchenko wrote:
> On Mon, Jun 15, 2026 at 05:00:01PM -0500, David Lechner (TI) wrote:
> > Implement support for the programmable gain amplifier on the internal
> > short SYS_MON channel. This channel is used for calibration, so it is
> > useful to be able to set the PGA to the same gain as the external
> > channels. The gain setting is implemented via the `_scale` attribute.
> >
> > In the future, we may want to support different reference voltages for
> > this channel, so the scale_available table is populated during probe
> > rather than being a static table.
>
...
> > +
> > + for (i = 0; i < ARRAY_SIZE(ads112c14_pga_gains_x10); i++) {
> > + int *scale_avail = &data->sys_mon_chan_short_scale_available[i][0];
> > + u32 gain_x10 = ads112c14_pga_gains_x10[i];
> > +
> > + /* NB: slightly odd arrangement to avoid overflow. */
> > + scale_avail[0] = div_u64_rem(div_u64((u64)NANO * 10 /
> > + (MICRO / MILLI) * vref_uV /
> > + gain_x10,
> > + BIT(fsr_bits)),
> > + NANO, &scale_avail[1]);
>
> Oh, what about temporary variable for the inner division? Also note one trick
> to avoid casting (and making it shorter).
>
> u64 foo;
>
> foo = div_u64(10ULL * NANO / (MICRO / MILLI) * vref_uV / gain_x10, BIT(fsr_bits));
>
> /* NB: slightly odd arrangement to avoid overflow. */
> scale_avail[0] = div_u64_rem(foo, NANO, &scale_avail[1]);
>
> Now, with much more readability, it's visible that the first division is just a right shift.
>
> u64 foo;
>
> /* ...a comment to explain voodoo calculations... */
> foo = (10ULL * NANO / (MICRO / MILLI) * vref_uV / gain_x10) >> fsr_bits;
Did not looked enough time but also looks like maybe mul_u64_u32_shr()
could be used? And given that it handles things nicely (even uses 128bit
when possible), might open the door for more simplifications.
Anyways, agree with Andy that in the current patch it's a bit of a pain
to the eyes :).
- Nuno Sá
>
> scale_avail[0] = div_u64_rem(foo, NANO, &scale_avail[1]);
>
> > + }
> > +}
>
> --
> With Best Regards,
> Andy Shevchenko
>
>
next prev parent reply other threads:[~2026-06-16 10:02 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-15 21:59 [PATCH 0/4] iio: adc: new ti-ads112c14 driver David Lechner (TI)
2026-06-15 21:59 ` [PATCH 1/4] dt-bindings: iio: adc: add ti,ads122c14 David Lechner (TI)
2026-06-15 22:10 ` sashiko-bot
2026-06-16 0:26 ` Kurt Borja
2026-06-15 22:00 ` [PATCH 2/4] iio: adc: add ti-ads112c14 driver David Lechner (TI)
2026-06-15 22:11 ` sashiko-bot
2026-06-16 7:32 ` Andy Shevchenko
2026-06-15 22:00 ` [PATCH 3/4] iio: adc: ti-ads112c14: implement gain on internal short SYS_MON channel David Lechner (TI)
2026-06-15 22:14 ` sashiko-bot
2026-06-16 7:58 ` Andy Shevchenko
2026-06-16 10:03 ` Nuno Sá [this message]
2026-06-15 22:00 ` [PATCH 4/4] iio: adc: ti-ads112c14: add measurement channel support David Lechner (TI)
2026-06-15 22:13 ` sashiko-bot
2026-06-16 8:36 ` Andy Shevchenko
2026-06-16 0:18 ` [PATCH 0/4] iio: adc: new ti-ads112c14 driver Kurt Borja
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=ajEeZWj8JWoJi1oP@nsa \
--to=noname.nuno@gmail.com \
--cc=andriy.shevchenko@intel.com \
--cc=andy@kernel.org \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=dlechner@baylibre.com \
--cc=jic23@kernel.org \
--cc=krzk+dt@kernel.org \
--cc=kuurtb@gmail.com \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=nuno.sa@analog.com \
--cc=robh@kernel.org \
--cc=zizuzacker@gmail.com \
/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