Disable SPI CS pin to be used as GPIO

Hello Toradex Community,

in my current project I’ve two ICs connected to the ecspi1. The CS line of the first IC is connected to the ecspi1 CS and the CS line of the second IC to a common GPIO. To be able to communicate with both ICs I would like to disable the SPI CS and use it as common GPIO as well.

To solve this issue I’ve read several topics in this forum and at NXP but non of them could get me to the final solution. At my current state of configuration I am using the following dts file

// Definitions for the FXAS21002C Gyroscope
/dts-v1/;
/plugin/;

#include "imx8mp-pinfunc.h"

/ {
	compatible = "toradex,verdin-imx8mp";
};

&{/} {
	leds {
		compatible = "gpio-leds";

		pinctrl-names = "default";
		pinctrl-0 = <&pinctrl_spi_cs_ISO1H816G>;

		/* SODIMM 202 - ISO1H816G_CS_0 */
		ISO1H816G_CS_0 {
			gpios = <&gpio5 9 0>;
			default-state = "on";
		};
		/* SODIMM 54 - ISO1H816G_CS_1 */
		ISO1H816G_CS_1 {
			gpios = <&gpio1 8 0>;
			default-state = "on";
		};
	};
};

&ecspi1 {
    #address-cells = <1>;
    #size-cells = <0>;
    status = "okay";
    fsl,espi-num-chipselects = <0>;
	pinctrl-names = "default";
	pinctrl-0 = <&pinctrl_ecspi1>;
};

&iomuxc {
	pinctrl_spi_cs_ISO1H816G: spi_cs_ISO1H816G_grp {
		fsl,pins = <
			MX8MP_IOMUXC_ECSPI1_SS0__GPIO5_IO09 0x106	/* SODIMM 202 */
			MX8MP_IOMUXC_GPIO1_IO08__GPIO1_IO08 0x106	/* SODIMM 218 */
		>;
	};
};

After flashing this to my Verdin (V1.1A) modul

dmesg | grep spi

returns the following lines

[ 0.921734] imx8mp-pinctrl 30330000.pinctrl: pin MX8MP_IOMUXC_ECSPI1_SS0 already requested by leds; cannot claim for 30820000.spi
[ 0.921747] imx8mp-pinctrl 30330000.pinctrl: pin-123 (30820000.spi) status -22
[ 0.921756] imx8mp-pinctrl 30330000.pinctrl: could not request pin 123 (MX8MP_IOMUXC_ECSPI1_SS0) from group ecspi1grp on device 30330000.pinctrl
[ 0.921766] spi_imx 30820000.spi: Error applying setting, reverse things back

and in the /dev/ folder I do not find any spi

ls /dev/spi*

ls: cannot access ‘/dev/spi*’: No such file or directory

If I remove the configuration of GPIO5 IO09 from iomuxc I receive no error messages from spi and find the expected interface within the /dev/ folder. Therefore I assume the SPI to be enabled and functional in general.

Another attempt I’ve made was replacing the entire configuration of pinctrl_ecspi1 in iomuxc without a CS Pin configured. But with this attempt I’ve received the following message.

spi_imx 30820000.spi: error -EBUSY: bitbang start failed

I’m looking forward to read your ideas what I could try next.
Best regards

KDehren

Potentially solved this issue

Hello,

after some more try and error I somewhere found a hint that redefining the IO mux of the SPI interface should work event if no CS Pin is defined. I managed to update the dts so that I don’t receive any errors on dmesg and have one spi interface available in /dev/ and 2 “LED” GPIOs in /sys/class/leds

changes to dts file:
updated cs-gpios to be an empty list
removed fsl,espi-num-chipselects line

&ecspi1 {
    #address-cells = <1>;
    #size-cells = <0>;
    status = "okay";
    pinctrl-names = "default";
    pinctrl-0 = <&pinctrl_ecspi1_cover>;
    cs-gpios = <>;

    spi-max-frequency = <20000000>;
};

defined new iomux group for spi without CS pin

pinctrl_ecspi1_cover: ecspi1grp_cover {
	fsl,pins = <
		MX8MP_IOMUXC_ECSPI1_SCLK__ECSPI1_SCLK		0x4	/* SODIMM 196 */
		MX8MP_IOMUXC_ECSPI1_MOSI__ECSPI1_MOSI		0x4	/* SODIMM 200 */
		MX8MP_IOMUXC_ECSPI1_MISO__ECSPI1_MISO		0x1c4	/* SODIMM 198 */
	>;
};

Hi @KDehren !

Welcome to Toradex Community! :tada:

Feel free to browse around :smiley:

From the device tree sources you shared, I understand you won’t use any drivers to communicate with your SPI devices. In other words, you are going to develop some application to communicate with them. Is this right?

If yes, you would still need to use the spidev driver in your device tree to have access to /dev/spidev* on userspace, which is usually needed by SPI libraries.

Please refer to our device tree overlay repository: verdin-imx8mp_spidev_overlay.dts « overlays - device-tree-overlays.git - Sources for Device Tree Overlays

To learn more about Device Trees in general, please take your time to read our nice documentation about it. I am sure it will help you to better understand how to deal with it :slight_smile:

https://developer.toradex.com/software/linux-resources/device-tree/

In there you will find articles that explain to you the issues you faced.

Be aware that you will need to configure the MISO, MOSI, SCLK plus the GPIOs (to be used as the chip selects - CS) in the same pinctrl group in your device tree. Example based on what you shared (I didn’t check your pad control values):

&pinctrl_ecspi1 {
	fsl,pins =
		<MX8MP_IOMUXC_ECSPI1_MISO__ECSPI1_MISO		0x1c4>,	/* SODIMM 198 */
		<MX8MP_IOMUXC_ECSPI1_MOSI__ECSPI1_MOSI		0x4>,	/* SODIMM 200 */
		<MX8MP_IOMUXC_ECSPI1_SCLK__ECSPI1_SCLK		0x4>,	/* SODIMM 196 */
		<MX8MP_IOMUXC_ECSPI1_SS0__GPIO5_IO09		0x1c4>,	/* SODIMM 202 */
		<MX8MP_IOMUXC_GPIO1_IO08__GPIO1_IO08        0x106>; /* SODIMM 218 */
};

(By the way, be careful with syntax :wink: )

As explained in our documentation (shared above) you will need to be sure that the pins MX8MP_IOMUXC_ECSPI1_SS0 and MX8MP_IOMUXC_GPIO1_IO08 are not in use by any other node.

Then, as an example, for your devices (based on verdin-imx8mp_spidev_overlay.dts « overlays - device-tree-overlays.git - Sources for Device Tree Overlays), you could have:

&ecspi1 {
	#address-cells = <1>;
	#size-cells = <0>;
	status = "okay";

	spidev@0 {
		/* Use compatible "rohm,dh2228fv" to bind spidev driver */
		compatible = "rohm,dh2228fv";
		reg = <0>;
		spi-max-frequency = <10000000>;
	};

	spidev@1 {
		/* Use compatible "rohm,dh2228fv" to bind spidev driver */
		compatible = "rohm,dh2228fv";
		reg = <1>;
		spi-max-frequency = <10000000>;
	};
};

Let us know if this helps you :slight_smile:

Best regards,

Hi henrique.tx

thank you for your assintance.

That’s correct. Unfortunately there is no driver available for the IC we are using.

I’ve just tested the changed code sections you’ve mentioned and received the following output on

ll /dev/*spi*

crw-rw-r-- 1 root spidev 153, 1 Jul 27  2023 /dev/spidev1.0
crw-rw-r-- 1 root spidev 153, 0 Jul 27  2023 /dev/spidev1.1
lrwxrwxrwx 1 root root        9 Jul 27  2023 /dev/verdin-spi-cs0 -> spidev1.0
lrwxrwxrwx 1 root root        9 Jul 27  2023 /dev/verdin-spi-cs1 -> spidev1.1


ll /sys/bus/spi/devices/
total 0
lrwxrwxrwx 1 root root 0 Jan 31 07:22 spi1.0 -> ../../../devices/platform/soc@0/30800000.bus/30820000.spi/spi_master/spi1/spi1.0
lrwxrwxrwx 1 root root 0 Jan 31 07:22 spi1.1 -> ../../../devices/platform/soc@0/30800000.bus/30820000.spi/spi_master/spi1/spi1.1

It seams to work perfectly.
Just for my understanding. By adding GPIO1 IO8 to pinctrl_ecspi1 I don’t have to define cs-gpios in the ecspi1 section. Is that correct?

Best regards,
KDehren

Hi @KDehren !

I see :slight_smile:
So you could contribute with Linux Kernel with a driver for it (if you have time, of course :grin:)

Nice!

I missed this part. Sorry for this. Please refer to spi-controller.yaml « spi « bindings « devicetree « Documentation - linux-toradex.git - Linux kernel for Apalis, Colibri and Verdin modules to find out how to correctly set the cs-gpio property.

By the way, as listed in our documentation about Device Trees (shared previously), checking the Documentation - linux-toradex.git - Linux kernel for Apalis, Colibri and Verdin modules helps you to better understand how to use “stuff” :slight_smile:

Best regards,

Hi @henrique.tx

Thank you verry mutch for your help.I’ve added the cs-gpio definition to the configuration and everything works fine.

This is my final and working dts file:

/dts-v1/;
/plugin/;

#include "imx8mp-pinfunc.h"

/ {
	compatible = "toradex,verdin-imx8mp";
};

&ecspi1 {
    #address-cells = <1>;
    #size-cells = <0>;
    status = "okay";
	pinctrl-names = "default";
	pinctrl-0 = <&pinctrl_ecspi1>;
	cs-gpios = 
		<&gpio5 9 1>, 
		<&gpio1 8 1>;
    
	spidev@0 {
		/* Use compatible "rohm,dh2228fv" to bind spidev driver */
		compatible = "rohm,dh2228fv";
		reg = <0>;
		spi-max-frequency = <10000000>;
	};

	spidev@1 {
		/* Use compatible "rohm,dh2228fv" to bind spidev driver */
		compatible = "rohm,dh2228fv";
		reg = <1>;
		spi-max-frequency = <10000000>;
	};
};

&pinctrl_ecspi1 {
    fsl,pins = 
        <MX8MP_IOMUXC_ECSPI1_SCLK__ECSPI1_SCLK		0x4>,	/* SODIMM 196 */
        <MX8MP_IOMUXC_ECSPI1_MOSI__ECSPI1_MOSI		0x4>,	/* SODIMM 200 */
        <MX8MP_IOMUXC_ECSPI1_MISO__ECSPI1_MISO		0x1c4>,	/* SODIMM 198 */
        <MX8MP_IOMUXC_ECSPI1_SS0__GPIO5_IO09		0x1c4>,	/* SODIMM 202 */
        <MX8MP_IOMUXC_GPIO1_IO08__GPIO1_IO08        0x106>; /* SODIMM 218 */
};

Hi @KDehren !

Great news! Congratulations! :smiley:

And thanks a lot for sharing the related excerpt from your working device tree! This is what make this Community so great! :smiley:

Have a nice day!

Best regards,