1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * AD7170/AD7171 and AD7780/AD7781 SPI ADC driver
4 *
5 * Copyright 2011 Analog Devices Inc.
6 * Copyright 2019 Renato Lui Geh
7 */
8
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>
21
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
24 #include <linux/iio/adc/ad_sigma_delta.h>
25
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)
32
33 #define AD7170_ID 0
34 #define AD7171_ID 1
35 #define AD7780_ID 1
36 #define AD7781_ID 0
37
38 #define AD7780_ID_MASK (AD7780_ID0 | AD7780_ID1)
39
40 #define AD7780_PATTERN_GOOD 1
41 #define AD7780_PATTERN_MASK GENMASK(1, 0)
42
43 #define AD7170_PATTERN_GOOD 5
44 #define AD7170_PATTERN_MASK GENMASK(2, 0)
45
46 #define AD7780_GAIN_MIDPOINT 64
47 #define AD7780_FILTER_MIDPOINT 13350
48
49 static const unsigned int ad778x_gain[2] = { 1, 128 };
50 static const unsigned int ad778x_odr_avail[2] = { 10000, 16700 };
51
52 struct ad7780_chip_info {
53 struct iio_chan_spec channel;
54 unsigned int pattern_mask;
55 unsigned int pattern;
56 bool is_ad778x;
57 };
58
59 struct ad7780_state {
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;
65 unsigned int gain;
66 unsigned int odr;
67 unsigned int int_vref_mv;
68
69 struct ad_sigma_delta sd;
70 };
71
72 enum ad7780_supported_device_ids {
73 ID_AD7170,
74 ID_AD7171,
75 ID_AD7780,
76 ID_AD7781,
77 };
78
ad_sigma_delta_to_ad7780(struct ad_sigma_delta * sd)79 static struct ad7780_state *ad_sigma_delta_to_ad7780(struct ad_sigma_delta *sd)
80 {
81 return container_of(sd, struct ad7780_state, sd);
82 }
83
ad7780_set_mode(struct ad_sigma_delta * sigma_delta,enum ad_sigma_delta_mode mode)84 static int ad7780_set_mode(struct ad_sigma_delta *sigma_delta,
85 enum ad_sigma_delta_mode mode)
86 {
87 struct ad7780_state *st = ad_sigma_delta_to_ad7780(sigma_delta);
88 unsigned int val;
89
90 switch (mode) {
91 case AD_SD_MODE_SINGLE:
92 case AD_SD_MODE_CONTINUOUS:
93 val = 1;
94 break;
95 default:
96 val = 0;
97 break;
98 }
99
100 gpiod_set_value(st->powerdown_gpio, val);
101
102 return 0;
103 }
104
ad7780_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long m)105 static int ad7780_read_raw(struct iio_dev *indio_dev,
106 struct iio_chan_spec const *chan,
107 int *val,
108 int *val2,
109 long m)
110 {
111 struct ad7780_state *st = iio_priv(indio_dev);
112 int voltage_uv;
113
114 switch (m) {
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);
119 if (voltage_uv < 0)
120 return voltage_uv;
121 voltage_uv /= 1000;
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));
128 return IIO_VAL_INT;
129 case IIO_CHAN_INFO_SAMP_FREQ:
130 *val = st->odr;
131 return IIO_VAL_INT;
132 default:
133 break;
134 }
135
136 return -EINVAL;
137 }
138
ad7780_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long m)139 static int ad7780_write_raw(struct iio_dev *indio_dev,
140 struct iio_chan_spec const *chan,
141 int val,
142 int val2,
143 long m)
144 {
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;
149
150 if (!chip_info->is_ad778x)
151 return -EINVAL;
152
153 switch (m) {
154 case IIO_CHAN_INFO_SCALE:
155 if (val != 0 || val2 == 0)
156 return -EINVAL;
157
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);
162 st->gain = gain;
163 if (gain < AD7780_GAIN_MIDPOINT)
164 gain = 0;
165 else
166 gain = 1;
167 gpiod_set_value(st->gain_gpio, gain);
168 break;
169 case IIO_CHAN_INFO_SAMP_FREQ:
170 if (1000*val + val2/1000 < AD7780_FILTER_MIDPOINT)
171 val = 0;
172 else
173 val = 1;
174 st->odr = ad778x_odr_avail[val];
175 gpiod_set_value(st->filter_gpio, val);
176 break;
177 default:
178 break;
179 }
180
181 return 0;
182 }
183
ad7780_postprocess_sample(struct ad_sigma_delta * sigma_delta,unsigned int raw_sample)184 static int ad7780_postprocess_sample(struct ad_sigma_delta *sigma_delta,
185 unsigned int raw_sample)
186 {
187 struct ad7780_state *st = ad_sigma_delta_to_ad7780(sigma_delta);
188 const struct ad7780_chip_info *chip_info = st->chip_info;
189
190 if ((raw_sample & AD7780_ERR) ||
191 ((raw_sample & chip_info->pattern_mask) != chip_info->pattern))
192 return -EIO;
193
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];
197 }
198
199 return 0;
200 }
201
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,
207 };
208
209 #define _AD7780_CHANNEL(_bits, _wordsize, _mask_all) \
210 { \
211 .type = IIO_VOLTAGE, \
212 .indexed = 1, \
213 .channel = 0, \
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, \
218 .scan_index = 1, \
219 .scan_type = { \
220 .sign = 'u', \
221 .realbits = (_bits), \
222 .storagebits = 32, \
223 .shift = (_wordsize) - (_bits), \
224 .endianness = IIO_BE, \
225 }, \
226 }
227
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)
232
233 static const struct ad7780_chip_info ad7780_chip_info_tbl[] = {
234 [ID_AD7170] = {
235 .channel = AD7170_CHANNEL(12, 24),
236 .pattern = AD7170_PATTERN_GOOD,
237 .pattern_mask = AD7170_PATTERN_MASK,
238 .is_ad778x = false,
239 },
240 [ID_AD7171] = {
241 .channel = AD7170_CHANNEL(16, 24),
242 .pattern = AD7170_PATTERN_GOOD,
243 .pattern_mask = AD7170_PATTERN_MASK,
244 .is_ad778x = false,
245 },
246 [ID_AD7780] = {
247 .channel = AD7780_CHANNEL(24, 32),
248 .pattern = AD7780_PATTERN_GOOD,
249 .pattern_mask = AD7780_PATTERN_MASK,
250 .is_ad778x = true,
251 },
252 [ID_AD7781] = {
253 .channel = AD7780_CHANNEL(20, 32),
254 .pattern = AD7780_PATTERN_GOOD,
255 .pattern_mask = AD7780_PATTERN_MASK,
256 .is_ad778x = true,
257 },
258 };
259
260 static const struct iio_info ad7780_info = {
261 .read_raw = ad7780_read_raw,
262 .write_raw = ad7780_write_raw,
263 };
264
ad7780_init_gpios(struct device * dev,struct ad7780_state * st)265 static int ad7780_init_gpios(struct device *dev, struct ad7780_state *st)
266 {
267 int ret;
268
269 st->powerdown_gpio = devm_gpiod_get_optional(dev,
270 "powerdown",
271 GPIOD_OUT_LOW);
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);
275 return ret;
276 }
277
278 if (!st->chip_info->is_ad778x)
279 return 0;
280
281
282 st->gain_gpio = devm_gpiod_get_optional(dev,
283 "adi,gain",
284 GPIOD_OUT_HIGH);
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);
288 return ret;
289 }
290
291 st->filter_gpio = devm_gpiod_get_optional(dev,
292 "adi,filter",
293 GPIOD_OUT_HIGH);
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);
297 return ret;
298 }
299
300 return 0;
301 }
302
ad7780_reg_disable(void * reg)303 static void ad7780_reg_disable(void *reg)
304 {
305 regulator_disable(reg);
306 }
307
ad7780_probe(struct spi_device * spi)308 static int ad7780_probe(struct spi_device *spi)
309 {
310 struct ad7780_state *st;
311 struct iio_dev *indio_dev;
312 int ret;
313
314 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
315 if (!indio_dev)
316 return -ENOMEM;
317
318 st = iio_priv(indio_dev);
319 st->gain = 1;
320
321 ad_sd_init(&st->sd, indio_dev, spi, &ad7780_sigma_delta_info);
322
323 st->chip_info =
324 &ad7780_chip_info_tbl[spi_get_device_id(spi)->driver_data];
325
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;
331
332 ret = ad7780_init_gpios(&spi->dev, st);
333 if (ret)
334 return ret;
335
336 st->reg = devm_regulator_get(&spi->dev, "avdd");
337 if (IS_ERR(st->reg))
338 return PTR_ERR(st->reg);
339
340 ret = regulator_enable(st->reg);
341 if (ret) {
342 dev_err(&spi->dev, "Failed to enable specified AVdd supply\n");
343 return ret;
344 }
345
346 ret = devm_add_action_or_reset(&spi->dev, ad7780_reg_disable, st->reg);
347 if (ret)
348 return ret;
349
350 ret = devm_ad_sd_setup_buffer_and_trigger(&spi->dev, indio_dev);
351 if (ret)
352 return ret;
353
354 return devm_iio_device_register(&spi->dev, indio_dev);
355 }
356
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 },
362 { }
363 };
364 MODULE_DEVICE_TABLE(spi, ad7780_id);
365
366 static struct spi_driver ad7780_driver = {
367 .driver = {
368 .name = "ad7780",
369 },
370 .probe = ad7780_probe,
371 .id_table = ad7780_id,
372 };
373 module_spi_driver(ad7780_driver);
374
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");
379