How to add simple GPIO defnition to a device tree (Yocto)?

292 Views Asked by At

I'm working with TI AM64XX and a base project that has limited definition of some GPIO of the board on the device tree. And I need more GPIOs than what it's defined in the DTS in order to use in my c/c++ code later and manipulate them as simple outputs. Based on the datasheet I've selected pins gpio1_26, gpio1_25 and I don't know how to modify the device tree exactly, in order to implement these three GPIOs

&gpio1 { 
     status = "okay";
     pinctrl-names = "default";
     pinctrl-0 = <&main_gpio1_pins_default>;
     gpio-controller;
};

main_gpio1_pins_default: main-gpio1-pins-default {
    pinctrl-single,pins = <
        AM64X_IOPAD(0x01C8, PIN_OUTPUT, 7) /* gpio1_26 */
        AM64X_IOPAD(0x01C4, PIN_OUTPUT, 7) /* gpio1_25 */
        AM64X_IOPAD(0x01C0, PIN_OUTPUT, 7) /*gpio1_24 */
        >;
};

Have you any idea?

I've tried some definition like this but it fails in the compilation part, here is the error screenshot

label or path gpio1 not found

2

There are 2 best solutions below

1
Th. Thielemann On

I don't know your CPU but in general it should be possible to extend the current block by additional GPIOs.

main_gpio1_pins_default: main-gpio1-pins-default {
pinctrl-single,pins = <
        AM64X_IOPAD(0x01C8, PIN_OUTPUT, 7) /* gpio1_26 */
        AM64X_IOPAD(0x01C4, PIN_OUTPUT, 7) /* gpio1_25 */
        AM64X_IOPAD(0x01C0, PIN_OUTPUT, 7) /*gpio1_24 */
        [PLACE MORE ENTRIES HERE]
        >;
        };

Look into the description of your CPU to find the PIN of the required GPIOs. There should be also a description to calculate the address. For other TI CPUs this can be done with an online tool. Or there could be a mapping file which contains all available GPIO pins. Grep the code for it.

3
Edher Carbajal On

As you can see in the compilation output, there's no definition for gpio1 node. & symbol in the dts makes reference to a previously defined node, if it wasn't defined by default or if you didn't explicitly defined it, that reference makes no sense.

Reviewing the dts 'k3-am642-sk.dts' that you are using I see that there's a node in the line 130 called main_pmx0, that's the main pinmux mode node and there you should add your config.

Other option (actually the recommended one) is to make a .dtsi file and include it your main, but for the sole purpose of simplicity you can modify the original one.

Inside the main pinmux node you can define your pins:

&main_pmx0 {
    custom_pins: custom_pins {
            pinctrl-single,pins = <
                AM64X_IOPAD(0x01C8, PIN_OUTPUT, 7) 
                AM64X_IOPAD(0x01C4, PIN_OUTPUT, 7) 
            >;
};

In the example I just added 2 pins, feel free to add as much as you want.

Doing this you should be able to work with the gpios with sys/class/gpio or with gpio char dev driver (or tools like libgpiod)

NOTE: Make sure to do the proper research about pinmux mode (refer to the corresponding reference processor manual), there are some pins that can't be used as gpios. I just copied the pins directions and pinmuxmode you write in your question, assuming you already got that info.

NOTE 2: If you're compiling using bitbake then you should recompile .wic image, otherwise nvm

NOTE 3 (Answering to your comment): If you don't see /sys/class/gpio that's because it's deprecated and the new standard is to use gpio char device. If you do ls /dev you will find some character dev drives called something like gpiochipX. If you want to add sys/class/gpio then you have to configure it in the kernel menuconfig:

Open kernel menuconfig and activate the following:

Symbol: EXPERT [=y]
Location:
General Setup --->
    [*] Configure standard kernel features (expert users)

Symbol: SYSFS [=y]
Location:
File systems --->
    Pseudo file systems --->
        -*- sysfs file system support

Symbol: GPIO_SYSFS [=y]
Location:
Device Drivers --->
    [*] GPIO support --->
        [*] sys/class/gpio