mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Jernej Škrabec" <jernej.skrabec@gmail.com>
To: Linus Walleij <linusw@kernel.org>, linux-sunxi@lists.linux.dev
Cc: Chen-Yu Tsai <wens@kernel.org>,
	Samuel Holland <samuel@sholland.org>,
	linux-gpio@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
	linux-sunxi@lists.linux.dev, linux-kernel@vger.kernel.org,
	Yang Siyuan <hsyysy@163.com>
Subject: Re: [PATCH v3 RESEND] pinctrl: sunxi: request the per-bank supply optionally
Date: Tue, 17 Nov 2026 17:17:25 +0100	[thread overview]
Message-ID: <uNcaWoiORlCb9uOBOQgmvw@gmail.com> (raw)
In-Reply-To: <20261117091944.12082-1-hsyysy@163.com>

Dne torek, 17. november 2026 ob 10:19:44 Srednjeevropski standardni čas je Yang Siyuan napisal(a):
> The per-bank "vcc-pX" supplies are optional in the device tree: the
> binding documents them, but notes that many boards do not describe them.
> sunxi_pmx_request() uses regulator_get() anyway, so the regulator core
> falls back to a dummy regulator and warns for every bank that has no
> supply:
> 
>   sun50i-h616-r-pinctrl 7022000.pinctrl: supply vcc-pl not found, using
>   dummy regulator
> 
> The H616 R_PIO is such a controller, and its PL0/PL1 pins drive the
> R_I2C bus, so every H616 board hits the warning during boot.
> 
> Use of_regulator_get_optional() instead.  A bank supply that is described
> in the device tree is still acquired and enabled, as it powers the
> physical VCC-Px rail, but one that is absent is skipped without a
> warning.
> 
> The OF-only lookup matters here: regulator_get_optional() also falls back
> to a global regulator-name lookup.  If a regulator with the same name as
> an absent bank supply is registered later, a subsequent pin request in
> that bank could acquire it and start the per-bank refcount at one, after
> earlier pins in the bank had already been requested without a regulator.
> Freeing any of those pins would then disable the supply while other pins
> in the bank are still active.  Restricting the lookup to the pinctrl node
> makes an absent supply permanent; record that state so later pin requests
> do not retry the lookup.  The refcount therefore stays balanced.
> 
> Signed-off-by: Yang Siyuan <hsyysy@163.com>
> ---
> 
> Changes in v3:
> - Use of_regulator_get_optional() instead of regulator_get_optional().
>   The latter still performs a global regulator-name lookup, which can
>   bind an unrelated regulator that appears after some pins were already
>   requested and unbalance the per-bank refcount.
> - Remember that a bank has no supply so later pin requests skip the
>   lookup, and keep the existing no-regulator check in sunxi_pmx_free().

Remembering IMO isn't worth it. It complicates the code which runs only
at probe time.

Best regards,
Jernej

> - Link to v2: https://lore.kernel.org/linux-gpio/20261026152027.1417178-1-hsyysy@163.com/
> - Link to v1: https://lore.kernel.org/linux-gpio/20261026144751.1392715-1-hsyysy@163.com/
> ---
> ---
>  drivers/pinctrl/sunxi/pinctrl-sunxi.c | 29 +++++++++++++++++++++++++--
>  drivers/pinctrl/sunxi/pinctrl-sunxi.h |  1 +
>  2 files changed, 28 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/pinctrl/sunxi/pinctrl-sunxi.c b/drivers/pinctrl/sunxi/pinctrl-sunxi.c
> index 70099052d..d2345a85a 100644
> --- a/drivers/pinctrl/sunxi/pinctrl-sunxi.c
> +++ b/drivers/pinctrl/sunxi/pinctrl-sunxi.c
> @@ -913,23 +913,44 @@ static int sunxi_pmx_request(struct pinctrl_dev *pctldev, unsigned offset)
>  					    PINS_PER_BANK;
>  	struct sunxi_pinctrl_regulator *s_reg = &pctl->regulators[bank_offset];
>  	struct regulator *reg = s_reg->regulator;
> +	struct device_node *np = dev_of_node(pctl->dev);
>  	char supply[16];
>  	int ret;
>  
>  	if (WARN_ON_ONCE(bank_offset >= ARRAY_SIZE(pctl->regulators)))
>  		return -EINVAL;
>  
> +	if (s_reg->supply_absent)
> +		return 0;
> +
>  	if (reg) {
>  		refcount_inc(&s_reg->refcount);
>  		return 0;
>  	}
>  
> +	if (!np) {
> +		s_reg->supply_absent = true;
> +		return 0;
> +	}
> +
>  	snprintf(supply, sizeof(supply), "vcc-p%c", 'a' + bank);
> -	reg = regulator_get(pctl->dev, supply);
> -	if (IS_ERR(reg))
> +	/*
> +	 * Use the OF-only lookup: regulator_get_optional() would also match
> +	 * a same-named regulator from another device.  Such a regulator can
> +	 * appear after some pins in this bank have already been requested,
> +	 * which would unbalance the per-bank refcount.
> +	 */
> +	reg = of_regulator_get_optional(pctl->dev, np, supply);
> +	if (IS_ERR(reg)) {
> +		if (PTR_ERR(reg) == -ENODEV) {
> +			s_reg->supply_absent = true;
> +			return 0;
> +		}
> +
>  		return dev_err_probe(pctl->dev, PTR_ERR(reg),
>  				     "Couldn't get bank P%c regulator\n",
>  				     'A' + bank);
> +	}
>  
>  	ret = regulator_enable(reg);
>  	if (ret) {
> @@ -959,6 +980,10 @@ static int sunxi_pmx_free(struct pinctrl_dev *pctldev, unsigned offset)
>  					    PINS_PER_BANK;
>  	struct sunxi_pinctrl_regulator *s_reg = &pctl->regulators[bank_offset];
>  
> +	/* The per-bank supply is optional, so this bank may not have one. */
> +	if (!s_reg->regulator)
> +		return 0;
> +
>  	if (!refcount_dec_and_test(&s_reg->refcount))
>  		return 0;
>  
> diff --git a/drivers/pinctrl/sunxi/pinctrl-sunxi.h b/drivers/pinctrl/sunxi/pinctrl-sunxi.h
> index 8bd00c6ff..651249b23 100644
> --- a/drivers/pinctrl/sunxi/pinctrl-sunxi.h
> +++ b/drivers/pinctrl/sunxi/pinctrl-sunxi.h
> @@ -162,6 +162,7 @@ struct sunxi_pinctrl_group {
>  struct sunxi_pinctrl_regulator {
>  	struct regulator	*regulator;
>  	refcount_t		refcount;
> +	bool			supply_absent;
>  };
>  
>  struct sunxi_pinctrl {
> base-commit: 091b06388b117fefdcb63336ac09956a4914b961
> 





      reply	other threads:[~2026-11-17 16:17 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17  9:19 Yang Siyuan
2026-11-17 16:17 ` Jernej Škrabec [this message]

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=uNcaWoiORlCb9uOBOQgmvw@gmail.com \
    --to=jernej.skrabec@gmail.com \
    --cc=hsyysy@163.com \
    --cc=linusw@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sunxi@lists.linux.dev \
    --cc=samuel@sholland.org \
    --cc=wens@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

all inboxes | Powered by JetHome®