2 * AD7606 SPI ADC driver
4 * Copyright 2011 Analog Devices Inc.
6 * Licensed under the GPL-2.
9 #include <linux/interrupt.h>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/sysfs.h>
14 #include <linux/regulator/consumer.h>
15 #include <linux/err.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/delay.h>
18 #include <linux/sched.h>
19 #include <linux/module.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 #include <linux/iio/buffer.h>
24 #include <linux/iio/trigger_consumer.h>
25 #include <linux/iio/triggered_buffer.h>
30 * Scales are computed as 5000/32768 and 10000/32768 respectively,
31 * so that when applied to the raw values they provide mV values
33 static const unsigned int scale_avail[2][2] = {
34 {0, 152588}, {0, 305176}
37 static int ad7606_reset(struct ad7606_state *st)
40 gpiod_set_value(st->gpio_reset, 1);
41 ndelay(100); /* t_reset >= 100ns */
42 gpiod_set_value(st->gpio_reset, 0);
49 static int ad7606_read_samples(struct ad7606_state *st)
51 unsigned int num = st->chip_info->num_channels;
56 * The frstdata signal is set to high while and after reading the sample
57 * of the first channel and low for all other channels. This can be used
58 * to check that the incoming data is correctly aligned. During normal
59 * operation the data should never become unaligned, but some glitch or
60 * electrostatic discharge might cause an extra read or clock cycle.
61 * Monitoring the frstdata signal allows to recover from such failure
65 if (st->gpio_frstdata) {
66 ret = st->bops->read_block(st->dev, 1, data);
70 if (!gpiod_get_value(st->gpio_frstdata)) {
79 return st->bops->read_block(st->dev, num, data);
82 static irqreturn_t ad7606_trigger_handler(int irq, void *p)
84 struct iio_poll_func *pf = p;
85 struct ad7606_state *st = iio_priv(pf->indio_dev);
87 gpiod_set_value(st->gpio_convst, 1);
93 * ad7606_poll_bh_to_ring() bh of trigger launched polling to ring buffer
94 * @work_s: the work struct through which this was scheduled
96 * Currently there is no option in this driver to disable the saving of
97 * timestamps within the ring.
98 * I think the one copy of this at a time was to avoid problems if the
99 * trigger was set far too high and the reads then locked up the computer.
101 static void ad7606_poll_bh_to_ring(struct work_struct *work_s)
103 struct ad7606_state *st = container_of(work_s, struct ad7606_state,
105 struct iio_dev *indio_dev = iio_priv_to_dev(st);
108 ret = ad7606_read_samples(st);
110 iio_push_to_buffers_with_timestamp(indio_dev, st->data,
111 iio_get_time_ns(indio_dev));
113 gpiod_set_value(st->gpio_convst, 0);
114 iio_trigger_notify_done(indio_dev->trig);
117 static int ad7606_scan_direct(struct iio_dev *indio_dev, unsigned int ch)
119 struct ad7606_state *st = iio_priv(indio_dev);
123 gpiod_set_value(st->gpio_convst, 1);
125 ret = wait_event_interruptible(st->wq_data_avail, st->done);
129 ret = ad7606_read_samples(st);
134 gpiod_set_value(st->gpio_convst, 0);
139 static int ad7606_read_raw(struct iio_dev *indio_dev,
140 struct iio_chan_spec const *chan,
146 struct ad7606_state *st = iio_priv(indio_dev);
149 case IIO_CHAN_INFO_RAW:
150 ret = iio_device_claim_direct_mode(indio_dev);
154 ret = ad7606_scan_direct(indio_dev, chan->address);
155 iio_device_release_direct_mode(indio_dev);
161 case IIO_CHAN_INFO_SCALE:
162 *val = scale_avail[st->range][0];
163 *val2 = scale_avail[st->range][1];
164 return IIO_VAL_INT_PLUS_MICRO;
165 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
166 *val = st->oversampling;
172 static ssize_t in_voltage_scale_available_show(struct device *dev,
173 struct device_attribute *attr,
178 for (i = 0; i < ARRAY_SIZE(scale_avail); i++)
179 len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%06u ",
180 scale_avail[i][0], scale_avail[i][1]);
187 static IIO_DEVICE_ATTR_RO(in_voltage_scale_available, 0);
189 static int ad7606_oversampling_get_index(unsigned int val)
191 unsigned char supported[] = {1, 2, 4, 8, 16, 32, 64};
194 for (i = 0; i < ARRAY_SIZE(supported); i++)
195 if (val == supported[i])
201 static int ad7606_write_raw(struct iio_dev *indio_dev,
202 struct iio_chan_spec const *chan,
207 struct ad7606_state *st = iio_priv(indio_dev);
208 DECLARE_BITMAP(values, 3);
212 case IIO_CHAN_INFO_SCALE:
214 mutex_lock(&st->lock);
215 for (i = 0; i < ARRAY_SIZE(scale_avail); i++)
216 if (val2 == scale_avail[i][1]) {
217 gpiod_set_value(st->gpio_range, i);
223 mutex_unlock(&st->lock);
226 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
229 ret = ad7606_oversampling_get_index(val);
235 mutex_lock(&st->lock);
236 gpiod_set_array_value(3, st->gpio_os->desc, st->gpio_os->info,
238 st->oversampling = val;
239 mutex_unlock(&st->lock);
247 static IIO_CONST_ATTR(oversampling_ratio_available, "1 2 4 8 16 32 64");
249 static struct attribute *ad7606_attributes_os_and_range[] = {
250 &iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
251 &iio_const_attr_oversampling_ratio_available.dev_attr.attr,
255 static const struct attribute_group ad7606_attribute_group_os_and_range = {
256 .attrs = ad7606_attributes_os_and_range,
259 static struct attribute *ad7606_attributes_os[] = {
260 &iio_const_attr_oversampling_ratio_available.dev_attr.attr,
264 static const struct attribute_group ad7606_attribute_group_os = {
265 .attrs = ad7606_attributes_os,
268 static struct attribute *ad7606_attributes_range[] = {
269 &iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
273 static const struct attribute_group ad7606_attribute_group_range = {
274 .attrs = ad7606_attributes_range,
277 #define AD760X_CHANNEL(num, mask) \
279 .type = IIO_VOLTAGE, \
283 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
284 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),\
285 .info_mask_shared_by_all = mask, \
291 .endianness = IIO_CPU, \
295 #define AD7605_CHANNEL(num) \
296 AD760X_CHANNEL(num, 0)
298 #define AD7606_CHANNEL(num) \
299 AD760X_CHANNEL(num, BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO))
301 static const struct iio_chan_spec ad7605_channels[] = {
302 IIO_CHAN_SOFT_TIMESTAMP(4),
309 static const struct iio_chan_spec ad7606_channels[] = {
310 IIO_CHAN_SOFT_TIMESTAMP(8),
321 static const struct ad7606_chip_info ad7606_chip_info_tbl[] = {
323 * More devices added in future
326 .channels = ad7605_channels,
330 .channels = ad7606_channels,
332 .has_oversampling = true,
335 .channels = ad7606_channels,
337 .has_oversampling = true,
340 .channels = ad7606_channels,
342 .has_oversampling = true,
346 static int ad7606_request_gpios(struct ad7606_state *st)
348 struct device *dev = st->dev;
350 st->gpio_convst = devm_gpiod_get(dev, "conversion-start",
352 if (IS_ERR(st->gpio_convst))
353 return PTR_ERR(st->gpio_convst);
355 st->gpio_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
356 if (IS_ERR(st->gpio_reset))
357 return PTR_ERR(st->gpio_reset);
359 st->gpio_range = devm_gpiod_get_optional(dev, "range", GPIOD_OUT_LOW);
360 if (IS_ERR(st->gpio_range))
361 return PTR_ERR(st->gpio_range);
363 st->gpio_standby = devm_gpiod_get_optional(dev, "standby",
365 if (IS_ERR(st->gpio_standby))
366 return PTR_ERR(st->gpio_standby);
368 st->gpio_frstdata = devm_gpiod_get_optional(dev, "first-data",
370 if (IS_ERR(st->gpio_frstdata))
371 return PTR_ERR(st->gpio_frstdata);
373 if (!st->chip_info->has_oversampling)
376 st->gpio_os = devm_gpiod_get_array_optional(dev, "oversampling-ratio",
378 return PTR_ERR_OR_ZERO(st->gpio_os);
384 static irqreturn_t ad7606_interrupt(int irq, void *dev_id)
386 struct iio_dev *indio_dev = dev_id;
387 struct ad7606_state *st = iio_priv(indio_dev);
389 if (iio_buffer_enabled(indio_dev)) {
390 schedule_work(&st->poll_work);
393 wake_up_interruptible(&st->wq_data_avail);
399 static const struct iio_info ad7606_info_no_os_or_range = {
400 .read_raw = &ad7606_read_raw,
403 static const struct iio_info ad7606_info_os_and_range = {
404 .read_raw = &ad7606_read_raw,
405 .write_raw = &ad7606_write_raw,
406 .attrs = &ad7606_attribute_group_os_and_range,
409 static const struct iio_info ad7606_info_os = {
410 .read_raw = &ad7606_read_raw,
411 .write_raw = &ad7606_write_raw,
412 .attrs = &ad7606_attribute_group_os,
415 static const struct iio_info ad7606_info_range = {
416 .read_raw = &ad7606_read_raw,
417 .write_raw = &ad7606_write_raw,
418 .attrs = &ad7606_attribute_group_range,
421 int ad7606_probe(struct device *dev, int irq, void __iomem *base_address,
422 const char *name, unsigned int id,
423 const struct ad7606_bus_ops *bops)
425 struct ad7606_state *st;
427 struct iio_dev *indio_dev;
429 indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
433 st = iio_priv(indio_dev);
436 mutex_init(&st->lock);
438 st->base_address = base_address;
439 /* tied to logic low, analog input range is +/- 5V */
441 st->oversampling = 1;
442 INIT_WORK(&st->poll_work, &ad7606_poll_bh_to_ring);
444 st->reg = devm_regulator_get(dev, "avcc");
446 return PTR_ERR(st->reg);
448 ret = regulator_enable(st->reg);
450 dev_err(dev, "Failed to enable specified AVcc supply\n");
454 st->chip_info = &ad7606_chip_info_tbl[id];
456 ret = ad7606_request_gpios(st);
458 goto error_disable_reg;
460 indio_dev->dev.parent = dev;
463 indio_dev->info = &ad7606_info_os_and_range;
465 indio_dev->info = &ad7606_info_os;
468 indio_dev->info = &ad7606_info_range;
470 indio_dev->info = &ad7606_info_no_os_or_range;
472 indio_dev->modes = INDIO_DIRECT_MODE;
473 indio_dev->name = name;
474 indio_dev->channels = st->chip_info->channels;
475 indio_dev->num_channels = st->chip_info->num_channels;
477 init_waitqueue_head(&st->wq_data_avail);
479 ret = ad7606_reset(st);
481 dev_warn(st->dev, "failed to RESET: no RESET GPIO specified\n");
483 ret = request_irq(irq, ad7606_interrupt, IRQF_TRIGGER_FALLING, name,
486 goto error_disable_reg;
488 ret = iio_triggered_buffer_setup(indio_dev, &ad7606_trigger_handler,
493 ret = iio_device_register(indio_dev);
495 goto error_unregister_ring;
497 dev_set_drvdata(dev, indio_dev);
500 error_unregister_ring:
501 iio_triggered_buffer_cleanup(indio_dev);
504 free_irq(irq, indio_dev);
507 regulator_disable(st->reg);
510 EXPORT_SYMBOL_GPL(ad7606_probe);
512 int ad7606_remove(struct device *dev, int irq)
514 struct iio_dev *indio_dev = dev_get_drvdata(dev);
515 struct ad7606_state *st = iio_priv(indio_dev);
517 iio_device_unregister(indio_dev);
518 iio_triggered_buffer_cleanup(indio_dev);
520 free_irq(irq, indio_dev);
521 regulator_disable(st->reg);
525 EXPORT_SYMBOL_GPL(ad7606_remove);
527 #ifdef CONFIG_PM_SLEEP
529 static int ad7606_suspend(struct device *dev)
531 struct iio_dev *indio_dev = dev_get_drvdata(dev);
532 struct ad7606_state *st = iio_priv(indio_dev);
534 if (st->gpio_standby) {
535 gpiod_set_value(st->gpio_range, 1);
536 gpiod_set_value(st->gpio_standby, 0);
542 static int ad7606_resume(struct device *dev)
544 struct iio_dev *indio_dev = dev_get_drvdata(dev);
545 struct ad7606_state *st = iio_priv(indio_dev);
547 if (st->gpio_standby) {
548 gpiod_set_value(st->gpio_range, st->range);
549 gpiod_set_value(st->gpio_standby, 1);
556 SIMPLE_DEV_PM_OPS(ad7606_pm_ops, ad7606_suspend, ad7606_resume);
557 EXPORT_SYMBOL_GPL(ad7606_pm_ops);
561 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
562 MODULE_DESCRIPTION("Analog Devices AD7606 ADC");
563 MODULE_LICENSE("GPL v2");