1 // SPDX-License-Identifier: GPL-2.0
3 * AD7606 SPI ADC driver
5 * Copyright 2011 Analog Devices Inc.
8 #include <linux/delay.h>
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/sysfs.h>
19 #include <linux/util_macros.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/buffer.h>
23 #include <linux/iio/sysfs.h>
24 #include <linux/iio/trigger.h>
25 #include <linux/iio/triggered_buffer.h>
26 #include <linux/iio/trigger_consumer.h>
31 * Scales are computed as 5000/32768 and 10000/32768 respectively,
32 * so that when applied to the raw values they provide mV values
34 static const unsigned int ad7606_scale_avail[2] = {
38 static const unsigned int ad7606_oversampling_avail[7] = {
39 1, 2, 4, 8, 16, 32, 64,
42 static const unsigned int ad7616_oversampling_avail[8] = {
43 1, 2, 4, 8, 16, 32, 64, 128,
46 static int ad7606_reset(struct ad7606_state *st)
49 gpiod_set_value(st->gpio_reset, 1);
50 ndelay(100); /* t_reset >= 100ns */
51 gpiod_set_value(st->gpio_reset, 0);
58 static int ad7606_read_samples(struct ad7606_state *st)
60 unsigned int num = st->chip_info->num_channels;
65 * The frstdata signal is set to high while and after reading the sample
66 * of the first channel and low for all other channels. This can be used
67 * to check that the incoming data is correctly aligned. During normal
68 * operation the data should never become unaligned, but some glitch or
69 * electrostatic discharge might cause an extra read or clock cycle.
70 * Monitoring the frstdata signal allows to recover from such failure
74 if (st->gpio_frstdata) {
75 ret = st->bops->read_block(st->dev, 1, data);
79 if (!gpiod_get_value(st->gpio_frstdata)) {
88 return st->bops->read_block(st->dev, num, data);
91 static irqreturn_t ad7606_trigger_handler(int irq, void *p)
93 struct iio_poll_func *pf = p;
94 struct iio_dev *indio_dev = pf->indio_dev;
95 struct ad7606_state *st = iio_priv(indio_dev);
98 mutex_lock(&st->lock);
100 ret = ad7606_read_samples(st);
102 iio_push_to_buffers_with_timestamp(indio_dev, st->data,
103 iio_get_time_ns(indio_dev));
105 iio_trigger_notify_done(indio_dev->trig);
106 /* The rising edge of the CONVST signal starts a new conversion. */
107 gpiod_set_value(st->gpio_convst, 1);
109 mutex_unlock(&st->lock);
114 static int ad7606_scan_direct(struct iio_dev *indio_dev, unsigned int ch)
116 struct ad7606_state *st = iio_priv(indio_dev);
119 gpiod_set_value(st->gpio_convst, 1);
120 ret = wait_for_completion_timeout(&st->completion,
121 msecs_to_jiffies(1000));
127 ret = ad7606_read_samples(st);
132 gpiod_set_value(st->gpio_convst, 0);
137 static int ad7606_read_raw(struct iio_dev *indio_dev,
138 struct iio_chan_spec const *chan,
144 struct ad7606_state *st = iio_priv(indio_dev);
147 case IIO_CHAN_INFO_RAW:
148 ret = iio_device_claim_direct_mode(indio_dev);
152 ret = ad7606_scan_direct(indio_dev, chan->address);
153 iio_device_release_direct_mode(indio_dev);
159 case IIO_CHAN_INFO_SCALE:
161 *val2 = st->scale_avail[st->range];
162 return IIO_VAL_INT_PLUS_MICRO;
163 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
164 *val = st->oversampling;
170 static ssize_t ad7606_show_avail(char *buf, const unsigned int *vals,
171 unsigned int n, bool micros)
176 for (i = 0; i < n; i++) {
177 len += scnprintf(buf + len, PAGE_SIZE - len,
178 micros ? "0.%06u " : "%u ", vals[i]);
185 static ssize_t in_voltage_scale_available_show(struct device *dev,
186 struct device_attribute *attr,
189 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
190 struct ad7606_state *st = iio_priv(indio_dev);
192 return ad7606_show_avail(buf, st->scale_avail, st->num_scales, true);
195 static IIO_DEVICE_ATTR_RO(in_voltage_scale_available, 0);
197 static int ad7606_write_raw(struct iio_dev *indio_dev,
198 struct iio_chan_spec const *chan,
203 struct ad7606_state *st = iio_priv(indio_dev);
204 DECLARE_BITMAP(values, 3);
208 case IIO_CHAN_INFO_SCALE:
209 mutex_lock(&st->lock);
210 i = find_closest(val2, st->scale_avail, st->num_scales);
211 gpiod_set_value(st->gpio_range, i);
213 mutex_unlock(&st->lock);
216 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
219 i = find_closest(val, st->oversampling_avail,
224 mutex_lock(&st->lock);
225 gpiod_set_array_value(ARRAY_SIZE(values), st->gpio_os->desc,
226 st->gpio_os->info, values);
228 /* AD7616 requires a reset to update value */
229 if (st->chip_info->os_req_reset)
232 st->oversampling = st->oversampling_avail[i];
233 mutex_unlock(&st->lock);
241 static ssize_t ad7606_oversampling_ratio_avail(struct device *dev,
242 struct device_attribute *attr,
245 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
246 struct ad7606_state *st = iio_priv(indio_dev);
248 return ad7606_show_avail(buf, st->oversampling_avail,
249 st->num_os_ratios, false);
252 static IIO_DEVICE_ATTR(oversampling_ratio_available, 0444,
253 ad7606_oversampling_ratio_avail, NULL, 0);
255 static struct attribute *ad7606_attributes_os_and_range[] = {
256 &iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
257 &iio_dev_attr_oversampling_ratio_available.dev_attr.attr,
261 static const struct attribute_group ad7606_attribute_group_os_and_range = {
262 .attrs = ad7606_attributes_os_and_range,
265 static struct attribute *ad7606_attributes_os[] = {
266 &iio_dev_attr_oversampling_ratio_available.dev_attr.attr,
270 static const struct attribute_group ad7606_attribute_group_os = {
271 .attrs = ad7606_attributes_os,
274 static struct attribute *ad7606_attributes_range[] = {
275 &iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
279 static const struct attribute_group ad7606_attribute_group_range = {
280 .attrs = ad7606_attributes_range,
283 #define AD760X_CHANNEL(num, mask) { \
284 .type = IIO_VOLTAGE, \
288 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
289 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),\
290 .info_mask_shared_by_all = mask, \
296 .endianness = IIO_CPU, \
300 #define AD7605_CHANNEL(num) \
301 AD760X_CHANNEL(num, 0)
303 #define AD7606_CHANNEL(num) \
304 AD760X_CHANNEL(num, BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO))
306 static const struct iio_chan_spec ad7605_channels[] = {
307 IIO_CHAN_SOFT_TIMESTAMP(4),
314 static const struct iio_chan_spec ad7606_channels[] = {
315 IIO_CHAN_SOFT_TIMESTAMP(8),
327 * The current assumption that this driver makes for AD7616, is that it's
328 * working in Hardware Mode with Serial, Burst and Sequencer modes activated.
329 * To activate them, following pins must be pulled high:
332 * And following pins must be pulled low:
336 static const struct iio_chan_spec ad7616_channels[] = {
337 IIO_CHAN_SOFT_TIMESTAMP(16),
356 static const struct ad7606_chip_info ad7606_chip_info_tbl[] = {
357 /* More devices added in future */
359 .channels = ad7605_channels,
363 .channels = ad7606_channels,
365 .oversampling_avail = ad7606_oversampling_avail,
366 .oversampling_num = ARRAY_SIZE(ad7606_oversampling_avail),
369 .channels = ad7606_channels,
371 .oversampling_avail = ad7606_oversampling_avail,
372 .oversampling_num = ARRAY_SIZE(ad7606_oversampling_avail),
375 .channels = ad7606_channels,
377 .oversampling_avail = ad7606_oversampling_avail,
378 .oversampling_num = ARRAY_SIZE(ad7606_oversampling_avail),
381 .channels = ad7616_channels,
383 .oversampling_avail = ad7616_oversampling_avail,
384 .oversampling_num = ARRAY_SIZE(ad7616_oversampling_avail),
385 .os_req_reset = true,
389 static int ad7606_request_gpios(struct ad7606_state *st)
391 struct device *dev = st->dev;
393 st->gpio_convst = devm_gpiod_get(dev, "adi,conversion-start",
395 if (IS_ERR(st->gpio_convst))
396 return PTR_ERR(st->gpio_convst);
398 st->gpio_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
399 if (IS_ERR(st->gpio_reset))
400 return PTR_ERR(st->gpio_reset);
402 st->gpio_range = devm_gpiod_get_optional(dev, "adi,range",
404 if (IS_ERR(st->gpio_range))
405 return PTR_ERR(st->gpio_range);
407 st->gpio_standby = devm_gpiod_get_optional(dev, "standby",
409 if (IS_ERR(st->gpio_standby))
410 return PTR_ERR(st->gpio_standby);
412 st->gpio_frstdata = devm_gpiod_get_optional(dev, "adi,first-data",
414 if (IS_ERR(st->gpio_frstdata))
415 return PTR_ERR(st->gpio_frstdata);
417 if (!st->chip_info->oversampling_num)
420 st->gpio_os = devm_gpiod_get_array_optional(dev,
421 "adi,oversampling-ratio",
423 return PTR_ERR_OR_ZERO(st->gpio_os);
427 * The BUSY signal indicates when conversions are in progress, so when a rising
428 * edge of CONVST is applied, BUSY goes logic high and transitions low at the
429 * end of the entire conversion process. The falling edge of the BUSY signal
430 * triggers this interrupt.
432 static irqreturn_t ad7606_interrupt(int irq, void *dev_id)
434 struct iio_dev *indio_dev = dev_id;
435 struct ad7606_state *st = iio_priv(indio_dev);
437 if (iio_buffer_enabled(indio_dev)) {
438 gpiod_set_value(st->gpio_convst, 0);
439 iio_trigger_poll_chained(st->trig);
441 complete(&st->completion);
447 static int ad7606_validate_trigger(struct iio_dev *indio_dev,
448 struct iio_trigger *trig)
450 struct ad7606_state *st = iio_priv(indio_dev);
452 if (st->trig != trig)
458 static int ad7606_buffer_postenable(struct iio_dev *indio_dev)
460 struct ad7606_state *st = iio_priv(indio_dev);
462 iio_triggered_buffer_postenable(indio_dev);
463 gpiod_set_value(st->gpio_convst, 1);
468 static int ad7606_buffer_predisable(struct iio_dev *indio_dev)
470 struct ad7606_state *st = iio_priv(indio_dev);
472 gpiod_set_value(st->gpio_convst, 0);
474 return iio_triggered_buffer_predisable(indio_dev);
477 static const struct iio_buffer_setup_ops ad7606_buffer_ops = {
478 .postenable = &ad7606_buffer_postenable,
479 .predisable = &ad7606_buffer_predisable,
482 static const struct iio_info ad7606_info_no_os_or_range = {
483 .read_raw = &ad7606_read_raw,
484 .validate_trigger = &ad7606_validate_trigger,
487 static const struct iio_info ad7606_info_os_and_range = {
488 .read_raw = &ad7606_read_raw,
489 .write_raw = &ad7606_write_raw,
490 .attrs = &ad7606_attribute_group_os_and_range,
491 .validate_trigger = &ad7606_validate_trigger,
494 static const struct iio_info ad7606_info_os = {
495 .read_raw = &ad7606_read_raw,
496 .write_raw = &ad7606_write_raw,
497 .attrs = &ad7606_attribute_group_os,
498 .validate_trigger = &ad7606_validate_trigger,
501 static const struct iio_info ad7606_info_range = {
502 .read_raw = &ad7606_read_raw,
503 .write_raw = &ad7606_write_raw,
504 .attrs = &ad7606_attribute_group_range,
505 .validate_trigger = &ad7606_validate_trigger,
508 static const struct iio_trigger_ops ad7606_trigger_ops = {
509 .validate_device = iio_trigger_validate_own_device,
512 static void ad7606_regulator_disable(void *data)
514 struct ad7606_state *st = data;
516 regulator_disable(st->reg);
519 int ad7606_probe(struct device *dev, int irq, void __iomem *base_address,
520 const char *name, unsigned int id,
521 const struct ad7606_bus_ops *bops)
523 struct ad7606_state *st;
525 struct iio_dev *indio_dev;
527 indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
531 st = iio_priv(indio_dev);
532 dev_set_drvdata(dev, indio_dev);
535 mutex_init(&st->lock);
537 st->base_address = base_address;
538 /* tied to logic low, analog input range is +/- 5V */
540 st->oversampling = 1;
541 st->scale_avail = ad7606_scale_avail;
542 st->num_scales = ARRAY_SIZE(ad7606_scale_avail);
544 st->reg = devm_regulator_get(dev, "avcc");
546 return PTR_ERR(st->reg);
548 ret = regulator_enable(st->reg);
550 dev_err(dev, "Failed to enable specified AVcc supply\n");
554 ret = devm_add_action_or_reset(dev, ad7606_regulator_disable, st);
558 st->chip_info = &ad7606_chip_info_tbl[id];
560 if (st->chip_info->oversampling_num) {
561 st->oversampling_avail = st->chip_info->oversampling_avail;
562 st->num_os_ratios = st->chip_info->oversampling_num;
565 ret = ad7606_request_gpios(st);
569 indio_dev->dev.parent = dev;
572 indio_dev->info = &ad7606_info_os_and_range;
574 indio_dev->info = &ad7606_info_os;
577 indio_dev->info = &ad7606_info_range;
579 indio_dev->info = &ad7606_info_no_os_or_range;
581 indio_dev->modes = INDIO_DIRECT_MODE;
582 indio_dev->name = name;
583 indio_dev->channels = st->chip_info->channels;
584 indio_dev->num_channels = st->chip_info->num_channels;
586 init_completion(&st->completion);
588 ret = ad7606_reset(st);
590 dev_warn(st->dev, "failed to RESET: no RESET GPIO specified\n");
592 st->trig = devm_iio_trigger_alloc(dev, "%s-dev%d",
593 indio_dev->name, indio_dev->id);
597 st->trig->ops = &ad7606_trigger_ops;
598 st->trig->dev.parent = dev;
599 iio_trigger_set_drvdata(st->trig, indio_dev);
600 ret = devm_iio_trigger_register(dev, st->trig);
604 indio_dev->trig = iio_trigger_get(st->trig);
606 ret = devm_request_threaded_irq(dev, irq,
609 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
614 ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
615 &iio_pollfunc_store_time,
616 &ad7606_trigger_handler,
621 return devm_iio_device_register(dev, indio_dev);
623 EXPORT_SYMBOL_GPL(ad7606_probe);
625 #ifdef CONFIG_PM_SLEEP
627 static int ad7606_suspend(struct device *dev)
629 struct iio_dev *indio_dev = dev_get_drvdata(dev);
630 struct ad7606_state *st = iio_priv(indio_dev);
632 if (st->gpio_standby) {
633 gpiod_set_value(st->gpio_range, 1);
634 gpiod_set_value(st->gpio_standby, 0);
640 static int ad7606_resume(struct device *dev)
642 struct iio_dev *indio_dev = dev_get_drvdata(dev);
643 struct ad7606_state *st = iio_priv(indio_dev);
645 if (st->gpio_standby) {
646 gpiod_set_value(st->gpio_range, st->range);
647 gpiod_set_value(st->gpio_standby, 1);
654 SIMPLE_DEV_PM_OPS(ad7606_pm_ops, ad7606_suspend, ad7606_resume);
655 EXPORT_SYMBOL_GPL(ad7606_pm_ops);
660 MODULE_DESCRIPTION("Analog Devices AD7606 ADC");
661 MODULE_LICENSE("GPL v2");