pinctrl: single: Cannot disable bias - PIN_CONFIG_BIAS_DISABLE not handled
Subsystems (3)
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/[email protected]/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
Tony Lindgren
maintainerHi,
Quoted 9 lines · Matthijs Kooijman
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_setby looking up the requested setting in a DT-defined lookup table, which defines what bits correspond to each setting. ForPIN_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
Quoted 6 lines
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_DISABLEout of the inner loop, running that instead of the inner loop in that case.
Maybe post a suggested patch for Haojian to look at?
Quoted 7 lines
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
Quoted 9 lines · Tony Lindgren
Quoted 3 lines
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:
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 :)
Quoted 2 lines
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
Hey folks,
Quoted 4 lines · Nathan Chancellor
Quoted 3 lines · Tony Lindgren
Quoted 3 lines
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
Tony Lindgren
maintainerQuoted 9 lines · Nathan Chancellor
Quoted 8 lines · Tony Lindgren
Quoted 3 lines
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:
Quoted 6 lines
Quoted 1 line
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.
Quoted 11 lines
Quoted 2 lines
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_64hosts.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
Hi Tony,
Quoted 5 lines
Quoted 3 lines
An obvious fix for this would be to lift the handling for
PIN_CONFIG_BIAS_DISABLEout 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
PATCH 1/1 pinctrl: single: Fix PIN_CONFIG_BIAS_DISABLE handling
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.
--- drivers/pinctrl/pinctrl-single.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-)
drivers/pinctrl/pinctrl-single.c +12 -6
@@ -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)
Signature · 1 line
2.40.1
Re: [PATCH] pinctrl: single: Fix PIN_CONFIG_BIAS_DISABLE handling
Quoted 18 lines · Matthijs Kooijman
The pinctrl-single driver handles
pin_config_setby looking up the requested setting in a DT-defined lookup table, which defines what bits correspond to each setting. There is no way to addPIN_CONFIG_BIAS_DISABLEentries 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_DISABLEin 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_DISABLEis requested, skippipng the table lookup loop.This also introduces a new
paramvariable to make the code slightly more readable.
Quoted 7 lines
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 forPIN_CONFIG_BIAS_DISABLEin the lookup table, but that was removed, which is probably how this bug was introduced.Signed-off-by Matthijs Kooijman <[email protected]>
This looks reasonable to me, but I need Tony to review it before applying.
Yours,
Linus Walleij
Re: [PATCH] pinctrl: single: Fix PIN_CONFIG_BIAS_DISABLE handling
Quoted 26 lines · Linus Walleij
Quoted 24 lines · Matthijs Kooijman
The pinctrl-single driver handles
pin_config_setby looking up the requested setting in a DT-defined lookup table, which defines what bits correspond to each setting. There is no way to addPIN_CONFIG_BIAS_DISABLEentries 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_DISABLEin 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_DISABLEis requested, skippipng the table lookup loop.This also introduces a new
paramvariable 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 forPIN_CONFIG_BIAS_DISABLEin the lookup table, but that was removed, which is probably how this bug was introduced.
Quoted 3 lines
Quoted 1 line
Signed-off-by Matthijs Kooijman <[email protected]>This looks reasonable to me, but I need Tony to review it before applying.
Looks good to me:
Re: [PATCH] pinctrl: single: Fix PIN_CONFIG_BIAS_DISABLE handling
In reply to Matthijs KooijmanQuoted 80 lines · Matthijs Kooijman
The pinctrl-single driver handles
pin_config_setby looking up the requested setting in a DT-defined lookup table, which defines what bits correspond to each setting. There is no way to addPIN_CONFIG_BIAS_DISABLEentries 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_DISABLEin 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_DISABLEis requested, skippipng the table lookup loop.This also introduces a new
paramvariable 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 forPIN_CONFIG_BIAS_DISABLEin the lookup table, but that was removed, which is probably how this bug was introduced.Signed-off-by Matthijs Kooijman <[email protected]>--- drivers/pinctrl/pinctrl-single.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-)drivers/pinctrl/pinctrl-single.c +12
-6@@ -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)Signature · 1 line
2.40.1
Re: [PATCH] pinctrl: single: Fix PIN_CONFIG_BIAS_DISABLE handling
In reply to Matthijs KooijmanQuoted 18 lines · Matthijs Kooijman
The pinctrl-single driver handles
pin_config_setby looking up the requested setting in a DT-defined lookup table, which defines what bits correspond to each setting. There is no way to addPIN_CONFIG_BIAS_DISABLEentries 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_DISABLEin 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_DISABLEis requested, skippipng the table lookup loop.This also introduces a new
paramvariable to make the code slightly more readable.
Quoted 7 lines
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 forPIN_CONFIG_BIAS_DISABLEin the lookup table, but that was removed, which is probably how this bug was introduced.Signed-off-by Matthijs Kooijman <[email protected]>
Patch applied!
Yours,
Linus Walleij
Tony Lindgren
maintainerQuoted 9 lines · Matthijs Kooijman
Hi Tony,
Quoted 5 lines
Quoted 3 lines
An obvious fix for this would be to lift the handling for
PIN_CONFIG_BIAS_DISABLEout 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!
Quoted 5 lines
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
Haojian Zhuang
maintainerQuoted 19 lines · Tony Lindgren
Quoted 9 lines · Matthijs Kooijman
Hi Tony,
Quoted 5 lines
Quoted 3 lines
An obvious fix for this would be to lift the handling for
PIN_CONFIG_BIAS_DISABLEout 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!
Quoted 5 lines
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).
Quoted 5 lines
OK
Regards,
Tony
Thanks. I'm fine on this patch.
Best Regards
Haojian