Skip to contentSkip to conversation

iio: light: Sparse friendly claim of direct mode

PATCH In review Jonathan Cameron 6 participants 31 messages Mar 9, 2025 – Apr 2, 2025

Subsystems (4)
lore
Share & tools
lore

Jonathan Cameron

author maintainer
#0 lore raw

cover iio: light: Sparse friendly claim of direct mode

From: Jonathan Cameron <[email protected]>

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 4 of what will probably be around 7 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/[email protected]/

This series focuses on the light sensor drivers.

Jonathan Cameron (18):
iio: light: apds9306: Switch to sparse friendly
iio_device_claim/release_direct()
iio: light: gp2ap020a00f: Switch to sparse friendly
iio_device_claim/release_direct()
iio: light: isl29125: Switch to sparse friendly
iio_device_claim/release_direct()
iio: light: as73211: Use guard() and move mode switch into inner
write_raw fucntion
iio: light: as73211: Switch to sparse friendly
iio_device_claim/release_direct()
iio: light: ltr501: Factor out IIO_INFO_RAW leg of read_raw()
callback.
iio: light: ltr501: Factor out core of write_raw() where direct mode
claim is held.
iio: light: ltr501: Switch to sparse friendly
iio_device_claim/release_direct()
iio: light: opt4060: Switch to sparse friendly
iio_device_claim/release_direct()
iio: light: rohm-bu27034: Switch to sparse friendly
iio_device_claim/release_direct()
iio: light: rpr0521: Factor out handling of IIO_INFO_RAW and use

    guard()

iio: light: rpr0521: Switch to sparse friendly
iio_device_claim/release_direct()
iio: light: si1145: Switch to sparse friendly
iio_device_claim/release_direct()
iio: light: st_uvis25: Switch to sparse friendly
iio_device_claim/release_direct()
iio: light: tcs3414: Switch to sparse friendly
iio_device_claim/release_direct()
iio: light: tcs3472: Switch to sparse friendly
iio_device_claim/release_direct()
iio: light: vcnl4000: Switch to sparse friendly
iio_device_claim/release_direct()
iio: light: vcnl4035: Switch to sparse friendly
iio_device_claim/release_direct()

 drivers/iio/light/apds9306.c       |   7 +-
 drivers/iio/light/as73211.c        |  42 ++++----
 drivers/iio/light/gp2ap020a00f.c   |   7 +-
 drivers/iio/light/isl29125.c       |   7 +-
 drivers/iio/light/ltr501.c         | 164 +++++++++++++++--------------
 drivers/iio/light/opt4060.c        |   5 +-
 drivers/iio/light/rohm-bu27034.c   |  14 ++-
 drivers/iio/light/rpr0521.c        |  63 ++++++-----
 drivers/iio/light/si1145.c         |  25 ++---
 drivers/iio/light/st_uvis25_core.c |   7 +-
 drivers/iio/light/tcs3414.c        |   9 +-
 drivers/iio/light/tcs3472.c        |   9 +-
 drivers/iio/light/vcnl4000.c       |  78 +++++++-------
 drivers/iio/light/vcnl4035.c       |  42 +++++---
 14 files changed, 245 insertions(+), 234 deletions(-)
Signature · 1 line
2.48.1
author maintainer
#1 lore raw

PATCH 1/18 iio: light: apds9306: Switch to sparse friendly iio_device_claim/release_direct()

From: Jonathan Cameron <[email protected]>

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.

---
 drivers/iio/light/apds9306.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/iio/light/apds9306.c +3 -4
index 69a0d609cffc..fca7e73a905c 100644
@@ -831,11 +831,10 @@ static int apds9306_read_raw(struct iio_dev *indio_dev,
 		 * Changing device parameters during adc operation, resets
 		 * the ADC which has to avoided.
 		 */
-		ret = iio_device_claim_direct_mode(indio_dev);
-		if (ret)
-			return ret;
+		if (!iio_device_claim_direct(indio_dev))
+			return -EBUSY;
 		ret = apds9306_read_data(data, val, reg);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		if (ret)
 			return ret;
 
Signature · 1 line
2.48.1
maintainer
#2 lore raw

Re: [PATCH 01/18] iio: light: apds9306: Switch to sparse friendly iio_device_claim/release_direct()

Quoted 21 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

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.

---
  drivers/iio/light/apds9306.c | 7 +++----
  1 file changed, 3 insertions(+), 4 deletions(-)
drivers/iio/light/apds9306.c +0 -1
index 69a0d609cffc..fca7e73a905c 100644
@@ -831,11 +831,10 @@ static int apds9306_read_raw(struct iio_dev *indio_dev,
  		 * Changing device parameters during adc operation, resets
  		 * the ADC which has to avoided.
  		 */
-		ret = iio_device_claim_direct_mode(indio_dev);

drivers/iio/light/apds9306.c static int apds9306_read_raw(struct iio_dev *indio_dev, · line 840

Quoted 10 lines
drivers/iio/light/apds9306.c +3 -3
-		if (ret)
-			return ret;
+		if (!iio_device_claim_direct(indio_dev))
+			return -EBUSY;
  		ret = apds9306_read_data(data, val, reg);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
  		if (ret)
  			return ret;
 

Thank you Jonathan.

author maintainer
#3 lore raw

Re: [PATCH 01/18] iio: light: apds9306: Switch to sparse friendly iio_device_claim/release_direct()

Quoted 25 lines · On Mon, 10 Mar 2025 17:05:50 +1030 Subhajit Ghosh…
Quoted 24 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

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.

---
  drivers/iio/light/apds9306.c | 7 +++----
  1 file changed, 3 insertions(+), 4 deletions(-)
drivers/iio/light/apds9306.c +1 -3
index 69a0d609cffc..fca7e73a905c 100644
@@ -831,11 +831,10 @@ static int apds9306_read_raw(struct iio_dev *indio_dev,
  		 * Changing device parameters during adc operation, resets
  		 * the ADC which has to avoided.
  		 */
-		ret = iio_device_claim_direct_mode(indio_dev);
-		if (ret)
-			return ret;
+		if (!iio_device_claim_direct(indio_dev))
Quoted 8 lines

drivers/iio/light/apds9306.c static int apds9306_read_raw(struct iio_dev *indio_dev, · line 840

Quoted 7 lines
drivers/iio/light/apds9306.c +2 -1
+			return -EBUSY;
  		ret = apds9306_read_data(data, val, reg);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
  		if (ret)
  			return ret;
 

Thank you Jonathan.

Maybe a tag if it looks fine to you? Reviewed-by or Acked-by

Thanks,

Jonathan

maintainer Acked-by
#4 lore raw

Re: [PATCH 01/18] iio: light: apds9306: Switch to sparse friendly iio_device_claim/release_direct()

Quoted 36 lines · Jonathan Cameron
Quoted 25 lines · On Mon, 10 Mar 2025 17:05:50 +1030 Subhajit Ghosh…
Quoted 24 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

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.

---
   drivers/iio/light/apds9306.c | 7 +++----
   1 file changed, 3 insertions(+), 4 deletions(-)
drivers/iio/light/apds9306.c +1 -3
index 69a0d609cffc..fca7e73a905c 100644
@@ -831,11 +831,10 @@ static int apds9306_read_raw(struct iio_dev *indio_dev,
   		 * Changing device parameters during adc operation, resets
   		 * the ADC which has to avoided.
   		 */
-		ret = iio_device_claim_direct_mode(indio_dev);
-		if (ret)
-			return ret;
+		if (!iio_device_claim_direct(indio_dev))
Quoted 8 lines

drivers/iio/light/apds9306.c static int apds9306_read_raw(struct iio_dev *indio_dev, · line 840

Quoted 7 lines
drivers/iio/light/apds9306.c +2 -1
+			return -EBUSY;
   		ret = apds9306_read_data(data, val, reg);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
   		if (ret)
   			return ret;
 

Thank you Jonathan.

Quoted 5 lines

Maybe a tag if it looks fine to you? Reviewed-by or Acked-by

Thanks,

Jonathan

Hi Jonathan,

Looks good. My bad. Totally missed it.

Regards,
Subhajit Ghosh

author maintainer
#5 lore raw

PATCH 2/18 iio: light: gp2ap020a00f: Switch to sparse friendly iio_device_claim/release_direct()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

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.

---
 drivers/iio/light/gp2ap020a00f.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/iio/light/gp2ap020a00f.c +3 -4
index 1a352c88598e..c7df4b258e2c 100644
@@ -1283,12 +1283,11 @@ static int gp2ap020a00f_read_raw(struct iio_dev *indio_dev,
 	int err = -EINVAL;
 
 	if (mask == IIO_CHAN_INFO_RAW) {
-		err = iio_device_claim_direct_mode(indio_dev);
-		if (err)
-			return err;
+		if (!iio_device_claim_direct(indio_dev))
+			return -EBUSY;
 
 		err = gp2ap020a00f_read_channel(data, chan, val);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 	}
 	return err < 0 ? err : IIO_VAL_INT;
 }
Signature · 1 line
2.48.1
author maintainer
#6 lore raw

PATCH 3/18 iio: light: isl29125: Switch to sparse friendly iio_device_claim/release_direct()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

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.

---
 drivers/iio/light/isl29125.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/iio/light/isl29125.c +3 -4
index 326dc39e7929..6bc23b164cc5 100644
@@ -131,11 +131,10 @@ static int isl29125_read_raw(struct iio_dev *indio_dev,
 
 	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 = isl29125_read_data(data, chan->scan_index);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		if (ret < 0)
 			return ret;
 		*val = ret;
Signature · 1 line
2.48.1
author maintainer
#7 lore raw

PATCH 4/18 iio: light: as73211: Use guard() and move mode switch into inner write_raw fucntion

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

By using guard(mutex) and moving code that switches the device to config mode into _as73211_write_raw() the error flow is simplified.

---
 drivers/iio/light/as73211.c | 30 ++++++++++++++----------------
 1 file changed, 14 insertions(+), 16 deletions(-)
drivers/iio/light/as73211.c +14 -16
index 37fffce35dd1..4b73ce57a3d8 100644
@@ -17,6 +17,7 @@
 
 #include <linux/bitfield.h>
 #include <linux/completion.h>
+#include <linux/cleanup.h>
 #include <linux/delay.h>
 #include <linux/i2c.h>
 #include <linux/iio/buffer.h>
@@ -517,6 +518,16 @@ static int _as73211_write_raw(struct iio_dev *indio_dev,
 	struct as73211_data *data = iio_priv(indio_dev);
 	int ret;
 
+	/* Need to switch to config mode ... */
+	if ((data->osr & AS73211_OSR_DOS_MASK) != AS73211_OSR_DOS_CONFIG) {
+		data->osr &= ~AS73211_OSR_DOS_MASK;
+		data->osr |= AS73211_OSR_DOS_CONFIG;
+
+		ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_OSR, data->osr);
+		if (ret < 0)
+			return ret;
+	}
+
 	switch (mask) {
 	case IIO_CHAN_INFO_SAMP_FREQ: {
 		int reg_bits, freq_kHz = val / HZ_PER_KHZ;  /* 1024, 2048, ... */
@@ -601,28 +612,15 @@ static int as73211_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec con
 	struct as73211_data *data = iio_priv(indio_dev);
 	int ret;
 
-	mutex_lock(&data->mutex);
+	guard(mutex)(&data->mutex);
 
 	ret = iio_device_claim_direct_mode(indio_dev);
 	if (ret < 0)
-		goto error_unlock;
-
-	/* Need to switch to config mode ... */
-	if ((data->osr & AS73211_OSR_DOS_MASK) != AS73211_OSR_DOS_CONFIG) {
-		data->osr &= ~AS73211_OSR_DOS_MASK;
-		data->osr |= AS73211_OSR_DOS_CONFIG;
-
-		ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_OSR, data->osr);
-		if (ret < 0)
-			goto error_release;
-	}
+		return ret;
 
 	ret = _as73211_write_raw(indio_dev, chan, val, val2, mask);
-
-error_release:
 	iio_device_release_direct_mode(indio_dev);
-error_unlock:
-	mutex_unlock(&data->mutex);
+
 	return ret;
 }
 
Signature · 1 line
2.48.1
reviewer
#8 lore raw

Re: [PATCH 04/18] iio: light: as73211: Use guard() and move mode switch into inner write_raw fucntion

Quoted 16 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

By using guard(mutex) and moving code that switches the device to config mode into _as73211_write_raw() the error flow is simplified.

---
 drivers/iio/light/as73211.c | 30 ++++++++++++++----------------
 1 file changed, 14 insertions(+), 16 deletions(-)
drivers/iio/light/as73211.c +0 -0
index 37fffce35dd1..4b73ce57a3d8 100644

drivers/iio/light/as73211.c line 20

Quoted 4 lines
drivers/iio/light/as73211.c +1 -0
@@ -17,6 +17,7 @@
 
 #include <linux/bitfield.h>
 #include <linux/completion.h>
+#include <linux/cleanup.h>

Alphabetical order?

Quoted 3 lines
drivers/iio/light/as73211.c +0 -0
 #include <linux/delay.h>
 #include <linux/i2c.h>
 #include <linux/iio/buffer.h>
author maintainer
#9 lore raw

PATCH 5/18 iio: light: as73211: Switch to sparse friendly iio_device_claim/release_direct()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

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

---
 drivers/iio/light/as73211.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
drivers/iio/light/as73211.c +7 -9
index 4b73ce57a3d8..22422ca89e64 100644
@@ -419,18 +419,17 @@ static int as73211_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec cons
 	case IIO_CHAN_INFO_RAW: {
 		int ret;
 
-		ret = iio_device_claim_direct_mode(indio_dev);
-		if (ret < 0)
-			return ret;
+		if (!iio_device_claim_direct(indio_dev))
+			return -EBUSY;
 
 		ret = as73211_req_data(data);
 		if (ret < 0) {
-			iio_device_release_direct_mode(indio_dev);
+			iio_device_release_direct(indio_dev);
 			return ret;
 		}
 
 		ret = i2c_smbus_read_word_data(data->client, chan->address);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		if (ret < 0)
 			return ret;
 
@@ -614,12 +613,11 @@ static int as73211_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec con
 
 	guard(mutex)(&data->mutex);
 
-	ret = iio_device_claim_direct_mode(indio_dev);
-	if (ret < 0)
-		return ret;
+	if (!iio_device_claim_direct(indio_dev))
+		return -EBUSY;
 
 	ret = _as73211_write_raw(indio_dev, chan, val, val2, mask);
-	iio_device_release_direct_mode(indio_dev);
+	iio_device_release_direct(indio_dev);
 
 	return ret;
 }
Signature · 1 line
2.48.1
author maintainer
#10 lore raw

PATCH 6/18 iio: light: ltr501: Factor out IIO_INFO_RAW leg of read_raw() callback.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Factoring this code out allows for direct returns, simplifying code flow.

---
 drivers/iio/light/ltr501.c | 55 +++++++++++++++++++++-----------------
 1 file changed, 31 insertions(+), 24 deletions(-)
drivers/iio/light/ltr501.c +31 -24
index 669da0840eba..b42df15b36e1 100644
@@ -646,6 +646,36 @@ static const struct iio_chan_spec ltr301_channels[] = {
 	IIO_CHAN_SOFT_TIMESTAMP(2),
 };
 
+static int ltr501_read_info_raw(struct ltr501_data *data,
+				struct iio_chan_spec const *chan,
+				int *val)
+{
+	__le16 buf[2];
+	int ret;
+
+	switch (chan->type) {
+	case IIO_INTENSITY:
+		mutex_lock(&data->lock_als);
+		ret = ltr501_read_als(data, buf);
+		mutex_unlock(&data->lock_als);
+		if (ret < 0)
+			return ret;
+		*val = le16_to_cpu(chan->address == LTR501_ALS_DATA1 ?
+				   buf[0] : buf[1]);
+		return IIO_VAL_INT;
+	case IIO_PROXIMITY:
+		mutex_lock(&data->lock_ps);
+		ret = ltr501_read_ps(data);
+		mutex_unlock(&data->lock_ps);
+		if (ret < 0)
+			return ret;
+		*val = ret & LTR501_PS_DATA_MASK;
+		return IIO_VAL_INT;
+	default:
+		return -EINVAL;
+	}
+}
+
 static int ltr501_read_raw(struct iio_dev *indio_dev,
 			   struct iio_chan_spec const *chan,
 			   int *val, int *val2, long mask)
@@ -679,30 +709,7 @@ static int ltr501_read_raw(struct iio_dev *indio_dev,
 		if (ret)
 			return ret;
 
-		switch (chan->type) {
-		case IIO_INTENSITY:
-			mutex_lock(&data->lock_als);
-			ret = ltr501_read_als(data, buf);
-			mutex_unlock(&data->lock_als);
-			if (ret < 0)
-				break;
-			*val = le16_to_cpu(chan->address == LTR501_ALS_DATA1 ?
-					   buf[0] : buf[1]);
-			ret = IIO_VAL_INT;
-			break;
-		case IIO_PROXIMITY:
-			mutex_lock(&data->lock_ps);
-			ret = ltr501_read_ps(data);
-			mutex_unlock(&data->lock_ps);
-			if (ret < 0)
-				break;
-			*val = ret & LTR501_PS_DATA_MASK;
-			ret = IIO_VAL_INT;
-			break;
-		default:
-			ret = -EINVAL;
-			break;
-		}
+		ret = ltr501_read_info_raw(data, chan, val);
 
 		iio_device_release_direct_mode(indio_dev);
 		return ret;
Signature · 1 line
2.48.1
author maintainer
#11 lore raw

PATCH 7/18 iio: light: ltr501: Factor out core of write_raw() where direct mode claim is held.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Factoring this code out allows for direct returns on error simplifying code flow.

---
 drivers/iio/light/ltr501.c | 94 +++++++++++++++++++-------------------
 1 file changed, 46 insertions(+), 48 deletions(-)
drivers/iio/light/ltr501.c +46 -48
index b42df15b36e1..c44c852a7d76 100644
@@ -763,18 +763,14 @@ static int ltr501_get_gain_index(const struct ltr501_gain *gain, int size,
 	return -1;
 }
 
-static int ltr501_write_raw(struct iio_dev *indio_dev,
-			    struct iio_chan_spec const *chan,
-			    int val, int val2, long mask)
+static int __ltr501_write_raw(struct iio_dev *indio_dev,
+			      struct iio_chan_spec const *chan,
+			      int val, int val2, long mask)
 {
 	struct ltr501_data *data = iio_priv(indio_dev);
 	int i, ret, freq_val, freq_val2;
 	const struct ltr501_chip_info *info = data->chip_info;
 
-	ret = iio_device_claim_direct_mode(indio_dev);
-	if (ret)
-		return ret;
-
 	switch (mask) {
 	case IIO_CHAN_INFO_SCALE:
 		switch (chan->type) {
@@ -782,53 +778,43 @@ static int ltr501_write_raw(struct iio_dev *indio_dev,
 			i = ltr501_get_gain_index(info->als_gain,
 						  info->als_gain_tbl_size,
 						  val, val2);
-			if (i < 0) {
-				ret = -EINVAL;
-				break;
-			}
+			if (i < 0)
+				return -EINVAL;
 
 			data->als_contr &= ~info->als_gain_mask;
 			data->als_contr |= i << info->als_gain_shift;
 
-			ret = regmap_write(data->regmap, LTR501_ALS_CONTR,
-					   data->als_contr);
-			break;
+			return regmap_write(data->regmap, LTR501_ALS_CONTR,
+					    data->als_contr);
 		case IIO_PROXIMITY:
 			i = ltr501_get_gain_index(info->ps_gain,
 						  info->ps_gain_tbl_size,
 						  val, val2);
-			if (i < 0) {
-				ret = -EINVAL;
-				break;
-			}
+			if (i < 0)
+				return -EINVAL;
+
 			data->ps_contr &= ~LTR501_CONTR_PS_GAIN_MASK;
 			data->ps_contr |= i << LTR501_CONTR_PS_GAIN_SHIFT;
 
-			ret = regmap_write(data->regmap, LTR501_PS_CONTR,
-					   data->ps_contr);
-			break;
+			return regmap_write(data->regmap, LTR501_PS_CONTR,
+					    data->ps_contr);
 		default:
-			ret = -EINVAL;
-			break;
+			return -EINVAL;
 		}
-		break;
 
 	case IIO_CHAN_INFO_INT_TIME:
 		switch (chan->type) {
 		case IIO_INTENSITY:
-			if (val != 0) {
-				ret = -EINVAL;
-				break;
-			}
+			if (val != 0)
+				return -EINVAL;
+
 			mutex_lock(&data->lock_als);
 			ret = ltr501_set_it_time(data, val2);
 			mutex_unlock(&data->lock_als);
-			break;
+			return ret;
 		default:
-			ret = -EINVAL;
-			break;
+			return -EINVAL;
 		}
-		break;
 
 	case IIO_CHAN_INFO_SAMP_FREQ:
 		switch (chan->type) {
@@ -836,50 +822,62 @@ static int ltr501_write_raw(struct iio_dev *indio_dev,
 			ret = ltr501_als_read_samp_freq(data, &freq_val,
 							&freq_val2);
 			if (ret < 0)
-				break;
+				return ret;
 
 			ret = ltr501_als_write_samp_freq(data, val, val2);
 			if (ret < 0)
-				break;
+				return ret;
 
 			/* update persistence count when changing frequency */
 			ret = ltr501_write_intr_prst(data, chan->type,
 						     0, data->als_period);
 
 			if (ret < 0)
-				ret = ltr501_als_write_samp_freq(data, freq_val,
-								 freq_val2);
-			break;
+				/* Do not ovewrite error */
+				ltr501_als_write_samp_freq(data, freq_val,
+							   freq_val2);
+			return ret;
 		case IIO_PROXIMITY:
 			ret = ltr501_ps_read_samp_freq(data, &freq_val,
 						       &freq_val2);
 			if (ret < 0)
-				break;
+				return ret;
 
 			ret = ltr501_ps_write_samp_freq(data, val, val2);
 			if (ret < 0)
-				break;
+				return ret;
 
 			/* update persistence count when changing frequency */
 			ret = ltr501_write_intr_prst(data, chan->type,
 						     0, data->ps_period);
 
 			if (ret < 0)
-				ret = ltr501_ps_write_samp_freq(data, freq_val,
-								freq_val2);
-			break;
+				/* Do not overwrite error */
+				ltr501_ps_write_samp_freq(data, freq_val,
+							  freq_val2);
+			return ret;
 		default:
-			ret = -EINVAL;
-			break;
+			return -EINVAL;
 		}
-		break;
-
 	default:
-		ret = -EINVAL;
-		break;
+		return -EINVAL;
 	}
+}
+
+static int ltr501_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;
+
+	ret = __ltr501_write_raw(indio_dev, chan, val, val2, mask);
 
 	iio_device_release_direct_mode(indio_dev);
+
 	return ret;
 }
 
Signature · 1 line
2.48.1
author maintainer
#12 lore raw

PATCH 8/18 iio: light: ltr501: Switch to sparse friendly iio_device_claim/release_direct()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

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.

---
 drivers/iio/light/ltr501.c | 21 +++++++++------------
 1 file changed, 9 insertions(+), 12 deletions(-)
drivers/iio/light/ltr501.c +9 -12
index c44c852a7d76..23fd0713c64b 100644
@@ -688,14 +688,13 @@ static int ltr501_read_raw(struct iio_dev *indio_dev,
 	case IIO_CHAN_INFO_PROCESSED:
 		switch (chan->type) {
 		case IIO_LIGHT:
-			ret = iio_device_claim_direct_mode(indio_dev);
-			if (ret)
-				return ret;
+			if (!iio_device_claim_direct(indio_dev))
+				return -EBUSY;
 
 			mutex_lock(&data->lock_als);
 			ret = ltr501_read_als(data, buf);
 			mutex_unlock(&data->lock_als);
-			iio_device_release_direct_mode(indio_dev);
+			iio_device_release_direct(indio_dev);
 			if (ret < 0)
 				return ret;
 			*val = ltr501_calculate_lux(le16_to_cpu(buf[1]),
@@ -705,13 +704,12 @@ static int ltr501_read_raw(struct iio_dev *indio_dev,
 			return -EINVAL;
 		}
 	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 = ltr501_read_info_raw(data, chan, val);
 
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		return ret;
 
 	case IIO_CHAN_INFO_SCALE:
@@ -870,13 +868,12 @@ static int ltr501_write_raw(struct iio_dev *indio_dev,
 {
 	int ret;
 
-	ret = iio_device_claim_direct_mode(indio_dev);
-	if (ret)
-		return ret;
+	if (!iio_device_claim_direct(indio_dev))
+		return -EBUSY;
 
 	ret = __ltr501_write_raw(indio_dev, chan, val, val2, mask);
 
-	iio_device_release_direct_mode(indio_dev);
+	iio_device_release_direct(indio_dev);
 
 	return ret;
 }
Signature · 1 line
2.48.1
author maintainer
#13 lore raw

PATCH 9/18 iio: light: opt4060: Switch to sparse friendly iio_device_claim/release_direct()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

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. This is a case where the code is pinning down the mode so also has a claim on buffered mode. A follow up set may move those calls over to a sparse friendly form as well.

Cc Per-Daniel Olsson <[email protected]>
---
 drivers/iio/light/opt4060.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/iio/light/opt4060.c +3 -2
index ab55f8d2ea0c..f4085020e03e 100644
@@ -311,7 +311,7 @@ static int opt4060_set_driver_state(struct iio_dev *indio_dev,
 		 * concurrently change. And we just keep trying until we get one
 		 * of the modes...
 		 */
-		if (iio_device_claim_direct_mode(indio_dev))
+		if (!iio_device_claim_direct(indio_dev))
 			goto any_mode_retry;
 		/*
 		 * This path means that we managed to claim direct mode. In
@@ -320,7 +320,8 @@ static int opt4060_set_driver_state(struct iio_dev *indio_dev,
 		 */
 		ret = opt4060_set_state_common(chip, continuous_sampling,
 					       continuous_irq);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
+		return ret;
 	} else {
 		/*
 		 * This path means that we managed to claim buffer mode. In
Signature · 1 line
2.48.1
Acked-by on v1 9/18
#14 lore raw

Re: [PATCH 09/18] iio: light: opt4060: Switch to sparse friendly iio_device_claim/release_direct()

Quoted 11 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

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. This is a case where the code is pinning down the mode so also has a claim on buffered mode. A follow up set may move those calls over to a sparse friendly form as well.

Cc Per-Daniel Olsson <[email protected]>

Best regards / P-D

Tested-by Reviewed-by
#15 lore raw

Re: [PATCH 09/18] iio: light: opt4060: Switch to sparse friendly iio_device_claim/release_direct()

Quoted 10 lines · Per-Daniel Olsson
Quoted 9 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

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. This is a case where the code is pinning down the mode so also has a claim on buffered mode. A follow up set may move those calls over to a sparse friendly form as well.

Quoted 6 lines
Quoted 2 lines
Cc Per-Daniel Olsson <[email protected]>

Best regards / P-D

I have tested the patch and reviewed it, maybe these tags are more accurate than just Acked-by.

Best regards / P-D

author maintainer
#16 lore raw

PATCH 10/18 iio: light: rohm-bu27034: Switch to sparse friendly iio_device_claim/release_direct()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

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.

---
 drivers/iio/light/rohm-bu27034.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
drivers/iio/light/rohm-bu27034.c +6 -8
index cc25596cb248..7cec5e943373 100644
@@ -998,9 +998,8 @@ static int bu27034_read_raw(struct iio_dev *idev,
 			return -EINVAL;
 
 		/* Don't mess with measurement enabling while buffering */
-		ret = iio_device_claim_direct_mode(idev);
-		if (ret)
-			return ret;
+		if (!iio_device_claim_direct(idev))
+			return -EBUSY;
 
 		mutex_lock(&data->mutex);
 		/*
@@ -1011,7 +1010,7 @@ static int bu27034_read_raw(struct iio_dev *idev,
 		ret = result_get(data, chan->channel, val);
 
 		mutex_unlock(&data->mutex);
-		iio_device_release_direct_mode(idev);
+		iio_device_release_direct(idev);
 
 		if (ret)
 			return ret;
@@ -1050,9 +1049,8 @@ static int bu27034_write_raw(struct iio_dev *idev,
 	struct bu27034_data *data = iio_priv(idev);
 	int ret;
 
-	ret = iio_device_claim_direct_mode(idev);
-	if (ret)
-		return ret;
+	if (!iio_device_claim_direct(idev))
+		return -EBUSY;
 
 	switch (mask) {
 	case IIO_CHAN_INFO_SCALE:
@@ -1069,7 +1067,7 @@ static int bu27034_write_raw(struct iio_dev *idev,
 		break;
 	}
 
-	iio_device_release_direct_mode(idev);
+	iio_device_release_direct(idev);
 
 	return ret;
 }
Signature · 1 line
2.48.1
maintainer Acked-by on v1 10/18
#17 lore raw

Re: [PATCH 10/18] iio: light: rohm-bu27034: Switch to sparse friendly iio_device_claim/release_direct()

Quoted 8 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

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.

Yours,

	--Matti
author maintainer
#18 lore raw

PATCH 11/18 iio: light: rpr0521: Factor out handling of IIO_INFO_RAW and use guard()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Factor out the code which is only called with the direct mode claimed. This and the use of guard(mutex) allows direct returns simplifying code flow.

---
 drivers/iio/light/rpr0521.c | 57 ++++++++++++++++++++++---------------
 1 file changed, 34 insertions(+), 23 deletions(-)
drivers/iio/light/rpr0521.c +34 -23
index 2ba917c5c138..65c60a1d2f0b 100644
@@ -11,6 +11,7 @@
 
 #include <linux/module.h>
 #include <linux/mod_devicetable.h>
+#include <linux/cleanup.h>
 #include <linux/init.h>
 #include <linux/i2c.h>
 #include <linux/regmap.h>
@@ -704,6 +705,38 @@ static int rpr0521_write_ps_offset(struct rpr0521_data *data, int offset)
 	return ret;
 }
 
+static int rpr0521_read_info_raw(struct rpr0521_data *data,
+				 struct iio_chan_spec const *chan,
+				 int *val)
+{
+	u8 device_mask;
+	__le16 raw_data;
+	int ret;
+
+	device_mask = rpr0521_data_reg[chan->address].device_mask;
+
+	guard(mutex)(&data->lock);
+	ret = rpr0521_set_power_state(data, true, device_mask);
+	if (ret < 0)
+		return ret;
+
+	ret = regmap_bulk_read(data->regmap,
+			       rpr0521_data_reg[chan->address].address,
+			       &raw_data, sizeof(raw_data));
+	if (ret < 0) {
+		rpr0521_set_power_state(data, false, device_mask);
+		return ret;
+	}
+
+	ret = rpr0521_set_power_state(data, false, device_mask);
+	if (ret < 0)
+		return ret;
+
+	*val = le16_to_cpu(raw_data);
+
+	return 0;
+}
+
 static int rpr0521_read_raw(struct iio_dev *indio_dev,
 			    struct iio_chan_spec const *chan, int *val,
 			    int *val2, long mask)
@@ -711,8 +744,6 @@ static int rpr0521_read_raw(struct iio_dev *indio_dev,
 	struct rpr0521_data *data = iio_priv(indio_dev);
 	int ret;
 	int busy;
-	u8 device_mask;
-	__le16 raw_data;
 
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
@@ -723,31 +754,11 @@ static int rpr0521_read_raw(struct iio_dev *indio_dev,
 		if (busy)
 			return -EBUSY;
 
-		device_mask = rpr0521_data_reg[chan->address].device_mask;
-
-		mutex_lock(&data->lock);
-		ret = rpr0521_set_power_state(data, true, device_mask);
-		if (ret < 0)
-			goto rpr0521_read_raw_out;
-
-		ret = regmap_bulk_read(data->regmap,
-				       rpr0521_data_reg[chan->address].address,
-				       &raw_data, sizeof(raw_data));
-		if (ret < 0) {
-			rpr0521_set_power_state(data, false, device_mask);
-			goto rpr0521_read_raw_out;
-		}
-
-		ret = rpr0521_set_power_state(data, false, device_mask);
-
-rpr0521_read_raw_out:
-		mutex_unlock(&data->lock);
+		ret = rpr0521_read_info_raw(data, chan, val);
 		iio_device_release_direct_mode(indio_dev);
 		if (ret < 0)
 			return ret;
 
-		*val = le16_to_cpu(raw_data);
-
 		return IIO_VAL_INT;
 
 	case IIO_CHAN_INFO_SCALE:
Signature · 1 line
2.48.1
author maintainer
#19 lore raw

PATCH 12/18 iio: light: rpr0521: Switch to sparse friendly iio_device_claim/release_direct()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

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.

---
 drivers/iio/light/rpr0521.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/iio/light/rpr0521.c +2 -4
index 65c60a1d2f0b..92e7552f3e39 100644
@@ -743,19 +743,17 @@ static int rpr0521_read_raw(struct iio_dev *indio_dev,
 {
 	struct rpr0521_data *data = iio_priv(indio_dev);
 	int ret;
-	int busy;
 
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
 		if (chan->type != IIO_INTENSITY && chan->type != IIO_PROXIMITY)
 			return -EINVAL;
 
-		busy = iio_device_claim_direct_mode(indio_dev);
-		if (busy)
+		if (!iio_device_claim_direct(indio_dev))
 			return -EBUSY;
 
 		ret = rpr0521_read_info_raw(data, chan, val);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		if (ret < 0)
 			return ret;
 
Signature · 1 line
2.48.1
author maintainer
#20 lore raw

PATCH 13/18 iio: light: si1145: Switch to sparse friendly iio_device_claim/release_direct()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

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.

---
 drivers/iio/light/si1145.c | 25 +++++++++++--------------
 1 file changed, 11 insertions(+), 14 deletions(-)
drivers/iio/light/si1145.c +11 -14
index 66abda021696..4aa02afd853e 100644
@@ -633,11 +633,10 @@ static int si1145_read_raw(struct iio_dev *indio_dev,
 		case IIO_VOLTAGE:
 		case IIO_TEMP:
 		case IIO_UVINDEX:
-			ret = iio_device_claim_direct_mode(indio_dev);
-			if (ret)
-				return ret;
+			if (!iio_device_claim_direct(indio_dev))
+				return -EBUSY;
 			ret = si1145_measure(indio_dev, chan);
-			iio_device_release_direct_mode(indio_dev);
+			iio_device_release_direct(indio_dev);
 
 			if (ret < 0)
 				return ret;
@@ -750,18 +749,17 @@ static int si1145_write_raw(struct iio_dev *indio_dev,
 			return -EINVAL;
 		}
 
-		ret = iio_device_claim_direct_mode(indio_dev);
-		if (ret)
-			return ret;
+		if (!iio_device_claim_direct(indio_dev))
+			return -EBUSY;
 
 		ret = si1145_param_set(data, reg1, val);
 		if (ret < 0) {
-			iio_device_release_direct_mode(indio_dev);
+			iio_device_release_direct(indio_dev);
 			return ret;
 		}
 		/* Set recovery period to one's complement of gain */
 		ret = si1145_param_set(data, reg2, (~val & 0x07) << 4);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		return ret;
 	case IIO_CHAN_INFO_RAW:
 		if (chan->type != IIO_CURRENT)
@@ -773,19 +771,18 @@ static int si1145_write_raw(struct iio_dev *indio_dev,
 		reg1 = SI1145_PS_LED_REG(chan->channel);
 		shift = SI1145_PS_LED_SHIFT(chan->channel);
 
-		ret = iio_device_claim_direct_mode(indio_dev);
-		if (ret)
-			return ret;
+		if (!iio_device_claim_direct(indio_dev))
+			return -EBUSY;
 
 		ret = i2c_smbus_read_byte_data(data->client, reg1);
 		if (ret < 0) {
-			iio_device_release_direct_mode(indio_dev);
+			iio_device_release_direct(indio_dev);
 			return ret;
 		}
 		ret = i2c_smbus_write_byte_data(data->client, reg1,
 			(ret & ~(0x0f << shift)) |
 			((val & 0x0f) << shift));
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		return ret;
 	case IIO_CHAN_INFO_SAMP_FREQ:
 		return si1145_store_samp_freq(data, val);
Signature · 1 line
2.48.1
author maintainer
#21 lore raw

PATCH 14/18 iio: light: st_uvis25: Switch to sparse friendly iio_device_claim/release_direct()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

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.

---
 drivers/iio/light/st_uvis25_core.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/iio/light/st_uvis25_core.c +3 -4
index 40a810000df0..124a8f9204a9 100644
@@ -117,9 +117,8 @@ static int st_uvis25_read_raw(struct iio_dev *iio_dev,
 {
 	int ret;
 
-	ret = iio_device_claim_direct_mode(iio_dev);
-	if (ret)
-		return ret;
+	if (!iio_device_claim_direct(iio_dev))
+		return -EBUSY;
 
 	switch (mask) {
 	case IIO_CHAN_INFO_PROCESSED: {
@@ -144,7 +143,7 @@ static int st_uvis25_read_raw(struct iio_dev *iio_dev,
 		break;
 	}
 
-	iio_device_release_direct_mode(iio_dev);
+	iio_device_release_direct(iio_dev);
 
 	return ret;
 }
Signature · 1 line
2.48.1
author maintainer
#22 lore raw

PATCH 15/18 iio: light: tcs3414: Switch to sparse friendly iio_device_claim/release_direct()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

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.

---
 drivers/iio/light/tcs3414.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/iio/light/tcs3414.c +4 -5
index 884e43e4cda4..39268f855c77 100644
@@ -134,16 +134,15 @@ static int tcs3414_read_raw(struct iio_dev *indio_dev,
 
 	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 = tcs3414_req_data(data);
 		if (ret < 0) {
-			iio_device_release_direct_mode(indio_dev);
+			iio_device_release_direct(indio_dev);
 			return ret;
 		}
 		ret = i2c_smbus_read_word_data(data->client, chan->address);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		if (ret < 0)
 			return ret;
 		*val = ret;
Signature · 1 line
2.48.1
author maintainer
#23 lore raw

PATCH 16/18 iio: light: tcs3472: Switch to sparse friendly iio_device_claim/release_direct()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

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.

---
 drivers/iio/light/tcs3472.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/iio/light/tcs3472.c +4 -5
index 2bd36a344ea5..0f8bf8503edd 100644
@@ -148,16 +148,15 @@ static int tcs3472_read_raw(struct iio_dev *indio_dev,
 
 	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 = tcs3472_req_data(data);
 		if (ret < 0) {
-			iio_device_release_direct_mode(indio_dev);
+			iio_device_release_direct(indio_dev);
 			return ret;
 		}
 		ret = i2c_smbus_read_word_data(data->client, chan->address);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		if (ret < 0)
 			return ret;
 		*val = ret;
Signature · 1 line
2.48.1
author maintainer
#24 lore raw

PATCH 17/18 iio: light: vcnl4000: Switch to sparse friendly iio_device_claim/release_direct()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

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.

Also split the enabling and disabling of thresholds into separate functions given overlap is small.

---
 drivers/iio/light/vcnl4000.c | 78 +++++++++++++++++++-----------------
 1 file changed, 41 insertions(+), 37 deletions(-)
drivers/iio/light/vcnl4000.c +41 -37
index e19199b17f2e..d7489bee2dff 100644
@@ -1084,9 +1084,8 @@ static int vcnl4010_read_raw(struct iio_dev *indio_dev,
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
 	case IIO_CHAN_INFO_SCALE:
-		ret = iio_device_claim_direct_mode(indio_dev);
-		if (ret)
-			return ret;
+		if (!iio_device_claim_direct(indio_dev))
+			return -EBUSY;
 
 		/* Protect against event capture. */
 		if (vcnl4010_is_in_periodic_mode(data)) {
@@ -1096,7 +1095,7 @@ static int vcnl4010_read_raw(struct iio_dev *indio_dev,
 						mask);
 		}
 
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		return ret;
 	case IIO_CHAN_INFO_SAMP_FREQ:
 		switch (chan->type) {
@@ -1157,9 +1156,8 @@ static int vcnl4010_write_raw(struct iio_dev *indio_dev,
 	int ret;
 	struct vcnl4000_data *data = iio_priv(indio_dev);
 
-	ret = iio_device_claim_direct_mode(indio_dev);
-	if (ret)
-		return ret;
+	if (!iio_device_claim_direct(indio_dev))
+		return -EBUSY;
 
 	/* Protect against event capture. */
 	if (vcnl4010_is_in_periodic_mode(data)) {
@@ -1183,7 +1181,7 @@ static int vcnl4010_write_raw(struct iio_dev *indio_dev,
 	}
 
 end:
-	iio_device_release_direct_mode(indio_dev);
+	iio_device_release_direct(indio_dev);
 	return ret;
 }
 
@@ -1410,46 +1408,52 @@ static int vcnl4010_read_event_config(struct iio_dev *indio_dev,
 	}
 }
 
-static int vcnl4010_config_threshold(struct iio_dev *indio_dev, bool state)
+static int vcnl4010_config_threshold_enable(struct vcnl4000_data *data)
 {
-	struct vcnl4000_data *data = iio_priv(indio_dev);
 	int ret;
-	int icr;
-	int command;
 
-	if (state) {
-		ret = iio_device_claim_direct_mode(indio_dev);
-		if (ret)
-			return ret;
+	/* Enable periodic measurement of proximity data. */
+	ret = i2c_smbus_write_byte_data(data->client, VCNL4000_COMMAND,
+					VCNL4000_SELF_TIMED_EN | VCNL4000_PROX_EN);
+	if (ret < 0)
+		return ret;
 
-		/* Enable periodic measurement of proximity data. */
-		command = VCNL4000_SELF_TIMED_EN | VCNL4000_PROX_EN;
+	/*
+	 * Enable interrupts on threshold, for proximity data by
+	 * default.
+	 */
+	return i2c_smbus_write_byte_data(data->client, VCNL4010_INT_CTRL,
+					 VCNL4010_INT_THR_EN);
+}
 
-		/*
-		 * Enable interrupts on threshold, for proximity data by
-		 * default.
-		 */
-		icr = VCNL4010_INT_THR_EN;
-	} else {
-		if (!vcnl4010_is_thr_enabled(data))
-			return 0;
+static int vcnl4010_config_threshold_disable(struct vcnl4000_data *data)
+{
+	int ret;
 
-		command = 0;
-		icr = 0;
-	}
+	if (!vcnl4010_is_thr_enabled(data))
+		return 0;
 
-	ret = i2c_smbus_write_byte_data(data->client, VCNL4000_COMMAND,
-					command);
+	ret = i2c_smbus_write_byte_data(data->client, VCNL4000_COMMAND, 0);
 	if (ret < 0)
-		goto end;
+		return ret;
 
-	ret = i2c_smbus_write_byte_data(data->client, VCNL4010_INT_CTRL, icr);
+	return i2c_smbus_write_byte_data(data->client, VCNL4010_INT_CTRL, 0);
+}
 
-end:
-	if (state)
-		iio_device_release_direct_mode(indio_dev);
+static int vcnl4010_config_threshold(struct iio_dev *indio_dev, bool state)
+{
+	struct vcnl4000_data *data = iio_priv(indio_dev);
+	int ret;
 
-	return ret;
+	if (state) {
+		if (!iio_device_claim_direct(indio_dev))
+			return -EBUSY;
+		ret = vcnl4010_config_threshold_enable(data);
+		iio_device_release_direct(indio_dev);
+		return ret;
+	} else {
+		return vcnl4010_config_threshold_disable(data);
+	}
 }
 
 static int vcnl4010_write_event_config(struct iio_dev *indio_dev,
Signature · 1 line
2.48.1
reviewer
#25 lore raw

Re: [PATCH 17/18] iio: light: vcnl4000: Switch to sparse friendly iio_device_claim/release_direct()

Quoted 8 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

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.

Also split the enabling and disabling of thresholds into separate functions given overlap is small.

Should be 2 patches since doing 2 different things?

Quoted 4 lines

---

author maintainer
#26 lore raw

Re: [PATCH 17/18] iio: light: vcnl4000: Switch to sparse friendly iio_device_claim/release_direct()

Quoted 11 lines · On Tue, 11 Mar 2025 14:11:16 -0500 David Lechner <[email protected]> wrote:
Quoted 8 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

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.

Also split the enabling and disabling of thresholds into separate functions given overlap is small.

Should be 2 patches since doing 2 different things?

They are messily overlapped because of the oddity that we only take the direct mode state if enabling. It is in theory possible to do the split of functions first but that will result in messier functions that we then clean up. I'm not keen on that complexity so going to be lazy this time and not do it.

I'll add something to the patch description to say that.

Jonathan

Quoted 5 lines
Quoted 5 lines

---

Reviewed-by
#27 lore raw

Re: [PATCH 17/18] iio: light: vcnl4000: Switch to sparse friendly iio_device_claim/release_direct()

Quoted 21 lines · Jonathan Cameron
Quoted 11 lines · On Tue, 11 Mar 2025 14:11:16 -0500 David Lechner <[email protected]> wrote:
Quoted 8 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

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.

Also split the enabling and disabling of thresholds into separate functions given overlap is small.

Should be 2 patches since doing 2 different things?

They are messily overlapped because of the oddity that we only take the direct mode state if enabling. It is in theory possible to do the split of functions first but that will result in messier functions that we then clean up. I'm not keen on that complexity so going to be lazy this time and not do it.

Quoted 11 lines

I'll add something to the patch description to say that.

Jonathan

Quoted 5 lines
Quoted 5 lines

---

Looks good to me also with one patch.

Tested-by Reviewed-by on v1 17/18
#28 lore raw

Re: [PATCH 17/18] iio: light: vcnl4000: Switch to sparse friendly iio_device_claim/release_direct()

In reply to Jonathan Cameron
Quoted 11 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

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.

Also split the enabling and disabling of thresholds into separate functions given overlap is small.

Best regards / P-D

author maintainer
#29 lore raw

PATCH 18/18 iio: light: vcnl4035: Switch to sparse friendly iio_device_claim/release_direct()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

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.

This case triggered a false positive from sparse, resolved by factoring out the code that includes the claim and release of direct mode.

---
 drivers/iio/light/vcnl4035.c | 42 ++++++++++++++++++++++--------------
 1 file changed, 26 insertions(+), 16 deletions(-)
drivers/iio/light/vcnl4035.c +26 -16
index 67c94be02018..b2bede9d3daa 100644
@@ -156,6 +156,31 @@ static int vcnl4035_set_pm_runtime_state(struct vcnl4035_data *data, bool on)
 	return ret;
 }
 
+static int vcnl4035_read_info_raw(struct iio_dev *indio_dev,
+				  struct iio_chan_spec const *chan, int *val)
+{
+	struct vcnl4035_data *data = iio_priv(indio_dev);
+	int ret;
+	int raw_data;
+	unsigned int reg;
+
+	if (!iio_device_claim_direct(indio_dev))
+		return -EBUSY;
+
+	if (chan->channel)
+		reg = VCNL4035_ALS_DATA;
+	else
+		reg = VCNL4035_WHITE_DATA;
+	ret = regmap_read(data->regmap, reg, &raw_data);
+	iio_device_release_direct(indio_dev);
+	if (ret)
+		return ret;
+
+	*val = raw_data;
+
+	return IIO_VAL_INT;
+}
+
 /*
  *	Device IT	INT Time (ms)	Scale (lux/step)
  *	000		50		0.064
@@ -175,28 +200,13 @@ static int vcnl4035_read_raw(struct iio_dev *indio_dev,
 {
 	struct vcnl4035_data *data = iio_priv(indio_dev);
 	int ret;
-	int raw_data;
-	unsigned int reg;
 
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
 		ret = vcnl4035_set_pm_runtime_state(data, true);
 		if  (ret < 0)
 			return ret;
-
-		ret = iio_device_claim_direct_mode(indio_dev);
-		if (!ret) {
-			if (chan->channel)
-				reg = VCNL4035_ALS_DATA;
-			else
-				reg = VCNL4035_WHITE_DATA;
-			ret = regmap_read(data->regmap, reg, &raw_data);
-			iio_device_release_direct_mode(indio_dev);
-			if (!ret) {
-				*val = raw_data;
-				ret = IIO_VAL_INT;
-			}
-		}
+		ret = vcnl4035_read_info_raw(indio_dev, chan, val);
 		vcnl4035_set_pm_runtime_state(data, false);
 		return ret;
 	case IIO_CHAN_INFO_INT_TIME:
Signature · 1 line
2.48.1
reviewer Reviewed-by on the v1 series
#30 lore raw
In reply to Jonathan Cameron
Quoted 14 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

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 4 of what will probably be around 7 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.

Quoted 5 lines

More details can be found in the cover letter of the first series:
https://lore.kernel.org/all/[email protected]/

This series focuses on the light sensor drivers.

Made a couple of minor comments, but overall looks OK.