Linux IIO development
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: linux-iio@vger.kernel.org
Cc: "David Lechner" <dlechner@baylibre.com>,
	"Nuno Sá" <noname.nuno@gmail.com>,
	"Olivier Moysan" <olivier.moysan@foss.st.com>,
	"Mike Looijmans" <mike.looijmans@topic.nl>,
	"Phil Reid" <preid@electromag.com.au>,
	"Marek Vasut" <marek.vasut+renesas@gmail.com>,
	"Miquel Raynal" <miquel.raynal@bootlin.com>,
	"Claudiu Beznea" <claudiu.beznea@tuxon.dev>,
	"Uwe Kleine-König" <u.kleine-koenig@baylibre.com>,
	"Alisa-Dariana Roman" <alisa.roman@analog.com>,
	"Marek Vasut" <marex@denx.de>, "Frank Li" <Frank.Li@nxp.com>,
	"Jonathan Cameron" <Jonathan.Cameron@huawei.com>
Subject: [PATCH 10/29] iio: adc: ad7192: Factor out core of ad7192_write_raw() to simplify error handling.
Date: Mon, 17 Feb 2025 14:16:10 +0000	[thread overview]
Message-ID: <20250217141630.897334-11-jic23@kernel.org> (raw)
In-Reply-To: <20250217141630.897334-1-jic23@kernel.org>

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

Factor out everything under the lock, use guard() for the mutex to
avoid need to manually unlock, and switch sense of matching checks
in loops to reduce indent.

There are some functional changes in here as well as the code
no longer updates the filter_freq_available if no change has
been made to the oversampling ratio.

Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Cc: Alisa-Dariana Roman <alisa.roman@analog.com>
---
 drivers/iio/adc/ad7192.c | 111 ++++++++++++++++++++-------------------
 1 file changed, 57 insertions(+), 54 deletions(-)

diff --git a/drivers/iio/adc/ad7192.c b/drivers/iio/adc/ad7192.c
index e96a5ae92375..785429900da8 100644
--- a/drivers/iio/adc/ad7192.c
+++ b/drivers/iio/adc/ad7192.c
@@ -7,6 +7,7 @@
 
 #include <linux/interrupt.h>
 #include <linux/bitfield.h>
+#include <linux/cleanup.h>
 #include <linux/clk.h>
 #include <linux/clk-provider.h>
 #include <linux/device.h>
@@ -945,80 +946,82 @@ static int ad7192_read_raw(struct iio_dev *indio_dev,
 	return -EINVAL;
 }
 
-static int ad7192_write_raw(struct iio_dev *indio_dev,
-			    struct iio_chan_spec const *chan,
-			    int val,
-			    int val2,
-			    long mask)
+static int __ad7192_write_raw(struct iio_dev *indio_dev,
+			      struct iio_chan_spec const *chan,
+			      int val,
+			      int val2,
+			      long mask)
 {
 	struct ad7192_state *st = iio_priv(indio_dev);
-	int ret, i, div;
+	int i, div;
 	unsigned int tmp;
 
-	ret = iio_device_claim_direct_mode(indio_dev);
-	if (ret)
-		return ret;
-
-	mutex_lock(&st->lock);
+	guard(mutex)(&st->lock);
 
 	switch (mask) {
 	case IIO_CHAN_INFO_SCALE:
-		ret = -EINVAL;
-		for (i = 0; i < ARRAY_SIZE(st->scale_avail); i++)
-			if (val2 == st->scale_avail[i][1]) {
-				ret = 0;
-				tmp = st->conf;
-				st->conf &= ~AD7192_CONF_GAIN_MASK;
-				st->conf |= FIELD_PREP(AD7192_CONF_GAIN_MASK, i);
-				if (tmp == st->conf)
-					break;
-				ad_sd_write_reg(&st->sd, AD7192_REG_CONF,
-						3, st->conf);
-				ad7192_calibrate_all(st);
-				break;
-			}
-		break;
-	case IIO_CHAN_INFO_SAMP_FREQ:
-		if (!val) {
-			ret = -EINVAL;
-			break;
+		for (i = 0; i < ARRAY_SIZE(st->scale_avail); i++) {
+			if (val2 != st->scale_avail[i][1])
+				continue;
+
+			tmp = st->conf;
+			st->conf &= ~AD7192_CONF_GAIN_MASK;
+			st->conf |= FIELD_PREP(AD7192_CONF_GAIN_MASK, i);
+			if (tmp == st->conf)
+				return 0;
+			ad_sd_write_reg(&st->sd, AD7192_REG_CONF, 3, st->conf);
+			ad7192_calibrate_all(st);
+			return 0;
 		}
+		return -EINVAL;
+	case IIO_CHAN_INFO_SAMP_FREQ:
+		if (!val)
+			return -EINVAL;
 
 		div = st->fclk / (val * ad7192_get_f_order(st) * 1024);
-		if (div < 1 || div > 1023) {
-			ret = -EINVAL;
-			break;
-		}
+		if (div < 1 || div > 1023)
+			return -EINVAL;
 
 		st->mode &= ~AD7192_MODE_RATE_MASK;
 		st->mode |= FIELD_PREP(AD7192_MODE_RATE_MASK, div);
 		ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, st->mode);
 		ad7192_update_filter_freq_avail(st);
-		break;
+		return 0;
 	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
-		ret = ad7192_set_3db_filter_freq(st, val, val2 / 1000);
-		break;
+		return ad7192_set_3db_filter_freq(st, val, val2 / 1000);
 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
-		ret = -EINVAL;
-		for (i = 0; i < ARRAY_SIZE(st->oversampling_ratio_avail); i++)
-			if (val == st->oversampling_ratio_avail[i]) {
-				ret = 0;
-				tmp = st->mode;
-				st->mode &= ~AD7192_MODE_AVG_MASK;
-				st->mode |= FIELD_PREP(AD7192_MODE_AVG_MASK, i);
-				if (tmp == st->mode)
-					break;
-				ad_sd_write_reg(&st->sd, AD7192_REG_MODE,
-						3, st->mode);
-				break;
-			}
-		ad7192_update_filter_freq_avail(st);
-		break;
+		for (i = 0; i < ARRAY_SIZE(st->oversampling_ratio_avail); i++) {
+			if (val != st->oversampling_ratio_avail[i])
+				continue;
+
+			tmp = st->mode;
+			st->mode &= ~AD7192_MODE_AVG_MASK;
+			st->mode |= FIELD_PREP(AD7192_MODE_AVG_MASK, i);
+			if (tmp == st->mode)
+				return 0;
+			ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, st->mode);
+			ad7192_update_filter_freq_avail(st);
+			return 0;
+		}
+		return -EINVAL;
 	default:
-		ret = -EINVAL;
+		return -EINVAL;
 	}
+}
+
+static int ad7192_write_raw(struct iio_dev *indio_dev,
+			    struct iio_chan_spec const *chan,
+			    int val,
+			    int val2,
+			    long mask)
+{
+	int ret;
+
+	ret = iio_device_claim_direct_mode(indio_dev);
+	if (ret)
+		return ret;
 
-	mutex_unlock(&st->lock);
+	ret = __ad7192_write_raw(indio_dev, chan, val, val2, mask);
 
 	iio_device_release_direct_mode(indio_dev);
 
-- 
2.48.1


  parent reply	other threads:[~2025-02-17 14:17 UTC|newest]

Thread overview: 55+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-17 14:16 [PATCH 00/29] IIO: ADCs: Sparse friendly claim of direct mode Jonathan Cameron
2025-02-17 14:16 ` [PATCH 01/29] iio: adc: vf610: Move claim of direct mode to caller of vf610_read_sample and use guard(mutex) Jonathan Cameron
2025-02-17 14:16 ` [PATCH 02/29] iio: adc: vf610: Switch to sparse friendly iio_device_claim/release_direct() Jonathan Cameron
2025-02-17 14:16 ` [PATCH 03/29] iio: adc: ti-ads1100: Use guard(mutex) to allow direct returns Jonathan Cameron
2025-02-17 14:16 ` [PATCH 04/29] iio: adc: ti-ads1100: Switch to sparse friendly iio_device_claim/release_direct() Jonathan Cameron
2025-02-17 14:16 ` [PATCH 05/29] iio: adc: ti-ads1015: Use guard(mutex) and factor out code for INFO_RAW Jonathan Cameron
2025-02-17 14:16 ` [PATCH 06/29] iio: adc: ti-ads1015: Switch to sparse friendly iio_device_claim/release_direct() Jonathan Cameron
2025-02-17 14:16 ` [PATCH 07/29] iio: adc: stm32-dfsdm: Factor out core of reading INFO_RAW Jonathan Cameron
2025-02-18 15:32   ` Olivier MOYSAN
2025-02-17 14:16 ` [PATCH 08/29] iio: adc: stm32-dfsdm: Switch to sparse friendly iio_device_claim/release_direct() Jonathan Cameron
2025-02-18 15:34   ` Olivier MOYSAN
2025-02-17 14:16 ` [PATCH 09/29] iio: adc: ad4030: " Jonathan Cameron
2025-02-19 10:54   ` Nuno Sá
2025-02-17 14:16 ` Jonathan Cameron [this message]
2025-02-19 10:54   ` [PATCH 10/29] iio: adc: ad7192: Factor out core of ad7192_write_raw() to simplify error handling Nuno Sá
2025-02-17 14:16 ` [PATCH 11/29] iio: adc: ad7192: Switch to sparse friendly iio_device_claim/release_direct() Jonathan Cameron
2025-02-19 10:55   ` Nuno Sá
2025-02-17 14:16 ` [PATCH 12/29] iio: adc: ad7768-1: Move setting of val a bit later to avoid unnecessary return value check Jonathan Cameron
2025-02-19 10:56   ` Nuno Sá
2025-02-17 14:16 ` [PATCH 13/29] iio: adc: ad7768-1: Switch to sparse friendly iio_device_claim/release_direct() Jonathan Cameron
2025-02-19 10:56   ` Nuno Sá
2025-02-17 14:16 ` [PATCH 14/29] iio: adc: ad7606: " Jonathan Cameron
2025-02-19 10:57   ` Nuno Sá
2025-02-17 14:16 ` [PATCH 15/29] iio: adc: ad7791: Factor out core of ad7791_write_raw() to simplify error handling Jonathan Cameron
2025-02-19 10:59   ` Nuno Sá
2025-02-17 14:16 ` [PATCH 16/29] iio: adc: ad7791: Switch to sparse friendly iio_device_claim/release_direct() Jonathan Cameron
2025-02-19 11:00   ` Nuno Sá
2025-02-17 14:16 ` [PATCH 17/29] iio: adc: ad7793: Factor out core of ad7793_write_raw() to simplify error handling Jonathan Cameron
2025-02-19 11:00   ` Nuno Sá
2025-02-17 14:16 ` [PATCH 18/29] iio: adc: ad7793: Switch to sparse friendly iio_device_claim/release_direct() Jonathan Cameron
2025-02-19 11:01   ` Nuno Sá
2025-02-17 14:16 ` [PATCH 19/29] iio: adc: ad799x: " Jonathan Cameron
2025-02-19 11:02   ` Nuno Sá
2025-02-17 14:16 ` [PATCH 20/29] iio: adc: ad_sigma_delta: " Jonathan Cameron
2025-02-19 11:03   ` Nuno Sá
2025-02-17 14:16 ` [PATCH 21/29] iio: adc: at91-sama5d2: Move claim of direct mode up a level and use guard() Jonathan Cameron
2025-02-19 12:45   ` Claudiu Beznea
2025-02-17 14:16 ` [PATCH 22/29] iio: adc: at91-sama5d2: Switch to sparse friendly iio_device_claim/release_direct() Jonathan Cameron
2025-02-19 12:46   ` Claudiu Beznea
2025-02-17 14:16 ` [PATCH 23/29] iio: adc: max1027: Move claim of direct mode up one level and use guard() Jonathan Cameron
2025-02-17 15:38   ` Miquel Raynal
2025-02-22 13:04   ` Jonathan Cameron
2025-02-17 14:16 ` [PATCH 24/29] iio: adc: max1027: Switch to sparse friendly iio_device_claim/release_direct() Jonathan Cameron
2025-02-17 15:40   ` Miquel Raynal
2025-02-22 13:06     ` Jonathan Cameron
2025-02-17 14:16 ` [PATCH 25/29] iio: adc: max11410: Factor out writing of sampling frequency to simplify errro paths Jonathan Cameron
2025-02-19 11:03   ` Nuno Sá
2025-02-17 14:16 ` [PATCH 26/29] iio: adc: max11410: Switch to sparse friendly iio_device_claim/release_direct() Jonathan Cameron
2025-02-19 11:04   ` Nuno Sá
2025-02-17 14:16 ` [PATCH 27/29] iio: adc: mxs-lradc: " Jonathan Cameron
2025-02-17 14:16 ` [PATCH 28/29] iio: adc: rcar: " Jonathan Cameron
2025-02-17 14:16 ` [PATCH 29/29] iio: adc: " Jonathan Cameron
2025-02-19 11:07   ` Nuno Sá
2025-02-20 12:47   ` Mike Looijmans
2025-02-22 13:09 ` [PATCH 00/29] IIO: ADCs: Sparse friendly claim of direct mode 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=20250217141630.897334-11-jic23@kernel.org \
    --to=jic23@kernel.org \
    --cc=Frank.Li@nxp.com \
    --cc=Jonathan.Cameron@huawei.com \
    --cc=alisa.roman@analog.com \
    --cc=claudiu.beznea@tuxon.dev \
    --cc=dlechner@baylibre.com \
    --cc=linux-iio@vger.kernel.org \
    --cc=marek.vasut+renesas@gmail.com \
    --cc=marex@denx.de \
    --cc=mike.looijmans@topic.nl \
    --cc=miquel.raynal@bootlin.com \
    --cc=noname.nuno@gmail.com \
    --cc=olivier.moysan@foss.st.com \
    --cc=preid@electromag.com.au \
    --cc=u.kleine-koenig@baylibre.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