All of lore.kernel.org
 help / color / mirror / Atom feed
From: David Lechner <dlechner@baylibre.com>
To: Jonathan Cameron <jic23@kernel.org>
Cc: "David Lechner" <dlechner@baylibre.com>,
	"Michael Hennerich" <Michael.Hennerich@analog.com>,
	"Nuno Sá" <nuno.sa@analog.com>,
	linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] iio: adc: ad7944: Consolidate spi_sync() wrapper
Date: Fri, 12 Apr 2024 18:25:02 -0500	[thread overview]
Message-ID: <20240412-ad7944-consolidate-msg-v1-1-7fdeff89172f@baylibre.com> (raw)

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

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

             reply	other threads:[~2024-04-12 23:25 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-12 23:25 David Lechner [this message]
2024-04-13 11:05 ` [PATCH] iio: adc: ad7944: Consolidate spi_sync() wrapper Jonathan Cameron

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240412-ad7944-consolidate-msg-v1-1-7fdeff89172f@baylibre.com \
    --to=dlechner@baylibre.com \
    --cc=Michael.Hennerich@analog.com \
    --cc=jic23@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nuno.sa@analog.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.