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>
29 /* Scales are computed as 2.5/2**16 and 5/2**16 respectively */
30 static const unsigned int scale_avail[2][2] = {
31 {0, 38147}, {0, 76294}
34 static int ad7606_reset(struct ad7606_state *st)
37 gpiod_set_value(st->gpio_reset, 1);
38 ndelay(100); /* t_reset >= 100ns */
39 gpiod_set_value(st->gpio_reset, 0);
46 static int ad7606_read_samples(struct ad7606_state *st)
48 unsigned int num = st->chip_info->num_channels;
53 * The frstdata signal is set to high while and after reading the sample
54 * of the first channel and low for all other channels. This can be used
55 * to check that the incoming data is correctly aligned. During normal
56 * operation the data should never become unaligned, but some glitch or
57 * electrostatic discharge might cause an extra read or clock cycle.
58 * Monitoring the frstdata signal allows to recover from such failure
62 if (st->gpio_frstdata) {
63 ret = st->bops->read_block(st->dev, 1, data);
67 if (!gpiod_get_value(st->gpio_frstdata)) {
76 return st->bops->read_block(st->dev, num, data);
79 static irqreturn_t ad7606_trigger_handler(int irq, void *p)
81 struct iio_poll_func *pf = p;
82 struct ad7606_state *st = iio_priv(pf->indio_dev);
84 gpiod_set_value(st->gpio_convst, 1);
90 * ad7606_poll_bh_to_ring() bh of trigger launched polling to ring buffer
91 * @work_s: the work struct through which this was scheduled
93 * Currently there is no option in this driver to disable the saving of
94 * timestamps within the ring.
95 * I think the one copy of this at a time was to avoid problems if the
96 * trigger was set far too high and the reads then locked up the computer.
98 static void ad7606_poll_bh_to_ring(struct work_struct *work_s)
100 struct ad7606_state *st = container_of(work_s, struct ad7606_state,
102 struct iio_dev *indio_dev = iio_priv_to_dev(st);
105 ret = ad7606_read_samples(st);
107 iio_push_to_buffers_with_timestamp(indio_dev, st->data,
108 iio_get_time_ns(indio_dev));
110 gpiod_set_value(st->gpio_convst, 0);
111 iio_trigger_notify_done(indio_dev->trig);
114 static int ad7606_scan_direct(struct iio_dev *indio_dev, unsigned int ch)
116 struct ad7606_state *st = iio_priv(indio_dev);
120 gpiod_set_value(st->gpio_convst, 1);
122 ret = wait_event_interruptible(st->wq_data_avail, st->done);
126 ret = ad7606_read_samples(st);
131 gpiod_set_value(st->gpio_convst, 0);
136 static int ad7606_read_raw(struct iio_dev *indio_dev,
137 struct iio_chan_spec const *chan,
143 struct ad7606_state *st = iio_priv(indio_dev);
146 case IIO_CHAN_INFO_RAW:
147 ret = iio_device_claim_direct_mode(indio_dev);
151 ret = ad7606_scan_direct(indio_dev, chan->address);
152 iio_device_release_direct_mode(indio_dev);
158 case IIO_CHAN_INFO_SCALE:
159 *val = scale_avail[st->range][0];
160 *val2 = scale_avail[st->range][1];
161 return IIO_VAL_INT_PLUS_MICRO;
162 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
163 *val = st->oversampling;
169 static ssize_t in_voltage_scale_available_show(struct device *dev,
170 struct device_attribute *attr,
175 for (i = 0; i < ARRAY_SIZE(scale_avail); i++)
176 len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%06u ",
177 scale_avail[i][0], scale_avail[i][1]);
184 static IIO_DEVICE_ATTR_RO(in_voltage_scale_available, 0);
186 static int ad7606_oversampling_get_index(unsigned int val)
188 unsigned char supported[] = {1, 2, 4, 8, 16, 32, 64};
191 for (i = 0; i < ARRAY_SIZE(supported); i++)
192 if (val == supported[i])
198 static int ad7606_write_raw(struct iio_dev *indio_dev,
199 struct iio_chan_spec const *chan,
204 struct ad7606_state *st = iio_priv(indio_dev);
209 case IIO_CHAN_INFO_SCALE:
211 mutex_lock(&st->lock);
212 for (i = 0; i < ARRAY_SIZE(scale_avail); i++)
213 if (val2 == scale_avail[i][1]) {
214 gpiod_set_value(st->gpio_range, i);
220 mutex_unlock(&st->lock);
223 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
226 ret = ad7606_oversampling_get_index(val);
230 values[0] = (ret >> 0) & 1;
231 values[1] = (ret >> 1) & 1;
232 values[2] = (ret >> 2) & 1;
234 mutex_lock(&st->lock);
235 gpiod_set_array_value(ARRAY_SIZE(values), st->gpio_os->desc,
237 st->oversampling = val;
238 mutex_unlock(&st->lock);
246 static IIO_CONST_ATTR(oversampling_ratio_available, "1 2 4 8 16 32 64");
248 static struct attribute *ad7606_attributes_os_and_range[] = {
249 &iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
250 &iio_const_attr_oversampling_ratio_available.dev_attr.attr,
254 static const struct attribute_group ad7606_attribute_group_os_and_range = {
255 .attrs = ad7606_attributes_os_and_range,
258 static struct attribute *ad7606_attributes_os[] = {
259 &iio_const_attr_oversampling_ratio_available.dev_attr.attr,
263 static const struct attribute_group ad7606_attribute_group_os = {
264 .attrs = ad7606_attributes_os,
267 static struct attribute *ad7606_attributes_range[] = {
268 &iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
272 static const struct attribute_group ad7606_attribute_group_range = {
273 .attrs = ad7606_attributes_range,
276 #define AD7606_CHANNEL(num) \
278 .type = IIO_VOLTAGE, \
282 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
283 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),\
284 .info_mask_shared_by_all = \
285 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
291 .endianness = IIO_CPU, \
295 static const struct iio_chan_spec ad7606_channels[] = {
296 IIO_CHAN_SOFT_TIMESTAMP(8),
307 static const struct ad7606_chip_info ad7606_chip_info_tbl[] = {
309 * More devices added in future
312 .channels = ad7606_channels,
316 .channels = ad7606_channels,
320 .channels = ad7606_channels,
325 static int ad7606_request_gpios(struct ad7606_state *st)
327 struct device *dev = st->dev;
329 st->gpio_convst = devm_gpiod_get(dev, "conversion-start",
331 if (IS_ERR(st->gpio_convst))
332 return PTR_ERR(st->gpio_convst);
334 st->gpio_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
335 if (IS_ERR(st->gpio_reset))
336 return PTR_ERR(st->gpio_reset);
338 st->gpio_range = devm_gpiod_get_optional(dev, "range", GPIOD_OUT_LOW);
339 if (IS_ERR(st->gpio_range))
340 return PTR_ERR(st->gpio_range);
342 st->gpio_standby = devm_gpiod_get_optional(dev, "standby",
344 if (IS_ERR(st->gpio_standby))
345 return PTR_ERR(st->gpio_standby);
347 st->gpio_frstdata = devm_gpiod_get_optional(dev, "first-data",
349 if (IS_ERR(st->gpio_frstdata))
350 return PTR_ERR(st->gpio_frstdata);
352 st->gpio_os = devm_gpiod_get_array_optional(dev, "oversampling-ratio",
354 return PTR_ERR_OR_ZERO(st->gpio_os);
360 static irqreturn_t ad7606_interrupt(int irq, void *dev_id)
362 struct iio_dev *indio_dev = dev_id;
363 struct ad7606_state *st = iio_priv(indio_dev);
365 if (iio_buffer_enabled(indio_dev)) {
366 schedule_work(&st->poll_work);
369 wake_up_interruptible(&st->wq_data_avail);
375 static const struct iio_info ad7606_info_no_os_or_range = {
376 .driver_module = THIS_MODULE,
377 .read_raw = &ad7606_read_raw,
380 static const struct iio_info ad7606_info_os_and_range = {
381 .driver_module = THIS_MODULE,
382 .read_raw = &ad7606_read_raw,
383 .write_raw = &ad7606_write_raw,
384 .attrs = &ad7606_attribute_group_os_and_range,
387 static const struct iio_info ad7606_info_os = {
388 .driver_module = THIS_MODULE,
389 .read_raw = &ad7606_read_raw,
390 .write_raw = &ad7606_write_raw,
391 .attrs = &ad7606_attribute_group_os,
394 static const struct iio_info ad7606_info_range = {
395 .driver_module = THIS_MODULE,
396 .read_raw = &ad7606_read_raw,
397 .write_raw = &ad7606_write_raw,
398 .attrs = &ad7606_attribute_group_range,
401 int ad7606_probe(struct device *dev, int irq, void __iomem *base_address,
402 const char *name, unsigned int id,
403 const struct ad7606_bus_ops *bops)
405 struct ad7606_state *st;
407 struct iio_dev *indio_dev;
409 indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
413 st = iio_priv(indio_dev);
416 mutex_init(&st->lock);
418 st->base_address = base_address;
419 /* tied to logic low, analog input range is +/- 5V */
421 st->oversampling = 1;
422 INIT_WORK(&st->poll_work, &ad7606_poll_bh_to_ring);
424 st->reg = devm_regulator_get(dev, "avcc");
426 return PTR_ERR(st->reg);
428 ret = regulator_enable(st->reg);
430 dev_err(dev, "Failed to enable specified AVcc supply\n");
434 ret = ad7606_request_gpios(st);
436 goto error_disable_reg;
438 st->chip_info = &ad7606_chip_info_tbl[id];
440 indio_dev->dev.parent = dev;
443 indio_dev->info = &ad7606_info_os_and_range;
445 indio_dev->info = &ad7606_info_os;
448 indio_dev->info = &ad7606_info_range;
450 indio_dev->info = &ad7606_info_no_os_or_range;
452 indio_dev->modes = INDIO_DIRECT_MODE;
453 indio_dev->name = name;
454 indio_dev->channels = st->chip_info->channels;
455 indio_dev->num_channels = st->chip_info->num_channels;
457 init_waitqueue_head(&st->wq_data_avail);
459 ret = ad7606_reset(st);
461 dev_warn(st->dev, "failed to RESET: no RESET GPIO specified\n");
463 ret = request_irq(irq, ad7606_interrupt, IRQF_TRIGGER_FALLING, name,
466 goto error_disable_reg;
468 ret = iio_triggered_buffer_setup(indio_dev, &ad7606_trigger_handler,
473 ret = iio_device_register(indio_dev);
475 goto error_unregister_ring;
477 dev_set_drvdata(dev, indio_dev);
480 error_unregister_ring:
481 iio_triggered_buffer_cleanup(indio_dev);
484 free_irq(irq, indio_dev);
487 regulator_disable(st->reg);
490 EXPORT_SYMBOL_GPL(ad7606_probe);
492 int ad7606_remove(struct device *dev, int irq)
494 struct iio_dev *indio_dev = dev_get_drvdata(dev);
495 struct ad7606_state *st = iio_priv(indio_dev);
497 iio_device_unregister(indio_dev);
498 iio_triggered_buffer_cleanup(indio_dev);
500 free_irq(irq, indio_dev);
501 regulator_disable(st->reg);
505 EXPORT_SYMBOL_GPL(ad7606_remove);
507 #ifdef CONFIG_PM_SLEEP
509 static int ad7606_suspend(struct device *dev)
511 struct iio_dev *indio_dev = dev_get_drvdata(dev);
512 struct ad7606_state *st = iio_priv(indio_dev);
514 if (st->gpio_standby) {
515 gpiod_set_value(st->gpio_range, 1);
516 gpiod_set_value(st->gpio_standby, 0);
522 static int ad7606_resume(struct device *dev)
524 struct iio_dev *indio_dev = dev_get_drvdata(dev);
525 struct ad7606_state *st = iio_priv(indio_dev);
527 if (st->gpio_standby) {
528 gpiod_set_value(st->gpio_range, st->range);
529 gpiod_set_value(st->gpio_standby, 1);
536 SIMPLE_DEV_PM_OPS(ad7606_pm_ops, ad7606_suspend, ad7606_resume);
537 EXPORT_SYMBOL_GPL(ad7606_pm_ops);
541 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
542 MODULE_DESCRIPTION("Analog Devices AD7606 ADC");
543 MODULE_LICENSE("GPL v2");