spidev 6.8 KB

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