@@ -140,6 +140,13 @@ struct ad7768_clk_configuration {
140
140
enum ad7768_pwrmode pwrmode ;
141
141
};
142
142
143
+ enum ad7768_scan_type {
144
+ AD7768_SCAN_TYPE_DMA_NORMAL ,
145
+ AD7768_SCAN_TYPE_DMA_HIGH_SPEED ,
146
+ AD7768_SCAN_TYPE_NORMAL ,
147
+ AD7768_SCAN_TYPE_HIGH_SPEED ,
148
+ };
149
+
143
150
static const char * const ad7768_vcm_modes [] = {
144
151
"(AVDD1-AVSS)/2" ,
145
152
"2V5" ,
@@ -151,6 +158,10 @@ static const char * const ad7768_vcm_modes[] = {
151
158
"OFF" ,
152
159
};
153
160
161
+ static const int ad7768_mclk_div_rates [4 ] = {
162
+ 16 , 8 , 4 , 2 ,
163
+ };
164
+
154
165
static const struct ad7768_clk_configuration ad7768_clk_config [] = {
155
166
{ AD7768_MCLK_DIV_2 , AD7768_DEC_RATE_32 , 64 , AD7768_FAST_MODE },
156
167
{ AD7768_MCLK_DIV_2 , AD7768_DEC_RATE_64 , 128 , AD7768_FAST_MODE },
@@ -163,6 +174,33 @@ static const struct ad7768_clk_configuration ad7768_clk_config[] = {
163
174
{ AD7768_MCLK_DIV_16 , AD7768_DEC_RATE_1024 , 16384 , AD7768_ECO_MODE },
164
175
};
165
176
177
+ static const struct iio_scan_type ad7768_scan_type [] = {
178
+ [AD7768_SCAN_TYPE_NORMAL ] = {
179
+ .sign = 's' ,
180
+ .realbits = 24 ,
181
+ .storagebits = 32 ,
182
+ .endianness = IIO_BE ,
183
+ },
184
+ [AD7768_SCAN_TYPE_HIGH_SPEED ] = {
185
+ .sign = 's' ,
186
+ .realbits = 16 ,
187
+ .storagebits = 32 ,
188
+ .endianness = IIO_BE ,
189
+ },
190
+ [AD7768_SCAN_TYPE_DMA_NORMAL ] = {
191
+ .sign = 's' ,
192
+ .realbits = 24 ,
193
+ .storagebits = 32 ,
194
+ .endianness = IIO_CPU ,
195
+ },
196
+ [AD7768_SCAN_TYPE_DMA_HIGH_SPEED ] = {
197
+ .sign = 's' ,
198
+ .realbits = 16 ,
199
+ .storagebits = 32 ,
200
+ .endianness = IIO_CPU ,
201
+ },
202
+ };
203
+
166
204
static int ad7768_get_vcm (struct iio_dev * dev , const struct iio_chan_spec * chan );
167
205
static int ad7768_set_vcm (struct iio_dev * dev , const struct iio_chan_spec * chan ,
168
206
unsigned int mode );
@@ -194,12 +232,9 @@ static const struct iio_chan_spec ad7768_channels[] = {
194
232
.indexed = 1 ,
195
233
.channel = 0 ,
196
234
.scan_index = 0 ,
197
- .scan_type = {
198
- .sign = 's' ,
199
- .realbits = 24 ,
200
- .storagebits = 32 ,
201
- .shift = 8 ,
202
- },
235
+ .has_ext_scan_type = 1 ,
236
+ .ext_scan_type = ad7768_scan_type ,
237
+ .num_ext_scan_type = ARRAY_SIZE (ad7768_scan_type ),
203
238
},
204
239
};
205
240
@@ -211,6 +246,7 @@ struct ad7768_state {
211
246
struct gpio_chip gpiochip ;
212
247
unsigned int gpio_avail_map ;
213
248
unsigned int mclk_freq ;
249
+ unsigned int dec_rate ;
214
250
unsigned int samp_freq ;
215
251
unsigned int common_mode_voltage ;
216
252
struct completion completion ;
@@ -324,6 +360,15 @@ static int ad7768_scan_direct(struct iio_dev *indio_dev)
324
360
ret = ad7768_spi_reg_read (st , AD7768_REG_ADC_DATA , & readval , 3 );
325
361
if (ret < 0 )
326
362
return ret ;
363
+
364
+ /*
365
+ * When the decimation rate is set to x8, the ADC data precision is reduced
366
+ * from 24 bits to 16 bits. Since the AD7768_REG_ADC_DATA register provides
367
+ * 24-bit data, the precision is reduced by right-shifting the read value
368
+ * by 8 bits.
369
+ */
370
+ if (st -> dec_rate == 8 )
371
+ readval = readval >> 8 ;
327
372
/*
328
373
* Any SPI configuration of the AD7768-1 can only be
329
374
* performed in continuous conversion mode.
@@ -505,6 +550,8 @@ static int ad7768_set_freq(struct ad7768_state *st,
505
550
if (ret < 0 )
506
551
return ret ;
507
552
553
+ st -> dec_rate = ad7768_clk_config [idx ].clk_div /
554
+ ad7768_mclk_div_rates [ad7768_clk_config [idx ].mclk_div ];
508
555
st -> samp_freq = DIV_ROUND_CLOSEST (st -> mclk_freq ,
509
556
ad7768_clk_config [idx ].clk_div );
510
557
@@ -560,8 +607,13 @@ static int ad7768_read_raw(struct iio_dev *indio_dev,
560
607
int * val , int * val2 , long info )
561
608
{
562
609
struct ad7768_state * st = iio_priv (indio_dev );
610
+ const struct iio_scan_type * scan_type ;
563
611
int scale_uv , ret ;
564
612
613
+ scan_type = iio_get_current_scan_type (indio_dev , chan );
614
+ if (IS_ERR (scan_type ))
615
+ return PTR_ERR (scan_type );
616
+
565
617
switch (info ) {
566
618
case IIO_CHAN_INFO_RAW :
567
619
ret = iio_device_claim_direct_mode (indio_dev );
@@ -570,7 +622,7 @@ static int ad7768_read_raw(struct iio_dev *indio_dev,
570
622
571
623
ret = ad7768_scan_direct (indio_dev );
572
624
if (ret >= 0 )
573
- * val = sign_extend32 (ret , chan -> scan_type . realbits - 1 );
625
+ * val = sign_extend32 (ret , scan_type -> realbits - 1 );
574
626
575
627
iio_device_release_direct_mode (indio_dev );
576
628
if (ret < 0 )
@@ -584,7 +636,7 @@ static int ad7768_read_raw(struct iio_dev *indio_dev,
584
636
return scale_uv ;
585
637
586
638
* val = (scale_uv * 2 ) / 1000 ;
587
- * val2 = chan -> scan_type . realbits ;
639
+ * val2 = scan_type -> realbits ;
588
640
589
641
return IIO_VAL_FRACTIONAL_LOG2 ;
590
642
@@ -628,11 +680,25 @@ static const struct attribute_group ad7768_group = {
628
680
.attrs = ad7768_attributes ,
629
681
};
630
682
683
+ static int ad7768_get_current_scan_type (const struct iio_dev * indio_dev ,
684
+ const struct iio_chan_spec * chan )
685
+ {
686
+ struct ad7768_state * st = iio_priv (indio_dev );
687
+
688
+ if (st -> spi_is_dma_mapped )
689
+ return st -> dec_rate == 8 ? AD7768_SCAN_TYPE_DMA_HIGH_SPEED :
690
+ AD7768_SCAN_TYPE_DMA_NORMAL ;
691
+
692
+ return st -> dec_rate == 8 ? AD7768_SCAN_TYPE_HIGH_SPEED :
693
+ AD7768_SCAN_TYPE_NORMAL ;
694
+ }
695
+
631
696
static const struct iio_info ad7768_info = {
632
697
.attrs = & ad7768_group ,
633
698
.read_raw = & ad7768_read_raw ,
634
699
.write_raw = & ad7768_write_raw ,
635
700
.read_label = ad7768_read_label ,
701
+ .get_current_scan_type = & ad7768_get_current_scan_type ,
636
702
.debugfs_reg_access = & ad7768_reg_access ,
637
703
};
638
704
@@ -720,14 +786,20 @@ static irqreturn_t ad7768_interrupt(int irq, void *dev_id)
720
786
static int ad7768_buffer_postenable (struct iio_dev * indio_dev )
721
787
{
722
788
struct ad7768_state * st = iio_priv (indio_dev );
789
+ const struct iio_scan_type * scan_type ;
723
790
struct spi_transfer xfer = {
724
791
.len = 1 ,
725
- .bits_per_word = 32
726
792
};
727
793
unsigned int rx_data [2 ];
728
794
struct spi_message msg ;
729
795
int ret ;
730
796
797
+ scan_type = iio_get_current_scan_type (indio_dev , & indio_dev -> channels [0 ]);
798
+ if (IS_ERR (scan_type ))
799
+ return PTR_ERR (scan_type );
800
+
801
+ xfer .bits_per_word = scan_type -> realbits ;
802
+
731
803
/*
732
804
* Write a 1 to the LSB of the INTERFACE_FORMAT register to enter
733
805
* continuous read mode. Subsequent data reads do not require an
0 commit comments