Debian Codegraph @

drivers/iio/adc/ad7768-1.c

raw
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Analog Devices AD7768-1 SPI ADC driver
4 *
5 * Copyright 2017 Analog Devices Inc.
6 */
7 #include <linux/array_size.h>
8 #include <linux/bitfield.h>
9 #include <linux/cleanup.h>
10 #include <linux/clk.h>
11 #include <linux/completion.h>
12 #include <linux/delay.h>
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/gpio/driver.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/interrupt.h>
18 #include <linux/limits.h>
19 #include <linux/math.h>
20 #include <linux/minmax.h>
21 #include <linux/module.h>
22 #include <linux/mutex.h>
23 #include <linux/rational.h>
24 #include <linux/regmap.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/regulator/driver.h>
27 #include <linux/sysfs.h>
28 #include <linux/spi/spi.h>
29 #include <linux/unaligned.h>
30 #include <linux/units.h>
31 #include <linux/util_macros.h>
32
33 #include <linux/iio/buffer.h>
34 #include <linux/iio/iio.h>
35 #include <linux/iio/sysfs.h>
36 #include <linux/iio/trigger.h>
37 #include <linux/iio/triggered_buffer.h>
38 #include <linux/iio/trigger_consumer.h>
39
40 #include <dt-bindings/iio/adc/adi,ad7768-1.h>
41
42 /* AD7768 registers definition */
43 #define AD7768_REG_CHIP_TYPE 0x3
44 #define AD7768_REG_PROD_ID_L 0x4
45 #define AD7768_REG_PROD_ID_H 0x5
46 #define AD7768_REG_CHIP_GRADE 0x6
47 #define AD7768_REG_SCRATCH_PAD 0x0A
48 #define AD7768_REG_VENDOR_L 0x0C
49 #define AD7768_REG_VENDOR_H 0x0D
50 #define AD7768_REG_INTERFACE_FORMAT 0x14
51 #define AD7768_REG_POWER_CLOCK 0x15
52 #define AD7768_REG_ANALOG 0x16
53 #define AD7768_REG_ANALOG2 0x17
54 #define AD7768_REG_CONVERSION 0x18
55 #define AD7768_REG_DIGITAL_FILTER 0x19
56 #define AD7768_REG_SINC3_DEC_RATE_MSB 0x1A
57 #define AD7768_REG_SINC3_DEC_RATE_LSB 0x1B
58 #define AD7768_REG_DUTY_CYCLE_RATIO 0x1C
59 #define AD7768_REG_SYNC_RESET 0x1D
60 #define AD7768_REG_GPIO_CONTROL 0x1E
61 #define AD7768_REG_GPIO_WRITE 0x1F
62 #define AD7768_REG_GPIO_READ 0x20
63 #define AD7768_REG_OFFSET_HI 0x21
64 #define AD7768_REG_OFFSET_MID 0x22
65 #define AD7768_REG_OFFSET_LO 0x23
66 #define AD7768_REG_GAIN_HI 0x24
67 #define AD7768_REG_GAIN_MID 0x25
68 #define AD7768_REG_GAIN_LO 0x26
69 #define AD7768_REG_SPI_DIAG_ENABLE 0x28
70 #define AD7768_REG_ADC_DIAG_ENABLE 0x29
71 #define AD7768_REG_DIG_DIAG_ENABLE 0x2A
72 #define AD7768_REG24_ADC_DATA 0x2C
73 #define AD7768_REG_MASTER_STATUS 0x2D
74 #define AD7768_REG_SPI_DIAG_STATUS 0x2E
75 #define AD7768_REG_ADC_DIAG_STATUS 0x2F
76 #define AD7768_REG_DIG_DIAG_STATUS 0x30
77 #define AD7768_REG_MCLK_COUNTER 0x31
78 #define AD7768_REG_COEFF_CONTROL 0x32
79 #define AD7768_REG24_COEFF_DATA 0x33
80 #define AD7768_REG_ACCESS_KEY 0x34
81
82 /* AD7768_REG_POWER_CLOCK */
83 #define AD7768_PWR_MCLK_DIV_MSK GENMASK(5, 4)
84 #define AD7768_PWR_MCLK_DIV(x) FIELD_PREP(AD7768_PWR_MCLK_DIV_MSK, x)
85 #define AD7768_PWR_PWRMODE_MSK GENMASK(1, 0)
86 #define AD7768_PWR_PWRMODE(x) FIELD_PREP(AD7768_PWR_PWRMODE_MSK, x)
87
88 /* AD7768_REG_DIGITAL_FILTER */
89 #define AD7768_DIG_FIL_EN_60HZ_REJ BIT(7)
90 #define AD7768_DIG_FIL_FIL_MSK GENMASK(6, 4)
91 #define AD7768_DIG_FIL_FIL(x) FIELD_PREP(AD7768_DIG_FIL_FIL_MSK, x)
92 #define AD7768_DIG_FIL_DEC_MSK GENMASK(2, 0)
93 #define AD7768_DIG_FIL_DEC_RATE(x) FIELD_PREP(AD7768_DIG_FIL_DEC_MSK, x)
94
95 /* AD7768_REG_CONVERSION */
96 #define AD7768_CONV_MODE_MSK GENMASK(2, 0)
97 #define AD7768_CONV_MODE(x) FIELD_PREP(AD7768_CONV_MODE_MSK, x)
98
99 /* AD7768_REG_ANALOG2 */
100 #define AD7768_REG_ANALOG2_VCM_MSK GENMASK(2, 0)
101 #define AD7768_REG_ANALOG2_VCM(x) FIELD_PREP(AD7768_REG_ANALOG2_VCM_MSK, (x))
102
103 /* AD7768_REG_GPIO_CONTROL */
104 #define AD7768_GPIO_UNIVERSAL_EN BIT(7)
105 #define AD7768_GPIO_CONTROL_MSK GENMASK(3, 0)
106
107 /* AD7768_REG_GPIO_WRITE */
108 #define AD7768_GPIO_WRITE_MSK GENMASK(3, 0)
109
110 /* AD7768_REG_GPIO_READ */
111 #define AD7768_GPIO_READ_MSK GENMASK(3, 0)
112
113 #define AD7768_VCM_OFF 0x07
114
115 #define ADAQ776X_GAIN_MAX_NANO (128 * NANO)
116 #define ADAQ776X_MAX_GAIN_MODES 8
117
118 #define AD7768_TRIGGER_SOURCE_SYNC_IDX 0
119
120 #define AD7768_MAX_CHANNELS 1
121
122 #define ADAQ7768_PGA_PINS 3
123
124 enum ad7768_conv_mode {
125 AD7768_CONTINUOUS,
126 AD7768_ONE_SHOT,
127 AD7768_SINGLE,
128 AD7768_PERIODIC,
129 AD7768_STANDBY
130 };
131
132 enum ad7768_pwrmode {
133 AD7768_ECO_MODE = 0,
134 AD7768_MED_MODE = 2,
135 AD7768_FAST_MODE = 3
136 };
137
138 enum ad7768_mclk_div {
139 AD7768_MCLK_DIV_16,
140 AD7768_MCLK_DIV_8,
141 AD7768_MCLK_DIV_4,
142 AD7768_MCLK_DIV_2
143 };
144
145 enum ad7768_filter_type {
146 AD7768_FILTER_SINC5,
147 AD7768_FILTER_SINC3,
148 AD7768_FILTER_WIDEBAND,
149 AD7768_FILTER_SINC3_REJ60,
150 };
151
152 enum ad7768_filter_regval {
153 AD7768_FILTER_REGVAL_SINC5 = 0,
154 AD7768_FILTER_REGVAL_SINC5_X8 = 1,
155 AD7768_FILTER_REGVAL_SINC5_X16 = 2,
156 AD7768_FILTER_REGVAL_SINC3 = 3,
157 AD7768_FILTER_REGVAL_WIDEBAND = 4,
158 AD7768_FILTER_REGVAL_SINC3_REJ60 = 11,
159 };
160
161 enum ad7768_scan_type {
162 AD7768_SCAN_TYPE_NORMAL,
163 AD7768_SCAN_TYPE_HIGH_SPEED,
164 };
165
166 enum {
167 AD7768_PGA_GAIN_0,
168 AD7768_PGA_GAIN_1,
169 AD7768_PGA_GAIN_2,
170 AD7768_PGA_GAIN_3,
171 AD7768_PGA_GAIN_4,
172 AD7768_PGA_GAIN_5,
173 AD7768_PGA_GAIN_6,
174 AD7768_PGA_GAIN_7,
175 };
176
177 enum {
178 AD7768_AAF_IN1,
179 AD7768_AAF_IN2,
180 AD7768_AAF_IN3,
181 };
182
183 /* PGA and AAF gains in V/V */
184 static const int adaq7768_gains[] = {
185 [AD7768_PGA_GAIN_0] = 325, /* 0.325 */
186 [AD7768_PGA_GAIN_1] = 650, /* 0.650 */
187 [AD7768_PGA_GAIN_2] = 1300, /* 1.300 */
188 [AD7768_PGA_GAIN_3] = 2600, /* 2.600 */
189 [AD7768_PGA_GAIN_4] = 5200, /* 5.200 */
190 [AD7768_PGA_GAIN_5] = 10400, /* 10.400 */
191 [AD7768_PGA_GAIN_6] = 20800, /* 20.800 */
192 };
193
194 static const int adaq7769_gains[] = {
195 [AD7768_PGA_GAIN_0] = 1000, /* 1.000 */
196 [AD7768_PGA_GAIN_1] = 2000, /* 2.000 */
197 [AD7768_PGA_GAIN_2] = 4000, /* 4.000 */
198 [AD7768_PGA_GAIN_3] = 8000, /* 8.000 */
199 [AD7768_PGA_GAIN_4] = 16000, /* 16.000 */
200 [AD7768_PGA_GAIN_5] = 32000, /* 32.000 */
201 [AD7768_PGA_GAIN_6] = 64000, /* 64.000 */
202 [AD7768_PGA_GAIN_7] = 128000, /* 128.000 */
203 };
204
205 static const int ad7768_aaf_gains_bp[] = {
206 [AD7768_AAF_IN1] = 10000, /* 1.000 */
207 [AD7768_AAF_IN2] = 3640, /* 0.364 */
208 [AD7768_AAF_IN3] = 1430, /* 0.143 */
209 };
210
211 /* -3dB cutoff frequency multipliers (relative to ODR) for each filter type. */
212 static const int ad7768_filter_3db_odr_multiplier[] = {
213 [AD7768_FILTER_SINC5] = 204, /* 0.204 */
214 [AD7768_FILTER_SINC3] = 262, /* 0.2617 */
215 [AD7768_FILTER_SINC3_REJ60] = 262, /* 0.2617 */
216 [AD7768_FILTER_WIDEBAND] = 433, /* 0.433 */
217 };
218
219 static const int ad7768_mclk_div_rates[] = {
220 16, 8, 4, 2,
221 };
222
223 static const int ad7768_dec_rate_values[8] = {
224 8, 16, 32, 64, 128, 256, 512, 1024,
225 };
226
227 /* Decimation rate range for sinc3 filter */
228 static const int ad7768_sinc3_dec_rate_range[3] = {
229 32, 32, 163840,
230 };
231
232 /*
233 * The AD7768-1 supports three primary filter types:
234 * Sinc5, Sinc3, and Wideband.
235 * However, the filter register values can also encode additional parameters
236 * such as decimation rates and 60Hz rejection. This utility array separates
237 * the filter type from these parameters.
238 */
239 static const int ad7768_filter_regval_to_type[] = {
240 [AD7768_FILTER_REGVAL_SINC5] = AD7768_FILTER_SINC5,
241 [AD7768_FILTER_REGVAL_SINC5_X8] = AD7768_FILTER_SINC5,
242 [AD7768_FILTER_REGVAL_SINC5_X16] = AD7768_FILTER_SINC5,
243 [AD7768_FILTER_REGVAL_SINC3] = AD7768_FILTER_SINC3,
244 [AD7768_FILTER_REGVAL_WIDEBAND] = AD7768_FILTER_WIDEBAND,
245 [AD7768_FILTER_REGVAL_SINC3_REJ60] = AD7768_FILTER_SINC3_REJ60,
246 };
247
248 static const char * const ad7768_filter_enum[] = {
249 [AD7768_FILTER_SINC5] = "sinc5",
250 [AD7768_FILTER_SINC3] = "sinc3",
251 [AD7768_FILTER_WIDEBAND] = "wideband",
252 [AD7768_FILTER_SINC3_REJ60] = "sinc3+rej60",
253 };
254
255 static const struct iio_scan_type ad7768_scan_type[] = {
256 [AD7768_SCAN_TYPE_NORMAL] = {
257 .sign = 's',
258 .realbits = 24,
259 .storagebits = 32,
260 .shift = 8,
261 .endianness = IIO_BE,
262 },
263 [AD7768_SCAN_TYPE_HIGH_SPEED] = {
264 .sign = 's',
265 .realbits = 16,
266 .storagebits = 16,
267 .endianness = IIO_BE,
268 },
269 };
270
271 struct ad7768_chip_info {
272 const char *name;
273 const struct iio_chan_spec *channel_spec;
274 int num_channels;
275 const int *pga_gains;
276 int num_pga_modes;
277 int default_pga_mode;
278 int pgia_mode2pin_offset;
279 bool has_pga;
280 bool has_variable_aaf;
281 bool has_vcm_regulator;
282 };
283
284 struct ad7768_state {
285 struct spi_device *spi;
286 struct regmap *regmap;
287 struct regmap *regmap24;
288 int vref_uv;
289 struct regulator_dev *vcm_rdev;
290 unsigned int vcm_output_sel;
291 struct clk *mclk;
292 unsigned int mclk_freq;
293 unsigned int mclk_div;
294 unsigned int oversampling_ratio;
295 enum ad7768_filter_type filter_type;
296 unsigned int samp_freq;
297 unsigned int samp_freq_avail[ARRAY_SIZE(ad7768_mclk_div_rates)];
298 unsigned int samp_freq_avail_len;
299 unsigned int pga_gain_mode;
300 unsigned int aaf_gain;
301 int scale_tbl[ADAQ776X_MAX_GAIN_MODES][2];
302 struct completion completion;
303 struct iio_trigger *trig;
304 struct gpio_descs *pga_gpios;
305 struct gpio_desc *gpio_sync_in;
306 struct gpio_desc *gpio_reset;
307 const char *labels[AD7768_MAX_CHANNELS];
308 struct gpio_chip gpiochip;
309 const struct ad7768_chip_info *chip;
310 bool en_spi_sync;
311 struct mutex pga_lock; /* protect device internal state (PGA) */
312 /*
313 * DMA (thus cache coherency maintenance) may require the
314 * transfer buffers to live in their own cache lines.
315 */
316 union {
317 struct {
318 __be32 chan;
319 aligned_s64 timestamp;
320 } scan;
321 __be32 d32;
322 u8 d8[2];
323 } data __aligned(IIO_DMA_MINALIGN);
324 };
325
326 static const struct regmap_range ad7768_regmap_rd_ranges[] = {
327 regmap_reg_range(AD7768_REG_CHIP_TYPE, AD7768_REG_CHIP_GRADE),
328 regmap_reg_range(AD7768_REG_SCRATCH_PAD, AD7768_REG_SCRATCH_PAD),
329 regmap_reg_range(AD7768_REG_VENDOR_L, AD7768_REG_VENDOR_H),
330 regmap_reg_range(AD7768_REG_INTERFACE_FORMAT, AD7768_REG_GAIN_LO),
331 regmap_reg_range(AD7768_REG_SPI_DIAG_ENABLE, AD7768_REG_DIG_DIAG_ENABLE),
332 regmap_reg_range(AD7768_REG_MASTER_STATUS, AD7768_REG_COEFF_CONTROL),
333 regmap_reg_range(AD7768_REG_ACCESS_KEY, AD7768_REG_ACCESS_KEY),
334 };
335
336 static const struct regmap_access_table ad7768_regmap_rd_table = {
337 .yes_ranges = ad7768_regmap_rd_ranges,
338 .n_yes_ranges = ARRAY_SIZE(ad7768_regmap_rd_ranges),
339 };
340
341 static const struct regmap_range ad7768_regmap_wr_ranges[] = {
342 regmap_reg_range(AD7768_REG_SCRATCH_PAD, AD7768_REG_SCRATCH_PAD),
343 regmap_reg_range(AD7768_REG_INTERFACE_FORMAT, AD7768_REG_GPIO_WRITE),
344 regmap_reg_range(AD7768_REG_OFFSET_HI, AD7768_REG_GAIN_LO),
345 regmap_reg_range(AD7768_REG_SPI_DIAG_ENABLE, AD7768_REG_DIG_DIAG_ENABLE),
346 regmap_reg_range(AD7768_REG_SPI_DIAG_STATUS, AD7768_REG_SPI_DIAG_STATUS),
347 regmap_reg_range(AD7768_REG_COEFF_CONTROL, AD7768_REG_COEFF_CONTROL),
348 regmap_reg_range(AD7768_REG_ACCESS_KEY, AD7768_REG_ACCESS_KEY),
349 };
350
351 static const struct regmap_access_table ad7768_regmap_wr_table = {
352 .yes_ranges = ad7768_regmap_wr_ranges,
353 .n_yes_ranges = ARRAY_SIZE(ad7768_regmap_wr_ranges),
354 };
355
356 static const struct regmap_config ad7768_regmap_config = {
357 .name = "ad7768-1-8",
358 .reg_bits = 8,
359 .val_bits = 8,
360 .read_flag_mask = BIT(6),
361 .rd_table = &ad7768_regmap_rd_table,
362 .wr_table = &ad7768_regmap_wr_table,
363 .max_register = AD7768_REG_ACCESS_KEY,
364 .use_single_write = true,
365 .use_single_read = true,
366 };
367
368 static const struct regmap_range ad7768_regmap24_rd_ranges[] = {
369 regmap_reg_range(AD7768_REG24_ADC_DATA, AD7768_REG24_ADC_DATA),
370 regmap_reg_range(AD7768_REG24_COEFF_DATA, AD7768_REG24_COEFF_DATA),
371 };
372
373 static const struct regmap_access_table ad7768_regmap24_rd_table = {
374 .yes_ranges = ad7768_regmap24_rd_ranges,
375 .n_yes_ranges = ARRAY_SIZE(ad7768_regmap24_rd_ranges),
376 };
377
378 static const struct regmap_range ad7768_regmap24_wr_ranges[] = {
379 regmap_reg_range(AD7768_REG24_COEFF_DATA, AD7768_REG24_COEFF_DATA),
380 };
381
382 static const struct regmap_access_table ad7768_regmap24_wr_table = {
383 .yes_ranges = ad7768_regmap24_wr_ranges,
384 .n_yes_ranges = ARRAY_SIZE(ad7768_regmap24_wr_ranges),
385 };
386
387 static const struct regmap_config ad7768_regmap24_config = {
388 .name = "ad7768-1-24",
389 .reg_bits = 8,
390 .val_bits = 24,
391 .read_flag_mask = BIT(6),
392 .rd_table = &ad7768_regmap24_rd_table,
393 .wr_table = &ad7768_regmap24_wr_table,
394 .max_register = AD7768_REG24_COEFF_DATA,
395 };
396
397 static int ad7768_send_sync_pulse(struct ad7768_state *st)
398 {
399 if (st->en_spi_sync)
400 return regmap_write(st->regmap, AD7768_REG_SYNC_RESET, 0x00);
401
402 /*
403 * The datasheet specifies a minimum SYNC_IN pulse width of 1.5 × Tmclk,
404 * where Tmclk is the MCLK period. The supported MCLK frequencies range
405 * from 0.6 MHz to 17 MHz, which corresponds to a minimum SYNC_IN pulse
406 * width of approximately 2.5 µs in the worst-case scenario (0.6 MHz).
407 *
408 * Add a delay to ensure the pulse width is always sufficient to
409 * trigger synchronization.
410 */
411 gpiod_set_value_cansleep(st->gpio_sync_in, 1);
412 fsleep(3);
413 gpiod_set_value_cansleep(st->gpio_sync_in, 0);
414
415 return 0;
416 }
417
418 static void ad7768_fill_samp_freq_tbl(struct ad7768_state *st)
419 {
420 unsigned int i, samp_freq_avail, freq_filtered;
421 unsigned int len = 0;
422
423 freq_filtered = DIV_ROUND_CLOSEST(st->mclk_freq, st->oversampling_ratio);
424 for (i = 0; i < ARRAY_SIZE(ad7768_mclk_div_rates); i++) {
425 samp_freq_avail = DIV_ROUND_CLOSEST(freq_filtered, ad7768_mclk_div_rates[i]);
426 /* Sampling frequency cannot be lower than the minimum of 50 SPS */
427 if (samp_freq_avail < 50)
428 continue;
429
430 st->samp_freq_avail[len++] = samp_freq_avail;
431 }
432
433 st->samp_freq_avail_len = len;
434 }
435
436 static int ad7768_set_mclk_div(struct ad7768_state *st, unsigned int mclk_div)
437 {
438 unsigned int mclk_div_value;
439
440 mclk_div_value = AD7768_PWR_MCLK_DIV(mclk_div);
441 /*
442 * Set power mode based on mclk_div value.
443 * ECO_MODE is only recommended for MCLK_DIV = 16.
444 */
445 mclk_div_value |= mclk_div > AD7768_MCLK_DIV_16 ?
446 AD7768_PWR_PWRMODE(AD7768_FAST_MODE) :
447 AD7768_PWR_PWRMODE(AD7768_ECO_MODE);
448
449 return regmap_update_bits(st->regmap, AD7768_REG_POWER_CLOCK,
450 AD7768_PWR_MCLK_DIV_MSK | AD7768_PWR_PWRMODE_MSK,
451 mclk_div_value);
452 }
453
454 static int ad7768_set_mode(struct ad7768_state *st,
455 enum ad7768_conv_mode mode)
456 {
457 return regmap_update_bits(st->regmap, AD7768_REG_CONVERSION,
458 AD7768_CONV_MODE_MSK, AD7768_CONV_MODE(mode));
459 }
460
461 static int ad7768_scan_direct(struct iio_dev *indio_dev)
462 {
463 struct ad7768_state *st = iio_priv(indio_dev);
464 int readval, ret;
465
466 reinit_completion(&st->completion);
467
468 ret = wait_for_completion_timeout(&st->completion,
469 msecs_to_jiffies(1000));
470 if (!ret)
471 return -ETIMEDOUT;
472
473 ret = regmap_read(st->regmap24, AD7768_REG24_ADC_DATA, &readval);
474 if (ret)
475 return ret;
476
477 /*
478 * When the decimation rate is set to x8, the ADC data precision is
479 * reduced from 24 bits to 16 bits. Since the AD7768_REG_ADC_DATA
480 * register provides 24-bit data, the precision is reduced by
481 * right-shifting the read value by 8 bits.
482 */
483 if (st->oversampling_ratio == 8)
484 readval >>= 8;
485
486 return readval;
487 }
488
489 static int ad7768_reg_access(struct iio_dev *indio_dev,
490 unsigned int reg,
491 unsigned int writeval,
492 unsigned int *readval)
493 {
494 struct ad7768_state *st = iio_priv(indio_dev);
495 int ret;
496
497 if (!iio_device_claim_direct(indio_dev))
498 return -EBUSY;
499
500 ret = -EINVAL;
501 if (readval) {
502 if (regmap_check_range_table(st->regmap, reg, &ad7768_regmap_rd_table))
503 ret = regmap_read(st->regmap, reg, readval);
504
505 if (regmap_check_range_table(st->regmap24, reg, &ad7768_regmap24_rd_table))
506 ret = regmap_read(st->regmap24, reg, readval);
507
508 } else {
509 if (regmap_check_range_table(st->regmap, reg, &ad7768_regmap_wr_table))
510 ret = regmap_write(st->regmap, reg, writeval);
511
512 if (regmap_check_range_table(st->regmap24, reg, &ad7768_regmap24_wr_table))
513 ret = regmap_write(st->regmap24, reg, writeval);
514
515 }
516
517 iio_device_release_direct(indio_dev);
518
519 return ret;
520 }
521
522 static int ad7768_fill_scale_tbl(struct iio_dev *dev)
523 {
524 struct ad7768_state *st = iio_priv(dev);
525 const struct iio_scan_type *scan_type;
526 int val, val2, tmp0, tmp1, i;
527 struct u32_fract fract;
528 unsigned long n, d;
529 u64 tmp2;
530
531 scan_type = iio_get_current_scan_type(dev, &dev->channels[0]);
532 if (IS_ERR(scan_type)) {
533 dev_err(&st->spi->dev, "Failed to get scan type.\n");
534 return PTR_ERR(scan_type);
535 }
536
537 if (scan_type->sign == 's')
538 val2 = scan_type->realbits - 1;
539 else
540 val2 = scan_type->realbits;
541
542 for (i = 0; i < st->chip->num_pga_modes; i++) {
543 /* Convert gain to a fraction format */
544 fract.numerator = st->chip->pga_gains[i];
545 fract.denominator = MILLI;
546 if (st->chip->has_variable_aaf) {
547 fract.numerator *= ad7768_aaf_gains_bp[st->aaf_gain];
548 fract.denominator *= PERMYRIAD;
549 }
550
551 rational_best_approximation(fract.numerator, fract.denominator,
552 INT_MAX, INT_MAX, &n, &d);
553
554 val = mult_frac(st->vref_uv, d, n);
555 /* Would multiply by NANO here, but value is already in milli */
556 tmp2 = ((u64)val * MICRO) >> val2;
557 tmp0 = div_u64_rem(tmp2, NANO, &tmp1);
558 st->scale_tbl[i][0] = tmp0; /* Integer part */
559 st->scale_tbl[i][1] = abs(tmp1); /* Fractional part */
560 }
561
562 return 0;
563 }
564
565 static int ad7768_set_sinc3_dec_rate(struct ad7768_state *st,
566 unsigned int dec_rate)
567 {
568 unsigned int max_dec_rate;
569 u8 dec_rate_reg[2];
570 u16 regval;
571 int ret;
572
573 /*
574 * Maximum dec_rate is limited by the MCLK_DIV value and by the ODR.
575 * The edge case is for MCLK_DIV = 2, ODR = 50 SPS.
576 * max_dec_rate <= MCLK / (2 * 50)
577 */
578 max_dec_rate = st->mclk_freq / 100;
579 dec_rate = clamp(dec_rate, 32, max_dec_rate);
580 /*
581 * Calculate the equivalent value to sinc3 decimation ratio
582 * to be written on the SINC3_DEC_RATE register:
583 * Value = (DEC_RATE / 32) - 1
584 */
585 dec_rate = DIV_ROUND_UP(dec_rate, 32) - 1;
586
587 /*
588 * The SINC3_DEC_RATE value is a 13-bit value split across two
589 * registers: MSB [12:8] and LSB [7:0]. Prepare the 13-bit value using
590 * FIELD_PREP() and store it with the right endianness in dec_rate_reg.
591 */
592 regval = FIELD_PREP(GENMASK(12, 0), dec_rate);
593 put_unaligned_be16(regval, dec_rate_reg);
594 ret = regmap_bulk_write(st->regmap, AD7768_REG_SINC3_DEC_RATE_MSB,
595 dec_rate_reg, 2);
596 if (ret)
597 return ret;
598
599 st->oversampling_ratio = (dec_rate + 1) * 32;
600
601 return 0;
602 }
603
604 static int ad7768_configure_dig_fil(struct iio_dev *dev,
605 enum ad7768_filter_type filter_type,
606 unsigned int dec_rate)
607 {
608 struct ad7768_state *st = iio_priv(dev);
609 unsigned int dec_rate_idx, dig_filter_regval;
610 int ret;
611
612 switch (filter_type) {
613 case AD7768_FILTER_SINC3:
614 dig_filter_regval = AD7768_DIG_FIL_FIL(AD7768_FILTER_REGVAL_SINC3);
615 break;
616 case AD7768_FILTER_SINC3_REJ60:
617 dig_filter_regval = AD7768_DIG_FIL_FIL(AD7768_FILTER_REGVAL_SINC3) |
618 AD7768_DIG_FIL_EN_60HZ_REJ;
619 break;
620 case AD7768_FILTER_WIDEBAND:
621 /* Skip decimations 8 and 16, not supported by the wideband filter */
622 dec_rate_idx = find_closest(dec_rate, &ad7768_dec_rate_values[2],
623 ARRAY_SIZE(ad7768_dec_rate_values) - 2);
624 dig_filter_regval = AD7768_DIG_FIL_FIL(AD7768_FILTER_REGVAL_WIDEBAND) |
625 AD7768_DIG_FIL_DEC_RATE(dec_rate_idx);
626 /* Correct the index offset */
627 dec_rate_idx += 2;
628 break;
629 case AD7768_FILTER_SINC5:
630 dec_rate_idx = find_closest(dec_rate, ad7768_dec_rate_values,
631 ARRAY_SIZE(ad7768_dec_rate_values));
632
633 /*
634 * Decimations 8 (idx 0) and 16 (idx 1) are set in the
635 * FILTER[6:4] field. The other decimations are set in the
636 * DEC_RATE[2:0] field, and the idx needs to be offsetted by two.
637 */
638 if (dec_rate_idx == 0)
639 dig_filter_regval = AD7768_DIG_FIL_FIL(AD7768_FILTER_REGVAL_SINC5_X8);
640 else if (dec_rate_idx == 1)
641 dig_filter_regval = AD7768_DIG_FIL_FIL(AD7768_FILTER_REGVAL_SINC5_X16);
642 else
643 dig_filter_regval = AD7768_DIG_FIL_FIL(AD7768_FILTER_REGVAL_SINC5) |
644 AD7768_DIG_FIL_DEC_RATE(dec_rate_idx - 2);
645 break;
646 }
647
648 ret = regmap_write(st->regmap, AD7768_REG_DIGITAL_FILTER, dig_filter_regval);
649 if (ret)
650 return ret;
651
652 st->filter_type = filter_type;
653 /*
654 * The decimation for SINC3 filters are configured in different
655 * registers.
656 */
657 if (filter_type == AD7768_FILTER_SINC3 ||
658 filter_type == AD7768_FILTER_SINC3_REJ60) {
659 ret = ad7768_set_sinc3_dec_rate(st, dec_rate);
660 if (ret)
661 return ret;
662 } else {
663 st->oversampling_ratio = ad7768_dec_rate_values[dec_rate_idx];
664 }
665
666 /* Update scale table: scale values vary according to the precision */
667 ret = ad7768_fill_scale_tbl(dev);
668 if (ret)
669 return ret;
670
671 ad7768_fill_samp_freq_tbl(st);
672
673 /* A sync-in pulse is required after every configuration change */
674 return ad7768_send_sync_pulse(st);
675 }
676
677 static int ad7768_setup_pga(struct device *dev, struct ad7768_state *st)
678 {
679 st->pga_gpios = devm_gpiod_get_array(dev, "pga", GPIOD_OUT_LOW);
680 if (IS_ERR(st->pga_gpios))
681 return dev_err_probe(dev, PTR_ERR(st->pga_gpios),
682 "Failed to get PGA gpios.\n");
683
684 if (st->pga_gpios->ndescs != ADAQ7768_PGA_PINS)
685 return dev_err_probe(dev, -EINVAL,
686 "Expected %d GPIOs for PGA control.\n",
687 ADAQ7768_PGA_PINS);
688 return 0;
689 }
690
691 static int ad7768_calc_pga_gain(struct ad7768_state *st, int gain_int,
692 int gain_fract, int precision)
693 {
694 u64 gain_nano;
695 u32 tmp;
696
697 gain_nano = gain_int * NANO + gain_fract;
698 gain_nano = clamp(gain_nano, 0, ADAQ776X_GAIN_MAX_NANO);
699 tmp = DIV_ROUND_CLOSEST_ULL(gain_nano << precision, NANO);
700 gain_nano = DIV_ROUND_CLOSEST(st->vref_uv, tmp);
701 if (st->chip->has_variable_aaf)
702 gain_nano = DIV_ROUND_CLOSEST_ULL(gain_nano * PERMYRIAD,
703 ad7768_aaf_gains_bp[st->aaf_gain]);
704
705 return find_closest(gain_nano, st->chip->pga_gains,
706 (int)st->chip->num_pga_modes);
707 }
708
709 static int ad7768_set_pga_gain(struct ad7768_state *st,
710 int gain_mode)
711 {
712 int pgia_pins_value = abs(gain_mode - st->chip->pgia_mode2pin_offset);
713 DECLARE_BITMAP(bitmap, ADAQ7768_PGA_PINS) = { };
714 int ret;
715
716 guard(mutex)(&st->pga_lock);
717
718 bitmap_write(bitmap, pgia_pins_value, 0, ADAQ7768_PGA_PINS);
719 ret = gpiod_multi_set_value_cansleep(st->pga_gpios, bitmap);
720 if (ret)
721 return ret;
722
723 st->pga_gain_mode = gain_mode;
724
725 return 0;
726 }
727
728 static int ad7768_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
729 {
730 struct iio_dev *indio_dev = gpiochip_get_data(chip);
731 struct ad7768_state *st = iio_priv(indio_dev);
732 int ret;
733
734 if (!iio_device_claim_direct(indio_dev))
735 return -EBUSY;
736
737 ret = regmap_clear_bits(st->regmap, AD7768_REG_GPIO_CONTROL,
738 BIT(offset));
739 iio_device_release_direct(indio_dev);
740
741 return ret;
742 }
743
744 static int ad7768_gpio_direction_output(struct gpio_chip *chip,
745 unsigned int offset, int value)
746 {
747 struct iio_dev *indio_dev = gpiochip_get_data(chip);
748 struct ad7768_state *st = iio_priv(indio_dev);
749 int ret;
750
751 if (!iio_device_claim_direct(indio_dev))
752 return -EBUSY;
753
754 ret = regmap_set_bits(st->regmap, AD7768_REG_GPIO_CONTROL,
755 BIT(offset));
756 iio_device_release_direct(indio_dev);
757
758 return ret;
759 }
760
761 static int ad7768_gpio_get(struct gpio_chip *chip, unsigned int offset)
762 {
763 struct iio_dev *indio_dev = gpiochip_get_data(chip);
764 struct ad7768_state *st = iio_priv(indio_dev);
765 unsigned int val;
766 int ret;
767
768 if (!iio_device_claim_direct(indio_dev))
769 return -EBUSY;
770
771 ret = regmap_read(st->regmap, AD7768_REG_GPIO_CONTROL, &val);
772 if (ret)
773 goto err_release;
774
775 /*
776 * If the GPIO is configured as an output, read the current value from
777 * AD7768_REG_GPIO_WRITE. Otherwise, read the input value from
778 * AD7768_REG_GPIO_READ.
779 */
780 if (val & BIT(offset))
781 ret = regmap_read(st->regmap, AD7768_REG_GPIO_WRITE, &val);
782 else
783 ret = regmap_read(st->regmap, AD7768_REG_GPIO_READ, &val);
784 if (ret)
785 goto err_release;
786
787 ret = !!(val & BIT(offset));
788 err_release:
789 iio_device_release_direct(indio_dev);
790
791 return ret;
792 }
793
794 static int ad7768_gpio_set(struct gpio_chip *chip, unsigned int offset, int value)
795 {
796 struct iio_dev *indio_dev = gpiochip_get_data(chip);
797 struct ad7768_state *st = iio_priv(indio_dev);
798 unsigned int val;
799 int ret;
800
801 if (!iio_device_claim_direct(indio_dev))
802 return -EBUSY;
803
804 ret = regmap_read(st->regmap, AD7768_REG_GPIO_CONTROL, &val);
805 if (ret)
806 goto err_release;
807
808 if (val & BIT(offset))
809 ret = regmap_assign_bits(st->regmap, AD7768_REG_GPIO_WRITE,
810 BIT(offset), value);
811
812 err_release:
813 iio_device_release_direct(indio_dev);
814
815 return ret;
816 }
817
818 static int ad7768_gpio_init(struct iio_dev *indio_dev)
819 {
820 struct ad7768_state *st = iio_priv(indio_dev);
821 int ret;
822
823 ret = regmap_write(st->regmap, AD7768_REG_GPIO_CONTROL,
824 AD7768_GPIO_UNIVERSAL_EN);
825 if (ret)
826 return ret;
827
828 st->gpiochip = (struct gpio_chip) {
829 .label = "ad7768_1_gpios",
830 .base = -1,
831 .ngpio = 4,
832 .parent = &st->spi->dev,
833 .can_sleep = true,
834 .direction_input = ad7768_gpio_direction_input,
835 .direction_output = ad7768_gpio_direction_output,
836 .get = ad7768_gpio_get,
837 .set = ad7768_gpio_set,
838 .owner = THIS_MODULE,
839 };
840
841 return devm_gpiochip_add_data(&st->spi->dev, &st->gpiochip, indio_dev);
842 }
843
844 static int ad7768_set_freq(struct ad7768_state *st,
845 unsigned int freq)
846 {
847 unsigned int idx, mclk_div;
848 int ret;
849
850 freq = clamp(freq, 50, 1024000);
851
852 mclk_div = DIV_ROUND_CLOSEST(st->mclk_freq, freq * st->oversampling_ratio);
853 /* Find the closest match for the desired sampling frequency */
854 idx = find_closest_descending(mclk_div, ad7768_mclk_div_rates,
855 ARRAY_SIZE(ad7768_mclk_div_rates));
856 /* Set both the mclk_div and pwrmode */
857 ret = ad7768_set_mclk_div(st, idx);
858 if (ret)
859 return ret;
860
861 st->samp_freq = DIV_ROUND_CLOSEST(st->mclk_freq,
862 ad7768_mclk_div_rates[idx] * st->oversampling_ratio);
863
864 /* A sync-in pulse is required after every configuration change */
865 return ad7768_send_sync_pulse(st);
866 }
867
868 static int ad7768_set_filter_type_attr(struct iio_dev *dev,
869 const struct iio_chan_spec *chan,
870 unsigned int filter)
871 {
872 struct ad7768_state *st = iio_priv(dev);
873 int ret;
874
875 ret = ad7768_configure_dig_fil(dev, filter, st->oversampling_ratio);
876 if (ret)
877 return ret;
878
879 /* Update sampling frequency */
880 return ad7768_set_freq(st, st->samp_freq);
881 }
882
883 static int ad7768_get_filter_type_attr(struct iio_dev *dev,
884 const struct iio_chan_spec *chan)
885 {
886 struct ad7768_state *st = iio_priv(dev);
887 int ret;
888 unsigned int mode, mask;
889
890 ret = regmap_read(st->regmap, AD7768_REG_DIGITAL_FILTER, &mode);
891 if (ret)
892 return ret;
893
894 mask = AD7768_DIG_FIL_EN_60HZ_REJ | AD7768_DIG_FIL_FIL_MSK;
895 /* From the register value, get the corresponding filter type */
896 return ad7768_filter_regval_to_type[FIELD_GET(mask, mode)];
897 }
898
899 static int ad7768_update_dec_rate(struct iio_dev *dev, unsigned int dec_rate)
900 {
901 struct ad7768_state *st = iio_priv(dev);
902 int ret;
903
904 ret = ad7768_configure_dig_fil(dev, st->filter_type, dec_rate);
905 if (ret)
906 return ret;
907
908 /* Update sampling frequency */
909 return ad7768_set_freq(st, st->samp_freq);
910 }
911
912 static const struct iio_enum ad7768_filter_type_iio_enum = {
913 .items = ad7768_filter_enum,
914 .num_items = ARRAY_SIZE(ad7768_filter_enum),
915 .set = ad7768_set_filter_type_attr,
916 .get = ad7768_get_filter_type_attr,
917 };
918
919 static const struct iio_chan_spec_ext_info ad7768_ext_info[] = {
920 IIO_ENUM("filter_type", IIO_SHARED_BY_ALL, &ad7768_filter_type_iio_enum),
921 IIO_ENUM_AVAILABLE("filter_type", IIO_SHARED_BY_ALL, &ad7768_filter_type_iio_enum),
922 { }
923 };
924
925 #define AD7768_CHAN(_idx, _msk_avail) \
926 { \
927 .type = IIO_VOLTAGE, \
928 .info_mask_separate_available = _msk_avail, \
929 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
930 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
931 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY) | \
932 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
933 .info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
934 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
935 .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
936 .ext_info = ad7768_ext_info, \
937 .indexed = 1, \
938 .channel = _idx, \
939 .scan_index = _idx, \
940 .has_ext_scan_type = 1, \
941 .ext_scan_type = ad7768_scan_type, \
942 .num_ext_scan_type = ARRAY_SIZE(ad7768_scan_type), \
943 }
944
945 static const struct iio_chan_spec ad7768_channels[] = {
946 AD7768_CHAN(0, 0),
947 };
948
949 static const struct iio_chan_spec adaq776x_channels[] = {
950 AD7768_CHAN(0, BIT(IIO_CHAN_INFO_SCALE)),
951 };
952
953 static int ad7768_read_raw(struct iio_dev *indio_dev,
954 struct iio_chan_spec const *chan,
955 int *val, int *val2, long info)
956 {
957 struct ad7768_state *st = iio_priv(indio_dev);
958 const struct iio_scan_type *scan_type;
959 int ret, temp;
960
961 scan_type = iio_get_current_scan_type(indio_dev, chan);
962 if (IS_ERR(scan_type))
963 return PTR_ERR(scan_type);
964
965 switch (info) {
966 case IIO_CHAN_INFO_RAW:
967 if (!iio_device_claim_direct(indio_dev))
968 return -EBUSY;
969
970 ret = ad7768_scan_direct(indio_dev);
971
972 iio_device_release_direct(indio_dev);
973 if (ret < 0)
974 return ret;
975 *val = sign_extend32(ret, scan_type->realbits - 1);
976
977 return IIO_VAL_INT;
978
979 case IIO_CHAN_INFO_SCALE:
980 if (st->chip->has_pga) {
981 guard(mutex)(&st->pga_lock);
982
983 *val = st->scale_tbl[st->pga_gain_mode][0];
984 *val2 = st->scale_tbl[st->pga_gain_mode][1];
985 return IIO_VAL_INT_PLUS_NANO;
986 }
987
988 temp = (st->vref_uv * 2) / 1000;
989 if (st->chip->has_variable_aaf)
990 temp = (temp * PERMYRIAD) / ad7768_aaf_gains_bp[st->aaf_gain];
991
992 *val = temp;
993 *val2 = scan_type->realbits;
994
995 return IIO_VAL_FRACTIONAL_LOG2;
996
997 case IIO_CHAN_INFO_SAMP_FREQ:
998 *val = st->samp_freq;
999
1000 return IIO_VAL_INT;
1001
1002 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
1003 *val = st->oversampling_ratio;
1004
1005 return IIO_VAL_INT;
1006
1007 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
1008 temp = st->samp_freq * ad7768_filter_3db_odr_multiplier[st->filter_type];
1009 *val = DIV_ROUND_CLOSEST(temp, MILLI);
1010
1011 return IIO_VAL_INT;
1012 }
1013
1014 return -EINVAL;
1015 }
1016
1017 static int ad7768_read_avail(struct iio_dev *indio_dev,
1018 struct iio_chan_spec const *chan,
1019 const int **vals, int *type, int *length,
1020 long info)
1021 {
1022 struct ad7768_state *st = iio_priv(indio_dev);
1023 unsigned int shift;
1024
1025 switch (info) {
1026 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
1027 /*
1028 * Sinc3 filter allows a wider range of OSR values, so show
1029 * the available values in range format.
1030 */
1031 if (st->filter_type == AD7768_FILTER_SINC3 ||
1032 st->filter_type == AD7768_FILTER_SINC3_REJ60) {
1033 *vals = (int *)ad7768_sinc3_dec_rate_range;
1034 *type = IIO_VAL_INT;
1035 return IIO_AVAIL_RANGE;
1036 }
1037
1038 shift = st->filter_type == AD7768_FILTER_SINC5 ? 0 : 2;
1039 *vals = (int *)&ad7768_dec_rate_values[shift];
1040 *length = ARRAY_SIZE(ad7768_dec_rate_values) - shift;
1041 *type = IIO_VAL_INT;
1042 return IIO_AVAIL_LIST;
1043 case IIO_CHAN_INFO_SAMP_FREQ:
1044 *vals = (int *)st->samp_freq_avail;
1045 *length = st->samp_freq_avail_len;
1046 *type = IIO_VAL_INT;
1047 return IIO_AVAIL_LIST;
1048 case IIO_CHAN_INFO_SCALE:
1049 *vals = (int *)st->scale_tbl;
1050 *length = st->chip->num_pga_modes * 2;
1051 *type = IIO_VAL_INT_PLUS_NANO;
1052 return IIO_AVAIL_LIST;
1053 default:
1054 return -EINVAL;
1055 }
1056 }
1057
1058 static int ad7768_write_raw_get_fmt(struct iio_dev *indio_dev,
1059 struct iio_chan_spec const *chan, long mask)
1060 {
1061 switch (mask) {
1062 case IIO_CHAN_INFO_SCALE:
1063 return IIO_VAL_INT_PLUS_NANO;
1064 default:
1065 return IIO_VAL_INT_PLUS_MICRO;
1066 }
1067 }
1068
1069 static int ad7768_write_raw(struct iio_dev *indio_dev,
1070 struct iio_chan_spec const *chan,
1071 int val, int val2, long info)
1072 {
1073 struct ad7768_state *st = iio_priv(indio_dev);
1074 const struct iio_scan_type *scan_type;
1075 int ret;
1076
1077 scan_type = iio_get_current_scan_type(indio_dev, chan);
1078 if (IS_ERR(scan_type))
1079 return PTR_ERR(scan_type);
1080
1081 switch (info) {
1082 case IIO_CHAN_INFO_SAMP_FREQ:
1083 if (!iio_device_claim_direct(indio_dev))
1084 return -EBUSY;
1085
1086 ret = ad7768_set_freq(st, val);
1087 iio_device_release_direct(indio_dev);
1088 return ret;
1089 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
1090 if (!iio_device_claim_direct(indio_dev))
1091 return -EBUSY;
1092
1093 ret = ad7768_update_dec_rate(indio_dev, val);
1094 iio_device_release_direct(indio_dev);
1095 return ret;
1096 case IIO_CHAN_INFO_SCALE: {
1097 int gain_mode;
1098
1099 if (!st->chip->has_pga)
1100 return -EOPNOTSUPP;
1101
1102 if (scan_type->sign == 's')
1103 gain_mode = ad7768_calc_pga_gain(st, val, val2,
1104 scan_type->realbits - 1);
1105 else
1106 gain_mode = ad7768_calc_pga_gain(st, val, val2,
1107 scan_type->realbits);
1108
1109 return ad7768_set_pga_gain(st, gain_mode);
1110 }
1111 default:
1112 return -EINVAL;
1113 }
1114 }
1115
1116 static int ad7768_read_label(struct iio_dev *indio_dev,
1117 const struct iio_chan_spec *chan, char *label)
1118 {
1119 struct ad7768_state *st = iio_priv(indio_dev);
1120
1121 return sysfs_emit(label, "%s\n", st->labels[chan->channel]);
1122 }
1123
1124 static int ad7768_get_current_scan_type(const struct iio_dev *indio_dev,
1125 const struct iio_chan_spec *chan)
1126 {
1127 struct ad7768_state *st = iio_priv(indio_dev);
1128
1129 return st->oversampling_ratio == 8 ?
1130 AD7768_SCAN_TYPE_HIGH_SPEED : AD7768_SCAN_TYPE_NORMAL;
1131 }
1132
1133 static const struct iio_info ad7768_info = {
1134 .read_raw = &ad7768_read_raw,
1135 .read_avail = &ad7768_read_avail,
1136 .write_raw = &ad7768_write_raw,
1137 .write_raw_get_fmt = &ad7768_write_raw_get_fmt,
1138 .read_label = ad7768_read_label,
1139 .get_current_scan_type = &ad7768_get_current_scan_type,
1140 .debugfs_reg_access = &ad7768_reg_access,
1141 };
1142
1143 static struct fwnode_handle *
1144 ad7768_fwnode_find_reference_args(const struct fwnode_handle *fwnode,
1145 const char *name, const char *nargs_prop,
1146 unsigned int nargs, unsigned int index,
1147 struct fwnode_reference_args *args)
1148 {
1149 int ret;
1150
1151 ret = fwnode_property_get_reference_args(fwnode, name, nargs_prop,
1152 nargs, index, args);
1153 return ret ? ERR_PTR(ret) : args->fwnode;
1154 }
1155
1156 static int ad7768_trigger_sources_sync_setup(struct device *dev,
1157 struct fwnode_handle *fwnode,
1158 struct ad7768_state *st)
1159 {
1160 struct fwnode_reference_args args;
1161
1162 struct fwnode_handle *ref __free(fwnode_handle) =
1163 ad7768_fwnode_find_reference_args(fwnode, "trigger-sources",
1164 "#trigger-source-cells", 0,
1165 AD7768_TRIGGER_SOURCE_SYNC_IDX,
1166 &args);
1167 if (IS_ERR(ref))
1168 return PTR_ERR(ref);
1169
1170 ref = args.fwnode;
1171 /* First, try getting the GPIO trigger source */
1172 if (fwnode_device_is_compatible(ref, "gpio-trigger")) {
1173 st->gpio_sync_in = devm_fwnode_gpiod_get_index(dev, ref, NULL, 0,
1174 GPIOD_OUT_LOW,
1175 "sync-in");
1176 return PTR_ERR_OR_ZERO(st->gpio_sync_in);
1177 }
1178
1179 /*
1180 * TODO: Support the other cases when we have a trigger subsystem
1181 * to reliably handle other types of devices as trigger sources.
1182 *
1183 * For now, return an error message. For self triggering, omit the
1184 * trigger-sources property.
1185 */
1186 return dev_err_probe(dev, -EOPNOTSUPP, "Invalid synchronization trigger source\n");
1187 }
1188
1189 static int ad7768_trigger_sources_get_sync(struct device *dev,
1190 struct ad7768_state *st)
1191 {
1192 struct fwnode_handle *fwnode = dev_fwnode(dev);
1193
1194 /*
1195 * The AD7768-1 allows two primary methods for driving the SYNC_IN pin
1196 * to synchronize one or more devices:
1197 * 1. Using an external GPIO.
1198 * 2. Using a SPI command, where the SYNC_OUT pin generates a
1199 * synchronization pulse that drives the SYNC_IN pin.
1200 */
1201 if (fwnode_property_present(fwnode, "trigger-sources"))
1202 return ad7768_trigger_sources_sync_setup(dev, fwnode, st);
1203
1204 /*
1205 * In the absence of trigger-sources property, enable self
1206 * synchronization over SPI (SYNC_OUT).
1207 */
1208 st->en_spi_sync = true;
1209
1210 return 0;
1211 }
1212
1213 static int ad7768_setup(struct iio_dev *indio_dev)
1214 {
1215 struct ad7768_state *st = iio_priv(indio_dev);
1216 int ret;
1217
1218 st->gpio_reset = devm_gpiod_get_optional(&st->spi->dev, "reset",
1219 GPIOD_OUT_HIGH);
1220 if (IS_ERR(st->gpio_reset))
1221 return PTR_ERR(st->gpio_reset);
1222
1223 if (st->gpio_reset) {
1224 fsleep(10);
1225 gpiod_set_value_cansleep(st->gpio_reset, 0);
1226 fsleep(200);
1227 } else {
1228 /*
1229 * Two writes to the SPI_RESET[1:0] bits are required to initiate
1230 * a software reset. The bits must first be set to 11, and then
1231 * to 10. When the sequence is detected, the reset occurs.
1232 * See the datasheet, page 70.
1233 */
1234 ret = regmap_write(st->regmap, AD7768_REG_SYNC_RESET, 0x3);
1235 if (ret)
1236 return ret;
1237
1238 ret = regmap_write(st->regmap, AD7768_REG_SYNC_RESET, 0x2);
1239 if (ret)
1240 return ret;
1241 }
1242
1243 ret = ad7768_set_mode(st, AD7768_CONTINUOUS);
1244 if (ret)
1245 return ret;
1246
1247 /* For backwards compatibility, try the adi,sync-in-gpios property */
1248 st->gpio_sync_in = devm_gpiod_get_optional(&st->spi->dev, "adi,sync-in",
1249 GPIOD_OUT_LOW);
1250 if (IS_ERR(st->gpio_sync_in))
1251 return PTR_ERR(st->gpio_sync_in);
1252
1253 /*
1254 * If the synchronization is not defined by adi,sync-in-gpios, try the
1255 * trigger-sources.
1256 */
1257 if (!st->gpio_sync_in) {
1258 ret = ad7768_trigger_sources_get_sync(&st->spi->dev, st);
1259 if (ret)
1260 return ret;
1261 }
1262
1263 /* Only create a Chip GPIO if flagged for it */
1264 if (device_property_read_bool(&st->spi->dev, "gpio-controller")) {
1265 ret = ad7768_gpio_init(indio_dev);
1266 if (ret)
1267 return ret;
1268 }
1269
1270 /*
1271 * Set Default Digital Filter configuration:
1272 * SINC5 filter with x32 Decimation rate
1273 */
1274 ret = ad7768_configure_dig_fil(indio_dev, AD7768_FILTER_SINC5, 32);
1275 if (ret)
1276 return ret;
1277
1278 /* Set the default sampling frequency to 32000 kSPS */
1279 return ad7768_set_freq(st, 32000);
1280 }
1281
1282 static irqreturn_t ad7768_trigger_handler(int irq, void *p)
1283 {
1284 struct iio_poll_func *pf = p;
1285 struct iio_dev *indio_dev = pf->indio_dev;
1286 struct ad7768_state *st = iio_priv(indio_dev);
1287 const struct iio_scan_type *scan_type;
1288 int ret;
1289
1290 scan_type = iio_get_current_scan_type(indio_dev, &indio_dev->channels[0]);
1291 if (IS_ERR(scan_type))
1292 goto out;
1293
1294 ret = spi_read(st->spi, &st->data.scan.chan,
1295 BITS_TO_BYTES(scan_type->realbits));
1296 if (ret < 0)
1297 goto out;
1298
1299 iio_push_to_buffers_with_ts(indio_dev, &st->data.scan,
1300 sizeof(st->data.scan),
1301 iio_get_time_ns(indio_dev));
1302
1303 out:
1304 iio_trigger_notify_done(indio_dev->trig);
1305
1306 return IRQ_HANDLED;
1307 }
1308
1309 static irqreturn_t ad7768_interrupt(int irq, void *dev_id)
1310 {
1311 struct iio_dev *indio_dev = dev_id;
1312 struct ad7768_state *st = iio_priv(indio_dev);
1313
1314 if (iio_buffer_enabled(indio_dev))
1315 iio_trigger_poll(st->trig);
1316 else
1317 complete(&st->completion);
1318
1319 return IRQ_HANDLED;
1320 };
1321
1322 static int ad7768_buffer_postenable(struct iio_dev *indio_dev)
1323 {
1324 struct ad7768_state *st = iio_priv(indio_dev);
1325
1326 /*
1327 * Write a 1 to the LSB of the INTERFACE_FORMAT register to enter
1328 * continuous read mode. Subsequent data reads do not require an
1329 * initial 8-bit write to query the ADC_DATA register.
1330 */
1331 return regmap_write(st->regmap, AD7768_REG_INTERFACE_FORMAT, 0x01);
1332 }
1333
1334 static int ad7768_buffer_predisable(struct iio_dev *indio_dev)
1335 {
1336 struct ad7768_state *st = iio_priv(indio_dev);
1337 unsigned int unused;
1338
1339 /*
1340 * To exit continuous read mode, perform a single read of the ADC_DATA
1341 * reg (0x2C), which allows further configuration of the device.
1342 */
1343 return regmap_read(st->regmap24, AD7768_REG24_ADC_DATA, &unused);
1344 }
1345
1346 static const struct iio_buffer_setup_ops ad7768_buffer_ops = {
1347 .postenable = &ad7768_buffer_postenable,
1348 .predisable = &ad7768_buffer_predisable,
1349 };
1350
1351 static const struct iio_trigger_ops ad7768_trigger_ops = {
1352 .validate_device = iio_trigger_validate_own_device,
1353 };
1354
1355 static int ad7768_set_channel_label(struct iio_dev *indio_dev,
1356 int num_channels)
1357 {
1358 struct ad7768_state *st = iio_priv(indio_dev);
1359 struct device *device = indio_dev->dev.parent;
1360 const char *label;
1361 int crt_ch = 0;
1362
1363 device_for_each_child_node_scoped(device, child) {
1364 if (fwnode_property_read_u32(child, "reg", &crt_ch))
1365 continue;
1366
1367 if (crt_ch >= num_channels)
1368 continue;
1369
1370 if (fwnode_property_read_string(child, "label", &label))
1371 continue;
1372
1373 st->labels[crt_ch] = label;
1374 }
1375
1376 return 0;
1377 }
1378
1379 static int ad7768_triggered_buffer_alloc(struct iio_dev *indio_dev)
1380 {
1381 struct ad7768_state *st = iio_priv(indio_dev);
1382 int ret;
1383
1384 st->trig = devm_iio_trigger_alloc(indio_dev->dev.parent, "%s-dev%d",
1385 indio_dev->name,
1386 iio_device_id(indio_dev));
1387 if (!st->trig)
1388 return -ENOMEM;
1389
1390 st->trig->ops = &ad7768_trigger_ops;
1391 iio_trigger_set_drvdata(st->trig, indio_dev);
1392 ret = devm_iio_trigger_register(indio_dev->dev.parent, st->trig);
1393 if (ret)
1394 return ret;
1395
1396 indio_dev->trig = iio_trigger_get(st->trig);
1397
1398 return devm_iio_triggered_buffer_setup(indio_dev->dev.parent, indio_dev,
1399 &iio_pollfunc_store_time,
1400 &ad7768_trigger_handler,
1401 &ad7768_buffer_ops);
1402 }
1403
1404 static int ad7768_vcm_enable(struct regulator_dev *rdev)
1405 {
1406 struct iio_dev *indio_dev = rdev_get_drvdata(rdev);
1407 struct ad7768_state *st = iio_priv(indio_dev);
1408 int ret, regval;
1409
1410 if (!iio_device_claim_direct(indio_dev))
1411 return -EBUSY;
1412
1413 /* To enable, set the last selected output */
1414 regval = AD7768_REG_ANALOG2_VCM(st->vcm_output_sel + 1);
1415 ret = regmap_update_bits(st->regmap, AD7768_REG_ANALOG2,
1416 AD7768_REG_ANALOG2_VCM_MSK, regval);
1417 iio_device_release_direct(indio_dev);
1418
1419 return ret;
1420 }
1421
1422 static int ad7768_vcm_disable(struct regulator_dev *rdev)
1423 {
1424 struct iio_dev *indio_dev = rdev_get_drvdata(rdev);
1425 struct ad7768_state *st = iio_priv(indio_dev);
1426 int ret;
1427
1428 if (!iio_device_claim_direct(indio_dev))
1429 return -EBUSY;
1430
1431 ret = regmap_update_bits(st->regmap, AD7768_REG_ANALOG2,
1432 AD7768_REG_ANALOG2_VCM_MSK, AD7768_VCM_OFF);
1433 iio_device_release_direct(indio_dev);
1434
1435 return ret;
1436 }
1437
1438 static int ad7768_vcm_is_enabled(struct regulator_dev *rdev)
1439 {
1440 struct iio_dev *indio_dev = rdev_get_drvdata(rdev);
1441 struct ad7768_state *st = iio_priv(indio_dev);
1442 int ret, val;
1443
1444 if (!iio_device_claim_direct(indio_dev))
1445 return -EBUSY;
1446
1447 ret = regmap_read(st->regmap, AD7768_REG_ANALOG2, &val);
1448 iio_device_release_direct(indio_dev);
1449 if (ret)
1450 return ret;
1451
1452 return FIELD_GET(AD7768_REG_ANALOG2_VCM_MSK, val) != AD7768_VCM_OFF;
1453 }
1454
1455 static int ad7768_set_voltage_sel(struct regulator_dev *rdev,
1456 unsigned int selector)
1457 {
1458 unsigned int regval = AD7768_REG_ANALOG2_VCM(selector + 1);
1459 struct iio_dev *indio_dev = rdev_get_drvdata(rdev);
1460 struct ad7768_state *st = iio_priv(indio_dev);
1461 int ret;
1462
1463 if (!iio_device_claim_direct(indio_dev))
1464 return -EBUSY;
1465
1466 ret = regmap_update_bits(st->regmap, AD7768_REG_ANALOG2,
1467 AD7768_REG_ANALOG2_VCM_MSK, regval);
1468 iio_device_release_direct(indio_dev);
1469 if (ret)
1470 return ret;
1471
1472 st->vcm_output_sel = selector;
1473
1474 return 0;
1475 }
1476
1477 static int ad7768_get_voltage_sel(struct regulator_dev *rdev)
1478 {
1479 struct iio_dev *indio_dev = rdev_get_drvdata(rdev);
1480 struct ad7768_state *st = iio_priv(indio_dev);
1481 int ret, val;
1482
1483 if (!iio_device_claim_direct(indio_dev))
1484 return -EBUSY;
1485
1486 ret = regmap_read(st->regmap, AD7768_REG_ANALOG2, &val);
1487 iio_device_release_direct(indio_dev);
1488 if (ret)
1489 return ret;
1490
1491 val = FIELD_GET(AD7768_REG_ANALOG2_VCM_MSK, val);
1492
1493 return clamp(val, 1, rdev->desc->n_voltages) - 1;
1494 }
1495
1496 static const struct regulator_ops vcm_regulator_ops = {
1497 .enable = ad7768_vcm_enable,
1498 .disable = ad7768_vcm_disable,
1499 .is_enabled = ad7768_vcm_is_enabled,
1500 .list_voltage = regulator_list_voltage_table,
1501 .set_voltage_sel = ad7768_set_voltage_sel,
1502 .get_voltage_sel = ad7768_get_voltage_sel,
1503 };
1504
1505 static const unsigned int vcm_voltage_table[] = {
1506 2500000,
1507 2050000,
1508 1650000,
1509 1900000,
1510 1100000,
1511 900000,
1512 };
1513
1514 static const struct regulator_desc vcm_desc = {
1515 .name = "ad7768-1-vcm",
1516 .of_match = "vcm-output",
1517 .regulators_node = "regulators",
1518 .n_voltages = ARRAY_SIZE(vcm_voltage_table),
1519 .volt_table = vcm_voltage_table,
1520 .ops = &vcm_regulator_ops,
1521 .type = REGULATOR_VOLTAGE,
1522 .owner = THIS_MODULE,
1523 };
1524
1525 static int ad7768_register_vcm_regulator(struct device *dev,
1526 struct ad7768_state *st,
1527 struct iio_dev *indio_dev)
1528 {
1529 struct regulator_config config = {
1530 .dev = dev,
1531 .driver_data = indio_dev,
1532 };
1533 int ret;
1534
1535 /* Disable the regulator before registering it */
1536 ret = regmap_update_bits(st->regmap, AD7768_REG_ANALOG2,
1537 AD7768_REG_ANALOG2_VCM_MSK, AD7768_VCM_OFF);
1538 if (ret)
1539 return ret;
1540
1541 st->vcm_rdev = devm_regulator_register(dev, &vcm_desc, &config);
1542 if (IS_ERR(st->vcm_rdev))
1543 return dev_err_probe(dev, PTR_ERR(st->vcm_rdev),
1544 "failed to register VCM regulator\n");
1545
1546 return 0;
1547 }
1548
1549 static int ad7768_parse_aaf_gain(struct device *dev, struct ad7768_state *st)
1550 {
1551 u32 val;
1552 int ret;
1553
1554 ret = device_property_read_u32(dev, "adi,aaf-gain-bp", &val);
1555 if (ret == -EINVAL) {
1556 /* If controllable, use default */
1557 if (st->chip->has_variable_aaf)
1558 st->aaf_gain = AD7768_AAF_IN1;
1559 return 0;
1560 }
1561 if (ret)
1562 return dev_err_probe(dev, ret, "Failed to get AAF gain value\n");
1563
1564 if (!st->chip->has_variable_aaf)
1565 return dev_err_probe(dev, -EOPNOTSUPP,
1566 "AAF gain provided, but not supported for %s\n", st->chip->name);
1567
1568 switch (val) {
1569 case 10000:
1570 st->aaf_gain = AD7768_AAF_IN1;
1571 break;
1572 case 3640:
1573 st->aaf_gain = AD7768_AAF_IN2;
1574 break;
1575 case 1430:
1576 st->aaf_gain = AD7768_AAF_IN3;
1577 break;
1578 default:
1579 return dev_err_probe(dev, -EINVAL, "Invalid firmware provided AAF gain\n");
1580 }
1581
1582 return 0;
1583 }
1584
1585 static const struct ad7768_chip_info ad7768_chip_info = {
1586 .name = "ad7768-1",
1587 .channel_spec = ad7768_channels,
1588 .num_channels = ARRAY_SIZE(ad7768_channels),
1589 .has_vcm_regulator = true,
1590 };
1591
1592 static const struct ad7768_chip_info adaq7767_chip_info = {
1593 .name = "adaq7767-1",
1594 .channel_spec = ad7768_channels,
1595 .num_channels = ARRAY_SIZE(ad7768_channels),
1596 .has_variable_aaf = true,
1597 };
1598
1599 static const struct ad7768_chip_info adaq7768_chip_info = {
1600 .name = "adaq7768-1",
1601 .channel_spec = adaq776x_channels,
1602 .num_channels = ARRAY_SIZE(adaq776x_channels),
1603 .pga_gains = adaq7768_gains,
1604 .default_pga_mode = AD7768_PGA_GAIN_2,
1605 .num_pga_modes = ARRAY_SIZE(adaq7768_gains),
1606 .pgia_mode2pin_offset = 6,
1607 .has_pga = true,
1608 };
1609
1610 static const struct ad7768_chip_info adaq7769_chip_info = {
1611 .name = "adaq7769-1",
1612 .channel_spec = adaq776x_channels,
1613 .num_channels = ARRAY_SIZE(adaq776x_channels),
1614 .pga_gains = adaq7769_gains,
1615 .default_pga_mode = AD7768_PGA_GAIN_0,
1616 .num_pga_modes = ARRAY_SIZE(adaq7769_gains),
1617 .pgia_mode2pin_offset = 0,
1618 .has_pga = true,
1619 .has_variable_aaf = true,
1620 };
1621
1622 static int ad7768_probe(struct spi_device *spi)
1623 {
1624 struct ad7768_state *st;
1625 struct iio_dev *indio_dev;
1626 int ret;
1627
1628 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
1629 if (!indio_dev)
1630 return -ENOMEM;
1631
1632 st = iio_priv(indio_dev);
1633 /*
1634 * Datasheet recommends SDI line to be kept high when data is not being
1635 * clocked out of the controller and the spi clock is free running,
1636 * to prevent accidental reset.
1637 * Since many controllers do not support the SPI_MOSI_IDLE_HIGH flag
1638 * yet, only request the MOSI idle state to enable if the controller
1639 * supports it.
1640 */
1641 if (spi->controller->mode_bits & SPI_MOSI_IDLE_HIGH) {
1642 spi->mode |= SPI_MOSI_IDLE_HIGH;
1643 ret = spi_setup(spi);
1644 if (ret < 0)
1645 return ret;
1646 }
1647
1648 st->chip = spi_get_device_match_data(spi);
1649 st->spi = spi;
1650
1651 st->regmap = devm_regmap_init_spi(spi, &ad7768_regmap_config);
1652 if (IS_ERR(st->regmap))
1653 return dev_err_probe(&spi->dev, PTR_ERR(st->regmap),
1654 "Failed to initialize regmap");
1655
1656 st->regmap24 = devm_regmap_init_spi(spi, &ad7768_regmap24_config);
1657 if (IS_ERR(st->regmap24))
1658 return dev_err_probe(&spi->dev, PTR_ERR(st->regmap24),
1659 "Failed to initialize regmap24");
1660
1661 ret = devm_regulator_get_enable_read_voltage(&spi->dev, "vref");
1662 if (ret < 0)
1663 return dev_err_probe(&spi->dev, ret,
1664 "Failed to get VREF voltage\n");
1665 st->vref_uv = ret;
1666
1667 st->mclk = devm_clk_get_enabled(&spi->dev, "mclk");
1668 if (IS_ERR(st->mclk))
1669 return PTR_ERR(st->mclk);
1670
1671 st->mclk_freq = clk_get_rate(st->mclk);
1672
1673 indio_dev->channels = st->chip->channel_spec;
1674 indio_dev->num_channels = st->chip->num_channels;
1675 indio_dev->name = st->chip->name;
1676 indio_dev->info = &ad7768_info;
1677 indio_dev->modes = INDIO_DIRECT_MODE;
1678
1679 /* Register VCM output regulator */
1680 if (st->chip->has_vcm_regulator) {
1681 ret = ad7768_register_vcm_regulator(&spi->dev, st, indio_dev);
1682 if (ret)
1683 return ret;
1684 }
1685
1686 ret = ad7768_parse_aaf_gain(&spi->dev, st);
1687 if (ret)
1688 return ret;
1689
1690 ret = ad7768_setup(indio_dev);
1691 if (ret < 0) {
1692 dev_err(&spi->dev, "AD7768 setup failed\n");
1693 return ret;
1694 }
1695
1696 init_completion(&st->completion);
1697 ret = devm_mutex_init(&spi->dev, &st->pga_lock);
1698 if (ret)
1699 return ret;
1700
1701 if (st->chip->has_pga) {
1702 ret = ad7768_setup_pga(&spi->dev, st);
1703 if (ret)
1704 return ret;
1705
1706 ret = ad7768_set_pga_gain(st, st->chip->default_pga_mode);
1707 if (ret)
1708 return ret;
1709 }
1710
1711 ret = ad7768_set_channel_label(indio_dev, st->chip->num_channels);
1712 if (ret)
1713 return ret;
1714
1715 ret = devm_request_irq(&spi->dev, spi->irq, &ad7768_interrupt,
1716 IRQF_TRIGGER_RISING | IRQF_NO_THREAD,
1717 indio_dev->name, indio_dev);
1718 if (ret)
1719 return ret;
1720
1721 ret = ad7768_triggered_buffer_alloc(indio_dev);
1722 if (ret)
1723 return ret;
1724
1725 return devm_iio_device_register(&spi->dev, indio_dev);
1726 }
1727
1728 static const struct spi_device_id ad7768_id_table[] = {
1729 { "ad7768-1", (kernel_ulong_t)&ad7768_chip_info },
1730 { "adaq7767-1", (kernel_ulong_t)&adaq7767_chip_info },
1731 { "adaq7768-1", (kernel_ulong_t)&adaq7768_chip_info },
1732 { "adaq7769-1", (kernel_ulong_t)&adaq7769_chip_info },
1733 { }
1734 };
1735 MODULE_DEVICE_TABLE(spi, ad7768_id_table);
1736
1737 static const struct of_device_id ad7768_of_match[] = {
1738 { .compatible = "adi,ad7768-1", .data = &ad7768_chip_info },
1739 { .compatible = "adi,adaq7767-1", .data = &adaq7767_chip_info },
1740 { .compatible = "adi,adaq7768-1", .data = &adaq7768_chip_info },
1741 { .compatible = "adi,adaq7769-1", .data = &adaq7769_chip_info },
1742 { }
1743 };
1744 MODULE_DEVICE_TABLE(of, ad7768_of_match);
1745
1746 static struct spi_driver ad7768_driver = {
1747 .driver = {
1748 .name = "ad7768-1",
1749 .of_match_table = ad7768_of_match,
1750 },
1751 .probe = ad7768_probe,
1752 .id_table = ad7768_id_table,
1753 };
1754 module_spi_driver(ad7768_driver);
1755
1756 MODULE_AUTHOR("Stefan Popa <[email protected]>");
1757 MODULE_DESCRIPTION("Analog Devices AD7768-1 ADC driver");
1758 MODULE_LICENSE("GPL v2");