linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: linux-iio@vger.kernel.org,
	Andy Shevchenko <andy.shevchenko@gmail.com>,
	Peter Rosin <peda@axentia.se>
Cc: Michael Hennerich <michael.hennerich@analog.com>,
	Lars-Peter Clausen <lars@metafoo.de>,
	Vincent Whitchurch <vincent.whitchurch@axis.com>,
	Jonathan Cameron <Jonathan.Cameron@huawei.com>
Subject: [PATCH v2 07/17] staging: iio: cdc: ad7764: Push locking down into case statements in read/write_raw
Date: Sun, 19 Jun 2022 19:58:29 +0100	[thread overview]
Message-ID: <20220619185839.1363503-8-jic23@kernel.org> (raw)
In-Reply-To: <20220619185839.1363503-1-jic23@kernel.org>

From: Jonathan Cameron <Jonathan.Cameron@huawei.com>

Not all paths require any locking at all. So to simplify the
removal of such locking push the locks down into the individual
case statements.

Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
---
 drivers/staging/iio/cdc/ad7746.c | 125 ++++++++++++++-----------------
 1 file changed, 58 insertions(+), 67 deletions(-)

diff --git a/drivers/staging/iio/cdc/ad7746.c b/drivers/staging/iio/cdc/ad7746.c
index bf20c14f60e4..41d9ea4ccb02 100644
--- a/drivers/staging/iio/cdc/ad7746.c
+++ b/drivers/staging/iio/cdc/ad7746.c
@@ -418,14 +418,10 @@ static int ad7746_write_raw(struct iio_dev *indio_dev,
 	struct ad7746_chip_info *chip = iio_priv(indio_dev);
 	int ret, reg;
 
-	mutex_lock(&chip->lock);
-
 	switch (mask) {
 	case IIO_CHAN_INFO_CALIBSCALE:
-		if (val != 1) {
-			ret = -EINVAL;
-			goto out;
-		}
+		if (val != 1)
+			return -EINVAL;
 
 		val = (val2 * 1024) / 15625;
 
@@ -437,33 +433,31 @@ static int ad7746_write_raw(struct iio_dev *indio_dev,
 			reg = AD7746_REG_VOLT_GAINH;
 			break;
 		default:
-			ret = -EINVAL;
-			goto out;
+			return -EINVAL;
 		}
 
+		mutex_lock(&chip->lock);
 		ret = i2c_smbus_write_word_swapped(chip->client, reg, val);
+		mutex_unlock(&chip->lock);
 		if (ret < 0)
-			goto out;
+			return ret;
 
-		ret = 0;
-		break;
+		return 0;
 	case IIO_CHAN_INFO_CALIBBIAS:
-		if (val < 0 || val > 0xFFFF) {
-			ret = -EINVAL;
-			goto out;
-		}
+		if (val < 0 || val > 0xFFFF)
+			return -EINVAL;
+
+		mutex_lock(&chip->lock);
 		ret = i2c_smbus_write_word_swapped(chip->client,
 						   AD7746_REG_CAP_OFFH, val);
+		mutex_unlock(&chip->lock);
 		if (ret < 0)
-			goto out;
+			return ret;
 
-		ret = 0;
-		break;
+		return 0;
 	case IIO_CHAN_INFO_OFFSET:
-		if (val < 0 || val > 43008000) { /* 21pF */
-			ret = -EINVAL;
-			goto out;
-		}
+		if (val < 0 || val > 43008000) /* 21pF */
+			return -EINVAL;
 
 		/*
 		 * CAPDAC Scale = 21pF_typ / 127
@@ -472,34 +466,43 @@ static int ad7746_write_raw(struct iio_dev *indio_dev,
 		 */
 
 		val /= 338646;
-
+		mutex_lock(&chip->lock);
 		chip->capdac[chan->channel][chan->differential] = val > 0 ?
 			AD7746_CAPDAC_DACP(val) | AD7746_CAPDAC_DACEN : 0;
 
 		ret = ad7746_set_capdac(chip, chan->channel);
-		if (ret < 0)
-			goto out;
+		if (ret < 0) {
+			mutex_unlock(&chip->lock);
+			return ret;
+		}
 
 		chip->capdac_set = chan->channel;
+		mutex_unlock(&chip->lock);
 
-		ret = 0;
-		break;
+		return 0;
 	case IIO_CHAN_INFO_SAMP_FREQ:
-		if (val2) {
-			ret = -EINVAL;
-			goto out;
-		}
+		if (val2)
+			return -EINVAL;
 
 		switch (chan->type) {
 		case IIO_CAPACITANCE:
+			mutex_lock(&chip->lock);
 			ret = ad7746_store_cap_filter_rate_setup(chip, val);
-			break;
+			mutex_unlock(&chip->lock);
+			return ret;
 		case IIO_VOLTAGE:
+			mutex_lock(&chip->lock);
 			ret = ad7746_store_vt_filter_rate_setup(chip, val);
-			break;
+			mutex_unlock(&chip->lock);
+			return ret;
 		default:
-			ret = -EINVAL;
+			return -EINVAL;
 		}
+	default:
+		return -EINVAL;
+	}
+}
+
 static int ad7746_read_channel(struct iio_dev *indio_dev,
 			       struct iio_chan_spec const *chan,
 			       int *val)
@@ -552,17 +555,16 @@ static int ad7746_read_raw(struct iio_dev *indio_dev,
 	int ret, idx;
 	u8 reg;
 
-	mutex_lock(&chip->lock);
-
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
 	case IIO_CHAN_INFO_PROCESSED:
+		mutex_lock(&chip->lock);
 		ret = ad7746_read_channel(indio_dev, chan, val);
+		mutex_unlock(&chip->lock);
 		if (ret < 0)
-			goto out;
+			return ret;
 
-		ret = IIO_VAL_INT;
-		break;
+		return IIO_VAL_INT;
 	case IIO_CHAN_INFO_CALIBSCALE:
 		switch (chan->type) {
 		case IIO_CAPACITANCE:
@@ -572,80 +574,69 @@ static int ad7746_read_raw(struct iio_dev *indio_dev,
 			reg = AD7746_REG_VOLT_GAINH;
 			break;
 		default:
-			ret = -EINVAL;
-			goto out;
+			return -EINVAL;
 		}
 
+		mutex_lock(&chip->lock);
 		ret = i2c_smbus_read_word_swapped(chip->client, reg);
+		mutex_unlock(&chip->lock);
 		if (ret < 0)
-			goto out;
+			return ret;
 		/* 1 + gain_val / 2^16 */
 		*val = 1;
 		*val2 = (15625 * ret) / 1024;
 
-		ret = IIO_VAL_INT_PLUS_MICRO;
-		break;
+		return IIO_VAL_INT_PLUS_MICRO;
 	case IIO_CHAN_INFO_CALIBBIAS:
+		mutex_lock(&chip->lock);
 		ret = i2c_smbus_read_word_swapped(chip->client,
 						  AD7746_REG_CAP_OFFH);
+		mutex_unlock(&chip->lock);
 		if (ret < 0)
-			goto out;
+			return ret;
 		*val = ret;
 
-		ret = IIO_VAL_INT;
-		break;
+		return IIO_VAL_INT;
 	case IIO_CHAN_INFO_OFFSET:
 		*val = AD7746_CAPDAC_DACP(chip->capdac[chan->channel]
 					  [chan->differential]) * 338646;
 
-		ret = IIO_VAL_INT;
-		break;
+		return IIO_VAL_INT;
 	case IIO_CHAN_INFO_SCALE:
 		switch (chan->type) {
 		case IIO_CAPACITANCE:
 			/* 8.192pf / 2^24 */
 			*val =  0;
 			*val2 = 488;
-			ret = IIO_VAL_INT_PLUS_NANO;
-			break;
+			return IIO_VAL_INT_PLUS_NANO;
 		case IIO_VOLTAGE:
 			/* 1170mV / 2^23 */
 			*val = 1170;
 			if (chan->channel == 1)
 				*val *= 6;
 			*val2 = 23;
-			ret = IIO_VAL_FRACTIONAL_LOG2;
-			break;
+			return IIO_VAL_FRACTIONAL_LOG2;
 		default:
-			ret = -EINVAL;
-			break;
+			return -EINVAL;
 		}
-
-		break;
 	case IIO_CHAN_INFO_SAMP_FREQ:
 		switch (chan->type) {
 		case IIO_CAPACITANCE:
 			idx = (chip->config & AD7746_CONF_CAPFS_MASK) >>
 				AD7746_CONF_CAPFS_SHIFT;
 			*val = ad7746_cap_filter_rate_table[idx][0];
-			ret = IIO_VAL_INT;
-			break;
+			return IIO_VAL_INT;
 		case IIO_VOLTAGE:
 			idx = (chip->config & AD7746_CONF_VTFS_MASK) >>
 				AD7746_CONF_VTFS_SHIFT;
 			*val = ad7746_vt_filter_rate_table[idx][0];
-			ret = IIO_VAL_INT;
-			break;
+			return IIO_VAL_INT;
 		default:
-			ret = -EINVAL;
+			return -EINVAL;
 		}
-		break;
 	default:
-		ret = -EINVAL;
+		return -EINVAL;
 	}
-out:
-	mutex_unlock(&chip->lock);
-	return ret;
 }
 
 static const struct iio_info ad7746_info = {
-- 
2.36.1


  parent reply	other threads:[~2022-06-19 18:59 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-19 18:58 [PATCH v2 00/17] staging/iio: Clean up AD7746 CDC driver and move from staging Jonathan Cameron
2022-06-19 18:58 ` [PATCH v2 01/17] iio: core: Increase precision of IIO_VAL_FRACTIONAL_LOG2 when possible Jonathan Cameron
2022-06-19 18:58 ` [PATCH v2 02/17] iio: ABI: Fix wrong format of differential capacitance channel ABI Jonathan Cameron
2022-06-19 22:58   ` Andy Shevchenko
2022-06-20 17:50     ` Jonathan Cameron
2022-06-19 18:58 ` [PATCH v2 03/17] staging: iio: cdc: ad7746: Use explicit be24 handling Jonathan Cameron
2022-06-19 18:58 ` [PATCH v2 04/17] staging: iio: cdc: ad7746: Push handling of supply voltage scale to userspace Jonathan Cameron
2022-06-19 18:58 ` [PATCH v2 05/17] staging: iio: cdc: ad7746: Use local buffer for multi byte reads Jonathan Cameron
2022-06-19 23:00   ` Andy Shevchenko
2022-06-20 16:42     ` Jonathan Cameron
2022-06-20 18:23       ` Jonathan Cameron
2022-06-19 18:58 ` [PATCH v2 06/17] staging: iio: cdc: ad7746: Factor out ad7746_read_channel() Jonathan Cameron
2022-06-19 18:58 ` Jonathan Cameron [this message]
2022-06-19 18:58 ` [PATCH v2 08/17] staging: iio: cdc: ad7746: Break up use of chan->address and use FIELD_PREP etc Jonathan Cameron
2022-06-19 18:58 ` [PATCH v2 09/17] staging: iio: cdc: ad7746: Drop usused i2c_set_clientdata() Jonathan Cameron
2022-06-19 18:58 ` [PATCH v2 10/17] staging: iio: cdc: ad7746: Use _raw and _scale for temperature channels Jonathan Cameron
2022-06-19 18:58 ` [PATCH v2 11/17] iio: core: Introduce _inputoffset for differential channels Jonathan Cameron
2022-06-19 23:20   ` Andy Shevchenko
2022-06-20 18:13     ` Jonathan Cameron
2022-06-20 18:35       ` Andy Shevchenko
2022-06-20 20:01         ` Jonathan Cameron
2022-06-20 19:57   ` Jonathan Cameron
2022-06-19 18:58 ` [PATCH v2 12/17] staging: iio: cdc: ad7746: Switch from _offset to " Jonathan Cameron
2022-06-19 18:58 ` [PATCH v2 13/17] staging: iio: cdc: ad7746: Use read_avail() rather than opencoding Jonathan Cameron
2022-06-19 18:58 ` [PATCH v2 14/17] staging: iio: ad7746: White space cleanup Jonathan Cameron
2022-06-19 18:58 ` [PATCH v2 15/17] iio: cdc: ad7746: Add device specific ABI documentation Jonathan Cameron
2022-06-19 18:58 ` [PATCH v2 16/17] iio: cdc: ad7746: Move driver out of staging Jonathan Cameron
2022-06-19 23:11   ` Andy Shevchenko
2022-06-20 16:40     ` Jonathan Cameron
2022-06-20 18:30       ` Jonathan Cameron
2022-06-20 18:40       ` Andy Shevchenko
2022-06-20 19:53         ` Jonathan Cameron
2022-06-20 20:01           ` Andy Shevchenko
2022-06-19 18:58 ` [PATCH v2 17/17] RFC: iio: cdc: ad7746: Add roadtest Jonathan Cameron
2022-06-19 23:23 ` [PATCH v2 00/17] staging/iio: Clean up AD7746 CDC driver and move from staging Andy Shevchenko
2022-06-20 18:07   ` Jonathan Cameron
2022-06-21 14:34   ` Joe Simmons-Talbott
2022-06-22 13:07 ` Vincent Whitchurch
2022-06-26 11:07   ` 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=20220619185839.1363503-8-jic23@kernel.org \
    --to=jic23@kernel.org \
    --cc=Jonathan.Cameron@huawei.com \
    --cc=andy.shevchenko@gmail.com \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=michael.hennerich@analog.com \
    --cc=peda@axentia.se \
    --cc=vincent.whitchurch@axis.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).