Named GPIOs in DeviceTree
Asked Answered
S

2

14

I am trying to create a device tree for an embedded system, and would like to expose a few GPIOs to userspace. These are not and should not be used by kernel drivers. For instance, there is a USB device soldered to the board that is controlled by a user-space driver. It has a GPIO reset line, which the userspace library needs to access.

I would like these to be exposed by name in sysfs somewhere so that userspace can access /sys/class/gpio/usbreset instead of needing to know the magic gpio number and needing to specifically "export" it. I have tried setting it up as a GPIO hog, which initializes the GPIO, but its name does not appear in sysfs, and the gpio cannot be exported when it is hogged. I know that I can pick another kernel driver type such as LED, but it is not an LED, and this does not seem very clean to me.

What is the right way to export a named GPIO in sysfs?

Sawyers answered 22/4, 2016 at 20:53 Comment(1)
Try using a device tree overlay, as suggested here.Unreserved
C
2

You can use the "gpio-leds" type in the devtree. Make sure you have CONFIG_LEDS_GPIO in your kernel. Set up your devtree as described in Documentation/devicetree/bindings/leds/leds-gpio.yaml. I know, it says "LED", but the driver just wiggles the GPIO, and doesn't care what's hooked up to it.

Example devtree entry (copied from the docs):

run-control {
        compatible = "gpio-leds";
        red {
                gpios = <&mpc8572 6 GPIO_ACTIVE_HIGH>;
                default-state = "off";
        };
        green {
                gpios = <&mpc8572 7 GPIO_ACTIVE_HIGH>;
                default-state = "on";
        };
};

Those entries will be accessible by name in sysfs, and you can manipulate them from userspace there.

Carencarena answered 11/3, 2019 at 14:10 Comment(1)
"I know, it says "LED", but the driver just wiggles the GPIO, and doesn't care what's hooked up to it." That's not true. This may work well enough for something like a test-point. But LEDs are always outputs and wont trigger interrupts. LED's have properties ike brightness, and you can setup triggers like heartbeat or nand-disk which I may not want to make available for some generic GPIO. Also, I may want to give a meaningful name to a GPIO that is an input, like a push button or an interrupt pin telling me about an event on another device.Unreserved
S
0

I propose writing a simple kernel module to ask for the GPIO and then exporting a link, the link can be named and hence suitable for your request.

https://www.kernel.org/doc/Documentation/gpio/sysfs.txt

from the link above:

After the GPIO has been exported, gpiod_export_link() allows creating symlinks from elsewhere in sysfs to the GPIO sysfs node. Drivers can use this to provide the interface under their own device in sysfs with a descriptive name

Sesterce answered 21/1, 2018 at 13:18 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.