Skip to contentSkip to conversation

pinctrl: sunxi: Introduce DT-based pinctrl builder

RFC PATCH In review Andre Przywara 2 participants 6 messages Nov 10, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover pinctrl: sunxi: Introduce DT-based pinctrl builder

Hi,

since the dawn of time every Allwinner SoC dumped a rather large table of data into the kernel, to describe the mapping between the pinctrl function name and its mux value, for each pin.

This series introduces code that avoids that (for new SoCs), by instead reading that information directly from the devicetree. We have per-pin group nodes there anyway, and were just missing the mux value.

Compared to my previous effort almost exactly five years ago [1], this new version drops the idea of describing the pinctrl data entirely in the DT, instead it still relies on driver provided information for that. That is more flexible, since it allows to introduce quirks and special handling more cleanly, at the cost of still requiring a separate driver file for each SoC. However this file is now very small, and can be easily written and reviewed. All that is needed is the number of pins per bank, plus information about each bank's IRQ capability. Patch 2/2 shows an example, for the yet unsupported Allwinner V5 SoC.

On the DT side all that would be needed is *one* extra property per pin group to announce the mux value:

	uart0_pb_pins: uart0-pb-pins {
		pins = "PB9", "PB10";
		function = "uart0";
		pinmux = <2>;
	};

The new code works by providing a function that builds the former mapping table *at runtime*, by using both the driver provided information, plus traversing all children of the pinctrl DT node, to find all pin groups needed. This table looks the same as what we hardcoded so far, so can easily be digested by the existing sunxi pinctrl driver.

Please have a look and tell me whether this new approach has a better future than my previous attempt.

Cheers,
Andre

[1] https://patchwork.ozlabs.org/project/linux-gpio/cover/[email protected]/

Andre Przywara (2):
pinctrl: sunxi: allow reading mux values from DT
pinctrl: sunxi: Add support for the Allwinner V5 pin controller

 drivers/pinctrl/sunxi/Kconfig            |   5 +
 drivers/pinctrl/sunxi/Makefile           |   2 +
 drivers/pinctrl/sunxi/pinctrl-sun8i-v5.c |  52 ++++
 drivers/pinctrl/sunxi/pinctrl-sunxi-dt.c | 355 +++++++++++++++++++++++
 drivers/pinctrl/sunxi/pinctrl-sunxi.h    |   8 +
 5 files changed, 422 insertions(+)
 create mode 100644 drivers/pinctrl/sunxi/pinctrl-sun8i-v5.c
 create mode 100644 drivers/pinctrl/sunxi/pinctrl-sunxi-dt.c
Signature · 1 line
2.35.5
#1 lore raw

RFC PATCH 1/2 pinctrl: sunxi: allow reading mux values from DT

So far every Allwinner SoC needs a large table in the kernel code, to describe the mapping between the pinctrl function names ("uart") and the actual pincontroller mux value to be written into the registers. This adds a lot of data into a single image kernel, and also looks somewhat weird, as the DT can easily store the mux value.

Add some code that allows to avoid that table: the struct that describes the existing pins will be build at *runtime*, based on very basic information provided by the respective SoC's pinctrl driver. This consists of the number of pins per bank, plus information which bank provides IRQ support, along with the mux value to use for that. The code will then iterate over all children of the pincontroller DT node (which describe each pin group), and populate that struct with the mapping between function names and mux values. The only thing that needs adding in the DT is a property with that value, per pin group.

When this table is built, it will be handed over to the existing sunxi pinctrl driver, which cannot tell a difference between a hardcoded struct and this new one built at runtime. It will take care of registering the pinctrl device with the pinctrl subsystem.

All a new SoC driver would need to do is to provide two arrays, and then call the sunxi_pinctrl_dt_table_init() function.

---
 drivers/pinctrl/sunxi/Makefile           |   1 +
 drivers/pinctrl/sunxi/pinctrl-sunxi-dt.c | 355 +++++++++++++++++++++++
 drivers/pinctrl/sunxi/pinctrl-sunxi.h    |   8 +
 3 files changed, 364 insertions(+)
 create mode 100644 drivers/pinctrl/sunxi/pinctrl-sunxi-dt.c
drivers/pinctrl/sunxi/Makefile +1 -0
index 2ff5a55927ad..f5bad7a52951 100644
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0
 # Core
 obj-y					+= pinctrl-sunxi.o
+obj-y					+= pinctrl-sunxi-dt.o
 
 # SoC Drivers
 obj-$(CONFIG_PINCTRL_SUNIV_F1C100S)	+= pinctrl-suniv-f1c100s.o
drivers/pinctrl/sunxi/pinctrl-sunxi-dt.c new file +355 -0

355 lines not on this page · raw message

drivers/pinctrl/sunxi/pinctrl-sunxi.h +8 -0
index a87a2f944d60..2305d05c70f8 100644
@@ -29,6 +29,9 @@
 #define PM_BASE	384
 #define PN_BASE	416
 
+/* maximum number of banks per controller (PA -> PK) */
+#define SUNXI_PINCTRL_MAX_BANKS	11
+
 #define SUNXI_PINCTRL_PIN(bank, pin)		\
 	PINCTRL_PIN(P ## bank ## _BASE + (pin), "P" #bank #pin)
 
@@ -306,4 +309,9 @@ int sunxi_pinctrl_init_with_variant(struct platform_device *pdev,
 #define sunxi_pinctrl_init(_dev, _desc) \
 	sunxi_pinctrl_init_with_variant(_dev, _desc, 0)
 
+int sunxi_pinctrl_dt_table_init(struct platform_device *pdev,
+				const u8 *pins_per_bank,
+				const u8 *irq_bank_muxes,
+				struct sunxi_pinctrl_desc *desc);
+
 #endif /* __PINCTRL_SUNXI_H */
Signature · 1 line
2.35.5
#2 lore raw

RFC PATCH 2/2 pinctrl: sunxi: Add support for the Allwinner V5 pin controller

In reply to Andre Przywara

The Allwinner V5 contains pins in all 10 possible pin banks. Use the newly introduced DT based pinctrl driver to describe just the generic pinctrl properties, so advertise the number of pins per bank and the interrupt capabilities. The actual function/mux assignment is taken from the devicetree.

---
 drivers/pinctrl/sunxi/Kconfig            |  5 +++
 drivers/pinctrl/sunxi/Makefile           |  1 +
 drivers/pinctrl/sunxi/pinctrl-sun8i-v5.c | 52 ++++++++++++++++++++++++
 3 files changed, 58 insertions(+)
 create mode 100644 drivers/pinctrl/sunxi/pinctrl-sun8i-v5.c
drivers/pinctrl/sunxi/Kconfig +5 -0
index a78fdbbdfc0c..6b8ea56c08fd 100644
@@ -131,4 +131,9 @@ config PINCTRL_SUN50I_H616_R
 	default ARM64 && ARCH_SUNXI
 	select PINCTRL_SUNXI
 
+config PINCTRL_SUN8I_V5
+	bool "Support for the Allwinner V5 PIO"
+	default MACH_SUN8I
+	select PINCTRL_SUNXI
+
 endif
drivers/pinctrl/sunxi/Makefile +1 -0
index f5bad7a52951..5b289e18bd5a 100644
@@ -21,6 +21,7 @@ obj-$(CONFIG_PINCTRL_SUN8I_A83T_R)	+= pinctrl-sun8i-a83t-r.o
 obj-$(CONFIG_PINCTRL_SUN8I_H3)		+= pinctrl-sun8i-h3.o
 obj-$(CONFIG_PINCTRL_SUN8I_H3_R)	+= pinctrl-sun8i-h3-r.o
 obj-$(CONFIG_PINCTRL_SUN8I_V3S)		+= pinctrl-sun8i-v3s.o
+obj-$(CONFIG_PINCTRL_SUN8I_V5)		+= pinctrl-sun8i-v5.o
 obj-$(CONFIG_PINCTRL_SUN20I_D1)		+= pinctrl-sun20i-d1.o
 obj-$(CONFIG_PINCTRL_SUN50I_H5)		+= pinctrl-sun50i-h5.o
 obj-$(CONFIG_PINCTRL_SUN50I_H6)		+= pinctrl-sun50i-h6.o
drivers/pinctrl/sunxi/pinctrl-sun8i-v5.c new file +52 -0

52 lines not on this page · raw message

Signature · 1 line
2.35.5
#3 lore raw
In reply to Andre Przywara
Quoted 3 lines · Andre Przywara

Compared to my previous effort almost exactly five years ago [1], this new version drops the idea of describing the pinctrl data entirely in the DT, instead it still relies on driver provided information for that.

(...)

Quoted 8 lines

On the DT side all that would be needed is *one* extra property per pin group to announce the mux value:

        uart0_pb_pins: uart0-pb-pins {
                pins = "PB9", "PB10";
                function = "uart0";
                pinmux = <2>;
        };

So what you need to do is to convince the device tree people that this is a good idea.

For me as linux maintainer it's no big deal, it's fine either way. The new code looks elegant.

But from a DT point of view this needs to make sense also for Windows and BSD, so that is who you have to convince. If it is possible to derive the same information from the compatible string (like today) that will need an extended argument why all operating systems will benefit from this.

Yours,
Linus Walleij

#4 lore raw

On Thu, 10 Nov 2022 11:21:02 +0100
Linus Walleij <[email protected]> wrote:

Hi Linus,

thanks for having a look!

Quoted 18 lines · Andre Przywara
Quoted 4 lines

Compared to my previous effort almost exactly five years ago [1], this new version drops the idea of describing the pinctrl data entirely in the DT, instead it still relies on driver provided information for that.

(...)

Quoted 8 lines

On the DT side all that would be needed is *one* extra property per pin group to announce the mux value:

        uart0_pb_pins: uart0-pb-pins {
                pins = "PB9", "PB10";
                function = "uart0";
                pinmux = <2>;
        };  

So what you need to do is to convince the device tree people that this is a good idea.

Quoted 8 lines

For me as linux maintainer it's no big deal, it's fine either way. The new code looks elegant.

But from a DT point of view this needs to make sense also for Windows and BSD, so that is who you have to convince. If it is possible to derive the same information from the compatible string (like today) that will need an extended argument why all operating systems will benefit from this.

This is actually an argument in favour of it: at the moment *every* OS (or DT user) has to carry some form of this table[1]. For U-Boot this is a major pain, for instance, and we came up with some minimal and simplified version of that (assuming one pinmux per function name, ignoring different mappings in different ports: [2]), but we are already touching its limits.
And I don't think this DT argument counts anyway: we already store a much bigger chunk of "information" in the DT, namely the function name. This has no technical meaning, really, other than to map this to a 4-bit value internally. I don't know why we have an information like "UART0 is using the 'uart0' pin group" in the DT, but refuse to put the actual hardware information in there. We could possibly even get rid of the string, and derive this from the node name, if we need some human readable identifier.

And just to make sure: I don't propose to change this for existing DTs, it's just for new SoCs going forward. Allwinner at the moment spins out many SoCs with only little differences, but all require this largish table, since the pin assignments are the ones that differ.

Cheers,
Andre

[1]
https://github.com/freebsd/freebsd-src/blob/main/sys/arm/allwinner/a64/a64_padconf.c
[2]
https://source.denx.de/u-boot/u-boot/-/blob/master/drivers/pinctrl/sunxi/pinctrl-sunxi.c#L587-605

#5 lore raw
Quoted 6 lines · Andre Przywara

This is actually an argument in favour of it: at the moment *every* OS (or DT user) has to carry some form of this table[1]. For U-Boot this is a major pain, for instance, and we came up with some minimal and simplified version of that (assuming one pinmux per function name, ignoring different mappings in different ports: [2]), but we are already touching its limits.

That's a compelling argument.

I don't know about reusing the existing pinmux property in a new way which differs from the standard one:

pinmux:
description:
The list of numeric pin ids and their mux settings that properties in the node apply to (either this, "pins" or "groups" have to be specified)
$ref: /schemas/types.yaml#/definitions/uint32-array

You should rather invent something like "sunxi,pin-mux-val" or so.

That makes me happy to merge it at least, I don't see any problem with it.

Quoted 8 lines

And I don't think this DT argument counts anyway: we already store a much bigger chunk of "information" in the DT, namely the function name. This has no technical meaning, really, other than to map this to a 4-bit value internally. I don't know why we have an information like "UART0 is using the 'uart0' pin group" in the DT, but refuse to put the actual hardware information in there. We could possibly even get rid of the string, and derive this from the node name, if we need some human readable identifier.

These exist for consistency and maintenance, they are established standard bindings:
Documentation/devicetree/bindings/pinctrl/pinmux-node.yaml

Associating a function "uart0" with something like [ "uart0-txrx", uart0-rtscts" ] by a line such as:

groups = "uart0-txrx", "uart0-rtscts";

is simple to understand, and makes it easier for maintainers who have to look at a lot of different platforms with different muxes. So these are there for human readability.

I.e. the goal of standard properties is not to minimize amount of information (which is your goal here) but to structure things in a way that makes maintenance easier by being similar on several platforms.

Some systems deviate from standard properties, and admittedly this way of structuring things with strings is maybe not the ultimate. The most deviating one is:
Documentation/devicetree/bindings/pinctrl/pinctrl-single.txt which is used by OMAP and HiSilicon.
Some feel this should never have been merged.
But it was merged.
By me.

There is also the pinmux property above which some systems use for putting in enumerated magic. That's part of the standard but makes the standard somewhat incoherent.

This difference in pin control DT patterns is because we could not come up with something that was acceptable for all and the result of some diplomacy when the subsystem was created around 2011-2012.

The thing is that I am not a very consistent and stubborn person and I think more along the lines of the IETF motto "rough consensus and running code". The DT people were different back in 2011, and also softer around the edges, not as strict and not insisting on things being done one way for all systems. Their ambitions have stepped up since. So these are the people you need to convince.

I suggest you propose the bindings with the patch set by Cc [email protected] using the custom "sunxi,pin" or whatever name you want and see if the DT reviewers agree with this solution.

If they say nothing in like 2 weeks I usually merge things that I find non-objectionable anyways.

Yours,
Linus Walleij