gpiod - use labels in devicetree
Asked Answered
C

1

4

I want to use libgpiod to control a few GPIOs via userspace on a custom board. I have an i.MX6UL processor, which has hundreds of pins, I'll use only 8 of them (as GPIOs).

I read about the libgpiod as it is replacing the old sysfs API, and I'm happy that you can specify labels for each GPIO. The GPIO block of the processor looks like the following code block and has already the gpio-controller property set. (Taken from Linux kernel v4.14)

gpio2: gpio@20a0000 {
    compatible = "fsl,imx6ul-gpio", "fsl,imx35-gpio";
    reg = <0x020a0000 0x4000>;
    interrupts = <GIC_SPI 68 IRQ_TYPE_LEVEL_HIGH>,
             <GIC_SPI 69 IRQ_TYPE_LEVEL_HIGH>;
    gpio-controller;
    #gpio-cells = <2>;
    interrupt-controller;
    #interrupt-cells = <2>;
    gpio-ranges = <&iomuxc 0 49 16>, <&iomuxc 16 111 6>;
};

I want to use a single pin of this controller, so I added the following block:

&gpio2 {
    resetl0 {
        //gpio-hog;
        output-high;
        line-name = "COBO0_ResetL";
        gpios = <15 1>;
    };
};

Without the gpio-hog property, the gpioinfo tool is unable to show me the labels, same if I omit the output-high/low. With the property, the label is correctly displayed, but the gpio is marked as used, so I cannot control from userspace. (Device or resource busy)

So in short: I need a way to set a label in device tree, which I'm able to read from userspace and to control the gpios. I already saw the gpio-line-names in the RPi devicetree, but I don't want to specify the whole bank as NC, when using only one. Is it possible with libgpiod? How?

Cardholder answered 6/2, 2020 at 17:52 Comment(4)
The label of the line is a property of the provider, and not the consumer. So, you have to define labels in GPIO controller device node.Loiretcher
Yeah, that's what I mentioned with the gpio-line-names, but does this mean that i have to define the labels for the whole gpio2 IO-Bank?Cardholder
Sadly, it appears the answer is, "yes"- which is pathetic. We went from one convoluted mess to another within the framework.Parenthesis
I've had to sort this recently on my Raspberry Pi. I was able to create a device tree overlay that added the gpio-line-names (see here for details). YMMV - I'm under the impression that device trees and their "compilation" vary widely between platofrms.Geosyncline
P
4

I tested on Odroid-N2 kernel v5.4.

meson-g12b-odroid-n2.dts

&gpio {
    gpio-line-names = 
        /* GPIOZ */
        "", "", "", "", "", "", "", "",
        "", "", "", "", "", "", "", "",
        /* GPIOH */
        "", "", "", "", "", "", "", "",
        "",
        /* BOOT */
        "", "", "", "", "", "", "", "",
        "", "", "", "", "", "", "", "",
        /* GPIOC */
        "", "", "", "", "", "", "", "",
        /* GPIOA */
        "44", "46", "45", "47",
        "26", "",   "",   "",
        "",   "",   "",   "42",
        "32", "7",  "27", "28",
        /* GPIOX */
        "16", "18", "22", "11",
        "13", "33", "35", "15",
        "19", "21", "24", "23",
        "8",  "0",  "29", "31",
        "12", "3",  "5",  "36";

    usb-hub {
        gpio-hog;
        gpios = <GPIOH_4 GPIO_ACTIVE_HIGH>;
        output-high;
        line-name = "usb-hub-reset";
    };
};

Output of gpioinfo

gpiochip0 - 85 lines:
...
    line  20:      unnamed "usb-hub-reset" input active-high [used]
...
    line  49:         "44"       unused   input  active-high 
    line  50:         "46"       unused   input  active-high 
    line  51:         "45"       unused   input  active-high 
    line  52:         "47"       unused   input  active-high 
    line  53:         "26"       unused   input  active-high 
...
    line  60:         "42"       unused   input  active-high 
    line  61:         "32"       unused   input  active-high 
    line  62:          "7"       unused   input  active-high 
    line  63:         "27"       unused   input  active-high 
    line  64:         "28"       unused   input  active-high 
    line  65:         "16"       unused   input  active-high 
    line  66:         "18"       unused   input  active-high 
    line  67:         "22"       unused   input  active-high 
    line  68:         "11"       unused   input  active-high 
    line  69:         "13"       unused   input  active-high 
    line  70:         "33"       unused   input  active-high 
    line  71:         "35"       unused   input  active-high 
    line  72:         "15"       unused   input  active-high 
    line  73:         "19"       unused   input  active-high 
    line  74:         "21"       unused   input  active-high 
    line  75:         "24"       unused   input  active-high 
    line  76:         "23"       unused   input  active-high 
    line  77:          "8"       unused   input  active-high 
    line  78:          "0"       unused   input  active-high 
    line  79:         "29"       unused   input  active-high 
    line  80:         "31"       unused   input  active-high 
    line  81:         "12"       unused   input  active-high 
    line  82:          "3"       unused   input  active-high 
    line  83:          "5"       unused   input  active-high 
    line  84:         "36"       unused   input  active-high 
...

gpioinfo displayed 'gpio-line-names' as a name and 'line-name' as a consumer.

Pradeep answered 13/3, 2020 at 1:42 Comment(1)
Thank you, I ended up in the same solution for my case, although I don't find it a good solution. I'd prefer a method which works for single pins too.Cardholder

© 2022 - 2025 — McMap. All rights reserved.