Skip to content

Commit 6cc7e4b

Browse files
dlechjic23
authored andcommitted
iio: adc: ad4695: implement triggered buffer
This implements buffered reads for the ad4695 driver using the typical triggered buffer implementation, including adding a soft timestamp channel. The chip has 4 different modes for doing conversions. The driver is using the advanced sequencer mode since that is the only mode that allows individual configuration of all aspects each channel (e.g. bipolar config currently and oversampling to be added in the future). Signed-off-by: David Lechner <[email protected]> Link: https://patch.msgid.link/20240813-iio-adc-ad4695-buffered-read-v2-1-9bb19fc1924b@baylibre.com Signed-off-by: Jonathan Cameron <[email protected]>
1 parent d3bde22 commit 6cc7e4b

File tree

1 file changed

+247
-3
lines changed

1 file changed

+247
-3
lines changed

drivers/iio/adc/ad4695.c

Lines changed: 247 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@
1111
* Copyright 2024 BayLibre, SAS
1212
*/
1313

14+
#include <linux/align.h>
1415
#include <linux/bitfield.h>
1516
#include <linux/bits.h>
1617
#include <linux/compiler.h>
1718
#include <linux/delay.h>
1819
#include <linux/device.h>
1920
#include <linux/err.h>
2021
#include <linux/gpio/consumer.h>
22+
#include <linux/iio/buffer.h>
2123
#include <linux/iio/iio.h>
24+
#include <linux/iio/triggered_buffer.h>
25+
#include <linux/iio/trigger_consumer.h>
2226
#include <linux/property.h>
2327
#include <linux/regmap.h>
2428
#include <linux/regulator/consumer.h>
@@ -61,6 +65,7 @@
6165
#define AD4695_REG_GPIO_CTRL 0x0026
6266
#define AD4695_REG_GP_MODE 0x0027
6367
#define AD4695_REG_TEMP_CTRL 0x0029
68+
#define AD4695_REG_TEMP_CTRL_TEMP_EN BIT(0)
6469
#define AD4695_REG_CONFIG_IN(n) (0x0030 | (n))
6570
#define AD4695_REG_CONFIG_IN_MODE BIT(6)
6671
#define AD4695_REG_CONFIG_IN_PAIR GENMASK(5, 4)
@@ -84,9 +89,13 @@
8489
#define AD4695_T_WAKEUP_HW_MS 3
8590
#define AD4695_T_WAKEUP_SW_MS 3
8691
#define AD4695_T_REFBUF_MS 100
92+
#define AD4695_T_REGCONFIG_NS 20
8793
#define AD4695_REG_ACCESS_SCLK_HZ (10 * MEGA)
8894

95+
/* Max number of voltage input channels. */
8996
#define AD4695_MAX_CHANNELS 16
97+
/* Max size of 1 raw sample in bytes. */
98+
#define AD4695_MAX_CHANNEL_SIZE 2
9099

91100
enum ad4695_in_pair {
92101
AD4695_IN_PAIR_REFGND,
@@ -97,6 +106,7 @@ enum ad4695_in_pair {
97106
struct ad4695_chip_info {
98107
const char *name;
99108
int max_sample_rate;
109+
u32 t_acq_ns;
100110
u8 num_voltage_inputs;
101111
};
102112

@@ -112,15 +122,21 @@ struct ad4695_state {
112122
struct spi_device *spi;
113123
struct regmap *regmap;
114124
struct gpio_desc *reset_gpio;
115-
struct iio_chan_spec iio_chan[AD4695_MAX_CHANNELS + 1];
125+
/* voltages channels plus temperature and timestamp */
126+
struct iio_chan_spec iio_chan[AD4695_MAX_CHANNELS + 2];
116127
struct ad4695_channel_config channels_cfg[AD4695_MAX_CHANNELS];
117128
const struct ad4695_chip_info *chip_info;
118129
/* Reference voltage. */
119130
unsigned int vref_mv;
120131
/* Common mode input pin voltage. */
121132
unsigned int com_mv;
133+
/* 1 per voltage and temperature chan plus 1 xfer to trigger 1st CNV */
134+
struct spi_transfer buf_read_xfer[AD4695_MAX_CHANNELS + 2];
135+
struct spi_message buf_read_msg;
122136
/* Raw conversion data received. */
123-
u16 raw_data __aligned(IIO_DMA_MINALIGN);
137+
u8 buf[ALIGN((AD4695_MAX_CHANNELS + 2) * AD4695_MAX_CHANNEL_SIZE,
138+
sizeof(s64)) + sizeof(s64)] __aligned(IIO_DMA_MINALIGN);
139+
u16 raw_data;
124140
/* Commands to send for single conversion. */
125141
u16 cnv_cmd;
126142
u8 cnv_cmd2;
@@ -193,31 +209,38 @@ static const struct iio_chan_spec ad4695_temp_channel_template = {
193209
},
194210
};
195211

212+
static const struct iio_chan_spec ad4695_soft_timestamp_channel_template =
213+
IIO_CHAN_SOFT_TIMESTAMP(0);
214+
196215
static const char * const ad4695_power_supplies[] = {
197216
"avdd", "vio"
198217
};
199218

200219
static const struct ad4695_chip_info ad4695_chip_info = {
201220
.name = "ad4695",
202221
.max_sample_rate = 500 * KILO,
222+
.t_acq_ns = 1715,
203223
.num_voltage_inputs = 16,
204224
};
205225

206226
static const struct ad4695_chip_info ad4696_chip_info = {
207227
.name = "ad4696",
208228
.max_sample_rate = 1 * MEGA,
229+
.t_acq_ns = 715,
209230
.num_voltage_inputs = 16,
210231
};
211232

212233
static const struct ad4695_chip_info ad4697_chip_info = {
213234
.name = "ad4697",
214235
.max_sample_rate = 500 * KILO,
236+
.t_acq_ns = 1715,
215237
.num_voltage_inputs = 8,
216238
};
217239

218240
static const struct ad4695_chip_info ad4698_chip_info = {
219241
.name = "ad4698",
220242
.max_sample_rate = 1 * MEGA,
243+
.t_acq_ns = 715,
221244
.num_voltage_inputs = 8,
222245
};
223246

@@ -254,6 +277,61 @@ static int ad4695_set_single_cycle_mode(struct ad4695_state *st,
254277
AD4695_REG_SETUP_SPI_CYC_CTRL);
255278
}
256279

280+
/**
281+
* ad4695_enter_advanced_sequencer_mode - Put the ADC in advanced sequencer mode
282+
* @st: The driver state
283+
* @n: The number of slots to use - must be >= 2, <= 128
284+
*
285+
* As per the datasheet, to enable advanced sequencer, we need to set
286+
* STD_SEQ_EN=0, NUM_SLOTS_AS=n-1 and CYC_CTRL=0 (Table 15). Setting SPI_MODE=1
287+
* triggers the first conversion using the channel in AS_SLOT0.
288+
*
289+
* Return: 0 on success, a negative error code on failure
290+
*/
291+
static int ad4695_enter_advanced_sequencer_mode(struct ad4695_state *st, u32 n)
292+
{
293+
int ret;
294+
295+
ret = regmap_update_bits(st->regmap, AD4695_REG_SEQ_CTRL,
296+
AD4695_REG_SEQ_CTRL_STD_SEQ_EN |
297+
AD4695_REG_SEQ_CTRL_NUM_SLOTS_AS,
298+
FIELD_PREP(AD4695_REG_SEQ_CTRL_STD_SEQ_EN, 0) |
299+
FIELD_PREP(AD4695_REG_SEQ_CTRL_NUM_SLOTS_AS, n - 1));
300+
if (ret)
301+
return ret;
302+
303+
return regmap_update_bits(st->regmap, AD4695_REG_SETUP,
304+
AD4695_REG_SETUP_SPI_MODE | AD4695_REG_SETUP_SPI_CYC_CTRL,
305+
FIELD_PREP(AD4695_REG_SETUP_SPI_MODE, 1) |
306+
FIELD_PREP(AD4695_REG_SETUP_SPI_CYC_CTRL, 0));
307+
}
308+
309+
/**
310+
* ad4695_exit_conversion_mode - Exit conversion mode
311+
* @st: The AD4695 state
312+
*
313+
* Sends SPI command to exit conversion mode.
314+
*
315+
* Return: 0 on success, a negative error code on failure
316+
*/
317+
static int ad4695_exit_conversion_mode(struct ad4695_state *st)
318+
{
319+
struct spi_transfer xfer = {
320+
.tx_buf = &st->cnv_cmd2,
321+
.len = 1,
322+
.delay.value = AD4695_T_REGCONFIG_NS,
323+
.delay.unit = SPI_DELAY_UNIT_NSECS,
324+
};
325+
326+
/*
327+
* Technically, could do a 5-bit transfer, but shifting to start of
328+
* 8 bits instead for better SPI controller support.
329+
*/
330+
st->cnv_cmd2 = AD4695_CMD_EXIT_CNV_MODE << 3;
331+
332+
return spi_sync_transfer(st->spi, &xfer, 1);
333+
}
334+
257335
static int ad4695_set_ref_voltage(struct ad4695_state *st, int vref_mv)
258336
{
259337
u8 val;
@@ -296,6 +374,161 @@ static int ad4695_write_chn_cfg(struct ad4695_state *st,
296374
mask, val);
297375
}
298376

377+
static int ad4695_buffer_preenable(struct iio_dev *indio_dev)
378+
{
379+
struct ad4695_state *st = iio_priv(indio_dev);
380+
struct spi_transfer *xfer;
381+
u8 temp_chan_bit = st->chip_info->num_voltage_inputs;
382+
u32 bit, num_xfer, num_slots;
383+
u32 temp_en = 0;
384+
int ret;
385+
386+
/*
387+
* We are using the advanced sequencer since it is the only way to read
388+
* multiple channels that allows individual configuration of each
389+
* voltage input channel. Slot 0 in the advanced sequencer is used to
390+
* account for the gap between trigger polls - we don't read data from
391+
* this slot. Each enabled voltage channel is assigned a slot starting
392+
* with slot 1.
393+
*/
394+
num_slots = 1;
395+
396+
memset(st->buf_read_xfer, 0, sizeof(st->buf_read_xfer));
397+
398+
/* First xfer is only to trigger conversion of slot 1, so no rx. */
399+
xfer = &st->buf_read_xfer[0];
400+
xfer->cs_change = 1;
401+
xfer->delay.value = st->chip_info->t_acq_ns;
402+
xfer->delay.unit = SPI_DELAY_UNIT_NSECS;
403+
xfer->cs_change_delay.value = AD4695_T_CONVERT_NS;
404+
xfer->cs_change_delay.unit = SPI_DELAY_UNIT_NSECS;
405+
num_xfer = 1;
406+
407+
iio_for_each_active_channel(indio_dev, bit) {
408+
xfer = &st->buf_read_xfer[num_xfer];
409+
xfer->bits_per_word = 16;
410+
xfer->rx_buf = &st->buf[(num_xfer - 1) * 2];
411+
xfer->len = 2;
412+
xfer->cs_change = 1;
413+
xfer->cs_change_delay.value = AD4695_T_CONVERT_NS;
414+
xfer->cs_change_delay.unit = SPI_DELAY_UNIT_NSECS;
415+
416+
if (bit == temp_chan_bit) {
417+
temp_en = 1;
418+
} else {
419+
ret = regmap_write(st->regmap,
420+
AD4695_REG_AS_SLOT(num_slots),
421+
FIELD_PREP(AD4695_REG_AS_SLOT_INX, bit));
422+
if (ret)
423+
return ret;
424+
425+
num_slots++;
426+
}
427+
428+
num_xfer++;
429+
}
430+
431+
/*
432+
* The advanced sequencer requires that at least 2 slots are enabled.
433+
* Since slot 0 is always used for other purposes, we need only 1
434+
* enabled voltage channel to meet this requirement. If the temperature
435+
* channel is the only enabled channel, we need to add one more slot
436+
* in the sequence but not read from it.
437+
*/
438+
if (num_slots < 2) {
439+
/* move last xfer so we can insert one more xfer before it */
440+
st->buf_read_xfer[num_xfer] = *xfer;
441+
num_xfer++;
442+
443+
/* modify 2nd to last xfer for extra slot */
444+
memset(xfer, 0, sizeof(*xfer));
445+
xfer->cs_change = 1;
446+
xfer->delay.value = st->chip_info->t_acq_ns;
447+
xfer->delay.unit = SPI_DELAY_UNIT_NSECS;
448+
xfer->cs_change_delay.value = AD4695_T_CONVERT_NS;
449+
xfer->cs_change_delay.unit = SPI_DELAY_UNIT_NSECS;
450+
xfer++;
451+
452+
/* and add the extra slot in the sequencer */
453+
ret = regmap_write(st->regmap,
454+
AD4695_REG_AS_SLOT(num_slots),
455+
FIELD_PREP(AD4695_REG_AS_SLOT_INX, 0));
456+
if (ret)
457+
return ret;
458+
459+
num_slots++;
460+
}
461+
462+
/*
463+
* Don't keep CS asserted after last xfer. Also triggers conversion of
464+
* slot 0.
465+
*/
466+
xfer->cs_change = 0;
467+
468+
/*
469+
* Temperature channel isn't included in the sequence, but rather
470+
* controlled by setting a bit in the TEMP_CTRL register.
471+
*/
472+
473+
ret = regmap_update_bits(st->regmap, AD4695_REG_TEMP_CTRL,
474+
AD4695_REG_TEMP_CTRL_TEMP_EN,
475+
FIELD_PREP(AD4695_REG_TEMP_CTRL_TEMP_EN, temp_en));
476+
if (ret)
477+
return ret;
478+
479+
spi_message_init_with_transfers(&st->buf_read_msg, st->buf_read_xfer,
480+
num_xfer);
481+
482+
ret = spi_optimize_message(st->spi, &st->buf_read_msg);
483+
if (ret)
484+
return ret;
485+
486+
/* This triggers conversion of slot 0. */
487+
ret = ad4695_enter_advanced_sequencer_mode(st, num_slots);
488+
if (ret)
489+
spi_unoptimize_message(&st->buf_read_msg);
490+
491+
return ret;
492+
}
493+
494+
static int ad4695_buffer_postdisable(struct iio_dev *indio_dev)
495+
{
496+
struct ad4695_state *st = iio_priv(indio_dev);
497+
int ret;
498+
499+
ret = ad4695_exit_conversion_mode(st);
500+
if (ret)
501+
return ret;
502+
503+
spi_unoptimize_message(&st->buf_read_msg);
504+
505+
return 0;
506+
}
507+
508+
static const struct iio_buffer_setup_ops ad4695_buffer_setup_ops = {
509+
.preenable = ad4695_buffer_preenable,
510+
.postdisable = ad4695_buffer_postdisable,
511+
};
512+
513+
static irqreturn_t ad4695_trigger_handler(int irq, void *p)
514+
{
515+
struct iio_poll_func *pf = p;
516+
struct iio_dev *indio_dev = pf->indio_dev;
517+
struct ad4695_state *st = iio_priv(indio_dev);
518+
int ret;
519+
520+
ret = spi_sync(st->spi, &st->buf_read_msg);
521+
if (ret)
522+
goto out;
523+
524+
iio_push_to_buffers_with_timestamp(indio_dev, st->buf, pf->timestamp);
525+
526+
out:
527+
iio_trigger_notify_done(indio_dev->trig);
528+
529+
return IRQ_HANDLED;
530+
}
531+
299532
/**
300533
* ad4695_read_one_sample - Read a single sample using single-cycle mode
301534
* @st: The AD4695 state
@@ -527,6 +760,10 @@ static int ad4695_parse_channel_cfg(struct ad4695_state *st)
527760
/* Temperature channel must be next scan index after voltage channels. */
528761
st->iio_chan[i] = ad4695_temp_channel_template;
529762
st->iio_chan[i].scan_index = i;
763+
i++;
764+
765+
st->iio_chan[i] = ad4695_soft_timestamp_channel_template;
766+
st->iio_chan[i].scan_index = i;
530767

531768
return 0;
532769
}
@@ -695,7 +932,14 @@ static int ad4695_probe(struct spi_device *spi)
695932
indio_dev->info = &ad4695_info;
696933
indio_dev->modes = INDIO_DIRECT_MODE;
697934
indio_dev->channels = st->iio_chan;
698-
indio_dev->num_channels = st->chip_info->num_voltage_inputs + 1;
935+
indio_dev->num_channels = st->chip_info->num_voltage_inputs + 2;
936+
937+
ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
938+
iio_pollfunc_store_time,
939+
ad4695_trigger_handler,
940+
&ad4695_buffer_setup_ops);
941+
if (ret)
942+
return ret;
699943

700944
return devm_iio_device_register(dev, indio_dev);
701945
}

0 commit comments

Comments
 (0)