1 // SPDX-License-Identifier: GPL-2.0
3 * Analog Devices AD7292 SPI ADC driver
5 * Copyright 2019 Analog Devices Inc.
8 #include <linux/bitfield.h>
9 #include <linux/device.h>
10 #include <linux/module.h>
12 #include <linux/regulator/consumer.h>
13 #include <linux/spi/spi.h>
15 #include <linux/iio/iio.h>
17 #define ADI_VENDOR_ID 0x0018
19 /* AD7292 registers definition */
20 #define AD7292_REG_VENDOR_ID 0x00
21 #define AD7292_REG_CONF_BANK 0x05
22 #define AD7292_REG_CONV_COMM 0x0E
23 #define AD7292_REG_ADC_CH(x) (0x10 + (x))
25 /* AD7292 configuration bank subregisters definition */
26 #define AD7292_BANK_REG_VIN_RNG0 0x10
27 #define AD7292_BANK_REG_VIN_RNG1 0x11
28 #define AD7292_BANK_REG_SAMP_MODE 0x12
30 #define AD7292_RD_FLAG_MSK(x) (BIT(7) | ((x) & 0x3F))
32 /* AD7292_REG_ADC_CONVERSION */
33 #define AD7292_ADC_DATA_MASK GENMASK(15, 6)
34 #define AD7292_ADC_DATA(x) FIELD_GET(AD7292_ADC_DATA_MASK, x)
36 /* AD7292_CHANNEL_SAMPLING_MODE */
37 #define AD7292_CH_SAMP_MODE(reg, ch) (((reg) >> 8) & BIT(ch))
39 /* AD7292_CHANNEL_VIN_RANGE */
40 #define AD7292_CH_VIN_RANGE(reg, ch) ((reg) & BIT(ch))
42 #define AD7292_VOLTAGE_CHAN(_chan) \
44 .type = IIO_VOLTAGE, \
45 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
46 BIT(IIO_CHAN_INFO_SCALE), \
51 static const struct iio_chan_spec ad7292_channels[] = {
52 AD7292_VOLTAGE_CHAN(0),
53 AD7292_VOLTAGE_CHAN(1),
54 AD7292_VOLTAGE_CHAN(2),
55 AD7292_VOLTAGE_CHAN(3),
56 AD7292_VOLTAGE_CHAN(4),
57 AD7292_VOLTAGE_CHAN(5),
58 AD7292_VOLTAGE_CHAN(6),
59 AD7292_VOLTAGE_CHAN(7)
62 static const struct iio_chan_spec ad7292_channels_diff[] = {
65 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
71 AD7292_VOLTAGE_CHAN(2),
72 AD7292_VOLTAGE_CHAN(3),
73 AD7292_VOLTAGE_CHAN(4),
74 AD7292_VOLTAGE_CHAN(5),
75 AD7292_VOLTAGE_CHAN(6),
76 AD7292_VOLTAGE_CHAN(7)
80 struct spi_device *spi;
81 struct regulator *reg;
82 unsigned short vref_mv;
84 __be16 d16 __aligned(IIO_DMA_MINALIGN);
88 static int ad7292_spi_reg_read(struct ad7292_state *st, unsigned int addr)
92 st->d8[0] = AD7292_RD_FLAG_MSK(addr);
94 ret = spi_write_then_read(st->spi, st->d8, 1, &st->d16, 2);
98 return be16_to_cpu(st->d16);
101 static int ad7292_spi_subreg_read(struct ad7292_state *st, unsigned int addr,
102 unsigned int sub_addr, unsigned int len)
104 unsigned int shift = 16 - (8 * len);
107 st->d8[0] = AD7292_RD_FLAG_MSK(addr);
108 st->d8[1] = sub_addr;
110 ret = spi_write_then_read(st->spi, st->d8, 2, &st->d16, len);
114 return (be16_to_cpu(st->d16) >> shift);
117 static int ad7292_single_conversion(struct ad7292_state *st,
118 unsigned int chan_addr)
122 struct spi_transfer t[] = {
128 .unit = SPI_DELAY_UNIT_USECS
136 st->d8[0] = chan_addr;
137 st->d8[1] = AD7292_RD_FLAG_MSK(AD7292_REG_CONV_COMM);
139 ret = spi_sync_transfer(st->spi, t, ARRAY_SIZE(t));
144 return be16_to_cpu(st->d16);
147 static int ad7292_vin_range_multiplier(struct ad7292_state *st, int channel)
149 int samp_mode, range0, range1, factor = 1;
152 * Every AD7292 ADC channel may have its input range adjusted according
153 * to the settings at the ADC sampling mode and VIN range subregisters.
154 * For a given channel, the minimum input range is equal to Vref, and it
155 * may be increased by a multiplier factor of 2 or 4 according to the
157 * If channel is being sampled with respect to AGND:
158 * factor = 4 if VIN range0 and VIN range1 equal 0
159 * factor = 2 if only one of VIN ranges equal 1
160 * factor = 1 if both VIN range0 and VIN range1 equal 1
161 * If channel is being sampled with respect to AVDD:
162 * factor = 4 if VIN range0 and VIN range1 equal 0
163 * Behavior is undefined if any of VIN range doesn't equal 0
166 samp_mode = ad7292_spi_subreg_read(st, AD7292_REG_CONF_BANK,
167 AD7292_BANK_REG_SAMP_MODE, 2);
172 range0 = ad7292_spi_subreg_read(st, AD7292_REG_CONF_BANK,
173 AD7292_BANK_REG_VIN_RNG0, 2);
178 range1 = ad7292_spi_subreg_read(st, AD7292_REG_CONF_BANK,
179 AD7292_BANK_REG_VIN_RNG1, 2);
184 if (AD7292_CH_SAMP_MODE(samp_mode, channel)) {
185 /* Sampling with respect to AGND */
186 if (!AD7292_CH_VIN_RANGE(range0, channel))
189 if (!AD7292_CH_VIN_RANGE(range1, channel))
193 /* Sampling with respect to AVDD */
194 if (AD7292_CH_VIN_RANGE(range0, channel) ||
195 AD7292_CH_VIN_RANGE(range1, channel))
204 static int ad7292_read_raw(struct iio_dev *indio_dev,
205 const struct iio_chan_spec *chan,
206 int *val, int *val2, long info)
208 struct ad7292_state *st = iio_priv(indio_dev);
209 unsigned int ch_addr;
213 case IIO_CHAN_INFO_RAW:
214 ch_addr = AD7292_REG_ADC_CH(chan->channel);
215 ret = ad7292_single_conversion(st, ch_addr);
219 *val = AD7292_ADC_DATA(ret);
222 case IIO_CHAN_INFO_SCALE:
224 * To convert a raw value to standard units, the IIO defines
225 * this formula: Scaled value = (raw + offset) * scale.
226 * For the scale to be a correct multiplier for (raw + offset),
227 * it must be calculated as the input range divided by the
228 * number of possible distinct input values. Given the ADC data
229 * is 10 bit long, it may assume 2^10 distinct values.
230 * Hence, scale = range / 2^10. The IIO_VAL_FRACTIONAL_LOG2
231 * return type indicates to the IIO API to divide *val by 2 to
232 * the power of *val2 when returning from read_raw.
235 ret = ad7292_vin_range_multiplier(st, chan->channel);
239 *val = st->vref_mv * ret;
241 return IIO_VAL_FRACTIONAL_LOG2;
248 static const struct iio_info ad7292_info = {
249 .read_raw = ad7292_read_raw,
252 static void ad7292_regulator_disable(void *data)
254 struct ad7292_state *st = data;
256 regulator_disable(st->reg);
259 static int ad7292_probe(struct spi_device *spi)
261 struct ad7292_state *st;
262 struct iio_dev *indio_dev;
263 struct device_node *child;
264 bool diff_channels = false;
267 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
271 st = iio_priv(indio_dev);
274 ret = ad7292_spi_reg_read(st, AD7292_REG_VENDOR_ID);
275 if (ret != ADI_VENDOR_ID) {
276 dev_err(&spi->dev, "Wrong vendor id 0x%x\n", ret);
280 st->reg = devm_regulator_get_optional(&spi->dev, "vref");
281 if (!IS_ERR(st->reg)) {
282 ret = regulator_enable(st->reg);
285 "Failed to enable external vref supply\n");
289 ret = devm_add_action_or_reset(&spi->dev,
290 ad7292_regulator_disable, st);
294 ret = regulator_get_voltage(st->reg);
298 st->vref_mv = ret / 1000;
300 /* Use the internal voltage reference. */
304 indio_dev->name = spi_get_device_id(spi)->name;
305 indio_dev->modes = INDIO_DIRECT_MODE;
306 indio_dev->info = &ad7292_info;
308 for_each_available_child_of_node(spi->dev.of_node, child) {
309 diff_channels = of_property_read_bool(child, "diff-channels");
317 indio_dev->num_channels = ARRAY_SIZE(ad7292_channels_diff);
318 indio_dev->channels = ad7292_channels_diff;
320 indio_dev->num_channels = ARRAY_SIZE(ad7292_channels);
321 indio_dev->channels = ad7292_channels;
324 return devm_iio_device_register(&spi->dev, indio_dev);
327 static const struct spi_device_id ad7292_id_table[] = {
331 MODULE_DEVICE_TABLE(spi, ad7292_id_table);
333 static const struct of_device_id ad7292_of_match[] = {
334 { .compatible = "adi,ad7292" },
337 MODULE_DEVICE_TABLE(of, ad7292_of_match);
339 static struct spi_driver ad7292_driver = {
342 .of_match_table = ad7292_of_match,
344 .probe = ad7292_probe,
345 .id_table = ad7292_id_table,
347 module_spi_driver(ad7292_driver);
350 MODULE_DESCRIPTION("Analog Devices AD7292 ADC driver");
351 MODULE_LICENSE("GPL v2");