Use GPIO Descriptors in Drivers With gpiod_get
Request, configure and drive GPIO lines from a Linux 6.12 platform driver with devm_gpiod_get and the descriptor API, including active-low handling.

A driver for a sensor or a display often needs a few GPIO lines: a reset pin, an enable pin, a data-ready interrupt line. The old way was to ask for a global GPIO number with gpio_request(), and it is deprecated for the same reasons user space left sysfs: numbers are unstable and the polarity belongs to the board, not to the driver. The current way is the descriptor interface in <linux/gpio/consumer.h>, where the driver asks for a line by name and the device tree or ACPI table says which physical pin it is and whether it is active-low. This post uses Linux 6.12 to write a platform driver that requests a reset line and an enable line, and explains the logical-versus-physical distinction that removes a whole class of board-specific bugs. It follows the intro to drivers.
The reference is the kernel's GPIO consumer guide.
Descriptors, not numbers
A struct gpio_desc * is an opaque handle to one line. The driver never sees the global number or the controller. It asks for a line by function name, and the firmware description wires the name to a pin. In a device tree, a property named <name>-gpios gives the line for the function <name>:
sensor {
compatible = "example,sensor";
reset-gpios = <&gpio1 17 GPIO_ACTIVE_LOW>;
enable-gpios = <&gpio1 18 GPIO_ACTIVE_HIGH>;
};The first cell after the controller reference is the line offset in that controller, and the second is flags. GPIO_ACTIVE_LOW says that the signal is asserted by driving the pin low, which is how most reset lines are wired. The driver's name for the function is the part before -gpios, so "reset" and "enable".
Request the lines
#include <linux/gpio/consumer.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/delay.h>
struct sample {
struct gpio_desc *reset;
struct gpio_desc *enable;
};
static int sample_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct sample *s;
s = devm_kzalloc(dev, sizeof(*s), GFP_KERNEL);
if (!s)
return -ENOMEM;
s->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
if (IS_ERR(s->reset))
return dev_err_probe(dev, PTR_ERR(s->reset),
"cannot get reset line\n");
s->enable = devm_gpiod_get_optional(dev, "enable", GPIOD_OUT_LOW);
if (IS_ERR(s->enable))
return dev_err_probe(dev, PTR_ERR(s->enable),
"cannot get enable line\n");
gpiod_set_value_cansleep(s->enable, 1);
usleep_range(1000, 2000);
gpiod_set_value_cansleep(s->reset, 0);
msleep(10);
dev_info(dev, "reset released, enable %s\n",
s->enable ? "driven" : "not wired");
platform_set_drvdata(pdev, s);
return 0;
}
static const struct of_device_id sample_of_match[] = {
{ .compatible = "example,sensor" },
{ }
};
MODULE_DEVICE_TABLE(of, sample_of_match);
static struct platform_driver sample_driver = {
.probe = sample_probe,
.driver = {
.name = "sample-gpio",
.of_match_table = sample_of_match,
},
};
module_platform_driver(sample_driver);
MODULE_DESCRIPTION("GPIO descriptor example");
MODULE_LICENSE("GPL");With both lines present in the device tree, the log shows:
sample-gpio soc:sensor: reset released, enable drivenThe prefix is the device name the platform core gave the node, which depends on where it sits in the tree. Several details in this probe are worth explaining.
devm_gpiod_get() requests the line and arranges for it to be released when the driver detaches, which is why remove needs no GPIO code. The third argument sets the initial state in one step: GPIOD_IN makes it an input, GPIOD_OUT_LOW an output at the logical low level, GPIOD_OUT_HIGH an output at the logical high level, and GPIOD_ASIS leaves the pin as it is. Choosing the initial level at request time avoids a glitch between requesting and setting, which matters for a reset line.
devm_gpiod_get_optional() returns NULL when the property does not exist, and an error pointer for real failures. The gpiod functions accept a NULL descriptor and do nothing, so the later gpiod_set_value_cansleep(s->enable, 1) is safe even when the board has no enable line. That makes optional lines free of if checks.
dev_err_probe() prints the error message and handles the -EPROBE_DEFER case quietly. A GPIO controller that has not probed yet makes the request return -EPROBE_DEFER, which tells the core to try again later and is not a failure. dev_err_probe() records the reason for the deferral instead of printing a noisy error.
Logical values and polarity
This is the main idea of the interface. The driver speaks in logical levels: gpiod_set_value(desc, 1) means "assert this signal", and 0 means "deassert". The GPIO_ACTIVE_LOW flag in the device tree makes the library invert, so that asserting a reset drives the pin low. The driver is the same for a board that wires the reset active-low and one that wires it active-high. Only the device tree differs.
Walk through the reset in the example. The request used GPIOD_OUT_HIGH, which is a logical high, so the reset is asserted from the start and the pin is physically low on an active-low board. After enabling power, the code writes 0, deasserting the reset, which drives the pin physically high. The driver never wrote "pin low" or "pin high", only "asserted" and "released".
Do not mix the two. Code that reads a physical level with gpiod_get_raw_value() and compares it against a logical constant has hardcoded one board's wiring. The raw functions exist for the rare driver that must know the physical level, and almost no driver does.
Sleeping and atomic contexts
A GPIO controller can be memory-mapped, which is fast and never sleeps, or sit on a slow bus such as I2C or SPI, which does. The descriptor API has two sets of setters to match:
| Function | Context |
|---|---|
gpiod_set_value() | Any context, but only for controllers that never sleep |
gpiod_set_value_cansleep() | Process context only, works for every controller |
Calling the first on a line behind an I2C expander produces a warning, since it cannot sleep. A driver that runs in probe or a workqueue should use the cansleep variants always, which is correct for any wiring. Use the plain ones only inside an interrupt handler or under a spinlock, and know that then the board must have a controller that does not sleep, which gpiod_cansleep() can verify.
Reading, arrays and interrupts
An input line is read with gpiod_get_value_cansleep(), which returns the logical level. A line set up as GPIOD_IN is converted to an interrupt with gpiod_to_irq():
s->ready = devm_gpiod_get(dev, "ready", GPIOD_IN);
if (IS_ERR(s->ready))
return PTR_ERR(s->ready);
irq = gpiod_to_irq(s->ready);
if (irq < 0)
return irq;
ret = devm_request_threaded_irq(dev, irq, NULL, sample_irq_thread,
IRQF_TRIGGER_RISING | IRQF_ONESHOT,
"sample-ready", s);Several lines with the same function name, such as a data bus, are requested together as an array. The property is then a list in the device tree, and devm_gpiod_get_array(dev, "data", GPIOD_OUT_LOW) returns a struct gpio_descs with a desc[] array and a count. gpiod_set_array_value_cansleep() sets them in one call, and the kernel uses a single hardware write when the lines share a controller word.
When the line is missing
If the device tree has no reset-gpios, devm_gpiod_get() returns -ENOENT and the probe fails with the message from dev_err_probe(). That is the right behavior for a required line. Use the _optional variant for a line that some boards lack. For ACPI systems, the line is described with a _DSD property or a GpioIo resource and the same call works unchanged, which is another reason to avoid numbers.


