All of lore.kernel.org
 help / color / mirror / Atom feed
From: Gwendal Grignou <gwendal@chromium.org>
To: Stephen Boyd <swboyd@chromium.org>
Cc: Daniel Campello <campello@chromium.org>,
	Jonathan Cameron <jic23@kernel.org>,
	Lars-Peter Clausen <lars@metafoo.de>,
	linux-iio <linux-iio@vger.kernel.org>
Subject: Re: [PATCH] iio: sx9310: Support ACPI property
Date: Fri, 5 Feb 2021 13:49:12 -0800	[thread overview]
Message-ID: <CAPUE2uu0y8pRmCHxhLQ3Ca2oAMJ0ihwAHfUdHNBOUNS-hZhmdA@mail.gmail.com> (raw)
In-Reply-To: <161229346726.76967.648868878998146729@swboyd.mtv.corp.google.com>

On Tue, Feb 2, 2021 at 11:17 AM Stephen Boyd <swboyd@chromium.org> wrote:
>
> Quoting Gwendal Grignou (2021-02-01 22:45:41)
> > Use device_property_read_... to support both device tree and ACPI
> > bindings.
> >
> > Add support for variable array per documentation
> > ("iio/proximity/semtech,sx9310.yaml").
> >
> > Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
> > ---
> >  drivers/iio/proximity/sx9310.c | 36 ++++++++++++++++++++--------------
> >  1 file changed, 21 insertions(+), 15 deletions(-)
> >
> > diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
> > index 37fd0b65a0140..1a8a441c9774d 100644
> > --- a/drivers/iio/proximity/sx9310.c
> > +++ b/drivers/iio/proximity/sx9310.c
> > @@ -1213,31 +1214,36 @@ static int sx9310_init_compensation(struct iio_dev *indio_dev)
> >  }
> >
> >  static const struct sx9310_reg_default *
> > -sx9310_get_default_reg(struct sx9310_data *data, int i,
> > +sx9310_get_default_reg(struct device *dev, int i,
> >                        struct sx9310_reg_default *reg_def)
> >  {
> > -       int ret;
> > -       const struct device_node *np = data->client->dev.of_node;
> > +       int ret, count;
> >         u32 combined[SX9310_NUM_CHANNELS] = { 4, 4, 4, 4 };
> >         unsigned long comb_mask = 0;
> >         const char *res;
> >         u32 start = 0, raw = 0, pos = 0;
> >
> >         memcpy(reg_def, &sx9310_default_regs[i], sizeof(*reg_def));
> > -       if (!np)
> > -               return reg_def;
> > -
> >         switch (reg_def->reg) {
> >         case SX9310_REG_PROX_CTRL2:
> > -               if (of_property_read_bool(np, "semtech,cs0-ground")) {
> > +               if (device_property_read_bool(dev, "semtech,cs0-ground")) {
> >                         reg_def->def &= ~SX9310_REG_PROX_CTRL2_SHIELDEN_MASK;
> >                         reg_def->def |= SX9310_REG_PROX_CTRL2_SHIELDEN_GROUND;
> >                 }
> >
> >                 reg_def->def &= ~SX9310_REG_PROX_CTRL2_COMBMODE_MASK;
> > -               of_property_read_u32_array(np, "semtech,combined-sensors",
> > -                                          combined, ARRAY_SIZE(combined));
> > -               for (i = 0; i < ARRAY_SIZE(combined); i++) {
> > +               count = device_property_read_u32_array(dev,
> > +                               "semtech,combined-sensors", NULL, 0);
> > +               if (count > 0 && count <= ARRAY_SIZE(combined))
> > +                       ret = device_property_read_u32_array(dev,
> > +                                       "semtech,combined-sensors", combined,
> > +                                       count);
> > +               else
> > +                       ret = -EINVAL;
> > +               if (ret)
> > +                       count = ARRAY_SIZE(combined);
>
> I wish this could be written simpler. Unfortunately there isn't any sort
> of for_each_device_property() iterator macro like we have with
> of_property_for_each_u32(). Or device_property_read_u32_array() can be
> OK if the length of the property doesn't exceed the size of the
> 'combined' array?
device_property_read_u32_array(...,nval) calls
acpi_data_get_property(..., nval) in ACPI case.
If nval > obj->package.count, then -EOVERFLOW is returned.
Therefore count can not be to SX9310_NUM_CHANNELS, in case
combined-sensors is only 3 entries or less.

This method of asking first for the number of element and a second
time for the values is already used at different places in the kernel:
drivers/power/supply/gpio-charger.c : see init_charge_current_limit()
or  madera_get_variable_u32_array insound/soc/codecs/madera.c.

However, it could use device_property_count_u32(...), which is more
readable than device_property_count_u32(..., NULL,0).

Gwendal.


>
> > +
> > +               for (i = 0; i < count; i++) {
> >                         if (combined[i] <= SX9310_NUM_CHANNELS)
> >                                 comb_mask |= BIT(combined[i]);
> >                 }
>
> Otherwise
>
> Reviewed-by: Stephen Boyd <swboyd@chromium.org>

  reply	other threads:[~2021-02-05 21:51 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-02  6:45 [PATCH] iio: sx9310: Support ACPI property Gwendal Grignou
2021-02-02 19:17 ` Stephen Boyd
2021-02-05 21:49   ` Gwendal Grignou [this message]
2021-02-06  1:41     ` Stephen Boyd
2021-02-09  2:36       ` Gwendal Grignou
2021-02-09  2:38         ` Stephen Boyd
2021-02-09  2:53           ` Stephen Boyd
2021-02-09  4:07             ` Gwendal Grignou
2021-02-09  6:56               ` Stephen Boyd
2021-02-09  3:00           ` Gwendal Grignou

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=CAPUE2uu0y8pRmCHxhLQ3Ca2oAMJ0ihwAHfUdHNBOUNS-hZhmdA@mail.gmail.com \
    --to=gwendal@chromium.org \
    --cc=campello@chromium.org \
    --cc=jic23@kernel.org \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=swboyd@chromium.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 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.