1 // SPDX-License-Identifier: GPL-2.0
3 * AD7190 AD7192 AD7193 AD7195 SPI ADC driver
5 * Copyright 2011-2015 Analog Devices Inc.
8 #include <linux/interrupt.h>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/sysfs.h>
14 #include <linux/spi/spi.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/err.h>
17 #include <linux/sched.h>
18 #include <linux/delay.h>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/sysfs.h>
22 #include <linux/iio/buffer.h>
23 #include <linux/iio/trigger.h>
24 #include <linux/iio/trigger_consumer.h>
25 #include <linux/iio/triggered_buffer.h>
26 #include <linux/iio/adc/ad_sigma_delta.h>
29 #define AD7192_REG_COMM 0 /* Communications Register (WO, 8-bit) */
30 #define AD7192_REG_STAT 0 /* Status Register (RO, 8-bit) */
31 #define AD7192_REG_MODE 1 /* Mode Register (RW, 24-bit */
32 #define AD7192_REG_CONF 2 /* Configuration Register (RW, 24-bit) */
33 #define AD7192_REG_DATA 3 /* Data Register (RO, 24/32-bit) */
34 #define AD7192_REG_ID 4 /* ID Register (RO, 8-bit) */
35 #define AD7192_REG_GPOCON 5 /* GPOCON Register (RO, 8-bit) */
36 #define AD7192_REG_OFFSET 6 /* Offset Register (RW, 16-bit */
37 /* (AD7792)/24-bit (AD7192)) */
38 #define AD7192_REG_FULLSALE 7 /* Full-Scale Register */
39 /* (RW, 16-bit (AD7792)/24-bit (AD7192)) */
41 /* Communications Register Bit Designations (AD7192_REG_COMM) */
42 #define AD7192_COMM_WEN BIT(7) /* Write Enable */
43 #define AD7192_COMM_WRITE 0 /* Write Operation */
44 #define AD7192_COMM_READ BIT(6) /* Read Operation */
45 #define AD7192_COMM_ADDR(x) (((x) & 0x7) << 3) /* Register Address */
46 #define AD7192_COMM_CREAD BIT(2) /* Continuous Read of Data Register */
48 /* Status Register Bit Designations (AD7192_REG_STAT) */
49 #define AD7192_STAT_RDY BIT(7) /* Ready */
50 #define AD7192_STAT_ERR BIT(6) /* Error (Overrange, Underrange) */
51 #define AD7192_STAT_NOREF BIT(5) /* Error no external reference */
52 #define AD7192_STAT_PARITY BIT(4) /* Parity */
53 #define AD7192_STAT_CH3 BIT(2) /* Channel 3 */
54 #define AD7192_STAT_CH2 BIT(1) /* Channel 2 */
55 #define AD7192_STAT_CH1 BIT(0) /* Channel 1 */
57 /* Mode Register Bit Designations (AD7192_REG_MODE) */
58 #define AD7192_MODE_SEL(x) (((x) & 0x7) << 21) /* Operation Mode Select */
59 #define AD7192_MODE_SEL_MASK (0x7 << 21) /* Operation Mode Select Mask */
60 #define AD7192_MODE_DAT_STA BIT(20) /* Status Register transmission */
61 #define AD7192_MODE_CLKSRC(x) (((x) & 0x3) << 18) /* Clock Source Select */
62 #define AD7192_MODE_SINC3 BIT(15) /* SINC3 Filter Select */
63 #define AD7192_MODE_ACX BIT(14) /* AC excitation enable(AD7195 only)*/
64 #define AD7192_MODE_ENPAR BIT(13) /* Parity Enable */
65 #define AD7192_MODE_CLKDIV BIT(12) /* Clock divide by 2 (AD7190/2 only)*/
66 #define AD7192_MODE_SCYCLE BIT(11) /* Single cycle conversion */
67 #define AD7192_MODE_REJ60 BIT(10) /* 50/60Hz notch filter */
68 #define AD7192_MODE_RATE(x) ((x) & 0x3FF) /* Filter Update Rate Select */
70 /* Mode Register: AD7192_MODE_SEL options */
71 #define AD7192_MODE_CONT 0 /* Continuous Conversion Mode */
72 #define AD7192_MODE_SINGLE 1 /* Single Conversion Mode */
73 #define AD7192_MODE_IDLE 2 /* Idle Mode */
74 #define AD7192_MODE_PWRDN 3 /* Power-Down Mode */
75 #define AD7192_MODE_CAL_INT_ZERO 4 /* Internal Zero-Scale Calibration */
76 #define AD7192_MODE_CAL_INT_FULL 5 /* Internal Full-Scale Calibration */
77 #define AD7192_MODE_CAL_SYS_ZERO 6 /* System Zero-Scale Calibration */
78 #define AD7192_MODE_CAL_SYS_FULL 7 /* System Full-Scale Calibration */
80 /* Mode Register: AD7192_MODE_CLKSRC options */
81 #define AD7192_CLK_EXT_MCLK1_2 0 /* External 4.92 MHz Clock connected*/
82 /* from MCLK1 to MCLK2 */
83 #define AD7192_CLK_EXT_MCLK2 1 /* External Clock applied to MCLK2 */
84 #define AD7192_CLK_INT 2 /* Internal 4.92 MHz Clock not */
85 /* available at the MCLK2 pin */
86 #define AD7192_CLK_INT_CO 3 /* Internal 4.92 MHz Clock available*/
87 /* at the MCLK2 pin */
89 /* Configuration Register Bit Designations (AD7192_REG_CONF) */
91 #define AD7192_CONF_CHOP BIT(23) /* CHOP enable */
92 #define AD7192_CONF_REFSEL BIT(20) /* REFIN1/REFIN2 Reference Select */
93 #define AD7192_CONF_CHAN(x) ((x) << 8) /* Channel select */
94 #define AD7192_CONF_CHAN_MASK (0x7FF << 8) /* Channel select mask */
95 #define AD7192_CONF_BURN BIT(7) /* Burnout current enable */
96 #define AD7192_CONF_REFDET BIT(6) /* Reference detect enable */
97 #define AD7192_CONF_BUF BIT(4) /* Buffered Mode Enable */
98 #define AD7192_CONF_UNIPOLAR BIT(3) /* Unipolar/Bipolar Enable */
99 #define AD7192_CONF_GAIN(x) ((x) & 0x7) /* Gain Select */
101 #define AD7192_CH_AIN1P_AIN2M BIT(0) /* AIN1(+) - AIN2(-) */
102 #define AD7192_CH_AIN3P_AIN4M BIT(1) /* AIN3(+) - AIN4(-) */
103 #define AD7192_CH_TEMP BIT(2) /* Temp Sensor */
104 #define AD7192_CH_AIN2P_AIN2M BIT(3) /* AIN2(+) - AIN2(-) */
105 #define AD7192_CH_AIN1 BIT(4) /* AIN1 - AINCOM */
106 #define AD7192_CH_AIN2 BIT(5) /* AIN2 - AINCOM */
107 #define AD7192_CH_AIN3 BIT(6) /* AIN3 - AINCOM */
108 #define AD7192_CH_AIN4 BIT(7) /* AIN4 - AINCOM */
110 #define AD7193_CH_AIN1P_AIN2M 0x001 /* AIN1(+) - AIN2(-) */
111 #define AD7193_CH_AIN3P_AIN4M 0x002 /* AIN3(+) - AIN4(-) */
112 #define AD7193_CH_AIN5P_AIN6M 0x004 /* AIN5(+) - AIN6(-) */
113 #define AD7193_CH_AIN7P_AIN8M 0x008 /* AIN7(+) - AIN8(-) */
114 #define AD7193_CH_TEMP 0x100 /* Temp senseor */
115 #define AD7193_CH_AIN2P_AIN2M 0x200 /* AIN2(+) - AIN2(-) */
116 #define AD7193_CH_AIN1 0x401 /* AIN1 - AINCOM */
117 #define AD7193_CH_AIN2 0x402 /* AIN2 - AINCOM */
118 #define AD7193_CH_AIN3 0x404 /* AIN3 - AINCOM */
119 #define AD7193_CH_AIN4 0x408 /* AIN4 - AINCOM */
120 #define AD7193_CH_AIN5 0x410 /* AIN5 - AINCOM */
121 #define AD7193_CH_AIN6 0x420 /* AIN6 - AINCOM */
122 #define AD7193_CH_AIN7 0x440 /* AIN7 - AINCOM */
123 #define AD7193_CH_AIN8 0x480 /* AIN7 - AINCOM */
124 #define AD7193_CH_AINCOM 0x600 /* AINCOM - AINCOM */
126 /* ID Register Bit Designations (AD7192_REG_ID) */
127 #define ID_AD7190 0x4
128 #define ID_AD7192 0x0
129 #define ID_AD7193 0x2
130 #define ID_AD7195 0x6
131 #define AD7192_ID_MASK 0x0F
133 /* GPOCON Register Bit Designations (AD7192_REG_GPOCON) */
134 #define AD7192_GPOCON_BPDSW BIT(6) /* Bridge power-down switch enable */
135 #define AD7192_GPOCON_GP32EN BIT(5) /* Digital Output P3 and P2 enable */
136 #define AD7192_GPOCON_GP10EN BIT(4) /* Digital Output P1 and P0 enable */
137 #define AD7192_GPOCON_P3DAT BIT(3) /* P3 state */
138 #define AD7192_GPOCON_P2DAT BIT(2) /* P2 state */
139 #define AD7192_GPOCON_P1DAT BIT(1) /* P1 state */
140 #define AD7192_GPOCON_P0DAT BIT(0) /* P0 state */
142 #define AD7192_EXT_FREQ_MHZ_MIN 2457600
143 #define AD7192_EXT_FREQ_MHZ_MAX 5120000
144 #define AD7192_INT_FREQ_MHZ 4915200
146 #define AD7192_NO_SYNC_FILTER 1
147 #define AD7192_SYNC3_FILTER 3
148 #define AD7192_SYNC4_FILTER 4
151 * The AD7190/2/5 features a dual use data out ready DOUT/RDY output.
152 * In order to avoid contentions on the SPI bus, it's therefore necessary
153 * to use spi bus locking.
155 * The DOUT/RDY output must also be wired to an interrupt capable GPIO.
159 AD7192_SYSCALIB_ZERO_SCALE,
160 AD7192_SYSCALIB_FULL_SCALE,
163 struct ad7192_state {
164 struct regulator *avdd;
165 struct regulator *dvdd;
172 u32 scale_avail[8][2];
176 struct mutex lock; /* protect sensor state */
179 struct ad_sigma_delta sd;
182 static const char * const ad7192_syscalib_modes[] = {
183 [AD7192_SYSCALIB_ZERO_SCALE] = "zero_scale",
184 [AD7192_SYSCALIB_FULL_SCALE] = "full_scale",
187 static int ad7192_set_syscalib_mode(struct iio_dev *indio_dev,
188 const struct iio_chan_spec *chan,
191 struct ad7192_state *st = iio_priv(indio_dev);
193 st->syscalib_mode[chan->channel] = mode;
198 static int ad7192_get_syscalib_mode(struct iio_dev *indio_dev,
199 const struct iio_chan_spec *chan)
201 struct ad7192_state *st = iio_priv(indio_dev);
203 return st->syscalib_mode[chan->channel];
206 static ssize_t ad7192_write_syscalib(struct iio_dev *indio_dev,
208 const struct iio_chan_spec *chan,
209 const char *buf, size_t len)
211 struct ad7192_state *st = iio_priv(indio_dev);
215 ret = strtobool(buf, &sys_calib);
219 temp = st->syscalib_mode[chan->channel];
221 if (temp == AD7192_SYSCALIB_ZERO_SCALE)
222 ret = ad_sd_calibrate(&st->sd, AD7192_MODE_CAL_SYS_ZERO,
225 ret = ad_sd_calibrate(&st->sd, AD7192_MODE_CAL_SYS_FULL,
229 return ret ? ret : len;
232 static const struct iio_enum ad7192_syscalib_mode_enum = {
233 .items = ad7192_syscalib_modes,
234 .num_items = ARRAY_SIZE(ad7192_syscalib_modes),
235 .set = ad7192_set_syscalib_mode,
236 .get = ad7192_get_syscalib_mode
239 static const struct iio_chan_spec_ext_info ad7192_calibsys_ext_info[] = {
241 .name = "sys_calibration",
242 .write = ad7192_write_syscalib,
243 .shared = IIO_SEPARATE,
245 IIO_ENUM("sys_calibration_mode", IIO_SEPARATE,
246 &ad7192_syscalib_mode_enum),
247 IIO_ENUM_AVAILABLE("sys_calibration_mode", &ad7192_syscalib_mode_enum),
251 static struct ad7192_state *ad_sigma_delta_to_ad7192(struct ad_sigma_delta *sd)
253 return container_of(sd, struct ad7192_state, sd);
256 static int ad7192_set_channel(struct ad_sigma_delta *sd, unsigned int channel)
258 struct ad7192_state *st = ad_sigma_delta_to_ad7192(sd);
260 st->conf &= ~AD7192_CONF_CHAN_MASK;
261 st->conf |= AD7192_CONF_CHAN(channel);
263 return ad_sd_write_reg(&st->sd, AD7192_REG_CONF, 3, st->conf);
266 static int ad7192_set_mode(struct ad_sigma_delta *sd,
267 enum ad_sigma_delta_mode mode)
269 struct ad7192_state *st = ad_sigma_delta_to_ad7192(sd);
271 st->mode &= ~AD7192_MODE_SEL_MASK;
272 st->mode |= AD7192_MODE_SEL(mode);
274 return ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, st->mode);
277 static const struct ad_sigma_delta_info ad7192_sigma_delta_info = {
278 .set_channel = ad7192_set_channel,
279 .set_mode = ad7192_set_mode,
280 .has_registers = true,
285 static const struct ad_sd_calib_data ad7192_calib_arr[8] = {
286 {AD7192_MODE_CAL_INT_ZERO, AD7192_CH_AIN1},
287 {AD7192_MODE_CAL_INT_FULL, AD7192_CH_AIN1},
288 {AD7192_MODE_CAL_INT_ZERO, AD7192_CH_AIN2},
289 {AD7192_MODE_CAL_INT_FULL, AD7192_CH_AIN2},
290 {AD7192_MODE_CAL_INT_ZERO, AD7192_CH_AIN3},
291 {AD7192_MODE_CAL_INT_FULL, AD7192_CH_AIN3},
292 {AD7192_MODE_CAL_INT_ZERO, AD7192_CH_AIN4},
293 {AD7192_MODE_CAL_INT_FULL, AD7192_CH_AIN4}
296 static int ad7192_calibrate_all(struct ad7192_state *st)
298 return ad_sd_calibrate_all(&st->sd, ad7192_calib_arr,
299 ARRAY_SIZE(ad7192_calib_arr));
302 static inline bool ad7192_valid_external_frequency(u32 freq)
304 return (freq >= AD7192_EXT_FREQ_MHZ_MIN &&
305 freq <= AD7192_EXT_FREQ_MHZ_MAX);
308 static int ad7192_of_clock_select(struct ad7192_state *st)
310 struct device_node *np = st->sd.spi->dev.of_node;
311 unsigned int clock_sel;
313 clock_sel = AD7192_CLK_INT;
315 /* use internal clock */
316 if (PTR_ERR(st->mclk) == -ENOENT) {
317 if (of_property_read_bool(np, "adi,int-clock-output-enable"))
318 clock_sel = AD7192_CLK_INT_CO;
320 if (of_property_read_bool(np, "adi,clock-xtal"))
321 clock_sel = AD7192_CLK_EXT_MCLK1_2;
323 clock_sel = AD7192_CLK_EXT_MCLK2;
329 static int ad7192_setup(struct ad7192_state *st, struct device_node *np)
331 struct iio_dev *indio_dev = spi_get_drvdata(st->sd.spi);
332 bool rej60_en, refin2_en;
333 bool buf_en, bipolar, burnout_curr_en;
334 unsigned long long scale_uv;
337 /* reset the serial interface */
338 ret = ad_sd_reset(&st->sd, 48);
341 usleep_range(500, 1000); /* Wait for at least 500us */
343 /* write/read test for device presence */
344 ret = ad_sd_read_reg(&st->sd, AD7192_REG_ID, 1, &id);
348 id &= AD7192_ID_MASK;
351 dev_warn(&st->sd.spi->dev, "device ID query failed (0x%X)\n",
354 st->mode = AD7192_MODE_SEL(AD7192_MODE_IDLE) |
355 AD7192_MODE_CLKSRC(st->clock_sel) |
356 AD7192_MODE_RATE(480);
358 st->conf = AD7192_CONF_GAIN(0);
360 rej60_en = of_property_read_bool(np, "adi,rejection-60-Hz-enable");
362 st->mode |= AD7192_MODE_REJ60;
364 refin2_en = of_property_read_bool(np, "adi,refin2-pins-enable");
365 if (refin2_en && st->devid != ID_AD7195)
366 st->conf |= AD7192_CONF_REFSEL;
368 st->conf &= ~AD7192_CONF_CHOP;
369 st->f_order = AD7192_NO_SYNC_FILTER;
371 buf_en = of_property_read_bool(np, "adi,buffer-enable");
373 st->conf |= AD7192_CONF_BUF;
375 bipolar = of_property_read_bool(np, "bipolar");
377 st->conf |= AD7192_CONF_UNIPOLAR;
379 burnout_curr_en = of_property_read_bool(np,
380 "adi,burnout-currents-enable");
381 if (burnout_curr_en && buf_en) {
382 st->conf |= AD7192_CONF_BURN;
383 } else if (burnout_curr_en) {
384 dev_warn(&st->sd.spi->dev,
385 "Can't enable burnout currents: see CHOP or buffer\n");
388 ret = ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, st->mode);
392 ret = ad_sd_write_reg(&st->sd, AD7192_REG_CONF, 3, st->conf);
396 ret = ad7192_calibrate_all(st);
400 /* Populate available ADC input ranges */
401 for (i = 0; i < ARRAY_SIZE(st->scale_avail); i++) {
402 scale_uv = ((u64)st->int_vref_mv * 100000000)
403 >> (indio_dev->channels[0].scan_type.realbits -
404 ((st->conf & AD7192_CONF_UNIPOLAR) ? 0 : 1));
407 st->scale_avail[i][1] = do_div(scale_uv, 100000000) * 10;
408 st->scale_avail[i][0] = scale_uv;
414 static ssize_t ad7192_show_ac_excitation(struct device *dev,
415 struct device_attribute *attr,
418 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
419 struct ad7192_state *st = iio_priv(indio_dev);
421 return sprintf(buf, "%d\n", !!(st->mode & AD7192_MODE_ACX));
424 static ssize_t ad7192_show_bridge_switch(struct device *dev,
425 struct device_attribute *attr,
428 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
429 struct ad7192_state *st = iio_priv(indio_dev);
431 return sprintf(buf, "%d\n", !!(st->gpocon & AD7192_GPOCON_BPDSW));
434 static ssize_t ad7192_set(struct device *dev,
435 struct device_attribute *attr,
439 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
440 struct ad7192_state *st = iio_priv(indio_dev);
441 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
445 ret = strtobool(buf, &val);
449 ret = iio_device_claim_direct_mode(indio_dev);
453 switch ((u32)this_attr->address) {
454 case AD7192_REG_GPOCON:
456 st->gpocon |= AD7192_GPOCON_BPDSW;
458 st->gpocon &= ~AD7192_GPOCON_BPDSW;
460 ad_sd_write_reg(&st->sd, AD7192_REG_GPOCON, 1, st->gpocon);
462 case AD7192_REG_MODE:
464 st->mode |= AD7192_MODE_ACX;
466 st->mode &= ~AD7192_MODE_ACX;
468 ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, st->mode);
474 iio_device_release_direct_mode(indio_dev);
476 return ret ? ret : len;
479 static void ad7192_get_available_filter_freq(struct ad7192_state *st,
484 /* Formulas for filter at page 25 of the datasheet */
485 fadc = DIV_ROUND_CLOSEST(st->fclk,
486 AD7192_SYNC4_FILTER * AD7192_MODE_RATE(st->mode));
487 freq[0] = DIV_ROUND_CLOSEST(fadc * 240, 1024);
489 fadc = DIV_ROUND_CLOSEST(st->fclk,
490 AD7192_SYNC3_FILTER * AD7192_MODE_RATE(st->mode));
491 freq[1] = DIV_ROUND_CLOSEST(fadc * 240, 1024);
493 fadc = DIV_ROUND_CLOSEST(st->fclk, AD7192_MODE_RATE(st->mode));
494 freq[2] = DIV_ROUND_CLOSEST(fadc * 230, 1024);
495 freq[3] = DIV_ROUND_CLOSEST(fadc * 272, 1024);
498 static ssize_t ad7192_show_filter_avail(struct device *dev,
499 struct device_attribute *attr,
502 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
503 struct ad7192_state *st = iio_priv(indio_dev);
504 unsigned int freq_avail[4], i;
507 ad7192_get_available_filter_freq(st, freq_avail);
509 for (i = 0; i < ARRAY_SIZE(freq_avail); i++)
510 len += scnprintf(buf + len, PAGE_SIZE - len,
511 "%d.%d ", freq_avail[i] / 1000,
512 freq_avail[i] % 1000);
519 static IIO_DEVICE_ATTR(filter_low_pass_3db_frequency_available,
520 0444, ad7192_show_filter_avail, NULL, 0);
522 static IIO_DEVICE_ATTR(bridge_switch_en, 0644,
523 ad7192_show_bridge_switch, ad7192_set,
526 static IIO_DEVICE_ATTR(ac_excitation_en, 0644,
527 ad7192_show_ac_excitation, ad7192_set,
530 static struct attribute *ad7192_attributes[] = {
531 &iio_dev_attr_filter_low_pass_3db_frequency_available.dev_attr.attr,
532 &iio_dev_attr_bridge_switch_en.dev_attr.attr,
533 &iio_dev_attr_ac_excitation_en.dev_attr.attr,
537 static const struct attribute_group ad7192_attribute_group = {
538 .attrs = ad7192_attributes,
541 static struct attribute *ad7195_attributes[] = {
542 &iio_dev_attr_filter_low_pass_3db_frequency_available.dev_attr.attr,
543 &iio_dev_attr_bridge_switch_en.dev_attr.attr,
547 static const struct attribute_group ad7195_attribute_group = {
548 .attrs = ad7195_attributes,
551 static unsigned int ad7192_get_temp_scale(bool unipolar)
553 return unipolar ? 2815 * 2 : 2815;
556 static int ad7192_set_3db_filter_freq(struct ad7192_state *st,
559 int freq_avail[4], i, ret, freq;
560 unsigned int diff_new, diff_old;
564 freq = val * 1000 + val2;
566 ad7192_get_available_filter_freq(st, freq_avail);
568 for (i = 0; i < ARRAY_SIZE(freq_avail); i++) {
569 diff_new = abs(freq - freq_avail[i]);
570 if (diff_new < diff_old) {
578 st->f_order = AD7192_SYNC4_FILTER;
579 st->mode &= ~AD7192_MODE_SINC3;
581 st->conf |= AD7192_CONF_CHOP;
584 st->f_order = AD7192_SYNC3_FILTER;
585 st->mode |= AD7192_MODE_SINC3;
587 st->conf |= AD7192_CONF_CHOP;
590 st->f_order = AD7192_NO_SYNC_FILTER;
591 st->mode &= ~AD7192_MODE_SINC3;
593 st->conf &= ~AD7192_CONF_CHOP;
596 st->f_order = AD7192_NO_SYNC_FILTER;
597 st->mode |= AD7192_MODE_SINC3;
599 st->conf &= ~AD7192_CONF_CHOP;
603 ret = ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, st->mode);
607 return ad_sd_write_reg(&st->sd, AD7192_REG_CONF, 3, st->conf);
610 static int ad7192_get_3db_filter_freq(struct ad7192_state *st)
614 fadc = DIV_ROUND_CLOSEST(st->fclk,
615 st->f_order * AD7192_MODE_RATE(st->mode));
617 if (st->conf & AD7192_CONF_CHOP)
618 return DIV_ROUND_CLOSEST(fadc * 240, 1024);
619 if (st->mode & AD7192_MODE_SINC3)
620 return DIV_ROUND_CLOSEST(fadc * 272, 1024);
622 return DIV_ROUND_CLOSEST(fadc * 230, 1024);
625 static int ad7192_read_raw(struct iio_dev *indio_dev,
626 struct iio_chan_spec const *chan,
631 struct ad7192_state *st = iio_priv(indio_dev);
632 bool unipolar = !!(st->conf & AD7192_CONF_UNIPOLAR);
635 case IIO_CHAN_INFO_RAW:
636 return ad_sigma_delta_single_conversion(indio_dev, chan, val);
637 case IIO_CHAN_INFO_SCALE:
638 switch (chan->type) {
640 mutex_lock(&st->lock);
641 *val = st->scale_avail[AD7192_CONF_GAIN(st->conf)][0];
642 *val2 = st->scale_avail[AD7192_CONF_GAIN(st->conf)][1];
643 mutex_unlock(&st->lock);
644 return IIO_VAL_INT_PLUS_NANO;
647 *val2 = 1000000000 / ad7192_get_temp_scale(unipolar);
648 return IIO_VAL_INT_PLUS_NANO;
652 case IIO_CHAN_INFO_OFFSET:
654 *val = -(1 << (chan->scan_type.realbits - 1));
657 /* Kelvin to Celsius */
658 if (chan->type == IIO_TEMP)
659 *val -= 273 * ad7192_get_temp_scale(unipolar);
661 case IIO_CHAN_INFO_SAMP_FREQ:
663 (st->f_order * 1024 * AD7192_MODE_RATE(st->mode));
665 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
666 *val = ad7192_get_3db_filter_freq(st);
668 return IIO_VAL_FRACTIONAL;
674 static int ad7192_write_raw(struct iio_dev *indio_dev,
675 struct iio_chan_spec const *chan,
680 struct ad7192_state *st = iio_priv(indio_dev);
684 ret = iio_device_claim_direct_mode(indio_dev);
689 case IIO_CHAN_INFO_SCALE:
691 mutex_lock(&st->lock);
692 for (i = 0; i < ARRAY_SIZE(st->scale_avail); i++)
693 if (val2 == st->scale_avail[i][1]) {
696 st->conf &= ~AD7192_CONF_GAIN(-1);
697 st->conf |= AD7192_CONF_GAIN(i);
700 ad_sd_write_reg(&st->sd, AD7192_REG_CONF,
702 ad7192_calibrate_all(st);
705 mutex_unlock(&st->lock);
707 case IIO_CHAN_INFO_SAMP_FREQ:
713 div = st->fclk / (val * st->f_order * 1024);
714 if (div < 1 || div > 1023) {
719 st->mode &= ~AD7192_MODE_RATE(-1);
720 st->mode |= AD7192_MODE_RATE(div);
721 ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, st->mode);
723 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
724 ret = ad7192_set_3db_filter_freq(st, val, val2 / 1000);
730 iio_device_release_direct_mode(indio_dev);
735 static int ad7192_write_raw_get_fmt(struct iio_dev *indio_dev,
736 struct iio_chan_spec const *chan,
740 case IIO_CHAN_INFO_SCALE:
741 return IIO_VAL_INT_PLUS_NANO;
742 case IIO_CHAN_INFO_SAMP_FREQ:
744 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
745 return IIO_VAL_INT_PLUS_MICRO;
751 static int ad7192_read_avail(struct iio_dev *indio_dev,
752 struct iio_chan_spec const *chan,
753 const int **vals, int *type, int *length,
756 struct ad7192_state *st = iio_priv(indio_dev);
759 case IIO_CHAN_INFO_SCALE:
760 *vals = (int *)st->scale_avail;
761 *type = IIO_VAL_INT_PLUS_NANO;
762 /* Values are stored in a 2D matrix */
763 *length = ARRAY_SIZE(st->scale_avail) * 2;
765 return IIO_AVAIL_LIST;
771 static const struct iio_info ad7192_info = {
772 .read_raw = ad7192_read_raw,
773 .write_raw = ad7192_write_raw,
774 .write_raw_get_fmt = ad7192_write_raw_get_fmt,
775 .read_avail = ad7192_read_avail,
776 .attrs = &ad7192_attribute_group,
777 .validate_trigger = ad_sd_validate_trigger,
780 static const struct iio_info ad7195_info = {
781 .read_raw = ad7192_read_raw,
782 .write_raw = ad7192_write_raw,
783 .write_raw_get_fmt = ad7192_write_raw_get_fmt,
784 .read_avail = ad7192_read_avail,
785 .attrs = &ad7195_attribute_group,
786 .validate_trigger = ad_sd_validate_trigger,
789 static const struct iio_chan_spec ad7192_channels[] = {
790 AD_SD_DIFF_CHANNEL(0, 1, 2, AD7192_CH_AIN1P_AIN2M, 24, 32, 0),
791 AD_SD_DIFF_CHANNEL(1, 3, 4, AD7192_CH_AIN3P_AIN4M, 24, 32, 0),
792 AD_SD_TEMP_CHANNEL(2, AD7192_CH_TEMP, 24, 32, 0),
793 AD_SD_SHORTED_CHANNEL(3, 2, AD7192_CH_AIN2P_AIN2M, 24, 32, 0),
794 AD_SD_CHANNEL(4, 1, AD7192_CH_AIN1, 24, 32, 0),
795 AD_SD_CHANNEL(5, 2, AD7192_CH_AIN2, 24, 32, 0),
796 AD_SD_CHANNEL(6, 3, AD7192_CH_AIN3, 24, 32, 0),
797 AD_SD_CHANNEL(7, 4, AD7192_CH_AIN4, 24, 32, 0),
798 IIO_CHAN_SOFT_TIMESTAMP(8),
801 static const struct iio_chan_spec ad7193_channels[] = {
802 AD_SD_DIFF_CHANNEL(0, 1, 2, AD7193_CH_AIN1P_AIN2M, 24, 32, 0),
803 AD_SD_DIFF_CHANNEL(1, 3, 4, AD7193_CH_AIN3P_AIN4M, 24, 32, 0),
804 AD_SD_DIFF_CHANNEL(2, 5, 6, AD7193_CH_AIN5P_AIN6M, 24, 32, 0),
805 AD_SD_DIFF_CHANNEL(3, 7, 8, AD7193_CH_AIN7P_AIN8M, 24, 32, 0),
806 AD_SD_TEMP_CHANNEL(4, AD7193_CH_TEMP, 24, 32, 0),
807 AD_SD_SHORTED_CHANNEL(5, 2, AD7193_CH_AIN2P_AIN2M, 24, 32, 0),
808 AD_SD_CHANNEL(6, 1, AD7193_CH_AIN1, 24, 32, 0),
809 AD_SD_CHANNEL(7, 2, AD7193_CH_AIN2, 24, 32, 0),
810 AD_SD_CHANNEL(8, 3, AD7193_CH_AIN3, 24, 32, 0),
811 AD_SD_CHANNEL(9, 4, AD7193_CH_AIN4, 24, 32, 0),
812 AD_SD_CHANNEL(10, 5, AD7193_CH_AIN5, 24, 32, 0),
813 AD_SD_CHANNEL(11, 6, AD7193_CH_AIN6, 24, 32, 0),
814 AD_SD_CHANNEL(12, 7, AD7193_CH_AIN7, 24, 32, 0),
815 AD_SD_CHANNEL(13, 8, AD7193_CH_AIN8, 24, 32, 0),
816 IIO_CHAN_SOFT_TIMESTAMP(14),
819 static int ad7192_channels_config(struct iio_dev *indio_dev)
821 struct ad7192_state *st = iio_priv(indio_dev);
822 const struct iio_chan_spec *channels;
823 struct iio_chan_spec *chan;
828 channels = ad7193_channels;
829 indio_dev->num_channels = ARRAY_SIZE(ad7193_channels);
832 channels = ad7192_channels;
833 indio_dev->num_channels = ARRAY_SIZE(ad7192_channels);
837 chan = devm_kcalloc(indio_dev->dev.parent, indio_dev->num_channels,
838 sizeof(*chan), GFP_KERNEL);
842 indio_dev->channels = chan;
844 for (i = 0; i < indio_dev->num_channels; i++) {
846 chan->info_mask_shared_by_all |=
847 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY);
848 if (chan->type != IIO_TEMP) {
849 chan->info_mask_shared_by_type_available |=
850 BIT(IIO_CHAN_INFO_SCALE);
851 chan->ext_info = ad7192_calibsys_ext_info;
859 static int ad7192_probe(struct spi_device *spi)
861 struct ad7192_state *st;
862 struct iio_dev *indio_dev;
863 int ret, voltage_uv = 0;
866 dev_err(&spi->dev, "no IRQ?\n");
870 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
874 st = iio_priv(indio_dev);
876 mutex_init(&st->lock);
878 st->avdd = devm_regulator_get(&spi->dev, "avdd");
879 if (IS_ERR(st->avdd))
880 return PTR_ERR(st->avdd);
882 ret = regulator_enable(st->avdd);
884 dev_err(&spi->dev, "Failed to enable specified AVdd supply\n");
888 st->dvdd = devm_regulator_get(&spi->dev, "dvdd");
889 if (IS_ERR(st->dvdd)) {
890 ret = PTR_ERR(st->dvdd);
891 goto error_disable_avdd;
894 ret = regulator_enable(st->dvdd);
896 dev_err(&spi->dev, "Failed to enable specified DVdd supply\n");
897 goto error_disable_avdd;
900 voltage_uv = regulator_get_voltage(st->avdd);
903 st->int_vref_mv = voltage_uv / 1000;
905 dev_err(&spi->dev, "Device tree error, reference voltage undefined\n");
907 spi_set_drvdata(spi, indio_dev);
908 st->devid = spi_get_device_id(spi)->driver_data;
909 indio_dev->dev.parent = &spi->dev;
910 indio_dev->name = spi_get_device_id(spi)->name;
911 indio_dev->modes = INDIO_DIRECT_MODE;
913 ret = ad7192_channels_config(indio_dev);
915 goto error_disable_dvdd;
917 if (st->devid == ID_AD7195)
918 indio_dev->info = &ad7195_info;
920 indio_dev->info = &ad7192_info;
922 ad_sd_init(&st->sd, indio_dev, spi, &ad7192_sigma_delta_info);
924 ret = ad_sd_setup_buffer_and_trigger(indio_dev);
926 goto error_disable_dvdd;
928 st->fclk = AD7192_INT_FREQ_MHZ;
930 st->mclk = devm_clk_get(&st->sd.spi->dev, "mclk");
931 if (IS_ERR(st->mclk) && PTR_ERR(st->mclk) != -ENOENT) {
932 ret = PTR_ERR(st->mclk);
933 goto error_remove_trigger;
936 st->clock_sel = ad7192_of_clock_select(st);
938 if (st->clock_sel == AD7192_CLK_EXT_MCLK1_2 ||
939 st->clock_sel == AD7192_CLK_EXT_MCLK2) {
940 ret = clk_prepare_enable(st->mclk);
942 goto error_remove_trigger;
944 st->fclk = clk_get_rate(st->mclk);
945 if (!ad7192_valid_external_frequency(st->fclk)) {
948 "External clock frequency out of bounds\n");
949 goto error_disable_clk;
953 ret = ad7192_setup(st, spi->dev.of_node);
955 goto error_disable_clk;
957 ret = iio_device_register(indio_dev);
959 goto error_disable_clk;
963 clk_disable_unprepare(st->mclk);
964 error_remove_trigger:
965 ad_sd_cleanup_buffer_and_trigger(indio_dev);
967 regulator_disable(st->dvdd);
969 regulator_disable(st->avdd);
974 static int ad7192_remove(struct spi_device *spi)
976 struct iio_dev *indio_dev = spi_get_drvdata(spi);
977 struct ad7192_state *st = iio_priv(indio_dev);
979 iio_device_unregister(indio_dev);
980 clk_disable_unprepare(st->mclk);
981 ad_sd_cleanup_buffer_and_trigger(indio_dev);
983 regulator_disable(st->dvdd);
984 regulator_disable(st->avdd);
989 static const struct spi_device_id ad7192_id[] = {
990 {"ad7190", ID_AD7190},
991 {"ad7192", ID_AD7192},
992 {"ad7193", ID_AD7193},
993 {"ad7195", ID_AD7195},
997 MODULE_DEVICE_TABLE(spi, ad7192_id);
999 static const struct of_device_id ad7192_of_match[] = {
1000 { .compatible = "adi,ad7190" },
1001 { .compatible = "adi,ad7192" },
1002 { .compatible = "adi,ad7193" },
1003 { .compatible = "adi,ad7195" },
1007 MODULE_DEVICE_TABLE(of, ad7192_of_match);
1009 static struct spi_driver ad7192_driver = {
1012 .of_match_table = ad7192_of_match,
1014 .probe = ad7192_probe,
1015 .remove = ad7192_remove,
1016 .id_table = ad7192_id,
1018 module_spi_driver(ad7192_driver);
1021 MODULE_DESCRIPTION("Analog Devices AD7190, AD7192, AD7193, AD7195 ADC");
1022 MODULE_LICENSE("GPL v2");