1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Analog Devices AD4030 and AD4630 ADC family driver.
4 *
5 * Copyright 2024 Analog Devices, Inc.
6 * Copyright 2024 BayLibre, SAS
7 *
8 * based on code from:
9 * Analog Devices, Inc.
10 * Sergiu Cuciurean <sergiu.cuciurean@analog.com>
11 * Nuno Sa <nuno.sa@analog.com>
12 * Marcelo Schmitt <marcelo.schmitt@analog.com>
13 * Liviu Adace <liviu.adace@analog.com>
14 */
15
16 #include <linux/bitfield.h>
17 #include <linux/cleanup.h>
18 #include <linux/clk.h>
19 #include <linux/dmaengine.h>
20 #include <linux/limits.h>
21 #include <linux/log2.h>
22 #include <linux/math64.h>
23 #include <linux/minmax.h>
24 #include <linux/pwm.h>
25 #include <linux/regmap.h>
26 #include <linux/regulator/consumer.h>
27 #include <linux/spi/offload/consumer.h>
28 #include <linux/spi/spi.h>
29 #include <linux/unaligned.h>
30 #include <linux/units.h>
31 #include <linux/types.h>
32
33 #include <linux/iio/buffer-dmaengine.h>
34 #include <linux/iio/iio.h>
35 #include <linux/iio/trigger_consumer.h>
36 #include <linux/iio/triggered_buffer.h>
37
38 #define AD4030_REG_INTERFACE_CONFIG_A 0x00
39 #define AD4030_REG_INTERFACE_CONFIG_A_SW_RESET (BIT(0) | BIT(7))
40 #define AD4030_REG_INTERFACE_CONFIG_B 0x01
41 #define AD4030_REG_DEVICE_CONFIG 0x02
42 #define AD4030_REG_CHIP_TYPE 0x03
43 #define AD4030_REG_PRODUCT_ID_L 0x04
44 #define AD4030_REG_PRODUCT_ID_H 0x05
45 #define AD4030_REG_CHIP_GRADE 0x06
46 #define AD4030_REG_CHIP_GRADE_AD4030_24_GRADE 0x10
47 #define AD4030_REG_CHIP_GRADE_AD4630_16_GRADE 0x03
48 #define AD4030_REG_CHIP_GRADE_AD4630_24_GRADE 0x00
49 #define AD4030_REG_CHIP_GRADE_AD4632_16_GRADE 0x05
50 #define AD4030_REG_CHIP_GRADE_AD4632_24_GRADE 0x02
51 #define AD4030_REG_CHIP_GRADE_ADAQ4216_GRADE 0x1E
52 #define AD4030_REG_CHIP_GRADE_ADAQ4224_GRADE 0x1C
53 #define AD4030_REG_CHIP_GRADE_MASK_CHIP_GRADE GENMASK(7, 3)
54 #define AD4030_REG_SCRATCH_PAD 0x0A
55 #define AD4030_REG_SPI_REVISION 0x0B
56 #define AD4030_REG_VENDOR_L 0x0C
57 #define AD4030_REG_VENDOR_H 0x0D
58 #define AD4030_REG_STREAM_MODE 0x0E
59 #define AD4030_REG_INTERFACE_CONFIG_C 0x10
60 #define AD4030_REG_INTERFACE_STATUS_A 0x11
61 #define AD4030_REG_EXIT_CFG_MODE 0x14
62 #define AD4030_REG_EXIT_CFG_MODE_EXIT_MSK BIT(0)
63 #define AD4030_REG_AVG 0x15
64 #define AD4030_REG_AVG_MASK_AVG_SYNC BIT(7)
65 #define AD4030_REG_AVG_MASK_AVG_VAL GENMASK(4, 0)
66 #define AD4030_REG_OFFSET_X0_0 0x16
67 #define AD4030_REG_OFFSET_X0_1 0x17
68 #define AD4030_REG_OFFSET_X0_2 0x18
69 #define AD4030_REG_OFFSET_X1_0 0x19
70 #define AD4030_REG_OFFSET_X1_1 0x1A
71 #define AD4030_REG_OFFSET_X1_2 0x1B
72 #define AD4030_REG_OFFSET_BYTES_NB 3
73 #define AD4030_REG_OFFSET_CHAN(ch) \
74 (AD4030_REG_OFFSET_X0_2 + (AD4030_REG_OFFSET_BYTES_NB * (ch)))
75 #define AD4030_REG_GAIN_X0_LSB 0x1C
76 #define AD4030_REG_GAIN_X0_MSB 0x1D
77 #define AD4030_REG_GAIN_X1_LSB 0x1E
78 #define AD4030_REG_GAIN_X1_MSB 0x1F
79 #define AD4030_REG_GAIN_MAX_GAIN 1999970
80 #define AD4030_REG_GAIN_BYTES_NB 2
81 #define AD4030_REG_GAIN_CHAN(ch) \
82 (AD4030_REG_GAIN_X0_MSB + (AD4030_REG_GAIN_BYTES_NB * (ch)))
83 #define AD4030_REG_MODES 0x20
84 #define AD4030_REG_MODES_MASK_OUT_DATA_MODE GENMASK(2, 0)
85 #define AD4030_REG_MODES_MASK_LANE_MODE GENMASK(7, 6)
86 #define AD4030_REG_OSCILATOR 0x21
87 #define AD4030_REG_IO 0x22
88 #define AD4030_REG_IO_MASK_IO2X BIT(1)
89 #define AD4030_REG_PAT0 0x23
90 #define AD4030_REG_PAT1 0x24
91 #define AD4030_REG_PAT2 0x25
92 #define AD4030_REG_PAT3 0x26
93 #define AD4030_REG_DIG_DIAG 0x34
94 #define AD4030_REG_DIG_ERR 0x35
95
96 /* Sequence starting with "1 0 1" to enable reg access */
97 #define AD4030_REG_ACCESS 0xA0
98
99 #define AD4030_MAX_IIO_SAMPLE_SIZE_BUFFERED BITS_TO_BYTES(64)
100 #define AD4030_MAX_HARDWARE_CHANNEL_NB 2
101 #define AD4030_MAX_IIO_CHANNEL_NB 5
102 #define AD4030_SINGLE_COMMON_BYTE_CHANNELS_MASK 0b10
103 #define AD4030_DUAL_COMMON_BYTE_CHANNELS_MASK 0b1100
104 #define AD4030_GAIN_MIDLE_POINT 0x8000
105 /*
106 * This accounts for 1 sample per channel plus one s64 for the timestamp,
107 * aligned on a s64 boundary
108 */
109 #define AD4030_MAXIMUM_RX_BUFFER_SIZE \
110 (ALIGN(AD4030_MAX_IIO_SAMPLE_SIZE_BUFFERED * \
111 AD4030_MAX_HARDWARE_CHANNEL_NB, \
112 sizeof(s64)) + sizeof(s64))
113
114 #define AD4030_VREF_MIN_UV (4096 * MILLI)
115 #define AD4030_VREF_MAX_UV (5000 * MILLI)
116 #define AD4030_VIO_THRESHOLD_UV (1400 * MILLI)
117 #define AD4030_SPI_MAX_XFER_LEN 8
118 #define AD4030_SPI_MAX_REG_XFER_SPEED (80 * MEGA)
119 #define AD4030_TCNVH_NS 10
120 #define AD4030_TCNVL_NS 20
121 #define AD4030_TCYC_NS 500
122 #define AD4030_TCYC_ADJUSTED_NS (AD4030_TCYC_NS - AD4030_TCNVL_NS)
123 #define AD4030_TRESET_PW_NS 50
124 #define AD4632_TCYC_NS 2000
125 #define AD4632_TCYC_ADJUSTED_NS (AD4632_TCYC_NS - AD4030_TCNVL_NS)
126 #define AD4030_TRESET_COM_DELAY_MS 750
127 /* Datasheet says 9.8ns, so use the closest integer value */
128 #define AD4030_TQUIET_CNV_DELAY_NS 10
129
130 /* HARDWARE_GAIN */
131 #define ADAQ4616_PGA_PINS 2
132 #define ADAQ4616_PGA_GAIN_MAX_NANO (NANO * 2 / 3)
133
134 enum ad4030_out_mode {
135 AD4030_OUT_DATA_MD_DIFF,
136 AD4030_OUT_DATA_MD_16_DIFF_8_COM,
137 AD4030_OUT_DATA_MD_24_DIFF_8_COM,
138 AD4030_OUT_DATA_MD_30_AVERAGED_DIFF,
139 AD4030_OUT_DATA_MD_32_PATTERN,
140 };
141
142 enum {
143 AD4030_LANE_MD_1_PER_CH,
144 AD4030_LANE_MD_2_PER_CH,
145 AD4030_LANE_MD_4_PER_CH,
146 AD4030_LANE_MD_INTERLEAVED,
147 };
148
149 enum {
150 AD4030_SCAN_TYPE_NORMAL,
151 AD4030_SCAN_TYPE_AVG,
152 };
153
154 /*
155 * Gains computed as fractions of 1000 so they can be expressed by integers.
156 */
157 static const int adaq4216_hw_gains_vpv[] = {
158 1 * MILLI / 3, /* 0.333 */
159 5 * MILLI / 9, /* 0.555 */
160 20 * MILLI / 9, /* 0.2222 */
161 20 * MILLI / 3, /* 0.6666 */
162 };
163
164 static const int adaq4216_hw_gains_frac[][2] = {
165 { 1, 3 }, /* 1/3 V/V gain */
166 { 5, 9 }, /* 5/9 V/V gain */
167 { 20, 9 }, /* 20/9 V/V gain */
168 { 20, 3 }, /* 20/3 V/V gain */
169 };
170
171 struct ad4030_chip_info {
172 const char *name;
173 const unsigned long *available_masks;
174 const struct iio_chan_spec channels[AD4030_MAX_IIO_CHANNEL_NB];
175 const struct iio_chan_spec offload_channels[AD4030_MAX_IIO_CHANNEL_NB];
176 u8 grade;
177 u8 precision_bits;
178 bool has_pga;
179 /* Number of hardware channels */
180 int num_voltage_inputs;
181 unsigned int tcyc_ns;
182 unsigned int max_sample_rate_hz;
183 };
184
185 struct ad4030_state {
186 struct spi_device *spi;
187 struct regmap *regmap;
188 const struct ad4030_chip_info *chip;
189 struct gpio_desc *cnv_gpio;
190 int vref_uv;
191 int vio_uv;
192 int offset_avail[3];
193 unsigned int avg_log2;
194 enum ad4030_out_mode mode;
195 /* Offload sampling */
196 struct spi_transfer offload_xfer;
197 struct spi_message offload_msg;
198 struct spi_offload *offload;
199 struct spi_offload_trigger *offload_trigger;
200 struct spi_offload_trigger_config offload_trigger_config;
201 struct pwm_device *cnv_trigger;
202 size_t scale_avail_size;
203 struct pwm_waveform cnv_wf;
204 unsigned int scale_avail[ARRAY_SIZE(adaq4216_hw_gains_vpv)][2];
205 struct gpio_descs *pga_gpios;
206 unsigned int pga_index;
207
208 /*
209 * DMA (thus cache coherency maintenance) requires the transfer buffers
210 * to live in their own cache lines.
211 */
212 u8 tx_data[AD4030_SPI_MAX_XFER_LEN] __aligned(IIO_DMA_MINALIGN);
213 union {
214 u8 raw[AD4030_MAXIMUM_RX_BUFFER_SIZE];
215 struct {
216 s32 diff;
217 u8 common;
218 } single;
219 struct {
220 s32 diff[2];
221 u8 common[2];
222 } dual;
223 } rx_data;
224 };
225
226 /*
227 * For a chip with 2 hardware channel this will be used to create 2 common-mode
228 * channels:
229 * - voltage4
230 * - voltage5
231 * As the common-mode channels are after the differential ones, we compute the
232 * channel number like this:
233 * - _idx is the scan_index (the order in the output buffer)
234 * - _ch is the hardware channel number this common-mode channel is related
235 * - _idx - _ch gives us the number of channel in the chip
236 * - _idx - _ch * 2 is the starting number of the common-mode channels, since
237 * for each differential channel there is a common-mode channel
238 * - _idx - _ch * 2 + _ch gives the channel number for this specific common-mode
239 * channel
240 */
241 #define AD4030_CHAN_CMO(_idx, _ch) { \
242 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
243 BIT(IIO_CHAN_INFO_SCALE), \
244 .type = IIO_VOLTAGE, \
245 .indexed = 1, \
246 .address = (_ch), \
247 .channel = ((_idx) - (_ch)) * 2 + (_ch), \
248 .scan_index = (_idx), \
249 .scan_type = { \
250 .sign = 'u', \
251 .storagebits = 8, \
252 .realbits = 8, \
253 .endianness = IIO_BE, \
254 }, \
255 }
256
257 /*
258 * For a chip with 2 hardware channel this will be used to create 2 differential
259 * channels:
260 * - voltage0-voltage1
261 * - voltage2-voltage3
262 */
263 #define __AD4030_CHAN_DIFF(_idx, _scan_type, _offload, _pga) { \
264 .info_mask_shared_by_all = \
265 (_offload ? BIT(IIO_CHAN_INFO_SAMP_FREQ) : 0) | \
266 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
267 .info_mask_shared_by_all_available = \
268 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
269 .info_mask_separate = BIT(IIO_CHAN_INFO_SCALE) | \
270 BIT(IIO_CHAN_INFO_CALIBSCALE) | \
271 BIT(IIO_CHAN_INFO_CALIBBIAS) | \
272 BIT(IIO_CHAN_INFO_RAW), \
273 .info_mask_separate_available = BIT(IIO_CHAN_INFO_CALIBBIAS) | \
274 (_pga ? BIT(IIO_CHAN_INFO_SCALE) : 0) | \
275 BIT(IIO_CHAN_INFO_CALIBSCALE), \
276 .type = IIO_VOLTAGE, \
277 .indexed = 1, \
278 .address = (_idx), \
279 .channel = (_idx) * 2, \
280 .channel2 = (_idx) * 2 + 1, \
281 .scan_index = (_idx), \
282 .differential = true, \
283 .has_ext_scan_type = 1, \
284 .ext_scan_type = _scan_type, \
285 .num_ext_scan_type = ARRAY_SIZE(_scan_type), \
286 }
287
288 #define AD4030_CHAN_DIFF(_idx, _scan_type) \
289 __AD4030_CHAN_DIFF(_idx, _scan_type, 0, 0)
290
291 #define AD4030_OFFLOAD_CHAN_DIFF(_idx, _scan_type) \
292 __AD4030_CHAN_DIFF(_idx, _scan_type, 1, 0)
293
294 #define ADAQ4216_CHAN_DIFF(_idx, _scan_type) \
295 __AD4030_CHAN_DIFF(_idx, _scan_type, 0, 1)
296
297 #define ADAQ4216_OFFLOAD_CHAN_DIFF(_idx, _scan_type) \
298 __AD4030_CHAN_DIFF(_idx, _scan_type, 1, 1)
299
300 /*
301 * AD4030 can average over 2^N samples, where N = 1, 2, 3, ..., 16.
302 * We use N = 0 to mean no sample averaging.
303 */
304 static const int ad4030_average_modes[] = {
305 BIT(0), /* No sampling average */
306 BIT(1), BIT(2), BIT(3), BIT(4),
307 BIT(5), BIT(6), BIT(7), BIT(8),
308 BIT(9), BIT(10), BIT(11), BIT(12),
309 BIT(13), BIT(14), BIT(15), BIT(16),
310 };
311
312 static const struct spi_offload_config ad4030_offload_config = {
313 .capability_flags = SPI_OFFLOAD_CAP_TRIGGER |
314 SPI_OFFLOAD_CAP_RX_STREAM_DMA,
315 };
316
ad4030_enter_config_mode(struct ad4030_state * st)317 static int ad4030_enter_config_mode(struct ad4030_state *st)
318 {
319 st->tx_data[0] = AD4030_REG_ACCESS;
320
321 struct spi_transfer xfer = {
322 .tx_buf = st->tx_data,
323 .len = 1,
324 .speed_hz = AD4030_SPI_MAX_REG_XFER_SPEED,
325 };
326
327 return spi_sync_transfer(st->spi, &xfer, 1);
328 }
329
ad4030_exit_config_mode(struct ad4030_state * st)330 static int ad4030_exit_config_mode(struct ad4030_state *st)
331 {
332 st->tx_data[0] = 0;
333 st->tx_data[1] = AD4030_REG_EXIT_CFG_MODE;
334 st->tx_data[2] = AD4030_REG_EXIT_CFG_MODE_EXIT_MSK;
335
336 struct spi_transfer xfer = {
337 .tx_buf = st->tx_data,
338 .len = 3,
339 .speed_hz = AD4030_SPI_MAX_REG_XFER_SPEED,
340 };
341
342 return spi_sync_transfer(st->spi, &xfer, 1);
343 }
344
ad4030_spi_read(void * context,const void * reg,size_t reg_size,void * val,size_t val_size)345 static int ad4030_spi_read(void *context, const void *reg, size_t reg_size,
346 void *val, size_t val_size)
347 {
348 int ret;
349 struct ad4030_state *st = context;
350 struct spi_transfer xfer = {
351 .tx_buf = st->tx_data,
352 .rx_buf = st->rx_data.raw,
353 .len = reg_size + val_size,
354 .speed_hz = AD4030_SPI_MAX_REG_XFER_SPEED,
355 };
356
357 if (xfer.len > sizeof(st->tx_data) ||
358 xfer.len > sizeof(st->rx_data.raw))
359 return -EINVAL;
360
361 ret = ad4030_enter_config_mode(st);
362 if (ret)
363 return ret;
364
365 memset(st->tx_data, 0, sizeof(st->tx_data));
366 memcpy(st->tx_data, reg, reg_size);
367
368 ret = spi_sync_transfer(st->spi, &xfer, 1);
369 if (ret)
370 return ret;
371
372 memcpy(val, &st->rx_data.raw[reg_size], val_size);
373
374 return ad4030_exit_config_mode(st);
375 }
376
ad4030_spi_write(void * context,const void * data,size_t count)377 static int ad4030_spi_write(void *context, const void *data, size_t count)
378 {
379 int ret;
380 struct ad4030_state *st = context;
381 bool is_reset = count >= 3 &&
382 ((u8 *)data)[0] == 0 &&
383 ((u8 *)data)[1] == 0 &&
384 ((u8 *)data)[2] == 0x81;
385 struct spi_transfer xfer = {
386 .tx_buf = st->tx_data,
387 .len = count,
388 .speed_hz = AD4030_SPI_MAX_REG_XFER_SPEED,
389 };
390
391 if (count > sizeof(st->tx_data))
392 return -EINVAL;
393
394 ret = ad4030_enter_config_mode(st);
395 if (ret)
396 return ret;
397
398 memcpy(st->tx_data, data, count);
399
400 ret = spi_sync_transfer(st->spi, &xfer, 1);
401 if (ret)
402 return ret;
403
404 /*
405 * From datasheet: "After a [...] reset, no SPI commands or conversions
406 * can be started for 750us"
407 * After a reset we are in conversion mode, no need to exit config mode
408 */
409 if (is_reset) {
410 fsleep(750);
411 return 0;
412 }
413
414 return ad4030_exit_config_mode(st);
415 }
416
417 static const struct regmap_bus ad4030_regmap_bus = {
418 .read = ad4030_spi_read,
419 .write = ad4030_spi_write,
420 .reg_format_endian_default = REGMAP_ENDIAN_BIG,
421 };
422
423 static const struct regmap_range ad4030_regmap_rd_range[] = {
424 regmap_reg_range(AD4030_REG_INTERFACE_CONFIG_A, AD4030_REG_CHIP_GRADE),
425 regmap_reg_range(AD4030_REG_SCRATCH_PAD, AD4030_REG_STREAM_MODE),
426 regmap_reg_range(AD4030_REG_INTERFACE_CONFIG_C,
427 AD4030_REG_INTERFACE_STATUS_A),
428 regmap_reg_range(AD4030_REG_EXIT_CFG_MODE, AD4030_REG_PAT3),
429 regmap_reg_range(AD4030_REG_DIG_DIAG, AD4030_REG_DIG_ERR),
430 };
431
432 static const struct regmap_range ad4030_regmap_wr_range[] = {
433 regmap_reg_range(AD4030_REG_CHIP_TYPE, AD4030_REG_CHIP_GRADE),
434 regmap_reg_range(AD4030_REG_SPI_REVISION, AD4030_REG_VENDOR_H),
435 };
436
437 static const struct regmap_access_table ad4030_regmap_rd_table = {
438 .yes_ranges = ad4030_regmap_rd_range,
439 .n_yes_ranges = ARRAY_SIZE(ad4030_regmap_rd_range),
440 };
441
442 static const struct regmap_access_table ad4030_regmap_wr_table = {
443 .no_ranges = ad4030_regmap_wr_range,
444 .n_no_ranges = ARRAY_SIZE(ad4030_regmap_wr_range),
445 };
446
447 static const struct regmap_config ad4030_regmap_config = {
448 .reg_bits = 16,
449 .val_bits = 8,
450 .read_flag_mask = 0x80,
451 .rd_table = &ad4030_regmap_rd_table,
452 .wr_table = &ad4030_regmap_wr_table,
453 .max_register = AD4030_REG_DIG_ERR,
454 };
455
ad4030_fill_scale_avail(struct ad4030_state * st)456 static void ad4030_fill_scale_avail(struct ad4030_state *st)
457 {
458 unsigned int mag_bits, int_part, fract_part;
459 u64 range;
460
461 /*
462 * The maximum precision of differential channels is retrieved from the
463 * chip properties. The output code of differential channels is in two's
464 * complement format (i.e. signed), so the MSB is the sign bit and only
465 * (precision_bits - 1) bits express voltage magnitude.
466 */
467 mag_bits = st->chip->precision_bits - 1;
468
469 for (unsigned int i = 0; i < ARRAY_SIZE(adaq4216_hw_gains_frac); i++) {
470 range = mult_frac(st->vref_uv, adaq4216_hw_gains_frac[i][1],
471 adaq4216_hw_gains_frac[i][0]);
472 /*
473 * If range were in mV, we would multiply it by NANO below.
474 * Though, range is in µV so multiply it by MICRO only so the
475 * result after right shift and division scales output codes to
476 * millivolts.
477 */
478 int_part = div_u64_rem((range * MICRO) >> mag_bits, NANO, &fract_part);
479 st->scale_avail[i][0] = int_part;
480 st->scale_avail[i][1] = fract_part;
481 }
482 }
483
ad4030_set_pga_gain(struct ad4030_state * st)484 static int ad4030_set_pga_gain(struct ad4030_state *st)
485 {
486 DECLARE_BITMAP(bitmap, ADAQ4616_PGA_PINS) = { };
487
488 bitmap_write(bitmap, st->pga_index, 0, ADAQ4616_PGA_PINS);
489
490 return gpiod_multi_set_value_cansleep(st->pga_gpios, bitmap);
491 }
492
ad4030_set_pga(struct iio_dev * indio_dev,int gain_int,int gain_fract)493 static int ad4030_set_pga(struct iio_dev *indio_dev, int gain_int, int gain_fract)
494 {
495 struct ad4030_state *st = iio_priv(indio_dev);
496 unsigned int mag_bits = st->chip->precision_bits - 1;
497 unsigned int tmp;
498 u64 gain_nano;
499
500 if (!st->pga_gpios)
501 return -EINVAL;
502
503 gain_nano = gain_int * NANO + gain_fract;
504 if (!in_range(gain_nano, 1, ADAQ4616_PGA_GAIN_MAX_NANO))
505 return -EINVAL;
506
507 tmp = DIV_ROUND_CLOSEST_ULL(gain_nano << mag_bits, NANO);
508 gain_nano = DIV_ROUND_CLOSEST(st->vref_uv, tmp);
509 st->pga_index = find_closest(gain_nano, adaq4216_hw_gains_vpv,
510 ARRAY_SIZE(adaq4216_hw_gains_vpv));
511
512 return ad4030_set_pga_gain(st);
513 }
514
ad4030_get_chan_scale(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2)515 static int ad4030_get_chan_scale(struct iio_dev *indio_dev,
516 struct iio_chan_spec const *chan,
517 int *val,
518 int *val2)
519 {
520 struct ad4030_state *st = iio_priv(indio_dev);
521 const struct iio_scan_type *scan_type;
522
523 scan_type = iio_get_current_scan_type(indio_dev, chan);
524 if (IS_ERR(scan_type))
525 return PTR_ERR(scan_type);
526
527 /* The LSB of the 8-bit common-mode data is always vref/256. */
528 if (st->chip->has_pga && scan_type->realbits != 8) {
529 *val = st->scale_avail[st->pga_index][0];
530 *val2 = st->scale_avail[st->pga_index][1];
531 return IIO_VAL_INT_PLUS_NANO;
532 }
533
534 if (chan->differential)
535 *val = (st->vref_uv * 2) / MILLI;
536 else
537 *val = st->vref_uv / MILLI;
538
539 *val2 = scan_type->realbits;
540
541 return IIO_VAL_FRACTIONAL_LOG2;
542 }
543
ad4030_get_chan_calibscale(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2)544 static int ad4030_get_chan_calibscale(struct iio_dev *indio_dev,
545 struct iio_chan_spec const *chan,
546 int *val,
547 int *val2)
548 {
549 struct ad4030_state *st = iio_priv(indio_dev);
550 u16 gain;
551 int ret;
552
553 ret = regmap_bulk_read(st->regmap, AD4030_REG_GAIN_CHAN(chan->address),
554 st->rx_data.raw, AD4030_REG_GAIN_BYTES_NB);
555 if (ret)
556 return ret;
557
558 gain = get_unaligned_be16(st->rx_data.raw);
559
560 /* From datasheet: multiplied output = input × gain word/0x8000 */
561 *val = gain / AD4030_GAIN_MIDLE_POINT;
562 *val2 = mul_u64_u32_div(gain % AD4030_GAIN_MIDLE_POINT, NANO,
563 AD4030_GAIN_MIDLE_POINT);
564
565 return IIO_VAL_INT_PLUS_NANO;
566 }
567
568 /* Returns the offset where 1 LSB = (VREF/2^precision_bits - 1)/gain */
ad4030_get_chan_calibbias(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val)569 static int ad4030_get_chan_calibbias(struct iio_dev *indio_dev,
570 struct iio_chan_spec const *chan,
571 int *val)
572 {
573 struct ad4030_state *st = iio_priv(indio_dev);
574 int ret;
575
576 ret = regmap_bulk_read(st->regmap,
577 AD4030_REG_OFFSET_CHAN(chan->address),
578 st->rx_data.raw, AD4030_REG_OFFSET_BYTES_NB);
579 if (ret)
580 return ret;
581
582 switch (st->chip->precision_bits) {
583 case 16:
584 *val = sign_extend32(get_unaligned_be16(st->rx_data.raw), 15);
585 return IIO_VAL_INT;
586
587 case 24:
588 *val = sign_extend32(get_unaligned_be24(st->rx_data.raw), 23);
589 return IIO_VAL_INT;
590
591 default:
592 return -EINVAL;
593 }
594 }
595
ad4030_get_sampling_freq(struct ad4030_state * st,int * freq)596 static void ad4030_get_sampling_freq(struct ad4030_state *st, int *freq)
597 {
598 struct spi_offload_trigger_config *config = &st->offload_trigger_config;
599
600 /*
601 * Conversion data is fetched from the device when the offload transfer
602 * is triggered. Thus, provide the SPI offload trigger frequency as the
603 * sampling frequency.
604 */
605 *freq = config->periodic.frequency_hz;
606 }
607
ad4030_update_conversion_rate(struct ad4030_state * st,unsigned int freq_hz,unsigned int avg_log2)608 static int ad4030_update_conversion_rate(struct ad4030_state *st,
609 unsigned int freq_hz, unsigned int avg_log2)
610 {
611 struct spi_offload_trigger_config *config = &st->offload_trigger_config;
612 unsigned int offload_period_ns, cnv_rate_hz;
613 struct pwm_waveform cnv_wf = { };
614 u64 target = AD4030_TCNVH_NS;
615 u64 offload_offset_ns;
616 int ret;
617
618 /*
619 * When averaging/oversampling over N samples, we fire the offload
620 * trigger once at every N pulses of the CNV signal. Conversely, the CNV
621 * signal needs to be N times faster than the offload trigger. Take that
622 * into account to correctly re-evaluate both the PWM waveform connected
623 * to CNV and the SPI offload trigger.
624 */
625 cnv_rate_hz = freq_hz << avg_log2;
626
627 cnv_wf.period_length_ns = DIV_ROUND_CLOSEST(NSEC_PER_SEC, cnv_rate_hz);
628 /*
629 * The datasheet lists a minimum time of 9.8 ns, but no maximum. If the
630 * rounded PWM's value is less than 10, increase the target value by 10
631 * and attempt to round the waveform again, until the value is at least
632 * 10 ns. Use a separate variable to represent the target in case the
633 * rounding is severe enough to keep putting the first few results under
634 * the minimum 10ns condition checked by the while loop.
635 */
636 do {
637 cnv_wf.duty_length_ns = target;
638 ret = pwm_round_waveform_might_sleep(st->cnv_trigger, &cnv_wf);
639 if (ret)
640 return ret;
641 target += AD4030_TCNVH_NS;
642 } while (cnv_wf.duty_length_ns < AD4030_TCNVH_NS);
643
644 /*
645 * The CNV waveform period (period_length_ns) might get rounded down by
646 * pwm_round_waveform_might_sleep(). Check the resultant PWM period
647 * is not smaller than the minimum data conversion cycle time.
648 */
649 if (!in_range(cnv_wf.period_length_ns, AD4030_TCYC_NS, INT_MAX))
650 return -EINVAL;
651
652 offload_period_ns = DIV_ROUND_CLOSEST(NSEC_PER_SEC, freq_hz);
653
654 config->periodic.frequency_hz = DIV_ROUND_UP(HZ_PER_GHZ, offload_period_ns);
655
656 /*
657 * The hardware does the capture on zone 2 (when SPI trigger PWM
658 * is used). This means that the SPI trigger signal should happen at
659 * tsync + tquiet_con_delay being tsync the conversion signal period
660 * and tquiet_con_delay 9.8ns. Hence set the PWM phase accordingly.
661 *
662 * The PWM waveform API only supports nanosecond resolution right now,
663 * so round this setting to the closest available value.
664 */
665 offload_offset_ns = AD4030_TQUIET_CNV_DELAY_NS;
666 do {
667 config->periodic.offset_ns = offload_offset_ns;
668 ret = spi_offload_trigger_validate(st->offload_trigger, config);
669 if (ret)
670 return ret;
671 offload_offset_ns += AD4030_TQUIET_CNV_DELAY_NS;
672 } while (config->periodic.offset_ns < AD4030_TQUIET_CNV_DELAY_NS);
673
674 st->cnv_wf = cnv_wf;
675
676 return 0;
677 }
678
ad4030_set_sampling_freq(struct iio_dev * indio_dev,int freq_hz)679 static int ad4030_set_sampling_freq(struct iio_dev *indio_dev, int freq_hz)
680 {
681 struct ad4030_state *st = iio_priv(indio_dev);
682
683 if (freq_hz == 0)
684 return -EINVAL;
685
686 if (!in_range(freq_hz, 0, st->chip->max_sample_rate_hz))
687 return -ERANGE;
688
689 return ad4030_update_conversion_rate(st, freq_hz, st->avg_log2);
690 }
691
ad4030_set_chan_calibscale(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int gain_int,int gain_frac)692 static int ad4030_set_chan_calibscale(struct iio_dev *indio_dev,
693 struct iio_chan_spec const *chan,
694 int gain_int,
695 int gain_frac)
696 {
697 struct ad4030_state *st = iio_priv(indio_dev);
698 u64 gain;
699
700 if (gain_int < 0 || gain_frac < 0)
701 return -EINVAL;
702
703 gain = mul_u32_u32(gain_int, MICRO) + gain_frac;
704
705 if (gain > AD4030_REG_GAIN_MAX_GAIN)
706 return -EINVAL;
707
708 put_unaligned_be16(DIV_ROUND_CLOSEST_ULL(gain * AD4030_GAIN_MIDLE_POINT,
709 MICRO),
710 st->tx_data);
711
712 return regmap_bulk_write(st->regmap,
713 AD4030_REG_GAIN_CHAN(chan->address),
714 st->tx_data, AD4030_REG_GAIN_BYTES_NB);
715 }
716
ad4030_set_chan_calibbias(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int offset)717 static int ad4030_set_chan_calibbias(struct iio_dev *indio_dev,
718 struct iio_chan_spec const *chan,
719 int offset)
720 {
721 struct ad4030_state *st = iio_priv(indio_dev);
722
723 if (offset < st->offset_avail[0] || offset > st->offset_avail[2])
724 return -EINVAL;
725
726 st->tx_data[2] = 0;
727
728 switch (st->chip->precision_bits) {
729 case 16:
730 put_unaligned_be16(offset, st->tx_data);
731 break;
732
733 case 24:
734 put_unaligned_be24(offset, st->tx_data);
735 break;
736
737 default:
738 return -EINVAL;
739 }
740
741 return regmap_bulk_write(st->regmap,
742 AD4030_REG_OFFSET_CHAN(chan->address),
743 st->tx_data, AD4030_REG_OFFSET_BYTES_NB);
744 }
745
ad4030_set_avg_frame_len(struct iio_dev * dev,int avg_val)746 static int ad4030_set_avg_frame_len(struct iio_dev *dev, int avg_val)
747 {
748 struct ad4030_state *st = iio_priv(dev);
749 unsigned int last_avg_idx = ARRAY_SIZE(ad4030_average_modes) - 1;
750 unsigned int avg_log2;
751 int freq_hz;
752 int ret;
753
754 /* Reject unsupported modes */
755 if (avg_val > ad4030_average_modes[last_avg_idx])
756 return -EINVAL;
757
758 /* Avoid invalid values for logarithm since it's undefined */
759 if (avg_val < 1)
760 return -EINVAL;
761
762 avg_log2 = ilog2(avg_val);
763
764 if (st->offload_trigger) {
765 /*
766 * The sample averaging and sampling frequency configurations
767 * are mutually dependent on each other. That's because the
768 * effective data sample rate is fCNV / 2^N, where N is the
769 * number of samples being averaged.
770 *
771 * When SPI offload is supported and we have control over the
772 * sample rate, the conversion start signal (CNV) and the SPI
773 * offload trigger frequencies must be re-evaluated so data is
774 * fetched only after 'avg_val' conversions.
775 */
776 ad4030_get_sampling_freq(st, &freq_hz);
777 ret = ad4030_update_conversion_rate(st, freq_hz, avg_log2);
778 if (ret)
779 return ret;
780 }
781
782 ret = regmap_write(st->regmap, AD4030_REG_AVG,
783 AD4030_REG_AVG_MASK_AVG_SYNC |
784 FIELD_PREP(AD4030_REG_AVG_MASK_AVG_VAL, avg_log2));
785 if (ret)
786 return ret;
787
788 st->avg_log2 = avg_log2;
789
790 return 0;
791 }
792
ad4030_is_common_byte_asked(struct ad4030_state * st,unsigned int mask)793 static bool ad4030_is_common_byte_asked(struct ad4030_state *st,
794 unsigned int mask)
795 {
796 return mask & (st->chip->num_voltage_inputs == 1 ?
797 AD4030_SINGLE_COMMON_BYTE_CHANNELS_MASK :
798 AD4030_DUAL_COMMON_BYTE_CHANNELS_MASK);
799 }
800
ad4030_set_mode(struct iio_dev * indio_dev,unsigned long mask)801 static int ad4030_set_mode(struct iio_dev *indio_dev, unsigned long mask)
802 {
803 struct ad4030_state *st = iio_priv(indio_dev);
804
805 if (st->avg_log2 > 0) {
806 st->mode = AD4030_OUT_DATA_MD_30_AVERAGED_DIFF;
807 } else if (ad4030_is_common_byte_asked(st, mask)) {
808 switch (st->chip->precision_bits) {
809 case 16:
810 st->mode = AD4030_OUT_DATA_MD_16_DIFF_8_COM;
811 break;
812
813 case 24:
814 st->mode = AD4030_OUT_DATA_MD_24_DIFF_8_COM;
815 break;
816
817 default:
818 return -EINVAL;
819 }
820 } else {
821 st->mode = AD4030_OUT_DATA_MD_DIFF;
822 }
823
824 return regmap_update_bits(st->regmap, AD4030_REG_MODES,
825 AD4030_REG_MODES_MASK_OUT_DATA_MODE,
826 st->mode);
827 }
828
829 /*
830 * Descramble 2 32bits numbers out of a 64bits. The bits are interleaved:
831 * 1 bit for first number, 1 bit for the second, and so on...
832 */
ad4030_extract_interleaved(u8 * src,u32 * ch0,u32 * ch1)833 static void ad4030_extract_interleaved(u8 *src, u32 *ch0, u32 *ch1)
834 {
835 u8 h0, h1, l0, l1;
836 u32 out0, out1;
837 u8 *out0_raw = (u8 *)&out0;
838 u8 *out1_raw = (u8 *)&out1;
839
840 for (int i = 0; i < 4; i++) {
841 h0 = src[i * 2];
842 l1 = src[i * 2 + 1];
843 h1 = h0 << 1;
844 l0 = l1 >> 1;
845
846 h0 &= 0xAA;
847 l0 &= 0x55;
848 h1 &= 0xAA;
849 l1 &= 0x55;
850
851 h0 = (h0 | h0 << 001) & 0xCC;
852 h1 = (h1 | h1 << 001) & 0xCC;
853 l0 = (l0 | l0 >> 001) & 0x33;
854 l1 = (l1 | l1 >> 001) & 0x33;
855 h0 = (h0 | h0 << 002) & 0xF0;
856 h1 = (h1 | h1 << 002) & 0xF0;
857 l0 = (l0 | l0 >> 002) & 0x0F;
858 l1 = (l1 | l1 >> 002) & 0x0F;
859
860 out0_raw[i] = h0 | l0;
861 out1_raw[i] = h1 | l1;
862 }
863
864 *ch0 = out0;
865 *ch1 = out1;
866 }
867
ad4030_conversion(struct iio_dev * indio_dev)868 static int ad4030_conversion(struct iio_dev *indio_dev)
869 {
870 struct ad4030_state *st = iio_priv(indio_dev);
871 const struct iio_scan_type *scan_type;
872 unsigned char diff_realbytes, diff_storagebytes;
873 unsigned int bytes_to_read;
874 unsigned long cnv_nb = BIT(st->avg_log2);
875 unsigned int i;
876 int ret;
877
878 scan_type = iio_get_current_scan_type(indio_dev, st->chip->channels);
879 if (IS_ERR(scan_type))
880 return PTR_ERR(scan_type);
881
882 diff_realbytes = BITS_TO_BYTES(scan_type->realbits);
883 diff_storagebytes = BITS_TO_BYTES(scan_type->storagebits);
884
885 /* Number of bytes for one differential channel */
886 bytes_to_read = diff_realbytes;
887 /* Add one byte if we are using a differential + common byte mode */
888 bytes_to_read += (st->mode == AD4030_OUT_DATA_MD_24_DIFF_8_COM ||
889 st->mode == AD4030_OUT_DATA_MD_16_DIFF_8_COM) ? 1 : 0;
890 /* Multiply by the number of hardware channels */
891 bytes_to_read *= st->chip->num_voltage_inputs;
892
893 for (i = 0; i < cnv_nb; i++) {
894 gpiod_set_value_cansleep(st->cnv_gpio, 1);
895 ndelay(AD4030_TCNVH_NS);
896 gpiod_set_value_cansleep(st->cnv_gpio, 0);
897 ndelay(st->chip->tcyc_ns);
898 }
899
900 ret = spi_read(st->spi, st->rx_data.raw, bytes_to_read);
901 if (ret)
902 return ret;
903
904 if (st->chip->num_voltage_inputs == 2)
905 ad4030_extract_interleaved(st->rx_data.raw,
906 &st->rx_data.dual.diff[0],
907 &st->rx_data.dual.diff[1]);
908
909 /*
910 * If no common mode voltage channel is enabled, we can use the raw
911 * data as is. Otherwise, we need to rearrange the data a bit to match
912 * the natural alignment of the IIO buffer.
913 */
914
915 if (st->mode != AD4030_OUT_DATA_MD_16_DIFF_8_COM &&
916 st->mode != AD4030_OUT_DATA_MD_24_DIFF_8_COM)
917 return 0;
918
919 if (st->chip->num_voltage_inputs == 1) {
920 st->rx_data.single.common = st->rx_data.raw[diff_realbytes];
921 return 0;
922 }
923
924 for (i = 0; i < st->chip->num_voltage_inputs; i++)
925 st->rx_data.dual.common[i] =
926 st->rx_data.raw[diff_storagebytes * i + diff_realbytes];
927
928 return 0;
929 }
930
ad4030_single_conversion(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,int * val)931 static int ad4030_single_conversion(struct iio_dev *indio_dev,
932 const struct iio_chan_spec *chan, int *val)
933 {
934 struct ad4030_state *st = iio_priv(indio_dev);
935 int ret;
936
937 ret = ad4030_set_mode(indio_dev, BIT(chan->scan_index));
938 if (ret)
939 return ret;
940
941 ret = ad4030_conversion(indio_dev);
942 if (ret)
943 return ret;
944
945 if (chan->differential)
946 if (st->chip->num_voltage_inputs == 1)
947 *val = st->rx_data.single.diff;
948 else
949 *val = st->rx_data.dual.diff[chan->address];
950 else
951 if (st->chip->num_voltage_inputs == 1)
952 *val = st->rx_data.single.common;
953 else
954 *val = st->rx_data.dual.common[chan->address];
955
956 return IIO_VAL_INT;
957 }
958
ad4030_trigger_handler(int irq,void * p)959 static irqreturn_t ad4030_trigger_handler(int irq, void *p)
960 {
961 struct iio_poll_func *pf = p;
962 struct iio_dev *indio_dev = pf->indio_dev;
963 struct ad4030_state *st = iio_priv(indio_dev);
964 int ret;
965
966 ret = ad4030_conversion(indio_dev);
967 if (ret)
968 goto out;
969
970 iio_push_to_buffers_with_ts(indio_dev, &st->rx_data, sizeof(st->rx_data),
971 pf->timestamp);
972
973 out:
974 iio_trigger_notify_done(indio_dev->trig);
975
976 return IRQ_HANDLED;
977 }
978
979 static const int ad4030_gain_avail[3][2] = {
980 { 0, 0 },
981 { 0, 30518 },
982 { 1, 999969482 },
983 };
984
ad4030_read_avail(struct iio_dev * indio_dev,struct iio_chan_spec const * channel,const int ** vals,int * type,int * length,long mask)985 static int ad4030_read_avail(struct iio_dev *indio_dev,
986 struct iio_chan_spec const *channel,
987 const int **vals, int *type,
988 int *length, long mask)
989 {
990 struct ad4030_state *st = iio_priv(indio_dev);
991
992 switch (mask) {
993 case IIO_CHAN_INFO_CALIBBIAS:
994 *vals = st->offset_avail;
995 *type = IIO_VAL_INT;
996 return IIO_AVAIL_RANGE;
997
998 case IIO_CHAN_INFO_CALIBSCALE:
999 *vals = (void *)ad4030_gain_avail;
1000 *type = IIO_VAL_INT_PLUS_NANO;
1001 return IIO_AVAIL_RANGE;
1002
1003 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
1004 *vals = ad4030_average_modes;
1005 *type = IIO_VAL_INT;
1006 *length = ARRAY_SIZE(ad4030_average_modes);
1007 return IIO_AVAIL_LIST;
1008
1009 case IIO_CHAN_INFO_SCALE:
1010 if (st->scale_avail_size == 1)
1011 *vals = (int *)st->scale_avail[st->pga_index];
1012 else
1013 *vals = (int *)st->scale_avail;
1014 *length = st->scale_avail_size * 2; /* print int and nano part */
1015 *type = IIO_VAL_INT_PLUS_NANO;
1016 return IIO_AVAIL_LIST;
1017
1018 default:
1019 return -EINVAL;
1020 }
1021 }
1022
ad4030_read_raw_dispatch(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long info)1023 static int ad4030_read_raw_dispatch(struct iio_dev *indio_dev,
1024 struct iio_chan_spec const *chan, int *val,
1025 int *val2, long info)
1026 {
1027 struct ad4030_state *st = iio_priv(indio_dev);
1028
1029 switch (info) {
1030 case IIO_CHAN_INFO_RAW:
1031 return ad4030_single_conversion(indio_dev, chan, val);
1032
1033 case IIO_CHAN_INFO_CALIBSCALE:
1034 return ad4030_get_chan_calibscale(indio_dev, chan, val, val2);
1035
1036 case IIO_CHAN_INFO_CALIBBIAS:
1037 return ad4030_get_chan_calibbias(indio_dev, chan, val);
1038
1039 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
1040 *val = BIT(st->avg_log2);
1041 return IIO_VAL_INT;
1042
1043 case IIO_CHAN_INFO_SAMP_FREQ:
1044 ad4030_get_sampling_freq(st, val);
1045 return IIO_VAL_INT;
1046
1047 default:
1048 return -EINVAL;
1049 }
1050 }
1051
ad4030_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long info)1052 static int ad4030_read_raw(struct iio_dev *indio_dev,
1053 struct iio_chan_spec const *chan, int *val,
1054 int *val2, long info)
1055 {
1056 int ret;
1057
1058 if (info == IIO_CHAN_INFO_SCALE)
1059 return ad4030_get_chan_scale(indio_dev, chan, val, val2);
1060
1061 if (!iio_device_claim_direct(indio_dev))
1062 return -EBUSY;
1063
1064 ret = ad4030_read_raw_dispatch(indio_dev, chan, val, val2, info);
1065
1066 iio_device_release_direct(indio_dev);
1067
1068 return ret;
1069 }
1070
ad4030_write_raw_dispatch(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long info)1071 static int ad4030_write_raw_dispatch(struct iio_dev *indio_dev,
1072 struct iio_chan_spec const *chan, int val,
1073 int val2, long info)
1074 {
1075 switch (info) {
1076 case IIO_CHAN_INFO_CALIBSCALE:
1077 return ad4030_set_chan_calibscale(indio_dev, chan, val, val2);
1078
1079 case IIO_CHAN_INFO_CALIBBIAS:
1080 if (val2 != 0)
1081 return -EINVAL;
1082 return ad4030_set_chan_calibbias(indio_dev, chan, val);
1083
1084 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
1085 return ad4030_set_avg_frame_len(indio_dev, val);
1086
1087 case IIO_CHAN_INFO_SAMP_FREQ:
1088 return ad4030_set_sampling_freq(indio_dev, val);
1089
1090 case IIO_CHAN_INFO_SCALE:
1091 return ad4030_set_pga(indio_dev, val, val2);
1092
1093 default:
1094 return -EINVAL;
1095 }
1096 }
1097
ad4030_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long info)1098 static int ad4030_write_raw(struct iio_dev *indio_dev,
1099 struct iio_chan_spec const *chan, int val,
1100 int val2, long info)
1101 {
1102 int ret;
1103
1104 if (!iio_device_claim_direct(indio_dev))
1105 return -EBUSY;
1106
1107 ret = ad4030_write_raw_dispatch(indio_dev, chan, val, val2, info);
1108
1109 iio_device_release_direct(indio_dev);
1110
1111 return ret;
1112 }
1113
ad4030_write_raw_get_fmt(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,long mask)1114 static int ad4030_write_raw_get_fmt(struct iio_dev *indio_dev,
1115 struct iio_chan_spec const *chan, long mask)
1116 {
1117 switch (mask) {
1118 case IIO_CHAN_INFO_SCALE:
1119 return IIO_VAL_INT_PLUS_NANO;
1120 default:
1121 return IIO_VAL_INT_PLUS_MICRO;
1122 }
1123 }
1124
ad4030_reg_access(struct iio_dev * indio_dev,unsigned int reg,unsigned int writeval,unsigned int * readval)1125 static int ad4030_reg_access(struct iio_dev *indio_dev, unsigned int reg,
1126 unsigned int writeval, unsigned int *readval)
1127 {
1128 const struct ad4030_state *st = iio_priv(indio_dev);
1129 int ret;
1130
1131 if (!iio_device_claim_direct(indio_dev))
1132 return -EBUSY;
1133
1134 if (readval)
1135 ret = regmap_read(st->regmap, reg, readval);
1136 else
1137 ret = regmap_write(st->regmap, reg, writeval);
1138
1139 iio_device_release_direct(indio_dev);
1140
1141 return ret;
1142 }
1143
ad4030_read_label(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,char * label)1144 static int ad4030_read_label(struct iio_dev *indio_dev,
1145 struct iio_chan_spec const *chan,
1146 char *label)
1147 {
1148 if (chan->differential)
1149 return sysfs_emit(label, "differential%lu\n", chan->address);
1150 return sysfs_emit(label, "common-mode%lu\n", chan->address);
1151 }
1152
ad4030_get_current_scan_type(const struct iio_dev * indio_dev,const struct iio_chan_spec * chan)1153 static int ad4030_get_current_scan_type(const struct iio_dev *indio_dev,
1154 const struct iio_chan_spec *chan)
1155 {
1156 struct ad4030_state *st = iio_priv(indio_dev);
1157
1158 return st->avg_log2 ? AD4030_SCAN_TYPE_AVG : AD4030_SCAN_TYPE_NORMAL;
1159 }
1160
ad4030_update_scan_mode(struct iio_dev * indio_dev,const unsigned long * scan_mask)1161 static int ad4030_update_scan_mode(struct iio_dev *indio_dev,
1162 const unsigned long *scan_mask)
1163 {
1164 return ad4030_set_mode(indio_dev, *scan_mask);
1165 }
1166
1167 static const struct iio_info ad4030_iio_info = {
1168 .read_avail = ad4030_read_avail,
1169 .read_raw = ad4030_read_raw,
1170 .write_raw = ad4030_write_raw,
1171 .write_raw_get_fmt = &ad4030_write_raw_get_fmt,
1172 .debugfs_reg_access = ad4030_reg_access,
1173 .read_label = ad4030_read_label,
1174 .get_current_scan_type = ad4030_get_current_scan_type,
1175 .update_scan_mode = ad4030_update_scan_mode,
1176 };
1177
ad4030_validate_scan_mask(struct iio_dev * indio_dev,const unsigned long * scan_mask)1178 static bool ad4030_validate_scan_mask(struct iio_dev *indio_dev,
1179 const unsigned long *scan_mask)
1180 {
1181 struct ad4030_state *st = iio_priv(indio_dev);
1182
1183 /* Asking for both common channels and averaging */
1184 if (st->avg_log2 && ad4030_is_common_byte_asked(st, *scan_mask))
1185 return false;
1186
1187 return true;
1188 }
1189
1190 static const struct iio_buffer_setup_ops ad4030_buffer_setup_ops = {
1191 .validate_scan_mask = ad4030_validate_scan_mask,
1192 };
1193
ad4030_prepare_offload_msg(struct iio_dev * indio_dev)1194 static void ad4030_prepare_offload_msg(struct iio_dev *indio_dev)
1195 {
1196 struct ad4030_state *st = iio_priv(indio_dev);
1197 u8 offload_bpw;
1198
1199 if (st->mode == AD4030_OUT_DATA_MD_30_AVERAGED_DIFF)
1200 offload_bpw = 32;
1201 else
1202 offload_bpw = st->chip->precision_bits;
1203
1204 st->offload_xfer.bits_per_word = offload_bpw;
1205 st->offload_xfer.len = spi_bpw_to_bytes(offload_bpw);
1206 st->offload_xfer.offload_flags = SPI_OFFLOAD_XFER_RX_STREAM;
1207 spi_message_init_with_transfers(&st->offload_msg, &st->offload_xfer, 1);
1208 }
1209
ad4030_offload_buffer_postenable(struct iio_dev * indio_dev)1210 static int ad4030_offload_buffer_postenable(struct iio_dev *indio_dev)
1211 {
1212 struct ad4030_state *st = iio_priv(indio_dev);
1213 unsigned int reg_modes;
1214 int ret;
1215
1216 /*
1217 * When data from 2 analog input channels is output through a single
1218 * bus line (interleaved mode (LANE_MD == 0b11)) and gets pushed through
1219 * DMA, extra hardware is required to do the de-interleaving. While we
1220 * don't support such hardware configurations, disallow interleaved mode
1221 * when using SPI offload.
1222 */
1223 ret = regmap_read(st->regmap, AD4030_REG_MODES, ®_modes);
1224 if (ret)
1225 return ret;
1226
1227 if (st->chip->num_voltage_inputs > 1 &&
1228 FIELD_GET(AD4030_REG_MODES_MASK_LANE_MODE, reg_modes) == AD4030_LANE_MD_INTERLEAVED)
1229 return -EINVAL;
1230
1231 ad4030_prepare_offload_msg(indio_dev);
1232 st->offload_msg.offload = st->offload;
1233 ret = spi_optimize_message(st->spi, &st->offload_msg);
1234 if (ret)
1235 return ret;
1236
1237 ret = pwm_set_waveform_might_sleep(st->cnv_trigger, &st->cnv_wf, false);
1238 if (ret)
1239 goto out_unoptimize;
1240
1241 ret = spi_offload_trigger_enable(st->offload, st->offload_trigger,
1242 &st->offload_trigger_config);
1243 if (ret)
1244 goto out_pwm_disable;
1245
1246 return 0;
1247
1248 out_pwm_disable:
1249 pwm_disable(st->cnv_trigger);
1250 out_unoptimize:
1251 spi_unoptimize_message(&st->offload_msg);
1252
1253 return ret;
1254 }
1255
ad4030_offload_buffer_predisable(struct iio_dev * indio_dev)1256 static int ad4030_offload_buffer_predisable(struct iio_dev *indio_dev)
1257 {
1258 struct ad4030_state *st = iio_priv(indio_dev);
1259
1260 spi_offload_trigger_disable(st->offload, st->offload_trigger);
1261
1262 pwm_disable(st->cnv_trigger);
1263
1264 spi_unoptimize_message(&st->offload_msg);
1265
1266 return 0;
1267 }
1268
1269 static const struct iio_buffer_setup_ops ad4030_offload_buffer_setup_ops = {
1270 .postenable = &ad4030_offload_buffer_postenable,
1271 .predisable = &ad4030_offload_buffer_predisable,
1272 };
1273
ad4030_regulators_get(struct ad4030_state * st)1274 static int ad4030_regulators_get(struct ad4030_state *st)
1275 {
1276 struct device *dev = &st->spi->dev;
1277 static const char * const ids[] = { "vdd-5v", "vdd-1v8" };
1278 int ret;
1279
1280 ret = devm_regulator_bulk_get_enable(dev, ARRAY_SIZE(ids), ids);
1281 if (ret)
1282 return dev_err_probe(dev, ret, "Failed to enable regulators\n");
1283
1284 st->vio_uv = devm_regulator_get_enable_read_voltage(dev, "vio");
1285 if (st->vio_uv < 0)
1286 return dev_err_probe(dev, st->vio_uv,
1287 "Failed to enable and read vio voltage\n");
1288
1289 st->vref_uv = devm_regulator_get_enable_read_voltage(dev, "ref");
1290 if (st->vref_uv < 0) {
1291 if (st->vref_uv != -ENODEV)
1292 return dev_err_probe(dev, st->vref_uv,
1293 "Failed to read ref voltage\n");
1294
1295 /* if not using optional REF, the REFIN must be used */
1296 st->vref_uv = devm_regulator_get_enable_read_voltage(dev,
1297 "refin");
1298 if (st->vref_uv < 0)
1299 return dev_err_probe(dev, st->vref_uv,
1300 "Failed to read refin voltage\n");
1301 }
1302
1303 return 0;
1304 }
1305
ad4030_reset(struct ad4030_state * st)1306 static int ad4030_reset(struct ad4030_state *st)
1307 {
1308 struct device *dev = &st->spi->dev;
1309 struct gpio_desc *reset;
1310
1311 reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
1312 if (IS_ERR(reset))
1313 return dev_err_probe(dev, PTR_ERR(reset),
1314 "Failed to get reset GPIO\n");
1315
1316 if (reset) {
1317 ndelay(50);
1318 gpiod_set_value_cansleep(reset, 0);
1319 return 0;
1320 }
1321
1322 return regmap_write(st->regmap, AD4030_REG_INTERFACE_CONFIG_A,
1323 AD4030_REG_INTERFACE_CONFIG_A_SW_RESET);
1324 }
1325
ad4030_detect_chip_info(const struct ad4030_state * st)1326 static int ad4030_detect_chip_info(const struct ad4030_state *st)
1327 {
1328 unsigned int grade;
1329 int ret;
1330
1331 ret = regmap_read(st->regmap, AD4030_REG_CHIP_GRADE, &grade);
1332 if (ret)
1333 return ret;
1334
1335 grade = FIELD_GET(AD4030_REG_CHIP_GRADE_MASK_CHIP_GRADE, grade);
1336 if (grade != st->chip->grade)
1337 dev_warn(&st->spi->dev, "Unknown grade(0x%x) for %s\n", grade,
1338 st->chip->name);
1339
1340 return 0;
1341 }
1342
ad4030_pwm_get(struct ad4030_state * st)1343 static int ad4030_pwm_get(struct ad4030_state *st)
1344 {
1345 struct device *dev = &st->spi->dev;
1346
1347 st->cnv_trigger = devm_pwm_get(dev, NULL);
1348 if (IS_ERR(st->cnv_trigger))
1349 return dev_err_probe(dev, PTR_ERR(st->cnv_trigger),
1350 "Failed to get CNV PWM\n");
1351
1352 /*
1353 * Preemptively disable the PWM, since we only want to enable it with
1354 * the buffer.
1355 */
1356 pwm_disable(st->cnv_trigger);
1357
1358 return 0;
1359 }
1360
ad4030_config(struct ad4030_state * st)1361 static int ad4030_config(struct ad4030_state *st)
1362 {
1363 int ret;
1364 u8 reg_modes;
1365
1366 st->offset_avail[0] = (int)BIT(st->chip->precision_bits - 1) * -1;
1367 st->offset_avail[1] = 1;
1368 st->offset_avail[2] = BIT(st->chip->precision_bits - 1) - 1;
1369
1370 if (st->chip->num_voltage_inputs > 1)
1371 reg_modes = FIELD_PREP(AD4030_REG_MODES_MASK_LANE_MODE,
1372 AD4030_LANE_MD_INTERLEAVED);
1373 else
1374 reg_modes = FIELD_PREP(AD4030_REG_MODES_MASK_LANE_MODE,
1375 AD4030_LANE_MD_1_PER_CH);
1376
1377 ret = regmap_write(st->regmap, AD4030_REG_MODES, reg_modes);
1378 if (ret)
1379 return ret;
1380
1381 if (st->vio_uv < AD4030_VIO_THRESHOLD_UV)
1382 return regmap_write(st->regmap, AD4030_REG_IO,
1383 AD4030_REG_IO_MASK_IO2X);
1384
1385 return 0;
1386 }
1387
ad4030_spi_offload_setup(struct iio_dev * indio_dev,struct ad4030_state * st)1388 static int ad4030_spi_offload_setup(struct iio_dev *indio_dev,
1389 struct ad4030_state *st)
1390 {
1391 struct device *dev = &st->spi->dev;
1392 struct dma_chan *rx_dma;
1393
1394 indio_dev->setup_ops = &ad4030_offload_buffer_setup_ops;
1395
1396 st->offload_trigger = devm_spi_offload_trigger_get(dev, st->offload,
1397 SPI_OFFLOAD_TRIGGER_PERIODIC);
1398 if (IS_ERR(st->offload_trigger))
1399 return dev_err_probe(dev, PTR_ERR(st->offload_trigger),
1400 "failed to get offload trigger\n");
1401
1402 st->offload_trigger_config.type = SPI_OFFLOAD_TRIGGER_PERIODIC;
1403
1404 rx_dma = devm_spi_offload_rx_stream_request_dma_chan(dev, st->offload);
1405 if (IS_ERR(rx_dma))
1406 return dev_err_probe(dev, PTR_ERR(rx_dma),
1407 "failed to get offload RX DMA\n");
1408
1409 return devm_iio_dmaengine_buffer_setup_with_handle(dev, indio_dev, rx_dma,
1410 IIO_BUFFER_DIRECTION_IN);
1411 }
1412
ad4030_setup_pga(struct device * dev,struct iio_dev * indio_dev,struct ad4030_state * st)1413 static int ad4030_setup_pga(struct device *dev, struct iio_dev *indio_dev,
1414 struct ad4030_state *st)
1415 {
1416 /* Setup GPIOs for PGA control */
1417 st->pga_gpios = devm_gpiod_get_array(dev, "pga", GPIOD_OUT_LOW);
1418 if (IS_ERR(st->pga_gpios))
1419 return dev_err_probe(dev, PTR_ERR(st->pga_gpios),
1420 "Failed to get PGA gpios.\n");
1421
1422 if (st->pga_gpios->ndescs != ADAQ4616_PGA_PINS)
1423 return dev_err_probe(dev, -EINVAL,
1424 "Expected %d GPIOs for PGA control.\n",
1425 ADAQ4616_PGA_PINS);
1426
1427 st->scale_avail_size = ARRAY_SIZE(adaq4216_hw_gains_vpv);
1428 st->pga_index = 0;
1429
1430 return 0;
1431 }
1432
ad4030_probe(struct spi_device * spi)1433 static int ad4030_probe(struct spi_device *spi)
1434 {
1435 struct device *dev = &spi->dev;
1436 struct iio_dev *indio_dev;
1437 struct ad4030_state *st;
1438 int ret;
1439
1440 indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
1441 if (!indio_dev)
1442 return -ENOMEM;
1443
1444 st = iio_priv(indio_dev);
1445 st->spi = spi;
1446
1447 st->regmap = devm_regmap_init(dev, &ad4030_regmap_bus, st,
1448 &ad4030_regmap_config);
1449 if (IS_ERR(st->regmap))
1450 return dev_err_probe(dev, PTR_ERR(st->regmap),
1451 "Failed to initialize regmap\n");
1452
1453 st->chip = spi_get_device_match_data(spi);
1454 if (!st->chip)
1455 return -EINVAL;
1456
1457 ret = ad4030_regulators_get(st);
1458 if (ret)
1459 return ret;
1460
1461 /*
1462 * From datasheet: "Perform a reset no sooner than 3ms after the power
1463 * supplies are valid and stable"
1464 */
1465 fsleep(3000);
1466
1467 ret = ad4030_reset(st);
1468 if (ret)
1469 return ret;
1470
1471 ret = ad4030_detect_chip_info(st);
1472 if (ret)
1473 return ret;
1474
1475 if (st->chip->has_pga) {
1476 ret = ad4030_setup_pga(dev, indio_dev, st);
1477 if (ret)
1478 return ret;
1479
1480 ad4030_fill_scale_avail(st);
1481 }
1482
1483 ret = ad4030_config(st);
1484 if (ret)
1485 return ret;
1486
1487 st->cnv_gpio = devm_gpiod_get(dev, "cnv", GPIOD_OUT_LOW);
1488 if (IS_ERR(st->cnv_gpio))
1489 return dev_err_probe(dev, PTR_ERR(st->cnv_gpio),
1490 "Failed to get cnv gpio\n");
1491
1492 indio_dev->name = st->chip->name;
1493 indio_dev->modes = INDIO_DIRECT_MODE;
1494 indio_dev->info = &ad4030_iio_info;
1495 indio_dev->available_scan_masks = st->chip->available_masks;
1496
1497 st->offload = devm_spi_offload_get(dev, spi, &ad4030_offload_config);
1498 ret = PTR_ERR_OR_ZERO(st->offload);
1499 /* Fall back to low speed usage when no SPI offload is available. */
1500 if (ret == -ENODEV) {
1501 /*
1502 * One hardware channel is split in two software channels when
1503 * using common byte mode. Add one more channel for the timestamp.
1504 */
1505 indio_dev->num_channels = 2 * st->chip->num_voltage_inputs + 1;
1506 indio_dev->channels = st->chip->channels;
1507
1508 ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
1509 iio_pollfunc_store_time,
1510 ad4030_trigger_handler,
1511 &ad4030_buffer_setup_ops);
1512 if (ret)
1513 return dev_err_probe(dev, ret,
1514 "Failed to setup triggered buffer\n");
1515 } else if (ret) {
1516 return dev_err_probe(dev, ret, "failed to get offload\n");
1517 } else {
1518 /*
1519 * Offloaded SPI transfers can't support software timestamp so
1520 * no additional timestamp channel is added.
1521 */
1522 indio_dev->num_channels = st->chip->num_voltage_inputs;
1523 indio_dev->channels = st->chip->offload_channels;
1524 ret = ad4030_spi_offload_setup(indio_dev, st);
1525 if (ret)
1526 return dev_err_probe(dev, ret,
1527 "Failed to setup SPI offload\n");
1528
1529 ret = ad4030_pwm_get(st);
1530 if (ret)
1531 return dev_err_probe(dev, ret, "Failed to get PWM\n");
1532
1533 /*
1534 * Start with a slower sampling rate so there is some room for
1535 * adjusting the sample averaging and the sampling frequency
1536 * without hitting the maximum conversion rate.
1537 */
1538 ret = ad4030_update_conversion_rate(st, st->chip->max_sample_rate_hz >> 4,
1539 st->avg_log2);
1540 if (ret)
1541 return dev_err_probe(dev, ret,
1542 "Failed to set offload samp freq\n");
1543 }
1544
1545 return devm_iio_device_register(dev, indio_dev);
1546 }
1547
1548 static const unsigned long ad4030_channel_masks[] = {
1549 /* Differential only */
1550 BIT(0),
1551 /* Differential and common-mode voltage */
1552 GENMASK(1, 0),
1553 0,
1554 };
1555
1556 static const unsigned long ad4630_channel_masks[] = {
1557 /* Differential only */
1558 BIT(1) | BIT(0),
1559 /* Differential with common byte */
1560 GENMASK(3, 0),
1561 0,
1562 };
1563
1564 static const struct iio_scan_type ad4030_24_scan_types[] = {
1565 [AD4030_SCAN_TYPE_NORMAL] = {
1566 .sign = 's',
1567 .storagebits = 32,
1568 .realbits = 24,
1569 .shift = 8,
1570 .endianness = IIO_BE,
1571 },
1572 [AD4030_SCAN_TYPE_AVG] = {
1573 .sign = 's',
1574 .storagebits = 32,
1575 .realbits = 30,
1576 .shift = 2,
1577 .endianness = IIO_BE,
1578 },
1579 };
1580
1581 static const struct iio_scan_type ad4030_24_offload_scan_types[] = {
1582 [AD4030_SCAN_TYPE_NORMAL] = {
1583 .sign = 's',
1584 .realbits = 24,
1585 .storagebits = 32,
1586 .shift = 0,
1587 .endianness = IIO_CPU,
1588 },
1589 [AD4030_SCAN_TYPE_AVG] = {
1590 .sign = 's',
1591 .realbits = 30,
1592 .storagebits = 32,
1593 .shift = 2,
1594 .endianness = IIO_CPU,
1595 },
1596 };
1597
1598 static const struct iio_scan_type ad4030_16_scan_types[] = {
1599 [AD4030_SCAN_TYPE_NORMAL] = {
1600 .sign = 's',
1601 .realbits = 16,
1602 .storagebits = 32,
1603 .shift = 16,
1604 .endianness = IIO_BE,
1605 },
1606 [AD4030_SCAN_TYPE_AVG] = {
1607 .sign = 's',
1608 .storagebits = 32,
1609 .realbits = 30,
1610 .shift = 2,
1611 .endianness = IIO_BE,
1612 }
1613 };
1614
1615 static const struct iio_scan_type ad4030_16_offload_scan_types[] = {
1616 [AD4030_SCAN_TYPE_NORMAL] = {
1617 .sign = 's',
1618 .realbits = 16,
1619 .storagebits = 32,
1620 .shift = 0,
1621 .endianness = IIO_CPU,
1622 },
1623 [AD4030_SCAN_TYPE_AVG] = {
1624 .sign = 's',
1625 .realbits = 30,
1626 .storagebits = 32,
1627 .shift = 2,
1628 .endianness = IIO_CPU,
1629 },
1630 };
1631
1632 static const struct ad4030_chip_info ad4030_24_chip_info = {
1633 .name = "ad4030-24",
1634 .available_masks = ad4030_channel_masks,
1635 .channels = {
1636 AD4030_CHAN_DIFF(0, ad4030_24_scan_types),
1637 AD4030_CHAN_CMO(1, 0),
1638 IIO_CHAN_SOFT_TIMESTAMP(2),
1639 },
1640 .offload_channels = {
1641 AD4030_OFFLOAD_CHAN_DIFF(0, ad4030_24_offload_scan_types),
1642 },
1643 .grade = AD4030_REG_CHIP_GRADE_AD4030_24_GRADE,
1644 .precision_bits = 24,
1645 .num_voltage_inputs = 1,
1646 .tcyc_ns = AD4030_TCYC_ADJUSTED_NS,
1647 .max_sample_rate_hz = 2 * HZ_PER_MHZ,
1648 };
1649
1650 static const struct ad4030_chip_info ad4630_16_chip_info = {
1651 .name = "ad4630-16",
1652 .available_masks = ad4630_channel_masks,
1653 .channels = {
1654 AD4030_CHAN_DIFF(0, ad4030_16_scan_types),
1655 AD4030_CHAN_DIFF(1, ad4030_16_scan_types),
1656 AD4030_CHAN_CMO(2, 0),
1657 AD4030_CHAN_CMO(3, 1),
1658 IIO_CHAN_SOFT_TIMESTAMP(4),
1659 },
1660 .offload_channels = {
1661 AD4030_OFFLOAD_CHAN_DIFF(0, ad4030_16_offload_scan_types),
1662 AD4030_OFFLOAD_CHAN_DIFF(1, ad4030_16_offload_scan_types),
1663 },
1664 .grade = AD4030_REG_CHIP_GRADE_AD4630_16_GRADE,
1665 .precision_bits = 16,
1666 .num_voltage_inputs = 2,
1667 .tcyc_ns = AD4030_TCYC_ADJUSTED_NS,
1668 .max_sample_rate_hz = 2 * HZ_PER_MHZ,
1669 };
1670
1671 static const struct ad4030_chip_info ad4630_24_chip_info = {
1672 .name = "ad4630-24",
1673 .available_masks = ad4630_channel_masks,
1674 .channels = {
1675 AD4030_CHAN_DIFF(0, ad4030_24_scan_types),
1676 AD4030_CHAN_DIFF(1, ad4030_24_scan_types),
1677 AD4030_CHAN_CMO(2, 0),
1678 AD4030_CHAN_CMO(3, 1),
1679 IIO_CHAN_SOFT_TIMESTAMP(4),
1680 },
1681 .offload_channels = {
1682 AD4030_OFFLOAD_CHAN_DIFF(0, ad4030_24_offload_scan_types),
1683 AD4030_OFFLOAD_CHAN_DIFF(1, ad4030_24_offload_scan_types),
1684 },
1685 .grade = AD4030_REG_CHIP_GRADE_AD4630_24_GRADE,
1686 .precision_bits = 24,
1687 .num_voltage_inputs = 2,
1688 .tcyc_ns = AD4030_TCYC_ADJUSTED_NS,
1689 .max_sample_rate_hz = 2 * HZ_PER_MHZ,
1690 };
1691
1692 static const struct ad4030_chip_info ad4632_16_chip_info = {
1693 .name = "ad4632-16",
1694 .available_masks = ad4630_channel_masks,
1695 .channels = {
1696 AD4030_CHAN_DIFF(0, ad4030_16_scan_types),
1697 AD4030_CHAN_DIFF(1, ad4030_16_scan_types),
1698 AD4030_CHAN_CMO(2, 0),
1699 AD4030_CHAN_CMO(3, 1),
1700 IIO_CHAN_SOFT_TIMESTAMP(4),
1701 },
1702 .offload_channels = {
1703 AD4030_OFFLOAD_CHAN_DIFF(0, ad4030_16_offload_scan_types),
1704 AD4030_OFFLOAD_CHAN_DIFF(1, ad4030_16_offload_scan_types),
1705 },
1706 .grade = AD4030_REG_CHIP_GRADE_AD4632_16_GRADE,
1707 .precision_bits = 16,
1708 .num_voltage_inputs = 2,
1709 .tcyc_ns = AD4632_TCYC_ADJUSTED_NS,
1710 .max_sample_rate_hz = 500 * HZ_PER_KHZ,
1711 };
1712
1713 static const struct ad4030_chip_info ad4632_24_chip_info = {
1714 .name = "ad4632-24",
1715 .available_masks = ad4630_channel_masks,
1716 .channels = {
1717 AD4030_CHAN_DIFF(0, ad4030_24_scan_types),
1718 AD4030_CHAN_DIFF(1, ad4030_24_scan_types),
1719 AD4030_CHAN_CMO(2, 0),
1720 AD4030_CHAN_CMO(3, 1),
1721 IIO_CHAN_SOFT_TIMESTAMP(4),
1722 },
1723 .offload_channels = {
1724 AD4030_OFFLOAD_CHAN_DIFF(0, ad4030_24_offload_scan_types),
1725 AD4030_OFFLOAD_CHAN_DIFF(1, ad4030_24_offload_scan_types),
1726 },
1727 .grade = AD4030_REG_CHIP_GRADE_AD4632_24_GRADE,
1728 .precision_bits = 24,
1729 .num_voltage_inputs = 2,
1730 .tcyc_ns = AD4632_TCYC_ADJUSTED_NS,
1731 .max_sample_rate_hz = 500 * HZ_PER_KHZ,
1732 };
1733
1734 static const struct ad4030_chip_info adaq4216_chip_info = {
1735 .name = "adaq4216",
1736 .available_masks = ad4030_channel_masks,
1737 .channels = {
1738 ADAQ4216_CHAN_DIFF(0, ad4030_16_scan_types),
1739 AD4030_CHAN_CMO(1, 0),
1740 IIO_CHAN_SOFT_TIMESTAMP(2),
1741 },
1742 .offload_channels = {
1743 ADAQ4216_OFFLOAD_CHAN_DIFF(0, ad4030_16_offload_scan_types),
1744 },
1745 .grade = AD4030_REG_CHIP_GRADE_ADAQ4216_GRADE,
1746 .precision_bits = 16,
1747 .has_pga = true,
1748 .num_voltage_inputs = 1,
1749 .tcyc_ns = AD4030_TCYC_ADJUSTED_NS,
1750 .max_sample_rate_hz = 2 * HZ_PER_MHZ,
1751 };
1752
1753 static const struct ad4030_chip_info adaq4224_chip_info = {
1754 .name = "adaq4224",
1755 .available_masks = ad4030_channel_masks,
1756 .channels = {
1757 ADAQ4216_CHAN_DIFF(0, ad4030_24_scan_types),
1758 AD4030_CHAN_CMO(1, 0),
1759 IIO_CHAN_SOFT_TIMESTAMP(2),
1760 },
1761 .offload_channels = {
1762 ADAQ4216_OFFLOAD_CHAN_DIFF(0, ad4030_24_offload_scan_types),
1763 },
1764 .grade = AD4030_REG_CHIP_GRADE_ADAQ4224_GRADE,
1765 .precision_bits = 24,
1766 .has_pga = true,
1767 .num_voltage_inputs = 1,
1768 .tcyc_ns = AD4030_TCYC_ADJUSTED_NS,
1769 .max_sample_rate_hz = 2 * HZ_PER_MHZ,
1770 };
1771
1772 static const struct spi_device_id ad4030_id_table[] = {
1773 { .name = "ad4030-24", .driver_data = (kernel_ulong_t)&ad4030_24_chip_info },
1774 { .name = "ad4630-16", .driver_data = (kernel_ulong_t)&ad4630_16_chip_info },
1775 { .name = "ad4630-24", .driver_data = (kernel_ulong_t)&ad4630_24_chip_info },
1776 { .name = "ad4632-16", .driver_data = (kernel_ulong_t)&ad4632_16_chip_info },
1777 { .name = "ad4632-24", .driver_data = (kernel_ulong_t)&ad4632_24_chip_info },
1778 { .name = "adaq4216", .driver_data = (kernel_ulong_t)&adaq4216_chip_info },
1779 { .name = "adaq4224", .driver_data = (kernel_ulong_t)&adaq4224_chip_info },
1780 { }
1781 };
1782 MODULE_DEVICE_TABLE(spi, ad4030_id_table);
1783
1784 static const struct of_device_id ad4030_of_match[] = {
1785 { .compatible = "adi,ad4030-24", .data = &ad4030_24_chip_info },
1786 { .compatible = "adi,ad4630-16", .data = &ad4630_16_chip_info },
1787 { .compatible = "adi,ad4630-24", .data = &ad4630_24_chip_info },
1788 { .compatible = "adi,ad4632-16", .data = &ad4632_16_chip_info },
1789 { .compatible = "adi,ad4632-24", .data = &ad4632_24_chip_info },
1790 { .compatible = "adi,adaq4216", .data = &adaq4216_chip_info },
1791 { .compatible = "adi,adaq4224", .data = &adaq4224_chip_info },
1792 { }
1793 };
1794 MODULE_DEVICE_TABLE(of, ad4030_of_match);
1795
1796 static struct spi_driver ad4030_driver = {
1797 .driver = {
1798 .name = "ad4030",
1799 .of_match_table = ad4030_of_match,
1800 },
1801 .probe = ad4030_probe,
1802 .id_table = ad4030_id_table,
1803 };
1804 module_spi_driver(ad4030_driver);
1805
1806 MODULE_AUTHOR("Esteban Blanc <eblanc@baylibre.com>");
1807 MODULE_DESCRIPTION("Analog Devices AD4630 ADC family driver");
1808 MODULE_LICENSE("GPL");
1809 MODULE_IMPORT_NS("IIO_DMAENGINE_BUFFER");
1810