Skip to content

Commit 5cbe083

Browse files
machschmittjic23
authored andcommitted
iio: adc: ad4170-4: Add support for buffered data capture
Extend the AD4170-4 driver to allow buffered data capture in continuous read mode. In continuous read mode, the chip skips the instruction phase and outputs just ADC sample data, enabling faster sample rates to be reached. The internal channel sequencer always starts sampling from channel 0 and channel 0 must be enabled if more than one channel is selected for data capture. The scan mask validation callback checks if the aforementioned condition is met. Signed-off-by: Marcelo Schmitt <marcelo.schmitt@analog.com> Link: https://patch.msgid.link/5ac9ffec07b24e75a55caed027a76f7d69f65221.1751289747.git.marcelo.schmitt@analog.com Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
1 parent 106082d commit 5cbe083

File tree

2 files changed

+217
-1
lines changed

2 files changed

+217
-1
lines changed

drivers/iio/adc/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ config AD4170_4
8989
tristate "Analog Device AD4170-4 ADC Driver"
9090
depends on SPI
9191
select REGMAP_SPI
92+
select IIO_BUFFER
93+
select IIO_TRIGGERED_BUFFER
9294
help
9395
Say yes here to build support for Analog Devices AD4170-4 SPI analog
9496
to digital converters (ADC).

drivers/iio/adc/ad4170-4.c

Lines changed: 215 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
#include <linux/delay.h>
1717
#include <linux/device.h>
1818
#include <linux/err.h>
19+
#include <linux/iio/buffer.h>
1920
#include <linux/iio/iio.h>
21+
#include <linux/iio/trigger.h>
22+
#include <linux/iio/trigger_consumer.h>
23+
#include <linux/iio/triggered_buffer.h>
2024
#include <linux/interrupt.h>
2125
#include <linux/irq.h>
2226
#include <linux/math64.h>
@@ -61,6 +65,7 @@
6165
#define AD4170_FILTER_FS_REG(x) (0xC7 + 14 * (x))
6266
#define AD4170_OFFSET_REG(x) (0xCA + 14 * (x))
6367
#define AD4170_GAIN_REG(x) (0xCD + 14 * (x))
68+
#define AD4170_ADC_CTRL_CONT_READ_EXIT_REG 0x200 /* virtual reg */
6469

6570
#define AD4170_REG_READ_MASK BIT(14)
6671

@@ -72,6 +77,7 @@
7277

7378
/* AD4170_ADC_CTRL_REG */
7479
#define AD4170_ADC_CTRL_MULTI_DATA_REG_SEL_MSK BIT(7)
80+
#define AD4170_ADC_CTRL_CONT_READ_MSK GENMASK(5, 4)
7581
#define AD4170_ADC_CTRL_MODE_MSK GENMASK(3, 0)
7682

7783
/* AD4170_CHAN_EN_REG */
@@ -116,9 +122,13 @@
116122
#define AD4170_PIN_MUXING_DIG_AUX1_RDY 0x1
117123

118124
/* AD4170_ADC_CTRL_REG constants */
125+
#define AD4170_ADC_CTRL_MODE_CONT 0x0
119126
#define AD4170_ADC_CTRL_MODE_SINGLE 0x4
120127
#define AD4170_ADC_CTRL_MODE_IDLE 0x7
121128

129+
#define AD4170_ADC_CTRL_CONT_READ_DISABLE 0x0
130+
#define AD4170_ADC_CTRL_CONT_READ_ENABLE 0x1
131+
122132
/* AD4170_FILTER_REG constants */
123133
#define AD4170_FILTER_FILTER_TYPE_SINC5_AVG 0x0
124134
#define AD4170_FILTER_FILTER_TYPE_SINC5 0x4
@@ -150,6 +160,8 @@
150160

151161
#define AD4170_GAIN_REG_DEFAULT 0x555555
152162

163+
#define AD4170_ADC_CTRL_CONT_READ_EXIT 0xA5
164+
153165
static const unsigned int ad4170_reg_size[] = {
154166
[AD4170_CONFIG_A_REG] = 1,
155167
[AD4170_DATA_24B_REG] = 3,
@@ -186,6 +198,7 @@ static const unsigned int ad4170_reg_size[] = {
186198
[AD4170_OFFSET_REG(5) ... AD4170_GAIN_REG(5)] = 3,
187199
[AD4170_OFFSET_REG(6) ... AD4170_GAIN_REG(6)] = 3,
188200
[AD4170_OFFSET_REG(7) ... AD4170_GAIN_REG(7)] = 3,
201+
[AD4170_ADC_CTRL_CONT_READ_EXIT_REG] = 0,
189202
};
190203

191204
enum ad4170_ref_buf {
@@ -320,6 +333,10 @@ struct ad4170_state {
320333
struct spi_device *spi;
321334
struct regmap *regmap;
322335
int sps_tbl[ARRAY_SIZE(ad4170_filt_names)][AD4170_MAX_FS_TBL_SIZE][2];
336+
__be32 bounce_buffer[AD4170_MAX_CHANNELS];
337+
struct spi_message msg;
338+
struct spi_transfer xfer;
339+
struct iio_trigger *trig;
323340
struct completion completion;
324341
unsigned int pins_fn[AD4170_NUM_ANALOG_PINS];
325342
u32 int_pin_sel;
@@ -407,6 +424,10 @@ static int ad4170_reg_write(void *context, unsigned int reg, unsigned int val)
407424
case 1:
408425
tx_buf[AD4170_SPI_INST_PHASE_LEN] = val;
409426
break;
427+
case 0:
428+
/* Write continuous read exit code */
429+
tx_buf[0] = AD4170_ADC_CTRL_CONT_READ_EXIT;
430+
return spi_write_then_read(st->spi, tx_buf, 1, NULL, 0);
410431
default:
411432
return -EINVAL;
412433
}
@@ -800,6 +821,7 @@ static const struct iio_chan_spec ad4170_channel_template = {
800821
.scan_type = {
801822
.realbits = 24,
802823
.storagebits = 32,
824+
.shift = 8,
803825
.endianness = IIO_BE,
804826
},
805827
};
@@ -1384,11 +1406,27 @@ static int ad4170_write_raw_get_fmt(struct iio_dev *indio_dev,
13841406
}
13851407
}
13861408

1409+
static int ad4170_update_scan_mode(struct iio_dev *indio_dev,
1410+
const unsigned long *active_scan_mask)
1411+
{
1412+
struct ad4170_state *st = iio_priv(indio_dev);
1413+
unsigned int chan_index;
1414+
int ret;
1415+
1416+
iio_for_each_active_channel(indio_dev, chan_index) {
1417+
ret = ad4170_set_channel_enable(st, chan_index, true);
1418+
if (ret)
1419+
return ret;
1420+
}
1421+
return 0;
1422+
}
1423+
13871424
static const struct iio_info ad4170_info = {
13881425
.read_raw = ad4170_read_raw,
13891426
.read_avail = ad4170_read_avail,
13901427
.write_raw = ad4170_write_raw,
13911428
.write_raw_get_fmt = ad4170_write_raw_get_fmt,
1429+
.update_scan_mode = ad4170_update_scan_mode,
13921430
.debugfs_reg_access = ad4170_debugfs_reg_access,
13931431
};
13941432

@@ -1683,16 +1721,178 @@ static int ad4170_initial_config(struct iio_dev *indio_dev)
16831721
AD4170_ADC_CTRL_MULTI_DATA_REG_SEL_MSK);
16841722
}
16851723

1724+
static int ad4170_prepare_spi_message(struct ad4170_state *st)
1725+
{
1726+
/*
1727+
* Continuous data register read is enabled on buffer postenable so
1728+
* no instruction phase is needed meaning we don't need to send the
1729+
* register address to read data. Transfer only needs the read buffer.
1730+
*/
1731+
st->xfer.rx_buf = &st->rx_buf;
1732+
st->xfer.len = BITS_TO_BYTES(ad4170_channel_template.scan_type.realbits);
1733+
1734+
spi_message_init_with_transfers(&st->msg, &st->xfer, 1);
1735+
1736+
return devm_spi_optimize_message(&st->spi->dev, st->spi, &st->msg);
1737+
}
1738+
1739+
static int ad4170_buffer_postenable(struct iio_dev *indio_dev)
1740+
{
1741+
struct ad4170_state *st = iio_priv(indio_dev);
1742+
int ret;
1743+
1744+
ret = regmap_update_bits(st->regmap, AD4170_ADC_CTRL_REG,
1745+
AD4170_ADC_CTRL_MODE_MSK,
1746+
FIELD_PREP(AD4170_ADC_CTRL_MODE_MSK,
1747+
AD4170_ADC_CTRL_MODE_CONT));
1748+
if (ret)
1749+
return ret;
1750+
1751+
/*
1752+
* This enables continuous read of the ADC data register. The ADC must
1753+
* be in continuous conversion mode.
1754+
*/
1755+
return regmap_update_bits(st->regmap, AD4170_ADC_CTRL_REG,
1756+
AD4170_ADC_CTRL_CONT_READ_MSK,
1757+
FIELD_PREP(AD4170_ADC_CTRL_CONT_READ_MSK,
1758+
AD4170_ADC_CTRL_CONT_READ_ENABLE));
1759+
}
1760+
1761+
static int ad4170_buffer_predisable(struct iio_dev *indio_dev)
1762+
{
1763+
struct ad4170_state *st = iio_priv(indio_dev);
1764+
unsigned int i;
1765+
int ret;
1766+
1767+
/*
1768+
* Use a high register address (virtual register) to request a write of
1769+
* 0xA5 to the ADC during the first 8 SCLKs of the ADC data read cycle,
1770+
* thus exiting continuous read.
1771+
*/
1772+
ret = regmap_write(st->regmap, AD4170_ADC_CTRL_CONT_READ_EXIT_REG, 0);
1773+
if (ret)
1774+
return ret;
1775+
1776+
ret = regmap_update_bits(st->regmap, AD4170_ADC_CTRL_REG,
1777+
AD4170_ADC_CTRL_CONT_READ_MSK,
1778+
FIELD_PREP(AD4170_ADC_CTRL_CONT_READ_MSK,
1779+
AD4170_ADC_CTRL_CONT_READ_DISABLE));
1780+
if (ret)
1781+
return ret;
1782+
1783+
ret = regmap_update_bits(st->regmap, AD4170_ADC_CTRL_REG,
1784+
AD4170_ADC_CTRL_MODE_MSK,
1785+
FIELD_PREP(AD4170_ADC_CTRL_MODE_MSK,
1786+
AD4170_ADC_CTRL_MODE_IDLE));
1787+
if (ret)
1788+
return ret;
1789+
1790+
/*
1791+
* The ADC sequences through all the enabled channels (see datasheet
1792+
* page 95). That can lead to incorrect channel being read if a
1793+
* single-shot read (or buffered read with different active_scan_mask)
1794+
* is done after buffer disable. Disable all channels so only requested
1795+
* channels will be read.
1796+
*/
1797+
for (i = 0; i < indio_dev->num_channels; i++) {
1798+
ret = ad4170_set_channel_enable(st, i, false);
1799+
if (ret)
1800+
return ret;
1801+
}
1802+
1803+
return 0;
1804+
}
1805+
1806+
static bool ad4170_validate_scan_mask(struct iio_dev *indio_dev,
1807+
const unsigned long *scan_mask)
1808+
{
1809+
unsigned int masklength = iio_get_masklength(indio_dev);
1810+
unsigned int enabled;
1811+
1812+
/*
1813+
* The channel sequencer cycles through the enabled channels in
1814+
* sequential order, from channel 0 to channel 15, bypassing disabled
1815+
* channels. When more than one channel is enabled, channel 0 must
1816+
* always be enabled. See datasheet channel_en register description at
1817+
* page 95.
1818+
*/
1819+
enabled = bitmap_weight(scan_mask, masklength);
1820+
if (enabled > 1)
1821+
return test_bit(0, scan_mask);
1822+
1823+
return enabled == 1;
1824+
}
1825+
1826+
static const struct iio_buffer_setup_ops ad4170_buffer_ops = {
1827+
.postenable = ad4170_buffer_postenable,
1828+
.predisable = ad4170_buffer_predisable,
1829+
.validate_scan_mask = ad4170_validate_scan_mask,
1830+
};
1831+
1832+
static irqreturn_t ad4170_trigger_handler(int irq, void *p)
1833+
{
1834+
struct iio_poll_func *pf = p;
1835+
struct iio_dev *indio_dev = pf->indio_dev;
1836+
struct ad4170_state *st = iio_priv(indio_dev);
1837+
unsigned int chan_index;
1838+
unsigned int i = 0;
1839+
int ret;
1840+
1841+
iio_for_each_active_channel(indio_dev, chan_index) {
1842+
ret = spi_sync(st->spi, &st->msg);
1843+
if (ret)
1844+
goto err_out;
1845+
1846+
memcpy(&st->bounce_buffer[i++], st->rx_buf, ARRAY_SIZE(st->rx_buf));
1847+
}
1848+
1849+
iio_push_to_buffers(indio_dev, st->bounce_buffer);
1850+
err_out:
1851+
iio_trigger_notify_done(indio_dev->trig);
1852+
return IRQ_HANDLED;
1853+
}
1854+
1855+
static const struct iio_trigger_ops ad4170_trigger_ops = {
1856+
.validate_device = iio_trigger_validate_own_device,
1857+
};
1858+
16861859
static irqreturn_t ad4170_irq_handler(int irq, void *dev_id)
16871860
{
16881861
struct iio_dev *indio_dev = dev_id;
16891862
struct ad4170_state *st = iio_priv(indio_dev);
16901863

1691-
complete(&st->completion);
1864+
if (iio_buffer_enabled(indio_dev))
1865+
iio_trigger_poll(st->trig);
1866+
else
1867+
complete(&st->completion);
16921868

16931869
return IRQ_HANDLED;
16941870
};
16951871

1872+
static int ad4170_trigger_setup(struct iio_dev *indio_dev)
1873+
{
1874+
struct ad4170_state *st = iio_priv(indio_dev);
1875+
struct device *dev = &st->spi->dev;
1876+
int ret;
1877+
1878+
st->trig = devm_iio_trigger_alloc(dev, "%s-trig%d",
1879+
indio_dev->name,
1880+
iio_device_id(indio_dev));
1881+
if (!st->trig)
1882+
return -ENOMEM;
1883+
1884+
st->trig->ops = &ad4170_trigger_ops;
1885+
1886+
iio_trigger_set_drvdata(st->trig, indio_dev);
1887+
ret = devm_iio_trigger_register(dev, st->trig);
1888+
if (ret)
1889+
return dev_err_probe(dev, ret, "Failed to register trigger\n");
1890+
1891+
indio_dev->trig = iio_trigger_get(st->trig);
1892+
1893+
return 0;
1894+
}
1895+
16961896
static int ad4170_regulator_setup(struct ad4170_state *st)
16971897
{
16981898
struct device *dev = &st->spi->dev;
@@ -1817,8 +2017,22 @@ static int ad4170_probe(struct spi_device *spi)
18172017
IRQF_ONESHOT, indio_dev->name, indio_dev);
18182018
if (ret)
18192019
return ret;
2020+
2021+
ret = ad4170_trigger_setup(indio_dev);
2022+
if (ret)
2023+
return ret;
18202024
}
18212025

2026+
ret = ad4170_prepare_spi_message(st);
2027+
if (ret)
2028+
return dev_err_probe(dev, ret, "Failed to prepare SPI message\n");
2029+
2030+
ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
2031+
&ad4170_trigger_handler,
2032+
&ad4170_buffer_ops);
2033+
if (ret)
2034+
return dev_err_probe(dev, ret, "Failed to setup read buffer\n");
2035+
18222036
return devm_iio_device_register(dev, indio_dev);
18232037
}
18242038

0 commit comments

Comments
 (0)