1d1efcf88SDavid Lechner // SPDX-License-Identifier: GPL-2.0-only
2d1efcf88SDavid Lechner /*
3d1efcf88SDavid Lechner * Analog Devices AD7944/85/86 PulSAR ADC family driver.
4d1efcf88SDavid Lechner *
5d1efcf88SDavid Lechner * Copyright 2024 Analog Devices, Inc.
6d1efcf88SDavid Lechner * Copyright 2024 BayLibre, SAS
7d1efcf88SDavid Lechner */
8d1efcf88SDavid Lechner
964ce7d43SDavid Lechner #include <linux/align.h>
10d1efcf88SDavid Lechner #include <linux/bitfield.h>
11d1efcf88SDavid Lechner #include <linux/bitops.h>
12d1efcf88SDavid Lechner #include <linux/delay.h>
13d1efcf88SDavid Lechner #include <linux/device.h>
14d1efcf88SDavid Lechner #include <linux/err.h>
15d1efcf88SDavid Lechner #include <linux/gpio/consumer.h>
16d1efcf88SDavid Lechner #include <linux/module.h>
17d1efcf88SDavid Lechner #include <linux/property.h>
18d1efcf88SDavid Lechner #include <linux/regulator/consumer.h>
19cbc986cdSDavid Lechner #include <linux/spi/offload/consumer.h>
20d1efcf88SDavid Lechner #include <linux/spi/spi.h>
21d1efcf88SDavid Lechner #include <linux/string_helpers.h>
22cbc986cdSDavid Lechner #include <linux/units.h>
23d1efcf88SDavid Lechner
24d1efcf88SDavid Lechner #include <linux/iio/iio.h>
25d1efcf88SDavid Lechner #include <linux/iio/sysfs.h>
26cbc986cdSDavid Lechner #include <linux/iio/buffer-dmaengine.h>
27d1efcf88SDavid Lechner #include <linux/iio/trigger_consumer.h>
28d1efcf88SDavid Lechner #include <linux/iio/triggered_buffer.h>
29d1efcf88SDavid Lechner
30d1efcf88SDavid Lechner #define AD7944_INTERNAL_REF_MV 4096
31d1efcf88SDavid Lechner
32d1efcf88SDavid Lechner struct ad7944_timing_spec {
33d1efcf88SDavid Lechner /* Normal mode max conversion time (t_{CONV}). */
34d1efcf88SDavid Lechner unsigned int conv_ns;
35d1efcf88SDavid Lechner /* TURBO mode max conversion time (t_{CONV}). */
36d1efcf88SDavid Lechner unsigned int turbo_conv_ns;
37d1efcf88SDavid Lechner };
38d1efcf88SDavid Lechner
39346ae0e8SDavid Lechner enum ad7944_spi_mode {
40346ae0e8SDavid Lechner /* datasheet calls this "4-wire mode" */
41346ae0e8SDavid Lechner AD7944_SPI_MODE_DEFAULT,
42346ae0e8SDavid Lechner /* datasheet calls this "3-wire mode" (not related to SPI_3WIRE!) */
43346ae0e8SDavid Lechner AD7944_SPI_MODE_SINGLE,
44346ae0e8SDavid Lechner /* datasheet calls this "chain mode" */
45346ae0e8SDavid Lechner AD7944_SPI_MODE_CHAIN,
46346ae0e8SDavid Lechner };
47346ae0e8SDavid Lechner
48346ae0e8SDavid Lechner /* maps adi,spi-mode property value to enum */
49346ae0e8SDavid Lechner static const char * const ad7944_spi_modes[] = {
50346ae0e8SDavid Lechner [AD7944_SPI_MODE_DEFAULT] = "",
51346ae0e8SDavid Lechner [AD7944_SPI_MODE_SINGLE] = "single",
52346ae0e8SDavid Lechner [AD7944_SPI_MODE_CHAIN] = "chain",
53346ae0e8SDavid Lechner };
54346ae0e8SDavid Lechner
55d1efcf88SDavid Lechner struct ad7944_adc {
56d1efcf88SDavid Lechner struct spi_device *spi;
57346ae0e8SDavid Lechner enum ad7944_spi_mode spi_mode;
586020ca4dSDavid Lechner struct spi_transfer xfers[3];
596020ca4dSDavid Lechner struct spi_message msg;
60cbc986cdSDavid Lechner struct spi_transfer offload_xfers[2];
61cbc986cdSDavid Lechner struct spi_message offload_msg;
62cbc986cdSDavid Lechner struct spi_offload *offload;
63cbc986cdSDavid Lechner struct spi_offload_trigger *offload_trigger;
64cbc986cdSDavid Lechner unsigned long offload_trigger_hz;
65cbc986cdSDavid Lechner int sample_freq_range[3];
6664ce7d43SDavid Lechner void *chain_mode_buf;
67d1efcf88SDavid Lechner /* Chip-specific timing specifications. */
68d1efcf88SDavid Lechner const struct ad7944_timing_spec *timing_spec;
69d1efcf88SDavid Lechner /* GPIO connected to CNV pin. */
70d1efcf88SDavid Lechner struct gpio_desc *cnv;
71d1efcf88SDavid Lechner /* Optional GPIO to enable turbo mode. */
72d1efcf88SDavid Lechner struct gpio_desc *turbo;
73d1efcf88SDavid Lechner /* Indicates TURBO is hard-wired to be always enabled. */
74d1efcf88SDavid Lechner bool always_turbo;
75d1efcf88SDavid Lechner /* Reference voltage (millivolts). */
76d1efcf88SDavid Lechner unsigned int ref_mv;
77d1efcf88SDavid Lechner
78d1efcf88SDavid Lechner /*
79d1efcf88SDavid Lechner * DMA (thus cache coherency maintenance) requires the
80d1efcf88SDavid Lechner * transfer buffers to live in their own cache lines.
81d1efcf88SDavid Lechner */
82d1efcf88SDavid Lechner struct {
83d1efcf88SDavid Lechner union {
84d1efcf88SDavid Lechner u16 u16;
85d1efcf88SDavid Lechner u32 u32;
86d1efcf88SDavid Lechner } raw;
87bed883e4SJonathan Cameron aligned_s64 timestamp;
88d1efcf88SDavid Lechner } sample __aligned(IIO_DMA_MINALIGN);
89d1efcf88SDavid Lechner };
90d1efcf88SDavid Lechner
91346ae0e8SDavid Lechner /* quite time before CNV rising edge */
923eb27cf1SDavid Lechner #define AD7944_T_QUIET_NS 20
93cbc986cdSDavid Lechner /* minimum CNV high time to trigger conversion */
94cbc986cdSDavid Lechner #define AD7944_T_CNVH_NS 10
95346ae0e8SDavid Lechner
96d1efcf88SDavid Lechner static const struct ad7944_timing_spec ad7944_timing_spec = {
97d1efcf88SDavid Lechner .conv_ns = 420,
98d1efcf88SDavid Lechner .turbo_conv_ns = 320,
99d1efcf88SDavid Lechner };
100d1efcf88SDavid Lechner
101d1efcf88SDavid Lechner static const struct ad7944_timing_spec ad7986_timing_spec = {
102d1efcf88SDavid Lechner .conv_ns = 500,
103d1efcf88SDavid Lechner .turbo_conv_ns = 400,
104d1efcf88SDavid Lechner };
105d1efcf88SDavid Lechner
106d1efcf88SDavid Lechner struct ad7944_chip_info {
107d1efcf88SDavid Lechner const char *name;
108d1efcf88SDavid Lechner const struct ad7944_timing_spec *timing_spec;
109cbc986cdSDavid Lechner u32 max_sample_rate_hz;
110d1efcf88SDavid Lechner const struct iio_chan_spec channels[2];
111cbc986cdSDavid Lechner const struct iio_chan_spec offload_channels[1];
112d1efcf88SDavid Lechner };
113d1efcf88SDavid Lechner
114503d20edSDavid Lechner /* get number of bytes for SPI xfer */
115503d20edSDavid Lechner #define AD7944_SPI_BYTES(scan_type) ((scan_type).realbits > 16 ? 4 : 2)
116503d20edSDavid Lechner
117d1efcf88SDavid Lechner /*
118d1efcf88SDavid Lechner * AD7944_DEFINE_CHIP_INFO - Define a chip info structure for a specific chip
119d1efcf88SDavid Lechner * @_name: The name of the chip
120d1efcf88SDavid Lechner * @_ts: The timing specification for the chip
121cbc986cdSDavid Lechner * @_max: The maximum sample rate in Hz
122d1efcf88SDavid Lechner * @_bits: The number of bits in the conversion result
123d1efcf88SDavid Lechner * @_diff: Whether the chip is true differential or not
124d1efcf88SDavid Lechner */
125cbc986cdSDavid Lechner #define AD7944_DEFINE_CHIP_INFO(_name, _ts, _max, _bits, _diff) \
126d1efcf88SDavid Lechner static const struct ad7944_chip_info _name##_chip_info = { \
127d1efcf88SDavid Lechner .name = #_name, \
128d1efcf88SDavid Lechner .timing_spec = &_ts##_timing_spec, \
129cbc986cdSDavid Lechner .max_sample_rate_hz = _max, \
130d1efcf88SDavid Lechner .channels = { \
131d1efcf88SDavid Lechner { \
132d1efcf88SDavid Lechner .type = IIO_VOLTAGE, \
133d1efcf88SDavid Lechner .indexed = 1, \
134d1efcf88SDavid Lechner .differential = _diff, \
135d1efcf88SDavid Lechner .channel = 0, \
136d1efcf88SDavid Lechner .channel2 = _diff ? 1 : 0, \
137d1efcf88SDavid Lechner .scan_index = 0, \
138d1efcf88SDavid Lechner .scan_type.sign = _diff ? 's' : 'u', \
139d1efcf88SDavid Lechner .scan_type.realbits = _bits, \
140d1efcf88SDavid Lechner .scan_type.storagebits = _bits > 16 ? 32 : 16, \
141d1efcf88SDavid Lechner .scan_type.endianness = IIO_CPU, \
142d1efcf88SDavid Lechner .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) \
143d1efcf88SDavid Lechner | BIT(IIO_CHAN_INFO_SCALE), \
144d1efcf88SDavid Lechner }, \
145d1efcf88SDavid Lechner IIO_CHAN_SOFT_TIMESTAMP(1), \
146d1efcf88SDavid Lechner }, \
147cbc986cdSDavid Lechner .offload_channels = { \
148cbc986cdSDavid Lechner { \
149cbc986cdSDavid Lechner .type = IIO_VOLTAGE, \
150cbc986cdSDavid Lechner .indexed = 1, \
151cbc986cdSDavid Lechner .differential = _diff, \
152cbc986cdSDavid Lechner .channel = 0, \
153cbc986cdSDavid Lechner .channel2 = _diff ? 1 : 0, \
154cbc986cdSDavid Lechner .scan_index = 0, \
155cbc986cdSDavid Lechner .scan_type.sign = _diff ? 's' : 'u', \
156cbc986cdSDavid Lechner .scan_type.realbits = _bits, \
157cbc986cdSDavid Lechner .scan_type.storagebits = 32, \
158cbc986cdSDavid Lechner .scan_type.endianness = IIO_CPU, \
159cbc986cdSDavid Lechner .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) \
160cbc986cdSDavid Lechner | BIT(IIO_CHAN_INFO_SCALE) \
161cbc986cdSDavid Lechner | BIT(IIO_CHAN_INFO_SAMP_FREQ), \
162cbc986cdSDavid Lechner .info_mask_separate_available = \
163cbc986cdSDavid Lechner BIT(IIO_CHAN_INFO_SAMP_FREQ), \
164cbc986cdSDavid Lechner }, \
165cbc986cdSDavid Lechner }, \
166d1efcf88SDavid Lechner }
167d1efcf88SDavid Lechner
168cbc986cdSDavid Lechner /*
169cbc986cdSDavid Lechner * Notes on the offload channels:
170cbc986cdSDavid Lechner * - There is no soft timestamp since everything is done in hardware.
171cbc986cdSDavid Lechner * - There is a sampling frequency attribute added. This controls the SPI
172cbc986cdSDavid Lechner * offload trigger.
173cbc986cdSDavid Lechner * - The storagebits value depends on the SPI offload provider. Currently there
174cbc986cdSDavid Lechner * is only one supported provider, namely the ADI PULSAR ADC HDL project,
175cbc986cdSDavid Lechner * which always uses 32-bit words for data values, even for <= 16-bit ADCs.
176cbc986cdSDavid Lechner * So the value is just hardcoded to 32 for now.
177cbc986cdSDavid Lechner */
178cbc986cdSDavid Lechner
179d1efcf88SDavid Lechner /* pseudo-differential with ground sense */
180cbc986cdSDavid Lechner AD7944_DEFINE_CHIP_INFO(ad7944, ad7944, 2.5 * MEGA, 14, 0);
181cbc986cdSDavid Lechner AD7944_DEFINE_CHIP_INFO(ad7985, ad7944, 2.5 * MEGA, 16, 0);
182d1efcf88SDavid Lechner /* fully differential */
183cbc986cdSDavid Lechner AD7944_DEFINE_CHIP_INFO(ad7986, ad7986, 2 * MEGA, 18, 1);
184d1efcf88SDavid Lechner
ad7944_3wire_cs_mode_init_msg(struct device * dev,struct ad7944_adc * adc,const struct iio_chan_spec * chan)1856020ca4dSDavid Lechner static int ad7944_3wire_cs_mode_init_msg(struct device *dev, struct ad7944_adc *adc,
1866020ca4dSDavid Lechner const struct iio_chan_spec *chan)
1876020ca4dSDavid Lechner {
1886020ca4dSDavid Lechner unsigned int t_conv_ns = adc->always_turbo ? adc->timing_spec->turbo_conv_ns
1896020ca4dSDavid Lechner : adc->timing_spec->conv_ns;
1906020ca4dSDavid Lechner struct spi_transfer *xfers = adc->xfers;
1916020ca4dSDavid Lechner
1926020ca4dSDavid Lechner /*
1936020ca4dSDavid Lechner * CS is tied to CNV and we need a low to high transition to start the
1946020ca4dSDavid Lechner * conversion, so place CNV low for t_QUIET to prepare for this.
1956020ca4dSDavid Lechner */
1963eb27cf1SDavid Lechner xfers[0].delay.value = AD7944_T_QUIET_NS;
1976020ca4dSDavid Lechner xfers[0].delay.unit = SPI_DELAY_UNIT_NSECS;
1986020ca4dSDavid Lechner
1996020ca4dSDavid Lechner /*
2006020ca4dSDavid Lechner * CS has to be high for full conversion time to avoid triggering the
2016020ca4dSDavid Lechner * busy indication.
2026020ca4dSDavid Lechner */
2036020ca4dSDavid Lechner xfers[1].cs_off = 1;
2046020ca4dSDavid Lechner xfers[1].delay.value = t_conv_ns;
2056020ca4dSDavid Lechner xfers[1].delay.unit = SPI_DELAY_UNIT_NSECS;
2066020ca4dSDavid Lechner
2076020ca4dSDavid Lechner /* Then we can read the data during the acquisition phase */
2086020ca4dSDavid Lechner xfers[2].rx_buf = &adc->sample.raw;
209503d20edSDavid Lechner xfers[2].len = AD7944_SPI_BYTES(chan->scan_type);
2106020ca4dSDavid Lechner xfers[2].bits_per_word = chan->scan_type.realbits;
2116020ca4dSDavid Lechner
2126020ca4dSDavid Lechner spi_message_init_with_transfers(&adc->msg, xfers, 3);
2136020ca4dSDavid Lechner
214340fa834SDavid Lechner return devm_spi_optimize_message(dev, adc->spi, &adc->msg);
2156020ca4dSDavid Lechner }
2166020ca4dSDavid Lechner
ad7944_4wire_mode_init_msg(struct device * dev,struct ad7944_adc * adc,const struct iio_chan_spec * chan)2176020ca4dSDavid Lechner static int ad7944_4wire_mode_init_msg(struct device *dev, struct ad7944_adc *adc,
2186020ca4dSDavid Lechner const struct iio_chan_spec *chan)
2196020ca4dSDavid Lechner {
2206020ca4dSDavid Lechner unsigned int t_conv_ns = adc->always_turbo ? adc->timing_spec->turbo_conv_ns
2216020ca4dSDavid Lechner : adc->timing_spec->conv_ns;
2226020ca4dSDavid Lechner struct spi_transfer *xfers = adc->xfers;
2236020ca4dSDavid Lechner
2246020ca4dSDavid Lechner /*
2256020ca4dSDavid Lechner * CS has to be high for full conversion time to avoid triggering the
2266020ca4dSDavid Lechner * busy indication.
2276020ca4dSDavid Lechner */
2286020ca4dSDavid Lechner xfers[0].cs_off = 1;
2296020ca4dSDavid Lechner xfers[0].delay.value = t_conv_ns;
2306020ca4dSDavid Lechner xfers[0].delay.unit = SPI_DELAY_UNIT_NSECS;
2316020ca4dSDavid Lechner
2326020ca4dSDavid Lechner xfers[1].rx_buf = &adc->sample.raw;
233503d20edSDavid Lechner xfers[1].len = AD7944_SPI_BYTES(chan->scan_type);
2346020ca4dSDavid Lechner xfers[1].bits_per_word = chan->scan_type.realbits;
2356020ca4dSDavid Lechner
2366020ca4dSDavid Lechner spi_message_init_with_transfers(&adc->msg, xfers, 2);
2376020ca4dSDavid Lechner
238340fa834SDavid Lechner return devm_spi_optimize_message(dev, adc->spi, &adc->msg);
2396020ca4dSDavid Lechner }
2406020ca4dSDavid Lechner
ad7944_chain_mode_init_msg(struct device * dev,struct ad7944_adc * adc,const struct iio_chan_spec * chan,u32 n_chain_dev)24164ce7d43SDavid Lechner static int ad7944_chain_mode_init_msg(struct device *dev, struct ad7944_adc *adc,
24264ce7d43SDavid Lechner const struct iio_chan_spec *chan,
24364ce7d43SDavid Lechner u32 n_chain_dev)
24464ce7d43SDavid Lechner {
24564ce7d43SDavid Lechner struct spi_transfer *xfers = adc->xfers;
24664ce7d43SDavid Lechner
24764ce7d43SDavid Lechner /*
24864ce7d43SDavid Lechner * NB: SCLK has to be low before we toggle CS to avoid triggering the
24964ce7d43SDavid Lechner * busy indication.
25064ce7d43SDavid Lechner */
25164ce7d43SDavid Lechner if (adc->spi->mode & SPI_CPOL)
25264ce7d43SDavid Lechner return dev_err_probe(dev, -EINVAL,
25364ce7d43SDavid Lechner "chain mode requires ~SPI_CPOL\n");
25464ce7d43SDavid Lechner
25564ce7d43SDavid Lechner /*
25664ce7d43SDavid Lechner * We only support CNV connected to CS in chain mode and we need CNV
25764ce7d43SDavid Lechner * to be high during the transfer to trigger the conversion.
25864ce7d43SDavid Lechner */
25964ce7d43SDavid Lechner if (!(adc->spi->mode & SPI_CS_HIGH))
26064ce7d43SDavid Lechner return dev_err_probe(dev, -EINVAL,
26164ce7d43SDavid Lechner "chain mode requires SPI_CS_HIGH\n");
26264ce7d43SDavid Lechner
26364ce7d43SDavid Lechner /* CNV has to be high for full conversion time before reading data. */
26464ce7d43SDavid Lechner xfers[0].delay.value = adc->timing_spec->conv_ns;
26564ce7d43SDavid Lechner xfers[0].delay.unit = SPI_DELAY_UNIT_NSECS;
26664ce7d43SDavid Lechner
26764ce7d43SDavid Lechner xfers[1].rx_buf = adc->chain_mode_buf;
268503d20edSDavid Lechner xfers[1].len = AD7944_SPI_BYTES(chan->scan_type) * n_chain_dev;
26964ce7d43SDavid Lechner xfers[1].bits_per_word = chan->scan_type.realbits;
27064ce7d43SDavid Lechner
27164ce7d43SDavid Lechner spi_message_init_with_transfers(&adc->msg, xfers, 2);
27264ce7d43SDavid Lechner
273340fa834SDavid Lechner return devm_spi_optimize_message(dev, adc->spi, &adc->msg);
27464ce7d43SDavid Lechner }
27564ce7d43SDavid Lechner
276cbc986cdSDavid Lechner /*
277cbc986cdSDavid Lechner * Unlike ad7944_3wire_cs_mode_init_msg(), this creates a message that reads
278cbc986cdSDavid Lechner * during the conversion phase instead of the acquisition phase when reading
279cbc986cdSDavid Lechner * a sample from the ADC. This is needed to be able to read at the maximum
280cbc986cdSDavid Lechner * sample rate. It requires the SPI controller to have offload support and a
281cbc986cdSDavid Lechner * high enough SCLK rate to read the sample during the conversion phase.
282cbc986cdSDavid Lechner */
ad7944_3wire_cs_mode_init_offload_msg(struct device * dev,struct ad7944_adc * adc,const struct iio_chan_spec * chan)283cbc986cdSDavid Lechner static int ad7944_3wire_cs_mode_init_offload_msg(struct device *dev,
284cbc986cdSDavid Lechner struct ad7944_adc *adc,
285cbc986cdSDavid Lechner const struct iio_chan_spec *chan)
286cbc986cdSDavid Lechner {
287cbc986cdSDavid Lechner struct spi_transfer *xfers = adc->offload_xfers;
288cbc986cdSDavid Lechner int ret;
289cbc986cdSDavid Lechner
290cbc986cdSDavid Lechner /*
291cbc986cdSDavid Lechner * CS is tied to CNV and we need a low to high transition to start the
292cbc986cdSDavid Lechner * conversion, so place CNV low for t_QUIET to prepare for this.
293cbc986cdSDavid Lechner */
294cbc986cdSDavid Lechner xfers[0].delay.value = AD7944_T_QUIET_NS;
295cbc986cdSDavid Lechner xfers[0].delay.unit = SPI_DELAY_UNIT_NSECS;
296cbc986cdSDavid Lechner /* CNV has to be high for a minimum time to trigger conversion. */
297cbc986cdSDavid Lechner xfers[0].cs_change = 1;
298cbc986cdSDavid Lechner xfers[0].cs_change_delay.value = AD7944_T_CNVH_NS;
299cbc986cdSDavid Lechner xfers[0].cs_change_delay.unit = SPI_DELAY_UNIT_NSECS;
300cbc986cdSDavid Lechner
301cbc986cdSDavid Lechner /* Then we can read the previous sample during the conversion phase */
302cbc986cdSDavid Lechner xfers[1].offload_flags = SPI_OFFLOAD_XFER_RX_STREAM;
303cbc986cdSDavid Lechner xfers[1].len = AD7944_SPI_BYTES(chan->scan_type);
304cbc986cdSDavid Lechner xfers[1].bits_per_word = chan->scan_type.realbits;
305cbc986cdSDavid Lechner
306cbc986cdSDavid Lechner spi_message_init_with_transfers(&adc->offload_msg, xfers,
307cbc986cdSDavid Lechner ARRAY_SIZE(adc->offload_xfers));
308cbc986cdSDavid Lechner
309cbc986cdSDavid Lechner adc->offload_msg.offload = adc->offload;
310cbc986cdSDavid Lechner
311cbc986cdSDavid Lechner ret = devm_spi_optimize_message(dev, adc->spi, &adc->offload_msg);
312cbc986cdSDavid Lechner if (ret)
313cbc986cdSDavid Lechner return dev_err_probe(dev, ret, "failed to prepare offload msg\n");
314cbc986cdSDavid Lechner
315cbc986cdSDavid Lechner return 0;
316cbc986cdSDavid Lechner }
317cbc986cdSDavid Lechner
31861c8031aSDavid Lechner /**
31961c8031aSDavid Lechner * ad7944_convert_and_acquire - Perform a single conversion and acquisition
320346ae0e8SDavid Lechner * @adc: The ADC device structure
321346ae0e8SDavid Lechner * Return: 0 on success, a negative error code on failure
322346ae0e8SDavid Lechner *
32361c8031aSDavid Lechner * Perform a conversion and acquisition of a single sample using the
32461c8031aSDavid Lechner * pre-optimized adc->msg.
325346ae0e8SDavid Lechner *
32664ce7d43SDavid Lechner * Upon successful return adc->sample.raw will contain the conversion result
32764ce7d43SDavid Lechner * (or adc->chain_mode_buf if the device is using chain mode).
328346ae0e8SDavid Lechner */
ad7944_convert_and_acquire(struct ad7944_adc * adc)32933c33a96SDavid Lechner static int ad7944_convert_and_acquire(struct ad7944_adc *adc)
330d1efcf88SDavid Lechner {
331d1efcf88SDavid Lechner int ret;
332d1efcf88SDavid Lechner
333d1efcf88SDavid Lechner /*
334d1efcf88SDavid Lechner * In 4-wire mode, the CNV line is held high for the entire conversion
33561c8031aSDavid Lechner * and acquisition process. In other modes adc->cnv is NULL and is
33661c8031aSDavid Lechner * ignored (CS is wired to CNV in those cases).
337d1efcf88SDavid Lechner */
338d1efcf88SDavid Lechner gpiod_set_value_cansleep(adc->cnv, 1);
3396020ca4dSDavid Lechner ret = spi_sync(adc->spi, &adc->msg);
340d1efcf88SDavid Lechner gpiod_set_value_cansleep(adc->cnv, 0);
341d1efcf88SDavid Lechner
342d1efcf88SDavid Lechner return ret;
343d1efcf88SDavid Lechner }
344d1efcf88SDavid Lechner
ad7944_single_conversion(struct ad7944_adc * adc,const struct iio_chan_spec * chan,int * val)345d1efcf88SDavid Lechner static int ad7944_single_conversion(struct ad7944_adc *adc,
346d1efcf88SDavid Lechner const struct iio_chan_spec *chan,
347d1efcf88SDavid Lechner int *val)
348d1efcf88SDavid Lechner {
349d1efcf88SDavid Lechner int ret;
350d1efcf88SDavid Lechner
35133c33a96SDavid Lechner ret = ad7944_convert_and_acquire(adc);
352d1efcf88SDavid Lechner if (ret)
353d1efcf88SDavid Lechner return ret;
354d1efcf88SDavid Lechner
35564ce7d43SDavid Lechner if (adc->spi_mode == AD7944_SPI_MODE_CHAIN) {
356503d20edSDavid Lechner if (chan->scan_type.realbits > 16)
35764ce7d43SDavid Lechner *val = ((u32 *)adc->chain_mode_buf)[chan->scan_index];
35864ce7d43SDavid Lechner else
35964ce7d43SDavid Lechner *val = ((u16 *)adc->chain_mode_buf)[chan->scan_index];
36064ce7d43SDavid Lechner } else {
361503d20edSDavid Lechner if (chan->scan_type.realbits > 16)
362d1efcf88SDavid Lechner *val = adc->sample.raw.u32;
363d1efcf88SDavid Lechner else
364d1efcf88SDavid Lechner *val = adc->sample.raw.u16;
36564ce7d43SDavid Lechner }
366d1efcf88SDavid Lechner
367d1efcf88SDavid Lechner if (chan->scan_type.sign == 's')
368d1efcf88SDavid Lechner *val = sign_extend32(*val, chan->scan_type.realbits - 1);
369*7cdfbc01SDavid Lechner else
370*7cdfbc01SDavid Lechner *val &= GENMASK(chan->scan_type.realbits - 1, 0);
371d1efcf88SDavid Lechner
372d1efcf88SDavid Lechner return IIO_VAL_INT;
373d1efcf88SDavid Lechner }
374d1efcf88SDavid Lechner
ad7944_read_avail(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,const int ** vals,int * type,int * length,long mask)375cbc986cdSDavid Lechner static int ad7944_read_avail(struct iio_dev *indio_dev,
376cbc986cdSDavid Lechner struct iio_chan_spec const *chan,
377cbc986cdSDavid Lechner const int **vals, int *type, int *length,
378cbc986cdSDavid Lechner long mask)
379cbc986cdSDavid Lechner {
380cbc986cdSDavid Lechner struct ad7944_adc *adc = iio_priv(indio_dev);
381cbc986cdSDavid Lechner
382cbc986cdSDavid Lechner switch (mask) {
383cbc986cdSDavid Lechner case IIO_CHAN_INFO_SAMP_FREQ:
384cbc986cdSDavid Lechner *vals = adc->sample_freq_range;
385cbc986cdSDavid Lechner *type = IIO_VAL_INT;
386cbc986cdSDavid Lechner return IIO_AVAIL_RANGE;
387cbc986cdSDavid Lechner default:
388cbc986cdSDavid Lechner return -EINVAL;
389cbc986cdSDavid Lechner }
390cbc986cdSDavid Lechner }
391cbc986cdSDavid Lechner
ad7944_read_raw(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,int * val,int * val2,long info)392d1efcf88SDavid Lechner static int ad7944_read_raw(struct iio_dev *indio_dev,
393d1efcf88SDavid Lechner const struct iio_chan_spec *chan,
394d1efcf88SDavid Lechner int *val, int *val2, long info)
395d1efcf88SDavid Lechner {
396d1efcf88SDavid Lechner struct ad7944_adc *adc = iio_priv(indio_dev);
397d1efcf88SDavid Lechner int ret;
398d1efcf88SDavid Lechner
399d1efcf88SDavid Lechner switch (info) {
400d1efcf88SDavid Lechner case IIO_CHAN_INFO_RAW:
4011a21a984SJonathan Cameron if (!iio_device_claim_direct(indio_dev))
4021a21a984SJonathan Cameron return -EBUSY;
403d1efcf88SDavid Lechner
404d1efcf88SDavid Lechner ret = ad7944_single_conversion(adc, chan, val);
4051a21a984SJonathan Cameron iio_device_release_direct(indio_dev);
406d1efcf88SDavid Lechner return ret;
407d1efcf88SDavid Lechner
408d1efcf88SDavid Lechner case IIO_CHAN_INFO_SCALE:
409d1efcf88SDavid Lechner switch (chan->type) {
410d1efcf88SDavid Lechner case IIO_VOLTAGE:
411d1efcf88SDavid Lechner *val = adc->ref_mv;
412d1efcf88SDavid Lechner
413d1efcf88SDavid Lechner if (chan->scan_type.sign == 's')
414d1efcf88SDavid Lechner *val2 = chan->scan_type.realbits - 1;
415d1efcf88SDavid Lechner else
416d1efcf88SDavid Lechner *val2 = chan->scan_type.realbits;
417d1efcf88SDavid Lechner
418d1efcf88SDavid Lechner return IIO_VAL_FRACTIONAL_LOG2;
419d1efcf88SDavid Lechner default:
420d1efcf88SDavid Lechner return -EINVAL;
421d1efcf88SDavid Lechner }
422d1efcf88SDavid Lechner
423cbc986cdSDavid Lechner case IIO_CHAN_INFO_SAMP_FREQ:
424cbc986cdSDavid Lechner *val = adc->offload_trigger_hz;
425cbc986cdSDavid Lechner return IIO_VAL_INT;
426cbc986cdSDavid Lechner
427d1efcf88SDavid Lechner default:
428d1efcf88SDavid Lechner return -EINVAL;
429d1efcf88SDavid Lechner }
430d1efcf88SDavid Lechner }
431d1efcf88SDavid Lechner
ad7944_set_sample_freq(struct ad7944_adc * adc,int val)432cbc986cdSDavid Lechner static int ad7944_set_sample_freq(struct ad7944_adc *adc, int val)
433cbc986cdSDavid Lechner {
434cbc986cdSDavid Lechner struct spi_offload_trigger_config config = {
435cbc986cdSDavid Lechner .type = SPI_OFFLOAD_TRIGGER_PERIODIC,
436cbc986cdSDavid Lechner .periodic = {
437cbc986cdSDavid Lechner .frequency_hz = val,
438cbc986cdSDavid Lechner },
439cbc986cdSDavid Lechner };
440cbc986cdSDavid Lechner int ret;
441cbc986cdSDavid Lechner
442cbc986cdSDavid Lechner ret = spi_offload_trigger_validate(adc->offload_trigger, &config);
443cbc986cdSDavid Lechner if (ret)
444cbc986cdSDavid Lechner return ret;
445cbc986cdSDavid Lechner
446cbc986cdSDavid Lechner adc->offload_trigger_hz = config.periodic.frequency_hz;
447cbc986cdSDavid Lechner
448cbc986cdSDavid Lechner return 0;
449cbc986cdSDavid Lechner }
450cbc986cdSDavid Lechner
ad7944_write_raw(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,int val,int val2,long info)451cbc986cdSDavid Lechner static int ad7944_write_raw(struct iio_dev *indio_dev,
452cbc986cdSDavid Lechner const struct iio_chan_spec *chan,
453cbc986cdSDavid Lechner int val, int val2, long info)
454cbc986cdSDavid Lechner {
455cbc986cdSDavid Lechner struct ad7944_adc *adc = iio_priv(indio_dev);
456cbc986cdSDavid Lechner
457cbc986cdSDavid Lechner switch (info) {
458cbc986cdSDavid Lechner case IIO_CHAN_INFO_SAMP_FREQ:
459cbc986cdSDavid Lechner if (val < 1 || val > adc->sample_freq_range[2])
460cbc986cdSDavid Lechner return -EINVAL;
461cbc986cdSDavid Lechner
462cbc986cdSDavid Lechner return ad7944_set_sample_freq(adc, val);
463cbc986cdSDavid Lechner default:
464cbc986cdSDavid Lechner return -EINVAL;
465cbc986cdSDavid Lechner }
466cbc986cdSDavid Lechner }
467cbc986cdSDavid Lechner
ad7944_write_raw_get_fmt(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,long mask)468cbc986cdSDavid Lechner static int ad7944_write_raw_get_fmt(struct iio_dev *indio_dev,
469cbc986cdSDavid Lechner const struct iio_chan_spec *chan,
470cbc986cdSDavid Lechner long mask)
471cbc986cdSDavid Lechner {
472cbc986cdSDavid Lechner switch (mask) {
473cbc986cdSDavid Lechner case IIO_CHAN_INFO_SAMP_FREQ:
474cbc986cdSDavid Lechner return IIO_VAL_INT;
475cbc986cdSDavid Lechner default:
476cbc986cdSDavid Lechner return IIO_VAL_INT_PLUS_MICRO;
477cbc986cdSDavid Lechner }
478cbc986cdSDavid Lechner }
479cbc986cdSDavid Lechner
480d1efcf88SDavid Lechner static const struct iio_info ad7944_iio_info = {
481cbc986cdSDavid Lechner .read_avail = &ad7944_read_avail,
482d1efcf88SDavid Lechner .read_raw = &ad7944_read_raw,
483cbc986cdSDavid Lechner .write_raw = &ad7944_write_raw,
484cbc986cdSDavid Lechner .write_raw_get_fmt = &ad7944_write_raw_get_fmt,
485cbc986cdSDavid Lechner };
486cbc986cdSDavid Lechner
ad7944_offload_buffer_postenable(struct iio_dev * indio_dev)487cbc986cdSDavid Lechner static int ad7944_offload_buffer_postenable(struct iio_dev *indio_dev)
488cbc986cdSDavid Lechner {
489cbc986cdSDavid Lechner struct ad7944_adc *adc = iio_priv(indio_dev);
490cbc986cdSDavid Lechner struct spi_offload_trigger_config config = {
491cbc986cdSDavid Lechner .type = SPI_OFFLOAD_TRIGGER_PERIODIC,
492cbc986cdSDavid Lechner .periodic = {
493cbc986cdSDavid Lechner .frequency_hz = adc->offload_trigger_hz,
494cbc986cdSDavid Lechner },
495cbc986cdSDavid Lechner };
496cbc986cdSDavid Lechner int ret;
497cbc986cdSDavid Lechner
498cbc986cdSDavid Lechner gpiod_set_value_cansleep(adc->turbo, 1);
499cbc986cdSDavid Lechner
500cbc986cdSDavid Lechner ret = spi_offload_trigger_enable(adc->offload, adc->offload_trigger,
501cbc986cdSDavid Lechner &config);
502cbc986cdSDavid Lechner if (ret)
503cbc986cdSDavid Lechner gpiod_set_value_cansleep(adc->turbo, 0);
504cbc986cdSDavid Lechner
505cbc986cdSDavid Lechner return ret;
506cbc986cdSDavid Lechner }
507cbc986cdSDavid Lechner
ad7944_offload_buffer_predisable(struct iio_dev * indio_dev)508cbc986cdSDavid Lechner static int ad7944_offload_buffer_predisable(struct iio_dev *indio_dev)
509cbc986cdSDavid Lechner {
510cbc986cdSDavid Lechner struct ad7944_adc *adc = iio_priv(indio_dev);
511cbc986cdSDavid Lechner
512cbc986cdSDavid Lechner spi_offload_trigger_disable(adc->offload, adc->offload_trigger);
513cbc986cdSDavid Lechner gpiod_set_value_cansleep(adc->turbo, 0);
514cbc986cdSDavid Lechner
515cbc986cdSDavid Lechner return 0;
516cbc986cdSDavid Lechner }
517cbc986cdSDavid Lechner
518cbc986cdSDavid Lechner static const struct iio_buffer_setup_ops ad7944_offload_buffer_setup_ops = {
519cbc986cdSDavid Lechner .postenable = &ad7944_offload_buffer_postenable,
520cbc986cdSDavid Lechner .predisable = &ad7944_offload_buffer_predisable,
521d1efcf88SDavid Lechner };
522d1efcf88SDavid Lechner
ad7944_trigger_handler(int irq,void * p)523d1efcf88SDavid Lechner static irqreturn_t ad7944_trigger_handler(int irq, void *p)
524d1efcf88SDavid Lechner {
525d1efcf88SDavid Lechner struct iio_poll_func *pf = p;
526d1efcf88SDavid Lechner struct iio_dev *indio_dev = pf->indio_dev;
527d1efcf88SDavid Lechner struct ad7944_adc *adc = iio_priv(indio_dev);
528d1efcf88SDavid Lechner int ret;
529d1efcf88SDavid Lechner
53033c33a96SDavid Lechner ret = ad7944_convert_and_acquire(adc);
531d1efcf88SDavid Lechner if (ret)
532d1efcf88SDavid Lechner goto out;
533d1efcf88SDavid Lechner
53464ce7d43SDavid Lechner if (adc->spi_mode == AD7944_SPI_MODE_CHAIN)
53564ce7d43SDavid Lechner iio_push_to_buffers_with_timestamp(indio_dev, adc->chain_mode_buf,
53664ce7d43SDavid Lechner pf->timestamp);
53764ce7d43SDavid Lechner else
538d1efcf88SDavid Lechner iio_push_to_buffers_with_timestamp(indio_dev, &adc->sample.raw,
539d1efcf88SDavid Lechner pf->timestamp);
540d1efcf88SDavid Lechner
541d1efcf88SDavid Lechner out:
542d1efcf88SDavid Lechner iio_trigger_notify_done(indio_dev->trig);
543d1efcf88SDavid Lechner
544d1efcf88SDavid Lechner return IRQ_HANDLED;
545d1efcf88SDavid Lechner }
546d1efcf88SDavid Lechner
54764ce7d43SDavid Lechner /**
54864ce7d43SDavid Lechner * ad7944_chain_mode_alloc - allocate and initialize channel specs and buffers
54964ce7d43SDavid Lechner * for daisy-chained devices
55064ce7d43SDavid Lechner * @dev: The device for devm_ functions
55164ce7d43SDavid Lechner * @chan_template: The channel template for the devices (array of 2 channels
55264ce7d43SDavid Lechner * voltage and timestamp)
55364ce7d43SDavid Lechner * @n_chain_dev: The number of devices in the chain
55464ce7d43SDavid Lechner * @chain_chan: Pointer to receive the allocated channel specs
55564ce7d43SDavid Lechner * @chain_mode_buf: Pointer to receive the allocated rx buffer
55664ce7d43SDavid Lechner * @chain_scan_masks: Pointer to receive the allocated scan masks
55764ce7d43SDavid Lechner * Return: 0 on success, a negative error code on failure
55864ce7d43SDavid Lechner */
ad7944_chain_mode_alloc(struct device * dev,const struct iio_chan_spec * chan_template,u32 n_chain_dev,struct iio_chan_spec ** chain_chan,void ** chain_mode_buf,unsigned long ** chain_scan_masks)55964ce7d43SDavid Lechner static int ad7944_chain_mode_alloc(struct device *dev,
56064ce7d43SDavid Lechner const struct iio_chan_spec *chan_template,
56164ce7d43SDavid Lechner u32 n_chain_dev,
56264ce7d43SDavid Lechner struct iio_chan_spec **chain_chan,
56364ce7d43SDavid Lechner void **chain_mode_buf,
56464ce7d43SDavid Lechner unsigned long **chain_scan_masks)
56564ce7d43SDavid Lechner {
56664ce7d43SDavid Lechner struct iio_chan_spec *chan;
56764ce7d43SDavid Lechner size_t chain_mode_buf_size;
56864ce7d43SDavid Lechner unsigned long *scan_masks;
56964ce7d43SDavid Lechner void *buf;
57064ce7d43SDavid Lechner int i;
57164ce7d43SDavid Lechner
57264ce7d43SDavid Lechner /* 1 channel for each device in chain plus 1 for soft timestamp */
57364ce7d43SDavid Lechner
57464ce7d43SDavid Lechner chan = devm_kcalloc(dev, n_chain_dev + 1, sizeof(*chan), GFP_KERNEL);
57564ce7d43SDavid Lechner if (!chan)
57664ce7d43SDavid Lechner return -ENOMEM;
57764ce7d43SDavid Lechner
57864ce7d43SDavid Lechner for (i = 0; i < n_chain_dev; i++) {
57964ce7d43SDavid Lechner chan[i] = chan_template[0];
58064ce7d43SDavid Lechner
58164ce7d43SDavid Lechner if (chan_template[0].differential) {
58264ce7d43SDavid Lechner chan[i].channel = 2 * i;
58364ce7d43SDavid Lechner chan[i].channel2 = 2 * i + 1;
58464ce7d43SDavid Lechner } else {
58564ce7d43SDavid Lechner chan[i].channel = i;
58664ce7d43SDavid Lechner }
58764ce7d43SDavid Lechner
58864ce7d43SDavid Lechner chan[i].scan_index = i;
58964ce7d43SDavid Lechner }
59064ce7d43SDavid Lechner
59164ce7d43SDavid Lechner /* soft timestamp */
59264ce7d43SDavid Lechner chan[i] = chan_template[1];
59364ce7d43SDavid Lechner chan[i].scan_index = i;
59464ce7d43SDavid Lechner
59564ce7d43SDavid Lechner *chain_chan = chan;
59664ce7d43SDavid Lechner
59764ce7d43SDavid Lechner /* 1 word for each voltage channel + aligned u64 for timestamp */
59864ce7d43SDavid Lechner
59964ce7d43SDavid Lechner chain_mode_buf_size = ALIGN(n_chain_dev *
600503d20edSDavid Lechner AD7944_SPI_BYTES(chan[0].scan_type), sizeof(u64)) + sizeof(u64);
60164ce7d43SDavid Lechner buf = devm_kzalloc(dev, chain_mode_buf_size, GFP_KERNEL);
60264ce7d43SDavid Lechner if (!buf)
60364ce7d43SDavid Lechner return -ENOMEM;
60464ce7d43SDavid Lechner
60564ce7d43SDavid Lechner *chain_mode_buf = buf;
60664ce7d43SDavid Lechner
60764ce7d43SDavid Lechner /*
60864ce7d43SDavid Lechner * Have to limit n_chain_dev due to current implementation of
60964ce7d43SDavid Lechner * available_scan_masks.
61064ce7d43SDavid Lechner */
61164ce7d43SDavid Lechner if (n_chain_dev > BITS_PER_LONG)
61264ce7d43SDavid Lechner return dev_err_probe(dev, -EINVAL,
61364ce7d43SDavid Lechner "chain is limited to 32 devices\n");
61464ce7d43SDavid Lechner
61564ce7d43SDavid Lechner scan_masks = devm_kcalloc(dev, 2, sizeof(*scan_masks), GFP_KERNEL);
61664ce7d43SDavid Lechner if (!scan_masks)
61764ce7d43SDavid Lechner return -ENOMEM;
61864ce7d43SDavid Lechner
61964ce7d43SDavid Lechner /*
62064ce7d43SDavid Lechner * Scan mask is needed since we always have to read all devices in the
62164ce7d43SDavid Lechner * chain in one SPI transfer.
62264ce7d43SDavid Lechner */
62364ce7d43SDavid Lechner scan_masks[0] = GENMASK(n_chain_dev - 1, 0);
62464ce7d43SDavid Lechner
62564ce7d43SDavid Lechner *chain_scan_masks = scan_masks;
62664ce7d43SDavid Lechner
62764ce7d43SDavid Lechner return 0;
62864ce7d43SDavid Lechner }
62964ce7d43SDavid Lechner
630d1efcf88SDavid Lechner static const char * const ad7944_power_supplies[] = {
631d1efcf88SDavid Lechner "avdd", "dvdd", "bvdd", "vio"
632d1efcf88SDavid Lechner };
633d1efcf88SDavid Lechner
634cbc986cdSDavid Lechner static const struct spi_offload_config ad7944_offload_config = {
635cbc986cdSDavid Lechner .capability_flags = SPI_OFFLOAD_CAP_TRIGGER |
636cbc986cdSDavid Lechner SPI_OFFLOAD_CAP_RX_STREAM_DMA,
637cbc986cdSDavid Lechner };
638cbc986cdSDavid Lechner
ad7944_probe(struct spi_device * spi)639d1efcf88SDavid Lechner static int ad7944_probe(struct spi_device *spi)
640d1efcf88SDavid Lechner {
641d1efcf88SDavid Lechner const struct ad7944_chip_info *chip_info;
642d1efcf88SDavid Lechner struct device *dev = &spi->dev;
643d1efcf88SDavid Lechner struct iio_dev *indio_dev;
644d1efcf88SDavid Lechner struct ad7944_adc *adc;
645182b6164SDavid Lechner bool have_refin;
64664ce7d43SDavid Lechner struct iio_chan_spec *chain_chan;
64764ce7d43SDavid Lechner unsigned long *chain_scan_masks;
64864ce7d43SDavid Lechner u32 n_chain_dev;
649182b6164SDavid Lechner int ret, ref_mv;
650d1efcf88SDavid Lechner
651d1efcf88SDavid Lechner indio_dev = devm_iio_device_alloc(dev, sizeof(*adc));
652d1efcf88SDavid Lechner if (!indio_dev)
653d1efcf88SDavid Lechner return -ENOMEM;
654d1efcf88SDavid Lechner
655d1efcf88SDavid Lechner adc = iio_priv(indio_dev);
656d1efcf88SDavid Lechner adc->spi = spi;
657d1efcf88SDavid Lechner
658d1efcf88SDavid Lechner chip_info = spi_get_device_match_data(spi);
659d1efcf88SDavid Lechner if (!chip_info)
660d1efcf88SDavid Lechner return dev_err_probe(dev, -EINVAL, "no chip info\n");
661d1efcf88SDavid Lechner
662d1efcf88SDavid Lechner adc->timing_spec = chip_info->timing_spec;
663d1efcf88SDavid Lechner
664cbc986cdSDavid Lechner adc->sample_freq_range[0] = 1; /* min */
665cbc986cdSDavid Lechner adc->sample_freq_range[1] = 1; /* step */
666cbc986cdSDavid Lechner adc->sample_freq_range[2] = chip_info->max_sample_rate_hz; /* max */
667cbc986cdSDavid Lechner
66827eea477SDavid Lechner ret = device_property_match_property_string(dev, "adi,spi-mode",
66927eea477SDavid Lechner ad7944_spi_modes,
67027eea477SDavid Lechner ARRAY_SIZE(ad7944_spi_modes));
671346ae0e8SDavid Lechner /* absence of adi,spi-mode property means default mode */
67227eea477SDavid Lechner if (ret == -EINVAL)
673346ae0e8SDavid Lechner adc->spi_mode = AD7944_SPI_MODE_DEFAULT;
67427eea477SDavid Lechner else if (ret < 0)
67527eea477SDavid Lechner return dev_err_probe(dev, ret,
67627eea477SDavid Lechner "getting adi,spi-mode property failed\n");
67727eea477SDavid Lechner else
67827eea477SDavid Lechner adc->spi_mode = ret;
679346ae0e8SDavid Lechner
680d1efcf88SDavid Lechner /*
681d1efcf88SDavid Lechner * Some chips use unusual word sizes, so check now instead of waiting
682d1efcf88SDavid Lechner * for the first xfer.
683d1efcf88SDavid Lechner */
684d1efcf88SDavid Lechner if (!spi_is_bpw_supported(spi, chip_info->channels[0].scan_type.realbits))
685d1efcf88SDavid Lechner return dev_err_probe(dev, -EINVAL,
686d1efcf88SDavid Lechner "SPI host does not support %d bits per word\n",
687d1efcf88SDavid Lechner chip_info->channels[0].scan_type.realbits);
688d1efcf88SDavid Lechner
689d1efcf88SDavid Lechner ret = devm_regulator_bulk_get_enable(dev,
690d1efcf88SDavid Lechner ARRAY_SIZE(ad7944_power_supplies),
691d1efcf88SDavid Lechner ad7944_power_supplies);
692d1efcf88SDavid Lechner if (ret)
693d1efcf88SDavid Lechner return dev_err_probe(dev, ret,
694d1efcf88SDavid Lechner "failed to get and enable supplies\n");
695d1efcf88SDavid Lechner
696d1efcf88SDavid Lechner /*
697d1efcf88SDavid Lechner * Sort out what is being used for the reference voltage. Options are:
698d1efcf88SDavid Lechner * - internal reference: neither REF or REFIN is connected
699d1efcf88SDavid Lechner * - internal reference with external buffer: REF not connected, REFIN
700d1efcf88SDavid Lechner * is connected
701d1efcf88SDavid Lechner * - external reference: REF is connected, REFIN is not connected
702d1efcf88SDavid Lechner */
703d1efcf88SDavid Lechner
704182b6164SDavid Lechner ret = devm_regulator_get_enable_read_voltage(dev, "ref");
705182b6164SDavid Lechner if (ret < 0 && ret != -ENODEV)
706182b6164SDavid Lechner return dev_err_probe(dev, ret, "failed to get REF voltage\n");
707d1efcf88SDavid Lechner
708182b6164SDavid Lechner ref_mv = ret == -ENODEV ? 0 : ret / 1000;
709d1efcf88SDavid Lechner
710d1efcf88SDavid Lechner ret = devm_regulator_get_enable_optional(dev, "refin");
711182b6164SDavid Lechner if (ret < 0 && ret != -ENODEV)
712182b6164SDavid Lechner return dev_err_probe(dev, ret, "failed to get REFIN voltage\n");
713d1efcf88SDavid Lechner
714182b6164SDavid Lechner have_refin = ret != -ENODEV;
715182b6164SDavid Lechner
716182b6164SDavid Lechner if (have_refin && ref_mv)
717d1efcf88SDavid Lechner return dev_err_probe(dev, -EINVAL,
718d1efcf88SDavid Lechner "cannot have both refin and ref supplies\n");
719d1efcf88SDavid Lechner
720182b6164SDavid Lechner adc->ref_mv = ref_mv ?: AD7944_INTERNAL_REF_MV;
721d1efcf88SDavid Lechner
722346ae0e8SDavid Lechner adc->cnv = devm_gpiod_get_optional(dev, "cnv", GPIOD_OUT_LOW);
723d1efcf88SDavid Lechner if (IS_ERR(adc->cnv))
724d1efcf88SDavid Lechner return dev_err_probe(dev, PTR_ERR(adc->cnv),
725d1efcf88SDavid Lechner "failed to get CNV GPIO\n");
726d1efcf88SDavid Lechner
727346ae0e8SDavid Lechner if (!adc->cnv && adc->spi_mode == AD7944_SPI_MODE_DEFAULT)
728346ae0e8SDavid Lechner return dev_err_probe(&spi->dev, -EINVAL, "CNV GPIO is required\n");
729346ae0e8SDavid Lechner if (adc->cnv && adc->spi_mode != AD7944_SPI_MODE_DEFAULT)
730346ae0e8SDavid Lechner return dev_err_probe(&spi->dev, -EINVAL,
731346ae0e8SDavid Lechner "CNV GPIO in single and chain mode is not currently supported\n");
732346ae0e8SDavid Lechner
733d1efcf88SDavid Lechner adc->turbo = devm_gpiod_get_optional(dev, "turbo", GPIOD_OUT_LOW);
734d1efcf88SDavid Lechner if (IS_ERR(adc->turbo))
735d1efcf88SDavid Lechner return dev_err_probe(dev, PTR_ERR(adc->turbo),
736d1efcf88SDavid Lechner "failed to get TURBO GPIO\n");
737d1efcf88SDavid Lechner
738d1efcf88SDavid Lechner adc->always_turbo = device_property_present(dev, "adi,always-turbo");
739d1efcf88SDavid Lechner
740d1efcf88SDavid Lechner if (adc->turbo && adc->always_turbo)
741d1efcf88SDavid Lechner return dev_err_probe(dev, -EINVAL,
742d1efcf88SDavid Lechner "cannot have both turbo-gpios and adi,always-turbo\n");
743d1efcf88SDavid Lechner
744346ae0e8SDavid Lechner if (adc->spi_mode == AD7944_SPI_MODE_CHAIN && adc->always_turbo)
745346ae0e8SDavid Lechner return dev_err_probe(dev, -EINVAL,
746346ae0e8SDavid Lechner "cannot have both chain mode and always turbo\n");
747346ae0e8SDavid Lechner
7486020ca4dSDavid Lechner switch (adc->spi_mode) {
7496020ca4dSDavid Lechner case AD7944_SPI_MODE_DEFAULT:
7506020ca4dSDavid Lechner ret = ad7944_4wire_mode_init_msg(dev, adc, &chip_info->channels[0]);
7516020ca4dSDavid Lechner if (ret)
7526020ca4dSDavid Lechner return ret;
7536020ca4dSDavid Lechner
7546020ca4dSDavid Lechner break;
7556020ca4dSDavid Lechner case AD7944_SPI_MODE_SINGLE:
7566020ca4dSDavid Lechner ret = ad7944_3wire_cs_mode_init_msg(dev, adc, &chip_info->channels[0]);
7576020ca4dSDavid Lechner if (ret)
7586020ca4dSDavid Lechner return ret;
7596020ca4dSDavid Lechner
7606020ca4dSDavid Lechner break;
7616020ca4dSDavid Lechner case AD7944_SPI_MODE_CHAIN:
76264ce7d43SDavid Lechner ret = device_property_read_u32(dev, "#daisy-chained-devices",
76364ce7d43SDavid Lechner &n_chain_dev);
76464ce7d43SDavid Lechner if (ret)
76564ce7d43SDavid Lechner return dev_err_probe(dev, ret,
76664ce7d43SDavid Lechner "failed to get #daisy-chained-devices\n");
76764ce7d43SDavid Lechner
76864ce7d43SDavid Lechner ret = ad7944_chain_mode_alloc(dev, chip_info->channels,
76964ce7d43SDavid Lechner n_chain_dev, &chain_chan,
77064ce7d43SDavid Lechner &adc->chain_mode_buf,
77164ce7d43SDavid Lechner &chain_scan_masks);
77264ce7d43SDavid Lechner if (ret)
77364ce7d43SDavid Lechner return ret;
77464ce7d43SDavid Lechner
77564ce7d43SDavid Lechner ret = ad7944_chain_mode_init_msg(dev, adc, &chain_chan[0],
77664ce7d43SDavid Lechner n_chain_dev);
77764ce7d43SDavid Lechner if (ret)
77864ce7d43SDavid Lechner return ret;
77964ce7d43SDavid Lechner
78064ce7d43SDavid Lechner break;
7816020ca4dSDavid Lechner }
7826020ca4dSDavid Lechner
783d1efcf88SDavid Lechner indio_dev->name = chip_info->name;
784d1efcf88SDavid Lechner indio_dev->modes = INDIO_DIRECT_MODE;
785d1efcf88SDavid Lechner indio_dev->info = &ad7944_iio_info;
78664ce7d43SDavid Lechner
787cbc986cdSDavid Lechner adc->offload = devm_spi_offload_get(dev, spi, &ad7944_offload_config);
788cbc986cdSDavid Lechner ret = PTR_ERR_OR_ZERO(adc->offload);
789cbc986cdSDavid Lechner if (ret && ret != -ENODEV)
790cbc986cdSDavid Lechner return dev_err_probe(dev, ret, "failed to get offload\n");
791cbc986cdSDavid Lechner
792cbc986cdSDavid Lechner /* Fall back to low speed usage when no SPI offload available. */
793cbc986cdSDavid Lechner if (ret == -ENODEV) {
79464ce7d43SDavid Lechner if (adc->spi_mode == AD7944_SPI_MODE_CHAIN) {
79564ce7d43SDavid Lechner indio_dev->available_scan_masks = chain_scan_masks;
79664ce7d43SDavid Lechner indio_dev->channels = chain_chan;
79764ce7d43SDavid Lechner indio_dev->num_channels = n_chain_dev + 1;
79864ce7d43SDavid Lechner } else {
799d1efcf88SDavid Lechner indio_dev->channels = chip_info->channels;
800d1efcf88SDavid Lechner indio_dev->num_channels = ARRAY_SIZE(chip_info->channels);
80164ce7d43SDavid Lechner }
802d1efcf88SDavid Lechner
803d1efcf88SDavid Lechner ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
804d1efcf88SDavid Lechner iio_pollfunc_store_time,
805cbc986cdSDavid Lechner ad7944_trigger_handler,
806cbc986cdSDavid Lechner NULL);
807d1efcf88SDavid Lechner if (ret)
808d1efcf88SDavid Lechner return ret;
809cbc986cdSDavid Lechner } else {
810cbc986cdSDavid Lechner struct dma_chan *rx_dma;
811cbc986cdSDavid Lechner
812cbc986cdSDavid Lechner if (adc->spi_mode != AD7944_SPI_MODE_SINGLE)
813cbc986cdSDavid Lechner return dev_err_probe(dev, -EINVAL,
814cbc986cdSDavid Lechner "offload only supported in single mode\n");
815cbc986cdSDavid Lechner
816cbc986cdSDavid Lechner indio_dev->setup_ops = &ad7944_offload_buffer_setup_ops;
817cbc986cdSDavid Lechner indio_dev->channels = chip_info->offload_channels;
818cbc986cdSDavid Lechner indio_dev->num_channels = ARRAY_SIZE(chip_info->offload_channels);
819cbc986cdSDavid Lechner
820cbc986cdSDavid Lechner adc->offload_trigger = devm_spi_offload_trigger_get(dev,
821cbc986cdSDavid Lechner adc->offload, SPI_OFFLOAD_TRIGGER_PERIODIC);
822cbc986cdSDavid Lechner if (IS_ERR(adc->offload_trigger))
823cbc986cdSDavid Lechner return dev_err_probe(dev, PTR_ERR(adc->offload_trigger),
824cbc986cdSDavid Lechner "failed to get offload trigger\n");
825cbc986cdSDavid Lechner
826cbc986cdSDavid Lechner ret = ad7944_set_sample_freq(adc, 2 * MEGA);
827cbc986cdSDavid Lechner if (ret)
828cbc986cdSDavid Lechner return dev_err_probe(dev, ret,
829cbc986cdSDavid Lechner "failed to init sample rate\n");
830cbc986cdSDavid Lechner
831cbc986cdSDavid Lechner rx_dma = devm_spi_offload_rx_stream_request_dma_chan(dev,
832cbc986cdSDavid Lechner adc->offload);
833cbc986cdSDavid Lechner if (IS_ERR(rx_dma))
834cbc986cdSDavid Lechner return dev_err_probe(dev, PTR_ERR(rx_dma),
835cbc986cdSDavid Lechner "failed to get offload RX DMA\n");
836cbc986cdSDavid Lechner
837cbc986cdSDavid Lechner /*
838cbc986cdSDavid Lechner * REVISIT: ideally, we would confirm that the offload RX DMA
839cbc986cdSDavid Lechner * buffer layout is the same as what is hard-coded in
840cbc986cdSDavid Lechner * offload_channels. Right now, the only supported offload
841cbc986cdSDavid Lechner * is the pulsar_adc project which always uses 32-bit word
842cbc986cdSDavid Lechner * size for data values, regardless of the SPI bits per word.
843cbc986cdSDavid Lechner */
844cbc986cdSDavid Lechner
845cbc986cdSDavid Lechner ret = devm_iio_dmaengine_buffer_setup_with_handle(dev,
846cbc986cdSDavid Lechner indio_dev, rx_dma, IIO_BUFFER_DIRECTION_IN);
847cbc986cdSDavid Lechner if (ret)
848cbc986cdSDavid Lechner return ret;
849cbc986cdSDavid Lechner
850cbc986cdSDavid Lechner ret = ad7944_3wire_cs_mode_init_offload_msg(dev, adc,
851cbc986cdSDavid Lechner &chip_info->offload_channels[0]);
852cbc986cdSDavid Lechner if (ret)
853cbc986cdSDavid Lechner return ret;
854cbc986cdSDavid Lechner }
855d1efcf88SDavid Lechner
856d1efcf88SDavid Lechner return devm_iio_device_register(dev, indio_dev);
857d1efcf88SDavid Lechner }
858d1efcf88SDavid Lechner
859d1efcf88SDavid Lechner static const struct of_device_id ad7944_of_match[] = {
860d1efcf88SDavid Lechner { .compatible = "adi,ad7944", .data = &ad7944_chip_info },
861d1efcf88SDavid Lechner { .compatible = "adi,ad7985", .data = &ad7985_chip_info },
862d1efcf88SDavid Lechner { .compatible = "adi,ad7986", .data = &ad7986_chip_info },
863d1efcf88SDavid Lechner { }
864d1efcf88SDavid Lechner };
865d1efcf88SDavid Lechner MODULE_DEVICE_TABLE(of, ad7944_of_match);
866d1efcf88SDavid Lechner
867d1efcf88SDavid Lechner static const struct spi_device_id ad7944_spi_id[] = {
868d1efcf88SDavid Lechner { "ad7944", (kernel_ulong_t)&ad7944_chip_info },
869d1efcf88SDavid Lechner { "ad7985", (kernel_ulong_t)&ad7985_chip_info },
870d1efcf88SDavid Lechner { "ad7986", (kernel_ulong_t)&ad7986_chip_info },
871d1efcf88SDavid Lechner { }
872d1efcf88SDavid Lechner
873d1efcf88SDavid Lechner };
874d1efcf88SDavid Lechner MODULE_DEVICE_TABLE(spi, ad7944_spi_id);
875d1efcf88SDavid Lechner
876d1efcf88SDavid Lechner static struct spi_driver ad7944_driver = {
877d1efcf88SDavid Lechner .driver = {
878d1efcf88SDavid Lechner .name = "ad7944",
879d1efcf88SDavid Lechner .of_match_table = ad7944_of_match,
880d1efcf88SDavid Lechner },
881d1efcf88SDavid Lechner .probe = ad7944_probe,
882d1efcf88SDavid Lechner .id_table = ad7944_spi_id,
883d1efcf88SDavid Lechner };
884d1efcf88SDavid Lechner module_spi_driver(ad7944_driver);
885d1efcf88SDavid Lechner
886d1efcf88SDavid Lechner MODULE_AUTHOR("David Lechner <dlechner@baylibre.com>");
887d1efcf88SDavid Lechner MODULE_DESCRIPTION("Analog Devices AD7944 PulSAR ADC family driver");
888d1efcf88SDavid Lechner MODULE_LICENSE("GPL");
889cbc986cdSDavid Lechner MODULE_IMPORT_NS("IIO_DMAENGINE_BUFFER");
890