gpio: mmio: report the line direction on chips without direction registers
Subsystems (5)
v5 cover gpio: mmio: report the line direction on chips without direction registers
From: Mehmet Fide <[email protected]>
Hi Bartosz, Linus,
this replaces the gpiolib guard patch [1], along the lines Bartosz suggested there: instead of teaching gpiod_get_direction() to stay quiet when a chip has no get_direction(), give gpio-mmio one and let the pin controller tell it what the pad does.
The user is the Vybrid GPIO block (gpio-vf610, a generic mmio chip with GPIO_GENERIC_PINCTRL_BACKEND and no direction registers, the direction lives in the iomuxc pad as the OBE bit). Today every gpiod_get_direction() there trips the WARN in gpiolib, 21 backtraces per boot on a Colibri VF61/VF50.
Patch 1 is the pinctrl-imx side. Bartosz asked whether the raw register coming back from pin_config_get() is a bug in pinctrl-imx: it is, the callback never looked at which parameter was requested. It now answers PIN_CONFIG_OUTPUT_ENABLE and PIN_CONFIG_INPUT_ENABLE on SoCs that say where those bits live (Vybrid: OBE bit 1, IBE bit 0) and -ENOTSUPP for everything else, the SCU based SoCs included; the debugfs dump, the only raw-register user, reads the register through its own helper. The set callback stays raw, as the fsl,pins binding requires. Converting the driver fully to generic pinconf is a bigger job than this fix needs.
Patch 2 is what Linus asked for on v3: an optional get_config() in struct gpio_chip and gpiochip_generic_get_config() as its pin control backed implementation, the mirror of gpiochip_generic_config(). The packed parameter goes in, its bare argument comes out, as with pinctrl_gpio_get_config(). No consumer API.
Patch 3 keeps the direction in gpio-mmio's existing shadow and installs the shadow-reading get_direction() for the "pinctrl backend, no direction registers" combination. The pad is asked once, from request(), in process context, through gc->get_config(), so it is safe for the gpiochip_lock_as_irq() path that calls get_direction() under the irq descriptor lock.
Tested on a Colibri VF61 (Iris carrier) on top of gpio/for-next, with DEBUG_ATOMIC_SLEEP and PROVE_LOCKING enabled: no backtraces, and /sys/kernel/debug/gpio shows the right direction for every requested line (the hogs, the SD card detect input, the USB VBUS regulator output). Lines the pin controller cannot answer for keep the input default gpiolib assumed before, so nothing that worked before is affected. The initial direction scan in gpiochip_add_data_with_key() runs before the pin ranges exist and still guesses; only requested lines get the real answer.
Patch 1 was also booted on a Verdin iMX8MP (gprarray, 6.12.107): the pinconf-pins and pinconf-groups debugfs dumps are byte for byte the same as before the patch, and nothing else changed.
Patch 3 needs patch 1 to give correct answers; taking all three through one tree, with an ack from the other side, avoids the window.
[1] https://lore.kernel.org/linux-gpio/[email protected]/
Changes in v5:
- patch 2: the get_config() documentation said the answer comes back packed; it is the bare argument, like pinctrl_gpio_get_config() returns (Sashiko review). Text only, no code change.
Changes in v4:
- new patch 2: get_config() in struct gpio_chip and gpiochip_generic_get_config() (Linus)
- patch 3: seed the shadow through gc->get_config(), no pinctrl call and no CONFIG_PINCTRL check in gpio-mmio (Linus)
- patch 1: drop the npins check, pin_request() already rejects a pin the controller does not have
Changes in v3:
- patch 1: return -ENOTSUPP for the SCU based SoCs instead of letting the firmware call hand back the raw pad value (Sashiko review)
- patch 1: -EINVAL for a pin the device tree never configured, -ENOTSUPP only for unsupported parameters and SoCs
- patch 1, 2: drop two comments that only restated the code
Changes in v2:
- patch 1: decode the requested parameter instead of returning the raw register; debugfs group dump reads the register through its own helper
- patch 2: keep the direction in the gpio-mmio shadow, ask pinctrl once from request() instead of from get_direction()
Mehmet Fide (3):
pinctrl: imx: answer OUTPUT_ENABLE/INPUT_ENABLE queries from the pad
register
gpiolib: add get_config() and gpiochip_generic_get_config()
gpio: mmio: track the direction of chips without direction registers
Documentation/driver-api/gpio/driver.rst | 7 +++ drivers/gpio/gpio-mmio.c | 60 +++++++++++++++++++++-- drivers/gpio/gpiolib.c | 23 +++++++++ drivers/pinctrl/freescale/pinctrl-imx.c | 54 ++++++++++++++++++-- drivers/pinctrl/freescale/pinctrl-imx.h | 4 ++ drivers/pinctrl/freescale/pinctrl-vf610.c | 2 + include/linux/gpio/driver.h | 9 ++++ 7 files changed, 151 insertions(+), 8 deletions(-)
base-commit: 1900b5a41493e7050c68c1e62780d6fb9a209457
Signature · 1 line
2.54.0
PATCH v5 1/3 pinctrl: imx: answer OUTPUT_ENABLE/INPUT_ENABLE queries from the pad register
From: Mehmet Fide <[email protected]>
The mmio pinconf get callback ignores which parameter was requested and returns the raw conf register, so a generic query through pinctrl_gpio_get_config() gets register bits back instead of the packed parameter it asked for.
Decode the requested parameter and answer PIN_CONFIG_OUTPUT_ENABLE and PIN_CONFIG_INPUT_ENABLE on SoCs that declare where those bits live in the pad register; Vybrid has OBE at bit 1 and IBE at bit 0. The answer is 0 with the bit value as the argument, which is what the pinctrl_gpio_get_config() users (gpio-by-pinctrl, and gpio-mmio in the next patch) expect. Other parameters and the SCU based SoCs, whose firmware call returns the raw pad value as well, get -ENOTSUPP; a pin the device tree never configured gets -EINVAL, as the raw helper already does, so a caller can tell "no answer for this pin" from "this controller never answers".
The only in-tree user of the raw register was the debugfs group dump, which called the callback with an uninitialized config; it now reads the register through its own helper, like the single pin dump already did.
The set callback is not touched: the fsl,pins binding hands it the raw pad register value and that stays the only thing it accepts. Nothing in-tree sends generic parameters to it on these SoCs; making it understand them is a separate change.
This gives gpio-mmio a way to read back the line direction on chips whose direction lives in the pin controller.
--- drivers/pinctrl/freescale/pinctrl-imx.c | 54 +++++++++++++++++++++-- drivers/pinctrl/freescale/pinctrl-imx.h | 4 ++ drivers/pinctrl/freescale/pinctrl-vf610.c | 2 + 3 files changed, 56 insertions(+), 4 deletions(-)
drivers/pinctrl/freescale/pinctrl-imx.c +50 -4
@@ -21,6 +21,7 @@#include <linux/pinctrl/machine.h> #include <linux/pinctrl/pinconf.h> +#include <linux/pinctrl/pinconf-generic.h> #include <linux/pinctrl/pinctrl.h> #include <linux/pinctrl/pinmux.h>
@@ -291,8 +292,8 @@ struct pinmux_ops imx_pmx_ops = { .set_mux = imx_pmx_set, }; -static int imx_pinconf_get_mmio (struct pinctrl_dev *pctldev,unsigned pin_id, -unsigned long *config) +static int imx_pinconf_get_raw_mmio (struct pinctrl_dev *pctldev, +unsigned int pin_id,unsigned long *config) {struct imx_pinctrl *ipctl =pinctrl_dev_get_drvdata (pctldev);const struct imx_pinctrl_soc_info *info = ipctl->info;
@@ -312,16 +313,61 @@ static int imx_pinconf_get_mmio(struct pinctrl_dev *pctldev, unsigned pin_id,return 0 ; } +static int imx_pinconf_get_mmio (struct pinctrl_dev *pctldev, +unsigned int pin_id,unsigned long *config) +{ +struct imx_pinctrl *ipctl =pinctrl_dev_get_drvdata (pctldev); +const struct imx_pinctrl_soc_info *info = ipctl->info; +const struct imx_pin_reg *pin_reg = &ipctl->pin_regs[pin_id]; +enum pin_config_param param =pinconf_to_config_param (*config); +unsigned int mask; +u32 raw; + +switch (param) { +case PIN_CONFIG_OUTPUT_ENABLE : + mask = info->obe_mask; +break ; +case PIN_CONFIG_INPUT_ENABLE : + mask = info->ibe_mask; +break ; +default : + mask =0 ; +break ; + } + +if (!mask) +return -ENOTSUPP ; +if (pin_reg->conf_reg ==-1 ) +return -EINVAL ; + + raw =readl (ipctl->base + pin_reg->conf_reg); + *config =pinconf_to_config_packed (param, !!(raw & mask)); + +return 0 ; +} +static int imx_pinconf_get (struct pinctrl_dev *pctldev,unsigned pin_id,unsigned long *config) {struct imx_pinctrl *ipctl =pinctrl_dev_get_drvdata (pctldev);const struct imx_pinctrl_soc_info *info = ipctl->info; +if (info->flags &IMX_USE_SCU ) +return -ENOTSUPP ; + +return imx_pinconf_get_mmio (pctldev, pin_id, config); +} + +static int imx_pinconf_get_raw (struct pinctrl_dev *pctldev, +unsigned int pin_id,unsigned long *config) +{ +struct imx_pinctrl *ipctl =pinctrl_dev_get_drvdata (pctldev); +const struct imx_pinctrl_soc_info *info = ipctl->info; +if (info->flags &IMX_USE_SCU )return info->imx_pinconf_get (pctldev, pin_id, config);else -return imx_pinconf_get_mmio (pctldev, pin_id, config); +return imx_pinconf_get_raw_mmio (pctldev, pin_id, config); } static int imx_pinconf_set_mmio(struct pinctrl_dev *pctldev,
@@ -426,7 +472,7 @@ static void imx_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,struct imx_pin *pin = &((struct imx_pin *)(grp->data))[i]; name =pin_get_name (pctldev, pin->pin); - ret =imx_pinconf_get (pctldev, pin->pin, &config); + ret =imx_pinconf_get_raw (pctldev, pin->pin, &config);if (ret)return ;seq_printf (s," %s: 0x%lx\n" , name, config);
drivers/pinctrl/freescale/pinctrl-imx.h +4 -0
@@ -91,6 +91,10 @@ struct imx_pinctrl_soc_info {unsigned int mux_mask;u8 mux_shift; +/* OBE/IBE bits in the conf register, 0 if the pad does not have them */ +unsigned int obe_mask; +unsigned int ibe_mask; + int (*gpio_set_direction)(struct pinctrl_dev *pctldev, struct pinctrl_gpio_range *range, unsigned offset,
drivers/pinctrl/freescale/pinctrl-vf610.c +2 -0
@@ -319,6 +319,8 @@ static const struct imx_pinctrl_soc_info vf610_pinctrl_info = { .gpio_set_direction = vf610_pmx_gpio_set_direction, .mux_mask = 0x700000, .mux_shift = 20, + .obe_mask = 0x2, + .ibe_mask = 0x1, };static const struct of_device_id vf610_pinctrl_of_match[] = {
Signature · 1 line
2.54.0
Re: [PATCH v5 1/3] pinctrl: imx: answer OUTPUT_ENABLE/INPUT_ENABLE queries from the pad register
Quoted 23 lines · Mehmet Fide
From: Mehmet Fide <[email protected]>
The mmio pinconf get callback ignores which parameter was requested and returns the raw conf register, so a generic query through
pinctrl_gpio_get_config()gets register bits back instead of the packed parameter it asked for.Decode the requested parameter and answer
PIN_CONFIG_OUTPUT_ENABLEandPIN_CONFIG_INPUT_ENABLEon SoCs that declare where those bits live in the pad register; Vybrid has OBE at bit 1 and IBE at bit 0. The answer is 0 with the bit value as the argument, which is what thepinctrl_gpio_get_config()users (gpio-by-pinctrl, and gpio-mmio in the next patch) expect. Other parameters and the SCU based SoCs, whose firmware call returns the raw pad value as well, get-ENOTSUPP; a pin the device tree never configured gets-EINVAL, as the raw helper already does, so a caller can tell "no answer for this pin" from "this controller never answers".The only in-tree user of the raw register was the debugfs group dump, which called the callback with an uninitialized config; it now reads the register through its own helper, like the single pin dump already did.
Quoted 7 lines
The set callback is not touched: the fsl,pins binding hands it the raw pad register value and that stays the only thing it accepts. Nothing in-tree sends generic parameters to it on these SoCs; making it understand them is a separate change.
This gives gpio-mmio a way to read back the line direction on chips whose direction lives in the pin controller.
Since vf610 and imx7ulp/imx8ulp share one gpio driver(gpio-vf610.c), I double check the gpio driver, imx7ulp/imx8ulp has PDDR, so it will go another path in mmio, not impacted by this patch. So even the pin config register of imx7ulp/imx8ulp also include IBE/OBE, no need to add obe_mask/ ibe_mask as vf610.
Regards
Haibo Chen
Quoted 135 lines
Suggested-by Bartosz Golaszewski <[email protected]>Signed-off-by Mehmet Fide <[email protected]>--- drivers/pinctrl/freescale/pinctrl-imx.c | 54 +++++++++++++++++++++-- drivers/pinctrl/freescale/pinctrl-imx.h | 4 ++ drivers/pinctrl/freescale/pinctrl-vf610.c | 2 + 3 files changed, 56 insertions(+), 4 deletions(-)drivers/pinctrl/freescale/pinctrl-imx.c +50
-4@@ -21,6 +21,7 @@#include <linux/pinctrl/machine.h> #include <linux/pinctrl/pinconf.h> +#include <linux/pinctrl/pinconf-generic.h> #include <linux/pinctrl/pinctrl.h> #include <linux/pinctrl/pinmux.h> @@ -291,8 +292,8 @@ struct pinmux_ops imx_pmx_ops = { .set_mux = imx_pmx_set, }; -static int imx_pinconf_get_mmio (struct pinctrl_dev *pctldev,unsigned pin_id, -unsigned long *config) +static int imx_pinconf_get_raw_mmio (struct pinctrl_dev *pctldev, +unsigned int pin_id,unsigned long *config) {struct imx_pinctrl *ipctl =pinctrl_dev_get_drvdata (pctldev);const struct imx_pinctrl_soc_info *info = ipctl->info;@@ -312,16 +313,61 @@ static int imx_pinconf_get_mmio(struct pinctrl_dev *pctldev, unsigned pin_id,return 0 ; } +static int imx_pinconf_get_mmio (struct pinctrl_dev *pctldev, +unsigned int pin_id,unsigned long *config) +{ +struct imx_pinctrl *ipctl =pinctrl_dev_get_drvdata (pctldev); +const struct imx_pinctrl_soc_info *info = ipctl->info; +const struct imx_pin_reg *pin_reg = &ipctl->pin_regs[pin_id]; +enum pin_config_param param =pinconf_to_config_param (*config); +unsigned int mask; +u32 raw; + +switch (param) { +case PIN_CONFIG_OUTPUT_ENABLE : + mask = info->obe_mask; +break ; +case PIN_CONFIG_INPUT_ENABLE : + mask = info->ibe_mask; +break ; +default : + mask =0 ; +break ; + } + +if (!mask) +return -ENOTSUPP ; +if (pin_reg->conf_reg ==-1 ) +return -EINVAL ; + + raw =readl (ipctl->base + pin_reg->conf_reg); + *config =pinconf_to_config_packed (param, !!(raw & mask)); + +return 0 ; +} +static int imx_pinconf_get (struct pinctrl_dev *pctldev,unsigned pin_id,unsigned long *config) {struct imx_pinctrl *ipctl =pinctrl_dev_get_drvdata (pctldev);const struct imx_pinctrl_soc_info *info = ipctl->info; +if (info->flags &IMX_USE_SCU ) +return -ENOTSUPP ; + +return imx_pinconf_get_mmio (pctldev, pin_id, config); +} + +static int imx_pinconf_get_raw (struct pinctrl_dev *pctldev, +unsigned int pin_id,unsigned long *config) +{ +struct imx_pinctrl *ipctl =pinctrl_dev_get_drvdata (pctldev); +const struct imx_pinctrl_soc_info *info = ipctl->info; +if (info->flags &IMX_USE_SCU )return info->imx_pinconf_get (pctldev, pin_id, config);else -return imx_pinconf_get_mmio (pctldev, pin_id, config); +return imx_pinconf_get_raw_mmio (pctldev, pin_id, config); } static int imx_pinconf_set_mmio(struct pinctrl_dev *pctldev,@@ -426,7 +472,7 @@ static void imx_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,struct imx_pin *pin = &((struct imx_pin *)(grp->data))[i]; name =pin_get_name (pctldev, pin->pin); - ret =imx_pinconf_get (pctldev, pin->pin, &config); + ret =imx_pinconf_get_raw (pctldev, pin->pin, &config);if (ret)return ;seq_printf (s," %s: 0x%lx\n" , name, config);drivers/pinctrl/freescale/pinctrl-imx.h +4
-0@@ -91,6 +91,10 @@ struct imx_pinctrl_soc_info {unsigned int mux_mask;u8 mux_shift; +/* OBE/IBE bits in the conf register, 0 if the pad does not have them */ +unsigned int obe_mask; +unsigned int ibe_mask; + int (*gpio_set_direction)(struct pinctrl_dev *pctldev, struct pinctrl_gpio_range *range, unsigned offset,drivers/pinctrl/freescale/pinctrl-vf610.c +2
-0@@ -319,6 +319,8 @@ static const struct imx_pinctrl_soc_info vf610_pinctrl_info = { .gpio_set_direction = vf610_pmx_gpio_set_direction, .mux_mask = 0x700000, .mux_shift = 20, + .obe_mask = 0x2, + .ibe_mask = 0x1, };static const struct of_device_id vf610_pinctrl_of_match[] = {Signature · 1 line
2.54.0
Re: [PATCH v5 1/3] pinctrl: imx: answer OUTPUT_ENABLE/INPUT_ENABLE queries from the pad register
In reply to Mehmet FideQuoted 28 lines · Mehmet Fide
From: Mehmet Fide <[email protected]>
The mmio pinconf get callback ignores which parameter was requested and returns the raw conf register, so a generic query through
pinctrl_gpio_get_config()gets register bits back instead of the packed parameter it asked for.Decode the requested parameter and answer
PIN_CONFIG_OUTPUT_ENABLEandPIN_CONFIG_INPUT_ENABLEon SoCs that declare where those bits live in the pad register; Vybrid has OBE at bit 1 and IBE at bit 0. The answer is 0 with the bit value as the argument, which is what thepinctrl_gpio_get_config()users (gpio-by-pinctrl, and gpio-mmio in the next patch) expect. Other parameters and the SCU based SoCs, whose firmware call returns the raw pad value as well, get-ENOTSUPP; a pin the device tree never configured gets-EINVAL, as the raw helper already does, so a caller can tell "no answer for this pin" from "this controller never answers".The only in-tree user of the raw register was the debugfs group dump, which called the callback with an uninitialized config; it now reads the register through its own helper, like the single pin dump already did.
The set callback is not touched: the fsl,pins binding hands it the raw pad register value and that stays the only thing it accepts. Nothing in-tree sends generic parameters to it on these SoCs; making it understand them is a separate change.
Quoted 5 lines
This gives gpio-mmio a way to read back the line direction on chips whose direction lives in the pin controller.
Suggested-by Bartosz Golaszewski <[email protected]>Signed-off-by Mehmet Fide <[email protected]>
This looks like something I could apply separately from the rest, can I just queue this patch in the pinctrl tree?
Yours,
Linus Walleij
Re: [PATCH v5 1/3] pinctrl: imx: answer OUTPUT_ENABLE/INPUT_ENABLE queries from the pad register
Hi Linus,
Quoted 2 lines · On 2026-09-14 Linus Walleij wrote:
This looks like something I could apply separately from the rest, can I just queue this patch in the pinctrl tree?
Thanks for the reviews. If it is all the same to you, I would keep the three together until v6 has been looked at; gpio-mmio depends on this patch to get correct answers on Vybrid, so I would rather not have it land ahead of the rest. v6 goes out this week with Haibo's comments addressed and a small pinctrl patch for the CONFIG_PINCTRL=n stubs added. Once that is settled, this patch can go through your tree and the gpio patches through Bartosz's in the same cycle.
Best regards,
Mehmet
PATCH v5 2/3 gpiolib: add get_config() and gpiochip_generic_get_config()
In reply to Mehmet FideFrom: Mehmet Fide <[email protected]>
A chip with a pin control backend sets a line's configuration through set_config() and gpiochip_generic_config(), but has no way to read one back. gpio-mmio needs that to learn the direction of a line whose direction lives in the pin controller.
Add the optional get_config() callback, taking the packed parameter to query and returning its bare argument the way pinctrl_gpio_get_config() does, and gpiochip_generic_get_config() as the pin control backed implementation, the mirror of gpiochip_generic_config(). Without CONFIG_PINCTRL the pinctrl stub returns 0 and leaves the config alone, so the helper answers -ENOTSUPP there instead.
Nothing in gpiolib calls get_config() and there is no consumer API; it is for the chip's own use.
--- Documentation/driver-api/gpio/driver.rst | 7 +++++++ drivers/gpio/gpiolib.c | 23 +++++++++++++++++++++++ include/linux/gpio/driver.h | 9 +++++++++ 3 files changed, 39 insertions(+)
Documentation/driver-api/gpio/driver.rst +7 -0
@@ -134,6 +134,13 @@ ending up in the pin control back-end "behind" the GPIO controller, usually closer to the actual pins. This way the pin controller can manage the below listed GPIO configurations. +The optional .get_config() callback reads a configuration back: the packed +parameter to query goes in, its bare argument comes out, the way +pinctrl_gpio_get_config() answers. gpiochip_generic_get_config() is its pin +control backed counterpart. Nothing in gpiolib calls it; it is for the GPIO +driver's own use, for example to learn the direction of a line when the pin +controller owns it. + If a pin controller back-end is used, the GPIO controller or hardware description needs to provide "GPIO ranges" mapping the GPIO line offsets to pin numbers on the pin controller so they can properly cross-reference each other.
drivers/gpio/gpiolib.c +23 -0
@@ -2440,6 +2440,29 @@ int gpiochip_generic_config(struct gpio_chip *gc, unsigned int offset, }EXPORT_SYMBOL_GPL (gpiochip_generic_config); +/** +* gpiochip_generic_get_config() - read back the configuration of a pin +* @gc: the gpiochip owning the GPIO +* @offset: the offset of the GPIO to query +* @config: the packed parameter to query, replaced by its bare argument +* +* Returns: +* 0 on success, or negative errno on failure. +*/ +int gpiochip_generic_get_config (struct gpio_chip *gc,unsigned int offset, +unsigned long *config) +{ +#ifdef CONFIG_PINCTRL +if (list_empty (&gc->gpiodev->pin_ranges)) +return -ENOTSUPP ; + +return pinctrl_gpio_get_config (gc, offset, config); +#else +return -ENOTSUPP ; +#endif +} +EXPORT_SYMBOL_GPL (gpiochip_generic_get_config); +#ifdef CONFIG_PINCTRL /**
include/linux/gpio/driver.h +9 -0
@@ -359,6 +359,10 @@ struct gpio_irq_chip {* @set_config: optional hook for all kinds of settings. Uses the same * packed config format as generic pinconf. Must return 0 on success and * a negative error number on failure. +* @get_config: optional hook to read back a setting. Takes the packed +* generic pinconf parameter to query and returns its bare argument in +* the same variable, like pinctrl_gpio_get_config(). Must return 0 on +* success and a negative error number on failure. * @to_irq: optional hook supporting non-static gpiod_to_irq() mappings; * implementation may not sleep * @dbg_show: optional routine to show contents in debugfs; default code
@@ -434,6 +438,9 @@ struct gpio_chip {int (*set_config)(struct gpio_chip *gc,unsigned int offset,unsigned long config); +int (*get_config)(struct gpio_chip *gc, +unsigned int offset, +unsigned long *config);int (*to_irq)(struct gpio_chip *gc,unsigned int offset);
@@ -708,6 +715,8 @@ int gpiochip_generic_request(struct gpio_chip *gc, unsigned int offset);void gpiochip_generic_free (struct gpio_chip *gc,unsigned int offset);int gpiochip_generic_config (struct gpio_chip *gc,unsigned int offset,unsigned long config); +int gpiochip_generic_get_config (struct gpio_chip *gc,unsigned int offset, +unsigned long *config);/** * struct gpio_pin_range - pin range controlled by a gpio chip
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v5 2/3] gpiolib: add get_config() and gpiochip_generic_get_config()
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [High] The fallback stub for pinctrl_gpio_get_config() returns 0 without modifying the output config parameter, causing callers to use query enums or uninitialized memory as hardware states.
- [High] gpiochip_generic_config() returns 0 (success) when CONFIG_PINCTRL is disabled, silently ignoring hardware configuration requests and creating an API inconsistency. --
commit 5e30160f0acecf52b9ebfedbbaf09c6e99e8ce08
Author: Mehmet Fide <[email protected]>
Subject: gpiolib: add get_config() and gpiochip_generic_get_config()
This commit introduces a new get_config() callback for gpiochip and provides gpiochip_generic_get_config() as the generic pin control backend implementation. This allows a chip to read back a line's configuration, which is useful for determining line direction when the pin controller owns it.
drivers/gpio/gpiolib.c int gpiochip_generic_config(struct gpio_chip *gc, unsigned int offset, · line 2441
Quoted 7 lines
drivers/gpio/gpiolib.c +0
-0@@ -2440,6 +2440,29 @@ int gpiochip_generic_config(struct gpio_chip *gc, unsigned int offset, }EXPORT_SYMBOL_GPL (gpiochip_generic_config);
[Severity: High]
This isn't a bug introduced by this patch, but right above this new function, gpiochip_generic_config() delegates to pinctrl_gpio_set_config(). When CONFIG_PINCTRL is disabled, it skips the pin_ranges check and calls the stub, which silently returns 0:
include/linux/pinctrl/consumer.h:pinctrl_gpio_set_config() {
return 0;
}Does this cause hardware configuration requests to be silently ignored while reporting success to the caller? The new getter correctly avoids this by returning -ENOTSUPP when the backend is disabled. Should the pre-existing setter stub also be updated to return -ENOTSUPP to ensure consistent API behavior?
Quoted 11 lines
drivers/gpio/gpiolib.c +10
-0+/** +* gpiochip_generic_get_config() - read back the configuration of a pin +* @gc: the gpiochip owning the GPIO +* @offset: the offset of the GPIO to query +* @config: the packed parameter to query, replaced by its bare argument +* +* Returns: +* 0 on success, or negative errno on failure. +*/ +int gpiochip_generic_get_config(struct gpio_chip *gc, unsigned int offset,
drivers/gpio/gpiolib.c int gpiochip_generic_config(struct gpio_chip *gc, unsigned int offset, · line 2462
Quoted 10 lines
drivers/gpio/gpiolib.c +10
-0+ unsigned long *config) +{ +#ifdef CONFIG_PINCTRL +if (list_empty (&gc->gpiodev->pin_ranges)) +return -ENOTSUPP ; + +return pinctrl_gpio_get_config (gc, offset, config); +#else +return -ENOTSUPP ; +#endif
[Severity: High]
This is a pre-existing issue, but I notice this helper explicitly works around the behavior of the fallback stub for pinctrl_gpio_get_config().
Since the stub returns 0 without modifying the output config parameter:
include/linux/pinctrl/consumer.h:pinctrl_gpio_get_config() {
return 0;
}direct callers (such as pin_control_gpio_get_direction() in drivers/gpio/gpio-by-pinctrl.c) are left reading uninitialized memory or query enum values as hardware states when CONFIG_PINCTRL is disabled.
Should the root stub in include/linux/pinctrl/consumer.h be fixed to return -ENOTSUPP directly to prevent other callers from breaking?
Quoted 2 lines
drivers/gpio/gpiolib.c +2
-0+} +EXPORT_SYMBOL_GPL (gpiochip_generic_get_config);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH v5 2/3] gpiolib: add get_config() and gpiochip_generic_get_config()
Quoted 34 lines · On Thu, Sep 03, 2026 at 08:14:31AM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [High] The fallback stub forpinctrl_gpio_get_config()returns 0 without modifying the outputconfigparameter, causing callers to use query enums or uninitialized memory as hardware states.
- [High]gpiochip_generic_config()returns 0 (success) whenCONFIG_PINCTRLis disabled, silently ignoring hardware configuration requests and creating an API inconsistency. --commit
5e30160f0acecf52b9ebfedbbaf09c6e99e8ce08
Author: Mehmet Fide <[email protected]>
Subject: gpiolib: addget_config()andgpiochip_generic_get_config()This commit introduces a new
get_config()callback for gpiochip and providesgpiochip_generic_get_config()as the generic pin control backend implementation. This allows a chip to read back a line's configuration, which is useful for determining line direction when the pin controller owns it.drivers/gpio/gpiolib.c int gpiochip_generic_config(struct gpio_chip *gc, unsigned int offset, · line 2441
Quoted 7 lines
drivers/gpio/gpiolib.c +0
-0@@ -2440,6 +2440,29 @@ int gpiochip_generic_config(struct gpio_chip *gc, unsigned int offset, }EXPORT_SYMBOL_GPL (gpiochip_generic_config);[Severity: High]
This isn't a bug introduced by this patch, but right above this new function,gpiochip_generic_config()delegates topinctrl_gpio_set_config(). WhenCONFIG_PINCTRLis disabled, it skips thepin_rangescheck and calls the stub, which silently returns 0:include/linux/pinctrl/consumer.h:pinctrl_gpio_set_config() { return 0; }
Quoted 5 lines
Does this cause hardware configuration requests to be silently ignored while reporting success to the caller? The new getter correctly avoids this by returning
-ENOTSUPPwhen the backend is disabled. Should the pre-existing setter stub also be updated to return-ENOTSUPPto ensure consistent API behavior?
I think Sashiko is correct, better to aligned here, but in a separate patch.
Quoted 33 lines
Quoted 12 lines
drivers/gpio/gpiolib.c +10
-0+/** +* gpiochip_generic_get_config() - read back the configuration of a pin +* @gc: the gpiochip owning the GPIO +* @offset: the offset of the GPIO to query +* @config: the packed parameter to query, replaced by its bare argument +* +* Returns: +* 0 on success, or negative errno on failure. +*/ +int gpiochip_generic_get_config(struct gpio_chip *gc, unsigned int offset,drivers/gpio/gpiolib.c int gpiochip_generic_config(struct gpio_chip *gc, unsigned int offset, · line 2462
Quoted 10 lines
drivers/gpio/gpiolib.c +10
-0+ unsigned long *config) +{ +#ifdef CONFIG_PINCTRL +if (list_empty (&gc->gpiodev->pin_ranges)) +return -ENOTSUPP ; + +return pinctrl_gpio_get_config (gc, offset, config); +#else +return -ENOTSUPP ; +#endif [Severity: High]
This is a pre-existing issue, but I notice this helper explicitly works around the behavior of the fallback stub forpinctrl_gpio_get_config().Since the stub returns 0 without modifying the output config parameter:
include/linux/pinctrl/consumer.h:pinctrl_gpio_get_config() { return 0; }
Quoted 6 lines
direct callers (such as
pin_control_gpio_get_direction()indrivers/gpio/gpio-by-pinctrl.c) are left reading uninitialized memory or query enum values as hardware states whenCONFIG_PINCTRLis disabled.Should the root stub in
include/linux/pinctrl/consumer.hbe fixed to return-ENOTSUPPdirectly to prevent other callers from breaking?
This is also correct, and more critical than the first one. If changing the CONFIG_PINCTRL=n stub in include/linux/pinctrl/consumer.h to return -ENOTSUPP, then I think you can drop the explicit "#else return -ENOTSUPP" in gpiochip_generic_get_config()
It depends on you to send a new version or a separate patch for this.
Regards
Haibo Chen
Quoted 6 lines
Quoted 3 lines
drivers/gpio/gpiolib.c +2
-0+} +EXPORT_SYMBOL_GPL (gpiochip_generic_get_config);Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH v5 2/3] gpiolib: add get_config() and gpiochip_generic_get_config()
From: Mehmet Fide <[email protected]>
Hi Haibo,
Quoted 4 lines · Bough Chen
Quoted 2 lines
Should the pre-existing setter stub also be updated to return
-ENOTSUPPto ensure consistent API behavior?I think Sashiko is correct, better to aligned here, but in a separate patch.
Agreed. v6 will start with a preparatory patch that makes both CONFIG_PINCTRL=n stubs, pinctrl_gpio_get_config() and pinctrl_gpio_set_config(), return -ENOTSUPP. With CONFIG_PINCTRL=y a chip without pin ranges already gets -ENOTSUPP from gpiochip_generic_config(), so every caller of that path handles the value today; the only other users of the stubs, gpio-by-pinctrl and gpio-aspeed, are pinctrl-backed drivers. I will compile-test a CONFIG_PINCTRL=n GPIOLIB build with the change.
Quoted 3 lines
If changing the
CONFIG_PINCTRL=nstub ininclude/linux/pinctrl/consumer.hto return-ENOTSUPP, then I think you can drop the explicit "#else return-ENOTSUPP" ingpiochip_generic_get_config()
Yes, the #else goes away in v6.
Thanks for the review,
Mehmet
Re: [PATCH v5 2/3] gpiolib: add get_config() and gpiochip_generic_get_config()
In reply to Mehmet FideQuoted 14 lines · Mehmet Fide
From: Mehmet Fide <[email protected]>
A chip with a pin control backend sets a line's configuration through
set_config()andgpiochip_generic_config(), but has no way to read one back. gpio-mmio needs that to learn the direction of a line whose direction lives in the pin controller.Add the optional
get_config()callback, taking the packed parameter to query and returning its bare argument the waypinctrl_gpio_get_config()does, andgpiochip_generic_get_config()as the pin control backed implementation, the mirror ofgpiochip_generic_config(). WithoutCONFIG_PINCTRLthe pinctrl stub returns 0 and leaves the config alone, so the helper answers-ENOTSUPPthere instead.
Quoted 5 lines
Nothing in gpiolib calls
get_config()and there is no consumer API; it is for the chip's own use.Suggested-by Linus Walleij <[email protected]>Signed-off-by Mehmet Fide <[email protected]>
Excellent Mehmet, exactly what I had in mind!
Yours,
Linus Walleij
PATCH v5 3/3 gpio: mmio: track the direction of chips without direction registers
In reply to Mehmet FideFrom: Mehmet Fide <[email protected]>
A generic chip with GPIO_GENERIC_PINCTRL_BACKEND and no direction registers sets the direction through pinctrl but has no get_direction callback, so every gpiod_get_direction() call trips the WARN in gpiolib and the direction gpiolib reports is whatever it assumed. On a Vybrid Colibri module that is 21 backtraces per boot.
Keep the direction of such a chip in the existing shadow: the direction setters update sdir under the chip lock, and get_direction() is the shadow-reading path already used for unreadable direction registers. That keeps the callback usable in atomic context, which it has to be:gpiochip_lock_as_irq() calls it for !can_sleep chips from gpiochip_irq_domain_activate(), under the irq descriptor lock.
The pad's actual state is read once, in process context, when a line is
requested: gpiolib calls request() right before get_direction() for a new line, so the shadow is seeded there from PIN_CONFIG_OUTPUT_ENABLE through the chip's get_config(), which is gpiochip_generic_get_config() for the pinctrl backend, and the line reports what the pin controller says. Lines pinctrl cannot answer for keep the input default, which is what gpiolib assumed before.
--- drivers/gpio/gpio-mmio.c | 60 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 4 deletions(-)
drivers/gpio/gpio-mmio.c +56 -4
@@ -49,6 +49,7 @@ o ` ~~~~\___/~~~~ ` controller in FPGA is ,.`#include <linux/log2.h> #include <linux/module.h> #include <linux/pinctrl/consumer.h> +#include <linux/pinctrl/pinconf-generic.h> #include <linux/platform_device.h> #include <linux/property.h> #include <linux/spinlock.h>
@@ -372,7 +373,17 @@ static int gpio_mmio_dir_in_err(struct gpio_chip *gc, unsigned int gpio)static int gpio_mmio_simple_dir_in (struct gpio_chip *gc,unsigned int gpio) { -return gpio_mmio_dir_return (gc, gpio, false); +struct gpio_generic_chip *chip =to_gpio_generic_chip (gc); +int ret; + + ret =gpio_mmio_dir_return (gc, gpio, false); +if (ret) +return ret; + +guard (raw_spinlock_irqsave)(&chip->lock); + chip->sdir &= ~gpio_mmio_line2mask (gc, gpio); + +return 0 ; } static int gpio_mmio_dir_out_err(struct gpio_chip *gc, unsigned int gpio,
@@ -384,9 +395,19 @@ static int gpio_mmio_dir_out_err(struct gpio_chip *gc, unsigned int gpio,static int gpio_mmio_simple_dir_out (struct gpio_chip *gc,unsigned int gpio,int val) { +struct gpio_generic_chip *chip =to_gpio_generic_chip (gc); +int ret; + gc->set (gc, gpio, val); -return gpio_mmio_dir_return (gc, gpio, true); + ret =gpio_mmio_dir_return (gc, gpio, true); +if (ret) +return ret; + +guard (raw_spinlock_irqsave)(&chip->lock); + chip->sdir |=gpio_mmio_line2mask (gc, gpio); + +return 0 ; }static int gpio_mmio_dir_in (struct gpio_chip *gc,unsigned int gpio)
@@ -601,20 +622,51 @@ static int gpio_mmio_setup_direction(struct gpio_generic_chip *chip, gc->direction_input = gpio_mmio_dir_in_err; else gc->direction_input = gpio_mmio_simple_dir_in; + +if (cfg->flags &GPIO_GENERIC_PINCTRL_BACKEND ) { + chip->dir_unreadable = true; + gc->get_direction = gpio_mmio_get_dir; + gc->get_config = gpiochip_generic_get_config; + } }return 0 ; } +static void gpio_mmio_seed_dir_from_pinctrl (struct gpio_chip *gc, +unsigned int gpio) +{ +struct gpio_generic_chip *chip =to_gpio_generic_chip (gc); +unsigned long config; + +if (!gc->get_config || chip->reg_dir_out || chip->reg_dir_in) +return ; + + config =pinconf_to_config_packed (PIN_CONFIG_OUTPUT_ENABLE ,0 ); +if (gc->get_config (gc, gpio, &config)) +return ; + +guard (raw_spinlock_irqsave)(&chip->lock); +if (config) + chip->sdir |=gpio_mmio_line2mask (gc, gpio); +else + chip->sdir &= ~gpio_mmio_line2mask (gc, gpio); +} +static int gpio_mmio_request (struct gpio_chip *gc,unsigned int gpio_pin) {struct gpio_generic_chip *chip =to_gpio_generic_chip (gc); +int ret;if (gpio_pin >= gc->ngpio)return -EINVAL ; -if (chip->pinctrl) -return gpiochip_generic_request (gc, gpio_pin); +if (chip->pinctrl) { + ret =gpiochip_generic_request (gc, gpio_pin); +if (ret) +return ret; +gpio_mmio_seed_dir_from_pinctrl (gc, gpio_pin); + }return 0 ; }
Signature · 1 line
2.54.0
Re: [PATCH v5 3/3] gpio: mmio: track the direction of chips without direction registers
Quoted 15 lines · Mehmet Fide
From: Mehmet Fide <[email protected]>
A generic chip with
GPIO_GENERIC_PINCTRL_BACKENDand no direction registers sets the direction through pinctrl but has noget_directioncallback, so everygpiod_get_direction()call trips the WARN in gpiolib and the direction gpiolib reports is whatever it assumed. On a Vybrid Colibri module that is 21 backtraces per boot.Keep the direction of such a chip in the existing shadow: the direction setters update sdir under the chip lock, and
get_direction()is the shadow-reading path already used for unreadable direction registers. That keeps the callback usable in atomic context, which it has to be:gpiochip_lock_as_irq()calls it for !can_sleep chips fromgpiochip_irq_domain_activate(), under the irq descriptor lock.
Quoted 7 lines
The pad's actual state is read once, in process context, when a line is
requested: gpiolib callsrequest()right beforeget_direction()for a new line, so the shadow is seeded there fromPIN_CONFIG_OUTPUT_ENABLEthrough the chip'sget_config(), which isgpiochip_generic_get_config()for the pinctrl backend, and the line reports what the pin controller says. Lines pinctrl cannot answer for keep the input default, which is what gpiolib assumed before.
dir_unreadable is now set from a second source (PINCTRL_BACKEND with no direction register, in addition to GPIO_GENERIC_UNREADABLE_REG_DIR); worth a line in the commit message.
Regards
Haibo Chen
Quoted 116 lines
Suggested-by Bartosz Golaszewski <[email protected]>Signed-off-by Mehmet Fide <[email protected]>--- drivers/gpio/gpio-mmio.c | 60 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 4 deletions(-)drivers/gpio/gpio-mmio.c +56
-4@@ -49,6 +49,7 @@ o ` ~~~~\___/~~~~ ` controller in FPGA is ,.`#include <linux/log2.h> #include <linux/module.h> #include <linux/pinctrl/consumer.h> +#include <linux/pinctrl/pinconf-generic.h> #include <linux/platform_device.h> #include <linux/property.h> #include <linux/spinlock.h> @@ -372,7 +373,17 @@ static int gpio_mmio_dir_in_err(struct gpio_chip *gc, unsigned int gpio)static int gpio_mmio_simple_dir_in (struct gpio_chip *gc,unsigned int gpio) { -return gpio_mmio_dir_return (gc, gpio, false); +struct gpio_generic_chip *chip =to_gpio_generic_chip (gc); +int ret; + + ret =gpio_mmio_dir_return (gc, gpio, false); +if (ret) +return ret; + +guard (raw_spinlock_irqsave)(&chip->lock); + chip->sdir &= ~gpio_mmio_line2mask (gc, gpio); + +return 0 ; } static int gpio_mmio_dir_out_err(struct gpio_chip *gc, unsigned int gpio,@@ -384,9 +395,19 @@ static int gpio_mmio_dir_out_err(struct gpio_chip *gc, unsigned int gpio,static int gpio_mmio_simple_dir_out (struct gpio_chip *gc,unsigned int gpio,int val) { +struct gpio_generic_chip *chip =to_gpio_generic_chip (gc); +int ret; + gc->set (gc, gpio, val); -return gpio_mmio_dir_return (gc, gpio, true); + ret =gpio_mmio_dir_return (gc, gpio, true); +if (ret) +return ret; + +guard (raw_spinlock_irqsave)(&chip->lock); + chip->sdir |=gpio_mmio_line2mask (gc, gpio); + +return 0 ; }static int gpio_mmio_dir_in (struct gpio_chip *gc,unsigned int gpio)@@ -601,20 +622,51 @@ static int gpio_mmio_setup_direction(struct gpio_generic_chip *chip, gc->direction_input = gpio_mmio_dir_in_err; else gc->direction_input = gpio_mmio_simple_dir_in; + +if (cfg->flags &GPIO_GENERIC_PINCTRL_BACKEND ) { + chip->dir_unreadable = true; + gc->get_direction = gpio_mmio_get_dir; + gc->get_config = gpiochip_generic_get_config; + } }return 0 ; } +static void gpio_mmio_seed_dir_from_pinctrl (struct gpio_chip *gc, +unsigned int gpio) +{ +struct gpio_generic_chip *chip =to_gpio_generic_chip (gc); +unsigned long config; + +if (!gc->get_config || chip->reg_dir_out || chip->reg_dir_in) +return ; + + config =pinconf_to_config_packed (PIN_CONFIG_OUTPUT_ENABLE ,0 ); +if (gc->get_config (gc, gpio, &config)) +return ; + +guard (raw_spinlock_irqsave)(&chip->lock); +if (config) + chip->sdir |=gpio_mmio_line2mask (gc, gpio); +else + chip->sdir &= ~gpio_mmio_line2mask (gc, gpio); +} +static int gpio_mmio_request (struct gpio_chip *gc,unsigned int gpio_pin) {struct gpio_generic_chip *chip =to_gpio_generic_chip (gc); +int ret;if (gpio_pin >= gc->ngpio)return -EINVAL ; -if (chip->pinctrl) -return gpiochip_generic_request (gc, gpio_pin); +if (chip->pinctrl) { + ret =gpiochip_generic_request (gc, gpio_pin); +if (ret) +return ret; +gpio_mmio_seed_dir_from_pinctrl (gc, gpio_pin); + }return 0 ; }Signature · 1 line
2.54.0
Re: [PATCH v5 3/3] gpio: mmio: track the direction of chips without direction registers
From: Mehmet Fide <[email protected]>
Hi Haibo,
Quoted 3 lines · Bough Chen
dir_unreadableis now set from a second source (PINCTRL_BACKENDwith no direction register, in addition toGPIO_GENERIC_UNREADABLE_REG_DIR); worth a line in the commit message.
Will do in v6, together with your Reviewed-by on 1/3 and 3/3. Thanks for checking the i.MX7ULP/i.MX8ULP side as well.
Mehmet
Bartosz Golaszewski
maintainerQuoted 9 lines · Mehmet Fide
From: Mehmet Fide <[email protected]>
Hi Bartosz, Linus,
this replaces the gpiolib guard patch [1], along the lines Bartosz suggested there: instead of teaching
gpiod_get_direction()to stay quiet when a chip has noget_direction(), give gpio-mmio one and let the pin controller tell it what the pad does.
Why are you spamming the list with multiple versions per day? You're sending a v5 as I'm looking at v4 and I really don't like to have my time wasted like this. Please limit yourself to one iteration a week unless you get a set of comments in which case try to stay at two iterations a week at most.
Bartosz
From: Mehmet Fide <[email protected]>
Quoted 4 lines
Why are you spamming the list with multiple versions per day? You're sending a v5 as I'm looking at v4 and I really don't like to have my time wasted like this. Please limit yourself to one iteration a week unless you get a set of comments in which case try to stay at two iterations a week at most.
Sorry, that was wrong of me. v5 is v4 with only the get_config() documentation corrected (it said the answer comes back packed, it is the bare argument), no code change, so please review v5 and ignore v4. No further version from me until you and Linus have had your say, and I will keep to one iteration a week from now on.
Mehmet