Skip to contentSkip to conversation

IIO: Sparse friendly claim of direct mode (the rest)

PATCH In review Jonathan Cameron 8 participants 58 messages Mar 31, 2025 – Apr 12, 2025

Subsystems (14)
lore
Share & tools
lore

Jonathan Cameron

author maintainer
#0 lore raw

cover IIO: Sparse friendly claim of direct mode (the rest)

From: Jonathan Cameron <[email protected]>

Firstly, sorry for the huge patch set!

I've run out of signficiant groups to break the remaining patches up into. Rather than have lots of small series of 5-10 patches, lets just have one mega one. Feel free to review a subset! I'll nibble away at this as reviews come in and post a v2 later in the cycle with whatever remains unreviewed if that is necessary.

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!

This is effectively part 5 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/[email protected]/

This series focuses on all the remaining drivers.
The final patch removes the old infrastructure.

Jonathan Cameron (37):
iio: addac: ad74115: Switch to sparse friendly
iio_device_claim/release_direct()
iio: chemical: ccs811: Factor out handling of read of IIO_INFO_RAW to
simplify error paths.
iio: chemical: ccs811: Switch to sparse friendly
iio_device_claim/release_direct()
iio: chemical: atlas-sensor: Switch to sparse friendly
iio_device_claim/release_direct()
iio: chemical: scd4x: Switch to sparse friendly
iio_device_claim/release_direct()
iio: common: scmi: Switch to sparse friendly
iio_device_claim/release_direct()
iio: common: st_sensors: Switch to sparse friendly
iio_device_claim/release_direct()
iio: gyro: adxrs290: Switch to sparse friendly
iio_device_claim/release_direct()
iio: health: max30102: Switch to sparse friendly
iio_device_claim/release_direct()
iio: humidity: hdc100x: Use guard(mutex) to simplify code flow
iio: humidity: hdc100x: Switch to sparse friendly
iio_device_claim/release_direct()
iio: humidity: hdc2010: Switch to sparse friendly
iio_device_claim/release_direct()
iio: humidity: hts211: Factor out everything under direct mode claim
into helper functions.
iio: humidity: hts211: Switch to sparse friendly
iio_device_claim/release_direct()
iio: imu: inv_icm42600: Switch to sparse friendly
iio_device_claim/release_direct()
iio: imu: inv_mpu6050: Switch to sparse friendly
iio_device_claim/release_direct()
iio: imu: smi240: Switch to sparse friendly
iio_device_claim/release_direct()
iio: imu: st_lsm6dsx: Factor out parts of st_lsm6dsx_shub_write_raw()
to allow direct returns
iio: imu: st_lsm6dsx: Switch to sparse friendly claim/release_direct()
iio: imu: st_lsm6dsx: Switch to sparse friendly
iio_device_claim/release_direct()
iio: magnetometer: mag3110: Factor out core of read/write_raw() and
use guard() to simplify code flow.
iio: magnetometer: mag3110: Switch to sparse friendly
iio_device_claim/release_direct()
iio: magnetometer: rm3100: Switch to sparse friendly
iio_device_claim/release_direct()
iio: pressure: dlhl60d: Switch to sparse friendly
iio_device_claim/release_direct()
iio: pressure: icp10100: Switch to sparse friendly
iio_device_claim/release_direct()
iio: pressure: mpl3115: factor out core of IIO_INFO_RAW read to
simplify code flow
iio: pressure: mpl3115: Switch to sparse friendly
iio_device_claim/release_direct()
iio: pressure: ms5611: Switch to sparse friendly
iio_device_claim/release_direct()
iio: pressure: rohm-bm1390: Switch to sparse friendly
iio_device_claim/release_direct()
iio: pressure: zpa2326: Switch to sparse friendly
iio_device_claim/release_direct()
iio: proximity: hx9023s: Switch to sparse friendly
iio_device_claim/release_direct()
iio: proximity: pulsed-light: Switch to sparse friendly
iio_device_claim/release_direct()
iio: proximity: sx9500: Switch to sparse friendly
iio_device_claim/release_direct()
iio: temp: maxim_thermocouple: Switch to sparse friendly
iio_device_claim/release_direct()
iio: temp: maxim_thermocouple: Drop unused mutex.h include.
staging: iio: ad5933: Switch to sparse friendly
iio_device_claim/release_direct()
iio: Adjust internals of handling of direct mode claiming to suit new
API.

 drivers/iio/addac/ad74115.c                   |   7 +-
 drivers/iio/chemical/atlas-sensor.c           |   7 +-
 drivers/iio/chemical/ccs811.c                 |  79 ++++-----
 drivers/iio/chemical/scd4x.c                  |   7 +-
 drivers/iio/common/scmi_sensors/scmi_iio.c    |   7 +-
 .../iio/common/st_sensors/st_sensors_core.c   |   7 +-
 drivers/iio/gyro/adxrs290.c                   |  14 +-
 drivers/iio/health/max30102.c                 |   4 +-
 drivers/iio/humidity/hdc100x.c                |  68 +++-----
 drivers/iio/humidity/hdc2010.c                |  14 +-
 drivers/iio/humidity/hts221_core.c            |  91 +++++-----
 .../iio/imu/inv_icm42600/inv_icm42600_accel.c |  21 +--
 .../iio/imu/inv_icm42600/inv_icm42600_gyro.c  |  21 +--
 .../iio/imu/inv_icm42600/inv_icm42600_temp.c  |   7 +-
 drivers/iio/imu/inv_mpu6050/inv_mpu_core.c    |  14 +-
 drivers/iio/imu/smi240.c                      |   7 +-
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c  |  16 +-
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c  |  73 ++++----
 drivers/iio/industrialio-core.c               |  28 +--
 drivers/iio/magnetometer/mag3110.c            | 161 +++++++++---------
 drivers/iio/magnetometer/rm3100-core.c        |   7 +-
 drivers/iio/pressure/dlhl60d.c                |   7 +-
 drivers/iio/pressure/icp10100.c               |  15 +-
 drivers/iio/pressure/mpl3115.c                |  97 ++++++-----
 drivers/iio/pressure/ms5611_core.c            |   8 +-
 drivers/iio/pressure/rohm-bm1390.c            |   7 +-
 drivers/iio/pressure/zpa2326.c                |  15 +-
 drivers/iio/proximity/hx9023s.c               |   7 +-
 .../iio/proximity/pulsedlight-lidar-lite-v2.c |   4 +-
 drivers/iio/proximity/sx9500.c                |   7 +-
 drivers/iio/temperature/maxim_thermocouple.c  |  30 ++--
 .../staging/iio/impedance-analyzer/ad5933.c   |  38 +++--
 include/linux/iio/iio.h                       |  10 +-
 33 files changed, 428 insertions(+), 477 deletions(-)
Signature · 1 line
2.48.1
author maintainer
#1 lore raw

PATCH 1/37 iio: addac: ad74115: 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/addac/ad74115.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/iio/addac/ad74115.c +3 -4
index a7e480f2472d..edccdc9cf34b 100644
@@ -866,15 +866,14 @@ static int ad74115_get_adc_code(struct iio_dev *indio_dev,
 	struct ad74115_state *st = iio_priv(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;
 
 	mutex_lock(&st->lock);
 	ret = _ad74115_get_adc_code(st, channel, val);
 	mutex_unlock(&st->lock);
 
-	iio_device_release_direct_mode(indio_dev);
+	iio_device_release_direct(indio_dev);
 
 	return ret;
 }
Signature · 1 line
2.48.1
author maintainer
#2 lore raw

PATCH 2/37 iio: chemical: ccs811: Factor out handling of read of IIO_INFO_RAW to simplify error paths.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Factor out the implementation of this part of read_raw() and use guard() to allow direct returns, simplifying both error and non error paths.

---
 drivers/iio/chemical/ccs811.c | 72 ++++++++++++++++++-----------------
 1 file changed, 37 insertions(+), 35 deletions(-)
drivers/iio/chemical/ccs811.c +37 -35
index 451fb65dbe60..75b0cb05dd86 100644
@@ -15,6 +15,7 @@
  * 4. Read error register and put the information in logs
  */
 
+#include <linux/cleanup.h>
 #include <linux/delay.h>
 #include <linux/gpio/consumer.h>
 #include <linux/i2c.h>
@@ -214,6 +215,40 @@ static int ccs811_get_measurement(struct ccs811_data *data)
 	return ret;
 }
 
+static int ccs811_read_info_raw(struct ccs811_data *data,
+				struct iio_chan_spec const *chan,
+				int *val, int mask)
+{
+	int ret;
+
+	guard(mutex)(&data->lock);
+	ret = ccs811_get_measurement(data);
+	if (ret < 0)
+		return ret;
+
+	switch (chan->type) {
+	case IIO_VOLTAGE:
+		*val = be16_to_cpu(data->buffer.raw_data) & CCS811_VOLTAGE_MASK;
+		return IIO_VAL_INT;
+	case IIO_CURRENT:
+		*val = be16_to_cpu(data->buffer.raw_data) >> 10;
+		return IIO_VAL_INT;
+	case IIO_CONCENTRATION:
+		switch (chan->channel2) {
+		case IIO_MOD_CO2:
+			*val = be16_to_cpu(data->buffer.co2);
+			return IIO_VAL_INT;
+		case IIO_MOD_VOC:
+			*val = be16_to_cpu(data->buffer.voc);
+			return IIO_VAL_INT;
+		default:
+			return -EINVAL;
+		}
+	default:
+		return -EINVAL;
+	}
+}
+
 static int ccs811_read_raw(struct iio_dev *indio_dev,
 			   struct iio_chan_spec const *chan,
 			   int *val, int *val2, long mask)
@@ -226,42 +261,9 @@ static int ccs811_read_raw(struct iio_dev *indio_dev,
 		ret = iio_device_claim_direct_mode(indio_dev);
 		if (ret)
 			return ret;
-		mutex_lock(&data->lock);
-		ret = ccs811_get_measurement(data);
-		if (ret < 0) {
-			mutex_unlock(&data->lock);
-			iio_device_release_direct_mode(indio_dev);
-			return ret;
-		}
 
-		switch (chan->type) {
-		case IIO_VOLTAGE:
-			*val = be16_to_cpu(data->buffer.raw_data) &
-					   CCS811_VOLTAGE_MASK;
-			ret = IIO_VAL_INT;
-			break;
-		case IIO_CURRENT:
-			*val = be16_to_cpu(data->buffer.raw_data) >> 10;
-			ret = IIO_VAL_INT;
-			break;
-		case IIO_CONCENTRATION:
-			switch (chan->channel2) {
-			case IIO_MOD_CO2:
-				*val = be16_to_cpu(data->buffer.co2);
-				ret =  IIO_VAL_INT;
-				break;
-			case IIO_MOD_VOC:
-				*val = be16_to_cpu(data->buffer.voc);
-				ret = IIO_VAL_INT;
-				break;
-			default:
-				ret = -EINVAL;
-			}
-			break;
-		default:
-			ret = -EINVAL;
-		}
-		mutex_unlock(&data->lock);
+		ret = ccs811_read_info_raw(data, chan, val, mask);
+
 		iio_device_release_direct_mode(indio_dev);
 
 		return ret;
Signature · 1 line
2.48.1
author maintainer
#3 lore raw

PATCH 3/37 iio: chemical: ccs811: 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/chemical/ccs811.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/iio/chemical/ccs811.c +3 -4
index 75b0cb05dd86..1eab256a1e00 100644
@@ -258,13 +258,12 @@ static int ccs811_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 = ccs811_read_info_raw(data, chan, val, mask);
 
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 
 		return ret;
 
Signature · 1 line
2.48.1
author maintainer
#4 lore raw

PATCH 4/37 iio: chemical: atlas-sensor: 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/chemical/atlas-sensor.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/iio/chemical/atlas-sensor.c +3 -4
index baf93e5e3ca7..593b73ccbeb7 100644
@@ -518,13 +518,12 @@ static int atlas_read_raw(struct iio_dev *indio_dev,
 		case IIO_CONCENTRATION:
 		case IIO_ELECTRICALCONDUCTIVITY:
 		case IIO_VOLTAGE:
-			ret = iio_device_claim_direct_mode(indio_dev);
-			if (ret)
-				return ret;
+			if (!iio_device_claim_direct(indio_dev))
+				return -EBUSY;
 
 			ret = atlas_read_measurement(data, chan->address, &reg);
 
-			iio_device_release_direct_mode(indio_dev);
+			iio_device_release_direct(indio_dev);
 			break;
 		default:
 			ret = -EINVAL;
Signature · 1 line
2.48.1
author maintainer
#5 lore raw

PATCH 5/37 iio: chemical: scd4x: 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/chemical/scd4x.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/iio/chemical/scd4x.c +3 -4
index 50e3ac44422b..4877bd3e907b 100644
@@ -358,15 +358,14 @@ static int scd4x_read_raw(struct iio_dev *indio_dev,
 			return IIO_VAL_INT;
 		}
 
-		ret = iio_device_claim_direct_mode(indio_dev);
-		if (ret)
-			return ret;
+		if (!iio_device_claim_direct(indio_dev))
+			return -EBUSY;
 
 		mutex_lock(&state->lock);
 		ret = scd4x_read_channel(state, chan->address);
 		mutex_unlock(&state->lock);
 
-		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
#6 lore raw

PATCH 6/37 iio: common: scmi: 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/common/scmi_sensors/scmi_iio.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/iio/common/scmi_sensors/scmi_iio.c +3 -4
index ed15dcbf4cf6..1a62dd902f05 100644
@@ -351,12 +351,11 @@ static int scmi_iio_read_raw(struct iio_dev *iio_dev,
 		ret = scmi_iio_get_odr_val(iio_dev, val, val2);
 		return ret ? ret : IIO_VAL_INT_PLUS_MICRO;
 	case IIO_CHAN_INFO_RAW:
-		ret = iio_device_claim_direct_mode(iio_dev);
-		if (ret)
-			return ret;
+		if (!iio_device_claim_direct(iio_dev))
+			return -EBUSY;
 
 		ret = scmi_iio_read_channel_data(iio_dev, ch, val, val2);
-		iio_device_release_direct_mode(iio_dev);
+		iio_device_release_direct(iio_dev);
 		return ret;
 	default:
 		return -EINVAL;
Signature · 1 line
2.48.1
author maintainer
#7 lore raw

PATCH 7/37 iio: common: st_sensors: 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/common/st_sensors/st_sensors_core.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/iio/common/st_sensors/st_sensors_core.c +3 -4
index e4f5a7ff7e74..8ce1dccfea4f 100644
@@ -530,9 +530,8 @@ int st_sensors_read_info_raw(struct iio_dev *indio_dev,
 	int err;
 	struct st_sensor_data *sdata = iio_priv(indio_dev);
 
-	err = iio_device_claim_direct_mode(indio_dev);
-	if (err)
-		return err;
+	if (!iio_device_claim_direct(indio_dev))
+		return -EBUSY;
 
 	mutex_lock(&sdata->odr_lock);
 
@@ -551,7 +550,7 @@ int st_sensors_read_info_raw(struct iio_dev *indio_dev,
 
 out:
 	mutex_unlock(&sdata->odr_lock);
-	iio_device_release_direct_mode(indio_dev);
+	iio_device_release_direct(indio_dev);
 
 	return err;
 }
Signature · 1 line
2.48.1
author maintainer
#8 lore raw

PATCH 8/37 iio: gyro: adxrs290: 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/gyro/adxrs290.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
drivers/iio/gyro/adxrs290.c +6 -8
index 223fc181109c..8fcb41f45baa 100644
@@ -290,9 +290,8 @@ static int adxrs290_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;
 
 		switch (chan->type) {
 		case IIO_ANGL_VEL:
@@ -316,7 +315,7 @@ static int adxrs290_read_raw(struct iio_dev *indio_dev,
 			break;
 		}
 
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		return ret;
 	case IIO_CHAN_INFO_SCALE:
 		switch (chan->type) {
@@ -366,9 +365,8 @@ static int adxrs290_write_raw(struct iio_dev *indio_dev,
 	struct adxrs290_state *st = iio_priv(indio_dev);
 	int ret, lpf_idx, hpf_idx;
 
-	ret = iio_device_claim_direct_mode(indio_dev);
-	if (ret)
-		return ret;
+	if (!iio_device_claim_direct(indio_dev))
+		return -EBUSY;
 
 	switch (mask) {
 	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
@@ -408,7 +406,7 @@ static int adxrs290_write_raw(struct iio_dev *indio_dev,
 		break;
 	}
 
-	iio_device_release_direct_mode(indio_dev);
+	iio_device_release_direct(indio_dev);
 	return ret;
 }
 
Signature · 1 line
2.48.1
author maintainer
#9 lore raw

PATCH 9/37 iio: health: max30102: 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 particular case is about ensuring the mode does not change rather than ensuring we are in direct mode. A follow up may cleanup the buffer mode claim.

---
 drivers/iio/health/max30102.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/health/max30102.c +2 -2
index 1d074eb6a8c5..dacc489f7293 100644
@@ -484,11 +484,11 @@ static int max30102_read_raw(struct iio_dev *indio_dev,
 			 * things cannot 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;
 
 			ret = max30102_get_temp(data, val, true);
-			iio_device_release_direct_mode(indio_dev);
+			iio_device_release_direct(indio_dev);
 		} else {
 			ret = max30102_get_temp(data, val, false);
 			iio_device_release_buffer_mode(indio_dev);
Signature · 1 line
2.48.1
author maintainer
#10 lore raw

PATCH 10/37 iio: humidity: hdc100x: Use guard(mutex) to simplify code flow

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

By using autoreleasing on the lock a number of paths can use direct returns allow earlier exit from functions.

---
 drivers/iio/humidity/hdc100x.c | 69 +++++++++++++---------------------
 1 file changed, 27 insertions(+), 42 deletions(-)
drivers/iio/humidity/hdc100x.c +27 -42
index a303f704b7ed..fb8584423ad8 100644
@@ -13,6 +13,7 @@
  * https://www.ti.com/product/HDC1080/datasheet
  */
 
+#include <linux/cleanup.h>
 #include <linux/delay.h>
 #include <linux/module.h>
 #include <linux/mod_devicetable.h>
@@ -206,26 +207,21 @@ static int hdc100x_read_raw(struct iio_dev *indio_dev,
 	case IIO_CHAN_INFO_RAW: {
 		int ret;
 
-		mutex_lock(&data->lock);
+		guard(mutex)(&data->lock);
 		if (chan->type == IIO_CURRENT) {
 			*val = hdc100x_get_heater_status(data);
-			ret = IIO_VAL_INT;
-		} else {
-			ret = iio_device_claim_direct_mode(indio_dev);
-			if (ret) {
-				mutex_unlock(&data->lock);
-				return ret;
-			}
-
-			ret = hdc100x_get_measurement(data, chan);
-			iio_device_release_direct_mode(indio_dev);
-			if (ret >= 0) {
-				*val = ret;
-				ret = IIO_VAL_INT;
-			}
+			return IIO_VAL_INT;
 		}
-		mutex_unlock(&data->lock);
-		return ret;
+		ret = iio_device_claim_direct_mode(indio_dev);
+		if (ret)
+			return ret;
+
+		ret = hdc100x_get_measurement(data, chan);
+		iio_device_release_direct_mode(indio_dev);
+		if (ret < 0)
+			return ret;
+		*val = ret;
+		return IIO_VAL_INT;
 	}
 	case IIO_CHAN_INFO_INT_TIME:
 		*val = 0;
@@ -256,26 +252,23 @@ static int hdc100x_write_raw(struct iio_dev *indio_dev,
 			     int val, int val2, long mask)
 {
 	struct hdc100x_data *data = iio_priv(indio_dev);
-	int ret = -EINVAL;
 
 	switch (mask) {
-	case IIO_CHAN_INFO_INT_TIME:
+	case IIO_CHAN_INFO_INT_TIME: {
 		if (val != 0)
 			return -EINVAL;
 
-		mutex_lock(&data->lock);
-		ret = hdc100x_set_it_time(data, chan->address, val2);
-		mutex_unlock(&data->lock);
-		return ret;
-	case IIO_CHAN_INFO_RAW:
+		guard(mutex)(&data->lock);
+		return hdc100x_set_it_time(data, chan->address, val2);
+	}
+	case IIO_CHAN_INFO_RAW: {
 		if (chan->type != IIO_CURRENT || val2 != 0)
 			return -EINVAL;
 
-		mutex_lock(&data->lock);
-		ret = hdc100x_update_config(data, HDC100X_REG_CONFIG_HEATER_EN,
-					val ? HDC100X_REG_CONFIG_HEATER_EN : 0);
-		mutex_unlock(&data->lock);
-		return ret;
+		guard(mutex)(&data->lock);
+		return hdc100x_update_config(data, HDC100X_REG_CONFIG_HEATER_EN,
+					     val ? HDC100X_REG_CONFIG_HEATER_EN : 0);
+	}
 	default:
 		return -EINVAL;
 	}
@@ -284,27 +277,19 @@ static int hdc100x_write_raw(struct iio_dev *indio_dev,
 static int hdc100x_buffer_postenable(struct iio_dev *indio_dev)
 {
 	struct hdc100x_data *data = iio_priv(indio_dev);
-	int ret;
 
 	/* Buffer is enabled. First set ACQ Mode, then attach poll func */
-	mutex_lock(&data->lock);
-	ret = hdc100x_update_config(data, HDC100X_REG_CONFIG_ACQ_MODE,
-				    HDC100X_REG_CONFIG_ACQ_MODE);
-	mutex_unlock(&data->lock);
-
-	return ret;
+	guard(mutex)(&data->lock);
+	return hdc100x_update_config(data, HDC100X_REG_CONFIG_ACQ_MODE,
+				     HDC100X_REG_CONFIG_ACQ_MODE);
 }
 
 static int hdc100x_buffer_predisable(struct iio_dev *indio_dev)
 {
 	struct hdc100x_data *data = iio_priv(indio_dev);
-	int ret;
 
-	mutex_lock(&data->lock);
-	ret = hdc100x_update_config(data, HDC100X_REG_CONFIG_ACQ_MODE, 0);
-	mutex_unlock(&data->lock);
-
-	return ret;
+	guard(mutex)(&data->lock);
+	return hdc100x_update_config(data, HDC100X_REG_CONFIG_ACQ_MODE, 0);
 }
 
 static const struct iio_buffer_setup_ops hdc_buffer_setup_ops = {
Signature · 1 line
2.48.1
author maintainer
#11 lore raw

PATCH 11/37 iio: humidity: hdc100x: 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/humidity/hdc100x.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/iio/humidity/hdc100x.c +3 -4
index fb8584423ad8..c2b36e682e06 100644
@@ -212,12 +212,11 @@ static int hdc100x_read_raw(struct iio_dev *indio_dev,
 			*val = hdc100x_get_heater_status(data);
 			return IIO_VAL_INT;
 		}
-		ret = iio_device_claim_direct_mode(indio_dev);
-		if (ret)
-			return ret;
+		if (!iio_device_claim_direct(indio_dev))
+			return -EBUSY;
 
 		ret = hdc100x_get_measurement(data, chan);
-		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
#12 lore raw

PATCH 12/37 iio: humidity: hdc2010: 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.

Cc Eugene Zaikonnikov <[email protected]>
---
 drivers/iio/humidity/hdc2010.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
drivers/iio/humidity/hdc2010.c +6 -8
index f5867659e00f..894a8b4ab193 100644
@@ -169,13 +169,12 @@ static int hdc2010_read_raw(struct iio_dev *indio_dev,
 			*val = hdc2010_get_heater_status(data);
 			return IIO_VAL_INT;
 		}
-		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);
 		ret = hdc2010_get_prim_measurement_word(data, chan);
 		mutex_unlock(&data->lock);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		if (ret < 0)
 			return ret;
 		*val = ret;
@@ -184,13 +183,12 @@ static int hdc2010_read_raw(struct iio_dev *indio_dev,
 	case IIO_CHAN_INFO_PEAK: {
 		int ret;
 
-		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);
 		ret = hdc2010_get_peak_measurement_byte(data, chan);
 		mutex_unlock(&data->lock);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		if (ret < 0)
 			return ret;
 		/* Scaling up the value so we can use same offset as RAW */
Signature · 1 line
2.48.1
Acked-by on v1 12/37
#13 lore raw

Re: [PATCH 12/37] iio: humidity: hdc2010: Switch to sparse friendly iio_device_claim/release_direct()

Hi Jonathan,

Thanks for the patch. It looks good to me.

Quoted 50 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.

Cc Eugene Zaikonnikov <[email protected]>
---
 drivers/iio/humidity/hdc2010.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
drivers/iio/humidity/hdc2010.c +6 -8
index f5867659e00f..894a8b4ab193 100644
@@ -169,13 +169,12 @@ static int hdc2010_read_raw(struct iio_dev *indio_dev,
 			*val = hdc2010_get_heater_status(data);
 			return IIO_VAL_INT;
 		}
-		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);
 		ret = hdc2010_get_prim_measurement_word(data, chan);
 		mutex_unlock(&data->lock);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		if (ret < 0)
 			return ret;
 		*val = ret;
@@ -184,13 +183,12 @@ static int hdc2010_read_raw(struct iio_dev *indio_dev,
 	case IIO_CHAN_INFO_PEAK: {
 		int ret;
 
-		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);
 		ret = hdc2010_get_peak_measurement_byte(data, chan);
 		mutex_unlock(&data->lock);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		if (ret < 0)
 			return ret;
 		/* Scaling up the value so we can use same offset as RAW */
Signature · 6 lines
Regards,

  Eugene Zaikonnikov

  Norphonic AS
  Tel: +47 98 23 97 73
author maintainer
#14 lore raw

PATCH 13/37 iio: humidity: hts211: Factor out everything under direct mode claim into helper functions.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Pulling out the functionality of read_raw() and write_raw() callbacks so that only the mode claim is done in the initial call allows for direct returns and simpler error handling in the new __hts211_write_raw() / __hts211_read_raw() functions.

---
 drivers/iio/humidity/hts221_core.c | 89 +++++++++++++++---------------
 1 file changed, 45 insertions(+), 44 deletions(-)
drivers/iio/humidity/hts221_core.c +45 -44
index 0be11470730c..ca4746f2ecba 100644
@@ -418,31 +418,22 @@ static int hts221_read_oneshot(struct hts221_hw *hw, u8 addr, int *val)
 	return IIO_VAL_INT;
 }
 
-static int hts221_read_raw(struct iio_dev *iio_dev,
-			   struct iio_chan_spec const *ch,
-			   int *val, int *val2, long mask)
+static int __hts221_read_raw(struct iio_dev *iio_dev,
+			     struct iio_chan_spec const *ch,
+			     int *val, int *val2, long mask)
 {
 	struct hts221_hw *hw = iio_priv(iio_dev);
-	int ret;
-
-	ret = iio_device_claim_direct_mode(iio_dev);
-	if (ret)
-		return ret;
 
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
-		ret = hts221_read_oneshot(hw, ch->address, val);
-		break;
+		return hts221_read_oneshot(hw, ch->address, val);
 	case IIO_CHAN_INFO_SCALE:
-		ret = hts221_get_sensor_scale(hw, ch->type, val, val2);
-		break;
+		return hts221_get_sensor_scale(hw, ch->type, val, val2);
 	case IIO_CHAN_INFO_OFFSET:
-		ret = hts221_get_sensor_offset(hw, ch->type, val, val2);
-		break;
+		return hts221_get_sensor_offset(hw, ch->type, val, val2);
 	case IIO_CHAN_INFO_SAMP_FREQ:
 		*val = hw->odr;
-		ret = IIO_VAL_INT;
-		break;
+		return IIO_VAL_INT;
 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO: {
 		u8 idx;
 		const struct hts221_avg *avg;
@@ -452,62 +443,72 @@ static int hts221_read_raw(struct iio_dev *iio_dev,
 			avg = &hts221_avg_list[HTS221_SENSOR_H];
 			idx = hw->sensors[HTS221_SENSOR_H].cur_avg_idx;
 			*val = avg->avg_avl[idx];
-			ret = IIO_VAL_INT;
-			break;
+			return IIO_VAL_INT;
 		case IIO_TEMP:
 			avg = &hts221_avg_list[HTS221_SENSOR_T];
 			idx = hw->sensors[HTS221_SENSOR_T].cur_avg_idx;
 			*val = avg->avg_avl[idx];
-			ret = IIO_VAL_INT;
-			break;
+			return IIO_VAL_INT;
 		default:
-			ret = -EINVAL;
-			break;
+			return -EINVAL;
 		}
-		break;
 	}
 	default:
-		ret = -EINVAL;
-		break;
+		return -EINVAL;
 	}
-
-	iio_device_release_direct_mode(iio_dev);
-
-	return ret;
 }
 
-static int hts221_write_raw(struct iio_dev *iio_dev,
-			    struct iio_chan_spec const *chan,
-			    int val, int val2, long mask)
+static int hts221_read_raw(struct iio_dev *iio_dev,
+			   struct iio_chan_spec const *ch,
+			   int *val, int *val2, long mask)
 {
-	struct hts221_hw *hw = iio_priv(iio_dev);
 	int ret;
 
 	ret = iio_device_claim_direct_mode(iio_dev);
 	if (ret)
 		return ret;
 
+	ret = __hts221_read_raw(iio_dev, ch, val, val2, mask);
+
+	iio_device_release_direct_mode(iio_dev);
+
+	return ret;
+}
+
+static int __hts221_write_raw(struct iio_dev *iio_dev,
+			      struct iio_chan_spec const *chan,
+			      int val, long mask)
+{
+	struct hts221_hw *hw = iio_priv(iio_dev);
+
 	switch (mask) {
 	case IIO_CHAN_INFO_SAMP_FREQ:
-		ret = hts221_update_odr(hw, val);
-		break;
+		return hts221_update_odr(hw, val);
 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
 		switch (chan->type) {
 		case IIO_HUMIDITYRELATIVE:
-			ret = hts221_update_avg(hw, HTS221_SENSOR_H, val);
-			break;
+			return hts221_update_avg(hw, HTS221_SENSOR_H, val);
 		case IIO_TEMP:
-			ret = hts221_update_avg(hw, HTS221_SENSOR_T, val);
-			break;
+			return hts221_update_avg(hw, HTS221_SENSOR_T, val);
 		default:
-			ret = -EINVAL;
-			break;
+			return -EINVAL;
 		}
-		break;
 	default:
-		ret = -EINVAL;
-		break;
+		return -EINVAL;
 	}
+}
+
+static int hts221_write_raw(struct iio_dev *iio_dev,
+			    struct iio_chan_spec const *chan,
+			    int val, int val2, long mask)
+{
+	int ret;
+
+	ret = iio_device_claim_direct_mode(iio_dev);
+	if (ret)
+		return ret;
+
+	ret = __hts221_write_raw(iio_dev, chan, val, mask);
 
 	iio_device_release_direct_mode(iio_dev);
 
Signature · 1 line
2.48.1
maintainer Acked-by on v1 13/37
#15 lore raw

Re: [PATCH 13/37] iio: humidity: hts211: Factor out everything under direct mode claim into helper functions.

Quoted 8 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Pulling out the functionality of read_raw() and write_raw() callbacks so that only the mode claim is done in the initial call allows for direct returns and simpler error handling in the new __hts211_write_raw() / __hts211_read_raw() functions.

Quoted 151 lines
---
 drivers/iio/humidity/hts221_core.c | 89 +++++++++++++++---------------
 1 file changed, 45 insertions(+), 44 deletions(-)
drivers/iio/humidity/hts221_core.c +45 -44
index 0be11470730c..ca4746f2ecba 100644
@@ -418,31 +418,22 @@ static int hts221_read_oneshot(struct hts221_hw *hw, u8 addr, int *val)
 	return IIO_VAL_INT;
 }
 
-static int hts221_read_raw(struct iio_dev *iio_dev,
-			   struct iio_chan_spec const *ch,
-			   int *val, int *val2, long mask)
+static int __hts221_read_raw(struct iio_dev *iio_dev,
+			     struct iio_chan_spec const *ch,
+			     int *val, int *val2, long mask)
 {
 	struct hts221_hw *hw = iio_priv(iio_dev);
-	int ret;
-
-	ret = iio_device_claim_direct_mode(iio_dev);
-	if (ret)
-		return ret;
 
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
-		ret = hts221_read_oneshot(hw, ch->address, val);
-		break;
+		return hts221_read_oneshot(hw, ch->address, val);
 	case IIO_CHAN_INFO_SCALE:
-		ret = hts221_get_sensor_scale(hw, ch->type, val, val2);
-		break;
+		return hts221_get_sensor_scale(hw, ch->type, val, val2);
 	case IIO_CHAN_INFO_OFFSET:
-		ret = hts221_get_sensor_offset(hw, ch->type, val, val2);
-		break;
+		return hts221_get_sensor_offset(hw, ch->type, val, val2);
 	case IIO_CHAN_INFO_SAMP_FREQ:
 		*val = hw->odr;
-		ret = IIO_VAL_INT;
-		break;
+		return IIO_VAL_INT;
 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO: {
 		u8 idx;
 		const struct hts221_avg *avg;
@@ -452,62 +443,72 @@ static int hts221_read_raw(struct iio_dev *iio_dev,
 			avg = &hts221_avg_list[HTS221_SENSOR_H];
 			idx = hw->sensors[HTS221_SENSOR_H].cur_avg_idx;
 			*val = avg->avg_avl[idx];
-			ret = IIO_VAL_INT;
-			break;
+			return IIO_VAL_INT;
 		case IIO_TEMP:
 			avg = &hts221_avg_list[HTS221_SENSOR_T];
 			idx = hw->sensors[HTS221_SENSOR_T].cur_avg_idx;
 			*val = avg->avg_avl[idx];
-			ret = IIO_VAL_INT;
-			break;
+			return IIO_VAL_INT;
 		default:
-			ret = -EINVAL;
-			break;
+			return -EINVAL;
 		}
-		break;
 	}
 	default:
-		ret = -EINVAL;
-		break;
+		return -EINVAL;
 	}
-
-	iio_device_release_direct_mode(iio_dev);
-
-	return ret;
 }
 
-static int hts221_write_raw(struct iio_dev *iio_dev,
-			    struct iio_chan_spec const *chan,
-			    int val, int val2, long mask)
+static int hts221_read_raw(struct iio_dev *iio_dev,
+			   struct iio_chan_spec const *ch,
+			   int *val, int *val2, long mask)
 {
-	struct hts221_hw *hw = iio_priv(iio_dev);
 	int ret;
 
 	ret = iio_device_claim_direct_mode(iio_dev);
 	if (ret)
 		return ret;
 
+	ret = __hts221_read_raw(iio_dev, ch, val, val2, mask);
+
+	iio_device_release_direct_mode(iio_dev);
+
+	return ret;
+}
+
+static int __hts221_write_raw(struct iio_dev *iio_dev,
+			      struct iio_chan_spec const *chan,
+			      int val, long mask)
+{
+	struct hts221_hw *hw = iio_priv(iio_dev);
+
 	switch (mask) {
 	case IIO_CHAN_INFO_SAMP_FREQ:
-		ret = hts221_update_odr(hw, val);
-		break;
+		return hts221_update_odr(hw, val);
 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
 		switch (chan->type) {
 		case IIO_HUMIDITYRELATIVE:
-			ret = hts221_update_avg(hw, HTS221_SENSOR_H, val);
-			break;
+			return hts221_update_avg(hw, HTS221_SENSOR_H, val);
 		case IIO_TEMP:
-			ret = hts221_update_avg(hw, HTS221_SENSOR_T, val);
-			break;
+			return hts221_update_avg(hw, HTS221_SENSOR_T, val);
 		default:
-			ret = -EINVAL;
-			break;
+			return -EINVAL;
 		}
-		break;
 	default:
-		ret = -EINVAL;
-		break;
+		return -EINVAL;
 	}
+}
+
+static int hts221_write_raw(struct iio_dev *iio_dev,
+			    struct iio_chan_spec const *chan,
+			    int val, int val2, long mask)
+{
+	int ret;
+
+	ret = iio_device_claim_direct_mode(iio_dev);
+	if (ret)
+		return ret;
+
+	ret = __hts221_write_raw(iio_dev, chan, val, mask);
 
 	iio_device_release_direct_mode(iio_dev);
 
Signature · 1 line
2.48.1
author maintainer
#16 lore raw

PATCH 14/37 iio: humidity: hts211: 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/humidity/hts221_core.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
drivers/iio/humidity/hts221_core.c +6 -8
index ca4746f2ecba..bfeb0a60d3af 100644
@@ -464,13 +464,12 @@ static int hts221_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;
 
 	ret = __hts221_read_raw(iio_dev, ch, val, val2, mask);
 
-	iio_device_release_direct_mode(iio_dev);
+	iio_device_release_direct(iio_dev);
 
 	return ret;
 }
@@ -504,13 +503,12 @@ static int hts221_write_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;
 
 	ret = __hts221_write_raw(iio_dev, chan, val, mask);
 
-	iio_device_release_direct_mode(iio_dev);
+	iio_device_release_direct(iio_dev);
 
 	return ret;
 }
Signature · 1 line
2.48.1
maintainer Acked-by on v1 14/37
#17 lore raw

Re: [PATCH 14/37] iio: humidity: hts211: Switch to sparse friendly iio_device_claim/release_direct()

Quoted 7 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.

Quoted 45 lines
---
 drivers/iio/humidity/hts221_core.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
drivers/iio/humidity/hts221_core.c +6 -8
index ca4746f2ecba..bfeb0a60d3af 100644
@@ -464,13 +464,12 @@ static int hts221_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;
 
 	ret = __hts221_read_raw(iio_dev, ch, val, val2, mask);
 
-	iio_device_release_direct_mode(iio_dev);
+	iio_device_release_direct(iio_dev);
 
 	return ret;
 }
@@ -504,13 +503,12 @@ static int hts221_write_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;
 
 	ret = __hts221_write_raw(iio_dev, chan, val, mask);
 
-	iio_device_release_direct_mode(iio_dev);
+	iio_device_release_direct(iio_dev);
 
 	return ret;
 }
Signature · 1 line
2.48.1
author maintainer
#18 lore raw

PATCH 15/37 iio: imu: inv_icm42600: 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.

Cc Jean-Baptiste Maneyrol <[email protected]>
---
 .../iio/imu/inv_icm42600/inv_icm42600_accel.c | 21 ++++++++-----------
 .../iio/imu/inv_icm42600/inv_icm42600_gyro.c  | 21 ++++++++-----------
 .../iio/imu/inv_icm42600/inv_icm42600_temp.c  |  7 +++----
 3 files changed, 21 insertions(+), 28 deletions(-)
drivers/iio/imu/inv_icm42600/inv_icm42600_accel.c +9 -12
index 388520ec60b5..1a67f5ce1800 100644
@@ -685,11 +685,10 @@ static int inv_icm42600_accel_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 = inv_icm42600_accel_read_sensor(indio_dev, chan, &data);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		if (ret)
 			return ret;
 		*val = data;
@@ -747,20 +746,18 @@ static int inv_icm42600_accel_write_raw(struct iio_dev *indio_dev,
 
 	switch (mask) {
 	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;
 		ret = inv_icm42600_accel_write_scale(indio_dev, val, val2);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		return ret;
 	case IIO_CHAN_INFO_SAMP_FREQ:
 		return inv_icm42600_accel_write_odr(indio_dev, val, val2);
 	case IIO_CHAN_INFO_CALIBBIAS:
-		ret = iio_device_claim_direct_mode(indio_dev);
-		if (ret)
-			return ret;
+		if (!iio_device_claim_direct(indio_dev))
+			return -EBUSY;
 		ret = inv_icm42600_accel_write_offset(st, chan, val, val2);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		return ret;
 	default:
 		return -EINVAL;
drivers/iio/imu/inv_icm42600/inv_icm42600_gyro.c +9 -12
index 591ed78a55bb..70bac8ca28fa 100644
@@ -591,11 +591,10 @@ static int inv_icm42600_gyro_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 = inv_icm42600_gyro_read_sensor(st, chan, &data);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		if (ret)
 			return ret;
 		*val = data;
@@ -653,20 +652,18 @@ static int inv_icm42600_gyro_write_raw(struct iio_dev *indio_dev,
 
 	switch (mask) {
 	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;
 		ret = inv_icm42600_gyro_write_scale(indio_dev, val, val2);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		return ret;
 	case IIO_CHAN_INFO_SAMP_FREQ:
 		return inv_icm42600_gyro_write_odr(indio_dev, val, val2);
 	case IIO_CHAN_INFO_CALIBBIAS:
-		ret = iio_device_claim_direct_mode(indio_dev);
-		if (ret)
-			return ret;
+		if (!iio_device_claim_direct(indio_dev))
+			return -EBUSY;
 		ret = inv_icm42600_gyro_write_offset(st, chan, val, val2);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		return ret;
 	default:
 		return -EINVAL;
drivers/iio/imu/inv_icm42600/inv_icm42600_temp.c +3 -4
index 213cce1c3111..f77645d92efd 100644
@@ -56,11 +56,10 @@ int inv_icm42600_temp_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 = inv_icm42600_temp_read(st, &temp);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		if (ret)
 			return ret;
 		*val = temp;
Signature · 1 line
2.48.1
maintainer Acked-by on v1 15/37
#19 lore raw

Re: [PATCH 15/37] iio: imu: inv_icm42600: Switch to sparse friendly iio_device_claim/release_direct()

Hello Jonathan,

looks good for me, thanks for the patch.

Thanks,
JB

________________________________________
From: Jonathan Cameron <[email protected]>
Sent: Monday, March 31, 2025 14:12
To: [email protected] <[email protected]>
Cc: David Lechner <[email protected]>; Nuno Sá <[email protected]>; Cosmin Tanislav <[email protected]>; Roan van Dijk <[email protected]>; Jyoti Bhayana <[email protected]>; Nishant Malpani <[email protected]>; Eugene Zaikonnikov <[email protected]>; Jean-Baptiste Maneyrol <[email protected]>; Shen Jianping <[email protected]>; Lorenzo Bianconi <[email protected]>; Matti Vaittinen <[email protected]>; Yasin Lee <[email protected]>; Andy Shevchenko <[email protected]>; Jonathan Cameron <[email protected]> Subject: [PATCH 15/37] iio: imu: inv_icm42600: Switch to sparse friendly iio_device_claim/release_direct()

This Message Is From an External Sender
This message came from outside your organization.

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.

Cc Jean-Baptiste Maneyrol <[email protected]>
---
 .../iio/imu/inv_icm42600/inv_icm42600_accel.c | 21 ++++++++-----------
 .../iio/imu/inv_icm42600/inv_icm42600_gyro.c  | 21 ++++++++-----------
 .../iio/imu/inv_icm42600/inv_icm42600_temp.c  |  7 +++----
 3 files changed, 21 insertions(+), 28 deletions(-)
drivers/iio/imu/inv_icm42600/inv_icm42600_accel.c +9 -12
index 388520ec60b5..1a67f5ce1800 100644
@@ -685,11 +685,10 @@ static int inv_icm42600_accel_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 = inv_icm42600_accel_read_sensor(indio_dev, chan, &data);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		if (ret)
 			return ret;
 		*val = data;
@@ -747,20 +746,18 @@ static int inv_icm42600_accel_write_raw(struct iio_dev *indio_dev,
 
 	switch (mask) {
 	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;
 		ret = inv_icm42600_accel_write_scale(indio_dev, val, val2);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		return ret;
 	case IIO_CHAN_INFO_SAMP_FREQ:
 		return inv_icm42600_accel_write_odr(indio_dev, val, val2);
 	case IIO_CHAN_INFO_CALIBBIAS:
-		ret = iio_device_claim_direct_mode(indio_dev);
-		if (ret)
-			return ret;
+		if (!iio_device_claim_direct(indio_dev))
+			return -EBUSY;
 		ret = inv_icm42600_accel_write_offset(st, chan, val, val2);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		return ret;
 	default:
 		return -EINVAL;
drivers/iio/imu/inv_icm42600/inv_icm42600_gyro.c +9 -12
index 591ed78a55bb..70bac8ca28fa 100644
@@ -591,11 +591,10 @@ static int inv_icm42600_gyro_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 = inv_icm42600_gyro_read_sensor(st, chan, &data);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		if (ret)
 			return ret;
 		*val = data;
@@ -653,20 +652,18 @@ static int inv_icm42600_gyro_write_raw(struct iio_dev *indio_dev,
 
 	switch (mask) {
 	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;
 		ret = inv_icm42600_gyro_write_scale(indio_dev, val, val2);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		return ret;
 	case IIO_CHAN_INFO_SAMP_FREQ:
 		return inv_icm42600_gyro_write_odr(indio_dev, val, val2);
 	case IIO_CHAN_INFO_CALIBBIAS:
-		ret = iio_device_claim_direct_mode(indio_dev);
-		if (ret)
-			return ret;
+		if (!iio_device_claim_direct(indio_dev))
+			return -EBUSY;
 		ret = inv_icm42600_gyro_write_offset(st, chan, val, val2);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		return ret;
 	default:
 		return -EINVAL;
drivers/iio/imu/inv_icm42600/inv_icm42600_temp.c +3 -4
index 213cce1c3111..f77645d92efd 100644
@@ -56,11 +56,10 @@ int inv_icm42600_temp_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 = inv_icm42600_temp_read(st, &temp);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		if (ret)
 			return ret;
 		*val = temp;
Signature · 1 line
2.48.1
author maintainer
#20 lore raw

PATCH 16/37 iio: imu: inv_mpu6050: 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.

Cc Jean-Baptiste Maneyrol <[email protected]>
---
 drivers/iio/imu/inv_mpu6050/inv_mpu_core.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
drivers/iio/imu/inv_mpu6050/inv_mpu_core.c +6 -8
index 5bcd5e797046..b8656c02354a 100644
@@ -755,13 +755,12 @@ inv_mpu6050_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;
 		mutex_lock(&st->lock);
 		ret = inv_mpu6050_read_channel_data(indio_dev, chan, val);
 		mutex_unlock(&st->lock);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		return ret;
 	case IIO_CHAN_INFO_SCALE:
 		switch (chan->type) {
@@ -895,9 +894,8 @@ static int inv_mpu6050_write_raw(struct iio_dev *indio_dev,
 	 * we should only update scale when the chip is disabled, i.e.
 	 * not running
 	 */
-	result = iio_device_claim_direct_mode(indio_dev);
-	if (result)
-		return result;
+	if (!iio_device_claim_direct(indio_dev))
+		return -EBUSY;
 
 	mutex_lock(&st->lock);
 	result = pm_runtime_resume_and_get(pdev);
@@ -944,7 +942,7 @@ static int inv_mpu6050_write_raw(struct iio_dev *indio_dev,
 	pm_runtime_put_autosuspend(pdev);
 error_write_raw_unlock:
 	mutex_unlock(&st->lock);
-	iio_device_release_direct_mode(indio_dev);
+	iio_device_release_direct(indio_dev);
 
 	return result;
 }
Signature · 1 line
2.48.1
maintainer Acked-by on v1 16/37
#21 lore raw

Re: [PATCH 16/37] iio: imu: inv_mpu6050: Switch to sparse friendly iio_device_claim/release_direct()

Hello Jonathan,

looks good, thanks for the patch.

Thanks,
JB

________________________________________
From: Jonathan Cameron <[email protected]>
Sent: Monday, March 31, 2025 14:12
To: [email protected] <[email protected]>
Cc: David Lechner <[email protected]>; Nuno Sá <[email protected]>; Cosmin Tanislav <[email protected]>; Roan van Dijk <[email protected]>; Jyoti Bhayana <[email protected]>; Nishant Malpani <[email protected]>; Eugene Zaikonnikov <[email protected]>; Jean-Baptiste Maneyrol <[email protected]>; Shen Jianping <[email protected]>; Lorenzo Bianconi <[email protected]>; Matti Vaittinen <[email protected]>; Yasin Lee <[email protected]>; Andy Shevchenko <[email protected]>; Jonathan Cameron <[email protected]> Subject: [PATCH 16/37] iio: imu: inv_mpu6050: Switch to sparse friendly iio_device_claim/release_direct()

This Message Is From an External Sender
This message came from outside your organization.

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.

Cc Jean-Baptiste Maneyrol <[email protected]>
---
 drivers/iio/imu/inv_mpu6050/inv_mpu_core.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
drivers/iio/imu/inv_mpu6050/inv_mpu_core.c +6 -8
index 5bcd5e797046..b8656c02354a 100644
@@ -755,13 +755,12 @@ inv_mpu6050_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;
 		mutex_lock(&st->lock);
 		ret = inv_mpu6050_read_channel_data(indio_dev, chan, val);
 		mutex_unlock(&st->lock);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		return ret;
 	case IIO_CHAN_INFO_SCALE:
 		switch (chan->type) {
@@ -895,9 +894,8 @@ static int inv_mpu6050_write_raw(struct iio_dev *indio_dev,
 	 * we should only update scale when the chip is disabled, i.e.
 	 * not running
 	 */
-	result = iio_device_claim_direct_mode(indio_dev);
-	if (result)
-		return result;
+	if (!iio_device_claim_direct(indio_dev))
+		return -EBUSY;
 
 	mutex_lock(&st->lock);
 	result = pm_runtime_resume_and_get(pdev);
@@ -944,7 +942,7 @@ static int inv_mpu6050_write_raw(struct iio_dev *indio_dev,
 	pm_runtime_put_autosuspend(pdev);
 error_write_raw_unlock:
 	mutex_unlock(&st->lock);
-	iio_device_release_direct_mode(indio_dev);
+	iio_device_release_direct(indio_dev);
 
 	return result;
 }
Signature · 1 line
2.48.1
author maintainer
#22 lore raw

PATCH 17/37 iio: imu: smi240: 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/imu/smi240.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/iio/imu/smi240.c +3 -4
index 4492c4d013bd..d159ee59acdd 100644
@@ -414,11 +414,10 @@ static int smi240_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 = smi240_get_data(data, chan->type, chan->channel2, val);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		if (ret)
 			return ret;
 		return IIO_VAL_INT;
Signature · 1 line
2.48.1
author maintainer
#23 lore raw

PATCH 18/37 iio: imu: st_lsm6dsx: Factor out parts of st_lsm6dsx_shub_write_raw() to allow direct returns

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

By factoring out all the code that occurs with direct mode claimed to a helper function, that helper function can directly return simplifying code flow.

---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c | 65 +++++++++++---------
 1 file changed, 35 insertions(+), 30 deletions(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c +35 -30
index c1b444520d2a..17a74f5adfc0 100644
@@ -614,53 +614,58 @@ st_lsm6dsx_shub_set_full_scale(struct st_lsm6dsx_sensor *sensor,
 }
 
 static int
-st_lsm6dsx_shub_write_raw(struct iio_dev *iio_dev,
-			  struct iio_chan_spec const *chan,
-			  int val, int val2, long mask)
+__st_lsm6dsx_shub_write_raw(struct iio_dev *iio_dev,
+			    struct iio_chan_spec const *chan,
+			    int val, int val2, long mask)
 {
 	struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
 	int err;
 
-	err = iio_device_claim_direct_mode(iio_dev);
-	if (err)
-		return err;
-
 	switch (mask) {
 	case IIO_CHAN_INFO_SAMP_FREQ: {
+		struct st_lsm6dsx_hw *hw = sensor->hw;
+		struct st_lsm6dsx_sensor *ref_sensor;
+		u8 odr_val;
 		u16 data;
+		int odr;
 
 		val = val * 1000 + val2 / 1000;
 		err = st_lsm6dsx_shub_get_odr_val(sensor, val, &data);
-		if (!err) {
-			struct st_lsm6dsx_hw *hw = sensor->hw;
-			struct st_lsm6dsx_sensor *ref_sensor;
-			u8 odr_val;
-			int odr;
-
-			ref_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_ACC]);
-			odr = st_lsm6dsx_check_odr(ref_sensor, val, &odr_val);
-			if (odr < 0) {
-				err = odr;
-				goto release;
-			}
-
-			sensor->ext_info.slv_odr = val;
-			sensor->odr = odr;
-		}
-		break;
+		if (err)
+			return err;
+
+		ref_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_ACC]);
+		odr = st_lsm6dsx_check_odr(ref_sensor, val, &odr_val);
+		if (odr < 0)
+			return odr;
+
+		sensor->ext_info.slv_odr = val;
+		sensor->odr = odr;
+		return 0;
 	}
 	case IIO_CHAN_INFO_SCALE:
-		err = st_lsm6dsx_shub_set_full_scale(sensor, val2);
-		break;
+		return st_lsm6dsx_shub_set_full_scale(sensor, val2);
 	default:
-		err = -EINVAL;
-		break;
+		return -EINVAL;
 	}
+}
+
+static int
+st_lsm6dsx_shub_write_raw(struct iio_dev *iio_dev,
+			  struct iio_chan_spec const *chan,
+			  int val, int val2, long mask)
+{
+	int ret;
+
+	ret = iio_device_claim_direct_mode(iio_dev);
+	if (ret)
+		return ret;
+
+	ret = __st_lsm6dsx_shub_write_raw(iio_dev, chan, val, val2, mask);
 
-release:
 	iio_device_release_direct_mode(iio_dev);
 
-	return err;
+	return ret;
 }
 
 static ssize_t
Signature · 1 line
2.48.1
reviewer
#24 lore raw

Re: [PATCH 18/37] iio: imu: st_lsm6dsx: Factor out parts of st_lsm6dsx_shub_write_raw() to allow direct returns

Quoted 32 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

By factoring out all the code that occurs with direct mode claimed to a helper function, that helper function can directly return simplifying code flow.

---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c | 65 +++++++++++---------
 1 file changed, 35 insertions(+), 30 deletions(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c +3 -5
index c1b444520d2a..17a74f5adfc0 100644
@@ -614,53 +614,58 @@ st_lsm6dsx_shub_set_full_scale(struct st_lsm6dsx_sensor *sensor,
 }
 
 static int
-st_lsm6dsx_shub_write_raw(struct iio_dev *iio_dev,
-			  struct iio_chan_spec const *chan,
-			  int val, int val2, long mask)
+__st_lsm6dsx_shub_write_raw(struct iio_dev *iio_dev,
+			    struct iio_chan_spec const *chan,
+			    int val, int val2, long mask)
 {
 	struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
 	int err;
 
-	err = iio_device_claim_direct_mode(iio_dev);
-	if (err)

drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c st_lsm6dsx_shub_set_full_scale(struct st_lsm6dsx_sensor *sensor, · line 631

Quoted 10 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c +4 -2
-		return err;
-
 	switch (mask) {
 	case IIO_CHAN_INFO_SAMP_FREQ: {
+		struct st_lsm6dsx_hw *hw = sensor->hw;
+		struct st_lsm6dsx_sensor *ref_sensor;
+		u8 odr_val;
 		u16 data;
+		int odr;
 

I would be tempted to rename err to ret so we don't have to introduce a new odr variable.

maintainer
#25 lore raw

Re: [PATCH 18/37] iio: imu: st_lsm6dsx: Factor out parts of st_lsm6dsx_shub_write_raw() to allow direct returns

Quoted 31 lines · Jonathan Cameron
Quoted 30 lines

From: Jonathan Cameron <[email protected]>

By factoring out all the code that occurs with direct mode claimed to a helper function, that helper function can directly return simplifying code flow.

---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c | 65 +++++++++++---------
 1 file changed, 35 insertions(+), 30 deletions(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c +3 -3
index c1b444520d2a..17a74f5adfc0 100644
@@ -614,53 +614,58 @@ st_lsm6dsx_shub_set_full_scale(struct st_lsm6dsx_sensor *sensor,
 }
 
 static int
-st_lsm6dsx_shub_write_raw(struct iio_dev *iio_dev,
-			  struct iio_chan_spec const *chan,
-			  int val, int val2, long mask)
+__st_lsm6dsx_shub_write_raw(struct iio_dev *iio_dev,
+			    struct iio_chan_spec const *chan,
+			    int val, int val2, long mask)
 {
 	struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
 	int err;
 
Quoted 14 lines

drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c st_lsm6dsx_shub_set_full_scale(struct st_lsm6dsx_sensor *sensor, · line 631

Quoted 12 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c +4 -4
-	err = iio_device_claim_direct_mode(iio_dev);
-	if (err)
-		return err;
-
 	switch (mask) {
 	case IIO_CHAN_INFO_SAMP_FREQ: {
+		struct st_lsm6dsx_hw *hw = sensor->hw;
+		struct st_lsm6dsx_sensor *ref_sensor;
+		u8 odr_val;
 		u16 data;
+		int odr;
 

I would be tempted to rename err to ret so we don't have to introduce a new odr variable.

I guess keeping odr variable makes the code more readable.

Regards,
Lorenzo

author maintainer
#26 lore raw

Re: [PATCH 18/37] iio: imu: st_lsm6dsx: Factor out parts of st_lsm6dsx_shub_write_raw() to allow direct returns

Quoted 46 lines · On Tue, 1 Apr 2025 08:28:34 +0200 Lorenzo Bianconi <[email protected]> wrote:
Quoted 31 lines · Jonathan Cameron
Quoted 30 lines

From: Jonathan Cameron <[email protected]>

By factoring out all the code that occurs with direct mode claimed to a helper function, that helper function can directly return simplifying code flow.

---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c | 65 +++++++++++---------
 1 file changed, 35 insertions(+), 30 deletions(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c +3 -3
index c1b444520d2a..17a74f5adfc0 100644
@@ -614,53 +614,58 @@ st_lsm6dsx_shub_set_full_scale(struct st_lsm6dsx_sensor *sensor,
 }
 
 static int
-st_lsm6dsx_shub_write_raw(struct iio_dev *iio_dev,
-			  struct iio_chan_spec const *chan,
-			  int val, int val2, long mask)
+__st_lsm6dsx_shub_write_raw(struct iio_dev *iio_dev,
+			    struct iio_chan_spec const *chan,
+			    int val, int val2, long mask)
 {
 	struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
 	int err;
 
Quoted 14 lines

drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c st_lsm6dsx_shub_set_full_scale(struct st_lsm6dsx_sensor *sensor, · line 631

Quoted 12 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c +4 -4
-	err = iio_device_claim_direct_mode(iio_dev);
-	if (err)
-		return err;
-
 	switch (mask) {
 	case IIO_CHAN_INFO_SAMP_FREQ: {
+		struct st_lsm6dsx_hw *hw = sensor->hw;
+		struct st_lsm6dsx_sensor *ref_sensor;
+		u8 odr_val;
 		u16 data;
+		int odr;
 

I would be tempted to rename err to ret so we don't have to introduce a new odr variable.

Quoted 1 line

I guess keeping odr variable makes the code more readable.

Hmm. I'm not sure either way. Will keep this as a potential follow up.

Jonathan

Quoted 4 lines

Regards,
Lorenzo

maintainer Acked-by on v1 18/37
#27 lore raw

Re: [PATCH 18/37] iio: imu: st_lsm6dsx: Factor out parts of st_lsm6dsx_shub_write_raw() to allow direct returns

In reply to Jonathan Cameron
Quoted 8 lines

From: Jonathan Cameron <[email protected]>

By factoring out all the code that occurs with direct mode claimed to a helper function, that helper function can directly return simplifying code flow.

Quoted 100 lines
---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c | 65 +++++++++++---------
 1 file changed, 35 insertions(+), 30 deletions(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c +35 -30
index c1b444520d2a..17a74f5adfc0 100644
@@ -614,53 +614,58 @@ st_lsm6dsx_shub_set_full_scale(struct st_lsm6dsx_sensor *sensor,
 }
 
 static int
-st_lsm6dsx_shub_write_raw(struct iio_dev *iio_dev,
-			  struct iio_chan_spec const *chan,
-			  int val, int val2, long mask)
+__st_lsm6dsx_shub_write_raw(struct iio_dev *iio_dev,
+			    struct iio_chan_spec const *chan,
+			    int val, int val2, long mask)
 {
 	struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
 	int err;
 
-	err = iio_device_claim_direct_mode(iio_dev);
-	if (err)
-		return err;
-
 	switch (mask) {
 	case IIO_CHAN_INFO_SAMP_FREQ: {
+		struct st_lsm6dsx_hw *hw = sensor->hw;
+		struct st_lsm6dsx_sensor *ref_sensor;
+		u8 odr_val;
 		u16 data;
+		int odr;
 
 		val = val * 1000 + val2 / 1000;
 		err = st_lsm6dsx_shub_get_odr_val(sensor, val, &data);
-		if (!err) {
-			struct st_lsm6dsx_hw *hw = sensor->hw;
-			struct st_lsm6dsx_sensor *ref_sensor;
-			u8 odr_val;
-			int odr;
-
-			ref_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_ACC]);
-			odr = st_lsm6dsx_check_odr(ref_sensor, val, &odr_val);
-			if (odr < 0) {
-				err = odr;
-				goto release;
-			}
-
-			sensor->ext_info.slv_odr = val;
-			sensor->odr = odr;
-		}
-		break;
+		if (err)
+			return err;
+
+		ref_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_ACC]);
+		odr = st_lsm6dsx_check_odr(ref_sensor, val, &odr_val);
+		if (odr < 0)
+			return odr;
+
+		sensor->ext_info.slv_odr = val;
+		sensor->odr = odr;
+		return 0;
 	}
 	case IIO_CHAN_INFO_SCALE:
-		err = st_lsm6dsx_shub_set_full_scale(sensor, val2);
-		break;
+		return st_lsm6dsx_shub_set_full_scale(sensor, val2);
 	default:
-		err = -EINVAL;
-		break;
+		return -EINVAL;
 	}
+}
+
+static int
+st_lsm6dsx_shub_write_raw(struct iio_dev *iio_dev,
+			  struct iio_chan_spec const *chan,
+			  int val, int val2, long mask)
+{
+	int ret;
+
+	ret = iio_device_claim_direct_mode(iio_dev);
+	if (ret)
+		return ret;
+
+	ret = __st_lsm6dsx_shub_write_raw(iio_dev, chan, val, val2, mask);
 
-release:
 	iio_device_release_direct_mode(iio_dev);
 
-	return err;
+	return ret;
 }
 
 static ssize_t
Signature · 1 line
2.48.1
author maintainer
#28 lore raw

PATCH 19/37 iio: imu: st_lsm6dsx: Switch to sparse friendly claim/release_direct()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

This driver caused a false positive with __cond_lock() style solution but is fine with the simple boolean return approach now used.

---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +7 -9
index 4fdcc2acc94e..670cd217eb50 100644
@@ -1804,12 +1804,11 @@ static int st_lsm6dsx_read_raw(struct iio_dev *iio_dev,
 
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
-		ret = iio_device_claim_direct_mode(iio_dev);
-		if (ret)
-			break;
+		if (!iio_device_claim_direct(iio_dev))
+			return -EBUSY;
 
 		ret = st_lsm6dsx_read_oneshot(sensor, ch->address, val);
-		iio_device_release_direct_mode(iio_dev);
+		iio_device_release_direct(iio_dev);
 		break;
 	case IIO_CHAN_INFO_SAMP_FREQ:
 		*val = sensor->odr / 1000;
@@ -1834,11 +1833,10 @@ static int st_lsm6dsx_write_raw(struct iio_dev *iio_dev,
 				int val, int val2, long mask)
 {
 	struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
-	int err;
+	int err = 0;
 
-	err = iio_device_claim_direct_mode(iio_dev);
-	if (err)
-		return err;
+	if (!iio_device_claim_direct(iio_dev))
+		return -EBUSY;
 
 	switch (mask) {
 	case IIO_CHAN_INFO_SCALE:
@@ -1860,7 +1858,7 @@ static int st_lsm6dsx_write_raw(struct iio_dev *iio_dev,
 		break;
 	}
 
-	iio_device_release_direct_mode(iio_dev);
+	iio_device_release_direct(iio_dev);
 
 	return err;
 }
Signature · 1 line
2.48.1
maintainer Acked-by on v1 19/37
#29 lore raw

Re: [PATCH 19/37] iio: imu: st_lsm6dsx: Switch to sparse friendly claim/release_direct()

Quoted 7 lines

From: Jonathan Cameron <[email protected]>

This driver caused a false positive with __cond_lock() style solution but is fine with the simple boolean return approach now used.

Quoted 51 lines
---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +7 -9
index 4fdcc2acc94e..670cd217eb50 100644
@@ -1804,12 +1804,11 @@ static int st_lsm6dsx_read_raw(struct iio_dev *iio_dev,
 
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
-		ret = iio_device_claim_direct_mode(iio_dev);
-		if (ret)
-			break;
+		if (!iio_device_claim_direct(iio_dev))
+			return -EBUSY;
 
 		ret = st_lsm6dsx_read_oneshot(sensor, ch->address, val);
-		iio_device_release_direct_mode(iio_dev);
+		iio_device_release_direct(iio_dev);
 		break;
 	case IIO_CHAN_INFO_SAMP_FREQ:
 		*val = sensor->odr / 1000;
@@ -1834,11 +1833,10 @@ static int st_lsm6dsx_write_raw(struct iio_dev *iio_dev,
 				int val, int val2, long mask)
 {
 	struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
-	int err;
+	int err = 0;
 
-	err = iio_device_claim_direct_mode(iio_dev);
-	if (err)
-		return err;
+	if (!iio_device_claim_direct(iio_dev))
+		return -EBUSY;
 
 	switch (mask) {
 	case IIO_CHAN_INFO_SCALE:
@@ -1860,7 +1858,7 @@ static int st_lsm6dsx_write_raw(struct iio_dev *iio_dev,
 		break;
 	}
 
-	iio_device_release_direct_mode(iio_dev);
+	iio_device_release_direct(iio_dev);
 
 	return err;
 }
Signature · 1 line
2.48.1
author maintainer
#30 lore raw

PATCH 20/37 iio: imu: st_lsm6dsx: 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/imu/st_lsm6dsx/st_lsm6dsx_shub.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c +6 -8
index 17a74f5adfc0..3c5e65dc0f97 100644
@@ -558,12 +558,11 @@ st_lsm6dsx_shub_read_raw(struct iio_dev *iio_dev,
 
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
-		ret = iio_device_claim_direct_mode(iio_dev);
-		if (ret)
-			break;
+		if (!iio_device_claim_direct(iio_dev))
+			return -EBUSY;
 
 		ret = st_lsm6dsx_shub_read_oneshot(sensor, ch, val);
-		iio_device_release_direct_mode(iio_dev);
+		iio_device_release_direct(iio_dev);
 		break;
 	case IIO_CHAN_INFO_SAMP_FREQ:
 		*val = sensor->ext_info.slv_odr / 1000;
@@ -657,13 +656,12 @@ st_lsm6dsx_shub_write_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;
 
 	ret = __st_lsm6dsx_shub_write_raw(iio_dev, chan, val, val2, mask);
 
-	iio_device_release_direct_mode(iio_dev);
+	iio_device_release_direct(iio_dev);
 
 	return ret;
 }
Signature · 1 line
2.48.1
maintainer Acked-by on v1 20/37
#31 lore raw

Re: [PATCH 20/37] iio: imu: st_lsm6dsx: Switch to sparse friendly iio_device_claim/release_direct()

Quoted 8 lines

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.

Quoted 44 lines
---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c +6 -8
index 17a74f5adfc0..3c5e65dc0f97 100644
@@ -558,12 +558,11 @@ st_lsm6dsx_shub_read_raw(struct iio_dev *iio_dev,
 
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
-		ret = iio_device_claim_direct_mode(iio_dev);
-		if (ret)
-			break;
+		if (!iio_device_claim_direct(iio_dev))
+			return -EBUSY;
 
 		ret = st_lsm6dsx_shub_read_oneshot(sensor, ch, val);
-		iio_device_release_direct_mode(iio_dev);
+		iio_device_release_direct(iio_dev);
 		break;
 	case IIO_CHAN_INFO_SAMP_FREQ:
 		*val = sensor->ext_info.slv_odr / 1000;
@@ -657,13 +656,12 @@ st_lsm6dsx_shub_write_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;
 
 	ret = __st_lsm6dsx_shub_write_raw(iio_dev, chan, val, val2, mask);
 
-	iio_device_release_direct_mode(iio_dev);
+	iio_device_release_direct(iio_dev);
 
 	return ret;
 }
Signature · 1 line
2.48.1
author maintainer
#32 lore raw

PATCH 21/37 iio: magnetometer: mag3110: Factor out core of read/write_raw() and use guard() to simplify code flow.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

The combination of guard(mutex) and factoring out sections of code that occur with the device held in direct mode simplifies code flow.

---
 drivers/iio/magnetometer/mag3110.c | 153 ++++++++++++++---------------
 1 file changed, 75 insertions(+), 78 deletions(-)
drivers/iio/magnetometer/mag3110.c +75 -78
index 2fe8e97f2cf8..b633bdf793ed 100644
@@ -9,6 +9,7 @@
  * TODO: irq, user offset, oversampling, continuous mode
  */
 
+#include <linux/cleanup.h>
 #include <linux/module.h>
 #include <linux/i2c.h>
 #include <linux/iio/iio.h>
@@ -102,17 +103,12 @@ static int mag3110_read(struct mag3110_data *data, __be16 buf[3])
 {
 	int ret;
 
-	mutex_lock(&data->lock);
+	guard(mutex)(&data->lock);
 	ret = mag3110_request(data);
-	if (ret < 0) {
-		mutex_unlock(&data->lock);
+	if (ret < 0)
 		return ret;
-	}
-	ret = i2c_smbus_read_i2c_block_data(data->client,
-		MAG3110_OUT_X, 3 * sizeof(__be16), (u8 *) buf);
-	mutex_unlock(&data->lock);
-
-	return ret;
+	return i2c_smbus_read_i2c_block_data(data->client, MAG3110_OUT_X,
+					     3 * sizeof(__be16), (u8 *) buf);
 }
 
 static ssize_t mag3110_show_int_plus_micros(char *buf,
@@ -231,19 +227,17 @@ static int mag3110_change_config(struct mag3110_data *data, u8 reg, u8 val)
 	int ret;
 	int is_active;
 
-	mutex_lock(&data->lock);
+	guard(mutex)(&data->lock);
 
 	is_active = mag3110_is_active(data);
-	if (is_active < 0) {
-		ret = is_active;
-		goto fail;
-	}
+	if (is_active < 0)
+		return is_active;
 
 	/* config can only be changed when in standby */
 	if (is_active > 0) {
 		ret = mag3110_standby(data);
 		if (ret < 0)
-			goto fail;
+			return ret;
 	}
 
 	/*
@@ -252,23 +246,52 @@ static int mag3110_change_config(struct mag3110_data *data, u8 reg, u8 val)
 	 */
 	ret = mag3110_wait_standby(data);
 	if (ret < 0)
-		goto fail;
+		return ret;
 
 	ret = i2c_smbus_write_byte_data(data->client, reg, val);
 	if (ret < 0)
-		goto fail;
+		return ret;
 
 	if (is_active > 0) {
 		ret = mag3110_active(data);
 		if (ret < 0)
-			goto fail;
+			return ret;
 	}
 
-	ret = 0;
-fail:
-	mutex_unlock(&data->lock);
+	return 0;
+}
 
-	return ret;
+static int __mag3110_read_info_raw(struct mag3110_data *data,
+				   struct iio_chan_spec const *chan,
+				   int *val)
+{
+	__be16 buffer[3];
+	int ret;
+
+	switch (chan->type) {
+	case IIO_MAGN: /* in 0.1 uT / LSB */
+		ret = mag3110_read(data, buffer);
+		if (ret < 0)
+			return ret;
+		*val = sign_extend32(be16_to_cpu(buffer[chan->scan_index]),
+				     chan->scan_type.realbits - 1);
+		return IIO_VAL_INT;
+
+	case IIO_TEMP: { /* in 1 C / LSB */
+		guard(mutex)(&data->lock);
+		ret = mag3110_request(data);
+		if (ret < 0)
+			return ret;
+		ret = i2c_smbus_read_byte_data(data->client,
+					       MAG3110_DIE_TEMP);
+		if (ret < 0)
+			return ret;
+		*val = sign_extend32(ret, chan->scan_type.realbits - 1);
+		return IIO_VAL_INT;
+	}
+	default:
+		return -EINVAL;
+	}
 }
 
 static int mag3110_read_raw(struct iio_dev *indio_dev,
@@ -276,7 +299,6 @@ static int mag3110_read_raw(struct iio_dev *indio_dev,
 			    int *val, int *val2, long mask)
 {
 	struct mag3110_data *data = iio_priv(indio_dev);
-	__be16 buffer[3];
 	int i, ret;
 
 	switch (mask) {
@@ -284,37 +306,7 @@ static int mag3110_read_raw(struct iio_dev *indio_dev,
 		ret = iio_device_claim_direct_mode(indio_dev);
 		if (ret)
 			return ret;
-
-		switch (chan->type) {
-		case IIO_MAGN: /* in 0.1 uT / LSB */
-			ret = mag3110_read(data, buffer);
-			if (ret < 0)
-				goto release;
-			*val = sign_extend32(
-				be16_to_cpu(buffer[chan->scan_index]),
-					    chan->scan_type.realbits - 1);
-			ret = IIO_VAL_INT;
-			break;
-		case IIO_TEMP: /* in 1 C / LSB */
-			mutex_lock(&data->lock);
-			ret = mag3110_request(data);
-			if (ret < 0) {
-				mutex_unlock(&data->lock);
-				goto release;
-			}
-			ret = i2c_smbus_read_byte_data(data->client,
-				MAG3110_DIE_TEMP);
-			mutex_unlock(&data->lock);
-			if (ret < 0)
-				goto release;
-			*val = sign_extend32(ret,
-					     chan->scan_type.realbits - 1);
-			ret = IIO_VAL_INT;
-			break;
-		default:
-			ret = -EINVAL;
-		}
-release:
+		ret = __mag3110_read_info_raw(data, chan, val);
 		iio_device_release_direct_mode(indio_dev);
 		return ret;
 
@@ -346,24 +338,18 @@ static int mag3110_read_raw(struct iio_dev *indio_dev,
 	return -EINVAL;
 }
 
-static int mag3110_write_raw(struct iio_dev *indio_dev,
-			     struct iio_chan_spec const *chan,
-			     int val, int val2, long mask)
+static int __mag3110_write_raw(struct mag3110_data *data,
+			      struct iio_chan_spec const *chan,
+			      int val, int val2, long mask)
 {
-	struct mag3110_data *data = iio_priv(indio_dev);
-	int rate, ret;
-
-	ret = iio_device_claim_direct_mode(indio_dev);
-	if (ret)
-		return ret;
+	int rate;
 
 	switch (mask) {
 	case IIO_CHAN_INFO_SAMP_FREQ:
 		rate = mag3110_get_samp_freq_index(data, val, val2);
-		if (rate < 0) {
-			ret = -EINVAL;
-			break;
-		}
+		if (rate < 0)
+			return -EINVAL;
+
 		data->ctrl_reg1 &= 0xff & ~MAG3110_CTRL_DR_MASK
 					& ~MAG3110_CTRL_AC;
 		data->ctrl_reg1 |= rate << MAG3110_CTRL_DR_SHIFT;
@@ -371,22 +357,33 @@ static int mag3110_write_raw(struct iio_dev *indio_dev,
 		if (data->sleep_val < 40)
 			data->ctrl_reg1 |= MAG3110_CTRL_AC;
 
-		ret = mag3110_change_config(data, MAG3110_CTRL_REG1,
-					    data->ctrl_reg1);
-		break;
+		return mag3110_change_config(data, MAG3110_CTRL_REG1,
+					     data->ctrl_reg1);
+
 	case IIO_CHAN_INFO_CALIBBIAS:
-		if (val < -10000 || val > 10000) {
-			ret = -EINVAL;
-			break;
-		}
-		ret = i2c_smbus_write_word_swapped(data->client,
+		if (val < -10000 || val > 10000)
+			return -EINVAL;
+
+		return i2c_smbus_write_word_swapped(data->client,
 			MAG3110_OFF_X + 2 * chan->scan_index, val << 1);
-		break;
 	default:
-		ret = -EINVAL;
-		break;
+		return -EINVAL;
 	}
+}
+
+static int mag3110_write_raw(struct iio_dev *indio_dev,
+			     struct iio_chan_spec const *chan,
+			     int val, int val2, long mask)
+{
+	struct mag3110_data *data = iio_priv(indio_dev);
+	int ret;
+
+	ret = iio_device_claim_direct_mode(indio_dev);
+	if (ret)
+		return ret;
+	ret = __mag3110_write_raw(data, chan, val, val2, mask);
 	iio_device_release_direct_mode(indio_dev);
+
 	return ret;
 }
 
Signature · 1 line
2.48.1
author maintainer
#33 lore raw

PATCH 22/37 iio: magnetometer: mag3110: 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/magnetometer/mag3110.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
drivers/iio/magnetometer/mag3110.c +6 -8
index b633bdf793ed..92d4511ed372 100644
@@ -303,11 +303,10 @@ static int mag3110_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 = __mag3110_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:
@@ -378,11 +377,10 @@ static int mag3110_write_raw(struct iio_dev *indio_dev,
 	struct mag3110_data *data = iio_priv(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 = __mag3110_write_raw(data, 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
#34 lore raw

PATCH 23/37 iio: magnetometer: rm3100: 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/magnetometer/rm3100-core.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/iio/magnetometer/rm3100-core.c +3 -4
index c99694a77a14..e5162ee64e01 100644
@@ -399,12 +399,11 @@ static int rm3100_read_raw(struct iio_dev *indio_dev,
 
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
-		ret = iio_device_claim_direct_mode(indio_dev);
-		if (ret < 0)
-			return ret;
+		if (!iio_device_claim_direct(indio_dev))
+			return -EBUSY;
 
 		ret = rm3100_read_mag(data, chan->scan_index, val);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 
 		return ret;
 	case IIO_CHAN_INFO_SCALE:
Signature · 1 line
2.48.1
author maintainer
#35 lore raw

PATCH 24/37 iio: pressure: dlhl60d: 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/pressure/dlhl60d.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/iio/pressure/dlhl60d.c +3 -4
index e99e97ea6300..e0aa12949cde 100644
@@ -147,12 +147,11 @@ static int dlh_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 = dlh_read_direct(st, &pressure, &temperature);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		if (ret)
 			return ret;
 
Signature · 1 line
2.48.1
author maintainer
#36 lore raw

PATCH 25/37 iio: pressure: icp10100: 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/pressure/icp10100.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
drivers/iio/pressure/icp10100.c +6 -9
index 3e0bf5d31ad7..1951c1cc84cf 100644
@@ -343,9 +343,8 @@ static int icp10100_read_raw_measures(struct iio_dev *indio_dev,
 	uint32_t pressure_mPa;
 	int ret;
 
-	ret = iio_device_claim_direct_mode(indio_dev);
-	if (ret)
-		return ret;
+	if (!iio_device_claim_direct(indio_dev))
+		return -EBUSY;
 
 	ret = icp10100_get_measures(st, &raw_pressure, &raw_temp);
 	if (ret)
@@ -370,7 +369,7 @@ static int icp10100_read_raw_measures(struct iio_dev *indio_dev,
 	}
 
 error_release:
-	iio_device_release_direct_mode(indio_dev);
+	iio_device_release_direct(indio_dev);
 	return ret;
 }
 
@@ -439,7 +438,6 @@ static int icp10100_write_raw(struct iio_dev *indio_dev,
 {
 	struct icp10100_state *st = iio_priv(indio_dev);
 	unsigned int mode;
-	int ret;
 
 	switch (mask) {
 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
@@ -449,13 +447,12 @@ static int icp10100_write_raw(struct iio_dev *indio_dev,
 		mode = ilog2(val);
 		if (mode >= ICP10100_MODE_NB)
 			return -EINVAL;
-		ret = iio_device_claim_direct_mode(indio_dev);
-		if (ret)
-			return ret;
+		if (!iio_device_claim_direct(indio_dev))
+			return -EBUSY;
 		mutex_lock(&st->lock);
 		st->mode = mode;
 		mutex_unlock(&st->lock);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		return 0;
 	default:
 		return -EINVAL;
Signature · 1 line
2.48.1
maintainer Acked-by on v1 25/37
#37 lore raw

Re: [PATCH 25/37] iio: pressure: icp10100: Switch to sparse friendly iio_device_claim/release_direct()

Hello Jonathan,

looks good, thanks for the patch.

Thanks,
JB

________________________________________
From: Jonathan Cameron <[email protected]>
Sent: Monday, March 31, 2025 14:13
To: [email protected] <[email protected]>
Cc: David Lechner <[email protected]>; Nuno Sá <[email protected]>; Cosmin Tanislav <[email protected]>; Roan van Dijk <[email protected]>; Jyoti Bhayana <[email protected]>; Nishant Malpani <[email protected]>; Eugene Zaikonnikov <[email protected]>; Jean-Baptiste Maneyrol <[email protected]>; Shen Jianping <[email protected]>; Lorenzo Bianconi <[email protected]>; Matti Vaittinen <[email protected]>; Yasin Lee <[email protected]>; Andy Shevchenko <[email protected]>; Jonathan Cameron <[email protected]> Subject: [PATCH 25/37] iio: pressure: icp10100: Switch to sparse friendly iio_device_claim/release_direct()

This Message Is From an External Sender
This message came from outside your organization.

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/pressure/icp10100.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
drivers/iio/pressure/icp10100.c +6 -9
index 3e0bf5d31ad7..1951c1cc84cf 100644
@@ -343,9 +343,8 @@ static int icp10100_read_raw_measures(struct iio_dev *indio_dev,
 	uint32_t pressure_mPa;
 	int ret;
 
-	ret = iio_device_claim_direct_mode(indio_dev);
-	if (ret)
-		return ret;
+	if (!iio_device_claim_direct(indio_dev))
+		return -EBUSY;
 
 	ret = icp10100_get_measures(st, &raw_pressure, &raw_temp);
 	if (ret)
@@ -370,7 +369,7 @@ static int icp10100_read_raw_measures(struct iio_dev *indio_dev,
 	}
 
 error_release:
-	iio_device_release_direct_mode(indio_dev);
+	iio_device_release_direct(indio_dev);
 	return ret;
 }
 
@@ -439,7 +438,6 @@ static int icp10100_write_raw(struct iio_dev *indio_dev,
 {
 	struct icp10100_state *st = iio_priv(indio_dev);
 	unsigned int mode;
-	int ret;
 
 	switch (mask) {
 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
@@ -449,13 +447,12 @@ static int icp10100_write_raw(struct iio_dev *indio_dev,
 		mode = ilog2(val);
 		if (mode >= ICP10100_MODE_NB)
 			return -EINVAL;
-		ret = iio_device_claim_direct_mode(indio_dev);
-		if (ret)
-			return ret;
+		if (!iio_device_claim_direct(indio_dev))
+			return -EBUSY;
 		mutex_lock(&st->lock);
 		st->mode = mode;
 		mutex_unlock(&st->lock);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		return 0;
 	default:
 		return -EINVAL;
Signature · 1 line
2.48.1
author maintainer
#38 lore raw

PATCH 26/37 iio: pressure: mpl3115: factor out core of IIO_INFO_RAW read to simplify code flow

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Apply guard(mutex) to remove the need for manual release of the lock. Factor out the code that occurs under the direct claim. These two changes allow for direct returns simplifying code flow.

---
 drivers/iio/pressure/mpl3115.c | 90 ++++++++++++++++++----------------
 1 file changed, 47 insertions(+), 43 deletions(-)
drivers/iio/pressure/mpl3115.c +47 -43
index 71ded2eee060..207031b5ff72 100644
@@ -69,6 +69,52 @@ static int mpl3115_request(struct mpl3115_data *data)
 	return 0;
 }
 
+static int mpl3115_read_info_raw(struct mpl3115_data *data,
+				 struct iio_chan_spec const *chan, int *val)
+{
+	int ret;
+
+	switch (chan->type) {
+	case IIO_PRESSURE: { /* in 0.25 pascal / LSB */
+		__be32 tmp = 0;
+
+		guard(mutex)(&data->lock);
+		ret = mpl3115_request(data);
+		if (ret < 0)
+			return ret;
+
+		ret = i2c_smbus_read_i2c_block_data(data->client,
+						    MPL3115_OUT_PRESS,
+						    3, (u8 *) &tmp);
+		if (ret < 0)
+			return ret;
+
+		*val = be32_to_cpu(tmp) >> chan->scan_type.shift;
+		return IIO_VAL_INT;
+	}
+	case IIO_TEMP: { /* in 0.0625 celsius / LSB */
+		__be16 tmp;
+
+		guard(mutex)(&data->lock);
+		ret = mpl3115_request(data);
+		if (ret < 0)
+			return ret;
+
+		ret = i2c_smbus_read_i2c_block_data(data->client,
+						    MPL3115_OUT_TEMP,
+						    2, (u8 *) &tmp);
+		if (ret < 0)
+			return ret;
+
+		*val = sign_extend32(be16_to_cpu(tmp) >> chan->scan_type.shift,
+				     chan->scan_type.realbits - 1);
+		return IIO_VAL_INT;
+	}
+	default:
+		return -EINVAL;
+	}
+}
+
 static int mpl3115_read_raw(struct iio_dev *indio_dev,
 			    struct iio_chan_spec const *chan,
 			    int *val, int *val2, long mask)
@@ -82,49 +128,7 @@ static int mpl3115_read_raw(struct iio_dev *indio_dev,
 		if (ret)
 			return ret;
 
-		switch (chan->type) {
-		case IIO_PRESSURE: { /* in 0.25 pascal / LSB */
-			__be32 tmp = 0;
-
-			mutex_lock(&data->lock);
-			ret = mpl3115_request(data);
-			if (ret < 0) {
-				mutex_unlock(&data->lock);
-				break;
-			}
-			ret = i2c_smbus_read_i2c_block_data(data->client,
-				MPL3115_OUT_PRESS, 3, (u8 *) &tmp);
-			mutex_unlock(&data->lock);
-			if (ret < 0)
-				break;
-			*val = be32_to_cpu(tmp) >> chan->scan_type.shift;
-			ret = IIO_VAL_INT;
-			break;
-		}
-		case IIO_TEMP: { /* in 0.0625 celsius / LSB */
-			__be16 tmp;
-
-			mutex_lock(&data->lock);
-			ret = mpl3115_request(data);
-			if (ret < 0) {
-				mutex_unlock(&data->lock);
-				break;
-			}
-			ret = i2c_smbus_read_i2c_block_data(data->client,
-				MPL3115_OUT_TEMP, 2, (u8 *) &tmp);
-			mutex_unlock(&data->lock);
-			if (ret < 0)
-				break;
-			*val = sign_extend32(be16_to_cpu(tmp) >> chan->scan_type.shift,
-					     chan->scan_type.realbits - 1);
-			ret = IIO_VAL_INT;
-			break;
-		}
-		default:
-			ret = -EINVAL;
-			break;
-		}
-
+		ret = mpl3115_read_info_raw(data, chan, val);
 		iio_device_release_direct_mode(indio_dev);
 		return ret;
 
Signature · 1 line
2.48.1
author maintainer
#39 lore raw

PATCH 27/37 iio: pressure: mpl3115: 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/pressure/mpl3115.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/iio/pressure/mpl3115.c +3 -4
index 207031b5ff72..8397155555bd 100644
@@ -124,12 +124,11 @@ static int mpl3115_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 = mpl3115_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:
Signature · 1 line
2.48.1
author maintainer
#40 lore raw

PATCH 28/37 iio: pressure: ms5611: 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/pressure/ms5611_core.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/iio/pressure/ms5611_core.c +3 -5
index 00c077b2a2a4..bdac27bd5a5d 100644
@@ -308,7 +308,6 @@ static int ms5611_write_raw(struct iio_dev *indio_dev,
 {
 	struct ms5611_state *st = iio_priv(indio_dev);
 	const struct ms5611_osr *osr = NULL;
-	int ret;
 
 	if (mask != IIO_CHAN_INFO_OVERSAMPLING_RATIO)
 		return -EINVAL;
@@ -322,9 +321,8 @@ static int ms5611_write_raw(struct iio_dev *indio_dev,
 	if (!osr)
 		return -EINVAL;
 
-	ret = iio_device_claim_direct_mode(indio_dev);
-	if (ret)
-		return ret;
+	if (!iio_device_claim_direct(indio_dev))
+		return -EBUSY;
 
 	mutex_lock(&st->lock);
 
@@ -334,7 +332,7 @@ static int ms5611_write_raw(struct iio_dev *indio_dev,
 		st->pressure_osr = osr;
 
 	mutex_unlock(&st->lock);
-	iio_device_release_direct_mode(indio_dev);
+	iio_device_release_direct(indio_dev);
 
 	return 0;
 }
Signature · 1 line
2.48.1
author maintainer
#41 lore raw

PATCH 29/37 iio: pressure: rohm-bm1390: 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/pressure/rohm-bm1390.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/iio/pressure/rohm-bm1390.c +3 -4
index 9c1197f0e742..c48231739f48 100644
@@ -319,12 +319,11 @@ static int bm1390_read_raw(struct iio_dev *idev,
 
 		return -EINVAL;
 	case IIO_CHAN_INFO_RAW:
-		ret = iio_device_claim_direct_mode(idev);
-		if (ret)
-			return ret;
+		if (!iio_device_claim_direct(idev))
+			return -EBUSY;
 
 		ret = bm1390_read_data(data, chan, val, val2);
-		iio_device_release_direct_mode(idev);
+		iio_device_release_direct(idev);
 		if (ret)
 			return ret;
 
Signature · 1 line
2.48.1
maintainer Reviewed-by on v1 29/37
#42 lore raw

Re: [PATCH 29/37] iio: pressure: rohm-bm1390: Switch to sparse friendly iio_device_claim/release_direct()

Quoted 7 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
#43 lore raw

PATCH 30/37 iio: pressure: zpa2326: 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/pressure/zpa2326.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
drivers/iio/pressure/zpa2326.c +6 -9
index 9db1c94dfc18..0a510d5fc1d4 100644
@@ -1062,9 +1062,8 @@ static int zpa2326_sample_oneshot(struct iio_dev     *indio_dev,
 	int                     ret;
 	struct zpa2326_private *priv;
 
-	ret = iio_device_claim_direct_mode(indio_dev);
-	if (ret)
-		return ret;
+	if (!iio_device_claim_direct(indio_dev))
+		return -EBUSY;
 
 	ret = zpa2326_resume(indio_dev);
 	if (ret < 0)
@@ -1120,7 +1119,7 @@ static int zpa2326_sample_oneshot(struct iio_dev     *indio_dev,
 suspend:
 	zpa2326_suspend(indio_dev);
 release:
-	iio_device_release_direct_mode(indio_dev);
+	iio_device_release_direct(indio_dev);
 
 	return ret;
 }
@@ -1438,7 +1437,6 @@ static int zpa2326_set_frequency(struct iio_dev *indio_dev, int hz)
 {
 	struct zpa2326_private *priv = iio_priv(indio_dev);
 	int                     freq;
-	int                     err;
 
 	/* Check if requested frequency is supported. */
 	for (freq = 0; freq < ARRAY_SIZE(zpa2326_sampling_frequencies); freq++)
@@ -1448,13 +1446,12 @@ static int zpa2326_set_frequency(struct iio_dev *indio_dev, int hz)
 		return -EINVAL;
 
 	/* Don't allow changing frequency if buffered sampling is ongoing. */
-	err = iio_device_claim_direct_mode(indio_dev);
-	if (err)
-		return err;
+	if (!iio_device_claim_direct(indio_dev))
+		return -EBUSY;
 
 	priv->frequency = &zpa2326_sampling_frequencies[freq];
 
-	iio_device_release_direct_mode(indio_dev);
+	iio_device_release_direct(indio_dev);
 
 	return 0;
 }
Signature · 1 line
2.48.1
author maintainer
#44 lore raw

PATCH 31/37 iio: proximity: hx9023s: 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/proximity/hx9023s.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/iio/proximity/hx9023s.c +3 -4
index 5aa8e5a22f32..5be5f4986347 100644
@@ -701,12 +701,11 @@ static int hx9023s_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 = hx9023s_get_proximity(data, chan, val);
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		return ret;
 	case IIO_CHAN_INFO_SAMP_FREQ:
 		return hx9023s_get_samp_freq(data, val, val2);
Signature · 1 line
2.48.1
author maintainer
#45 lore raw

PATCH 32/37 iio: proximity: pulsed-light: 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/proximity/pulsedlight-lidar-lite-v2.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/proximity/pulsedlight-lidar-lite-v2.c +2 -2
index f3d054b06b4c..fbf9f8513055 100644
@@ -208,7 +208,7 @@ static int lidar_read_raw(struct iio_dev *indio_dev,
 	case IIO_CHAN_INFO_RAW: {
 		u16 reg;
 
-		if (iio_device_claim_direct_mode(indio_dev))
+		if (!iio_device_claim_direct(indio_dev))
 			return -EBUSY;
 
 		ret = lidar_get_measurement(data, &reg);
@@ -216,7 +216,7 @@ static int lidar_read_raw(struct iio_dev *indio_dev,
 			*val = reg;
 			ret = IIO_VAL_INT;
 		}
-		iio_device_release_direct_mode(indio_dev);
+		iio_device_release_direct(indio_dev);
 		break;
 	}
 	case IIO_CHAN_INFO_SCALE:
Signature · 1 line
2.48.1
author maintainer
#46 lore raw

PATCH 33/37 iio: proximity: sx9500: 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/proximity/sx9500.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/iio/proximity/sx9500.c +3 -4
index c4e94d0fb163..b5e2855975ee 100644
@@ -387,11 +387,10 @@ static int sx9500_read_raw(struct iio_dev *indio_dev,
 	case IIO_PROXIMITY:
 		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 = sx9500_read_proximity(data, chan, val);
-			iio_device_release_direct_mode(indio_dev);
+			iio_device_release_direct(indio_dev);
 			return ret;
 		case IIO_CHAN_INFO_SAMP_FREQ:
 			return sx9500_read_samp_freq(data, val, val2);
Signature · 1 line
2.48.1
author maintainer
#47 lore raw

PATCH 34/37 iio: temp: maxim_thermocouple: 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.

Tidy up a few direct returns whilst here.

---
 drivers/iio/temperature/maxim_thermocouple.c | 29 ++++++++------------
 1 file changed, 12 insertions(+), 17 deletions(-)
drivers/iio/temperature/maxim_thermocouple.c +12 -17
index c28a7a6dea5f..a1c213d5c469 100644
@@ -183,40 +183,35 @@ static int maxim_thermocouple_read_raw(struct iio_dev *indio_dev,
 				       int *val, int *val2, long mask)
 {
 	struct maxim_thermocouple_data *data = iio_priv(indio_dev);
-	int ret = -EINVAL;
+	int ret;
 
 	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 = maxim_thermocouple_read(data, chan, val);
-		iio_device_release_direct_mode(indio_dev);
-
-		if (!ret)
-			return IIO_VAL_INT;
+		iio_device_release_direct(indio_dev);
+		if (ret)
+			return ret;
 
-		break;
+		return IIO_VAL_INT;
 	case IIO_CHAN_INFO_SCALE:
 		switch (chan->channel2) {
 		case IIO_MOD_TEMP_AMBIENT:
 			*val = 62;
 			*val2 = 500000; /* 1000 * 0.0625 */
-			ret = IIO_VAL_INT_PLUS_MICRO;
-			break;
+			return IIO_VAL_INT_PLUS_MICRO;
 		default:
 			*val = 250; /* 1000 * 0.25 */
-			ret = IIO_VAL_INT;
+			return IIO_VAL_INT;
 		}
-		break;
 	case IIO_CHAN_INFO_THERMOCOUPLE_TYPE:
 		*val = data->tc_type;
-		ret = IIO_VAL_CHAR;
-		break;
+		return IIO_VAL_CHAR;
+	default:
+		return -EINVAL;
 	}
-
-	return ret;
 }
 
 static const struct iio_info maxim_thermocouple_info = {
Signature · 1 line
2.48.1
author maintainer
#48 lore raw

PATCH 35/37 iio: temp: maxim_thermocouple: Drop unused mutex.h include.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

There are no mutex related calls in this driver.

---
 drivers/iio/temperature/maxim_thermocouple.c | 1 -
 1 file changed, 1 deletion(-)
drivers/iio/temperature/maxim_thermocouple.c +0 -1
index a1c213d5c469..a13efde76397 100644
@@ -9,7 +9,6 @@
 #include <linux/init.h>
 #include <linux/mod_devicetable.h>
 #include <linux/module.h>
-#include <linux/mutex.h>
 #include <linux/err.h>
 #include <linux/spi/spi.h>
 #include <linux/iio/iio.h>
Signature · 1 line
2.48.1
author maintainer
#49 lore raw

PATCH 36/37 staging: iio: ad5933: 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.

---
 .../staging/iio/impedance-analyzer/ad5933.c   | 38 ++++++++++---------
 1 file changed, 21 insertions(+), 17 deletions(-)
drivers/staging/iio/impedance-analyzer/ad5933.c +21 -17
index d5544fc2fe98..5aaa43e94c52 100644
@@ -271,11 +271,12 @@ static ssize_t ad5933_show_frequency(struct device *dev,
 		u8 d8[4];
 	} dat;
 
-	ret = iio_device_claim_direct_mode(indio_dev);
-	if (ret)
-		return ret;
+	if (!iio_device_claim_direct(indio_dev))
+		return -EBUSY;
+
 	ret = ad5933_i2c_read(st->client, this_attr->address, 3, &dat.d8[1]);
-	iio_device_release_direct_mode(indio_dev);
+
+	iio_device_release_direct(indio_dev);
 	if (ret < 0)
 		return ret;
 
@@ -305,11 +306,12 @@ static ssize_t ad5933_store_frequency(struct device *dev,
 	if (val > AD5933_MAX_OUTPUT_FREQ_Hz)
 		return -EINVAL;
 
-	ret = iio_device_claim_direct_mode(indio_dev);
-	if (ret)
-		return ret;
+	if (!iio_device_claim_direct(indio_dev))
+		return -EBUSY;
+
 	ret = ad5933_set_freq(st, this_attr->address, val);
-	iio_device_release_direct_mode(indio_dev);
+
+	iio_device_release_direct(indio_dev);
 
 	return ret ? ret : len;
 }
@@ -384,9 +386,9 @@ static ssize_t ad5933_store(struct device *dev,
 			return ret;
 	}
 
-	ret = iio_device_claim_direct_mode(indio_dev);
-	if (ret)
-		return ret;
+	if (!iio_device_claim_direct(indio_dev))
+		return -EBUSY;
+
 	mutex_lock(&st->lock);
 	switch ((u32)this_attr->address) {
 	case AD5933_OUT_RANGE:
@@ -438,7 +440,8 @@ static ssize_t ad5933_store(struct device *dev,
 	}
 
 	mutex_unlock(&st->lock);
-	iio_device_release_direct_mode(indio_dev);
+
+	iio_device_release_direct(indio_dev);
 	return ret ? ret : len;
 }
 
@@ -506,9 +509,9 @@ static int ad5933_read_raw(struct iio_dev *indio_dev,
 
 	switch (m) {
 	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 = ad5933_cmd(st, AD5933_CTRL_MEASURE_TEMP);
 		if (ret < 0)
 			goto out;
@@ -521,7 +524,8 @@ static int ad5933_read_raw(struct iio_dev *indio_dev,
 				      2, (u8 *)&dat);
 		if (ret < 0)
 			goto out;
-		iio_device_release_direct_mode(indio_dev);
+
+		iio_device_release_direct(indio_dev);
 		*val = sign_extend32(be16_to_cpu(dat), 13);
 
 		return IIO_VAL_INT;
@@ -533,7 +537,7 @@ static int ad5933_read_raw(struct iio_dev *indio_dev,
 
 	return -EINVAL;
 out:
-	iio_device_release_direct_mode(indio_dev);
+	iio_device_release_direct(indio_dev);
 	return ret;
 }
 
Signature · 1 line
2.48.1
author maintainer
#50 lore raw

PATCH 37/37 iio: Adjust internals of handling of direct mode claiming to suit new API.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Now there are no remaining callers of iio_device_claim_direct_mode() and iio_device_release_direct_mode() rename those functions to ensure they are no used in new drivers. Also make them now return booleans in line with the sparse friendly static inline wrappers.

---
 drivers/iio/industrialio-core.c | 28 ++++++++++++++++------------
 include/linux/iio/iio.h         | 10 ++++------
 2 files changed, 20 insertions(+), 18 deletions(-)
drivers/iio/industrialio-core.c +16 -12
index c9955a1c1090..c1921b55cfc5 100644
@@ -2157,17 +2157,19 @@ int __devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev,
 EXPORT_SYMBOL_GPL(__devm_iio_device_register);
 
 /**
- * iio_device_claim_direct_mode - Keep device in direct mode
+ * __iio_device_claim_direct - Keep device in direct mode
  * @indio_dev:	the iio_dev associated with the device
  *
  * If the device is in direct mode it is guaranteed to stay
- * that way until iio_device_release_direct_mode() is called.
+ * that way until __iio_device_release_direct() is called.
  *
- * Use with iio_device_release_direct_mode()
+ * Use with __iio_device_release_direct().
  *
- * Returns: 0 on success, -EBUSY on failure.
+ * Drivers should only call iio_device_claim_direct()
+ *
+ * Returns: true on success, false on failure.
  */
-int iio_device_claim_direct_mode(struct iio_dev *indio_dev)
+bool __iio_device_claim_direct(struct iio_dev *indio_dev)
 {
 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
 
@@ -2175,26 +2177,28 @@ int iio_device_claim_direct_mode(struct iio_dev *indio_dev)
 
 	if (iio_buffer_enabled(indio_dev)) {
 		mutex_unlock(&iio_dev_opaque->mlock);
-		return -EBUSY;
+		return false;
 	}
-	return 0;
+	return true;
 }
-EXPORT_SYMBOL_GPL(iio_device_claim_direct_mode);
+EXPORT_SYMBOL_GPL(__iio_device_claim_direct);
 
 /**
- * iio_device_release_direct_mode - releases claim on direct mode
+ * __iio_device_release_direct - releases claim on direct mode
  * @indio_dev:	the iio_dev associated with the device
  *
  * Release the claim. Device is no longer guaranteed to stay
  * in direct mode.
  *
- * Use with iio_device_claim_direct_mode()
+ * Drivers should only call iio_device_release_direct()
+ *
+ * Use with __iio_device_claim_direct()
  */
-void iio_device_release_direct_mode(struct iio_dev *indio_dev)
+void __iio_device_release_direct(struct iio_dev *indio_dev)
 {
 	mutex_unlock(&to_iio_dev_opaque(indio_dev)->mlock);
 }
-EXPORT_SYMBOL_GPL(iio_device_release_direct_mode);
+EXPORT_SYMBOL_GPL(__iio_device_release_direct);
 
 /**
  * iio_device_claim_buffer_mode - Keep device in buffer mode
include/linux/iio/iio.h +4 -6
index 07a0e8132e88..638cf2420fbd 100644
@@ -659,8 +659,8 @@ void iio_device_unregister(struct iio_dev *indio_dev);
 int __devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev,
 			       struct module *this_mod);
 int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp);
-int iio_device_claim_direct_mode(struct iio_dev *indio_dev);
-void iio_device_release_direct_mode(struct iio_dev *indio_dev);
+bool __iio_device_claim_direct(struct iio_dev *indio_dev);
+void __iio_device_release_direct(struct iio_dev *indio_dev);
 
 /*
  * Helper functions that allow claim and release of direct mode
@@ -671,9 +671,7 @@ void iio_device_release_direct_mode(struct iio_dev *indio_dev);
  */
 static inline bool iio_device_claim_direct(struct iio_dev *indio_dev)
 {
-	int ret = iio_device_claim_direct_mode(indio_dev);
-
-	if (ret)
+	if (!__iio_device_claim_direct(indio_dev))
 		return false;
 
 	__acquire(iio_dev);
@@ -683,7 +681,7 @@ static inline bool iio_device_claim_direct(struct iio_dev *indio_dev)
 
 static inline void iio_device_release_direct(struct iio_dev *indio_dev)
 {
-	iio_device_release_direct_mode(indio_dev);
+	__iio_device_release_direct(indio_dev);
 	__release(indio_dev);
 }
 
Signature · 1 line
2.48.1
#51 lore raw

Re: [PATCH 37/37] iio: Adjust internals of handling of direct mode claiming to suit new API.

Quoted 6 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Now there are no remaining callers of iio_device_claim_direct_mode() and iio_device_release_direct_mode() rename those functions to ensure they are no used in new drivers. Also make them now return booleans

not used

Quoted 1 line

in line with the sparse friendly static inline wrappers.

...

Quoted 6 lines

/**
- * iio_device_claim_direct_mode - Keep device in direct mode
+ * __iio_device_claim_direct - Keep device in direct mode
* @indio_dev: the iio_dev associated with the device *
* If the device is in direct mode it is guaranteed to stay

Quoted 8 lines

- * that way until iio_device_release_direct_mode() is called.
+ * that way until __iio_device_release_direct() is called. *
- * Use with iio_device_release_direct_mode()
+ * Use with __iio_device_release_direct().
*
- * Returns: 0 on success, -EBUSY on failure.
+ * Drivers should only call iio_device_claim_direct()

Missed period.

Quoted 2 lines

+ * Returns: true on success, false on failure.
*/

...

drivers/iio/industrialio-core.c int iio_device_claim_direct_mode(struct iio_dev *indio_dev) · line 2184

Quoted 2 lines
drivers/iio/industrialio-core.c +1 -1
-EXPORT_SYMBOL_GPL(iio_device_claim_direct_mode);
+EXPORT_SYMBOL_GPL(__iio_device_claim_direct);

Can we move it to the namespace while at it?

...

Quoted 10 lines

/**
- * iio_device_release_direct_mode - releases claim on direct mode
+ * __iio_device_release_direct - releases claim on direct mode
* @indio_dev: the iio_dev associated with the device *
* Release the claim. Device is no longer guaranteed to stay
* in direct mode.
*
- * Use with iio_device_claim_direct_mode()
+ * Drivers should only call iio_device_release_direct()

Missed period.

Quoted 2 lines

+ * Use with __iio_device_claim_direct()
*/

...

drivers/iio/industrialio-core.c int iio_device_claim_direct_mode(struct iio_dev *indio_dev) · line 2201

Quoted 2 lines
drivers/iio/industrialio-core.c +1 -1
-EXPORT_SYMBOL_GPL(iio_device_release_direct_mode);
+EXPORT_SYMBOL_GPL(__iio_device_release_direct);

Namespace?
Note, the good outcome of the namespace is that we can easily see who (ab)uses these... OTOH, there are static inline functions in the header, which provokes this to add MODULE_IMPORT_NS() into the header as well. That said, perhaps also get rid of static inliners and have them to be real functions?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
reviewer
#52 lore raw

Re: [PATCH 37/37] iio: Adjust internals of handling of direct mode claiming to suit new API.

Quoted 3 lines · Andy Shevchenko

That said, perhaps also get rid of static inliners and have them to be real functions?

That requires fixing sparse first. It doesn't currently support __cond_acquires(). So we are suck with static inline until then.

longer explanation: https://lore.kernel.org/linux-iio/[email protected]/

author maintainer
#53 lore raw

Re: [PATCH 37/37] iio: Adjust internals of handling of direct mode claiming to suit new API.

In reply to Andy Shevchenko
Quoted 34 lines · On Mon, 31 Mar 2025 16:41:42 +0300 Andy Shevchenko <[email protected]>…
Quoted 6 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Now there are no remaining callers of iio_device_claim_direct_mode() and iio_device_release_direct_mode() rename those functions to ensure they are no used in new drivers. Also make them now return booleans

not used

Quoted 1 line

in line with the sparse friendly static inline wrappers.

...

Quoted 6 lines

/**
- * iio_device_claim_direct_mode - Keep device in direct mode
+ * __iio_device_claim_direct - Keep device in direct mode
* @indio_dev: the iio_dev associated with the device *
* If the device is in direct mode it is guaranteed to stay

Quoted 8 lines

- * that way until iio_device_release_direct_mode() is called.
+ * that way until __iio_device_release_direct() is called. *
- * Use with iio_device_release_direct_mode()
+ * Use with __iio_device_release_direct().
*
- * Returns: 0 on success, -EBUSY on failure.
+ * Drivers should only call iio_device_claim_direct()

Missed period.

Quoted 2 lines

+ * Returns: true on success, false on failure.
*/

Quoted 6 lines

...

drivers/iio/industrialio-core.c int iio_device_claim_direct_mode(struct iio_dev *indio_dev) · line 2184

Quoted 2 lines
drivers/iio/industrialio-core.c +1 -1
-EXPORT_SYMBOL_GPL(iio_device_claim_direct_mode);
+EXPORT_SYMBOL_GPL(__iio_device_claim_direct);  

Can we move it to the namespace while at it?

I'm personally not convinced by the namespace defaults in headers thing pwm.h does. So to namespace these is a massive patch adding the import to most drivers.

So I'd rather deal with that later.

Quoted 21 lines

...

Quoted 10 lines

/**
- * iio_device_release_direct_mode - releases claim on direct mode
+ * __iio_device_release_direct - releases claim on direct mode
* @indio_dev: the iio_dev associated with the device *
* Release the claim. Device is no longer guaranteed to stay
* in direct mode.
*
- * Use with iio_device_claim_direct_mode()
+ * Drivers should only call iio_device_release_direct()

Missed period.

Quoted 2 lines

+ * Use with __iio_device_claim_direct()
*/

...

Quoted 9 lines

drivers/iio/industrialio-core.c int iio_device_claim_direct_mode(struct iio_dev *indio_dev) · line 2201

Quoted 2 lines
drivers/iio/industrialio-core.c +1 -1
-EXPORT_SYMBOL_GPL(iio_device_release_direct_mode);
+EXPORT_SYMBOL_GPL(__iio_device_release_direct);  

Namespace?
Note, the good outcome of the namespace is that we can easily see who (ab)uses these... OTOH, there are static inline functions in the header, which provokes this to add MODULE_IMPORT_NS() into the header as well. That said, perhaps also get rid of static inliners and have them to be real functions?

As David said. Unfortunately not :(

Though static inlines using these is fine as any driver that doesn't import will still fail in the correct way. It will just be a bit 'weird' to fail like that. The missing namespace warnings are pretty obvious though so that should be fine.

Jonathan

Quoted 1 line
#54 lore raw

Re: [PATCH 37/37] iio: Adjust internals of handling of direct mode claiming to suit new API.

Quoted 3 lines · Jonathan Cameron
Quoted 1 line · On Mon, 31 Mar 2025 16:41:42 +0300 Andy Shevchenko <[email protected]>…

On Mon, Mar 31, 2025 at 3:17 PM Jonathan Cameron <[email protected]> wrote:

...

Quoted 8 lines
Quoted 4 lines

drivers/iio/industrialio-core.c int iio_device_claim_direct_mode(struct iio_dev *indio_dev) · line 2184

Quoted 2 lines
drivers/iio/industrialio-core.c +1 -1
-EXPORT_SYMBOL_GPL(iio_device_claim_direct_mode);
+EXPORT_SYMBOL_GPL(__iio_device_claim_direct);

Can we move it to the namespace while at it?

I'm personally not convinced by the namespace defaults in headers thing pwm.h does. So to namespace these is a massive patch adding the import to most drivers.

We are on the same page here! I even mentioned this disadvantage in the comment below.

Quoted 1 line

So I'd rather deal with that later.

...

Quoted 11 lines
Quoted 9 lines

drivers/iio/industrialio-core.c int iio_device_claim_direct_mode(struct iio_dev *indio_dev) · line 2201

Quoted 2 lines
drivers/iio/industrialio-core.c +1 -1
-EXPORT_SYMBOL_GPL(iio_device_release_direct_mode);
+EXPORT_SYMBOL_GPL(__iio_device_release_direct);

Namespace?
Note, the good outcome of the namespace is that we can easily see who (ab)uses these... OTOH, there are static inline functions in the header, which provokes this to add MODULE_IMPORT_NS() into the header as well. That said, perhaps also get rid of static inliners and have them to be real functions?

As David said. Unfortunately not :(

Yeah, that's a pity.

Quoted 4 lines

Though static inlines using these is fine as any driver that doesn't import will still fail in the correct way. It will just be a bit 'weird' to fail like that. The missing namespace warnings are pretty obvious though so that should be fine.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#56 lore raw
Quoted 8 lines · On Tue, 1 Apr 2025 14:13:24 -0500 David Lechner <[email protected]> wrote:
Quoted 4 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Firstly, sorry for the huge patch set!

I've applied all these to the testing branch of iio.git. Only changes were the typos and punctuation that Andy pointed out in the final patch. They didn't feel like the justified a v2.

Note that I'm not planning to push this out as non rebasing until next weekend, so there is still time for additional review.

The namespace question can wait for another time.

Thanks,

Jonathan

Reviewed-by on the v1 series
#57 lore raw
In reply to Jonathan Cameron

Hi Jonathan,

For the whole series

Really nice series IMHO.

Thanks,
Marcelo

Quoted 25 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Firstly, sorry for the huge patch set!

I've run out of signficiant groups to break the remaining patches up into. Rather than have lots of small series of 5-10 patches, lets just have one mega one. Feel free to review a subset! I'll nibble away at this as reviews come in and post a v2 later in the cycle with whatever remains unreviewed if that is necessary.

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!

This is effectively part 5 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/[email protected]/

This series focuses on all the remaining drivers.
The final patch removes the old infrastructure.