Skip to content
systemcoding.dev
Embedded Linux

Configure Pin Multiplexing With pinctrl in a Device Tree

Select a pin's function with pinctrl-names and pinctrl-0, define groups in the SoC's pin controller node and check the result in debugfs.

6 min read
Summarize in AI
Flat illustration of a single-board computer with its SoC, memory, header and ports, with the Linux, Debian and Raspberry Pi logos.

A modern SoC has more peripherals than package pins, so most pins can do several jobs. The same ball may be GPIO 8, the transmit line of UART 0, or a data line of an SPI controller, and a small block of hardware, the pin controller, decides which. Until that block is told, the UART you enabled in the device tree is connected to nothing, and the board is silent for a reason no driver message will state. The device tree solves it with the pinctrl bindings: a device names the pin configuration it needs, and the pin controller driver applies it before the device's own driver starts. This post shows the two halves, using the generic bindings from Linux 6.12 with an Allwinner style controller, and how to inspect the result.

Two halves: the controller and the consumer

The system has two kinds of nodes.

The pin controller node belongs to the SoC. It describes the controller's registers and defines named groups of pin settings, usually in its children. SoC vendors write these, and boards reuse them.

A consumer is any device that needs pins: a UART, an I2C bus, an SPI controller. It refers to a group by phandle, and the kernel's pinctrl core asks the controller driver to apply it.

The consumer side has two properties:

dts
&uart0 {
	pinctrl-names = "default";
	pinctrl-0 = <&uart0_pb_pins>;
	status = "okay";
};

pinctrl-names lists the states the device can be in, in order, and each state is numbered from zero. pinctrl-0 is the list of pin groups for state 0, pinctrl-1 for state 1 and so on. The name default is special: the driver core selects it automatically before it calls probe(), so the driver needs no pinctrl code at all. A device that supports suspend can name a second state:

dts
	pinctrl-names = "default", "sleep";
	pinctrl-0 = <&uart0_pb_pins>;
	pinctrl-1 = <&uart0_sleep_pins>;

The sleep state is applied by the driver core when the device suspends, which lets you tri-state or pull the pins to save power while the board sleeps.

The controller side

The groups live in the pin controller's node, which the SoC's .dtsi defines. On the Allwinner H5 style controllers the UART group reads:

dts
pio: pinctrl@1c20800 {
	compatible = "allwinner,sun50i-h5-pinctrl";
	reg = <0x01c20800 0x400>;
	/* clocks, interrupts and gpio-controller properties omitted */

	uart0_pa_pins: uart0-pa-pins {
		pins = "PA4", "PA5";
		function = "uart0";
	};
};

This uses the generic pin configuration bindings: pins lists the pin names the controller recognizes, and function names the peripheral to connect them to. The names are the controller's own, so you read them from its binding file under Documentation/devicetree/bindings/pinctrl/. A different vendor uses a different shape. Freescale's i.MX controllers, for example, use one property, fsl,pins, holding triples of a pin mux macro and a pad configuration value, and ST's STM32 uses pinmux with macros that combine port, pin and alternate function. The consumer side is the same everywhere. Only the group's contents change.

Settings other than the function go into the same group or a sub-node. The generic bindings define bias-pull-up, bias-pull-down, bias-disable, drive-strength (in milliamps), input-enable and several others:

dts
i2c1_pins: i2c1-pins {
	pins = "PA18", "PA19";
	function = "i2c1";
	bias-pull-up;
	drive-strength = <10>;
};

Which properties apply to which pin is the controller's decision. Setting a property the driver does not support is not an error in the device tree, but the driver logs a message or ignores it, so read dmesg after the first boot.

Check what the kernel applied

The pin controller exposes its state in debugfs. Mount it if it is not already:

Shell
mount -t debugfs none /sys/kernel/debug
ls /sys/kernel/debug/pinctrl/
Text
1c20800.pinctrl-pinctrl-sun50i-h5
pinctrl-devices
pinctrl-handles
pinctrl-maps

The first directory is your controller. Read its pinmux-pins file to see who owns each pin:

Shell
grep -E "pin (4|5) " /sys/kernel/debug/pinctrl/1c20800.pinctrl-pinctrl-sun50i-h5/pinmux-pins
Text
pin 4 (PA4): device 1c28000.serial function uart0 group PA4
pin 5 (PA5): device 1c28000.serial function uart0 group PA5

The device name 1c28000.serial is the UART's platform device, and function uart0 confirms the mux was applied. A pin nobody uses reads UNCLAIMED. The pinctrl-maps file lists every state mapping the kernel built from the device tree, which shows whether your pinctrl-0 was parsed at all.

The device you enabled and the pin the board uses must match. A UART that works on PA4 and PA5 in the board's schematic and is described with uart0_pb_pins (the PB pins) will probe, print nothing and look healthy. Compare the physical pin names on the schematic with the group you picked.

The errors you will see

Pin claimed twice. Two devices ask for the same pin, and the second fails:

Text
sun50i-h5-pinctrl 1c20800.pinctrl: pin PA4 already requested by 1c28000.serial; cannot claim for 1c2ac00.i2c
sun50i-h5-pinctrl 1c20800.pinctrl: pin-4 (1c2ac00.i2c) status -22
sun50i-h5-pinctrl 1c20800.pinctrl: could not request pin 4 (PA4) from group PA4 on device 1c20800.pinctrl

The exact wording and the numbers belong to the controller and the pins. The cure is to remove one user, or to pick the other group in the board file. A hog, described below, can also be the second owner.

Unknown function or pin. A typo in function or a pin the controller does not have gives invalid function or unsupported pin from pinctrl_generic_to_map-style messages. Compare the strings with the binding, character by character, since they are case sensitive.

Missing pinctrl-names. A pinctrl-0 without pinctrl-names is ignored: the core finds no state named default to apply, and the device runs with whatever the bootloader left. It often works on the bench because U-Boot did the muxing, and fails on the next board.

The group is applied but the signal is wrong. The mux is right and the line is still dead. Check the bias and drive properties, the pull-up, and whether another block, such as a JTAG or a boot strap, is still driving the pin.

Hogs for pins no driver owns

Some pins have no consumer driver, such as a GPIO that must stay at a fixed level or a pin routed to an external connector. A hog applies a state with no device attached, by putting the settings under the controller node with a pinctrl-names of its own:

dts
&pio {
	pinctrl-names = "default";
	pinctrl-0 = <&led_pins>;
};

Alternatively, gpio-hog nodes on a GPIO controller drive a fixed value at boot. Use these sparingly: a hog hides a decision in a place no driver documents.

A checklist for a peripheral that stays silent

When a newly enabled device does nothing, check the pins in the same order every time, from the board inward. First, the schematic: write down the pin names and the function the board expects. Second, the group: confirm that the pins list in the group you referenced names those same pins, and that function matches the peripheral instance, because uart0 and uart1 on the same pins are different settings. Third, the consumer: confirm pinctrl-names = "default" is present and that the node's status is okay. Fourth, debugfs: read pinmux-pins and confirm the right device owns the pin. Fifth, the electrical side: check bias and drive, and measure the line.

Most failures end at step one or two. The fifth is where the remaining few land, and it is the one where the kernel has no more to tell you.

Keep the pin groups close to what they configure. In a board file, define a group next to the &uart0 block that uses it when it is specific to the board, and in the SoC .dtsi when every board would pick the same pins. A group nobody uses costs nothing at run time but adds noise, and the compiler will not flag it.

Also inHardware
Share

Keep reading

Flat illustration of user space above the system call boundary, the kernel and the hardware, with the Linux, C and GNU logos.
Kernels

Write a Pin Controller Driver With pinctrl

Write a Linux 6.12 pin controller driver: pinctrl_desc, pins, groups and functions, the set_mux callback, and the device tree states that consumers select.

8 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.