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
:
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_LOW
,
209 #define AD7780_CHANNEL(bits, wordsize) \
210 AD_SD_CHANNEL(1, 0, 0, bits, 32, (wordsize) - (bits))
211 #define AD7170_CHANNEL(bits, wordsize) \
212 AD_SD_CHANNEL_NO_SAMP_FREQ(1, 0, 0, bits, 32, (wordsize) - (bits))
214 static const struct ad7780_chip_info ad7780_chip_info_tbl
[] = {
216 .channel
= AD7170_CHANNEL(12, 24),
217 .pattern
= AD7170_PATTERN_GOOD
,
218 .pattern_mask
= AD7170_PATTERN_MASK
,
222 .channel
= AD7170_CHANNEL(16, 24),
223 .pattern
= AD7170_PATTERN_GOOD
,
224 .pattern_mask
= AD7170_PATTERN_MASK
,
228 .channel
= AD7780_CHANNEL(24, 32),
229 .pattern
= AD7780_PATTERN_GOOD
,
230 .pattern_mask
= AD7780_PATTERN_MASK
,
234 .channel
= AD7780_CHANNEL(20, 32),
235 .pattern
= AD7780_PATTERN_GOOD
,
236 .pattern_mask
= AD7780_PATTERN_MASK
,
241 static const struct iio_info ad7780_info
= {
242 .read_raw
= ad7780_read_raw
,
243 .write_raw
= ad7780_write_raw
,
246 static int ad7780_init_gpios(struct device
*dev
, struct ad7780_state
*st
)
250 st
->powerdown_gpio
= devm_gpiod_get_optional(dev
,
253 if (IS_ERR(st
->powerdown_gpio
)) {
254 ret
= PTR_ERR(st
->powerdown_gpio
);
255 dev_err(dev
, "Failed to request powerdown GPIO: %d\n", ret
);
259 if (!st
->chip_info
->is_ad778x
)
263 st
->gain_gpio
= devm_gpiod_get_optional(dev
,
266 if (IS_ERR(st
->gain_gpio
)) {
267 ret
= PTR_ERR(st
->gain_gpio
);
268 dev_err(dev
, "Failed to request gain GPIO: %d\n", ret
);
272 st
->filter_gpio
= devm_gpiod_get_optional(dev
,
275 if (IS_ERR(st
->filter_gpio
)) {
276 ret
= PTR_ERR(st
->filter_gpio
);
277 dev_err(dev
, "Failed to request filter GPIO: %d\n", ret
);
284 static int ad7780_probe(struct spi_device
*spi
)
286 struct ad7780_state
*st
;
287 struct iio_dev
*indio_dev
;
290 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
294 st
= iio_priv(indio_dev
);
297 ad_sd_init(&st
->sd
, indio_dev
, spi
, &ad7780_sigma_delta_info
);
300 &ad7780_chip_info_tbl
[spi_get_device_id(spi
)->driver_data
];
302 spi_set_drvdata(spi
, indio_dev
);
304 indio_dev
->dev
.parent
= &spi
->dev
;
305 indio_dev
->name
= spi_get_device_id(spi
)->name
;
306 indio_dev
->modes
= INDIO_DIRECT_MODE
;
307 indio_dev
->channels
= &st
->chip_info
->channel
;
308 indio_dev
->num_channels
= 1;
309 indio_dev
->info
= &ad7780_info
;
311 ret
= ad7780_init_gpios(&spi
->dev
, st
);
313 goto error_cleanup_buffer_and_trigger
;
315 st
->reg
= devm_regulator_get(&spi
->dev
, "avdd");
317 return PTR_ERR(st
->reg
);
319 ret
= regulator_enable(st
->reg
);
321 dev_err(&spi
->dev
, "Failed to enable specified AVdd supply\n");
325 ret
= ad_sd_setup_buffer_and_trigger(indio_dev
);
327 goto error_disable_reg
;
329 ret
= iio_device_register(indio_dev
);
331 goto error_cleanup_buffer_and_trigger
;
335 error_cleanup_buffer_and_trigger
:
336 ad_sd_cleanup_buffer_and_trigger(indio_dev
);
338 regulator_disable(st
->reg
);
343 static int ad7780_remove(struct spi_device
*spi
)
345 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
346 struct ad7780_state
*st
= iio_priv(indio_dev
);
348 iio_device_unregister(indio_dev
);
349 ad_sd_cleanup_buffer_and_trigger(indio_dev
);
351 regulator_disable(st
->reg
);
356 static const struct spi_device_id ad7780_id
[] = {
357 {"ad7170", ID_AD7170
},
358 {"ad7171", ID_AD7171
},
359 {"ad7780", ID_AD7780
},
360 {"ad7781", ID_AD7781
},
363 MODULE_DEVICE_TABLE(spi
, ad7780_id
);
365 static struct spi_driver ad7780_driver
= {
369 .probe
= ad7780_probe
,
370 .remove
= ad7780_remove
,
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");