Message ID | 20250310-iio-adc-ad4030-check-scan-type-err-v1-4-589e4ebd9711@baylibre.com (mailing list archive) |
---|---|
State | New |
Headers | show |
Series | iio: adc: ad4030: scan_type cleanups | expand |
On Mon, 2025-03-10 at 15:43 -0500, David Lechner wrote: > Move getting the scan_type to ad4030_conversion(). Previously, we were > getting the scan_type in two other places, then storing it in the > state struct before using it in ad4030_conversion(). This was a bit > fragile against potential future changes since it isn't obvious that > anything that could potentially change the scan_type would need to > also update the state struct. Also, the non-obviousness of this led to > a redundant call to iio_get_current_scan_type() in > ad4030_single_conversion() since it also calls ad4030_set_mode() which > in turn calls ad4030_conversion(). > > To simplify things, just call iio_get_current_scan_type() in > ad4030_conversion() where the returned struct is actually used and > don't bother storing it in the state struct. > > Signed-off-by: David Lechner <dlechner@baylibre.com> > --- Reviewed-by: Nuno Sá <nuno.sa@analog.com> > drivers/iio/adc/ad4030.c | 24 +++++++++--------------- > 1 file changed, 9 insertions(+), 15 deletions(-) > > diff --git a/drivers/iio/adc/ad4030.c b/drivers/iio/adc/ad4030.c > index > c2117c7a296f22aeeec6911c8a8c74ed576296a0..54ad74b96c9f256a67848330f875379edc82 > 8b0b 100644 > --- a/drivers/iio/adc/ad4030.c > +++ b/drivers/iio/adc/ad4030.c > @@ -147,7 +147,6 @@ struct ad4030_state { > struct spi_device *spi; > struct regmap *regmap; > const struct ad4030_chip_info *chip; > - const struct iio_scan_type *current_scan_type; > struct gpio_desc *cnv_gpio; > int vref_uv; > int vio_uv; > @@ -562,11 +561,6 @@ static int ad4030_set_mode(struct iio_dev *indio_dev, > unsigned long mask) > st->mode = AD4030_OUT_DATA_MD_DIFF; > } > > - st->current_scan_type = iio_get_current_scan_type(indio_dev, > - st->chip- > >channels); > - if (IS_ERR(st->current_scan_type)) > - return PTR_ERR(st->current_scan_type); > - > return regmap_update_bits(st->regmap, AD4030_REG_MODES, > AD4030_REG_MODES_MASK_OUT_DATA_MODE, > st->mode); > @@ -614,15 +608,20 @@ static void ad4030_extract_interleaved(u8 *src, u32 > *ch0, u32 *ch1) > static int ad4030_conversion(struct iio_dev *indio_dev) > { > struct ad4030_state *st = iio_priv(indio_dev); > - unsigned char diff_realbytes = > - BITS_TO_BYTES(st->current_scan_type->realbits); > - unsigned char diff_storagebytes = > - BITS_TO_BYTES(st->current_scan_type->storagebits); > + const struct iio_scan_type *scan_type; > + unsigned char diff_realbytes, diff_storagebytes; > unsigned int bytes_to_read; > unsigned long cnv_nb = BIT(st->avg_log2); > unsigned int i; > int ret; > > + scan_type = iio_get_current_scan_type(indio_dev, st->chip->channels); > + if (IS_ERR(scan_type)) > + return PTR_ERR(scan_type); > + > + diff_realbytes = BITS_TO_BYTES(scan_type->realbits); > + diff_storagebytes = BITS_TO_BYTES(scan_type->storagebits); > + > /* Number of bytes for one differential channel */ > bytes_to_read = diff_realbytes; > /* Add one byte if we are using a differential + common byte mode */ > @@ -673,11 +672,6 @@ static int ad4030_single_conversion(struct iio_dev > *indio_dev, > if (ret) > return ret; > > - st->current_scan_type = iio_get_current_scan_type(indio_dev, > - st->chip- > >channels); > - if (IS_ERR(st->current_scan_type)) > - return PTR_ERR(st->current_scan_type); > - > ret = ad4030_conversion(indio_dev); > if (ret) > return ret; >
diff --git a/drivers/iio/adc/ad4030.c b/drivers/iio/adc/ad4030.c index c2117c7a296f22aeeec6911c8a8c74ed576296a0..54ad74b96c9f256a67848330f875379edc828b0b 100644 --- a/drivers/iio/adc/ad4030.c +++ b/drivers/iio/adc/ad4030.c @@ -147,7 +147,6 @@ struct ad4030_state { struct spi_device *spi; struct regmap *regmap; const struct ad4030_chip_info *chip; - const struct iio_scan_type *current_scan_type; struct gpio_desc *cnv_gpio; int vref_uv; int vio_uv; @@ -562,11 +561,6 @@ static int ad4030_set_mode(struct iio_dev *indio_dev, unsigned long mask) st->mode = AD4030_OUT_DATA_MD_DIFF; } - st->current_scan_type = iio_get_current_scan_type(indio_dev, - st->chip->channels); - if (IS_ERR(st->current_scan_type)) - return PTR_ERR(st->current_scan_type); - return regmap_update_bits(st->regmap, AD4030_REG_MODES, AD4030_REG_MODES_MASK_OUT_DATA_MODE, st->mode); @@ -614,15 +608,20 @@ static void ad4030_extract_interleaved(u8 *src, u32 *ch0, u32 *ch1) static int ad4030_conversion(struct iio_dev *indio_dev) { struct ad4030_state *st = iio_priv(indio_dev); - unsigned char diff_realbytes = - BITS_TO_BYTES(st->current_scan_type->realbits); - unsigned char diff_storagebytes = - BITS_TO_BYTES(st->current_scan_type->storagebits); + const struct iio_scan_type *scan_type; + unsigned char diff_realbytes, diff_storagebytes; unsigned int bytes_to_read; unsigned long cnv_nb = BIT(st->avg_log2); unsigned int i; int ret; + scan_type = iio_get_current_scan_type(indio_dev, st->chip->channels); + if (IS_ERR(scan_type)) + return PTR_ERR(scan_type); + + diff_realbytes = BITS_TO_BYTES(scan_type->realbits); + diff_storagebytes = BITS_TO_BYTES(scan_type->storagebits); + /* Number of bytes for one differential channel */ bytes_to_read = diff_realbytes; /* Add one byte if we are using a differential + common byte mode */ @@ -673,11 +672,6 @@ static int ad4030_single_conversion(struct iio_dev *indio_dev, if (ret) return ret; - st->current_scan_type = iio_get_current_scan_type(indio_dev, - st->chip->channels); - if (IS_ERR(st->current_scan_type)) - return PTR_ERR(st->current_scan_type); - ret = ad4030_conversion(indio_dev); if (ret) return ret;
Move getting the scan_type to ad4030_conversion(). Previously, we were getting the scan_type in two other places, then storing it in the state struct before using it in ad4030_conversion(). This was a bit fragile against potential future changes since it isn't obvious that anything that could potentially change the scan_type would need to also update the state struct. Also, the non-obviousness of this led to a redundant call to iio_get_current_scan_type() in ad4030_single_conversion() since it also calls ad4030_set_mode() which in turn calls ad4030_conversion(). To simplify things, just call iio_get_current_scan_type() in ad4030_conversion() where the returned struct is actually used and don't bother storing it in the state struct. Signed-off-by: David Lechner <dlechner@baylibre.com> --- drivers/iio/adc/ad4030.c | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-)