mangodan2003
Posts: 66
Joined: Thu Apr 23, 2015 1:10 pm

[SOLVED] Interfacing i2c device to CM4

Wed Mar 24, 2021 5:05 pm

Hi, I am trying to interface a sensor to the CM4 module on the pins labelled in the CM4 Datasheet as SCL0 and SDA0 (which the bcm2711 ref manual says correspond to GPIO44 and 45).

With a bit of tweaking I managed to get the kernel module for the sensor to build. I can load it manually so I am trying to make a dt overlay to do so automatically but am struggling with it. It compiles fine, but the module does not get loaded at boot. There seem to be no errors relating to it in dmesg. I have a dtoverlay entry for it in /boot/config.txt.

I have run depmod and modprobe finds the module ok.

This is my dts.

Code: Select all

/dts-v1/;
/plugin/;

/ {
    compatible = "brcm,bcm2711";

	fragment@0 {
		target = <&i2c0_pins>;
		pins: __overlay__ {
			brcm,pins = <44 45>;
			brcm,function = <5>; /* alt1 */
		};
	};

    fragment@1 {
        target = <&i2c0>;

        __overlay__ {
            #address-cells = <0x1>;
            #size-cells = <0x0>;

            stmvl53l1: stmvl53l1@29 {
                compatible = "st,stmvl53l1";
                reg = <0x29>;
                xsdn-gpios = <&gpio 22 0>;
                intr-gpios = <&gpio 27 0>;
            };
        };
    };
};
Can anybody help me with this?

mangodan2003
Posts: 66
Joined: Thu Apr 23, 2015 1:10 pm

Re: Interfacing i2c device to CM4

Wed Mar 24, 2021 5:15 pm

Well I did at one point have the module loading manually, now its gone back to complaining it can't get the i2c adaptor and I don't think I've changed anything.

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 3881
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: Interfacing i2c device to CM4

Wed Mar 24, 2021 5:31 pm

i2c0 is normally owned by the VPU/firmware so needs to be treated with caution. It can also be mapped to several different GPIO locations so even more care is needed.

The current Device Trees create an I2C multiplexer that arbitrates been multiple uses for the same underlying I2C interface. Using the mux node with the DT label "i2c0" maps it to GPIOs 0 & 1, while "i2c_csi_dsi" maps it to GPIOs 44 & 45.

Try the following:

Code: Select all

/dts-v1/;
/plugin/;

/ {
    compatible = "brcm,bcm2835";

    fragment@0 {
        target = <&i2c_csi_dsi>;

        __overlay__ {
            #address-cells = <0x1>;
            #size-cells = <0x0>;
            status = "okay";

            stmvl53l1: stmvl53l1@29 {
                compatible = "st,stmvl53l1";
                reg = <0x29>;
                xsdn-gpios = <&gpio 22 0>;
                intr-gpios = <&gpio 27 0>;
            };
        };
    };

    fragment@1 {
        target = <&i2c0if>;
        __overlay__ {
            status = "okay";
        };
    };

    fragment@2 {
        target = <&i2c0mux>;
        __overlay__ {
            status = "okay";
        };
    };
};
It hasn't been tested but it should be about right.

mangodan2003
Posts: 66
Joined: Thu Apr 23, 2015 1:10 pm

Re: Interfacing i2c device to CM4

Thu Mar 25, 2021 10:46 am

Thanks, that gives me :

Code: Select all

pi@rpi:~ $ find /proc/device-tree/ | grep stm
/proc/device-tree/soc/i2c0mux/i2c@1/stmvl53l1@29
/proc/device-tree/soc/i2c0mux/i2c@1/stmvl53l1@29/xsdn-gpios
/proc/device-tree/soc/i2c0mux/i2c@1/stmvl53l1@29/compatible
/proc/device-tree/soc/i2c0mux/i2c@1/stmvl53l1@29/intr-gpios
/proc/device-tree/soc/i2c0mux/i2c@1/stmvl53l1@29/phandle
/proc/device-tree/soc/i2c0mux/i2c@1/stmvl53l1@29/reg
/proc/device-tree/soc/i2c0mux/i2c@1/stmvl53l1@29/name
But the module is still not loaded. It has :

Code: Select all

	{ .compatible = "st,"STMVL53L1_DRV_NAME, },

Code: Select all

./vl53L1/stmvl53l1.h:#define STMVL53L1_DRV_NAME "stmvl53l1"
Which seems like it should match.

I can manually load the module but it doesn't find any devices, It also permits trying to "force the device" by allowing one to specify the i2c adapter and the gpios but this results in an error, it fails to get the i2c adapter via i2c_get_adapter.

One thing I'm unsure of is the placement of the module, I assume it should be ok as long as modprobe can find it but as it's an out of tree module I just dumped it in /lib/modules/5.10.14-v7l+/kernel/drivers/i2c/ and ran depmod -a

I don't know what else might cause it to not get loaded, any other overlays and modules I have worked with to date have just worked this far as far as I recall.
Last edited by mangodan2003 on Thu Mar 25, 2021 1:30 pm, edited 1 time in total.

mangodan2003
Posts: 66
Joined: Thu Apr 23, 2015 1:10 pm

Re: Interfacing i2c device to CM4

Thu Mar 25, 2021 12:35 pm

Something must have changed todo with the ofmatch entries since ST wrote the driver as it had i2c in place of for the MODULE_DEVICE_TABLE macro.

I've updated that and now get

Code: Select all

pi@rpi:~ $ cat /lib/modules/5.10.14-v7l+/modules.alias | grep stmvl
alias of:N*T*Cst,stmvl53l1C* stmvl53l1
alias of:N*T*Cst,stmvl53l1 stmvl53l1
But the module is still not loaded.

mangodan2003
Posts: 66
Joined: Thu Apr 23, 2015 1:10 pm

Re: Interfacing i2c device to CM4

Thu Mar 25, 2021 2:37 pm

I was using 6by9s branch 5.10.14 from experimenting bringup DSI displays as he had some fixes there. I have no idea why but having switched to the current stable 5.10.17 and rebuilt the module against that it loads automatically at boot :D , tho it doesn't seem to pick up the gpio pin specs correctly atm.

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 3881
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: Interfacing i2c device to CM4

Thu Mar 25, 2021 2:48 pm

So is it all working now?

mangodan2003
Posts: 66
Joined: Thu Apr 23, 2015 1:10 pm

Re: Interfacing i2c device to CM4

Wed May 05, 2021 2:23 pm

Yes I had this working in a proof of concept manor at least.

Thanks

mangodan2003
Posts: 66
Joined: Thu Apr 23, 2015 1:10 pm

Re: [SOLVED] Interfacing i2c device to CM4

Thu May 13, 2021 11:37 am

I just want to add that should anybody else come by here the actual dts I ended up using was a little different to the above due to the way the driver is using the DT.

Code: Select all

/dts-v1/;
/plugin/;

/ {
    compatible = "brcm,bcm2711";

    fragment@0 {
        target = <&i2c_csi_dsi>;

        __overlay__ {
            #address-cells = <0x1>;
            #size-cells = <0x0>;
            status = "okay";

            stmvl53l1: stmvl53l1@29 {
                compatible = "st,stmvl53l1";
                reg = <0x29>;
                xsdn-gpio = <22>;
                intr-gpio = <27>;
            };
        };
    };

    fragment@1 {
        target = <&i2c0if>;
        __overlay__ {
            status = "okay";
        };
    };

    fragment@2 {
        target = <&i2c0mux>;
        __overlay__ {
            status = "okay";
        };
    };
};

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 3881
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: [SOLVED] Interfacing i2c device to CM4

Thu May 13, 2021 11:49 am

That driver must be old - nothing should be using absolute GPIO numbers any more.

mangodan2003
Posts: 66
Joined: Thu Apr 23, 2015 1:10 pm

Re: [SOLVED] Interfacing i2c device to CM4

Fri May 14, 2021 10:09 am

It is, it is using the old gpio_ rather than gpiod_ api for a start. But it is allowing one to pass in the arguments as kernel module parameters to a manually loaded module and only if not set manually in that way falling back on parsing from the device tree in what looks like an insanely complex way compared to what can be easily achieved with gpiod_get. I couldn't just see a simple way to handle that behaviour as gpiod_get expects a device tree entry label to identify the gpio, whereas the gpio_request takes the simple integer which can be given to insmod. As such and as it works for now I chose to just leave it as it is for now.

Return to “Device Tree”