1 // SPDX-License-Identifier: GPL-2.0
3 * Analog Devices AD7466/7/8 AD7476/5/7/8 (A) SPI ADC driver
4 * TI ADC081S/ADC101S/ADC121S 8/10/12-bit SPI ADC driver
6 * Copyright 2010 Analog Devices Inc.
9 #include <linux/device.h>
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/sysfs.h>
13 #include <linux/spi/spi.h>
14 #include <linux/regulator/consumer.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/err.h>
17 #include <linux/module.h>
18 #include <linux/bitops.h>
19 #include <linux/delay.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 #include <linux/iio/buffer.h>
24 #include <linux/iio/trigger_consumer.h>
25 #include <linux/iio/triggered_buffer.h>
29 struct ad7476_chip_info {
30 unsigned int int_vref_uv;
31 struct iio_chan_spec channel[2];
32 /* channels used when convst gpio is defined */
33 struct iio_chan_spec convst_channel[2];
34 void (*reset)(struct ad7476_state *);
40 struct spi_device *spi;
41 const struct ad7476_chip_info *chip_info;
42 struct regulator *ref_reg;
43 struct gpio_desc *convst_gpio;
44 struct spi_transfer xfer;
45 struct spi_message msg;
47 * DMA (thus cache coherency maintenance) may require the
48 * transfer buffers to live in their own cache lines.
49 * Make the buffer large enough for one 16 bit sample and one 64 bit
50 * aligned 64 bit timestamp.
52 unsigned char data[ALIGN(2, sizeof(s64)) + sizeof(s64)] __aligned(IIO_DMA_MINALIGN);
55 enum ad7476_supported_device_ids {
78 static void ad7091_convst(struct ad7476_state *st)
83 gpiod_set_value(st->convst_gpio, 0);
84 udelay(1); /* CONVST pulse width: 10 ns min */
85 gpiod_set_value(st->convst_gpio, 1);
86 udelay(1); /* Conversion time: 650 ns max */
89 static irqreturn_t ad7476_trigger_handler(int irq, void *p)
91 struct iio_poll_func *pf = p;
92 struct iio_dev *indio_dev = pf->indio_dev;
93 struct ad7476_state *st = iio_priv(indio_dev);
98 b_sent = spi_sync(st->spi, &st->msg);
102 iio_push_to_buffers_with_timestamp(indio_dev, st->data,
103 iio_get_time_ns(indio_dev));
105 iio_trigger_notify_done(indio_dev->trig);
110 static void ad7091_reset(struct ad7476_state *st)
112 /* Any transfers with 8 scl cycles will reset the device */
113 spi_read(st->spi, st->data, 1);
116 static int ad7476_scan_direct(struct ad7476_state *st)
122 ret = spi_sync(st->spi, &st->msg);
126 return be16_to_cpup((__be16 *)st->data);
129 static int ad7476_read_raw(struct iio_dev *indio_dev,
130 struct iio_chan_spec const *chan,
136 struct ad7476_state *st = iio_priv(indio_dev);
140 case IIO_CHAN_INFO_RAW:
141 ret = iio_device_claim_direct_mode(indio_dev);
144 ret = ad7476_scan_direct(st);
145 iio_device_release_direct_mode(indio_dev);
149 *val = (ret >> st->chip_info->channel[0].scan_type.shift) &
150 GENMASK(st->chip_info->channel[0].scan_type.realbits - 1, 0);
152 case IIO_CHAN_INFO_SCALE:
154 scale_uv = regulator_get_voltage(st->ref_reg);
158 scale_uv = st->chip_info->int_vref_uv;
160 *val = scale_uv / 1000;
161 *val2 = chan->scan_type.realbits;
162 return IIO_VAL_FRACTIONAL_LOG2;
167 #define _AD7476_CHAN(bits, _shift, _info_mask_sep) \
169 .type = IIO_VOLTAGE, \
171 .info_mask_separate = _info_mask_sep, \
172 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
175 .realbits = (bits), \
178 .endianness = IIO_BE, \
182 #define ADC081S_CHAN(bits) _AD7476_CHAN((bits), 12 - (bits), \
183 BIT(IIO_CHAN_INFO_RAW))
184 #define AD7476_CHAN(bits) _AD7476_CHAN((bits), 13 - (bits), \
185 BIT(IIO_CHAN_INFO_RAW))
186 #define AD7940_CHAN(bits) _AD7476_CHAN((bits), 15 - (bits), \
187 BIT(IIO_CHAN_INFO_RAW))
188 #define AD7091R_CHAN(bits) _AD7476_CHAN((bits), 16 - (bits), 0)
189 #define AD7091R_CONVST_CHAN(bits) _AD7476_CHAN((bits), 16 - (bits), \
190 BIT(IIO_CHAN_INFO_RAW))
191 #define ADS786X_CHAN(bits) _AD7476_CHAN((bits), 12 - (bits), \
192 BIT(IIO_CHAN_INFO_RAW))
194 static const struct ad7476_chip_info ad7476_chip_info_tbl[] = {
196 .channel[0] = AD7091R_CHAN(12),
197 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
198 .convst_channel[0] = AD7091R_CONVST_CHAN(12),
199 .convst_channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
200 .reset = ad7091_reset,
203 .channel[0] = AD7091R_CHAN(12),
204 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
205 .convst_channel[0] = AD7091R_CONVST_CHAN(12),
206 .convst_channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
207 .int_vref_uv = 2500000,
209 .reset = ad7091_reset,
212 .channel[0] = AD7940_CHAN(10),
213 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
217 .channel[0] = AD7940_CHAN(12),
218 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
222 .channel[0] = AD7940_CHAN(12),
223 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
226 .channel[0] = AD7940_CHAN(10),
227 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
230 .channel[0] = AD7940_CHAN(8),
231 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
234 .channel[0] = AD7476_CHAN(12),
235 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
238 .channel[0] = AD7476_CHAN(10),
239 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
242 .channel[0] = AD7476_CHAN(8),
243 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
246 .channel[0] = AD7476_CHAN(12),
247 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
252 .channel[0] = AD7476_CHAN(12),
253 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
254 .int_vref_uv = 2500000,
258 .channel[0] = AD7940_CHAN(14),
259 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
262 .channel[0] = ADC081S_CHAN(8),
263 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
266 .channel[0] = ADC081S_CHAN(10),
267 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
270 .channel[0] = ADC081S_CHAN(12),
271 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
274 .channel[0] = ADS786X_CHAN(12),
275 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
278 .channel[0] = ADS786X_CHAN(10),
279 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
282 .channel[0] = ADS786X_CHAN(8),
283 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
286 .channel[0] = AD7940_CHAN(14),
287 .channel[1] = IIO_CHAN_SOFT_TIMESTAMP(1),
292 static const struct iio_info ad7476_info = {
293 .read_raw = &ad7476_read_raw,
296 static void ad7476_reg_disable(void *data)
298 struct regulator *reg = data;
300 regulator_disable(reg);
303 static int ad7476_probe(struct spi_device *spi)
305 struct ad7476_state *st;
306 struct iio_dev *indio_dev;
307 struct regulator *reg;
310 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
314 st = iio_priv(indio_dev);
316 &ad7476_chip_info_tbl[spi_get_device_id(spi)->driver_data];
318 reg = devm_regulator_get(&spi->dev, "vcc");
322 ret = regulator_enable(reg);
326 ret = devm_add_action_or_reset(&spi->dev, ad7476_reg_disable, reg);
330 /* Either vcc or vref (below) as appropriate */
331 if (!st->chip_info->int_vref_uv)
334 if (st->chip_info->has_vref) {
336 /* If a device has an internal reference vref is optional */
337 if (st->chip_info->int_vref_uv) {
338 reg = devm_regulator_get_optional(&spi->dev, "vref");
339 if (IS_ERR(reg) && (PTR_ERR(reg) != -ENODEV))
342 reg = devm_regulator_get(&spi->dev, "vref");
348 ret = regulator_enable(reg);
352 ret = devm_add_action_or_reset(&spi->dev,
360 * Can only get here if device supports both internal
361 * and external reference, but the regulator connected
362 * to the external reference is not connected.
363 * Set the reference regulator pointer to NULL to
370 if (st->chip_info->has_vdrive) {
371 ret = devm_regulator_get_enable(&spi->dev, "vdrive");
376 st->convst_gpio = devm_gpiod_get_optional(&spi->dev,
377 "adi,conversion-start",
379 if (IS_ERR(st->convst_gpio))
380 return PTR_ERR(st->convst_gpio);
384 indio_dev->name = spi_get_device_id(spi)->name;
385 indio_dev->modes = INDIO_DIRECT_MODE;
386 indio_dev->channels = st->chip_info->channel;
387 indio_dev->num_channels = 2;
388 indio_dev->info = &ad7476_info;
391 indio_dev->channels = st->chip_info->convst_channel;
392 /* Setup default message */
394 st->xfer.rx_buf = &st->data;
395 st->xfer.len = st->chip_info->channel[0].scan_type.storagebits / 8;
397 spi_message_init(&st->msg);
398 spi_message_add_tail(&st->xfer, &st->msg);
400 ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev, NULL,
401 &ad7476_trigger_handler, NULL);
405 if (st->chip_info->reset)
406 st->chip_info->reset(st);
408 return devm_iio_device_register(&spi->dev, indio_dev);
411 static const struct spi_device_id ad7476_id[] = {
412 { "ad7091", ID_AD7091 },
413 { "ad7091r", ID_AD7091R },
414 { "ad7273", ID_AD7273 },
415 { "ad7274", ID_AD7274 },
416 { "ad7276", ID_AD7276},
417 { "ad7277", ID_AD7277 },
418 { "ad7278", ID_AD7278 },
419 { "ad7466", ID_AD7466 },
420 { "ad7467", ID_AD7467 },
421 { "ad7468", ID_AD7468 },
422 { "ad7475", ID_AD7475 },
423 { "ad7476", ID_AD7466 },
424 { "ad7476a", ID_AD7466 },
425 { "ad7477", ID_AD7467 },
426 { "ad7477a", ID_AD7467 },
427 { "ad7478", ID_AD7468 },
428 { "ad7478a", ID_AD7468 },
429 { "ad7495", ID_AD7495 },
430 { "ad7910", ID_AD7467 },
431 { "ad7920", ID_AD7466 },
432 { "ad7940", ID_AD7940 },
433 { "adc081s", ID_ADC081S },
434 { "adc101s", ID_ADC101S },
435 { "adc121s", ID_ADC121S },
436 { "ads7866", ID_ADS7866 },
437 { "ads7867", ID_ADS7867 },
438 { "ads7868", ID_ADS7868 },
439 { "ltc2314-14", ID_LTC2314_14 },
442 MODULE_DEVICE_TABLE(spi, ad7476_id);
444 static struct spi_driver ad7476_driver = {
448 .probe = ad7476_probe,
449 .id_table = ad7476_id,
451 module_spi_driver(ad7476_driver);
454 MODULE_DESCRIPTION("Analog Devices AD7476 and similar 1-channel ADCs");
455 MODULE_LICENSE("GPL v2");