Skip to content

Commit eede063

Browse files
dlechnunojsa
authored andcommitted
iio: adc: ad7944: Consolidate spi_sync() wrapper
Since commit 6020ca4 ("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> Link: https://lore.kernel.org/r/20240412-ad7944-consolidate-msg-v1-1-7fdeff89172f@baylibre.com Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
1 parent fe61aed commit eede063

File tree

1 file changed

+13
-54
lines changed

1 file changed

+13
-54
lines changed

drivers/iio/adc/ad7944.c

Lines changed: 13 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -214,40 +214,26 @@ static int ad7944_4wire_mode_init_msg(struct device *dev, struct ad7944_adc *adc
214214
return devm_add_action_or_reset(dev, ad7944_unoptimize_msg, &adc->msg);
215215
}
216216

217-
/*
218-
* ad7944_3wire_cs_mode_conversion - Perform a 3-wire CS mode conversion and
219-
* acquisition
217+
/**
218+
* ad7944_convert_and_acquire - Perform a single conversion and acquisition
220219
* @adc: The ADC device structure
221220
* @chan: The channel specification
222221
* Return: 0 on success, a negative error code on failure
223222
*
224-
* This performs a conversion and reads data when the chip is wired in 3-wire
225-
* mode with the CNV line on the ADC tied to the CS line on the SPI controller.
226-
*
227-
* Upon successful return adc->sample.raw will contain the conversion result.
228-
*/
229-
static int ad7944_3wire_cs_mode_conversion(struct ad7944_adc *adc,
230-
const struct iio_chan_spec *chan)
231-
{
232-
return spi_sync(adc->spi, &adc->msg);
233-
}
234-
235-
/*
236-
* ad7944_4wire_mode_conversion - Perform a 4-wire mode conversion and acquisition
237-
* @adc: The ADC device structure
238-
* @chan: The channel specification
239-
* Return: 0 on success, a negative error code on failure
223+
* Perform a conversion and acquisition of a single sample using the
224+
* pre-optimized adc->msg.
240225
*
241226
* Upon successful return adc->sample.raw will contain the conversion result.
242227
*/
243-
static int ad7944_4wire_mode_conversion(struct ad7944_adc *adc,
244-
const struct iio_chan_spec *chan)
228+
static int ad7944_convert_and_acquire(struct ad7944_adc *adc,
229+
const struct iio_chan_spec *chan)
245230
{
246231
int ret;
247232

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

265-
switch (adc->spi_mode) {
266-
case AD7944_SPI_MODE_DEFAULT:
267-
ret = ad7944_4wire_mode_conversion(adc, chan);
268-
if (ret)
269-
return ret;
270-
271-
break;
272-
case AD7944_SPI_MODE_SINGLE:
273-
ret = ad7944_3wire_cs_mode_conversion(adc, chan);
274-
if (ret)
275-
return ret;
276-
277-
break;
278-
default:
279-
return -EOPNOTSUPP;
280-
}
251+
ret = ad7944_convert_and_acquire(adc, chan);
252+
if (ret)
253+
return ret;
281254

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

341-
switch (adc->spi_mode) {
342-
case AD7944_SPI_MODE_DEFAULT:
343-
ret = ad7944_4wire_mode_conversion(adc, &indio_dev->channels[0]);
344-
if (ret)
345-
goto out;
346-
347-
break;
348-
case AD7944_SPI_MODE_SINGLE:
349-
ret = ad7944_3wire_cs_mode_conversion(adc, &indio_dev->channels[0]);
350-
if (ret)
351-
goto out;
352-
353-
break;
354-
default:
355-
/* not supported */
314+
ret = ad7944_convert_and_acquire(adc, &indio_dev->channels[0]);
315+
if (ret)
356316
goto out;
357-
}
358317

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

0 commit comments

Comments
 (0)