Register multiple SPI ports (devices) to single SPI Platform Driver?

Viewed 309

I'm developing a Linux spi driver to handle communication via SPI port. My SoC offers three spi modules (which I understand it as ports) called ecspi1/ecspi2/ecspi3. I have needs to send two kinds of data using ecspi1 and ecspi2.

I've implemented a driver which registered to spi driver and already successfully handled ecspi1 by add below to dts and driver:

[ dts ]

&ecspi1 {
    status = "okay";

    fpga1: lfe5u12f6bg256i@0 {
        reg = <0>;
        compatible = "lattice,lfe5u12f6bg256i";
        spi-max-frequency = <10000000>;
    };
};

[ driver ]

static const struct of_device_id fpga_spi_of_match[] = {
    { .compatible = "lattice,lfe5u12f6bg256i", },
    {},
};

I've tried to add ecspi2 to driver with below modifications. However, driver's probed twice on boot and failed at the second probe period.

[ dts ]

 &ecspi1 {
    status = "okay";

    fpga1: lfe5u12f6bg256i@0 {
        reg = <0>;
        compatible = "lattice,lfe5u12f6bg256i";
        spi-max-frequency = <10000000>;
    };
 };

+&ecspi2 {
+   status = "okay";
+
+   fpga0: fpga_fw@0 {
+       reg = <0>;
+       compatible = "fpga_fw,lfe5u12f6bg256i";
+       spi-max-frequency = <10000000>;
+   };
+};

[ driver ]

 static const struct of_device_id fpga_spi_of_match[] = {
    { .compatible = "fpga_fw,lfe5u12f6bg256i", },
+   { .compatible = "lattice,lfe5u12f6bg256i", },
    {},
 };

Does anyone know how to handle multiple SPI ports (devices) in single driver?

1 Answers

To handle two identical SPI devices with the same driver, the driver doesn't need to be modified and needs only one compatible string:

DTS:

&ecspi1 {
    status = "okay";

    fpga1: lfe5u12f6bg256i@0 {
        reg = <0>;
        compatible = "lattice,lfe5u12f6bg256i";
        spi-max-frequency = <10000000>;
    };
 };

&ecspi2 {
    status = "okay";

    fpga2: lfe5u12f6bg256i@0 {
        reg = <0>;
        compatible = "lattice,lfe5u12f6bg256i";
        spi-max-frequency = <10000000>;
    };
 };

Driver:

static const struct of_device_id fpga_spi_of_match[] = {
    { .compatible = "lattice,lfe5u12f6bg256i", },
    {},
};

The driver will be instantiated twice, once for each device, and each time it will receive a different struct spi_device.

Related