Migrate STX104 to the regmap API
Subsystems (2)
v5 cover Migrate STX104 to the regmap API
Changes in v5:
- Explicitly check for negative val for IIO_CHAN_INFO_RAW case in stx104_write_raw(); this avoids the explicit case previously used
- Use U16_MAX rather than hardcoding unsigned 16-bit maximum value
- Improve ADC conversion comment format and use proper SI units format
Changes in v4:
- Fix race conditions in stx104_read_raw() and stx104_write_raw() by utilizing a mutex
- Improve indentation in stx104_write_raw() by bailing out early if chan->output is false in for the IIO_CHAN_INFO_RAW case
- Remove regmap_config max_register lines as superfluous
- Utilize BIT() for gain calculation in stx104_read_raw()
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.
To make the git history cleaner, precursor patches to use a define rather than hardcoded limit and to improve indentation in stx104_write_raw(), and a follow-up patch to utilize regmap_read_poll_timeout() for polling the ADC conversion status are included.
William Breathitt Gray (6):
iio: addac: stx104: Fix race condition for stx104_write_raw()
iio: addac: stx104: Fix race condition when converting
analog-to-digital
iio: addac: stx104: Use define rather than hardcoded limit for write
val
iio: addac: stx104: Improve indentation in stx104_write_raw()
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 | 460 +++++++++++++++++++++---------------- 2 files changed, 264 insertions(+), 198 deletions(-)
base-commit: f73df43e957a6fc705a9bd6d143585bdf1b13365
Signature · 1 line
2.39.2
PATCH v5 1/6 iio: addac: stx104: Fix race condition for stx104_write_raw()
The priv->chan_out_states array and actual DAC value can become mismatched if stx104_write_raw() is called concurrently. Prevent such a race condition by utilizing a mutex.
---
Changes in v5: none
drivers/iio/addac/stx104.c | 8 ++++++++ 1 file changed, 8 insertions(+)
drivers/iio/addac/stx104.c +8 -0
@@ -15,6 +15,7 @@#include <linux/kernel.h> #include <linux/module.h> #include <linux/moduleparam.h> +#include <linux/mutex.h> #include <linux/spinlock.h> #include <linux/types.h>
@@ -69,10 +70,12 @@ struct stx104_reg {/** * struct stx104_iio - IIO device private data structure +* @lock: synchronization lock to prevent I/O race conditions * @chan_out_states: channels' output states * @reg: I/O address offset for the device registers */ struct stx104_iio { +struct mutex lock;unsigned int chan_out_states[STX104_NUM_OUT_CHAN ];struct stx104_reg __iomem *reg; };
@@ -178,9 +181,12 @@ static int stx104_write_raw(struct iio_dev *indio_dev,if ((unsigned int )val >65535 )return -EINVAL ; +mutex_lock (&priv->lock); + priv->chan_out_states[chan->channel] = val;iowrite16 (val, &priv->reg->dac[chan->channel]); +mutex_unlock (&priv->lock);return 0 ; }return -EINVAL ;
@@ -351,6 +357,8 @@ static int stx104_probe(struct device *dev, unsigned int id) indio_dev->name =dev_name (dev); +mutex_init (&priv->lock); +/* configure device for software trigger operation */ iowrite8 (0 , &priv->reg->acr);
Signature · 1 line
2.39.2
Jonathan Cameron
maintainerRe: [PATCH v5 1/6] iio: addac: stx104: Fix race condition for stx104_write_raw()
Quoted 6 lines · On Thu, 6 Apr 2023 10:40:10 -0400 William Breathitt Gray…
The
priv->chan_out_statesarray and actual DAC value can become mismatched ifstx104_write_raw()is called concurrently. Prevent such a race condition by utilizing a mutex.Fixes 97a445dad37a ("iio: Add IIO support for the DAC on the Apex Embedded Systems STX104")Signed-off-by William Breathitt Gray <[email protected]>
Marked these first two patches for stable as probably worth a backport even if no one notices the affect of the race. I'll queue them for the next merge window though as we are fairly late in this cycle and I don't want to delay the 4 patches that follow a whole cycle.
Thanks,
Jonathan
Quoted 53 lines
---
Changes in v5: nonedrivers/iio/addac/stx104.c | 8 ++++++++ 1 file changed, 8 insertions(+)drivers/iio/addac/stx104.c +8
-0@@ -15,6 +15,7 @@#include <linux/kernel.h> #include <linux/module.h> #include <linux/moduleparam.h> +#include <linux/mutex.h> #include <linux/spinlock.h> #include <linux/types.h> @@ -69,10 +70,12 @@ struct stx104_reg {/** * struct stx104_iio - IIO device private data structure +* @lock: synchronization lock to prevent I/O race conditions * @chan_out_states: channels' output states * @reg: I/O address offset for the device registers */ struct stx104_iio { +struct mutex lock;unsigned int chan_out_states[STX104_NUM_OUT_CHAN ];struct stx104_reg __iomem *reg; };@@ -178,9 +181,12 @@ static int stx104_write_raw(struct iio_dev *indio_dev,if ((unsigned int )val >65535 )return -EINVAL ; +mutex_lock (&priv->lock); + priv->chan_out_states[chan->channel] = val;iowrite16 (val, &priv->reg->dac[chan->channel]); +mutex_unlock (&priv->lock);return 0 ; }return -EINVAL ;@@ -351,6 +357,8 @@ static int stx104_probe(struct device *dev, unsigned int id) indio_dev->name =dev_name (dev); +mutex_init (&priv->lock); +/* configure device for software trigger operation */ iowrite8 (0 , &priv->reg->acr);
PATCH v5 2/6 iio: addac: stx104: Fix race condition when converting analog-to-digital
In reply to William Breathitt GrayThe ADC conversion procedure requires several device I/O operations performed in a particular sequence. If stx104_read_raw() is called concurrently, the ADC conversion procedure could be clobbered. Prevent such a race condition by utilizing a mutex.
---
Changes in v5: none
drivers/iio/addac/stx104.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/iio/addac/stx104.c +4 -0
@@ -117,6 +117,8 @@ static int stx104_read_raw(struct iio_dev *indio_dev,return IIO_VAL_INT ; } +mutex_lock (&priv->lock); +/* select ADC channel */ iowrite8 (chan->channel | (chan->channel <<4 ), ®->achan);
@@ -127,6 +129,8 @@ static int stx104_read_raw(struct iio_dev *indio_dev,while (ioread8 (®->cir_asr) &BIT (7 )); *val =ioread16 (®->ssr_ad); + +mutex_unlock (&priv->lock);return IIO_VAL_INT ;case IIO_CHAN_INFO_OFFSET :/* get ADC bipolar/unipolar configuration */
Signature · 1 line
2.39.2
PATCH v5 3/6 iio: addac: stx104: Use define rather than hardcoded limit for write val
In reply to William Breathitt GrayThe DAC register is 16 bits wide, so the value passed by write_raw() should be checked against that limit. Rather than hardcoding the 16-bit maximum value limit, use a define to improve readability and make the intention of the code clearer. The explicit cast is also avoided by instead explicitly checking for negative values.
--- drivers/iio/addac/stx104.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/addac/stx104.c +2 -2
@@ -13,6 +13,7 @@#include <linux/ioport.h> #include <linux/isa.h> #include <linux/kernel.h> +#include <linux/limits.h> #include <linux/module.h> #include <linux/moduleparam.h> #include <linux/mutex.h>
@@ -181,8 +182,7 @@ static int stx104_write_raw(struct iio_dev *indio_dev,return 0 ;case IIO_CHAN_INFO_RAW :if (chan->output) { -/* DAC can only accept up to a 16-bit value */ -if ((unsigned int )val >65535 ) +if (val <0 || val >U16_MAX )return -EINVAL ;mutex_lock (&priv->lock);
Signature · 1 line
2.39.2
PATCH v5 4/6 iio: addac: stx104: Improve indentation in stx104_write_raw()
In reply to William Breathitt GrayBy bailing out early if chan->output is false for the IIO_CHAN_INFO_RAW, indentation can be decreased by a tab and code readability improved.
---
Changes in v5: none
drivers/iio/addac/stx104.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
drivers/iio/addac/stx104.c +10 -10
@@ -181,19 +181,19 @@ static int stx104_write_raw(struct iio_dev *indio_dev,return 0 ;case IIO_CHAN_INFO_RAW : -if (chan->output) { -if (val <0 || val >U16_MAX ) -return -EINVAL ; +if (!chan->output) +return -EINVAL ; -mutex_lock (&priv->lock); +if (val <0 || val >U16_MAX ) +return -EINVAL ; - priv->chan_out_states[chan->channel] = val; -iowrite16 (val, &priv->reg->dac[chan->channel]); +mutex_lock (&priv->lock); -mutex_unlock (&priv->lock); -return 0 ; - } -return -EINVAL ; + priv->chan_out_states[chan->channel] = val; +iowrite16 (val, &priv->reg->dac[chan->channel]); + +mutex_unlock (&priv->lock); +return 0 ; }return -EINVAL ;
Signature · 1 line
2.39.2
PATCH v5 5/6 iio: addac: stx104: Migrate to the regmap API
In reply to William Breathitt GrayThe 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.
In addition, to improve code organization in stx104_probe(), the devm_iio_device_register() call is moved above GPIO configuration in order to keep relevant code closer together.
---
Changes in v5: none
Changes in v4:
- Remove regmap_config max_register lines as superfluous
- Utilize BIT() for gain calculation in stx104_read_raw()
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 | 438 ++++++++++++++++++++----------------- 2 files changed, 245 insertions(+), 195 deletions(-)
drivers/iio/addac/Kconfig +2 -0
@@ -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 +243 -195
588 lines not on this page · raw message
Signature · 1 line
2.39.2
PATCH v5 6/6 iio: addac: stx104: Use regmap_read_poll_timeout() for conversion poll
In reply to William Breathitt GrayADC sample captures take a certain amount of time to complete after initiated; this conversion time range can be anywhere from 5 microseconds 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. Call regmap_read_poll_timeout() to poll until the ADC conversion is completed (or timeout if more than 53.68 seconds passes).
---
Changes in v5:
- Improve ADC conversion comment format and use proper SI units format
Changes in v4: none
drivers/iio/addac/stx104.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-)
drivers/iio/addac/stx104.c +13 -9
@@ -205,21 +205,25 @@ static int stx104_read_raw(struct iio_dev *indio_dev,return err; } -/* trigger ADC sample capture by writing to the 8-bit -* Software Strobe Register and wait for completion +/* +* Trigger ADC sample capture by writing to the 8-bit Software Strobe Register and +* wait for completion; the conversion time range is 5 microseconds 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 nanoseconds ). +* Where 0 <= AIFT <= 2147483648. */ err =regmap_write (priv->aio_ctl_map,STX104_SOFTWARE_STROBE ,0 );if (err) {mutex_unlock (&priv->lock);return err; } -do { - err =regmap_read (priv->aio_ctl_map,STX104_ADC_STATUS , &adc_status); -if (err) { -mutex_unlock (&priv->lock); -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) { +mutex_unlock (&priv->lock); +return err; + } err =regmap_read (priv->aio_data_map,STX104_ADC_DATA , &value);if (err) {
Signature · 1 line
2.39.2
Jonathan Cameron
maintainerOn Thu, 6 Apr 2023 10:40:09 -0400
William Breathitt Gray <[email protected]> wrote:
Series applied to the togreg branch of iio.git and pushed out initially as testing. If anyone wants to offer tags / remaining comments there will still be a few days before I push this out in a non rebasing form.
Thanks,
Jonathan
Quoted 54 lines
Changes in v5:
- Explicitly check for negative val forIIO_CHAN_INFO_RAWcase instx104_write_raw(); this avoids the explicit case previously used
- UseU16_MAXrather than hardcoding unsigned 16-bit maximum value
- Improve ADC conversion comment format and use proper SI units format
Changes in v4:
- Fix race conditions instx104_read_raw()andstx104_write_raw()by utilizing a mutex
- Improve indentation instx104_write_raw()by bailing out early ifchan->outputis false in for theIIO_CHAN_INFO_RAWcase
- Removeregmap_configmax_registerlines as superfluous
- UtilizeBIT()for gain calculation instx104_read_raw()
Changes in v3:
- AddSTX104_prefixes to defines to avoid potential name classes
- RenameSAME_CHANNEL()toSTX104_SINGLE_CHANNEL()to convey intention better
- Utilizeu8_encode_bits()to defineSTX104_SOFTWARE_TRIGGER
- Adjust to utilizereg_basemembers inregmap_configstructures
- Fix off-by-one errors in aio_data_wr_ranges[], aio_data_rd_ranges[], andaio_data_regmap_configmax_register
- Inlinegpio_configinitialization to avoid zeroing it at declaration
- Add blank lines between register map init blocks for clarity
- Utilizeregmap_read_poll_timeout()for ADC conversion status poll
Changes in v2:
- Relocate structstx104_iiofor the sake of a clearer patch diff
- ReplaceFIELD_PREP()andFIELD_GET()withu8_encode_bits()andu8_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.
To make the git history cleaner, precursor patches to use a define rather than hardcoded limit and to improve indentation in
stx104_write_raw(), and a follow-up patch to utilizeregmap_read_poll_timeout()for polling the ADC conversion status are included.William Breathitt Gray (6):
iio: addac: stx104: Fix race condition forstx104_write_raw()
iio: addac: stx104: Fix race condition when converting
analog-to-digital
iio: addac: stx104: Use define rather than hardcoded limit for write
val
iio: addac: stx104: Improve indentation instx104_write_raw()
iio: addac: stx104: Migrate to the regmap API
iio: addac: stx104: Useregmap_read_poll_timeout()for conversion polldrivers/iio/addac/Kconfig | 2 + drivers/iio/addac/stx104.c | 460 +++++++++++++++++++++---------------- 2 files changed, 264 insertions(+), 198 deletions(-)base-commit:
f73df43e957a6fc705a9bd6d143585bdf1b13365