Re: [PATCH 5/6] gpiolib: consolidate GPIO lookups

Dmitry Torokhov
2022-11-05T04:57:12Z

On Fri, Nov 04, 2022 at 11:06:58PM +0200, Andy Shevchenko wrote:
> On Fri, Nov 04, 2022 at 11:52:26AM -0700, Dmitry Torokhov wrote:
> > On Fri, Nov 04, 2022 at 07:17:27PM +0200, Andy Shevchenko wrote:
> > > On Thu, Nov 03, 2022 at 11:10:15PM -0700, Dmitry Torokhov wrote:
>
> ...
>
> > > > +static struct gpio_desc *gpiod_find_by_fwnode(struct fwnode_handle *fwnode,
> > > > + struct device *consumer,
> > > > + const char *con_id,
> > > > + unsigned int idx,
> > > > + enum gpiod_flags *flags,
> > > > + unsigned long *lookupflags)
> > > > {
> > >
> > > > + struct gpio_desc *desc = ERR_PTR(-ENOENT);
> > >
> > > No need, just return directly.
> > >
> > > > + dev_dbg(consumer, "GPIO lookup for consumer %s in node '%s'\n",
> > > > + con_id, fwnode_get_name(fwnode));
> > >
> > > %pfwP ?
> >
> > OK. Although, I think I like %pfw (without 'P') better as it gives
> > results like:
> >
> > /soc/i2c@11007000/edp-bridge@8
> >
> > or
> >
> > \_SB.PCI0.I2C1.D010
> >
> > which should help identifying the exact node.
>
> I agree.
>
> > > > + /* Using device tree? */
> > > > if (is_of_node(fwnode)) {
> > > > + dev_dbg(consumer, "using device tree for GPIO lookup\n");
> > > > + desc = of_find_gpio(to_of_node(fwnode),
> > > > + con_id, idx, lookupflags);
> > > > } else if (is_acpi_node(fwnode)) {
> > >
> > > With direct return, no need for 'else' here.
> >
> > When we have several branches of equal weight I prefer not to have
> > early/inline returns, but I can add:
> >
> > } else {
> > desc = ERR_PTR(-ENOENT);
> > }
> >
> > at the end, what do you think?
>
> No strong opinion here.
>
> > > > + dev_dbg(consumer, "using ACPI for GPIO lookup\n");
> > > > + desc = acpi_find_gpio(fwnode, con_id, idx, flags, lookupflags);
> > > > }
> > > >
> > > > + return desc;
> > > > +}
>
> ...
>
> > > > + struct gpio_desc *desc = ERR_PTR(-ENOENT);
> > >
> > > We can get rid of the assignment, see below.
>
> Still below another thought which affects this.
>
> > > > + if (fwnode)
> > >
> > > Do we need this check?
> >
> > Yes, I would prefer to have it as it clearly informs the reader that we
> > are only doing lookup by node if we actually have a node.
> >
> > gpiod_find_and_request() expects that it gets a valid node and in the
> > followup change it will be dereferencing fwnode without checking for
> > NULL-ness.
>
> But most of the code will check for the NULL anyway. The exceptions are
> dev_dbg() and accessing to the secondary fwnode.

I think it is just a matter of what I want to express through source. I
want to show that the device might not have fwnode, and that we only
descend into gpiod_find_by_fwnode() in cases where we actually have
fwnode.

>
> > > Debug message above (when %pfw is used) would be even useful when
> > > fwnode == NULL.
>
> > > > + desc = gpiod_find_by_fwnode(fwnode, consumer, con_id, idx,
> > > > + &flags, &lookupflags);
>
> Looking into drivers/base/property.c makes me realize that you might need to
> test for error pointer as well.
>
> Perhaps something like
>
> if (IS_ERR_OR_NULL(fwnode))
> return ERR_PTR(-ENOENT);
>
> in the gpiod_find_by_fwnode() needs to be added. Can you check this?

No, only fwnode->secondary pointer can be PTR_ERR()-encoded.

From comment to set_primary_fwnode() in drivers/base/core.c

* Valid fwnode cases are:
* - primary --> secondary --> -ENODEV
* - primary --> NULL
* - secondary --> -ENODEV
* - NULL

I do not believe we should be concerned about someone passing secondary
pointers from fwnodes directly into gpiolib.

Thanks.

--
Dmitry