diff mbox series

iio: adc: ad7944: Consolidate spi_sync() wrapper

Message ID 20240412-ad7944-consolidate-msg-v1-1-7fdeff89172f@baylibre.com (mailing list archive)
State Accepted
Headers show
Series iio: adc: ad7944: Consolidate spi_sync() wrapper | expand

Commit Message

David Lechner April 12, 2024, 11:25 p.m. UTC
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 <dlechner@baylibre.com>
---
 drivers/iio/adc/ad7944.c | 67 ++++++++++--------------------------------------
 1 file changed, 13 insertions(+), 54 deletions(-)


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

Comments

Jonathan Cameron April 13, 2024, 11:05 a.m. UTC | #1
On Fri, 12 Apr 2024 18:25:02 -0500
David Lechner <dlechner@baylibre.com> 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 <dlechner@baylibre.com>
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.
diff mbox series

Patch

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);