]> git.karo-electronics.de Git - karo-tx-linux.git/commit
spi: Add helper functions for setting up transfers
authorLars-Peter Clausen <lars@metafoo.de>
Wed, 9 Jan 2013 17:31:00 +0000 (17:31 +0000)
committerJonathan Cameron <jic23@kernel.org>
Sat, 9 Feb 2013 11:02:07 +0000 (11:02 +0000)
commit6d9eecd418afb2c12e5db5be3d72f0f1df43bdd9
tree6c9fc5330e8e6970ab08f1c1220d612caeb44344
parentad76fda78318821fe8823b4b7c587a46a268eecf
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>
include/linux/spi/spi.h