title: Linux SPI Protocol driver
body: <p>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?</p>
  
  <hr>
  
  <h2>How do I write an "SPI Protocol Driver"?</h2>
  
  <p>Most SPI drivers are currently kernel drivers, but there's also support
  for userspace drivers.  Here we talk only about kernel drivers.</p>
  
  <p>SPI protocol drivers somewhat resemble platform device drivers:</p>
  
  <pre><code>static struct spi_driver CHIP_driver = {
      .driver = {
          .name       = "CHIP",
          .owner      = THIS_MODULE,
      },
  
      .probe      = CHIP_probe,
      .remove     = CHIP_remove,
      .suspend    = CHIP_suspend,
      .resume     = CHIP_resume,
  };
  </code></pre>
  
  <p>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).</p>
  
  <pre><code>static int CHIP_probe(struct spi_device *spi)
  {
      struct CHIP         *chip;
      struct CHIP_platform_data   *pdata;
  
      /* assuming the driver requires board-specific data: */
      pdata = &amp;spi-&gt;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;
  }
  </code></pre>
  
  <p>----------—--------------------------</p>