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 05/29] iio: adc: ti-ads1015: Use guard(mutex) and factor out code for INFO_RAW
Date: Mon, 17 Feb 2025 14:16:05 +0000	[thread overview]
Message-ID: <20250217141630.897334-6-jic23@kernel.org> (raw)
In-Reply-To: <20250217141630.897334-1-jic23@kernel.org>

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

By use of automatic lock release and introducing a new utility
function to handle the core activity of reading the ADC channel,
many more complex code flows can be replaced by direct returns.

Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Cc: Marek Vasut <marex@denx.de>
---
 drivers/iio/adc/ti-ads1015.c | 162 ++++++++++++++---------------------
 1 file changed, 64 insertions(+), 98 deletions(-)

diff --git a/drivers/iio/adc/ti-ads1015.c b/drivers/iio/adc/ti-ads1015.c
index 4355726b373a..a91ec18ddbec 100644
--- a/drivers/iio/adc/ti-ads1015.c
+++ b/drivers/iio/adc/ti-ads1015.c
@@ -12,6 +12,7 @@
  */
 
 #include <linux/module.h>
+#include <linux/cleanup.h>
 #include <linux/init.h>
 #include <linux/irq.h>
 #include <linux/i2c.h>
@@ -533,6 +534,31 @@ static int ads1015_read_avail(struct iio_dev *indio_dev,
 	}
 }
 
+static int __ads1015_read_info_raw(struct ads1015_data *data,
+				   struct iio_chan_spec const *chan, int *val)
+{
+	int ret;
+
+	if (ads1015_event_channel_enabled(data) &&
+	    data->event_channel != chan->address)
+		return -EBUSY;
+
+	ret = ads1015_set_power_state(data, true);
+	if (ret < 0)
+		return ret;
+
+	ret = ads1015_get_adc_result(data, chan->address, val);
+	if (ret < 0) {
+		ads1015_set_power_state(data, false);
+		return ret;
+	}
+
+	*val = sign_extend32(*val >> chan->scan_type.shift,
+			     chan->scan_type.realbits - 1);
+
+	return ads1015_set_power_state(data, false);
+}
+
 static int ads1015_read_raw(struct iio_dev *indio_dev,
 			    struct iio_chan_spec const *chan, int *val,
 			    int *val2, long mask)
@@ -540,58 +566,30 @@ static int ads1015_read_raw(struct iio_dev *indio_dev,
 	int ret, idx;
 	struct ads1015_data *data = iio_priv(indio_dev);
 
-	mutex_lock(&data->lock);
+	guard(mutex)(&data->lock);
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
 		ret = iio_device_claim_direct_mode(indio_dev);
 		if (ret)
-			break;
-
-		if (ads1015_event_channel_enabled(data) &&
-				data->event_channel != chan->address) {
-			ret = -EBUSY;
-			goto release_direct;
-		}
-
-		ret = ads1015_set_power_state(data, true);
-		if (ret < 0)
-			goto release_direct;
-
-		ret = ads1015_get_adc_result(data, chan->address, val);
-		if (ret < 0) {
-			ads1015_set_power_state(data, false);
-			goto release_direct;
-		}
-
-		*val = sign_extend32(*val >> chan->scan_type.shift,
-				     chan->scan_type.realbits - 1);
-
-		ret = ads1015_set_power_state(data, false);
-		if (ret < 0)
-			goto release_direct;
-
-		ret = IIO_VAL_INT;
-release_direct:
+			return ret;
+		ret = __ads1015_read_info_raw(data, chan, val);
 		iio_device_release_direct_mode(indio_dev);
-		break;
+		if (ret)
+			return ret;
+
+		return IIO_VAL_INT;
 	case IIO_CHAN_INFO_SCALE:
 		idx = data->channel_data[chan->address].pga;
 		*val = ads1015_fullscale_range[idx];
 		*val2 = chan->scan_type.realbits - 1;
-		ret = IIO_VAL_FRACTIONAL_LOG2;
-		break;
+		return IIO_VAL_FRACTIONAL_LOG2;
 	case IIO_CHAN_INFO_SAMP_FREQ:
 		idx = data->channel_data[chan->address].data_rate;
 		*val = data->chip->data_rate[idx];
-		ret = IIO_VAL_INT;
-		break;
+		return IIO_VAL_INT;
 	default:
-		ret = -EINVAL;
-		break;
+		return -EINVAL;
 	}
-	mutex_unlock(&data->lock);
-
-	return ret;
 }
 
 static int ads1015_write_raw(struct iio_dev *indio_dev,
@@ -599,23 +597,16 @@ static int ads1015_write_raw(struct iio_dev *indio_dev,
 			     int val2, long mask)
 {
 	struct ads1015_data *data = iio_priv(indio_dev);
-	int ret;
 
-	mutex_lock(&data->lock);
+	guard(mutex)(&data->lock);
 	switch (mask) {
 	case IIO_CHAN_INFO_SCALE:
-		ret = ads1015_set_scale(data, chan, val, val2);
-		break;
+		return ads1015_set_scale(data, chan, val, val2);
 	case IIO_CHAN_INFO_SAMP_FREQ:
-		ret = ads1015_set_data_rate(data, chan->address, val);
-		break;
+		return ads1015_set_data_rate(data, chan->address, val);
 	default:
-		ret = -EINVAL;
-		break;
+		return -EINVAL;
 	}
-	mutex_unlock(&data->lock);
-
-	return ret;
 }
 
 static int ads1015_read_event(struct iio_dev *indio_dev,
@@ -624,20 +615,18 @@ static int ads1015_read_event(struct iio_dev *indio_dev,
 	int *val2)
 {
 	struct ads1015_data *data = iio_priv(indio_dev);
-	int ret;
 	unsigned int comp_queue;
 	int period;
 	int dr;
 
-	mutex_lock(&data->lock);
+	guard(mutex)(&data->lock);
 
 	switch (info) {
 	case IIO_EV_INFO_VALUE:
 		*val = (dir == IIO_EV_DIR_RISING) ?
 			data->thresh_data[chan->address].high_thresh :
 			data->thresh_data[chan->address].low_thresh;
-		ret = IIO_VAL_INT;
-		break;
+		return IIO_VAL_INT;
 	case IIO_EV_INFO_PERIOD:
 		dr = data->channel_data[chan->address].data_rate;
 		comp_queue = data->thresh_data[chan->address].comp_queue;
@@ -646,16 +635,10 @@ static int ads1015_read_event(struct iio_dev *indio_dev,
 
 		*val = period / USEC_PER_SEC;
 		*val2 = period % USEC_PER_SEC;
-		ret = IIO_VAL_INT_PLUS_MICRO;
-		break;
+		return IIO_VAL_INT_PLUS_MICRO;
 	default:
-		ret = -EINVAL;
-		break;
+		return -EINVAL;
 	}
-
-	mutex_unlock(&data->lock);
-
-	return ret;
 }
 
 static int ads1015_write_event(struct iio_dev *indio_dev,
@@ -666,24 +649,22 @@ static int ads1015_write_event(struct iio_dev *indio_dev,
 	struct ads1015_data *data = iio_priv(indio_dev);
 	const int *data_rate = data->chip->data_rate;
 	int realbits = chan->scan_type.realbits;
-	int ret = 0;
 	long long period;
 	int i;
 	int dr;
 
-	mutex_lock(&data->lock);
+	guard(mutex)(&data->lock);
 
 	switch (info) {
 	case IIO_EV_INFO_VALUE:
-		if (val >= 1 << (realbits - 1) || val < -1 << (realbits - 1)) {
-			ret = -EINVAL;
-			break;
-		}
+		if (val >= 1 << (realbits - 1) || val < -1 << (realbits - 1))
+			return -EINVAL;
+
 		if (dir == IIO_EV_DIR_RISING)
 			data->thresh_data[chan->address].high_thresh = val;
 		else
 			data->thresh_data[chan->address].low_thresh = val;
-		break;
+		return 0;
 	case IIO_EV_INFO_PERIOD:
 		dr = data->channel_data[chan->address].data_rate;
 		period = val * USEC_PER_SEC + val2;
@@ -694,15 +675,10 @@ static int ads1015_write_event(struct iio_dev *indio_dev,
 				break;
 		}
 		data->thresh_data[chan->address].comp_queue = i;
-		break;
+		return 0;
 	default:
-		ret = -EINVAL;
-		break;
+		return -EINVAL;
 	}
-
-	mutex_unlock(&data->lock);
-
-	return ret;
 }
 
 static int ads1015_read_event_config(struct iio_dev *indio_dev,
@@ -710,25 +686,19 @@ static int ads1015_read_event_config(struct iio_dev *indio_dev,
 	enum iio_event_direction dir)
 {
 	struct ads1015_data *data = iio_priv(indio_dev);
-	int ret = 0;
 
-	mutex_lock(&data->lock);
-	if (data->event_channel == chan->address) {
-		switch (dir) {
-		case IIO_EV_DIR_RISING:
-			ret = 1;
-			break;
-		case IIO_EV_DIR_EITHER:
-			ret = (data->comp_mode == ADS1015_CFG_COMP_MODE_WINDOW);
-			break;
-		default:
-			ret = -EINVAL;
-			break;
-		}
-	}
-	mutex_unlock(&data->lock);
+	guard(mutex)(&data->lock);
+	if (data->event_channel != chan->address)
+		return -EBUSY;
 
-	return ret;
+	switch (dir) {
+	case IIO_EV_DIR_RISING:
+		return 1;
+	case IIO_EV_DIR_EITHER:
+		return (data->comp_mode == ADS1015_CFG_COMP_MODE_WINDOW);
+	default:
+		return -EINVAL;
+	}
 }
 
 static int ads1015_enable_event_config(struct ads1015_data *data,
@@ -813,14 +783,12 @@ static int ads1015_write_event_config(struct iio_dev *indio_dev,
 	int comp_mode = (dir == IIO_EV_DIR_EITHER) ?
 		ADS1015_CFG_COMP_MODE_WINDOW : ADS1015_CFG_COMP_MODE_TRAD;
 
-	mutex_lock(&data->lock);
+	guard(mutex)(&data->lock);
 
 	/* Prevent from enabling both buffer and event at a time */
 	ret = iio_device_claim_direct_mode(indio_dev);
-	if (ret) {
-		mutex_unlock(&data->lock);
+	if (ret)
 		return ret;
-	}
 
 	if (state)
 		ret = ads1015_enable_event_config(data, chan, comp_mode);
@@ -828,8 +796,6 @@ static int ads1015_write_event_config(struct iio_dev *indio_dev,
 		ret = ads1015_disable_event_config(data, chan, comp_mode);
 
 	iio_device_release_direct_mode(indio_dev);
-	mutex_unlock(&data->lock);
-
 	return ret;
 }
 
-- 
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 ` Jonathan Cameron [this message]
2025-02-17 14:16 ` [PATCH 06/29] iio: adc: ti-ads1015: " 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 ` [PATCH 10/29] iio: adc: ad7192: Factor out core of ad7192_write_raw() to simplify error handling Jonathan Cameron
2025-02-19 10:54   ` 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-6-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