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] = {
39 static const unsigned int ad7616_sw_scale_avail
[3] = {
43 static const unsigned int ad7606_oversampling_avail
[7] = {
44 1, 2, 4, 8, 16, 32, 64,
47 static const unsigned int ad7616_oversampling_avail
[8] = {
48 1, 2, 4, 8, 16, 32, 64, 128,
51 static int ad7606_reset(struct ad7606_state
*st
)
54 gpiod_set_value(st
->gpio_reset
, 1);
55 ndelay(100); /* t_reset >= 100ns */
56 gpiod_set_value(st
->gpio_reset
, 0);
63 static int ad7606_reg_access(struct iio_dev
*indio_dev
,
65 unsigned int writeval
,
66 unsigned int *readval
)
68 struct ad7606_state
*st
= iio_priv(indio_dev
);
71 mutex_lock(&st
->lock
);
73 ret
= st
->bops
->reg_read(st
, reg
);
79 ret
= st
->bops
->reg_write(st
, reg
, writeval
);
82 mutex_unlock(&st
->lock
);
86 static int ad7606_read_samples(struct ad7606_state
*st
)
88 unsigned int num
= st
->chip_info
->num_channels
- 1;
93 * The frstdata signal is set to high while and after reading the sample
94 * of the first channel and low for all other channels. This can be used
95 * to check that the incoming data is correctly aligned. During normal
96 * operation the data should never become unaligned, but some glitch or
97 * electrostatic discharge might cause an extra read or clock cycle.
98 * Monitoring the frstdata signal allows to recover from such failure
102 if (st
->gpio_frstdata
) {
103 ret
= st
->bops
->read_block(st
->dev
, 1, data
);
107 if (!gpiod_get_value(st
->gpio_frstdata
)) {
116 return st
->bops
->read_block(st
->dev
, num
, data
);
119 static irqreturn_t
ad7606_trigger_handler(int irq
, void *p
)
121 struct iio_poll_func
*pf
= p
;
122 struct iio_dev
*indio_dev
= pf
->indio_dev
;
123 struct ad7606_state
*st
= iio_priv(indio_dev
);
126 mutex_lock(&st
->lock
);
128 ret
= ad7606_read_samples(st
);
130 iio_push_to_buffers_with_timestamp(indio_dev
, st
->data
,
131 iio_get_time_ns(indio_dev
));
133 iio_trigger_notify_done(indio_dev
->trig
);
134 /* The rising edge of the CONVST signal starts a new conversion. */
135 gpiod_set_value(st
->gpio_convst
, 1);
137 mutex_unlock(&st
->lock
);
142 static int ad7606_scan_direct(struct iio_dev
*indio_dev
, unsigned int ch
)
144 struct ad7606_state
*st
= iio_priv(indio_dev
);
147 gpiod_set_value(st
->gpio_convst
, 1);
148 ret
= wait_for_completion_timeout(&st
->completion
,
149 msecs_to_jiffies(1000));
155 ret
= ad7606_read_samples(st
);
160 gpiod_set_value(st
->gpio_convst
, 0);
165 static int ad7606_read_raw(struct iio_dev
*indio_dev
,
166 struct iio_chan_spec
const *chan
,
172 struct ad7606_state
*st
= iio_priv(indio_dev
);
175 case IIO_CHAN_INFO_RAW
:
176 ret
= iio_device_claim_direct_mode(indio_dev
);
180 ret
= ad7606_scan_direct(indio_dev
, chan
->address
);
181 iio_device_release_direct_mode(indio_dev
);
187 case IIO_CHAN_INFO_SCALE
:
191 *val2
= st
->scale_avail
[st
->range
[ch
]];
192 return IIO_VAL_INT_PLUS_MICRO
;
193 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
194 *val
= st
->oversampling
;
200 static ssize_t
ad7606_show_avail(char *buf
, const unsigned int *vals
,
201 unsigned int n
, bool micros
)
206 for (i
= 0; i
< n
; i
++) {
207 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
,
208 micros
? "0.%06u " : "%u ", vals
[i
]);
215 static ssize_t
in_voltage_scale_available_show(struct device
*dev
,
216 struct device_attribute
*attr
,
219 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
220 struct ad7606_state
*st
= iio_priv(indio_dev
);
222 return ad7606_show_avail(buf
, st
->scale_avail
, st
->num_scales
, true);
225 static IIO_DEVICE_ATTR_RO(in_voltage_scale_available
, 0);
227 static int ad7606_write_scale_hw(struct iio_dev
*indio_dev
, int ch
, int val
)
229 struct ad7606_state
*st
= iio_priv(indio_dev
);
231 gpiod_set_value(st
->gpio_range
, val
);
236 static int ad7606_write_os_hw(struct iio_dev
*indio_dev
, int val
)
238 struct ad7606_state
*st
= iio_priv(indio_dev
);
239 DECLARE_BITMAP(values
, 3);
243 gpiod_set_array_value(ARRAY_SIZE(values
), st
->gpio_os
->desc
,
244 st
->gpio_os
->info
, values
);
246 /* AD7616 requires a reset to update value */
247 if (st
->chip_info
->os_req_reset
)
253 static int ad7606_write_raw(struct iio_dev
*indio_dev
,
254 struct iio_chan_spec
const *chan
,
259 struct ad7606_state
*st
= iio_priv(indio_dev
);
263 case IIO_CHAN_INFO_SCALE
:
264 mutex_lock(&st
->lock
);
265 i
= find_closest(val2
, st
->scale_avail
, st
->num_scales
);
268 ret
= st
->write_scale(indio_dev
, ch
, i
);
270 mutex_unlock(&st
->lock
);
274 mutex_unlock(&st
->lock
);
277 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
280 i
= find_closest(val
, st
->oversampling_avail
,
282 mutex_lock(&st
->lock
);
283 ret
= st
->write_os(indio_dev
, i
);
285 mutex_unlock(&st
->lock
);
288 st
->oversampling
= st
->oversampling_avail
[i
];
289 mutex_unlock(&st
->lock
);
297 static ssize_t
ad7606_oversampling_ratio_avail(struct device
*dev
,
298 struct device_attribute
*attr
,
301 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
302 struct ad7606_state
*st
= iio_priv(indio_dev
);
304 return ad7606_show_avail(buf
, st
->oversampling_avail
,
305 st
->num_os_ratios
, false);
308 static IIO_DEVICE_ATTR(oversampling_ratio_available
, 0444,
309 ad7606_oversampling_ratio_avail
, NULL
, 0);
311 static struct attribute
*ad7606_attributes_os_and_range
[] = {
312 &iio_dev_attr_in_voltage_scale_available
.dev_attr
.attr
,
313 &iio_dev_attr_oversampling_ratio_available
.dev_attr
.attr
,
317 static const struct attribute_group ad7606_attribute_group_os_and_range
= {
318 .attrs
= ad7606_attributes_os_and_range
,
321 static struct attribute
*ad7606_attributes_os
[] = {
322 &iio_dev_attr_oversampling_ratio_available
.dev_attr
.attr
,
326 static const struct attribute_group ad7606_attribute_group_os
= {
327 .attrs
= ad7606_attributes_os
,
330 static struct attribute
*ad7606_attributes_range
[] = {
331 &iio_dev_attr_in_voltage_scale_available
.dev_attr
.attr
,
335 static const struct attribute_group ad7606_attribute_group_range
= {
336 .attrs
= ad7606_attributes_range
,
339 static const struct iio_chan_spec ad7605_channels
[] = {
340 IIO_CHAN_SOFT_TIMESTAMP(4),
347 static const struct iio_chan_spec ad7606_channels
[] = {
348 IIO_CHAN_SOFT_TIMESTAMP(8),
360 * The current assumption that this driver makes for AD7616, is that it's
361 * working in Hardware Mode with Serial, Burst and Sequencer modes activated.
362 * To activate them, following pins must be pulled high:
365 * And following pins must be pulled low:
369 static const struct iio_chan_spec ad7616_channels
[] = {
370 IIO_CHAN_SOFT_TIMESTAMP(16),
389 static const struct ad7606_chip_info ad7606_chip_info_tbl
[] = {
390 /* More devices added in future */
392 .channels
= ad7605_channels
,
396 .channels
= ad7606_channels
,
398 .oversampling_avail
= ad7606_oversampling_avail
,
399 .oversampling_num
= ARRAY_SIZE(ad7606_oversampling_avail
),
402 .channels
= ad7606_channels
,
404 .oversampling_avail
= ad7606_oversampling_avail
,
405 .oversampling_num
= ARRAY_SIZE(ad7606_oversampling_avail
),
408 .channels
= ad7606_channels
,
410 .oversampling_avail
= ad7606_oversampling_avail
,
411 .oversampling_num
= ARRAY_SIZE(ad7606_oversampling_avail
),
414 .channels
= ad7606_channels
,
416 .oversampling_avail
= ad7606_oversampling_avail
,
417 .oversampling_num
= ARRAY_SIZE(ad7606_oversampling_avail
),
420 .channels
= ad7616_channels
,
422 .oversampling_avail
= ad7616_oversampling_avail
,
423 .oversampling_num
= ARRAY_SIZE(ad7616_oversampling_avail
),
424 .os_req_reset
= true,
429 static int ad7606_request_gpios(struct ad7606_state
*st
)
431 struct device
*dev
= st
->dev
;
433 st
->gpio_convst
= devm_gpiod_get(dev
, "adi,conversion-start",
435 if (IS_ERR(st
->gpio_convst
))
436 return PTR_ERR(st
->gpio_convst
);
438 st
->gpio_reset
= devm_gpiod_get_optional(dev
, "reset", GPIOD_OUT_LOW
);
439 if (IS_ERR(st
->gpio_reset
))
440 return PTR_ERR(st
->gpio_reset
);
442 st
->gpio_range
= devm_gpiod_get_optional(dev
, "adi,range",
444 if (IS_ERR(st
->gpio_range
))
445 return PTR_ERR(st
->gpio_range
);
447 st
->gpio_standby
= devm_gpiod_get_optional(dev
, "standby",
449 if (IS_ERR(st
->gpio_standby
))
450 return PTR_ERR(st
->gpio_standby
);
452 st
->gpio_frstdata
= devm_gpiod_get_optional(dev
, "adi,first-data",
454 if (IS_ERR(st
->gpio_frstdata
))
455 return PTR_ERR(st
->gpio_frstdata
);
457 if (!st
->chip_info
->oversampling_num
)
460 st
->gpio_os
= devm_gpiod_get_array_optional(dev
,
461 "adi,oversampling-ratio",
463 return PTR_ERR_OR_ZERO(st
->gpio_os
);
467 * The BUSY signal indicates when conversions are in progress, so when a rising
468 * edge of CONVST is applied, BUSY goes logic high and transitions low at the
469 * end of the entire conversion process. The falling edge of the BUSY signal
470 * triggers this interrupt.
472 static irqreturn_t
ad7606_interrupt(int irq
, void *dev_id
)
474 struct iio_dev
*indio_dev
= dev_id
;
475 struct ad7606_state
*st
= iio_priv(indio_dev
);
477 if (iio_buffer_enabled(indio_dev
)) {
478 gpiod_set_value(st
->gpio_convst
, 0);
479 iio_trigger_poll_chained(st
->trig
);
481 complete(&st
->completion
);
487 static int ad7606_validate_trigger(struct iio_dev
*indio_dev
,
488 struct iio_trigger
*trig
)
490 struct ad7606_state
*st
= iio_priv(indio_dev
);
492 if (st
->trig
!= trig
)
498 static int ad7606_buffer_postenable(struct iio_dev
*indio_dev
)
500 struct ad7606_state
*st
= iio_priv(indio_dev
);
502 gpiod_set_value(st
->gpio_convst
, 1);
507 static int ad7606_buffer_predisable(struct iio_dev
*indio_dev
)
509 struct ad7606_state
*st
= iio_priv(indio_dev
);
511 gpiod_set_value(st
->gpio_convst
, 0);
516 static const struct iio_buffer_setup_ops ad7606_buffer_ops
= {
517 .postenable
= &ad7606_buffer_postenable
,
518 .predisable
= &ad7606_buffer_predisable
,
521 static const struct iio_info ad7606_info_no_os_or_range
= {
522 .read_raw
= &ad7606_read_raw
,
523 .validate_trigger
= &ad7606_validate_trigger
,
526 static const struct iio_info ad7606_info_os_and_range
= {
527 .read_raw
= &ad7606_read_raw
,
528 .write_raw
= &ad7606_write_raw
,
529 .attrs
= &ad7606_attribute_group_os_and_range
,
530 .validate_trigger
= &ad7606_validate_trigger
,
533 static const struct iio_info ad7606_info_os_range_and_debug
= {
534 .read_raw
= &ad7606_read_raw
,
535 .write_raw
= &ad7606_write_raw
,
536 .debugfs_reg_access
= &ad7606_reg_access
,
537 .attrs
= &ad7606_attribute_group_os_and_range
,
538 .validate_trigger
= &ad7606_validate_trigger
,
541 static const struct iio_info ad7606_info_os
= {
542 .read_raw
= &ad7606_read_raw
,
543 .write_raw
= &ad7606_write_raw
,
544 .attrs
= &ad7606_attribute_group_os
,
545 .validate_trigger
= &ad7606_validate_trigger
,
548 static const struct iio_info ad7606_info_range
= {
549 .read_raw
= &ad7606_read_raw
,
550 .write_raw
= &ad7606_write_raw
,
551 .attrs
= &ad7606_attribute_group_range
,
552 .validate_trigger
= &ad7606_validate_trigger
,
555 static const struct iio_trigger_ops ad7606_trigger_ops
= {
556 .validate_device
= iio_trigger_validate_own_device
,
559 static void ad7606_regulator_disable(void *data
)
561 struct ad7606_state
*st
= data
;
563 regulator_disable(st
->reg
);
566 int ad7606_probe(struct device
*dev
, int irq
, void __iomem
*base_address
,
567 const char *name
, unsigned int id
,
568 const struct ad7606_bus_ops
*bops
)
570 struct ad7606_state
*st
;
572 struct iio_dev
*indio_dev
;
574 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*st
));
578 st
= iio_priv(indio_dev
);
579 dev_set_drvdata(dev
, indio_dev
);
582 mutex_init(&st
->lock
);
584 st
->base_address
= base_address
;
585 /* tied to logic low, analog input range is +/- 5V */
587 st
->oversampling
= 1;
588 st
->scale_avail
= ad7606_scale_avail
;
589 st
->num_scales
= ARRAY_SIZE(ad7606_scale_avail
);
591 st
->reg
= devm_regulator_get(dev
, "avcc");
593 return PTR_ERR(st
->reg
);
595 ret
= regulator_enable(st
->reg
);
597 dev_err(dev
, "Failed to enable specified AVcc supply\n");
601 ret
= devm_add_action_or_reset(dev
, ad7606_regulator_disable
, st
);
605 st
->chip_info
= &ad7606_chip_info_tbl
[id
];
607 if (st
->chip_info
->oversampling_num
) {
608 st
->oversampling_avail
= st
->chip_info
->oversampling_avail
;
609 st
->num_os_ratios
= st
->chip_info
->oversampling_num
;
612 ret
= ad7606_request_gpios(st
);
618 indio_dev
->info
= &ad7606_info_os_and_range
;
620 indio_dev
->info
= &ad7606_info_os
;
623 indio_dev
->info
= &ad7606_info_range
;
625 indio_dev
->info
= &ad7606_info_no_os_or_range
;
627 indio_dev
->modes
= INDIO_DIRECT_MODE
;
628 indio_dev
->name
= name
;
629 indio_dev
->channels
= st
->chip_info
->channels
;
630 indio_dev
->num_channels
= st
->chip_info
->num_channels
;
632 init_completion(&st
->completion
);
634 ret
= ad7606_reset(st
);
636 dev_warn(st
->dev
, "failed to RESET: no RESET GPIO specified\n");
638 /* AD7616 requires al least 15ms to reconfigure after a reset */
639 if (st
->chip_info
->init_delay_ms
) {
640 if (msleep_interruptible(st
->chip_info
->init_delay_ms
))
644 st
->write_scale
= ad7606_write_scale_hw
;
645 st
->write_os
= ad7606_write_os_hw
;
647 if (st
->bops
->sw_mode_config
)
648 st
->sw_mode_en
= device_property_present(st
->dev
,
651 if (st
->sw_mode_en
) {
652 /* Scale of 0.076293 is only available in sw mode */
653 st
->scale_avail
= ad7616_sw_scale_avail
;
654 st
->num_scales
= ARRAY_SIZE(ad7616_sw_scale_avail
);
656 /* After reset, in software mode, ±10 V is set by default */
657 memset32(st
->range
, 2, ARRAY_SIZE(st
->range
));
658 indio_dev
->info
= &ad7606_info_os_range_and_debug
;
660 ret
= st
->bops
->sw_mode_config(indio_dev
);
665 st
->trig
= devm_iio_trigger_alloc(dev
, "%s-dev%d",
666 indio_dev
->name
, indio_dev
->id
);
670 st
->trig
->ops
= &ad7606_trigger_ops
;
671 st
->trig
->dev
.parent
= dev
;
672 iio_trigger_set_drvdata(st
->trig
, indio_dev
);
673 ret
= devm_iio_trigger_register(dev
, st
->trig
);
677 indio_dev
->trig
= iio_trigger_get(st
->trig
);
679 ret
= devm_request_threaded_irq(dev
, irq
,
682 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
687 ret
= devm_iio_triggered_buffer_setup(dev
, indio_dev
,
688 &iio_pollfunc_store_time
,
689 &ad7606_trigger_handler
,
694 return devm_iio_device_register(dev
, indio_dev
);
696 EXPORT_SYMBOL_GPL(ad7606_probe
);
698 #ifdef CONFIG_PM_SLEEP
700 static int ad7606_suspend(struct device
*dev
)
702 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
703 struct ad7606_state
*st
= iio_priv(indio_dev
);
705 if (st
->gpio_standby
) {
706 gpiod_set_value(st
->gpio_range
, 1);
707 gpiod_set_value(st
->gpio_standby
, 0);
713 static int ad7606_resume(struct device
*dev
)
715 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
716 struct ad7606_state
*st
= iio_priv(indio_dev
);
718 if (st
->gpio_standby
) {
719 gpiod_set_value(st
->gpio_range
, st
->range
[0]);
720 gpiod_set_value(st
->gpio_standby
, 1);
727 SIMPLE_DEV_PM_OPS(ad7606_pm_ops
, ad7606_suspend
, ad7606_resume
);
728 EXPORT_SYMBOL_GPL(ad7606_pm_ops
);
732 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
733 MODULE_DESCRIPTION("Analog Devices AD7606 ADC");
734 MODULE_LICENSE("GPL v2");