linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@cam.ac.uk>
To: linux-iio@vger.kernel.org
Cc: Michael Hennerich <michael.hennerich@analog.com>,
	Jonathan Cameron <jic23@cam.ac.uk>
Subject: [PATCH 11/16] staging:iio:adc:ad7152: Add proper locking
Date: Fri, 26 Aug 2011 11:19:28 +0100	[thread overview]
Message-ID: <1314353973-7662-12-git-send-email-jic23@cam.ac.uk> (raw)
In-Reply-To: <1314353973-7662-1-git-send-email-jic23@cam.ac.uk>

From: Michael Hennerich <michael.hennerich@analog.com>

Add proper locking.
Consistently use indio_dev.

Signed-off-by: Michael Hennerich <michael.hennerich@analog.com>
Signed-off-by: Jonathan Cameron <jic23@cam.ac.uk>
---
 drivers/staging/iio/adc/ad7152.c |  109 ++++++++++++++++++++++++-------------
 1 files changed, 71 insertions(+), 38 deletions(-)

diff --git a/drivers/staging/iio/adc/ad7152.c b/drivers/staging/iio/adc/ad7152.c
index d3ffcdf..bf26d38 100644
--- a/drivers/staging/iio/adc/ad7152.c
+++ b/drivers/staging/iio/adc/ad7152.c
@@ -18,7 +18,7 @@
 #include "../sysfs.h"
 
 /*
- * TODO: Check compliance of calibscale and calibbias with abi (units)
+ * TODO: Check compliance of calibbias with abi (units)
  */
 /*
  * AD7152 registers definition
@@ -93,8 +93,8 @@ static inline ssize_t ad7152_start_calib(struct device *dev,
 					 size_t len,
 					 u8 regval)
 {
-	struct iio_dev *dev_info = dev_get_drvdata(dev);
-	struct ad7152_chip_info *chip = iio_priv(dev_info);
+	struct iio_dev *indio_dev = dev_get_drvdata(dev);
+	struct ad7152_chip_info *chip = iio_priv(indio_dev);
 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
 	bool doit;
 	int ret, timeout = 10;
@@ -111,17 +111,23 @@ static inline ssize_t ad7152_start_calib(struct device *dev,
 	else
 		regval |= AD7152_CONF_CH2EN;
 
+	mutex_lock(&indio_dev->mlock);
 	ret = i2c_smbus_write_byte_data(chip->client, AD7152_REG_CFG, regval);
-	if (ret < 0)
+	if (ret < 0) {
+		mutex_unlock(&indio_dev->mlock);
 		return ret;
+	}
 
 	do {
 		mdelay(20);
 		ret = i2c_smbus_read_byte_data(chip->client, AD7152_REG_CFG);
-		if (ret < 0)
+		if (ret < 0) {
+			mutex_unlock(&indio_dev->mlock);
 			return ret;
+		}
 	} while ((ret == regval) && timeout--);
 
+	mutex_unlock(&indio_dev->mlock);
 	return len;
 }
 static ssize_t ad7152_start_offset_calib(struct device *dev,
@@ -157,8 +163,8 @@ static ssize_t ad7152_show_filter_rate_setup(struct device *dev,
 		struct device_attribute *attr,
 		char *buf)
 {
-	struct iio_dev *dev_info = dev_get_drvdata(dev);
-	struct ad7152_chip_info *chip = iio_priv(dev_info);
+	struct iio_dev *indio_dev = dev_get_drvdata(dev);
+	struct ad7152_chip_info *chip = iio_priv(indio_dev);
 
 	return sprintf(buf, "0x%02x\n", chip->filter_rate_setup);
 }
@@ -168,8 +174,8 @@ static ssize_t ad7152_store_filter_rate_setup(struct device *dev,
 		const char *buf,
 		size_t len)
 {
-	struct iio_dev *dev_info = dev_get_drvdata(dev);
-	struct ad7152_chip_info *chip = iio_priv(dev_info);
+	struct iio_dev *indio_dev = dev_get_drvdata(dev);
+	struct ad7152_chip_info *chip = iio_priv(indio_dev);
 	u8 data;
 	int ret;
 
@@ -177,11 +183,13 @@ static ssize_t ad7152_store_filter_rate_setup(struct device *dev,
 	if (ret < 0)
 		return ret;
 
+	mutex_lock(&indio_dev->mlock);
 	ret = i2c_smbus_write_byte_data(chip->client, AD7152_REG_CFG2, data);
 	if (ret < 0)
 		return ret;
 
 	chip->filter_rate_setup = data;
+	mutex_unlock(&indio_dev->mlock);
 
 	return len;
 }
@@ -219,19 +227,23 @@ static const int ad7152_scale_table[] = {
 	30525, 7631, 15263, 61050
 };
 
-static int ad7152_write_raw(struct iio_dev *dev_info,
+static int ad7152_write_raw(struct iio_dev *indio_dev,
 			    struct iio_chan_spec const *chan,
 			    int val,
 			    int val2,
 			    long mask)
 {
-	struct ad7152_chip_info *chip = iio_priv(dev_info);
+	struct ad7152_chip_info *chip = iio_priv(indio_dev);
 	int ret, i;
 
+	mutex_lock(&indio_dev->mlock);
+
 	switch (mask) {
 	case (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE):
-		if (val != 1)
-			return -EINVAL;
+		if (val != 1) {
+			ret = -EINVAL;
+			goto out;
+		}
 
 		val = (val2 * 1024) / 15625;
 
@@ -239,23 +251,29 @@ static int ad7152_write_raw(struct iio_dev *dev_info,
 				ad7152_addresses[chan->channel][AD7152_GAIN],
 				swab16(val));
 		if (ret < 0)
-			return ret;
+			goto out;
 
-		return 0;
+		ret = 0;
+		break;
 
 	case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE):
-		if ((val < 0) | (val > 0xFFFF))
-			return -EINVAL;
+		if ((val < 0) | (val > 0xFFFF)) {
+			ret = -EINVAL;
+			goto out;
+		}
 		ret = i2c_smbus_write_word_data(chip->client,
 				ad7152_addresses[chan->channel][AD7152_OFFS],
 				swab16(val));
 		if (ret < 0)
-			return ret;
+			goto out;
 
-		return 0;
+		ret = 0;
+		break;
 	case (1 << IIO_CHAN_INFO_SCALE_SEPARATE):
-		if (val != 0)
-			return -EINVAL;
+		if (val != 0) {
+			ret = -EINVAL;
+			goto out;
+		}
 		for (i = 0; i < ARRAY_SIZE(ad7152_scale_table); i++)
 			if (val2 == ad7152_scale_table[i])
 				break;
@@ -267,21 +285,29 @@ static int ad7152_write_raw(struct iio_dev *dev_info,
 				ad7152_addresses[chan->channel][AD7152_SETUP],
 				chip->setup[chan->channel]);
 		if (ret < 0)
-			return ret;
-		else
-			return 0;
+			goto out;
+
+		ret = 0;
+		break;
 	default:
-		return -EINVAL;
+		ret = -EINVAL;
 	}
+
+out:
+	mutex_unlock(&indio_dev->mlock);
+	return ret;
 }
-static int ad7152_read_raw(struct iio_dev *dev_info,
+static int ad7152_read_raw(struct iio_dev *indio_dev,
 			   struct iio_chan_spec const *chan,
 			   int *val, int *val2,
 			   long mask)
 {
-	struct ad7152_chip_info *chip = iio_priv(dev_info);
+	struct ad7152_chip_info *chip = iio_priv(indio_dev);
 	int ret;
 	u8 regval = 0;
+
+	mutex_lock(&indio_dev->mlock);
+
 	switch (mask) {
 	case 0:
 		/* First set whether in differential mode */
@@ -298,7 +324,7 @@ static int ad7152_read_raw(struct iio_dev *dev_info,
 				ad7152_addresses[chan->channel][AD7152_SETUP],
 				chip->setup[chan->channel]);
 			if (ret < 0)
-				return ret;
+				goto out;
 		}
 		/* Make sure the channel is enabled */
 		if (chan->channel == 0)
@@ -311,48 +337,55 @@ static int ad7152_read_raw(struct iio_dev *dev_info,
 		ret = i2c_smbus_write_byte_data(chip->client, AD7152_REG_CFG,
 				regval);
 		if (ret < 0)
-			return ret;
+			goto out;
 
 		msleep(60); /* Slowest conversion time */
 		/* Now read the actual register */
 		ret = i2c_smbus_read_word_data(chip->client,
 				ad7152_addresses[chan->channel][AD7152_DATA]);
 		if (ret < 0)
-			return ret;
+			goto out;
 		*val = swab16(ret);
 
-		return IIO_VAL_INT;
+		ret = IIO_VAL_INT;
+		break;
 	case (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE):
 
 		ret = i2c_smbus_read_word_data(chip->client,
 				ad7152_addresses[chan->channel][AD7152_GAIN]);
 		if (ret < 0)
-			return ret;
+			goto out;
 		/* 1 + gain_val / 2^16 */
 		*val = 1;
 		*val2 = (15625 * swab16(ret)) / 1024;
 
-		return IIO_VAL_INT_PLUS_MICRO;
+		ret = IIO_VAL_INT_PLUS_MICRO;
+		break;
 	case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE):
 		ret = i2c_smbus_read_word_data(chip->client,
 				ad7152_addresses[chan->channel][AD7152_OFFS]);
 		if (ret < 0)
-			return ret;
+			goto out;
 		*val = swab16(ret);
 
-		return IIO_VAL_INT;
+		ret = IIO_VAL_INT;
+		break;
 	case (1 << IIO_CHAN_INFO_SCALE_SEPARATE):
 		ret = i2c_smbus_read_byte_data(chip->client,
 				ad7152_addresses[chan->channel][AD7152_SETUP]);
 		if (ret < 0)
-			return ret;
+			goto out;
 		*val = 0;
 		*val2 = ad7152_scale_table[ret >> 6];
 
-		return IIO_VAL_INT_PLUS_NANO;
+		ret = IIO_VAL_INT_PLUS_NANO;
+		break;
 	default:
-		return -EINVAL;
+		ret = -EINVAL;
 	};
+out:
+	mutex_unlock(&indio_dev->mlock);
+	return ret;
 }
 
 static int ad7152_write_raw_get_fmt(struct iio_dev *indio_dev,
-- 
1.7.3.4


  parent reply	other threads:[~2011-08-26 10:11 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-08-26 10:19 [PATCH 00/16] staging:iio: CDC and other driver updates Jonathan Cameron
2011-08-26 10:19 ` [PATCH 01/16] staging:iio:dac:ad5791 chan spec conversion Jonathan Cameron
2011-08-26 10:19 ` [PATCH 02/16] staging:iio:adc:ad7150: chan_spec conv + i2c_smbus commands + drop unused poweroff timeout control Jonathan Cameron
2011-08-26 10:19 ` [PATCH 03/16] staging:iio:adc:ad7150: remove conversion mode handling Jonathan Cameron
2011-08-26 10:19 ` [PATCH 04/16] staging:iio:adc:ad7150: Add support for the second interrupt strobe Jonathan Cameron
2011-08-26 10:19 ` [PATCH 05/16] staging:iio:adc:ad7152 use smbus read / write functions + checkpatch fixes Jonathan Cameron
2011-08-26 10:19 ` [PATCH 06/16] staging:iio:adc:ad7152 set correct number of channels for ad7153 Jonathan Cameron
2011-08-26 10:19 ` [PATCH 07/16] staging:iio:adc:ad7152 bring more into line with abi Jonathan Cameron
2011-08-26 10:19 ` [PATCH 08/16] staging:iio:adc:ad7152: increase readability by introducing proper bit defines Jonathan Cameron
2011-08-26 10:19 ` [PATCH 09/16] staging:iio:adc:ad7152: Miscellaneous fixes and touch-up Jonathan Cameron
2011-08-26 10:19 ` [PATCH 10/16] staging:iio:adc:ad7152: update scale handling Jonathan Cameron
2011-08-26 10:19 ` Jonathan Cameron [this message]
2011-08-26 10:19 ` [PATCH 12/16] staging:iio:adc:ad7152: Update sample rate, conversion time, digital filter handling Jonathan Cameron
2011-08-26 10:19 ` [PATCH 13/16] staging:iio:adc:ad7152: Fix differential channel return value and increase delay Jonathan Cameron
2011-08-26 10:19 ` [PATCH 14/16] staging:iio:adc:ad7291 bring into line with current abi + chan_spec conversion Jonathan Cameron
2011-08-26 10:19 ` [PATCH 15/16] staging:iio:imu:adis16400 cleanups Jonathan Cameron
2011-08-26 10:19 ` [PATCH 16/16] iio: imu: adis16400: Avoid using printk facility Jonathan Cameron
2011-09-02 16:24 ` [PATCH 00/16] staging:iio: CDC and other driver updates 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=1314353973-7662-12-git-send-email-jic23@cam.ac.uk \
    --to=jic23@cam.ac.uk \
    --cc=linux-iio@vger.kernel.org \
    --cc=michael.hennerich@analog.com \
    /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).