xref: /linux/drivers/iio/adc/ad7944.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
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>
19d1efcf88SDavid Lechner #include <linux/spi/spi.h>
20d1efcf88SDavid Lechner #include <linux/string_helpers.h>
21d1efcf88SDavid Lechner 
22d1efcf88SDavid Lechner #include <linux/iio/iio.h>
23d1efcf88SDavid Lechner #include <linux/iio/sysfs.h>
24d1efcf88SDavid Lechner #include <linux/iio/trigger_consumer.h>
25d1efcf88SDavid Lechner #include <linux/iio/triggered_buffer.h>
26d1efcf88SDavid Lechner 
27d1efcf88SDavid Lechner #define AD7944_INTERNAL_REF_MV		4096
28d1efcf88SDavid Lechner 
29d1efcf88SDavid Lechner struct ad7944_timing_spec {
30d1efcf88SDavid Lechner 	/* Normal mode max conversion time (t_{CONV}). */
31d1efcf88SDavid Lechner 	unsigned int conv_ns;
32d1efcf88SDavid Lechner 	/* TURBO mode max conversion time (t_{CONV}). */
33d1efcf88SDavid Lechner 	unsigned int turbo_conv_ns;
34d1efcf88SDavid Lechner };
35d1efcf88SDavid Lechner 
36346ae0e8SDavid Lechner enum ad7944_spi_mode {
37346ae0e8SDavid Lechner 	/* datasheet calls this "4-wire mode" */
38346ae0e8SDavid Lechner 	AD7944_SPI_MODE_DEFAULT,
39346ae0e8SDavid Lechner 	/* datasheet calls this "3-wire mode" (not related to SPI_3WIRE!) */
40346ae0e8SDavid Lechner 	AD7944_SPI_MODE_SINGLE,
41346ae0e8SDavid Lechner 	/* datasheet calls this "chain mode" */
42346ae0e8SDavid Lechner 	AD7944_SPI_MODE_CHAIN,
43346ae0e8SDavid Lechner };
44346ae0e8SDavid Lechner 
45346ae0e8SDavid Lechner /* maps adi,spi-mode property value to enum */
46346ae0e8SDavid Lechner static const char * const ad7944_spi_modes[] = {
47346ae0e8SDavid Lechner 	[AD7944_SPI_MODE_DEFAULT] = "",
48346ae0e8SDavid Lechner 	[AD7944_SPI_MODE_SINGLE] = "single",
49346ae0e8SDavid Lechner 	[AD7944_SPI_MODE_CHAIN] = "chain",
50346ae0e8SDavid Lechner };
51346ae0e8SDavid Lechner 
52d1efcf88SDavid Lechner struct ad7944_adc {
53d1efcf88SDavid Lechner 	struct spi_device *spi;
54346ae0e8SDavid Lechner 	enum ad7944_spi_mode spi_mode;
556020ca4dSDavid Lechner 	struct spi_transfer xfers[3];
566020ca4dSDavid Lechner 	struct spi_message msg;
5764ce7d43SDavid Lechner 	void *chain_mode_buf;
58d1efcf88SDavid Lechner 	/* Chip-specific timing specifications. */
59d1efcf88SDavid Lechner 	const struct ad7944_timing_spec *timing_spec;
60d1efcf88SDavid Lechner 	/* GPIO connected to CNV pin. */
61d1efcf88SDavid Lechner 	struct gpio_desc *cnv;
62d1efcf88SDavid Lechner 	/* Optional GPIO to enable turbo mode. */
63d1efcf88SDavid Lechner 	struct gpio_desc *turbo;
64d1efcf88SDavid Lechner 	/* Indicates TURBO is hard-wired to be always enabled. */
65d1efcf88SDavid Lechner 	bool always_turbo;
66d1efcf88SDavid Lechner 	/* Reference voltage (millivolts). */
67d1efcf88SDavid Lechner 	unsigned int ref_mv;
68d1efcf88SDavid Lechner 
69d1efcf88SDavid Lechner 	/*
70d1efcf88SDavid Lechner 	 * DMA (thus cache coherency maintenance) requires the
71d1efcf88SDavid Lechner 	 * transfer buffers to live in their own cache lines.
72d1efcf88SDavid Lechner 	 */
73d1efcf88SDavid Lechner 	struct {
74d1efcf88SDavid Lechner 		union {
75d1efcf88SDavid Lechner 			u16 u16;
76d1efcf88SDavid Lechner 			u32 u32;
77d1efcf88SDavid Lechner 		} raw;
78d1efcf88SDavid Lechner 		u64 timestamp __aligned(8);
79d1efcf88SDavid Lechner 	 } sample __aligned(IIO_DMA_MINALIGN);
80d1efcf88SDavid Lechner };
81d1efcf88SDavid Lechner 
82346ae0e8SDavid Lechner /* quite time before CNV rising edge */
83346ae0e8SDavid Lechner #define T_QUIET_NS	20
84346ae0e8SDavid Lechner 
85d1efcf88SDavid Lechner static const struct ad7944_timing_spec ad7944_timing_spec = {
86d1efcf88SDavid Lechner 	.conv_ns = 420,
87d1efcf88SDavid Lechner 	.turbo_conv_ns = 320,
88d1efcf88SDavid Lechner };
89d1efcf88SDavid Lechner 
90d1efcf88SDavid Lechner static const struct ad7944_timing_spec ad7986_timing_spec = {
91d1efcf88SDavid Lechner 	.conv_ns = 500,
92d1efcf88SDavid Lechner 	.turbo_conv_ns = 400,
93d1efcf88SDavid Lechner };
94d1efcf88SDavid Lechner 
95d1efcf88SDavid Lechner struct ad7944_chip_info {
96d1efcf88SDavid Lechner 	const char *name;
97d1efcf88SDavid Lechner 	const struct ad7944_timing_spec *timing_spec;
98d1efcf88SDavid Lechner 	const struct iio_chan_spec channels[2];
99d1efcf88SDavid Lechner };
100d1efcf88SDavid Lechner 
101d1efcf88SDavid Lechner /*
102d1efcf88SDavid Lechner  * AD7944_DEFINE_CHIP_INFO - Define a chip info structure for a specific chip
103d1efcf88SDavid Lechner  * @_name: The name of the chip
104d1efcf88SDavid Lechner  * @_ts: The timing specification for the chip
105d1efcf88SDavid Lechner  * @_bits: The number of bits in the conversion result
106d1efcf88SDavid Lechner  * @_diff: Whether the chip is true differential or not
107d1efcf88SDavid Lechner  */
108d1efcf88SDavid Lechner #define AD7944_DEFINE_CHIP_INFO(_name, _ts, _bits, _diff)		\
109d1efcf88SDavid Lechner static const struct ad7944_chip_info _name##_chip_info = {		\
110d1efcf88SDavid Lechner 	.name = #_name,							\
111d1efcf88SDavid Lechner 	.timing_spec = &_ts##_timing_spec,				\
112d1efcf88SDavid Lechner 	.channels = {							\
113d1efcf88SDavid Lechner 		{							\
114d1efcf88SDavid Lechner 			.type = IIO_VOLTAGE,				\
115d1efcf88SDavid Lechner 			.indexed = 1,					\
116d1efcf88SDavid Lechner 			.differential = _diff,				\
117d1efcf88SDavid Lechner 			.channel = 0,					\
118d1efcf88SDavid Lechner 			.channel2 = _diff ? 1 : 0,			\
119d1efcf88SDavid Lechner 			.scan_index = 0,				\
120d1efcf88SDavid Lechner 			.scan_type.sign = _diff ? 's' : 'u',		\
121d1efcf88SDavid Lechner 			.scan_type.realbits = _bits,			\
122d1efcf88SDavid Lechner 			.scan_type.storagebits = _bits > 16 ? 32 : 16,	\
123d1efcf88SDavid Lechner 			.scan_type.endianness = IIO_CPU,		\
124d1efcf88SDavid Lechner 			.info_mask_separate = BIT(IIO_CHAN_INFO_RAW)	\
125d1efcf88SDavid Lechner 					| BIT(IIO_CHAN_INFO_SCALE),	\
126d1efcf88SDavid Lechner 		},							\
127d1efcf88SDavid Lechner 		IIO_CHAN_SOFT_TIMESTAMP(1),				\
128d1efcf88SDavid Lechner 	},								\
129d1efcf88SDavid Lechner }
130d1efcf88SDavid Lechner 
131d1efcf88SDavid Lechner /* pseudo-differential with ground sense */
132d1efcf88SDavid Lechner AD7944_DEFINE_CHIP_INFO(ad7944, ad7944, 14, 0);
133d1efcf88SDavid Lechner AD7944_DEFINE_CHIP_INFO(ad7985, ad7944, 16, 0);
134d1efcf88SDavid Lechner /* fully differential */
135d1efcf88SDavid Lechner AD7944_DEFINE_CHIP_INFO(ad7986, ad7986, 18, 1);
136d1efcf88SDavid Lechner 
ad7944_3wire_cs_mode_init_msg(struct device * dev,struct ad7944_adc * adc,const struct iio_chan_spec * chan)1376020ca4dSDavid Lechner static int ad7944_3wire_cs_mode_init_msg(struct device *dev, struct ad7944_adc *adc,
1386020ca4dSDavid Lechner 					 const struct iio_chan_spec *chan)
1396020ca4dSDavid Lechner {
1406020ca4dSDavid Lechner 	unsigned int t_conv_ns = adc->always_turbo ? adc->timing_spec->turbo_conv_ns
1416020ca4dSDavid Lechner 						   : adc->timing_spec->conv_ns;
1426020ca4dSDavid Lechner 	struct spi_transfer *xfers = adc->xfers;
1436020ca4dSDavid Lechner 
1446020ca4dSDavid Lechner 	/*
1456020ca4dSDavid Lechner 	 * NB: can get better performance from some SPI controllers if we use
1466020ca4dSDavid Lechner 	 * the same bits_per_word in every transfer.
1476020ca4dSDavid Lechner 	 */
1486020ca4dSDavid Lechner 	xfers[0].bits_per_word = chan->scan_type.realbits;
1496020ca4dSDavid Lechner 	/*
1506020ca4dSDavid Lechner 	 * CS is tied to CNV and we need a low to high transition to start the
1516020ca4dSDavid Lechner 	 * conversion, so place CNV low for t_QUIET to prepare for this.
1526020ca4dSDavid Lechner 	 */
1536020ca4dSDavid Lechner 	xfers[0].delay.value = T_QUIET_NS;
1546020ca4dSDavid Lechner 	xfers[0].delay.unit = SPI_DELAY_UNIT_NSECS;
1556020ca4dSDavid Lechner 
1566020ca4dSDavid Lechner 	/*
1576020ca4dSDavid Lechner 	 * CS has to be high for full conversion time to avoid triggering the
1586020ca4dSDavid Lechner 	 * busy indication.
1596020ca4dSDavid Lechner 	 */
1606020ca4dSDavid Lechner 	xfers[1].cs_off = 1;
1616020ca4dSDavid Lechner 	xfers[1].delay.value = t_conv_ns;
1626020ca4dSDavid Lechner 	xfers[1].delay.unit = SPI_DELAY_UNIT_NSECS;
1636020ca4dSDavid Lechner 	xfers[1].bits_per_word = chan->scan_type.realbits;
1646020ca4dSDavid Lechner 
1656020ca4dSDavid Lechner 	/* Then we can read the data during the acquisition phase */
1666020ca4dSDavid Lechner 	xfers[2].rx_buf = &adc->sample.raw;
1676020ca4dSDavid Lechner 	xfers[2].len = BITS_TO_BYTES(chan->scan_type.storagebits);
1686020ca4dSDavid Lechner 	xfers[2].bits_per_word = chan->scan_type.realbits;
1696020ca4dSDavid Lechner 
1706020ca4dSDavid Lechner 	spi_message_init_with_transfers(&adc->msg, xfers, 3);
1716020ca4dSDavid Lechner 
172*340fa834SDavid Lechner 	return devm_spi_optimize_message(dev, adc->spi, &adc->msg);
1736020ca4dSDavid Lechner }
1746020ca4dSDavid Lechner 
ad7944_4wire_mode_init_msg(struct device * dev,struct ad7944_adc * adc,const struct iio_chan_spec * chan)1756020ca4dSDavid Lechner static int ad7944_4wire_mode_init_msg(struct device *dev, struct ad7944_adc *adc,
1766020ca4dSDavid Lechner 				      const struct iio_chan_spec *chan)
1776020ca4dSDavid Lechner {
1786020ca4dSDavid Lechner 	unsigned int t_conv_ns = adc->always_turbo ? adc->timing_spec->turbo_conv_ns
1796020ca4dSDavid Lechner 						   : adc->timing_spec->conv_ns;
1806020ca4dSDavid Lechner 	struct spi_transfer *xfers = adc->xfers;
1816020ca4dSDavid Lechner 
1826020ca4dSDavid Lechner 	/*
1836020ca4dSDavid Lechner 	 * NB: can get better performance from some SPI controllers if we use
1846020ca4dSDavid Lechner 	 * the same bits_per_word in every transfer.
1856020ca4dSDavid Lechner 	 */
1866020ca4dSDavid Lechner 	xfers[0].bits_per_word = chan->scan_type.realbits;
1876020ca4dSDavid Lechner 	/*
1886020ca4dSDavid Lechner 	 * CS has to be high for full conversion time to avoid triggering the
1896020ca4dSDavid Lechner 	 * busy indication.
1906020ca4dSDavid Lechner 	 */
1916020ca4dSDavid Lechner 	xfers[0].cs_off = 1;
1926020ca4dSDavid Lechner 	xfers[0].delay.value = t_conv_ns;
1936020ca4dSDavid Lechner 	xfers[0].delay.unit = SPI_DELAY_UNIT_NSECS;
1946020ca4dSDavid Lechner 
1956020ca4dSDavid Lechner 	xfers[1].rx_buf = &adc->sample.raw;
1966020ca4dSDavid Lechner 	xfers[1].len = BITS_TO_BYTES(chan->scan_type.storagebits);
1976020ca4dSDavid Lechner 	xfers[1].bits_per_word = chan->scan_type.realbits;
1986020ca4dSDavid Lechner 
1996020ca4dSDavid Lechner 	spi_message_init_with_transfers(&adc->msg, xfers, 2);
2006020ca4dSDavid Lechner 
201*340fa834SDavid Lechner 	return devm_spi_optimize_message(dev, adc->spi, &adc->msg);
2026020ca4dSDavid Lechner }
2036020ca4dSDavid Lechner 
ad7944_chain_mode_init_msg(struct device * dev,struct ad7944_adc * adc,const struct iio_chan_spec * chan,u32 n_chain_dev)20464ce7d43SDavid Lechner static int ad7944_chain_mode_init_msg(struct device *dev, struct ad7944_adc *adc,
20564ce7d43SDavid Lechner 				      const struct iio_chan_spec *chan,
20664ce7d43SDavid Lechner 				      u32 n_chain_dev)
20764ce7d43SDavid Lechner {
20864ce7d43SDavid Lechner 	struct spi_transfer *xfers = adc->xfers;
20964ce7d43SDavid Lechner 
21064ce7d43SDavid Lechner 	/*
21164ce7d43SDavid Lechner 	 * NB: SCLK has to be low before we toggle CS to avoid triggering the
21264ce7d43SDavid Lechner 	 * busy indication.
21364ce7d43SDavid Lechner 	 */
21464ce7d43SDavid Lechner 	if (adc->spi->mode & SPI_CPOL)
21564ce7d43SDavid Lechner 		return dev_err_probe(dev, -EINVAL,
21664ce7d43SDavid Lechner 				     "chain mode requires ~SPI_CPOL\n");
21764ce7d43SDavid Lechner 
21864ce7d43SDavid Lechner 	/*
21964ce7d43SDavid Lechner 	 * We only support CNV connected to CS in chain mode and we need CNV
22064ce7d43SDavid Lechner 	 * to be high during the transfer to trigger the conversion.
22164ce7d43SDavid Lechner 	 */
22264ce7d43SDavid Lechner 	if (!(adc->spi->mode & SPI_CS_HIGH))
22364ce7d43SDavid Lechner 		return dev_err_probe(dev, -EINVAL,
22464ce7d43SDavid Lechner 				     "chain mode requires SPI_CS_HIGH\n");
22564ce7d43SDavid Lechner 
22664ce7d43SDavid Lechner 	/* CNV has to be high for full conversion time before reading data. */
22764ce7d43SDavid Lechner 	xfers[0].delay.value = adc->timing_spec->conv_ns;
22864ce7d43SDavid Lechner 	xfers[0].delay.unit = SPI_DELAY_UNIT_NSECS;
22964ce7d43SDavid Lechner 
23064ce7d43SDavid Lechner 	xfers[1].rx_buf = adc->chain_mode_buf;
23164ce7d43SDavid Lechner 	xfers[1].len = BITS_TO_BYTES(chan->scan_type.storagebits) * n_chain_dev;
23264ce7d43SDavid Lechner 	xfers[1].bits_per_word = chan->scan_type.realbits;
23364ce7d43SDavid Lechner 
23464ce7d43SDavid Lechner 	spi_message_init_with_transfers(&adc->msg, xfers, 2);
23564ce7d43SDavid Lechner 
236*340fa834SDavid Lechner 	return devm_spi_optimize_message(dev, adc->spi, &adc->msg);
23764ce7d43SDavid Lechner }
23864ce7d43SDavid Lechner 
23961c8031aSDavid Lechner /**
24061c8031aSDavid Lechner  * ad7944_convert_and_acquire - Perform a single conversion and acquisition
241346ae0e8SDavid Lechner  * @adc: The ADC device structure
242346ae0e8SDavid Lechner  * Return: 0 on success, a negative error code on failure
243346ae0e8SDavid Lechner  *
24461c8031aSDavid Lechner  * Perform a conversion and acquisition of a single sample using the
24561c8031aSDavid Lechner  * pre-optimized adc->msg.
246346ae0e8SDavid Lechner  *
24764ce7d43SDavid Lechner  * Upon successful return adc->sample.raw will contain the conversion result
24864ce7d43SDavid Lechner  * (or adc->chain_mode_buf if the device is using chain mode).
249346ae0e8SDavid Lechner  */
ad7944_convert_and_acquire(struct ad7944_adc * adc)25033c33a96SDavid Lechner static int ad7944_convert_and_acquire(struct ad7944_adc *adc)
251d1efcf88SDavid Lechner {
252d1efcf88SDavid Lechner 	int ret;
253d1efcf88SDavid Lechner 
254d1efcf88SDavid Lechner 	/*
255d1efcf88SDavid Lechner 	 * In 4-wire mode, the CNV line is held high for the entire conversion
25661c8031aSDavid Lechner 	 * and acquisition process. In other modes adc->cnv is NULL and is
25761c8031aSDavid Lechner 	 * ignored (CS is wired to CNV in those cases).
258d1efcf88SDavid Lechner 	 */
259d1efcf88SDavid Lechner 	gpiod_set_value_cansleep(adc->cnv, 1);
2606020ca4dSDavid Lechner 	ret = spi_sync(adc->spi, &adc->msg);
261d1efcf88SDavid Lechner 	gpiod_set_value_cansleep(adc->cnv, 0);
262d1efcf88SDavid Lechner 
263d1efcf88SDavid Lechner 	return ret;
264d1efcf88SDavid Lechner }
265d1efcf88SDavid Lechner 
ad7944_single_conversion(struct ad7944_adc * adc,const struct iio_chan_spec * chan,int * val)266d1efcf88SDavid Lechner static int ad7944_single_conversion(struct ad7944_adc *adc,
267d1efcf88SDavid Lechner 				    const struct iio_chan_spec *chan,
268d1efcf88SDavid Lechner 				    int *val)
269d1efcf88SDavid Lechner {
270d1efcf88SDavid Lechner 	int ret;
271d1efcf88SDavid Lechner 
27233c33a96SDavid Lechner 	ret = ad7944_convert_and_acquire(adc);
273d1efcf88SDavid Lechner 	if (ret)
274d1efcf88SDavid Lechner 		return ret;
275d1efcf88SDavid Lechner 
27664ce7d43SDavid Lechner 	if (adc->spi_mode == AD7944_SPI_MODE_CHAIN) {
27764ce7d43SDavid Lechner 		if (chan->scan_type.storagebits > 16)
27864ce7d43SDavid Lechner 			*val = ((u32 *)adc->chain_mode_buf)[chan->scan_index];
27964ce7d43SDavid Lechner 		else
28064ce7d43SDavid Lechner 			*val = ((u16 *)adc->chain_mode_buf)[chan->scan_index];
28164ce7d43SDavid Lechner 	} else {
282d1efcf88SDavid Lechner 		if (chan->scan_type.storagebits > 16)
283d1efcf88SDavid Lechner 			*val = adc->sample.raw.u32;
284d1efcf88SDavid Lechner 		else
285d1efcf88SDavid Lechner 			*val = adc->sample.raw.u16;
28664ce7d43SDavid Lechner 	}
287d1efcf88SDavid Lechner 
288d1efcf88SDavid Lechner 	if (chan->scan_type.sign == 's')
289d1efcf88SDavid Lechner 		*val = sign_extend32(*val, chan->scan_type.realbits - 1);
290d1efcf88SDavid Lechner 
291d1efcf88SDavid Lechner 	return IIO_VAL_INT;
292d1efcf88SDavid Lechner }
293d1efcf88SDavid Lechner 
ad7944_read_raw(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,int * val,int * val2,long info)294d1efcf88SDavid Lechner static int ad7944_read_raw(struct iio_dev *indio_dev,
295d1efcf88SDavid Lechner 			   const struct iio_chan_spec *chan,
296d1efcf88SDavid Lechner 			   int *val, int *val2, long info)
297d1efcf88SDavid Lechner {
298d1efcf88SDavid Lechner 	struct ad7944_adc *adc = iio_priv(indio_dev);
299d1efcf88SDavid Lechner 	int ret;
300d1efcf88SDavid Lechner 
301d1efcf88SDavid Lechner 	switch (info) {
302d1efcf88SDavid Lechner 	case IIO_CHAN_INFO_RAW:
303d1efcf88SDavid Lechner 		ret = iio_device_claim_direct_mode(indio_dev);
304d1efcf88SDavid Lechner 		if (ret)
305d1efcf88SDavid Lechner 			return ret;
306d1efcf88SDavid Lechner 
307d1efcf88SDavid Lechner 		ret = ad7944_single_conversion(adc, chan, val);
308d1efcf88SDavid Lechner 		iio_device_release_direct_mode(indio_dev);
309d1efcf88SDavid Lechner 		return ret;
310d1efcf88SDavid Lechner 
311d1efcf88SDavid Lechner 	case IIO_CHAN_INFO_SCALE:
312d1efcf88SDavid Lechner 		switch (chan->type) {
313d1efcf88SDavid Lechner 		case IIO_VOLTAGE:
314d1efcf88SDavid Lechner 			*val = adc->ref_mv;
315d1efcf88SDavid Lechner 
316d1efcf88SDavid Lechner 			if (chan->scan_type.sign == 's')
317d1efcf88SDavid Lechner 				*val2 = chan->scan_type.realbits - 1;
318d1efcf88SDavid Lechner 			else
319d1efcf88SDavid Lechner 				*val2 = chan->scan_type.realbits;
320d1efcf88SDavid Lechner 
321d1efcf88SDavid Lechner 			return IIO_VAL_FRACTIONAL_LOG2;
322d1efcf88SDavid Lechner 		default:
323d1efcf88SDavid Lechner 			return -EINVAL;
324d1efcf88SDavid Lechner 		}
325d1efcf88SDavid Lechner 
326d1efcf88SDavid Lechner 	default:
327d1efcf88SDavid Lechner 		return -EINVAL;
328d1efcf88SDavid Lechner 	}
329d1efcf88SDavid Lechner }
330d1efcf88SDavid Lechner 
331d1efcf88SDavid Lechner static const struct iio_info ad7944_iio_info = {
332d1efcf88SDavid Lechner 	.read_raw = &ad7944_read_raw,
333d1efcf88SDavid Lechner };
334d1efcf88SDavid Lechner 
ad7944_trigger_handler(int irq,void * p)335d1efcf88SDavid Lechner static irqreturn_t ad7944_trigger_handler(int irq, void *p)
336d1efcf88SDavid Lechner {
337d1efcf88SDavid Lechner 	struct iio_poll_func *pf = p;
338d1efcf88SDavid Lechner 	struct iio_dev *indio_dev = pf->indio_dev;
339d1efcf88SDavid Lechner 	struct ad7944_adc *adc = iio_priv(indio_dev);
340d1efcf88SDavid Lechner 	int ret;
341d1efcf88SDavid Lechner 
34233c33a96SDavid Lechner 	ret = ad7944_convert_and_acquire(adc);
343d1efcf88SDavid Lechner 	if (ret)
344d1efcf88SDavid Lechner 		goto out;
345d1efcf88SDavid Lechner 
34664ce7d43SDavid Lechner 	if (adc->spi_mode == AD7944_SPI_MODE_CHAIN)
34764ce7d43SDavid Lechner 		iio_push_to_buffers_with_timestamp(indio_dev, adc->chain_mode_buf,
34864ce7d43SDavid Lechner 						   pf->timestamp);
34964ce7d43SDavid Lechner 	else
350d1efcf88SDavid Lechner 		iio_push_to_buffers_with_timestamp(indio_dev, &adc->sample.raw,
351d1efcf88SDavid Lechner 						   pf->timestamp);
352d1efcf88SDavid Lechner 
353d1efcf88SDavid Lechner out:
354d1efcf88SDavid Lechner 	iio_trigger_notify_done(indio_dev->trig);
355d1efcf88SDavid Lechner 
356d1efcf88SDavid Lechner 	return IRQ_HANDLED;
357d1efcf88SDavid Lechner }
358d1efcf88SDavid Lechner 
35964ce7d43SDavid Lechner /**
36064ce7d43SDavid Lechner  * ad7944_chain_mode_alloc - allocate and initialize channel specs and buffers
36164ce7d43SDavid Lechner  *                           for daisy-chained devices
36264ce7d43SDavid Lechner  * @dev: The device for devm_ functions
36364ce7d43SDavid Lechner  * @chan_template: The channel template for the devices (array of 2 channels
36464ce7d43SDavid Lechner  *                 voltage and timestamp)
36564ce7d43SDavid Lechner  * @n_chain_dev: The number of devices in the chain
36664ce7d43SDavid Lechner  * @chain_chan: Pointer to receive the allocated channel specs
36764ce7d43SDavid Lechner  * @chain_mode_buf: Pointer to receive the allocated rx buffer
36864ce7d43SDavid Lechner  * @chain_scan_masks: Pointer to receive the allocated scan masks
36964ce7d43SDavid Lechner  * Return: 0 on success, a negative error code on failure
37064ce7d43SDavid 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)37164ce7d43SDavid Lechner static int ad7944_chain_mode_alloc(struct device *dev,
37264ce7d43SDavid Lechner 				   const struct iio_chan_spec *chan_template,
37364ce7d43SDavid Lechner 				   u32 n_chain_dev,
37464ce7d43SDavid Lechner 				   struct iio_chan_spec **chain_chan,
37564ce7d43SDavid Lechner 				   void **chain_mode_buf,
37664ce7d43SDavid Lechner 				   unsigned long **chain_scan_masks)
37764ce7d43SDavid Lechner {
37864ce7d43SDavid Lechner 	struct iio_chan_spec *chan;
37964ce7d43SDavid Lechner 	size_t chain_mode_buf_size;
38064ce7d43SDavid Lechner 	unsigned long *scan_masks;
38164ce7d43SDavid Lechner 	void *buf;
38264ce7d43SDavid Lechner 	int i;
38364ce7d43SDavid Lechner 
38464ce7d43SDavid Lechner 	/* 1 channel for each device in chain plus 1 for soft timestamp */
38564ce7d43SDavid Lechner 
38664ce7d43SDavid Lechner 	chan = devm_kcalloc(dev, n_chain_dev + 1, sizeof(*chan), GFP_KERNEL);
38764ce7d43SDavid Lechner 	if (!chan)
38864ce7d43SDavid Lechner 		return -ENOMEM;
38964ce7d43SDavid Lechner 
39064ce7d43SDavid Lechner 	for (i = 0; i < n_chain_dev; i++) {
39164ce7d43SDavid Lechner 		chan[i] = chan_template[0];
39264ce7d43SDavid Lechner 
39364ce7d43SDavid Lechner 		if (chan_template[0].differential) {
39464ce7d43SDavid Lechner 			chan[i].channel = 2 * i;
39564ce7d43SDavid Lechner 			chan[i].channel2 = 2 * i + 1;
39664ce7d43SDavid Lechner 		} else {
39764ce7d43SDavid Lechner 			chan[i].channel = i;
39864ce7d43SDavid Lechner 		}
39964ce7d43SDavid Lechner 
40064ce7d43SDavid Lechner 		chan[i].scan_index = i;
40164ce7d43SDavid Lechner 	}
40264ce7d43SDavid Lechner 
40364ce7d43SDavid Lechner 	/* soft timestamp */
40464ce7d43SDavid Lechner 	chan[i] = chan_template[1];
40564ce7d43SDavid Lechner 	chan[i].scan_index = i;
40664ce7d43SDavid Lechner 
40764ce7d43SDavid Lechner 	*chain_chan = chan;
40864ce7d43SDavid Lechner 
40964ce7d43SDavid Lechner 	/* 1 word for each voltage channel + aligned u64 for timestamp */
41064ce7d43SDavid Lechner 
41164ce7d43SDavid Lechner 	chain_mode_buf_size = ALIGN(n_chain_dev *
41264ce7d43SDavid Lechner 		BITS_TO_BYTES(chan[0].scan_type.storagebits), sizeof(u64))
41364ce7d43SDavid Lechner 		+ sizeof(u64);
41464ce7d43SDavid Lechner 	buf = devm_kzalloc(dev, chain_mode_buf_size, GFP_KERNEL);
41564ce7d43SDavid Lechner 	if (!buf)
41664ce7d43SDavid Lechner 		return -ENOMEM;
41764ce7d43SDavid Lechner 
41864ce7d43SDavid Lechner 	*chain_mode_buf = buf;
41964ce7d43SDavid Lechner 
42064ce7d43SDavid Lechner 	/*
42164ce7d43SDavid Lechner 	 * Have to limit n_chain_dev due to current implementation of
42264ce7d43SDavid Lechner 	 * available_scan_masks.
42364ce7d43SDavid Lechner 	 */
42464ce7d43SDavid Lechner 	if (n_chain_dev > BITS_PER_LONG)
42564ce7d43SDavid Lechner 		return dev_err_probe(dev, -EINVAL,
42664ce7d43SDavid Lechner 				     "chain is limited to 32 devices\n");
42764ce7d43SDavid Lechner 
42864ce7d43SDavid Lechner 	scan_masks = devm_kcalloc(dev, 2, sizeof(*scan_masks), GFP_KERNEL);
42964ce7d43SDavid Lechner 	if (!scan_masks)
43064ce7d43SDavid Lechner 		return -ENOMEM;
43164ce7d43SDavid Lechner 
43264ce7d43SDavid Lechner 	/*
43364ce7d43SDavid Lechner 	 * Scan mask is needed since we always have to read all devices in the
43464ce7d43SDavid Lechner 	 * chain in one SPI transfer.
43564ce7d43SDavid Lechner 	 */
43664ce7d43SDavid Lechner 	scan_masks[0] = GENMASK(n_chain_dev - 1, 0);
43764ce7d43SDavid Lechner 
43864ce7d43SDavid Lechner 	*chain_scan_masks = scan_masks;
43964ce7d43SDavid Lechner 
44064ce7d43SDavid Lechner 	return 0;
44164ce7d43SDavid Lechner }
44264ce7d43SDavid Lechner 
443d1efcf88SDavid Lechner static const char * const ad7944_power_supplies[] = {
444d1efcf88SDavid Lechner 	"avdd",	"dvdd",	"bvdd", "vio"
445d1efcf88SDavid Lechner };
446d1efcf88SDavid Lechner 
ad7944_probe(struct spi_device * spi)447d1efcf88SDavid Lechner static int ad7944_probe(struct spi_device *spi)
448d1efcf88SDavid Lechner {
449d1efcf88SDavid Lechner 	const struct ad7944_chip_info *chip_info;
450d1efcf88SDavid Lechner 	struct device *dev = &spi->dev;
451d1efcf88SDavid Lechner 	struct iio_dev *indio_dev;
452d1efcf88SDavid Lechner 	struct ad7944_adc *adc;
453182b6164SDavid Lechner 	bool have_refin;
45464ce7d43SDavid Lechner 	struct iio_chan_spec *chain_chan;
45564ce7d43SDavid Lechner 	unsigned long *chain_scan_masks;
45664ce7d43SDavid Lechner 	u32 n_chain_dev;
457182b6164SDavid Lechner 	int ret, ref_mv;
458d1efcf88SDavid Lechner 
459d1efcf88SDavid Lechner 	indio_dev = devm_iio_device_alloc(dev, sizeof(*adc));
460d1efcf88SDavid Lechner 	if (!indio_dev)
461d1efcf88SDavid Lechner 		return -ENOMEM;
462d1efcf88SDavid Lechner 
463d1efcf88SDavid Lechner 	adc = iio_priv(indio_dev);
464d1efcf88SDavid Lechner 	adc->spi = spi;
465d1efcf88SDavid Lechner 
466d1efcf88SDavid Lechner 	chip_info = spi_get_device_match_data(spi);
467d1efcf88SDavid Lechner 	if (!chip_info)
468d1efcf88SDavid Lechner 		return dev_err_probe(dev, -EINVAL, "no chip info\n");
469d1efcf88SDavid Lechner 
470d1efcf88SDavid Lechner 	adc->timing_spec = chip_info->timing_spec;
471d1efcf88SDavid Lechner 
47227eea477SDavid Lechner 	ret = device_property_match_property_string(dev, "adi,spi-mode",
47327eea477SDavid Lechner 						    ad7944_spi_modes,
47427eea477SDavid Lechner 						    ARRAY_SIZE(ad7944_spi_modes));
475346ae0e8SDavid Lechner 	/* absence of adi,spi-mode property means default mode */
47627eea477SDavid Lechner 	if (ret == -EINVAL)
477346ae0e8SDavid Lechner 		adc->spi_mode = AD7944_SPI_MODE_DEFAULT;
47827eea477SDavid Lechner 	else if (ret < 0)
47927eea477SDavid Lechner 		return dev_err_probe(dev, ret,
48027eea477SDavid Lechner 				     "getting adi,spi-mode property failed\n");
48127eea477SDavid Lechner 	else
48227eea477SDavid Lechner 		adc->spi_mode = ret;
483346ae0e8SDavid Lechner 
484d1efcf88SDavid Lechner 	/*
485d1efcf88SDavid Lechner 	 * Some chips use unusual word sizes, so check now instead of waiting
486d1efcf88SDavid Lechner 	 * for the first xfer.
487d1efcf88SDavid Lechner 	 */
488d1efcf88SDavid Lechner 	if (!spi_is_bpw_supported(spi, chip_info->channels[0].scan_type.realbits))
489d1efcf88SDavid Lechner 		return dev_err_probe(dev, -EINVAL,
490d1efcf88SDavid Lechner 				"SPI host does not support %d bits per word\n",
491d1efcf88SDavid Lechner 				chip_info->channels[0].scan_type.realbits);
492d1efcf88SDavid Lechner 
493d1efcf88SDavid Lechner 	ret = devm_regulator_bulk_get_enable(dev,
494d1efcf88SDavid Lechner 					     ARRAY_SIZE(ad7944_power_supplies),
495d1efcf88SDavid Lechner 					     ad7944_power_supplies);
496d1efcf88SDavid Lechner 	if (ret)
497d1efcf88SDavid Lechner 		return dev_err_probe(dev, ret,
498d1efcf88SDavid Lechner 				     "failed to get and enable supplies\n");
499d1efcf88SDavid Lechner 
500d1efcf88SDavid Lechner 	/*
501d1efcf88SDavid Lechner 	 * Sort out what is being used for the reference voltage. Options are:
502d1efcf88SDavid Lechner 	 * - internal reference: neither REF or REFIN is connected
503d1efcf88SDavid Lechner 	 * - internal reference with external buffer: REF not connected, REFIN
504d1efcf88SDavid Lechner 	 *   is connected
505d1efcf88SDavid Lechner 	 * - external reference: REF is connected, REFIN is not connected
506d1efcf88SDavid Lechner 	 */
507d1efcf88SDavid Lechner 
508182b6164SDavid Lechner 	ret = devm_regulator_get_enable_read_voltage(dev, "ref");
509182b6164SDavid Lechner 	if (ret < 0 && ret != -ENODEV)
510182b6164SDavid Lechner 		return dev_err_probe(dev, ret, "failed to get REF voltage\n");
511d1efcf88SDavid Lechner 
512182b6164SDavid Lechner 	ref_mv = ret == -ENODEV ? 0 : ret / 1000;
513d1efcf88SDavid Lechner 
514d1efcf88SDavid Lechner 	ret = devm_regulator_get_enable_optional(dev, "refin");
515182b6164SDavid Lechner 	if (ret < 0 && ret != -ENODEV)
516182b6164SDavid Lechner 		return dev_err_probe(dev, ret, "failed to get REFIN voltage\n");
517d1efcf88SDavid Lechner 
518182b6164SDavid Lechner 	have_refin = ret != -ENODEV;
519182b6164SDavid Lechner 
520182b6164SDavid Lechner 	if (have_refin && ref_mv)
521d1efcf88SDavid Lechner 		return dev_err_probe(dev, -EINVAL,
522d1efcf88SDavid Lechner 				     "cannot have both refin and ref supplies\n");
523d1efcf88SDavid Lechner 
524182b6164SDavid Lechner 	adc->ref_mv = ref_mv ?: AD7944_INTERNAL_REF_MV;
525d1efcf88SDavid Lechner 
526346ae0e8SDavid Lechner 	adc->cnv = devm_gpiod_get_optional(dev, "cnv", GPIOD_OUT_LOW);
527d1efcf88SDavid Lechner 	if (IS_ERR(adc->cnv))
528d1efcf88SDavid Lechner 		return dev_err_probe(dev, PTR_ERR(adc->cnv),
529d1efcf88SDavid Lechner 				     "failed to get CNV GPIO\n");
530d1efcf88SDavid Lechner 
531346ae0e8SDavid Lechner 	if (!adc->cnv && adc->spi_mode == AD7944_SPI_MODE_DEFAULT)
532346ae0e8SDavid Lechner 		return dev_err_probe(&spi->dev, -EINVAL, "CNV GPIO is required\n");
533346ae0e8SDavid Lechner 	if (adc->cnv && adc->spi_mode != AD7944_SPI_MODE_DEFAULT)
534346ae0e8SDavid Lechner 		return dev_err_probe(&spi->dev, -EINVAL,
535346ae0e8SDavid Lechner 				     "CNV GPIO in single and chain mode is not currently supported\n");
536346ae0e8SDavid Lechner 
537d1efcf88SDavid Lechner 	adc->turbo = devm_gpiod_get_optional(dev, "turbo", GPIOD_OUT_LOW);
538d1efcf88SDavid Lechner 	if (IS_ERR(adc->turbo))
539d1efcf88SDavid Lechner 		return dev_err_probe(dev, PTR_ERR(adc->turbo),
540d1efcf88SDavid Lechner 				     "failed to get TURBO GPIO\n");
541d1efcf88SDavid Lechner 
542d1efcf88SDavid Lechner 	adc->always_turbo = device_property_present(dev, "adi,always-turbo");
543d1efcf88SDavid Lechner 
544d1efcf88SDavid Lechner 	if (adc->turbo && adc->always_turbo)
545d1efcf88SDavid Lechner 		return dev_err_probe(dev, -EINVAL,
546d1efcf88SDavid Lechner 			"cannot have both turbo-gpios and adi,always-turbo\n");
547d1efcf88SDavid Lechner 
548346ae0e8SDavid Lechner 	if (adc->spi_mode == AD7944_SPI_MODE_CHAIN && adc->always_turbo)
549346ae0e8SDavid Lechner 		return dev_err_probe(dev, -EINVAL,
550346ae0e8SDavid Lechner 			"cannot have both chain mode and always turbo\n");
551346ae0e8SDavid Lechner 
5526020ca4dSDavid Lechner 	switch (adc->spi_mode) {
5536020ca4dSDavid Lechner 	case AD7944_SPI_MODE_DEFAULT:
5546020ca4dSDavid Lechner 		ret = ad7944_4wire_mode_init_msg(dev, adc, &chip_info->channels[0]);
5556020ca4dSDavid Lechner 		if (ret)
5566020ca4dSDavid Lechner 			return ret;
5576020ca4dSDavid Lechner 
5586020ca4dSDavid Lechner 		break;
5596020ca4dSDavid Lechner 	case AD7944_SPI_MODE_SINGLE:
5606020ca4dSDavid Lechner 		ret = ad7944_3wire_cs_mode_init_msg(dev, adc, &chip_info->channels[0]);
5616020ca4dSDavid Lechner 		if (ret)
5626020ca4dSDavid Lechner 			return ret;
5636020ca4dSDavid Lechner 
5646020ca4dSDavid Lechner 		break;
5656020ca4dSDavid Lechner 	case AD7944_SPI_MODE_CHAIN:
56664ce7d43SDavid Lechner 		ret = device_property_read_u32(dev, "#daisy-chained-devices",
56764ce7d43SDavid Lechner 					       &n_chain_dev);
56864ce7d43SDavid Lechner 		if (ret)
56964ce7d43SDavid Lechner 			return dev_err_probe(dev, ret,
57064ce7d43SDavid Lechner 					"failed to get #daisy-chained-devices\n");
57164ce7d43SDavid Lechner 
57264ce7d43SDavid Lechner 		ret = ad7944_chain_mode_alloc(dev, chip_info->channels,
57364ce7d43SDavid Lechner 					      n_chain_dev, &chain_chan,
57464ce7d43SDavid Lechner 					      &adc->chain_mode_buf,
57564ce7d43SDavid Lechner 					      &chain_scan_masks);
57664ce7d43SDavid Lechner 		if (ret)
57764ce7d43SDavid Lechner 			return ret;
57864ce7d43SDavid Lechner 
57964ce7d43SDavid Lechner 		ret = ad7944_chain_mode_init_msg(dev, adc, &chain_chan[0],
58064ce7d43SDavid Lechner 						 n_chain_dev);
58164ce7d43SDavid Lechner 		if (ret)
58264ce7d43SDavid Lechner 			return ret;
58364ce7d43SDavid Lechner 
58464ce7d43SDavid Lechner 		break;
5856020ca4dSDavid Lechner 	}
5866020ca4dSDavid Lechner 
587d1efcf88SDavid Lechner 	indio_dev->name = chip_info->name;
588d1efcf88SDavid Lechner 	indio_dev->modes = INDIO_DIRECT_MODE;
589d1efcf88SDavid Lechner 	indio_dev->info = &ad7944_iio_info;
59064ce7d43SDavid Lechner 
59164ce7d43SDavid Lechner 	if (adc->spi_mode == AD7944_SPI_MODE_CHAIN) {
59264ce7d43SDavid Lechner 		indio_dev->available_scan_masks = chain_scan_masks;
59364ce7d43SDavid Lechner 		indio_dev->channels = chain_chan;
59464ce7d43SDavid Lechner 		indio_dev->num_channels = n_chain_dev + 1;
59564ce7d43SDavid Lechner 	} else {
596d1efcf88SDavid Lechner 		indio_dev->channels = chip_info->channels;
597d1efcf88SDavid Lechner 		indio_dev->num_channels = ARRAY_SIZE(chip_info->channels);
59864ce7d43SDavid Lechner 	}
599d1efcf88SDavid Lechner 
600d1efcf88SDavid Lechner 	ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
601d1efcf88SDavid Lechner 					      iio_pollfunc_store_time,
602d1efcf88SDavid Lechner 					      ad7944_trigger_handler, NULL);
603d1efcf88SDavid Lechner 	if (ret)
604d1efcf88SDavid Lechner 		return ret;
605d1efcf88SDavid Lechner 
606d1efcf88SDavid Lechner 	return devm_iio_device_register(dev, indio_dev);
607d1efcf88SDavid Lechner }
608d1efcf88SDavid Lechner 
609d1efcf88SDavid Lechner static const struct of_device_id ad7944_of_match[] = {
610d1efcf88SDavid Lechner 	{ .compatible = "adi,ad7944", .data = &ad7944_chip_info },
611d1efcf88SDavid Lechner 	{ .compatible = "adi,ad7985", .data = &ad7985_chip_info },
612d1efcf88SDavid Lechner 	{ .compatible = "adi,ad7986", .data = &ad7986_chip_info },
613d1efcf88SDavid Lechner 	{ }
614d1efcf88SDavid Lechner };
615d1efcf88SDavid Lechner MODULE_DEVICE_TABLE(of, ad7944_of_match);
616d1efcf88SDavid Lechner 
617d1efcf88SDavid Lechner static const struct spi_device_id ad7944_spi_id[] = {
618d1efcf88SDavid Lechner 	{ "ad7944", (kernel_ulong_t)&ad7944_chip_info },
619d1efcf88SDavid Lechner 	{ "ad7985", (kernel_ulong_t)&ad7985_chip_info },
620d1efcf88SDavid Lechner 	{ "ad7986", (kernel_ulong_t)&ad7986_chip_info },
621d1efcf88SDavid Lechner 	{ }
622d1efcf88SDavid Lechner 
623d1efcf88SDavid Lechner };
624d1efcf88SDavid Lechner MODULE_DEVICE_TABLE(spi, ad7944_spi_id);
625d1efcf88SDavid Lechner 
626d1efcf88SDavid Lechner static struct spi_driver ad7944_driver = {
627d1efcf88SDavid Lechner 	.driver = {
628d1efcf88SDavid Lechner 		.name = "ad7944",
629d1efcf88SDavid Lechner 		.of_match_table = ad7944_of_match,
630d1efcf88SDavid Lechner 	},
631d1efcf88SDavid Lechner 	.probe = ad7944_probe,
632d1efcf88SDavid Lechner 	.id_table = ad7944_spi_id,
633d1efcf88SDavid Lechner };
634d1efcf88SDavid Lechner module_spi_driver(ad7944_driver);
635d1efcf88SDavid Lechner 
636d1efcf88SDavid Lechner MODULE_AUTHOR("David Lechner <dlechner@baylibre.com>");
637d1efcf88SDavid Lechner MODULE_DESCRIPTION("Analog Devices AD7944 PulSAR ADC family driver");
638d1efcf88SDavid Lechner MODULE_LICENSE("GPL");
639