1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2022 Analog Devices, Inc.
7 #include <linux/bitfield.h>
8 #include <linux/bitops.h>
10 #include <linux/clk-provider.h>
11 #include <linux/delay.h>
12 #include <linux/device.h>
13 #include <linux/err.h>
14 #include <linux/gpio/driver.h>
15 #include <linux/interrupt.h>
16 #include <linux/irq.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/property.h>
20 #include <linux/regmap.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/spi/spi.h>
23 #include <linux/units.h>
25 #include <asm/div64.h>
26 #include <asm/unaligned.h>
28 #include <linux/iio/buffer.h>
29 #include <linux/iio/iio.h>
30 #include <linux/iio/kfifo_buf.h>
31 #include <linux/iio/sysfs.h>
33 #define AD4130_NAME "ad4130"
35 #define AD4130_COMMS_READ_MASK BIT(6)
37 #define AD4130_STATUS_REG 0x00
39 #define AD4130_ADC_CONTROL_REG 0x01
40 #define AD4130_ADC_CONTROL_BIPOLAR_MASK BIT(14)
41 #define AD4130_ADC_CONTROL_INT_REF_VAL_MASK BIT(13)
42 #define AD4130_INT_REF_2_5V 2500000
43 #define AD4130_INT_REF_1_25V 1250000
44 #define AD4130_ADC_CONTROL_CSB_EN_MASK BIT(9)
45 #define AD4130_ADC_CONTROL_INT_REF_EN_MASK BIT(8)
46 #define AD4130_ADC_CONTROL_MODE_MASK GENMASK(5, 2)
47 #define AD4130_ADC_CONTROL_MCLK_SEL_MASK GENMASK(1, 0)
48 #define AD4130_MCLK_FREQ_76_8KHZ 76800
49 #define AD4130_MCLK_FREQ_153_6KHZ 153600
51 #define AD4130_DATA_REG 0x02
53 #define AD4130_IO_CONTROL_REG 0x03
54 #define AD4130_IO_CONTROL_INT_PIN_SEL_MASK GENMASK(9, 8)
55 #define AD4130_IO_CONTROL_GPIO_DATA_MASK GENMASK(7, 4)
56 #define AD4130_IO_CONTROL_GPIO_CTRL_MASK GENMASK(3, 0)
58 #define AD4130_VBIAS_REG 0x04
60 #define AD4130_ID_REG 0x05
62 #define AD4130_ERROR_REG 0x06
64 #define AD4130_ERROR_EN_REG 0x07
66 #define AD4130_MCLK_COUNT_REG 0x08
68 #define AD4130_CHANNEL_X_REG(x) (0x09 + (x))
69 #define AD4130_CHANNEL_EN_MASK BIT(23)
70 #define AD4130_CHANNEL_SETUP_MASK GENMASK(22, 20)
71 #define AD4130_CHANNEL_AINP_MASK GENMASK(17, 13)
72 #define AD4130_CHANNEL_AINM_MASK GENMASK(12, 8)
73 #define AD4130_CHANNEL_IOUT1_MASK GENMASK(7, 4)
74 #define AD4130_CHANNEL_IOUT2_MASK GENMASK(3, 0)
76 #define AD4130_CONFIG_X_REG(x) (0x19 + (x))
77 #define AD4130_CONFIG_IOUT1_VAL_MASK GENMASK(15, 13)
78 #define AD4130_CONFIG_IOUT2_VAL_MASK GENMASK(12, 10)
79 #define AD4130_CONFIG_BURNOUT_MASK GENMASK(9, 8)
80 #define AD4130_CONFIG_REF_BUFP_MASK BIT(7)
81 #define AD4130_CONFIG_REF_BUFM_MASK BIT(6)
82 #define AD4130_CONFIG_REF_SEL_MASK GENMASK(5, 4)
83 #define AD4130_CONFIG_PGA_MASK GENMASK(3, 1)
85 #define AD4130_FILTER_X_REG(x) (0x21 + (x))
86 #define AD4130_FILTER_MODE_MASK GENMASK(15, 12)
87 #define AD4130_FILTER_SELECT_MASK GENMASK(10, 0)
88 #define AD4130_FILTER_SELECT_MIN 1
90 #define AD4130_OFFSET_X_REG(x) (0x29 + (x))
92 #define AD4130_GAIN_X_REG(x) (0x31 + (x))
94 #define AD4130_MISC_REG 0x39
96 #define AD4130_FIFO_CONTROL_REG 0x3a
97 #define AD4130_FIFO_CONTROL_HEADER_MASK BIT(18)
98 #define AD4130_FIFO_CONTROL_MODE_MASK GENMASK(17, 16)
99 #define AD4130_FIFO_CONTROL_WM_INT_EN_MASK BIT(9)
100 #define AD4130_FIFO_CONTROL_WM_MASK GENMASK(7, 0)
101 #define AD4130_WATERMARK_256 0
103 #define AD4130_FIFO_STATUS_REG 0x3b
105 #define AD4130_FIFO_THRESHOLD_REG 0x3c
107 #define AD4130_FIFO_DATA_REG 0x3d
108 #define AD4130_FIFO_SIZE 256
109 #define AD4130_FIFO_MAX_SAMPLE_SIZE 3
111 #define AD4130_MAX_ANALOG_PINS 16
112 #define AD4130_MAX_CHANNELS 16
113 #define AD4130_MAX_DIFF_INPUTS 30
114 #define AD4130_MAX_GPIOS 4
115 #define AD4130_MAX_ODR 2400
116 #define AD4130_MAX_PGA 8
117 #define AD4130_MAX_SETUPS 8
119 #define AD4130_AIN2_P1 0x2
120 #define AD4130_AIN3_P2 0x3
122 #define AD4130_RESET_BUF_SIZE 8
123 #define AD4130_RESET_SLEEP_US (160 * MICRO / AD4130_MCLK_FREQ_76_8KHZ)
125 #define AD4130_INVALID_SLOT -1
127 static const unsigned int ad4130_reg_size[] = {
128 [AD4130_STATUS_REG] = 1,
129 [AD4130_ADC_CONTROL_REG] = 2,
130 [AD4130_DATA_REG] = 3,
131 [AD4130_IO_CONTROL_REG] = 2,
132 [AD4130_VBIAS_REG] = 2,
134 [AD4130_ERROR_REG] = 2,
135 [AD4130_ERROR_EN_REG] = 2,
136 [AD4130_MCLK_COUNT_REG] = 1,
137 [AD4130_CHANNEL_X_REG(0) ... AD4130_CHANNEL_X_REG(AD4130_MAX_CHANNELS - 1)] = 3,
138 [AD4130_CONFIG_X_REG(0) ... AD4130_CONFIG_X_REG(AD4130_MAX_SETUPS - 1)] = 2,
139 [AD4130_FILTER_X_REG(0) ... AD4130_FILTER_X_REG(AD4130_MAX_SETUPS - 1)] = 3,
140 [AD4130_OFFSET_X_REG(0) ... AD4130_OFFSET_X_REG(AD4130_MAX_SETUPS - 1)] = 3,
141 [AD4130_GAIN_X_REG(0) ... AD4130_GAIN_X_REG(AD4130_MAX_SETUPS - 1)] = 3,
142 [AD4130_MISC_REG] = 2,
143 [AD4130_FIFO_CONTROL_REG] = 3,
144 [AD4130_FIFO_STATUS_REG] = 1,
145 [AD4130_FIFO_THRESHOLD_REG] = 3,
146 [AD4130_FIFO_DATA_REG] = 3,
149 enum ad4130_int_ref_val {
150 AD4130_INT_REF_VAL_2_5V,
151 AD4130_INT_REF_VAL_1_25V,
154 enum ad4130_mclk_sel {
156 AD4130_MCLK_76_8KHZ_OUT,
157 AD4130_MCLK_76_8KHZ_EXT,
158 AD4130_MCLK_153_6KHZ_EXT,
161 enum ad4130_int_pin_sel {
173 AD4130_IOUT_100000NA,
174 AD4130_IOUT_150000NA,
175 AD4130_IOUT_200000NA,
180 enum ad4130_burnout {
182 AD4130_BURNOUT_500NA,
183 AD4130_BURNOUT_2000NA,
184 AD4130_BURNOUT_4000NA,
188 enum ad4130_ref_sel {
191 AD4130_REF_REFOUT_AVSS,
192 AD4130_REF_AVDD_AVSS,
196 enum ad4130_fifo_mode {
197 AD4130_FIFO_MODE_DISABLED = 0b00,
198 AD4130_FIFO_MODE_WM = 0b01,
202 AD4130_MODE_CONTINUOUS = 0b0000,
203 AD4130_MODE_IDLE = 0b0100,
206 enum ad4130_filter_mode {
208 AD4130_FILTER_SINC4_SINC1,
210 AD4130_FILTER_SINC3_REJ60,
211 AD4130_FILTER_SINC3_SINC1,
212 AD4130_FILTER_SINC3_PF1,
213 AD4130_FILTER_SINC3_PF2,
214 AD4130_FILTER_SINC3_PF3,
215 AD4130_FILTER_SINC3_PF4,
218 enum ad4130_pin_function {
220 AD4130_PIN_FN_SPECIAL = BIT(0),
221 AD4130_PIN_FN_DIFF = BIT(1),
222 AD4130_PIN_FN_EXCITATION = BIT(2),
223 AD4130_PIN_FN_VBIAS = BIT(3),
226 struct ad4130_setup_info {
227 unsigned int iout0_val;
228 unsigned int iout1_val;
229 unsigned int burnout;
233 enum ad4130_filter_mode filter_mode;
238 struct ad4130_slot_info {
239 struct ad4130_setup_info setup;
240 unsigned int enabled_channels;
241 unsigned int channels;
244 struct ad4130_chan_info {
245 struct ad4130_setup_info setup;
253 struct ad4130_filter_config {
254 enum ad4130_filter_mode filter_mode;
255 unsigned int odr_div;
257 enum iio_available_type samp_freq_avail_type;
258 int samp_freq_avail_len;
259 int samp_freq_avail[3][2];
262 struct ad4130_state {
263 struct regmap *regmap;
264 struct spi_device *spi;
266 struct regulator_bulk_data regulators[4];
271 * Synchronize access to members the of driver state, and ensure
272 * atomicity of consecutive regmap operations.
275 struct completion completion;
277 struct iio_chan_spec chans[AD4130_MAX_CHANNELS];
278 struct ad4130_chan_info chans_info[AD4130_MAX_CHANNELS];
279 struct ad4130_slot_info slots_info[AD4130_MAX_SETUPS];
280 enum ad4130_pin_function pins_fn[AD4130_MAX_ANALOG_PINS];
281 u32 vbias_pins[AD4130_MAX_ANALOG_PINS];
283 int scale_tbls[AD4130_REF_SEL_MAX][AD4130_MAX_PGA][2];
285 struct clk_hw int_clk_hw;
293 unsigned int num_enabled_channels;
294 unsigned int effective_watermark;
295 unsigned int watermark;
297 struct spi_message fifo_msg;
298 struct spi_transfer fifo_xfer[2];
301 * DMA (thus cache coherency maintenance) requires any transfer
302 * buffers to live in their own cache lines. As the use of these
303 * buffers is synchronous, all of the buffers used for DMA in this
304 * driver may share a cache line.
306 u8 reset_buf[AD4130_RESET_BUF_SIZE] __aligned(IIO_DMA_MINALIGN);
307 u8 reg_write_tx_buf[4];
308 u8 reg_read_tx_buf[1];
309 u8 reg_read_rx_buf[3];
311 u8 fifo_rx_buf[AD4130_FIFO_SIZE *
312 AD4130_FIFO_MAX_SAMPLE_SIZE];
315 static const char * const ad4130_int_pin_names[] = {
316 [AD4130_INT_PIN_INT] = "int",
317 [AD4130_INT_PIN_CLK] = "clk",
318 [AD4130_INT_PIN_P2] = "p2",
319 [AD4130_INT_PIN_DOUT] = "dout",
322 static const unsigned int ad4130_iout_current_na_tbl[AD4130_IOUT_MAX] = {
323 [AD4130_IOUT_OFF] = 0,
324 [AD4130_IOUT_100NA] = 100,
325 [AD4130_IOUT_10000NA] = 10000,
326 [AD4130_IOUT_20000NA] = 20000,
327 [AD4130_IOUT_50000NA] = 50000,
328 [AD4130_IOUT_100000NA] = 100000,
329 [AD4130_IOUT_150000NA] = 150000,
330 [AD4130_IOUT_200000NA] = 200000,
333 static const unsigned int ad4130_burnout_current_na_tbl[AD4130_BURNOUT_MAX] = {
334 [AD4130_BURNOUT_OFF] = 0,
335 [AD4130_BURNOUT_500NA] = 500,
336 [AD4130_BURNOUT_2000NA] = 2000,
337 [AD4130_BURNOUT_4000NA] = 4000,
340 #define AD4130_VARIABLE_ODR_CONFIG(_filter_mode, _odr_div, _fs_max) \
342 .filter_mode = (_filter_mode), \
343 .odr_div = (_odr_div), \
344 .fs_max = (_fs_max), \
345 .samp_freq_avail_type = IIO_AVAIL_RANGE, \
346 .samp_freq_avail = { \
347 { AD4130_MAX_ODR, (_odr_div) * (_fs_max) }, \
348 { AD4130_MAX_ODR, (_odr_div) * (_fs_max) }, \
349 { AD4130_MAX_ODR, (_odr_div) }, \
353 #define AD4130_FIXED_ODR_CONFIG(_filter_mode, _odr_div) \
355 .filter_mode = (_filter_mode), \
356 .odr_div = (_odr_div), \
357 .fs_max = AD4130_FILTER_SELECT_MIN, \
358 .samp_freq_avail_type = IIO_AVAIL_LIST, \
359 .samp_freq_avail_len = 1, \
360 .samp_freq_avail = { \
361 { AD4130_MAX_ODR, (_odr_div) }, \
365 static const struct ad4130_filter_config ad4130_filter_configs[] = {
366 AD4130_VARIABLE_ODR_CONFIG(AD4130_FILTER_SINC4, 1, 10),
367 AD4130_VARIABLE_ODR_CONFIG(AD4130_FILTER_SINC4_SINC1, 11, 10),
368 AD4130_VARIABLE_ODR_CONFIG(AD4130_FILTER_SINC3, 1, 2047),
369 AD4130_VARIABLE_ODR_CONFIG(AD4130_FILTER_SINC3_REJ60, 1, 2047),
370 AD4130_VARIABLE_ODR_CONFIG(AD4130_FILTER_SINC3_SINC1, 10, 2047),
371 AD4130_FIXED_ODR_CONFIG(AD4130_FILTER_SINC3_PF1, 92),
372 AD4130_FIXED_ODR_CONFIG(AD4130_FILTER_SINC3_PF2, 100),
373 AD4130_FIXED_ODR_CONFIG(AD4130_FILTER_SINC3_PF3, 124),
374 AD4130_FIXED_ODR_CONFIG(AD4130_FILTER_SINC3_PF4, 148),
377 static const char * const ad4130_filter_modes_str[] = {
378 [AD4130_FILTER_SINC4] = "sinc4",
379 [AD4130_FILTER_SINC4_SINC1] = "sinc4+sinc1",
380 [AD4130_FILTER_SINC3] = "sinc3",
381 [AD4130_FILTER_SINC3_REJ60] = "sinc3+rej60",
382 [AD4130_FILTER_SINC3_SINC1] = "sinc3+sinc1",
383 [AD4130_FILTER_SINC3_PF1] = "sinc3+pf1",
384 [AD4130_FILTER_SINC3_PF2] = "sinc3+pf2",
385 [AD4130_FILTER_SINC3_PF3] = "sinc3+pf3",
386 [AD4130_FILTER_SINC3_PF4] = "sinc3+pf4",
389 static int ad4130_get_reg_size(struct ad4130_state *st, unsigned int reg,
392 if (reg >= ARRAY_SIZE(ad4130_reg_size))
395 *size = ad4130_reg_size[reg];
400 static unsigned int ad4130_data_reg_size(struct ad4130_state *st)
402 unsigned int data_reg_size;
405 ret = ad4130_get_reg_size(st, AD4130_DATA_REG, &data_reg_size);
409 return data_reg_size;
412 static unsigned int ad4130_resolution(struct ad4130_state *st)
414 return ad4130_data_reg_size(st) * BITS_PER_BYTE;
417 static int ad4130_reg_write(void *context, unsigned int reg, unsigned int val)
419 struct ad4130_state *st = context;
423 ret = ad4130_get_reg_size(st, reg, &size);
427 st->reg_write_tx_buf[0] = reg;
431 put_unaligned_be24(val, &st->reg_write_tx_buf[1]);
434 put_unaligned_be16(val, &st->reg_write_tx_buf[1]);
437 st->reg_write_tx_buf[1] = val;
443 return spi_write(st->spi, st->reg_write_tx_buf, size + 1);
446 static int ad4130_reg_read(void *context, unsigned int reg, unsigned int *val)
448 struct ad4130_state *st = context;
449 struct spi_transfer t[] = {
451 .tx_buf = st->reg_read_tx_buf,
452 .len = sizeof(st->reg_read_tx_buf),
455 .rx_buf = st->reg_read_rx_buf,
461 ret = ad4130_get_reg_size(st, reg, &size);
465 st->reg_read_tx_buf[0] = AD4130_COMMS_READ_MASK | reg;
468 ret = spi_sync_transfer(st->spi, t, ARRAY_SIZE(t));
474 *val = get_unaligned_be24(st->reg_read_rx_buf);
477 *val = get_unaligned_be16(st->reg_read_rx_buf);
480 *val = st->reg_read_rx_buf[0];
489 static const struct regmap_config ad4130_regmap_config = {
490 .reg_read = ad4130_reg_read,
491 .reg_write = ad4130_reg_write,
494 static int ad4130_gpio_init_valid_mask(struct gpio_chip *gc,
495 unsigned long *valid_mask,
498 struct ad4130_state *st = gpiochip_get_data(gc);
502 * Output-only GPIO functionality is available on pins AIN2 through
503 * AIN5. If these pins are used for anything else, do not expose them.
505 for (i = 0; i < ngpios; i++) {
506 unsigned int pin = i + AD4130_AIN2_P1;
507 bool valid = st->pins_fn[pin] == AD4130_PIN_FN_NONE;
509 __assign_bit(i, valid_mask, valid);
515 static int ad4130_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
517 return GPIO_LINE_DIRECTION_OUT;
520 static void ad4130_gpio_set(struct gpio_chip *gc, unsigned int offset,
523 struct ad4130_state *st = gpiochip_get_data(gc);
524 unsigned int mask = FIELD_PREP(AD4130_IO_CONTROL_GPIO_DATA_MASK,
527 regmap_update_bits(st->regmap, AD4130_IO_CONTROL_REG, mask,
531 static int ad4130_set_mode(struct ad4130_state *st, enum ad4130_mode mode)
533 return regmap_update_bits(st->regmap, AD4130_ADC_CONTROL_REG,
534 AD4130_ADC_CONTROL_MODE_MASK,
535 FIELD_PREP(AD4130_ADC_CONTROL_MODE_MASK, mode));
538 static int ad4130_set_watermark_interrupt_en(struct ad4130_state *st, bool en)
540 return regmap_update_bits(st->regmap, AD4130_FIFO_CONTROL_REG,
541 AD4130_FIFO_CONTROL_WM_INT_EN_MASK,
542 FIELD_PREP(AD4130_FIFO_CONTROL_WM_INT_EN_MASK, en));
545 static unsigned int ad4130_watermark_reg_val(unsigned int val)
547 if (val == AD4130_FIFO_SIZE)
548 val = AD4130_WATERMARK_256;
553 static int ad4130_set_fifo_mode(struct ad4130_state *st,
554 enum ad4130_fifo_mode mode)
556 return regmap_update_bits(st->regmap, AD4130_FIFO_CONTROL_REG,
557 AD4130_FIFO_CONTROL_MODE_MASK,
558 FIELD_PREP(AD4130_FIFO_CONTROL_MODE_MASK, mode));
561 static void ad4130_push_fifo_data(struct iio_dev *indio_dev)
563 struct ad4130_state *st = iio_priv(indio_dev);
564 unsigned int data_reg_size = ad4130_data_reg_size(st);
565 unsigned int transfer_len = st->effective_watermark * data_reg_size;
566 unsigned int set_size = st->num_enabled_channels * data_reg_size;
570 st->fifo_tx_buf[1] = ad4130_watermark_reg_val(st->effective_watermark);
571 st->fifo_xfer[1].len = transfer_len;
573 ret = spi_sync(st->spi, &st->fifo_msg);
577 for (i = 0; i < transfer_len; i += set_size)
578 iio_push_to_buffers(indio_dev, &st->fifo_rx_buf[i]);
581 static irqreturn_t ad4130_irq_handler(int irq, void *private)
583 struct iio_dev *indio_dev = private;
584 struct ad4130_state *st = iio_priv(indio_dev);
586 if (iio_buffer_enabled(indio_dev))
587 ad4130_push_fifo_data(indio_dev);
589 complete(&st->completion);
594 static int ad4130_find_slot(struct ad4130_state *st,
595 struct ad4130_setup_info *target_setup_info,
596 unsigned int *slot, bool *overwrite)
600 *slot = AD4130_INVALID_SLOT;
603 for (i = 0; i < AD4130_MAX_SETUPS; i++) {
604 struct ad4130_slot_info *slot_info = &st->slots_info[i];
606 /* Immediately accept a matching setup info. */
607 if (!memcmp(target_setup_info, &slot_info->setup,
608 sizeof(*target_setup_info))) {
613 /* Ignore all setups which are used by enabled channels. */
614 if (slot_info->enabled_channels)
617 /* Find the least used slot. */
618 if (*slot == AD4130_INVALID_SLOT ||
619 slot_info->channels < st->slots_info[*slot].channels)
623 if (*slot == AD4130_INVALID_SLOT)
631 static void ad4130_unlink_channel(struct ad4130_state *st, unsigned int channel)
633 struct ad4130_chan_info *chan_info = &st->chans_info[channel];
634 struct ad4130_slot_info *slot_info = &st->slots_info[chan_info->slot];
636 chan_info->slot = AD4130_INVALID_SLOT;
637 slot_info->channels--;
640 static int ad4130_unlink_slot(struct ad4130_state *st, unsigned int slot)
644 for (i = 0; i < AD4130_MAX_CHANNELS; i++) {
645 struct ad4130_chan_info *chan_info = &st->chans_info[i];
647 if (!chan_info->initialized || chan_info->slot != slot)
650 ad4130_unlink_channel(st, i);
656 static int ad4130_link_channel_slot(struct ad4130_state *st,
657 unsigned int channel, unsigned int slot)
659 struct ad4130_slot_info *slot_info = &st->slots_info[slot];
660 struct ad4130_chan_info *chan_info = &st->chans_info[channel];
663 ret = regmap_update_bits(st->regmap, AD4130_CHANNEL_X_REG(channel),
664 AD4130_CHANNEL_SETUP_MASK,
665 FIELD_PREP(AD4130_CHANNEL_SETUP_MASK, slot));
669 chan_info->slot = slot;
670 slot_info->channels++;
675 static int ad4130_write_slot_setup(struct ad4130_state *st,
677 struct ad4130_setup_info *setup_info)
682 val = FIELD_PREP(AD4130_CONFIG_IOUT1_VAL_MASK, setup_info->iout0_val) |
683 FIELD_PREP(AD4130_CONFIG_IOUT1_VAL_MASK, setup_info->iout1_val) |
684 FIELD_PREP(AD4130_CONFIG_BURNOUT_MASK, setup_info->burnout) |
685 FIELD_PREP(AD4130_CONFIG_REF_BUFP_MASK, setup_info->ref_bufp) |
686 FIELD_PREP(AD4130_CONFIG_REF_BUFM_MASK, setup_info->ref_bufm) |
687 FIELD_PREP(AD4130_CONFIG_REF_SEL_MASK, setup_info->ref_sel) |
688 FIELD_PREP(AD4130_CONFIG_PGA_MASK, setup_info->pga);
690 ret = regmap_write(st->regmap, AD4130_CONFIG_X_REG(slot), val);
694 val = FIELD_PREP(AD4130_FILTER_MODE_MASK, setup_info->filter_mode) |
695 FIELD_PREP(AD4130_FILTER_SELECT_MASK, setup_info->fs);
697 ret = regmap_write(st->regmap, AD4130_FILTER_X_REG(slot), val);
701 memcpy(&st->slots_info[slot].setup, setup_info, sizeof(*setup_info));
706 static int ad4130_write_channel_setup(struct ad4130_state *st,
707 unsigned int channel, bool on_enable)
709 struct ad4130_chan_info *chan_info = &st->chans_info[channel];
710 struct ad4130_setup_info *setup_info = &chan_info->setup;
716 * The following cases need to be handled.
718 * 1. Enabled and linked channel with setup changes:
719 * - Find a slot. If not possible, return error.
720 * - Unlink channel from current slot.
721 * - If the slot has channels linked to it, unlink all channels, and
722 * write the new setup to it.
723 * - Link channel to new slot.
725 * 2. Soon to be enabled and unlinked channel:
726 * - Find a slot. If not possible, return error.
727 * - If the slot has channels linked to it, unlink all channels, and
728 * write the new setup to it.
729 * - Link channel to the slot.
731 * 3. Disabled and linked channel with setup changes:
732 * - Unlink channel from current slot.
734 * 4. Soon to be enabled and linked channel:
735 * 5. Disabled and unlinked channel with setup changes:
740 if (on_enable && chan_info->slot != AD4130_INVALID_SLOT)
743 if (!on_enable && !chan_info->enabled) {
744 if (chan_info->slot != AD4130_INVALID_SLOT)
746 ad4130_unlink_channel(st, channel);
753 ret = ad4130_find_slot(st, setup_info, &slot, &overwrite);
757 if (chan_info->slot != AD4130_INVALID_SLOT)
759 ad4130_unlink_channel(st, channel);
762 ret = ad4130_unlink_slot(st, slot);
766 ret = ad4130_write_slot_setup(st, slot, setup_info);
771 return ad4130_link_channel_slot(st, channel, slot);
774 static int ad4130_set_channel_enable(struct ad4130_state *st,
775 unsigned int channel, bool status)
777 struct ad4130_chan_info *chan_info = &st->chans_info[channel];
778 struct ad4130_slot_info *slot_info;
781 if (chan_info->enabled == status)
785 ret = ad4130_write_channel_setup(st, channel, true);
790 slot_info = &st->slots_info[chan_info->slot];
792 ret = regmap_update_bits(st->regmap, AD4130_CHANNEL_X_REG(channel),
793 AD4130_CHANNEL_EN_MASK,
794 FIELD_PREP(AD4130_CHANNEL_EN_MASK, status));
798 slot_info->enabled_channels += status ? 1 : -1;
799 chan_info->enabled = status;
805 * Table 58. FILTER_MODE_n bits and Filter Types of the datasheet describes
806 * the relation between filter mode, ODR and FS.
808 * Notice that the max ODR of each filter mode is not necessarily the
809 * absolute max ODR supported by the chip.
811 * The ODR divider is not explicitly specified, but it can be deduced based
812 * on the ODR range of each filter mode.
814 * For example, for Sinc4+Sinc1, max ODR is 218.18. That means that the
815 * absolute max ODR is divided by 11 to achieve the max ODR of this filter
818 * The formulas for converting between ODR and FS for a specific filter
819 * mode can be deduced from the same table.
821 * Notice that FS = 1 actually means max ODR, and that ODR decreases by
822 * (maximum ODR / maximum FS) for each increment of FS.
824 * odr = MAX_ODR / odr_div * (1 - (fs - 1) / fs_max) <=>
825 * odr = MAX_ODR * (1 - (fs - 1) / fs_max) / odr_div <=>
826 * odr = MAX_ODR * (1 - (fs - 1) / fs_max) / odr_div <=>
827 * odr = MAX_ODR * (fs_max - fs + 1) / (fs_max * odr_div)
828 * (used in ad4130_fs_to_freq)
830 * For the opposite formula, FS can be extracted from the last one.
832 * MAX_ODR * (fs_max - fs + 1) = fs_max * odr_div * odr <=>
833 * fs_max - fs + 1 = fs_max * odr_div * odr / MAX_ODR <=>
834 * fs = 1 + fs_max - fs_max * odr_div * odr / MAX_ODR
835 * (used in ad4130_fs_to_freq)
838 static void ad4130_freq_to_fs(enum ad4130_filter_mode filter_mode,
839 int val, int val2, unsigned int *fs)
841 const struct ad4130_filter_config *filter_config =
842 &ad4130_filter_configs[filter_mode];
843 u64 dividend, divisor;
846 dividend = filter_config->fs_max * filter_config->odr_div *
847 ((u64)val * NANO + val2);
848 divisor = (u64)AD4130_MAX_ODR * NANO;
850 temp = AD4130_FILTER_SELECT_MIN + filter_config->fs_max -
851 DIV64_U64_ROUND_CLOSEST(dividend, divisor);
853 if (temp < AD4130_FILTER_SELECT_MIN)
854 temp = AD4130_FILTER_SELECT_MIN;
855 else if (temp > filter_config->fs_max)
856 temp = filter_config->fs_max;
861 static void ad4130_fs_to_freq(enum ad4130_filter_mode filter_mode,
862 unsigned int fs, int *val, int *val2)
864 const struct ad4130_filter_config *filter_config =
865 &ad4130_filter_configs[filter_mode];
866 unsigned int dividend, divisor;
869 dividend = (filter_config->fs_max - fs + AD4130_FILTER_SELECT_MIN) *
871 divisor = filter_config->fs_max * filter_config->odr_div;
873 temp = div_u64((u64)dividend * NANO, divisor);
874 *val = div_u64_rem(temp, NANO, val2);
877 static int ad4130_set_filter_mode(struct iio_dev *indio_dev,
878 const struct iio_chan_spec *chan,
881 struct ad4130_state *st = iio_priv(indio_dev);
882 unsigned int channel = chan->scan_index;
883 struct ad4130_chan_info *chan_info = &st->chans_info[channel];
884 struct ad4130_setup_info *setup_info = &chan_info->setup;
885 enum ad4130_filter_mode old_filter_mode;
886 int freq_val, freq_val2;
890 mutex_lock(&st->lock);
891 if (setup_info->filter_mode == val)
894 old_fs = setup_info->fs;
895 old_filter_mode = setup_info->filter_mode;
898 * When switching between filter modes, try to match the ODR as
899 * close as possible. To do this, convert the current FS into ODR
900 * using the old filter mode, then convert it back into FS using
901 * the new filter mode.
903 ad4130_fs_to_freq(setup_info->filter_mode, setup_info->fs,
904 &freq_val, &freq_val2);
906 ad4130_freq_to_fs(val, freq_val, freq_val2, &setup_info->fs);
908 setup_info->filter_mode = val;
910 ret = ad4130_write_channel_setup(st, channel, false);
912 setup_info->fs = old_fs;
913 setup_info->filter_mode = old_filter_mode;
917 mutex_unlock(&st->lock);
922 static int ad4130_get_filter_mode(struct iio_dev *indio_dev,
923 const struct iio_chan_spec *chan)
925 struct ad4130_state *st = iio_priv(indio_dev);
926 unsigned int channel = chan->scan_index;
927 struct ad4130_setup_info *setup_info = &st->chans_info[channel].setup;
928 enum ad4130_filter_mode filter_mode;
930 mutex_lock(&st->lock);
931 filter_mode = setup_info->filter_mode;
932 mutex_unlock(&st->lock);
937 static const struct iio_enum ad4130_filter_mode_enum = {
938 .items = ad4130_filter_modes_str,
939 .num_items = ARRAY_SIZE(ad4130_filter_modes_str),
940 .set = ad4130_set_filter_mode,
941 .get = ad4130_get_filter_mode,
944 static const struct iio_chan_spec_ext_info ad4130_filter_mode_ext_info[] = {
945 IIO_ENUM("filter_mode", IIO_SEPARATE, &ad4130_filter_mode_enum),
946 IIO_ENUM_AVAILABLE("filter_mode", IIO_SHARED_BY_TYPE,
947 &ad4130_filter_mode_enum),
951 static const struct iio_chan_spec ad4130_channel_template = {
955 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
956 BIT(IIO_CHAN_INFO_SCALE) |
957 BIT(IIO_CHAN_INFO_OFFSET) |
958 BIT(IIO_CHAN_INFO_SAMP_FREQ),
959 .info_mask_separate_available = BIT(IIO_CHAN_INFO_SCALE) |
960 BIT(IIO_CHAN_INFO_SAMP_FREQ),
961 .ext_info = ad4130_filter_mode_ext_info,
964 .endianness = IIO_BE,
968 static int ad4130_set_channel_pga(struct ad4130_state *st, unsigned int channel,
971 struct ad4130_chan_info *chan_info = &st->chans_info[channel];
972 struct ad4130_setup_info *setup_info = &chan_info->setup;
973 unsigned int pga, old_pga;
976 for (pga = 0; pga < AD4130_MAX_PGA; pga++)
977 if (val == st->scale_tbls[setup_info->ref_sel][pga][0] &&
978 val2 == st->scale_tbls[setup_info->ref_sel][pga][1])
981 if (pga == AD4130_MAX_PGA)
984 mutex_lock(&st->lock);
985 if (pga == setup_info->pga)
988 old_pga = setup_info->pga;
989 setup_info->pga = pga;
991 ret = ad4130_write_channel_setup(st, channel, false);
993 setup_info->pga = old_pga;
996 mutex_unlock(&st->lock);
1001 static int ad4130_set_channel_freq(struct ad4130_state *st,
1002 unsigned int channel, int val, int val2)
1004 struct ad4130_chan_info *chan_info = &st->chans_info[channel];
1005 struct ad4130_setup_info *setup_info = &chan_info->setup;
1006 unsigned int fs, old_fs;
1009 mutex_lock(&st->lock);
1010 old_fs = setup_info->fs;
1012 ad4130_freq_to_fs(setup_info->filter_mode, val, val2, &fs);
1014 if (fs == setup_info->fs)
1017 setup_info->fs = fs;
1019 ret = ad4130_write_channel_setup(st, channel, false);
1021 setup_info->fs = old_fs;
1024 mutex_unlock(&st->lock);
1029 static int _ad4130_read_sample(struct iio_dev *indio_dev, unsigned int channel,
1032 struct ad4130_state *st = iio_priv(indio_dev);
1035 ret = ad4130_set_channel_enable(st, channel, true);
1039 reinit_completion(&st->completion);
1041 ret = ad4130_set_mode(st, AD4130_MODE_CONTINUOUS);
1045 ret = wait_for_completion_timeout(&st->completion,
1046 msecs_to_jiffies(1000));
1050 ret = ad4130_set_mode(st, AD4130_MODE_IDLE);
1054 ret = regmap_read(st->regmap, AD4130_DATA_REG, val);
1058 ret = ad4130_set_channel_enable(st, channel, false);
1065 static int ad4130_read_sample(struct iio_dev *indio_dev, unsigned int channel,
1068 struct ad4130_state *st = iio_priv(indio_dev);
1071 ret = iio_device_claim_direct_mode(indio_dev);
1075 mutex_lock(&st->lock);
1076 ret = _ad4130_read_sample(indio_dev, channel, val);
1077 mutex_unlock(&st->lock);
1079 iio_device_release_direct_mode(indio_dev);
1084 static int ad4130_read_raw(struct iio_dev *indio_dev,
1085 struct iio_chan_spec const *chan,
1086 int *val, int *val2, long info)
1088 struct ad4130_state *st = iio_priv(indio_dev);
1089 unsigned int channel = chan->scan_index;
1090 struct ad4130_setup_info *setup_info = &st->chans_info[channel].setup;
1093 case IIO_CHAN_INFO_RAW:
1094 return ad4130_read_sample(indio_dev, channel, val);
1095 case IIO_CHAN_INFO_SCALE:
1096 mutex_lock(&st->lock);
1097 *val = st->scale_tbls[setup_info->ref_sel][setup_info->pga][0];
1098 *val2 = st->scale_tbls[setup_info->ref_sel][setup_info->pga][1];
1099 mutex_unlock(&st->lock);
1101 return IIO_VAL_INT_PLUS_NANO;
1102 case IIO_CHAN_INFO_OFFSET:
1103 *val = st->bipolar ? -BIT(chan->scan_type.realbits - 1) : 0;
1106 case IIO_CHAN_INFO_SAMP_FREQ:
1107 mutex_lock(&st->lock);
1108 ad4130_fs_to_freq(setup_info->filter_mode, setup_info->fs,
1110 mutex_unlock(&st->lock);
1112 return IIO_VAL_INT_PLUS_NANO;
1118 static int ad4130_read_avail(struct iio_dev *indio_dev,
1119 struct iio_chan_spec const *chan,
1120 const int **vals, int *type, int *length,
1123 struct ad4130_state *st = iio_priv(indio_dev);
1124 unsigned int channel = chan->scan_index;
1125 struct ad4130_setup_info *setup_info = &st->chans_info[channel].setup;
1126 const struct ad4130_filter_config *filter_config;
1129 case IIO_CHAN_INFO_SCALE:
1130 *vals = (int *)st->scale_tbls[setup_info->ref_sel];
1131 *length = ARRAY_SIZE(st->scale_tbls[setup_info->ref_sel]) * 2;
1133 *type = IIO_VAL_INT_PLUS_NANO;
1135 return IIO_AVAIL_LIST;
1136 case IIO_CHAN_INFO_SAMP_FREQ:
1137 mutex_lock(&st->lock);
1138 filter_config = &ad4130_filter_configs[setup_info->filter_mode];
1139 mutex_unlock(&st->lock);
1141 *vals = (int *)filter_config->samp_freq_avail;
1142 *length = filter_config->samp_freq_avail_len * 2;
1143 *type = IIO_VAL_FRACTIONAL;
1145 return filter_config->samp_freq_avail_type;
1151 static int ad4130_write_raw_get_fmt(struct iio_dev *indio_dev,
1152 struct iio_chan_spec const *chan,
1156 case IIO_CHAN_INFO_SCALE:
1157 case IIO_CHAN_INFO_SAMP_FREQ:
1158 return IIO_VAL_INT_PLUS_NANO;
1164 static int ad4130_write_raw(struct iio_dev *indio_dev,
1165 struct iio_chan_spec const *chan,
1166 int val, int val2, long info)
1168 struct ad4130_state *st = iio_priv(indio_dev);
1169 unsigned int channel = chan->scan_index;
1172 case IIO_CHAN_INFO_SCALE:
1173 return ad4130_set_channel_pga(st, channel, val, val2);
1174 case IIO_CHAN_INFO_SAMP_FREQ:
1175 return ad4130_set_channel_freq(st, channel, val, val2);
1181 static int ad4130_reg_access(struct iio_dev *indio_dev, unsigned int reg,
1182 unsigned int writeval, unsigned int *readval)
1184 struct ad4130_state *st = iio_priv(indio_dev);
1187 return regmap_read(st->regmap, reg, readval);
1189 return regmap_write(st->regmap, reg, writeval);
1192 static int ad4130_update_scan_mode(struct iio_dev *indio_dev,
1193 const unsigned long *scan_mask)
1195 struct ad4130_state *st = iio_priv(indio_dev);
1196 unsigned int channel;
1197 unsigned int val = 0;
1200 mutex_lock(&st->lock);
1202 for_each_set_bit(channel, scan_mask, indio_dev->num_channels) {
1203 ret = ad4130_set_channel_enable(st, channel, true);
1210 st->num_enabled_channels = val;
1213 mutex_unlock(&st->lock);
1218 static int ad4130_set_fifo_watermark(struct iio_dev *indio_dev, unsigned int val)
1220 struct ad4130_state *st = iio_priv(indio_dev);
1224 if (val > AD4130_FIFO_SIZE)
1227 eff = val * st->num_enabled_channels;
1228 if (eff > AD4130_FIFO_SIZE)
1230 * Always set watermark to a multiple of the number of
1231 * enabled channels to avoid making the FIFO unaligned.
1233 eff = rounddown(AD4130_FIFO_SIZE, st->num_enabled_channels);
1235 mutex_lock(&st->lock);
1237 ret = regmap_update_bits(st->regmap, AD4130_FIFO_CONTROL_REG,
1238 AD4130_FIFO_CONTROL_WM_MASK,
1239 FIELD_PREP(AD4130_FIFO_CONTROL_WM_MASK,
1240 ad4130_watermark_reg_val(eff)));
1244 st->effective_watermark = eff;
1245 st->watermark = val;
1248 mutex_unlock(&st->lock);
1253 static const struct iio_info ad4130_info = {
1254 .read_raw = ad4130_read_raw,
1255 .read_avail = ad4130_read_avail,
1256 .write_raw_get_fmt = ad4130_write_raw_get_fmt,
1257 .write_raw = ad4130_write_raw,
1258 .update_scan_mode = ad4130_update_scan_mode,
1259 .hwfifo_set_watermark = ad4130_set_fifo_watermark,
1260 .debugfs_reg_access = ad4130_reg_access,
1263 static int ad4130_buffer_postenable(struct iio_dev *indio_dev)
1265 struct ad4130_state *st = iio_priv(indio_dev);
1268 mutex_lock(&st->lock);
1270 ret = ad4130_set_watermark_interrupt_en(st, true);
1274 ret = irq_set_irq_type(st->spi->irq, st->inv_irq_trigger);
1278 ret = ad4130_set_fifo_mode(st, AD4130_FIFO_MODE_WM);
1282 ret = ad4130_set_mode(st, AD4130_MODE_CONTINUOUS);
1285 mutex_unlock(&st->lock);
1290 static int ad4130_buffer_predisable(struct iio_dev *indio_dev)
1292 struct ad4130_state *st = iio_priv(indio_dev);
1296 mutex_lock(&st->lock);
1298 ret = ad4130_set_mode(st, AD4130_MODE_IDLE);
1302 ret = irq_set_irq_type(st->spi->irq, st->irq_trigger);
1306 ret = ad4130_set_fifo_mode(st, AD4130_FIFO_MODE_DISABLED);
1310 ret = ad4130_set_watermark_interrupt_en(st, false);
1315 * update_scan_mode() is not called in the disable path, disable all
1318 for (i = 0; i < indio_dev->num_channels; i++) {
1319 ret = ad4130_set_channel_enable(st, i, false);
1325 mutex_unlock(&st->lock);
1330 static const struct iio_buffer_setup_ops ad4130_buffer_ops = {
1331 .postenable = ad4130_buffer_postenable,
1332 .predisable = ad4130_buffer_predisable,
1335 static ssize_t hwfifo_watermark_show(struct device *dev,
1336 struct device_attribute *attr, char *buf)
1338 struct ad4130_state *st = iio_priv(dev_to_iio_dev(dev));
1341 mutex_lock(&st->lock);
1342 val = st->watermark;
1343 mutex_unlock(&st->lock);
1345 return sysfs_emit(buf, "%d\n", val);
1348 static ssize_t hwfifo_enabled_show(struct device *dev,
1349 struct device_attribute *attr, char *buf)
1351 struct ad4130_state *st = iio_priv(dev_to_iio_dev(dev));
1355 ret = regmap_read(st->regmap, AD4130_FIFO_CONTROL_REG, &val);
1359 val = FIELD_GET(AD4130_FIFO_CONTROL_MODE_MASK, val);
1361 return sysfs_emit(buf, "%d\n", val != AD4130_FIFO_MODE_DISABLED);
1364 static ssize_t hwfifo_watermark_min_show(struct device *dev,
1365 struct device_attribute *attr,
1368 return sysfs_emit(buf, "%s\n", "1");
1371 static ssize_t hwfifo_watermark_max_show(struct device *dev,
1372 struct device_attribute *attr,
1375 return sysfs_emit(buf, "%s\n", __stringify(AD4130_FIFO_SIZE));
1378 static IIO_DEVICE_ATTR_RO(hwfifo_watermark_min, 0);
1379 static IIO_DEVICE_ATTR_RO(hwfifo_watermark_max, 0);
1380 static IIO_DEVICE_ATTR_RO(hwfifo_watermark, 0);
1381 static IIO_DEVICE_ATTR_RO(hwfifo_enabled, 0);
1383 static const struct iio_dev_attr *ad4130_fifo_attributes[] = {
1384 &iio_dev_attr_hwfifo_watermark_min,
1385 &iio_dev_attr_hwfifo_watermark_max,
1386 &iio_dev_attr_hwfifo_watermark,
1387 &iio_dev_attr_hwfifo_enabled,
1391 static int _ad4130_find_table_index(const unsigned int *tbl, size_t len,
1396 for (i = 0; i < len; i++)
1403 #define ad4130_find_table_index(table, val) \
1404 _ad4130_find_table_index(table, ARRAY_SIZE(table), val)
1406 static int ad4130_get_ref_voltage(struct ad4130_state *st,
1407 enum ad4130_ref_sel ref_sel)
1410 case AD4130_REF_REFIN1:
1411 return regulator_get_voltage(st->regulators[2].consumer);
1412 case AD4130_REF_REFIN2:
1413 return regulator_get_voltage(st->regulators[3].consumer);
1414 case AD4130_REF_AVDD_AVSS:
1415 return regulator_get_voltage(st->regulators[0].consumer);
1416 case AD4130_REF_REFOUT_AVSS:
1417 return st->int_ref_uv;
1423 static int ad4130_parse_fw_setup(struct ad4130_state *st,
1424 struct fwnode_handle *child,
1425 struct ad4130_setup_info *setup_info)
1427 struct device *dev = &st->spi->dev;
1432 fwnode_property_read_u32(child, "adi,excitation-current-0-nanoamp", &tmp);
1433 ret = ad4130_find_table_index(ad4130_iout_current_na_tbl, tmp);
1435 return dev_err_probe(dev, ret,
1436 "Invalid excitation current %unA\n", tmp);
1437 setup_info->iout0_val = ret;
1440 fwnode_property_read_u32(child, "adi,excitation-current-1-nanoamp", &tmp);
1441 ret = ad4130_find_table_index(ad4130_iout_current_na_tbl, tmp);
1443 return dev_err_probe(dev, ret,
1444 "Invalid excitation current %unA\n", tmp);
1445 setup_info->iout1_val = ret;
1448 fwnode_property_read_u32(child, "adi,burnout-current-nanoamp", &tmp);
1449 ret = ad4130_find_table_index(ad4130_burnout_current_na_tbl, tmp);
1451 return dev_err_probe(dev, ret,
1452 "Invalid burnout current %unA\n", tmp);
1453 setup_info->burnout = ret;
1455 setup_info->ref_bufp = fwnode_property_read_bool(child, "adi,buffered-positive");
1456 setup_info->ref_bufm = fwnode_property_read_bool(child, "adi,buffered-negative");
1458 setup_info->ref_sel = AD4130_REF_REFIN1;
1459 fwnode_property_read_u32(child, "adi,reference-select",
1460 &setup_info->ref_sel);
1461 if (setup_info->ref_sel >= AD4130_REF_SEL_MAX)
1462 return dev_err_probe(dev, -EINVAL,
1463 "Invalid reference selected %u\n",
1464 setup_info->ref_sel);
1466 if (setup_info->ref_sel == AD4130_REF_REFOUT_AVSS)
1467 st->int_ref_en = true;
1469 ret = ad4130_get_ref_voltage(st, setup_info->ref_sel);
1471 return dev_err_probe(dev, ret, "Cannot use reference %u\n",
1472 setup_info->ref_sel);
1477 static int ad4130_validate_diff_channel(struct ad4130_state *st, u32 pin)
1479 struct device *dev = &st->spi->dev;
1481 if (pin >= AD4130_MAX_DIFF_INPUTS)
1482 return dev_err_probe(dev, -EINVAL,
1483 "Invalid differential channel %u\n", pin);
1485 if (pin >= AD4130_MAX_ANALOG_PINS)
1488 if (st->pins_fn[pin] == AD4130_PIN_FN_SPECIAL)
1489 return dev_err_probe(dev, -EINVAL,
1490 "Pin %u already used with fn %u\n", pin,
1493 st->pins_fn[pin] |= AD4130_PIN_FN_DIFF;
1498 static int ad4130_validate_diff_channels(struct ad4130_state *st,
1499 u32 *pins, unsigned int len)
1504 for (i = 0; i < len; i++) {
1505 ret = ad4130_validate_diff_channel(st, pins[i]);
1513 static int ad4130_validate_excitation_pin(struct ad4130_state *st, u32 pin)
1515 struct device *dev = &st->spi->dev;
1517 if (pin >= AD4130_MAX_ANALOG_PINS)
1518 return dev_err_probe(dev, -EINVAL,
1519 "Invalid excitation pin %u\n", pin);
1521 if (st->pins_fn[pin] == AD4130_PIN_FN_SPECIAL)
1522 return dev_err_probe(dev, -EINVAL,
1523 "Pin %u already used with fn %u\n", pin,
1526 st->pins_fn[pin] |= AD4130_PIN_FN_EXCITATION;
1531 static int ad4130_validate_vbias_pin(struct ad4130_state *st, u32 pin)
1533 struct device *dev = &st->spi->dev;
1535 if (pin >= AD4130_MAX_ANALOG_PINS)
1536 return dev_err_probe(dev, -EINVAL, "Invalid vbias pin %u\n",
1539 if (st->pins_fn[pin] == AD4130_PIN_FN_SPECIAL)
1540 return dev_err_probe(dev, -EINVAL,
1541 "Pin %u already used with fn %u\n", pin,
1544 st->pins_fn[pin] |= AD4130_PIN_FN_VBIAS;
1549 static int ad4130_validate_vbias_pins(struct ad4130_state *st,
1550 u32 *pins, unsigned int len)
1555 for (i = 0; i < st->num_vbias_pins; i++) {
1556 ret = ad4130_validate_vbias_pin(st, pins[i]);
1564 static int ad4130_parse_fw_channel(struct iio_dev *indio_dev,
1565 struct fwnode_handle *child)
1567 struct ad4130_state *st = iio_priv(indio_dev);
1568 unsigned int resolution = ad4130_resolution(st);
1569 unsigned int index = indio_dev->num_channels++;
1570 struct device *dev = &st->spi->dev;
1571 struct ad4130_chan_info *chan_info;
1572 struct iio_chan_spec *chan;
1576 if (index >= AD4130_MAX_CHANNELS)
1577 return dev_err_probe(dev, -EINVAL, "Too many channels\n");
1579 chan = &st->chans[index];
1580 chan_info = &st->chans_info[index];
1582 *chan = ad4130_channel_template;
1583 chan->scan_type.realbits = resolution;
1584 chan->scan_type.storagebits = resolution;
1585 chan->scan_index = index;
1587 chan_info->slot = AD4130_INVALID_SLOT;
1588 chan_info->setup.fs = AD4130_FILTER_SELECT_MIN;
1589 chan_info->initialized = true;
1591 ret = fwnode_property_read_u32_array(child, "diff-channels", pins,
1596 ret = ad4130_validate_diff_channels(st, pins, ARRAY_SIZE(pins));
1600 chan->channel = pins[0];
1601 chan->channel2 = pins[1];
1603 ret = ad4130_parse_fw_setup(st, child, &chan_info->setup);
1607 fwnode_property_read_u32(child, "adi,excitation-pin-0",
1609 if (chan_info->setup.iout0_val != AD4130_IOUT_OFF) {
1610 ret = ad4130_validate_excitation_pin(st, chan_info->iout0);
1615 fwnode_property_read_u32(child, "adi,excitation-pin-1",
1617 if (chan_info->setup.iout1_val != AD4130_IOUT_OFF) {
1618 ret = ad4130_validate_excitation_pin(st, chan_info->iout1);
1626 static int ad4130_parse_fw_children(struct iio_dev *indio_dev)
1628 struct ad4130_state *st = iio_priv(indio_dev);
1629 struct device *dev = &st->spi->dev;
1630 struct fwnode_handle *child;
1633 indio_dev->channels = st->chans;
1635 device_for_each_child_node(dev, child) {
1636 ret = ad4130_parse_fw_channel(indio_dev, child);
1638 fwnode_handle_put(child);
1646 static int ad4310_parse_fw(struct iio_dev *indio_dev)
1648 struct ad4130_state *st = iio_priv(indio_dev);
1649 struct device *dev = &st->spi->dev;
1650 u32 ext_clk_freq = AD4130_MCLK_FREQ_76_8KHZ;
1656 st->mclk = devm_clk_get_optional(dev, "mclk");
1657 if (IS_ERR(st->mclk))
1658 return dev_err_probe(dev, PTR_ERR(st->mclk),
1659 "Failed to get mclk\n");
1661 st->int_pin_sel = AD4130_INT_PIN_INT;
1663 for (i = 0; i < ARRAY_SIZE(ad4130_int_pin_names); i++) {
1664 irq = fwnode_irq_get_byname(dev_fwnode(dev),
1665 ad4130_int_pin_names[i]);
1667 st->int_pin_sel = i;
1672 if (st->int_pin_sel == AD4130_INT_PIN_DOUT)
1673 return dev_err_probe(dev, -EINVAL,
1674 "Cannot use DOUT as interrupt pin\n");
1676 if (st->int_pin_sel == AD4130_INT_PIN_P2)
1677 st->pins_fn[AD4130_AIN3_P2] = AD4130_PIN_FN_SPECIAL;
1679 device_property_read_u32(dev, "adi,ext-clk-freq-hz", &ext_clk_freq);
1680 if (ext_clk_freq != AD4130_MCLK_FREQ_153_6KHZ &&
1681 ext_clk_freq != AD4130_MCLK_FREQ_76_8KHZ)
1682 return dev_err_probe(dev, -EINVAL,
1683 "Invalid external clock frequency %u\n",
1686 if (st->mclk && ext_clk_freq == AD4130_MCLK_FREQ_153_6KHZ)
1687 st->mclk_sel = AD4130_MCLK_153_6KHZ_EXT;
1689 st->mclk_sel = AD4130_MCLK_76_8KHZ_EXT;
1691 st->mclk_sel = AD4130_MCLK_76_8KHZ;
1693 if (st->int_pin_sel == AD4130_INT_PIN_CLK &&
1694 st->mclk_sel != AD4130_MCLK_76_8KHZ)
1695 return dev_err_probe(dev, -EINVAL,
1696 "Invalid clock %u for interrupt pin %u\n",
1697 st->mclk_sel, st->int_pin_sel);
1699 st->int_ref_uv = AD4130_INT_REF_2_5V;
1702 * When the AVDD supply is set to below 2.5V the internal reference of
1703 * 1.25V should be selected.
1704 * See datasheet page 37, section ADC REFERENCE.
1706 avdd_uv = regulator_get_voltage(st->regulators[0].consumer);
1707 if (avdd_uv > 0 && avdd_uv < AD4130_INT_REF_2_5V)
1708 st->int_ref_uv = AD4130_INT_REF_1_25V;
1710 st->bipolar = device_property_read_bool(dev, "adi,bipolar");
1712 ret = device_property_count_u32(dev, "adi,vbias-pins");
1714 if (ret > AD4130_MAX_ANALOG_PINS)
1715 return dev_err_probe(dev, -EINVAL,
1716 "Too many vbias pins %u\n", ret);
1718 st->num_vbias_pins = ret;
1720 ret = device_property_read_u32_array(dev, "adi,vbias-pins",
1722 st->num_vbias_pins);
1724 return dev_err_probe(dev, ret,
1725 "Failed to read vbias pins\n");
1727 ret = ad4130_validate_vbias_pins(st, st->vbias_pins,
1728 st->num_vbias_pins);
1733 ret = ad4130_parse_fw_children(indio_dev);
1740 static void ad4130_fill_scale_tbls(struct ad4130_state *st)
1742 unsigned int pow = ad4130_resolution(st) - st->bipolar;
1745 for (i = 0; i < AD4130_REF_SEL_MAX; i++) {
1749 ret = ad4130_get_ref_voltage(st, i);
1753 nv = (u64)ret * NANO;
1755 for (j = 0; j < AD4130_MAX_PGA; j++)
1756 st->scale_tbls[i][j][1] = div_u64(nv >> (pow + j), MILLI);
1760 static void ad4130_clk_disable_unprepare(void *clk)
1762 clk_disable_unprepare(clk);
1765 static int ad4130_set_mclk_sel(struct ad4130_state *st,
1766 enum ad4130_mclk_sel mclk_sel)
1768 return regmap_update_bits(st->regmap, AD4130_ADC_CONTROL_REG,
1769 AD4130_ADC_CONTROL_MCLK_SEL_MASK,
1770 FIELD_PREP(AD4130_ADC_CONTROL_MCLK_SEL_MASK,
1774 static unsigned long ad4130_int_clk_recalc_rate(struct clk_hw *hw,
1775 unsigned long parent_rate)
1777 return AD4130_MCLK_FREQ_76_8KHZ;
1780 static int ad4130_int_clk_is_enabled(struct clk_hw *hw)
1782 struct ad4130_state *st = container_of(hw, struct ad4130_state, int_clk_hw);
1784 return st->mclk_sel == AD4130_MCLK_76_8KHZ_OUT;
1787 static int ad4130_int_clk_prepare(struct clk_hw *hw)
1789 struct ad4130_state *st = container_of(hw, struct ad4130_state, int_clk_hw);
1792 ret = ad4130_set_mclk_sel(st, AD4130_MCLK_76_8KHZ_OUT);
1796 st->mclk_sel = AD4130_MCLK_76_8KHZ_OUT;
1801 static void ad4130_int_clk_unprepare(struct clk_hw *hw)
1803 struct ad4130_state *st = container_of(hw, struct ad4130_state, int_clk_hw);
1806 ret = ad4130_set_mclk_sel(st, AD4130_MCLK_76_8KHZ);
1810 st->mclk_sel = AD4130_MCLK_76_8KHZ;
1813 static const struct clk_ops ad4130_int_clk_ops = {
1814 .recalc_rate = ad4130_int_clk_recalc_rate,
1815 .is_enabled = ad4130_int_clk_is_enabled,
1816 .prepare = ad4130_int_clk_prepare,
1817 .unprepare = ad4130_int_clk_unprepare,
1820 static int ad4130_setup_int_clk(struct ad4130_state *st)
1822 struct device *dev = &st->spi->dev;
1823 struct device_node *of_node = dev_of_node(dev);
1824 struct clk_init_data init;
1825 const char *clk_name;
1828 if (st->int_pin_sel == AD4130_INT_PIN_CLK ||
1829 st->mclk_sel != AD4130_MCLK_76_8KHZ)
1835 clk_name = of_node->name;
1836 of_property_read_string(of_node, "clock-output-names", &clk_name);
1838 init.name = clk_name;
1839 init.ops = &ad4130_int_clk_ops;
1841 st->int_clk_hw.init = &init;
1842 clk = devm_clk_register(dev, &st->int_clk_hw);
1844 return PTR_ERR(clk);
1846 return of_clk_add_provider(of_node, of_clk_src_simple_get, clk);
1849 static int ad4130_setup(struct iio_dev *indio_dev)
1851 struct ad4130_state *st = iio_priv(indio_dev);
1852 struct device *dev = &st->spi->dev;
1853 unsigned int int_ref_val;
1854 unsigned long rate = AD4130_MCLK_FREQ_76_8KHZ;
1859 if (st->mclk_sel == AD4130_MCLK_153_6KHZ_EXT)
1860 rate = AD4130_MCLK_FREQ_153_6KHZ;
1862 ret = clk_set_rate(st->mclk, rate);
1866 ret = clk_prepare_enable(st->mclk);
1870 ret = devm_add_action_or_reset(dev, ad4130_clk_disable_unprepare,
1875 if (st->int_ref_uv == AD4130_INT_REF_2_5V)
1876 int_ref_val = AD4130_INT_REF_VAL_2_5V;
1878 int_ref_val = AD4130_INT_REF_VAL_1_25V;
1880 /* Switch to SPI 4-wire mode. */
1881 val = FIELD_PREP(AD4130_ADC_CONTROL_CSB_EN_MASK, 1);
1882 val |= FIELD_PREP(AD4130_ADC_CONTROL_BIPOLAR_MASK, st->bipolar);
1883 val |= FIELD_PREP(AD4130_ADC_CONTROL_INT_REF_EN_MASK, st->int_ref_en);
1884 val |= FIELD_PREP(AD4130_ADC_CONTROL_MODE_MASK, AD4130_MODE_IDLE);
1885 val |= FIELD_PREP(AD4130_ADC_CONTROL_MCLK_SEL_MASK, st->mclk_sel);
1886 val |= FIELD_PREP(AD4130_ADC_CONTROL_INT_REF_VAL_MASK, int_ref_val);
1888 ret = regmap_write(st->regmap, AD4130_ADC_CONTROL_REG, val);
1893 * Configure all GPIOs for output. If configured, the interrupt function
1894 * of P2 takes priority over the GPIO out function.
1896 val = AD4130_IO_CONTROL_GPIO_CTRL_MASK;
1897 val |= FIELD_PREP(AD4130_IO_CONTROL_INT_PIN_SEL_MASK, st->int_pin_sel);
1899 ret = regmap_write(st->regmap, AD4130_IO_CONTROL_REG, val);
1904 for (i = 0; i < st->num_vbias_pins; i++)
1905 val |= BIT(st->vbias_pins[i]);
1907 ret = regmap_write(st->regmap, AD4130_VBIAS_REG, val);
1911 ret = regmap_update_bits(st->regmap, AD4130_FIFO_CONTROL_REG,
1912 AD4130_FIFO_CONTROL_HEADER_MASK, 0);
1916 /* FIFO watermark interrupt starts out as enabled, disable it. */
1917 ret = ad4130_set_watermark_interrupt_en(st, false);
1921 /* Setup channels. */
1922 for (i = 0; i < indio_dev->num_channels; i++) {
1923 struct ad4130_chan_info *chan_info = &st->chans_info[i];
1924 struct iio_chan_spec *chan = &st->chans[i];
1927 val = FIELD_PREP(AD4130_CHANNEL_AINP_MASK, chan->channel) |
1928 FIELD_PREP(AD4130_CHANNEL_AINM_MASK, chan->channel2) |
1929 FIELD_PREP(AD4130_CHANNEL_IOUT1_MASK, chan_info->iout0) |
1930 FIELD_PREP(AD4130_CHANNEL_IOUT2_MASK, chan_info->iout1);
1932 ret = regmap_write(st->regmap, AD4130_CHANNEL_X_REG(i), val);
1940 static int ad4130_soft_reset(struct ad4130_state *st)
1944 ret = spi_write(st->spi, st->reset_buf, sizeof(st->reset_buf));
1948 fsleep(AD4130_RESET_SLEEP_US);
1953 static void ad4130_disable_regulators(void *data)
1955 struct ad4130_state *st = data;
1957 regulator_bulk_disable(ARRAY_SIZE(st->regulators), st->regulators);
1960 static int ad4130_probe(struct spi_device *spi)
1962 struct device *dev = &spi->dev;
1963 struct iio_dev *indio_dev;
1964 struct ad4130_state *st;
1967 indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
1971 st = iio_priv(indio_dev);
1973 memset(st->reset_buf, 0xff, sizeof(st->reset_buf));
1974 init_completion(&st->completion);
1975 mutex_init(&st->lock);
1979 * Xfer: [ XFR1 ] [ XFR2 ]
1980 * Master: 0x7D N ......................
1981 * Slave: ...... DATA1 DATA2 ... DATAN
1983 st->fifo_tx_buf[0] = AD4130_COMMS_READ_MASK | AD4130_FIFO_DATA_REG;
1984 st->fifo_xfer[0].tx_buf = st->fifo_tx_buf;
1985 st->fifo_xfer[0].len = sizeof(st->fifo_tx_buf);
1986 st->fifo_xfer[1].rx_buf = st->fifo_rx_buf;
1987 spi_message_init_with_transfers(&st->fifo_msg, st->fifo_xfer,
1988 ARRAY_SIZE(st->fifo_xfer));
1990 indio_dev->name = AD4130_NAME;
1991 indio_dev->modes = INDIO_DIRECT_MODE;
1992 indio_dev->info = &ad4130_info;
1994 st->regmap = devm_regmap_init(dev, NULL, st, &ad4130_regmap_config);
1995 if (IS_ERR(st->regmap))
1996 return PTR_ERR(st->regmap);
1998 st->regulators[0].supply = "avdd";
1999 st->regulators[1].supply = "iovdd";
2000 st->regulators[2].supply = "refin1";
2001 st->regulators[3].supply = "refin2";
2003 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(st->regulators),
2006 return dev_err_probe(dev, ret, "Failed to get regulators\n");
2008 ret = regulator_bulk_enable(ARRAY_SIZE(st->regulators), st->regulators);
2010 return dev_err_probe(dev, ret, "Failed to enable regulators\n");
2012 ret = devm_add_action_or_reset(dev, ad4130_disable_regulators, st);
2014 return dev_err_probe(dev, ret,
2015 "Failed to add regulators disable action\n");
2017 ret = ad4130_soft_reset(st);
2021 ret = ad4310_parse_fw(indio_dev);
2025 ret = ad4130_setup(indio_dev);
2029 ret = ad4130_setup_int_clk(st);
2033 ad4130_fill_scale_tbls(st);
2035 st->gc.owner = THIS_MODULE;
2036 st->gc.label = AD4130_NAME;
2038 st->gc.ngpio = AD4130_MAX_GPIOS;
2039 st->gc.parent = dev;
2040 st->gc.can_sleep = true;
2041 st->gc.init_valid_mask = ad4130_gpio_init_valid_mask;
2042 st->gc.get_direction = ad4130_gpio_get_direction;
2043 st->gc.set = ad4130_gpio_set;
2045 ret = devm_gpiochip_add_data(dev, &st->gc, st);
2049 ret = devm_iio_kfifo_buffer_setup_ext(dev, indio_dev,
2051 ad4130_fifo_attributes);
2055 ret = devm_request_threaded_irq(dev, spi->irq, NULL,
2056 ad4130_irq_handler, IRQF_ONESHOT,
2057 indio_dev->name, indio_dev);
2059 return dev_err_probe(dev, ret, "Failed to request irq\n");
2062 * When the chip enters FIFO mode, IRQ polarity is inverted.
2063 * When the chip exits FIFO mode, IRQ polarity returns to normal.
2064 * See datasheet pages: 65, FIFO Watermark Interrupt section,
2065 * and 71, Bit Descriptions for STATUS Register, RDYB.
2066 * Cache the normal and inverted IRQ triggers to set them when
2067 * entering and exiting FIFO mode.
2069 st->irq_trigger = irq_get_trigger_type(spi->irq);
2070 if (st->irq_trigger & IRQF_TRIGGER_RISING)
2071 st->inv_irq_trigger = IRQF_TRIGGER_FALLING;
2072 else if (st->irq_trigger & IRQF_TRIGGER_FALLING)
2073 st->inv_irq_trigger = IRQF_TRIGGER_RISING;
2075 return dev_err_probe(dev, -EINVAL, "Invalid irq flags: %u\n",
2078 return devm_iio_device_register(dev, indio_dev);
2081 static const struct of_device_id ad4130_of_match[] = {
2083 .compatible = "adi,ad4130",
2087 MODULE_DEVICE_TABLE(of, ad4130_of_match);
2089 static struct spi_driver ad4130_driver = {
2091 .name = AD4130_NAME,
2092 .of_match_table = ad4130_of_match,
2094 .probe = ad4130_probe,
2096 module_spi_driver(ad4130_driver);
2099 MODULE_DESCRIPTION("Analog Devices AD4130 SPI driver");
2100 MODULE_LICENSE("GPL");