Skip to contentSkip to conversation

Bring software triggers support to MAX1027-like ADCs

PATCH Applied Miquel Raynal 3 participants 71 messages Aug 18, 2021 – Sep 6, 2021

Subsystems (2)

v1 of 4 Applied Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover Bring software triggers support to MAX1027-like ADCs

Until now the max1027.c driver, which handles 10-bit devices (max10xx) and 12-bit devices (max12xx), only supported hardware triggers. When a hardware trigger is not wired it is very convenient to trigger periodic conversions with timers or on userspace demand with a sysfs trigger. Overall, when several values are needed at the same time using triggers and buffers improves quite a lot the performances.

This series starts with two small fixes, then does a bit of cleaning/code reorganization before actually adding support for software triggers.

This series has been developed and tested on a custom board with a 4.14 kernel. I then rebased the series on top of a mainline kernel (v5.14-rc1) but unfortunately after quite some time debugging it I was unable to get all the necessary blocks running in order to properly test it. Anyway, there was very little changes in that series when rebasing it from v4.14 to v5.14-rc1 so I am pretty confident it will smoothly work with a more recent kernel.

How to test sysfs triggers:
echo 0 > /sys/bus/iio/devices/iio_sysfs_trigger/add_trigger cat /sys/bus/iio/devices/iio_sysfs_trigger/trigger0/name > \
/sys/bus/iio/devices/iio:device0/trigger/current_trigger
echo 1 > /sys/bus/iio/devices/iio:device0/scan_elements/in_voltageX_en echo 1 > /sys/bus/iio/devices/iio:device0/scan_elements/in_voltageY_en echo 1 > /sys/bus/iio/devices/iio:device0/buffer/enable cat /dev/iio\:device0 > /tmp/data &
echo 1 > /sys/bus/iio/devices/trigger0/trigger_now
od -t x1 /tmp/data

Cheers,
Miquèl

Miquel Raynal (16):
iio: adc: max1027: Fix wrong shift with 12-bit devices
iio: adc: max1027: Fix the number of max1X31 channels
iio: adc: max1027: Push only the requested samples
iio: adc: max1027: Lower conversion time
iio: adc: max1027: Drop extra warning message
iio: adc: max1027: Rename a helper
iio: adc: max1027: Create a helper to configure the trigger
iio: adc: max1027: Explain better how the trigger state gets changed
iio: adc: max1027: Create a helper to configure the channels to scan
iio: adc: max1027: Prevent single channel accesses during buffer reads
iio: adc: max1027: Separate the IRQ handler from the read logic
iio: adc: max1027: Introduce an end of conversion helper
iio: adc: max1027: Prepare re-using the EOC interrupt
iio: adc: max1027: Consolidate the end of conversion helper
iio: adc: max1027: Support software triggers
iio: adc: max1027: Enable software triggers to be used without IRQ

 drivers/iio/adc/max1027.c | 236 ++++++++++++++++++++++++++++----------
 1 file changed, 177 insertions(+), 59 deletions(-)
Signature · 1 line
2.27.0
#1 lore raw

PATCH 1/16 iio: adc: max1027: Fix wrong shift with 12-bit devices

10-bit devices must shift the value twice.
This is not needed anymore on 12-bit devices.

Fixes ae47d009b508 ("iio: adc: max1027: Introduce 12-bit devices support")
---
 drivers/iio/adc/max1027.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/max1027.c +1 -1
index 655ab02d03d8..4a42d140a4b0 100644
@@ -103,7 +103,7 @@ MODULE_DEVICE_TABLE(of, max1027_adc_dt_ids);
 			.sign = 'u',					\
 			.realbits = depth,				\
 			.storagebits = 16,				\
-			.shift = 2,					\
+			.shift = (depth == 10) ? 2 : 0,			\
 			.endianness = IIO_BE,				\
 		},							\
 	}
Signature · 1 line
2.27.0
reviewer Reviewed-by on v1 1/16
#2 lore raw

RE: [PATCH 01/16] iio: adc: max1027: Fix wrong shift with 12-bit devices

Quoted 39 lines

-----Original Message-----
From: Miquel Raynal <[email protected]>
Sent: Wednesday, August 18, 2021 1:11 PM
To: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>
Cc: Thomas Petazzoni <[email protected]>; linux- [email protected]; [email protected]; Miquel Raynal <[email protected]>; [email protected]
Subject: [PATCH 01/16] iio: adc: max1027: Fix wrong shift with 12-bit devices

[External]

10-bit devices must shift the value twice.
This is not needed anymore on 12-bit devices.

Fixes ae47d009b508 ("iio: adc: max1027: Introduce 12-bit devices

support")

---
 drivers/iio/adc/max1027.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/max1027.c +1 -1
index 655ab02d03d8..4a42d140a4b0 100644
@@ -103,7 +103,7 @@ MODULE_DEVICE_TABLE(of,
 max1027_adc_dt_ids);
 			.sign = 'u',					\
 			.realbits = depth,				\
 			.storagebits = 16,				\
-			.shift = 2,					\
+			.shift = (depth == 10) ? 2 : 0,			\
 			.endianness = IIO_BE,				\
 		},							\
Quoted 3 lines

}
--
2.27.0

maintainer Applied
#3 lore raw

Re: [PATCH 01/16] iio: adc: max1027: Fix wrong shift with 12-bit devices

Quoted 34 lines · Nuno
Quoted 34 lines

-----Original Message-----
From: Miquel Raynal <[email protected]>
Sent: Wednesday, August 18, 2021 1:11 PM
To: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>
Cc: Thomas Petazzoni <[email protected]>; linux- [email protected]; [email protected]; Miquel Raynal <[email protected]>; [email protected]
Subject: [PATCH 01/16] iio: adc: max1027: Fix wrong shift with 12-bit devices

[External]

10-bit devices must shift the value twice.
This is not needed anymore on 12-bit devices.

Fixes ae47d009b508 ("iio: adc: max1027: Introduce 12-bit devices

support")

---
 drivers/iio/adc/max1027.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/max1027.c +0 -1
index 655ab02d03d8..4a42d140a4b0 100644
@@ -103,7 +103,7 @@ MODULE_DEVICE_TABLE(of,
 max1027_adc_dt_ids);
 			.sign = 'u',					\
 			.realbits = depth,				\
 			.storagebits = 16,				\
-			.shift = 2,					\
Quoted 8 lines
Quoted 6 lines
drivers/iio/adc/max1027.c +1 -0
+			.shift = (depth == 10) ? 2 : 0,			\
 			.endianness = IIO_BE,				\
 		},							\

}
--
2.27.0

Ouch. I briefly wondered if we should dot his as 12 - depth, but given we are unlikely to ever see a 9 or 11 bit device and it doesn't make much sense for anything 8 or less what you have here is effectively the same.

Applied to the fixes-togreg branch of iio.git

Jonathan

Quoted 1 line
#4 lore raw

PATCH 2/16 iio: adc: max1027: Fix the number of max1X31 channels

In reply to Miquel Raynal

The macro MAX1X29_CHANNELS() already calls MAX1X27_CHANNELS(). Calling MAX1X27_CHANNELS() before MAX1X29_CHANNELS() in the definition of MAX1X31_CHANNELS() declares the first 8 channels twice. So drop this extra call from the MAX1X31 channels list definition.

Fixes 7af5257d8427 ("iio: adc: max1027: Prepare the introduction of different resolutions")
---
 drivers/iio/adc/max1027.c | 1 -
 1 file changed, 1 deletion(-)
drivers/iio/adc/max1027.c +0 -1
index 4a42d140a4b0..b753658bb41e 100644
@@ -142,7 +142,6 @@ MODULE_DEVICE_TABLE(of, max1027_adc_dt_ids);
 	MAX1027_V_CHAN(11, depth)
 
 #define MAX1X31_CHANNELS(depth)			\
-	MAX1X27_CHANNELS(depth),		\
 	MAX1X29_CHANNELS(depth),		\
 	MAX1027_V_CHAN(12, depth),		\
 	MAX1027_V_CHAN(13, depth),		\
Signature · 1 line
2.27.0
reviewer Reviewed-by on v1 2/16
#5 lore raw

RE: [PATCH 02/16] iio: adc: max1027: Fix the number of max1X31 channels

Quoted 43 lines

-----Original Message-----
From: Miquel Raynal <[email protected]>
Sent: Wednesday, August 18, 2021 1:11 PM
To: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>
Cc: Thomas Petazzoni <[email protected]>; linux- [email protected]; [email protected]; Miquel Raynal <[email protected]>; [email protected]
Subject: [PATCH 02/16] iio: adc: max1027: Fix the number of max1X31 channels

[External]

The macro MAX1X29_CHANNELS() already calls MAX1X27_CHANNELS().
Calling MAX1X27_CHANNELS() before MAX1X29_CHANNELS() in the definition
of MAX1X31_CHANNELS() declares the first 8 channels twice. So drop this
extra call from the MAX1X31 channels list definition.

Fixes 7af5257d8427 ("iio: adc: max1027: Prepare the introduction of

different resolutions")

---
 drivers/iio/adc/max1027.c | 1 -
 1 file changed, 1 deletion(-)
drivers/iio/adc/max1027.c +0 -1
index 4a42d140a4b0..b753658bb41e 100644
@@ -142,7 +142,6 @@ MODULE_DEVICE_TABLE(of,
 max1027_adc_dt_ids);
 	MAX1027_V_CHAN(11, depth)
 
 #define MAX1X31_CHANNELS(depth)			\
-	MAX1X27_CHANNELS(depth),		\
 	MAX1X29_CHANNELS(depth),		\
 	MAX1027_V_CHAN(12, depth),		\
Quoted 3 lines

MAX1027_V_CHAN(13, depth), \
--
2.27.0

maintainer Applied
#6 lore raw

Re: [PATCH 02/16] iio: adc: max1027: Fix the number of max1X31 channels

Quoted 37 lines · Nuno
Quoted 37 lines

-----Original Message-----
From: Miquel Raynal <[email protected]>
Sent: Wednesday, August 18, 2021 1:11 PM
To: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>
Cc: Thomas Petazzoni <[email protected]>; linux- [email protected]; [email protected]; Miquel Raynal <[email protected]>; [email protected]
Subject: [PATCH 02/16] iio: adc: max1027: Fix the number of max1X31 channels

[External]

The macro MAX1X29_CHANNELS() already calls MAX1X27_CHANNELS().
Calling MAX1X27_CHANNELS() before MAX1X29_CHANNELS() in the definition
of MAX1X31_CHANNELS() declares the first 8 channels twice. So drop this
extra call from the MAX1X31 channels list definition.

Fixes 7af5257d8427 ("iio: adc: max1027: Prepare the introduction of

different resolutions")

---
 drivers/iio/adc/max1027.c | 1 -
 1 file changed, 1 deletion(-)
drivers/iio/adc/max1027.c +0 -0
index 4a42d140a4b0..b753658bb41e 100644
@@ -142,7 +142,6 @@ MODULE_DEVICE_TABLE(of,
 max1027_adc_dt_ids);
 	MAX1027_V_CHAN(11, depth)
 
Quoted 10 lines
Quoted 7 lines
drivers/iio/adc/max1027.c +0 -1
 #define MAX1X31_CHANNELS(depth)			\
-	MAX1X27_CHANNELS(depth),		\
 	MAX1X29_CHANNELS(depth),		\
 	MAX1027_V_CHAN(12, depth),		\

MAX1027_V_CHAN(13, depth), \
--
2.27.0

I guess we don't have many users of these devices as I would have expected this to blow up spectacularly. Ah well.

Applied to the fixes-togreg branch of iio.git

Thanks,

Jonathan

#7 lore raw

PATCH 3/16 iio: adc: max1027: Push only the requested samples

In reply to Miquel Raynal

When a triggered scan occurs, the identity of the desired channels is known in indio_dev->active_scan_mask. Instead of reading and pushing to the IIO buffers all channels each time, scan the minimum amount of channels (0 to maximum requested chan, to be exact) and only provide the samples requested by the user.

For example, if the user wants channels 1, 4 and 5, all channels from 0 to 5 will be scanned but only the desired channels will be pushed to the IIO buffers.

---
 drivers/iio/adc/max1027.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
drivers/iio/adc/max1027.c +21 -4
index b753658bb41e..8ab660f596b5 100644
@@ -360,6 +360,9 @@ static int max1027_set_trigger_state(struct iio_trigger *trig, bool state)
 	struct max1027_state *st = iio_priv(indio_dev);
 	int ret;
 
+	if (bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength))
+		return -EINVAL;
+
 	if (state) {
 		/* Start acquisition on cnvst */
 		st->reg = MAX1027_SETUP_REG | MAX1027_CKS_MODE0 |
@@ -368,9 +371,12 @@ static int max1027_set_trigger_state(struct iio_trigger *trig, bool state)
 		if (ret < 0)
 			return ret;
 
-		/* Scan from 0 to max */
-		st->reg = MAX1027_CONV_REG | MAX1027_CHAN(0) |
-			  MAX1027_SCAN_N_M | MAX1027_TEMP;
+		/*
+		 * Scan from 0 to the highest requested channel. The temperature
+		 * could be avoided but it simplifies a bit the logic.
+		 */
+		st->reg = MAX1027_CONV_REG | MAX1027_SCAN_0_N | MAX1027_TEMP;
+		st->reg |= MAX1027_CHAN(fls(*indio_dev->active_scan_mask) - 2);
 		ret = spi_write(st->spi, &st->reg, 1);
 		if (ret < 0)
 			return ret;
@@ -391,11 +397,22 @@ static irqreturn_t max1027_trigger_handler(int irq, void *private)
 	struct iio_poll_func *pf = private;
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct max1027_state *st = iio_priv(indio_dev);
+	unsigned int scanned_chans = fls(*indio_dev->active_scan_mask);
+	u16 *buf = st->buffer;
+	unsigned int bit;
 
 	pr_debug("%s(irq=%d, private=0x%p)\n", __func__, irq, private);
 
 	/* fill buffer with all channel */
-	spi_read(st->spi, st->buffer, indio_dev->masklength * 2);
+	spi_read(st->spi, st->buffer, scanned_chans * 2);
+
+	/* Only keep the channels selected by the user */
+	for_each_set_bit(bit, indio_dev->active_scan_mask,
+			 indio_dev->masklength) {
+		if (buf[0] != st->buffer[bit])
+			buf[0] = st->buffer[bit];
+		buf++;
+	}
 
 	iio_push_to_buffers(indio_dev, st->buffer);
 
Signature · 1 line
2.27.0
reviewer
#8 lore raw

RE: [PATCH 03/16] iio: adc: max1027: Push only the requested samples

Quoted 37 lines

-----Original Message-----
From: Miquel Raynal <[email protected]>
Sent: Wednesday, August 18, 2021 1:11 PM
To: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>
Cc: Thomas Petazzoni <[email protected]>; linux- [email protected]; [email protected]; Miquel Raynal <[email protected]>
Subject: [PATCH 03/16] iio: adc: max1027: Push only the requested samples

[External]

When a triggered scan occurs, the identity of the desired channels is known in indio_dev->active_scan_mask. Instead of reading and pushing to
the IIO buffers all channels each time, scan the minimum amount of channels (0 to maximum requested chan, to be exact) and only provide the
samples requested by the user.

For example, if the user wants channels 1, 4 and 5, all channels from 0 to 5 will be scanned but only the desired channels will be pushed to the IIO buffers.

---
 drivers/iio/adc/max1027.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
drivers/iio/adc/max1027.c +0 -0
index b753658bb41e..8ab660f596b5 100644

drivers/iio/adc/max1027.c static int max1027_set_trigger_state(struct · line 367

Quoted 8 lines
drivers/iio/adc/max1027.c +3 -0
@@ -360,6 +360,9 @@ static int max1027_set_trigger_state(struct
 iio_trigger *trig, bool state)
 	struct max1027_state *st = iio_priv(indio_dev);
 	int ret;
 
+	if (bitmap_empty(indio_dev->active_scan_mask, indio_dev-
 >masklength))
+		return -EINVAL;
+

I'm not sure this can actually happen. If you try to enable the buffer with no scan element, it should give you an error before you reach this point...

Quoted 25 lines
drivers/iio/adc/max1027.c +0 -0
 	if (state) {
drivers/iio/adc/max1027.c +0 -0
 		/* Start acquisition on cnvst */
 		st->reg = MAX1027_SETUP_REG |

MAX1027_CKS_MODE0 |

drivers/iio/adc/max1027.c +2 -3
@@ -368,9 +371,12 @@ static int max1027_set_trigger_state(struct
 iio_trigger *trig, bool state)
 		if (ret < 0)
 			return ret;
 
-		/* Scan from 0 to max */
-		st->reg = MAX1027_CONV_REG | MAX1027_CHAN(0) |
-			  MAX1027_SCAN_N_M | MAX1027_TEMP;
+		/*
+		 * Scan from 0 to the highest requested channel. The

temperature

drivers/iio/adc/max1027.c +3 -0
+		 * could be avoided but it simplifies a bit the logic.
+		 */
+		st->reg = MAX1027_CONV_REG |

MAX1027_SCAN_0_N | MAX1027_TEMP;

drivers/iio/adc/max1027.c +1 -0
+		st->reg |= MAX1027_CHAN(fls(*indio_dev-
Quoted 1 line

active_scan_mask) - 2);

drivers/iio/adc/max1027.c +0 -0
 		ret = spi_write(st->spi, &st->reg, 1);
 		if (ret < 0)
 			return ret;

drivers/iio/adc/max1027.c static irqreturn_t · line 403

Quoted 7 lines
drivers/iio/adc/max1027.c +2 -0
@@ -391,11 +397,22 @@ static irqreturn_t
 max1027_trigger_handler(int irq, void *private)
 	struct iio_poll_func *pf = private;
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct max1027_state *st = iio_priv(indio_dev);
+	unsigned int scanned_chans = fls(*indio_dev-
 >active_scan_mask);
+	u16 *buf = st->buffer;

I think sparse will complain here. buffer is a __be16 restricted type so you should not mix those...

Quoted 4 lines
drivers/iio/adc/max1027.c +1 -0
+	unsigned int bit;
 
 	pr_debug("%s(irq=%d, private=0x%p)\n", __func__, irq,
 private);

drivers/iio/adc/max1027.c static irqreturn_t max1027_trigger_handler(int irq, void *private) · line 413

Quoted 10 lines
drivers/iio/adc/max1027.c +7 -1
 
 	/* fill buffer with all channel */
-	spi_read(st->spi, st->buffer, indio_dev->masklength * 2);
+	spi_read(st->spi, st->buffer, scanned_chans * 2);
+
+	/* Only keep the channels selected by the user */
+	for_each_set_bit(bit, indio_dev->active_scan_mask,
+			 indio_dev->masklength) {
+		if (buf[0] != st->buffer[bit])
+			buf[0] = st->buffer[bit];

Since we are here, when looking into the driver, I realized that st->buffer is not DMA safe. In IIO, we kind of want to enforce that all buffers that are passed to spi/i2c buses are safe... Maybe this is something you can include in your series.

- Nuno Sá

Quoted 7 lines
drivers/iio/adc/max1027.c +2 -0
+		buf++;
+	}

iio_push_to_buffers(indio_dev, st->buffer);

--
2.27.0

maintainer
#9 lore raw

Re: [PATCH 03/16] iio: adc: max1027: Push only the requested samples

Quoted 88 lines · Nuno
Quoted 35 lines

-----Original Message-----
From: Miquel Raynal <[email protected]>
Sent: Wednesday, August 18, 2021 1:11 PM
To: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>
Cc: Thomas Petazzoni <[email protected]>; linux- [email protected]; [email protected]; Miquel Raynal <[email protected]>
Subject: [PATCH 03/16] iio: adc: max1027: Push only the requested samples

[External]

When a triggered scan occurs, the identity of the desired channels is known in indio_dev->active_scan_mask. Instead of reading and pushing to
the IIO buffers all channels each time, scan the minimum amount of channels (0 to maximum requested chan, to be exact) and only provide the
samples requested by the user.

For example, if the user wants channels 1, 4 and 5, all channels from 0 to 5 will be scanned but only the desired channels will be pushed to the IIO buffers.

---
 drivers/iio/adc/max1027.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
drivers/iio/adc/max1027.c +0 -0
index b753658bb41e..8ab660f596b5 100644

drivers/iio/adc/max1027.c static int max1027_set_trigger_state(struct · line 367

Quoted 8 lines
drivers/iio/adc/max1027.c +3 -0
@@ -360,6 +360,9 @@ static int max1027_set_trigger_state(struct
 iio_trigger *trig, bool state)
 	struct max1027_state *st = iio_priv(indio_dev);
 	int ret;
 
+	if (bitmap_empty(indio_dev->active_scan_mask, indio_dev-  
 >masklength))  
+		return -EINVAL;
+  

I'm not sure this can actually happen. If you try to enable the buffer with no scan element, it should give you an error before you reach this point...

Quoted 25 lines
drivers/iio/adc/max1027.c +0 -0
 	if (state) {
drivers/iio/adc/max1027.c +0 -0
 		/* Start acquisition on cnvst */
 		st->reg = MAX1027_SETUP_REG |

MAX1027_CKS_MODE0 |

drivers/iio/adc/max1027.c +2 -3
@@ -368,9 +371,12 @@ static int max1027_set_trigger_state(struct
 iio_trigger *trig, bool state)
 		if (ret < 0)
 			return ret;
 
-		/* Scan from 0 to max */
-		st->reg = MAX1027_CONV_REG | MAX1027_CHAN(0) |
-			  MAX1027_SCAN_N_M | MAX1027_TEMP;
+		/*
+		 * Scan from 0 to the highest requested channel. The

temperature

drivers/iio/adc/max1027.c +3 -0
+		 * could be avoided but it simplifies a bit the logic.
+		 */
+		st->reg = MAX1027_CONV_REG |

MAX1027_SCAN_0_N | MAX1027_TEMP;

drivers/iio/adc/max1027.c +1 -0
+		st->reg |= MAX1027_CHAN(fls(*indio_dev-  
Quoted 1 line

active_scan_mask) - 2);

drivers/iio/adc/max1027.c +0 -0
 		ret = spi_write(st->spi, &st->reg, 1);
 		if (ret < 0)
 			return ret;

drivers/iio/adc/max1027.c static irqreturn_t · line 403

Quoted 7 lines
drivers/iio/adc/max1027.c +2 -0
@@ -391,11 +397,22 @@ static irqreturn_t
 max1027_trigger_handler(int irq, void *private)
 	struct iio_poll_func *pf = private;
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct max1027_state *st = iio_priv(indio_dev);
+	unsigned int scanned_chans = fls(*indio_dev-  
 >active_scan_mask);  
+	u16 *buf = st->buffer;  

I think sparse will complain here. buffer is a __be16 restricted type so you should not mix those...

Quoted 5 lines
drivers/iio/adc/max1027.c +1 -0
+	unsigned int bit;
 
 	pr_debug("%s(irq=%d, private=0x%p)\n", __func__, irq,

private);in/20210818_miquel_raynal_bring_software_triggers_support_to_max1027_like_adcs.mbx

Quoted 14 lines

drivers/iio/adc/max1027.c static irqreturn_t max1027_trigger_handler(int irq, void *private) · line 413

Quoted 9 lines
drivers/iio/adc/max1027.c +7 -1
 	/* fill buffer with all channel */
-	spi_read(st->spi, st->buffer, indio_dev->masklength * 2);
+	spi_read(st->spi, st->buffer, scanned_chans * 2);
+
+	/* Only keep the channels selected by the user */
+	for_each_set_bit(bit, indio_dev->active_scan_mask,
+			 indio_dev->masklength) {
+		if (buf[0] != st->buffer[bit])
+			buf[0] = st->buffer[bit];  

Since we are here, when looking into the driver, I realized that st->buffer is not DMA safe. In IIO, we kind of want to enforce that all buffers that are passed to spi/i2c buses are safe... Maybe this is something you can include in your series.

Why is it not? st->buffer is result of a devm_kmalloc_array() call and that should provide a DMA safe buffer as I understand it.

Quoted 11 lines

- Nuno Sá

Quoted 7 lines
drivers/iio/adc/max1027.c +2 -1
+		buf++;
+	}
 
 	iio_push_to_buffers(indio_dev, st->buffer);
 
--

2.27.0

reviewer
#10 lore raw

RE: [PATCH 03/16] iio: adc: max1027: Push only the requested samples

Quoted 127 lines

-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Monday, August 30, 2021 12:08 PM
To: Sa, Nuno <[email protected]>
Cc: Miquel Raynal <[email protected]>; Lars-Peter Clausen <[email protected]>; Thomas Petazzoni <[email protected]>; [email protected]; linux- [email protected]
Subject: Re: [PATCH 03/16] iio: adc: max1027: Push only the requested samples

[External]

Quoted 11 lines · Nuno
Quoted 11 lines

-----Original Message-----
From: Miquel Raynal <[email protected]>
Sent: Wednesday, August 18, 2021 1:11 PM
To: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>
Cc: Thomas Petazzoni <[email protected]>; linux- [email protected]; [email protected]; Miquel Raynal <[email protected]>
Subject: [PATCH 03/16] iio: adc: max1027: Push only the requested samples

Quoted 3 lines
Quoted 3 lines

[External]

When a triggered scan occurs, the identity of the desired channels

is

Quoted 3 lines
Quoted 3 lines

known in indio_dev->active_scan_mask. Instead of reading and pushing to
the IIO buffers all channels each time, scan the minimum amount

of

Quoted 5 lines
Quoted 5 lines

channels (0 to maximum requested chan, to be exact) and only provide the
samples requested by the user.

For example, if the user wants channels 1, 4 and 5, all channels

from

Quoted 1 line
Quoted 1 line

0 to 5 will be scanned but only the desired channels will be pushed

to

Quoted 56 lines
Quoted 12 lines

the IIO buffers.

---
 drivers/iio/adc/max1027.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
drivers/iio/adc/max1027.c +0 -0
index b753658bb41e..8ab660f596b5 100644

drivers/iio/adc/max1027.c static int max1027_set_trigger_state(struct · line 367

Quoted 8 lines
drivers/iio/adc/max1027.c +3 -0
@@ -360,6 +360,9 @@ static int max1027_set_trigger_state(struct
 iio_trigger *trig, bool state)
 	struct max1027_state *st = iio_priv(indio_dev);
 	int ret;
 
+	if (bitmap_empty(indio_dev->active_scan_mask, indio_dev-
 >masklength))
+		return -EINVAL;
+

I'm not sure this can actually happen. If you try to enable the buffer with no scan element, it should give you an error before you reach this point...

Quoted 31 lines
drivers/iio/adc/max1027.c +0 -0
 	if (state) {
drivers/iio/adc/max1027.c +0 -0
 		/* Start acquisition on cnvst */
 		st->reg = MAX1027_SETUP_REG |

MAX1027_CKS_MODE0 |

drivers/iio/adc/max1027.c +2 -3
@@ -368,9 +371,12 @@ static int max1027_set_trigger_state(struct
 iio_trigger *trig, bool state)
 		if (ret < 0)
 			return ret;
 
-		/* Scan from 0 to max */
-		st->reg = MAX1027_CONV_REG | MAX1027_CHAN(0) |
-			  MAX1027_SCAN_N_M | MAX1027_TEMP;
+		/*
+		 * Scan from 0 to the highest requested channel. The

temperature

drivers/iio/adc/max1027.c +3 -0
+		 * could be avoided but it simplifies a bit the logic.
+		 */
+		st->reg = MAX1027_CONV_REG |

MAX1027_SCAN_0_N | MAX1027_TEMP;

drivers/iio/adc/max1027.c +1 -0
+		st->reg |= MAX1027_CHAN(fls(*indio_dev-
Quoted 1 line

active_scan_mask) - 2);

drivers/iio/adc/max1027.c +1 -0
 		ret = spi_write(st->spi, &st->reg, 1);
 		if (ret < 0)
 			return ret;
@@ -391,11 +397,22 @@ static irqreturn_t
 max1027_trigger_handler(int irq, void *private)
 	struct iio_poll_func *pf = private;
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct max1027_state *st = iio_priv(indio_dev);
+	unsigned int scanned_chans = fls(*indio_dev-
 >active_scan_mask);
Quoted 8 lines

drivers/iio/adc/max1027.c static irqreturn_t max1027_trigger_handler(int irq, void *private) · line 401

Quoted 1 line
drivers/iio/adc/max1027.c +1 -0
+	u16 *buf = st->buffer;

I think sparse will complain here. buffer is a __be16 restricted type so you should not mix those...

drivers/iio/adc/max1027.c static irqreturn_t max1027_trigger_handler(int irq, void *private) · line 405

Quoted 4 lines
drivers/iio/adc/max1027.c +1 -0
+	unsigned int bit;
 
 	pr_debug("%s(irq=%d, private=0x%p)\n", __func__, irq,
 

private);in/20210818_miquel_raynal_bring_software_triggers_support _to_max1027_like_adcs.mbx

Quoted 15 lines

drivers/iio/adc/max1027.c static irqreturn_t max1027_trigger_handler(int irq, void *private) · line 413

Quoted 10 lines
drivers/iio/adc/max1027.c +7 -1
 	/* fill buffer with all channel */
-	spi_read(st->spi, st->buffer, indio_dev->masklength * 2);
+	spi_read(st->spi, st->buffer, scanned_chans * 2);
+
+	/* Only keep the channels selected by the user */
+	for_each_set_bit(bit, indio_dev->active_scan_mask,
+			 indio_dev->masklength) {
+		if (buf[0] != st->buffer[bit])
+			buf[0] = st->buffer[bit];

Since we are here, when looking into the driver, I realized that st->buffer is not DMA safe. In IIO, we kind of want to enforce that all buffers that are passed to spi/i2c buses are safe... Maybe this is something you can include in your series.

Quoted 3 lines

Why is it not? st->buffer is result of a devm_kmalloc_array() call and that should provide a DMA safe buffer as I understand it.

That's a good question. I'm not sure how I came to that conclusion which is clearly wrong. Though I think the buffer might share the line with the mutex...

- Nuno Sá

maintainer
#11 lore raw

Re: [PATCH 03/16] iio: adc: max1027: Push only the requested samples

Quoted 125 lines · Nuno
Quoted 125 lines

-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Monday, August 30, 2021 12:08 PM
To: Sa, Nuno <[email protected]>
Cc: Miquel Raynal <[email protected]>; Lars-Peter Clausen <[email protected]>; Thomas Petazzoni <[email protected]>; [email protected]; linux- [email protected]
Subject: Re: [PATCH 03/16] iio: adc: max1027: Push only the requested samples

[External]

Quoted 11 lines · Nuno
Quoted 11 lines

-----Original Message-----
From: Miquel Raynal <[email protected]>
Sent: Wednesday, August 18, 2021 1:11 PM
To: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>
Cc: Thomas Petazzoni <[email protected]>; linux- [email protected]; [email protected]; Miquel Raynal <[email protected]>
Subject: [PATCH 03/16] iio: adc: max1027: Push only the requested samples

Quoted 3 lines
Quoted 3 lines

[External]

When a triggered scan occurs, the identity of the desired channels

is

Quoted 3 lines
Quoted 3 lines

known in indio_dev->active_scan_mask. Instead of reading and pushing to
the IIO buffers all channels each time, scan the minimum amount

of

Quoted 5 lines
Quoted 5 lines

channels (0 to maximum requested chan, to be exact) and only provide the
samples requested by the user.

For example, if the user wants channels 1, 4 and 5, all channels

from

Quoted 1 line
Quoted 1 line

0 to 5 will be scanned but only the desired channels will be pushed

to

Quoted 56 lines
Quoted 12 lines

the IIO buffers.

---
 drivers/iio/adc/max1027.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
drivers/iio/adc/max1027.c +0 -0
index b753658bb41e..8ab660f596b5 100644

drivers/iio/adc/max1027.c static int max1027_set_trigger_state(struct · line 367

Quoted 8 lines
drivers/iio/adc/max1027.c +3 -0
@@ -360,6 +360,9 @@ static int max1027_set_trigger_state(struct
 iio_trigger *trig, bool state)
 	struct max1027_state *st = iio_priv(indio_dev);
 	int ret;
 
+	if (bitmap_empty(indio_dev->active_scan_mask, indio_dev-  
 >masklength))  
+		return -EINVAL;
+  

I'm not sure this can actually happen. If you try to enable the buffer with no scan element, it should give you an error before you reach this point...

Quoted 31 lines
drivers/iio/adc/max1027.c +0 -0
 	if (state) {
drivers/iio/adc/max1027.c +0 -0
 		/* Start acquisition on cnvst */
 		st->reg = MAX1027_SETUP_REG |

MAX1027_CKS_MODE0 |

drivers/iio/adc/max1027.c +2 -3
@@ -368,9 +371,12 @@ static int max1027_set_trigger_state(struct
 iio_trigger *trig, bool state)
 		if (ret < 0)
 			return ret;
 
-		/* Scan from 0 to max */
-		st->reg = MAX1027_CONV_REG | MAX1027_CHAN(0) |
-			  MAX1027_SCAN_N_M | MAX1027_TEMP;
+		/*
+		 * Scan from 0 to the highest requested channel. The

temperature

drivers/iio/adc/max1027.c +3 -0
+		 * could be avoided but it simplifies a bit the logic.
+		 */
+		st->reg = MAX1027_CONV_REG |

MAX1027_SCAN_0_N | MAX1027_TEMP;

drivers/iio/adc/max1027.c +1 -0
+		st->reg |= MAX1027_CHAN(fls(*indio_dev-  
Quoted 1 line

active_scan_mask) - 2);

drivers/iio/adc/max1027.c +1 -0
 		ret = spi_write(st->spi, &st->reg, 1);
 		if (ret < 0)
 			return ret;
@@ -391,11 +397,22 @@ static irqreturn_t
 max1027_trigger_handler(int irq, void *private)
 	struct iio_poll_func *pf = private;
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct max1027_state *st = iio_priv(indio_dev);
+	unsigned int scanned_chans = fls(*indio_dev-  
 >active_scan_mask);  
Quoted 8 lines

drivers/iio/adc/max1027.c static irqreturn_t max1027_trigger_handler(int irq, void *private) · line 401

Quoted 1 line
drivers/iio/adc/max1027.c +1 -0
+	u16 *buf = st->buffer;  

I think sparse will complain here. buffer is a __be16 restricted type so you should not mix those...

drivers/iio/adc/max1027.c static irqreturn_t max1027_trigger_handler(int irq, void *private) · line 405

Quoted 4 lines
drivers/iio/adc/max1027.c +1 -0
+	unsigned int bit;
 
 	pr_debug("%s(irq=%d, private=0x%p)\n", __func__, irq,
 

private);in/20210818_miquel_raynal_bring_software_triggers_support _to_max1027_like_adcs.mbx

Quoted 15 lines

drivers/iio/adc/max1027.c static irqreturn_t max1027_trigger_handler(int irq, void *private) · line 413

Quoted 10 lines
drivers/iio/adc/max1027.c +7 -1
 	/* fill buffer with all channel */
-	spi_read(st->spi, st->buffer, indio_dev->masklength * 2);
+	spi_read(st->spi, st->buffer, scanned_chans * 2);
+
+	/* Only keep the channels selected by the user */
+	for_each_set_bit(bit, indio_dev->active_scan_mask,
+			 indio_dev->masklength) {
+		if (buf[0] != st->buffer[bit])
+			buf[0] = st->buffer[bit];  

Since we are here, when looking into the driver, I realized that st->buffer is not DMA safe. In IIO, we kind of want to enforce that all buffers that are passed to spi/i2c buses are safe... Maybe this is something you can include in your series.

Quoted 7 lines
Quoted 3 lines

Why is it not? st->buffer is result of a devm_kmalloc_array() call and that should provide a DMA safe buffer as I understand it.

That's a good question. I'm not sure how I came to that conclusion which is clearly wrong. Though I think the buffer might share the line with the mutex...

Pointer shares a line. The buffer it points to doesn't as allocated by separate heap allocation.

J

Quoted 3 lines

- Nuno Sá

reviewer
#12 lore raw

RE: [PATCH 03/16] iio: adc: max1027: Push only the requested samples

Quoted 170 lines

-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Monday, August 30, 2021 4:30 PM
To: Sa, Nuno <[email protected]>
Cc: Miquel Raynal <[email protected]>; Lars-Peter Clausen <[email protected]>; Thomas Petazzoni <[email protected]>; [email protected]; linux- [email protected]
Subject: Re: [PATCH 03/16] iio: adc: max1027: Push only the requested samples

[External]

Quoted 5 lines · Nuno
Quoted 5 lines

-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Monday, August 30, 2021 12:08 PM
To: Sa, Nuno <[email protected]>
Cc: Miquel Raynal <[email protected]>; Lars-Peter

Clausen

Quoted 2 lines
Quoted 2 lines

<[email protected]>; Thomas Petazzoni <[email protected]>; [email protected];

linux-

Quoted 2 lines
Quoted 2 lines

[email protected]
Subject: Re: [PATCH 03/16] iio: adc: max1027: Push only the

requested

Quoted 7 lines
Quoted 7 lines

samples

[External]

On Fri, 20 Aug 2021 07:10:48 +0000 "Sa, Nuno" <[email protected]> wrote:

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

-----Original Message-----
From: Miquel Raynal <[email protected]>
Sent: Wednesday, August 18, 2021 1:11 PM
To: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>
Cc: Thomas Petazzoni <[email protected]>; linux- [email protected]; [email protected]; Miquel

Raynal

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

<[email protected]>
Subject: [PATCH 03/16] iio: adc: max1027: Push only the

requested

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

samples

[External]

When a triggered scan occurs, the identity of the desired

channels

Quoted 4 lines
Quoted 4 lines

is

Quoted 3 lines
Quoted 3 lines

known in indio_dev->active_scan_mask. Instead of reading and pushing to
the IIO buffers all channels each time, scan the minimum

amount

Quoted 8 lines
Quoted 8 lines

of

Quoted 5 lines
Quoted 5 lines

channels (0 to maximum requested chan, to be exact) and only provide the
samples requested by the user.

For example, if the user wants channels 1, 4 and 5, all channels

from

Quoted 1 line
Quoted 1 line

0 to 5 will be scanned but only the desired channels will be

pushed

Quoted 9 lines
Quoted 9 lines

to

Quoted 8 lines

Review

Quoted 8 lines

the IIO buffers.

---
 drivers/iio/adc/max1027.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
(quoted diff) +0 -0

b/drivers/iio/adc/max1027.c

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

drivers/iio/adc/max1027.c static int

Quoted 4 lines

index b753658bb41e..8ab660f596b5 100644

drivers/iio/adc/max1027.c +0 -0
@@ -360,6 +360,9 @@ static int

max1027_set_trigger_state(struct

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

drivers/iio/adc/max1027.c static int max1027_set_trigger_state(struct iio_trigger *trig, bool state)

Quoted 5 lines
drivers/iio/adc/max1027.c +1 -0
 iio_trigger *trig, bool state)
 	struct max1027_state *st = iio_priv(indio_dev);
 	int ret;
 
+	if (bitmap_empty(indio_dev->active_scan_mask,

indio_dev-

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

drivers/iio/adc/max1027.c static int max1027_set_trigger_state(struct iio_trigger *trig, bool state) · line 365

Quoted 3 lines
drivers/iio/adc/max1027.c +2 -0
 >masklength))
+		return -EINVAL;
+

I'm not sure this can actually happen. If you try to enable the

buffer

Quoted 1 line
Quoted 1 line
Quoted 1 line

with no scan element, it should give you an error before you

reach

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

this point...

drivers/iio/adc/max1027.c static int

Quoted 5 lines
drivers/iio/adc/max1027.c +0 -0
 	if (state) {
drivers/iio/adc/max1027.c +0 -0
 		/* Start acquisition on cnvst */
 		st->reg = MAX1027_SETUP_REG |

MAX1027_CKS_MODE0 |

drivers/iio/adc/max1027.c +0 -0
@@ -368,9 +371,12 @@ static int

max1027_set_trigger_state(struct

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

drivers/iio/adc/max1027.c static int max1027_set_trigger_state(struct iio_trigger *trig, bool state)

Quoted 6 lines
drivers/iio/adc/max1027.c +0 -2
 iio_trigger *trig, bool state)
 		if (ret < 0)
 			return ret;
 
-		/* Scan from 0 to max */
-		st->reg = MAX1027_CONV_REG |

MAX1027_CHAN(0) |

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/iio/adc/max1027.c static int · line 374

Quoted 1 line
drivers/iio/adc/max1027.c +0 -1
-			  MAX1027_SCAN_N_M |

MAX1027_TEMP;

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/iio/adc/max1027.c static int · line 376

Quoted 2 lines
drivers/iio/adc/max1027.c +2 -0
+		/*
+		 * Scan from 0 to the highest requested

channel. The

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/iio/adc/max1027.c

Quoted 2 lines

temperature

drivers/iio/adc/max1027.c +1 -0
+		 * could be avoided but it simplifies a bit the

logic.

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
drivers/iio/adc/max1027.c +2 -0
+		 */
+		st->reg = MAX1027_CONV_REG |

MAX1027_SCAN_0_N | MAX1027_TEMP;

drivers/iio/adc/max1027.c +1 -0
+		st->reg |= MAX1027_CHAN(fls(*indio_dev-
Quoted 1 line

active_scan_mask) - 2);

drivers/iio/adc/max1027.c +1 -0
 		ret = spi_write(st->spi, &st->reg, 1);
 		if (ret < 0)
 			return ret;
@@ -391,11 +397,22 @@ static irqreturn_t
 max1027_trigger_handler(int irq, void *private)
 	struct iio_poll_func *pf = private;
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct max1027_state *st = iio_priv(indio_dev);
+	unsigned int scanned_chans = fls(*indio_dev-
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/adc/max1027.c static irqreturn_t · line 403

Quoted 2 lines
drivers/iio/adc/max1027.c +1 -0
 >active_scan_mask);
+	u16 *buf = st->buffer;

I think sparse will complain here. buffer is a __be16 restricted type so you should not mix those...

drivers/iio/adc/max1027.c static irqreturn_t max1027_trigger_handler(int irq, void *private)

Quoted 3 lines
drivers/iio/adc/max1027.c +1 -0
+	unsigned int bit;
 
 	pr_debug("%s(irq=%d, private=0x%p)\n", __func__,

irq,

Quoted 2 lines
Quoted 2 lines
Quoted 1 line
Quoted 1 line

private);in/20210818_miquel_raynal_bring_software_triggers_support

Quoted 4 lines
Quoted 4 lines

_to_max1027_like_adcs.mbx

Quoted 3 lines

drivers/iio/adc/max1027.c

Quoted 3 lines
drivers/iio/adc/max1027.c +0 -1
 	/* fill buffer with all channel */
-	spi_read(st->spi, st->buffer, indio_dev->masklength *

2);

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

drivers/iio/adc/max1027.c static irqreturn_t max1027_trigger_handler(int irq, void *private) · line 413

Quoted 7 lines
drivers/iio/adc/max1027.c +7 -0
+	spi_read(st->spi, st->buffer, scanned_chans * 2);
+
+	/* Only keep the channels selected by the user */
+	for_each_set_bit(bit, indio_dev->active_scan_mask,
+			 indio_dev->masklength) {
+		if (buf[0] != st->buffer[bit])
+			buf[0] = st->buffer[bit];

Since we are here, when looking into the driver, I realized that st->buffer is not DMA safe. In IIO, we kind of want to

enforce

Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

that all buffers that are passed to spi/i2c buses are safe... Maybe this is something you can include in your series.

Why is it not? st->buffer is result of a devm_kmalloc_array() call

and

Quoted 3 lines
Quoted 2 lines

that should provide a DMA safe buffer as I understand it.

Quoted 8 lines
Quoted 1 line

That's a good question. I'm not sure how I came to that conclusion

which

Quoted 1 line

is clearly wrong. Though I think the buffer might share the line with

the

Quoted 1 line

mutex...

Pointer shares a line. The buffer it points to doesn't as allocated by separate heap allocation.

Ups, sure :facepalm:

- Nuno Sá

#13 lore raw

Re: [PATCH 03/16] iio: adc: max1027: Push only the requested samples

Hello,

"Sa, Nuno" <[email protected]> wrote on Mon, 30 Aug 2021 15:02:26 +0000:

Quoted 170 lines
Quoted 170 lines

-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Monday, August 30, 2021 4:30 PM
To: Sa, Nuno <[email protected]>
Cc: Miquel Raynal <[email protected]>; Lars-Peter Clausen <[email protected]>; Thomas Petazzoni <[email protected]>; [email protected]; linux- [email protected]
Subject: Re: [PATCH 03/16] iio: adc: max1027: Push only the requested samples

[External]

Quoted 5 lines · Nuno
Quoted 5 lines

-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Monday, August 30, 2021 12:08 PM
To: Sa, Nuno <[email protected]>
Cc: Miquel Raynal <[email protected]>; Lars-Peter

Clausen

Quoted 2 lines
Quoted 2 lines

<[email protected]>; Thomas Petazzoni <[email protected]>; [email protected];

linux-

Quoted 2 lines
Quoted 2 lines

[email protected]
Subject: Re: [PATCH 03/16] iio: adc: max1027: Push only the

requested

Quoted 7 lines
Quoted 7 lines

samples

[External]

On Fri, 20 Aug 2021 07:10:48 +0000 "Sa, Nuno" <[email protected]> wrote:

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

-----Original Message-----
From: Miquel Raynal <[email protected]>
Sent: Wednesday, August 18, 2021 1:11 PM
To: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>
Cc: Thomas Petazzoni <[email protected]>; linux- [email protected]; [email protected]; Miquel

Raynal

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

<[email protected]>
Subject: [PATCH 03/16] iio: adc: max1027: Push only the

requested

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

samples

[External]

When a triggered scan occurs, the identity of the desired

channels

Quoted 4 lines
Quoted 4 lines

is

Quoted 3 lines
Quoted 3 lines

known in indio_dev->active_scan_mask. Instead of reading and pushing to
the IIO buffers all channels each time, scan the minimum

amount

Quoted 8 lines
Quoted 8 lines

of

Quoted 5 lines
Quoted 5 lines

channels (0 to maximum requested chan, to be exact) and only provide the
samples requested by the user.

For example, if the user wants channels 1, 4 and 5, all channels

from

Quoted 1 line
Quoted 1 line

0 to 5 will be scanned but only the desired channels will be

pushed

Quoted 9 lines
Quoted 9 lines

to

Quoted 8 lines

Review

Quoted 8 lines

the IIO buffers.

---
 drivers/iio/adc/max1027.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
(quoted diff) +0 -0

b/drivers/iio/adc/max1027.c

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

drivers/iio/adc/max1027.c static int

Quoted 4 lines

index b753658bb41e..8ab660f596b5 100644

drivers/iio/adc/max1027.c +0 -0
@@ -360,6 +360,9 @@ static int  

max1027_set_trigger_state(struct

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

drivers/iio/adc/max1027.c static int max1027_set_trigger_state(struct iio_trigger *trig, bool state)

Quoted 5 lines
drivers/iio/adc/max1027.c +1 -0
 iio_trigger *trig, bool state)
 	struct max1027_state *st = iio_priv(indio_dev);
 	int ret;
 
+	if (bitmap_empty(indio_dev->active_scan_mask,  

indio_dev-

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

drivers/iio/adc/max1027.c static int max1027_set_trigger_state(struct iio_trigger *trig, bool state) · line 365

Quoted 3 lines
drivers/iio/adc/max1027.c +2 -0
 >masklength))  
+		return -EINVAL;
+  

I'm not sure this can actually happen. If you try to enable the

buffer

Quoted 1 line
Quoted 1 line
Quoted 1 line

with no scan element, it should give you an error before you

reach

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

this point...

drivers/iio/adc/max1027.c static int

Quoted 5 lines
drivers/iio/adc/max1027.c +0 -0
 	if (state) {
drivers/iio/adc/max1027.c +0 -0
 		/* Start acquisition on cnvst */
 		st->reg = MAX1027_SETUP_REG |

MAX1027_CKS_MODE0 |

drivers/iio/adc/max1027.c +0 -0
@@ -368,9 +371,12 @@ static int  

max1027_set_trigger_state(struct

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

drivers/iio/adc/max1027.c static int max1027_set_trigger_state(struct iio_trigger *trig, bool state)

Quoted 6 lines
drivers/iio/adc/max1027.c +0 -2
 iio_trigger *trig, bool state)
 		if (ret < 0)
 			return ret;
 
-		/* Scan from 0 to max */
-		st->reg = MAX1027_CONV_REG |  

MAX1027_CHAN(0) |

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/iio/adc/max1027.c static int · line 374

Quoted 1 line
drivers/iio/adc/max1027.c +0 -1
-			  MAX1027_SCAN_N_M |  

MAX1027_TEMP;

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/iio/adc/max1027.c static int · line 376

Quoted 2 lines
drivers/iio/adc/max1027.c +2 -0
+		/*
+		 * Scan from 0 to the highest requested  

channel. The

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/iio/adc/max1027.c

Quoted 2 lines

temperature

drivers/iio/adc/max1027.c +1 -0
+		 * could be avoided but it simplifies a bit the  

logic.

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
drivers/iio/adc/max1027.c +2 -0
+		 */
+		st->reg = MAX1027_CONV_REG |

MAX1027_SCAN_0_N | MAX1027_TEMP;

drivers/iio/adc/max1027.c +1 -0
+		st->reg |= MAX1027_CHAN(fls(*indio_dev-  
Quoted 1 line

active_scan_mask) - 2);

drivers/iio/adc/max1027.c +1 -0
 		ret = spi_write(st->spi, &st->reg, 1);
 		if (ret < 0)
 			return ret;
@@ -391,11 +397,22 @@ static irqreturn_t
 max1027_trigger_handler(int irq, void *private)
 	struct iio_poll_func *pf = private;
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct max1027_state *st = iio_priv(indio_dev);
+	unsigned int scanned_chans = fls(*indio_dev-  
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/adc/max1027.c static irqreturn_t · line 403

Quoted 2 lines
drivers/iio/adc/max1027.c +1 -0
 >active_scan_mask);  
+	u16 *buf = st->buffer;  

I think sparse will complain here. buffer is a __be16 restricted type so you should not mix those...

drivers/iio/adc/max1027.c static irqreturn_t max1027_trigger_handler(int irq, void *private)

Quoted 3 lines
drivers/iio/adc/max1027.c +1 -0
+	unsigned int bit;
 
 	pr_debug("%s(irq=%d, private=0x%p)\n", __func__,  

irq,

Quoted 2 lines
Quoted 2 lines
Quoted 1 line
Quoted 1 line

private);in/20210818_miquel_raynal_bring_software_triggers_support

Quoted 4 lines
Quoted 4 lines

_to_max1027_like_adcs.mbx

Quoted 3 lines

drivers/iio/adc/max1027.c

Quoted 3 lines
drivers/iio/adc/max1027.c +0 -1
 	/* fill buffer with all channel */
-	spi_read(st->spi, st->buffer, indio_dev->masklength *  

2);

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

drivers/iio/adc/max1027.c static irqreturn_t max1027_trigger_handler(int irq, void *private) · line 413

Quoted 7 lines
drivers/iio/adc/max1027.c +7 -0
+	spi_read(st->spi, st->buffer, scanned_chans * 2);
+
+	/* Only keep the channels selected by the user */
+	for_each_set_bit(bit, indio_dev->active_scan_mask,
+			 indio_dev->masklength) {
+		if (buf[0] != st->buffer[bit])
+			buf[0] = st->buffer[bit];  

Since we are here, when looking into the driver, I realized that st->buffer is not DMA safe. In IIO, we kind of want to

enforce

Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

that all buffers that are passed to spi/i2c buses are safe... Maybe this is something you can include in your series.

Why is it not? st->buffer is result of a devm_kmalloc_array() call

and

Quoted 4 lines
Quoted 2 lines

that should provide a DMA safe buffer as I understand it.

That's a good question. I'm not sure how I came to that conclusion

which

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

is clearly wrong. Though I think the buffer might share the line with

the

Quoted 1 line

mutex...

Pointer shares a line. The buffer it points to doesn't as allocated by separate heap allocation.

Ups, sure :facepalm:

My understanding [1] was that devm_ allocations were generally not suitable for DMA and should not be used for this particular purpose because of the extra 16 bytes allocated for storing the devm magic somewhere, which shifts the entire buffer and prevents it to always be aligned on a cache line. I will propose a patch to switch to kmalloc_array() instead.

[1] https://linux-arm-kernel.infradead.narkive.com/vyJqy0RQ/question-devm-kmalloc-for-dma

Thanks,
Miquèl

maintainer
#14 lore raw

Re: [PATCH 03/16] iio: adc: max1027: Push only the requested samples

Quoted 184 lines · On Wed, 1 Sep 2021 10:12:09 +0200 Miquel Raynal <[email protected]>…

Hello,

"Sa, Nuno" <[email protected]> wrote on Mon, 30 Aug 2021 15:02:26 +0000:

Quoted 170 lines
Quoted 170 lines

-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Monday, August 30, 2021 4:30 PM
To: Sa, Nuno <[email protected]>
Cc: Miquel Raynal <[email protected]>; Lars-Peter Clausen <[email protected]>; Thomas Petazzoni <[email protected]>; [email protected]; linux- [email protected]
Subject: Re: [PATCH 03/16] iio: adc: max1027: Push only the requested samples

[External]

Quoted 5 lines · Nuno
Quoted 5 lines

-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Monday, August 30, 2021 12:08 PM
To: Sa, Nuno <[email protected]>
Cc: Miquel Raynal <[email protected]>; Lars-Peter

Clausen

Quoted 2 lines
Quoted 2 lines

<[email protected]>; Thomas Petazzoni <[email protected]>; [email protected];

linux-

Quoted 2 lines
Quoted 2 lines

[email protected]
Subject: Re: [PATCH 03/16] iio: adc: max1027: Push only the

requested

Quoted 7 lines
Quoted 7 lines

samples

[External]

On Fri, 20 Aug 2021 07:10:48 +0000 "Sa, Nuno" <[email protected]> wrote:

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

-----Original Message-----
From: Miquel Raynal <[email protected]>
Sent: Wednesday, August 18, 2021 1:11 PM
To: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>
Cc: Thomas Petazzoni <[email protected]>; linux- [email protected]; [email protected]; Miquel

Raynal

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

<[email protected]>
Subject: [PATCH 03/16] iio: adc: max1027: Push only the

requested

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

samples

[External]

When a triggered scan occurs, the identity of the desired

channels

Quoted 4 lines
Quoted 4 lines

is

Quoted 3 lines
Quoted 3 lines

known in indio_dev->active_scan_mask. Instead of reading and pushing to
the IIO buffers all channels each time, scan the minimum

amount

Quoted 8 lines
Quoted 8 lines

of

Quoted 5 lines
Quoted 5 lines

channels (0 to maximum requested chan, to be exact) and only provide the
samples requested by the user.

For example, if the user wants channels 1, 4 and 5, all channels

from

Quoted 1 line
Quoted 1 line

0 to 5 will be scanned but only the desired channels will be

pushed

Quoted 9 lines
Quoted 9 lines

to

Quoted 8 lines

Review

Quoted 8 lines

the IIO buffers.

---
 drivers/iio/adc/max1027.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
(quoted diff) +0 -0

b/drivers/iio/adc/max1027.c

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

drivers/iio/adc/max1027.c static int

Quoted 4 lines

index b753658bb41e..8ab660f596b5 100644

drivers/iio/adc/max1027.c +0 -0
@@ -360,6 +360,9 @@ static int    

max1027_set_trigger_state(struct

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

drivers/iio/adc/max1027.c static int · line 364

Quoted 5 lines
drivers/iio/adc/max1027.c +1 -0
 iio_trigger *trig, bool state)
 	struct max1027_state *st = iio_priv(indio_dev);
 	int ret;
 
+	if (bitmap_empty(indio_dev->active_scan_mask,    

indio_dev-

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

drivers/iio/adc/max1027.c static int · line 367

Quoted 3 lines
drivers/iio/adc/max1027.c +2 -0
 >masklength))    
+		return -EINVAL;
+    

I'm not sure this can actually happen. If you try to enable the

buffer

Quoted 1 line
Quoted 1 line
Quoted 1 line

with no scan element, it should give you an error before you

reach

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

this point...

drivers/iio/adc/max1027.c static int

Quoted 5 lines
drivers/iio/adc/max1027.c +0 -0
 	if (state) {
drivers/iio/adc/max1027.c +0 -0
 		/* Start acquisition on cnvst */
 		st->reg = MAX1027_SETUP_REG |

MAX1027_CKS_MODE0 |

drivers/iio/adc/max1027.c +0 -0
@@ -368,9 +371,12 @@ static int    

max1027_set_trigger_state(struct

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

drivers/iio/adc/max1027.c static int · line 373

Quoted 6 lines
drivers/iio/adc/max1027.c +0 -2
 iio_trigger *trig, bool state)
 		if (ret < 0)
 			return ret;
 
-		/* Scan from 0 to max */
-		st->reg = MAX1027_CONV_REG |    

MAX1027_CHAN(0) |

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/iio/adc/max1027.c static int · line 374

Quoted 1 line
drivers/iio/adc/max1027.c +0 -1
-			  MAX1027_SCAN_N_M |    

MAX1027_TEMP;

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/iio/adc/max1027.c static int · line 376

Quoted 2 lines
drivers/iio/adc/max1027.c +2 -0
+		/*
+		 * Scan from 0 to the highest requested    

channel. The

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/iio/adc/max1027.c

Quoted 2 lines

temperature

drivers/iio/adc/max1027.c +1 -0
+		 * could be avoided but it simplifies a bit the    

logic.

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
drivers/iio/adc/max1027.c +2 -0
+		 */
+		st->reg = MAX1027_CONV_REG |

MAX1027_SCAN_0_N | MAX1027_TEMP;

drivers/iio/adc/max1027.c +1 -0
+		st->reg |= MAX1027_CHAN(fls(*indio_dev-    
Quoted 1 line

active_scan_mask) - 2);

drivers/iio/adc/max1027.c +1 -0
 		ret = spi_write(st->spi, &st->reg, 1);
 		if (ret < 0)
 			return ret;
@@ -391,11 +397,22 @@ static irqreturn_t
 max1027_trigger_handler(int irq, void *private)
 	struct iio_poll_func *pf = private;
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct max1027_state *st = iio_priv(indio_dev);
+	unsigned int scanned_chans = fls(*indio_dev-    
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/adc/max1027.c static irqreturn_t · line 403

Quoted 2 lines
drivers/iio/adc/max1027.c +1 -0
 >active_scan_mask);    
+	u16 *buf = st->buffer;    

I think sparse will complain here. buffer is a __be16 restricted type so you should not mix those...

drivers/iio/adc/max1027.c static irqreturn_t · line 406

Quoted 3 lines
drivers/iio/adc/max1027.c +1 -0
+	unsigned int bit;
 
 	pr_debug("%s(irq=%d, private=0x%p)\n", __func__,    

irq,

Quoted 2 lines
Quoted 2 lines
Quoted 1 line
Quoted 1 line

private);in/20210818_miquel_raynal_bring_software_triggers_support

Quoted 4 lines
Quoted 4 lines

_to_max1027_like_adcs.mbx

Quoted 3 lines

drivers/iio/adc/max1027.c

Quoted 3 lines
drivers/iio/adc/max1027.c +0 -1
 	/* fill buffer with all channel */
-	spi_read(st->spi, st->buffer, indio_dev->masklength *    

2);

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

drivers/iio/adc/max1027.c

Quoted 7 lines
drivers/iio/adc/max1027.c +7 -0
+	spi_read(st->spi, st->buffer, scanned_chans * 2);
+
+	/* Only keep the channels selected by the user */
+	for_each_set_bit(bit, indio_dev->active_scan_mask,
+			 indio_dev->masklength) {
+		if (buf[0] != st->buffer[bit])
+			buf[0] = st->buffer[bit];    

Since we are here, when looking into the driver, I realized that st->buffer is not DMA safe. In IIO, we kind of want to

enforce

Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

that all buffers that are passed to spi/i2c buses are safe... Maybe this is something you can include in your series.

Why is it not? st->buffer is result of a devm_kmalloc_array() call

and

Quoted 4 lines
Quoted 2 lines

that should provide a DMA safe buffer as I understand it.

That's a good question. I'm not sure how I came to that conclusion

which

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

is clearly wrong. Though I think the buffer might share the line with

the

Quoted 1 line

mutex...

Pointer shares a line. The buffer it points to doesn't as allocated by separate heap allocation.

Ups, sure :facepalm:

Quoted 8 lines

My understanding [1] was that devm_ allocations were generally not suitable for DMA and should not be used for this particular purpose because of the extra 16 bytes allocated for storing the devm magic somewhere, which shifts the entire buffer and prevents it to always be aligned on a cache line. I will propose a patch to switch to kmalloc_array() instead.

[1] https://linux-arm-kernel.infradead.narkive.com/vyJqy0RQ/question-devm-kmalloc-for-dma

That shouldn't actually matter because here we don't care about it being aligned on a cacheline - but we do care about it being aligned so that nothing else we might touch is in the same cacheline. Note the thread you link talks about this.

If we were possibly doing additional devm_ managed allocations whilst DMA was active then it might be a problem, but I'm fairly sure we aren't doing that here.

Not there might be a bus controller that has stricter alignment requirements - whether we need to be careful of those isn't particularly clear to me. There are lots of places in IIO where we cachealign a set of buffers, but for example the rx is after the tx buffers and isn't aligned as would be required. As such I'm fairly sure it's not a problem.

Jonathan

Quoted 3 lines

Thanks,
Miquèl

reviewer
#15 lore raw

RE: [PATCH 03/16] iio: adc: max1027: Push only the requested samples

In reply to Miquel Raynal
Quoted 214 lines

-----Original Message-----
From: Miquel Raynal <[email protected]>
Sent: Wednesday, September 1, 2021 10:12 AM
To: Sa, Nuno <[email protected]>
Cc: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>; Thomas Petazzoni <[email protected]>; [email protected]; linux- [email protected]
Subject: Re: [PATCH 03/16] iio: adc: max1027: Push only the requested samples

[External]

Hello,

"Sa, Nuno" <[email protected]> wrote on Mon, 30 Aug 2021 15:02:26
+0000:

Quoted 5 lines
Quoted 5 lines

-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Monday, August 30, 2021 4:30 PM
To: Sa, Nuno <[email protected]>
Cc: Miquel Raynal <[email protected]>; Lars-Peter

Clausen

Quoted 2 lines
Quoted 2 lines

<[email protected]>; Thomas Petazzoni <[email protected]>; [email protected];

linux-

Quoted 2 lines
Quoted 2 lines

[email protected]
Subject: Re: [PATCH 03/16] iio: adc: max1027: Push only the

requested

Quoted 23 lines
Quoted 23 lines

samples

[External]

Quoted 5 lines · Nuno
Quoted 5 lines

-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Monday, August 30, 2021 12:08 PM
To: Sa, Nuno <[email protected]>
Cc: Miquel Raynal <[email protected]>; Lars-Peter

Clausen

Quoted 2 lines
Quoted 2 lines

<[email protected]>; Thomas Petazzoni <[email protected]>; [email protected];

linux-

Quoted 2 lines
Quoted 2 lines

[email protected]
Subject: Re: [PATCH 03/16] iio: adc: max1027: Push only the

requested

Quoted 4 lines
Quoted 4 lines

samples

[External]

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines · Nuno
Quoted 4 lines

-----Original Message-----
From: Miquel Raynal <[email protected]>
Sent: Wednesday, August 18, 2021 1:11 PM
To: Jonathan Cameron <[email protected]>; Lars-Peter

Clausen

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

<[email protected]>
Cc: Thomas Petazzoni <[email protected]>;

linux-

Quoted 5 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

[email protected]; [email protected]; Miquel

Raynal

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

<[email protected]>
Subject: [PATCH 03/16] iio: adc: max1027: Push only the

requested

Quoted 8 lines
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

samples

[External]

When a triggered scan occurs, the identity of the desired

channels

Quoted 2 lines
Quoted 2 lines

is

Quoted 1 line
Quoted 1 line

known in indio_dev->active_scan_mask. Instead of reading

and

Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

pushing to
the IIO buffers all channels each time, scan the minimum

amount

Quoted 2 lines
Quoted 2 lines

of

Quoted 1 line
Quoted 1 line

channels (0 to maximum requested chan, to be exact) and

only

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

provide the
samples requested by the user.

For example, if the user wants channels 1, 4 and 5, all

channels

Quoted 9 lines
Quoted 9 lines
Quoted 2 lines
Quoted 2 lines

from

Quoted 1 line
Quoted 1 line

0 to 5 will be scanned but only the desired channels will be

pushed

Quoted 6 lines
Quoted 6 lines

to

Quoted 5 lines
Quoted 5 lines

the IIO buffers.

---
 drivers/iio/adc/max1027.c | 25 +++++++++++++++++++++-

---

Quoted 65 lines
Quoted 65 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Review

Quoted 3 lines
 1 file changed, 21 insertions(+), 4 deletions(-)
(quoted diff) +0 -0

b/drivers/iio/adc/max1027.c

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

drivers/iio/adc/max1027.c static int

Quoted 4 lines

index b753658bb41e..8ab660f596b5 100644

drivers/iio/adc/max1027.c +0 -0
@@ -360,6 +360,9 @@ static int

max1027_set_trigger_state(struct

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

drivers/iio/adc/max1027.c static int · line 364

Quoted 5 lines
drivers/iio/adc/max1027.c +1 -0
 iio_trigger *trig, bool state)
 	struct max1027_state *st = iio_priv(indio_dev);
 	int ret;
 
+	if (bitmap_empty(indio_dev->active_scan_mask,

indio_dev-

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

drivers/iio/adc/max1027.c static int · line 367

Quoted 3 lines
drivers/iio/adc/max1027.c +2 -0
 >masklength))
+		return -EINVAL;
+

I'm not sure this can actually happen. If you try to enable the

buffer

Quoted 1 line
Quoted 1 line
Quoted 1 line

with no scan element, it should give you an error before you

reach

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

this point...

drivers/iio/adc/max1027.c static int

Quoted 5 lines
drivers/iio/adc/max1027.c +0 -0
 	if (state) {
drivers/iio/adc/max1027.c +0 -0
 		/* Start acquisition on cnvst */
 		st->reg = MAX1027_SETUP_REG |

MAX1027_CKS_MODE0 |

drivers/iio/adc/max1027.c +0 -0
@@ -368,9 +371,12 @@ static int

max1027_set_trigger_state(struct

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

drivers/iio/adc/max1027.c static int · line 373

Quoted 6 lines
drivers/iio/adc/max1027.c +0 -2
 iio_trigger *trig, bool state)
 		if (ret < 0)
 			return ret;
 
-		/* Scan from 0 to max */
-		st->reg = MAX1027_CONV_REG |

MAX1027_CHAN(0) |

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/iio/adc/max1027.c static int · line 374

Quoted 1 line
drivers/iio/adc/max1027.c +0 -1
-			  MAX1027_SCAN_N_M |

MAX1027_TEMP;

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/iio/adc/max1027.c static int · line 376

Quoted 2 lines
drivers/iio/adc/max1027.c +2 -0
+		/*
+		 * Scan from 0 to the highest requested

channel. The

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/iio/adc/max1027.c

Quoted 2 lines

temperature

drivers/iio/adc/max1027.c +1 -0
+		 * could be avoided but it simplifies a bit the

logic.

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
Quoted 9 lines
drivers/iio/adc/max1027.c +2 -0
+		 */
+		st->reg = MAX1027_CONV_REG |

MAX1027_SCAN_0_N | MAX1027_TEMP;

drivers/iio/adc/max1027.c +1 -0
+		st->reg |= MAX1027_CHAN(fls(*indio_dev-
Quoted 1 line

active_scan_mask) - 2);

drivers/iio/adc/max1027.c +0 -0
 		ret = spi_write(st->spi, &st->reg, 1);
 		if (ret < 0)
 			return ret;

drivers/iio/adc/max1027.c static irqreturn_t · line 403

Quoted 7 lines
drivers/iio/adc/max1027.c +2 -0
@@ -391,11 +397,22 @@ static irqreturn_t
 max1027_trigger_handler(int irq, void *private)
 	struct iio_poll_func *pf = private;
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct max1027_state *st = iio_priv(indio_dev);
+	unsigned int scanned_chans = fls(*indio_dev-
 >active_scan_mask);
+	u16 *buf = st->buffer;

I think sparse will complain here. buffer is a __be16 restricted type so you should not mix those...

Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/iio/adc/max1027.c static irqreturn_t · line 406

Quoted 3 lines
drivers/iio/adc/max1027.c +1 -0
+	unsigned int bit;
 
 	pr_debug("%s(irq=%d, private=0x%p)\n", __func__,

irq,

Quoted 2 lines
Quoted 2 lines
Quoted 1 line
Quoted 1 line

private);in/20210818_miquel_raynal_bring_software_triggers_support

Quoted 15 lines
Quoted 15 lines
Quoted 4 lines
Quoted 4 lines

_to_max1027_like_adcs.mbx

Quoted 3 lines

drivers/iio/adc/max1027.c

Quoted 3 lines
drivers/iio/adc/max1027.c +0 -1
 	/* fill buffer with all channel */
-	spi_read(st->spi, st->buffer, indio_dev->masklength *

2);

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

drivers/iio/adc/max1027.c

Quoted 7 lines
drivers/iio/adc/max1027.c +7 -0
+	spi_read(st->spi, st->buffer, scanned_chans * 2);
+
+	/* Only keep the channels selected by the user */
+	for_each_set_bit(bit, indio_dev->active_scan_mask,
+			 indio_dev->masklength) {
+		if (buf[0] != st->buffer[bit])
+			buf[0] = st->buffer[bit];

Since we are here, when looking into the driver, I realized that st->buffer is not DMA safe. In IIO, we kind of want to

Quoted 2 lines
Quoted 2 lines

enforce

Quoted 1 line
Quoted 1 line
Quoted 1 line

that all buffers that are passed to spi/i2c buses are safe...

Maybe

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

this is something you can include in your series.

Why is it not? st->buffer is result of a devm_kmalloc_array()

call

Quoted 5 lines
Quoted 5 lines

and

Quoted 4 lines
Quoted 2 lines

that should provide a DMA safe buffer as I understand it.

That's a good question. I'm not sure how I came to that

conclusion

Quoted 2 lines
Quoted 2 lines

which

Quoted 1 line

is clearly wrong. Though I think the buffer might share the line

with

Quoted 7 lines
Quoted 5 lines

the

Quoted 1 line

mutex...

Pointer shares a line. The buffer it points to doesn't as allocated by separate heap allocation.

Ups, sure :facepalm:

Quoted 7 lines

My understanding [1] was that devm_ allocations were generally not suitable for DMA and should not be used for this particular purpose because of the extra 16 bytes allocated for storing the devm magic somewhere, which shifts the entire buffer and prevents it to always be
aligned on a cache line. I will propose a patch to switch to kmalloc_array() instead.

I do not think this is a problem anymore [1]. Nowadays, 'devm_kmalloc' should give you the same alignment guarantees as 'kmalloc'

[1]: https://elixir.bootlin.com/linux/latest/source/drivers/base/devres.c#L35

- Nuno Sá

maintainer
#16 lore raw

Re: [PATCH 03/16] iio: adc: max1027: Push only the requested samples

Quoted 220 lines · Nuno
Quoted 212 lines

-----Original Message-----
From: Miquel Raynal <[email protected]>
Sent: Wednesday, September 1, 2021 10:12 AM
To: Sa, Nuno <[email protected]>
Cc: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>; Thomas Petazzoni <[email protected]>; [email protected]; linux- [email protected]
Subject: Re: [PATCH 03/16] iio: adc: max1027: Push only the requested samples

[External]

Hello,

"Sa, Nuno" <[email protected]> wrote on Mon, 30 Aug 2021 15:02:26
+0000:

Quoted 5 lines
Quoted 5 lines

-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Monday, August 30, 2021 4:30 PM
To: Sa, Nuno <[email protected]>
Cc: Miquel Raynal <[email protected]>; Lars-Peter

Clausen

Quoted 2 lines
Quoted 2 lines

<[email protected]>; Thomas Petazzoni <[email protected]>; [email protected];

linux-

Quoted 2 lines
Quoted 2 lines

[email protected]
Subject: Re: [PATCH 03/16] iio: adc: max1027: Push only the

requested

Quoted 23 lines
Quoted 23 lines

samples

[External]

Quoted 5 lines · Nuno
Quoted 5 lines

-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Monday, August 30, 2021 12:08 PM
To: Sa, Nuno <[email protected]>
Cc: Miquel Raynal <[email protected]>; Lars-Peter

Clausen

Quoted 2 lines
Quoted 2 lines

<[email protected]>; Thomas Petazzoni <[email protected]>; [email protected];

linux-

Quoted 2 lines
Quoted 2 lines

[email protected]
Subject: Re: [PATCH 03/16] iio: adc: max1027: Push only the

requested

Quoted 4 lines
Quoted 4 lines

samples

[External]

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines · Nuno
Quoted 4 lines

-----Original Message-----
From: Miquel Raynal <[email protected]>
Sent: Wednesday, August 18, 2021 1:11 PM
To: Jonathan Cameron <[email protected]>; Lars-Peter

Clausen

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

<[email protected]>
Cc: Thomas Petazzoni <[email protected]>;

linux-

Quoted 5 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

[email protected]; [email protected]; Miquel

Raynal

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

<[email protected]>
Subject: [PATCH 03/16] iio: adc: max1027: Push only the

requested

Quoted 8 lines
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

samples

[External]

When a triggered scan occurs, the identity of the desired

channels

Quoted 2 lines
Quoted 2 lines

is

Quoted 1 line
Quoted 1 line

known in indio_dev->active_scan_mask. Instead of reading

and

Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

pushing to
the IIO buffers all channels each time, scan the minimum

amount

Quoted 2 lines
Quoted 2 lines

of

Quoted 1 line
Quoted 1 line

channels (0 to maximum requested chan, to be exact) and

only

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

provide the
samples requested by the user.

For example, if the user wants channels 1, 4 and 5, all

channels

Quoted 9 lines
Quoted 9 lines
Quoted 2 lines
Quoted 2 lines

from

Quoted 1 line
Quoted 1 line

0 to 5 will be scanned but only the desired channels will be

pushed

Quoted 6 lines
Quoted 6 lines

to

Quoted 5 lines
Quoted 5 lines

the IIO buffers.

---
 drivers/iio/adc/max1027.c | 25 +++++++++++++++++++++-  

---

Quoted 65 lines
Quoted 65 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Review

Quoted 3 lines
 1 file changed, 21 insertions(+), 4 deletions(-)
(quoted diff) +0 -0

b/drivers/iio/adc/max1027.c

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

drivers/iio/adc/max1027.c static int

Quoted 4 lines

index b753658bb41e..8ab660f596b5 100644

drivers/iio/adc/max1027.c +0 -0
@@ -360,6 +360,9 @@ static int  

max1027_set_trigger_state(struct

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

drivers/iio/adc/max1027.c static int · line 364

Quoted 5 lines
drivers/iio/adc/max1027.c +1 -0
 iio_trigger *trig, bool state)
 	struct max1027_state *st = iio_priv(indio_dev);
 	int ret;
 
+	if (bitmap_empty(indio_dev->active_scan_mask,  

indio_dev-

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

drivers/iio/adc/max1027.c static int · line 367

Quoted 3 lines
drivers/iio/adc/max1027.c +2 -0
 >masklength))  
+		return -EINVAL;
+  

I'm not sure this can actually happen. If you try to enable the

buffer

Quoted 1 line
Quoted 1 line
Quoted 1 line

with no scan element, it should give you an error before you

reach

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

this point...

drivers/iio/adc/max1027.c static int

Quoted 5 lines
drivers/iio/adc/max1027.c +0 -0
 	if (state) {
drivers/iio/adc/max1027.c +0 -0
 		/* Start acquisition on cnvst */
 		st->reg = MAX1027_SETUP_REG |

MAX1027_CKS_MODE0 |

drivers/iio/adc/max1027.c +0 -0
@@ -368,9 +371,12 @@ static int  

max1027_set_trigger_state(struct

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

drivers/iio/adc/max1027.c static int · line 373

Quoted 6 lines
drivers/iio/adc/max1027.c +0 -2
 iio_trigger *trig, bool state)
 		if (ret < 0)
 			return ret;
 
-		/* Scan from 0 to max */
-		st->reg = MAX1027_CONV_REG |  

MAX1027_CHAN(0) |

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/iio/adc/max1027.c static int · line 374

Quoted 1 line
drivers/iio/adc/max1027.c +0 -1
-			  MAX1027_SCAN_N_M |  

MAX1027_TEMP;

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/iio/adc/max1027.c static int · line 376

Quoted 2 lines
drivers/iio/adc/max1027.c +2 -0
+		/*
+		 * Scan from 0 to the highest requested  

channel. The

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/iio/adc/max1027.c

Quoted 2 lines

temperature

drivers/iio/adc/max1027.c +1 -0
+		 * could be avoided but it simplifies a bit the  

logic.

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
Quoted 9 lines
drivers/iio/adc/max1027.c +2 -0
+		 */
+		st->reg = MAX1027_CONV_REG |

MAX1027_SCAN_0_N | MAX1027_TEMP;

drivers/iio/adc/max1027.c +1 -0
+		st->reg |= MAX1027_CHAN(fls(*indio_dev-  
Quoted 1 line

active_scan_mask) - 2);

drivers/iio/adc/max1027.c +0 -0
 		ret = spi_write(st->spi, &st->reg, 1);
 		if (ret < 0)
 			return ret;

drivers/iio/adc/max1027.c static irqreturn_t · line 403

Quoted 7 lines
drivers/iio/adc/max1027.c +2 -0
@@ -391,11 +397,22 @@ static irqreturn_t
 max1027_trigger_handler(int irq, void *private)
 	struct iio_poll_func *pf = private;
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct max1027_state *st = iio_priv(indio_dev);
+	unsigned int scanned_chans = fls(*indio_dev-  
 >active_scan_mask);  
+	u16 *buf = st->buffer;  

I think sparse will complain here. buffer is a __be16 restricted type so you should not mix those...

Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/iio/adc/max1027.c static irqreturn_t · line 406

Quoted 3 lines
drivers/iio/adc/max1027.c +1 -0
+	unsigned int bit;
 
 	pr_debug("%s(irq=%d, private=0x%p)\n", __func__,  

irq,

Quoted 2 lines
Quoted 2 lines
Quoted 1 line
Quoted 1 line

private);in/20210818_miquel_raynal_bring_software_triggers_support

Quoted 15 lines
Quoted 15 lines
Quoted 4 lines
Quoted 4 lines

_to_max1027_like_adcs.mbx

Quoted 3 lines

drivers/iio/adc/max1027.c

Quoted 3 lines
drivers/iio/adc/max1027.c +0 -1
 	/* fill buffer with all channel */
-	spi_read(st->spi, st->buffer, indio_dev->masklength *  

2);

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

drivers/iio/adc/max1027.c

Quoted 7 lines
drivers/iio/adc/max1027.c +7 -0
+	spi_read(st->spi, st->buffer, scanned_chans * 2);
+
+	/* Only keep the channels selected by the user */
+	for_each_set_bit(bit, indio_dev->active_scan_mask,
+			 indio_dev->masklength) {
+		if (buf[0] != st->buffer[bit])
+			buf[0] = st->buffer[bit];  

Since we are here, when looking into the driver, I realized that st->buffer is not DMA safe. In IIO, we kind of want to

Quoted 2 lines
Quoted 2 lines

enforce

Quoted 1 line
Quoted 1 line
Quoted 1 line

that all buffers that are passed to spi/i2c buses are safe...

Maybe

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

this is something you can include in your series.

Why is it not? st->buffer is result of a devm_kmalloc_array()

call

Quoted 5 lines
Quoted 5 lines

and

Quoted 4 lines
Quoted 2 lines

that should provide a DMA safe buffer as I understand it.

That's a good question. I'm not sure how I came to that

conclusion

Quoted 2 lines
Quoted 2 lines

which

Quoted 1 line

is clearly wrong. Though I think the buffer might share the line

with

Quoted 7 lines
Quoted 5 lines

the

Quoted 1 line

mutex...

Pointer shares a line. The buffer it points to doesn't as allocated by separate heap allocation.

Ups, sure :facepalm:

Quoted 7 lines

My understanding [1] was that devm_ allocations were generally not suitable for DMA and should not be used for this particular purpose because of the extra 16 bytes allocated for storing the devm magic somewhere, which shifts the entire buffer and prevents it to always be
aligned on a cache line. I will propose a patch to switch to kmalloc_array() instead.

Quoted 4 lines

I do not think this is a problem anymore [1]. Nowadays, 'devm_kmalloc' should give you the same alignment guarantees as 'kmalloc'

[1]: https://elixir.bootlin.com/linux/latest/source/drivers/base/devres.c#L35

Great info. I remembered a discussion about fixing that, but couldn't find the patch. For some reason I didn't just check the code :)

Thanks.

Jonathan

Quoted 2 lines

- Nuno Sá

#17 lore raw

Re: [PATCH 03/16] iio: adc: max1027: Push only the requested samples

Hi Jonathan,

[email protected] wrote on Mon, 6 Sep 2021 17:56:57 +0100:

Quoted 223 lines
Quoted 220 lines · Nuno
Quoted 219 lines

-----Original Message-----
From: Miquel Raynal <[email protected]>
Sent: Wednesday, September 1, 2021 10:12 AM
To: Sa, Nuno <[email protected]>
Cc: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>; Thomas Petazzoni <[email protected]>; [email protected]; linux- [email protected]
Subject: Re: [PATCH 03/16] iio: adc: max1027: Push only the requested samples

[External]

Hello,

"Sa, Nuno" <[email protected]> wrote on Mon, 30 Aug 2021 15:02:26
+0000:

Quoted 5 lines
Quoted 5 lines

-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Monday, August 30, 2021 4:30 PM
To: Sa, Nuno <[email protected]>
Cc: Miquel Raynal <[email protected]>; Lars-Peter

Clausen

Quoted 2 lines
Quoted 2 lines

<[email protected]>; Thomas Petazzoni <[email protected]>; [email protected];

linux-

Quoted 2 lines
Quoted 2 lines

[email protected]
Subject: Re: [PATCH 03/16] iio: adc: max1027: Push only the

requested

Quoted 23 lines
Quoted 23 lines

samples

[External]

Quoted 5 lines · Nuno
Quoted 5 lines

-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Monday, August 30, 2021 12:08 PM
To: Sa, Nuno <[email protected]>
Cc: Miquel Raynal <[email protected]>; Lars-Peter

Clausen

Quoted 2 lines
Quoted 2 lines

<[email protected]>; Thomas Petazzoni <[email protected]>; [email protected];

linux-

Quoted 2 lines
Quoted 2 lines

[email protected]
Subject: Re: [PATCH 03/16] iio: adc: max1027: Push only the

requested

Quoted 4 lines
Quoted 4 lines

samples

[External]

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines · Nuno
Quoted 4 lines

-----Original Message-----
From: Miquel Raynal <[email protected]>
Sent: Wednesday, August 18, 2021 1:11 PM
To: Jonathan Cameron <[email protected]>; Lars-Peter

Clausen

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

<[email protected]>
Cc: Thomas Petazzoni <[email protected]>;

linux-

Quoted 5 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

[email protected]; [email protected]; Miquel

Raynal

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

<[email protected]>
Subject: [PATCH 03/16] iio: adc: max1027: Push only the

requested

Quoted 8 lines
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

samples

[External]

When a triggered scan occurs, the identity of the desired

channels

Quoted 2 lines
Quoted 2 lines

is

Quoted 1 line
Quoted 1 line

known in indio_dev->active_scan_mask. Instead of reading

and

Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

pushing to
the IIO buffers all channels each time, scan the minimum

amount

Quoted 2 lines
Quoted 2 lines

of

Quoted 1 line
Quoted 1 line

channels (0 to maximum requested chan, to be exact) and

only

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

provide the
samples requested by the user.

For example, if the user wants channels 1, 4 and 5, all

channels

Quoted 9 lines
Quoted 9 lines
Quoted 2 lines
Quoted 2 lines

from

Quoted 1 line
Quoted 1 line

0 to 5 will be scanned but only the desired channels will be

pushed

Quoted 6 lines
Quoted 6 lines

to

Quoted 5 lines
Quoted 5 lines

the IIO buffers.

---
 drivers/iio/adc/max1027.c | 25 +++++++++++++++++++++-    

---

Quoted 65 lines
Quoted 65 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Review

Quoted 3 lines
 1 file changed, 21 insertions(+), 4 deletions(-)
(quoted diff) +0 -0

b/drivers/iio/adc/max1027.c

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

drivers/iio/adc/max1027.c static int

Quoted 4 lines

index b753658bb41e..8ab660f596b5 100644

drivers/iio/adc/max1027.c +0 -0
@@ -360,6 +360,9 @@ static int    

max1027_set_trigger_state(struct

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

drivers/iio/adc/max1027.c static int · line 364

Quoted 5 lines
drivers/iio/adc/max1027.c +1 -0
 iio_trigger *trig, bool state)
 	struct max1027_state *st = iio_priv(indio_dev);
 	int ret;
 
+	if (bitmap_empty(indio_dev->active_scan_mask,    

indio_dev-

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

drivers/iio/adc/max1027.c static int · line 367

Quoted 3 lines
drivers/iio/adc/max1027.c +2 -0
 >masklength))    
+		return -EINVAL;
+    

I'm not sure this can actually happen. If you try to enable the

buffer

Quoted 1 line
Quoted 1 line
Quoted 1 line

with no scan element, it should give you an error before you

reach

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

this point...

drivers/iio/adc/max1027.c static int

Quoted 5 lines
drivers/iio/adc/max1027.c +0 -0
 	if (state) {
drivers/iio/adc/max1027.c +0 -0
 		/* Start acquisition on cnvst */
 		st->reg = MAX1027_SETUP_REG |

MAX1027_CKS_MODE0 |

drivers/iio/adc/max1027.c +0 -0
@@ -368,9 +371,12 @@ static int    

max1027_set_trigger_state(struct

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

drivers/iio/adc/max1027.c static int · line 373

Quoted 6 lines
drivers/iio/adc/max1027.c +0 -2
 iio_trigger *trig, bool state)
 		if (ret < 0)
 			return ret;
 
-		/* Scan from 0 to max */
-		st->reg = MAX1027_CONV_REG |    

MAX1027_CHAN(0) |

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/iio/adc/max1027.c static int · line 374

Quoted 1 line
drivers/iio/adc/max1027.c +0 -1
-			  MAX1027_SCAN_N_M |    

MAX1027_TEMP;

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/iio/adc/max1027.c static int · line 376

Quoted 2 lines
drivers/iio/adc/max1027.c +2 -0
+		/*
+		 * Scan from 0 to the highest requested    

channel. The

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/iio/adc/max1027.c

Quoted 2 lines

temperature

drivers/iio/adc/max1027.c +1 -0
+		 * could be avoided but it simplifies a bit the    

logic.

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
Quoted 9 lines
drivers/iio/adc/max1027.c +2 -0
+		 */
+		st->reg = MAX1027_CONV_REG |

MAX1027_SCAN_0_N | MAX1027_TEMP;

drivers/iio/adc/max1027.c +1 -0
+		st->reg |= MAX1027_CHAN(fls(*indio_dev-    
Quoted 1 line

active_scan_mask) - 2);

drivers/iio/adc/max1027.c +0 -0
 		ret = spi_write(st->spi, &st->reg, 1);
 		if (ret < 0)
 			return ret;

drivers/iio/adc/max1027.c static irqreturn_t · line 403

Quoted 7 lines
drivers/iio/adc/max1027.c +2 -0
@@ -391,11 +397,22 @@ static irqreturn_t
 max1027_trigger_handler(int irq, void *private)
 	struct iio_poll_func *pf = private;
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct max1027_state *st = iio_priv(indio_dev);
+	unsigned int scanned_chans = fls(*indio_dev-    
 >active_scan_mask);    
+	u16 *buf = st->buffer;    

I think sparse will complain here. buffer is a __be16 restricted type so you should not mix those...

Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/iio/adc/max1027.c static irqreturn_t · line 406

Quoted 3 lines
drivers/iio/adc/max1027.c +1 -0
+	unsigned int bit;
 
 	pr_debug("%s(irq=%d, private=0x%p)\n", __func__,    

irq,

Quoted 2 lines
Quoted 2 lines
Quoted 1 line
Quoted 1 line

private);in/20210818_miquel_raynal_bring_software_triggers_support

Quoted 15 lines
Quoted 15 lines
Quoted 4 lines
Quoted 4 lines

_to_max1027_like_adcs.mbx

Quoted 3 lines

drivers/iio/adc/max1027.c

Quoted 3 lines
drivers/iio/adc/max1027.c +0 -1
 	/* fill buffer with all channel */
-	spi_read(st->spi, st->buffer, indio_dev->masklength *    

2);

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

drivers/iio/adc/max1027.c

Quoted 7 lines
drivers/iio/adc/max1027.c +7 -0
+	spi_read(st->spi, st->buffer, scanned_chans * 2);
+
+	/* Only keep the channels selected by the user */
+	for_each_set_bit(bit, indio_dev->active_scan_mask,
+			 indio_dev->masklength) {
+		if (buf[0] != st->buffer[bit])
+			buf[0] = st->buffer[bit];    

Since we are here, when looking into the driver, I realized that st->buffer is not DMA safe. In IIO, we kind of want to

Quoted 2 lines
Quoted 2 lines

enforce

Quoted 1 line
Quoted 1 line
Quoted 1 line

that all buffers that are passed to spi/i2c buses are safe...

Maybe

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

this is something you can include in your series.

Why is it not? st->buffer is result of a devm_kmalloc_array()

call

Quoted 5 lines
Quoted 5 lines

and

Quoted 4 lines
Quoted 2 lines

that should provide a DMA safe buffer as I understand it.

That's a good question. I'm not sure how I came to that

conclusion

Quoted 2 lines
Quoted 2 lines

which

Quoted 1 line

is clearly wrong. Though I think the buffer might share the line

with

Quoted 7 lines
Quoted 5 lines

the

Quoted 1 line

mutex...

Pointer shares a line. The buffer it points to doesn't as allocated by separate heap allocation.

Ups, sure :facepalm:

My understanding [1] was that devm_ allocations were generally not suitable for DMA and should not be used for this particular purpose because of the extra 16 bytes allocated for storing the devm magic somewhere, which shifts the entire buffer and prevents it to always be
aligned on a cache line. I will propose a patch to switch to kmalloc_array() instead.

Quoted 6 lines
Quoted 4 lines

I do not think this is a problem anymore [1]. Nowadays, 'devm_kmalloc' should give you the same alignment guarantees as 'kmalloc'

[1]: https://elixir.bootlin.com/linux/latest/source/drivers/base/devres.c#L35

Great info. I remembered a discussion about fixing that, but couldn't find the patch. For some reason I didn't just check the code :)

Nice! I didn't know about that, thanks a lot for sharing. So this patch can be safely discarded then.

Thanks,
Miquèl

maintainer
#18 lore raw

Re: [PATCH 03/16] iio: adc: max1027: Push only the requested samples

In reply to Miquel Raynal
Quoted 8 lines · On Wed, 18 Aug 2021 13:11:26 +0200 Miquel Raynal <[email protected]>…

When a triggered scan occurs, the identity of the desired channels is known in indio_dev->active_scan_mask. Instead of reading and pushing to the IIO buffers all channels each time, scan the minimum amount of channels (0 to maximum requested chan, to be exact) and only provide the samples requested by the user.

For example, if the user wants channels 1, 4 and 5, all channels from 0 to 5 will be scanned

That's a reasonably optimisation

Quoted 2 lines

but only the desired channels will be pushed to
the IIO buffers.

Don't do this last bit. The core handles demuxing the channels via active_scan_masks. As a general rule it does a more efficient job of this than a hand coded version in a driver (precached copy rules etc). The core has to have the logic anyway to support multiple consumers (e.g. in kernel consumer and userspace) so we reuse it for these cases.

Quoted 23 lines
---
 drivers/iio/adc/max1027.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
drivers/iio/adc/max1027.c +3 -0
index b753658bb41e..8ab660f596b5 100644
@@ -360,6 +360,9 @@ static int max1027_set_trigger_state(struct iio_trigger *trig, bool state)
 	struct max1027_state *st = iio_priv(indio_dev);
 	int ret;
 
+	if (bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength))
+		return -EINVAL;
+
 	if (state) {
 		/* Start acquisition on cnvst */
 		st->reg = MAX1027_SETUP_REG | MAX1027_CKS_MODE0 |
@@ -368,9 +371,12 @@ static int max1027_set_trigger_state(struct iio_trigger *trig, bool state)
 		if (ret < 0)
 			return ret;

drivers/iio/adc/max1027.c static int max1027_set_trigger_state(struct iio_trigger *trig, bool state) · line 379

Quoted 10 lines
drivers/iio/adc/max1027.c +6 -3
 
-		/* Scan from 0 to max */
-		st->reg = MAX1027_CONV_REG | MAX1027_CHAN(0) |
-			  MAX1027_SCAN_N_M | MAX1027_TEMP;
+		/*
+		 * Scan from 0 to the highest requested channel. The temperature
+		 * could be avoided but it simplifies a bit the logic.
+		 */
+		st->reg = MAX1027_CONV_REG | MAX1027_SCAN_0_N | MAX1027_TEMP;
+		st->reg |= MAX1027_CHAN(fls(*indio_dev->active_scan_mask) - 2);

This should be combined with appropriate additions to available_scan_masks arrays so the IIO core can handle choosing the right one to match enabled channels.

Quoted 27 lines
drivers/iio/adc/max1027.c +12 -1
 		ret = spi_write(st->spi, &st->reg, 1);
 		if (ret < 0)
 			return ret;
@@ -391,11 +397,22 @@ static irqreturn_t max1027_trigger_handler(int irq, void *private)
 	struct iio_poll_func *pf = private;
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct max1027_state *st = iio_priv(indio_dev);
+	unsigned int scanned_chans = fls(*indio_dev->active_scan_mask);
+	u16 *buf = st->buffer;
+	unsigned int bit;
 
 	pr_debug("%s(irq=%d, private=0x%p)\n", __func__, irq, private);
 
 	/* fill buffer with all channel */
-	spi_read(st->spi, st->buffer, indio_dev->masklength * 2);
+	spi_read(st->spi, st->buffer, scanned_chans * 2);
+
+	/* Only keep the channels selected by the user */
+	for_each_set_bit(bit, indio_dev->active_scan_mask,
+			 indio_dev->masklength) {
+		if (buf[0] != st->buffer[bit])
+			buf[0] = st->buffer[bit];
+		buf++;
+	}
 
 	iio_push_to_buffers(indio_dev, st->buffer);
 
#19 lore raw

PATCH 4/16 iio: adc: max1027: Lower conversion time

In reply to Miquel Raynal

When only a few channels are requested, the core will request all of them as long as ->available_scan_masks is present, thus reducing the impact of any driver side optimization to avoid converting the unneeded channels.

Do not share this bitmap with the core, which is optional anyway.

---
 drivers/iio/adc/max1027.c | 1 -
 1 file changed, 1 deletion(-)
drivers/iio/adc/max1027.c +0 -1
index 8ab660f596b5..d79dabf20567 100644
@@ -457,7 +457,6 @@ static int max1027_probe(struct spi_device *spi)
 	indio_dev->modes = INDIO_DIRECT_MODE;
 	indio_dev->channels = st->info->channels;
 	indio_dev->num_channels = st->info->num_channels;
-	indio_dev->available_scan_masks = st->info->available_scan_masks;
 
 	st->buffer = devm_kmalloc_array(&indio_dev->dev,
 				  indio_dev->num_channels, 2,
Signature · 1 line
2.27.0
reviewer Reviewed-by on v1 4/16
#20 lore raw

RE: [PATCH 04/16] iio: adc: max1027: Lower conversion time

Quoted 42 lines

-----Original Message-----
From: Miquel Raynal <[email protected]>
Sent: Wednesday, August 18, 2021 1:11 PM
To: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>
Cc: Thomas Petazzoni <[email protected]>; linux- [email protected]; [email protected]; Miquel Raynal <[email protected]>
Subject: [PATCH 04/16] iio: adc: max1027: Lower conversion time

[External]

When only a few channels are requested, the core will request all of them as long as ->available_scan_masks is present, thus reducing the impact of any driver side optimization to avoid converting the unneeded
channels.

Do not share this bitmap with the core, which is optional anyway.

---
 drivers/iio/adc/max1027.c | 1 -
 1 file changed, 1 deletion(-)
drivers/iio/adc/max1027.c +0 -1
index 8ab660f596b5..d79dabf20567 100644
@@ -457,7 +457,6 @@ static int max1027_probe(struct spi_device
 *spi)
 	indio_dev->modes = INDIO_DIRECT_MODE;
 	indio_dev->channels = st->info->channels;
 	indio_dev->num_channels = st->info->num_channels;
-	indio_dev->available_scan_masks = st->info-
 >available_scan_masks;
 
drivers/iio/adc/max1027.c +0 -1
 	st->buffer = devm_kmalloc_array(&indio_dev->dev,
 				  indio_dev->num_channels, 2,
--
Quoted 1 line

2.27.0

maintainer
#21 lore raw

Re: [PATCH 04/16] iio: adc: max1027: Lower conversion time

In reply to Miquel Raynal
Quoted 6 lines · On Wed, 18 Aug 2021 13:11:27 +0200 Miquel Raynal <[email protected]>…

When only a few channels are requested, the core will request all of them as long as ->available_scan_masks is present, thus reducing the impact of any driver side optimization to avoid converting the unneeded channels.

Do not share this bitmap with the core, which is optional anyway.

As mentioned in previous patch - do share it with the core, but update it to include the masks you now support.

As a side note, you should have done this in previous patch anyway given that patch is effectively a noop with out it. (assuming it was a good thing to do at all which I'm not sure it is!)

Jonathan

Quoted 18 lines
---
 drivers/iio/adc/max1027.c | 1 -
 1 file changed, 1 deletion(-)
drivers/iio/adc/max1027.c +0 -1
index 8ab660f596b5..d79dabf20567 100644
@@ -457,7 +457,6 @@ static int max1027_probe(struct spi_device *spi)
 	indio_dev->modes = INDIO_DIRECT_MODE;
 	indio_dev->channels = st->info->channels;
 	indio_dev->num_channels = st->info->num_channels;
-	indio_dev->available_scan_masks = st->info->available_scan_masks;
 
 	st->buffer = devm_kmalloc_array(&indio_dev->dev,
 				  indio_dev->num_channels, 2,
#22 lore raw

PATCH 5/16 iio: adc: max1027: Drop extra warning message

In reply to Miquel Raynal

Memory allocation errors automatically trigger the right logs, no need to have our own.

---
 drivers/iio/adc/max1027.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/iio/adc/max1027.c +1 -3
index d79dabf20567..ac603b4ca787 100644
@@ -461,10 +461,8 @@ static int max1027_probe(struct spi_device *spi)
 	st->buffer = devm_kmalloc_array(&indio_dev->dev,
 				  indio_dev->num_channels, 2,
 				  GFP_KERNEL);
-	if (st->buffer == NULL) {
-		dev_err(&indio_dev->dev, "Can't allocate buffer\n");
+	if (!st->buffer)
 		return -ENOMEM;
-	}
 
 	if (spi->irq) {
 		ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev,
Signature · 1 line
2.27.0
reviewer Reviewed-by on v1 5/16
#23 lore raw

RE: [PATCH 05/16] iio: adc: max1027: Drop extra warning message

Quoted 38 lines

-----Original Message-----
From: Miquel Raynal <[email protected]>
Sent: Wednesday, August 18, 2021 1:11 PM
To: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>
Cc: Thomas Petazzoni <[email protected]>; linux- [email protected]; [email protected]; Miquel Raynal <[email protected]>
Subject: [PATCH 05/16] iio: adc: max1027: Drop extra warning message

[External]

Memory allocation errors automatically trigger the right logs, no need to have our own.

---
 drivers/iio/adc/max1027.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/iio/adc/max1027.c +1 -3
index d79dabf20567..ac603b4ca787 100644
@@ -461,10 +461,8 @@ static int max1027_probe(struct spi_device
 *spi)
 	st->buffer = devm_kmalloc_array(&indio_dev->dev,
 				  indio_dev->num_channels, 2,
 				  GFP_KERNEL);
-	if (st->buffer == NULL) {
-		dev_err(&indio_dev->dev, "Can't allocate buffer\n");
+	if (!st->buffer)
 		return -ENOMEM;
-	}
 
 	if (spi->irq) {
Quoted 4 lines

ret = devm_iio_triggered_buffer_setup(&spi->dev,
indio_dev,
--
2.27.0

maintainer
#24 lore raw

Re: [PATCH 05/16] iio: adc: max1027: Drop extra warning message

In reply to Miquel Raynal
Quoted 13 lines · On Wed, 18 Aug 2021 13:11:28 +0200 Miquel Raynal <[email protected]>…

Memory allocation errors automatically trigger the right logs, no need to have our own.

---
 drivers/iio/adc/max1027.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/iio/adc/max1027.c +0 -0
index d79dabf20567..ac603b4ca787 100644

drivers/iio/adc/max1027.c static int max1027_probe(struct spi_device *spi) · line 464

Quoted 6 lines
drivers/iio/adc/max1027.c +1 -2
@@ -461,10 +461,8 @@ static int max1027_probe(struct spi_device *spi)
 	st->buffer = devm_kmalloc_array(&indio_dev->dev,
 				  indio_dev->num_channels, 2,
 				  GFP_KERNEL);
-	if (st->buffer == NULL) {
-		dev_err(&indio_dev->dev, "Can't allocate buffer\n");
+	if (!st->buffer)

Don't change the form of the check. Whilst it doesn't matter in general which of these is used (and I actually prefer the one you end up with) this change makes this case inconsistent with the style elsewhere in the driver.

If you want to clean these all up then I don't mind that, but please do the lot in one patch doing just that.

Thanks,

Jonathan

Quoted 5 lines
drivers/iio/adc/max1027.c +0 -1
 		return -ENOMEM;
-	}
 
 	if (spi->irq) {
 		ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev,
#25 lore raw

PATCH 6/16 iio: adc: max1027: Rename a helper

In reply to Miquel Raynal

Make it clear that the *_set_trigger_state() hook is responsible for cnvst based conversions by renaming the helper. This may avoid confusions with software trigger support that is going to be introduced.

---
 drivers/iio/adc/max1027.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/adc/max1027.c +2 -2
index ac603b4ca787..a6ebc951c09a 100644
@@ -354,7 +354,7 @@ static int max1027_validate_trigger(struct iio_dev *indio_dev,
 	return 0;
 }
 
-static int max1027_set_trigger_state(struct iio_trigger *trig, bool state)
+static int max1027_set_cnvst_trigger_state(struct iio_trigger *trig, bool state)
 {
 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
 	struct max1027_state *st = iio_priv(indio_dev);
@@ -423,7 +423,7 @@ static irqreturn_t max1027_trigger_handler(int irq, void *private)
 
 static const struct iio_trigger_ops max1027_trigger_ops = {
 	.validate_device = &iio_trigger_validate_own_device,
-	.set_trigger_state = &max1027_set_trigger_state,
+	.set_trigger_state = &max1027_set_cnvst_trigger_state,
 };
 
 static const struct iio_info max1027_info = {
Signature · 1 line
2.27.0
reviewer Reviewed-by on v1 6/16
#26 lore raw

RE: [PATCH 06/16] iio: adc: max1027: Rename a helper

Quoted 49 lines

-----Original Message-----
From: Miquel Raynal <[email protected]>
Sent: Wednesday, August 18, 2021 1:11 PM
To: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>
Cc: Thomas Petazzoni <[email protected]>; linux- [email protected]; [email protected]; Miquel Raynal <[email protected]>
Subject: [PATCH 06/16] iio: adc: max1027: Rename a helper

[External]

Make it clear that the *_set_trigger_state() hook is responsible for cnvst based conversions by renaming the helper. This may avoid confusions with software trigger support that is going to be introduced.

---
 drivers/iio/adc/max1027.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/adc/max1027.c +1 -1
index ac603b4ca787..a6ebc951c09a 100644
@@ -354,7 +354,7 @@ static int max1027_validate_trigger(struct
 iio_dev *indio_dev,
 	return 0;
 }
 
-static int max1027_set_trigger_state(struct iio_trigger *trig, bool
 state)
+static int max1027_set_cnvst_trigger_state(struct iio_trigger *trig,
 bool state)

{

drivers/iio/adc/max1027.c +1 -1
 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
 	struct max1027_state *st = iio_priv(indio_dev);
@@ -423,7 +423,7 @@ static irqreturn_t max1027_trigger_handler(int
 irq, void *private)
 
 static const struct iio_trigger_ops max1027_trigger_ops = {
 	.validate_device = &iio_trigger_validate_own_device,
-	.set_trigger_state = &max1027_set_trigger_state,
+	.set_trigger_state = &max1027_set_cnvst_trigger_state,
 };
 
Quoted 3 lines

static const struct iio_info max1027_info = {
--
2.27.0

#27 lore raw

PATCH 7/16 iio: adc: max1027: Create a helper to configure the trigger

In reply to Miquel Raynal

There are two ways to physically trigger a conversion:
- A falling edge on the cnvst pin
- A write operation on the conversion register

Let's create a helper for this.

---
 drivers/iio/adc/max1027.c | 52 ++++++++++++++++++++++++++-------------
 1 file changed, 35 insertions(+), 17 deletions(-)
drivers/iio/adc/max1027.c +35 -17
index a6ebc951c09a..59914fcfd320 100644
@@ -232,10 +232,37 @@ struct max1027_state {
 	struct iio_trigger		*trig;
 	__be16				*buffer;
 	struct mutex			lock;
-
+	bool				cnvst_trigger;
 	u8				reg ____cacheline_aligned;
 };
 
+static int max1027_configure_trigger(struct iio_dev *indio_dev)
+{
+	struct max1027_state *st = iio_priv(indio_dev);
+	int ret;
+
+	st->reg = MAX1027_SETUP_REG | MAX1027_REF_MODE2;
+
+	/*
+	 * Start acquisition on:
+	 * MODE0: external hardware trigger wired to the cnvst input pin
+	 * MODE2: conversion register write
+	 */
+	if (st->cnvst_trigger)
+		st->reg |= MAX1027_CKS_MODE0;
+	else
+		st->reg |= MAX1027_CKS_MODE2;
+
+	ret = spi_write(st->spi, &st->reg, 1);
+	if (ret < 0) {
+		dev_err(&indio_dev->dev,
+			"Failed to configure register (%d)\n", ret);
+		return ret;
+	}
+
+	return 0;
+}
+
 static int max1027_read_single_value(struct iio_dev *indio_dev,
 				     struct iio_chan_spec const *chan,
 				     int *val)
@@ -248,14 +275,9 @@ static int max1027_read_single_value(struct iio_dev *indio_dev,
 		return -EBUSY;
 	}
 
-	/* Start acquisition on conversion register write */
-	st->reg = MAX1027_SETUP_REG | MAX1027_REF_MODE2 | MAX1027_CKS_MODE2;
-	ret = spi_write(st->spi, &st->reg, 1);
-	if (ret < 0) {
-		dev_err(&indio_dev->dev,
-			"Failed to configure setup register\n");
+	ret = max1027_configure_trigger(indio_dev);
+	if (ret)
 		return ret;
-	}
 
 	/* Configure conversion register with the requested chan */
 	st->reg = MAX1027_CONV_REG | MAX1027_CHAN(chan->channel) |
@@ -363,12 +385,10 @@ static int max1027_set_cnvst_trigger_state(struct iio_trigger *trig, bool state)
 	if (bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength))
 		return -EINVAL;
 
+	st->cnvst_trigger = state;
 	if (state) {
-		/* Start acquisition on cnvst */
-		st->reg = MAX1027_SETUP_REG | MAX1027_CKS_MODE0 |
-			  MAX1027_REF_MODE2;
-		ret = spi_write(st->spi, &st->reg, 1);
-		if (ret < 0)
+		ret = max1027_configure_trigger(indio_dev);
+		if (ret)
 			return ret;
 
 		/*
@@ -382,10 +402,8 @@ static int max1027_set_cnvst_trigger_state(struct iio_trigger *trig, bool state)
 			return ret;
 	} else {
 		/* Start acquisition on conversion register write */
-		st->reg = MAX1027_SETUP_REG | MAX1027_CKS_MODE2	|
-			  MAX1027_REF_MODE2;
-		ret = spi_write(st->spi, &st->reg, 1);
-		if (ret < 0)
+		ret = max1027_configure_trigger(indio_dev);
+		if (ret)
 			return ret;
 	}
 
Signature · 1 line
2.27.0
reviewer Reviewed-by on v1 7/16
#28 lore raw

RE: [PATCH 07/16] iio: adc: max1027: Create a helper to configure the trigger

Quoted 54 lines

-----Original Message-----
From: Miquel Raynal <[email protected]>
Sent: Wednesday, August 18, 2021 1:12 PM
To: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>
Cc: Thomas Petazzoni <[email protected]>; linux- [email protected]; [email protected]; Miquel Raynal <[email protected]>
Subject: [PATCH 07/16] iio: adc: max1027: Create a helper to configure the trigger

[External]

There are two ways to physically trigger a conversion:
- A falling edge on the cnvst pin
- A write operation on the conversion register

Let's create a helper for this.

---
 drivers/iio/adc/max1027.c | 52 ++++++++++++++++++++++++++------
-------
 1 file changed, 35 insertions(+), 17 deletions(-)
drivers/iio/adc/max1027.c +11 -1
index a6ebc951c09a..59914fcfd320 100644
@@ -232,10 +232,37 @@ struct max1027_state {
 	struct iio_trigger		*trig;
 	__be16				*buffer;
 	struct mutex			lock;
-
+	bool				cnvst_trigger;
 	u8				reg ____cacheline_aligned;
 };
 
+static int max1027_configure_trigger(struct iio_dev *indio_dev)
+{
+	struct max1027_state *st = iio_priv(indio_dev);
+	int ret;
+
+	st->reg = MAX1027_SETUP_REG | MAX1027_REF_MODE2;
+
+	/*
+	 * Start acquisition on:
+	 * MODE0: external hardware trigger wired to the cnvst input

pin

drivers/iio/adc/max1027.c +3 -0
+	 * MODE2: conversion register write
+	 */
+	if (st->cnvst_trigger)

drivers/iio/adc/max1027.c struct max1027_state { · line 261

Quoted 10 lines
drivers/iio/adc/max1027.c +10 -0
+		st->reg |= MAX1027_CKS_MODE0;
+	else
+		st->reg |= MAX1027_CKS_MODE2;
+
+	ret = spi_write(st->spi, &st->reg, 1);
+	if (ret < 0) {
+		dev_err(&indio_dev->dev,
+			"Failed to configure register (%d)\n", ret);
+		return ret;
+	}

I do not think the error message is so important here that we cannot make this more simple and just do:

return spi_write(st->spi, &st->reg, 1);

Anyways, not that important so:

Quoted 62 lines
drivers/iio/adc/max1027.c +5 -7
+	return 0;
+}
+
 static int max1027_read_single_value(struct iio_dev *indio_dev,
 				     struct iio_chan_spec const *chan,
 				     int *val)
@@ -248,14 +275,9 @@ static int max1027_read_single_value(struct
 iio_dev *indio_dev,
 		return -EBUSY;
 	}
 
-	/* Start acquisition on conversion register write */
-	st->reg = MAX1027_SETUP_REG | MAX1027_REF_MODE2 |
 MAX1027_CKS_MODE2;
-	ret = spi_write(st->spi, &st->reg, 1);
-	if (ret < 0) {
-		dev_err(&indio_dev->dev,
-			"Failed to configure setup register\n");
+	ret = max1027_configure_trigger(indio_dev);
+	if (ret)
 		return ret;
-	}
 
drivers/iio/adc/max1027.c +0 -0
 	/* Configure conversion register with the requested chan */
 	st->reg = MAX1027_CONV_REG | MAX1027_CHAN(chan-
Quoted 1 line

channel) |

drivers/iio/adc/max1027.c +1 -2
@@ -363,12 +385,10 @@ static int
 max1027_set_cnvst_trigger_state(struct iio_trigger *trig, bool state)
 	if (bitmap_empty(indio_dev->active_scan_mask, indio_dev-
 >masklength))
 		return -EINVAL;
 
+	st->cnvst_trigger = state;
 	if (state) {
-		/* Start acquisition on cnvst */
-		st->reg = MAX1027_SETUP_REG |

MAX1027_CKS_MODE0 |

drivers/iio/adc/max1027.c +2 -4
-			  MAX1027_REF_MODE2;
-		ret = spi_write(st->spi, &st->reg, 1);
-		if (ret < 0)
+		ret = max1027_configure_trigger(indio_dev);
+		if (ret)
 			return ret;
 
 		/*
@@ -382,10 +402,8 @@ static int
 max1027_set_cnvst_trigger_state(struct iio_trigger *trig, bool state)
 			return ret;
 	} else {
 		/* Start acquisition on conversion register write */
-		st->reg = MAX1027_SETUP_REG |

MAX1027_CKS_MODE2 |

drivers/iio/adc/max1027.c +2 -4
-			  MAX1027_REF_MODE2;
-		ret = spi_write(st->spi, &st->reg, 1);
-		if (ret < 0)
+		ret = max1027_configure_trigger(indio_dev);
+		if (ret)
 			return ret;
 	}
 
--

2.27.0

maintainer
#29 lore raw

Re: [PATCH 07/16] iio: adc: max1027: Create a helper to configure the trigger

In reply to Miquel Raynal
Quoted 7 lines · On Wed, 18 Aug 2021 13:11:30 +0200 Miquel Raynal <[email protected]>…

There are two ways to physically trigger a conversion:
- A falling edge on the cnvst pin
- A write operation on the conversion register

Let's create a helper for this.

Trivial comments inline.

Thanks,

Jonathan

drivers/iio/adc/max1027.c struct max1027_state { · line 235

Quoted 13 lines
---
 drivers/iio/adc/max1027.c | 52 ++++++++++++++++++++++++++-------------
 1 file changed, 35 insertions(+), 17 deletions(-)
drivers/iio/adc/max1027.c +0 -1
index a6ebc951c09a..59914fcfd320 100644
@@ -232,10 +232,37 @@ struct max1027_state {
 	struct iio_trigger		*trig;
 	__be16				*buffer;
 	struct mutex			lock;
-

Avoid white space changes in a patch doing something else. In this case, I'd imagine the reg had a blank line before it to more strongly indicate the cacheline_aligned nature meaning there would be a bit of padding inserted here.

drivers/iio/adc/max1027.c struct max1027_state { · line 235

Quoted 1 line
drivers/iio/adc/max1027.c +1 -0
+	bool				cnvst_trigger;

I'd expect a cnvst_trigger named variable to be a pointer to the trigger. Perhaps call it use_cnvst_trigger or something like that?

Quoted 79 lines
drivers/iio/adc/max1027.c +34 -16
 	u8				reg ____cacheline_aligned;
 };
 
+static int max1027_configure_trigger(struct iio_dev *indio_dev)
+{
+	struct max1027_state *st = iio_priv(indio_dev);
+	int ret;
+
+	st->reg = MAX1027_SETUP_REG | MAX1027_REF_MODE2;
+
+	/*
+	 * Start acquisition on:
+	 * MODE0: external hardware trigger wired to the cnvst input pin
+	 * MODE2: conversion register write
+	 */
+	if (st->cnvst_trigger)
+		st->reg |= MAX1027_CKS_MODE0;
+	else
+		st->reg |= MAX1027_CKS_MODE2;
+
+	ret = spi_write(st->spi, &st->reg, 1);
+	if (ret < 0) {
+		dev_err(&indio_dev->dev,
+			"Failed to configure register (%d)\n", ret);
+		return ret;
+	}
+
+	return 0;
+}
+
 static int max1027_read_single_value(struct iio_dev *indio_dev,
 				     struct iio_chan_spec const *chan,
 				     int *val)
@@ -248,14 +275,9 @@ static int max1027_read_single_value(struct iio_dev *indio_dev,
 		return -EBUSY;
 	}
 
-	/* Start acquisition on conversion register write */
-	st->reg = MAX1027_SETUP_REG | MAX1027_REF_MODE2 | MAX1027_CKS_MODE2;
-	ret = spi_write(st->spi, &st->reg, 1);
-	if (ret < 0) {
-		dev_err(&indio_dev->dev,
-			"Failed to configure setup register\n");
+	ret = max1027_configure_trigger(indio_dev);
+	if (ret)
 		return ret;
-	}
 
 	/* Configure conversion register with the requested chan */
 	st->reg = MAX1027_CONV_REG | MAX1027_CHAN(chan->channel) |
@@ -363,12 +385,10 @@ static int max1027_set_cnvst_trigger_state(struct iio_trigger *trig, bool state)
 	if (bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength))
 		return -EINVAL;
 
+	st->cnvst_trigger = state;
 	if (state) {
-		/* Start acquisition on cnvst */
-		st->reg = MAX1027_SETUP_REG | MAX1027_CKS_MODE0 |
-			  MAX1027_REF_MODE2;
-		ret = spi_write(st->spi, &st->reg, 1);
-		if (ret < 0)
+		ret = max1027_configure_trigger(indio_dev);
+		if (ret)
 			return ret;
 
 		/*
@@ -382,10 +402,8 @@ static int max1027_set_cnvst_trigger_state(struct iio_trigger *trig, bool state)
 			return ret;
 	} else {
 		/* Start acquisition on conversion register write */
-		st->reg = MAX1027_SETUP_REG | MAX1027_CKS_MODE2	|
-			  MAX1027_REF_MODE2;
-		ret = spi_write(st->spi, &st->reg, 1);
-		if (ret < 0)
+		ret = max1027_configure_trigger(indio_dev);
+		if (ret)
 			return ret;
 	}
 
#30 lore raw

PATCH 8/16 iio: adc: max1027: Explain better how the trigger state gets changed

In reply to Miquel Raynal

It may appear strange to configure the trigger to start upon register write while we want to disable the trigger. Explain why we do this in the comment.

---
 drivers/iio/adc/max1027.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/iio/adc/max1027.c +6 -1
index 59914fcfd320..4a78c9cbc339 100644
@@ -401,7 +401,12 @@ static int max1027_set_cnvst_trigger_state(struct iio_trigger *trig, bool state)
 		if (ret < 0)
 			return ret;
 	} else {
-		/* Start acquisition on conversion register write */
+		/*
+		 * Start acquisition on conversion register write, which
+		 * basically disables triggering conversions upon cnvst changes
+		 * and thus has the effect of disabling the external hardware
+		 * trigger.
+		 */
 		ret = max1027_configure_trigger(indio_dev);
 		if (ret)
 			return ret;
Signature · 1 line
2.27.0
reviewer Reviewed-by on v1 8/16
#31 lore raw

RE: [PATCH 08/16] iio: adc: max1027: Explain better how the trigger state gets changed

Quoted 46 lines

-----Original Message-----
From: Miquel Raynal <[email protected]>
Sent: Wednesday, August 18, 2021 1:12 PM
To: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>
Cc: Thomas Petazzoni <[email protected]>; linux- [email protected]; [email protected]; Miquel Raynal <[email protected]>
Subject: [PATCH 08/16] iio: adc: max1027: Explain better how the trigger state gets changed

[External]

It may appear strange to configure the trigger to start upon register write while we want to disable the trigger. Explain why we do this in the comment.

---
 drivers/iio/adc/max1027.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/iio/adc/max1027.c +3 -1
index 59914fcfd320..4a78c9cbc339 100644
@@ -401,7 +401,12 @@ static int
 max1027_set_cnvst_trigger_state(struct iio_trigger *trig, bool state)
 		if (ret < 0)
 			return ret;
 	} else {
-		/* Start acquisition on conversion register write */
+		/*
+		 * Start acquisition on conversion register write, which
+		 * basically disables triggering conversions upon cnvst

changes

drivers/iio/adc/max1027.c +1 -0
+		 * and thus has the effect of disabling the external

hardware

drivers/iio/adc/max1027.c +2 -1
+		 * trigger.
+		 */
 		ret = max1027_configure_trigger(indio_dev);
 		if (ret)
 			return ret;
--
Quoted 1 line

2.27.0

#32 lore raw

PATCH 9/16 iio: adc: max1027: Create a helper to configure the channels to scan

In reply to Miquel Raynal

These bits are meant to be reused for triggered buffers setup.

---
 drivers/iio/adc/max1027.c | 26 ++++++++++++++++++--------
 1 file changed, 18 insertions(+), 8 deletions(-)
drivers/iio/adc/max1027.c +18 -8
index 4a78c9cbc339..223c9e4abd86 100644
@@ -236,6 +236,22 @@ struct max1027_state {
 	u8				reg ____cacheline_aligned;
 };
 
+/* Scan from 0 to the highest requested channel */
+static int max1027_configure_chans_to_scan(struct iio_dev *indio_dev)
+{
+	struct max1027_state *st = iio_priv(indio_dev);
+	int ret;
+
+	/* The temperature could be avoided but it simplifies a bit the logic */
+	st->reg = MAX1027_CONV_REG | MAX1027_SCAN_0_N | MAX1027_TEMP;
+	st->reg |= MAX1027_CHAN(fls(*indio_dev->active_scan_mask) - 2);
+	ret = spi_write(st->spi, &st->reg, 1);
+	if (ret < 0)
+		return ret;
+
+	return 0;
+}
+
 static int max1027_configure_trigger(struct iio_dev *indio_dev)
 {
 	struct max1027_state *st = iio_priv(indio_dev);
@@ -391,14 +407,8 @@ static int max1027_set_cnvst_trigger_state(struct iio_trigger *trig, bool state)
 		if (ret)
 			return ret;
 
-		/*
-		 * Scan from 0 to the highest requested channel. The temperature
-		 * could be avoided but it simplifies a bit the logic.
-		 */
-		st->reg = MAX1027_CONV_REG | MAX1027_SCAN_0_N | MAX1027_TEMP;
-		st->reg |= MAX1027_CHAN(fls(*indio_dev->active_scan_mask) - 2);
-		ret = spi_write(st->spi, &st->reg, 1);
-		if (ret < 0)
+		ret = max1027_configure_chans_to_scan(indio_dev);
+		if (ret)
 			return ret;
 	} else {
 		/*
Signature · 1 line
2.27.0
reviewer
#33 lore raw

RE: [PATCH 09/16] iio: adc: max1027: Create a helper to configure the channels to scan

Quoted 43 lines

-----Original Message-----
From: Miquel Raynal <[email protected]>
Sent: Wednesday, August 18, 2021 1:12 PM
To: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>
Cc: Thomas Petazzoni <[email protected]>; linux- [email protected]; [email protected]; Miquel Raynal <[email protected]>
Subject: [PATCH 09/16] iio: adc: max1027: Create a helper to configure the channels to scan

[External]

These bits are meant to be reused for triggered buffers setup.

---
 drivers/iio/adc/max1027.c | 26 ++++++++++++++++++--------
 1 file changed, 18 insertions(+), 8 deletions(-)
drivers/iio/adc/max1027.c +7 -0
index 4a78c9cbc339..223c9e4abd86 100644
@@ -236,6 +236,22 @@ struct max1027_state {
 	u8				reg ____cacheline_aligned;
 };
 
+/* Scan from 0 to the highest requested channel */
+static int max1027_configure_chans_to_scan(struct iio_dev
 *indio_dev)
+{
+	struct max1027_state *st = iio_priv(indio_dev);
+	int ret;
+
+	/* The temperature could be avoided but it simplifies a bit the

logic */

drivers/iio/adc/max1027.c +1 -0
+	st->reg = MAX1027_CONV_REG | MAX1027_SCAN_0_N |

MAX1027_TEMP;

drivers/iio/adc/max1027.c +1 -0
+	st->reg |= MAX1027_CHAN(fls(*indio_dev-
Quoted 1 line

active_scan_mask) - 2);

drivers/iio/adc/max1027.c struct max1027_state { · line 252

Quoted 5 lines
drivers/iio/adc/max1027.c +5 -0
+	ret = spi_write(st->spi, &st->reg, 1);
+	if (ret < 0)
+		return ret;
+
+	return 0;

return spi_write(st->spi, &st->reg, 1);?

Quoted 28 lines
drivers/iio/adc/max1027.c +2 -2
+}
+
 static int max1027_configure_trigger(struct iio_dev *indio_dev)
 {
 	struct max1027_state *st = iio_priv(indio_dev);
@@ -391,14 +407,8 @@ static int
 max1027_set_cnvst_trigger_state(struct iio_trigger *trig, bool state)
 		if (ret)
 			return ret;
 
-		/*
-		 * Scan from 0 to the highest requested channel. The

temperature

drivers/iio/adc/max1027.c +0 -3
-		 * could be avoided but it simplifies a bit the logic.
-		 */
-		st->reg = MAX1027_CONV_REG |

MAX1027_SCAN_0_N | MAX1027_TEMP;

drivers/iio/adc/max1027.c +0 -1
-		st->reg |= MAX1027_CHAN(fls(*indio_dev-
Quoted 1 line

active_scan_mask) - 2);

drivers/iio/adc/max1027.c +2 -3
-		ret = spi_write(st->spi, &st->reg, 1);
-		if (ret < 0)
+		ret = max1027_configure_chans_to_scan(indio_dev);
+		if (ret)
 			return ret;
 	} else {
 		/*
--

2.27.0

#34 lore raw

PATCH 10/16 iio: adc: max1027: Prevent single channel accesses during buffer reads

In reply to Miquel Raynal

When hardware buffers are enabled (the cnvst pin being the trigger), one should not mess with the device state by requesting a single channel read. Prevent it with a iio_buffer_enabled() check.

---
 drivers/iio/adc/max1027.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/iio/adc/max1027.c +2 -0
index 223c9e4abd86..83526f3d7d3a 100644
@@ -335,6 +335,8 @@ static int max1027_read_raw(struct iio_dev *indio_dev,
 
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
+		if (iio_buffer_enabled(indio_dev))
+			return -EBUSY;
 		ret = max1027_read_single_value(indio_dev, chan, val);
 		break;
 	case IIO_CHAN_INFO_SCALE:
Signature · 1 line
2.27.0
reviewer
#35 lore raw

RE: [PATCH 10/16] iio: adc: max1027: Prevent single channel accesses during buffer reads

Quoted 30 lines

-----Original Message-----
From: Miquel Raynal <[email protected]>
Sent: Wednesday, August 18, 2021 1:12 PM
To: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>
Cc: Thomas Petazzoni <[email protected]>; linux- [email protected]; [email protected]; Miquel Raynal <[email protected]>
Subject: [PATCH 10/16] iio: adc: max1027: Prevent single channel accesses during buffer reads

[External]

When hardware buffers are enabled (the cnvst pin being the trigger), one
should not mess with the device state by requesting a single channel read. Prevent it with a iio_buffer_enabled() check.

---
 drivers/iio/adc/max1027.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/iio/adc/max1027.c +0 -0
index 223c9e4abd86..83526f3d7d3a 100644

drivers/iio/adc/max1027.c static int max1027_read_raw(struct iio_dev · line 340

Quoted 6 lines
drivers/iio/adc/max1027.c +2 -0
@@ -335,6 +335,8 @@ static int max1027_read_raw(struct iio_dev
 *indio_dev,
 
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
+		if (iio_buffer_enabled(indio_dev))
+			return -EBUSY;

I guess 'iio_device_claim_direct_mode()' would be a better option here? There's nothing preventing this check to pass and then, concurrently someone enables the buffer...

- Nuno Sá

Quoted 6 lines
drivers/iio/adc/max1027.c +0 -0
 		ret = max1027_read_single_value(indio_dev, chan,
 val);
drivers/iio/adc/max1027.c +0 -1
 		break;
 	case IIO_CHAN_INFO_SCALE:
--

2.27.0

reviewer
#36 lore raw

RE: [PATCH 10/16] iio: adc: max1027: Prevent single channel accesses during buffer reads

Quoted 47 lines

-----Original Message-----
From: Sa, Nuno <[email protected]>
Sent: Friday, August 20, 2021 9:21 AM
To: Miquel Raynal <[email protected]>; Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>
Cc: Thomas Petazzoni <[email protected]>; linux- [email protected]; [email protected]
Subject: RE: [PATCH 10/16] iio: adc: max1027: Prevent single channel accesses during buffer reads

[External]

Quoted 11 lines

-----Original Message-----
From: Miquel Raynal <[email protected]>
Sent: Wednesday, August 18, 2021 1:12 PM
To: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>
Cc: Thomas Petazzoni <[email protected]>; linux- [email protected]; [email protected]; Miquel Raynal <[email protected]>
Subject: [PATCH 10/16] iio: adc: max1027: Prevent single channel accesses during buffer reads

Quoted 3 lines

[External]

When hardware buffers are enabled (the cnvst pin being the

trigger),

Quoted 16 lines

one
should not mess with the device state by requesting a single channel read. Prevent it with a iio_buffer_enabled() check.

---
 drivers/iio/adc/max1027.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/iio/adc/max1027.c +0 -0
index 223c9e4abd86..83526f3d7d3a 100644
@@ -335,6 +335,8 @@ static int max1027_read_raw(struct iio_dev
 *indio_dev,
 
Quoted 10 lines

drivers/iio/adc/max1027.c static int max1027_read_raw(struct iio_dev *indio_dev, · line 339

Quoted 4 lines
drivers/iio/adc/max1027.c +2 -0
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
+		if (iio_buffer_enabled(indio_dev))
+			return -EBUSY;

I guess 'iio_device_claim_direct_mode()' would be a better option here? There's nothing preventing this check to pass and then, concurrently
someone enables the buffer...

Taking a second look, it seems that this check is already done [1]? Am I missing I missing something?

Also, I think we are returning with the 'st->lock' held...

[1]: https://elixir.bootlin.com/linux/latest/source/drivers/iio/adc/max1027.c#L247

maintainer
#37 lore raw

Re: [PATCH 10/16] iio: adc: max1027: Prevent single channel accesses during buffer reads

Quoted 56 lines · Nuno
Quoted 45 lines

-----Original Message-----
From: Sa, Nuno <[email protected]>
Sent: Friday, August 20, 2021 9:21 AM
To: Miquel Raynal <[email protected]>; Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>
Cc: Thomas Petazzoni <[email protected]>; linux- [email protected]; [email protected]
Subject: RE: [PATCH 10/16] iio: adc: max1027: Prevent single channel accesses during buffer reads

[External]

Quoted 11 lines

-----Original Message-----
From: Miquel Raynal <[email protected]>
Sent: Wednesday, August 18, 2021 1:12 PM
To: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>
Cc: Thomas Petazzoni <[email protected]>; linux- [email protected]; [email protected]; Miquel Raynal <[email protected]>
Subject: [PATCH 10/16] iio: adc: max1027: Prevent single channel accesses during buffer reads

Quoted 3 lines

[External]

When hardware buffers are enabled (the cnvst pin being the

trigger),

Quoted 16 lines

one
should not mess with the device state by requesting a single channel read. Prevent it with a iio_buffer_enabled() check.

---
 drivers/iio/adc/max1027.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/iio/adc/max1027.c +0 -0
index 223c9e4abd86..83526f3d7d3a 100644
@@ -335,6 +335,8 @@ static int max1027_read_raw(struct iio_dev
 *indio_dev,
 
Quoted 10 lines

drivers/iio/adc/max1027.c static int max1027_read_raw(struct iio_dev *indio_dev, · line 339

Quoted 4 lines
drivers/iio/adc/max1027.c +2 -0
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
+		if (iio_buffer_enabled(indio_dev))
+			return -EBUSY;  

I guess 'iio_device_claim_direct_mode()' would be a better option here? There's nothing preventing this check to pass and then, concurrently
someone enables the buffer...

Quoted 6 lines

Taking a second look, it seems that this check is already done [1]? Am I missing I missing something?

Also, I think we are returning with the 'st->lock' held...

[1]: https://elixir.bootlin.com/linux/latest/source/drivers/iio/adc/max1027.c#L247

Absolutely agree this should be done with iio_device_claim_direct_mode() to close the possible races.

I wonder why this one has been missed in all the cleanups of that stuff? Looks like a simple case, but I guess it wasn't immediately visible in the read_raw() function so no one noticed.

Jonathan

#38 lore raw

Re: [PATCH 10/16] iio: adc: max1027: Prevent single channel accesses during buffer reads

Hi Jonathan,

Jonathan Cameron <[email protected]> wrote on Mon, 30 Aug 2021 11:20:24 +0100:

Quoted 59 lines
Quoted 56 lines · Nuno
Quoted 55 lines

-----Original Message-----
From: Sa, Nuno <[email protected]>
Sent: Friday, August 20, 2021 9:21 AM
To: Miquel Raynal <[email protected]>; Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>
Cc: Thomas Petazzoni <[email protected]>; linux- [email protected]; [email protected]
Subject: RE: [PATCH 10/16] iio: adc: max1027: Prevent single channel accesses during buffer reads

[External]

Quoted 11 lines

-----Original Message-----
From: Miquel Raynal <[email protected]>
Sent: Wednesday, August 18, 2021 1:12 PM
To: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>
Cc: Thomas Petazzoni <[email protected]>; linux- [email protected]; [email protected]; Miquel Raynal <[email protected]>
Subject: [PATCH 10/16] iio: adc: max1027: Prevent single channel accesses during buffer reads

Quoted 3 lines

[External]

When hardware buffers are enabled (the cnvst pin being the

trigger),

Quoted 14 lines

one
should not mess with the device state by requesting a single channel read. Prevent it with a iio_buffer_enabled() check.

---
 drivers/iio/adc/max1027.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/iio/adc/max1027.c +0 -0
index 223c9e4abd86..83526f3d7d3a 100644

drivers/iio/adc/max1027.c static int max1027_read_raw(struct iio_dev · line 340

Quoted 6 lines
drivers/iio/adc/max1027.c +2 -0
@@ -335,6 +335,8 @@ static int max1027_read_raw(struct iio_dev
 *indio_dev,
 
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
+		if (iio_buffer_enabled(indio_dev))
+			return -EBUSY;    

I guess 'iio_device_claim_direct_mode()' would be a better option here? There's nothing preventing this check to pass and then, concurrently
someone enables the buffer...

Quoted 2 lines
Quoted 2 lines

Taking a second look, it seems that this check is already done [1]? Am I missing I missing something?

You're right, I missed that too.

Quoted 5 lines
Quoted 3 lines

Also, I think we are returning with the 'st->lock' held...

[1]: https://elixir.bootlin.com/linux/latest/source/drivers/iio/adc/max1027.c#L247

Absolutely agree this should be done with iio_device_claim_direct_mode() to close the possible races.

Didn't know this helper, nice.

Quoted 6 lines

I wonder why this one has been missed in all the cleanups of that stuff? Looks like a simple case, but I guess it wasn't immediately visible in the read_raw() function so no one noticed.

Jonathan

Thanks,
Miquèl

#39 lore raw

PATCH 11/16 iio: adc: max1027: Separate the IRQ handler from the read logic

In reply to Miquel Raynal

Create a max1027_read_scan() helper which will make clearer the future IRQ handler updates (no functional change).

---
 drivers/iio/adc/max1027.c | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)
drivers/iio/adc/max1027.c +21 -6
index 83526f3d7d3a..afc3ce69f7ea 100644
@@ -427,19 +427,18 @@ static int max1027_set_cnvst_trigger_state(struct iio_trigger *trig, bool state)
 	return 0;
 }
 
-static irqreturn_t max1027_trigger_handler(int irq, void *private)
+static int max1027_read_scan(struct iio_dev *indio_dev)
 {
-	struct iio_poll_func *pf = private;
-	struct iio_dev *indio_dev = pf->indio_dev;
 	struct max1027_state *st = iio_priv(indio_dev);
 	unsigned int scanned_chans = fls(*indio_dev->active_scan_mask);
 	u16 *buf = st->buffer;
 	unsigned int bit;
-
-	pr_debug("%s(irq=%d, private=0x%p)\n", __func__, irq, private);
+	int ret;
 
 	/* fill buffer with all channel */
-	spi_read(st->spi, st->buffer, scanned_chans * 2);
+	ret = spi_read(st->spi, st->buffer, scanned_chans * 2);
+	if (ret < 0)
+		return ret;
 
 	/* Only keep the channels selected by the user */
 	for_each_set_bit(bit, indio_dev->active_scan_mask,
@@ -451,6 +450,22 @@ static irqreturn_t max1027_trigger_handler(int irq, void *private)
 
 	iio_push_to_buffers(indio_dev, st->buffer);
 
+	return 0;
+}
+
+static irqreturn_t max1027_trigger_handler(int irq, void *private)
+{
+	struct iio_poll_func *pf = private;
+	struct iio_dev *indio_dev = pf->indio_dev;
+	int ret;
+
+	pr_debug("%s(irq=%d, private=0x%p)\n", __func__, irq, private);
+
+	ret = max1027_read_scan(indio_dev);
+	if (ret)
+		dev_err(&indio_dev->dev,
+			"Cannot read scanned values (%d)\n", ret);
+
 	iio_trigger_notify_done(indio_dev->trig);
 
 	return IRQ_HANDLED;
Signature · 1 line
2.27.0
reviewer
#40 lore raw

RE: [PATCH 11/16] iio: adc: max1027: Separate the IRQ handler from the read logic

Quoted 63 lines

-----Original Message-----
From: Miquel Raynal <[email protected]>
Sent: Wednesday, August 18, 2021 1:12 PM
To: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>
Cc: Thomas Petazzoni <[email protected]>; linux- [email protected]; [email protected]; Miquel Raynal <[email protected]>
Subject: [PATCH 11/16] iio: adc: max1027: Separate the IRQ handler from the read logic

[External]

Create a max1027_read_scan() helper which will make clearer the future IRQ
handler updates (no functional change).

---
 drivers/iio/adc/max1027.c | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)
drivers/iio/adc/max1027.c +5 -6
index 83526f3d7d3a..afc3ce69f7ea 100644
@@ -427,19 +427,18 @@ static int
 max1027_set_cnvst_trigger_state(struct iio_trigger *trig, bool state)
 	return 0;
 }
 
-static irqreturn_t max1027_trigger_handler(int irq, void *private)
+static int max1027_read_scan(struct iio_dev *indio_dev)
 {
-	struct iio_poll_func *pf = private;
-	struct iio_dev *indio_dev = pf->indio_dev;
 	struct max1027_state *st = iio_priv(indio_dev);
 	unsigned int scanned_chans = fls(*indio_dev-
 >active_scan_mask);
 	u16 *buf = st->buffer;
 	unsigned int bit;
-
-	pr_debug("%s(irq=%d, private=0x%p)\n", __func__, irq,
 private);
+	int ret;
 
 	/* fill buffer with all channel */
-	spi_read(st->spi, st->buffer, scanned_chans * 2);
+	ret = spi_read(st->spi, st->buffer, scanned_chans * 2);
+	if (ret < 0)
+		return ret;
drivers/iio/adc/max1027.c +2 -0
 	/* Only keep the channels selected by the user */
 	for_each_set_bit(bit, indio_dev->active_scan_mask,
@@ -451,6 +450,22 @@ static irqreturn_t max1027_trigger_handler(int
 irq, void *private)
 
 	iio_push_to_buffers(indio_dev, st->buffer);
 
+	return 0;
+}

drivers/iio/adc/max1027.c static irqreturn_t max1027_trigger_handler(int · line 465

Quoted 10 lines
drivers/iio/adc/max1027.c +9 -0
+
+static irqreturn_t max1027_trigger_handler(int irq, void *private)
+{
+	struct iio_poll_func *pf = private;
+	struct iio_dev *indio_dev = pf->indio_dev;
+	int ret;
+
+	pr_debug("%s(irq=%d, private=0x%p)\n", __func__, irq,
 private);
+

This should be more consistent... use 'dev_err()'. I would also argue to use the spi dev as the driver state structure holds a pointer to it...

- Nuno Sá

Quoted 10 lines
drivers/iio/adc/max1027.c +5 -0
+	ret = max1027_read_scan(indio_dev);
+	if (ret)
+		dev_err(&indio_dev->dev,
+			"Cannot read scanned values (%d)\n", ret);
+
 	iio_trigger_notify_done(indio_dev->trig);

return IRQ_HANDLED;
--
2.27.0

#41 lore raw

Re: [PATCH 11/16] iio: adc: max1027: Separate the IRQ handler from the read logic

Hi Nuno,

"Sa, Nuno" <[email protected]> wrote on Fri, 20 Aug 2021 07:23:33 +0000:

Quoted 59 lines
Quoted 59 lines

-----Original Message-----
From: Miquel Raynal <[email protected]>
Sent: Wednesday, August 18, 2021 1:12 PM
To: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>
Cc: Thomas Petazzoni <[email protected]>; linux- [email protected]; [email protected]; Miquel Raynal <[email protected]>
Subject: [PATCH 11/16] iio: adc: max1027: Separate the IRQ handler from the read logic

[External]

Create a max1027_read_scan() helper which will make clearer the future IRQ
handler updates (no functional change).

---
 drivers/iio/adc/max1027.c | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)
drivers/iio/adc/max1027.c +5 -6
index 83526f3d7d3a..afc3ce69f7ea 100644
@@ -427,19 +427,18 @@ static int
 max1027_set_cnvst_trigger_state(struct iio_trigger *trig, bool state)
 	return 0;
 }
 
-static irqreturn_t max1027_trigger_handler(int irq, void *private)
+static int max1027_read_scan(struct iio_dev *indio_dev)
 {
-	struct iio_poll_func *pf = private;
-	struct iio_dev *indio_dev = pf->indio_dev;
 	struct max1027_state *st = iio_priv(indio_dev);
 	unsigned int scanned_chans = fls(*indio_dev-  
 >active_scan_mask);  
 	u16 *buf = st->buffer;
 	unsigned int bit;
-
-	pr_debug("%s(irq=%d, private=0x%p)\n", __func__, irq,
 private);
+	int ret;
 
 	/* fill buffer with all channel */
-	spi_read(st->spi, st->buffer, scanned_chans * 2);
+	ret = spi_read(st->spi, st->buffer, scanned_chans * 2);
+	if (ret < 0)
+		return ret;
drivers/iio/adc/max1027.c +0 -0
 	/* Only keep the channels selected by the user */
 	for_each_set_bit(bit, indio_dev->active_scan_mask,
@@ -451,6 +450,22 @@ static irqreturn_t max1027_trigger_handler(int
 irq, void *private)
 
 	iio_push_to_buffers(indio_dev, st->buffer);
 
Quoted 16 lines

drivers/iio/adc/max1027.c static irqreturn_t max1027_trigger_handler(int irq, void *private) · line 463

Quoted 12 lines
drivers/iio/adc/max1027.c +11 -0
+	return 0;
+}
+
+static irqreturn_t max1027_trigger_handler(int irq, void *private)
+{
+	struct iio_poll_func *pf = private;
+	struct iio_dev *indio_dev = pf->indio_dev;
+	int ret;
+
+	pr_debug("%s(irq=%d, private=0x%p)\n", __func__, irq,
 private);
+  

This should be more consistent... use 'dev_err()'. I would also argue to use the spi dev as the driver state structure holds a pointer to it...

I honestly don't see the point of these debug messages (there is another useless pr_debug in probe). I kept it here as I am just moving code around without any changes, but if you don't like it (me neither) I'll add a simple patch dropping them.

Thanks,
Miquèl

maintainer
#42 lore raw

Re: [PATCH 11/16] iio: adc: max1027: Separate the IRQ handler from the read logic

Quoted 81 lines · On Thu, 2 Sep 2021 10:55:19 +0200 Miquel Raynal <[email protected]>…

Hi Nuno,

"Sa, Nuno" <[email protected]> wrote on Fri, 20 Aug 2021 07:23:33 +0000:

Quoted 59 lines
Quoted 59 lines

-----Original Message-----
From: Miquel Raynal <[email protected]>
Sent: Wednesday, August 18, 2021 1:12 PM
To: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>
Cc: Thomas Petazzoni <[email protected]>; linux- [email protected]; [email protected]; Miquel Raynal <[email protected]>
Subject: [PATCH 11/16] iio: adc: max1027: Separate the IRQ handler from the read logic

[External]

Create a max1027_read_scan() helper which will make clearer the future IRQ
handler updates (no functional change).

---
 drivers/iio/adc/max1027.c | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)
drivers/iio/adc/max1027.c +5 -6
index 83526f3d7d3a..afc3ce69f7ea 100644
@@ -427,19 +427,18 @@ static int
 max1027_set_cnvst_trigger_state(struct iio_trigger *trig, bool state)
 	return 0;
 }
 
-static irqreturn_t max1027_trigger_handler(int irq, void *private)
+static int max1027_read_scan(struct iio_dev *indio_dev)
 {
-	struct iio_poll_func *pf = private;
-	struct iio_dev *indio_dev = pf->indio_dev;
 	struct max1027_state *st = iio_priv(indio_dev);
 	unsigned int scanned_chans = fls(*indio_dev-    
 >active_scan_mask);    
 	u16 *buf = st->buffer;
 	unsigned int bit;
-
-	pr_debug("%s(irq=%d, private=0x%p)\n", __func__, irq,
 private);
+	int ret;
 
 	/* fill buffer with all channel */
-	spi_read(st->spi, st->buffer, scanned_chans * 2);
+	ret = spi_read(st->spi, st->buffer, scanned_chans * 2);
+	if (ret < 0)
+		return ret;
drivers/iio/adc/max1027.c +0 -0
 	/* Only keep the channels selected by the user */
 	for_each_set_bit(bit, indio_dev->active_scan_mask,
@@ -451,6 +450,22 @@ static irqreturn_t max1027_trigger_handler(int
 irq, void *private)
 
 	iio_push_to_buffers(indio_dev, st->buffer);
 
Quoted 16 lines

drivers/iio/adc/max1027.c static irqreturn_t max1027_trigger_handler(int irq, void *private) · line 463

Quoted 12 lines
drivers/iio/adc/max1027.c +11 -0
+	return 0;
+}
+
+static irqreturn_t max1027_trigger_handler(int irq, void *private)
+{
+	struct iio_poll_func *pf = private;
+	struct iio_dev *indio_dev = pf->indio_dev;
+	int ret;
+
+	pr_debug("%s(irq=%d, private=0x%p)\n", __func__, irq,
 private);
+    

This should be more consistent... use 'dev_err()'. I would also argue to use the spi dev as the driver state structure holds a pointer to it...

Quoted 4 lines

I honestly don't see the point of these debug messages (there is another useless pr_debug in probe). I kept it here as I am just moving code around without any changes, but if you don't like it (me neither) I'll add a simple patch dropping them.

Go for it :)

Quoted 3 lines

Thanks,
Miquèl

#43 lore raw

PATCH 12/16 iio: adc: max1027: Introduce an end of conversion helper

In reply to Miquel Raynal

For now this helper only waits for the maximum duration of a conversion, but it will soon be improved to properly handle the end of conversion interrupt.

---
 drivers/iio/adc/max1027.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
drivers/iio/adc/max1027.c +21 -2
index afc3ce69f7ea..2d6485591761 100644
@@ -60,6 +60,9 @@
 #define MAX1027_NAVG_32   (0x03 << 2)
 #define MAX1027_AVG_EN    BIT(4)
 
+/* Device can achieve 300ksps so we assume a 3.33us conversion delay */
+#define MAX1027_CONVERSION_UDELAY 4
+
 enum max1027_id {
 	max1027,
 	max1029,
@@ -236,6 +239,20 @@ struct max1027_state {
 	u8				reg ____cacheline_aligned;
 };
 
+static DECLARE_WAIT_QUEUE_HEAD(max1027_queue);
+
+static int max1027_wait_eoc(struct iio_dev *indio_dev)
+{
+	unsigned int conversion_time = MAX1027_CONVERSION_UDELAY;
+
+	if (indio_dev->active_scan_mask)
+		conversion_time *= hweight32(*indio_dev->active_scan_mask);
+
+	usleep_range(conversion_time, conversion_time * 2);
+
+	return 0;
+}
+
 /* Scan from 0 to the highest requested channel */
 static int max1027_configure_chans_to_scan(struct iio_dev *indio_dev)
 {
@@ -310,9 +327,11 @@ static int max1027_read_single_value(struct iio_dev *indio_dev,
 	/*
 	 * For an unknown reason, when we use the mode "10" (write
 	 * conversion register), the interrupt doesn't occur every time.
-	 * So we just wait 1 ms.
+	 * So we just wait the maximum conversion time and deliver the value.
 	 */
-	mdelay(1);
+	ret = max1027_wait_eoc(indio_dev);
+	if (ret)
+		return ret;
 
 	/* Read result */
 	ret = spi_read(st->spi, st->buffer, (chan->type == IIO_TEMP) ? 4 : 2);
Signature · 1 line
2.27.0
reviewer
#44 lore raw

RE: [PATCH 12/16] iio: adc: max1027: Introduce an end of conversion helper

Quoted 72 lines

-----Original Message-----
From: Miquel Raynal <[email protected]>
Sent: Wednesday, August 18, 2021 1:12 PM
To: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>
Cc: Thomas Petazzoni <[email protected]>; linux- [email protected]; [email protected]; Miquel Raynal <[email protected]>
Subject: [PATCH 12/16] iio: adc: max1027: Introduce an end of conversion helper

[External]

For now this helper only waits for the maximum duration of a conversion,
but it will soon be improved to properly handle the end of conversion interrupt.

---
 drivers/iio/adc/max1027.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
drivers/iio/adc/max1027.c +1 -0
index afc3ce69f7ea..2d6485591761 100644
@@ -60,6 +60,9 @@
 #define MAX1027_NAVG_32   (0x03 << 2)
 #define MAX1027_AVG_EN    BIT(4)
 
+/* Device can achieve 300ksps so we assume a 3.33us conversion

delay */

drivers/iio/adc/max1027.c +16 -0
+#define MAX1027_CONVERSION_UDELAY 4
+
 enum max1027_id {
 	max1027,
 	max1029,
@@ -236,6 +239,20 @@ struct max1027_state {
 	u8				reg ____cacheline_aligned;
 };
 
+static DECLARE_WAIT_QUEUE_HEAD(max1027_queue);
+
+static int max1027_wait_eoc(struct iio_dev *indio_dev)
+{
+	unsigned int conversion_time =
 MAX1027_CONVERSION_UDELAY;
+
+	if (indio_dev->active_scan_mask)
+		conversion_time *= hweight32(*indio_dev-
 >active_scan_mask);
+
+	usleep_range(conversion_time, conversion_time * 2);
+
+	return 0;
+}
+
 /* Scan from 0 to the highest requested channel */

static int max1027_configure_chans_to_scan(struct iio_dev
*indio_dev)
{

drivers/iio/adc/max1027.c +1 -1
@@ -310,9 +327,11 @@ static int max1027_read_single_value(struct
 iio_dev *indio_dev,
 	/*
 	 * For an unknown reason, when we use the mode "10" (write
 	 * conversion register), the interrupt doesn't occur every time.
-	 * So we just wait 1 ms.
+	 * So we just wait the maximum conversion time and deliver

the value.

drivers/iio/adc/max1027.c static int max1027_read_single_value(struct iio_dev *indio_dev, · line 334

Quoted 5 lines
drivers/iio/adc/max1027.c +3 -1
 	 */
-	mdelay(1);
+	ret = max1027_wait_eoc(indio_dev);
+	if (ret)
+		return ret;

I'm a bit confused here... Why are we looking at the active_scan_mask? Aren't we preventing this for running concurrently with buffering?

- Nuno Sá

Quoted 6 lines
drivers/iio/adc/max1027.c +0 -0
 	/* Read result */
 	ret = spi_read(st->spi, st->buffer, (chan->type == IIO_TEMP) ?

4 : 2);
--
2.27.0

#45 lore raw

Re: [PATCH 12/16] iio: adc: max1027: Introduce an end of conversion helper

Hi Nuno,

"Sa, Nuno" <[email protected]> wrote on Fri, 20 Aug 2021 07:28:17 +0000:

Quoted 70 lines
Quoted 70 lines

-----Original Message-----
From: Miquel Raynal <[email protected]>
Sent: Wednesday, August 18, 2021 1:12 PM
To: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>
Cc: Thomas Petazzoni <[email protected]>; linux- [email protected]; [email protected]; Miquel Raynal <[email protected]>
Subject: [PATCH 12/16] iio: adc: max1027: Introduce an end of conversion helper

[External]

For now this helper only waits for the maximum duration of a conversion,
but it will soon be improved to properly handle the end of conversion interrupt.

---
 drivers/iio/adc/max1027.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
drivers/iio/adc/max1027.c +1 -0
index afc3ce69f7ea..2d6485591761 100644
@@ -60,6 +60,9 @@
 #define MAX1027_NAVG_32   (0x03 << 2)
 #define MAX1027_AVG_EN    BIT(4)
 
+/* Device can achieve 300ksps so we assume a 3.33us conversion

delay */

drivers/iio/adc/max1027.c +16 -0
+#define MAX1027_CONVERSION_UDELAY 4
+
 enum max1027_id {
 	max1027,
 	max1029,
@@ -236,6 +239,20 @@ struct max1027_state {
 	u8				reg ____cacheline_aligned;
 };
 
+static DECLARE_WAIT_QUEUE_HEAD(max1027_queue);
+
+static int max1027_wait_eoc(struct iio_dev *indio_dev)
+{
+	unsigned int conversion_time =
 MAX1027_CONVERSION_UDELAY;
+
+	if (indio_dev->active_scan_mask)
+		conversion_time *= hweight32(*indio_dev-  
 >active_scan_mask);  
+
+	usleep_range(conversion_time, conversion_time * 2);
+
+	return 0;
+}
+
 /* Scan from 0 to the highest requested channel */

static int max1027_configure_chans_to_scan(struct iio_dev
*indio_dev)
{

drivers/iio/adc/max1027.c +1 -1
@@ -310,9 +327,11 @@ static int max1027_read_single_value(struct
 iio_dev *indio_dev,
 	/*
 	 * For an unknown reason, when we use the mode "10" (write
 	 * conversion register), the interrupt doesn't occur every time.
-	 * So we just wait 1 ms.
+	 * So we just wait the maximum conversion time and deliver

the value.

Quoted 8 lines

drivers/iio/adc/max1027.c static int max1027_read_single_value(struct iio_dev *indio_dev, · line 334

Quoted 5 lines
drivers/iio/adc/max1027.c +3 -1
 	 */
-	mdelay(1);
+	ret = max1027_wait_eoc(indio_dev);
+	if (ret)
+		return ret;  

I'm a bit confused here... Why are we looking at the active_scan_mask? Aren't we preventing this for running concurrently with buffering?

Sorry for the confusion, at this stage the "if (active_masks)" is useless, I moved it later in the series when we actually start to use this EOC helper for the buffered reads as well.

Thanks,
Miquèl

maintainer
#46 lore raw

Re: [PATCH 12/16] iio: adc: max1027: Introduce an end of conversion helper

In reply to Miquel Raynal
Quoted 24 lines · On Wed, 18 Aug 2021 13:11:35 +0200 Miquel Raynal <[email protected]>…

For now this helper only waits for the maximum duration of a conversion, but it will soon be improved to properly handle the end of conversion interrupt.

---
 drivers/iio/adc/max1027.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
drivers/iio/adc/max1027.c +3 -0
index afc3ce69f7ea..2d6485591761 100644
@@ -60,6 +60,9 @@
 #define MAX1027_NAVG_32   (0x03 << 2)
 #define MAX1027_AVG_EN    BIT(4)
 
+/* Device can achieve 300ksps so we assume a 3.33us conversion delay */
+#define MAX1027_CONVERSION_UDELAY 4
+
 enum max1027_id {
 	max1027,
 	max1029,

drivers/iio/adc/max1027.c struct max1027_state { · line 242

Quoted 4 lines
drivers/iio/adc/max1027.c +1 -0
@@ -236,6 +239,20 @@ struct max1027_state {
 	u8				reg ____cacheline_aligned;
 };
 
+static DECLARE_WAIT_QUEUE_HEAD(max1027_queue);

Why here? This had me confused when I couldn't find it being introduced in the later patch that uses it. More comments in that patch.

Quoted 30 lines
drivers/iio/adc/max1027.c +17 -2
+
+static int max1027_wait_eoc(struct iio_dev *indio_dev)
+{
+	unsigned int conversion_time = MAX1027_CONVERSION_UDELAY;
+
+	if (indio_dev->active_scan_mask)
+		conversion_time *= hweight32(*indio_dev->active_scan_mask);
+
+	usleep_range(conversion_time, conversion_time * 2);
+
+	return 0;
+}
+
 /* Scan from 0 to the highest requested channel */
 static int max1027_configure_chans_to_scan(struct iio_dev *indio_dev)
 {
@@ -310,9 +327,11 @@ static int max1027_read_single_value(struct iio_dev *indio_dev,
 	/*
 	 * For an unknown reason, when we use the mode "10" (write
 	 * conversion register), the interrupt doesn't occur every time.
-	 * So we just wait 1 ms.
+	 * So we just wait the maximum conversion time and deliver the value.
 	 */
-	mdelay(1);
+	ret = max1027_wait_eoc(indio_dev);
+	if (ret)
+		return ret;
 
 	/* Read result */
 	ret = spi_read(st->spi, st->buffer, (chan->type == IIO_TEMP) ? 4 : 2);
#47 lore raw

PATCH 13/16 iio: adc: max1027: Prepare re-using the EOC interrupt

In reply to Miquel Raynal

Right now the driver only has hardware trigger support, which makes use of the End Of Conversion (EOC) interrupt by:
- Enabling the external trigger
- At completion of the conversion, run a generic handler
- Push the data to the IIO subsystem by using the triggered buffer infrastructure, which may not only serve this purpose.

In fact, the interrupt will fire for more reasons than just a hardware trigger condition, so make a dedicated interrupt handler which will be enhanced by the upcoming changes to handle more situations.

---
 drivers/iio/adc/max1027.c | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)
drivers/iio/adc/max1027.c +20 -2
index 2d6485591761..8d86e77fb5db 100644
@@ -472,6 +472,24 @@ static int max1027_read_scan(struct iio_dev *indio_dev)
 	return 0;
 }
 
+static irqreturn_t max1027_eoc_irq_handler(int irq, void *private)
+{
+	struct iio_dev *indio_dev = private;
+	struct max1027_state *st = iio_priv(indio_dev);
+	int ret = 0;
+
+	if (st->cnvst_trigger) {
+		ret = max1027_read_scan(indio_dev);
+		iio_trigger_notify_done(indio_dev->trig);
+	}
+
+	if (ret)
+		dev_err(&indio_dev->dev,
+			"Cannot read scanned values (%d)\n", ret);
+
+	return IRQ_HANDLED;
+}
+
 static irqreturn_t max1027_trigger_handler(int irq, void *private)
 {
 	struct iio_poll_func *pf = private;
@@ -563,11 +581,11 @@ static int max1027_probe(struct spi_device *spi)
 		}
 
 		ret = devm_request_threaded_irq(&spi->dev, spi->irq,
-						iio_trigger_generic_data_rdy_poll,
+						max1027_eoc_irq_handler,
 						NULL,
 						IRQF_TRIGGER_FALLING,
 						spi->dev.driver->name,
-						st->trig);
+						indio_dev);
 		if (ret < 0) {
 			dev_err(&indio_dev->dev, "Failed to allocate IRQ.\n");
 			return ret;
Signature · 1 line
2.27.0
reviewer
#48 lore raw

RE: [PATCH 13/16] iio: adc: max1027: Prepare re-using the EOC interrupt

Quoted 45 lines

-----Original Message-----
From: Miquel Raynal <[email protected]>
Sent: Wednesday, August 18, 2021 1:12 PM
To: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>
Cc: Thomas Petazzoni <[email protected]>; linux- [email protected]; [email protected]; Miquel Raynal <[email protected]>
Subject: [PATCH 13/16] iio: adc: max1027: Prepare re-using the EOC interrupt

[External]

Right now the driver only has hardware trigger support, which makes use
of the End Of Conversion (EOC) interrupt by:
- Enabling the external trigger
- At completion of the conversion, run a generic handler
- Push the data to the IIO subsystem by using the triggered buffer infrastructure, which may not only serve this purpose.

In fact, the interrupt will fire for more reasons than just a hardware trigger condition, so make a dedicated interrupt handler which will be enhanced by the upcoming changes to handle more situations.

---
 drivers/iio/adc/max1027.c | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)
drivers/iio/adc/max1027.c +4 -0
index 2d6485591761..8d86e77fb5db 100644
@@ -472,6 +472,24 @@ static int max1027_read_scan(struct iio_dev
 *indio_dev)
 	return 0;
 }
 
+static irqreturn_t max1027_eoc_irq_handler(int irq, void *private)
+{
+	struct iio_dev *indio_dev = private;
+	struct max1027_state *st = iio_priv(indio_dev);

drivers/iio/adc/max1027.c static int max1027_read_scan(struct iio_dev · line 489

Quoted 10 lines
drivers/iio/adc/max1027.c +10 -0
+	int ret = 0;
+
+	if (st->cnvst_trigger) {
+		ret = max1027_read_scan(indio_dev);
+		iio_trigger_notify_done(indio_dev->trig);
+	}
+
+	if (ret)
+		dev_err(&indio_dev->dev,
+			"Cannot read scanned values (%d)\n", ret);

Huh? Shouldn't this be right after 'max1027_read_scan()'?

- Nuno Sá

Quoted 27 lines
drivers/iio/adc/max1027.c +5 -2
+	return IRQ_HANDLED;
+}
+
 static irqreturn_t max1027_trigger_handler(int irq, void *private)
 {
 	struct iio_poll_func *pf = private;
@@ -563,11 +581,11 @@ static int max1027_probe(struct spi_device
 *spi)
 		}
 
 		ret = devm_request_threaded_irq(&spi->dev, spi->irq,
-
 	iio_trigger_generic_data_rdy_poll,
+
 	max1027_eoc_irq_handler,
 						NULL,
 
 	IRQF_TRIGGER_FALLING,
 						spi->dev.driver->name,
-						st->trig);
+						indio_dev);
 		if (ret < 0) {
 			dev_err(&indio_dev->dev, "Failed to allocate

IRQ.\n");
return ret;
--
2.27.0

maintainer
#49 lore raw

Re: [PATCH 13/16] iio: adc: max1027: Prepare re-using the EOC interrupt

In reply to Miquel Raynal
Quoted 7 lines · On Wed, 18 Aug 2021 13:11:36 +0200 Miquel Raynal <[email protected]>…

Right now the driver only has hardware trigger support, which makes use of the End Of Conversion (EOC) interrupt by:
- Enabling the external trigger
- At completion of the conversion, run a generic handler
- Push the data to the IIO subsystem by using the triggered buffer infrastructure, which may not only serve this purpose.

Quoted 5 lines

In fact, the interrupt will fire for more reasons than just a hardware trigger condition, so make a dedicated interrupt handler which will be enhanced by the upcoming changes to handle more situations.

one suggestion inline.

Quoted 9 lines
---
 drivers/iio/adc/max1027.c | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)
drivers/iio/adc/max1027.c +0 -0
index 2d6485591761..8d86e77fb5db 100644

drivers/iio/adc/max1027.c static int max1027_read_scan(struct iio_dev *indio_dev) · line 481

Quoted 10 lines
drivers/iio/adc/max1027.c +7 -0
@@ -472,6 +472,24 @@ static int max1027_read_scan(struct iio_dev *indio_dev)
 	return 0;
 }
 
+static irqreturn_t max1027_eoc_irq_handler(int irq, void *private)
+{
+	struct iio_dev *indio_dev = private;
+	struct max1027_state *st = iio_priv(indio_dev);
+	int ret = 0;
+
+	if (st->cnvst_trigger) {

I missed this earlier, but it is very similar in purpose to the logic used in iio_trigger_validate_own_device. Perhaps you could reuse that rather than carrying an explicit variable to check for this?

drivers/iio/adc/max1027.c static int max1027_read_scan(struct iio_dev *indio_dev) · line 488

Quoted 7 lines
drivers/iio/adc/max1027.c +7 -0
+		ret = max1027_read_scan(indio_dev);
+		iio_trigger_notify_done(indio_dev->trig);
+	}
+
+	if (ret)
+		dev_err(&indio_dev->dev,
+			"Cannot read scanned values (%d)\n", ret);

Perhaps better to move that into the if statement. I guess this may make more sense later though!

Quoted 21 lines
drivers/iio/adc/max1027.c +6 -2
+
+	return IRQ_HANDLED;
+}
+
 static irqreturn_t max1027_trigger_handler(int irq, void *private)
 {
 	struct iio_poll_func *pf = private;
@@ -563,11 +581,11 @@ static int max1027_probe(struct spi_device *spi)
 		}
 
 		ret = devm_request_threaded_irq(&spi->dev, spi->irq,
-						iio_trigger_generic_data_rdy_poll,
+						max1027_eoc_irq_handler,
 						NULL,
 						IRQF_TRIGGER_FALLING,
 						spi->dev.driver->name,
-						st->trig);
+						indio_dev);
 		if (ret < 0) {
 			dev_err(&indio_dev->dev, "Failed to allocate IRQ.\n");
 			return ret;
maintainer
#50 lore raw

Re: [PATCH 13/16] iio: adc: max1027: Prepare re-using the EOC interrupt

In reply to Miquel Raynal
Quoted 23 lines · On Wed, 18 Aug 2021 13:11:36 +0200 Miquel Raynal <[email protected]>…

Right now the driver only has hardware trigger support, which makes use of the End Of Conversion (EOC) interrupt by:
- Enabling the external trigger
- At completion of the conversion, run a generic handler
- Push the data to the IIO subsystem by using the triggered buffer infrastructure, which may not only serve this purpose.

In fact, the interrupt will fire for more reasons than just a hardware trigger condition, so make a dedicated interrupt handler which will be enhanced by the upcoming changes to handle more situations.

---
 drivers/iio/adc/max1027.c | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)
drivers/iio/adc/max1027.c +0 -0
index 2d6485591761..8d86e77fb5db 100644
@@ -472,6 +472,24 @@ static int max1027_read_scan(struct iio_dev *indio_dev)
 	return 0;
 }

drivers/iio/adc/max1027.c static int max1027_read_scan(struct iio_dev *indio_dev) · line 483

Quoted 10 lines
drivers/iio/adc/max1027.c +9 -0
 
+static irqreturn_t max1027_eoc_irq_handler(int irq, void *private)
+{
+	struct iio_dev *indio_dev = private;
+	struct max1027_state *st = iio_priv(indio_dev);
+	int ret = 0;
+
+	if (st->cnvst_trigger) {
+		ret = max1027_read_scan(indio_dev);
+		iio_trigger_notify_done(indio_dev->trig);

Hmm. Having read on I now realise how you have decided to handle this. For the 'data ready' type interrupts you are bypassing the triggering framework entirely. in which case iio_trigger_notify_done() is irrelevant as the iio_trigger_poll() has never been called.

Normally we only do this sort of bypassing of the trigger infrastructure when there is a hw fifo involved and hence a given 'interrupt' doesn't represent a single trigger. Where possible we do expose the trigger because it can in turn be used to trigger other devices.
In this driver that is currently prevented by the validate_device() callback for the trigger so arguably the change you make here has no affect.

I wonder how hard it would be to change things to allow the validate_device() protection to be dropped. There is a slightly nasty corner case where another device is attached to this trigger but this device is not. We don't need to make that 'work' as such because it makes little sense, but we do need to ensure that things don't blow up if someone configure it like that.

Quoted 26 lines
drivers/iio/adc/max1027.c +11 -2
+	}
+
+	if (ret)
+		dev_err(&indio_dev->dev,
+			"Cannot read scanned values (%d)\n", ret);
+
+	return IRQ_HANDLED;
+}
+
 static irqreturn_t max1027_trigger_handler(int irq, void *private)
 {
 	struct iio_poll_func *pf = private;
@@ -563,11 +581,11 @@ static int max1027_probe(struct spi_device *spi)
 		}
 
 		ret = devm_request_threaded_irq(&spi->dev, spi->irq,
-						iio_trigger_generic_data_rdy_poll,
+						max1027_eoc_irq_handler,
 						NULL,
 						IRQF_TRIGGER_FALLING,
 						spi->dev.driver->name,
-						st->trig);
+						indio_dev);
 		if (ret < 0) {
 			dev_err(&indio_dev->dev, "Failed to allocate IRQ.\n");
 			return ret;
#51 lore raw

PATCH 14/16 iio: adc: max1027: Consolidate the end of conversion helper

In reply to Miquel Raynal

Now that we have a dedicated handler for End Of Conversion interrupts, let's create a second path:
- Situation 1: we are using the external hardware trigger, a conversion has been triggered and the ADC pushed the data to its FIFO, we need to retrieve the data and push it to the IIO buffers.
- Situation 2: we are not using the external hardware trigger, hence we are likely waiting in a blocked thread waiting for this interrupt to
happen: in this case we just wake up the waiting thread.

---
 drivers/iio/adc/max1027.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
drivers/iio/adc/max1027.c +17 -3
index 8d86e77fb5db..8c5995ae59f2 100644
@@ -235,6 +235,7 @@ struct max1027_state {
 	struct iio_trigger		*trig;
 	__be16				*buffer;
 	struct mutex			lock;
+	bool				data_rdy;
 	bool				cnvst_trigger;
 	u8				reg ____cacheline_aligned;
 };
@@ -243,12 +244,22 @@ static DECLARE_WAIT_QUEUE_HEAD(max1027_queue);
 
 static int max1027_wait_eoc(struct iio_dev *indio_dev)
 {
+	struct max1027_state *st = iio_priv(indio_dev);
 	unsigned int conversion_time = MAX1027_CONVERSION_UDELAY;
+	int ret;
 
-	if (indio_dev->active_scan_mask)
-		conversion_time *= hweight32(*indio_dev->active_scan_mask);
+	if (st->spi->irq) {
+		ret = wait_event_interruptible_timeout(max1027_queue,
+						       st->data_rdy, HZ / 1000);
+		st->data_rdy = false;
+		if (ret == -ERESTARTSYS)
+			return ret;
+	} else {
+		if (indio_dev->active_scan_mask)
+			conversion_time *= hweight32(*indio_dev->active_scan_mask);
 
-	usleep_range(conversion_time, conversion_time * 2);
+		usleep_range(conversion_time, conversion_time * 2);
+	}
 
 	return 0;
 }
@@ -481,6 +492,9 @@ static irqreturn_t max1027_eoc_irq_handler(int irq, void *private)
 	if (st->cnvst_trigger) {
 		ret = max1027_read_scan(indio_dev);
 		iio_trigger_notify_done(indio_dev->trig);
+	} else {
+		st->data_rdy = true;
+		wake_up(&max1027_queue);
 	}
 
 	if (ret)
Signature · 1 line
2.27.0
reviewer
#52 lore raw

RE: [PATCH 14/16] iio: adc: max1027: Consolidate the end of conversion helper

Quoted 46 lines

-----Original Message-----
From: Miquel Raynal <[email protected]>
Sent: Wednesday, August 18, 2021 1:12 PM
To: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>
Cc: Thomas Petazzoni <[email protected]>; linux- [email protected]; [email protected]; Miquel Raynal <[email protected]>
Subject: [PATCH 14/16] iio: adc: max1027: Consolidate the end of conversion helper

[External]

Now that we have a dedicated handler for End Of Conversion interrupts,
let's create a second path:
- Situation 1: we are using the external hardware trigger, a conversion has been triggered and the ADC pushed the data to its FIFO, we need
to
retrieve the data and push it to the IIO buffers.
- Situation 2: we are not using the external hardware trigger, hence we are likely waiting in a blocked thread waiting for this interrupt to
happen: in this case we just wake up the waiting thread.

---
 drivers/iio/adc/max1027.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
drivers/iio/adc/max1027.c +1 -0
index 8d86e77fb5db..8c5995ae59f2 100644
@@ -235,6 +235,7 @@ struct max1027_state {
 	struct iio_trigger		*trig;
 	__be16				*buffer;
 	struct mutex			lock;
+	bool				data_rdy;
 	bool				cnvst_trigger;
 	u8				reg ____cacheline_aligned;
 };
@@ -243,12 +244,22 @@ static
 DECLARE_WAIT_QUEUE_HEAD(max1027_queue);
 static int max1027_wait_eoc(struct iio_dev *indio_dev)

drivers/iio/adc/max1027.c static · line 253

Quoted 10 lines
drivers/iio/adc/max1027.c +3 -2
 {
+	struct max1027_state *st = iio_priv(indio_dev);
 	unsigned int conversion_time =
 MAX1027_CONVERSION_UDELAY;
+	int ret;
 
-	if (indio_dev->active_scan_mask)
-		conversion_time *= hweight32(*indio_dev-
 >active_scan_mask);
+	if (st->spi->irq) {

Wouldn't it be more clear looking at 'st-> cnvst_trigger'? since that is what we are using the wake up or nor the waiters...

- Nuno Sá

Quoted 31 lines
drivers/iio/adc/max1027.c +13 -1
+		ret =
 wait_event_interruptible_timeout(max1027_queue,
+						       st->data_rdy, HZ /
 1000);
+		st->data_rdy = false;
+		if (ret == -ERESTARTSYS)
+			return ret;
+	} else {
+		if (indio_dev->active_scan_mask)
+			conversion_time *= hweight32(*indio_dev-
 >active_scan_mask);
 
-	usleep_range(conversion_time, conversion_time * 2);
+		usleep_range(conversion_time, conversion_time * 2);
+	}
 
 	return 0;
 }
@@ -481,6 +492,9 @@ static irqreturn_t max1027_eoc_irq_handler(int
 irq, void *private)
 	if (st->cnvst_trigger) {
 		ret = max1027_read_scan(indio_dev);
 		iio_trigger_notify_done(indio_dev->trig);
+	} else {
+		st->data_rdy = true;
+		wake_up(&max1027_queue);
 	}
 

if (ret)
--
2.27.0

maintainer
#53 lore raw

Re: [PATCH 14/16] iio: adc: max1027: Consolidate the end of conversion helper

In reply to Miquel Raynal
Quoted 53 lines · On Wed, 18 Aug 2021 13:11:37 +0200 Miquel Raynal <[email protected]>…

Now that we have a dedicated handler for End Of Conversion interrupts, let's create a second path:
- Situation 1: we are using the external hardware trigger, a conversion has been triggered and the ADC pushed the data to its FIFO, we need to retrieve the data and push it to the IIO buffers.
- Situation 2: we are not using the external hardware trigger, hence we are likely waiting in a blocked thread waiting for this interrupt to
happen: in this case we just wake up the waiting thread.

---
 drivers/iio/adc/max1027.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
drivers/iio/adc/max1027.c +14 -3
index 8d86e77fb5db..8c5995ae59f2 100644
@@ -235,6 +235,7 @@ struct max1027_state {
 	struct iio_trigger		*trig;
 	__be16				*buffer;
 	struct mutex			lock;
+	bool				data_rdy;
 	bool				cnvst_trigger;
 	u8				reg ____cacheline_aligned;
 };
@@ -243,12 +244,22 @@ static DECLARE_WAIT_QUEUE_HEAD(max1027_queue);
 
 static int max1027_wait_eoc(struct iio_dev *indio_dev)
 {
+	struct max1027_state *st = iio_priv(indio_dev);
 	unsigned int conversion_time = MAX1027_CONVERSION_UDELAY;
+	int ret;
 
-	if (indio_dev->active_scan_mask)
-		conversion_time *= hweight32(*indio_dev->active_scan_mask);
+	if (st->spi->irq) {
+		ret = wait_event_interruptible_timeout(max1027_queue,
+						       st->data_rdy, HZ / 1000);
+		st->data_rdy = false;
+		if (ret == -ERESTARTSYS)
+			return ret;
+	} else {
+		if (indio_dev->active_scan_mask)
+			conversion_time *= hweight32(*indio_dev->active_scan_mask);
 
-	usleep_range(conversion_time, conversion_time * 2);
+		usleep_range(conversion_time, conversion_time * 2);
+	}
 
 	return 0;
 }

drivers/iio/adc/max1027.c static irqreturn_t max1027_eoc_irq_handler(int irq, void *private) · line 497

Quoted 6 lines
drivers/iio/adc/max1027.c +3 -0
@@ -481,6 +492,9 @@ static irqreturn_t max1027_eoc_irq_handler(int irq, void *private)
 	if (st->cnvst_trigger) {
 		ret = max1027_read_scan(indio_dev);
 		iio_trigger_notify_done(indio_dev->trig);
+	} else {
+		st->data_rdy = true;
+		wake_up(&max1027_queue);

I can't see why a queue is appropriate for this. Use a completion and have one per instance of the device. No need for the flag etc in that case as complete() means we have had an interrupt.

Quoted 3 lines
drivers/iio/adc/max1027.c +0 -0
 	}
 
 	if (ret)
reviewer
#54 lore raw

RE: [PATCH 14/16] iio: adc: max1027: Consolidate the end of conversion helper

Quoted 79 lines

-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Monday, August 30, 2021 12:37 PM
To: Miquel Raynal <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>; Thomas Petazzoni <[email protected]>; [email protected]; linux- [email protected]
Subject: Re: [PATCH 14/16] iio: adc: max1027: Consolidate the end of conversion helper

Quoted 1 line · On Wed, 18 Aug 2021 13:11:37 +0200 Miquel Raynal <[email protected]>…

Now that we have a dedicated handler for End Of Conversion

interrupts,

Quoted 2 lines

let's create a second path:
- Situation 1: we are using the external hardware trigger, a

conversion

Quoted 1 line

has been triggered and the ADC pushed the data to its FIFO, we

need to

Quoted 2 lines

retrieve the data and push it to the IIO buffers.
- Situation 2: we are not using the external hardware trigger, hence

we

drivers/iio/adc/max1027.c static

Quoted 21 lines

are likely waiting in a blocked thread waiting for this interrupt to
happen: in this case we just wake up the waiting thread.

---
 drivers/iio/adc/max1027.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
drivers/iio/adc/max1027.c +1 -0
index 8d86e77fb5db..8c5995ae59f2 100644
@@ -235,6 +235,7 @@ struct max1027_state {
 	struct iio_trigger		*trig;
 	__be16				*buffer;
 	struct mutex			lock;
+	bool				data_rdy;
 	bool				cnvst_trigger;
 	u8				reg ____cacheline_aligned;
 };
@@ -243,12 +244,22 @@ static

DECLARE_WAIT_QUEUE_HEAD(max1027_queue);

drivers/iio/adc/max1027.c static DECLARE_WAIT_QUEUE_HEAD(max1027_queue);

Quoted 5 lines
drivers/iio/adc/max1027.c +1 -0
 static int max1027_wait_eoc(struct iio_dev *indio_dev)
 {
+	struct max1027_state *st = iio_priv(indio_dev);
 	unsigned int conversion_time =

MAX1027_CONVERSION_UDELAY;

drivers/iio/adc/max1027.c static DECLARE_WAIT_QUEUE_HEAD(max1027_queue);

Quoted 7 lines
drivers/iio/adc/max1027.c +3 -2
+	int ret;
 
-	if (indio_dev->active_scan_mask)
-		conversion_time *= hweight32(*indio_dev-
 active_scan_mask);
+	if (st->spi->irq) {
+		ret =

wait_event_interruptible_timeout(max1027_queue,

drivers/iio/adc/max1027.c static · line 253

Quoted 1 line
drivers/iio/adc/max1027.c +1 -0
+						       st->data_rdy, HZ /

1000);

drivers/iio/adc/max1027.c static irqreturn_t

Quoted 15 lines
drivers/iio/adc/max1027.c +8 -1
+		st->data_rdy = false;
+		if (ret == -ERESTARTSYS)
+			return ret;
+	} else {
+		if (indio_dev->active_scan_mask)
+			conversion_time *= hweight32(*indio_dev-
 active_scan_mask);
 
-	usleep_range(conversion_time, conversion_time * 2);
+		usleep_range(conversion_time, conversion_time * 2);
+	}
 
 	return 0;

}

drivers/iio/adc/max1027.c +0 -0
@@ -481,6 +492,9 @@ static irqreturn_t

max1027_eoc_irq_handler(int irq, void *private)

Quoted 12 lines

drivers/iio/adc/max1027.c static irqreturn_t max1027_eoc_irq_handler(int irq, void *private) · line 497

Quoted 6 lines
drivers/iio/adc/max1027.c +3 -0
 	if (st->cnvst_trigger) {
 		ret = max1027_read_scan(indio_dev);
 		iio_trigger_notify_done(indio_dev->trig);
+	} else {
+		st->data_rdy = true;
+		wake_up(&max1027_queue);

I can't see why a queue is appropriate for this. Use a completion and have
one per instance of the device. No need for the flag etc in that case as complete() means we have had an interrupt.

In the case that 'st-> cnvst_trigger' is not set but the spi IRQ
is present, we will wait until we get 'wake_up()' called from here. I wonder if that is a good idea as the device own trigger is not being used. FWIW, I think this sync logic is a bit confusing... I would still use the normal trigger infrastructure ('iio_trigger_generic_data_rdy_poll()') and use the 'cnvst_trigger' flag in the trigger handler to manually start conversions + wait till eoc. But I might be missing something though.

Regarding this handler, I just realized that this is the hard IRQ handler which might end up calling 'max1027_read_scan()' which in turn calls 'spi_read()'. Am I missing something here?

- Nuno Sá

maintainer
#55 lore raw

Re: [PATCH 14/16] iio: adc: max1027: Consolidate the end of conversion helper

Quoted 98 lines · Nuno
Quoted 77 lines

-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Monday, August 30, 2021 12:37 PM
To: Miquel Raynal <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>; Thomas Petazzoni <[email protected]>; [email protected]; linux- [email protected]
Subject: Re: [PATCH 14/16] iio: adc: max1027: Consolidate the end of conversion helper

Quoted 1 line · On Wed, 18 Aug 2021 13:11:37 +0200 Miquel Raynal <[email protected]>…

Now that we have a dedicated handler for End Of Conversion

interrupts,

Quoted 2 lines

let's create a second path:
- Situation 1: we are using the external hardware trigger, a

conversion

Quoted 1 line

has been triggered and the ADC pushed the data to its FIFO, we

need to

Quoted 2 lines

retrieve the data and push it to the IIO buffers.
- Situation 2: we are not using the external hardware trigger, hence

we

drivers/iio/adc/max1027.c static

Quoted 21 lines

are likely waiting in a blocked thread waiting for this interrupt to
happen: in this case we just wake up the waiting thread.

---
 drivers/iio/adc/max1027.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
drivers/iio/adc/max1027.c +1 -0
index 8d86e77fb5db..8c5995ae59f2 100644
@@ -235,6 +235,7 @@ struct max1027_state {
 	struct iio_trigger		*trig;
 	__be16				*buffer;
 	struct mutex			lock;
+	bool				data_rdy;
 	bool				cnvst_trigger;
 	u8				reg ____cacheline_aligned;
 };
@@ -243,12 +244,22 @@ static  

DECLARE_WAIT_QUEUE_HEAD(max1027_queue);

drivers/iio/adc/max1027.c static DECLARE_WAIT_QUEUE_HEAD(max1027_queue);

Quoted 5 lines
drivers/iio/adc/max1027.c +1 -0
 static int max1027_wait_eoc(struct iio_dev *indio_dev)
 {
+	struct max1027_state *st = iio_priv(indio_dev);
 	unsigned int conversion_time =  

MAX1027_CONVERSION_UDELAY;

drivers/iio/adc/max1027.c static DECLARE_WAIT_QUEUE_HEAD(max1027_queue);

Quoted 7 lines
drivers/iio/adc/max1027.c +3 -2
+	int ret;
 
-	if (indio_dev->active_scan_mask)
-		conversion_time *= hweight32(*indio_dev-
 active_scan_mask);
+	if (st->spi->irq) {
+		ret =  

wait_event_interruptible_timeout(max1027_queue,

drivers/iio/adc/max1027.c static · line 253

Quoted 1 line
drivers/iio/adc/max1027.c +1 -0
+						       st->data_rdy, HZ /  

1000);

drivers/iio/adc/max1027.c static irqreturn_t

Quoted 15 lines
drivers/iio/adc/max1027.c +8 -1
+		st->data_rdy = false;
+		if (ret == -ERESTARTSYS)
+			return ret;
+	} else {
+		if (indio_dev->active_scan_mask)
+			conversion_time *= hweight32(*indio_dev-
 active_scan_mask);
 
-	usleep_range(conversion_time, conversion_time * 2);
+		usleep_range(conversion_time, conversion_time * 2);
+	}
 
 	return 0;

}

drivers/iio/adc/max1027.c +0 -0
@@ -481,6 +492,9 @@ static irqreturn_t  

max1027_eoc_irq_handler(int irq, void *private)

Quoted 12 lines

drivers/iio/adc/max1027.c static irqreturn_t max1027_eoc_irq_handler(int irq, void *private) · line 497

Quoted 6 lines
drivers/iio/adc/max1027.c +3 -0
 	if (st->cnvst_trigger) {
 		ret = max1027_read_scan(indio_dev);
 		iio_trigger_notify_done(indio_dev->trig);
+	} else {
+		st->data_rdy = true;
+		wake_up(&max1027_queue);  

I can't see why a queue is appropriate for this. Use a completion and have
one per instance of the device. No need for the flag etc in that case as complete() means we have had an interrupt.

In the case that 'st-> cnvst_trigger' is not set but the spi IRQ
is present, we will wait until we get 'wake_up()' called from here. I wonder if that is a good idea as the device own trigger is not being used. FWIW, I think this sync logic is a bit confusing... I would still use the normal trigger infrastructure ('iio_trigger_generic_data_rdy_poll()') and use the 'cnvst_trigger' flag in the trigger handler to manually start conversions + wait till eoc. But I might be missing something though.

Quoted 3 lines

Regarding this handler, I just realized that this is the hard IRQ handler which might end up calling 'max1027_read_scan()' which in turn calls 'spi_read()'. Am I missing something here?

Good point. This will need to be the threaded handler if done like this.

J

Quoted 2 lines

- Nuno Sá

#56 lore raw

Re: [PATCH 14/16] iio: adc: max1027: Consolidate the end of conversion helper

In reply to Sa, Nuno

Hi Nuno,

"Sa, Nuno" <[email protected]> wrote on Mon, 30 Aug 2021 12:44:48 +0000:

Quoted 90 lines
Quoted 77 lines

-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Monday, August 30, 2021 12:37 PM
To: Miquel Raynal <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>; Thomas Petazzoni <[email protected]>; [email protected]; linux- [email protected]
Subject: Re: [PATCH 14/16] iio: adc: max1027: Consolidate the end of conversion helper

Quoted 1 line · On Wed, 18 Aug 2021 13:11:37 +0200 Miquel Raynal <[email protected]>…

Now that we have a dedicated handler for End Of Conversion

interrupts,

Quoted 2 lines

let's create a second path:
- Situation 1: we are using the external hardware trigger, a

conversion

Quoted 1 line

has been triggered and the ADC pushed the data to its FIFO, we

need to

Quoted 2 lines

retrieve the data and push it to the IIO buffers.
- Situation 2: we are not using the external hardware trigger, hence

we

drivers/iio/adc/max1027.c static

Quoted 21 lines

are likely waiting in a blocked thread waiting for this interrupt to
happen: in this case we just wake up the waiting thread.

---
 drivers/iio/adc/max1027.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
drivers/iio/adc/max1027.c +1 -0
index 8d86e77fb5db..8c5995ae59f2 100644
@@ -235,6 +235,7 @@ struct max1027_state {
 	struct iio_trigger		*trig;
 	__be16				*buffer;
 	struct mutex			lock;
+	bool				data_rdy;
 	bool				cnvst_trigger;
 	u8				reg ____cacheline_aligned;
 };
@@ -243,12 +244,22 @@ static  

DECLARE_WAIT_QUEUE_HEAD(max1027_queue);

drivers/iio/adc/max1027.c static DECLARE_WAIT_QUEUE_HEAD(max1027_queue);

Quoted 5 lines
drivers/iio/adc/max1027.c +1 -0
 static int max1027_wait_eoc(struct iio_dev *indio_dev)
 {
+	struct max1027_state *st = iio_priv(indio_dev);
 	unsigned int conversion_time =  

MAX1027_CONVERSION_UDELAY;

drivers/iio/adc/max1027.c static DECLARE_WAIT_QUEUE_HEAD(max1027_queue);

Quoted 7 lines
drivers/iio/adc/max1027.c +3 -2
+	int ret;
 
-	if (indio_dev->active_scan_mask)
-		conversion_time *= hweight32(*indio_dev-
 active_scan_mask);
+	if (st->spi->irq) {
+		ret =  

wait_event_interruptible_timeout(max1027_queue,

drivers/iio/adc/max1027.c static · line 253

Quoted 1 line
drivers/iio/adc/max1027.c +1 -0
+						       st->data_rdy, HZ /  

1000);

drivers/iio/adc/max1027.c static irqreturn_t

Quoted 15 lines
drivers/iio/adc/max1027.c +8 -1
+		st->data_rdy = false;
+		if (ret == -ERESTARTSYS)
+			return ret;
+	} else {
+		if (indio_dev->active_scan_mask)
+			conversion_time *= hweight32(*indio_dev-
 active_scan_mask);
 
-	usleep_range(conversion_time, conversion_time * 2);
+		usleep_range(conversion_time, conversion_time * 2);
+	}
 
 	return 0;

}

drivers/iio/adc/max1027.c +0 -0
@@ -481,6 +492,9 @@ static irqreturn_t  

max1027_eoc_irq_handler(int irq, void *private)

Quoted 12 lines

drivers/iio/adc/max1027.c static irqreturn_t max1027_eoc_irq_handler(int irq, void *private) · line 497

Quoted 6 lines
drivers/iio/adc/max1027.c +3 -0
 	if (st->cnvst_trigger) {
 		ret = max1027_read_scan(indio_dev);
 		iio_trigger_notify_done(indio_dev->trig);
+	} else {
+		st->data_rdy = true;
+		wake_up(&max1027_queue);  

I can't see why a queue is appropriate for this. Use a completion and have
one per instance of the device. No need for the flag etc in that case as complete() means we have had an interrupt.

Quoted 7 lines

In the case that 'st-> cnvst_trigger' is not set but the spi IRQ
is present, we will wait until we get 'wake_up()' called from here. I wonder if that is a good idea as the device own trigger is not being used. FWIW, I think this sync logic is a bit confusing... I would still use the normal trigger infrastructure ('iio_trigger_generic_data_rdy_poll()') and use the 'cnvst_trigger' flag in the trigger handler to manually start conversions + wait till eoc. But I might be missing something though.

I implemented it your way, but I think I found a situation that was not fully handled (the 3rd), which makes the handler very complicated as we need to handle all the following cases:
1/ no trigger, irq enabled -> single read EOC interrupt
2/ external trigger, no irq -> handle the whole conversion process 3/ external trigger, irq enabled -> handle the whole conversion process
but also have a dedicated condition to handle the EOC interrupt properly (fortunately this is a threaded handler that can be preempted): we need to wait from the handler itself that the handler gets called again: the first time it is executed as "pollfunc", the second time as "EOC interrupt". In the second instance, call complete() in order to deliver the first running instance of the handler and continue until the reading part.
4/ cnvst trigger, irq enabled -> only reads the data.
5/ cnvst trigger, irq disabled -> not possible.

I added a lot of comments to make it clearer.

Quoted 3 lines

Regarding this handler, I just realized that this is the hard IRQ handler which might end up calling 'max1027_read_scan()' which in turn calls 'spi_read()'. Am I missing something here?

I renamed it to make it clear, but this is already a threaded handler.

Thanks,
Miquèl

reviewer
#57 lore raw

RE: [PATCH 14/16] iio: adc: max1027: Consolidate the end of conversion helper

Hi Miquel,

Quoted 143 lines

From: Miquel Raynal <[email protected]>
Sent: Thursday, September 2, 2021 5:13 PM
To: Sa, Nuno <[email protected]>
Cc: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>; Thomas Petazzoni <[email protected]>; [email protected]; linux- [email protected]
Subject: Re: [PATCH 14/16] iio: adc: max1027: Consolidate the end of conversion helper

Hi Nuno,

"Sa, Nuno" <[email protected]> wrote on Mon, 30 Aug 2021 12:44:48
+0000:

Quoted 6 lines
Quoted 6 lines

-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Monday, August 30, 2021 12:37 PM
To: Miquel Raynal <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>; Thomas Petazzoni <[email protected]>; [email protected];

linux-

Quoted 2 lines
Quoted 2 lines

[email protected]
Subject: Re: [PATCH 14/16] iio: adc: max1027: Consolidate the end

of

Quoted 6 lines
Quoted 6 lines

conversion helper

Quoted 1 line · On Wed, 18 Aug 2021 13:11:37 +0200 Miquel Raynal <[email protected]>…

Now that we have a dedicated handler for End Of Conversion

Quoted 8 lines
Quoted 8 lines

interrupts,

Quoted 2 lines

let's create a second path:
- Situation 1: we are using the external hardware trigger, a

conversion

Quoted 1 line

has been triggered and the ADC pushed the data to its FIFO, we

need to

Quoted 2 lines

retrieve the data and push it to the IIO buffers.
- Situation 2: we are not using the external hardware trigger,

hence

Quoted 10 lines
Quoted 10 lines

we

Review

Quoted 9 lines

are likely waiting in a blocked thread waiting for this interrupt to
happen: in this case we just wake up the waiting thread.

---
 drivers/iio/adc/max1027.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
(quoted diff) +0 -0

b/drivers/iio/adc/max1027.c

Quoted 45 lines
Quoted 45 lines

drivers/iio/adc/max1027.c static

Quoted 12 lines

index 8d86e77fb5db..8c5995ae59f2 100644

drivers/iio/adc/max1027.c +1 -0
@@ -235,6 +235,7 @@ struct max1027_state {
 	struct iio_trigger		*trig;
 	__be16				*buffer;
 	struct mutex			lock;
+	bool				data_rdy;
 	bool				cnvst_trigger;
 	u8				reg ____cacheline_aligned;
 };
@@ -243,12 +244,22 @@ static

DECLARE_WAIT_QUEUE_HEAD(max1027_queue);

drivers/iio/adc/max1027.c static DECLARE_WAIT_QUEUE_HEAD(max1027_queue);

Quoted 5 lines
drivers/iio/adc/max1027.c +1 -0
 static int max1027_wait_eoc(struct iio_dev *indio_dev)
 {
+	struct max1027_state *st = iio_priv(indio_dev);
 	unsigned int conversion_time =

MAX1027_CONVERSION_UDELAY;

drivers/iio/adc/max1027.c static DECLARE_WAIT_QUEUE_HEAD(max1027_queue);

Quoted 7 lines
drivers/iio/adc/max1027.c +3 -2
+	int ret;
 
-	if (indio_dev->active_scan_mask)
-		conversion_time *= hweight32(*indio_dev-
 active_scan_mask);
+	if (st->spi->irq) {
+		ret =

wait_event_interruptible_timeout(max1027_queue,

drivers/iio/adc/max1027.c static · line 253

Quoted 1 line
drivers/iio/adc/max1027.c +1 -0
+						       st->data_rdy, HZ /

1000);

drivers/iio/adc/max1027.c static irqreturn_t

Quoted 15 lines
drivers/iio/adc/max1027.c +8 -1
+		st->data_rdy = false;
+		if (ret == -ERESTARTSYS)
+			return ret;
+	} else {
+		if (indio_dev->active_scan_mask)
+			conversion_time *= hweight32(*indio_dev-
 active_scan_mask);
 
-	usleep_range(conversion_time, conversion_time * 2);
+		usleep_range(conversion_time, conversion_time * 2);
+	}
 
 	return 0;

}

drivers/iio/adc/max1027.c +0 -0
@@ -481,6 +492,9 @@ static irqreturn_t

max1027_eoc_irq_handler(int irq, void *private)

Quoted 8 lines
Quoted 8 lines

drivers/iio/adc/max1027.c static irqreturn_t max1027_eoc_irq_handler(int irq, void *private) · line 497

Quoted 6 lines
drivers/iio/adc/max1027.c +3 -0
 	if (st->cnvst_trigger) {
 		ret = max1027_read_scan(indio_dev);
 		iio_trigger_notify_done(indio_dev->trig);
+	} else {
+		st->data_rdy = true;
+		wake_up(&max1027_queue);

I can't see why a queue is appropriate for this. Use a completion

and

Quoted 2 lines
Quoted 2 lines

have
one per instance of the device. No need for the flag etc in that

case as

Quoted 5 lines
Quoted 2 lines

complete() means we have had an interrupt.

In the case that 'st-> cnvst_trigger' is not set but the spi IRQ is present, we will wait until we get 'wake_up()' called from here. I

wonder if

Quoted 1 line

that is a good idea as the device own trigger is not being used. FWIW,

I think this

Quoted 1 line

sync logic is a bit confusing... I would still use the normal trigger

infrastructure

Quoted 1 line

('iio_trigger_generic_data_rdy_poll()') and use the 'cnvst_trigger'

flag in the

Quoted 1 line

trigger handler to manually start conversions + wait till eoc. But I

might be missing

Quoted 1 line

something though.

I implemented it your way, but I think I found a situation that was not fully handled (the 3rd), which makes the handler very complicated as we need to handle all the following cases:
1/ no trigger, irq enabled -> single read EOC interrupt
2/ external trigger, no irq -> handle the whole conversion process 3/ external trigger, irq enabled -> handle the whole conversion process
but also have a dedicated condition to handle the EOC interrupt properly (fortunately this is a threaded handler that can be preempted): we need to wait from the handler itself that the handler gets called again: the first time it is executed as "pollfunc", the second time as "EOC interrupt". In the second instance, call complete() in order to deliver the first running instance of the handler and continue until the reading part.
4/ cnvst trigger, irq enabled -> only reads the data.
5/ cnvst trigger, irq disabled -> not possible.

I added a lot of comments to make it clearer.

Quoted 8 lines
Quoted 1 line

Regarding this handler, I just realized that this is the hard IRQ handler

which

Quoted 1 line

might end up calling 'max1027_read_scan()' which in turn calls

'spi_read()'. Am I

Quoted 1 line

missing something here?

I renamed it to make it clear, but this is already a threaded handler.

Hmm, I think I get what you're trying to do.... FWIW, I think you're just going into a lot of trouble here for scenario 3 (I assume external trigger is something else other the device own one). IMO, I would just assume that if we are using an external trigger we have to wait (sleep) for the end of conversion (i.e, I would not care about the IRQ in this case). It would make things much more simpler and I guess it should be expected that if some user is deliberately not using the device own trigger, will have to wait more for scans.

I cannot also see a reason why someone would want to use some external trigger if the device one is available... Does it really make sense?

- Nuno Sá

#58 lore raw

Re: [PATCH 14/16] iio: adc: max1027: Consolidate the end of conversion helper

Hi Nuno,

[email protected] wrote on Fri, 3 Sep 2021 14:28:52 +0000:

Quoted 154 lines

Hi Miquel,

Quoted 143 lines

From: Miquel Raynal <[email protected]>
Sent: Thursday, September 2, 2021 5:13 PM
To: Sa, Nuno <[email protected]>
Cc: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>; Thomas Petazzoni <[email protected]>; [email protected]; linux- [email protected]
Subject: Re: [PATCH 14/16] iio: adc: max1027: Consolidate the end of conversion helper

Hi Nuno,

"Sa, Nuno" <[email protected]> wrote on Mon, 30 Aug 2021 12:44:48
+0000:

Quoted 6 lines
Quoted 6 lines

-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Monday, August 30, 2021 12:37 PM
To: Miquel Raynal <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>; Thomas Petazzoni <[email protected]>; [email protected];

linux-

Quoted 2 lines
Quoted 2 lines

[email protected]
Subject: Re: [PATCH 14/16] iio: adc: max1027: Consolidate the end

of

Quoted 6 lines
Quoted 6 lines

conversion helper

Quoted 1 line · On Wed, 18 Aug 2021 13:11:37 +0200 Miquel Raynal <[email protected]>…

Now that we have a dedicated handler for End Of Conversion

Quoted 8 lines
Quoted 8 lines

interrupts,

Quoted 2 lines

let's create a second path:
- Situation 1: we are using the external hardware trigger, a

conversion

Quoted 1 line

has been triggered and the ADC pushed the data to its FIFO, we

need to

Quoted 2 lines

retrieve the data and push it to the IIO buffers.
- Situation 2: we are not using the external hardware trigger,

hence

Quoted 10 lines
Quoted 10 lines

we

Review

Quoted 9 lines

are likely waiting in a blocked thread waiting for this interrupt to
happen: in this case we just wake up the waiting thread.

---
 drivers/iio/adc/max1027.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
(quoted diff) +0 -0

b/drivers/iio/adc/max1027.c

Quoted 45 lines
Quoted 45 lines

drivers/iio/adc/max1027.c static

Quoted 12 lines

index 8d86e77fb5db..8c5995ae59f2 100644

drivers/iio/adc/max1027.c +1 -0
@@ -235,6 +235,7 @@ struct max1027_state {
 	struct iio_trigger		*trig;
 	__be16				*buffer;
 	struct mutex			lock;
+	bool				data_rdy;
 	bool				cnvst_trigger;
 	u8				reg ____cacheline_aligned;
 };
@@ -243,12 +244,22 @@ static  

DECLARE_WAIT_QUEUE_HEAD(max1027_queue);

drivers/iio/adc/max1027.c static DECLARE_WAIT_QUEUE_HEAD(max1027_queue);

Quoted 5 lines
drivers/iio/adc/max1027.c +1 -0
 static int max1027_wait_eoc(struct iio_dev *indio_dev)
 {
+	struct max1027_state *st = iio_priv(indio_dev);
 	unsigned int conversion_time =  

MAX1027_CONVERSION_UDELAY;

drivers/iio/adc/max1027.c static DECLARE_WAIT_QUEUE_HEAD(max1027_queue);

Quoted 7 lines
drivers/iio/adc/max1027.c +3 -2
+	int ret;
 
-	if (indio_dev->active_scan_mask)
-		conversion_time *= hweight32(*indio_dev-
 active_scan_mask);
+	if (st->spi->irq) {
+		ret =  

wait_event_interruptible_timeout(max1027_queue,

drivers/iio/adc/max1027.c static · line 253

Quoted 1 line
drivers/iio/adc/max1027.c +1 -0
+						       st->data_rdy, HZ /  

1000);

drivers/iio/adc/max1027.c static irqreturn_t

Quoted 15 lines
drivers/iio/adc/max1027.c +8 -1
+		st->data_rdy = false;
+		if (ret == -ERESTARTSYS)
+			return ret;
+	} else {
+		if (indio_dev->active_scan_mask)
+			conversion_time *= hweight32(*indio_dev-
 active_scan_mask);
 
-	usleep_range(conversion_time, conversion_time * 2);
+		usleep_range(conversion_time, conversion_time * 2);
+	}
 
 	return 0;

}

drivers/iio/adc/max1027.c +0 -0
@@ -481,6 +492,9 @@ static irqreturn_t  

max1027_eoc_irq_handler(int irq, void *private)

Quoted 8 lines
Quoted 8 lines

drivers/iio/adc/max1027.c static irqreturn_t max1027_eoc_irq_handler(int irq, void *private) · line 497

Quoted 6 lines
drivers/iio/adc/max1027.c +3 -0
 	if (st->cnvst_trigger) {
 		ret = max1027_read_scan(indio_dev);
 		iio_trigger_notify_done(indio_dev->trig);
+	} else {
+		st->data_rdy = true;
+		wake_up(&max1027_queue);  

I can't see why a queue is appropriate for this. Use a completion

and

Quoted 2 lines
Quoted 2 lines

have
one per instance of the device. No need for the flag etc in that

case as

Quoted 5 lines
Quoted 2 lines

complete() means we have had an interrupt.

In the case that 'st-> cnvst_trigger' is not set but the spi IRQ is present, we will wait until we get 'wake_up()' called from here. I

wonder if

Quoted 1 line

that is a good idea as the device own trigger is not being used. FWIW,

I think this

Quoted 1 line

sync logic is a bit confusing... I would still use the normal trigger

infrastructure

Quoted 1 line

('iio_trigger_generic_data_rdy_poll()') and use the 'cnvst_trigger'

flag in the

Quoted 1 line

trigger handler to manually start conversions + wait till eoc. But I

might be missing

Quoted 1 line

something though.

I implemented it your way, but I think I found a situation that was not fully handled (the 3rd), which makes the handler very complicated as we need to handle all the following cases:
1/ no trigger, irq enabled -> single read EOC interrupt
2/ external trigger, no irq -> handle the whole conversion process 3/ external trigger, irq enabled -> handle the whole conversion process
but also have a dedicated condition to handle the EOC interrupt properly (fortunately this is a threaded handler that can be preempted): we need to wait from the handler itself that the handler gets called again: the first time it is executed as "pollfunc", the second time as "EOC interrupt". In the second instance, call complete() in order to deliver the first running instance of the handler and continue until the reading part.
4/ cnvst trigger, irq enabled -> only reads the data.
5/ cnvst trigger, irq disabled -> not possible.

I added a lot of comments to make it clearer.

Quoted 8 lines
Quoted 1 line

Regarding this handler, I just realized that this is the hard IRQ handler

which

Quoted 1 line

might end up calling 'max1027_read_scan()' which in turn calls

'spi_read()'. Am I

Quoted 1 line

missing something here?

I renamed it to make it clear, but this is already a threaded handler.

Quoted 7 lines

Hmm, I think I get what you're trying to do.... FWIW, I think you're just going into a lot of trouble here for scenario 3 (I assume external trigger is something else other the device own one). IMO, I would just assume that if we are using an external trigger we have to wait (sleep) for the end of conversion (i.e, I would not care about the IRQ in this case). It would make things much more simpler and I guess it should be expected that if some user is deliberately not using the device own trigger, will have to wait more for scans.

I thought about that, but this means playing with enabling/disabling IRQs, which in the end I fear could be almost as verbose :/

Can you look at the new implementation that I proposed and give me your feedback on it?
[PATCH v2 15/16] iio: adc: max1027: Add support for external triggers

Quoted 2 lines

I cannot also see a reason why someone would want to use some external trigger if the device one is available... Does it really make sense?

I would say that "genericity" is a good enough reason for that, but in practice I agree with you.

Thanks,
Miquèl

maintainer
#59 lore raw

Re: [PATCH 14/16] iio: adc: max1027: Consolidate the end of conversion helper

Quoted 173 lines · On Fri, 3 Sep 2021 16:46:50 +0200 Miquel Raynal <[email protected]>…

Hi Nuno,

[email protected] wrote on Fri, 3 Sep 2021 14:28:52 +0000:

Quoted 154 lines

Hi Miquel,

Quoted 143 lines

From: Miquel Raynal <[email protected]>
Sent: Thursday, September 2, 2021 5:13 PM
To: Sa, Nuno <[email protected]>
Cc: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>; Thomas Petazzoni <[email protected]>; [email protected]; linux- [email protected]
Subject: Re: [PATCH 14/16] iio: adc: max1027: Consolidate the end of conversion helper

Hi Nuno,

"Sa, Nuno" <[email protected]> wrote on Mon, 30 Aug 2021 12:44:48
+0000:

Quoted 6 lines
Quoted 6 lines

-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Monday, August 30, 2021 12:37 PM
To: Miquel Raynal <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>; Thomas Petazzoni <[email protected]>; [email protected];

linux-

Quoted 2 lines
Quoted 2 lines

[email protected]
Subject: Re: [PATCH 14/16] iio: adc: max1027: Consolidate the end

of

Quoted 6 lines
Quoted 6 lines

conversion helper

Quoted 1 line · On Wed, 18 Aug 2021 13:11:37 +0200 Miquel Raynal <[email protected]>…

Now that we have a dedicated handler for End Of Conversion

Quoted 8 lines
Quoted 8 lines

interrupts,

Quoted 2 lines

let's create a second path:
- Situation 1: we are using the external hardware trigger, a

conversion

Quoted 1 line

has been triggered and the ADC pushed the data to its FIFO, we

need to

Quoted 2 lines

retrieve the data and push it to the IIO buffers.
- Situation 2: we are not using the external hardware trigger,

hence

Quoted 10 lines
Quoted 10 lines

we

Review

Quoted 9 lines

are likely waiting in a blocked thread waiting for this interrupt to
happen: in this case we just wake up the waiting thread.

---
 drivers/iio/adc/max1027.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
(quoted diff) +0 -0

b/drivers/iio/adc/max1027.c

Quoted 45 lines
Quoted 45 lines

drivers/iio/adc/max1027.c static

Quoted 12 lines

index 8d86e77fb5db..8c5995ae59f2 100644

drivers/iio/adc/max1027.c +1 -0
@@ -235,6 +235,7 @@ struct max1027_state {
 	struct iio_trigger		*trig;
 	__be16				*buffer;
 	struct mutex			lock;
+	bool				data_rdy;
 	bool				cnvst_trigger;
 	u8				reg ____cacheline_aligned;
 };
@@ -243,12 +244,22 @@ static    

DECLARE_WAIT_QUEUE_HEAD(max1027_queue);

drivers/iio/adc/max1027.c static DECLARE_WAIT_QUEUE_HEAD(max1027_queue);

Quoted 5 lines
drivers/iio/adc/max1027.c +1 -0
 static int max1027_wait_eoc(struct iio_dev *indio_dev)
 {
+	struct max1027_state *st = iio_priv(indio_dev);
 	unsigned int conversion_time =    

MAX1027_CONVERSION_UDELAY;

drivers/iio/adc/max1027.c static DECLARE_WAIT_QUEUE_HEAD(max1027_queue);

Quoted 7 lines
drivers/iio/adc/max1027.c +3 -2
+	int ret;
 
-	if (indio_dev->active_scan_mask)
-		conversion_time *= hweight32(*indio_dev-
 active_scan_mask);
+	if (st->spi->irq) {
+		ret =    

wait_event_interruptible_timeout(max1027_queue,

drivers/iio/adc/max1027.c static · line 253

Quoted 1 line
drivers/iio/adc/max1027.c +1 -0
+						       st->data_rdy, HZ /    

1000);

drivers/iio/adc/max1027.c static irqreturn_t

Quoted 15 lines
drivers/iio/adc/max1027.c +8 -1
+		st->data_rdy = false;
+		if (ret == -ERESTARTSYS)
+			return ret;
+	} else {
+		if (indio_dev->active_scan_mask)
+			conversion_time *= hweight32(*indio_dev-
 active_scan_mask);
 
-	usleep_range(conversion_time, conversion_time * 2);
+		usleep_range(conversion_time, conversion_time * 2);
+	}
 
 	return 0;

}

drivers/iio/adc/max1027.c +0 -0
@@ -481,6 +492,9 @@ static irqreturn_t    

max1027_eoc_irq_handler(int irq, void *private)

Quoted 8 lines
Quoted 8 lines

drivers/iio/adc/max1027.c static irqreturn_t max1027_eoc_irq_handler(int irq, void *private) · line 497

Quoted 6 lines
drivers/iio/adc/max1027.c +3 -0
 	if (st->cnvst_trigger) {
 		ret = max1027_read_scan(indio_dev);
 		iio_trigger_notify_done(indio_dev->trig);
+	} else {
+		st->data_rdy = true;
+		wake_up(&max1027_queue);    

I can't see why a queue is appropriate for this. Use a completion

and

Quoted 2 lines
Quoted 2 lines

have
one per instance of the device. No need for the flag etc in that

case as

Quoted 5 lines
Quoted 2 lines

complete() means we have had an interrupt.

In the case that 'st-> cnvst_trigger' is not set but the spi IRQ is present, we will wait until we get 'wake_up()' called from here. I

wonder if

Quoted 1 line

that is a good idea as the device own trigger is not being used. FWIW,

I think this

Quoted 1 line

sync logic is a bit confusing... I would still use the normal trigger

infrastructure

Quoted 1 line

('iio_trigger_generic_data_rdy_poll()') and use the 'cnvst_trigger'

flag in the

Quoted 1 line

trigger handler to manually start conversions + wait till eoc. But I

might be missing

Quoted 1 line

something though.

I implemented it your way, but I think I found a situation that was not fully handled (the 3rd), which makes the handler very complicated as we need to handle all the following cases:
1/ no trigger, irq enabled -> single read EOC interrupt
2/ external trigger, no irq -> handle the whole conversion process 3/ external trigger, irq enabled -> handle the whole conversion process
but also have a dedicated condition to handle the EOC interrupt properly (fortunately this is a threaded handler that can be preempted): we need to wait from the handler itself that the handler gets called again: the first time it is executed as "pollfunc", the second time as "EOC interrupt". In the second instance, call complete() in order to deliver the first running instance of the handler and continue until the reading part.
4/ cnvst trigger, irq enabled -> only reads the data.
5/ cnvst trigger, irq disabled -> not possible.

I added a lot of comments to make it clearer.

Quoted 8 lines
Quoted 1 line

Regarding this handler, I just realized that this is the hard IRQ handler

which

Quoted 1 line

might end up calling 'max1027_read_scan()' which in turn calls

'spi_read()'. Am I

Quoted 1 line

missing something here?

I renamed it to make it clear, but this is already a threaded handler.

Quoted 7 lines

Hmm, I think I get what you're trying to do.... FWIW, I think you're just going into a lot of trouble here for scenario 3 (I assume external trigger is something else other the device own one). IMO, I would just assume that if we are using an external trigger we have to wait (sleep) for the end of conversion (i.e, I would not care about the IRQ in this case). It would make things much more simpler and I guess it should be expected that if some user is deliberately not using the device own trigger, will have to wait more for scans.

I thought about that, but this means playing with enabling/disabling IRQs, which in the end I fear could be almost as verbose :/

Can you look at the new implementation that I proposed and give me your feedback on it?
[PATCH v2 15/16] iio: adc: max1027: Add support for external triggers

Quoted 5 lines
Quoted 2 lines

I cannot also see a reason why someone would want to use some external trigger if the device one is available... Does it really make sense?

I would say that "genericity" is a good enough reason for that, but in practice I agree with you.

Synchronising capture across a number of devices is usual reason for this. It's not perfect, but particularly if you have a device with a complex trigger that is not occurring on at an even 'tick' of time, then in those cases there isn't really any other way of doing it. However that irq is unrelated to this driver. This driver sees a trigger which it then identifies as not being it's own and from that fires the conversion and waits for a completion. The EOC interrupt handler knows we are in this mode and calls complete() rather than iio_poll_trigger()

Jonathan

Quoted 3 lines

Thanks,
Miquèl

reviewer
#60 lore raw

RE: [PATCH 14/16] iio: adc: max1027: Consolidate the end of conversion helper

Quoted 224 lines

-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Sunday, September 5, 2021 11:41 AM
To: Miquel Raynal <[email protected]>
Cc: Sa, Nuno <[email protected]>; Lars-Peter Clausen <[email protected]>; Thomas Petazzoni <[email protected]>; [email protected]; linux- [email protected]
Subject: Re: [PATCH 14/16] iio: adc: max1027: Consolidate the end of conversion helper

[External]

Quoted 12 lines · On Fri, 3 Sep 2021 16:46:50 +0200 Miquel Raynal <[email protected]>…

Hi Nuno,

[email protected] wrote on Fri, 3 Sep 2021 14:28:52 +0000:

Quoted 8 lines

Hi Miquel,

Quoted 6 lines

From: Miquel Raynal <[email protected]>
Sent: Thursday, September 2, 2021 5:13 PM
To: Sa, Nuno <[email protected]>
Cc: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>; Thomas Petazzoni <[email protected]>; [email protected];

linux-

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

[email protected]
Subject: Re: [PATCH 14/16] iio: adc: max1027: Consolidate the

end of

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

conversion helper

Hi Nuno,

"Sa, Nuno" <[email protected]> wrote on Mon, 30 Aug 2021 12:44:48
+0000:

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Monday, August 30, 2021 12:37 PM
To: Miquel Raynal <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>; Thomas

Petazzoni

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line

<[email protected]>; [email protected];

linux-

Quoted 2 lines
Quoted 2 lines

[email protected]
Subject: Re: [PATCH 14/16] iio: adc: max1027: Consolidate the

end

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

of

Quoted 6 lines
Quoted 6 lines

conversion helper

Quoted 1 line · On Wed, 18 Aug 2021 13:11:37 +0200 Miquel Raynal <[email protected]>…

Now that we have a dedicated handler for End Of

Conversion

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

interrupts,

Quoted 2 lines

let's create a second path:
- Situation 1: we are using the external hardware trigger, a

conversion

Quoted 1 line

has been triggered and the ADC pushed the data to its

FIFO, we

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

need to

Quoted 2 lines

retrieve the data and push it to the IIO buffers.
- Situation 2: we are not using the external hardware

trigger,

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

hence

Quoted 2 lines
Quoted 2 lines

we

Quoted 1 line

are likely waiting in a blocked thread waiting for this

interrupt to

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 8 lines

Review

Quoted 8 lines

happen: in this case we just wake up the waiting thread.

---
 drivers/iio/adc/max1027.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
(quoted diff) +0 -0

b/drivers/iio/adc/max1027.c

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

drivers/iio/adc/max1027.c struct max1027_state { · line 240

Quoted 10 lines

index 8d86e77fb5db..8c5995ae59f2 100644

drivers/iio/adc/max1027.c +1 -0
@@ -235,6 +235,7 @@ struct max1027_state {
 	struct iio_trigger		*trig;
 	__be16				*buffer;
 	struct mutex			lock;
+	bool				data_rdy;
 	bool				cnvst_trigger;
 	u8				reg

____cacheline_aligned;

Quoted 19 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

drivers/iio/adc/max1027.c static

Quoted 2 lines
drivers/iio/adc/max1027.c +0 -0
 };
@@ -243,12 +244,22 @@ static

DECLARE_WAIT_QUEUE_HEAD(max1027_queue);

drivers/iio/adc/max1027.c static · line 247

Quoted 5 lines
drivers/iio/adc/max1027.c +1 -0
 static int max1027_wait_eoc(struct iio_dev *indio_dev)
 {
+	struct max1027_state *st = iio_priv(indio_dev);
 	unsigned int conversion_time =

MAX1027_CONVERSION_UDELAY;

Quoted 2 lines
drivers/iio/adc/max1027.c +1 -0
+	int ret;
 
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

drivers/iio/adc/max1027.c static · line 252

Quoted 5 lines
drivers/iio/adc/max1027.c +2 -2
-	if (indio_dev->active_scan_mask)
-		conversion_time *= hweight32(*indio_dev-
 active_scan_mask);
+	if (st->spi->irq) {
+		ret =

wait_event_interruptible_timeout(max1027_queue,

drivers/iio/adc/max1027.c static · line 253

Quoted 1 line
drivers/iio/adc/max1027.c +1 -0
+						       st-

data_rdy, HZ /

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

1000);

drivers/iio/adc/max1027.c static · line 259

Quoted 6 lines
drivers/iio/adc/max1027.c +6 -0
+		st->data_rdy = false;
+		if (ret == -ERESTARTSYS)
+			return ret;
+	} else {
+		if (indio_dev->active_scan_mask)
+			conversion_time *=

hweight32(*indio_dev-

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

drivers/iio/adc/max1027.c static · line 262

Quoted 4 lines
drivers/iio/adc/max1027.c +1 -1
 active_scan_mask);
 
-	usleep_range(conversion_time, conversion_time * 2);
+		usleep_range(conversion_time,

conversion_time * 2);

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

drivers/iio/adc/max1027.c static irqreturn_t

Quoted 5 lines
drivers/iio/adc/max1027.c +1 -0
+	}
 
 	return 0;

}

drivers/iio/adc/max1027.c +0 -0
@@ -481,6 +492,9 @@ static irqreturn_t

max1027_eoc_irq_handler(int irq, void *private)

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/adc/max1027.c static irqreturn_t · line 497

Quoted 6 lines
drivers/iio/adc/max1027.c +3 -0
 	if (st->cnvst_trigger) {
 		ret = max1027_read_scan(indio_dev);
 		iio_trigger_notify_done(indio_dev->trig);
+	} else {
+		st->data_rdy = true;
+		wake_up(&max1027_queue);

I can't see why a queue is appropriate for this. Use a

completion

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

and

Quoted 2 lines
Quoted 2 lines

have
one per instance of the device. No need for the flag etc in

that

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

case as

Quoted 5 lines
Quoted 2 lines

complete() means we have had an interrupt.

In the case that 'st-> cnvst_trigger' is not set but the spi IRQ is present, we will wait until we get 'wake_up()' called from

here. I

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

wonder if

Quoted 1 line

that is a good idea as the device own trigger is not being used.

FWIW,

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

I think this

Quoted 1 line

sync logic is a bit confusing... I would still use the normal trigger

infrastructure

Quoted 1 line

('iio_trigger_generic_data_rdy_poll()') and use the

'cnvst_trigger'

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

flag in the

Quoted 1 line

trigger handler to manually start conversions + wait till eoc. But I

might be missing

Quoted 1 line

something though.

I implemented it your way, but I think I found a situation that was

not

Quoted 1 line
Quoted 1 line
Quoted 1 line

fully handled (the 3rd), which makes the handler very

complicated

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

as we need to handle all the following cases:
1/ no trigger, irq enabled -> single read EOC interrupt 2/ external trigger, no irq -> handle the whole conversion process 3/ external trigger, irq enabled -> handle the whole conversion

process

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

but also have a dedicated condition to handle the EOC interrupt properly (fortunately this is a threaded handler that can be preempted): we need to wait from the handler itself that the handler gets called again: the first time it is executed as "pollfunc", the second time as "EOC interrupt". In the second instance, call complete() in order to deliver the first running instance of the handler and continue until the reading part.
4/ cnvst trigger, irq enabled -> only reads the data. 5/ cnvst trigger, irq disabled -> not possible.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

I added a lot of comments to make it clearer.

Quoted 1 line

Regarding this handler, I just realized that this is the hard IRQ

handler

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

which

Quoted 1 line

might end up calling 'max1027_read_scan()' which in turn calls

'spi_read()'. Am I

Quoted 1 line

missing something here?

I renamed it to make it clear, but this is already a threaded

handler.

Quoted 3 lines
Quoted 3 lines
Quoted 1 line

Hmm, I think I get what you're trying to do.... FWIW, I think you're

just going

Quoted 1 line
Quoted 1 line

into a lot of trouble here for scenario 3 (I assume external trigger is

something

Quoted 1 line
Quoted 1 line

else other the device own one). IMO, I would just assume that if

we are using

Quoted 1 line
Quoted 1 line

an external trigger we have to wait (sleep) for the end of

conversion (i.e, I would

Quoted 1 line
Quoted 1 line

not care about the IRQ in this case). It would make things much

more simpler and

Quoted 1 line
Quoted 1 line

I guess it should be expected that if some user is deliberately not

using the

Quoted 6 lines
Quoted 1 line

device own trigger, will have to wait more for scans.

I thought about that, but this means playing with enabling/disabling IRQs, which in the end I fear could be almost as verbose :/

Can you look at the new implementation that I proposed and give

me your

Quoted 5 lines

feedback on it?
[PATCH v2 15/16] iio: adc: max1027: Add support for external triggers

Quoted 2 lines

I cannot also see a reason why someone would want to use some external trigger if the device one is available... Does it really make

sense?

Quoted 1 line
Quoted 7 lines
Quoted 2 lines

I would say that "genericity" is a good enough reason for that, but in practice I agree with you.

Synchronising capture across a number of devices is usual reason for this. It's not perfect, but particularly if you have a device with a complex trigger that is not occurring on at an even 'tick' of time, then in those cases there isn't really any other way of doing

I see. That is indeed a valid use case for this...

Quoted 6 lines

it. However that irq is unrelated to this driver. This driver sees a trigger which it then identifies as not being it's own and from that fires the conversion and waits for a completion. The EOC interrupt handler knows we are in this mode and calls complete() rather than iio_poll_trigger()

Yes, I do understand the logic. I'm just not 100% convinced that the added complexity for the syncing brings much added value. It evens looks like that the IRQ is not being used for normal raw reads? So, I would [probably] just act as no IRQ is present for external triggers (I do not think it's not that bad to enable/disable the IRQ on the trigger state cb). Alternatively, starting to us the IRQ for raw reads would make a stronger argument to enforce this logic as it would be more consistent...

But I guess this also boils down to personal preference so as long as you are fine with this logic :) ...

- Nuno Sá

reviewer
#61 lore raw

RE: [PATCH 14/16] iio: adc: max1027: Consolidate the end of conversion helper

Quoted 269 lines

-----Original Message-----
From: Sa, Nuno <[email protected]>
Sent: Monday, September 6, 2021 11:12 AM
To: Jonathan Cameron <[email protected]>; Miquel Raynal <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>; Thomas Petazzoni <[email protected]>; [email protected]; linux- [email protected]
Subject: RE: [PATCH 14/16] iio: adc: max1027: Consolidate the end of conversion helper

[External]

Quoted 28 lines

-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Sunday, September 5, 2021 11:41 AM
To: Miquel Raynal <[email protected]>
Cc: Sa, Nuno <[email protected]>; Lars-Peter Clausen <[email protected]>; Thomas Petazzoni <[email protected]>; [email protected]; linux- [email protected]
Subject: Re: [PATCH 14/16] iio: adc: max1027: Consolidate the end of conversion helper

[External]

Quoted 12 lines · On Fri, 3 Sep 2021 16:46:50 +0200 Miquel Raynal <[email protected]>…

Hi Nuno,

[email protected] wrote on Fri, 3 Sep 2021 14:28:52 +0000:

Quoted 8 lines

Hi Miquel,

Quoted 6 lines

From: Miquel Raynal <[email protected]>
Sent: Thursday, September 2, 2021 5:13 PM
To: Sa, Nuno <[email protected]>
Cc: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>; Thomas Petazzoni <[email protected]>; [email protected];

Quoted 9 lines

linux-

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

[email protected]
Subject: Re: [PATCH 14/16] iio: adc: max1027: Consolidate the

end of

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

conversion helper

Hi Nuno,

"Sa, Nuno" <[email protected]> wrote on Mon, 30 Aug

2021

Quoted 10 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

12:44:48
+0000:

Quoted 5 lines
Quoted 5 lines

-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Monday, August 30, 2021 12:37 PM
To: Miquel Raynal <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>; Thomas

Petazzoni

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

<[email protected]>; linux-

[email protected];

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

linux-

Quoted 2 lines
Quoted 2 lines

[email protected]
Subject: Re: [PATCH 14/16] iio: adc: max1027: Consolidate

the

Quoted 15 lines

end

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

of

Quoted 6 lines
Quoted 6 lines

conversion helper

Quoted 1 line · On Wed, 18 Aug 2021 13:11:37 +0200 Miquel Raynal <[email protected]>…

Now that we have a dedicated handler for End Of

Conversion

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

interrupts,

Quoted 2 lines

let's create a second path:
- Situation 1: we are using the external hardware trigger, a

conversion

Quoted 1 line

has been triggered and the ADC pushed the data to its

FIFO, we

Quoted 11 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

need to

Quoted 2 lines

retrieve the data and push it to the IIO buffers.
- Situation 2: we are not using the external hardware

trigger,

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

hence

Quoted 2 lines
Quoted 2 lines

we

Quoted 1 line

are likely waiting in a blocked thread waiting for this

interrupt to

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

happen: in this case we just wake up the waiting thread.

Signed-off-by Miquel Raynal

<[email protected]>

Quoted 72 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines

Review

Quoted 5 lines
---
 drivers/iio/adc/max1027.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
(quoted diff) +0 -0

b/drivers/iio/adc/max1027.c

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

drivers/iio/adc/max1027.c struct max1027_state { · line 240

Quoted 10 lines

index 8d86e77fb5db..8c5995ae59f2 100644

drivers/iio/adc/max1027.c +1 -0
@@ -235,6 +235,7 @@ struct max1027_state {
 	struct iio_trigger		*trig;
 	__be16				*buffer;
 	struct mutex			lock;
+	bool				data_rdy;
 	bool				cnvst_trigger;
 	u8				reg

____cacheline_aligned;

Quoted 19 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

drivers/iio/adc/max1027.c static

Quoted 2 lines
drivers/iio/adc/max1027.c +0 -0
 };
@@ -243,12 +244,22 @@ static

DECLARE_WAIT_QUEUE_HEAD(max1027_queue);

drivers/iio/adc/max1027.c static · line 247

Quoted 5 lines
drivers/iio/adc/max1027.c +1 -0
 static int max1027_wait_eoc(struct iio_dev *indio_dev)
 {
+	struct max1027_state *st = iio_priv(indio_dev);
 	unsigned int conversion_time =

MAX1027_CONVERSION_UDELAY;

Quoted 2 lines
drivers/iio/adc/max1027.c +1 -0
+	int ret;
 
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

drivers/iio/adc/max1027.c static · line 252

Quoted 5 lines
drivers/iio/adc/max1027.c +2 -2
-	if (indio_dev->active_scan_mask)
-		conversion_time *= hweight32(*indio_dev-
 active_scan_mask);
+	if (st->spi->irq) {
+		ret =

wait_event_interruptible_timeout(max1027_queue,

drivers/iio/adc/max1027.c static · line 253

Quoted 1 line
drivers/iio/adc/max1027.c +1 -0
+						       st-

data_rdy, HZ /

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

1000);

drivers/iio/adc/max1027.c static · line 259

Quoted 6 lines
drivers/iio/adc/max1027.c +6 -0
+		st->data_rdy = false;
+		if (ret == -ERESTARTSYS)
+			return ret;
+	} else {
+		if (indio_dev->active_scan_mask)
+			conversion_time *=

hweight32(*indio_dev-

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

drivers/iio/adc/max1027.c static · line 262

Quoted 4 lines
drivers/iio/adc/max1027.c +1 -1
 active_scan_mask);
 
-	usleep_range(conversion_time, conversion_time * 2);
+		usleep_range(conversion_time,

conversion_time * 2);

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

drivers/iio/adc/max1027.c static irqreturn_t

Quoted 5 lines
drivers/iio/adc/max1027.c +1 -0
+	}
 
 	return 0;

}

drivers/iio/adc/max1027.c +0 -0
@@ -481,6 +492,9 @@ static irqreturn_t

max1027_eoc_irq_handler(int irq, void *private)

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/adc/max1027.c static irqreturn_t · line 497

Quoted 6 lines
drivers/iio/adc/max1027.c +3 -0
 	if (st->cnvst_trigger) {
 		ret = max1027_read_scan(indio_dev);
 		iio_trigger_notify_done(indio_dev->trig);
+	} else {
+		st->data_rdy = true;
+		wake_up(&max1027_queue);

I can't see why a queue is appropriate for this. Use a

completion

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

and

Quoted 2 lines
Quoted 2 lines

have
one per instance of the device. No need for the flag etc in

that

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

case as

Quoted 3 lines
Quoted 2 lines

complete() means we have had an interrupt.

Quoted 8 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

In the case that 'st-> cnvst_trigger' is not set but the spi IRQ is present, we will wait until we get 'wake_up()' called from

here. I

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

wonder if

Quoted 1 line

that is a good idea as the device own trigger is not being used.

FWIW,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

I think this

Quoted 1 line

sync logic is a bit confusing... I would still use the normal

trigger

Quoted 5 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

infrastructure

Quoted 1 line

('iio_trigger_generic_data_rdy_poll()') and use the

'cnvst_trigger'

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

flag in the

Quoted 1 line

trigger handler to manually start conversions + wait till eoc.

But I

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

might be missing

Quoted 1 line

something though.

I implemented it your way, but I think I found a situation that

was

Quoted 6 lines

not

Quoted 1 line
Quoted 1 line
Quoted 1 line

fully handled (the 3rd), which makes the handler very

complicated

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

as we need to handle all the following cases:
1/ no trigger, irq enabled -> single read EOC interrupt 2/ external trigger, no irq -> handle the whole conversion

process

Quoted 3 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

3/ external trigger, irq enabled -> handle the whole conversion

process

Quoted 1 line
Quoted 1 line
Quoted 1 line

but also have a dedicated condition to handle the EOC

interrupt

Quoted 12 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

properly (fortunately this is a threaded handler that can be preempted): we need to wait from the handler itself that the handler gets called again: the first time it is executed as "pollfunc", the second time as "EOC interrupt". In the second instance, call complete() in order to deliver the first running instance of the handler and continue until the reading part.
4/ cnvst trigger, irq enabled -> only reads the data. 5/ cnvst trigger, irq disabled -> not possible.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

I added a lot of comments to make it clearer.

Quoted 1 line

Regarding this handler, I just realized that this is the hard IRQ

Quoted 11 lines

handler

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

which

Quoted 1 line

might end up calling 'max1027_read_scan()' which in turn calls

'spi_read()'. Am I

Quoted 1 line

missing something here?

I renamed it to make it clear, but this is already a threaded

handler.

Quoted 3 lines
Quoted 3 lines
Quoted 1 line

Hmm, I think I get what you're trying to do.... FWIW, I think

you're

Quoted 2 lines

just going

Quoted 1 line
Quoted 1 line

into a lot of trouble here for scenario 3 (I assume external trigger

is

Quoted 8 lines

something

Quoted 1 line
Quoted 1 line

else other the device own one). IMO, I would just assume that if

we are using

Quoted 1 line
Quoted 1 line

an external trigger we have to wait (sleep) for the end of

conversion (i.e, I would

Quoted 1 line
Quoted 1 line

not care about the IRQ in this case). It would make things much

more simpler and

Quoted 1 line
Quoted 1 line

I guess it should be expected that if some user is deliberately not

Quoted 10 lines

using the

Quoted 6 lines
Quoted 1 line

device own trigger, will have to wait more for scans.

I thought about that, but this means playing with enabling/disabling IRQs, which in the end I fear could be almost as verbose :/

Can you look at the new implementation that I proposed and give

me your

Quoted 2 lines

feedback on it?
[PATCH v2 15/16] iio: adc: max1027: Add support for external

triggers

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

I cannot also see a reason why someone would want to use some external trigger if the device one is available... Does it really make

sense?

Quoted 2 lines

I would say that "genericity" is a good enough reason for that, but

in

Quoted 6 lines
Quoted 1 line

practice I agree with you.

Synchronising capture across a number of devices is usual reason for this. It's not perfect, but particularly if you have a device with a complex trigger that is not occurring on at an even 'tick' of time, then in those cases there isn't really any other way of doing

I see. That is indeed a valid use case for this...

Quoted 6 lines

it. However that irq is unrelated to this driver. This driver sees a trigger which it then identifies as not being it's own and from that fires the conversion and waits for a completion. The EOC interrupt handler knows we are in this mode and calls complete() rather than iio_poll_trigger()

Quoted 3 lines

Yes, I do understand the logic. I'm just not 100% convinced that the added complexity for the syncing brings much added value. It evens looks like that the IRQ is not being used for normal raw reads?

Ups, my bad. Just reading the new version and the EOC is also used in raw reads... You can ignore my other email :)

- Nuno Sá

#62 lore raw

PATCH 15/16 iio: adc: max1027: Support software triggers

In reply to Miquel Raynal

Now that max1027_trigger_handler() has been freed from handling hardware triggers EOC situations, we can use it for what it has been designed in the first place: trigger software originated conversions. In other words, when userspace initiates a conversion with a sysfs trigger or a hrtimer trigger, we must do all configuration steps, ie:
1- Configuring the trigger
2- Configuring the channels to scan
3- Starting the conversion (actually done automatically by step 2 in
this case)
4- Waiting for the conversion to end
5- Retrieving the data from the ADC
6- Push the data to the IIO core and notify it

Add the missing steps to this helper and drop the trigger verification hook otherwise software triggers would simply not be accepted at all.

---
 drivers/iio/adc/max1027.c | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)
drivers/iio/adc/max1027.c +14 -12
index 8c5995ae59f2..bb437e43adaf 100644
@@ -413,17 +413,6 @@ static int max1027_debugfs_reg_access(struct iio_dev *indio_dev,
 	return spi_write(st->spi, val, 1);
 }
 
-static int max1027_validate_trigger(struct iio_dev *indio_dev,
-				    struct iio_trigger *trig)
-{
-	struct max1027_state *st = iio_priv(indio_dev);
-
-	if (st->trig != trig)
-		return -EINVAL;
-
-	return 0;
-}
-
 static int max1027_set_cnvst_trigger_state(struct iio_trigger *trig, bool state)
 {
 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
@@ -512,7 +501,21 @@ static irqreturn_t max1027_trigger_handler(int irq, void *private)
 
 	pr_debug("%s(irq=%d, private=0x%p)\n", __func__, irq, private);
 
+	ret = max1027_configure_trigger(indio_dev);
+	if (ret)
+		goto out;
+
+	ret = max1027_configure_chans_to_scan(indio_dev);
+	if (ret)
+		goto out;
+
+	ret = max1027_wait_eoc(indio_dev);
+	if (ret)
+		goto out;
+
 	ret = max1027_read_scan(indio_dev);
+
+out:
 	if (ret)
 		dev_err(&indio_dev->dev,
 			"Cannot read scanned values (%d)\n", ret);
@@ -529,7 +532,6 @@ static const struct iio_trigger_ops max1027_trigger_ops = {
 
 static const struct iio_info max1027_info = {
 	.read_raw = &max1027_read_raw,
-	.validate_trigger = &max1027_validate_trigger,
 	.debugfs_reg_access = &max1027_debugfs_reg_access,
 };
 
Signature · 1 line
2.27.0
reviewer
#63 lore raw

RE: [PATCH 15/16] iio: adc: max1027: Support software triggers

Quoted 69 lines

-----Original Message-----
From: Miquel Raynal <[email protected]>
Sent: Wednesday, August 18, 2021 1:12 PM
To: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>
Cc: Thomas Petazzoni <[email protected]>; linux- [email protected]; [email protected]; Miquel Raynal <[email protected]>
Subject: [PATCH 15/16] iio: adc: max1027: Support software triggers

[External]

Now that max1027_trigger_handler() has been freed from handling hardware
triggers EOC situations, we can use it for what it has been designed in the first place: trigger software originated conversions. In other words, when userspace initiates a conversion with a sysfs trigger or a hrtimer trigger, we must do all configuration steps, ie:
1- Configuring the trigger
2- Configuring the channels to scan
3- Starting the conversion (actually done automatically by step 2 in
this case)
4- Waiting for the conversion to end
5- Retrieving the data from the ADC
6- Push the data to the IIO core and notify it

Add the missing steps to this helper and drop the trigger verification hook otherwise software triggers would simply not be accepted at all.

---
 drivers/iio/adc/max1027.c | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)
drivers/iio/adc/max1027.c +0 -11
index 8c5995ae59f2..bb437e43adaf 100644
@@ -413,17 +413,6 @@ static int max1027_debugfs_reg_access(struct
 iio_dev *indio_dev,
 	return spi_write(st->spi, val, 1);
 }
 
-static int max1027_validate_trigger(struct iio_dev *indio_dev,
-				    struct iio_trigger *trig)
-{
-	struct max1027_state *st = iio_priv(indio_dev);
-
-	if (st->trig != trig)
-		return -EINVAL;
-
-	return 0;
-}
-
 static int max1027_set_cnvst_trigger_state(struct iio_trigger *trig,
 bool state)

{

drivers/iio/adc/max1027.c +3 -0
 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
@@ -512,7 +501,21 @@ static irqreturn_t max1027_trigger_handler(int
 irq, void *private)
 
 	pr_debug("%s(irq=%d, private=0x%p)\n", __func__, irq,
 private);
 
+	ret = max1027_configure_trigger(indio_dev);
+	if (ret)
+		goto out;

drivers/iio/adc/max1027.c static irqreturn_t max1027_trigger_handler(int · line 518

Quoted 10 lines
drivers/iio/adc/max1027.c +9 -0
+
+	ret = max1027_configure_chans_to_scan(indio_dev);
+	if (ret)
+		goto out;
+
+	ret = max1027_wait_eoc(indio_dev);
+	if (ret)
+		goto out;
+
 	ret = max1027_read_scan(indio_dev);

There's something that I'm not getting... How are we checking that we have software triggers? This API is called only if the device allocates it's own trigger which will happen if there's a spi IRQ.

I'm probably missing something as this series is fairly big but the way I would do it is (in the probe):

- always call 'devm_iio_triggered_buffer_setup()' function and properly use buffer ops [1] (for example, you can use 'validate_scan_mask()' to setup the channels to read);
- only allocate a trigger if an IRQ is present in which case, we assume HW triggering is supported.

Does this makes sense?

- Nuno Sá

#64 lore raw

Re: [PATCH 15/16] iio: adc: max1027: Support software triggers

Hi Nuno,

"Sa, Nuno" <[email protected]> wrote on Fri, 20 Aug 2021 07:58:25 +0000:

Quoted 82 lines
Quoted 67 lines

-----Original Message-----
From: Miquel Raynal <[email protected]>
Sent: Wednesday, August 18, 2021 1:12 PM
To: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>
Cc: Thomas Petazzoni <[email protected]>; linux- [email protected]; [email protected]; Miquel Raynal <[email protected]>
Subject: [PATCH 15/16] iio: adc: max1027: Support software triggers

[External]

Now that max1027_trigger_handler() has been freed from handling hardware
triggers EOC situations, we can use it for what it has been designed in the first place: trigger software originated conversions. In other words, when userspace initiates a conversion with a sysfs trigger or a hrtimer trigger, we must do all configuration steps, ie:
1- Configuring the trigger
2- Configuring the channels to scan
3- Starting the conversion (actually done automatically by step 2 in
this case)
4- Waiting for the conversion to end
5- Retrieving the data from the ADC
6- Push the data to the IIO core and notify it

Add the missing steps to this helper and drop the trigger verification hook otherwise software triggers would simply not be accepted at all.

---
 drivers/iio/adc/max1027.c | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)
drivers/iio/adc/max1027.c +0 -11
index 8c5995ae59f2..bb437e43adaf 100644
@@ -413,17 +413,6 @@ static int max1027_debugfs_reg_access(struct
 iio_dev *indio_dev,
 	return spi_write(st->spi, val, 1);
 }
 
-static int max1027_validate_trigger(struct iio_dev *indio_dev,
-				    struct iio_trigger *trig)
-{
-	struct max1027_state *st = iio_priv(indio_dev);
-
-	if (st->trig != trig)
-		return -EINVAL;
-
-	return 0;
-}
-
 static int max1027_set_cnvst_trigger_state(struct iio_trigger *trig,
 bool state)

{

drivers/iio/adc/max1027.c +3 -0
 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
@@ -512,7 +501,21 @@ static irqreturn_t max1027_trigger_handler(int
 irq, void *private)
 
 	pr_debug("%s(irq=%d, private=0x%p)\n", __func__, irq,
 private);
 
+	ret = max1027_configure_trigger(indio_dev);
+	if (ret)
+		goto out;

drivers/iio/adc/max1027.c static irqreturn_t max1027_trigger_handler(int · line 518

Quoted 10 lines
drivers/iio/adc/max1027.c +9 -0
+
+	ret = max1027_configure_chans_to_scan(indio_dev);
+	if (ret)
+		goto out;
+
+	ret = max1027_wait_eoc(indio_dev);
+	if (ret)
+		goto out;
+
 	ret = max1027_read_scan(indio_dev);  

There's something that I'm not getting... How are we checking that we have software triggers? This API is called only if the device allocates it's own trigger which will happen if there's a spi IRQ.

Quoted 8 lines

I'm probably missing something as this series is fairly big but the way I would do it is (in the probe):

- always call 'devm_iio_triggered_buffer_setup()' function and properly use buffer ops [1] (for example, you can use 'validate_scan_mask()' to setup the channels to read);
- only allocate a trigger if an IRQ is present in which case, we assume HW triggering is supported.

I think these are the exact steps that are enforced in the next patch, I can squash them if you wish but I think it makes sense to have it in two steps.

Thanks,
Miquèl

reviewer
#65 lore raw

RE: [PATCH 15/16] iio: adc: max1027: Support software triggers

Quoted 116 lines

-----Original Message-----
From: Miquel Raynal <[email protected]>
Sent: Thursday, September 2, 2021 2:25 PM
To: Sa, Nuno <[email protected]>
Cc: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>; Thomas Petazzoni <[email protected]>; [email protected]; linux- [email protected]
Subject: Re: [PATCH 15/16] iio: adc: max1027: Support software triggers

[External]

Hi Nuno,

"Sa, Nuno" <[email protected]> wrote on Fri, 20 Aug 2021 07:58:25 +0000:

Quoted 10 lines
Quoted 10 lines

-----Original Message-----
From: Miquel Raynal <[email protected]>
Sent: Wednesday, August 18, 2021 1:12 PM
To: Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>
Cc: Thomas Petazzoni <[email protected]>; linux- [email protected]; [email protected]; Miquel Raynal <[email protected]>
Subject: [PATCH 15/16] iio: adc: max1027: Support software triggers

Quoted 3 lines
Quoted 3 lines

[External]

Now that max1027_trigger_handler() has been freed from

handling

Quoted 2 lines
Quoted 2 lines

hardware
triggers EOC situations, we can use it for what it has been designed

in

Quoted 2 lines
Quoted 2 lines

the first place: trigger software originated conversions. In other words, when userspace initiates a conversion with a sysfs trigger or

a

Quoted 10 lines
Quoted 10 lines

hrtimer trigger, we must do all configuration steps, ie:
1- Configuring the trigger
2- Configuring the channels to scan
3- Starting the conversion (actually done automatically by step 2 in
this case)
4- Waiting for the conversion to end
5- Retrieving the data from the ADC
6- Push the data to the IIO core and notify it

Add the missing steps to this helper and drop the trigger

verification

Quoted 1 line
Quoted 1 line

hook otherwise software triggers would simply not be accepted at

all.

Quoted 11 lines

drivers/iio/adc/max1027.c static int

Quoted 11 lines
---
 drivers/iio/adc/max1027.c | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)
drivers/iio/adc/max1027.c +0 -0
index 8c5995ae59f2..bb437e43adaf 100644
@@ -413,17 +413,6 @@ static int

max1027_debugfs_reg_access(struct

Quoted 4 lines
Quoted 4 lines
drivers/iio/adc/max1027.c +0 -0
 iio_dev *indio_dev,
 	return spi_write(st->spi, val, 1);
 }
 
Quoted 16 lines

drivers/iio/adc/max1027.c static irqreturn_t

Quoted 16 lines
drivers/iio/adc/max1027.c +0 -11
-static int max1027_validate_trigger(struct iio_dev *indio_dev,
-				    struct iio_trigger *trig)
-{
-	struct max1027_state *st = iio_priv(indio_dev);
-
-	if (st->trig != trig)
-		return -EINVAL;
-
-	return 0;
-}
-
 static int max1027_set_cnvst_trigger_state(struct iio_trigger *trig,
 bool state)

{

drivers/iio/adc/max1027.c +0 -0
 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
@@ -512,7 +501,21 @@ static irqreturn_t

max1027_trigger_handler(int

Quoted 23 lines
Quoted 8 lines
drivers/iio/adc/max1027.c +3 -0
 irq, void *private)
 
 	pr_debug("%s(irq=%d, private=0x%p)\n", __func__, irq,
 private);
 
+	ret = max1027_configure_trigger(indio_dev);
+	if (ret)
+		goto out;

drivers/iio/adc/max1027.c static irqreturn_t max1027_trigger_handler(int irq, void *private) · line 516

Quoted 10 lines
drivers/iio/adc/max1027.c +9 -0
+
+	ret = max1027_configure_chans_to_scan(indio_dev);
+	if (ret)
+		goto out;
+
+	ret = max1027_wait_eoc(indio_dev);
+	if (ret)
+		goto out;
+
 	ret = max1027_read_scan(indio_dev);

There's something that I'm not getting... How are we checking that we have software triggers? This API is called only if the device allocates it's own trigger which will happen if there's a spi IRQ.

Quoted 4 lines

I'm probably missing something as this series is fairly big but the way I would do it is (in the probe):

- always call 'devm_iio_triggered_buffer_setup()' function and

properly use

Quoted 1 line

buffer ops [1] (for example, you can use 'validate_scan_mask()' to

setup the

Quoted 9 lines
Quoted 2 lines

channels to read);
- only allocate a trigger if an IRQ is present in which case, we assume

HW

Quoted 1 line

triggering is supported.

I think these are the exact steps that are enforced in the next patch, I can squash them if you wish but I think it makes sense to have it in two steps.

Yeah, my bad here...

- Nuno Sá

maintainer
#66 lore raw

Re: [PATCH 15/16] iio: adc: max1027: Support software triggers

In reply to Miquel Raynal
Quoted 3 lines · On Wed, 18 Aug 2021 13:11:38 +0200 Miquel Raynal <[email protected]>…

Now that max1027_trigger_handler() has been freed from handling hardware triggers EOC situations, we can use it for what it has been designed in the first place: trigger software originated conversions.

As mentioned earlier, this is not how I'd normally expect this sort of case to be handled. I'd be expecting the cnvst trigger to still be calling this function and the function to do the relevant check to ensure it knows the data is already available in that case.

Quoted 42 lines

In other
words, when userspace initiates a conversion with a sysfs trigger or a hrtimer trigger, we must do all configuration steps, ie:
1- Configuring the trigger
2- Configuring the channels to scan
3- Starting the conversion (actually done automatically by step 2 in
this case)
4- Waiting for the conversion to end
5- Retrieving the data from the ADC
6- Push the data to the IIO core and notify it

Add the missing steps to this helper and drop the trigger verification hook otherwise software triggers would simply not be accepted at all.

---
 drivers/iio/adc/max1027.c | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)
drivers/iio/adc/max1027.c +0 -11
index 8c5995ae59f2..bb437e43adaf 100644
@@ -413,17 +413,6 @@ static int max1027_debugfs_reg_access(struct iio_dev *indio_dev,
 	return spi_write(st->spi, val, 1);
 }
 
-static int max1027_validate_trigger(struct iio_dev *indio_dev,
-				    struct iio_trigger *trig)
-{
-	struct max1027_state *st = iio_priv(indio_dev);
-
-	if (st->trig != trig)
-		return -EINVAL;
-
-	return 0;
-}
-
 static int max1027_set_cnvst_trigger_state(struct iio_trigger *trig, bool state)
 {
 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);

drivers/iio/adc/max1027.c static irqreturn_t max1027_trigger_handler(int irq, void *private) · line 504

Quoted 4 lines
drivers/iio/adc/max1027.c +1 -0
@@ -512,7 +501,21 @@ static irqreturn_t max1027_trigger_handler(int irq, void *private)
 
 	pr_debug("%s(irq=%d, private=0x%p)\n", __func__, irq, private);
 
+	ret = max1027_configure_trigger(indio_dev);

I'd not expect to see this ever time. The configuration shouldn't change from one call of this function to the next.

drivers/iio/adc/max1027.c static irqreturn_t max1027_trigger_handler(int irq, void *private) · line 508

Quoted 4 lines
drivers/iio/adc/max1027.c +4 -0
+	if (ret)
+		goto out;
+
+	ret = max1027_configure_chans_to_scan(indio_dev);

This should also not change unless it is also responsible for the 'go' signal. If that's true then it is badly named.

Quoted 21 lines
drivers/iio/adc/max1027.c +9 -1
+	if (ret)
+		goto out;
+
+	ret = max1027_wait_eoc(indio_dev);
+	if (ret)
+		goto out;
+
 	ret = max1027_read_scan(indio_dev);
+
+out:
 	if (ret)
 		dev_err(&indio_dev->dev,
 			"Cannot read scanned values (%d)\n", ret);
@@ -529,7 +532,6 @@ static const struct iio_trigger_ops max1027_trigger_ops = {
 
 static const struct iio_info max1027_info = {
 	.read_raw = &max1027_read_raw,
-	.validate_trigger = &max1027_validate_trigger,
 	.debugfs_reg_access = &max1027_debugfs_reg_access,
 };
 
#67 lore raw

Re: [PATCH 15/16] iio: adc: max1027: Support software triggers

Hi Jonathan,

Jonathan Cameron <[email protected]> wrote on Mon, 30 Aug 2021 11:50:46 +0100:

Quoted 11 lines
Quoted 3 lines · On Wed, 18 Aug 2021 13:11:38 +0200 Miquel Raynal <[email protected]>…

Now that max1027_trigger_handler() has been freed from handling hardware triggers EOC situations, we can use it for what it has been designed in the first place: trigger software originated conversions.

As mentioned earlier, this is not how I'd normally expect this sort of case to be handled. I'd be expecting the cnvst trigger to still be calling this function and the function to do the relevant check to ensure it knows the data is already available in that case.

I tried to follow your advice and Nuno's regarding this, I hope the new version will match your expectations (new version coming soon). However if my changes do not match, I will probably need more guidance to understand in deep what you suggest.

Quoted 43 lines
Quoted 43 lines

In other
words, when userspace initiates a conversion with a sysfs trigger or a hrtimer trigger, we must do all configuration steps, ie:
1- Configuring the trigger
2- Configuring the channels to scan
3- Starting the conversion (actually done automatically by step 2 in
this case)
4- Waiting for the conversion to end
5- Retrieving the data from the ADC
6- Push the data to the IIO core and notify it

Add the missing steps to this helper and drop the trigger verification hook otherwise software triggers would simply not be accepted at all.

---
 drivers/iio/adc/max1027.c | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)
drivers/iio/adc/max1027.c +0 -11
index 8c5995ae59f2..bb437e43adaf 100644
@@ -413,17 +413,6 @@ static int max1027_debugfs_reg_access(struct iio_dev *indio_dev,
 	return spi_write(st->spi, val, 1);
 }
 
-static int max1027_validate_trigger(struct iio_dev *indio_dev,
-				    struct iio_trigger *trig)
-{
-	struct max1027_state *st = iio_priv(indio_dev);
-
-	if (st->trig != trig)
-		return -EINVAL;
-
-	return 0;
-}
-
 static int max1027_set_cnvst_trigger_state(struct iio_trigger *trig, bool state)
 {
 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
@@ -512,7 +501,21 @@ static irqreturn_t max1027_trigger_handler(int irq, void *private)
 
Quoted 6 lines

drivers/iio/adc/max1027.c static irqreturn_t max1027_trigger_handler(int irq, void *private) · line 504

Quoted 3 lines
drivers/iio/adc/max1027.c +1 -0
 	pr_debug("%s(irq=%d, private=0x%p)\n", __func__, irq, private);
 
+	ret = max1027_configure_trigger(indio_dev);  

I'd not expect to see this ever time. The configuration shouldn't change from one call of this function to the next.

True, this is not needed.

Quoted 7 lines

drivers/iio/adc/max1027.c static irqreturn_t max1027_trigger_handler(int irq, void *private) · line 508

Quoted 4 lines
drivers/iio/adc/max1027.c +4 -0
+	if (ret)
+		goto out;
+
+	ret = max1027_configure_chans_to_scan(indio_dev);  

This should also not change unless it is also responsible for the 'go' signal. If that's true then it is badly named.

It's responsible for the go signal, I renamed it "configure_and_start".

However, just for my own understanding, when would I be supposed to configure the channels requested by the user otherwise?

Thanks,
Miquèl

maintainer
#68 lore raw

Re: [PATCH 15/16] iio: adc: max1027: Support software triggers

Quoted 82 lines · On Thu, 2 Sep 2021 17:21:00 +0200 Miquel Raynal <[email protected]>…

Hi Jonathan,

Jonathan Cameron <[email protected]> wrote on Mon, 30 Aug 2021 11:50:46 +0100:

Quoted 11 lines
Quoted 3 lines · On Wed, 18 Aug 2021 13:11:38 +0200 Miquel Raynal <[email protected]>…

Now that max1027_trigger_handler() has been freed from handling hardware triggers EOC situations, we can use it for what it has been designed in the first place: trigger software originated conversions.

As mentioned earlier, this is not how I'd normally expect this sort of case to be handled. I'd be expecting the cnvst trigger to still be calling this function and the function to do the relevant check to ensure it knows the data is already available in that case.

I tried to follow your advice and Nuno's regarding this, I hope the new version will match your expectations (new version coming soon). However if my changes do not match, I will probably need more guidance to understand in deep what you suggest.

Quoted 43 lines
Quoted 43 lines

In other
words, when userspace initiates a conversion with a sysfs trigger or a hrtimer trigger, we must do all configuration steps, ie:
1- Configuring the trigger
2- Configuring the channels to scan
3- Starting the conversion (actually done automatically by step 2 in
this case)
4- Waiting for the conversion to end
5- Retrieving the data from the ADC
6- Push the data to the IIO core and notify it

Add the missing steps to this helper and drop the trigger verification hook otherwise software triggers would simply not be accepted at all.

---
 drivers/iio/adc/max1027.c | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)
drivers/iio/adc/max1027.c +0 -11
index 8c5995ae59f2..bb437e43adaf 100644
@@ -413,17 +413,6 @@ static int max1027_debugfs_reg_access(struct iio_dev *indio_dev,
 	return spi_write(st->spi, val, 1);
 }
 
-static int max1027_validate_trigger(struct iio_dev *indio_dev,
-				    struct iio_trigger *trig)
-{
-	struct max1027_state *st = iio_priv(indio_dev);
-
-	if (st->trig != trig)
-		return -EINVAL;
-
-	return 0;
-}
-
 static int max1027_set_cnvst_trigger_state(struct iio_trigger *trig, bool state)
 {
 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
@@ -512,7 +501,21 @@ static irqreturn_t max1027_trigger_handler(int irq, void *private)
 
Quoted 6 lines

drivers/iio/adc/max1027.c static irqreturn_t max1027_trigger_handler(int irq, void *private) · line 504

Quoted 3 lines
drivers/iio/adc/max1027.c +1 -0
 	pr_debug("%s(irq=%d, private=0x%p)\n", __func__, irq, private);
 
+	ret = max1027_configure_trigger(indio_dev);    

I'd not expect to see this ever time. The configuration shouldn't change from one call of this function to the next.

True, this is not needed.

Quoted 7 lines

drivers/iio/adc/max1027.c static irqreturn_t max1027_trigger_handler(int irq, void *private) · line 508

Quoted 4 lines
drivers/iio/adc/max1027.c +4 -0
+	if (ret)
+		goto out;
+
+	ret = max1027_configure_chans_to_scan(indio_dev);    

This should also not change unless it is also responsible for the 'go' signal. If that's true then it is badly named.

Quoted 4 lines

It's responsible for the go signal, I renamed it "configure_and_start".

However, just for my own understanding, when would I be supposed to configure the channels requested by the user otherwise?

For buffered operation update_scan_mask callback.
There is a quirk where that's not called on the buffer disable path because it's not always obvious what to 'reset to'.

J

Quoted 3 lines

Thanks,
Miquèl

#69 lore raw

PATCH 16/16 iio: adc: max1027: Enable software triggers to be used without IRQ

In reply to Miquel Raynal

Software triggers do not need a device IRQ to work. As opposed to hardware triggers which need it to yield the data to the IIO core, software triggers run a dedicated thread which does all the tasks on their behalf. Then, the end of conversion status may either come from an interrupt or from a sufficient enough extra delay. IRQs are not mandatory so move the triggered buffer setup out of the IRQ condition.

---
 drivers/iio/adc/max1027.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
drivers/iio/adc/max1027.c +11 -9
index bb437e43adaf..e767437a578e 100644
@@ -567,16 +567,18 @@ static int max1027_probe(struct spi_device *spi)
 	if (!st->buffer)
 		return -ENOMEM;
 
+	/* Enable triggered buffers */
+	ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev,
+					      &iio_pollfunc_store_time,
+					      &max1027_trigger_handler,
+					      NULL);
+	if (ret < 0) {
+		dev_err(&indio_dev->dev, "Failed to setup buffer\n");
+		return ret;
+	}
+
+	/* If there is an EOC interrupt, enable the hardware trigger (cnvst) */
 	if (spi->irq) {
-		ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev,
-						      &iio_pollfunc_store_time,
-						      &max1027_trigger_handler,
-						      NULL);
-		if (ret < 0) {
-			dev_err(&indio_dev->dev, "Failed to setup buffer\n");
-			return ret;
-		}
-
 		st->trig = devm_iio_trigger_alloc(&spi->dev, "%s-trigger",
 						  indio_dev->name);
 		if (st->trig == NULL) {
Signature · 1 line
2.27.0
maintainer
#70 lore raw

Re: [PATCH 16/16] iio: adc: max1027: Enable software triggers to be used without IRQ

Quoted 6 lines · On Wed, 18 Aug 2021 13:11:39 +0200 Miquel Raynal <[email protected]>…

Software triggers do not need a device IRQ to work. As opposed to hardware triggers which need it to yield the data to the IIO core, software triggers run a dedicated thread which does all the tasks on their behalf. Then, the end of conversion status may either come from an interrupt or from a sufficient enough extra delay. IRQs are not mandatory so move the triggered buffer setup out of the IRQ condition.

I'd stop referring to software triggers in these descriptions. This could just as easily be about enabling a different hardware trigger such as a gpio trigger or indeed on a dataready trigger provided by an entirely different device.

Otherwise the logic is correct.

Good to get this more flexible support into the driver. If we can make it a tiny bit more flexible by enabling use of the cnvst trigger to drive this 'and' other drivers that would be even better and conform more closely to the normal way an IIO driver work.

The validate_device / validate_trigger callbacks are often about making it easier to bring a device driver up in the first place, so it's great to see them go away in later improvements like this. (note I'm not saying there aren't complex cases where we can't remove them though!)

Jonathan

Quoted 38 lines
---
 drivers/iio/adc/max1027.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
drivers/iio/adc/max1027.c +11 -9
index bb437e43adaf..e767437a578e 100644
@@ -567,16 +567,18 @@ static int max1027_probe(struct spi_device *spi)
 	if (!st->buffer)
 		return -ENOMEM;
 
+	/* Enable triggered buffers */
+	ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev,
+					      &iio_pollfunc_store_time,
+					      &max1027_trigger_handler,
+					      NULL);
+	if (ret < 0) {
+		dev_err(&indio_dev->dev, "Failed to setup buffer\n");
+		return ret;
+	}
+
+	/* If there is an EOC interrupt, enable the hardware trigger (cnvst) */
 	if (spi->irq) {
-		ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev,
-						      &iio_pollfunc_store_time,
-						      &max1027_trigger_handler,
-						      NULL);
-		if (ret < 0) {
-			dev_err(&indio_dev->dev, "Failed to setup buffer\n");
-			return ret;
-		}
-
 		st->trig = devm_iio_trigger_alloc(&spi->dev, "%s-trigger",
 						  indio_dev->name);
 		if (st->trig == NULL) {