SPI add devices/GPIO CS/shared MISO MOSI



  • Hello, I'm trying to add some devices on spi. Here i found the way to do it.
    This my spi section in device tree:

    pinctrl-names = "default";
    pinctrl-0 = <&spi_pins>, <&spi_cs1_pins>;
    cs-gpios = <0>, <0>, <&gpio1 17 0>;
    
    m25p80@0 {
    	#address-cells = <1>;
    	#size-cells = <1>;
    	compatible = "jedec,spi-nor";
    	reg = <0>;
    	spi-max-frequency = <40000000>;
    	m25p,chunked-io = <31>;
    
    	partition@0 {
    		label = "u-boot";
    		reg = <0x0 0x30000>;
    		read-only;
    	};
    
    	partition@30000 {
    		label = "u-boot-env";
    		reg = <0x30000 0x10000>;
    		read-only;
    	};
    
    	factory: partition@40000 {
    		label = "factory";
    		reg = <0x40000 0x10000>;
    		read-only;
    	};
    
    	firmware: partition@50000 {
    		label = "firmware";
    	};
    };
    
    spidev@1 {
    	compatible = "linux,spidev";
    	reg = <1>;
    	spi-max-frequency = <40000000>;
    };
    
    spidev@2 {
    	compatible = "linux,spidev";
    	reg = <2>;
    	spi-max-frequency = <40000000>;
    };
    

    current driver : https://github.com/wdu/mt7688

    my code, using python :

    import onionSpi, onionGpio, time
    
    busNumber = 2
    deviceId = 32766
    
    rst  = onionGpio.OnionGpio(15)
    
    rst.setOutputDirection(0)
    
    codec = onionSpi.OnionSpi(busNumber, deviceId)
    
    codec.sck =  7
    codec.mosi = 8
    codec.miso = 9
    codec.cs = 17
    codec.mode = 3
    codec.speed = 3125000
    
    # check the device
    if codec.checkDevice() is 0:
            mapped = "the device adapter is already mapped."
    else:
            mapped = "the device adapter is NOT mapped."
    print("Registered: {}".format(mapped))
    
    # register the device
    if codec.registerDevice() is 0:
            success = "Device with the specified bus number and device ID is already registered OR         successfully registered SPI device adapter."
    else:
            success = "Unable to register device."
    print("Registering.. : {}".format(success))
    
    # initialize the device parameters
    if codec.setupDevice() is 0:
            success = "Setup successful."
    else:
            success = "Setup failed."
    
    print("Setting up spi device: {}".format(success))
    
    # check the device again
    if codec.checkDevice() is 0:
            mapped = "the device adapter is already mapped."
    else:
            mapped = "the device adapter is NOT mapped."
    print("Registered: {}".format(mapped))
    
    
    rst.setOutputDirection(1)
    
    time.sleep(0.0001)
    
    rst.setOutputDirection(0)
    ret     = codec.writeBytes(0x04, [0x1A])
    print(ret)
    

    it's returned:

    Registered: the device adapter is already mapped.
    SPI device already available
    Registering.. : Device with the specified bus number and device ID is already registered OR successfully registered SPI device adapter.
    Initializing SPI parameters...
    Set SPI mode: 0x500
    Set bits per word: 0
    Set max speed: 3125000 Hz (3125 KHz)
    ERROR: Cannot set SPI mode 0x500
    Setting up spi device: Setup failed.
    Registered: the device adapter is already mapped.
    ERROR: SPI transfer failed
    Traceback (most recent call last):
    File "/root/kek.py", line 56, in <module>
    ret = codec.writeBytes(0x04, [0x1A])
    IOError: SPI transaction failed.

    spidev is returned :

    [ 2084.178830] spidev spi32766.2: setup: ignoring unsupported mode bits 500
    [ 2084.185747] spidev spi32766.2: failed to request gpio cs 49
    

    WHY GPIO 49??

    GPIO 17 always HIGH. Any Ideas?



  • Ok, i understand why it was 49, i was using wrong gpio bank, but anyway now it's look like that

    root@Omega-6D86:/# cat /sys/kernel/debug/gpio
    GPIOs 0-31, platform/10000600.gpio, 10000600.gpio:
     gpio-17  (                    |spi-mt7621          ) out hi
    
    GPIOs 32-63, platform/10000600.gpio, 10000600.gpio:
     gpio-38  (                    |reset               ) in  lo
    
    GPIOs 64-95, platform/10000600.gpio, 10000600.gpio:
    

    gpio 17 really registred like cs, but when i'm tryin to make a transaction:

    root@Omega-6D86:~# python writebytes2.py
    Traceback (most recent call last):
     File "writebytes2.py", line 16, in <module>
      time.sleep(0.0004)
    IOError: [Errno 16] Resource busy
    

    and what i have, when tryin to export this pin:

         root@Omega-6D86:/sys/class/gpio# echo 17 > export
         -ash: write error: Resource busy
    

    Any ideas?



  • Okay, I solved this, now i have workin SPI with cs lines on gpio.
    This final spi driver spi-mt7621

    In Device tree(Omega2.dtsi) in node spi u need to add cs-gpios like this:

    &spi0 {
    status = "okay";
    
    pinctrl-names = "default";
    pinctrl-0 = <&spi_pins>, <&spi_cs1_pins>;
    cs-gpios = <0>, <0>, <&gpio0 17 1>;
    
    m25p80@0 {
    	#address-cells = <1>;
    	#size-cells = <1>;
    	compatible = "jedec,spi-nor";
    

    and u need to add spi-device node like this :

    spidev@1 {
    	#address-cells = <1>;
    	#size-cells = <1>;
    	compatible = "linux,spidev";
    	spi-max-frequency = <3125000>;
    	reg = <1>;
    	};
    
    ad193x: ad193x@2 {
    	#address-cells = <1>;
    	#size-cells = <1>;
    	compatible = "adi,ad193x";
    	spi-max-frequency = <3125000>;
    	reg = <2>;
    	#sound-dai-cells = <0>;
    	};	
    

    It must be work fine.



  • As you're also linking in the source of the driver, the following question arises:

    Is this purely a device tree addition/enhancement, or is also a patch to the spidev kernel module needed?

    Btw: great work! šŸ™‚



  • @Rogier-Lodewijks no, u don't need patch spidev, only spi-mt7621.c source file and it's in kernel yea. After this u can add cs-gpios in device tree, and spi device in node. Spi device can be spidev if u developing spi driver and u need its API. Thank you)



Looks like your connection to Community was lost, please wait while we try to reconnect.