2024-04-12 23:25:17

by David Lechner

[permalink] [raw]
Subject: [PATCH] iio: adc: ad7944: Consolidate spi_sync() wrapper

Since commit 6020ca4de8e5 ("iio: adc: ad7944: use spi_optimize_message()"),
The helper functions wrapping spi_sync() for 3-wire and 4-wire modes are
virtually identical. Since gpiod_set_value_cansleep() does a NULL check
internally, we can consolidate the two functions into one and avoid
switch statements at the call sites.

The default cases of the removed switch statement were just to make the
compiler happy and are not reachable since the mode is validated in the
probe function. So removing those should be safe.

Signed-off-by: David Lechner <[email protected]>
---
drivers/iio/adc/ad7944.c | 67 ++++++++++--------------------------------------
1 file changed, 13 insertions(+), 54 deletions(-)

diff --git a/drivers/iio/adc/ad7944.c b/drivers/iio/adc/ad7944.c
index 9dc86ec23c36..4af574ffa864 100644
--- a/drivers/iio/adc/ad7944.c
+++ b/drivers/iio/adc/ad7944.c
@@ -214,40 +214,26 @@ static int ad7944_4wire_mode_init_msg(struct device *dev, struct ad7944_adc *adc
return devm_add_action_or_reset(dev, ad7944_unoptimize_msg, &adc->msg);
}

-/*
- * ad7944_3wire_cs_mode_conversion - Perform a 3-wire CS mode conversion and
- * acquisition
+/**
+ * ad7944_convert_and_acquire - Perform a single conversion and acquisition
* @adc: The ADC device structure
* @chan: The channel specification
* Return: 0 on success, a negative error code on failure
*
- * This performs a conversion and reads data when the chip is wired in 3-wire
- * mode with the CNV line on the ADC tied to the CS line on the SPI controller.
- *
- * Upon successful return adc->sample.raw will contain the conversion result.
- */
-static int ad7944_3wire_cs_mode_conversion(struct ad7944_adc *adc,
- const struct iio_chan_spec *chan)
-{
- return spi_sync(adc->spi, &adc->msg);
-}
-
-/*
- * ad7944_4wire_mode_conversion - Perform a 4-wire mode conversion and acquisition
- * @adc: The ADC device structure
- * @chan: The channel specification
- * Return: 0 on success, a negative error code on failure
+ * Perform a conversion and acquisition of a single sample using the
+ * pre-optimized adc->msg.
*
* Upon successful return adc->sample.raw will contain the conversion result.
*/
-static int ad7944_4wire_mode_conversion(struct ad7944_adc *adc,
- const struct iio_chan_spec *chan)
+static int ad7944_convert_and_acquire(struct ad7944_adc *adc,
+ const struct iio_chan_spec *chan)
{
int ret;

/*
* In 4-wire mode, the CNV line is held high for the entire conversion
- * and acquisition process.
+ * and acquisition process. In other modes adc->cnv is NULL and is
+ * ignored (CS is wired to CNV in those cases).
*/
gpiod_set_value_cansleep(adc->cnv, 1);
ret = spi_sync(adc->spi, &adc->msg);
@@ -262,22 +248,9 @@ static int ad7944_single_conversion(struct ad7944_adc *adc,
{
int ret;

- switch (adc->spi_mode) {
- case AD7944_SPI_MODE_DEFAULT:
- ret = ad7944_4wire_mode_conversion(adc, chan);
- if (ret)
- return ret;
-
- break;
- case AD7944_SPI_MODE_SINGLE:
- ret = ad7944_3wire_cs_mode_conversion(adc, chan);
- if (ret)
- return ret;
-
- break;
- default:
- return -EOPNOTSUPP;
- }
+ ret = ad7944_convert_and_acquire(adc, chan);
+ if (ret)
+ return ret;

if (chan->scan_type.storagebits > 16)
*val = adc->sample.raw.u32;
@@ -338,23 +311,9 @@ static irqreturn_t ad7944_trigger_handler(int irq, void *p)
struct ad7944_adc *adc = iio_priv(indio_dev);
int ret;

- switch (adc->spi_mode) {
- case AD7944_SPI_MODE_DEFAULT:
- ret = ad7944_4wire_mode_conversion(adc, &indio_dev->channels[0]);
- if (ret)
- goto out;
-
- break;
- case AD7944_SPI_MODE_SINGLE:
- ret = ad7944_3wire_cs_mode_conversion(adc, &indio_dev->channels[0]);
- if (ret)
- goto out;
-
- break;
- default:
- /* not supported */
+ ret = ad7944_convert_and_acquire(adc, &indio_dev->channels[0]);
+ if (ret)
goto out;
- }

iio_push_to_buffers_with_timestamp(indio_dev, &adc->sample.raw,
pf->timestamp);

---
base-commit: aabc0aa90c927a03d509d0b592720d9897894ce4
change-id: 20240412-ad7944-consolidate-msg-378515e51628


2024-04-13 11:05:30

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH] iio: adc: ad7944: Consolidate spi_sync() wrapper

On Fri, 12 Apr 2024 18:25:02 -0500
David Lechner <[email protected]> wrote:

> Since commit 6020ca4de8e5 ("iio: adc: ad7944: use spi_optimize_message()"),
> The helper functions wrapping spi_sync() for 3-wire and 4-wire modes are
> virtually identical. Since gpiod_set_value_cansleep() does a NULL check
> internally, we can consolidate the two functions into one and avoid
> switch statements at the call sites.
>
> The default cases of the removed switch statement were just to make the
> compiler happy and are not reachable since the mode is validated in the
> probe function. So removing those should be safe.
>
> Signed-off-by: David Lechner <[email protected]>
I was initially dubious about implying the need to change a GPIO when we don't,
but given that the resulting function is really simple and the comment on that
is immediately above the code (not obvious from this diff) this seems fine to
me.

Applied.