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 33*f6f6a632SJavier Martinez CanillasThe spidev driver contains lists of SPI devices that are supported for 34*f6f6a632SJavier Martinez Canillasthe different hardware topology representations. 359cdd273eSMauro Carvalho Chehab 36*f6f6a632SJavier Martinez CanillasThe following are the SPI device tables supported by the spidev driver: 37*f6f6a632SJavier Martinez Canillas 38*f6f6a632SJavier Martinez Canillas - struct spi_device_id spidev_spi_ids[]: list of devices that can be 39*f6f6a632SJavier Martinez Canillas bound when these are defined using a struct spi_board_info with a 40*f6f6a632SJavier Martinez Canillas .modalias field matching one of the entries in the table. 41*f6f6a632SJavier Martinez Canillas 42*f6f6a632SJavier Martinez Canillas - struct of_device_id spidev_dt_ids[]: list of devices that can be 43*f6f6a632SJavier Martinez Canillas bound when these are defined using a Device Tree node that has a 44*f6f6a632SJavier Martinez Canillas compatible string matching one of the entries in the table. 45*f6f6a632SJavier Martinez Canillas 46*f6f6a632SJavier Martinez Canillas - struct acpi_device_id spidev_acpi_ids[]: list of devices that can 47*f6f6a632SJavier Martinez Canillas be bound when these are defined using a ACPI device object with a 48*f6f6a632SJavier Martinez Canillas _HID matching one of the entries in the table. 49*f6f6a632SJavier Martinez Canillas 50*f6f6a632SJavier Martinez CanillasYou are encouraged to add an entry for your SPI device name to relevant 51*f6f6a632SJavier Martinez Canillastables, if these don't already have an entry for the device. To do that, 52*f6f6a632SJavier Martinez Canillaspost a patch for spidev to the linux-spi@vger.kernel.org mailing list. 53*f6f6a632SJavier Martinez Canillas 54*f6f6a632SJavier Martinez CanillasIt used to be supported to define an SPI device using the "spidev" name. 55*f6f6a632SJavier Martinez CanillasFor example, as .modalias = "spidev" or compatible = "spidev". But this 56*f6f6a632SJavier Martinez Canillasis no longer supported by the Linux kernel and instead a real SPI device 57*f6f6a632SJavier Martinez Canillasname as listed in one of the tables must be used. 58*f6f6a632SJavier Martinez Canillas 59*f6f6a632SJavier Martinez CanillasNot having a real SPI device name will lead to an error being printed and 60*f6f6a632SJavier Martinez Canillasthe spidev driver failing to probe. 61*f6f6a632SJavier Martinez Canillas 62*f6f6a632SJavier Martinez CanillasSysfs also supports userspace driven binding/unbinding of drivers to 63*f6f6a632SJavier Martinez Canillasdevices that do not bind automatically using one of the tables above. 64*f6f6a632SJavier Martinez CanillasTo make the spidev driver bind to such a device, use the following: 65*f6f6a632SJavier Martinez Canillas 66*f6f6a632SJavier Martinez Canillas echo spidev > /sys/bus/spi/devices/spiB.C/driver_override 67*f6f6a632SJavier Martinez Canillas echo spiB.C > /sys/bus/spi/drivers/spidev/bind 68*f6f6a632SJavier Martinez Canillas 69*f6f6a632SJavier Martinez CanillasWhen the spidev driver is bound to a SPI device, the sysfs node for the 70*f6f6a632SJavier Martinez Canillasdevice will include a child device node with a "dev" attribute that will 71*f6f6a632SJavier Martinez Canillasbe understood by udev or mdev (udev replacement from BusyBox; it's less 72*f6f6a632SJavier Martinez Canillasfeatureful, but often enough). 73*f6f6a632SJavier Martinez Canillas 74*f6f6a632SJavier 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