Linux GPIO subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Pufan Jin" <cyan2254650260@gmail.com>
Cc: linux-gpio@vger.kernel.org
Subject: Re: [PATCH v2 1/5] gpiolib: add gpio_device_find_by_fwnode_args()
Date: Sat, 05 Dec 2026 07:07:18 +0000	[thread overview]
Message-ID: <sashiko-outbox-251189@kernel.org> (raw)
In-Reply-To: <20261205064355.137581-2-cyan2254650260@gmail.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] gpiolib: of_gpiochip_match_and_xlate() masks -EPROBE_DEFER
  with -EINVAL
- [Medium] gpiolib: gpio_device_find_by_fwnode_args() dispatches on
  provider fwnode
- [Low] gpiolib: of_gpiochip_match_and_xlate() casts away const on find
  data
--

commit e01202920088285e29b396ea7dcc2688a01bf0d4
Author: Pufan Jin <cyan2254650260@gmail.com>

gpiolib: add gpio_device_find_by_fwnode_args()

This commit adds gpio_device_find_by_fwnode_args() to resolve parsed
fwnode_reference_args into a struct gpio_device, chip-relative hardware
offset, and Linux lookup flags for OF and software nodes.

> diff --git a/drivers/gpio/gpiolib-of.c b/drivers/gpio/gpiolib-of.c
> index 9fc4eb7aee8ec..dd8a4b4e0508f 100644
> --- a/drivers/gpio/gpiolib-of.c
> +++ b/drivers/gpio/gpiolib-of.c

[ ... ]

> @@ -779,6 +797,71 @@ struct gpio_desc *of_gpiochip_get_hog_desc(struct gpio_chip *chip,
>  	return desc;
>  }
>  
> +struct of_gpio_xlate_ctx {
> +	const struct of_phandle_args *gpiospec;
> +	unsigned int offset;
> +	enum of_gpio_flags of_flags;
> +	int err;
> +};
> +
> +static int of_gpiochip_match_and_xlate(struct gpio_chip *gc, const void *data)
> +{
> +	struct of_gpio_xlate_ctx *ctx = (void *)data;

[Severity: Low]
Should of_gpiochip_match_and_xlate() cast away const on data to pass
results out of gpio_device_find()?

Commit faf6efd2e5e2 ("gpio: constify opaque pointer in gpio_device_find()
match function") constified that parameter to keep match callbacks
side-effect free, following 762c2e46c059 ("gpio: of: remove
of_gpiochip_and_xlate() and struct gg_data").

Reusing of_find_gpio_device_by_xlate() and translating under gdev->srcu
would keep matching pure and avoid duplicating the OF matching logic.

[ ... ]

> +	ret = gc->of_xlate(gc, ctx->gpiospec, &of_flags);
> +	if (ret < 0) {
> +		ctx->err = ret;
> +		return 0;
> +	}

[ ... ]

> +	gdev = gpio_device_find(&ctx, of_gpiochip_match_and_xlate);
> +	if (!gdev)
> +		return ERR_PTR(ctx.err);

[Severity: Medium]
Can overwriting ctx->err here mask -EPROBE_DEFER with -EINVAL on
multi-bank OF GPIO controllers?

Drivers like spacemit_gpio_probe() register multiple gpio_chips against one
OF node in a loop, and of_gpio_threecell_xlate() returns -EINVAL for pins
on another bank.

If a consumer probes after bank 0 registers but before the target bank
does, bank 0 overwrites ctx.err with -EINVAL and the consumer fails
permanently instead of deferring.  of_get_named_gpiod_flags() avoids this
by returning -EPROBE_DEFER whenever of_find_gpio_device_by_xlate() fails.

[ ... ]

> diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
> index fe2d67c5f8a47..aeed9322ef88f 100644
> --- a/drivers/gpio/gpiolib.c
> +++ b/drivers/gpio/gpiolib.c
> @@ -1586,6 +1586,38 @@ struct gpio_device *gpio_device_find_by_fwnode(const struct fwnode_handle *fwnod
>  }
>  EXPORT_SYMBOL_GPL(gpio_device_find_by_fwnode);

[ ... ]

> +struct gpio_device *
> +gpio_device_find_by_fwnode_args(const struct fwnode_reference_args *args,
> +				unsigned int *offset, unsigned long *lflags)
> +{
> +	if (!args || !args->fwnode)
> +		return ERR_PTR(-EINVAL);
> +
> +	if (is_of_node(args->fwnode))
> +		return of_gpio_device_find_by_fwnode_args(args, offset, lflags);
> +
> +	if (is_software_node(args->fwnode))
> +		return swnode_gpio_device_find_by_fwnode_args(args, offset, lflags);
> +
> +	return ERR_PTR(-ENOENT);
> +}

[Severity: Medium]
Should gpio_device_find_by_fwnode_args() dispatch on the consumer fwnode
rather than the provider node args->fwnode?

Software-node GPIO references can target OF or ACPI GPIO controllers (as
supported by software_node_get_reference_args() and
swnode_get_gpio_device()) while still encoding Linux enum gpio_lookup_flags.

Dispatching on args->fwnode misroutes a software-node reference to an OF
controller into of_gpio_device_find_by_fwnode_args(), where
of_convert_gpio_flags() turns GPIO_OPEN_DRAIN (0x2, matching
OF_GPIO_SINGLE_ENDED) into GPIO_OPEN_SOURCE and drops GPIO_OPEN_SOURCE.

It also returns -ENOENT for software-node references targeting an ACPI
controller.  Matching gpiod_find_by_fwnode() by passing the consumer fwnode
to this exported API would select the right translator.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205064355.137581-1-cyan2254650260@gmail.com?part=1

  reply	other threads:[~2026-12-05  7:07 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05  6:43 [PATCH v2 0/5] gpiolib / reset: resolve GPIO specifiers via gpiolib Pufan Jin
2026-12-05  6:43 ` [PATCH v2 1/5] gpiolib: add gpio_device_find_by_fwnode_args() Pufan Jin
2026-12-05  7:07   ` sashiko-bot [this message]
2026-12-05  6:43 ` [PATCH v2 2/5] gpiolib: swnode: prefer matching parent gpio_device Pufan Jin
2026-12-05  6:43 ` [PATCH v2 3/5] gpiolib: shared: match reset-gpio against translated entry offset Pufan Jin
2026-12-05  7:18   ` sashiko-bot
2026-12-05  6:43 ` [PATCH v2 4/5] reset: gpio: derive fwnode_reset_n_cells from platform data Pufan Jin
2026-12-05  6:43 ` [PATCH v2 5/5] reset: core: translate GPIO specifier via gpio_device_find_by_fwnode_args() Pufan Jin
2026-12-05  7:33   ` sashiko-bot

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=sashiko-outbox-251189@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=cyan2254650260@gmail.com \
    --cc=linux-gpio@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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