Back to home page

OSCL-LXR

 
 

    


0001 =================
0002 SPI userspace API
0003 =================
0004 
0005 SPI devices have a limited userspace API, supporting basic half-duplex
0006 read() and write() access to SPI slave devices.  Using ioctl() requests,
0007 full duplex transfers and device I/O configuration are also available.
0008 
0009 ::
0010 
0011         #include <fcntl.h>
0012         #include <unistd.h>
0013         #include <sys/ioctl.h>
0014         #include <linux/types.h>
0015         #include <linux/spi/spidev.h>
0016 
0017 Some reasons you might want to use this programming interface include:
0018 
0019  * Prototyping in an environment that's not crash-prone; stray pointers
0020    in userspace won't normally bring down any Linux system.
0021 
0022  * Developing simple protocols used to talk to microcontrollers acting
0023    as SPI slaves, which you may need to change quite often.
0024 
0025 Of course there are drivers that can never be written in userspace, because
0026 they need to access kernel interfaces (such as IRQ handlers or other layers
0027 of the driver stack) that are not accessible to userspace.
0028 
0029 
0030 DEVICE CREATION, DRIVER BINDING
0031 ===============================
0032 
0033 The spidev driver contains lists of SPI devices that are supported for
0034 the different hardware topology representations.
0035 
0036 The following are the SPI device tables supported by the spidev driver:
0037 
0038     - struct spi_device_id spidev_spi_ids[]: list of devices that can be
0039       bound when these are defined using a struct spi_board_info with a
0040       .modalias field matching one of the entries in the table.
0041 
0042     - struct of_device_id spidev_dt_ids[]: list of devices that can be
0043       bound when these are defined using a Device Tree node that has a
0044       compatible string matching one of the entries in the table.
0045 
0046     - struct acpi_device_id spidev_acpi_ids[]: list of devices that can
0047       be bound when these are defined using a ACPI device object with a
0048       _HID matching one of the entries in the table.
0049 
0050 You are encouraged to add an entry for your SPI device name to relevant
0051 tables, if these don't already have an entry for the device. To do that,
0052 post a patch for spidev to the linux-spi@vger.kernel.org mailing list.
0053 
0054 It used to be supported to define an SPI device using the "spidev" name.
0055 For example, as .modalias = "spidev" or compatible = "spidev".  But this
0056 is no longer supported by the Linux kernel and instead a real SPI device
0057 name as listed in one of the tables must be used.
0058 
0059 Not having a real SPI device name will lead to an error being printed and
0060 the spidev driver failing to probe.
0061 
0062 Sysfs also supports userspace driven binding/unbinding of drivers to
0063 devices that do not bind automatically using one of the tables above.
0064 To make the spidev driver bind to such a device, use the following:
0065 
0066     echo spidev > /sys/bus/spi/devices/spiB.C/driver_override
0067     echo spiB.C > /sys/bus/spi/drivers/spidev/bind
0068 
0069 When the spidev driver is bound to a SPI device, the sysfs node for the
0070 device will include a child device node with a "dev" attribute that will
0071 be understood by udev or mdev (udev replacement from BusyBox; it's less
0072 featureful, but often enough).
0073 
0074 For a SPI device with chipselect C on bus B, you should see:
0075 
0076     /dev/spidevB.C ...
0077         character special device, major number 153 with
0078         a dynamically chosen minor device number.  This is the node
0079         that userspace programs will open, created by "udev" or "mdev".
0080 
0081     /sys/devices/.../spiB.C ...
0082         as usual, the SPI device node will
0083         be a child of its SPI master controller.
0084 
0085     /sys/class/spidev/spidevB.C ...
0086         created when the "spidev" driver
0087         binds to that device.  (Directory or symlink, based on whether
0088         or not you enabled the "deprecated sysfs files" Kconfig option.)
0089 
0090 Do not try to manage the /dev character device special file nodes by hand.
0091 That's error prone, and you'd need to pay careful attention to system
0092 security issues; udev/mdev should already be configured securely.
0093 
0094 If you unbind the "spidev" driver from that device, those two "spidev" nodes
0095 (in sysfs and in /dev) should automatically be removed (respectively by the
0096 kernel and by udev/mdev).  You can unbind by removing the "spidev" driver
0097 module, which will affect all devices using this driver.  You can also unbind
0098 by having kernel code remove the SPI device, probably by removing the driver
0099 for its SPI controller (so its spi_master vanishes).
0100 
0101 Since this is a standard Linux device driver -- even though it just happens
0102 to expose a low level API to userspace -- it can be associated with any number
0103 of devices at a time.  Just provide one spi_board_info record for each such
0104 SPI device, and you'll get a /dev device node for each device.
0105 
0106 
0107 BASIC CHARACTER DEVICE API
0108 ==========================
0109 Normal open() and close() operations on /dev/spidevB.D files work as you
0110 would expect.
0111 
0112 Standard read() and write() operations are obviously only half-duplex, and
0113 the chipselect is deactivated between those operations.  Full-duplex access,
0114 and composite operation without chipselect de-activation, is available using
0115 the SPI_IOC_MESSAGE(N) request.
0116 
0117 Several ioctl() requests let your driver read or override the device's current
0118 settings for data transfer parameters:
0119 
0120     SPI_IOC_RD_MODE, SPI_IOC_WR_MODE ...
0121         pass a pointer to a byte which will
0122         return (RD) or assign (WR) the SPI transfer mode.  Use the constants
0123         SPI_MODE_0..SPI_MODE_3; or if you prefer you can combine SPI_CPOL
0124         (clock polarity, idle high iff this is set) or SPI_CPHA (clock phase,
0125         sample on trailing edge iff this is set) flags.
0126         Note that this request is limited to SPI mode flags that fit in a
0127         single byte.
0128 
0129     SPI_IOC_RD_MODE32, SPI_IOC_WR_MODE32 ...
0130         pass a pointer to a uin32_t
0131         which will return (RD) or assign (WR) the full SPI transfer mode,
0132         not limited to the bits that fit in one byte.
0133 
0134     SPI_IOC_RD_LSB_FIRST, SPI_IOC_WR_LSB_FIRST ...
0135         pass a pointer to a byte
0136         which will return (RD) or assign (WR) the bit justification used to
0137         transfer SPI words.  Zero indicates MSB-first; other values indicate
0138         the less common LSB-first encoding.  In both cases the specified value
0139         is right-justified in each word, so that unused (TX) or undefined (RX)
0140         bits are in the MSBs.
0141 
0142     SPI_IOC_RD_BITS_PER_WORD, SPI_IOC_WR_BITS_PER_WORD ...
0143         pass a pointer to
0144         a byte which will return (RD) or assign (WR) the number of bits in
0145         each SPI transfer word.  The value zero signifies eight bits.
0146 
0147     SPI_IOC_RD_MAX_SPEED_HZ, SPI_IOC_WR_MAX_SPEED_HZ ...
0148         pass a pointer to a
0149         u32 which will return (RD) or assign (WR) the maximum SPI transfer
0150         speed, in Hz.  The controller can't necessarily assign that specific
0151         clock speed.
0152 
0153 NOTES:
0154 
0155     - At this time there is no async I/O support; everything is purely
0156       synchronous.
0157 
0158     - There's currently no way to report the actual bit rate used to
0159       shift data to/from a given device.
0160 
0161     - From userspace, you can't currently change the chip select polarity;
0162       that could corrupt transfers to other devices sharing the SPI bus.
0163       Each SPI device is deselected when it's not in active use, allowing
0164       other drivers to talk to other devices.
0165 
0166     - There's a limit on the number of bytes each I/O request can transfer
0167       to the SPI device.  It defaults to one page, but that can be changed
0168       using a module parameter.
0169 
0170     - Because SPI has no low-level transfer acknowledgement, you usually
0171       won't see any I/O errors when talking to a non-existent device.
0172 
0173 
0174 FULL DUPLEX CHARACTER DEVICE API
0175 ================================
0176 
0177 See the spidev_fdx.c sample program for one example showing the use of the
0178 full duplex programming interface.  (Although it doesn't perform a full duplex
0179 transfer.)  The model is the same as that used in the kernel spi_sync()
0180 request; the individual transfers offer the same capabilities as are
0181 available to kernel drivers (except that it's not asynchronous).
0182 
0183 The example shows one half-duplex RPC-style request and response message.
0184 These requests commonly require that the chip not be deselected between
0185 the request and response.  Several such requests could be chained into
0186 a single kernel request, even allowing the chip to be deselected after
0187 each response.  (Other protocol options include changing the word size
0188 and bitrate for each transfer segment.)
0189 
0190 To make a full duplex request, provide both rx_buf and tx_buf for the
0191 same transfer.  It's even OK if those are the same buffer.