18c2ecf20Sopenharmony_ci====================================
28c2ecf20Sopenharmony_ciOverview of Linux kernel SPI support
38c2ecf20Sopenharmony_ci====================================
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci02-Feb-2012
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ciWhat is SPI?
88c2ecf20Sopenharmony_ci------------
98c2ecf20Sopenharmony_ciThe "Serial Peripheral Interface" (SPI) is a synchronous four wire serial
108c2ecf20Sopenharmony_cilink used to connect microcontrollers to sensors, memory, and peripherals.
118c2ecf20Sopenharmony_ciIt's a simple "de facto" standard, not complicated enough to acquire a
128c2ecf20Sopenharmony_cistandardization body.  SPI uses a master/slave configuration.
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ciThe three signal wires hold a clock (SCK, often on the order of 10 MHz),
158c2ecf20Sopenharmony_ciand parallel data lines with "Master Out, Slave In" (MOSI) or "Master In,
168c2ecf20Sopenharmony_ciSlave Out" (MISO) signals.  (Other names are also used.)  There are four
178c2ecf20Sopenharmony_ciclocking modes through which data is exchanged; mode-0 and mode-3 are most
188c2ecf20Sopenharmony_cicommonly used.  Each clock cycle shifts data out and data in; the clock
198c2ecf20Sopenharmony_cidoesn't cycle except when there is a data bit to shift.  Not all data bits
208c2ecf20Sopenharmony_ciare used though; not every protocol uses those full duplex capabilities.
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ciSPI masters use a fourth "chip select" line to activate a given SPI slave
238c2ecf20Sopenharmony_cidevice, so those three signal wires may be connected to several chips
248c2ecf20Sopenharmony_ciin parallel.  All SPI slaves support chipselects; they are usually active
258c2ecf20Sopenharmony_cilow signals, labeled nCSx for slave 'x' (e.g. nCS0).  Some devices have
268c2ecf20Sopenharmony_ciother signals, often including an interrupt to the master.
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ciUnlike serial busses like USB or SMBus, even low level protocols for
298c2ecf20Sopenharmony_ciSPI slave functions are usually not interoperable between vendors
308c2ecf20Sopenharmony_ci(except for commodities like SPI memory chips).
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci  - SPI may be used for request/response style device protocols, as with
338c2ecf20Sopenharmony_ci    touchscreen sensors and memory chips.
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci  - It may also be used to stream data in either direction (half duplex),
368c2ecf20Sopenharmony_ci    or both of them at the same time (full duplex).
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci  - Some devices may use eight bit words.  Others may use different word
398c2ecf20Sopenharmony_ci    lengths, such as streams of 12-bit or 20-bit digital samples.
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci  - Words are usually sent with their most significant bit (MSB) first,
428c2ecf20Sopenharmony_ci    but sometimes the least significant bit (LSB) goes first instead.
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci  - Sometimes SPI is used to daisy-chain devices, like shift registers.
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ciIn the same way, SPI slaves will only rarely support any kind of automatic
478c2ecf20Sopenharmony_cidiscovery/enumeration protocol.  The tree of slave devices accessible from
488c2ecf20Sopenharmony_cia given SPI master will normally be set up manually, with configuration
498c2ecf20Sopenharmony_citables.
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ciSPI is only one of the names used by such four-wire protocols, and
528c2ecf20Sopenharmony_cimost controllers have no problem handling "MicroWire" (think of it as
538c2ecf20Sopenharmony_cihalf-duplex SPI, for request/response protocols), SSP ("Synchronous
548c2ecf20Sopenharmony_ciSerial Protocol"), PSP ("Programmable Serial Protocol"), and other
558c2ecf20Sopenharmony_cirelated protocols.
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ciSome chips eliminate a signal line by combining MOSI and MISO, and
588c2ecf20Sopenharmony_cilimiting themselves to half-duplex at the hardware level.  In fact
598c2ecf20Sopenharmony_cisome SPI chips have this signal mode as a strapping option.  These
608c2ecf20Sopenharmony_cican be accessed using the same programming interface as SPI, but of
618c2ecf20Sopenharmony_cicourse they won't handle full duplex transfers.  You may find such
628c2ecf20Sopenharmony_cichips described as using "three wire" signaling: SCK, data, nCSx.
638c2ecf20Sopenharmony_ci(That data line is sometimes called MOMI or SISO.)
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ciMicrocontrollers often support both master and slave sides of the SPI
668c2ecf20Sopenharmony_ciprotocol.  This document (and Linux) supports both the master and slave
678c2ecf20Sopenharmony_cisides of SPI interactions.
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ciWho uses it?  On what kinds of systems?
718c2ecf20Sopenharmony_ci---------------------------------------
728c2ecf20Sopenharmony_ciLinux developers using SPI are probably writing device drivers for embedded
738c2ecf20Sopenharmony_cisystems boards.  SPI is used to control external chips, and it is also a
748c2ecf20Sopenharmony_ciprotocol supported by every MMC or SD memory card.  (The older "DataFlash"
758c2ecf20Sopenharmony_cicards, predating MMC cards but using the same connectors and card shape,
768c2ecf20Sopenharmony_cisupport only SPI.)  Some PC hardware uses SPI flash for BIOS code.
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ciSPI slave chips range from digital/analog converters used for analog
798c2ecf20Sopenharmony_cisensors and codecs, to memory, to peripherals like USB controllers
808c2ecf20Sopenharmony_cior Ethernet adapters; and more.
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ciMost systems using SPI will integrate a few devices on a mainboard.
838c2ecf20Sopenharmony_ciSome provide SPI links on expansion connectors; in cases where no
848c2ecf20Sopenharmony_cidedicated SPI controller exists, GPIO pins can be used to create a
858c2ecf20Sopenharmony_cilow speed "bitbanging" adapter.  Very few systems will "hotplug" an SPI
868c2ecf20Sopenharmony_cicontroller; the reasons to use SPI focus on low cost and simple operation,
878c2ecf20Sopenharmony_ciand if dynamic reconfiguration is important, USB will often be a more
888c2ecf20Sopenharmony_ciappropriate low-pincount peripheral bus.
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ciMany microcontrollers that can run Linux integrate one or more I/O
918c2ecf20Sopenharmony_ciinterfaces with SPI modes.  Given SPI support, they could use MMC or SD
928c2ecf20Sopenharmony_cicards without needing a special purpose MMC/SD/SDIO controller.
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ciI'm confused.  What are these four SPI "clock modes"?
968c2ecf20Sopenharmony_ci-----------------------------------------------------
978c2ecf20Sopenharmony_ciIt's easy to be confused here, and the vendor documentation you'll
988c2ecf20Sopenharmony_cifind isn't necessarily helpful.  The four modes combine two mode bits:
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci - CPOL indicates the initial clock polarity.  CPOL=0 means the
1018c2ecf20Sopenharmony_ci   clock starts low, so the first (leading) edge is rising, and
1028c2ecf20Sopenharmony_ci   the second (trailing) edge is falling.  CPOL=1 means the clock
1038c2ecf20Sopenharmony_ci   starts high, so the first (leading) edge is falling.
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci - CPHA indicates the clock phase used to sample data; CPHA=0 says
1068c2ecf20Sopenharmony_ci   sample on the leading edge, CPHA=1 means the trailing edge.
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci   Since the signal needs to stablize before it's sampled, CPHA=0
1098c2ecf20Sopenharmony_ci   implies that its data is written half a clock before the first
1108c2ecf20Sopenharmony_ci   clock edge.  The chipselect may have made it become available.
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ciChip specs won't always say "uses SPI mode X" in as many words,
1138c2ecf20Sopenharmony_cibut their timing diagrams will make the CPOL and CPHA modes clear.
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ciIn the SPI mode number, CPOL is the high order bit and CPHA is the
1168c2ecf20Sopenharmony_cilow order bit.  So when a chip's timing diagram shows the clock
1178c2ecf20Sopenharmony_cistarting low (CPOL=0) and data stabilized for sampling during the
1188c2ecf20Sopenharmony_citrailing clock edge (CPHA=1), that's SPI mode 1.
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ciNote that the clock mode is relevant as soon as the chipselect goes
1218c2ecf20Sopenharmony_ciactive.  So the master must set the clock to inactive before selecting
1228c2ecf20Sopenharmony_cia slave, and the slave can tell the chosen polarity by sampling the
1238c2ecf20Sopenharmony_ciclock level when its select line goes active.  That's why many devices
1248c2ecf20Sopenharmony_cisupport for example both modes 0 and 3:  they don't care about polarity,
1258c2ecf20Sopenharmony_ciand always clock data in/out on rising clock edges.
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ciHow do these driver programming interfaces work?
1298c2ecf20Sopenharmony_ci------------------------------------------------
1308c2ecf20Sopenharmony_ciThe <linux/spi/spi.h> header file includes kerneldoc, as does the
1318c2ecf20Sopenharmony_cimain source code, and you should certainly read that chapter of the
1328c2ecf20Sopenharmony_cikernel API document.  This is just an overview, so you get the big
1338c2ecf20Sopenharmony_cipicture before those details.
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ciSPI requests always go into I/O queues.  Requests for a given SPI device
1368c2ecf20Sopenharmony_ciare always executed in FIFO order, and complete asynchronously through
1378c2ecf20Sopenharmony_cicompletion callbacks.  There are also some simple synchronous wrappers
1388c2ecf20Sopenharmony_cifor those calls, including ones for common transaction types like writing
1398c2ecf20Sopenharmony_cia command and then reading its response.
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ciThere are two types of SPI driver, here called:
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci  Controller drivers ...
1448c2ecf20Sopenharmony_ci        controllers may be built into System-On-Chip
1458c2ecf20Sopenharmony_ci	processors, and often support both Master and Slave roles.
1468c2ecf20Sopenharmony_ci	These drivers touch hardware registers and may use DMA.
1478c2ecf20Sopenharmony_ci	Or they can be PIO bitbangers, needing just GPIO pins.
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci  Protocol drivers ...
1508c2ecf20Sopenharmony_ci        these pass messages through the controller
1518c2ecf20Sopenharmony_ci	driver to communicate with a Slave or Master device on the
1528c2ecf20Sopenharmony_ci	other side of an SPI link.
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ciSo for example one protocol driver might talk to the MTD layer to export
1558c2ecf20Sopenharmony_cidata to filesystems stored on SPI flash like DataFlash; and others might
1568c2ecf20Sopenharmony_cicontrol audio interfaces, present touchscreen sensors as input interfaces,
1578c2ecf20Sopenharmony_cior monitor temperature and voltage levels during industrial processing.
1588c2ecf20Sopenharmony_ciAnd those might all be sharing the same controller driver.
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ciA "struct spi_device" encapsulates the controller-side interface between
1618c2ecf20Sopenharmony_cithose two types of drivers.
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ciThere is a minimal core of SPI programming interfaces, focussing on
1648c2ecf20Sopenharmony_ciusing the driver model to connect controller and protocol drivers using
1658c2ecf20Sopenharmony_cidevice tables provided by board specific initialization code.  SPI
1668c2ecf20Sopenharmony_cishows up in sysfs in several locations::
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci   /sys/devices/.../CTLR ... physical node for a given SPI controller
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci   /sys/devices/.../CTLR/spiB.C ... spi_device on bus "B",
1718c2ecf20Sopenharmony_ci	chipselect C, accessed through CTLR.
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci   /sys/bus/spi/devices/spiB.C ... symlink to that physical
1748c2ecf20Sopenharmony_ci	.../CTLR/spiB.C device
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci   /sys/devices/.../CTLR/spiB.C/modalias ... identifies the driver
1778c2ecf20Sopenharmony_ci	that should be used with this device (for hotplug/coldplug)
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci   /sys/bus/spi/drivers/D ... driver for one or more spi*.* devices
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci   /sys/class/spi_master/spiB ... symlink (or actual device node) to
1828c2ecf20Sopenharmony_ci	a logical node which could hold class related state for the SPI
1838c2ecf20Sopenharmony_ci	master controller managing bus "B".  All spiB.* devices share one
1848c2ecf20Sopenharmony_ci	physical SPI bus segment, with SCLK, MOSI, and MISO.
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci   /sys/devices/.../CTLR/slave ... virtual file for (un)registering the
1878c2ecf20Sopenharmony_ci	slave device for an SPI slave controller.
1888c2ecf20Sopenharmony_ci	Writing the driver name of an SPI slave handler to this file
1898c2ecf20Sopenharmony_ci	registers the slave device; writing "(null)" unregisters the slave
1908c2ecf20Sopenharmony_ci	device.
1918c2ecf20Sopenharmony_ci	Reading from this file shows the name of the slave device ("(null)"
1928c2ecf20Sopenharmony_ci	if not registered).
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci   /sys/class/spi_slave/spiB ... symlink (or actual device node) to
1958c2ecf20Sopenharmony_ci	a logical node which could hold class related state for the SPI
1968c2ecf20Sopenharmony_ci	slave controller on bus "B".  When registered, a single spiB.*
1978c2ecf20Sopenharmony_ci	device is present here, possible sharing the physical SPI bus
1988c2ecf20Sopenharmony_ci	segment with other SPI slave devices.
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ciNote that the actual location of the controller's class state depends
2018c2ecf20Sopenharmony_cion whether you enabled CONFIG_SYSFS_DEPRECATED or not.  At this time,
2028c2ecf20Sopenharmony_cithe only class-specific state is the bus number ("B" in "spiB"), so
2038c2ecf20Sopenharmony_cithose /sys/class entries are only useful to quickly identify busses.
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ciHow does board-specific init code declare SPI devices?
2078c2ecf20Sopenharmony_ci------------------------------------------------------
2088c2ecf20Sopenharmony_ciLinux needs several kinds of information to properly configure SPI devices.
2098c2ecf20Sopenharmony_ciThat information is normally provided by board-specific code, even for
2108c2ecf20Sopenharmony_cichips that do support some of automated discovery/enumeration.
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ciDeclare Controllers
2138c2ecf20Sopenharmony_ci^^^^^^^^^^^^^^^^^^^
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ciThe first kind of information is a list of what SPI controllers exist.
2168c2ecf20Sopenharmony_ciFor System-on-Chip (SOC) based boards, these will usually be platform
2178c2ecf20Sopenharmony_cidevices, and the controller may need some platform_data in order to
2188c2ecf20Sopenharmony_cioperate properly.  The "struct platform_device" will include resources
2198c2ecf20Sopenharmony_cilike the physical address of the controller's first register and its IRQ.
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ciPlatforms will often abstract the "register SPI controller" operation,
2228c2ecf20Sopenharmony_cimaybe coupling it with code to initialize pin configurations, so that
2238c2ecf20Sopenharmony_cithe arch/.../mach-*/board-*.c files for several boards can all share the
2248c2ecf20Sopenharmony_cisame basic controller setup code.  This is because most SOCs have several
2258c2ecf20Sopenharmony_ciSPI-capable controllers, and only the ones actually usable on a given
2268c2ecf20Sopenharmony_ciboard should normally be set up and registered.
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ciSo for example arch/.../mach-*/board-*.c files might have code like::
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	#include <mach/spi.h>	/* for mysoc_spi_data */
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	/* if your mach-* infrastructure doesn't support kernels that can
2338c2ecf20Sopenharmony_ci	 * run on multiple boards, pdata wouldn't benefit from "__init".
2348c2ecf20Sopenharmony_ci	 */
2358c2ecf20Sopenharmony_ci	static struct mysoc_spi_data pdata __initdata = { ... };
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	static __init board_init(void)
2388c2ecf20Sopenharmony_ci	{
2398c2ecf20Sopenharmony_ci		...
2408c2ecf20Sopenharmony_ci		/* this board only uses SPI controller #2 */
2418c2ecf20Sopenharmony_ci		mysoc_register_spi(2, &pdata);
2428c2ecf20Sopenharmony_ci		...
2438c2ecf20Sopenharmony_ci	}
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ciAnd SOC-specific utility code might look something like::
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	#include <mach/spi.h>
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	static struct platform_device spi2 = { ... };
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	void mysoc_register_spi(unsigned n, struct mysoc_spi_data *pdata)
2528c2ecf20Sopenharmony_ci	{
2538c2ecf20Sopenharmony_ci		struct mysoc_spi_data *pdata2;
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci		pdata2 = kmalloc(sizeof *pdata2, GFP_KERNEL);
2568c2ecf20Sopenharmony_ci		*pdata2 = pdata;
2578c2ecf20Sopenharmony_ci		...
2588c2ecf20Sopenharmony_ci		if (n == 2) {
2598c2ecf20Sopenharmony_ci			spi2->dev.platform_data = pdata2;
2608c2ecf20Sopenharmony_ci			register_platform_device(&spi2);
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci			/* also: set up pin modes so the spi2 signals are
2638c2ecf20Sopenharmony_ci			 * visible on the relevant pins ... bootloaders on
2648c2ecf20Sopenharmony_ci			 * production boards may already have done this, but
2658c2ecf20Sopenharmony_ci			 * developer boards will often need Linux to do it.
2668c2ecf20Sopenharmony_ci			 */
2678c2ecf20Sopenharmony_ci		}
2688c2ecf20Sopenharmony_ci		...
2698c2ecf20Sopenharmony_ci	}
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ciNotice how the platform_data for boards may be different, even if the
2728c2ecf20Sopenharmony_cisame SOC controller is used.  For example, on one board SPI might use
2738c2ecf20Sopenharmony_cian external clock, where another derives the SPI clock from current
2748c2ecf20Sopenharmony_cisettings of some master clock.
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ciDeclare Slave Devices
2778c2ecf20Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ciThe second kind of information is a list of what SPI slave devices exist
2808c2ecf20Sopenharmony_cion the target board, often with some board-specific data needed for the
2818c2ecf20Sopenharmony_cidriver to work correctly.
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ciNormally your arch/.../mach-*/board-*.c files would provide a small table
2848c2ecf20Sopenharmony_cilisting the SPI devices on each board.  (This would typically be only a
2858c2ecf20Sopenharmony_cismall handful.)  That might look like::
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	static struct ads7846_platform_data ads_info = {
2888c2ecf20Sopenharmony_ci		.vref_delay_usecs	= 100,
2898c2ecf20Sopenharmony_ci		.x_plate_ohms		= 580,
2908c2ecf20Sopenharmony_ci		.y_plate_ohms		= 410,
2918c2ecf20Sopenharmony_ci	};
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	static struct spi_board_info spi_board_info[] __initdata = {
2948c2ecf20Sopenharmony_ci	{
2958c2ecf20Sopenharmony_ci		.modalias	= "ads7846",
2968c2ecf20Sopenharmony_ci		.platform_data	= &ads_info,
2978c2ecf20Sopenharmony_ci		.mode		= SPI_MODE_0,
2988c2ecf20Sopenharmony_ci		.irq		= GPIO_IRQ(31),
2998c2ecf20Sopenharmony_ci		.max_speed_hz	= 120000 /* max sample rate at 3V */ * 16,
3008c2ecf20Sopenharmony_ci		.bus_num	= 1,
3018c2ecf20Sopenharmony_ci		.chip_select	= 0,
3028c2ecf20Sopenharmony_ci	},
3038c2ecf20Sopenharmony_ci	};
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ciAgain, notice how board-specific information is provided; each chip may need
3068c2ecf20Sopenharmony_ciseveral types.  This example shows generic constraints like the fastest SPI
3078c2ecf20Sopenharmony_ciclock to allow (a function of board voltage in this case) or how an IRQ pin
3088c2ecf20Sopenharmony_ciis wired, plus chip-specific constraints like an important delay that's
3098c2ecf20Sopenharmony_cichanged by the capacitance at one pin.
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci(There's also "controller_data", information that may be useful to the
3128c2ecf20Sopenharmony_cicontroller driver.  An example would be peripheral-specific DMA tuning
3138c2ecf20Sopenharmony_cidata or chipselect callbacks.  This is stored in spi_device later.)
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ciThe board_info should provide enough information to let the system work
3168c2ecf20Sopenharmony_ciwithout the chip's driver being loaded.  The most troublesome aspect of
3178c2ecf20Sopenharmony_cithat is likely the SPI_CS_HIGH bit in the spi_device.mode field, since
3188c2ecf20Sopenharmony_cisharing a bus with a device that interprets chipselect "backwards" is
3198c2ecf20Sopenharmony_cinot possible until the infrastructure knows how to deselect it.
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ciThen your board initialization code would register that table with the SPI
3228c2ecf20Sopenharmony_ciinfrastructure, so that it's available later when the SPI master controller
3238c2ecf20Sopenharmony_cidriver is registered::
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	spi_register_board_info(spi_board_info, ARRAY_SIZE(spi_board_info));
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ciLike with other static board-specific setup, you won't unregister those.
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ciThe widely used "card" style computers bundle memory, cpu, and little else
3308c2ecf20Sopenharmony_cionto a card that's maybe just thirty square centimeters.  On such systems,
3318c2ecf20Sopenharmony_ciyour ``arch/.../mach-.../board-*.c`` file would primarily provide information
3328c2ecf20Sopenharmony_ciabout the devices on the mainboard into which such a card is plugged.  That
3338c2ecf20Sopenharmony_cicertainly includes SPI devices hooked up through the card connectors!
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ciNon-static Configurations
3378c2ecf20Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^^^
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ciDeveloper boards often play by different rules than product boards, and one
3408c2ecf20Sopenharmony_ciexample is the potential need to hotplug SPI devices and/or controllers.
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ciFor those cases you might need to use spi_busnum_to_master() to look
3438c2ecf20Sopenharmony_ciup the spi bus master, and will likely need spi_new_device() to provide the
3448c2ecf20Sopenharmony_ciboard info based on the board that was hotplugged.  Of course, you'd later
3458c2ecf20Sopenharmony_cicall at least spi_unregister_device() when that board is removed.
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ciWhen Linux includes support for MMC/SD/SDIO/DataFlash cards through SPI, those
3488c2ecf20Sopenharmony_ciconfigurations will also be dynamic.  Fortunately, such devices all support
3498c2ecf20Sopenharmony_cibasic device identification probes, so they should hotplug normally.
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ciHow do I write an "SPI Protocol Driver"?
3538c2ecf20Sopenharmony_ci----------------------------------------
3548c2ecf20Sopenharmony_ciMost SPI drivers are currently kernel drivers, but there's also support
3558c2ecf20Sopenharmony_cifor userspace drivers.  Here we talk only about kernel drivers.
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ciSPI protocol drivers somewhat resemble platform device drivers::
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	static struct spi_driver CHIP_driver = {
3608c2ecf20Sopenharmony_ci		.driver = {
3618c2ecf20Sopenharmony_ci			.name		= "CHIP",
3628c2ecf20Sopenharmony_ci			.owner		= THIS_MODULE,
3638c2ecf20Sopenharmony_ci			.pm		= &CHIP_pm_ops,
3648c2ecf20Sopenharmony_ci		},
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci		.probe		= CHIP_probe,
3678c2ecf20Sopenharmony_ci		.remove		= CHIP_remove,
3688c2ecf20Sopenharmony_ci	};
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ciThe driver core will automatically attempt to bind this driver to any SPI
3718c2ecf20Sopenharmony_cidevice whose board_info gave a modalias of "CHIP".  Your probe() code
3728c2ecf20Sopenharmony_cimight look like this unless you're creating a device which is managing
3738c2ecf20Sopenharmony_cia bus (appearing under /sys/class/spi_master).
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci::
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	static int CHIP_probe(struct spi_device *spi)
3788c2ecf20Sopenharmony_ci	{
3798c2ecf20Sopenharmony_ci		struct CHIP			*chip;
3808c2ecf20Sopenharmony_ci		struct CHIP_platform_data	*pdata;
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci		/* assuming the driver requires board-specific data: */
3838c2ecf20Sopenharmony_ci		pdata = &spi->dev.platform_data;
3848c2ecf20Sopenharmony_ci		if (!pdata)
3858c2ecf20Sopenharmony_ci			return -ENODEV;
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci		/* get memory for driver's per-chip state */
3888c2ecf20Sopenharmony_ci		chip = kzalloc(sizeof *chip, GFP_KERNEL);
3898c2ecf20Sopenharmony_ci		if (!chip)
3908c2ecf20Sopenharmony_ci			return -ENOMEM;
3918c2ecf20Sopenharmony_ci		spi_set_drvdata(spi, chip);
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci		... etc
3948c2ecf20Sopenharmony_ci		return 0;
3958c2ecf20Sopenharmony_ci	}
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ciAs soon as it enters probe(), the driver may issue I/O requests to
3988c2ecf20Sopenharmony_cithe SPI device using "struct spi_message".  When remove() returns,
3998c2ecf20Sopenharmony_cior after probe() fails, the driver guarantees that it won't submit
4008c2ecf20Sopenharmony_ciany more such messages.
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci  - An spi_message is a sequence of protocol operations, executed
4038c2ecf20Sopenharmony_ci    as one atomic sequence.  SPI driver controls include:
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci      + when bidirectional reads and writes start ... by how its
4068c2ecf20Sopenharmony_ci        sequence of spi_transfer requests is arranged;
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci      + which I/O buffers are used ... each spi_transfer wraps a
4098c2ecf20Sopenharmony_ci        buffer for each transfer direction, supporting full duplex
4108c2ecf20Sopenharmony_ci        (two pointers, maybe the same one in both cases) and half
4118c2ecf20Sopenharmony_ci        duplex (one pointer is NULL) transfers;
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci      + optionally defining short delays after transfers ... using
4148c2ecf20Sopenharmony_ci        the spi_transfer.delay_usecs setting (this delay can be the
4158c2ecf20Sopenharmony_ci        only protocol effect, if the buffer length is zero);
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci      + whether the chipselect becomes inactive after a transfer and
4188c2ecf20Sopenharmony_ci        any delay ... by using the spi_transfer.cs_change flag;
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci      + hinting whether the next message is likely to go to this same
4218c2ecf20Sopenharmony_ci        device ... using the spi_transfer.cs_change flag on the last
4228c2ecf20Sopenharmony_ci	transfer in that atomic group, and potentially saving costs
4238c2ecf20Sopenharmony_ci	for chip deselect and select operations.
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci  - Follow standard kernel rules, and provide DMA-safe buffers in
4268c2ecf20Sopenharmony_ci    your messages.  That way controller drivers using DMA aren't forced
4278c2ecf20Sopenharmony_ci    to make extra copies unless the hardware requires it (e.g. working
4288c2ecf20Sopenharmony_ci    around hardware errata that force the use of bounce buffering).
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci    If standard dma_map_single() handling of these buffers is inappropriate,
4318c2ecf20Sopenharmony_ci    you can use spi_message.is_dma_mapped to tell the controller driver
4328c2ecf20Sopenharmony_ci    that you've already provided the relevant DMA addresses.
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci  - The basic I/O primitive is spi_async().  Async requests may be
4358c2ecf20Sopenharmony_ci    issued in any context (irq handler, task, etc) and completion
4368c2ecf20Sopenharmony_ci    is reported using a callback provided with the message.
4378c2ecf20Sopenharmony_ci    After any detected error, the chip is deselected and processing
4388c2ecf20Sopenharmony_ci    of that spi_message is aborted.
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci  - There are also synchronous wrappers like spi_sync(), and wrappers
4418c2ecf20Sopenharmony_ci    like spi_read(), spi_write(), and spi_write_then_read().  These
4428c2ecf20Sopenharmony_ci    may be issued only in contexts that may sleep, and they're all
4438c2ecf20Sopenharmony_ci    clean (and small, and "optional") layers over spi_async().
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci  - The spi_write_then_read() call, and convenience wrappers around
4468c2ecf20Sopenharmony_ci    it, should only be used with small amounts of data where the
4478c2ecf20Sopenharmony_ci    cost of an extra copy may be ignored.  It's designed to support
4488c2ecf20Sopenharmony_ci    common RPC-style requests, such as writing an eight bit command
4498c2ecf20Sopenharmony_ci    and reading a sixteen bit response -- spi_w8r16() being one its
4508c2ecf20Sopenharmony_ci    wrappers, doing exactly that.
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ciSome drivers may need to modify spi_device characteristics like the
4538c2ecf20Sopenharmony_citransfer mode, wordsize, or clock rate.  This is done with spi_setup(),
4548c2ecf20Sopenharmony_ciwhich would normally be called from probe() before the first I/O is
4558c2ecf20Sopenharmony_cidone to the device.  However, that can also be called at any time
4568c2ecf20Sopenharmony_cithat no message is pending for that device.
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ciWhile "spi_device" would be the bottom boundary of the driver, the
4598c2ecf20Sopenharmony_ciupper boundaries might include sysfs (especially for sensor readings),
4608c2ecf20Sopenharmony_cithe input layer, ALSA, networking, MTD, the character device framework,
4618c2ecf20Sopenharmony_cior other Linux subsystems.
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ciNote that there are two types of memory your driver must manage as part
4648c2ecf20Sopenharmony_ciof interacting with SPI devices.
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci  - I/O buffers use the usual Linux rules, and must be DMA-safe.
4678c2ecf20Sopenharmony_ci    You'd normally allocate them from the heap or free page pool.
4688c2ecf20Sopenharmony_ci    Don't use the stack, or anything that's declared "static".
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci  - The spi_message and spi_transfer metadata used to glue those
4718c2ecf20Sopenharmony_ci    I/O buffers into a group of protocol transactions.  These can
4728c2ecf20Sopenharmony_ci    be allocated anywhere it's convenient, including as part of
4738c2ecf20Sopenharmony_ci    other allocate-once driver data structures.  Zero-init these.
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ciIf you like, spi_message_alloc() and spi_message_free() convenience
4768c2ecf20Sopenharmony_ciroutines are available to allocate and zero-initialize an spi_message
4778c2ecf20Sopenharmony_ciwith several transfers.
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ciHow do I write an "SPI Master Controller Driver"?
4818c2ecf20Sopenharmony_ci-------------------------------------------------
4828c2ecf20Sopenharmony_ciAn SPI controller will probably be registered on the platform_bus; write
4838c2ecf20Sopenharmony_cia driver to bind to the device, whichever bus is involved.
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ciThe main task of this type of driver is to provide an "spi_master".
4868c2ecf20Sopenharmony_ciUse spi_alloc_master() to allocate the master, and spi_master_get_devdata()
4878c2ecf20Sopenharmony_cito get the driver-private data allocated for that device.
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci::
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_ci	struct spi_master	*master;
4928c2ecf20Sopenharmony_ci	struct CONTROLLER	*c;
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci	master = spi_alloc_master(dev, sizeof *c);
4958c2ecf20Sopenharmony_ci	if (!master)
4968c2ecf20Sopenharmony_ci		return -ENODEV;
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci	c = spi_master_get_devdata(master);
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ciThe driver will initialize the fields of that spi_master, including the
5018c2ecf20Sopenharmony_cibus number (maybe the same as the platform device ID) and three methods
5028c2ecf20Sopenharmony_ciused to interact with the SPI core and SPI protocol drivers.  It will
5038c2ecf20Sopenharmony_cialso initialize its own internal state.  (See below about bus numbering
5048c2ecf20Sopenharmony_ciand those methods.)
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ciAfter you initialize the spi_master, then use spi_register_master() to
5078c2ecf20Sopenharmony_cipublish it to the rest of the system. At that time, device nodes for the
5088c2ecf20Sopenharmony_cicontroller and any predeclared spi devices will be made available, and
5098c2ecf20Sopenharmony_cithe driver model core will take care of binding them to drivers.
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ciIf you need to remove your SPI controller driver, spi_unregister_master()
5128c2ecf20Sopenharmony_ciwill reverse the effect of spi_register_master().
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ciBus Numbering
5168c2ecf20Sopenharmony_ci^^^^^^^^^^^^^
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ciBus numbering is important, since that's how Linux identifies a given
5198c2ecf20Sopenharmony_ciSPI bus (shared SCK, MOSI, MISO).  Valid bus numbers start at zero.  On
5208c2ecf20Sopenharmony_ciSOC systems, the bus numbers should match the numbers defined by the chip
5218c2ecf20Sopenharmony_cimanufacturer.  For example, hardware controller SPI2 would be bus number 2,
5228c2ecf20Sopenharmony_ciand spi_board_info for devices connected to it would use that number.
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ciIf you don't have such hardware-assigned bus number, and for some reason
5258c2ecf20Sopenharmony_ciyou can't just assign them, then provide a negative bus number.  That will
5268c2ecf20Sopenharmony_cithen be replaced by a dynamically assigned number. You'd then need to treat
5278c2ecf20Sopenharmony_cithis as a non-static configuration (see above).
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ciSPI Master Methods
5318c2ecf20Sopenharmony_ci^^^^^^^^^^^^^^^^^^
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci``master->setup(struct spi_device *spi)``
5348c2ecf20Sopenharmony_ci	This sets up the device clock rate, SPI mode, and word sizes.
5358c2ecf20Sopenharmony_ci	Drivers may change the defaults provided by board_info, and then
5368c2ecf20Sopenharmony_ci	call spi_setup(spi) to invoke this routine.  It may sleep.
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci	Unless each SPI slave has its own configuration registers, don't
5398c2ecf20Sopenharmony_ci	change them right away ... otherwise drivers could corrupt I/O
5408c2ecf20Sopenharmony_ci	that's in progress for other SPI devices.
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_ci	.. note::
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci		BUG ALERT:  for some reason the first version of
5458c2ecf20Sopenharmony_ci		many spi_master drivers seems to get this wrong.
5468c2ecf20Sopenharmony_ci		When you code setup(), ASSUME that the controller
5478c2ecf20Sopenharmony_ci		is actively processing transfers for another device.
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci``master->cleanup(struct spi_device *spi)``
5508c2ecf20Sopenharmony_ci	Your controller driver may use spi_device.controller_state to hold
5518c2ecf20Sopenharmony_ci	state it dynamically associates with that device.  If you do that,
5528c2ecf20Sopenharmony_ci	be sure to provide the cleanup() method to free that state.
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_ci``master->prepare_transfer_hardware(struct spi_master *master)``
5558c2ecf20Sopenharmony_ci	This will be called by the queue mechanism to signal to the driver
5568c2ecf20Sopenharmony_ci	that a message is coming in soon, so the subsystem requests the
5578c2ecf20Sopenharmony_ci	driver to prepare the transfer hardware by issuing this call.
5588c2ecf20Sopenharmony_ci	This may sleep.
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci``master->unprepare_transfer_hardware(struct spi_master *master)``
5618c2ecf20Sopenharmony_ci	This will be called by the queue mechanism to signal to the driver
5628c2ecf20Sopenharmony_ci	that there are no more messages pending in the queue and it may
5638c2ecf20Sopenharmony_ci	relax the hardware (e.g. by power management calls). This may sleep.
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci``master->transfer_one_message(struct spi_master *master, struct spi_message *mesg)``
5668c2ecf20Sopenharmony_ci	The subsystem calls the driver to transfer a single message while
5678c2ecf20Sopenharmony_ci	queuing transfers that arrive in the meantime. When the driver is
5688c2ecf20Sopenharmony_ci	finished with this message, it must call
5698c2ecf20Sopenharmony_ci	spi_finalize_current_message() so the subsystem can issue the next
5708c2ecf20Sopenharmony_ci	message. This may sleep.
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci``master->transfer_one(struct spi_master *master, struct spi_device *spi, struct spi_transfer *transfer)``
5738c2ecf20Sopenharmony_ci	The subsystem calls the driver to transfer a single transfer while
5748c2ecf20Sopenharmony_ci	queuing transfers that arrive in the meantime. When the driver is
5758c2ecf20Sopenharmony_ci	finished with this transfer, it must call
5768c2ecf20Sopenharmony_ci	spi_finalize_current_transfer() so the subsystem can issue the next
5778c2ecf20Sopenharmony_ci	transfer. This may sleep. Note: transfer_one and transfer_one_message
5788c2ecf20Sopenharmony_ci	are mutually exclusive; when both are set, the generic subsystem does
5798c2ecf20Sopenharmony_ci	not call your transfer_one callback.
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci	Return values:
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci	* negative errno: error
5848c2ecf20Sopenharmony_ci	* 0: transfer is finished
5858c2ecf20Sopenharmony_ci	* 1: transfer is still in progress
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci``master->set_cs_timing(struct spi_device *spi, u8 setup_clk_cycles, u8 hold_clk_cycles, u8 inactive_clk_cycles)``
5888c2ecf20Sopenharmony_ci	This method allows SPI client drivers to request SPI master controller
5898c2ecf20Sopenharmony_ci	for configuring device specific CS setup, hold and inactive timing
5908c2ecf20Sopenharmony_ci	requirements.
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ciDeprecated Methods
5938c2ecf20Sopenharmony_ci^^^^^^^^^^^^^^^^^^
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci``master->transfer(struct spi_device *spi, struct spi_message *message)``
5968c2ecf20Sopenharmony_ci	This must not sleep. Its responsibility is to arrange that the
5978c2ecf20Sopenharmony_ci	transfer happens and its complete() callback is issued. The two
5988c2ecf20Sopenharmony_ci	will normally happen later, after other transfers complete, and
5998c2ecf20Sopenharmony_ci	if the controller is idle it will need to be kickstarted. This
6008c2ecf20Sopenharmony_ci	method is not used on queued controllers and must be NULL if
6018c2ecf20Sopenharmony_ci	transfer_one_message() and (un)prepare_transfer_hardware() are
6028c2ecf20Sopenharmony_ci	implemented.
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ciSPI Message Queue
6068c2ecf20Sopenharmony_ci^^^^^^^^^^^^^^^^^
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ciIf you are happy with the standard queueing mechanism provided by the
6098c2ecf20Sopenharmony_ciSPI subsystem, just implement the queued methods specified above. Using
6108c2ecf20Sopenharmony_cithe message queue has the upside of centralizing a lot of code and
6118c2ecf20Sopenharmony_ciproviding pure process-context execution of methods. The message queue
6128c2ecf20Sopenharmony_cican also be elevated to realtime priority on high-priority SPI traffic.
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ciUnless the queueing mechanism in the SPI subsystem is selected, the bulk
6158c2ecf20Sopenharmony_ciof the driver will be managing the I/O queue fed by the now deprecated
6168c2ecf20Sopenharmony_cifunction transfer().
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ciThat queue could be purely conceptual.  For example, a driver used only
6198c2ecf20Sopenharmony_cifor low-frequency sensor access might be fine using synchronous PIO.
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_ciBut the queue will probably be very real, using message->queue, PIO,
6228c2ecf20Sopenharmony_cioften DMA (especially if the root filesystem is in SPI flash), and
6238c2ecf20Sopenharmony_ciexecution contexts like IRQ handlers, tasklets, or workqueues (such
6248c2ecf20Sopenharmony_cias keventd).  Your driver can be as fancy, or as simple, as you need.
6258c2ecf20Sopenharmony_ciSuch a transfer() method would normally just add the message to a
6268c2ecf20Sopenharmony_ciqueue, and then start some asynchronous transfer engine (unless it's
6278c2ecf20Sopenharmony_cialready running).
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ciTHANKS TO
6318c2ecf20Sopenharmony_ci---------
6328c2ecf20Sopenharmony_ciContributors to Linux-SPI discussions include (in alphabetical order,
6338c2ecf20Sopenharmony_ciby last name):
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci- Mark Brown
6368c2ecf20Sopenharmony_ci- David Brownell
6378c2ecf20Sopenharmony_ci- Russell King
6388c2ecf20Sopenharmony_ci- Grant Likely
6398c2ecf20Sopenharmony_ci- Dmitry Pervushin
6408c2ecf20Sopenharmony_ci- Stephen Street
6418c2ecf20Sopenharmony_ci- Mark Underwood
6428c2ecf20Sopenharmony_ci- Andrew Victor
6438c2ecf20Sopenharmony_ci- Linus Walleij
6448c2ecf20Sopenharmony_ci- Vitaly Wool
645