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.

A modern SoC has far more peripheral signals than it has pins. A single ball on the package might be a GPIO, a UART transmit line, an I2C clock or a PWM output, and a multiplexer in the chip decides which. The pinctrl subsystem is how Linux drives that multiplexer. A pin controller driver describes the pins, the groups of pins that make up a peripheral's signals, and the functions those groups can take, and the subsystem then applies the right configuration whenever a consumer driver's device tree node asks for it. This post writes a pin controller on Linux 6.12 for an eight-pin block with a simple mux register, and explains the three tables, the callbacks and the device tree states. It sits beside the GPIO controller driver, which reads and writes pins that the pin controller has already made into GPIOs.
The framework is described in the kernel's PINCTRL subsystem documentation.
Pins, groups and functions
Three concepts organize the work:
| Concept | Meaning | Example |
|---|---|---|
| Pin | One physical pad, with a number and a name | pin 2, "PA2" |
| Group | A set of pins that must be configured together for one peripheral | uart0_grp = pins 2 and 3 |
| Function | A named role that a group can take | uart0, i2c0 |
A consumer, such as a serial port driver, never mentions registers. Its device tree node says which function and group it wants, and the pinctrl core calls the controller driver to switch the pins to it. The driver supplies the tables and one operation, set_mux, that writes the hardware.
The example hardware has eight pins and one 32-bit mux register with two bits per pin. A value of 0 selects GPIO, 1 selects the first alternate function and 2 selects the second:
#define REG_MUX 0x00
#define MUX_GPIO 0
#define MUX_ALT1 1
#define MUX_ALT2 2The tables
The pins have numbers and names:
#include <linux/io.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/pinctrl/pinctrl.h>
#include <linux/pinctrl/pinmux.h>
#include <linux/pinctrl/pinconf-generic.h>
#include "pinctrl-utils.h"
#define NPINS 8
static const struct pinctrl_pin_desc sample_pins[] = {
PINCTRL_PIN(0, "PA0"), PINCTRL_PIN(1, "PA1"),
PINCTRL_PIN(2, "PA2"), PINCTRL_PIN(3, "PA3"),
PINCTRL_PIN(4, "PA4"), PINCTRL_PIN(5, "PA5"),
PINCTRL_PIN(6, "PA6"), PINCTRL_PIN(7, "PA7"),
};
static const unsigned int uart0_pins[] = { 2, 3 };
static const unsigned int i2c0_pins[] = { 4, 5 };
struct sample_group {
const char *name;
const unsigned int *pins;
unsigned int npins;
unsigned int mux; /* value written for every pin */
};
static const struct sample_group sample_groups[] = {
{ "uart0_grp", uart0_pins, ARRAY_SIZE(uart0_pins), MUX_ALT1 },
{ "i2c0_grp", i2c0_pins, ARRAY_SIZE(i2c0_pins), MUX_ALT2 },
};
static const char * const uart0_groups[] = { "uart0_grp" };
static const char * const i2c0_groups[] = { "i2c0_grp" };
struct sample_function {
const char *name;
const char * const *groups;
unsigned int ngroups;
};
static const struct sample_function sample_functions[] = {
{ "uart0", uart0_groups, ARRAY_SIZE(uart0_groups) },
{ "i2c0", i2c0_groups, ARRAY_SIZE(i2c0_groups) },
};Keeping the mux value in the group table, as here, makes set_mux generic: it never needs a switch on the group. Real controllers store more, such as the register offset and bit position of each pin, and larger drivers generate the tables from a macro or a data file.
The two operation sets
The first set, pinctrl_ops, lets the core list the groups and read their pins:
struct sample_pinctrl {
struct device *dev;
void __iomem *regs;
struct pinctrl_dev *pctl;
};
static int sample_get_groups_count(struct pinctrl_dev *pctldev)
{
return ARRAY_SIZE(sample_groups);
}
static const char *sample_get_group_name(struct pinctrl_dev *pctldev,
unsigned int selector)
{
return sample_groups[selector].name;
}
static int sample_get_group_pins(struct pinctrl_dev *pctldev,
unsigned int selector,
const unsigned int **pins,
unsigned int *npins)
{
*pins = sample_groups[selector].pins;
*npins = sample_groups[selector].npins;
return 0;
}
static const struct pinctrl_ops sample_pctl_ops = {
.get_groups_count = sample_get_groups_count,
.get_group_name = sample_get_group_name,
.get_group_pins = sample_get_group_pins,
.dt_node_to_map = pinconf_generic_dt_node_to_map_group,
.dt_free_map = pinctrl_utils_free_map,
};The selectors are indexes into the arrays, not hardware values. The core asks "what is group 1's name" and "which pins are in group 1", and the driver answers from its table. The last two entries connect the device tree: pinconf_generic_dt_node_to_map_group() is a standard helper that reads the function and groups properties of a pin configuration node and builds the mapping the core needs, so a driver with the standard binding needs no parsing code. pinctrl_utils_free_map() frees what it built. It is declared in drivers/pinctrl/pinctrl-utils.h, a header private to the pinctrl directory, so this form fits a driver that lives in the kernel tree under drivers/pinctrl/ and enables CONFIG_GENERIC_PINCONF. An out-of-tree driver writes its own dt_node_to_map and dt_free_map.
The second set, pinmux_ops, lists functions and does the muxing:
static int sample_get_functions_count(struct pinctrl_dev *pctldev)
{
return ARRAY_SIZE(sample_functions);
}
static const char *sample_get_function_name(struct pinctrl_dev *pctldev,
unsigned int selector)
{
return sample_functions[selector].name;
}
static int sample_get_function_groups(struct pinctrl_dev *pctldev,
unsigned int selector,
const char * const **groups,
unsigned int * const ngroups)
{
*groups = sample_functions[selector].groups;
*ngroups = sample_functions[selector].ngroups;
return 0;
}
static int sample_set_mux(struct pinctrl_dev *pctldev,
unsigned int func_selector,
unsigned int group_selector)
{
struct sample_pinctrl *p = pinctrl_dev_get_drvdata(pctldev);
const struct sample_group *g = &sample_groups[group_selector];
u32 mux = ioread32(p->regs + REG_MUX);
unsigned int i;
for (i = 0; i < g->npins; i++) {
unsigned int shift = g->pins[i] * 2;
mux &= ~(0x3 << shift);
mux |= g->mux << shift;
}
iowrite32(mux, p->regs + REG_MUX);
return 0;
}
static const struct pinmux_ops sample_pmx_ops = {
.get_functions_count = sample_get_functions_count,
.get_function_name = sample_get_function_name,
.get_function_groups = sample_get_function_groups,
.set_mux = sample_set_mux,
};set_mux is where the hardware changes. It receives the function and group by index and must make every pin of the group take the function. Here it clears the two bits for each pin and writes the group's alternate-function value. The read-modify-write of one register is not locked, because the pinctrl core serializes calls to a controller with its own mutex, which is why pin controller callbacks may sleep. A controller whose registers are shared with another driver, such as a GPIO bank in the same register block, needs its own lock for those accesses.
Registration
static const struct pinctrl_desc sample_desc = {
.name = "sample-pinctrl",
.pins = sample_pins,
.npins = NPINS,
.pctlops = &sample_pctl_ops,
.pmxops = &sample_pmx_ops,
.owner = THIS_MODULE,
};
static int sample_pinctrl_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct sample_pinctrl *p;
p = devm_kzalloc(dev, sizeof(*p), GFP_KERNEL);
if (!p)
return -ENOMEM;
p->dev = dev;
p->regs = devm_platform_ioremap_resource(pdev, 0);
if (IS_ERR(p->regs))
return PTR_ERR(p->regs);
p->pctl = devm_pinctrl_register(dev, &sample_desc, p);
return PTR_ERR_OR_ZERO(p->pctl);
}
static const struct of_device_id sample_pinctrl_of_match[] = {
{ .compatible = "example,sample-pinctrl" },
{ }
};
MODULE_DEVICE_TABLE(of, sample_pinctrl_of_match);
static struct platform_driver sample_pinctrl_driver = {
.probe = sample_pinctrl_probe,
.driver = {
.name = "sample-pinctrl",
.of_match_table = sample_pinctrl_of_match,
},
};
module_platform_driver(sample_pinctrl_driver);
MODULE_DESCRIPTION("Sample pin controller");
MODULE_LICENSE("GPL");devm_pinctrl_register() returns the pinctrl_dev handle, or an error pointer, and the driver data pointer is what pinctrl_dev_get_drvdata() returns in callbacks.
The consumer's side
A peripheral's node chooses its pin state by name. The controller's node holds the configuration nodes:
pinctrl@10002000 {
compatible = "example,sample-pinctrl";
reg = <0x10002000 0x4>;
uart0_default: uart0-default {
function = "uart0";
groups = "uart0_grp";
};
};
serial@10003000 {
compatible = "example,sample-uart";
reg = <0x10003000 0x100>;
pinctrl-names = "default";
pinctrl-0 = <&uart0_default>;
};When the serial driver's device is about to probe, the driver core applies the default state before calling probe, so the pins are already routed to the UART when the driver starts. That ordering is automatic and requires nothing in the UART driver. A state named sleep can be applied during suspend to put the pins in a low-leakage configuration, which is why consumers may have several named states.
Seeing the result
The subsystem exports its state in debugfs, which is the first place to look when a peripheral does not work:
sudo cat /sys/kernel/debug/pinctrl/sample-pinctrl/pinmux-pins | head -n 6Pinmux settings per pin
Format: pin (name): mux_owner gpio_owner hog?
pin 0 (PA0): UNCLAIMED UNCLAIMED
pin 1 (PA1): UNCLAIMED UNCLAIMED
pin 2 (PA2): 10003000.serial (GPIO UNCLAIMED) function uart0 group uart0_grp
pin 3 (PA3): 10003000.serial (GPIO UNCLAIMED) function uart0 group uart0_grpPins 2 and 3 are owned by the serial device through the uart0 function. A pin that shows as claimed by one device cannot be taken by another, and the core refuses the second claim, which prevents two peripherals from silently fighting for a pad. pinctrl-handles lists each device's states, and pingroups and pinmux-functions print the tables you registered, so a typo in a group name is visible at once.
What this driver leaves out
Electrical configuration is the other half of a real pin controller: pull-up and pull-down resistors, drive strength, slew rate and input enable. They are pinconf_ops, a third operation set that applies parameters such as bias-pull-up or drive-strength, taken from the same device tree nodes. Hardware that muxes GPIO and peripherals also needs gpio_request_enable so the GPIO controller can claim a pin and the pin controller switches it to GPIO mode. Both are additions to this structure, not changes to it.
Debugging a pin that does not move
When a peripheral stays silent after the pin state was supposed to be applied, check four things in order. Confirm the consumer's node names the state and the label exists: a misspelled pinctrl-0 label fails the build of the device tree and a misspelled state name silently applies nothing. Confirm the pinmux-pins file shows the pin owned by the consumer, since a pin shown as UNCLAIMED was never muxed. Confirm the group's mux value is the one the reference manual lists for that function, because a wrong alternate-function number muxes the pad to a different peripheral without any error. And confirm no other node claims the same pad, since the core reports the conflict at probe and the second consumer then fails with -EBUSY. Each check is a single command, and together they cover almost every failure the first bring-up of a controller produces.


