All of lore.kernel.org
 help / color / mirror / Atom feed
* pinctrl: single: Cannot disable bias - PIN_CONFIG_BIAS_DISABLE not handled
@ 2024-02-28 12:04 Matthijs Kooijman
  2024-02-29  7:32 ` Tony Lindgren
  0 siblings, 1 reply; 13+ messages in thread
From: Matthijs Kooijman @ 2024-02-28 12:04 UTC (permalink / raw)
  To: linux-gpio; +Cc: Haojian Zhuang, Tony Lindgren

[-- Attachment #1: Type: text/plain, Size: 6189 bytes --]

Hi,

While figuring out why bias-setting on the beaglebone black did not
work, I noticed what looks like a bug in the pinctrl-single driver,
preventing it from disabling a bias once enabled.

The short version is: It handles pin_config_set by looking up the requested
setting in a DT-defined lookup table, which defines what bits correspond to
each setting. For PIN_CONFIG_BIAS_DISABLE, this never works since that setting
is never defined in the lookup table (so the code that exists to handle this
setting is never actually executed).


For the longer version: The pcs_pinconf_set() function handles setting
various settings, including PIN_CONFIG_BIAS_DISABLE,
PIN_CONFIG_BIAS_PULL_DOWN and PIN_CONFIG_BIAS_PULL_UP.

The relevant bits of code look like this (full code at [1]):

	static int pcs_pinconf_set(struct pinctrl_dev *pctldev, unsigned pin, unsigned long *configs, unsigned num_configs) {
		...
		ret = pcs_get_function(pctldev, pin, &func);
		...
		for (j = 0; j < num_configs; j++) {
			for (i = 0; i < func->nconfs; i++) {
				if (pinconf_to_config_param(configs[j]) != func->conf[i].param)
					continue;

				...
				switch (func->conf[i].param) {
				...
				case PIN_CONFIG_BIAS_DISABLE:
					pcs_pinconf_clear_bias(pctldev, pin);
					break;
				case PIN_CONFIG_BIAS_PULL_DOWN:
				case PIN_CONFIG_BIAS_PULL_UP:
				...
				}
				pcs->write(data, pcs->base + offset);


				break;
			}
			...

Functionally, this requests the table of supported configurations for
the to-be-configured pin using pcs_get_function(). This per-pin (group) table
is populated through setting e.g. pinctrl-single,bias-pullup in the device tree
(see [2]). However, this table never contains entries for
PIN_CONFIG_BIAS_DISABLE (see pcs_parse_pinconf() which populates the table), so
the `if (pinconf_to_config_param(configs[j]) != func->conf[i].param)` line
above is the never true, and the handling for PIN_CONFIG_BIAS_DISABLE never
runs. This is probably because in an earlier version of this code, there
was a separate DT entry for PIN_CONFIG_BIAS_DISABLE, see discussion
links below.

It seems this code has been broken since it was first introduced in
commit 9dddb4df90d1 (pinctrl: single: support generic pinconf).

An obvious fix for this would be to lift the handling for
PIN_CONFIG_BIAS_DISABLE out of the inner loop, running that instead of
the inner loop in that case.


Note that I have not verified my analysis of the code by testing. I did
confirm that if I define the (what I think is) proper DT definitions for
bias control for the beaglebone black board, I can enable bias, but not
disable them, but I cannot rule out that that problem has a different
cause. I also do not have a good toolchain setup for compiling custom
kernels for this setup (and limited time for setting up one), so I do
not expect to be able to provide more testing or a patch anytime soon.


I also wonder about the design in general - currently the DT defines 4 values
for both pullup and pulldown: value, enable, disable, mask. Of these, I think
value is not actually used. Mask is used to clear all relevant bits before
setting a value and enable/disable are bits to set for either state.
PIN_CONFIG_BIAS_DISABLE is implemented by disabling both the pullup and
the pulldown.

This expressivity allows expressing a situation where the pullup and
pulldown can be both enabled at the same time, and a pullup can be
disabled by passing 0 to PIN_CONFIG_BIAS_PULL_UP or using
PIN_CONFIG_BIAS_DISABLE. However, in practice, enabling both is often
not supported by hardware, and even if it is, it makes no sense
electrically. Also, I think that in practice this API is never used as
such, for example gpiolib.c enforces only one of these is set, and
always uses PIN_CONFIG_BIAS_DISABLE to disable a bias. The only docs
I could find about this, was in pinconf-generic.h, which says:

   * @PIN_CONFIG_BIAS_DISABLE: disable any pin bias on the pin, a
   *      transition from say pull-up to pull-down implies that you disable
   *      pull-up in the process, this setting disables all biasing.

Suggesting that it is indeed never possible to have pullup and pulldown
enabled at the same time.

For the pinctrl-single driver, it would IMHO make more sense to define
three DT properties, for pullup, pulldown and bias-disabled with a value
and mask each, that can be used when each of these settings is enabled.
If a pullup or pulldown is separately disabled (by calling e.g.
PIN_CONFIG_BIAS_PULL_UP with a zero value), this would just apply the
"bias-disabled" value. This is slightly different from the current
behavior for hardware that has separate pullup/pulldown bits (and no
shared enable bit), but matches the behavior of hardware with a shared
pullup/down enable bit (so callers cannot rely on independent state for
the pullup and pulldown anyway).


Looking at the history of the patch that added this code, it seems an
earlier vdrsion has exactly the implementation that I suggested above
(with a pullup/pulldown/disable attribute with a value and mask, and
additonally also a match value for getting):

https://lore.kernel.org/all/20130121171436.GI15361@atomide.com/T/#ma87c4da5183ca8a10c0d6fe72c9a1fffd206e40e

Then it was changed to the current implementation, seemingly with an
argument that having a "PIN_CONFIG_BIAS_ENABLE" API would be more
consistent than "PIN_CONFIG_BIAS_DISABLE", but then also concluding then
that the enable API is actually redundant if you can also disable pullup
and pulldown separately, and finally settling for
a PIN_CONFIG_BIAS_DISABLE after all (since an existing pinctrl driver
turned out to have it), but without the DT configuration for that.

It could very well be that I'm missing some more complex behavior or API that
needs this, so consider this an open question from a somewhat outsider
perspective, and feel free to ignore it because changing this will need
effort and does not add much except maybe some clarity in code.

[1]: https://github.com/torvalds/linux/blob/45ec2f5f6ed3ec3a79ba1329ad585497cdcbe663/drivers/pinctrl/pinctrl-single.c#L548
[2]: https://www.kernel.org/doc/Documentation/devicetree/bindings/pinctrl/pinctrl-single.txt

Gr.

Matthijs

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: pinctrl: single: Cannot disable bias - PIN_CONFIG_BIAS_DISABLE not handled
  2024-02-28 12:04 pinctrl: single: Cannot disable bias - PIN_CONFIG_BIAS_DISABLE not handled Matthijs Kooijman
@ 2024-02-29  7:32 ` Tony Lindgren
  2024-02-29 18:31   ` Nathan Chancellor
  2024-03-19 11:05   ` Matthijs Kooijman
  0 siblings, 2 replies; 13+ messages in thread
From: Tony Lindgren @ 2024-02-29  7:32 UTC (permalink / raw)
  To: Matthijs Kooijman, linux-gpio, Haojian Zhuang; +Cc: Nathan Chancellor

Hi,

* Matthijs Kooijman <matthijs@stdin.nl> [240228 12:04]:
> While figuring out why bias-setting on the beaglebone black did not
> work, I noticed what looks like a bug in the pinctrl-single driver,
> preventing it from disabling a bias once enabled.
> 
> The short version is: It handles pin_config_set by looking up the requested
> setting in a DT-defined lookup table, which defines what bits correspond to
> each setting. For PIN_CONFIG_BIAS_DISABLE, this never works since that setting
> is never defined in the lookup table (so the code that exists to handle this
> setting is never actually executed).

OK

> It seems this code has been broken since it was first introduced in
> commit 9dddb4df90d1 (pinctrl: single: support generic pinconf).
> 
> An obvious fix for this would be to lift the handling for
> PIN_CONFIG_BIAS_DISABLE out of the inner loop, running that instead of
> the inner loop in that case.

Maybe post a suggested patch for Haojian to look at?

> Note that I have not verified my analysis of the code by testing. I did
> confirm that if I define the (what I think is) proper DT definitions for
> bias control for the beaglebone black board, I can enable bias, but not
> disable them, but I cannot rule out that that problem has a different
> cause. I also do not have a good toolchain setup for compiling custom
> kernels for this setup (and limited time for setting up one), so I do
> not expect to be able to provide more testing or a patch anytime soon.

The buildall script should allow you to easily build a cross compiler
on pretty much any current Linux host. I think this is the current
git tree for it:

https://github.com/nathanchance/buildall

Seems like buildall should be hosted at kernel.org but I don't know the
details, so adding Nathan to Cc too.

Regards,

Tony

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: pinctrl: single: Cannot disable bias - PIN_CONFIG_BIAS_DISABLE not handled
  2024-02-29  7:32 ` Tony Lindgren
@ 2024-02-29 18:31   ` Nathan Chancellor
  2024-02-29 19:07     ` Matthijs Kooijman
  2024-03-01 10:04     ` Tony Lindgren
  2024-03-19 11:05   ` Matthijs Kooijman
  1 sibling, 2 replies; 13+ messages in thread
From: Nathan Chancellor @ 2024-02-29 18:31 UTC (permalink / raw)
  To: Tony Lindgren; +Cc: Matthijs Kooijman, linux-gpio, Haojian Zhuang

On Thu, Feb 29, 2024 at 09:32:11AM +0200, Tony Lindgren wrote:
> > cause. I also do not have a good toolchain setup for compiling custom
> > kernels for this setup (and limited time for setting up one), so I do
> > not expect to be able to provide more testing or a patch anytime soon.
> 
> The buildall script should allow you to easily build a cross compiler
> on pretty much any current Linux host. I think this is the current
> git tree for it:
> 
> https://github.com/nathanchance/buildall

That is actually a fork of
http://git.infradead.org/users/segher/buildall.git from Segher
Boessenkool, which appears to no longer exist, so good thing I did that
:)

> Seems like buildall should be hosted at kernel.org but I don't know the
> details, so adding Nathan to Cc too.

For what it's worth, there are prebuilt GCC and LLVM toolchains on
kernel.org that should work with the majority of distros on aarch64 and
x86_64 hosts.

https://mirrors.edge.kernel.org/pub/tools/crosstool/
https://mirrors.edge.kernel.org/pub/tools/llvm/

Arnd maintains the GCC ones, I maintain the LLVM ones.

Cheers,
Nathan

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: pinctrl: single: Cannot disable bias - PIN_CONFIG_BIAS_DISABLE not handled
  2024-02-29 18:31   ` Nathan Chancellor
@ 2024-02-29 19:07     ` Matthijs Kooijman
  2024-03-01 10:04     ` Tony Lindgren
  1 sibling, 0 replies; 13+ messages in thread
From: Matthijs Kooijman @ 2024-02-29 19:07 UTC (permalink / raw)
  To: Nathan Chancellor; +Cc: Tony Lindgren, linux-gpio, Haojian Zhuang

[-- Attachment #1: Type: text/plain, Size: 964 bytes --]

Hey folks,

On Thu, Feb 29, 2024 at 11:31:15AM -0700, Nathan Chancellor wrote:
> On Thu, Feb 29, 2024 at 09:32:11AM +0200, Tony Lindgren wrote:
> > > cause. I also do not have a good toolchain setup for compiling custom
> > > kernels for this setup (and limited time for setting up one), so I do
> > > not expect to be able to provide more testing or a patch anytime soon.

Thanks for the suggestions for buildall, but when I said "toolchain",
I did not really refer to the actual gcc toolchain (I have that from
Debian packages), I meant more like knowing what kernel tree is used on
the board I am using (I've asked the maintainer of the image, no reply
yet), what kernel configuration to use, figuring out the right way to
deploy the kernel for that particular board. Maybe I should have used
workflow rather than "toolchain".


Wrt the suggestion made earlier to submit a patch even if untested - I'll
see if I can whip something up next week.


Gr.

Matthijs

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: pinctrl: single: Cannot disable bias - PIN_CONFIG_BIAS_DISABLE not handled
  2024-02-29 18:31   ` Nathan Chancellor
  2024-02-29 19:07     ` Matthijs Kooijman
@ 2024-03-01 10:04     ` Tony Lindgren
  1 sibling, 0 replies; 13+ messages in thread
From: Tony Lindgren @ 2024-03-01 10:04 UTC (permalink / raw)
  To: Nathan Chancellor; +Cc: Matthijs Kooijman, linux-gpio, Haojian Zhuang

* Nathan Chancellor <nathan@kernel.org> [240229 18:31]:
> On Thu, Feb 29, 2024 at 09:32:11AM +0200, Tony Lindgren wrote:
> > > cause. I also do not have a good toolchain setup for compiling custom
> > > kernels for this setup (and limited time for setting up one), so I do
> > > not expect to be able to provide more testing or a patch anytime soon.
> > 
> > The buildall script should allow you to easily build a cross compiler
> > on pretty much any current Linux host. I think this is the current
> > git tree for it:
> > 
> > https://github.com/nathanchance/buildall
> 
> That is actually a fork of
> http://git.infradead.org/users/segher/buildall.git from Segher
> Boessenkool, which appears to no longer exist, so good thing I did that
> :)

Yes, no idea what happened to the original git repo.

> > Seems like buildall should be hosted at kernel.org but I don't know the
> > details, so adding Nathan to Cc too.
> 
> For what it's worth, there are prebuilt GCC and LLVM toolchains on
> kernel.org that should work with the majority of distros on aarch64 and
> x86_64 hosts.
> 
> https://mirrors.edge.kernel.org/pub/tools/crosstool/
> https://mirrors.edge.kernel.org/pub/tools/llvm/
> 
> Arnd maintains the GCC ones, I maintain the LLVM ones.

OK great thanks for the links.

Regards,

Tony

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: pinctrl: single: Cannot disable bias - PIN_CONFIG_BIAS_DISABLE not handled
  2024-02-29  7:32 ` Tony Lindgren
  2024-02-29 18:31   ` Nathan Chancellor
@ 2024-03-19 11:05   ` Matthijs Kooijman
  2024-03-19 11:06     ` [PATCH] pinctrl: single: Fix PIN_CONFIG_BIAS_DISABLE handling Matthijs Kooijman
  2024-03-22  6:24     ` pinctrl: single: Cannot disable bias - PIN_CONFIG_BIAS_DISABLE not handled Tony Lindgren
  1 sibling, 2 replies; 13+ messages in thread
From: Matthijs Kooijman @ 2024-03-19 11:05 UTC (permalink / raw)
  To: Tony Lindgren; +Cc: linux-gpio, Haojian Zhuang

[-- Attachment #1: Type: text/plain, Size: 641 bytes --]

Hi Tony,

> > An obvious fix for this would be to lift the handling for
> > PIN_CONFIG_BIAS_DISABLE out of the inner loop, running that instead of
> > the inner loop in that case.
>
> Maybe post a suggested patch for Haojian to look at?

I've created a patch (will send in a followup) to do exactly this.

I've compile-tested it but have not been able to runtime test it (I
managed to compile and run a mainline kernel for the BBB, but ran into
problems setting up the bias control devicetree). For now, I'll just
leave the patch here for review, maybe I'll find some time also runtime
test it in the future (but likely not).

Gr.

Matthijs

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH] pinctrl: single: Fix PIN_CONFIG_BIAS_DISABLE handling
  2024-03-19 11:05   ` Matthijs Kooijman
@ 2024-03-19 11:06     ` Matthijs Kooijman
  2024-03-28 21:02       ` Linus Walleij
                         ` (2 more replies)
  2024-03-22  6:24     ` pinctrl: single: Cannot disable bias - PIN_CONFIG_BIAS_DISABLE not handled Tony Lindgren
  1 sibling, 3 replies; 13+ messages in thread
From: Matthijs Kooijman @ 2024-03-19 11:06 UTC (permalink / raw)
  To: Haojian Zhuang, Tony Lindgren
  Cc: linux-gpio, Linus Walleij, linux-omap, Matthijs Kooijman

The pinctrl-single driver handles pin_config_set by looking up the
requested setting in a DT-defined lookup table, which defines what bits
correspond to each setting. There is no way to add
PIN_CONFIG_BIAS_DISABLE entries to the table, since there is instead
code to disable the bias by applying the disable values of both the
pullup and pulldown entries in the table.

However, this code is inside the table-lookup loop, so it would only
execute if there is an entry for PIN_CONFIG_BIAS_DISABLE in the table,
which can never exist, so this code never runs.

This commit lifts the offending code out of the loop, so it just
executes directly whenever PIN_CONFIG_BIAS_DISABLE is requested,
skippipng the table lookup loop.

This also introduces a new `param` variable to make the code slightly
more readable.

This bug seems to have existed when this code was first merged in commit
9dddb4df90d13 ("pinctrl: single: support generic pinconf"). Earlier
versions of this patch did have an entry for PIN_CONFIG_BIAS_DISABLE in
the lookup table, but that was removed, which is probably how this bug
was introduced.

Signed-off-by: Matthijs Kooijman <matthijs@stdin.nl>
---
 drivers/pinctrl/pinctrl-single.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/drivers/pinctrl/pinctrl-single.c b/drivers/pinctrl/pinctrl-single.c
index 19cc0db771a5a..c7a03b63fa812 100644
--- a/drivers/pinctrl/pinctrl-single.c
+++ b/drivers/pinctrl/pinctrl-single.c
@@ -554,21 +554,30 @@ static int pcs_pinconf_set(struct pinctrl_dev *pctldev,
 	unsigned offset = 0, shift = 0, i, data, ret;
 	u32 arg;
 	int j;
+	enum pin_config_param param;
 
 	ret = pcs_get_function(pctldev, pin, &func);
 	if (ret)
 		return ret;
 
 	for (j = 0; j < num_configs; j++) {
+		param = pinconf_to_config_param(configs[j]);
+
+		/* BIAS_DISABLE has no entry in the func->conf table */
+		if (param == PIN_CONFIG_BIAS_DISABLE) {
+			/* This just disables all bias entries */
+			pcs_pinconf_clear_bias(pctldev, pin);
+			continue;
+		}
+
 		for (i = 0; i < func->nconfs; i++) {
-			if (pinconf_to_config_param(configs[j])
-				!= func->conf[i].param)
+			if (param != func->conf[i].param)
 				continue;
 
 			offset = pin * (pcs->width / BITS_PER_BYTE);
 			data = pcs->read(pcs->base + offset);
 			arg = pinconf_to_config_argument(configs[j]);
-			switch (func->conf[i].param) {
+			switch (param) {
 			/* 2 parameters */
 			case PIN_CONFIG_INPUT_SCHMITT:
 			case PIN_CONFIG_DRIVE_STRENGTH:
@@ -580,9 +589,6 @@ static int pcs_pinconf_set(struct pinctrl_dev *pctldev,
 				data |= (arg << shift) & func->conf[i].mask;
 				break;
 			/* 4 parameters */
-			case PIN_CONFIG_BIAS_DISABLE:
-				pcs_pinconf_clear_bias(pctldev, pin);
-				break;
 			case PIN_CONFIG_BIAS_PULL_DOWN:
 			case PIN_CONFIG_BIAS_PULL_UP:
 				if (arg)
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: pinctrl: single: Cannot disable bias - PIN_CONFIG_BIAS_DISABLE not handled
  2024-03-19 11:05   ` Matthijs Kooijman
  2024-03-19 11:06     ` [PATCH] pinctrl: single: Fix PIN_CONFIG_BIAS_DISABLE handling Matthijs Kooijman
@ 2024-03-22  6:24     ` Tony Lindgren
  2024-03-29  2:06       ` Haojian Zhuang
  1 sibling, 1 reply; 13+ messages in thread
From: Tony Lindgren @ 2024-03-22  6:24 UTC (permalink / raw)
  To: Matthijs Kooijman, linux-gpio, Haojian Zhuang

* Matthijs Kooijman <matthijs@stdin.nl> [240319 11:05]:
> Hi Tony,
> 
> > > An obvious fix for this would be to lift the handling for
> > > PIN_CONFIG_BIAS_DISABLE out of the inner loop, running that instead of
> > > the inner loop in that case.
> >
> > Maybe post a suggested patch for Haojian to look at?
> 
> I've created a patch (will send in a followup) to do exactly this.

OK great thanks!

> I've compile-tested it but have not been able to runtime test it (I
> managed to compile and run a mainline kernel for the BBB, but ran into
> problems setting up the bias control devicetree). For now, I'll just
> leave the patch here for review, maybe I'll find some time also runtime
> test it in the future (but likely not).

OK

Regards,

Tony

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] pinctrl: single: Fix PIN_CONFIG_BIAS_DISABLE handling
  2024-03-19 11:06     ` [PATCH] pinctrl: single: Fix PIN_CONFIG_BIAS_DISABLE handling Matthijs Kooijman
@ 2024-03-28 21:02       ` Linus Walleij
  2024-04-03  8:59         ` Tony Lindgren
  2024-03-29  2:05       ` Haojian Zhuang
  2024-04-04 12:00       ` Linus Walleij
  2 siblings, 1 reply; 13+ messages in thread
From: Linus Walleij @ 2024-03-28 21:02 UTC (permalink / raw)
  To: Matthijs Kooijman; +Cc: Haojian Zhuang, Tony Lindgren, linux-gpio, linux-omap

On Tue, Mar 19, 2024 at 12:07 PM Matthijs Kooijman <matthijs@stdin.nl> wrote:

> The pinctrl-single driver handles pin_config_set by looking up the
> requested setting in a DT-defined lookup table, which defines what bits
> correspond to each setting. There is no way to add
> PIN_CONFIG_BIAS_DISABLE entries to the table, since there is instead
> code to disable the bias by applying the disable values of both the
> pullup and pulldown entries in the table.
>
> However, this code is inside the table-lookup loop, so it would only
> execute if there is an entry for PIN_CONFIG_BIAS_DISABLE in the table,
> which can never exist, so this code never runs.
>
> This commit lifts the offending code out of the loop, so it just
> executes directly whenever PIN_CONFIG_BIAS_DISABLE is requested,
> skippipng the table lookup loop.
>
> This also introduces a new `param` variable to make the code slightly
> more readable.
>
> This bug seems to have existed when this code was first merged in commit
> 9dddb4df90d13 ("pinctrl: single: support generic pinconf"). Earlier
> versions of this patch did have an entry for PIN_CONFIG_BIAS_DISABLE in
> the lookup table, but that was removed, which is probably how this bug
> was introduced.
>
> Signed-off-by: Matthijs Kooijman <matthijs@stdin.nl>

This looks reasonable to me, but I need Tony to review it before applying.

Yours,
Linus Walleij

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] pinctrl: single: Fix PIN_CONFIG_BIAS_DISABLE handling
  2024-03-19 11:06     ` [PATCH] pinctrl: single: Fix PIN_CONFIG_BIAS_DISABLE handling Matthijs Kooijman
  2024-03-28 21:02       ` Linus Walleij
@ 2024-03-29  2:05       ` Haojian Zhuang
  2024-04-04 12:00       ` Linus Walleij
  2 siblings, 0 replies; 13+ messages in thread
From: Haojian Zhuang @ 2024-03-29  2:05 UTC (permalink / raw)
  To: Matthijs Kooijman; +Cc: Tony Lindgren, linux-gpio, Linus Walleij, linux-omap

On Tue, Mar 19, 2024 at 12:06:34PM +0100, Matthijs Kooijman wrote:
> The pinctrl-single driver handles pin_config_set by looking up the
> requested setting in a DT-defined lookup table, which defines what bits
> correspond to each setting. There is no way to add
> PIN_CONFIG_BIAS_DISABLE entries to the table, since there is instead
> code to disable the bias by applying the disable values of both the
> pullup and pulldown entries in the table.
> 
> However, this code is inside the table-lookup loop, so it would only
> execute if there is an entry for PIN_CONFIG_BIAS_DISABLE in the table,
> which can never exist, so this code never runs.
> 
> This commit lifts the offending code out of the loop, so it just
> executes directly whenever PIN_CONFIG_BIAS_DISABLE is requested,
> skippipng the table lookup loop.
> 
> This also introduces a new `param` variable to make the code slightly
> more readable.
> 
> This bug seems to have existed when this code was first merged in commit
> 9dddb4df90d13 ("pinctrl: single: support generic pinconf"). Earlier
> versions of this patch did have an entry for PIN_CONFIG_BIAS_DISABLE in
> the lookup table, but that was removed, which is probably how this bug
> was introduced.
> 
> Signed-off-by: Matthijs Kooijman <matthijs@stdin.nl>
> ---
>  drivers/pinctrl/pinctrl-single.c | 18 ++++++++++++------
>  1 file changed, 12 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/pinctrl/pinctrl-single.c b/drivers/pinctrl/pinctrl-single.c
> index 19cc0db771a5a..c7a03b63fa812 100644
> --- a/drivers/pinctrl/pinctrl-single.c
> +++ b/drivers/pinctrl/pinctrl-single.c
> @@ -554,21 +554,30 @@ static int pcs_pinconf_set(struct pinctrl_dev *pctldev,
>  	unsigned offset = 0, shift = 0, i, data, ret;
>  	u32 arg;
>  	int j;
> +	enum pin_config_param param;
>  
>  	ret = pcs_get_function(pctldev, pin, &func);
>  	if (ret)
>  		return ret;
>  
>  	for (j = 0; j < num_configs; j++) {
> +		param = pinconf_to_config_param(configs[j]);
> +
> +		/* BIAS_DISABLE has no entry in the func->conf table */
> +		if (param == PIN_CONFIG_BIAS_DISABLE) {
> +			/* This just disables all bias entries */
> +			pcs_pinconf_clear_bias(pctldev, pin);
> +			continue;
> +		}
> +
>  		for (i = 0; i < func->nconfs; i++) {
> -			if (pinconf_to_config_param(configs[j])
> -				!= func->conf[i].param)
> +			if (param != func->conf[i].param)
>  				continue;
>  
>  			offset = pin * (pcs->width / BITS_PER_BYTE);
>  			data = pcs->read(pcs->base + offset);
>  			arg = pinconf_to_config_argument(configs[j]);
> -			switch (func->conf[i].param) {
> +			switch (param) {
>  			/* 2 parameters */
>  			case PIN_CONFIG_INPUT_SCHMITT:
>  			case PIN_CONFIG_DRIVE_STRENGTH:
> @@ -580,9 +589,6 @@ static int pcs_pinconf_set(struct pinctrl_dev *pctldev,
>  				data |= (arg << shift) & func->conf[i].mask;
>  				break;
>  			/* 4 parameters */
> -			case PIN_CONFIG_BIAS_DISABLE:
> -				pcs_pinconf_clear_bias(pctldev, pin);
> -				break;
>  			case PIN_CONFIG_BIAS_PULL_DOWN:
>  			case PIN_CONFIG_BIAS_PULL_UP:
>  				if (arg)
> -- 
> 2.40.1
> 

Reviewed-by: Haojian Zhuang <haojian.zhuang@linaro.org>

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: pinctrl: single: Cannot disable bias - PIN_CONFIG_BIAS_DISABLE not handled
  2024-03-22  6:24     ` pinctrl: single: Cannot disable bias - PIN_CONFIG_BIAS_DISABLE not handled Tony Lindgren
@ 2024-03-29  2:06       ` Haojian Zhuang
  0 siblings, 0 replies; 13+ messages in thread
From: Haojian Zhuang @ 2024-03-29  2:06 UTC (permalink / raw)
  To: Tony Lindgren; +Cc: Matthijs Kooijman, linux-gpio

On Fri, Mar 22, 2024 at 08:24:19AM +0200, Tony Lindgren wrote:
> * Matthijs Kooijman <matthijs@stdin.nl> [240319 11:05]:
> > Hi Tony,
> > 
> > > > An obvious fix for this would be to lift the handling for
> > > > PIN_CONFIG_BIAS_DISABLE out of the inner loop, running that instead of
> > > > the inner loop in that case.
> > >
> > > Maybe post a suggested patch for Haojian to look at?
> > 
> > I've created a patch (will send in a followup) to do exactly this.
> 
> OK great thanks!
> 
> > I've compile-tested it but have not been able to runtime test it (I
> > managed to compile and run a mainline kernel for the BBB, but ran into
> > problems setting up the bias control devicetree). For now, I'll just
> > leave the patch here for review, maybe I'll find some time also runtime
> > test it in the future (but likely not).
> 
> OK
> 
> Regards,
> 
> Tony

Thanks. I'm fine on this patch.

Best Regards
Haojian

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] pinctrl: single: Fix PIN_CONFIG_BIAS_DISABLE handling
  2024-03-28 21:02       ` Linus Walleij
@ 2024-04-03  8:59         ` Tony Lindgren
  0 siblings, 0 replies; 13+ messages in thread
From: Tony Lindgren @ 2024-04-03  8:59 UTC (permalink / raw)
  To: Linus Walleij; +Cc: Matthijs Kooijman, Haojian Zhuang, linux-gpio, linux-omap

* Linus Walleij <linus.walleij@linaro.org> [240328 21:02]:
> On Tue, Mar 19, 2024 at 12:07 PM Matthijs Kooijman <matthijs@stdin.nl> wrote:
> 
> > The pinctrl-single driver handles pin_config_set by looking up the
> > requested setting in a DT-defined lookup table, which defines what bits
> > correspond to each setting. There is no way to add
> > PIN_CONFIG_BIAS_DISABLE entries to the table, since there is instead
> > code to disable the bias by applying the disable values of both the
> > pullup and pulldown entries in the table.
> >
> > However, this code is inside the table-lookup loop, so it would only
> > execute if there is an entry for PIN_CONFIG_BIAS_DISABLE in the table,
> > which can never exist, so this code never runs.
> >
> > This commit lifts the offending code out of the loop, so it just
> > executes directly whenever PIN_CONFIG_BIAS_DISABLE is requested,
> > skippipng the table lookup loop.
> >
> > This also introduces a new `param` variable to make the code slightly
> > more readable.
> >
> > This bug seems to have existed when this code was first merged in commit
> > 9dddb4df90d13 ("pinctrl: single: support generic pinconf"). Earlier
> > versions of this patch did have an entry for PIN_CONFIG_BIAS_DISABLE in
> > the lookup table, but that was removed, which is probably how this bug
> > was introduced.
> >
> > Signed-off-by: Matthijs Kooijman <matthijs@stdin.nl>
> 
> This looks reasonable to me, but I need Tony to review it before applying.

Looks good to me:

Reviewed-by: Tony Lindgren <tony@atomide.com>

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] pinctrl: single: Fix PIN_CONFIG_BIAS_DISABLE handling
  2024-03-19 11:06     ` [PATCH] pinctrl: single: Fix PIN_CONFIG_BIAS_DISABLE handling Matthijs Kooijman
  2024-03-28 21:02       ` Linus Walleij
  2024-03-29  2:05       ` Haojian Zhuang
@ 2024-04-04 12:00       ` Linus Walleij
  2 siblings, 0 replies; 13+ messages in thread
From: Linus Walleij @ 2024-04-04 12:00 UTC (permalink / raw)
  To: Matthijs Kooijman; +Cc: Haojian Zhuang, Tony Lindgren, linux-gpio, linux-omap

On Tue, Mar 19, 2024 at 12:07 PM Matthijs Kooijman <matthijs@stdin.nl> wrote:

> The pinctrl-single driver handles pin_config_set by looking up the
> requested setting in a DT-defined lookup table, which defines what bits
> correspond to each setting. There is no way to add
> PIN_CONFIG_BIAS_DISABLE entries to the table, since there is instead
> code to disable the bias by applying the disable values of both the
> pullup and pulldown entries in the table.
>
> However, this code is inside the table-lookup loop, so it would only
> execute if there is an entry for PIN_CONFIG_BIAS_DISABLE in the table,
> which can never exist, so this code never runs.
>
> This commit lifts the offending code out of the loop, so it just
> executes directly whenever PIN_CONFIG_BIAS_DISABLE is requested,
> skippipng the table lookup loop.
>
> This also introduces a new `param` variable to make the code slightly
> more readable.
>
> This bug seems to have existed when this code was first merged in commit
> 9dddb4df90d13 ("pinctrl: single: support generic pinconf"). Earlier
> versions of this patch did have an entry for PIN_CONFIG_BIAS_DISABLE in
> the lookup table, but that was removed, which is probably how this bug
> was introduced.
>
> Signed-off-by: Matthijs Kooijman <matthijs@stdin.nl>

Patch applied!

Yours,
Linus Walleij

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2024-04-04 12:00 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-28 12:04 pinctrl: single: Cannot disable bias - PIN_CONFIG_BIAS_DISABLE not handled Matthijs Kooijman
2024-02-29  7:32 ` Tony Lindgren
2024-02-29 18:31   ` Nathan Chancellor
2024-02-29 19:07     ` Matthijs Kooijman
2024-03-01 10:04     ` Tony Lindgren
2024-03-19 11:05   ` Matthijs Kooijman
2024-03-19 11:06     ` [PATCH] pinctrl: single: Fix PIN_CONFIG_BIAS_DISABLE handling Matthijs Kooijman
2024-03-28 21:02       ` Linus Walleij
2024-04-03  8:59         ` Tony Lindgren
2024-03-29  2:05       ` Haojian Zhuang
2024-04-04 12:00       ` Linus Walleij
2024-03-22  6:24     ` pinctrl: single: Cannot disable bias - PIN_CONFIG_BIAS_DISABLE not handled Tony Lindgren
2024-03-29  2:06       ` Haojian Zhuang

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.