From: Jonathan Cameron <Jonathan.Cameron@huawei.com>
To: "Uwe Kleine-König" <u.kleine-koenig@baylibre.com>
Cc: Antoniu Miclaus <antoniu.miclaus@analog.com>, <jic23@kernel.org>,
<robh@kernel.org>, <conor+dt@kernel.org>,
<linux-iio@vger.kernel.org>, <devicetree@vger.kernel.org>,
<linux-kernel@vger.kernel.org>, <linux-pwm@vger.kernel.org>
Subject: Re: [PATCH v9 8/8] iio: adc: ad4851: add ad485x driver
Date: Fri, 17 Jan 2025 15:43:09 +0000 [thread overview]
Message-ID: <20250117154309.000003e1@huawei.com> (raw)
In-Reply-To: <d4ur7trhknm7jtjvsyms4aewypl75uuvgtccgwc7dfycheh4qo@jqmpv5t3lip6>
On Fri, 17 Jan 2025 10:59:04 +0100
Uwe Kleine-König <u.kleine-koenig@baylibre.com> wrote:
> Hello,
>
> On Fri, Dec 20, 2024 at 02:01:34PM +0200, Antoniu Miclaus wrote:
> > +static const int ad4851_oversampling_ratios[] = {
> > + 1, 2, 4, 8, 16, 32, 64, 128,
> > + 256, 512, 1024, 2048, 4096, 8192, 16384, 32768,
> > + 65536,
> > +};
> > +
> > +static int ad4851_osr_to_regval(unsigned int ratio)
> > +{
> > + int i;
> > +
> > + for (i = 1; i < ARRAY_SIZE(ad4851_oversampling_ratios); i++)
> > + if (ratio == ad4851_oversampling_ratios[i])
> > + return i - 1;
> > +
> > + return -EINVAL;
> > +}
>
> This can be simplified (I guess) using something like:
>
> if (ratio >= 2 && ratio <= 65536 && is_power_of_2(ratio))
> return ilog2(ratio) - 1;
>
> return -EINVAL;
Hi Uwe,
Only at the cost of providing custom handling to compute the above
array in order to provide it to userspace via the read_avail() callback.
We could do what you have here and provide the array but that would
be less clear than just looking it up.
>
> > +static void __ad4851_get_scale(struct iio_dev *indio_dev, int scale_tbl,
> > + unsigned int *val, unsigned int *val2)
> > +{
> > [...]
> > +}
> > +
> > +static int ad4851_scale_fill(struct iio_dev *indio_dev)
> > +{
> > [...]
> > +}
> > +
> > +static int ad4851_set_oversampling_ratio(struct iio_dev *indio_dev,
> > + const struct iio_chan_spec *chan,
> > + unsigned int osr)
> > +{
> > [...]
> > +}
> > +
> > +static int ad4851_get_oversampling_ratio(struct ad4851_state *st, unsigned int *val)
> > +{
> > + unsigned int osr;
> > + int ret;
> > +
> > + guard(mutex)(&st->lock);
> > +
> > + ret = regmap_read(st->regmap, AD4851_REG_OVERSAMPLE, &osr);
> > + if (ret)
> > + return ret;
> > +
> > + if (!FIELD_GET(AD4851_OS_EN_MSK, osr))
> > + *val = 1;
> > + else
> > + *val = ad4851_oversampling_ratios[FIELD_GET(AD4851_OS_RATIO_MSK, osr) + 1];
>
> With the suggestion above this gets:
>
> *val = 2 << FIELD_GET(AD4851_OS_RATIO_MSK, osr);
>
> (or
> *val = 1 << (FIELD_GET(AD4851_OS_RATIO_MSK, osr) + 1);
>
> ). Then you can drop ad4851_oversampling_ratios[].
You missed the usage in as4851_read_avail() which is the reason it exists.
These others are just convenient given that it already exists.
Jonathan
>
> > +
> > + st->osr = *val;
> > +
> > + return IIO_VAL_INT;
> > +}
>
> Best regards
> Uwe
>
next prev parent reply other threads:[~2025-01-17 15:43 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-12-20 12:01 [PATCH v9 1/8] iio: backend: add API for interface get Antoniu Miclaus
2024-12-20 12:01 ` [PATCH v9 2/8] iio: backend: add support for data size set Antoniu Miclaus
2024-12-20 12:01 ` [PATCH v9 3/8] iio: backend: add API for oversampling Antoniu Miclaus
2024-12-20 12:01 ` [PATCH v9 4/8] iio: adc: adi-axi-adc: add interface type Antoniu Miclaus
2024-12-20 12:01 ` [PATCH v9 5/8] iio: adc: adi-axi-adc: set data format Antoniu Miclaus
2024-12-20 12:01 ` [PATCH v9 6/8] iio: adc: adi-axi-adc: add oversampling Antoniu Miclaus
2024-12-20 12:01 ` [PATCH v9 7/8] dt-bindings: iio: adc: add ad4851 Antoniu Miclaus
2024-12-23 11:40 ` Jonathan Cameron
2025-01-08 16:48 ` David Lechner
2024-12-20 12:01 ` [PATCH v9 8/8] iio: adc: ad4851: add ad485x driver Antoniu Miclaus
2024-12-23 12:00 ` Jonathan Cameron
2025-01-08 17:06 ` David Lechner
2025-01-08 23:24 ` David Lechner
2025-01-14 12:01 ` Miclaus, Antoniu
2025-01-14 13:20 ` Jonathan Cameron
2025-01-14 15:51 ` David Lechner
2025-01-17 9:59 ` Uwe Kleine-König
2025-01-17 15:43 ` Jonathan Cameron [this message]
2024-12-23 11:33 ` [PATCH v9 1/8] iio: backend: add API for interface get 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=20250117154309.000003e1@huawei.com \
--to=jonathan.cameron@huawei.com \
--cc=antoniu.miclaus@analog.com \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=jic23@kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pwm@vger.kernel.org \
--cc=robh@kernel.org \
--cc=u.kleine-koenig@baylibre.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.