iio: addac: stx104: Migrate to the regmap API
Subsystems (2)
PATCH v2 1/1 iio: addac: stx104: Migrate to the regmap API
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 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 | 420 ++++++++++++++++++++----------------- 2 files changed, 230 insertions(+), 192 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 +228 -192
562 lines not on this page · raw message
base-commit: 46e33707fe95a21aa9896bded0be97285b779509
Signature · 1 line
2.39.2
Jonathan Cameron
maintainerQuoted 6 lines · On Thu, 23 Mar 2023 23:09:16 -0400 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.
Suggested-by Andy Shevchenko <[email protected]>Signed-off-by William Breathitt Gray <[email protected]>
I would have preferred slightly if you had avoided reording the probe (previously gpio chip was registered before iio device and now it is after) but it make no real difference so I'm not that bothered.
A few other minor comments. Biggest one being that the defines should be prefixed.
Thanks,
Jonathan
Quoted 31 lines
drivers/iio/addac/Kconfig +0
-0integrated analog PC/104 card.drivers/iio/addac/stx104.c +25
-0+#define AIO_BASE 0x0 +#define SOFTWARE_STROBE AIO_BASE +#define ADC_DATA AIO_BASE +#define ADC_CHANNEL (AIO_BASE + 0x2) +#define DIO_REG (AIO_BASE + 0x3) +#define DAC_BASE (AIO_BASE + 0x4) +#define ADC_STATUS (AIO_BASE + 0x8) +#define ADC_CONTROL (AIO_BASE + 0x9) +#define ADC_CONFIGURATION (AIO_BASE + 0x11) + +#define AIO_DATA_STRIDE 2 +#define DAC_OFFSET (_channel) (DAC_BASE + AIO_DATA_STRIDE * (_channel)) + +/* ADC Channel */ +#define FC GENMASK(3, 0) +#define LC GENMASK(7, 4) +#define SAME_CHANNEL (_channel) (u8_encode_bits(_channel, FC) | u8_encode_bits(_channel, LC)) + +/* ADC Status */ +#define SD BIT(5) +#define CNV BIT(7) +#define DIFFERENTIAL 1 + +/* ADC Control */ +#define ALSS GENMASK(1, 0)
drivers/iio/addac/stx104.c static unsigned int num_stx104; · line 81
Quoted 10 lines
drivers/iio/addac/stx104.c +10
-0+#define SOFTWARE_TRIGGER 0 + +/* ADC Configuration */ +#define GAIN GENMASK(1, 0) +#define ADBU BIT(2) +#define BIPOLAR 0 +#define GAIN_X1 0 +#define GAIN_X2 1 +#define GAIN_X4 2 +#define GAIN_X8 3
Better to give these an STX104_ prefix to avoid potential name clash problems in the future.
Quoted 3 lines
static int stx104_read_raw(struct
iio_dev*indio_dev,
structiio_chan_specconst *chan, int *val, int *val2, long mask)
{
...
Quoted 5 lines
drivers/iio/addac/stx104.c +0
-2/* trigger ADC sample capture by writing to the 8-bit * Software Strobe Register and wait for completion */ -iowrite8 (0 , ®->ssr_ad); -while (ioread8 (®->cir_asr) &BIT (7 ));
drivers/iio/addac/stx104.c static unsigned int num_stx104; · line 207
Quoted 10 lines
drivers/iio/addac/stx104.c +8
-2- - *val =ioread16 (®->ssr_ad); + err =regmap_write (priv->aio_ctl_map,SOFTWARE_STROBE ,0 ); +if (err) +return err; +do { + err =regmap_read (priv->aio_ctl_map,ADC_STATUS , &adc_status); +if (err) +return err; + }while (u8_get_bits (adc_status,CNV ));
This looks like a polled regmap read.
It should probably have a timeout as well just in case the device is broken. regmap_read_poll_timeout()?
That is a functional change however, so perhaps should be a follow up patch.
Quoted 4 lines
drivers/iio/addac/stx104.c +4
-0+ + err =regmap_read (priv->aio_data_map,ADC_DATA , &value); +if (err) +return err;
drivers/iio/addac/stx104.c static const struct iio_chan_spec stx104_channels_diff[] = { · line 349
Quoted 10 lines
drivers/iio/addac/stx104.c +2
-1+ *val = value;return IIO_VAL_INT ;static int stx104_probe (struct device *dev,unsigned int id) {struct iio_dev *indio_dev;struct stx104_iio *priv; -struct stx104_gpio *stx104gpio; +struct gpio_regmap_config gpio_config = {};
You could fill this whole thing later with
gpio_config = (struct gpio_regmap_config) {
.parent = dev,
... etc
};It might prove neater down there and would avoid need to zero the whole thing up here (though hopefully the compiler will figure out that is mostly not needed anyway).
Quoted 23 lines
drivers/iio/addac/stx104.c +5
-5+void __iomem *stx104_base; +struct regmap *aio_ctl_map; +struct regmap *aio_data_map; +struct regmap *dio_map;int err; +unsigned int adc_status; indio_dev =devm_iio_device_alloc (dev,sizeof (*priv));if (!indio_dev)return -ENOMEM ; - stx104gpio =devm_kzalloc (dev,sizeof (*stx104gpio),GFP_KERNEL ); -if (!stx104gpio) -return -ENOMEM ; -if (!devm_request_region (dev, base[id],STX104_EXTENT ,dev_name (dev))) { dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",@@ -332,16 +365,35 @@ static int stx104_probe(struct device *dev, unsigned int id)return -EBUSY ; } - priv =iio_priv (indio_dev);
drivers/iio/addac/stx104.c static int stx104_probe(struct device *dev, unsigned int id) · line 375
Quoted 10 lines
drivers/iio/addac/stx104.c +6
-2- priv->reg =devm_ioport_map (dev, base[id],STX104_EXTENT ); -if (!priv->reg) + stx104_base =devm_ioport_map (dev, base[id],STX104_EXTENT ); +if (!stx104_base)return -ENOMEM ; + aio_ctl_map =devm_regmap_init_mmio (dev, stx104_base +AIO_BASE , &aio_ctl_regmap_config); +if (IS_ERR (aio_ctl_map)) +return dev_err_probe (dev,PTR_ERR (aio_ctl_map), +"Unable to initialize aio_ctl register map\n" );
Blank line here would slightly help readability as it keeps each set of call and error check visually separated.
drivers/iio/addac/stx104.c static int stx104_probe(struct device *dev, unsigned int id) · line 379
Quoted 4 lines
drivers/iio/addac/stx104.c +4
-0+ aio_data_map =devm_regmap_init_mmio (dev, stx104_base +AIO_BASE , &aio_data_regmap_config); +if (IS_ERR (aio_data_map)) +return dev_err_probe (dev,PTR_ERR (aio_data_map), +"Unable to initialize aio_data register map\n" );
also here
Quoted 8 lines
drivers/iio/addac/stx104.c +8
-0+ dio_map =devm_regmap_init_mmio (dev, stx104_base +DIO_REG , &dio_regmap_config); +if (IS_ERR (dio_map)) +return dev_err_probe (dev,PTR_ERR (dio_map), +"Unable to initialize dio register map\n" ); + + priv =iio_priv (indio_dev); + priv->aio_ctl_map = aio_ctl_map; + priv->aio_data_map = aio_data_map;
Quoted 14 lines · Jonathan Cameron
Quoted 6 lines · On Thu, 23 Mar 2023 23:09:16 -0400 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.
Suggested-by Andy Shevchenko <[email protected]>Signed-off-by William Breathitt Gray <[email protected]>I would have preferred slightly if you had avoided reording the probe (previously gpio chip was registered before iio device and now it is after) but it make no real difference so I'm not that bothered.
Quoted 6 lines
A few other minor comments. Biggest one being that the defines should be prefixed.
Thanks,
Jonathan
Hi Jonathan,
I'll be submitting a v3 soon addressing your comments as well as some minor fixes to v2; I'll make the regmap_read_poll_timeout() change as a follow-up patch as suggested.
Regarding the GPIO code reordering in the probe, I decided to move it after the iio device registration so that all the IIO-related code is grouped together and finished before we deal with GPIO-related stuff. Given that all the original gpio chip code is removed anyway in this patch, I figure this is a minor enough cleanup to perform here. If you aren't too strongly opposed to this change I'll keep it in v3 as it avoids the hassle of creating a separate patch for such a trivial change.
William Breathitt Gray
Jonathan Cameron
maintainerQuoted 28 lines · On Sun, 26 Mar 2023 17:13:18 -0400 William Breathitt Gray…
Quoted 14 lines · Jonathan Cameron
Quoted 6 lines · On Thu, 23 Mar 2023 23:09:16 -0400 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.
Suggested-by Andy Shevchenko <[email protected]>Signed-off-by William Breathitt Gray <[email protected]>I would have preferred slightly if you had avoided reording the probe (previously gpio chip was registered before iio device and now it is after) but it make no real difference so I'm not that bothered.
Quoted 6 lines
A few other minor comments. Biggest one being that the defines should be prefixed.
Thanks,
Jonathan
Hi Jonathan,
I'll be submitting a v3 soon addressing your comments as well as some minor fixes to v2; I'll make the
regmap_read_poll_timeout()change as a follow-up patch as suggested.
Quoted 8 lines
Regarding the GPIO code reordering in the probe, I decided to move it after the iio device registration so that all the IIO-related code is grouped together and finished before we deal with GPIO-related stuff. Given that all the original gpio chip code is removed anyway in this patch, I figure this is a minor enough cleanup to perform here. If you aren't too strongly opposed to this change I'll keep it in v3 as it avoids the hassle of creating a separate patch for such a trivial change.
That's fine, just call it out in the patch description as a "While making these changes, also ..." or similar.
Jonathan
Quoted 2 lines
William Breathitt Gray