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:
163 *val2 = st->scale_avail[st->range[ch]];
164 return IIO_VAL_INT_PLUS_MICRO;
165 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
166 *val = st->oversampling;
172 static ssize_t ad7606_show_avail(char *buf, const unsigned int *vals,
173 unsigned int n, bool micros)
178 for (i = 0; i < n; i++) {
179 len += scnprintf(buf + len, PAGE_SIZE - len,
180 micros ? "0.%06u " : "%u ", vals[i]);
187 static ssize_t in_voltage_scale_available_show(struct device *dev,
188 struct device_attribute *attr,
191 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
192 struct ad7606_state *st = iio_priv(indio_dev);
194 return ad7606_show_avail(buf, st->scale_avail, st->num_scales, true);
197 static IIO_DEVICE_ATTR_RO(in_voltage_scale_available, 0);
199 static int ad7606_write_scale_hw(struct iio_dev *indio_dev, int ch, int val)
201 struct ad7606_state *st = iio_priv(indio_dev);
203 gpiod_set_value(st->gpio_range, val);
208 static int ad7606_write_os_hw(struct iio_dev *indio_dev, int val)
210 struct ad7606_state *st = iio_priv(indio_dev);
211 DECLARE_BITMAP(values, 3);
215 gpiod_set_array_value(ARRAY_SIZE(values), st->gpio_os->desc,
216 st->gpio_os->info, values);
218 /* AD7616 requires a reset to update value */
219 if (st->chip_info->os_req_reset)
225 static int ad7606_write_raw(struct iio_dev *indio_dev,
226 struct iio_chan_spec const *chan,
231 struct ad7606_state *st = iio_priv(indio_dev);
235 case IIO_CHAN_INFO_SCALE:
236 mutex_lock(&st->lock);
237 i = find_closest(val2, st->scale_avail, st->num_scales);
240 ret = st->write_scale(indio_dev, ch, i);
242 mutex_unlock(&st->lock);
246 mutex_unlock(&st->lock);
249 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
252 i = find_closest(val, st->oversampling_avail,
254 mutex_lock(&st->lock);
255 ret = st->write_os(indio_dev, i);
257 mutex_unlock(&st->lock);
260 st->oversampling = st->oversampling_avail[i];
261 mutex_unlock(&st->lock);
269 static ssize_t ad7606_oversampling_ratio_avail(struct device *dev,
270 struct device_attribute *attr,
273 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
274 struct ad7606_state *st = iio_priv(indio_dev);
276 return ad7606_show_avail(buf, st->oversampling_avail,
277 st->num_os_ratios, false);
280 static IIO_DEVICE_ATTR(oversampling_ratio_available, 0444,
281 ad7606_oversampling_ratio_avail, NULL, 0);
283 static struct attribute *ad7606_attributes_os_and_range[] = {
284 &iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
285 &iio_dev_attr_oversampling_ratio_available.dev_attr.attr,
289 static const struct attribute_group ad7606_attribute_group_os_and_range = {
290 .attrs = ad7606_attributes_os_and_range,
293 static struct attribute *ad7606_attributes_os[] = {
294 &iio_dev_attr_oversampling_ratio_available.dev_attr.attr,
298 static const struct attribute_group ad7606_attribute_group_os = {
299 .attrs = ad7606_attributes_os,
302 static struct attribute *ad7606_attributes_range[] = {
303 &iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
307 static const struct attribute_group ad7606_attribute_group_range = {
308 .attrs = ad7606_attributes_range,
311 #define AD760X_CHANNEL(num, mask) { \
312 .type = IIO_VOLTAGE, \
316 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
317 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),\
318 .info_mask_shared_by_all = mask, \
324 .endianness = IIO_CPU, \
328 #define AD7605_CHANNEL(num) \
329 AD760X_CHANNEL(num, 0)
331 #define AD7606_CHANNEL(num) \
332 AD760X_CHANNEL(num, BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO))
334 static const struct iio_chan_spec ad7605_channels[] = {
335 IIO_CHAN_SOFT_TIMESTAMP(4),
342 static const struct iio_chan_spec ad7606_channels[] = {
343 IIO_CHAN_SOFT_TIMESTAMP(8),
355 * The current assumption that this driver makes for AD7616, is that it's
356 * working in Hardware Mode with Serial, Burst and Sequencer modes activated.
357 * To activate them, following pins must be pulled high:
360 * And following pins must be pulled low:
364 static const struct iio_chan_spec ad7616_channels[] = {
365 IIO_CHAN_SOFT_TIMESTAMP(16),
384 static const struct ad7606_chip_info ad7606_chip_info_tbl[] = {
385 /* More devices added in future */
387 .channels = ad7605_channels,
391 .channels = ad7606_channels,
393 .oversampling_avail = ad7606_oversampling_avail,
394 .oversampling_num = ARRAY_SIZE(ad7606_oversampling_avail),
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 = ad7616_channels,
411 .oversampling_avail = ad7616_oversampling_avail,
412 .oversampling_num = ARRAY_SIZE(ad7616_oversampling_avail),
413 .os_req_reset = true,
417 static int ad7606_request_gpios(struct ad7606_state *st)
419 struct device *dev = st->dev;
421 st->gpio_convst = devm_gpiod_get(dev, "adi,conversion-start",
423 if (IS_ERR(st->gpio_convst))
424 return PTR_ERR(st->gpio_convst);
426 st->gpio_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
427 if (IS_ERR(st->gpio_reset))
428 return PTR_ERR(st->gpio_reset);
430 st->gpio_range = devm_gpiod_get_optional(dev, "adi,range",
432 if (IS_ERR(st->gpio_range))
433 return PTR_ERR(st->gpio_range);
435 st->gpio_standby = devm_gpiod_get_optional(dev, "standby",
437 if (IS_ERR(st->gpio_standby))
438 return PTR_ERR(st->gpio_standby);
440 st->gpio_frstdata = devm_gpiod_get_optional(dev, "adi,first-data",
442 if (IS_ERR(st->gpio_frstdata))
443 return PTR_ERR(st->gpio_frstdata);
445 if (!st->chip_info->oversampling_num)
448 st->gpio_os = devm_gpiod_get_array_optional(dev,
449 "adi,oversampling-ratio",
451 return PTR_ERR_OR_ZERO(st->gpio_os);
455 * The BUSY signal indicates when conversions are in progress, so when a rising
456 * edge of CONVST is applied, BUSY goes logic high and transitions low at the
457 * end of the entire conversion process. The falling edge of the BUSY signal
458 * triggers this interrupt.
460 static irqreturn_t ad7606_interrupt(int irq, void *dev_id)
462 struct iio_dev *indio_dev = dev_id;
463 struct ad7606_state *st = iio_priv(indio_dev);
465 if (iio_buffer_enabled(indio_dev)) {
466 gpiod_set_value(st->gpio_convst, 0);
467 iio_trigger_poll_chained(st->trig);
469 complete(&st->completion);
475 static int ad7606_validate_trigger(struct iio_dev *indio_dev,
476 struct iio_trigger *trig)
478 struct ad7606_state *st = iio_priv(indio_dev);
480 if (st->trig != trig)
486 static int ad7606_buffer_postenable(struct iio_dev *indio_dev)
488 struct ad7606_state *st = iio_priv(indio_dev);
490 iio_triggered_buffer_postenable(indio_dev);
491 gpiod_set_value(st->gpio_convst, 1);
496 static int ad7606_buffer_predisable(struct iio_dev *indio_dev)
498 struct ad7606_state *st = iio_priv(indio_dev);
500 gpiod_set_value(st->gpio_convst, 0);
502 return iio_triggered_buffer_predisable(indio_dev);
505 static const struct iio_buffer_setup_ops ad7606_buffer_ops = {
506 .postenable = &ad7606_buffer_postenable,
507 .predisable = &ad7606_buffer_predisable,
510 static const struct iio_info ad7606_info_no_os_or_range = {
511 .read_raw = &ad7606_read_raw,
512 .validate_trigger = &ad7606_validate_trigger,
515 static const struct iio_info ad7606_info_os_and_range = {
516 .read_raw = &ad7606_read_raw,
517 .write_raw = &ad7606_write_raw,
518 .attrs = &ad7606_attribute_group_os_and_range,
519 .validate_trigger = &ad7606_validate_trigger,
522 static const struct iio_info ad7606_info_os = {
523 .read_raw = &ad7606_read_raw,
524 .write_raw = &ad7606_write_raw,
525 .attrs = &ad7606_attribute_group_os,
526 .validate_trigger = &ad7606_validate_trigger,
529 static const struct iio_info ad7606_info_range = {
530 .read_raw = &ad7606_read_raw,
531 .write_raw = &ad7606_write_raw,
532 .attrs = &ad7606_attribute_group_range,
533 .validate_trigger = &ad7606_validate_trigger,
536 static const struct iio_trigger_ops ad7606_trigger_ops = {
537 .validate_device = iio_trigger_validate_own_device,
540 static void ad7606_regulator_disable(void *data)
542 struct ad7606_state *st = data;
544 regulator_disable(st->reg);
547 int ad7606_probe(struct device *dev, int irq, void __iomem *base_address,
548 const char *name, unsigned int id,
549 const struct ad7606_bus_ops *bops)
551 struct ad7606_state *st;
553 struct iio_dev *indio_dev;
555 indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
559 st = iio_priv(indio_dev);
560 dev_set_drvdata(dev, indio_dev);
563 mutex_init(&st->lock);
565 st->base_address = base_address;
566 /* tied to logic low, analog input range is +/- 5V */
568 st->oversampling = 1;
569 st->scale_avail = ad7606_scale_avail;
570 st->num_scales = ARRAY_SIZE(ad7606_scale_avail);
572 st->reg = devm_regulator_get(dev, "avcc");
574 return PTR_ERR(st->reg);
576 ret = regulator_enable(st->reg);
578 dev_err(dev, "Failed to enable specified AVcc supply\n");
582 ret = devm_add_action_or_reset(dev, ad7606_regulator_disable, st);
586 st->chip_info = &ad7606_chip_info_tbl[id];
588 if (st->chip_info->oversampling_num) {
589 st->oversampling_avail = st->chip_info->oversampling_avail;
590 st->num_os_ratios = st->chip_info->oversampling_num;
593 ret = ad7606_request_gpios(st);
597 indio_dev->dev.parent = dev;
600 indio_dev->info = &ad7606_info_os_and_range;
602 indio_dev->info = &ad7606_info_os;
605 indio_dev->info = &ad7606_info_range;
607 indio_dev->info = &ad7606_info_no_os_or_range;
609 indio_dev->modes = INDIO_DIRECT_MODE;
610 indio_dev->name = name;
611 indio_dev->channels = st->chip_info->channels;
612 indio_dev->num_channels = st->chip_info->num_channels;
614 init_completion(&st->completion);
616 ret = ad7606_reset(st);
618 dev_warn(st->dev, "failed to RESET: no RESET GPIO specified\n");
620 st->write_scale = ad7606_write_scale_hw;
621 st->write_os = ad7606_write_os_hw;
623 if (st->chip_info->sw_mode_config)
624 st->sw_mode_en = device_property_present(st->dev,
627 if (st->sw_mode_en) {
628 /* After reset, in software mode, ±10 V is set by default */
629 memset32(st->range, 2, ARRAY_SIZE(st->range));
630 indio_dev->info = &ad7606_info_os_and_range;
633 * In software mode, the range gpio has no longer its function.
634 * Instead, the scale can be configured individually for each
635 * channel from the range registers.
637 if (st->chip_info->write_scale_sw)
638 st->write_scale = st->chip_info->write_scale_sw;
641 * In software mode, the oversampling is no longer configured
642 * with GPIO pins. Instead, the oversampling can be configured
643 * in configuratiion register.
645 if (st->chip_info->write_os_sw)
646 st->write_os = st->chip_info->write_os_sw;
648 ret = st->chip_info->sw_mode_config(indio_dev);
653 st->trig = devm_iio_trigger_alloc(dev, "%s-dev%d",
654 indio_dev->name, indio_dev->id);
658 st->trig->ops = &ad7606_trigger_ops;
659 st->trig->dev.parent = dev;
660 iio_trigger_set_drvdata(st->trig, indio_dev);
661 ret = devm_iio_trigger_register(dev, st->trig);
665 indio_dev->trig = iio_trigger_get(st->trig);
667 ret = devm_request_threaded_irq(dev, irq,
670 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
675 ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
676 &iio_pollfunc_store_time,
677 &ad7606_trigger_handler,
682 return devm_iio_device_register(dev, indio_dev);
684 EXPORT_SYMBOL_GPL(ad7606_probe);
686 #ifdef CONFIG_PM_SLEEP
688 static int ad7606_suspend(struct device *dev)
690 struct iio_dev *indio_dev = dev_get_drvdata(dev);
691 struct ad7606_state *st = iio_priv(indio_dev);
693 if (st->gpio_standby) {
694 gpiod_set_value(st->gpio_range, 1);
695 gpiod_set_value(st->gpio_standby, 0);
701 static int ad7606_resume(struct device *dev)
703 struct iio_dev *indio_dev = dev_get_drvdata(dev);
704 struct ad7606_state *st = iio_priv(indio_dev);
706 if (st->gpio_standby) {
707 gpiod_set_value(st->gpio_range, st->range[0]);
708 gpiod_set_value(st->gpio_standby, 1);
715 SIMPLE_DEV_PM_OPS(ad7606_pm_ops, ad7606_suspend, ad7606_resume);
716 EXPORT_SYMBOL_GPL(ad7606_pm_ops);
720 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
721 MODULE_DESCRIPTION("Analog Devices AD7606 ADC");
722 MODULE_LICENSE("GPL v2");