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

BCM2835 gpio device tree raspberry pi


I was looking to modify my GPIO driver for raspberry pi using device tree support. First there were 2 files:

  1. I read the device tree file in /arc/arm/boot/dts/bcm2835.dts

and for gpio following section was present:

gpio: gpio {

                   compatible = "brcm,bcm2835-gpio";
                   reg = <0x7e200000 0xb4>;
                   /*
                    * The GPIO IP block is designed for 3 banks of GPIOs.
                     * Each bank has a GPIO interrupt for itself.
                     * There is an overall "any bank" interrupt.
                     * In order, these are GIC interrupts 17, 18, 19, 20.
                     * Since the BCM2835 only has 2 banks, the 2nd bank
                     * interrupt output appears to be mirrored onto the
                     * 3rd bank's interrupt signal.
                     * So, a bank0 interrupt shows up on 17, 20, and
                     * a bank1 interrupt shows up on 18, 19, 20!
                     */
                    interrupts = <2 17>, <2 18>, <2 19>, <2 20>;
                    gpio-controller;
                    #gpio-cells = <2>;
                    interrupt-controller;
                    #interrupt-cells = <2>;
            };

From the references on the internet The reg = 0x7e200000 is understood but What is 0xb4.

  1. I read the device tree file in /arch/arm/boot/dts/bcm2835-rpi-b.dts

and for gpio following section was present:

/ {
    compatible = "raspberrypi,model-b", "brcm,bcm2835";
    model = "Raspberry Pi Model B";

    memory {
            reg = <0 0x10000000>;
    };

    leds {
            compatible = "gpio-leds";

            act {
                    label = "ACT";
                    gpios = <&gpio 16 1>;
                    default-state = "keep";
                    linux,default-trigger = "heartbeat";
            };
    };
};

&gpio {
    pinctrl-names = "default";
    pinctrl-0 = <&alt0 &alt3>;

    alt0: alt0 {
            brcm,pins = <0 1 2 3 4 5 6 7 8 9 10 11 14 15 40 45>;
            brcm,function = <4>; /* alt0 */
    };

    alt3: alt3 {
            brcm,pins = <48 49 50 51 52 53>;
            brcm,function = <7>; /* alt3 */
    };
};

So, Which one of the dts files should I use, and how to read and interpret those key value pairs, for eg: what is pinctrl. and how does this approach affect on my code.

I know I am asking a lot of stuff here, but this is new and looks interesting and I want to modify my driver using this approach. Please help.

PS: I have made a driver using the standard udev support. So dynamic device node creation is managed. I am not using platform model.


Solution

  • 1. From the references on the internet The reg = 0x7e200000 is understood but What is 0xb4. reg = <0x7e200000 0xb4>

    Here 0xb4 refers to the length of the register. "reg : Address and length of the register set for the device"

    You can probably checkout this pdf for better info http://events.linuxfoundation.org/sites/events/files/slides/petazzoni-device-tree-dummies.pdf

    2. So, Which one of the dts files should I use, and how to read and interpret those key value pairs,

    I will split the question into two parts. For reading key value pairs:

    Every Device tree entry would have an associated binding file that describes how you read the key value pairs. For example http://lxr.free-electrons.com/source/Documentation/devicetree/bindings/arm/bcm/brcm,bcm11351-cpu-method.txt . You can get the corresponding details .

    Regarding which dts files should I use:

    Now if u have noticed bcm2835.dtsi. is not a dts file but a dtsi file. http://lxr.free-electrons.com/source/arch/arm/boot/dts/bcm2835.dtsi

    dtsi files can be included into other dts or dtsi files just like we include other libraries like conio.h. or stdio.h in our C code.

    Here bcm2835-rpi-b.dts is a dts file and if u notice the file here http://lxr.free-electrons.com/source/arch/arm/boot/dts/bcm2835-rpi-b.dts

    it includes the following:

    /include/ "bcm2835.dtsi"

    This means that all the dt entries in bcm2835.dtsi is imported into bcm2835-rpi-b.dts. You can choose to leave the nodes as is or modify the properties in rpi-b-dts, but the final entry made in dts file will be the one reflected in the dtb.

    3. for eg: what is pinctrl. and how does this approach affect on my code.

    Pinctrl is framework provided in the kernel for accessing PIN's here gpio's. You can probably checkout the documentation used https://www.kernel.org/doc/Documentation/pinctrl.txt