1 // SPDX-License-Identifier: GPL-2.0+
3 * AD7124 SPI ADC driver
5 * Copyright 2018 Analog Devices Inc.
7 #include <linux/bitfield.h>
9 #include <linux/delay.h>
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/regulator/consumer.h>
15 #include <linux/spi/spi.h>
17 #include <linux/iio/iio.h>
18 #include <linux/iio/adc/ad_sigma_delta.h>
19 #include <linux/iio/sysfs.h>
21 /* AD7124 registers */
22 #define AD7124_COMMS 0x00
23 #define AD7124_STATUS 0x00
24 #define AD7124_ADC_CONTROL 0x01
25 #define AD7124_DATA 0x02
26 #define AD7124_IO_CONTROL_1 0x03
27 #define AD7124_IO_CONTROL_2 0x04
28 #define AD7124_ID 0x05
29 #define AD7124_ERROR 0x06
30 #define AD7124_ERROR_EN 0x07
31 #define AD7124_MCLK_COUNT 0x08
32 #define AD7124_CHANNEL(x) (0x09 + (x))
33 #define AD7124_CONFIG(x) (0x19 + (x))
34 #define AD7124_FILTER(x) (0x21 + (x))
35 #define AD7124_OFFSET(x) (0x29 + (x))
36 #define AD7124_GAIN(x) (0x31 + (x))
39 #define AD7124_STATUS_POR_FLAG_MSK BIT(4)
41 /* AD7124_ADC_CONTROL */
42 #define AD7124_ADC_CTRL_PWR_MSK GENMASK(7, 6)
43 #define AD7124_ADC_CTRL_PWR(x) FIELD_PREP(AD7124_ADC_CTRL_PWR_MSK, x)
44 #define AD7124_ADC_CTRL_MODE_MSK GENMASK(5, 2)
45 #define AD7124_ADC_CTRL_MODE(x) FIELD_PREP(AD7124_ADC_CTRL_MODE_MSK, x)
47 /* AD7124_CHANNEL_X */
48 #define AD7124_CHANNEL_EN_MSK BIT(15)
49 #define AD7124_CHANNEL_EN(x) FIELD_PREP(AD7124_CHANNEL_EN_MSK, x)
50 #define AD7124_CHANNEL_SETUP_MSK GENMASK(14, 12)
51 #define AD7124_CHANNEL_SETUP(x) FIELD_PREP(AD7124_CHANNEL_SETUP_MSK, x)
52 #define AD7124_CHANNEL_AINP_MSK GENMASK(9, 5)
53 #define AD7124_CHANNEL_AINP(x) FIELD_PREP(AD7124_CHANNEL_AINP_MSK, x)
54 #define AD7124_CHANNEL_AINM_MSK GENMASK(4, 0)
55 #define AD7124_CHANNEL_AINM(x) FIELD_PREP(AD7124_CHANNEL_AINM_MSK, x)
58 #define AD7124_CONFIG_BIPOLAR_MSK BIT(11)
59 #define AD7124_CONFIG_BIPOLAR(x) FIELD_PREP(AD7124_CONFIG_BIPOLAR_MSK, x)
60 #define AD7124_CONFIG_REF_SEL_MSK GENMASK(4, 3)
61 #define AD7124_CONFIG_REF_SEL(x) FIELD_PREP(AD7124_CONFIG_REF_SEL_MSK, x)
62 #define AD7124_CONFIG_PGA_MSK GENMASK(2, 0)
63 #define AD7124_CONFIG_PGA(x) FIELD_PREP(AD7124_CONFIG_PGA_MSK, x)
66 #define AD7124_FILTER_FS_MSK GENMASK(10, 0)
67 #define AD7124_FILTER_FS(x) FIELD_PREP(AD7124_FILTER_FS_MSK, x)
81 enum ad7124_power_mode {
87 static const unsigned int ad7124_gain[8] = {
88 1, 2, 4, 8, 16, 32, 64, 128
91 static const int ad7124_master_clk_freq_hz[3] = {
92 [AD7124_LOW_POWER] = 76800,
93 [AD7124_MID_POWER] = 153600,
94 [AD7124_FULL_POWER] = 614400,
97 static const char * const ad7124_ref_names[] = {
98 [AD7124_REFIN1] = "refin1",
99 [AD7124_REFIN2] = "refin2",
100 [AD7124_INT_REF] = "int",
101 [AD7124_AVDD_REF] = "avdd",
104 struct ad7124_chip_info {
105 unsigned int num_inputs;
108 struct ad7124_channel_config {
109 enum ad7124_ref_sel refsel;
112 unsigned int vref_mv;
113 unsigned int pga_bits;
117 struct ad7124_state {
118 const struct ad7124_chip_info *chip_info;
119 struct ad_sigma_delta sd;
120 struct ad7124_channel_config channel_config[4];
121 struct regulator *vref[4];
123 unsigned int adc_control;
124 unsigned int num_channels;
127 static const struct iio_chan_spec ad7124_channel_template = {
131 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
132 BIT(IIO_CHAN_INFO_SCALE) |
133 BIT(IIO_CHAN_INFO_OFFSET) |
134 BIT(IIO_CHAN_INFO_SAMP_FREQ),
140 .endianness = IIO_BE,
144 static struct ad7124_chip_info ad7124_chip_info_tbl[] = {
153 static int ad7124_find_closest_match(const int *array,
154 unsigned int size, int val)
157 unsigned int diff_new, diff_old;
162 for (i = 0; i < size; i++) {
163 diff_new = abs(val - array[i]);
164 if (diff_new < diff_old) {
173 static int ad7124_spi_write_mask(struct ad7124_state *st,
179 unsigned int readval;
182 ret = ad_sd_read_reg(&st->sd, addr, bytes, &readval);
189 return ad_sd_write_reg(&st->sd, addr, bytes, readval);
192 static int ad7124_set_mode(struct ad_sigma_delta *sd,
193 enum ad_sigma_delta_mode mode)
195 struct ad7124_state *st = container_of(sd, struct ad7124_state, sd);
197 st->adc_control &= ~AD7124_ADC_CTRL_MODE_MSK;
198 st->adc_control |= AD7124_ADC_CTRL_MODE(mode);
200 return ad_sd_write_reg(&st->sd, AD7124_ADC_CONTROL, 2, st->adc_control);
203 static int ad7124_set_channel(struct ad_sigma_delta *sd, unsigned int channel)
205 struct ad7124_state *st = container_of(sd, struct ad7124_state, sd);
208 val = st->channel_config[channel].ain | AD7124_CHANNEL_EN(1) |
209 AD7124_CHANNEL_SETUP(channel);
211 return ad_sd_write_reg(&st->sd, AD7124_CHANNEL(channel), 2, val);
214 static const struct ad_sigma_delta_info ad7124_sigma_delta_info = {
215 .set_channel = ad7124_set_channel,
216 .set_mode = ad7124_set_mode,
217 .has_registers = true,
220 .data_reg = AD7124_DATA,
223 static int ad7124_set_channel_odr(struct ad7124_state *st,
224 unsigned int channel,
227 unsigned int fclk, odr_sel_bits;
230 fclk = clk_get_rate(st->mclk);
232 * FS[10:0] = fCLK / (fADC x 32) where:
233 * fADC is the output data rate
234 * fCLK is the master clock frequency
235 * FS[10:0] are the bits in the filter register
236 * FS[10:0] can have a value from 1 to 2047
238 odr_sel_bits = DIV_ROUND_CLOSEST(fclk, odr * 32);
239 if (odr_sel_bits < 1)
241 else if (odr_sel_bits > 2047)
244 ret = ad7124_spi_write_mask(st, AD7124_FILTER(channel),
245 AD7124_FILTER_FS_MSK,
246 AD7124_FILTER_FS(odr_sel_bits), 3);
249 /* fADC = fCLK / (FS[10:0] x 32) */
250 st->channel_config[channel].odr =
251 DIV_ROUND_CLOSEST(fclk, odr_sel_bits * 32);
256 static int ad7124_set_channel_gain(struct ad7124_state *st,
257 unsigned int channel,
263 res = ad7124_find_closest_match(ad7124_gain,
264 ARRAY_SIZE(ad7124_gain), gain);
265 ret = ad7124_spi_write_mask(st, AD7124_CONFIG(channel),
266 AD7124_CONFIG_PGA_MSK,
267 AD7124_CONFIG_PGA(res), 2);
271 st->channel_config[channel].pga_bits = res;
276 static int ad7124_read_raw(struct iio_dev *indio_dev,
277 struct iio_chan_spec const *chan,
278 int *val, int *val2, long info)
280 struct ad7124_state *st = iio_priv(indio_dev);
284 case IIO_CHAN_INFO_RAW:
285 ret = ad_sigma_delta_single_conversion(indio_dev, chan, val);
289 /* After the conversion is performed, disable the channel */
290 ret = ad_sd_write_reg(&st->sd,
291 AD7124_CHANNEL(chan->address), 2,
292 st->channel_config[chan->address].ain |
293 AD7124_CHANNEL_EN(0));
298 case IIO_CHAN_INFO_SCALE:
299 idx = st->channel_config[chan->address].pga_bits;
300 *val = st->channel_config[chan->address].vref_mv;
301 if (st->channel_config[chan->address].bipolar)
302 *val2 = chan->scan_type.realbits - 1 + idx;
304 *val2 = chan->scan_type.realbits + idx;
306 return IIO_VAL_FRACTIONAL_LOG2;
307 case IIO_CHAN_INFO_OFFSET:
308 if (st->channel_config[chan->address].bipolar)
309 *val = -(1 << (chan->scan_type.realbits - 1));
314 case IIO_CHAN_INFO_SAMP_FREQ:
315 *val = st->channel_config[chan->address].odr;
323 static int ad7124_write_raw(struct iio_dev *indio_dev,
324 struct iio_chan_spec const *chan,
325 int val, int val2, long info)
327 struct ad7124_state *st = iio_priv(indio_dev);
328 unsigned int res, gain, full_scale, vref;
331 case IIO_CHAN_INFO_SAMP_FREQ:
335 return ad7124_set_channel_odr(st, chan->address, val);
336 case IIO_CHAN_INFO_SCALE:
340 if (st->channel_config[chan->address].bipolar)
341 full_scale = 1 << (chan->scan_type.realbits - 1);
343 full_scale = 1 << chan->scan_type.realbits;
345 vref = st->channel_config[chan->address].vref_mv * 1000000LL;
346 res = DIV_ROUND_CLOSEST(vref, full_scale);
347 gain = DIV_ROUND_CLOSEST(res, val2);
349 return ad7124_set_channel_gain(st, chan->address, gain);
355 static IIO_CONST_ATTR(in_voltage_scale_available,
356 "0.000001164 0.000002328 0.000004656 0.000009313 0.000018626 0.000037252 0.000074505 0.000149011 0.000298023");
358 static struct attribute *ad7124_attributes[] = {
359 &iio_const_attr_in_voltage_scale_available.dev_attr.attr,
363 static const struct attribute_group ad7124_attrs_group = {
364 .attrs = ad7124_attributes,
367 static const struct iio_info ad7124_info = {
368 .read_raw = ad7124_read_raw,
369 .write_raw = ad7124_write_raw,
370 .validate_trigger = ad_sd_validate_trigger,
371 .attrs = &ad7124_attrs_group,
374 static int ad7124_soft_reset(struct ad7124_state *st)
376 unsigned int readval, timeout;
379 ret = ad_sd_reset(&st->sd, 64);
385 ret = ad_sd_read_reg(&st->sd, AD7124_STATUS, 1, &readval);
389 if (!(readval & AD7124_STATUS_POR_FLAG_MSK))
392 /* The AD7124 requires typically 2ms to power up and settle */
393 usleep_range(100, 2000);
396 dev_err(&st->sd.spi->dev, "Soft reset failed\n");
401 static int ad7124_init_channel_vref(struct ad7124_state *st,
402 unsigned int channel_number)
404 unsigned int refsel = st->channel_config[channel_number].refsel;
409 case AD7124_AVDD_REF:
410 if (IS_ERR(st->vref[refsel])) {
411 dev_err(&st->sd.spi->dev,
412 "Error, trying to use external voltage reference without a %s regulator.\n",
413 ad7124_ref_names[refsel]);
414 return PTR_ERR(st->vref[refsel]);
416 st->channel_config[channel_number].vref_mv =
417 regulator_get_voltage(st->vref[refsel]);
418 /* Conversion from uV to mV */
419 st->channel_config[channel_number].vref_mv /= 1000;
422 st->channel_config[channel_number].vref_mv = 2500;
425 dev_err(&st->sd.spi->dev, "Invalid reference %d\n", refsel);
432 static int ad7124_of_parse_channel_config(struct iio_dev *indio_dev,
433 struct device_node *np)
435 struct ad7124_state *st = iio_priv(indio_dev);
436 struct device_node *child;
437 struct iio_chan_spec *chan;
438 unsigned int ain[2], channel = 0, tmp;
441 st->num_channels = of_get_available_child_count(np);
442 if (!st->num_channels) {
443 dev_err(indio_dev->dev.parent, "no channel children\n");
447 chan = devm_kcalloc(indio_dev->dev.parent, st->num_channels,
448 sizeof(*chan), GFP_KERNEL);
452 indio_dev->channels = chan;
453 indio_dev->num_channels = st->num_channels;
455 for_each_available_child_of_node(np, child) {
456 ret = of_property_read_u32(child, "reg", &channel);
460 ret = of_property_read_u32_array(child, "diff-channels",
465 if (ain[0] >= st->chip_info->num_inputs ||
466 ain[1] >= st->chip_info->num_inputs) {
467 dev_err(indio_dev->dev.parent,
468 "Input pin number out of range.\n");
472 st->channel_config[channel].ain = AD7124_CHANNEL_AINP(ain[0]) |
473 AD7124_CHANNEL_AINM(ain[1]);
474 st->channel_config[channel].bipolar =
475 of_property_read_bool(child, "bipolar");
477 ret = of_property_read_u32(child, "adi,reference-select", &tmp);
479 st->channel_config[channel].refsel = AD7124_INT_REF;
481 st->channel_config[channel].refsel = tmp;
483 *chan = ad7124_channel_template;
484 chan->address = channel;
485 chan->scan_index = channel;
486 chan->channel = ain[0];
487 chan->channel2 = ain[1];
499 static int ad7124_setup(struct ad7124_state *st)
501 unsigned int val, fclk, power_mode;
504 fclk = clk_get_rate(st->mclk);
508 /* The power mode changes the master clock frequency */
509 power_mode = ad7124_find_closest_match(ad7124_master_clk_freq_hz,
510 ARRAY_SIZE(ad7124_master_clk_freq_hz),
512 if (fclk != ad7124_master_clk_freq_hz[power_mode]) {
513 ret = clk_set_rate(st->mclk, fclk);
518 /* Set the power mode */
519 st->adc_control &= ~AD7124_ADC_CTRL_PWR_MSK;
520 st->adc_control |= AD7124_ADC_CTRL_PWR(power_mode);
521 ret = ad_sd_write_reg(&st->sd, AD7124_ADC_CONTROL, 2, st->adc_control);
525 for (i = 0; i < st->num_channels; i++) {
526 val = st->channel_config[i].ain | AD7124_CHANNEL_SETUP(i);
527 ret = ad_sd_write_reg(&st->sd, AD7124_CHANNEL(i), 2, val);
531 ret = ad7124_init_channel_vref(st, i);
535 val = AD7124_CONFIG_BIPOLAR(st->channel_config[i].bipolar) |
536 AD7124_CONFIG_REF_SEL(st->channel_config[i].refsel);
537 ret = ad_sd_write_reg(&st->sd, AD7124_CONFIG(i), 2, val);
541 * 9.38 SPS is the minimum output data rate supported
542 * regardless of the selected power mode. Round it up to 10 and
543 * set all the enabled channels to this default value.
545 ret = ad7124_set_channel_odr(st, i, 10);
551 static int ad7124_probe(struct spi_device *spi)
553 const struct spi_device_id *id;
554 struct ad7124_state *st;
555 struct iio_dev *indio_dev;
558 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
562 st = iio_priv(indio_dev);
564 id = spi_get_device_id(spi);
565 st->chip_info = &ad7124_chip_info_tbl[id->driver_data];
567 ad_sd_init(&st->sd, indio_dev, spi, &ad7124_sigma_delta_info);
569 spi_set_drvdata(spi, indio_dev);
571 indio_dev->dev.parent = &spi->dev;
572 indio_dev->name = spi_get_device_id(spi)->name;
573 indio_dev->modes = INDIO_DIRECT_MODE;
574 indio_dev->info = &ad7124_info;
576 ret = ad7124_of_parse_channel_config(indio_dev, spi->dev.of_node);
580 for (i = 0; i < ARRAY_SIZE(st->vref); i++) {
581 if (i == AD7124_INT_REF)
584 st->vref[i] = devm_regulator_get_optional(&spi->dev,
585 ad7124_ref_names[i]);
586 if (PTR_ERR(st->vref[i]) == -ENODEV)
588 else if (IS_ERR(st->vref[i]))
589 return PTR_ERR(st->vref[i]);
591 ret = regulator_enable(st->vref[i]);
596 st->mclk = devm_clk_get(&spi->dev, "mclk");
597 if (IS_ERR(st->mclk)) {
598 ret = PTR_ERR(st->mclk);
599 goto error_regulator_disable;
602 ret = clk_prepare_enable(st->mclk);
604 goto error_regulator_disable;
606 ret = ad7124_soft_reset(st);
608 goto error_clk_disable_unprepare;
610 ret = ad7124_setup(st);
612 goto error_clk_disable_unprepare;
614 ret = ad_sd_setup_buffer_and_trigger(indio_dev);
616 goto error_clk_disable_unprepare;
618 ret = iio_device_register(indio_dev);
620 dev_err(&spi->dev, "Failed to register iio device\n");
621 goto error_remove_trigger;
626 error_remove_trigger:
627 ad_sd_cleanup_buffer_and_trigger(indio_dev);
628 error_clk_disable_unprepare:
629 clk_disable_unprepare(st->mclk);
630 error_regulator_disable:
631 for (i = ARRAY_SIZE(st->vref) - 1; i >= 0; i--) {
632 if (!IS_ERR_OR_NULL(st->vref[i]))
633 regulator_disable(st->vref[i]);
639 static int ad7124_remove(struct spi_device *spi)
641 struct iio_dev *indio_dev = spi_get_drvdata(spi);
642 struct ad7124_state *st = iio_priv(indio_dev);
645 iio_device_unregister(indio_dev);
646 ad_sd_cleanup_buffer_and_trigger(indio_dev);
647 clk_disable_unprepare(st->mclk);
649 for (i = ARRAY_SIZE(st->vref) - 1; i >= 0; i--) {
650 if (!IS_ERR_OR_NULL(st->vref[i]))
651 regulator_disable(st->vref[i]);
657 static const struct spi_device_id ad7124_id_table[] = {
658 { "ad7124-4", ID_AD7124_4 },
659 { "ad7124-8", ID_AD7124_8 },
662 MODULE_DEVICE_TABLE(spi, ad7124_id_table);
664 static const struct of_device_id ad7124_of_match[] = {
665 { .compatible = "adi,ad7124-4" },
666 { .compatible = "adi,ad7124-8" },
669 MODULE_DEVICE_TABLE(of, ad7124_of_match);
671 static struct spi_driver ad71124_driver = {
674 .of_match_table = ad7124_of_match,
676 .probe = ad7124_probe,
677 .remove = ad7124_remove,
678 .id_table = ad7124_id_table,
680 module_spi_driver(ad71124_driver);
682 MODULE_AUTHOR("Stefan Popa <stefan.popa@analog.com>");
683 MODULE_DESCRIPTION("Analog Devices AD7124 SPI driver");
684 MODULE_LICENSE("GPL");