2

I am writing a simple SPI Protocol driver to connect to a spi master and send data out to a non-spi decive over IO11 MOSI on Intel Galileo Gen 2. But I am confused about how to bind to the platform spi device. I have the following code which I understand will do the binding but the description confuses me when it says the it will bind with any spi device with the modalias of "CHIP". My question is what name should I use that is already created as a platform device and to which I will bind?


How do I write an "SPI Protocol Driver"?

Most SPI drivers are currently kernel drivers, but there's also support for userspace drivers. Here we talk only about kernel drivers.

SPI protocol drivers somewhat resemble platform device drivers:

static struct spi_driver CHIP_driver = {
    .driver = {
        .name       = "CHIP",
        .owner      = THIS_MODULE,
    },

    .probe      = CHIP_probe,
    .remove     = CHIP_remove,
    .suspend    = CHIP_suspend,
    .resume     = CHIP_resume,
};

The driver core will automatically attempt to bind this driver to any SPI device whose board_info gave a modalias of "CHIP". Your probe() code might look like this unless you're creating a device which is managing a bus (appearing under /sys/class/spi_master).

static int CHIP_probe(struct spi_device *spi)
{
    struct CHIP         *chip;
    struct CHIP_platform_data   *pdata;

    /* assuming the driver requires board-specific data: */
    pdata = &spi->dev.platform_data;
    if (!pdata)
        return -ENODEV;

    /* get memory for driver's per-chip state */
    chip = kzalloc(sizeof *chip, GFP_KERNEL);
    if (!chip)
        return -ENOMEM;
    spi_set_drvdata(spi, chip);

    ... etc
    return 0;
}

----------—--------------------------

ASU Boy
  • 21
  • 1

0 Answers0