aboutsummaryrefslogtreecommitdiffstats
path: root/include/linux/spi/spi.h
diff options
context:
space:
mode:
authorDavid Brownell <david-b@pacbell.net>2006-01-08 16:34:19 -0500
committerGreg Kroah-Hartman <gregkh@suse.de>2006-01-13 19:29:54 -0500
commit8ae12a0d85987dc138f8c944cb78a92bf466cea0 (patch)
treeca032f25bb26f88cc35d68c6f8065143ce64a6a8 /include/linux/spi/spi.h
parent67daf5f11f06b9b15f8320de1d237ccc2e74fe43 (diff)
[PATCH] spi: simple SPI framework
This is the core of a small SPI framework, implementing the model of a queue of messages which complete asynchronously (with thin synchronous wrappers on top). - It's still less than 2KB of ".text" (ARM). If there's got to be a mid-layer for something so simple, that's the right size budget. :) - The guts use board-specific SPI device tables to build the driver model tree. (Hardware probing is rarely an option.) - This version of Kconfig includes no drivers. At this writing there are two known master controller drivers (PXA/SSP, OMAP MicroWire) and three protocol drivers (CS8415a, ADS7846, DataFlash) with LKML mentions of other drivers in development. - No userspace API. There are several implementations to compare. Implement them like any other driver, and bind them with sysfs. The changes from last version posted to LKML (on 11-Nov-2005) are minor, and include: - One bugfix (removes a FIXME), with the visible effect of making device names be "spiB.C" where B is the bus number and C is the chipselect. - The "caller provides DMA mappings" mechanism now has kerneldoc, for DMA drivers that want to be fancy. - Hey, the framework init can be subsys_init. Even though board init logic fires earlier, at arch_init ... since the framework init is for driver support, and the board init support uses static init. - Various additional spec/doc clarifications based on discussions with other folk. It adds a brief "thank you" at the end, for folk who've helped nudge this framework into existence. As I've said before, I think that "protocol tweaking" is the main support that this driver framework will need to evolve. From: Mark Underwood <basicmark@yahoo.com> Update the SPI framework to remove a potential priority inversion case by reverting to kmalloc if the pre-allocated DMA-safe buffer isn't available. Signed-off-by: David Brownell <dbrownell@users.sourceforge.net> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'include/linux/spi/spi.h')
-rw-r--r--include/linux/spi/spi.h542
1 files changed, 542 insertions, 0 deletions
diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h
new file mode 100644
index 000000000000..51a6769114df
--- /dev/null
+++ b/include/linux/spi/spi.h
@@ -0,0 +1,542 @@
1/*
2 * Copyright (C) 2005 David Brownell
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19#ifndef __LINUX_SPI_H
20#define __LINUX_SPI_H
21
22/*
23 * INTERFACES between SPI master drivers and infrastructure
24 * (There's no SPI slave support for Linux yet...)
25 *
26 * A "struct device_driver" for an spi_device uses "spi_bus_type" and
27 * needs no special API wrappers (much like platform_bus). These drivers
28 * are bound to devices based on their names (much like platform_bus),
29 * and are available in dev->driver.
30 */
31extern struct bus_type spi_bus_type;
32
33/**
34 * struct spi_device - Master side proxy for an SPI slave device
35 * @dev: Driver model representation of the device.
36 * @master: SPI controller used with the device.
37 * @max_speed_hz: Maximum clock rate to be used with this chip
38 * (on this board); may be changed by the device's driver.
39 * @chip-select: Chipselect, distinguishing chips handled by "master".
40 * @mode: The spi mode defines how data is clocked out and in.
41 * This may be changed by the device's driver.
42 * @bits_per_word: Data transfers involve one or more words; word sizes
43 * like eight or 12 bits are common. In-memory wordsizes are
44 * powers of two bytes (e.g. 20 bit samples use 32 bits).
45 * This may be changed by the device's driver.
46 * @irq: Negative, or the number passed to request_irq() to receive
47 * interrupts from this device.
48 * @controller_state: Controller's runtime state
49 * @controller_data: Static board-specific definitions for controller, such
50 * as FIFO initialization parameters; from board_info.controller_data
51 *
52 * An spi_device is used to interchange data between an SPI slave
53 * (usually a discrete chip) and CPU memory.
54 *
55 * In "dev", the platform_data is used to hold information about this
56 * device that's meaningful to the device's protocol driver, but not
57 * to its controller. One example might be an identifier for a chip
58 * variant with slightly different functionality.
59 */
60struct spi_device {
61 struct device dev;
62 struct spi_master *master;
63 u32 max_speed_hz;
64 u8 chip_select;
65 u8 mode;
66#define SPI_CPHA 0x01 /* clock phase */
67#define SPI_CPOL 0x02 /* clock polarity */
68#define SPI_MODE_0 (0|0)
69#define SPI_MODE_1 (0|SPI_CPHA)
70#define SPI_MODE_2 (SPI_CPOL|0)
71#define SPI_MODE_3 (SPI_CPOL|SPI_CPHA)
72#define SPI_CS_HIGH 0x04 /* chipselect active high? */
73 u8 bits_per_word;
74 int irq;
75 void *controller_state;
76 const void *controller_data;
77 const char *modalias;
78
79 // likely need more hooks for more protocol options affecting how
80 // the controller talks to its chips, like:
81 // - bit order (default is wordwise msb-first)
82 // - memory packing (12 bit samples into low bits, others zeroed)
83 // - priority
84 // - chipselect delays
85 // - ...
86};
87
88static inline struct spi_device *to_spi_device(struct device *dev)
89{
90 return container_of(dev, struct spi_device, dev);
91}
92
93/* most drivers won't need to care about device refcounting */
94static inline struct spi_device *spi_dev_get(struct spi_device *spi)
95{
96 return (spi && get_device(&spi->dev)) ? spi : NULL;
97}
98
99static inline void spi_dev_put(struct spi_device *spi)
100{
101 if (spi)
102 put_device(&spi->dev);
103}
104
105/* ctldata is for the bus_master driver's runtime state */
106static inline void *spi_get_ctldata(struct spi_device *spi)
107{
108 return spi->controller_state;
109}
110
111static inline void spi_set_ctldata(struct spi_device *spi, void *state)
112{
113 spi->controller_state = state;
114}
115
116
117struct spi_message;
118
119
120/**
121 * struct spi_master - interface to SPI master controller
122 * @cdev: class interface to this driver
123 * @bus_num: board-specific (and often SOC-specific) identifier for a
124 * given SPI controller.
125 * @num_chipselects: chipselects are used to distinguish individual
126 * SPI slaves, and are numbered from zero to num_chipselects.
127 * each slave has a chipselect signal, but it's common that not
128 * every chipselect is connected to a slave.
129 * @setup: updates the device mode and clocking records used by a
130 * device's SPI controller; protocol code may call this.
131 * @transfer: adds a message to the controller's transfer queue.
132 * @cleanup: frees controller-specific state
133 *
134 * Each SPI master controller can communicate with one or more spi_device
135 * children. These make a small bus, sharing MOSI, MISO and SCK signals
136 * but not chip select signals. Each device may be configured to use a
137 * different clock rate, since those shared signals are ignored unless
138 * the chip is selected.
139 *
140 * The driver for an SPI controller manages access to those devices through
141 * a queue of spi_message transactions, copyin data between CPU memory and
142 * an SPI slave device). For each such message it queues, it calls the
143 * message's completion function when the transaction completes.
144 */
145struct spi_master {
146 struct class_device cdev;
147
148 /* other than zero (== assign one dynamically), bus_num is fully
149 * board-specific. usually that simplifies to being SOC-specific.
150 * example: one SOC has three SPI controllers, numbered 1..3,
151 * and one board's schematics might show it using SPI-2. software
152 * would normally use bus_num=2 for that controller.
153 */
154 u16 bus_num;
155
156 /* chipselects will be integral to many controllers; some others
157 * might use board-specific GPIOs.
158 */
159 u16 num_chipselect;
160
161 /* setup mode and clock, etc (spi driver may call many times) */
162 int (*setup)(struct spi_device *spi);
163
164 /* bidirectional bulk transfers
165 *
166 * + The transfer() method may not sleep; its main role is
167 * just to add the message to the queue.
168 * + For now there's no remove-from-queue operation, or
169 * any other request management
170 * + To a given spi_device, message queueing is pure fifo
171 *
172 * + The master's main job is to process its message queue,
173 * selecting a chip then transferring data
174 * + If there are multiple spi_device children, the i/o queue
175 * arbitration algorithm is unspecified (round robin, fifo,
176 * priority, reservations, preemption, etc)
177 *
178 * + Chipselect stays active during the entire message
179 * (unless modified by spi_transfer.cs_change != 0).
180 * + The message transfers use clock and SPI mode parameters
181 * previously established by setup() for this device
182 */
183 int (*transfer)(struct spi_device *spi,
184 struct spi_message *mesg);
185
186 /* called on release() to free memory provided by spi_master */
187 void (*cleanup)(const struct spi_device *spi);
188};
189
190/* the spi driver core manages memory for the spi_master classdev */
191extern struct spi_master *
192spi_alloc_master(struct device *host, unsigned size);
193
194extern int spi_register_master(struct spi_master *master);
195extern void spi_unregister_master(struct spi_master *master);
196
197extern struct spi_master *spi_busnum_to_master(u16 busnum);
198
199/*---------------------------------------------------------------------------*/
200
201/*
202 * I/O INTERFACE between SPI controller and protocol drivers
203 *
204 * Protocol drivers use a queue of spi_messages, each transferring data
205 * between the controller and memory buffers.
206 *
207 * The spi_messages themselves consist of a series of read+write transfer
208 * segments. Those segments always read the same number of bits as they
209 * write; but one or the other is easily ignored by passing a null buffer
210 * pointer. (This is unlike most types of I/O API, because SPI hardware
211 * is full duplex.)
212 *
213 * NOTE: Allocation of spi_transfer and spi_message memory is entirely
214 * up to the protocol driver, which guarantees the integrity of both (as
215 * well as the data buffers) for as long as the message is queued.
216 */
217
218/**
219 * struct spi_transfer - a read/write buffer pair
220 * @tx_buf: data to be written (dma-safe address), or NULL
221 * @rx_buf: data to be read (dma-safe address), or NULL
222 * @tx_dma: DMA address of buffer, if spi_message.is_dma_mapped
223 * @rx_dma: DMA address of buffer, if spi_message.is_dma_mapped
224 * @len: size of rx and tx buffers (in bytes)
225 * @cs_change: affects chipselect after this transfer completes
226 * @delay_usecs: microseconds to delay after this transfer before
227 * (optionally) changing the chipselect status, then starting
228 * the next transfer or completing this spi_message.
229 *
230 * SPI transfers always write the same number of bytes as they read.
231 * Protocol drivers should always provide rx_buf and/or tx_buf.
232 * In some cases, they may also want to provide DMA addresses for
233 * the data being transferred; that may reduce overhead, when the
234 * underlying driver uses dma.
235 *
236 * All SPI transfers start with the relevant chipselect active. Drivers
237 * can change behavior of the chipselect after the transfer finishes
238 * (including any mandatory delay). The normal behavior is to leave it
239 * selected, except for the last transfer in a message. Setting cs_change
240 * allows two additional behavior options:
241 *
242 * (i) If the transfer isn't the last one in the message, this flag is
243 * used to make the chipselect briefly go inactive in the middle of the
244 * message. Toggling chipselect in this way may be needed to terminate
245 * a chip command, letting a single spi_message perform all of group of
246 * chip transactions together.
247 *
248 * (ii) When the transfer is the last one in the message, the chip may
249 * stay selected until the next transfer. This is purely a performance
250 * hint; the controller driver may need to select a different device
251 * for the next message.
252 */
253struct spi_transfer {
254 /* it's ok if tx_buf == rx_buf (right?)
255 * for MicroWire, one buffer must be null
256 * buffers must work with dma_*map_single() calls
257 */
258 const void *tx_buf;
259 void *rx_buf;
260 unsigned len;
261
262 dma_addr_t tx_dma;
263 dma_addr_t rx_dma;
264
265 unsigned cs_change:1;
266 u16 delay_usecs;
267};
268
269/**
270 * struct spi_message - one multi-segment SPI transaction
271 * @transfers: the segements of the transaction
272 * @n_transfer: how many segments
273 * @spi: SPI device to which the transaction is queued
274 * @is_dma_mapped: if true, the caller provided both dma and cpu virtual
275 * addresses for each transfer buffer
276 * @complete: called to report transaction completions
277 * @context: the argument to complete() when it's called
278 * @actual_length: how many bytes were transferd
279 * @status: zero for success, else negative errno
280 * @queue: for use by whichever driver currently owns the message
281 * @state: for use by whichever driver currently owns the message
282 */
283struct spi_message {
284 struct spi_transfer *transfers;
285 unsigned n_transfer;
286
287 struct spi_device *spi;
288
289 unsigned is_dma_mapped:1;
290
291 /* REVISIT: we might want a flag affecting the behavior of the
292 * last transfer ... allowing things like "read 16 bit length L"
293 * immediately followed by "read L bytes". Basically imposing
294 * a specific message scheduling algorithm.
295 *
296 * Some controller drivers (message-at-a-time queue processing)
297 * could provide that as their default scheduling algorithm. But
298 * others (with multi-message pipelines) would need a flag to
299 * tell them about such special cases.
300 */
301
302 /* completion is reported through a callback */
303 void FASTCALL((*complete)(void *context));
304 void *context;
305 unsigned actual_length;
306 int status;
307
308 /* for optional use by whatever driver currently owns the
309 * spi_message ... between calls to spi_async and then later
310 * complete(), that's the spi_master controller driver.
311 */
312 struct list_head queue;
313 void *state;
314};
315
316/**
317 * spi_setup -- setup SPI mode and clock rate
318 * @spi: the device whose settings are being modified
319 *
320 * SPI protocol drivers may need to update the transfer mode if the
321 * device doesn't work with the mode 0 default. They may likewise need
322 * to update clock rates or word sizes from initial values. This function
323 * changes those settings, and must be called from a context that can sleep.
324 */
325static inline int
326spi_setup(struct spi_device *spi)
327{
328 return spi->master->setup(spi);
329}
330
331
332/**
333 * spi_async -- asynchronous SPI transfer
334 * @spi: device with which data will be exchanged
335 * @message: describes the data transfers, including completion callback
336 *
337 * This call may be used in_irq and other contexts which can't sleep,
338 * as well as from task contexts which can sleep.
339 *
340 * The completion callback is invoked in a context which can't sleep.
341 * Before that invocation, the value of message->status is undefined.
342 * When the callback is issued, message->status holds either zero (to
343 * indicate complete success) or a negative error code.
344 *
345 * Note that although all messages to a spi_device are handled in
346 * FIFO order, messages may go to different devices in other orders.
347 * Some device might be higher priority, or have various "hard" access
348 * time requirements, for example.
349 */
350static inline int
351spi_async(struct spi_device *spi, struct spi_message *message)
352{
353 message->spi = spi;
354 return spi->master->transfer(spi, message);
355}
356
357/*---------------------------------------------------------------------------*/
358
359/* All these synchronous SPI transfer routines are utilities layered
360 * over the core async transfer primitive. Here, "synchronous" means
361 * they will sleep uninterruptibly until the async transfer completes.
362 */
363
364extern int spi_sync(struct spi_device *spi, struct spi_message *message);
365
366/**
367 * spi_write - SPI synchronous write
368 * @spi: device to which data will be written
369 * @buf: data buffer
370 * @len: data buffer size
371 *
372 * This writes the buffer and returns zero or a negative error code.
373 * Callable only from contexts that can sleep.
374 */
375static inline int
376spi_write(struct spi_device *spi, const u8 *buf, size_t len)
377{
378 struct spi_transfer t = {
379 .tx_buf = buf,
380 .rx_buf = NULL,
381 .len = len,
382 .cs_change = 0,
383 };
384 struct spi_message m = {
385 .transfers = &t,
386 .n_transfer = 1,
387 };
388
389 return spi_sync(spi, &m);
390}
391
392/**
393 * spi_read - SPI synchronous read
394 * @spi: device from which data will be read
395 * @buf: data buffer
396 * @len: data buffer size
397 *
398 * This writes the buffer and returns zero or a negative error code.
399 * Callable only from contexts that can sleep.
400 */
401static inline int
402spi_read(struct spi_device *spi, u8 *buf, size_t len)
403{
404 struct spi_transfer t = {
405 .tx_buf = NULL,
406 .rx_buf = buf,
407 .len = len,
408 .cs_change = 0,
409 };
410 struct spi_message m = {
411 .transfers = &t,
412 .n_transfer = 1,
413 };
414
415 return spi_sync(spi, &m);
416}
417
418extern int spi_write_then_read(struct spi_device *spi,
419 const u8 *txbuf, unsigned n_tx,
420 u8 *rxbuf, unsigned n_rx);
421
422/**
423 * spi_w8r8 - SPI synchronous 8 bit write followed by 8 bit read
424 * @spi: device with which data will be exchanged
425 * @cmd: command to be written before data is read back
426 *
427 * This returns the (unsigned) eight bit number returned by the
428 * device, or else a negative error code. Callable only from
429 * contexts that can sleep.
430 */
431static inline ssize_t spi_w8r8(struct spi_device *spi, u8 cmd)
432{
433 ssize_t status;
434 u8 result;
435
436 status = spi_write_then_read(spi, &cmd, 1, &result, 1);
437
438 /* return negative errno or unsigned value */
439 return (status < 0) ? status : result;
440}
441
442/**
443 * spi_w8r16 - SPI synchronous 8 bit write followed by 16 bit read
444 * @spi: device with which data will be exchanged
445 * @cmd: command to be written before data is read back
446 *
447 * This returns the (unsigned) sixteen bit number returned by the
448 * device, or else a negative error code. Callable only from
449 * contexts that can sleep.
450 *
451 * The number is returned in wire-order, which is at least sometimes
452 * big-endian.
453 */
454static inline ssize_t spi_w8r16(struct spi_device *spi, u8 cmd)
455{
456 ssize_t status;
457 u16 result;
458
459 status = spi_write_then_read(spi, &cmd, 1, (u8 *) &result, 2);
460
461 /* return negative errno or unsigned value */
462 return (status < 0) ? status : result;
463}
464
465/*---------------------------------------------------------------------------*/
466
467/*
468 * INTERFACE between board init code and SPI infrastructure.
469 *
470 * No SPI driver ever sees these SPI device table segments, but
471 * it's how the SPI core (or adapters that get hotplugged) grows
472 * the driver model tree.
473 *
474 * As a rule, SPI devices can't be probed. Instead, board init code
475 * provides a table listing the devices which are present, with enough
476 * information to bind and set up the device's driver. There's basic
477 * support for nonstatic configurations too; enough to handle adding
478 * parport adapters, or microcontrollers acting as USB-to-SPI bridges.
479 */
480
481/* board-specific information about each SPI device */
482struct spi_board_info {
483 /* the device name and module name are coupled, like platform_bus;
484 * "modalias" is normally the driver name.
485 *
486 * platform_data goes to spi_device.dev.platform_data,
487 * controller_data goes to spi_device.platform_data,
488 * irq is copied too
489 */
490 char modalias[KOBJ_NAME_LEN];
491 const void *platform_data;
492 const void *controller_data;
493 int irq;
494
495 /* slower signaling on noisy or low voltage boards */
496 u32 max_speed_hz;
497
498
499 /* bus_num is board specific and matches the bus_num of some
500 * spi_master that will probably be registered later.
501 *
502 * chip_select reflects how this chip is wired to that master;
503 * it's less than num_chipselect.
504 */
505 u16 bus_num;
506 u16 chip_select;
507
508 /* ... may need additional spi_device chip config data here.
509 * avoid stuff protocol drivers can set; but include stuff
510 * needed to behave without being bound to a driver:
511 * - chipselect polarity
512 * - quirks like clock rate mattering when not selected
513 */
514};
515
516#ifdef CONFIG_SPI
517extern int
518spi_register_board_info(struct spi_board_info const *info, unsigned n);
519#else
520/* board init code may ignore whether SPI is configured or not */
521static inline int
522spi_register_board_info(struct spi_board_info const *info, unsigned n)
523 { return 0; }
524#endif
525
526
527/* If you're hotplugging an adapter with devices (parport, usb, etc)
528 * use spi_new_device() to describe each device. You can also call
529 * spi_unregister_device() to get start making that device vanish,
530 * but normally that would be handled by spi_unregister_master().
531 */
532extern struct spi_device *
533spi_new_device(struct spi_master *, struct spi_board_info *);
534
535static inline void
536spi_unregister_device(struct spi_device *spi)
537{
538 if (spi)
539 device_unregister(&spi->dev);
540}
541
542#endif /* __LINUX_SPI_H */