Ubnuntu 6.1.0 kernel. zero 3w spi3 gpio chip select

I have a radxa zero 3w. I am able to get spidev3.0 to be available using the overlay for it, rk3568-spi3-m1-cs0-spidev.dtbo. I am trying to use pin 12 or another available gpio as a second chipselect so I can use more than spi device. However I am unable to get it to work.

/dts-v1/;
/plugin/;

/ {
metadata {
title = “Enable spidev on SPI3-M1 with CS0 and CS1”;
compatible = “radxa,zero3”;
category = “misc”;
exclusive = “GPIO4_C6”, “GPIO3_A3”;
description = “Enable spidev on SPI3-M1 with hardware CS0 and software CS1.”;
};
};

&spi3 {
status = “okay”;
#address-cells = <1>;
#size-cells = <0>;
pinctrl-names = “default”, “high_speed”;
pinctrl-0 = <&spi3m1_cs0 &spi3m1_pins>;
pinctrl-1 = <&spi3m1_cs0 &spi3m1_pins_hs>;
max-freq = <50000000>;
num-cs = <2>;

spidev@0 {
    compatible = "rockchip,spidev";
    reg = <0>;
    spi-max-frequency = <50000000>;
    status = "okay";
};

spidev@1 {
    compatible = "rockchip,spidev";
    reg = <1>;
    spi-max-frequency = <50000000>;
    cs-gpios = <&gpio3 3 GPIO_ACTIVE_LOW>;
    status = "okay";
};

};

dtc -@ -I dts -O dtb -o rk3568-spi3m1-cs0-gpio-cs1.dtbo rk3568-spi3m1-cs0-gpio-cs1.dts
Error: rk3568-spi3m1-cs0-gpio-cs1.dts:36.30-31 syntax error
FATAL ERROR: Unable to parse input tree

I keep getting this error though. Has anybody gotten a second chip select working using a gpio pin as it for spidev3?

Missing header files ?
Please try

#include <dt-bindings/gpio/gpio.h>

including that header file still gave the same error. where is that header located specifically? I see a lot of different rk3568-*.dts overlays. I used the spi3-m1-cs0 and that worked as well. but I am not able to get an overlay that has another chipselect that uses a gpio for it.

you need install the header file first

root@radxa-zero3:~# cat rk3568-spi3m1-cs0-gpio-cs1.dts
/dts-v1/;
/plugin/;

#include <dt-bindings/gpio/gpio.h>

/ {
metadata {
title = “Enable spidev on SPI3-M1 with CS0 and CS1”;
compatible = “radxa,zero3”;
category = “misc”;
exclusive = “GPIO4_C6”, “GPIO3_A3”;
description = “Enable spidev on SPI3-M1 with hardware CS0 and software CS1.”;
};
};

&spi3 {
status = “okay”;
#address-cells = <1>;
#size-cells = <0>;
pinctrl-names = “default”, “high_speed”;
pinctrl-0 = <&spi3m1_cs0 &spi3m1_pins>;
pinctrl-1 = <&spi3m1_cs0 &spi3m1_pins_hs>;
max-freq = <50000000>;
num-cs = <2>;

spidev@0 {
compatible = “rockchip,spidev”;
reg = <0>;
spi-max-frequency = <50000000>;
status = “okay”;
};

spidev@1 {
compatible = “rockchip,spidev”;
reg = <1>;
spi-max-frequency = <50000000>;
cs-gpios = <&gpio3 3 GPIO_ACTIVE_LOW>;
status = “okay”;
};
};
root@radxa-zero3:~# uname -a
Linux radxa-zero3 5.10.160-26-rk356x #bfb9351f3 SMP Wed Jan 10 07:01:50 UTC 2024 aarch64 GNU/Linux
root@radxa-zero3:~# dtc -@ -I dts -O dtb -o rk3568-spi3m1-cs0-gpio-cs1.dtbo rk3568-spi3m1-cs0-gpio-cs1.dts
Error: rk3568-spi3m1-cs0-gpio-cs1.dts:4.1-9 syntax error
FATAL ERROR: Unable to parse input tree
root@radxa-zero3:~#

Still getting errors after going back to the recommended bullseye xfce image.

/dts-v1/;
/plugin/;

#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/pinctrl/rockchip.h>

/ {
fragment@0 {
target = <&spi3>;
overlay {
status = “okay”;
#address-cells = <1>;
#size-cells = <0>;

        pinctrl-names = "default";
        pinctrl-0 = <&spi3m1_cs0 &spi3m1_pins>;

        num-cs = <2>;
        
        spidev@0 {
            compatible = "rockchip,spidev";
            reg = <0>;
            spi-max-frequency = <50000000>;
            status = "okay";
        };

        spidev@1 {
            compatible = "rockchip,spidev";
            reg = <1>;
            spi-max-frequency = <50000000>;
            cs-gpios = <&gpio1 20 GPIO_ACTIVE_LOW>;  // GPIO1_B4 = GPIO20 = pin 12
            status = "okay";
        };
    };
};

fragment@1 {
    target = <&gpio1>;
    __overlay__ {
        gpio-line-names = "spi3-cs1-gpio";
    };
};

};

This is the dts file I ended up using. I had to preprocess the file first in order to not get errors. Is this the correct way to compile the dtb when using the includes?

cpp -nostdinc -I /usr/src/linux-headers-5.10.160-26-rk356x/include
-I /usr/src/linux-headers-5.10.160-26-rk356x/include/dt-bindings
-undef -x assembler-with-cpp rk3568-spi3-m1-gpio-cs1.dts
-o rk3568-spi3-m1-gpio-cs1.preprocessed.dts

And then compile

dtc -@ -I dts -O dtb -o rk3568-spi3-m1-gpio-cs1.dtbo rk3568-spi3-m1-gpio-cs1.preprocessed.dts

I now have both two different chip selects available. I need to test it now to make sure it works

radxa@radxa-zero3:~$ ls /dev/spidev3.*

/dev/spidev3.0 /dev/spidev3.1

radxa@radxa-zero3:~$

If I did something wrong please let me know.

UPDATE:

I used my waveshare can hat to test the spi port. spidev3.0 worked as expected and spidev3.1 luckily worked as well from running spidev_test. Will need to test with actual sensors next.

radxa@radxa-zero3 : ~/spidev-test $ ./spidev_test -D /dev/spidev3.1 -v

spi mode: 0x0

bits per word: 8

max speed: 500000 Hz (500 KHz)

TX | FF FF FF FF FF FF 40 00 00 00 00 95 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF F0 0D | …@…?..?.

RX | FF FF FF FF FF FF 40 00 00 00 00 95 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF F0 0D | …@…?..?.

radxa@radxa-zero3 : ~/spidev-test $