From: Mehmet Fide <mehmet.fide@gmail.com>
To: Bartosz Golaszewski <brgl@kernel.org>, Linus Walleij <linusw@kernel.org>
Cc: Dong Aisheng <aisheng.dong@nxp.com>,
Fabio Estevam <festevam@gmail.com>, Frank Li <Frank.Li@nxp.com>,
Jacky Bai <ping.bai@nxp.com>,
Sascha Hauer <s.hauer@pengutronix.de>,
Pengutronix Kernel Team <kernel@pengutronix.de>,
imx@lists.linux.dev, linux-gpio@vger.kernel.org,
linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org,
Mehmet Fide <mehmet.fide@screeningeagle.com>
Subject: [PATCH v2 2/2] gpio: mmio: track the direction of chips without direction registers
Date: Wed, 2 Sep 2026 09:39:46 +0200 [thread overview]
Message-ID: <20260902073946.3709080-3-mehmet.fide@gmail.com> (raw)
In-Reply-To: <20260902073946.3709080-1-mehmet.fide@gmail.com>
From: Mehmet Fide <mehmet.fide@screeningeagle.com>
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 from PIN_CONFIG_OUTPUT_ENABLE there and
the line reports what the pin controller says. Lines pinctrl cannot
answer for keep the input default, which is what gpiolib assumed before.
Suggested-by: Bartosz Golaszewski <brgl@kernel.org>
Signed-off-by: Mehmet Fide <mehmet.fide@screeningeagle.com>
---
v2:
- keep the direction in the chip's shadow and reuse the shadow-reading
get_direction(); seed the shadow from pinctrl in request(), because
gpiochip_lock_as_irq() calls get_direction() under the irq descriptor
lock for !can_sleep chips (Sashiko)
- lines pinctrl cannot answer for keep the input default, no error
returned to gpiochip_lock_as_irq() (Sashiko)
- drop the claim about the initial direction scan, it runs before the
pin ranges exist (Sashiko)
drivers/gpio/gpio-mmio.c | 65 +++++++++++++++++++++++++++++++++++++---
1 file changed, 61 insertions(+), 4 deletions(-)
drivers/gpio/gpio-mmio.c | 63 +++++++++++++++++++++++++++++++++++++---
1 file changed, 59 insertions(+), 4 deletions(-)
diff --git a/drivers/gpio/gpio-mmio.c b/drivers/gpio/gpio-mmio.c
index 7e4b3e8d609f..84bf8cc7a0c7 100644
--- a/drivers/gpio/gpio-mmio.c
+++ b/drivers/gpio/gpio-mmio.c
@@ -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,54 @@ 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;
+ }
}
return 0;
}
+/*
+ * Without direction registers the direction lives in the pin controller,
+ * so seed the shadow from there when a line is requested.
+ */
+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 (!IS_ENABLED(CONFIG_PINCTRL) || chip->reg_dir_out || chip->reg_dir_in)
+ return;
+
+ config = pinconf_to_config_packed(PIN_CONFIG_OUTPUT_ENABLE, 0);
+ if (pinctrl_gpio_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;
}
--
2.54.0
prev parent reply other threads:[~2026-09-02 7:39 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-02 7:39 [PATCH v2 0/2] gpio: mmio: report the line direction on chips without direction registers Mehmet Fide
2026-09-02 7:39 ` [PATCH v2 1/2] pinctrl: imx: answer OUTPUT_ENABLE/INPUT_ENABLE queries from the pad register Mehmet Fide
2026-09-02 7:56 ` sashiko-bot
2026-09-02 11:08 ` Mehmet Fide
2026-09-02 7:39 ` Mehmet Fide [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260902073946.3709080-3-mehmet.fide@gmail.com \
--to=mehmet.fide@gmail.com \
--cc=Frank.Li@nxp.com \
--cc=aisheng.dong@nxp.com \
--cc=brgl@kernel.org \
--cc=festevam@gmail.com \
--cc=imx@lists.linux.dev \
--cc=kernel@pengutronix.de \
--cc=linusw@kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-gpio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mehmet.fide@screeningeagle.com \
--cc=ping.bai@nxp.com \
--cc=s.hauer@pengutronix.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is 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.