summaryrefslogtreecommitdiffstats
path: root/drivers/spi/spi.c
diff options
context:
space:
mode:
authorHubert Feurstein <h.feurstein@gmail.com>2011-06-20 23:46:33 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2011-06-21 13:56:49 +0200
commitc0b962986773539ec059f1cb4d47506a7c8f6077 (patch)
tree6e32b6ad5081ca89acd92e3201f253764376e25c /drivers/spi/spi.c
parent76f46b561974865b1a3abc553ec56c95ee34f9fb (diff)
downloadbarebox-c0b962986773539ec059f1cb4d47506a7c8f6077.tar.gz
barebox-c0b962986773539ec059f1cb4d47506a7c8f6077.tar.xz
spi: add more spi transfer functions
This commit adds the following spi transfer functions: - spi_write - spi_read - spi_write_then_read - spi_w8r8 The code has been ported from the linux kernel. Signed-off-by: Hubert Feurstein <h.feurstein@gmail.com> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'drivers/spi/spi.c')
-rw-r--r--drivers/spi/spi.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 588c2633f5..64568976b2 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -195,3 +195,44 @@ int spi_sync(struct spi_device *spi, struct spi_message *message)
return spi->master->transfer(spi, message);
}
+/**
+ * spi_write_then_read - SPI synchronous write followed by read
+ * @spi: device with which data will be exchanged
+ * @txbuf: data to be written
+ * @n_tx: size of txbuf, in bytes
+ * @rxbuf: buffer into which data will be read
+ * @n_rx: size of rxbuf, in bytes
+ * Context: can sleep
+ *
+ * This performs a half duplex MicroWire style transaction with the
+ * device, sending txbuf and then reading rxbuf. The return value
+ * is zero for success, else a negative errno status code.
+ * This call may only be used from a context that may sleep.
+ */
+int spi_write_then_read(struct spi_device *spi,
+ const void *txbuf, unsigned n_tx,
+ void *rxbuf, unsigned n_rx)
+{
+ int status;
+ struct spi_message message;
+ struct spi_transfer x[2];
+
+ spi_message_init(&message);
+ memset(x, 0, sizeof x);
+ if (n_tx) {
+ x[0].len = n_tx;
+ spi_message_add_tail(&x[0], &message);
+ }
+ if (n_rx) {
+ x[1].len = n_rx;
+ spi_message_add_tail(&x[1], &message);
+ }
+
+ x[0].tx_buf = txbuf;
+ x[1].rx_buf = rxbuf;
+
+ /* do the i/o */
+ status = spi_sync(spi, &message);
+ return status;
+}
+EXPORT_SYMBOL(spi_write_then_read);