Skip to content

Commit b55c225

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 bdafef4 commit b55c225

File tree

1 file changed

+74
-9
lines changed

1 file changed

+74
-9
lines changed

drivers/iio/adc/ad7768-1.c

Lines changed: 74 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,19 @@ enum ad7768_dec_rate {
157157
AD7768_DEC_RATE_16 = 10
158158
};
159159

160+
enum ad7768_scan_type {
161+
AD7768_SCAN_TYPE_DMA_NORMAL,
162+
AD7768_SCAN_TYPE_DMA_HIGH_SPEED,
163+
AD7768_SCAN_TYPE_NORMAL,
164+
AD7768_SCAN_TYPE_HIGH_SPEED,
165+
};
166+
167+
struct ad7768_clk_configuration {
168+
enum ad7768_mclk_div mclk_div;
169+
enum ad7768_dec_rate dec_rate;
170+
unsigned int clk_div;
171+
};
172+
160173
static const char * const ad7768_vcm_modes[] = {
161174
"(AVDD1-AVSS)/2",
162175
"2V5",
@@ -180,6 +193,33 @@ static const int sinc3_dec_rate_max_values[4] = {
180193
20480, 40960, 81920, 163840,
181194
};
182195

196+
static const struct iio_scan_type ad7768_scan_type[] = {
197+
[AD7768_SCAN_TYPE_NORMAL] = {
198+
.sign = 's',
199+
.realbits = 24,
200+
.storagebits = 32,
201+
.endianness = IIO_BE,
202+
},
203+
[AD7768_SCAN_TYPE_HIGH_SPEED] = {
204+
.sign = 's',
205+
.realbits = 16,
206+
.storagebits = 32,
207+
.endianness = IIO_BE,
208+
},
209+
[AD7768_SCAN_TYPE_DMA_NORMAL] = {
210+
.sign = 's',
211+
.realbits = 24,
212+
.storagebits = 32,
213+
.endianness = IIO_CPU,
214+
},
215+
[AD7768_SCAN_TYPE_DMA_HIGH_SPEED] = {
216+
.sign = 's',
217+
.realbits = 16,
218+
.storagebits = 32,
219+
.endianness = IIO_CPU,
220+
},
221+
};
222+
183223
static const char * const ad7768_filter_enum[] = {
184224
[SINC5] = "sinc5",
185225
[SINC3] = "sinc3",
@@ -242,12 +282,9 @@ static const struct iio_chan_spec ad7768_channels[] = {
242282
.indexed = 1,
243283
.channel = 0,
244284
.scan_index = 0,
245-
.scan_type = {
246-
.sign = 's',
247-
.realbits = 24,
248-
.storagebits = 32,
249-
.shift = 8,
250-
},
285+
.has_ext_scan_type = 1,
286+
.ext_scan_type = ad7768_scan_type,
287+
.num_ext_scan_type = ARRAY_SIZE(ad7768_scan_type),
251288
},
252289
};
253290

@@ -422,6 +459,9 @@ static int ad7768_scan_direct(struct iio_dev *indio_dev)
422459
ret = ad7768_spi_reg_read(st, AD7768_REG_ADC_DATA, &readval, 3);
423460
if (ret < 0)
424461
return ret;
462+
463+
if (st->filter_mode == SINC5_DEC_X8)
464+
readval = readval >> 8;
425465
/*
426466
* Any SPI configuration of the AD7768-1 can only be
427467
* performed in continuous conversion mode.
@@ -916,8 +956,13 @@ static int ad7768_read_raw(struct iio_dev *indio_dev,
916956
int *val, int *val2, long info)
917957
{
918958
struct ad7768_state *st = iio_priv(indio_dev);
959+
const struct iio_scan_type *scan_type;
919960
int scale_uv, ret;
920961

962+
scan_type = iio_get_current_scan_type(indio_dev, chan);
963+
if (IS_ERR(scan_type))
964+
return PTR_ERR(scan_type);
965+
921966
switch (info) {
922967
case IIO_CHAN_INFO_RAW:
923968
ret = iio_device_claim_direct_mode(indio_dev);
@@ -926,7 +971,7 @@ static int ad7768_read_raw(struct iio_dev *indio_dev,
926971

927972
ret = ad7768_scan_direct(indio_dev);
928973
if (ret >= 0)
929-
*val = sign_extend32(ret, chan->scan_type.realbits - 1);
974+
*val = sign_extend32(ret, scan_type->realbits - 1);
930975

931976
iio_device_release_direct_mode(indio_dev);
932977
if (ret < 0)
@@ -940,7 +985,7 @@ static int ad7768_read_raw(struct iio_dev *indio_dev,
940985
return scale_uv;
941986

942987
*val = (scale_uv * 2) / 1000;
943-
*val2 = chan->scan_type.realbits;
988+
*val2 = scan_type->realbits;
944989

945990
return IIO_VAL_FRACTIONAL_LOG2;
946991

@@ -985,11 +1030,25 @@ static const struct attribute_group ad7768_group = {
9851030
.attrs = ad7768_attributes,
9861031
};
9871032

1033+
static int ad7768_get_current_scan_type(const struct iio_dev *indio_dev,
1034+
const struct iio_chan_spec *chan)
1035+
{
1036+
struct ad7768_state *st = iio_priv(indio_dev);
1037+
1038+
if (st->spi_is_dma_mapped)
1039+
return st->filter_mode == SINC5_DEC_X8 ? AD7768_SCAN_TYPE_DMA_HIGH_SPEED :
1040+
AD7768_SCAN_TYPE_DMA_NORMAL;
1041+
else
1042+
return st->filter_mode == SINC5_DEC_X8 ? AD7768_SCAN_TYPE_HIGH_SPEED :
1043+
AD7768_SCAN_TYPE_NORMAL;
1044+
}
1045+
9881046
static const struct iio_info ad7768_info = {
9891047
.attrs = &ad7768_group,
9901048
.read_raw = &ad7768_read_raw,
9911049
.write_raw = &ad7768_write_raw,
9921050
.read_label = ad7768_read_label,
1051+
.get_current_scan_type = &ad7768_get_current_scan_type,
9931052
.debugfs_reg_access = &ad7768_reg_access,
9941053
};
9951054

@@ -1095,15 +1154,21 @@ static irqreturn_t ad7768_interrupt(int irq, void *dev_id)
10951154
static int ad7768_buffer_postenable(struct iio_dev *indio_dev)
10961155
{
10971156
struct ad7768_state *st = iio_priv(indio_dev);
1157+
const struct iio_scan_type *scan_type;
10981158
struct spi_transfer xfer = {
10991159
.len = 1,
1100-
.bits_per_word = 32
11011160
};
11021161
unsigned int rx_data[2];
11031162
unsigned int tx_data[2];
11041163
struct spi_message msg;
11051164
int ret;
11061165

1166+
scan_type = iio_get_current_scan_type(indio_dev, &indio_dev->channels[0]);
1167+
if (IS_ERR(scan_type))
1168+
return PTR_ERR(scan_type);
1169+
1170+
xfer.bits_per_word = scan_type->realbits;
1171+
11071172
/*
11081173
* Write a 1 to the LSB of the INTERFACE_FORMAT register to enter
11091174
* continuous read mode. Subsequent data reads do not require an

0 commit comments

Comments
 (0)