Commit | Line | Data |
---|---|---|
8ae12a0d DB |
1 | Overview of Linux kernel SPI support |
2 | ==================================== | |
3 | ||
b885244e | 4 | 02-Dec-2005 |
8ae12a0d DB |
5 | |
6 | What is SPI? | |
7 | ------------ | |
b885244e DB |
8 | The "Serial Peripheral Interface" (SPI) is a synchronous four wire serial |
9 | link used to connect microcontrollers to sensors, memory, and peripherals. | |
8ae12a0d | 10 | |
33e34dc6 | 11 | The three signal wires hold a clock (SCK, often on the order of 10 MHz), |
8ae12a0d DB |
12 | and parallel data lines with "Master Out, Slave In" (MOSI) or "Master In, |
13 | Slave Out" (MISO) signals. (Other names are also used.) There are four | |
14 | clocking modes through which data is exchanged; mode-0 and mode-3 are most | |
b885244e DB |
15 | commonly used. Each clock cycle shifts data out and data in; the clock |
16 | doesn't cycle except when there is data to shift. | |
8ae12a0d DB |
17 | |
18 | SPI masters may use a "chip select" line to activate a given SPI slave | |
19 | device, so those three signal wires may be connected to several chips | |
20 | in parallel. All SPI slaves support chipselects. Some devices have | |
21 | other signals, often including an interrupt to the master. | |
22 | ||
23 | Unlike serial busses like USB or SMBUS, even low level protocols for | |
24 | SPI slave functions are usually not interoperable between vendors | |
33e34dc6 | 25 | (except for commodities like SPI memory chips). |
8ae12a0d DB |
26 | |
27 | - SPI may be used for request/response style device protocols, as with | |
28 | touchscreen sensors and memory chips. | |
29 | ||
30 | - It may also be used to stream data in either direction (half duplex), | |
31 | or both of them at the same time (full duplex). | |
32 | ||
33 | - Some devices may use eight bit words. Others may different word | |
34 | lengths, such as streams of 12-bit or 20-bit digital samples. | |
35 | ||
36 | In the same way, SPI slaves will only rarely support any kind of automatic | |
37 | discovery/enumeration protocol. The tree of slave devices accessible from | |
38 | a given SPI master will normally be set up manually, with configuration | |
39 | tables. | |
40 | ||
41 | SPI is only one of the names used by such four-wire protocols, and | |
42 | most controllers have no problem handling "MicroWire" (think of it as | |
43 | half-duplex SPI, for request/response protocols), SSP ("Synchronous | |
44 | Serial Protocol"), PSP ("Programmable Serial Protocol"), and other | |
45 | related protocols. | |
46 | ||
47 | Microcontrollers often support both master and slave sides of the SPI | |
48 | protocol. This document (and Linux) currently only supports the master | |
49 | side of SPI interactions. | |
50 | ||
51 | ||
52 | Who uses it? On what kinds of systems? | |
53 | --------------------------------------- | |
54 | Linux developers using SPI are probably writing device drivers for embedded | |
55 | systems boards. SPI is used to control external chips, and it is also a | |
56 | protocol supported by every MMC or SD memory card. (The older "DataFlash" | |
57 | cards, predating MMC cards but using the same connectors and card shape, | |
58 | support only SPI.) Some PC hardware uses SPI flash for BIOS code. | |
59 | ||
60 | SPI slave chips range from digital/analog converters used for analog | |
61 | sensors and codecs, to memory, to peripherals like USB controllers | |
62 | or Ethernet adapters; and more. | |
63 | ||
64 | Most systems using SPI will integrate a few devices on a mainboard. | |
65 | Some provide SPI links on expansion connectors; in cases where no | |
66 | dedicated SPI controller exists, GPIO pins can be used to create a | |
67 | low speed "bitbanging" adapter. Very few systems will "hotplug" an SPI | |
68 | controller; the reasons to use SPI focus on low cost and simple operation, | |
69 | and if dynamic reconfiguration is important, USB will often be a more | |
70 | appropriate low-pincount peripheral bus. | |
71 | ||
72 | Many microcontrollers that can run Linux integrate one or more I/O | |
73 | interfaces with SPI modes. Given SPI support, they could use MMC or SD | |
74 | cards without needing a special purpose MMC/SD/SDIO controller. | |
75 | ||
76 | ||
77 | How do these driver programming interfaces work? | |
78 | ------------------------------------------------ | |
79 | The <linux/spi/spi.h> header file includes kerneldoc, as does the | |
33e34dc6 DB |
80 | main source code, and you should certainly read that chapter of the |
81 | kernel API document. This is just an overview, so you get the big | |
82 | picture before those details. | |
8ae12a0d | 83 | |
b885244e DB |
84 | SPI requests always go into I/O queues. Requests for a given SPI device |
85 | are always executed in FIFO order, and complete asynchronously through | |
86 | completion callbacks. There are also some simple synchronous wrappers | |
87 | for those calls, including ones for common transaction types like writing | |
88 | a command and then reading its response. | |
89 | ||
8ae12a0d DB |
90 | There are two types of SPI driver, here called: |
91 | ||
33e34dc6 | 92 | Controller drivers ... controllers may be built in to System-On-Chip |
8ae12a0d DB |
93 | processors, and often support both Master and Slave roles. |
94 | These drivers touch hardware registers and may use DMA. | |
b885244e | 95 | Or they can be PIO bitbangers, needing just GPIO pins. |
8ae12a0d DB |
96 | |
97 | Protocol drivers ... these pass messages through the controller | |
98 | driver to communicate with a Slave or Master device on the | |
99 | other side of an SPI link. | |
100 | ||
101 | So for example one protocol driver might talk to the MTD layer to export | |
102 | data to filesystems stored on SPI flash like DataFlash; and others might | |
103 | control audio interfaces, present touchscreen sensors as input interfaces, | |
104 | or monitor temperature and voltage levels during industrial processing. | |
105 | And those might all be sharing the same controller driver. | |
106 | ||
107 | A "struct spi_device" encapsulates the master-side interface between | |
108 | those two types of driver. At this writing, Linux has no slave side | |
109 | programming interface. | |
110 | ||
111 | There is a minimal core of SPI programming interfaces, focussing on | |
33e34dc6 | 112 | using the driver model to connect controller and protocol drivers using |
8ae12a0d DB |
113 | device tables provided by board specific initialization code. SPI |
114 | shows up in sysfs in several locations: | |
115 | ||
33e34dc6 | 116 | /sys/devices/.../CTLR/spiB.C ... spi_device on bus "B", |
8ae12a0d DB |
117 | chipselect C, accessed through CTLR. |
118 | ||
7111763d DB |
119 | /sys/devices/.../CTLR/spiB.C/modalias ... identifies the driver |
120 | that should be used with this device (for hotplug/coldplug) | |
121 | ||
8ae12a0d | 122 | /sys/bus/spi/devices/spiB.C ... symlink to the physical |
33e34dc6 | 123 | spiB.C device |
8ae12a0d DB |
124 | |
125 | /sys/bus/spi/drivers/D ... driver for one or more spi*.* devices | |
126 | ||
127 | /sys/class/spi_master/spiB ... class device for the controller | |
128 | managing bus "B". All the spiB.* devices share the same | |
129 | physical SPI bus segment, with SCLK, MOSI, and MISO. | |
130 | ||
8ae12a0d DB |
131 | |
132 | How does board-specific init code declare SPI devices? | |
133 | ------------------------------------------------------ | |
134 | Linux needs several kinds of information to properly configure SPI devices. | |
135 | That information is normally provided by board-specific code, even for | |
136 | chips that do support some of automated discovery/enumeration. | |
137 | ||
138 | DECLARE CONTROLLERS | |
139 | ||
140 | The first kind of information is a list of what SPI controllers exist. | |
141 | For System-on-Chip (SOC) based boards, these will usually be platform | |
142 | devices, and the controller may need some platform_data in order to | |
143 | operate properly. The "struct platform_device" will include resources | |
144 | like the physical address of the controller's first register and its IRQ. | |
145 | ||
146 | Platforms will often abstract the "register SPI controller" operation, | |
147 | maybe coupling it with code to initialize pin configurations, so that | |
148 | the arch/.../mach-*/board-*.c files for several boards can all share the | |
149 | same basic controller setup code. This is because most SOCs have several | |
150 | SPI-capable controllers, and only the ones actually usable on a given | |
151 | board should normally be set up and registered. | |
152 | ||
153 | So for example arch/.../mach-*/board-*.c files might have code like: | |
154 | ||
155 | #include <asm/arch/spi.h> /* for mysoc_spi_data */ | |
156 | ||
157 | /* if your mach-* infrastructure doesn't support kernels that can | |
158 | * run on multiple boards, pdata wouldn't benefit from "__init". | |
159 | */ | |
160 | static struct mysoc_spi_data __init pdata = { ... }; | |
161 | ||
162 | static __init board_init(void) | |
163 | { | |
164 | ... | |
165 | /* this board only uses SPI controller #2 */ | |
166 | mysoc_register_spi(2, &pdata); | |
167 | ... | |
168 | } | |
169 | ||
170 | And SOC-specific utility code might look something like: | |
171 | ||
172 | #include <asm/arch/spi.h> | |
173 | ||
174 | static struct platform_device spi2 = { ... }; | |
175 | ||
176 | void mysoc_register_spi(unsigned n, struct mysoc_spi_data *pdata) | |
177 | { | |
178 | struct mysoc_spi_data *pdata2; | |
179 | ||
180 | pdata2 = kmalloc(sizeof *pdata2, GFP_KERNEL); | |
181 | *pdata2 = pdata; | |
182 | ... | |
183 | if (n == 2) { | |
184 | spi2->dev.platform_data = pdata2; | |
185 | register_platform_device(&spi2); | |
186 | ||
187 | /* also: set up pin modes so the spi2 signals are | |
188 | * visible on the relevant pins ... bootloaders on | |
189 | * production boards may already have done this, but | |
190 | * developer boards will often need Linux to do it. | |
191 | */ | |
192 | } | |
193 | ... | |
194 | } | |
195 | ||
196 | Notice how the platform_data for boards may be different, even if the | |
197 | same SOC controller is used. For example, on one board SPI might use | |
198 | an external clock, where another derives the SPI clock from current | |
199 | settings of some master clock. | |
200 | ||
201 | ||
202 | DECLARE SLAVE DEVICES | |
203 | ||
204 | The second kind of information is a list of what SPI slave devices exist | |
205 | on the target board, often with some board-specific data needed for the | |
206 | driver to work correctly. | |
207 | ||
208 | Normally your arch/.../mach-*/board-*.c files would provide a small table | |
209 | listing the SPI devices on each board. (This would typically be only a | |
210 | small handful.) That might look like: | |
211 | ||
212 | static struct ads7846_platform_data ads_info = { | |
213 | .vref_delay_usecs = 100, | |
214 | .x_plate_ohms = 580, | |
215 | .y_plate_ohms = 410, | |
216 | }; | |
217 | ||
218 | static struct spi_board_info spi_board_info[] __initdata = { | |
219 | { | |
220 | .modalias = "ads7846", | |
221 | .platform_data = &ads_info, | |
222 | .mode = SPI_MODE_0, | |
223 | .irq = GPIO_IRQ(31), | |
224 | .max_speed_hz = 120000 /* max sample rate at 3V */ * 16, | |
225 | .bus_num = 1, | |
226 | .chip_select = 0, | |
227 | }, | |
228 | }; | |
229 | ||
230 | Again, notice how board-specific information is provided; each chip may need | |
231 | several types. This example shows generic constraints like the fastest SPI | |
232 | clock to allow (a function of board voltage in this case) or how an IRQ pin | |
233 | is wired, plus chip-specific constraints like an important delay that's | |
234 | changed by the capacitance at one pin. | |
235 | ||
236 | (There's also "controller_data", information that may be useful to the | |
237 | controller driver. An example would be peripheral-specific DMA tuning | |
238 | data or chipselect callbacks. This is stored in spi_device later.) | |
239 | ||
240 | The board_info should provide enough information to let the system work | |
241 | without the chip's driver being loaded. The most troublesome aspect of | |
242 | that is likely the SPI_CS_HIGH bit in the spi_device.mode field, since | |
243 | sharing a bus with a device that interprets chipselect "backwards" is | |
33e34dc6 | 244 | not possible until the infrastructure knows how to deselect it. |
8ae12a0d DB |
245 | |
246 | Then your board initialization code would register that table with the SPI | |
247 | infrastructure, so that it's available later when the SPI master controller | |
248 | driver is registered: | |
249 | ||
250 | spi_register_board_info(spi_board_info, ARRAY_SIZE(spi_board_info)); | |
251 | ||
252 | Like with other static board-specific setup, you won't unregister those. | |
253 | ||
7111763d DB |
254 | The widely used "card" style computers bundle memory, cpu, and little else |
255 | onto a card that's maybe just thirty square centimeters. On such systems, | |
256 | your arch/.../mach-.../board-*.c file would primarily provide information | |
257 | about the devices on the mainboard into which such a card is plugged. That | |
258 | certainly includes SPI devices hooked up through the card connectors! | |
259 | ||
8ae12a0d DB |
260 | |
261 | NON-STATIC CONFIGURATIONS | |
262 | ||
263 | Developer boards often play by different rules than product boards, and one | |
264 | example is the potential need to hotplug SPI devices and/or controllers. | |
265 | ||
670e9f34 | 266 | For those cases you might need to use spi_busnum_to_master() to look |
8ae12a0d DB |
267 | up the spi bus master, and will likely need spi_new_device() to provide the |
268 | board info based on the board that was hotplugged. Of course, you'd later | |
269 | call at least spi_unregister_device() when that board is removed. | |
270 | ||
7111763d | 271 | When Linux includes support for MMC/SD/SDIO/DataFlash cards through SPI, those |
33e34dc6 DB |
272 | configurations will also be dynamic. Fortunately, such devices all support |
273 | basic device identification probes, so they should hotplug normally. | |
7111763d | 274 | |
8ae12a0d DB |
275 | |
276 | How do I write an "SPI Protocol Driver"? | |
277 | ---------------------------------------- | |
33e34dc6 DB |
278 | Most SPI drivers are currently kernel drivers, but there's also support |
279 | for userspace drivers. Here we talk only about kernel drivers. | |
8ae12a0d | 280 | |
b885244e DB |
281 | SPI protocol drivers somewhat resemble platform device drivers: |
282 | ||
283 | static struct spi_driver CHIP_driver = { | |
284 | .driver = { | |
285 | .name = "CHIP", | |
b885244e DB |
286 | .owner = THIS_MODULE, |
287 | }, | |
8ae12a0d | 288 | |
8ae12a0d | 289 | .probe = CHIP_probe, |
b885244e | 290 | .remove = __devexit_p(CHIP_remove), |
8ae12a0d DB |
291 | .suspend = CHIP_suspend, |
292 | .resume = CHIP_resume, | |
293 | }; | |
294 | ||
b885244e | 295 | The driver core will autmatically attempt to bind this driver to any SPI |
8ae12a0d DB |
296 | device whose board_info gave a modalias of "CHIP". Your probe() code |
297 | might look like this unless you're creating a class_device: | |
298 | ||
b885244e | 299 | static int __devinit CHIP_probe(struct spi_device *spi) |
8ae12a0d | 300 | { |
8ae12a0d | 301 | struct CHIP *chip; |
b885244e DB |
302 | struct CHIP_platform_data *pdata; |
303 | ||
304 | /* assuming the driver requires board-specific data: */ | |
305 | pdata = &spi->dev.platform_data; | |
306 | if (!pdata) | |
307 | return -ENODEV; | |
8ae12a0d DB |
308 | |
309 | /* get memory for driver's per-chip state */ | |
310 | chip = kzalloc(sizeof *chip, GFP_KERNEL); | |
311 | if (!chip) | |
312 | return -ENOMEM; | |
9b40ff4d | 313 | spi_set_drvdata(spi, chip); |
8ae12a0d DB |
314 | |
315 | ... etc | |
316 | return 0; | |
317 | } | |
318 | ||
319 | As soon as it enters probe(), the driver may issue I/O requests to | |
320 | the SPI device using "struct spi_message". When remove() returns, | |
33e34dc6 DB |
321 | or after probe() fails, the driver guarantees that it won't submit |
322 | any more such messages. | |
8ae12a0d | 323 | |
670e9f34 | 324 | - An spi_message is a sequence of protocol operations, executed |
8ae12a0d DB |
325 | as one atomic sequence. SPI driver controls include: |
326 | ||
327 | + when bidirectional reads and writes start ... by how its | |
328 | sequence of spi_transfer requests is arranged; | |
329 | ||
330 | + optionally defining short delays after transfers ... using | |
331 | the spi_transfer.delay_usecs setting; | |
332 | ||
333 | + whether the chipselect becomes inactive after a transfer and | |
334 | any delay ... by using the spi_transfer.cs_change flag; | |
335 | ||
336 | + hinting whether the next message is likely to go to this same | |
337 | device ... using the spi_transfer.cs_change flag on the last | |
338 | transfer in that atomic group, and potentially saving costs | |
339 | for chip deselect and select operations. | |
340 | ||
341 | - Follow standard kernel rules, and provide DMA-safe buffers in | |
342 | your messages. That way controller drivers using DMA aren't forced | |
343 | to make extra copies unless the hardware requires it (e.g. working | |
344 | around hardware errata that force the use of bounce buffering). | |
345 | ||
346 | If standard dma_map_single() handling of these buffers is inappropriate, | |
347 | you can use spi_message.is_dma_mapped to tell the controller driver | |
348 | that you've already provided the relevant DMA addresses. | |
349 | ||
350 | - The basic I/O primitive is spi_async(). Async requests may be | |
351 | issued in any context (irq handler, task, etc) and completion | |
352 | is reported using a callback provided with the message. | |
b885244e DB |
353 | After any detected error, the chip is deselected and processing |
354 | of that spi_message is aborted. | |
8ae12a0d DB |
355 | |
356 | - There are also synchronous wrappers like spi_sync(), and wrappers | |
357 | like spi_read(), spi_write(), and spi_write_then_read(). These | |
358 | may be issued only in contexts that may sleep, and they're all | |
359 | clean (and small, and "optional") layers over spi_async(). | |
360 | ||
361 | - The spi_write_then_read() call, and convenience wrappers around | |
362 | it, should only be used with small amounts of data where the | |
363 | cost of an extra copy may be ignored. It's designed to support | |
364 | common RPC-style requests, such as writing an eight bit command | |
365 | and reading a sixteen bit response -- spi_w8r16() being one its | |
366 | wrappers, doing exactly that. | |
367 | ||
368 | Some drivers may need to modify spi_device characteristics like the | |
369 | transfer mode, wordsize, or clock rate. This is done with spi_setup(), | |
370 | which would normally be called from probe() before the first I/O is | |
33e34dc6 DB |
371 | done to the device. However, that can also be called at any time |
372 | that no message is pending for that device. | |
8ae12a0d DB |
373 | |
374 | While "spi_device" would be the bottom boundary of the driver, the | |
375 | upper boundaries might include sysfs (especially for sensor readings), | |
376 | the input layer, ALSA, networking, MTD, the character device framework, | |
377 | or other Linux subsystems. | |
378 | ||
0c868461 DB |
379 | Note that there are two types of memory your driver must manage as part |
380 | of interacting with SPI devices. | |
381 | ||
382 | - I/O buffers use the usual Linux rules, and must be DMA-safe. | |
383 | You'd normally allocate them from the heap or free page pool. | |
384 | Don't use the stack, or anything that's declared "static". | |
385 | ||
386 | - The spi_message and spi_transfer metadata used to glue those | |
387 | I/O buffers into a group of protocol transactions. These can | |
388 | be allocated anywhere it's convenient, including as part of | |
389 | other allocate-once driver data structures. Zero-init these. | |
390 | ||
391 | If you like, spi_message_alloc() and spi_message_free() convenience | |
392 | routines are available to allocate and zero-initialize an spi_message | |
393 | with several transfers. | |
394 | ||
8ae12a0d DB |
395 | |
396 | How do I write an "SPI Master Controller Driver"? | |
397 | ------------------------------------------------- | |
398 | An SPI controller will probably be registered on the platform_bus; write | |
399 | a driver to bind to the device, whichever bus is involved. | |
400 | ||
401 | The main task of this type of driver is to provide an "spi_master". | |
402 | Use spi_alloc_master() to allocate the master, and class_get_devdata() | |
403 | to get the driver-private data allocated for that device. | |
404 | ||
405 | struct spi_master *master; | |
406 | struct CONTROLLER *c; | |
407 | ||
408 | master = spi_alloc_master(dev, sizeof *c); | |
409 | if (!master) | |
410 | return -ENODEV; | |
411 | ||
412 | c = class_get_devdata(&master->cdev); | |
413 | ||
414 | The driver will initialize the fields of that spi_master, including the | |
415 | bus number (maybe the same as the platform device ID) and three methods | |
416 | used to interact with the SPI core and SPI protocol drivers. It will | |
a020ed75 DB |
417 | also initialize its own internal state. (See below about bus numbering |
418 | and those methods.) | |
419 | ||
420 | After you initialize the spi_master, then use spi_register_master() to | |
421 | publish it to the rest of the system. At that time, device nodes for | |
422 | the controller and any predeclared spi devices will be made available, | |
423 | and the driver model core will take care of binding them to drivers. | |
424 | ||
425 | If you need to remove your SPI controller driver, spi_unregister_master() | |
426 | will reverse the effect of spi_register_master(). | |
427 | ||
428 | ||
429 | BUS NUMBERING | |
430 | ||
431 | Bus numbering is important, since that's how Linux identifies a given | |
432 | SPI bus (shared SCK, MOSI, MISO). Valid bus numbers start at zero. On | |
433 | SOC systems, the bus numbers should match the numbers defined by the chip | |
434 | manufacturer. For example, hardware controller SPI2 would be bus number 2, | |
435 | and spi_board_info for devices connected to it would use that number. | |
436 | ||
437 | If you don't have such hardware-assigned bus number, and for some reason | |
438 | you can't just assign them, then provide a negative bus number. That will | |
439 | then be replaced by a dynamically assigned number. You'd then need to treat | |
440 | this as a non-static configuration (see above). | |
441 | ||
442 | ||
443 | SPI MASTER METHODS | |
8ae12a0d DB |
444 | |
445 | master->setup(struct spi_device *spi) | |
446 | This sets up the device clock rate, SPI mode, and word sizes. | |
447 | Drivers may change the defaults provided by board_info, and then | |
448 | call spi_setup(spi) to invoke this routine. It may sleep. | |
33e34dc6 DB |
449 | Unless each SPI slave has its own configuration registers, don't |
450 | change them right away ... otherwise drivers could corrupt I/O | |
451 | that's in progress for other SPI devices. | |
8ae12a0d DB |
452 | |
453 | master->transfer(struct spi_device *spi, struct spi_message *message) | |
454 | This must not sleep. Its responsibility is arrange that the | |
33e34dc6 DB |
455 | transfer happens and its complete() callback is issued. The two |
456 | will normally happen later, after other transfers complete, and | |
457 | if the controller is idle it will need to be kickstarted. | |
8ae12a0d DB |
458 | |
459 | master->cleanup(struct spi_device *spi) | |
460 | Your controller driver may use spi_device.controller_state to hold | |
461 | state it dynamically associates with that device. If you do that, | |
462 | be sure to provide the cleanup() method to free that state. | |
463 | ||
a020ed75 DB |
464 | |
465 | SPI MESSAGE QUEUE | |
466 | ||
8ae12a0d DB |
467 | The bulk of the driver will be managing the I/O queue fed by transfer(). |
468 | ||
469 | That queue could be purely conceptual. For example, a driver used only | |
470 | for low-frequency sensor acess might be fine using synchronous PIO. | |
471 | ||
472 | But the queue will probably be very real, using message->queue, PIO, | |
473 | often DMA (especially if the root filesystem is in SPI flash), and | |
474 | execution contexts like IRQ handlers, tasklets, or workqueues (such | |
475 | as keventd). Your driver can be as fancy, or as simple, as you need. | |
a020ed75 DB |
476 | Such a transfer() method would normally just add the message to a |
477 | queue, and then start some asynchronous transfer engine (unless it's | |
478 | already running). | |
8ae12a0d DB |
479 | |
480 | ||
481 | THANKS TO | |
482 | --------- | |
483 | Contributors to Linux-SPI discussions include (in alphabetical order, | |
484 | by last name): | |
485 | ||
486 | David Brownell | |
487 | Russell King | |
488 | Dmitry Pervushin | |
489 | Stephen Street | |
490 | Mark Underwood | |
491 | Andrew Victor | |
492 | Vitaly Wool | |
493 |