1 // SPDX-License-Identifier: GPL-2.0+
3 * AD717x and AD411x family SPI ADC driver
6 * AD4111/AD4112/AD4113/AD4114/AD4115/AD4116
7 * AD7172-2/AD7172-4/AD7173-8/AD7175-2
8 * AD7175-8/AD7176-2/AD7177-2
10 * Copyright (C) 2015, 2024 Analog Devices, Inc.
13 #include <linux/array_size.h>
14 #include <linux/bitfield.h>
15 #include <linux/bitmap.h>
16 #include <linux/container_of.h>
17 #include <linux/clk.h>
18 #include <linux/clk-provider.h>
19 #include <linux/delay.h>
20 #include <linux/device.h>
21 #include <linux/err.h>
22 #include <linux/gpio/driver.h>
23 #include <linux/gpio/regmap.h>
24 #include <linux/idr.h>
25 #include <linux/interrupt.h>
26 #include <linux/math64.h>
27 #include <linux/module.h>
28 #include <linux/mod_devicetable.h>
29 #include <linux/property.h>
30 #include <linux/regmap.h>
31 #include <linux/regulator/consumer.h>
32 #include <linux/slab.h>
33 #include <linux/spi/spi.h>
34 #include <linux/types.h>
35 #include <linux/units.h>
37 #include <linux/iio/buffer.h>
38 #include <linux/iio/iio.h>
39 #include <linux/iio/trigger_consumer.h>
40 #include <linux/iio/triggered_buffer.h>
42 #include <linux/iio/adc/ad_sigma_delta.h>
44 #define AD7173_REG_COMMS 0x00
45 #define AD7173_REG_ADC_MODE 0x01
46 #define AD7173_REG_INTERFACE_MODE 0x02
47 #define AD7173_REG_CRC 0x03
48 #define AD7173_REG_DATA 0x04
49 #define AD7173_REG_GPIO 0x06
50 #define AD7173_REG_ID 0x07
51 #define AD7173_REG_CH(x) (0x10 + (x))
52 #define AD7173_REG_SETUP(x) (0x20 + (x))
53 #define AD7173_REG_FILTER(x) (0x28 + (x))
54 #define AD7173_REG_OFFSET(x) (0x30 + (x))
55 #define AD7173_REG_GAIN(x) (0x38 + (x))
57 #define AD7173_RESET_LENGTH BITS_TO_BYTES(64)
59 #define AD7173_CH_ENABLE BIT(15)
60 #define AD7173_CH_SETUP_SEL_MASK GENMASK(14, 12)
61 #define AD7173_CH_SETUP_AINPOS_MASK GENMASK(9, 5)
62 #define AD7173_CH_SETUP_AINNEG_MASK GENMASK(4, 0)
64 #define AD7173_NO_AINS_PER_CHANNEL 2
65 #define AD7173_CH_ADDRESS(pos, neg) \
66 (FIELD_PREP(AD7173_CH_SETUP_AINPOS_MASK, pos) | \
67 FIELD_PREP(AD7173_CH_SETUP_AINNEG_MASK, neg))
68 #define AD7173_AIN_TEMP_POS 17
69 #define AD7173_AIN_TEMP_NEG 18
70 #define AD7173_AIN_POW_MON_POS 19
71 #define AD7173_AIN_POW_MON_NEG 20
72 #define AD7173_AIN_REF_POS 21
73 #define AD7173_AIN_REF_NEG 22
75 #define AD7173_IS_REF_INPUT(x) ((x) == AD7173_AIN_REF_POS || \
76 (x) == AD7173_AIN_REF_NEG)
78 #define AD7172_2_ID 0x00d0
79 #define AD7176_ID 0x0c90
80 #define AD7175_ID 0x0cd0
81 #define AD7175_2_ID 0x0cd0
82 #define AD7172_4_ID 0x2050
83 #define AD7173_ID 0x30d0
84 #define AD4111_ID AD7173_ID
85 #define AD4112_ID AD7173_ID
86 #define AD4114_ID AD7173_ID
87 #define AD4113_ID 0x31d0
88 #define AD4116_ID 0x34d0
89 #define AD4115_ID 0x38d0
90 #define AD7175_8_ID 0x3cd0
91 #define AD7177_ID 0x4fd0
92 #define AD7173_ID_MASK GENMASK(15, 4)
94 #define AD7173_ADC_MODE_REF_EN BIT(15)
95 #define AD7173_ADC_MODE_SING_CYC BIT(13)
96 #define AD7173_ADC_MODE_MODE_MASK GENMASK(6, 4)
97 #define AD7173_ADC_MODE_CLOCKSEL_MASK GENMASK(3, 2)
98 #define AD7173_ADC_MODE_CLOCKSEL_INT 0x0
99 #define AD7173_ADC_MODE_CLOCKSEL_INT_OUTPUT 0x1
100 #define AD7173_ADC_MODE_CLOCKSEL_EXT 0x2
101 #define AD7173_ADC_MODE_CLOCKSEL_XTAL 0x3
103 #define AD7173_GPIO_PDSW BIT(14)
104 #define AD7173_GPIO_OP_EN2_3 BIT(13)
105 #define AD7173_GPIO_MUX_IO BIT(12)
106 #define AD7173_GPIO_SYNC_EN BIT(11)
107 #define AD7173_GPIO_ERR_EN BIT(10)
108 #define AD7173_GPIO_ERR_DAT BIT(9)
109 #define AD7173_GPIO_GP_DATA3 BIT(7)
110 #define AD7173_GPIO_GP_DATA2 BIT(6)
111 #define AD7173_GPIO_IP_EN1 BIT(5)
112 #define AD7173_GPIO_IP_EN0 BIT(4)
113 #define AD7173_GPIO_OP_EN1 BIT(3)
114 #define AD7173_GPIO_OP_EN0 BIT(2)
115 #define AD7173_GPIO_GP_DATA1 BIT(1)
116 #define AD7173_GPIO_GP_DATA0 BIT(0)
118 #define AD7173_GPO12_DATA(x) BIT((x) + 0)
119 #define AD7173_GPO23_DATA(x) BIT((x) + 4)
120 #define AD4111_GPO01_DATA(x) BIT((x) + 6)
121 #define AD7173_GPO_DATA(x) ((x) < 2 ? AD7173_GPO12_DATA(x) : AD7173_GPO23_DATA(x))
123 #define AD7173_INTERFACE_DATA_STAT BIT(6)
124 #define AD7173_INTERFACE_DATA_STAT_EN(x) \
125 FIELD_PREP(AD7173_INTERFACE_DATA_STAT, x)
127 #define AD7173_SETUP_BIPOLAR BIT(12)
128 #define AD7173_SETUP_AREF_BUF_MASK GENMASK(11, 10)
129 #define AD7173_SETUP_AIN_BUF_MASK GENMASK(9, 8)
131 #define AD7173_SETUP_REF_SEL_MASK GENMASK(5, 4)
132 #define AD7173_SETUP_REF_SEL_AVDD1_AVSS 0x3
133 #define AD7173_SETUP_REF_SEL_INT_REF 0x2
134 #define AD7173_SETUP_REF_SEL_EXT_REF2 0x1
135 #define AD7173_SETUP_REF_SEL_EXT_REF 0x0
136 #define AD7173_VOLTAGE_INT_REF_uV 2500000
137 #define AD7173_TEMP_SENSIIVITY_uV_per_C 477
138 #define AD7177_ODR_START_VALUE 0x07
139 #define AD4111_SHUNT_RESISTOR_OHM 50
140 #define AD4111_DIVIDER_RATIO 10
141 #define AD4111_CURRENT_CHAN_CUTOFF 16
142 #define AD4111_VINCOM_INPUT 0x10
144 /* pin < num_voltage_in is a normal voltage input */
145 /* pin >= num_voltage_in_div is a voltage input without a divider */
146 #define AD4111_IS_VINCOM_MISMATCH(pin1, pin2) ((pin1) == AD4111_VINCOM_INPUT && \
147 (pin2) < st->info->num_voltage_in && \
148 (pin2) >= st->info->num_voltage_in_div)
150 #define AD7173_FILTER_ODR0_MASK GENMASK(5, 0)
151 #define AD7173_MAX_CONFIGS 8
153 struct ad7173_device_info
{
154 const unsigned int *sinc5_data_rates
;
155 unsigned int num_sinc5_data_rates
;
156 unsigned int odr_start_value
;
158 * AD4116 has both inputs with a voltage divider and without.
159 * These inputs cannot be mixed in the channel configuration.
160 * Does not include the VINCOM input.
162 unsigned int num_voltage_in_div
;
163 unsigned int num_channels
;
164 unsigned int num_configs
;
165 unsigned int num_voltage_in
;
169 bool has_current_inputs
;
170 bool has_vincom_input
;
172 /* ((AVDD1 − AVSS)/5) */
173 bool has_pow_supply_monitoring
;
174 bool data_reg_only_16bit
;
178 bool higher_gpio_bits
;
182 struct ad7173_channel_config
{
186 /* Following fields are used to compare equality. */
187 struct_group(config_props
,
195 struct ad7173_channel
{
196 unsigned int chan_reg
;
198 struct ad7173_channel_config cfg
;
201 struct ad7173_state
{
202 struct ad_sigma_delta sd
;
203 const struct ad7173_device_info
*info
;
204 struct ad7173_channel
*channels
;
205 struct regulator_bulk_data regulators
[3];
206 unsigned int adc_mode
;
207 unsigned int interface_mode
;
208 unsigned int num_channels
;
209 struct ida cfg_slots_status
;
210 unsigned long long config_usage_counter
;
211 unsigned long long *config_cnts
;
213 struct clk_hw int_clk_hw
;
214 #if IS_ENABLED(CONFIG_GPIOLIB)
215 struct regmap
*reg_gpiocon_regmap
;
216 struct gpio_regmap
*gpio_regmap
;
220 static unsigned int ad4115_sinc5_data_rates
[] = {
221 24845000, 24845000, 20725000, 20725000, /* 0-3 */
222 15564000, 13841000, 10390000, 10390000, /* 4-7 */
223 4994000, 2499000, 1000000, 500000, /* 8-11 */
224 395500, 200000, 100000, 59890, /* 12-15 */
225 49920, 20000, 16660, 10000, /* 16-19 */
226 5000, 2500, 2500, /* 20-22 */
229 static unsigned int ad4116_sinc5_data_rates
[] = {
230 12422360, 12422360, 12422360, 12422360, /* 0-3 */
231 10362690, 10362690, 7782100, 6290530, /* 4-7 */
232 5194800, 2496900, 1007600, 499900, /* 8-11 */
233 390600, 200300, 100000, 59750, /* 12-15 */
234 49840, 20000, 16650, 10000, /* 16-19 */
235 5000, 2500, 1250, /* 20-22 */
238 static const unsigned int ad7173_sinc5_data_rates
[] = {
239 6211000, 6211000, 6211000, 6211000, 6211000, 6211000, 5181000, 4444000, /* 0-7 */
240 3115000, 2597000, 1007000, 503800, 381000, 200300, 100500, 59520, /* 8-15 */
241 49680, 20010, 16333, 10000, 5000, 2500, 1250, /* 16-22 */
244 static const unsigned int ad7175_sinc5_data_rates
[] = {
245 50000000, 41667000, 31250000, 27778000, /* 0-3 */
246 20833000, 17857000, 12500000, 10000000, /* 4-7 */
247 5000000, 2500000, 1000000, 500000, /* 8-11 */
248 397500, 200000, 100000, 59920, /* 12-15 */
249 49960, 20000, 16666, 10000, /* 16-19 */
253 static unsigned int ad4111_current_channel_config
[] = {
254 /* Ain sel: pos neg */
255 0x1E8, /* 15:IIN0+ 8:IIN0− */
256 0x1C9, /* 14:IIN1+ 9:IIN1− */
257 0x1AA, /* 13:IIN2+ 10:IIN2− */
258 0x18B, /* 12:IIN3+ 11:IIN3− */
261 static const struct ad7173_device_info ad4111_device_info
= {
264 .num_voltage_in_div
= 8,
269 .higher_gpio_bits
= true,
271 .has_vincom_input
= true,
272 .has_input_buf
= true,
273 .has_current_inputs
= true,
275 .clock
= 2 * HZ_PER_MHZ
,
276 .sinc5_data_rates
= ad7173_sinc5_data_rates
,
277 .num_sinc5_data_rates
= ARRAY_SIZE(ad7173_sinc5_data_rates
),
280 static const struct ad7173_device_info ad4112_device_info
= {
283 .num_voltage_in_div
= 8,
288 .higher_gpio_bits
= true,
289 .has_vincom_input
= true,
291 .has_input_buf
= true,
292 .has_current_inputs
= true,
294 .clock
= 2 * HZ_PER_MHZ
,
295 .sinc5_data_rates
= ad7173_sinc5_data_rates
,
296 .num_sinc5_data_rates
= ARRAY_SIZE(ad7173_sinc5_data_rates
),
299 static const struct ad7173_device_info ad4113_device_info
= {
302 .num_voltage_in_div
= 8,
307 .data_reg_only_16bit
= true,
308 .higher_gpio_bits
= true,
309 .has_vincom_input
= true,
310 .has_input_buf
= true,
312 .clock
= 2 * HZ_PER_MHZ
,
313 .sinc5_data_rates
= ad7173_sinc5_data_rates
,
314 .num_sinc5_data_rates
= ARRAY_SIZE(ad7173_sinc5_data_rates
),
317 static const struct ad7173_device_info ad4114_device_info
= {
320 .num_voltage_in_div
= 16,
323 .num_voltage_in
= 16,
325 .has_vincom_input
= true,
327 .has_input_buf
= true,
329 .clock
= 2 * HZ_PER_MHZ
,
330 .sinc5_data_rates
= ad7173_sinc5_data_rates
,
331 .num_sinc5_data_rates
= ARRAY_SIZE(ad7173_sinc5_data_rates
),
334 static const struct ad7173_device_info ad4115_device_info
= {
337 .num_voltage_in_div
= 16,
340 .num_voltage_in
= 16,
342 .has_vincom_input
= true,
344 .has_input_buf
= true,
346 .clock
= 8 * HZ_PER_MHZ
,
347 .sinc5_data_rates
= ad4115_sinc5_data_rates
,
348 .num_sinc5_data_rates
= ARRAY_SIZE(ad4115_sinc5_data_rates
),
351 static const struct ad7173_device_info ad4116_device_info
= {
354 .num_voltage_in_div
= 11,
357 .num_voltage_in
= 16,
359 .has_vincom_input
= true,
361 .has_input_buf
= true,
363 .clock
= 4 * HZ_PER_MHZ
,
364 .sinc5_data_rates
= ad4116_sinc5_data_rates
,
365 .num_sinc5_data_rates
= ARRAY_SIZE(ad4116_sinc5_data_rates
),
368 static const struct ad7173_device_info ad7172_2_device_info
= {
376 .has_input_buf
= true,
378 .has_pow_supply_monitoring
= true,
379 .clock
= 2 * HZ_PER_MHZ
,
380 .sinc5_data_rates
= ad7173_sinc5_data_rates
,
381 .num_sinc5_data_rates
= ARRAY_SIZE(ad7173_sinc5_data_rates
),
384 static const struct ad7173_device_info ad7172_4_device_info
= {
391 .has_input_buf
= true,
393 .has_pow_supply_monitoring
= true,
394 .clock
= 2 * HZ_PER_MHZ
,
395 .sinc5_data_rates
= ad7173_sinc5_data_rates
,
396 .num_sinc5_data_rates
= ARRAY_SIZE(ad7173_sinc5_data_rates
),
399 static const struct ad7173_device_info ad7173_8_device_info
= {
402 .num_voltage_in
= 17,
407 .has_input_buf
= true,
410 .clock
= 2 * HZ_PER_MHZ
,
411 .sinc5_data_rates
= ad7173_sinc5_data_rates
,
412 .num_sinc5_data_rates
= ARRAY_SIZE(ad7173_sinc5_data_rates
),
415 static const struct ad7173_device_info ad7175_2_device_info
= {
423 .has_input_buf
= true,
425 .has_pow_supply_monitoring
= true,
426 .clock
= 16 * HZ_PER_MHZ
,
427 .sinc5_data_rates
= ad7175_sinc5_data_rates
,
428 .num_sinc5_data_rates
= ARRAY_SIZE(ad7175_sinc5_data_rates
),
431 static const struct ad7173_device_info ad7175_8_device_info
= {
434 .num_voltage_in
= 17,
439 .has_input_buf
= true,
442 .has_pow_supply_monitoring
= true,
443 .clock
= 16 * HZ_PER_MHZ
,
444 .sinc5_data_rates
= ad7175_sinc5_data_rates
,
445 .num_sinc5_data_rates
= ARRAY_SIZE(ad7175_sinc5_data_rates
),
448 static const struct ad7173_device_info ad7176_2_device_info
= {
456 .clock
= 16 * HZ_PER_MHZ
,
457 .sinc5_data_rates
= ad7175_sinc5_data_rates
,
458 .num_sinc5_data_rates
= ARRAY_SIZE(ad7175_sinc5_data_rates
),
461 static const struct ad7173_device_info ad7177_2_device_info
= {
469 .has_input_buf
= true,
471 .has_pow_supply_monitoring
= true,
472 .clock
= 16 * HZ_PER_MHZ
,
473 .odr_start_value
= AD7177_ODR_START_VALUE
,
474 .sinc5_data_rates
= ad7175_sinc5_data_rates
,
475 .num_sinc5_data_rates
= ARRAY_SIZE(ad7175_sinc5_data_rates
),
478 static const char *const ad7173_ref_sel_str
[] = {
479 [AD7173_SETUP_REF_SEL_EXT_REF
] = "vref",
480 [AD7173_SETUP_REF_SEL_EXT_REF2
] = "vref2",
481 [AD7173_SETUP_REF_SEL_INT_REF
] = "refout-avss",
482 [AD7173_SETUP_REF_SEL_AVDD1_AVSS
] = "avdd",
485 static const char *const ad7173_clk_sel
[] = {
489 #if IS_ENABLED(CONFIG_GPIOLIB)
491 static const struct regmap_range ad7173_range_gpio
[] = {
492 regmap_reg_range(AD7173_REG_GPIO
, AD7173_REG_GPIO
),
495 static const struct regmap_access_table ad7173_access_table
= {
496 .yes_ranges
= ad7173_range_gpio
,
497 .n_yes_ranges
= ARRAY_SIZE(ad7173_range_gpio
),
500 static const struct regmap_config ad7173_regmap_config
= {
503 .rd_table
= &ad7173_access_table
,
504 .wr_table
= &ad7173_access_table
,
505 .read_flag_mask
= BIT(6),
508 static int ad7173_mask_xlate(struct gpio_regmap
*gpio
, unsigned int base
,
509 unsigned int offset
, unsigned int *reg
,
512 *mask
= AD7173_GPO_DATA(offset
);
517 static int ad4111_mask_xlate(struct gpio_regmap
*gpio
, unsigned int base
,
518 unsigned int offset
, unsigned int *reg
,
521 *mask
= AD4111_GPO01_DATA(offset
);
526 static void ad7173_gpio_disable(void *data
)
528 struct ad7173_state
*st
= data
;
531 mask
= AD7173_GPIO_OP_EN0
| AD7173_GPIO_OP_EN1
| AD7173_GPIO_OP_EN2_3
;
532 regmap_update_bits(st
->reg_gpiocon_regmap
, AD7173_REG_GPIO
, mask
, ~mask
);
535 static int ad7173_gpio_init(struct ad7173_state
*st
)
537 struct gpio_regmap_config gpio_regmap
= {};
538 struct device
*dev
= &st
->sd
.spi
->dev
;
542 st
->reg_gpiocon_regmap
= devm_regmap_init_spi(st
->sd
.spi
, &ad7173_regmap_config
);
543 ret
= PTR_ERR_OR_ZERO(st
->reg_gpiocon_regmap
);
545 return dev_err_probe(dev
, ret
, "Unable to init regmap\n");
547 mask
= AD7173_GPIO_OP_EN0
| AD7173_GPIO_OP_EN1
| AD7173_GPIO_OP_EN2_3
;
548 regmap_update_bits(st
->reg_gpiocon_regmap
, AD7173_REG_GPIO
, mask
, mask
);
550 ret
= devm_add_action_or_reset(dev
, ad7173_gpio_disable
, st
);
554 gpio_regmap
.parent
= dev
;
555 gpio_regmap
.regmap
= st
->reg_gpiocon_regmap
;
556 gpio_regmap
.ngpio
= st
->info
->num_gpios
;
557 gpio_regmap
.reg_set_base
= AD7173_REG_GPIO
;
558 if (st
->info
->higher_gpio_bits
)
559 gpio_regmap
.reg_mask_xlate
= ad4111_mask_xlate
;
561 gpio_regmap
.reg_mask_xlate
= ad7173_mask_xlate
;
563 st
->gpio_regmap
= devm_gpio_regmap_register(dev
, &gpio_regmap
);
564 ret
= PTR_ERR_OR_ZERO(st
->gpio_regmap
);
566 return dev_err_probe(dev
, ret
, "Unable to init gpio-regmap\n");
571 static int ad7173_gpio_init(struct ad7173_state
*st
)
575 #endif /* CONFIG_GPIOLIB */
577 static struct ad7173_state
*ad_sigma_delta_to_ad7173(struct ad_sigma_delta
*sd
)
579 return container_of(sd
, struct ad7173_state
, sd
);
582 static struct ad7173_state
*clk_hw_to_ad7173(struct clk_hw
*hw
)
584 return container_of(hw
, struct ad7173_state
, int_clk_hw
);
587 static void ad7173_ida_destroy(void *data
)
589 struct ad7173_state
*st
= data
;
591 ida_destroy(&st
->cfg_slots_status
);
594 static void ad7173_reset_usage_cnts(struct ad7173_state
*st
)
596 memset64(st
->config_cnts
, 0, st
->info
->num_configs
);
597 st
->config_usage_counter
= 0;
600 static struct ad7173_channel_config
*
601 ad7173_find_live_config(struct ad7173_state
*st
, struct ad7173_channel_config
*cfg
)
603 struct ad7173_channel_config
*cfg_aux
;
607 cmp_size
= sizeof_field(struct ad7173_channel_config
, config_props
);
608 for (i
= 0; i
< st
->num_channels
; i
++) {
609 cfg_aux
= &st
->channels
[i
].cfg
;
612 !memcmp(&cfg
->config_props
, &cfg_aux
->config_props
, cmp_size
))
618 /* Could be replaced with a generic LRU implementation */
619 static int ad7173_free_config_slot_lru(struct ad7173_state
*st
)
621 int i
, lru_position
= 0;
623 for (i
= 1; i
< st
->info
->num_configs
; i
++)
624 if (st
->config_cnts
[i
] < st
->config_cnts
[lru_position
])
627 for (i
= 0; i
< st
->num_channels
; i
++)
628 if (st
->channels
[i
].cfg
.cfg_slot
== lru_position
)
629 st
->channels
[i
].cfg
.live
= false;
631 ida_free(&st
->cfg_slots_status
, lru_position
);
632 return ida_alloc(&st
->cfg_slots_status
, GFP_KERNEL
);
635 /* Could be replaced with a generic LRU implementation */
636 static int ad7173_load_config(struct ad7173_state
*st
,
637 struct ad7173_channel_config
*cfg
)
640 int free_cfg_slot
, ret
;
642 free_cfg_slot
= ida_alloc_range(&st
->cfg_slots_status
, 0,
643 st
->info
->num_configs
- 1, GFP_KERNEL
);
644 if (free_cfg_slot
< 0)
645 free_cfg_slot
= ad7173_free_config_slot_lru(st
);
647 cfg
->cfg_slot
= free_cfg_slot
;
648 config
= FIELD_PREP(AD7173_SETUP_REF_SEL_MASK
, cfg
->ref_sel
);
651 config
|= AD7173_SETUP_BIPOLAR
;
654 config
|= AD7173_SETUP_AIN_BUF_MASK
;
656 ret
= ad_sd_write_reg(&st
->sd
, AD7173_REG_SETUP(free_cfg_slot
), 2, config
);
660 return ad_sd_write_reg(&st
->sd
, AD7173_REG_FILTER(free_cfg_slot
), 2,
661 AD7173_FILTER_ODR0_MASK
& cfg
->odr
);
664 static int ad7173_config_channel(struct ad7173_state
*st
, int addr
)
666 struct ad7173_channel_config
*cfg
= &st
->channels
[addr
].cfg
;
667 struct ad7173_channel_config
*live_cfg
;
671 live_cfg
= ad7173_find_live_config(st
, cfg
);
673 cfg
->cfg_slot
= live_cfg
->cfg_slot
;
675 ret
= ad7173_load_config(st
, cfg
);
682 if (st
->config_usage_counter
== U64_MAX
)
683 ad7173_reset_usage_cnts(st
);
685 st
->config_usage_counter
++;
686 st
->config_cnts
[cfg
->cfg_slot
] = st
->config_usage_counter
;
691 static int ad7173_set_channel(struct ad_sigma_delta
*sd
, unsigned int channel
)
693 struct ad7173_state
*st
= ad_sigma_delta_to_ad7173(sd
);
697 ret
= ad7173_config_channel(st
, channel
);
701 val
= AD7173_CH_ENABLE
|
702 FIELD_PREP(AD7173_CH_SETUP_SEL_MASK
, st
->channels
[channel
].cfg
.cfg_slot
) |
703 st
->channels
[channel
].ain
;
705 return ad_sd_write_reg(&st
->sd
, AD7173_REG_CH(channel
), 2, val
);
708 static int ad7173_set_mode(struct ad_sigma_delta
*sd
,
709 enum ad_sigma_delta_mode mode
)
711 struct ad7173_state
*st
= ad_sigma_delta_to_ad7173(sd
);
713 st
->adc_mode
&= ~AD7173_ADC_MODE_MODE_MASK
;
714 st
->adc_mode
|= FIELD_PREP(AD7173_ADC_MODE_MODE_MASK
, mode
);
716 return ad_sd_write_reg(&st
->sd
, AD7173_REG_ADC_MODE
, 2, st
->adc_mode
);
719 static int ad7173_append_status(struct ad_sigma_delta
*sd
, bool append
)
721 struct ad7173_state
*st
= ad_sigma_delta_to_ad7173(sd
);
722 unsigned int interface_mode
= st
->interface_mode
;
725 interface_mode
&= ~AD7173_INTERFACE_DATA_STAT
;
726 interface_mode
|= AD7173_INTERFACE_DATA_STAT_EN(append
);
727 ret
= ad_sd_write_reg(&st
->sd
, AD7173_REG_INTERFACE_MODE
, 2, interface_mode
);
731 st
->interface_mode
= interface_mode
;
736 static int ad7173_disable_all(struct ad_sigma_delta
*sd
)
738 struct ad7173_state
*st
= ad_sigma_delta_to_ad7173(sd
);
742 for (i
= 0; i
< st
->num_channels
; i
++) {
743 ret
= ad_sd_write_reg(sd
, AD7173_REG_CH(i
), 2, 0);
751 static int ad7173_disable_one(struct ad_sigma_delta
*sd
, unsigned int chan
)
753 return ad_sd_write_reg(sd
, AD7173_REG_CH(chan
), 2, 0);
756 static struct ad_sigma_delta_info ad7173_sigma_delta_info
= {
757 .set_channel
= ad7173_set_channel
,
758 .append_status
= ad7173_append_status
,
759 .disable_all
= ad7173_disable_all
,
760 .disable_one
= ad7173_disable_one
,
761 .set_mode
= ad7173_set_mode
,
762 .has_registers
= true,
765 .status_ch_mask
= GENMASK(3, 0),
766 .data_reg
= AD7173_REG_DATA
,
769 static int ad7173_setup(struct iio_dev
*indio_dev
)
771 struct ad7173_state
*st
= iio_priv(indio_dev
);
772 struct device
*dev
= &st
->sd
.spi
->dev
;
773 u8 buf
[AD7173_RESET_LENGTH
];
777 /* reset the serial interface */
778 memset(buf
, 0xff, AD7173_RESET_LENGTH
);
779 ret
= spi_write_then_read(st
->sd
.spi
, buf
, sizeof(buf
), NULL
, 0);
783 /* datasheet recommends a delay of at least 500us after reset */
786 ret
= ad_sd_read_reg(&st
->sd
, AD7173_REG_ID
, 2, &id
);
790 id
&= AD7173_ID_MASK
;
791 if (id
!= st
->info
->id
)
792 dev_warn(dev
, "Unexpected device id: 0x%04X, expected: 0x%04X\n",
795 st
->adc_mode
|= AD7173_ADC_MODE_SING_CYC
;
796 st
->interface_mode
= 0x0;
798 st
->config_usage_counter
= 0;
799 st
->config_cnts
= devm_kcalloc(dev
, st
->info
->num_configs
,
800 sizeof(*st
->config_cnts
), GFP_KERNEL
);
801 if (!st
->config_cnts
)
804 /* All channels are enabled by default after a reset */
805 return ad7173_disable_all(&st
->sd
);
808 static unsigned int ad7173_get_ref_voltage_milli(struct ad7173_state
*st
,
813 switch (reference_select
) {
814 case AD7173_SETUP_REF_SEL_EXT_REF
:
815 vref
= regulator_get_voltage(st
->regulators
[0].consumer
);
818 case AD7173_SETUP_REF_SEL_EXT_REF2
:
819 vref
= regulator_get_voltage(st
->regulators
[1].consumer
);
822 case AD7173_SETUP_REF_SEL_INT_REF
:
823 vref
= AD7173_VOLTAGE_INT_REF_uV
;
826 case AD7173_SETUP_REF_SEL_AVDD1_AVSS
:
827 vref
= regulator_get_voltage(st
->regulators
[2].consumer
);
837 return vref
/ (MICRO
/ MILLI
);
840 static int ad7173_read_raw(struct iio_dev
*indio_dev
,
841 struct iio_chan_spec
const *chan
,
842 int *val
, int *val2
, long info
)
844 struct ad7173_state
*st
= iio_priv(indio_dev
);
845 struct ad7173_channel
*ch
= &st
->channels
[chan
->address
];
851 case IIO_CHAN_INFO_RAW
:
852 ret
= ad_sigma_delta_single_conversion(indio_dev
, chan
, val
);
857 case IIO_CHAN_INFO_SCALE
:
859 switch (chan
->type
) {
861 temp
= AD7173_VOLTAGE_INT_REF_uV
* MILLI
;
862 temp
/= AD7173_TEMP_SENSIIVITY_uV_per_C
;
864 *val2
= chan
->scan_type
.realbits
;
865 return IIO_VAL_FRACTIONAL_LOG2
;
867 *val
= ad7173_get_ref_voltage_milli(st
, ch
->cfg
.ref_sel
);
868 *val2
= chan
->scan_type
.realbits
- !!(ch
->cfg
.bipolar
);
870 if (chan
->channel
< st
->info
->num_voltage_in_div
)
871 *val
*= AD4111_DIVIDER_RATIO
;
872 return IIO_VAL_FRACTIONAL_LOG2
;
874 *val
= ad7173_get_ref_voltage_milli(st
, ch
->cfg
.ref_sel
);
875 *val
/= AD4111_SHUNT_RESISTOR_OHM
;
876 *val2
= chan
->scan_type
.realbits
- ch
->cfg
.bipolar
;
877 return IIO_VAL_FRACTIONAL_LOG2
;
881 case IIO_CHAN_INFO_OFFSET
:
883 switch (chan
->type
) {
885 /* 0 Kelvin -> raw sample */
886 temp
= -ABSOLUTE_ZERO_MILLICELSIUS
;
887 temp
*= AD7173_TEMP_SENSIIVITY_uV_per_C
;
888 temp
<<= chan
->scan_type
.realbits
;
889 temp
= DIV_U64_ROUND_CLOSEST(temp
,
890 AD7173_VOLTAGE_INT_REF_uV
*
896 *val
= -BIT(chan
->scan_type
.realbits
- 1);
901 case IIO_CHAN_INFO_SAMP_FREQ
:
902 reg
= st
->channels
[chan
->address
].cfg
.odr
;
904 *val
= st
->info
->sinc5_data_rates
[reg
] / MILLI
;
905 *val2
= (st
->info
->sinc5_data_rates
[reg
] % MILLI
) * (MICRO
/ MILLI
);
907 return IIO_VAL_INT_PLUS_MICRO
;
913 static int ad7173_write_raw(struct iio_dev
*indio_dev
,
914 struct iio_chan_spec
const *chan
,
915 int val
, int val2
, long info
)
917 struct ad7173_state
*st
= iio_priv(indio_dev
);
918 struct ad7173_channel_config
*cfg
;
919 unsigned int freq
, i
;
922 ret
= iio_device_claim_direct_mode(indio_dev
);
928 * This attribute sets the sampling frequency for each channel individually.
929 * There are no issues for raw or buffered reads of an individual channel.
931 * When multiple channels are enabled in buffered mode, the effective
932 * sampling rate of a channel is lowered in correlation to the number
933 * of channels enabled and the sampling rate of the other channels.
935 * Example: 3 channels enabled with rates CH1:6211sps CH2,CH3:10sps
936 * While the reading of CH1 takes only 0.16ms, the reading of CH2 and CH3
937 * will take 100ms each.
939 * This will cause the reading of CH1 to be actually done once every
940 * 200.16ms, an effective rate of 4.99sps.
942 case IIO_CHAN_INFO_SAMP_FREQ
:
943 freq
= val
* MILLI
+ val2
/ MILLI
;
944 for (i
= st
->info
->odr_start_value
; i
< st
->info
->num_sinc5_data_rates
- 1; i
++)
945 if (freq
>= st
->info
->sinc5_data_rates
[i
])
948 cfg
= &st
->channels
[chan
->address
].cfg
;
958 iio_device_release_direct_mode(indio_dev
);
962 static int ad7173_update_scan_mode(struct iio_dev
*indio_dev
,
963 const unsigned long *scan_mask
)
965 struct ad7173_state
*st
= iio_priv(indio_dev
);
968 for (i
= 0; i
< indio_dev
->num_channels
; i
++) {
969 if (test_bit(i
, scan_mask
))
970 ret
= ad7173_set_channel(&st
->sd
, i
);
972 ret
= ad_sd_write_reg(&st
->sd
, AD7173_REG_CH(i
), 2, 0);
980 static int ad7173_debug_reg_access(struct iio_dev
*indio_dev
, unsigned int reg
,
981 unsigned int writeval
, unsigned int *readval
)
983 struct ad7173_state
*st
= iio_priv(indio_dev
);
986 if (reg
== AD7173_REG_COMMS
)
988 else if (reg
== AD7173_REG_CRC
|| reg
== AD7173_REG_DATA
||
989 reg
>= AD7173_REG_OFFSET(0))
995 return ad_sd_read_reg(&st
->sd
, reg
, reg_size
, readval
);
997 return ad_sd_write_reg(&st
->sd
, reg
, reg_size
, writeval
);
1000 static const struct iio_info ad7173_info
= {
1001 .read_raw
= &ad7173_read_raw
,
1002 .write_raw
= &ad7173_write_raw
,
1003 .debugfs_reg_access
= &ad7173_debug_reg_access
,
1004 .validate_trigger
= ad_sd_validate_trigger
,
1005 .update_scan_mode
= ad7173_update_scan_mode
,
1008 static const struct iio_scan_type ad4113_scan_type
= {
1012 .endianness
= IIO_BE
,
1015 static const struct iio_chan_spec ad7173_channel_template
= {
1016 .type
= IIO_VOLTAGE
,
1018 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1019 BIT(IIO_CHAN_INFO_SCALE
) | BIT(IIO_CHAN_INFO_SAMP_FREQ
),
1024 .endianness
= IIO_BE
,
1028 static const struct iio_chan_spec ad7173_temp_iio_channel_template
= {
1030 .channel
= AD7173_AIN_TEMP_POS
,
1031 .channel2
= AD7173_AIN_TEMP_NEG
,
1032 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1033 BIT(IIO_CHAN_INFO_SCALE
) | BIT(IIO_CHAN_INFO_OFFSET
) |
1034 BIT(IIO_CHAN_INFO_SAMP_FREQ
),
1039 .endianness
= IIO_BE
,
1043 static void ad7173_disable_regulators(void *data
)
1045 struct ad7173_state
*st
= data
;
1047 regulator_bulk_disable(ARRAY_SIZE(st
->regulators
), st
->regulators
);
1050 static void ad7173_clk_disable_unprepare(void *clk
)
1052 clk_disable_unprepare(clk
);
1055 static unsigned long ad7173_sel_clk(struct ad7173_state
*st
,
1056 unsigned int clk_sel
)
1060 st
->adc_mode
&= ~AD7173_ADC_MODE_CLOCKSEL_MASK
;
1061 st
->adc_mode
|= FIELD_PREP(AD7173_ADC_MODE_CLOCKSEL_MASK
, clk_sel
);
1062 ret
= ad_sd_write_reg(&st
->sd
, AD7173_REG_ADC_MODE
, 0x2, st
->adc_mode
);
1067 static unsigned long ad7173_clk_recalc_rate(struct clk_hw
*hw
,
1068 unsigned long parent_rate
)
1070 struct ad7173_state
*st
= clk_hw_to_ad7173(hw
);
1072 return st
->info
->clock
/ HZ_PER_KHZ
;
1075 static int ad7173_clk_output_is_enabled(struct clk_hw
*hw
)
1077 struct ad7173_state
*st
= clk_hw_to_ad7173(hw
);
1080 clk_sel
= FIELD_GET(AD7173_ADC_MODE_CLOCKSEL_MASK
, st
->adc_mode
);
1081 return clk_sel
== AD7173_ADC_MODE_CLOCKSEL_INT_OUTPUT
;
1084 static int ad7173_clk_output_prepare(struct clk_hw
*hw
)
1086 struct ad7173_state
*st
= clk_hw_to_ad7173(hw
);
1088 return ad7173_sel_clk(st
, AD7173_ADC_MODE_CLOCKSEL_INT_OUTPUT
);
1091 static void ad7173_clk_output_unprepare(struct clk_hw
*hw
)
1093 struct ad7173_state
*st
= clk_hw_to_ad7173(hw
);
1095 ad7173_sel_clk(st
, AD7173_ADC_MODE_CLOCKSEL_INT
);
1098 static const struct clk_ops ad7173_int_clk_ops
= {
1099 .recalc_rate
= ad7173_clk_recalc_rate
,
1100 .is_enabled
= ad7173_clk_output_is_enabled
,
1101 .prepare
= ad7173_clk_output_prepare
,
1102 .unprepare
= ad7173_clk_output_unprepare
,
1105 static int ad7173_register_clk_provider(struct iio_dev
*indio_dev
)
1107 struct ad7173_state
*st
= iio_priv(indio_dev
);
1108 struct device
*dev
= indio_dev
->dev
.parent
;
1109 struct fwnode_handle
*fwnode
= dev_fwnode(dev
);
1110 struct clk_init_data init
= {};
1113 if (!IS_ENABLED(CONFIG_COMMON_CLK
))
1116 init
.name
= fwnode_get_name(fwnode
);
1117 init
.ops
= &ad7173_int_clk_ops
;
1119 st
->int_clk_hw
.init
= &init
;
1120 ret
= devm_clk_hw_register(dev
, &st
->int_clk_hw
);
1124 return devm_of_clk_add_hw_provider(dev
, of_clk_hw_simple_get
,
1128 static int ad4111_validate_current_ain(struct ad7173_state
*st
,
1129 const unsigned int ain
[AD7173_NO_AINS_PER_CHANNEL
])
1131 struct device
*dev
= &st
->sd
.spi
->dev
;
1133 if (!st
->info
->has_current_inputs
)
1134 return dev_err_probe(dev
, -EINVAL
,
1135 "Model %s does not support current channels\n",
1138 if (ain
[0] >= ARRAY_SIZE(ad4111_current_channel_config
))
1139 return dev_err_probe(dev
, -EINVAL
,
1140 "For current channels single-channel must be <[0-3]>\n");
1145 static int ad7173_validate_voltage_ain_inputs(struct ad7173_state
*st
,
1146 unsigned int ain0
, unsigned int ain1
)
1148 struct device
*dev
= &st
->sd
.spi
->dev
;
1149 bool special_input0
, special_input1
;
1151 /* (AVDD1-AVSS)/5 power supply monitoring */
1152 if (ain0
== AD7173_AIN_POW_MON_POS
&& ain1
== AD7173_AIN_POW_MON_NEG
&&
1153 st
->info
->has_pow_supply_monitoring
)
1156 special_input0
= AD7173_IS_REF_INPUT(ain0
) ||
1157 (ain0
== AD4111_VINCOM_INPUT
&& st
->info
->has_vincom_input
);
1158 special_input1
= AD7173_IS_REF_INPUT(ain1
) ||
1159 (ain1
== AD4111_VINCOM_INPUT
&& st
->info
->has_vincom_input
);
1161 if ((ain0
>= st
->info
->num_voltage_in
&& !special_input0
) ||
1162 (ain1
>= st
->info
->num_voltage_in
&& !special_input1
)) {
1163 if (ain0
== AD4111_VINCOM_INPUT
|| ain1
== AD4111_VINCOM_INPUT
)
1164 return dev_err_probe(dev
, -EINVAL
,
1165 "VINCOM not supported for %s\n", st
->info
->name
);
1167 return dev_err_probe(dev
, -EINVAL
,
1168 "Input pin number out of range for pair (%d %d).\n",
1172 if (AD4111_IS_VINCOM_MISMATCH(ain0
, ain1
) ||
1173 AD4111_IS_VINCOM_MISMATCH(ain1
, ain0
))
1174 return dev_err_probe(dev
, -EINVAL
,
1175 "VINCOM must be paired with inputs having divider.\n");
1177 if (!special_input0
&& !special_input1
&&
1178 ((ain0
>= st
->info
->num_voltage_in_div
) !=
1179 (ain1
>= st
->info
->num_voltage_in_div
)))
1180 return dev_err_probe(dev
, -EINVAL
,
1181 "Both inputs must either have a voltage divider or not have: (%d %d).\n",
1187 static int ad7173_validate_reference(struct ad7173_state
*st
, int ref_sel
)
1189 struct device
*dev
= &st
->sd
.spi
->dev
;
1192 if (ref_sel
== AD7173_SETUP_REF_SEL_INT_REF
&& !st
->info
->has_int_ref
)
1193 return dev_err_probe(dev
, -EINVAL
,
1194 "Internal reference is not available on current model.\n");
1196 if (ref_sel
== AD7173_SETUP_REF_SEL_EXT_REF2
&& !st
->info
->has_ref2
)
1197 return dev_err_probe(dev
, -EINVAL
,
1198 "External reference 2 is not available on current model.\n");
1200 ret
= ad7173_get_ref_voltage_milli(st
, ref_sel
);
1202 return dev_err_probe(dev
, ret
, "Cannot use reference %u\n",
1208 static int ad7173_fw_parse_channel_config(struct iio_dev
*indio_dev
)
1210 struct ad7173_channel
*chans_st_arr
, *chan_st_priv
;
1211 struct ad7173_state
*st
= iio_priv(indio_dev
);
1212 struct device
*dev
= indio_dev
->dev
.parent
;
1213 struct iio_chan_spec
*chan_arr
, *chan
;
1214 unsigned int ain
[AD7173_NO_AINS_PER_CHANNEL
], chan_index
= 0;
1215 int ref_sel
, ret
, num_channels
;
1217 num_channels
= device_get_child_node_count(dev
);
1219 if (st
->info
->has_temp
)
1222 if (num_channels
== 0)
1223 return dev_err_probe(dev
, -ENODATA
, "No channels specified\n");
1225 if (num_channels
> st
->info
->num_channels
)
1226 return dev_err_probe(dev
, -EINVAL
,
1227 "Too many channels specified. Maximum is %d, not including temperature channel if supported.\n",
1228 st
->info
->num_channels
);
1230 indio_dev
->num_channels
= num_channels
;
1231 st
->num_channels
= num_channels
;
1233 chan_arr
= devm_kcalloc(dev
, sizeof(*indio_dev
->channels
),
1234 st
->num_channels
, GFP_KERNEL
);
1238 chans_st_arr
= devm_kcalloc(dev
, st
->num_channels
, sizeof(*st
->channels
),
1243 indio_dev
->channels
= chan_arr
;
1244 st
->channels
= chans_st_arr
;
1246 if (st
->info
->has_temp
) {
1247 chan_arr
[chan_index
] = ad7173_temp_iio_channel_template
;
1248 chan_st_priv
= &chans_st_arr
[chan_index
];
1250 AD7173_CH_ADDRESS(chan_arr
[chan_index
].channel
,
1251 chan_arr
[chan_index
].channel2
);
1252 chan_st_priv
->cfg
.bipolar
= false;
1253 chan_st_priv
->cfg
.input_buf
= st
->info
->has_input_buf
;
1254 chan_st_priv
->cfg
.ref_sel
= AD7173_SETUP_REF_SEL_INT_REF
;
1255 st
->adc_mode
|= AD7173_ADC_MODE_REF_EN
;
1256 if (st
->info
->data_reg_only_16bit
)
1257 chan_arr
[chan_index
].scan_type
= ad4113_scan_type
;
1262 device_for_each_child_node_scoped(dev
, child
) {
1263 bool is_current_chan
= false;
1265 chan
= &chan_arr
[chan_index
];
1266 *chan
= ad7173_channel_template
;
1267 chan_st_priv
= &chans_st_arr
[chan_index
];
1268 ret
= fwnode_property_read_u32_array(child
, "diff-channels",
1269 ain
, ARRAY_SIZE(ain
));
1271 ret
= fwnode_property_read_u32(child
, "single-channel",
1274 return dev_err_probe(dev
, ret
,
1275 "Channel must define one of diff-channels or single-channel.\n");
1277 is_current_chan
= fwnode_property_read_bool(child
, "adi,current-channel");
1279 chan
->differential
= true;
1282 if (is_current_chan
) {
1283 ret
= ad4111_validate_current_ain(st
, ain
);
1287 if (!chan
->differential
) {
1288 ret
= fwnode_property_read_u32(child
,
1289 "common-mode-channel", ain
+ 1);
1291 return dev_err_probe(dev
, ret
,
1292 "common-mode-channel must be defined for single-ended channels.\n");
1294 ret
= ad7173_validate_voltage_ain_inputs(st
, ain
[0], ain
[1]);
1299 ret
= fwnode_property_match_property_string(child
,
1300 "adi,reference-select",
1302 ARRAY_SIZE(ad7173_ref_sel_str
));
1304 ref_sel
= AD7173_SETUP_REF_SEL_INT_REF
;
1308 ret
= ad7173_validate_reference(st
, ref_sel
);
1312 if (ref_sel
== AD7173_SETUP_REF_SEL_INT_REF
)
1313 st
->adc_mode
|= AD7173_ADC_MODE_REF_EN
;
1314 chan_st_priv
->cfg
.ref_sel
= ref_sel
;
1316 chan
->address
= chan_index
;
1317 chan
->scan_index
= chan_index
;
1318 chan
->channel
= ain
[0];
1319 chan_st_priv
->chan_reg
= chan_index
;
1320 chan_st_priv
->cfg
.input_buf
= st
->info
->has_input_buf
;
1321 chan_st_priv
->cfg
.odr
= 0;
1323 chan_st_priv
->cfg
.bipolar
= fwnode_property_read_bool(child
, "bipolar");
1324 if (chan_st_priv
->cfg
.bipolar
)
1325 chan
->info_mask_separate
|= BIT(IIO_CHAN_INFO_OFFSET
);
1327 if (is_current_chan
) {
1328 chan
->type
= IIO_CURRENT
;
1329 chan
->differential
= false;
1331 chan_st_priv
->ain
= ad4111_current_channel_config
[ain
[0]];
1333 chan_st_priv
->cfg
.input_buf
= st
->info
->has_input_buf
;
1334 chan
->channel2
= ain
[1];
1335 chan_st_priv
->ain
= AD7173_CH_ADDRESS(ain
[0], ain
[1]);
1338 if (st
->info
->data_reg_only_16bit
)
1339 chan_arr
[chan_index
].scan_type
= ad4113_scan_type
;
1346 static int ad7173_fw_parse_device_config(struct iio_dev
*indio_dev
)
1348 struct ad7173_state
*st
= iio_priv(indio_dev
);
1349 struct device
*dev
= indio_dev
->dev
.parent
;
1352 st
->regulators
[0].supply
= ad7173_ref_sel_str
[AD7173_SETUP_REF_SEL_EXT_REF
];
1353 st
->regulators
[1].supply
= ad7173_ref_sel_str
[AD7173_SETUP_REF_SEL_EXT_REF2
];
1354 st
->regulators
[2].supply
= ad7173_ref_sel_str
[AD7173_SETUP_REF_SEL_AVDD1_AVSS
];
1357 * If a regulator is not available, it will be set to a dummy regulator.
1358 * Each channel reference is checked with regulator_get_voltage() before
1359 * setting attributes so if any channel uses a dummy supply the driver
1362 ret
= devm_regulator_bulk_get(dev
, ARRAY_SIZE(st
->regulators
),
1365 return dev_err_probe(dev
, ret
, "Failed to get regulators\n");
1367 ret
= regulator_bulk_enable(ARRAY_SIZE(st
->regulators
), st
->regulators
);
1369 return dev_err_probe(dev
, ret
, "Failed to enable regulators\n");
1371 ret
= devm_add_action_or_reset(dev
, ad7173_disable_regulators
, st
);
1373 return dev_err_probe(dev
, ret
,
1374 "Failed to add regulators disable action\n");
1376 ret
= device_property_match_property_string(dev
, "clock-names",
1378 ARRAY_SIZE(ad7173_clk_sel
));
1380 st
->adc_mode
|= FIELD_PREP(AD7173_ADC_MODE_CLOCKSEL_MASK
,
1381 AD7173_ADC_MODE_CLOCKSEL_INT
);
1382 ad7173_register_clk_provider(indio_dev
);
1384 st
->adc_mode
|= FIELD_PREP(AD7173_ADC_MODE_CLOCKSEL_MASK
,
1385 AD7173_ADC_MODE_CLOCKSEL_EXT
+ ret
);
1386 st
->ext_clk
= devm_clk_get(dev
, ad7173_clk_sel
[ret
]);
1387 if (IS_ERR(st
->ext_clk
))
1388 return dev_err_probe(dev
, PTR_ERR(st
->ext_clk
),
1389 "Failed to get external clock\n");
1391 ret
= clk_prepare_enable(st
->ext_clk
);
1393 return dev_err_probe(dev
, ret
,
1394 "Failed to enable external clock\n");
1396 ret
= devm_add_action_or_reset(dev
, ad7173_clk_disable_unprepare
,
1402 ret
= fwnode_irq_get_byname(dev_fwnode(dev
), "rdy");
1404 return dev_err_probe(dev
, ret
, "Interrupt 'rdy' is required\n");
1406 ad7173_sigma_delta_info
.irq_line
= ret
;
1408 return ad7173_fw_parse_channel_config(indio_dev
);
1411 static int ad7173_probe(struct spi_device
*spi
)
1413 struct device
*dev
= &spi
->dev
;
1414 struct ad7173_state
*st
;
1415 struct iio_dev
*indio_dev
;
1418 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*st
));
1422 st
= iio_priv(indio_dev
);
1423 st
->info
= spi_get_device_match_data(spi
);
1427 ida_init(&st
->cfg_slots_status
);
1428 ret
= devm_add_action_or_reset(dev
, ad7173_ida_destroy
, st
);
1432 indio_dev
->name
= st
->info
->name
;
1433 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1434 indio_dev
->info
= &ad7173_info
;
1436 spi
->mode
= SPI_MODE_3
;
1439 ad7173_sigma_delta_info
.num_slots
= st
->info
->num_configs
;
1440 ret
= ad_sd_init(&st
->sd
, indio_dev
, spi
, &ad7173_sigma_delta_info
);
1444 ret
= ad7173_fw_parse_device_config(indio_dev
);
1448 ret
= devm_ad_sd_setup_buffer_and_trigger(dev
, indio_dev
);
1452 ret
= ad7173_setup(indio_dev
);
1456 ret
= devm_iio_device_register(dev
, indio_dev
);
1460 if (IS_ENABLED(CONFIG_GPIOLIB
))
1461 return ad7173_gpio_init(st
);
1466 static const struct of_device_id ad7173_of_match
[] = {
1467 { .compatible
= "adi,ad4111", .data
= &ad4111_device_info
},
1468 { .compatible
= "adi,ad4112", .data
= &ad4112_device_info
},
1469 { .compatible
= "adi,ad4113", .data
= &ad4113_device_info
},
1470 { .compatible
= "adi,ad4114", .data
= &ad4114_device_info
},
1471 { .compatible
= "adi,ad4115", .data
= &ad4115_device_info
},
1472 { .compatible
= "adi,ad4116", .data
= &ad4116_device_info
},
1473 { .compatible
= "adi,ad7172-2", .data
= &ad7172_2_device_info
},
1474 { .compatible
= "adi,ad7172-4", .data
= &ad7172_4_device_info
},
1475 { .compatible
= "adi,ad7173-8", .data
= &ad7173_8_device_info
},
1476 { .compatible
= "adi,ad7175-2", .data
= &ad7175_2_device_info
},
1477 { .compatible
= "adi,ad7175-8", .data
= &ad7175_8_device_info
},
1478 { .compatible
= "adi,ad7176-2", .data
= &ad7176_2_device_info
},
1479 { .compatible
= "adi,ad7177-2", .data
= &ad7177_2_device_info
},
1482 MODULE_DEVICE_TABLE(of
, ad7173_of_match
);
1484 static const struct spi_device_id ad7173_id_table
[] = {
1485 { "ad4111", (kernel_ulong_t
)&ad4111_device_info
},
1486 { "ad4112", (kernel_ulong_t
)&ad4112_device_info
},
1487 { "ad4113", (kernel_ulong_t
)&ad4113_device_info
},
1488 { "ad4114", (kernel_ulong_t
)&ad4114_device_info
},
1489 { "ad4115", (kernel_ulong_t
)&ad4115_device_info
},
1490 { "ad4116", (kernel_ulong_t
)&ad4116_device_info
},
1491 { "ad7172-2", (kernel_ulong_t
)&ad7172_2_device_info
},
1492 { "ad7172-4", (kernel_ulong_t
)&ad7172_4_device_info
},
1493 { "ad7173-8", (kernel_ulong_t
)&ad7173_8_device_info
},
1494 { "ad7175-2", (kernel_ulong_t
)&ad7175_2_device_info
},
1495 { "ad7175-8", (kernel_ulong_t
)&ad7175_8_device_info
},
1496 { "ad7176-2", (kernel_ulong_t
)&ad7176_2_device_info
},
1497 { "ad7177-2", (kernel_ulong_t
)&ad7177_2_device_info
},
1500 MODULE_DEVICE_TABLE(spi
, ad7173_id_table
);
1502 static struct spi_driver ad7173_driver
= {
1505 .of_match_table
= ad7173_of_match
,
1507 .probe
= ad7173_probe
,
1508 .id_table
= ad7173_id_table
,
1510 module_spi_driver(ad7173_driver
);
1512 MODULE_IMPORT_NS("IIO_AD_SIGMA_DELTA");
1513 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafo.de>");
1514 MODULE_AUTHOR("Dumitru Ceclan <dumitru.ceclan@analog.com>");
1515 MODULE_DESCRIPTION("Analog Devices AD7173 and similar ADC driver");
1516 MODULE_LICENSE("GPL");