Linux IIO development
 help / color / mirror / Atom feed
* [PATCH v2 0/8] IIO: ADCs: Sparse friendly claim of direct mode
@ 2025-03-09 16:58 Jonathan Cameron
  2025-03-09 16:58 ` [PATCH v2 1/8] iio: adc: vf610: Move claim of direct mode to caller of vf610_read_sample and use guard(mutex) Jonathan Cameron
                   ` (8 more replies)
  0 siblings, 9 replies; 13+ messages in thread
From: Jonathan Cameron @ 2025-03-09 16:58 UTC (permalink / raw)
  To: Frank Li, Marek Vasut, linux-iio, Nuno Sá
  Cc: Marek Vasut, Mike Looijmans, Nuno Sá, David Lechner,
	Jonathan Cameron

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

Resend of remaining unreviewed updates in drivers/iio/adc/*

I will eventually just apply these, but I'd much rather someone
else took a quick look to check I haven't done anything stupid!

Note that a number of the drivers touched in this series have no
obvious active maintainer, so it would be much appreciated if anyone
has time to take a look! It is a large series so feel free to review
any you have time to look at rather than feeling you need to look
at the whole thing!

This is effectively part 3 of what will probably be around 5 series
focused on moving from iio_device_claim/release_direct_mode() to
iio_device_claim/release_direct(). The new form is more consistent
with conditional locking semantics and sparse markings have been
added that let us detect miss-balance between claim and release.

More details can be found in the cover letter of the first series:
https://lore.kernel.org/all/20250209180624.701140-1-jic23@kernel.org/

This series focuses on the ADC drivers.

Jonathan Cameron (8):
  iio: adc: vf610: Move claim of direct mode to caller of
    vf610_read_sample and use guard(mutex)
  iio: adc: vf610: Switch to sparse friendly
    iio_device_claim/release_direct()
  iio: adc: ti-ads1100: Use guard(mutex) to allow direct returns
  iio: adc: ti-ads1100: Switch to sparse friendly
    iio_device_claim/release_direct()
  iio: adc: ti-ads1015: Use guard(mutex) and factor out code for
    INFO_RAW
  iio: adc: ti-ads1015: Switch to sparse friendly
    iio_device_claim/release_direct()
  iio: adc: mxs-lradc: Switch to sparse friendly
    iio_device_claim/release_direct()
  iio: adc: rcar: Switch to sparse friendly
    iio_device_claim/release_direct()

 drivers/iio/adc/mxs-lradc-adc.c |  14 ++-
 drivers/iio/adc/rcar-gyroadc.c  |   9 +-
 drivers/iio/adc/ti-ads1015.c    | 168 +++++++++++++-------------------
 drivers/iio/adc/ti-ads1100.c    |  44 +++------
 drivers/iio/adc/vf610_adc.c     |  36 +++----
 5 files changed, 105 insertions(+), 166 deletions(-)

-- 
2.48.1


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH v2 1/8] iio: adc: vf610: Move claim of direct mode to caller of vf610_read_sample and use guard(mutex)
  2025-03-09 16:58 [PATCH v2 0/8] IIO: ADCs: Sparse friendly claim of direct mode Jonathan Cameron
@ 2025-03-09 16:58 ` Jonathan Cameron
  2025-03-09 16:58 ` [PATCH v2 2/8] iio: adc: vf610: Switch to sparse friendly iio_device_claim/release_direct() Jonathan Cameron
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 13+ messages in thread
From: Jonathan Cameron @ 2025-03-09 16:58 UTC (permalink / raw)
  To: Frank Li, Marek Vasut, linux-iio, Nuno Sá
  Cc: Marek Vasut, Mike Looijmans, Nuno Sá, David Lechner,
	Jonathan Cameron

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

These two changes allow direct returns in all paths, improving code
readablity.

Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Cc: Frank Li <Frank.Li@nxp.com>
---
 drivers/iio/adc/vf610_adc.c | 37 ++++++++++++++-----------------------
 1 file changed, 14 insertions(+), 23 deletions(-)

diff --git a/drivers/iio/adc/vf610_adc.c b/drivers/iio/adc/vf610_adc.c
index 513365d42aa5..57a22e31cfc7 100644
--- a/drivers/iio/adc/vf610_adc.c
+++ b/drivers/iio/adc/vf610_adc.c
@@ -11,6 +11,7 @@
 #include <linux/property.h>
 #include <linux/platform_device.h>
 #include <linux/interrupt.h>
+#include <linux/cleanup.h>
 #include <linux/delay.h>
 #include <linux/kernel.h>
 #include <linux/slab.h>
@@ -630,36 +631,29 @@ static const struct attribute_group vf610_attribute_group = {
 	.attrs = vf610_attributes,
 };
 
-static int vf610_read_sample(struct iio_dev *indio_dev,
+static int vf610_read_sample(struct vf610_adc *info,
 			     struct iio_chan_spec const *chan, int *val)
 {
-	struct vf610_adc *info = iio_priv(indio_dev);
 	unsigned int hc_cfg;
 	int ret;
 
-	ret = iio_device_claim_direct_mode(indio_dev);
-	if (ret)
-		return ret;
-
-	mutex_lock(&info->lock);
+	guard(mutex)(&info->lock);
 	reinit_completion(&info->completion);
 	hc_cfg = VF610_ADC_ADCHC(chan->channel);
 	hc_cfg |= VF610_ADC_AIEN;
 	writel(hc_cfg, info->regs + VF610_REG_ADC_HC0);
 	ret = wait_for_completion_interruptible_timeout(&info->completion,
 							VF610_ADC_TIMEOUT);
-	if (ret == 0) {
-		ret = -ETIMEDOUT;
-		goto out_unlock;
-	}
+	if (ret == 0)
+		return -ETIMEDOUT;
 
 	if (ret < 0)
-		goto out_unlock;
+		return ret;
 
 	switch (chan->type) {
 	case IIO_VOLTAGE:
 		*val = info->value;
-		break;
+		return 0;
 	case IIO_TEMP:
 		/*
 		 * Calculate in degree Celsius times 1000
@@ -669,17 +663,10 @@ static int vf610_read_sample(struct iio_dev *indio_dev,
 		*val = 25000 - ((int)info->value - VF610_VTEMP25_3V3) *
 				1000000 / VF610_TEMP_SLOPE_COEFF;
 
-		break;
+		return 0;
 	default:
-		ret = -EINVAL;
-		break;
+		return -EINVAL;
 	}
-
-out_unlock:
-	mutex_unlock(&info->lock);
-	iio_device_release_direct_mode(indio_dev);
-
-	return ret;
 }
 
 static int vf610_read_raw(struct iio_dev *indio_dev,
@@ -694,7 +681,11 @@ static int vf610_read_raw(struct iio_dev *indio_dev,
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
 	case IIO_CHAN_INFO_PROCESSED:
-		ret = vf610_read_sample(indio_dev, chan, val);
+		ret = iio_device_claim_direct_mode(indio_dev);
+		if (ret)
+			return ret;
+		ret = vf610_read_sample(info, chan, val);
+		iio_device_release_direct_mode(indio_dev);
 		if (ret < 0)
 			return ret;
 
-- 
2.48.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v2 2/8] iio: adc: vf610: Switch to sparse friendly iio_device_claim/release_direct()
  2025-03-09 16:58 [PATCH v2 0/8] IIO: ADCs: Sparse friendly claim of direct mode Jonathan Cameron
  2025-03-09 16:58 ` [PATCH v2 1/8] iio: adc: vf610: Move claim of direct mode to caller of vf610_read_sample and use guard(mutex) Jonathan Cameron
@ 2025-03-09 16:58 ` Jonathan Cameron
  2025-03-09 16:58 ` [PATCH v2 3/8] iio: adc: ti-ads1100: Use guard(mutex) to allow direct returns Jonathan Cameron
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 13+ messages in thread
From: Jonathan Cameron @ 2025-03-09 16:58 UTC (permalink / raw)
  To: Frank Li, Marek Vasut, linux-iio, Nuno Sá
  Cc: Marek Vasut, Mike Looijmans, Nuno Sá, David Lechner,
	Jonathan Cameron

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

These new functions allow sparse to find failures to release
direct mode reducing chances of bugs over the claim_direct_mode()
functions that are deprecated.

Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Cc: Frank Li <Frank.Li@nxp.com>
---
 drivers/iio/adc/vf610_adc.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/drivers/iio/adc/vf610_adc.c b/drivers/iio/adc/vf610_adc.c
index 57a22e31cfc7..f506ca4150b1 100644
--- a/drivers/iio/adc/vf610_adc.c
+++ b/drivers/iio/adc/vf610_adc.c
@@ -681,11 +681,10 @@ static int vf610_read_raw(struct iio_dev *indio_dev,
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
 	case IIO_CHAN_INFO_PROCESSED:
-		ret = iio_device_claim_direct_mode(indio_dev);
-		if (ret)
-			return ret;
+		if (!iio_device_claim_direct(indio_dev))
+			return -EBUSY;
 		ret = vf610_read_sample(info, chan, val);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		if (ret < 0)
 			return ret;
 
-- 
2.48.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v2 3/8] iio: adc: ti-ads1100: Use guard(mutex) to allow direct returns
  2025-03-09 16:58 [PATCH v2 0/8] IIO: ADCs: Sparse friendly claim of direct mode Jonathan Cameron
  2025-03-09 16:58 ` [PATCH v2 1/8] iio: adc: vf610: Move claim of direct mode to caller of vf610_read_sample and use guard(mutex) Jonathan Cameron
  2025-03-09 16:58 ` [PATCH v2 2/8] iio: adc: vf610: Switch to sparse friendly iio_device_claim/release_direct() Jonathan Cameron
@ 2025-03-09 16:58 ` Jonathan Cameron
  2025-03-09 16:58 ` [PATCH v2 4/8] iio: adc: ti-ads1100: Switch to sparse friendly iio_device_claim/release_direct() Jonathan Cameron
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 13+ messages in thread
From: Jonathan Cameron @ 2025-03-09 16:58 UTC (permalink / raw)
  To: Frank Li, Marek Vasut, linux-iio, Nuno Sá
  Cc: Marek Vasut, Mike Looijmans, Nuno Sá, David Lechner,
	Jonathan Cameron

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

Use of automated lock release simplifies the code.

Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Cc: Mike Looijmans <mike.looijmans@topic.nl>
---
 drivers/iio/adc/ti-ads1100.c | 39 +++++++++++++-----------------------
 1 file changed, 14 insertions(+), 25 deletions(-)

diff --git a/drivers/iio/adc/ti-ads1100.c b/drivers/iio/adc/ti-ads1100.c
index 1e46f07a9ca6..0519f8afb033 100644
--- a/drivers/iio/adc/ti-ads1100.c
+++ b/drivers/iio/adc/ti-ads1100.c
@@ -10,6 +10,7 @@
 
 #include <linux/bitfield.h>
 #include <linux/bits.h>
+#include <linux/cleanup.h>
 #include <linux/delay.h>
 #include <linux/module.h>
 #include <linux/init.h>
@@ -219,36 +220,31 @@ static int ads1100_read_raw(struct iio_dev *indio_dev,
 	int ret;
 	struct ads1100_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;
+			return ret;
 
 		ret = ads1100_get_adc_result(data, chan->address, val);
-		if (ret >= 0)
-			ret = IIO_VAL_INT;
 		iio_device_release_direct_mode(indio_dev);
-		break;
+		if (ret < 0)
+			return ret;
+
+		return IIO_VAL_INT;
 	case IIO_CHAN_INFO_SCALE:
 		/* full-scale is the supply voltage in millivolts */
 		*val = ads1100_get_vdd_millivolts(data);
 		*val2 = 15 + FIELD_GET(ADS1100_PGA_MASK, data->config);
-		ret = IIO_VAL_FRACTIONAL_LOG2;
-		break;
+		return IIO_VAL_FRACTIONAL_LOG2;
 	case IIO_CHAN_INFO_SAMP_FREQ:
 		*val = ads1100_data_rate[FIELD_GET(ADS1100_DR_MASK,
 						   data->config)];
-		ret = IIO_VAL_INT;
-		break;
+		return IIO_VAL_INT;
 	default:
-		ret = -EINVAL;
-		break;
+		return -EINVAL;
 	}
-	mutex_unlock(&data->lock);
-
-	return ret;
 }
 
 static int ads1100_write_raw(struct iio_dev *indio_dev,
@@ -256,23 +252,16 @@ static int ads1100_write_raw(struct iio_dev *indio_dev,
 			     int val2, long mask)
 {
 	struct ads1100_data *data = iio_priv(indio_dev);
-	int ret;
 
-	mutex_lock(&data->lock);
+	guard(mutex)(&data->lock);
 	switch (mask) {
 	case IIO_CHAN_INFO_SCALE:
-		ret = ads1100_set_scale(data, val, val2);
-		break;
+		return ads1100_set_scale(data, val, val2);
 	case IIO_CHAN_INFO_SAMP_FREQ:
-		ret = ads1100_set_data_rate(data, chan->address, val);
-		break;
+		return ads1100_set_data_rate(data, chan->address, val);
 	default:
-		ret = -EINVAL;
-		break;
+		return -EINVAL;
 	}
-	mutex_unlock(&data->lock);
-
-	return ret;
 }
 
 static const struct iio_info ads1100_info = {
-- 
2.48.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v2 4/8] iio: adc: ti-ads1100: Switch to sparse friendly iio_device_claim/release_direct()
  2025-03-09 16:58 [PATCH v2 0/8] IIO: ADCs: Sparse friendly claim of direct mode Jonathan Cameron
                   ` (2 preceding siblings ...)
  2025-03-09 16:58 ` [PATCH v2 3/8] iio: adc: ti-ads1100: Use guard(mutex) to allow direct returns Jonathan Cameron
@ 2025-03-09 16:58 ` Jonathan Cameron
  2025-03-09 16:58 ` [PATCH v2 5/8] iio: adc: ti-ads1015: Use guard(mutex) and factor out code for INFO_RAW Jonathan Cameron
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 13+ messages in thread
From: Jonathan Cameron @ 2025-03-09 16:58 UTC (permalink / raw)
  To: Frank Li, Marek Vasut, linux-iio, Nuno Sá
  Cc: Marek Vasut, Mike Looijmans, Nuno Sá, David Lechner,
	Jonathan Cameron

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

These new functions allow sparse to find failures to release
direct mode reducing chances of bugs over the claim_direct_mode()
functions that are deprecated.

Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Cc: Mike Looijmans <mike.looijmans@topic.nl>
---
 drivers/iio/adc/ti-ads1100.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/drivers/iio/adc/ti-ads1100.c b/drivers/iio/adc/ti-ads1100.c
index 0519f8afb033..b0790e300b18 100644
--- a/drivers/iio/adc/ti-ads1100.c
+++ b/drivers/iio/adc/ti-ads1100.c
@@ -223,12 +223,11 @@ static int ads1100_read_raw(struct iio_dev *indio_dev,
 	guard(mutex)(&data->lock);
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
-		ret = iio_device_claim_direct_mode(indio_dev);
-		if (ret)
-			return ret;
+		if (!iio_device_claim_direct(indio_dev))
+			return -EBUSY;
 
 		ret = ads1100_get_adc_result(data, chan->address, val);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		if (ret < 0)
 			return ret;
 
-- 
2.48.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v2 5/8] iio: adc: ti-ads1015: Use guard(mutex) and factor out code for INFO_RAW
  2025-03-09 16:58 [PATCH v2 0/8] IIO: ADCs: Sparse friendly claim of direct mode Jonathan Cameron
                   ` (3 preceding siblings ...)
  2025-03-09 16:58 ` [PATCH v2 4/8] iio: adc: ti-ads1100: Switch to sparse friendly iio_device_claim/release_direct() Jonathan Cameron
@ 2025-03-09 16:58 ` Jonathan Cameron
  2025-03-10 23:18   ` David Lechner
  2025-03-09 16:58 ` [PATCH v2 6/8] iio: adc: ti-ads1015: Switch to sparse friendly iio_device_claim/release_direct() Jonathan Cameron
                   ` (3 subsequent siblings)
  8 siblings, 1 reply; 13+ messages in thread
From: Jonathan Cameron @ 2025-03-09 16:58 UTC (permalink / raw)
  To: Frank Li, Marek Vasut, linux-iio, Nuno Sá
  Cc: Marek Vasut, Mike Looijmans, Nuno Sá, David Lechner,
	Jonathan Cameron

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


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v2 6/8] iio: adc: ti-ads1015: Switch to sparse friendly iio_device_claim/release_direct()
  2025-03-09 16:58 [PATCH v2 0/8] IIO: ADCs: Sparse friendly claim of direct mode Jonathan Cameron
                   ` (4 preceding siblings ...)
  2025-03-09 16:58 ` [PATCH v2 5/8] iio: adc: ti-ads1015: Use guard(mutex) and factor out code for INFO_RAW Jonathan Cameron
@ 2025-03-09 16:58 ` Jonathan Cameron
  2025-03-09 16:58 ` [PATCH v2 7/8] iio: adc: mxs-lradc: " Jonathan Cameron
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 13+ messages in thread
From: Jonathan Cameron @ 2025-03-09 16:58 UTC (permalink / raw)
  To: Frank Li, Marek Vasut, linux-iio, Nuno Sá
  Cc: Marek Vasut, Mike Looijmans, Nuno Sá, David Lechner,
	Jonathan Cameron

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

These new functions allow sparse to find failures to release
direct mode reducing chances of bugs over the claim_direct_mode()
functions that are deprecated.

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

diff --git a/drivers/iio/adc/ti-ads1015.c b/drivers/iio/adc/ti-ads1015.c
index a91ec18ddbec..729977b611ec 100644
--- a/drivers/iio/adc/ti-ads1015.c
+++ b/drivers/iio/adc/ti-ads1015.c
@@ -569,11 +569,10 @@ static int ads1015_read_raw(struct iio_dev *indio_dev,
 	guard(mutex)(&data->lock);
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
-		ret = iio_device_claim_direct_mode(indio_dev);
-		if (ret)
-			return ret;
+		if (!iio_device_claim_direct(indio_dev))
+			return -EBUSY;
 		ret = __ads1015_read_info_raw(data, chan, val);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		if (ret)
 			return ret;
 
@@ -786,16 +785,15 @@ static int ads1015_write_event_config(struct iio_dev *indio_dev,
 	guard(mutex)(&data->lock);
 
 	/* Prevent from enabling both buffer and event at a time */
-	ret = iio_device_claim_direct_mode(indio_dev);
-	if (ret)
-		return ret;
+	if (!iio_device_claim_direct(indio_dev))
+		return -EBUSY;
 
 	if (state)
 		ret = ads1015_enable_event_config(data, chan, comp_mode);
 	else
 		ret = ads1015_disable_event_config(data, chan, comp_mode);
 
-	iio_device_release_direct_mode(indio_dev);
+	iio_device_release_direct(indio_dev);
 	return ret;
 }
 
-- 
2.48.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v2 7/8] iio: adc: mxs-lradc: Switch to sparse friendly iio_device_claim/release_direct()
  2025-03-09 16:58 [PATCH v2 0/8] IIO: ADCs: Sparse friendly claim of direct mode Jonathan Cameron
                   ` (5 preceding siblings ...)
  2025-03-09 16:58 ` [PATCH v2 6/8] iio: adc: ti-ads1015: Switch to sparse friendly iio_device_claim/release_direct() Jonathan Cameron
@ 2025-03-09 16:58 ` Jonathan Cameron
  2025-03-09 16:58 ` [PATCH v2 8/8] iio: adc: rcar: " Jonathan Cameron
  2025-03-10 23:21 ` [PATCH v2 0/8] IIO: ADCs: Sparse friendly claim of direct mode David Lechner
  8 siblings, 0 replies; 13+ messages in thread
From: Jonathan Cameron @ 2025-03-09 16:58 UTC (permalink / raw)
  To: Frank Li, Marek Vasut, linux-iio, Nuno Sá
  Cc: Marek Vasut, Mike Looijmans, Nuno Sá, David Lechner,
	Jonathan Cameron

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

These new functions allow sparse to find failures to release
direct mode reducing chances of bugs over the claim_direct_mode()
functions that are deprecated.

Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
---
 drivers/iio/adc/mxs-lradc-adc.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)

diff --git a/drivers/iio/adc/mxs-lradc-adc.c b/drivers/iio/adc/mxs-lradc-adc.c
index 152cbe265e1a..8f1e6acea53b 100644
--- a/drivers/iio/adc/mxs-lradc-adc.c
+++ b/drivers/iio/adc/mxs-lradc-adc.c
@@ -141,9 +141,8 @@ static int mxs_lradc_adc_read_single(struct iio_dev *iio_dev, int chan,
 	 * the same time, yet the code becomes horribly complicated. Therefore I
 	 * applied KISS principle here.
 	 */
-	ret = iio_device_claim_direct_mode(iio_dev);
-	if (ret)
-		return ret;
+	if (!iio_device_claim_direct(iio_dev))
+		return -EBUSY;
 
 	reinit_completion(&adc->completion);
 
@@ -192,7 +191,7 @@ static int mxs_lradc_adc_read_single(struct iio_dev *iio_dev, int chan,
 	writel(LRADC_CTRL1_LRADC_IRQ_EN(0),
 	       adc->base + LRADC_CTRL1 + STMP_OFFSET_REG_CLR);
 
-	iio_device_release_direct_mode(iio_dev);
+	iio_device_release_direct(iio_dev);
 
 	return ret;
 }
@@ -275,9 +274,8 @@ static int mxs_lradc_adc_write_raw(struct iio_dev *iio_dev,
 			adc->scale_avail[chan->channel];
 	int ret;
 
-	ret = iio_device_claim_direct_mode(iio_dev);
-	if (ret)
-		return ret;
+	if (!iio_device_claim_direct(iio_dev))
+		return -EBUSY;
 
 	switch (m) {
 	case IIO_CHAN_INFO_SCALE:
@@ -300,7 +298,7 @@ static int mxs_lradc_adc_write_raw(struct iio_dev *iio_dev,
 		break;
 	}
 
-	iio_device_release_direct_mode(iio_dev);
+	iio_device_release_direct(iio_dev);
 
 	return ret;
 }
-- 
2.48.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v2 8/8] iio: adc: rcar: Switch to sparse friendly iio_device_claim/release_direct()
  2025-03-09 16:58 [PATCH v2 0/8] IIO: ADCs: Sparse friendly claim of direct mode Jonathan Cameron
                   ` (6 preceding siblings ...)
  2025-03-09 16:58 ` [PATCH v2 7/8] iio: adc: mxs-lradc: " Jonathan Cameron
@ 2025-03-09 16:58 ` Jonathan Cameron
  2025-03-10 23:21 ` [PATCH v2 0/8] IIO: ADCs: Sparse friendly claim of direct mode David Lechner
  8 siblings, 0 replies; 13+ messages in thread
From: Jonathan Cameron @ 2025-03-09 16:58 UTC (permalink / raw)
  To: Frank Li, Marek Vasut, linux-iio, Nuno Sá
  Cc: Marek Vasut, Mike Looijmans, Nuno Sá, David Lechner,
	Jonathan Cameron

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

These new functions allow sparse to find failures to release
direct mode reducing chances of bugs over the claim_direct_mode()
functions that are deprecated.

Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Cc: Marek Vasut <marek.vasut+renesas@gmail.com>
---
 drivers/iio/adc/rcar-gyroadc.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/drivers/iio/adc/rcar-gyroadc.c b/drivers/iio/adc/rcar-gyroadc.c
index 11170b5852d1..221c075da198 100644
--- a/drivers/iio/adc/rcar-gyroadc.c
+++ b/drivers/iio/adc/rcar-gyroadc.c
@@ -199,13 +199,12 @@ static int rcar_gyroadc_read_raw(struct iio_dev *indio_dev,
 		if (!consumer)
 			return -EINVAL;
 
-		ret = iio_device_claim_direct_mode(indio_dev);
-		if (ret)
-			return ret;
+		if (!iio_device_claim_direct(indio_dev))
+			return -EBUSY;
 
 		ret = rcar_gyroadc_set_power(priv, true);
 		if (ret < 0) {
-			iio_device_release_direct_mode(indio_dev);
+			iio_device_release_direct(indio_dev);
 			return ret;
 		}
 
@@ -213,7 +212,7 @@ static int rcar_gyroadc_read_raw(struct iio_dev *indio_dev,
 		*val &= BIT(priv->sample_width) - 1;
 
 		ret = rcar_gyroadc_set_power(priv, false);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		if (ret < 0)
 			return ret;
 
-- 
2.48.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH v2 5/8] iio: adc: ti-ads1015: Use guard(mutex) and factor out code for INFO_RAW
  2025-03-09 16:58 ` [PATCH v2 5/8] iio: adc: ti-ads1015: Use guard(mutex) and factor out code for INFO_RAW Jonathan Cameron
@ 2025-03-10 23:18   ` David Lechner
  2025-03-31 11:32     ` Jonathan Cameron
  0 siblings, 1 reply; 13+ messages in thread
From: David Lechner @ 2025-03-10 23:18 UTC (permalink / raw)
  To: Jonathan Cameron, Frank Li, Marek Vasut, linux-iio, Nuno Sá
  Cc: Marek Vasut, Mike Looijmans, Nuno Sá, Jonathan Cameron

On 3/9/25 11:58 AM, Jonathan Cameron wrote:
> 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>
> ---

...

>  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;

The old code returned 0 in this case instead of -EBUSY, so this seems like an
unrelated or unintentional change.

>  
> -	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;
> +	}
>  }
>  

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v2 0/8] IIO: ADCs: Sparse friendly claim of direct mode
  2025-03-09 16:58 [PATCH v2 0/8] IIO: ADCs: Sparse friendly claim of direct mode Jonathan Cameron
                   ` (7 preceding siblings ...)
  2025-03-09 16:58 ` [PATCH v2 8/8] iio: adc: rcar: " Jonathan Cameron
@ 2025-03-10 23:21 ` David Lechner
  2025-03-31 11:33   ` Jonathan Cameron
  8 siblings, 1 reply; 13+ messages in thread
From: David Lechner @ 2025-03-10 23:21 UTC (permalink / raw)
  To: Jonathan Cameron, Frank Li, Marek Vasut, linux-iio, Nuno Sá
  Cc: Marek Vasut, Mike Looijmans, Nuno Sá, Jonathan Cameron

On 3/9/25 11:58 AM, Jonathan Cameron wrote:
> From: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> 
> Resend of remaining unreviewed updates in drivers/iio/adc/*
> 
> I will eventually just apply these, but I'd much rather someone
> else took a quick look to check I haven't done anything stupid!
> 
Only managed to find one little potential oversight in patch 5/8.

Reviewed-by: David Lechner <dlechner@baylibre.com>


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v2 5/8] iio: adc: ti-ads1015: Use guard(mutex) and factor out code for INFO_RAW
  2025-03-10 23:18   ` David Lechner
@ 2025-03-31 11:32     ` Jonathan Cameron
  0 siblings, 0 replies; 13+ messages in thread
From: Jonathan Cameron @ 2025-03-31 11:32 UTC (permalink / raw)
  To: David Lechner
  Cc: Frank Li, Marek Vasut, linux-iio, Nuno Sá, Marek Vasut,
	Mike Looijmans, Nuno Sá, Jonathan Cameron

On Mon, 10 Mar 2025 18:18:57 -0500
David Lechner <dlechner@baylibre.com> wrote:

> On 3/9/25 11:58 AM, Jonathan Cameron wrote:
> > 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>
> > ---  
> 
> ...
> 
> >  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;  
> 
> The old code returned 0 in this case instead of -EBUSY, so this seems like an
> unrelated or unintentional change.
> 
Good catch, not intended so I've made it return 0.

Thanks,

> >  
> > -	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;
> > +	}
> >  }
> >    


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v2 0/8] IIO: ADCs: Sparse friendly claim of direct mode
  2025-03-10 23:21 ` [PATCH v2 0/8] IIO: ADCs: Sparse friendly claim of direct mode David Lechner
@ 2025-03-31 11:33   ` Jonathan Cameron
  0 siblings, 0 replies; 13+ messages in thread
From: Jonathan Cameron @ 2025-03-31 11:33 UTC (permalink / raw)
  To: David Lechner
  Cc: Frank Li, Marek Vasut, linux-iio, Nuno Sá, Marek Vasut,
	Mike Looijmans, Nuno Sá, Jonathan Cameron

On Mon, 10 Mar 2025 18:21:30 -0500
David Lechner <dlechner@baylibre.com> wrote:

> On 3/9/25 11:58 AM, Jonathan Cameron wrote:
> > From: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> > 
> > Resend of remaining unreviewed updates in drivers/iio/adc/*
> > 
> > I will eventually just apply these, but I'd much rather someone
> > else took a quick look to check I haven't done anything stupid!
> >   
> Only managed to find one little potential oversight in patch 5/8.
Fixed that up.
> 
> Reviewed-by: David Lechner <dlechner@baylibre.com>
> 
Applied to the testing branch of iio.git.

Thanks for reviewing!

Jonathan



^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2025-03-31 11:33 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-09 16:58 [PATCH v2 0/8] IIO: ADCs: Sparse friendly claim of direct mode Jonathan Cameron
2025-03-09 16:58 ` [PATCH v2 1/8] iio: adc: vf610: Move claim of direct mode to caller of vf610_read_sample and use guard(mutex) Jonathan Cameron
2025-03-09 16:58 ` [PATCH v2 2/8] iio: adc: vf610: Switch to sparse friendly iio_device_claim/release_direct() Jonathan Cameron
2025-03-09 16:58 ` [PATCH v2 3/8] iio: adc: ti-ads1100: Use guard(mutex) to allow direct returns Jonathan Cameron
2025-03-09 16:58 ` [PATCH v2 4/8] iio: adc: ti-ads1100: Switch to sparse friendly iio_device_claim/release_direct() Jonathan Cameron
2025-03-09 16:58 ` [PATCH v2 5/8] iio: adc: ti-ads1015: Use guard(mutex) and factor out code for INFO_RAW Jonathan Cameron
2025-03-10 23:18   ` David Lechner
2025-03-31 11:32     ` Jonathan Cameron
2025-03-09 16:58 ` [PATCH v2 6/8] iio: adc: ti-ads1015: Switch to sparse friendly iio_device_claim/release_direct() Jonathan Cameron
2025-03-09 16:58 ` [PATCH v2 7/8] iio: adc: mxs-lradc: " Jonathan Cameron
2025-03-09 16:58 ` [PATCH v2 8/8] iio: adc: rcar: " Jonathan Cameron
2025-03-10 23:21 ` [PATCH v2 0/8] IIO: ADCs: Sparse friendly claim of direct mode David Lechner
2025-03-31 11:33   ` Jonathan Cameron

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox