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/property.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/sched.h>
18 #include <linux/slab.h>
19 #include <linux/sysfs.h>
20 #include <linux/util_macros.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/buffer.h>
24 #include <linux/iio/sysfs.h>
25 #include <linux/iio/trigger.h>
26 #include <linux/iio/triggered_buffer.h>
27 #include <linux/iio/trigger_consumer.h>
32 * Scales are computed as 5000/32768 and 10000/32768 respectively,
33 * so that when applied to the raw values they provide mV values
35 static const unsigned int ad7606_scale_avail[2] = {
40 static const unsigned int ad7616_sw_scale_avail[3] = {
44 static const unsigned int ad7606_oversampling_avail[7] = {
45 1, 2, 4, 8, 16, 32, 64,
48 static const unsigned int ad7616_oversampling_avail[8] = {
49 1, 2, 4, 8, 16, 32, 64, 128,
52 static int ad7606_reset(struct ad7606_state *st)
55 gpiod_set_value(st->gpio_reset, 1);
56 ndelay(100); /* t_reset >= 100ns */
57 gpiod_set_value(st->gpio_reset, 0);
64 static int ad7606_reg_access(struct iio_dev *indio_dev,
66 unsigned int writeval,
67 unsigned int *readval)
69 struct ad7606_state *st = iio_priv(indio_dev);
72 mutex_lock(&st->lock);
74 ret = st->bops->reg_read(st, reg);
80 ret = st->bops->reg_write(st, reg, writeval);
83 mutex_unlock(&st->lock);
87 static int ad7606_read_samples(struct ad7606_state *st)
89 unsigned int num = st->chip_info->num_channels - 1;
94 * The frstdata signal is set to high while and after reading the sample
95 * of the first channel and low for all other channels. This can be used
96 * to check that the incoming data is correctly aligned. During normal
97 * operation the data should never become unaligned, but some glitch or
98 * electrostatic discharge might cause an extra read or clock cycle.
99 * Monitoring the frstdata signal allows to recover from such failure
103 if (st->gpio_frstdata) {
104 ret = st->bops->read_block(st->dev, 1, data);
108 if (!gpiod_get_value(st->gpio_frstdata)) {
117 return st->bops->read_block(st->dev, num, data);
120 static irqreturn_t ad7606_trigger_handler(int irq, void *p)
122 struct iio_poll_func *pf = p;
123 struct iio_dev *indio_dev = pf->indio_dev;
124 struct ad7606_state *st = iio_priv(indio_dev);
127 mutex_lock(&st->lock);
129 ret = ad7606_read_samples(st);
131 iio_push_to_buffers_with_timestamp(indio_dev, st->data,
132 iio_get_time_ns(indio_dev));
134 iio_trigger_notify_done(indio_dev->trig);
135 /* The rising edge of the CONVST signal starts a new conversion. */
136 gpiod_set_value(st->gpio_convst, 1);
138 mutex_unlock(&st->lock);
143 static int ad7606_scan_direct(struct iio_dev *indio_dev, unsigned int ch)
145 struct ad7606_state *st = iio_priv(indio_dev);
148 gpiod_set_value(st->gpio_convst, 1);
149 ret = wait_for_completion_timeout(&st->completion,
150 msecs_to_jiffies(1000));
156 ret = ad7606_read_samples(st);
161 gpiod_set_value(st->gpio_convst, 0);
166 static int ad7606_read_raw(struct iio_dev *indio_dev,
167 struct iio_chan_spec const *chan,
173 struct ad7606_state *st = iio_priv(indio_dev);
176 case IIO_CHAN_INFO_RAW:
177 ret = iio_device_claim_direct_mode(indio_dev);
181 ret = ad7606_scan_direct(indio_dev, chan->address);
182 iio_device_release_direct_mode(indio_dev);
188 case IIO_CHAN_INFO_SCALE:
192 *val2 = st->scale_avail[st->range[ch]];
193 return IIO_VAL_INT_PLUS_MICRO;
194 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
195 *val = st->oversampling;
201 static ssize_t ad7606_show_avail(char *buf, const unsigned int *vals,
202 unsigned int n, bool micros)
207 for (i = 0; i < n; i++) {
208 len += scnprintf(buf + len, PAGE_SIZE - len,
209 micros ? "0.%06u " : "%u ", vals[i]);
216 static ssize_t in_voltage_scale_available_show(struct device *dev,
217 struct device_attribute *attr,
220 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
221 struct ad7606_state *st = iio_priv(indio_dev);
223 return ad7606_show_avail(buf, st->scale_avail, st->num_scales, true);
226 static IIO_DEVICE_ATTR_RO(in_voltage_scale_available, 0);
228 static int ad7606_write_scale_hw(struct iio_dev *indio_dev, int ch, int val)
230 struct ad7606_state *st = iio_priv(indio_dev);
232 gpiod_set_value(st->gpio_range, val);
237 static int ad7606_write_os_hw(struct iio_dev *indio_dev, int val)
239 struct ad7606_state *st = iio_priv(indio_dev);
240 DECLARE_BITMAP(values, 3);
244 gpiod_set_array_value(ARRAY_SIZE(values), st->gpio_os->desc,
245 st->gpio_os->info, values);
247 /* AD7616 requires a reset to update value */
248 if (st->chip_info->os_req_reset)
254 static int ad7606_write_raw(struct iio_dev *indio_dev,
255 struct iio_chan_spec const *chan,
260 struct ad7606_state *st = iio_priv(indio_dev);
264 case IIO_CHAN_INFO_SCALE:
265 mutex_lock(&st->lock);
266 i = find_closest(val2, st->scale_avail, st->num_scales);
269 ret = st->write_scale(indio_dev, ch, i);
271 mutex_unlock(&st->lock);
275 mutex_unlock(&st->lock);
278 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
281 i = find_closest(val, st->oversampling_avail,
283 mutex_lock(&st->lock);
284 ret = st->write_os(indio_dev, i);
286 mutex_unlock(&st->lock);
289 st->oversampling = st->oversampling_avail[i];
290 mutex_unlock(&st->lock);
298 static ssize_t ad7606_oversampling_ratio_avail(struct device *dev,
299 struct device_attribute *attr,
302 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
303 struct ad7606_state *st = iio_priv(indio_dev);
305 return ad7606_show_avail(buf, st->oversampling_avail,
306 st->num_os_ratios, false);
309 static IIO_DEVICE_ATTR(oversampling_ratio_available, 0444,
310 ad7606_oversampling_ratio_avail, NULL, 0);
312 static struct attribute *ad7606_attributes_os_and_range[] = {
313 &iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
314 &iio_dev_attr_oversampling_ratio_available.dev_attr.attr,
318 static const struct attribute_group ad7606_attribute_group_os_and_range = {
319 .attrs = ad7606_attributes_os_and_range,
322 static struct attribute *ad7606_attributes_os[] = {
323 &iio_dev_attr_oversampling_ratio_available.dev_attr.attr,
327 static const struct attribute_group ad7606_attribute_group_os = {
328 .attrs = ad7606_attributes_os,
331 static struct attribute *ad7606_attributes_range[] = {
332 &iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
336 static const struct attribute_group ad7606_attribute_group_range = {
337 .attrs = ad7606_attributes_range,
340 static const struct iio_chan_spec ad7605_channels[] = {
341 IIO_CHAN_SOFT_TIMESTAMP(4),
348 static const struct iio_chan_spec ad7606_channels[] = {
349 IIO_CHAN_SOFT_TIMESTAMP(8),
361 * The current assumption that this driver makes for AD7616, is that it's
362 * working in Hardware Mode with Serial, Burst and Sequencer modes activated.
363 * To activate them, following pins must be pulled high:
366 * And following pins must be pulled low:
370 static const struct iio_chan_spec ad7616_channels[] = {
371 IIO_CHAN_SOFT_TIMESTAMP(16),
390 static const struct ad7606_chip_info ad7606_chip_info_tbl[] = {
391 /* More devices added in future */
393 .channels = ad7605_channels,
397 .channels = ad7606_channels,
399 .oversampling_avail = ad7606_oversampling_avail,
400 .oversampling_num = ARRAY_SIZE(ad7606_oversampling_avail),
403 .channels = ad7606_channels,
405 .oversampling_avail = ad7606_oversampling_avail,
406 .oversampling_num = ARRAY_SIZE(ad7606_oversampling_avail),
409 .channels = ad7606_channels,
411 .oversampling_avail = ad7606_oversampling_avail,
412 .oversampling_num = ARRAY_SIZE(ad7606_oversampling_avail),
415 .channels = ad7606_channels,
417 .oversampling_avail = ad7606_oversampling_avail,
418 .oversampling_num = ARRAY_SIZE(ad7606_oversampling_avail),
421 .channels = ad7616_channels,
423 .oversampling_avail = ad7616_oversampling_avail,
424 .oversampling_num = ARRAY_SIZE(ad7616_oversampling_avail),
425 .os_req_reset = true,
430 static int ad7606_request_gpios(struct ad7606_state *st)
432 struct device *dev = st->dev;
434 st->gpio_convst = devm_gpiod_get(dev, "adi,conversion-start",
436 if (IS_ERR(st->gpio_convst))
437 return PTR_ERR(st->gpio_convst);
439 st->gpio_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
440 if (IS_ERR(st->gpio_reset))
441 return PTR_ERR(st->gpio_reset);
443 st->gpio_range = devm_gpiod_get_optional(dev, "adi,range",
445 if (IS_ERR(st->gpio_range))
446 return PTR_ERR(st->gpio_range);
448 st->gpio_standby = devm_gpiod_get_optional(dev, "standby",
450 if (IS_ERR(st->gpio_standby))
451 return PTR_ERR(st->gpio_standby);
453 st->gpio_frstdata = devm_gpiod_get_optional(dev, "adi,first-data",
455 if (IS_ERR(st->gpio_frstdata))
456 return PTR_ERR(st->gpio_frstdata);
458 if (!st->chip_info->oversampling_num)
461 st->gpio_os = devm_gpiod_get_array_optional(dev,
462 "adi,oversampling-ratio",
464 return PTR_ERR_OR_ZERO(st->gpio_os);
468 * The BUSY signal indicates when conversions are in progress, so when a rising
469 * edge of CONVST is applied, BUSY goes logic high and transitions low at the
470 * end of the entire conversion process. The falling edge of the BUSY signal
471 * triggers this interrupt.
473 static irqreturn_t ad7606_interrupt(int irq, void *dev_id)
475 struct iio_dev *indio_dev = dev_id;
476 struct ad7606_state *st = iio_priv(indio_dev);
478 if (iio_buffer_enabled(indio_dev)) {
479 gpiod_set_value(st->gpio_convst, 0);
480 iio_trigger_poll_nested(st->trig);
482 complete(&st->completion);
488 static int ad7606_validate_trigger(struct iio_dev *indio_dev,
489 struct iio_trigger *trig)
491 struct ad7606_state *st = iio_priv(indio_dev);
493 if (st->trig != trig)
499 static int ad7606_buffer_postenable(struct iio_dev *indio_dev)
501 struct ad7606_state *st = iio_priv(indio_dev);
503 gpiod_set_value(st->gpio_convst, 1);
508 static int ad7606_buffer_predisable(struct iio_dev *indio_dev)
510 struct ad7606_state *st = iio_priv(indio_dev);
512 gpiod_set_value(st->gpio_convst, 0);
517 static const struct iio_buffer_setup_ops ad7606_buffer_ops = {
518 .postenable = &ad7606_buffer_postenable,
519 .predisable = &ad7606_buffer_predisable,
522 static const struct iio_info ad7606_info_no_os_or_range = {
523 .read_raw = &ad7606_read_raw,
524 .validate_trigger = &ad7606_validate_trigger,
527 static const struct iio_info ad7606_info_os_and_range = {
528 .read_raw = &ad7606_read_raw,
529 .write_raw = &ad7606_write_raw,
530 .attrs = &ad7606_attribute_group_os_and_range,
531 .validate_trigger = &ad7606_validate_trigger,
534 static const struct iio_info ad7606_info_os_range_and_debug = {
535 .read_raw = &ad7606_read_raw,
536 .write_raw = &ad7606_write_raw,
537 .debugfs_reg_access = &ad7606_reg_access,
538 .attrs = &ad7606_attribute_group_os_and_range,
539 .validate_trigger = &ad7606_validate_trigger,
542 static const struct iio_info ad7606_info_os = {
543 .read_raw = &ad7606_read_raw,
544 .write_raw = &ad7606_write_raw,
545 .attrs = &ad7606_attribute_group_os,
546 .validate_trigger = &ad7606_validate_trigger,
549 static const struct iio_info ad7606_info_range = {
550 .read_raw = &ad7606_read_raw,
551 .write_raw = &ad7606_write_raw,
552 .attrs = &ad7606_attribute_group_range,
553 .validate_trigger = &ad7606_validate_trigger,
556 static const struct iio_trigger_ops ad7606_trigger_ops = {
557 .validate_device = iio_trigger_validate_own_device,
560 int ad7606_probe(struct device *dev, int irq, void __iomem *base_address,
561 const char *name, unsigned int id,
562 const struct ad7606_bus_ops *bops)
564 struct ad7606_state *st;
566 struct iio_dev *indio_dev;
568 indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
572 st = iio_priv(indio_dev);
573 dev_set_drvdata(dev, indio_dev);
576 mutex_init(&st->lock);
578 st->base_address = base_address;
579 /* tied to logic low, analog input range is +/- 5V */
581 st->oversampling = 1;
582 st->scale_avail = ad7606_scale_avail;
583 st->num_scales = ARRAY_SIZE(ad7606_scale_avail);
585 ret = devm_regulator_get_enable(dev, "avcc");
587 return dev_err_probe(dev, ret,
588 "Failed to enable specified AVcc supply\n");
590 st->chip_info = &ad7606_chip_info_tbl[id];
592 if (st->chip_info->oversampling_num) {
593 st->oversampling_avail = st->chip_info->oversampling_avail;
594 st->num_os_ratios = st->chip_info->oversampling_num;
597 ret = ad7606_request_gpios(st);
603 indio_dev->info = &ad7606_info_os_and_range;
605 indio_dev->info = &ad7606_info_os;
608 indio_dev->info = &ad7606_info_range;
610 indio_dev->info = &ad7606_info_no_os_or_range;
612 indio_dev->modes = INDIO_DIRECT_MODE;
613 indio_dev->name = name;
614 indio_dev->channels = st->chip_info->channels;
615 indio_dev->num_channels = st->chip_info->num_channels;
617 init_completion(&st->completion);
619 ret = ad7606_reset(st);
621 dev_warn(st->dev, "failed to RESET: no RESET GPIO specified\n");
623 /* AD7616 requires al least 15ms to reconfigure after a reset */
624 if (st->chip_info->init_delay_ms) {
625 if (msleep_interruptible(st->chip_info->init_delay_ms))
629 st->write_scale = ad7606_write_scale_hw;
630 st->write_os = ad7606_write_os_hw;
632 if (st->bops->sw_mode_config)
633 st->sw_mode_en = device_property_present(st->dev,
636 if (st->sw_mode_en) {
637 /* Scale of 0.076293 is only available in sw mode */
638 st->scale_avail = ad7616_sw_scale_avail;
639 st->num_scales = ARRAY_SIZE(ad7616_sw_scale_avail);
641 /* After reset, in software mode, ±10 V is set by default */
642 memset32(st->range, 2, ARRAY_SIZE(st->range));
643 indio_dev->info = &ad7606_info_os_range_and_debug;
645 ret = st->bops->sw_mode_config(indio_dev);
650 st->trig = devm_iio_trigger_alloc(dev, "%s-dev%d",
652 iio_device_id(indio_dev));
656 st->trig->ops = &ad7606_trigger_ops;
657 iio_trigger_set_drvdata(st->trig, indio_dev);
658 ret = devm_iio_trigger_register(dev, st->trig);
662 indio_dev->trig = iio_trigger_get(st->trig);
664 ret = devm_request_threaded_irq(dev, irq,
667 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
672 ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
673 &iio_pollfunc_store_time,
674 &ad7606_trigger_handler,
679 return devm_iio_device_register(dev, indio_dev);
681 EXPORT_SYMBOL_NS_GPL(ad7606_probe, IIO_AD7606);
683 #ifdef CONFIG_PM_SLEEP
685 static int ad7606_suspend(struct device *dev)
687 struct iio_dev *indio_dev = dev_get_drvdata(dev);
688 struct ad7606_state *st = iio_priv(indio_dev);
690 if (st->gpio_standby) {
691 gpiod_set_value(st->gpio_range, 1);
692 gpiod_set_value(st->gpio_standby, 0);
698 static int ad7606_resume(struct device *dev)
700 struct iio_dev *indio_dev = dev_get_drvdata(dev);
701 struct ad7606_state *st = iio_priv(indio_dev);
703 if (st->gpio_standby) {
704 gpiod_set_value(st->gpio_range, st->range[0]);
705 gpiod_set_value(st->gpio_standby, 1);
712 SIMPLE_DEV_PM_OPS(ad7606_pm_ops, ad7606_suspend, ad7606_resume);
713 EXPORT_SYMBOL_NS_GPL(ad7606_pm_ops, IIO_AD7606);
718 MODULE_DESCRIPTION("Analog Devices AD7606 ADC");
719 MODULE_LICENSE("GPL v2");