aboutsummaryrefslogtreecommitdiffstats
path: root/include/linux/spi
diff options
context:
space:
mode:
authorLars-Peter Clausen <lars@metafoo.de>2013-01-09 12:31:00 -0500
committerJonathan Cameron <jic23@kernel.org>2013-02-09 06:02:07 -0500
commit6d9eecd418afb2c12e5db5be3d72f0f1df43bdd9 (patch)
tree6c9fc5330e8e6970ab08f1c1220d612caeb44344 /include/linux/spi
parentad76fda78318821fe8823b4b7c587a46a268eecf (diff)
spi: Add helper functions for setting up transfers
Quite often the pattern used for setting up and transferring a synchronous SPI transaction looks very much like the following: struct spi_message msg; struct spi_transfer xfers[] = { ... }; spi_message_init(&msg); spi_message_add_tail(&xfers[0], &msg); ... spi_message_add_tail(&xfers[ARRAY_SIZE(xfers) - 1], &msg); ret = spi_sync(&msg); This patch adds two new helper functions for handling this case. The first helper function spi_message_init_with_transfers() takes a spi_message and an array of spi_transfers. It will initialize the message and then call spi_message_add_tail() for each transfer in the array. E.g. the following spi_message_init(&msg); spi_message_add_tail(&xfers[0], &msg); ... spi_message_add_tail(&xfers[ARRAY_SIZE(xfers) - 1], &msg); can be rewritten as spi_message_init_with_transfers(&msg, xfers, ARRAY_SIZE(xfers)); The second function spi_sync_transfer() takes a SPI device and an array of spi_transfers. It will allocate a new spi_message (on the stack) and add all transfers in the array to the message. Finally it will call spi_sync() on the message. E.g. the follwing struct spi_message msg; struct spi_transfer xfers[] = { ... }; spi_message_init(&msg); spi_message_add_tail(&xfers[0], &msg); ... spi_message_add_tail(&xfers[ARRAY_SIZE(xfers) - 1], &msg); ret = spi_sync(spi, &msg); can be rewritten as struct spi_transfer xfers[] = { ... }; ret = spi_sync_transfer(spi, xfers, ARRAY_SIZE(xfers)); A coccinelle script to find such instances will follow. Signed-off-by: Lars-Peter Clausen <lars@metafoo.de> Reviewed-by: Mark Brown <broonie@opensource.wolfsonmicro.com> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
Diffstat (limited to 'include/linux/spi')
-rw-r--r--include/linux/spi/spi.h44
1 files changed, 44 insertions, 0 deletions
diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h
index f62918946d86..7dbe58642525 100644
--- a/include/linux/spi/spi.h
+++ b/include/linux/spi/spi.h
@@ -591,6 +591,26 @@ spi_transfer_del(struct spi_transfer *t)
591 list_del(&t->transfer_list); 591 list_del(&t->transfer_list);
592} 592}
593 593
594/**
595 * spi_message_init_with_transfers - Initialize spi_message and append transfers
596 * @m: spi_message to be initialized
597 * @xfers: An array of spi transfers
598 * @num_xfers: Number of items in the xfer array
599 *
600 * This function initializes the given spi_message and adds each spi_transfer in
601 * the given array to the message.
602 */
603static inline void
604spi_message_init_with_transfers(struct spi_message *m,
605struct spi_transfer *xfers, unsigned int num_xfers)
606{
607 unsigned int i;
608
609 spi_message_init(m);
610 for (i = 0; i < num_xfers; ++i)
611 spi_message_add_tail(&xfers[i], m);
612}
613
594/* It's fine to embed message and transaction structures in other data 614/* It's fine to embed message and transaction structures in other data
595 * structures so long as you don't free them while they're in use. 615 * structures so long as you don't free them while they're in use.
596 */ 616 */
@@ -683,6 +703,30 @@ spi_read(struct spi_device *spi, void *buf, size_t len)
683 return spi_sync(spi, &m); 703 return spi_sync(spi, &m);
684} 704}
685 705
706/**
707 * spi_sync_transfer - synchronous SPI data transfer
708 * @spi: device with which data will be exchanged
709 * @xfers: An array of spi_transfers
710 * @num_xfers: Number of items in the xfer array
711 * Context: can sleep
712 *
713 * Does a synchronous SPI data transfer of the given spi_transfer array.
714 *
715 * For more specific semantics see spi_sync().
716 *
717 * It returns zero on success, else a negative error code.
718 */
719static inline int
720spi_sync_transfer(struct spi_device *spi, struct spi_transfer *xfers,
721 unsigned int num_xfers)
722{
723 struct spi_message msg;
724
725 spi_message_init_with_transfers(&msg, xfers, num_xfers);
726
727 return spi_sync(spi, &msg);
728}
729
686/* this copies txbuf and rxbuf data; for small transfers only! */ 730/* this copies txbuf and rxbuf data; for small transfers only! */
687extern int spi_write_then_read(struct spi_device *spi, 731extern int spi_write_then_read(struct spi_device *spi,
688 const void *txbuf, unsigned n_tx, 732 const void *txbuf, unsigned n_tx,