Skip to content

Commit af7d935

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. Using multiple scan types feature enables the driver to switch scan type in runtime, making possible to support both 24-bits and 16-bits resolution. Signed-off-by: Jonathan Santos <Jonathan.Santos@analog.com>
1 parent c7f75e0 commit af7d935

File tree

1 file changed

+48
-12
lines changed

1 file changed

+48
-12
lines changed

drivers/iio/adc/ad7768-1.c

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ enum ad7768_dec_rate {
143143
AD7768_DEC_RATE_1024 = 5
144144
};
145145

146+
enum ad7768_scan_type {
147+
AD7768_SCAN_TYPE_NORMAL,
148+
AD7768_SCAN_TYPE_HIGH_SPEED,
149+
};
150+
146151
struct ad7768_clk_configuration {
147152
enum ad7768_mclk_div mclk_div;
148153
enum ad7768_dec_rate dec_rate;
@@ -199,6 +204,19 @@ static const int sinc3_dec_rate_max_values[4] = {
199204
20480, 40960, 81920, 163840,
200205
};
201206

207+
static const struct iio_scan_type ad7768_scan_type[] = {
208+
[AD7768_SCAN_TYPE_NORMAL] = {
209+
.sign = 's',
210+
.realbits = 24,
211+
.storagebits = 32,
212+
},
213+
[AD7768_SCAN_TYPE_HIGH_SPEED] = {
214+
.sign = 's',
215+
.realbits = 16,
216+
.storagebits = 32,
217+
},
218+
};
219+
202220
static const char * const ad7768_filter_enum[] = {
203221
[SINC5] = "sinc5",
204222
[SINC5_DEC_X8] = "sinc5-dec8",
@@ -270,12 +288,9 @@ static const struct iio_chan_spec ad7768_channels[] = {
270288
.indexed = 1,
271289
.channel = 0,
272290
.scan_index = 0,
273-
.scan_type = {
274-
.sign = 's',
275-
.realbits = 24,
276-
.storagebits = 32,
277-
.shift = 8,
278-
},
291+
.has_ext_scan_type = 1,
292+
.ext_scan_type = ad7768_scan_type,
293+
.num_ext_scan_type = ARRAY_SIZE(ad7768_scan_type),
279294
},
280295
};
281296

@@ -441,6 +456,9 @@ static int ad7768_scan_direct(struct iio_dev *indio_dev)
441456
ret = ad7768_spi_reg_read(st, AD7768_REG_ADC_DATA, &readval, 3);
442457
if (ret < 0)
443458
return ret;
459+
460+
if (st->filter_mode == SINC5_DEC_X8)
461+
readval = readval >> 8;
444462
/*
445463
* Any SPI configuration of the AD7768-1 can only be
446464
* performed in continuous conversion mode.
@@ -839,8 +857,13 @@ static int ad7768_read_raw(struct iio_dev *indio_dev,
839857
int *val, int *val2, long info)
840858
{
841859
struct ad7768_state *st = iio_priv(indio_dev);
860+
const struct iio_scan_type *scan_type;
842861
int scale_uv, ret;
843862

863+
scan_type = iio_get_current_scan_type(indio_dev, chan);
864+
if (IS_ERR(scan_type))
865+
return PTR_ERR(scan_type);
866+
844867
switch (info) {
845868
case IIO_CHAN_INFO_RAW:
846869
ret = iio_device_claim_direct_mode(indio_dev);
@@ -849,7 +872,7 @@ static int ad7768_read_raw(struct iio_dev *indio_dev,
849872

850873
ret = ad7768_scan_direct(indio_dev);
851874
if (ret >= 0)
852-
*val = sign_extend32(ret, chan->scan_type.realbits - 1);
875+
*val = sign_extend32(ret, scan_type->realbits - 1);
853876

854877
iio_device_release_direct_mode(indio_dev);
855878
if (ret < 0)
@@ -863,7 +886,7 @@ static int ad7768_read_raw(struct iio_dev *indio_dev,
863886
return scale_uv;
864887

865888
*val = (scale_uv * 2) / 1000;
866-
*val2 = chan->scan_type.realbits;
889+
*val2 = scan_type->realbits;
867890

868891
return IIO_VAL_FRACTIONAL_LOG2;
869892

@@ -907,11 +930,21 @@ static const struct attribute_group ad7768_group = {
907930
.attrs = ad7768_attributes,
908931
};
909932

933+
static int ad7768_get_current_scan_type(const struct iio_dev *indio_dev,
934+
const struct iio_chan_spec *chan)
935+
{
936+
struct ad7768_state *st = iio_priv(indio_dev);
937+
938+
return st->filter_mode == SINC5_DEC_X8 ? AD7768_SCAN_TYPE_HIGH_SPEED
939+
: AD7768_SCAN_TYPE_NORMAL;
940+
}
941+
910942
static const struct iio_info ad7768_info = {
911943
.attrs = &ad7768_group,
912944
.read_raw = &ad7768_read_raw,
913945
.write_raw = &ad7768_write_raw,
914946
.read_label = ad7768_read_label,
947+
.get_current_scan_type = &ad7768_get_current_scan_type,
915948
.debugfs_reg_access = &ad7768_reg_access,
916949
};
917950

@@ -1029,15 +1062,20 @@ static irqreturn_t ad7768_interrupt(int irq, void *dev_id)
10291062
static int ad7768_buffer_postenable(struct iio_dev *indio_dev)
10301063
{
10311064
struct ad7768_state *st = iio_priv(indio_dev);
1065+
const struct iio_scan_type *scan_type;
10321066
struct spi_transfer xfer = {
10331067
.len = 1,
1034-
.bits_per_word = 32
10351068
};
10361069
unsigned int rx_data[2];
1037-
unsigned int tx_data[2];
10381070
struct spi_message msg;
10391071
int ret;
10401072

1073+
scan_type = iio_get_current_scan_type(indio_dev, &indio_dev->channels[0]);
1074+
if (IS_ERR(scan_type))
1075+
return PTR_ERR(scan_type);
1076+
1077+
xfer.bits_per_word = scan_type->realbits;
1078+
10411079
/*
10421080
* Write a 1 to the LSB of the INTERFACE_FORMAT register to enter
10431081
* continuous read mode. Subsequent data reads do not require an
@@ -1050,8 +1088,6 @@ static int ad7768_buffer_postenable(struct iio_dev *indio_dev)
10501088
if (st->spi_is_dma_mapped) {
10511089
spi_bus_lock(st->spi->master);
10521090

1053-
tx_data[0] = AD7768_RD_FLAG_MSK(AD7768_REG_ADC_DATA) << 24;
1054-
xfer.tx_buf = tx_data;
10551091
xfer.rx_buf = rx_data;
10561092
spi_message_init_with_transfers(&msg, &xfer, 1);
10571093
ret = spi_engine_offload_load_msg(st->spi, &msg);

0 commit comments

Comments
 (0)