Skip to content

Commit 6d9eecd

Browse files
larsclausenjic23
authored andcommitted
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>
1 parent ad76fda commit 6d9eecd

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

‎include/linux/spi/spi.h‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,26 @@ spi_transfer_del(struct spi_transfer *t)
591591
list_del(&t->transfer_list);
592592
}
593593

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+
*/
603+
static inline void
604+
spi_message_init_with_transfers(struct spi_message *m,
605+
struct 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+
594614
/* It's fine to embed message and transaction structures in other data
595615
* structures so long as you don't free them while they're in use.
596616
*/
@@ -683,6 +703,30 @@ spi_read(struct spi_device *spi, void *buf, size_t len)
683703
return spi_sync(spi, &m);
684704
}
685705

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+
*/
719+
static inline int
720+
spi_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+
686730
/* this copies txbuf and rxbuf data; for small transfers only! */
687731
extern int spi_write_then_read(struct spi_device *spi,
688732
const void *txbuf, unsigned n_tx,

0 commit comments

Comments
 (0)