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/workqueue.h>
11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/sysfs.h>
15 #include <linux/list.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/err.h>
18 #include <linux/gpio.h>
19 #include <linux/delay.h>
20 #include <linux/sched.h>
24 #include "../ring_generic.h"
29 int ad7606_reset(struct ad7606_state *st)
32 gpio_set_value(st->pdata->gpio_reset, 1);
33 ndelay(100); /* t_reset >= 100ns */
34 gpio_set_value(st->pdata->gpio_reset, 0);
41 static int ad7606_scan_direct(struct ad7606_state *st, unsigned ch)
46 gpio_set_value(st->pdata->gpio_convst, 1);
48 ret = wait_event_interruptible(st->wq_data_avail, st->done);
52 if (st->have_frstdata) {
53 ret = st->bops->read_block(st->dev, 1, st->data);
56 if (!gpio_get_value(st->pdata->gpio_frstdata)) {
57 /* This should never happen */
62 ret = st->bops->read_block(st->dev,
63 st->chip_info->num_channels - 1, &st->data[1]);
67 ret = st->bops->read_block(st->dev,
68 st->chip_info->num_channels, st->data);
76 gpio_set_value(st->pdata->gpio_convst, 0);
81 static int ad7606_read_raw(struct iio_dev *dev_info,
82 struct iio_chan_spec const *chan,
88 struct ad7606_state *st = dev_info->dev_data;
89 unsigned int scale_uv;
93 mutex_lock(&dev_info->mlock);
94 if (iio_ring_enabled(dev_info))
95 ret = ad7606_scan_from_ring(st, chan->address);
97 ret = ad7606_scan_direct(st, chan->address);
98 mutex_unlock(&dev_info->mlock);
104 case (1 << IIO_CHAN_INFO_SCALE_SHARED):
105 scale_uv = (st->range * 1000 * 2)
106 >> st->chip_info->channels[0].scan_type.realbits;
107 *val = scale_uv / 1000;
108 *val2 = (scale_uv % 1000) * 1000;
109 return IIO_VAL_INT_PLUS_MICRO;
114 static ssize_t ad7606_show_range(struct device *dev,
115 struct device_attribute *attr, char *buf)
117 struct iio_dev *dev_info = dev_get_drvdata(dev);
118 struct ad7606_state *st = iio_dev_get_devdata(dev_info);
120 return sprintf(buf, "%u\n", st->range);
123 static ssize_t ad7606_store_range(struct device *dev,
124 struct device_attribute *attr, const char *buf, size_t count)
126 struct iio_dev *dev_info = dev_get_drvdata(dev);
127 struct ad7606_state *st = iio_dev_get_devdata(dev_info);
130 if (strict_strtoul(buf, 10, &lval))
132 if (!(lval == 5000 || lval == 10000)) {
133 dev_err(dev, "range is not supported\n");
136 mutex_lock(&dev_info->mlock);
137 gpio_set_value(st->pdata->gpio_range, lval == 10000);
139 mutex_unlock(&dev_info->mlock);
144 static IIO_DEVICE_ATTR(range, S_IRUGO | S_IWUSR, \
145 ad7606_show_range, ad7606_store_range, 0);
146 static IIO_CONST_ATTR(range_available, "5000 10000");
148 static ssize_t ad7606_show_oversampling_ratio(struct device *dev,
149 struct device_attribute *attr, char *buf)
151 struct iio_dev *dev_info = dev_get_drvdata(dev);
152 struct ad7606_state *st = iio_dev_get_devdata(dev_info);
154 return sprintf(buf, "%u\n", st->oversampling);
157 static int ad7606_oversampling_get_index(unsigned val)
159 unsigned char supported[] = {0, 2, 4, 8, 16, 32, 64};
162 for (i = 0; i < ARRAY_SIZE(supported); i++)
163 if (val == supported[i])
169 static ssize_t ad7606_store_oversampling_ratio(struct device *dev,
170 struct device_attribute *attr, const char *buf, size_t count)
172 struct iio_dev *dev_info = dev_get_drvdata(dev);
173 struct ad7606_state *st = iio_dev_get_devdata(dev_info);
177 if (strict_strtoul(buf, 10, &lval))
180 ret = ad7606_oversampling_get_index(lval);
182 dev_err(dev, "oversampling %lu is not supported\n", lval);
186 mutex_lock(&dev_info->mlock);
187 gpio_set_value(st->pdata->gpio_os0, (ret >> 0) & 1);
188 gpio_set_value(st->pdata->gpio_os1, (ret >> 1) & 1);
189 gpio_set_value(st->pdata->gpio_os1, (ret >> 2) & 1);
190 st->oversampling = lval;
191 mutex_unlock(&dev_info->mlock);
196 static IIO_DEVICE_ATTR(oversampling_ratio, S_IRUGO | S_IWUSR,
197 ad7606_show_oversampling_ratio,
198 ad7606_store_oversampling_ratio, 0);
199 static IIO_CONST_ATTR(oversampling_ratio_available, "0 2 4 8 16 32 64");
201 static struct attribute *ad7606_attributes[] = {
202 &iio_dev_attr_range.dev_attr.attr,
203 &iio_const_attr_range_available.dev_attr.attr,
204 &iio_dev_attr_oversampling_ratio.dev_attr.attr,
205 &iio_const_attr_oversampling_ratio_available.dev_attr.attr,
209 static mode_t ad7606_attr_is_visible(struct kobject *kobj,
210 struct attribute *attr, int n)
212 struct device *dev = container_of(kobj, struct device, kobj);
213 struct iio_dev *dev_info = dev_get_drvdata(dev);
214 struct ad7606_state *st = iio_dev_get_devdata(dev_info);
216 mode_t mode = attr->mode;
219 (attr == &iio_dev_attr_oversampling_ratio.dev_attr.attr ||
221 &iio_const_attr_oversampling_ratio_available.dev_attr.attr))
223 else if (!st->have_range &&
224 (attr == &iio_dev_attr_range.dev_attr.attr ||
225 attr == &iio_const_attr_range_available.dev_attr.attr))
231 static const struct attribute_group ad7606_attribute_group = {
232 .attrs = ad7606_attributes,
233 .is_visible = ad7606_attr_is_visible,
236 static struct iio_chan_spec ad7606_8_channels[] = {
237 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
238 (1 << IIO_CHAN_INFO_SCALE_SHARED),
239 0, 0, IIO_ST('s', 16, 16, 0), 0),
240 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
241 (1 << IIO_CHAN_INFO_SCALE_SHARED),
242 1, 1, IIO_ST('s', 16, 16, 0), 0),
243 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 2, 0,
244 (1 << IIO_CHAN_INFO_SCALE_SHARED),
245 2, 2, IIO_ST('s', 16, 16, 0), 0),
246 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 3, 0,
247 (1 << IIO_CHAN_INFO_SCALE_SHARED),
248 3, 3, IIO_ST('s', 16, 16, 0), 0),
249 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 4, 0,
250 (1 << IIO_CHAN_INFO_SCALE_SHARED),
251 4, 4, IIO_ST('s', 16, 16, 0), 0),
252 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 5, 0,
253 (1 << IIO_CHAN_INFO_SCALE_SHARED),
254 5, 5, IIO_ST('s', 16, 16, 0), 0),
255 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 6, 0,
256 (1 << IIO_CHAN_INFO_SCALE_SHARED),
257 6, 6, IIO_ST('s', 16, 16, 0), 0),
258 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 7, 0,
259 (1 << IIO_CHAN_INFO_SCALE_SHARED),
260 7, 7, IIO_ST('s', 16, 16, 0), 0),
261 IIO_CHAN_SOFT_TIMESTAMP(8),
264 static struct iio_chan_spec ad7606_6_channels[] = {
265 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
266 (1 << IIO_CHAN_INFO_SCALE_SHARED),
267 0, 0, IIO_ST('s', 16, 16, 0), 0),
268 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
269 (1 << IIO_CHAN_INFO_SCALE_SHARED),
270 1, 1, IIO_ST('s', 16, 16, 0), 0),
271 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 2, 0,
272 (1 << IIO_CHAN_INFO_SCALE_SHARED),
273 2, 2, IIO_ST('s', 16, 16, 0), 0),
274 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 3, 0,
275 (1 << IIO_CHAN_INFO_SCALE_SHARED),
276 3, 3, IIO_ST('s', 16, 16, 0), 0),
277 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 4, 0,
278 (1 << IIO_CHAN_INFO_SCALE_SHARED),
279 4, 4, IIO_ST('s', 16, 16, 0), 0),
280 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 5, 0,
281 (1 << IIO_CHAN_INFO_SCALE_SHARED),
282 5, 5, IIO_ST('s', 16, 16, 0), 0),
283 IIO_CHAN_SOFT_TIMESTAMP(6),
286 static struct iio_chan_spec ad7606_4_channels[] = {
287 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
288 (1 << IIO_CHAN_INFO_SCALE_SHARED),
289 0, 0, IIO_ST('s', 16, 16, 0), 0),
290 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
291 (1 << IIO_CHAN_INFO_SCALE_SHARED),
292 1, 1, IIO_ST('s', 16, 16, 0), 0),
293 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 2, 0,
294 (1 << IIO_CHAN_INFO_SCALE_SHARED),
295 2, 2, IIO_ST('s', 16, 16, 0), 0),
296 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 3, 0,
297 (1 << IIO_CHAN_INFO_SCALE_SHARED),
298 3, 3, IIO_ST('s', 16, 16, 0), 0),
299 IIO_CHAN_SOFT_TIMESTAMP(4),
302 static const struct ad7606_chip_info ad7606_chip_info_tbl[] = {
304 * More devices added in future
309 .channels = ad7606_8_channels,
315 .channels = ad7606_6_channels,
321 .channels = ad7606_4_channels,
326 static int ad7606_request_gpios(struct ad7606_state *st)
328 struct gpio gpio_array[3] = {
330 .gpio = st->pdata->gpio_os0,
331 .flags = GPIOF_DIR_OUT | ((st->oversampling & 1) ?
332 GPIOF_INIT_HIGH : GPIOF_INIT_LOW),
333 .label = "AD7606_OS0",
336 .gpio = st->pdata->gpio_os1,
337 .flags = GPIOF_DIR_OUT | ((st->oversampling & 2) ?
338 GPIOF_INIT_HIGH : GPIOF_INIT_LOW),
339 .label = "AD7606_OS1",
342 .gpio = st->pdata->gpio_os2,
343 .flags = GPIOF_DIR_OUT | ((st->oversampling & 4) ?
344 GPIOF_INIT_HIGH : GPIOF_INIT_LOW),
345 .label = "AD7606_OS2",
350 ret = gpio_request_one(st->pdata->gpio_convst, GPIOF_OUT_INIT_LOW,
353 dev_err(st->dev, "failed to request GPIO CONVST\n");
357 ret = gpio_request_array(gpio_array, ARRAY_SIZE(gpio_array));
362 ret = gpio_request_one(st->pdata->gpio_reset, GPIOF_OUT_INIT_LOW,
365 st->have_reset = true;
367 ret = gpio_request_one(st->pdata->gpio_range, GPIOF_DIR_OUT |
368 ((st->range == 10000) ? GPIOF_INIT_HIGH :
369 GPIOF_INIT_LOW), "AD7606_RANGE");
371 st->have_range = true;
373 ret = gpio_request_one(st->pdata->gpio_stby, GPIOF_OUT_INIT_HIGH,
376 st->have_stby = true;
378 if (gpio_is_valid(st->pdata->gpio_frstdata)) {
379 ret = gpio_request_one(st->pdata->gpio_frstdata, GPIOF_IN,
382 st->have_frstdata = true;
388 static void ad7606_free_gpios(struct ad7606_state *st)
391 gpio_free(st->pdata->gpio_range);
394 gpio_free(st->pdata->gpio_stby);
397 gpio_free(st->pdata->gpio_os0);
398 gpio_free(st->pdata->gpio_os1);
399 gpio_free(st->pdata->gpio_os2);
403 gpio_free(st->pdata->gpio_reset);
405 if (st->have_frstdata)
406 gpio_free(st->pdata->gpio_frstdata);
408 gpio_free(st->pdata->gpio_convst);
414 static irqreturn_t ad7606_interrupt(int irq, void *dev_id)
416 struct iio_dev *indio_dev = dev_id;
417 struct ad7606_state *st = iio_priv(indio_dev);
419 if (iio_ring_enabled(indio_dev)) {
420 if (!work_pending(&st->poll_work))
421 schedule_work(&st->poll_work);
424 wake_up_interruptible(&st->wq_data_avail);
430 struct iio_dev *ad7606_probe(struct device *dev, int irq,
431 void __iomem *base_address,
433 const struct ad7606_bus_ops *bops)
435 struct ad7606_platform_data *pdata = dev->platform_data;
436 struct ad7606_state *st;
437 int ret, regdone = 0;
438 struct iio_dev *indio_dev = iio_allocate_device(sizeof(*st));
440 if (indio_dev == NULL) {
445 st = iio_priv(indio_dev);
451 st->base_address = base_address;
452 st->range = pdata->default_range == 10000 ? 10000 : 5000;
454 ret = ad7606_oversampling_get_index(pdata->default_os);
456 dev_warn(dev, "oversampling %d is not supported\n",
458 st->oversampling = 0;
460 st->oversampling = pdata->default_os;
463 st->reg = regulator_get(dev, "vcc");
464 if (!IS_ERR(st->reg)) {
465 ret = regulator_enable(st->reg);
471 st->chip_info = &ad7606_chip_info_tbl[id];
473 indio_dev->dev.parent = dev;
474 indio_dev->attrs = &ad7606_attribute_group;
475 indio_dev->dev_data = (void *)(st);
476 indio_dev->driver_module = THIS_MODULE;
477 indio_dev->modes = INDIO_DIRECT_MODE;
478 indio_dev->name = st->chip_info->name;
479 indio_dev->channels = st->chip_info->channels;
480 indio_dev->num_channels = st->chip_info->num_channels;
481 indio_dev->read_raw = &ad7606_read_raw;
483 init_waitqueue_head(&st->wq_data_avail);
485 ret = ad7606_request_gpios(st);
487 goto error_disable_reg;
489 ret = ad7606_reset(st);
491 dev_warn(st->dev, "failed to RESET: no RESET GPIO specified\n");
493 ret = request_irq(st->irq, ad7606_interrupt,
494 IRQF_TRIGGER_FALLING, st->chip_info->name, indio_dev);
496 goto error_free_gpios;
498 ret = ad7606_register_ring_funcs_and_init(indio_dev);
502 ret = iio_device_register(indio_dev);
507 ret = iio_ring_buffer_register_ex(indio_dev->ring, 0,
509 indio_dev->num_channels);
511 goto error_cleanup_ring;
516 ad7606_ring_cleanup(indio_dev);
519 free_irq(st->irq, indio_dev);
522 ad7606_free_gpios(st);
525 if (!IS_ERR(st->reg))
526 regulator_disable(st->reg);
528 if (!IS_ERR(st->reg))
529 regulator_put(st->reg);
531 iio_device_unregister(indio_dev);
533 iio_free_device(indio_dev);
538 int ad7606_remove(struct iio_dev *indio_dev)
540 struct ad7606_state *st = iio_priv(indio_dev);
542 iio_ring_buffer_unregister(indio_dev->ring);
543 ad7606_ring_cleanup(indio_dev);
545 free_irq(st->irq, indio_dev);
546 if (!IS_ERR(st->reg)) {
547 regulator_disable(st->reg);
548 regulator_put(st->reg);
551 ad7606_free_gpios(st);
552 iio_device_unregister(indio_dev);
557 void ad7606_suspend(struct iio_dev *indio_dev)
559 struct ad7606_state *st = iio_priv(indio_dev);
563 gpio_set_value(st->pdata->gpio_range, 1);
564 gpio_set_value(st->pdata->gpio_stby, 0);
568 void ad7606_resume(struct iio_dev *indio_dev)
570 struct ad7606_state *st = iio_priv(indio_dev);
574 gpio_set_value(st->pdata->gpio_range,
577 gpio_set_value(st->pdata->gpio_stby, 1);
582 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
583 MODULE_DESCRIPTION("Analog Devices AD7606 ADC");
584 MODULE_LICENSE("GPL v2");