linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Lars-Peter Clausen <lars@metafoo.de>
To: Jonathan Cameron <jic23@cam.ac.uk>
Cc: linux-iio@vger.kernel.org, Lars-Peter Clausen <lars@metafoo.de>
Subject: [PATCH 3/7] staging:iio:adis16080: Cleanup SPI transfer
Date: Wed,  9 Jan 2013 15:01:15 +0100	[thread overview]
Message-ID: <1357740079-12326-3-git-send-email-lars@metafoo.de> (raw)
In-Reply-To: <1357740079-12326-1-git-send-email-lars@metafoo.de>

During sampling the driver currently does a spi_read followed by a spi_write.
This is not a problem per se, since CS needs to be deasserted between the two
transfers. So even if another device claims the bus between the two transfers we
should still get a result. But the code is actually spread out over multiple
functions. E.g. the spi_read happens in one function the spi_write in another,
this makes the code harder to follow. This patch re-factors the code to just use
a single spi transaction to do both the read and the write transfer.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
---
 drivers/staging/iio/gyro/adis16080_core.c | 61 +++++++++++++------------------
 1 file changed, 25 insertions(+), 36 deletions(-)

diff --git a/drivers/staging/iio/gyro/adis16080_core.c b/drivers/staging/iio/gyro/adis16080_core.c
index 0268f2a..6016e4c 100644
--- a/drivers/staging/iio/gyro/adis16080_core.c
+++ b/drivers/staging/iio/gyro/adis16080_core.c
@@ -42,32 +42,32 @@ struct adis16080_state {
 	u8 buf[2] ____cacheline_aligned;
 };
 
-static int adis16080_spi_write(struct iio_dev *indio_dev,
-		u16 val)
+static int adis16080_read_sample(struct iio_dev *indio_dev,
+		u16 addr, int *val)
 {
-	int ret;
 	struct adis16080_state *st = iio_priv(indio_dev);
-
-	mutex_lock(&st->buf_lock);
-	st->buf[0] = val >> 8;
-	st->buf[1] = val;
-
-	ret = spi_write(st->us, st->buf, 2);
-	mutex_unlock(&st->buf_lock);
-
-	return ret;
-}
-
-static int adis16080_spi_read(struct iio_dev *indio_dev,
-			      u16 *val)
-{
+	struct spi_message m;
 	int ret;
-	struct adis16080_state *st = iio_priv(indio_dev);
+	struct spi_transfer	t[] = {
+		{
+			.tx_buf		= &st->buf,
+			.len		= 2,
+			.cs_change	= 1,
+		}, {
+			.rx_buf		= &st->buf,
+			.len		= 2,
+		},
+	};
 
 	mutex_lock(&st->buf_lock);
+	st->buf[0] = addr >> 8;
+	st->buf[1] = addr;
 
-	ret = spi_read(st->us, st->buf, 2);
+	spi_message_init(&m);
+	spi_message_add_tail(&t[0], &m);
+	spi_message_add_tail(&t[1], &m);
 
+	ret = spi_sync(st->us, &m);
 	if (ret == 0)
 		*val = sign_extend32(12, ((st->buf[0] & 0xF) << 8) | st->buf[1]);
 	mutex_unlock(&st->buf_lock);
@@ -81,28 +81,17 @@ static int adis16080_read_raw(struct iio_dev *indio_dev,
 			     int *val2,
 			     long mask)
 {
-	int ret = -EINVAL;
-	u16 ut = 0;
-	/* Take the iio_dev status lock */
+	int ret;
 
-	mutex_lock(&indio_dev->mlock);
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
-		ret = adis16080_spi_write(indio_dev,
-					  chan->address |
-					  ADIS16080_DIN_WRITE);
-		if (ret < 0)
-			break;
-		ret = adis16080_spi_read(indio_dev, &ut);
-		if (ret < 0)
-			break;
-		*val = ut;
-		ret = IIO_VAL_INT;
-		break;
+		mutex_lock(&indio_dev->mlock);
+		ret = adis16080_read_sample(indio_dev, chan->address, val);
+		mutex_unlock(&indio_dev->mlock);
+		return ret ? ret : IIO_VAL_INT;
 	}
-	mutex_unlock(&indio_dev->mlock);
 
-	return ret;
+	return -EINVAL;
 }
 
 static const struct iio_chan_spec adis16080_channels[] = {
-- 
1.8.0


  parent reply	other threads:[~2013-01-09 14:00 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-09 14:01 [PATCH 1/7] staging:iio:adis16080: Perform sign extension Lars-Peter Clausen
2013-01-09 14:01 ` [PATCH 2/7] staging:iio:adis16080: Add device id table entry for the adis16100 Lars-Peter Clausen
2013-01-09 21:44   ` Jonathan Cameron
2013-01-09 14:01 ` Lars-Peter Clausen [this message]
2013-01-09 21:45   ` [PATCH 3/7] staging:iio:adis16080: Cleanup SPI transfer Jonathan Cameron
2013-01-09 14:01 ` [PATCH 4/7] staging:iio:adis16080: be16 cleanups Lars-Peter Clausen
2013-01-09 21:49   ` Jonathan Cameron
2013-01-09 14:01 ` [PATCH 5/7] staging:iio:adis16080: Remove unnecessary lock Lars-Peter Clausen
2013-01-09 14:01 ` [PATCH 6/7] staging:iio:adis16080: Add scale and offset attributes Lars-Peter Clausen
2013-01-09 14:01 ` [PATCH 7/7] staging:iio:adis16080: Move out of staging Lars-Peter Clausen
2013-01-09 20:09 ` [PATCH 1/7] staging:iio:adis16080: Perform sign extension Jonathan Cameron
2013-01-09 20:34   ` Lars-Peter Clausen
2013-01-09 21:16     ` Jonathan Cameron
2013-01-26 10:32       ` Jonathan Cameron

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1357740079-12326-3-git-send-email-lars@metafoo.de \
    --to=lars@metafoo.de \
    --cc=jic23@cam.ac.uk \
    --cc=linux-iio@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).