[1/1] pinctrl: sunxi: add GPIO get_direction callback

Message ID 20260211033249.2770281-1-james.hilliard1@gmail.com (mailing list archive)
State New
Headers
Series [1/1] pinctrl: sunxi: add GPIO get_direction callback |

Commit Message

James Hilliard Feb. 11, 2026, 3:32 a.m. UTC
Implement sunxi_pinctrl_gpio_get_direction() and wire it into
the sunxi gpio_chip setup.

The new callback reads the pin mux register and compares the mux
value against the pin descriptor gpio_in and gpio_out functions
to report GPIO_LINE_DIRECTION_IN or GPIO_LINE_DIRECTION_OUT.
If the pin is muxed to irq, report it as input.

Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
 drivers/pinctrl/sunxi/pinctrl-sunxi.c | 32 +++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
  

Comments

Chen-Yu Tsai Feb. 11, 2026, 3:49 a.m. UTC | #1
On Wed, Feb 11, 2026 at 11:33 AM James Hilliard
<james.hilliard1@gmail.com> wrote:

Ah, you beat me to it.

> Implement sunxi_pinctrl_gpio_get_direction() and wire it into
> the sunxi gpio_chip setup.

Can you also mention that without this, calls to gpiochip_get_direction()
cause a big warning?

> The new callback reads the pin mux register and compares the mux
> value against the pin descriptor gpio_in and gpio_out functions
> to report GPIO_LINE_DIRECTION_IN or GPIO_LINE_DIRECTION_OUT.
> If the pin is muxed to irq, report it as input.
>
> Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
> ---
>  drivers/pinctrl/sunxi/pinctrl-sunxi.c | 32 +++++++++++++++++++++++++++
>  1 file changed, 32 insertions(+)
>
> diff --git a/drivers/pinctrl/sunxi/pinctrl-sunxi.c b/drivers/pinctrl/sunxi/pinctrl-sunxi.c
> index 0fb057a07dcc..424f23be27b2 100644
> --- a/drivers/pinctrl/sunxi/pinctrl-sunxi.c
> +++ b/drivers/pinctrl/sunxi/pinctrl-sunxi.c
> @@ -995,6 +995,37 @@ static int sunxi_pinctrl_gpio_direction_output(struct gpio_chip *chip,
>                                             chip->base + offset, false);
>  }
>
> +static int sunxi_pinctrl_gpio_get_direction(struct gpio_chip *chip,
> +                                           unsigned int offset)
> +{
> +       struct sunxi_pinctrl *pctl = gpiochip_get_data(chip);
> +       struct sunxi_desc_function *in, *out, *irq;
> +       u32 reg, shift, mask, val;
> +       u16 pin = chip->base + offset;
> +
> +       in = sunxi_pinctrl_desc_find_function_by_pin(pctl, pin, "gpio_in");
> +       out = sunxi_pinctrl_desc_find_function_by_pin(pctl, pin, "gpio_out");
> +       if (!in || !out)
> +               return -EINVAL;
> +
> +       irq = sunxi_pinctrl_desc_find_function_by_pin(pctl, pin, "irq");

This is less efficient, as you end up (linearly) searching the whole pin
array three times.

What I did in my version was implement a search based on pin and muxval.
I can send that out if it's OK with you.


ChenYu

> +
> +       sunxi_mux_reg(pctl, offset, &reg, &shift, &mask);
> +       val = (readl(pctl->membase + reg) & mask) >> shift;
> +
> +       if (val == in->muxval)
> +               return GPIO_LINE_DIRECTION_IN;
> +
> +       if (val == out->muxval)
> +               return GPIO_LINE_DIRECTION_OUT;
> +
> +       /* IRQ function is effectively input. */
> +       if (irq && val == irq->muxval)
> +               return GPIO_LINE_DIRECTION_IN;
> +
> +       return -EINVAL;
> +}
> +
>  static int sunxi_pinctrl_gpio_of_xlate(struct gpio_chip *gc,
>                                 const struct of_phandle_args *gpiospec,
>                                 u32 *flags)
> @@ -1603,6 +1634,7 @@ int sunxi_pinctrl_init_with_flags(struct platform_device *pdev,
>         pctl->chip->set_config = gpiochip_generic_config;
>         pctl->chip->direction_input = sunxi_pinctrl_gpio_direction_input;
>         pctl->chip->direction_output = sunxi_pinctrl_gpio_direction_output;
> +       pctl->chip->get_direction = sunxi_pinctrl_gpio_get_direction;
>         pctl->chip->get = sunxi_pinctrl_gpio_get;
>         pctl->chip->set = sunxi_pinctrl_gpio_set;
>         pctl->chip->of_xlate = sunxi_pinctrl_gpio_of_xlate;
> --
> 2.43.0
>
  
James Hilliard Feb. 11, 2026, 4:04 a.m. UTC | #2
On Tue, Feb 10, 2026 at 8:49 PM Chen-Yu Tsai <wens@kernel.org> wrote:
>
> On Wed, Feb 11, 2026 at 11:33 AM James Hilliard
> <james.hilliard1@gmail.com> wrote:
>
> Ah, you beat me to it.
>
> > Implement sunxi_pinctrl_gpio_get_direction() and wire it into
> > the sunxi gpio_chip setup.
>
> Can you also mention that without this, calls to gpiochip_get_direction()
> cause a big warning?

Yeah, this was mostly just to silence that warning(I don't think I actually
was actually using the functionality).

> > The new callback reads the pin mux register and compares the mux
> > value against the pin descriptor gpio_in and gpio_out functions
> > to report GPIO_LINE_DIRECTION_IN or GPIO_LINE_DIRECTION_OUT.
> > If the pin is muxed to irq, report it as input.
> >
> > Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
> > ---
> >  drivers/pinctrl/sunxi/pinctrl-sunxi.c | 32 +++++++++++++++++++++++++++
> >  1 file changed, 32 insertions(+)
> >
> > diff --git a/drivers/pinctrl/sunxi/pinctrl-sunxi.c b/drivers/pinctrl/sunxi/pinctrl-sunxi.c
> > index 0fb057a07dcc..424f23be27b2 100644
> > --- a/drivers/pinctrl/sunxi/pinctrl-sunxi.c
> > +++ b/drivers/pinctrl/sunxi/pinctrl-sunxi.c
> > @@ -995,6 +995,37 @@ static int sunxi_pinctrl_gpio_direction_output(struct gpio_chip *chip,
> >                                             chip->base + offset, false);
> >  }
> >
> > +static int sunxi_pinctrl_gpio_get_direction(struct gpio_chip *chip,
> > +                                           unsigned int offset)
> > +{
> > +       struct sunxi_pinctrl *pctl = gpiochip_get_data(chip);
> > +       struct sunxi_desc_function *in, *out, *irq;
> > +       u32 reg, shift, mask, val;
> > +       u16 pin = chip->base + offset;
> > +
> > +       in = sunxi_pinctrl_desc_find_function_by_pin(pctl, pin, "gpio_in");
> > +       out = sunxi_pinctrl_desc_find_function_by_pin(pctl, pin, "gpio_out");
> > +       if (!in || !out)
> > +               return -EINVAL;
> > +
> > +       irq = sunxi_pinctrl_desc_find_function_by_pin(pctl, pin, "irq");
>
> This is less efficient, as you end up (linearly) searching the whole pin
> array three times.
>
> What I did in my version was implement a search based on pin and muxval.
> I can send that out if it's OK with you.

If you have a better patch I'll just use that instead of sending
a follow up.

>
>
> ChenYu
>
> > +
> > +       sunxi_mux_reg(pctl, offset, &reg, &shift, &mask);
> > +       val = (readl(pctl->membase + reg) & mask) >> shift;
> > +
> > +       if (val == in->muxval)
> > +               return GPIO_LINE_DIRECTION_IN;
> > +
> > +       if (val == out->muxval)
> > +               return GPIO_LINE_DIRECTION_OUT;
> > +
> > +       /* IRQ function is effectively input. */
> > +       if (irq && val == irq->muxval)
> > +               return GPIO_LINE_DIRECTION_IN;
> > +
> > +       return -EINVAL;
> > +}
> > +
> >  static int sunxi_pinctrl_gpio_of_xlate(struct gpio_chip *gc,
> >                                 const struct of_phandle_args *gpiospec,
> >                                 u32 *flags)
> > @@ -1603,6 +1634,7 @@ int sunxi_pinctrl_init_with_flags(struct platform_device *pdev,
> >         pctl->chip->set_config = gpiochip_generic_config;
> >         pctl->chip->direction_input = sunxi_pinctrl_gpio_direction_input;
> >         pctl->chip->direction_output = sunxi_pinctrl_gpio_direction_output;
> > +       pctl->chip->get_direction = sunxi_pinctrl_gpio_get_direction;
> >         pctl->chip->get = sunxi_pinctrl_gpio_get;
> >         pctl->chip->set = sunxi_pinctrl_gpio_set;
> >         pctl->chip->of_xlate = sunxi_pinctrl_gpio_of_xlate;
> > --
> > 2.43.0
> >
  
Andre Przywara Feb. 11, 2026, 9:08 a.m. UTC | #3
Hi James,

On 2/11/26 04:32, James Hilliard wrote:
> Implement sunxi_pinctrl_gpio_get_direction() and wire it into
> the sunxi gpio_chip setup.

thanks for taking care and sending a patch! I am still scratching my 
head what this warning is about exactly and why we only see this now? I 
never saw this, so what caused that? libgpiod accesses from userland?

Regardless, can we hold back this patch, please? As part of the A733 
pinctrl support (and as LinusW asked kindly about that) I was reworking 
our driver to use the generic gpio interface, and I have a feeling this 
would solve this problem automatically?
The actual pinctrl conversion is done, I just need to plug in the IRQ 
support. I ideally would have something in the next days to post, would 
that be worth waiting for?

Cheers,
Andre



> 
> The new callback reads the pin mux register and compares the mux
> value against the pin descriptor gpio_in and gpio_out functions
> to report GPIO_LINE_DIRECTION_IN or GPIO_LINE_DIRECTION_OUT.
> If the pin is muxed to irq, report it as input.
> 
> Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
> ---
>   drivers/pinctrl/sunxi/pinctrl-sunxi.c | 32 +++++++++++++++++++++++++++
>   1 file changed, 32 insertions(+)
> 
> diff --git a/drivers/pinctrl/sunxi/pinctrl-sunxi.c b/drivers/pinctrl/sunxi/pinctrl-sunxi.c
> index 0fb057a07dcc..424f23be27b2 100644
> --- a/drivers/pinctrl/sunxi/pinctrl-sunxi.c
> +++ b/drivers/pinctrl/sunxi/pinctrl-sunxi.c
> @@ -995,6 +995,37 @@ static int sunxi_pinctrl_gpio_direction_output(struct gpio_chip *chip,
>   					    chip->base + offset, false);
>   }
>   
> +static int sunxi_pinctrl_gpio_get_direction(struct gpio_chip *chip,
> +					    unsigned int offset)
> +{
> +	struct sunxi_pinctrl *pctl = gpiochip_get_data(chip);
> +	struct sunxi_desc_function *in, *out, *irq;
> +	u32 reg, shift, mask, val;
> +	u16 pin = chip->base + offset;
> +
> +	in = sunxi_pinctrl_desc_find_function_by_pin(pctl, pin, "gpio_in");
> +	out = sunxi_pinctrl_desc_find_function_by_pin(pctl, pin, "gpio_out");
> +	if (!in || !out)
> +		return -EINVAL;
> +
> +	irq = sunxi_pinctrl_desc_find_function_by_pin(pctl, pin, "irq");
> +
> +	sunxi_mux_reg(pctl, offset, &reg, &shift, &mask);
> +	val = (readl(pctl->membase + reg) & mask) >> shift;
> +
> +	if (val == in->muxval)
> +		return GPIO_LINE_DIRECTION_IN;
> +
> +	if (val == out->muxval)
> +		return GPIO_LINE_DIRECTION_OUT;
> +
> +	/* IRQ function is effectively input. */
> +	if (irq && val == irq->muxval)
> +		return GPIO_LINE_DIRECTION_IN;
> +
> +	return -EINVAL;
> +}
> +
>   static int sunxi_pinctrl_gpio_of_xlate(struct gpio_chip *gc,
>   				const struct of_phandle_args *gpiospec,
>   				u32 *flags)
> @@ -1603,6 +1634,7 @@ int sunxi_pinctrl_init_with_flags(struct platform_device *pdev,
>   	pctl->chip->set_config = gpiochip_generic_config;
>   	pctl->chip->direction_input = sunxi_pinctrl_gpio_direction_input;
>   	pctl->chip->direction_output = sunxi_pinctrl_gpio_direction_output;
> +	pctl->chip->get_direction = sunxi_pinctrl_gpio_get_direction;
>   	pctl->chip->get = sunxi_pinctrl_gpio_get;
>   	pctl->chip->set = sunxi_pinctrl_gpio_set;
>   	pctl->chip->of_xlate = sunxi_pinctrl_gpio_of_xlate;
  
Chen-Yu Tsai Feb. 11, 2026, 9:54 a.m. UTC | #4
On Wed, Feb 11, 2026 at 5:08 PM Andre Przywara <andre.przywara@arm.com> wrote:
>
> Hi James,
>
> On 2/11/26 04:32, James Hilliard wrote:
> > Implement sunxi_pinctrl_gpio_get_direction() and wire it into
> > the sunxi gpio_chip setup.
>
> thanks for taking care and sending a patch! I am still scratching my
> head what this warning is about exactly and why we only see this now? I
> never saw this, so what caused that? libgpiod accesses from userland?

For me it was `cat /sys/kernel/debug/gpio`.

> Regardless, can we hold back this patch, please? As part of the A733
> pinctrl support (and as LinusW asked kindly about that) I was reworking
> our driver to use the generic gpio interface, and I have a feeling this
> would solve this problem automatically?
> The actual pinctrl conversion is done, I just need to plug in the IRQ
> support. I ideally would have something in the next days to post, would
> that be worth waiting for?

We probably want something for the stable kernels though. The commit
that adds the warning was merged sometime last year, so it's part
of the latest LTS.


ChenYu

> Cheers,
> Andre
>
>
>
> >
> > The new callback reads the pin mux register and compares the mux
> > value against the pin descriptor gpio_in and gpio_out functions
> > to report GPIO_LINE_DIRECTION_IN or GPIO_LINE_DIRECTION_OUT.
> > If the pin is muxed to irq, report it as input.
> >
> > Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
> > ---
> >   drivers/pinctrl/sunxi/pinctrl-sunxi.c | 32 +++++++++++++++++++++++++++
> >   1 file changed, 32 insertions(+)
> >
> > diff --git a/drivers/pinctrl/sunxi/pinctrl-sunxi.c b/drivers/pinctrl/sunxi/pinctrl-sunxi.c
> > index 0fb057a07dcc..424f23be27b2 100644
> > --- a/drivers/pinctrl/sunxi/pinctrl-sunxi.c
> > +++ b/drivers/pinctrl/sunxi/pinctrl-sunxi.c
> > @@ -995,6 +995,37 @@ static int sunxi_pinctrl_gpio_direction_output(struct gpio_chip *chip,
> >                                           chip->base + offset, false);
> >   }
> >
> > +static int sunxi_pinctrl_gpio_get_direction(struct gpio_chip *chip,
> > +                                         unsigned int offset)
> > +{
> > +     struct sunxi_pinctrl *pctl = gpiochip_get_data(chip);
> > +     struct sunxi_desc_function *in, *out, *irq;
> > +     u32 reg, shift, mask, val;
> > +     u16 pin = chip->base + offset;
> > +
> > +     in = sunxi_pinctrl_desc_find_function_by_pin(pctl, pin, "gpio_in");
> > +     out = sunxi_pinctrl_desc_find_function_by_pin(pctl, pin, "gpio_out");
> > +     if (!in || !out)
> > +             return -EINVAL;
> > +
> > +     irq = sunxi_pinctrl_desc_find_function_by_pin(pctl, pin, "irq");
> > +
> > +     sunxi_mux_reg(pctl, offset, &reg, &shift, &mask);
> > +     val = (readl(pctl->membase + reg) & mask) >> shift;
> > +
> > +     if (val == in->muxval)
> > +             return GPIO_LINE_DIRECTION_IN;
> > +
> > +     if (val == out->muxval)
> > +             return GPIO_LINE_DIRECTION_OUT;
> > +
> > +     /* IRQ function is effectively input. */
> > +     if (irq && val == irq->muxval)
> > +             return GPIO_LINE_DIRECTION_IN;
> > +
> > +     return -EINVAL;
> > +}
> > +
> >   static int sunxi_pinctrl_gpio_of_xlate(struct gpio_chip *gc,
> >                               const struct of_phandle_args *gpiospec,
> >                               u32 *flags)
> > @@ -1603,6 +1634,7 @@ int sunxi_pinctrl_init_with_flags(struct platform_device *pdev,
> >       pctl->chip->set_config = gpiochip_generic_config;
> >       pctl->chip->direction_input = sunxi_pinctrl_gpio_direction_input;
> >       pctl->chip->direction_output = sunxi_pinctrl_gpio_direction_output;
> > +     pctl->chip->get_direction = sunxi_pinctrl_gpio_get_direction;
> >       pctl->chip->get = sunxi_pinctrl_gpio_get;
> >       pctl->chip->set = sunxi_pinctrl_gpio_set;
> >       pctl->chip->of_xlate = sunxi_pinctrl_gpio_of_xlate;
>
  
Linus Walleij Feb. 23, 2026, 10:25 a.m. UTC | #5
On Wed, Feb 11, 2026 at 10:08 AM Andre Przywara <andre.przywara@arm.com> wrote:

> thanks for taking care and sending a patch! I am still scratching my
> head what this warning is about exactly and why we only see this now? I
> never saw this, so what caused that? libgpiod accesses from userland?

I don't understand that either.

> Regardless, can we hold back this patch, please? As part of the A733
> pinctrl support (and as LinusW asked kindly about that) I was reworking
> our driver to use the generic gpio interface, and I have a feeling this
> would solve this problem automatically?
> The actual pinctrl conversion is done, I just need to plug in the IRQ
> support. I ideally would have something in the next days to post, would
> that be worth waiting for?

We are early in the v7.1 development cycle, so we certainly have
time for this to materialize and figure out how to consolidate all
of the new development.

Yours,
Linus Walleij
  

Patch

diff --git a/drivers/pinctrl/sunxi/pinctrl-sunxi.c b/drivers/pinctrl/sunxi/pinctrl-sunxi.c
index 0fb057a07dcc..424f23be27b2 100644
--- a/drivers/pinctrl/sunxi/pinctrl-sunxi.c
+++ b/drivers/pinctrl/sunxi/pinctrl-sunxi.c
@@ -995,6 +995,37 @@  static int sunxi_pinctrl_gpio_direction_output(struct gpio_chip *chip,
 					    chip->base + offset, false);
 }
 
+static int sunxi_pinctrl_gpio_get_direction(struct gpio_chip *chip,
+					    unsigned int offset)
+{
+	struct sunxi_pinctrl *pctl = gpiochip_get_data(chip);
+	struct sunxi_desc_function *in, *out, *irq;
+	u32 reg, shift, mask, val;
+	u16 pin = chip->base + offset;
+
+	in = sunxi_pinctrl_desc_find_function_by_pin(pctl, pin, "gpio_in");
+	out = sunxi_pinctrl_desc_find_function_by_pin(pctl, pin, "gpio_out");
+	if (!in || !out)
+		return -EINVAL;
+
+	irq = sunxi_pinctrl_desc_find_function_by_pin(pctl, pin, "irq");
+
+	sunxi_mux_reg(pctl, offset, &reg, &shift, &mask);
+	val = (readl(pctl->membase + reg) & mask) >> shift;
+
+	if (val == in->muxval)
+		return GPIO_LINE_DIRECTION_IN;
+
+	if (val == out->muxval)
+		return GPIO_LINE_DIRECTION_OUT;
+
+	/* IRQ function is effectively input. */
+	if (irq && val == irq->muxval)
+		return GPIO_LINE_DIRECTION_IN;
+
+	return -EINVAL;
+}
+
 static int sunxi_pinctrl_gpio_of_xlate(struct gpio_chip *gc,
 				const struct of_phandle_args *gpiospec,
 				u32 *flags)
@@ -1603,6 +1634,7 @@  int sunxi_pinctrl_init_with_flags(struct platform_device *pdev,
 	pctl->chip->set_config = gpiochip_generic_config;
 	pctl->chip->direction_input = sunxi_pinctrl_gpio_direction_input;
 	pctl->chip->direction_output = sunxi_pinctrl_gpio_direction_output;
+	pctl->chip->get_direction = sunxi_pinctrl_gpio_get_direction;
 	pctl->chip->get = sunxi_pinctrl_gpio_get;
 	pctl->chip->set = sunxi_pinctrl_gpio_set;
 	pctl->chip->of_xlate = sunxi_pinctrl_gpio_of_xlate;