Skip to content

Commit 97bfb17

Browse files
committed
iio: adc: ad7768-1: add multiple scan types to support 16-bits mode
When the device is configured to Sinc5 filter and decimation x8, output data is reduced to 16-bits in order to support 1 MHz of sampling frequency due to clock limitation. Use multiple scan types feature to enable the driver to switch scan type in runtime, making possible to support both 24-bit and 16-bit resolution. Signed-off-by: Jonathan Santos <Jonathan.Santos@analog.com>
1 parent 9142415 commit 97bfb17

File tree

1 file changed

+81
-9
lines changed

1 file changed

+81
-9
lines changed

drivers/iio/adc/ad7768-1.c

Lines changed: 81 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ struct ad7768_clk_configuration {
140140
enum ad7768_pwrmode pwrmode;
141141
};
142142

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+
143150
static const char * const ad7768_vcm_modes[] = {
144151
"(AVDD1-AVSS)/2",
145152
"2V5",
@@ -151,6 +158,10 @@ static const char * const ad7768_vcm_modes[] = {
151158
"OFF",
152159
};
153160

161+
static const int ad7768_mclk_div_rates[4] = {
162+
16, 8, 4, 2,
163+
};
164+
154165
static const struct ad7768_clk_configuration ad7768_clk_config[] = {
155166
{ AD7768_MCLK_DIV_2, AD7768_DEC_RATE_32, 64, AD7768_FAST_MODE },
156167
{ 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[] = {
163174
{ AD7768_MCLK_DIV_16, AD7768_DEC_RATE_1024, 16384, AD7768_ECO_MODE },
164175
};
165176

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+
166204
static int ad7768_get_vcm(struct iio_dev *dev, const struct iio_chan_spec *chan);
167205
static int ad7768_set_vcm(struct iio_dev *dev, const struct iio_chan_spec *chan,
168206
unsigned int mode);
@@ -194,12 +232,9 @@ static const struct iio_chan_spec ad7768_channels[] = {
194232
.indexed = 1,
195233
.channel = 0,
196234
.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),
203238
},
204239
};
205240

@@ -211,6 +246,7 @@ struct ad7768_state {
211246
struct gpio_chip gpiochip;
212247
unsigned int gpio_avail_map;
213248
unsigned int mclk_freq;
249+
unsigned int dec_rate;
214250
unsigned int samp_freq;
215251
unsigned int common_mode_voltage;
216252
struct completion completion;
@@ -324,6 +360,15 @@ static int ad7768_scan_direct(struct iio_dev *indio_dev)
324360
ret = ad7768_spi_reg_read(st, AD7768_REG_ADC_DATA, &readval, 3);
325361
if (ret < 0)
326362
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;
327372
/*
328373
* Any SPI configuration of the AD7768-1 can only be
329374
* performed in continuous conversion mode.
@@ -505,6 +550,8 @@ static int ad7768_set_freq(struct ad7768_state *st,
505550
if (ret < 0)
506551
return ret;
507552

553+
st->dec_rate = ad7768_clk_config[idx].clk_div /
554+
ad7768_mclk_div_rates[ad7768_clk_config[idx].mclk_div];
508555
st->samp_freq = DIV_ROUND_CLOSEST(st->mclk_freq,
509556
ad7768_clk_config[idx].clk_div);
510557

@@ -560,8 +607,13 @@ static int ad7768_read_raw(struct iio_dev *indio_dev,
560607
int *val, int *val2, long info)
561608
{
562609
struct ad7768_state *st = iio_priv(indio_dev);
610+
const struct iio_scan_type *scan_type;
563611
int scale_uv, ret;
564612

613+
scan_type = iio_get_current_scan_type(indio_dev, chan);
614+
if (IS_ERR(scan_type))
615+
return PTR_ERR(scan_type);
616+
565617
switch (info) {
566618
case IIO_CHAN_INFO_RAW:
567619
ret = iio_device_claim_direct_mode(indio_dev);
@@ -570,7 +622,7 @@ static int ad7768_read_raw(struct iio_dev *indio_dev,
570622

571623
ret = ad7768_scan_direct(indio_dev);
572624
if (ret >= 0)
573-
*val = sign_extend32(ret, chan->scan_type.realbits - 1);
625+
*val = sign_extend32(ret, scan_type->realbits - 1);
574626

575627
iio_device_release_direct_mode(indio_dev);
576628
if (ret < 0)
@@ -584,7 +636,7 @@ static int ad7768_read_raw(struct iio_dev *indio_dev,
584636
return scale_uv;
585637

586638
*val = (scale_uv * 2) / 1000;
587-
*val2 = chan->scan_type.realbits;
639+
*val2 = scan_type->realbits;
588640

589641
return IIO_VAL_FRACTIONAL_LOG2;
590642

@@ -628,11 +680,25 @@ static const struct attribute_group ad7768_group = {
628680
.attrs = ad7768_attributes,
629681
};
630682

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+
631696
static const struct iio_info ad7768_info = {
632697
.attrs = &ad7768_group,
633698
.read_raw = &ad7768_read_raw,
634699
.write_raw = &ad7768_write_raw,
635700
.read_label = ad7768_read_label,
701+
.get_current_scan_type = &ad7768_get_current_scan_type,
636702
.debugfs_reg_access = &ad7768_reg_access,
637703
};
638704

@@ -720,14 +786,20 @@ static irqreturn_t ad7768_interrupt(int irq, void *dev_id)
720786
static int ad7768_buffer_postenable(struct iio_dev *indio_dev)
721787
{
722788
struct ad7768_state *st = iio_priv(indio_dev);
789+
const struct iio_scan_type *scan_type;
723790
struct spi_transfer xfer = {
724791
.len = 1,
725-
.bits_per_word = 32
726792
};
727793
unsigned int rx_data[2];
728794
struct spi_message msg;
729795
int ret;
730796

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+
731803
/*
732804
* Write a 1 to the LSB of the INTERFACE_FORMAT register to enter
733805
* continuous read mode. Subsequent data reads do not require an

0 commit comments

Comments
 (0)