1 // SPDX-License-Identifier: GPL-2.0
2 /* ad7949.c - Analog Devices ADC driver 14/16 bits 4/8 channels
4 * Copyright (C) 2018 CMC NV
6 * https://www.analog.com/media/en/technical-documentation/data-sheets/AD7949.pdf
9 #include <linux/delay.h>
10 #include <linux/iio/iio.h>
11 #include <linux/module.h>
12 #include <linux/regulator/consumer.h>
13 #include <linux/spi/spi.h>
14 #include <linux/bitfield.h>
16 #define AD7949_CFG_MASK_TOTAL GENMASK(13, 0)
18 /* CFG: Configuration Update */
19 #define AD7949_CFG_MASK_OVERWRITE BIT(13)
21 /* INCC: Input Channel Configuration */
22 #define AD7949_CFG_MASK_INCC GENMASK(12, 10)
23 #define AD7949_CFG_VAL_INCC_UNIPOLAR_GND 7
24 #define AD7949_CFG_VAL_INCC_UNIPOLAR_COMM 6
25 #define AD7949_CFG_VAL_INCC_UNIPOLAR_DIFF 4
26 #define AD7949_CFG_VAL_INCC_TEMP 3
27 #define AD7949_CFG_VAL_INCC_BIPOLAR 2
28 #define AD7949_CFG_VAL_INCC_BIPOLAR_DIFF 0
30 /* INX: Input channel Selection in a binary fashion */
31 #define AD7949_CFG_MASK_INX GENMASK(9, 7)
33 /* BW: select bandwidth for low-pass filter. Full or Quarter */
34 #define AD7949_CFG_MASK_BW_FULL BIT(6)
36 /* REF: reference/buffer selection */
37 #define AD7949_CFG_MASK_REF GENMASK(5, 3)
38 #define AD7949_CFG_VAL_REF_EXT_TEMP_BUF 3
39 #define AD7949_CFG_VAL_REF_EXT_TEMP 2
40 #define AD7949_CFG_VAL_REF_INT_4096 1
41 #define AD7949_CFG_VAL_REF_INT_2500 0
42 #define AD7949_CFG_VAL_REF_EXTERNAL BIT(1)
44 /* SEQ: channel sequencer. Allows for scanning channels */
45 #define AD7949_CFG_MASK_SEQ GENMASK(2, 1)
47 /* RB: Read back the CFG register */
48 #define AD7949_CFG_MASK_RBN BIT(0)
56 struct ad7949_adc_spec {
61 static const struct ad7949_adc_spec ad7949_adc_spec[] = {
62 [ID_AD7949] = { .num_channels = 8, .resolution = 14 },
63 [ID_AD7682] = { .num_channels = 4, .resolution = 16 },
64 [ID_AD7689] = { .num_channels = 8, .resolution = 16 },
68 * struct ad7949_adc_chip - AD ADC chip
69 * @lock: protects write sequences
70 * @vref: regulator generating Vref
71 * @indio_dev: reference to iio structure
72 * @spi: reference to spi structure
73 * @refsel: reference selection
74 * @resolution: resolution of the chip
75 * @cfg: copy of the configuration register
76 * @current_channel: current channel in use
77 * @buffer: buffer to send / receive data to / from device
78 * @buf8b: be16 buffer to exchange data with the device in 8-bit transfers
80 struct ad7949_adc_chip {
82 struct regulator *vref;
83 struct iio_dev *indio_dev;
84 struct spi_device *spi;
88 unsigned int current_channel;
89 u16 buffer __aligned(IIO_DMA_MINALIGN);
93 static int ad7949_spi_write_cfg(struct ad7949_adc_chip *ad7949_adc, u16 val,
98 ad7949_adc->cfg = (val & mask) | (ad7949_adc->cfg & ~mask);
100 switch (ad7949_adc->spi->bits_per_word) {
102 ad7949_adc->buffer = ad7949_adc->cfg << 2;
103 ret = spi_write(ad7949_adc->spi, &ad7949_adc->buffer, 2);
106 ad7949_adc->buffer = ad7949_adc->cfg;
107 ret = spi_write(ad7949_adc->spi, &ad7949_adc->buffer, 2);
110 /* Here, type is big endian as it must be sent in two transfers */
111 ad7949_adc->buf8b = cpu_to_be16(ad7949_adc->cfg << 2);
112 ret = spi_write(ad7949_adc->spi, &ad7949_adc->buf8b, 2);
115 dev_err(&ad7949_adc->indio_dev->dev, "unsupported BPW\n");
120 * This delay is to avoid a new request before the required time to
121 * send a new command to the device
127 static int ad7949_spi_read_channel(struct ad7949_adc_chip *ad7949_adc, int *val,
128 unsigned int channel)
134 * 1: write CFG for sample N and read old data (sample N-2)
135 * 2: if CFG was not changed since sample N-1 then we'll get good data
136 * at the next xfer, so we bail out now, otherwise we write something
137 * and we read garbage (sample N-1 configuration).
139 for (i = 0; i < 2; i++) {
140 ret = ad7949_spi_write_cfg(ad7949_adc,
141 FIELD_PREP(AD7949_CFG_MASK_INX, channel),
142 AD7949_CFG_MASK_INX);
145 if (channel == ad7949_adc->current_channel)
149 /* 3: write something and read actual data */
150 if (ad7949_adc->spi->bits_per_word == 8)
151 ret = spi_read(ad7949_adc->spi, &ad7949_adc->buf8b, 2);
153 ret = spi_read(ad7949_adc->spi, &ad7949_adc->buffer, 2);
159 * This delay is to avoid a new request before the required time to
160 * send a new command to the device
164 ad7949_adc->current_channel = channel;
166 switch (ad7949_adc->spi->bits_per_word) {
168 *val = ad7949_adc->buffer;
169 /* Shift-out padding bits */
170 *val >>= 16 - ad7949_adc->resolution;
173 *val = ad7949_adc->buffer & GENMASK(13, 0);
176 /* Here, type is big endian as data was sent in two transfers */
177 *val = be16_to_cpu(ad7949_adc->buf8b);
178 /* Shift-out padding bits */
179 *val >>= 16 - ad7949_adc->resolution;
182 dev_err(&ad7949_adc->indio_dev->dev, "unsupported BPW\n");
189 #define AD7949_ADC_CHANNEL(chan) { \
190 .type = IIO_VOLTAGE, \
193 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
194 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
197 static const struct iio_chan_spec ad7949_adc_channels[] = {
198 AD7949_ADC_CHANNEL(0),
199 AD7949_ADC_CHANNEL(1),
200 AD7949_ADC_CHANNEL(2),
201 AD7949_ADC_CHANNEL(3),
202 AD7949_ADC_CHANNEL(4),
203 AD7949_ADC_CHANNEL(5),
204 AD7949_ADC_CHANNEL(6),
205 AD7949_ADC_CHANNEL(7),
208 static int ad7949_spi_read_raw(struct iio_dev *indio_dev,
209 struct iio_chan_spec const *chan,
210 int *val, int *val2, long mask)
212 struct ad7949_adc_chip *ad7949_adc = iio_priv(indio_dev);
219 case IIO_CHAN_INFO_RAW:
220 mutex_lock(&ad7949_adc->lock);
221 ret = ad7949_spi_read_channel(ad7949_adc, val, chan->channel);
222 mutex_unlock(&ad7949_adc->lock);
229 case IIO_CHAN_INFO_SCALE:
230 switch (ad7949_adc->refsel) {
231 case AD7949_CFG_VAL_REF_INT_2500:
234 case AD7949_CFG_VAL_REF_INT_4096:
237 case AD7949_CFG_VAL_REF_EXT_TEMP:
238 case AD7949_CFG_VAL_REF_EXT_TEMP_BUF:
239 ret = regulator_get_voltage(ad7949_adc->vref);
243 /* convert value back to mV */
248 *val2 = (1 << ad7949_adc->resolution) - 1;
249 return IIO_VAL_FRACTIONAL;
255 static int ad7949_spi_reg_access(struct iio_dev *indio_dev,
256 unsigned int reg, unsigned int writeval,
257 unsigned int *readval)
259 struct ad7949_adc_chip *ad7949_adc = iio_priv(indio_dev);
263 *readval = ad7949_adc->cfg;
265 ret = ad7949_spi_write_cfg(ad7949_adc, writeval,
266 AD7949_CFG_MASK_TOTAL);
271 static const struct iio_info ad7949_spi_info = {
272 .read_raw = ad7949_spi_read_raw,
273 .debugfs_reg_access = ad7949_spi_reg_access,
276 static int ad7949_spi_init(struct ad7949_adc_chip *ad7949_adc)
282 ad7949_adc->current_channel = 0;
284 cfg = FIELD_PREP(AD7949_CFG_MASK_OVERWRITE, 1) |
285 FIELD_PREP(AD7949_CFG_MASK_INCC, AD7949_CFG_VAL_INCC_UNIPOLAR_GND) |
286 FIELD_PREP(AD7949_CFG_MASK_INX, ad7949_adc->current_channel) |
287 FIELD_PREP(AD7949_CFG_MASK_BW_FULL, 1) |
288 FIELD_PREP(AD7949_CFG_MASK_REF, ad7949_adc->refsel) |
289 FIELD_PREP(AD7949_CFG_MASK_SEQ, 0x0) |
290 FIELD_PREP(AD7949_CFG_MASK_RBN, 1);
292 ret = ad7949_spi_write_cfg(ad7949_adc, cfg, AD7949_CFG_MASK_TOTAL);
295 * Do two dummy conversions to apply the first configuration setting.
296 * Required only after the start up of the device.
298 ad7949_spi_read_channel(ad7949_adc, &val, ad7949_adc->current_channel);
299 ad7949_spi_read_channel(ad7949_adc, &val, ad7949_adc->current_channel);
304 static void ad7949_disable_reg(void *reg)
306 regulator_disable(reg);
309 static int ad7949_spi_probe(struct spi_device *spi)
311 u32 spi_ctrl_mask = spi->controller->bits_per_word_mask;
312 struct device *dev = &spi->dev;
313 const struct ad7949_adc_spec *spec;
314 struct ad7949_adc_chip *ad7949_adc;
315 struct iio_dev *indio_dev;
319 indio_dev = devm_iio_device_alloc(dev, sizeof(*ad7949_adc));
321 dev_err(dev, "can not allocate iio device\n");
325 indio_dev->info = &ad7949_spi_info;
326 indio_dev->name = spi_get_device_id(spi)->name;
327 indio_dev->modes = INDIO_DIRECT_MODE;
328 indio_dev->channels = ad7949_adc_channels;
329 spi_set_drvdata(spi, indio_dev);
331 ad7949_adc = iio_priv(indio_dev);
332 ad7949_adc->indio_dev = indio_dev;
333 ad7949_adc->spi = spi;
335 spec = &ad7949_adc_spec[spi_get_device_id(spi)->driver_data];
336 indio_dev->num_channels = spec->num_channels;
337 ad7949_adc->resolution = spec->resolution;
339 /* Set SPI bits per word */
340 if (spi_ctrl_mask & SPI_BPW_MASK(ad7949_adc->resolution)) {
341 spi->bits_per_word = ad7949_adc->resolution;
342 } else if (spi_ctrl_mask == SPI_BPW_MASK(16)) {
343 spi->bits_per_word = 16;
344 } else if (spi_ctrl_mask == SPI_BPW_MASK(8)) {
345 spi->bits_per_word = 8;
347 dev_err(dev, "unable to find common BPW with spi controller\n");
351 /* Setup internal voltage reference */
353 device_property_read_u32(dev, "adi,internal-ref-microvolt", &tmp);
357 ad7949_adc->refsel = AD7949_CFG_VAL_REF_INT_2500;
360 ad7949_adc->refsel = AD7949_CFG_VAL_REF_INT_4096;
363 dev_err(dev, "unsupported internal voltage reference\n");
367 /* Setup external voltage reference, buffered? */
368 ad7949_adc->vref = devm_regulator_get_optional(dev, "vrefin");
369 if (IS_ERR(ad7949_adc->vref)) {
370 ret = PTR_ERR(ad7949_adc->vref);
374 ad7949_adc->vref = devm_regulator_get_optional(dev, "vref");
375 if (IS_ERR(ad7949_adc->vref)) {
376 ret = PTR_ERR(ad7949_adc->vref);
380 ad7949_adc->refsel = AD7949_CFG_VAL_REF_EXT_TEMP;
383 ad7949_adc->refsel = AD7949_CFG_VAL_REF_EXT_TEMP_BUF;
386 if (ad7949_adc->refsel & AD7949_CFG_VAL_REF_EXTERNAL) {
387 ret = regulator_enable(ad7949_adc->vref);
389 dev_err(dev, "fail to enable regulator\n");
393 ret = devm_add_action_or_reset(dev, ad7949_disable_reg,
399 mutex_init(&ad7949_adc->lock);
401 ret = ad7949_spi_init(ad7949_adc);
403 dev_err(dev, "fail to init this device: %d\n", ret);
407 ret = devm_iio_device_register(dev, indio_dev);
409 dev_err(dev, "fail to register iio device: %d\n", ret);
414 static const struct of_device_id ad7949_spi_of_id[] = {
415 { .compatible = "adi,ad7949" },
416 { .compatible = "adi,ad7682" },
417 { .compatible = "adi,ad7689" },
420 MODULE_DEVICE_TABLE(of, ad7949_spi_of_id);
422 static const struct spi_device_id ad7949_spi_id[] = {
423 { "ad7949", ID_AD7949 },
424 { "ad7682", ID_AD7682 },
425 { "ad7689", ID_AD7689 },
428 MODULE_DEVICE_TABLE(spi, ad7949_spi_id);
430 static struct spi_driver ad7949_spi_driver = {
433 .of_match_table = ad7949_spi_of_id,
435 .probe = ad7949_spi_probe,
436 .id_table = ad7949_spi_id,
438 module_spi_driver(ad7949_spi_driver);
441 MODULE_DESCRIPTION("Analog Devices 14/16-bit 8-channel ADC driver");
442 MODULE_LICENSE("GPL v2");