Migrate STX104 to the regmap API
Subsystems (2)
v4 cover Migrate STX104 to the regmap API
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, a precursor patch 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 (5):
iio: addac: stx104: Fix race condition for stx104_write_raw()
iio: addac: stx104: Fix race condition when converting
analog-to-digital
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 | 455 +++++++++++++++++++++---------------- 2 files changed, 261 insertions(+), 196 deletions(-)
base-commit: f73df43e957a6fc705a9bd6d143585bdf1b13365
Signature · 1 line
2.39.2
PATCH v4 1/5 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.
--- 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
PATCH v4 2/5 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.
--- 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 v4 3/5 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.
--- drivers/iio/addac/stx104.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)
drivers/iio/addac/stx104.c +11 -11
@@ -180,20 +180,20 @@ 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 ) -return -EINVAL ; +if (!chan->output) +return -EINVAL ; -mutex_lock (&priv->lock); +/* 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]); +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 v4 4/5 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 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
587 lines not on this page · raw message
Signature · 1 line
2.39.2
PATCH v4 5/5 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 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).
---
Changes in v4: none
drivers/iio/addac/stx104.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-)
drivers/iio/addac/stx104.c +10 -7
@@ -206,19 +206,22 @@ 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) {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
Re: [PATCH v4 3/5] iio: addac: stx104: Improve indentation in stx104_write_raw()
In reply to William Breathitt GrayQuoted 2 lines · William Breathitt Gray
By bailing out early if
chan->outputis false for theIIO_CHAN_INFO_RAW, indentation can be decreased by a tab and code readability improved.
...
drivers/iio/addac/stx104.c static int stx104_write_raw(struct iio_dev *indio_dev, · line 188
Quoted 3 lines
drivers/iio/addac/stx104.c +3
-0+/* DAC can only accept up to a 16-bit value */ +if ((unsigned int )val >65535 ) +return -EINVAL ;
While the patch is good per se, I don't like two things (which are also in the original code):
- explicit casting (can it be avoided?)
- would be good to have U16_MAX or ?.. instead of hard coded number
Can it be addressed with (additional) patches?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v4 5/5] iio: addac: stx104: Use regmap_read_poll_timeout() for conversion poll
In reply to William Breathitt GrayQuoted 7 lines · William Breathitt Gray
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).
...
Quoted 1 line
/* trigger ADC sample capture by writing to the 8-bit
Perhaps fix the style here while at it?
/* * Trigger ADC sample capture by writing to the 8-bit
drivers/iio/addac/stx104.c static int stx104_read_raw(struct iio_dev *indio_dev, · line 209
Quoted 2 lines
drivers/iio/addac/stx104.c +1
-0* Software Strobe Register and wait for completion +* Range is 5 uSec to 53.68 Seconds in steps of 25 nanoseconds.
seconds (in SI the small letter is for the unit(s) of seconds).
Same for the commit message.
drivers/iio/addac/stx104.c static int stx104_read_raw(struct iio_dev *indio_dev, · line 211
Quoted 2 lines
drivers/iio/addac/stx104.c +2
-0+* The actual Analog Input Frame Timer time interval is calculated as: +* ai_time_frame_ns = ( AIFT + 1 ) * ( 25 nSec ).
nSec --> nanosecond
Quoted 2 lines
drivers/iio/addac/stx104.c +1
-0+* Where 0 <= AIFT <= 2147483648. */
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v4 3/5] iio: addac: stx104: Improve indentation in stx104_write_raw()
In reply to Andy ShevchenkoQuoted 6 lines · Andy Shevchenko
Quoted 2 lines · William Breathitt Gray
By bailing out early if
chan->outputis false for theIIO_CHAN_INFO_RAW, indentation can be decreased by a tab and code readability improved....
Quoted 7 lines
drivers/iio/addac/stx104.c static int stx104_write_raw(struct iio_dev *indio_dev, · line 188
Quoted 3 lines
drivers/iio/addac/stx104.c +3
-0+/* DAC can only accept up to a 16-bit value */ +if ((unsigned int )val >65535 ) +return -EINVAL ;While the patch is good per se, I don't like two things (which are also in the original code):
- explicit casting (can it be avoided?)
We could explicitly check for negative instead:
if (val < 0 || val > 65535)
Would that be better?
Quoted 1 line
- would be good to have
U16_MAXor ?.. instead of hard coded number
Fair point, I'll use U16_MAX then.
Quoted 1 line
Can it be addressed with (additional) patches?
Sure, I'll submit a separate patch to address this.
William Breathitt Gray