Skip to contentSkip to conversation

staging/iio: Clean up AD7746 CDC driver and move from staging.

PATCH Superseded Jonathan Cameron 5 participants 39 messages Jun 19, 2022 – Jun 26, 2022

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Jonathan Cameron

author maintainer
#0 lore raw

v2 cover staging/iio: Clean up AD7746 CDC driver and move from staging.

From: Jonathan Cameron <[email protected]>

Vincent: I'm getting some instability with roadtest after forwards porting to current mainline. Tests run 'sometimes'. Other times I get a crash in um_set_signal. Seems unrelated to the test this series adds.

Changes since v1: Thanks to Andy and Peter for reviews.
- Use sizeof(data) for be24 buffer data[3]
- Make the precision patch fall back to old maths if overflow would have occured.

This started out as an experiment with Vincent Whitchurch's roadtest testing framework [1] and that worked well so I carried on cleaning up the driver.

Mostly this is standard tidy up, move to new interfaces, then move the driver out of staging, but there are a few other things in here.

1) Precision improvement for IIO_VAL_FRACTIONAL_LOG2.
The ad7746 is a 24 bit sensor and this highlighted that 9 decimal places wasn't enough to keep the error introduced by
_raw * _scale from growing too large over the whole range (I couldn't write a sensible test for it). So I've proposed increasing the precision of the calculation used to provide the sysfs attributes with this value type and printing a few more decimal places (12), but only if overflow will not occur due to val[0] > ULLONG_MAX / PICO
2) _inputoffset ABI addition. This driver had an odd use of _offset for the case of differential channels that applied the offset to both of the differential pair (hence usespace shouldn't not apply it when converting to the base units. That isn't inline with the existing documentation for _offset and it wasn't clear to me that it made sense at all. To avoid confusion I've added this new ABI (_inputoffset) for t
3) roadtest file - note this is not a complete test set for the driver and mainly focused on the main channel reads and places I thought I might have broken things whilst working on the driver.

My conclusion on roadtest - Very useful indeed. I'd encourage others to consider developing some basic sanity tests for drivers they are working on. Hopefully my python code isn't too hideous to understand at least! Vincent, it might be worth thinking about some generic code to handle the 'variants' on correct ABI like I introduce here because I switched from a shared by type scale to an individual one per channel for the voltages. Both were ABI compliant so that sort of change is fine most of the time though we have to be careful with it.

All comments welcome. Note there may be changes that make more sense to do after moving this out of staging as long as there are no ABI changes involved etc. Feel free to highlight those sorts of changes as well as anything more significant.

[1] https://lore.kernel.org/all/[email protected]/

Jonathan Cameron (17):
iio: core: Increase precision of IIO_VAL_FRACTIONAL_LOG2 when possible
iio: ABI: Fix wrong format of differential capacitance channel ABI.
staging: iio: cdc: ad7746: Use explicit be24 handling.
staging: iio: cdc: ad7746: Push handling of supply voltage scale to
userspace.
staging: iio: cdc: ad7746: Use local buffer for multi byte reads.
staging: iio: cdc: ad7746: Factor out ad7746_read_channel()
staging: iio: cdc: ad7764: Push locking down into case statements in
read/write_raw
staging: iio: cdc: ad7746: Break up use of chan->address and use
FIELD_PREP etc
staging: iio: cdc: ad7746: Drop usused i2c_set_clientdata()
staging: iio: cdc: ad7746: Use _raw and _scale for temperature
channels.
iio: core: Introduce _inputoffset for differential channels
staging: iio: cdc: ad7746: Switch from _offset to _inputoffset for
differential channels.
staging: iio: cdc: ad7746: Use read_avail() rather than opencoding.
staging: iio: ad7746: White space cleanup
iio: cdc: ad7746: Add device specific ABI documentation.
iio: cdc: ad7746: Move driver out of staging.
RFC: iio: cdc: ad7746: Add roadtest

 Documentation/ABI/testing/sysfs-bus-iio       |  15 +-
 .../ABI/testing/sysfs-bus-iio-cdc-ad7746      |  11 +
 drivers/iio/cdc/Kconfig                       |  10 +
 drivers/iio/cdc/Makefile                      |   1 +
 drivers/iio/cdc/ad7746.c                      | 818 ++++++++++++++++++
 drivers/iio/industrialio-core.c               |  31 +-
 drivers/staging/iio/cdc/ad7746.c              | 767 ----------------
 include/linux/iio/types.h                     |   1 +
 .../roadtest/tests/iio/cdc/__init__.py        |   0
 .../roadtest/roadtest/tests/iio/cdc/config    |   1 +
 .../roadtest/tests/iio/cdc/test_ad7746.py     | 323 +++++++
 11 files changed, 1202 insertions(+), 776 deletions(-)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-iio-cdc-ad7746
 create mode 100644 drivers/iio/cdc/ad7746.c
 delete mode 100644 drivers/staging/iio/cdc/ad7746.c
 create mode 100644 tools/testing/roadtest/roadtest/tests/iio/cdc/__init__.py
 create mode 100644 tools/testing/roadtest/roadtest/tests/iio/cdc/config
 create mode 100644 tools/testing/roadtest/roadtest/tests/iio/cdc/test_ad7746.py
Signature · 1 line
2.36.1
author maintainer
#1 lore raw

PATCH v2 1/17 iio: core: Increase precision of IIO_VAL_FRACTIONAL_LOG2 when possible

From: Jonathan Cameron <[email protected]>

With some high resolution sensors such as the ad7746 the build up of error when multiplying the _raw and _scale values together can be significant. Reduce this affect by providing additional resolution in both calculation and formatting of result. If overflow would occur with a 1e12 multiplier, fall back to the 1e9 used before this patch.

---
 drivers/iio/industrialio-core.c | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)
drivers/iio/industrialio-core.c +22 -8
index dc3e1cb9bfbd..6b401f63805f 100644
@@ -674,14 +674,28 @@ static ssize_t __iio_format_value(char *buf, size_t offset, unsigned int type,
 		else
 			return sysfs_emit_at(buf, offset, "%d.%09u", tmp0,
 					     abs(tmp1));
-	case IIO_VAL_FRACTIONAL_LOG2:
-		tmp2 = shift_right((s64)vals[0] * 1000000000LL, vals[1]);
-		tmp0 = (int)div_s64_rem(tmp2, 1000000000LL, &tmp1);
-		if (tmp0 == 0 && tmp2 < 0)
-			return sysfs_emit_at(buf, offset, "-0.%09u", abs(tmp1));
-		else
-			return sysfs_emit_at(buf, offset, "%d.%09u", tmp0,
-					     abs(tmp1));
+	case IIO_VAL_FRACTIONAL_LOG2: {
+		u64 t1, t2, mult;
+		int integer, precision;
+		bool neg = vals[0] < 0;
+
+		if (vals[0] > ULLONG_MAX / PICO) {
+			mult = NANO;
+			precision = 9;
+		} else {
+			mult = PICO;
+			precision = 12;
+		}
+		t1 = shift_right((u64)abs(vals[0]) * mult, vals[1]);
+		integer = (int)div64_u64_rem(t1, mult, &t2);
+		if (integer == 0 && neg)
+			return sysfs_emit_at(buf, offset, "-0.%0*llu",
+					     precision, abs(t2));
+		if (neg)
+			integer *= -1;
+		return sysfs_emit_at(buf, offset, "%d.%0*llu", integer,
+				     precision, abs(t2));
+	}
 	case IIO_VAL_INT_MULTIPLE:
 	{
 		int i;
Signature · 1 line
2.36.1
author maintainer
#2 lore raw

PATCH v2 2/17 iio: ABI: Fix wrong format of differential capacitance channel ABI.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

in_ only occurs once in these attributes.

---
 Documentation/ABI/testing/sysfs-bus-iio | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Documentation/ABI/testing/sysfs-bus-iio +1 -1
index 3e00d7f7ee22..d3a0c0ef8948 100644
@@ -193,7 +193,7 @@ Description:
 		Raw capacitance measurement from channel Y. Units after
 		application of scale and offset are nanofarads.
 
-What:		/sys/.../iio:deviceX/in_capacitanceY-in_capacitanceZ_raw
+What:		/sys/.../iio:deviceX/in_capacitanceY-capacitanceZ_raw
 KernelVersion:	3.2
 Contact:	[email protected]
 Description:
Signature · 1 line
2.36.1
#3 lore raw

Re: [PATCH v2 02/17] iio: ABI: Fix wrong format of differential capacitance channel ABI.

Quoted 4 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

in_ only occurs once in these attributes.

Shouldn't this be a fix independently of the series context? If so, adding a Fixes tag?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#4 lore raw

Re: [PATCH v2 02/17] iio: ABI: Fix wrong format of differential capacitance channel ABI.

Quoted 9 lines · On Mon, 20 Jun 2022 00:58:20 +0200 Andy Shevchenko <[email protected]>…
Quoted 4 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

in_ only occurs once in these attributes.

Shouldn't this be a fix independently of the series context? If so, adding a Fixes tag?

Good point. Added tag for
0baf29d658c7 ("staging:iio:documentation Add abi docs for capacitance adcs.")

Thanks,

Jonathan

author maintainer
#5 lore raw

PATCH v2 3/17 staging: iio: cdc: ad7746: Use explicit be24 handling.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Chance from fiddly local implementation of be24 to cpu endian conversion by reading into a 3 byte buffer and using get_unaligned_be24()

---
 drivers/staging/iio/cdc/ad7746.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
drivers/staging/iio/cdc/ad7746.c +5 -7
index 52b8957c19c9..9208fb179240 100644
@@ -95,10 +95,7 @@ struct ad7746_chip_info {
 	u8	capdac[2][2];
 	s8	capdac_set;
 
-	union {
-		__be32 d32;
-		u8 d8[4];
-	} data ____cacheline_aligned;
+	u8 data[3] ____cacheline_aligned;
 };
 
 enum ad7746_chan {
@@ -546,13 +543,14 @@ static int ad7746_read_raw(struct iio_dev *indio_dev,
 		/* Now read the actual register */
 
 		ret = i2c_smbus_read_i2c_block_data(chip->client,
-						    chan->address >> 8, 3,
-						    &chip->data.d8[1]);
+						    chan->address >> 8,
+						    sizeof(chip->data),
+						    chip->data);
 
 		if (ret < 0)
 			goto out;
 
-		*val = (be32_to_cpu(chip->data.d32) & 0xFFFFFF) - 0x800000;
+		*val = get_unaligned_be24(chip->data) - 0x800000;
 
 		switch (chan->type) {
 		case IIO_TEMP:
Signature · 1 line
2.36.1
author maintainer
#6 lore raw

PATCH v2 4/17 staging: iio: cdc: ad7746: Push handling of supply voltage scale to userspace.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

The supply voltage is attenuated by 6 before being fed to the ADC. Handle this explcitly rather than premultiplying the _raw value by 6.

---
 drivers/staging/iio/cdc/ad7746.c | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)
drivers/staging/iio/cdc/ad7746.c +6 -10
index 9208fb179240..df12fdb20662 100644
@@ -114,9 +114,8 @@ static const struct iio_chan_spec ad7746_channels[] = {
 		.type = IIO_VOLTAGE,
 		.indexed = 1,
 		.channel = 0,
-		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
-		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
-			BIT(IIO_CHAN_INFO_SAMP_FREQ),
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
+		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ),
 		.address = AD7746_REG_VT_DATA_HIGH << 8 |
 			AD7746_VTSETUP_VTMD_EXT_VIN,
 	},
@@ -125,9 +124,8 @@ static const struct iio_chan_spec ad7746_channels[] = {
 		.indexed = 1,
 		.channel = 1,
 		.extend_name = "supply",
-		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
-		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
-			BIT(IIO_CHAN_INFO_SAMP_FREQ),
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
+		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ),
 		.address = AD7746_REG_VT_DATA_HIGH << 8 |
 			AD7746_VTSETUP_VTMD_VDD_MON,
 	},
@@ -560,10 +558,6 @@ static int ad7746_read_raw(struct iio_dev *indio_dev,
 			 */
 			*val = (*val * 125) / 256;
 			break;
-		case IIO_VOLTAGE:
-			if (chan->channel == 1) /* supply_raw*/
-				*val = *val * 6;
-			break;
 		default:
 			break;
 		}
@@ -618,6 +612,8 @@ static int ad7746_read_raw(struct iio_dev *indio_dev,
 		case IIO_VOLTAGE:
 			/* 1170mV / 2^23 */
 			*val = 1170;
+			if (chan->channel == 1)
+				*val *= 6;
 			*val2 = 23;
 			ret = IIO_VAL_FRACTIONAL_LOG2;
 			break;
Signature · 1 line
2.36.1
author maintainer
#7 lore raw

PATCH v2 5/17 staging: iio: cdc: ad7746: Use local buffer for multi byte reads.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

I2C does not require DMA safe buffers so there is no need to ensure the buffers are in their own cacheline. Hence simplify things by using a local variable instead of embedding the buffer in the chip info structure.

---
 drivers/staging/iio/cdc/ad7746.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/staging/iio/cdc/ad7746.c +2 -4
index df12fdb20662..f909b841c4d6 100644
@@ -94,8 +94,6 @@ struct ad7746_chip_info {
 	u8	vt_setup;
 	u8	capdac[2][2];
 	s8	capdac_set;
-
-	u8 data[3] ____cacheline_aligned;
 };
 
 enum ad7746_chan {
@@ -520,6 +518,7 @@ static int ad7746_read_raw(struct iio_dev *indio_dev,
 	struct ad7746_chip_info *chip = iio_priv(indio_dev);
 	int ret, delay, idx;
 	u8 regval, reg;
+	u8 data[3];
 
 	mutex_lock(&chip->lock);
 
@@ -544,11 +543,10 @@ static int ad7746_read_raw(struct iio_dev *indio_dev,
 						    chan->address >> 8,
 						    sizeof(chip->data),
 						    chip->data);
-
 		if (ret < 0)
 			goto out;
 
-		*val = get_unaligned_be24(chip->data) - 0x800000;
+		*val = get_unaligned_be24(data) - 0x800000;
 
 		switch (chan->type) {
 		case IIO_TEMP:
Signature · 1 line
2.36.1
#8 lore raw

Re: [PATCH v2 05/17] staging: iio: cdc: ad7746: Use local buffer for multi byte reads.

Quoted 7 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

I2C does not require DMA safe buffers so there is no need to ensure the buffers are in their own cacheline. Hence simplify things by using a local variable instead of embedding the buffer in the chip info structure.

...

Quoted 2 lines
                                                    sizeof(chip->data),
                                                    chip->data);

How will these compile? Or am I missing something?

Quoted 6 lines
drivers/staging/iio/cdc/ad7746.c +1 -2
-
                if (ret < 0)
                        goto out;
 
-               *val = get_unaligned_be24(chip->data) - 0x800000;
+               *val = get_unaligned_be24(data) - 0x800000;
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#9 lore raw

Re: [PATCH v2 05/17] staging: iio: cdc: ad7746: Use local buffer for multi byte reads.

Quoted 9 lines · On Mon, 20 Jun 2022 01:00:23 +0200 Andy Shevchenko <[email protected]>…
Quoted 7 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

I2C does not require DMA safe buffers so there is no need to ensure the buffers are in their own cacheline. Hence simplify things by using a local variable instead of embedding the buffer in the chip info structure.

Quoted 6 lines

...

Quoted 2 lines
                                                    sizeof(chip->data),
                                                    chip->data);  

How will these compile? Or am I missing something?

Nope. Something went wrong in the rebase. I should have rechecked build patch by patch. Later on, this code is factored out, so I guess that fixed up the build issue.

Will check properly for v3. :(

Quoted 8 lines
Quoted 7 lines
drivers/staging/iio/cdc/ad7746.c +1 -2
-
                if (ret < 0)
                        goto out;
 
-               *val = get_unaligned_be24(chip->data) - 0x800000;
+               *val = get_unaligned_be24(data) - 0x800000;  
author maintainer
#10 lore raw

Re: [PATCH v2 05/17] staging: iio: cdc: ad7746: Use local buffer for multi byte reads.

Quoted 17 lines · On Mon, 20 Jun 2022 17:42:36 +0100 Jonathan Cameron…
Quoted 14 lines · On Mon, 20 Jun 2022 01:00:23 +0200 Andy Shevchenko <[email protected]>…
Quoted 7 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

I2C does not require DMA safe buffers so there is no need to ensure the buffers are in their own cacheline. Hence simplify things by using a local variable instead of embedding the buffer in the chip info structure.

...

Quoted 2 lines
                                                    sizeof(chip->data),
                                                    chip->data);    
Quoted 7 lines
Quoted 1 line

How will these compile? Or am I missing something?

Nope. Something went wrong in the rebase. I should have rechecked build patch by patch. Later on, this code is factored out, so I guess that fixed up the build issue.

Will check properly for v3. :(

yikes rebase went very wrong. Anyhow, will fix up.

Turns out I as being an idiot and building with the roadtest makefile which doesn't actually enable this driver until the last patch.

Jonathan

Quoted 11 lines
Quoted 10 lines
Quoted 9 lines
drivers/staging/iio/cdc/ad7746.c +1 -2
-
                if (ret < 0)
                        goto out;
 
-               *val = get_unaligned_be24(chip->data) - 0x800000;
+               *val = get_unaligned_be24(data) - 0x800000;    
author maintainer
#11 lore raw

PATCH v2 6/17 staging: iio: cdc: ad7746: Factor out ad7746_read_channel()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Reduce deep indenting and simplify the locking cleanup that follows.

---
 drivers/staging/iio/cdc/ad7746.c | 79 ++++++++++++++++----------------
 1 file changed, 40 insertions(+), 39 deletions(-)
drivers/staging/iio/cdc/ad7746.c +40 -39
index f909b841c4d6..bf20c14f60e4 100644
@@ -500,14 +500,47 @@ static int ad7746_write_raw(struct iio_dev *indio_dev,
 		default:
 			ret = -EINVAL;
 		}
+static int ad7746_read_channel(struct iio_dev *indio_dev,
+			       struct iio_chan_spec const *chan,
+			       int *val)
+{
+	struct ad7746_chip_info *chip = iio_priv(indio_dev);
+	int ret, delay;
+	u8 data[3];
+	u8 regval;
+
+	ret = ad7746_select_channel(indio_dev, chan);
+	if (ret < 0)
+		return ret;
+	delay = ret;
+
+	regval = chip->config | AD7746_CONF_MODE_SINGLE_CONV;
+	ret = i2c_smbus_write_byte_data(chip->client, AD7746_REG_CFG, regval);
+	if (ret < 0)
+		return ret;
+
+	msleep(delay);
+	/* Now read the actual register */
+	ret = i2c_smbus_read_i2c_block_data(chip->client,  chan->address >> 8,
+					    sizeof(data), data);
+	if (ret < 0)
+		return ret;
+
+	*val = get_unaligned_be24(data) - 0x800000;
+
+	switch (chan->type) {
+	case IIO_TEMP:
+		/*
+		 * temperature in milli degrees Celsius
+		 * T = ((*val / 2048) - 4096) * 1000
+		 */
+		*val = (*val  * 125) / 256;
 		break;
 	default:
-		ret = -EINVAL;
+		break;
 	}
 
-out:
-	mutex_unlock(&chip->lock);
-	return ret;
+	return 0;
 }
 
 static int ad7746_read_raw(struct iio_dev *indio_dev,
@@ -516,50 +549,18 @@ static int ad7746_read_raw(struct iio_dev *indio_dev,
 			   long mask)
 {
 	struct ad7746_chip_info *chip = iio_priv(indio_dev);
-	int ret, delay, idx;
-	u8 regval, reg;
-	u8 data[3];
+	int ret, idx;
+	u8 reg;
 
 	mutex_lock(&chip->lock);
 
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
 	case IIO_CHAN_INFO_PROCESSED:
-		ret = ad7746_select_channel(indio_dev, chan);
-		if (ret < 0)
-			goto out;
-		delay = ret;
-
-		regval = chip->config | AD7746_CONF_MODE_SINGLE_CONV;
-		ret = i2c_smbus_write_byte_data(chip->client, AD7746_REG_CFG,
-						regval);
-		if (ret < 0)
-			goto out;
-
-		msleep(delay);
-		/* Now read the actual register */
-
-		ret = i2c_smbus_read_i2c_block_data(chip->client,
-						    chan->address >> 8,
-						    sizeof(chip->data),
-						    chip->data);
+		ret = ad7746_read_channel(indio_dev, chan, val);
 		if (ret < 0)
 			goto out;
 
-		*val = get_unaligned_be24(data) - 0x800000;
-
-		switch (chan->type) {
-		case IIO_TEMP:
-			/*
-			 * temperature in milli degrees Celsius
-			 * T = ((*val / 2048) - 4096) * 1000
-			 */
-			*val = (*val * 125) / 256;
-			break;
-		default:
-			break;
-		}
-
 		ret = IIO_VAL_INT;
 		break;
 	case IIO_CHAN_INFO_CALIBSCALE:
Signature · 1 line
2.36.1
author maintainer
#12 lore raw

PATCH v2 7/17 staging: iio: cdc: ad7764: Push locking down into case statements in read/write_raw

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Not all paths require any locking at all. So to simplify the removal of such locking push the locks down into the individual case statements.

---
 drivers/staging/iio/cdc/ad7746.c | 125 ++++++++++++++-----------------
 1 file changed, 58 insertions(+), 67 deletions(-)
drivers/staging/iio/cdc/ad7746.c +58 -67
index bf20c14f60e4..41d9ea4ccb02 100644
@@ -418,14 +418,10 @@ static int ad7746_write_raw(struct iio_dev *indio_dev,
 	struct ad7746_chip_info *chip = iio_priv(indio_dev);
 	int ret, reg;
 
-	mutex_lock(&chip->lock);
-
 	switch (mask) {
 	case IIO_CHAN_INFO_CALIBSCALE:
-		if (val != 1) {
-			ret = -EINVAL;
-			goto out;
-		}
+		if (val != 1)
+			return -EINVAL;
 
 		val = (val2 * 1024) / 15625;
 
@@ -437,33 +433,31 @@ static int ad7746_write_raw(struct iio_dev *indio_dev,
 			reg = AD7746_REG_VOLT_GAINH;
 			break;
 		default:
-			ret = -EINVAL;
-			goto out;
+			return -EINVAL;
 		}
 
+		mutex_lock(&chip->lock);
 		ret = i2c_smbus_write_word_swapped(chip->client, reg, val);
+		mutex_unlock(&chip->lock);
 		if (ret < 0)
-			goto out;
+			return ret;
 
-		ret = 0;
-		break;
+		return 0;
 	case IIO_CHAN_INFO_CALIBBIAS:
-		if (val < 0 || val > 0xFFFF) {
-			ret = -EINVAL;
-			goto out;
-		}
+		if (val < 0 || val > 0xFFFF)
+			return -EINVAL;
+
+		mutex_lock(&chip->lock);
 		ret = i2c_smbus_write_word_swapped(chip->client,
 						   AD7746_REG_CAP_OFFH, val);
+		mutex_unlock(&chip->lock);
 		if (ret < 0)
-			goto out;
+			return ret;
 
-		ret = 0;
-		break;
+		return 0;
 	case IIO_CHAN_INFO_OFFSET:
-		if (val < 0 || val > 43008000) { /* 21pF */
-			ret = -EINVAL;
-			goto out;
-		}
+		if (val < 0 || val > 43008000) /* 21pF */
+			return -EINVAL;
 
 		/*
 		 * CAPDAC Scale = 21pF_typ / 127
@@ -472,34 +466,43 @@ static int ad7746_write_raw(struct iio_dev *indio_dev,
 		 */
 
 		val /= 338646;
-
+		mutex_lock(&chip->lock);
 		chip->capdac[chan->channel][chan->differential] = val > 0 ?
 			AD7746_CAPDAC_DACP(val) | AD7746_CAPDAC_DACEN : 0;
 
 		ret = ad7746_set_capdac(chip, chan->channel);
-		if (ret < 0)
-			goto out;
+		if (ret < 0) {
+			mutex_unlock(&chip->lock);
+			return ret;
+		}
 
 		chip->capdac_set = chan->channel;
+		mutex_unlock(&chip->lock);
 
-		ret = 0;
-		break;
+		return 0;
 	case IIO_CHAN_INFO_SAMP_FREQ:
-		if (val2) {
-			ret = -EINVAL;
-			goto out;
-		}
+		if (val2)
+			return -EINVAL;
 
 		switch (chan->type) {
 		case IIO_CAPACITANCE:
+			mutex_lock(&chip->lock);
 			ret = ad7746_store_cap_filter_rate_setup(chip, val);
-			break;
+			mutex_unlock(&chip->lock);
+			return ret;
 		case IIO_VOLTAGE:
+			mutex_lock(&chip->lock);
 			ret = ad7746_store_vt_filter_rate_setup(chip, val);
-			break;
+			mutex_unlock(&chip->lock);
+			return ret;
 		default:
-			ret = -EINVAL;
+			return -EINVAL;
 		}
+	default:
+		return -EINVAL;
+	}
+}
+
 static int ad7746_read_channel(struct iio_dev *indio_dev,
 			       struct iio_chan_spec const *chan,
 			       int *val)
@@ -552,17 +555,16 @@ static int ad7746_read_raw(struct iio_dev *indio_dev,
 	int ret, idx;
 	u8 reg;
 
-	mutex_lock(&chip->lock);
-
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
 	case IIO_CHAN_INFO_PROCESSED:
+		mutex_lock(&chip->lock);
 		ret = ad7746_read_channel(indio_dev, chan, val);
+		mutex_unlock(&chip->lock);
 		if (ret < 0)
-			goto out;
+			return ret;
 
-		ret = IIO_VAL_INT;
-		break;
+		return IIO_VAL_INT;
 	case IIO_CHAN_INFO_CALIBSCALE:
 		switch (chan->type) {
 		case IIO_CAPACITANCE:
@@ -572,80 +574,69 @@ static int ad7746_read_raw(struct iio_dev *indio_dev,
 			reg = AD7746_REG_VOLT_GAINH;
 			break;
 		default:
-			ret = -EINVAL;
-			goto out;
+			return -EINVAL;
 		}
 
+		mutex_lock(&chip->lock);
 		ret = i2c_smbus_read_word_swapped(chip->client, reg);
+		mutex_unlock(&chip->lock);
 		if (ret < 0)
-			goto out;
+			return ret;
 		/* 1 + gain_val / 2^16 */
 		*val = 1;
 		*val2 = (15625 * ret) / 1024;
 
-		ret = IIO_VAL_INT_PLUS_MICRO;
-		break;
+		return IIO_VAL_INT_PLUS_MICRO;
 	case IIO_CHAN_INFO_CALIBBIAS:
+		mutex_lock(&chip->lock);
 		ret = i2c_smbus_read_word_swapped(chip->client,
 						  AD7746_REG_CAP_OFFH);
+		mutex_unlock(&chip->lock);
 		if (ret < 0)
-			goto out;
+			return ret;
 		*val = ret;
 
-		ret = IIO_VAL_INT;
-		break;
+		return IIO_VAL_INT;
 	case IIO_CHAN_INFO_OFFSET:
 		*val = AD7746_CAPDAC_DACP(chip->capdac[chan->channel]
 					  [chan->differential]) * 338646;
 
-		ret = IIO_VAL_INT;
-		break;
+		return IIO_VAL_INT;
 	case IIO_CHAN_INFO_SCALE:
 		switch (chan->type) {
 		case IIO_CAPACITANCE:
 			/* 8.192pf / 2^24 */
 			*val =  0;
 			*val2 = 488;
-			ret = IIO_VAL_INT_PLUS_NANO;
-			break;
+			return IIO_VAL_INT_PLUS_NANO;
 		case IIO_VOLTAGE:
 			/* 1170mV / 2^23 */
 			*val = 1170;
 			if (chan->channel == 1)
 				*val *= 6;
 			*val2 = 23;
-			ret = IIO_VAL_FRACTIONAL_LOG2;
-			break;
+			return IIO_VAL_FRACTIONAL_LOG2;
 		default:
-			ret = -EINVAL;
-			break;
+			return -EINVAL;
 		}
-
-		break;
 	case IIO_CHAN_INFO_SAMP_FREQ:
 		switch (chan->type) {
 		case IIO_CAPACITANCE:
 			idx = (chip->config & AD7746_CONF_CAPFS_MASK) >>
 				AD7746_CONF_CAPFS_SHIFT;
 			*val = ad7746_cap_filter_rate_table[idx][0];
-			ret = IIO_VAL_INT;
-			break;
+			return IIO_VAL_INT;
 		case IIO_VOLTAGE:
 			idx = (chip->config & AD7746_CONF_VTFS_MASK) >>
 				AD7746_CONF_VTFS_SHIFT;
 			*val = ad7746_vt_filter_rate_table[idx][0];
-			ret = IIO_VAL_INT;
-			break;
+			return IIO_VAL_INT;
 		default:
-			ret = -EINVAL;
+			return -EINVAL;
 		}
-		break;
 	default:
-		ret = -EINVAL;
+		return -EINVAL;
 	}
-out:
-	mutex_unlock(&chip->lock);
-	return ret;
 }
 
 static const struct iio_info ad7746_info = {
Signature · 1 line
2.36.1
author maintainer
#13 lore raw

PATCH v2 8/17 staging: iio: cdc: ad7746: Break up use of chan->address and use FIELD_PREP etc

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Instead of encoding several different fields into chan->address use an indirection to a separate per channel structure where the various fields can be expressed in a more readable form. This also allows the register values to be constructed at runtime using FIELD_PREP()

Drop the now redundant _SHIFT macros.

---
 drivers/staging/iio/cdc/ad7746.c | 152 ++++++++++++++++++++-----------
 1 file changed, 99 insertions(+), 53 deletions(-)
drivers/staging/iio/cdc/ad7746.c +99 -53
index 41d9ea4ccb02..a1b0a440728f 100644
@@ -5,6 +5,7 @@
  * Copyright 2011 Analog Devices Inc.
  */
 
+#include <linux/bitfield.h>
 #include <linux/delay.h>
 #include <linux/device.h>
 #include <linux/i2c.h>
@@ -48,11 +49,12 @@
 #define AD7746_CAPSETUP_CACHOP		BIT(0)
 
 /* Voltage/Temperature Setup Register Bit Designations (AD7746_REG_VT_SETUP) */
-#define AD7746_VTSETUP_VTEN		(1 << 7)
-#define AD7746_VTSETUP_VTMD_INT_TEMP	(0 << 5)
-#define AD7746_VTSETUP_VTMD_EXT_TEMP	(1 << 5)
-#define AD7746_VTSETUP_VTMD_VDD_MON	(2 << 5)
-#define AD7746_VTSETUP_VTMD_EXT_VIN	(3 << 5)
+#define AD7746_VTSETUP_VTEN		BIT(7)
+#define AD7746_VTSETUP_VTMD_MASK	GENMASK(6, 5)
+#define AD7746_VTSETUP_VTMD_INT_TEMP	0
+#define AD7746_VTSETUP_VTMD_EXT_TEMP	1
+#define AD7746_VTSETUP_VTMD_VDD_MON	2
+#define AD7746_VTSETUP_VTMD_EXT_VIN	3
 #define AD7746_VTSETUP_EXTREF		BIT(4)
 #define AD7746_VTSETUP_VTSHORT		BIT(1)
 #define AD7746_VTSETUP_VTCHOP		BIT(0)
@@ -64,23 +66,22 @@
 #define AD7746_EXCSETUP_NEXCB		BIT(4)
 #define AD7746_EXCSETUP_EXCA		BIT(3)
 #define AD7746_EXCSETUP_NEXCA		BIT(2)
-#define AD7746_EXCSETUP_EXCLVL(x)	(((x) & 0x3) << 0)
+#define AD7746_EXCSETUP_EXCLVL_MASK	GENMASK(1, 0)
 
 /* Config Register Bit Designations (AD7746_REG_CFG) */
-#define AD7746_CONF_VTFS_SHIFT		6
-#define AD7746_CONF_CAPFS_SHIFT		3
 #define AD7746_CONF_VTFS_MASK		GENMASK(7, 6)
 #define AD7746_CONF_CAPFS_MASK		GENMASK(5, 3)
-#define AD7746_CONF_MODE_IDLE		(0 << 0)
-#define AD7746_CONF_MODE_CONT_CONV	(1 << 0)
-#define AD7746_CONF_MODE_SINGLE_CONV	(2 << 0)
-#define AD7746_CONF_MODE_PWRDN		(3 << 0)
-#define AD7746_CONF_MODE_OFFS_CAL	(5 << 0)
-#define AD7746_CONF_MODE_GAIN_CAL	(6 << 0)
+#define AD7746_CONF_MODE_MASK		GENMASK(2, 0)
+#define AD7746_CONF_MODE_IDLE		0
+#define AD7746_CONF_MODE_CONT_CONV	1
+#define AD7746_CONF_MODE_SINGLE_CONV	2
+#define AD7746_CONF_MODE_PWRDN		3
+#define AD7746_CONF_MODE_OFFS_CAL	5
+#define AD7746_CONF_MODE_GAIN_CAL	6
 
 /* CAPDAC Register Bit Designations (AD7746_REG_CAPDACx) */
 #define AD7746_CAPDAC_DACEN		BIT(7)
-#define AD7746_CAPDAC_DACP(x)		((x) & 0x7F)
+#define AD7746_CAPDAC_DACP_MASK		0x7F
 
 struct ad7746_chip_info {
 	struct i2c_client *client;
@@ -107,6 +108,52 @@ enum ad7746_chan {
 	CIN2_DIFF,
 };
 
+struct ad7746_chan_info {
+	u8 addr;
+	union {
+		u8 vtmd;
+		struct { /* CAP SETUP fields */
+			unsigned int cin2 : 1;
+			unsigned int capdiff : 1;
+		};
+	};
+};
+
+static const struct ad7746_chan_info ad7746_chan_info[] = {
+	[VIN] = {
+		.addr = AD7746_REG_VT_DATA_HIGH,
+		.vtmd = AD7746_VTSETUP_VTMD_EXT_VIN,
+	},
+	[VIN_VDD] = {
+		.addr = AD7746_REG_VT_DATA_HIGH,
+		.vtmd = AD7746_VTSETUP_VTMD_VDD_MON,
+	},
+	[TEMP_INT] = {
+		.addr = AD7746_REG_VT_DATA_HIGH,
+		.vtmd = AD7746_VTSETUP_VTMD_INT_TEMP,
+	},
+	[TEMP_EXT] = {
+		.addr = AD7746_REG_VT_DATA_HIGH,
+		.vtmd = AD7746_VTSETUP_VTMD_EXT_TEMP,
+	},
+	[CIN1] = {
+		.addr = AD7746_REG_CAP_DATA_HIGH,
+	},
+	[CIN1_DIFF] = {
+		.addr =  AD7746_REG_CAP_DATA_HIGH,
+		.capdiff = 1,
+	},
+	[CIN2] = {
+		.addr = AD7746_REG_CAP_DATA_HIGH,
+		.cin2 = 1,
+	},
+	[CIN2_DIFF] = {
+		.addr = AD7746_REG_CAP_DATA_HIGH,
+		.cin2 = 1,
+		.capdiff = 1,
+	},
+};
+
 static const struct iio_chan_spec ad7746_channels[] = {
 	[VIN] = {
 		.type = IIO_VOLTAGE,
@@ -114,8 +161,7 @@ static const struct iio_chan_spec ad7746_channels[] = {
 		.channel = 0,
 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ),
-		.address = AD7746_REG_VT_DATA_HIGH << 8 |
-			AD7746_VTSETUP_VTMD_EXT_VIN,
+		.address = VIN,
 	},
 	[VIN_VDD] = {
 		.type = IIO_VOLTAGE,
@@ -124,24 +170,21 @@ static const struct iio_chan_spec ad7746_channels[] = {
 		.extend_name = "supply",
 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ),
-		.address = AD7746_REG_VT_DATA_HIGH << 8 |
-			AD7746_VTSETUP_VTMD_VDD_MON,
+		.address = VIN_VDD,
 	},
 	[TEMP_INT] = {
 		.type = IIO_TEMP,
 		.indexed = 1,
 		.channel = 0,
 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
-		.address = AD7746_REG_VT_DATA_HIGH << 8 |
-			AD7746_VTSETUP_VTMD_INT_TEMP,
+		.address = TEMP_INT,
 	},
 	[TEMP_EXT] = {
 		.type = IIO_TEMP,
 		.indexed = 1,
 		.channel = 1,
 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
-		.address = AD7746_REG_VT_DATA_HIGH << 8 |
-			AD7746_VTSETUP_VTMD_EXT_TEMP,
+		.address = TEMP_EXT,
 	},
 	[CIN1] = {
 		.type = IIO_CAPACITANCE,
@@ -151,7 +194,7 @@ static const struct iio_chan_spec ad7746_channels[] = {
 		BIT(IIO_CHAN_INFO_CALIBSCALE) | BIT(IIO_CHAN_INFO_OFFSET),
 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_CALIBBIAS) |
 		BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_SAMP_FREQ),
-		.address = AD7746_REG_CAP_DATA_HIGH << 8,
+		.address = CIN1,
 	},
 	[CIN1_DIFF] = {
 		.type = IIO_CAPACITANCE,
@@ -163,8 +206,7 @@ static const struct iio_chan_spec ad7746_channels[] = {
 		BIT(IIO_CHAN_INFO_CALIBSCALE) | BIT(IIO_CHAN_INFO_OFFSET),
 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_CALIBBIAS) |
 		BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_SAMP_FREQ),
-		.address = AD7746_REG_CAP_DATA_HIGH << 8 |
-			AD7746_CAPSETUP_CAPDIFF
+		.address = CIN1_DIFF,
 	},
 	[CIN2] = {
 		.type = IIO_CAPACITANCE,
@@ -174,8 +216,7 @@ static const struct iio_chan_spec ad7746_channels[] = {
 		BIT(IIO_CHAN_INFO_CALIBSCALE) | BIT(IIO_CHAN_INFO_OFFSET),
 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_CALIBBIAS) |
 		BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_SAMP_FREQ),
-		.address = AD7746_REG_CAP_DATA_HIGH << 8 |
-			AD7746_CAPSETUP_CIN2,
+		.address = CIN2,
 	},
 	[CIN2_DIFF] = {
 		.type = IIO_CAPACITANCE,
@@ -187,8 +228,7 @@ static const struct iio_chan_spec ad7746_channels[] = {
 		BIT(IIO_CHAN_INFO_CALIBSCALE) | BIT(IIO_CHAN_INFO_OFFSET),
 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_CALIBBIAS) |
 		BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_SAMP_FREQ),
-		.address = AD7746_REG_CAP_DATA_HIGH << 8 |
-			AD7746_CAPSETUP_CAPDIFF | AD7746_CAPSETUP_CIN2,
+		.address = CIN2_DIFF,
 	}
 };
 
@@ -224,10 +264,13 @@ static int ad7746_select_channel(struct iio_dev *indio_dev,
 
 	switch (chan->type) {
 	case IIO_CAPACITANCE:
-		cap_setup = (chan->address & 0xFF) | AD7746_CAPSETUP_CAPEN;
+		cap_setup = FIELD_PREP(AD7746_CAPSETUP_CIN2,
+				       ad7746_chan_info[chan->address].cin2) |
+			FIELD_PREP(AD7746_CAPSETUP_CAPDIFF,
+				   ad7746_chan_info[chan->address].capdiff) |
+			FIELD_PREP(AD7746_CAPSETUP_CAPEN, 1);
 		vt_setup = chip->vt_setup & ~AD7746_VTSETUP_VTEN;
-		idx = (chip->config & AD7746_CONF_CAPFS_MASK) >>
-			AD7746_CONF_CAPFS_SHIFT;
+		idx = FIELD_GET(AD7746_CONF_CAPFS_MASK, chip->config);
 		delay = ad7746_cap_filter_rate_table[idx][1];
 
 		ret = ad7746_set_capdac(chip, chan->channel);
@@ -239,10 +282,11 @@ static int ad7746_select_channel(struct iio_dev *indio_dev,
 		break;
 	case IIO_VOLTAGE:
 	case IIO_TEMP:
-		vt_setup = (chan->address & 0xFF) | AD7746_VTSETUP_VTEN;
+		vt_setup = FIELD_PREP(AD7746_VTSETUP_VTMD_MASK,
+				      ad7746_chan_info[chan->address].vtmd) |
+			FIELD_PREP(AD7746_VTSETUP_VTEN, 1);
 		cap_setup = chip->cap_setup & ~AD7746_CAPSETUP_CAPEN;
-		idx = (chip->config & AD7746_CONF_VTFS_MASK) >>
-			AD7746_CONF_VTFS_SHIFT;
+		idx = FIELD_GET(AD7746_CONF_VTFS_MASK, chip->config);
 		delay = ad7746_cap_filter_rate_table[idx][1];
 		break;
 	default:
@@ -325,7 +369,8 @@ static ssize_t ad7746_start_offset_calib(struct device *dev,
 		return ret;
 
 	return ad7746_start_calib(dev, attr, buf, len,
-				  AD7746_CONF_MODE_OFFS_CAL);
+				  FIELD_PREP(AD7746_CONF_MODE_MASK,
+					     AD7746_CONF_MODE_OFFS_CAL));
 }
 
 static ssize_t ad7746_start_gain_calib(struct device *dev,
@@ -340,7 +385,8 @@ static ssize_t ad7746_start_gain_calib(struct device *dev,
 		return ret;
 
 	return ad7746_start_calib(dev, attr, buf, len,
-				  AD7746_CONF_MODE_GAIN_CAL);
+				  FIELD_PREP(AD7746_CONF_MODE_MASK,
+					     AD7746_CONF_MODE_GAIN_CAL));
 }
 
 static IIO_DEVICE_ATTR(in_capacitance0_calibbias_calibration,
@@ -367,7 +413,7 @@ static int ad7746_store_cap_filter_rate_setup(struct ad7746_chip_info *chip,
 		i = ARRAY_SIZE(ad7746_cap_filter_rate_table) - 1;
 
 	chip->config &= ~AD7746_CONF_CAPFS_MASK;
-	chip->config |= i << AD7746_CONF_CAPFS_SHIFT;
+	chip->config |= FIELD_PREP(AD7746_CONF_CAPFS_MASK, i);
 
 	return 0;
 }
@@ -385,7 +431,7 @@ static int ad7746_store_vt_filter_rate_setup(struct ad7746_chip_info *chip,
 		i = ARRAY_SIZE(ad7746_vt_filter_rate_table) - 1;
 
 	chip->config &= ~AD7746_CONF_VTFS_MASK;
-	chip->config |= i << AD7746_CONF_VTFS_SHIFT;
+	chip->config |= FIELD_PREP(AD7746_CONF_VTFS_MASK, i);
 
 	return 0;
 }
@@ -468,7 +514,7 @@ static int ad7746_write_raw(struct iio_dev *indio_dev,
 		val /= 338646;
 		mutex_lock(&chip->lock);
 		chip->capdac[chan->channel][chan->differential] = val > 0 ?
-			AD7746_CAPDAC_DACP(val) | AD7746_CAPDAC_DACEN : 0;
+			FIELD_PREP(AD7746_CAPDAC_DACP_MASK, val) | AD7746_CAPDAC_DACEN : 0;
 
 		ret = ad7746_set_capdac(chip, chan->channel);
 		if (ret < 0) {
@@ -517,14 +563,16 @@ static int ad7746_read_channel(struct iio_dev *indio_dev,
 		return ret;
 	delay = ret;
 
-	regval = chip->config | AD7746_CONF_MODE_SINGLE_CONV;
+	regval = chip->config | FIELD_PREP(AD7746_CONF_MODE_MASK,
+					   AD7746_CONF_MODE_SINGLE_CONV);
 	ret = i2c_smbus_write_byte_data(chip->client, AD7746_REG_CFG, regval);
 	if (ret < 0)
 		return ret;
 
 	msleep(delay);
 	/* Now read the actual register */
-	ret = i2c_smbus_read_i2c_block_data(chip->client,  chan->address >> 8,
+	ret = i2c_smbus_read_i2c_block_data(chip->client,
+					    ad7746_chan_info[chan->address].addr,
 					    sizeof(data), data);
 	if (ret < 0)
 		return ret;
@@ -598,8 +646,8 @@ static int ad7746_read_raw(struct iio_dev *indio_dev,
 
 		return IIO_VAL_INT;
 	case IIO_CHAN_INFO_OFFSET:
-		*val = AD7746_CAPDAC_DACP(chip->capdac[chan->channel]
-					  [chan->differential]) * 338646;
+		*val = FIELD_GET(AD7746_CAPDAC_DACP_MASK,
+				 chip->capdac[chan->channel][chan->differential]) * 338646;
 
 		return IIO_VAL_INT;
 	case IIO_CHAN_INFO_SCALE:
@@ -622,13 +670,11 @@ static int ad7746_read_raw(struct iio_dev *indio_dev,
 	case IIO_CHAN_INFO_SAMP_FREQ:
 		switch (chan->type) {
 		case IIO_CAPACITANCE:
-			idx = (chip->config & AD7746_CONF_CAPFS_MASK) >>
-				AD7746_CONF_CAPFS_SHIFT;
+			idx = FIELD_GET(AD7746_CONF_CAPFS_MASK, chip->config);
 			*val = ad7746_cap_filter_rate_table[idx][0];
 			return IIO_VAL_INT;
 		case IIO_VOLTAGE:
-			idx = (chip->config & AD7746_CONF_VTFS_MASK) >>
-				AD7746_CONF_VTFS_SHIFT;
+			idx = FIELD_GET(AD7746_CONF_VTFS_MASK, chip->config);
 			*val = ad7746_vt_filter_rate_table[idx][0];
 			return IIO_VAL_INT;
 		default:
@@ -694,16 +740,16 @@ static int ad7746_probe(struct i2c_client *client,
 	if (!ret) {
 		switch (vdd_permille) {
 		case 125:
-			regval |= AD7746_EXCSETUP_EXCLVL(0);
+			regval |= FIELD_PREP(AD7746_EXCSETUP_EXCLVL_MASK, 0);
 			break;
 		case 250:
-			regval |= AD7746_EXCSETUP_EXCLVL(1);
+			regval |= FIELD_PREP(AD7746_EXCSETUP_EXCLVL_MASK, 1);
 			break;
 		case 375:
-			regval |= AD7746_EXCSETUP_EXCLVL(2);
+			regval |= FIELD_PREP(AD7746_EXCSETUP_EXCLVL_MASK, 2);
 			break;
 		case 500:
-			regval |= AD7746_EXCSETUP_EXCLVL(3);
+			regval |= FIELD_PREP(AD7746_EXCSETUP_EXCLVL_MASK, 3);
 			break;
 		default:
 			break;
Signature · 1 line
2.36.1
author maintainer
#14 lore raw

PATCH v2 9/17 staging: iio: cdc: ad7746: Drop usused i2c_set_clientdata()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

As the comment states, this was only used in remove() and now there is no explicit remove() function to make use of it.

---
 drivers/staging/iio/cdc/ad7746.c | 2 --
 1 file changed, 2 deletions(-)
drivers/staging/iio/cdc/ad7746.c +0 -2
index a1b0a440728f..7eaffb90e8c3 100644
@@ -706,8 +706,6 @@ static int ad7746_probe(struct i2c_client *client,
 		return -ENOMEM;
 	chip = iio_priv(indio_dev);
 	mutex_init(&chip->lock);
-	/* this is only used for device removal purposes */
-	i2c_set_clientdata(client, indio_dev);
 
 	chip->client = client;
 	chip->capdac_set = -1;
Signature · 1 line
2.36.1
author maintainer
#15 lore raw

PATCH v2 10/17 staging: iio: cdc: ad7746: Use _raw and _scale for temperature channels.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Performing the maths to rescale a 24 bit raw reading within the driver was resulting in precision losses. So make that userspace's problem by exporting the scale and letting the maths be done in userspace with appropriate precision. Issue identified using roadtester testing framework.

---
 drivers/staging/iio/cdc/ad7746.c | 23 ++++++++---------------
 1 file changed, 8 insertions(+), 15 deletions(-)
drivers/staging/iio/cdc/ad7746.c +8 -15
index 7eaffb90e8c3..3caba37afc63 100644
@@ -176,14 +176,16 @@ static const struct iio_chan_spec ad7746_channels[] = {
 		.type = IIO_TEMP,
 		.indexed = 1,
 		.channel = 0,
-		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
 		.address = TEMP_INT,
 	},
 	[TEMP_EXT] = {
 		.type = IIO_TEMP,
 		.indexed = 1,
 		.channel = 1,
-		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
 		.address = TEMP_EXT,
 	},
 	[CIN1] = {
@@ -579,18 +581,6 @@ static int ad7746_read_channel(struct iio_dev *indio_dev,
 
 	*val = get_unaligned_be24(data) - 0x800000;
 
-	switch (chan->type) {
-	case IIO_TEMP:
-		/*
-		 * temperature in milli degrees Celsius
-		 * T = ((*val / 2048) - 4096) * 1000
-		 */
-		*val = (*val  * 125) / 256;
-		break;
-	default:
-		break;
-	}
-
 	return 0;
 }
 
@@ -605,7 +595,6 @@ static int ad7746_read_raw(struct iio_dev *indio_dev,
 
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
-	case IIO_CHAN_INFO_PROCESSED:
 		mutex_lock(&chip->lock);
 		ret = ad7746_read_channel(indio_dev, chan, val);
 		mutex_unlock(&chip->lock);
@@ -664,6 +653,10 @@ static int ad7746_read_raw(struct iio_dev *indio_dev,
 				*val *= 6;
 			*val2 = 23;
 			return IIO_VAL_FRACTIONAL_LOG2;
+		case IIO_TEMP:
+			*val = 125;
+			*val2 = 8;
+			return IIO_VAL_FRACTIONAL_LOG2;
 		default:
 			return -EINVAL;
 		}
Signature · 1 line
2.36.1
author maintainer
#16 lore raw

PATCH v2 11/17 iio: core: Introduce _inputoffset for differential channels

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Address an ABI gap for device where the offset of both lines in a differential pair may be controlled so as to allow a wider range of inputs, but without having any direct effect of the differential measurement.

_offset cannot be used as to remain in line with existing usage, userspace would be expected to apply it as (_raw + _offset) * scale whereas _inputoffset is not.
Similarly calibbias is expected to tweak the measurement seen, not the adjust the two lines of the differential pair.

Needed for in_capacitanceX-capacitanceY_inputoffset for the AD7746 CDC driver.

---
I''m not entirely happy with the naming on this one, so if anyone has a better idea I'm definitely open to it!

---
 Documentation/ABI/testing/sysfs-bus-iio | 13 +++++++++++++
 drivers/iio/industrialio-core.c         |  1 +
 include/linux/iio/types.h               |  1 +
 3 files changed, 15 insertions(+)
Documentation/ABI/testing/sysfs-bus-iio +13 -0
index d3a0c0ef8948..132eb4e849f9 100644
@@ -204,6 +204,19 @@ Description:
 		is required is a consistent labeling.  Units after application
 		of scale and offset are nanofarads.
 
+What:		/sys/.../iio:deviceX/in_capacitableY-capacitanceZ_inputoffset
+KernelVersion:	5.19
+Contact:	[email protected]
+Description:
+		For differential channels, this an offset that is applied
+		equally to both inputs. As the reading is of the difference
+		between the two inputs this should not be applied to the _raw
+		reading by userspace (unlike _offset) and unlike calibbias
+		it does not affect the differential value measured because
+		the effect of _inputoffset cancels out across the two inputs
+		that make up the differential pair. It is expressed with
+		the same scaling as _raw.
+
 What:		/sys/bus/iio/devices/iio:deviceX/in_temp_raw
 What:		/sys/bus/iio/devices/iio:deviceX/in_tempX_raw
 What:		/sys/bus/iio/devices/iio:deviceX/in_temp_x_raw
drivers/iio/industrialio-core.c +1 -0
index 6b401f63805f..ad1c2354502d 100644
@@ -168,6 +168,7 @@ static const char * const iio_chan_info_postfix[] = {
 	[IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio",
 	[IIO_CHAN_INFO_THERMOCOUPLE_TYPE] = "thermocouple_type",
 	[IIO_CHAN_INFO_CALIBAMBIENT] = "calibambient",
+	[IIO_CHAN_INFO_INPUTOFFSET] = "inputoffset",
 };
 /**
  * iio_device_id() - query the unique ID for the device
include/linux/iio/types.h +1 -0
index a7aa91f3a8dc..c7f79a251a14 100644
@@ -63,6 +63,7 @@ enum iio_chan_info_enum {
 	IIO_CHAN_INFO_OVERSAMPLING_RATIO,
 	IIO_CHAN_INFO_THERMOCOUPLE_TYPE,
 	IIO_CHAN_INFO_CALIBAMBIENT,
+	IIO_CHAN_INFO_INPUTOFFSET,
 };
 
 #endif /* _IIO_TYPES_H_ */
Signature · 1 line
2.36.1
#17 lore raw

Re: [PATCH v2 11/17] iio: core: Introduce _inputoffset for differential channels

Quoted 14 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Address an ABI gap for device where the offset of both lines in a differential pair may be controlled so as to allow a wider range of inputs, but without having any direct effect of the differential measurement.

_offset cannot be used as to remain in line with existing usage, userspace would be expected to apply it as (_raw + _offset) * scale whereas _inputoffset is not.
Similarly calibbias is expected to tweak the measurement seen, not the adjust the two lines of the differential pair.

Quoted 8 lines

Needed for in_capacitanceX-capacitanceY_inputoffset for the AD7746 CDC driver.

---
I''m not entirely happy with the naming on this one, so if anyone has a better idea I'm definitely open to it!

I'm not sure I got the idea behind this, but would be "tare" / "tarepoint" / alike suitable here?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#18 lore raw

Re: [PATCH v2 11/17] iio: core: Introduce _inputoffset for differential channels

Quoted 18 lines · On Mon, 20 Jun 2022 01:20:08 +0200 Andy Shevchenko <[email protected]>…
Quoted 17 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Address an ABI gap for device where the offset of both lines in a differential pair may be controlled so as to allow a wider range of inputs, but without having any direct effect of the differential measurement.

_offset cannot be used as to remain in line with existing usage, userspace would be expected to apply it as (_raw + _offset) * scale whereas _inputoffset is not.
Similarly calibbias is expected to tweak the measurement seen, not the adjust the two lines of the differential pair.

Needed for in_capacitanceX-capacitanceY_inputoffset for the AD7746 CDC driver.

Quoted 9 lines
Quoted 5 lines

---
I''m not entirely happy with the naming on this one, so if anyone has a better idea I'm definitely open to it!

I'm not sure I got the idea behind this, but would be "tare" / "tarepoint" / alike suitable here?

Hmm I don't think tare maps well as I've never seen it applied to a differential channel like this. Not really seen it much used outside of checkweighing (the mention brought back some horrible memories of debugging software for high speed check weighers :)

So to try and explain what this is doing in more depth.

This is basically applying a negative offset X to both the P(ostitive) and N(egative) lines. Hence

measured capacitance = (P - X) - (N - X)

                     = P - N

the aim of X being to keep the signal hitting some internal point on the device within a range that is measurable.

The relevant text on the datasheet is:

"The CAPDAC can be understood as a negative capacitance connected internally to the CIN pin."

"Each of the two input capacitances CX and CY between the EXC and CIN pins must be less than 4 pF (without using the CAPDACs) or must be less than 21 pF and balanced by the CAPDACs. Balancing by the CAPDACs means that both CX–CAPDAC(+) and CY–CAPDAC(–) are less than 4 pF."

So basically if you have both sides of the differential pair that are too large (>4 pF) then you can drag them together down to that range by adding negative capacitance.

Ouch that's confusing :)

Jonathan

#19 lore raw

Re: [PATCH v2 11/17] iio: core: Introduce _inputoffset for differential channels

Quoted 2 lines · Jonathan Cameron

On Mon, 20 Jun 2022 01:20:08 +0200
Andy Shevchenko <[email protected]> wrote:

...

Quoted 22 lines

So to try and explain what this is doing in more depth.

This is basically applying a negative offset X to both the P(ostitive) and N(egative) lines. Hence

measured capacitance = (P - X) - (N - X)

                     = P - N

the aim of X being to keep the signal hitting some internal point on the device within a range that is measurable.

The relevant text on the datasheet is:

"The CAPDAC can be understood as a negative capacitance connected internally to the CIN pin."

"Each of the two input capacitances CX and CY between the EXC and CIN pins must be less than 4 pF (without using the CAPDACs) or must be less than 21 pF and balanced by the CAPDACs. Balancing by the CAPDACs means that both CX–CAPDAC(+) and CY–CAPDAC(–) are less than 4 pF."

Quoted 3 lines

So basically if you have both sides of the differential pair that are too large (>4 pF) then you can drag them together down to that range by adding negative capacitance.

Ah, in a long wording it's "measurement window offset". Dunno how to make it shorter.
Also, 0-point on the axis in math.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#20 lore raw

Re: [PATCH v2 11/17] iio: core: Introduce _inputoffset for differential channels

Quoted 28 lines · On Mon, 20 Jun 2022 20:35:57 +0200 Andy Shevchenko <[email protected]>…
Quoted 2 lines · Jonathan Cameron

On Mon, 20 Jun 2022 01:20:08 +0200
Andy Shevchenko <[email protected]> wrote:

...

Quoted 22 lines

So to try and explain what this is doing in more depth.

This is basically applying a negative offset X to both the P(ostitive) and N(egative) lines. Hence

measured capacitance = (P - X) - (N - X)

                     = P - N

the aim of X being to keep the signal hitting some internal point on the device within a range that is measurable.

The relevant text on the datasheet is:

"The CAPDAC can be understood as a negative capacitance connected internally to the CIN pin."

"Each of the two input capacitances CX and CY between the EXC and CIN pins must be less than 4 pF (without using the CAPDACs) or must be less than 21 pF and balanced by the CAPDACs. Balancing by the CAPDACs means that both CX–CAPDAC(+) and CY–CAPDAC(–) are less than 4 pF."

Quoted 8 lines
Quoted 3 lines

So basically if you have both sides of the differential pair that are too large (>4 pF) then you can drag them together down to that range by adding negative capacitance.

Ah, in a long wording it's "measurement window offset". Dunno how to make it shorter.
Also, 0-point on the axis in math.

Hmm. Both good descriptions so I'll add something along these lines to the description.

Characters are cheap aren't then? :)

in_capacitanceY-capacitanceZ_zero_point
isn't too bad - will think more. No connection to offset which is what it would be for a single ended channel though...

Jonathan

author maintainer
#21 lore raw

Re: [PATCH v2 11/17] iio: core: Introduce _inputoffset for differential channels

In reply to Jonathan Cameron
Quoted 32 lines · On Sun, 19 Jun 2022 19:58:33 +0100 Jonathan Cameron <[email protected]> wrote:

From: Jonathan Cameron <[email protected]>

Address an ABI gap for device where the offset of both lines in a differential pair may be controlled so as to allow a wider range of inputs, but without having any direct effect of the differential measurement.

_offset cannot be used as to remain in line with existing usage, userspace would be expected to apply it as (_raw + _offset) * scale whereas _inputoffset is not.
Similarly calibbias is expected to tweak the measurement seen, not the adjust the two lines of the differential pair.

Needed for in_capacitanceX-capacitanceY_inputoffset for the AD7746 CDC driver.

---
I''m not entirely happy with the naming on this one, so if anyone has a better idea I'm definitely open to it!

---
 Documentation/ABI/testing/sysfs-bus-iio | 13 +++++++++++++
 drivers/iio/industrialio-core.c         |  1 +
 include/linux/iio/types.h               |  1 +
 3 files changed, 15 insertions(+)
Documentation/ABI/testing/sysfs-bus-iio +0 -0
index d3a0c0ef8948..132eb4e849f9 100644

Documentation/ABI/testing/sysfs-bus-iio Description: · line 207

Quoted 4 lines
Documentation/ABI/testing/sysfs-bus-iio +1 -0
@@ -204,6 +204,19 @@ Description:
 		is required is a consistent labeling.  Units after application
 		of scale and offset are nanofarads.
 
+What:		/sys/.../iio:deviceX/in_capacitableY-capacitanceZ_inputoffset

capacitable? What was I drinking that day... Will fix.

Quoted 39 lines
Documentation/ABI/testing/sysfs-bus-iio +12 -0
+KernelVersion:	5.19
+Contact:	[email protected]
+Description:
+		For differential channels, this an offset that is applied
+		equally to both inputs. As the reading is of the difference
+		between the two inputs this should not be applied to the _raw
+		reading by userspace (unlike _offset) and unlike calibbias
+		it does not affect the differential value measured because
+		the effect of _inputoffset cancels out across the two inputs
+		that make up the differential pair. It is expressed with
+		the same scaling as _raw.
+
 What:		/sys/bus/iio/devices/iio:deviceX/in_temp_raw
 What:		/sys/bus/iio/devices/iio:deviceX/in_tempX_raw
 What:		/sys/bus/iio/devices/iio:deviceX/in_temp_x_raw
drivers/iio/industrialio-core.c +1 -0
index 6b401f63805f..ad1c2354502d 100644
@@ -168,6 +168,7 @@ static const char * const iio_chan_info_postfix[] = {
 	[IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio",
 	[IIO_CHAN_INFO_THERMOCOUPLE_TYPE] = "thermocouple_type",
 	[IIO_CHAN_INFO_CALIBAMBIENT] = "calibambient",
+	[IIO_CHAN_INFO_INPUTOFFSET] = "inputoffset",
 };
 /**
  * iio_device_id() - query the unique ID for the device
include/linux/iio/types.h +1 -0
index a7aa91f3a8dc..c7f79a251a14 100644
@@ -63,6 +63,7 @@ enum iio_chan_info_enum {
 	IIO_CHAN_INFO_OVERSAMPLING_RATIO,
 	IIO_CHAN_INFO_THERMOCOUPLE_TYPE,
 	IIO_CHAN_INFO_CALIBAMBIENT,
+	IIO_CHAN_INFO_INPUTOFFSET,
 };
 
 #endif /* _IIO_TYPES_H_ */
author maintainer
#22 lore raw

PATCH v2 12/17 staging: iio: cdc: ad7746: Switch from _offset to _inputoffset for differential channels.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

As this offset is applied equally to both lines of the differential pair, _ofset should not be used. Use the new ABI _inputoffset instead to avoid userspace software applying this value when calculating real value = (_raw + _offset) * _scale

Also add a comment to explain why an offset of 0x800000 is applied within the driver rather than exposed to userspace.

---
 drivers/staging/iio/cdc/ad7746.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/staging/iio/cdc/ad7746.c +8 -2
index 3caba37afc63..3801bd0a6176 100644
@@ -205,7 +205,7 @@ static const struct iio_chan_spec ad7746_channels[] = {
 		.channel = 0,
 		.channel2 = 2,
 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
-		BIT(IIO_CHAN_INFO_CALIBSCALE) | BIT(IIO_CHAN_INFO_OFFSET),
+		BIT(IIO_CHAN_INFO_CALIBSCALE) | BIT(IIO_CHAN_INFO_INPUTOFFSET),
 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_CALIBBIAS) |
 		BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_SAMP_FREQ),
 		.address = CIN1_DIFF,
@@ -227,7 +227,7 @@ static const struct iio_chan_spec ad7746_channels[] = {
 		.channel = 1,
 		.channel2 = 3,
 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
-		BIT(IIO_CHAN_INFO_CALIBSCALE) | BIT(IIO_CHAN_INFO_OFFSET),
+		BIT(IIO_CHAN_INFO_CALIBSCALE) | BIT(IIO_CHAN_INFO_INPUTOFFSET),
 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_CALIBBIAS) |
 		BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_SAMP_FREQ),
 		.address = CIN2_DIFF,
@@ -504,6 +504,7 @@ static int ad7746_write_raw(struct iio_dev *indio_dev,
 
 		return 0;
 	case IIO_CHAN_INFO_OFFSET:
+	case IIO_CHAN_INFO_INPUTOFFSET:
 		if (val < 0 || val > 43008000) /* 21pF */
 			return -EINVAL;
 
@@ -579,6 +580,10 @@ static int ad7746_read_channel(struct iio_dev *indio_dev,
 	if (ret < 0)
 		return ret;
 
+	/*
+	 * Offset applied internally becaue the _offset userspace interface is
+	 * needed for the CAP DACs which apply a controllable offset.
+	 */
 	*val = get_unaligned_be24(data) - 0x800000;
 
 	return 0;
@@ -635,6 +640,7 @@ static int ad7746_read_raw(struct iio_dev *indio_dev,
 
 		return IIO_VAL_INT;
 	case IIO_CHAN_INFO_OFFSET:
+	case IIO_CHAN_INFO_INPUTOFFSET:
 		*val = FIELD_GET(AD7746_CAPDAC_DACP_MASK,
 				 chip->capdac[chan->channel][chan->differential]) * 338646;
 
Signature · 1 line
2.36.1
author maintainer
#23 lore raw

PATCH v2 13/17 staging: iio: cdc: ad7746: Use read_avail() rather than opencoding.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Switch over to the IIO core handling for _available attributes making them available for in kernel users and enforcing correct naming etc automatically.

---
 drivers/staging/iio/cdc/ad7746.c | 39 +++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 6 deletions(-)
drivers/staging/iio/cdc/ad7746.c +33 -6
index 3801bd0a6176..70dfb35d6135 100644
@@ -161,6 +161,7 @@ static const struct iio_chan_spec ad7746_channels[] = {
 		.channel = 0,
 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ),
+		.info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_SAMP_FREQ),
 		.address = VIN,
 	},
 	[VIN_VDD] = {
@@ -170,6 +171,7 @@ static const struct iio_chan_spec ad7746_channels[] = {
 		.extend_name = "supply",
 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ),
+		.info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_SAMP_FREQ),
 		.address = VIN_VDD,
 	},
 	[TEMP_INT] = {
@@ -196,6 +198,7 @@ static const struct iio_chan_spec ad7746_channels[] = {
 		BIT(IIO_CHAN_INFO_CALIBSCALE) | BIT(IIO_CHAN_INFO_OFFSET),
 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_CALIBBIAS) |
 		BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_SAMP_FREQ),
+		.info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_SAMP_FREQ),
 		.address = CIN1,
 	},
 	[CIN1_DIFF] = {
@@ -208,6 +211,7 @@ static const struct iio_chan_spec ad7746_channels[] = {
 		BIT(IIO_CHAN_INFO_CALIBSCALE) | BIT(IIO_CHAN_INFO_INPUTOFFSET),
 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_CALIBBIAS) |
 		BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_SAMP_FREQ),
+		.info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_SAMP_FREQ),
 		.address = CIN1_DIFF,
 	},
 	[CIN2] = {
@@ -218,6 +222,7 @@ static const struct iio_chan_spec ad7746_channels[] = {
 		BIT(IIO_CHAN_INFO_CALIBSCALE) | BIT(IIO_CHAN_INFO_OFFSET),
 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_CALIBBIAS) |
 		BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_SAMP_FREQ),
+		.info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_SAMP_FREQ),
 		.address = CIN2,
 	},
 	[CIN2_DIFF] = {
@@ -230,6 +235,7 @@ static const struct iio_chan_spec ad7746_channels[] = {
 		BIT(IIO_CHAN_INFO_CALIBSCALE) | BIT(IIO_CHAN_INFO_INPUTOFFSET),
 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_CALIBBIAS) |
 		BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_SAMP_FREQ),
+		.info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_SAMP_FREQ),
 		.address = CIN2_DIFF,
 	}
 };
@@ -438,18 +444,12 @@ static int ad7746_store_vt_filter_rate_setup(struct ad7746_chip_info *chip,
 	return 0;
 }
 
-static IIO_CONST_ATTR(in_voltage_sampling_frequency_available, "50 31 16 8");
-static IIO_CONST_ATTR(in_capacitance_sampling_frequency_available,
-		       "91 84 50 26 16 13 11 9");
-
 static struct attribute *ad7746_attributes[] = {
 	&iio_dev_attr_in_capacitance0_calibbias_calibration.dev_attr.attr,
 	&iio_dev_attr_in_capacitance0_calibscale_calibration.dev_attr.attr,
 	&iio_dev_attr_in_capacitance1_calibscale_calibration.dev_attr.attr,
 	&iio_dev_attr_in_capacitance1_calibbias_calibration.dev_attr.attr,
 	&iio_dev_attr_in_voltage0_calibscale_calibration.dev_attr.attr,
-	&iio_const_attr_in_voltage_sampling_frequency_available.dev_attr.attr,
-	&iio_const_attr_in_capacitance_sampling_frequency_available.dev_attr.attr,
 	NULL,
 };
 
@@ -552,6 +552,32 @@ static int ad7746_write_raw(struct iio_dev *indio_dev,
 	}
 }
 
+static const int ad7746_v_samp_freq[] = { 50, 31, 16, 8, };
+static const int ad7746_cap_samp_freq[] = { 91, 84, 50, 26, 16, 13, 11, 9, };
+
+static int ad7746_read_avail(struct iio_dev *indio_dev,
+			     struct iio_chan_spec const *chan, const int **vals,
+			     int *type, int *length, long mask)
+{
+	if (mask != IIO_CHAN_INFO_SAMP_FREQ)
+		return -EINVAL;
+
+	switch (chan->type) {
+	case IIO_VOLTAGE:
+		*vals = ad7746_v_samp_freq;
+		*length = ARRAY_SIZE(ad7746_v_samp_freq);
+		break;
+	case IIO_CAPACITANCE:
+		*vals = ad7746_cap_samp_freq;
+		*length = ARRAY_SIZE(ad7746_cap_samp_freq);
+		break;
+	default:
+		return -EINVAL;
+	}
+	*type = IIO_VAL_INT;
+	return IIO_AVAIL_LIST;
+}
+
 static int ad7746_read_channel(struct iio_dev *indio_dev,
 			       struct iio_chan_spec const *chan,
 			       int *val)
@@ -687,6 +713,7 @@ static int ad7746_read_raw(struct iio_dev *indio_dev,
 static const struct iio_info ad7746_info = {
 	.attrs = &ad7746_attribute_group,
 	.read_raw = ad7746_read_raw,
+	.read_avail = ad7746_read_avail,
 	.write_raw = ad7746_write_raw,
 };
 
Signature · 1 line
2.36.1
author maintainer
#24 lore raw

PATCH v2 14/17 staging: iio: ad7746: White space cleanup

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Tidy up some trivial whitespace issues.

---
 drivers/staging/iio/cdc/ad7746.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)
drivers/staging/iio/cdc/ad7746.c +7 -10
index 70dfb35d6135..480f2734b5ad 100644
@@ -19,9 +19,7 @@
 #include <linux/iio/iio.h>
 #include <linux/iio/sysfs.h>
 
-/*
- * AD7746 Register Definition
- */
+/* AD7746 Register Definition */
 
 #define AD7746_REG_STATUS		0
 #define AD7746_REG_CAP_DATA_HIGH	1
@@ -242,12 +240,12 @@ static const struct iio_chan_spec ad7746_channels[] = {
 
 /* Values are Update Rate (Hz), Conversion Time (ms) + 1*/
 static const unsigned char ad7746_vt_filter_rate_table[][2] = {
-	{50, 20 + 1}, {31, 32 + 1}, {16, 62 + 1}, {8, 122 + 1},
+	{ 50, 20 + 1 }, { 31, 32 + 1 }, { 16, 62 + 1 }, { 8, 122 + 1 },
 };
 
 static const unsigned char ad7746_cap_filter_rate_table[][2] = {
-	{91, 11 + 1}, {84, 12 + 1}, {50, 20 + 1}, {26, 38 + 1},
-	{16, 62 + 1}, {13, 77 + 1}, {11, 92 + 1}, {9, 110 + 1},
+	{ 91, 11 + 1 }, { 84, 12 + 1 }, { 50, 20 + 1 }, { 26, 38 + 1 },
+	{ 16, 62 + 1 }, { 13, 77 + 1 }, { 11, 92 + 1 }, { 9, 110 + 1 },
 };
 
 static int ad7746_set_capdac(struct ad7746_chip_info *chip, int channel)
@@ -730,6 +728,7 @@ static int ad7746_probe(struct i2c_client *client,
 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
 	if (!indio_dev)
 		return -ENOMEM;
+
 	chip = iio_priv(indio_dev);
 	mutex_init(&chip->lock);
 
@@ -780,8 +779,8 @@ static int ad7746_probe(struct i2c_client *client,
 		}
 	}
 
-	ret = i2c_smbus_write_byte_data(chip->client,
-					AD7746_REG_EXC_SETUP, regval);
+	ret = i2c_smbus_write_byte_data(chip->client, AD7746_REG_EXC_SETUP,
+					regval);
 	if (ret < 0)
 		return ret;
 
@@ -794,7 +793,6 @@ static const struct i2c_device_id ad7746_id[] = {
 	{ "ad7747", 7747 },
 	{}
 };
-
 MODULE_DEVICE_TABLE(i2c, ad7746_id);
 
 static const struct of_device_id ad7746_of_match[] = {
@@ -803,7 +801,6 @@ static const struct of_device_id ad7746_of_match[] = {
 	{ .compatible = "adi,ad7747" },
 	{ },
 };
-
 MODULE_DEVICE_TABLE(of, ad7746_of_match);
 
 static struct i2c_driver ad7746_driver = {
Signature · 1 line
2.36.1
author maintainer
#25 lore raw

PATCH v2 15/17 iio: cdc: ad7746: Add device specific ABI documentation.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

The datasheet description of offset calibration is complex, so for that on just refer the reader to the device datasheet.

---
 Documentation/ABI/testing/sysfs-bus-iio-cdc-ad7746 | 11 +++++++++++
 1 file changed, 11 insertions(+)
Documentation/ABI/testing/sysfs-bus-iio-cdc-ad7746 new file +11 -0
new file mode 100644
index 000000000000..6db81725b5d2
@@ -0,0 +1,11 @@
+What:		/sys/.../iio:deviceX/in_capacitableY_calibbias_calibration
+What:		/sys/.../iio:deviceX/in_capacitableY_calibscale_calibration
+KernelVersion:	5.19
+Contact:	[email protected]
+Description:
+		Write 1 to trigger a calibration of the calibbias or
+		calibscale. For calibscale, a fullscale capacitance should
+		be connected to the capacitance input and a
+		calibscale_calibration then started.  For calibbias see
+		the device datasheet section on "capacitive system offset
+		calibration".
Signature · 1 line
2.36.1
author maintainer
#26 lore raw

PATCH v2 16/17 iio: cdc: ad7746: Move driver out of staging.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

All known major issues with this driver resolved so time to move it out of staging.

Note this cleanup work was done using the roadtest framework.
https://lore.kernel.org/all/[email protected]/

---
 drivers/iio/cdc/Kconfig          |  10 +
 drivers/iio/cdc/Makefile         |   1 +
 drivers/iio/cdc/ad7746.c         | 818 +++++++++++++++++++++++++++++++
 drivers/staging/iio/cdc/ad7746.c | 818 -------------------------------
 4 files changed, 829 insertions(+), 818 deletions(-)
drivers/iio/cdc/Kconfig +10 -0
index 5e3319a3ff48..e0a5ce66a984 100644
@@ -14,4 +14,14 @@ config AD7150
 	  To compile this driver as a module, choose M here: the
 	  module will be called ad7150.
 
+config AD7746
+	tristate "Analog Devices AD7745, AD7746 AD7747 capacitive sensor driver"
+	depends on I2C
+	help
+	  Say yes here to build support for Analog Devices capacitive sensors.
+	  (AD7745, AD7746, AD7747) Provides direct access via sysfs.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called ad7746.
+
 endmenu
drivers/iio/cdc/Makefile +1 -0
index ee490637b032..41db756d8020 100644
@@ -4,3 +4,4 @@
 #
 
 obj-$(CONFIG_AD7150) += ad7150.o
+obj-$(CONFIG_AD7746) += ad7746.o
drivers/iio/cdc/ad7746.c new file +818 -0

818 lines not on this page · raw message

drivers/staging/iio/cdc/ad7746.c deleted +0 -818

818 lines not on this page · raw message

Signature · 1 line
2.36.1
#27 lore raw

Re: [PATCH v2 16/17] iio: cdc: ad7746: Move driver out of staging.

Quoted 9 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

All known major issues with this driver resolved so time to move it out of staging.

Note this cleanup work was done using the roadtest framework.
https://lore.kernel.org/all/[email protected]/

Quoted 6 lines
---
 drivers/iio/cdc/Kconfig          |  10 +
 drivers/iio/cdc/Makefile         |   1 +
 drivers/iio/cdc/ad7746.c         | 818 +++++++++++++++++++++++++++++++
 drivers/staging/iio/cdc/ad7746.c | 818 -------------------------------

It's a bit hard to review, perhaps you forgot to add -M -C when generating this patch?

(Also note that git am able to parse renamings, the only thing which should be in full in patches is when one deletes the file, although it may be already supported by newest versions of Git, dunno)

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

Re: [PATCH v2 16/17] iio: cdc: ad7746: Move driver out of staging.

Quoted 17 lines · On Mon, 20 Jun 2022 01:11:36 +0200 Andy Shevchenko <[email protected]>…
Quoted 9 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

All known major issues with this driver resolved so time to move it out of staging.

Note this cleanup work was done using the roadtest framework.
https://lore.kernel.org/all/[email protected]/

Quoted 6 lines
---
 drivers/iio/cdc/Kconfig          |  10 +
 drivers/iio/cdc/Makefile         |   1 +
 drivers/iio/cdc/ad7746.c         | 818 +++++++++++++++++++++++++++++++
 drivers/staging/iio/cdc/ad7746.c | 818 -------------------------------  
Quoted 2 lines

It's a bit hard to review, perhaps you forgot to add -M -C when generating this patch?

Specifically passed --no-renames because this is a direct file move - I should have stated that though.

The intent is to allow review of the full driver as being proposed for move out of staging. Including the bits that otherwise aren't visible anywhere in the driver. I only do this (and ask for it from others) for staging graduation patches.

However, I've just noticed the diff doesn't include removing the entries from drivers/staging/iio/cdc/Kconfig + drivers/staging/iio/cdc/Makefile which is weird. Will check that for v3.

Thanks,

Jonathan

Quoted 5 lines

(Also note that git am able to parse renamings, the only thing which should be in full in patches is when one deletes the file, although it may be already supported by newest versions of Git, dunno)

author maintainer
#29 lore raw

Re: [PATCH v2 16/17] iio: cdc: ad7746: Move driver out of staging.

Quoted 26 lines · On Mon, 20 Jun 2022 17:40:44 +0100 Jonathan Cameron…
Quoted 17 lines · On Mon, 20 Jun 2022 01:11:36 +0200 Andy Shevchenko <[email protected]>…
Quoted 9 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

All known major issues with this driver resolved so time to move it out of staging.

Note this cleanup work was done using the roadtest framework.
https://lore.kernel.org/all/[email protected]/

Quoted 6 lines
---
 drivers/iio/cdc/Kconfig          |  10 +
 drivers/iio/cdc/Makefile         |   1 +
 drivers/iio/cdc/ad7746.c         | 818 +++++++++++++++++++++++++++++++
 drivers/staging/iio/cdc/ad7746.c | 818 -------------------------------    
Quoted 2 lines

It's a bit hard to review, perhaps you forgot to add -M -C when generating this patch?

Specifically passed --no-renames because this is a direct file move - I should have stated that though.

Quoted 8 lines

The intent is to allow review of the full driver as being proposed for move out of staging. Including the bits that otherwise aren't visible anywhere in the driver. I only do this (and ask for it from others) for staging graduation patches.

However, I've just noticed the diff doesn't include removing the entries from drivers/staging/iio/cdc/Kconfig + drivers/staging/iio/cdc/Makefile which is weird. Will check that for v3.

Follow through from using the roadtest specific Makefile which wasn't building staging drivers. A normal build indeed through up that there was a clash due to the leftover files. Wow, I wasn't on good form whilst doing this!

Jonathan

Quoted 11 lines

Thanks,

Jonathan

Quoted 5 lines

(Also note that git am able to parse renamings, the only thing which should be in full in patches is when one deletes the file, although it may be already supported by newest versions of Git, dunno)

#30 lore raw

Re: [PATCH v2 16/17] iio: cdc: ad7746: Move driver out of staging.

In reply to Jonathan Cameron
Quoted 3 lines · Jonathan Cameron
Quoted 1 line · On Mon, 20 Jun 2022 01:11:36 +0200 Andy Shevchenko <[email protected]>…

On Sun, Jun 19, 2022 at 8:59 PM Jonathan Cameron <[email protected]> wrote:

...

Quoted 11 lines
Quoted 5 lines
Quoted 2 lines
 drivers/iio/cdc/ad7746.c         | 818 +++++++++++++++++++++++++++++++
 drivers/staging/iio/cdc/ad7746.c | 818 -------------------------------

It's a bit hard to review, perhaps you forgot to add -M -C when generating this patch?

Specifically passed --no-renames because this is a direct file move - I should have stated that though.

The intent is to allow review of the full driver as being proposed for move out of staging.

In (my) practice it only makes review much harder without any additional benefits. Git is smart to understand renaming and in the diffstat there is a special line about renaming.

Quoted 3 lines

Including the bits that otherwise aren't
visible anywhere in the driver. I only do this (and ask for it from others) for staging graduation patches.

Perhaps somebody finds that useful, but me :-)

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#31 lore raw

Re: [PATCH v2 16/17] iio: cdc: ad7746: Move driver out of staging.

Quoted 24 lines · On Mon, 20 Jun 2022 20:40:17 +0200 Andy Shevchenko <[email protected]>…
Quoted 3 lines · Jonathan Cameron
Quoted 1 line · On Mon, 20 Jun 2022 01:11:36 +0200 Andy Shevchenko <[email protected]>…

On Sun, Jun 19, 2022 at 8:59 PM Jonathan Cameron <[email protected]> wrote:

...

Quoted 11 lines
Quoted 5 lines
Quoted 2 lines
 drivers/iio/cdc/ad7746.c         | 818 +++++++++++++++++++++++++++++++
 drivers/staging/iio/cdc/ad7746.c | 818 -------------------------------  

It's a bit hard to review, perhaps you forgot to add -M -C when generating this patch?

Specifically passed --no-renames because this is a direct file move - I should have stated that though.

The intent is to allow review of the full driver as being proposed for move out of staging.

In (my) practice it only makes review much harder without any additional benefits. Git is smart to understand renaming and in the diffstat there is a special line about renaming.

Quoted 6 lines
Quoted 3 lines

Including the bits that otherwise aren't
visible anywhere in the driver. I only do this (and ask for it from others) for staging graduation patches.

Perhaps somebody finds that useful, but me :-)

I definitely do as lets me lazy and not apply the patches just do a review
+ where do you comment on something in the original driver that is getting moved? That particularly use case only really applies to drivers moving out of staging though. Alternative is to paste the entire driver in the cover letter which is not a great solution either...

Jonathan

#32 lore raw

Re: [PATCH v2 16/17] iio: cdc: ad7746: Move driver out of staging.

Quoted 7 lines · Jonathan Cameron
Quoted 5 lines · On Mon, 20 Jun 2022 20:40:17 +0200 Andy Shevchenko <[email protected]>…
Quoted 3 lines · Jonathan Cameron
Quoted 1 line · On Mon, 20 Jun 2022 01:11:36 +0200 Andy Shevchenko <[email protected]>…

On Sun, Jun 19, 2022 at 8:59 PM Jonathan Cameron <[email protected]> wrote:

...

Quoted 20 lines
Quoted 20 lines
Quoted 11 lines
Quoted 5 lines
Quoted 2 lines
 drivers/iio/cdc/ad7746.c         | 818 +++++++++++++++++++++++++++++++
 drivers/staging/iio/cdc/ad7746.c | 818 -------------------------------

It's a bit hard to review, perhaps you forgot to add -M -C when generating this patch?

Specifically passed --no-renames because this is a direct file move - I should have stated that though.

The intent is to allow review of the full driver as being proposed for move out of staging.

In (my) practice it only makes review much harder without any additional benefits. Git is smart to understand renaming and in the diffstat there is a special line about renaming.

Quoted 3 lines

Including the bits that otherwise aren't
visible anywhere in the driver. I only do this (and ask for it from others) for staging graduation patches.

Quoted 8 lines
Quoted 2 lines

Perhaps somebody finds that useful, but me :-)

I definitely do as lets me lazy and not apply the patches just do a review
+ where do you comment on something in the original driver that is getting moved? That particularly use case only really applies to drivers moving out of staging though. Alternative is to paste the entire driver in the cover letter which is not a great solution either...

Ah, I think I got your point. You want somebody to go through the driver without applying series locally and tell if it looks okay to be out of the staging, This is fair point, but perhaps I trust you that much that I believe if anything is left, it's not a blocker of moving things out of staging.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#33 lore raw

PATCH v2 17/17 RFC: iio: cdc: ad7746: Add roadtest

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

The recent work to cleanup and migrate this driver out of staging relied on using roadtest to verify that the various refactoring and fixes did not cause any regressions.

Note I am far from an experienced python coder so a large part of this exercise was to establish if roadtest was useful / usable without that particular skill set. Conclusion yes it is :)

Apologies for the code!

---
 .../roadtest/tests/iio/cdc/__init__.py        |   0
 .../roadtest/roadtest/tests/iio/cdc/config    |   1 +
 .../roadtest/tests/iio/cdc/test_ad7746.py     | 323 ++++++++++++++++++
 3 files changed, 324 insertions(+)
tools/testing/roadtest/roadtest/tests/iio/cdc/__init__.py new file +0 -0
new file mode 100644
index 000000000000..e69de29bb2d1
tools/testing/roadtest/roadtest/tests/iio/cdc/config new file +1 -0
new file mode 100644
index 000000000000..6e9397325918
@@ -0,0 +1 @@
+CONFIG_AD7746=m
tools/testing/roadtest/roadtest/tests/iio/cdc/test_ad7746.py new file +323 -0

323 lines not on this page · raw message

Signature · 1 line
2.36.1
#34 lore raw
In reply to Jonathan Cameron
Quoted 16 lines · Jonathan Cameron

Vincent: I'm getting some instability with roadtest after forwards porting to current mainline. Tests run 'sometimes'. Other times I get a crash in um_set_signal. Seems unrelated to the test this series adds.

Changes since v1: Thanks to Andy and Peter for reviews.
- Use sizeof(data) for be24 buffer data[3]
- Make the precision patch fall back to old maths if overflow would have occured.

This started out as an experiment with Vincent Whitchurch's roadtest testing framework [1] and that worked well so I carried on cleaning up the driver.

Mostly this is standard tidy up, move to new interfaces, then move the driver out of staging, but there are a few other things in here.

Quoted 11 lines

1) Precision improvement for IIO_VAL_FRACTIONAL_LOG2.
The ad7746 is a 24 bit sensor and this highlighted that 9 decimal places wasn't enough to keep the error introduced by
_raw * _scale from growing too large over the whole range (I couldn't write a sensible test for it). So I've proposed increasing the precision of the calculation used to provide the sysfs attributes with this value type and printing a few more decimal places (12), but only if overflow will not occur due to val[0] > ULLONG_MAX / PICO
2) _inputoffset ABI addition. This driver had an odd use of _offset for the case of differential channels that applied the offset to both of the differential pair (hence usespace shouldn't not apply it when

userspace?

Quoted 16 lines

converting to the base units. That isn't inline with the existing documentation for _offset and it wasn't clear to me that it made sense at all. To avoid confusion I've added this new ABI (_inputoffset) for t
3) roadtest file - note this is not a complete test set for the driver and mainly focused on the main channel reads and places I thought I might have broken things whilst working on the driver.

My conclusion on roadtest - Very useful indeed. I'd encourage others to consider developing some basic sanity tests for drivers they are working on. Hopefully my python code isn't too hideous to understand at least! Vincent, it might be worth thinking about some generic code to handle the 'variants' on correct ABI like I introduce here because I switched from a shared by type scale to an individual one per channel for the voltages. Both were ABI compliant so that sort of change is fine most of the time though we have to be careful with it.

Quoted 4 lines

All comments welcome. Note there may be changes that make more sense to do after moving this out of staging as long as there are no ABI changes involved etc. Feel free to highlight those sorts of changes as well as anything more significant.

Overall it looks good to me. One Q though, do you plan to switch it to regmap APIs?

...

Quoted 1 line

staging: iio: cdc: ad7746: Drop usused i2c_set_clientdata()

unused

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#35 lore raw
Quoted 4 lines

Overall it looks good to me. One Q though, do you plan to switch it to regmap APIs?

Probably not unless doing some more significant work on the driver for some other reason. It's definitely not yet a requirement that IIO drivers use regmap, even though it is of course desirable where it fits well. Given it should be fine to do the conversion and rely on roadtest to see if anything isn't broken, converting this one might be a good first patch for someone who is interested in writing modern drivers.

Thanks,

Jonathan

#36 lore raw
In reply to Andy Shevchenko
Quoted 30 lines · Andy Shevchenko
Quoted 16 lines · Jonathan Cameron

Vincent: I'm getting some instability with roadtest after forwards porting to current mainline. Tests run 'sometimes'. Other times I get a crash in um_set_signal. Seems unrelated to the test this series adds.

Changes since v1: Thanks to Andy and Peter for reviews.
- Use sizeof(data) for be24 buffer data[3]
- Make the precision patch fall back to old maths if overflow would have occured.

This started out as an experiment with Vincent Whitchurch's roadtest testing framework [1] and that worked well so I carried on cleaning up the driver.

Mostly this is standard tidy up, move to new interfaces, then move the driver out of staging, but there are a few other things in here.

Quoted 11 lines

1) Precision improvement for IIO_VAL_FRACTIONAL_LOG2.
The ad7746 is a 24 bit sensor and this highlighted that 9 decimal places wasn't enough to keep the error introduced by
_raw * _scale from growing too large over the whole range (I couldn't write a sensible test for it). So I've proposed increasing the precision of the calculation used to provide the sysfs attributes with this value type and printing a few more decimal places (12), but only if overflow will not occur due to val[0] > ULLONG_MAX / PICO
2) _inputoffset ABI addition. This driver had an odd use of _offset for the case of differential channels that applied the offset to both of the differential pair (hence usespace shouldn't not apply it when

Quoted 2 lines

userspace?

Also, there's a confusing double negative. Either "should" or "should not".

Thanks,
Joe

#37 lore raw
In reply to Jonathan Cameron
Quoted 3 lines · Jonathan Cameron

Vincent: I'm getting some instability with roadtest after forwards porting to current mainline. Tests run 'sometimes'. Other times I get a crash in um_set_signal. Seems unrelated to the test this series adds.

My local work-in-progress version has a lot of changes since the RFC, but yes, I'm seeing a splat too (below) if I use the RFC version on v5.19-rc3. The problem goes away if I enable CONFIG_UML_RANDOM=y, as I have done in my local branch:

tools/testing/roadtest/roadtest/tests/base/config +1 -0
index c1952d047c8e..74b201c48d42 100644
@@ -4,6 +4,7 @@ CONFIG_LOG_BUF_SHIFT=14
 CONFIG_EXPERT=y
 CONFIG_HOSTFS=y
 CONFIG_UML_TIME_TRAVEL_SUPPORT=y
+CONFIG_UML_RANDOM=y
 CONFIG_NULL_CHAN=y
 CONFIG_PORT_CHAN=y
 CONFIG_PTY_CHAN=y
 [  547.130000][   T21] BUG: failure at arch/um/kernel/time.c:415/time_travel_update_time()!
 [  547.130000][   T21] Kernel panic - not syncing: BUG!
 [  547.130000][   T21] CPU: 0 PID: 21 Comm: python3 Not tainted 5.19.0-rc3-00018-gcb2e63986f7f #77
 [  547.130000][   T21] Stack:
 [  547.130000][   T21]  605860dd 60290b07 60037130 00000000
 [  547.130000][   T21]  605860dd 00000000 607a39d0 60426253
 [  547.130000][   T21]  6064d1a8 00000000 604106ad 60557d16
 [  547.130000][   T21] Call Trace:
 [  547.130000][   T21]  [<60290b07>] ? dump_stack_print_info+0xd7/0xf0
 [  547.130000][   T21]  [<60037130>] ? um_set_signals+0x0/0x50
 [  547.130000][   T21]  [<60426253>] ? dump_stack_lvl+0x5b/0x7a
 [  547.130000][   T21]  [<604106ad>] ? _printk+0x0/0x9b
 [  547.130000][   T21]  [<60025440>] ? time_travel_del_event+0x0/0xc0
 [  547.130000][   T21]  [<6040b016>] ? panic+0x1a8/0x372
 [  547.130000][   T21]  [<6040ae6e>] ? panic+0x0/0x372
 [  547.130000][   T21]  [<604106ad>] ? _printk+0x0/0x9b
 [  547.130000][   T21]  [<60037130>] ? um_set_signals+0x0/0x50
 [  547.130000][   T21]  [<60037174>] ? um_set_signals+0x44/0x50
 [  547.130000][   T21]  [<60025440>] ? time_travel_del_event+0x0/0xc0
 [  547.130000][   T21]  [<60409c35>] ? time_travel_update_time.cold+0x6c/0x133
 [  547.130000][   T21]  [<6043b5f0>] ? __schedule+0x780/0x880
 [  547.130000][   T21]  [<6043ae70>] ? __schedule+0x0/0x880
 [  547.130000][   T21]  [<600bd220>] ? random_get_entropy_fallback+0x0/0x30
 [  547.130000][   T21]  [<6003be10>] ? get_fp_registers+0x0/0x80
 [  547.130000][   T21]  [<600258fd>] ? timer_read+0xbd/0xf0
 [  547.130000][   T21]  [<60305fb0>] ? mix_pool_bytes+0x0/0x60
 [  547.130000][   T21]  [<60305fb0>] ? mix_pool_bytes+0x0/0x60
 [  547.130000][   T21]  [<60430d32>] ? try_to_generate_entropy+0x14d/0x164
 [  547.130000][   T21]  [<60430f98>] ? entropy_timer+0x0/0x48
 [  547.130000][   T21]  [<60431049>] ? urandom_read_iter.cold+0xc/0x11
 [  547.130000][   T21]  [<60176b52>] ? new_sync_read+0xe2/0x150
 [  547.130000][   T21]  [<60178dc2>] ? vfs_read+0xf2/0x200
 [  547.130000][   T21]  [<601a51d5>] ? __fdget_pos+0x15/0x60
 [  547.130000][   T21]  [<601793a1>] ? ksys_read+0x61/0xf0
 [  547.130000][   T21]  [<600280ca>] ? handle_syscall+0xaa/0xf0
 [  547.130000][   T21]  [<60039236>] ? userspace+0x346/0x570
 [  547.130000][   T21]  [<60037130>] ? um_set_signals+0x0/0x50
author maintainer
#38 lore raw
Quoted 9 lines · On Wed, 22 Jun 2022 15:07:54 +0200 Vincent Whitchurch…
Quoted 3 lines · Jonathan Cameron

Vincent: I'm getting some instability with roadtest after forwards porting to current mainline. Tests run 'sometimes'. Other times I get a crash in um_set_signal. Seems unrelated to the test this series adds.

My local work-in-progress version has a lot of changes since the RFC, but yes, I'm seeing a splat too (below) if I use the RFC version on v5.19-rc3. The problem goes away if I enable CONFIG_UML_RANDOM=y, as I have done in my local branch:

Thanks, that's indeed effective though I guess papering over a real problem.

Works for me though :)

Jonathan

Quoted 52 lines
tools/testing/roadtest/roadtest/tests/base/config +1 -0
index c1952d047c8e..74b201c48d42 100644
@@ -4,6 +4,7 @@ CONFIG_LOG_BUF_SHIFT=14
 CONFIG_EXPERT=y
 CONFIG_HOSTFS=y
 CONFIG_UML_TIME_TRAVEL_SUPPORT=y
+CONFIG_UML_RANDOM=y
 CONFIG_NULL_CHAN=y
 CONFIG_PORT_CHAN=y
 CONFIG_PTY_CHAN=y
 [  547.130000][   T21] BUG: failure at arch/um/kernel/time.c:415/time_travel_update_time()!
 [  547.130000][   T21] Kernel panic - not syncing: BUG!
 [  547.130000][   T21] CPU: 0 PID: 21 Comm: python3 Not tainted 5.19.0-rc3-00018-gcb2e63986f7f #77
 [  547.130000][   T21] Stack:
 [  547.130000][   T21]  605860dd 60290b07 60037130 00000000
 [  547.130000][   T21]  605860dd 00000000 607a39d0 60426253
 [  547.130000][   T21]  6064d1a8 00000000 604106ad 60557d16
 [  547.130000][   T21] Call Trace:
 [  547.130000][   T21]  [<60290b07>] ? dump_stack_print_info+0xd7/0xf0
 [  547.130000][   T21]  [<60037130>] ? um_set_signals+0x0/0x50
 [  547.130000][   T21]  [<60426253>] ? dump_stack_lvl+0x5b/0x7a
 [  547.130000][   T21]  [<604106ad>] ? _printk+0x0/0x9b
 [  547.130000][   T21]  [<60025440>] ? time_travel_del_event+0x0/0xc0
 [  547.130000][   T21]  [<6040b016>] ? panic+0x1a8/0x372
 [  547.130000][   T21]  [<6040ae6e>] ? panic+0x0/0x372
 [  547.130000][   T21]  [<604106ad>] ? _printk+0x0/0x9b
 [  547.130000][   T21]  [<60037130>] ? um_set_signals+0x0/0x50
 [  547.130000][   T21]  [<60037174>] ? um_set_signals+0x44/0x50
 [  547.130000][   T21]  [<60025440>] ? time_travel_del_event+0x0/0xc0
 [  547.130000][   T21]  [<60409c35>] ? time_travel_update_time.cold+0x6c/0x133
 [  547.130000][   T21]  [<6043b5f0>] ? __schedule+0x780/0x880
 [  547.130000][   T21]  [<6043ae70>] ? __schedule+0x0/0x880
 [  547.130000][   T21]  [<600bd220>] ? random_get_entropy_fallback+0x0/0x30
 [  547.130000][   T21]  [<6003be10>] ? get_fp_registers+0x0/0x80
 [  547.130000][   T21]  [<600258fd>] ? timer_read+0xbd/0xf0
 [  547.130000][   T21]  [<60305fb0>] ? mix_pool_bytes+0x0/0x60
 [  547.130000][   T21]  [<60305fb0>] ? mix_pool_bytes+0x0/0x60
 [  547.130000][   T21]  [<60430d32>] ? try_to_generate_entropy+0x14d/0x164
 [  547.130000][   T21]  [<60430f98>] ? entropy_timer+0x0/0x48
 [  547.130000][   T21]  [<60431049>] ? urandom_read_iter.cold+0xc/0x11
 [  547.130000][   T21]  [<60176b52>] ? new_sync_read+0xe2/0x150
 [  547.130000][   T21]  [<60178dc2>] ? vfs_read+0xf2/0x200
 [  547.130000][   T21]  [<601a51d5>] ? __fdget_pos+0x15/0x60
 [  547.130000][   T21]  [<601793a1>] ? ksys_read+0x61/0xf0
 [  547.130000][   T21]  [<600280ca>] ? handle_syscall+0xaa/0xf0
 [  547.130000][   T21]  [<60039236>] ? userspace+0x346/0x570
 [  547.130000][   T21]  [<60037130>] ? um_set_signals+0x0/0x50