xref: /linux/Documentation/spi/spidev.rst (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
19cdd273eSMauro Carvalho Chehab=================
29cdd273eSMauro Carvalho ChehabSPI userspace API
39cdd273eSMauro Carvalho Chehab=================
49cdd273eSMauro Carvalho Chehab
59cdd273eSMauro Carvalho ChehabSPI devices have a limited userspace API, supporting basic half-duplex
69cdd273eSMauro Carvalho Chehabread() and write() access to SPI slave devices.  Using ioctl() requests,
79cdd273eSMauro Carvalho Chehabfull duplex transfers and device I/O configuration are also available.
89cdd273eSMauro Carvalho Chehab
99cdd273eSMauro Carvalho Chehab::
109cdd273eSMauro Carvalho Chehab
119cdd273eSMauro Carvalho Chehab	#include <fcntl.h>
129cdd273eSMauro Carvalho Chehab	#include <unistd.h>
139cdd273eSMauro Carvalho Chehab	#include <sys/ioctl.h>
149cdd273eSMauro Carvalho Chehab	#include <linux/types.h>
159cdd273eSMauro Carvalho Chehab	#include <linux/spi/spidev.h>
169cdd273eSMauro Carvalho Chehab
179cdd273eSMauro Carvalho ChehabSome reasons you might want to use this programming interface include:
189cdd273eSMauro Carvalho Chehab
199cdd273eSMauro Carvalho Chehab * Prototyping in an environment that's not crash-prone; stray pointers
209cdd273eSMauro Carvalho Chehab   in userspace won't normally bring down any Linux system.
219cdd273eSMauro Carvalho Chehab
229cdd273eSMauro Carvalho Chehab * Developing simple protocols used to talk to microcontrollers acting
239cdd273eSMauro Carvalho Chehab   as SPI slaves, which you may need to change quite often.
249cdd273eSMauro Carvalho Chehab
259cdd273eSMauro Carvalho ChehabOf course there are drivers that can never be written in userspace, because
269cdd273eSMauro Carvalho Chehabthey need to access kernel interfaces (such as IRQ handlers or other layers
279cdd273eSMauro Carvalho Chehabof the driver stack) that are not accessible to userspace.
289cdd273eSMauro Carvalho Chehab
299cdd273eSMauro Carvalho Chehab
309cdd273eSMauro Carvalho ChehabDEVICE CREATION, DRIVER BINDING
319cdd273eSMauro Carvalho Chehab===============================
329cdd273eSMauro Carvalho Chehab
33f6f6a632SJavier Martinez CanillasThe spidev driver contains lists of SPI devices that are supported for
34f6f6a632SJavier Martinez Canillasthe different hardware topology representations.
359cdd273eSMauro Carvalho Chehab
36f6f6a632SJavier Martinez CanillasThe following are the SPI device tables supported by the spidev driver:
37f6f6a632SJavier Martinez Canillas
38f6f6a632SJavier Martinez Canillas    - struct spi_device_id spidev_spi_ids[]: list of devices that can be
39f6f6a632SJavier Martinez Canillas      bound when these are defined using a struct spi_board_info with a
40f6f6a632SJavier Martinez Canillas      .modalias field matching one of the entries in the table.
41f6f6a632SJavier Martinez Canillas
42f6f6a632SJavier Martinez Canillas    - struct of_device_id spidev_dt_ids[]: list of devices that can be
43f6f6a632SJavier Martinez Canillas      bound when these are defined using a Device Tree node that has a
44f6f6a632SJavier Martinez Canillas      compatible string matching one of the entries in the table.
45f6f6a632SJavier Martinez Canillas
46f6f6a632SJavier Martinez Canillas    - struct acpi_device_id spidev_acpi_ids[]: list of devices that can
47f6f6a632SJavier Martinez Canillas      be bound when these are defined using a ACPI device object with a
48f6f6a632SJavier Martinez Canillas      _HID matching one of the entries in the table.
49f6f6a632SJavier Martinez Canillas
50f6f6a632SJavier Martinez CanillasYou are encouraged to add an entry for your SPI device name to relevant
51f6f6a632SJavier Martinez Canillastables, if these don't already have an entry for the device. To do that,
52f6f6a632SJavier Martinez Canillaspost a patch for spidev to the linux-spi@vger.kernel.org mailing list.
53f6f6a632SJavier Martinez Canillas
54f6f6a632SJavier Martinez CanillasIt used to be supported to define an SPI device using the "spidev" name.
55f6f6a632SJavier Martinez CanillasFor example, as .modalias = "spidev" or compatible = "spidev".  But this
56f6f6a632SJavier Martinez Canillasis no longer supported by the Linux kernel and instead a real SPI device
57f6f6a632SJavier Martinez Canillasname as listed in one of the tables must be used.
58f6f6a632SJavier Martinez Canillas
59f6f6a632SJavier Martinez CanillasNot having a real SPI device name will lead to an error being printed and
60f6f6a632SJavier Martinez Canillasthe spidev driver failing to probe.
61f6f6a632SJavier Martinez Canillas
62f6f6a632SJavier Martinez CanillasSysfs also supports userspace driven binding/unbinding of drivers to
63f6f6a632SJavier Martinez Canillasdevices that do not bind automatically using one of the tables above.
64*2ff0573eSDavid LechnerTo make the spidev driver bind to such a device, use the following::
65f6f6a632SJavier Martinez Canillas
66f6f6a632SJavier Martinez Canillas    echo spidev > /sys/bus/spi/devices/spiB.C/driver_override
67f6f6a632SJavier Martinez Canillas    echo spiB.C > /sys/bus/spi/drivers/spidev/bind
68f6f6a632SJavier Martinez Canillas
69f6f6a632SJavier Martinez CanillasWhen the spidev driver is bound to a SPI device, the sysfs node for the
70f6f6a632SJavier Martinez Canillasdevice will include a child device node with a "dev" attribute that will
71f6f6a632SJavier Martinez Canillasbe understood by udev or mdev (udev replacement from BusyBox; it's less
72f6f6a632SJavier Martinez Canillasfeatureful, but often enough).
73f6f6a632SJavier Martinez Canillas
74f6f6a632SJavier Martinez CanillasFor a SPI device with chipselect C on bus B, you should see:
759cdd273eSMauro Carvalho Chehab
769cdd273eSMauro Carvalho Chehab    /dev/spidevB.C ...
779cdd273eSMauro Carvalho Chehab	character special device, major number 153 with
789cdd273eSMauro Carvalho Chehab	a dynamically chosen minor device number.  This is the node
799cdd273eSMauro Carvalho Chehab	that userspace programs will open, created by "udev" or "mdev".
809cdd273eSMauro Carvalho Chehab
819cdd273eSMauro Carvalho Chehab    /sys/devices/.../spiB.C ...
829cdd273eSMauro Carvalho Chehab	as usual, the SPI device node will
839cdd273eSMauro Carvalho Chehab	be a child of its SPI master controller.
849cdd273eSMauro Carvalho Chehab
859cdd273eSMauro Carvalho Chehab    /sys/class/spidev/spidevB.C ...
869cdd273eSMauro Carvalho Chehab	created when the "spidev" driver
879cdd273eSMauro Carvalho Chehab	binds to that device.  (Directory or symlink, based on whether
889cdd273eSMauro Carvalho Chehab	or not you enabled the "deprecated sysfs files" Kconfig option.)
899cdd273eSMauro Carvalho Chehab
909cdd273eSMauro Carvalho ChehabDo not try to manage the /dev character device special file nodes by hand.
919cdd273eSMauro Carvalho ChehabThat's error prone, and you'd need to pay careful attention to system
929cdd273eSMauro Carvalho Chehabsecurity issues; udev/mdev should already be configured securely.
939cdd273eSMauro Carvalho Chehab
949cdd273eSMauro Carvalho ChehabIf you unbind the "spidev" driver from that device, those two "spidev" nodes
959cdd273eSMauro Carvalho Chehab(in sysfs and in /dev) should automatically be removed (respectively by the
969cdd273eSMauro Carvalho Chehabkernel and by udev/mdev).  You can unbind by removing the "spidev" driver
979cdd273eSMauro Carvalho Chehabmodule, which will affect all devices using this driver.  You can also unbind
989cdd273eSMauro Carvalho Chehabby having kernel code remove the SPI device, probably by removing the driver
999cdd273eSMauro Carvalho Chehabfor its SPI controller (so its spi_master vanishes).
1009cdd273eSMauro Carvalho Chehab
1019cdd273eSMauro Carvalho ChehabSince this is a standard Linux device driver -- even though it just happens
1029cdd273eSMauro Carvalho Chehabto expose a low level API to userspace -- it can be associated with any number
1039cdd273eSMauro Carvalho Chehabof devices at a time.  Just provide one spi_board_info record for each such
1049cdd273eSMauro Carvalho ChehabSPI device, and you'll get a /dev device node for each device.
1059cdd273eSMauro Carvalho Chehab
1069cdd273eSMauro Carvalho Chehab
1079cdd273eSMauro Carvalho ChehabBASIC CHARACTER DEVICE API
1089cdd273eSMauro Carvalho Chehab==========================
1099cdd273eSMauro Carvalho ChehabNormal open() and close() operations on /dev/spidevB.D files work as you
1109cdd273eSMauro Carvalho Chehabwould expect.
1119cdd273eSMauro Carvalho Chehab
1129cdd273eSMauro Carvalho ChehabStandard read() and write() operations are obviously only half-duplex, and
1139cdd273eSMauro Carvalho Chehabthe chipselect is deactivated between those operations.  Full-duplex access,
1149cdd273eSMauro Carvalho Chehaband composite operation without chipselect de-activation, is available using
1159cdd273eSMauro Carvalho Chehabthe SPI_IOC_MESSAGE(N) request.
1169cdd273eSMauro Carvalho Chehab
1179cdd273eSMauro Carvalho ChehabSeveral ioctl() requests let your driver read or override the device's current
1189cdd273eSMauro Carvalho Chehabsettings for data transfer parameters:
1199cdd273eSMauro Carvalho Chehab
1209cdd273eSMauro Carvalho Chehab    SPI_IOC_RD_MODE, SPI_IOC_WR_MODE ...
1219cdd273eSMauro Carvalho Chehab	pass a pointer to a byte which will
1229cdd273eSMauro Carvalho Chehab	return (RD) or assign (WR) the SPI transfer mode.  Use the constants
1239cdd273eSMauro Carvalho Chehab	SPI_MODE_0..SPI_MODE_3; or if you prefer you can combine SPI_CPOL
1249cdd273eSMauro Carvalho Chehab	(clock polarity, idle high iff this is set) or SPI_CPHA (clock phase,
1259cdd273eSMauro Carvalho Chehab	sample on trailing edge iff this is set) flags.
1269cdd273eSMauro Carvalho Chehab	Note that this request is limited to SPI mode flags that fit in a
1279cdd273eSMauro Carvalho Chehab	single byte.
1289cdd273eSMauro Carvalho Chehab
1299cdd273eSMauro Carvalho Chehab    SPI_IOC_RD_MODE32, SPI_IOC_WR_MODE32 ...
1309cdd273eSMauro Carvalho Chehab	pass a pointer to a uin32_t
1319cdd273eSMauro Carvalho Chehab	which will return (RD) or assign (WR) the full SPI transfer mode,
1329cdd273eSMauro Carvalho Chehab	not limited to the bits that fit in one byte.
1339cdd273eSMauro Carvalho Chehab
1349cdd273eSMauro Carvalho Chehab    SPI_IOC_RD_LSB_FIRST, SPI_IOC_WR_LSB_FIRST ...
1359cdd273eSMauro Carvalho Chehab	pass a pointer to a byte
1369cdd273eSMauro Carvalho Chehab	which will return (RD) or assign (WR) the bit justification used to
1379cdd273eSMauro Carvalho Chehab	transfer SPI words.  Zero indicates MSB-first; other values indicate
1389cdd273eSMauro Carvalho Chehab	the less common LSB-first encoding.  In both cases the specified value
1399cdd273eSMauro Carvalho Chehab	is right-justified in each word, so that unused (TX) or undefined (RX)
1409cdd273eSMauro Carvalho Chehab	bits are in the MSBs.
1419cdd273eSMauro Carvalho Chehab
1429cdd273eSMauro Carvalho Chehab    SPI_IOC_RD_BITS_PER_WORD, SPI_IOC_WR_BITS_PER_WORD ...
1439cdd273eSMauro Carvalho Chehab	pass a pointer to
1449cdd273eSMauro Carvalho Chehab	a byte which will return (RD) or assign (WR) the number of bits in
1459cdd273eSMauro Carvalho Chehab	each SPI transfer word.  The value zero signifies eight bits.
1469cdd273eSMauro Carvalho Chehab
1479cdd273eSMauro Carvalho Chehab    SPI_IOC_RD_MAX_SPEED_HZ, SPI_IOC_WR_MAX_SPEED_HZ ...
1489cdd273eSMauro Carvalho Chehab	pass a pointer to a
1499cdd273eSMauro Carvalho Chehab	u32 which will return (RD) or assign (WR) the maximum SPI transfer
1509cdd273eSMauro Carvalho Chehab	speed, in Hz.  The controller can't necessarily assign that specific
1519cdd273eSMauro Carvalho Chehab	clock speed.
1529cdd273eSMauro Carvalho Chehab
1539cdd273eSMauro Carvalho ChehabNOTES:
1549cdd273eSMauro Carvalho Chehab
1559cdd273eSMauro Carvalho Chehab    - At this time there is no async I/O support; everything is purely
1569cdd273eSMauro Carvalho Chehab      synchronous.
1579cdd273eSMauro Carvalho Chehab
1589cdd273eSMauro Carvalho Chehab    - There's currently no way to report the actual bit rate used to
1599cdd273eSMauro Carvalho Chehab      shift data to/from a given device.
1609cdd273eSMauro Carvalho Chehab
1619cdd273eSMauro Carvalho Chehab    - From userspace, you can't currently change the chip select polarity;
1629cdd273eSMauro Carvalho Chehab      that could corrupt transfers to other devices sharing the SPI bus.
1639cdd273eSMauro Carvalho Chehab      Each SPI device is deselected when it's not in active use, allowing
1649cdd273eSMauro Carvalho Chehab      other drivers to talk to other devices.
1659cdd273eSMauro Carvalho Chehab
1669cdd273eSMauro Carvalho Chehab    - There's a limit on the number of bytes each I/O request can transfer
1679cdd273eSMauro Carvalho Chehab      to the SPI device.  It defaults to one page, but that can be changed
1689cdd273eSMauro Carvalho Chehab      using a module parameter.
1699cdd273eSMauro Carvalho Chehab
1709cdd273eSMauro Carvalho Chehab    - Because SPI has no low-level transfer acknowledgement, you usually
1719cdd273eSMauro Carvalho Chehab      won't see any I/O errors when talking to a non-existent device.
1729cdd273eSMauro Carvalho Chehab
1739cdd273eSMauro Carvalho Chehab
1749cdd273eSMauro Carvalho ChehabFULL DUPLEX CHARACTER DEVICE API
1759cdd273eSMauro Carvalho Chehab================================
1769cdd273eSMauro Carvalho Chehab
1779cdd273eSMauro Carvalho ChehabSee the spidev_fdx.c sample program for one example showing the use of the
1789cdd273eSMauro Carvalho Chehabfull duplex programming interface.  (Although it doesn't perform a full duplex
1799cdd273eSMauro Carvalho Chehabtransfer.)  The model is the same as that used in the kernel spi_sync()
1809cdd273eSMauro Carvalho Chehabrequest; the individual transfers offer the same capabilities as are
1819cdd273eSMauro Carvalho Chehabavailable to kernel drivers (except that it's not asynchronous).
1829cdd273eSMauro Carvalho Chehab
1839cdd273eSMauro Carvalho ChehabThe example shows one half-duplex RPC-style request and response message.
1849cdd273eSMauro Carvalho ChehabThese requests commonly require that the chip not be deselected between
1859cdd273eSMauro Carvalho Chehabthe request and response.  Several such requests could be chained into
1869cdd273eSMauro Carvalho Chehaba single kernel request, even allowing the chip to be deselected after
1879cdd273eSMauro Carvalho Chehabeach response.  (Other protocol options include changing the word size
1889cdd273eSMauro Carvalho Chehaband bitrate for each transfer segment.)
1899cdd273eSMauro Carvalho Chehab
1909cdd273eSMauro Carvalho ChehabTo make a full duplex request, provide both rx_buf and tx_buf for the
1919cdd273eSMauro Carvalho Chehabsame transfer.  It's even OK if those are the same buffer.
192