Skip to content

Commit cb8ff0a

Browse files
justephnunojsa
authored andcommitted
iio: adc: ad7380: enable sequencer for single-ended parts
ad7386/7/8(-4) single-ended parts have a 2:1 mux in front of each ADC. >From an IIO point of view, all inputs are exported, i.e ad7386/7/8 export 4 channels and ad7386-4/7-4/8-4 export 8 channels. First inputs of muxes correspond to the first half of IIO channels (i.e 0-1 or 0-3) and second inputs correspond to second half (i.e 2-3 or 4-7) Currently, the driver supports only sampling first half OR second half of the IIO channels. To enable sampling all channels simultaneously, these parts have an internal sequencer that automatically cycles through the mux entries. When enabled, the maximum throughput is divided by two. Moreover, the ADCs need additional settling time, so we add an extra CS toggle to correctly propagate setting, and an additional spi transfer to read the second half. Signed-off-by: Julien Stephan <jstephan@baylibre.com> Reviewed-by: David Lechner <dlechner@baylibre.com> Link: https://patch.msgid.link/20240731-ad7380-add-single-ended-chips-v2-6-cd63bf05744c@baylibre.com Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
1 parent 3b7392f commit cb8ff0a

File tree

1 file changed

+130
-45
lines changed

1 file changed

+130
-45
lines changed

drivers/iio/adc/ad7380.c

Lines changed: 130 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#include <linux/iio/trigger_consumer.h>
3434
#include <linux/iio/triggered_buffer.h>
3535

36-
#define MAX_NUM_CHANNELS 4
36+
#define MAX_NUM_CHANNELS 8
3737
/* 2.5V internal reference voltage */
3838
#define AD7380_INTERNAL_REF_MV 2500
3939

@@ -52,6 +52,7 @@
5252
#define AD7380_REG_ADDR_ALERT_HIGH_TH 0x5
5353

5454
#define AD7380_CONFIG1_CH BIT(11)
55+
#define AD7380_CONFIG1_SEQ BIT(10)
5556
#define AD7380_CONFIG1_OS_MODE BIT(9)
5657
#define AD7380_CONFIG1_OSR GENMASK(8, 6)
5758
#define AD7380_CONFIG1_CRC_W BIT(5)
@@ -290,16 +291,28 @@ static const unsigned long ad7380_4_channel_scan_masks[] = {
290291
*
291292
* Since this is simultaneous sampling for AinX0 OR AinX1 we have two separate
292293
* scan masks.
294+
* When sequencer mode is enabled, chip automatically cycles through
295+
* AinX0 and AinX1 channels. From an IIO point of view, we ca enable all
296+
* channels, at the cost of an extra read, thus dividing the maximum rate by
297+
* two.
293298
*/
299+
enum {
300+
AD7380_SCAN_MASK_CH_0,
301+
AD7380_SCAN_MASK_CH_1,
302+
AD7380_SCAN_MASK_SEQ,
303+
};
304+
294305
static const unsigned long ad7380_2x2_channel_scan_masks[] = {
295-
GENMASK(1, 0),
296-
GENMASK(3, 2),
306+
[AD7380_SCAN_MASK_CH_0] = GENMASK(1, 0),
307+
[AD7380_SCAN_MASK_CH_1] = GENMASK(3, 2),
308+
[AD7380_SCAN_MASK_SEQ] = GENMASK(3, 0),
297309
0
298310
};
299311

300312
static const unsigned long ad7380_2x4_channel_scan_masks[] = {
301-
GENMASK(3, 0),
302-
GENMASK(7, 4),
313+
[AD7380_SCAN_MASK_CH_0] = GENMASK(3, 0),
314+
[AD7380_SCAN_MASK_CH_1] = GENMASK(7, 4),
315+
[AD7380_SCAN_MASK_SEQ] = GENMASK(7, 0),
303316
0
304317
};
305318

@@ -467,11 +480,14 @@ struct ad7380_state {
467480
unsigned int oversampling_ratio;
468481
bool resolution_boost_enabled;
469482
unsigned int ch;
483+
bool seq;
470484
unsigned int vref_mv;
471485
unsigned int vcm_mv[MAX_NUM_CHANNELS];
472486
/* xfers, message an buffer for reading sample data */
473-
struct spi_transfer xfer[2];
474-
struct spi_message msg;
487+
struct spi_transfer normal_xfer[2];
488+
struct spi_message normal_msg;
489+
struct spi_transfer seq_xfer[4];
490+
struct spi_message seq_msg;
475491
/*
476492
* DMA (thus cache coherency maintenance) requires the transfer buffers
477493
* to live in their own cache lines.
@@ -609,33 +625,47 @@ static int ad7380_set_ch(struct ad7380_state *st, unsigned int ch)
609625
static void ad7380_update_xfers(struct ad7380_state *st,
610626
const struct iio_scan_type *scan_type)
611627
{
612-
/*
613-
* First xfer only triggers conversion and has to be long enough for
614-
* all conversions to complete, which can be multiple conversion in the
615-
* case of oversampling. Technically T_CONVERT_X_NS is lower for some
616-
* chips, but we use the maximum value for simplicity for now.
617-
*/
618-
if (st->oversampling_ratio > 1)
619-
st->xfer[0].delay.value = T_CONVERT_0_NS + T_CONVERT_X_NS *
620-
(st->oversampling_ratio - 1);
621-
else
622-
st->xfer[0].delay.value = T_CONVERT_NS;
623-
624-
st->xfer[0].delay.unit = SPI_DELAY_UNIT_NSECS;
628+
struct spi_transfer *xfer = st->seq ? st->seq_xfer : st->normal_xfer;
629+
unsigned int t_convert = T_CONVERT_NS;
625630

626631
/*
627-
* Second xfer reads all channels. Data size depends on if resolution
628-
* boost is enabled or not.
632+
* In the case of oversampling, conversion time is higher than in normal
633+
* mode. Technically T_CONVERT_X_NS is lower for some chips, but we use
634+
* the maximum value for simplicity for now.
629635
*/
630-
st->xfer[1].bits_per_word = scan_type->realbits;
631-
st->xfer[1].len = BITS_TO_BYTES(scan_type->storagebits) *
632-
st->chip_info->num_simult_channels;
636+
if (st->oversampling_ratio > 1)
637+
t_convert = T_CONVERT_0_NS + T_CONVERT_X_NS *
638+
(st->oversampling_ratio - 1);
639+
640+
if (st->seq) {
641+
xfer[0].delay.value = xfer[1].delay.value = t_convert;
642+
xfer[0].delay.unit = xfer[1].delay.unit = SPI_DELAY_UNIT_NSECS;
643+
xfer[2].bits_per_word = xfer[3].bits_per_word =
644+
scan_type->realbits;
645+
xfer[2].len = xfer[3].len =
646+
BITS_TO_BYTES(scan_type->storagebits) *
647+
st->chip_info->num_simult_channels;
648+
xfer[3].rx_buf = xfer[2].rx_buf + xfer[2].len;
649+
/* Additional delay required here when oversampling is enabled */
650+
if (st->oversampling_ratio > 1)
651+
xfer[2].delay.value = t_convert;
652+
else
653+
xfer[2].delay.value = 0;
654+
xfer[2].delay.unit = SPI_DELAY_UNIT_NSECS;
655+
} else {
656+
xfer[0].delay.value = t_convert;
657+
xfer[0].delay.unit = SPI_DELAY_UNIT_NSECS;
658+
xfer[1].bits_per_word = scan_type->realbits;
659+
xfer[1].len = BITS_TO_BYTES(scan_type->storagebits) *
660+
st->chip_info->num_simult_channels;
661+
}
633662
}
634663

635664
static int ad7380_triggered_buffer_preenable(struct iio_dev *indio_dev)
636665
{
637666
struct ad7380_state *st = iio_priv(indio_dev);
638667
const struct iio_scan_type *scan_type;
668+
struct spi_message *msg = &st->normal_msg;
639669

640670
/*
641671
* Currently, we always read all channels at the same time. The scan_type
@@ -651,28 +681,57 @@ static int ad7380_triggered_buffer_preenable(struct iio_dev *indio_dev)
651681

652682
/*
653683
* Depending on the requested scan_mask and current state,
654-
* we need to change CH bit to sample correct data.
684+
* we need to either change CH bit, or enable sequencer mode
685+
* to sample correct data.
686+
* Sequencer mode is enabled if active mask corresponds to all
687+
* IIO channels enabled. Otherwise, CH bit is set.
655688
*/
656689
ret = iio_active_scan_mask_index(indio_dev);
657690
if (ret < 0)
658691
return ret;
659692

660693
index = ret;
661-
ret = ad7380_set_ch(st, index);
662-
if (ret)
663-
return ret;
694+
if (index == AD7380_SCAN_MASK_SEQ) {
695+
ret = regmap_update_bits(st->regmap,
696+
AD7380_REG_ADDR_CONFIG1,
697+
AD7380_CONFIG1_SEQ,
698+
FIELD_PREP(AD7380_CONFIG1_SEQ, 1));
699+
if (ret)
700+
return ret;
701+
msg = &st->seq_msg;
702+
st->seq = true;
703+
} else {
704+
ret = ad7380_set_ch(st, index);
705+
if (ret)
706+
return ret;
707+
}
708+
664709
}
665710

666711
ad7380_update_xfers(st, scan_type);
667712

668-
return spi_optimize_message(st->spi, &st->msg);
713+
return spi_optimize_message(st->spi, msg);
669714
}
670715

671716
static int ad7380_triggered_buffer_postdisable(struct iio_dev *indio_dev)
672717
{
673718
struct ad7380_state *st = iio_priv(indio_dev);
719+
struct spi_message *msg = &st->normal_msg;
720+
int ret;
721+
722+
if (st->seq) {
723+
ret = regmap_update_bits(st->regmap,
724+
AD7380_REG_ADDR_CONFIG1,
725+
AD7380_CONFIG1_SEQ,
726+
FIELD_PREP(AD7380_CONFIG1_SEQ, 0));
727+
if (ret)
728+
return ret;
729+
730+
msg = &st->seq_msg;
731+
st->seq = false;
732+
}
674733

675-
spi_unoptimize_message(&st->msg);
734+
spi_unoptimize_message(msg);
676735

677736
return 0;
678737
}
@@ -687,9 +746,10 @@ static irqreturn_t ad7380_trigger_handler(int irq, void *p)
687746
struct iio_poll_func *pf = p;
688747
struct iio_dev *indio_dev = pf->indio_dev;
689748
struct ad7380_state *st = iio_priv(indio_dev);
749+
struct spi_message *msg = st->seq ? &st->seq_msg : &st->normal_msg;
690750
int ret;
691751

692-
ret = spi_sync(st->spi, &st->msg);
752+
ret = spi_sync(st->spi, msg);
693753
if (ret)
694754
goto out;
695755

@@ -723,7 +783,7 @@ static int ad7380_read_direct(struct ad7380_state *st, unsigned int scan_index,
723783

724784
ad7380_update_xfers(st, scan_type);
725785

726-
ret = spi_sync(st->spi, &st->msg);
786+
ret = spi_sync(st->spi, &st->normal_msg);
727787
if (ret < 0)
728788
return ret;
729789

@@ -919,6 +979,7 @@ static int ad7380_init(struct ad7380_state *st, struct regulator *vref)
919979
/* This is the default value after reset. */
920980
st->oversampling_ratio = 1;
921981
st->ch = 0;
982+
st->seq = false;
922983

923984
/* SPI 1-wire mode */
924985
return regmap_update_bits(st->regmap, AD7380_REG_ADDR_CONFIG2,
@@ -1020,21 +1081,45 @@ static int ad7380_probe(struct spi_device *spi)
10201081
"failed to allocate register map\n");
10211082

10221083
/*
1023-
* Setting up a low latency read for getting sample data. Used for both
1024-
* direct read an triggered buffer. Additional fields will be set up in
1025-
* ad7380_update_xfers() based on the current state of the driver at the
1026-
* time of the read.
1084+
* Setting up xfer structures for both normal and sequence mode. These
1085+
* struct are used for both direct read and triggered buffer. Additional
1086+
* fields will be set up in ad7380_update_xfers() based on the current
1087+
* state of the driver at the time of the read.
10271088
*/
10281089

1029-
/* toggle CS (no data xfer) to trigger a conversion */
1030-
st->xfer[0].cs_change = 1;
1031-
st->xfer[0].cs_change_delay.value = st->chip_info->timing_specs->t_csh_ns;
1032-
st->xfer[0].cs_change_delay.unit = SPI_DELAY_UNIT_NSECS;
1033-
1034-
/* then do a second xfer to read the data */
1035-
st->xfer[1].rx_buf = st->scan_data;
1090+
/*
1091+
* In normal mode a read is composed of two steps:
1092+
* - first, toggle CS (no data xfer) to trigger a conversion
1093+
* - then, read data
1094+
*/
1095+
st->normal_xfer[0].cs_change = 1;
1096+
st->normal_xfer[0].cs_change_delay.value = st->chip_info->timing_specs->t_csh_ns;
1097+
st->normal_xfer[0].cs_change_delay.unit = SPI_DELAY_UNIT_NSECS;
1098+
st->normal_xfer[1].rx_buf = st->scan_data;
10361099

1037-
spi_message_init_with_transfers(&st->msg, st->xfer, ARRAY_SIZE(st->xfer));
1100+
spi_message_init_with_transfers(&st->normal_msg, st->normal_xfer,
1101+
ARRAY_SIZE(st->normal_xfer));
1102+
/*
1103+
* In sequencer mode a read is composed of four steps:
1104+
* - CS toggle (no data xfer) to get the right point in the sequence
1105+
* - CS toggle (no data xfer) to trigger a conversion of AinX0 and
1106+
* acquisition of AinX1
1107+
* - 2 data reads, to read AinX0 and AinX1
1108+
*/
1109+
st->seq_xfer[0].cs_change = 1;
1110+
st->seq_xfer[0].cs_change_delay.value = st->chip_info->timing_specs->t_csh_ns;
1111+
st->seq_xfer[0].cs_change_delay.unit = SPI_DELAY_UNIT_NSECS;
1112+
st->seq_xfer[1].cs_change = 1;
1113+
st->seq_xfer[1].cs_change_delay.value = st->chip_info->timing_specs->t_csh_ns;
1114+
st->seq_xfer[1].cs_change_delay.unit = SPI_DELAY_UNIT_NSECS;
1115+
1116+
st->seq_xfer[2].rx_buf = st->scan_data;
1117+
st->seq_xfer[2].cs_change = 1;
1118+
st->seq_xfer[2].cs_change_delay.value = st->chip_info->timing_specs->t_csh_ns;
1119+
st->seq_xfer[2].cs_change_delay.unit = SPI_DELAY_UNIT_NSECS;
1120+
1121+
spi_message_init_with_transfers(&st->seq_msg, st->seq_xfer,
1122+
ARRAY_SIZE(st->seq_xfer));
10381123

10391124
indio_dev->channels = st->chip_info->channels;
10401125
indio_dev->num_channels = st->chip_info->num_channels;

0 commit comments

Comments
 (0)