Skip to contentSkip to conversation

Migrate STX104 to the regmap API

PATCH Superseded William Breathitt Gray 3 participants 10 messages Mar 26, 2023 – Apr 2, 2023

Subsystems (2)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover Migrate STX104 to the regmap API

Changes in v3:
- Add STX104_ prefixes to defines to avoid potential name classes
- Rename SAME_CHANNEL() to STX104_SINGLE_CHANNEL() to convey intention better
- Utilize u8_encode_bits() to define STX104_SOFTWARE_TRIGGER
- Adjust to utilize reg_base members in regmap_config structures
- Fix off-by-one errors in aio_data_wr_ranges[], aio_data_rd_ranges[], and aio_data_regmap_config max_register
- Inline gpio_config initialization to avoid zeroing it at declaration
- Add blank lines between register map init blocks for clarity
- Utilize regmap_read_poll_timeout() for ADC conversion status poll
Changes in v2:
- Relocate struct stx104_iio for the sake of a clearer patch diff
- Replace FIELD_PREP() and FIELD_GET() with u8_encode_bits() and u8_get_bits()

The regmap API supports IO port accessors so we can take advantage of regmap abstractions rather than handling access to the device registers directly in the driver.

A patch to utilize regmap_read_poll_timeout() for polling the ADC conversion status is included as follow-up to make the git history clearer for this change.

William Breathitt Gray (2):
iio: addac: stx104: Migrate to the regmap API
iio: addac: stx104: Use regmap_read_poll_timeout() for conversion poll

 drivers/iio/addac/Kconfig  |   2 +
 drivers/iio/addac/stx104.c | 434 +++++++++++++++++++++----------------
 2 files changed, 244 insertions(+), 192 deletions(-)

base-commit: 46e33707fe95a21aa9896bded0be97285b779509

Signature · 1 line
2.39.2
#9 lore raw

PATCH v3 2/2 iio: addac: stx104: Use regmap_read_poll_timeout() for conversion poll

ADC sample captures take a certain amount of time to complete after initiated; this conversion time range can be anywhere from 5 uSec to 53.68 Seconds depending on the configuration of the Analog Input Frame Timer register. When the conversion is in progress, the ADC Status register CNV bit is high. Utilize regmap_read_poll_timeout() to poll until the ADC conversion is completed (or timeout if more than 53.68 Seconds passes).

---
 drivers/iio/addac/stx104.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/iio/addac/stx104.c +8 -5
index 17d8b8e22dc3..a804f23b5619 100644
@@ -202,15 +202,18 @@ static int stx104_read_raw(struct iio_dev *indio_dev,
 
 		/* trigger ADC sample capture by writing to the 8-bit
 		 * Software Strobe Register and wait for completion
+		 * Range is 5 uSec to 53.68 Seconds in steps of 25 nanoseconds.
+		 * The actual Analog Input Frame Timer time interval is calculated as:
+		 * ai_time_frame_ns = ( AIFT + 1 ) * ( 25 nSec ).
+		 * Where 0 <= AIFT <= 2147483648.
 		 */
 		err = regmap_write(priv->aio_ctl_map, STX104_SOFTWARE_STROBE, 0);
 		if (err)
 			return err;
-		do {
-			err = regmap_read(priv->aio_ctl_map, STX104_ADC_STATUS, &adc_status);
-			if (err)
-				return err;
-		} while (u8_get_bits(adc_status, STX104_CNV));
+		err = regmap_read_poll_timeout(priv->aio_ctl_map, STX104_ADC_STATUS, adc_status,
+					       !u8_get_bits(adc_status, STX104_CNV), 0, 53687092);
+		if (err)
+			return err;
 
 		err = regmap_read(priv->aio_data_map, STX104_ADC_DATA, &value);
 		if (err)
Signature · 1 line
2.39.2
#1 lore raw

PATCH v3 1/2 iio: addac: stx104: Migrate to the regmap API

In reply to William Breathitt Gray

The regmap API supports IO port accessors so we can take advantage of regmap abstractions rather than handling access to the device registers directly in the driver.

---
Changes in v3:
- Add STX104_ prefixes to defines to avoid potential name classes
- Rename SAME_CHANNEL() to STX104_SINGLE_CHANNEL() to convey intention better
- Utilize u8_encode_bits() to define STX104_SOFTWARE_TRIGGER
- Adjust to utilize reg_base members in regmap_config structures
- Fix off-by-one errors in aio_data_wr_ranges[], aio_data_rd_ranges[], and aio_data_regmap_config max_register
- Inline gpio_config initialization to avoid zeroing it at declaration
- Add blank lines between register map init blocks for clarity
Changes in v2:
- Relocate struct stx104_iio for the sake of a clearer patch diff
- Replace FIELD_PREP() and FIELD_GET() with u8_encode_bits() and u8_get_bits()

 drivers/iio/addac/Kconfig  |   2 +
 drivers/iio/addac/stx104.c | 431 ++++++++++++++++++++-----------------
 2 files changed, 241 insertions(+), 192 deletions(-)
drivers/iio/addac/Kconfig +2 -0
index 2843fcb70e24..877f9124803c 100644
@@ -35,7 +35,9 @@ config STX104
 	tristate "Apex Embedded Systems STX104 driver"
 	depends on PC104 && X86
 	select ISA_BUS_API
+	select REGMAP_MMIO
 	select GPIOLIB
+	select GPIO_REGMAP
 	help
 	  Say yes here to build support for the Apex Embedded Systems STX104
 	  integrated analog PC/104 card.
drivers/iio/addac/stx104.c +239 -192

573 lines not on this page · raw message

Signature · 1 line
2.39.2
#2 lore raw

Re: [PATCH v3 1/2] iio: addac: stx104: Migrate to the regmap API

Quoted 3 lines · William Breathitt Gray

The regmap API supports IO port accessors so we can take advantage of regmap abstractions rather than handling access to the device registers directly in the driver.

...

drivers/iio/addac/stx104.c static unsigned int num_stx104; · line 124

Quoted 8 lines
drivers/iio/addac/stx104.c +8 -0
+static const struct regmap_config aio_ctl_regmap_config = {
+	.name = "aio_ctl",
+	.reg_bits = 8,
+	.reg_stride = 1,
+	.reg_base = STX104_AIO_BASE,
+	.val_bits = 8,
+	.io_port = true,
+	.max_register = 0x11,

Not sure if define would be better for this, so it will be grouped with register offset definitions. (Same for the other configs)

drivers/iio/addac/stx104.c static unsigned int num_stx104; · line 129

Quoted 5 lines
drivers/iio/addac/stx104.c +5 -0
+	.wr_table = &aio_ctl_wr_table,
+	.rd_table = &aio_ctl_rd_table,
+	.volatile_table = &aio_ctl_volatile_table,
+	.cache_type = REGCACHE_FLAT,
+};

Do we need regmap lock?

...

drivers/iio/addac/stx104.c static unsigned int num_stx104; · line 158

Quoted 13 lines
drivers/iio/addac/stx104.c +13 -0
+static const struct regmap_config aio_data_regmap_config = {
+	.name = "aio_data",
+	.reg_bits = 16,
+	.reg_stride = STX104_AIO_DATA_STRIDE,
+	.reg_base = STX104_AIO_BASE,
+	.val_bits = 16,
+	.io_port = true,
+	.max_register = 0x6,
+	.wr_table = &aio_data_wr_table,
+	.rd_table = &aio_data_rd_table,
+	.volatile_table = &aio_data_rd_table,
+	.cache_type = REGCACHE_FLAT,
+};

Ditto.

drivers/iio/addac/stx104.c static unsigned int num_stx104; · line 168

Quoted 9 lines
drivers/iio/addac/stx104.c +8 -0
+static const struct regmap_config dio_regmap_config = {
+	.name = "dio",
+	.reg_bits = 8,
+	.reg_stride = 1,
+	.reg_base = STX104_DIO_REG,
+	.val_bits = 8,
+	.io_port = true,
+	.max_register = 0x0,
 };

Ditto.

...

drivers/iio/addac/stx104.c static unsigned int num_stx104; · line 185

Quoted 6 lines
drivers/iio/addac/stx104.c +4 -1
+		err = regmap_read(priv->aio_ctl_map, STX104_ADC_CONFIGURATION, &adc_config);
+		if (err)
+			return err;
 
-		*val = 1 << gain;
+		*val = 1 << u8_get_bits(adc_config, STX104_GAIN);

Maybe not for this change, but why not BIT()?

...

drivers/iio/addac/stx104.c static unsigned int num_stx104; · line 213

Quoted 5 lines
drivers/iio/addac/stx104.c +5 -0
+		do {
+			err = regmap_read(priv->aio_ctl_map, STX104_ADC_STATUS, &adc_status);
+			if (err)
+				return err;
+		} while (u8_get_bits(adc_status, STX104_CNV));

Hmm... Isn't it a potential infinite loop (e.g., ther hardware / firmware is broken)?

Why not using regmap_read_poll_timeout() (or its atomic variant, depends on the case)?

...

Review

Quoted 2 lines
(quoted diff) +0 -0
 	case IIO_CHAN_INFO_RAW:
 		if (chan->output) {

You can decrease indentation by

		if (!chan->output)
			return -EINVAL;

here.

Quoted 13 lines
drivers/iio/addac/stx104.c +2 -4
 			/* DAC can only accept up to a 16-bit value */
 			if ((unsigned int)val > 65535)
 				return -EINVAL;
 
-			priv->chan_out_states[chan->channel] = val;
-			iowrite16(val, &priv->reg->dac[chan->channel]);
-
-			return 0;
+			return regmap_write(priv->aio_data_map, STX104_DAC_OFFSET(chan->channel),
+					    val);
 		}
 		return -EINVAL;
 	}

...

drivers/iio/addac/stx104.c static int stx104_probe(struct device *dev, unsigned int id) · line 433

Quoted 11 lines
drivers/iio/addac/stx104.c +11 -0
+	gpio_config = (struct gpio_regmap_config) {
+		.parent = dev,
+		.regmap = dio_map,
+		.ngpio = STX104_NGPIO,
+		.names = stx104_names,
+		.reg_dat_base = GPIO_REGMAP_ADDR(STX104_DIO_REG),
+		.reg_set_base = GPIO_REGMAP_ADDR(STX104_DIO_REG),
+		.ngpio_per_reg = STX104_NGPIO,
+		.reg_mask_xlate = stx104_reg_mask_xlate,
+		.drvdata = dio_map,
+	};

Not sure of compound literal is good to have in such case, but if Jonathan asked for that...

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

Re: [PATCH v3 1/2] iio: addac: stx104: Migrate to the regmap API

Quoted 16 lines

drivers/iio/addac/stx104.c static int stx104_probe(struct device *dev, unsigned int id) · line 433

Quoted 13 lines
drivers/iio/addac/stx104.c +11 -0
+	gpio_config = (struct gpio_regmap_config) {
+		.parent = dev,
+		.regmap = dio_map,
+		.ngpio = STX104_NGPIO,
+		.names = stx104_names,
+		.reg_dat_base = GPIO_REGMAP_ADDR(STX104_DIO_REG),
+		.reg_set_base = GPIO_REGMAP_ADDR(STX104_DIO_REG),
+		.ngpio_per_reg = STX104_NGPIO,
+		.reg_mask_xlate = stx104_reg_mask_xlate,
+		.drvdata = dio_map,
+	};  

Not sure of compound literal is good to have in such case, but if Jonathan asked for that...

I didn't like the separately zeroing of the structure in a different place in the code. This avoids that (but then so would using an explicit memset just before filling the rest in.)

Jonathan

maintainer
#4 lore raw

Re: [PATCH v3 1/2] iio: addac: stx104: Migrate to the regmap API

In reply to Andy Shevchenko
Quoted 11 lines

drivers/iio/addac/stx104.c static unsigned int num_stx104; · line 213

Quoted 7 lines
drivers/iio/addac/stx104.c +5 -0
+		do {
+			err = regmap_read(priv->aio_ctl_map, STX104_ADC_STATUS, &adc_status);
+			if (err)
+				return err;
+		} while (u8_get_bits(adc_status, STX104_CNV));  

Hmm... Isn't it a potential infinite loop (e.g., ther hardware / firmware is broken)?

Quoted 2 lines

Why not using regmap_read_poll_timeout() (or its atomic variant, depends on the case)?

Just to shortcut things as I'm looking at this.
That's currently handled in patch 2. Argument being this is a direct conversion of existing code, whereas changing to xxx_poll_timeout() is an improvement.

I'm fine with it just being rolled into first patch with a note in the patch description though if that works better.

#5 lore raw

Re: [PATCH v3 1/2] iio: addac: stx104: Migrate to the regmap API

Quoted 14 lines · Jonathan Cameron
Quoted 11 lines

drivers/iio/addac/stx104.c static unsigned int num_stx104; · line 213

Quoted 7 lines
drivers/iio/addac/stx104.c +5 -0
+		do {
+			err = regmap_read(priv->aio_ctl_map, STX104_ADC_STATUS, &adc_status);
+			if (err)
+				return err;
+		} while (u8_get_bits(adc_status, STX104_CNV));  

Hmm... Isn't it a potential infinite loop (e.g., ther hardware / firmware is broken)?

Quoted 2 lines

Why not using regmap_read_poll_timeout() (or its atomic variant, depends on the case)?

Quoted 6 lines

Just to shortcut things as I'm looking at this.
That's currently handled in patch 2. Argument being this is a direct conversion of existing code, whereas changing to xxx_poll_timeout() is an improvement.

I'm fine with it just being rolled into first patch with a note in the patch description though if that works better.

I'm going to keep it as a separate follow-up patch because introducing a timeout is enough of a functional change to risk a regression that having it in a dedicated commit makes it easier to revert later if we need to.

William Breathitt Gray

#6 lore raw

Re: [PATCH v3 1/2] iio: addac: stx104: Migrate to the regmap API

In reply to Andy Shevchenko
Quoted 7 lines · Andy Shevchenko
Quoted 3 lines · William Breathitt Gray

The regmap API supports IO port accessors so we can take advantage of regmap abstractions rather than handling access to the device registers directly in the driver.

...

Quoted 11 lines

drivers/iio/addac/stx104.c static unsigned int num_stx104; · line 124

Quoted 8 lines
drivers/iio/addac/stx104.c +8 -0
+static const struct regmap_config aio_ctl_regmap_config = {
+	.name = "aio_ctl",
+	.reg_bits = 8,
+	.reg_stride = 1,
+	.reg_base = STX104_AIO_BASE,
+	.val_bits = 8,
+	.io_port = true,
+	.max_register = 0x11,

Not sure if define would be better for this, so it will be grouped with register offset definitions. (Same for the other configs)

Actually, I'll remove this max_register line because it's superfluous when we define both rd_table and wr_table.

However, I explain my reasoning for hardcoding the max_register value in a response to the ebc-c384_wdt watchdog driver conversion [0]. To summarize, it's not immediately obvious whether a FOO_REGISTER comes before or after a BAR_REGISTER so someone reading the code would need to verify the define; there are cases as well, such as GPIO drivers, where the only the base register is defined but the max register would be an extent from there; and finally, a reviewer would ultimately need to check against the datasheet to verify that the max_register is actually at the named defined register location, whereas it is far easier to lookup the address range in the datasheet rather than named registers. These things make hardcoding max_register to be not only clearer code to read and verify but also less likely to be set to the wrong address.

[0] https://lore.kernel.org/all/ZAyY3VGlo4N4SLZQ@fedora/

Quoted 7 lines

drivers/iio/addac/stx104.c static unsigned int num_stx104; · line 129

Quoted 5 lines
drivers/iio/addac/stx104.c +5 -0
+	.wr_table = &aio_ctl_wr_table,
+	.rd_table = &aio_ctl_rd_table,
+	.volatile_table = &aio_ctl_volatile_table,
+	.cache_type = REGCACHE_FLAT,
+};

Do we need regmap lock?

I think the regmap lock is opt-out, so I don't think we need to set an custom lock callback for the regmaps in this driver.

Jonathan, do read_raw() and write_raw() require explicit locking?

Quoted 8 lines

drivers/iio/addac/stx104.c static unsigned int num_stx104; · line 185

Quoted 6 lines
drivers/iio/addac/stx104.c +4 -1
+		err = regmap_read(priv->aio_ctl_map, STX104_ADC_CONFIGURATION, &adc_config);
+		if (err)
+			return err;
 
-		*val = 1 << gain;
+		*val = 1 << u8_get_bits(adc_config, STX104_GAIN);

Maybe not for this change, but why not BIT()?

This probably should have been BIT(gain) when it was originally introduced. I'm avoiding it here just to keep this migration patch more straight-forward to review; but perhaps I'll make this change afterall in a v4 submission.

Quoted 9 lines

Review

Quoted 2 lines
(quoted diff) +0 -0
 	case IIO_CHAN_INFO_RAW:
 		if (chan->output) {

You can decrease indentation by

		if (!chan->output)
			return -EINVAL;

here.

Sure I can make this change as well in a v4.

William Breathitt Gray

maintainer
#7 lore raw

Re: [PATCH v3 1/2] iio: addac: stx104: Migrate to the regmap API

Quoted 9 lines
Quoted 8 lines

drivers/iio/addac/stx104.c static unsigned int num_stx104; · line 129

Quoted 6 lines
drivers/iio/addac/stx104.c +5 -0
+	.wr_table = &aio_ctl_wr_table,
+	.rd_table = &aio_ctl_rd_table,
+	.volatile_table = &aio_ctl_volatile_table,
+	.cache_type = REGCACHE_FLAT,
+};  

Do we need regmap lock?

Quoted 4 lines

I think the regmap lock is opt-out, so I don't think we need to set an custom lock callback for the regmaps in this driver.

Jonathan, do read_raw() and write_raw() require explicit locking?

The don't provide their own locking. Depending on the access pattern the underlying bus locking may be sufficient. If you have read modify write cycles though you'll want locking at the appropriate level for that which might well be at the level of regmap.

Jonathan

#8 lore raw

Re: [PATCH v3 1/2] iio: addac: stx104: Migrate to the regmap API

Quoted 12 lines · Jonathan Cameron
Quoted 12 lines
Quoted 8 lines

drivers/iio/addac/stx104.c static unsigned int num_stx104; · line 129

Quoted 6 lines
drivers/iio/addac/stx104.c +5 -0
+	.wr_table = &aio_ctl_wr_table,
+	.rd_table = &aio_ctl_rd_table,
+	.volatile_table = &aio_ctl_volatile_table,
+	.cache_type = REGCACHE_FLAT,
+};  

Do we need regmap lock?

I think the regmap lock is opt-out, so I don't think we need to set an custom lock callback for the regmaps in this driver.

Quoted 8 lines
Quoted 1 line

Jonathan, do read_raw() and write_raw() require explicit locking?

The don't provide their own locking. Depending on the access pattern the underlying bus locking may be sufficient. If you have read modify write cycles though you'll want locking at the appropriate level for that which might well be at the level of regmap.

Jonathan

If read_raw() can be called concurrently multiple times then we risk changing the ADC channel while the analog-to-digital conversion is in progress, thus resulting in an incorrect reported value. Looks like we'll need an explicit lock for this after all, so I'll create a precursor patch addressing that.

William Breathitt Gray