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 <asm/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 struct fwnode_handle *child;
700 u32 reference, sig_path;
701 const char *node_name;
702 u32 inputs[2], scale;
707 num_ch = device_get_child_node_count(dev);
709 return dev_err_probe(&indio_dev->dev, -ENODEV,
710 "FW has no channels defined\n");
712 /* Reserve space for soft timestamp channel */
714 channels = devm_kcalloc(dev, num_ch, sizeof(*channels), GFP_KERNEL);
718 st->channels = devm_kcalloc(dev, num_ch, sizeof(*st->channels),
723 device_for_each_child_node(dev, child) {
724 node_name = fwnode_get_name(child);
725 if (fwnode_property_present(child, "diff-channels")) {
726 ret = fwnode_property_read_u32_array(child,
731 chanspec.differential = 1;
733 ret = fwnode_property_read_u32(child, "reg", &inputs[0]);
736 chanspec.differential = 0;
739 fwnode_handle_put(child);
743 if (inputs[0] > MAX11410_CHANNEL_INDEX_MAX ||
744 inputs[1] > MAX11410_CHANNEL_INDEX_MAX) {
745 fwnode_handle_put(child);
746 return dev_err_probe(&indio_dev->dev, -EINVAL,
747 "Invalid channel index for %s, should be less than %d\n",
749 MAX11410_CHANNEL_INDEX_MAX + 1);
752 cfg = &st->channels[chan_idx];
754 reference = MAX11410_REFSEL_AVDD_AGND;
755 fwnode_property_read_u32(child, "adi,reference", &reference);
756 if (reference > MAX11410_REFSEL_MAX) {
757 fwnode_handle_put(child);
758 return dev_err_probe(&indio_dev->dev, -EINVAL,
759 "Invalid adi,reference value for %s, should be less than %d.\n",
760 node_name, MAX11410_REFSEL_MAX + 1);
763 if (!max11410_get_vrefp(st, reference) ||
764 (!max11410_get_vrefn(st, reference) && reference <= 2)) {
765 fwnode_handle_put(child);
766 return dev_err_probe(&indio_dev->dev, -EINVAL,
767 "Invalid VREF configuration for %s, either specify corresponding VREF regulators or change adi,reference property.\n",
771 sig_path = MAX11410_PGA_SIG_PATH_BUFFERED;
772 fwnode_property_read_u32(child, "adi,input-mode", &sig_path);
773 if (sig_path > MAX11410_SIG_PATH_MAX) {
774 fwnode_handle_put(child);
775 return dev_err_probe(&indio_dev->dev, -EINVAL,
776 "Invalid adi,input-mode value for %s, should be less than %d.\n",
777 node_name, MAX11410_SIG_PATH_MAX + 1);
780 fwnode_property_read_u32(child, "settling-time-us",
781 &cfg->settling_time_us);
782 cfg->bipolar = fwnode_property_read_bool(child, "bipolar");
783 cfg->buffered_vrefp = fwnode_property_read_bool(child, "adi,buffered-vrefp");
784 cfg->buffered_vrefn = fwnode_property_read_bool(child, "adi,buffered-vrefn");
785 cfg->refsel = reference;
786 cfg->sig_path = sig_path;
789 /* Enable scale_available property if input mode is PGA */
790 if (sig_path == MAX11410_PGA_SIG_PATH_PGA) {
791 __set_bit(IIO_CHAN_INFO_SCALE,
792 &chanspec.info_mask_separate_available);
793 cfg->scale_avail = devm_kcalloc(dev, MAX11410_SCALE_AVAIL_SIZE * 2,
794 sizeof(*cfg->scale_avail),
796 if (!cfg->scale_avail) {
797 fwnode_handle_put(child);
801 scale = max11410_get_scale(st, *cfg);
802 for (i = 0; i < MAX11410_SCALE_AVAIL_SIZE; i++) {
803 cfg->scale_avail[2 * i] = scale >> i;
804 cfg->scale_avail[2 * i + 1] = chanspec.scan_type.realbits;
807 __clear_bit(IIO_CHAN_INFO_SCALE,
808 &chanspec.info_mask_separate_available);
811 chanspec.address = chan_idx;
812 chanspec.scan_index = chan_idx;
813 chanspec.channel = inputs[0];
814 chanspec.channel2 = inputs[1];
816 channels[chan_idx] = chanspec;
820 channels[chan_idx] = (struct iio_chan_spec)IIO_CHAN_SOFT_TIMESTAMP(chan_idx);
822 indio_dev->num_channels = chan_idx + 1;
823 indio_dev->channels = channels;
828 static void max11410_disable_reg(void *reg)
830 regulator_disable(reg);
833 static int max11410_init_vref(struct device *dev,
834 struct regulator **vref,
837 struct regulator *reg;
840 reg = devm_regulator_get_optional(dev, id);
841 if (PTR_ERR(reg) == -ENODEV) {
844 } else if (IS_ERR(reg)) {
847 ret = regulator_enable(reg);
849 return dev_err_probe(dev, ret,
850 "Failed to enable regulator %s\n", id);
853 return devm_add_action_or_reset(dev, max11410_disable_reg, reg);
856 static int max11410_calibrate(struct max11410_state *st, u32 cal_type)
860 ret = max11410_write_reg(st, MAX11410_REG_CAL_START, cal_type);
864 /* Wait for status register Calibration Ready flag */
865 ret = read_poll_timeout(max11410_read_reg, ret2,
866 ret2 || (val & MAX11410_STATUS_CAL_READY_BIT),
867 50000, MAX11410_CALIB_TIMEOUT_MS * 1000, true,
868 st, MAX11410_REG_STATUS, &val);
875 static int max11410_self_calibrate(struct max11410_state *st)
879 ret = regmap_write_bits(st->regmap, MAX11410_REG_FILTER,
880 MAX11410_FILTER_RATE_MASK,
881 FIELD_PREP(MAX11410_FILTER_RATE_MASK,
882 MAX11410_FILTER_RATE_MAX));
886 ret = max11410_calibrate(st, MAX11410_CAL_START_SELF);
890 ret = regmap_write_bits(st->regmap, MAX11410_REG_PGA,
891 MAX11410_PGA_SIG_PATH_MASK,
892 FIELD_PREP(MAX11410_PGA_SIG_PATH_MASK,
893 MAX11410_PGA_SIG_PATH_PGA));
897 /* PGA calibrations */
898 for (i = 1; i < 8; ++i) {
899 ret = regmap_write_bits(st->regmap, MAX11410_REG_PGA,
900 MAX11410_PGA_GAIN_MASK, i);
904 ret = max11410_calibrate(st, MAX11410_CAL_START_PGA);
910 ret = regmap_write_bits(st->regmap, MAX11410_REG_PGA,
911 MAX11410_PGA_GAIN_MASK, 0);
915 ret = regmap_write_bits(st->regmap, MAX11410_REG_FILTER,
916 MAX11410_FILTER_RATE_MASK, 0);
920 return regmap_write_bits(st->regmap, MAX11410_REG_PGA,
921 MAX11410_PGA_SIG_PATH_MASK,
922 FIELD_PREP(MAX11410_PGA_SIG_PATH_MASK,
923 MAX11410_PGA_SIG_PATH_BUFFERED));
926 static int max11410_probe(struct spi_device *spi)
928 const char *vrefp_regs[] = { "vref0p", "vref1p", "vref2p" };
929 const char *vrefn_regs[] = { "vref0n", "vref1n", "vref2n" };
930 struct device *dev = &spi->dev;
931 struct max11410_state *st;
932 struct iio_dev *indio_dev;
936 indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
940 st = iio_priv(indio_dev);
942 init_completion(&st->completion);
943 mutex_init(&st->lock);
945 indio_dev->name = "max11410";
946 indio_dev->modes = INDIO_DIRECT_MODE;
947 indio_dev->info = &max11410_info;
949 st->regmap = devm_regmap_init_spi(spi, ®map_config);
950 if (IS_ERR(st->regmap))
951 return dev_err_probe(dev, PTR_ERR(st->regmap),
952 "regmap initialization failed\n");
954 ret = max11410_init_vref(dev, &st->avdd, "avdd");
958 for (i = 0; i < ARRAY_SIZE(vrefp_regs); i++) {
959 ret = max11410_init_vref(dev, &st->vrefp[i], vrefp_regs[i]);
963 ret = max11410_init_vref(dev, &st->vrefn[i], vrefn_regs[i]);
969 * Regulators must be configured before parsing channels for
970 * validating "adi,reference" property of each channel.
972 ret = max11410_parse_channels(st, indio_dev);
976 irqs[0] = fwnode_irq_get_byname(dev_fwnode(dev), "gpio0");
977 irqs[1] = fwnode_irq_get_byname(dev_fwnode(dev), "gpio1");
981 ret = regmap_write(st->regmap, MAX11410_REG_GPIO_CTRL(0),
982 MAX11410_GPIO_INTRB);
983 } else if (irqs[1] > 0) {
985 ret = regmap_write(st->regmap, MAX11410_REG_GPIO_CTRL(1),
986 MAX11410_GPIO_INTRB);
987 } else if (spi->irq > 0) {
988 return dev_err_probe(dev, -ENODEV,
989 "no interrupt name specified");
995 ret = regmap_set_bits(st->regmap, MAX11410_REG_CTRL,
996 MAX11410_CTRL_FORMAT_BIT);
1000 ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
1001 &max11410_trigger_handler,
1002 &max11410_buffer_ops);
1007 st->trig = devm_iio_trigger_alloc(dev, "%s-dev%d",
1009 iio_device_id(indio_dev));
1013 st->trig->ops = &max11410_trigger_ops;
1014 ret = devm_iio_trigger_register(dev, st->trig);
1018 ret = devm_request_threaded_irq(dev, st->irq, NULL,
1019 &max11410_interrupt,
1020 IRQF_ONESHOT, "max11410",
1026 ret = max11410_self_calibrate(st);
1028 return dev_err_probe(dev, ret,
1029 "cannot perform device self calibration\n");
1031 return devm_iio_device_register(dev, indio_dev);
1034 static const struct of_device_id max11410_spi_of_id[] = {
1035 { .compatible = "adi,max11410" },
1038 MODULE_DEVICE_TABLE(of, max11410_spi_of_id);
1040 static const struct spi_device_id max11410_id[] = {
1044 MODULE_DEVICE_TABLE(spi, max11410_id);
1046 static struct spi_driver max11410_driver = {
1049 .of_match_table = max11410_spi_of_id,
1051 .probe = max11410_probe,
1052 .id_table = max11410_id,
1054 module_spi_driver(max11410_driver);
1058 MODULE_DESCRIPTION("Analog Devices MAX11410 ADC");
1059 MODULE_LICENSE("GPL");