devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jonathan Santos <jonath4nns@gmail.com>
To: Andy Shevchenko <andy.shevchenko@gmail.com>
Cc: Jonathan Santos <Jonathan.Santos@analog.com>,
	linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org,
	devicetree@vger.kernel.org, Michael.Hennerich@analog.com,
	lars@metafoo.de, jic23@kernel.org, dlechner@baylibre.com,
	nuno.sa@analog.com, andy@kernel.org, robh@kernel.org,
	krzk+dt@kernel.org, conor+dt@kernel.org,
	marcelo.schmitt1@gmail.com
Subject: Re: [PATCH v2 4/4] iio: adc: ad7768-1: add support for ADAQ776x-1 ADC Family
Date: Fri, 29 Aug 2025 16:11:24 -0300	[thread overview]
Message-ID: <aLH7XNuaZ2CkQVWo@JSANTO12-L01.ad.analog.com> (raw)
In-Reply-To: <CAHp75VcGG_h+wpo7hHL=ERYqbrvvAaufwPAYBsEbRn3dB8-dfA@mail.gmail.com>

On 08/24, Andy Shevchenko wrote:
> On Sun, Aug 24, 2025 at 7:10 AM Jonathan Santos
> <Jonathan.Santos@analog.com> wrote:
> >
> > Add support for ADAQ7767/68/69-1 series, which includes PGIA and
> > Anti-aliasing filter (AAF) gains. Unlike the AD7768-1, they do not
> > provide a VCM regulator interface.
> >
> > The PGA gain is configured in run-time through the scale attribute,
> > if supported by the device. PGA is enabled and controlled by the GPIO
> > controller (GPIOs 0, 1 and 2) provided by the device with the SPI
> > interface.
> >
> > The AAF gain is defined by hardware connections and should be specified
> > in device tree.
> 
> the device
> 
> ...
> 

...

> >         struct completion completion;
> >         struct iio_trigger *trig;
> >         struct gpio_desc *gpio_sync_in;
> 
> >         struct gpio_chip gpiochip;
> >         const struct ad7768_chip_info *chip;
> >         bool en_spi_sync;
> > +       struct mutex pga_lock; /* protect PGA value access */
> 
> >  }
> 
> Have you run `pahole`? Does it suggest a better layout?
> 
> ...
> 
yes, I have run with the reorganaize option, tried a few combinations, but couldn't found a better arrengement.

> > +static void ad7768_fill_scale_tbl(struct iio_dev *dev)
> > +{
> > +       struct ad7768_state *st = iio_priv(dev);
> > +       const struct iio_scan_type *scan_type;
> > +       int val, val2, tmp0, tmp1, i;
> 
> > +       unsigned long denominator, numerator;
> 
> struct u32_fract fract;
> 

Problem is that rational_best_approximation requires a unsigned long. I
could do:

rational_best_approximation(fract.numerator, fract.denominator,
					    INT_MAX, INT_MAX,
					    (unsigned long *)&fract.numerator,
					    (unsigned long *)&fract.denominator);

But I don't know if this is the best practice.

One way around this would be to represent the gains in fractions instead
of 1000x, example:

static const int ad7768_gains_frac[][2] = {
...
	[AD7768_PGA_GAIN_3] = { 13, 5 }, /* 2.600 */
...
};

then we could drop the rational approximation.

> > +       u64 tmp2;
> > +
> > +       scan_type = iio_get_current_scan_type(dev, &dev->channels[0]);
> > +       if (scan_type->sign == 's')
> > +               val2 = scan_type->realbits - 1;
> > +       else
> > +               val2 = scan_type->realbits;
> > +
> > +       for (i = 0; i < st->chip->num_pga_modes; i++) {
> > +               /* Convert gain to a fraction format */
> > +               numerator = st->chip->pga_gains[i];
> > +               denominator = MILLI;
> > +               if (st->chip->has_variable_aaf) {
> > +                       numerator *= ad7768_aaf_gains[st->aaf_gain];
> > +                       denominator *= MILLI;
> > +               }
> > +
> > +               rational_best_approximation(numerator, denominator, INT_MAX, INT_MAX,
> > +                                           &numerator, &denominator);
> > +
> > +               val = mult_frac(st->vref_uv, denominator, numerator);
> > +               /* Would multiply by NANO here, but value is already in milli */
> > +               tmp2 = shift_right((u64)val * MICRO, val2);
...
> >         init_completion(&st->completion);
> > +       mutex_init(&st->pga_lock);
> 
> Perhaps devm_mutex_init()?
> 

sure!

> -- 
> With Best Regards,
> Andy Shevchenko

  reply	other threads:[~2025-08-29 19:11 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-24  4:10 [PATCH v2 4/4] iio: adc: ad7768-1: add support for ADAQ776x-1 ADC Family Jonathan Santos
2025-08-24 19:43 ` Andy Shevchenko
2025-08-29 19:11   ` Jonathan Santos [this message]
2025-08-30  4:54     ` Andy Shevchenko
2025-08-25 14:44 ` Jonathan Cameron
2025-08-27  2:57   ` Jonathan Santos
2025-08-31 12:30     ` 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=aLH7XNuaZ2CkQVWo@JSANTO12-L01.ad.analog.com \
    --to=jonath4nns@gmail.com \
    --cc=CAHp75VcGG_h+wpo7hHL=ERYqbrvvAaufwPAYBsEbRn3dB8-dfA@mail.gmail.com \
    --cc=Jonathan.Santos@analog.com \
    --cc=Michael.Hennerich@analog.com \
    --cc=andy.shevchenko@gmail.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=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=marcelo.schmitt1@gmail.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;
as well as URLs for NNTP newsgroup(s).