1 // SPDX-License-Identifier: GPL-2.0
3 * AD7170/AD7171 and AD7780/AD7781 SPI ADC driver
5 * Copyright 2011 Analog Devices Inc.
6 * Copyright 2019 Renato Lui Geh
9 #include <linux/interrupt.h>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/sysfs.h>
14 #include <linux/spi/spi.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/err.h>
17 #include <linux/sched.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/module.h>
20 #include <linux/bits.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
24 #include <linux/iio/adc/ad_sigma_delta.h>
26 #define AD7780_RDY BIT(7)
27 #define AD7780_FILTER BIT(6)
28 #define AD7780_ERR BIT(5)
29 #define AD7780_ID1 BIT(4)
30 #define AD7780_ID0 BIT(3)
31 #define AD7780_GAIN BIT(2)
38 #define AD7780_ID_MASK (AD7780_ID0 | AD7780_ID1)
40 #define AD7780_PATTERN_GOOD 1
41 #define AD7780_PATTERN_MASK GENMASK(1, 0)
43 #define AD7170_PATTERN_GOOD 5
44 #define AD7170_PATTERN_MASK GENMASK(2, 0)
46 #define AD7780_GAIN_MIDPOINT 64
47 #define AD7780_FILTER_MIDPOINT 13350
49 static const unsigned int ad778x_gain
[2] = { 1, 128 };
50 static const unsigned int ad778x_odr_avail
[2] = { 10000, 16700 };
52 struct ad7780_chip_info
{
53 struct iio_chan_spec channel
;
54 unsigned int pattern_mask
;
60 const struct ad7780_chip_info
*chip_info
;
61 struct regulator
*reg
;
62 struct gpio_desc
*powerdown_gpio
;
63 struct gpio_desc
*gain_gpio
;
64 struct gpio_desc
*filter_gpio
;
67 unsigned int int_vref_mv
;
69 struct ad_sigma_delta sd
;
72 enum ad7780_supported_device_ids
{
79 static struct ad7780_state
*ad_sigma_delta_to_ad7780(struct ad_sigma_delta
*sd
)
81 return container_of(sd
, struct ad7780_state
, sd
);
84 static int ad7780_set_mode(struct ad_sigma_delta
*sigma_delta
,
85 enum ad_sigma_delta_mode mode
)
87 struct ad7780_state
*st
= ad_sigma_delta_to_ad7780(sigma_delta
);
91 case AD_SD_MODE_SINGLE
:
92 case AD_SD_MODE_CONTINUOUS
:
100 gpiod_set_value(st
->powerdown_gpio
, val
);
105 static int ad7780_read_raw(struct iio_dev
*indio_dev
,
106 struct iio_chan_spec
const *chan
,
111 struct ad7780_state
*st
= iio_priv(indio_dev
);
115 case IIO_CHAN_INFO_RAW
:
116 return ad_sigma_delta_single_conversion(indio_dev
, chan
, val
);
117 case IIO_CHAN_INFO_SCALE
:
118 voltage_uv
= regulator_get_voltage(st
->reg
);
122 *val
= voltage_uv
* st
->gain
;
123 *val2
= chan
->scan_type
.realbits
- 1;
124 st
->int_vref_mv
= voltage_uv
;
125 return IIO_VAL_FRACTIONAL_LOG2
;
126 case IIO_CHAN_INFO_OFFSET
:
127 *val
= -(1 << (chan
->scan_type
.realbits
- 1));
129 case IIO_CHAN_INFO_SAMP_FREQ
:
139 static int ad7780_write_raw(struct iio_dev
*indio_dev
,
140 struct iio_chan_spec
const *chan
,
145 struct ad7780_state
*st
= iio_priv(indio_dev
);
146 const struct ad7780_chip_info
*chip_info
= st
->chip_info
;
147 unsigned long long vref
;
148 unsigned int full_scale
, gain
;
150 if (!chip_info
->is_ad778x
)
154 case IIO_CHAN_INFO_SCALE
:
155 if (val
!= 0 || val2
== 0)
158 vref
= st
->int_vref_mv
* 1000000LL;
159 full_scale
= 1 << (chip_info
->channel
.scan_type
.realbits
- 1);
160 gain
= DIV_ROUND_CLOSEST_ULL(vref
, full_scale
);
161 gain
= DIV_ROUND_CLOSEST(gain
, val2
);
163 if (gain
< AD7780_GAIN_MIDPOINT
)
167 gpiod_set_value(st
->gain_gpio
, gain
);
169 case IIO_CHAN_INFO_SAMP_FREQ
:
170 if (1000*val
+ val2
/1000 < AD7780_FILTER_MIDPOINT
)
174 st
->odr
= ad778x_odr_avail
[val
];
175 gpiod_set_value(st
->filter_gpio
, val
);
184 static int ad7780_postprocess_sample(struct ad_sigma_delta
*sigma_delta
,
185 unsigned int raw_sample
)
187 struct ad7780_state
*st
= ad_sigma_delta_to_ad7780(sigma_delta
);
188 const struct ad7780_chip_info
*chip_info
= st
->chip_info
;
190 if ((raw_sample
& AD7780_ERR
) ||
191 ((raw_sample
& chip_info
->pattern_mask
) != chip_info
->pattern
))
194 if (chip_info
->is_ad778x
) {
195 st
->gain
= ad778x_gain
[raw_sample
& AD7780_GAIN
];
196 st
->odr
= ad778x_odr_avail
[raw_sample
& AD7780_FILTER
];
202 static const struct ad_sigma_delta_info ad7780_sigma_delta_info
= {
203 .set_mode
= ad7780_set_mode
,
204 .postprocess_sample
= ad7780_postprocess_sample
,
205 .has_registers
= false,
206 .irq_flags
= IRQF_TRIGGER_FALLING
,
209 #define _AD7780_CHANNEL(_bits, _wordsize, _mask_all) \
211 .type = IIO_VOLTAGE, \
214 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
215 BIT(IIO_CHAN_INFO_OFFSET), \
216 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
217 .info_mask_shared_by_all = _mask_all, \
221 .realbits = (_bits), \
223 .shift = (_wordsize) - (_bits), \
224 .endianness = IIO_BE, \
228 #define AD7780_CHANNEL(_bits, _wordsize) \
229 _AD7780_CHANNEL(_bits, _wordsize, BIT(IIO_CHAN_INFO_SAMP_FREQ))
230 #define AD7170_CHANNEL(_bits, _wordsize) \
231 _AD7780_CHANNEL(_bits, _wordsize, 0)
233 static const struct ad7780_chip_info ad7780_chip_info_tbl
[] = {
235 .channel
= AD7170_CHANNEL(12, 24),
236 .pattern
= AD7170_PATTERN_GOOD
,
237 .pattern_mask
= AD7170_PATTERN_MASK
,
241 .channel
= AD7170_CHANNEL(16, 24),
242 .pattern
= AD7170_PATTERN_GOOD
,
243 .pattern_mask
= AD7170_PATTERN_MASK
,
247 .channel
= AD7780_CHANNEL(24, 32),
248 .pattern
= AD7780_PATTERN_GOOD
,
249 .pattern_mask
= AD7780_PATTERN_MASK
,
253 .channel
= AD7780_CHANNEL(20, 32),
254 .pattern
= AD7780_PATTERN_GOOD
,
255 .pattern_mask
= AD7780_PATTERN_MASK
,
260 static const struct iio_info ad7780_info
= {
261 .read_raw
= ad7780_read_raw
,
262 .write_raw
= ad7780_write_raw
,
265 static int ad7780_init_gpios(struct device
*dev
, struct ad7780_state
*st
)
269 st
->powerdown_gpio
= devm_gpiod_get_optional(dev
,
272 if (IS_ERR(st
->powerdown_gpio
)) {
273 ret
= PTR_ERR(st
->powerdown_gpio
);
274 dev_err(dev
, "Failed to request powerdown GPIO: %d\n", ret
);
278 if (!st
->chip_info
->is_ad778x
)
282 st
->gain_gpio
= devm_gpiod_get_optional(dev
,
285 if (IS_ERR(st
->gain_gpio
)) {
286 ret
= PTR_ERR(st
->gain_gpio
);
287 dev_err(dev
, "Failed to request gain GPIO: %d\n", ret
);
291 st
->filter_gpio
= devm_gpiod_get_optional(dev
,
294 if (IS_ERR(st
->filter_gpio
)) {
295 ret
= PTR_ERR(st
->filter_gpio
);
296 dev_err(dev
, "Failed to request filter GPIO: %d\n", ret
);
303 static void ad7780_reg_disable(void *reg
)
305 regulator_disable(reg
);
308 static int ad7780_probe(struct spi_device
*spi
)
310 struct ad7780_state
*st
;
311 struct iio_dev
*indio_dev
;
314 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
318 st
= iio_priv(indio_dev
);
321 ad_sd_init(&st
->sd
, indio_dev
, spi
, &ad7780_sigma_delta_info
);
324 &ad7780_chip_info_tbl
[spi_get_device_id(spi
)->driver_data
];
326 indio_dev
->name
= spi_get_device_id(spi
)->name
;
327 indio_dev
->modes
= INDIO_DIRECT_MODE
;
328 indio_dev
->channels
= &st
->chip_info
->channel
;
329 indio_dev
->num_channels
= 1;
330 indio_dev
->info
= &ad7780_info
;
332 ret
= ad7780_init_gpios(&spi
->dev
, st
);
336 st
->reg
= devm_regulator_get(&spi
->dev
, "avdd");
338 return PTR_ERR(st
->reg
);
340 ret
= regulator_enable(st
->reg
);
342 dev_err(&spi
->dev
, "Failed to enable specified AVdd supply\n");
346 ret
= devm_add_action_or_reset(&spi
->dev
, ad7780_reg_disable
, st
->reg
);
350 ret
= devm_ad_sd_setup_buffer_and_trigger(&spi
->dev
, indio_dev
);
354 return devm_iio_device_register(&spi
->dev
, indio_dev
);
357 static const struct spi_device_id ad7780_id
[] = {
358 { "ad7170", ID_AD7170
},
359 { "ad7171", ID_AD7171
},
360 { "ad7780", ID_AD7780
},
361 { "ad7781", ID_AD7781
},
364 MODULE_DEVICE_TABLE(spi
, ad7780_id
);
366 static struct spi_driver ad7780_driver
= {
370 .probe
= ad7780_probe
,
371 .id_table
= ad7780_id
,
373 module_spi_driver(ad7780_driver
);
375 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
376 MODULE_DESCRIPTION("Analog Devices AD7780 and similar ADCs");
377 MODULE_LICENSE("GPL v2");
378 MODULE_IMPORT_NS("IIO_AD_SIGMA_DELTA");