Commit 15ffee89 authored by David Lechner's avatar David Lechner Committed by Jonathan Cameron
Browse files

iio: adc: ad4030: don't store scan_type in state



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: default avatarDavid Lechner <dlechner@baylibre.com>
Reviewed-by: default avatarNuno Sá <nuno.sa@analog.com>
Link: https://patch.msgid.link/20250310-iio-adc-ad4030-check-scan-type-err-v1-4-589e4ebd9711@baylibre.com


Signed-off-by: default avatarJonathan Cameron <Jonathan.Cameron@huawei.com>
parent efaa981e
Loading
Loading
Loading
Loading
+9 −15
Original line number Diff line number Diff line
@@ -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;