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>
27 int ad7606_reset(struct ad7606_state *st)
30 gpiod_set_value(st->gpio_reset, 1);
31 ndelay(100); /* t_reset >= 100ns */
32 gpiod_set_value(st->gpio_reset, 0);
39 int ad7606_read_samples(struct ad7606_state *st)
41 unsigned int num = st->chip_info->num_channels;
46 * The frstdata signal is set to high while and after reading the sample
47 * of the first channel and low for all other channels. This can be used
48 * to check that the incoming data is correctly aligned. During normal
49 * operation the data should never become unaligned, but some glitch or
50 * electrostatic discharge might cause an extra read or clock cycle.
51 * Monitoring the frstdata signal allows to recover from such failure
55 if (st->gpio_frstdata) {
56 ret = st->bops->read_block(st->dev, 1, data);
60 if (!gpiod_get_value(st->gpio_frstdata)) {
69 return st->bops->read_block(st->dev, num, data);
72 static int ad7606_scan_direct(struct iio_dev *indio_dev, unsigned int ch)
74 struct ad7606_state *st = iio_priv(indio_dev);
78 gpiod_set_value(st->gpio_convst, 1);
80 ret = wait_event_interruptible(st->wq_data_avail, st->done);
84 ret = ad7606_read_samples(st);
89 gpiod_set_value(st->gpio_convst, 0);
94 static int ad7606_read_raw(struct iio_dev *indio_dev,
95 struct iio_chan_spec const *chan,
101 struct ad7606_state *st = iio_priv(indio_dev);
104 case IIO_CHAN_INFO_RAW:
105 ret = iio_device_claim_direct_mode(indio_dev);
109 ret = ad7606_scan_direct(indio_dev, chan->address);
110 iio_device_release_direct_mode(indio_dev);
116 case IIO_CHAN_INFO_SCALE:
117 *val = st->range * 2;
118 *val2 = st->chip_info->channels[0].scan_type.realbits;
119 return IIO_VAL_FRACTIONAL_LOG2;
120 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
121 *val = st->oversampling;
127 static ssize_t ad7606_show_range(struct device *dev,
128 struct device_attribute *attr, char *buf)
130 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
131 struct ad7606_state *st = iio_priv(indio_dev);
133 return sprintf(buf, "%u\n", st->range);
136 static ssize_t ad7606_store_range(struct device *dev,
137 struct device_attribute *attr,
138 const char *buf, size_t count)
140 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
141 struct ad7606_state *st = iio_priv(indio_dev);
145 ret = kstrtoul(buf, 10, &lval);
149 if (!(lval == 5000 || lval == 10000))
152 mutex_lock(&indio_dev->mlock);
153 gpiod_set_value(st->gpio_range, lval == 10000);
155 mutex_unlock(&indio_dev->mlock);
160 static IIO_DEVICE_ATTR(in_voltage_range, S_IRUGO | S_IWUSR,
161 ad7606_show_range, ad7606_store_range, 0);
162 static IIO_CONST_ATTR(in_voltage_range_available, "5000 10000");
164 static int ad7606_oversampling_get_index(unsigned int val)
166 unsigned char supported[] = {1, 2, 4, 8, 16, 32, 64};
169 for (i = 0; i < ARRAY_SIZE(supported); i++)
170 if (val == supported[i])
176 static int ad7606_write_raw(struct iio_dev *indio_dev,
177 struct iio_chan_spec const *chan,
182 struct ad7606_state *st = iio_priv(indio_dev);
187 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
190 ret = ad7606_oversampling_get_index(val);
194 values[0] = (ret >> 0) & 1;
195 values[1] = (ret >> 1) & 1;
196 values[2] = (ret >> 2) & 1;
198 mutex_lock(&indio_dev->mlock);
199 gpiod_set_array_value(ARRAY_SIZE(values), st->gpio_os->desc,
201 st->oversampling = val;
202 mutex_unlock(&indio_dev->mlock);
210 static IIO_CONST_ATTR(oversampling_ratio_available, "1 2 4 8 16 32 64");
212 static struct attribute *ad7606_attributes_os_and_range[] = {
213 &iio_dev_attr_in_voltage_range.dev_attr.attr,
214 &iio_const_attr_in_voltage_range_available.dev_attr.attr,
215 &iio_const_attr_oversampling_ratio_available.dev_attr.attr,
219 static const struct attribute_group ad7606_attribute_group_os_and_range = {
220 .attrs = ad7606_attributes_os_and_range,
223 static struct attribute *ad7606_attributes_os[] = {
224 &iio_const_attr_oversampling_ratio_available.dev_attr.attr,
228 static const struct attribute_group ad7606_attribute_group_os = {
229 .attrs = ad7606_attributes_os,
232 static struct attribute *ad7606_attributes_range[] = {
233 &iio_dev_attr_in_voltage_range.dev_attr.attr,
234 &iio_const_attr_in_voltage_range_available.dev_attr.attr,
238 static const struct attribute_group ad7606_attribute_group_range = {
239 .attrs = ad7606_attributes_range,
242 #define AD7606_CHANNEL(num) \
244 .type = IIO_VOLTAGE, \
248 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
249 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),\
250 .info_mask_shared_by_all = \
251 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
257 .endianness = IIO_CPU, \
261 static const struct iio_chan_spec ad7606_channels[] = {
262 IIO_CHAN_SOFT_TIMESTAMP(8),
273 static const struct ad7606_chip_info ad7606_chip_info_tbl[] = {
275 * More devices added in future
278 .channels = ad7606_channels,
282 .channels = ad7606_channels,
286 .channels = ad7606_channels,
291 static int ad7606_request_gpios(struct ad7606_state *st)
293 struct device *dev = st->dev;
295 st->gpio_convst = devm_gpiod_get(dev, "conversion-start",
297 if (IS_ERR(st->gpio_convst))
298 return PTR_ERR(st->gpio_convst);
300 st->gpio_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
301 if (IS_ERR(st->gpio_reset))
302 return PTR_ERR(st->gpio_reset);
304 st->gpio_range = devm_gpiod_get_optional(dev, "range", GPIOD_OUT_LOW);
305 if (IS_ERR(st->gpio_range))
306 return PTR_ERR(st->gpio_range);
308 st->gpio_standby = devm_gpiod_get_optional(dev, "standby",
310 if (IS_ERR(st->gpio_standby))
311 return PTR_ERR(st->gpio_standby);
313 st->gpio_frstdata = devm_gpiod_get_optional(dev, "first-data",
315 if (IS_ERR(st->gpio_frstdata))
316 return PTR_ERR(st->gpio_frstdata);
318 st->gpio_os = devm_gpiod_get_array_optional(dev, "oversampling-ratio",
320 if (IS_ERR(st->gpio_os))
321 return PTR_ERR(st->gpio_os);
329 static irqreturn_t ad7606_interrupt(int irq, void *dev_id)
331 struct iio_dev *indio_dev = dev_id;
332 struct ad7606_state *st = iio_priv(indio_dev);
334 if (iio_buffer_enabled(indio_dev)) {
335 schedule_work(&st->poll_work);
338 wake_up_interruptible(&st->wq_data_avail);
344 static const struct iio_info ad7606_info_no_os_or_range = {
345 .driver_module = THIS_MODULE,
346 .read_raw = &ad7606_read_raw,
349 static const struct iio_info ad7606_info_os_and_range = {
350 .driver_module = THIS_MODULE,
351 .read_raw = &ad7606_read_raw,
352 .write_raw = &ad7606_write_raw,
353 .attrs = &ad7606_attribute_group_os_and_range,
356 static const struct iio_info ad7606_info_os = {
357 .driver_module = THIS_MODULE,
358 .read_raw = &ad7606_read_raw,
359 .write_raw = &ad7606_write_raw,
360 .attrs = &ad7606_attribute_group_os,
363 static const struct iio_info ad7606_info_range = {
364 .driver_module = THIS_MODULE,
365 .read_raw = &ad7606_read_raw,
366 .attrs = &ad7606_attribute_group_range,
369 int ad7606_probe(struct device *dev, int irq, void __iomem *base_address,
370 const char *name, unsigned int id,
371 const struct ad7606_bus_ops *bops)
373 struct ad7606_state *st;
375 struct iio_dev *indio_dev;
377 indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
381 st = iio_priv(indio_dev);
385 st->base_address = base_address;
387 st->oversampling = 1;
389 st->reg = devm_regulator_get(dev, "vcc");
390 if (!IS_ERR(st->reg)) {
391 ret = regulator_enable(st->reg);
396 ret = ad7606_request_gpios(st);
398 goto error_disable_reg;
400 st->chip_info = &ad7606_chip_info_tbl[id];
402 indio_dev->dev.parent = dev;
405 indio_dev->info = &ad7606_info_os_and_range;
407 indio_dev->info = &ad7606_info_os;
410 indio_dev->info = &ad7606_info_range;
412 indio_dev->info = &ad7606_info_no_os_or_range;
414 indio_dev->modes = INDIO_DIRECT_MODE;
415 indio_dev->name = name;
416 indio_dev->channels = st->chip_info->channels;
417 indio_dev->num_channels = st->chip_info->num_channels;
419 init_waitqueue_head(&st->wq_data_avail);
421 ret = ad7606_reset(st);
423 dev_warn(st->dev, "failed to RESET: no RESET GPIO specified\n");
425 ret = request_irq(irq, ad7606_interrupt, IRQF_TRIGGER_FALLING, name,
428 goto error_disable_reg;
430 ret = ad7606_register_ring_funcs_and_init(indio_dev);
434 ret = iio_device_register(indio_dev);
436 goto error_unregister_ring;
438 dev_set_drvdata(dev, indio_dev);
441 error_unregister_ring:
442 ad7606_ring_cleanup(indio_dev);
445 free_irq(irq, indio_dev);
448 if (!IS_ERR(st->reg))
449 regulator_disable(st->reg);
452 EXPORT_SYMBOL_GPL(ad7606_probe);
454 int ad7606_remove(struct device *dev, int irq)
456 struct iio_dev *indio_dev = dev_get_drvdata(dev);
457 struct ad7606_state *st = iio_priv(indio_dev);
459 iio_device_unregister(indio_dev);
460 ad7606_ring_cleanup(indio_dev);
462 free_irq(irq, indio_dev);
463 if (!IS_ERR(st->reg))
464 regulator_disable(st->reg);
468 EXPORT_SYMBOL_GPL(ad7606_remove);
470 #ifdef CONFIG_PM_SLEEP
472 static int ad7606_suspend(struct device *dev)
474 struct iio_dev *indio_dev = dev_get_drvdata(dev);
475 struct ad7606_state *st = iio_priv(indio_dev);
477 if (st->gpio_standby) {
478 gpiod_set_value(st->gpio_range, 1);
479 gpiod_set_value(st->gpio_standby, 0);
485 static int ad7606_resume(struct device *dev)
487 struct iio_dev *indio_dev = dev_get_drvdata(dev);
488 struct ad7606_state *st = iio_priv(indio_dev);
490 if (st->gpio_standby) {
491 gpiod_set_value(st->gpio_range, st->range == 10000);
492 gpiod_set_value(st->gpio_standby, 1);
499 SIMPLE_DEV_PM_OPS(ad7606_pm_ops, ad7606_suspend, ad7606_resume);
500 EXPORT_SYMBOL_GPL(ad7606_pm_ops);
504 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
505 MODULE_DESCRIPTION("Analog Devices AD7606 ADC");
506 MODULE_LICENSE("GPL v2");