19fb27f80SRenato Lui Geh // SPDX-License-Identifier: GPL-2.0
29fb27f80SRenato Lui Geh /*
39fb27f80SRenato Lui Geh * AD7170/AD7171 and AD7780/AD7781 SPI ADC driver
49fb27f80SRenato Lui Geh *
59fb27f80SRenato Lui Geh * Copyright 2011 Analog Devices Inc.
69fb27f80SRenato Lui Geh * Copyright 2019 Renato Lui Geh
79fb27f80SRenato Lui Geh */
89fb27f80SRenato Lui Geh
99fb27f80SRenato Lui Geh #include <linux/interrupt.h>
109fb27f80SRenato Lui Geh #include <linux/device.h>
119fb27f80SRenato Lui Geh #include <linux/kernel.h>
129fb27f80SRenato Lui Geh #include <linux/slab.h>
139fb27f80SRenato Lui Geh #include <linux/sysfs.h>
149fb27f80SRenato Lui Geh #include <linux/spi/spi.h>
159fb27f80SRenato Lui Geh #include <linux/regulator/consumer.h>
169fb27f80SRenato Lui Geh #include <linux/err.h>
179fb27f80SRenato Lui Geh #include <linux/sched.h>
189fb27f80SRenato Lui Geh #include <linux/gpio/consumer.h>
199fb27f80SRenato Lui Geh #include <linux/module.h>
209fb27f80SRenato Lui Geh #include <linux/bits.h>
219fb27f80SRenato Lui Geh
229fb27f80SRenato Lui Geh #include <linux/iio/iio.h>
239fb27f80SRenato Lui Geh #include <linux/iio/sysfs.h>
249fb27f80SRenato Lui Geh #include <linux/iio/adc/ad_sigma_delta.h>
259fb27f80SRenato Lui Geh
269fb27f80SRenato Lui Geh #define AD7780_RDY BIT(7)
279fb27f80SRenato Lui Geh #define AD7780_FILTER BIT(6)
289fb27f80SRenato Lui Geh #define AD7780_ERR BIT(5)
299fb27f80SRenato Lui Geh #define AD7780_ID1 BIT(4)
309fb27f80SRenato Lui Geh #define AD7780_ID0 BIT(3)
319fb27f80SRenato Lui Geh #define AD7780_GAIN BIT(2)
329fb27f80SRenato Lui Geh
339fb27f80SRenato Lui Geh #define AD7170_ID 0
349fb27f80SRenato Lui Geh #define AD7171_ID 1
359fb27f80SRenato Lui Geh #define AD7780_ID 1
369fb27f80SRenato Lui Geh #define AD7781_ID 0
379fb27f80SRenato Lui Geh
389fb27f80SRenato Lui Geh #define AD7780_ID_MASK (AD7780_ID0 | AD7780_ID1)
399fb27f80SRenato Lui Geh
409fb27f80SRenato Lui Geh #define AD7780_PATTERN_GOOD 1
419fb27f80SRenato Lui Geh #define AD7780_PATTERN_MASK GENMASK(1, 0)
429fb27f80SRenato Lui Geh
439fb27f80SRenato Lui Geh #define AD7170_PATTERN_GOOD 5
449fb27f80SRenato Lui Geh #define AD7170_PATTERN_MASK GENMASK(2, 0)
459fb27f80SRenato Lui Geh
469fb27f80SRenato Lui Geh #define AD7780_GAIN_MIDPOINT 64
479fb27f80SRenato Lui Geh #define AD7780_FILTER_MIDPOINT 13350
489fb27f80SRenato Lui Geh
499fb27f80SRenato Lui Geh static const unsigned int ad778x_gain[2] = { 1, 128 };
509fb27f80SRenato Lui Geh static const unsigned int ad778x_odr_avail[2] = { 10000, 16700 };
519fb27f80SRenato Lui Geh
529fb27f80SRenato Lui Geh struct ad7780_chip_info {
539fb27f80SRenato Lui Geh struct iio_chan_spec channel;
549fb27f80SRenato Lui Geh unsigned int pattern_mask;
559fb27f80SRenato Lui Geh unsigned int pattern;
569fb27f80SRenato Lui Geh bool is_ad778x;
579fb27f80SRenato Lui Geh };
589fb27f80SRenato Lui Geh
599fb27f80SRenato Lui Geh struct ad7780_state {
609fb27f80SRenato Lui Geh const struct ad7780_chip_info *chip_info;
619fb27f80SRenato Lui Geh struct regulator *reg;
629fb27f80SRenato Lui Geh struct gpio_desc *powerdown_gpio;
639fb27f80SRenato Lui Geh struct gpio_desc *gain_gpio;
649fb27f80SRenato Lui Geh struct gpio_desc *filter_gpio;
659fb27f80SRenato Lui Geh unsigned int gain;
669fb27f80SRenato Lui Geh unsigned int odr;
679fb27f80SRenato Lui Geh unsigned int int_vref_mv;
689fb27f80SRenato Lui Geh
699fb27f80SRenato Lui Geh struct ad_sigma_delta sd;
709fb27f80SRenato Lui Geh };
719fb27f80SRenato Lui Geh
729fb27f80SRenato Lui Geh enum ad7780_supported_device_ids {
739fb27f80SRenato Lui Geh ID_AD7170,
749fb27f80SRenato Lui Geh ID_AD7171,
759fb27f80SRenato Lui Geh ID_AD7780,
769fb27f80SRenato Lui Geh ID_AD7781,
779fb27f80SRenato Lui Geh };
789fb27f80SRenato Lui Geh
ad_sigma_delta_to_ad7780(struct ad_sigma_delta * sd)799fb27f80SRenato Lui Geh static struct ad7780_state *ad_sigma_delta_to_ad7780(struct ad_sigma_delta *sd)
809fb27f80SRenato Lui Geh {
819fb27f80SRenato Lui Geh return container_of(sd, struct ad7780_state, sd);
829fb27f80SRenato Lui Geh }
839fb27f80SRenato Lui Geh
ad7780_set_mode(struct ad_sigma_delta * sigma_delta,enum ad_sigma_delta_mode mode)849fb27f80SRenato Lui Geh static int ad7780_set_mode(struct ad_sigma_delta *sigma_delta,
859fb27f80SRenato Lui Geh enum ad_sigma_delta_mode mode)
869fb27f80SRenato Lui Geh {
879fb27f80SRenato Lui Geh struct ad7780_state *st = ad_sigma_delta_to_ad7780(sigma_delta);
889fb27f80SRenato Lui Geh unsigned int val;
899fb27f80SRenato Lui Geh
909fb27f80SRenato Lui Geh switch (mode) {
919fb27f80SRenato Lui Geh case AD_SD_MODE_SINGLE:
929fb27f80SRenato Lui Geh case AD_SD_MODE_CONTINUOUS:
939fb27f80SRenato Lui Geh val = 1;
949fb27f80SRenato Lui Geh break;
959fb27f80SRenato Lui Geh default:
969fb27f80SRenato Lui Geh val = 0;
979fb27f80SRenato Lui Geh break;
989fb27f80SRenato Lui Geh }
999fb27f80SRenato Lui Geh
1009fb27f80SRenato Lui Geh gpiod_set_value(st->powerdown_gpio, val);
1019fb27f80SRenato Lui Geh
1029fb27f80SRenato Lui Geh return 0;
1039fb27f80SRenato Lui Geh }
1049fb27f80SRenato Lui Geh
ad7780_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long m)1059fb27f80SRenato Lui Geh static int ad7780_read_raw(struct iio_dev *indio_dev,
1069fb27f80SRenato Lui Geh struct iio_chan_spec const *chan,
1079fb27f80SRenato Lui Geh int *val,
1089fb27f80SRenato Lui Geh int *val2,
1099fb27f80SRenato Lui Geh long m)
1109fb27f80SRenato Lui Geh {
1119fb27f80SRenato Lui Geh struct ad7780_state *st = iio_priv(indio_dev);
1129fb27f80SRenato Lui Geh int voltage_uv;
1139fb27f80SRenato Lui Geh
1149fb27f80SRenato Lui Geh switch (m) {
1159fb27f80SRenato Lui Geh case IIO_CHAN_INFO_RAW:
1169fb27f80SRenato Lui Geh return ad_sigma_delta_single_conversion(indio_dev, chan, val);
1179fb27f80SRenato Lui Geh case IIO_CHAN_INFO_SCALE:
1189fb27f80SRenato Lui Geh voltage_uv = regulator_get_voltage(st->reg);
1199fb27f80SRenato Lui Geh if (voltage_uv < 0)
1209fb27f80SRenato Lui Geh return voltage_uv;
1219fb27f80SRenato Lui Geh voltage_uv /= 1000;
1229fb27f80SRenato Lui Geh *val = voltage_uv * st->gain;
1239fb27f80SRenato Lui Geh *val2 = chan->scan_type.realbits - 1;
1249fb27f80SRenato Lui Geh st->int_vref_mv = voltage_uv;
1259fb27f80SRenato Lui Geh return IIO_VAL_FRACTIONAL_LOG2;
1269fb27f80SRenato Lui Geh case IIO_CHAN_INFO_OFFSET:
1279fb27f80SRenato Lui Geh *val = -(1 << (chan->scan_type.realbits - 1));
1289fb27f80SRenato Lui Geh return IIO_VAL_INT;
1299fb27f80SRenato Lui Geh case IIO_CHAN_INFO_SAMP_FREQ:
1309fb27f80SRenato Lui Geh *val = st->odr;
1319fb27f80SRenato Lui Geh return IIO_VAL_INT;
1329fb27f80SRenato Lui Geh default:
1339fb27f80SRenato Lui Geh break;
1349fb27f80SRenato Lui Geh }
1359fb27f80SRenato Lui Geh
1369fb27f80SRenato Lui Geh return -EINVAL;
1379fb27f80SRenato Lui Geh }
1389fb27f80SRenato Lui Geh
ad7780_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long m)1399fb27f80SRenato Lui Geh static int ad7780_write_raw(struct iio_dev *indio_dev,
1409fb27f80SRenato Lui Geh struct iio_chan_spec const *chan,
1419fb27f80SRenato Lui Geh int val,
1429fb27f80SRenato Lui Geh int val2,
1439fb27f80SRenato Lui Geh long m)
1449fb27f80SRenato Lui Geh {
1459fb27f80SRenato Lui Geh struct ad7780_state *st = iio_priv(indio_dev);
1469fb27f80SRenato Lui Geh const struct ad7780_chip_info *chip_info = st->chip_info;
1479fb27f80SRenato Lui Geh unsigned long long vref;
1489fb27f80SRenato Lui Geh unsigned int full_scale, gain;
1499fb27f80SRenato Lui Geh
1509fb27f80SRenato Lui Geh if (!chip_info->is_ad778x)
1519fb27f80SRenato Lui Geh return -EINVAL;
1529fb27f80SRenato Lui Geh
1539fb27f80SRenato Lui Geh switch (m) {
1549fb27f80SRenato Lui Geh case IIO_CHAN_INFO_SCALE:
155*7e3a8ea3SZicheng Qu if (val != 0 || val2 == 0)
1569fb27f80SRenato Lui Geh return -EINVAL;
1579fb27f80SRenato Lui Geh
1589fb27f80SRenato Lui Geh vref = st->int_vref_mv * 1000000LL;
1599fb27f80SRenato Lui Geh full_scale = 1 << (chip_info->channel.scan_type.realbits - 1);
1609fb27f80SRenato Lui Geh gain = DIV_ROUND_CLOSEST_ULL(vref, full_scale);
1619fb27f80SRenato Lui Geh gain = DIV_ROUND_CLOSEST(gain, val2);
1629fb27f80SRenato Lui Geh st->gain = gain;
1639fb27f80SRenato Lui Geh if (gain < AD7780_GAIN_MIDPOINT)
1649fb27f80SRenato Lui Geh gain = 0;
1659fb27f80SRenato Lui Geh else
1669fb27f80SRenato Lui Geh gain = 1;
1679fb27f80SRenato Lui Geh gpiod_set_value(st->gain_gpio, gain);
1689fb27f80SRenato Lui Geh break;
1699fb27f80SRenato Lui Geh case IIO_CHAN_INFO_SAMP_FREQ:
1709fb27f80SRenato Lui Geh if (1000*val + val2/1000 < AD7780_FILTER_MIDPOINT)
1719fb27f80SRenato Lui Geh val = 0;
1729fb27f80SRenato Lui Geh else
1739fb27f80SRenato Lui Geh val = 1;
1749fb27f80SRenato Lui Geh st->odr = ad778x_odr_avail[val];
1759fb27f80SRenato Lui Geh gpiod_set_value(st->filter_gpio, val);
1769fb27f80SRenato Lui Geh break;
1779fb27f80SRenato Lui Geh default:
1789fb27f80SRenato Lui Geh break;
1799fb27f80SRenato Lui Geh }
1809fb27f80SRenato Lui Geh
1819fb27f80SRenato Lui Geh return 0;
1829fb27f80SRenato Lui Geh }
1839fb27f80SRenato Lui Geh
ad7780_postprocess_sample(struct ad_sigma_delta * sigma_delta,unsigned int raw_sample)1849fb27f80SRenato Lui Geh static int ad7780_postprocess_sample(struct ad_sigma_delta *sigma_delta,
1859fb27f80SRenato Lui Geh unsigned int raw_sample)
1869fb27f80SRenato Lui Geh {
1879fb27f80SRenato Lui Geh struct ad7780_state *st = ad_sigma_delta_to_ad7780(sigma_delta);
1889fb27f80SRenato Lui Geh const struct ad7780_chip_info *chip_info = st->chip_info;
1899fb27f80SRenato Lui Geh
1909fb27f80SRenato Lui Geh if ((raw_sample & AD7780_ERR) ||
1919fb27f80SRenato Lui Geh ((raw_sample & chip_info->pattern_mask) != chip_info->pattern))
1929fb27f80SRenato Lui Geh return -EIO;
1939fb27f80SRenato Lui Geh
1949fb27f80SRenato Lui Geh if (chip_info->is_ad778x) {
1959fb27f80SRenato Lui Geh st->gain = ad778x_gain[raw_sample & AD7780_GAIN];
1969fb27f80SRenato Lui Geh st->odr = ad778x_odr_avail[raw_sample & AD7780_FILTER];
1979fb27f80SRenato Lui Geh }
1989fb27f80SRenato Lui Geh
1999fb27f80SRenato Lui Geh return 0;
2009fb27f80SRenato Lui Geh }
2019fb27f80SRenato Lui Geh
2029fb27f80SRenato Lui Geh static const struct ad_sigma_delta_info ad7780_sigma_delta_info = {
2039fb27f80SRenato Lui Geh .set_mode = ad7780_set_mode,
2049fb27f80SRenato Lui Geh .postprocess_sample = ad7780_postprocess_sample,
2059fb27f80SRenato Lui Geh .has_registers = false,
206e081102fSAlexandru Tachici .irq_flags = IRQF_TRIGGER_FALLING,
2079fb27f80SRenato Lui Geh };
2089fb27f80SRenato Lui Geh
2095924dab2SAlexandru Ardelean #define _AD7780_CHANNEL(_bits, _wordsize, _mask_all) \
2105924dab2SAlexandru Ardelean { \
2115924dab2SAlexandru Ardelean .type = IIO_VOLTAGE, \
2125924dab2SAlexandru Ardelean .indexed = 1, \
2135924dab2SAlexandru Ardelean .channel = 0, \
2145924dab2SAlexandru Ardelean .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
2155924dab2SAlexandru Ardelean BIT(IIO_CHAN_INFO_OFFSET), \
2165924dab2SAlexandru Ardelean .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
2175924dab2SAlexandru Ardelean .info_mask_shared_by_all = _mask_all, \
2185924dab2SAlexandru Ardelean .scan_index = 1, \
2195924dab2SAlexandru Ardelean .scan_type = { \
2205924dab2SAlexandru Ardelean .sign = 'u', \
2215924dab2SAlexandru Ardelean .realbits = (_bits), \
2225924dab2SAlexandru Ardelean .storagebits = 32, \
2235924dab2SAlexandru Ardelean .shift = (_wordsize) - (_bits), \
2245924dab2SAlexandru Ardelean .endianness = IIO_BE, \
2255924dab2SAlexandru Ardelean }, \
2265924dab2SAlexandru Ardelean }
2275924dab2SAlexandru Ardelean
2285924dab2SAlexandru Ardelean #define AD7780_CHANNEL(_bits, _wordsize) \
2295924dab2SAlexandru Ardelean _AD7780_CHANNEL(_bits, _wordsize, BIT(IIO_CHAN_INFO_SAMP_FREQ))
2305924dab2SAlexandru Ardelean #define AD7170_CHANNEL(_bits, _wordsize) \
2315924dab2SAlexandru Ardelean _AD7780_CHANNEL(_bits, _wordsize, 0)
2329fb27f80SRenato Lui Geh
2339fb27f80SRenato Lui Geh static const struct ad7780_chip_info ad7780_chip_info_tbl[] = {
2349fb27f80SRenato Lui Geh [ID_AD7170] = {
2359fb27f80SRenato Lui Geh .channel = AD7170_CHANNEL(12, 24),
2369fb27f80SRenato Lui Geh .pattern = AD7170_PATTERN_GOOD,
2379fb27f80SRenato Lui Geh .pattern_mask = AD7170_PATTERN_MASK,
2389fb27f80SRenato Lui Geh .is_ad778x = false,
2399fb27f80SRenato Lui Geh },
2409fb27f80SRenato Lui Geh [ID_AD7171] = {
2419fb27f80SRenato Lui Geh .channel = AD7170_CHANNEL(16, 24),
2429fb27f80SRenato Lui Geh .pattern = AD7170_PATTERN_GOOD,
2439fb27f80SRenato Lui Geh .pattern_mask = AD7170_PATTERN_MASK,
2449fb27f80SRenato Lui Geh .is_ad778x = false,
2459fb27f80SRenato Lui Geh },
2469fb27f80SRenato Lui Geh [ID_AD7780] = {
2479fb27f80SRenato Lui Geh .channel = AD7780_CHANNEL(24, 32),
2489fb27f80SRenato Lui Geh .pattern = AD7780_PATTERN_GOOD,
2499fb27f80SRenato Lui Geh .pattern_mask = AD7780_PATTERN_MASK,
2509fb27f80SRenato Lui Geh .is_ad778x = true,
2519fb27f80SRenato Lui Geh },
2529fb27f80SRenato Lui Geh [ID_AD7781] = {
2539fb27f80SRenato Lui Geh .channel = AD7780_CHANNEL(20, 32),
2549fb27f80SRenato Lui Geh .pattern = AD7780_PATTERN_GOOD,
2559fb27f80SRenato Lui Geh .pattern_mask = AD7780_PATTERN_MASK,
2569fb27f80SRenato Lui Geh .is_ad778x = true,
2579fb27f80SRenato Lui Geh },
2589fb27f80SRenato Lui Geh };
2599fb27f80SRenato Lui Geh
2609fb27f80SRenato Lui Geh static const struct iio_info ad7780_info = {
2619fb27f80SRenato Lui Geh .read_raw = ad7780_read_raw,
2629fb27f80SRenato Lui Geh .write_raw = ad7780_write_raw,
2639fb27f80SRenato Lui Geh };
2649fb27f80SRenato Lui Geh
ad7780_init_gpios(struct device * dev,struct ad7780_state * st)2659fb27f80SRenato Lui Geh static int ad7780_init_gpios(struct device *dev, struct ad7780_state *st)
2669fb27f80SRenato Lui Geh {
2679fb27f80SRenato Lui Geh int ret;
2689fb27f80SRenato Lui Geh
2699fb27f80SRenato Lui Geh st->powerdown_gpio = devm_gpiod_get_optional(dev,
2709fb27f80SRenato Lui Geh "powerdown",
2719fb27f80SRenato Lui Geh GPIOD_OUT_LOW);
2729fb27f80SRenato Lui Geh if (IS_ERR(st->powerdown_gpio)) {
2739fb27f80SRenato Lui Geh ret = PTR_ERR(st->powerdown_gpio);
2749fb27f80SRenato Lui Geh dev_err(dev, "Failed to request powerdown GPIO: %d\n", ret);
2759fb27f80SRenato Lui Geh return ret;
2769fb27f80SRenato Lui Geh }
2779fb27f80SRenato Lui Geh
2789fb27f80SRenato Lui Geh if (!st->chip_info->is_ad778x)
2799fb27f80SRenato Lui Geh return 0;
2809fb27f80SRenato Lui Geh
2819fb27f80SRenato Lui Geh
2829fb27f80SRenato Lui Geh st->gain_gpio = devm_gpiod_get_optional(dev,
2839fb27f80SRenato Lui Geh "adi,gain",
2849fb27f80SRenato Lui Geh GPIOD_OUT_HIGH);
2859fb27f80SRenato Lui Geh if (IS_ERR(st->gain_gpio)) {
2869fb27f80SRenato Lui Geh ret = PTR_ERR(st->gain_gpio);
2879fb27f80SRenato Lui Geh dev_err(dev, "Failed to request gain GPIO: %d\n", ret);
2889fb27f80SRenato Lui Geh return ret;
2899fb27f80SRenato Lui Geh }
2909fb27f80SRenato Lui Geh
2919fb27f80SRenato Lui Geh st->filter_gpio = devm_gpiod_get_optional(dev,
2929fb27f80SRenato Lui Geh "adi,filter",
2939fb27f80SRenato Lui Geh GPIOD_OUT_HIGH);
2949fb27f80SRenato Lui Geh if (IS_ERR(st->filter_gpio)) {
2959fb27f80SRenato Lui Geh ret = PTR_ERR(st->filter_gpio);
2969fb27f80SRenato Lui Geh dev_err(dev, "Failed to request filter GPIO: %d\n", ret);
2979fb27f80SRenato Lui Geh return ret;
2989fb27f80SRenato Lui Geh }
2999fb27f80SRenato Lui Geh
3009fb27f80SRenato Lui Geh return 0;
3019fb27f80SRenato Lui Geh }
3029fb27f80SRenato Lui Geh
ad7780_reg_disable(void * reg)303e50aab18SAlexandru Ardelean static void ad7780_reg_disable(void *reg)
304e50aab18SAlexandru Ardelean {
305e50aab18SAlexandru Ardelean regulator_disable(reg);
306e50aab18SAlexandru Ardelean }
307e50aab18SAlexandru Ardelean
ad7780_probe(struct spi_device * spi)3089fb27f80SRenato Lui Geh static int ad7780_probe(struct spi_device *spi)
3099fb27f80SRenato Lui Geh {
3109fb27f80SRenato Lui Geh struct ad7780_state *st;
3119fb27f80SRenato Lui Geh struct iio_dev *indio_dev;
3129fb27f80SRenato Lui Geh int ret;
3139fb27f80SRenato Lui Geh
3149fb27f80SRenato Lui Geh indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
3159fb27f80SRenato Lui Geh if (!indio_dev)
3169fb27f80SRenato Lui Geh return -ENOMEM;
3179fb27f80SRenato Lui Geh
3189fb27f80SRenato Lui Geh st = iio_priv(indio_dev);
3199fb27f80SRenato Lui Geh st->gain = 1;
3209fb27f80SRenato Lui Geh
3219fb27f80SRenato Lui Geh ad_sd_init(&st->sd, indio_dev, spi, &ad7780_sigma_delta_info);
3229fb27f80SRenato Lui Geh
3239fb27f80SRenato Lui Geh st->chip_info =
3249fb27f80SRenato Lui Geh &ad7780_chip_info_tbl[spi_get_device_id(spi)->driver_data];
3259fb27f80SRenato Lui Geh
3269fb27f80SRenato Lui Geh indio_dev->name = spi_get_device_id(spi)->name;
3279fb27f80SRenato Lui Geh indio_dev->modes = INDIO_DIRECT_MODE;
3289fb27f80SRenato Lui Geh indio_dev->channels = &st->chip_info->channel;
3299fb27f80SRenato Lui Geh indio_dev->num_channels = 1;
3309fb27f80SRenato Lui Geh indio_dev->info = &ad7780_info;
3319fb27f80SRenato Lui Geh
3329fb27f80SRenato Lui Geh ret = ad7780_init_gpios(&spi->dev, st);
3339fb27f80SRenato Lui Geh if (ret)
334b0536f98SChristophe JAILLET return ret;
3359fb27f80SRenato Lui Geh
3369fb27f80SRenato Lui Geh st->reg = devm_regulator_get(&spi->dev, "avdd");
3379fb27f80SRenato Lui Geh if (IS_ERR(st->reg))
3389fb27f80SRenato Lui Geh return PTR_ERR(st->reg);
3399fb27f80SRenato Lui Geh
3409fb27f80SRenato Lui Geh ret = regulator_enable(st->reg);
3419fb27f80SRenato Lui Geh if (ret) {
3429fb27f80SRenato Lui Geh dev_err(&spi->dev, "Failed to enable specified AVdd supply\n");
3439fb27f80SRenato Lui Geh return ret;
3449fb27f80SRenato Lui Geh }
3459fb27f80SRenato Lui Geh
346e50aab18SAlexandru Ardelean ret = devm_add_action_or_reset(&spi->dev, ad7780_reg_disable, st->reg);
3479fb27f80SRenato Lui Geh if (ret)
3489fb27f80SRenato Lui Geh return ret;
3499fb27f80SRenato Lui Geh
350e50aab18SAlexandru Ardelean ret = devm_ad_sd_setup_buffer_and_trigger(&spi->dev, indio_dev);
351e50aab18SAlexandru Ardelean if (ret)
352e50aab18SAlexandru Ardelean return ret;
3539fb27f80SRenato Lui Geh
354e50aab18SAlexandru Ardelean return devm_iio_device_register(&spi->dev, indio_dev);
3559fb27f80SRenato Lui Geh }
3569fb27f80SRenato Lui Geh
3579fb27f80SRenato Lui Geh static const struct spi_device_id ad7780_id[] = {
3589fb27f80SRenato Lui Geh {"ad7170", ID_AD7170},
3599fb27f80SRenato Lui Geh {"ad7171", ID_AD7171},
3609fb27f80SRenato Lui Geh {"ad7780", ID_AD7780},
3619fb27f80SRenato Lui Geh {"ad7781", ID_AD7781},
3629fb27f80SRenato Lui Geh {}
3639fb27f80SRenato Lui Geh };
3649fb27f80SRenato Lui Geh MODULE_DEVICE_TABLE(spi, ad7780_id);
3659fb27f80SRenato Lui Geh
3669fb27f80SRenato Lui Geh static struct spi_driver ad7780_driver = {
3679fb27f80SRenato Lui Geh .driver = {
3689fb27f80SRenato Lui Geh .name = "ad7780",
3699fb27f80SRenato Lui Geh },
3709fb27f80SRenato Lui Geh .probe = ad7780_probe,
3719fb27f80SRenato Lui Geh .id_table = ad7780_id,
3729fb27f80SRenato Lui Geh };
3739fb27f80SRenato Lui Geh module_spi_driver(ad7780_driver);
3749fb27f80SRenato Lui Geh
3759fb27f80SRenato Lui Geh MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
3769fb27f80SRenato Lui Geh MODULE_DESCRIPTION("Analog Devices AD7780 and similar ADCs");
3779fb27f80SRenato Lui Geh MODULE_LICENSE("GPL v2");
378ef807729SJonathan Cameron MODULE_IMPORT_NS(IIO_AD_SIGMA_DELTA);
379