spidev.rst 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. =================
  2. SPI userspace API
  3. =================
  4. SPI devices have a limited userspace API, supporting basic half-duplex
  5. read() and write() access to SPI slave devices. Using ioctl() requests,
  6. full duplex transfers and device I/O configuration are also available.
  7. ::
  8. #include <fcntl.h>
  9. #include <unistd.h>
  10. #include <sys/ioctl.h>
  11. #include <linux/types.h>
  12. #include <linux/spi/spidev.h>
  13. Some reasons you might want to use this programming interface include:
  14. * Prototyping in an environment that's not crash-prone; stray pointers
  15. in userspace won't normally bring down any Linux system.
  16. * Developing simple protocols used to talk to microcontrollers acting
  17. as SPI slaves, which you may need to change quite often.
  18. Of course there are drivers that can never be written in userspace, because
  19. they need to access kernel interfaces (such as IRQ handlers or other layers
  20. of the driver stack) that are not accessible to userspace.
  21. DEVICE CREATION, DRIVER BINDING
  22. ===============================
  23. The spidev driver contains lists of SPI devices that are supported for
  24. the different hardware topology representations.
  25. The following are the SPI device tables supported by the spidev driver:
  26. - struct spi_device_id spidev_spi_ids[]: list of devices that can be
  27. bound when these are defined using a struct spi_board_info with a
  28. .modalias field matching one of the entries in the table.
  29. - struct of_device_id spidev_dt_ids[]: list of devices that can be
  30. bound when these are defined using a Device Tree node that has a
  31. compatible string matching one of the entries in the table.
  32. - struct acpi_device_id spidev_acpi_ids[]: list of devices that can
  33. be bound when these are defined using a ACPI device object with a
  34. _HID matching one of the entries in the table.
  35. You are encouraged to add an entry for your SPI device name to relevant
  36. tables, if these don't already have an entry for the device. To do that,
  37. post a patch for spidev to the [email protected] mailing list.
  38. It used to be supported to define an SPI device using the "spidev" name.
  39. For example, as .modalias = "spidev" or compatible = "spidev". But this
  40. is no longer supported by the Linux kernel and instead a real SPI device
  41. name as listed in one of the tables must be used.
  42. Not having a real SPI device name will lead to an error being printed and
  43. the spidev driver failing to probe.
  44. Sysfs also supports userspace driven binding/unbinding of drivers to
  45. devices that do not bind automatically using one of the tables above.
  46. To make the spidev driver bind to such a device, use the following:
  47. echo spidev > /sys/bus/spi/devices/spiB.C/driver_override
  48. echo spiB.C > /sys/bus/spi/drivers/spidev/bind
  49. When the spidev driver is bound to a SPI device, the sysfs node for the
  50. device will include a child device node with a "dev" attribute that will
  51. be understood by udev or mdev (udev replacement from BusyBox; it's less
  52. featureful, but often enough).
  53. For a SPI device with chipselect C on bus B, you should see:
  54. /dev/spidevB.C ...
  55. character special device, major number 153 with
  56. a dynamically chosen minor device number. This is the node
  57. that userspace programs will open, created by "udev" or "mdev".
  58. /sys/devices/.../spiB.C ...
  59. as usual, the SPI device node will
  60. be a child of its SPI master controller.
  61. /sys/class/spidev/spidevB.C ...
  62. created when the "spidev" driver
  63. binds to that device. (Directory or symlink, based on whether
  64. or not you enabled the "deprecated sysfs files" Kconfig option.)
  65. Do not try to manage the /dev character device special file nodes by hand.
  66. That's error prone, and you'd need to pay careful attention to system
  67. security issues; udev/mdev should already be configured securely.
  68. If you unbind the "spidev" driver from that device, those two "spidev" nodes
  69. (in sysfs and in /dev) should automatically be removed (respectively by the
  70. kernel and by udev/mdev). You can unbind by removing the "spidev" driver
  71. module, which will affect all devices using this driver. You can also unbind
  72. by having kernel code remove the SPI device, probably by removing the driver
  73. for its SPI controller (so its spi_master vanishes).
  74. Since this is a standard Linux device driver -- even though it just happens
  75. to expose a low level API to userspace -- it can be associated with any number
  76. of devices at a time. Just provide one spi_board_info record for each such
  77. SPI device, and you'll get a /dev device node for each device.
  78. BASIC CHARACTER DEVICE API
  79. ==========================
  80. Normal open() and close() operations on /dev/spidevB.D files work as you
  81. would expect.
  82. Standard read() and write() operations are obviously only half-duplex, and
  83. the chipselect is deactivated between those operations. Full-duplex access,
  84. and composite operation without chipselect de-activation, is available using
  85. the SPI_IOC_MESSAGE(N) request.
  86. Several ioctl() requests let your driver read or override the device's current
  87. settings for data transfer parameters:
  88. SPI_IOC_RD_MODE, SPI_IOC_WR_MODE ...
  89. pass a pointer to a byte which will
  90. return (RD) or assign (WR) the SPI transfer mode. Use the constants
  91. SPI_MODE_0..SPI_MODE_3; or if you prefer you can combine SPI_CPOL
  92. (clock polarity, idle high iff this is set) or SPI_CPHA (clock phase,
  93. sample on trailing edge iff this is set) flags.
  94. Note that this request is limited to SPI mode flags that fit in a
  95. single byte.
  96. SPI_IOC_RD_MODE32, SPI_IOC_WR_MODE32 ...
  97. pass a pointer to a uin32_t
  98. which will return (RD) or assign (WR) the full SPI transfer mode,
  99. not limited to the bits that fit in one byte.
  100. SPI_IOC_RD_LSB_FIRST, SPI_IOC_WR_LSB_FIRST ...
  101. pass a pointer to a byte
  102. which will return (RD) or assign (WR) the bit justification used to
  103. transfer SPI words. Zero indicates MSB-first; other values indicate
  104. the less common LSB-first encoding. In both cases the specified value
  105. is right-justified in each word, so that unused (TX) or undefined (RX)
  106. bits are in the MSBs.
  107. SPI_IOC_RD_BITS_PER_WORD, SPI_IOC_WR_BITS_PER_WORD ...
  108. pass a pointer to
  109. a byte which will return (RD) or assign (WR) the number of bits in
  110. each SPI transfer word. The value zero signifies eight bits.
  111. SPI_IOC_RD_MAX_SPEED_HZ, SPI_IOC_WR_MAX_SPEED_HZ ...
  112. pass a pointer to a
  113. u32 which will return (RD) or assign (WR) the maximum SPI transfer
  114. speed, in Hz. The controller can't necessarily assign that specific
  115. clock speed.
  116. NOTES:
  117. - At this time there is no async I/O support; everything is purely
  118. synchronous.
  119. - There's currently no way to report the actual bit rate used to
  120. shift data to/from a given device.
  121. - From userspace, you can't currently change the chip select polarity;
  122. that could corrupt transfers to other devices sharing the SPI bus.
  123. Each SPI device is deselected when it's not in active use, allowing
  124. other drivers to talk to other devices.
  125. - There's a limit on the number of bytes each I/O request can transfer
  126. to the SPI device. It defaults to one page, but that can be changed
  127. using a module parameter.
  128. - Because SPI has no low-level transfer acknowledgement, you usually
  129. won't see any I/O errors when talking to a non-existent device.
  130. FULL DUPLEX CHARACTER DEVICE API
  131. ================================
  132. See the spidev_fdx.c sample program for one example showing the use of the
  133. full duplex programming interface. (Although it doesn't perform a full duplex
  134. transfer.) The model is the same as that used in the kernel spi_sync()
  135. request; the individual transfers offer the same capabilities as are
  136. available to kernel drivers (except that it's not asynchronous).
  137. The example shows one half-duplex RPC-style request and response message.
  138. These requests commonly require that the chip not be deselected between
  139. the request and response. Several such requests could be chained into
  140. a single kernel request, even allowing the chip to be deselected after
  141. each response. (Other protocol options include changing the word size
  142. and bitrate for each transfer segment.)
  143. To make a full duplex request, provide both rx_buf and tx_buf for the
  144. same transfer. It's even OK if those are the same buffer.