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);
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);
233 values[0] = (ret >> 0) & 1;
234 values[1] = (ret >> 1) & 1;
235 values[2] = (ret >> 2) & 1;
237 mutex_lock(&st->lock);
238 gpiod_set_array_value(ARRAY_SIZE(values), st->gpio_os->desc,
240 st->oversampling = val;
241 mutex_unlock(&st->lock);
249 static IIO_CONST_ATTR(oversampling_ratio_available, "1 2 4 8 16 32 64");
251 static struct attribute *ad7606_attributes_os_and_range[] = {
252 &iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
253 &iio_const_attr_oversampling_ratio_available.dev_attr.attr,
257 static const struct attribute_group ad7606_attribute_group_os_and_range = {
258 .attrs = ad7606_attributes_os_and_range,
261 static struct attribute *ad7606_attributes_os[] = {
262 &iio_const_attr_oversampling_ratio_available.dev_attr.attr,
266 static const struct attribute_group ad7606_attribute_group_os = {
267 .attrs = ad7606_attributes_os,
270 static struct attribute *ad7606_attributes_range[] = {
271 &iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
275 static const struct attribute_group ad7606_attribute_group_range = {
276 .attrs = ad7606_attributes_range,
279 #define AD7606_CHANNEL(num) \
281 .type = IIO_VOLTAGE, \
285 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
286 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),\
287 .info_mask_shared_by_all = \
288 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
294 .endianness = IIO_CPU, \
298 static const struct iio_chan_spec ad7606_channels[] = {
299 IIO_CHAN_SOFT_TIMESTAMP(8),
310 static const struct ad7606_chip_info ad7606_chip_info_tbl[] = {
312 * More devices added in future
315 .channels = ad7606_channels,
319 .channels = ad7606_channels,
323 .channels = ad7606_channels,
328 static int ad7606_request_gpios(struct ad7606_state *st)
330 struct device *dev = st->dev;
332 st->gpio_convst = devm_gpiod_get(dev, "conversion-start",
334 if (IS_ERR(st->gpio_convst))
335 return PTR_ERR(st->gpio_convst);
337 st->gpio_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
338 if (IS_ERR(st->gpio_reset))
339 return PTR_ERR(st->gpio_reset);
341 st->gpio_range = devm_gpiod_get_optional(dev, "range", GPIOD_OUT_LOW);
342 if (IS_ERR(st->gpio_range))
343 return PTR_ERR(st->gpio_range);
345 st->gpio_standby = devm_gpiod_get_optional(dev, "standby",
347 if (IS_ERR(st->gpio_standby))
348 return PTR_ERR(st->gpio_standby);
350 st->gpio_frstdata = devm_gpiod_get_optional(dev, "first-data",
352 if (IS_ERR(st->gpio_frstdata))
353 return PTR_ERR(st->gpio_frstdata);
355 st->gpio_os = devm_gpiod_get_array_optional(dev, "oversampling-ratio",
357 return PTR_ERR_OR_ZERO(st->gpio_os);
363 static irqreturn_t ad7606_interrupt(int irq, void *dev_id)
365 struct iio_dev *indio_dev = dev_id;
366 struct ad7606_state *st = iio_priv(indio_dev);
368 if (iio_buffer_enabled(indio_dev)) {
369 schedule_work(&st->poll_work);
372 wake_up_interruptible(&st->wq_data_avail);
378 static const struct iio_info ad7606_info_no_os_or_range = {
379 .driver_module = THIS_MODULE,
380 .read_raw = &ad7606_read_raw,
383 static const struct iio_info ad7606_info_os_and_range = {
384 .driver_module = THIS_MODULE,
385 .read_raw = &ad7606_read_raw,
386 .write_raw = &ad7606_write_raw,
387 .attrs = &ad7606_attribute_group_os_and_range,
390 static const struct iio_info ad7606_info_os = {
391 .driver_module = THIS_MODULE,
392 .read_raw = &ad7606_read_raw,
393 .write_raw = &ad7606_write_raw,
394 .attrs = &ad7606_attribute_group_os,
397 static const struct iio_info ad7606_info_range = {
398 .driver_module = THIS_MODULE,
399 .read_raw = &ad7606_read_raw,
400 .write_raw = &ad7606_write_raw,
401 .attrs = &ad7606_attribute_group_range,
404 int ad7606_probe(struct device *dev, int irq, void __iomem *base_address,
405 const char *name, unsigned int id,
406 const struct ad7606_bus_ops *bops)
408 struct ad7606_state *st;
410 struct iio_dev *indio_dev;
412 indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
416 st = iio_priv(indio_dev);
419 mutex_init(&st->lock);
421 st->base_address = base_address;
422 /* tied to logic low, analog input range is +/- 5V */
424 st->oversampling = 1;
425 INIT_WORK(&st->poll_work, &ad7606_poll_bh_to_ring);
427 st->reg = devm_regulator_get(dev, "avcc");
429 return PTR_ERR(st->reg);
431 ret = regulator_enable(st->reg);
433 dev_err(dev, "Failed to enable specified AVcc supply\n");
437 ret = ad7606_request_gpios(st);
439 goto error_disable_reg;
441 st->chip_info = &ad7606_chip_info_tbl[id];
443 indio_dev->dev.parent = dev;
446 indio_dev->info = &ad7606_info_os_and_range;
448 indio_dev->info = &ad7606_info_os;
451 indio_dev->info = &ad7606_info_range;
453 indio_dev->info = &ad7606_info_no_os_or_range;
455 indio_dev->modes = INDIO_DIRECT_MODE;
456 indio_dev->name = name;
457 indio_dev->channels = st->chip_info->channels;
458 indio_dev->num_channels = st->chip_info->num_channels;
460 init_waitqueue_head(&st->wq_data_avail);
462 ret = ad7606_reset(st);
464 dev_warn(st->dev, "failed to RESET: no RESET GPIO specified\n");
466 ret = request_irq(irq, ad7606_interrupt, IRQF_TRIGGER_FALLING, name,
469 goto error_disable_reg;
471 ret = iio_triggered_buffer_setup(indio_dev, &ad7606_trigger_handler,
476 ret = iio_device_register(indio_dev);
478 goto error_unregister_ring;
480 dev_set_drvdata(dev, indio_dev);
483 error_unregister_ring:
484 iio_triggered_buffer_cleanup(indio_dev);
487 free_irq(irq, indio_dev);
490 regulator_disable(st->reg);
493 EXPORT_SYMBOL_GPL(ad7606_probe);
495 int ad7606_remove(struct device *dev, int irq)
497 struct iio_dev *indio_dev = dev_get_drvdata(dev);
498 struct ad7606_state *st = iio_priv(indio_dev);
500 iio_device_unregister(indio_dev);
501 iio_triggered_buffer_cleanup(indio_dev);
503 free_irq(irq, indio_dev);
504 regulator_disable(st->reg);
508 EXPORT_SYMBOL_GPL(ad7606_remove);
510 #ifdef CONFIG_PM_SLEEP
512 static int ad7606_suspend(struct device *dev)
514 struct iio_dev *indio_dev = dev_get_drvdata(dev);
515 struct ad7606_state *st = iio_priv(indio_dev);
517 if (st->gpio_standby) {
518 gpiod_set_value(st->gpio_range, 1);
519 gpiod_set_value(st->gpio_standby, 0);
525 static int ad7606_resume(struct device *dev)
527 struct iio_dev *indio_dev = dev_get_drvdata(dev);
528 struct ad7606_state *st = iio_priv(indio_dev);
530 if (st->gpio_standby) {
531 gpiod_set_value(st->gpio_range, st->range);
532 gpiod_set_value(st->gpio_standby, 1);
539 SIMPLE_DEV_PM_OPS(ad7606_pm_ops, ad7606_suspend, ad7606_resume);
540 EXPORT_SYMBOL_GPL(ad7606_pm_ops);
544 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
545 MODULE_DESCRIPTION("Analog Devices AD7606 ADC");
546 MODULE_LICENSE("GPL v2");