1 // SPDX-License-Identifier: GPL-2.0-only
3 * Analog Devices AD738x Simultaneous Sampling SAR ADCs
5 * Copyright 2017 Analog Devices Inc.
6 * Copyright 2024 BayLibre, SAS
8 * Datasheets of supported parts:
9 * ad7380/1 : https://www.analog.com/media/en/technical-documentation/data-sheets/AD7380-7381.pdf
10 * ad7383/4 : https://www.analog.com/media/en/technical-documentation/data-sheets/ad7383-7384.pdf
11 * ad7386/7/8 : https://www.analog.com/media/en/technical-documentation/data-sheets/AD7386-7387-7388.pdf
12 * ad7380-4 : https://www.analog.com/media/en/technical-documentation/data-sheets/ad7380-4.pdf
13 * ad7381-4 : https://www.analog.com/media/en/technical-documentation/data-sheets/ad7381-4.pdf
14 * ad7383/4-4 : https://www.analog.com/media/en/technical-documentation/data-sheets/ad7383-4-ad7384-4.pdf
15 * ad7386/7/8-4 : https://www.analog.com/media/en/technical-documentation/data-sheets/ad7386-4-7387-4-7388-4.pdf
16 * adaq4370-4 : https://www.analog.com/media/en/technical-documentation/data-sheets/adaq4370-4.pdf
17 * adaq4380-4 : https://www.analog.com/media/en/technical-documentation/data-sheets/adaq4380-4.pdf
20 #include <linux/align.h>
21 #include <linux/bitfield.h>
22 #include <linux/bitops.h>
23 #include <linux/cleanup.h>
24 #include <linux/device.h>
25 #include <linux/err.h>
26 #include <linux/kernel.h>
27 #include <linux/math.h>
28 #include <linux/module.h>
29 #include <linux/regmap.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/slab.h>
32 #include <linux/spi/spi.h>
33 #include <linux/units.h>
34 #include <linux/util_macros.h>
36 #include <linux/iio/buffer.h>
37 #include <linux/iio/iio.h>
38 #include <linux/iio/trigger_consumer.h>
39 #include <linux/iio/triggered_buffer.h>
41 #define MAX_NUM_CHANNELS 8
42 /* 2.5V internal reference voltage */
43 #define AD7380_INTERNAL_REF_MV 2500
44 /* 3.3V internal reference voltage for ADAQ */
45 #define ADAQ4380_INTERNAL_REF_MV 3300
47 /* reading and writing registers is more reliable at lower than max speed */
48 #define AD7380_REG_WR_SPEED_HZ 10000000
50 #define AD7380_REG_WR BIT(15)
51 #define AD7380_REG_REGADDR GENMASK(14, 12)
52 #define AD7380_REG_DATA GENMASK(11, 0)
54 #define AD7380_REG_ADDR_NOP 0x0
55 #define AD7380_REG_ADDR_CONFIG1 0x1
56 #define AD7380_REG_ADDR_CONFIG2 0x2
57 #define AD7380_REG_ADDR_ALERT 0x3
58 #define AD7380_REG_ADDR_ALERT_LOW_TH 0x4
59 #define AD7380_REG_ADDR_ALERT_HIGH_TH 0x5
61 #define AD7380_CONFIG1_CH BIT(11)
62 #define AD7380_CONFIG1_SEQ BIT(10)
63 #define AD7380_CONFIG1_OS_MODE BIT(9)
64 #define AD7380_CONFIG1_OSR GENMASK(8, 6)
65 #define AD7380_CONFIG1_CRC_W BIT(5)
66 #define AD7380_CONFIG1_CRC_R BIT(4)
67 #define AD7380_CONFIG1_ALERTEN BIT(3)
68 #define AD7380_CONFIG1_RES BIT(2)
69 #define AD7380_CONFIG1_REFSEL BIT(1)
70 #define AD7380_CONFIG1_PMODE BIT(0)
72 #define AD7380_CONFIG2_SDO2 GENMASK(9, 8)
73 #define AD7380_CONFIG2_SDO BIT(8)
74 #define AD7380_CONFIG2_RESET GENMASK(7, 0)
76 #define AD7380_CONFIG2_RESET_SOFT 0x3C
77 #define AD7380_CONFIG2_RESET_HARD 0xFF
79 #define AD7380_ALERT_LOW_TH GENMASK(11, 0)
80 #define AD7380_ALERT_HIGH_TH GENMASK(11, 0)
82 #define T_CONVERT_NS 190 /* conversion time */
83 #define T_CONVERT_0_NS 10 /* 1st conversion start time (oversampling) */
84 #define T_CONVERT_X_NS 500 /* xth conversion start time (oversampling) */
85 #define T_POWERUP_US 5000 /* Power up */
88 * AD738x support several SDO lines to increase throughput, but driver currently
89 * supports only 1 SDO line (standard SPI transaction)
91 #define AD7380_NUM_SDO_LINES 1
92 #define AD7380_DEFAULT_GAIN_MILLI 1000
94 struct ad7380_timing_specs {
95 const unsigned int t_csh_ns; /* CS minimum high time */
98 struct ad7380_chip_info {
100 const struct iio_chan_spec *channels;
101 unsigned int num_channels;
102 unsigned int num_simult_channels;
103 bool has_hardware_gain;
105 const char * const *supplies;
106 unsigned int num_supplies;
107 bool external_ref_only;
108 bool adaq_internal_ref_only;
109 const char * const *vcm_supplies;
110 unsigned int num_vcm_supplies;
111 const unsigned long *available_scan_masks;
112 const struct ad7380_timing_specs *timing_specs;
116 AD7380_SCAN_TYPE_NORMAL,
117 AD7380_SCAN_TYPE_RESOLUTION_BOOST,
120 /* Extended scan types for 12-bit unsigned chips. */
121 static const struct iio_scan_type ad7380_scan_type_12_u[] = {
122 [AD7380_SCAN_TYPE_NORMAL] = {
126 .endianness = IIO_CPU,
128 [AD7380_SCAN_TYPE_RESOLUTION_BOOST] = {
132 .endianness = IIO_CPU,
136 /* Extended scan types for 14-bit signed chips. */
137 static const struct iio_scan_type ad7380_scan_type_14_s[] = {
138 [AD7380_SCAN_TYPE_NORMAL] = {
142 .endianness = IIO_CPU,
144 [AD7380_SCAN_TYPE_RESOLUTION_BOOST] = {
148 .endianness = IIO_CPU,
152 /* Extended scan types for 14-bit unsigned chips. */
153 static const struct iio_scan_type ad7380_scan_type_14_u[] = {
154 [AD7380_SCAN_TYPE_NORMAL] = {
158 .endianness = IIO_CPU,
160 [AD7380_SCAN_TYPE_RESOLUTION_BOOST] = {
164 .endianness = IIO_CPU,
168 /* Extended scan types for 16-bit signed_chips. */
169 static const struct iio_scan_type ad7380_scan_type_16_s[] = {
170 [AD7380_SCAN_TYPE_NORMAL] = {
174 .endianness = IIO_CPU,
176 [AD7380_SCAN_TYPE_RESOLUTION_BOOST] = {
180 .endianness = IIO_CPU,
184 /* Extended scan types for 16-bit unsigned chips. */
185 static const struct iio_scan_type ad7380_scan_type_16_u[] = {
186 [AD7380_SCAN_TYPE_NORMAL] = {
190 .endianness = IIO_CPU,
192 [AD7380_SCAN_TYPE_RESOLUTION_BOOST] = {
196 .endianness = IIO_CPU,
200 #define _AD7380_CHANNEL(index, bits, diff, sign, gain) { \
201 .type = IIO_VOLTAGE, \
202 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
203 ((gain) ? BIT(IIO_CHAN_INFO_SCALE) : 0) | \
204 ((diff) ? 0 : BIT(IIO_CHAN_INFO_OFFSET)), \
205 .info_mask_shared_by_type = ((gain) ? 0 : BIT(IIO_CHAN_INFO_SCALE)) | \
206 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
207 .info_mask_shared_by_type_available = \
208 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
210 .differential = (diff), \
211 .channel = (diff) ? (2 * (index)) : (index), \
212 .channel2 = (diff) ? (2 * (index) + 1) : 0, \
213 .scan_index = (index), \
214 .has_ext_scan_type = 1, \
215 .ext_scan_type = ad7380_scan_type_##bits##_##sign, \
216 .num_ext_scan_type = ARRAY_SIZE(ad7380_scan_type_##bits##_##sign), \
219 #define AD7380_CHANNEL(index, bits, diff, sign) \
220 _AD7380_CHANNEL(index, bits, diff, sign, false)
222 #define ADAQ4380_CHANNEL(index, bits, diff, sign) \
223 _AD7380_CHANNEL(index, bits, diff, sign, true)
225 #define DEFINE_AD7380_2_CHANNEL(name, bits, diff, sign) \
226 static const struct iio_chan_spec name[] = { \
227 AD7380_CHANNEL(0, bits, diff, sign), \
228 AD7380_CHANNEL(1, bits, diff, sign), \
229 IIO_CHAN_SOFT_TIMESTAMP(2), \
232 #define DEFINE_AD7380_4_CHANNEL(name, bits, diff, sign) \
233 static const struct iio_chan_spec name[] = { \
234 AD7380_CHANNEL(0, bits, diff, sign), \
235 AD7380_CHANNEL(1, bits, diff, sign), \
236 AD7380_CHANNEL(2, bits, diff, sign), \
237 AD7380_CHANNEL(3, bits, diff, sign), \
238 IIO_CHAN_SOFT_TIMESTAMP(4), \
241 #define DEFINE_ADAQ4380_4_CHANNEL(name, bits, diff, sign) \
242 static const struct iio_chan_spec name[] = { \
243 ADAQ4380_CHANNEL(0, bits, diff, sign), \
244 ADAQ4380_CHANNEL(1, bits, diff, sign), \
245 ADAQ4380_CHANNEL(2, bits, diff, sign), \
246 ADAQ4380_CHANNEL(3, bits, diff, sign), \
247 IIO_CHAN_SOFT_TIMESTAMP(4), \
250 #define DEFINE_AD7380_8_CHANNEL(name, bits, diff, sign) \
251 static const struct iio_chan_spec name[] = { \
252 AD7380_CHANNEL(0, bits, diff, sign), \
253 AD7380_CHANNEL(1, bits, diff, sign), \
254 AD7380_CHANNEL(2, bits, diff, sign), \
255 AD7380_CHANNEL(3, bits, diff, sign), \
256 AD7380_CHANNEL(4, bits, diff, sign), \
257 AD7380_CHANNEL(5, bits, diff, sign), \
258 AD7380_CHANNEL(6, bits, diff, sign), \
259 AD7380_CHANNEL(7, bits, diff, sign), \
260 IIO_CHAN_SOFT_TIMESTAMP(8), \
263 /* fully differential */
264 DEFINE_AD7380_2_CHANNEL(ad7380_channels, 16, 1, s);
265 DEFINE_AD7380_2_CHANNEL(ad7381_channels, 14, 1, s);
266 DEFINE_AD7380_4_CHANNEL(ad7380_4_channels, 16, 1, s);
267 DEFINE_AD7380_4_CHANNEL(ad7381_4_channels, 14, 1, s);
268 DEFINE_ADAQ4380_4_CHANNEL(adaq4380_4_channels, 16, 1, s);
269 /* pseudo differential */
270 DEFINE_AD7380_2_CHANNEL(ad7383_channels, 16, 0, s);
271 DEFINE_AD7380_2_CHANNEL(ad7384_channels, 14, 0, s);
272 DEFINE_AD7380_4_CHANNEL(ad7383_4_channels, 16, 0, s);
273 DEFINE_AD7380_4_CHANNEL(ad7384_4_channels, 14, 0, s);
276 DEFINE_AD7380_4_CHANNEL(ad7386_channels, 16, 0, u);
277 DEFINE_AD7380_4_CHANNEL(ad7387_channels, 14, 0, u);
278 DEFINE_AD7380_4_CHANNEL(ad7388_channels, 12, 0, u);
279 DEFINE_AD7380_8_CHANNEL(ad7386_4_channels, 16, 0, u);
280 DEFINE_AD7380_8_CHANNEL(ad7387_4_channels, 14, 0, u);
281 DEFINE_AD7380_8_CHANNEL(ad7388_4_channels, 12, 0, u);
283 static const char * const ad7380_supplies[] = {
287 static const char * const adaq4380_supplies[] = {
288 "ldo", "vcc", "vlogic", "vs-p", "vs-n", "refin",
291 static const char * const ad7380_2_channel_vcm_supplies[] = {
295 static const char * const ad7380_4_channel_vcm_supplies[] = {
296 "aina", "ainb", "ainc", "aind",
299 /* Since this is simultaneous sampling, we don't allow individual channels. */
300 static const unsigned long ad7380_2_channel_scan_masks[] = {
305 static const unsigned long ad7380_4_channel_scan_masks[] = {
311 * Single ended parts have a 2:1 multiplexer in front of each ADC.
313 * From an IIO point of view, all inputs are exported, i.e ad7386/7/8
314 * export 4 channels and ad7386-4/7-4/8-4 export 8 channels.
316 * Inputs AinX0 of multiplexers correspond to the first half of IIO channels
317 * (i.e 0-1 or 0-3) and inputs AinX1 correspond to second half (i.e 2-3 or
318 * 4-7). Example for AD7386/7/8 (2 channels parts):
321 * | +----------------------------
324 * voltage0 | AinA0 --|--->| | | |
325 * | | | mux |----->| ADCA |---
326 * voltage2 | AinA1 --|--->| | | |
327 * | | |_____| |_____ |
330 * voltage1 | AinB0 --|--->| | | |
331 * | | | mux |----->| ADCB |---
332 * voltage3 | AinB1 --|--->| | | |
333 * | | |_____| |______|
335 * | +----------------------------
337 * Since this is simultaneous sampling for AinX0 OR AinX1 we have two separate
339 * When sequencer mode is enabled, chip automatically cycles through
340 * AinX0 and AinX1 channels. From an IIO point of view, we ca enable all
341 * channels, at the cost of an extra read, thus dividing the maximum rate by
345 AD7380_SCAN_MASK_CH_0,
346 AD7380_SCAN_MASK_CH_1,
347 AD7380_SCAN_MASK_SEQ,
350 static const unsigned long ad7380_2x2_channel_scan_masks[] = {
351 [AD7380_SCAN_MASK_CH_0] = GENMASK(1, 0),
352 [AD7380_SCAN_MASK_CH_1] = GENMASK(3, 2),
353 [AD7380_SCAN_MASK_SEQ] = GENMASK(3, 0),
357 static const unsigned long ad7380_2x4_channel_scan_masks[] = {
358 [AD7380_SCAN_MASK_CH_0] = GENMASK(3, 0),
359 [AD7380_SCAN_MASK_CH_1] = GENMASK(7, 4),
360 [AD7380_SCAN_MASK_SEQ] = GENMASK(7, 0),
364 static const struct ad7380_timing_specs ad7380_timing = {
368 static const struct ad7380_timing_specs ad7380_4_timing = {
373 * Available oversampling ratios. The indices correspond with the bit value
374 * expected by the chip. The available ratios depend on the averaging mode,
375 * only normal averaging is supported for now.
377 static const int ad7380_oversampling_ratios[] = {
381 /* Gains stored as fractions of 1000 so they can be expressed by integers. */
382 static const int ad7380_gains[] = {
383 300, 600, 1000, 1600,
386 static const struct ad7380_chip_info ad7380_chip_info = {
388 .channels = ad7380_channels,
389 .num_channels = ARRAY_SIZE(ad7380_channels),
390 .num_simult_channels = 2,
391 .supplies = ad7380_supplies,
392 .num_supplies = ARRAY_SIZE(ad7380_supplies),
393 .available_scan_masks = ad7380_2_channel_scan_masks,
394 .timing_specs = &ad7380_timing,
397 static const struct ad7380_chip_info ad7381_chip_info = {
399 .channels = ad7381_channels,
400 .num_channels = ARRAY_SIZE(ad7381_channels),
401 .num_simult_channels = 2,
402 .supplies = ad7380_supplies,
403 .num_supplies = ARRAY_SIZE(ad7380_supplies),
404 .available_scan_masks = ad7380_2_channel_scan_masks,
405 .timing_specs = &ad7380_timing,
408 static const struct ad7380_chip_info ad7383_chip_info = {
410 .channels = ad7383_channels,
411 .num_channels = ARRAY_SIZE(ad7383_channels),
412 .num_simult_channels = 2,
413 .supplies = ad7380_supplies,
414 .num_supplies = ARRAY_SIZE(ad7380_supplies),
415 .vcm_supplies = ad7380_2_channel_vcm_supplies,
416 .num_vcm_supplies = ARRAY_SIZE(ad7380_2_channel_vcm_supplies),
417 .available_scan_masks = ad7380_2_channel_scan_masks,
418 .timing_specs = &ad7380_timing,
421 static const struct ad7380_chip_info ad7384_chip_info = {
423 .channels = ad7384_channels,
424 .num_channels = ARRAY_SIZE(ad7384_channels),
425 .num_simult_channels = 2,
426 .supplies = ad7380_supplies,
427 .num_supplies = ARRAY_SIZE(ad7380_supplies),
428 .vcm_supplies = ad7380_2_channel_vcm_supplies,
429 .num_vcm_supplies = ARRAY_SIZE(ad7380_2_channel_vcm_supplies),
430 .available_scan_masks = ad7380_2_channel_scan_masks,
431 .timing_specs = &ad7380_timing,
434 static const struct ad7380_chip_info ad7386_chip_info = {
436 .channels = ad7386_channels,
437 .num_channels = ARRAY_SIZE(ad7386_channels),
438 .num_simult_channels = 2,
439 .supplies = ad7380_supplies,
440 .num_supplies = ARRAY_SIZE(ad7380_supplies),
442 .available_scan_masks = ad7380_2x2_channel_scan_masks,
443 .timing_specs = &ad7380_timing,
446 static const struct ad7380_chip_info ad7387_chip_info = {
448 .channels = ad7387_channels,
449 .num_channels = ARRAY_SIZE(ad7387_channels),
450 .num_simult_channels = 2,
451 .supplies = ad7380_supplies,
452 .num_supplies = ARRAY_SIZE(ad7380_supplies),
454 .available_scan_masks = ad7380_2x2_channel_scan_masks,
455 .timing_specs = &ad7380_timing,
458 static const struct ad7380_chip_info ad7388_chip_info = {
460 .channels = ad7388_channels,
461 .num_channels = ARRAY_SIZE(ad7388_channels),
462 .num_simult_channels = 2,
463 .supplies = ad7380_supplies,
464 .num_supplies = ARRAY_SIZE(ad7380_supplies),
466 .available_scan_masks = ad7380_2x2_channel_scan_masks,
467 .timing_specs = &ad7380_timing,
470 static const struct ad7380_chip_info ad7380_4_chip_info = {
472 .channels = ad7380_4_channels,
473 .num_channels = ARRAY_SIZE(ad7380_4_channels),
474 .num_simult_channels = 4,
475 .supplies = ad7380_supplies,
476 .num_supplies = ARRAY_SIZE(ad7380_supplies),
477 .external_ref_only = true,
478 .available_scan_masks = ad7380_4_channel_scan_masks,
479 .timing_specs = &ad7380_4_timing,
482 static const struct ad7380_chip_info ad7381_4_chip_info = {
484 .channels = ad7381_4_channels,
485 .num_channels = ARRAY_SIZE(ad7381_4_channels),
486 .num_simult_channels = 4,
487 .supplies = ad7380_supplies,
488 .num_supplies = ARRAY_SIZE(ad7380_supplies),
489 .available_scan_masks = ad7380_4_channel_scan_masks,
490 .timing_specs = &ad7380_4_timing,
493 static const struct ad7380_chip_info ad7383_4_chip_info = {
495 .channels = ad7383_4_channels,
496 .num_channels = ARRAY_SIZE(ad7383_4_channels),
497 .num_simult_channels = 4,
498 .supplies = ad7380_supplies,
499 .num_supplies = ARRAY_SIZE(ad7380_supplies),
500 .vcm_supplies = ad7380_4_channel_vcm_supplies,
501 .num_vcm_supplies = ARRAY_SIZE(ad7380_4_channel_vcm_supplies),
502 .available_scan_masks = ad7380_4_channel_scan_masks,
503 .timing_specs = &ad7380_4_timing,
506 static const struct ad7380_chip_info ad7384_4_chip_info = {
508 .channels = ad7384_4_channels,
509 .num_channels = ARRAY_SIZE(ad7384_4_channels),
510 .num_simult_channels = 4,
511 .supplies = ad7380_supplies,
512 .num_supplies = ARRAY_SIZE(ad7380_supplies),
513 .vcm_supplies = ad7380_4_channel_vcm_supplies,
514 .num_vcm_supplies = ARRAY_SIZE(ad7380_4_channel_vcm_supplies),
515 .available_scan_masks = ad7380_4_channel_scan_masks,
516 .timing_specs = &ad7380_4_timing,
519 static const struct ad7380_chip_info ad7386_4_chip_info = {
521 .channels = ad7386_4_channels,
522 .num_channels = ARRAY_SIZE(ad7386_4_channels),
523 .num_simult_channels = 4,
524 .supplies = ad7380_supplies,
525 .num_supplies = ARRAY_SIZE(ad7380_supplies),
527 .available_scan_masks = ad7380_2x4_channel_scan_masks,
528 .timing_specs = &ad7380_4_timing,
531 static const struct ad7380_chip_info ad7387_4_chip_info = {
533 .channels = ad7387_4_channels,
534 .num_channels = ARRAY_SIZE(ad7387_4_channels),
535 .num_simult_channels = 4,
536 .supplies = ad7380_supplies,
537 .num_supplies = ARRAY_SIZE(ad7380_supplies),
539 .available_scan_masks = ad7380_2x4_channel_scan_masks,
540 .timing_specs = &ad7380_4_timing,
543 static const struct ad7380_chip_info ad7388_4_chip_info = {
545 .channels = ad7388_4_channels,
546 .num_channels = ARRAY_SIZE(ad7388_4_channels),
547 .num_simult_channels = 4,
548 .supplies = ad7380_supplies,
549 .num_supplies = ARRAY_SIZE(ad7380_supplies),
551 .available_scan_masks = ad7380_2x4_channel_scan_masks,
552 .timing_specs = &ad7380_4_timing,
555 static const struct ad7380_chip_info adaq4370_4_chip_info = {
556 .name = "adaq4370-4",
557 .channels = adaq4380_4_channels,
558 .num_channels = ARRAY_SIZE(adaq4380_4_channels),
559 .num_simult_channels = 4,
560 .supplies = adaq4380_supplies,
561 .num_supplies = ARRAY_SIZE(adaq4380_supplies),
562 .adaq_internal_ref_only = true,
563 .has_hardware_gain = true,
564 .available_scan_masks = ad7380_4_channel_scan_masks,
565 .timing_specs = &ad7380_4_timing,
568 static const struct ad7380_chip_info adaq4380_4_chip_info = {
569 .name = "adaq4380-4",
570 .channels = adaq4380_4_channels,
571 .num_channels = ARRAY_SIZE(adaq4380_4_channels),
572 .num_simult_channels = 4,
573 .supplies = adaq4380_supplies,
574 .num_supplies = ARRAY_SIZE(adaq4380_supplies),
575 .adaq_internal_ref_only = true,
576 .has_hardware_gain = true,
577 .available_scan_masks = ad7380_4_channel_scan_masks,
578 .timing_specs = &ad7380_4_timing,
581 struct ad7380_state {
582 const struct ad7380_chip_info *chip_info;
583 struct spi_device *spi;
584 struct regmap *regmap;
585 unsigned int oversampling_ratio;
586 bool resolution_boost_enabled;
589 unsigned int vref_mv;
590 unsigned int vcm_mv[MAX_NUM_CHANNELS];
591 unsigned int gain_milli[MAX_NUM_CHANNELS];
592 /* xfers, message an buffer for reading sample data */
593 struct spi_transfer normal_xfer[2];
594 struct spi_message normal_msg;
595 struct spi_transfer seq_xfer[4];
596 struct spi_message seq_msg;
598 * DMA (thus cache coherency maintenance) requires the transfer buffers
599 * to live in their own cache lines.
601 * Make the buffer large enough for MAX_NUM_CHANNELS 32-bit samples and
602 * one 64-bit aligned 64-bit timestamp.
604 u8 scan_data[ALIGN(MAX_NUM_CHANNELS * sizeof(u32), sizeof(s64))
605 + sizeof(s64)] __aligned(IIO_DMA_MINALIGN);
606 /* buffers for reading/writing registers */
611 static int ad7380_regmap_reg_write(void *context, unsigned int reg,
614 struct ad7380_state *st = context;
615 struct spi_transfer xfer = {
616 .speed_hz = AD7380_REG_WR_SPEED_HZ,
622 st->tx = FIELD_PREP(AD7380_REG_WR, 1) |
623 FIELD_PREP(AD7380_REG_REGADDR, reg) |
624 FIELD_PREP(AD7380_REG_DATA, val);
626 return spi_sync_transfer(st->spi, &xfer, 1);
629 static int ad7380_regmap_reg_read(void *context, unsigned int reg,
632 struct ad7380_state *st = context;
633 struct spi_transfer xfers[] = {
635 .speed_hz = AD7380_REG_WR_SPEED_HZ,
641 .value = st->chip_info->timing_specs->t_csh_ns,
642 .unit = SPI_DELAY_UNIT_NSECS,
645 .speed_hz = AD7380_REG_WR_SPEED_HZ,
653 st->tx = FIELD_PREP(AD7380_REG_WR, 0) |
654 FIELD_PREP(AD7380_REG_REGADDR, reg) |
655 FIELD_PREP(AD7380_REG_DATA, 0);
657 ret = spi_sync_transfer(st->spi, xfers, ARRAY_SIZE(xfers));
661 *val = FIELD_GET(AD7380_REG_DATA, st->rx);
666 static const struct regmap_config ad7380_regmap_config = {
669 .reg_read = ad7380_regmap_reg_read,
670 .reg_write = ad7380_regmap_reg_write,
671 .max_register = AD7380_REG_ADDR_ALERT_HIGH_TH,
675 static int ad7380_debugfs_reg_access(struct iio_dev *indio_dev, u32 reg,
676 u32 writeval, u32 *readval)
678 iio_device_claim_direct_scoped(return -EBUSY, indio_dev) {
679 struct ad7380_state *st = iio_priv(indio_dev);
682 return regmap_read(st->regmap, reg, readval);
684 return regmap_write(st->regmap, reg, writeval);
690 * When switching channel, the ADC require an additional settling time.
691 * According to the datasheet, data is value on the third CS low. We already
692 * have an extra toggle before each read (either direct reads or buffered reads)
693 * to sample correct data, so we just add a single CS toggle at the end of the
696 static int ad7380_set_ch(struct ad7380_state *st, unsigned int ch)
698 struct spi_transfer xfer = {
700 .value = T_CONVERT_NS,
701 .unit = SPI_DELAY_UNIT_NSECS,
709 ret = regmap_update_bits(st->regmap,
710 AD7380_REG_ADDR_CONFIG1,
712 FIELD_PREP(AD7380_CONFIG1_CH, ch));
719 if (st->oversampling_ratio > 1)
720 xfer.delay.value = T_CONVERT_0_NS +
721 T_CONVERT_X_NS * (st->oversampling_ratio - 1) *
722 st->chip_info->num_simult_channels / AD7380_NUM_SDO_LINES;
724 return spi_sync_transfer(st->spi, &xfer, 1);
728 * ad7380_update_xfers - update the SPI transfers base on the current scan type
729 * @st: device instance specific state
730 * @scan_type: current scan type
732 static void ad7380_update_xfers(struct ad7380_state *st,
733 const struct iio_scan_type *scan_type)
735 struct spi_transfer *xfer = st->seq ? st->seq_xfer : st->normal_xfer;
736 unsigned int t_convert = T_CONVERT_NS;
739 * In the case of oversampling, conversion time is higher than in normal
740 * mode. Technically T_CONVERT_X_NS is lower for some chips, but we use
741 * the maximum value for simplicity for now.
743 if (st->oversampling_ratio > 1)
744 t_convert = T_CONVERT_0_NS + T_CONVERT_X_NS *
745 (st->oversampling_ratio - 1) *
746 st->chip_info->num_simult_channels / AD7380_NUM_SDO_LINES;
749 xfer[0].delay.value = xfer[1].delay.value = t_convert;
750 xfer[0].delay.unit = xfer[1].delay.unit = SPI_DELAY_UNIT_NSECS;
751 xfer[2].bits_per_word = xfer[3].bits_per_word =
753 xfer[2].len = xfer[3].len =
754 BITS_TO_BYTES(scan_type->storagebits) *
755 st->chip_info->num_simult_channels;
756 xfer[3].rx_buf = xfer[2].rx_buf + xfer[2].len;
757 /* Additional delay required here when oversampling is enabled */
758 if (st->oversampling_ratio > 1)
759 xfer[2].delay.value = t_convert;
761 xfer[2].delay.value = 0;
762 xfer[2].delay.unit = SPI_DELAY_UNIT_NSECS;
764 xfer[0].delay.value = t_convert;
765 xfer[0].delay.unit = SPI_DELAY_UNIT_NSECS;
766 xfer[1].bits_per_word = scan_type->realbits;
767 xfer[1].len = BITS_TO_BYTES(scan_type->storagebits) *
768 st->chip_info->num_simult_channels;
772 static int ad7380_triggered_buffer_preenable(struct iio_dev *indio_dev)
774 struct ad7380_state *st = iio_priv(indio_dev);
775 const struct iio_scan_type *scan_type;
776 struct spi_message *msg = &st->normal_msg;
779 * Currently, we always read all channels at the same time. The scan_type
780 * is the same for all channels, so we just pass the first channel.
782 scan_type = iio_get_current_scan_type(indio_dev, &indio_dev->channels[0]);
783 if (IS_ERR(scan_type))
784 return PTR_ERR(scan_type);
786 if (st->chip_info->has_mux) {
791 * Depending on the requested scan_mask and current state,
792 * we need to either change CH bit, or enable sequencer mode
793 * to sample correct data.
794 * Sequencer mode is enabled if active mask corresponds to all
795 * IIO channels enabled. Otherwise, CH bit is set.
797 ret = iio_active_scan_mask_index(indio_dev);
802 if (index == AD7380_SCAN_MASK_SEQ) {
803 ret = regmap_update_bits(st->regmap,
804 AD7380_REG_ADDR_CONFIG1,
806 FIELD_PREP(AD7380_CONFIG1_SEQ, 1));
812 ret = ad7380_set_ch(st, index);
819 ad7380_update_xfers(st, scan_type);
821 return spi_optimize_message(st->spi, msg);
824 static int ad7380_triggered_buffer_postdisable(struct iio_dev *indio_dev)
826 struct ad7380_state *st = iio_priv(indio_dev);
827 struct spi_message *msg = &st->normal_msg;
831 ret = regmap_update_bits(st->regmap,
832 AD7380_REG_ADDR_CONFIG1,
834 FIELD_PREP(AD7380_CONFIG1_SEQ, 0));
842 spi_unoptimize_message(msg);
847 static const struct iio_buffer_setup_ops ad7380_buffer_setup_ops = {
848 .preenable = ad7380_triggered_buffer_preenable,
849 .postdisable = ad7380_triggered_buffer_postdisable,
852 static irqreturn_t ad7380_trigger_handler(int irq, void *p)
854 struct iio_poll_func *pf = p;
855 struct iio_dev *indio_dev = pf->indio_dev;
856 struct ad7380_state *st = iio_priv(indio_dev);
857 struct spi_message *msg = st->seq ? &st->seq_msg : &st->normal_msg;
860 ret = spi_sync(st->spi, msg);
864 iio_push_to_buffers_with_timestamp(indio_dev, &st->scan_data,
868 iio_trigger_notify_done(indio_dev->trig);
873 static int ad7380_read_direct(struct ad7380_state *st, unsigned int scan_index,
874 const struct iio_scan_type *scan_type, int *val)
876 unsigned int index = scan_index;
879 if (st->chip_info->has_mux) {
882 if (index >= st->chip_info->num_simult_channels) {
883 index -= st->chip_info->num_simult_channels;
887 ret = ad7380_set_ch(st, ch);
892 ad7380_update_xfers(st, scan_type);
894 ret = spi_sync(st->spi, &st->normal_msg);
898 if (scan_type->storagebits > 16) {
899 if (scan_type->sign == 's')
900 *val = sign_extend32(*(u32 *)(st->scan_data + 4 * index),
901 scan_type->realbits - 1);
903 *val = *(u32 *)(st->scan_data + 4 * index) &
904 GENMASK(scan_type->realbits - 1, 0);
906 if (scan_type->sign == 's')
907 *val = sign_extend32(*(u16 *)(st->scan_data + 2 * index),
908 scan_type->realbits - 1);
910 *val = *(u16 *)(st->scan_data + 2 * index) &
911 GENMASK(scan_type->realbits - 1, 0);
917 static int ad7380_read_raw(struct iio_dev *indio_dev,
918 struct iio_chan_spec const *chan,
919 int *val, int *val2, long info)
921 struct ad7380_state *st = iio_priv(indio_dev);
922 const struct iio_scan_type *scan_type;
924 scan_type = iio_get_current_scan_type(indio_dev, chan);
926 if (IS_ERR(scan_type))
927 return PTR_ERR(scan_type);
930 case IIO_CHAN_INFO_RAW:
931 iio_device_claim_direct_scoped(return -EBUSY, indio_dev) {
932 return ad7380_read_direct(st, chan->scan_index,
936 case IIO_CHAN_INFO_SCALE:
938 * According to the datasheet, the LSB size is:
939 * * (2 × VREF) / 2^N, for differential chips
940 * * VREF / 2^N, for pseudo-differential chips
941 * where N is the ADC resolution (i.e realbits)
943 * The gain is stored as a fraction of 1000 and, as we need to
944 * divide vref_mv by the gain, we invert the gain/1000 fraction.
946 if (st->chip_info->has_hardware_gain)
947 *val = mult_frac(st->vref_mv, MILLI,
948 st->gain_milli[chan->scan_index]);
951 *val2 = scan_type->realbits - chan->differential;
953 return IIO_VAL_FRACTIONAL_LOG2;
954 case IIO_CHAN_INFO_OFFSET:
956 * According to IIO ABI, offset is applied before scale,
957 * so offset is: vcm_mv / scale
959 *val = st->vcm_mv[chan->channel] * (1 << scan_type->realbits)
963 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
964 *val = st->oversampling_ratio;
972 static int ad7380_read_avail(struct iio_dev *indio_dev,
973 struct iio_chan_spec const *chan,
974 const int **vals, int *type, int *length,
978 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
979 *vals = ad7380_oversampling_ratios;
980 *length = ARRAY_SIZE(ad7380_oversampling_ratios);
983 return IIO_AVAIL_LIST;
990 * ad7380_osr_to_regval - convert ratio to OSR register value
991 * @ratio: ratio to check
993 * Check if ratio is present in the list of available ratios and return the
994 * corresponding value that needs to be written to the register to select that
997 * Returns: register value (0 to 7) or -EINVAL if there is not an exact match
999 static int ad7380_osr_to_regval(int ratio)
1003 for (i = 0; i < ARRAY_SIZE(ad7380_oversampling_ratios); i++) {
1004 if (ratio == ad7380_oversampling_ratios[i])
1011 static int ad7380_write_raw(struct iio_dev *indio_dev,
1012 struct iio_chan_spec const *chan, int val,
1013 int val2, long mask)
1015 struct ad7380_state *st = iio_priv(indio_dev);
1016 int ret, osr, boost;
1019 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
1020 osr = ad7380_osr_to_regval(val);
1024 /* always enable resolution boost when oversampling is enabled */
1025 boost = osr > 0 ? 1 : 0;
1027 iio_device_claim_direct_scoped(return -EBUSY, indio_dev) {
1028 ret = regmap_update_bits(st->regmap,
1029 AD7380_REG_ADDR_CONFIG1,
1030 AD7380_CONFIG1_OSR | AD7380_CONFIG1_RES,
1031 FIELD_PREP(AD7380_CONFIG1_OSR, osr) |
1032 FIELD_PREP(AD7380_CONFIG1_RES, boost));
1037 st->oversampling_ratio = val;
1038 st->resolution_boost_enabled = boost;
1041 * Perform a soft reset. This will flush the oversampling
1042 * block and FIFO but will maintain the content of the
1043 * configurable registers.
1045 return regmap_update_bits(st->regmap,
1046 AD7380_REG_ADDR_CONFIG2,
1047 AD7380_CONFIG2_RESET,
1048 FIELD_PREP(AD7380_CONFIG2_RESET,
1049 AD7380_CONFIG2_RESET_SOFT));
1057 static int ad7380_get_current_scan_type(const struct iio_dev *indio_dev,
1058 const struct iio_chan_spec *chan)
1060 struct ad7380_state *st = iio_priv(indio_dev);
1062 return st->resolution_boost_enabled ? AD7380_SCAN_TYPE_RESOLUTION_BOOST
1063 : AD7380_SCAN_TYPE_NORMAL;
1066 static const struct iio_info ad7380_info = {
1067 .read_raw = &ad7380_read_raw,
1068 .read_avail = &ad7380_read_avail,
1069 .write_raw = &ad7380_write_raw,
1070 .get_current_scan_type = &ad7380_get_current_scan_type,
1071 .debugfs_reg_access = &ad7380_debugfs_reg_access,
1074 static int ad7380_init(struct ad7380_state *st, bool external_ref_en)
1078 /* perform hard reset */
1079 ret = regmap_update_bits(st->regmap, AD7380_REG_ADDR_CONFIG2,
1080 AD7380_CONFIG2_RESET,
1081 FIELD_PREP(AD7380_CONFIG2_RESET,
1082 AD7380_CONFIG2_RESET_HARD));
1086 if (external_ref_en) {
1087 /* select external reference voltage */
1088 ret = regmap_set_bits(st->regmap, AD7380_REG_ADDR_CONFIG1,
1089 AD7380_CONFIG1_REFSEL);
1094 /* This is the default value after reset. */
1095 st->oversampling_ratio = 1;
1099 /* SPI 1-wire mode */
1100 return regmap_update_bits(st->regmap, AD7380_REG_ADDR_CONFIG2,
1102 FIELD_PREP(AD7380_CONFIG2_SDO,
1103 AD7380_NUM_SDO_LINES));
1106 static int ad7380_probe(struct spi_device *spi)
1108 struct device *dev = &spi->dev;
1109 struct iio_dev *indio_dev;
1110 struct ad7380_state *st;
1111 bool external_ref_en;
1114 indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
1118 st = iio_priv(indio_dev);
1120 st->chip_info = spi_get_device_match_data(spi);
1122 return dev_err_probe(dev, -EINVAL, "missing match data\n");
1124 ret = devm_regulator_bulk_get_enable(dev, st->chip_info->num_supplies,
1125 st->chip_info->supplies);
1128 return dev_err_probe(dev, ret,
1129 "Failed to enable power supplies\n");
1130 fsleep(T_POWERUP_US);
1132 if (st->chip_info->adaq_internal_ref_only) {
1134 * ADAQ chips use fixed internal reference but still
1135 * require a specific reference supply to power it.
1136 * "refin" is already enabled with other power supplies
1137 * in bulk_get_enable().
1140 st->vref_mv = ADAQ4380_INTERNAL_REF_MV;
1142 /* these chips don't have a register bit for this */
1143 external_ref_en = false;
1144 } else if (st->chip_info->external_ref_only) {
1145 ret = devm_regulator_get_enable_read_voltage(dev, "refin");
1147 return dev_err_probe(dev, ret,
1148 "Failed to get refin regulator\n");
1150 st->vref_mv = ret / 1000;
1152 /* these chips don't have a register bit for this */
1153 external_ref_en = false;
1156 * If there is no REFIO supply, then it means that we are using
1157 * the internal reference, otherwise REFIO is reference voltage.
1159 ret = devm_regulator_get_enable_read_voltage(dev, "refio");
1160 if (ret < 0 && ret != -ENODEV)
1161 return dev_err_probe(dev, ret,
1162 "Failed to get refio regulator\n");
1164 external_ref_en = ret != -ENODEV;
1165 st->vref_mv = external_ref_en ? ret / 1000 : AD7380_INTERNAL_REF_MV;
1168 if (st->chip_info->num_vcm_supplies > ARRAY_SIZE(st->vcm_mv))
1169 return dev_err_probe(dev, -EINVAL,
1170 "invalid number of VCM supplies\n");
1173 * pseudo-differential chips have common mode supplies for the negative
1176 for (i = 0; i < st->chip_info->num_vcm_supplies; i++) {
1177 const char *vcm = st->chip_info->vcm_supplies[i];
1179 ret = devm_regulator_get_enable_read_voltage(dev, vcm);
1181 return dev_err_probe(dev, ret,
1182 "Failed to get %s regulator\n",
1185 st->vcm_mv[i] = ret / 1000;
1188 for (i = 0; i < MAX_NUM_CHANNELS; i++)
1189 st->gain_milli[i] = AD7380_DEFAULT_GAIN_MILLI;
1191 if (st->chip_info->has_hardware_gain) {
1192 device_for_each_child_node_scoped(dev, node) {
1193 unsigned int channel, gain;
1196 ret = fwnode_property_read_u32(node, "reg", &channel);
1198 return dev_err_probe(dev, ret,
1199 "Failed to read reg property\n");
1201 if (channel >= st->chip_info->num_channels - 1)
1202 return dev_err_probe(dev, -EINVAL,
1203 "Invalid channel number %i\n",
1206 ret = fwnode_property_read_u32(node, "adi,gain-milli",
1208 if (ret && ret != -EINVAL)
1209 return dev_err_probe(dev, ret,
1210 "Failed to read gain for channel %i\n",
1212 if (ret != -EINVAL) {
1214 * Match gain value from dt to one of supported
1217 gain_idx = find_closest(gain, ad7380_gains,
1218 ARRAY_SIZE(ad7380_gains));
1219 st->gain_milli[channel] = ad7380_gains[gain_idx];
1224 st->regmap = devm_regmap_init(dev, NULL, st, &ad7380_regmap_config);
1225 if (IS_ERR(st->regmap))
1226 return dev_err_probe(dev, PTR_ERR(st->regmap),
1227 "failed to allocate register map\n");
1230 * Setting up xfer structures for both normal and sequence mode. These
1231 * struct are used for both direct read and triggered buffer. Additional
1232 * fields will be set up in ad7380_update_xfers() based on the current
1233 * state of the driver at the time of the read.
1237 * In normal mode a read is composed of two steps:
1238 * - first, toggle CS (no data xfer) to trigger a conversion
1241 st->normal_xfer[0].cs_change = 1;
1242 st->normal_xfer[0].cs_change_delay.value = st->chip_info->timing_specs->t_csh_ns;
1243 st->normal_xfer[0].cs_change_delay.unit = SPI_DELAY_UNIT_NSECS;
1244 st->normal_xfer[1].rx_buf = st->scan_data;
1246 spi_message_init_with_transfers(&st->normal_msg, st->normal_xfer,
1247 ARRAY_SIZE(st->normal_xfer));
1249 * In sequencer mode a read is composed of four steps:
1250 * - CS toggle (no data xfer) to get the right point in the sequence
1251 * - CS toggle (no data xfer) to trigger a conversion of AinX0 and
1252 * acquisition of AinX1
1253 * - 2 data reads, to read AinX0 and AinX1
1255 st->seq_xfer[0].cs_change = 1;
1256 st->seq_xfer[0].cs_change_delay.value = st->chip_info->timing_specs->t_csh_ns;
1257 st->seq_xfer[0].cs_change_delay.unit = SPI_DELAY_UNIT_NSECS;
1258 st->seq_xfer[1].cs_change = 1;
1259 st->seq_xfer[1].cs_change_delay.value = st->chip_info->timing_specs->t_csh_ns;
1260 st->seq_xfer[1].cs_change_delay.unit = SPI_DELAY_UNIT_NSECS;
1262 st->seq_xfer[2].rx_buf = st->scan_data;
1263 st->seq_xfer[2].cs_change = 1;
1264 st->seq_xfer[2].cs_change_delay.value = st->chip_info->timing_specs->t_csh_ns;
1265 st->seq_xfer[2].cs_change_delay.unit = SPI_DELAY_UNIT_NSECS;
1267 spi_message_init_with_transfers(&st->seq_msg, st->seq_xfer,
1268 ARRAY_SIZE(st->seq_xfer));
1270 indio_dev->channels = st->chip_info->channels;
1271 indio_dev->num_channels = st->chip_info->num_channels;
1272 indio_dev->name = st->chip_info->name;
1273 indio_dev->info = &ad7380_info;
1274 indio_dev->modes = INDIO_DIRECT_MODE;
1275 indio_dev->available_scan_masks = st->chip_info->available_scan_masks;
1277 ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
1278 iio_pollfunc_store_time,
1279 ad7380_trigger_handler,
1280 &ad7380_buffer_setup_ops);
1284 ret = ad7380_init(st, external_ref_en);
1288 return devm_iio_device_register(dev, indio_dev);
1291 static const struct of_device_id ad7380_of_match_table[] = {
1292 { .compatible = "adi,ad7380", .data = &ad7380_chip_info },
1293 { .compatible = "adi,ad7381", .data = &ad7381_chip_info },
1294 { .compatible = "adi,ad7383", .data = &ad7383_chip_info },
1295 { .compatible = "adi,ad7384", .data = &ad7384_chip_info },
1296 { .compatible = "adi,ad7386", .data = &ad7386_chip_info },
1297 { .compatible = "adi,ad7387", .data = &ad7387_chip_info },
1298 { .compatible = "adi,ad7388", .data = &ad7388_chip_info },
1299 { .compatible = "adi,ad7380-4", .data = &ad7380_4_chip_info },
1300 { .compatible = "adi,ad7381-4", .data = &ad7381_4_chip_info },
1301 { .compatible = "adi,ad7383-4", .data = &ad7383_4_chip_info },
1302 { .compatible = "adi,ad7384-4", .data = &ad7384_4_chip_info },
1303 { .compatible = "adi,ad7386-4", .data = &ad7386_4_chip_info },
1304 { .compatible = "adi,ad7387-4", .data = &ad7387_4_chip_info },
1305 { .compatible = "adi,ad7388-4", .data = &ad7388_4_chip_info },
1306 { .compatible = "adi,adaq4370-4", .data = &adaq4370_4_chip_info },
1307 { .compatible = "adi,adaq4380-4", .data = &adaq4380_4_chip_info },
1311 static const struct spi_device_id ad7380_id_table[] = {
1312 { "ad7380", (kernel_ulong_t)&ad7380_chip_info },
1313 { "ad7381", (kernel_ulong_t)&ad7381_chip_info },
1314 { "ad7383", (kernel_ulong_t)&ad7383_chip_info },
1315 { "ad7384", (kernel_ulong_t)&ad7384_chip_info },
1316 { "ad7386", (kernel_ulong_t)&ad7386_chip_info },
1317 { "ad7387", (kernel_ulong_t)&ad7387_chip_info },
1318 { "ad7388", (kernel_ulong_t)&ad7388_chip_info },
1319 { "ad7380-4", (kernel_ulong_t)&ad7380_4_chip_info },
1320 { "ad7381-4", (kernel_ulong_t)&ad7381_4_chip_info },
1321 { "ad7383-4", (kernel_ulong_t)&ad7383_4_chip_info },
1322 { "ad7384-4", (kernel_ulong_t)&ad7384_4_chip_info },
1323 { "ad7386-4", (kernel_ulong_t)&ad7386_4_chip_info },
1324 { "ad7387-4", (kernel_ulong_t)&ad7387_4_chip_info },
1325 { "ad7388-4", (kernel_ulong_t)&ad7388_4_chip_info },
1326 { "adaq4370-4", (kernel_ulong_t)&adaq4370_4_chip_info },
1327 { "adaq4380-4", (kernel_ulong_t)&adaq4380_4_chip_info },
1330 MODULE_DEVICE_TABLE(spi, ad7380_id_table);
1332 static struct spi_driver ad7380_driver = {
1335 .of_match_table = ad7380_of_match_table,
1337 .probe = ad7380_probe,
1338 .id_table = ad7380_id_table,
1340 module_spi_driver(ad7380_driver);
1343 MODULE_DESCRIPTION("Analog Devices AD738x ADC driver");
1344 MODULE_LICENSE("GPL");