Skip to content

Commit 5ec0eeb

Browse files
dlechnunojsa
authored andcommitted
iio: adc: ad7944: add support for chain mode
This adds support for the chain mode of the AD7944 ADC. This mode allows multiple ADCs to be daisy-chained together. Data from all of the ADCs in is read by reading multiple words from the first ADC in the chain. Each chip in the chain adds an extra IIO input voltage channel to the IIO device. Only the wiring configuration where the SPI controller CS line is connected to the CNV pin of all of the ADCs in the chain is supported in this patch. Signed-off-by: David Lechner <dlechner@baylibre.com> Link: https://lore.kernel.org/r/20240425-iio-ad7944-chain-mode-v1-1-9d9220ff21e1@baylibre.com Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
1 parent eede063 commit 5ec0eeb

File tree

1 file changed

+176
-10
lines changed

1 file changed

+176
-10
lines changed

drivers/iio/adc/ad7944.c

Lines changed: 176 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Copyright 2024 BayLibre, SAS
77
*/
88

9+
#include <linux/align.h>
910
#include <linux/bitfield.h>
1011
#include <linux/bitops.h>
1112
#include <linux/delay.h>
@@ -53,6 +54,7 @@ struct ad7944_adc {
5354
enum ad7944_spi_mode spi_mode;
5455
struct spi_transfer xfers[3];
5556
struct spi_message msg;
57+
void *chain_mode_buf;
5658
/* Chip-specific timing specifications. */
5759
const struct ad7944_timing_spec *timing_spec;
5860
/* GPIO connected to CNV pin. */
@@ -214,6 +216,46 @@ static int ad7944_4wire_mode_init_msg(struct device *dev, struct ad7944_adc *adc
214216
return devm_add_action_or_reset(dev, ad7944_unoptimize_msg, &adc->msg);
215217
}
216218

219+
static int ad7944_chain_mode_init_msg(struct device *dev, struct ad7944_adc *adc,
220+
const struct iio_chan_spec *chan,
221+
u32 n_chain_dev)
222+
{
223+
struct spi_transfer *xfers = adc->xfers;
224+
int ret;
225+
226+
/*
227+
* NB: SCLK has to be low before we toggle CS to avoid triggering the
228+
* busy indication.
229+
*/
230+
if (adc->spi->mode & SPI_CPOL)
231+
return dev_err_probe(dev, -EINVAL,
232+
"chain mode requires ~SPI_CPOL\n");
233+
234+
/*
235+
* We only support CNV connected to CS in chain mode and we need CNV
236+
* to be high during the transfer to trigger the conversion.
237+
*/
238+
if (!(adc->spi->mode & SPI_CS_HIGH))
239+
return dev_err_probe(dev, -EINVAL,
240+
"chain mode requires SPI_CS_HIGH\n");
241+
242+
/* CNV has to be high for full conversion time before reading data. */
243+
xfers[0].delay.value = adc->timing_spec->conv_ns;
244+
xfers[0].delay.unit = SPI_DELAY_UNIT_NSECS;
245+
246+
xfers[1].rx_buf = adc->chain_mode_buf;
247+
xfers[1].len = BITS_TO_BYTES(chan->scan_type.storagebits) * n_chain_dev;
248+
xfers[1].bits_per_word = chan->scan_type.realbits;
249+
250+
spi_message_init_with_transfers(&adc->msg, xfers, 2);
251+
252+
ret = spi_optimize_message(adc->spi, &adc->msg);
253+
if (ret)
254+
return ret;
255+
256+
return devm_add_action_or_reset(dev, ad7944_unoptimize_msg, &adc->msg);
257+
}
258+
217259
/**
218260
* ad7944_convert_and_acquire - Perform a single conversion and acquisition
219261
* @adc: The ADC device structure
@@ -223,7 +265,8 @@ static int ad7944_4wire_mode_init_msg(struct device *dev, struct ad7944_adc *adc
223265
* Perform a conversion and acquisition of a single sample using the
224266
* pre-optimized adc->msg.
225267
*
226-
* Upon successful return adc->sample.raw will contain the conversion result.
268+
* Upon successful return adc->sample.raw will contain the conversion result
269+
* (or adc->chain_mode_buf if the device is using chain mode).
227270
*/
228271
static int ad7944_convert_and_acquire(struct ad7944_adc *adc,
229272
const struct iio_chan_spec *chan)
@@ -252,10 +295,17 @@ static int ad7944_single_conversion(struct ad7944_adc *adc,
252295
if (ret)
253296
return ret;
254297

255-
if (chan->scan_type.storagebits > 16)
256-
*val = adc->sample.raw.u32;
257-
else
258-
*val = adc->sample.raw.u16;
298+
if (adc->spi_mode == AD7944_SPI_MODE_CHAIN) {
299+
if (chan->scan_type.storagebits > 16)
300+
*val = ((u32 *)adc->chain_mode_buf)[chan->scan_index];
301+
else
302+
*val = ((u16 *)adc->chain_mode_buf)[chan->scan_index];
303+
} else {
304+
if (chan->scan_type.storagebits > 16)
305+
*val = adc->sample.raw.u32;
306+
else
307+
*val = adc->sample.raw.u16;
308+
}
259309

260310
if (chan->scan_type.sign == 's')
261311
*val = sign_extend32(*val, chan->scan_type.realbits - 1);
@@ -315,15 +365,103 @@ static irqreturn_t ad7944_trigger_handler(int irq, void *p)
315365
if (ret)
316366
goto out;
317367

318-
iio_push_to_buffers_with_timestamp(indio_dev, &adc->sample.raw,
319-
pf->timestamp);
368+
if (adc->spi_mode == AD7944_SPI_MODE_CHAIN)
369+
iio_push_to_buffers_with_timestamp(indio_dev, adc->chain_mode_buf,
370+
pf->timestamp);
371+
else
372+
iio_push_to_buffers_with_timestamp(indio_dev, &adc->sample.raw,
373+
pf->timestamp);
320374

321375
out:
322376
iio_trigger_notify_done(indio_dev->trig);
323377

324378
return IRQ_HANDLED;
325379
}
326380

381+
/**
382+
* ad7944_chain_mode_alloc - allocate and initialize channel specs and buffers
383+
* for daisy-chained devices
384+
* @dev: The device for devm_ functions
385+
* @chan_template: The channel template for the devices (array of 2 channels
386+
* voltage and timestamp)
387+
* @n_chain_dev: The number of devices in the chain
388+
* @chain_chan: Pointer to receive the allocated channel specs
389+
* @chain_mode_buf: Pointer to receive the allocated rx buffer
390+
* @chain_scan_masks: Pointer to receive the allocated scan masks
391+
* Return: 0 on success, a negative error code on failure
392+
*/
393+
static int ad7944_chain_mode_alloc(struct device *dev,
394+
const struct iio_chan_spec *chan_template,
395+
u32 n_chain_dev,
396+
struct iio_chan_spec **chain_chan,
397+
void **chain_mode_buf,
398+
unsigned long **chain_scan_masks)
399+
{
400+
struct iio_chan_spec *chan;
401+
size_t chain_mode_buf_size;
402+
unsigned long *scan_masks;
403+
void *buf;
404+
int i;
405+
406+
/* 1 channel for each device in chain plus 1 for soft timestamp */
407+
408+
chan = devm_kcalloc(dev, n_chain_dev + 1, sizeof(*chan), GFP_KERNEL);
409+
if (!chan)
410+
return -ENOMEM;
411+
412+
for (i = 0; i < n_chain_dev; i++) {
413+
chan[i] = chan_template[0];
414+
415+
if (chan_template[0].differential) {
416+
chan[i].channel = 2 * i;
417+
chan[i].channel2 = 2 * i + 1;
418+
} else {
419+
chan[i].channel = i;
420+
}
421+
422+
chan[i].scan_index = i;
423+
}
424+
425+
/* soft timestamp */
426+
chan[i] = chan_template[1];
427+
chan[i].scan_index = i;
428+
429+
*chain_chan = chan;
430+
431+
/* 1 word for each voltage channel + aligned u64 for timestamp */
432+
433+
chain_mode_buf_size = ALIGN(n_chain_dev *
434+
BITS_TO_BYTES(chan[0].scan_type.storagebits), sizeof(u64))
435+
+ sizeof(u64);
436+
buf = devm_kzalloc(dev, chain_mode_buf_size, GFP_KERNEL);
437+
if (!buf)
438+
return -ENOMEM;
439+
440+
*chain_mode_buf = buf;
441+
442+
/*
443+
* Have to limit n_chain_dev due to current implementation of
444+
* available_scan_masks.
445+
*/
446+
if (n_chain_dev > BITS_PER_LONG)
447+
return dev_err_probe(dev, -EINVAL,
448+
"chain is limited to 32 devices\n");
449+
450+
scan_masks = devm_kcalloc(dev, 2, sizeof(*scan_masks), GFP_KERNEL);
451+
if (!scan_masks)
452+
return -ENOMEM;
453+
454+
/*
455+
* Scan mask is needed since we always have to read all devices in the
456+
* chain in one SPI transfer.
457+
*/
458+
scan_masks[0] = GENMASK(n_chain_dev - 1, 0);
459+
460+
*chain_scan_masks = scan_masks;
461+
462+
return 0;
463+
}
464+
327465
static const char * const ad7944_power_supplies[] = {
328466
"avdd", "dvdd", "bvdd", "vio"
329467
};
@@ -341,6 +479,9 @@ static int ad7944_probe(struct spi_device *spi)
341479
struct ad7944_adc *adc;
342480
bool have_refin = false;
343481
struct regulator *ref;
482+
struct iio_chan_spec *chain_chan;
483+
unsigned long *chain_scan_masks;
484+
u32 n_chain_dev;
344485
int ret;
345486

346487
indio_dev = devm_iio_device_alloc(dev, sizeof(*adc));
@@ -474,14 +615,39 @@ static int ad7944_probe(struct spi_device *spi)
474615

475616
break;
476617
case AD7944_SPI_MODE_CHAIN:
477-
return dev_err_probe(dev, -EINVAL, "chain mode is not implemented\n");
618+
ret = device_property_read_u32(dev, "#daisy-chained-devices",
619+
&n_chain_dev);
620+
if (ret)
621+
return dev_err_probe(dev, ret,
622+
"failed to get #daisy-chained-devices\n");
623+
624+
ret = ad7944_chain_mode_alloc(dev, chip_info->channels,
625+
n_chain_dev, &chain_chan,
626+
&adc->chain_mode_buf,
627+
&chain_scan_masks);
628+
if (ret)
629+
return ret;
630+
631+
ret = ad7944_chain_mode_init_msg(dev, adc, &chain_chan[0],
632+
n_chain_dev);
633+
if (ret)
634+
return ret;
635+
636+
break;
478637
}
479638

480639
indio_dev->name = chip_info->name;
481640
indio_dev->modes = INDIO_DIRECT_MODE;
482641
indio_dev->info = &ad7944_iio_info;
483-
indio_dev->channels = chip_info->channels;
484-
indio_dev->num_channels = ARRAY_SIZE(chip_info->channels);
642+
643+
if (adc->spi_mode == AD7944_SPI_MODE_CHAIN) {
644+
indio_dev->available_scan_masks = chain_scan_masks;
645+
indio_dev->channels = chain_chan;
646+
indio_dev->num_channels = n_chain_dev + 1;
647+
} else {
648+
indio_dev->channels = chip_info->channels;
649+
indio_dev->num_channels = ARRAY_SIZE(chip_info->channels);
650+
}
485651

486652
ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
487653
iio_pollfunc_store_time,

0 commit comments

Comments
 (0)