blob: cc990205f306149b20795f9392454ad12fc1fb24 [file] [log] [blame]
Greg Kroah-Hartman2506abe2019-04-02 12:31:55 +02001// SPDX-License-Identifier: GPL-2.0
Michael Hennerich7a27b042011-08-17 17:29:34 +02002/*
Lars-Peter Clausend69051b2016-02-08 11:45:04 +01003 * AD7190 AD7192 AD7193 AD7195 SPI ADC driver
Michael Hennerich7a27b042011-08-17 17:29:34 +02004 *
Lars-Peter Clausend69051b2016-02-08 11:45:04 +01005 * Copyright 2011-2015 Analog Devices Inc.
Michael Hennerich7a27b042011-08-17 17:29:34 +02006 */
7
8#include <linux/interrupt.h>
Mircea Caprioru3ad7a932019-03-15 13:29:02 +02009#include <linux/clk.h>
Michael Hennerich7a27b042011-08-17 17:29:34 +020010#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>
Alexandru Tachici66614ab2020-02-12 18:17:19 +020019#include <linux/of_device.h>
Michael Hennerich7a27b042011-08-17 17:29:34 +020020
Jonathan Cameron06458e22012-04-25 15:54:58 +010021#include <linux/iio/iio.h>
22#include <linux/iio/sysfs.h>
23#include <linux/iio/buffer.h>
Jonathan Cameron06458e22012-04-25 15:54:58 +010024#include <linux/iio/trigger.h>
25#include <linux/iio/trigger_consumer.h>
Lars-Peter Clausena6482322012-06-18 18:33:50 +020026#include <linux/iio/triggered_buffer.h>
Lars-Peter Clausen3f7c3302012-08-10 17:36:00 +010027#include <linux/iio/adc/ad_sigma_delta.h>
Michael Hennerich7a27b042011-08-17 17:29:34 +020028
Michael Hennerich7a27b042011-08-17 17:29:34 +020029/* Registers */
30#define AD7192_REG_COMM 0 /* Communications Register (WO, 8-bit) */
31#define AD7192_REG_STAT 0 /* Status Register (RO, 8-bit) */
32#define AD7192_REG_MODE 1 /* Mode Register (RW, 24-bit */
33#define AD7192_REG_CONF 2 /* Configuration Register (RW, 24-bit) */
34#define AD7192_REG_DATA 3 /* Data Register (RO, 24/32-bit) */
35#define AD7192_REG_ID 4 /* ID Register (RO, 8-bit) */
36#define AD7192_REG_GPOCON 5 /* GPOCON Register (RO, 8-bit) */
Alison Schofielda6634f82016-03-21 15:39:53 -070037#define AD7192_REG_OFFSET 6 /* Offset Register (RW, 16-bit */
38 /* (AD7792)/24-bit (AD7192)) */
39#define AD7192_REG_FULLSALE 7 /* Full-Scale Register */
40 /* (RW, 16-bit (AD7792)/24-bit (AD7192)) */
Michael Hennerich7a27b042011-08-17 17:29:34 +020041
42/* Communications Register Bit Designations (AD7192_REG_COMM) */
Haneen Mohammed418880f2015-03-26 02:23:29 +030043#define AD7192_COMM_WEN BIT(7) /* Write Enable */
44#define AD7192_COMM_WRITE 0 /* Write Operation */
45#define AD7192_COMM_READ BIT(6) /* Read Operation */
Michael Hennerich7a27b042011-08-17 17:29:34 +020046#define AD7192_COMM_ADDR(x) (((x) & 0x7) << 3) /* Register Address */
Haneen Mohammed418880f2015-03-26 02:23:29 +030047#define AD7192_COMM_CREAD BIT(2) /* Continuous Read of Data Register */
Michael Hennerich7a27b042011-08-17 17:29:34 +020048
49/* Status Register Bit Designations (AD7192_REG_STAT) */
Haneen Mohammed418880f2015-03-26 02:23:29 +030050#define AD7192_STAT_RDY BIT(7) /* Ready */
51#define AD7192_STAT_ERR BIT(6) /* Error (Overrange, Underrange) */
52#define AD7192_STAT_NOREF BIT(5) /* Error no external reference */
53#define AD7192_STAT_PARITY BIT(4) /* Parity */
54#define AD7192_STAT_CH3 BIT(2) /* Channel 3 */
55#define AD7192_STAT_CH2 BIT(1) /* Channel 2 */
56#define AD7192_STAT_CH1 BIT(0) /* Channel 1 */
Michael Hennerich7a27b042011-08-17 17:29:34 +020057
58/* Mode Register Bit Designations (AD7192_REG_MODE) */
59#define AD7192_MODE_SEL(x) (((x) & 0x7) << 21) /* Operation Mode Select */
Lars-Peter Clausen3f7c3302012-08-10 17:36:00 +010060#define AD7192_MODE_SEL_MASK (0x7 << 21) /* Operation Mode Select Mask */
Haneen Mohammed418880f2015-03-26 02:23:29 +030061#define AD7192_MODE_DAT_STA BIT(20) /* Status Register transmission */
Michael Hennerich7a27b042011-08-17 17:29:34 +020062#define AD7192_MODE_CLKSRC(x) (((x) & 0x3) << 18) /* Clock Source Select */
Haneen Mohammed418880f2015-03-26 02:23:29 +030063#define AD7192_MODE_SINC3 BIT(15) /* SINC3 Filter Select */
64#define AD7192_MODE_ACX BIT(14) /* AC excitation enable(AD7195 only)*/
65#define AD7192_MODE_ENPAR BIT(13) /* Parity Enable */
66#define AD7192_MODE_CLKDIV BIT(12) /* Clock divide by 2 (AD7190/2 only)*/
67#define AD7192_MODE_SCYCLE BIT(11) /* Single cycle conversion */
68#define AD7192_MODE_REJ60 BIT(10) /* 50/60Hz notch filter */
Michael Hennerich7a27b042011-08-17 17:29:34 +020069#define AD7192_MODE_RATE(x) ((x) & 0x3FF) /* Filter Update Rate Select */
70
71/* Mode Register: AD7192_MODE_SEL options */
72#define AD7192_MODE_CONT 0 /* Continuous Conversion Mode */
73#define AD7192_MODE_SINGLE 1 /* Single Conversion Mode */
74#define AD7192_MODE_IDLE 2 /* Idle Mode */
75#define AD7192_MODE_PWRDN 3 /* Power-Down Mode */
76#define AD7192_MODE_CAL_INT_ZERO 4 /* Internal Zero-Scale Calibration */
77#define AD7192_MODE_CAL_INT_FULL 5 /* Internal Full-Scale Calibration */
78#define AD7192_MODE_CAL_SYS_ZERO 6 /* System Zero-Scale Calibration */
79#define AD7192_MODE_CAL_SYS_FULL 7 /* System Full-Scale Calibration */
80
81/* Mode Register: AD7192_MODE_CLKSRC options */
Alison Schofielda6634f82016-03-21 15:39:53 -070082#define AD7192_CLK_EXT_MCLK1_2 0 /* External 4.92 MHz Clock connected*/
83 /* from MCLK1 to MCLK2 */
Michael Hennerich7a27b042011-08-17 17:29:34 +020084#define AD7192_CLK_EXT_MCLK2 1 /* External Clock applied to MCLK2 */
Alison Schofielda6634f82016-03-21 15:39:53 -070085#define AD7192_CLK_INT 2 /* Internal 4.92 MHz Clock not */
86 /* available at the MCLK2 pin */
87#define AD7192_CLK_INT_CO 3 /* Internal 4.92 MHz Clock available*/
88 /* at the MCLK2 pin */
Michael Hennerich7a27b042011-08-17 17:29:34 +020089
Michael Hennerich7a27b042011-08-17 17:29:34 +020090/* Configuration Register Bit Designations (AD7192_REG_CONF) */
91
Haneen Mohammed418880f2015-03-26 02:23:29 +030092#define AD7192_CONF_CHOP BIT(23) /* CHOP enable */
93#define AD7192_CONF_REFSEL BIT(20) /* REFIN1/REFIN2 Reference Select */
Lars-Peter Clausend69051b2016-02-08 11:45:04 +010094#define AD7192_CONF_CHAN(x) ((x) << 8) /* Channel select */
95#define AD7192_CONF_CHAN_MASK (0x7FF << 8) /* Channel select mask */
Haneen Mohammed418880f2015-03-26 02:23:29 +030096#define AD7192_CONF_BURN BIT(7) /* Burnout current enable */
97#define AD7192_CONF_REFDET BIT(6) /* Reference detect enable */
98#define AD7192_CONF_BUF BIT(4) /* Buffered Mode Enable */
99#define AD7192_CONF_UNIPOLAR BIT(3) /* Unipolar/Bipolar Enable */
Michael Hennerich7a27b042011-08-17 17:29:34 +0200100#define AD7192_CONF_GAIN(x) ((x) & 0x7) /* Gain Select */
101
Lars-Peter Clausend69051b2016-02-08 11:45:04 +0100102#define AD7192_CH_AIN1P_AIN2M BIT(0) /* AIN1(+) - AIN2(-) */
103#define AD7192_CH_AIN3P_AIN4M BIT(1) /* AIN3(+) - AIN4(-) */
104#define AD7192_CH_TEMP BIT(2) /* Temp Sensor */
105#define AD7192_CH_AIN2P_AIN2M BIT(3) /* AIN2(+) - AIN2(-) */
106#define AD7192_CH_AIN1 BIT(4) /* AIN1 - AINCOM */
107#define AD7192_CH_AIN2 BIT(5) /* AIN2 - AINCOM */
108#define AD7192_CH_AIN3 BIT(6) /* AIN3 - AINCOM */
109#define AD7192_CH_AIN4 BIT(7) /* AIN4 - AINCOM */
110
Mircea Caprioru7ce0f212019-02-20 13:08:20 +0200111#define AD7193_CH_AIN1P_AIN2M 0x001 /* AIN1(+) - AIN2(-) */
112#define AD7193_CH_AIN3P_AIN4M 0x002 /* AIN3(+) - AIN4(-) */
113#define AD7193_CH_AIN5P_AIN6M 0x004 /* AIN5(+) - AIN6(-) */
114#define AD7193_CH_AIN7P_AIN8M 0x008 /* AIN7(+) - AIN8(-) */
Lars-Peter Clausend69051b2016-02-08 11:45:04 +0100115#define AD7193_CH_TEMP 0x100 /* Temp senseor */
116#define AD7193_CH_AIN2P_AIN2M 0x200 /* AIN2(+) - AIN2(-) */
117#define AD7193_CH_AIN1 0x401 /* AIN1 - AINCOM */
118#define AD7193_CH_AIN2 0x402 /* AIN2 - AINCOM */
119#define AD7193_CH_AIN3 0x404 /* AIN3 - AINCOM */
120#define AD7193_CH_AIN4 0x408 /* AIN4 - AINCOM */
121#define AD7193_CH_AIN5 0x410 /* AIN5 - AINCOM */
122#define AD7193_CH_AIN6 0x420 /* AIN6 - AINCOM */
123#define AD7193_CH_AIN7 0x440 /* AIN7 - AINCOM */
124#define AD7193_CH_AIN8 0x480 /* AIN7 - AINCOM */
125#define AD7193_CH_AINCOM 0x600 /* AINCOM - AINCOM */
Michael Hennerich7a27b042011-08-17 17:29:34 +0200126
127/* ID Register Bit Designations (AD7192_REG_ID) */
Alexandru Ardelean8f2273b2020-04-15 08:58:03 +0300128#define CHIPID_AD7190 0x4
129#define CHIPID_AD7192 0x0
130#define CHIPID_AD7193 0x2
131#define CHIPID_AD7195 0x6
Michael Hennerich7a27b042011-08-17 17:29:34 +0200132#define AD7192_ID_MASK 0x0F
133
134/* GPOCON Register Bit Designations (AD7192_REG_GPOCON) */
Haneen Mohammed418880f2015-03-26 02:23:29 +0300135#define AD7192_GPOCON_BPDSW BIT(6) /* Bridge power-down switch enable */
136#define AD7192_GPOCON_GP32EN BIT(5) /* Digital Output P3 and P2 enable */
137#define AD7192_GPOCON_GP10EN BIT(4) /* Digital Output P1 and P0 enable */
138#define AD7192_GPOCON_P3DAT BIT(3) /* P3 state */
139#define AD7192_GPOCON_P2DAT BIT(2) /* P2 state */
140#define AD7192_GPOCON_P1DAT BIT(1) /* P1 state */
141#define AD7192_GPOCON_P0DAT BIT(0) /* P0 state */
Michael Hennerich7a27b042011-08-17 17:29:34 +0200142
Alexandru Ardeleane31b6172018-01-22 11:53:12 +0200143#define AD7192_EXT_FREQ_MHZ_MIN 2457600
144#define AD7192_EXT_FREQ_MHZ_MAX 5120000
Ioana Ciornei5f7e2802015-10-14 21:14:19 +0300145#define AD7192_INT_FREQ_MHZ 4915200
Michael Hennerich7a27b042011-08-17 17:29:34 +0200146
Mircea Caprioru77f6a232019-08-14 10:31:47 +0300147#define AD7192_NO_SYNC_FILTER 1
148#define AD7192_SYNC3_FILTER 3
149#define AD7192_SYNC4_FILTER 4
150
Michael Hennerich7a27b042011-08-17 17:29:34 +0200151/* NOTE:
152 * The AD7190/2/5 features a dual use data out ready DOUT/RDY output.
153 * In order to avoid contentions on the SPI bus, it's therefore necessary
154 * to use spi bus locking.
155 *
156 * The DOUT/RDY output must also be wired to an interrupt capable GPIO.
157 */
158
Mircea Caprioru42776c12019-09-02 16:08:30 +0300159enum {
Deepak R Varma4230c862020-03-23 01:24:13 +0530160 AD7192_SYSCALIB_ZERO_SCALE,
161 AD7192_SYSCALIB_FULL_SCALE,
Mircea Caprioru42776c12019-09-02 16:08:30 +0300162};
163
Alexandru Ardelean8f2273b2020-04-15 08:58:03 +0300164enum {
165 ID_AD7190,
166 ID_AD7192,
167 ID_AD7193,
168 ID_AD7195,
169};
170
171struct ad7192_chip_info {
172 unsigned int chip_id;
173 const char *name;
174};
175
Michael Hennerich7a27b042011-08-17 17:29:34 +0200176struct ad7192_state {
Alexandru Ardelean8f2273b2020-04-15 08:58:03 +0300177 const struct ad7192_chip_info *chip_info;
Eva Rachel Retuya9d154812016-11-01 01:04:33 +0800178 struct regulator *avdd;
Eva Rachel Retuyacb8bb162016-11-01 01:04:32 +0800179 struct regulator *dvdd;
Mircea Caprioru3ad7a932019-03-15 13:29:02 +0200180 struct clk *mclk;
Michael Hennerich7a27b042011-08-17 17:29:34 +0200181 u16 int_vref_mv;
Mircea Caprioru3ad7a932019-03-15 13:29:02 +0200182 u32 fclk;
Michael Hennerich7a27b042011-08-17 17:29:34 +0200183 u32 f_order;
184 u32 mode;
185 u32 conf;
186 u32 scale_avail[8][2];
Michael Hennerich7a27b042011-08-17 17:29:34 +0200187 u8 gpocon;
Mircea Caprioru3ad7a932019-03-15 13:29:02 +0200188 u8 clock_sel;
Aastha Gupta2db82e32017-09-27 12:01:59 +0530189 struct mutex lock; /* protect sensor state */
Mircea Caprioru42776c12019-09-02 16:08:30 +0300190 u8 syscalib_mode[8];
Lars-Peter Clausen3f7c3302012-08-10 17:36:00 +0100191
192 struct ad_sigma_delta sd;
Michael Hennerich7a27b042011-08-17 17:29:34 +0200193};
194
Mircea Caprioru42776c12019-09-02 16:08:30 +0300195static const char * const ad7192_syscalib_modes[] = {
196 [AD7192_SYSCALIB_ZERO_SCALE] = "zero_scale",
197 [AD7192_SYSCALIB_FULL_SCALE] = "full_scale",
198};
199
200static int ad7192_set_syscalib_mode(struct iio_dev *indio_dev,
201 const struct iio_chan_spec *chan,
202 unsigned int mode)
203{
204 struct ad7192_state *st = iio_priv(indio_dev);
205
206 st->syscalib_mode[chan->channel] = mode;
207
208 return 0;
209}
210
211static int ad7192_get_syscalib_mode(struct iio_dev *indio_dev,
212 const struct iio_chan_spec *chan)
213{
214 struct ad7192_state *st = iio_priv(indio_dev);
215
216 return st->syscalib_mode[chan->channel];
217}
218
219static ssize_t ad7192_write_syscalib(struct iio_dev *indio_dev,
220 uintptr_t private,
221 const struct iio_chan_spec *chan,
222 const char *buf, size_t len)
223{
224 struct ad7192_state *st = iio_priv(indio_dev);
225 bool sys_calib;
226 int ret, temp;
227
228 ret = strtobool(buf, &sys_calib);
229 if (ret)
230 return ret;
231
232 temp = st->syscalib_mode[chan->channel];
233 if (sys_calib) {
234 if (temp == AD7192_SYSCALIB_ZERO_SCALE)
235 ret = ad_sd_calibrate(&st->sd, AD7192_MODE_CAL_SYS_ZERO,
236 chan->address);
237 else
238 ret = ad_sd_calibrate(&st->sd, AD7192_MODE_CAL_SYS_FULL,
239 chan->address);
240 }
241
242 return ret ? ret : len;
243}
244
245static const struct iio_enum ad7192_syscalib_mode_enum = {
246 .items = ad7192_syscalib_modes,
247 .num_items = ARRAY_SIZE(ad7192_syscalib_modes),
248 .set = ad7192_set_syscalib_mode,
249 .get = ad7192_get_syscalib_mode
250};
251
252static const struct iio_chan_spec_ext_info ad7192_calibsys_ext_info[] = {
253 {
254 .name = "sys_calibration",
255 .write = ad7192_write_syscalib,
256 .shared = IIO_SEPARATE,
257 },
258 IIO_ENUM("sys_calibration_mode", IIO_SEPARATE,
259 &ad7192_syscalib_mode_enum),
Antoniu Miclausffc7c512021-11-19 10:56:27 +0200260 IIO_ENUM_AVAILABLE("sys_calibration_mode", IIO_SHARED_BY_TYPE,
261 &ad7192_syscalib_mode_enum),
Mircea Caprioru42776c12019-09-02 16:08:30 +0300262 {}
263};
264
Lars-Peter Clausen3f7c3302012-08-10 17:36:00 +0100265static struct ad7192_state *ad_sigma_delta_to_ad7192(struct ad_sigma_delta *sd)
Michael Hennerich7a27b042011-08-17 17:29:34 +0200266{
Lars-Peter Clausen3f7c3302012-08-10 17:36:00 +0100267 return container_of(sd, struct ad7192_state, sd);
Michael Hennerich7a27b042011-08-17 17:29:34 +0200268}
269
Lars-Peter Clausen3f7c3302012-08-10 17:36:00 +0100270static int ad7192_set_channel(struct ad_sigma_delta *sd, unsigned int channel)
Michael Hennerich7a27b042011-08-17 17:29:34 +0200271{
Lars-Peter Clausen3f7c3302012-08-10 17:36:00 +0100272 struct ad7192_state *st = ad_sigma_delta_to_ad7192(sd);
273
274 st->conf &= ~AD7192_CONF_CHAN_MASK;
275 st->conf |= AD7192_CONF_CHAN(channel);
276
277 return ad_sd_write_reg(&st->sd, AD7192_REG_CONF, 3, st->conf);
Michael Hennerich7a27b042011-08-17 17:29:34 +0200278}
279
Lars-Peter Clausen3f7c3302012-08-10 17:36:00 +0100280static int ad7192_set_mode(struct ad_sigma_delta *sd,
281 enum ad_sigma_delta_mode mode)
Michael Hennerich7a27b042011-08-17 17:29:34 +0200282{
Lars-Peter Clausen3f7c3302012-08-10 17:36:00 +0100283 struct ad7192_state *st = ad_sigma_delta_to_ad7192(sd);
Michael Hennerich7a27b042011-08-17 17:29:34 +0200284
Lars-Peter Clausen3f7c3302012-08-10 17:36:00 +0100285 st->mode &= ~AD7192_MODE_SEL_MASK;
286 st->mode |= AD7192_MODE_SEL(mode);
Michael Hennerich7a27b042011-08-17 17:29:34 +0200287
Lars-Peter Clausen3f7c3302012-08-10 17:36:00 +0100288 return ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, st->mode);
Michael Hennerich7a27b042011-08-17 17:29:34 +0200289}
290
Lars-Peter Clausen3f7c3302012-08-10 17:36:00 +0100291static const struct ad_sigma_delta_info ad7192_sigma_delta_info = {
292 .set_channel = ad7192_set_channel,
293 .set_mode = ad7192_set_mode,
294 .has_registers = true,
295 .addr_shift = 3,
296 .read_mask = BIT(6),
Alexandru Tachici89a86da2021-09-06 09:56:28 +0300297 .irq_flags = IRQF_TRIGGER_FALLING,
Lars-Peter Clausen3f7c3302012-08-10 17:36:00 +0100298};
Michael Hennerich7a27b042011-08-17 17:29:34 +0200299
Lars-Peter Clausen3f7c3302012-08-10 17:36:00 +0100300static const struct ad_sd_calib_data ad7192_calib_arr[8] = {
Michael Hennerich7a27b042011-08-17 17:29:34 +0200301 {AD7192_MODE_CAL_INT_ZERO, AD7192_CH_AIN1},
302 {AD7192_MODE_CAL_INT_FULL, AD7192_CH_AIN1},
303 {AD7192_MODE_CAL_INT_ZERO, AD7192_CH_AIN2},
304 {AD7192_MODE_CAL_INT_FULL, AD7192_CH_AIN2},
305 {AD7192_MODE_CAL_INT_ZERO, AD7192_CH_AIN3},
306 {AD7192_MODE_CAL_INT_FULL, AD7192_CH_AIN3},
307 {AD7192_MODE_CAL_INT_ZERO, AD7192_CH_AIN4},
308 {AD7192_MODE_CAL_INT_FULL, AD7192_CH_AIN4}
309};
310
311static int ad7192_calibrate_all(struct ad7192_state *st)
312{
Mircea Caprioru17b90e62019-03-15 13:14:24 +0200313 return ad_sd_calibrate_all(&st->sd, ad7192_calib_arr,
314 ARRAY_SIZE(ad7192_calib_arr));
Michael Hennerich7a27b042011-08-17 17:29:34 +0200315}
316
Alexandru Ardeleane31b6172018-01-22 11:53:12 +0200317static inline bool ad7192_valid_external_frequency(u32 freq)
318{
319 return (freq >= AD7192_EXT_FREQ_MHZ_MIN &&
320 freq <= AD7192_EXT_FREQ_MHZ_MAX);
321}
322
Mircea Caprioru3ad7a932019-03-15 13:29:02 +0200323static int ad7192_of_clock_select(struct ad7192_state *st)
324{
325 struct device_node *np = st->sd.spi->dev.of_node;
326 unsigned int clock_sel;
327
328 clock_sel = AD7192_CLK_INT;
329
330 /* use internal clock */
Alexandru Ardeleanc9ec2cb2021-05-13 15:07:49 +0300331 if (st->mclk) {
Mircea Caprioru3ad7a932019-03-15 13:29:02 +0200332 if (of_property_read_bool(np, "adi,int-clock-output-enable"))
333 clock_sel = AD7192_CLK_INT_CO;
334 } else {
335 if (of_property_read_bool(np, "adi,clock-xtal"))
336 clock_sel = AD7192_CLK_EXT_MCLK1_2;
337 else
338 clock_sel = AD7192_CLK_EXT_MCLK2;
339 }
340
341 return clock_sel;
342}
343
Mircea Capriorueb4f07a2019-03-15 13:29:03 +0200344static int ad7192_setup(struct ad7192_state *st, struct device_node *np)
Michael Hennerich7a27b042011-08-17 17:29:34 +0200345{
Lars-Peter Clausen3f7c3302012-08-10 17:36:00 +0100346 struct iio_dev *indio_dev = spi_get_drvdata(st->sd.spi);
Mircea Caprioru77f6a232019-08-14 10:31:47 +0300347 bool rej60_en, refin2_en;
Mircea Capriorueb4f07a2019-03-15 13:29:03 +0200348 bool buf_en, bipolar, burnout_curr_en;
Michael Hennerich7a27b042011-08-17 17:29:34 +0200349 unsigned long long scale_uv;
350 int i, ret, id;
Michael Hennerich7a27b042011-08-17 17:29:34 +0200351
352 /* reset the serial interface */
Stefan Popaf7909232017-09-14 16:50:28 +0300353 ret = ad_sd_reset(&st->sd, 48);
Michael Hennerich7a27b042011-08-17 17:29:34 +0200354 if (ret < 0)
Mircea Caprioru753a9872019-03-15 13:14:25 +0200355 return ret;
Vaishali Thakkarbb49a0f2014-09-24 17:16:54 +0530356 usleep_range(500, 1000); /* Wait for at least 500us */
Michael Hennerich7a27b042011-08-17 17:29:34 +0200357
358 /* write/read test for device presence */
Lars-Peter Clausen3f7c3302012-08-10 17:36:00 +0100359 ret = ad_sd_read_reg(&st->sd, AD7192_REG_ID, 1, &id);
Michael Hennerich7a27b042011-08-17 17:29:34 +0200360 if (ret)
Mircea Caprioru753a9872019-03-15 13:14:25 +0200361 return ret;
Michael Hennerich7a27b042011-08-17 17:29:34 +0200362
363 id &= AD7192_ID_MASK;
364
Alexandru Ardelean8f2273b2020-04-15 08:58:03 +0300365 if (id != st->chip_info->chip_id)
Vaishali Thakkar29aeb332014-10-06 15:10:25 +0530366 dev_warn(&st->sd.spi->dev, "device ID query failed (0x%X)\n",
Ioana Ciorneie8ef49f2015-10-14 21:14:13 +0300367 id);
Michael Hennerich7a27b042011-08-17 17:29:34 +0200368
Michael Hennerich7a27b042011-08-17 17:29:34 +0200369 st->mode = AD7192_MODE_SEL(AD7192_MODE_IDLE) |
Mircea Caprioru3ad7a932019-03-15 13:29:02 +0200370 AD7192_MODE_CLKSRC(st->clock_sel) |
Michael Hennerich7a27b042011-08-17 17:29:34 +0200371 AD7192_MODE_RATE(480);
372
373 st->conf = AD7192_CONF_GAIN(0);
374
Mircea Capriorueb4f07a2019-03-15 13:29:03 +0200375 rej60_en = of_property_read_bool(np, "adi,rejection-60-Hz-enable");
376 if (rej60_en)
Michael Hennerich7a27b042011-08-17 17:29:34 +0200377 st->mode |= AD7192_MODE_REJ60;
378
Mircea Capriorueb4f07a2019-03-15 13:29:03 +0200379 refin2_en = of_property_read_bool(np, "adi,refin2-pins-enable");
Alexandru Ardelean8f2273b2020-04-15 08:58:03 +0300380 if (refin2_en && st->chip_info->chip_id != CHIPID_AD7195)
Michael Hennerich7a27b042011-08-17 17:29:34 +0200381 st->conf |= AD7192_CONF_REFSEL;
382
Mircea Caprioru77f6a232019-08-14 10:31:47 +0300383 st->conf &= ~AD7192_CONF_CHOP;
384 st->f_order = AD7192_NO_SYNC_FILTER;
Michael Hennerich7a27b042011-08-17 17:29:34 +0200385
Mircea Capriorueb4f07a2019-03-15 13:29:03 +0200386 buf_en = of_property_read_bool(np, "adi,buffer-enable");
387 if (buf_en)
Michael Hennerich7a27b042011-08-17 17:29:34 +0200388 st->conf |= AD7192_CONF_BUF;
389
Mircea Capriorueb4f07a2019-03-15 13:29:03 +0200390 bipolar = of_property_read_bool(np, "bipolar");
391 if (!bipolar)
Michael Hennerich7a27b042011-08-17 17:29:34 +0200392 st->conf |= AD7192_CONF_UNIPOLAR;
393
Mircea Capriorueb4f07a2019-03-15 13:29:03 +0200394 burnout_curr_en = of_property_read_bool(np,
395 "adi,burnout-currents-enable");
Mircea Caprioru77f6a232019-08-14 10:31:47 +0300396 if (burnout_curr_en && buf_en) {
Michael Hennerich7a27b042011-08-17 17:29:34 +0200397 st->conf |= AD7192_CONF_BURN;
Mircea Capriorueb4f07a2019-03-15 13:29:03 +0200398 } else if (burnout_curr_en) {
Alexandru Ardelean64eb8a12018-01-18 16:57:40 +0200399 dev_warn(&st->sd.spi->dev,
400 "Can't enable burnout currents: see CHOP or buffer\n");
401 }
Michael Hennerich7a27b042011-08-17 17:29:34 +0200402
Lars-Peter Clausen3f7c3302012-08-10 17:36:00 +0100403 ret = ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, st->mode);
Michael Hennerich7a27b042011-08-17 17:29:34 +0200404 if (ret)
Mircea Caprioru753a9872019-03-15 13:14:25 +0200405 return ret;
Michael Hennerich7a27b042011-08-17 17:29:34 +0200406
Lars-Peter Clausen3f7c3302012-08-10 17:36:00 +0100407 ret = ad_sd_write_reg(&st->sd, AD7192_REG_CONF, 3, st->conf);
Michael Hennerich7a27b042011-08-17 17:29:34 +0200408 if (ret)
Mircea Caprioru753a9872019-03-15 13:14:25 +0200409 return ret;
Michael Hennerich7a27b042011-08-17 17:29:34 +0200410
411 ret = ad7192_calibrate_all(st);
412 if (ret)
Mircea Caprioru753a9872019-03-15 13:14:25 +0200413 return ret;
Michael Hennerich7a27b042011-08-17 17:29:34 +0200414
415 /* Populate available ADC input ranges */
416 for (i = 0; i < ARRAY_SIZE(st->scale_avail); i++) {
417 scale_uv = ((u64)st->int_vref_mv * 100000000)
418 >> (indio_dev->channels[0].scan_type.realbits -
419 ((st->conf & AD7192_CONF_UNIPOLAR) ? 0 : 1));
420 scale_uv >>= i;
421
422 st->scale_avail[i][1] = do_div(scale_uv, 100000000) * 10;
423 st->scale_avail[i][0] = scale_uv;
424 }
425
426 return 0;
Michael Hennerich7a27b042011-08-17 17:29:34 +0200427}
428
Michael Hennerich7a27b042011-08-17 17:29:34 +0200429static ssize_t ad7192_show_ac_excitation(struct device *dev,
Ioana Ciorneie8ef49f2015-10-14 21:14:13 +0300430 struct device_attribute *attr,
431 char *buf)
Michael Hennerich7a27b042011-08-17 17:29:34 +0200432{
Lars-Peter Clausen62c51832012-05-12 15:39:42 +0200433 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Michael Hennerich7a27b042011-08-17 17:29:34 +0200434 struct ad7192_state *st = iio_priv(indio_dev);
435
436 return sprintf(buf, "%d\n", !!(st->mode & AD7192_MODE_ACX));
437}
438
439static ssize_t ad7192_show_bridge_switch(struct device *dev,
Ioana Ciorneie8ef49f2015-10-14 21:14:13 +0300440 struct device_attribute *attr,
441 char *buf)
Michael Hennerich7a27b042011-08-17 17:29:34 +0200442{
Lars-Peter Clausen62c51832012-05-12 15:39:42 +0200443 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Michael Hennerich7a27b042011-08-17 17:29:34 +0200444 struct ad7192_state *st = iio_priv(indio_dev);
445
446 return sprintf(buf, "%d\n", !!(st->gpocon & AD7192_GPOCON_BPDSW));
447}
448
449static ssize_t ad7192_set(struct device *dev,
Ioana Ciorneie8ef49f2015-10-14 21:14:13 +0300450 struct device_attribute *attr,
451 const char *buf,
452 size_t len)
Michael Hennerich7a27b042011-08-17 17:29:34 +0200453{
Lars-Peter Clausen62c51832012-05-12 15:39:42 +0200454 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Michael Hennerich7a27b042011-08-17 17:29:34 +0200455 struct ad7192_state *st = iio_priv(indio_dev);
456 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
457 int ret;
458 bool val;
459
460 ret = strtobool(buf, &val);
461 if (ret < 0)
462 return ret;
463
Alison Schofield1c118b7232016-03-09 11:30:43 -0800464 ret = iio_device_claim_direct_mode(indio_dev);
465 if (ret)
466 return ret;
Michael Hennerich7a27b042011-08-17 17:29:34 +0200467
Ioana Ciorneic30685c2015-10-14 21:14:15 +0300468 switch ((u32)this_attr->address) {
Michael Hennerich7a27b042011-08-17 17:29:34 +0200469 case AD7192_REG_GPOCON:
470 if (val)
471 st->gpocon |= AD7192_GPOCON_BPDSW;
472 else
473 st->gpocon &= ~AD7192_GPOCON_BPDSW;
474
Lars-Peter Clausen3f7c3302012-08-10 17:36:00 +0100475 ad_sd_write_reg(&st->sd, AD7192_REG_GPOCON, 1, st->gpocon);
Michael Hennerich7a27b042011-08-17 17:29:34 +0200476 break;
477 case AD7192_REG_MODE:
478 if (val)
479 st->mode |= AD7192_MODE_ACX;
480 else
481 st->mode &= ~AD7192_MODE_ACX;
482
Lars-Peter Clausen3f7c3302012-08-10 17:36:00 +0100483 ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, st->mode);
Michael Hennerich7a27b042011-08-17 17:29:34 +0200484 break;
485 default:
486 ret = -EINVAL;
487 }
488
Alison Schofield1c118b7232016-03-09 11:30:43 -0800489 iio_device_release_direct_mode(indio_dev);
Michael Hennerich7a27b042011-08-17 17:29:34 +0200490
491 return ret ? ret : len;
492}
493
Mircea Caprioru77f6a232019-08-14 10:31:47 +0300494static void ad7192_get_available_filter_freq(struct ad7192_state *st,
495 int *freq)
496{
497 unsigned int fadc;
498
499 /* Formulas for filter at page 25 of the datasheet */
500 fadc = DIV_ROUND_CLOSEST(st->fclk,
501 AD7192_SYNC4_FILTER * AD7192_MODE_RATE(st->mode));
502 freq[0] = DIV_ROUND_CLOSEST(fadc * 240, 1024);
503
504 fadc = DIV_ROUND_CLOSEST(st->fclk,
505 AD7192_SYNC3_FILTER * AD7192_MODE_RATE(st->mode));
506 freq[1] = DIV_ROUND_CLOSEST(fadc * 240, 1024);
507
508 fadc = DIV_ROUND_CLOSEST(st->fclk, AD7192_MODE_RATE(st->mode));
509 freq[2] = DIV_ROUND_CLOSEST(fadc * 230, 1024);
510 freq[3] = DIV_ROUND_CLOSEST(fadc * 272, 1024);
511}
512
513static ssize_t ad7192_show_filter_avail(struct device *dev,
514 struct device_attribute *attr,
515 char *buf)
516{
517 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
518 struct ad7192_state *st = iio_priv(indio_dev);
519 unsigned int freq_avail[4], i;
520 size_t len = 0;
521
522 ad7192_get_available_filter_freq(st, freq_avail);
523
524 for (i = 0; i < ARRAY_SIZE(freq_avail); i++)
525 len += scnprintf(buf + len, PAGE_SIZE - len,
526 "%d.%d ", freq_avail[i] / 1000,
527 freq_avail[i] % 1000);
528
529 buf[len - 1] = '\n';
530
531 return len;
532}
533
534static IIO_DEVICE_ATTR(filter_low_pass_3db_frequency_available,
535 0444, ad7192_show_filter_avail, NULL, 0);
536
Artur Lorincz2eaf1422017-02-04 20:11:34 +0100537static IIO_DEVICE_ATTR(bridge_switch_en, 0644,
Michael Hennerich7a27b042011-08-17 17:29:34 +0200538 ad7192_show_bridge_switch, ad7192_set,
539 AD7192_REG_GPOCON);
540
Artur Lorincz2eaf1422017-02-04 20:11:34 +0100541static IIO_DEVICE_ATTR(ac_excitation_en, 0644,
Michael Hennerich7a27b042011-08-17 17:29:34 +0200542 ad7192_show_ac_excitation, ad7192_set,
543 AD7192_REG_MODE);
544
545static struct attribute *ad7192_attributes[] = {
Mircea Caprioru77f6a232019-08-14 10:31:47 +0300546 &iio_dev_attr_filter_low_pass_3db_frequency_available.dev_attr.attr,
Michael Hennerich7a27b042011-08-17 17:29:34 +0200547 &iio_dev_attr_bridge_switch_en.dev_attr.attr,
548 &iio_dev_attr_ac_excitation_en.dev_attr.attr,
549 NULL
550};
551
Michael Hennerich7a27b042011-08-17 17:29:34 +0200552static const struct attribute_group ad7192_attribute_group = {
553 .attrs = ad7192_attributes,
Jonathan Cameron15bbb772012-01-29 11:07:00 +0000554};
555
556static struct attribute *ad7195_attributes[] = {
Mircea Caprioru77f6a232019-08-14 10:31:47 +0300557 &iio_dev_attr_filter_low_pass_3db_frequency_available.dev_attr.attr,
Jonathan Cameron15bbb772012-01-29 11:07:00 +0000558 &iio_dev_attr_bridge_switch_en.dev_attr.attr,
559 NULL
560};
561
562static const struct attribute_group ad7195_attribute_group = {
563 .attrs = ad7195_attributes,
Michael Hennerich7a27b042011-08-17 17:29:34 +0200564};
565
Lars-Peter Clausen4fcbcf92012-08-10 17:36:00 +0100566static unsigned int ad7192_get_temp_scale(bool unipolar)
567{
568 return unipolar ? 2815 * 2 : 2815;
569}
570
Mircea Caprioru77f6a232019-08-14 10:31:47 +0300571static int ad7192_set_3db_filter_freq(struct ad7192_state *st,
572 int val, int val2)
573{
574 int freq_avail[4], i, ret, freq;
575 unsigned int diff_new, diff_old;
576 int idx = 0;
577
578 diff_old = U32_MAX;
579 freq = val * 1000 + val2;
580
581 ad7192_get_available_filter_freq(st, freq_avail);
582
583 for (i = 0; i < ARRAY_SIZE(freq_avail); i++) {
584 diff_new = abs(freq - freq_avail[i]);
585 if (diff_new < diff_old) {
586 diff_old = diff_new;
587 idx = i;
588 }
589 }
590
591 switch (idx) {
592 case 0:
593 st->f_order = AD7192_SYNC4_FILTER;
594 st->mode &= ~AD7192_MODE_SINC3;
595
596 st->conf |= AD7192_CONF_CHOP;
597 break;
598 case 1:
599 st->f_order = AD7192_SYNC3_FILTER;
600 st->mode |= AD7192_MODE_SINC3;
601
602 st->conf |= AD7192_CONF_CHOP;
603 break;
604 case 2:
605 st->f_order = AD7192_NO_SYNC_FILTER;
606 st->mode &= ~AD7192_MODE_SINC3;
607
608 st->conf &= ~AD7192_CONF_CHOP;
609 break;
610 case 3:
611 st->f_order = AD7192_NO_SYNC_FILTER;
612 st->mode |= AD7192_MODE_SINC3;
613
614 st->conf &= ~AD7192_CONF_CHOP;
615 break;
616 }
617
618 ret = ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, st->mode);
619 if (ret < 0)
620 return ret;
621
622 return ad_sd_write_reg(&st->sd, AD7192_REG_CONF, 3, st->conf);
623}
624
625static int ad7192_get_3db_filter_freq(struct ad7192_state *st)
626{
627 unsigned int fadc;
628
629 fadc = DIV_ROUND_CLOSEST(st->fclk,
630 st->f_order * AD7192_MODE_RATE(st->mode));
631
632 if (st->conf & AD7192_CONF_CHOP)
633 return DIV_ROUND_CLOSEST(fadc * 240, 1024);
634 if (st->mode & AD7192_MODE_SINC3)
635 return DIV_ROUND_CLOSEST(fadc * 272, 1024);
636 else
637 return DIV_ROUND_CLOSEST(fadc * 230, 1024);
638}
639
Michael Hennerich7a27b042011-08-17 17:29:34 +0200640static int ad7192_read_raw(struct iio_dev *indio_dev,
641 struct iio_chan_spec const *chan,
642 int *val,
643 int *val2,
644 long m)
645{
646 struct ad7192_state *st = iio_priv(indio_dev);
Michael Hennerich7a27b042011-08-17 17:29:34 +0200647 bool unipolar = !!(st->conf & AD7192_CONF_UNIPOLAR);
648
649 switch (m) {
Jonathan Cameronb11f98f2012-04-15 17:41:18 +0100650 case IIO_CHAN_INFO_RAW:
Lars-Peter Clausen3f7c3302012-08-10 17:36:00 +0100651 return ad_sigma_delta_single_conversion(indio_dev, chan, val);
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100652 case IIO_CHAN_INFO_SCALE:
653 switch (chan->type) {
654 case IIO_VOLTAGE:
Aastha Gupta2db82e32017-09-27 12:01:59 +0530655 mutex_lock(&st->lock);
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100656 *val = st->scale_avail[AD7192_CONF_GAIN(st->conf)][0];
657 *val2 = st->scale_avail[AD7192_CONF_GAIN(st->conf)][1];
Aastha Gupta2db82e32017-09-27 12:01:59 +0530658 mutex_unlock(&st->lock);
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100659 return IIO_VAL_INT_PLUS_NANO;
660 case IIO_TEMP:
Lars-Peter Clausen4fcbcf92012-08-10 17:36:00 +0100661 *val = 0;
662 *val2 = 1000000000 / ad7192_get_temp_scale(unipolar);
663 return IIO_VAL_INT_PLUS_NANO;
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100664 default:
665 return -EINVAL;
666 }
Lars-Peter Clausen58cdff62012-08-10 17:36:00 +0100667 case IIO_CHAN_INFO_OFFSET:
668 if (!unipolar)
Lars-Peter Clausen4fcbcf92012-08-10 17:36:00 +0100669 *val = -(1 << (chan->scan_type.realbits - 1));
Lars-Peter Clausen58cdff62012-08-10 17:36:00 +0100670 else
671 *val = 0;
Lars-Peter Clausen4fcbcf92012-08-10 17:36:00 +0100672 /* Kelvin to Celsius */
673 if (chan->type == IIO_TEMP)
674 *val -= 273 * ad7192_get_temp_scale(unipolar);
Lars-Peter Clausen58cdff62012-08-10 17:36:00 +0100675 return IIO_VAL_INT;
Eva Rachel Retuyaa13e8312016-10-05 11:06:21 +0800676 case IIO_CHAN_INFO_SAMP_FREQ:
Mircea Caprioru3ad7a932019-03-15 13:29:02 +0200677 *val = st->fclk /
Eva Rachel Retuyaa13e8312016-10-05 11:06:21 +0800678 (st->f_order * 1024 * AD7192_MODE_RATE(st->mode));
679 return IIO_VAL_INT;
Mircea Caprioru77f6a232019-08-14 10:31:47 +0300680 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
681 *val = ad7192_get_3db_filter_freq(st);
682 *val2 = 1000;
683 return IIO_VAL_FRACTIONAL;
Michael Hennerich7a27b042011-08-17 17:29:34 +0200684 }
685
686 return -EINVAL;
687}
688
689static int ad7192_write_raw(struct iio_dev *indio_dev,
Ioana Ciorneie8ef49f2015-10-14 21:14:13 +0300690 struct iio_chan_spec const *chan,
691 int val,
692 int val2,
693 long mask)
Michael Hennerich7a27b042011-08-17 17:29:34 +0200694{
695 struct ad7192_state *st = iio_priv(indio_dev);
Eva Rachel Retuyaa13e8312016-10-05 11:06:21 +0800696 int ret, i, div;
Michael Hennerich7a27b042011-08-17 17:29:34 +0200697 unsigned int tmp;
698
Alison Schofield1c118b7232016-03-09 11:30:43 -0800699 ret = iio_device_claim_direct_mode(indio_dev);
700 if (ret)
701 return ret;
Michael Hennerich7a27b042011-08-17 17:29:34 +0200702
703 switch (mask) {
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100704 case IIO_CHAN_INFO_SCALE:
Michael Hennerich7a27b042011-08-17 17:29:34 +0200705 ret = -EINVAL;
Aastha Gupta2db82e32017-09-27 12:01:59 +0530706 mutex_lock(&st->lock);
Michael Hennerich7a27b042011-08-17 17:29:34 +0200707 for (i = 0; i < ARRAY_SIZE(st->scale_avail); i++)
708 if (val2 == st->scale_avail[i][1]) {
Lars-Peter Clausen3f7c3302012-08-10 17:36:00 +0100709 ret = 0;
Michael Hennerich7a27b042011-08-17 17:29:34 +0200710 tmp = st->conf;
711 st->conf &= ~AD7192_CONF_GAIN(-1);
712 st->conf |= AD7192_CONF_GAIN(i);
Lars-Peter Clausen3f7c3302012-08-10 17:36:00 +0100713 if (tmp == st->conf)
714 break;
715 ad_sd_write_reg(&st->sd, AD7192_REG_CONF,
Ioana Ciorneie8ef49f2015-10-14 21:14:13 +0300716 3, st->conf);
Lars-Peter Clausen3f7c3302012-08-10 17:36:00 +0100717 ad7192_calibrate_all(st);
718 break;
Michael Hennerich7a27b042011-08-17 17:29:34 +0200719 }
Aastha Gupta2db82e32017-09-27 12:01:59 +0530720 mutex_unlock(&st->lock);
Lars-Peter Clausenf09906f2012-08-10 17:36:00 +0100721 break;
Eva Rachel Retuyaa13e8312016-10-05 11:06:21 +0800722 case IIO_CHAN_INFO_SAMP_FREQ:
723 if (!val) {
724 ret = -EINVAL;
725 break;
726 }
727
Mircea Caprioru3ad7a932019-03-15 13:29:02 +0200728 div = st->fclk / (val * st->f_order * 1024);
Eva Rachel Retuyaa13e8312016-10-05 11:06:21 +0800729 if (div < 1 || div > 1023) {
730 ret = -EINVAL;
731 break;
732 }
733
734 st->mode &= ~AD7192_MODE_RATE(-1);
735 st->mode |= AD7192_MODE_RATE(div);
736 ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, st->mode);
737 break;
Mircea Caprioru77f6a232019-08-14 10:31:47 +0300738 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
739 ret = ad7192_set_3db_filter_freq(st, val, val2 / 1000);
740 break;
Michael Hennerich7a27b042011-08-17 17:29:34 +0200741 default:
742 ret = -EINVAL;
743 }
744
Alison Schofield1c118b7232016-03-09 11:30:43 -0800745 iio_device_release_direct_mode(indio_dev);
Michael Hennerich7a27b042011-08-17 17:29:34 +0200746
747 return ret;
748}
749
Michael Hennerich7a27b042011-08-17 17:29:34 +0200750static int ad7192_write_raw_get_fmt(struct iio_dev *indio_dev,
Ioana Ciorneie8ef49f2015-10-14 21:14:13 +0300751 struct iio_chan_spec const *chan,
752 long mask)
Michael Hennerich7a27b042011-08-17 17:29:34 +0200753{
Eva Rachel Retuyaa13e8312016-10-05 11:06:21 +0800754 switch (mask) {
755 case IIO_CHAN_INFO_SCALE:
756 return IIO_VAL_INT_PLUS_NANO;
757 case IIO_CHAN_INFO_SAMP_FREQ:
758 return IIO_VAL_INT;
Mircea Caprioru77f6a232019-08-14 10:31:47 +0300759 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
760 return IIO_VAL_INT_PLUS_MICRO;
Eva Rachel Retuyaa13e8312016-10-05 11:06:21 +0800761 default:
762 return -EINVAL;
763 }
Michael Hennerich7a27b042011-08-17 17:29:34 +0200764}
765
Mircea Caprioru2b0d1c62019-03-19 16:25:20 +0200766static int ad7192_read_avail(struct iio_dev *indio_dev,
767 struct iio_chan_spec const *chan,
768 const int **vals, int *type, int *length,
769 long mask)
770{
771 struct ad7192_state *st = iio_priv(indio_dev);
772
773 switch (mask) {
774 case IIO_CHAN_INFO_SCALE:
775 *vals = (int *)st->scale_avail;
776 *type = IIO_VAL_INT_PLUS_NANO;
777 /* Values are stored in a 2D matrix */
778 *length = ARRAY_SIZE(st->scale_avail) * 2;
779
780 return IIO_AVAIL_LIST;
781 }
782
783 return -EINVAL;
784}
785
Michael Hennerich7a27b042011-08-17 17:29:34 +0200786static const struct iio_info ad7192_info = {
simran singhal705d5ed2017-03-11 19:56:35 +0530787 .read_raw = ad7192_read_raw,
788 .write_raw = ad7192_write_raw,
789 .write_raw_get_fmt = ad7192_write_raw_get_fmt,
Mircea Caprioru2b0d1c62019-03-19 16:25:20 +0200790 .read_avail = ad7192_read_avail,
Michael Hennerich7a27b042011-08-17 17:29:34 +0200791 .attrs = &ad7192_attribute_group,
Lars-Peter Clausen3f7c3302012-08-10 17:36:00 +0100792 .validate_trigger = ad_sd_validate_trigger,
Michael Hennerich7a27b042011-08-17 17:29:34 +0200793};
794
Jonathan Cameron15bbb772012-01-29 11:07:00 +0000795static const struct iio_info ad7195_info = {
simran singhal705d5ed2017-03-11 19:56:35 +0530796 .read_raw = ad7192_read_raw,
797 .write_raw = ad7192_write_raw,
798 .write_raw_get_fmt = ad7192_write_raw_get_fmt,
Mircea Caprioru2b0d1c62019-03-19 16:25:20 +0200799 .read_avail = ad7192_read_avail,
Jonathan Cameron15bbb772012-01-29 11:07:00 +0000800 .attrs = &ad7195_attribute_group,
Lars-Peter Clausen3f7c3302012-08-10 17:36:00 +0100801 .validate_trigger = ad_sd_validate_trigger,
Jonathan Cameron15bbb772012-01-29 11:07:00 +0000802};
803
Alexandru Tachici893ac1a2020-02-12 18:17:18 +0200804#define __AD719x_CHANNEL(_si, _channel1, _channel2, _address, _extend_name, \
805 _type, _mask_type_av, _ext_info) \
806 { \
807 .type = (_type), \
808 .differential = ((_channel2) == -1 ? 0 : 1), \
809 .indexed = 1, \
810 .channel = (_channel1), \
811 .channel2 = (_channel2), \
812 .address = (_address), \
813 .extend_name = (_extend_name), \
814 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
815 BIT(IIO_CHAN_INFO_OFFSET), \
816 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
817 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
818 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
819 .info_mask_shared_by_type_available = (_mask_type_av), \
820 .ext_info = (_ext_info), \
821 .scan_index = (_si), \
822 .scan_type = { \
823 .sign = 'u', \
824 .realbits = 24, \
825 .storagebits = 32, \
826 .endianness = IIO_BE, \
827 }, \
828 }
829
830#define AD719x_DIFF_CHANNEL(_si, _channel1, _channel2, _address) \
831 __AD719x_CHANNEL(_si, _channel1, _channel2, _address, NULL, \
832 IIO_VOLTAGE, BIT(IIO_CHAN_INFO_SCALE), \
833 ad7192_calibsys_ext_info)
834
835#define AD719x_CHANNEL(_si, _channel1, _address) \
836 __AD719x_CHANNEL(_si, _channel1, -1, _address, NULL, IIO_VOLTAGE, \
837 BIT(IIO_CHAN_INFO_SCALE), ad7192_calibsys_ext_info)
838
839#define AD719x_SHORTED_CHANNEL(_si, _channel1, _address) \
840 __AD719x_CHANNEL(_si, _channel1, -1, _address, "shorted", IIO_VOLTAGE, \
841 BIT(IIO_CHAN_INFO_SCALE), ad7192_calibsys_ext_info)
842
843#define AD719x_TEMP_CHANNEL(_si, _address) \
844 __AD719x_CHANNEL(_si, 0, -1, _address, NULL, IIO_TEMP, 0, NULL)
845
Lars-Peter Clausenf4e4b952012-08-09 08:51:00 +0100846static const struct iio_chan_spec ad7192_channels[] = {
Alexandru Tachici893ac1a2020-02-12 18:17:18 +0200847 AD719x_DIFF_CHANNEL(0, 1, 2, AD7192_CH_AIN1P_AIN2M),
848 AD719x_DIFF_CHANNEL(1, 3, 4, AD7192_CH_AIN3P_AIN4M),
849 AD719x_TEMP_CHANNEL(2, AD7192_CH_TEMP),
850 AD719x_SHORTED_CHANNEL(3, 2, AD7192_CH_AIN2P_AIN2M),
851 AD719x_CHANNEL(4, 1, AD7192_CH_AIN1),
852 AD719x_CHANNEL(5, 2, AD7192_CH_AIN2),
853 AD719x_CHANNEL(6, 3, AD7192_CH_AIN3),
854 AD719x_CHANNEL(7, 4, AD7192_CH_AIN4),
Michael Hennerich7a27b042011-08-17 17:29:34 +0200855 IIO_CHAN_SOFT_TIMESTAMP(8),
856};
857
Lars-Peter Clausend69051b2016-02-08 11:45:04 +0100858static const struct iio_chan_spec ad7193_channels[] = {
Alexandru Tachici893ac1a2020-02-12 18:17:18 +0200859 AD719x_DIFF_CHANNEL(0, 1, 2, AD7193_CH_AIN1P_AIN2M),
860 AD719x_DIFF_CHANNEL(1, 3, 4, AD7193_CH_AIN3P_AIN4M),
861 AD719x_DIFF_CHANNEL(2, 5, 6, AD7193_CH_AIN5P_AIN6M),
862 AD719x_DIFF_CHANNEL(3, 7, 8, AD7193_CH_AIN7P_AIN8M),
863 AD719x_TEMP_CHANNEL(4, AD7193_CH_TEMP),
864 AD719x_SHORTED_CHANNEL(5, 2, AD7193_CH_AIN2P_AIN2M),
865 AD719x_CHANNEL(6, 1, AD7193_CH_AIN1),
866 AD719x_CHANNEL(7, 2, AD7193_CH_AIN2),
867 AD719x_CHANNEL(8, 3, AD7193_CH_AIN3),
868 AD719x_CHANNEL(9, 4, AD7193_CH_AIN4),
869 AD719x_CHANNEL(10, 5, AD7193_CH_AIN5),
870 AD719x_CHANNEL(11, 6, AD7193_CH_AIN6),
871 AD719x_CHANNEL(12, 7, AD7193_CH_AIN7),
872 AD719x_CHANNEL(13, 8, AD7193_CH_AIN8),
Lars-Peter Clausend69051b2016-02-08 11:45:04 +0100873 IIO_CHAN_SOFT_TIMESTAMP(14),
874};
875
Alexandru Ardelean8f2273b2020-04-15 08:58:03 +0300876static const struct ad7192_chip_info ad7192_chip_info_tbl[] = {
877 [ID_AD7190] = {
878 .chip_id = CHIPID_AD7190,
879 .name = "ad7190",
880 },
881 [ID_AD7192] = {
882 .chip_id = CHIPID_AD7192,
883 .name = "ad7192",
884 },
885 [ID_AD7193] = {
886 .chip_id = CHIPID_AD7193,
887 .name = "ad7193",
888 },
889 [ID_AD7195] = {
890 .chip_id = CHIPID_AD7195,
891 .name = "ad7195",
892 },
893};
894
Mircea Caprioru2b0d1c62019-03-19 16:25:20 +0200895static int ad7192_channels_config(struct iio_dev *indio_dev)
896{
897 struct ad7192_state *st = iio_priv(indio_dev);
Mircea Caprioru2b0d1c62019-03-19 16:25:20 +0200898
Alexandru Ardelean8f2273b2020-04-15 08:58:03 +0300899 switch (st->chip_info->chip_id) {
900 case CHIPID_AD7193:
Alexandru Tachici893ac1a2020-02-12 18:17:18 +0200901 indio_dev->channels = ad7193_channels;
Mircea Caprioru2b0d1c62019-03-19 16:25:20 +0200902 indio_dev->num_channels = ARRAY_SIZE(ad7193_channels);
903 break;
904 default:
Alexandru Tachici893ac1a2020-02-12 18:17:18 +0200905 indio_dev->channels = ad7192_channels;
Mircea Caprioru2b0d1c62019-03-19 16:25:20 +0200906 indio_dev->num_channels = ARRAY_SIZE(ad7192_channels);
907 break;
908 }
909
Mircea Caprioru2b0d1c62019-03-19 16:25:20 +0200910 return 0;
911}
912
Alexandru Ardeleanbd5dcde2021-05-13 15:07:50 +0300913static void ad7192_reg_disable(void *reg)
914{
915 regulator_disable(reg);
916}
917
918static void ad7192_clk_disable(void *clk)
919{
920 clk_disable_unprepare(clk);
921}
922
Bill Pemberton4ae1c612012-11-19 13:21:57 -0500923static int ad7192_probe(struct spi_device *spi)
Michael Hennerich7a27b042011-08-17 17:29:34 +0200924{
Michael Hennerich7a27b042011-08-17 17:29:34 +0200925 struct ad7192_state *st;
926 struct iio_dev *indio_dev;
Alexandru Ardeleanb0f27fc2021-05-13 15:07:44 +0300927 int ret;
Michael Hennerich7a27b042011-08-17 17:29:34 +0200928
Michael Hennerich7a27b042011-08-17 17:29:34 +0200929 if (!spi->irq) {
930 dev_err(&spi->dev, "no IRQ?\n");
931 return -ENODEV;
932 }
933
Sachin Kamat1e319ce2013-08-31 18:12:00 +0100934 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
Ioana Ciornei603f102f2015-10-14 21:14:14 +0300935 if (!indio_dev)
Michael Hennerich7a27b042011-08-17 17:29:34 +0200936 return -ENOMEM;
937
938 st = iio_priv(indio_dev);
939
Aastha Gupta2db82e32017-09-27 12:01:59 +0530940 mutex_init(&st->lock);
941
Eva Rachel Retuya9d154812016-11-01 01:04:33 +0800942 st->avdd = devm_regulator_get(&spi->dev, "avdd");
943 if (IS_ERR(st->avdd))
944 return PTR_ERR(st->avdd);
Michael Hennerich7a27b042011-08-17 17:29:34 +0200945
Eva Rachel Retuya9d154812016-11-01 01:04:33 +0800946 ret = regulator_enable(st->avdd);
Eva Rachel Retuya3925ff02016-11-01 01:04:31 +0800947 if (ret) {
948 dev_err(&spi->dev, "Failed to enable specified AVdd supply\n");
949 return ret;
Michael Hennerich7a27b042011-08-17 17:29:34 +0200950 }
Eva Rachel Retuyacb8bb162016-11-01 01:04:32 +0800951
Alexandru Ardeleanbd5dcde2021-05-13 15:07:50 +0300952 ret = devm_add_action_or_reset(&spi->dev, ad7192_reg_disable, st->avdd);
953 if (ret)
954 return ret;
955
Eva Rachel Retuyacb8bb162016-11-01 01:04:32 +0800956 st->dvdd = devm_regulator_get(&spi->dev, "dvdd");
Alexandru Ardeleanbd5dcde2021-05-13 15:07:50 +0300957 if (IS_ERR(st->dvdd))
958 return PTR_ERR(st->dvdd);
Eva Rachel Retuyacb8bb162016-11-01 01:04:32 +0800959
960 ret = regulator_enable(st->dvdd);
961 if (ret) {
962 dev_err(&spi->dev, "Failed to enable specified DVdd supply\n");
Alexandru Ardeleanbd5dcde2021-05-13 15:07:50 +0300963 return ret;
Eva Rachel Retuyacb8bb162016-11-01 01:04:32 +0800964 }
965
Alexandru Ardeleanbd5dcde2021-05-13 15:07:50 +0300966 ret = devm_add_action_or_reset(&spi->dev, ad7192_reg_disable, st->dvdd);
967 if (ret)
968 return ret;
969
Alexandru Ardeleanb0f27fc2021-05-13 15:07:44 +0300970 ret = regulator_get_voltage(st->avdd);
971 if (ret < 0) {
Mircea Caprioru4be27c22019-08-12 12:00:34 +0300972 dev_err(&spi->dev, "Device tree error, reference voltage undefined\n");
Alexandru Ardeleanbd5dcde2021-05-13 15:07:50 +0300973 return ret;
Alexandru Tachiciab0afa62020-02-12 18:17:17 +0200974 }
Alexandru Ardeleanb0f27fc2021-05-13 15:07:44 +0300975 st->int_vref_mv = ret / 1000;
Michael Hennerich7a27b042011-08-17 17:29:34 +0200976
Alexandru Ardelean8f2273b2020-04-15 08:58:03 +0300977 st->chip_info = of_device_get_match_data(&spi->dev);
Alexandru Ardelean8f2273b2020-04-15 08:58:03 +0300978 indio_dev->name = st->chip_info->name;
Michael Hennerich7a27b042011-08-17 17:29:34 +0200979 indio_dev->modes = INDIO_DIRECT_MODE;
Lars-Peter Clausend69051b2016-02-08 11:45:04 +0100980
Mircea Caprioru2b0d1c62019-03-19 16:25:20 +0200981 ret = ad7192_channels_config(indio_dev);
982 if (ret < 0)
Alexandru Ardeleanbd5dcde2021-05-13 15:07:50 +0300983 return ret;
Lars-Peter Clausend69051b2016-02-08 11:45:04 +0100984
Alexandru Ardelean8f2273b2020-04-15 08:58:03 +0300985 if (st->chip_info->chip_id == CHIPID_AD7195)
Jonathan Cameron15bbb772012-01-29 11:07:00 +0000986 indio_dev->info = &ad7195_info;
987 else
988 indio_dev->info = &ad7192_info;
Michael Hennerich7a27b042011-08-17 17:29:34 +0200989
Lars-Peter Clausen3f7c3302012-08-10 17:36:00 +0100990 ad_sd_init(&st->sd, indio_dev, spi, &ad7192_sigma_delta_info);
Michael Hennerich7a27b042011-08-17 17:29:34 +0200991
Alexandru Ardeleanbd5dcde2021-05-13 15:07:50 +0300992 ret = devm_ad_sd_setup_buffer_and_trigger(&spi->dev, indio_dev);
Michael Hennerich7a27b042011-08-17 17:29:34 +0200993 if (ret)
Alexandru Ardeleanbd5dcde2021-05-13 15:07:50 +0300994 return ret;
Michael Hennerich7a27b042011-08-17 17:29:34 +0200995
Mircea Caprioru3ad7a932019-03-15 13:29:02 +0200996 st->fclk = AD7192_INT_FREQ_MHZ;
997
Alexandru Ardeleanc9ec2cb2021-05-13 15:07:49 +0300998 st->mclk = devm_clk_get_optional(&spi->dev, "mclk");
Alexandru Ardeleanbd5dcde2021-05-13 15:07:50 +0300999 if (IS_ERR(st->mclk))
1000 return PTR_ERR(st->mclk);
Mircea Caprioru3ad7a932019-03-15 13:29:02 +02001001
1002 st->clock_sel = ad7192_of_clock_select(st);
1003
1004 if (st->clock_sel == AD7192_CLK_EXT_MCLK1_2 ||
1005 st->clock_sel == AD7192_CLK_EXT_MCLK2) {
1006 ret = clk_prepare_enable(st->mclk);
1007 if (ret < 0)
Alexandru Ardeleanbd5dcde2021-05-13 15:07:50 +03001008 return ret;
1009
1010 ret = devm_add_action_or_reset(&spi->dev, ad7192_clk_disable,
1011 st->mclk);
1012 if (ret)
1013 return ret;
Mircea Caprioru3ad7a932019-03-15 13:29:02 +02001014
1015 st->fclk = clk_get_rate(st->mclk);
1016 if (!ad7192_valid_external_frequency(st->fclk)) {
Mircea Caprioru3ad7a932019-03-15 13:29:02 +02001017 dev_err(&spi->dev,
1018 "External clock frequency out of bounds\n");
Alexandru Ardeleanbd5dcde2021-05-13 15:07:50 +03001019 return -EINVAL;
Mircea Caprioru3ad7a932019-03-15 13:29:02 +02001020 }
1021 }
1022
Mircea Capriorueb4f07a2019-03-15 13:29:03 +02001023 ret = ad7192_setup(st, spi->dev.of_node);
Michael Hennerich7a27b042011-08-17 17:29:34 +02001024 if (ret)
Alexandru Ardeleanbd5dcde2021-05-13 15:07:50 +03001025 return ret;
Michael Hennerich7a27b042011-08-17 17:29:34 +02001026
Alexandru Ardeleanbd5dcde2021-05-13 15:07:50 +03001027 return devm_iio_device_register(&spi->dev, indio_dev);
Michael Hennerich7a27b042011-08-17 17:29:34 +02001028}
1029
Alexandru Ardelean3eca1d22020-04-15 08:58:04 +03001030static const struct of_device_id ad7192_of_match[] = {
1031 { .compatible = "adi,ad7190", .data = &ad7192_chip_info_tbl[ID_AD7190] },
1032 { .compatible = "adi,ad7192", .data = &ad7192_chip_info_tbl[ID_AD7192] },
1033 { .compatible = "adi,ad7193", .data = &ad7192_chip_info_tbl[ID_AD7193] },
1034 { .compatible = "adi,ad7195", .data = &ad7192_chip_info_tbl[ID_AD7195] },
1035 {}
1036};
1037MODULE_DEVICE_TABLE(of, ad7192_of_match);
1038
Michael Hennerich7a27b042011-08-17 17:29:34 +02001039static struct spi_driver ad7192_driver = {
1040 .driver = {
1041 .name = "ad7192",
Bárbara Fernandes0eec1f32019-06-28 16:49:22 -03001042 .of_match_table = ad7192_of_match,
Michael Hennerich7a27b042011-08-17 17:29:34 +02001043 },
1044 .probe = ad7192_probe,
Michael Hennerich7a27b042011-08-17 17:29:34 +02001045};
Lars-Peter Clausenae6ae6f2011-11-16 10:13:39 +01001046module_spi_driver(ad7192_driver);
Michael Hennerich7a27b042011-08-17 17:29:34 +02001047
Michael Hennerich9920ed22018-08-14 13:23:17 +02001048MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
Lars-Peter Clausend69051b2016-02-08 11:45:04 +01001049MODULE_DESCRIPTION("Analog Devices AD7190, AD7192, AD7193, AD7195 ADC");
Michael Hennerich7a27b042011-08-17 17:29:34 +02001050MODULE_LICENSE("GPL v2");