33
33
#include <linux/iio/trigger_consumer.h>
34
34
#include <linux/iio/triggered_buffer.h>
35
35
36
- #define MAX_NUM_CHANNELS 4
36
+ #define MAX_NUM_CHANNELS 8
37
37
/* 2.5V internal reference voltage */
38
38
#define AD7380_INTERNAL_REF_MV 2500
39
39
52
52
#define AD7380_REG_ADDR_ALERT_HIGH_TH 0x5
53
53
54
54
#define AD7380_CONFIG1_CH BIT(11)
55
+ #define AD7380_CONFIG1_SEQ BIT(10)
55
56
#define AD7380_CONFIG1_OS_MODE BIT(9)
56
57
#define AD7380_CONFIG1_OSR GENMASK(8, 6)
57
58
#define AD7380_CONFIG1_CRC_W BIT(5)
@@ -290,16 +291,28 @@ static const unsigned long ad7380_4_channel_scan_masks[] = {
290
291
*
291
292
* Since this is simultaneous sampling for AinX0 OR AinX1 we have two separate
292
293
* 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.
293
298
*/
299
+ enum {
300
+ AD7380_SCAN_MASK_CH_0 ,
301
+ AD7380_SCAN_MASK_CH_1 ,
302
+ AD7380_SCAN_MASK_SEQ ,
303
+ };
304
+
294
305
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 ),
297
309
0
298
310
};
299
311
300
312
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 ),
303
316
0
304
317
};
305
318
@@ -467,11 +480,14 @@ struct ad7380_state {
467
480
unsigned int oversampling_ratio ;
468
481
bool resolution_boost_enabled ;
469
482
unsigned int ch ;
483
+ bool seq ;
470
484
unsigned int vref_mv ;
471
485
unsigned int vcm_mv [MAX_NUM_CHANNELS ];
472
486
/* 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 ;
475
491
/*
476
492
* DMA (thus cache coherency maintenance) requires the transfer buffers
477
493
* to live in their own cache lines.
@@ -609,33 +625,47 @@ static int ad7380_set_ch(struct ad7380_state *st, unsigned int ch)
609
625
static void ad7380_update_xfers (struct ad7380_state * st ,
610
626
const struct iio_scan_type * scan_type )
611
627
{
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 ;
625
630
626
631
/*
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.
629
635
*/
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
+ }
633
662
}
634
663
635
664
static int ad7380_triggered_buffer_preenable (struct iio_dev * indio_dev )
636
665
{
637
666
struct ad7380_state * st = iio_priv (indio_dev );
638
667
const struct iio_scan_type * scan_type ;
668
+ struct spi_message * msg = & st -> normal_msg ;
639
669
640
670
/*
641
671
* 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)
651
681
652
682
/*
653
683
* 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.
655
688
*/
656
689
ret = iio_active_scan_mask_index (indio_dev );
657
690
if (ret < 0 )
658
691
return ret ;
659
692
660
693
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
+
664
709
}
665
710
666
711
ad7380_update_xfers (st , scan_type );
667
712
668
- return spi_optimize_message (st -> spi , & st -> msg );
713
+ return spi_optimize_message (st -> spi , msg );
669
714
}
670
715
671
716
static int ad7380_triggered_buffer_postdisable (struct iio_dev * indio_dev )
672
717
{
673
718
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
+ }
674
733
675
- spi_unoptimize_message (& st -> msg );
734
+ spi_unoptimize_message (msg );
676
735
677
736
return 0 ;
678
737
}
@@ -687,9 +746,10 @@ static irqreturn_t ad7380_trigger_handler(int irq, void *p)
687
746
struct iio_poll_func * pf = p ;
688
747
struct iio_dev * indio_dev = pf -> indio_dev ;
689
748
struct ad7380_state * st = iio_priv (indio_dev );
749
+ struct spi_message * msg = st -> seq ? & st -> seq_msg : & st -> normal_msg ;
690
750
int ret ;
691
751
692
- ret = spi_sync (st -> spi , & st -> msg );
752
+ ret = spi_sync (st -> spi , msg );
693
753
if (ret )
694
754
goto out ;
695
755
@@ -723,7 +783,7 @@ static int ad7380_read_direct(struct ad7380_state *st, unsigned int scan_index,
723
783
724
784
ad7380_update_xfers (st , scan_type );
725
785
726
- ret = spi_sync (st -> spi , & st -> msg );
786
+ ret = spi_sync (st -> spi , & st -> normal_msg );
727
787
if (ret < 0 )
728
788
return ret ;
729
789
@@ -919,6 +979,7 @@ static int ad7380_init(struct ad7380_state *st, struct regulator *vref)
919
979
/* This is the default value after reset. */
920
980
st -> oversampling_ratio = 1 ;
921
981
st -> ch = 0 ;
982
+ st -> seq = false;
922
983
923
984
/* SPI 1-wire mode */
924
985
return regmap_update_bits (st -> regmap , AD7380_REG_ADDR_CONFIG2 ,
@@ -1020,21 +1081,45 @@ static int ad7380_probe(struct spi_device *spi)
1020
1081
"failed to allocate register map\n" );
1021
1082
1022
1083
/*
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.
1027
1088
*/
1028
1089
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 ;
1036
1099
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 ));
1038
1123
1039
1124
indio_dev -> channels = st -> chip_info -> channels ;
1040
1125
indio_dev -> num_channels = st -> chip_info -> num_channels ;
0 commit comments