spi.h 29.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Copyright (C) 2005 David Brownell
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#ifndef __LINUX_SPI_H
#define __LINUX_SPI_H

/*
23
 * INTERFACES between SPI master-side drivers and SPI infrastructure.
24 25 26 27 28 29 30 31 32 33
 * (There's no SPI slave support for Linux yet...)
 */
extern struct bus_type spi_bus_type;

/**
 * struct spi_device - Master side proxy for an SPI slave device
 * @dev: Driver model representation of the device.
 * @master: SPI controller used with the device.
 * @max_speed_hz: Maximum clock rate to be used with this chip
 *	(on this board); may be changed by the device's driver.
34
 *	The spi_transfer.speed_hz can override this for each transfer.
D
David Brownell 已提交
35
 * @chip_select: Chipselect, distinguishing chips handled by @master.
36 37
 * @mode: The spi mode defines how data is clocked out and in.
 *	This may be changed by the device's driver.
D
David Brownell 已提交
38 39 40
 *	The "active low" default for chipselect mode can be overridden
 *	(by specifying SPI_CS_HIGH) as can the "MSB first" default for
 *	each word in a transfer (by specifying SPI_LSB_FIRST).
41
 * @bits_per_word: Data transfers involve one or more words; word sizes
42
 *	like eight or 12 bits are common.  In-memory wordsizes are
43
 *	powers of two bytes (e.g. 20 bit samples use 32 bits).
44 45
 *	This may be changed by the device's driver, or left at the
 *	default (0) indicating protocol words are eight bit bytes.
46
 *	The spi_transfer.bits_per_word can override this for each transfer.
47
 * @irq: Negative, or the number passed to request_irq() to receive
48
 *	interrupts from this device.
49
 * @controller_state: Controller's runtime state
50
 * @controller_data: Board-specific definitions for controller, such as
51
 *	FIFO initialization parameters; from board_info.controller_data
D
David Brownell 已提交
52 53 54
 * @modalias: Name of the driver to use with this device, or an alias
 *	for that name.  This appears in the sysfs "modalias" attribute
 *	for driver coldplugging, and in uevents used for hotplugging
55
 *
D
David Brownell 已提交
56
 * A @spi_device is used to interchange data between an SPI slave
57 58
 * (usually a discrete chip) and CPU memory.
 *
D
David Brownell 已提交
59
 * In @dev, the platform_data is used to hold information about this
60 61
 * device that's meaningful to the device's protocol driver, but not
 * to its controller.  One example might be an identifier for a chip
D
David Brownell 已提交
62 63
 * variant with slightly different functionality; another might be
 * information about how this particular board wires the chip's pins.
64 65 66 67 68 69 70
 */
struct spi_device {
	struct device		dev;
	struct spi_master	*master;
	u32			max_speed_hz;
	u8			chip_select;
	u8			mode;
71 72
#define	SPI_CPHA	0x01			/* clock phase */
#define	SPI_CPOL	0x02			/* clock polarity */
D
David Brownell 已提交
73 74
#define	SPI_MODE_0	(0|0)			/* (original MicroWire) */
#define	SPI_MODE_1	(0|SPI_CPHA)
75 76
#define	SPI_MODE_2	(SPI_CPOL|0)
#define	SPI_MODE_3	(SPI_CPOL|SPI_CPHA)
77
#define	SPI_CS_HIGH	0x04			/* chipselect active high? */
78
#define	SPI_LSB_FIRST	0x08			/* per-word bits-on-wire */
D
David Brownell 已提交
79
#define	SPI_3WIRE	0x10			/* SI/SO signals shared */
A
Anton Vorontsov 已提交
80
#define	SPI_LOOP	0x20			/* loopback mode */
81 82 83
	u8			bits_per_word;
	int			irq;
	void			*controller_state;
84
	void			*controller_data;
85
	char			modalias[32];
86

D
David Brownell 已提交
87 88 89 90 91 92 93 94 95
	/*
	 * likely need more hooks for more protocol options affecting how
	 * the controller talks to each chip, like:
	 *  - memory packing (12 bit samples into low bits, others zeroed)
	 *  - priority
	 *  - drop chipselect after each word
	 *  - chipselect delays
	 *  - ...
	 */
96 97 98 99
};

static inline struct spi_device *to_spi_device(struct device *dev)
{
100
	return dev ? container_of(dev, struct spi_device, dev) : NULL;
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
}

/* most drivers won't need to care about device refcounting */
static inline struct spi_device *spi_dev_get(struct spi_device *spi)
{
	return (spi && get_device(&spi->dev)) ? spi : NULL;
}

static inline void spi_dev_put(struct spi_device *spi)
{
	if (spi)
		put_device(&spi->dev);
}

/* ctldata is for the bus_master driver's runtime state */
static inline void *spi_get_ctldata(struct spi_device *spi)
{
	return spi->controller_state;
}

static inline void spi_set_ctldata(struct spi_device *spi, void *state)
{
	spi->controller_state = state;
}

126 127 128 129 130 131 132 133 134 135 136
/* device driver data */

static inline void spi_set_drvdata(struct spi_device *spi, void *data)
{
	dev_set_drvdata(&spi->dev, data);
}

static inline void *spi_get_drvdata(struct spi_device *spi)
{
	return dev_get_drvdata(&spi->dev);
}
137 138 139 140

struct spi_message;


141

D
David Brownell 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
/**
 * struct spi_driver - Host side "protocol" driver
 * @probe: Binds this driver to the spi device.  Drivers can verify
 *	that the device is actually present, and may need to configure
 *	characteristics (such as bits_per_word) which weren't needed for
 *	the initial configuration done during system setup.
 * @remove: Unbinds this driver from the spi device
 * @shutdown: Standard shutdown callback used during system state
 *	transitions such as powerdown/halt and kexec
 * @suspend: Standard suspend callback used during system state transitions
 * @resume: Standard resume callback used during system state transitions
 * @driver: SPI device drivers should initialize the name and owner
 *	field of this structure.
 *
 * This represents the kind of device driver that uses SPI messages to
 * interact with the hardware at the other end of a SPI link.  It's called
 * a "protocol" driver because it works through messages rather than talking
 * directly to SPI hardware (which is what the underlying SPI controller
 * driver does to pass those messages).  These protocols are defined in the
 * specification for the device(s) supported by the driver.
 *
 * As a rule, those device protocols represent the lowest level interface
 * supported by a driver, and it will support upper level interfaces too.
 * Examples of such upper levels include frameworks like MTD, networking,
 * MMC, RTC, filesystem character device nodes, and hardware monitoring.
 */
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
struct spi_driver {
	int			(*probe)(struct spi_device *spi);
	int			(*remove)(struct spi_device *spi);
	void			(*shutdown)(struct spi_device *spi);
	int			(*suspend)(struct spi_device *spi, pm_message_t mesg);
	int			(*resume)(struct spi_device *spi);
	struct device_driver	driver;
};

static inline struct spi_driver *to_spi_driver(struct device_driver *drv)
{
	return drv ? container_of(drv, struct spi_driver, driver) : NULL;
}

extern int spi_register_driver(struct spi_driver *sdrv);

D
David Brownell 已提交
184 185 186 187 188
/**
 * spi_unregister_driver - reverse effect of spi_register_driver
 * @sdrv: the driver to unregister
 * Context: can sleep
 */
189 190
static inline void spi_unregister_driver(struct spi_driver *sdrv)
{
191 192
	if (sdrv)
		driver_unregister(&sdrv->driver);
193 194 195
}


196 197
/**
 * struct spi_master - interface to SPI master controller
T
Tony Jones 已提交
198
 * @dev: device interface to this driver
199
 * @bus_num: board-specific (and often SOC-specific) identifier for a
200
 *	given SPI controller.
201
 * @num_chipselect: chipselects are used to distinguish individual
202 203 204
 *	SPI slaves, and are numbered from zero to num_chipselects.
 *	each slave has a chipselect signal, but it's common that not
 *	every chipselect is connected to a slave.
205
 * @setup: updates the device mode and clocking records used by a
D
David Brownell 已提交
206 207
 *	device's SPI controller; protocol code may call this.  This
 *	must fail if an unrecognized or unsupported mode is requested.
D
David Brownell 已提交
208 209
 *	It's always safe to call this unless transfers are pending on
 *	the device whose settings are being modified.
210 211 212
 * @transfer: adds a message to the controller's transfer queue.
 * @cleanup: frees controller-specific state
 *
D
David Brownell 已提交
213
 * Each SPI master controller can communicate with one or more @spi_device
214 215 216 217 218 219
 * children.  These make a small bus, sharing MOSI, MISO and SCK signals
 * but not chip select signals.  Each device may be configured to use a
 * different clock rate, since those shared signals are ignored unless
 * the chip is selected.
 *
 * The driver for an SPI controller manages access to those devices through
D
David Brownell 已提交
220 221
 * a queue of spi_message transactions, copying data between CPU memory and
 * an SPI slave device.  For each such message it queues, it calls the
222 223 224
 * message's completion function when the transaction completes.
 */
struct spi_master {
T
Tony Jones 已提交
225
	struct device	dev;
226

227
	/* other than negative (== assign one dynamically), bus_num is fully
228
	 * board-specific.  usually that simplifies to being SOC-specific.
229
	 * example:  one SOC has three SPI controllers, numbered 0..2,
230 231 232
	 * and one board's schematics might show it using SPI-2.  software
	 * would normally use bus_num=2 for that controller.
	 */
233
	s16			bus_num;
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265

	/* chipselects will be integral to many controllers; some others
	 * might use board-specific GPIOs.
	 */
	u16			num_chipselect;

	/* setup mode and clock, etc (spi driver may call many times) */
	int			(*setup)(struct spi_device *spi);

	/* bidirectional bulk transfers
	 *
	 * + The transfer() method may not sleep; its main role is
	 *   just to add the message to the queue.
	 * + For now there's no remove-from-queue operation, or
	 *   any other request management
	 * + To a given spi_device, message queueing is pure fifo
	 *
	 * + The master's main job is to process its message queue,
	 *   selecting a chip then transferring data
	 * + If there are multiple spi_device children, the i/o queue
	 *   arbitration algorithm is unspecified (round robin, fifo,
	 *   priority, reservations, preemption, etc)
	 *
	 * + Chipselect stays active during the entire message
	 *   (unless modified by spi_transfer.cs_change != 0).
	 * + The message transfers use clock and SPI mode parameters
	 *   previously established by setup() for this device
	 */
	int			(*transfer)(struct spi_device *spi,
						struct spi_message *mesg);

	/* called on release() to free memory provided by spi_master */
266
	void			(*cleanup)(struct spi_device *spi);
267 268
};

D
David Brownell 已提交
269 270
static inline void *spi_master_get_devdata(struct spi_master *master)
{
T
Tony Jones 已提交
271
	return dev_get_drvdata(&master->dev);
D
David Brownell 已提交
272 273 274 275
}

static inline void spi_master_set_devdata(struct spi_master *master, void *data)
{
T
Tony Jones 已提交
276
	dev_set_drvdata(&master->dev, data);
D
David Brownell 已提交
277 278 279 280
}

static inline struct spi_master *spi_master_get(struct spi_master *master)
{
T
Tony Jones 已提交
281
	if (!master || !get_device(&master->dev))
D
David Brownell 已提交
282 283 284 285 286 287 288
		return NULL;
	return master;
}

static inline void spi_master_put(struct spi_master *master)
{
	if (master)
T
Tony Jones 已提交
289
		put_device(&master->dev);
D
David Brownell 已提交
290 291 292
}


293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
/* the spi driver core manages memory for the spi_master classdev */
extern struct spi_master *
spi_alloc_master(struct device *host, unsigned size);

extern int spi_register_master(struct spi_master *master);
extern void spi_unregister_master(struct spi_master *master);

extern struct spi_master *spi_busnum_to_master(u16 busnum);

/*---------------------------------------------------------------------------*/

/*
 * I/O INTERFACE between SPI controller and protocol drivers
 *
 * Protocol drivers use a queue of spi_messages, each transferring data
 * between the controller and memory buffers.
 *
 * The spi_messages themselves consist of a series of read+write transfer
 * segments.  Those segments always read the same number of bits as they
 * write; but one or the other is easily ignored by passing a null buffer
 * pointer.  (This is unlike most types of I/O API, because SPI hardware
 * is full duplex.)
 *
 * NOTE:  Allocation of spi_transfer and spi_message memory is entirely
 * up to the protocol driver, which guarantees the integrity of both (as
 * well as the data buffers) for as long as the message is queued.
 */

/**
 * struct spi_transfer - a read/write buffer pair
323 324
 * @tx_buf: data to be written (dma-safe memory), or NULL
 * @rx_buf: data to be read (dma-safe memory), or NULL
D
David Brownell 已提交
325 326
 * @tx_dma: DMA address of tx_buf, if @spi_message.is_dma_mapped
 * @rx_dma: DMA address of rx_buf, if @spi_message.is_dma_mapped
327
 * @len: size of rx and tx buffers (in bytes)
328
 * @speed_hz: Select a speed other then the device default for this
D
David Brownell 已提交
329
 *      transfer. If 0 the default (from @spi_device) is used.
330
 * @bits_per_word: select a bits_per_word other then the device default
D
David Brownell 已提交
331
 *      for this transfer. If 0 the default (from @spi_device) is used.
332 333
 * @cs_change: affects chipselect after this transfer completes
 * @delay_usecs: microseconds to delay after this transfer before
334
 *	(optionally) changing the chipselect status, then starting
D
David Brownell 已提交
335 336
 *	the next transfer or completing this @spi_message.
 * @transfer_list: transfers are sequenced through @spi_message.transfers
337 338
 *
 * SPI transfers always write the same number of bytes as they read.
D
David Brownell 已提交
339
 * Protocol drivers should always provide @rx_buf and/or @tx_buf.
340 341 342 343
 * In some cases, they may also want to provide DMA addresses for
 * the data being transferred; that may reduce overhead, when the
 * underlying driver uses dma.
 *
344
 * If the transmit buffer is null, zeroes will be shifted out
D
David Brownell 已提交
345
 * while filling @rx_buf.  If the receive buffer is null, the data
346 347 348 349 350
 * shifted in will be discarded.  Only "len" bytes shift out (or in).
 * It's an error to try to shift out a partial word.  (For example, by
 * shifting out three bytes with word size of sixteen or twenty bits;
 * the former uses two bytes per word, the latter uses four bytes.)
 *
D
David Brownell 已提交
351 352 353
 * In-memory data values are always in native CPU byte order, translated
 * from the wire byte order (big-endian except with SPI_LSB_FIRST).  So
 * for example when bits_per_word is sixteen, buffers are 2N bytes long
D
David Brownell 已提交
354
 * (@len = 2N) and hold N sixteen bit words in CPU byte order.
D
David Brownell 已提交
355 356 357 358 359 360
 *
 * When the word size of the SPI transfer is not a power-of-two multiple
 * of eight bits, those in-memory words include extra bits.  In-memory
 * words are always seen by protocol drivers as right-justified, so the
 * undefined (rx) or unused (tx) bits are always the most significant bits.
 *
361 362
 * All SPI transfers start with the relevant chipselect active.  Normally
 * it stays selected until after the last transfer in a message.  Drivers
D
David Brownell 已提交
363
 * can affect the chipselect signal using cs_change.
364 365 366 367 368 369 370 371
 *
 * (i) If the transfer isn't the last one in the message, this flag is
 * used to make the chipselect briefly go inactive in the middle of the
 * message.  Toggling chipselect in this way may be needed to terminate
 * a chip command, letting a single spi_message perform all of group of
 * chip transactions together.
 *
 * (ii) When the transfer is the last one in the message, the chip may
D
David Brownell 已提交
372 373 374 375 376 377 378 379
 * stay selected until the next transfer.  On multi-device SPI busses
 * with nothing blocking messages going to other devices, this is just
 * a performance hint; starting a message to another device deselects
 * this one.  But in other cases, this can be used to ensure correctness.
 * Some devices need protocol transactions to be built from a series of
 * spi_message submissions, where the content of one message is determined
 * by the results of previous messages and where the whole transaction
 * ends when the chipselect goes intactive.
D
David Brownell 已提交
380 381 382 383
 *
 * The code that submits an spi_message (and its spi_transfers)
 * to the lower layers is responsible for managing its memory.
 * Zero-initialize every field you don't set up explicitly, to
384 385
 * insulate against future API updates.  After you submit a message
 * and its transfers, ignore them until its completion callback.
386 387 388 389
 */
struct spi_transfer {
	/* it's ok if tx_buf == rx_buf (right?)
	 * for MicroWire, one buffer must be null
D
David Brownell 已提交
390 391
	 * buffers must work with dma_*map_single() calls, unless
	 *   spi_message.is_dma_mapped reports a pre-existing mapping
392 393 394 395 396 397 398 399 400
	 */
	const void	*tx_buf;
	void		*rx_buf;
	unsigned	len;

	dma_addr_t	tx_dma;
	dma_addr_t	rx_dma;

	unsigned	cs_change:1;
401
	u8		bits_per_word;
402
	u16		delay_usecs;
403
	u32		speed_hz;
404 405

	struct list_head transfer_list;
406 407 408 409
};

/**
 * struct spi_message - one multi-segment SPI transaction
410
 * @transfers: list of transfer segments in this transaction
411 412 413 414 415
 * @spi: SPI device to which the transaction is queued
 * @is_dma_mapped: if true, the caller provided both dma and cpu virtual
 *	addresses for each transfer buffer
 * @complete: called to report transaction completions
 * @context: the argument to complete() when it's called
416 417
 * @actual_length: the total number of bytes that were transferred in all
 *	successful segments
418 419 420
 * @status: zero for success, else negative errno
 * @queue: for use by whichever driver currently owns the message
 * @state: for use by whichever driver currently owns the message
D
David Brownell 已提交
421
 *
D
David Brownell 已提交
422
 * A @spi_message is used to execute an atomic sequence of data transfers,
423 424 425 426 427 428 429
 * each represented by a struct spi_transfer.  The sequence is "atomic"
 * in the sense that no other spi_message may use that SPI bus until that
 * sequence completes.  On some systems, many such sequences can execute as
 * as single programmed DMA transfer.  On all systems, these messages are
 * queued, and might complete after transactions to other devices.  Messages
 * sent to a given spi_device are alway executed in FIFO order.
 *
D
David Brownell 已提交
430 431 432
 * The code that submits an spi_message (and its spi_transfers)
 * to the lower layers is responsible for managing its memory.
 * Zero-initialize every field you don't set up explicitly, to
433 434
 * insulate against future API updates.  After you submit a message
 * and its transfers, ignore them until its completion callback.
435 436
 */
struct spi_message {
437
	struct list_head	transfers;
438 439 440 441 442 443 444 445 446 447 448 449

	struct spi_device	*spi;

	unsigned		is_dma_mapped:1;

	/* REVISIT:  we might want a flag affecting the behavior of the
	 * last transfer ... allowing things like "read 16 bit length L"
	 * immediately followed by "read L bytes".  Basically imposing
	 * a specific message scheduling algorithm.
	 *
	 * Some controller drivers (message-at-a-time queue processing)
	 * could provide that as their default scheduling algorithm.  But
450
	 * others (with multi-message pipelines) could need a flag to
451 452 453 454
	 * tell them about such special cases.
	 */

	/* completion is reported through a callback */
455
	void			(*complete)(void *context);
456 457 458 459 460 461 462 463 464 465 466 467
	void			*context;
	unsigned		actual_length;
	int			status;

	/* for optional use by whatever driver currently owns the
	 * spi_message ...  between calls to spi_async and then later
	 * complete(), that's the spi_master controller driver.
	 */
	struct list_head	queue;
	void			*state;
};

468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
static inline void spi_message_init(struct spi_message *m)
{
	memset(m, 0, sizeof *m);
	INIT_LIST_HEAD(&m->transfers);
}

static inline void
spi_message_add_tail(struct spi_transfer *t, struct spi_message *m)
{
	list_add_tail(&t->transfer_list, &m->transfers);
}

static inline void
spi_transfer_del(struct spi_transfer *t)
{
	list_del(&t->transfer_list);
}

D
David Brownell 已提交
486 487 488 489 490 491 492 493 494 495 496 497
/* It's fine to embed message and transaction structures in other data
 * structures so long as you don't free them while they're in use.
 */

static inline struct spi_message *spi_message_alloc(unsigned ntrans, gfp_t flags)
{
	struct spi_message *m;

	m = kzalloc(sizeof(struct spi_message)
			+ ntrans * sizeof(struct spi_transfer),
			flags);
	if (m) {
498 499 500 501 502 503
		int i;
		struct spi_transfer *t = (struct spi_transfer *)(m + 1);

		INIT_LIST_HEAD(&m->transfers);
		for (i = 0; i < ntrans; i++, t++)
			spi_message_add_tail(t, m);
D
David Brownell 已提交
504 505 506 507 508 509 510 511 512
	}
	return m;
}

static inline void spi_message_free(struct spi_message *m)
{
	kfree(m);
}

513
/**
D
David Brownell 已提交
514
 * spi_setup - setup SPI mode and clock rate
515
 * @spi: the device whose settings are being modified
D
David Brownell 已提交
516
 * Context: can sleep, and no requests are queued to the device
517 518
 *
 * SPI protocol drivers may need to update the transfer mode if the
D
David Brownell 已提交
519
 * device doesn't work with its default.  They may likewise need
520 521
 * to update clock rates or word sizes from initial values.  This function
 * changes those settings, and must be called from a context that can sleep.
D
David Brownell 已提交
522 523 524
 * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
 * effect the next time the device is selected and data is transferred to
 * or from it.  When this function returns, the spi device is deselected.
D
David Brownell 已提交
525
 *
D
David Brownell 已提交
526
 * Note that this call will fail if the protocol driver specifies an option
D
David Brownell 已提交
527 528 529
 * that the underlying controller or its driver does not support.  For
 * example, not all hardware supports wire transfers using nine bit words,
 * LSB-first wire encoding, or active-high chipselects.
530 531 532 533 534 535 536 537 538
 */
static inline int
spi_setup(struct spi_device *spi)
{
	return spi->master->setup(spi);
}


/**
D
David Brownell 已提交
539
 * spi_async - asynchronous SPI transfer
540 541
 * @spi: device with which data will be exchanged
 * @message: describes the data transfers, including completion callback
D
David Brownell 已提交
542
 * Context: any (irqs may be blocked, etc)
543 544 545 546 547 548 549
 *
 * This call may be used in_irq and other contexts which can't sleep,
 * as well as from task contexts which can sleep.
 *
 * The completion callback is invoked in a context which can't sleep.
 * Before that invocation, the value of message->status is undefined.
 * When the callback is issued, message->status holds either zero (to
D
David Brownell 已提交
550 551 552 553
 * indicate complete success) or a negative error code.  After that
 * callback returns, the driver which issued the transfer request may
 * deallocate the associated memory; it's no longer in use by any SPI
 * core or controller driver code.
554 555 556 557 558
 *
 * Note that although all messages to a spi_device are handled in
 * FIFO order, messages may go to different devices in other orders.
 * Some device might be higher priority, or have various "hard" access
 * time requirements, for example.
559 560 561 562 563 564 565
 *
 * On detection of any fault during the transfer, processing of
 * the entire message is aborted, and the device is deselected.
 * Until returning from the associated message completion callback,
 * no other spi_message queued to that device will be processed.
 * (This rule applies equally to all the synchronous transfer calls,
 * which are wrappers around this core asynchronous primitive.)
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
 */
static inline int
spi_async(struct spi_device *spi, struct spi_message *message)
{
	message->spi = spi;
	return spi->master->transfer(spi, message);
}

/*---------------------------------------------------------------------------*/

/* All these synchronous SPI transfer routines are utilities layered
 * over the core async transfer primitive.  Here, "synchronous" means
 * they will sleep uninterruptibly until the async transfer completes.
 */

extern int spi_sync(struct spi_device *spi, struct spi_message *message);

/**
 * spi_write - SPI synchronous write
 * @spi: device to which data will be written
 * @buf: data buffer
 * @len: data buffer size
D
David Brownell 已提交
588
 * Context: can sleep
589 590 591 592 593 594 595 596 597 598 599
 *
 * This writes the buffer and returns zero or a negative error code.
 * Callable only from contexts that can sleep.
 */
static inline int
spi_write(struct spi_device *spi, const u8 *buf, size_t len)
{
	struct spi_transfer	t = {
			.tx_buf		= buf,
			.len		= len,
		};
600
	struct spi_message	m;
601

602 603
	spi_message_init(&m);
	spi_message_add_tail(&t, &m);
604 605 606 607 608 609 610 611
	return spi_sync(spi, &m);
}

/**
 * spi_read - SPI synchronous read
 * @spi: device from which data will be read
 * @buf: data buffer
 * @len: data buffer size
D
David Brownell 已提交
612
 * Context: can sleep
613
 *
D
David Brownell 已提交
614
 * This reads the buffer and returns zero or a negative error code.
615 616 617 618 619 620 621 622 623
 * Callable only from contexts that can sleep.
 */
static inline int
spi_read(struct spi_device *spi, u8 *buf, size_t len)
{
	struct spi_transfer	t = {
			.rx_buf		= buf,
			.len		= len,
		};
624
	struct spi_message	m;
625

626 627
	spi_message_init(&m);
	spi_message_add_tail(&t, &m);
628 629 630
	return spi_sync(spi, &m);
}

D
David Brownell 已提交
631
/* this copies txbuf and rxbuf data; for small transfers only! */
632 633 634 635 636 637 638 639
extern int spi_write_then_read(struct spi_device *spi,
		const u8 *txbuf, unsigned n_tx,
		u8 *rxbuf, unsigned n_rx);

/**
 * spi_w8r8 - SPI synchronous 8 bit write followed by 8 bit read
 * @spi: device with which data will be exchanged
 * @cmd: command to be written before data is read back
D
David Brownell 已提交
640
 * Context: can sleep
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
 *
 * This returns the (unsigned) eight bit number returned by the
 * device, or else a negative error code.  Callable only from
 * contexts that can sleep.
 */
static inline ssize_t spi_w8r8(struct spi_device *spi, u8 cmd)
{
	ssize_t			status;
	u8			result;

	status = spi_write_then_read(spi, &cmd, 1, &result, 1);

	/* return negative errno or unsigned value */
	return (status < 0) ? status : result;
}

/**
 * spi_w8r16 - SPI synchronous 8 bit write followed by 16 bit read
 * @spi: device with which data will be exchanged
 * @cmd: command to be written before data is read back
D
David Brownell 已提交
661
 * Context: can sleep
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
 *
 * This returns the (unsigned) sixteen bit number returned by the
 * device, or else a negative error code.  Callable only from
 * contexts that can sleep.
 *
 * The number is returned in wire-order, which is at least sometimes
 * big-endian.
 */
static inline ssize_t spi_w8r16(struct spi_device *spi, u8 cmd)
{
	ssize_t			status;
	u16			result;

	status = spi_write_then_read(spi, &cmd, 1, (u8 *) &result, 2);

	/* return negative errno or unsigned value */
	return (status < 0) ? status : result;
}

/*---------------------------------------------------------------------------*/

/*
 * INTERFACE between board init code and SPI infrastructure.
 *
 * No SPI driver ever sees these SPI device table segments, but
 * it's how the SPI core (or adapters that get hotplugged) grows
 * the driver model tree.
 *
 * As a rule, SPI devices can't be probed.  Instead, board init code
 * provides a table listing the devices which are present, with enough
 * information to bind and set up the device's driver.  There's basic
 * support for nonstatic configurations too; enough to handle adding
 * parport adapters, or microcontrollers acting as USB-to-SPI bridges.
 */

D
David Brownell 已提交
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
/**
 * struct spi_board_info - board-specific template for a SPI device
 * @modalias: Initializes spi_device.modalias; identifies the driver.
 * @platform_data: Initializes spi_device.platform_data; the particular
 *	data stored there is driver-specific.
 * @controller_data: Initializes spi_device.controller_data; some
 *	controllers need hints about hardware setup, e.g. for DMA.
 * @irq: Initializes spi_device.irq; depends on how the board is wired.
 * @max_speed_hz: Initializes spi_device.max_speed_hz; based on limits
 *	from the chip datasheet and board-specific signal quality issues.
 * @bus_num: Identifies which spi_master parents the spi_device; unused
 *	by spi_new_device(), and otherwise depends on board wiring.
 * @chip_select: Initializes spi_device.chip_select; depends on how
 *	the board is wired.
 * @mode: Initializes spi_device.mode; based on the chip datasheet, board
 *	wiring (some devices support both 3WIRE and standard modes), and
 *	possibly presence of an inverter in the chipselect path.
 *
 * When adding new SPI devices to the device tree, these structures serve
 * as a partial device template.  They hold information which can't always
 * be determined by drivers.  Information that probe() can establish (such
 * as the default transfer wordsize) is not included here.
 *
 * These structures are used in two places.  Their primary role is to
 * be stored in tables of board-specific device descriptors, which are
 * declared early in board initialization and then used (much later) to
 * populate a controller's device tree after the that controller's driver
 * initializes.  A secondary (and atypical) role is as a parameter to
 * spi_new_device() call, which happens after those controller drivers
 * are active in some dynamic board configuration models.
 */
728 729 730 731 732
struct spi_board_info {
	/* the device name and module name are coupled, like platform_bus;
	 * "modalias" is normally the driver name.
	 *
	 * platform_data goes to spi_device.dev.platform_data,
733
	 * controller_data goes to spi_device.controller_data,
734 735
	 * irq is copied too
	 */
736
	char		modalias[32];
737
	const void	*platform_data;
738
	void		*controller_data;
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
	int		irq;

	/* slower signaling on noisy or low voltage boards */
	u32		max_speed_hz;


	/* bus_num is board specific and matches the bus_num of some
	 * spi_master that will probably be registered later.
	 *
	 * chip_select reflects how this chip is wired to that master;
	 * it's less than num_chipselect.
	 */
	u16		bus_num;
	u16		chip_select;

754 755 756 757 758
	/* mode becomes spi_device.mode, and is essential for chips
	 * where the default of SPI_CS_HIGH = 0 is wrong.
	 */
	u8		mode;

759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
	/* ... may need additional spi_device chip config data here.
	 * avoid stuff protocol drivers can set; but include stuff
	 * needed to behave without being bound to a driver:
	 *  - quirks like clock rate mattering when not selected
	 */
};

#ifdef	CONFIG_SPI
extern int
spi_register_board_info(struct spi_board_info const *info, unsigned n);
#else
/* board init code may ignore whether SPI is configured or not */
static inline int
spi_register_board_info(struct spi_board_info const *info, unsigned n)
	{ return 0; }
#endif


/* If you're hotplugging an adapter with devices (parport, usb, etc)
D
David Brownell 已提交
778 779 780
 * use spi_new_device() to describe each device.  You can also call
 * spi_unregister_device() to start making that device vanish, but
 * normally that would be handled by spi_unregister_master().
781 782 783 784 785 786
 *
 * You can also use spi_alloc_device() and spi_add_device() to use a two
 * stage registration sequence for each spi_device.  This gives the caller
 * some more control over the spi_device structure before it is registered,
 * but requires that caller to initialize fields that would otherwise
 * be defined using the board info.
787
 */
788 789 790 791 792 793
extern struct spi_device *
spi_alloc_device(struct spi_master *master);

extern int
spi_add_device(struct spi_device *spi);

794 795 796 797 798 799 800 801 802 803 804
extern struct spi_device *
spi_new_device(struct spi_master *, struct spi_board_info *);

static inline void
spi_unregister_device(struct spi_device *spi)
{
	if (spi)
		device_unregister(&spi->dev);
}

#endif /* __LINUX_SPI_H */