1 // SPDX-License-Identifier: GPL-2.0-only
3 * MAX11410 SPI ADC driver
5 * Copyright 2022 Analog Devices Inc.
7 #include <linux/bitfield.h>
8 #include <linux/delay.h>
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/interrupt.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/regmap.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/spi/spi.h>
18 #include <linux/unaligned.h>
20 #include <linux/iio/buffer.h>
21 #include <linux/iio/sysfs.h>
22 #include <linux/iio/trigger.h>
23 #include <linux/iio/trigger_consumer.h>
24 #include <linux/iio/triggered_buffer.h>
26 #define MAX11410_REG_CONV_START 0x01
27 #define MAX11410_CONV_TYPE_SINGLE 0x00
28 #define MAX11410_CONV_TYPE_CONTINUOUS 0x01
29 #define MAX11410_REG_CAL_START 0x03
30 #define MAX11410_CAL_START_SELF 0x00
31 #define MAX11410_CAL_START_PGA 0x01
32 #define MAX11410_REG_GPIO_CTRL(ch) ((ch) ? 0x05 : 0x04)
33 #define MAX11410_GPIO_INTRB 0xC1
34 #define MAX11410_REG_FILTER 0x08
35 #define MAX11410_FILTER_RATE_MASK GENMASK(3, 0)
36 #define MAX11410_FILTER_RATE_MAX 0x0F
37 #define MAX11410_FILTER_LINEF_MASK GENMASK(5, 4)
38 #define MAX11410_FILTER_50HZ BIT(5)
39 #define MAX11410_FILTER_60HZ BIT(4)
40 #define MAX11410_REG_CTRL 0x09
41 #define MAX11410_CTRL_REFSEL_MASK GENMASK(2, 0)
42 #define MAX11410_CTRL_VREFN_BUF_BIT BIT(3)
43 #define MAX11410_CTRL_VREFP_BUF_BIT BIT(4)
44 #define MAX11410_CTRL_FORMAT_BIT BIT(5)
45 #define MAX11410_CTRL_UNIPOLAR_BIT BIT(6)
46 #define MAX11410_REG_MUX_CTRL0 0x0B
47 #define MAX11410_REG_PGA 0x0E
48 #define MAX11410_PGA_GAIN_MASK GENMASK(2, 0)
49 #define MAX11410_PGA_SIG_PATH_MASK GENMASK(5, 4)
50 #define MAX11410_PGA_SIG_PATH_BUFFERED 0x00
51 #define MAX11410_PGA_SIG_PATH_BYPASS 0x01
52 #define MAX11410_PGA_SIG_PATH_PGA 0x02
53 #define MAX11410_REG_DATA0 0x30
54 #define MAX11410_REG_STATUS 0x38
55 #define MAX11410_STATUS_CONV_READY_BIT BIT(0)
56 #define MAX11410_STATUS_CAL_READY_BIT BIT(2)
58 #define MAX11410_REFSEL_AVDD_AGND 0x03
59 #define MAX11410_REFSEL_MAX 0x06
60 #define MAX11410_SIG_PATH_MAX 0x02
61 #define MAX11410_CHANNEL_INDEX_MAX 0x0A
62 #define MAX11410_AINP_AVDD 0x0A
63 #define MAX11410_AINN_GND 0x0A
65 #define MAX11410_CONVERSION_TIMEOUT_MS 2000
66 #define MAX11410_CALIB_TIMEOUT_MS 2000
68 #define MAX11410_SCALE_AVAIL_SIZE 8
70 enum max11410_filter
{
71 MAX11410_FILTER_FIR5060
,
72 MAX11410_FILTER_FIR50
,
73 MAX11410_FILTER_FIR60
,
74 MAX11410_FILTER_SINC4
,
77 static const u8 max11410_sampling_len
[] = {
78 [MAX11410_FILTER_FIR5060
] = 5,
79 [MAX11410_FILTER_FIR50
] = 6,
80 [MAX11410_FILTER_FIR60
] = 6,
81 [MAX11410_FILTER_SINC4
] = 10,
84 static const int max11410_sampling_rates
[4][10][2] = {
85 [MAX11410_FILTER_FIR5060
] = {
92 [MAX11410_FILTER_FIR50
] = {
100 [MAX11410_FILTER_FIR60
] = {
108 [MAX11410_FILTER_SINC4
] = {
122 struct max11410_channel_config
{
123 u32 settling_time_us
;
133 struct max11410_state
{
134 struct spi_device
*spi_dev
;
135 struct iio_trigger
*trig
;
136 struct completion completion
;
137 struct mutex lock
; /* Prevent changing channel config during sampling */
138 struct regmap
*regmap
;
139 struct regulator
*avdd
;
140 struct regulator
*vrefp
[3];
141 struct regulator
*vrefn
[3];
142 struct max11410_channel_config
*channels
;
145 u32 data
__aligned(IIO_DMA_MINALIGN
);
150 static const struct iio_chan_spec chanspec_template
= {
153 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
154 BIT(IIO_CHAN_INFO_SCALE
) |
155 BIT(IIO_CHAN_INFO_OFFSET
),
156 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
157 .info_mask_shared_by_all_available
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
162 .endianness
= IIO_LE
,
166 static unsigned int max11410_reg_size(unsigned int reg
)
168 /* Registers from 0x00 to 0x10 are 1 byte, the rest are 3 bytes long. */
169 return reg
<= 0x10 ? 1 : 3;
172 static int max11410_write_reg(struct max11410_state
*st
, unsigned int reg
,
175 /* This driver only needs to write 8-bit registers */
176 if (max11410_reg_size(reg
) != 1)
179 return regmap_write(st
->regmap
, reg
, val
);
182 static int max11410_read_reg(struct max11410_state
*st
, unsigned int reg
,
187 if (max11410_reg_size(reg
) == 3) {
188 ret
= regmap_bulk_read(st
->regmap
, reg
, &st
->scan
.data
, 3);
192 *val
= get_unaligned_be24(&st
->scan
.data
);
196 return regmap_read(st
->regmap
, reg
, val
);
199 static struct regulator
*max11410_get_vrefp(struct max11410_state
*st
,
206 return st
->vrefp
[refsel
];
209 static struct regulator
*max11410_get_vrefn(struct max11410_state
*st
,
215 return st
->vrefn
[refsel
];
218 static const struct regmap_config regmap_config
= {
221 .max_register
= 0x39,
224 static ssize_t
max11410_notch_en_show(struct device
*dev
,
225 struct device_attribute
*devattr
,
228 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
229 struct max11410_state
*state
= iio_priv(indio_dev
);
230 struct iio_dev_attr
*iio_attr
= to_iio_dev_attr(devattr
);
234 ret
= max11410_read_reg(state
, MAX11410_REG_FILTER
, &val
);
238 switch (iio_attr
->address
) {
240 val
= !FIELD_GET(MAX11410_FILTER_50HZ
, val
);
243 val
= !FIELD_GET(MAX11410_FILTER_60HZ
, val
);
246 val
= FIELD_GET(MAX11410_FILTER_LINEF_MASK
, val
) == 3;
252 return sysfs_emit(buf
, "%d\n", val
);
255 static ssize_t
max11410_notch_en_store(struct device
*dev
,
256 struct device_attribute
*devattr
,
257 const char *buf
, size_t count
)
259 struct iio_dev_attr
*iio_attr
= to_iio_dev_attr(devattr
);
260 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
261 struct max11410_state
*state
= iio_priv(indio_dev
);
262 unsigned int filter_bits
;
266 ret
= kstrtobool(buf
, &enable
);
270 switch (iio_attr
->address
) {
272 filter_bits
= MAX11410_FILTER_50HZ
;
275 filter_bits
= MAX11410_FILTER_60HZ
;
279 filter_bits
= MAX11410_FILTER_50HZ
| MAX11410_FILTER_60HZ
;
285 ret
= regmap_clear_bits(state
->regmap
, MAX11410_REG_FILTER
,
288 ret
= regmap_set_bits(state
->regmap
, MAX11410_REG_FILTER
,
297 static ssize_t
in_voltage_filter2_notch_center_show(struct device
*dev
,
298 struct device_attribute
*devattr
,
301 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
302 struct max11410_state
*state
= iio_priv(indio_dev
);
303 int ret
, reg
, rate
, filter
;
305 ret
= regmap_read(state
->regmap
, MAX11410_REG_FILTER
, ®
);
309 rate
= FIELD_GET(MAX11410_FILTER_RATE_MASK
, reg
);
310 rate
= clamp_val(rate
, 0,
311 max11410_sampling_len
[MAX11410_FILTER_SINC4
] - 1);
312 filter
= max11410_sampling_rates
[MAX11410_FILTER_SINC4
][rate
][0];
314 return sysfs_emit(buf
, "%d\n", filter
);
317 static IIO_CONST_ATTR(in_voltage_filter0_notch_center
, "50");
318 static IIO_CONST_ATTR(in_voltage_filter1_notch_center
, "60");
319 static IIO_DEVICE_ATTR_RO(in_voltage_filter2_notch_center
, 2);
321 static IIO_DEVICE_ATTR(in_voltage_filter0_notch_en
, 0644,
322 max11410_notch_en_show
, max11410_notch_en_store
, 0);
323 static IIO_DEVICE_ATTR(in_voltage_filter1_notch_en
, 0644,
324 max11410_notch_en_show
, max11410_notch_en_store
, 1);
325 static IIO_DEVICE_ATTR(in_voltage_filter2_notch_en
, 0644,
326 max11410_notch_en_show
, max11410_notch_en_store
, 2);
328 static struct attribute
*max11410_attributes
[] = {
329 &iio_const_attr_in_voltage_filter0_notch_center
.dev_attr
.attr
,
330 &iio_const_attr_in_voltage_filter1_notch_center
.dev_attr
.attr
,
331 &iio_dev_attr_in_voltage_filter2_notch_center
.dev_attr
.attr
,
332 &iio_dev_attr_in_voltage_filter0_notch_en
.dev_attr
.attr
,
333 &iio_dev_attr_in_voltage_filter1_notch_en
.dev_attr
.attr
,
334 &iio_dev_attr_in_voltage_filter2_notch_en
.dev_attr
.attr
,
338 static const struct attribute_group max11410_attribute_group
= {
339 .attrs
= max11410_attributes
,
342 static int max11410_set_input_mux(struct max11410_state
*st
, u8 ainp
, u8 ainn
)
344 if (ainp
> MAX11410_CHANNEL_INDEX_MAX
||
345 ainn
> MAX11410_CHANNEL_INDEX_MAX
)
348 return max11410_write_reg(st
, MAX11410_REG_MUX_CTRL0
,
352 static int max11410_configure_channel(struct max11410_state
*st
,
353 struct iio_chan_spec
const *chan
)
355 struct max11410_channel_config cfg
= st
->channels
[chan
->address
];
359 if (chan
->differential
)
360 ret
= max11410_set_input_mux(st
, chan
->channel
, chan
->channel2
);
362 ret
= max11410_set_input_mux(st
, chan
->channel
,
368 regval
= FIELD_PREP(MAX11410_CTRL_VREFP_BUF_BIT
, cfg
.buffered_vrefp
) |
369 FIELD_PREP(MAX11410_CTRL_VREFN_BUF_BIT
, cfg
.buffered_vrefn
) |
370 FIELD_PREP(MAX11410_CTRL_REFSEL_MASK
, cfg
.refsel
) |
371 FIELD_PREP(MAX11410_CTRL_UNIPOLAR_BIT
, cfg
.bipolar
? 0 : 1);
372 ret
= regmap_update_bits(st
->regmap
, MAX11410_REG_CTRL
,
373 MAX11410_CTRL_REFSEL_MASK
|
374 MAX11410_CTRL_VREFP_BUF_BIT
|
375 MAX11410_CTRL_VREFN_BUF_BIT
|
376 MAX11410_CTRL_UNIPOLAR_BIT
, regval
);
380 regval
= FIELD_PREP(MAX11410_PGA_SIG_PATH_MASK
, cfg
.sig_path
) |
381 FIELD_PREP(MAX11410_PGA_GAIN_MASK
, cfg
.gain
);
382 ret
= regmap_write(st
->regmap
, MAX11410_REG_PGA
, regval
);
386 if (cfg
.settling_time_us
)
387 fsleep(cfg
.settling_time_us
);
392 static int max11410_sample(struct max11410_state
*st
, int *sample_raw
,
393 struct iio_chan_spec
const *chan
)
397 ret
= max11410_configure_channel(st
, chan
);
402 reinit_completion(&st
->completion
);
404 /* Start Conversion */
405 ret
= max11410_write_reg(st
, MAX11410_REG_CONV_START
,
406 MAX11410_CONV_TYPE_SINGLE
);
411 /* Wait for an interrupt. */
412 ret
= wait_for_completion_timeout(&st
->completion
,
413 msecs_to_jiffies(MAX11410_CONVERSION_TIMEOUT_MS
));
419 /* Wait for status register Conversion Ready flag */
420 ret
= read_poll_timeout(max11410_read_reg
, ret2
,
421 ret2
|| (val
& MAX11410_STATUS_CONV_READY_BIT
),
422 5000, MAX11410_CONVERSION_TIMEOUT_MS
* 1000,
423 true, st
, MAX11410_REG_STATUS
, &val
);
431 return max11410_read_reg(st
, MAX11410_REG_DATA0
, sample_raw
);
434 static int max11410_get_scale(struct max11410_state
*state
,
435 struct max11410_channel_config cfg
)
437 struct regulator
*vrefp
, *vrefn
;
440 vrefp
= max11410_get_vrefp(state
, cfg
.refsel
);
442 scale
= regulator_get_voltage(vrefp
) / 1000;
443 vrefn
= max11410_get_vrefn(state
, cfg
.refsel
);
445 scale
-= regulator_get_voltage(vrefn
) / 1000;
450 return scale
>> cfg
.gain
;
453 static int max11410_read_raw(struct iio_dev
*indio_dev
,
454 struct iio_chan_spec
const *chan
,
455 int *val
, int *val2
, long info
)
457 struct max11410_state
*state
= iio_priv(indio_dev
);
458 struct max11410_channel_config cfg
= state
->channels
[chan
->address
];
459 int ret
, reg_val
, filter
, rate
;
462 case IIO_CHAN_INFO_SCALE
:
463 *val
= max11410_get_scale(state
, cfg
);
464 *val2
= chan
->scan_type
.realbits
;
465 return IIO_VAL_FRACTIONAL_LOG2
;
466 case IIO_CHAN_INFO_OFFSET
:
468 *val
= -BIT(chan
->scan_type
.realbits
- 1);
473 case IIO_CHAN_INFO_RAW
:
474 ret
= iio_device_claim_direct_mode(indio_dev
);
478 mutex_lock(&state
->lock
);
480 ret
= max11410_sample(state
, ®_val
, chan
);
482 mutex_unlock(&state
->lock
);
484 iio_device_release_direct_mode(indio_dev
);
492 case IIO_CHAN_INFO_SAMP_FREQ
:
493 ret
= regmap_read(state
->regmap
, MAX11410_REG_FILTER
, ®_val
);
497 filter
= FIELD_GET(MAX11410_FILTER_LINEF_MASK
, reg_val
);
498 rate
= reg_val
& MAX11410_FILTER_RATE_MASK
;
499 if (rate
>= max11410_sampling_len
[filter
])
500 rate
= max11410_sampling_len
[filter
] - 1;
502 *val
= max11410_sampling_rates
[filter
][rate
][0];
503 *val2
= max11410_sampling_rates
[filter
][rate
][1];
505 return IIO_VAL_INT_PLUS_MICRO
;
510 static int max11410_write_raw(struct iio_dev
*indio_dev
,
511 struct iio_chan_spec
const *chan
,
512 int val
, int val2
, long mask
)
514 struct max11410_state
*st
= iio_priv(indio_dev
);
515 int i
, ret
, reg_val
, filter
, gain
;
519 case IIO_CHAN_INFO_SCALE
:
520 scale_avail
= st
->channels
[chan
->address
].scale_avail
;
524 /* Accept values in range 0.000001 <= scale < 1.000000 */
525 if (val
!= 0 || val2
== 0)
528 ret
= iio_device_claim_direct_mode(indio_dev
);
532 /* Convert from INT_PLUS_MICRO to FRACTIONAL_LOG2 */
533 val2
= val2
* DIV_ROUND_CLOSEST(BIT(24), 1000000);
534 val2
= DIV_ROUND_CLOSEST(scale_avail
[0], val2
);
535 gain
= order_base_2(val2
);
537 st
->channels
[chan
->address
].gain
= clamp_val(gain
, 0, 7);
539 iio_device_release_direct_mode(indio_dev
);
542 case IIO_CHAN_INFO_SAMP_FREQ
:
543 ret
= iio_device_claim_direct_mode(indio_dev
);
547 mutex_lock(&st
->lock
);
549 ret
= regmap_read(st
->regmap
, MAX11410_REG_FILTER
, ®_val
);
553 filter
= FIELD_GET(MAX11410_FILTER_LINEF_MASK
, reg_val
);
555 for (i
= 0; i
< max11410_sampling_len
[filter
]; ++i
) {
556 if (val
== max11410_sampling_rates
[filter
][i
][0] &&
557 val2
== max11410_sampling_rates
[filter
][i
][1])
560 if (i
== max11410_sampling_len
[filter
]) {
565 ret
= regmap_write_bits(st
->regmap
, MAX11410_REG_FILTER
,
566 MAX11410_FILTER_RATE_MASK
, i
);
569 mutex_unlock(&st
->lock
);
570 iio_device_release_direct_mode(indio_dev
);
578 static int max11410_read_avail(struct iio_dev
*indio_dev
,
579 struct iio_chan_spec
const *chan
,
580 const int **vals
, int *type
, int *length
,
583 struct max11410_state
*st
= iio_priv(indio_dev
);
584 struct max11410_channel_config cfg
;
585 int ret
, reg_val
, filter
;
588 case IIO_CHAN_INFO_SAMP_FREQ
:
589 ret
= regmap_read(st
->regmap
, MAX11410_REG_FILTER
, ®_val
);
593 filter
= FIELD_GET(MAX11410_FILTER_LINEF_MASK
, reg_val
);
595 *vals
= (const int *)max11410_sampling_rates
[filter
];
596 *length
= max11410_sampling_len
[filter
] * 2;
597 *type
= IIO_VAL_INT_PLUS_MICRO
;
599 return IIO_AVAIL_LIST
;
600 case IIO_CHAN_INFO_SCALE
:
601 cfg
= st
->channels
[chan
->address
];
603 if (!cfg
.scale_avail
)
606 *vals
= cfg
.scale_avail
;
607 *length
= MAX11410_SCALE_AVAIL_SIZE
* 2;
608 *type
= IIO_VAL_FRACTIONAL_LOG2
;
610 return IIO_AVAIL_LIST
;
615 static const struct iio_info max11410_info
= {
616 .read_raw
= max11410_read_raw
,
617 .write_raw
= max11410_write_raw
,
618 .read_avail
= max11410_read_avail
,
619 .attrs
= &max11410_attribute_group
,
622 static irqreturn_t
max11410_trigger_handler(int irq
, void *p
)
624 struct iio_poll_func
*pf
= p
;
625 struct iio_dev
*indio_dev
= pf
->indio_dev
;
626 struct max11410_state
*st
= iio_priv(indio_dev
);
629 ret
= max11410_read_reg(st
, MAX11410_REG_DATA0
, &st
->scan
.data
);
631 dev_err(&indio_dev
->dev
, "cannot read data\n");
635 iio_push_to_buffers_with_timestamp(indio_dev
, &st
->scan
,
636 iio_get_time_ns(indio_dev
));
639 iio_trigger_notify_done(indio_dev
->trig
);
644 static int max11410_buffer_postenable(struct iio_dev
*indio_dev
)
646 struct max11410_state
*st
= iio_priv(indio_dev
);
649 scan_ch
= ffs(*indio_dev
->active_scan_mask
) - 1;
651 ret
= max11410_configure_channel(st
, &indio_dev
->channels
[scan_ch
]);
655 /* Start continuous conversion. */
656 return max11410_write_reg(st
, MAX11410_REG_CONV_START
,
657 MAX11410_CONV_TYPE_CONTINUOUS
);
660 static int max11410_buffer_predisable(struct iio_dev
*indio_dev
)
662 struct max11410_state
*st
= iio_priv(indio_dev
);
664 /* Stop continuous conversion. */
665 return max11410_write_reg(st
, MAX11410_REG_CONV_START
,
666 MAX11410_CONV_TYPE_SINGLE
);
669 static const struct iio_buffer_setup_ops max11410_buffer_ops
= {
670 .postenable
= &max11410_buffer_postenable
,
671 .predisable
= &max11410_buffer_predisable
,
672 .validate_scan_mask
= &iio_validate_scan_mask_onehot
,
675 static const struct iio_trigger_ops max11410_trigger_ops
= {
676 .validate_device
= iio_trigger_validate_own_device
,
679 static irqreturn_t
max11410_interrupt(int irq
, void *dev_id
)
681 struct iio_dev
*indio_dev
= dev_id
;
682 struct max11410_state
*st
= iio_priv(indio_dev
);
684 if (iio_buffer_enabled(indio_dev
))
685 iio_trigger_poll_nested(st
->trig
);
687 complete(&st
->completion
);
692 static int max11410_parse_channels(struct max11410_state
*st
,
693 struct iio_dev
*indio_dev
)
695 struct iio_chan_spec chanspec
= chanspec_template
;
696 struct device
*dev
= &st
->spi_dev
->dev
;
697 struct max11410_channel_config
*cfg
;
698 struct iio_chan_spec
*channels
;
699 u32 reference
, sig_path
;
700 const char *node_name
;
701 u32 inputs
[2], scale
;
706 num_ch
= device_get_child_node_count(dev
);
708 return dev_err_probe(&indio_dev
->dev
, -ENODEV
,
709 "FW has no channels defined\n");
711 /* Reserve space for soft timestamp channel */
713 channels
= devm_kcalloc(dev
, num_ch
, sizeof(*channels
), GFP_KERNEL
);
717 st
->channels
= devm_kcalloc(dev
, num_ch
, sizeof(*st
->channels
),
722 device_for_each_child_node_scoped(dev
, child
) {
723 node_name
= fwnode_get_name(child
);
724 if (fwnode_property_present(child
, "diff-channels")) {
725 ret
= fwnode_property_read_u32_array(child
,
730 chanspec
.differential
= 1;
732 ret
= fwnode_property_read_u32(child
, "reg", &inputs
[0]);
735 chanspec
.differential
= 0;
740 if (inputs
[0] > MAX11410_CHANNEL_INDEX_MAX
||
741 inputs
[1] > MAX11410_CHANNEL_INDEX_MAX
)
742 return dev_err_probe(&indio_dev
->dev
, -EINVAL
,
743 "Invalid channel index for %s, should be less than %d\n",
745 MAX11410_CHANNEL_INDEX_MAX
+ 1);
747 cfg
= &st
->channels
[chan_idx
];
749 reference
= MAX11410_REFSEL_AVDD_AGND
;
750 fwnode_property_read_u32(child
, "adi,reference", &reference
);
751 if (reference
> MAX11410_REFSEL_MAX
)
752 return dev_err_probe(&indio_dev
->dev
, -EINVAL
,
753 "Invalid adi,reference value for %s, should be less than %d.\n",
754 node_name
, MAX11410_REFSEL_MAX
+ 1);
756 if (!max11410_get_vrefp(st
, reference
) ||
757 (!max11410_get_vrefn(st
, reference
) && reference
<= 2))
758 return dev_err_probe(&indio_dev
->dev
, -EINVAL
,
759 "Invalid VREF configuration for %s, either specify corresponding VREF regulators or change adi,reference property.\n",
762 sig_path
= MAX11410_PGA_SIG_PATH_BUFFERED
;
763 fwnode_property_read_u32(child
, "adi,input-mode", &sig_path
);
764 if (sig_path
> MAX11410_SIG_PATH_MAX
)
765 return dev_err_probe(&indio_dev
->dev
, -EINVAL
,
766 "Invalid adi,input-mode value for %s, should be less than %d.\n",
767 node_name
, MAX11410_SIG_PATH_MAX
+ 1);
769 fwnode_property_read_u32(child
, "settling-time-us",
770 &cfg
->settling_time_us
);
771 cfg
->bipolar
= fwnode_property_read_bool(child
, "bipolar");
772 cfg
->buffered_vrefp
= fwnode_property_read_bool(child
, "adi,buffered-vrefp");
773 cfg
->buffered_vrefn
= fwnode_property_read_bool(child
, "adi,buffered-vrefn");
774 cfg
->refsel
= reference
;
775 cfg
->sig_path
= sig_path
;
778 /* Enable scale_available property if input mode is PGA */
779 if (sig_path
== MAX11410_PGA_SIG_PATH_PGA
) {
780 __set_bit(IIO_CHAN_INFO_SCALE
,
781 &chanspec
.info_mask_separate_available
);
782 cfg
->scale_avail
= devm_kcalloc(dev
, MAX11410_SCALE_AVAIL_SIZE
* 2,
783 sizeof(*cfg
->scale_avail
),
785 if (!cfg
->scale_avail
)
788 scale
= max11410_get_scale(st
, *cfg
);
789 for (i
= 0; i
< MAX11410_SCALE_AVAIL_SIZE
; i
++) {
790 cfg
->scale_avail
[2 * i
] = scale
>> i
;
791 cfg
->scale_avail
[2 * i
+ 1] = chanspec
.scan_type
.realbits
;
794 __clear_bit(IIO_CHAN_INFO_SCALE
,
795 &chanspec
.info_mask_separate_available
);
798 chanspec
.address
= chan_idx
;
799 chanspec
.scan_index
= chan_idx
;
800 chanspec
.channel
= inputs
[0];
801 chanspec
.channel2
= inputs
[1];
803 channels
[chan_idx
] = chanspec
;
807 channels
[chan_idx
] = (struct iio_chan_spec
)IIO_CHAN_SOFT_TIMESTAMP(chan_idx
);
809 indio_dev
->num_channels
= chan_idx
+ 1;
810 indio_dev
->channels
= channels
;
815 static void max11410_disable_reg(void *reg
)
817 regulator_disable(reg
);
820 static int max11410_init_vref(struct device
*dev
,
821 struct regulator
**vref
,
824 struct regulator
*reg
;
827 reg
= devm_regulator_get_optional(dev
, id
);
828 if (PTR_ERR(reg
) == -ENODEV
) {
831 } else if (IS_ERR(reg
)) {
834 ret
= regulator_enable(reg
);
836 return dev_err_probe(dev
, ret
,
837 "Failed to enable regulator %s\n", id
);
840 return devm_add_action_or_reset(dev
, max11410_disable_reg
, reg
);
843 static int max11410_calibrate(struct max11410_state
*st
, u32 cal_type
)
847 ret
= max11410_write_reg(st
, MAX11410_REG_CAL_START
, cal_type
);
851 /* Wait for status register Calibration Ready flag */
852 ret
= read_poll_timeout(max11410_read_reg
, ret2
,
853 ret2
|| (val
& MAX11410_STATUS_CAL_READY_BIT
),
854 50000, MAX11410_CALIB_TIMEOUT_MS
* 1000, true,
855 st
, MAX11410_REG_STATUS
, &val
);
862 static int max11410_self_calibrate(struct max11410_state
*st
)
866 ret
= regmap_write_bits(st
->regmap
, MAX11410_REG_FILTER
,
867 MAX11410_FILTER_RATE_MASK
,
868 FIELD_PREP(MAX11410_FILTER_RATE_MASK
,
869 MAX11410_FILTER_RATE_MAX
));
873 ret
= max11410_calibrate(st
, MAX11410_CAL_START_SELF
);
877 ret
= regmap_write_bits(st
->regmap
, MAX11410_REG_PGA
,
878 MAX11410_PGA_SIG_PATH_MASK
,
879 FIELD_PREP(MAX11410_PGA_SIG_PATH_MASK
,
880 MAX11410_PGA_SIG_PATH_PGA
));
884 /* PGA calibrations */
885 for (i
= 1; i
< 8; ++i
) {
886 ret
= regmap_write_bits(st
->regmap
, MAX11410_REG_PGA
,
887 MAX11410_PGA_GAIN_MASK
, i
);
891 ret
= max11410_calibrate(st
, MAX11410_CAL_START_PGA
);
897 ret
= regmap_write_bits(st
->regmap
, MAX11410_REG_PGA
,
898 MAX11410_PGA_GAIN_MASK
, 0);
902 ret
= regmap_write_bits(st
->regmap
, MAX11410_REG_FILTER
,
903 MAX11410_FILTER_RATE_MASK
, 0);
907 return regmap_write_bits(st
->regmap
, MAX11410_REG_PGA
,
908 MAX11410_PGA_SIG_PATH_MASK
,
909 FIELD_PREP(MAX11410_PGA_SIG_PATH_MASK
,
910 MAX11410_PGA_SIG_PATH_BUFFERED
));
913 static int max11410_probe(struct spi_device
*spi
)
915 const char *vrefp_regs
[] = { "vref0p", "vref1p", "vref2p" };
916 const char *vrefn_regs
[] = { "vref0n", "vref1n", "vref2n" };
917 struct device
*dev
= &spi
->dev
;
918 struct max11410_state
*st
;
919 struct iio_dev
*indio_dev
;
923 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*st
));
927 st
= iio_priv(indio_dev
);
929 init_completion(&st
->completion
);
930 mutex_init(&st
->lock
);
932 indio_dev
->name
= "max11410";
933 indio_dev
->modes
= INDIO_DIRECT_MODE
;
934 indio_dev
->info
= &max11410_info
;
936 st
->regmap
= devm_regmap_init_spi(spi
, ®map_config
);
937 if (IS_ERR(st
->regmap
))
938 return dev_err_probe(dev
, PTR_ERR(st
->regmap
),
939 "regmap initialization failed\n");
941 ret
= max11410_init_vref(dev
, &st
->avdd
, "avdd");
945 for (i
= 0; i
< ARRAY_SIZE(vrefp_regs
); i
++) {
946 ret
= max11410_init_vref(dev
, &st
->vrefp
[i
], vrefp_regs
[i
]);
950 ret
= max11410_init_vref(dev
, &st
->vrefn
[i
], vrefn_regs
[i
]);
956 * Regulators must be configured before parsing channels for
957 * validating "adi,reference" property of each channel.
959 ret
= max11410_parse_channels(st
, indio_dev
);
963 irqs
[0] = fwnode_irq_get_byname(dev_fwnode(dev
), "gpio0");
964 irqs
[1] = fwnode_irq_get_byname(dev_fwnode(dev
), "gpio1");
968 ret
= regmap_write(st
->regmap
, MAX11410_REG_GPIO_CTRL(0),
969 MAX11410_GPIO_INTRB
);
970 } else if (irqs
[1] > 0) {
972 ret
= regmap_write(st
->regmap
, MAX11410_REG_GPIO_CTRL(1),
973 MAX11410_GPIO_INTRB
);
974 } else if (spi
->irq
> 0) {
975 return dev_err_probe(dev
, -ENODEV
,
976 "no interrupt name specified");
982 ret
= regmap_set_bits(st
->regmap
, MAX11410_REG_CTRL
,
983 MAX11410_CTRL_FORMAT_BIT
);
987 ret
= devm_iio_triggered_buffer_setup(dev
, indio_dev
, NULL
,
988 &max11410_trigger_handler
,
989 &max11410_buffer_ops
);
994 st
->trig
= devm_iio_trigger_alloc(dev
, "%s-dev%d",
996 iio_device_id(indio_dev
));
1000 st
->trig
->ops
= &max11410_trigger_ops
;
1001 ret
= devm_iio_trigger_register(dev
, st
->trig
);
1005 ret
= devm_request_threaded_irq(dev
, st
->irq
, NULL
,
1006 &max11410_interrupt
,
1007 IRQF_ONESHOT
, "max11410",
1013 ret
= max11410_self_calibrate(st
);
1015 return dev_err_probe(dev
, ret
,
1016 "cannot perform device self calibration\n");
1018 return devm_iio_device_register(dev
, indio_dev
);
1021 static const struct of_device_id max11410_spi_of_id
[] = {
1022 { .compatible
= "adi,max11410" },
1025 MODULE_DEVICE_TABLE(of
, max11410_spi_of_id
);
1027 static const struct spi_device_id max11410_id
[] = {
1031 MODULE_DEVICE_TABLE(spi
, max11410_id
);
1033 static struct spi_driver max11410_driver
= {
1036 .of_match_table
= max11410_spi_of_id
,
1038 .probe
= max11410_probe
,
1039 .id_table
= max11410_id
,
1041 module_spi_driver(max11410_driver
);
1043 MODULE_AUTHOR("David Jung <David.Jung@analog.com>");
1044 MODULE_AUTHOR("Ibrahim Tilki <Ibrahim.Tilki@analog.com>");
1045 MODULE_DESCRIPTION("Analog Devices MAX11410 ADC");
1046 MODULE_LICENSE("GPL");