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 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);
63 EXPORT_SYMBOL_NS_GPL(ad7606_reset
, IIO_AD7606
);
65 static int ad7606_reg_access(struct iio_dev
*indio_dev
,
67 unsigned int writeval
,
68 unsigned int *readval
)
70 struct ad7606_state
*st
= iio_priv(indio_dev
);
73 guard(mutex
)(&st
->lock
);
76 ret
= st
->bops
->reg_read(st
, reg
);
82 return st
->bops
->reg_write(st
, reg
, writeval
);
86 static int ad7606_read_samples(struct ad7606_state
*st
)
88 unsigned int num
= st
->chip_info
->num_channels
- 1;
91 return st
->bops
->read_block(st
->dev
, num
, data
);
94 static irqreturn_t
ad7606_trigger_handler(int irq
, void *p
)
96 struct iio_poll_func
*pf
= p
;
97 struct iio_dev
*indio_dev
= pf
->indio_dev
;
98 struct ad7606_state
*st
= iio_priv(indio_dev
);
101 guard(mutex
)(&st
->lock
);
103 ret
= ad7606_read_samples(st
);
107 iio_push_to_buffers_with_timestamp(indio_dev
, st
->data
,
108 iio_get_time_ns(indio_dev
));
110 iio_trigger_notify_done(indio_dev
->trig
);
111 /* The rising edge of the CONVST signal starts a new conversion. */
112 gpiod_set_value(st
->gpio_convst
, 1);
117 static int ad7606_scan_direct(struct iio_dev
*indio_dev
, unsigned int ch
)
119 struct ad7606_state
*st
= iio_priv(indio_dev
);
122 gpiod_set_value(st
->gpio_convst
, 1);
123 ret
= wait_for_completion_timeout(&st
->completion
,
124 msecs_to_jiffies(1000));
130 ret
= ad7606_read_samples(st
);
135 gpiod_set_value(st
->gpio_convst
, 0);
140 static int ad7606_read_raw(struct iio_dev
*indio_dev
,
141 struct iio_chan_spec
const *chan
,
147 struct ad7606_state
*st
= iio_priv(indio_dev
);
150 case IIO_CHAN_INFO_RAW
:
151 iio_device_claim_direct_scoped(return -EBUSY
, indio_dev
) {
152 ret
= ad7606_scan_direct(indio_dev
, chan
->address
);
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);
213 values
[0] = val
& GENMASK(2, 0);
215 gpiod_set_array_value(st
->gpio_os
->ndescs
, 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
);
234 guard(mutex
)(&st
->lock
);
237 case IIO_CHAN_INFO_SCALE
:
238 i
= find_closest(val2
, st
->scale_avail
, st
->num_scales
);
241 ret
= st
->write_scale(indio_dev
, ch
, i
);
247 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
250 i
= find_closest(val
, st
->oversampling_avail
,
252 ret
= st
->write_os(indio_dev
, i
);
262 static ssize_t
ad7606_oversampling_ratio_avail(struct device
*dev
,
263 struct device_attribute
*attr
,
266 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
267 struct ad7606_state
*st
= iio_priv(indio_dev
);
269 return ad7606_show_avail(buf
, st
->oversampling_avail
,
270 st
->num_os_ratios
, false);
273 static IIO_DEVICE_ATTR(oversampling_ratio_available
, 0444,
274 ad7606_oversampling_ratio_avail
, NULL
, 0);
276 static struct attribute
*ad7606_attributes_os_and_range
[] = {
277 &iio_dev_attr_in_voltage_scale_available
.dev_attr
.attr
,
278 &iio_dev_attr_oversampling_ratio_available
.dev_attr
.attr
,
282 static const struct attribute_group ad7606_attribute_group_os_and_range
= {
283 .attrs
= ad7606_attributes_os_and_range
,
286 static struct attribute
*ad7606_attributes_os
[] = {
287 &iio_dev_attr_oversampling_ratio_available
.dev_attr
.attr
,
291 static const struct attribute_group ad7606_attribute_group_os
= {
292 .attrs
= ad7606_attributes_os
,
295 static struct attribute
*ad7606_attributes_range
[] = {
296 &iio_dev_attr_in_voltage_scale_available
.dev_attr
.attr
,
300 static const struct attribute_group ad7606_attribute_group_range
= {
301 .attrs
= ad7606_attributes_range
,
304 static const struct iio_chan_spec ad7605_channels
[] = {
305 IIO_CHAN_SOFT_TIMESTAMP(4),
312 static const struct iio_chan_spec ad7606_channels
[] = {
313 IIO_CHAN_SOFT_TIMESTAMP(8),
325 * The current assumption that this driver makes for AD7616, is that it's
326 * working in Hardware Mode with Serial, Burst and Sequencer modes activated.
327 * To activate them, following pins must be pulled high:
330 * And following pins must be pulled low:
334 static const struct iio_chan_spec ad7616_channels
[] = {
335 IIO_CHAN_SOFT_TIMESTAMP(16),
354 static const struct ad7606_chip_info ad7606_chip_info_tbl
[] = {
355 /* More devices added in future */
357 .channels
= ad7605_channels
,
361 .channels
= ad7606_channels
,
363 .oversampling_avail
= ad7606_oversampling_avail
,
364 .oversampling_num
= ARRAY_SIZE(ad7606_oversampling_avail
),
367 .channels
= ad7606_channels
,
369 .oversampling_avail
= ad7606_oversampling_avail
,
370 .oversampling_num
= ARRAY_SIZE(ad7606_oversampling_avail
),
373 .channels
= ad7606_channels
,
375 .oversampling_avail
= ad7606_oversampling_avail
,
376 .oversampling_num
= ARRAY_SIZE(ad7606_oversampling_avail
),
379 .channels
= ad7606_channels
,
381 .oversampling_avail
= ad7606_oversampling_avail
,
382 .oversampling_num
= ARRAY_SIZE(ad7606_oversampling_avail
),
385 .channels
= ad7616_channels
,
387 .oversampling_avail
= ad7616_oversampling_avail
,
388 .oversampling_num
= ARRAY_SIZE(ad7616_oversampling_avail
),
389 .os_req_reset
= true,
394 static int ad7606_request_gpios(struct ad7606_state
*st
)
396 struct device
*dev
= st
->dev
;
398 st
->gpio_convst
= devm_gpiod_get(dev
, "adi,conversion-start",
400 if (IS_ERR(st
->gpio_convst
))
401 return PTR_ERR(st
->gpio_convst
);
403 st
->gpio_reset
= devm_gpiod_get_optional(dev
, "reset", GPIOD_OUT_LOW
);
404 if (IS_ERR(st
->gpio_reset
))
405 return PTR_ERR(st
->gpio_reset
);
407 st
->gpio_range
= devm_gpiod_get_optional(dev
, "adi,range",
409 if (IS_ERR(st
->gpio_range
))
410 return PTR_ERR(st
->gpio_range
);
412 st
->gpio_standby
= devm_gpiod_get_optional(dev
, "standby",
414 if (IS_ERR(st
->gpio_standby
))
415 return PTR_ERR(st
->gpio_standby
);
417 st
->gpio_frstdata
= devm_gpiod_get_optional(dev
, "adi,first-data",
419 if (IS_ERR(st
->gpio_frstdata
))
420 return PTR_ERR(st
->gpio_frstdata
);
422 if (!st
->chip_info
->oversampling_num
)
425 st
->gpio_os
= devm_gpiod_get_array_optional(dev
,
426 "adi,oversampling-ratio",
428 return PTR_ERR_OR_ZERO(st
->gpio_os
);
432 * The BUSY signal indicates when conversions are in progress, so when a rising
433 * edge of CONVST is applied, BUSY goes logic high and transitions low at the
434 * end of the entire conversion process. The falling edge of the BUSY signal
435 * triggers this interrupt.
437 static irqreturn_t
ad7606_interrupt(int irq
, void *dev_id
)
439 struct iio_dev
*indio_dev
= dev_id
;
440 struct ad7606_state
*st
= iio_priv(indio_dev
);
442 if (iio_buffer_enabled(indio_dev
)) {
443 gpiod_set_value(st
->gpio_convst
, 0);
444 iio_trigger_poll_nested(st
->trig
);
446 complete(&st
->completion
);
452 static int ad7606_validate_trigger(struct iio_dev
*indio_dev
,
453 struct iio_trigger
*trig
)
455 struct ad7606_state
*st
= iio_priv(indio_dev
);
457 if (st
->trig
!= trig
)
463 static int ad7606_buffer_postenable(struct iio_dev
*indio_dev
)
465 struct ad7606_state
*st
= iio_priv(indio_dev
);
467 gpiod_set_value(st
->gpio_convst
, 1);
472 static int ad7606_buffer_predisable(struct iio_dev
*indio_dev
)
474 struct ad7606_state
*st
= iio_priv(indio_dev
);
476 gpiod_set_value(st
->gpio_convst
, 0);
481 static const struct iio_buffer_setup_ops ad7606_buffer_ops
= {
482 .postenable
= &ad7606_buffer_postenable
,
483 .predisable
= &ad7606_buffer_predisable
,
486 static const struct iio_info ad7606_info_no_os_or_range
= {
487 .read_raw
= &ad7606_read_raw
,
488 .validate_trigger
= &ad7606_validate_trigger
,
491 static const struct iio_info ad7606_info_os_and_range
= {
492 .read_raw
= &ad7606_read_raw
,
493 .write_raw
= &ad7606_write_raw
,
494 .attrs
= &ad7606_attribute_group_os_and_range
,
495 .validate_trigger
= &ad7606_validate_trigger
,
498 static const struct iio_info ad7606_info_os_range_and_debug
= {
499 .read_raw
= &ad7606_read_raw
,
500 .write_raw
= &ad7606_write_raw
,
501 .debugfs_reg_access
= &ad7606_reg_access
,
502 .attrs
= &ad7606_attribute_group_os_and_range
,
503 .validate_trigger
= &ad7606_validate_trigger
,
506 static const struct iio_info ad7606_info_os
= {
507 .read_raw
= &ad7606_read_raw
,
508 .write_raw
= &ad7606_write_raw
,
509 .attrs
= &ad7606_attribute_group_os
,
510 .validate_trigger
= &ad7606_validate_trigger
,
513 static const struct iio_info ad7606_info_range
= {
514 .read_raw
= &ad7606_read_raw
,
515 .write_raw
= &ad7606_write_raw
,
516 .attrs
= &ad7606_attribute_group_range
,
517 .validate_trigger
= &ad7606_validate_trigger
,
520 static const struct iio_trigger_ops ad7606_trigger_ops
= {
521 .validate_device
= iio_trigger_validate_own_device
,
524 int ad7606_probe(struct device
*dev
, int irq
, void __iomem
*base_address
,
525 const char *name
, unsigned int id
,
526 const struct ad7606_bus_ops
*bops
)
528 struct ad7606_state
*st
;
530 struct iio_dev
*indio_dev
;
532 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*st
));
536 st
= iio_priv(indio_dev
);
537 dev_set_drvdata(dev
, indio_dev
);
540 mutex_init(&st
->lock
);
542 st
->base_address
= base_address
;
543 /* tied to logic low, analog input range is +/- 5V */
545 st
->oversampling
= 1;
546 st
->scale_avail
= ad7606_scale_avail
;
547 st
->num_scales
= ARRAY_SIZE(ad7606_scale_avail
);
549 ret
= devm_regulator_get_enable(dev
, "avcc");
551 return dev_err_probe(dev
, ret
,
552 "Failed to enable specified AVcc supply\n");
554 st
->chip_info
= &ad7606_chip_info_tbl
[id
];
556 if (st
->chip_info
->oversampling_num
) {
557 st
->oversampling_avail
= st
->chip_info
->oversampling_avail
;
558 st
->num_os_ratios
= st
->chip_info
->oversampling_num
;
561 ret
= ad7606_request_gpios(st
);
567 indio_dev
->info
= &ad7606_info_os_and_range
;
569 indio_dev
->info
= &ad7606_info_os
;
572 indio_dev
->info
= &ad7606_info_range
;
574 indio_dev
->info
= &ad7606_info_no_os_or_range
;
576 indio_dev
->modes
= INDIO_DIRECT_MODE
;
577 indio_dev
->name
= name
;
578 indio_dev
->channels
= st
->chip_info
->channels
;
579 indio_dev
->num_channels
= st
->chip_info
->num_channels
;
581 init_completion(&st
->completion
);
583 ret
= ad7606_reset(st
);
585 dev_warn(st
->dev
, "failed to RESET: no RESET GPIO specified\n");
587 /* AD7616 requires al least 15ms to reconfigure after a reset */
588 if (st
->chip_info
->init_delay_ms
) {
589 if (msleep_interruptible(st
->chip_info
->init_delay_ms
))
593 st
->write_scale
= ad7606_write_scale_hw
;
594 st
->write_os
= ad7606_write_os_hw
;
596 if (st
->bops
->sw_mode_config
)
597 st
->sw_mode_en
= device_property_present(st
->dev
,
600 if (st
->sw_mode_en
) {
601 /* Scale of 0.076293 is only available in sw mode */
602 st
->scale_avail
= ad7616_sw_scale_avail
;
603 st
->num_scales
= ARRAY_SIZE(ad7616_sw_scale_avail
);
605 /* After reset, in software mode, ±10 V is set by default */
606 memset32(st
->range
, 2, ARRAY_SIZE(st
->range
));
607 indio_dev
->info
= &ad7606_info_os_range_and_debug
;
609 ret
= st
->bops
->sw_mode_config(indio_dev
);
614 st
->trig
= devm_iio_trigger_alloc(dev
, "%s-dev%d",
616 iio_device_id(indio_dev
));
620 st
->trig
->ops
= &ad7606_trigger_ops
;
621 iio_trigger_set_drvdata(st
->trig
, indio_dev
);
622 ret
= devm_iio_trigger_register(dev
, st
->trig
);
626 indio_dev
->trig
= iio_trigger_get(st
->trig
);
628 ret
= devm_request_threaded_irq(dev
, irq
,
631 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
636 ret
= devm_iio_triggered_buffer_setup(dev
, indio_dev
,
637 &iio_pollfunc_store_time
,
638 &ad7606_trigger_handler
,
643 return devm_iio_device_register(dev
, indio_dev
);
645 EXPORT_SYMBOL_NS_GPL(ad7606_probe
, IIO_AD7606
);
647 #ifdef CONFIG_PM_SLEEP
649 static int ad7606_suspend(struct device
*dev
)
651 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
652 struct ad7606_state
*st
= iio_priv(indio_dev
);
654 if (st
->gpio_standby
) {
655 gpiod_set_value(st
->gpio_range
, 1);
656 gpiod_set_value(st
->gpio_standby
, 1);
662 static int ad7606_resume(struct device
*dev
)
664 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
665 struct ad7606_state
*st
= iio_priv(indio_dev
);
667 if (st
->gpio_standby
) {
668 gpiod_set_value(st
->gpio_range
, st
->range
[0]);
669 gpiod_set_value(st
->gpio_standby
, 1);
676 SIMPLE_DEV_PM_OPS(ad7606_pm_ops
, ad7606_suspend
, ad7606_resume
);
677 EXPORT_SYMBOL_NS_GPL(ad7606_pm_ops
, IIO_AD7606
);
681 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
682 MODULE_DESCRIPTION("Analog Devices AD7606 ADC");
683 MODULE_LICENSE("GPL v2");