Search code examples
linux-kernelraspberry-pilinux-device-drivergpiodevice-tree

How to use the gpio pins on the raspberry pi with the device tree in a device driver?


I try to develop a driver to control custom hardware over the gpio pins on the raspberry pi.

I would love to use the new gpio_desc in combination with the device tree. But I cannot get this to work.

If i try to get a pin via red = gpio_to_desc( 17 ); everything works perfekt but

green = gpiod_get(dev, "carr", GPIOD_OUT_LOW);

seems to skip the device tree.

In dmesg:

[ 4326.023976] carrera_driver carrera.0: GPIO lookup for consumer carr
[ 4326.023986] carrera_driver carrera.0: using lookup tables for GPIO lookup
[ 4326.023999] carrera_driver carrera.0: No GPIO consumer carr found
[ 4326.024010] carrera_driver carrera.0: Failed to get carr GPIO: -2
[ 4326.024034] carrera_driver: probe of carrera.0 failed with error -2

device tree:

/dts-v1/;
/plugin/;

/{

    compatible = "brcm,bcm2835";

    fragment@0 {
    target = <&gpio>;
        __overlay__ {                   
            carrera_pins: carrera_pins {
                compatible = "kru,carrera";
                brcm,pins = <18>;
                brcm,function = <1>; // out
                brcm,pull = <0>;

            };
        };
    };

    fragment@1 {
        target = <&gpio>;
        __overlay__ {
            carrera: carrera {
                compatible = "kru,carrera";
                #gpio-cells = <2>;
                pinctrl-names = "default";
                pinctrl-0 = <&carrera_pins>;
                carr-gpios   = <&gpio 18 0>;
            };
        };
    };

};

I think this is the part where the device tree mapping with the driver happens

struct platform_device linuxmag = {
        .name = "carrera", /* driver identification */
        .id = 0,
        .dev = {
                .release = linuxmag_release,
        }
};

static struct of_device_id linmag_match[] = {
        {.compatible = "kru,carrera"},
        {}
};

static struct platform_driver mydriver = {
        .probe = linuxmag_probe_device,
        .remove = linuxmag_remove_device,
        .driver = {
                .name = "carrera_driver",
                .of_match_table = linmag_match,
        }
};

Dont be confused with the names. I used the template from this article. https://www.linux-magazin.de/ausgaben/2017/08/kern-technik/3/

I think I am missing something very significant.

Someone has an example on how to get at least a led on over the gpiod submodule with device tree? Everything i found on the Internet has an incomplete device tree or no code.


Solution

  • Here is a short answer that worked for me.

    /dts-v1/;
    /plugin/;
    
    / {
        fragment@0 {
            target = <&gpio>;
            __overlay__ {
                hsnrcarr {
                    compatible = "kru,carrera";
                    carr-gpios = <&gpio 17 0 >;
                };
            };
        };
    
    };
    

    I switched to GPIO 17.

    I hope I find the time to post an more detailed answer.